Skip to content

Commit 93a9b65

Browse files
committed
fix(task): never dequeue queued messages for an aborted task
Tools now await the write checkpoint before handing off queued work, so a cancellation can land between the write and the handoff. processQueuedMessages dequeued unconditionally and scheduled submitUserMessage, which rejects once abort is set - the queued message was consumed and dropped instead of being left for the resume/abort flow. The dequeue is now skipped while the task is aborted, guarding every handoff site at the lowest layer. Regression test pins that an aborted task leaves the queue intact (negative control verified: the unconditional dequeue reddens it). Review feedback on PR 1404 (Lifecycle Resource Cleanup row); ported to every unit of this series.
1 parent aa7f9e4 commit 93a9b65

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6310,6 +6310,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
63106310
* @param context - Context string for logging (e.g., the calling tool name)
63116311
*/
63126312
public processQueuedMessages(): void {
6313+
// An aborted task must not dequeue queued messages: submitUserMessage
6314+
// rejects once abort is set, so consuming the queue here would drop the
6315+
// message instead of leaving it for the resume/abort flow. Tools await
6316+
// checkpoint saves before handing off, so a cancellation can land between
6317+
// the write and this call.
6318+
if (this.abort) {
6319+
return
6320+
}
63136321
try {
63146322
if (!this.messageQueueService.isEmpty()) {
63156323
const queued = this.messageQueueService.dequeueMessage()

‎src/core/task/__tests__/Task.spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7119,6 +7119,34 @@ describe("Queued message processing after condense", () => {
71197119
expect(submitSpy).toHaveBeenCalledWith("queued text", ["img1.png"])
71207120
expect(task.messageQueueService.isEmpty()).toBe(true)
71217121
})
7122+
it("does not dequeue queued messages once the task is aborted", () => {
7123+
// Tools await the checkpoint before handing off queued work; a
7124+
// cancellation landing during that await must not consume the queued
7125+
// message: submitUserMessage rejects once abort is set, so dequeuing
7126+
// would drop the input instead of leaving it for the resume flow.
7127+
if (!TelemetryService.hasInstance()) {
7128+
TelemetryService.createInstance([])
7129+
}
7130+
const provider = createProvider()
7131+
const task = new Task({
7132+
provider,
7133+
apiConfiguration: apiConfig,
7134+
task: "initial task",
7135+
startTask: false,
7136+
})
7137+
const submitSpy = vi.spyOn(task, "submitUserMessage").mockResolvedValue(undefined)
7138+
7139+
task.messageQueueService.addMessage("queued text")
7140+
task.abort = true
7141+
try {
7142+
task.processQueuedMessages()
7143+
} finally {
7144+
task.abort = false
7145+
}
7146+
7147+
expect(submitSpy).not.toHaveBeenCalled()
7148+
expect(task.messageQueueService.isEmpty()).toBe(false)
7149+
})
71227150

71237151
it("does not cross-drain queues between separate tasks", async () => {
71247152
const providerA = createProvider()

0 commit comments

Comments
 (0)