Skip to content

Commit 2e90d6c

Browse files
committed
fix(task): drain pending presenter updates before unlock
1 parent 2453214 commit 2e90d6c

2 files changed

Lines changed: 78 additions & 5 deletions

File tree

‎src/core/assistant-message/__tests__/presentAssistantMessage-custom-tool.spec.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,75 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
209209
})
210210

211211
describe("Presentation lock ownership", () => {
212+
it("drains an update queued during the helper-to-wrapper handoff", async () => {
213+
mockTask.assistantMessageContent = [{ type: "text", content: "initial", partial: true }]
214+
let queuedCall: Promise<void> | undefined
215+
let lockedDuringHandoff = false
216+
mockTask.say.mockImplementationOnce(async () => {
217+
// The first microtask precedes the helper's await continuation.
218+
// The second follows that continuation but precedes the wrapper's.
219+
queueMicrotask(() => {
220+
queueMicrotask(() => {
221+
lockedDuringHandoff = mockTask.presentAssistantMessageLocked
222+
mockTask.assistantMessageContent[0] = { type: "text", content: "final", partial: false }
223+
mockTask.didCompleteReadingStream = true
224+
queuedCall = presentAssistantMessage(mockTask)
225+
})
226+
})
227+
})
228+
229+
await presentAssistantMessage(mockTask)
230+
await queuedCall
231+
232+
expect(lockedDuringHandoff).toBe(true)
233+
expect(mockTask.say.mock.calls).toEqual([
234+
["text", "initial", undefined, true],
235+
["text", "final", undefined, false],
236+
])
237+
expect(mockTask.currentStreamingContentIndex).toBe(1)
238+
expect(mockTask.userMessageContentReady).toBe(true)
239+
expect(mockTask.presentAssistantMessageHasPendingUpdates).toBe(false)
240+
expect(mockTask.presentAssistantMessageLocked).toBe(false)
241+
})
242+
243+
it("stops draining an update queued during the handoff once the task aborts", async () => {
244+
mockTask.assistantMessageContent = [{ type: "text", content: "initial", partial: true }]
245+
let queuedCall: Promise<void> | undefined
246+
let pendingDuringHandoff = false
247+
let aborted = false
248+
let abortChecksAfterAbort = 0
249+
Object.defineProperty(mockTask, "abort", {
250+
configurable: true,
251+
get: () => {
252+
// Fail fast instead of hanging if a drain keeps retrying an
253+
// aborted pass whose pending update can never be consumed.
254+
if (aborted && ++abortChecksAfterAbort > 10) {
255+
throw new Error("presenter kept draining after abort")
256+
}
257+
return aborted
258+
},
259+
})
260+
mockTask.say.mockImplementationOnce(async () => {
261+
queueMicrotask(() => {
262+
queueMicrotask(() => {
263+
mockTask.assistantMessageContent[0] = { type: "text", content: "final", partial: false }
264+
mockTask.didCompleteReadingStream = true
265+
queuedCall = presentAssistantMessage(mockTask)
266+
pendingDuringHandoff = mockTask.presentAssistantMessageHasPendingUpdates
267+
aborted = true
268+
})
269+
})
270+
})
271+
272+
await presentAssistantMessage(mockTask)
273+
await queuedCall
274+
275+
expect(pendingDuringHandoff).toBe(true)
276+
expect(mockTask.say).toHaveBeenCalledExactlyOnceWith("text", "initial", undefined, true)
277+
expect(mockTask.currentStreamingContentIndex).toBe(0)
278+
expect(mockTask.presentAssistantMessageLocked).toBe(false)
279+
})
280+
212281
it("holds one lock across consecutive blocks and queues overlapping calls", async () => {
213282
mockTask.assistantMessageContent = [
214283
{ type: "text", content: "first", partial: false },

‎src/core/assistant-message/presentAssistantMessage.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ export async function presentAssistantMessage(cline: Task) {
9898

9999
cline.presentAssistantMessageLocked = true
100100
try {
101-
await presentAssistantMessageBlock(cline)
101+
// Drain updates queued while the lock was held, including one that
102+
// arrives after the helper's final check but before this continuation.
103+
// The last pending check and the release below run in the same
104+
// synchronous step, so no update can be stranded behind the lock.
105+
do {
106+
await presentAssistantMessageBlock(cline)
107+
} while (!cline.abort && cline.presentAssistantMessageHasPendingUpdates)
102108
} finally {
103109
// Tool handlers and provider-state reads can reject. Never strand the
104110
// task behind a dispatch lock after the presenter has unwound.
@@ -1128,10 +1134,8 @@ async function presentAssistantMessageBlock(cline: Task): Promise<void> {
11281134
}
11291135
}
11301136

1131-
// Block is partial, but the read stream may have finished.
1132-
if (cline.presentAssistantMessageHasPendingUpdates) {
1133-
return await presentAssistantMessageBlock(cline)
1134-
}
1137+
// Pending updates are drained by presentAssistantMessage, which owns the
1138+
// lock and checks for them immediately before releasing it.
11351139
}
11361140

11371141
/**

0 commit comments

Comments
 (0)