Skip to content

Commit 352b8e2

Browse files
committed
fix(task): check abort before an ask row posts
1 parent a077066 commit 352b8e2

3 files changed

Lines changed: 98 additions & 2 deletions

File tree

‎docs/architecture/task-lifecycle-run-state-kernel-tickets.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ Shadow mode must report disagreement. Before any read flips, the kernel must agr
316316
- [ ] Gate the comparison as described in the Rollout gate section. Do not gate the kernel.
317317
- [ ] Do not throw on divergence in production. Log only.
318318

319-
Exit criteria: a forced divergence in a test produces one log line and one metric increment. A forced R-3 window (an abort during the `checkAutoApproval` await, before RSK-20) produces a tagged mismatch, not an untagged one.
319+
Exit criteria: a forced divergence in a test produces one log line and one metric increment. A forced divergence on a known race that still has an open fix ticket produces a tagged mismatch, not an untagged one.
320320

321321
Reversibility: additive observability.
322322

‎src/core/task/Task.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
15421542
// simply removes the reference to this instance, but the instance is
15431543
// still alive until this promise resolves or rejects.)
15441544
if (this.abort) {
1545-
throw new Error(`[RooCode#ask] task ${this.taskId}.${this.instanceId} aborted`)
1545+
throw new Error(`[ZooCode#ask] task ${this.taskId}.${this.instanceId} aborted`)
15461546
}
15471547

15481548
let askTs: number
@@ -1568,6 +1568,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
15681568
const isAutoAnswered = approval.decision === "approve" || approval.decision === "deny"
15691569
const autoApprovalDecision = isAutoAnswered ? approval.decision : undefined
15701570

1571+
// Re-check: an abort during the getState/checkAutoApproval awaits must not post an ask row.
1572+
if (this.abort) {
1573+
if (queuedMessage) {
1574+
this.messageQueueService.releaseMessage(queuedMessage.id)
1575+
}
1576+
throw new Error(`[ZooCode#ask] task ${this.taskId}.${this.instanceId} aborted`)
1577+
}
1578+
15711579
if (partial !== undefined) {
15721580
const lastMessage = this.clineMessages.at(-1)
15731581

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import type { ClineMessage } from "@roo-code/types"
2+
3+
import { MessageQueueService } from "../../message-queue/MessageQueueService"
4+
import { Task } from "../Task"
5+
6+
function buildTask(getState: () => Promise<Record<string, unknown>>) {
7+
const task = Object.create(Task.prototype) as Task
8+
task["abort"] = false
9+
task["clineMessages"] = []
10+
task["lastMessageTs"] = undefined
11+
task["addToClineMessages"] = vi.fn(async () => {})
12+
task["saveClineMessages"] = vi.fn(async () => true)
13+
task["updateClineMessage"] = vi.fn(async () => {})
14+
// Double assertion: `providerRef` is a `WeakRef<ClineProvider>`; `Task.ask` only calls `deref()` and `getState()`.
15+
task["providerRef"] = { deref: () => ({ getState }) } as unknown as Task["providerRef"]
16+
Object.defineProperty(task, "messageQueueService", { value: new MessageQueueService() })
17+
return task
18+
}
19+
20+
function deferred() {
21+
let release!: () => void
22+
const promise = new Promise<void>((resolve) => {
23+
release = resolve
24+
})
25+
return { promise, release }
26+
}
27+
28+
describe("Task.ask abort during the auto-approval await", () => {
29+
it.each([
30+
["complete", false],
31+
["undefined", undefined],
32+
["partial", true],
33+
] as const)("does not add an ask row when aborted mid-await (partial=%s)", async (_label, partial) => {
34+
const gate = deferred()
35+
const task = buildTask(async () => {
36+
await gate.promise
37+
return { autoApprovalEnabled: true, alwaysAllowReadOnly: true }
38+
})
39+
40+
const askPromise = task.ask("tool", JSON.stringify({ tool: "readFile" }), partial)
41+
task["abort"] = true
42+
gate.release()
43+
44+
await expect(askPromise).rejects.toThrow(/aborted/)
45+
expect(task["addToClineMessages"]).not.toHaveBeenCalled()
46+
expect(task["lastMessageTs"]).toBeUndefined()
47+
})
48+
49+
it.each([
50+
["partial update", true],
51+
["partial completion", false],
52+
] as const)("leaves an existing partial row untouched on %s when aborted mid-await", async (_label, partial) => {
53+
const gate = deferred()
54+
const task = buildTask(async () => {
55+
await gate.promise
56+
return {}
57+
})
58+
const original: ClineMessage = { ts: 1, type: "ask", ask: "followup", text: "old", partial: true }
59+
task["clineMessages"] = [{ ...original }]
60+
61+
const askPromise = task.ask("followup", "new", partial)
62+
task["abort"] = true
63+
gate.release()
64+
65+
await expect(askPromise).rejects.toThrow(/aborted/)
66+
expect(task["clineMessages"]).toEqual([original])
67+
expect(task["saveClineMessages"]).not.toHaveBeenCalled()
68+
expect(task["updateClineMessage"]).not.toHaveBeenCalled()
69+
expect(task["lastMessageTs"]).toBeUndefined()
70+
})
71+
72+
it("releases the claimed queued message when aborted mid-await", async () => {
73+
const gate = deferred()
74+
const task = buildTask(async () => {
75+
await gate.promise
76+
return {}
77+
})
78+
task.messageQueueService.addMessage("queued feedback")
79+
80+
const askPromise = task.ask("followup", "Q?", false)
81+
task["abort"] = true
82+
gate.release()
83+
84+
await expect(askPromise).rejects.toThrow(/aborted/)
85+
expect(task["addToClineMessages"]).not.toHaveBeenCalled()
86+
expect(task.messageQueueService.claimNextMessage()?.text).toBe("queued feedback")
87+
})
88+
})

0 commit comments

Comments
 (0)