@pugi/cli 0.1.0-beta.4 → 0.1.0-beta.41

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 (250) hide show
  1. package/THIRD_PARTY_NOTICES.md +40 -0
  2. package/assets/pugi-mascot.ansi +15 -25
  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/commands/smoke.js +133 -0
  7. package/dist/core/agent-progress/cleanup.js +134 -0
  8. package/dist/core/agent-progress/schema.js +144 -0
  9. package/dist/core/agent-progress/writer.js +101 -0
  10. package/dist/core/artifact-chain/dispatcher.js +148 -0
  11. package/dist/core/artifact-chain/exporter.js +164 -0
  12. package/dist/core/artifact-chain/state.js +243 -0
  13. package/dist/core/artifact-chain/steps.js +169 -0
  14. package/dist/core/auth/ensure-authenticated.js +129 -0
  15. package/dist/core/auth/env-provider.js +238 -0
  16. package/dist/core/auto-update/channels.js +122 -0
  17. package/dist/core/auto-update/checker.js +241 -0
  18. package/dist/core/auto-update/state.js +235 -0
  19. package/dist/core/bare-mode/index.js +107 -0
  20. package/dist/core/bash-classifier.js +108 -1
  21. package/dist/core/checkpoint/resumer.js +149 -0
  22. package/dist/core/checkpoint/rewinder.js +291 -0
  23. package/dist/core/codegraph/decision-store.js +248 -0
  24. package/dist/core/codegraph/detect-repo.js +459 -0
  25. package/dist/core/codegraph/install.js +134 -0
  26. package/dist/core/codegraph/offer-hook.js +220 -0
  27. package/dist/core/compact/auto-trigger.js +96 -0
  28. package/dist/core/compact/buffer-rewriter.js +115 -0
  29. package/dist/core/compact/summarizer.js +208 -0
  30. package/dist/core/compact/token-counter.js +108 -0
  31. package/dist/core/consensus/diff-capture.js +73 -0
  32. package/dist/core/context/index.js +7 -0
  33. package/dist/core/context/markdown-traverse.js +255 -0
  34. package/dist/core/cost/rate-card.js +129 -0
  35. package/dist/core/cost/tracker.js +221 -0
  36. package/dist/core/denial-tracking/index.js +8 -0
  37. package/dist/core/denial-tracking/state.js +264 -0
  38. package/dist/core/diagnostics/probe-runner.js +93 -0
  39. package/dist/core/diagnostics/probes/api.js +46 -0
  40. package/dist/core/diagnostics/probes/auth.js +86 -0
  41. package/dist/core/diagnostics/probes/bare-mode.js +42 -0
  42. package/dist/core/diagnostics/probes/cli-version.js +127 -0
  43. package/dist/core/diagnostics/probes/config.js +72 -0
  44. package/dist/core/diagnostics/probes/denial-tracking.js +57 -0
  45. package/dist/core/diagnostics/probes/disk.js +81 -0
  46. package/dist/core/diagnostics/probes/git.js +65 -0
  47. package/dist/core/diagnostics/probes/mcp.js +75 -0
  48. package/dist/core/diagnostics/probes/node.js +59 -0
  49. package/dist/core/diagnostics/probes/pnpm.js +36 -0
  50. package/dist/core/diagnostics/probes/pugi-md.js +89 -0
  51. package/dist/core/diagnostics/probes/session.js +74 -0
  52. package/dist/core/diagnostics/probes/status-snapshot.js +488 -0
  53. package/dist/core/diagnostics/probes/workspace.js +63 -0
  54. package/dist/core/diagnostics/types.js +70 -0
  55. package/dist/core/dispatch/cache-cleanup.js +197 -0
  56. package/dist/core/dispatch/cache-handoff.js +295 -0
  57. package/dist/core/edits/dispatch.js +218 -2
  58. package/dist/core/edits/journal.js +199 -0
  59. package/dist/core/edits/layer-d-ast.js +557 -14
  60. package/dist/core/edits/verify-hook.js +273 -0
  61. package/dist/core/edits/worktree.js +322 -0
  62. package/dist/core/engine/anvil-client.js +115 -5
  63. package/dist/core/engine/budgets.js +98 -0
  64. package/dist/core/engine/context-prefix.js +155 -0
  65. package/dist/core/engine/intent.js +260 -0
  66. package/dist/core/engine/native-pugi.js +860 -211
  67. package/dist/core/engine/prompts.js +88 -2
  68. package/dist/core/engine/strip-internal-fields.js +124 -0
  69. package/dist/core/engine/tool-bridge.js +1045 -36
  70. package/dist/core/feedback/queue.js +177 -0
  71. package/dist/core/feedback/submitter.js +145 -0
  72. package/dist/core/file-cache.js +113 -1
  73. package/dist/core/hooks/events.js +44 -0
  74. package/dist/core/hooks/index.js +15 -0
  75. package/dist/core/hooks/registry.js +213 -0
  76. package/dist/core/hooks/runner.js +236 -0
  77. package/dist/core/hooks/v2/event-emitter.js +115 -0
  78. package/dist/core/hooks/v2/executor.js +282 -0
  79. package/dist/core/hooks/v2/index.js +25 -0
  80. package/dist/core/hooks/v2/lifecycle.js +104 -0
  81. package/dist/core/hooks/v2/loader.js +216 -0
  82. package/dist/core/hooks/v2/matcher.js +125 -0
  83. package/dist/core/hooks/v2/trust.js +143 -0
  84. package/dist/core/hooks/v2/types.js +86 -0
  85. package/dist/core/lsp/cache.js +105 -0
  86. package/dist/core/lsp/client.js +776 -0
  87. package/dist/core/lsp/language-detect.js +66 -0
  88. package/dist/core/lsp/post-edit-diagnostics.js +171 -0
  89. package/dist/core/mcp/client.js +75 -6
  90. package/dist/core/mcp/http-server.js +553 -0
  91. package/dist/core/mcp/orchestrator-tools.js +662 -0
  92. package/dist/core/mcp/permission.js +190 -0
  93. package/dist/core/mcp/registry.js +24 -2
  94. package/dist/core/mcp/server-tools.js +219 -0
  95. package/dist/core/mcp/server.js +397 -0
  96. package/dist/core/memory/dual-write.js +416 -0
  97. package/dist/core/memory/phase1-kinds.js +20 -0
  98. package/dist/core/memory-sync/queue.js +158 -0
  99. package/dist/core/onboarding/ensure-initialized.js +133 -0
  100. package/dist/core/onboarding/marker.js +111 -0
  101. package/dist/core/onboarding/telemetry-state.js +108 -0
  102. package/dist/core/output-style/presets.js +176 -0
  103. package/dist/core/output-style/state.js +185 -0
  104. package/dist/core/permissions/auto-classifier.js +124 -0
  105. package/dist/core/permissions/circuit-breaker.js +83 -0
  106. package/dist/core/permissions/gate.js +278 -0
  107. package/dist/core/permissions/index.js +20 -0
  108. package/dist/core/permissions/mode.js +174 -0
  109. package/dist/core/permissions/state.js +241 -0
  110. package/dist/core/permissions/tool-class.js +93 -0
  111. package/dist/core/prd-check/parser.js +215 -0
  112. package/dist/core/prd-check/reporter.js +127 -0
  113. package/dist/core/prd-check/session-review.js +557 -0
  114. package/dist/core/prd-check/verifiers.js +223 -0
  115. package/dist/core/pugi-md/context-injector.js +76 -0
  116. package/dist/core/pugi-md/walk-up.js +207 -0
  117. package/dist/core/release-notes/parser.js +241 -0
  118. package/dist/core/release-notes/state.js +116 -0
  119. package/dist/core/repl/history.js +11 -1
  120. package/dist/core/repl/model-pricing.js +135 -0
  121. package/dist/core/repl/session.js +1899 -38
  122. package/dist/core/repl/slash-commands.js +406 -21
  123. package/dist/core/repl/store/session-store.js +31 -2
  124. package/dist/core/repl/workspace-context.js +22 -0
  125. package/dist/core/repo-map/build.js +125 -0
  126. package/dist/core/repo-map/cache.js +185 -0
  127. package/dist/core/repo-map/extractor.js +254 -0
  128. package/dist/core/repo-map/formatter.js +145 -0
  129. package/dist/core/repo-map/scanner.js +211 -0
  130. package/dist/core/retry-budget/budget.js +284 -0
  131. package/dist/core/retry-budget/index.js +5 -0
  132. package/dist/core/session.js +92 -0
  133. package/dist/core/settings.js +80 -0
  134. package/dist/core/share/formatter.js +271 -0
  135. package/dist/core/share/redactor.js +221 -0
  136. package/dist/core/share/uploader.js +267 -0
  137. package/dist/core/skills/defaults.js +457 -0
  138. package/dist/core/smoke/headless-driver.js +174 -0
  139. package/dist/core/smoke/orchestrator.js +194 -0
  140. package/dist/core/smoke/runner.js +238 -0
  141. package/dist/core/smoke/scenario-parser.js +316 -0
  142. package/dist/core/subagents/dispatcher-real.js +600 -0
  143. package/dist/core/subagents/dispatcher.js +113 -24
  144. package/dist/core/subagents/index.js +18 -5
  145. package/dist/core/subagents/isolation-matrix.js +213 -0
  146. package/dist/core/subagents/spawn.js +19 -4
  147. package/dist/core/telemetry/emitter.js +229 -0
  148. package/dist/core/telemetry/queue.js +251 -0
  149. package/dist/core/theme/context.js +91 -0
  150. package/dist/core/theme/presets.js +228 -0
  151. package/dist/core/theme/state.js +181 -0
  152. package/dist/core/todos/invariant.js +10 -0
  153. package/dist/core/todos/state.js +177 -0
  154. package/dist/core/transport/version-interceptor.js +166 -0
  155. package/dist/core/vim/keymap.js +288 -0
  156. package/dist/core/vim/state.js +92 -0
  157. package/dist/index.js +28 -0
  158. package/dist/runtime/bootstrap.js +190 -0
  159. package/dist/runtime/cli.js +3073 -321
  160. package/dist/runtime/commands/cancel.js +231 -0
  161. package/dist/runtime/commands/chain.js +489 -0
  162. package/dist/runtime/commands/codegraph-status.js +227 -0
  163. package/dist/runtime/commands/compact.js +297 -0
  164. package/dist/runtime/commands/cost.js +199 -0
  165. package/dist/runtime/commands/delegate.js +242 -11
  166. package/dist/runtime/commands/dispatch.js +126 -0
  167. package/dist/runtime/commands/doctor.js +390 -0
  168. package/dist/runtime/commands/feedback.js +184 -0
  169. package/dist/runtime/commands/hooks.js +184 -0
  170. package/dist/runtime/commands/lsp.js +368 -0
  171. package/dist/runtime/commands/mcp.js +879 -0
  172. package/dist/runtime/commands/memory.js +508 -0
  173. package/dist/runtime/commands/model.js +237 -0
  174. package/dist/runtime/commands/onboarding.js +275 -0
  175. package/dist/runtime/commands/patch.js +128 -0
  176. package/dist/runtime/commands/permissions.js +112 -0
  177. package/dist/runtime/commands/plan.js +143 -0
  178. package/dist/runtime/commands/prd-check.js +285 -0
  179. package/dist/runtime/commands/redo-blob-store.js +92 -0
  180. package/dist/runtime/commands/redo.js +361 -0
  181. package/dist/runtime/commands/release-notes.js +229 -0
  182. package/dist/runtime/commands/repo-map.js +95 -0
  183. package/dist/runtime/commands/report.js +299 -0
  184. package/dist/runtime/commands/resume.js +118 -0
  185. package/dist/runtime/commands/review-consensus.js +17 -2
  186. package/dist/runtime/commands/rewind.js +333 -0
  187. package/dist/runtime/commands/sessions.js +163 -0
  188. package/dist/runtime/commands/share.js +316 -0
  189. package/dist/runtime/commands/status.js +186 -0
  190. package/dist/runtime/commands/stickers.js +82 -0
  191. package/dist/runtime/commands/style.js +194 -0
  192. package/dist/runtime/commands/theme.js +196 -0
  193. package/dist/runtime/commands/undo.js +32 -0
  194. package/dist/runtime/commands/update.js +289 -0
  195. package/dist/runtime/commands/vim.js +140 -0
  196. package/dist/runtime/commands/worktree.js +177 -0
  197. package/dist/runtime/headless-repl.js +195 -0
  198. package/dist/runtime/headless.js +543 -0
  199. package/dist/runtime/load-hooks-or-exit.js +71 -0
  200. package/dist/runtime/plan-decompose.js +531 -0
  201. package/dist/runtime/version.js +65 -0
  202. package/dist/tools/agent-tool.js +229 -0
  203. package/dist/tools/apply-patch.js +556 -0
  204. package/dist/tools/ask-user-question.js +213 -0
  205. package/dist/tools/ask-user.js +115 -0
  206. package/dist/tools/file-tools.js +85 -14
  207. package/dist/tools/lsp-tools.js +189 -0
  208. package/dist/tools/mcp-tool.js +260 -0
  209. package/dist/tools/multi-edit.js +361 -0
  210. package/dist/tools/powershell.js +156 -0
  211. package/dist/tools/registry.js +51 -0
  212. package/dist/tools/skill-tool.js +96 -0
  213. package/dist/tools/tasks.js +208 -0
  214. package/dist/tools/todo-write.js +184 -0
  215. package/dist/tools/web-fetch.js +147 -2
  216. package/dist/tools/web-search.js +458 -0
  217. package/dist/tui/agent-progress-card.js +111 -0
  218. package/dist/tui/agent-tree.js +10 -0
  219. package/dist/tui/ask-modal.js +2 -2
  220. package/dist/tui/ask-user-question-prompt.js +192 -0
  221. package/dist/tui/compact-banner.js +81 -0
  222. package/dist/tui/conversation-pane.js +82 -8
  223. package/dist/tui/cost-table.js +111 -0
  224. package/dist/tui/doctor-table.js +46 -0
  225. package/dist/tui/feedback-prompt.js +156 -0
  226. package/dist/tui/input-box.js +69 -2
  227. package/dist/tui/markdown-render.js +4 -4
  228. package/dist/tui/onboarding-wizard.js +240 -0
  229. package/dist/tui/permissions-picker.js +86 -0
  230. package/dist/tui/render.js +35 -0
  231. package/dist/tui/repl-render.js +303 -13
  232. package/dist/tui/repl-splash.js +2 -2
  233. package/dist/tui/repl.js +72 -14
  234. package/dist/tui/splash.js +1 -1
  235. package/dist/tui/status-bar.js +94 -16
  236. package/dist/tui/status-table.js +7 -0
  237. package/dist/tui/stickers-art.js +136 -0
  238. package/dist/tui/style-table.js +28 -0
  239. package/dist/tui/theme-table.js +29 -0
  240. package/dist/tui/tool-stream-pane.js +52 -3
  241. package/dist/tui/update-banner.js +20 -2
  242. package/dist/tui/vim-input.js +267 -0
  243. package/docs/examples/codegraph.mcp.json +10 -0
  244. package/package.json +12 -6
  245. package/test/scenarios/codegen-create-file.scenario.txt +13 -0
  246. package/test/scenarios/compact-force.scenario.txt +11 -0
  247. package/test/scenarios/identity.scenario.txt +11 -0
  248. package/test/scenarios/persona-handoff.scenario.txt +11 -0
  249. package/test/scenarios/walkback.scenario.txt +12 -0
  250. package/dist/core/engine/compaction-hook.js +0 -154
@@ -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))