aaed57e105
Conflicts resolved keeping both sides: main's active-client rules, input lane, reply marking and key input, and this branch's flow control, acknowledgements, subscriptions and sliced emulator feed. terminal_ack joins the terminal lane; the feed marks each live slice it writes as drawing, so the emulator's answers still go to onReply. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
145 lines
6.8 KiB
Go
145 lines
6.8 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. Reply
|
|
// marks an automatic answer of the client's emulator, which reaches the
|
|
// terminal only from its active client; typed input may carry the size
|
|
// (cols, rows) the client draws at (as chat_shell_input).
|
|
TerminalInput = "terminal_input"
|
|
// TerminalResize reports the size (cols, rows) a client draws a running
|
|
// terminal at; the terminal takes its active client's size, and Activate
|
|
// makes the sender active (as chat_shell_resize). The opener of a
|
|
// terminal is its first active client. 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"`
|
|
// Reply (terminal_input) and Activate (terminal_resize): see above.
|
|
Reply bool `json:"reply,omitempty"`
|
|
Activate bool `json:"activate,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"`
|
|
}
|