Skip to content

Commit a3ea9df

Browse files
committed
test(task): pin protected write-tool asks against queued-message drain
What: mirror the two protected-command drain tests for protected tool asks, using the write-tool path (alwaysAllowWrite on, alwaysAllowWriteProtected unset, isProtected set) that presentAssistantMessage uses for WriteToFileTool/EditTool/ ApplyDiff/ApplyPatch blocked writes. Why: the drain guards key on the isProtected flag, so protected write-tool asks are covered too, but only the command case was pinned (review round converged on this gap). Impact: red without the guards (4 protected tests fail), green with them (674 core/task tests pass); diff is tests only.
1 parent 1f71f98 commit a3ea9df

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

‎src/core/task/__tests__/ask-queued-message-drain.spec.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,72 @@ describe("Task.ask queued message drain", () => {
170170
expect(task.messageQueueService.claimNextMessage()?.text).toBe("mid-wait message")
171171
})
172172

173+
// The drain guards key on the isProtected flag, so they also cover protected
174+
// write-tool asks (WriteToFileTool/EditTool/ApplyDiff/ApplyPatch mark blocked
175+
// writes protected via presentAssistantMessage). These mirror the command pair
176+
// above: alwaysAllowWrite covers the write, but without alwaysAllowWriteProtected
177+
// the protection keeps the ask pending, and a queued message must not stand in
178+
// for the explicit approval.
179+
it("does not auto-approve a protected write-tool ask from a queued message", async () => {
180+
const task = await createTask({
181+
getState: async () => ({ autoApprovalEnabled: true, alwaysAllowWrite: true }),
182+
})
183+
task.messageQueueService.addMessage("write it anyway")
184+
185+
const ask = task.ask(
186+
"tool",
187+
JSON.stringify({ tool: "newFileCreated", path: "AGENTS.md" }),
188+
false,
189+
undefined,
190+
true,
191+
)
192+
193+
// The drain must not resolve the protected ask; only an explicit user
194+
// approval may.
195+
const premature = await Promise.race([
196+
ask,
197+
new Promise<"pending">((resolve) => setTimeout(() => resolve("pending"), 250)),
198+
])
199+
expect(premature).toBe("pending")
200+
expect(task.messageQueueService.isEmpty()).toBe(false)
201+
202+
task.approveAsk()
203+
const result = await ask
204+
expect(result).toMatchObject({ response: "yesButtonClicked", text: undefined })
205+
// claimNextMessage only returns unclaimed messages: the queued message must
206+
// still be present and unclaimed after the protected ask resolved.
207+
expect(task.messageQueueService.claimNextMessage()?.text).toBe("write it anyway")
208+
})
209+
210+
it("does not auto-approve a protected write-tool ask when a message is queued mid-wait", async () => {
211+
const task = await createTask({
212+
getState: async () => ({ autoApprovalEnabled: true, alwaysAllowWrite: true }),
213+
})
214+
215+
const ask = task.ask(
216+
"tool",
217+
JSON.stringify({ tool: "newFileCreated", path: "AGENTS.md" }),
218+
false,
219+
undefined,
220+
true,
221+
)
222+
task.messageQueueService.addMessage("mid-wait message")
223+
224+
const premature = await Promise.race([
225+
ask,
226+
new Promise<"pending">((resolve) => setTimeout(() => resolve("pending"), 250)),
227+
])
228+
expect(premature).toBe("pending")
229+
expect(task.messageQueueService.isEmpty()).toBe(false)
230+
231+
task.approveAsk()
232+
const result = await ask
233+
expect(result).toMatchObject({ response: "yesButtonClicked", text: undefined })
234+
// claimNextMessage only returns unclaimed messages: the queued message must
235+
// still be present and unclaimed after the protected ask resolved.
236+
expect(task.messageQueueService.claimNextMessage()?.text).toBe("mid-wait message")
237+
})
238+
173239
it("claims lifecycle feedback that arrives while an ask is waiting", async () => {
174240
const task = await createTask()
175241
const ask = task.ask("tool", JSON.stringify({ tool: "finishTask" }), false)

0 commit comments

Comments
 (0)