pantheon-opencode 1.6.1 → 1.6.2-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 (169) hide show
  1. package/AGENTS.global.md +11 -11
  2. package/AGENTS.md +2 -2
  3. package/CHANGELOG.md +179 -0
  4. package/README.md +268 -20
  5. package/README.pt-BR.md +34 -11
  6. package/bin/pantheon-init.mjs +301 -32
  7. package/package.json +13 -4
  8. package/plugin.json +1 -1
  9. package/prompts/README.md +58 -0
  10. package/prompts/debug-issue.prompt.md +46 -0
  11. package/prompts/dynamic/agora-generated.txt +38 -0
  12. package/prompts/dynamic/orchestrate-generated.txt +51 -0
  13. package/prompts/focus.prompt.md +42 -0
  14. package/prompts/implement-feature.prompt.md +39 -0
  15. package/prompts/optimize-database.prompt.md +58 -0
  16. package/prompts/orchestrate-with-zeus.prompt.md +441 -0
  17. package/prompts/plan-architecture.prompt.md +59 -0
  18. package/prompts/quick-discovery-large-codebase.prompt.md +294 -0
  19. package/prompts/quick-plan-large-feature.prompt.md +197 -0
  20. package/prompts/semantic-summarize.md +50 -0
  21. package/prompts/sketch.prompt.md +91 -0
  22. package/prompts/subtask.prompt.md +131 -0
  23. package/pyproject.toml +1 -1
  24. package/scripts/build-agents-md.mjs +1 -1
  25. package/scripts/decision-router-budget.mjs +780 -0
  26. package/scripts/decision-router-v2-budget.mjs +218 -0
  27. package/scripts/decision-router-v2-egress.mjs +272 -0
  28. package/scripts/decision-router.mjs +758 -0
  29. package/scripts/docs-check-changelog.mjs +5 -3
  30. package/scripts/doctor.mjs +213 -45
  31. package/scripts/install/agents-md.mjs +2 -1
  32. package/scripts/install/component-selection.mjs +9 -4
  33. package/scripts/install/config-migration.mjs +344 -393
  34. package/scripts/install/config-paths.mjs +15 -0
  35. package/scripts/install/decision-layer-preview.mjs +376 -0
  36. package/scripts/install/decision-router-config.mjs +560 -0
  37. package/scripts/install/health-check.mjs +20 -11
  38. package/scripts/install/interactive.mjs +103 -14
  39. package/scripts/install/interrupt.mjs +4 -3
  40. package/scripts/install/mcp-permissions.mjs +164 -0
  41. package/scripts/install/migrate.mjs +123 -72
  42. package/scripts/install/opencode-version.mjs +31 -13
  43. package/scripts/install/opencode-wildcard.mjs +106 -0
  44. package/scripts/install/opencode.mjs +411 -184
  45. package/scripts/install/plan-artifact.mjs +12 -1
  46. package/scripts/install/plan.mjs +8 -1
  47. package/scripts/install/plugin.mjs +18 -1
  48. package/scripts/install/state.mjs +12 -0
  49. package/scripts/install/strings.mjs +22 -0
  50. package/scripts/install/v2-capabilities.mjs +5 -6
  51. package/scripts/install/venv.mjs +34 -17
  52. package/scripts/mcp_resources.py +11 -9
  53. package/scripts/package-evidence.mjs +1 -0
  54. package/scripts/release-notes.mjs +30 -7
  55. package/scripts/restore.mjs +22 -10
  56. package/scripts/sandbox-env.sh +174 -0
  57. package/scripts/setup-v2-config.mjs +1703 -0
  58. package/scripts/setup.mjs +415 -0
  59. package/scripts/sync-tui.mjs +9 -1
  60. package/scripts/test-opencode-v2-sandbox.sh +448 -157
  61. package/scripts/update-skills-lock.mjs +30 -0
  62. package/scripts/versioning.mjs +41 -5
  63. package/skills-lock.json +10 -10
  64. package/src/agents/aphrodite.md +11 -3
  65. package/src/agents/apollo.md +6 -0
  66. package/src/agents/athena.md +7 -1
  67. package/src/agents/demeter.md +6 -2
  68. package/src/agents/gaia.md +5 -0
  69. package/src/agents/hephaestus.md +7 -2
  70. package/src/agents/hermes.md +14 -13
  71. package/src/agents/iris.md +6 -2
  72. package/src/agents/mnemosyne.md +21 -50
  73. package/src/agents/nyx.md +6 -2
  74. package/src/agents/prometheus.md +5 -1
  75. package/src/agents/talos.md +10 -1
  76. package/src/agents/themis.md +5 -1
  77. package/src/agents/zeus.md +61 -65
  78. package/src/instructions/agent-return-format.instructions.md +1 -1
  79. package/src/instructions/memory-protocol.instructions.md +3 -3
  80. package/src/instructions/test-economy.instructions.md +6 -6
  81. package/src/mcp/eval_store.py +11 -4
  82. package/src/mcp/mcp_resources.py +11 -9
  83. package/src/mcp/memory_mcp.py +293 -150
  84. package/src/mcp/pantheon_vision.py +37 -36
  85. package/src/pantheon/cost-command.ts +30 -133
  86. package/src/pantheon/jev-decision-tool.ts +347 -0
  87. package/src/pantheon/jev-decisions.ts +625 -0
  88. package/src/pantheon/local-decision-agents.ts +170 -0
  89. package/src/pantheon/local-decision-budget.ts +1046 -0
  90. package/src/pantheon/local-decision-egress-gate.ts +144 -0
  91. package/src/pantheon/local-decision-prompt.ts +256 -0
  92. package/src/pantheon/local-decision-provider-matrix.ts +44 -0
  93. package/src/pantheon/local-decision-router.ts +225 -0
  94. package/src/pantheon/local-decision-skills.ts +281 -0
  95. package/src/pantheon/local-decision-tool.ts +160 -0
  96. package/src/pantheon/local-decision-turn.ts +101 -0
  97. package/src/pantheon/local-decision-usage-evidence.internal.ts +168 -0
  98. package/src/pantheon/local-orchestration.ts +322 -0
  99. package/src/pantheon/model-pricing.ts +1 -536
  100. package/src/pantheon/pantheon-decision-catalog.ts +101 -0
  101. package/src/pantheon/v2-decision-budget.ts +456 -0
  102. package/src/pantheon/v2-decision-contracts.ts +300 -0
  103. package/src/pantheon/v2-decision-egress.ts +203 -0
  104. package/src/pantheon/v2-decision-events.ts +180 -0
  105. package/src/pantheon/v2-decision-heuristic.ts +121 -0
  106. package/src/pantheon/v2-decision-points/p1-agent.ts +91 -0
  107. package/src/pantheon/v2-decision-points/p10-claims.ts +243 -0
  108. package/src/pantheon/v2-decision-points/p11-catalog.ts +220 -0
  109. package/src/pantheon/v2-decision-points/p2-skill.ts +83 -0
  110. package/src/pantheon/v2-decision-points/p3-complexity.ts +108 -0
  111. package/src/pantheon/v2-decision-points/p4-memory.ts +137 -0
  112. package/src/pantheon/v2-decision-points/p5-themis-verify.ts +190 -0
  113. package/src/pantheon/v2-decision-points/p6-council.ts +161 -0
  114. package/src/pantheon/v2-decision-points/p7-loop.ts +263 -0
  115. package/src/pantheon/v2-decision-points/p8-guardrail.ts +367 -0
  116. package/src/pantheon/v2-decision-points/p9-memory-compaction.ts +304 -0
  117. package/src/pantheon/v2-decision-points.ts +242 -0
  118. package/src/pantheon/v2-decision-policy.ts +166 -0
  119. package/src/pantheon/v2-decision-premises.ts +146 -0
  120. package/src/pantheon/v2-decision-prompt-surface.ts +115 -0
  121. package/src/pantheon/v2-decision-rank.ts +91 -0
  122. package/src/pantheon/v2-decision-registry.ts +391 -0
  123. package/src/pantheon/v2-decision-resource-ids.ts +127 -0
  124. package/src/pantheon/v2-decision-shadow.ts +123 -0
  125. package/src/pantheon/v2-decision-telemetry.ts +214 -0
  126. package/src/pantheon/v2-decision-verify.ts +51 -0
  127. package/src/pantheon/v2-hook-engine.ts +307 -0
  128. package/src/pantheon/v2-native-decisions.ts +365 -0
  129. package/src/pantheon/v2-shell-hooks.ts +66 -2
  130. package/src/pantheon/v2-tools.ts +38 -15
  131. package/src/pantheon/v2-vision.ts +34 -29
  132. package/src/pantheon/vision.ts +43 -10
  133. package/src/plugin-v2.ts +449 -79
  134. package/src/plugin.ts +4 -0
  135. package/src/plugins/tui/THIRD_PARTY_NOTICES.md +24 -0
  136. package/src/plugins/tui/dist/server-v2.d.ts +22 -0
  137. package/src/plugins/tui/dist/server-v2.d.ts.map +1 -0
  138. package/src/plugins/tui/dist/server-v2.js +26 -0
  139. package/src/plugins/tui/dist/server-v2.js.map +1 -0
  140. package/src/plugins/tui/dist/tui-v2.d.ts +54 -51
  141. package/src/plugins/tui/dist/tui-v2.d.ts.map +1 -1
  142. package/src/plugins/tui/dist/tui-v2.js +762 -529
  143. package/src/plugins/tui/dist/tui-v2.js.map +1 -1
  144. package/src/plugins/tui/dist/tui.js +1 -1
  145. package/src/plugins/tui/index-v2.ts +3 -6
  146. package/src/plugins/tui/package-lock.json +17 -2
  147. package/src/plugins/tui/package.json +6 -3
  148. package/src/plugins/tui/src/context-gauge.ts +59 -0
  149. package/src/plugins/tui/src/cost-data.ts +607 -0
  150. package/src/plugins/tui/src/index-v2.tsx +360 -242
  151. package/src/plugins/tui/src/model-pricing.ts +536 -0
  152. package/src/plugins/tui/src/model-usage-limits.ts +4 -0
  153. package/src/plugins/tui/src/server-v2.ts +21 -0
  154. package/src/plugins/tui/src/sidebar-width.ts +129 -0
  155. package/src/plugins/tui/tsconfig.typecheck.json +1 -1
  156. package/src/plugins/tui/tsdown.config.ts +34 -5
  157. package/src/routing.yml +72 -13
  158. package/src/skills/README.md +4 -0
  159. package/src/skills/agent-coordination/SKILL.md +0 -3
  160. package/src/skills/artifact-management/SKILL.md +0 -3
  161. package/src/skills/auto-continue/SKILL.md +0 -3
  162. package/src/skills/code-review-checklist/SKILL.md +0 -3
  163. package/src/skills/context-compression/SKILL.md +0 -3
  164. package/src/skills/memory-bank/SKILL.md +6 -28
  165. package/src/skills/session-goal/SKILL.md +0 -3
  166. package/src/skills/simplify/SKILL.md +5 -4
  167. package/src/skills/tdd-with-agents/SKILL.md +0 -3
  168. package/src/skills/visual-review-pipeline/SKILL.md +0 -1
  169. package/src/pantheon/memory-context.ts +0 -1017
@@ -0,0 +1,83 @@
1
+ /**
2
+ * P2 (skill) shadow adapter.
3
+ *
4
+ * P2a reports *where* the skill list came from — the prompt payload or the
5
+ * per-turn draft (`ctx.skill.list()`, F-06) — and refuses with an explicit
6
+ * reason when neither source is usable. It reports only: P2 is an empirical
7
+ * no-op on the default path (F-09, PRE-02). P2b was cancelled rather than
8
+ * emulated, because no measured mutation surface reaches project skills
9
+ * (PROBE-E); the registry records P2 as advisory.
10
+ *
11
+ * Shadow by construction: the admission payload is cloned and frozen by
12
+ * `observeShadow`, the record always says `effect: 'none'`, and no failure is
13
+ * rethrown into admission.
14
+ */
15
+
16
+ import { resolveSkillSource, type SkillsSource } from '../local-decision-prompt.ts'
17
+ import { rankCandidates } from '../v2-decision-rank.ts'
18
+ import {
19
+ observeShadow,
20
+ type ShadowDecision,
21
+ type ShadowRecord,
22
+ type ShadowSink,
23
+ } from '../v2-decision-shadow.ts'
24
+
25
+ export interface SkillPointResult {
26
+ readonly record: ShadowRecord
27
+ /** Which source produced the observed list: the prompt, the draft, or none. */
28
+ readonly skillsSource: SkillsSource
29
+ /** Token explaining the decision; always present. */
30
+ readonly reason: string
31
+ /** Deterministic order of the observed source list; empty when there is none. */
32
+ readonly rankedSkillIds: readonly string[]
33
+ }
34
+
35
+ function isRecord(value: unknown): value is Record<string, unknown> {
36
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
37
+ }
38
+
39
+ /** Read `prompt.skills` from an admission payload without normalizing it. */
40
+ function promptSkills(admission: unknown): unknown {
41
+ if (!isRecord(admission)) return undefined
42
+ const prompt = admission.prompt
43
+ return isRecord(prompt) ? prompt.skills : undefined
44
+ }
45
+
46
+ /**
47
+ * Observe P2 in shadow.
48
+ *
49
+ * @param admission Host admission payload (never mutated).
50
+ * @param sink Optional shadow-record observer.
51
+ * @param draftSkills Optional per-turn draft list; observed, never applied.
52
+ */
53
+ export function evaluateSkillPoint(
54
+ admission: unknown,
55
+ sink?: ShadowSink,
56
+ draftSkills?: unknown,
57
+ ): SkillPointResult {
58
+ const resolution = resolveSkillSource(promptSkills(admission), draftSkills)
59
+ let rankedSkillIds: readonly string[] = []
60
+ let decision: ShadowDecision = { decision: 'refusal', reason: resolution.reason }
61
+
62
+ if (resolution.skillIds.length > 0) {
63
+ const ranked = rankCandidates(
64
+ resolution.skillIds.map((id) => ({
65
+ id,
66
+ score: 1,
67
+ justification: `source.${resolution.skillsSource}`,
68
+ })),
69
+ )
70
+ if (ranked.ok) {
71
+ rankedSkillIds = ranked.ordered.map((candidate) => candidate.id)
72
+ decision = { decision: 'rank', reason: `rank.source.${resolution.skillsSource}` }
73
+ }
74
+ }
75
+
76
+ const record = observeShadow('P2', admission, () => decision, sink)
77
+ return {
78
+ record,
79
+ skillsSource: resolution.skillsSource,
80
+ reason: decision.reason,
81
+ rankedSkillIds,
82
+ }
83
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * P3 (complexity) shadow adapter.
3
+ *
4
+ * Two properties are made explicit here, because the V1 surface left both
5
+ * ambiguous (D1, D4).
6
+ *
7
+ * D4 — label polarity and `label = max`. A dimension vector is classified by
8
+ * its **maximum** dimension score: `2 → high`, `1 → medium`, otherwise `low`.
9
+ * Higher is always more complex, and {@link COMPLEXITY_LABEL_LEVEL} maps the
10
+ * label to a `score` level that increases with it (`low = 1` … `high = 3`).
11
+ * Raising any dimension can never lower the label.
12
+ *
13
+ * D1 — `gateStatus` has an observable effect. The same complexity vector yields
14
+ * a different decision and reason per gate: `none → score`, `review_required →
15
+ * advisory` (classification is usable only as advice), and
16
+ * `human_approval_required` / `unresolved → refusal`. In shadow the effect is
17
+ * the record itself; no admission decision is changed.
18
+ */
19
+
20
+ import {
21
+ classifyLocalDecisionComplexity,
22
+ type LocalDecisionComplexityDimensions,
23
+ type LocalDecisionGateStatus,
24
+ } from '../local-decision-router.ts'
25
+ import {
26
+ observeShadow,
27
+ type ShadowDecision,
28
+ type ShadowRecord,
29
+ type ShadowSink,
30
+ } from '../v2-decision-shadow.ts'
31
+
32
+ export type ComplexityLabel = 'low' | 'medium' | 'high' | 'abstain'
33
+ export type ComplexityLevel = 1 | 2 | 3
34
+
35
+ /** Polarity: the level rises with the label, so `high` is the most complex. */
36
+ export const COMPLEXITY_LABEL_LEVEL: Readonly<Record<'low' | 'medium' | 'high', ComplexityLevel>> =
37
+ Object.freeze({ low: 1, medium: 2, high: 3 })
38
+
39
+ const GATE_STATUSES: ReadonlySet<string> = new Set([
40
+ 'none',
41
+ 'review_required',
42
+ 'human_approval_required',
43
+ 'unresolved',
44
+ ])
45
+
46
+ export interface ComplexityPointResult {
47
+ readonly record: ShadowRecord
48
+ readonly label: ComplexityLabel
49
+ /** `null` for `abstain`: no ordinal level is invented for an invalid vector. */
50
+ readonly level: ComplexityLevel | null
51
+ readonly gateStatus: LocalDecisionGateStatus | null
52
+ readonly reason: string
53
+ }
54
+
55
+ function isRecord(value: unknown): value is Record<string, unknown> {
56
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
57
+ }
58
+
59
+ /** Gate outcome: only `none` leaves the classification free of advice or refusal. */
60
+ function gateOutcome(
61
+ gate: LocalDecisionGateStatus,
62
+ label: Exclude<ComplexityLabel, 'abstain'>,
63
+ ): ShadowDecision {
64
+ if (gate === 'none') return { decision: `score.${label}`, reason: 'gate.none' }
65
+ if (gate === 'review_required') {
66
+ return { decision: `advisory.${label}`, reason: 'gate.review.required' }
67
+ }
68
+ if (gate === 'human_approval_required') {
69
+ return { decision: 'refusal', reason: 'gate.human.approval.required' }
70
+ }
71
+ return { decision: 'refusal', reason: 'gate.unresolved' }
72
+ }
73
+
74
+ /**
75
+ * Observe P3 in shadow.
76
+ *
77
+ * @param input `{ complexity, gateStatus }` — structured dimensions plus the
78
+ * security status, which never changes the label itself.
79
+ * @param sink Optional shadow-record observer.
80
+ */
81
+ export function evaluateComplexityPoint(input: unknown, sink?: ShadowSink): ComplexityPointResult {
82
+ const complexity = isRecord(input) ? input.complexity : undefined
83
+ const rawGate = isRecord(input) ? input.gateStatus : undefined
84
+ const classified = classifyLocalDecisionComplexity(
85
+ complexity as LocalDecisionComplexityDimensions | undefined,
86
+ )
87
+ const gateStatus =
88
+ typeof rawGate === 'string' && GATE_STATUSES.has(rawGate)
89
+ ? (rawGate as LocalDecisionGateStatus)
90
+ : null
91
+
92
+ let decision: ShadowDecision
93
+ if (classified.label === 'abstain') {
94
+ decision = { decision: 'refusal', reason: 'complexity.invalid' }
95
+ } else if (gateStatus === null) {
96
+ decision = { decision: 'refusal', reason: 'invalid.gate' }
97
+ } else {
98
+ decision = gateOutcome(gateStatus, classified.label)
99
+ }
100
+
101
+ return {
102
+ record: observeShadow('P3', input, () => decision, sink),
103
+ label: classified.label,
104
+ level: classified.label === 'abstain' ? null : COMPLEXITY_LABEL_LEVEL[classified.label],
105
+ gateStatus,
106
+ reason: decision.reason,
107
+ }
108
+ }
@@ -0,0 +1,137 @@
1
+ /**
2
+ * P4 (memory) shadow adapter — `rank` over memory candidates.
3
+ *
4
+ * P4 sits on the measured carrier (`src/pantheon` memory reaches the model
5
+ * through the `system` array, F-02, and survives compaction as a checkpoint,
6
+ * F-03). The *candidate source* itself was not measured, so this adapter does
7
+ * not reshape it: it consumes the shared `rank` contract unchanged and refuses
8
+ * when the source is absent or does not already satisfy that contract. A
9
+ * missing justification is never invented, and an absent source is never
10
+ * treated as an empty ranking.
11
+ *
12
+ * Shadow by construction: `observeShadow` clones and freezes the source, the
13
+ * record always says `effect: 'none'`, and no failure — including an unclonable
14
+ * source — is rethrown into admission.
15
+ */
16
+
17
+ import { type RankFailure, rankCandidates } from '../v2-decision-rank.ts'
18
+ import {
19
+ observeShadow,
20
+ type ShadowDecision,
21
+ type ShadowRecord,
22
+ type ShadowSink,
23
+ } from '../v2-decision-shadow.ts'
24
+
25
+ export type MemorySource = 'candidates' | 'none'
26
+
27
+ export interface MemoryPointResult {
28
+ readonly record: ShadowRecord
29
+ /** `candidates` when the source was a candidate array, `none` otherwise. */
30
+ readonly memorySource: MemorySource
31
+ /** Deterministic ranked ids; empty on every failure. */
32
+ readonly rankedMemoryIds: readonly string[]
33
+ /** Token explaining the decision; always present. */
34
+ readonly reason: string
35
+ }
36
+
37
+ /**
38
+ * Retrieval comparison record for PLAN Phase 2.
39
+ *
40
+ * BM25 stays the baseline. The embeddings arm is **NOT RUN**: this task has no
41
+ * embedding provider and no network, and hand-authored vectors would measure
42
+ * the fixture rather than an embedding model. No number is reported here and no
43
+ * adoption decision is claimed; promoting embeddings requires a measured
44
+ * comparison, not this record.
45
+ */
46
+ export const MEMORY_RETRIEVAL_COMPARISON = Object.freeze({
47
+ baseline: 'bm25',
48
+ embeddings: 'not_run',
49
+ reason:
50
+ 'offline: no embedding model available; synthetic vectors would measure the fixture, not embeddings',
51
+ } as const)
52
+
53
+ interface MemoryDecision {
54
+ readonly decision: ShadowDecision
55
+ readonly source: MemorySource
56
+ readonly ids: readonly string[]
57
+ }
58
+
59
+ /** Contract failure codes are uppercase; record tokens are lower-case dotted. */
60
+ function tokenOf(code: RankFailure): string {
61
+ return code.toLowerCase().split('_').join('.')
62
+ }
63
+
64
+ /**
65
+ * Classify one observed source.
66
+ *
67
+ * The array is passed through to `rankCandidates` untouched: what the rank
68
+ * contract rejects is reported, never repaired.
69
+ */
70
+ function classifyMemorySource(source: unknown): MemoryDecision {
71
+ if (source === undefined || source === null) {
72
+ return {
73
+ decision: { decision: 'refusal', reason: 'memory.source.absent' },
74
+ source: 'none',
75
+ ids: [],
76
+ }
77
+ }
78
+ if (!Array.isArray(source)) {
79
+ return {
80
+ decision: { decision: 'refusal', reason: 'memory.source.unsupported' },
81
+ source: 'none',
82
+ ids: [],
83
+ }
84
+ }
85
+ if (source.length === 0) {
86
+ return {
87
+ decision: { decision: 'refusal', reason: 'memory.source.empty' },
88
+ source: 'candidates',
89
+ ids: [],
90
+ }
91
+ }
92
+ const ranked = rankCandidates(source)
93
+ if (!ranked.ok) {
94
+ return {
95
+ decision: { decision: 'refusal', reason: `memory.rank.${tokenOf(ranked.code)}` },
96
+ source: 'candidates',
97
+ ids: [],
98
+ }
99
+ }
100
+ return {
101
+ decision: { decision: 'rank', reason: 'memory.ranked' },
102
+ source: 'candidates',
103
+ ids: ranked.ordered.map((candidate) => candidate.id),
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Observe P4 in shadow.
109
+ *
110
+ * @param source Memory candidate source. Never mutated.
111
+ * @param sink Optional shadow-record observer.
112
+ */
113
+ export function evaluateMemoryPoint(source: unknown, sink?: ShadowSink): MemoryPointResult {
114
+ let observed: MemoryDecision = {
115
+ decision: { decision: 'refusal', reason: 'memory.source.absent' },
116
+ source: 'none',
117
+ ids: [],
118
+ }
119
+ const record = observeShadow(
120
+ 'P4',
121
+ source,
122
+ (isolated) => {
123
+ const classified = classifyMemorySource(isolated)
124
+ observed = classified
125
+ return classified.decision
126
+ },
127
+ sink,
128
+ )
129
+
130
+ const failed = record.decision === 'unavailable'
131
+ return {
132
+ record,
133
+ memorySource: failed ? 'none' : observed.source,
134
+ rankedMemoryIds: failed ? [] : observed.ids,
135
+ reason: record.reason,
136
+ }
137
+ }
@@ -0,0 +1,190 @@
1
+ /**
2
+ * P5 (Themis) shadow adapter — `verify` in the `execute.after` position.
3
+ *
4
+ * The verdict vocabulary is exactly `PASS | FAIL | BLOCK`. Nothing else is
5
+ * promoted: a response the contract does not recognize is `BLOCK` when the
6
+ * verification is mandatory and `FAIL` when it is advisory, and
7
+ * `failed_to_respond` follows the same rule. A verdict is never `PASS` because
8
+ * a verification was skipped, missing or unreadable.
9
+ *
10
+ * Shadow, and not only by configuration. `execute.before` remains the **only**
11
+ * hard barrier in this design: this adapter runs after the host already ran the
12
+ * tool, returns a record and nothing else, and is synchronous. It cannot deny
13
+ * an admission, cannot delay a tool result and cannot mutate the event it
14
+ * observes — the payload is cloned and frozen before any reading, and the
15
+ * record always says `effect: 'none'`.
16
+ *
17
+ * `execute.after` itself was never fired in a probe (PM-09: `execute.before`
18
+ * and `permission.evaluate` were registered and never invoked), so this module
19
+ * is a contract-shaped recorder, not evidence that verification runs in
20
+ * production.
21
+ */
22
+
23
+ import {
24
+ observeShadow,
25
+ type ShadowDecision,
26
+ type ShadowRecord,
27
+ type ShadowSink,
28
+ } from '../v2-decision-shadow.ts'
29
+ import {
30
+ resolveVerify,
31
+ type VerifyOutcome,
32
+ type VerifyRequirement,
33
+ type VerifyVerdict,
34
+ } from '../v2-decision-verify.ts'
35
+
36
+ const TOKEN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/
37
+
38
+ export type ThemisVerificationSource = 'declared' | 'none'
39
+
40
+ export interface ThemisPointResult {
41
+ readonly record: ShadowRecord
42
+ /** `declared` when at least one usable requirement was observed. */
43
+ readonly verificationSource: ThemisVerificationSource
44
+ /** One outcome per requirement, in declaration order; empty when none was usable. */
45
+ readonly verdicts: readonly VerifyOutcome[]
46
+ /** BLOCK over FAIL over PASS; `null` when no verdict could be stated. */
47
+ readonly verdict: VerifyVerdict | null
48
+ /** Whether an `execute.after` event was observed at all. */
49
+ readonly executionObserved: boolean
50
+ /** Token explaining the decision; always present. */
51
+ readonly reason: string
52
+ }
53
+
54
+ interface RequirementParse {
55
+ readonly ok: boolean
56
+ /** Usable requirements, in declaration order. */
57
+ readonly requirements: readonly VerifyRequirement[]
58
+ /** Token naming why the parse failed; empty when it succeeded. */
59
+ readonly reason: string
60
+ }
61
+
62
+ function isRecord(value: unknown): value is Record<string, unknown> {
63
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
64
+ }
65
+
66
+ function isToken(value: unknown): value is string {
67
+ return typeof value === 'string' && TOKEN.test(value)
68
+ }
69
+
70
+ /**
71
+ * Parse the declared verifications.
72
+ *
73
+ * A requirement is usable only when it names a token id and states `mandatory`
74
+ * as a boolean. Duplicate ids are refused: the same requirement counted twice
75
+ * would let one gap look like two checks.
76
+ */
77
+ function parseRequirements(candidate: unknown): RequirementParse {
78
+ if (!Array.isArray(candidate) || candidate.length === 0) {
79
+ return { ok: false, requirements: [], reason: 'themis.requirements.absent' }
80
+ }
81
+ const requirements: VerifyRequirement[] = []
82
+ const ids = new Set<string>()
83
+ for (const entry of candidate) {
84
+ if (!isRecord(entry) || !isToken(entry.id) || typeof entry.mandatory !== 'boolean') {
85
+ return { ok: false, requirements: [], reason: 'themis.requirement.invalid' }
86
+ }
87
+ if (ids.has(entry.id)) {
88
+ return { ok: false, requirements: [], reason: 'themis.requirements.duplicate' }
89
+ }
90
+ ids.add(entry.id)
91
+ requirements.push({ id: entry.id, mandatory: entry.mandatory })
92
+ }
93
+ return { ok: true, requirements, reason: '' }
94
+ }
95
+
96
+ /** Read one response by requirement id; an unreadable map yields none at all. */
97
+ function responseFor(responses: unknown, requirementId: string): unknown {
98
+ if (!isRecord(responses)) return undefined
99
+ return responses[requirementId]
100
+ }
101
+
102
+ /** BLOCK dominates FAIL, FAIL dominates PASS. */
103
+ function aggregateVerdict(verdicts: readonly VerifyOutcome[]): VerifyVerdict | null {
104
+ if (verdicts.length === 0) return null
105
+ if (verdicts.some((outcome) => outcome.verdict === 'BLOCK')) return 'BLOCK'
106
+ if (verdicts.some((outcome) => outcome.verdict === 'FAIL')) return 'FAIL'
107
+ return 'PASS'
108
+ }
109
+
110
+ interface ThemisAnalysis {
111
+ readonly decision: ShadowDecision
112
+ readonly source: ThemisVerificationSource
113
+ readonly verdicts: readonly VerifyOutcome[]
114
+ readonly verdict: VerifyVerdict | null
115
+ readonly executionObserved: boolean
116
+ }
117
+
118
+ function analyze(input: unknown): ThemisAnalysis {
119
+ const executionObserved = isRecord(input) && isRecord(input.execution)
120
+ const parsed = parseRequirements(isRecord(input) ? input.requirements : undefined)
121
+ if (!parsed.ok) {
122
+ return {
123
+ decision: { decision: 'refusal', reason: parsed.reason },
124
+ source: 'none',
125
+ verdicts: [],
126
+ verdict: null,
127
+ executionObserved,
128
+ }
129
+ }
130
+ const responses = isRecord(input) ? input.responses : undefined
131
+ const verdicts = parsed.requirements.map((requirement) =>
132
+ resolveVerify(requirement, responseFor(responses, requirement.id)),
133
+ )
134
+ const verdict = aggregateVerdict(verdicts)
135
+ if (verdict === null) {
136
+ return {
137
+ decision: { decision: 'refusal', reason: 'themis.verdict.absent' },
138
+ source: 'none',
139
+ verdicts: [],
140
+ verdict: null,
141
+ executionObserved,
142
+ }
143
+ }
144
+ return {
145
+ decision: {
146
+ decision: `verify.${verdict.toLowerCase()}`,
147
+ reason: `themis.verified.${verdict.toLowerCase()}`,
148
+ },
149
+ source: 'declared',
150
+ verdicts,
151
+ verdict,
152
+ executionObserved,
153
+ }
154
+ }
155
+
156
+ /**
157
+ * Observe P5 in shadow.
158
+ *
159
+ * @param input `{ execution, requirements, responses }`. The `execute.after`
160
+ * event is observed, never written back.
161
+ * @param sink Optional shadow-record observer.
162
+ */
163
+ export function evaluateThemisPoint(input: unknown, sink?: ShadowSink): ThemisPointResult {
164
+ let analysis: ThemisAnalysis = {
165
+ decision: { decision: 'refusal', reason: 'themis.requirements.absent' },
166
+ source: 'none',
167
+ verdicts: [],
168
+ verdict: null,
169
+ executionObserved: false,
170
+ }
171
+ const record = observeShadow(
172
+ 'P5',
173
+ input,
174
+ (isolated) => {
175
+ analysis = analyze(isolated)
176
+ return analysis.decision
177
+ },
178
+ sink,
179
+ )
180
+
181
+ const contained = record.decision === 'unavailable'
182
+ return {
183
+ record,
184
+ verificationSource: contained ? 'none' : analysis.source,
185
+ verdicts: contained ? [] : analysis.verdicts,
186
+ verdict: contained ? null : analysis.verdict,
187
+ executionObserved: contained ? false : analysis.executionObserved,
188
+ reason: record.reason,
189
+ }
190
+ }
@@ -0,0 +1,161 @@
1
+ /**
2
+ * P6 (council) shadow adapter — `rank` + `score` over council candidates.
3
+ *
4
+ * Ranking is the shared deterministic contract: score descending, id ascending,
5
+ * a justification per candidate. The `score` question is a real validated
6
+ * `ask.score` over the top-ranked candidate, built with at most
7
+ * {@link COUNCIL_SCORE_LEVELS} ordinal levels. Shadow has no responder, so an
8
+ * unanswered question is recorded as `council.no.answer` rather than being
9
+ * filled with a fabricated level; a supplied answer is resolved through the
10
+ * shared contract and still changes nothing.
11
+ *
12
+ * What was measured is the prompt payload **shape** and the presence of the
13
+ * candidate surface (F-08); a populated `prompt.agents` was never observed
14
+ * (PM-07), so this adapter never reads the admission payload and never claims
15
+ * that a council ran. It consumes candidates a caller already has.
16
+ */
17
+
18
+ import { evaluateAskAnswer, type ScoreQuestion, validateAsk } from '../v2-decision-contracts.ts'
19
+ import { rankCandidates } from '../v2-decision-rank.ts'
20
+ import {
21
+ observeShadow,
22
+ type ShadowDecision,
23
+ type ShadowRecord,
24
+ type ShadowSink,
25
+ } from '../v2-decision-shadow.ts'
26
+
27
+ /** Ordinal levels of the council score question (2..10 is the contract range). */
28
+ export const COUNCIL_SCORE_LEVELS = 5
29
+
30
+ const QUESTION_TEXT = 'Rate the priority of the top-ranked council candidate.'
31
+
32
+ export type CouncilSource = 'candidates' | 'none'
33
+
34
+ export interface CouncilPointResult {
35
+ readonly record: ShadowRecord
36
+ /** `candidates` when a candidate array was observed, `none` otherwise. */
37
+ readonly councilSource: CouncilSource
38
+ /** Ranked ids, best first; empty on every failure. */
39
+ readonly rankedIds: readonly string[]
40
+ /** The validated score question, or `null` when none could be built. */
41
+ readonly question: ScoreQuestion | null
42
+ /** Resolved ordinal level, or `null` when no valid answer was supplied. */
43
+ readonly score: number | null
44
+ /** Token explaining the decision; always present. */
45
+ readonly reason: string
46
+ }
47
+
48
+ function isRecord(value: unknown): value is Record<string, unknown> {
49
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
50
+ }
51
+
52
+ /** Contract failure codes are uppercase; record tokens are lower-case dotted. */
53
+ function tokenOf(code: string): string {
54
+ return code.toLowerCase().split('_').join('.')
55
+ }
56
+
57
+ interface CouncilAnalysis {
58
+ readonly decision: ShadowDecision
59
+ readonly source: CouncilSource
60
+ readonly ids: readonly string[]
61
+ readonly question: ScoreQuestion | null
62
+ readonly score: number | null
63
+ }
64
+
65
+ function refusal(reason: string, source: CouncilSource = 'none'): CouncilAnalysis {
66
+ return {
67
+ decision: { decision: 'refusal', reason },
68
+ source,
69
+ ids: [],
70
+ question: null,
71
+ score: null,
72
+ }
73
+ }
74
+
75
+ function analyze(input: unknown): CouncilAnalysis {
76
+ const candidates = isRecord(input) ? input.candidates : undefined
77
+ if (candidates === undefined || candidates === null) {
78
+ return refusal('council.source.absent')
79
+ }
80
+ if (!Array.isArray(candidates)) return refusal('council.source.unsupported')
81
+ if (candidates.length === 0) return refusal('council.source.empty', 'candidates')
82
+
83
+ const ranked = rankCandidates(candidates)
84
+ if (!ranked.ok) return refusal(`council.rank.${tokenOf(ranked.code)}`, 'candidates')
85
+
86
+ const requestedLevels = isRecord(input) ? input.levels : undefined
87
+ const levels = requestedLevels === undefined ? COUNCIL_SCORE_LEVELS : requestedLevels
88
+ const validated = validateAsk({
89
+ mode: 'score',
90
+ id: 'p6.council',
91
+ text: QUESTION_TEXT,
92
+ levels,
93
+ })
94
+ if (!validated.ok) {
95
+ return refusal(`council.question.${tokenOf(validated.code)}`, 'candidates')
96
+ }
97
+ const question = validated.question as ScoreQuestion
98
+ const ids = ranked.ordered.map((candidate) => candidate.id)
99
+
100
+ const answer = isRecord(input) ? input.answer : undefined
101
+ if (answer === undefined) {
102
+ // Shadow has no responder. Recording the ranked order is the observation;
103
+ // no level is invented for the unanswered question.
104
+ return {
105
+ decision: { decision: 'rank', reason: 'council.no.answer' },
106
+ source: 'candidates',
107
+ ids,
108
+ question,
109
+ score: null,
110
+ }
111
+ }
112
+ const resolution = evaluateAskAnswer(question, answer)
113
+ if (resolution.status === 'score') {
114
+ return {
115
+ decision: { decision: 'score', reason: 'council.score.resolved' },
116
+ source: 'candidates',
117
+ ids,
118
+ question,
119
+ score: resolution.level,
120
+ }
121
+ }
122
+ return {
123
+ decision: {
124
+ decision: 'refusal',
125
+ reason: resolution.status === 'refusal' ? 'council.score.refused' : 'council.score.failed',
126
+ },
127
+ source: 'candidates',
128
+ ids,
129
+ question,
130
+ score: null,
131
+ }
132
+ }
133
+
134
+ /**
135
+ * Observe P6 in shadow.
136
+ *
137
+ * @param input `{ candidates, levels?, answer? }`. Candidates are never mutated.
138
+ * @param sink Optional shadow-record observer.
139
+ */
140
+ export function evaluateCouncilPoint(input: unknown, sink?: ShadowSink): CouncilPointResult {
141
+ let analysis: CouncilAnalysis = refusal('council.source.absent')
142
+ const record = observeShadow(
143
+ 'P6',
144
+ input,
145
+ (isolated) => {
146
+ analysis = analyze(isolated)
147
+ return analysis.decision
148
+ },
149
+ sink,
150
+ )
151
+
152
+ const contained = record.decision === 'unavailable'
153
+ return {
154
+ record,
155
+ councilSource: contained ? 'none' : analysis.source,
156
+ rankedIds: contained ? [] : analysis.ids,
157
+ question: contained ? null : analysis.question,
158
+ score: contained ? null : analysis.score,
159
+ reason: record.reason,
160
+ }
161
+ }