pi-daddy 0.18.1 → 0.19.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 (130) hide show
  1. package/CHANGELOG.md +136 -0
  2. package/README.md +46 -2
  3. package/contracts/ledger/v2/README.md +59 -0
  4. package/contracts/ledger/v2/fixtures/capability-decision.json +95 -0
  5. package/contracts/ledger/v2/fixtures/check-receipt.json +40 -0
  6. package/contracts/ledger/v2/fixtures/child-lifecycle.json +42 -0
  7. package/contracts/ledger/v2/fixtures/workspace-lease.json +41 -0
  8. package/contracts/ledger/v2/ledger-event.schema.json +633 -0
  9. package/dist/approval-prompt.d.ts +2 -1
  10. package/dist/approval-prompt.d.ts.map +1 -1
  11. package/dist/approval-prompt.js +9 -0
  12. package/dist/approval-prompt.js.map +1 -1
  13. package/dist/approval.d.ts +4 -2
  14. package/dist/approval.d.ts.map +1 -1
  15. package/dist/approval.js +4 -0
  16. package/dist/approval.js.map +1 -1
  17. package/dist/capabilities.d.ts +90 -0
  18. package/dist/capabilities.d.ts.map +1 -1
  19. package/dist/capabilities.js +114 -3
  20. package/dist/capabilities.js.map +1 -1
  21. package/dist/catalog.d.ts +15 -1
  22. package/dist/catalog.d.ts.map +1 -1
  23. package/dist/catalog.js +54 -3
  24. package/dist/catalog.js.map +1 -1
  25. package/dist/check-runner.d.ts.map +1 -1
  26. package/dist/check-runner.js +5 -7
  27. package/dist/check-runner.js.map +1 -1
  28. package/dist/cli.d.ts.map +1 -1
  29. package/dist/cli.js +21 -1
  30. package/dist/cli.js.map +1 -1
  31. package/dist/definitions.d.ts.map +1 -1
  32. package/dist/definitions.js +7 -1
  33. package/dist/definitions.js.map +1 -1
  34. package/dist/delegate.d.ts.map +1 -1
  35. package/dist/delegate.js +31 -4
  36. package/dist/delegate.js.map +1 -1
  37. package/dist/delegation-approval.d.ts.map +1 -1
  38. package/dist/delegation-approval.js +37 -12
  39. package/dist/delegation-approval.js.map +1 -1
  40. package/dist/executor.d.ts +2 -1
  41. package/dist/executor.d.ts.map +1 -1
  42. package/dist/executor.js +1 -0
  43. package/dist/executor.js.map +1 -1
  44. package/dist/grant-env.d.ts +2 -0
  45. package/dist/grant-env.d.ts.map +1 -1
  46. package/dist/grant-env.js +26 -3
  47. package/dist/grant-env.js.map +1 -1
  48. package/dist/index.d.ts +1 -1
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +1 -1
  51. package/dist/index.js.map +1 -1
  52. package/dist/init.d.ts +13 -1
  53. package/dist/init.d.ts.map +1 -1
  54. package/dist/init.js +35 -2
  55. package/dist/init.js.map +1 -1
  56. package/dist/lease-helper.d.ts +58 -0
  57. package/dist/lease-helper.d.ts.map +1 -0
  58. package/dist/lease-helper.js +94 -0
  59. package/dist/lease-helper.js.map +1 -0
  60. package/dist/lease-record.d.ts +15 -3
  61. package/dist/lease-record.d.ts.map +1 -1
  62. package/dist/lease-record.js.map +1 -1
  63. package/dist/ledger-events.d.ts +35 -14
  64. package/dist/ledger-events.d.ts.map +1 -1
  65. package/dist/ledger-events.js +41 -0
  66. package/dist/ledger-events.js.map +1 -1
  67. package/dist/ledger.d.ts +10 -5
  68. package/dist/ledger.d.ts.map +1 -1
  69. package/dist/ledger.js +13 -3
  70. package/dist/ledger.js.map +1 -1
  71. package/dist/propagation.d.ts +16 -0
  72. package/dist/propagation.d.ts.map +1 -1
  73. package/dist/propagation.js +20 -1
  74. package/dist/propagation.js.map +1 -1
  75. package/dist/refusals.d.ts +1 -1
  76. package/dist/refusals.d.ts.map +1 -1
  77. package/dist/refusals.js +1 -0
  78. package/dist/refusals.js.map +1 -1
  79. package/dist/resolve.d.ts +10 -0
  80. package/dist/resolve.d.ts.map +1 -1
  81. package/dist/resolve.js +27 -3
  82. package/dist/resolve.js.map +1 -1
  83. package/dist/routing-authority.d.ts +71 -0
  84. package/dist/routing-authority.d.ts.map +1 -0
  85. package/dist/routing-authority.js +100 -0
  86. package/dist/routing-authority.js.map +1 -0
  87. package/dist/skill-packages.d.ts +11 -5
  88. package/dist/skill-packages.d.ts.map +1 -1
  89. package/dist/skill-packages.js +20 -11
  90. package/dist/skill-packages.js.map +1 -1
  91. package/dist/workspace-lease.d.ts +15 -3
  92. package/dist/workspace-lease.d.ts.map +1 -1
  93. package/dist/workspace-lease.js +81 -24
  94. package/dist/workspace-lease.js.map +1 -1
  95. package/dist/workspace.d.ts +25 -0
  96. package/dist/workspace.d.ts.map +1 -1
  97. package/dist/workspace.js +142 -5
  98. package/dist/workspace.js.map +1 -1
  99. package/extensions/delegation.ts +7 -1
  100. package/extensions/grants-command.ts +11 -1
  101. package/extensions/grants.ts +6 -1
  102. package/extensions/init-command.ts +33 -2
  103. package/extensions/session-report.ts +13 -20
  104. package/extensions/session.ts +9 -1
  105. package/extensions/workspace-runtime.ts +34 -4
  106. package/package.json +8 -3
  107. package/src/approval-prompt.ts +2 -1
  108. package/src/approval.ts +4 -2
  109. package/src/capabilities.ts +120 -4
  110. package/src/catalog.ts +62 -4
  111. package/src/check-runner.ts +8 -8
  112. package/src/cli.ts +24 -1
  113. package/src/definitions.ts +7 -1
  114. package/src/delegate.ts +41 -4
  115. package/src/delegation-approval.ts +37 -12
  116. package/src/executor.ts +2 -1
  117. package/src/grant-env.ts +39 -7
  118. package/src/index.ts +1 -0
  119. package/src/init.ts +40 -2
  120. package/src/lease-helper.ts +97 -0
  121. package/src/lease-record.ts +15 -3
  122. package/src/ledger-events.ts +66 -22
  123. package/src/ledger.ts +30 -6
  124. package/src/propagation.ts +21 -1
  125. package/src/refusals.ts +1 -0
  126. package/src/resolve.ts +29 -3
  127. package/src/routing-authority.ts +121 -0
  128. package/src/skill-packages.ts +20 -13
  129. package/src/workspace-lease.ts +84 -24
  130. package/src/workspace.ts +179 -6
@@ -330,9 +330,19 @@ handler: async (args: string, ctx: any) => {
330
330
  ` approvals ${sessionApprovals.size} this session, ${valid.size} persisted` +
331
331
  `${inheritedApprovals.size > 0 ? `, ${inheritedApprovals.size} inherited` : ""}` +
332
332
  ` — /grants approvals`,
333
+ // Every kind the catalog can hold is named, and the numbers add up to the total on purpose. `workspace`
334
+ // was missing when ADR-0035's review added the kind: the total counted them and the breakdown did not,
335
+ // so `catalog 16 capabilities — 9 builtin, 0 extension, 2 skill, 2 agent-type` printed a sum of 13. And
336
+ // the comment that justified putting workspaces in the catalog said it was "for `/grants` to list what
337
+ // this session may route to", which nothing then did — the claim was in the commit and the behaviour
338
+ // was not. A kind added without a term here silently stops adding up.
333
339
  ` catalog ${catalog.all.length} capabilities — ` +
334
340
  `${catalog.byKind("builtin").length} builtin, ${catalog.byKind("extension").length} extension, ` +
335
- `${catalog.byKind("skill").length} skill, ${catalog.byKind("agentType").length} agent-type`,
341
+ `${catalog.byKind("skill").length} skill, ${catalog.byKind("agentType").length} agent-type, ` +
342
+ `${catalog.byKind("workspace").length} workspace`,
343
+ ...(catalog.byKind("workspace").length > 0
344
+ ? [` routable ${catalog.byKind("workspace").join(", ")} — held ones only are usable (ADR-0035)`]
345
+ : []),
336
346
  ];
337
347
  // Runs the REAL planner AND the real approval step over each definition, so this listing cannot
338
348
  // disagree with what a spawn would do — the R-28 lesson, kept structural rather than remembered.
@@ -25,6 +25,7 @@ import { fileURLToPath } from "node:url";
25
25
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
26
26
  import { WILDCARD } from "../src/pi-tools.ts";
27
27
  import { buildCatalog } from "../src/catalog.ts";
28
+ import { ENV_WORKSPACE_REGISTRY } from "../src/workspace.ts";
28
29
  import { appendRecord, buildRecord } from "../src/ledger.ts";
29
30
  import { openPaneCount, reapOpenPanesAsync } from "../src/pane-reaper.ts";
30
31
  import {
@@ -216,7 +217,11 @@ export default function (pi: ExtensionAPI) {
216
217
  // Keep the handle: a concurrent `delegate` awaits this rather than reading a half-built catalog.
217
218
  // The `catch` resolves to the CURRENT catalog rather than rejecting, so a failed refresh degrades
218
219
  // to the previous view instead of failing every delegation in the session.
219
- session.catalogReady = buildCatalog({ cwd: session.cwd, observedTools: names })
220
+ session.catalogReady = buildCatalog({
221
+ cwd: session.cwd,
222
+ observedTools: names,
223
+ registryPath: process.env[ENV_WORKSPACE_REGISTRY],
224
+ })
220
225
  .then((c) => (session.catalog = c))
221
226
  .catch(() => session.catalog);
222
227
  } catch {
@@ -9,6 +9,7 @@
9
9
 
10
10
  import { discoverSkillPackages } from "../src/skill-packages.ts";
11
11
  import { applyInit, planInit } from "../src/init.ts";
12
+ import { registeredWorkspaceIds } from "../src/workspace.ts";
12
13
  import { saveGrant, grantStorePath } from "../src/grant-store.ts";
13
14
  import { expandSubsumed, SUBSUMPTION, type Capability } from "../src/resolve.ts";
14
15
  import type { GrantsSession } from "./session.ts";
@@ -50,7 +51,7 @@ export async function runInit(
50
51
  return;
51
52
  }
52
53
 
53
- const plan = planInit(packages, ctx.cwd);
54
+ const plan = planInit(packages, ctx.cwd, await registeredWorkspaceIds());
54
55
  const outcome = await applyInit(plan);
55
56
  const lines = [
56
57
  `grants: ${plan.skills.length} definition(s) from ${packages.map((p) => `${p.name}@${p.version}`).join(", ")}`,
@@ -81,9 +82,28 @@ export async function runInit(
81
82
  for (const name of neededBy) grant.add(`agent:${name}` as Capability);
82
83
  continue;
83
84
  }
85
+ // The consequence sentence is per capability, and it reads the gate **in effect for this session** —
86
+ // `session.gated`, which is `PI_GRANTS_GATED` when the operator set it and `DEFAULT_GATED` otherwise.
87
+ //
88
+ // The first version read `DEFAULT_GATED` directly, which is the compile-time constant `["tool:bash"]`, and
89
+ // `tool:bash` is consumed by the branch above — so the branch was **unreachable in every configuration**,
90
+ // and with `PI_GRANTS_GATED="tool:bash,tool:write"` (the value `renderGrantEnv` itself suggests) the
91
+ // dialog still printed the exact false sentence the fix claimed to remove. Dead code beside a claim that
92
+ // it worked, which is this project's failure mode, in the commit correcting that failure mode.
93
+ // No subsumption closure here, and the absence is deliberate. A `|| SUBSUMPTION[capability]…` disjunct was
94
+ // added and is **dead in every configuration**: `SUBSUMPTION` has one key, `tool:bash`, and `tool:bash` is
95
+ // consumed by the ternary below before `gatedAtSpawn` is ever read. Two reviewers and a mutation confirmed
96
+ // it changed no result — dead code beside a claim that it worked, in the hunk whose comment denounces
97
+ // exactly that. A second `SUBSUMPTION` entry is the moment to add it back.
98
+ const gatedAtSpawn = session.gated.includes(capability);
84
99
  const answer = await ctx.ui.select(
85
100
  `grants: grant ${capability} to sub-agents?\n needed by: ${neededBy.join(", ")}\n` +
86
- ` this can change your machine — ${capability === "tool:bash" ? "and bash also confers write and edit" : "it is not gated, so no dialog at spawn time"}`,
101
+ ` this can change your machine — ` +
102
+ (capability === "tool:bash"
103
+ ? "and bash also confers write and edit"
104
+ : gatedAtSpawn
105
+ ? "a human is still asked at spawn time"
106
+ : "it is not gated, so no dialog at spawn time"),
87
107
  ["No", "Yes"],
88
108
  );
89
109
  if (answer === "Yes") {
@@ -121,6 +141,17 @@ export async function runInit(
121
141
  lines.push(` ALREADY CONFERRED, not asked about: ${alreadyConferred.join(", ")}`);
122
142
  }
123
143
  if (declined.length > 0) lines.push(` withheld: ${declined.join("; ")}`);
144
+ // Routing is LISTED, never granted (ADR-0028) — so the operator has to be told it exists, or the listing is
145
+ // invisible to anyone running `/grants init` in-session rather than reading the generated file. 0.19.0 made
146
+ // `workspace:<id>` mandatory for routing, so this is also the migration hint, and a breaking change whose
147
+ // migration is only discoverable by opening a file is not much of a migration.
148
+ if (plan.routableWorkspaces.length > 0) {
149
+ lines.push(
150
+ ` ROUTABLE WORKSPACES, listed and NOT granted: ${plan.routableWorkspaces.join(", ")}`,
151
+ ` routing a child to one needs its id in the grant (ADR-0035); add the ones this project may use to ` +
152
+ `${plan.grantEnvPath}. Which worktree a child starts in is not something a package can declare for you.`,
153
+ );
154
+ }
124
155
  lines.push(` live now (${finalGrant.length} capabilities) — no restart. /grants shows the verdicts.`);
125
156
  ctx.ui.notify(lines.join("\n"), "info");
126
157
  }
@@ -80,16 +80,6 @@ export async function reportSessionStart(session: GrantsSession, ctx: SessionRep
80
80
  } catch {
81
81
  /* never throw into the agent loop */
82
82
  }
83
- // R-47. `gatedBlocked` filters `requested`, and for a definition spawn `requested` is that
84
- // definition's CEILING — which never contains `agent:<name>`, because the authorisation check
85
- // (ADR-0017) is a separate, ungated branch. So `PI_GRANTS_GATED=agent:deploy`, written by an operator
86
- // who read "it attenuates like any other capability" and meant "ask me before deploy runs", produces
87
- // no dialog and no warning. It DOES bite when a definition passes the id down in its own
88
- // `allowed-tools`, so the flag half-works — which is worse than not working, and is R-25's shape in
89
- // the namespace ADR-0017 just promoted out of exactly that state.
90
- //
91
- // Warned rather than enforced: making it gate the spawn is a behaviour change and wants a decision.
92
- // Silence is the part that is indefensible either way.
93
83
  // `agent:*` grants no tools, but it authorises every definition in BOTH skill roots — including
94
84
  // `~/.pi/agent/skills/`, which other software installs into, so ADR-0017's "an operator-authored
95
85
  // file" is not true of everything it covers. Paired with a shell that is every body on disk running
@@ -103,16 +93,19 @@ export async function reportSessionStart(session: GrantsSession, ctx: SessionRep
103
93
  "warning",
104
94
  );
105
95
  }
106
- const inertGates = session.gated.filter((c) => c.startsWith("agent:"));
107
- if (inertGates.length > 0) {
108
- ctx.ui.notify(
109
- `grants: ${inertGates.join(", ")} in PI_GRANTS_GATED does NOT gate spawning that definition — ` +
110
- `the authorisation check for a definition is separate and ungated, so a human is never asked. ` +
111
- `It applies only where a definition passes the id down in its own allowed-tools. To control ` +
112
- `which definitions may run, withhold the agent: capability from PI_GRANTS_GRANT instead.`,
113
- "warning",
114
- );
115
- }
96
+ // REMOVED 2026-08-21. This warned that an `agent:` id in `PI_GRANTS_GATED` "does NOT gate spawning that
97
+ // definition — a human is never asked". It was R-47's PARTIAL fix (0.11.1) and became false one release
98
+ // later at 0.12.0, when ADR-0024's gate landed (`4673348`, "gating a definition asks before it runs") and
99
+ // a gated `agent:<name>` began blocking the spawn until somebody approved it. ADR-0024's own Costs section
100
+ // leaned on this warning — "mitigated by the fact that the warning shipped hours earlier tells them it
101
+ // currently does nothing" — and nothing retired it when its own decision made the mitigation false.
102
+ //
103
+ // So from 0.12.0 through 0.18.1 the session banner told operators to delete a gate that works, and
104
+ // `test-integration/governance.it.ts` REQUIRED it to, which is how a stale claim outlives the person who
105
+ // notices. It is R-28's shape in the direction that talks somebody OUT of a control. ADR-0035 then
106
+ // repeated the pattern one namespace over by claiming a `workspace:` gate that did not exist; both are
107
+ // gates now, so there is nothing inert left to warn about. Deleted rather than reworded — a warning with
108
+ // no live case is the next stale claim. R-134.
116
109
  // R-34. `verifyLedger` existed and nothing ran it, so a torn line was detectable and undetected —
117
110
  // and a check an operator has to know to run is not a control, it is a feature. Setting
118
111
  // `PI_GRANTS_LEDGER` already means "I want an audit trail"; noticing that the trail is damaged is
@@ -42,6 +42,7 @@ import {
42
42
  import type { Capability } from "../src/resolve.ts";
43
43
  import { loadDefinitions } from "../src/definitions.ts";
44
44
  import { buildCatalog } from "../src/catalog.ts";
45
+ import { ENV_WORKSPACE_REGISTRY } from "../src/workspace.ts";
45
46
  import { loadGrantSync, grantStorePath } from "../src/grant-store.ts";
46
47
  import { republishable } from "./approvals.ts";
47
48
 
@@ -210,7 +211,14 @@ export interface GrantsSession {
210
211
  */
211
212
  export async function loadProjectDefinitions(session: GrantsSession, cwd: string): Promise<void> {
212
213
  session.definitions = await loadDefinitions(cwd);
213
- session.catalogReady = buildCatalog({ cwd, observedTools: session.observedTools });
214
+ session.catalogReady = buildCatalog({
215
+ cwd,
216
+ observedTools: session.observedTools,
217
+ // ADR-0035: `workspace:<id>` is a capability, so the registered ids belong in the catalog the same way
218
+ // discovered definitions do — for `/grants` to list what this session may route to and for `init` to
219
+ // scaffold them. Read live rather than cached at load, because the registry is an operator file.
220
+ registryPath: process.env[ENV_WORKSPACE_REGISTRY],
221
+ });
214
222
  session.catalog = await session.catalogReady;
215
223
  }
216
224
 
@@ -23,12 +23,39 @@ export interface DelegationWorkspaceSpec {
23
23
  access: WorkspaceAccess;
24
24
  }
25
25
 
26
+ /**
27
+ * Tools that cannot change a worktree.
28
+ *
29
+ * **`tool:delegate` is deliberately NOT here, and that is a scope decision.** It was added, and it was true of
30
+ * the capability and false of the code: a routed child's cwd IS the leased root, `PI_GRANTS_LEDGER` was
31
+ * passed through relative (`init` scaffolds `.pi/grants.jsonl`), and a `read` lease takes no kernel lock — so
32
+ * two delegating children classified `read` both created `.pi/` in one worktree and neither excluded the
33
+ * other. Making `tool:delegate` non-writing requires first making every child-inherited path absolute, and
34
+ * that is a change to the ledger and lease plumbing rather than to ADR-0035. Tracked as R-141.
35
+ */
26
36
  const KNOWN_READ_ONLY_TOOLS = new Set(["tool:read", "tool:grep", "tool:find", "tool:ls"]);
27
37
 
28
- /** A model may ask for stricter coordination but cannot label a write-capable grant read-only. */
38
+ /**
39
+ * A model may ask for stricter coordination but cannot label a write-capable grant read-only.
40
+ *
41
+ * **Only a TOOL is considered, and that is a fix rather than an oversight.** The check used to require *every*
42
+ * requested capability to be a known read-only tool, which was correct while `tool:` and `ext:` were the only
43
+ * things that could appear — and became wrong the moment ADR-0035's review made `workspace:<id>` grantable to
44
+ * a child. Measured: `governedWorkspaceAccess("read", ["tool:read", "workspace:staging"])` returned `"write"`,
45
+ * so the intended shape — *route this child read-only and let it route its own grandchild* — silently took an
46
+ * exclusive writer lease, blocked every other writer on that root, and recorded `access: "write"` in the
47
+ * ledger when the operator had asked for `read`. A record asserting a stronger claim than anybody made is the
48
+ * same failure this whole review is about, pointing the other way.
49
+ *
50
+ * A `workspace:`, `agent:` or `skill:` id confers no filesystem ability whatsoever: routing chooses a
51
+ * directory, `agent:` authorises a definition whose ceiling is still clipped to the child's own grant, and
52
+ * `skill:` loads instructions. None of them can write, so none of them should force a writer lease. If a
53
+ * descendant does hold a write tool, that tool is in `requested` and this check refuses on its own terms.
54
+ */
29
55
  export function governedWorkspaceAccess(declared: WorkspaceAccess, requested: readonly Capability[]): WorkspaceAccess {
30
56
  if (declared === "write") return "write";
31
- return requested.every((capability) => KNOWN_READ_ONLY_TOOLS.has(capability)) ? "read" : "write";
57
+ const tools = requested.filter((c) => c.startsWith("tool:") || c.startsWith("ext:"));
58
+ return tools.every((capability) => KNOWN_READ_ONLY_TOOLS.has(capability)) ? "read" : "write";
32
59
  }
33
60
 
34
61
  export interface PreparedWorkspace {
@@ -133,8 +160,11 @@ export async function releaseDelegationWorkspace(input: {
133
160
  // A retained lease writes no `state: "released"`, so the record stays `active` and the NEXT owner reads
134
161
  // it as a crash — the exact blame `retained` was added to remove. Marking it keeps the successor honest;
135
162
  // R-104 was fixed in the release event's wording only.
136
- const outcome: LeaseReleaseOutcome | "retained" = input.retain
137
- ? (await input.prepared.lease.markRetained(input.reason), "retained")
163
+ // **Ledger what happened, not what was intended (R-152).** This used to discard `markRetained`'s result and
164
+ // write the word "retained" unconditionally, so a helper that had already died, or a lease already released,
165
+ // was still recorded as a pane that may still be live.
166
+ const outcome: LeaseReleaseOutcome = input.retain
167
+ ? await input.prepared.lease.markRetained(input.reason)
138
168
  : await input.prepared.lease.release(input.reason);
139
169
  if (input.ledgerPath) {
140
170
  await appendLedgerEvent(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-daddy",
3
- "version": "0.18.1",
3
+ "version": "0.19.0",
4
4
  "description": "Capability governance for pi sub-agents: spawn Agent Skills (SKILL.md) definitions whose allowed-tools becomes a grant that can only narrow going down a delegation tree, enforced by pi's own --tools allowlist, with an append-only ledger.",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -128,7 +128,9 @@
128
128
  "./check-runner": {
129
129
  "types": "./dist/check-runner.d.ts",
130
130
  "default": "./dist/check-runner.js"
131
- }
131
+ },
132
+ "./contracts/ledger/v2/ledger-event.schema.json": "./contracts/ledger/v2/ledger-event.schema.json",
133
+ "./contracts/ledger/v2/fixtures/*.json": "./contracts/ledger/v2/fixtures/*.json"
132
134
  },
133
135
  "bin": {
134
136
  "pi-daddy": "./dist/cli.js"
@@ -137,6 +139,7 @@
137
139
  "dist",
138
140
  "extensions",
139
141
  "src",
142
+ "contracts",
140
143
  "README.md",
141
144
  "CHANGELOG.md",
142
145
  "LICENSE"
@@ -146,8 +149,10 @@
146
149
  "typecheck": "tsc -p tsconfig.check.json",
147
150
  "test": "node --test test/*.test.ts",
148
151
  "test:integration": "node --test test-integration/*.it.ts",
152
+ "contracts:generate": "node scripts/generate-ledger-v2-contract.ts",
149
153
  "prepack": "npm run build",
150
- "test:smoke": "node scripts/smoke-installed.mjs"
154
+ "test:smoke": "node scripts/smoke-installed.mjs",
155
+ "test:mutation": "node scripts/mutation-audit.mjs"
151
156
  },
152
157
  "main": "./dist/index.js",
153
158
  "types": "./dist/index.d.ts",
@@ -51,7 +51,8 @@ export interface PromptRequest {
51
51
  * breaking) cannot recover that from `scope` or from parsing `reason`. Only "declined" means a human said
52
52
  * no; the other three are absence-of-signal, not a signal.
53
53
  */
54
- export type PromptOutcomeKind = "granted" | "no-ui" | "declined" | "dismissed" | "error";
54
+ export const PROMPT_OUTCOME_KINDS = ["granted", "declined", "dismissed", "no-ui", "error"] as const;
55
+ export type PromptOutcomeKind = typeof PROMPT_OUTCOME_KINDS[number];
55
56
 
56
57
  export interface PromptOutcome {
57
58
  /** The scope the human chose, or null for any form of no. */
package/src/approval.ts CHANGED
@@ -14,10 +14,12 @@ import type { Capability, ResolveResult } from "./resolve.ts";
14
14
  import { approvalBindingsEqual, type ApprovalBinding } from "./correlation.ts";
15
15
 
16
16
  /** How far a single yes reaches in time. */
17
- export type ApprovalScope = "once" | "session" | "always";
17
+ export const APPROVAL_SCOPES = ["once", "session", "always"] as const;
18
+ export type ApprovalScope = typeof APPROVAL_SCOPES[number];
18
19
 
19
20
  /** Where a yes came from, for the ledger. These call for different follow-ups, so they stay distinct. */
20
- export type ApprovalSource = "prompt" | "session" | "persisted" | "inherited";
21
+ export const APPROVAL_SOURCES = ["prompt", "session", "persisted", "inherited"] as const;
22
+ export type ApprovalSource = typeof APPROVAL_SOURCES[number];
21
23
 
22
24
  /** Which call site is asking. Determines the scopes offered — see `offeredScopes`. */
23
25
  export type ApprovalPath = "definition" | "delegate";
@@ -12,7 +12,7 @@
12
12
  * *what one delegation does*.
13
13
  */
14
14
 
15
- import { AGENT_WILDCARD, type Capability } from "./resolve.ts";
15
+ import { AGENT_WILDCARD, WORKSPACE_WILDCARD, type Capability } from "./resolve.ts";
16
16
  import { WILDCARD } from "./pi-tools.ts";
17
17
  import { GovernanceRefusal, refusal } from "./refusals.ts";
18
18
 
@@ -42,15 +42,61 @@ export function maySpawnDefinition(ownGrant: Capability[], name: string): boolea
42
42
  /** The tool name that confers the ability to delegate further. */
43
43
  export const DELEGATE_CAPABILITY: Capability = "tool:delegate";
44
44
 
45
+ /**
46
+ * Every namespace a capability id may carry, as its literal prefix.
47
+ *
48
+ * **One list, because there were three and adding a namespace only updated some of them.** ADR-0035 taught
49
+ * `normaliseCapability` about `workspace:` and left `ceilingForDefinition` (which mangled it into
50
+ * `tool:workspace:<id>`) and `isSafeCapability` (which called it malformed) behind. The two places that parse
51
+ * an id's namespace read this, so a fifth namespace is one entry rather than a third divergence.
52
+ *
53
+ * **Not every prefix decision in the package.** Inline `startsWith("` tests on a capability namespace are
54
+ * scattered across `src/` and `extensions/`; this list is read by two of them. **No number is written here,
55
+ * and the history is why:** SPEC first claimed the list was "what every site reads"; a correction said "six"
56
+ * while its own enumeration listed seven; this docstring then said "nine" in the sentence claiming it gave no
57
+ * number; and the correction to *that* said "fourteen", which counted every file containing any
58
+ * `startsWith("` — CLI flags, `git worktree list` parsing, an assurance sentinel and node_modules entries
59
+ * among them. Four numbers, four wrong. The command, so nobody has to quote a predecessor:
60
+ *
61
+ * grep -rl 'startsWith("\(tool:\|ext:\|agent:\|skill:\|workspace:\)' src extensions
62
+ *
63
+ * Consolidating the sites is a separate change, and a count nobody re-derives is the defect this list exists
64
+ * to prevent.
65
+ *
66
+ * `docs/SPEC.md`'s grammar section is the prose statement of the same list and is kept in step with it.
67
+ */
68
+ export const CAPABILITY_NAMESPACE_PREFIXES = ["tool:", "ext:", "skill:", "agent:", "workspace:"] as const;
69
+
45
70
  /** Accept `read` or `tool:read` or `ext:pkg/tool` and normalise to a capability id. */
46
71
  export function normaliseCapability(raw: string): Capability {
47
72
  const value = raw.trim();
48
- if (value.startsWith("tool:") || value.startsWith("ext:") || value.startsWith("skill:") || value.startsWith("agent:")) {
49
- return value;
50
- }
73
+ if (CAPABILITY_NAMESPACE_PREFIXES.some((prefix) => value.startsWith(prefix))) return value;
51
74
  return `tool:${value}`;
52
75
  }
53
76
 
77
+ /** The capability that authorises routing a child to one registered workspace (ADR-0035). */
78
+ export function workspaceCapability(workspaceId: string): Capability {
79
+ return `workspace:${workspaceId}`;
80
+ }
81
+
82
+ /**
83
+ * May this session route a child to workspace `id`?
84
+ *
85
+ * Mirrors `maySpawnDefinition` deliberately — same shape, same wildcard handling, same reason. Routing was
86
+ * the one governance dimension that did not attenuate (R-131, measured in
87
+ * `docs/probes/g36-workspace-attenuation`): the registry inherited into every child and nothing checked the
88
+ * caller's authority, so a child routed to `staging` could route its grandchild to `prod`.
89
+ *
90
+ * `tool:*` satisfies it because governance is opt-in — an ungoverned session holds the wildcard and must
91
+ * keep working exactly as before.
92
+ */
93
+ export function mayRouteToWorkspace(ownGrant: readonly Capability[], workspaceId: string): boolean {
94
+ const held = new Set(ownGrant);
95
+ return held.has(WILDCARD)
96
+ || held.has(WORKSPACE_WILDCARD)
97
+ || held.has(workspaceCapability(workspaceId));
98
+ }
99
+
54
100
  /**
55
101
  * Characters that make a capability id mean something OTHER than one capability.
56
102
  *
@@ -78,6 +124,76 @@ export function isWellFormedCapability(id: string): boolean {
78
124
  return id.length > 0 && !CAPABILITY_ID_SEPARATORS.test(id) && id.trim() === id;
79
125
  }
80
126
 
127
+ /**
128
+ * The STRICT grammar: a whitelist of what a capability id may look like, not a blocklist of what it may not.
129
+ *
130
+ * **Two consumers, and they are two different reasons for the same rule.** It lives here rather than in
131
+ * either of them because it shipped in one and was needed by both:
132
+ *
133
+ * 1. `skill-packages.ts` — the boundary that GENERATES a grant. R-77/R-78: a package declaring
134
+ * `allowed-tools: Read,ext:x";touch /tmp/pwned;PI_GRANTS_GRANT="` produced a `.pi/grants.env` that ran
135
+ * arbitrary code the moment an operator sourced the line `init` prints.
136
+ * 2. `workspace.ts` — the operator registry, since ADR-0035 made a registry id the tail of a capability id
137
+ * (`workspace:<id>`). That made the registry an input to this grammar, and it got the LOOSE
138
+ * `isWellFormedCapability` blocklist instead, which review showed costs three things:
139
+ * an id of literally `*` loaded and minted `WORKSPACE_WILDCARD`, so an operator naming ONE worktree held
140
+ * routing authority over every id in the registry including ones added later;
141
+ * an id containing a space became TWO capabilities, because `ceilingForDefinition` splits `allowed-tools`
142
+ * on `[\s,]+` — `allowed-tools: read, workspace:prod bash` measured as
143
+ * `['tool:bash','tool:read','workspace:prod']`, i.e. routing over production plus a shell, neither typed
144
+ * by anyone (0.18.1's comma, one namespace over);
145
+ * and quote/`$()`/backtick ids reached the `ROUTABLE WORKSPACES` block of the generated file, whose own
146
+ * instructions tell the operator to paste them into `PI_GRANTS_GRANT`.
147
+ *
148
+ * **A blocklist was the wrong shape for this and the comment on `isWellFormedCapability` says why it is the
149
+ * right shape THERE**: that one guards the enforcement path, which must keep accepting whatever ids operators
150
+ * already have. A registry is a file the operator writes and can rename, so refusing a hostile id costs
151
+ * nothing — and `grant-env.ts`'s standing warning applies to it exactly: *"the third channel — whatever it
152
+ * turns out to be — should cost a refusal rather than an injection."* The registry was the third channel.
153
+ */
154
+ /**
155
+ * A workspace registry id, which is NOT a tool name and needed its own rule.
156
+ *
157
+ * `isSafeCapability` was reused here first, and that was the error: it is the grammar for a *tool* name, and
158
+ * it refused ids that published 0.18.1 accepted — `feature/x` above all. Git worktrees are routinely named
159
+ * after their branch, so a slash is the ordinary case, and a slash splits nothing: not the comma-separated
160
+ * grant, not `allowed-tools`' `[\s,]+`. Refusing it bought no safety and broke the common setup.
161
+ *
162
+ * What is refused, and why each one earns it:
163
+ * - **whitespace** — `ceilingForDefinition` splits `allowed-tools` on `[\s,]+`, so `workspace:prod bash`
164
+ * measured as `['tool:bash','tool:read','workspace:prod']`: routing over production plus a shell, neither
165
+ * typed by anyone. 0.18.1's comma, one namespace over.
166
+ * - **comma, CR, LF, NUL** — split a grant.
167
+ * - **`*`** — collides with `WORKSPACE_WILDCARD`, so registering a worktree as `*` and granting
168
+ * `workspace:*` believing it named that one root minted routing authority over the whole registry.
169
+ * - **quotes, `$`, backticks, `;`, `&`, `|`, `<`, `>`, `(`, `)`, `\`, `#`** — reach the ROUTABLE WORKSPACES
170
+ * block of a generated `.pi/grants.env`, whose own instructions tell the operator to paste the id into
171
+ * `PI_GRANTS_GRANT`. Sourcing the file was safe; following its instructions executed. R-77/R-78.
172
+ * - **control characters and non-ASCII** — the generated file is reviewed in an editor and `/grants` prints
173
+ * these; backspace and ANSI escapes let one id render as another. This is the one refusal that costs a
174
+ * legitimate user something (a non-English worktree name), and it is a deliberate trade recorded as
175
+ * breaking rather than asserted to cost nothing.
176
+ */
177
+ export function isSafeWorkspaceId(id: string): boolean {
178
+ return /^[A-Za-z0-9][A-Za-z0-9._/-]*$/.test(id);
179
+ }
180
+
181
+ export function isSafeCapability(id: Capability): boolean {
182
+ const segment = "[A-Za-z0-9][A-Za-z0-9._-]*";
183
+ // `workspace:` delegates to the WORKSPACE grammar rather than reusing `segment`, and that is the fifth
184
+ // site of the same defect. `isSafeWorkspaceId` allows a slash because a git worktree is routinely named
185
+ // after its branch; `segment` does not, so this function refused `workspace:feature/x` — dropping the whole
186
+ // definition, exiting 1, and telling the operator "a capability id is tool:/skill:/agent:<name> or
187
+ // ext:<pkg>/<tool>", which does not even mention the namespace. Meanwhile the CHANGELOG told them slashes
188
+ // are fine. **The boundary that GENERATES grants could not emit the id shape the release advertises**, so
189
+ // the migration path for a breaking change was closed by the commit that documented it as open.
190
+ if (id.startsWith("workspace:")) return isSafeWorkspaceId(id.slice("workspace:".length));
191
+ return (
192
+ new RegExp(`^(tool|skill|agent):${segment}$`).test(id) ||
193
+ new RegExp(`^ext:(@${segment}/)?${segment}/${segment}$`).test(id)
194
+ );
195
+ }
196
+
81
197
  /**
82
198
  * The write-side backstop, for every channel that joins capabilities into one string.
83
199
  *
package/src/catalog.ts CHANGED
@@ -23,9 +23,11 @@ import { homedir } from "node:os";
23
23
  import { join } from "node:path";
24
24
  import { loadDefinitions, type SkillDefinition } from "./definitions.ts";
25
25
  import { PI_BUILTIN_TOOLS, WILDCARD } from "./pi-tools.ts";
26
- import { AGENT_WILDCARD, type Capability } from "./resolve.ts";
26
+ import { AGENT_WILDCARD, WORKSPACE_WILDCARD, type Capability } from "./resolve.ts";
27
+ import { loadWorkspaceRegistry, type WorkspaceRegistryFile } from "./workspace.ts";
28
+ import { isSafeWorkspaceId } from "./capabilities.ts";
27
29
 
28
- export type CapabilityKind = "builtin" | "extension" | "skill" | "agentType";
30
+ export type CapabilityKind = "builtin" | "extension" | "skill" | "agentType" | "workspace";
29
31
 
30
32
  export interface CatalogEntry {
31
33
  capability: Capability;
@@ -112,6 +114,22 @@ export function definitionEntries(definitions: Map<string, SkillDefinition>): Ca
112
114
  }));
113
115
  }
114
116
 
117
+ /**
118
+ * Registered workspaces, as `workspace:<id>` capabilities (ADR-0035).
119
+ *
120
+ * For DISPLAY and SCAFFOLDING only — `/grants` listing what this session may route to, and `init` offering
121
+ * the ids without choosing among them (ADR-0028). It is deliberately **not** what `unknownCapabilities`
122
+ * checks against; see the comment there.
123
+ *
124
+ * The operator registry is the authority on which ids exist, exactly as it is at `resolveWorkspace`. This
125
+ * enumerates it; it does not decide anything.
126
+ */
127
+ export function workspaceEntries(registry: WorkspaceRegistryFile, source?: string): CatalogEntry[] {
128
+ return Object.keys(registry.workspaces)
129
+ .sort()
130
+ .map((id) => ({ capability: `workspace:${id}` as Capability, kind: "workspace" as const, ...(source ? { source } : {}) }));
131
+ }
132
+
115
133
  /** Assemble a catalog from parts. Pure, so it is testable without a filesystem. */
116
134
  export function makeCatalog(entries: CatalogEntry[]): Catalog {
117
135
  const deduped = new Map<Capability, CatalogEntry>();
@@ -131,8 +149,23 @@ export function makeCatalog(entries: CatalogEntry[]): Catalog {
131
149
  export async function buildCatalog(input: {
132
150
  cwd: string;
133
151
  observedTools: string[] | null;
152
+ /** Operator workspace registry (`PI_GRANTS_WORKSPACE_REGISTRY`). Absent or unreadable yields no entries. */
153
+ registryPath?: string;
134
154
  }): Promise<Catalog> {
135
- const [skills, definitions] = await Promise.all([loadSkills(input.cwd), loadDefinitions(input.cwd)]);
155
+ const [skills, definitions, workspaces] = await Promise.all([
156
+ loadSkills(input.cwd),
157
+ loadDefinitions(input.cwd),
158
+ // Fails SOFT, and only because nothing here is an authority. A malformed registry must not stop a
159
+ // session from starting — `loadWorkspaceRegistry` throws a GovernanceRefusal naming the file, and that
160
+ // refusal is the operator's signal at the point of USE, where routing actually depends on it. Swallowing
161
+ // it there would be unsafe; swallowing it here costs a display list.
162
+ input.registryPath
163
+ ? loadWorkspaceRegistry(input.registryPath).then(
164
+ (r) => workspaceEntries(r, input.registryPath),
165
+ () => [] as CatalogEntry[],
166
+ )
167
+ : Promise.resolve([] as CatalogEntry[]),
168
+ ]);
136
169
  return makeCatalog([
137
170
  // pi's built-ins are seeded unconditionally, because they are known statically and the catalog is
138
171
  // consulted BEFORE any provider request has happened — `/grants` runs at that point. Without this,
@@ -148,6 +181,7 @@ export async function buildCatalog(input: {
148
181
  ...(input.observedTools ? classifyToolNames(input.observedTools) : []),
149
182
  ...skills,
150
183
  ...definitionEntries(definitions),
184
+ ...workspaces,
151
185
  ]);
152
186
  }
153
187
 
@@ -165,7 +199,31 @@ export function unknownCapabilities(requested: Capability[], catalog: Catalog):
165
199
  // refused it BEFORE `resolve` could apply ADR-0023's rule. That made the ADR's "a parent holding
166
200
  // `agent:*` may hand down `agent:*`" false, and made a definition declaring `allowed-tools: agent:*`
167
201
  // unspawnable from any grant. The wildcard is live only at the root without this.
168
- return requested.filter((c) => c !== WILDCARD && c !== AGENT_WILDCARD && !catalog.has(c)).sort();
202
+ //
203
+ // `workspace:` is exempt as a NAMESPACE, not merely at its wildcard, and that asymmetry is deliberate.
204
+ // ADR-0035 minted the namespace and taught `normaliseCapability`, `resolve` and `childEnv` about it, but
205
+ // not this line — so with a catalog present (and `delegationContext` always supplies one) every requested
206
+ // `workspace:<id>` was refused UNKNOWN_TOOL as *"a typo, or an uninstalled package"*. A child could
207
+ // therefore never be granted a workspace capability at all, which made routing stop dead below the root
208
+ // instead of attenuating, and made the ADR's own "two authorities, not one" unreachable in production.
209
+ //
210
+ // Exempt rather than catalogued-and-checked because the operator registry is the authority and it is
211
+ // consulted where it matters: `resolveWorkspace` refuses an unregistered id with WORKSPACE_NOT_REGISTERED,
212
+ // naming the registry. A second, weaker check here can only turn that precise refusal into a misleading
213
+ // one — and it would do so for reasons that have nothing to do with the id, like a registry this session
214
+ // cannot read. `buildCatalog` still ENUMERATES workspaces, for `/grants` and for `init`'s scaffold; that
215
+ // is display, and display is not authority. Same trade-off the built-ins comment above states and accepts.
216
+ // The workspace exemption requires a WELL-FORMED id, not merely the prefix. A bare `workspace:` names
217
+ // nothing, and exempting it let it reach a child's grant and the ledger as authority over no workspace at
218
+ // all — an exemption for ids the registry is authoritative about should not also cover ids no registry
219
+ // could contain.
220
+ // `WORKSPACE_WILDCARD` is listed with the other two because it is GRAMMAR, and `isSafeCapability` refuses
221
+ // wildcards by design — so folding it into the namespace test below un-exempts it. Caught by the tests for
222
+ // the previous two fixes, which is the checklist paying for itself.
223
+ const exempt = (c: Capability) =>
224
+ c === WILDCARD || c === AGENT_WILDCARD || c === WORKSPACE_WILDCARD
225
+ || (c.startsWith("workspace:") && isSafeWorkspaceId(c.slice("workspace:".length)));
226
+ return requested.filter((c) => !exempt(c) && !catalog.has(c)).sort();
169
227
  }
170
228
 
171
229
  /**
@@ -5,9 +5,8 @@ import { basename, isAbsolute, join } from "node:path";
5
5
  import { normaliseCorrelation, type CorrelationMetadata } from "./correlation.ts";
6
6
  import {
7
7
  appendLedgerEvent,
8
+ buildCheckReceiptLedgerEvent,
8
9
  buildWorkspaceLeaseEvent,
9
- type CheckReceiptLedgerEvent,
10
- LEDGER_VERSION,
11
10
  } from "./ledger.ts";
12
11
  import { computeGitCandidateIdentity } from "./git-identity.ts";
13
12
  import { runChild } from "./run-child.ts";
@@ -272,12 +271,13 @@ export async function runNamedCheck(input: {
272
271
  };
273
272
  const receipt: CheckReceipt = { receipt_id: receiptId(body), ...body };
274
273
  if (input.ledgerPath) {
275
- const event: CheckReceiptLedgerEvent = {
276
- ledgerVersion: LEDGER_VERSION, event: "check_receipt", ts: ended.toISOString(), childId: ownerId,
277
- receiptId: receipt.receipt_id, workspaceId: input.workspace.workspaceId, checkId: input.checkId,
278
- treeSha: receipt.tree_sha, ...(correlation ? { correlation } : {}),
279
- };
280
- await appendLedgerEvent({ path: input.ledgerPath, strict: true }, event);
274
+ await appendLedgerEvent(
275
+ { path: input.ledgerPath, strict: true },
276
+ buildCheckReceiptLedgerEvent({
277
+ childId: ownerId, receiptId: receipt.receipt_id, workspaceId: input.workspace.workspaceId,
278
+ checkId: input.checkId, treeSha: receipt.tree_sha, correlation, now: ended,
279
+ }),
280
+ );
281
281
  }
282
282
  return { output: result.text, exitCode: result.code, signal: result.signal ?? null, receipt };
283
283
  } catch (error) {
package/src/cli.ts CHANGED
@@ -19,6 +19,7 @@ import { relative, resolve as resolvePath } from "node:path";
19
19
  import { pathToFileURL } from "node:url";
20
20
  import { UnsafeGrantError } from "./grant-env.ts";
21
21
  import { applyInit, countDeclaring, planInit, type InitPlan } from "./init.ts";
22
+ import { registeredWorkspaceIds } from "./workspace.ts";
22
23
  import { discoverSkillPackages, skillPackageRoots, type RefusedSkill, type SkillPackage } from "./skill-packages.ts";
23
24
 
24
25
  const USAGE = `pi-daddy — capability governance for pi sub-agents
@@ -109,7 +110,7 @@ async function init(cwd: string, force: boolean): Promise<number> {
109
110
 
110
111
  let plan: InitPlan;
111
112
  try {
112
- plan = planInit(packages, cwd);
113
+ plan = planInit(packages, cwd, await registeredWorkspaceIds());
113
114
  } catch (error) {
114
115
  // R-78's backstop reaching the surface. Nothing is written: a grant that could mean something to a
115
116
  // shell is not a grant, and half-scaffolding a project would be worse than scaffolding none of it.
@@ -210,6 +211,28 @@ function report(plan: InitPlan): void {
210
211
  );
211
212
  }
212
213
 
214
+ // ROUTING, which had no line here at all — and `report()` is the output of the command the docs tell an
215
+ // operator to run. Keeping `workspace:` ids out of `withheldCapabilities` (so the `/grants init` dialog
216
+ // could not grant them off a package declaration) removed the ONLY thing this path said about a definition
217
+ // that cannot be spawned: `needs-withheld` is not one of the cases handled above, so a routing package
218
+ // produced a copied definition, an unusable grant, and total silence.
219
+ //
220
+ // The fix that caused it argued that "a breaking change whose migration is only discoverable by opening a
221
+ // file is not much of a migration" — and then applied that to the in-session notify and not to the CLI.
222
+ if (plan.routableWorkspaces.length > 0) {
223
+ const blocked = plan.skills.filter((s) => s.withheld === "needs-withheld").map((s) => s.name);
224
+ console.log(
225
+ `\nROUTABLE WORKSPACES: ${plan.routableWorkspaces.join(", ")}.\n` +
226
+ `Routing a child to one needs its id in PI_GRANTS_GRANT (ADR-0035); without it the delegation is\n` +
227
+ `refused WORKSPACE_NOT_AUTHORIZED. They are listed COMMENTED in .pi/grants.env and never granted for\n` +
228
+ `you — which worktree a child starts in is not something a package can declare.` +
229
+ (blocked.length > 0
230
+ ? `\nUntil you grant one, these cannot be spawned: ${blocked.join(", ")} — add the capability, then\n` +
231
+ `their \`agent:\` ids.`
232
+ : ""),
233
+ );
234
+ }
235
+
213
236
  console.log(
214
237
  `\nLive grant (${plan.grant.length} capabilities): ${plan.grant.join(", ")}\n\n` +
215
238
  ` $EDITOR .pi/grants.env # review it, then commit it\n` +