vt: an alt-screen snapshot can break an escape sequence in the live terminal #53

Closed
opened 2026-10-02 09:55:32 +00:00 by jhgaylor · 1 comment
Owner

Found while fixing #1 (d774b26); it predates that change.

To format the primary screen and its scrollback while an alt screen shows, snapshot_history flips the live terminal with CSI ?47l, formats, then CSI ?47h (crates/vt/src/ghostty/wire.rs:140 and :151).

If the PTY stream last stopped in the middle of an escape sequence (the parser holds a continuation), that vt_write lands inside the unfinished sequence: the sequence is corrupted and the remainder of it, when it arrives, prints as text, in the engine and so for every client. Snapshots happen on every attach and resync, so this is most likely during floods, exactly when resyncs happen.

Fix ideas:

  • don't touch the live terminal: format the primary screen from a copy (GHOSTSNP decode), or from a formatter option that picks a screen if libghostty grows one;
  • or detect a pending continuation (non-empty continuation record in the GHOSTSNP prefix the wire cache already reads) and take the copy path only then.

Done when: a test feeds half an escape sequence (e.g. \x1b[38;2;1) under an alt screen, takes a snapshot, feeds the rest, and the live screen and a fresh snapshot match the same stream fed without the snapshot.

Found while fixing #1 (d774b26); it predates that change. To format the primary screen and its scrollback while an alt screen shows, `snapshot_history` flips the **live** terminal with `CSI ?47l`, formats, then `CSI ?47h` (`crates/vt/src/ghostty/wire.rs:140` and `:151`). If the PTY stream last stopped in the middle of an escape sequence (the parser holds a continuation), that `vt_write` lands inside the unfinished sequence: the sequence is corrupted and the remainder of it, when it arrives, prints as text, in the engine and so for every client. Snapshots happen on every attach and resync, so this is most likely during floods, exactly when resyncs happen. **Fix ideas:** - don't touch the live terminal: format the primary screen from a copy (GHOSTSNP decode), or from a formatter option that picks a screen if libghostty grows one; - or detect a pending continuation (non-empty continuation record in the GHOSTSNP prefix the wire cache already reads) and take the copy path only then. **Done when:** a test feeds half an escape sequence (e.g. `\x1b[38;2;1`) under an alt screen, takes a snapshot, feeds the rest, and the live screen and a fresh snapshot match the same stream fed without the snapshot.
Author
Owner

Fixed on main: fe2dac2 (Merge #53). The live terminal is flipped with ?47l/?47h only when its parser is at ground. Mid-sequence, the primary screen is formatted from a GHOSTSNP copy (CAN ends the copy's sequence), and the live terminal is never written to. Wire snapshots now also end with the pending continuation, so a client fed a mid-sequence snapshot finishes the sequence correctly. Tests: an_alt_screen_snapshot_mid_sequence_leaves_the_stream_alone (CSI, ESC, OSC, UTF-8, over-limit OSC, on both screens) and a reattach.spec.ts Playwright case that fails on the old main. PLAN.md: S1 vt/snapshot notes.

Fixed on main: fe2dac2 (Merge #53). The live terminal is flipped with ?47l/?47h only when its parser is at ground. Mid-sequence, the primary screen is formatted from a GHOSTSNP copy (CAN ends the copy's sequence), and the live terminal is never written to. Wire snapshots now also end with the pending continuation, so a client fed a mid-sequence snapshot finishes the sequence correctly. Tests: an_alt_screen_snapshot_mid_sequence_leaves_the_stream_alone (CSI, ESC, OSC, UTF-8, over-limit OSC, on both screens) and a reattach.spec.ts Playwright case that fails on the old main. PLAN.md: S1 vt/snapshot notes.
jhgaylor referenced this issue from a commit 2026-10-02 12:48:47 +00:00
Commenting is not possible because the repository is archived.
No milestone
No project
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jhgaylor/illogical#53
No description provided.