Repository navigation
Fix Vim alternate-screen terminal redraw recovery - #7142
Conversation
📝 WalkthroughWalkthroughThis change adds alternate-screen switch tracking in Estimated code review effort: Medium Related issues: None specified Related PRs: None specified Suggested labels: terminal, webgl, tests Suggested reviewers: None specified 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/renderer/src/components/terminal-pane/pty-connection.test.ts (1)
8919-9036: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winGood coverage for the primary scenarios; consider a split-chunk regression test.
The three cases (already-alternate buffer, single-chunk enter sequence, ordinary rewrite) match the new decision logic well. Given the chunk-boundary gap flagged in
pty-connection.ts(containsAlternateScreenEnterSequence), consider adding a case that delivers\x1b[?1049hacross twoonDatacalls to lock in that fix once made.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 8e8dd19f-cbd9-4847-940e-cc46d901d8b7
📒 Files selected for processing (2)
src/renderer/src/components/terminal-pane/pty-connection.test.tssrc/renderer/src/components/terminal-pane/pty-connection.ts
Replace the raw-chunk scan for alternate-screen enter sequences with the xterm parser's own verdict: in-place rewrite chunks check buffer.active.type (plus a parse-time buffer-switch count) inside the write callback, where the buffer is authoritative. Covers enter/exit sequences split across PTY chunk boundaries and full enter-exit cycles coalesced into one write, and deletes the hand-rolled CSI parser. Adds regression tests from a real captured vim session (including a CSI sequence split at a 1024-byte PTY read boundary) and a contract pin against the real @xterm/headless parser. Co-authored-by: Orca <[email protected]>
* Fix Vim alternate-screen terminal redraw recovery * Decide alternate-screen atlas recovery from parsed buffer state Replace the raw-chunk scan for alternate-screen enter sequences with the xterm parser's own verdict: in-place rewrite chunks check buffer.active.type (plus a parse-time buffer-switch count) inside the write callback, where the buffer is authoritative. Covers enter/exit sequences split across PTY chunk boundaries and full enter-exit cycles coalesced into one write, and deletes the hand-rolled CSI parser. Adds regression tests from a real captured vim session (including a CSI sequence split at a 1024-byte PTY read boundary) and a contract pin against the real @xterm/headless parser. Co-authored-by: Orca <[email protected]> --------- Co-authored-by: Orca <[email protected]>
Summary
Fixes #7070 — Vim (and other full-screen TUIs) ghosting/garbled redraws in Orca's terminal.
Vim redraws with plain-ASCII cursor-move/erase rewrites that xterm parses correctly, but the shared WebGL glyph atlas can keep stale glyphs until it is rebuilt, so erased cells ghost ("eepo" over "expo"). Orca already forced a viewport refresh for in-place rewrites; this PR additionally schedules the existing coalesced WebGL atlas recovery for rewrites that touch the alternate screen.
How the alternate-screen check works. Whether a rewrite touched the alternate screen is decided at parse completion, not by scanning raw chunk bytes. xterm's
write(data, callback)contract runs the callback after the parser has processed that chunk (FIFO per chunk), and the alternate-screen switch (DECSET 47/1047/1049) happens synchronously inside the parse — sobuffer.active.typeread inside the callback is authoritative. Two captures make the coverage complete: the pre-parse buffer type (so a TUI's final frame — redraw +?1049lrestore in one chunk — still rebuilds the atlas for the restored prompt), and abuffer.onBufferChangeswitch count (so a full enter→draw→exit cycle parsed in one write — e.g. a coalesced backlog flush — is still seen even though the buffer type nets back tonormal).This supersedes the first revision of this PR, which scanned each raw chunk for
?47h/?1047h/?1049henter sequences. That scan missed sequences split across PTY chunk boundaries — and real vim output does split CSI sequences at 1024-byte PTY read boundaries (see Evidence). The post-parse check closes that gap and deletes the hand-rolled CSI parser outright; xterm's parser is the single authority on alternate-screen state.Screenshots
No visual change (bug fix; live-app verification below confirms clean vim rendering).
Testing
pnpm exec oxlint src/renderer/src/components/terminal-pane/pty-connection.ts src/renderer/src/components/terminal-pane/pty-connection.test.ts src/renderer/src/components/terminal-pane/terminal-alternate-screen-parse.test.tspnpm exec tsgo --noEmit -p config/tsconfig.tc.web.jsonpnpm exec vitest run --config config/vitest.config.ts src/renderer/src/components/terminal-pane/pty-connection.test.ts src/renderer/src/lib/pane-manager/pane-terminal-output-scheduler.test.ts(334 tests)pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/components/terminal-pane/terminal-alternate-screen-parse.test.ts src/renderer/src/components/terminal-pane/terminal-webgl-atlas-recovery.test.ts src/renderer/src/components/terminal-pane/terminal-visibility-resume.test.tsEvidence of fix
1. Real PTY capture proves the failure mode is real. Captured a real
vim -u NONE -N package.jsonsession through node-pty (120×34, macOS,TERM=xterm-256color): vim enters with\x1b[?1049h, redraws via plain-ASCII\x1b[K/cursor-move rewrites, and a 1024-byte PTY read boundary split the cursor move\x1b[30;5Hacross twoonDatachunks (…\x1b[K\x1b[30+;5H…). Any sequence — including the alternate-screen enter — can straddle a chunk boundary, which is exactly what byte scanning cannot see. The captured bytes are now regression fixtures.2. Before/after regression test.
schedules WebGL atlas recovery when the alternate-screen enter sequence splits across chunksfails against the first revision's chunk scanner (scheduleTerminalWebglAtlasRecoverycalled 0 times) and passes with the post-parse check. Additional tests pin: already-in-alternate redraws, the exit chunk (recovery for the restored prompt), the single-write enter+exit cycle (switch-count path), the real captured split-mid-CSI redraw bytes, and the negative case (ordinary shell rewrites never schedule recovery).3. Parser contract pin.
terminal-alternate-screen-parse.test.tsruns the real@xterm/headlessparser and locks the semantics the fix depends on:buffer.active.typereflects alternate-screen enter/exit by write-callback time, including the split-chunk case and the captured vim enter/redraw/exit chunks. If a future xterm upgrade changed this, the pin fails before ghosting could return.4. Live app verification. Dev build with
scheduleTerminalWebglAtlasRecoverytemporarily instrumented (call + coalesced-burst counters), realvimdriven through the rendered terminal via keyboard events, exercising the exact repro from #7070 (open JSON file, scrollG/gg/Ctrl-D/Ctrl-U,ciwedit,:q!):\rin-place rewritevimopensciwedit (issue repro):q!exit chunkRendering stayed pixel-clean throughout — no ghosting on scroll, on the in-place edit, or on the restored prompt. The instrumentation was removed before commit.
AI Review Report
Multi-agent adversarial review over four lenses (correctness, performance, test quality, codebase consistency), each finding independently verified against the real control flow before acceptance:
onParsedmore than once, which the parse-time predicate self-corrects (the final firing sees fully parsed state); the hidden-pane recovery path and Windows branches (ConPTY synchronized frames, East-Asian refresh) are untouched; snapshot-restore/replay writes bypass this path entirely. The review surfaced the single-write enter+exit cycle gap; fixed with theonBufferChangeswitch count plus regression tests (mock wiring + real-parser pin).Security Audit
No input execution, path handling, auth, secrets, or IPC surface touched. The change consumes already-parsed terminal state from xterm's public buffer API instead of pattern-matching raw PTY bytes, which removes a bespoke escape-sequence parser (strictly less code interpreting untrusted terminal output). Recovery scheduling remains coalesced (single-burst latch), so hostile output storms cannot queue unbounded timers.
Notes