Skip to content

fix(ui): keep a running chat's replay buffer small and capped - #5343

Merged
kovtcharov-amd merged 4 commits into
amd:mainfrom
kovtcharov:claude/rel-ui-replay-buffer
Oct 19, 2026
Merged

kovtcharov-amd merged 4 commits into
amd:mainfrom
kovtcharov:claude/rel-ui-replay-buffer

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

Before: every SSE frame a chat turn sent stayed in that run's replay buffer: each text chunk padded to 512 bytes, plus a keepalive every 5 seconds. A long turn grew the buffer without limit, and every time the user switched back to the chat, the whole thing was replayed. After: the buffer keeps only what a re-attaching client needs to rebuild the reply, so a normal reply stays at tens of kilobytes. It's capped at 8 MB; past that the oldest progress is trimmed behind a note that says so.

Reproduced by tests/unit/chat/ui/test_replay_buffer_bounds.py: on main, a 3,000-chunk reply keeps all 3,601 frames and nothing caps the size. Here it keeps about 600 merged frames and replays the identical answer text.

Overlaps #5242 and #5337, which also edit run_manager.py (Run.__init__, cancel()), in different lines.

Test plan

  • pytest tests/unit/chat/ui: all pass
  • python util/lint.py --black --isort --pylint --flake8 --test-lanes
🔍 Technical details
  • Run._record drops non-data: frames (keepalives, padding comments), stores frames unpadded, merges consecutive {"type","content"}-only chunks, and replaces a status: working that directly follows another. That matches how ChatView consolidates them.
  • subscribe() pads replayed frames back to 512 bytes, the Chromium flush rule the producer already follows.
  • Live subscribers still get every frame exactly as emitted; only the replay copy changes.
  • Not LLM-affecting.

@github-actions github-actions Bot added the tests Test changes label Oct 18, 2026
@kovtcharov
kovtcharov marked this pull request as ready for review October 18, 2026 16:56
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

This keeps a running chat's replay buffer small: keepalives and padding are dropped, streamed answer text is merged, repeated "Model is processing" ticks collapse, and there's an 8 MB cap with a visible note when older progress is trimmed. The logic holds up, live subscribers still get every frame unchanged, and the tests reproduce the original growth.

  • Thinking output isn't merged, so thinking models get little benefit. Every streamed thinking token is still its own frame, and each is padded back to 512 bytes on replay. In a simulated turn with 5,000 thinking tokens, the buffer held 5,000 frames and a re-attach replayed about 2.5 MB, which is the same as before this PR. The default model on large machines runs with thinking on. The chat UI already appends consecutive thinking events into one step, so merging them here the way answer chunks are merged would be safe. Worth doing in this PR or as a quick follow-up.
  • No real-world evidence of the re-attach path. Switching away from a running chat and back is something users do, but the PR only shows unit tests. A before/after of switching back mid-turn in gaia chat --ui would close this gap. Treat it as a nudge, not a blocker.

Real-world evidence

N/A: no evidence bundle was produced for this run. The verdict rests on static review plus a local simulation of the buffer (shown below). pytest wasn't available on this runner, so the PR's own tests weren't re-run here.

🔍 Technical details

🟡 Thinking tokens bypass the merge (src/gaia/ui/run_manager.py, _record / _plain_chunk)
sse_handler.py emits {"type": "thinking", "content": ...} per token while inside a <think> block (the "Still inside thinking — emit partial" branch). _plain_chunk only matches type == "chunk", so each thinking token is appended as its own frame, and subscribe() re-pads each one to 512 bytes. Local simulation (5,000 thinking tokens followed by 500 chunks):

buffer frames: 5000   buffer bytes: 243,890   replayed frames: 5001   replayed bytes: 2,562,430

ChatView.tsx (~line 861) calls appendThinkingContent when the previous step is also thinking, so concatenating consecutive {type, content} thinking events renders the same result. One way to do it: generalize the open-chunk state to "open merge of type T" for T in {"chunk", "thinking"}, and close it when the type changes. The cap still bounds the worst case today, which is why this is rated important rather than critical.

🟢 list.pop(0) / list.pop(2) once past the cap
Both are O(n) per trimmed event. At 8 MB of small parts, _open_chunk can hold on the order of 10⁶ elements, so every token after the cap triggers a large memmove. This is unrealistic for normal replies (it needs megabytes of uninterrupted text), but a collections.deque for _open_chunk would remove the cliff cheaply.

🟢 Byte accounting counts characters, not bytes
_buffer_bytes uses len(frame). This is correct today because the producer uses json.dumps with default ensure_ascii=True, but the "sized as it will be sent" comment on the open-chunk path implies the whole buffer is measured that way. It only holds while the producer escapes non-ASCII.

Checked and fine

  • Trim keeps the first frame, the note, and the newest frame. The note can't be popped by the progress-tick replacement, because a pending open chunk is closed (which sets _last_event to chunk) before any tick comparison.
  • _padded always yields frames of 512 bytes or more. Non-JSON data: frames (e.g. data: 1 in the existing tests) parse to None and are kept verbatim.
  • emit/subscribe both run on the event loop, so there's no race between the replay snapshot and live fan-out.

Strengths

  • The replay copy and the live stream are cleanly separated, so live clients see no behaviour change.
  • The tests target the real failure mode (a 3,000-chunk turn, capped trim with a visible note, non-ASCII sizing, newest-frame survival), not just the happy path.
  • Trimming tells the user instead of silently dropping progress, which fits the repo's fail-loudly rule.

kovtcharov and others added 4 commits October 18, 2026 23:14
Every SSE frame a turn emitted (each chunk padded to 512 bytes, a
keepalive every 5 s) stayed in the run's replay buffer, so a long turn
grew it without limit and each re-attach replayed all of it.

The buffer now keeps only what a re-attaching client needs: keepalives
and padding are dropped, consecutive chunks merge, and a 'working' status
replaces the one before it. Replayed frames are padded again so the
browser still flushes them at once. Past 8 MB the oldest progress is
trimmed behind a note that says so.
Review follow-ups on the replay buffer:

- Only the stream loop's periodic 'Model is processing' tick replaces the
  one before it. Other working statuses stay, so a re-attached chat shows
  the same rows as the live one.
- Streaming text is kept as parts and joined only when replayed or when
  the next event closes it, instead of re-serialising the whole reply on
  every chunk; a reply bigger than the cap keeps its end.
Review hardening: a trim never drops the newest frame, and chunk text is
sized as its JSON escape (non-ASCII text grows up to six times), so the
cap holds for any language.
A thinking model streams thousands of thinking tokens, and each stayed its
own replay frame, padded to 512 bytes on re-attach, so the buffer saved
nothing for those turns. Consecutive thinking tokens now merge into one
frame, as the chat already shows them as one step. Trimming the oldest
text of an oversized reply no longer slows down past the cap.
@kovtcharov-amd
kovtcharov-amd force-pushed the claude/rel-ui-replay-buffer branch from fa404c0 to 6199bc8 Compare October 19, 2026 06:22
@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Oct 19, 2026
Merged via the queue into amd:main with commit 349830b Oct 19, 2026
33 of 35 checks passed
@kovtcharov-amd
kovtcharov-amd deleted the claude/rel-ui-replay-buffer branch October 19, 2026 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants