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
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { PANTHEON_SKILL_CATALOG, PANTHEON_SKILL_IDS } from './pantheon-decision-catalog.ts'
|
|
2
2
|
|
|
3
|
-
export type SkillSelectionReason =
|
|
3
|
+
export type SkillSelectionReason =
|
|
4
|
+
| 'SELECTED_BY_KEYWORDS'
|
|
5
|
+
| 'SELECTED_BY_MATERIAL_REQUIREMENT'
|
|
6
|
+
| 'NO_LOCAL_MATCH'
|
|
7
|
+
| 'INVALID_INPUT'
|
|
8
|
+
|
|
9
|
+
/** Material requirement that must seat `artifact-management` (D2). */
|
|
10
|
+
export type MaterialRequirement = 'gate' | 'planning'
|
|
4
11
|
|
|
5
12
|
export interface LocalSkillSelection {
|
|
6
13
|
skillIds: string[]
|
|
7
14
|
reasonCode: SkillSelectionReason
|
|
15
|
+
/** `null` unless the request materially requires a gate or a plan (D2). */
|
|
16
|
+
materialRequirement: MaterialRequirement | null
|
|
8
17
|
}
|
|
9
18
|
|
|
10
19
|
/** Bounded so a malformed or huge prompt can never fan out local work. */
|
|
@@ -106,6 +115,55 @@ const KEYWORDS: Readonly<Record<string, readonly string[]>> = Object.freeze({
|
|
|
106
115
|
worktrees: ['worktree', 'worktrees', 'parallel', 'isolation', 'isolated'],
|
|
107
116
|
})
|
|
108
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Material gate/planning signals (D2).
|
|
120
|
+
*
|
|
121
|
+
* `artifact-management` exists for planned or materially risky work. Keyword
|
|
122
|
+
* scoring alone left it unreachable when the request demanded a gate or a plan
|
|
123
|
+
* without naming the artifact: "breaking change with human approval and a
|
|
124
|
+
* rollback plan" matched no keyword at all. These are static phrase/token
|
|
125
|
+
* lists, so detection stays local, deterministic, and bounded: no file, memory,
|
|
126
|
+
* catalog, or network read.
|
|
127
|
+
*/
|
|
128
|
+
const MATERIAL_GATE_TOKENS: ReadonlySet<string> = new Set([
|
|
129
|
+
'approval',
|
|
130
|
+
'cutover',
|
|
131
|
+
'gate',
|
|
132
|
+
'irreversible',
|
|
133
|
+
'signoff',
|
|
134
|
+
])
|
|
135
|
+
|
|
136
|
+
const MATERIAL_GATE_PHRASES: readonly string[] = Object.freeze([
|
|
137
|
+
'breaking change',
|
|
138
|
+
'risk assessment',
|
|
139
|
+
'risk review',
|
|
140
|
+
'schema change',
|
|
141
|
+
'sign off',
|
|
142
|
+
])
|
|
143
|
+
|
|
144
|
+
const MATERIAL_PLANNING_TOKENS: ReadonlySet<string> = new Set([
|
|
145
|
+
'deliverable',
|
|
146
|
+
'deliverables',
|
|
147
|
+
'milestone',
|
|
148
|
+
'milestones',
|
|
149
|
+
'plan',
|
|
150
|
+
'planning',
|
|
151
|
+
'roadmap',
|
|
152
|
+
'rollback',
|
|
153
|
+
])
|
|
154
|
+
|
|
155
|
+
const MATERIAL_PLANNING_PHRASES: readonly string[] = Object.freeze([
|
|
156
|
+
'across phases',
|
|
157
|
+
'break down',
|
|
158
|
+
'design doc',
|
|
159
|
+
'implementation plan',
|
|
160
|
+
'migration plan',
|
|
161
|
+
'rollout plan',
|
|
162
|
+
'work breakdown',
|
|
163
|
+
])
|
|
164
|
+
|
|
165
|
+
const ARTIFACT_SKILL_ID = 'artifact-management'
|
|
166
|
+
|
|
109
167
|
const CATALOG_ORDER = new Map(PANTHEON_SKILL_CATALOG.map((entry, index) => [entry.id, index]))
|
|
110
168
|
|
|
111
169
|
function normalize(value: string): string {
|
|
@@ -133,10 +191,53 @@ function scoreSkill(normalizedRequest: string, tokens: ReadonlySet<string>, skil
|
|
|
133
191
|
return score
|
|
134
192
|
}
|
|
135
193
|
|
|
194
|
+
function hasMaterialSignal(
|
|
195
|
+
normalizedRequest: string,
|
|
196
|
+
tokens: ReadonlySet<string>,
|
|
197
|
+
signalTokens: ReadonlySet<string>,
|
|
198
|
+
signalPhrases: readonly string[],
|
|
199
|
+
): boolean {
|
|
200
|
+
for (const token of signalTokens) {
|
|
201
|
+
if (tokens.has(token)) return true
|
|
202
|
+
}
|
|
203
|
+
return signalPhrases.some((phrase) => normalizedRequest.includes(phrase))
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Detect a material gate or planning requirement (D2). A gate requirement wins
|
|
208
|
+
* over a planning one, so the reported token is stable when both are present.
|
|
209
|
+
*/
|
|
210
|
+
function detectMaterialRequirement(
|
|
211
|
+
normalizedRequest: string,
|
|
212
|
+
tokens: ReadonlySet<string>,
|
|
213
|
+
): MaterialRequirement | null {
|
|
214
|
+
if (hasMaterialSignal(normalizedRequest, tokens, MATERIAL_GATE_TOKENS, MATERIAL_GATE_PHRASES)) {
|
|
215
|
+
return 'gate'
|
|
216
|
+
}
|
|
217
|
+
if (
|
|
218
|
+
hasMaterialSignal(
|
|
219
|
+
normalizedRequest,
|
|
220
|
+
tokens,
|
|
221
|
+
MATERIAL_PLANNING_TOKENS,
|
|
222
|
+
MATERIAL_PLANNING_PHRASES,
|
|
223
|
+
)
|
|
224
|
+
) {
|
|
225
|
+
return 'planning'
|
|
226
|
+
}
|
|
227
|
+
return null
|
|
228
|
+
}
|
|
229
|
+
|
|
136
230
|
/**
|
|
137
231
|
* Select a small, deterministic set of allowlisted Pantheon skills for the
|
|
138
232
|
* current request. This function never reads files, memory, transcripts, or
|
|
139
233
|
* the network, and it never returns the whole catalog as a fallback.
|
|
234
|
+
*
|
|
235
|
+
* D2 — when the request materially requires a gate or a plan, the artifact
|
|
236
|
+
* skill is guaranteed a seat instead of depending on the literal keywords
|
|
237
|
+
* `artifact`/`planned`/`multi-phase`/`impl`. That seat is first, so a hard
|
|
238
|
+
* budget drops the lowest-scoring keyword skill rather than the required one.
|
|
239
|
+
* This only ranks identifiers: it does not filter or mutate any prompt, and P2
|
|
240
|
+
* remains an empirical no-op on the default path (F-09).
|
|
140
241
|
*/
|
|
141
242
|
export function selectPantheonSkills(
|
|
142
243
|
request: unknown,
|
|
@@ -150,22 +251,31 @@ export function selectPantheonSkills(
|
|
|
150
251
|
maxSkills < 1 ||
|
|
151
252
|
maxSkills > MAX_SELECTED_SKILLS
|
|
152
253
|
) {
|
|
153
|
-
return { skillIds: [], reasonCode: 'INVALID_INPUT' }
|
|
254
|
+
return { skillIds: [], reasonCode: 'INVALID_INPUT', materialRequirement: null }
|
|
154
255
|
}
|
|
155
256
|
|
|
156
257
|
const normalizedRequest = normalize(request)
|
|
157
258
|
const tokens = tokenize(normalizedRequest)
|
|
259
|
+
const material = detectMaterialRequirement(normalizedRequest, tokens)
|
|
158
260
|
const scored: Array<{ id: string; score: number; order: number }> = []
|
|
159
261
|
for (const skillId of PANTHEON_SKILL_IDS) {
|
|
160
262
|
const score = scoreSkill(normalizedRequest, tokens, skillId)
|
|
161
263
|
if (score <= 0) continue
|
|
162
264
|
scored.push({ id: skillId, score, order: CATALOG_ORDER.get(skillId) ?? 0 })
|
|
163
265
|
}
|
|
164
|
-
if (scored.length === 0)
|
|
266
|
+
if (material === null && scored.length === 0) {
|
|
267
|
+
return { skillIds: [], reasonCode: 'NO_LOCAL_MATCH', materialRequirement: null }
|
|
268
|
+
}
|
|
165
269
|
|
|
166
270
|
scored.sort((left, right) => right.score - left.score || left.order - right.order)
|
|
271
|
+
const ranked = scored.map((entry) => entry.id)
|
|
272
|
+
const ordered =
|
|
273
|
+
material === null
|
|
274
|
+
? ranked
|
|
275
|
+
: [ARTIFACT_SKILL_ID, ...ranked.filter((id) => id !== ARTIFACT_SKILL_ID)]
|
|
167
276
|
return {
|
|
168
|
-
skillIds:
|
|
169
|
-
reasonCode: 'SELECTED_BY_KEYWORDS',
|
|
277
|
+
skillIds: ordered.slice(0, maxSkills),
|
|
278
|
+
reasonCode: material === null ? 'SELECTED_BY_KEYWORDS' : 'SELECTED_BY_MATERIAL_REQUIREMENT',
|
|
279
|
+
materialRequirement: material,
|
|
170
280
|
}
|
|
171
281
|
}
|
|
@@ -8,6 +8,8 @@ import {
|
|
|
8
8
|
loadLocalDecisionPolicy,
|
|
9
9
|
recommendLocalOrchestration,
|
|
10
10
|
} from './local-orchestration.ts'
|
|
11
|
+
import { evaluateComplexityPoint } from './v2-decision-points/p3-complexity.ts'
|
|
12
|
+
import type { ShadowSink } from './v2-decision-shadow.ts'
|
|
11
13
|
|
|
12
14
|
const TOOL_DESCRIPTION =
|
|
13
15
|
'Zeus-only local advisory selector. Use selectively only when specialist routing is genuinely ' +
|
|
@@ -48,6 +50,32 @@ type LocalDecisionToolOptions = {
|
|
|
48
50
|
loadPolicy?: () => Promise<LocalDecisionPolicy | undefined>
|
|
49
51
|
}
|
|
50
52
|
|
|
53
|
+
/**
|
|
54
|
+
* Optional shadow observer for the V2 tool point.
|
|
55
|
+
*
|
|
56
|
+
* P3 declares `tool.invoke`, and the local selector's input is the surface that
|
|
57
|
+
* carries `complexity` and `gateStatus`. The sink is supplied by the plugin when
|
|
58
|
+
* it registers the tool; nothing here applies its record.
|
|
59
|
+
*/
|
|
60
|
+
type LocalDecisionShadowOptions = {
|
|
61
|
+
sink?: ShadowSink
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Observe P3 in shadow at `tool.invoke`.
|
|
66
|
+
*
|
|
67
|
+
* Contained on purpose: the adapter already contains its own failures, and
|
|
68
|
+
* this wrapper guarantees that even an unexpected throw cannot change the
|
|
69
|
+
* tool result. No input is mutated and no decision is applied.
|
|
70
|
+
*/
|
|
71
|
+
function observeToolComplexity(input: unknown, sink?: ShadowSink): void {
|
|
72
|
+
try {
|
|
73
|
+
evaluateComplexityPoint(input, sink)
|
|
74
|
+
} catch {
|
|
75
|
+
// Shadow is best-effort; the tool result is unaffected.
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
51
79
|
async function runDecision(
|
|
52
80
|
input: unknown,
|
|
53
81
|
callerAgent: unknown,
|
|
@@ -108,7 +136,9 @@ const v2InputSchema: Record<string, unknown> = {
|
|
|
108
136
|
}
|
|
109
137
|
|
|
110
138
|
/** Create a V2 transform definition with the same Zeus-only execution guard and local plan. */
|
|
111
|
-
export function createV2LocalDecisionDefinition(
|
|
139
|
+
export function createV2LocalDecisionDefinition(
|
|
140
|
+
options: LocalDecisionToolOptions & LocalDecisionShadowOptions = {},
|
|
141
|
+
) {
|
|
112
142
|
const policyLoader = options.loadPolicy ?? loadLocalDecisionPolicy
|
|
113
143
|
return {
|
|
114
144
|
name: 'pantheon_local_decision',
|
|
@@ -116,6 +146,10 @@ export function createV2LocalDecisionDefinition(options: LocalDecisionToolOption
|
|
|
116
146
|
input: v2InputSchema,
|
|
117
147
|
output: {},
|
|
118
148
|
execute: async (input: Record<string, unknown>, context: unknown) => {
|
|
149
|
+
// P3 shadow observation at `tool.invoke`. The input schema declares
|
|
150
|
+
// `complexity` and `gateStatus`, so this is the one wired surface that
|
|
151
|
+
// can feed a complexity decision; the record never alters the result.
|
|
152
|
+
observeToolComplexity(input, options.sink)
|
|
119
153
|
const callerAgent =
|
|
120
154
|
context !== null && typeof context === 'object'
|
|
121
155
|
? (context as { agent?: unknown }).agent
|
|
@@ -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
|
}
|