Repository navigation
refactor(terminal): read the terminal screen through one TerminalScreen interface - #4657
Conversation
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
…TerminalScreen.active()
|
Reviews on 42f6ae0: an omo gate review (BLOCK, one MEDIUM) and a Codex adversarial review (gpt-6.2-sol-fast, xhigh; two LOWs). Fixed in the next commit:
Validation after the fix: |
|
@coderabbitai review |
Action performed
|
|
Merged through Reviews on this PR: omo gate reviewer (BLOCK on 42f6ae0, one MEDIUM: file-link click across a buffer switch) and Codex adversarial review (gpt-6.2-sol-fast xhigh, two LOWs); all three fixed in c08c177 with a regression test that fails on the old wiring. CodeRabbit reviewed c08c177 with no actionable comments. Behaviour-neutral refactor; secretScreen.ts reads the same rows as before. Merged on the lane brief phone-engine-x1 (owner OK 2026-11-25). |
…4669) ## Summary Step 1 of moving the terminal's screen reads from xterm to libghostty (after X1, #4657). Today every byte is parsed twice: libghostty draws the screen and xterm's buffer answers every read. The goal is a phone on herdr-core where xterm parses only the control sequences its input side needs, so each reader has to move to libghostty first. The masked-prompt detection goes first because it is the security-relevant one. - `shared/ghostty/engine.ts`: adds `rowText(y)`, which returns a kept row's code points, widths and graphemes plus its soft-wrap flag (no style reads), and `rowCells(y)`, which returns styled cells. Both look up one grid ref per row and step its `x` along the row, so a cell costs one wasm call. Also adds `screenCursor()` and `alternate()`. - `src/lib/ghostty/screenRows.ts`: `ghosttyRows(vt)` gives libghostty's kept rows the shape of xterm's buffer (`ScreenRows`). History rows beyond the configured scrollback are left out the way xterm drops them, because libghostty frees history a page at a time. - `src/lib/terminalScreen.ts`: `screenRows(term)` returns libghostty's rows while the overlay holds the screen and xterm's otherwise. The overlay registers itself after it attaches and unregisters on dispose. If libghostty fails, it also gives up drawing, and reads go back to xterm. - `src/lib/secretScreen.ts` reads `screenRows(term)`. Every other reader still reads xterm. ## Same answer as before - `secretScreen.test.ts` runs all 16 cases twice, once on xterm's screen and once on libghostty's (fed each byte before xterm parses it, as the overlay does). All 32 pass. As a mutation check, I broke `translateToString` in the libghostty rows; that made all 16 libghostty cases fail and left the xterm cases passing. - `screenRows.test.ts` compares the two engines on every row and every cell. It covers text, wide cells, ZWJ/flag graphemes, bold, dim, italic, underline, strike, overline, inverse and invisible, palette, 256-colour and RGB colours, an erase that leaves only a background, soft wraps, history with a wide cell pushed to the next row, the alternate screen, the cursor, `length`, `baseY` and `viewportY`. The two engines match exactly on all of it. - Replay of a real 1.56 MB Claude Code recording, chunk by chunk (381 chunks, 80x37 with 2,000 lines of history and 49x35 with none): the prompt verdict differed on 0 of 381 chunks. ## Known difference xterm keeps a stale wrap flag on rows that DL/IL shift and on the row after an erased one; libghostty clears it. With `erase-ends-wrap.patch` (an erase from a row's first column, or a line feed onto a row, ends the wrap into it, as in xterm), a 120x37 replay of the real recording matches xterm on every row's text and wrap flag. ## Cost (phone emulation: 390x844, DPR 3, CPU 4x slower; real path: isolated herdr-core -> Ovis -> WebSocket; 1.56 MB Claude recording) - The masked-prompt reads themselves, timed in the page: main 113 ms over ~108 reads, this branch 236 ms over ~109 reads (2 runs each, ABBA). That is about +1.1 ms per read, or roughly +120 ms (about 3%) of main-thread time for the whole replay. - The whole replay, 5 runs each, ABBA, pinned to 8 idle cores (median [min–max]; main = origin/main, branch = 571c62b): | metric | main | branch | |---|---:|---:| | main thread ms | 3761 [3564–4467] | 4471 [3317–5127] | | script ms | 2142 [1951–2486] | 2449 [1901–2862] | | layout+style ms | 787 [775–922] | 946 [704–1069] | | start to settled ms | 3969 [3730–5014] | 4858 [3539–5743] | | long tasks over 50 ms | 7 [6–12] | 10 [4–25] | | JS heap growth MB | 3.8 [2.8–3.9] | 3.5 [2.1–3.9] | | browser CPU ms | 8000 [7540–9250] | 9130 [7380–10480] | The whole-replay spread is wider than the difference: layout+style moved by about the same share, and this change touches no DOM. The machine's load was 37–82 during the runs. The direct timing above is the cost this change adds. All 10 runs received the replay byte-exact. - What this buys: in step 7 xterm stops parsing every byte. The earlier prototype measured about 1.9x less main-thread time for the same replay. ## Validation - See the validation comment for the checks that ran and their results.
Summary
Step X1 of the terminal engine work: every place the client reads what an xterm terminal shows (rows and cells, cursor, history position, selection, OSC 8 links) now goes through one interface,
TerminalScreeninsrc/lib/terminalScreen.ts, with xterm as its only implementation. No behaviour change; a later step can answer the same reads from libghostty (masked prompt first).ScreenRows/ScreenLine/ScreenCellfollow xterm's buffer API, so xterm's rows and cells serve as they are (no per-cell copies, same hot-path cost) and the existing tests that passterm.buffer.activestill type-check.terminalScreen(term)is cached per terminal and reads whichever buffer is active at the time of the read, as the oldterm.buffer.activereads did.hyperlinkAt(xterm internals) moved from terminalFold into the xterm implementation.clearSelection/scrollLines.Direct reads outside
terminalScreen.ts(git grep -nE '\.buffer\.active|\bIBuffer(Line|Cell)?\b|\b(term|terminal|shown)\.(hasSelection|getSelection|getSelectionPosition)\(|_oscLinkService|_selectionState'over non-testsrc/): 52 before (11 files) -> 0 after.Validation
bun run check fast: passed (typecheck, build, 27444 unit tests).bun test src/lib/inputRow.test.ts src/lib/secretScreen.test.ts src/lib/terminalFileLinks.test.ts src/lib/terminalFold.test.ts src/lib/terminalWheel.test.ts: 306 pass.No changelog fragment: internal refactor with no user-visible change.