buzz icon indicating copy to clipboard operation
buzz copied to clipboard

Observer transcript is fully rebuilt on every relay envelope once an agent buffers 3000 events

Open GeneralJah215 opened this issue 3 months ago • 0 comments

Describe the bug

appendAgentEvents in desktop/src/features/agents/observerRelayStore.ts has an off-by-one in its cap check that permanently disables the incremental transcript path.

const sorted = [...current, ...sortedAdded].sort(compareObserverEvents);
const trimmed = sorted.length > MAX_OBSERVER_EVENTS;
const final = trimmed
  ? sorted.slice(sorted.length - MAX_OBSERVER_EVENTS)
  : sorted;

final.length is capped at exactly MAX_OBSERVER_EVENTS (3000). So from the moment an agent's journal reaches 3000, every later call sees sorted.length >= 3001, trimmed is true, and this gate:

if (allAtEnd && !trimmed) { /* incremental fold */ }
else { transcriptByAgent.set(key, buildTranscriptState(final)); }

can never take the incremental branch again. Nothing shrinks eventsByAgent except a full store reset, so it is permanent for the life of the renderer process, per agent.

The fallback is not O(3000). buildTranscriptState (desktop/src/features/agents/ui/agentSessionTranscript.ts:1159) is a full replay, and agentSessionTranscript.ts:305 folds streaming text with text: existing.text + text, uncapped. So each rebuild re-grows a monotonically longer string — O(T) time and O(T) garbage, with T growing for as long as agents stream. ensureMutable (:102-108) additionally copies the full items array and the full itemsById Map on the first mutation of every event, and replaceItem does a linear findIndex.

Steps to reproduce

  1. Run the desktop app with several agents streaming observer telemetry.
  2. Leave it running. At roughly 90 frames/minute an agent fills its 3000-event journal in about 33 minutes.
  3. Watch renderer CPU and working set. Agents tip into the slow path one at a time, so CPU ramps rather than steps.

Reproducible headless through the exported syncAgentObserverEvents seam: push MAX_OBSERVER_EVENTS + 500 events for one agent and time the ingest.

Expected behavior

Steady-state ingestion should stay O(1) amortized per event. Reaching the retention cap should evict from the front of the window, not convert every subsequent append into a full-history replay.

Version and platform

  • Built from main. The cap check is unchanged from 8342dfcc5 through current main.
  • Windows 11 Pro 26200.

Logs / additional context

Measured headless: the same 3,500-event ingest takes 141,000 ms with the defect present and 750 ms without it — about 188x.

Measured on a live machine, renderer CPU as a percentage of one logical core: 62.6% visible, 52.3% just after minimizing, 119.1% after five minutes untouched, with a 959 MB renderer working set. It escalates while nothing is happening, which rules out a fixed-rate timer.

Not fixable by throttling on visibility: relay callbacks and useSyncExternalStore notifications run unthrottled while the window is hidden, and the work is wasted while visible too.

A note on #5680. Batch observer-store publications per relay envelope refactored this exact function — it rewrote the dedupe into a Set, changed the end-of-array test to the allAtEnd predicate, and added batch-envelope support. The cap check itself was not touched. Because publication is now batched, the rebuild fires once per envelope rather than once per event, so the frequency on current main is lower than it was before that PR. The per-rebuild cost and the permanence are unchanged.

On testing this. Worth flagging because it caught me out: asserting the transcript content passes with and without the defect. Both branches produce an equivalent transcript — that is the point of the fallback — so a content assertion is not a regression test here. The defect is about which code path runs, so the test has to assert the path, for example an exported rebuild counter that must not move after the cap is reached. A timing threshold can be widened until it passes; a call count cannot. The boundary also needs covering explicitly: 2999 to 3000 must not trim, 3000 to 3001 must.

On a fix. I have one running locally, but it is written against a module that does not exist in your tree, so it is not directly portable and I have not proposed it as a PR. The shape is to let the incremental fold survive a trim: record which transcript items each journal event touched, so an event leaving the capped window evicts exactly the items it produced, with the full rebuild retained as the fallback for genuine out-of-order arrival.

One thing that surfaced while building it, which may matter to you independently: eviction is deliberately not byte-equivalent to a rebuild, and every divergence retains more history. A rebuild silently truncates the leading text of an assistant message whose chunks straddle the cap, because it can only see in-window contributions. That is a latent defect in the rebuild path today, invisible only because the rebuild runs constantly.

Happy to write a PR against your tree if you tell me the approach is acceptable. Related and adjacent: #5596 on bounding renderer retention.

Deliberately not bundled: archiveEventsByChannel and the timeline cache are uncapped, which is a separate memory concern, and the all-agents fan-out in activeAgentTurnsStore re-scans every agent on every notification, which is a separate CPU concern. Both are real; neither belongs in this report.

GeneralJah215 avatar Aug 13 '26 00:08 GeneralJah215