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
+11 -3
View File
@@ -31,9 +31,11 @@ const (
// terminal is its first active client. The reply echoes the terminal.
TerminalResize = "terminal_resize"
// TerminalOutput reads one terminal with its retained output from the
// absolute byte After on (all of it when After precedes the retained
// tail; OutputStart says where it starts). It also subscribes the
// connection to the terminal's terminal_event output frames.
// absolute byte After on (OutputStart says where it starts). When After
// precedes the retained tail (the newest TerminalRetained bytes; a stream
// no longer than that is read whole), the reply is a snapshot of the
// terminal's state instead (OutputSnapshot). It also subscribes the connection to the terminal's
// terminal_event output frames.
TerminalOutput = "terminal_output"
// TerminalClose ends a terminal: its login or shell is killed
// and its record removed. The reply echoes the last record.
@@ -127,6 +129,12 @@ type TerminalRecord struct {
OutputEnd int64 `json:"output_end"`
Output string `json:"output,omitempty"`
OutputStart int64 `json:"output_start,omitempty"`
// OutputSnapshot marks a terminal_output reply whose Output is not raw
// output but the server's redraw of the terminal as of OutputEnd (which
// OutputStart equals): it resets the emulator and restores the
// scrollback, the screens, the cursor and the modes. A client writes it
// into a reset emulator as it would a replay.
OutputSnapshot bool `json:"output_snapshot,omitempty"`
// Window is set on the terminal_open and terminal_output replies of a
// flow-controlled subscription: the bytes the server sends past the
// last terminal_ack. Its absence means the server takes no acks.