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:
@@ -287,6 +287,7 @@ func (p *terminalProcess) applySize(cols, rows int) error {
|
||||
cols, rows = chatShellTerminalSize(cols, rows)
|
||||
p.mu.Lock()
|
||||
p.record.Cols, p.record.Rows = cols, rows
|
||||
p.screen.Resize(cols, rows)
|
||||
terminal := p.pty
|
||||
p.mu.Unlock()
|
||||
if terminal != nil {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
//go:build darwin || linux
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/termstate"
|
||||
)
|
||||
|
||||
// tuiCommand enters the alternate screen, sets mouse, bracketed paste and
|
||||
// application cursor/keypad modes, writes more than the retained tail
|
||||
// there, draws a last frame and waits for a line; then it restores the
|
||||
// modes, leaves the alternate screen and prints a marker. The markers are
|
||||
// computed, so the command's own echo does not contain them.
|
||||
const tuiCommand = `echo before-$((1))tui; ` +
|
||||
`printf '\033[?1049h\033[?1000h\033[?1006h\033[?2004h\033[?1h\033=\033[H\033[2J'; ` +
|
||||
`head -c 1300000 /dev/zero | tr '\0' x; ` +
|
||||
`printf '\033[H\033[2JFRAME-%s\033[24;1Hstatus-%s' $((7)) $((8)); read x; ` +
|
||||
`printf '\033[?2004l\033[?1006l\033[?1000l\033[?1l\033>\033[?1049l'; echo after-$((2))tui` + "\r"
|
||||
|
||||
// A client that attaches (a reload, or one that fell behind the retained
|
||||
// tail) while a full-screen program runs gets the terminal's state, not the
|
||||
// tail: the program's screen on the alternate screen with its modes, and
|
||||
// the shell's screen underneath, which the program's exit brings back.
|
||||
func TestTerminalSnapshotAfterReload(t *testing.T) {
|
||||
terminalHome(t)
|
||||
ctx, conns := dialTerminalConns(t, 2, 60*time.Second)
|
||||
term := openLoggedIn(t, ctx, conns[0], true)
|
||||
reader := &terminalReader{t: t, ctx: ctx, conn: conns[0], id: term.ID, end: term.OutputEnd, ack: true}
|
||||
typeInto(t, ctx, conns[0], term.ID, tuiCommand)
|
||||
reader.until("status-8", nil)
|
||||
|
||||
// The reload: no After at all, and one far before the tail.
|
||||
var reply *protocol.TerminalRecord
|
||||
for _, after := range []int64{0, 5} {
|
||||
reply = terminalReply(t, ctx, conns[1], terminalRequest(protocol.TerminalOutput, "reload", protocol.TerminalRequest{TerminalID: term.ID, After: after, Flow: true})).Terminal
|
||||
if !reply.OutputSnapshot || reply.OutputStart != reply.OutputEnd || reply.OutputEnd != reader.end || reply.Window != protocol.TerminalFlowWindow {
|
||||
t.Fatalf("after %d: reply start %d end %d (reader at %d) snapshot %v window %d", after, reply.OutputStart, reply.OutputEnd, reader.end, reply.OutputSnapshot, reply.Window)
|
||||
}
|
||||
}
|
||||
for _, want := range []string{"\x1b[?1049h", "\x1b[?1000h", "\x1b[?1006h", "\x1b[?2004h", "\x1b[?1h", "\x1b=", "FRAME-7", "status-8", "before-1tui"} {
|
||||
if !strings.Contains(reply.Output, want) {
|
||||
t.Fatalf("snapshot lacks %q", want)
|
||||
}
|
||||
}
|
||||
if len(reply.Output) > 64<<10 {
|
||||
t.Fatalf("snapshot is %d bytes", len(reply.Output))
|
||||
}
|
||||
// A resume inside the tail is raw output, as before.
|
||||
if resume := terminalReply(t, ctx, conns[1], terminalRequest(protocol.TerminalOutput, "resume", protocol.TerminalRequest{TerminalID: term.ID, After: reply.OutputEnd, Flow: true})).Terminal; resume.OutputSnapshot || resume.Output != "" {
|
||||
t.Fatalf("resume = %+v", resume)
|
||||
}
|
||||
|
||||
screen := termstate.New(term.Cols, term.Rows, termstate.DefaultScrollback)
|
||||
screen.Write([]byte(reply.Output))
|
||||
if !screen.AltScreen() || !strings.HasPrefix(screen.Text(), "FRAME-7") || !strings.Contains(screen.Text(), "status-8") {
|
||||
t.Fatalf("snapshot screen (alt %v):\n%s", screen.AltScreen(), screen.Text())
|
||||
}
|
||||
// The program exits; the frames continue exactly after the snapshot.
|
||||
typeInto(t, ctx, conns[0], term.ID, "\r")
|
||||
late := &terminalReader{t: t, ctx: ctx, conn: conns[1], id: term.ID, end: reply.OutputEnd, ack: true}
|
||||
late.until("after-2tui", nil)
|
||||
if late.gaps != 0 {
|
||||
t.Fatalf("frames after the snapshot skipped %d bytes", late.gaps)
|
||||
}
|
||||
screen.Write([]byte(late.text.String()))
|
||||
text := screen.Text()
|
||||
if screen.AltScreen() || !strings.Contains(text, "before-1tui") || !strings.Contains(text, "after-2tui") || strings.Contains(text, "FRAME-7") || strings.Contains(text, "xxxx") {
|
||||
t.Fatalf("main screen after the exit (alt %v):\n%s", screen.AltScreen(), text)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user