Skip to content

Commit f65eb12

Browse files
committed
fix(task): reserve a consumed drain submission through its durable ack
What: MessageQueueService gains claimMessage(id), and the pending-slot consumption in Task.ask claims the entry when handing its ID to the caller. Persistence already settles the reservation: removeMessage on success, releaseMessage on failure/abort, and the ask abort/superseded paths release the handed ID. Why: the drain path releases its claim after submitting, and the pending-slot consumption handed the ID back without claiming — so while persistQueuedFeedbackAndAcknowledge retried (backoff can run seconds), the entry sat unclaimed in the queue. A background drain (tracker already cleared) or a second ask could claim it again, submitting and persisting the same user message twice, with one ack failing after the other removed the entry (CodeRabbit 4173559934). Impact: the entry stays reserved from consumption until persistence settles; the claim-path consumption was already reserved by its own claim. Pin: interception -> claimNextMessage undefined and a second drain no-ops while the ack is unsettled; a failed ack releases the reservation for redelivery.
1 parent ed89a6e commit f65eb12

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎src/core/message-queue/MessageQueueService.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,23 @@ export class MessageQueueService extends EventEmitter<QueueEvents> {
113113
return this._messages.length === 0
114114
}
115115

116+
/**
117+
* Reserve a specific queued message by ID when it is still present and
118+
* unclaimed. Lets a consumer hold a consumed entry through its durable ack
119+
* so neither another ask nor a background drain can claim it again
120+
* mid-persistence.
121+
*/
122+
public claimMessage(id: string): boolean {
123+
if (this.claimedMessageIds.has(id)) {
124+
return false
125+
}
126+
if (!this._messages.some((message) => message.id === id)) {
127+
return false
128+
}
129+
this.claimedMessageIds.add(id)
130+
return true
131+
}
132+
116133
/**
117134
* Whether at least one queued message is still available to be claimed.
118135
*

‎src/core/task/Task.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,12 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
25292529
// cannot lose a message that was already dequeued.
25302530
queuedMessageId = this.pendingSubmittedQueuedMessageId
25312531
this.pendingSubmittedQueuedMessageId = undefined
2532+
// Reserve the entry through the durable ack: while persistence
2533+
// retries (which can take seconds), neither a second ask nor a
2534+
// background drain may claim it again and persist the same
2535+
// message twice. Persistence settles the reservation itself —
2536+
// removeMessage on success, releaseMessage on failure/abort.
2537+
this.messageQueueService.claimMessage(queuedMessageId)
25322538
} else if (
25332539
!this.messageQueueService.messages.some(
25342540
(message) => message.id === this.pendingSubmittedQueuedMessageId,

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,43 @@ describe("Task.ask queued message drain", () => {
420420
expect(task.messageQueueService.claimNextMessage()?.text).toBe("stranded no more")
421421
})
422422

423+
it("reserves a consumed drain submission through its durable ack", async () => {
424+
const task = await createTask({ getState: async () => ({}) })
425+
426+
// Interception: the ask is blocked before the drain submits, so the
427+
// pending-slot consumption path hands the ID back.
428+
const askPromise = task.ask("completion_result", "Done", false)
429+
await new Promise((resolve) => setTimeout(resolve, 150))
430+
task.messageQueueService.addMessage("Reserve me")
431+
await task.processQueuedMessages()
432+
const result = await askPromise
433+
const entry = task.messageQueueService.messages.at(0)
434+
if (!entry) throw new Error("queued message missing")
435+
expect(result.queuedMessageId).toBe(entry.id)
436+
437+
// While the durable ack has not settled, the entry stays reserved:
438+
// neither a later ask nor a background drain can claim it again and
439+
// persist the same message twice.
440+
expect(task.messageQueueService.claimNextMessage()).toBeUndefined()
441+
const submitSpy = vi.spyOn(task, "submitUserMessage")
442+
await expect(task.processQueuedMessages()).resolves.toBe(false)
443+
expect(submitSpy).not.toHaveBeenCalled()
444+
445+
// A failed ack releases the reservation for redelivery.
446+
const access = getQueueTaskTestAccess(task)
447+
access.say = vi.fn().mockResolvedValue(undefined)
448+
access.saveClineMessages = vi.fn(async () => false)
449+
vi.useFakeTimers()
450+
try {
451+
const persistence = task.persistQueuedFeedbackAndAcknowledge(entry.id, result.text, result.images)
452+
await vi.runAllTimersAsync()
453+
await expect(persistence).resolves.toBe(false)
454+
} finally {
455+
vi.useRealTimers()
456+
}
457+
expect(task.messageQueueService.claimNextMessage()?.text).toBe("Reserve me")
458+
})
459+
423460
it("delivers an intercepted message exactly once when a consumer acks through the durable helper", async () => {
424461
const task = await createTask({ getState: async () => ({}) })
425462
const submitSpy = vi.spyOn(task, "submitUserMessage")

0 commit comments

Comments
 (0)