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
+9 -2
View File
@@ -1330,8 +1330,15 @@ app's terminal panel). Every request carries `terminal_request`
- `terminal_output {terminal_id, after, flow?}` replies with the retained
output (1 MiB tail) from byte `after` of the stream named `stream`, covering
bytes `[output_start, output_end)`; the output frames that follow the reply
start at `output_end`. Opening or reading subscribes the connection to the
terminal's output frames.
start at `output_end`. When `after` is before the retained tail (or
missing) and the stream is longer than the tail, the reply carries
`output_snapshot: true` instead: `output` is the server's redraw of the
terminal as of `output_end` (= `output_start`): a reset, the scrollback
(last 1000 lines), the main screen, the alternate screen when in use, the
cursor, modes (mouse, bracketed paste, cursor keys, keypad, ...) and pen,
from a headless emulator the server feeds with the terminal's output. A
client writes it into its emulator as it would a replay. Opening or
reading subscribes the connection to the terminal's output frames.
- `terminal_ack {terminal_id, after}` (flow-controlled subscriptions only):
the connection has drawn the output up to byte `after`. Not logged.
- `terminal_close {terminal_id}` kills the login or shell.