Skip to content

Commit 00479ac

Browse files
committed
test(task): tighten rejection isolation and batch read denial coverage
1 parent 0d6b47c commit 00479ac

2 files changed

Lines changed: 54 additions & 13 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,19 @@ describe("ToolExecutionContext", () => {
151151
expect(mockTask.activeToolContexts.get("call_a")).toBe(first)
152152
})
153153

154-
it("sets didReject on the rejected tool's context in the map, not on a sibling's", async () => {
154+
it("sets didReject on the rejected tool's context only, and the serial flag keeps skipping the sibling", async () => {
155155
mockTask.assistantMessageContent = [listFilesBlock("call_a"), listFilesBlock("call_b")]
156+
mockTask.activeToolContexts.set("call_a", { toolCallId: "call_a", didReject: false })
157+
mockTask.activeToolContexts.set("call_b", { toolCallId: "call_b", didReject: false })
156158
mockTask.ask.mockResolvedValueOnce({ response: "noButtonClicked" })
157159

158160
await presentAssistantMessage(asTask(mockTask), baseSnapshot)
159-
mockTask.didRejectTool = false
160161
mockTask.presentAssistantMessageLocked = false
161162
mockTask.currentStreamingContentIndex = 1
162163
await presentAssistantMessage(asTask(mockTask), baseSnapshot)
163164

165+
expect(mockTask.didRejectTool).toBe(true)
166+
expect(listFilesHandle).toHaveBeenCalledTimes(1)
164167
expect(mockTask.activeToolContexts.get("call_a")?.didReject).toBe(true)
165168
expect(mockTask.activeToolContexts.get("call_b")?.didReject).toBe(false)
166169
})

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

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import path from "path"
1616

1717
import type { ToolUse } from "../../../shared/tools"
18-
import type { Task } from "../../task/Task"
18+
import type { Task, ToolExecutionContext } from "../../task/Task"
1919

2020
import { isBinaryFile } from "isbinaryfile"
2121

@@ -154,7 +154,7 @@ function createMockTask(options: MockTaskOptions = {}) {
154154
consecutiveMistakeCount: 0,
155155
didToolFailInCurrentTurn: false,
156156
didRejectTool: false,
157-
activeToolContexts: new Map(),
157+
activeToolContexts: new Map<string, ToolExecutionContext>(),
158158
ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked", text: undefined, images: undefined }),
159159
say: vi.fn().mockResolvedValue(undefined),
160160
sayAndCreateMissingParamError: vi.fn().mockResolvedValue("Missing required parameter: path"),
@@ -804,18 +804,22 @@ describe("ReadFileTool", () => {
804804
})
805805

806806
describe("batch approval denial", () => {
807-
it.each([
808-
["the user clicks no", { response: "noButtonClicked", text: undefined, images: undefined }],
809-
["individual permissions deny a file", { response: "messageResponse", text: "{}", images: undefined }],
810-
[
811-
"individual permissions are not valid JSON",
812-
{ response: "messageResponse", text: "not json", images: undefined },
813-
],
814-
])("marks the tool call rejected when %s", async (_name, askResult) => {
807+
const requestBatchApproval = async (answer: {
808+
response: string
809+
text?: string
810+
approvedIndexes?: number[]
811+
}) => {
815812
const mockTask = createMockTask()
816813
const context = { toolCallId: "batch-call", didReject: false }
817814
mockTask.activeToolContexts.set("batch-call", context)
818-
mockTask.ask.mockResolvedValue(askResult)
815+
mockTask.ask.mockImplementation(async (_type: string, message: string) => {
816+
if (!answer.approvedIndexes) return { response: answer.response, text: answer.text }
817+
const { batchFiles } = JSON.parse(message) as { batchFiles: { key: string }[] }
818+
const permissions = Object.fromEntries(
819+
batchFiles.map((file, index) => [file.key, answer.approvedIndexes!.includes(index)]),
820+
)
821+
return { response: answer.response, text: JSON.stringify(permissions) }
822+
})
819823
const updateFileResult = vi.fn()
820824
const filesToApprove = ["file1.ts", "file2.ts"].map((path) => ({
821825
path,
@@ -825,11 +829,45 @@ describe("ReadFileTool", () => {
825829

826830
await readFileTool["requestApproval"](mockTask as never, "batch-call", filesToApprove, updateFileResult)
827831

832+
return { mockTask, context, updateFileResult }
833+
}
834+
835+
it.each([
836+
["the user clicks no", { response: "noButtonClicked" }],
837+
["individual permissions deny every file", { response: "messageResponse", text: "{}" }],
838+
["individual permissions are not valid JSON", { response: "messageResponse", text: "not json" }],
839+
])("marks the tool call rejected and denies both files when %s", async (_name, askResult) => {
840+
const { mockTask, context, updateFileResult } = await requestBatchApproval(askResult)
841+
828842
expect(mockTask.didRejectTool).toBe(true)
829843
expect(context.didReject).toBe(true)
830844
expect(updateFileResult).toHaveBeenCalledWith("file1.ts", expect.objectContaining({ status: "denied" }))
831845
expect(updateFileResult).toHaveBeenCalledWith("file2.ts", expect.objectContaining({ status: "denied" }))
832846
})
847+
848+
it("marks the tool call rejected when individual permissions approve one file and deny the other", async () => {
849+
const { mockTask, context, updateFileResult } = await requestBatchApproval({
850+
response: "messageResponse",
851+
approvedIndexes: [0],
852+
})
853+
854+
expect(mockTask.didRejectTool).toBe(true)
855+
expect(context.didReject).toBe(true)
856+
expect(updateFileResult).toHaveBeenCalledWith("file1.ts", { status: "approved" })
857+
expect(updateFileResult).toHaveBeenCalledWith("file2.ts", expect.objectContaining({ status: "denied" }))
858+
})
859+
860+
it("leaves the tool call unrejected when individual permissions approve every file", async () => {
861+
const { mockTask, context, updateFileResult } = await requestBatchApproval({
862+
response: "messageResponse",
863+
approvedIndexes: [0, 1],
864+
})
865+
866+
expect(mockTask.didRejectTool).toBe(false)
867+
expect(context.didReject).toBe(false)
868+
expect(updateFileResult).toHaveBeenCalledWith("file1.ts", { status: "approved" })
869+
expect(updateFileResult).toHaveBeenCalledWith("file2.ts", { status: "approved" })
870+
})
833871
})
834872

835873
it("should block rooignore-protected files in legacy format", async () => {

0 commit comments

Comments
 (0)