Skip to content

Terminals and providers

An AgentHandler runs inside an AI6 terminal Run so the watchdog can observe its output and an authorized operator can attach without restarting the Run.

Live channels in the browser

Outer Office pairs a Noise key from a browser that supports X25519 to the person's sign-in. The browser proves possession of the key to Universal Exports and pins the gateway's key from the pairing response. Each live terminal opens a diptel/1 stream on the same browser link. Universal Exports checks the person's access to the Run and requires a write lease for terminal input. Signing out revokes the paired key for that sign-in.

DipTel frames retain their message boundaries. A reconnect asks for the frames after the last sequence shown. The Station republishes recent frames at their original sequences after a gateway restart. The Station's [publisher] replay limits bound that history across its live Runs. The terminal marks a gap if some output was dropped. Browsers without X25519 and native clients use the existing WebSocket routes.

The Station chooses the terminal

Which terminal provider a Run gets is decided by the Station, from what the Agent's handler declares about its harness (AgentHandler::stdio), never from a provider the Agent or handler names:

  • A handler whose harness speaks JSON over standard input and output (json: the Claude Code and Codex handlers) gets a provider without a terminal (Terminal::tty is false), preferring pipe (ai6::DEFAULT_PIPE). With none available, the Mission is refused at start with the reason.
  • Any other handler (terminal, the default) gets pty (ai6::DEFAULT_TERMINAL) when it is available, else another provider with a terminal, else any available provider.

The station record's terminal, or StationConfig::with_terminal in Rust, pins one. A pin is used when it suits the harness; a pin that gives a JSON harness a terminal is refused at Mission start with the reason, never silently replaced. A field runner chooses the same way among the providers it loads, under its own terminal setting or the Station's. A resumed Run reuses the terminal it started with.

The Station's log names the handler and terminal of every harness it starts or resumes, for Missions and Moneypenny alike (ai6: Mission "mission-007": started the claude harness in the pipe terminal); a Mission's Run record keeps both as well. A program's exit code is reported as a signed 32-bit number on every platform, so a Windows status such as 0xC000013A reads -1073741510, which the Claude Code and Codex handlers show with its hex (exited with code -1073741510 (0xC000013A)).

Shipped providers

Provider Platforms Terminal Behavior
pty Linux, macOS, Windows yes openpty on Unix; ConPTY and a Job Object on Windows
pipe Linux, macOS, Windows no Plain pipes; a process group on Unix, a Job Object on Windows

ai6-terminal-pty and ai6-terminal-pipe are one crate each: the run logic is shared, and the platform parts (owning the child's process tree, and for pty translating input) live in src/unix.rs and src/windows.rs. Build them with the daemon:

cargo build -p ai6 --features cli -p ai6-terminal-pty -p ai6-terminal-pipe

Pass the build output directory with --plugin-path when starting ai6 serve. AI6 discovers Guatiao providers and reports unavailable ones in its diagnostics.

Running harnesses as a non-root user

A harness that runs as root owns the host if it is compromised, so a Station running as root refuses to start one. On Unix, give the pipe terminal a run_as user in [providers.pipe]: the program runs as that user (never uid 0), with only its own group, and its working directory and the files the Station writes for the Run must be readable by it. The pty terminal cannot drop privileges (its pseudo-terminal library has no hook between fork and exec), so a Station running as root either starts JSON harnesses on pipe with run_as or sets allow_root = true on the provider, which is the operator's weakening and is named in the startup log. A Station that already runs as a non-root user needs neither; Windows has no uid and neither applies. A remote Theater's login user is that Theater's concern.

The pipe provider

pipe starts the program with a pipe for its standard input and one for its standard output, so it sees no terminal: a harness that checks (claude -p refuses to read its input from a terminal) reads what is written to it, nothing is echoed, an input line of any length arrives whole, and the output is the program's bytes exactly, with no CR inserted and no control sequence. Standard error goes through a pipe of its own, and only its last 8 KiB, from the first whole line in them, follow the output once it has ended, so a JSON stream is never broken up by it and the handler still reads why a harness failed. The provider setting stderr = "merge" puts it into the same pipe as standard output instead, keeping the order the program wrote them in, and stderr = "drop" discards it. The output ends once the program has exited and its pipe has closed, or, when a process it left running holds the pipe, once it has exited and nothing has arrived for 200 ms. resize does nothing. With no terminal, a written ^C is one more byte of input, not a signal, so a JSON harness is interrupted over its own protocol: Claude Code with a stream-json control_request of subtype interrupt, Codex with turn/interrupt.

The program starts alone: it inherits only its standard streams. On Unix it leads a run and process group of its own, and every other descriptor is closed when it executes; on Windows only its three standard handles are inherited, it is in its Job Object from its first instruction, and it starts with no console window. Kill takes the whole tree: on Unix the group, and every process whose parent is in the tree, are stopped and then killed with SIGKILL, so a process that left the group (setsid) goes too, unless its parent exited first and it was reparented away; on Windows the Job Object is terminated. On Windows a bare program name is found on the child's PATH with each PATHEXT extension, so codex finds npm's codex.cmd, and is refused when found nowhere there.

Terminal mode on Unix

A Unix terminal starts in canonical mode with echo: it holds input until a line ends, cuts a line at its limit (4095 bytes on Linux; macOS never delivers a line over 1024 bytes), and copies what is written back into the output. A handler writes each message to its harness as one line, of any length, and never needs a line editor. So before the program starts, the pty provider turns off canonical mode, echo, IEXTEN and XON/XOFF flow control. It keeps signals (a written ^C interrupts the program), CR-to-LF on input (Enter typed in an attached viewer) and output processing (the program's LF arrives as CRLF, which JsonLines and an attached viewer both read). A program that wants another mode sets it itself.

Adding a terminal provider

A terminal provider implements Guatiao's ai6_terminal kind, the ai6::directorates::q::terminal contract: spawn, read and write, resize, kill and exit status, and tty, which answers false when the program gets no terminal (it defaults to true). Put it under crates/providers/terminals/<name>/; the library holds its own Guatiao entry point, so no forwarding-only plugin crate is needed. Every provider must terminate the whole agent process tree: a process group on POSIX, a Job Object on Windows.

Cursor handshake

A program under a pty can ask for the cursor position (ESC[6n) and wait for an emulator, and ConPTY on Windows always asks once before its program's first output and holds that output until it is answered (portable-pty creates every pseudo-console with PSEUDOCONSOLE_INHERIT_CURSOR). On Windows the pty provider answers every such query itself (row 1, column 1, from a thread of its own, so output keeps flowing while a write waits) and removes it from the output, so a harness starts with nobody attached and an attached viewer does not answer a second time. On Unix queries pass through to whoever reads.

A transport that reaches another host and can open a terminal there (ssh) does so through the same contract. A harness on another host runs under the runner there, in the terminal provider the runner chooses, or, on a POSIX host without one, in the detached wrapper the Station starts over the transport (no terminal, as with pipe; a resize does nothing). See Theaters.

Reading a harness through ConPTY

ConPTY renders the console it hosts rather than passing bytes through. Its output starts with mode and title sequences (ESC[?9001h ESC[?1004h ESC[m, ESC]0;title BEL, ESC[?25h); it may move the cursor where the program wrote blank lines; input a program reads a line at a time comes back as an echo; and a line longer than the terminal is wide is broken with CRLF and a cursor move. The Claude Code and Codex handlers run in pipe, whose stream is the harness's own. They read it with JsonLines, which changes nothing on a clean stream; on a rendered one it removes the controls and rejoins a broken JSON line, marking it rebuilt, and the Codex handler declines a green ink approval read from such a line. They also ask for the widest terminal ConPTY takes (32767 columns, STREAM_COLS), which pipe ignores. An OSC sequence with no end stops at the line break, and a CSI at the first byte no CSI holds or after 64 bytes. (ai6::directorates::q::terminal).

A viewer reconnects from what it has

A terminal attaches with the sequence of the last event it received, starting from 0, and Universal Exports replays what it still holds past that. It holds the last scrollback_events frames of a Run, in memory. Older ones are gone, and a gateway that restarted holds none. When what it holds starts later than the position, or skips a sequence, the replay says so with a gap frame, and it does so live when a Run's next event skips one: a Station dropped output while the gateway was away. Outer Office shows [AI6: output is missing here] at that point in the terminal.