@pugi/cli 0.1.0-beta.2 → 0.1.0-beta.20

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/THIRD_PARTY_NOTICES.md +40 -0
  2. package/assets/pugi-mascot.ansi +15 -40
  3. package/bin/run.js +33 -1
  4. package/dist/commands/jobs-watch.js +201 -0
  5. package/dist/commands/jobs.js +15 -0
  6. package/dist/core/agent-progress/cleanup.js +134 -0
  7. package/dist/core/agent-progress/schema.js +144 -0
  8. package/dist/core/agent-progress/writer.js +101 -0
  9. package/dist/core/compact/auto-trigger.js +96 -0
  10. package/dist/core/compact/buffer-rewriter.js +115 -0
  11. package/dist/core/compact/summarizer.js +196 -0
  12. package/dist/core/compact/token-counter.js +108 -0
  13. package/dist/core/consensus/diff-capture.js +73 -0
  14. package/dist/core/context/index.js +7 -0
  15. package/dist/core/context/markdown-traverse.js +255 -0
  16. package/dist/core/cost/rate-card.js +129 -0
  17. package/dist/core/cost/tracker.js +221 -0
  18. package/dist/core/denial-tracking/index.js +8 -0
  19. package/dist/core/denial-tracking/state.js +264 -0
  20. package/dist/core/diagnostics/probe-runner.js +93 -0
  21. package/dist/core/diagnostics/probes/api.js +46 -0
  22. package/dist/core/diagnostics/probes/auth.js +86 -0
  23. package/dist/core/diagnostics/probes/cli-version.js +127 -0
  24. package/dist/core/diagnostics/probes/config.js +72 -0
  25. package/dist/core/diagnostics/probes/denial-tracking.js +57 -0
  26. package/dist/core/diagnostics/probes/disk.js +81 -0
  27. package/dist/core/diagnostics/probes/git.js +65 -0
  28. package/dist/core/diagnostics/probes/mcp.js +75 -0
  29. package/dist/core/diagnostics/probes/node.js +59 -0
  30. package/dist/core/diagnostics/probes/pnpm.js +36 -0
  31. package/dist/core/diagnostics/probes/session.js +74 -0
  32. package/dist/core/diagnostics/probes/status-snapshot.js +442 -0
  33. package/dist/core/diagnostics/probes/workspace.js +63 -0
  34. package/dist/core/diagnostics/types.js +70 -0
  35. package/dist/core/edits/dispatch.js +218 -2
  36. package/dist/core/edits/journal.js +199 -0
  37. package/dist/core/edits/layer-d-ast.js +557 -14
  38. package/dist/core/edits/verify-hook.js +273 -0
  39. package/dist/core/edits/worktree.js +111 -18
  40. package/dist/core/engine/anvil-client.js +115 -5
  41. package/dist/core/engine/budgets.js +89 -0
  42. package/dist/core/engine/context-prefix.js +155 -0
  43. package/dist/core/engine/intent.js +260 -0
  44. package/dist/core/engine/native-pugi.js +744 -210
  45. package/dist/core/engine/prompts.js +61 -6
  46. package/dist/core/engine/strip-internal-fields.js +124 -0
  47. package/dist/core/engine/tool-bridge.js +818 -31
  48. package/dist/core/file-cache.js +113 -1
  49. package/dist/core/init/scaffold.js +195 -0
  50. package/dist/core/lsp/client.js +174 -29
  51. package/dist/core/mcp/client.js +75 -6
  52. package/dist/core/mcp/http-server.js +553 -0
  53. package/dist/core/mcp/permission.js +190 -0
  54. package/dist/core/mcp/registry.js +24 -2
  55. package/dist/core/mcp/server-tools.js +219 -0
  56. package/dist/core/mcp/server.js +397 -0
  57. package/dist/core/permissions/gate.js +187 -0
  58. package/dist/core/permissions/index.js +18 -0
  59. package/dist/core/permissions/mode.js +102 -0
  60. package/dist/core/permissions/state.js +160 -0
  61. package/dist/core/permissions/tool-class.js +93 -0
  62. package/dist/core/repl/codebase-survey.js +308 -0
  63. package/dist/core/repl/history.js +11 -1
  64. package/dist/core/repl/init-interview.js +457 -0
  65. package/dist/core/repl/model-pricing.js +135 -0
  66. package/dist/core/repl/onboarding-state.js +297 -0
  67. package/dist/core/repl/session.js +719 -29
  68. package/dist/core/repl/slash-commands.js +133 -9
  69. package/dist/core/retry-budget/budget.js +284 -0
  70. package/dist/core/retry-budget/index.js +5 -0
  71. package/dist/core/settings.js +71 -0
  72. package/dist/core/skills/defaults.js +457 -0
  73. package/dist/core/subagents/dispatcher-real.js +600 -0
  74. package/dist/core/subagents/dispatcher.js +113 -24
  75. package/dist/core/subagents/index.js +18 -5
  76. package/dist/core/subagents/isolation-matrix.js +213 -0
  77. package/dist/core/subagents/spawn.js +19 -4
  78. package/dist/core/transport/version-interceptor.js +166 -0
  79. package/dist/index.js +28 -0
  80. package/dist/runtime/bootstrap.js +190 -0
  81. package/dist/runtime/cli.js +1588 -266
  82. package/dist/runtime/commands/compact.js +296 -0
  83. package/dist/runtime/commands/cost.js +199 -0
  84. package/dist/runtime/commands/delegate.js +289 -0
  85. package/dist/runtime/commands/doctor.js +369 -0
  86. package/dist/runtime/commands/lsp.js +187 -5
  87. package/dist/runtime/commands/mcp.js +824 -0
  88. package/dist/runtime/commands/patch.js +17 -0
  89. package/dist/runtime/commands/permissions.js +87 -0
  90. package/dist/runtime/commands/report.js +299 -0
  91. package/dist/runtime/commands/review-consensus.js +17 -2
  92. package/dist/runtime/commands/roster.js +117 -0
  93. package/dist/runtime/commands/status.js +178 -0
  94. package/dist/runtime/commands/worktree.js +50 -6
  95. package/dist/runtime/headless.js +543 -0
  96. package/dist/runtime/load-hooks-or-exit.js +71 -0
  97. package/dist/runtime/plan-decompose.js +531 -0
  98. package/dist/runtime/version.js +65 -0
  99. package/dist/tools/agent-tool.js +206 -0
  100. package/dist/tools/apply-patch.js +281 -39
  101. package/dist/tools/ask-user-question.js +213 -0
  102. package/dist/tools/ask-user.js +115 -0
  103. package/dist/tools/file-tools.js +85 -14
  104. package/dist/tools/mcp-tool.js +260 -0
  105. package/dist/tools/multi-edit.js +361 -0
  106. package/dist/tools/registry.js +22 -2
  107. package/dist/tools/skill-tool.js +96 -0
  108. package/dist/tools/tasks.js +208 -0
  109. package/dist/tools/web-fetch.js +147 -2
  110. package/dist/tools/web-search.js +458 -0
  111. package/dist/tui/agent-progress-card.js +111 -0
  112. package/dist/tui/agent-tree.js +10 -0
  113. package/dist/tui/ask-modal.js +2 -2
  114. package/dist/tui/ask-user-question-prompt.js +192 -0
  115. package/dist/tui/compact-banner.js +54 -0
  116. package/dist/tui/conversation-pane.js +69 -8
  117. package/dist/tui/cost-table.js +111 -0
  118. package/dist/tui/doctor-table.js +31 -0
  119. package/dist/tui/input-box.js +1 -1
  120. package/dist/tui/markdown-render.js +4 -4
  121. package/dist/tui/repl-render.js +276 -37
  122. package/dist/tui/repl-splash.js +2 -2
  123. package/dist/tui/repl.js +25 -6
  124. package/dist/tui/splash.js +1 -1
  125. package/dist/tui/status-bar.js +94 -16
  126. package/dist/tui/status-table.js +7 -0
  127. package/dist/tui/tool-stream-pane.js +7 -0
  128. package/dist/tui/update-banner.js +20 -2
  129. package/docs/examples/codegraph.mcp.json +10 -0
  130. package/package.json +9 -6
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Subagent dispatcher (Sprint a5.4 M1 gap remediation D).
2
+ * Subagent dispatcher (β2 S1 + S4 2026-05-26).
3
3
  *
4
4
  * The dispatcher is the runtime side of the @pugi/sdk subagent contracts.
5
5
  * Given a SubagentTask, it:
@@ -7,25 +7,30 @@
7
7
  * 1. Resolves the role to a Cyber-Zoo persona via the local registry
8
8
  * (apps/pugi-cli/src/core/agents/registry.ts, which itself sources
9
9
  * @pugi/personas).
10
- * 2. Classifies isolation per the M1 matrix (see isolationForRole).
10
+ * 2. Classifies isolation per the matrix (see isolationForRole).
11
11
  * 3. Builds the dispatch-time permission overrides (Vera as reviewer
12
12
  * or verifier loses every edit/write/bash class — see
13
13
  * permissionOverridesForRole).
14
14
  * 4. Emits subagent.spawned into the session events log.
15
- * 5. Runs the dispatch (M1: stub returning shipped immediately so the
16
- * contract surface is exercisable; M2+ swaps the body for
17
- * worktree-isolated execution backed by runEngineLoop).
15
+ * 5. Drives the dispatch via one of two backends:
16
+ * - REAL (β2 S1): when ctx carries an EngineLoopClient, the child
17
+ * runs a genuine `runEngineLoop` against Anvil with a per-child
18
+ * tools schema gated by the isolation-matrix capability map
19
+ * (β2 S4). See `dispatcher-real.ts::runRealDispatch`.
20
+ * - STUB (M1 legacy): when no engine client is supplied, the
21
+ * dispatcher returns a `shipped` result with zero metrics so
22
+ * the legacy `inMemoryDispatcherContext` test path stays green.
23
+ * This is the back-compat door for the M1 spec coverage.
18
24
  * 6. Emits subagent.completed | blocked | failed into the session
19
- * events log.
25
+ * events log (real backend emits richer details; stub emits the
26
+ * M1-compatible shape).
20
27
  * 7. Returns the typed SubagentResult.
21
28
  *
22
- * Why a stub at M1: the contract surface itself, the event emission, the
23
- * isolation classification, and the permission overrides are real
24
- * load-bearing pieces the cabinet UI, audit replay, and triple-review
25
- * gating all read these events. The model-driven loop that actually
26
- * spawns a separate Anvil session is alpha-5.7 work (REPL-by-default).
27
- * The stub returns a shipped result with the correct persona slug + role
28
- * pair so downstream consumers can wire against the real shape.
29
+ * Why we kept the stub path: the existing M1 spec coverage exercises
30
+ * the dispatcher's contract surface (role-to-persona, isolation tier,
31
+ * permission overrides, event ordering) without any HTTP transport.
32
+ * β2 must not regress that surface every existing assertion still
33
+ * holds for in-memory contexts. The real backend is a strict superset.
29
34
  *
30
35
  * The dispatcher is the only place that knows the isolation matrix and
31
36
  * the permission overrides. Both surfaces are exported so engine adapter
@@ -164,19 +169,51 @@ export function budgetForRole(role, override) {
164
169
  };
165
170
  }
166
171
  /* ------------------------------------------------------------------ */
167
- /* Dispatch */
172
+ /* Real-backend lazy import (memoized) */
168
173
  /* ------------------------------------------------------------------ */
169
174
  /**
170
- * Spawn a subagent. M1 implementation is a stub that synchronously
171
- * returns a shipped result so the contract surface is exercised by
172
- * tests and the cabinet UI. M2+ replaces the body with an Anvil-side
173
- * dispatch over a per-task worktree (ADR-0057, deferred).
175
+ * β2a r1 (Backend Architect P1, 2026-05-26): cached lazy-import of
176
+ * the real dispatch backend. Hoisting the dynamic import to
177
+ * module scope (instead of running it on every dispatch call) means
178
+ * the first agent spawn does not pay 50-200ms cold-start latency.
174
179
  *
175
- * The function still emits real subagent.spawned and subagent.completed
176
- * events; downstream consumers (audit replay, cabinet activity feed,
177
- * eval harness) cannot tell the stub apart from a real dispatch on the
178
- * event surface alone, which is the property we want for forward-
179
- * compatibility testing.
180
+ * The cache is a Promise so concurrent first-callers share one
181
+ * import; ESM's own module cache also dedups but the Promise wrapper
182
+ * lets `prewarmRealDispatch` kick off the import without awaiting.
183
+ */
184
+ let realDispatchPromise = null;
185
+ function ensureRealDispatch() {
186
+ if (!realDispatchPromise) {
187
+ realDispatchPromise = import('./dispatcher-real.js');
188
+ }
189
+ return realDispatchPromise;
190
+ }
191
+ /**
192
+ * β2a r1: pre-warm the real dispatcher's module graph. Called by the
193
+ * engine adapter (`NativePugiEngineAdapter`) at construction time
194
+ * when an engine client is wired, so the first `dispatch()` call
195
+ * with `ctx.engineClient` set returns instantly. Safe to call
196
+ * multiple times — subsequent calls hit the cached promise.
197
+ */
198
+ export function prewarmRealDispatch() {
199
+ return ensureRealDispatch();
200
+ }
201
+ /**
202
+ * Spawn a subagent. Two backends:
203
+ *
204
+ * - REAL (β2 S1): when `ctx.engineClient` is set, the dispatcher
205
+ * spawns a genuine child engine loop. See `dispatcher-real.ts`.
206
+ * The child's tool surface is filtered by the isolation matrix
207
+ * (β2 S4) so a `researcher` role cannot see `write`/`edit`/`bash`
208
+ * in its tools schema and the executor refuses if the model
209
+ * fabricates a call.
210
+ *
211
+ * - STUB (M1 legacy): when no engine client is supplied, the
212
+ * dispatcher returns a `shipped` result with zero metrics. This
213
+ * is the back-compat door for the M1 spec coverage and for
214
+ * in-memory consumers that only want to assert the dispatcher's
215
+ * CONTRACT surface (role-to-persona, isolation tier, permission
216
+ * overrides, event ordering) without standing up Anvil.
180
217
  *
181
218
  * The function rejects with ZodError when the task fails schema
182
219
  * validation. Throwing rather than returning a failed result is the
@@ -186,6 +223,58 @@ export function budgetForRole(role, override) {
186
223
  */
187
224
  export async function dispatch(task, ctx) {
188
225
  const validated = subagentTaskSchema.parse(task);
226
+ if (ctx.engineClient) {
227
+ // β2a r1 (Backend Architect P1, 2026-05-26): the lazy import
228
+ // chain (worktree + engine SDK graph) cost 50-200ms on the FIRST
229
+ // dispatch call. `ensureRealDispatch` memoizes the promise so the
230
+ // import happens at most once per process; subsequent dispatches
231
+ // hit the cached promise instantly. Production callers should
232
+ // prewarm via `prewarmRealDispatch()` at engine adapter init so
233
+ // the operator never pays cold-start on the first agent call.
234
+ const { runRealDispatch } = await ensureRealDispatch();
235
+ const outcome = await runRealDispatch(validated, {
236
+ sessionId: ctx.sessionId,
237
+ workspaceRoot: ctx.workspaceRoot,
238
+ appendEvent: ctx.appendEvent,
239
+ ...(ctx.now ? { now: ctx.now } : {}),
240
+ engineClient: ctx.engineClient,
241
+ ...(ctx.commandKind ? { commandKind: ctx.commandKind } : {}),
242
+ ...(ctx.useWorktreeIsolation !== undefined
243
+ ? { useWorktreeIsolation: ctx.useWorktreeIsolation }
244
+ : {}),
245
+ ...(ctx.signal ? { signal: ctx.signal } : {}),
246
+ });
247
+ return outcome.result;
248
+ }
249
+ return runStubDispatch(validated, ctx);
250
+ }
251
+ /**
252
+ * Real-backend variant that also surfaces the optional worktree
253
+ * handle. Callers that need to promote/drop the scratch worktree
254
+ * (e.g. the REPL `/agent` surface, or the Agent tool dispatcher) use
255
+ * this entry point.
256
+ */
257
+ export async function dispatchWithOutcome(task, ctx) {
258
+ const validated = subagentTaskSchema.parse(task);
259
+ if (ctx.engineClient) {
260
+ const { runRealDispatch } = await ensureRealDispatch();
261
+ return runRealDispatch(validated, {
262
+ sessionId: ctx.sessionId,
263
+ workspaceRoot: ctx.workspaceRoot,
264
+ appendEvent: ctx.appendEvent,
265
+ ...(ctx.now ? { now: ctx.now } : {}),
266
+ engineClient: ctx.engineClient,
267
+ ...(ctx.commandKind ? { commandKind: ctx.commandKind } : {}),
268
+ ...(ctx.useWorktreeIsolation !== undefined
269
+ ? { useWorktreeIsolation: ctx.useWorktreeIsolation }
270
+ : {}),
271
+ ...(ctx.signal ? { signal: ctx.signal } : {}),
272
+ });
273
+ }
274
+ const result = await runStubDispatch(validated, ctx);
275
+ return { result };
276
+ }
277
+ async function runStubDispatch(validated, ctx) {
189
278
  const persona = getPersonaForRole(validated.role);
190
279
  const isolation = isolationForRole(validated.role);
191
280
  void budgetForRole(validated.role, validated.budget);
@@ -233,7 +322,7 @@ export async function dispatch(task, ctx) {
233
322
  return result;
234
323
  }
235
324
  function stubSummaryFor(role, personaName) {
236
- return `${personaName} (${role}) dispatched: stub returning shipped (M1 contract surface only; real dispatch in alpha-5.7)`;
325
+ return `${personaName} (${role}) dispatched: in-memory stub backend (no engine client supplied; production callers should pass DispatcherContext.engineClient)`;
237
326
  }
238
327
  function defaultNow() {
239
328
  return new Date().toISOString();
@@ -14,13 +14,26 @@
14
14
  * would invite the kind of accidental drift the persona-registry
15
15
  * extraction was designed to prevent.
16
16
  */
17
- export { budgetForRole, dispatch, inMemoryDispatcherContext, isolationForRole, permissionOverridesForRole, } from './dispatcher.js';
17
+ export { budgetForRole, dispatch, dispatchWithOutcome, inMemoryDispatcherContext, isolationForRole, permissionOverridesForRole, } from './dispatcher.js';
18
+ /**
19
+ * β2 S4: per-role capability matrix. Surfaced via the barrel so
20
+ * engine adapter code, the Agent tool, and tests can introspect a
21
+ * role's allowed tool set without importing the matrix module
22
+ * directly.
23
+ */
24
+ export { allowedToolsForRole, capabilitiesForRole, roleHasToolAccess, ROLE_CAPABILITIES, } from './isolation-matrix.js';
25
+ /**
26
+ * β2 S1: real-backend entry point. Exposed for callers that want to
27
+ * drive the dispatch with the worktree handle in scope (e.g. the
28
+ * Agent tool, the REPL `/agent` surface). Most callers should prefer
29
+ * the `dispatch()` / `dispatchWithOutcome()` helpers above which
30
+ * route to this module when ctx.engineClient is set.
31
+ */
32
+ export { runRealDispatch } from './dispatcher-real.js';
18
33
  /**
19
34
  * Spawn a subagent from inside the engine adapter loop. Re-exported via
20
35
  * the barrel so engine code does not have to import the dispatcher
21
- * module directly. The actual task_dispatch tool that the model uses
22
- * to invoke a subagent lands in alpha-5.7 (REPL); for now the helper
23
- * exists so adapter code has a single seam to wire against.
36
+ * module directly.
24
37
  */
25
- export { spawnSubagent } from './spawn.js';
38
+ export { spawnSubagent, spawnSubagentWithOutcome } from './spawn.js';
26
39
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,213 @@
1
+ const CAP_READ_ONLY = new Set([
2
+ 'read',
3
+ 'task',
4
+ 'skill',
5
+ ]);
6
+ const CAP_VERIFIER = new Set([
7
+ 'read',
8
+ 'task',
9
+ 'skill',
10
+ // β2a r1 (Codex P1, 2026-05-26): verifier previously got the FULL
11
+ // `bash` capability. The class-aware bash tool defaults to
12
+ // permission mode `auto`, which permits `write_workspace` class
13
+ // commands (e.g. `echo x > src/file.ts`, `sed -i`, `rm`). That
14
+ // silently bypassed the no-edit/no-write contract — a verifier
15
+ // could mutate the workspace it was meant to read.
16
+ //
17
+ // The fix splits bash into two capabilities:
18
+ // - `bash` → full bash (writers only)
19
+ // - `bash_read_only` → bash gate that forces read-only classifier
20
+ // mode regardless of operator settings
21
+ // verifier needs the read-only flavor so test commands (pnpm test,
22
+ // jest --listFiles, typecheck) still work but a fabricated
23
+ // `echo x > file.ts` is refused at the executor layer.
24
+ 'bash_read_only',
25
+ ]);
26
+ const CAP_WRITER = new Set([
27
+ 'read',
28
+ 'write',
29
+ 'bash',
30
+ 'task',
31
+ 'skill',
32
+ 'ask_user',
33
+ ]);
34
+ const CAP_FULL = new Set([
35
+ 'read',
36
+ 'write',
37
+ 'bash',
38
+ 'task',
39
+ 'skill',
40
+ 'ask_user',
41
+ 'web_fetch',
42
+ 'agent',
43
+ ]);
44
+ /**
45
+ * Per-role capability map. Add a new role only when the matching
46
+ * isolation tier classification in dispatcher.ts agrees with the
47
+ * capability set here — drift would let a `coder` role get write
48
+ * privileges with `shared_fs_readonly` isolation, which would mean
49
+ * the dispatcher emits readonly-isolation events while the child
50
+ * actually writes. Always touch both files together.
51
+ */
52
+ export const ROLE_CAPABILITIES = new Map([
53
+ [
54
+ 'orchestrator',
55
+ {
56
+ role: 'orchestrator',
57
+ capabilities: CAP_FULL,
58
+ rationale: 'orchestrator (Pugi/Mira) runs in parent context with full toolset; '
59
+ + 'parent permissions still gate any actual mutation',
60
+ },
61
+ ],
62
+ [
63
+ 'architect',
64
+ {
65
+ role: 'architect',
66
+ capabilities: CAP_READ_ONLY,
67
+ rationale: 'architect role is read-only by design (analysis + planning, no mutations)',
68
+ },
69
+ ],
70
+ [
71
+ 'coder',
72
+ {
73
+ role: 'coder',
74
+ capabilities: CAP_WRITER,
75
+ rationale: 'coder role mutates the workspace via write + edit + bash',
76
+ },
77
+ ],
78
+ [
79
+ 'verifier',
80
+ {
81
+ role: 'verifier',
82
+ capabilities: CAP_VERIFIER,
83
+ rationale: 'verifier role reads workspace + executes verification commands (tests, typecheck) '
84
+ + 'but never edits the code it is verifying',
85
+ },
86
+ ],
87
+ [
88
+ 'reviewer',
89
+ {
90
+ role: 'reviewer',
91
+ capabilities: CAP_READ_ONLY,
92
+ rationale: 'reviewer role is read-only by policy (no edits to code under review); '
93
+ + 'shell is denied because reviewer should not be re-running tests',
94
+ },
95
+ ],
96
+ [
97
+ 'researcher',
98
+ {
99
+ role: 'researcher',
100
+ capabilities: CAP_READ_ONLY,
101
+ rationale: 'researcher role is read-only (corpus search + summarization)',
102
+ },
103
+ ],
104
+ [
105
+ 'release',
106
+ {
107
+ role: 'release',
108
+ capabilities: CAP_WRITER,
109
+ rationale: 'release role needs write + bash for changelog edits + version bumps',
110
+ },
111
+ ],
112
+ [
113
+ 'devops',
114
+ {
115
+ role: 'devops',
116
+ capabilities: CAP_WRITER,
117
+ rationale: 'devops role needs write + bash for infra config + deploy scripts',
118
+ },
119
+ ],
120
+ [
121
+ 'design_qa',
122
+ {
123
+ role: 'design_qa',
124
+ capabilities: CAP_WRITER,
125
+ rationale: 'design_qa role needs write + bash for UI tweaks + screenshot scripts',
126
+ },
127
+ ],
128
+ ]);
129
+ /**
130
+ * Resolve the capability set for a role. Throws when the role is not
131
+ * registered — the closed SubagentRole union prevents that at compile
132
+ * time for typed callers, but the runtime guard catches dynamic dispatch
133
+ * paths (e.g. a tag parsed off Mira's reply text).
134
+ */
135
+ export function capabilitiesForRole(role) {
136
+ const entry = ROLE_CAPABILITIES.get(role);
137
+ if (!entry) {
138
+ throw new Error(`capabilitiesForRole: unknown role '${role}'`);
139
+ }
140
+ return entry;
141
+ }
142
+ /**
143
+ * Map capability classes → concrete tool names (matches tool-bridge.ts
144
+ * WIRED_TOOLS). This is the bridge between the policy layer (this file)
145
+ * and the schema-shaping layer (tool-bridge buildToolsSchema). Keep in
146
+ * lockstep with WIRED_TOOLS — a new tool added to the bridge should
147
+ * be classified here so subagents see (or do not see) it consistently.
148
+ */
149
+ const CAPABILITY_TO_TOOLS = {
150
+ read: ['read', 'grep', 'glob'],
151
+ write: ['write', 'edit'],
152
+ bash: ['bash'],
153
+ // β2a r1 (2026-05-26): `bash_read_only` maps to the same `bash`
154
+ // tool name so the model sees only one tool surface. The
155
+ // dispatcher-real executor wraps the verifier's bash calls with a
156
+ // forced read-only classifier mode (see `gatedExecutor` in
157
+ // dispatcher-real.ts) so a `write_workspace`-class command is
158
+ // rejected before the tool runs even though the capability set
159
+ // appears to advertise `bash`.
160
+ bash_read_only: ['bash'],
161
+ task: ['task_create', 'task_get', 'task_list', 'task_update'],
162
+ skill: ['skill', 'skills_list'],
163
+ ask_user: ['ask_user_question'],
164
+ web_fetch: ['web_fetch'],
165
+ // Agent tool is the subagent spawn primitive itself (S3). Only the
166
+ // orchestrator role gets it — child agents cannot recursively spawn
167
+ // grand-children, which keeps the spawn depth bounded at 1 and the
168
+ // budget rollup tractable.
169
+ agent: ['agent'],
170
+ };
171
+ /**
172
+ * Return the set of tool names a role is allowed to call. Used by the
173
+ * per-child tool-bridge to shape the OpenAI tools schema AND by the
174
+ * executor refusal gate.
175
+ *
176
+ * The function is pure — same role in, same set out — so the schema
177
+ * builder can call it from inside `buildToolsSchema`.
178
+ */
179
+ export function allowedToolsForRole(role) {
180
+ const caps = capabilitiesForRole(role);
181
+ const out = new Set();
182
+ for (const cap of caps.capabilities) {
183
+ for (const name of CAPABILITY_TO_TOOLS[cap]) {
184
+ out.add(name);
185
+ }
186
+ }
187
+ return out;
188
+ }
189
+ /**
190
+ * Predicate: is a tool name reachable by a role under the capability
191
+ * matrix? Used by the executor's pre-dispatch refusal gate.
192
+ *
193
+ * Returns true for orchestrator/full-capability roles and for every
194
+ * specific tool the role's capability set unlocks; false otherwise.
195
+ */
196
+ export function roleHasToolAccess(role, toolName) {
197
+ return allowedToolsForRole(role).has(toolName);
198
+ }
199
+ /**
200
+ * β2a r1 (Codex P1, 2026-05-26): predicate identifying roles whose
201
+ * bash access is restricted to read-only classifier mode. Used by
202
+ * dispatcher-real.ts's gatedExecutor to force-flag bash dispatches as
203
+ * read-only regardless of the workspace's permission settings.
204
+ *
205
+ * A role qualifies when it holds `bash_read_only` but NOT the
206
+ * full-power `bash` capability — orchestrators (which inherit both
207
+ * via CAP_FULL) keep full bash access through the regular path.
208
+ */
209
+ export function bashIsReadOnlyForRole(role) {
210
+ const caps = capabilitiesForRole(role).capabilities;
211
+ return caps.has('bash_read_only') && !caps.has('bash');
212
+ }
213
+ //# sourceMappingURL=isolation-matrix.js.map
@@ -1,5 +1,5 @@
1
1
  import { recordSubagentBlocked, recordSubagentCompleted, recordSubagentFailed, recordSubagentSpawned, recordSubagentToolCall, } from '../session.js';
2
- import { dispatch } from './dispatcher.js';
2
+ import { dispatch, dispatchWithOutcome, } from './dispatcher.js';
3
3
  /**
4
4
  * Spawn a subagent under an existing PugiSession. Events are routed
5
5
  * through the session module's recorder functions; if the session is
@@ -7,13 +7,28 @@ import { dispatch } from './dispatcher.js';
7
7
  * dispatch still runs — the contract is "dispatch always works, audit
8
8
  * is best-effort".
9
9
  */
10
- export async function spawnSubagent(task, session) {
11
- const ctx = {
10
+ export async function spawnSubagent(task, session, options = {}) {
11
+ return dispatch(task, buildContext(session, options));
12
+ }
13
+ /**
14
+ * β2 S1: spawnSubagent variant that surfaces the optional worktree
15
+ * handle so the caller can wire promote/drop follow-ups.
16
+ */
17
+ export async function spawnSubagentWithOutcome(task, session, options = {}) {
18
+ return dispatchWithOutcome(task, buildContext(session, options));
19
+ }
20
+ function buildContext(session, options) {
21
+ return {
12
22
  sessionId: session.id,
13
23
  workspaceRoot: session.root,
14
24
  appendEvent: (event) => routeEvent(event, session),
25
+ ...(options.engineClient ? { engineClient: options.engineClient } : {}),
26
+ ...(options.commandKind ? { commandKind: options.commandKind } : {}),
27
+ ...(options.useWorktreeIsolation !== undefined
28
+ ? { useWorktreeIsolation: options.useWorktreeIsolation }
29
+ : {}),
30
+ ...(options.signal ? { signal: options.signal } : {}),
15
31
  };
16
- return dispatch(task, ctx);
17
32
  }
18
33
  function routeEvent(event, session) {
19
34
  if (!isRecord(event))
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Pugi CLI ↔ admin-api version handshake — CLI-side interceptor.
3
+ *
4
+ * Wraps the engine HTTP transport (and, via the same helpers, any
5
+ * other `fetch` call the CLI makes to admin-api) so the CLI:
6
+ *
7
+ * 1. Sends `X-Pugi-Cli-Version: <installed semver>` on every
8
+ * outbound request. The server middleware
9
+ * (`apps/admin-api/src/runtime/cli-version.middleware.ts`) reads
10
+ * this header and decides whether to honour, soft-warn, or 426.
11
+ *
12
+ * 2. Inspects every response for:
13
+ * - `X-Pugi-Cli-Upgrade-Recommended` → cache the server's
14
+ * recommendation so `UpdateBanner` can compare it against the
15
+ * npm-registry poll and show the operator the higher of the two.
16
+ * - `X-Pugi-Server-Version` → cache for diagnostics
17
+ * (`pugi doctor --json`).
18
+ *
19
+ * 3. Throws `PugiCliUpgradeRequiredError` when the server returns
20
+ * HTTP 426. The top-level catch in `index.ts` / `runtime/cli.ts`
21
+ * renders a clean upgrade banner + `process.exit(1)`.
22
+ *
23
+ * # Design: pure helpers, not a callable wrapper
24
+ *
25
+ * Wrapping fetch as a higher-order function would require every
26
+ * transport call site to opt in via `interceptedFetch(...)`. Instead
27
+ * we expose three small helpers that the existing transport classes
28
+ * (currently just `AnvilEngineLoopClient`) call inline:
29
+ *
30
+ * - `injectClientVersionHeader(headers)` — adds the X-Pugi-Cli-Version
31
+ * entry to an outbound header bag.
32
+ * - `inspectVersionResponse(response)` — reads recommended/server
33
+ * headers, updates the module cache. Returns void.
34
+ * - `assertNotUpgradeRequired(response)` — if status is 426, parses
35
+ * the JSON body and throws `PugiCliUpgradeRequiredError`. Returns
36
+ * void otherwise.
37
+ *
38
+ * That layout keeps the spec tests trivially focused on each branch and
39
+ * gives future SSE callers (engine-stream EventSource) the same hooks
40
+ * without touching the fetch wrapper indirection.
41
+ */
42
+ import { PUGI_CLI_UPGRADE_RECOMMENDED_HEADER, PUGI_CLI_VERSION_HEADER, PUGI_SERVER_VERSION_HEADER, } from '../../runtime/version.js';
43
+ /**
44
+ * Thrown when admin-api responds with HTTP 426 Upgrade Required. The
45
+ * top-level CLI catch (see `runtime/cli.ts`) renders this with the
46
+ * operator-friendly upgrade banner.
47
+ *
48
+ * Mirrors the server-side `CliUpgradeRequiredBody` shape; populated
49
+ * from the response JSON when present and from the request context
50
+ * (installed version) when the body parse fails.
51
+ */
52
+ export class PugiCliUpgradeRequiredError extends Error {
53
+ code = 'cli_upgrade_required';
54
+ installedVersion;
55
+ minClientVersion;
56
+ recommendedVersion;
57
+ upgradeCommand;
58
+ upgradeUrl;
59
+ constructor(details) {
60
+ super(details.message ??
61
+ `Pugi CLI ${details.installedVersion} is below the server minimum ${details.minClientVersion}. Upgrade with: ${details.upgradeCommand}`);
62
+ this.name = 'PugiCliUpgradeRequiredError';
63
+ this.installedVersion = details.installedVersion;
64
+ this.minClientVersion = details.minClientVersion;
65
+ this.recommendedVersion = details.recommendedVersion;
66
+ this.upgradeCommand = details.upgradeCommand;
67
+ this.upgradeUrl = details.upgradeUrl;
68
+ }
69
+ }
70
+ /**
71
+ * Module-level cache of the most recent server-recommended version.
72
+ * Read by `UpdateBanner` (via `getCachedServerRecommendation`) when
73
+ * computing the version it shows the operator. Kept in module scope
74
+ * (not class state) because both the engine client and the banner live
75
+ * in different module subtrees and there's no shared service container
76
+ * to thread DI through.
77
+ */
78
+ let cachedServerRecommendation = null;
79
+ let cachedServerVersion = null;
80
+ /**
81
+ * UpdateBanner reads this to merge with its npm-registry poll. Returns
82
+ * `null` when no Pugi response has been seen yet (very first REPL
83
+ * launch before the engine has been called).
84
+ */
85
+ export function getCachedServerRecommendation() {
86
+ return cachedServerRecommendation;
87
+ }
88
+ /**
89
+ * `pugi doctor --json` reads this for the server-version diagnostic
90
+ * field. Returns `null` when no response has been seen yet.
91
+ */
92
+ export function getCachedServerVersion() {
93
+ return cachedServerVersion;
94
+ }
95
+ /**
96
+ * Test seam — reset the cache between specs so cross-test leakage
97
+ * doesn't make assertions flaky. Not part of the public CLI surface.
98
+ */
99
+ export function __resetVersionCacheForTests() {
100
+ cachedServerRecommendation = null;
101
+ cachedServerVersion = null;
102
+ }
103
+ /**
104
+ * Mutate an outbound header bag to add the CLI version. Accepts both
105
+ * the plain-object header shape that `fetch` uses and Headers
106
+ * instances. Returns the same bag for chaining.
107
+ *
108
+ * Header names go in canonical capitalization since some
109
+ * test/intermediary tools (mitmproxy, charles) display the exact case
110
+ * the client sent rather than normalising to lowercase.
111
+ */
112
+ export function injectClientVersionHeader(headers, cliVersion) {
113
+ if (headers instanceof Headers) {
114
+ headers.set(PUGI_CLI_VERSION_HEADER, cliVersion);
115
+ return headers;
116
+ }
117
+ headers[PUGI_CLI_VERSION_HEADER] = cliVersion;
118
+ return headers;
119
+ }
120
+ /**
121
+ * Inbound-header inspection. Reads `X-Pugi-Cli-Upgrade-Recommended`
122
+ * and `X-Pugi-Server-Version` from the response and updates the
123
+ * module-level cache. Caller passes a `headers.get(...)` shim so this
124
+ * helper stays decoupled from the concrete response type — `Response`,
125
+ * `undici.Dispatcher.ResponseData`, or a stub in tests.
126
+ */
127
+ export function inspectVersionResponse(getHeader) {
128
+ const recommended = getHeader(PUGI_CLI_UPGRADE_RECOMMENDED_HEADER);
129
+ if (typeof recommended === 'string' && recommended.length > 0) {
130
+ cachedServerRecommendation = recommended;
131
+ }
132
+ const serverVersion = getHeader(PUGI_SERVER_VERSION_HEADER);
133
+ if (typeof serverVersion === 'string' && serverVersion.length > 0) {
134
+ cachedServerVersion = serverVersion;
135
+ }
136
+ }
137
+ /**
138
+ * If the response is HTTP 426, throw `PugiCliUpgradeRequiredError`.
139
+ * Otherwise return void. Caller is responsible for already having
140
+ * read the response body — pass it in so we can parse the
141
+ * documented JSON shape without consuming the stream a second time.
142
+ *
143
+ * `installedFallback` is used when the response body doesn't carry
144
+ * `installedVersion` (e.g. CDN-injected 426). Should be the CLI's own
145
+ * PUGI_CLI_VERSION constant.
146
+ */
147
+ export function assertNotUpgradeRequired(status, bodyText, installedFallback) {
148
+ if (status !== 426)
149
+ return;
150
+ let parsed = {};
151
+ try {
152
+ parsed = JSON.parse(bodyText);
153
+ }
154
+ catch {
155
+ // Body wasn't JSON — fall back to constants below.
156
+ }
157
+ throw new PugiCliUpgradeRequiredError({
158
+ installedVersion: parsed.installedVersion ?? installedFallback,
159
+ minClientVersion: parsed.minClientVersion ?? 'unknown',
160
+ recommendedVersion: parsed.recommendedVersion ?? 'unknown',
161
+ upgradeCommand: parsed.upgradeCommand ?? 'npm i -g @pugi/cli@latest',
162
+ upgradeUrl: parsed.upgradeUrl ?? 'https://www.npmjs.com/package/@pugi/cli',
163
+ message: parsed.message,
164
+ });
165
+ }
166
+ //# sourceMappingURL=version-interceptor.js.map
package/dist/index.js CHANGED
@@ -1,6 +1,34 @@
1
1
  #!/usr/bin/env node
2
2
  import { runCli } from './runtime/cli.js';
3
+ import { PugiCliUpgradeRequiredError } from './core/transport/version-interceptor.js';
3
4
  runCli(process.argv.slice(2)).catch((error) => {
5
+ // PR-CLI-SERVER-VERSION-HANDSHAKE (#225). When the admin-api returns
6
+ // 426 Upgrade Required, the engine transport throws a typed
7
+ // PugiCliUpgradeRequiredError. Render an operator-friendly banner
8
+ // (vs. the bland `pugi: <message>` default) so the upgrade command
9
+ // is obvious + copy-pasteable. Plain console.error rather than Ink
10
+ // here because the error may surface during one-shot commands where
11
+ // no Ink renderer is mounted (REPL paths catch via runtime/cli.ts).
12
+ if (error instanceof PugiCliUpgradeRequiredError) {
13
+ const lines = [
14
+ '',
15
+ 'Pugi CLI upgrade required',
16
+ '',
17
+ ` Your installed version: ${error.installedVersion}`,
18
+ ` Server requires minimum: ${error.minClientVersion}`,
19
+ ` Latest recommended: ${error.recommendedVersion}`,
20
+ '',
21
+ ` Upgrade: ${error.upgradeCommand}`,
22
+ ` Docs: ${error.upgradeUrl}`,
23
+ '',
24
+ ' Until you upgrade, the server will reject your requests.',
25
+ ' This protects you from silent protocol-drift bugs.',
26
+ '',
27
+ ];
28
+ console.error(lines.join('\n'));
29
+ process.exitCode = 1;
30
+ return;
31
+ }
4
32
  const message = error instanceof Error ? error.message : String(error);
5
33
  console.error(`pugi: ${message}`);
6
34
  process.exitCode = 1;