ADR-006: Remote execution runs the agent locally and the workspace over system SSH with a remote helper
Status |
Accepted |
|---|---|
Date |
2026-10-26 |
Deciders |
@bitskrieg (Chris Apsey) |
Tags |
architecture, remote, ssh, packaging, security |
Relates |
ADR-004 (package boundaries), #488 (tracking), #489 (phase 1), #483 |
Context
pi runs on the machine that holds the code, and a developer working on a remote box reaches it over SSH in a terminal. Every keystroke and every mouse movement then crosses the link as raw escape sequences, which is where the spurious aborts of #483 came from. Clipboard images, drag-and-drop, desktop notifications and the model credentials also sit on the far side of the link.
The developer asked for the VS Code Remote-SSH shape (#488): install pi once, locally, and point it at a machine, so that it behaves as if it ran there. That raises three questions: which half runs where, how the two halves talk, and where the code that abstracts "the workspace" lives, given that pi-modes and pi-workflow, which both need it, may not import each other (ADR-004).
Facts the decision rests on:
-
pi’s built-in tools (
read,write,edit,bash,grep,find,ls) each take a swappable operations object (node_modules/@earendil-works/pi-coding-agent/dist/core/tools/*.d.ts).user_bashtakes one from a handler, and codemode calls tools throughctx.executeTool. pi shipsexamples/extensions/ssh.ts, which routes four of them over SSH. -
The GADHS packages touch the workspace directly in about twenty files, through synchronous
node:fsandexecFileSync: the commit guard, review snapshot and review context, containment inauthorize.ts,count,generate_image, monitors, helper worktrees. All of them run inside async hooks. -
pi-subagents builds each helper session in the same process, with a fresh resource loader that re-instantiates the installed extensions. Anything published on
globalThisis visible to helpers. -
Windows' OpenSSH has no ControlMaster (Win32-OpenSSH #1328, still open as of 2026-07).
Decision
The agent runs locally and the workspace runs remotely. The UI, model calls, credentials, sessions, memory, plans,
diagnostics and pi-remote’s phone link stay on the developer’s machine. The tools, user_bash, the guards,
containment and helpers act on the remote host.
The connection is the system ssh client running one long-lived helper on the remote. The helper is a single
Node file uploaded when missing or outdated, and speaks newline-delimited JSON on its stdin and stdout. Every
operation (files, stat and realpath, listing, glob, exec with streamed output and cancellation, a port watch) is
one round trip on that one channel. Port forwards are separate ssh -N -L children. This honours the user’s SSH
config, agent, jump hosts and host keys, and needs no ControlMaster, so a Windows client works. Requirement: the
remote is POSIX with Node 20 or later, checked at connect and refused with the reason.
One interface, Workspace, in a library package @gadhs/pi-workspace.
* It defines files and processes with absolute host paths: async throughout; stat and realpath undefined for a
missing path; an env that can only add to the host’s; argv-only exec and spawn; resolve carrying the
local-anchor mapping; and readCapped with workspace-read’s never-truncated rule.
* It has a local implementation and a slot on globalThis (Symbol.for("gadhs:workspace")).
* workspaceFor(cwd) returns the published workspace when cwd is under it, and the local one otherwise.
* A contract suite (@gadhs/pi-workspace/contract) defines the semantics every implementation must pass.
* pi-modes and pi-workflow depend on it by name, as both depend on @gadhs/pi-bash.
The SSH side is an extension package @gadhs/pi-ssh (phase 2): the helper, the protocol, the SSH Workspace, the
tool replacements, the pi-ssh launcher and its local anchor directory, and port forwarding. The meta package loads
it as 11-ssh.ts, after 10-modes.ts, which stays first.
This amends ADR-004’s package ceiling by its own criteria: a library on the pi-bash pattern for shared logic, and
an extension for a concern whose review posture is "transport, keys and containment". The work is phased (#489 to
#494); the meta plan plans/remote-execution.adoc carries the phase map, the risks with their modes, and the
limitations.
Alternatives Considered
-
The whole agent on the remote, with a thin local UI (Claude Desktop’s SSH sessions,
codex --remote, Cursor, JetBrains Gateway). Rejected: model calls and credentials stay remote, so none of the local wins arrive, and a client that re-renders pi’s whole TUI is a large build with nothing of pi’s to reuse. -
Fix the terminal link instead (mosh, Eternal Terminal). Rejected: neither carries a structured channel, and mosh does no port forwarding. They fix roaming, not escape sequences crossing the link.
-
One
ssh host cmdper operation (pi’s example). Rejected as the transport: every file read is a process spawn and a fresh channel, and there is no state for a port watch. Without ControlMaster on Windows, every call authenticates. -
The
ssh2npm package. Rejected: it re-implements SSH config (noProxyJump,Include,Match, certificates or FIDO keys), leaves host-key handling to us, and is marked inactive (last release 2025-08). -
The
Workspaceinterface inside pi-modes or pi-workflow. Rejected by ADR-004: the two may not import each other, and both need it.
Consequences
Positive
-
The terminal never crosses the link, so the class of input bugs behind #483 cannot reach a remote session.
-
Credentials never leave the developer’s machine.
execsends the remote no local environment except what a caller adds, and the interface cannot express a whole-environment replacement. -
Every workspace-touching file becomes testable against a fake workspace, and one contract suite holds the local and remote implementations to the same semantics.
Negative
-
Each operation costs a round trip; on a slow link the agent is slower than a local pi.
-
About twenty files move from synchronous to asynchronous calls (phases 3 and 4), with regression risk the existing suites bound.
-
Node 20 must be on the remote. A Windows remote is out of scope.
-
The remote project’s own pi extensions, skills and prompts are not loaded; they would run locally against the wrong filesystem.
Amendment (2026-10-26): Node 22, and the helper as a held-equal copy
Node 22, not 20. The helper’s glob is fs.promises.glob, which Node 22 introduced; Node 20 has been
end-of-life since 2026-04. Where this decision says Node 20, read Node 22 or later (#496).
The helper is a copy of the local implementation, held equal by the contract suite. ADR-004’s amendment prefers a
library for shared logic and copies held equal by test only for constants. Here the library cannot reach the
remote: the helper is one uploaded JavaScript file that imports nothing but node: built-ins, so it re-states
@gadhs/pi-workspace’s local semantics. The contract suite (@gadhs/pi-workspace/contract`) runs unchanged against
both, which is the strongest equality test the repository has, and it is what keeps the copy honest.
Amendment (2026-10-26): the anchor is entered by a session switch
There is no pi-ssh launcher. /ssh user@host:/path writes the local anchor and a session file whose cwd is the
anchor, and switches into it with pi’s own ctx.switchSession. pi takes a replacement session’s cwd from that file,
so no wrapper around pi is needed, on any platform (#498).
Amendment (2026-10-26): the commit path as a pure core and an async shell
Phase 3 (#491) moved pi-workflow onto Workspace as the Negative consequence above foresaw, and chose its shape:
the guard’s decisions became pure functions of facts, and one async shell per module gathers the facts through the
workspace. A synchronous facade over a worker thread was rejected: it would block pi’s event loop for each remote
round trip, and it would need a second connection. The workspace gained home(), so a cd or ~ in a command is
read with the remote’s home.
References
-
Meta plan:
docs/modules/ROOT/pages/plans/remote-execution.adoc; phase 1:plans/remote-1-workspace.adoc; phase 2a:plans/remote-2a-ssh-workspace.adoc. -
Research sources (VS Code Remote-SSH, Zed, OpenSSH, Win32-OpenSSH #1328, ssh2, mosh, Eternal Terminal): #488.
Amendment (2026-10-27): one workspace per anchor
The slot holds a set: one published workspace per anchor, a second for the same anchor refused. workspaceFor(cwd)
returns the one whose anchor or root holds cwd, the longest match winning, and the local workspace otherwise.
Workspace.at(root, anchor) is a view on the same host over the same connection; closing a view closes nothing of
its parent. A remote helper’s worktree is such a view, published under an anchor inside its parent’s, so its tools,
commits and asks act in its worktree (#492, plans/remote-4-modes.adoc). The allowlist of this machine’s
environment that reaches a remote command (sessionMarkers) lives in the library too.
Amendment (2026-10-27): a Windows client’s tools resolve under a UNC base
pi’s operations-backed tools (read, write, edit, ls) resolve a tool’s path with the client’s own path rules
before pi-ssh’s operations see it. On a Windows client, those rules gave a rooted, drive-less remote path the drive of
pi’s cwd (#501). There, pi-ssh hands the tools the remote root spelled under a UNC device naming the session,
\\pi-ssh\<share>\<root>, where <share> is a hash of the anchor, so each workspace (a helper’s view included) has its
own. win32 resolution keeps a base’s device, and hostPath turns each resolved path back into the host’s. A path
under the anchor maps under the root. Any other drive or UNC path is refused as missing, with the reason. A tool’s
result text and error messages name host paths, never the device. On a POSIX client all of this is the identity
(docs/modules/ROOT/pages/plans/remote-5-ssh.adoc, 5a).
Amendment (2026-10-27): the helper is a daemon, each connection a relay
The helper outlives its ssh connection (#493, 5c). The bootstrap execs node helper --connect <socket>, which
reaches a daemon on a Unix socket in ~/.pi-ssh (mode 0700, the socket 0600). If nothing answers, it starts one,
detached into its own session, behind an atomic start lock, and relays the connection to it byte for byte. Each pi
session is a client, by an id, that attaches on every connection. Streamed requests (exec, spawn) keep running
when a connection drops. Their numbered events are kept in a ring for replay, and the client resumes each from the
last event it saw; a held stream sends nothing new until resumed, so a replay is never overtaken. A read in flight
is sent again; a write in flight fails closed. The daemon ends a client’s work at its bye, and itself when it holds
nothing with no client attached, or ten minutes after its last client left with commands still running. If no daemon
can be had, the helper serves on stdio as before (daemon: false). Protocol version 2
(docs/modules/ROOT/pages/plans/remote-5-ssh.adoc, 5c).
Amendment (2026-10-27): a free drive letter, not a UNC base
The UNC base of the amendment above broke a Windows client’s write and edit (#505). pi keys those tools' mutation
queue by a local realpath of the resolved path, and tolerates only ENOENT and ENOTDIR there. Windows answers a
UNC server that does not exist with UNKNOWN, after about 2.7 s of network lookup; a drive that does not exist
answers ENOENT (pi-win’s probe). So the base is the remote root on the highest drive letter whose root does not
exist on the client, probed once per process: Z:\home\me\app. win32 resolution keeps a base’s drive as it keeps a
device. One letter serves every workspace, since the root follows it, and the per-anchor share is gone. With no letter
free, the switched tools refuse with that reason (docs/modules/ROOT/pages/plans/remote-6-release.adoc, 6a).
Amendment (2026-10-27): pi-ssh owns bash
pi refuses an extension that registers a tool another extension already owns, and drops that extension whole. pi-ssh
replaces pi’s bash to switch it to the remote, and pi-workflow replaces it to declare it sequential (#261). Loaded
together, pi refused pi-workflow, and its commit guard, reflection pause and batch order went with it (#511). Only
the release rehearsal loaded the two side by side.
In the distribution, pi-ssh owns bash. It loads first, and only through the meta package. Its bash is
sequential, and its local path is built with the shell settings pi reads, as pi-workflow’s is. pi-ssh is the owner
because if pi-workflow failed, a remote session’s commands would still run on the remote and never locally.
pi-workflow asks on pi’s per-runtime event bus whether bash has an owner, and registers its own only when none
answers: installed alone, or with pi-ssh off. The question and the shared shell options live in pi-workspace
(bash.ts). packages/pi/test/one-bash.test.ts loads both loaders through pi’s resource loader.