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
@@ -27,6 +27,7 @@ class ChatShellRun {
this.outputEnd = 0,
this.outputBytes = 0,
this.gap = false,
this.snapshot = false,
this.terminal = false,
this.cols = 0,
this.rows = 0,
@@ -59,6 +60,11 @@ class ChatShellRun {
/// stretch of what the command wrote.
final bool gap;
/// True when [output] starts with the server's redraw of the terminal's
/// state at [outputStart] (screens, scrollback, cursor and modes) instead
/// of raw output: nothing earlier is missing from what it shows.
final bool snapshot;
/// True when the command runs in a pseudo-terminal of [cols] x [rows]:
/// [output] is then the raw terminal stream and it accepts typed input
/// while it runs. Older runs (and servers without terminals) are plain.
@@ -78,7 +84,7 @@ class ChatShellRun {
final bool purged;
/// Whether earlier output is not shown here.
bool get truncated => outputStart > 0 || gap;
bool get truncated => !snapshot && (outputStart > 0 || gap);
ChatShellRunState get state => switch (status) {
'running' => ChatShellRunState.running,
@@ -144,6 +150,7 @@ class ChatShellRun {
? next.outputBytes
: outputBytes,
gap: gap,
snapshot: snapshot,
terminal: next.terminal || terminal,
cols: next.cols == 0 ? cols : next.cols,
rows: next.rows == 0 ? rows : next.rows,
@@ -180,6 +187,7 @@ class ChatShellRun {
outputEnd: end,
outputBytes: end > outputBytes ? end : outputBytes,
gap: gap || dropped || (!contiguous && offset > 0),
snapshot: snapshot && contiguous && !dropped,
terminal: terminal,
cols: cols,
rows: rows,
@@ -96,8 +96,12 @@ class ChatShellTerminalSession {
}
/// Draws [run]'s held output from scratch: on open, after a gap and after
/// a readback replaced the card's copy. Answers the emulator gives to old
/// queries in that output are not sent: the process asked them long ago.
/// a readback replaced the card's copy. A machine terminal's read from
/// before the server's tail is the server's snapshot
/// ([ChatShellRun.snapshot]), which resets the emulator and restores the
/// screens, scrollback, cursor and modes; it is drawn the same way.
/// Answers the emulator gives to old queries in that output are not sent:
/// the process asked them long ago.
/// A large output is drawn in slices across turns of the event loop
/// (ChatShellTerminalFeed), so it never stalls the UI.
void replay(ChatShellRun run) {
@@ -109,6 +113,9 @@ class ChatShellTerminalSession {
terminal.useMainBuffer();
terminal.buffer.clear();
terminal.buffer.setCursor(0, 0);
// A full reset (RIS): modes a program set in the output drawn before
// (mouse reporting, bracketed paste, the alternate screen) end too.
terminal.write('\x1bc');
terminal.setCursorVisibleMode(true);
terminal.write('\x1b[0m');
if (run.truncated) {