Terminal sessions
A terminal session is a session whose Pi runs its own interactive TUI in a pseudo-terminal that PiFi holds (Session.terminal on the wire), so you can open the real Pi interface from any terminal, the web app or a herdr tab, while phones and browsers keep their structured view: questions as cards, notifications, review. The other kind is an RPC session: Pi runs headless (pi --mode rpc) and has no TUI to open.
A session’s Pi runs in one of three ways: held in PiFi’s holder (a terminal session), in your own pane (a Pi you started by hand, enrolled), or headless (an RPC session); Details names the place under Runs in for the second. Terminal sessions are the default on every host that can run them; a host that cannot falls back to RPC sessions.
The herdr and tmux sessions that show these sessions as tabs are clients.
How it works
- A holder per session. The host agent starts a small detached process, the terminal holder, which runs interactive
piin a pseudo-terminal (node-pty). The holder outlives the host agent, so restarting or upgrading the agent no longer interrupts sessions: the next agent reattaches. While no agent is attached, the holder buffers the session’s events and hands them over on reattach. - An extension as the control channel. PiFi loads an extension into that Pi (
pifi-terminal.js). It answers the same commands the host sends over RPC (prompt, steer, follow-up, abort, state, stats) and reports session events in the same shape, so the rest of PiFi treats both kinds of session alike. - Questions go to the terminal and to your phone. Every extension’s
ctx.uiis one shared object in Pi. The extension wrapsselect,confirm,inputandeditoron it, so a question shows in the terminal and as a card at the same time, and the first answer wins. A card answered remotely closes the terminal dialog: through Pi’s abort signal, or with Esc for the editor. A question answered in the terminal shows “Answered in the terminal” on the card. If that object cannot be patched (a future Pi might freeze it), the extension says so at startup, and cards tell you to answer in the terminal instead. - Terminal bytes go through the coordinator.
pifi tui <session>connects to/ws/terminal/<session>on the coordinator, which relays to the host over the existing host link, which connects to the holder. It works from any machine signed in to the coordinator, and hosts still need no open ports. A headless terminal in the holder repaints the current screen for each new viewer.
Turning it off
Terminal sessions are on by default. To run a host’s sessions over RPC, set "terminalSessions": false in its host.json or PIFI_TERMINAL_SESSIONS=0, and restart the host agent. At start the agent starts a real pseudo-terminal and lets it go, then logs terminal sessions enabled, or the reason they are unavailable (for example node-pty is not available, or node-pty cannot start a terminal), in which case sessions run over RPC.
Requirements:
node-pty, an optional native dependency. It ships prebuilt binaries for macOS; on Linux it compiles on install (python3, make and a C++ compiler). The container image includes it. npm may install it without running its install script, which leaves itsspawn-helpernot executable; the agent and each holder set that right themselves before starting a terminal.- A coordinator that relays terminals (this version or later). An older coordinator simply never opens a terminal.
Using it
pifi tui <session id> # Ctrl+] detaches; the session keeps runningtui resumes a stopped session first. Several viewers can watch the same session; they share one terminal size. By default the smallest attached viewer sets it (a phone viewer shrinks every other viewer); set PIFI_TERMINAL_SIZING=latest in the host agent’s environment for the old behavior, where the last viewer to attach or resize wins. It applies to terminals started afterwards. Quitting Pi in the terminal (/quit, Ctrl+D) stops the session and archives it, unless it is the primary of a worktree branch that has not landed; the next prompt or tui resumes either.
The herdr and tmux client modules turn a herdr or tmux session into a mirror: a tab or window per active branch, showing Pi’s own TUI in each (clients.md). RPC sessions are left out of it.
Policy approval requests show a PiFi URL in Pi; answer through the existing web interaction flow, not a native terminal policy picker. A healthy approval wait can outlast 30 seconds, but loss of coordinator communication fails closed. Older live holders may lack the current bridge and remain unprotected until explicitly reloaded or restarted; see policies.md.
Sessions you start yourself
A Pi you start by hand in your own terminal can show up in PiFi too, so you can follow it and answer it from your phone. Load PiFi’s extension into every Pi on the machine with a one-line global extension:
echo "export { default } from \"$(pifi host pi-extension)\";" > ~/.pi/agent/extensions/pifi.jsAn interactive Pi with a terminal then enrols with the host agent on that machine (the one whose state directory is PIFI_STATE_DIR or the default; set PIFI_ENROLL_STATE_DIR to pick another, such as a dev agent’s). Within a few seconds it is a live session in the workspace containing its working directory. If that directory is in a worktree PiFi has not seen yet, it rescans the checkout and moves the session to that worktree. Pi that PiFi starts, print and RPC runs, --no-session and PIFI_ENROLL=0 never enrol, and without a host agent nothing happens. In a Pi that PiFi starts, only the copy its holder loads (PIFI_TERMINAL_EXTENSION) runs, so a global shim of another version stays out of the way.
It records the herdr or tmux client and pane it runs in: herdr:<session>@<machine> in herdr, tmux:<session>@<machine> in tmux (herdr wins when herdr runs inside tmux). The session keeps that place after you quit Pi, until PiFi starts it. In the web app, session details can show the reported client; the independent run-location filter can select a currently reported terminal regardless of grouping. Details says where it runs (Runs in), or after it stops where it last ran by hand; that remembered place is not a current terminal filter option. Missing client information does not establish who launched a session. A mirror in the same client opens no tab for it, and focusing its pane while you are attached counts as reading it.
The terminal stays yours:
- Web and phone can prompt, steer, queue, stop the current turn, change model and effort, and answer
select,confirmandinputquestions (first answer wins). Pi’s editor dialog is answered in the terminal only. - Stop and archive never quit your Pi. Quitting it (or starting a new session in its pane) archives the session, except a worktree branch’s primary; closing its herdr pane counts as closing it. A terminal that goes away, a reboot or a crash keeps it Current. Sending to a stopped session from PiFi resumes it as an ordinary PiFi session.
- There is no TUI relay:
pifi tui, the web terminal and mirror tabs do not show it. Running it in PiFi’s holder instead (held mode) is planned in #25. - No
pifi_*agent tools and no policy hooks: the session shows as unprotected (policies.md).
Limits
- Slash commands from PiFi clients run Pi’s extension commands, prompt templates and skills. PiFi itself handles /model, /thinking, /compact and /name. Pi’s other built-in commands (/tree, /settings, /login, …) work only in the terminal.
- “Clear queue” cannot clear Pi’s own queue, and the queue shown in clients is reconstructed by the extension, so it can lag what Pi shows.
ctx.ui.custom()interfaces show in the terminal; clients show “Waiting in the terminal” unless the extension also asks through PiFi’s remote UI service (pifi.remote-ui.v1onpi.events), as pi-plan-mode, pi-ask-user-question and recent pi-tool-gate releases do. Then the question is a card too and the first complete answer wins. Tool-gate’s follow-up message and rule-edit forms are web-only counterparts of fields in its native dialog.- No retry notices, and no extension error diagnostics in clients: Pi does not expose those events to extensions.
- A message that is streaming while the host agent restarts can appear twice in clients.
- The systemd unit uses
KillMode=process, so holders survive agent restarts on Linux too; reinstall the unit withpifi host install-serviceto pick it up. A host agent in a container loses its terminal sessions when the container is recreated. - Holders and their logs live next to the host’s bridge sockets (
<state dir>/runtime, or/tmp/pifi-<uid>when that path is too long for a socket).