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,579 @@
|
|
|
1
|
+
import { chmodSync, lstatSync, mkdirSync, readFileSync } from 'node:fs'
|
|
2
|
+
import { homedir } from 'node:os'
|
|
3
|
+
import { dirname, isAbsolute, join, parse, resolve, sep } from 'node:path'
|
|
4
|
+
import { assertNoSymlinkAncestors, writeFileAtomically } from './verify.mjs'
|
|
5
|
+
|
|
6
|
+
/** Versioned, non-secret installer preference for the advisory decision router. */
|
|
7
|
+
export const DECISION_ROUTER_CONFIG_SCHEMA_VERSION = 1
|
|
8
|
+
|
|
9
|
+
/**
|
|
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}.
|
|
14
|
+
*/
|
|
15
|
+
export const DECISION_ROUTER_PROVIDERS = Object.freeze(['local', 'openai-decisions', 'jev', 'auto'])
|
|
16
|
+
|
|
17
|
+
export const DEFAULT_DECISION_ROUTER_CONFIG = Object.freeze({
|
|
18
|
+
schema_version: DECISION_ROUTER_CONFIG_SCHEMA_VERSION,
|
|
19
|
+
provider: 'local',
|
|
20
|
+
egress_consent: false,
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Canonical user-level provider/model matrix. The provider identifiers are the
|
|
25
|
+
* same ones used by the runtime selector and by the private provider adapter;
|
|
26
|
+
* `decision-provider-budget.ts` maps them onto the ledger's accounting names.
|
|
27
|
+
*/
|
|
28
|
+
export const DECISION_ROUTER_MODELS = Object.freeze({
|
|
29
|
+
'openai-decisions': Object.freeze(['gpt-6-luna']),
|
|
30
|
+
'typesafe-jev': Object.freeze(['jev-1.14.0']),
|
|
31
|
+
'opencode-console-jev': Object.freeze(['jev-1.13']),
|
|
32
|
+
'opencode-console-jev-free': Object.freeze(['jev-1.13-free']),
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
/** Single mapping from a user-level provider ID to the local budget provider. */
|
|
36
|
+
export const DECISION_PROVIDER_BUDGET_MAPPING = Object.freeze({
|
|
37
|
+
'openai-decisions': 'openai-decisions',
|
|
38
|
+
'typesafe-jev': 'typesafe-jev',
|
|
39
|
+
'opencode-console-jev': 'opencode-console',
|
|
40
|
+
'opencode-console-jev-free': 'jev-free',
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
/** Providers whose usage and price are documented well enough to reconcile. */
|
|
44
|
+
export const REMOTELY_ELIGIBLE_DECISION_PROVIDERS = Object.freeze([
|
|
45
|
+
'openai-decisions',
|
|
46
|
+
'typesafe-jev',
|
|
47
|
+
])
|
|
48
|
+
|
|
49
|
+
export const USER_DECISION_ROUTER_CONFIG_SCHEMA_VERSION = 2
|
|
50
|
+
export const DEFAULT_USER_DECISION_ROUTER_CONFIG = Object.freeze({
|
|
51
|
+
schema_version: USER_DECISION_ROUTER_CONFIG_SCHEMA_VERSION,
|
|
52
|
+
enabled: false,
|
|
53
|
+
selection: Object.freeze({ mode: 'local' }),
|
|
54
|
+
egress_consent: false,
|
|
55
|
+
enabled_candidates: Object.freeze([]),
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
const LEGACY_PROVIDER_ALIASES = Object.freeze({ jev: 'typesafe-jev' })
|
|
59
|
+
const userCandidateProviders = new Set(Object.keys(DECISION_ROUTER_MODELS))
|
|
60
|
+
const remotelyEligibleProviders = new Set(REMOTELY_ELIGIBLE_DECISION_PROVIDERS)
|
|
61
|
+
const USER_CONFIG_KEYS = [
|
|
62
|
+
'egress_consent',
|
|
63
|
+
'enabled',
|
|
64
|
+
'enabled_candidates',
|
|
65
|
+
'schema_version',
|
|
66
|
+
'selection',
|
|
67
|
+
]
|
|
68
|
+
const LEGACY_USER_CONFIG_KEYS = [
|
|
69
|
+
'egress_consent',
|
|
70
|
+
'enabled_candidates',
|
|
71
|
+
'schema_version',
|
|
72
|
+
'selection',
|
|
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'])
|
|
83
|
+
const externalProviderSet = new Set(['openai-decisions', 'jev', 'typesafe-jev'])
|
|
84
|
+
|
|
85
|
+
/** Canonicalize the legacy `jev` spelling once, at every user-config boundary. */
|
|
86
|
+
export function normalizeDecisionProviderId(provider) {
|
|
87
|
+
if (typeof provider !== 'string') return provider
|
|
88
|
+
return LEGACY_PROVIDER_ALIASES[provider] ?? provider
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Migrate/validate persisted preference data. Missing or invalid legacy data is
|
|
93
|
+
* fail-closed: Local, no egress consent. Credentials are neither accepted nor
|
|
94
|
+
* inspected here.
|
|
95
|
+
* @param {unknown} value
|
|
96
|
+
* @returns {{ schema_version: 1, provider: string, egress_consent: boolean }}
|
|
97
|
+
*/
|
|
98
|
+
export function migrateDecisionRouterConfig(value) {
|
|
99
|
+
if (
|
|
100
|
+
!value ||
|
|
101
|
+
typeof value !== 'object' ||
|
|
102
|
+
Array.isArray(value) ||
|
|
103
|
+
value.schema_version !== DECISION_ROUTER_CONFIG_SCHEMA_VERSION ||
|
|
104
|
+
!ACCEPTED_INSTALL_STATE_PROVIDERS.has(value.provider) ||
|
|
105
|
+
typeof value.egress_consent !== 'boolean'
|
|
106
|
+
) {
|
|
107
|
+
return { ...DEFAULT_DECISION_ROUTER_CONFIG }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
schema_version: DECISION_ROUTER_CONFIG_SCHEMA_VERSION,
|
|
112
|
+
provider: value.provider,
|
|
113
|
+
egress_consent: value.provider === 'local' ? false : value.egress_consent,
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Build the only preference shape the installer persists. */
|
|
118
|
+
export function createDecisionRouterConfig(provider, egressConsent = false) {
|
|
119
|
+
return migrateDecisionRouterConfig({
|
|
120
|
+
schema_version: DECISION_ROUTER_CONFIG_SCHEMA_VERSION,
|
|
121
|
+
provider,
|
|
122
|
+
egress_consent: provider === 'local' ? false : egressConsent === true,
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Resolve a preference without probing the environment or credentials.
|
|
128
|
+
* Auto can choose only one separately authorized provider; zero or multiple
|
|
129
|
+
* candidates, missing consent, or a missing explicit budget all stay Local.
|
|
130
|
+
* @param {{ schema_version?: number, provider?: string, egress_consent?: boolean }} config
|
|
131
|
+
* @param {{ authorizedProviders?: string[], budgetConfigured?: boolean }} [options]
|
|
132
|
+
* @returns {'local'|'openai-decisions'|'jev'|'typesafe-jev'}
|
|
133
|
+
*/
|
|
134
|
+
export function resolveDecisionRouterProvider(
|
|
135
|
+
config,
|
|
136
|
+
{ authorizedProviders = [], budgetConfigured = false } = {},
|
|
137
|
+
) {
|
|
138
|
+
const preference = migrateDecisionRouterConfig(config)
|
|
139
|
+
if (!preference.egress_consent || budgetConfigured !== true) return 'local'
|
|
140
|
+
if (preference.provider === 'local') return 'local'
|
|
141
|
+
|
|
142
|
+
if (preference.provider === 'auto') {
|
|
143
|
+
const candidates = [
|
|
144
|
+
...new Set(authorizedProviders.filter((provider) => externalProviderSet.has(provider))),
|
|
145
|
+
]
|
|
146
|
+
return candidates.length === 1 ? candidates[0] : 'local'
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return externalProviderSet.has(preference.provider) ? preference.provider : 'local'
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Resolve Pantheon's private non-secret user configuration path.
|
|
154
|
+
* @param {Readonly<Record<string, string | undefined>>} [env]
|
|
155
|
+
*/
|
|
156
|
+
export function resolveUserDecisionRouterConfigPath(env = process.env) {
|
|
157
|
+
const xdg = env.XDG_CONFIG_HOME
|
|
158
|
+
const home = env.HOME && isAbsolute(env.HOME) ? env.HOME : homedir()
|
|
159
|
+
const root = xdg && isAbsolute(xdg) ? xdg : join(home, '.config')
|
|
160
|
+
return resolve(root, 'pantheon', 'decision-router.json')
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Migrate invalid or missing user data to a local-only, no-consent config. */
|
|
164
|
+
export function migrateUserDecisionRouterConfig(value) {
|
|
165
|
+
if (!isPlainObject(value)) return cloneDefaultUserConfig()
|
|
166
|
+
if (value.schema_version === 1) return migrateLegacyUserConfig(value)
|
|
167
|
+
if (value.schema_version !== USER_DECISION_ROUTER_CONFIG_SCHEMA_VERSION) {
|
|
168
|
+
return cloneDefaultUserConfig()
|
|
169
|
+
}
|
|
170
|
+
if (
|
|
171
|
+
!hasExactKeys(value, USER_CONFIG_KEYS) ||
|
|
172
|
+
typeof value.egress_consent !== 'boolean' ||
|
|
173
|
+
typeof value.enabled !== 'boolean' ||
|
|
174
|
+
!Array.isArray(value.enabled_candidates)
|
|
175
|
+
) {
|
|
176
|
+
return cloneDefaultUserConfig()
|
|
177
|
+
}
|
|
178
|
+
const selection = normalizeSelection(value.selection)
|
|
179
|
+
const candidates = normalizeEnabledCandidates(value.enabled_candidates)
|
|
180
|
+
if (selection === null || candidates === null) return cloneDefaultUserConfig()
|
|
181
|
+
return {
|
|
182
|
+
schema_version: USER_DECISION_ROUTER_CONFIG_SCHEMA_VERSION,
|
|
183
|
+
enabled: value.enabled,
|
|
184
|
+
selection,
|
|
185
|
+
egress_consent: selection.mode === 'local' ? false : value.egress_consent,
|
|
186
|
+
enabled_candidates: candidates,
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Schema v1 predated the explicit `enabled` switch. Preserve intent only when
|
|
192
|
+
* the user had selected a provider, consented, and enabled at least one
|
|
193
|
+
* candidate; every other legacy shape migrates to disabled.
|
|
194
|
+
*/
|
|
195
|
+
function migrateLegacyUserConfig(value) {
|
|
196
|
+
if (
|
|
197
|
+
!hasExactKeys(value, LEGACY_USER_CONFIG_KEYS) ||
|
|
198
|
+
typeof value.egress_consent !== 'boolean' ||
|
|
199
|
+
!Array.isArray(value.enabled_candidates)
|
|
200
|
+
) {
|
|
201
|
+
return cloneDefaultUserConfig()
|
|
202
|
+
}
|
|
203
|
+
const selection = normalizeSelection(value.selection)
|
|
204
|
+
const candidates = normalizeEnabledCandidates(value.enabled_candidates)
|
|
205
|
+
if (selection === null || candidates === null) return cloneDefaultUserConfig()
|
|
206
|
+
const egressConsent = selection.mode === 'local' ? false : value.egress_consent
|
|
207
|
+
return {
|
|
208
|
+
schema_version: USER_DECISION_ROUTER_CONFIG_SCHEMA_VERSION,
|
|
209
|
+
enabled: selection.mode !== 'local' && egressConsent && candidates.length > 0,
|
|
210
|
+
selection,
|
|
211
|
+
egress_consent: egressConsent,
|
|
212
|
+
enabled_candidates: candidates,
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Return a validated immutable update, without implying consent or enablement. */
|
|
217
|
+
export function updateUserDecisionRouterConfig(config, update) {
|
|
218
|
+
const current = migrateUserDecisionRouterConfig(config)
|
|
219
|
+
if (!isPlainObject(update)) throw new TypeError('Invalid decision-router update')
|
|
220
|
+
if (
|
|
221
|
+
Object.keys(update).some(
|
|
222
|
+
(key) => !['enabled', 'selection', 'egress_consent', 'enabled_candidates'].includes(key),
|
|
223
|
+
)
|
|
224
|
+
) {
|
|
225
|
+
throw new TypeError('Unsupported decision-router configuration field')
|
|
226
|
+
}
|
|
227
|
+
if (Object.hasOwn(update, 'selection') && normalizeSelection(update.selection) === null) {
|
|
228
|
+
throw new TypeError('Invalid decision-router selection')
|
|
229
|
+
}
|
|
230
|
+
if (Object.hasOwn(update, 'enabled') && typeof update.enabled !== 'boolean') {
|
|
231
|
+
throw new TypeError('Enabled must be explicitly true or false')
|
|
232
|
+
}
|
|
233
|
+
if (Object.hasOwn(update, 'egress_consent') && typeof update.egress_consent !== 'boolean') {
|
|
234
|
+
throw new TypeError('Egress consent must be explicitly true or false')
|
|
235
|
+
}
|
|
236
|
+
if (
|
|
237
|
+
Object.hasOwn(update, 'enabled_candidates') &&
|
|
238
|
+
normalizeEnabledCandidates(update.enabled_candidates) === null
|
|
239
|
+
) {
|
|
240
|
+
throw new TypeError('Invalid enabled provider/model candidates')
|
|
241
|
+
}
|
|
242
|
+
const next = { ...current, ...update }
|
|
243
|
+
// Selecting a different provider/model target is not consent for it (D9):
|
|
244
|
+
// `select` records a preference and can only ever leave egress OFF. A
|
|
245
|
+
// previously recorded consent is revoked when the target moves, unless the
|
|
246
|
+
// caller passes `egress_consent` in the same explicit update (an admin
|
|
247
|
+
// surface that names both fields at once).
|
|
248
|
+
if (
|
|
249
|
+
Object.hasOwn(update, 'selection') &&
|
|
250
|
+
!Object.hasOwn(update, 'egress_consent') &&
|
|
251
|
+
!sameSelection(normalizeSelection(update.selection), current.selection)
|
|
252
|
+
) {
|
|
253
|
+
next.egress_consent = false
|
|
254
|
+
}
|
|
255
|
+
const normalized = migrateUserDecisionRouterConfig(next)
|
|
256
|
+
if (normalized.selection.mode === 'local' && update.egress_consent === true) {
|
|
257
|
+
throw new TypeError('Local selection cannot enable egress consent')
|
|
258
|
+
}
|
|
259
|
+
return normalized
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** True only when the user explicitly enabled turn selection. */
|
|
263
|
+
export function isUserDecisionRouterEnabled(config) {
|
|
264
|
+
return migrateUserDecisionRouterConfig(config).enabled
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function eligibleAutoCandidates(preference, capValidCandidates) {
|
|
268
|
+
const validKeys = new Set(
|
|
269
|
+
capValidCandidates
|
|
270
|
+
.filter(isKnownCandidate)
|
|
271
|
+
.map(({ provider, model }) => candidateKey(normalizeDecisionProviderId(provider), model)),
|
|
272
|
+
)
|
|
273
|
+
return preference.enabled_candidates.filter(
|
|
274
|
+
(candidate) =>
|
|
275
|
+
remotelyEligibleProviders.has(candidate.provider) &&
|
|
276
|
+
validKeys.has(candidateKey(candidate.provider, candidate.model)),
|
|
277
|
+
)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Explain an Auto selection with an explicit cardinality branch (D5).
|
|
282
|
+
*
|
|
283
|
+
* The defect this closes: 0 and 2+ eligible candidates both collapsed into a
|
|
284
|
+
* single `null`, so a caller could not tell "nothing is enabled" from "several
|
|
285
|
+
* are enabled and I refuse to guess". The three outcomes below are distinct
|
|
286
|
+
* values with distinct reason codes, and every refusal names the state that
|
|
287
|
+
* caused it.
|
|
288
|
+
*
|
|
289
|
+
* @param {unknown} config
|
|
290
|
+
* @param {ReadonlyArray<{ provider: string, model: string }>} [capValidCandidates]
|
|
291
|
+
* @returns {{ outcome: 'ineligible', reason: 'DISABLED'|'CONSENT_MISSING'|'NOT_AUTO' }
|
|
292
|
+
* | { outcome: 'none', reason: 'NO_ELIGIBLE_CANDIDATE', count: 0 }
|
|
293
|
+
* | { outcome: 'single', reason: 'SINGLE_ELIGIBLE_CANDIDATE', count: 1,
|
|
294
|
+
* candidate: { provider: string, model: string } }
|
|
295
|
+
* | { outcome: 'ambiguous', reason: 'MULTIPLE_ELIGIBLE_CANDIDATES', count: number }}
|
|
296
|
+
*/
|
|
297
|
+
export function explainUserDecisionRouterAutoSelection(config, capValidCandidates = []) {
|
|
298
|
+
const preference = migrateUserDecisionRouterConfig(config)
|
|
299
|
+
// Preconditions are reported as ineligible, never as a cardinality outcome:
|
|
300
|
+
// a disabled router with two candidates is disabled, not ambiguous.
|
|
301
|
+
if (!preference.enabled) return { outcome: 'ineligible', reason: 'DISABLED' }
|
|
302
|
+
if (!preference.egress_consent) return { outcome: 'ineligible', reason: 'CONSENT_MISSING' }
|
|
303
|
+
if (preference.selection.mode !== 'auto') return { outcome: 'ineligible', reason: 'NOT_AUTO' }
|
|
304
|
+
|
|
305
|
+
const eligible = eligibleAutoCandidates(preference, capValidCandidates)
|
|
306
|
+
if (eligible.length === 0) {
|
|
307
|
+
return { outcome: 'none', reason: 'NO_ELIGIBLE_CANDIDATE', count: 0 }
|
|
308
|
+
}
|
|
309
|
+
if (eligible.length > 1) {
|
|
310
|
+
return {
|
|
311
|
+
outcome: 'ambiguous',
|
|
312
|
+
reason: 'MULTIPLE_ELIGIBLE_CANDIDATES',
|
|
313
|
+
count: eligible.length,
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
outcome: 'single',
|
|
318
|
+
reason: 'SINGLE_ELIGIBLE_CANDIDATE',
|
|
319
|
+
count: 1,
|
|
320
|
+
candidate: eligible[0],
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** Auto resolves only one enabled, cap-valid direct provider/model tuple. */
|
|
325
|
+
export function resolveUserDecisionRouterCandidate(config, capValidCandidates = []) {
|
|
326
|
+
const preference = migrateUserDecisionRouterConfig(config)
|
|
327
|
+
if (!preference.enabled || !preference.egress_consent) return null
|
|
328
|
+
if (preference.selection.mode === 'local') return null
|
|
329
|
+
if (preference.selection.mode === 'explicit') {
|
|
330
|
+
const selected = preference.selection
|
|
331
|
+
const enabled = eligibleAutoCandidates(preference, capValidCandidates)
|
|
332
|
+
return (
|
|
333
|
+
enabled.find(
|
|
334
|
+
(candidate) =>
|
|
335
|
+
candidateKey(candidate.provider, candidate.model) ===
|
|
336
|
+
candidateKey(selected.provider, selected.model),
|
|
337
|
+
) ?? null
|
|
338
|
+
)
|
|
339
|
+
}
|
|
340
|
+
const explained = explainUserDecisionRouterAutoSelection(preference, capValidCandidates)
|
|
341
|
+
return explained.outcome === 'single' ? explained.candidate : null
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** Read only a private user-level JSON file. Invalid contents fail closed. */
|
|
345
|
+
export function readUserDecisionRouterConfig({ env = process.env } = {}) {
|
|
346
|
+
const filePath = resolveUserDecisionRouterConfigPath(env)
|
|
347
|
+
try {
|
|
348
|
+
assertNoSymlinkAncestors(filePath)
|
|
349
|
+
assertTrustedDirectoryChain(dirname(filePath))
|
|
350
|
+
const stat = lstatSync(filePath)
|
|
351
|
+
assertPrivatePath(stat, true)
|
|
352
|
+
const parsed = JSON.parse(readFileSync(filePath, 'utf8'))
|
|
353
|
+
return migrateUserDecisionRouterConfig(parsed)
|
|
354
|
+
} catch (error) {
|
|
355
|
+
if (error?.code === 'ENOENT') return cloneDefaultUserConfig()
|
|
356
|
+
throw error
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** Persist only non-secret preference data in a user-owned 0600 file. */
|
|
361
|
+
export function writeUserDecisionRouterConfig(config, { env = process.env } = {}) {
|
|
362
|
+
const normalized = migrateUserDecisionRouterConfig(config)
|
|
363
|
+
if (JSON.stringify(normalized) !== JSON.stringify(config)) {
|
|
364
|
+
throw new TypeError('Refusing to persist invalid decision-router configuration')
|
|
365
|
+
}
|
|
366
|
+
const filePath = resolveUserDecisionRouterConfigPath(env)
|
|
367
|
+
const directory = dirname(filePath)
|
|
368
|
+
assertNoSymlinkAncestors(directory, { includeLeaf: false })
|
|
369
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 })
|
|
370
|
+
assertNoSymlinkAncestors(directory)
|
|
371
|
+
assertTrustedDirectoryChain(directory)
|
|
372
|
+
assertPrivateDirectory(directory)
|
|
373
|
+
try {
|
|
374
|
+
const existing = lstatSync(filePath)
|
|
375
|
+
assertPrivatePath(existing, true)
|
|
376
|
+
} catch (error) {
|
|
377
|
+
if (error?.code !== 'ENOENT') throw error
|
|
378
|
+
}
|
|
379
|
+
writeFileAtomically(filePath, serializeUserDecisionRouterConfig(normalized))
|
|
380
|
+
chmodSync(filePath, 0o600)
|
|
381
|
+
return filePath
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* The only serializer for the user-level config.
|
|
386
|
+
*
|
|
387
|
+
* A preview is honest only when the bytes it compares are the bytes the writer
|
|
388
|
+
* would produce, so the preview and the writer share this function. It is
|
|
389
|
+
* deliberately free of clock and environment reads.
|
|
390
|
+
*
|
|
391
|
+
* @param {unknown} config
|
|
392
|
+
* @returns {string}
|
|
393
|
+
*/
|
|
394
|
+
export function serializeUserDecisionRouterConfig(config) {
|
|
395
|
+
return `${JSON.stringify(migrateUserDecisionRouterConfig(config), null, 2)}\n`
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Project the user-level configuration onto the only install-state record that
|
|
400
|
+
* may exist (D7).
|
|
401
|
+
*
|
|
402
|
+
* Install-state records a target preference; the runtime reads the user-level
|
|
403
|
+
* file. Deriving the record from that file is what keeps the two surfaces from
|
|
404
|
+
* disagreeing: there is no second place where a provider or a consent decision
|
|
405
|
+
* can be typed in. The projection carries the user's own consent boolean, so it
|
|
406
|
+
* can never assert consent the runtime configuration does not have.
|
|
407
|
+
*
|
|
408
|
+
* @param {unknown} userConfig
|
|
409
|
+
* @returns {{ schema_version: 1, provider: string, egress_consent: boolean }}
|
|
410
|
+
*/
|
|
411
|
+
export function projectDecisionRouterInstallState(userConfig) {
|
|
412
|
+
const preference = migrateUserDecisionRouterConfig(userConfig)
|
|
413
|
+
const provider =
|
|
414
|
+
preference.selection.mode === 'explicit'
|
|
415
|
+
? normalizeDecisionProviderId(preference.selection.provider)
|
|
416
|
+
: preference.selection.mode
|
|
417
|
+
return migrateDecisionRouterConfig({
|
|
418
|
+
schema_version: DECISION_ROUTER_CONFIG_SCHEMA_VERSION,
|
|
419
|
+
provider,
|
|
420
|
+
egress_consent: preference.egress_consent,
|
|
421
|
+
})
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Field-level divergence between install-state and the user-level config (D7).
|
|
426
|
+
* Returns field NAMES and both states, never free-form content; callers print a
|
|
427
|
+
* reconciliation instruction instead of rewriting the record silently.
|
|
428
|
+
*
|
|
429
|
+
* @param {unknown} userConfig
|
|
430
|
+
* @param {unknown} installStateRecord
|
|
431
|
+
* @returns {{ expected: object, actual: object, differences: Array<{ field: string, installState: unknown, userConfig: unknown }> }}
|
|
432
|
+
*/
|
|
433
|
+
export function diffDecisionRouterSurfaces(userConfig, installStateRecord) {
|
|
434
|
+
const expected = projectDecisionRouterInstallState(userConfig)
|
|
435
|
+
const actual = migrateDecisionRouterConfig(installStateRecord)
|
|
436
|
+
const differences = []
|
|
437
|
+
if (actual.provider !== expected.provider) {
|
|
438
|
+
differences.push({
|
|
439
|
+
field: 'provider',
|
|
440
|
+
installState: actual.provider,
|
|
441
|
+
userConfig: expected.provider,
|
|
442
|
+
})
|
|
443
|
+
}
|
|
444
|
+
if (actual.egress_consent !== expected.egress_consent) {
|
|
445
|
+
differences.push({
|
|
446
|
+
field: 'egress_consent',
|
|
447
|
+
installState: actual.egress_consent,
|
|
448
|
+
userConfig: expected.egress_consent,
|
|
449
|
+
})
|
|
450
|
+
}
|
|
451
|
+
return { expected, actual, differences }
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function sameSelection(left, right) {
|
|
455
|
+
if (left === null || right === null) return left === right
|
|
456
|
+
if (left.mode !== right.mode) return false
|
|
457
|
+
if (left.mode !== 'explicit') return true
|
|
458
|
+
return left.provider === right.provider && left.model === right.model
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
function normalizeSelection(value) {
|
|
462
|
+
if (!isPlainObject(value) || typeof value.mode !== 'string') return null
|
|
463
|
+
if (value.mode === 'local' || value.mode === 'auto') {
|
|
464
|
+
return Object.keys(value).length === 1 ? { mode: value.mode } : null
|
|
465
|
+
}
|
|
466
|
+
if (
|
|
467
|
+
value.mode !== 'explicit' ||
|
|
468
|
+
Object.keys(value).length !== 3 ||
|
|
469
|
+
!isKnownCandidate({ provider: value.provider, model: value.model })
|
|
470
|
+
) {
|
|
471
|
+
return null
|
|
472
|
+
}
|
|
473
|
+
return {
|
|
474
|
+
mode: 'explicit',
|
|
475
|
+
provider: normalizeDecisionProviderId(value.provider),
|
|
476
|
+
model: value.model,
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
function normalizeEnabledCandidates(value) {
|
|
481
|
+
const seen = new Set()
|
|
482
|
+
const result = []
|
|
483
|
+
for (const item of value) {
|
|
484
|
+
if (!isKnownCandidate(item) || Object.keys(item).length !== 2) return null
|
|
485
|
+
const provider = normalizeDecisionProviderId(item.provider)
|
|
486
|
+
const key = candidateKey(provider, item.model)
|
|
487
|
+
if (seen.has(key)) return null
|
|
488
|
+
seen.add(key)
|
|
489
|
+
result.push({ provider, model: item.model })
|
|
490
|
+
}
|
|
491
|
+
return result
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function isKnownCandidate(value) {
|
|
495
|
+
if (
|
|
496
|
+
!isPlainObject(value) ||
|
|
497
|
+
typeof value.provider !== 'string' ||
|
|
498
|
+
typeof value.model !== 'string'
|
|
499
|
+
) {
|
|
500
|
+
return false
|
|
501
|
+
}
|
|
502
|
+
const provider = normalizeDecisionProviderId(value.provider)
|
|
503
|
+
return (
|
|
504
|
+
userCandidateProviders.has(provider) && DECISION_ROUTER_MODELS[provider].includes(value.model)
|
|
505
|
+
)
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function candidateKey(provider, model) {
|
|
509
|
+
return `${provider}/${model}`
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function isPlainObject(value) {
|
|
513
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false
|
|
514
|
+
const prototype = Object.getPrototypeOf(value)
|
|
515
|
+
return prototype === Object.prototype || prototype === null
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function hasExactKeys(value, keys) {
|
|
519
|
+
const actual = Object.keys(value).sort()
|
|
520
|
+
const expected = [...keys].sort()
|
|
521
|
+
return actual.length === expected.length && actual.every((key, index) => key === expected[index])
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function cloneDefaultUserConfig() {
|
|
525
|
+
return {
|
|
526
|
+
schema_version: DEFAULT_USER_DECISION_ROUTER_CONFIG.schema_version,
|
|
527
|
+
enabled: false,
|
|
528
|
+
selection: { mode: 'local' },
|
|
529
|
+
egress_consent: false,
|
|
530
|
+
enabled_candidates: [],
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function assertPrivatePath(stat, isFile) {
|
|
535
|
+
if (stat.isSymbolicLink() || (isFile ? !stat.isFile() : !stat.isDirectory())) {
|
|
536
|
+
throw new Error('Decision-router configuration path is not a regular private path')
|
|
537
|
+
}
|
|
538
|
+
if (typeof process.getuid !== 'function' || stat.uid !== process.getuid()) {
|
|
539
|
+
throw new Error('Decision-router configuration must be owned by the current user')
|
|
540
|
+
}
|
|
541
|
+
if ((stat.mode & 0o077) !== 0)
|
|
542
|
+
throw new Error('Decision-router configuration permissions must be private')
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function assertPrivateDirectory(directory) {
|
|
546
|
+
const stat = lstatSync(directory)
|
|
547
|
+
assertPrivatePath(stat, false)
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
function assertTrustedDirectoryChain(directory) {
|
|
551
|
+
const absolute = resolve(directory)
|
|
552
|
+
const root = parse(absolute).root
|
|
553
|
+
const parts = absolute.slice(root.length).split(sep).filter(Boolean)
|
|
554
|
+
let current = root
|
|
555
|
+
const uid = process.getuid?.()
|
|
556
|
+
if (uid === undefined)
|
|
557
|
+
throw new Error('Decision-router configuration requires user ownership checks')
|
|
558
|
+
for (const part of parts) {
|
|
559
|
+
current = join(current, part)
|
|
560
|
+
let stat
|
|
561
|
+
try {
|
|
562
|
+
stat = lstatSync(current)
|
|
563
|
+
} catch (error) {
|
|
564
|
+
if (error?.code === 'ENOENT') return
|
|
565
|
+
throw error
|
|
566
|
+
}
|
|
567
|
+
if (!stat.isDirectory() || stat.isSymbolicLink()) {
|
|
568
|
+
throw new Error('Decision-router configuration path contains a non-directory component')
|
|
569
|
+
}
|
|
570
|
+
if (stat.uid !== uid && stat.uid !== 0) {
|
|
571
|
+
throw new Error('Decision-router configuration path has an untrusted owner')
|
|
572
|
+
}
|
|
573
|
+
const mode = stat.mode & 0o7777
|
|
574
|
+
const rootStickyDirectory = stat.uid === 0 && (mode & 0o1000) !== 0
|
|
575
|
+
if ((mode & 0o022) !== 0 && !rootStickyDirectory) {
|
|
576
|
+
throw new Error('Decision-router configuration path is writable by other users')
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
import * as clack from '@clack/prompts'
|
|
4
|
+
import {
|
|
5
|
+
createDecisionRouterConfig,
|
|
6
|
+
DECISION_ROUTER_PROVIDERS,
|
|
7
|
+
DEFAULT_DECISION_ROUTER_CONFIG,
|
|
8
|
+
migrateDecisionRouterConfig,
|
|
9
|
+
} from './decision-router-config.mjs'
|
|
4
10
|
import { PROMPT_CANCELED } from './prompt-result.mjs'
|
|
5
11
|
import { strings } from './strings.mjs'
|
|
6
12
|
|
|
@@ -48,8 +54,8 @@ export async function promptReview(
|
|
|
48
54
|
/**
|
|
49
55
|
* Collect interactive setup choices before the installer begins any writes.
|
|
50
56
|
*
|
|
51
|
-
* @param {{ components?: string[], defaultComponents?: string[], noMcp?: boolean, prompts?: typeof clack, isTTY?: boolean }} [options]
|
|
52
|
-
* @returns {Promise<{ components?: Set<string>, target?: string, canceled?: boolean, cancelPoint?: string }>}
|
|
57
|
+
* @param {{ components?: string[], defaultComponents?: string[], noMcp?: boolean, prompts?: typeof clack, isTTY?: boolean, getDecisionRouterConfig?: (target: string) => unknown }} [options]
|
|
58
|
+
* @returns {Promise<{ components?: Set<string>, target?: string, decisionRouterConfig?: object, canceled?: boolean, cancelPoint?: string }>}
|
|
53
59
|
*
|
|
54
60
|
* `cancelPoint` names the setup choice where the user stopped (`components` or
|
|
55
61
|
* `target`), so the caller can print one explainable line and persist the same
|
|
@@ -59,6 +65,7 @@ export async function runInteractiveInstall({
|
|
|
59
65
|
components = [],
|
|
60
66
|
defaultComponents = [],
|
|
61
67
|
noMcp = false,
|
|
68
|
+
getDecisionRouterConfig = () => DEFAULT_DECISION_ROUTER_CONFIG,
|
|
62
69
|
prompts = clack,
|
|
63
70
|
isTTY = isTerminal(),
|
|
64
71
|
} = {}) {
|
|
@@ -68,8 +75,16 @@ export async function runInteractiveInstall({
|
|
|
68
75
|
|
|
69
76
|
// Preserve the same component and target defaults without asking a terminal
|
|
70
77
|
// prompt library to access stdin. Apply authorization is handled once, after
|
|
71
|
-
// the installer has built and displayed the complete plan.
|
|
72
|
-
|
|
78
|
+
// the installer has built and displayed the complete plan. Re-read the
|
|
79
|
+
// selected default target's preference so --interactive without a TTY does
|
|
80
|
+
// not reset an existing provider choice or consent on update.
|
|
81
|
+
if (!isTTY) {
|
|
82
|
+
return {
|
|
83
|
+
components: defaultSet,
|
|
84
|
+
target: 'global',
|
|
85
|
+
decisionRouterConfig: migrateDecisionRouterConfig(getDecisionRouterConfig('global')),
|
|
86
|
+
}
|
|
87
|
+
}
|
|
73
88
|
|
|
74
89
|
prompts.intro?.('OpenCode Installer')
|
|
75
90
|
|
|
@@ -111,5 +126,61 @@ export async function runInteractiveInstall({
|
|
|
111
126
|
})
|
|
112
127
|
if (cancelPrompt(prompts, target, 'target')) return { canceled: true, cancelPoint: 'target' }
|
|
113
128
|
|
|
114
|
-
|
|
129
|
+
const previousConfig = migrateDecisionRouterConfig(getDecisionRouterConfig(target))
|
|
130
|
+
const provider = await prompts.select({
|
|
131
|
+
message:
|
|
132
|
+
'Decision-router installer preference (default: Local). This records target-specific metadata only; runtime provider/model settings, egress consent, caps, and period confirmation are managed separately with `npx pantheon-opencode decision-router ...`. Installation makes no provider requests.',
|
|
133
|
+
options: [
|
|
134
|
+
{
|
|
135
|
+
label: 'Local',
|
|
136
|
+
value: DECISION_ROUTER_PROVIDERS[0],
|
|
137
|
+
hint: 'Local recommendation logic; no task data is sent to a provider',
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
label: 'OpenAI Decisions',
|
|
141
|
+
value: DECISION_ROUTER_PROVIDERS[1],
|
|
142
|
+
hint: 'GPT-6 Luna installer metadata only; set runtime config separately',
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
label: 'Jev',
|
|
146
|
+
value: DECISION_ROUTER_PROVIDERS[2],
|
|
147
|
+
hint: 'TypeSafe Jev installer metadata only; set runtime config separately',
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
label: 'Auto',
|
|
151
|
+
value: DECISION_ROUTER_PROVIDERS[3],
|
|
152
|
+
hint: 'Installer metadata only; runtime Auto requires one cap-valid enabled candidate',
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
initialValue: previousConfig.provider,
|
|
156
|
+
})
|
|
157
|
+
if (cancelPrompt(prompts, provider, 'decisionRouter')) {
|
|
158
|
+
return { canceled: true, cancelPoint: 'decisionRouter' }
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
let egressConsent = false
|
|
162
|
+
if (provider !== 'local') {
|
|
163
|
+
const providerLabel =
|
|
164
|
+
provider === 'openai-decisions'
|
|
165
|
+
? 'OpenAI Decisions'
|
|
166
|
+
: provider === 'jev'
|
|
167
|
+
? 'Jev'
|
|
168
|
+
: 'the Auto-selected provider'
|
|
169
|
+
const consent = await prompts.confirm({
|
|
170
|
+
message:
|
|
171
|
+
`Save a legacy installer consent preference for potential future ${providerLabel} use? ` +
|
|
172
|
+
'This is not runtime consent and does not authorize provider transmission. Configure actual user-level consent and caps with `npx pantheon-opencode decision-router ...`. Installation makes no provider requests.',
|
|
173
|
+
initialValue: previousConfig.provider === provider && previousConfig.egress_consent,
|
|
174
|
+
})
|
|
175
|
+
if (cancelPrompt(prompts, consent, 'egressConsent')) {
|
|
176
|
+
return { canceled: true, cancelPoint: 'egressConsent' }
|
|
177
|
+
}
|
|
178
|
+
egressConsent = consent === true
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
components: selectedComponents,
|
|
183
|
+
target,
|
|
184
|
+
decisionRouterConfig: createDecisionRouterConfig(provider, egressConsent),
|
|
185
|
+
}
|
|
115
186
|
}
|