pantheon-opencode 1.6.2-beta.8 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +121 -3
- package/README.md +34 -60
- package/README.pt-BR.md +10 -12
- package/bin/pantheon-init.mjs +30 -32
- package/package.json +3 -3
- package/plugin.json +1 -1
- package/pyproject.toml +1 -1
- package/scripts/decision-router.mjs +41 -56
- package/scripts/install/decision-router-config.mjs +67 -1
- package/scripts/install/npx-cache-sweep.mjs +103 -0
- package/scripts/install/opencode.mjs +7 -7
- package/scripts/install/plugin.mjs +4 -3
- package/scripts/install/strings.mjs +4 -0
- package/scripts/release-gates.mjs +6 -82
- package/scripts/sync-tui.mjs +3 -3
- package/src/agents/aphrodite.md +1 -1
- package/src/agents/apollo.md +1 -0
- package/src/agents/athena.md +1 -0
- package/src/agents/demeter.md +1 -1
- package/src/agents/gaia.md +1 -0
- package/src/agents/hephaestus.md +1 -1
- package/src/agents/hermes.md +1 -1
- package/src/agents/iris.md +1 -1
- package/src/agents/mnemosyne.md +1 -0
- package/src/agents/nyx.md +1 -1
- package/src/agents/prometheus.md +1 -1
- package/src/agents/talos.md +1 -1
- package/src/agents/themis.md +1 -1
- package/src/agents/zeus.md +5 -2
- package/src/pantheon/jev-decision-tool.ts +366 -114
- package/src/pantheon/jev-decisions.ts +11 -13
- package/src/pantheon/jev-escalation.ts +639 -0
- package/src/pantheon/jev-quality-score.ts +432 -0
- package/src/pantheon/jev-risk-gate.ts +479 -0
- package/src/pantheon/local-decision-prompt.ts +12 -18
- package/src/pantheon/v2-decision-telemetry.ts +14 -4
- package/src/pantheon/v2-native-decisions.ts +1 -1
- package/src/plugin-v2.ts +201 -24
- package/src/plugins/tui/dist/server-v2.d.ts +19 -11
- package/src/plugins/tui/dist/server-v2.d.ts.map +1 -1
- package/src/plugins/tui/dist/server-v2.js +19 -789
- package/src/plugins/tui/dist/server-v2.js.map +1 -1
- package/src/plugins/tui/dist/tui-v2.d.ts +12 -331
- package/src/plugins/tui/dist/tui-v2.d.ts.map +1 -1
- package/src/plugins/tui/dist/tui-v2.js +257 -5370
- package/src/plugins/tui/dist/tui-v2.js.map +1 -1
- 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
- package/src/plugins/tui/src/index-v2.tsx +213 -86
- package/src/plugins/tui/src/server-v2.ts +18 -982
- package/src/plugins/tui/tsconfig.typecheck.json +1 -6
- package/src/plugins/tui/tsdown.config.ts +1 -11
- package/src/plugins/tui/dist/cost-data-CCZUs1aJ.js +0 -749
- package/src/plugins/tui/dist/cost-data-CCZUs1aJ.js.map +0 -1
- package/src/plugins/tui/dist/quota-adapters.d.ts +0 -89
- package/src/plugins/tui/dist/quota-adapters.d.ts.map +0 -1
- package/src/plugins/tui/dist/quota-adapters.js +0 -1142
- package/src/plugins/tui/dist/quota-adapters.js.map +0 -1
- package/src/plugins/tui/dist/quota-cli.d.ts +0 -17
- package/src/plugins/tui/dist/quota-cli.d.ts.map +0 -1
- package/src/plugins/tui/dist/quota-cli.js +0 -184
- package/src/plugins/tui/dist/quota-cli.js.map +0 -1
- package/src/plugins/tui/dist/quota-rpc-DMmXBhBe.js +0 -684
- package/src/plugins/tui/dist/quota-rpc-DMmXBhBe.js.map +0 -1
- package/src/plugins/tui/dist/usage-history-contract-nt1rnBPE.js +0 -118
- package/src/plugins/tui/dist/usage-history-contract-nt1rnBPE.js.map +0 -1
- package/src/plugins/tui/dist/usage-history-worker.js +0 -25
- package/src/plugins/tui/dist/usage-history-worker.js.map +0 -1
- package/src/plugins/tui/src/go-key-diagnostic-ui.ts +0 -100
- package/src/plugins/tui/src/go-key-diagnostic.ts +0 -125
- package/src/plugins/tui/src/quota-adapters.ts +0 -1598
- package/src/plugins/tui/src/quota-cli.ts +0 -237
- package/src/plugins/tui/src/quota-rpc.ts +0 -373
- package/src/plugins/tui/src/quota-v2-model.ts +0 -1032
- package/src/plugins/tui/src/quota-v2.tsx +0 -1238
- package/src/plugins/tui/src/usage-history-contract.ts +0 -217
- package/src/plugins/tui/src/usage-history-validation.ts +0 -104
- package/src/plugins/tui/src/usage-history-view.tsx +0 -236
- package/src/plugins/tui/src/usage-history-worker.ts +0 -25
- package/src/plugins/tui/src/usage-view-model.ts +0 -272
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* J1 risk-score advisory gate — shadow-only observation of write/execute tool
|
|
3
|
+
* calls through the native Jev route.
|
|
4
|
+
*
|
|
5
|
+
* When `PANTHEON_J1_RISK_SCORE` is enabled, the plugin's `execute.before` hook
|
|
6
|
+
* fires ONE bounded native Jev decision per write/execute tool call through
|
|
7
|
+
* {@link runGatedNativeJevCall} — the SAME gate chain `pantheon_jev_decide`
|
|
8
|
+
* runs (user-level config, egress consent, budget-ledger reservation, the host
|
|
9
|
+
* permission kill-switch on `decision.egress`) — and records the outcome as a
|
|
10
|
+
* shadow record into the decision telemetry sink. The record is the whole
|
|
11
|
+
* effect: this gate never denies, never asks, never narrows a permission, and
|
|
12
|
+
* never delays the tool call. The 'flag' verdict
|
|
13
|
+
* ({@link RISK_GATE_FLAG_VERDICT}) is reserved for a future active phase and
|
|
14
|
+
* is unreachable while the gate is shadow-only.
|
|
15
|
+
*
|
|
16
|
+
* ## Sanitization contract
|
|
17
|
+
*
|
|
18
|
+
* The decision request carries NO user content: no task text, no file paths,
|
|
19
|
+
* no tool input values. The state text is the exact bounded template over
|
|
20
|
+
* three fields — the tool name (accepted only as a bounded
|
|
21
|
+
* `[A-Za-z0-9._:-]` token; anything else is `INPUT_INVALID`), the scope class
|
|
22
|
+
* (`write` | `execute` | `read`), and the sha256 of the serialized tool input
|
|
23
|
+
* (an opaque, bounded correlation value). Hostile names — paths, traversal,
|
|
24
|
+
* shell metacharacters, injection prose, oversize strings — are rejected
|
|
25
|
+
* outright, so no content slot exists in the first place. This mirrors the
|
|
26
|
+
* redaction discipline of the decision telemetry: an allowlist of bounded
|
|
27
|
+
* shapes, never a scrub of known-bad patterns.
|
|
28
|
+
*
|
|
29
|
+
* ## Fail-open
|
|
30
|
+
*
|
|
31
|
+
* Every failure mode — flag off, config disabled, consent missing, route or
|
|
32
|
+
* ledger denied, permission denied, decide throwing, the 2s deadline — skips
|
|
33
|
+
* the observation and records one `skip-…` reason token. The hook registers
|
|
34
|
+
* the observer only when the flag is on AND the host runtime exposes the
|
|
35
|
+
* native Decisions API, so a flag-off process pays zero overhead and a
|
|
36
|
+
* capability-less host behaves exactly as before.
|
|
37
|
+
*
|
|
38
|
+
* ## No delay, no retry, no queue
|
|
39
|
+
*
|
|
40
|
+
* The hook fires the observer without awaiting it (fire-and-observe); the
|
|
41
|
+
* observer bounds its own wait at {@link J1_RISK_GATE_TIMEOUT_MS} with an
|
|
42
|
+
* AbortController, records `skip-timeout` at the deadline, and lets the
|
|
43
|
+
* abandoned gate chain settle its own ledger reservation in the background.
|
|
44
|
+
* Exactly one call is ever fired: no retry, no queue.
|
|
45
|
+
*
|
|
46
|
+
* @module jev-risk-gate
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
import { createHash } from 'node:crypto'
|
|
50
|
+
import {
|
|
51
|
+
type GatedNativeJevFailure,
|
|
52
|
+
type GatedNativeJevRuntime,
|
|
53
|
+
runGatedNativeJevCall,
|
|
54
|
+
} from './jev-decision-tool.ts'
|
|
55
|
+
import { consumeJevDecisionResult } from './jev-decisions.ts'
|
|
56
|
+
import { observeShadow, type ShadowSink } from './v2-decision-shadow.ts'
|
|
57
|
+
import { secretScanApplies } from './v2-hook-engine.ts'
|
|
58
|
+
import type { NativeDecisionRequest } from './v2-native-decisions.ts'
|
|
59
|
+
|
|
60
|
+
/** On/off switch read by the plugin wiring. Any other value means off. */
|
|
61
|
+
export const J1_RISK_GATE_FLAG_ENV = 'PANTHEON_J1_RISK_SCORE'
|
|
62
|
+
|
|
63
|
+
/** Telemetry point id for the risk-score gate (J-series: the Jev uplift). */
|
|
64
|
+
export const J1_RISK_GATE_POINT = 'J1'
|
|
65
|
+
|
|
66
|
+
/** Wall-clock bound for one observation. The hook never waits on it. */
|
|
67
|
+
export const J1_RISK_GATE_TIMEOUT_MS = 2_000
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Reserved verdict for a future active phase. NOT reachable: the gate is
|
|
71
|
+
* shadow-only in this release, {@link riskGateVerdict} never returns it, and
|
|
72
|
+
* nothing in the wiring consults it. Kept as a named constant so the active
|
|
73
|
+
* phase cannot invent a second name for the same concept.
|
|
74
|
+
*/
|
|
75
|
+
export const RISK_GATE_FLAG_VERDICT = 'flag' as const
|
|
76
|
+
|
|
77
|
+
export type RiskGateScope = 'write' | 'execute' | 'read'
|
|
78
|
+
|
|
79
|
+
/** Question names of the two prepared questions, matched on consumption. */
|
|
80
|
+
export const RISK_QUESTION_NAME = 'risk'
|
|
81
|
+
export const INTENT_QUESTION_NAME = 'intent'
|
|
82
|
+
|
|
83
|
+
/** Score levels offered to the host; within the 2..10 question bound. */
|
|
84
|
+
export const J1_RISK_SCORE_LEVEL_COUNT = 5
|
|
85
|
+
|
|
86
|
+
/** Bound for the composed state text (the template is ~220 chars). */
|
|
87
|
+
export const J1_RISK_GATE_STATE_MAX_LENGTH = 512
|
|
88
|
+
|
|
89
|
+
/** Predicate probability at or above which the intent counts as consistent. */
|
|
90
|
+
export const RISK_GATE_INTENT_THRESHOLD = 0.5
|
|
91
|
+
|
|
92
|
+
const FLAG_ON = new Set(['1', 'on', 'true'])
|
|
93
|
+
const TOOL_NAME_TOKEN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/
|
|
94
|
+
const SHA256_HEX = /^[0-9a-f]{64}$/
|
|
95
|
+
const SCOPES: ReadonlySet<string> = new Set(['write', 'execute', 'read'])
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Tools whose call can run commands or reach the network. A NAME allowlist:
|
|
99
|
+
* an unknown tool is `read` (never observed) — the opposite bias of the
|
|
100
|
+
* secret scan, which must scan unknown names because it blocks; J1 spends
|
|
101
|
+
* budget per observation, so it observes only what it can classify.
|
|
102
|
+
*/
|
|
103
|
+
const EXECUTE_TOOLS: ReadonlySet<string> = new Set([
|
|
104
|
+
'bash',
|
|
105
|
+
'shell',
|
|
106
|
+
'sh',
|
|
107
|
+
'zsh',
|
|
108
|
+
'fish',
|
|
109
|
+
'powershell',
|
|
110
|
+
'cmd',
|
|
111
|
+
'webfetch',
|
|
112
|
+
])
|
|
113
|
+
const WRITE_TOOLS: ReadonlySet<string> = new Set([
|
|
114
|
+
'write',
|
|
115
|
+
'edit',
|
|
116
|
+
'multiedit',
|
|
117
|
+
'notebookedit',
|
|
118
|
+
'patch',
|
|
119
|
+
'apply_patch',
|
|
120
|
+
'hashline_edit',
|
|
121
|
+
])
|
|
122
|
+
|
|
123
|
+
/** Whether the operator explicitly enabled the flag. Absence means off. */
|
|
124
|
+
export function j1RiskGateEnabledFromEnv(
|
|
125
|
+
env: Record<string, string | undefined> = process.env,
|
|
126
|
+
): boolean {
|
|
127
|
+
const raw = (env[J1_RISK_GATE_FLAG_ENV] ?? '').trim().toLowerCase()
|
|
128
|
+
return FLAG_ON.has(raw)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Classify a tool name into the request's scope class. Unknown or non-string
|
|
133
|
+
* names are `read` — never observed, never guessed into write/execute.
|
|
134
|
+
*/
|
|
135
|
+
export function classifyRiskGateScope(tool: unknown): RiskGateScope {
|
|
136
|
+
if (typeof tool !== 'string') return 'read'
|
|
137
|
+
const normalized = tool.trim().toLowerCase()
|
|
138
|
+
if (EXECUTE_TOOLS.has(normalized)) return 'execute'
|
|
139
|
+
if (WRITE_TOOLS.has(normalized)) return 'write'
|
|
140
|
+
return 'read'
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Whether one tool call is a J1 observation target: a write/execute scope AND
|
|
145
|
+
* the secret scan's own exfil-capable gate. The second condition reuses the
|
|
146
|
+
* scan's read-only denylist (`v2-hook-engine.secretScanApplies`) so a tool the
|
|
147
|
+
* scan never inspects can never be observed either — one list, two consumers.
|
|
148
|
+
*/
|
|
149
|
+
export function isRiskGateTarget(tool: unknown): tool is string {
|
|
150
|
+
if (!secretScanApplies(tool)) return false
|
|
151
|
+
const scope = classifyRiskGateScope(tool)
|
|
152
|
+
return scope === 'write' || scope === 'execute'
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* sha256 hex of the serialized tool input — an opaque correlation value, the
|
|
157
|
+
* only content-derived field that ever leaves this process. Unserializable
|
|
158
|
+
* inputs hash a stable marker; this never throws.
|
|
159
|
+
*/
|
|
160
|
+
export function hashToolInput(input: unknown): string {
|
|
161
|
+
let serialized: string
|
|
162
|
+
if (typeof input === 'string') {
|
|
163
|
+
serialized = input
|
|
164
|
+
} else {
|
|
165
|
+
try {
|
|
166
|
+
const json = JSON.stringify(input)
|
|
167
|
+
serialized = json === undefined ? String(input) : json
|
|
168
|
+
} catch {
|
|
169
|
+
serialized = 'unserializable-tool-input'
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return createHash('sha256').update(serialized, 'utf8').digest('hex')
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export type RiskGateRequestFailure = 'INPUT_INVALID'
|
|
176
|
+
|
|
177
|
+
export type RiskGatePreparedRequest =
|
|
178
|
+
| {
|
|
179
|
+
readonly ok: true
|
|
180
|
+
/** The request body; the caller attaches the resolved route's `model`. */
|
|
181
|
+
readonly request: Omit<NativeDecisionRequest, 'model'>
|
|
182
|
+
}
|
|
183
|
+
| { readonly ok: false; readonly reasonCode: RiskGateRequestFailure }
|
|
184
|
+
|
|
185
|
+
const RISK_LEVELS = Object.freeze(
|
|
186
|
+
Array.from({ length: J1_RISK_SCORE_LEVEL_COUNT }, (_, index) => {
|
|
187
|
+
const labels = ['routine', 'low', 'moderate', 'high', 'critical'] as const
|
|
188
|
+
return Object.freeze({ label: labels[index] ?? `level-${index}` })
|
|
189
|
+
}),
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
const RISK_INSTRUCTIONS =
|
|
193
|
+
'Rate how risky this single tool call is for the current task, judging only the ' +
|
|
194
|
+
'tool class: routine and reversible at the low end, destructive or exfiltrating ' +
|
|
195
|
+
'at the high end. Advisory only; nothing is enforced.'
|
|
196
|
+
const INTENT_INSTRUCTIONS =
|
|
197
|
+
'Is a call of this tool class plausibly consistent with continuing the current ' +
|
|
198
|
+
'task? Answer as a probability. Advisory only; nothing is enforced.'
|
|
199
|
+
|
|
200
|
+
const QUESTIONS = Object.freeze([
|
|
201
|
+
Object.freeze({
|
|
202
|
+
type: 'score' as const,
|
|
203
|
+
name: RISK_QUESTION_NAME,
|
|
204
|
+
instructions: RISK_INSTRUCTIONS,
|
|
205
|
+
levels: RISK_LEVELS,
|
|
206
|
+
}),
|
|
207
|
+
Object.freeze({
|
|
208
|
+
type: 'predicate' as const,
|
|
209
|
+
name: INTENT_QUESTION_NAME,
|
|
210
|
+
instructions: INTENT_INSTRUCTIONS,
|
|
211
|
+
}),
|
|
212
|
+
])
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Build the bounded decision request — the sanitization contract made
|
|
216
|
+
* structural. The state is the exact template over the three admitted fields;
|
|
217
|
+
* the tool name must already be a bounded token (paths, metacharacters and
|
|
218
|
+
* prose are `INPUT_INVALID`, never sanitized on the way in); the hash must be
|
|
219
|
+
* lowercase sha256 hex. Two questions only: one score (2–10 levels), one
|
|
220
|
+
* predicate. No file parts, no JSON parts, no extra fields.
|
|
221
|
+
*/
|
|
222
|
+
export function buildRiskGateRequest(input: {
|
|
223
|
+
readonly toolName: string
|
|
224
|
+
readonly toolInputHash: string
|
|
225
|
+
readonly scope: RiskGateScope
|
|
226
|
+
}): RiskGatePreparedRequest {
|
|
227
|
+
const toolName = input?.toolName
|
|
228
|
+
const toolInputHash = input?.toolInputHash
|
|
229
|
+
const scope = input?.scope
|
|
230
|
+
if (scope === undefined || typeof scope !== 'string' || !SCOPES.has(scope)) {
|
|
231
|
+
return { ok: false, reasonCode: 'INPUT_INVALID' }
|
|
232
|
+
}
|
|
233
|
+
if (typeof toolName !== 'string' || !TOOL_NAME_TOKEN.test(toolName)) {
|
|
234
|
+
return { ok: false, reasonCode: 'INPUT_INVALID' }
|
|
235
|
+
}
|
|
236
|
+
if (typeof toolInputHash !== 'string' || !SHA256_HEX.test(toolInputHash)) {
|
|
237
|
+
return { ok: false, reasonCode: 'INPUT_INVALID' }
|
|
238
|
+
}
|
|
239
|
+
const state =
|
|
240
|
+
'Advisory risk probe for one tool call. ' +
|
|
241
|
+
`tool_name: ${toolName}. scope: ${scope}. tool_input_sha256: ${toolInputHash}. ` +
|
|
242
|
+
'The request carries no file paths, no tool arguments and no task content; ' +
|
|
243
|
+
'judge from the tool class alone.'
|
|
244
|
+
if (state.length > J1_RISK_GATE_STATE_MAX_LENGTH) {
|
|
245
|
+
return { ok: false, reasonCode: 'INPUT_INVALID' }
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
ok: true,
|
|
249
|
+
request: Object.freeze({
|
|
250
|
+
input: Object.freeze([Object.freeze({ type: 'text' as const, text: state })]),
|
|
251
|
+
questions: QUESTIONS,
|
|
252
|
+
}),
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export interface RiskGateEvaluation {
|
|
257
|
+
/**
|
|
258
|
+
* The host's score for the `risk` question; `null` when the answer is
|
|
259
|
+
* missing, mistyped, duplicated or an error. An ordinal, not a probability.
|
|
260
|
+
*/
|
|
261
|
+
readonly riskScore: number | null
|
|
262
|
+
/**
|
|
263
|
+
* Whether the `intent` predicate holds (probability ≥ 0.5); `null` under the
|
|
264
|
+
* same failures.
|
|
265
|
+
*/
|
|
266
|
+
readonly intentConsistent: boolean | null
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Consume a host Decisions response against the prepared request.
|
|
271
|
+
*
|
|
272
|
+
* Validation is strict and borrowed wholesale from the tool's own consumer
|
|
273
|
+
* (`consumeJevDecisionResult`): an answer counts only when its name and type
|
|
274
|
+
* match the prepared questions, score answers carry a non-negative finite
|
|
275
|
+
* score, predicate answers a probability in [0, 1], and duplicates become
|
|
276
|
+
* errors. Anything else — and any unknown response shape — is `null`, never a
|
|
277
|
+
* guess. Extra fields on answers never cross into the evaluation.
|
|
278
|
+
*/
|
|
279
|
+
export function evaluateRiskGateDecision(
|
|
280
|
+
response: unknown,
|
|
281
|
+
request: NativeDecisionRequest,
|
|
282
|
+
): RiskGateEvaluation {
|
|
283
|
+
const advice = consumeJevDecisionResult(response, request)
|
|
284
|
+
let riskScore: number | null = null
|
|
285
|
+
let intentConsistent: boolean | null = null
|
|
286
|
+
for (const answer of advice.answers) {
|
|
287
|
+
if (answer.type === 'error') continue
|
|
288
|
+
if (answer.type === 'score' && answer.name === RISK_QUESTION_NAME) {
|
|
289
|
+
riskScore = answer.score ?? null
|
|
290
|
+
} else if (answer.type === 'predicate' && answer.name === INTENT_QUESTION_NAME) {
|
|
291
|
+
intentConsistent = (answer.probability ?? 0) >= RISK_GATE_INTENT_THRESHOLD
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return { riskScore, intentConsistent }
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export type RiskGateVerdict = 'observe' | 'flag'
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* The policy: in shadow mode the verdict is ALWAYS `observe` — for every risk
|
|
301
|
+
* score, for every intent answer, for nulls. Shadow never denies and never
|
|
302
|
+
* asks; a high score changes the record's reason, nothing else. The `flag`
|
|
303
|
+
* verdict ({@link RISK_GATE_FLAG_VERDICT}) is reserved for a future active
|
|
304
|
+
* phase and is unreachable here by construction: `shadow` admits only `true`
|
|
305
|
+
* and no branch below returns anything but `observe`.
|
|
306
|
+
*/
|
|
307
|
+
export function riskGateVerdict(input: {
|
|
308
|
+
readonly riskScore: number | null
|
|
309
|
+
readonly intentConsistent: boolean | null
|
|
310
|
+
readonly shadow: true
|
|
311
|
+
}): RiskGateVerdict {
|
|
312
|
+
void input // every evaluation is admitted while the gate is shadow-only
|
|
313
|
+
return 'observe'
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function formatScoreToken(value: number): string {
|
|
317
|
+
if (!Number.isFinite(value) || value < 0 || value > 1000) return 'na'
|
|
318
|
+
const text = String(Math.round(value * 100) / 100)
|
|
319
|
+
return TOOL_NAME_TOKEN.test(text) ? text : 'na'
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Encode the evaluation into ONE telemetry reason token (the sink's allowlist
|
|
324
|
+
* has no slot for structured fields, and that is its strength): e.g.
|
|
325
|
+
* `risk-3-intent-yes`, `risk-na-intent-na`. Always a bounded
|
|
326
|
+
* `[A-Za-z0-9._:-]` token; a score outside 0..1000 or not finitely
|
|
327
|
+
* representable encodes as `na`.
|
|
328
|
+
*/
|
|
329
|
+
export function encodeRiskGateReason(input: {
|
|
330
|
+
readonly riskScore: number | null
|
|
331
|
+
readonly intentConsistent: boolean | null
|
|
332
|
+
}): string {
|
|
333
|
+
const score = input.riskScore === null ? 'na' : formatScoreToken(input.riskScore)
|
|
334
|
+
const intent = input.intentConsistent === null ? 'na' : input.intentConsistent ? 'yes' : 'no'
|
|
335
|
+
return `risk-${score}-intent-${intent}`
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const SKIP_REASON_BY_GATE_FAILURE: Readonly<Record<GatedNativeJevFailure, string>> = Object.freeze({
|
|
339
|
+
CONTEXT_UNAVAILABLE: 'skip-context-unavailable',
|
|
340
|
+
CONFIG_UNAVAILABLE: 'skip-config-unavailable',
|
|
341
|
+
DISABLED: 'skip-config-disabled',
|
|
342
|
+
CONSENT_MISSING: 'skip-consent-missing',
|
|
343
|
+
CAP_OR_PERIOD_DENIED: 'skip-reservation-denied',
|
|
344
|
+
NO_ELIGIBLE_CANDIDATE: 'skip-route-unavailable',
|
|
345
|
+
PERMISSION_UNAVAILABLE: 'skip-permission-unavailable',
|
|
346
|
+
PERMISSION_DENIED: 'skip-permission-denied',
|
|
347
|
+
STATE_INVALID: 'skip-request-invalid',
|
|
348
|
+
QUESTIONS_INVALID: 'skip-request-invalid',
|
|
349
|
+
PRESET_INVALID: 'skip-request-invalid',
|
|
350
|
+
RESERVATION_DENIED: 'skip-reservation-denied',
|
|
351
|
+
DECISION_CALL_FAILED: 'skip-decision-call-failed',
|
|
352
|
+
USAGE_UNRECONCILED: 'skip-usage-unreconciled',
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
/** Map a gate failure to its telemetry reason token; unknown → fail-open. */
|
|
356
|
+
export function mapGateFailureToSkipReason(reasonCode: string): string {
|
|
357
|
+
return SKIP_REASON_BY_GATE_FAILURE[reasonCode as GatedNativeJevFailure] ?? 'skip-gate-unavailable'
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** The `execute.before` fields the observer reads; `input` is hashed, never read. */
|
|
361
|
+
interface J1ExecuteBeforeEvent {
|
|
362
|
+
tool?: unknown
|
|
363
|
+
sessionID?: unknown
|
|
364
|
+
messageID?: unknown
|
|
365
|
+
agent?: unknown
|
|
366
|
+
input?: unknown
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export interface J1RiskGateObserverOptions {
|
|
370
|
+
/** The same runtime the `pantheon_jev_decide` tool uses. */
|
|
371
|
+
readonly runtime: GatedNativeJevRuntime
|
|
372
|
+
/** Shadow-record sink (the plugin wraps it with the telemetry mode). */
|
|
373
|
+
readonly sink?: ShadowSink
|
|
374
|
+
readonly timeoutMs?: number
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function recordSkip(sink: ShadowSink | undefined, skipReason: string): void {
|
|
378
|
+
observeShadow(
|
|
379
|
+
J1_RISK_GATE_POINT,
|
|
380
|
+
skipReason,
|
|
381
|
+
() => ({
|
|
382
|
+
decision: 'unavailable',
|
|
383
|
+
reason: skipReason,
|
|
384
|
+
}),
|
|
385
|
+
sink,
|
|
386
|
+
)
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function abortDeadline(signal: AbortSignal): Promise<'timeout'> {
|
|
390
|
+
return new Promise((resolve) => {
|
|
391
|
+
if (signal.aborted) {
|
|
392
|
+
resolve('timeout')
|
|
393
|
+
return
|
|
394
|
+
}
|
|
395
|
+
signal.addEventListener('abort', () => resolve('timeout'), { once: true })
|
|
396
|
+
})
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Create the J1 observer: one async, self-bounding observation per
|
|
401
|
+
* `execute.before` event. The returned function NEVER throws and NEVER
|
|
402
|
+
* records more than one shadow record per event; a read-scope or malformed
|
|
403
|
+
* event records nothing at all.
|
|
404
|
+
*
|
|
405
|
+
* The gate chain runs unawaited races against the deadline; on timeout the
|
|
406
|
+
* observer records `skip-timeout` and abandons while the abandoned chain
|
|
407
|
+
* settles its own ledger reservation in the background (settlement of an
|
|
408
|
+
* already-fired call — not a retry, and it writes no record).
|
|
409
|
+
*/
|
|
410
|
+
export function createJ1RiskGateObserver(
|
|
411
|
+
options: J1RiskGateObserverOptions,
|
|
412
|
+
): (event: unknown) => Promise<void> {
|
|
413
|
+
const timeoutMs = options.timeoutMs ?? J1_RISK_GATE_TIMEOUT_MS
|
|
414
|
+
const sink = options.sink
|
|
415
|
+
return async (event: unknown): Promise<void> => {
|
|
416
|
+
try {
|
|
417
|
+
const payload = (event ?? {}) as J1ExecuteBeforeEvent
|
|
418
|
+
if (!isRiskGateTarget(payload.tool)) return
|
|
419
|
+
const tool = payload.tool
|
|
420
|
+
const scope = classifyRiskGateScope(tool)
|
|
421
|
+
const built = buildRiskGateRequest({
|
|
422
|
+
toolName: tool,
|
|
423
|
+
toolInputHash: hashToolInput(payload.input),
|
|
424
|
+
scope,
|
|
425
|
+
})
|
|
426
|
+
if (!built.ok) {
|
|
427
|
+
recordSkip(sink, 'skip-input-invalid')
|
|
428
|
+
return
|
|
429
|
+
}
|
|
430
|
+
const controller = new AbortController()
|
|
431
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs)
|
|
432
|
+
if (typeof timer === 'object' && typeof timer.unref === 'function') timer.unref()
|
|
433
|
+
try {
|
|
434
|
+
const outcome = await Promise.race([
|
|
435
|
+
runGatedNativeJevCall({
|
|
436
|
+
sessionID: payload.sessionID,
|
|
437
|
+
messageID: payload.messageID,
|
|
438
|
+
agent: typeof payload.agent === 'string' ? payload.agent : undefined,
|
|
439
|
+
runtime: options.runtime,
|
|
440
|
+
prepare: (route) => ({
|
|
441
|
+
ok: true as const,
|
|
442
|
+
request: {
|
|
443
|
+
model: route.model,
|
|
444
|
+
input: built.request.input,
|
|
445
|
+
questions: built.request.questions,
|
|
446
|
+
},
|
|
447
|
+
}),
|
|
448
|
+
}),
|
|
449
|
+
abortDeadline(controller.signal),
|
|
450
|
+
])
|
|
451
|
+
if (outcome === 'timeout') {
|
|
452
|
+
recordSkip(sink, 'skip-timeout')
|
|
453
|
+
return
|
|
454
|
+
}
|
|
455
|
+
if (!outcome.ok) {
|
|
456
|
+
recordSkip(sink, mapGateFailureToSkipReason(outcome.reasonCode))
|
|
457
|
+
return
|
|
458
|
+
}
|
|
459
|
+
const evaluation = evaluateRiskGateDecision(outcome.call.response, outcome.call.request)
|
|
460
|
+
observeShadow(
|
|
461
|
+
J1_RISK_GATE_POINT,
|
|
462
|
+
evaluation,
|
|
463
|
+
(isolated) => ({
|
|
464
|
+
decision: 'observe',
|
|
465
|
+
reason: encodeRiskGateReason(isolated as RiskGateEvaluation),
|
|
466
|
+
}),
|
|
467
|
+
sink,
|
|
468
|
+
)
|
|
469
|
+
} finally {
|
|
470
|
+
clearTimeout(timer)
|
|
471
|
+
}
|
|
472
|
+
} catch {
|
|
473
|
+
// Fail-open: the gate chain rejects only when the budget ledger cannot
|
|
474
|
+
// be opened; any other unexpected failure lands here too. One skip
|
|
475
|
+
// record, no detail, no propagation into the tool call.
|
|
476
|
+
recordSkip(sink, 'skip-gate-unavailable')
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { decideLocalForTurn, type TurnDecision } from './local-decision-turn.ts'
|
|
2
|
-
import {
|
|
2
|
+
import { isPantheonSkillId } from './pantheon-decision-catalog.ts'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
// The marker FORMAT stays pinned: prompts that still carry one (saved prompts,
|
|
5
|
+
// copied messages) short-circuit through `hasDecisionRouterMarker`.
|
|
5
6
|
const MARKER_PATTERN = /<!-- pantheon-decision-router:v1\b[^>]*-->/
|
|
6
7
|
|
|
7
8
|
/** The host sends the *string* `"undefined"` for absent prompt fields (F-08). */
|
|
@@ -29,6 +30,7 @@ export type PromptApplicationReason =
|
|
|
29
30
|
| 'skills.synthetic.narrowed'
|
|
30
31
|
|
|
31
32
|
export interface PromptApplicationResult {
|
|
33
|
+
/** Always false since beta.10: the visible marker is suppressed. */
|
|
32
34
|
markerApplied: boolean
|
|
33
35
|
skillsFiltered: boolean
|
|
34
36
|
/** Observable source of the skill list; `none` in the measured host shape (F-08). */
|
|
@@ -170,21 +172,18 @@ function narrowSkills(
|
|
|
170
172
|
return { skillsFiltered: true, skillsSource: 'prompt', reason: 'skills.synthetic.narrowed' }
|
|
171
173
|
}
|
|
172
174
|
|
|
173
|
-
function buildMarker(decision: TurnDecision): string | null {
|
|
174
|
-
const skillIds = decision.skillIds.filter((id) => isPantheonSkillId(id))
|
|
175
|
-
const agent = decision.agent !== null && isPantheonAgentId(decision.agent) ? decision.agent : null
|
|
176
|
-
if (skillIds.length === 0 && agent === null) return null
|
|
177
|
-
const parts = [`source=${decision.source}`]
|
|
178
|
-
if (skillIds.length > 0) parts.push(`skills=${skillIds.join(',')}`)
|
|
179
|
-
if (agent !== null) parts.push(`agent=${agent}`)
|
|
180
|
-
return `${MARKER_PREFIX} ${parts.join(' ')} -->`
|
|
181
|
-
}
|
|
182
|
-
|
|
183
175
|
/**
|
|
184
176
|
* Apply an advisory decision to the host prompt payload in place. It never
|
|
185
177
|
* dispatches, changes permissions, or removes a pre-existing host marker.
|
|
186
178
|
* `skillsDraft` is the per-turn draft list; it is observed for `skillsSource`
|
|
187
179
|
* and never applied to admission.
|
|
180
|
+
*
|
|
181
|
+
* The visible `<!-- pantheon-decision-router:v1 … -->` marker is SUPPRESSED
|
|
182
|
+
* (1.6.2-beta.10): the host chat rendered the HTML comment to the user. The
|
|
183
|
+
* router itself keeps running — shadow observation, telemetry and the
|
|
184
|
+
* synthetic-shape skill guard all stay — and measured evidence showed the
|
|
185
|
+
* marker was the only live prompt mutation while every measured prompt
|
|
186
|
+
* mutation was a no-op (F-24, F-08/F-09), so nothing behavioral is lost.
|
|
188
187
|
*/
|
|
189
188
|
export function applyTurnDecisionToPrompt(
|
|
190
189
|
prompt: unknown,
|
|
@@ -205,17 +204,12 @@ export function applyTurnDecisionToPrompt(
|
|
|
205
204
|
decision.skillIds.filter((id) => isPantheonSkillId(id)),
|
|
206
205
|
skillsDraft,
|
|
207
206
|
)
|
|
208
|
-
|
|
207
|
+
return {
|
|
209
208
|
markerApplied: false,
|
|
210
209
|
skillsFiltered: narrowing.skillsFiltered,
|
|
211
210
|
skillsSource: narrowing.skillsSource,
|
|
212
211
|
reason: narrowing.reason,
|
|
213
212
|
}
|
|
214
|
-
if (typeof target.text !== 'string' || target.text.includes(MARKER_PREFIX)) return result
|
|
215
|
-
const marker = buildMarker(decision)
|
|
216
|
-
if (marker === null) return result
|
|
217
|
-
target.text = `${target.text}\n\n${marker}`
|
|
218
|
-
return { ...result, markerApplied: true }
|
|
219
213
|
}
|
|
220
214
|
|
|
221
215
|
/** True when the prompt already carries a decision-router marker. */
|
|
@@ -37,9 +37,18 @@ export const TELEMETRY_FIELDS = [
|
|
|
37
37
|
export type TelemetryField = (typeof TELEMETRY_FIELDS)[number]
|
|
38
38
|
export type TelemetryMode = 'shadow' | 'active' | 'off' | 'migration'
|
|
39
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Point ids admitted into telemetry: the measured P-series registry points
|
|
42
|
+
* plus the J-series of the Jev uplift (J1 risk-score gate; J2 skill-selection
|
|
43
|
+
* scorecard; J3 turn-complexity escalation; J4 output-quality review; later J
|
|
44
|
+
* phases extend the series, never the P-series). The two series are disjoint
|
|
45
|
+
* on purpose — a J record is advisory-tool telemetry, not a registry point.
|
|
46
|
+
*/
|
|
47
|
+
export type TelemetryPointId = DecisionPointId | 'J1' | 'J2' | 'J3' | 'J4'
|
|
48
|
+
|
|
40
49
|
export interface DecisionTelemetryEvent {
|
|
41
50
|
readonly schemaVersion: typeof TELEMETRY_SCHEMA_VERSION
|
|
42
|
-
readonly point:
|
|
51
|
+
readonly point: TelemetryPointId
|
|
43
52
|
readonly decision: string
|
|
44
53
|
readonly effect: 'none'
|
|
45
54
|
readonly reason: string
|
|
@@ -63,7 +72,8 @@ export type TelemetryResult =
|
|
|
63
72
|
}
|
|
64
73
|
|
|
65
74
|
const TOKEN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/
|
|
66
|
-
|
|
75
|
+
/** Registry points (`P1`…`P11`) plus the Jev-uplift series (`J0`…`J99`). */
|
|
76
|
+
const POINT_ID = /^(?:P(?:[1-9]|1[01])|J[0-9]{1,2})$/
|
|
67
77
|
const TELEMETRY_KEYS: ReadonlySet<string> = new Set(TELEMETRY_FIELDS)
|
|
68
78
|
const MODES: ReadonlySet<string> = new Set(['shadow', 'active', 'off', 'migration'])
|
|
69
79
|
const DURATION_MAX_MS = 60_000
|
|
@@ -76,7 +86,7 @@ function isToken(value: unknown): value is string {
|
|
|
76
86
|
return typeof value === 'string' && TOKEN.test(value)
|
|
77
87
|
}
|
|
78
88
|
|
|
79
|
-
function isPointId(value: unknown): value is
|
|
89
|
+
function isPointId(value: unknown): value is TelemetryPointId {
|
|
80
90
|
return typeof value === 'string' && POINT_ID.test(value)
|
|
81
91
|
}
|
|
82
92
|
|
|
@@ -98,7 +108,7 @@ export function sanitizeTelemetry(input: unknown): TelemetryResult {
|
|
|
98
108
|
droppedFieldCount += 1
|
|
99
109
|
}
|
|
100
110
|
|
|
101
|
-
let point:
|
|
111
|
+
let point: TelemetryPointId | null = null
|
|
102
112
|
let decision: string | null = null
|
|
103
113
|
let reason: string | null = null
|
|
104
114
|
let mode: TelemetryMode | null = null
|
|
@@ -81,7 +81,7 @@ function readModelRef(value: unknown): NativeDecisionModelRef | null {
|
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
function findRouteKind(
|
|
84
|
+
export function findRouteKind(
|
|
85
85
|
model: NativeDecisionModelRef,
|
|
86
86
|
): Pick<ResolvedNativeDecisionRoute, 'kind' | 'billing'> {
|
|
87
87
|
const stock = NATIVE_STOCK_DECISION_ROUTES.find(
|