Plan: a write tool’s path outside the workspace is refused by the gate, naming where to work
Closes: #943. Relates to: #901 (the gate’s table), #502 and #167 (real locations), #747 (the scratch root). Branch: fix/outside-write-deny. Status: Done (2026-11-29) — deterministic outside write gate row shipped on auto (outsideWrites: "deny")
Erratum (2026-11-29): Added XDG_RUNTIME_DIR to docs/modules/ROOT/pages/public-surface.adoc (Environment variables), required by tools/lib/test/public-surface.test.mjs.
Context
Reported from an argo2 session (2026-11-27, #943): helpers wrote into a sibling git worktree (../argo2-wt-api). One helper’s ten writes were allowed, the next helper’s first two were allowed, and its third was refused twice as "outside the workspace … not a temp directory". The operator’s ruling (2026-11-29): a deterministic deny, planned.
What the code does today:
-
GATE_STAGES(packages/pi-modes/authorize.ts:212) is the gate, fourteen rows run in order byrunGate(:868); the first row to answer decides.runGateasksheldForJudge(:247) once and skips every row markedguardedwhen it holds (the safety valve, a secret-shaped value, a credential-shaped file name). -
workspaceWriteAllow(:619, row "workspace write") allows a write or edit whose real location is inside the session’s directory and answers nothing for any other. No row answers a write that lands outside, so it reaches the judge. -
The auto rubric’s rule
R15(packages/pi-modes/judge.ts:377) says "Writes outside the workspace deny", with temp directories and the user’s runtime directory excepted. A sampled model applied it unevenly. -
The shipped
autoandmanualmodes allow the write and edit tools under/tmp/,/var/tmp/and/var/folders/by rule (packages/pi-modes/modes.json);plandenies every write and edit by rule ("": "deny");yolohas no rules.
Design
1. The mode key
ModeDescriptor (packages/pi-modes/config.ts) gains outsideWrites?: "deny" | "ask", declared after workspaceWrites (:136), listed after "workspaceWrites" in MODE_KEYS (:795) and checked in validateMode after the workspaceWrites line (:939) with check.oneOf(mode.outsideWrites, at('"outsideWrites"'), ["deny", "ask"] as const). Its doc comment says what Design 3 does. Omitted or "ask": the row answers nothing and the ask goes on as today.
packages/pi-modes/modes.json: "outsideWrites": "deny" on auto, after its "workspaceWrites": "allow" (:513), and on no other mode. plan needs none: its rule deny is row 3 and answers every write first. manual has no judge, so an outside write is asked of the human, who is the one to approve it. yolo allows everything by the operator’s choice.
mergeModesConfig (config.ts:1087) spreads an overlay’s mode over the shipped one, so the human’s overlay may say { "modes": [{ "id": "auto", "outsideWrites": "ask" }] } and have the judge decide as before.
2. Where a write lands
packages/pi-modes/remote-containment.ts gains the type and the remote half:
/** Where a write tool's path really lands: in the session's workspace, in a temp home, or outside both. */
export type WriteHome = "workspace" | "temp" | "outside";
export async function remoteWriteHome(
ws: Workspace, path: string, tempHomes: readonly string[],
): Promise<WriteHome | undefined>
remoteWriteHome awaits remoteRealLocation(ws, path) (exported, :73) and rootReal(ws) (private, :87, the root’s realpath, cached); either undefined returns undefined. Inside the root by insidePosix (:82): "workspace". Else, for each temp home, "temp" when the location is inside the home as named or inside realpathOf(ws, home) (private, :24; undefined for a home that is absent or a call that failed). Else "outside".
packages/pi-modes/authorize-paths.ts gains two constants at the top, one export, and its private helpers (localTempHomes() below; the local half may be a second, to keep each function short):
/** Beside the temp directory: the places the judge's rubric holds as scratch space (R15). POSIX paths. */
const POSIX_TEMP_HOMES = ["/var/tmp", "/var/folders", "/run/user"] as const;
/** The same on a remote host, whose own TMPDIR and XDG_RUNTIME_DIR this machine cannot read. */
const REMOTE_TEMP_HOMES = ["/tmp", ...POSIX_TEMP_HOMES] as const;
export async function writeHome(ctx: PathContext, raw: string): Promise<WriteHome | undefined>
-
Remote (
ctx.workspace?.isRemote === true):remoteTarget(ws, raw)(:139; undefined for a~path, the remote’s home) thenremoteWriteHome(ws, target, REMOTE_TEMP_HOMES). -
Local: undefined when
ctx.cwdis undefined.real = realLocation(toolPath(raw), cwd)(:197,:344); undefined (a dangling link, an unreadable component) returns undefined.isInside(real, realOrNamed(cwd))(:386,:286):"workspace". InsiderealOrNamed(home)for a home oflocalTempHomes():"temp". Else"outside". -
localTempHomes()(private):tempRoots(process.platform, tempDir())(:307,:281: the temp directory made real, and/tmpoff Windows); off Windows alsoPOSIX_TEMP_HOMESandprocess.env.XDG_RUNTIME_DIRwhen it is an absolute path.
A home is compared made real where it exists and as named where it does not, so macOS’s /tmp is /private/tmp, and /run/user/1000/x is a temp home’s on a machine with no /run/user. /run/user stands for any user’s runtime directory: the row decides no more than whether to stand back, and the judge reads the rest (R15 holds its systemd/ entries a deny).
3. The row
authorize.ts gains outsideWriteDeny, and GATE_STAGES gains { name: "outside write", guarded: false, run: outsideWriteDeny } between "workspace read" and "no judge": row 12 of fifteen. The guarded rows stay 7 to 11 (gate-stages.test.ts holds the guarded set to its five sampled rows); the workspace-read row answers no write (authorize.test.ts, "a write tool never qualifies as a read, whatever its path").
async function outsideWriteDeny({ deps, details, mode, line }: GateAsk): Promise<Settled | undefined>
In order, each returning undefined (the row answers nothing):
-
mode.outsideWrites !== "deny". -
!isWriteAsk(details)(authorize-paths.ts:84) ordetails.pathis not a string. -
line.verdict?.action === "allow"andawait writeRuleHolds(deps, details, line.verdict.pattern)(:462: the write’s real location answers to the rule). The rule-allow row, which is guarded, has answered such a write already unlessheldForJudgeheld it; held, a place a rule allows stays the judge’s (T4). -
(await writeHome(deps, details.path)) !== "outside".
Otherwise it returns:
{
verdict: { kind: "deny", source: "rule", pattern: OUTSIDE_WRITE, reason },
guard: "count",
event: { name: "gadhs_mode_rule.verdict",
details: { mode: deps.modeId, surface: tool, pattern: OUTSIDE_WRITE, action: "deny", ask: path } },
}
OUTSIDE_WRITE = "outside-write"; path = details.path.slice(0, 160); tool is "edit" when details.toolName or details.surface is "edit", else "write". The record is a rule verdict’s, so task verdicts (tools/task.mjs, which tallies gadhs_mode_rule.verdict by action) and refusalsOf (tools/judge-field/streams.mjs) count it with no change. guard: "count" as every deny (#168). The reason, built by a private outsideWriteReason(deps, details):
"<path>" is outside the workspace (<where>) and outside the temp directories: mode "<modeId>" refuses a write or edit there, every time (outsideWrites). <OUTSIDE_WRITE_HOMES>
<where> is deps.workspace.label for a remote workspace, else deps.cwd. Two constants beside HELPER_MODE_HINT (authorize.ts:122):
const OUTSIDE_WRITE_HOMES =
"Work inside the workspace. A second checkout of this repository goes in a git worktree inside it that git " +
"ignores: git worktree add .worktrees/<name> <branch>, with .worktrees/ listed in .git/info/exclude. A helper " +
"gets a worktree of its own when worktreeAgents, in the human's modes overlay, names its agent type. Drafts " +
"and notes go under .pi/scratch/, throwaway files in a temp directory.";
const HELPER_OUTSIDE_HINT = "Report this to your parent session rather than retry.";
HELPER_OUTSIDE_HINT follows, after a space, when details.forwarded === true.
4. What stays as it is
-
Rows 1 to 11 run first (T6). A rule deny (a credential path,
.git/hooks, pi’s own config) keeps its pattern and reason; the plan draft keepsplan-draft; a path the operator’shighConsequencelist names still prompts; a step approved with/pendingruns; a rule allow answers (/tmp/,/var/tmp/,/var/folders/*, the operator’s own; T3, T4). -
Inside the workspace the row answers nothing: an
askrule’s path (.pi/,.github/,.git/) and a credential-shaped name are the judge’s (T3). -
A credential-shaped name in a temp home is the judge’s, whose rubric says deny (R15; T3).
-
A location that cannot be known is the judge’s, as before (T3, T8).
-
A bash line that writes outside (a redirect,
cp,tee) is not a write tool’s ask: the judge decides it by R15. The rubric’s text is unchanged, so no battery run is due.
Scope
| File | Change |
|---|---|
|
Design 1 |
|
Design 2 |
|
Design 3; |
|
new: T1 to T7, T10 |
|
T8, and the four existing tests below |
|
T9 |
|
the eight existing tests below |
|
one case after |
|
regenerated (T11) |
|
§ The life of one action: |
|
the canonical page. § Per-mode config: an |
|
the "Reads outside the workspace" bullet: in auto a write or edit tool’s path outside never reaches the judge; the rule decides a shell line’s |
|
§ Safeguards, "Containment is decided on the remote": a link out is refused in auto |
|
the |
|
this plan, last under Active |
|
|
Existing tests that assert an outside write reaches the judge, changed to assert the refusal as toMatchObject({ kind: "deny", source: "rule", pattern: "outside-write" }). Their dangling-link, lost-link, ~-on-a-remote and in-workspace lines keep asserting the judge.
-
authorize.test.ts: "through a link that leads out of it, write and edit take no allow and reach the judge" (the twoout/config.jsonlines); "a pattern with no fixed root stands when the real location still matches it, and not through a link out" (theaway/xline); "on a remote workspace the remote’s real location decides, the same way (#498)" (theout/xline); "the rest of .pi, the root itself, and a scratch directory elsewhere are not it" (the home’s.pi/scratchline); "a raw~,@or file:// spelling of a file outside the workspace is not the workspace’s (#447)" (its three outside spellings); "path escapes do not qualify: the ask proceeds to the judge instead"; "a CI file OUTSIDE the workspace still goes to the judge"; "a symlink inside the repo pointing out does not qualify" (#167: the fourlinkpaths and the edit;danglinganddangling/xstay). Titles that say "the judge" are reworded. -
remote-containment.test.ts: "a write under the remote tree’s scratch root…" (.pi/scratch/out/x); "a write through a link out, to a credential-shaped name, to ~, or with the link lost goes to the judge" (out/n.ts,out/../n.ts,/etc/passwd); "a path spelled under the anchor…" (${ANCHOR}/../escape.ts); the real-remote suite’sout/n.tsandout/../n.ts.
Not edited: gate-stages.test.ts (its samples are the guarded rows'; every write in it is inside the workspace), gate-order.test.ts (it reads the page and the table), eval-rules.test.ts (a refusal is not an allow), judge-wiring-callback.test.ts (its link-out case asserts "not allowed").
Not in scope: a bash line’s writes outside the workspace; the rubric’s text (#953); re-measuring the model battery for the new corpus case, which is the operator’s.
Order of work
-
Branch
fix/outside-write-denyfrommain; commit this plan and its nav line, StatusIn progress. -
Design 1 to 3 with T1 to T10; then the existing tests; then the corpus case and
vitest run --root packages/pi-modes test/gate-characterisation.test.ts -u --coverage.enabled=false, and read the record’s diff against T11. -
The docs and the CHANGELOG entry; Status
Done (date) — …. -
node tools/task.mjs check, the wholepackages/pi-modessuite with coverage, push, MR, merge train.
Tests
T1 to T7 and T10 are in outside-write.test.ts: the shipped auto mode, a workspace <base>/repo and a sibling <base>/repo-wt-api made under ~/.cache (not the temp directory) and removed with removeDir; "the judge" is { kind: "defer", source: "judge", failure: "no model registry to run the judge" } with registry: undefined. POSIX-only homes are it.skipIf(process.platform === "win32"), links it.skipIf(!CAN_SYMLINK).
| Test | Asserts | Mutant it fails on |
|---|---|---|
T1 regression (#943) |
with a registry that throws on any access and one |
the row removed from |
T2 contract |
the reason holds the path, the workspace, |
|
T3 |
the judge decides: |
|
T4 |
an operator’s |
step 3 of Design 3 removed |
T5 |
the same sibling write: manual |
the key added to |
T6 |
order: a |
the row moved above "high consequence" |
T7 |
a link in the workspace that leads to the sibling: a write through it is refused |
|
T8 |
through |
the named comparison removed |
T9 contract |
|
the |
T10 property (fast-check) |
the row alone, |
a |
T11 |
|
— |
gate-order.test.ts, unchanged, holds the page’s fifteen lines to the table. Windows: pi-win runs the suite after the merge; T1, T2, T4 to T7 and T10 run there as written.
Risks
| Risk | Kind | Mitigation |
|---|---|---|
A place the human does want written (a dotfile, a second repository) is refused in auto, where the judge sometimes allowed it |
fail-closed |
the reason names the homes; a |
A temp home is wider than meant ( |
silent |
standing back allows nothing: the judge decides, as before this change (T3); the row has no allow (T10) |
A location that cannot be known (dangling link, |
fail-open, as today |
the judge decides it, as before (T3, T8) |
The link drops while a remote temp home is resolved, and a write under a linked |
fail-closed |
a plain |
A link in the tree that leads into another checkout is refused, not judged |
fail-closed |
intended: the write lands outside (T7); an allow rule on the target carves (T4) |
A helper sent to a sibling directory stops at its first write |
fail-closed |
intended, and sooner than the eleventh write; the reason sends it to its parent (T2), and each refusal counts toward the loop guard, so a retrying helper reaches the human (#168; T1’s count) |
The changed assertions in existing tests hide a regression |
silent |
each asserts the verdict’s kind, source and pattern, more than the throw it replaces; the unknown-location lines still assert the judge |
Tools that read the review log miss the refusals |
silent |
the record is a rule verdict’s, which they count (T2) |
On Windows the write tool’s |
fail-closed |
said in |
The battery’s store has no rows for the new corpus case |
silent |
the battery is the operator’s to run ( |
Delivery
The canonical page is docs/modules/ROOT/pages/modes.adoc (the outsideWrites row); the release note is one entry in CHANGELOG.adoc == Unreleased. The MR carries Closes #943. After the merge: the merge SHA on the issue, and a post on pi-feedback/feedback to pi-win (the Windows run) and in reply to the report.