pantheon-opencode 1.6.2-beta.3 → 1.6.2-beta.5
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.
- package/CHANGELOG.md +42 -0
- package/README.md +209 -1
- package/bin/pantheon-init.mjs +22 -0
- package/package.json +9 -3
- package/plugin.json +1 -1
- package/pyproject.toml +1 -1
- package/scripts/decision-router-budget.mjs +781 -0
- package/scripts/decision-router-v2-budget.mjs +218 -0
- package/scripts/decision-router-v2-egress.mjs +272 -0
- package/scripts/decision-router.mjs +519 -0
- package/scripts/docs-check-changelog.mjs +5 -3
- package/scripts/install/decision-layer-preview.mjs +376 -0
- package/scripts/install/decision-router-config.mjs +579 -0
- package/scripts/install/interactive.mjs +76 -5
- package/scripts/install/opencode.mjs +12 -0
- package/scripts/install/state.mjs +5 -0
- package/scripts/install/strings.mjs +4 -0
- package/scripts/mcp_resources.py +11 -9
- package/scripts/release-notes.mjs +30 -7
- package/scripts/versioning.mjs +41 -5
- package/src/agents/zeus.md +12 -0
- package/src/mcp/eval_store.py +11 -4
- package/src/mcp/mcp_resources.py +11 -9
- package/src/mcp/memory_mcp.py +293 -150
- package/src/pantheon/local-decision-agents.ts +170 -0
- package/src/pantheon/local-decision-budget.ts +1043 -0
- package/src/pantheon/local-decision-egress-gate.ts +264 -0
- package/src/pantheon/local-decision-prompt.ts +268 -0
- package/src/pantheon/local-decision-provider-matrix.ts +92 -0
- package/src/pantheon/local-decision-provider-protocol.ts +335 -0
- package/src/pantheon/local-decision-provider.internal.ts +260 -0
- package/src/pantheon/local-decision-router.ts +225 -0
- package/src/pantheon/local-decision-skills.ts +281 -0
- package/src/pantheon/local-decision-tool.ts +160 -0
- package/src/pantheon/local-decision-turn.ts +338 -0
- package/src/pantheon/local-decision-usage-evidence.internal.ts +207 -0
- package/src/pantheon/local-orchestration.ts +322 -0
- package/src/pantheon/pantheon-decision-catalog.ts +101 -0
- package/src/pantheon/v2-decision-budget.ts +456 -0
- package/src/pantheon/v2-decision-contracts.ts +300 -0
- package/src/pantheon/v2-decision-egress.ts +203 -0
- package/src/pantheon/v2-decision-events.ts +180 -0
- package/src/pantheon/v2-decision-heuristic.ts +121 -0
- package/src/pantheon/v2-decision-points/p1-agent.ts +91 -0
- package/src/pantheon/v2-decision-points/p10-claims.ts +243 -0
- package/src/pantheon/v2-decision-points/p11-catalog.ts +220 -0
- package/src/pantheon/v2-decision-points/p2-skill.ts +83 -0
- package/src/pantheon/v2-decision-points/p3-complexity.ts +108 -0
- package/src/pantheon/v2-decision-points/p4-memory.ts +137 -0
- package/src/pantheon/v2-decision-points/p5-themis-verify.ts +190 -0
- package/src/pantheon/v2-decision-points/p6-council.ts +161 -0
- package/src/pantheon/v2-decision-points/p7-loop.ts +263 -0
- package/src/pantheon/v2-decision-points/p8-guardrail.ts +366 -0
- package/src/pantheon/v2-decision-points/p9-memory-compaction.ts +304 -0
- package/src/pantheon/v2-decision-points.ts +242 -0
- package/src/pantheon/v2-decision-policy.ts +166 -0
- package/src/pantheon/v2-decision-premises.ts +146 -0
- package/src/pantheon/v2-decision-prompt-surface.ts +115 -0
- package/src/pantheon/v2-decision-rank.ts +91 -0
- package/src/pantheon/v2-decision-registry.ts +391 -0
- package/src/pantheon/v2-decision-resource-ids.ts +127 -0
- package/src/pantheon/v2-decision-shadow.ts +123 -0
- package/src/pantheon/v2-decision-telemetry.ts +214 -0
- package/src/pantheon/v2-decision-verify.ts +51 -0
- package/src/pantheon/v2-tools.ts +35 -15
- package/src/plugin-v2.ts +357 -22
- package/src/plugin.ts +4 -0
- package/src/plugins/tui/dist/tui-v2.js +3 -3
- package/src/plugins/tui/dist/tui.js +1 -1
- package/src/plugins/tui/package-lock.json +2 -2
- package/src/plugins/tui/package.json +1 -1
|
@@ -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
|
+
}
|