pantheon-opencode 1.6.2-beta.4 → 1.6.2-beta.6
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 +27 -0
- package/README.md +202 -29
- package/README.pt-BR.md +9 -0
- package/bin/pantheon-init.mjs +6 -1
- package/package.json +2 -1
- package/plugin.json +1 -1
- package/pyproject.toml +1 -1
- package/scripts/decision-router-budget.mjs +7 -8
- package/scripts/decision-router-v2-budget.mjs +218 -0
- package/scripts/decision-router-v2-egress.mjs +272 -0
- package/scripts/decision-router.mjs +268 -46
- package/scripts/docs-check-changelog.mjs +5 -3
- package/scripts/install/decision-layer-preview.mjs +376 -0
- package/scripts/install/decision-router-config.mjs +162 -34
- package/scripts/mcp_resources.py +11 -9
- package/scripts/release-notes.mjs +30 -7
- package/scripts/versioning.mjs +41 -5
- 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/jev-decision-tool.ts +336 -0
- package/src/pantheon/jev-decisions.ts +625 -0
- package/src/pantheon/local-decision-budget.ts +4 -1
- package/src/pantheon/local-decision-egress-gate.ts +16 -136
- package/src/pantheon/local-decision-prompt.ts +184 -45
- package/src/pantheon/local-decision-provider-matrix.ts +14 -62
- package/src/pantheon/local-decision-skills.ts +115 -5
- package/src/pantheon/local-decision-tool.ts +35 -1
- package/src/pantheon/local-decision-turn.ts +15 -252
- package/src/pantheon/local-decision-usage-evidence.internal.ts +80 -119
- 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 +367 -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-native-decisions.ts +365 -0
- package/src/pantheon/v2-tools.ts +24 -2
- package/src/plugin-v2.ts +360 -14
- 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
- package/src/pantheon/local-decision-provider-protocol.ts +0 -335
- package/src/pantheon/local-decision-provider.internal.ts +0 -260
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* decision-layer-preview.mjs — a COMPUTED diff of the decision-layer target
|
|
3
|
+
* artifacts (PLAN §7 Fase 7 gate, PM-13, D7).
|
|
4
|
+
*
|
|
5
|
+
* Why this module exists at all: the installer's "Change plan" is **not** a
|
|
6
|
+
* preview. PROBE-B measured a second idempotent apply that announced
|
|
7
|
+
* `112 create` and created nothing, because the announcement is derived from
|
|
8
|
+
* what a writer was asked to do, not from what the target bytes are. Shipping a
|
|
9
|
+
* second announcement of the same kind would repeat the defect with a new
|
|
10
|
+
* label. So this module never reads a change plan and never reprints one:
|
|
11
|
+
*
|
|
12
|
+
* - it takes a content snapshot of each target artifact exactly as PROBE-B §4.2
|
|
13
|
+
* did (`lstat` + SHA-256 + size + mode);
|
|
14
|
+
* - it builds the candidate bytes with the SAME serializer the writer uses
|
|
15
|
+
* (`serializeUserDecisionRouterConfig`), so the compared bytes are the bytes
|
|
16
|
+
* that would land on disk;
|
|
17
|
+
* - the action is decided by comparing hashes, nothing else:
|
|
18
|
+
* `missing → create`, `same hash → unchanged`, `different hash → update`.
|
|
19
|
+
*
|
|
20
|
+
* Consequence, and the property a test pins: apply-then-preview reports
|
|
21
|
+
* `0 create · 0 update`, while the measured installer re-announced 112 creates
|
|
22
|
+
* on an apply that created nothing.
|
|
23
|
+
*
|
|
24
|
+
* Writes only happen in {@link applyDecisionLayerPlan}, after re-reading the
|
|
25
|
+
* current hash and refusing the whole plan if any artifact moved in between.
|
|
26
|
+
*
|
|
27
|
+
* Secret safety: the plan and its rendered form contain paths, actions, byte
|
|
28
|
+
* sizes, field NAMES, and the values of a fixed allowlist of activation flags
|
|
29
|
+
* (schema version, enabled, consent, selection mode/provider/model, candidate
|
|
30
|
+
* count). No free-form content, no file contents, no hashes are printed.
|
|
31
|
+
*/
|
|
32
|
+
import { createHash } from 'node:crypto'
|
|
33
|
+
import { existsSync, lstatSync, readFileSync } from 'node:fs'
|
|
34
|
+
import { join, resolve } from 'node:path'
|
|
35
|
+
import {
|
|
36
|
+
migrateUserDecisionRouterConfig,
|
|
37
|
+
projectDecisionRouterInstallState,
|
|
38
|
+
readUserDecisionRouterConfig,
|
|
39
|
+
resolveUserDecisionRouterConfigPath,
|
|
40
|
+
serializeUserDecisionRouterConfig,
|
|
41
|
+
updateUserDecisionRouterConfig,
|
|
42
|
+
writeUserDecisionRouterConfig,
|
|
43
|
+
} from './decision-router-config.mjs'
|
|
44
|
+
import { snapshot } from './plan.mjs'
|
|
45
|
+
import { assertNoSymlinkAncestors, writeFileAtomically } from './verify.mjs'
|
|
46
|
+
|
|
47
|
+
export const DECISION_LAYER_ACTIONS = Object.freeze({
|
|
48
|
+
CREATE: 'create',
|
|
49
|
+
UPDATE: 'update',
|
|
50
|
+
UNCHANGED: 'unchanged',
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
export const DECISION_LAYER_ROLES = Object.freeze({
|
|
54
|
+
USER_CONFIG: 'user-config',
|
|
55
|
+
INSTALL_STATE: 'install-state',
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Backups are credential-bearing. The installer's own log says a backup set
|
|
60
|
+
* contains API keys/tokens, and PROBE-B measured one backup directory
|
|
61
|
+
* accumulated per application. The decision-layer setup path creates none and
|
|
62
|
+
* says so; it never adds a silent backup copy.
|
|
63
|
+
*/
|
|
64
|
+
export const DECISION_LAYER_BACKUP_NOTICE =
|
|
65
|
+
'Backup notice: installer backup sets accumulate once per application and contain ' +
|
|
66
|
+
'credential-bearing files (API keys/tokens) by the installer log itself. This command ' +
|
|
67
|
+
'creates no backup and no copy; nothing extra is written or retained.'
|
|
68
|
+
|
|
69
|
+
/** Fields whose VALUES may be printed. They are activation flags, never secrets. */
|
|
70
|
+
const VISIBLE_FIELDS = Object.freeze([
|
|
71
|
+
'schema_version',
|
|
72
|
+
'enabled',
|
|
73
|
+
'egress_consent',
|
|
74
|
+
'selection.mode',
|
|
75
|
+
'selection.provider',
|
|
76
|
+
'selection.model',
|
|
77
|
+
])
|
|
78
|
+
|
|
79
|
+
function readRawJson(filePath) {
|
|
80
|
+
try {
|
|
81
|
+
assertNoSymlinkAncestors(filePath)
|
|
82
|
+
const stat = lstatSync(filePath)
|
|
83
|
+
if (!stat.isFile() || stat.isSymbolicLink()) return null
|
|
84
|
+
return JSON.parse(readFileSync(filePath, 'utf8'))
|
|
85
|
+
} catch {
|
|
86
|
+
return null
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function visibleValue(value) {
|
|
91
|
+
if (value === null || value === undefined) return null
|
|
92
|
+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
|
|
93
|
+
return value
|
|
94
|
+
}
|
|
95
|
+
return Array.isArray(value) ? value.length : typeof value
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function fieldValue(record, field) {
|
|
99
|
+
if (record === null || typeof record !== 'object') return undefined
|
|
100
|
+
let current = record
|
|
101
|
+
for (const part of field.split('.')) {
|
|
102
|
+
if (current === null || typeof current !== 'object') return undefined
|
|
103
|
+
current = current[part]
|
|
104
|
+
}
|
|
105
|
+
return current
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Field-level diff for the allowlisted activation flags only. */
|
|
109
|
+
function diffConfigFields(rawConfig, candidateConfig) {
|
|
110
|
+
const changes = []
|
|
111
|
+
for (const field of VISIBLE_FIELDS) {
|
|
112
|
+
const before = fieldValue(rawConfig, field)
|
|
113
|
+
const after = fieldValue(candidateConfig, field)
|
|
114
|
+
if (JSON.stringify(before) !== JSON.stringify(after)) {
|
|
115
|
+
changes.push({ field, before: visibleValue(before), after: visibleValue(after) })
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (rawConfig === null) return changes
|
|
119
|
+
const rawKeys = Object.keys(rawConfig).sort()
|
|
120
|
+
const candidateKeys = Object.keys(candidateConfig).sort()
|
|
121
|
+
for (const key of rawKeys) {
|
|
122
|
+
if (!candidateKeys.includes(key))
|
|
123
|
+
changes.push({ field: key, before: 'present', after: 'removed' })
|
|
124
|
+
}
|
|
125
|
+
for (const key of candidateKeys) {
|
|
126
|
+
if (!rawKeys.includes(key)) changes.push({ field: key, before: 'absent', after: 'present' })
|
|
127
|
+
}
|
|
128
|
+
if (
|
|
129
|
+
Array.isArray(rawConfig?.enabled_candidates) &&
|
|
130
|
+
rawConfig.enabled_candidates.length !== candidateConfig.enabled_candidates.length
|
|
131
|
+
) {
|
|
132
|
+
changes.push({
|
|
133
|
+
field: 'enabled_candidates.length',
|
|
134
|
+
before: rawConfig.enabled_candidates.length,
|
|
135
|
+
after: candidateConfig.enabled_candidates.length,
|
|
136
|
+
})
|
|
137
|
+
}
|
|
138
|
+
return changes
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function artifactDiff({ role, path, current, candidateBytes, changedFields }) {
|
|
142
|
+
const candidateSnapshot = {
|
|
143
|
+
sha256: snapshotBytesHash(candidateBytes),
|
|
144
|
+
size: Buffer.byteLength(candidateBytes),
|
|
145
|
+
}
|
|
146
|
+
let action
|
|
147
|
+
if (!current.exists) action = DECISION_LAYER_ACTIONS.CREATE
|
|
148
|
+
else if (current.sha256 === candidateSnapshot.sha256) action = DECISION_LAYER_ACTIONS.UNCHANGED
|
|
149
|
+
else action = DECISION_LAYER_ACTIONS.UPDATE
|
|
150
|
+
return {
|
|
151
|
+
role,
|
|
152
|
+
path,
|
|
153
|
+
action,
|
|
154
|
+
bytes: candidateSnapshot.size,
|
|
155
|
+
currentBytes: current.size,
|
|
156
|
+
// Internal comparison state: never rendered, never printed.
|
|
157
|
+
currentExists: current.exists,
|
|
158
|
+
currentHash: current.sha256,
|
|
159
|
+
candidateBytes,
|
|
160
|
+
expectedHash: candidateSnapshot.sha256,
|
|
161
|
+
changedFields: action === DECISION_LAYER_ACTIONS.UNCHANGED ? [] : changedFields,
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function snapshotBytesHash(bytes) {
|
|
166
|
+
return createHash('sha256').update(bytes).digest('hex')
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Resolve the install-state document for an explicit target, if it exists. */
|
|
170
|
+
export function resolveInstallStatePath(target) {
|
|
171
|
+
return join(resolve(target), '.pantheon', 'install-state.json')
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Build the plan. No writes, no directory creation, no timestamp reads.
|
|
176
|
+
*
|
|
177
|
+
* @param {{ env?: Record<string, string|undefined>, target?: string,
|
|
178
|
+
* selection?: { mode: 'local' } | { mode: 'auto' } |
|
|
179
|
+
* { mode: 'explicit', provider: string, model: string },
|
|
180
|
+
* acceptMigration?: boolean }} [options]
|
|
181
|
+
*/
|
|
182
|
+
export function buildDecisionLayerPlan(options = {}) {
|
|
183
|
+
const { env = process.env, target, selection, acceptMigration = false } = options
|
|
184
|
+
const configPath = resolveUserDecisionRouterConfigPath(env)
|
|
185
|
+
const currentConfig = readUserDecisionRouterConfig({ env })
|
|
186
|
+
const candidateConfig =
|
|
187
|
+
selection === undefined
|
|
188
|
+
? currentConfig
|
|
189
|
+
: updateUserDecisionRouterConfig(currentConfig, { selection })
|
|
190
|
+
const candidateBytes = serializeUserDecisionRouterConfig(candidateConfig)
|
|
191
|
+
const rawConfig = readRawJson(configPath)
|
|
192
|
+
|
|
193
|
+
const artifacts = [
|
|
194
|
+
artifactDiff({
|
|
195
|
+
role: DECISION_LAYER_ROLES.USER_CONFIG,
|
|
196
|
+
path: configPath,
|
|
197
|
+
current: snapshot(configPath),
|
|
198
|
+
candidateBytes,
|
|
199
|
+
changedFields: diffConfigFields(rawConfig, candidateConfig),
|
|
200
|
+
}),
|
|
201
|
+
]
|
|
202
|
+
|
|
203
|
+
const warnings = [DECISION_LAYER_BACKUP_NOTICE]
|
|
204
|
+
const blocked = []
|
|
205
|
+
|
|
206
|
+
// A legacy file being normalized to the canonical schema must never turn the
|
|
207
|
+
// turn router on as a side effect of a preview/apply. The file can be
|
|
208
|
+
// rewritten, but enabling requires its own explicit flag and its own review.
|
|
209
|
+
const migrationEnablesTurnRouter =
|
|
210
|
+
rawConfig !== null &&
|
|
211
|
+
rawConfig.schema_version !== migrateUserDecisionRouterConfig(candidateConfig).schema_version &&
|
|
212
|
+
rawConfig.enabled !== true &&
|
|
213
|
+
migrateUserDecisionRouterConfig(candidateConfig).enabled === true
|
|
214
|
+
if (migrationEnablesTurnRouter && !acceptMigration) {
|
|
215
|
+
blocked.push({
|
|
216
|
+
role: DECISION_LAYER_ROLES.USER_CONFIG,
|
|
217
|
+
path: configPath,
|
|
218
|
+
reason: 'MIGRATION_WOULD_ENABLE_TURN_ROUTER',
|
|
219
|
+
detail:
|
|
220
|
+
'Writing the canonical schema would set enabled: true from a legacy file. ' +
|
|
221
|
+
'Re-run with --accept-migration to accept that activation explicitly.',
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (target !== undefined) {
|
|
226
|
+
const statePath = resolveInstallStatePath(target)
|
|
227
|
+
if (existsSync(statePath)) {
|
|
228
|
+
const rawState = readRawJson(statePath)
|
|
229
|
+
if (rawState && rawState.decision_router !== undefined) {
|
|
230
|
+
const projection = projectDecisionRouterInstallState(candidateConfig)
|
|
231
|
+
const candidateState = { ...rawState, decision_router: projection }
|
|
232
|
+
const stateBytes = `${JSON.stringify(candidateState, null, 2)}\n`
|
|
233
|
+
const current = diffInstallStateRecord(rawState.decision_router, projection)
|
|
234
|
+
artifacts.push(
|
|
235
|
+
artifactDiff({
|
|
236
|
+
role: DECISION_LAYER_ROLES.INSTALL_STATE,
|
|
237
|
+
path: statePath,
|
|
238
|
+
current: snapshot(statePath),
|
|
239
|
+
candidateBytes: stateBytes,
|
|
240
|
+
changedFields: current,
|
|
241
|
+
}),
|
|
242
|
+
)
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
warnings.push(
|
|
246
|
+
`No install-state at ${statePath}: nothing to reconcile. The decision-layer setup ` +
|
|
247
|
+
'never fabricates install state for an unmanaged target.',
|
|
248
|
+
)
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const totals = {
|
|
253
|
+
create: artifacts.filter((a) => a.action === DECISION_LAYER_ACTIONS.CREATE).length,
|
|
254
|
+
update: artifacts.filter((a) => a.action === DECISION_LAYER_ACTIONS.UPDATE).length,
|
|
255
|
+
unchanged: artifacts.filter((a) => a.action === DECISION_LAYER_ACTIONS.UNCHANGED).length,
|
|
256
|
+
}
|
|
257
|
+
return { artifacts, totals, warnings, blocked, wrote: false }
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function diffInstallStateRecord(installStateRecord, projection) {
|
|
261
|
+
const changes = []
|
|
262
|
+
for (const field of ['provider', 'egress_consent']) {
|
|
263
|
+
const before = installStateRecord?.[field]
|
|
264
|
+
const after = projection[field]
|
|
265
|
+
if (JSON.stringify(before) !== JSON.stringify(after)) {
|
|
266
|
+
changes.push({
|
|
267
|
+
field: `decision_router.${field}`,
|
|
268
|
+
before: visibleValue(before),
|
|
269
|
+
after: visibleValue(after),
|
|
270
|
+
})
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return changes
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** Secret-safe rendering. Paths, actions, sizes, field names and flag values only. */
|
|
277
|
+
export function formatDecisionLayerPlan(plan, { scope = 'user', applyPending = false } = {}) {
|
|
278
|
+
const lines = [`Decision-layer preview — ${scope} (computed artifact diff, not a change plan)`]
|
|
279
|
+
for (const artifact of plan.artifacts) {
|
|
280
|
+
lines.push(` ${artifact.action.padEnd(9)} ${artifact.path} (${artifact.bytes} B)`)
|
|
281
|
+
for (const change of artifact.changedFields) {
|
|
282
|
+
const before = change.before === null ? 'absent' : String(change.before)
|
|
283
|
+
const after = change.after === null ? 'absent' : String(change.after)
|
|
284
|
+
lines.push(` ${change.field}: ${before} -> ${after}`)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
lines.push(
|
|
288
|
+
` ${plan.totals.create} create · ${plan.totals.update} update · ${plan.totals.unchanged} unchanged`,
|
|
289
|
+
)
|
|
290
|
+
for (const warning of plan.warnings) lines.push(` ${warning}`)
|
|
291
|
+
for (const entry of plan.blocked) {
|
|
292
|
+
lines.push(` BLOCKED ${entry.path}: ${entry.reason} — ${entry.detail}`)
|
|
293
|
+
}
|
|
294
|
+
lines.push(
|
|
295
|
+
applyPending
|
|
296
|
+
? ' Writing the artifacts above (explicit --apply). No backup copy is created.'
|
|
297
|
+
: ' No files were written.',
|
|
298
|
+
)
|
|
299
|
+
return lines.join('\n')
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Machine-readable form. Same facts as the text renderer, no contents. */
|
|
303
|
+
export function toDecisionLayerPlanJson(plan) {
|
|
304
|
+
return {
|
|
305
|
+
kind: 'decision-layer-preview',
|
|
306
|
+
preview: true,
|
|
307
|
+
wrote: false,
|
|
308
|
+
totals: plan.totals,
|
|
309
|
+
artifacts: plan.artifacts.map((artifact) => ({
|
|
310
|
+
role: artifact.role,
|
|
311
|
+
path: artifact.path,
|
|
312
|
+
action: artifact.action,
|
|
313
|
+
bytes: artifact.bytes,
|
|
314
|
+
changed_fields: artifact.changedFields.map((change) => ({
|
|
315
|
+
field: change.field,
|
|
316
|
+
before: change.before,
|
|
317
|
+
after: change.after,
|
|
318
|
+
})),
|
|
319
|
+
})),
|
|
320
|
+
blocked: plan.blocked.map((entry) => ({
|
|
321
|
+
role: entry.role,
|
|
322
|
+
path: entry.path,
|
|
323
|
+
reason: entry.reason,
|
|
324
|
+
})),
|
|
325
|
+
warnings: plan.warnings,
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Write exactly the artifacts the plan described.
|
|
331
|
+
*
|
|
332
|
+
* Every artifact is re-snapshotted first: if any hash or existence differs from
|
|
333
|
+
* the reviewed plan, the whole apply is refused before the first write, so a
|
|
334
|
+
* preview that raced an edit can never be applied. Unchanged artifacts are not
|
|
335
|
+
* written at all (no mtime churn, no backup copy).
|
|
336
|
+
*
|
|
337
|
+
* @returns {{ written: string[], skipped: string[] }}
|
|
338
|
+
*/
|
|
339
|
+
export function applyDecisionLayerPlan(plan, { env = process.env } = {}) {
|
|
340
|
+
if (plan.blocked.length > 0) {
|
|
341
|
+
throw new Error(
|
|
342
|
+
`Refusing to apply a blocked plan: ${plan.blocked.map((entry) => entry.reason).join(', ')}`,
|
|
343
|
+
)
|
|
344
|
+
}
|
|
345
|
+
for (const artifact of plan.artifacts) {
|
|
346
|
+
const live = snapshot(artifact.path)
|
|
347
|
+
if (live.exists !== artifact.currentExists) {
|
|
348
|
+
throw new Error(`Artifact changed since the preview: ${artifact.path}. Re-run the preview.`)
|
|
349
|
+
}
|
|
350
|
+
if (live.sha256 !== artifact.currentHash) {
|
|
351
|
+
throw new Error(`Artifact changed since the preview: ${artifact.path}. Re-run the preview.`)
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const written = []
|
|
355
|
+
const skipped = []
|
|
356
|
+
for (const artifact of plan.artifacts) {
|
|
357
|
+
if (artifact.action === DECISION_LAYER_ACTIONS.UNCHANGED) {
|
|
358
|
+
skipped.push(artifact.path)
|
|
359
|
+
continue
|
|
360
|
+
}
|
|
361
|
+
if (artifact.role === DECISION_LAYER_ROLES.USER_CONFIG) {
|
|
362
|
+
// Reuse the writer: path validation, ownership checks, 0700 directory and
|
|
363
|
+
// 0600 file mode are already centralized there, and it serializes with the
|
|
364
|
+
// same function this module diffed.
|
|
365
|
+
if (resolveUserDecisionRouterConfigPath(env) !== artifact.path) {
|
|
366
|
+
throw new Error('Decision-layer plan targets a different config path than the environment')
|
|
367
|
+
}
|
|
368
|
+
writeUserDecisionRouterConfig(JSON.parse(artifact.candidateBytes), { env })
|
|
369
|
+
} else {
|
|
370
|
+
assertNoSymlinkAncestors(artifact.path)
|
|
371
|
+
writeFileAtomically(artifact.path, artifact.candidateBytes)
|
|
372
|
+
}
|
|
373
|
+
written.push(artifact.path)
|
|
374
|
+
}
|
|
375
|
+
return { written, skipped }
|
|
376
|
+
}
|
|
@@ -7,8 +7,10 @@ import { assertNoSymlinkAncestors, writeFileAtomically } from './verify.mjs'
|
|
|
7
7
|
export const DECISION_ROUTER_CONFIG_SCHEMA_VERSION = 1
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
* Legacy installer-state providers. This file is target
|
|
11
|
-
* runtime reads the user-level configuration below, never
|
|
10
|
+
* Legacy installer-state providers, in prompt order. This file is target
|
|
11
|
+
* metadata only: the runtime reads the user-level configuration below, never
|
|
12
|
+
* install-state. The order is positional (the interactive prompt indexes it), so
|
|
13
|
+
* ids are appended only through {@link ACCEPTED_INSTALL_STATE_PROVIDERS}.
|
|
12
14
|
*/
|
|
13
15
|
export const DECISION_ROUTER_PROVIDERS = Object.freeze(['local', 'openai-decisions', 'jev', 'auto'])
|
|
14
16
|
|
|
@@ -70,8 +72,14 @@ const LEGACY_USER_CONFIG_KEYS = [
|
|
|
70
72
|
'selection',
|
|
71
73
|
]
|
|
72
74
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Accepted install-state provider ids. `typesafe-jev` is the canonical form of
|
|
77
|
+
* the legacy `jev` alias and is written by `projectDecisionRouterInstallState`;
|
|
78
|
+
* accepting it is what lets a projected record survive a read/write round trip
|
|
79
|
+
* instead of resetting to Local (D7). The prompt list above is unchanged, so no
|
|
80
|
+
* new install-time option appears.
|
|
81
|
+
*/
|
|
82
|
+
const ACCEPTED_INSTALL_STATE_PROVIDERS = new Set([...DECISION_ROUTER_PROVIDERS, 'typesafe-jev'])
|
|
75
83
|
|
|
76
84
|
/** Canonicalize the legacy `jev` spelling once, at every user-config boundary. */
|
|
77
85
|
export function normalizeDecisionProviderId(provider) {
|
|
@@ -92,7 +100,7 @@ export function migrateDecisionRouterConfig(value) {
|
|
|
92
100
|
typeof value !== 'object' ||
|
|
93
101
|
Array.isArray(value) ||
|
|
94
102
|
value.schema_version !== DECISION_ROUTER_CONFIG_SCHEMA_VERSION ||
|
|
95
|
-
!
|
|
103
|
+
!ACCEPTED_INSTALL_STATE_PROVIDERS.has(value.provider) ||
|
|
96
104
|
typeof value.egress_consent !== 'boolean'
|
|
97
105
|
) {
|
|
98
106
|
return { ...DEFAULT_DECISION_ROUTER_CONFIG }
|
|
@@ -116,29 +124,11 @@ export function createDecisionRouterConfig(provider, egressConsent = false) {
|
|
|
116
124
|
|
|
117
125
|
/**
|
|
118
126
|
* Resolve a preference without probing the environment or credentials.
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
* @returns {'local'|'openai-decisions'|'jev'|'typesafe-jev'}
|
|
127
|
+
* (The legacy `resolveDecisionRouterProvider` auto resolver was removed with
|
|
128
|
+
* the direct-fetch provider path; runtime selection uses
|
|
129
|
+
* `resolveUserDecisionRouterCandidate` / `explainUserDecisionRouterAutoSelection`
|
|
130
|
+
* and the native route resolver.)
|
|
124
131
|
*/
|
|
125
|
-
export function resolveDecisionRouterProvider(
|
|
126
|
-
config,
|
|
127
|
-
{ authorizedProviders = [], budgetConfigured = false } = {},
|
|
128
|
-
) {
|
|
129
|
-
const preference = migrateDecisionRouterConfig(config)
|
|
130
|
-
if (!preference.egress_consent || budgetConfigured !== true) return 'local'
|
|
131
|
-
if (preference.provider === 'local') return 'local'
|
|
132
|
-
|
|
133
|
-
if (preference.provider === 'auto') {
|
|
134
|
-
const candidates = [
|
|
135
|
-
...new Set(authorizedProviders.filter((provider) => externalProviderSet.has(provider))),
|
|
136
|
-
]
|
|
137
|
-
return candidates.length === 1 ? candidates[0] : 'local'
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return externalProviderSet.has(preference.provider) ? preference.provider : 'local'
|
|
141
|
-
}
|
|
142
132
|
|
|
143
133
|
/**
|
|
144
134
|
* Resolve Pantheon's private non-secret user configuration path.
|
|
@@ -231,6 +221,18 @@ export function updateUserDecisionRouterConfig(config, update) {
|
|
|
231
221
|
throw new TypeError('Invalid enabled provider/model candidates')
|
|
232
222
|
}
|
|
233
223
|
const next = { ...current, ...update }
|
|
224
|
+
// Selecting a different provider/model target is not consent for it (D9):
|
|
225
|
+
// `select` records a preference and can only ever leave egress OFF. A
|
|
226
|
+
// previously recorded consent is revoked when the target moves, unless the
|
|
227
|
+
// caller passes `egress_consent` in the same explicit update (an admin
|
|
228
|
+
// surface that names both fields at once).
|
|
229
|
+
if (
|
|
230
|
+
Object.hasOwn(update, 'selection') &&
|
|
231
|
+
!Object.hasOwn(update, 'egress_consent') &&
|
|
232
|
+
!sameSelection(normalizeSelection(update.selection), current.selection)
|
|
233
|
+
) {
|
|
234
|
+
next.egress_consent = false
|
|
235
|
+
}
|
|
234
236
|
const normalized = migrateUserDecisionRouterConfig(next)
|
|
235
237
|
if (normalized.selection.mode === 'local' && update.egress_consent === true) {
|
|
236
238
|
throw new TypeError('Local selection cannot enable egress consent')
|
|
@@ -243,23 +245,71 @@ export function isUserDecisionRouterEnabled(config) {
|
|
|
243
245
|
return migrateUserDecisionRouterConfig(config).enabled
|
|
244
246
|
}
|
|
245
247
|
|
|
246
|
-
|
|
247
|
-
export function resolveUserDecisionRouterCandidate(config, capValidCandidates = []) {
|
|
248
|
-
const preference = migrateUserDecisionRouterConfig(config)
|
|
249
|
-
if (!preference.enabled || !preference.egress_consent) return null
|
|
248
|
+
function eligibleAutoCandidates(preference, capValidCandidates) {
|
|
250
249
|
const validKeys = new Set(
|
|
251
250
|
capValidCandidates
|
|
252
251
|
.filter(isKnownCandidate)
|
|
253
252
|
.map(({ provider, model }) => candidateKey(normalizeDecisionProviderId(provider), model)),
|
|
254
253
|
)
|
|
255
|
-
|
|
254
|
+
return preference.enabled_candidates.filter(
|
|
256
255
|
(candidate) =>
|
|
257
256
|
remotelyEligibleProviders.has(candidate.provider) &&
|
|
258
257
|
validKeys.has(candidateKey(candidate.provider, candidate.model)),
|
|
259
258
|
)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Explain an Auto selection with an explicit cardinality branch (D5).
|
|
263
|
+
*
|
|
264
|
+
* The defect this closes: 0 and 2+ eligible candidates both collapsed into a
|
|
265
|
+
* single `null`, so a caller could not tell "nothing is enabled" from "several
|
|
266
|
+
* are enabled and I refuse to guess". The three outcomes below are distinct
|
|
267
|
+
* values with distinct reason codes, and every refusal names the state that
|
|
268
|
+
* caused it.
|
|
269
|
+
*
|
|
270
|
+
* @param {unknown} config
|
|
271
|
+
* @param {ReadonlyArray<{ provider: string, model: string }>} [capValidCandidates]
|
|
272
|
+
* @returns {{ outcome: 'ineligible', reason: 'DISABLED'|'CONSENT_MISSING'|'NOT_AUTO' }
|
|
273
|
+
* | { outcome: 'none', reason: 'NO_ELIGIBLE_CANDIDATE', count: 0 }
|
|
274
|
+
* | { outcome: 'single', reason: 'SINGLE_ELIGIBLE_CANDIDATE', count: 1,
|
|
275
|
+
* candidate: { provider: string, model: string } }
|
|
276
|
+
* | { outcome: 'ambiguous', reason: 'MULTIPLE_ELIGIBLE_CANDIDATES', count: number }}
|
|
277
|
+
*/
|
|
278
|
+
export function explainUserDecisionRouterAutoSelection(config, capValidCandidates = []) {
|
|
279
|
+
const preference = migrateUserDecisionRouterConfig(config)
|
|
280
|
+
// Preconditions are reported as ineligible, never as a cardinality outcome:
|
|
281
|
+
// a disabled router with two candidates is disabled, not ambiguous.
|
|
282
|
+
if (!preference.enabled) return { outcome: 'ineligible', reason: 'DISABLED' }
|
|
283
|
+
if (!preference.egress_consent) return { outcome: 'ineligible', reason: 'CONSENT_MISSING' }
|
|
284
|
+
if (preference.selection.mode !== 'auto') return { outcome: 'ineligible', reason: 'NOT_AUTO' }
|
|
285
|
+
|
|
286
|
+
const eligible = eligibleAutoCandidates(preference, capValidCandidates)
|
|
287
|
+
if (eligible.length === 0) {
|
|
288
|
+
return { outcome: 'none', reason: 'NO_ELIGIBLE_CANDIDATE', count: 0 }
|
|
289
|
+
}
|
|
290
|
+
if (eligible.length > 1) {
|
|
291
|
+
return {
|
|
292
|
+
outcome: 'ambiguous',
|
|
293
|
+
reason: 'MULTIPLE_ELIGIBLE_CANDIDATES',
|
|
294
|
+
count: eligible.length,
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
outcome: 'single',
|
|
299
|
+
reason: 'SINGLE_ELIGIBLE_CANDIDATE',
|
|
300
|
+
count: 1,
|
|
301
|
+
candidate: eligible[0],
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Auto resolves only one enabled, cap-valid direct provider/model tuple. */
|
|
306
|
+
export function resolveUserDecisionRouterCandidate(config, capValidCandidates = []) {
|
|
307
|
+
const preference = migrateUserDecisionRouterConfig(config)
|
|
308
|
+
if (!preference.enabled || !preference.egress_consent) return null
|
|
260
309
|
if (preference.selection.mode === 'local') return null
|
|
261
310
|
if (preference.selection.mode === 'explicit') {
|
|
262
311
|
const selected = preference.selection
|
|
312
|
+
const enabled = eligibleAutoCandidates(preference, capValidCandidates)
|
|
263
313
|
return (
|
|
264
314
|
enabled.find(
|
|
265
315
|
(candidate) =>
|
|
@@ -268,7 +318,8 @@ export function resolveUserDecisionRouterCandidate(config, capValidCandidates =
|
|
|
268
318
|
) ?? null
|
|
269
319
|
)
|
|
270
320
|
}
|
|
271
|
-
|
|
321
|
+
const explained = explainUserDecisionRouterAutoSelection(preference, capValidCandidates)
|
|
322
|
+
return explained.outcome === 'single' ? explained.candidate : null
|
|
272
323
|
}
|
|
273
324
|
|
|
274
325
|
/** Read only a private user-level JSON file. Invalid contents fail closed. */
|
|
@@ -306,11 +357,88 @@ export function writeUserDecisionRouterConfig(config, { env = process.env } = {}
|
|
|
306
357
|
} catch (error) {
|
|
307
358
|
if (error?.code !== 'ENOENT') throw error
|
|
308
359
|
}
|
|
309
|
-
writeFileAtomically(filePath,
|
|
360
|
+
writeFileAtomically(filePath, serializeUserDecisionRouterConfig(normalized))
|
|
310
361
|
chmodSync(filePath, 0o600)
|
|
311
362
|
return filePath
|
|
312
363
|
}
|
|
313
364
|
|
|
365
|
+
/**
|
|
366
|
+
* The only serializer for the user-level config.
|
|
367
|
+
*
|
|
368
|
+
* A preview is honest only when the bytes it compares are the bytes the writer
|
|
369
|
+
* would produce, so the preview and the writer share this function. It is
|
|
370
|
+
* deliberately free of clock and environment reads.
|
|
371
|
+
*
|
|
372
|
+
* @param {unknown} config
|
|
373
|
+
* @returns {string}
|
|
374
|
+
*/
|
|
375
|
+
export function serializeUserDecisionRouterConfig(config) {
|
|
376
|
+
return `${JSON.stringify(migrateUserDecisionRouterConfig(config), null, 2)}\n`
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Project the user-level configuration onto the only install-state record that
|
|
381
|
+
* may exist (D7).
|
|
382
|
+
*
|
|
383
|
+
* Install-state records a target preference; the runtime reads the user-level
|
|
384
|
+
* file. Deriving the record from that file is what keeps the two surfaces from
|
|
385
|
+
* disagreeing: there is no second place where a provider or a consent decision
|
|
386
|
+
* can be typed in. The projection carries the user's own consent boolean, so it
|
|
387
|
+
* can never assert consent the runtime configuration does not have.
|
|
388
|
+
*
|
|
389
|
+
* @param {unknown} userConfig
|
|
390
|
+
* @returns {{ schema_version: 1, provider: string, egress_consent: boolean }}
|
|
391
|
+
*/
|
|
392
|
+
export function projectDecisionRouterInstallState(userConfig) {
|
|
393
|
+
const preference = migrateUserDecisionRouterConfig(userConfig)
|
|
394
|
+
const provider =
|
|
395
|
+
preference.selection.mode === 'explicit'
|
|
396
|
+
? normalizeDecisionProviderId(preference.selection.provider)
|
|
397
|
+
: preference.selection.mode
|
|
398
|
+
return migrateDecisionRouterConfig({
|
|
399
|
+
schema_version: DECISION_ROUTER_CONFIG_SCHEMA_VERSION,
|
|
400
|
+
provider,
|
|
401
|
+
egress_consent: preference.egress_consent,
|
|
402
|
+
})
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Field-level divergence between install-state and the user-level config (D7).
|
|
407
|
+
* Returns field NAMES and both states, never free-form content; callers print a
|
|
408
|
+
* reconciliation instruction instead of rewriting the record silently.
|
|
409
|
+
*
|
|
410
|
+
* @param {unknown} userConfig
|
|
411
|
+
* @param {unknown} installStateRecord
|
|
412
|
+
* @returns {{ expected: object, actual: object, differences: Array<{ field: string, installState: unknown, userConfig: unknown }> }}
|
|
413
|
+
*/
|
|
414
|
+
export function diffDecisionRouterSurfaces(userConfig, installStateRecord) {
|
|
415
|
+
const expected = projectDecisionRouterInstallState(userConfig)
|
|
416
|
+
const actual = migrateDecisionRouterConfig(installStateRecord)
|
|
417
|
+
const differences = []
|
|
418
|
+
if (actual.provider !== expected.provider) {
|
|
419
|
+
differences.push({
|
|
420
|
+
field: 'provider',
|
|
421
|
+
installState: actual.provider,
|
|
422
|
+
userConfig: expected.provider,
|
|
423
|
+
})
|
|
424
|
+
}
|
|
425
|
+
if (actual.egress_consent !== expected.egress_consent) {
|
|
426
|
+
differences.push({
|
|
427
|
+
field: 'egress_consent',
|
|
428
|
+
installState: actual.egress_consent,
|
|
429
|
+
userConfig: expected.egress_consent,
|
|
430
|
+
})
|
|
431
|
+
}
|
|
432
|
+
return { expected, actual, differences }
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function sameSelection(left, right) {
|
|
436
|
+
if (left === null || right === null) return left === right
|
|
437
|
+
if (left.mode !== right.mode) return false
|
|
438
|
+
if (left.mode !== 'explicit') return true
|
|
439
|
+
return left.provider === right.provider && left.model === right.model
|
|
440
|
+
}
|
|
441
|
+
|
|
314
442
|
function normalizeSelection(value) {
|
|
315
443
|
if (!isPlainObject(value) || typeof value.mode !== 'string') return null
|
|
316
444
|
if (value.mode === 'local' || value.mode === 'auto') {
|