Skip to content

The agent that grows with you

perf(desktop): bound terminal retention without redundant publication

ghgoodreau4a15cf36b6e4
diff --git a/apps/desktop/ENGINEERING.md b/apps/desktop/ENGINEERING.md
index 7d889128720..c7a6e2a0cea 100644
--- a/apps/desktop/ENGINEERING.md
+++ b/apps/desktop/ENGINEERING.md
@@ -152,6 +152,9 @@ reopens the gesture-less forced-navigation class the advisory closed.
- Live transcript navigation reads the session store; the shared navigation
context carries only the selected historical page, not every streamed delta.
+- Unchanged retained snapshots must not republish the terminal store. Closing
+ the last mounted writer must enforce the cold agent-terminal retention budget
+ even when no later output arrives.
## Nous free tier: state is pulled, never latched in the renderer
diff --git a/apps/desktop/src/app/right-sidebar/terminal/agent-terminal-stream.bound.test.ts b/apps/desktop/src/app/right-sidebar/terminal/agent-terminal-stream.bound.test.ts
index 067d1b86a59..ddf1c4f1d55 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/agent-terminal-stream.bound.test.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/agent-terminal-stream.bound.test.ts
@@ -26,6 +26,23 @@ function runFinishedProc(id: string, chunks = 6): void {
}
describe('agent terminal retention is bounded', () => {
+ it('enforces the cold budget when mounted writers close without another output event', () => {
+ const stops = Array.from({ length: 40 }, (_, index) => registerAgentTerminalWriter(`closed-${index}`, () => {}))
+
+ for (let index = 0; index < stops.length; index++) {
+ runFinishedProc(`closed-${index}`)
+ }
+
+ for (const stop of stops) {
+ stop()
+ }
+
+ const retained = stops.map((_, index) => replay(`closed-${index}`))
+
+ expect(retained.reduce((chars, output) => chars + output.length, 0)).toBeLessThanOrEqual(2_000_000)
+ expect(retained.at(-1)).toBe(CHUNK.repeat(4))
+ })
+
it('a finished process does not pin its backlog for the life of the renderer', () => {
// 200 background commands, none mounted — the shape that grew to ~93 MB.
for (let i = 0; i < 200; i++) {
diff --git a/apps/desktop/src/app/right-sidebar/terminal/agent-terminal-stream.ts b/apps/desktop/src/app/right-sidebar/terminal/agent-terminal-stream.ts
index c9b778f6cfb..82fa6fdfe28 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/agent-terminal-stream.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/agent-terminal-stream.ts
@@ -81,6 +81,7 @@ export function registerAgentTerminalWriter(procId: string, write: Writer): () =
return () => {
if (writers.get(procId) === write) {
writers.delete(procId)
+ evictColdProcs()
}
}
}
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
index 5ca4ce4b5f1..c1e97139baf 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
@@ -62,7 +62,9 @@ describe('terminal store persistence', () => {
const { $terminals, ensureAgentTerminal, updateTerminalReviveBuffer } = await loadTerminalStore()
const agentId = ensureAgentTerminal('proc-1', 'background task')!
+ const before = $terminals.get()
updateTerminalReviveBuffer(agentId, 'should be ignored')
+ expect($terminals.get()).toBe(before)
expect($terminals.get().find(term => term.id === agentId)?.reviveBuffer).toBeUndefined()
expect(window.localStorage.getItem(STORAGE_KEY)).toBeNull()
@@ -81,6 +83,44 @@ describe('terminal store persistence', () => {
expect(stored).toBe(huge.slice(-stored.length))
})
+ it('does not republish an identical retained tail, including changes outside the cap', async () => {
+ const { $terminals, createTerminal, updateTerminalReviveBuffer } = await loadTerminalStore()
+ const id = createTerminal('/repo')
+ const output = 'ANSI \u001b[32m界 café\u001b[0m\r\n'.repeat(4_000)
+ updateTerminalReviveBuffer(id, output)
+ const before = $terminals.get()
+ const persisted = window.localStorage.getItem(STORAGE_KEY)
+
+ updateTerminalReviveBuffer(id, output)
+ expect($terminals.get()).toBe(before)
+ updateTerminalReviveBuffer(id, `discarded prefix\r\n${output}`)
+ expect($terminals.get()).toBe(before)
+ expect(window.localStorage.getItem(STORAGE_KEY)).toBe(persisted)
+
+ updateTerminalReviveBuffer(id, 'new output')
+ expect(JSON.parse(window.localStorage.getItem(STORAGE_KEY) ?? '{}').terminals[0].reviveBuffer).toBe('new output')
+ })
+
+ it('ignores late snapshots after persistence negotiation or terminal closure', async () => {
+ const { $terminals, closeTerminal, createTerminal, markTerminalPersistent, updateTerminalReviveBuffer } =
+ await loadTerminalStore()
+
+ const id = createTerminal('/repo')
+ updateTerminalReviveBuffer(id, 'renderer history')
+ markTerminalPersistent(id)
+ const persistent = $terminals.get()
+ const saved = window.localStorage.getItem(STORAGE_KEY)
+ updateTerminalReviveBuffer(id, 'late local snapshot')
+ expect($terminals.get()).toBe(persistent)
+ expect(window.localStorage.getItem(STORAGE_KEY)).toBe(saved)
+
+ closeTerminal(id)
+ const closed = $terminals.get()
+ updateTerminalReviveBuffer(id, 'late closed snapshot')
+ expect($terminals.get()).toBe(closed)
+ expect(window.localStorage.getItem(STORAGE_KEY)).toBeNull()
+ })
+
it('clears remembered tabs when all terminals close', async () => {
const { closeAllTerminals, createTerminal } = await loadTerminalStore()
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
index 65b2c12c1a0..854240a3a84 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
@@ -472,12 +472,26 @@ export function markTerminalPersistent(id: string): void {
}
export function updateTerminalReviveBuffer(id: string, reviveBuffer: string): void {
+ const terminals = $terminals.get()
+ const index = terminals.findIndex(term => term.id === id)
+ const term = terminals[index]
+
+ if (!term || term.kind !== 'user' || term.persistent) {
+ return
+ }
+
const capped =
reviveBuffer.length > MAX_REVIVE_BUFFER_CHARS ? reviveBuffer.slice(-MAX_REVIVE_BUFFER_CHARS) : reviveBuffer
- $terminals.set(
- $terminals.get().map(term => (term.id === id && term.kind === 'user' && !term.persistent ? { ...term, reviveBuffer: capped } : term))
- )
+ // Publishing also serializes every user tab and updates workspace subscribers.
+ // Stable TUI screens can produce identical snapshots for every capture.
+ if (term.reviveBuffer === capped) {
+ return
+ }
+
+ const next = terminals.slice()
+ next[index] = { ...term, reviveBuffer: capped }
+ $terminals.set(next)
}
/** Record the shell's latest working directory for a tab so the next launch can