Plan: Remote execution — pi here, the work over there (meta plan)

Closes: #488 (tracking; phases #489, #490, #491, #492, #493, #494). Relates to: #483. Branch: one per phase. Status: Done (2026-10-27) — pi over SSH shipped in 0.128.3; the LAN and WAN figures are #512

Design

Why

Today pi runs on the box where the code is, and the developer reaches it over SSH with a terminal. Every keystroke and every mouse movement crosses the link as raw escape sequences, which is where today’s spurious aborts came from (#483). More: clipboard images, drag-and-drop, desktop notifications and the model credentials all live on the wrong side of the link. The developer wants the VS Code Remote-SSH shape: install pi once, locally, and point it at a machine. pi becomes a window onto that machine.

The end result should behave as if pi ran on the remote, to the greatest extent possible, and the limitations are named below rather than discovered.

The architecture: agent local, workspace remote

Three shapes were compared (Research helper, 2026-10-26; sources on the tracking issue):

Shape Who does it Why not / why

Whole agent on the remote, thin UI local

Claude Desktop SSH sessions, Codex --remote, Cursor, JetBrains

Model calls and credentials stay on the remote, so none of the local wins arrive; and a thin client that re-renders pi’s whole TUI is a large build with nothing of pi’s to reuse. It is also what the developer has today, minus the terminal.

Fix the terminal link

mosh, Eternal Terminal

Neither carries port forwards or a structured channel; mosh explicitly does no forwarding. They fix roaming, not the escape-sequence problem, and change nothing else.

Agent local, tools remote

Zed (system ssh, one multiplexed connection, a headless server it uploads), VS Code

pi has the seam: every built-in tool takes a swappable operations object (node_modules/@earendil-works/pi-coding-agent/dist/core/tools/{read,write,edit,bash,grep,find,ls}.d.ts, the operations? option of each create*Tool: ReadOperations { readFile, access, detectImageMimeType? }, WriteOperations { writeFile, mkdir }, EditOperations { readFile, writeFile, access }, BashOperations { exec(command, cwd, { onData, signal, timeout, env }) }, GrepOperations { isDirectory, readFile }, FindOperations { exists, glob }, LsOperations { exists, stat, readdir }; pi’s grep still spawns a local rg, hence the replacement in phase 2), user_bash accepts { operations } from a handler (pi’s docs/extensions.md, "user_bash"), and codemode calls tools through ctx.executeTool, so it follows them. pi ships an example that routes four tools this way: examples/extensions/ssh.ts in the installed pi (~/.local/share/pi-node/current/lib/node_modules/@earendil-works/pi-coding-agent/, 220 lines; also under node_modules/@earendil-works/pi-coding-agent/examples/extensions/). Credentials, sessions, memory and plans stay local by construction. Chosen.

The connection: system OpenSSH, one channel, a helper on the remote

Compared (details and sources on the tracking issue):

  • One ssh host cmd per operation (pi’s ssh.ts example): honours ~/.ssh/config, but every file read is a process spawn and a channel open, there is no shared state for port watching, and Windows clients have no ControlMaster to amortise it (Win32-OpenSSH #1328 still open, 2026-07).

  • The ssh2 npm package: one persistent connection with many channels and works on Windows, but re-implements SSH config (no ProxyJump, Include, Match, certificates, FIDO keys), leaves host-key handling to us, and its maintenance is marked inactive (last release 2025-08).

  • System ssh running one long-lived helper on the remote (Zed’s shape): ssh host node ~/.pi-workspace/helper.mjs, a newline-delimited JSON protocol on its stdin/stdout carrying every operation (file reads and writes, stat and realpath, directory listing, glob, exec with streamed output and cancellation, a port watch). Honours the user’s SSH config, agent, jump hosts and host keys; one authentication per session; needs no ControlMaster so a Windows client works; one process to supervise and reconnect. The helper is a single file uploaded over the same connection when missing or outdated. Chosen. Port forwards are separate ssh -N -L children, one per forward, which also needs no ControlMaster.

Requirement this creates: Node 20 or later on the remote, and a POSIX remote. The remote is a machine pi would otherwise run on, so Node is a fair ask; it is checked at connect and refused with the reason.

The one abstraction: Workspace

Every place our packages touch the workspace goes through one interface, with a local implementation and the SSH one:

  • Files: readFile, writeFile, mkdir, stat/lstat, realpath, readdir, exists, glob, rm, rename.

  • Processes: exec(file, args, { cwd, env, signal, timeout, onData }) with streamed output and an exit code; git(args, cwd) as a convenience over it.

  • Identity: root (the workspace path on the host that holds it), label (local, or user@host:/path), isRemote.

Where it lives: ADR-004 forbids pi-modes and pi-workflow importing each other, and both need it. ADR-004’s amendment of 2026-09-26 made a library package the answer for shared logic (@gadhs/pi-bash: no extension, no loader, a regular dependency of both), and says a new package is its own decision. So: a library @gadhs/pi-workspace holds the Workspace interface, the local implementation and the slot (Symbol.for("gadhs:workspace") on globalThis, as the runtime bridge does; workspaceFor(cwd) looks it up per call and falls back to local), and pi-modes and pi-workflow depend on it by name. The SSH implementation, the helper and the extension that wires them are an extension package, @gadhs/pi-ssh, loaded by the meta package as ./loaders/11-ssh.ts: after 10-modes.ts, which stays first because its factory seeds the permission system’s config (packages/pi/package.json //order; distribution.test.ts:66 asserts modes first and is unchanged), and before 12-workflow.ts. Order does not otherwise matter: the workspace slot is read per call, never at load, and pi lets an extension’s registered tool replace a built-in whatever the load order. The pi.extensions list and the filename prefixes move together, as that test holds. ADR-006 (below) records both packages as that decision.

Three reads of the local cwd feed the system prompt from session-wiring.ts: contextFiles(ctx.cwd, agentDir) (the context budget, line 254), and from @gadhs/pi-guidance’s `payload.ts, detectLanguages(ctx.cwd) (the language profiles, line 316; it tests for marker files in cwd and its first-level directories) and readProjectGuidance(ctx.cwd) (<cwd>/.pi/gadhs-guidance.md, line 318). Under the anchor (below) they see the mirrored AGENTS.md but no source. Phase 2’s mirror therefore also copies the remote’s .pi/gadhs-guidance.md and the language marker files' names (empty files suffice: detectLanguages only tests existence), so all three read right with no code change; phase 4 points detectLanguages at the workspace instead and drops the marker mirror.

What stays local, by design

Sessions and their JSONL, memory, plan drafts and approvals, standing approvals, diagnostics, the modes overlay and config, the agent files, guidance, the model registry and every credential, the title and nudges, pi-remote’s phone link, dropped files (a local file the human drops is attached from where it is). Nothing of these crosses the link, and two contract tests pin it: the protocol has no message that carries a path outside the workspace root or an environment value, and exec sends the remote no local environment (the remote command gets its login environment plus an allowlist of TERM, LANG, LC_*, COLUMNS), so a local ANTHROPIC_*, GOOGLE_* or GITLAB_TOKEN can never reach the remote through pi.

What must act on the remote

pi’s seven built-in tools and user_bash (!); codemode follows them for free. Then ours: workspace-write and workspace-read containment (real-path resolution must happen where the symlinks are, or containment is weakened); count; generate_image saving its file; monitors (spawned commands); helper worktrees (git worktree on the remote); session facts (git); and the whole commit path in pi-workflow: the guard’s staged diff, the review snapshot, review context, review rules, the approved-plan check, the SPDX authoring guard, the git-dir reader, the workspace audit. Today these are synchronous node:fs and execFileSync calls (about 20 files, listed per phase); all run inside hooks that are already async.

Helpers: pi-subagents builds each helper session with a fresh resource loader that re-instantiates the installed extensions, so the workspace extension loads in every helper too, and finds the parent’s connection in the shared slot. A helper therefore acts on the same remote with no change to pi-subagents. Its worktree needs the remote variant.

The local anchor

pi keys sessions, trust, AGENTS.md and .pi/ discovery by the local cwd. A remote session must not borrow those from whatever local directory pi was started in. So a launcher, pi-ssh user@host:/path, runs pi in an anchor directory, ~/.pi/agent/remotes/<host>/<path-slug>/, with --ssh user@host:/path. Sessions for that remote project live under the anchor. On connect the extension mirrors the remote project’s AGENTS.md (and its ancestors' within the repository) into the anchor so pi’s own discovery reads the right one, rewrites the working directory in the system prompt, and shows ssh user@host:/path in the status line. Trusting the anchor is trusting that remote project.

Port forwarding

/ssh forward <port>[:<local>] starts an ssh -N -L child; /ssh forwards lists; /ssh unforward. The helper watches listening TCP ports on the remote (ss -ltnH, polled every 2 s, as VS Code scans the remote’s processes) and reports new ones; pi notifies ("port 3000 is listening on the remote; /ssh forward 3000`"), and `autoForward: true in config forwards them without asking. Forwards are torn down at shutdown.

Honest limitations

  • Latency. Every operation is a round trip on one channel; an edit is two. On a LAN this is milliseconds; on a slow link the agent is slower than a local pi. The helper keeps it to one round trip per operation and streams output, which is the best this shape allows.

  • A dropped link. Phase 2: the channel is re-established and a command that was running is reported failed, not resumed. Phase 5 daemonises the helper with an output buffer so a command survives the link.

  • The remote project’s own .pi/ extras. Its AGENTS.md is mirrored. Project-local pi extensions, skills and prompts on the remote are not loaded: they would run locally against the wrong filesystem. Documented, with the workaround of installing them locally.

  • Windows. Client: supported (no ControlMaster needed; pi-win verifies). Remote host: POSIX only; a Windows remote is out of scope.

  • Credentials on the remote. git push, glab and anything the remote runs still use the remote’s own credentials, as they would under a local pi there. Model credentials never leave the local machine, which is a feature.

  • pi-subagents' own env probe runs local git against the remote path and will show no branch for a helper; cosmetic, documented.

  • Interactive programs in bash do not work, exactly as today.

ADR-006

Written with phase 1: "Remote execution runs the agent locally and the workspace over system SSH with a remote helper." It records the three shapes and the three connections above, the Node requirement, the anchor, and the amendment to ADR-004’s package ceiling. Hard to reverse: the Workspace interface becomes the seam every workspace-touching file depends on.

Scope

The phases

Each phase is its own issue, branch, plan (written in plan mode when it starts, under 300 lines, reviewed as any plan), MR and release note. This meta plan is the design they share; a phase plan says how, within it.

Phase Delivers Done when

1. @gadhs/pi-workspace: the seam

The library: the Workspace interface, the local implementation, the globalThis slot and workspaceFor(cwd) lookup with local fallback; ADR-006. Contract tests: the local implementation matches node:fs and child_process on paths, symlinks, errors and exit codes. Property test: realpath through the interface equals Node’s for arbitrary paths under a scratch tree.

Published; nothing else changed yet; every later phase builds on it.

2. @gadhs/pi-ssh: the SSH workspace, tools and launcher

The helper (one file, uploaded), the protocol, the connection with reconnect-and-report, the SSH Workspace; the seven built-in tools and user_bash replaced through pi’s operations seams (grep runs rg on the remote through exec, since pi’s grep spawns a local rg); --ssh, the pi-ssh launcher and anchor, the AGENTS.md mirror, the system-prompt cwd, the status line, /ssh status and /ssh reconnect; the loader 11-ssh.ts; GADHS_PI_SSH=off; the commit refusal above. Wire-format (contract) tests: every protocol message round-trips, the version handshake refuses a mismatch and never falls back to a local tool (the tool result is the refusal), the environment allowlist. Security (contract): with a remote workspace, git commit through bash is refused by the guard with the named reason, and a workspace write goes to the judge. Integration: the helper under a real Node child over pipes; each tool against a loopback ssh localhost where the box has one (it.skipIf, visibly). Regression: the asks the judge sees over SSH equal the local ones for the same tool calls.

A session over pi-ssh reads, writes, edits, greps and runs commands on the remote, with the judge seeing the same asks as locally.

3. pi-workflow on the seam

git-guard.ts, review-snapshot.ts, review-context.ts, review-rules.ts, approved-plan.ts, commit-message.ts, workspace-read.ts, workspace-audit.ts, authoring-guard.ts, git-dir.ts (all packages/pi-workflow/) moved from node:fs and execFileSync to the async Workspace. Not moved, because they read local state: glab-ambient.ts (the local glab config, at factory load), plan-pause.ts (the local plan draft), workflow-config.ts. Regression: the existing suites, unchanged, are the contract for local behaviour. Contract: the same suites run once more against a fake remote workspace. The phase-2 commit refusal is lifted.

A commit made over SSH is guarded, snapshotted and reviewed exactly as a local one.

4. pi-modes on the seam

authorize.ts containment (realLocation, reallyInside) and workspace-read resolution on the remote; count-tool.ts, image-tool.ts, monitor-process.ts, session-facts.ts, helper-worktree.ts (remote git worktree), detectLanguages on the workspace; `yield-tool.ts’s sidecar stays local and says so; helpers inherit the connection. Regression: the existing suites. Security (contract): a symlink out of the remote workspace is denied; the phase-2 "every remote write goes to the judge" rule is lifted only when that test passes.

A helper over SSH works in a remote worktree; containment holds against remote symlinks.

5. Ports, resilience, Windows

/ssh forward, the helper’s port watch and auto-forward; the daemonised helper with an output buffer so a dropped link does not kill a command; Windows client verified by pi-win; the measured per-operation cost on LAN and WAN in the docs. Concurrency: a command’s output across a reconnect arrives once and in order. Integration: a forward reaches a listener on the loopback remote.

The developer runs a dev server on the remote and opens it locally; a Wi-Fi drop mid-command resumes.

6. Docs and release

One canonical page, ssh.adoc ("pi over SSH"; remote.adoc is pi-remote’s "Working remotely" and stays), the cookbook entries, the limitations above verbatim, the release that flips it on.

Installed on the developer’s machine and in daily use.

Ordering and risk

1 and 2 give a usable, if guard-blind, remote pi quickly; 3 and 4 are the bulk (about 20 files, sync to async) and carry the regression risk, which the existing suites bound. 5 is independent of 3 and 4 and can interleave. The developer’s own terminal problem is addressed by #483’s fix meanwhile.

The risks, each with its mode and mitigation; a phase plan refines its own:

  • Guards blind between phases 2 and 3 (fail-open: a commit over SSH would pass the commit guard unreviewed, since the guard reads a local .git that is not there). Mitigation: phase 2 ships with the tool_call guards refusing git commit when the workspace is remote ("not yet guarded over SSH: commit from a local checkout"), so the gap is a refusal, never a pass. Lifted in phase 3.

  • Containment weakened (fail-open: realLocation resolving symlinks on the wrong machine). Mitigation: until phase 4 moves it, every workspace write over SSH is treated as outside the workspace and goes to the judge; phase 4’s test puts a symlink out of the remote workspace and asserts the deny.

  • The link drops mid-command (fail-closed, loud: the tool result is an error naming the drop; nothing is retried blind). Phase 5 makes it resume.

  • The helper is outdated or missing (fail-closed: the version handshake refuses and says which to update; no silent fallback to a local tool).

  • A local environment value leaks to the remote (silent by nature). Mitigation: the allowlist above, and its contract test.

  • Wrong machine (silent: a session believed remote acting locally). Mitigation: the status line names the host, every tool result carries no path the model could mistake for local (paths are the remote’s), and session.start in diagnostics records the workspace label.

Out of scope, named

A Windows remote host; the remote project’s pi extensions, skills and prompts; agent forwarding of local credentials to the remote; syncing files between the two machines beyond the AGENTS.md mirror; a thin-client mode; mosh or ET as the transport; ssh2.

Delivery

  • The tracking issue holds the research sources, one issue per phase, and each phase’s merge SHA. Each phase MR says Relates to #<tracking> and Closes #<phase>.

  • This meta plan is committed as docs/modules/ROOT/pages/plans/remote-execution.adoc, In progress from phase 1’s start until phase 6 lands, when it is Done and the tracking issue is closed with the last merge SHA. Each phase plan is plans/remote-<n>-<name>.adoc, written in plan mode when the phase starts.

  • Each phase carries its own CHANGELOG.adoc Unreleased entry (Added for phases 1, 2, 5, 6; Changed for 3 and 4, which change where the guards read) and its own release note. Phases 1 and 2 release together; 3 and 4 together; 5 and 6 as they land.

  • Files this plan names that the cold reviewer was not handed (the review budget is twelve), all existing today and each supplied with the phase plan that touches it: packages/pi-workflow/{review-rules,approved-plan,commit-message,workspace-audit,authoring-guard,git-dir,glab-ambient,plan-pause,workflow-config}.ts, packages/pi-modes/{count-tool,image-tool,monitor-process,yield-tool}.ts, packages/pi-guidance/payload.ts, docs/modules/ROOT/pages/remote.adoc, CHANGELOG.adoc, and pi’s dist/core/tools/*.d.ts, which are untracked (node_modules).

Erratum (2026-10-26): the remote needs Node 22 or later, not 20: the helper’s glob is fs.promises.glob, Node 22’s, and Node 20 has been end-of-life since 2026-04 (#490; phase 2a, plans/remote-2a-ssh-workspace.adoc). Phase 2 is split into 2a (#496: helper, protocol, SSH workspace) and 2b (#490: tools, /ssh, anchor, safeguards, loader).

Erratum (2026-10-26): phase 2 is three parts, not two: 2a (#496: helper, protocol, SSH workspace), 2b (#490: pi’s tools on the remote, at parity) and 2c (#498: /ssh, the anchor, the safeguards, the loader and the release); plans/remote-2b-tools.adoc.

Erratum (2026-10-26): there is no pi-ssh launcher and no --ssh flag. /ssh user@host:/path writes the anchor and a session file whose cwd is the anchor, and switches into it (ctx.switchSession); pi "/ssh …" starts straight in one (phase 2c, #498; plans/remote-2c-session.adoc).

Erratum (2026-10-26): containment on the remote (realLocation, the workspace-write and workspace-read stages, a redirect’s destination) moved from phase 4 to phase 2c on the operator’s choice, in place of 2c’s "every remote write goes to the judge" rule. Phase 4 keeps count, generate_image, monitors, session facts, helper worktrees and detectLanguages (#492). ssh.adoc starts with 2c, which turns remote execution on; phase 6 completes it.

Erratum (2026-10-26): phase 3 moved pi-workflow onto the seam as a pure core and one async shell per module, not as an await on each call: the guard’s decisions are functions of facts already read, and the reads go through the workspace (plans/remote-3-workflow.adoc). Its suites run twice in validate, locally and over the SSH route; the "contract: the same suites against a fake remote workspace" is that second run, against the real SSH workspace over a local helper.

Erratum (2026-10-27): the slot holds one workspace per anchor, not one in all. workspaceFor(cwd) takes the longest anchor or root holding cwd, and Workspace.at(root, anchor) gives a view over the same connection. A remote helper’s worktree is such a view, published under an anchor inside its parent’s (phase 4, #492; plans/remote-4-modes.adoc).

Erratum (2026-10-27): phase 5 is three units (plans/remote-5-ssh.adoc): 5a the Windows client (#501), 5b port forwarding and the remote port watch, and 5c the helper as a daemon with a relay per connection. Its "measured per-operation cost on LAN and WAN" is in two parts. 5c measures pi’s own overhead per operation and a drop’s cost, on one machine without ssh, because this box cannot ssh to itself. The LAN and WAN figures come from the operator’s sessions with the release (phase 6).

Erratum (2026-10-27): the release that flips it on is 0.128.3. Its rehearsal, once it packed every package (#510), found pi refusing pi-workflow beside pi-ssh, because both registered bash. pi-ssh now owns the one bash, which is sequential and built with pi’s shell settings, and pi-workflow registers its own only when unclaimed (#511, ADR-006 amendment).

Edit this page · latest