pantheon-opencode 1.6.3-beta.2 → 1.6.3-beta.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/AGENTS.global.md +1 -1
  2. package/AGENTS.md +1 -1
  3. package/CHANGELOG.md +137 -0
  4. package/README.md +21 -3
  5. package/README.pt-BR.md +1 -1
  6. package/bin/pantheon-init.mjs +46 -28
  7. package/package.json +1 -1
  8. package/plugin.json +2 -2
  9. package/pyproject.toml +1 -1
  10. package/scripts/decision-router-budget.mjs +103 -3
  11. package/scripts/decision-router-v2-budget.mjs +2 -2
  12. package/scripts/decision-router.mjs +88 -6
  13. package/scripts/install/cli-ui.mjs +2 -1
  14. package/scripts/install/decision-router-config.mjs +53 -0
  15. package/scripts/install/interactive.mjs +103 -9
  16. package/scripts/install/opencode.mjs +101 -30
  17. package/scripts/install/plugin.mjs +17 -0
  18. package/scripts/jev-live-probe.mjs +210 -0
  19. package/scripts/skill-search.mjs +273 -0
  20. package/scripts/sync-tui.mjs +11 -0
  21. package/skills-lock.json +1 -0
  22. package/src/agents/aphrodite.md +34 -1
  23. package/src/agents/apollo.md +227 -0
  24. package/src/agents/athena.md +26 -0
  25. package/src/agents/demeter.md +33 -0
  26. package/src/agents/gaia.md +24 -0
  27. package/src/agents/hephaestus.md +24 -0
  28. package/src/agents/hermes.md +33 -0
  29. package/src/agents/iris.md +24 -0
  30. package/src/agents/mnemosyne.md +25 -3
  31. package/src/agents/nyx.md +50 -22
  32. package/src/agents/prometheus.md +242 -1
  33. package/src/agents/talos.md +24 -0
  34. package/src/agents/themis.md +24 -0
  35. package/src/agents/zeus.md +67 -6
  36. package/src/pantheon/j-telemetry-file.ts +96 -0
  37. package/src/pantheon/jev-decision-tool.ts +262 -34
  38. package/src/pantheon/jev-decisions.ts +92 -8
  39. package/src/pantheon/jev-quality-score.ts +7 -2
  40. package/src/pantheon/jev-rank-verify.ts +637 -0
  41. package/src/pantheon/jev-risk-gate.ts +70 -6
  42. package/src/pantheon/local-decision-budget.ts +170 -3
  43. package/src/pantheon/local-decision-router.ts +16 -0
  44. package/src/pantheon/local-decision-turn.ts +39 -4
  45. package/src/pantheon/local-decision-usage-evidence.internal.ts +2 -2
  46. package/src/pantheon/pantheon-decision-catalog.ts +9 -2
  47. package/src/pantheon/v2-decision-budget.ts +4 -2
  48. package/src/plugin-v2.ts +44 -7
  49. package/src/plugins/tui/dist/server-v2.d.ts +14 -6
  50. package/src/plugins/tui/dist/server-v2.d.ts.map +1 -1
  51. package/src/plugins/tui/dist/server-v2.js +16 -9
  52. package/src/plugins/tui/dist/server-v2.js.map +1 -1
  53. package/src/plugins/tui/dist/tui-v2.d.ts.map +1 -1
  54. package/src/plugins/tui/dist/tui-v2.js +560 -4
  55. package/src/plugins/tui/dist/tui-v2.js.map +1 -1
  56. package/src/plugins/tui/dist/tui.js +1 -1
  57. package/src/plugins/tui/package-lock.json +2 -2
  58. package/src/plugins/tui/package.json +1 -1
  59. package/src/plugins/tui/src/index-v2.tsx +6 -0
  60. package/src/plugins/tui/src/server-v2.ts +14 -7
  61. package/src/plugins/tui/src/whats-new-highlights.ts +317 -0
  62. package/src/plugins/tui/src/whats-new-open.ts +120 -0
  63. package/src/plugins/tui/src/whats-new-state.ts +128 -0
  64. package/src/plugins/tui/src/whats-new.tsx +172 -0
  65. package/src/routing.yml +1 -0
  66. package/src/skills/README.md +1 -1
  67. package/src/skills/prompt-refinery/SKILL.md +41 -0
@@ -0,0 +1,637 @@
1
+ /**
2
+ * The v3.1 rank and verify primitives on the native Jev route (tool modes on
3
+ * `pantheon_jev_decide`, built after the J4 review-mode pattern).
4
+ *
5
+ * ## Rank — `mode: "rank"`, one choice + one score, `{ winner, winnerFit }`
6
+ *
7
+ * Zeus can rank 2–8 named candidates for a state in ONE typed round-trip: one
8
+ * `choice` question (`winner`) over the candidate ids picks the best candidate
9
+ * and one `score` question (`fit`, the 2..10 rubric bound) rates that
10
+ * candidate's fit. THE HONEST LIMITATION: the host Decisions API returns one
11
+ * scalar answer per question (`NativeDecisionRequest` is tied to the 2.2.5
12
+ * host types), so per-candidate scores are not expressible in a single
13
+ * round-trip — one score question per candidate would break the 4-question
14
+ * bound above four candidates, and one ambiguous "score each candidate"
15
+ * question has no per-candidate answer structure to pair back. Rank therefore
16
+ * returns `{ winner, winnerFit }` and NOTHING more: no runner-up, no full
17
+ * ordering. A caller that needs a deeper order uses the explicit-questions
18
+ * mode with its own typed questions.
19
+ *
20
+ * ## Verify — `mode: "verify"`, one predicate per claim, fail-closed BLOCK
21
+ *
22
+ * Zeus can check 1–4 named claims in ONE typed round-trip: one `predicate`
23
+ * question per claim (within the 4-question bound), the claim text riding that
24
+ * question's instructions. The consumer maps each answer: probability
25
+ * ≥ {@link VERIFY_PASS_THRESHOLD} → `PASS`, below → `FAIL`, and every refusal,
26
+ * timeout, duplicate or malformed answer → `BLOCK` (fail-closed: an
27
+ * unanswered mandatory claim must never read as verified). The tool itself
28
+ * never acts on a verdict — `effect` stays `none` — and a gate-chain refusal
29
+ * stays the standard bounded refusal JSON; a caller that REQUIRES a verdict
30
+ * treats every refusal as `BLOCK` at its own call site.
31
+ *
32
+ * ## Sanitization contract (the J4 criteria class)
33
+ *
34
+ * Both modes take bounded caller-provided fields only: rank's candidate ids
35
+ * are strict tokens (they become choice values), its state rides the same
36
+ * 12,000-char bound and verbatim-carriage rule as the presets, and verify's
37
+ * claim texts are the only free text, riding the predicate instructions. The
38
+ * hostility scan mirrors the J4 criteria scan (paths, credential assignments,
39
+ * key material, control characters, over-length): hostile shapes are REJECTED
40
+ * — `CANDIDATE_INVALID` / `CLAIM_INVALID` — never sanitized on the way in.
41
+ * Neither mode has a telemetry point (they are tool modes, not shadow gates)
42
+ * and both reuse the tool's strict answer consumer
43
+ * (`consumeJevDecisionResult`): one answer per question name, duplicates
44
+ * become errors, unbounded host metadata never crosses.
45
+ *
46
+ * @module jev-rank-verify
47
+ */
48
+
49
+ import { consumeJevDecisionResult, JEV_DECISION_MAX_QUESTIONS } from './jev-decisions.ts'
50
+ import type { NativeDecisionRequest, ResolvedNativeDecisionRoute } from './v2-native-decisions.ts'
51
+ import { NATIVE_DECISION_MAX_PROMPT_LENGTH } from './v2-native-decisions.ts'
52
+
53
+ // ─── Bounds ──────────────────────────────────────────────────────────────
54
+
55
+ /** A ranking needs at least two candidates to choose between. */
56
+ export const RANK_MIN_CANDIDATES = 2
57
+ /** Mirrors the typesafe_rank reference contract (1-20 accepted upstream). */
58
+ export const RANK_MAX_CANDIDATES = 20
59
+
60
+ /** Question name of the rank's choice question. */
61
+ export const RANK_WINNER_QUESTION_NAME = 'winner'
62
+ /** Question name of the rank's fit score question. */
63
+ export const RANK_FIT_QUESTION_NAME = 'fit'
64
+
65
+ /** The fit rubric mirrors the documented 2..10 score-question bound. */
66
+ export const RANK_FIT_LEVEL_COUNT = 9
67
+
68
+ /** At least one claim per verify call; one predicate question per claim. */
69
+ export const VERIFY_MIN_CLAIMS = 1
70
+ /** One predicate question per claim, within the 4-question bound. */
71
+ export const VERIFY_MAX_CLAIMS = 4
72
+ /** Each claim is a bounded 1–200-char instruction string. */
73
+ export const CLAIM_MAX_LENGTH = 200
74
+
75
+ /** Predicate probability at or above which a claim counts as PASS. */
76
+ export const VERIFY_PASS_THRESHOLD = 0.5
77
+
78
+ const CANDIDATE_ID_MAX_LENGTH = 64
79
+ const CLAIM_NAME_MAX_LENGTH = 64
80
+ const DESCRIPTION_MAX_LENGTH = 500
81
+
82
+ /** Candidate ids become choice values: strict bounded tokens only. */
83
+ const TOKEN_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/
84
+
85
+ const RANK_FIT_LABELS: readonly string[] = Object.freeze([
86
+ 'unusable',
87
+ 'poor',
88
+ 'weak',
89
+ 'fair',
90
+ 'adequate',
91
+ 'good',
92
+ 'strong',
93
+ 'excellent',
94
+ 'exemplary',
95
+ ])
96
+
97
+ const WINNER_INSTRUCTIONS =
98
+ 'Choose the single best candidate from the listed options for the described state. ' +
99
+ 'Answer with exactly one candidate id. Advisory only; nothing is dispatched or executed.'
100
+
101
+ const FIT_INSTRUCTIONS =
102
+ 'Rate how well the candidate you chose as the best fits the described state. ' +
103
+ 'Higher means a better fit; judge from the state and the candidate descriptions alone. ' +
104
+ 'Advisory only; nothing is enforced.'
105
+
106
+ /**
107
+ * The verify request's only input part: a fixed template with NO free-text
108
+ * slot — the claims ride the predicate questions' instructions, so the state
109
+ * part cannot carry them twice (or be mistaken for a content field).
110
+ */
111
+ const VERIFY_STATE_TEMPLATE =
112
+ 'Advisory claim verification. Each claim is stated in its own question below; ' +
113
+ 'assess each claim on its own terms from the claim text alone. Nothing is enforced.'
114
+
115
+ // ─── Hostility scan (mirrors the J4 criteria scan) ───────────────────────
116
+
117
+ /** Path separators in any position: caller prose is never a location. */
118
+ const PATH_PATTERN = /[/\\]/
119
+
120
+ /**
121
+ * Control characters (C0 and DEL — newlines and tabs included): these are
122
+ * single-line prose fields. An explicit code-point scan, not a regex, so the
123
+ * hostile-input check stays lint-clean without a rule suppression.
124
+ */
125
+ function hasControlCharacter(value: string): boolean {
126
+ for (let index = 0; index < value.length; index += 1) {
127
+ const code = value.charCodeAt(index)
128
+ if (code <= 0x1f || code === 0x7f) return true
129
+ }
130
+ return false
131
+ }
132
+
133
+ /** Key material shapes: PEM headers, AWS/SDK/Git token literals, key files. */
134
+ const KEY_MATERIAL_PATTERNS: readonly RegExp[] = Object.freeze([
135
+ /-----BEGIN/,
136
+ /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/,
137
+ /\bsk-[A-Za-z0-9_-]{16,}\b/,
138
+ /\bgh[pousr]_[A-Za-z0-9]{20,}\b/,
139
+ /\b[a-z0-9._-]+\.(?:key|pem|p12|pfx|kdbx)\b/i,
140
+ ])
141
+
142
+ /**
143
+ * Credential ASSIGNMENT shapes only: naming a concept is prose ("handles
144
+ * secrets without echoing them" stays admissible); binding a value to it
145
+ * ("password=hunter2") is a credential shape and is rejected.
146
+ */
147
+ const CREDENTIAL_ASSIGNMENT_PATTERN =
148
+ /\b(?:password|passwd|secret|token|api[-_]?key|credential)s?\s*[:=]\s*\S+/i
149
+
150
+ function isHostileProse(value: string): boolean {
151
+ if (PATH_PATTERN.test(value)) return true
152
+ if (hasControlCharacter(value)) return true
153
+ if (KEY_MATERIAL_PATTERNS.some((pattern) => pattern.test(value))) return true
154
+ return CREDENTIAL_ASSIGNMENT_PATTERN.test(value)
155
+ }
156
+
157
+ function isBoundedNonBlankString(value: unknown, max: number): value is string {
158
+ return typeof value === 'string' && value.trim().length > 0 && value.length <= max
159
+ }
160
+
161
+ function isRecord(value: unknown): value is Record<string, unknown> {
162
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
163
+ }
164
+
165
+ // ─── Rank: request building ──────────────────────────────────────────────
166
+
167
+ export const RANK_ENVELOPE_KEYS: ReadonlySet<string> = new Set([
168
+ 'schemaVersion',
169
+ 'mode',
170
+ 'state',
171
+ 'candidates',
172
+ ])
173
+
174
+ export type JevRankBuildFailure = 'RANK_INPUT_INVALID' | 'CANDIDATE_INVALID' | 'STATE_INVALID'
175
+
176
+ /** Per-cause token attached to a rank build refusal (e.g. the accepted count range). */
177
+ export type JevRankBuildDetail = 'RANK_CANDIDATE_COUNT_RANGE'
178
+
179
+ export type JevRankBuild =
180
+ | { readonly ok: true; readonly request: Omit<NativeDecisionRequest, 'model'> }
181
+ | {
182
+ readonly ok: false
183
+ readonly reasonCode: JevRankBuildFailure
184
+ readonly detail?: JevRankBuildDetail
185
+ }
186
+
187
+ export type JevRankPreparation =
188
+ | { readonly ok: true; readonly request: NativeDecisionRequest }
189
+ | {
190
+ readonly ok: false
191
+ readonly reasonCode: JevRankBuildFailure
192
+ readonly detail?: JevRankBuildDetail
193
+ }
194
+
195
+ /**
196
+ * Validate the rank tool input and build the model-less request. The state is
197
+ * carried verbatim under the same bound and rule as the presets; the
198
+ * candidates ride the winner question's choice values (ids, strict tokens)
199
+ * and choice descriptions (bounded prose, hostility-scanned). Unknown
200
+ * envelope keys — `questions`, review fields, anything else — refuse the
201
+ * preparation: no second payload slot exists.
202
+ */
203
+ export function buildJevRankRequest(input: unknown): JevRankBuild {
204
+ if (!isRecord(input)) return { ok: false, reasonCode: 'RANK_INPUT_INVALID' }
205
+ for (const key of Object.keys(input)) {
206
+ if (!RANK_ENVELOPE_KEYS.has(key)) return { ok: false, reasonCode: 'RANK_INPUT_INVALID' }
207
+ }
208
+ const state = input.state
209
+ if (!isBoundedNonBlankString(state, NATIVE_DECISION_MAX_PROMPT_LENGTH)) {
210
+ return { ok: false, reasonCode: 'STATE_INVALID' }
211
+ }
212
+ const rawCandidates = input.candidates
213
+ // A MISSING candidates field is a malformed rank; a PRESENT-but-hostile
214
+ // candidates array keeps its precise CANDIDATE_INVALID (the review-mode
215
+ // discipline).
216
+ if (rawCandidates === undefined) return { ok: false, reasonCode: 'RANK_INPUT_INVALID' }
217
+ if (!Array.isArray(rawCandidates)) return { ok: false, reasonCode: 'CANDIDATE_INVALID' }
218
+ if (rawCandidates.length < RANK_MIN_CANDIDATES || rawCandidates.length > RANK_MAX_CANDIDATES) {
219
+ return { ok: false, reasonCode: 'CANDIDATE_INVALID', detail: 'RANK_CANDIDATE_COUNT_RANGE' }
220
+ }
221
+ const seen = new Set<string>()
222
+ const choices: Array<{ value: string; description?: string }> = []
223
+ for (const raw of rawCandidates) {
224
+ if (!isRecord(raw)) return { ok: false, reasonCode: 'CANDIDATE_INVALID' }
225
+ const extraKeys = Object.keys(raw).filter((key) => key !== 'id' && key !== 'description')
226
+ if (extraKeys.length > 0) return { ok: false, reasonCode: 'CANDIDATE_INVALID' }
227
+ const id = raw.id
228
+ if (
229
+ typeof id !== 'string' ||
230
+ id.length > CANDIDATE_ID_MAX_LENGTH ||
231
+ !TOKEN_PATTERN.test(id) ||
232
+ seen.has(id)
233
+ ) {
234
+ return { ok: false, reasonCode: 'CANDIDATE_INVALID' }
235
+ }
236
+ seen.add(id)
237
+ const description = raw.description
238
+ if (description === undefined) {
239
+ choices.push({ value: id })
240
+ } else if (
241
+ isBoundedNonBlankString(description, DESCRIPTION_MAX_LENGTH) &&
242
+ !isHostileProse(description)
243
+ ) {
244
+ choices.push({ value: id, description })
245
+ } else {
246
+ return { ok: false, reasonCode: 'CANDIDATE_INVALID' }
247
+ }
248
+ }
249
+ const request = {
250
+ input: Object.freeze([Object.freeze({ type: 'text' as const, text: state })]),
251
+ questions: Object.freeze([
252
+ Object.freeze({
253
+ type: 'choice' as const,
254
+ name: RANK_WINNER_QUESTION_NAME,
255
+ instructions: WINNER_INSTRUCTIONS,
256
+ choices: Object.freeze(choices.map((choice) => Object.freeze(choice))),
257
+ }),
258
+ Object.freeze({
259
+ type: 'score' as const,
260
+ name: RANK_FIT_QUESTION_NAME,
261
+ instructions: FIT_INSTRUCTIONS,
262
+ levels: Object.freeze(RANK_FIT_LABELS.map((label) => Object.freeze({ label }))),
263
+ }),
264
+ ]),
265
+ } satisfies Omit<NativeDecisionRequest, 'model'>
266
+ return { ok: true, request: Object.freeze(request) }
267
+ }
268
+
269
+ /** `prepareJevPresetRequest` contract: route plus tool input in, request out. */
270
+ export function prepareJevRankRequest(
271
+ route: ResolvedNativeDecisionRoute,
272
+ input: unknown,
273
+ ): JevRankPreparation {
274
+ const built = buildJevRankRequest(input)
275
+ if (!built.ok) return built
276
+ return {
277
+ ok: true,
278
+ request: {
279
+ model: route.model,
280
+ input: built.request.input,
281
+ questions: built.request.questions,
282
+ },
283
+ }
284
+ }
285
+
286
+ // ─── Order: top-K ordering (2-4 candidates, one fit question each) ───────
287
+
288
+ export const ORDER_ENVELOPE_KEYS: ReadonlySet<string> = new Set([
289
+ 'schemaVersion',
290
+ 'mode',
291
+ 'state',
292
+ 'candidates',
293
+ ])
294
+
295
+ export type JevOrderBuildFailure =
296
+ | 'ORDER_INPUT_INVALID'
297
+ | 'ORDER_CANDIDATE_INVALID'
298
+ | 'STATE_INVALID'
299
+
300
+ export type JevOrderBuildDetail = 'ORDER_CANDIDATE_COUNT_RANGE'
301
+
302
+ export type JevOrderBuild =
303
+ | { readonly ok: true; readonly request: Omit<NativeDecisionRequest, 'model'> }
304
+ | {
305
+ readonly ok: false
306
+ readonly reasonCode: JevOrderBuildFailure
307
+ readonly detail?: JevOrderBuildDetail
308
+ }
309
+
310
+ export type JevOrderPreparation =
311
+ | { readonly ok: true; readonly request: NativeDecisionRequest }
312
+ | {
313
+ readonly ok: false
314
+ readonly reasonCode: JevOrderBuildFailure
315
+ readonly detail?: JevOrderBuildDetail
316
+ }
317
+
318
+ const ORDER_MAX_CANDIDATES = JEV_DECISION_MAX_QUESTIONS // one score question per candidate
319
+
320
+ const ORDER_STATE_TEMPLATE =
321
+ 'Advisory ordering. The caller supplied a task and 2-4 candidates; ' +
322
+ 'each question scores ONE candidate for fit against the stated task. ' +
323
+ 'Score each candidate on its own terms. Nothing is enforced.'
324
+
325
+ /**
326
+ * Build the order request: one SCORE question per candidate (the host answers
327
+ * one scalar per question, so 2-4 candidates is the honest ceiling). Ids ride
328
+ * the candidate validation reused from rank; question names are positional
329
+ * (`fit_1`..) so no id pattern can collide with the question-name grammar.
330
+ */
331
+ export function buildJevOrderRequest(input: unknown): JevOrderBuild {
332
+ if (!isRecord(input)) return { ok: false, reasonCode: 'ORDER_INPUT_INVALID' }
333
+ for (const key of Object.keys(input)) {
334
+ if (!ORDER_ENVELOPE_KEYS.has(key)) return { ok: false, reasonCode: 'ORDER_INPUT_INVALID' }
335
+ }
336
+ const state = input.state
337
+ if (!isBoundedNonBlankString(state, NATIVE_DECISION_MAX_PROMPT_LENGTH) || isHostileProse(state)) {
338
+ return { ok: false, reasonCode: 'STATE_INVALID' }
339
+ }
340
+ const rawCandidates = input.candidates
341
+ if (rawCandidates === undefined) return { ok: false, reasonCode: 'ORDER_INPUT_INVALID' }
342
+ if (!Array.isArray(rawCandidates)) return { ok: false, reasonCode: 'ORDER_CANDIDATE_INVALID' }
343
+ if (rawCandidates.length < 2 || rawCandidates.length > ORDER_MAX_CANDIDATES) {
344
+ return {
345
+ ok: false,
346
+ reasonCode: 'ORDER_CANDIDATE_INVALID',
347
+ detail: 'ORDER_CANDIDATE_COUNT_RANGE',
348
+ }
349
+ }
350
+ const seen = new Set<string>()
351
+ const candidates: Array<{ id: string; description?: string }> = []
352
+ for (const raw of rawCandidates) {
353
+ if (!isRecord(raw)) return { ok: false, reasonCode: 'ORDER_CANDIDATE_INVALID' }
354
+ const extraKeys = Object.keys(raw).filter((key) => key !== 'id' && key !== 'description')
355
+ if (extraKeys.length > 0) return { ok: false, reasonCode: 'ORDER_CANDIDATE_INVALID' }
356
+ const id = raw.id
357
+ if (typeof id !== 'string' || id.length < 1 || id.length > 64) {
358
+ return { ok: false, reasonCode: 'ORDER_CANDIDATE_INVALID' }
359
+ }
360
+ if (seen.has(id)) return { ok: false, reasonCode: 'ORDER_CANDIDATE_INVALID' }
361
+ seen.add(id)
362
+ const description = raw.description
363
+ if (description !== undefined) {
364
+ if (!isBoundedNonBlankString(description, 500) || isHostileProse(description)) {
365
+ return { ok: false, reasonCode: 'ORDER_CANDIDATE_INVALID' }
366
+ }
367
+ candidates.push({ id, description })
368
+ } else {
369
+ candidates.push({ id })
370
+ }
371
+ }
372
+ const levels = Array.from({ length: 9 }, (_, index) => ({
373
+ label: String(index + 2),
374
+ }))
375
+ const questions = candidates.map((candidate, index) => ({
376
+ type: 'score' as const,
377
+ name: `fit_${index + 1}`,
378
+ instructions:
379
+ `Score how well candidate "${candidate.id}"` +
380
+ (candidate.description ? ` (${candidate.description})` : '') +
381
+ ` fits the task. 2 = poor fit, 10 = exact fit.`,
382
+ levels,
383
+ }))
384
+ const request = {
385
+ input: Object.freeze([
386
+ Object.freeze({ type: 'text' as const, text: `${ORDER_STATE_TEMPLATE} Task: ${state}` }),
387
+ ]),
388
+ questions: Object.freeze(questions.map((question) => Object.freeze(question))),
389
+ } satisfies Omit<NativeDecisionRequest, 'model'>
390
+ return { ok: true, request: Object.freeze(request) }
391
+ }
392
+
393
+ export function prepareJevOrderRequest(
394
+ route: ResolvedNativeDecisionRoute,
395
+ input: unknown,
396
+ ): JevOrderPreparation {
397
+ const built = buildJevOrderRequest(input)
398
+ if (!built.ok) return built
399
+ return {
400
+ ok: true,
401
+ request: {
402
+ model: route.model,
403
+ input: built.request.input,
404
+ questions: built.request.questions,
405
+ },
406
+ }
407
+ }
408
+
409
+ /** One ordered entry: the candidate id and its fit score (null when the host answered nothing usable). */
410
+ export interface JevOrderEntry {
411
+ readonly id: string
412
+ readonly score: number | null
413
+ }
414
+
415
+ /**
416
+ * Consume the order result: map each positional score answer back to its
417
+ * candidate, sort by score descending (nulls last, stable), and return the
418
+ * ordering. A missing/invalid score stays visible as null — never guessed.
419
+ */
420
+ export function consumeJevOrderResult(
421
+ response: unknown,
422
+ request: NativeDecisionRequest,
423
+ ): ReadonlyArray<JevOrderEntry> {
424
+ const advice = consumeJevDecisionResult(response, request)
425
+ const entries: Array<{ id: string; score: number | null; order: number }> = []
426
+ request.questions.forEach((question, index) => {
427
+ const answer = advice.answers.find((candidate) => candidate.name === question.name)
428
+ const score =
429
+ answer !== undefined && answer.type === 'score' && typeof answer.score === 'number'
430
+ ? answer.score
431
+ : null
432
+ entries.push({ id: `c${index + 1}`, score, order: index })
433
+ })
434
+ return Object.freeze(
435
+ entries
436
+ .sort((left, right) => {
437
+ if (left.score === null && right.score === null) return left.order - right.order
438
+ if (left.score === null) return 1
439
+ if (right.score === null) return -1
440
+ return right.score - left.score || left.order - right.order
441
+ })
442
+ .map(({ id, score }) => ({ id, score })),
443
+ )
444
+ }
445
+
446
+ // ─── Verify: request building ────────────────────────────────────────────
447
+
448
+ export const VERIFY_ENVELOPE_KEYS: ReadonlySet<string> = new Set([
449
+ 'schemaVersion',
450
+ 'mode',
451
+ 'claims',
452
+ // Optional evidence: the environment/context the claims are judged against
453
+ // (field report beta.13 §6 — without it, environment-dependent claims are
454
+ // unanswerable and the honest verdict is UNVERIFIABLE, not FAIL).
455
+ 'state',
456
+ ])
457
+
458
+ /** The verdict band treated as "the model could not tell" (neither PASS nor FAIL). */
459
+ export const VERIFY_UNVERIFIABLE_LOW = 0.35
460
+ export const VERIFY_UNVERIFIABLE_HIGH = 0.65
461
+
462
+ export type JevVerifyBuildFailure = 'VERIFY_INPUT_INVALID' | 'CLAIM_INVALID'
463
+
464
+ export type JevVerifyBuild =
465
+ | { readonly ok: true; readonly request: Omit<NativeDecisionRequest, 'model'> }
466
+ | { readonly ok: false; readonly reasonCode: JevVerifyBuildFailure }
467
+
468
+ export type JevVerifyPreparation =
469
+ | { readonly ok: true; readonly request: NativeDecisionRequest }
470
+ | { readonly ok: false; readonly reasonCode: JevVerifyBuildFailure }
471
+
472
+ function verifyInstructions(claim: string): string {
473
+ return (
474
+ 'Assess the following claim on its own terms and answer the probability that it holds. ' +
475
+ `Claim: "${claim}". Higher means the claim is more likely to hold. ` +
476
+ 'This is advisory verification; nothing is enforced.'
477
+ )
478
+ }
479
+
480
+ /**
481
+ * Validate the verify tool input and build the model-less request: one
482
+ * predicate question per claim, named after the claim, the claim text riding
483
+ * that question's instructions. The verify mode takes NO state text — any
484
+ * smuggled slot (`state`, `candidates`, review fields) refuses the
485
+ * preparation `VERIFY_INPUT_INVALID`; hostile claim shapes keep the precise
486
+ * `CLAIM_INVALID`.
487
+ */
488
+ export function buildJevVerifyRequest(input: unknown): JevVerifyBuild {
489
+ if (!isRecord(input)) return { ok: false, reasonCode: 'VERIFY_INPUT_INVALID' }
490
+ for (const key of Object.keys(input)) {
491
+ if (!VERIFY_ENVELOPE_KEYS.has(key)) return { ok: false, reasonCode: 'VERIFY_INPUT_INVALID' }
492
+ }
493
+ const rawClaims = input.claims
494
+ if (rawClaims === undefined) return { ok: false, reasonCode: 'VERIFY_INPUT_INVALID' }
495
+ if (!Array.isArray(rawClaims)) return { ok: false, reasonCode: 'CLAIM_INVALID' }
496
+ if (rawClaims.length < VERIFY_MIN_CLAIMS || rawClaims.length > VERIFY_MAX_CLAIMS) {
497
+ return { ok: false, reasonCode: 'CLAIM_INVALID' }
498
+ }
499
+ const seen = new Set<string>()
500
+ const questions: Array<{
501
+ type: 'predicate'
502
+ name: string
503
+ instructions: string
504
+ }> = []
505
+ for (const raw of rawClaims) {
506
+ if (!isRecord(raw)) return { ok: false, reasonCode: 'CLAIM_INVALID' }
507
+ const extraKeys = Object.keys(raw).filter((key) => key !== 'name' && key !== 'claim')
508
+ if (extraKeys.length > 0) return { ok: false, reasonCode: 'CLAIM_INVALID' }
509
+ const name = raw.name
510
+ if (
511
+ typeof name !== 'string' ||
512
+ name.length > CLAIM_NAME_MAX_LENGTH ||
513
+ !TOKEN_PATTERN.test(name) ||
514
+ seen.has(name)
515
+ ) {
516
+ return { ok: false, reasonCode: 'CLAIM_INVALID' }
517
+ }
518
+ const claim = raw.claim
519
+ if (!isBoundedNonBlankString(claim, CLAIM_MAX_LENGTH) || isHostileProse(claim)) {
520
+ return { ok: false, reasonCode: 'CLAIM_INVALID' }
521
+ }
522
+ seen.add(name)
523
+ questions.push({ type: 'predicate', name, instructions: verifyInstructions(claim) })
524
+ }
525
+ // Optional evidence: the caller's state rides as the input text the claims
526
+ // are judged against; without it the template keeps claims self-contained.
527
+ const rawState = (input as Record<string, unknown>).state
528
+ let evidence = VERIFY_STATE_TEMPLATE
529
+ if (rawState !== undefined) {
530
+ if (
531
+ !isBoundedNonBlankString(rawState, NATIVE_DECISION_MAX_PROMPT_LENGTH) ||
532
+ isHostileProse(rawState)
533
+ ) {
534
+ return { ok: false, reasonCode: 'VERIFY_INPUT_INVALID' }
535
+ }
536
+ evidence = `${VERIFY_STATE_TEMPLATE} Evidence/context supplied by the caller, to be trusted as given: ${rawState}`
537
+ }
538
+ const request = {
539
+ input: Object.freeze([Object.freeze({ type: 'text' as const, text: evidence })]),
540
+ questions: Object.freeze(questions.map((question) => Object.freeze(question))),
541
+ } satisfies Omit<NativeDecisionRequest, 'model'>
542
+ return { ok: true, request: Object.freeze(request) }
543
+ }
544
+
545
+ /** `prepareJevPresetRequest` contract: route plus tool input in, request out. */
546
+ export function prepareJevVerifyRequest(
547
+ route: ResolvedNativeDecisionRoute,
548
+ input: unknown,
549
+ ): JevVerifyPreparation {
550
+ const built = buildJevVerifyRequest(input)
551
+ if (!built.ok) return built
552
+ return {
553
+ ok: true,
554
+ request: {
555
+ model: route.model,
556
+ input: built.request.input,
557
+ questions: built.request.questions,
558
+ },
559
+ }
560
+ }
561
+
562
+ // ─── Consumption ─────────────────────────────────────────────────────────
563
+
564
+ export interface JevRankVerdict {
565
+ /**
566
+ * The host's chosen candidate id; `null` when the answer is missing,
567
+ * mistyped, duplicated, an error, or outside the prepared candidate ids.
568
+ */
569
+ readonly winner: string | null
570
+ /**
571
+ * The host's fit score for the winner, on the prepared 2..10 rubric;
572
+ * `null` under the same failures.
573
+ */
574
+ readonly winnerFit: number | null
575
+ }
576
+
577
+ /**
578
+ * Consume a host Decisions response against the prepared rank request.
579
+ * Strict validation is borrowed wholesale from the tool's consumer
580
+ * (`consumeJevDecisionResult`): an answer counts only when its name and type
581
+ * match the prepared questions, duplicates become errors, and anything else
582
+ * is `null`, never a guess.
583
+ */
584
+ export function consumeJevRankResult(
585
+ response: unknown,
586
+ request: NativeDecisionRequest,
587
+ ): JevRankVerdict {
588
+ const advice = consumeJevDecisionResult(response, request)
589
+ let winner: string | null = null
590
+ let winnerFit: number | null = null
591
+ for (const answer of advice.answers) {
592
+ if (answer.type === 'error') continue
593
+ if (answer.type === 'choice' && answer.name === RANK_WINNER_QUESTION_NAME) {
594
+ winner = answer.choice ?? null
595
+ } else if (answer.type === 'score' && answer.name === RANK_FIT_QUESTION_NAME) {
596
+ winnerFit = answer.score ?? null
597
+ }
598
+ }
599
+ return Object.freeze({ winner, winnerFit })
600
+ }
601
+
602
+ /**
603
+ * The verdict of one verified claim. `BLOCK` is the fail-closed answer: a
604
+ * host refusal, timeout, duplicate or malformed reply must never read as
605
+ * verified (or as refuted — an unverifiable claim is unverifiable, not
606
+ * false).
607
+ */
608
+ export type VerifyClaimVerdict = 'PASS' | 'FAIL' | 'UNVERIFIABLE' | 'BLOCK'
609
+
610
+ /**
611
+ * Consume a host Decisions response against the prepared verify request:
612
+ * one verdict per claim name. Probability ≥ {@link VERIFY_PASS_THRESHOLD} is
613
+ * `PASS`, below is `FAIL`; error answers, missing answers and any unusable
614
+ * response shape are `BLOCK` — fail-closed.
615
+ */
616
+ export function consumeJevVerifyResult(
617
+ response: unknown,
618
+ request: NativeDecisionRequest,
619
+ ): Readonly<Record<string, VerifyClaimVerdict>> {
620
+ const advice = consumeJevDecisionResult(response, request)
621
+ const verdicts: Record<string, VerifyClaimVerdict> = {}
622
+ for (const question of request.questions) {
623
+ const answer = advice.answers.find((candidate) => candidate.name === question.name)
624
+ if (answer !== undefined && answer.type === 'predicate') {
625
+ const probability = answer.probability ?? 0
626
+ verdicts[question.name] =
627
+ probability >= VERIFY_PASS_THRESHOLD
628
+ ? 'PASS'
629
+ : probability >= VERIFY_UNVERIFIABLE_LOW
630
+ ? 'UNVERIFIABLE'
631
+ : 'FAIL'
632
+ } else {
633
+ verdicts[question.name] = 'BLOCK'
634
+ }
635
+ }
636
+ return Object.freeze(verdicts)
637
+ }