Skip to content

fix(cli): cache per-item layout in replay_full to kill zarvis typing lag - #127

Merged
edwin-zvs merged 1 commit into
mainfrom
fix-zarvis-typing-lag
May 22, 2026
Merged

edwin-zvs merged 1 commit into
mainfrom
fix-zarvis-typing-lag

Conversation

@edwin-zvs

Copy link
Copy Markdown
Contributor

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 incremental replay_cached fast path. replay_full's vt100 parser feed was already incremental, but every frame it still walked all history items calling count_visible_lines / synth_block to 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 in pending_chunk (one big streaming chunk, handled incrementally). A real working session flushes chat into many Item::PtyChunk entries interleaved with Item::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 EditorState event, 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 to items. 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 any cols change 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).

  • Before: ~349 µs/frame (release), scaling linearly with history.
  • After: ~13 µs/frame, flat regardless of history size.

The test asserts < 300 µs/frame, so it fails on the old code and passes on the new. All existing pty_render perf + per-harness regression tests still pass (127 cli + 1 integration).

Built binary

Only touches crates/cli → relevant binary is agent at /Users/moon/agentd/.claude/worktrees/fix-zarvis-typing-lag/target/debug/agent. Other workspace binaries built but unchanged from main.

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
edwin-zvs merged commit f0dbaf6 into main May 22, 2026
1 check passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant