Terminals: keep screen state on the server; snapshot on reload and gaps

A headless xterm-compatible emulator (internal/termstate) follows each
machine terminal's PTY output under the same lock as the 1 MiB tail. A
terminal_output from before the retained tail (new page, reload, gap)
now gets output_snapshot: an escape stream that resets the client and
restores scrollback (1000 lines), main screen, the alternate screen when
in use (via ?1049h so the program's exit brings the shell back), saved
cursors, tabs, scroll region, cursor incl. pending wrap, modes (mouse
tracking/encoding, bracketed paste, DECCKM, keypad, autowrap, origin,
focus, ...), charsets, pen and title, as of output_end; frames continue
there. Reads inside the tail stay raw.

No Go library was adequate: charmbracelet/x/vt and vito/midterm scroll at
0.2-4 MB/s and hide modes/pen/region; vt10x is unmaintained. termstate
runs at 160-200 MB/s.

The app treats a snapshot reply as a replay without the dropped-output
line, and every replay now starts with RIS so stale modes end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 00:56:49 +00:00
parent fc5d3bc42b
commit e13cb99ade
24 changed files with 2788 additions and 15 deletions
+71 -1
View File
@@ -198,6 +198,73 @@ Emulator (`chat_shell_terminal_feed_test.dart`, 896 KiB replay): one
synchronous write 160 ms; sliced, the same 161 ms over 20 turns, the
longest turn 12.5 ms.
## Screen state across reloads (2026-11-26)
The server keeps each machine terminal's *state*, not only its bytes, so
a client that attaches late is brought to the exact current screen.
- `internal/termstate` is a headless xterm-compatible emulator (own code;
see "Why not a library" below). `terminal_process.go` feeds it every PTY
read under `p.mu`, the same lock as the 1 MiB tail, so its state always
belongs to the tail's end; `applySize` resizes it with the PTY.
- `terminal_output` whose `after` is before the retained tail (the newest
1 MiB; a missing `after` counts as 0) gets `output_snapshot: true`:
`output` is the emulator's snapshot and `output_start == output_end`
is the offset it describes; the frames that follow start there, and the
flow window counts from it as from any reply (`terminal_screen.go`). A
read inside the tail (an ordinary resume, or a stream not longer than
1 MiB) is raw bytes as before.
- The snapshot (`termstate.Snapshot`) is an escape stream for a reset
terminal: `RIS` + `ED 3`, the title (OSC 2), the inactive alternate
screen if it holds anything (`?47h` … `?47l`), the last 1000 scrollback
lines and the main screen row by row (soft wraps wrap again), then, if
the alternate screen is in use, the main screen's saved cursor and
`?1049h` (so the program's exit restores it) and the alternate screen
drawn row by row; then the saved cursor(s), tab stops, scroll region,
origin mode, the cursor (a pending wrap is recreated by rewriting the
last cell; the cell before the cursor is rewritten when it holds the last
character, for REP), modes (DECCKM, DECKPAM, autowrap, IRM, LNM, reverse
video, cursor blink/visibility/shape, mouse tracking 9/1000-1003 and
encoding 1005/1006/1015, focus 1004, bracketed paste 2004, 1007), G0/G1
character sets and SO, the pen (SGR incl. 256/RGB colours and underline
styles) and the start of an unfinished escape sequence or OSC string. An
unfinished UTF-8 character is left out (`output_end` is before it; the
next frame carries it whole).
- Client: `TerminalTab._read` resets its held copy and replays when the
reply is a snapshot (`TerminalStream.snapshot`, `ChatShellRun.snapshot`:
no "earlier output was dropped" line). `ChatShellTerminalSession.replay`
now writes RIS before any replay, so a raw replay no longer inherits
stale mouse/paste/alt-screen modes either.
- Chat shell interactive commands are not covered: their readbacks come
from the run record (DB snapshot every 2 s of the raw tail) on a
separate path; they get only the RIS-on-replay change.
Why not a library: `github.com/charmbracelet/x/vt` v0.1.0 (MIT, active)
keeps scroll region, pen and modes private and scrolls by copying cells:
measured 0.2-1 MB/s for `seq` and 4.5 MB/s for wrapping text, 33 MB/s for
redraws. `github.com/vito/midterm` v0.2.5 (MIT): 1-4 MB/s scrolling,
ignores keypad and most modes. `github.com/hinshun/vt10x`: unmaintained
since 2022, no scrollback or bracketed paste. termstate moves rows by
pointer, keeps blank cells implicit and recycles evicted scrollback rows.
Measured (`go test ./internal/termstate -bench .`, Ryzen 7 5800X, 8 MB
each, 32 KiB writes): `seq` 190 MB/s, coloured full-screen redraws 160-165
MB/s, wrapping mixed text 172-180 MB/s, at 80x24 and 200x50; a snapshot of
1000 full 200-column scrollback rows: 2 ms, 229 KiB (seq: 9 KiB; a 40-row
TUI: 4-39 KiB). Cells: 230 KiB per terminal for `seq`, up to 5.9 MB at
200 columns with 1000 full rows. End to end (`TestMeasureTerminalStream`,
same machine, two runs each, before → after): seq 20.8/22.8 → 25.0/25.3
MB/s, cat 80/102 → 80/76 MB/s, yes 10.4/11.4 → 10.4/10.7 MB/s, echo
median 0.36/0.41 → 0.48/0.38 ms: no measurable cost.
Known gaps: origin mode with the cursor outside the scroll region (a DECRC
can leave it there) is not recreated; hyperlinks (OSC 8), palette changes
(OSC 4/10/11), DECSLRM and kitty keyboard modes are not tracked; DECSC's
origin flag is not saved (as in the app's emulator); scrolled-in rows are
blank in the default colour (as in the app's emulator, not xterm's BCE).
The server emulates at the PTY size; a client drawing at another size gets
the snapshot at the PTY's.
## Login and lifetime (server)
- `internal/server/terminal_login.go`. A terminal runs, in its PTY,
@@ -276,7 +343,10 @@ longest turn 12.5 ms.
- Server: `internal/protocol/terminal.go`, `message.go`;
`internal/pipeline/terminals.go`, `store.go`;
`internal/server/terminal_{manager,supervise,process,login,legacy_tmux,clients,title,cwd,handlers}.go`,
`internal/server/terminal_{manager,supervise,process,login,legacy_tmux,clients,title,cwd,handlers,flow,screen}.go`,
`internal/termstate/` (headless emulator and snapshots; tests
`snapshot_test.go`, `random_test.go`, `bench_test.go`),
`terminal_screen_test.go`,
`websocket.go`, `cmd/pipelines-server/main.go`; tests `terminal_ws_test.go`,
`terminal_login_test.go`, `terminal_lifetime_test.go`, `terminal_legacy_tmux_test.go`,
`internal/pipeline/terminals_test.go`; `docs/cli.md`.