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
+12 -1
View File
@@ -11,6 +11,7 @@ import (
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/termstate"
)
// terminalProcess is one terminal this server process holds: running (its
@@ -20,6 +21,8 @@ type terminalProcess struct {
mu sync.Mutex
record protocol.TerminalRecord
out *chatShellOutput
// screen follows the output (terminal_screen.go).
screen *termstate.Terminal
titles terminalTitles
// login follows the output until the login completes; loginChanged is
// signalled when it changes.
@@ -54,6 +57,7 @@ func newTerminalProcess(record protocol.TerminalRecord) *terminalProcess {
return &terminalProcess{
record: record,
out: newChatShellOutput(protocol.TerminalRetained),
screen: termstate.New(record.Cols, record.Rows, termstate.DefaultScrollback),
input: make(chan []byte, protocol.ChatShellInputQueue),
done: make(chan struct{}),
views: map[*terminalView]struct{}{},
@@ -87,10 +91,16 @@ func (p *terminalProcess) snapshot(output bool, after int64) protocol.TerminalRe
}
// snapshotLocked is snapshot with p.mu held. The output ends at OutputEnd:
// an incomplete trailing character waits for the next frame.
// an incomplete trailing character waits for the next frame. A read from
// before the retained tail gets the screen's snapshot instead of the tail
// (terminal_screen.go).
func (p *terminalProcess) snapshotLocked(output bool, after int64) protocol.TerminalRecord {
record := p.record
record.OutputEnd = p.out.total
if output && after < p.retainedFromLocked() {
p.screenLocked(&record)
return record
}
if output {
from := max(after, p.out.start)
text, start, end, ok := p.out.chunk(from, p.out.total, protocol.TerminalRetained, p.outDone)
@@ -150,6 +160,7 @@ func (p *terminalProcess) read(terminal *os.File, titled func(string)) {
if n > 0 {
p.mu.Lock()
p.out.write(buffer[:n])
p.screen.Write(buffer[:n])
p.pokeViewsLocked()
if p.login.feed(buffer[:n]) {
select {