pantheon-opencode 1.6.0-beta.1 → 1.6.0-beta.10

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 (120) hide show
  1. package/AGENTS.global.md +182 -0
  2. package/AGENTS.md +10 -891
  3. package/CHANGELOG.md +326 -0
  4. package/README.md +496 -32
  5. package/README.pt-BR.md +285 -20
  6. package/bin/pantheon-init.mjs +26 -3
  7. package/commands/pantheon-audit.md +7 -1
  8. package/commands/pantheon-consolidate.md +1 -1
  9. package/commands/pantheon-deepwork.md +14 -3
  10. package/commands/pantheon-focus.md +17 -0
  11. package/commands/pantheon-vision.md +61 -12
  12. package/package.json +21 -14
  13. package/plugin.json +2 -2
  14. package/pyproject.toml +8 -6
  15. package/scripts/build-agents-md.mjs +374 -56
  16. package/scripts/changelog-merge.mjs +161 -0
  17. package/scripts/{code_mode_server.py → code_mode.py} +3 -3
  18. package/scripts/cost.mjs +244 -41
  19. package/scripts/doctor.mjs +151 -1
  20. package/scripts/install/agents-md.mjs +4 -4
  21. package/scripts/install/config-migration.mjs +317 -4
  22. package/scripts/install/health-check.mjs +21 -19
  23. package/scripts/install/interactive.mjs +1 -1
  24. package/scripts/install/opencode-version.mjs +246 -4
  25. package/scripts/install/opencode.mjs +627 -179
  26. package/scripts/install/plugin.mjs +43 -9
  27. package/scripts/install/shared.mjs +120 -12
  28. package/scripts/install/vision-prompt.mjs +161 -0
  29. package/scripts/install-mcp.mjs +28 -9
  30. package/scripts/{mcp_persistence_server.py → mcp_persistence.py} +2 -2
  31. package/{src/mcp/mcp_resources_server.py → scripts/mcp_resources.py} +2 -2
  32. package/scripts/{memory_mcp_server.py → memory_mcp.py} +2 -2
  33. package/scripts/probe-context-rehydrate.mjs +8 -2
  34. package/scripts/probe-pantheon-cost.mjs +71 -15
  35. package/scripts/run-node-tests.mjs +189 -0
  36. package/scripts/sync-artifacts.mjs +26 -13
  37. package/scripts/sync-tui.mjs +24 -3
  38. package/scripts/test-opencode-v2-sandbox.sh +180 -26
  39. package/scripts/validate-routing.mjs +3 -3
  40. package/scripts/validate-sandbox-install.mjs +138 -0
  41. package/scripts/versioning.mjs +13 -1
  42. package/scripts/vision-status.mjs +682 -0
  43. package/skills-lock.json +8 -8
  44. package/src/agents/aphrodite.md +20 -37
  45. package/src/agents/apollo.md +7 -3
  46. package/src/agents/athena.md +63 -35
  47. package/src/agents/demeter.md +8 -28
  48. package/src/agents/gaia.md +1 -1
  49. package/src/agents/hephaestus.md +10 -21
  50. package/src/agents/hermes.md +15 -38
  51. package/src/agents/iris.md +1 -1
  52. package/src/agents/mnemosyne.md +18 -54
  53. package/src/agents/nyx.md +1 -1
  54. package/src/agents/prometheus.md +16 -20
  55. package/src/agents/talos.md +10 -2
  56. package/src/agents/themis.md +5 -5
  57. package/src/agents/zeus.md +99 -129
  58. package/src/instructions/agent-return-format.instructions.md +18 -3
  59. package/src/instructions/backend-standards.instructions.md +2 -0
  60. package/src/instructions/frontend-standards.instructions.md +3 -1
  61. package/src/instructions/memory-protocol.instructions.md +11 -110
  62. package/src/instructions/zeus-anti-stall.instructions.md +14 -61
  63. package/src/instructions/zeus-communication-rules.instructions.md +0 -1
  64. package/src/instructions/zeus-council-synthesis.instructions.md +32 -260
  65. package/src/instructions/zeus-memory-operations.instructions.md +26 -0
  66. package/src/instructions/zeus-timeout-retry.instructions.md +40 -87
  67. package/src/mcp/{code_mode_server.py → code_mode.py} +3 -3
  68. package/src/mcp/eval_store.py +2 -2
  69. package/src/mcp/{mcp_persistence_server.py → mcp_persistence.py} +2 -2
  70. package/{scripts/mcp_resources_server.py → src/mcp/mcp_resources.py} +2 -2
  71. package/src/mcp/{memory_mcp_server.py → memory_mcp.py} +2 -2
  72. package/src/mcp/{pantheon_vision_server.py → pantheon_vision.py} +286 -42
  73. package/src/mcp/requirements-mcp.txt +6 -4
  74. package/src/mcp/requirements-vision.txt +2 -2
  75. package/src/pantheon/cost-command.ts +399 -101
  76. package/src/pantheon/delegation-enforce.ts +10 -1
  77. package/src/pantheon/presets.d.mts +0 -8
  78. package/src/pantheon/presets.mjs +5 -32
  79. package/src/pantheon/v2-bounded-read.ts +640 -0
  80. package/src/pantheon/v2-delegation-enforce.ts +214 -0
  81. package/src/pantheon/v2-execute-after.ts +186 -0
  82. package/src/pantheon/v2-tools.ts +429 -0
  83. package/src/pantheon/v2-unsupported.d.mts +11 -0
  84. package/src/pantheon/v2-unsupported.mjs +90 -0
  85. package/src/pantheon/v2-vision.ts +622 -0
  86. package/src/pantheon/vision-local.ts +531 -0
  87. package/src/pantheon/vision.ts +1160 -115
  88. package/src/plugin-v2.ts +1145 -219
  89. package/src/plugin.ts +12 -9
  90. package/src/plugins/tui/dist/server.d.ts +1 -2
  91. package/src/plugins/tui/dist/server.d.ts.map +1 -1
  92. package/src/plugins/tui/dist/tui-v2.d.ts +154 -0
  93. package/src/plugins/tui/dist/tui-v2.d.ts.map +1 -0
  94. package/src/plugins/tui/dist/tui-v2.js +659 -0
  95. package/src/plugins/tui/dist/tui-v2.js.map +1 -0
  96. package/src/plugins/tui/dist/tui.d.ts +85 -82
  97. package/src/plugins/tui/dist/tui.d.ts.map +1 -1
  98. package/src/plugins/tui/dist/tui.js +72 -59
  99. package/src/plugins/tui/index-v2.ts +6 -0
  100. package/src/plugins/tui/package-lock.json +5516 -1353
  101. package/src/plugins/tui/package.json +22 -12
  102. package/src/plugins/tui/src/index-v2.tsx +734 -0
  103. package/src/plugins/tui/src/index.tsx +106 -85
  104. package/src/plugins/tui/src/opencode-v2.d.ts +13 -0
  105. package/src/plugins/tui/tsconfig.build.json +5 -0
  106. package/src/plugins/tui/tsconfig.typecheck.json +5 -0
  107. package/src/plugins/tui/tsdown.config.ts +22 -2
  108. package/src/plugins/tui/tui-v2.ts +2 -0
  109. package/src/plugins/tui/tui.ts +3 -0
  110. package/src/routing.yml +11 -21
  111. package/src/skills/README.md +3 -3
  112. package/src/skills/agent-coordination/SKILL.md +7 -2
  113. package/src/skills/artifact-management/SKILL.md +12 -5
  114. package/src/skills/auto-continue/SKILL.md +39 -30
  115. package/src/skills/code-review-checklist/SKILL.md +5 -5
  116. package/src/skills/context-compression/SKILL.md +26 -879
  117. package/src/skills/session-goal/SKILL.md +14 -5
  118. package/src/skills/spec-driven-development/SKILL.md +3 -1
  119. package/src/skills/tdd-with-agents/SKILL.md +18 -10
  120. package/opencode.json +0 -58
package/src/plugin-v2.ts CHANGED
@@ -2,26 +2,70 @@
2
2
  * Pantheon V2 Plugin — full orchestration plugin for OpenCode V2.
3
3
  *
4
4
  * Registers:
5
- * - 6 orchestration tools via V2 tool.transform (with V1 bridge fallback)
6
- * - 4 event subscriptions (session.created, idle, error, compacted)
7
- * - Session hooks (prompt, context, compaction) and tool hooks (execute.before/after)
8
- * - Permission hooks for custom authorization
9
- *
10
- * The transitional SDK types used here expose these domains:
11
- * ctx.agent / ctx.command / ctx.model / ctx.reference / ctx.skill /
12
- * ctx.tool / ctx.event / ctx.permission / ctx.session
13
- *
14
- * A separate OpenCode 2.0.18 host runtime probe confirmed ctx.integration and
15
- * ctx.skill have callable `.transform` methods; only ctx.catalog was absent.
16
- * The probe's callback effects were
17
- * not observed for any domain. This runtime probe is not an SDK compatibility
18
- * claim. Pantheon's Phase 1/config transform
19
- * registrations cover agent, command, and reference; `ctx.tool.transform` is
20
- * attempted separately in Phase 2. Integration and skill transform callbacks
21
- * are not registered by Pantheon, so their effects remain unproven. The
22
- * narrower `SkillEditor.source()` helper is absent from the inspected SDK
23
- * editor shape, so directory-source registration remains unsupported; that
24
- * does not imply that `ctx.skill` or its transform is absent.
5
+ * - 3 self-sufficient tools via V2 tool.transform (hashline_edit,
6
+ * pantheon_cost, pantheon_model) plus native `vision_describe` when the host
7
+ * exposes the V2 model catalog; Vision resolves OpenCode Zen auth and sends
8
+ * images directly to the selected multimodal model.
9
+ * The goal tools need V1 infrastructure and are absent from this surface.
10
+ * - 5 event subscriptions (session.created, idle, deleted, error, compacted)
11
+ * - Session hooks (prompt, context, compaction)
12
+ * - A tool `execute.before` hook that ENFORCES read-only sessions: the agent
13
+ * arrives on the host's event, a read-only agent (apollo/gaia) registers its
14
+ * session, and the shared `createEnforcementGuard` throws to deny
15
+ * `hashline_edit` / `pantheon_model` / `edit` / `write` / `bash` / `task`.
16
+ * See `registerV2ToolHooks`.
17
+ * - The SAME `execute.before` hook runs scripts/hooks/scan-secrets.sh over the
18
+ * tool input and throws on exit 2 (high-confidence token match). This is the
19
+ * ONLY hard security block on the V2 surface. It is wired HERE, not delegated
20
+ * to `src/plugins/pantheon-hooks.ts`: opencode.json declares only
21
+ * `src/plugin-v2` in `plugins`, so the V1 plugin never loads on a V2-only
22
+ * install and V1's identical block was inert by construction.
23
+
24
+ * - A tool `execute.after` hook and a permission hook, with permission
25
+ * evaluation enforcing the caller/target matrix for Pantheon agents
26
+ *
27
+ * Every tool registered through `draft.add` MUST declare `output`, and its
28
+ * `execute` MUST resolve to a value carrying `output`. The host enforces the
29
+ * two as a pair — see `V2ToolDraft` for the exact contract.
30
+ *
31
+ * ## The host domains the SDK types do not carry
32
+ *
33
+ * `@opencode-ai/plugin` 1.18.33 types `PluginContext` as exactly:
34
+ * options · agent · aisdk · catalog · command · integration · plugin ·
35
+ * reference · skill
36
+ * — no `tool`, `event`, `permission` or `session`. 1.18.34 (`latest`) is the
37
+ * same, so following the stable SDK line does not resolve this: it is a
38
+ * STRUCTURAL DECLARATION task, not a pin reconciliation.
39
+ *
40
+ * A live opencode 2.0.22+ host DOES have these domains, with the shapes this module
41
+ * uses. Measured by `tests/canary/plugin-v2-tool-canary.test.mjs`, which loads
42
+ * this file through the real loader and records `Object.keys(ctx)` from inside
43
+ * the host:
44
+ *
45
+ * agent aisdk app command event experimental generate integration location
46
+ * mcp model options permission plugin provider reference rpc session shell
47
+ * skill storage tool vcs websearch worktree
48
+ *
49
+ * ctx.tool.transform function
50
+ * ctx.tool.hook function
51
+ * ctx.tool.list function
52
+ * ctx.event.subscribe function
53
+ * ctx.permission.hook function
54
+ * ctx.session.hook function
55
+ * ctx.catalog absent
56
+ *
57
+ * `HostPluginContext` below declares exactly the minimum of that, and
58
+ * `hostDomains` is the SINGLE documented cast that reaches them — replacing the
59
+ * six repeated `as unknown as Record<string, unknown>` casts this file carried
60
+ * (Phases 2, 3, 4, 5, 6 and 7 — every phase except Phase 1, which reads the
61
+ * SDK-typed domains directly), each of which type-checked nothing at all. The
62
+ * per-domain definition checks stay: `registerV2Tools` still returns `false`
63
+ * and marks `tool-transform` when `ctx.tool.transform` is missing, and likewise
64
+ * for the others. The cast is not a licence to assume: the canary above is what
65
+ * proves the domains exist on a real host, and it fails if they stop existing.
66
+ *
67
+ * If the SDK ever exposes these host domains, replacing `hostDomains` with the real
68
+ * type is a one-line change. Do NOT make that change while 1.18.x lacks them.
25
69
  *
26
70
  * V2_UNSUPPORTED_FEATURES distinguishes adapter limitations from observed
27
71
  * host-absent APIs; setup handles optional registrations on a best-effort basis.
@@ -29,7 +73,9 @@
29
73
  * @module plugin-v2
30
74
  */
31
75
 
32
- import { appendFileSync } from 'node:fs'
76
+ import { appendFileSync, mkdirSync } from 'node:fs'
77
+ import { realpath as realpathAsync } from 'node:fs/promises'
78
+ import path from 'node:path'
33
79
  import { fileURLToPath } from 'node:url'
34
80
  import type {
35
81
  AgentDraft,
@@ -38,11 +84,36 @@ import type {
38
84
  ReferenceDraft,
39
85
  } from '@opencode-ai/plugin/v2/promise'
40
86
  import { define } from '@opencode-ai/plugin/v2/promise'
87
+ import {
88
+ createEnforcementGuard,
89
+ readOnlyRegistry,
90
+ syncReadOnlySession,
91
+ type ToolExecuteBeforeInput,
92
+ type ToolExecuteBeforeOutput,
93
+ } from './pantheon/delegation-enforce.ts'
94
+ import {
95
+ BOUNDED_READ_TOOL,
96
+ createV2BoundedReadTool,
97
+ type V2BoundedReadOriginRegistry,
98
+ } from './pantheon/v2-bounded-read.ts'
41
99
  import {
42
100
  getV2BridgeFromContext,
43
101
  type PantheonV2Bridge,
44
102
  type V2ContextLike,
45
103
  } from './pantheon/v2-bridge.ts'
104
+ import {
105
+ type AuthoritativeSession,
106
+ evaluateV2Delegation,
107
+ PANTHEON_AGENTS,
108
+ type V2PermissionEvent,
109
+ V2SubagentOriginRegistry,
110
+ v2ResourceTarget,
111
+ } from './pantheon/v2-delegation-enforce.ts'
112
+ import { createV2ExecuteAfter, resolveV2SandboxConfig } from './pantheon/v2-execute-after.ts'
113
+ import { createV2ToolDefinitions, type V2ToolResult } from './pantheon/v2-tools.ts'
114
+ import { V2_UNSUPPORTED_FEATURE_SEED } from './pantheon/v2-unsupported.mjs'
115
+ import { buildV2VisionPromptContext, type V2VisionRuntime } from './pantheon/v2-vision.ts'
116
+ import { type HookPayload, type HookResult, runHook } from './plugins/hook-runner.ts'
46
117
 
47
118
  // ─── Unsupported Features Registry ───────────────────────────────────────
48
119
 
@@ -51,19 +122,18 @@ import {
51
122
  * the observed host. A listed feature is not necessarily a host-absent API.
52
123
  * Additional features are appended during setup when a required API is missing.
53
124
  */
54
- export const V2_UNSUPPORTED_FEATURES: string[] = [
55
- 'legacy-hooks',
56
- // `catalog` alone was absent in the OpenCode 2.0.18 runtime probe.
57
- 'catalog-transform',
58
- // The host exposes callable transforms for integration and skill, but
59
- // Pantheon does not register/use them and no host callback effects were
60
- // observed. These entries describe adapter support, not host availability.
61
- 'integration-transform',
62
- // The inspected SkillEditor shape has no `source()` helper for adding a
63
- // directory source. This is narrower than (and distinct from) ctx.skill's
64
- // host availability or callable transform.
65
- 'skill-transform',
66
- ]
125
+ /**
126
+ * Live unsupported-feature list: the shared seed plus anything `markUnsupported`
127
+ * appends while this process talks to a real host.
128
+ *
129
+ * The seed lives in `src/pantheon/v2-unsupported.mjs` — a plain-`.mjs` module —
130
+ * so that the V2 installer and `scripts/doctor.mjs` report the SAME strings
131
+ * without either of them having to import TypeScript. This array stays a
132
+ * mutable copy rather than the shared seed itself: `markUnsupported` appends to
133
+ * it, and the seed must stay frozen. `getUnsupportedFeatures()` still returns
134
+ * this exact reference.
135
+ */
136
+ export const V2_UNSUPPORTED_FEATURES: string[] = [...V2_UNSUPPORTED_FEATURE_SEED]
67
137
 
68
138
  // ─── Constants ───────────────────────────────────────────────────────────
69
139
 
@@ -146,6 +216,88 @@ function markUnsupported(feature: string): void {
146
216
  }
147
217
  }
148
218
 
219
+ // ─── Host context domains the SDK types do not carry ────────────────────
220
+
221
+ /**
222
+ * The minimal shape of `ctx.tool` this module uses, as a live opencode 2.0.22
223
+ * host provides it. `transform` registers the Pantheon tools, `hook` installs
224
+ * the `execute.before`/`execute.after` guards, `list` is the host-built
225
+ * descriptor dump the canary asserts on.
226
+ */
227
+ interface V2ToolDomain {
228
+ transform?: (cb: (draft: V2ToolDraft) => void) => Promise<unknown>
229
+ hook?: (name: string, handler: (event: unknown) => void | Promise<void>) => Promise<unknown>
230
+ list?: () => Promise<unknown>
231
+ }
232
+
233
+ /** The minimal shape of `ctx.event`: one async-iterable subscription. */
234
+ interface V2EventDomain {
235
+ subscribe?: (opts?: { signal?: AbortSignal }) => AsyncIterable<V2SessionEvent>
236
+ }
237
+
238
+ /**
239
+ * The minimal shape of `ctx.permission`.
240
+ *
241
+ * The V2.0.25 live contract supplies an `evaluate` event with `resources`;
242
+ * runtime validation remains defensive because SDK declarations omit this
243
+ * domain and the host may evolve independently.
244
+ */
245
+ interface V2PermissionDomain {
246
+ assert?: (input: Record<string, unknown>) => Promise<unknown>
247
+ hook?: (
248
+ name: string,
249
+ handler: (event: unknown, output?: unknown) => void | Promise<void>,
250
+ ) => Promise<unknown>
251
+ }
252
+
253
+ /**
254
+ * The minimal shape of `ctx.session` used by this module. `get` follows the V2
255
+ * Promise API's object input (`{ sessionID }`); session hooks accept any string
256
+ * and silently drop an unknown one, which is why the host-backed hook canary
257
+ * exists.
258
+ */
259
+ interface V2SessionDomain {
260
+ hook?: (name: string, handler: (event: unknown) => void | Promise<void>) => Promise<unknown>
261
+ get?: (input: { sessionID: string }) => Promise<unknown>
262
+ }
263
+
264
+ interface V2LocationDomain {
265
+ directory?: string
266
+ project?: { id?: string }
267
+ }
268
+
269
+ interface V2ModelDomain {
270
+ list?: V2VisionRuntime['model']['list']
271
+ }
272
+
273
+ interface V2GenerateDomain {
274
+ text?: (input: { model: { providerID: string; id: string }; prompt: string }) => Promise<unknown>
275
+ }
276
+
277
+ /** `PluginContext` plus the host domains 1.18.x omits. See the module header. */
278
+ interface HostPluginContext extends PluginContext {
279
+ tool?: V2ToolDomain
280
+ event?: V2EventDomain
281
+ permission?: V2PermissionDomain
282
+ session?: V2SessionDomain
283
+ model?: V2ModelDomain
284
+ generate?: V2GenerateDomain
285
+ location?: V2LocationDomain
286
+ }
287
+
288
+ /**
289
+ * THE cast to the host's context. Every optional member stays optional and every
290
+ * caller re-checks before use, so this narrows nothing away: it replaces five
291
+ * anonymous `Record<string, unknown>` casts with one named, documented,
292
+ * type-checked boundary.
293
+ *
294
+ * When the SDK ships these four domains, delete this function and use
295
+ * `PluginContext` directly.
296
+ */
297
+ function hostDomains(context: PluginContext): HostPluginContext {
298
+ return context as HostPluginContext
299
+ }
300
+
149
301
  // ─── V1 Bridge Integration ──────────────────────────────────────────────
150
302
 
151
303
  /**
@@ -283,32 +435,53 @@ function transformReferences(draft: ReferenceDraft): void {
283
435
  *
284
436
  * The V2 tool.transform API (when available) provides:
285
437
  * draft.namespace({ name: "pantheon", description: "..." })
286
- * draft.add({ name, description, input, execute })
438
+ * draft.add({ name, description, input, output, execute })
439
+ *
440
+ * `output` is part of the required shape, not an optional extra: see
441
+ * `V2ToolDraft` for the host contract that makes it mandatory.
287
442
  *
288
443
  * Returns true if tools were registered, false if the API is unavailable.
289
444
  */
290
- async function registerV2Tools(context: PluginContext): Promise<boolean> {
291
- const toolCtx = (context as unknown as Record<string, unknown>).tool as
292
- | { transform?: (cb: (draft: V2ToolDraft) => void) => Promise<unknown> }
293
- | undefined
445
+ async function registerV2Tools(
446
+ context: PluginContext,
447
+ boundedRead?: ReturnType<typeof createV2BoundedReadTool>['definition'],
448
+ ): Promise<boolean> {
449
+ const host = hostDomains(context)
450
+ const toolCtx = host.tool
294
451
 
295
452
  if (!toolCtx?.transform) {
296
453
  return false
297
454
  }
298
455
 
299
456
  try {
300
- // Tool definitions require infrastructure instances — when available via
301
- // V2 context, they are passed through; otherwise the factory returns
302
- // definitions with lazy execute wrappers.
303
- const toolDefs = createV2ToolDefinitionsFromContext(context)
457
+ // Tool definitions carry their own `output` declaration; the draft requires
458
+ // it, and the host rejects a definition whose result shape cannot satisfy
459
+ // the declaration it makes.
460
+ const model = host.model
461
+ const visionReady = typeof model?.list === 'function'
462
+ if (!visionReady) markUnsupported('vision-tool')
463
+ const visionRuntime = visionReady ? ({ model } as V2VisionRuntime) : undefined
464
+ const toolDefs = createV2ToolDefinitions(visionRuntime)
465
+ if (boundedRead !== undefined) toolDefs.push(boundedRead)
304
466
 
305
467
  await toolCtx.transform((draft: V2ToolDraft) => {
468
+ // Catalog grouping only. The host computes a tool's registration id as
469
+ // `options.namespace === undefined ? name : `${namespace}_${name}``, and
470
+ // `draft.namespace()` does NOT set `options.namespace` on later `add`
471
+ // calls — so the tools stay `hashline_edit` / `pantheon_cost` /
472
+ // `pantheon_model`, not `pantheon_*`. That bare name is what the
473
+ // enforcement guard's blocked-tool list matches on; see
474
+ // `adaptV2ExecuteBeforeEvent`.
306
475
  draft.namespace({ name: 'pantheon', description: 'Pantheon orchestration tools' })
307
476
  for (const def of toolDefs) {
308
477
  draft.add({
309
478
  name: def.name,
310
479
  description: def.description,
311
480
  input: def.input,
481
+ // Required by the host contract: without this the tool fails on
482
+ // every call with "Tool result declared output without an output
483
+ // schema". Must stay paired with def.execute resolving to `output`.
484
+ output: def.output,
312
485
  execute: def.execute,
313
486
  })
314
487
  }
@@ -329,10 +502,11 @@ async function registerV2Tools(context: PluginContext): Promise<boolean> {
329
502
  *
330
503
  * Returns a cleanup function, or undefined if the API is unavailable.
331
504
  */
332
- function subscribeV2Events(context: PluginContext): (() => void) | undefined {
333
- const eventCtx = (context as unknown as Record<string, unknown>).event as
334
- | { subscribe?: (opts?: { signal?: AbortSignal }) => AsyncIterable<V2SessionEvent> }
335
- | undefined
505
+ function subscribeV2Events(
506
+ context: PluginContext,
507
+ onTurnEnd?: (sessionID: string) => void,
508
+ ): (() => void) | undefined {
509
+ const eventCtx = hostDomains(context).event
336
510
 
337
511
  if (!eventCtx?.subscribe) {
338
512
  return undefined
@@ -344,6 +518,19 @@ function subscribeV2Events(context: PluginContext): (() => void) | undefined {
344
518
  // biome-ignore lint/style/noNonNullAssertion: subscribe guaranteed by guard above
345
519
  for await (const event of eventCtx.subscribe!({ signal: controller.signal })) {
346
520
  await handleV2SessionEvent(event)
521
+ if (event.type === 'session.idle' || event.type === 'session.deleted') {
522
+ const properties = event.properties ?? {}
523
+ const info = properties.info
524
+ const infoID =
525
+ info !== null && typeof info === 'object' ? (info as { id?: unknown }).id : undefined
526
+ const sessionID =
527
+ typeof properties.sessionID === 'string'
528
+ ? properties.sessionID
529
+ : typeof infoID === 'string'
530
+ ? infoID
531
+ : undefined
532
+ if (sessionID) onTurnEnd?.(sessionID)
533
+ }
347
534
  }
348
535
  } catch (err: unknown) {
349
536
  if (err instanceof Error && err.name === 'AbortError') return
@@ -366,6 +553,9 @@ async function handleV2SessionEvent(event: V2SessionEvent): Promise<void> {
366
553
  case 'session.idle':
367
554
  await onSessionIdle(event)
368
555
  break
556
+ case 'session.deleted':
557
+ await onSessionDeleted(event)
558
+ break
369
559
  case 'session.error':
370
560
  await onSessionError(event)
371
561
  break
@@ -377,20 +567,68 @@ async function handleV2SessionEvent(event: V2SessionEvent): Promise<void> {
377
567
  }
378
568
  }
379
569
 
380
- async function onSessionCreated(_event: V2SessionEvent): Promise<void> {
381
- // Seed session state — handled by V1 bridge or direct registration.
382
- // The V2 event doesn't carry enough context for full hierarchy setup;
383
- // bridge.board is used when available for root-session registration.
570
+ /**
571
+ * Record the v2.0.29 lifecycle event in the opt-in F4A canary. Runtime events
572
+ * use a `{ id, created, type, data }` envelope; the child data carries
573
+ * `sessionID`, `parentID` and `agent`. Admission itself does not depend on this
574
+ * subscription: it checks the current permission source and `ctx.session.get`
575
+ * synchronously, so resumed roots and children use the same path.
576
+ */
577
+ async function onSessionCreated(event: V2SessionEvent): Promise<void> {
578
+ const payload = event as V2SessionEvent & Record<string, unknown>
579
+ const properties = event.properties ?? {}
580
+ const data =
581
+ event.data !== null && typeof event.data === 'object'
582
+ ? (event.data as Record<string, unknown>)
583
+ : {}
584
+ const info = properties.info ?? data.info
585
+ const sessionInfo =
586
+ info !== null && typeof info === 'object' ? (info as Record<string, unknown>) : {}
587
+ writeV2AdmissionProof('session.created', {
588
+ eventKeys: Object.keys(payload),
589
+ propertyKeys: Object.keys(properties),
590
+ dataKeys: Object.keys(data),
591
+ infoKeys: Object.keys(sessionInfo),
592
+ id:
593
+ sessionInfo.id ??
594
+ sessionInfo.sessionID ??
595
+ data.id ??
596
+ data.sessionID ??
597
+ properties.id ??
598
+ properties.sessionID ??
599
+ payload.sessionID ??
600
+ null,
601
+ parentID: sessionInfo.parentID ?? data.parentID ?? properties.parentID ?? null,
602
+ agent: sessionInfo.agent ?? data.agent ?? properties.agent ?? null,
603
+ })
384
604
  const _bridge = resolveBridge()
385
605
  if (_bridge?.board != null) {
386
- // Bridge available: board will register root sessions via the V1 event hook.
387
- // This is a no-op here — V1 handles hierarchy setup.
606
+ // Bridge present ⇒ this process also loaded V1, whose own event hook owns
607
+ // root-session registration. Intentionally nothing to do on the V2 side.
388
608
  }
389
609
  }
390
610
 
391
611
  async function onSessionIdle(_event: V2SessionEvent): Promise<void> {
392
- // Idle continuation — goal loop / todo enforcer dispatch.
393
- // Bridge provides the full V1 infrastructure when available.
612
+ // Temp-image sweep on the V2-only path: DELIBERATELY DEFERRED.
613
+ //
614
+ // V1 unlinks a session's temp images on `session.idle`/`session.deleted`
615
+ // (`cleanupSessionTempImages`, grace-guarded and refcounted). V2 cannot reuse
616
+ // it: `saveDataUrlImageSync` (the V2 materialization path) receives no session
617
+ // id, so there is no per-session `path → createdAt` map to sweep, and the
618
+ // content-hash temp dir is refcounted ONLY on the V1 path. The one reusable
619
+ // piece, `enforceTempFileCap`, is already invoked off every V2 save
620
+ // (`saveDataUrlImageSync`), so disk usage is bounded without an idle pass —
621
+ // re-running it here would be redundant, and a GLOBAL age-based unlink fired
622
+ // by one session's idle could delete a file another live session still
623
+ // references. Delivery is also best-effort: with no `ctx.event.subscribe`
624
+ // the stream is unsupported (`markUnsupported('event-stream')`) and no idle
625
+ // ever arrives. Implementing this safely needs V2-side session threading
626
+ // first; until then it stays deferred rather than approximated.
627
+ //
628
+ // Idle continuation — goal loop / todo enforcer dispatch. This one really
629
+ // does delegate: `bridge.todoEnforcer` is the V1 enforcer instance, reached
630
+ // only in a process that also loaded V1. On a V2-only install the bridge
631
+ // resolves to null and idle continuation does not run.
394
632
  const sessionID = _event.properties?.sessionID as string | undefined
395
633
  if (!sessionID) return
396
634
  const bridge = resolveBridge()
@@ -403,23 +641,75 @@ async function onSessionIdle(_event: V2SessionEvent): Promise<void> {
403
641
  }
404
642
  }
405
643
 
406
- async function onSessionError(_event: V2SessionEvent): Promise<void> {
407
- // Error handling — board transition to error.
408
- const bridge = resolveBridge()
409
- if (bridge?.board != null) {
410
- // Bridge available: error handling is done by the V1 event hook.
411
- // V2 only logs — V1 owns the board transition.
644
+ /**
645
+ * Session-deleted handler: drop the read-only registration.
646
+ *
647
+ * `readOnlyRegistry` is a process-wide singleton and the `execute.before` hook
648
+ * populates it on every delegated `apollo`/`gaia` call. V1 prunes it on
649
+ * `session.deleted` (`src/plugin.ts`); V2 did not, so on a long-lived host
650
+ * every finished investigation session stayed registered for the life of the
651
+ * process — a slow leak, and a stale entry that would keep denying tools if
652
+ * the id were ever reused.
653
+ *
654
+ * This mirrors V1: unregister the session, which drops its agent entry with it
655
+ * (the registry stores one `ReadOnlyEntry` per session id). V1 also clears a
656
+ * `sessionAgents` map here; V2 has no such map and needs no equivalent, since
657
+ * it reads the active agent off the event instead of caching it.
658
+ *
659
+ * The id is read from `properties.sessionID` — the field the sibling
660
+ * `session.idle` handler already uses — falling back to V1's
661
+ * `properties.info.id`. Both spellings are accepted because the V2 SDK types
662
+ * `session.deleted` as `{ sessionID, info }` and a miss here is a silent leak,
663
+ * which is precisely the bug this handler exists to close.
664
+ *
665
+ * A payload with NEITHER spelling is fail-open, and unavoidably so: unregistering
666
+ * is what needs the id, so an id that cannot be resolved cannot be cleaned up
667
+ * and the entry survives. The docstring used to leave that as a documented
668
+ * silent branch; it now warns on the way out, because the only other symptom of
669
+ * a renamed field is a registry that grows for the life of the process.
670
+ */
671
+ async function onSessionDeleted(event: V2SessionEvent): Promise<void> {
672
+ const properties = event.properties
673
+ if (properties === undefined) return
674
+ const info = properties.info
675
+ const sessionID =
676
+ typeof properties.sessionID === 'string'
677
+ ? properties.sessionID
678
+ : info !== null && typeof info === 'object'
679
+ ? (info as { id?: unknown }).id
680
+ : undefined
681
+ if (typeof sessionID !== 'string' || sessionID === '') {
682
+ // Fail-open by necessity, loud by choice: the entry for this session (if
683
+ // any) cannot be identified, so it stays registered. Same `console.warn`
684
+ // channel as the event-subscription error above — this module has no
685
+ // logger, and a diagnostic nobody reads on stderr is still a silent leak.
686
+ console.warn(
687
+ '[Pantheon V2] session.deleted carried no session id ' +
688
+ '(neither properties.sessionID nor properties.info.id); ' +
689
+ 'its read-only registration cannot be pruned and will persist.',
690
+ )
691
+ return
412
692
  }
693
+ readOnlyRegistry.unregister(sessionID)
413
694
  }
414
695
 
415
- async function onSessionCompacted(_event: V2SessionEvent): Promise<void> {
416
- // Compaction restore — todo preserve / context rebuild.
417
- const bridge = resolveBridge()
418
- if (bridge?.todoEnforcer != null) {
419
- // Bridge available: todo preservation is done by V1 compaction hook.
420
- // V2 only logs — V1 handles full compaction context build.
421
- }
422
- }
696
+ /**
697
+ * Session-error handler: no V2-side implementation.
698
+ *
699
+ * The board error transition lives in V1's event hook, which is not loaded on
700
+ * a V2-only install. There is nothing to do here — deliberately, rather than
701
+ * describing behaviour this handler does not have.
702
+ */
703
+ async function onSessionError(_event: V2SessionEvent): Promise<void> {}
704
+
705
+ /**
706
+ * Session-compacted handler: no V2-side implementation.
707
+ *
708
+ * Todo preservation and compaction context build live in the V1 path
709
+ * (`todo-preserve.ts`, driven by V1's compaction hook), which is not loaded on
710
+ * a V2-only install.
711
+ */
712
+ async function onSessionCompacted(_event: V2SessionEvent): Promise<void> {}
423
713
 
424
714
  // ─── V2 Session Hooks ───────────────────────────────────────────────────
425
715
 
@@ -433,11 +723,7 @@ async function onSessionCompacted(_event: V2SessionEvent): Promise<void> {
433
723
  * Returns true if any hooks were registered.
434
724
  */
435
725
  async function registerV2SessionHooks(context: PluginContext): Promise<boolean> {
436
- const sessionCtx = (context as unknown as Record<string, unknown>).session as
437
- | {
438
- hook?: (name: string, handler: (event: unknown) => void | Promise<void>) => Promise<unknown>
439
- }
440
- | undefined
726
+ const sessionCtx = hostDomains(context).session
441
727
 
442
728
  if (!sessionCtx?.hook) {
443
729
  return false
@@ -485,10 +771,31 @@ async function registerV2SessionHooks(context: PluginContext): Promise<boolean>
485
771
  }
486
772
 
487
773
  try {
488
- // "prompt" hook — vision message interception
489
- await sessionCtx.hook('prompt', (_event: unknown) => {
490
- // Vision interception delegates to the V1 vision handler.
491
- // Without V1 client access, this is a graceful no-op.
774
+ // "prompt" hook — preserve attached image references and add model-visible
775
+ // instructions so Zeus can inspect visual context and pass it to the
776
+ // implementation specialist before editing.
777
+ await sessionCtx.hook('prompt', (event: unknown) => {
778
+ try {
779
+ // 2.0.29 host payload: `{ sessionID, messageID, prompt: { text,
780
+ // files, agents, skills }, metadata, delivery }`. The session id is
781
+ // top-level (verified against the host's `SessionPrompt.prepare`), so a
782
+ // data: attachment's panic-free moment also relays it to the MCP.
783
+ const admission = event as {
784
+ sessionID?: unknown
785
+ prompt?: { text?: unknown; files?: unknown }
786
+ } | null
787
+ const prompt = admission?.prompt
788
+ if (!prompt || typeof prompt.text !== 'string') return
789
+ if (prompt.text.includes('<!-- pantheon-v2-visual-context -->')) return
790
+ const sessionID =
791
+ typeof admission?.sessionID === 'string' && admission.sessionID.trim() !== ''
792
+ ? admission.sessionID
793
+ : undefined
794
+ const visualContext = buildV2VisionPromptContext(prompt.files, prompt.text, sessionID)
795
+ if (visualContext) prompt.text = `${prompt.text}\n\n${visualContext}`
796
+ } catch {
797
+ // Fail-open: malformed/unsupported prompt shapes must not block input.
798
+ }
492
799
  })
493
800
  registered = true
494
801
  } catch {
@@ -498,8 +805,395 @@ async function registerV2SessionHooks(context: PluginContext): Promise<boolean>
498
805
  return registered
499
806
  }
500
807
 
808
+ // ─── V2 Secret Scan ─────────────────────────────────────────────────────
809
+
810
+ /**
811
+ * High-confidence secret token formats — MIRRORS `SECRET_MASK_RE` in
812
+ * src/plugins/pantheon-hooks.ts (V1) and `HIGH_CONFIDENCE_PATTERNS` in
813
+ * scripts/hooks/scan-secrets.sh.
814
+ *
815
+ * Duplicated rather than imported because the V1 module is a separate plugin
816
+ * that is NOT loaded on a V2-only install (see the module header), and pulling
817
+ * it in for one regex would resurrect exactly the V1-hook coupling V2 exists to
818
+ * remove. The drift risk is real and is asserted by a test in
819
+ * tests/pantheon/plugin-v2-contract.test.ts.
820
+ *
821
+ * Applied to every log line this module writes, so a hook trail can never
822
+ * contain a raw credential. Header/KEY names (the Bifrost header alone,
823
+ * `api_key=...`) are deliberately NOT matched: they are not secret values.
824
+ */
825
+ const SECRET_MASK_RE =
826
+ /(sk-bf-[A-Za-z0-9_-]{8,}|AKIA[0-9A-Z]{16}|gh[pousr]_[A-Za-z0-9_]{36,}|glpat-[A-Za-z0-9_-]{20}|sk-[a-zA-Z0-9]{20,}|sk_live_[a-zA-Z0-9]{20,}|sk_test_[a-zA-Z0-9]{20,}|xox[baprs]-[0-9]{10,13}-[0-9]{10,13}-[a-zA-Z0-9]{24}|bearer\s+[a-zA-Z0-9_.-]{20,}|eyJ[A-Za-z0-9_-]*\.eyJ[A-Za-z0-9_-]*\.[A-Za-z0-9_-]*)/gi
827
+
828
+ /** Replace every high-confidence secret match with '****'. */
829
+ function maskSecret(text: string): string {
830
+ return text.replace(SECRET_MASK_RE, '****')
831
+ }
832
+
833
+ /**
834
+ * Message thrown when scan-secrets.sh exits 2 (a high-confidence token match).
835
+ *
836
+ * This is the only deliberate throw on the V2 surface. opencode's
837
+ * Plugin.trigger has no catch around hook invocations, so the rejection
838
+ * propagates and fails the tool's execute promise BEFORE the tool body runs —
839
+ * the same hard block V1 has (src/plugins/pantheon-hooks.ts
840
+ * `SECRET_BLOCK_MESSAGE`), which was inert on a V2-only install because
841
+ * pantheon-hooks.ts is never loaded there (opencode.json `plugins` declares only
842
+ * `src/plugin-v2`).
843
+ *
844
+ * English, like every other message this module emits: this string surfaces in
845
+ * the user's session as the tool error, so it is read by whoever hit the block,
846
+ * not by the code that raised it. A Portuguese release note elsewhere in the
847
+ * repo is not a reason for the runtime message to be Portuguese too.
848
+ */
849
+ const SECRET_BLOCK_MESSAGE =
850
+ '[plugin-v2] Blocked: high-confidence secret detected in tool input — see .pantheon/logs/hooks.log'
851
+
852
+ /**
853
+ * Message thrown when the secret scanner cannot produce a trustworthy verdict —
854
+ * the script is missing/unexecutable, the child timed out or was killed, the
855
+ * payload could not be serialized, the report was malformed, or the exit status
856
+ * was unexpected.
857
+ *
858
+ * This is a deliberate fail-CLOSED denial. The previous handler blocked only
859
+ * `code === 2` and treated every other outcome as advisory — but the runner
860
+ * reports a missing script, a spawn failure and a payload-serialization failure
861
+ * as `code: 1` too (src/plugins/hook-runner.ts), so a broken scanner read as a
862
+ * clean advisory and the tool ran, silently disabling the only hard secret block
863
+ * on the V2 surface.
864
+ *
865
+ * English and user-facing, like {@link SECRET_BLOCK_MESSAGE}. It carries no
866
+ * scanner output on purpose: a scan that failed mid-flight may still be holding
867
+ * the raw payload, and the denial must not become the leak it exists to prevent.
868
+ */
869
+ const SECRET_SCAN_UNAVAILABLE_MESSAGE =
870
+ '[plugin-v2] Blocked: the secret scanner could not complete, so this tool call was denied (fail-closed) — see .pantheon/logs/hooks.log'
871
+
872
+ /**
873
+ * Append one masked, ISO-stamped line to .pantheon/logs/hooks.log under the
874
+ * working directory, and return whether it landed.
875
+ *
876
+ * V1's `reportFailure` also writes to `client.app.log` and shows a TUI toast.
877
+ * The V2 `PluginContext` exposes NEITHER (it is
878
+ * `{options, agent, aisdk, catalog, command, integration, plugin, reference,
879
+ * skill}` — no `client`, no `directory`), so this module has only the file
880
+ * channel. That is a real reduction in observability versus V1, stated here
881
+ * rather than implied. Best-effort by contract: a logging failure must never
882
+ * take down the session.
883
+ */
884
+ function appendV2HookLog(line: string): boolean {
885
+ const masked = maskSecret(line)
886
+ if (masked === '') return false
887
+ const stamp = new Date().toISOString()
888
+ // Multi-line scanner stderr would otherwise produce continuation lines with
889
+ // no timestamp (V1's P0-5 fix) — one message becomes N timestamped lines.
890
+ const body = masked
891
+ .split('\n')
892
+ .map((part) => part.trim())
893
+ .filter((part) => part !== '')
894
+ .map((part) => `[${stamp}] ${part}`)
895
+ .join('\n')
896
+ if (body === '') return false
897
+ try {
898
+ const dir = path.join(process.cwd(), '.pantheon', 'logs')
899
+ mkdirSync(dir, { recursive: true })
900
+ appendFileSync(path.join(dir, 'hooks.log'), `${body}\n`, 'utf8')
901
+ return true
902
+ } catch {
903
+ return false
904
+ }
905
+ }
906
+
907
+ // ─── BENCH Hook Timing (beta-10 latency slice, Phase 0) ────────────────
908
+
909
+ /**
910
+ * BENCH INSTRUMENTATION — observation only, zero behavior change.
911
+ *
912
+ * The beta-10 latency slice measures the V2 tool hook chain. These helpers
913
+ * record the start/end of `execute.before` and `execute.after` per tool call
914
+ * and append one single-line `[bench]` entry per hook end to the EXISTING
915
+ * hooks.log channel (same appendV2HookLog path as every other line: masked,
916
+ * stamped, best-effort, never throwing). The lines are consumed by
917
+ * bench/lib/hooklog.mjs.
918
+ *
919
+ * Deliberate properties:
920
+ * - the log call happens in `finally`, so denied calls (read-only guard,
921
+ * secret block) are timed too, and the denial still propagates unchanged
922
+ * - the join key is the host callID (`event.id`); a TTL-bounded map prunes
923
+ * stale entries so a background/never-after call cannot leak memory
924
+ * - `since_before_ms=unavailable` when no before record exists — the bench
925
+ * contract never lets "unavailable" be encoded as 0
926
+ */
927
+
928
+ /** Entries older than this are pruned (a task tool result may never fire after). */
929
+ const BENCH_TIMING_TTL_MS = 5 * 60_000
930
+ /** Hard cap — mirrors the V1 delegationStarts bound. */
931
+ const BENCH_TIMING_MAX = 256
932
+
933
+ const benchTimings = new Map<string, number>()
934
+
935
+ function pruneBenchTimings(now: number): void {
936
+ for (const [callID, startedAt] of benchTimings) {
937
+ if (now - startedAt > BENCH_TIMING_TTL_MS) benchTimings.delete(callID)
938
+ }
939
+ while (benchTimings.size >= BENCH_TIMING_MAX) {
940
+ let oldestID: string | null = null
941
+ let oldestAt = Number.POSITIVE_INFINITY
942
+ for (const [id, at] of benchTimings) {
943
+ if (at < oldestAt) {
944
+ oldestAt = at
945
+ oldestID = id
946
+ }
947
+ }
948
+ if (oldestID === null) break
949
+ benchTimings.delete(oldestID)
950
+ }
951
+ }
952
+
953
+ /** Read `{tool, id}` off a V2 tool event without assuming its exact shape. */
954
+ function benchEventIdentity(event: unknown): { tool: string; callID: string } {
955
+ if (event === null || typeof event !== 'object') return { tool: '', callID: '' }
956
+ const payload = event as { tool?: unknown; id?: unknown }
957
+ return {
958
+ tool: typeof payload.tool === 'string' ? payload.tool : '',
959
+ callID: typeof payload.id === 'string' ? payload.id : '',
960
+ }
961
+ }
962
+
963
+ /**
964
+ * Append one `[bench]` timing line for a finished hook phase. Best-effort and
965
+ * exception-free by construction (appendV2HookLog never throws).
966
+ */
967
+ function logBenchHookTiming(phase: 'before' | 'after', event: unknown, startedAt: number): void {
968
+ try {
969
+ const { tool, callID } = benchEventIdentity(event)
970
+ const endedAt = Date.now()
971
+ if (phase === 'before') {
972
+ if (callID !== '') {
973
+ benchTimings.set(callID, startedAt)
974
+ pruneBenchTimings(startedAt)
975
+ }
976
+ appendV2HookLog(
977
+ `[bench] tool.execute.before end tool=${tool} callID=${callID} ` +
978
+ `started_at=${new Date(startedAt).toISOString()} duration_ms=${endedAt - startedAt}`,
979
+ )
980
+ return
981
+ }
982
+ const beforeAt = callID !== '' ? benchTimings.get(callID) : undefined
983
+ if (beforeAt !== undefined) benchTimings.delete(callID)
984
+ const sinceBefore = beforeAt === undefined ? 'unavailable' : String(endedAt - beforeAt)
985
+ appendV2HookLog(
986
+ `[bench] tool.execute.after end tool=${tool} callID=${callID} ` +
987
+ `duration_ms=${endedAt - startedAt} since_before_ms=${sinceBefore}`,
988
+ )
989
+ } catch {
990
+ // Instrumentation must never affect the hook chain.
991
+ }
992
+ }
993
+
994
+ // ─── V2 Secret-Scan Verdict ─────────────────────────────────────────────
995
+
996
+ /**
997
+ * The scanner's own report marker, written to stderr on every non-clean run
998
+ * (exit 1 advisory, exit 2 block). It is the discriminator between a genuine
999
+ * low-confidence advisory and a runner infrastructure failure, BOTH of which
1000
+ * surface as `code === 1`: the runner's own failures (missing/unexecutable
1001
+ * script, spawn error, payload-serialization error) resolve a structured
1002
+ * `HookResult` with `code: 1` and no scan report, while the real scanner's
1003
+ * advisory always carries this marker. Requiring it means a scanner that stops
1004
+ * reporting fails toward a denial, never toward a silent allow.
1005
+ */
1006
+ const SECRET_SCAN_MARKER = '[SECRET SCAN]'
1007
+
1008
+ /** Sanitized failure classes — logged in place of raw scanner output. */
1009
+ type SecretScanFailureReason =
1010
+ | 'scanner-timeout'
1011
+ | 'scanner-signal'
1012
+ | 'scanner-spawn-failure'
1013
+ | 'scanner-serialization-failure'
1014
+ | 'scanner-malformed-output'
1015
+ | 'scanner-unexpected-exit'
1016
+
1017
+ type SecretScanDecision =
1018
+ | { action: 'allow' }
1019
+ | { action: 'advisory' }
1020
+ | { action: 'block' }
1021
+ | { action: 'fail-closed'; reason: SecretScanFailureReason }
1022
+
1023
+ /**
1024
+ * The runner's own infrastructure-failure messages (see hook-runner.ts), matched
1025
+ * at the start of stderr where the runner is the only writer. Matching a prefix
1026
+ * rather than a substring keeps a scan report that merely happens to quote one
1027
+ * of these phrases from flipping a valid advisory into a denial.
1028
+ */
1029
+ const SCANNER_INFRA_FAILURES: ReadonlyArray<[string, SecretScanFailureReason]> = [
1030
+ ['payload serialization failed:', 'scanner-serialization-failure'],
1031
+ ['spawn failed:', 'scanner-spawn-failure'],
1032
+ ['hook script failed to spawn:', 'scanner-spawn-failure'],
1033
+ ]
1034
+
1035
+ /**
1036
+ * Classify one secret-scan {@link HookResult} into an enforcement decision.
1037
+ *
1038
+ * fail-closed by construction: `allow` and `advisory` are reachable only from a
1039
+ * result that is unambiguously a completed scanner run. Everything else —
1040
+ * timeout, signal, spawn/serialization failure, missing report, unknown or
1041
+ * unexpected exit status — denies. `advisory` additionally requires the
1042
+ * scanner's own report marker, which is what separates an exit-1 advisory from
1043
+ * the runner's exit-1 infrastructure failure.
1044
+ *
1045
+ * `block` covers the high-confidence match (exit 2); the caller turns it into
1046
+ * the user-facing block.
1047
+ */
1048
+ function evaluateSecretScanResult(scan: HookResult): SecretScanDecision {
1049
+ if (scan.timedOut) return { action: 'fail-closed', reason: 'scanner-timeout' }
1050
+ if (scan.signal !== null) return { action: 'fail-closed', reason: 'scanner-signal' }
1051
+ if (!Number.isInteger(scan.code)) {
1052
+ return { action: 'fail-closed', reason: 'scanner-malformed-output' }
1053
+ }
1054
+ for (const [signature, reason] of SCANNER_INFRA_FAILURES) {
1055
+ if (scan.stderr.startsWith(signature)) return { action: 'fail-closed', reason }
1056
+ }
1057
+ if (scan.code === 0) return { action: 'allow' }
1058
+ if (scan.code === 2) return { action: 'block' }
1059
+ if (scan.code === 1) {
1060
+ return scan.stderr.includes(SECRET_SCAN_MARKER)
1061
+ ? { action: 'advisory' }
1062
+ : { action: 'fail-closed', reason: 'scanner-malformed-output' }
1063
+ }
1064
+ return { action: 'fail-closed', reason: 'scanner-unexpected-exit' }
1065
+ }
1066
+
1067
+ /** Invocation of scripts/hooks/scan-secrets.sh used by the tool guard. */
1068
+ type SecretScanRunner = (payload: HookPayload) => Promise<HookResult>
1069
+
1070
+ const defaultSecretScanRunner: SecretScanRunner = (payload) => runHook('scan-secrets.sh', payload)
1071
+
1072
+ let secretScanRunner: SecretScanRunner = defaultSecretScanRunner
1073
+
1074
+ /**
1075
+ * Test seam — override (or, with `null`, restore) the V2 secret-scan runner.
1076
+ *
1077
+ * Exported only for tests/pantheon/plugin-v2-contract.test.ts, which injects
1078
+ * structured {@link HookResult}s (missing scanner, timeout, malformed output)
1079
+ * that the real script cannot be made to produce on demand. There is no
1080
+ * production caller; the shipped handler always uses `defaultSecretScanRunner`.
1081
+ */
1082
+ export function setV2SecretScanRunnerForTests(runner: SecretScanRunner | null): void {
1083
+ secretScanRunner = runner ?? defaultSecretScanRunner
1084
+ }
1085
+
501
1086
  // ─── V2 Tool Hooks ──────────────────────────────────────────────────────
502
1087
 
1088
+ /**
1089
+ * The `tool.execute.before` event as the host dispatches it (opencode 2.0.22).
1090
+ *
1091
+ * Transcribed from the host's tool service:
1092
+ *
1093
+ * trigger("tool", "execute.before", { tool, sessionID, agent, messageID, id, input })
1094
+ *
1095
+ * Two shape differences from V1's positional `(input, output)` pair, both
1096
+ * handled by {@link adaptV2ExecuteBeforeEvent}: the host passes ONE payload
1097
+ * object, and the tool arguments live on `input` where V1 called them
1098
+ * `output.args`.
1099
+ *
1100
+ * `agent` is a required field of the host's tool context
1101
+ * (`ToolInvocation.context = { sessionID, agent, messageID, id }`), so the
1102
+ * active agent arrives on the very event the enforcement guard needs. V1 had to
1103
+ * learn it from a separate `chat.params` hook and keep a session→agent map; V2
1104
+ * does not have to, and neither does this module.
1105
+ */
1106
+ interface V2ToolExecuteBeforeEvent {
1107
+ tool?: unknown
1108
+ sessionID?: unknown
1109
+ messageID?: unknown
1110
+ agent?: unknown
1111
+ id?: unknown
1112
+ input?: unknown
1113
+ }
1114
+
1115
+ /**
1116
+ * Read-only enforcement for the V2 tool surface.
1117
+ *
1118
+ * The same factory, the same `DEFAULT_BLOCKED_TOOLS` and the same read-only set
1119
+ * that V1 uses, so the two surfaces cannot drift on what counts as a mutating
1120
+ * tool — `hashline_edit` and `pantheon_model` are denied here for exactly the
1121
+ * reason they are denied in V1.
1122
+ *
1123
+ * Deliberately NOT passed `getSessionAgent` / `isRootSession` / `isChildSession`.
1124
+ * There are TWO distinct reasons, and conflating them is what produced a wrong
1125
+ * causal chain here once already — keep them apart:
1126
+ *
1127
+ * 1. MISSING `getSessionAgent` (wiring). This is what would deny every
1128
+ * `task()` call. V2 learns the active agent from the `execute.before` event
1129
+ * itself and keeps no session→agent map, so there is no lookup to hand the
1130
+ * guard. The guard would call `isDelegationAllowed(undefined, target)`,
1131
+ * which returns `false`, and throw "caller agent is unavailable" — for
1132
+ * every caller, including Zeus. Note what is NOT happening: an unseeded
1133
+ * `isRootSession` would *pass* the root gate (`isRoot` reports `true` for an
1134
+ * unknown session), so the hierarchy is not what denies here.
1135
+ *
1136
+ * 2. UNWIRED HIERARCHY (design). `SessionHierarchyRegistry` is what gates
1137
+ * exactly two things, and nothing else: the depth-2 child deny
1138
+ * (`isChildSession`) and the root-session gate (`isRootSession`). Left
1139
+ * unseeded, `isChild` is `false` for every session, so depth-2 would never
1140
+ * fire on a genuine child — a hierarchy that cannot tell a child from a root
1141
+ * is not a trustworthy input to either check.
1142
+ *
1143
+ * "Unwired", not "impossible": the V2 `session.created` event carries
1144
+ * `properties.info: Session` including `parentID?`, the same field V1 seeds
1145
+ * from on the same event, so one line in `onSessionCreated` would seed live
1146
+ * sessions (see its docstring). What has no V2 equivalent is V1's SECOND
1147
+ * source, the `client.session.list()` startup seed — V2's `PluginContext`
1148
+ * exposes no `client`. Whether a live 2.0.22 host populates `info.parentID`
1149
+ * is UNVERIFIED; no canary reads an event payload. So the correct summary is
1150
+ * "seeded for nothing", not "unseedable", and the guard's omission is right
1151
+ * today for the weaker reason that nothing seeds it at all.
1152
+ *
1153
+ * Omitting all three skips the branch wholesale: the guard only enters it when
1154
+ * `options?.isRootSession !== undefined`. `task` is still denied inside a
1155
+ * read-only session via the blocked-tool list, so depth-2 holds for
1156
+ * apollo/gaia; the caller/target matrix itself is NOT enforced on V2. That is
1157
+ * a known gap, not a covered case.
1158
+ */
1159
+ const v2EnforcementGuard = createEnforcementGuard({
1160
+ getReadOnlySessions: () => readOnlyRegistry.sessionIDs(),
1161
+ })
1162
+
1163
+ /**
1164
+ * Project a V2 `execute.before` event onto the two-argument shape
1165
+ * `createEnforcementGuard` was built for.
1166
+ *
1167
+ * `tool` is the host's registration id, computed as
1168
+ * `options.namespace === undefined ? name : `${namespace}_${name}``. Pantheon
1169
+ * declares a namespace for catalog grouping but sets `options.namespace` on no
1170
+ * tool, so the id is the bare name (`hashline_edit`) and the blocked-tool match
1171
+ * fires. A tool that opted into `options.namespace` would gain a prefix and stop
1172
+ * matching — the integration test in tests/pantheon/plugin-v2-contract.test.ts
1173
+ * drives the real registration path and asserts on the registered names, so
1174
+ * that drift fails a test instead of silently disabling enforcement.
1175
+ *
1176
+ * A missing `sessionID` maps to the empty key: the registry lookup then matches
1177
+ * the event's own (absent) identity, so a blocked tool in a read-only session is
1178
+ * still denied. Fail-closed on the one axis that matters.
1179
+ */
1180
+ function adaptV2ExecuteBeforeEvent(event: unknown): {
1181
+ input: ToolExecuteBeforeInput
1182
+ output: ToolExecuteBeforeOutput
1183
+ agent: unknown
1184
+ } {
1185
+ const payload = (event ?? {}) as V2ToolExecuteBeforeEvent
1186
+ return {
1187
+ input: {
1188
+ tool: typeof payload.tool === 'string' ? payload.tool : '',
1189
+ sessionID: typeof payload.sessionID === 'string' ? payload.sessionID : '',
1190
+ callID: typeof payload.id === 'string' ? payload.id : '',
1191
+ },
1192
+ output: { args: payload.input },
1193
+ agent: payload.agent,
1194
+ }
1195
+ }
1196
+
503
1197
  /**
504
1198
  * Register tool execution hooks via V2 ctx.tool.hook.
505
1199
  *
@@ -509,43 +1203,142 @@ async function registerV2SessionHooks(context: PluginContext): Promise<boolean>
509
1203
  *
510
1204
  * Returns true if any hooks were registered.
511
1205
  */
512
- async function registerV2ToolHooks(context: PluginContext): Promise<boolean> {
513
- const toolCtx = (context as unknown as Record<string, unknown>).tool as
514
- | {
515
- hook?: (name: string, handler: (event: unknown) => void | Promise<void>) => Promise<unknown>
516
- }
517
- | undefined
1206
+ async function registerV2ToolHooks(
1207
+ context: PluginContext,
1208
+ subagentOrigins: V2SubagentOriginRegistry,
1209
+ boundedReadOrigins?: V2BoundedReadOriginRegistry,
1210
+ ): Promise<{ registered: boolean; beforeRegistered: boolean }> {
1211
+ const toolCtx = hostDomains(context).tool
518
1212
 
519
1213
  if (!toolCtx?.hook) {
520
- return false
1214
+ return { registered: false, beforeRegistered: false }
521
1215
  }
522
1216
 
523
1217
  let registered = false
1218
+ let beforeRegistered = false
524
1219
 
525
1220
  try {
526
- // "execute.before" — read-only enforcement + command normalization
527
- await toolCtx.hook('execute.before', (_event: unknown) => {
528
- // Enforcement guard runs via V1 bridge when infrastructure is available.
529
- // The V2 hook is a registration point; actual enforcement is wired
530
- // through the V1 tool.execute.before hook.
531
- const bridge = resolveBridge()
532
- if (bridge?.board != null) {
533
- // Bridge available: V1 enforcement guard handles read-only + command normalization.
534
- // No additional V2-side enforcement needed.
1221
+ // "execute.before" — read-only enforcement. Throwing from this handler
1222
+ // DENIES the tool call and the host surfaces the message to the session,
1223
+ // which is how the guard blocks a mutating tool in a read-only session.
1224
+ //
1225
+ // This is the enforcement point for the V2 surface, not a delegation to
1226
+ // V1: `src/plugin.ts` is not loaded when only `plugin-v2` is configured
1227
+ // (opencode.json `plugins`), so the V1 `tool.execute.before` hook does not
1228
+ // exist in that process. The guard below is instantiated here and consulted
1229
+ // here; without it the tools registered by registerV2Tools would be a live
1230
+ // write path in every session, including read-only ones.
1231
+ await toolCtx.hook('execute.before', async (event: unknown) => {
1232
+ const benchStartedAt = Date.now()
1233
+ const { input, output, agent } = adaptV2ExecuteBeforeEvent(event)
1234
+ try {
1235
+ // V2's equivalent of V1's `chat.params` trigger: the host puts the active
1236
+ // agent on the event itself, so registering here needs no separate hook
1237
+ // and no session→agent map. A non-read-only (or absent) agent revokes the
1238
+ // registration, which also covers an in-session agent switch.
1239
+ syncReadOnlySession(readOnlyRegistry, input.sessionID, agent)
1240
+ // Deliberately OUTSIDE the try/catch below: a read-only denial must keep
1241
+ // propagating to the host, not be swallowed as a scanner failure.
1242
+ await v2EnforcementGuard(input, output)
1243
+
1244
+ // High-confidence secret scan (scripts/hooks/scan-secrets.sh). This is the
1245
+ // ONLY hard block on the V2 surface, and it exists here — rather than via
1246
+ // `src/plugins/pantheon-hooks.ts` — because that module is a separate
1247
+ // plugin that is never loaded on a V2-only install: opencode.json
1248
+ // `plugins` declares only `src/plugin-v2`. V1's scan-secrets block was
1249
+ // therefore inert by construction, not by misconfiguration.
1250
+ //
1251
+ // exit 0 → allow. exit 1 with a valid scanner report → advisory, logged
1252
+ // only. exit 2 (high-confidence match) → deny. A scanner that does NOT
1253
+ // produce a trustworthy verdict — missing/unexecutable script, timeout,
1254
+ // killed child, spawn/serialization failure, malformed or unexpected
1255
+ // output — is fail-CLOSED: this pending tool call is denied, the session
1256
+ // is not. (runHook resolves a structured result instead of rejecting, so
1257
+ // the exit-code-only reading below cannot lean on an exception.)
1258
+ let blockError: Error | null = null
1259
+ try {
1260
+ const payload: HookPayload = {
1261
+ tool_name: input.tool,
1262
+ tool_input: output.args ?? {},
1263
+ agent_id: typeof agent === 'string' ? agent : '',
1264
+ session_id: input.sessionID,
1265
+ }
1266
+ const scan = await secretScanRunner(payload)
1267
+ const decision = evaluateSecretScanResult(scan)
1268
+ if (decision.action === 'block') {
1269
+ appendV2HookLog(SECRET_BLOCK_MESSAGE)
1270
+ appendV2HookLog(scan.stderr)
1271
+ blockError = new Error(SECRET_BLOCK_MESSAGE)
1272
+ } else if (decision.action === 'advisory') {
1273
+ // Advisory: log only, never block.
1274
+ appendV2HookLog(scan.stderr)
1275
+ } else if (decision.action === 'fail-closed') {
1276
+ // Sanitized failure category only — never the scanner's raw stderr,
1277
+ // which on a failure path is unverified and may carry the very
1278
+ // payload the scan exists to keep out of logs.
1279
+ appendV2HookLog(`${SECRET_SCAN_UNAVAILABLE_MESSAGE} (${decision.reason})`)
1280
+ blockError = new Error(SECRET_SCAN_UNAVAILABLE_MESSAGE)
1281
+ }
1282
+ } catch {
1283
+ // The runner is contracted to resolve a structured result, so this only
1284
+ // guards an unexpected throw (including a future change to the runner).
1285
+ // A scanner that cannot run is fail-CLOSED: deny this call rather than
1286
+ // let it through with no scan at all.
1287
+ appendV2HookLog(SECRET_SCAN_UNAVAILABLE_MESSAGE)
1288
+ blockError = new Error(SECRET_SCAN_UNAVAILABLE_MESSAGE)
1289
+ }
1290
+ if (blockError !== null) throw blockError
1291
+ const boundedReadOrigin = boundedReadOrigins?.capture(event)
1292
+ const eventTool = (event as V2ToolExecuteBeforeEvent | null)?.tool
1293
+ if (eventTool === BOUNDED_READ_TOOL && boundedReadOrigin === undefined) {
1294
+ throw new Error('BOUND_READ_UNAUTHORIZED')
1295
+ }
1296
+ if (boundedReadOrigin !== undefined) {
1297
+ writeV2AdmissionProof('tool.execute.before bounded-read', {
1298
+ sessionID: boundedReadOrigin.sessionID,
1299
+ messageID: boundedReadOrigin.messageID,
1300
+ callID: boundedReadOrigin.callID,
1301
+ agent: boundedReadOrigin.agent,
1302
+ })
1303
+ }
1304
+ const origin = subagentOrigins.capture(event)
1305
+ if (origin !== undefined) {
1306
+ writeV2AdmissionProof('tool.execute.before', { ...origin })
1307
+ }
1308
+ } finally {
1309
+ // BENCH instrumentation — observation only; runs on the deny paths too.
1310
+ logBenchHookTiming('before', event, benchStartedAt)
535
1311
  }
536
1312
  })
537
1313
  registered = true
1314
+ beforeRegistered = true
538
1315
  } catch {
539
1316
  // Tool before-hook not supported
540
1317
  }
541
1318
 
542
1319
  try {
543
- // "execute.after" — hashline read enhance + context sandbox
544
- await toolCtx.hook('execute.after', (_event: unknown) => {
545
- // Read enhancer + sandbox run via V1 bridge.
546
- const bridge = resolveBridge()
547
- if (bridge?.visionHandler != null) {
548
- // Bridge available: V1 vision handler + read enhancer handle post-execution.
1320
+ // "execute.after" — the V1 parity chain (task-result-guard →
1321
+ // context-sandbox → read-enhancer) rebuilt on the V2 event shape.
1322
+ //
1323
+ // This IS behaviour, not a registration point: on a V2-only install the V1
1324
+ // `pantheon-hooks` plugin is not loaded, so this handler is the only thing
1325
+ // that truncates oversized output and tags `read` with hashline refs.
1326
+ // The sandbox config comes from `ctx.options.context_sandbox` (the V2 copy
1327
+ // the installer writes — V2.0.25 drops the V1 top-level key), resolved with
1328
+ // the same resolver V1 uses.
1329
+ const v2ExecuteAfter = createV2ExecuteAfter({
1330
+ sandbox: resolveV2SandboxConfig(context.options),
1331
+ })
1332
+ await toolCtx.hook('execute.after', async (event: unknown) => {
1333
+ const benchStartedAt = Date.now()
1334
+ try {
1335
+ subagentOrigins.forget(event)
1336
+ boundedReadOrigins?.forget(event)
1337
+ await v2ExecuteAfter(event)
1338
+ } finally {
1339
+ // BENCH instrumentation — observation only; the awaited call means the
1340
+ // duration covers the whole parity chain, not just its dispatch.
1341
+ logBenchHookTiming('after', event, benchStartedAt)
549
1342
  }
550
1343
  })
551
1344
  registered = true
@@ -553,7 +1346,7 @@ async function registerV2ToolHooks(context: PluginContext): Promise<boolean> {
553
1346
  // Tool after-hook not supported
554
1347
  }
555
1348
 
556
- return registered
1349
+ return { registered, beforeRegistered }
557
1350
  }
558
1351
 
559
1352
  // ─── V2 Permission Hook ─────────────────────────────────────────────────
@@ -562,16 +1355,18 @@ async function registerV2ToolHooks(context: PluginContext): Promise<boolean> {
562
1355
  * Register permission hook via V2 ctx.permission.hook.
563
1356
  *
564
1357
  * The V2 permission.hook API (when available) provides:
565
- * ctx.permission.hook("evaluate", handler) — custom permission logic
1358
+ * ctx.permission.hook("evaluate", handler) — host permission evaluation
1359
+ * The handler receives one mutable PermissionEvaluation event; decisions are
1360
+ * written to event.effect/event.message (there is no separate V1-style output).
566
1361
  *
567
1362
  * Returns true if the hook was registered.
568
1363
  */
569
- async function registerV2PermissionHook(context: PluginContext): Promise<boolean> {
570
- const permCtx = (context as unknown as Record<string, unknown>).permission as
571
- | {
572
- hook?: (name: string, handler: (event: unknown) => void | Promise<void>) => Promise<unknown>
573
- }
574
- | undefined
1364
+ async function registerV2PermissionHook(
1365
+ context: PluginContext,
1366
+ subagentOrigins: V2SubagentOriginRegistry,
1367
+ ): Promise<boolean> {
1368
+ const domains = hostDomains(context)
1369
+ const permCtx = domains.permission
575
1370
 
576
1371
  if (!permCtx?.hook) {
577
1372
  markUnsupported('permission-hook')
@@ -579,9 +1374,117 @@ async function registerV2PermissionHook(context: PluginContext): Promise<boolean
579
1374
  }
580
1375
 
581
1376
  try {
582
- await permCtx.hook('evaluate', (_event: unknown) => {
583
- // Custom permission logic — Zeus read guard + read-only enforcement.
584
- // Wired through V1 tool.execute.before when infrastructure is available.
1377
+ await permCtx.hook('evaluate', async (rawEvent: unknown) => {
1378
+ const event = rawEvent as V2PermissionEvent | null
1379
+ const effectBefore = event?.effect
1380
+ let origin: ReturnType<V2SubagentOriginRegistry['consume']>
1381
+ let decision: string | null = null
1382
+ let callerSessionParentID: unknown
1383
+ const writeProof = (): void => {
1384
+ const proofPath = process.env.PANTHEON_HOOK_CANARY_PERMISSION_PROOF
1385
+ if (!proofPath || !event || !Array.isArray(event.resources)) return
1386
+ try {
1387
+ appendFileSync(
1388
+ proofPath,
1389
+ `permission.evaluate ${JSON.stringify({
1390
+ action: event.action,
1391
+ resources: event.resources,
1392
+ agent: event.agent,
1393
+ sessionID: event.sessionID,
1394
+ callerSessionParentID: callerSessionParentID ?? null,
1395
+ source: event.source,
1396
+ matchedOrigin: origin ?? null,
1397
+ decision,
1398
+ effect: event.effect,
1399
+ message: event.message,
1400
+ eventFields: ['action', 'resources', 'agent', 'sessionID', 'source', 'effect'].filter(
1401
+ (key) => Object.hasOwn(event, key),
1402
+ ),
1403
+ effectBefore: effectBefore ?? null,
1404
+ effectAfter: event.effect ?? null,
1405
+ })}\n`,
1406
+ )
1407
+ } catch {
1408
+ // Instrumentation is best-effort and must not affect permission flow.
1409
+ }
1410
+ }
1411
+ const deny = (reason: string): void => {
1412
+ if (event === null || typeof event !== 'object') {
1413
+ throw new Error(
1414
+ `permission event is unavailable; denying V2 permission evaluation (${reason})`,
1415
+ )
1416
+ }
1417
+ event.effect = 'deny'
1418
+ event.message = `Pantheon delegation denied: ${reason}`
1419
+ if (event.effect !== 'deny') {
1420
+ throw new Error(
1421
+ `permission event is not mutable; denying V2 permission evaluation (${reason})`,
1422
+ )
1423
+ }
1424
+ writeProof()
1425
+ }
1426
+
1427
+ if (event?.effect === 'deny') {
1428
+ writeProof()
1429
+ return // explicit host deny is final
1430
+ }
1431
+ if (event == null) {
1432
+ deny('permission event is missing')
1433
+ return
1434
+ }
1435
+ if (!Array.isArray(event.resources) || event.resources.length === 0) {
1436
+ deny('permission resources are missing or malformed')
1437
+ return
1438
+ }
1439
+ const targets = event.resources.map(v2ResourceTarget)
1440
+ if (targets.some((target) => target === undefined)) {
1441
+ deny('permission resource target is malformed')
1442
+ return
1443
+ }
1444
+ const hasPantheonTarget = targets.some((target) => PANTHEON_AGENTS.has(target ?? ''))
1445
+ let session: AuthoritativeSession | undefined
1446
+ if (hasPantheonTarget) {
1447
+ origin = subagentOrigins.consume(event)
1448
+ if (origin === undefined) {
1449
+ deny('matching host subagent tool origin is missing or stale')
1450
+ return
1451
+ }
1452
+ const sessionID = typeof event.sessionID === 'string' ? event.sessionID.trim() : ''
1453
+ if (!sessionID) {
1454
+ deny('caller session ID is missing')
1455
+ return
1456
+ }
1457
+
1458
+ const sessionDomain = domains.session
1459
+ if (!sessionDomain || typeof sessionDomain.get !== 'function') {
1460
+ deny('session lookup API is unavailable')
1461
+ return
1462
+ }
1463
+
1464
+ let retrieved: unknown
1465
+ try {
1466
+ retrieved = await sessionDomain.get({ sessionID })
1467
+ } catch {
1468
+ deny('caller session lookup failed')
1469
+ return
1470
+ }
1471
+ if (retrieved === null || retrieved === undefined) {
1472
+ deny('caller session was not found')
1473
+ return
1474
+ }
1475
+ if (typeof retrieved !== 'object' || Array.isArray(retrieved)) {
1476
+ deny('caller session lookup returned invalid data')
1477
+ return
1478
+ }
1479
+ session = retrieved as AuthoritativeSession
1480
+ callerSessionParentID = session.parentID
1481
+ }
1482
+
1483
+ // Native-only resources remain under host policy. If a Pantheon target
1484
+ // appears anywhere, the shared policy evaluates the complete resource set.
1485
+ decision = evaluateV2Delegation(event, session, event.effect, origin)
1486
+ if (decision === 'deny') deny('caller or target is not allowed by delegation policy')
1487
+ else writeProof()
585
1488
  })
586
1489
  return true
587
1490
  } catch {
@@ -593,7 +1496,15 @@ async function registerV2PermissionHook(context: PluginContext): Promise<boolean
593
1496
  // ─── Tool Definition Factory ─────────────────────────────────────────────
594
1497
 
595
1498
  /**
596
- * Internal types for V2 tool draft.
1499
+ * Minimal structural view of the V2 tool draft.
1500
+ *
1501
+ * `output` is REQUIRED. The host (opencode 2.0.22) enforces a biconditional on
1502
+ * a tool's declared `output` and the value its `execute` resolves to:
1503
+ * `def.output === undefined` together with `'output' in result` throws
1504
+ * "Tool result declared output without an output schema", and a declared
1505
+ * `output` whose result omits the key throws "Tool did not return its
1506
+ * declared output". A missing declaration therefore fails every call, so the
1507
+ * field is mandatory here rather than optional.
597
1508
  */
598
1509
  interface V2ToolDraft {
599
1510
  namespace(config: { name: string; description: string }): void
@@ -601,91 +1512,12 @@ interface V2ToolDraft {
601
1512
  name: string
602
1513
  description: string
603
1514
  input: Record<string, unknown>
1515
+ output: Record<string, unknown>
604
1516
  execute: (input: Record<string, unknown>, context: unknown) => Promise<V2ToolResult>
605
1517
  }): void
606
1518
  }
607
1519
 
608
- /**
609
- * V2 tool result shape.
610
- *
611
- * The V1 SDK types `ToolResult` as `string | { output: string, ... }`, but
612
- * the V2 beta host reads a field off the resolved value (minified `Ce`) and
613
- * throws `Ce is not an Object` when a tool resolves to a bare string. Our
614
- * handlers are fail-open, so that host-side TypeError escapes instead of
615
- * being contained. Always resolve to the object shape — it satisfies both
616
- * the SDK union and the beta host.
617
- */
618
- export interface V2ToolResult {
619
- output: string
620
- }
621
-
622
- /**
623
- * A V2 tool definition ready for registration.
624
- */
625
- interface V2ToolDef {
626
- name: string
627
- description: string
628
- input: Record<string, unknown>
629
- execute: (input: Record<string, unknown>, context: unknown) => Promise<V2ToolResult>
630
- }
631
-
632
- /**
633
- * Create V2 tool definitions from the plugin context.
634
- *
635
- * When V1 infrastructure (BackgroundJobBoard, delegation client, etc.) is
636
- * available through the V2 context, tools are wired directly. Otherwise,
637
- * each tool returns an error message explaining the limitation.
638
- */
639
- function createV2ToolDefinitionsFromContext(_context: PluginContext): V2ToolDef[] {
640
- // Tool definitions are created lazily. When the full V1 infrastructure
641
- // is bridged through the V2 context, these can be wired to real implementations.
642
- // For now, they are structural definitions with placeholder execute functions
643
- // that explain the V2 limitation.
644
- const v2UnavailableMessage =
645
- 'This tool requires V1 infrastructure (BackgroundJobBoard, SDK client). ' +
646
- 'Use the V1 plugin (src/plugin.ts) for full tool support, or wire V1 ' +
647
- 'infrastructure through the V2 bridge (src/pantheon/v2-bridge.ts).'
648
-
649
- const toolNames = [
650
- 'hashline_edit',
651
- 'pantheon_goal_create',
652
- 'pantheon_goal_get',
653
- 'pantheon_goal_update',
654
- 'pantheon_cost',
655
- 'pantheon_model',
656
- ]
657
-
658
- const toolDescriptions: Record<string, string> = {
659
- hashline_edit:
660
- 'Edit a file anchored by hashline refs (LINE#TAG) instead of raw line numbers. ' +
661
- 'Ops: replace, append, prepend, delete. ALL refs validated against ORIGINAL file first.',
662
- pantheon_goal_create:
663
- 'Create the single active goal for this session. The goal loop then continues the ' +
664
- 'session automatically on idle until the goal is marked done.',
665
- pantheon_goal_get:
666
- 'Return the active goal for this session (status, objective, continuations).',
667
- pantheon_goal_update:
668
- 'Update the active goal: set status (pending/in_progress/done) and/or rewrite the objective. ' +
669
- 'Setting status to done stops all further continuations.',
670
- pantheon_cost:
671
- 'Report delegation cost + token usage by agent over the last N days, read from opencode.db.',
672
- pantheon_model:
673
- 'Show, set, or reset per-agent model overrides in active-preset.json (project or global).',
674
- }
675
-
676
- return toolNames.map((name) => ({
677
- name,
678
- description: toolDescriptions[name] ?? `Pantheon tool: ${name}`,
679
- input: { type: 'object' as const, properties: {} },
680
- // FOLLOW-UP: wire the real tool wrappers (costCommand/goalTools) once V1
681
- // infrastructure is resolvable in the V2 standalone context. Until then,
682
- // placeholders resolve to the object shape so the beta host never sees a
683
- // bare string.
684
- execute: async (_input: Record<string, unknown>, _context: unknown): Promise<V2ToolResult> => {
685
- return { output: `${name}: ${v2UnavailableMessage}` }
686
- },
687
- }))
688
- }
1520
+ export type { V2ToolDef, V2ToolResult } from './pantheon/v2-tools.ts'
689
1521
 
690
1522
  // ─── Plugin Definition ───────────────────────────────────────────────────
691
1523
 
@@ -693,6 +1525,83 @@ function createV2ToolDefinitionsFromContext(_context: PluginContext): V2ToolDef[
693
1525
  interface V2SessionEvent {
694
1526
  type: string
695
1527
  properties?: Record<string, unknown>
1528
+ data?: unknown
1529
+ }
1530
+
1531
+ /** Opt-in trace used by the isolated F4A canary; never logs prompts or inputs. */
1532
+ function writeV2AdmissionProof(label: string, detail: Record<string, unknown>): void {
1533
+ const proofPath = process.env.PANTHEON_HOOK_CANARY_PERMISSION_PROOF
1534
+ if (!proofPath) return
1535
+ try {
1536
+ appendFileSync(proofPath, `${label} ${JSON.stringify(detail)}\n`)
1537
+ } catch {
1538
+ // Proof is best-effort and must not affect admission behavior.
1539
+ }
1540
+ }
1541
+
1542
+ async function prepareV2BoundedRead(
1543
+ context: PluginContext,
1544
+ ): Promise<ReturnType<typeof createV2BoundedReadTool> | undefined> {
1545
+ const host = hostDomains(context)
1546
+ const directory = host.location?.directory
1547
+ const projectID = host.location?.project?.id
1548
+ const session = host.session
1549
+ const permission = host.permission
1550
+ if (
1551
+ typeof directory !== 'string' ||
1552
+ directory.trim() === '' ||
1553
+ typeof projectID !== 'string' ||
1554
+ projectID.trim() === '' ||
1555
+ typeof session?.get !== 'function' ||
1556
+ typeof permission?.assert !== 'function' ||
1557
+ typeof host.event?.subscribe !== 'function' ||
1558
+ typeof host.tool?.transform !== 'function' ||
1559
+ typeof host.tool?.hook !== 'function'
1560
+ ) {
1561
+ markUnsupported('bounded-read')
1562
+ return undefined
1563
+ }
1564
+
1565
+ const rootDirectory = await realpathAsync(directory).catch(() => undefined)
1566
+ if (rootDirectory === undefined) {
1567
+ markUnsupported('bounded-read')
1568
+ return undefined
1569
+ }
1570
+
1571
+ return createV2BoundedReadTool({
1572
+ rootDirectory,
1573
+ projectID,
1574
+ getSession: (sessionID) => {
1575
+ if (typeof session.get !== 'function') throw new Error('session lookup unavailable')
1576
+ return session.get.call(session, { sessionID })
1577
+ },
1578
+ assertPermission: (request) => {
1579
+ if (typeof permission.assert !== 'function') throw new Error('permission API unavailable')
1580
+ return permission.assert.call(permission, request)
1581
+ },
1582
+ scanContent: async ({ content, agent, sessionID, callID }) => {
1583
+ try {
1584
+ const scan = await secretScanRunner({
1585
+ tool_name: BOUNDED_READ_TOOL,
1586
+ tool_input: { content },
1587
+ agent_id: agent,
1588
+ session_id: sessionID,
1589
+ })
1590
+ const decision = evaluateSecretScanResult(scan)
1591
+ if (decision.action === 'allow') return 'allow'
1592
+ const reason = decision.action === 'fail-closed' ? decision.reason : 'content-match'
1593
+ appendV2HookLog(
1594
+ `[SECRET SCAN] bounded read withheld session=${sessionID} callID=${callID} reason=${reason}`,
1595
+ )
1596
+ return decision.action === 'fail-closed' ? 'unavailable' : 'deny'
1597
+ } catch {
1598
+ appendV2HookLog(
1599
+ `[SECRET SCAN] bounded read withheld session=${sessionID} callID=${callID} reason=scanner-failure`,
1600
+ )
1601
+ return 'unavailable'
1602
+ }
1603
+ },
1604
+ })
696
1605
  }
697
1606
 
698
1607
  /** Cleanup function type. */
@@ -713,10 +1622,12 @@ export const plugin = define({
713
1622
  //
714
1623
  // Pantheon's Phase 1/config transform registrations cover agent, command,
715
1624
  // and reference; ctx.tool.transform is attempted separately in Phase 2.
716
- // The 2.0.18 runtime probe found ctx.integration and ctx.skill with
717
- // callable transforms, but no callback effects were observed; neither is
718
- // registered here. Only ctx.catalog was absent in that probe.
719
- // The hook canary tests hook firing only, not transform callback effects.
1625
+ // ctx.integration and ctx.skill are present on a live 2.0.22 host and neither
1626
+ // is registered here — that is a Pantheon scope choice, not a host gap.
1627
+ // ctx.catalog is the one domain the host does not provide.
1628
+ // Phase 1 registers against the SDK-typed `context.agent` / `.command` /
1629
+ // `.reference` directly; the host domains the SDK omits are reached only
1630
+ // through `hostDomains` (see the module header).
720
1631
  const phase1Transforms: Array<{ feature: string; register: () => Promise<unknown> }> = [
721
1632
  { feature: 'agent-transform', register: () => context.agent.transform(transformAgents) },
722
1633
  {
@@ -739,44 +1650,49 @@ export const plugin = define({
739
1650
  }
740
1651
  })
741
1652
 
742
- // ─── Phase 2: V2 Tool Registration (best-effort) ─────────────────
743
- const toolsRegistered = await registerV2Tools(context).catch(() => false)
744
- if (!toolsRegistered) {
745
- markUnsupported('tool-transform')
746
- }
1653
+ const subagentOrigins = new V2SubagentOriginRegistry()
1654
+ const boundedRead = await prepareV2BoundedRead(context).catch(() => undefined)
1655
+ if (boundedRead === undefined) markUnsupported('bounded-read')
747
1656
 
748
- // ─── Phase 3: V2 Event Subscription (best-effort) ────────────────
749
- const eventCleanup = subscribeV2Events(context)
1657
+ // ─── Phase 2: V2 Event Subscription (best-effort) ────────────────
1658
+ const eventCleanup = subscribeV2Events(context, boundedRead?.clearSession)
750
1659
  if (eventCleanup) {
751
1660
  activeCleanups.push(eventCleanup)
752
1661
  } else {
753
1662
  markUnsupported('event-stream')
754
1663
  }
755
1664
 
756
- // ─── Phase 4: V2 Session Hooks (best-effort) ─────────────────────
1665
+ // ─── Phase 3: V2 Session Hooks (best-effort) ─────────────────────
757
1666
  const sessionHooksRegistered = await registerV2SessionHooks(context).catch(() => false)
758
1667
  if (!sessionHooksRegistered) {
759
1668
  markUnsupported('session-hooks')
760
1669
  }
761
1670
 
762
- // ─── Phase 5: V2 Tool Hooks (best-effort) ────────────────────────
763
- const toolHooksRegistered = await registerV2ToolHooks(context).catch(() => false)
764
- if (!toolHooksRegistered) {
1671
+ // ─── Phase 4: V2 Tool Hooks (best-effort) ────────────────────────
1672
+ const toolHookResult = await registerV2ToolHooks(
1673
+ context,
1674
+ subagentOrigins,
1675
+ boundedRead?.origins,
1676
+ ).catch(() => ({ registered: false, beforeRegistered: false }))
1677
+ if (!toolHookResult.registered) {
765
1678
  markUnsupported('tool-execute-hooks')
766
1679
  }
767
1680
 
1681
+ // ─── Phase 5: V2 Tool Registration (best-effort) ─────────────────
1682
+ const boundedReadDefinition = toolHookResult.beforeRegistered
1683
+ ? boundedRead?.definition
1684
+ : undefined
1685
+ if (boundedRead !== undefined && boundedReadDefinition === undefined) {
1686
+ markUnsupported('bounded-read')
1687
+ }
1688
+ const toolsRegistered = await registerV2Tools(context, boundedReadDefinition).catch(() => false)
1689
+ if (!toolsRegistered) markUnsupported('tool-transform')
1690
+
768
1691
  // ─── Phase 6: V2 Permission Hook (best-effort) ───────────────────
769
- await registerV2PermissionHook(context).catch(() => false)
1692
+ await registerV2PermissionHook(context, subagentOrigins).catch(() => false)
770
1693
 
771
1694
  // ─── Phase 7: V2 Compaction Hook (best-effort) ───────────────────
772
- const compactionCtx = (context as unknown as Record<string, unknown>).session as
773
- | {
774
- hook?: (
775
- name: string,
776
- handler: (event: unknown) => void | Promise<void>,
777
- ) => Promise<unknown>
778
- }
779
- | undefined
1695
+ const compactionCtx = hostDomains(context).session
780
1696
  if (compactionCtx?.hook) {
781
1697
  try {
782
1698
  // 2.0.16 `SessionHooks` key is `compaction` (the migration doc maps
@@ -786,7 +1702,10 @@ export const plugin = define({
786
1702
  // Proven by the canary's negative control.
787
1703
  await compactionCtx.hook('compaction', (_event: unknown) => {
788
1704
  // Compaction context build — injects active goals, pending todos,
789
- // and in-flight delegations. Wired through V1 infrastructure.
1705
+ // and in-flight delegations. No V2-side implementation: that build
1706
+ // lives in the V1 path, which is not loaded on a V2-only install.
1707
+ // This registration is a real hook (the canary below proves it
1708
+ // fires) with no V2 behaviour behind it.
790
1709
  //
791
1710
  // Opt-in lifetime proof for the host-backed hook canary
792
1711
  // (tests/pantheon/plugin-v2-hook-canary.test.mjs, MODE=real). It is
@@ -832,7 +1751,14 @@ export function v2Dispose(): void {
832
1751
  }
833
1752
 
834
1753
  /**
835
- * Get the current list of unsupported V2 features (for diagnostics).
1754
+ * The current list of unsupported V2 features.
1755
+ *
1756
+ * Read by the installer and by `doctor` (both via the shared seed in
1757
+ * `src/pantheon/v2-unsupported.mjs`) so the markers a user is shown are the
1758
+ * same ones this returns, rather than a hand-copied duplicate.
1759
+ *
1760
+ * This returns the LIVE array: the shared seed plus whatever `markUnsupported`
1761
+ * appended while this process talked to a real host.
836
1762
  */
837
1763
  export function getUnsupportedFeatures(): readonly string[] {
838
1764
  return V2_UNSUPPORTED_FEATURES