Files
pipelines/internal/protocol/chat_shell_terminal.go
T
alan c3458d8a7f Terminals: one active client per terminal; keystrokes skip the request queue
Several clients drawing one terminal (machine terminal or chat terminal
command) no longer interfere. The server tracks, per terminal, the
connections drawing it, most recently active first: the active client
(the opener, then whoever last typed or focused it) owns the PTY size and
is the only one whose emulator's automatic query answers reach the
process; other clients' answers are acknowledged and dropped, and their
sizes recorded but not applied. When the active connection closes the next
most recent client's size is applied.

Protocol (backwards compatible; all new fields optional):
- terminal_input / chat_shell_input: reply (emulator answer), cols/rows
  (the typist's size, applied before the input).
- terminal_resize / chat_shell_resize: activate (the client focused it).

Latency: PipelinesClient sends terminal_input, terminal_resize,
chat_shell_input and chat_shell_resize at once rather than behind the
previous request's reply. The server reads those onto a per-connection
terminal lane handled in order beside the ordinary request loop, and
terminal_open (up to 5 s waiting for su) now runs off the read loop.

Client: ChatShellTerminalSession routes what the emulator emits while
drawing output to onReply and reports focus through activate();
ChatShellInputPipe keeps answers and typing in separate requests.

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

58 lines
2.9 KiB
Go

package protocol
// Interactive operator shell commands. On macOS and Linux every chat_shell_run
// runs inside a pseudo-terminal (ChatShellRunRecord.Terminal): the command sees
// a TTY of the requested size with TERM=xterm-256color, its output is the raw
// terminal stream (escape sequences included) and any authenticated client
// watching the chat may type into it until it ends. Other platforms keep
// plain pipes and refuse input.
const (
// ChatShellInput writes Data to a running terminal command's input. The
// body is ChatShellRunRequest (session_id, run_id, data). Data is the
// UTF-8 text of what was typed or pasted, control characters included
// (Ctrl-C is "\u0003", Enter is "\r", arrow keys are their escape
// sequences); the bytes written are exactly its UTF-8 encoding. Input is
// never persisted or logged; the reply carries no body.
//
// Several clients may draw one terminal; the one that most recently typed
// or focused it (marked activate on a resize), or else the first to
// attach, is its active client. Reply marks Data
// as the client emulator's automatic answer to a terminal query (cursor
// position, device attributes) rather than typing: it reaches the process
// only from the active client and is otherwise acknowledged and dropped,
// so a query gets one answer. Typed input makes its client active; it may
// carry Cols and Rows, the size that client draws at, which the terminal
// then takes before the input is written.
ChatShellInput = "chat_shell_input"
// ChatShellResize reports the size (cols, rows) a client draws a running
// terminal command at. The terminal takes the active client's size (the
// process receives SIGWINCH); a resize marked activate makes its client
// the active one (it focused the terminal). When the active client's
// connection closes, the next most recently active client's last size is
// applied. A client that does not set the fields behaves as before: its
// typing makes it active. The reply echoes the run with the size applied.
ChatShellResize = "chat_shell_resize"
)
// Bounds of interactive terminals.
const (
// ChatShellInputLimit bounds the UTF-8 bytes of one chat_shell_input.
// Clients split a longer paste into several requests.
ChatShellInputLimit = 16 << 10
// ChatShellInputQueue bounds the input requests waiting for the command
// to read them; beyond it chat_shell_input is refused.
ChatShellInputQueue = 64
// Terminal sizes are clamped to these bounds; a run started without a
// size gets the defaults.
ChatShellTerminalMinCols = 20
ChatShellTerminalMaxCols = 500
ChatShellTerminalMinRows = 4
ChatShellTerminalMaxRows = 200
ChatShellTerminalDefaultCols = 100
ChatShellTerminalDefaultRows = 30
)
// ChatShellTerminalUnsupported is the refusal of chat_shell_input and
// chat_shell_resize on a server or run without a pseudo-terminal.
const ChatShellTerminalUnsupported = "interactive terminals are not supported on this server"