Skip to content

Commit 9ad099f

Browse files
author
Zoo (VP)
committed
fix(task): apply command protection checks in queued-message drain
1 parent bac8adc commit 9ad099f

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,8 +1458,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
14581458
// rendered, leaving them stuck on-screen).
14591459
const provider = this.providerRef.deref()
14601460
const state = provider ? await provider.getState() : undefined
1461+
// A queued message must not short-circuit protected asks (e.g.
1462+
// DCG-blocked commands): claiming one here skips checkAutoApproval, and
1463+
// the drain below would auto-approve what protection intentionally
1464+
// leaves pending for explicit user approval.
14611465
const queuedMessage =
1462-
partial === true || type === "command_output" ? undefined : this.messageQueueService.claimNextMessage()
1466+
partial === true || type === "command_output" || isProtected
1467+
? undefined
1468+
: this.messageQueueService.claimNextMessage()
14631469
const queuedAskResolution = queuedMessage ? queuedResponseForAsk(type, text) : undefined
14641470
// `this.cwd`, not `provider.cwd`:
14651471
// The path inside `text` was made relative to this task's workspace,
@@ -1657,8 +1663,9 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
16571663

16581664
// If a queued message arrives while we're blocked on an ask (e.g. a follow-up
16591665
// suggestion click that was incorrectly queued due to UI state), consume it
1660-
// immediately so the task doesn't hang.
1661-
if (shouldDrainQueuedMessageForAsk && !this.messageQueueService.isEmpty()) {
1666+
// immediately so the task doesn't hang. Protected asks are exempt: like the
1667+
// pre-block drain above, they must wait for explicit user approval.
1668+
if (shouldDrainQueuedMessageForAsk && !isProtected && !this.messageQueueService.isEmpty()) {
16621669
const message = this.messageQueueService.claimNextMessage()
16631670
const resolution = message ? queuedResponseForAsk(type, text) : undefined
16641671
if (message && resolution) {

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,50 @@ describe("Task.ask queued message drain", () => {
122122
expect(task.messageQueueService.isEmpty()).toBe(true)
123123
})
124124

125+
it("does not auto-approve a protected command ask from a queued message", async () => {
126+
const task = await createTask({
127+
getState: async () => ({ autoApprovalEnabled: true, alwaysAllowExecute: true }),
128+
})
129+
task.messageQueueService.addMessage("run it anyway")
130+
131+
const ask = task.ask("command", "rm -rf build", false, undefined, true)
132+
133+
// The drain must not resolve the protected ask; only an explicit user
134+
// approval may.
135+
const premature = await Promise.race([
136+
ask,
137+
new Promise<"pending">((resolve) => setTimeout(() => resolve("pending"), 250)),
138+
])
139+
expect(premature).toBe("pending")
140+
expect(task.messageQueueService.isEmpty()).toBe(false)
141+
142+
task.approveAsk()
143+
const result = await ask
144+
expect(result).toMatchObject({ response: "yesButtonClicked", text: undefined })
145+
expect(task.messageQueueService.messages[0]?.text).toBe("run it anyway")
146+
})
147+
148+
it("does not auto-approve a protected command ask when a message is queued mid-wait", async () => {
149+
const task = await createTask({
150+
getState: async () => ({ autoApprovalEnabled: true, alwaysAllowExecute: true }),
151+
})
152+
153+
const ask = task.ask("command", "rm -rf build", false, undefined, true)
154+
task.messageQueueService.addMessage("mid-wait message")
155+
156+
const premature = await Promise.race([
157+
ask,
158+
new Promise<"pending">((resolve) => setTimeout(() => resolve("pending"), 250)),
159+
])
160+
expect(premature).toBe("pending")
161+
expect(task.messageQueueService.isEmpty()).toBe(false)
162+
163+
task.approveAsk()
164+
const result = await ask
165+
expect(result).toMatchObject({ response: "yesButtonClicked", text: undefined })
166+
expect(task.messageQueueService.messages[0]?.text).toBe("mid-wait message")
167+
})
168+
125169
it("claims lifecycle feedback that arrives while an ask is waiting", async () => {
126170
const task = await createTask()
127171
const ask = task.ask("tool", JSON.stringify({ tool: "finishTask" }), false)

0 commit comments

Comments
 (0)