Files
pipelines/internal/protocol/terminal.go
T
alan 0963a071d0 Terminals: flow-controlled output, immediate echo, sliced emulator writes
Machine terminals no longer skip output for live viewers. Each connection's
subscription is a cursor into the 1 MiB retained tail, drained by the
connection's writer as soon as the PTY reader writes (no 50 ms tick; a
streaming frame gathers for at most 2 ms). A subscription opened with
flow: true gets window (256 KiB) in its reply and acknowledges drawn output
with terminal_ack; while one is full the reader stops reading the PTY, so
the program waits. A subscription full for 3 s without acking stops holding
the terminal and later sees a gap; a gone connection drops its
subscriptions. Clients that never ask for flow keep the old pacing (newest
64 KiB per 50 ms). terminal_open/terminal_output replies precede the frames
that follow them, which start exactly at the reply's output_end.

Chat shell runs share the buffer code: output is pushed as it arrives,
oldest first, while the chat's watchers keep short queues; 256 KiB unpushed
stops the reader, and watchers behind for 3 s get the old pacing.

The app asks for flow, takes offsets from the server (frame end, reply
output_end) instead of re-encoded text length, acks per quarter window, and
writes replays and bursts into the emulator in slices that yield to the
event loop (ChatShellTerminalFeed, shared with chat shells).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 23:57:18 +00:00

137 lines
6.3 KiB
Go

package protocol
// Machine terminals: interactive login shells the operator opens on a
// server's machine, independent of any chat. Each starts at the machine's
// login prompt for the server's own account (`su -l <user>` in a
// pseudo-terminal): the operator types that account's password before the
// login shell starts. A terminal ends with the server process and is then
// listed as ended (run tmux inside it to keep work across restarts). Typed
// input is never logged or stored.
const (
// TerminalOpen starts a terminal at the login prompt. The body is
// TerminalRequest (cwd, cols, rows); cwd defaults to the operator's home
// and must be an existing directory inside it (the shell starts there
// after the login). The reply, sent once the password prompt shows,
// carries the new terminal; the connection is subscribed to its
// terminal_event output frames. A login that cannot ask for a password
// (no su, a root server, an account without one) is refused.
TerminalOpen = "terminal_open"
// TerminalList lists this machine's terminals (running and ended),
// oldest first, without output. The body is optional.
TerminalList = "terminal_list"
// TerminalInput writes Data (UTF-8, control characters included, at most
// ChatShellInputLimit bytes) to a running terminal. No reply body.
TerminalInput = "terminal_input"
// TerminalResize sets a running terminal's size (cols, rows); the reply
// echoes the terminal.
TerminalResize = "terminal_resize"
// TerminalOutput reads one terminal with its retained output from the
// absolute byte After on (all of it when After precedes the retained
// tail; OutputStart says where it starts). It also subscribes the
// connection to the terminal's terminal_event output frames.
TerminalOutput = "terminal_output"
// TerminalClose ends a terminal: its login or shell is killed
// and its record removed. The reply echoes the last record.
TerminalClose = "terminal_close"
// TerminalAck tells the server that this connection has consumed
// (drawn) a terminal's output up to the absolute byte After. Only a
// connection whose terminal_open or terminal_output carried Flow (and
// whose reply carried Window) sends it; the reply has no body.
TerminalAck = "terminal_ack"
// TerminalEvent is pushed: an output chunk to the connections subscribed
// to that terminal (Output, Offset, End), or a lifecycle change (Terminal
// set: opened, title, exited, closed) to every connection.
//
// Output flow: a subscription opened with Flow gets every byte in order,
// at most Window bytes past its last terminal_ack; while such a
// subscription is full the server stops reading the terminal, so the
// program waits (a subscription that stays full without acknowledging for
// a few seconds stops holding it back, and later sees a gap). A
// subscription without Flow is never waited for: it gets at most one
// TerminalChunkLimit frame per 50 ms, the newest bytes, and a gap when it
// falls behind. A gap (Offset after the client's end) is repaired by
// terminal_output, whose reply's OutputEnd is where the frames that
// follow it start.
TerminalEvent = "terminal_event"
)
// Terminal status values. Running is the only live status.
const (
TerminalRunning = "running"
TerminalExited = "exited"
TerminalClosed = "closed"
)
// Terminal bounds.
const (
// TerminalOpenLimit bounds the running terminals of one machine.
TerminalOpenLimit = 8
// TerminalRetained is the output tail kept for reconnecting clients.
TerminalRetained = 1 << 20
// TerminalChunkLimit bounds one live output frame.
TerminalChunkLimit = 64 << 10
// TerminalFlowWindow bounds the output sent to a flow-controlled
// subscription past its last terminal_ack.
TerminalFlowWindow = 256 << 10
// TerminalEndedKept bounds the ended terminals still listed.
TerminalEndedKept = 16
// TerminalTitleLimit bounds a recorded title.
TerminalTitleLimit = 200
)
// TerminalRequest is the body of every terminal request.
type TerminalRequest struct {
TerminalID string `json:"terminal_id,omitempty"`
Cwd string `json:"cwd,omitempty"`
Cols int `json:"cols,omitempty"`
Rows int `json:"rows,omitempty"`
Data string `json:"data,omitempty"`
After int64 `json:"after,omitempty"`
// Flow (terminal_open, terminal_output) asks for flow-controlled output:
// the connection acknowledges what it consumed with terminal_ack.
Flow bool `json:"flow,omitempty"`
}
// TerminalRecord is one terminal. Output and OutputStart are set only on
// terminal_output replies.
type TerminalRecord struct {
ID string `json:"id"`
// Title is the last terminal title (OSC 0/2) the shell set, or empty;
// clients show the cwd then.
Title string `json:"title,omitempty"`
Cwd string `json:"cwd"`
Shell string `json:"shell"`
CreatedAt int64 `json:"created_at"`
Status string `json:"status"`
EndedAt int64 `json:"ended_at,omitempty"`
ExitCode *int `json:"exit_code,omitempty"`
// Reason says why an ended terminal ended when it was not simply its
// shell exiting (the login failed, the server stopped or restarted).
Reason string `json:"reason,omitempty"`
Cols int `json:"cols"`
Rows int `json:"rows"`
// Stream names this server process's output stream of the terminal;
// OutputEnd is the absolute byte count read into it (on a
// terminal_output reply, the end of Output: an incomplete trailing
// character follows in the next frame). A client holding another stream
// reads the terminal again from its start.
Stream string `json:"stream,omitempty"`
OutputEnd int64 `json:"output_end"`
Output string `json:"output,omitempty"`
OutputStart int64 `json:"output_start,omitempty"`
// Window is set on the terminal_open and terminal_output replies of a
// flow-controlled subscription: the bytes the server sends past the
// last terminal_ack. Its absence means the server takes no acks.
Window int64 `json:"window,omitempty"`
}
// TerminalEventFrame is one pushed terminal change: either Terminal (a
// lifecycle change) or one output chunk covering bytes [Offset, End).
type TerminalEventFrame struct {
TerminalID string `json:"terminal_id"`
Terminal *TerminalRecord `json:"terminal,omitempty"`
Output string `json:"output,omitempty"`
Offset int64 `json:"offset,omitempty"`
End int64 `json:"end,omitempty"`
}