Skip to content

Commit 0d6b47c

Browse files
committed
fix(task): record MCP rejection in per-call context and cover terminal operations
1 parent 5a7832a commit 0d6b47c

5 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎src/core/assistant-message/__tests__/presentAssistantMessage-auto-deny.spec.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ describe("presentAssistantMessage - automatic (policy) denials", () => {
602602
expect(payload.message).toBe("The user denied this operation.")
603603
expect(payload.type).toBeUndefined()
604604
expect(mockTask.didRejectTool).toBe(true)
605+
expect(mockTask.activeToolContexts.get("call_mcp")).toEqual({ toolCallId: "call_mcp", didReject: true })
605606
expect(mockTask.say).not.toHaveBeenCalledWith("user_feedback", expect.anything(), expect.anything())
606607
})
607608

@@ -666,6 +667,7 @@ describe("presentAssistantMessage - automatic (policy) denials", () => {
666667
expect(payload.rule_id).toBe("R-1")
667668
expect(mcpApproval).toBe(false)
668669
expect(mockTask.didRejectTool).toBe(false)
670+
expect(mockTask.activeToolContexts.get("call_mcp")).toEqual({ toolCallId: "call_mcp", didReject: false })
669671
})
670672

671673
it("denies dangerous_substitution with the fixed shell-expansion reason without aborting the turn", async () => {

‎src/core/assistant-message/presentAssistantMessage.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ async function presentAssistantMessageBlock(cline: Task, policySnapshot: Request
188188
// Track if we've already pushed a tool result
189189
let hasToolResult = false
190190
const toolCallId = mcpBlock.id
191+
if (toolCallId && !cline.activeToolContexts.has(toolCallId)) {
192+
cline.activeToolContexts.set(toolCallId, { toolCallId, didReject: false })
193+
}
191194

192195
// Store approval feedback to merge into tool result (GitHub #10465)
193196
let approvalFeedback: { text: string; images?: string[] } | undefined
@@ -287,7 +290,7 @@ async function presentAssistantMessageBlock(cline: Task, policySnapshot: Request
287290
} else {
288291
pushToolResult(formatResponse.toolDenied())
289292
}
290-
cline.didRejectTool = true
293+
markToolRejected(cline, toolCallId ?? "")
291294
return false
292295
}
293296

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Task } from "../Task"
2+
3+
function buildTask() {
4+
const task = Object.create(Task.prototype) as Task
5+
task.terminalProcesses = new Map()
6+
return task
7+
}
8+
9+
function stubProcess() {
10+
return { continue: vi.fn(), abort: vi.fn() }
11+
}
12+
13+
describe("Task.handleTerminalOperation", () => {
14+
it.each([
15+
["continue", "abort"],
16+
["abort", "continue"],
17+
] as const)("%s reaches every running terminal process exactly once", async (operation, other) => {
18+
const task = buildTask()
19+
const first = stubProcess()
20+
const second = stubProcess()
21+
task.terminalProcesses.set("call_a", first as never)
22+
task.terminalProcesses.set("call_b", second as never)
23+
24+
await task.handleTerminalOperation(operation)
25+
26+
expect(first[operation]).toHaveBeenCalledTimes(1)
27+
expect(second[operation]).toHaveBeenCalledTimes(1)
28+
expect(first[other]).not.toHaveBeenCalled()
29+
expect(second[other]).not.toHaveBeenCalled()
30+
})
31+
32+
it.each(["continue", "abort"] as const)(
33+
"%s does nothing when no terminal process is running",
34+
async (operation) => {
35+
await expect(buildTask().handleTerminalOperation(operation)).resolves.toBeUndefined()
36+
},
37+
)
38+
})

‎src/core/task/__tests__/ToolExecutionContext.spec.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { describe, it, expect, beforeEach, vi } from "vitest"
55
import { presentAssistantMessage } from "../../assistant-message/presentAssistantMessage"
66
import { validateToolUse } from "../../tools/validateToolUse"
77
import type { Task, ToolExecutionContext } from "../Task"
8+
import type { RequestPolicySnapshot } from "../../prompts/tools/effective-tool-policy"
89

910
vi.mock("../Task")
1011
vi.mock("../../tools/validateToolUse", async (importOriginal) => {
@@ -85,6 +86,11 @@ const listFilesBlock = (id: string) => ({
8586
partial: false,
8687
})
8788

89+
const baseSnapshot: RequestPolicySnapshot = {
90+
disabledTools: [],
91+
customModes: [],
92+
}
93+
8894
const asTask = (task: ReturnType<typeof buildMockTask>): Task => task as unknown as Task
8995

9096
describe("ToolExecutionContext", () => {
@@ -123,7 +129,7 @@ describe("ToolExecutionContext", () => {
123129
},
124130
)
125131

126-
await presentAssistantMessage(asTask(mockTask))
132+
await presentAssistantMessage(asTask(mockTask), baseSnapshot)
127133

128134
expect(contextsSeenDuringExecution.get("call_a")).toBe(mockTask.activeToolContexts.get("call_a"))
129135
expect(mockTask.activeToolContexts.get("call_a")).toEqual({
@@ -135,11 +141,11 @@ describe("ToolExecutionContext", () => {
135141

136142
it("reuses the context when a partial tool call is followed by its complete block", async () => {
137143
mockTask.assistantMessageContent = [{ ...listFilesBlock("call_a"), partial: true }]
138-
await presentAssistantMessage(asTask(mockTask))
144+
await presentAssistantMessage(asTask(mockTask), baseSnapshot)
139145
const first = mockTask.activeToolContexts.get("call_a")
140146

141147
mockTask.assistantMessageContent = [listFilesBlock("call_a")]
142-
await presentAssistantMessage(asTask(mockTask))
148+
await presentAssistantMessage(asTask(mockTask), baseSnapshot)
143149

144150
expect(first).toBeDefined()
145151
expect(mockTask.activeToolContexts.get("call_a")).toBe(first)
@@ -149,11 +155,11 @@ describe("ToolExecutionContext", () => {
149155
mockTask.assistantMessageContent = [listFilesBlock("call_a"), listFilesBlock("call_b")]
150156
mockTask.ask.mockResolvedValueOnce({ response: "noButtonClicked" })
151157

152-
await presentAssistantMessage(asTask(mockTask))
158+
await presentAssistantMessage(asTask(mockTask), baseSnapshot)
153159
mockTask.didRejectTool = false
154160
mockTask.presentAssistantMessageLocked = false
155161
mockTask.currentStreamingContentIndex = 1
156-
await presentAssistantMessage(asTask(mockTask))
162+
await presentAssistantMessage(asTask(mockTask), baseSnapshot)
157163

158164
expect(mockTask.activeToolContexts.get("call_a")?.didReject).toBe(true)
159165
expect(mockTask.activeToolContexts.get("call_b")?.didReject).toBe(false)
@@ -163,7 +169,7 @@ describe("ToolExecutionContext", () => {
163169
mockTask.assistantMessageContent = [listFilesBlock("call_a")]
164170
mockTask.ask.mockResolvedValue({ response: "noButtonClicked" })
165171

166-
await presentAssistantMessage(asTask(mockTask))
172+
await presentAssistantMessage(asTask(mockTask), baseSnapshot)
167173

168174
expect(mockTask.didRejectTool).toBe(true)
169175
expect(mockTask.activeToolContexts.get("call_a")?.didReject).toBe(true)

‎src/core/tools/__tests__/readFileTool.spec.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ describe("ReadFileTool", () => {
828828
expect(mockTask.didRejectTool).toBe(true)
829829
expect(context.didReject).toBe(true)
830830
expect(updateFileResult).toHaveBeenCalledWith("file1.ts", expect.objectContaining({ status: "denied" }))
831+
expect(updateFileResult).toHaveBeenCalledWith("file2.ts", expect.objectContaining({ status: "denied" }))
831832
})
832833
})
833834

0 commit comments

Comments
 (0)