pantheon-opencode 1.6.2-beta.5 → 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 +19 -0
- package/README.md +40 -38
- package/README.pt-BR.md +9 -0
- package/package.json +2 -1
- package/plugin.json +1 -1
- package/pyproject.toml +1 -1
- package/scripts/decision-router-budget.mjs +4 -5
- package/scripts/install/decision-router-config.mjs +4 -23
- 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 +3 -9
- package/src/pantheon/local-decision-provider-matrix.ts +14 -62
- 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-points/p8-guardrail.ts +3 -2
- package/src/pantheon/v2-decision-points.ts +1 -1
- package/src/pantheon/v2-native-decisions.ts +365 -0
- package/src/pantheon/v2-tools.ts +3 -0
- package/src/plugin-v2.ts +59 -15
- 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
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* decision-provider egress action.
|
|
2
|
+
* Host kill-switch contract for the single decision-egress action.
|
|
4
3
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* period means no request, and the host is not even asked.
|
|
10
|
-
*
|
|
11
|
-
* `permission.assert` is a host kill-switch layered on top, never consent:
|
|
4
|
+
* `permission.assert` is a host kill-switch layered on top of the user-level
|
|
5
|
+
* config, never consent itself. The native `pantheon_jev_decide` tool asks it
|
|
6
|
+
* with the logical resource `pantheon-native-decisions://<providerID>/<id>`
|
|
7
|
+
* after its own config/route/ledger gates pass.
|
|
12
8
|
*
|
|
13
9
|
* - Deny is observable: the host throws (`Permission.BlockedError`, whose
|
|
14
10
|
* message is `Permission denied: <action>` unless an evaluate hook supplied a
|
|
@@ -16,44 +12,16 @@
|
|
|
16
12
|
* - Allow is NOT observable as a positive value. The measured host contract
|
|
17
13
|
* (`Permission.assert` in the bundled opencode host) resolves `undefined` when
|
|
18
14
|
* every rule admits the request. A resolved non-denial therefore only means
|
|
19
|
-
* "the host did not block it"; the configuration
|
|
15
|
+
* "the host did not block it"; the configuration gates remain the authority.
|
|
20
16
|
*
|
|
21
17
|
* The custom action has no default rule, and the measured v2.2.0 canary showed
|
|
22
18
|
* the host is NOT deny-by-default for it: an assertion with no matching rule
|
|
23
|
-
* resolved allow. The config/consent gate
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* `assert` is called by the plugin in the same process it protects. It is not a
|
|
30
|
-
* boundary against hostile same-process code; the fixed endpoints, redirect
|
|
31
|
-
* refusal, timeout, response cap, and accounting ledger are the real protection
|
|
32
|
-
* against an accidental or over-broad egress.
|
|
33
|
-
*
|
|
34
|
-
* Canary note: a real host canary of v2.2.0 observed that `permission.assert`
|
|
35
|
-
* reaches the `evaluate` hook on the allow path and is not invoked for an
|
|
36
|
-
* explicit deny, that an assertion with no rule for this custom action resolved
|
|
37
|
-
* allow rather than deny, and that the host accepts a rule under root
|
|
38
|
-
* `permission`/`permissions` or `agents.<id>.permissions` while ignoring an
|
|
39
|
-
* `agents.<id>.permission` map. Egress therefore must not depend on an
|
|
40
|
-
* evaluate-hook decision, and the plugin does not mint or consume an
|
|
41
|
-
* allowance.
|
|
19
|
+
* resolved allow. The config/consent gate is therefore the source of truth for
|
|
20
|
+
* authorization, and an explicit `deny` rule is the kill-switch; the fix for a
|
|
21
|
+
* blocked host is an explicit user permission rule for `decision.egress`, not a
|
|
22
|
+
* bypass in this plugin. This module never retries, downgrades, or reinterprets
|
|
23
|
+
* a denial. Pantheon owns no transport: there is no endpoint to fall back to.
|
|
42
24
|
*/
|
|
43
|
-
import {
|
|
44
|
-
readUserDecisionRouterConfig,
|
|
45
|
-
resolveUserDecisionRouterCandidate,
|
|
46
|
-
type UserDecisionRouterCandidate,
|
|
47
|
-
} from '../../scripts/install/decision-router-config.mjs'
|
|
48
|
-
import {
|
|
49
|
-
openLocalDecisionBudgetLedger,
|
|
50
|
-
resolveLocalDecisionBudgetDbPath,
|
|
51
|
-
} from './local-decision-budget.ts'
|
|
52
|
-
import {
|
|
53
|
-
DECISION_PROVIDER_ADAPTERS,
|
|
54
|
-
type DecisionProviderId,
|
|
55
|
-
} from './local-decision-provider-matrix.ts'
|
|
56
|
-
|
|
57
25
|
/**
|
|
58
26
|
* Custom host action, distinct from `webfetch`. It has no default host rule; the
|
|
59
27
|
* measured host resolves an unruled assertion as allow, so only an explicit
|
|
@@ -70,16 +38,6 @@ export interface DecisionEgressSource {
|
|
|
70
38
|
messageID: string
|
|
71
39
|
}
|
|
72
40
|
|
|
73
|
-
/** Reason codes shared with the provider runner's fallback contract. */
|
|
74
|
-
export type DecisionEgressGateReason =
|
|
75
|
-
| 'CONFIG_UNAVAILABLE'
|
|
76
|
-
| 'DISABLED'
|
|
77
|
-
| 'CONSENT_MISSING'
|
|
78
|
-
| 'NO_ELIGIBLE_CANDIDATE'
|
|
79
|
-
| 'CAP_OR_PERIOD_DENIED'
|
|
80
|
-
| 'PERMISSION_UNAVAILABLE'
|
|
81
|
-
| 'PERMISSION_DENIED'
|
|
82
|
-
|
|
83
41
|
export type PermissionAssert = (input: Record<string, unknown>) => Promise<unknown>
|
|
84
42
|
|
|
85
43
|
export type DecisionEgressAssertion = 'allow' | 'deny'
|
|
@@ -103,9 +61,9 @@ export function decisionPermissionSource(
|
|
|
103
61
|
}
|
|
104
62
|
|
|
105
63
|
/**
|
|
106
|
-
* The only shape the
|
|
107
|
-
*
|
|
108
|
-
*
|
|
64
|
+
* The only shape the native tool sends to `permission.assert`. The resource is
|
|
65
|
+
* a logical URI (the host owns any real endpoint), so permission rules can
|
|
66
|
+
* target the native route without exposing a URL.
|
|
109
67
|
*/
|
|
110
68
|
export function buildDecisionEgressRequest(input: {
|
|
111
69
|
sessionID: string
|
|
@@ -169,8 +127,8 @@ export function isDecisionAssertionDenial(result: unknown): boolean {
|
|
|
169
127
|
*
|
|
170
128
|
* A throw is a deny. A resolved explicit denial marker is a deny. Everything
|
|
171
129
|
* else is `allow` — not because the host promised consent, but because the
|
|
172
|
-
* host's allow path is indistinguishable from `undefined`; the config
|
|
173
|
-
*
|
|
130
|
+
* host's allow path is indistinguishable from `undefined`; the config gates
|
|
131
|
+
* are what actually authorize egress.
|
|
174
132
|
*/
|
|
175
133
|
export async function assertDecisionEgressPermission(
|
|
176
134
|
permissionAssert: PermissionAssert,
|
|
@@ -184,81 +142,3 @@ export async function assertDecisionEgressPermission(
|
|
|
184
142
|
}
|
|
185
143
|
return isDecisionAssertionDenial(result) ? 'deny' : 'allow'
|
|
186
144
|
}
|
|
187
|
-
|
|
188
|
-
export interface CapValidDecisionCandidate {
|
|
189
|
-
provider: DecisionProviderId
|
|
190
|
-
adapter: (typeof DECISION_PROVIDER_ADAPTERS)[DecisionProviderId]
|
|
191
|
-
candidate: UserDecisionRouterCandidate
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Single cap-valid selection shared by the provider runner and the cached-turn
|
|
196
|
-
* recheck. Opens the ledger, keeps only enabled, egress-eligible candidates
|
|
197
|
-
* whose period and caps admit a reservation, resolves the preference, and
|
|
198
|
-
* requires the resolved model to match the fixed matrix row.
|
|
199
|
-
*
|
|
200
|
-
* Throws when the ledger cannot be opened or queried; callers map that to
|
|
201
|
-
* `CAP_OR_PERIOD_DENIED`. Returns `null` when no candidate qualifies.
|
|
202
|
-
*/
|
|
203
|
-
export function selectCapValidDecisionCandidate(
|
|
204
|
-
config: ReturnType<typeof readUserDecisionRouterConfig>,
|
|
205
|
-
): CapValidDecisionCandidate | null {
|
|
206
|
-
const ledger = openLocalDecisionBudgetLedger({ dbPath: resolveLocalDecisionBudgetDbPath() })
|
|
207
|
-
try {
|
|
208
|
-
const valid: UserDecisionRouterCandidate[] = []
|
|
209
|
-
for (const candidate of config.enabled_candidates) {
|
|
210
|
-
const provider = candidate.provider as DecisionProviderId
|
|
211
|
-
const adapter = DECISION_PROVIDER_ADAPTERS[provider]
|
|
212
|
-
if (adapter === undefined || !adapter.egressEligible) continue
|
|
213
|
-
if (ledger.canReserve(adapter.accountingProvider, candidate.model).status !== 'eligible') {
|
|
214
|
-
continue
|
|
215
|
-
}
|
|
216
|
-
valid.push(candidate)
|
|
217
|
-
}
|
|
218
|
-
const selected = resolveUserDecisionRouterCandidate(config, valid)
|
|
219
|
-
if (selected === null) return null
|
|
220
|
-
const provider = selected.provider as DecisionProviderId
|
|
221
|
-
const adapter = DECISION_PROVIDER_ADAPTERS[provider]
|
|
222
|
-
if (adapter === undefined || !adapter.egressEligible) return null
|
|
223
|
-
if (selected.model !== adapter.model) return null
|
|
224
|
-
return { provider, adapter, candidate: selected }
|
|
225
|
-
} finally {
|
|
226
|
-
ledger.close()
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export type DecisionEgressConsent =
|
|
231
|
-
| { ok: true; selected: CapValidDecisionCandidate }
|
|
232
|
-
| {
|
|
233
|
-
ok: false
|
|
234
|
-
reasonCode: Exclude<DecisionEgressGateReason, 'PERMISSION_UNAVAILABLE' | 'PERMISSION_DENIED'>
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Evaluate the consent/config gate without touching the network or the host.
|
|
239
|
-
*
|
|
240
|
-
* Callers run this BEFORE `permission.assert`: an invalid configuration is not
|
|
241
|
-
* worth an interactive prompt, and the host must not be asked to approve a
|
|
242
|
-
* request that the user's own configuration already forbids.
|
|
243
|
-
*/
|
|
244
|
-
export function resolveDecisionEgressConsent(): DecisionEgressConsent {
|
|
245
|
-
let config: ReturnType<typeof readUserDecisionRouterConfig>
|
|
246
|
-
try {
|
|
247
|
-
config = readUserDecisionRouterConfig()
|
|
248
|
-
} catch {
|
|
249
|
-
return { ok: false, reasonCode: 'CONFIG_UNAVAILABLE' }
|
|
250
|
-
}
|
|
251
|
-
if (!config.enabled) return { ok: false, reasonCode: 'DISABLED' }
|
|
252
|
-
if (!config.egress_consent || config.selection.mode === 'local') {
|
|
253
|
-
return { ok: false, reasonCode: 'CONSENT_MISSING' }
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
let selected: CapValidDecisionCandidate | null
|
|
257
|
-
try {
|
|
258
|
-
selected = selectCapValidDecisionCandidate(config)
|
|
259
|
-
} catch {
|
|
260
|
-
return { ok: false, reasonCode: 'CAP_OR_PERIOD_DENIED' }
|
|
261
|
-
}
|
|
262
|
-
if (selected === null) return { ok: false, reasonCode: 'NO_ELIGIBLE_CANDIDATE' }
|
|
263
|
-
return { ok: true, selected }
|
|
264
|
-
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { decideLocalForTurn, type TurnDecision } from './local-decision-turn.ts'
|
|
2
2
|
import { isPantheonAgentId, isPantheonSkillId } from './pantheon-decision-catalog.ts'
|
|
3
3
|
|
|
4
4
|
const MARKER_PREFIX = '<!-- pantheon-decision-router:v1'
|
|
@@ -223,14 +223,9 @@ export function hasDecisionRouterMarker(text: unknown): boolean {
|
|
|
223
223
|
return typeof text === 'string' && MARKER_PATTERN.test(text)
|
|
224
224
|
}
|
|
225
225
|
|
|
226
|
-
/**
|
|
227
|
-
* Compute and apply the advisory decision to a host admission payload. The
|
|
228
|
-
* optional `requestText` lets the caller keep Pantheon's own visual context out
|
|
229
|
-
* of the decision input; malformed payloads are a no-op.
|
|
230
|
-
*/
|
|
226
|
+
/** Apply the local advisory selector while keeping provider egress out of this route. */
|
|
231
227
|
export async function applyDecisionRouterToPrompt(
|
|
232
228
|
admission: unknown,
|
|
233
|
-
permissionAssert?: TurnDecisionInput['permissionAssert'],
|
|
234
229
|
requestText?: string,
|
|
235
230
|
): Promise<PromptApplicationResult> {
|
|
236
231
|
const none = (reason: PromptApplicationReason): PromptApplicationResult => ({
|
|
@@ -255,11 +250,10 @@ export async function applyDecisionRouterToPrompt(
|
|
|
255
250
|
typeof requestText === 'string' && requestText.trim().length > 0 ? requestText : promptText
|
|
256
251
|
if (hasDecisionRouterMarker(text)) return none('marker.present')
|
|
257
252
|
try {
|
|
258
|
-
const decision = await
|
|
253
|
+
const decision = await decideLocalForTurn({
|
|
259
254
|
request: text,
|
|
260
255
|
sessionID: event.sessionID,
|
|
261
256
|
messageID: event.messageID,
|
|
262
|
-
permissionAssert,
|
|
263
257
|
})
|
|
264
258
|
return applyTurnDecisionToPrompt(prompt, decision)
|
|
265
259
|
} catch {
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Private provider matrix: fixed provider/model
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Private provider matrix: fixed provider/model rows and their ledger
|
|
3
|
+
* accounting mapping. No network I/O, no endpoints, no credentials — the
|
|
4
|
+
* direct-fetch adapter fields were removed with the legacy fetch runner; the
|
|
5
|
+
* only egress transport is the host's native Decisions layer, whose provider
|
|
6
|
+
* and credentials the host owns. Pantheon uses this matrix for the config
|
|
7
|
+
* candidate model IDs and ledger accounting names only.
|
|
5
8
|
*/
|
|
6
9
|
import {
|
|
7
10
|
DECISION_PROVIDER_BUDGET_MAPPING,
|
|
@@ -15,78 +18,27 @@ export type DecisionProviderId =
|
|
|
15
18
|
| 'opencode-console-jev'
|
|
16
19
|
| 'opencode-console-jev-free'
|
|
17
20
|
|
|
18
|
-
export type DecisionProtocol = 'openai' | 'systemone'
|
|
19
|
-
|
|
20
|
-
export const PROVIDER_MAX_RESPONSE_BYTES = 64 * 1024
|
|
21
|
-
export const PROVIDER_REQUEST_TIMEOUT_MS = 5_000
|
|
22
|
-
|
|
23
|
-
export interface DecisionProviderAdapter {
|
|
24
|
-
model: string
|
|
25
|
-
endpoint: string
|
|
26
|
-
credentialEnv: string
|
|
27
|
-
accountingProvider: BudgetProvider
|
|
28
|
-
egressEligible: boolean
|
|
29
|
-
protocol: DecisionProtocol
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const ADAPTER_DEFINITIONS: Readonly<
|
|
33
|
-
Record<
|
|
34
|
-
DecisionProviderId,
|
|
35
|
-
{
|
|
36
|
-
endpoint: string
|
|
37
|
-
credentialEnv: string
|
|
38
|
-
egressEligible: boolean
|
|
39
|
-
protocol: DecisionProtocol
|
|
40
|
-
}
|
|
41
|
-
>
|
|
42
|
-
> = Object.freeze({
|
|
43
|
-
'openai-decisions': {
|
|
44
|
-
endpoint: 'https://api.openai.com/v1/decisions',
|
|
45
|
-
credentialEnv: 'OPENAI_API_KEY',
|
|
46
|
-
egressEligible: true,
|
|
47
|
-
protocol: 'openai',
|
|
48
|
-
},
|
|
49
|
-
'typesafe-jev': {
|
|
50
|
-
endpoint: 'https://api.typesafe.ai/v1/systemone',
|
|
51
|
-
credentialEnv: 'TYPESAFE_API_KEY',
|
|
52
|
-
egressEligible: true,
|
|
53
|
-
protocol: 'systemone',
|
|
54
|
-
},
|
|
55
|
-
'opencode-console-jev': {
|
|
56
|
-
endpoint: 'https://opencode.ai/inference/systemone/v1/systemone',
|
|
57
|
-
credentialEnv: 'OPENCODE_API_KEY',
|
|
58
|
-
egressEligible: false,
|
|
59
|
-
protocol: 'systemone',
|
|
60
|
-
},
|
|
61
|
-
'opencode-console-jev-free': {
|
|
62
|
-
endpoint: 'https://opencode.ai/inference/systemone/v1/systemone',
|
|
63
|
-
credentialEnv: 'OPENCODE_API_KEY',
|
|
64
|
-
egressEligible: false,
|
|
65
|
-
protocol: 'systemone',
|
|
66
|
-
},
|
|
67
|
-
})
|
|
68
|
-
|
|
69
21
|
/**
|
|
70
22
|
* Single provider matrix. Model and accounting IDs come from the canonical
|
|
71
|
-
* config so the
|
|
23
|
+
* config so the route resolver and the ledger can never drift silently.
|
|
72
24
|
*/
|
|
73
25
|
export const DECISION_PROVIDER_ADAPTERS: Readonly<
|
|
74
|
-
Record<DecisionProviderId,
|
|
26
|
+
Record<DecisionProviderId, { model: string; accountingProvider: BudgetProvider }>
|
|
75
27
|
> = Object.freeze(
|
|
76
28
|
Object.fromEntries(
|
|
77
|
-
(Object.keys(
|
|
78
|
-
const
|
|
79
|
-
const models = DECISION_ROUTER_MODELS[provider]
|
|
80
|
-
const model = models?.[0]
|
|
29
|
+
(Object.keys(DECISION_ROUTER_MODELS) as DecisionProviderId[]).map((provider) => {
|
|
30
|
+
const model = DECISION_ROUTER_MODELS[provider]?.[0]
|
|
81
31
|
if (model === undefined) throw new Error(`Missing decision provider model: ${provider}`)
|
|
82
32
|
return [
|
|
83
33
|
provider,
|
|
84
34
|
Object.freeze({
|
|
85
|
-
...definition,
|
|
86
35
|
model,
|
|
87
36
|
accountingProvider: DECISION_PROVIDER_BUDGET_MAPPING[provider] as BudgetProvider,
|
|
88
37
|
}),
|
|
89
38
|
]
|
|
90
39
|
}),
|
|
91
|
-
) as Record<
|
|
40
|
+
) as Record<
|
|
41
|
+
DecisionProviderId,
|
|
42
|
+
{ readonly model: string; readonly accountingProvider: BudgetProvider }
|
|
43
|
+
>,
|
|
92
44
|
)
|
|
@@ -1,39 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* The deterministic local advisory decision for one turn.
|
|
3
3
|
*
|
|
4
|
-
* The
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* The production V2 prompt hook uses `decideLocalForTurn` only. It is
|
|
5
|
+
* deterministic and local-only: no provider branch, no consent/credential/
|
|
6
|
+
* permission path, no transport, no cache — the legacy provider-capable
|
|
7
|
+
* `decideForTurn` and its one-call-per-turn cache were removed with the
|
|
8
|
+
* direct-fetch provider runner (the native route is the explicit
|
|
9
|
+
* `pantheon_jev_decide` tool, which correlates its own turn).
|
|
7
10
|
*/
|
|
8
11
|
import {
|
|
9
12
|
isUserDecisionRouterEnabled,
|
|
10
13
|
readUserDecisionRouterConfig,
|
|
11
14
|
type UserDecisionRouterConfig,
|
|
12
15
|
} from '../../scripts/install/decision-router-config.mjs'
|
|
13
|
-
import {
|
|
14
|
-
hasDelegationSignal,
|
|
15
|
-
listEligibleDecisionAgents,
|
|
16
|
-
recommendLocalDecisionAgent,
|
|
17
|
-
} from './local-decision-agents.ts'
|
|
18
|
-
import {
|
|
19
|
-
assertDecisionEgressPermission,
|
|
20
|
-
buildDecisionEgressRequest,
|
|
21
|
-
isDecisionAssertionDenial,
|
|
22
|
-
type PermissionAssert,
|
|
23
|
-
resolveDecisionEgressConsent,
|
|
24
|
-
} from './local-decision-egress-gate.ts'
|
|
25
|
-
import {
|
|
26
|
-
buildDecisionPayload,
|
|
27
|
-
DECISION_PROVIDER_ADAPTERS,
|
|
28
|
-
type DecisionPayload,
|
|
29
|
-
type DecisionProviderId,
|
|
30
|
-
runProviderDecision,
|
|
31
|
-
} from './local-decision-provider.internal.ts'
|
|
16
|
+
import { hasDelegationSignal, recommendLocalDecisionAgent } from './local-decision-agents.ts'
|
|
32
17
|
import { selectPantheonSkills } from './local-decision-skills.ts'
|
|
33
18
|
import { type LocalDecisionPolicy, loadLocalDecisionPolicy } from './local-orchestration.ts'
|
|
34
|
-
import { PANTHEON_SKILL_CATALOG } from './pantheon-decision-catalog.ts'
|
|
35
19
|
|
|
36
|
-
export type TurnDecisionSource = 'local'
|
|
20
|
+
export type TurnDecisionSource = 'local'
|
|
37
21
|
|
|
38
22
|
export interface TurnDecision {
|
|
39
23
|
schemaVersion: 1
|
|
@@ -44,31 +28,13 @@ export interface TurnDecision {
|
|
|
44
28
|
reasonCode: string
|
|
45
29
|
}
|
|
46
30
|
|
|
47
|
-
type PermissionOutcome = 'allowed' | 'denied'
|
|
48
|
-
|
|
49
|
-
interface TurnCacheEntry {
|
|
50
|
-
decision: TurnDecision
|
|
51
|
-
expiresAt: number
|
|
52
|
-
/**
|
|
53
|
-
* Assert outcome memoized per caller gate identity. The gate function is the
|
|
54
|
-
* only stable caller identity available here, so an interactive
|
|
55
|
-
* `permission.assert` is asked at most once per gate for a cached turn.
|
|
56
|
-
*/
|
|
57
|
-
permissions: WeakMap<PermissionAssert, PermissionOutcome>
|
|
58
|
-
}
|
|
59
|
-
|
|
60
31
|
export interface TurnDecisionInput {
|
|
61
32
|
request: unknown
|
|
62
33
|
sessionID: unknown
|
|
63
34
|
messageID: unknown
|
|
64
|
-
permissionAssert?: PermissionAssert | undefined
|
|
65
35
|
}
|
|
66
36
|
|
|
67
37
|
const MAX_REQUEST_LENGTH = 20_000
|
|
68
|
-
const CACHE_MAX_ENTRIES = 128
|
|
69
|
-
const CACHE_TTL_MS = 10 * 60_000
|
|
70
|
-
const turnCache = new Map<string, TurnCacheEntry>()
|
|
71
|
-
const inFlight = new Map<string, Promise<TurnDecision>>()
|
|
72
38
|
|
|
73
39
|
function inactive(reasonCode: string): TurnDecision {
|
|
74
40
|
return {
|
|
@@ -81,35 +47,6 @@ function inactive(reasonCode: string): TurnDecision {
|
|
|
81
47
|
}
|
|
82
48
|
}
|
|
83
49
|
|
|
84
|
-
function cacheKey(sessionID: string, messageID: string): string {
|
|
85
|
-
return `${sessionID}\u0000${messageID}`
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function readCache(key: string, now: number): TurnCacheEntry | undefined {
|
|
89
|
-
const entry = turnCache.get(key)
|
|
90
|
-
if (entry === undefined) return undefined
|
|
91
|
-
if (entry.expiresAt <= now) {
|
|
92
|
-
turnCache.delete(key)
|
|
93
|
-
return undefined
|
|
94
|
-
}
|
|
95
|
-
return entry
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function writeCache(key: string, entry: TurnCacheEntry, now: number): void {
|
|
99
|
-
if (turnCache.size >= CACHE_MAX_ENTRIES && !turnCache.has(key)) {
|
|
100
|
-
const oldest = turnCache.keys().next().value
|
|
101
|
-
if (oldest !== undefined) turnCache.delete(oldest)
|
|
102
|
-
}
|
|
103
|
-
entry.expiresAt = now + CACHE_TTL_MS
|
|
104
|
-
turnCache.set(key, entry)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/** Test-only cache reset. It cannot initiate a provider request. */
|
|
108
|
-
export function clearTurnDecisionCache(): void {
|
|
109
|
-
turnCache.clear()
|
|
110
|
-
inFlight.clear()
|
|
111
|
-
}
|
|
112
|
-
|
|
113
50
|
function readConfig(): UserDecisionRouterConfig | undefined {
|
|
114
51
|
try {
|
|
115
52
|
return readUserDecisionRouterConfig()
|
|
@@ -137,94 +74,13 @@ function localDecision(
|
|
|
137
74
|
}
|
|
138
75
|
}
|
|
139
76
|
|
|
140
|
-
function buildPayload(
|
|
141
|
-
request: string,
|
|
142
|
-
eligibleAgents: readonly string[],
|
|
143
|
-
includeAgents: boolean,
|
|
144
|
-
): DecisionPayload | null {
|
|
145
|
-
return buildDecisionPayload({
|
|
146
|
-
request,
|
|
147
|
-
skillIds: PANTHEON_SKILL_CATALOG.map((entry) => entry.id),
|
|
148
|
-
...(includeAgents && eligibleAgents.length > 0 ? { agentIds: eligibleAgents } : {}),
|
|
149
|
-
})
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* A cached/in-flight provider decision was minted under an earlier caller's
|
|
154
|
-
* permission assertion. Reuse therefore needs authority for *this* caller: the
|
|
155
|
-
* outcome is memoized per gate function identity, so the same (possibly
|
|
156
|
-
* interactive) gate is not prompted twice for one cached turn, while a
|
|
157
|
-
* different or missing gate is evaluated afresh. A missing or denying assertion
|
|
158
|
-
* downgrades the turn to inactive instead of handing out a remote
|
|
159
|
-
* recommendation (no new egress happens here).
|
|
160
|
-
*
|
|
161
|
-
* The config/consent gate is re-read on every reuse before the assertion: it is
|
|
162
|
-
* local (config file plus ledger reads, no network), and revoking consent,
|
|
163
|
-
* disabling the router, or dropping caps must take effect immediately rather
|
|
164
|
-
* than after the 10-minute cache expires.
|
|
165
|
-
*
|
|
166
|
-
* The assertion is the host's kill-switch; the user-level config remains the
|
|
167
|
-
* source of truth. A resolved non-denial is the host's allow path (`undefined`
|
|
168
|
-
* on the measured host), so it is treated as allowed. No allowance is minted or
|
|
169
|
-
* consumed: the v2.2.0 canary showed the evaluate hook runs on the allow path
|
|
170
|
-
* and is skipped for an explicit deny, so egress never depends on it.
|
|
171
|
-
*/
|
|
172
|
-
async function reuseDeniedReason(
|
|
173
|
-
entry: TurnCacheEntry,
|
|
174
|
-
permissionAssert: TurnDecisionInput['permissionAssert'],
|
|
175
|
-
sessionID: string,
|
|
176
|
-
messageID: string,
|
|
177
|
-
): Promise<string | null> {
|
|
178
|
-
if (entry.decision.source === 'local') return null
|
|
179
|
-
const adapter = DECISION_PROVIDER_ADAPTERS[entry.decision.source]
|
|
180
|
-
if (adapter === undefined || !adapter.egressEligible) return 'PERMISSION_DENIED'
|
|
181
|
-
const consent = resolveDecisionEgressConsent()
|
|
182
|
-
if (!consent.ok) return consent.reasonCode
|
|
183
|
-
if (typeof permissionAssert !== 'function') return 'PERMISSION_UNAVAILABLE'
|
|
184
|
-
const memo = entry.permissions.get(permissionAssert)
|
|
185
|
-
if (memo === 'denied') return 'PERMISSION_DENIED'
|
|
186
|
-
if (memo === 'allowed') return null
|
|
187
|
-
const outcome = await assertDecisionEgressPermission(
|
|
188
|
-
permissionAssert,
|
|
189
|
-
buildDecisionEgressRequest({ sessionID, messageID, endpoint: adapter.endpoint }),
|
|
190
|
-
)
|
|
191
|
-
if (outcome === 'deny') {
|
|
192
|
-
entry.permissions.set(permissionAssert, 'denied')
|
|
193
|
-
return 'PERMISSION_DENIED'
|
|
194
|
-
}
|
|
195
|
-
entry.permissions.set(permissionAssert, 'allowed')
|
|
196
|
-
return null
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Record the gate outcome for the computing caller so the in-flight path can
|
|
201
|
-
* reuse it instead of asserting a second time.
|
|
202
|
-
*/
|
|
203
|
-
function recordPermission(
|
|
204
|
-
entry: TurnCacheEntry,
|
|
205
|
-
permissionAssert: PermissionAssert,
|
|
206
|
-
): PermissionAssert {
|
|
207
|
-
return async (input) => {
|
|
208
|
-
let result: unknown
|
|
209
|
-
try {
|
|
210
|
-
result = await permissionAssert(input)
|
|
211
|
-
} catch (error) {
|
|
212
|
-
entry.permissions.set(permissionAssert, 'denied')
|
|
213
|
-
throw error
|
|
214
|
-
}
|
|
215
|
-
entry.permissions.set(
|
|
216
|
-
permissionAssert,
|
|
217
|
-
isDecisionAssertionDenial(result) ? 'denied' : 'allowed',
|
|
218
|
-
)
|
|
219
|
-
return result
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
77
|
/**
|
|
224
|
-
*
|
|
225
|
-
*
|
|
78
|
+
* Produce the deterministic local recommendation without entering the
|
|
79
|
+
* provider, consent, credential, permission, or transport path. The legacy
|
|
80
|
+
* per-turn `enabled` preference gates this advisory behavior; provider-selection
|
|
81
|
+
* and egress settings do not.
|
|
226
82
|
*/
|
|
227
|
-
export async function
|
|
83
|
+
export async function decideLocalForTurn(input: TurnDecisionInput): Promise<TurnDecision> {
|
|
228
84
|
const request = typeof input.request === 'string' ? input.request : ''
|
|
229
85
|
const sessionID = typeof input.sessionID === 'string' ? input.sessionID.trim() : ''
|
|
230
86
|
const messageID = typeof input.messageID === 'string' ? input.messageID.trim() : ''
|
|
@@ -240,99 +96,6 @@ export async function decideForTurn(input: TurnDecisionInput): Promise<TurnDecis
|
|
|
240
96
|
const config = readConfig()
|
|
241
97
|
if (config === undefined || !isUserDecisionRouterEnabled(config)) return inactive('DISABLED')
|
|
242
98
|
|
|
243
|
-
const key = cacheKey(sessionID, messageID)
|
|
244
|
-
const cached = readCache(key, Date.now())
|
|
245
|
-
if (cached !== undefined) {
|
|
246
|
-
const denied = await reuseDeniedReason(cached, input.permissionAssert, sessionID, messageID)
|
|
247
|
-
return denied === null ? cached.decision : inactive(denied)
|
|
248
|
-
}
|
|
249
|
-
const pending = inFlight.get(key)
|
|
250
|
-
if (pending !== undefined) {
|
|
251
|
-
const decision = await pending
|
|
252
|
-
const settled = readCache(key, Date.now())
|
|
253
|
-
const entry = settled ?? { decision, expiresAt: 0, permissions: new WeakMap() }
|
|
254
|
-
const denied = await reuseDeniedReason(entry, input.permissionAssert, sessionID, messageID)
|
|
255
|
-
return denied === null ? decision : inactive(denied)
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const entry: TurnCacheEntry = {
|
|
259
|
-
decision: inactive('INVALID_INPUT'),
|
|
260
|
-
expiresAt: 0,
|
|
261
|
-
permissions: new WeakMap(),
|
|
262
|
-
}
|
|
263
|
-
const computation = computeTurn(input, request, sessionID, messageID, key, config, entry).finally(
|
|
264
|
-
() => inFlight.delete(key),
|
|
265
|
-
)
|
|
266
|
-
inFlight.set(key, computation)
|
|
267
|
-
return computation
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
async function computeTurn(
|
|
271
|
-
input: TurnDecisionInput,
|
|
272
|
-
request: string,
|
|
273
|
-
sessionID: string,
|
|
274
|
-
messageID: string,
|
|
275
|
-
key: string,
|
|
276
|
-
config: UserDecisionRouterConfig,
|
|
277
|
-
entry: TurnCacheEntry,
|
|
278
|
-
): Promise<TurnDecision> {
|
|
279
99
|
const policy = await loadLocalDecisionPolicy().catch(() => undefined)
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const delegationSignal = hasDelegationSignal(request)
|
|
283
|
-
const eligibleAgents = delegationSignal ? listEligibleDecisionAgents(policy) : []
|
|
284
|
-
|
|
285
|
-
if (config.selection.mode === 'local' || !config.egress_consent) {
|
|
286
|
-
entry.decision = local
|
|
287
|
-
writeCache(key, entry, Date.now())
|
|
288
|
-
return local
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const payload = buildPayload(request, eligibleAgents, delegationSignal)
|
|
292
|
-
const permissionAssert =
|
|
293
|
-
typeof input.permissionAssert === 'function'
|
|
294
|
-
? recordPermission(entry, input.permissionAssert)
|
|
295
|
-
: undefined
|
|
296
|
-
const provider = await runProviderDecision({
|
|
297
|
-
payload,
|
|
298
|
-
permissionAssert,
|
|
299
|
-
sessionID,
|
|
300
|
-
messageID,
|
|
301
|
-
}).catch(() => null)
|
|
302
|
-
|
|
303
|
-
const providerAgent = provider?.status === 'decided' ? provider.agent : null
|
|
304
|
-
const providerSkills =
|
|
305
|
-
providerAgent !== null
|
|
306
|
-
? (provider?.skillIds ?? []).filter((skill) =>
|
|
307
|
-
(policy?.agents[providerAgent]?.skills ?? []).includes(skill),
|
|
308
|
-
)
|
|
309
|
-
: (provider?.skillIds ?? [])
|
|
310
|
-
|
|
311
|
-
const decision: TurnDecision =
|
|
312
|
-
provider !== null && provider.status === 'decided'
|
|
313
|
-
? {
|
|
314
|
-
schemaVersion: 1,
|
|
315
|
-
active: true,
|
|
316
|
-
source: provider.provider ?? 'local',
|
|
317
|
-
skillIds: providerSkills.length > 0 ? providerSkills : local.skillIds,
|
|
318
|
-
agent: provider.agent,
|
|
319
|
-
reasonCode: provider.reasonCode,
|
|
320
|
-
}
|
|
321
|
-
: provider !== null && provider.status === 'abstain'
|
|
322
|
-
? {
|
|
323
|
-
schemaVersion: 1,
|
|
324
|
-
active: true,
|
|
325
|
-
source: provider.provider ?? 'local',
|
|
326
|
-
skillIds: local.skillIds,
|
|
327
|
-
agent: null,
|
|
328
|
-
reasonCode: provider.reasonCode,
|
|
329
|
-
}
|
|
330
|
-
: {
|
|
331
|
-
...local,
|
|
332
|
-
reasonCode: provider?.reasonCode ?? 'LOCAL_FALLBACK',
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
entry.decision = decision
|
|
336
|
-
writeCache(key, entry, Date.now())
|
|
337
|
-
return decision
|
|
100
|
+
return localDecision(request, policy, 'LOCAL_SELECTION')
|
|
338
101
|
}
|