Skip to content

Commit f21dec2

Browse files
committed
fix(task): clamp partial-update elapsed time against backward clock steps; assert ts targets in throttle test
1 parent 73e008e commit f21dec2

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎src/core/task/Task.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,9 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
17821782
post(queued)
17831783
}
17841784

1785-
const elapsed = Date.now() - this.lastPartialMessagePostTs
1785+
// Clamp: a backward wall-clock step makes the difference negative, which would schedule
1786+
// the trailing post later than the interval.
1787+
const elapsed = Math.max(0, Date.now() - this.lastPartialMessagePostTs)
17861788
if (elapsed >= PARTIAL_MESSAGE_UPDATE_INTERVAL_MS) {
17871789
this.cancelPendingPartialMessageUpdate()
17881790
post(message)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4555,6 +4555,24 @@ describe("Cline", () => {
45554555

45564556
await vi.advanceTimersByTimeAsync(500)
45574557
expect(messageUpdates.map(({ text }) => text)).toEqual(["r2", "r3", "t2"])
4558+
// The queued "r3" post keeps the reasoning message's ts; "t2" targets the text message.
4559+
expect(messageUpdates[1].ts).toBe(messageUpdates[0].ts)
4560+
expect(messageUpdates[2].ts).not.toBe(messageUpdates[0].ts)
4561+
})
4562+
4563+
it("does not delay the trailing post beyond the interval when the clock steps backward", async () => {
4564+
const { task, messageUpdates } = createStreamingTask()
4565+
4566+
await task.say("text", "first", undefined, true)
4567+
await task.say("text", "second", undefined, true)
4568+
expect(messageUpdates.map(({ text }) => text)).toEqual(["second"])
4569+
4570+
vi.setSystemTime(Date.now() - 10_000)
4571+
await task.say("text", "third", undefined, true)
4572+
expect(messageUpdates.map(({ text }) => text)).toEqual(["second"])
4573+
4574+
await vi.advanceTimersByTimeAsync(120)
4575+
expect(messageUpdates.map(({ text }) => text)).toEqual(["second", "third"])
45584576
})
45594577

45604578
it("posts updates that are far enough apart immediately", async () => {

0 commit comments

Comments
 (0)