Skip to content

Commit e5a60f1

Browse files
PierrunoYTclaude
andcommitted
fix(task): flush queued partial update when a different message streams
Add tests for the 120ms throttle boundary and for cross-message flushing. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent a9aeb24 commit e5a60f1

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
17561756
})
17571757
}
17581758

1759+
// A queued update for a different message (e.g. reasoning followed by text) must not be
1760+
// overwritten by this one, so deliver it now.
1761+
const queued = this.pendingPartialMessage
1762+
if (queued && queued.ts !== message.ts) {
1763+
this.cancelPendingPartialMessageUpdate()
1764+
post(queued)
1765+
}
1766+
17591767
const elapsed = Date.now() - this.lastPartialMessagePostTs
17601768
if (elapsed >= PARTIAL_MESSAGE_UPDATE_INTERVAL_MS) {
17611769
this.cancelPendingPartialMessageUpdate()

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4467,6 +4467,40 @@ describe("Cline", () => {
44674467
expect(messageUpdates.map(({ text }) => text)).toEqual(["second"])
44684468
})
44694469

4470+
it("holds a queued update until the 120ms interval elapses", async () => {
4471+
const { task, messageUpdates } = createStreamingTask()
4472+
4473+
await task.say("text", "first", undefined, true)
4474+
await task.say("text", "second", undefined, true)
4475+
await task.say("text", "third", undefined, true)
4476+
expect(messageUpdates.map(({ text }) => text)).toEqual(["second"])
4477+
4478+
await vi.advanceTimersByTimeAsync(119)
4479+
expect(messageUpdates.map(({ text }) => text)).toEqual(["second"])
4480+
4481+
await vi.advanceTimersByTimeAsync(1)
4482+
expect(messageUpdates.map(({ text }) => text)).toEqual(["second", "third"])
4483+
})
4484+
4485+
it("delivers a queued update for one message before updating a different message", async () => {
4486+
const { task, messageUpdates } = createStreamingTask()
4487+
4488+
await task.say("reasoning", "r1", undefined, true)
4489+
await task.say("reasoning", "r2", undefined, true)
4490+
await task.say("reasoning", "r3", undefined, true)
4491+
expect(messageUpdates.map(({ text }) => text)).toEqual(["r2"])
4492+
4493+
// A new message starts, then streams: its update must not replace the queued "r3".
4494+
// Message timestamps are Date.now(), which fake timers freeze, so step the clock.
4495+
await vi.advanceTimersByTimeAsync(1)
4496+
await task.say("text", "t1", undefined, true)
4497+
await task.say("text", "t2", undefined, true)
4498+
expect(messageUpdates.map(({ text }) => text)).toEqual(["r2", "r3"])
4499+
4500+
await vi.advanceTimersByTimeAsync(500)
4501+
expect(messageUpdates.map(({ text }) => text)).toEqual(["r2", "r3", "t2"])
4502+
})
4503+
44704504
it("posts updates that are far enough apart immediately", async () => {
44714505
const { task, messageUpdates } = createStreamingTask()
44724506

0 commit comments

Comments
 (0)