Skip to content

Commit f6af7d8

Browse files
committed
fix(task): address CodeRabbit review on queued-message delivery
What: let direct (non-queued) submitUserMessage calls answer approval asks as deny-with-feedback while keeping queued drains barred, and treat a queue entry deleted mid-ack as a successful durable save. Why: the drain guard was over-applied to direct submissions, so a headless sendMessage during a command/use_mcp_server/tool approval ask threw instead of delivering the user's denial, and a user deleting the entry during the ack backoff made persistQueuedFeedbackAndAcknowledge report failure after the feedback row was durably saved. Impact: restores pre-drain direct-submission semantics; queued-message invariants unchanged. Regression tests cover both paths.
1 parent c7d0c44 commit f6af7d8

2 files changed

Lines changed: 72 additions & 8 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,11 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
11861186
}
11871187
if (confirmed) {
11881188
this.queuedFeedbackRows.delete(messageId)
1189-
return this.messageQueueService.removeMessage(messageId)
1189+
// The row is durably saved. A missing entry means the
1190+
// user deleted it during the ack (the webview remove
1191+
// handler ignores claims); that is not a save failure.
1192+
this.messageQueueService.removeMessage(messageId)
1193+
return true
11901194
}
11911195
}
11921196
if (attempt < QUEUED_FEEDBACK_SAVE_RETRY_DELAYS_MS.length) {
@@ -2821,13 +2825,26 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
28212825
return false
28222826
}
28232827

2824-
// Never overwrite a response an ask is blocked waiting on: an
2825-
// approval-gating ask must not be answered by the queue, and a
2828+
// Never overwrite a response an ask is blocked waiting on: a
28262829
// direct response that already landed in the slot must not be
28272830
// replaced (an approval would become a conversational answer).
2831+
// A queued (drain) submission must additionally never answer an
2832+
// approval ask or a failure gate on its own — a raw conversational
2833+
// post carries no user intent for those — while a direct
2834+
// submission is exactly that intent and may answer an approval
2835+
// ask (deny-with-feedback), matching the pre-drain behavior.
28282836
// Queue drains treat the returned false as "leave the message
28292837
// queued".
2830-
if (this.inFlightAskBlocksQueuedSubmission()) {
2838+
const inFlightAskGates = this.inFlightAskGates
2839+
const directAskBlocksSubmission =
2840+
inFlightAskGates !== undefined &&
2841+
[...inFlightAskGates].some(({ type, text }) => queuedResponseForAsk(type, text) === undefined)
2842+
const inFlightAskHasResponse = (inFlightAskGates?.size ?? 0) > 0 && this.askResponse !== undefined
2843+
if (
2844+
inFlightAskHasResponse ||
2845+
(sourceQueuedMessageId === undefined && directAskBlocksSubmission) ||
2846+
(sourceQueuedMessageId !== undefined && this.inFlightAskBlocksQueuedSubmission())
2847+
) {
28312848
return false
28322849
}
28332850

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

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,45 @@ describe("Task.ask queued message drain", () => {
646646
expect(task.messageQueueService.isEmpty()).toBe(true)
647647
})
648648

649+
it("returns success when the user deletes the queue entry during a failed-save backoff", async () => {
650+
const task = await createTask({ getState: async () => ({}) })
651+
652+
const askPromise = task.ask("completion_result", "Done", false)
653+
await new Promise((resolve) => setTimeout(resolve, 150))
654+
655+
task.messageQueueService.addMessage("deleted during ack")
656+
const drain = task.processQueuedMessages()
657+
658+
const result = await askPromise
659+
await drain
660+
const messageId = result.queuedMessageId!
661+
662+
const taskAccess = getQueueTaskTestAccess(task)
663+
taskAccess.addToClineMessages = async (message) => {
664+
taskAccess.clineMessages.push(message!)
665+
return true
666+
}
667+
const saveClineMessages = vi.fn().mockResolvedValue(false)
668+
taskAccess.saveClineMessages = saveClineMessages
669+
670+
vi.useFakeTimers()
671+
try {
672+
const persistence = task.persistQueuedFeedbackAndAcknowledge(messageId, result.text, result.images)
673+
// The first save fails and the retry backoff starts; while it runs,
674+
// the user deletes the entry from the queue UI (the webview remove
675+
// handler ignores claims). The next save succeeds, so the feedback
676+
// row is durable even though the cleanup removal no-ops.
677+
await vi.advanceTimersByTimeAsync(1)
678+
task.messageQueueService.removeMessage(messageId)
679+
saveClineMessages.mockResolvedValue(true)
680+
await vi.runAllTimersAsync()
681+
await expect(persistence).resolves.toBe(true)
682+
} finally {
683+
vi.useRealTimers()
684+
}
685+
expect(taskAccess.clineMessages.filter((message) => message.say === "user_feedback")).toHaveLength(1)
686+
})
687+
649688
it("associates the feedback row before the append so a throwing listener cannot strand it", async () => {
650689
const task = await createTask({ getState: async () => ({}) })
651690
task.messageQueueService.addMessage("dedupe me")
@@ -948,17 +987,25 @@ describe("Task.ask queued message drain", () => {
948987
expect(result).toMatchObject({ response: "yesButtonClicked", text: undefined })
949988
})
950989

951-
it("refuses to submit while an approval-gating ask is in flight", async () => {
990+
it("refuses a queued submission during an approval-gating ask but lets a direct one deny it", async () => {
952991
const task = await createTask({ getState: async () => ({}) })
953992

954993
const askPromise = task.ask("command", "npm test", false)
955994
await new Promise((resolve) => setTimeout(resolve, 150))
956995

957-
await expect(task.submitUserMessage("queued note")).resolves.toBe(false)
996+
// A queued submission carries no user intent and must never answer an
997+
// approval ask on its own.
998+
await expect(task.submitUserMessage("queued note", undefined, undefined, undefined, "queued-1")).resolves.toBe(
999+
false,
1000+
)
1001+
1002+
// A direct submission is explicit user intent: it answers the approval
1003+
// ask as deny-with-feedback, matching the pre-drain behavior the
1004+
// headless API relies on.
1005+
await expect(task.submitUserMessage("no, do not run it")).resolves.toBe(true)
9581006

959-
setTimeout(() => task.approveAsk(), 0)
9601007
const result = await askPromise
961-
expect(result).toMatchObject({ response: "yesButtonClicked", text: undefined })
1008+
expect(result).toMatchObject({ response: "messageResponse", text: "no, do not run it" })
9621009
})
9631010

9641011
it.each([

0 commit comments

Comments
 (0)