pi-subagents-extend 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +133 -2
  3. package/bin/pi-subagents-extend-helper-linux-arm64 +0 -0
  4. package/bin/pi-subagents-extend-helper-linux-x64 +0 -0
  5. package/package.json +60 -4
  6. package/skills/pi-subagents-extend/SKILL.md +317 -0
  7. package/skills/pi-subagents-extend-context/SKILL.md +103 -0
  8. package/skills/pi-subagents-extend-probes/SKILL.md +151 -0
  9. package/src/bash/env.ts +108 -0
  10. package/src/bash/index.ts +27 -0
  11. package/src/bash/network.ts +97 -0
  12. package/src/bash/programs.ts +367 -0
  13. package/src/bash/rules.ts +247 -0
  14. package/src/bash/tokens.ts +169 -0
  15. package/src/bash/unreachable.ts +429 -0
  16. package/src/budgets.ts +89 -0
  17. package/src/child-context.ts +24 -0
  18. package/src/context-scope.ts +93 -0
  19. package/src/deliverable.ts +216 -0
  20. package/src/dependencies.ts +320 -0
  21. package/src/handoff-hint.ts +69 -0
  22. package/src/index.ts +181 -0
  23. package/src/main-context.ts +30 -0
  24. package/src/main-skills.ts +77 -0
  25. package/src/notices.ts +299 -0
  26. package/src/paths/index.ts +165 -0
  27. package/src/policy/glob.ts +146 -0
  28. package/src/policy/index.ts +36 -0
  29. package/src/policy/payload.ts +312 -0
  30. package/src/probe.ts +495 -0
  31. package/src/role-config.ts +434 -0
  32. package/src/sandbox/grants.ts +339 -0
  33. package/src/sandbox/helper.ts +144 -0
  34. package/src/sandbox/index.ts +12 -0
  35. package/src/sandbox/spec.ts +285 -0
  36. package/src/sandbox-note.ts +194 -0
  37. package/src/wiring/agent-start.ts +371 -0
  38. package/src/wiring/context.ts +216 -0
  39. package/src/wiring/misc.ts +55 -0
  40. package/src/wiring/session-start.ts +69 -0
  41. package/src/wiring/tool-call/anchor.ts +138 -0
  42. package/src/wiring/tool-call/bash-path.ts +0 -0
  43. package/src/wiring/tool-call/dispatch-preflight.ts +0 -0
  44. package/src/wiring/tool-call/judge.ts +579 -0
  45. package/src/wiring/tool-call/paths.ts +235 -0
  46. package/src/wiring/tool-result.ts +68 -0
  47. package/src/worktree-default.ts +27 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 shay
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,134 @@
1
- # Temporary Holding Version
1
+ # pi-subagents-extend
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A pi extension that confines a subagent to the files its task covers, and keeps main-agent-only
4
+ instructions out of a subagent's prompt.
5
+
6
+ When the parent delegates, it names the files the task covers. Inside the subagent every tool call
7
+ outside that scope is refused, so it cannot wander through the repository — and `bash`, the one tool
8
+ whose paths cannot be judged from its arguments, either stays disabled or runs under a kernel
9
+ sandbox that enforces the same boundary. Separately, a section of `AGENTS.md` fenced by
10
+ `<!-- main-agent-only:start -->` and `<!-- main-agent-only:end -->` never reaches a subagent, and
11
+ `.pi/main-only.md` is injected into the main session only. Either concern works without the other.
12
+
13
+ ## Requirements
14
+
15
+ - Linux 6.15 or later for the sandbox (Landlock ABI 7). Below that, `bash` stays disabled rather than
16
+ falling back to weaker rules; scope enforcement and context isolation are unaffected.
17
+ - pi-subagents, if you want the extension loaded per agent.
18
+
19
+ ## Install
20
+
21
+ The package ships on npm with the sandbox helper prebuilt for linux-x64 and linux-arm64, so an
22
+ install needs neither Rust nor a network beyond npm:
23
+
24
+ ```bash
25
+ pi install npm:pi-subagents-extend@0.1.0 # the exact version is the pin
26
+ ```
27
+
28
+ From a checkout instead (contributing, or a change you want to try before it ships):
29
+
30
+ ```bash
31
+ cd packages/pi-plugin
32
+ pnpm install
33
+ pnpm run build:helper # cargo build --release, then into bin/
34
+ pi -e "$PWD" # this session only, no settings change
35
+ ```
36
+
37
+ [How the extension gets loaded](docs/loading.md) covers every channel pi accepts, and which
38
+ parent-side features do nothing when only subagents load this package.
39
+
40
+ If the helper is missing or does not match the platform, a session with `bash` enabled says so when it
41
+ starts — it names the path the package carries and the `PI_SUBAGENTS_EXTEND_HELPER_PATH` override,
42
+ rather than waiting for the first `bash` call to fail.
43
+
44
+ A role is declared in the agent's own markdown: `subagentOnlyExtensions` names the md-role loader, and
45
+ the `extend` block carries its policy. `.pi/agents/` in this repository has ten working examples —
46
+ `coder-bash.md` is the shortest one to copy.
47
+
48
+ ```markdown
49
+ ---
50
+ name: worker
51
+ description: Implements a task within the given scope
52
+ systemPromptMode: append # replace drops the <active_agent> tag the loader reads
53
+ subagentOnlyExtensions: /path/to/pi-subagents-extend/.pi/agent-profiles/md-role.ts
54
+ extend:
55
+ permissions:
56
+ scope: ["src/auth/**"]
57
+ bash:
58
+ allow: ["ls", "rg", "npm test"]
59
+ ---
60
+ ```
61
+
62
+ ## Quick start
63
+
64
+ The parent passes the scope on each dispatch:
65
+
66
+ ```text
67
+ subagent(
68
+ agent: "worker",
69
+ task: "Fix the null pointer in login.ts",
70
+ extensionBindings: { "pi-subagents-extend/1": { "permissions": { "scope": ["src/auth/**"], "maxReads": 20 } } }
71
+ )
72
+ ```
73
+
74
+ The namespace key never holds policy fields directly: a dispatch wraps them in `permissions`. A dispatch's `extensionBindings` and the role md's `extend.permissions` block use the same fields under different spellings: the dispatch spells the write scope `writeScope`, the deny budgets `maxDenies` / `maxWriteDenies`, plus the dispatch-only `maxReads`; the role md spells the write scope `write` and the budgets `budgets.deny` / `budgets.writeDeny`. `worktree` is a sibling of `permissions` in a dispatch, never a field inside it; the role md also keeps `worktree` as a separate block. Those spellings are not interchangeable. `setup` and `output` in a dispatch are ignored with a warning; put `setup.preExec` and `output.deliverable` in the role md.
75
+
76
+ That is the whole idea: `scope` is what the subagent may read, and everything else — writes, shell,
77
+ network — is off until named. A reviewer, for example, reads the repository and writes exactly one
78
+ report file:
79
+
80
+ ```json
81
+ { "pi-subagents-extend/1": { "permissions": { "scope": ["."], "readOnly": true, "writeScope": [".pi/review-reports"] } } }
82
+ ```
83
+
84
+ That report path reaches the `write` tool but not `bash`: the helper mounts a private tmpfs over
85
+ `/tmp`, so a spec that grants a path below it is refused with exit 77 — see
86
+ [sandbox](docs/sandbox.md#files).
87
+
88
+ Recommended shapes for a reviewer, a coder in a worktree, a researcher and a build runner are in
89
+ [usage](docs/usage.md#recommended-scenarios), with the full field reference beside them.
90
+
91
+ ## Skills
92
+
93
+ The extension ships three skills, offered to the agent that loads it:
94
+
95
+ | Skill | For |
96
+ | --- | --- |
97
+ | [`pi-subagents-extend`](packages/pi-plugin/skills/pi-subagents-extend/SKILL.md) | the parent agent: how to write a dispatch, what each field buys, and how to read a refusal |
98
+ | [`pi-subagents-extend-probes`](packages/pi-plugin/skills/pi-subagents-extend-probes/SKILL.md) | a subagent that was refused or warned: what is counted, what is not, and the way out |
99
+ | [`pi-subagents-extend-context`](packages/pi-plugin/skills/pi-subagents-extend-context/SKILL.md) | the context-isolation markers and the two entry points that apply them |
100
+
101
+ ## Documentation
102
+
103
+ | Document | Contents |
104
+ | --- | --- |
105
+ | [docs/usage.md](docs/usage.md) | the field reference, recommended scenarios, role defaults, and running without a parent agent |
106
+ | [docs/architecture.md](docs/architecture.md) | the three entry points, how the code is divided, configuration sources, context isolation, role defaults |
107
+ | [docs/architecture-refusals.md](docs/architecture-refusals.md) | the refusal model, the three write layers, probes, the denial budget, the hard stop |
108
+ | [docs/sandbox.md](docs/sandbox.md) | what the helper does: Landlock, the namespaces, capabilities, and why offline means "no route out" |
109
+ | [docs/worktrees.md](docs/worktrees.md) | the worktree setup hook, the `node_modules` link, and what a worktree run delivers |
110
+ | [docs/testing.md](docs/testing.md) | the offline suites, the mock model, and the four CI legs |
111
+
112
+ ## Development
113
+
114
+ ```bash
115
+ cd packages/pi-plugin && pnpm install && pnpm run typecheck && pnpm test
116
+ cd ../helper && cargo test
117
+ ```
118
+
119
+ `pnpm test` runs a lint pass and four offline suites: 163 scope cases, 10 context cases, 6 sessions
120
+ against a real pi process driven by a scripted mock model, and 5 end-to-end cases against the real
121
+ `pi-hashline-edit-pro` — no model, no network beyond localhost. See [testing](docs/testing.md).
122
+
123
+ ## Boundaries
124
+
125
+ The scope is a file-level boundary, not a content-level one: the sandbox prevents out-of-scope
126
+ writes and network access, not reads, because running a test suite needs to read the whole tree.
127
+
128
+ This is not a sandbox against malicious code. A subagent inherits the parent's environment and
129
+ credentials, so it guards against a subagent overstepping its task, not against an adversary;
130
+ genuinely untrusted input needs container-level isolation. One concrete limit follows from Landlock:
131
+ in a **plain checkout** the sandbox is granted `.git` whole, because the index and `HEAD` lock files
132
+ live there and a directory cannot be granted without its `hooks/` and `config` — so a subagent
133
+ working directly in a checkout can plant a hook the parent would later run. In a linked worktree it
134
+ cannot, which is why delegated work runs in one.
package/package.json CHANGED
@@ -1,6 +1,62 @@
1
1
  {
2
2
  "name": "pi-subagents-extend",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Everything a pi session needs around subagents: scope what a delegated subagent may touch, and keep main-agent-only context out of child prompts",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/yxlimo/pi-subagents-extend.git",
10
+ "directory": "packages/pi-plugin"
11
+ },
12
+ "homepage": "https://github.com/yxlimo/pi-subagents-extend#readme",
13
+ "bugs": "https://github.com/yxlimo/pi-subagents-extend/issues",
14
+ "keywords": [
15
+ "pi",
16
+ "pi-extension",
17
+ "subagents",
18
+ "sandbox",
19
+ "allowlist",
20
+ "agents-md"
21
+ ],
22
+ "pi": {
23
+ "extensions": [
24
+ "src/index.ts",
25
+ "src/main-context.ts"
26
+ ]
27
+ },
28
+ "engines": {
29
+ "node": ">=22.19.0"
30
+ },
31
+ "scripts": {
32
+ "typecheck": "tsc -p tsconfig.json --noEmit",
33
+ "build": "tsc -p tsconfig.json",
34
+ "build:helper": "cargo build --release --manifest-path ../helper/Cargo.toml --locked && mkdir -p bin && cp ../helper/target/release/pi-subagents-extend-helper bin/.helper.new && mv -f bin/.helper.new \"bin/pi-subagents-extend-helper-$(node -p 'process.platform + \"-\" + process.arch')\"",
35
+ "pretest": "pnpm run build:helper",
36
+ "test": "pnpm run lint && node ../../scripts/run-tests.mjs",
37
+ "test:mock": "node test/mock-session.test.ts",
38
+ "test:llm": "node test/e2e.test.ts",
39
+ "prepublishOnly": "pnpm run build",
40
+ "prepack": "cp -f ../../README.md ../../LICENSE .",
41
+ "lint": "eslint ."
42
+ },
43
+ "devDependencies": {
44
+ "@earendil-works/pi-coding-agent": "^1.4.0",
45
+ "@eslint/js": "^10.0.1",
46
+ "@types/node": "^24.0.0",
47
+ "eslint": "^9.39.4",
48
+ "eslint-plugin-unicorn": "^79.0.0",
49
+ "pi-hashline-edit-pro": "7.2.0",
50
+ "pi-subagents": "0.89.1",
51
+ "typescript": "^5.9.0",
52
+ "typescript-eslint": "^8.72.2"
53
+ },
54
+ "files": [
55
+ "src",
56
+ "skills",
57
+ "bin"
58
+ ],
59
+ "dependencies": {
60
+ "yaml": "^2.9.1"
61
+ }
62
+ }
@@ -0,0 +1,317 @@
1
+ ---
2
+ name: pi-subagents-extend
3
+ description: Use when delegating a pi-subagents subagent that must be confined to a file and command scope, and when giving each code area its own role. Declare the role once in its agent definition — `.pi/agents/<role>.md`, an `extend:` block holding the write grant, the command allowlist, the deny budget and the deliverable — then dispatch with `agent`, `model` and `task` only; a per-dispatch `extensionBindings` payload narrows one task further. Every out-of-scope tool_call is refused, allowlisted commands run in a Landlock sandbox (no network by default, writes confined to the grants), and a refusal names the file that decided it. Use it to keep a subagent from sweeping the whole repository, to allow typecheck/test, or to install dependencies in a worktree. Probe criteria and stopping are covered by pi-subagents-extend-probes, and stripping the main-agent-only sections of AGENTS.md by pi-subagents-extend-context.
4
+ ---
5
+
6
+ # pi-subagents-extend: confine a subagent to the task scope
7
+
8
+ When delegating a subagent, pass the scope of this task through `extensionBindings` — or rely on the role md's `extend` block when the role already declares the default there (see [Area roles](#area-roles-one-md-per-code-area-policy-inside-the-md)). In the subagent's session, every tool_call outside that scope is refused (fail closed): this keeps the subagent from wandering through the repository aimlessly.
9
+
10
+ ## When to use it
11
+
12
+ | Situation | Which field | See |
13
+ | --- | --- | --- |
14
+ | The subagent should only read/write the few files of this task | `scope`, plus `readOnly` for a read-only task | [The scope field](#the-scope-field) |
15
+ | The subagent should read the whole task scope but write into one directory (a review allowed to write only its report) | `writeScope` | [The scope field](#the-scope-field) |
16
+ | A read-only reviewer still needs to write its report somewhere outside the tree | `readOnly` + an absolute `writeScope` entry + `write` in `allowTools` | [The scope field](#the-scope-field) |
17
+ | The subagent has to run typecheck, tests or a build itself | `bash.allow` plus `writable: true` for commands whose program can write (`pnpm`/`npm`/`yarn`/`bun` with `run`/`test`/`exec` are refused at the gate without it; it also lets artefacts land on disk) | [The bash field](#the-bash-field) |
18
+ | The subagent runs in `worktree: true` and has to install dependencies | The project commits `.pi/worktree-setup.sh`, together with `writablePaths` / `network` | [bash in a worktree](#bash-in-a-worktree) |
19
+ | You do not want to hand-write the scope on every dispatch (it gets forgotten when you are busy) | the role md's `extend` block | [Area roles](#area-roles-one-md-per-code-area-policy-inside-the-md) |
20
+ | The subagent starts idling or going in circles | The other half of this package: probes | skill `pi-subagents-extend-probes` |
21
+ | AGENTS.md mixes in rules only the main agent should see | The other half of this package: context isolation | skill `pi-subagents-extend-context` |
22
+ | A heavy reference skill should load in a subagent only | mark it `subagent-only: true` in its own frontmatter — no bindings field; `disable-model-invocation` would hide it from the child too | [Subagent-only skills](../../../docs/usage.md#subagent-only-skills) |
23
+
24
+ The criteria and the stop (read-only idling, going in circles, finishing early, writing litter, colliding with permissions) are in the other skill, `pi-subagents-extend-probes`.
25
+
26
+ Context isolation (the sections fenced by `<!-- main-agent-only:start -->` in `AGENTS.md`, and `.pi/main-only.md`) is the other half of the same package: see skill `pi-subagents-extend-context`. It is decided by the entry point declared in the agent definition and has nothing to do with bindings — no field needs to be passed for it when delegating.
27
+
28
+ The delegation rules themselves (how to write the brief, how to wait, one cwd, one writer) follow the **skill shipped with pi-subagents**; this skill only adds the layer of "how to bind the scope and the command grants to a single delegation".
29
+
30
+ ## Prerequisites
31
+
32
+ The delegated agent must be able to load this extension. Choose one:
33
+
34
+ - declare `subagentOnlyExtensions: /path/to/pi-subagents-extend/packages/pi-plugin/src/index.ts` in the agent definition's frontmatter (works for foreground and background subagents; recommended);
35
+ - the extension is installed globally (`~/.pi/agent/extensions/pi-subagents-extend`) and this delegation sets `async: true` (only background subagents use ambient discovery).
36
+
37
+ Without the extension loaded, passing bindings does nothing: no interception happens.
38
+
39
+ A role md loads the extension through the md-role loader (`.pi/agent-profiles/md-role.ts`, which passes no profile name), so listing `…/src/index.ts` beside it is not needed (see [Area roles](#area-roles-one-md-per-code-area-policy-inside-the-md)).
40
+ If the project uses the `<!-- main-agent-only:start -->` marker, add `, …/src/child-context.ts` to the same line, otherwise the subagent reads the main-agent-only sections (see skill `pi-subagents-extend-context`).
41
+
42
+ Allowlisted commands run under the sandbox helper (the Rust binary in `packages/helper`): the file scope is enforced by Landlock, the network is off by default (all outbound TCP/UDP is cut and DNS resolution fails, but loopback works inside the sandbox, so a local server can be started and reached), and processes and capabilities are isolated as well (the host processes are invisible, the command has no capability at all). The host needs Landlock ABI 7 (Linux 6.15+); when the helper is missing or the kernel is too old, bash stays disabled (the remaining features are unaffected).
43
+
44
+ ## Call template
45
+
46
+ **When the role md already carries the default, pass only `agent`, `model` and `task`:**
47
+
48
+ ```text
49
+ subagent(agent: "coder", model: "buddy/coder-d", task: "<task description>")
50
+ ```
51
+
52
+ Hand-written `extensionBindings` narrows one task further than its role md, and is also the only way to bind a role whose md does not declare an `extend` block. It fails in two ways when used as the default:
53
+
54
+ - **A fan-out cannot carry it.** `extensionBindings` is a top-level parameter of `subagent` only. A `tasks` item accepts `agent`, `task`, `cwd` and `model` and nothing else, and rejects any other field; the same holds for a `chain` step and for `args.tasks` of a `workflow: "parallel"` script. There is no place in a parallel dispatch to declare a scope.
55
+ - **It is forgotten.** A dispatch that carries neither bindings nor a role md with an `extend` block is not constrained at all: the extension is loaded and inactive.
56
+
57
+ Declaring the default once in the role md removes both — see [Area roles](#area-roles-one-md-per-code-area-policy-inside-the-md).
58
+ Reading or writing files, no commands needed:
59
+
60
+ ```text
61
+ subagent(
62
+ agent: "<agent-name>",
63
+ task: "<task description>",
64
+ extensionBindings: { "pi-subagents-extend/1": { "permissions": { "scope": ["<file/directory glob>", "..."] } } }
65
+ )
66
+ ```
67
+
68
+ The subagent has to run tests or typecheck itself:
69
+
70
+ ```text
71
+ subagent(
72
+ agent: "<agent-name>",
73
+ task: "<task description>; run typecheck afterwards and report the result",
74
+ extensionBindings: {
75
+ "pi-subagents-extend/1": {
76
+ "permissions": {
77
+ "scope": ["<file/directory glob>"],
78
+ "bash": { "allow": ["pnpm run typecheck", "pnpm test"], "writable": true }
79
+ }
80
+ }
81
+ }
82
+ )
83
+ ```
84
+
85
+ The namespace is fixed at `pi-subagents-extend/1`. Pass it on every delegation, and narrow the scope to the files and directories this task actually touches; do not default to the whole repository.
86
+
87
+ A dispatch's `extensionBindings` and the role md's `extend` block use the same fields under different spellings: the dispatch nests them under `permissions` and spells the write scope `writeScope`, the deny budgets `maxDenies` / `maxWriteDenies`, plus the dispatch-only `maxReads`; the role md spells the write scope `write` and the budgets `budgets.deny` / `budgets.writeDeny`, and keeps `worktree` as its own block. Those spellings are not interchangeable. `setup` and `output` in a dispatch are ignored with a warning; put `setup.preExec` and `output.deliverable` in the role md.
88
+ The subagent runs in a worktree (`worktree: true`) and has to install dependencies or run verification:
89
+
90
+ ```text
91
+ subagent(
92
+ agent: "<agent-name>",
93
+ task: "<task description>",
94
+ options: { worktree: true },
95
+ extensionBindings: {
96
+ "pi-subagents-extend/1": {
97
+ "permissions": {
98
+ "scope": ["src/auth/**"],
99
+ "bash": {
100
+ "allow": ["pnpm run typecheck", "pnpm test"],
101
+ "writable": true,
102
+ "writablePaths": ["~/.local/share/pnpm/store"]
103
+ }
104
+ }
105
+ }
106
+ }
107
+ )
108
+ ```
109
+
110
+ `worktree` is a sibling of `permissions` in a dispatch, never a field inside it; the role md also keeps `worktree` as a separate block. A dispatch can therefore carry `{"pi-subagents-extend/1": {"permissions": {...}, "worktree": true}}`.
111
+
112
+ ## The `scope` field
113
+
114
+ | Field | Type | Meaning |
115
+ | --- | --- | --- |
116
+ | `scope` | `string[]` | Required. Globs describing what the subagent may read; an empty array refuses all file access |
117
+ | `writeScope` | `string[]` | Optional. Globs describing what `edit`/`write` may touch. Omitted means the same as `scope`; `[]` means no writes at all while reads keep working. Relative entries are bound to the task tree; an **absolute** entry grants that path wherever it is |
118
+ | `readOnly` | `boolean` | Defaults to `false`; when `true`, `edit`/`write` are refused — the same as `writeScope: []` — which suits read-only research and review agents. An explicit `writeScope` beside it, in the same layer, wins, so a reviewer can be given exactly one file to write. In a **dispatch** it also beats the role md's `writeScope`: read-only means read-only unless this dispatch names a directory |
119
+ | `maxReads` | `number` | Optional. Upper bound on successful `read` calls; once it is exceeded, every read is refused |
120
+ | `maxWriteDenies` | `number` | Optional. Write refusals have their own budget: defaults to 2, `0` disables. Kept apart from `maxDenies` because repeatedly trying to write is a usage mistake, answered by delivering a report |
121
+ | `allowTools` | `string[]` | Optional. Extra tool names to allow; executor-type tools such as `bash` cannot be allowed this way. It cannot widen a path tool: file tools are judged by the scope first, so listing a write tool here does not free it from `writeScope`. A dispatch naming it replaces the role md's list; an omitted field is filled from the role md |
122
+ | `tools` | `object` | Optional. Per-tool overrides for tools the defaults do not cover: `{ "my_edit": { "access": "write", "path": "file" } }`. `path` names the argument that carries the target; `false` means the tool has no path argument, and a write tool declared that way is always refused. Omit `path` and the argument name is read from the tool's own schema. `access` is `read` or `write`; a tool that touches a **second** file (`copy`/`move`) takes `transferSource: "read"\|"write"` as well — `copy`'s source is only read, `move`'s is written to, and an undeclared transfer tool is judged as a write on both sides |
123
+ | `worktree` | `boolean` | Optional, defaults to `false`. `true` in the role md's `extend.worktree` block (or in a dispatch's own bindings) fills `worktree: true` on `subagent` and nested `subagent_launch` calls for that role when the caller did not supply it — `??=` semantics, so `null` / `undefined` are filled while an explicit `false` (or any other value) on the call wins. It only fills a value: no refusal, no deny-budget charge. An `agent` with no strategy is left alone (fail open). The role md is committed with the project, so a worktree copy carries it; a dispatch-only value does not survive a worktree by itself either way. |
124
+
125
+ Glob rules: `**` matches any number of levels (including zero), `*` matches characters within a single level, `?` matches a single character.
126
+
127
+ For a directory, `dir`, `dir/` and `dir/**` are equivalent: all three cover the directory itself and its contents at any depth; `dir/*` covers only the direct children. So write the scope as directories, not as a list of files — listing files is easy to get wrong, and files added to the directory later are refused too. A path directly under `dir/**`, such as `dir/a.ts`, is inside it.
128
+
129
+ Reads and writes are authorised by three separate lists, and this is deliberate:
130
+
131
+ | Layer | Field | Governs |
132
+ | --- | --- | --- |
133
+ | read | `scope` | `read`, `grep`, `find`, `ls` |
134
+ | write, tools | `writeScope` | `edit`, `write` |
135
+ | write, processes | `bash.writablePaths` (with `bash.writable`) | the Landlock rules of bash subprocesses |
136
+
137
+ So "read the whole repository, write only `.pi/reviews`" is `scope: ["."]`, `writeScope: [".pi/reviews"]`.
138
+
139
+ `"."` and `"./x"` are normalised, so they mean the whole tree and `x`; they are not matched literally.
140
+
141
+ Targets are judged by their **real** path, not the text of the call. A symlink inside the scope that points outside it is refused, and so is any target whose real path leaves the working tree — including under `scope: ["."]`. A scope is written relative to the project, and the project is that tree, so `src/link` pointing elsewhere is not "inside `src/`" just because the string says so. This matters for `edit`/`write`, which run in the parent process where no sandbox backs them up.
142
+
143
+ Keep `.git` out of `writeScope` — the sandbox is given the parts of it git has to write on its own, and putting `.git` in the write scope lets a subagent hand-edit repository metadata (`hooks`, `config`) that the parent later executes.
144
+
145
+ ## The `bash` field
146
+
147
+ | Field | Type | Meaning |
148
+ | --- | --- | --- |
149
+ | `bash` | `object` | Optional. Without it bash is forbidden entirely; an allowlist with no entries also means forbidden |
150
+ | `bash.allow` | `string[]` | The allowlist column. It answers only *may this command run here*; whether the command reads or writes is judged from the command itself — a program table plus argument predicates (git subcommands and their flags, `sed -i`, `find -delete`/`-exec`/`-fprint*`, package-manager subcommands with `config`/`store`/`version` judged by their second token, interpreters, `xargs`). A literal is matched token by token as a prefix after splitting on whitespace; an entry starting with `re:` is matched as a regex against the whole string |
151
+ | `bash.read` / `bash.write` | `string[]` | Legacy columns, kept with their exact old meaning: a `read` entry grants read-only, a `write` entry grants write and counts as "write activity". The matching rules are the same. A config that spells them gets one migration warning per source; in an agent-md `permissions.bash` block they are rejected — that block takes only `allow`, `deny`, `writable` and `network` |
152
+ | `bash.writable` | `boolean` | Defaults to `false`, the sandbox root is read-only; when `true`, the paths the **write scope** covers, git's own directories and `node_modules` are writable inside the sandbox (test artefacts, snapshots). Only a scope entry that covers a whole subtree (`dir`, `dir/**`) opens that directory; a file-level entry (`src/auth/*.ts`) opens just the files it matches today, so a brand-new file under it is not writable through bash — write those with the `write` tool. In a linked worktree, git's own grant is the worktree's gitdir plus the common directory's `objects`, `refs` and `logs`; `hooks/` and `config` are deliberately outside it, because planting a hook there would later run it as the parent agent. In a plain checkout the index and `HEAD` lock files sit in `.git` itself, so there `.git` is granted whole and `hooks`/`config` stay writable — a Landlock limit, not a choice. The working directory is not granted wholesale, so a subagent allowed to read `lib/` but not write it cannot route around that with a shell redirect; anything else it needs goes in `writablePaths` |
153
+ | `bash.writablePaths` | `string[]` | Additional paths granted write access (absolute paths, starting with `~`, or relative to the cwd). Put the global directories the toolchain has to write here, such as the pnpm store |
154
+ | `bash.scratchPaths` | `false \| string[]` | Adjusts the build/test artefact directories, which are otherwise derived from the manifests the project has. `false` disables detection, an array appends explicit directories (created if missing) |
155
+ | `bash.env` | `object` | Environment variables injected into the sandbox (written into the spec's `env`). `XDG_RUNTIME_DIR` (the sandbox's private /tmp, which pnpm's lock directory relies on) and, in the worktree case, `MISE_STATE_DIR` are injected by default. A `TMPDIR` set here is the directory the helper shadows with its private tmpfs |
156
+ | `bash.gitExcludes` | `boolean` | Defaults to `true`. Injects global exclude rules into git inside the sandbox (`node_modules/`, `.pnpm-store/`), so that `git add -A` does not pull the dependency directories into the index |
157
+ | `bash.deny` | `string[]` | Blacklist: a matched command is **always refused, and the caller's allowlist cannot widen it**. Usually written in the role md's `bash.deny` to enforce the project skeleton (`git stash`, `git reset`) |
158
+ | `bash.dependencyGate` | `boolean \| string[]` | Defaults to `true`. Every directory in the tree (two levels down) that has a package-manager lockfile (pnpm, npm, yarn, bun) must also have `node_modules/.bin`. When one does not, build and package-manager commands are refused with the directory named; read-only commands and `git` run anyway, because `git` needs no dependencies and committing is what keeps a worktree from being discarded. Wrappers are seen through (`sh -c "pnpm test"`, `env pnpm test`), but a script file (`node build.mjs`) is opaque — the gate reduces a confusing failure, it does not make one impossible. It reports a missing install, not an install that has drifted from the lockfile. Not a permission the parent can widen: the install has to finish (`worktreeSetupHook`) or the store has to be writable. An array lists directory prefixes to skip, for a lockfile nobody installs (a `fixtures/` sample, a vendored example): a plain path covers that directory and everything below it, resolved against the working directory, and the match stops at a path boundary (`fixtures` does not cover `fixtures-old`); globs are not accepted. `[]` skips nothing, the same as `true`. `false` turns it off for a task that never builds |
159
+ | `bash.cpuStallSeconds` | `number \| false` | Off by default. Armed, the helper kills the command tree after that many seconds with **no** CPU use, prints the reason and exits `79`. It separates a deadlock from a slow build — a compiling tree gains CPU every sample, a tree waiting on a lock or an orphaned child gains none — which a wall-clock timeout cannot do. Costs: a command that deliberately waits (`sleep`, a backoff loop) is killed too, and the helper cannot see output (stdio is inherited, not piped), so a quiet-but-healthy gap longer than the number is a false positive. Choose a value well above the longest gap between CPU bursts; the result carries a `[sandbox]` note saying a retry stalls the same way. |
160
+
161
+ For a bash command outside the allowlist, the refusal text carries the detected pnpm store path — the model can see it and report it together with "what the parent agent wants to write". The extension only detects it; it does not allow it automatically.
162
+
163
+ **Build and test artefacts are writable where the project says they belong.** `bash.writable` grants the write scope plus the artefact directories of the toolchains the project actually uses. The extension decides that from manifest files in the repository root and up to two levels down (so `packages/web/package.json` counts): `package.json` means Node, `Cargo.toml` Rust, `go.mod` Go, `pyproject.toml` Python, `pom.xml` or `build.gradle` Java, `mix.exs` Elixir, `Gemfile` Ruby, `Package.swift` Swift, `build.zig` Zig, `*.csproj` .NET, and so on. Each toolchain carries its own conventional output directories, resolved next to its manifest.
164
+
165
+ A directory that already exists is granted. A toolchain's main output (`target` for Rust, `__pycache__` for Python, `_build` for Elixir) is also created when missing, because the first `cargo test` in a fresh checkout would otherwise fail. The rest — `dist`, `.next`, `coverage` — are granted only if the project already has them; the extension does not invent directories. Build caches that live outside the repository (`go`'s, `mypy`'s) go to a persistent `~/.cache` directory through `XDG_CACHE_HOME`; the sandbox's private `/tmp` would make every run recompile.
166
+
167
+ `bash.scratchPaths` adjusts the list: `false` turns detection off entirely, an array appends explicit directories (created if missing). Put it in the role md when a role always runs the same build, so no dispatch has to repeat it.
168
+
169
+ Everything else is still refused. A plain `echo x > tmp.txt` in the working directory, or a tool writing into a directory the project does not have, fails with `Permission denied` — deliberately, because otherwise `lib/a > lib/b` would walk straight around a directory the subagent may read but not write. Fix it by naming the location:
170
+
171
+ - the directory goes in `writeScope` if the subagent may also write it by hand (a review writing its report);
172
+ - it goes in `bash.writablePaths` if only the toolchain needs it;
173
+ - or point the tool at a temporary directory: the sandbox already makes it writable **and** mounts an empty tmpfs over it, so what lands there is scratch, not state. That is `/tmp` and also the command's own temp directory — the host's `TMPDIR`, or the one pinned through `bash.env`: `"env": { "COVERAGE_DIR": "/tmp/coverage" }`.
174
+
175
+ A grant **inside** one of those mount points is a mistake, and it is dropped rather than obeyed: the private tmpfs is mounted over `/tmp` (and over a `bash.env.TMPDIR`), so a directory under it does not exist in the child as it does on the host, and every command that needs it would fail with "spec path does not exist". The extension drops such a grant when it builds the sandbox spec and prints one warning naming the path, instead of writing a spec the helper will refuse. The helper refuses it too (exit `77`) if one reaches it anyway. The two ways out are the ones above: let the tool write under the tmpfs (it is already writable, and the contents are scratch), or move the directory out from under `/tmp`, for instance `bash.env.TMPDIR: "/var/tmp/pi"`.
176
+
177
+ Entries come in two forms:
178
+
179
+ - Literal: split on whitespace and match token by token as a prefix. `"pnpm test"` allows `pnpm test -t foo`, but not `pnpm publish`.
180
+ - `re:` prefix: a regex, anchored against the whole string. `"re: ^pnpm -r (test|build)$"`. Regexes are not guessed automatically — command arguments often contain `*` and `.`, and guessing wrong in that direction grants more than was intended.
181
+
182
+ ## Area roles: one md per code area, policy inside the md
183
+
184
+ Per-role defaults live in the role's own agent definition, `.pi/agents/<role>.md`, under an
185
+ `extend:` block — not in a bindings file. The block carries:
186
+
187
+ - `extend.permissions` — the same fields as the two tables above, in the role md's spellings:
188
+ `scope`, `write` (the write scope), `readOnly`, `allowTools`, `budgets` (`budgets.deny` /
189
+ `budgets.writeDeny`), and `bash` with `allow`, `deny`, `writable` and `network`;
190
+ - `extend.setup.preExec` — commands run **unsandboxed** at session start;
191
+ - `extend.output.deliverable` — what the role must hand back;
192
+ - `extend.worktree` — the dispatch default for `worktree`.
193
+
194
+ One file holds the area's write grant, its allowlist, its budget and the one command that decides
195
+ the work, and a dispatch to that role needs only `agent`, `model` and `task`; a per-dispatch
196
+ `extensionBindings` payload narrows one task further on top of that default.
197
+
198
+ The mechanism: the extension is loaded by a loader that passes **no** profile name
199
+ (`.pi/agent-profiles/md-role.ts`, which is just `export default activate`), declared in the role
200
+ md's `subagentOnlyExtensions`. Nothing reads a bindings file; the reader recomputes the policy
201
+ from the merged fields, so a md that supplies `scope` produces an active policy with no other
202
+ A dispatch that carries neither bindings nor a role md with an `extend`
203
+ block leaves the extension loaded and inactive — fail open. A md that **exists and declares
204
+ an `extend` block but cannot be applied** — unreadable file, invalid YAML, `extend` not an
205
+ object, unknown block names, bad field values — fails **closed**: every tool call is refused
206
+ and the refusal names the md path and the problems, so a typo cannot silently drop a grant.
207
+
208
+ What bites, in the order it bit here:
209
+
210
+ - **`systemPromptMode: append`, not `replace`.** `replace` drops the `<active_agent>` tag; identity
211
+ then falls back to matching the md body against the prompt, and when identity is not resolved the
212
+ policy stays inactive — **fail open**, not fail closed. A role md whose body is empty can never
213
+ be identified.
214
+ - **`write` entries are relative to the session cwd, which is the worktree root.** Write
215
+ `packages/pi-plugin/src/paths`, never `src/paths`: the short spelling matches nothing, so
216
+ *every* write is refused and it reads exactly like the area boundary working correctly.
217
+ - **The grant is an exact file list.** A new file is refused even inside the area. Grant a directory
218
+ (e.g. `packages/pi-plugin/test`) for areas that legitimately add files.
219
+ - **A fresh worktree has no sandbox helper.** `packages/pi-plugin/bin` is gitignored and
220
+ `mise run setup:helper` cannot create it once the sandbox is up, so a gate that needs the helper
221
+ dies before its first case. `extend.setup.preExec` runs **unsandboxed** at session start; use it
222
+ to copy the prebuilt helper in and to create the scratch directory:
223
+ `mkdir -p packages/pi-plugin/bin .test-state && cp <main-checkout>/packages/pi-plugin/bin/… packages/pi-plugin/bin/`.
224
+ - **Local green does not prove sandbox green.** Anything that falls back to a working path outside
225
+ the sandbox (the harness falls back to a package-local scratch directory when `.test-state` is
226
+ absent) passes locally and fails in a child. Verify a role by dispatching to it and reading what
227
+ it measured, not by running the gate yourself.
228
+ - `mise run test:all` is deliberately not in any area's allowlist: the parent runs the full set
229
+ before merging. Give each area the narrowest task that covers it, and state its case count in the
230
+ md — a count that moves is a case copied or lost.
231
+
232
+ `setup` and `output` are role contract, not dispatch input: a dispatch that supplies them is
233
+ warned about and ignored, because a caller must not be able to install a dependency or move a
234
+ deliverable.
235
+
236
+ ## bash in a worktree
237
+
238
+ Two things need extra handling when the subagent runs in a pi-subagents managed worktree (`worktree: true`):
239
+
240
+ - **Installing dependencies is not the extension's job**: the worktree's `node_modules` is a symlink to the main checkout, so `pnpm install` follows the link and installs into the main checkout, and the link itself is not replaced, which leaves the hierarchy wrong. Leave this to pi-subagents' `worktreeSetupHook` (this repository's `packages/worktree-setup`), which handles it before the subagent starts; see the next section. The extension only manages write access inside the sandbox.
241
+ - **Both pnpm and mise write outside the sandbox**: pnpm's lock directory lives in `XDG_RUNTIME_DIR` (`/run/user/<uid>`), and mise has to register the worktree's `mise.toml` in `~/.local/state/mise/trusted-configs`. The extension points both at the sandbox's private /tmp by default, so those two failures no longer need handling; places that genuinely have to be writable and persistent, such as the pnpm store, are given explicitly with `bash.writablePaths`. A host whose `TMPDIR` is somewhere other than `/tmp` gets a private tmpfs over that directory as well, so temp files stay inside the sandbox either way.
242
+ - **Which store pnpm uses inside the sandbox depends on write access**: when the global store (`~/.local/share/pnpm/store`) is not writable under the read-only root, pnpm detects that and falls back to `<project>/node_modules/.pnpm-store`; list the store directory in `bash.writablePaths` and `pnpm store path` still points at the global store (measured). Both of them run tests — the in-project store lands under `node_modules/`, which is writable anyway. The extension's git global exclude rules keep it out of the index, but the project's own `.gitignore` should ignore `node_modules/` as well.
243
+ - **Installing dependencies inside the sandbox needs the network** (`"network": true`): whichever store is used, installing a new package has to reach the registry. With the network off by default, `pnpm install` is bound to fail.
244
+
245
+ ## Worktree dependency installation (worktree hook)
246
+
247
+ pi-subagents runs the `worktreeSetupHook` once after each worktree is created and before the subagent starts, with the cwd set to the new worktree root. The hook provided by this repository is `packages/worktree-setup/worktree-setup.sh`; only one symlink is installed globally:
248
+
249
+ ```text
250
+ ~/.pi/agent/extensions/subagent/worktree-setup.sh -> <this repository>/packages/worktree-setup/worktree-setup.sh
251
+ ```
252
+
253
+ A project only has to commit a shell script at `<repo>/.pi/worktree-setup.sh`; without that file nothing happens (which is why one global hook serves every project):
254
+
255
+ ```sh
256
+ #!/bin/sh
257
+ set -eu
258
+ cd packages/pi-plugin # cd yourself when the manifest is not in the root directory
259
+ pnpm install --prefer-offline
260
+ ```
261
+
262
+ - **The exit code is the contract**: the hook does not judge a second time whether the install worked; the project decides in its own script. pnpm 12 still exits 1 after installing because of `ERR_PNPM_IGNORED_BUILDS`, so write `|| true` yourself or add a self-check. A self-check means writing "what counts as usable" as the project's own criterion:
263
+
264
+ ```sh
265
+ #!/bin/sh
266
+ set -u
267
+ cd packages/pi-plugin # cd yourself when the manifest is not in the root directory
268
+ pnpm install --prefer-offline || true # ignore pnpm 12's ignored-builds exit code
269
+
270
+ # the criterion belongs to the project: make sure the shim really resolves **in this worktree**, not in the inherited copy
271
+ if ! node -e 'require.resolve("typescript")' >/dev/null 2>&1; then
272
+ printf 'worktree setup: dependencies are unusable in %s\n' "$PWD" >&2
273
+ exit 1
274
+ fi
275
+ ```
276
+
277
+ Skipping silently is the most expensive failure mode here: the declaration exits 0, the hook believes it succeeded, and the subagent only discovers `Cannot find module` once it starts.
278
+ - **The hook deletes the `node_modules` symlink first**. When upstream creates a worktree it symlinks the **main checkout's root** `node_modules` into the worktree (`linkNodeModulesIfPresent`), so that a worktree which installs nothing can still run; the symlink points at **whatever the main checkout happens to have installed**, and both sides go wrong:
279
+ - **Nothing installed**: a package the worktree's manifest declares but the main checkout's tree does not have (someone added the manifest earlier and never reinstalled) makes the subagent get `Cannot find module` — even though the package is right there in the worktree's own `package.json` (measured: `is-number` resolves to `<main checkout>/node_modules/.pnpm/...`, `left-pad` fails outright). This has nothing to do with whether `package.json` changed during the work.
280
+ - **Installed**: pnpm writes through the symlink into the main checkout's copy — the main checkout's directory inode is unchanged, but it gains the package the worktree needed (measured: 4 top-level entries → 5, and the main checkout's own `package.json` never declared it).
281
+ - **The bookkeeping is written into someone else's directory**: with `virtualStoreType: global` (the worktree usage pnpm officially recommends), each project records the virtual store as a **relative path** in `node_modules/.modules.yaml`. Opened through the symlink, that file is the main checkout's, while pnpm computes the depth from the worktree, so the install rewrites the main checkout's copy: measured `../../../../.local/share/pnpm/store/v11/links` → `../../../../../../.local/share/pnpm/store/v11/links`, with the manifest untouched throughout. That value only holds for the project that wrote it last; resolved from the worktree it points at a directory that does not exist. So the hook deletes this symlink before running the declaration, leaves a real directory alone, and the declaration gets a clean starting point. A project that wants the symlink back can recreate it in its own script. Projects without a `node_modules` in the root do not get a symlink, so there is no impact.
282
+ - Output goes to `~/.cache/pi-worktree-setup/<worktree directory name>.log`; on failure the hook prints the last 20 lines and the log path to stderr, and when the output contains `not trusted` it additionally points at mise's `trusted_config_paths`.
283
+ - **The hook does not go through the sandbox**: it runs before the subagent, with host permissions. If the subagent runs `pnpm install` itself, it still needs write access inside the sandbox and `network`.
284
+ - The hook is POSIX sh, so the kernel has to honour the shebang; on Windows it has to be a native executable (upstream uses `spawn(hookPath, [], { shell: false })`, and Node will not run `.cmd` for you).
285
+
286
+ ## Rules in effect
287
+
288
+ - Inside the scope: `read`/`edit`/`write`/`grep`/`find`/`ls` are allowed (with `readOnly`, `edit`/`write` are refused).
289
+ - Always refused: `powershell`/`codemode`; unknown tools outside `scope`/`allowTools`.
290
+ - **Anchored-editing hosts**: when an extension such as pi-hashline-edit-pro replaces the builtin `edit`, the write tools are `replace`, `replace_match`, `insert`, `copy`, `move`, and `undo_last_change`. They are judged exactly like `edit` — do not declare them in `allowTools` expecting them to escape `writeScope`.
291
+ - **`copy` / `move` touch two files**, not one: `source_from` / `source_to` belong to the source, `anchor` to the destination, and both are judged. `copy` only reads the source, so the source may sit in the read scope while the destination is the writable one; `move` deletes from the source, so the source must be writable too. A cross-file copy whose destination is outside the write scope is refused — it is not a way to move content out.
292
+ - **Do not pin `edit` in an agent definition's `tools` field.** That field is a strict allowlist over *registered* names, so on an anchored-editing host `edit` never registers and the child stops at its first request ("declares tools: …, but an extension loaded in this child replaced the tools sent to the model"). Naming the anchored family instead breaks the child on every host that still has the builtin. Naming both is fatal too: a declared tool that never registered is itself a run error. Leave `tools` out and let this layer bound the child — it refuses any tool that is not a known path tool, a safe tool, or allowlisted.
293
+ - **`acceptanceRole` in the frontmatter accepts only `read-only` or `writer`** (pi-subagents validates it and rejects the whole definition otherwise — `analyst` or `reviewer` fails the dispatch before the run). It also changes what the child is asked to produce: `writer` yields a `checked` acceptance contract, `read-only` a `none` level with findings. With an async or dynamic writer, pi-subagents additionally marks an independent review by an agent named `reviewer` as **required** in that contract; it does not dispatch that agent, so the parent still has to run the review itself.
294
+ - **An anchored write with no `path`** gets its target from anchor ownership, which this layer reads from the host's session registry (`sessions/<sha256(sessionFile)[0:24]>.registry.jsonl`). A resolver miss refuses rather than allows, so anchors that expired (`free`/`clear`) produce a refusal saying the anchor is not in this session's registry. The anchor pair names differ per tool (`replace` uses `remove_from`/`remove_to`, `replace_match` uses `replace_from`/`replace_to`, `insert` uses `anchor`, `copy`/`move` use `source_from`/`source_to` plus `anchor`), and all of them resolve. `requirePath: true` in the host's config makes every call carry a path and removes that dependency.
295
+ - `contact_supervisor` / `intercom` / `structured_output` are always allowed, **allowed after the stop as well**: reporting to the supervisor mid-run is a legitimate channel and must not be shut off by a criterion or a probe.
296
+ - The stop is hard (`terminate`): when the whole batch of results is terminating, the turn ends right there, with no closing round. So ask for help before the stop; do not expect to be able to report anything after being stopped.
297
+ - **bash**: without an allowlist it is refused outright; with one, only allowlisted commands run, and each is rewritten to run in the helper sandbox — the file scope is enforced by Landlock, the network is off by default, and the writable paths are the ones the write scope covers (whole-subtree entries as directories, file-level entries as the files they match), git's own directories, `node_modules` (when `writable`), plus `writablePaths`. The cwd is not granted wholesale. Shell control characters are judged **outside quotes only**, because the command runs as `bash -lc <command>`: `; & | < > ( )` inside single or double quotes are ordinary characters, while `$` and a backtick still expand inside double quotes and are refused there too. So `python3 -c "print(1)"` and `echo 'a; b'` pass, but `pnpm test && cat other-file` and `echo "$(cat other-file)"` do not. An unterminated quote is refused. A leading `NAME=value` assignment is stripped before matching, so `HOME=/tmp/x node a.ts` matches an entry `node`; the scan is quote-aware and accepts `+=`, only the leading run is removed, and a `re:` entry still sees the command as written. Since the prefix is stripped, an entry must start with the program name — `HOME=/tmp node` is a rule that can never fire. Assignments that change which program runs or load code into it (`NODE_OPTIONS`, `LD_PRELOAD`, `GIT_EXTERNAL_DIFF`, `PATH`, `PYTHONPATH`, `BASH_ENV`, …) are refused with the variable named: `NODE_OPTIONS=--require=/tmp/x.js node -e ""` runs `/tmp/x.js`, so allowing it would make the allowlist decoration. If you genuinely need such a variable, the parent sets it in `bash.env`. Note the consequence: listing an interpreter (`node`, `python3`, `sh`) in the allowlist lets it run arbitrary code — the sandbox and the scope are the boundary, not the command text, and an interpreter belongs in the allowlist only when the task needs it.
298
+ - **Offline network commands are refused before they start**: when `bash.network` is off, a fixed list of round-trip-only commands is rejected after the allowlist and path checks but before the sandbox spec is written — `curl`, `wget`, `nc`/`ncat`/`netcat`, `telnet`, `ssh`, `scp`, `sftp`, `ping`, `dig`, `host`, `nslookup`, `traceroute`, `gh`, `docker pull|push|login`, `npm|pnpm|yarn publish`, and `git clone|fetch|pull|push|ls-remote|remote update|submodule update --remote`. The refusal says the network is off per the parent's bindings, that packets are dropped rather than refused (so these commands hang with no failure text to annotate), and to ask via `contact_supervisor` for `"network": true`; it counts as a permission collision on the grant budget, not the write budget. The matcher looks through absolute paths (`/usr/bin/curl` counts) and the pass-through wrappers `env`, `sudo`, `nice`, `timeout`, `command` and `stdbuf`, skipping their flags and flag values; it also skips `git`'s global flags (`-C`, `--git-dir`, `--work-tree`, `--no-pager`, `--progress`, `-c`) before the subcommand, so `git -C repo fetch` is judged while `git log --fetch` is not. It deliberately does not parse quoted text — `bash -c "curl …"` is allowed through — and anything it cannot resolve confidently (an unknown wrapper flag, `command -v curl`, a name assembled at run time) is allowed too. Local forms excluded on purpose: `curl file:///…` reads a file, and `curl --version` / `-V` / `--help` / `-h` print local information and exit, while `gh --version` / `version` / `help` / `config get …` only read local state; other `gh` subcommands and other curl URLs are still judged. **Accepted false positives**: `git push /local/path` and `nc -z localhost` are still refused even though both could complete offline — telling them apart would mean resolving a git remote URL or parsing a netcat host, which costs more than this guidance is worth; the way out is the same `contact_supervisor` request for `"network": true`. Looking past path prefixes has the same cost: `foo/curl`, `./curl` and `curl/` all count as curl even for a local file, because resolving the actual executable would cost more than this pre-spawn guidance is worth. Local work — `git status`/`diff`/`log`/`show`/`add`/`commit`, `node`, `rg`, `ls`, `cat`, `pnpm install`, `npm ci`, `cargo fetch`, package scripts — is never refused by this rule. The annotation below stays as a fallback for network failures the list does not cover.
299
+ - The notes below require that the command **actually failed** (the tool result's `isError`); a successful command whose printed output merely contains those strings is not annotated.
300
+ - **Network failures inside the sandbox are annotated**: when an offline bash hits `ENETUNREACH` / `EAI_AGAIN` / `ENOTFOUND` / `getaddrinfo failed`, a `[sandbox]` note is appended at the end of the result: the network was turned off by the parent agent, the machine is not broken, changing the registry / flag / timeout fails the same way, and if the network is needed, ask `contact_supervisor` (`"network": true`). `ECONNREFUSED` is not annotated — loopback works, that is a normal test result.
301
+ - **Out-of-scope writes are annotated as well**: when bash writes outside the granted scope, the kernel returns `EACCES` / `Permission denied` — the same wording as an ordinary file permission problem. The decision is not made by guessing at the wording: the extension takes the path out of the failure line and compares it with this command's `spec.write`; if the path is outside the grant and the host can read it, a `[sandbox]` note is appended at the end of the result, saying that this is an authorisation rather than a filesystem fault, and that writing requires `contact_supervisor`. Failures inside the grant, and failures caused by insufficient host permissions themselves (such as reading `/etc/shadow`), are not annotated. Paths ending in `.lock` are not annotated either: git probes for lock files it does not need (`packed-refs.lock` in a worktree), prints that refusal with exit status 0, and the commit lands anyway — reporting it would send the parent off to widen a correct grant.
302
+ - **A command that ran but cannot work is annotated**: when an allowlisted command fails because the working directory has no manifest (`pnpm` reports `No package.json found`), a `[sandbox]` note is appended at the end of the result: it names the allowlist entry that cannot work from that directory, the reason, and the form that would work (`pnpm --dir packages/pi-plugin run typecheck`). It names **the entry the allowlist actually used to authorise the command**, not just another entry that matches it — a wider `pnpm` in the same list must not be pinned on a command another entry granted. With several candidate packages it lists up to eight of them instead of picking one (the rest as `… and N more`), and when the only candidate has no such script it says ``no candidate has a `typecheck` script`` rather than inventing a form or asking which package was meant. A truncated list says so (`only the first 8 are listed`). `bun test` is never reported: it runs bun's own runner and needs no manifest at all.
303
+ - The text of an out-of-scope **file-tool** refusal is in English, and it spells out "which path is out of scope, do not retry, report it if you need it", and lists the current scope. The bash path nudge is a different refusal: it names a path outside the working tree and lists the working tree. A refusal for a missing path does not count as a permission problem: `grep`/`find` without a path are handled as the cwd (the text says `no path was given`), and `write`/`edit` must be given a path (the text says `needs a path`). Such refusals do not count towards the collision count.
304
+ - **Refusal counts are consecutive**: any allowed working call resets both budgets, so a mistake that has been corrected is not carried to the end of the run. `contact_supervisor` and `structured_output` are exempt from that reset — they are a report, not progress, so alternating a probe with an escalation does not keep the run alive at the warning level.
305
+ - Inside the scope a session can use `/permission` to see the current policy: `Source`, `Bindings`, `Mode`, `Write scope` (when it differs), `Read scope`, `Read denials: … · write denials: …`, the `bash allow:` column (with the no-write note when `writable: false` and entries exist), the legacy `bash (legacy columns, migrate to allow):` line when legacy entries exist, `Sandbox`, `Sandbox writable paths`, `Reads` and the probe state.
306
+
307
+ ## Notes for the parent agent
308
+
309
+ - When the subagent reports `no path was given` or `needs a path`: the tool argument was missing, it did not go to the wrong directory. Put the file paths into the task you give the subagent.
310
+ - When the subagent reports `is outside this task's scope`, decide whether that path really belongs to this task: if it does, widen `scope` and dispatch again; if it does not, the task description is ambiguous, so fix the task description first. Check the scope syntax itself first (a directory has to be written as `dir` / `dir/` / `dir/**`), and only then suspect that the subagent went to the wrong directory.
311
+ - When a batch contains an allowed result (the batch is not all terminating), the turn continues, and every later call gets the text of the "stop reason" (it holds historical paths). When judging, trust only the criterion on the last line; do not treat the paths inside it as this task's paths.
312
+ - Do not retry out-of-scope paths over and over to get around a refusal.
313
+ - When the subagent has to run tests/typecheck itself, list the commands in `bash.allow` — one column, and the extension judges from the command itself whether it reads or writes (use a `re:` regex when you need to be precise, but never `re: .*`). Add `"writable": true` for any command whose program can write: package managers (`pnpm`/`npm`/`yarn`/`bun` with `run`/`test`/`exec`) are refused at the gate without it, and it is also what lets their artefacts land on disk. The sandbox has no network by default, so installing dependencies and the like is refused.
314
+ - The read face stays open on purpose: running tests has to read the whole tree, the toolchain lives in `/nix/store`, and narrowing the read root would kill every command at once, so an allowlisted command can still `cat` a file outside the tree inside the sandbox. Do not treat the bash allowlist as a way of locking reads down. What the extension adds is a nudge, not a wall: for a **read-class** command, an argument that counts as a path — it contains a slash and nothing but path characters, so `src/index.ts` and `~/x` qualify — is resolved (symlinks by their real target) and refused when it lands outside the **working tree**, the directory the command runs in. That is deliberately not the read scope: a scope can be much narrower than the tree (`scope: ["src/auth/**"]` is the normal shape), while a build has to read the whole tree, so the tree is what a command is judged against. The toolchain directories are **not** exempt — the program token is, because that is the tool being run rather than a file of the task's. What is exempt is the system's `/etc`, `/proc`, `/dev` and `/sys`, the working tree itself and the command's private scratch directory. Anything doubtful is skipped rather than guessed at — quoted arguments, `$VAR`, globs, URLs, and bare words such as `foo` or `--flag`. A child that needs a file outside the tree should use `read` when the scope covers it, or say what it needs — not hunt through `/nix/store` with `rg`.
315
+ - When the subagent reports `command not in the allowlist`, first decide whether that command is really needed for this task: if it is, add it to the matching column and dispatch again; if it is not, change the task description instead of letting it keep trying. The refusal text carries the pnpm store path and the `bash.writablePaths` hint — copy it in when the subagent has to install dependencies, instead of letting it install them itself.
316
+ - Nested subagents do not inherit bindings: when you lock down an agent that may delegate in turn, do not also enable `allowNestedSubagents`.
317
+ - The bindings are capped at 16 KiB in total, so keep the scope globs terse; all fields of this extension share the namespace `pi-subagents-extend/1`, and the cap applies to the whole bindings object.