Skip to content

Commit 52d9e79

Browse files
committed
[Fix] Keep drain retention through a failed durable ack and bind queue ingress to its task
What: - Task.persistQueuedFeedbackAndAcknowledge now settles the consume through `consumeQueuedMessage` (remove + clear retention) only after the history save succeeds, and reports success from that save rather than from the queue removal. Every non-consumed unwind goes through `releaseUnconsumedQueuedMessage`, which re-arms `drainRetainedQueuedMessageIds` before releasing the claim. - `handleQueuedAskResponse` no longer deletes the retention marker at the durable-ack edge; the delete moved to the successful-remove edge. - The two `ask()` unwind paths (abort, supersession) release through the same helper. - `webviewMessageHandler` binds `provider.getCurrentTask()` before the await in `queueMessage` and `askResponse`, so a task switch during image resolution cannot deliver the message to the other task. - `claimAndSubmitNextQueuedMessage` returns false when the queue head is already retained (nothing was submitted), and the `@returns` doc plus the deferral comment now describe the real delivery path. Why: - The retention marker was deleted before persistence completed. On a persistent `saveClineMessages()` failure the message re-entered the pool unmarked, and the next command ask claimed it, skipped `checkAutoApproval`, and could execute on stale text that was never answered to that command. That is the exact silent approval this PR exists to prevent, reachable through a storage fault. - Deriving the ack result from the save also fixes the false failure when the user deletes the queue entry while the ack is in flight. - The post-await `getCurrentTask()` re-read let a queued message land in a task it was not written for, where its text could answer that task's command ask. Impact: - No public signature change. `processQueuedMessages()` now reports false for a retained head instead of true; no production caller reads the boolean. - Non-test changed lines vs the PR base stay at 500/500 (doc comments compacted to offset the additions). - Tests: new regression for the failed-durable-ack path and for the A->B deferred delivery; new case proving a direct response survives a re-post attempt; two drain-semantics assertions updated to the truthful return.
1 parent 0bbba8d commit 52d9e79

6 files changed

Lines changed: 242 additions & 142 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 90 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -509,23 +509,16 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
509509
// Message Queue Service
510510
public readonly messageQueueService: MessageQueueService
511511
private messageQueueStateChangedHandler: (() => void) | undefined
512-
// Serializes queued-message drains: a drain claims and submits only after
513-
// the previous drain's submission handoff finished, so two concurrent
514-
// drains cannot submit different messages into the single ask-response
515-
// slot (which would drop the earlier response after both messages were
516-
// already removed from the queue).
512+
// Serializes drains so two of them cannot submit into the single ask-response
513+
// slot and drop the earlier response.
517514
private queuedMessageDrainChain: Promise<unknown> = Promise.resolve()
518-
// IDs of queued messages a drain submitted into the ask-response slot but
519-
// no ask has consumed yet. They stay queued on purpose, so two consumers
520-
// must read this set: a later drain, so it never re-posts the same message
521-
// into the slot, and an execution-gating ask (`queuedResponseForAsk` would
522-
// answer it with `yesButtonClicked`), which must never let between-turn
523-
// feedback stand in for an approval. Cleared only on consumption.
515+
// IDs a drain submitted into the ask-response slot that no ask has consumed
516+
// yet. They stay queued on purpose: a later drain must not re-post them, and
517+
// an execution-gating ask must not let between-turn feedback stand in for an
518+
// approval. Cleared only when a consume settles.
524519
private drainRetainedQueuedMessageIds = new Set<string>()
525-
// Number of `ask()` calls in flight. A drain must not submit while one is
526-
// pending: a background completion can land during any prompt, and its
527-
// submission would answer that ask with text written for another turn while
528-
// the retained message stayed queued for a second delivery.
520+
// `ask()` calls in flight. A drain must not submit while one is pending: the
521+
// submission would answer that ask with text written for another turn.
529522
private pendingAskCount = 0
530523

531524
// Streaming
@@ -1125,11 +1118,15 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
11251118
await this.say("user_feedback", text ?? "", images)
11261119
for (let attempt = 0; attempt <= QUEUED_FEEDBACK_SAVE_RETRY_DELAYS_MS.length; attempt++) {
11271120
if (this.abort) {
1128-
this.messageQueueService.releaseMessage(messageId)
1121+
this.releaseUnconsumedQueuedMessage(messageId)
11291122
return false
11301123
}
11311124
if (await this.saveClineMessages()) {
1132-
return this.messageQueueService.removeMessage(messageId)
1125+
// Durable, so the consume happened: report success even when the queue
1126+
// entry is already gone (deleted mid-ack), or a saved row would be
1127+
// reported as a failure.
1128+
this.consumeQueuedMessage(messageId)
1129+
return true
11331130
}
11341131
if (attempt < QUEUED_FEEDBACK_SAVE_RETRY_DELAYS_MS.length) {
11351132
await delay(QUEUED_FEEDBACK_SAVE_RETRY_DELAYS_MS[attempt])
@@ -1138,7 +1135,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
11381135
console.error(
11391136
`[Task#persistQueuedFeedbackAndAcknowledge] Failed to durably save queued feedback ${messageId} after ${QUEUED_FEEDBACK_SAVE_RETRY_DELAYS_MS.length + 1} attempts`,
11401137
)
1141-
this.messageQueueService.releaseMessage(messageId)
1138+
this.releaseUnconsumedQueuedMessage(messageId)
11421139
return false
11431140
}
11441141

@@ -1251,17 +1248,38 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
12511248

12521249
private handleQueuedAskResponse(message: QueuedMessage, resolution: QueuedAskResolution): string | undefined {
12531250
this.handleWebviewAskResponse(resolution.response, message.text, message.images)
1254-
// This ask consumed the message, so a later drain may submit the next
1255-
// one instead of treating it as still pending. The retention record is
1256-
// cleared on the same edge: a consumed message is no longer retained.
1257-
this.drainRetainedQueuedMessageIds.delete(message.id)
12581251
if (resolution.requiresDurableAck) {
1252+
// The consume is not settled yet: the caller's persistence write can
1253+
// still fail, and the retention record must survive that unwind.
12591254
return message.id
12601255
}
1261-
this.messageQueueService.removeMessage(message.id)
1256+
this.consumeQueuedMessage(message.id)
12621257
return undefined
12631258
}
12641259

1260+
/**
1261+
* Settle a consumed queued message: drop it from the queue and clear its
1262+
* retention record on this one edge, so the record can never outlive the
1263+
* message. A consume whose persistence fails never reaches here.
1264+
*/
1265+
private consumeQueuedMessage(messageId: string): void {
1266+
this.messageQueueService.removeMessage(messageId)
1267+
this.drainRetainedQueuedMessageIds.delete(messageId)
1268+
}
1269+
1270+
/**
1271+
* Undo a claim whose consume did not happen.
1272+
*
1273+
* A drain submitted this message and no ask has delivered it to the model
1274+
* yet, so the claim is released for a later consumer and the retention record
1275+
* keeps it out of every `yesButtonClicked`-mapped ask. Releasing without that
1276+
* record would re-arm it as fresh input for the next command ask to approve.
1277+
*/
1278+
private releaseUnconsumedQueuedMessage(messageId: string): void {
1279+
this.drainRetainedQueuedMessageIds.add(messageId)
1280+
this.messageQueueService.releaseMessage(messageId)
1281+
}
1282+
12651283
/**
12661284
* Re-read the auto-approval policy immediately before a queued message would
12671285
* stand in for a command ask's approval, and consult the full command policy
@@ -1386,26 +1404,21 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
13861404
* flipping those would re-enable interactive prompt timers whenever a claim
13871405
* is outstanding.
13881406
*
1389-
* `excludeIds` carries the messages this ask may not consume at all; see
1390-
* `queuedClaimExclusionsForAsk`.
1407+
* `excludeIds` carries the messages this ask may not consume at all.
13911408
*/
13921409
private mayDrainQueuedMessageForAsk(excludeIds?: ReadonlySet<string>): boolean {
13931410
return !this.blanketDeniedCommandThisTurn && this.messageQueueService.hasUnclaimed(excludeIds)
13941411
}
13951412

13961413
/**
13971414
* Queued messages this ask must not claim. `queuedResponseForAsk` answers a
1398-
* command, tool, or use_mcp_server ask with `yesButtonClicked`, so letting one
1399-
* of those claim a drain-retained message would turn between-turn feedback
1400-
* into an approval: the ask skips `checkAutoApproval`, and a stale "ship it"
1401-
* would run the next command with no prompt even with auto-approval off. Such
1402-
* asks exclude every retained ID and get a real prompt; the message stays
1403-
* queued for a conversational ask, the only consumer that carries its text
1404-
* into the model as feedback.
1415+
* command, tool, or use_mcp_server ask with `yesButtonClicked`, so claiming a
1416+
* drain-retained message there would turn between-turn feedback into an
1417+
* approval with `checkAutoApproval` skipped.
14051418
*/
14061419
private queuedClaimExclusionsForAsk(type: ClineAsk, text?: string): ReadonlySet<string> | undefined {
1407-
// Drop IDs the queue no longer holds (deleted from the webview or the API,
1408-
// or cleared by dispose) so the record cannot outlive its message.
1420+
// Drop IDs the queue no longer holds (deleted from the webview or the API)
1421+
// so the record cannot outlive its message.
14091422
const queuedIds = new Set(this.messageQueueService.messages.map((message) => message.id))
14101423
for (const id of this.drainRetainedQueuedMessageIds) {
14111424
if (!queuedIds.has(id)) {
@@ -1895,14 +1908,13 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
18951908
queuedMessageId?: string
18961909
/**
18971910
* Present when the ask was resolved by an automatic (policy) denial
1898-
* rather than a user click. Consumers must not treat this as a user
1899-
* rejection: the denial is scoped to its own tool call.
1911+
* rather than a user click: not a user rejection, and scoped to its own
1912+
* tool call.
19001913
*/
19011914
autoDenyDetail?: AutoDenyDetail
19021915
}> {
1903-
// Bracket the whole ask, not just the wait: the counter is the drain's
1904-
// "a prompt is in flight" signal, and a submission landing during any
1905-
// await below would answer this ask with text written for another turn.
1916+
// Bracket the whole ask, not just the wait: a submission during any await
1917+
// below would answer this ask with text written for another turn.
19061918
this.pendingAskCount++
19071919
try {
19081920
return await this.askWithPendingCount(type, text, partial, progressStatus, isProtected, autoApprovalContext)
@@ -1923,11 +1935,6 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
19231935
text?: string
19241936
images?: string[]
19251937
queuedMessageId?: string
1926-
/**
1927-
* Present when the ask was resolved by an automatic (policy) denial
1928-
* rather than a user click. Consumers must not treat this as a user
1929-
* rejection: the denial is scoped to its own tool call.
1930-
*/
19311938
autoDenyDetail?: AutoDenyDetail
19321939
}> {
19331940
// If this Cline instance was aborted by the provider, then the only
@@ -2430,7 +2437,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
24302437
/* v8 ignore next 3 -- abort-while-waiting path; covered by e2e standalone-resume test */
24312438
if (this.abort) {
24322439
if (queuedMessageId) {
2433-
this.messageQueueService.releaseMessage(queuedMessageId)
2440+
this.releaseUnconsumedQueuedMessage(queuedMessageId)
24342441
}
24352442
throw new Error(`[ZooCode#ask] task ${this.taskId}.${this.instanceId} aborted`)
24362443
}
@@ -2440,7 +2447,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
24402447
// command_output. It's important that when we know an ask could
24412448
// fail, it is handled gracefully.
24422449
if (queuedMessageId) {
2443-
this.messageQueueService.releaseMessage(queuedMessageId)
2450+
this.releaseUnconsumedQueuedMessage(queuedMessageId)
24442451
}
24452452
throw new AskIgnoredError("superseded")
24462453
}
@@ -2485,9 +2492,8 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
24852492
this.askResponseText = text
24862493
this.askResponseImages = images
24872494
// No retention bookkeeping here: a direct response overwrites the slot a
2488-
// drain submitted into, which means that submission was NOT consumed, so
2489-
// the message stays queued and stays excluded from approval asks. Only
2490-
// `handleQueuedAskResponse` clears retention, on the consume edge.
2495+
// drain submitted into, so that submission was NOT consumed and the
2496+
// message stays queued and excluded from approval asks.
24912497

24922498
// Create a checkpoint whenever the user sends a message.
24932499
// Use allowEmpty=true to ensure a checkpoint is recorded even if there are no file changes.
@@ -2626,9 +2632,8 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
26262632
/**
26272633
* Submit a user message through the ask-response channel.
26282634
*
2629-
* @returns true when the message was handed to the ask-response channel;
2630-
* false when there was nothing to submit, the task is stopping, or the
2631-
* provider reference was lost. Callers draining the queue must check this.
2635+
* @returns false when there was nothing to submit, the task is stopping, or
2636+
* the provider reference was lost. Callers draining the queue must check it.
26322637
*/
26332638
public async submitUserMessage(
26342639
text: string,
@@ -2664,10 +2669,9 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
26642669
}
26652670
}
26662671

2667-
// Cancellation guard immediately before the slot write: abort and
2668-
// dispose both set this flag, and the emit/checkpoint side effects
2669-
// below must never run on a cancelled task. The check is adjacent
2670-
// to the write so no cancellation can interleave (single-threaded).
2672+
// Cancellation guard adjacent to the slot write: abort and dispose
2673+
// both set this flag, and the side effects below must never run on
2674+
// a cancelled task. Single-threaded, so nothing interleaves.
26712675
if (this.abort || this.abandoned) {
26722676
console.error("[Task#submitUserMessage] Task aborted, dropping user message submission")
26732677
return false
@@ -2852,9 +2856,8 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
28522856
contextCondense,
28532857
)
28542858

2855-
// Process any queued messages after condensing completes. Awaited so a
2856-
// failed submission rejects this call instead of vanishing; the caller
2857-
// (ClineProvider.condenseTaskContext) surfaces it.
2859+
// Awaited so a failed submission rejects this call instead of vanishing;
2860+
// the caller (ClineProvider.condenseTaskContext) surfaces it.
28582861
await this.processQueuedMessages()
28592862
}
28602863

@@ -6542,37 +6545,25 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
65426545
/**
65436546
* Process the next queued message by claiming and submitting it.
65446547
*
6545-
* The message is claimed — not dequeued — before submission, and the claim
6546-
* is released after the submission handoff succeeds so the message STAYS
6547-
* queued: removal is tied to ask-consumption (Task.ask claims queued
6548-
* messages and removes them via handleQueuedAskResponse, or hands them to
6549-
* persistQueuedFeedbackAndAcknowledge), not to submit-return. A message
6550-
* submitted between command completion and the next conversational ask is
6551-
* therefore retained until an ask actually consumes it, instead of being
6552-
* dropped if the turn fails or restarts first. When submission throws, the
6553-
* claim is released so the message stays queued for a later drain and the
6554-
* error propagates to the caller; when the handoff reports nothing
6555-
* delivered (task stopping, provider gone), the claim is released and the
6556-
* run resolves false.
6548+
* The message is claimed, not dequeued, and the claim is released after the
6549+
* submission handoff so the message STAYS queued: removal is tied to
6550+
* ask-consumption, not to submit-return, so a message submitted between turns
6551+
* survives a failed turn or restart. When submission throws, the claim is
6552+
* released and the error propagates; when the handoff reports nothing
6553+
* delivered, the claim is released and the run resolves false.
65576554
*
6558-
* Retention is restricted to conversational consumption. A retained message
6559-
* is recorded in `drainRetainedQueuedMessageIds`, and every ask whose
6560-
* `queuedResponseForAsk` mapping would answer with `yesButtonClicked`
6561-
* (command, tool, use_mcp_server) excludes those IDs from its claim. Without
6562-
* that exclusion the retained text would silently approve a later execution
6563-
* ask with no prompt, even with auto-approval off.
6555+
* Retention is restricted to conversational consumption: a retained ID is
6556+
* recorded in `drainRetainedQueuedMessageIds`, and every ask whose
6557+
* `queuedResponseForAsk` mapping answers `yesButtonClicked` excludes those
6558+
* IDs, so the retained text cannot silently approve a later execution ask.
65646559
*
65656560
* A drain also never submits while an `ask()` is pending: the submission
6566-
* would answer that ask with text written for another turn, bypassing the
6567-
* ask's own latch and denial authority, and the retained message would then
6568-
* be delivered a second time.
6569-
*
6570-
* Drains are serialized per task, so a background-completion drain and a
6571-
* post-result drain cannot interleave two messages into the single slot.
6561+
* would answer that ask with text written for another turn. Drains are
6562+
* serialized per task, so two of them cannot interleave into the single slot.
65726563
*
6573-
* @returns Promise resolving to true when a queued message was submitted
6574-
* (and remains queued until consumed); false when the queue was empty, an
6575-
* ask was pending, or the task is stopping.
6564+
* @returns true only when this call submitted a message. A retained queue
6565+
* head reports false: it was already submitted, and the single ask-response
6566+
* slot cannot take a second message without losing the first.
65766567
*/
65776568
public processQueuedMessages(): Promise<boolean> {
65786569
const run = this.queuedMessageDrainChain.then(() => this.claimAndSubmitNextQueuedMessage())
@@ -6588,37 +6579,33 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
65886579
private async claimAndSubmitNextQueuedMessage(): Promise<boolean> {
65896580
// A cancelled task must not submit: abortTask sets this flag before its
65906581
// awaited webview flush, and dispose clears the queue afterwards, so this
6591-
// guard closes the window where a drain could otherwise post into a
6592-
// dying task.
6582+
// guard closes the window where a drain could post into a dying task.
65936583
if (this.abort || this.abandoned) {
65946584
return false
65956585
}
65966586
// An ask is waiting for its response. Submitting now would resolve that
6597-
// prompt with text written for another turn — the ask's own claim gate,
6598-
// blanket-deny latch, and protected-ask exemption never run — and the
6599-
// message would stay queued for a second delivery. Defer: the message
6600-
// keeps its place and the next post-ask drain submits it.
6587+
// prompt with text written for another turn, bypassing the ask's claim gate,
6588+
// blanket-deny latch, and protected-ask exemption. Defer it instead.
66016589
if (this.pendingAskCount > 0) {
66026590
return false
66036591
}
66046592
const queued = this.messageQueueService.claimNextMessage()
66056593
if (!queued) {
66066594
return false
66076595
}
6608-
// An earlier drain already submitted this message and no ask has
6609-
// consumed it yet: re-posting would overwrite a distinct pending
6610-
// response and lose it, so release the claim and keep the message
6611-
// queued for the ask.
6596+
// An earlier drain already submitted this message and no ask has consumed
6597+
// it yet: re-posting would overwrite a distinct pending response and lose
6598+
// it, so release the claim and keep the message queued. Nothing was
6599+
// submitted, so this reports false.
66126600
if (this.drainRetainedQueuedMessageIds.has(queued.id)) {
66136601
this.messageQueueService.releaseMessage(queued.id)
6614-
return true
6602+
return false
66156603
}
66166604
try {
66176605
const submitted = await this.submitUserMessage(queued.text, queued.images)
66186606
if (!submitted) {
6619-
// Cancelled mid-handoff: the guard in submitUserMessage dropped
6620-
// the write, so release quietly instead of failing the drain on
6621-
// a task that is already stopping.
6607+
// Cancelled mid-handoff: the guard in submitUserMessage dropped the
6608+
// write, so release quietly instead of failing a stopping task.
66226609
this.messageQueueService.releaseMessage(queued.id)
66236610
return false
66246611
}
@@ -6627,11 +6614,9 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
66276614
this.messageQueueService.releaseMessage(queued.id)
66286615
throw error
66296616
}
6630-
// Submission succeeded, but the message is only removed once an ask
6631-
// consumes it. Record the ID so a later drain can tell "already
6632-
// submitted, still pending" from "not submitted yet" and so an
6633-
// execution-gating ask refuses to claim it, then release the claim so a
6634-
// conversational ask can take it.
6617+
// Record the ID before releasing the claim: a later drain must tell
6618+
// "already submitted, still pending" from "not submitted yet", and an
6619+
// execution-gating ask must refuse to claim it.
66356620
this.drainRetainedQueuedMessageIds.add(queued.id)
66366621
this.messageQueueService.releaseMessage(queued.id)
66376622
return true

0 commit comments

Comments
 (0)