Repository navigation
fix(cli): cache per-item layout in replay_full to kill zarvis typing lag - #127
Merged
Merged
Conversation
Sessions with tool blocks (zarvis) render through `replay_full`, which — even in steady state — re-walked every history item each frame calling `count_visible_lines` / `synth_block` to position tool-block hit rects. That's O(total history bytes) per frame. The parser feed was already incremental, and the streaming bootstrap case is fast because the bulk sits in `pending_chunk` (handled incrementally). But a real working session FLUSHES chat into many `Item::PtyChunk` entries interleaved with `Item::ToolBlock`s, so the per-frame re-scan grew with the transcript. Every keystroke triggers a redraw (the zarvis input buffer round-trips as an EditorState event), so the O(history) cost was paid on every character → "super laggy" typing. Cache each item's rendered layout (visible-line count + tool-block hit-rect metadata) on `CachedParser`, parallel to `items`. Steady state reuses the unchanged prefix and only computes layouts for newly-appended items; cumulative line positions are summed from the cached layouts (O(items), no byte scan). Layouts are cleared on rebuild, which already fires on any cols change or item mutation — the only things that can invalidate them. Repro test (`zarvis_steady_state_render_is_cheap_with_many_items`): a 300-cycle chat+tool history. Steady-state re-render drops from ~349 µs/frame to ~13 µs/frame (release) and no longer scales with history size. All existing pty_render perf + per-harness regression tests still pass.
edwin-zvs
added a commit
that referenced
this pull request
May 22, 2026
#128) #127 cut the steady-state replay_full cost, but typing was still slow. The real culprit: as the user types, the zarvis editor pane grows and `chat_area.height` shrinks, so render_terminal handed `replay` a smaller `rows` on (nearly) every keystroke. replay_full treats any rows change as a full O(history) vt100 rebuild — measured at ~26 ms/keystroke on a modest 300-cycle session in debug, and it scales linearly with history size. #127's steady-state test kept rows constant, so it never exercised this path. Fix it in the render layer instead of the parser: render the chat at the stable full pane height and show only its bottom `chat_area.height` rows (the editor pane covers the rest). Editor growth no longer changes the rows passed to `replay`, so the parser is never resized — and never rebuilt — per keystroke. A naive `set_size`-shrink was rejected: vt100 truncates from the bottom, dropping the most recent lines. Tool-block hit-rects are translated from full-screen rows into chat-area-relative rows (`translate_block_hits`); blocks scrolled above the slice are dropped, partially-visible ones clipped. No-editor-pane sessions (claude/codex/shell) get row_offset 0 — identical behavior. Tests: structural guard that editor growth doesn't resize the chat parser (and the shrunk chat still shows the newest content), plus unit coverage for the hit-rect translation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Typing into a long zarvis session in the TUI was "super laggy". Other harnesses (claude/codex/shell) were fine.
Root cause
Sessions with tool blocks (only zarvis) render through
ItemHistory::replay_full; everything else takes the incrementalreplay_cachedfast path.replay_full's vt100 parser feed was already incremental, but every frame it still walked all history items callingcount_visible_lines/synth_blockto position tool-block hit rects — O(total history bytes) per frame.The existing perf test (
replay_after_large_history_is_cheap_per_chunk) didn't catch this because it keeps the bulk inpending_chunk(one big streaming chunk, handled incrementally). A real working session flushes chat into manyItem::PtyChunkentries interleaved withItem::ToolBlocks, so the per-frame re-scan grows with the transcript.Why typing specifically: the zarvis input buffer isn't echoed locally — each keystroke round-trips to the adapter and comes back as an
EditorStateevent, which triggers a redraw. So the O(history) re-scan was paid on every character.Fix
Cache each item's rendered layout (visible-line count + tool-block hit-rect metadata) on
CachedParser, parallel toitems. Steady-state frames reuse the unchanged prefix and only compute layouts for newly-appended items; cumulative line positions are summed from the cached layouts — O(items), no byte scan. Layouts are cleared on rebuild, which already fires on anycolschange or item-signature mutation (the only things that can invalidate a layout).Test
New
zarvis_steady_state_render_is_cheap_with_many_items: builds a 300-cycle chat+tool history (~600 items) and measures steady-state re-render (what a keystroke's redraw does).The test asserts
< 300 µs/frame, so it fails on the old code and passes on the new. All existingpty_renderperf + per-harness regression tests still pass (127 cli + 1 integration).Built binary
Only touches
crates/cli→ relevant binary isagentat/Users/moon/agentd/.claude/worktrees/fix-zarvis-typing-lag/target/debug/agent. Other workspace binaries built but unchanged from main.