Skip to content

Commit da86b62

Browse files
author
Benoit Souche
committed
Fix gray screens on long sessions (Zoo-Code-Org#1675)
UI-action state posts no longer embed the whole conversation. Each state post sent the full clineMessages array to the webview. On long sessions this re-serialized tens to hundreds of MB per action. The renderer heap climbed to multi-GB. The webview grayed. This change splits the transport: state posts send UI state only with no message payload; new messages travel through a coalesced messagesAppended delta queue; Task stream completion posts use the message-free variant; the webview applies deltas through a MessageStore without array clones; the CLI consumes the same delta stream. Measured on a 100k-message session with identical workload: before 271 MB climbing to 1098 MB with sawtooth spikes, after 281 MB flat across the window.
1 parent 05d01f3 commit da86b62

24 files changed

Lines changed: 1728 additions & 129 deletions
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Tests for the messagesAppended delta channel in the CLI agent
2+
// (issue #1675 Step 2 co-update). Pure test: real MessageProcessor, real
3+
// StateStore, real TypedEventEmitter — no module mocking.
4+
5+
import type { ClineMessage, ExtensionMessage } from "@roo-code/types"
6+
import { MessageProcessor } from "../message-processor.js"
7+
import { StateStore } from "../state-store.js"
8+
import { TypedEventEmitter } from "../events.js"
9+
10+
/** Minimal say:text message factory. */
11+
function msg(ts: number, text: string, partial = false): ClineMessage {
12+
return { ts, type: "say", say: "text", text, partial, ask: undefined } as unknown as ClineMessage
13+
}
14+
15+
/** messagesAppended ExtensionMessage factory. */
16+
function appendedMessage(messages: ClineMessage[], messagesWatermark?: number): ExtensionMessage {
17+
return {
18+
type: "messagesAppended",
19+
messages,
20+
...(messagesWatermark !== undefined ? { messagesWatermark } : {}),
21+
} as ExtensionMessage
22+
}
23+
24+
describe("MessageProcessor messagesAppended (issue #1675 Step 2)", () => {
25+
let store: StateStore
26+
let emitter: TypedEventEmitter
27+
let processor: MessageProcessor
28+
29+
beforeEach(() => {
30+
store = new StateStore()
31+
emitter = new TypedEventEmitter()
32+
processor = new MessageProcessor(store, emitter)
33+
})
34+
35+
test("appends a continuous batch and emits per-message events", () => {
36+
const messages: ClineMessage[] = []
37+
emitter.on("message", (m: ClineMessage) => messages.push(m))
38+
39+
// Hydrate with 2 messages (count = 2), then a continuous batch of 3
40+
// whose watermark is exactly 5.
41+
store.setMessages([msg(1, "a"), msg(2, "b")])
42+
messages.length = 0
43+
44+
processor.processMessage(appendedMessage([msg(3, "c"), msg(4, "d"), msg(5, "e")], 5))
45+
46+
expect(store.getMessages().length).toBe(5)
47+
expect(store.getMessages().map((m) => m.text)).toEqual(["a", "b", "c", "d", "e"])
48+
expect(messages.length).toBe(3)
49+
})
50+
51+
test("drops a discontinuous batch and emits stateResyncNeeded", () => {
52+
const resyncs: Array<{ held: number; watermark: number }> = []
53+
emitter.on("stateResyncNeeded", (payload) => resyncs.push(payload as { held: number; watermark: number }))
54+
55+
store.setMessages([msg(1, "a"), msg(2, "b")])
56+
57+
// Watermark 9 means messages 3..6 were lost (held 2 + batch 3 != 9).
58+
processor.processMessage(appendedMessage([msg(7, "g"), msg(8, "h"), msg(9, "i")], 9))
59+
60+
expect(store.getMessages().length).toBe(2)
61+
expect(resyncs.length).toBe(1)
62+
expect(resyncs[0]).toEqual({ held: 2, watermark: 9 })
63+
})
64+
65+
test("treats a batch without watermark as continuous (backward compatible)", () => {
66+
store.setMessages([msg(1, "a")])
67+
68+
processor.processMessage(appendedMessage([msg(2, "b")]))
69+
70+
expect(store.getMessages().length).toBe(2)
71+
})
72+
73+
test("an empty batch is a no-op", () => {
74+
store.setMessages([msg(1, "a")])
75+
76+
processor.processMessage(appendedMessage([], 1))
77+
78+
expect(store.getMessages().length).toBe(1)
79+
})
80+
81+
test("a stale post-resync batch is dropped", () => {
82+
store.setMessages([msg(1, "a"), msg(2, "b"), msg(3, "c")])
83+
84+
// A resync already brought the store to 3 messages; a stale batch from
85+
// before the snapshot carries a smaller watermark and must not append.
86+
processor.processMessage(appendedMessage([msg(2, "b")], 2))
87+
88+
const messages = store.getMessages()
89+
expect(messages.length).toBe(3)
90+
expect(messages[messages.length - 1]?.text).toBe("c")
91+
})
92+
})

‎apps/cli/src/agent/events.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ export interface ClientEventMap {
7676
*/
7777
modeChanged: ModeChangedEvent
7878

79+
/**
80+
* Emitted when a messagesAppended batch fails the continuity check
81+
* (issue #1675 Step 2): the CLI's message list has a hole and the
82+
* transport layer should request a full-state resync from the extension.
83+
*/
84+
stateResyncNeeded: StateResyncNeededEvent
85+
7986
/**
8087
* Emitted on any error during message processing.
8188
*/
@@ -121,6 +128,18 @@ export interface TaskCompletedEvent {
121128
/**
122129
* Event payload when mode changes.
123130
*/
131+
/**
132+
* Emitted when a messagesAppended batch fails the continuity check
133+
* (issue #1675 Step 2). The CLI's message list has a hole; a full-state
134+
* resync should be requested.
135+
*/
136+
export interface StateResyncNeededEvent {
137+
/** Number of messages the CLI currently holds. */
138+
held: number
139+
/** The watermark carried by the discontinuous batch. */
140+
watermark: number
141+
}
142+
124143
export interface ModeChangedEvent {
125144
/** The previous mode (undefined if first mode set) */
126145
previousMode: string | undefined

‎apps/cli/src/agent/message-processor.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export class MessageProcessor {
113113
this.handleMessageUpdated(message)
114114
break
115115

116+
case "messagesAppended":
117+
this.handleMessagesAppended(message)
118+
break
119+
116120
case "action":
117121
this.handleAction(message)
118122
break
@@ -251,6 +255,52 @@ export class MessageProcessor {
251255
this.emitStateChangeEvents(previousState, currentState)
252256
}
253257

258+
/**
259+
* Handle a "messagesAppended" message - streaming deltas (issue #1675 Step 2).
260+
*
261+
* The extension now appends new messages via batches that carry only the
262+
* NEW messages plus a watermark (the task's total message count after the
263+
* batch). This keeps CLI memory and processing proportional to streaming
264+
* volume instead of conversation length.
265+
*
266+
* Continuity is verified the same way the webview does: the batch must
267+
* start exactly after the messages we hold. A discontinuity means a lost
268+
* batch; emit stateResyncNeeded so the transport layer can request a
269+
* full-state snapshot instead of operating on a hole.
270+
*/
271+
private handleMessagesAppended(message: ExtensionMessage): void {
272+
const appended = message.messages ?? []
273+
if (appended.length === 0) {
274+
return
275+
}
276+
277+
const watermark = message.messagesWatermark
278+
if (watermark !== undefined && this.store.getMessages().length + appended.length !== watermark) {
279+
if (this.options.debug) {
280+
debugLog("[MessageProcessor] messagesAppended discontinuity detected, requesting resync", {
281+
held: this.store.getMessages().length,
282+
batch: appended.length,
283+
watermark,
284+
})
285+
}
286+
this.emitter.emit("stateResyncNeeded", { held: this.store.getMessages().length, watermark })
287+
return
288+
}
289+
290+
const previousState = this.store.getAgentState()
291+
292+
for (const clineMessage of appended) {
293+
this.store.addMessage(clineMessage)
294+
// `message` is the existing new-message event on ClientEventMap.
295+
this.emitter.emit("message", clineMessage)
296+
}
297+
298+
const currentState = this.store.getAgentState()
299+
300+
// Emit state change events (appends can flip streaming/running states).
301+
this.emitStateChangeEvents(previousState, currentState)
302+
}
303+
254304
/**
255305
* Handle an "action" message - UI action trigger.
256306
*

‎apps/cli/src/ui/hooks/useMessageHandlers.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,24 @@ export function useMessageHandlers({ nonInteractive }: UseMessageHandlersOptions
301301
*/
302302
const handleExtensionMessage = useCallback(
303303
(msg: ExtensionMessage) => {
304-
if (msg.type === "state") {
304+
if (msg.type === "messagesAppended") {
305+
// Issue #1675 Step 2 co-update: streaming deltas carry only the
306+
// NEW messages; route each through the same say/ask handlers.
307+
// The flow-control layer (MessageProcessor/StateStore, and the
308+
// in-memory uiStore) enforces continuity upstream in the agent
309+
// layer; resilience to gaps is handled by the resync event.
310+
for (const clineMsg of msg.messages ?? []) {
311+
const type = clineMsg.type
312+
const say = clineMsg.say
313+
const ask = clineMsg.ask
314+
315+
if (type === "say" && say) {
316+
handleSayMessage(clineMsg.ts, say, clineMsg.text || "", clineMsg.partial || false)
317+
} else if (type === "ask" && ask) {
318+
handleAskMessage(clineMsg.ts, ask, clineMsg.text || "", clineMsg.partial || false)
319+
}
320+
}
321+
} else if (msg.type === "state") {
305322
const state = msg.state
306323

307324
if (!state) {

‎packages/types/src/vscode-extension-host.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface ExtensionMessage {
3838
| "workspaceUpdated"
3939
| "invoke"
4040
| "messageUpdated"
41+
| "messagesAppended"
4142
| "mcpServers"
4243
| "enhancedPrompt"
4344
| "commitSearchResults"
@@ -144,6 +145,21 @@ export interface ExtensionMessage {
144145
path?: string
145146
}>
146147
clineMessage?: ClineMessage
148+
/**
149+
* Streaming deltas for the messages-appended channel (issue #1675 Step 2):
150+
* `messages` carries newly appended clineMessages in order, replacing the
151+
* practice of embedding the full conversation array in every state push.
152+
* `messagesWatermark` is the total message count on the task after these
153+
* appends (captured at enqueue time, so it is exact per message). A batch
154+
* carries the last message's watermark; receivers verify continuity as
155+
* `held + batch.length === watermark`, detect gaps as `watermark > held +
156+
* batch.length`, and request a full-state resync instead of rendering a
157+
* hole. This is a DIFFERENT domain from the state-post `clineMessagesSeq`
158+
* post counter and the two are never compared.
159+
*/
160+
messages?: ClineMessage[]
161+
/** Batch watermark for the messagesAppended channel (see above). */
162+
messagesWatermark?: number
147163
routerModels?: RouterModels
148164
openAiModels?: string[]
149165
ollamaModels?: ModelRecord
@@ -491,6 +507,7 @@ export interface WebviewMessage {
491507
| "webviewDidLaunch"
492508
| "webviewDidFocus"
493509
| "newTask"
510+
| "requestStateResync"
494511
| "askResponse"
495512
| "terminalOperation"
496513
| "clearTask"

‎src/core/task/Task.ts‎

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,34 +1663,40 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
16631663
return readTaskMessages({ taskId: this.taskId, globalStoragePath: this.globalStoragePath })
16641664
}
16651665

1666+
/**
1667+
* Delta-queue one clineMessage to the webview (issue #1675 Step 2).
1668+
*
1669+
* Routing: the message itself reaches the webview via `messagesAppended`
1670+
* (small, O(new) payload, same coalescing cadence as the state throttle);
1671+
* non-message state (mode, cost, etc.) still flows through the throttled
1672+
* `postStateToWebviewWithoutClineMessages` — which never embeds messages.
1673+
*
1674+
* Unanswered asks keep their pre-existing immediate-await guarantee: an
1675+
* ask not yet answered is flushed straight through the delta queue so webview
1676+
* Message listeners can respond against it (askResponse ordering).
1677+
*/
16661678
private async addToClineMessages(message: ClineMessage) {
16671679
message.messageId ??= crypto.randomUUID()
16681680
this.clineMessages.push(message)
16691681
const provider = this.providerRef.deref()
1670-
// Unanswered asks must reach the webview before Message listeners can respond against its state.
1671-
//
1672-
// Partial `say` messages deliberately do NOT flush. `flushPostStateToWebviewThrottled()`
1673-
// invoked right after a leading-edge debounce call has no pending trailing invocation to
1674-
// run, so it only cancels the trailing timer — which makes the *next* call hit the leading
1675-
// edge and post immediately. Flushing on every partial therefore defeated the debounce
1676-
// entirely (one full-state post per message, the very behaviour #1078 set out to remove).
1677-
// They are safe on the throttled path: the trailing/maxWait post carries the message's
1678-
// current text, so a `messageUpdated` dropped for a not-yet-known `ts` is superseded
1679-
// rather than lost.
1680-
//
1681-
// Partial *asks* still flush, via the clause below — `Task#ask` adds them without
1682-
// `isAnswered`, so they keep the ordering guarantee that unanswered asks depend on.
1683-
const requiresImmediateState = message.type === "ask" && message.isAnswered !== true
1682+
1683+
const requiresImmediateDelivery = message.type === "ask" && message.isAnswered !== true
1684+
1685+
// Race-free watermark: captured immediately after the push, so it is
1686+
// exact even if more messages append before the batch is emitted.
1687+
provider?.queueMessageDelta(message, this.clineMessages.length)
1688+
16841689
try {
16851690
await provider?.postStateToWebviewThrottled()
16861691
} catch (error) {
16871692
console.error("[Task#addToClineMessages] postStateToWebviewThrottled failed:", error)
16881693
}
1689-
if (requiresImmediateState) {
1694+
1695+
if (requiresImmediateDelivery) {
16901696
try {
1691-
await provider?.flushPostStateToWebviewThrottled()
1697+
await provider?.flushMessageDeltas()
16921698
} catch (error) {
1693-
console.error("[Task#addToClineMessages] flushPostStateToWebviewThrottled failed:", error)
1699+
console.error("[Task#addToClineMessages] flushMessageDeltas failed:", error)
16941700
}
16951701
}
16961702
this.emit(RooCodeEventName.Message, { action: "created", message })
@@ -4023,7 +4029,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
40234029
} satisfies ClineApiReqInfo)
40244030

40254031
await this.saveClineMessages()
4026-
await this.providerRef.deref()?.postStateToWebviewWithoutTaskHistory()
4032+
// #1675 residual path (live-captured 2026-10-21: webview heap
4033+
// sawtoothed to 3.9 GB during an active conversation even with
4034+
// the UI-action sites converted): the pre-fix completione post
4035+
// here embedded the WHOLE clineMessages array on every message
4036+
// boundary. The conversation already travels incrementally via
4037+
// messagesAppended deltas / messageUpdated partials, so this post
4038+
// only needs the lightweight state fields (cost, tokens, etc.).
4039+
await this.providerRef.deref()?.postStateToWebviewWithoutClineMessages()
40274040

40284041
try {
40294042
let cacheWriteTokens = 0
@@ -4859,7 +4872,11 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
48594872
}
48604873

48614874
await this.saveClineMessages()
4862-
await this.providerRef.deref()?.postStateToWebviewWithoutTaskHistory()
4875+
// #1675 residual path (live-captured 2026-10-21): same as the
4876+
// site above — stream-completion posts embed the whole
4877+
// conversation. The deltas channel already delivered the
4878+
// messages incrementally; this post carries state fields only.
4879+
await this.providerRef.deref()?.postStateToWebviewWithoutClineMessages()
48634880

48644881
// No legacy text-stream tool parser state to reset.
48654882

‎src/core/task/__tests__/Task.abort-reason-race.spec.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ function makeMockProvider() {
9393
getState: vi.fn().mockResolvedValue({}),
9494
getSkillsManager: vi.fn().mockReturnValue(undefined),
9595
postStateToWebviewWithoutTaskHistory: vi.fn().mockResolvedValue(undefined),
96+
// #1675 residual-path fix: completion sites now post the
97+
// message-free variant.
98+
postStateToWebviewWithoutClineMessages: vi.fn().mockResolvedValue(undefined),
99+
queueMessageDelta: vi.fn(),
96100
flushPostStateToWebviewThrottled: vi.fn().mockResolvedValue(undefined),
97101
postMessageToWebview: vi.fn().mockResolvedValue(undefined),
98102
postStateToWebview: vi.fn().mockResolvedValue(undefined),

0 commit comments

Comments
 (0)