Plan: Remote execution phase 3 — the commit path on the seam

Closes: #491 (phase 3). Relates to: #488 (meta plan: plans/remote-execution.adoc), #498 (2c), #492 (phase 4). Branch: feat/workflow-on-the-seam. Status: Done (2026-10-26) — implemented as planned, with the errata below

Design

What this phase is for

Phase 2c made a remote project a pi session and refused git commit there, because the guard reads a local checkout that an anchor is not. This phase makes the guard read the checkout where it is. "Functions identically" (the meta plan) means: the same refusals, the same review, the same snapshot and declared context, the same trailers, whether the checkout is on this machine or on dev@box.

The shape: a pure core and one async shell

pi-workflow’s guard mixes two things in the same functions: deciding (what a command line commits, whether a message is well-formed, whether a relocation leaves the checkout, which trailer to splice) and reading (the index, .git/HEAD, a -F file, AGENTS.md at HEAD). Fifty-three functions touch node:fs or execFileSync directly or through a callee (call-graph count, 2026-10-26), most of them only because a pure decision takes a cwd and reads inline.

Two shapes were weighed. A synchronous facade over a worker thread (Atomics.wait on a SharedArrayBuffer) keeps every signature and every test, and is rejected: it blocks pi’s main thread for each remote round trip, so the TUI freezes and the lone-Esc patch (#483), which needs a free event loop, regresses; and the worker cannot reach the session’s connection, which lives in the main thread’s slot, so it would open a second one. It preserves the look of the code by hiding that the guard now does network I/O. An await on all fifty-three propagates the tangle. So:

  • The core stays synchronous and pure. validateMessage, attributed, stagesAndCommits, nestedCommit, noVerifyBackstop, textFindCommits, the structural parsers, `commitTargetOf’s policy, `splitApprovedPlan’s arithmetic: untouched, or given the fact they used to read. A pure function receives facts (text already read, a layout already found) and returns a decision.

  • One async shell per module gathers the facts through Workspace (packages/pi-workspace/types.ts: readFile, readCapped with workspace-read’s never-truncated rule, stat, lstat, realpath, readdir, git(args, cwd, options)), from workspaceFor(ctx.cwd): the local implementation in a local session, the SSH one in a remote session. The shell is where await lives, and it is thin.

  • The public functions keep their names and gain ws and async, so index.ts and the tests change by an await and an argument, while the decision they wrap is also exported pure for new tests to drive with hand-built facts. Nothing defaults ws silently: the caller names the workspace.

The facts, module by module

Module Reads today On the seam

git-dir.ts

resolveGitDir, commonGitDir: .git as a file (gitdir:), commondir

async over ws.readFile/ws.stat. The two callers below take the result.

git-guard.ts

repoRoot (walks for .git), currentBranch (HEAD), branchExists (refs/heads/<b>, packed-refs), readStagedDiff, readStagedForReview (git diff --cached/HEAD, --stat, excludes); followCd/expandLiteralPath read this machine’s homedir() for cd and ~ (git-guard.ts:331-341)

readRepoFacts(ws, cwd): Promise<RepoFacts>: { root, gitDir, commonDir, branch, mainExists, home }, read once per guarded call. commitTargetsElsewhere splits into pure relocationTargets(command, cwd, home) (the directories a cd, pushd, git -C lands in, or unknown) and pure elsewhereVerdict(command, cwd, home, rootOf) over a Map<dir, root> the shell fills with repoRootOf(ws, dir) per target. guardGitCommit(input, ws) is async and calls pure decideCommit(input, facts, commits); readStagedForReview(ws, …) and stagedDiffKey(ws, …) go over ws.git, a truncated result (ExecResult.truncated) read as unknown, as maxBuffer is today. nestedCommit, commitsAllTracked, commitNamesPaths and textCommitNamesPaths (git-guard.ts:660-703, 868) touch the filesystem only by calling findCommits for its -F read; they take `scanCommits’ invocations instead and stay synchronous.

commit-message.ts

findCommits reads a -F/--file message: realOrSelf (:342-361, the file a write earlier on the line targets, for the stale-message refusal settleMessageFile), resolveMessagePath (:545-560: realpath under the cwd or under one of the fixed SCRATCH_ROOTS, /tmp, /var/tmp, /var/folders, each skipped when absent) and readCapped

Pure scanCommits(command) yields invocations whose message is inline, from a file (its raw path kept), from stdin (unreadable), or absent; async findCommits(command, cwd, ws) resolves the file ones through ws.realpath (the cwd, the scratch roots and the written-earlier targets all made real on the host, absent roots skipped as today) and ws.readCapped with the same containment, then applies settleMessageFile. SCRATCH_ROOTS stays the fixed list the modes.json mirror test pins. The ordering of refusals is unchanged.

workspace-read.ts

resolveWorkspacePath (realpath inside the root), readCapped

async over ws.realpath and ws.readCapped; the never-truncated rule is the seam’s own.

review-context.ts

gitPathOf, isTracked (ls-files --error-unmatch), readIndexed (git show :./x), trackedFiles, isRegularFile, missingHint; resolvePlanContext (:217, a plan’s declared files and ranges, by resolveWorkspacePath and readCapped)

resolveReviewContext(ws, …) and resolvePlanContext(ws, …) async; each helper over ws.git/ws.stat/ws.realpath/ws.readCapped. plan-pause.ts:34,128 passes the session’s workspace for the repository reads.

review-snapshot.ts

git(args) by spawnSync for read_file, search, list on index, worktree, disk

createSnapshot(ws, cwd, subject, budget); execute becomes async. In reflection-review.ts, serveToolRound (:629-635, synchronous today, void) becomes async and askWithSnapshot (:714) awaits it; the loop around it is already async. A plan review’s disk subject is the repository on the workspace’s host; the plan draft itself stays a local read (plan-pause.ts:77), as it is local by design.

review-rules.ts

git rev-parse --show-toplevel, git show HEAD:AGENTS.md

async over ws.git.

approved-plan.ts

git rev-parse --show-toplevel, readFileSync(top/path)

async; `splitApprovedPlan’s arithmetic stays pure over the bytes handed in.

authoring-guard.ts

guardWrite (:99-108) reads whether the target exists (existsSync, for the SPDX header on a new file) and its content (readFileSync, for debt carried forward), through its injectable AuthoringDeps

guardWrite(path, content, previous) takes the prior content as a fact, undefined when the file is absent, and AuthoringDeps goes; pauseWrite (index.ts:231) reads it first through ws.stat and ws.readFile at ws.resolve(path). guardEdit reads nothing and is unchanged, as is pauseEdit.

workspace-audit.ts

.githooks existence, repository config, CHANGELOG.adoc, tags under refs/tags and packed-refs

async over ws.stat, ws.readFile, ws.readdir; warnGitSetup at session_start awaits it.

commit-pause.ts

orchestrates the above; 2c’s remoteWorkspaceRefusal

Passes ws down; the refusal and REMOTE_COMMIT_REFUSAL are removed.

index.ts

registerToolCallPauses, warnGitSetup

Computes ws = workspaceFor(ctx.cwd) once per event and hands it down.

Not moved, because they read local state by design (meta plan): glab-ambient.ts, plan-pause.ts’s draft read, `workflow-config.ts, `batch-order.ts’s settings.

One addition to the seam. Workspace.home(): Promise<string>: the host’s home directory, for followCd’s bare `cd and ~ (git-guard.ts:331-341), which today read this machine’s. Local: os.homedir(). SSH: the hello already carries home (protocol.ts:20), so sshWorkspace answers from it with no protocol change. The contract suite gains the case (equal to os.homedir() for the local implementation; equal to the hello’s for the helper over pipes).

What a commit over SSH then does

The agent runs git commit -F .gadhs-commit-msg through bash, which 2b runs on the remote. At tool_call, the shell reads the remote checkout’s facts, the message file on the remote, the staged diff and stat from the remote index, the declared context from the remote index, and AGENTS.md at the remote HEAD. The reviewer’s snapshot tools read the remote index. The guard’s mutation splices the trailers into the command text, which then runs on the remote, where the repository’s own hooks run. The verdict and the ledger key are what they would be for the same index on this machine.

Behaviour held equal, and how

  • The existing suites are the contract for local behaviour. They change by await and a ws argument only, and that is checked: a script over the test diff counts the changed lines and fails the MR when a change is anything but an added await/async, a ws argument, or a hand-built fact where a test used to lean on implicit I/O; the exceptions are listed in the commit message.

  • The same suites run against the SSH workspace. test/workspace-under-test.ts returns localWorkspace(cwd) or, under GADHS_PI_WORKFLOW_TEST_WS=ssh, sshWorkspace over the pipe transport (@gadhs/pi-ssh/test/transports.ts’s helper over pipes, root and anchor the same local path) for the same real repositories the suites build. `node tools/task.mjs validate runs pi-workflow’s suite in both modes. A behaviour that differs between the two fails here, by name.

  • Security (contract): with a remote workspace published for the session’s cwd, git commit -m 'feat: x' through bash is reviewed (the reviewer sees the remote’s staged diff and the message), refused for a --no-verify, refused on main, and attributed with the trailers; git commit from a local session is unchanged. Containment of a -F path and of declared context holds through the remote’s real paths (a link out of the checkout is refused as it is locally).

  • Regression: the 2c test "a commit in a remote session is refused" is replaced by "a commit in a remote session is guarded as a local one".

  • No test is weakened: a case that today asserts a synchronous throw asserts the rejection.

Fail modes

  • The link is down at the guard: ws.git and the reads fail; the staged state is unknown, which the guard already treats as a pause-and-retry refusal (UNKNOWN_STAGED), never a pass (fail-closed, loud: the refusal names the loss).

  • A read is truncated on the remote (ExecResult.truncated): read as unknown, as maxBuffer is today (fail-closed).

  • A -F file or declared path resolves outside the remote checkout: refused, as locally (fail-closed).

  • The reviewer’s snapshot exhausts its budget over a slow link: the review answers from what it has, as today (fail-open on detail, disclosed in the review’s reads).

Limitations

Each read is a round trip: a guarded commit over SSH costs on the order of ten, plus the reviewer’s own reads. The remote’s hooks run with the remote’s tools and credentials. A plan draft stays local, so a plan review’s declared files are read on the remote and the draft here.

Scope

In

  • packages/pi-workspace/: types.ts (home()), local.ts, contract.ts, README.

  • packages/pi-ssh/: ssh-workspace.ts (home() from the hello).

  • packages/pi-workflow/: the modules in the table; test/workspace-under-test.ts; every test file that calls a shell function (await and ws); new tests for the pure core (scanCommits, relocationTargets/elsewhereVerdict, decideCommit with hand-built RepoFacts); the security contract above in test/extension-wiring.test.ts; package.json devDependency @gadhs/pi-ssh for the pipe transport.

  • tools/task.mjs: validate runs pi-workflow’s suite twice, local and ssh.

  • tools/ or the commit: the test-diff check for this MR (a script run once, its output quoted in the commit message; not a permanent gate).

  • Mutation check: each refusal’s read moved to the seam (branch on main, --no-verify, a -F outside the checkout, a stale -F written earlier on the line, an elsewhere relocation through cd ~, an untracked declared file), the truncation-as-unknown rule, a scratch root absent on the host; each killed before the MR, under both workspaces.

  • Docs: modes.adoc "Workflow guards" gains one paragraph (the guard reads the session’s workspace, local or SSH); ssh.adoc drops the commit limitation and gains the round-trip note; packages.adoc’s pi-workflow and pi-workspace rows; `architecture.adoc’s pi-workflow line; `CHANGELOG.adoc Changed (where the guards read) and Fixed (the refusal lifted); the meta plan’s phase-3 row stands, with an erratum naming the core/shell shape; ADR-006 one line (phase 3 lands the pure core/async shell split the Negative consequence foresaw).

  • Delivery: one MR, Closes #491, Relates to #488; the merge SHA on #488; phase 4 (#492) next.

Out

pi-modes' reads (phase 4, #492): count, generate_image, monitors, session facts, helper worktrees, detectLanguages. A sync facade or worker. Caching facts across calls. Changing any refusal’s wording or order. The reviewer’s model calls, which are not workspace reads.

Named but not supplied to the cold reviewer (the review budget is twelve files): CHANGELOG.adoc, docs/modules/ROOT/pages/{modes,ssh,packages,architecture}.adoc, docs/modules/ROOT/pages/adrs/adr-006-remote-execution.adoc, packages/pi-workflow/{review-rules,approved-plan,workspace-audit,git-dir,glab-ambient,workflow-config,batch-order}.ts, packages/pi-workflow/test/*, packages/pi-workspace/{local,contract}.ts, packages/pi-ssh/{protocol.ts,ssh-workspace.ts}, packages/pi-ssh/test/transports.ts, tools/task.mjs.

Erratum (2026-10-26): findCommits keeps the scan whole and synchronous (scanCommits), handed a NamedFiles lookup; the async findCommits runs it once with a recording lookup to learn which files its own logic names, resolves them on the host, and runs it again. This keeps the order of -m, -F, stdin and substitution settlement exactly as it was. Erratum (2026-10-26): AuthoringDeps stays, as the required fact carrier with no filesystem default, instead of becoming a previous argument; pauseWrite reads the target through the workspace and hands it in, relative paths resolved against the session’s directory. The existing authoring tests already drove it with facts and are unchanged. Erratum (2026-10-26): the workspace’s home is optional to the relocation walk: when the host cannot say, a cd or ~ that needs it is unknown (refused) and a plain commit carries on to the staged-state read, which holds it; a thrown handler, which pi also blocks on, would have shown an extension error instead of the guard’s reason. Erratum (2026-10-26): readIndexed’s explicit truncation check is gone: output cut at cap + 1 bytes is over the cap by the length test after it (found equivalent by the mutation check). Erratum (2026-10-26): the CHANGELOG replaces 2c’s unreleased "commit refused over SSH" entry with this phase’s `Changed entry instead of adding a Fixed one: both ship in one release, and the refusal never did.

Edit this page · latest