Resume Cloud terminal replays inside escape sequences

Merged
LA
2mo ago
+1066-83

Opening or resizing a Cloud terminal while an agent streams output could print escape-sequence fragments as text (1;31mstyled, 8;;https://example.comlink) or disconnect every viewer. The daemon built each byte-mode replay from its screen but dropped the escape sequence or UTF-8 code point its parser was in the middle of, so the live bytes that completed it landed in a fresh parser as plain text. A resize at such a byte cancelled all byte attachments instead.

The VT boundary tracker in ghostty-vt now keeps the bytes fed since the last safe point (up to 1 MiB; direct Kitty uploads keep their own larger budget). VtReplay.bytes still ends at a parser boundary and the incomplete sequence travels separately as pending_sequence, because consumers write color-override OSCs right after a replay and those must not land inside the sequence. Consumers write pending last, after their colors and immediately before the live stream. A daemon resize now disconnects byte viewers only inside an oversized non-Kitty control string.

On the wire, vt-state and resized carry an optional base64 pending field only for attachments that advertise the new terminal-pending-sequence-v1 capability, matching how terminal-color-overrides-v1 avoids breaking strict SDK decoders. Other attachments get the bytes appended to the replay, which is correct for raw consumers such as chatmux-relay. The macOS pane, the iOS cmux-tui client and the cmux-tui remote client advertise it. The terminal-host protocol is unchanged: hosts still snapshot and resize only at a boundary. Spec: events.md, commands.md, transports.md.

Testing

Rust, on a Blacksmith Testbox (cargo test --locked, umask 022):

  • Red at ac210e0 (tests only): ghostty-vt theme_portable_replay_resumes_inside_every_partial_sequence, ..._zsh_startup_at_every_byte, replay_after_invalid_utf8_does_not_duplicate_replacement_characters, oversized_pending_sequence_is_reported_as_not_resumable, and cmux-tui-core byte_mirror_attached_inside_any_sequence_matches_the_terminal, byte_mirror_survives_owner_resize_inside_any_sequence, byte_mirrors_joining_during_owner_churn_converge_on_the_terminal all failed, printing fragments like 1;31mstyled.
  • Green at ab84d73. The byte-mirror tests model the pane (replay must end at a boundary before pending is written) and compare text, cursor and grid with the daemon at every byte split of a transcript with CSI, OSC (BEL and ST), OSC 8, DCS, APC and multi-byte UTF-8, including three viewers joining during owner resizes. Added: serializer tests for both wire shapes, and a remote-client test for replay, colors, pending ordering.
  • Full cargo test --locked --no-fail-fast on ab84d73 vs unmodified main (58a9cbc) on the same box: 4500 vs 4493 passing. The same 18 tests fail on both (hook, journal, plugin). Two more failed once on the branch under full-suite load (cmux-remote oversized_probe_stdout_kills_and_reaps_ssh with Text file busy, status_disables_repository_fsmonitor) and passed three isolated reruns; this PR does not touch that crate. cargo fmt --check, cargo clippy --locked --all-targets -D warnings and check-spec-inventory.py pass.

Swift, in CI's changed-suites app-host run:

  • Red at 929c2ea (test only): CloudRestoreReplayGridTests/replayResumesTheSequenceTheDaemonParserIsInside failed for vt-state and resized with the screen reading BEFORE 1mRED AFTER. It drives a real manual-I/O Ghostty surface through CloudTuiManualMirrorSession.
  • Green at ab84d73: that test and the new decoder test pass; 356 XCTest and 203 of 207 Swift Testing tests in 12 changed suites pass.
  • Four CloudRestoreReplayGridTests geometry-claim tests (hiddenRestoreReclaimsGeometryWithoutInput, intentionallyPassiveMirrorStillWaitsForExplicitFocus, typingIntoTheGeometryOwnerSendsOnlyOneWayInput, typingIntoAPaneAnotherClientSizesReclaimsTheGridFirst) time out waiting for resize-surface in both the red and green runs. At 929c2ea the Swift app code equals main, so they fail independently of this change; I could not get a clean main rerun because no recent main CI run kept app products.

Not verified: iOS beyond compilation (its CmuxTUI tests need the local sshd lab), and a live Cloud VM. VMs pick up the daemon side only after their guest daemon upgrades; an app build against an older daemon behaves as before.

Changelog

Fixed: Cloud terminals no longer show escape-sequence fragments or disconnect when opened or resized during streaming output

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Terminal replays and resizes now preserve incomplete control sequences, allowing subsequent output to continue correctly without displaying sequence fragments as text.
    • Remote and cloud terminal views retain the correct display state and colors when replay occurs mid-sequence.
    • Oversized incomplete control sequences that cannot be safely resumed are handled without corrupting the terminal stream.
    • Existing clients continue to receive incomplete sequence data as part of the replay.
  • Documentation
    • Documented how clients receive and apply pending sequence data during terminal attachment and resize.