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>
This commit is contained in:
2026-11-25 23:57:18 +00:00
parent 5ab1ec9e00
commit 0963a071d0
30 changed files with 1958 additions and 182 deletions
+30 -2
View File
@@ -33,9 +33,25 @@ const (
// 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"
)
@@ -54,6 +70,9 @@ const (
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.
@@ -68,6 +87,9 @@ type TerminalRequest struct {
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
@@ -89,12 +111,18 @@ type TerminalRecord struct {
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. A client holding
// another stream reads the terminal again from its start.
// 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