Skip to content

Commit be0a162

Browse files
author
Benoit Souche
committed
Keep long sessions far below the renderer limit: bound message retention (Zoo-Code-Org#1675)
A shared renderer that hosted several Zoo sessions died past the V8 heap ceiling: long agentic tool histories were retained in full, forever. This adds a per-session retention budget to the webview MessageStore. When the estimated retained message bytes exceed 64 MB, the oldest messages are evicted to lightweight shells: ts and type only. The shells keep UI scans working and delta continuity uses totalCount, so eviction never reads as a dropped batch. A Load-earlier button pages evicted content back from disk on demand through a requestMessagesRange handler. The budget converts multi-GB sessions into a fixed ~64 MB webview footprint. Gates: messageStore windowing spec covers budget, shells, clamp-at-one, prepend path; 39 webview suite tests, 1940 extension tests, tsc clean, knip trio and eslint green.
1 parent da86b62 commit be0a162

24 files changed

Lines changed: 458 additions & 59 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface ExtensionMessage {
3939
| "invoke"
4040
| "messageUpdated"
4141
| "messagesAppended"
42+
| "messagesRange"
4243
| "mcpServers"
4344
| "enhancedPrompt"
4445
| "commitSearchResults"
@@ -160,6 +161,14 @@ export interface ExtensionMessage {
160161
messages?: ClineMessage[]
161162
/** Batch watermark for the messagesAppended channel (see above). */
162163
messagesWatermark?: number
164+
/**
165+
* Response to requestMessagesRange (issue #1675 Step 3): an ordered page
166+
* of messages read from the task's persisted history on disk. `offset` and
167+
* `count` echo the request; `messages` is in task order (oldest first),
168+
* exactly the slice [offset, offset+count) of the task's full history
169+
* (0-based, with 0 = first message of the task).
170+
*/
171+
messagesRange?: { offset: number; count: number; messages: ClineMessage[] }
163172
routerModels?: RouterModels
164173
openAiModels?: string[]
165174
ollamaModels?: ModelRecord
@@ -528,6 +537,7 @@ export interface WebviewMessage {
528537
| typeof OllamaModelsMessageType.requestOllamaModels
529538
| typeof LmStudioModelsMessageType.requestLmStudioModels
530539
| "requestRooModels"
540+
| "requestMessagesRange"
531541
| typeof VsCodeLmModelsMessageType.requestVsCodeLmModels
532542
| typeof VsCodeLmModelsMessageType.githubCopilotSignIn
533543
| typeof VsCodeLmModelsMessageType.githubCopilotReconnect
@@ -688,6 +698,13 @@ export interface WebviewMessage {
688698
text?: string
689699
taskId?: string
690700
editedMessageContent?: string
701+
/**
702+
* requestMessagesRange (issue #1675 Step 3): load a page of an earlier
703+
* history from disk. `offset` is 0-based into the task's full persisted
704+
* history (0 = first message), `count` the page size. The provider guards
705+
* that `taskId` is the CURRENT task.
706+
*/
707+
messagesRangeRequest?: { offset: number; count: number }
691708
tab?: "settings" | "history" | "mcp" | "modes" | "chat" | "marketplace" | "cloud"
692709
disabled?: boolean
693710
context?: string

‎src/core/webview/webviewMessageHandler.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { CloudService } from "@roo-code/cloud"
3636
import { TelemetryService } from "@roo-code/telemetry"
3737

3838
import { type ApiMessage } from "../task-persistence/apiMessages"
39-
import { saveTaskMessages } from "../task-persistence"
39+
import { saveTaskMessages, readTaskMessages } from "../task-persistence"
4040
import { importRooTaskHistory } from "../task-persistence/importRooTaskHistory"
4141

4242
import { ClineProvider } from "./ClineProvider"
@@ -601,6 +601,42 @@ export const webviewMessageHandler = async (
601601
await provider.postStateToWebview()
602602
break
603603
}
604+
case "requestMessagesRange": {
605+
// Issue #1675 Step 3: load-earlier paging. The webview evicted
606+
// old messages to shells and now wants a page restored from the
607+
// task's persisted history on disk.
608+
const request = message.messagesRangeRequest
609+
const task = provider.getCurrentTask()
610+
if (!request || !task) {
611+
break
612+
}
613+
// Own-task guard: paging is only served for the current task so a
614+
// stale webview can't pull arbitrary tasks' histories.
615+
if (message.taskId !== task.taskId) {
616+
break
617+
}
618+
619+
try {
620+
const history = await readTaskMessages({
621+
taskId: task.taskId,
622+
globalStoragePath: provider.contextProxy.globalStorageUri.fsPath,
623+
})
624+
const offset = Math.max(0, Math.min(request.offset, history.length))
625+
const count = Math.max(0, request.count)
626+
const messages = history.slice(offset, offset + count)
627+
await provider.postMessageToWebview({
628+
type: "messagesRange",
629+
messagesRange: { offset, count: messages.length, messages },
630+
})
631+
} catch (error) {
632+
provider.log(`[requestMessagesRange] failed to read history: ${error}`)
633+
await provider.postMessageToWebview({
634+
type: "messagesRange",
635+
messagesRange: { offset: request.offset, count: 0, messages: [] },
636+
})
637+
}
638+
break
639+
}
604640
case "webviewDidLaunch":
605641
provider.log("[webview-lifecycle] webviewDidLaunch received")
606642
// Load custom modes first

‎webview-ui/src/components/chat/ChatView.tsx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
101101
messageQueue = [],
102102
showWorktreesInHomeScreen,
103103
telemetrySetting,
104+
evictedMessagesCount,
105+
loadEarlierMessages,
104106
} = useExtensionState()
105107

106108
// Show a WarningRow when the user sends a message with a retired provider.
@@ -1723,6 +1725,17 @@ const ChatViewComponent: React.ForwardRefRenderFunction<ChatViewRef, ChatViewPro
17231725

17241726
{task && (
17251727
<>
1728+
{evictedMessagesCount > 0 && (
1729+
<div className="flex justify-center py-1">
1730+
<Button
1731+
variant="secondary"
1732+
className="text-xs"
1733+
onClick={() => loadEarlierMessages()}
1734+
data-testid="load-earlier-messages">
1735+
{t("chat:loadEarlierMessages", { count: evictedMessagesCount })}
1736+
</Button>
1737+
</div>
1738+
)}
17261739
<div className="grow flex" ref={scrollContainerRef}>
17271740
<Virtuoso
17281741
ref={virtuosoRef}

‎webview-ui/src/context/ExtensionStateContext.tsx‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@ export interface ExtensionStateContextType extends ExtensionState {
153153
setShowWorktreesInHomeScreen: (value: boolean) => void
154154
skills?: SkillMetadata[]
155155
rules: RuleMetadata[]
156+
/**
157+
* Number of messages the store evicted to shells for the current task
158+
* (issue #1675 Step 3): non-zero means "load earlier messages" is
159+
* available. Decreases as pages are prepended back from disk.
160+
*/
161+
evictedMessagesCount: number
162+
/**
163+
* Request the provider to read the next page of earlier messages for the
164+
* current task from its persisted history and send them back as a
165+
* messagesRange message (which prepends them into the store). Callers
166+
* should read `evictedMessagesCount` first; a no-op request is sent when
167+
* nothing is evicted.
168+
*/
169+
loadEarlierMessages: (count?: number) => void
156170
}
157171

158172
export const ExtensionStateContext = createContext<ExtensionStateContextType | undefined>(undefined)
@@ -471,6 +485,8 @@ export const ExtensionStateContextProvider: React.FC<{
471485
// A watermark behind what we hold is a stale batch (e.g.
472486
// after a resync) — drop it too; the resync snapshot
473487
// supersedes it.
488+
// Continuity uses totalCount (retained + evicted shells) so
489+
// budget eviction never masquerades as a dropped batch.
474490
if (watermark !== undefined && messageStore.totalCount + appended.length !== watermark) {
475491
vscode.postMessage({ type: "requestStateResync" })
476492
return prevState
@@ -487,6 +503,20 @@ export const ExtensionStateContextProvider: React.FC<{
487503
})
488504
break
489505
}
506+
case "messagesRange": {
507+
// Issue #1675 Step 3: a page of earlier messages read from
508+
// disk. Prepend into the store (replacing the shells it
509+
// covers); publish the fresh window.
510+
const range = message.messagesRange
511+
if (!range) {
512+
break
513+
}
514+
setState((prevState) => {
515+
messageStore.prependMessages(range.messages, range.count)
516+
return { ...prevState, clineMessages: messageStore.getMessages() }
517+
})
518+
break
519+
}
490520
case "skills": {
491521
if (message.skills) {
492522
setSkills(message.skills)
@@ -719,6 +749,23 @@ export const ExtensionStateContextProvider: React.FC<{
719749
showWorktreesInHomeScreen: state.showWorktreesInHomeScreen ?? true,
720750
setShowWorktreesInHomeScreen: (value) =>
721751
setState((prevState) => ({ ...prevState, showWorktreesInHomeScreen: value })),
752+
evictedMessagesCount: messageStore.evictedCount,
753+
loadEarlierMessages: (count = 200) => {
754+
const evicted = messageStore.evictedCount
755+
if (evicted === 0) {
756+
return
757+
}
758+
const taskId = state.currentTaskItem?.id ?? state.currentTaskId
759+
if (!taskId) {
760+
return
761+
}
762+
const page = Math.min(count, evicted)
763+
vscode.postMessage({
764+
type: "requestMessagesRange",
765+
taskId,
766+
messagesRangeRequest: { offset: messageStore.totalCount - evicted - page, count: page },
767+
})
768+
},
722769
}
723770

724771
return <ExtensionStateContext.Provider value={contextValue}>{children}</ExtensionStateContext.Provider>

‎webview-ui/src/i18n/locales/ca/chat.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎webview-ui/src/i18n/locales/de/chat.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎webview-ui/src/i18n/locales/en/chat.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@
347347
"title": "Edit Unsuccessful"
348348
},
349349
"troubleMessage": "Zoo is having trouble...",
350+
"loadEarlierMessages": "Load {{count}} earlier messages",
350351
"powershell": {
351352
"issues": "It seems like you're having Windows PowerShell issues, please see this"
352353
},

‎webview-ui/src/i18n/locales/es/chat.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎webview-ui/src/i18n/locales/fr/chat.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎webview-ui/src/i18n/locales/hi/chat.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)