Files
pipelines/internal/server/terminal_screen.go
T
alan e13cb99ade 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>
2026-11-26 00:56:49 +00:00

38 lines
1.7 KiB
Go

package server
import "git.a-wan.net/alan/pipelines/internal/protocol"
// A machine terminal's output also feeds a headless emulator
// (internal/termstate), under the same lock as the retained tail, so its
// state always belongs to the tail's end. A client that reads from before
// the tail (a new page, a reload, a reconnect after the tail moved on, a
// gap) would otherwise redraw from an arbitrary cut: a full-screen program
// that wrote more than the tail since it started would land on the main
// screen, and its modes (mouse, bracketed paste, cursor keys, ...) would be
// lost. Such a read gets the emulator's snapshot instead: an escape
// sequence stream that resets the client's emulator and redraws the
// scrollback, the main screen, the alternate screen when in use, the
// cursor, the modes and the pen, as of OutputEnd; the frames that follow
// start there. A read inside the tail (an ordinary resume, or a stream not
// longer than the tail) gets the raw bytes as before.
// retainedFromLocked is where the retained tail starts: the newest
// TerminalRetained bytes (the buffer may hold more until it compacts).
func (p *terminalProcess) retainedFromLocked() int64 {
return max(p.out.start, p.out.total-protocol.TerminalRetained)
}
// screenLocked sets record's output to the screen's snapshot. p.mu is held.
func (p *terminalProcess) screenLocked(record *protocol.TerminalRecord) {
snap, partial := p.screen.Snapshot()
end := p.out.total
if !p.outDone {
// An unfinished character is left out of the snapshot and sent
// whole in the next frame.
end -= int64(partial)
}
record.Output = validChatShellText(snap)
record.OutputStart, record.OutputEnd = end, end
record.OutputSnapshot = true
}