Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ interface MockTask {
didCompleteReadingStream: boolean
didRejectTool: boolean
didAlreadyUseTool: boolean
activeToolContexts: Map<string, unknown>
consecutiveMistakeCount: number
clineMessages: unknown[]
api: { getModel: () => { id: string; info: Record<string, unknown> } }
Expand Down Expand Up @@ -109,6 +110,7 @@ function buildMockTask(): MockTask {
didCompleteReadingStream: true,
didRejectTool: false,
didAlreadyUseTool: false,
activeToolContexts: new Map(),
consecutiveMistakeCount: 0,
clineMessages: [],
api: {
Expand Down Expand Up @@ -600,6 +602,7 @@ describe("presentAssistantMessage - automatic (policy) denials", () => {
expect(payload.message).toBe("The user denied this operation.")
expect(payload.type).toBeUndefined()
expect(mockTask.didRejectTool).toBe(true)
expect(mockTask.activeToolContexts.get("call_mcp")).toEqual({ toolCallId: "call_mcp", didReject: true })
expect(mockTask.say).not.toHaveBeenCalledWith("user_feedback", expect.anything(), expect.anything())
})

Expand Down Expand Up @@ -664,6 +667,7 @@ describe("presentAssistantMessage - automatic (policy) denials", () => {
expect(payload.rule_id).toBe("R-1")
expect(mcpApproval).toBe(false)
expect(mockTask.didRejectTool).toBe(false)
expect(mockTask.activeToolContexts.get("call_mcp")).toEqual({ toolCallId: "call_mcp", didReject: false })
})

it("denies dangerous_substitution with the fixed shell-expansion reason without aborting the turn", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
didCompleteReadingStream: false,
didRejectTool: false,
didAlreadyUseTool: false,
activeToolContexts: new Map(),
consecutiveMistakeCount: 0,
clineMessages: [],
getTaskMode: vi.fn().mockResolvedValue("code"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe("presentAssistantMessage - Image Handling in Native Tool Calling", () =
didCompleteReadingStream: false,
didRejectTool: false,
didAlreadyUseTool: false,
activeToolContexts: new Map(),
consecutiveMistakeCount: 0,
getTaskMode: vi.fn().mockResolvedValue("code"),
api: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe("presentAssistantMessage - Tool Repetition Detection", () => {
didCompleteReadingStream: false,
didRejectTool: false,
didAlreadyUseTool: false,
activeToolContexts: new Map(),
consecutiveMistakeCount: 0,
consecutiveMistakeLimit: HARD_LIMIT,
clineMessages: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ interface MockTask {
didCompleteReadingStream: boolean
didRejectTool: boolean
didAlreadyUseTool: boolean
activeToolContexts: Map<string, unknown>
consecutiveMistakeCount: number
clineMessages: unknown[]
getTaskMode: Mock<() => Promise<string>>
Expand Down Expand Up @@ -107,6 +108,7 @@ describe("presentAssistantMessage - tool usage attribution", () => {
didCompleteReadingStream: false,
didRejectTool: false,
didAlreadyUseTool: false,
activeToolContexts: new Map(),
consecutiveMistakeCount: 0,
clineMessages: [],
getTaskMode: vi.fn().mockResolvedValue("code"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe("presentAssistantMessage - Unknown Tool Handling", () => {
didCompleteReadingStream: false,
didRejectTool: false,
didAlreadyUseTool: false,
activeToolContexts: new Map(),
consecutiveMistakeCount: 0,
clineMessages: [],
getTaskMode: vi.fn().mockResolvedValue("code"),
Expand Down
37 changes: 33 additions & 4 deletions src/core/assistant-message/presentAssistantMessage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { markToolRejected } from "../task/toolExecutionContext"
import { serializeError } from "serialize-error"
import { Anthropic } from "@anthropic-ai/sdk"

Expand Down Expand Up @@ -187,6 +188,9 @@
// Track if we've already pushed a tool result
let hasToolResult = false
const toolCallId = mcpBlock.id
if (toolCallId && !cline.activeToolContexts.has(toolCallId)) {
cline.activeToolContexts.set(toolCallId, { toolCallId, didReject: false })
}

// Store approval feedback to merge into tool result (GitHub #10465)
let approvalFeedback: { text: string; images?: string[] } | undefined
Expand Down Expand Up @@ -286,7 +290,7 @@
} else {
pushToolResult(formatResponse.toolDenied())
}
cline.didRejectTool = true
markToolRejected(cline, toolCallId ?? "")

Check warning on line 293 in src/core/assistant-message/presentAssistantMessage.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/core/assistant-message/presentAssistantMessage.ts:293: NoCoverage StringLiteral mutant (replacement: "Stryker was here!"). See the job summary for the complete list and resolution guidance.
return false
}

Expand Down Expand Up @@ -411,6 +415,7 @@
askApproval,
handleError,
pushToolResult,
toolCallId: toolCallId ?? "",

Check warning on line 418 in src/core/assistant-message/presentAssistantMessage.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/core/assistant-message/presentAssistantMessage.ts:418: NoCoverage StringLiteral mutant (replacement: "Stryker was here!"). See the job summary for the complete list and resolution guidance.
onValidated: mcpBlock.partial
? undefined
: () => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Expand Down Expand Up @@ -586,6 +591,10 @@
}
}

if (!cline.activeToolContexts.has(toolCallId)) {
cline.activeToolContexts.set(toolCallId, { toolCallId, didReject: false })
}

// Store approval feedback to merge into tool result (GitHub #10465)
let approvalFeedback: { text: string; images?: string[] } | undefined

Expand Down Expand Up @@ -689,7 +698,7 @@
} else {
pushToolResult(formatResponse.toolDenied())
}
cline.didRejectTool = true
markToolRejected(cline, toolCallId)
return false
}

Expand Down Expand Up @@ -875,13 +884,15 @@
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "update_todo_list":
await updateTodoListTool.handle(cline, block as ToolUse<"update_todo_list">, {
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "apply_diff":
Expand All @@ -890,6 +901,7 @@
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "edit":
Expand All @@ -899,6 +911,7 @@
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "search_replace":
Expand All @@ -907,6 +920,7 @@
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "edit_file":
Expand All @@ -915,6 +929,7 @@
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "apply_patch":
Expand All @@ -923,6 +938,7 @@
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "read_file":
Expand All @@ -931,69 +947,79 @@
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "list_files":
await listFilesTool.handle(cline, block as ToolUse<"list_files">, {
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "codebase_search":
await codebaseSearchTool.handle(cline, block as ToolUse<"codebase_search">, {
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "search_files":
await searchFilesTool.handle(cline, block as ToolUse<"search_files">, {
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "execute_command":
await executeCommandTool.handle(cline, block as ToolUse<"execute_command">, {
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "read_command_output":
await readCommandOutputTool.handle(cline, block as ToolUse<"read_command_output">, {
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "use_mcp_tool":
await useMcpToolTool.handle(cline, block as ToolUse<"use_mcp_tool">, {
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "access_mcp_resource":
await accessMcpResourceTool.handle(cline, block as ToolUse<"access_mcp_resource">, {
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "ask_followup_question":
await askFollowupQuestionTool.handle(cline, block as ToolUse<"ask_followup_question">, {
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "switch_mode":
await switchModeTool.handle(cline, block as ToolUse<"switch_mode">, {
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "new_task":
Expand All @@ -1002,7 +1028,7 @@
askApproval,
handleError,
pushToolResult,
toolCallId: block.id,
toolCallId,
})
break
case "attempt_completion": {
Expand All @@ -1012,7 +1038,7 @@
pushToolResult,
askFinishSubTaskApproval,
toolDescription,
toolCallId: block.id,
toolCallId,
}
await attemptCompletionTool.handle(
cline,
Expand All @@ -1026,13 +1052,15 @@
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "skill":
await skillTool.handle(cline, block as ToolUse<"skill">, {
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
case "generate_image":
Expand All @@ -1041,6 +1069,7 @@
askApproval,
handleError,
pushToolResult,
toolCallId,
})
break
default: {
Expand Down
13 changes: 10 additions & 3 deletions src/core/task/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@
const FORCED_CONTEXT_REDUCTION_PERCENT = 75 // Keep 75% of context (remove 25%) on context window errors
const MAX_CONTEXT_WINDOW_RETRIES = 3 // Maximum retries for context window errors

export interface ToolExecutionContext {
toolCallId: string
didReject: boolean
}

export interface TaskOptions extends CreateTaskOptions {
provider: ClineProvider
apiConfiguration: ProviderSettings
Expand Down Expand Up @@ -420,7 +425,8 @@
rooIgnoreController?: RooIgnoreController
rooProtectedController?: RooProtectedController
fileContextTracker: FileContextTracker
terminalProcess?: RooTerminalProcess
terminalProcesses = new Map<string, RooTerminalProcess>()
activeToolContexts = new Map<string, ToolExecutionContext>()

// Editing
diffViewProvider: DiffViewProvider
Expand Down Expand Up @@ -2535,9 +2541,9 @@

async handleTerminalOperation(terminalOperation: "continue" | "abort") {
if (terminalOperation === "continue") {
this.terminalProcess?.continue()
for (const process of this.terminalProcesses.values()) process.continue()
} else if (terminalOperation === "abort") {
this.terminalProcess?.abort()
for (const process of this.terminalProcesses.values()) process.abort()
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
}

Expand Down Expand Up @@ -4021,6 +4027,7 @@
this.didCompleteReadingStream = false
this.userMessageContent = []
this.userMessageContentReady = false
this.activeToolContexts.clear()

Check warning on line 4030 in src/core/task/Task.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/core/task/Task.ts:4030: Survived CallExpression mutant (replacement: ;). See the job summary for the complete list and resolution guidance.
this.didRejectTool = false
this.didAlreadyUseTool = false
this.assistantMessageSavedToHistory = false
Expand Down
38 changes: 38 additions & 0 deletions src/core/task/__tests__/Task.terminal-operation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Task } from "../Task"

function buildTask() {
const task = Object.create(Task.prototype) as Task
task.terminalProcesses = new Map()
return task
}

function stubProcess() {
return { continue: vi.fn(), abort: vi.fn() }
}

describe("Task.handleTerminalOperation", () => {
it.each([
["continue", "abort"],
["abort", "continue"],
] as const)("%s reaches every running terminal process exactly once", async (operation, other) => {
const task = buildTask()
const first = stubProcess()
const second = stubProcess()
task.terminalProcesses.set("call_a", first as never)
task.terminalProcesses.set("call_b", second as never)

await task.handleTerminalOperation(operation)

expect(first[operation]).toHaveBeenCalledTimes(1)
expect(second[operation]).toHaveBeenCalledTimes(1)
expect(first[other]).not.toHaveBeenCalled()
expect(second[other]).not.toHaveBeenCalled()
})

it.each(["continue", "abort"] as const)(
"%s does nothing when no terminal process is running",
async (operation) => {
await expect(buildTask().handleTerminalOperation(operation)).resolves.toBeUndefined()
},
)
})
Loading
Loading