Plan: Remote execution phase 4 — the rest of pi-modes on the seam
Closes: #492 (phase 4). Relates to: #488 (meta plan: plans/remote-execution.adoc), #491 (3), #498 (2c), #501. Branch: feat/modes-on-the-seam. Status: Done (2026-10-27) — implemented as planned, with the errata below
Design
What this phase is for
Phase 2c moved containment onto the remote, and phase 3 the commit path. What still reads this machine in a remote session: count (walks local files, count-tool.ts:67-90), generate_image (reads references and writes its image locally, image-tool.ts:77-144), monitors (spawn their command here, monitor-process.ts:57-110), the judge’s session facts (git remote on the anchor, session-facts.ts:19-25), language detection (the anchor’s mirrored marker files, session-wiring.ts:316), and helper worktrees (git worktree add on a local repository the anchor is not, helper-worktree.ts:66-100). This phase moves each onto the workspace, in phase 3’s shape: pure decisions, one async shell that reads through Workspace. Local sessions keep their behaviour; their suites are the contract.
The seam: one workspace per anchor, and views
A helper in its own worktree works in a different directory than its parent. Over SSH that directory is on the remote, so it needs a workspace of its own. The slot (packages/pi-workspace/slot.ts) holds one today, and publishWorkspace throws on a second. It becomes a set:
-
publishWorkspace(ws)addsws. It refuses a second workspace for the same anchor, with today’s error. The withdrawal removes only that one. -
workspaceFor(cwd)returns the published workspace whose anchor or root holdscwd, the longest match winning, else the local one. So a helper anchor inside its parent’s anchor finds the helper’s workspace. -
currentWorkspace()goes. Its callers (pi-ssh’ssessionDepsandSshSession.start) askworkspaceFor(ctx.cwd), which is the question they mean. -
Workspace.at(root, anchor): Workspace: a view of the same host at another root, under another anchor. Locally it islocalWorkspace(root). Over SSH it shares the connection (sshWorkspace’s `connection()closure), so a helper’s worktree costs no second ssh. The contract suite gains the view’s cases: the same files, its ownresolve, the parent unaffected,closeon a view closing nothing.
Helper worktrees on the remote (helper-worktree.ts)
The provider’s prepare(ctx) asks workspaceFor(ctx.baseCwd). Local: today’s code, unchanged. Remote:
-
The repository and base from the remote (
ws.git(["rev-parse", "--show-toplevel"], hostCwd), thenHEADat the top). -
A worktree directory from the remote’s
mktemp -d -t gadhs-helper-XXXXXX, andgit worktree add -b <branch> <dir> <base>there. -
A local anchor for it inside the parent’s anchor:
<parent anchor>/.gadhs-helpers/<agentId>/. pi’s context discovery walks a cwd’s ancestors, so the helper reads the parent’s mirroredAGENTS.mdwith no mirror of its own; pi-ssh’s mirror skips hidden directories, so it never touches these. -
publishWorkspace(parent.at(<dir>, <anchor>)). The helper’s cwd is the anchor.
pi-subagents' dispose is synchronous (WorkspaceDisposeResult | undefined, node_modules/@gotgenes/pi-subagents/dist/public.d.ts:99-108) and runs as the helper’s turn loop ends (src/lifecycle/subagent.ts:700-712), so the kept-or-removed decision cannot be read from the remote in time. Remotely, dispose withdraws the view and returns the addendum: the branch and the worktree on dev@box, kept for the parent to merge or open an MR from, with the removal command, and "removed shortly after if it holds no work". In the background it reads the worktree (status --porcelain, rev-list --count <base>..HEAD); with no commits and no changes it is removed, with the local anchor directory; either way diagnostics record the outcome. An upstream issue proposes an async dispose, after which the remote case reads like the local one.
A helper session in such an anchor has no remote.json. pi-ssh’s session_start (session.ts) already reuses a published workspace whose anchor is the cwd; it now checks that first, before remote.json, so the helper gets the <cwd> rewrite and the <gadhs-ssh> block naming its worktree.
The gate places a forwarded ask by its own path (judge-wiring.ts, authorize.ts)
A helper’s asks are forwarded to the parent session and decided there (modes.adoc, "A helper’s asks"). The gate’s cwd is then the parent’s anchor. A helper write under <parent anchor>/.gadhs-helpers/<id>/x would be placed by the parent’s workspace, at the wrong remote path. So decideAtGate (judge-wiring.ts:987-1012) hands authorizeAsk the workspace for the ask’s path when the path is absolute (workspaceFor(path)), and for the gate’s cwd otherwise. The helper’s write is then contained against its worktree. inRemoteTerms (authorize.ts:182-188) places it with that workspace’s anchor.
Session facts (session-facts.ts)
createSessionFacts, askFacts, readBranch, readPushKind and readPushRemote read git on the anchor through execFileSync. They become async over ws.git; remoteKindOf’s "a path-form remote that names a directory" asks the workspace’s host (`ws.stat), not this machine. The cache stays per cwd. withWorkspace (2c) keeps naming the SSH workspace, and stops overriding remote and remoteKind, now read from the remote. decideAtGate awaits them.
Language detection (session-wiring.ts)
detectLanguages(cwd, exists, subdirs) (packages/pi-guidance/payload.ts:66-77) already takes its reads as functions. For a remote workspace, pi-modes gathers the marker files present at the root and one level down in one find on the host, as pi-ssh’s mirror does today (packages/pi-ssh/mirror.ts, remoteMarkers), and calls detectLanguages with lookups into that set. pi-guidance is unchanged. pi-ssh’s mirror stops writing markers and removes the ones it wrote, as the meta plan foresaw.
count (count-tool.ts)
listFiles and the per-file read move to ws.stat, ws.readdir and ws.readCapped, keeping today’s rules: .git and node_modules skipped, the name filter, COUNT_LIMITS.maxFiles, maxFileBytes ("too large"). The counting stays the JavaScript RegExp it is, over text read back, so a remote count equals a local count of the same tree, which a parity test pins. Every file costs a round trip within those limits; ssh.adoc says so.
generate_image (image-tool.ts)
References are read through ws.readFile, existence through ws.stat, the output written through ws.mkdir and ws.writeFile. Containment is reallyInside locally and remoteWriteInside (2c) for a remote workspace. Paths resolve against the session’s directory on the host.
Monitors (monitor-process.ts, monitor-registry.ts)
The registry uses a child’s stdout data, its close and error, a start check, and a group kill (monitor-registry.ts:225-233, 350-359, 527). These become a MonitorProcess interface.
-
Local: today’s
ChildProcess, wrapped: detached group, pi’s shell,taskkillon Windows. -
Remote:
ws.spawn("bash", ["-lc", command], { cwd, env, onStdout, onStderr })(Workspace.spawn,types.ts:89-94). The helper starts it in its own group (2a), stderr is appended to the monitor’s local stderr file, andkillsignals the remote group,SIGKILLafter the grace. -
An
on_matchaction runs where its source runs. -
The log and script files stay local; they are the monitor’s records.
-
The remote command gets 2a’s environment allowlist and nothing else of this machine’s.
Fail modes
-
The link drops under a monitor: the source ends with the connection’s error, reported as a monitor exit (fail-closed, loud).
restart: on-exitrestarts it once the link is back. -
The link drops during
countorgenerate_image: the tool fails with the reason (fail-closed, loud). -
Session facts cannot be read:
none, as today for a directory with no repository; a push’s destination stays a network host (fail-closed for the shared-branch rules). -
A remote worktree cannot be made (no git, mktemp refused, the add fails):
preparethrows with git’s message and the helper fails, as locally, rather than running unisolated (fail-closed, loud). -
The background removal fails: the worktree is kept, as the addendum allowed, and diagnostics say why (fail-open on tidiness, loud).
-
Two workspaces published for one anchor: refused, as today (fail-closed).
Scope
In
-
packages/pi-workspace/:slot.ts(a set, longest match,currentWorkspaceremoved),types.tsandlocal.ts(at),contract.ts(the view’s cases),index.ts, README. -
packages/pi-ssh/:ssh-workspace.ts(atover the shared connection),extension.tsandsession.ts(workspaceFor(ctx.cwd); reuse first, from the workspace),mirror.ts(no markers; stale ones removed), README. -
packages/pi-modes/:helper-worktree.ts,subagent-wiring.tswhere it names the provider,judge-wiring.ts,authorize.ts,session-facts.ts,session-wiring.ts,count-tool.ts,image-tool.ts,monitor-process.ts,monitor-registry.ts,command-wiring.tswhere the tools are registered. -
Tests, by category:
-
Contract: the view in the workspace contract, local and over pipes.
-
Unit: the slot (many published, longest match, same anchor refused, a withdrawal removes only its own).
-
Parity:
countover the SSH workspace equals the local count on the same tree. -
Integration (over pipes, real temp trees and repositories):
generate_imagewrites on the remote; a monitor’s events arrive and a stop takes down the remote group; session facts read the remote origin, branch and push kind; languages are read from remote markers with the mirror writing none; a helper worktree is made on the remote, the helper’s tools act in it, its session gets the<cwd>rewrite, and dispose withdraws it; the background removal takes a clean worktree and keeps one with a commit. -
Security (contract):
generate_imagerefuses a path through a link out of the remote tree; a forwarded helper write is contained against its worktree, not the parent’s root; a remote monitor’s environment carries no local secret. -
Regression: the existing pi-modes suites unchanged except for
awaitand the workspace argument, checked as in phase 3.
-
-
Mutation check: the longest match, the same-anchor refusal,
at’s shared connection, the path-placed workspace at the gate, the remote `remoteKindOf, `count’s skip rules and limits, `generate_image’s remote containment, the monitor’s remote kill, and the clean-worktree removal; each killed before the MR. -
An upstream issue on
@gotgenes/pi-subagents: an asyncdispose. -
Docs:
ssh.adoc(helpers, monitors,countand images over SSH; the limitations above);modes.adoc(helper worktrees, monitors, the gate’s placement);packages.adoc(pi-workspace’s slot, pi-modes);CHANGELOG.adoc(Changed); a meta-plan erratum, since the slot’s one-workspace rule became one per anchor. -
Delivery: one MR,
Closes #492,Relates to #488; the merge SHA on #488; phase 5 (#493) next.
Out
The Windows client (#501, phase 5). Ports and the daemonised helper (phase 5). An async dispose in pi-subagents (upstream). Mirroring a helper’s own AGENTS.md, since it reads its parent’s by ancestry. Caching across calls.
Named but not supplied to the cold reviewer (the review budget is twelve files): CHANGELOG.adoc, docs/modules/ROOT/pages/{ssh,modes,packages}.adoc, packages/pi-workspace/{local,contract,index}.ts, packages/pi-ssh/{extension,session}.ts, packages/pi-modes/{subagent-wiring,command-wiring,session-wiring}.ts, packages/pi-guidance/payload.ts, and pi-subagents' dist/public.d.ts and src/lifecycle/subagent.ts (node_modules).
Erratum (2026-10-27): count reads each file with ws.readFile after a ws.stat size check, not with ws.readCapped. This keeps an unreadable file’s reason in its skip line, and the text is the same on either host, which the parity test pins.
Erratum (2026-10-27): the allowlist of what crosses to a remote command (sessionMarkers, pi’s PI_* session markers) moved from pi-ssh’s ops.ts to @gadhs/pi-workspace (env.ts). pi-modes' remote monitors send the same allowlist as pi-ssh’s bash tool, and pi-modes has no runtime dependency on pi-ssh. A remote action gets that allowlist plus the event’s GADHS_MONITOR_* variables.
Erratum (2026-10-27): a remote monitor’s shell is named bash on <destination>:<path>. In a remote session the monitor tool does not resolve this machine’s shell at all.
Erratum (2026-10-27): a link that drops under a remote monitor reaches it as an exit with no code: the connection ends every stream as SIGHUP. The closing notice is therefore the exit notice, as the fail-modes section says, rather than an error notice.
Erratum (2026-10-27): the security test for a helper’s forwarded write uses a worktree root outside /tmp. The shipped /tmp/* rule allows a write tool’s path by its text, before the workspace stage, so a link under /tmp is not walked. That is pre-existing, for local and remote sessions alike, and is filed as #502.
Erratum (2026-10-27): the session-start notice of kept helper work still reads local git, so a remote session does not list kept remote worktrees. Filed as #503, and named in `ssh.adoc’s limitations.
Erratum (2026-10-27): one existing test changed beyond await and the workspace argument. The headless Stop case in judge-wiring-callback.test.ts waited one tick for the gate’s dialog. The gate now awaits the push’s facts (async git calls) first, so the test waits until the dialog is open. Two count cases now assert a rejection or a resolution instead of a synchronous throw: a missing path, and the literal needles, whose walk is now awaited too.
Erratum (2026-10-27): the upstream issue for an async dispose is drafted in #504, ready to post at gotgenes/pi-packages. This box has no GitHub credentials. dispose is still synchronous in pi-subagents 25.0.4.