# Permanent terminal (bottom panel / floating window) A full terminal available from every destination, independent of chats. Each terminal starts at the machine's login prompt (`su -l ` for the server's own account), so the operator types that account's password before getting a shell. It keeps running, logged in, when the panel is hidden or the app navigates or reloads; it ends when the Pipelines server restarts (run tmux inside it for work that must survive that). ## Behaviour - **Modes** (Settings → Appearance → Terminal, or the terminal's own ⋮ menu): *Bottom panel* docks under the app (the app shrinks above it, never hidden under it); *Floating window* is a window over the app. Both are toggled by the bar's Terminal button (every destination's bar ends with it) and the shortcuts below. - **First open:** the first time the terminal is shown in an app run (once every machine's list has arrived) it starts one terminal, as New terminal does, unless one is already running; it shows the login prompt and the operator types the password into the terminal (the PTY hides it). From then on the button, shortcuts and the narrow page only hide and show it: no toggle opens another terminal, even after every tab is closed (New terminal does). Opening one by hand first counts as the first (`TerminalHub.firstOpenDone`, `openTerminalIfNone`). Each new tab is its own login and asks for the password again. - **Shortcuts** (`terminal_shortcuts.dart`, handled by the surface as a `FocusManager` early key handler, so they work wherever focus is, including in the chat composer, which does not receive the key): - **Alt+T** (Option+T), **Ctrl+T**, **Cmd+T** (Meta+T) and **Ctrl+`** show or hide the terminal in the selected mode; on a narrow window they open and close the full-screen page. Only the one modifier counts (Ctrl+Shift+T, Ctrl+Alt+T pass through); Option+T, which types † on a Mac, is also matched by key position. Cmd+` stays the macOS window switcher. - **Inside a focused terminal** (any `TypingSurface`, i.e. a terminal view) only **Cmd+T** and **Ctrl+`** toggle: **Ctrl+T** reaches the shell as `^T` (transpose-chars) and **Alt+T** as `ESC t` (transpose-words). A held chord that opened the terminal from outside keeps its repeats swallowed after the terminal takes the keyboard, so nothing is typed. The ⋮ menu and the shortcut cheatsheet say so (`TerminalShortcutLabels.inTerminalNote`). - **Ctrl+Tab / Ctrl+Shift+Tab**, while the terminal is shown (panel, window or narrow page), select the next / previous terminal tab, wrapping, and focus it. This deliberately overrides the chat's Ctrl+Tab pane cycling while the terminal is open (`TerminalActions.claimsTabKeys`, checked by `chat_screen_workspace.dart`); hidden, Ctrl+Tab cycles chat panes as before. - **Browsers** keep some of these: a normal Chrome/Safari/Firefox tab never receives Cmd+T (new tab) or Ctrl+Tab / Ctrl+Shift+Tab (switch tabs), and outside macOS Ctrl+T is new tab too. The web build therefore lists only Ctrl+T (macOS), Alt/Option+T and Ctrl+` in the button tooltip and the ⋮ menu (`TerminalShortcutLabels`), and no tab keys; the handlers stay on, so an installed app window that does deliver a key still works. Native macOS/Linux/Android-with-keyboard builds get all of them and list them. - **Focus:** whenever the terminal shows (shortcut, button, a reload that restores it, a mode change, un-minimising, a tab change by pointer, key or New terminal) the selected tab's emulator gets the keyboard: the body is a `FocusScope` the surface focuses after the frame, and the emulator autofocuses inside it (`terminal_focus.dart`). Hiding gives the keyboard back at once to what had it before opening (the composer, a field), if the terminal still has it; the narrow page's route restores focus itself. - **Motion:** `Motion.standard` (200 ms, `Motion.curve` in, ease-in-cubic out), played on the app clock the animation-speed setting dilates, and zero under reduced motion (`Motion.of`). The bottom panel keeps its full height and slides up from the bottom edge with a slight fade (opacity 0.4 to 1) while the app above shrinks with it frame by frame (no jump, and the emulator is not resized during the motion); the floating window scales from 92 % and fades in its own place. During the hide motion the terminal stays mounted but takes no pointer or focus (`terminal_stage.dart`). - **Bottom panel:** resized by dragging its top edge (the app's `AdaptiveResizeGrip`; arrow keys too, slider semantics); its height is remembered. The panel always leaves the app at least 160 px. - **Floating window:** dragged by its title bar, resized from its lower right corner, double-click or Maximise fills the app (8 px margin), Minimise collapses it to its title bar, × hides it. Frame and state are remembered. - **Narrow windows (< 600 px):** the terminal opens as a full-screen page (Material/Cupertino route) with the chat terminal's key toolbar (Paste, Enter, Ctrl-C, Tab, Esc, arrows, and the typing field on Android/iOS). - **Tabs:** one per terminal; the title is the shell's OSC 0/2 title (followed by the emulator, also recorded by the server for the list), else the start folder (`~` for home). Ended terminals are italic and show why they ended with a Close tab button. With more than one machine connected (or tabs from several) each tab names its machine, and New terminal (+) asks which machine. ⨯ on a tab closes it, which kills the login or shell. - **Open in terminal:** the chat header's More menu (the chat's folder, on the chat's machine) and a folder's ⋮ menu in the Files panel open a new terminal there and show the surface. - **Permanence:** the app's `TerminalHub` lives as long as the app; hiding or minimising never closes a shell. Reopening redraws the same emulator. The layout (mode, shown, height, frame) is saved per device, so a reload reopens the terminal, lists the machine's terminals and resumes each from its retained output. A dropped connection resumes after the last byte held. A restarted server lists the terminals as ended, with why. - **Keyboard:** a focused terminal gets every key (Ctrl-C, Esc, Tab, arrows, Ctrl-letters, Ctrl+T, Alt+T) except Cmd+T, Ctrl+` and Ctrl+Tab while shown; paste is Cmd-V (Apple) or Ctrl-V/Ctrl-Shift-V. Clicking elsewhere moves focus away normally. Keys are encoded as xterm does; see `chat-interactive-terminal.md` (Keys). **Option as Meta** (Settings → Appearance → Terminal, Macs only, saved with the layout, default on): a Mac's Option sends `ESC` + the key's character (Option+B = `ESC b`) instead of typing its symbol (∫). On by default because the shell's Meta bindings (word motion, transpose, yank-last-arg) are used far more in a terminal than Option's symbols; turn it off to type them. Other platforms' Alt is always Meta. - No terminal request is sent until the terminal is first shown (or a terminal is opened): each connection sends one request at a time, so the first screen's read is never queued behind `terminal_list`. ## Protocol (server) `internal/protocol/terminal.go`; documented in `docs/cli.md` ("Machine terminals"). Body `terminal_request {terminal_id?, cwd?, cols?, rows?, data?, after?}`. - `terminal_open {cwd, cols, rows}` → `terminal` once su's password prompt shows (subscribes the connection to its output first); refused when the login cannot ask for a password (below). - `terminal_list` → `terminals` (running and ended, oldest first; live records carry their current title, size and `stream`). - `terminal_input {terminal_id, data, reply?, cols?, rows?}` (UTF-8, ≤ 16 KiB, 64-request queue), `terminal_resize {terminal_id, cols, rows, activate?}` (clamped 20–500 × 4–200). - **Several clients on one terminal** (`terminal_viewers.go`, `terminal_active.go`; chat terminal commands too): one *active* client owns the PTY size and is the only one whose emulator's automatic answers (`reply: true`: cursor position, device attributes) reach the shell, so a query gets one answer, not one per client. The opener starts active; typing (carrying the typist's `cols`/`rows`, applied before the input) or focusing a terminal view (`terminal_resize` with `activate`) makes a client active. Others' sizes are kept, not applied; when the active connection closes the next most recent client's size is applied. The client marks answers by what the emulator emits while drawing output (`ChatShellTerminalSession.onReply`). - **Input latency**: `PipelinesClient.call` sends `terminal_input`, `terminal_resize`, `chat_shell_input` and `chat_shell_resize` at once instead of behind the previous request's reply; the server reads them onto a per-connection terminal lane (`terminal_lane.go`) handled in order beside the ordinary loop, and `terminal_open` runs off the read loop. `ChatShellInputPipe` still keeps one request in flight per terminal (typing during it is coalesced; answers go in their own requests). - `terminal_output {terminal_id, after}` → `terminal` with `output`, `output_start`, `output_end`, `stream`; subscribes the connection. - `terminal_close {terminal_id}` → the closed record (runs off the read loop: it waits up to 3 s for the shell). - Pushed `terminal_event`: output `{terminal_id, output, offset, end}` to subscribed connections, lifecycle `{terminal_id, terminal}` (opened, title, exited, closed) to every connection. Output flow is described under "Output flow" below; offsets are stream bytes, taken from the server (`end`, `output_end`), never from the text's re-encoded length. ## Output flow (2026-11-25) Server (`terminal_flow.go`, `terminal_clients.go`): each connection's subscription is a cursor into the terminal's 1 MiB retained tail; the connection's writer goroutine sends from it as soon as the PTY reader has written (no tick), coalescing what arrived during the previous write and, while output streams, letting a frame gather for at most 2 ms. The app asks for `flow: true` on `terminal_open`/`terminal_output`; those replies carry `window` (256 KiB) and the app sends `terminal_ack {terminal_id, after}` each time it has drawn a quarter window. A flow subscription is sent at most `window` bytes past its last ack, and while one is full the PTY reader stops (the program blocks on its writes) instead of skipping. One that stays full for 3 s without acknowledging stops holding the terminal; on its next ack it gets a frame after a gap and re-reads. A gone connection drops its subscriptions at once. A subscription without `flow` (an older app build) is never waited for and keeps the old pacing (newest 64 KiB, one frame per 50 ms). `terminal_open` and `terminal_output` replies are written before the frames that follow them, which start exactly at the reply's `output_end`. Client: `ChatShellTerminalFeed` (shared with chat shells) writes output into the emulator in 16 Ki-unit slices and yields to the event loop after 6 ms, so a 1 MiB replay or a burst never holds a UI frame; `drawn` is the server byte end of what has been written, which `TerminalTab` acknowledges. Chat shell runs (`chat_shell_supervise.go`) share the output buffer code: output is pushed as it arrives, oldest first, while every watcher of the chat has fewer than 8 queued frames; 256 KiB unpushed stops the reader. A watcher behind for 3 s gets the old pacing (newest chunk per flush tick, a gap) until it catches up. The DB snapshot every 2 s is unchanged. Measured (`PIPELINES_TERMINAL_MEASURE=1 go test ./internal/server -run TestMeasureTerminalStream -v`, loopback, fast reading client; before = the old server or a client without `flow`): | workload | before | after | | --- | --- | --- | | `seq 1 500000` (3.9 MB) | 3 frames, 3.69 MB skipped, 1.3 MB/s | 70 frames, 0 skipped, 27 MB/s | | `cat` 2.1 MB | 1 frame, 2.04 MB skipped | 34 frames, 0 skipped, 117 MB/s | | `yes \| head -1000000` (3.0 MB) | 5 frames, 2.67 MB skipped | 126 frames, 0 skipped, 12 MB/s | | keystroke echo (median, 40 keys) | 43.3 ms | 0.17 ms | | longest gap between frames while streaming | 50 ms | 2.5-4.2 ms | Emulator (`chat_shell_terminal_feed_test.dart`, 896 KiB replay): one synchronous write 160 ms; sliced, the same 161 ms over 20 turns, the longest turn 12.5 ms. ## Login and lifetime (server) - `internal/server/terminal_login.go`. A terminal runs, in its PTY, `su -l -c '