* perf(app): stop re-walking the timeline on every streamed delta Every coalesced delta rebuilds the stream render model with the same tail. The model re-sliced the tail at the history-window start on every call, so the rendered tail was a fresh array per delta and every WeakMap keyed on it (ordering, history split, turn timing, the layout history cache, row revision) missed each time. Cache the slice by tail identity and window start. Turn timing then still walked the whole rendered tail per delta and allocated a new assistant-timing Map, which the layout memo depends on. Split the walk: the tail is walked once per tail identity and the head is walked from the carried open turn per delta. The Map keeps its identity while the head adds no completed turn. Adds identity-contract tests and a vitest bench for the per-delta pipeline. 200 deltas over a 2,000-item timeline, mean per run: default mounted window 5.68ms -> 2.82ms, history fully revealed 16.95ms -> 1.81ms. Fixes #4816 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * perf(app): keep the stream view's per-delta work bounded by the head The chat outline concatenated tail and head, reduced over the result in the render body, and rebuilt jumpToPrompt on every delta even when the outline was disabled, which is every native viewport. Memoize the tail's contribution, scan only the head per delta, do nothing while disabled, make jumpToPrompt a stable event so the rail's memo holds, and republish the active prompt only when the index or reading position changes. The view built a Set of every mounted id plus the live head per delta for the outline's pending-jump check; key it on mounted history only and treat live head rows as always mounted. Split plugin projection so a head change does not re-project the tail, memoize the history-window boundary lookup, and read the turn presentation's submission flag with some() instead of allocating a filtered array. Refs #4816 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * test(app): confirm full history before measuring streaming --------- Co-authored-by: Ivan Malison <IvanMalison@gmail.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: paseo-bot[bot] <266920839+paseo-bot[bot]@users.noreply.github.com>
8.3 KiB
Agent stream performance
How assistant text gets from a provider to the screen, and why it is paced on the way. Read this before changing packages/server/src/server/agent/agent-stream-coalescer.ts, the reducer queue in packages/app/src/timeline/session-stream-reducers.ts, or the reveal in packages/app/src/hooks/use-revealed-text.ts.
For terminal output, which is a separate pipeline with separate budgets, see terminal-performance.md.
The pipeline
provider deltas (every provider streams incrementally)
→ AgentStreamCoalescer (daemon, leading + trailing, ≤1 message per 60ms per agent)
→ recordTimeline: one canonical row per flushed item
→ agent_stream ws message
→ reducer queue (app, one commit per frame) → session store
→ source-item plugin transforms → native Markdown blocks / tool grouping
→ paced reveal (app, per displayed item) → paint
Every provider delivers incremental text, so there is no provider that needs special handling: Claude via includePartialMessages, Codex via agent_message_delta, ACP agents via agent_message_chunk, Pi and OMP via text_delta.
Why the reveal is paced
Arrival is lumpy and there is no fixing that at the source. A 60ms coalescing window carries however many characters the model produced in those 60ms, which swings by an order of magnitude within a single turn. Painting each delta as it lands makes the size of those lumps visible, and that is what reads as jagged.
So arrival sets a target and the reveal rate is derived from the backlog instead. A burst makes the text catch up faster; it does not make the text jump. Shrinking the coalescing window does not fix this — it makes the lumps smaller and more frequent, at the cost of message rate on a daemon loop that already contends with terminal frames and per-message relay encryption.
Invariants
- The coalescer is leading + trailing. The first delta after an idle window flushes synchronously; only the rest of the burst waits for the trailing timer. Reverting to trailing-only adds a full window to the first character of every turn. Same shape and the same reason as
TerminalOutputCoalescer. - The leading flush adds a canonical row, and that is fine. A burst's first chunk lands as its own timeline row.
mergeAssistantChunks/mergeReasoningChunksintimeline-projection.tsjoin contiguous same-turn rows, and clients read the projected timeline, so history is unaffected. Tests that assert on raw rows have to account for the extra row; tests that assert on what a client sees do not. - The store holds the full text; only the rendered slice is paced. Copy, selection, the chat outline, and scroll geometry all read the same string the user can see. Pacing the store instead would leave the bottom anchor chasing a content height that is ahead of the reveal.
- Markdown blocks belong to presentation, and every assistant message is a block group.
agent-stream/presentation.tssplits a message into one display row per Markdown block, the same way whether it arrived streaming or as fetched history. Splitting in the reducer instead would discard paragraph separators and expose fragments to plugin callbacks, which need the whole source text. Live-head work stays cheap by parsing only the growing last block during append, retaining completed blocks by object identity, and caching the split per source item so a tail change does not re-split history. History was once left whole to keep cross-block Markdown context; that gave one message two shapes depending on how it arrived, and message-addressed features only worked on one of them. - A row id is never a message id. Block rows are
${messageId}:block:${n}withblockGroupId = messageId, including single-block messages, so nothing can come to rely on the two being equal.getStreamItemMessageIdinpresentation.tsis the only way to go from a row to its message, and web rows carry it asdata-message-idalongsidedata-history-row-id. Anything addressing a message — chat find, scroll-to-message, history reveal, the find expansion that lifts the render cap — uses the message id and must expect several rows to answer to it. Row ids stay for React keys, virtualizer measurement, scroll anchors, and per-row caches like assistant image occurrence keys. - First sight of a text is revealed whole. Only growth is paced. This is what makes history hydration, timeline replay, a virtualized row remounting on scroll, and an already-finished message all render complete on first paint without a special case for each.
- Leaving
phase: "streaming"snaps the reveal. A completed turn must never be left holding characters.layoutStreamsets the phase, so anything outside the live head with an active turn is already complete. - The reducer queue commits on a frame, with a timer as the ceiling. A frame callback never fires in a hidden tab, so a timer races it and wins when nothing is painting — the store has to keep advancing either way.
- A history row re-renders only when its item or layout item identity changes. The inverted
FlatList hands every mounted cell a new
indexandrefwhenever a row is prepended, so without a memo boundary each coalesced tick re-rendered every mounted row (about 50 on a phone, 100–250 ms of JS per tick).layoutStreamkeeps a layout item's identity when nothing about it changed,useRevisedHistoryRowshands a fresh item identity to rows whose tool-call group, expanded state, or breakpoint changed, andHistoryStreamRowmemoizes on both. Every viewport runs its history through that hook; the web viewport once skipped it and history hosts of a live tool group went stale. A new field onStreamLayoutItemmust be added toareLayoutItemsEquivalent, or sharing silently stops. - Streaming cost must not grow with revealed history. Reading older messages must not make the next reply progressively more expensive to display. Reuse unchanged history work while text arrives, and release obsolete window caches as the reader reveals more history; window caches must stay bounded while their source timeline remains loaded.
Measuring
- Smoothness (user-perceived):
packages/app/e2e/browser/agent-stream-smoothness.spec.ts, gated behindPASEO_AGENT_STREAM_PERF_E2E=1. Drives the mock provider'sbursty-streammodel and reports coefficient of variation of characters painted per frame (smoothness) plus p95 gap between visible updates (stalls). Both numbers are needed: a stalled stream is perfectly smooth. - Reproducing bursty arrival: the
bursty-streammodel inmock-load-test-agent.tsemits uneven runs of tokens separated by idle gaps. Burst sizes come from a seeded generator, so a run repeats exactly. - Rate policy in isolation:
computeRevealStepinpackages/app/src/agent-stream/text-reveal.tsis pure;text-reveal.test.tscovers convergence and burst flattening without a renderer. - Per-delta cost on a long timeline:
npx vitest bench --project unit src/agent-stream/stream-tick.bench.tsfrompackages/appreplays 200 deltas over a 2,000-item timeline through the pure model and layout pipeline, with the default mounted window and with history fully revealed. It measures the JS work per delta, not paint.
Healthy numbers (2026-08, Expo web against a local dev daemon, real Claude Haiku agent, ~8.5s samples during active streaming). Setting TEXT_REVEAL_HORIZON_MS to 0 makes the reveal paint on arrival, which is how the baseline column was taken:
| paint on arrival | paced | |
|---|---|---|
| frames that advanced the text | 6% | 87% |
| chars-per-frame CV | 4.11 | 1.86 |
| gap between visible updates, p50 | 317ms | 17ms |
| gap between visible updates, p95 | 383ms | 17ms |
Total characters painted is roughly the same either way — the reveal changes when they land, not how many arrive.
Measure the total length across every assistant-message element, not the last one. A turn emits many assistant messages, so the tail element keeps changing identity and its length is not monotonic; sampling only the tail reads those handovers as resets and reports almost no growth. sampleStreamFrames does this correctly.