pantheon-opencode 1.6.2-beta.8 → 1.6.2

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 (81) hide show
  1. package/CHANGELOG.md +121 -3
  2. package/README.md +34 -60
  3. package/README.pt-BR.md +10 -12
  4. package/bin/pantheon-init.mjs +30 -32
  5. package/package.json +3 -3
  6. package/plugin.json +1 -1
  7. package/pyproject.toml +1 -1
  8. package/scripts/decision-router.mjs +41 -56
  9. package/scripts/install/decision-router-config.mjs +67 -1
  10. package/scripts/install/npx-cache-sweep.mjs +103 -0
  11. package/scripts/install/opencode.mjs +7 -7
  12. package/scripts/install/plugin.mjs +4 -3
  13. package/scripts/install/strings.mjs +4 -0
  14. package/scripts/release-gates.mjs +6 -82
  15. package/scripts/sync-tui.mjs +3 -3
  16. package/src/agents/aphrodite.md +1 -1
  17. package/src/agents/apollo.md +1 -0
  18. package/src/agents/athena.md +1 -0
  19. package/src/agents/demeter.md +1 -1
  20. package/src/agents/gaia.md +1 -0
  21. package/src/agents/hephaestus.md +1 -1
  22. package/src/agents/hermes.md +1 -1
  23. package/src/agents/iris.md +1 -1
  24. package/src/agents/mnemosyne.md +1 -0
  25. package/src/agents/nyx.md +1 -1
  26. package/src/agents/prometheus.md +1 -1
  27. package/src/agents/talos.md +1 -1
  28. package/src/agents/themis.md +1 -1
  29. package/src/agents/zeus.md +5 -2
  30. package/src/pantheon/jev-decision-tool.ts +366 -114
  31. package/src/pantheon/jev-decisions.ts +11 -13
  32. package/src/pantheon/jev-escalation.ts +639 -0
  33. package/src/pantheon/jev-quality-score.ts +432 -0
  34. package/src/pantheon/jev-risk-gate.ts +479 -0
  35. package/src/pantheon/local-decision-prompt.ts +12 -18
  36. package/src/pantheon/v2-decision-telemetry.ts +14 -4
  37. package/src/pantheon/v2-native-decisions.ts +1 -1
  38. package/src/plugin-v2.ts +201 -24
  39. package/src/plugins/tui/dist/server-v2.d.ts +19 -11
  40. package/src/plugins/tui/dist/server-v2.d.ts.map +1 -1
  41. package/src/plugins/tui/dist/server-v2.js +19 -789
  42. package/src/plugins/tui/dist/server-v2.js.map +1 -1
  43. package/src/plugins/tui/dist/tui-v2.d.ts +12 -331
  44. package/src/plugins/tui/dist/tui-v2.d.ts.map +1 -1
  45. package/src/plugins/tui/dist/tui-v2.js +257 -5370
  46. package/src/plugins/tui/dist/tui-v2.js.map +1 -1
  47. package/src/plugins/tui/dist/tui.js +1 -1
  48. package/src/plugins/tui/package-lock.json +2 -2
  49. package/src/plugins/tui/package.json +1 -1
  50. package/src/plugins/tui/src/index-v2.tsx +213 -86
  51. package/src/plugins/tui/src/server-v2.ts +18 -982
  52. package/src/plugins/tui/tsconfig.typecheck.json +1 -6
  53. package/src/plugins/tui/tsdown.config.ts +1 -11
  54. package/src/plugins/tui/dist/cost-data-CCZUs1aJ.js +0 -749
  55. package/src/plugins/tui/dist/cost-data-CCZUs1aJ.js.map +0 -1
  56. package/src/plugins/tui/dist/quota-adapters.d.ts +0 -89
  57. package/src/plugins/tui/dist/quota-adapters.d.ts.map +0 -1
  58. package/src/plugins/tui/dist/quota-adapters.js +0 -1142
  59. package/src/plugins/tui/dist/quota-adapters.js.map +0 -1
  60. package/src/plugins/tui/dist/quota-cli.d.ts +0 -17
  61. package/src/plugins/tui/dist/quota-cli.d.ts.map +0 -1
  62. package/src/plugins/tui/dist/quota-cli.js +0 -184
  63. package/src/plugins/tui/dist/quota-cli.js.map +0 -1
  64. package/src/plugins/tui/dist/quota-rpc-DMmXBhBe.js +0 -684
  65. package/src/plugins/tui/dist/quota-rpc-DMmXBhBe.js.map +0 -1
  66. package/src/plugins/tui/dist/usage-history-contract-nt1rnBPE.js +0 -118
  67. package/src/plugins/tui/dist/usage-history-contract-nt1rnBPE.js.map +0 -1
  68. package/src/plugins/tui/dist/usage-history-worker.js +0 -25
  69. package/src/plugins/tui/dist/usage-history-worker.js.map +0 -1
  70. package/src/plugins/tui/src/go-key-diagnostic-ui.ts +0 -100
  71. package/src/plugins/tui/src/go-key-diagnostic.ts +0 -125
  72. package/src/plugins/tui/src/quota-adapters.ts +0 -1598
  73. package/src/plugins/tui/src/quota-cli.ts +0 -237
  74. package/src/plugins/tui/src/quota-rpc.ts +0 -373
  75. package/src/plugins/tui/src/quota-v2-model.ts +0 -1032
  76. package/src/plugins/tui/src/quota-v2.tsx +0 -1238
  77. package/src/plugins/tui/src/usage-history-contract.ts +0 -217
  78. package/src/plugins/tui/src/usage-history-validation.ts +0 -104
  79. package/src/plugins/tui/src/usage-history-view.tsx +0 -236
  80. package/src/plugins/tui/src/usage-history-worker.ts +0 -25
  81. package/src/plugins/tui/src/usage-view-model.ts +0 -272
@@ -12,6 +12,11 @@
12
12
  * preparer. Transport is the host's own provider layer: this module never
13
13
  * touches fetch, endpoints, or credentials.
14
14
  *
15
+ * The gate chain lives in {@link runGatedNativeJevCall} so a second caller (the
16
+ * J1 risk-score shadow gate in `jev-risk-gate.ts`) reuses the exact same
17
+ * config → route → permission → reserve → decide → reconcile sequence instead
18
+ * of duplicating it; only the request preparer is caller-supplied.
19
+ *
15
20
  * Settlement follows the fetch runner's fail-closed contract: reserve before
16
21
  * the call, reconcile from the host-reported cost after it, and leave the
17
22
  * reservation pending (budget held for the period) when no evidence can be
@@ -21,15 +26,26 @@ import { readUserDecisionRouterConfig } from '../../scripts/install/decision-rou
21
26
  import {
22
27
  consumeJevDecisionResult,
23
28
  isJevDecisionPreset,
24
- type JevDecisionCatalogEntry,
29
+ type JevDecisionPreparation,
25
30
  type JevHostSkill,
26
31
  type NativeJevRouteFailure,
32
+ type NativeJevRouteSelection,
27
33
  prepareJevDecisionRequest,
28
34
  prepareJevPresetRequest,
29
35
  resolveNativeJevRoute,
30
36
  } from './jev-decisions.ts'
37
+ import {
38
+ buildJevReviewRequest,
39
+ encodeQualityScoreReason,
40
+ evaluateQualityScoreDecision,
41
+ J4_QUALITY_SCORE_POINT,
42
+ type QualityScoreEvaluation,
43
+ qualityVerdict,
44
+ } from './jev-quality-score.ts'
45
+ import { mapGateFailureToSkipReason } from './jev-risk-gate.ts'
31
46
  import {
32
47
  openLocalDecisionBudgetLedger,
48
+ type ReservationResult,
33
49
  resolveLocalDecisionBudgetDbPath,
34
50
  } from './local-decision-budget.ts'
35
51
  import {
@@ -37,11 +53,17 @@ import {
37
53
  buildDecisionEgressRequest,
38
54
  } from './local-decision-egress-gate.ts'
39
55
  import { verifyNativeUsageForReservation } from './local-decision-usage-evidence.internal.ts'
40
- import type { NativeDecisionRequest } from './v2-native-decisions.ts'
56
+ import { observeShadow, type ShadowSink } from './v2-decision-shadow.ts'
57
+ import type { NativeDecisionRequest, ResolvedNativeDecisionRoute } from './v2-native-decisions.ts'
41
58
  import type { V2ToolDef } from './v2-tools.ts'
42
59
 
43
60
  export interface V2JevDecisionRuntime {
44
- /** Raw host model catalog (`ctx.model.list`); may resolve to anything. */
61
+ /**
62
+ * Raw host model catalog (`ctx.model.list`); still wired, but the route no
63
+ * longer consults it — field evidence (2.2.5): decision models are not chat
64
+ * models and never appear in the catalog. Removed with the 2.0 runtime
65
+ * reshaping.
66
+ */
45
67
  readonly listModels: () => Promise<unknown>
46
68
  /** The host's native Decisions API (`ctx.experimental.generate.decisions`). */
47
69
  readonly decide: (request: NativeDecisionRequest) => Promise<unknown>
@@ -52,6 +74,13 @@ export interface V2JevDecisionRuntime {
52
74
  readonly listSkills?: () => Promise<unknown>
53
75
  /** Host permission kill-switch (`ctx.permission.assert`), when present. */
54
76
  readonly permissionAssert?: (input: Record<string, unknown>) => Promise<unknown>
77
+ /**
78
+ * Shadow-record sink for the J4 output-quality review telemetry
79
+ * (`jev-quality-score.ts`); optional. The plugin wires the in-memory,
80
+ * bounded telemetry collector here; absent sink → no J4 records, nothing
81
+ * else changes.
82
+ */
83
+ readonly shadowSink?: ShadowSink
55
84
  }
56
85
 
57
86
  /** Logical permission resource: no endpoint URL exists on the native route. */
@@ -61,11 +90,12 @@ export function nativeJevPermissionResource(providerID: string, id: string): str
61
90
 
62
91
  export type JevDecisionRunFailure =
63
92
  | 'CALLER_NOT_ZEUS'
93
+ | 'REVIEW_INPUT_INVALID'
94
+ | 'CRITERIA_INVALID'
64
95
  | 'CONTEXT_UNAVAILABLE'
65
96
  | 'CONFIG_UNAVAILABLE'
66
97
  | 'DISABLED'
67
98
  | 'CONSENT_MISSING'
68
- | 'CATALOG_UNAVAILABLE'
69
99
  | 'CAP_OR_PERIOD_DENIED'
70
100
  | NativeJevRouteFailure
71
101
  | 'PERMISSION_UNAVAILABLE'
@@ -91,103 +121,116 @@ function isNonBlankString(value: unknown): value is string {
91
121
  return typeof value === 'string' && value.trim().length > 0
92
122
  }
93
123
 
124
+ /** Map a review-mode refusal to its telemetry skip token. */
125
+ function reviewRefusalSkipReason(reasonCode: JevDecisionRunFailure): string {
126
+ if (reasonCode === 'REVIEW_INPUT_INVALID') return 'skip-review-input-invalid'
127
+ if (reasonCode === 'CRITERIA_INVALID') return 'skip-criteria-invalid'
128
+ return mapGateFailureToSkipReason(reasonCode)
129
+ }
130
+
131
+ // ─── Shared gate chain ───────────────────────────────────────────────────
132
+
94
133
  /**
95
- * Read the host's installed skills for the stacking preset. Anything that is
96
- * not a plain {id, description?} record is skipped; a missing or hostile
97
- * registry degrades to the catalog-only preset. Reading the registry applies
98
- * no trust: installation already was the user's manual, reviewed act.
134
+ * Every gate-chain failure the shared runner can produce. It is
135
+ * {@link JevDecisionRunFailure} minus the tool-surface properties:
136
+ * `CALLER_NOT_ZEUS` (the caller check) and the review mode's input
137
+ * validation (`REVIEW_INPUT_INVALID` / `CRITERIA_INVALID`) — all three are
138
+ * properties of the explicit tool surface, whose input the caller supplies
139
+ * directly, not of the gate chain; the J1/J3 shadow observers run the same
140
+ * chain for any agent and can never produce them.
99
141
  */
100
- async function readHostSkills(runtime: V2JevDecisionRuntime): Promise<JevHostSkill[] | undefined> {
101
- if (typeof runtime.listSkills !== 'function') return undefined
102
- let raw: unknown
103
- try {
104
- raw = await runtime.listSkills()
105
- } catch {
106
- return undefined
107
- }
108
- if (!Array.isArray(raw)) return undefined
109
- const skills: JevHostSkill[] = []
110
- for (const entry of raw) {
111
- if (entry === null || typeof entry !== 'object' || Array.isArray(entry)) continue
112
- const record = entry as { id?: unknown; description?: unknown }
113
- if (typeof record.id !== 'string' || record.id.length === 0) continue
114
- skills.push({
115
- id: record.id,
116
- ...(typeof record.description === 'string' && record.description.length > 0
117
- ? { description: record.description }
118
- : {}),
119
- })
120
- }
121
- return skills
142
+ export type GatedNativeJevFailure = Exclude<
143
+ JevDecisionRunFailure,
144
+ 'CALLER_NOT_ZEUS' | 'REVIEW_INPUT_INVALID' | 'CRITERIA_INVALID'
145
+ >
146
+
147
+ /** The minimal runtime slice the gate chain needs. */
148
+ export type GatedNativeJevRuntime = Pick<V2JevDecisionRuntime, 'decide'> & {
149
+ /** Host permission kill-switch (`ctx.permission.assert`), when present. */
150
+ readonly permissionAssert?: (input: Record<string, unknown>) => Promise<unknown>
122
151
  }
123
152
 
124
- async function readCatalog(runtime: V2JevDecisionRuntime): Promise<JevDecisionCatalogEntry[]> {
125
- const raw = await runtime.listModels()
126
- if (!Array.isArray(raw)) throw new TypeError('Model catalog is not a list')
127
- const entries: JevDecisionCatalogEntry[] = []
128
- for (const model of raw) {
129
- if (model === null || typeof model !== 'object' || Array.isArray(model)) continue
130
- const record = model as Record<string, unknown>
131
- if (!isNonBlankString(record.providerID) || !isNonBlankString(record.id)) continue
132
- if (typeof record.type !== 'string') continue
133
- entries.push({
134
- providerID: record.providerID,
135
- id: record.id,
136
- type: record.type,
137
- } as JevDecisionCatalogEntry)
138
- }
139
- return entries
153
+ /** One completed, settled native Jev call: gates passed, usage reconciled. */
154
+ export interface GatedNativeJevCall {
155
+ /** The route the config + ledger gates selected. */
156
+ readonly selection: NativeJevRouteSelection
157
+ /** The prepared, bounded request this call ran. */
158
+ readonly request: NativeDecisionRequest
159
+ /** The raw host response; consume it through `consumeJevDecisionResult`. */
160
+ readonly response: unknown
161
+ /** The frozen ledger reconciliation state. */
162
+ readonly frozen: boolean
140
163
  }
141
164
 
165
+ export type GatedNativeJevCallResult =
166
+ | { readonly ok: true; readonly call: GatedNativeJevCall }
167
+ | {
168
+ readonly ok: false
169
+ readonly reasonCode: GatedNativeJevFailure
170
+ /** Sanitized error CLASS only (`TypeError`, …); set for call failures. */
171
+ readonly detail?: string
172
+ }
173
+
142
174
  /**
143
- * Run one explicit, gate-checked native Jev decision. Every failure path
144
- * returns a bounded refusal JSON; the only caller-visible success output is
145
- * the validated advice with `effect: 'none'`.
175
+ * Run ONE native Jev decision through the full gate chain — the exact chain
176
+ * `runJevDecision` has always run, extracted so a second caller (the J1
177
+ * risk-score shadow gate) reuses it instead of duplicating it:
178
+ * user-level config (`enabled`, `egress_consent`) → route via
179
+ * `resolveNativeJevRoute` with the ledger's admission check → host permission
180
+ * kill-switch on `decision.egress` → caller-supplied request preparation →
181
+ * reservation → the single host call → usage verification and reconciliation.
182
+ *
183
+ * The request preparer is injected because the two callers prepare differently
184
+ * (the tool from model input, J1 from a prebuilt bounded request); everything
185
+ * before and after it is shared. Settlement keeps the fetch runner's
186
+ * fail-closed contract: reserve before the call, reconcile from the
187
+ * host-reported cost after it, leave the reservation pending when no evidence
188
+ * can be minted.
189
+ *
190
+ * Rejects only when the budget ledger itself cannot be opened (the explicit
191
+ * tool surface propagates that as a tool error, unchanged); every gate failure
192
+ * is a structured `{ ok: false }` result, never a throw.
146
193
  */
147
- export async function runJevDecision(
148
- input: unknown,
149
- toolContext: unknown,
150
- runtime: V2JevDecisionRuntime,
151
- ): Promise<string> {
152
- const context =
153
- toolContext !== null && typeof toolContext === 'object' && !Array.isArray(toolContext)
154
- ? (toolContext as JevToolContext)
155
- : undefined
156
- if (context?.agent !== 'zeus') return refusal('CALLER_NOT_ZEUS')
157
- const sessionID = context?.sessionID
158
- const messageID = context?.messageID
159
- if (!isNonBlankString(sessionID) || !isNonBlankString(messageID)) {
160
- return refusal('CONTEXT_UNAVAILABLE')
194
+ export async function runGatedNativeJevCall(params: {
195
+ readonly sessionID: unknown
196
+ readonly messageID: unknown
197
+ /** Current agent, only when the caller actually knows it. */
198
+ readonly agent?: unknown
199
+ readonly runtime: GatedNativeJevRuntime
200
+ readonly prepare: (
201
+ route: ResolvedNativeDecisionRoute,
202
+ ) => JevDecisionPreparation | Promise<JevDecisionPreparation>
203
+ }): Promise<GatedNativeJevCallResult> {
204
+ if (!isNonBlankString(params.sessionID) || !isNonBlankString(params.messageID)) {
205
+ return { ok: false, reasonCode: 'CONTEXT_UNAVAILABLE' }
161
206
  }
207
+ const sessionID = params.sessionID
208
+ const messageID = params.messageID
209
+ const agent =
210
+ typeof params.agent === 'string' && params.agent.trim().length > 0 ? params.agent : undefined
162
211
 
163
212
  let config: ReturnType<typeof readUserDecisionRouterConfig>
164
213
  try {
165
214
  config = readUserDecisionRouterConfig()
166
215
  } catch {
167
- return refusal('CONFIG_UNAVAILABLE')
168
- }
169
- if (!config.enabled) return refusal('DISABLED')
170
- if (!config.egress_consent) return refusal('CONSENT_MISSING')
171
-
172
- let catalog: JevDecisionCatalogEntry[]
173
- try {
174
- catalog = await readCatalog(runtime)
175
- } catch {
176
- return refusal('CATALOG_UNAVAILABLE')
216
+ return { ok: false, reasonCode: 'CONFIG_UNAVAILABLE' }
177
217
  }
218
+ if (!config.enabled) return { ok: false, reasonCode: 'DISABLED' }
219
+ if (!config.egress_consent) return { ok: false, reasonCode: 'CONSENT_MISSING' }
178
220
 
179
221
  const ledger = openLocalDecisionBudgetLedger({ dbPath: resolveLocalDecisionBudgetDbPath() })
180
222
  try {
181
223
  const selection = resolveNativeJevRoute(
182
224
  config,
183
- catalog,
184
225
  (provider, model) => ledger.canReserve(provider, model).status === 'eligible',
185
226
  )
186
- if (!selection.ok) return refusal(selection.reasonCode)
227
+ if (!selection.ok) return { ok: false, reasonCode: selection.reasonCode }
187
228
 
188
- if (typeof runtime.permissionAssert !== 'function') return refusal('PERMISSION_UNAVAILABLE')
229
+ if (typeof params.runtime.permissionAssert !== 'function') {
230
+ return { ok: false, reasonCode: 'PERMISSION_UNAVAILABLE' }
231
+ }
189
232
  const assertion = await assertDecisionEgressPermission(
190
- runtime.permissionAssert,
233
+ params.runtime.permissionAssert,
191
234
  buildDecisionEgressRequest({
192
235
  sessionID,
193
236
  messageID,
@@ -195,36 +238,29 @@ export async function runJevDecision(
195
238
  selection.selection.route.model.providerID,
196
239
  selection.selection.route.model.id,
197
240
  ),
198
- agent: 'zeus',
241
+ ...(agent === undefined ? {} : { agent }),
199
242
  }),
200
243
  )
201
- if (assertion === 'deny') return refusal('PERMISSION_DENIED')
244
+ if (assertion === 'deny') return { ok: false, reasonCode: 'PERMISSION_DENIED' }
202
245
 
203
- const requested = (input ?? {}) as { mode?: unknown; questions?: unknown }
204
- const prepared =
205
- requested.mode === undefined
206
- ? prepareJevDecisionRequest(selection.selection.route, input ?? {})
207
- : isJevDecisionPreset(requested.mode)
208
- ? prepareJevPresetRequest(
209
- selection.selection.route,
210
- requested.mode,
211
- input ?? {},
212
- requested.mode === 'skills' ? await readHostSkills(runtime) : undefined,
213
- )
214
- : { ok: false as const, reasonCode: 'PRESET_INVALID' as const }
215
- if (!prepared.ok) return refusal(prepared.reasonCode)
246
+ const prepared = await params.prepare(selection.selection.route)
247
+ if (!prepared.ok) return { ok: false, reasonCode: prepared.reasonCode }
216
248
 
217
- const reservation = ledger.reserve(
249
+ const reservation: ReservationResult = ledger.reserve(
218
250
  selection.selection.accountingProvider,
219
251
  selection.selection.model,
220
252
  )
221
- if (reservation.status !== 'reserved') return refusal('RESERVATION_DENIED')
253
+ if (reservation.status !== 'reserved') return { ok: false, reasonCode: 'RESERVATION_DENIED' }
222
254
 
223
255
  let response: unknown
224
256
  try {
225
- response = await runtime.decide(prepared.request)
226
- } catch {
227
- return refusal('DECISION_CALL_FAILED')
257
+ response = await params.runtime.decide(prepared.request)
258
+ } catch (err) {
259
+ return {
260
+ ok: false,
261
+ reasonCode: 'DECISION_CALL_FAILED',
262
+ detail: err instanceof Error ? err.name : 'UnknownFailure',
263
+ }
228
264
  }
229
265
 
230
266
  const evidence = verifyNativeUsageForReservation(
@@ -236,40 +272,220 @@ export async function runJevDecision(
236
272
  },
237
273
  response,
238
274
  )
239
- if (evidence === null) return refusal('USAGE_UNRECONCILED')
275
+ if (evidence === null) return { ok: false, reasonCode: 'USAGE_UNRECONCILED' }
240
276
  const reconciliation = ledger.reconcileVerified(reservation.reservationId, evidence)
241
- if (reconciliation.status !== 'reconciled') return refusal('USAGE_UNRECONCILED')
277
+ if (reconciliation.status !== 'reconciled') {
278
+ return { ok: false, reasonCode: 'USAGE_UNRECONCILED' }
279
+ }
242
280
 
243
- const advice = consumeJevDecisionResult(response, prepared.request)
281
+ return {
282
+ ok: true,
283
+ call: {
284
+ selection: selection.selection,
285
+ request: prepared.request,
286
+ response,
287
+ frozen: reconciliation.frozen,
288
+ },
289
+ }
290
+ } finally {
291
+ ledger.close()
292
+ }
293
+ }
294
+
295
+ /**
296
+ * Read the host's installed skills for the stacking preset. Anything that is
297
+ * not a plain {id, description?} record is skipped; a missing or hostile
298
+ * registry degrades to the catalog-only preset. Reading the registry applies
299
+ * no trust: installation already was the user's manual, reviewed act.
300
+ */
301
+ async function readHostSkills(runtime: V2JevDecisionRuntime): Promise<JevHostSkill[] | undefined> {
302
+ if (typeof runtime.listSkills !== 'function') return undefined
303
+ let raw: unknown
304
+ try {
305
+ raw = await runtime.listSkills()
306
+ } catch {
307
+ return undefined
308
+ }
309
+ if (!Array.isArray(raw)) return undefined
310
+ const skills: JevHostSkill[] = []
311
+ for (const entry of raw) {
312
+ if (entry === null || typeof entry !== 'object' || Array.isArray(entry)) continue
313
+ const record = entry as { id?: unknown; description?: unknown }
314
+ if (typeof record.id !== 'string' || record.id.length === 0) continue
315
+ skills.push({
316
+ id: record.id,
317
+ ...(typeof record.description === 'string' && record.description.length > 0
318
+ ? { description: record.description }
319
+ : {}),
320
+ })
321
+ }
322
+ return skills
323
+ }
324
+
325
+ /**
326
+ * Run one explicit, gate-checked native Jev decision. Every failure path
327
+ * returns a bounded refusal JSON; the only caller-visible success output is
328
+ * the validated advice with `effect: 'none'`.
329
+ *
330
+ * The `mode: "review"` branch (J4 output-quality scoring, advisory only)
331
+ * validates and builds its bounded request BEFORE the gate chain — the same
332
+ * prebuilt-request pattern the J1/J3 observers use — so a malformed review
333
+ * never spends gate work, and its failure codes stay tool-surface properties.
334
+ * It records ONE J4 telemetry record per run (q-token on an answered review,
335
+ * skip tokens on every refusal path) and returns the evaluation plus the
336
+ * verdict flag; the verdict never blocks, retries, or alters anything.
337
+ */
338
+ export async function runJevDecision(
339
+ input: unknown,
340
+ toolContext: unknown,
341
+ runtime: V2JevDecisionRuntime,
342
+ ): Promise<string> {
343
+ const context =
344
+ toolContext !== null && typeof toolContext === 'object' && !Array.isArray(toolContext)
345
+ ? (toolContext as JevToolContext)
346
+ : undefined
347
+ if (context?.agent !== 'zeus') return refusal('CALLER_NOT_ZEUS')
348
+
349
+ const requested = (input ?? {}) as { mode?: unknown; questions?: unknown }
350
+ // Review mode (J4): the bounded request is built before any gate work, and
351
+ // an invalid review input is a tool-surface refusal — same class as the
352
+ // caller check, never a gate failure.
353
+ let reviewRequest: Omit<NativeDecisionRequest, 'model'> | undefined
354
+ if (requested.mode === 'review') {
355
+ const prepared = buildJevReviewRequest(input ?? {})
356
+ if (!prepared.ok) {
357
+ observeShadow(
358
+ J4_QUALITY_SCORE_POINT,
359
+ prepared.reasonCode,
360
+ () => ({
361
+ decision: 'unavailable',
362
+ reason: reviewRefusalSkipReason(prepared.reasonCode),
363
+ }),
364
+ runtime.shadowSink,
365
+ )
366
+ return refusal(prepared.reasonCode)
367
+ }
368
+ reviewRequest = prepared.request
369
+ }
370
+ const result = await runGatedNativeJevCall({
371
+ sessionID: context?.sessionID,
372
+ messageID: context?.messageID,
373
+ agent: 'zeus',
374
+ runtime,
375
+ prepare: async (route) =>
376
+ reviewRequest !== undefined
377
+ ? {
378
+ ok: true as const,
379
+ request: {
380
+ model: route.model,
381
+ input: reviewRequest.input,
382
+ questions: reviewRequest.questions,
383
+ },
384
+ }
385
+ : requested.mode === undefined
386
+ ? prepareJevDecisionRequest(route, input ?? {})
387
+ : isJevDecisionPreset(requested.mode)
388
+ ? prepareJevPresetRequest(
389
+ route,
390
+ requested.mode,
391
+ input ?? {},
392
+ requested.mode === 'skills' ? await readHostSkills(runtime) : undefined,
393
+ )
394
+ : { ok: false as const, reasonCode: 'PRESET_INVALID' as const },
395
+ })
396
+ if (!result.ok) {
397
+ if (requested.mode === 'review') {
398
+ // The review observed a gate refusal: one skip record, the refusal
399
+ // itself is unchanged.
400
+ observeShadow(
401
+ J4_QUALITY_SCORE_POINT,
402
+ result.reasonCode,
403
+ () => ({
404
+ decision: 'unavailable',
405
+ reason: reviewRefusalSkipReason(result.reasonCode),
406
+ }),
407
+ runtime.shadowSink,
408
+ )
409
+ }
410
+ if (result.detail === undefined) return refusal(result.reasonCode)
411
+ // Refusal with the error CLASS only (`TypeError`, `RangeError`, …): the
412
+ // message can carry paths, config or provider detail and never crosses
413
+ // into the chat.
414
+ return JSON.stringify({
415
+ advisory: false,
416
+ effect: 'none',
417
+ reasonCode: result.reasonCode,
418
+ detail: result.detail,
419
+ })
420
+ }
421
+
422
+ const advice = consumeJevDecisionResult(result.call.response, result.call.request)
423
+ if (requested.mode === 'review') {
424
+ // J4 advisory evaluation: strict validation, the verdict is telemetry
425
+ // plus a returned flag — it never blocks, retries, or alters the result.
426
+ const evaluation: QualityScoreEvaluation = evaluateQualityScoreDecision(
427
+ result.call.response,
428
+ result.call.request,
429
+ )
430
+ observeShadow(
431
+ J4_QUALITY_SCORE_POINT,
432
+ evaluation,
433
+ (isolated) => {
434
+ const scored = isolated as QualityScoreEvaluation
435
+ return {
436
+ decision: qualityVerdict(scored),
437
+ reason: encodeQualityScoreReason(scored),
438
+ }
439
+ },
440
+ runtime.shadowSink,
441
+ )
244
442
  return JSON.stringify({
245
443
  advisory: true,
246
444
  effect: 'none',
247
445
  reason: 'JEV_DECISION',
248
446
  route: {
249
- providerID: selection.selection.route.model.providerID,
250
- id: selection.selection.route.model.id,
251
- billing: selection.selection.route.billing,
447
+ providerID: result.call.selection.route.model.providerID,
448
+ id: result.call.selection.route.model.id,
449
+ billing: result.call.selection.route.billing,
252
450
  },
253
- ...(requested.mode === undefined ? {} : { preset: requested.mode }),
451
+ mode: 'review',
452
+ quality: evaluation.quality,
453
+ needsReview: evaluation.needsReview,
454
+ verdict: qualityVerdict(evaluation),
254
455
  answers: advice.answers,
255
456
  missing: advice.missing,
256
457
  usage: { costUsd: advice.costUsd, tokens: advice.tokens },
257
- frozen: reconciliation.frozen,
458
+ frozen: result.call.frozen,
258
459
  })
259
- } finally {
260
- ledger.close()
261
460
  }
461
+ return JSON.stringify({
462
+ advisory: true,
463
+ effect: 'none',
464
+ reason: 'JEV_DECISION',
465
+ route: {
466
+ providerID: result.call.selection.route.model.providerID,
467
+ id: result.call.selection.route.model.id,
468
+ billing: result.call.selection.route.billing,
469
+ },
470
+ ...(requested.mode === undefined ? {} : { preset: requested.mode }),
471
+ answers: advice.answers,
472
+ missing: advice.missing,
473
+ usage: { costUsd: advice.costUsd, tokens: advice.tokens },
474
+ frozen: result.call.frozen,
475
+ })
262
476
  }
263
477
 
264
478
  const TOOL_DESCRIPTION =
265
479
  'Zeus-only advisory typed decisions via the host native Jev route (OpenCode Console or a ' +
266
- 'configured decision model). Two ways to ask: pass `mode: "skills"` to stack skills for the ' +
480
+ 'configured decision model). Ways to ask: pass `mode: "skills"` to stack skills for the ' +
267
481
  'state text (local prefilter shortlist, catalog fallback, plus every skill installed in the ' +
268
482
  'host), `mode: "delegation"` to pick ' +
269
- 'a specialist, or pass explicit typed `questions` (choice, score, predicate). Returns ' +
270
- 'probabilistic answers with confidence. Requires decision-router egress consent and budget ' +
271
- 'caps; asks host permission first. It never dispatches agents, runs skills, executes tools, ' +
272
- 'or alters permissions — results are advisory only.'
483
+ 'a specialist, `mode: "review"` to score a finished delegated result against named criteria ' +
484
+ '(advisory quality score + needs-review flag; the result content itself is never sent — ' +
485
+ 'shape metrics and criteria only), or pass explicit typed `questions` (choice, score, ' +
486
+ 'predicate). Returns probabilistic answers with confidence. Requires decision-router egress ' +
487
+ 'consent and budget caps; asks host permission first. It never dispatches agents, runs ' +
488
+ 'skills, executes tools, blocks, retries, or alters permissions — results are advisory only.'
273
489
 
274
490
  const questionSchema = {
275
491
  type: 'object',
@@ -310,15 +526,51 @@ const questionSchema = {
310
526
  },
311
527
  }
312
528
 
529
+ const REVIEW_METRICS_SCHEMA = {
530
+ type: 'object',
531
+ additionalProperties: false,
532
+ required: ['outputChars', 'taskDurationMs'],
533
+ properties: {
534
+ outputChars: { type: 'integer', minimum: 0, maximum: 100_000_000 },
535
+ taskDurationMs: { type: ['integer', 'null'], minimum: 0, maximum: 100_000_000 },
536
+ },
537
+ } as const
538
+
539
+ /**
540
+ * One schema, two disjoint payloads: the state branch (presets and explicit
541
+ * questions carry the state text) and the review branch (J4 takes structural
542
+ * shape metrics, a delegate id and named criteria — never a state text, never
543
+ * result content; the criteria are the only free text, bounded at the DSH
544
+ * caps: at most 4, each 1–120 chars). The preparers are the authority; the
545
+ * schema is the model-facing contract.
546
+ */
313
547
  const JEV_INPUT_SCHEMA: Record<string, unknown> = {
314
548
  type: 'object',
315
549
  additionalProperties: false,
316
- required: ['schemaVersion', 'state'],
550
+ required: ['schemaVersion'],
551
+ anyOf: [
552
+ { required: ['state'] },
553
+ { required: ['delegateId', 'resultSummaryKind', 'resultMetrics', 'criteria'] },
554
+ ],
317
555
  properties: {
318
556
  schemaVersion: { type: 'integer', const: 1 },
319
557
  state: { type: 'string', minLength: 1, maxLength: 12_000 },
320
- mode: { type: 'string', enum: ['skills', 'delegation'] },
558
+ mode: { type: 'string', enum: ['skills', 'delegation', 'review'] },
321
559
  questions: { type: 'array', minItems: 1, maxItems: 4, items: questionSchema },
560
+ delegateId: {
561
+ type: 'string',
562
+ minLength: 1,
563
+ maxLength: 64,
564
+ pattern: '^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$',
565
+ },
566
+ resultSummaryKind: { type: 'string', enum: ['ok', 'empty', 'error'] },
567
+ resultMetrics: REVIEW_METRICS_SCHEMA,
568
+ criteria: {
569
+ type: 'array',
570
+ minItems: 1,
571
+ maxItems: 4,
572
+ items: { type: 'string', minLength: 1, maxLength: 120 },
573
+ },
322
574
  },
323
575
  }
324
576
 
@@ -25,15 +25,12 @@ import {
25
25
  import { selectPantheonSkills } from './local-decision-skills.ts'
26
26
  import { PANTHEON_AGENT_CATALOG, PANTHEON_SKILL_CATALOG } from './pantheon-decision-catalog.ts'
27
27
  import {
28
+ findRouteKind,
28
29
  NATIVE_DECISION_MAX_PROMPT_LENGTH,
29
30
  type NativeDecisionRequest,
30
31
  type ResolvedNativeDecisionRoute,
31
- resolveNativeDecisionRoute,
32
32
  } from './v2-native-decisions.ts'
33
33
 
34
- /** Catalog entry shape the route resolver needs from the host model list. */
35
- export type JevDecisionCatalogEntry = Parameters<typeof resolveNativeDecisionRoute>[2][number]
36
-
37
34
  /** Config-level providers that have a native host Decisions route. */
38
35
  export type NativeJevProviderId = Extract<
39
36
  DecisionProviderId,
@@ -56,7 +53,7 @@ export function isNativeJevProvider(provider: unknown): provider is NativeJevPro
56
53
  return typeof provider === 'string' && Object.hasOwn(NATIVE_JEV_HOST_PROVIDERS, provider)
57
54
  }
58
55
 
59
- export type NativeJevRouteFailure = 'NO_ELIGIBLE_CANDIDATE' | 'ROUTE_UNRESOLVED'
56
+ export type NativeJevRouteFailure = 'NO_ELIGIBLE_CANDIDATE'
60
57
 
61
58
  export interface NativeJevRouteSelection {
62
59
  readonly route: ResolvedNativeDecisionRoute
@@ -114,7 +111,6 @@ function readEnabledCandidates(config: unknown): UserDecisionRouterCandidate[] {
114
111
  */
115
112
  export function resolveNativeJevRoute(
116
113
  config: unknown,
117
- catalog: readonly JevDecisionCatalogEntry[],
118
114
  checkAdmission: (provider: BudgetProvider, model: string) => boolean,
119
115
  ): NativeJevRouteResolution {
120
116
  const record =
@@ -153,16 +149,18 @@ export function resolveNativeJevRoute(
153
149
  }
154
150
 
155
151
  const hostProvider = NATIVE_JEV_HOST_PROVIDERS[selected.provider]
156
- const resolution = resolveNativeDecisionRoute(
157
- { providerID: hostProvider, id: selected.model },
158
- undefined,
159
- catalog,
160
- )
161
- if (!resolution.ok) return { ok: false, reasonCode: 'ROUTE_UNRESOLVED' }
152
+ // NO catalog-presence gate. Field evidence (2.2.5, 2026-11-03): decision
153
+ // models are not chat models — `model.list()` never exposes them (561
154
+ // entries, 0 jev) while the decisions API answers the exact same ref
155
+ // directly (jev-1.13-free → 200; jev-1.13 → 404 from the host itself).
156
+ // The host is the authority on model existence: an unknown model surfaces
157
+ // later as DECISION_CALL_FAILED. Consent, budget caps and the host
158
+ // permission assertion remain the actual safety gates.
159
+ const routeModel = { providerID: hostProvider, id: selected.model }
162
160
  return {
163
161
  ok: true,
164
162
  selection: {
165
- route: resolution.route,
163
+ route: Object.freeze({ model: routeModel, ...findRouteKind(routeModel) }),
166
164
  provider: selected.provider,
167
165
  accountingProvider: DECISION_PROVIDER_ADAPTERS[selected.provider].accountingProvider,
168
166
  model: selected.model,