Plan: Remote execution phase 2b — pi’s tools on the remote, at parity

Closes: #490 (phase 2b). Relates to: #488 (meta plan), #496 (2a), #498 (2c). Branch: feat/pi-ssh-tools. Status: Done (2026-10-26) — implemented as planned, with the errata below

Design

What this phase is for

Phase 2a made a Workspace that acts on another host. This phase makes pi’s tools use it: read, write, edit, ls, find, grep, bash, and ! (user_bash). The bar is the meta plan’s: functions identically. Here that means a parity test for each tool: the same fixture tree, pi’s own tool run locally against it and ours run through an SSH workspace against it, and the text the model sees compared exactly. Where pi’s output includes a local path the remote cannot know, the plan says what differs and why.

How each tool reaches the remote

pi’s tools take an operations object (node_modules/@earendil-works/pi-coding-agent/dist/core/tools/*.d.ts). Where the operations cover everything the tool touches, ours wraps pi’s tool with operations over the workspace, and pi’s own formatting stays. Where they do not, ours re-states the tool, with the helpers pi exports from its package root (dist/index.d.ts, line 26: truncateHead, truncateTail, truncateLine, formatSize, DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, and the create*ToolDefinition factories) and a parity test.

Tool How Notes

read

pi’s tool, ReadOperations

readFile, access (a stat that throws ENOENT when missing), detectImageMimeType(path), which reads the file’s first bytes with readCapped-sized readFile and sniffs the PNG, JPEG, GIF and WebP magic, so images read remotely display as they do locally.

write

pi’s tool, WriteOperations

writeFile, mkdir recursive.

edit

pi’s tool, EditOperations

readFile, writeFile, access. pi’s own diff and its "old text not found" messages are unchanged.

ls

pi’s tool, LsOperations

exists, stat, readdir.

find

ours, fd on the remote

pi’s find with a custom glob drops fd’s `.gitignore awareness (find.js:69-80: only node_modules and .git ignored). Ours runs the same fd invocation pi runs (find.js:118-160: --glob --color=never --hidden, --no-require-git outside a repository, --max-results, --full-path for a pattern with a separator) on the remote, and formats as pi does (relative to the search root, the same notices). Parity against pi’s local find on a tree with a .gitignore.

grep

ours, rg on the remote

pi’s grep spawns a local rg (grep.js:52,101) and its GrepOperations only read context lines. Ours runs the same rg --json --line-number --color=never --hidden invocation on the remote, parses the JSON as pi does, and formats identically: path:line: text and path-line- text context, truncateLine, the 100-match and byte limits, the same notices. Parity against pi’s local grep on the same tree: pattern, glob, context, ignoreCase, literal, limit, and the no-match text.

bash

pi’s tool, BashOperations

exec(command, cwd, { onData, signal, timeout, env }): ["bash", "-lc", command] on the remote through workspace.exec, output to onData, timeout seconds to timeoutMs. Its contract (bash.js:50-123): throw Error("aborted") on abort, Error("timeout:<s>") on timeout, "Working directory does not exist" when cwd is missing (a remote stat first), and 128 + signal as the exit code of a signal-killed shell.

!

user_bash handler returning { operations }

The same BashOperations, so ! runs on the remote with pi’s streaming view.

What pi passes as env, and what crosses. pi’s bash tool builds env from the local shell environment plus PI_SESSION_ID, PI_SESSION_FILE, PI_PROVIDER, PI_MODEL, PI_REASONING_LEVEL (bash.js:135-152) and hands it to exec. The adapter forwards only the PI_* keys as env additions; everything else is dropped, and workspace.exec adds its allowlist (2a). So the remote command gets its own login environment, the terminal and locale, and pi’s session markers. Pinned by a test with a stubbed local GITLAB_TOKEN.

Truncated bash output. When output exceeds pi’s limits, pi writes the full output to a local temp file and names it in three places of the result (bash.js: the content[0].text notice, details.fullOutputPath, structuredContent.full_output_path). In a remote session the read tool reads the remote, so that path would be unreadable. The wrapper copies the local temp file to the remote (<home>/.pi-ssh/tmp/pi-bash-<id>.txt, home from the remote’s hello, through writeFile) and rewrites all three. Parity test: a command printing past the limit yields a full_output_path that read (remote) reads in full.

Paths. pi’s tools resolve relative paths against ctx.cwd, which in a remote session is the local anchor (2c). workspace.resolve maps an anchor path under the remote root and passes a remote absolute path through (2a), so both the model’s src/x.ts and its /srv/app/src/x.ts reach the right file. Output paths are relative to the search root (find, grep) or absent (read, edit), so they read the same.

Switching: extension.ts

registerRemoteTools(pi, deps) registers the seven tools under pi’s names and the user_bash handler. Each tool’s execute asks deps.workspace() (currentWorkspace() from @gadhs/pi-workspace, slot.ts): remote, and the call goes through the remote tool; otherwise through pi’s own local tool, unchanged.

The host facts the tools need. Workspace (types.ts) says nothing about the host, and sshWorkspace keeps its connection private. This phase widens sshWorkspace’s return type to `RemoteWorkspace extends Workspace with destination and host(): Promise<HostFacts> ({ platform, arch, home, node }, from the connection’s hello, connecting if needed), exported from ssh-workspace.ts; extension.ts narrows with "host" in ws. The hello gains arch: process.arch (protocol.ts Hello, helper.mjs, additive; protocol stays 1). Every remote path the tools write to is built from home, absolute, never $HOME in a string. The decision is per call, so a session that connects (2c) or loses its remote switches without re-registration. The default export is the extension factory; the loader in @gadhs/pi is 2c’s, so in this phase nothing loads it for a user.

rg and fd on the remote: remote-tools.ts

pi provisions rg and fd for the local machine from their GitHub releases (dist/utils/tools-manager.js: BurntSushi/ripgrep, sharkdp/fd, static linux-musl, apple-darwin and windows-msvc assets by architecture, cached under the agent dir, skipped under PI_OFFLINE). The remote needs the same, for its own platform and architecture. ensureRemoteTool(workspace: RemoteWorkspace, "rg" | "fd"), in order:

  1. command -v rg on the remote: present, use it.

  2. <home>/.pi-ssh/bin/rg on the remote with the pinned version (rg --version): use it.

  3. Otherwise download the asset for the remote’s platform and arch (from host()) locally, from the same release URLs pi uses, at versions pinned in remote-tools.ts (ripgrep 14.1.1, fd 10.2.0; a chore: bumps them), extract with the system tar as pi does, cache under <agentDir>/gadhs-pi-ssh/tools/, and writeFile the binary to <home>/.pi-ssh/bin/ on the remote with mode 0o755. Once per remote.

  4. Under PI_OFFLINE, or a download failure, or a platform with no static asset: the tool’s result is an error naming the host and what to install (apt install ripgrep, say). Fail closed: never a local rg on local files.

The download runs on the developer’s machine because that is the one with the credentials and, often, the internet.

Fail modes

  • The workspace is lost mid-call: the tool result is the connection’s error (fail-closed, loud); pi’s tool shows it as any failed tool; the next call reconnects (2a).

  • rg or fd cannot be had on the remote: the tool errors with the reason and the remedy; nothing searches locally instead (fail-closed).

  • A download is tampered with in transit: each pinned version carries the asset’s SHA-256 beside it, checked before upload; a mismatch is refused and named (fail-closed).

  • full_output_path upload fails: the result keeps pi’s text with the local path and adds one line saying the full output stayed on the local machine (fail-open on the detail, loud).

  • A remote whose bash -l prints to stdout on login (a chatty profile): the output precedes the command’s, as it would for any ssh host bash -lc (silent, by design; documented, the same as running pi there with that profile).

  • A parity test’s fixture depends on the local rg or fd: pi’s own tools download them on first use; where that cannot happen (offline CI), the parity cases for find and grep skip visibly, never pass empty (fail-closed for the test: a skip is reported, not a pass).

Scope

In

  • This plan as docs/modules/ROOT/pages/plans/remote-2b-tools.adoc, nav entry, In progress at the start.

  • packages/pi-ssh/: tools.ts (the operations objects and the two re-stated tools), remote-tools.ts (rg and fd), extension.ts (registerRemoteTools, the user_bash handler, the default export), index.ts exports, package.json exports gaining ./extension, peerDependencies @earendil-works/pi-coding-agent as the other extension packages declare it; helper.mjs hello gains arch.

  • Tests, by category:

    • Parity (contract): test/parity.test.ts builds one fixture tree (text files, nested directories, a .gitignore and ignored files, node_modules, a PNG, a long-lined file, a file with a BOM) and, for each tool and the inputs named in the table, asserts our result’s content text equals pi’s local tool’s on the same tree. bash compares output, exit code and the truncation shape. find and grep skip visibly without a local fd or rg.

    • Security (contract): the bash adapter forwards only PI_* of pi’s env; a stubbed local GITLAB_TOKEN and AWS_SECRET_ACCESS_KEY never reach the remote command; the PI_* markers do.

    • Unit: the bash contract (aborted, timeout, missing cwd, signal exit code), the full_output_path relocation and its fail-open line, image sniffing, ensureRemoteTool’s four branches with a fake downloader and a fake remote (`command -v found, cached, downloaded and uploaded with 0o755 and the checksum, refused offline and on a bad checksum), the per-call switch between local and remote.

    • Mutation check: the PI_* filter, the checksum check, the offline refusal, the per-call switch, find’s `--no-require-git branch, `grep’s limit; each killed before the MR.

  • Docs: a section in the package README; packages.adoc’s row updated; `CHANGELOG.adoc Added; the meta plan gains an Erratum line re-scoping phase 2 into 2a (#496), 2b (#490: the tools) and 2c (#498: /ssh, anchor, safeguards, loader, release), since its first erratum still describes 2b as carrying /ssh and the loader.

  • Named but not supplied to the cold reviewer: CHANGELOG.adoc, docs/modules/ROOT/nav.adoc, docs/modules/ROOT/pages/packages.adoc, packages/pi-ssh/{index,protocol}.ts, packages/pi-workspace/slot.ts, and pi’s dist/core/tools/{read}.js, dist/utils/tools-manager.js and dist/index.d.ts (untracked node_modules; the line numbers above are pi 1.1.0’s, the repository’s pinned copy).

  • Delivery: one MR, Closes #490, Relates to #488; the merge SHA on #488; 2c’s plan next. Released with 2c.

Out

  • /ssh, the anchor, the session switch, the system prompt, the status line, the loader, the commit refusal and judge safeguards, docs page and release (2c, #498). The guards and containment (phases 3 and 4). Port forwarding (5). PowerShell as a remote shell (a Windows remote is out of scope). Chunked uploads of large binaries: rg and fd are a few MB each and go in one writeFile.

Erratum (2026-10-26): the operations are ops.ts and the re-stated grep and find remote-search.ts, not one tools.ts; detectImageMimeType reads the first 16 bytes with the remote’s head -c. Erratum (2026-10-26): pinned at ripgrep 15.2.0 and fd 10.5.0, the current releases when implemented, not 14.1.1 and 10.2.0. Erratum (2026-10-26): grep passes --no-config first, as pi 1.4.0’s does (the repository’s pi 1.1.0 lacks it), so a remote’s ripgrep config cannot change results. Erratum (2026-10-26): an uploaded tool is <home>/.pi-ssh/bin/<tool>-<tag>, reused when present rather than asked --version, and written to a .part name then renamed, so an interrupted upload is never run. Erratum (2026-10-26): the forwarded markers are PI_SESSION_ID, PI_PROVIDER, PI_MODEL and PI_REASONING_LEVEL; PI_SESSION_FILE is a local path and stays local. Erratum (2026-10-26): the full output’s remote copy keeps pi’s temp file name, <home>/.pi-ssh/tmp/<name>, and a thrown abort or timeout naming it is rewritten too. Erratum (2026-10-26): a remote call sees the remote root as ctx.cwd (a proxy over pi’s context), not the anchor, so its messages name remote paths as pi’s do locally; a leading ~ in a path is the remote’s home.

Edit this page · latest