opencode-model-router 2.3.0 → 2.4.0
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 +149 -0
- package/README.md +56 -10
- package/package.json +1 -1
- package/src/commands/output.ts +21 -1
- package/src/compat/v2-hooks.ts +1322 -18
- package/src/escalate/ladder.ts +12 -0
- package/src/guard/enforce.ts +186 -22
- package/src/guard/guards.ts +132 -6
- package/src/guard/store.ts +5 -0
- package/src/index.ts +718 -29
- package/src/router/config.ts +247 -1
- package/src/router/dispatch-header.ts +46 -4
- package/src/router/git-tools.ts +1152 -0
- package/src/router/guard-profile.ts +100 -0
- package/src/router/plugin-agents.ts +433 -0
- package/src/router/prompts.ts +26 -1
- package/src/router/protocol.ts +133 -0
- package/src/router/read-only.ts +149 -0
- package/src/router/role-agents.ts +409 -0
- package/src/router/roles-config.ts +429 -0
- package/src/router/roles.ts +600 -0
- package/src/router/run-tools.ts +913 -0
- package/src/router/sensitive-paths.ts +101 -0
- package/src/router/sessions.ts +92 -0
- package/src/router/subagents.ts +28 -1
- package/src/routing/advisor/findings.ts +193 -1
- package/src/routing/advisor/index.ts +13 -5
- package/src/routing/classify/index.ts +23 -5
- package/src/routing/classify/route-line.ts +67 -3
- package/src/routing/classify/rules.ts +80 -20
- package/src/routing/classify/types.ts +34 -4
- package/src/routing/commands/stats.ts +3 -0
- package/src/routing/engine/kernel.ts +163 -0
- package/src/routing/engine/ladders.ts +242 -1
- package/src/routing/engine/protocol-line.ts +17 -0
- package/src/routing/engine/roles-taxonomy.ts +52 -0
- package/src/routing/engine/types.ts +99 -1
- package/src/routing/outcomes/beta.ts +10 -4
- package/src/routing/outcomes/index.ts +44 -1
- package/src/routing/outcomes/ingest.ts +163 -15
- package/src/routing/outcomes/persist.ts +23 -1
- package/src/routing/outcomes/signals.ts +650 -0
- package/src/routing/outcomes/stats.ts +273 -2
- package/src/routing/outcomes/store.ts +25 -5
- package/src/routing/outcomes/types.ts +145 -6
- package/src/routing/roles/authority.ts +495 -0
- package/src/routing/roles/binding.ts +543 -0
- package/src/routing/roles/policy.ts +416 -0
- package/src/routing/roles/work-root.ts +132 -0
- package/src/routing/wire/dispatch.ts +1306 -17
- package/src/routing/wire/hint.ts +14 -0
- package/src/routing/wire/runtime.ts +117 -28
- package/src/v2.ts +26 -1
- package/src/verify/checker.ts +249 -7
- package/src/verify/deterministic.ts +10 -1
- package/src/verify/dispatch.ts +63 -6
- package/src/verify/dod.ts +127 -10
- package/src/verify/gate.ts +75 -7
- package/src/verify/types.ts +12 -0
- package/src/verify/wiring.ts +55 -10
- package/tiers.json +13 -5
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation of the roles-and-assurance configuration keys (issue #84, plan P1.1).
|
|
3
|
+
*
|
|
4
|
+
* Every sanitiser here is total: a bad value never throws. It drops only the key or entry it
|
|
5
|
+
* belongs to and reports a {@link RolesIssue}, so a typo in `roleAgents.reviewer.budget` can
|
|
6
|
+
* never cost a layer or the whole `routing` block (the #80 lesson, applied to the new keys only).
|
|
7
|
+
* The caller turns issues into config notices.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { AuthorityAction, ExplorationConfig, RoleSpec, RunConfig } from "./roles";
|
|
11
|
+
|
|
12
|
+
export interface RolesIssue {
|
|
13
|
+
/** Config path of the offending key or entry, e.g. `routing.run.commands.build`. */
|
|
14
|
+
path: string;
|
|
15
|
+
message: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Sanitized<T> {
|
|
19
|
+
value: T | undefined;
|
|
20
|
+
issues: RolesIssue[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const DELEGATION_MODES = ["tiers", "roles"] as const;
|
|
24
|
+
export type DelegationMode = (typeof DELEGATION_MODES)[number];
|
|
25
|
+
|
|
26
|
+
export const EXPLORATION_MAX_RATE = 0.2;
|
|
27
|
+
export const DEFAULT_RUN_SCRIPTS: readonly string[] = ["test", "typecheck", "lint", "build"];
|
|
28
|
+
/**
|
|
29
|
+
* Default `routing.run.commands` (#84, P1.3 handoff): package.json scripts take no caller
|
|
30
|
+
* arguments, so a scoped test run needs a command entry. `npm run test -- <files>` goes through
|
|
31
|
+
* router_run's hardened npm path (npm-cli.js of the node install, pinned script shell). The
|
|
32
|
+
* patterns admit `test/...` paths and `--maxWorkers=N` only; confinement (no `..` segment, no
|
|
33
|
+
* absolute or drive path, option-like leads `-`/`@`/`+` only against a pattern with the same
|
|
34
|
+
* lead) is enforced by router_run itself (P1.3). A user `routing.run.commands` replaces it.
|
|
35
|
+
*/
|
|
36
|
+
export const DEFAULT_RUN_COMMANDS: RunConfig["commands"] = Object.freeze({
|
|
37
|
+
"test-files": Object.freeze({
|
|
38
|
+
argv: Object.freeze(["npm", "run", "test", "--"]),
|
|
39
|
+
args: Object.freeze(["test/*", "--maxWorkers=*"]),
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
export const DEFAULT_RUN_TIMEOUT_MS = 600_000;
|
|
43
|
+
export const RUN_TIMEOUT_BOUNDS = { min: 1_000, max: 3_600_000 } as const;
|
|
44
|
+
export const ROLES_V1_NOTICE = "roles delegation requires OpenCode v2; using tiers";
|
|
45
|
+
|
|
46
|
+
const AUTHORITY_ACTION_LIST = [
|
|
47
|
+
"read", "glob", "grep", "router_git", "router_run", "edit", "webfetch", "websearch", "context7", "execute",
|
|
48
|
+
] as const satisfies readonly AuthorityAction[];
|
|
49
|
+
type MissingActions = Exclude<AuthorityAction, (typeof AUTHORITY_ACTION_LIST)[number]>;
|
|
50
|
+
const _allActionsListed: [MissingActions] extends [never] ? true : never = true;
|
|
51
|
+
void _allActionsListed;
|
|
52
|
+
export const AUTHORITY_ACTIONS: readonly AuthorityAction[] = AUTHORITY_ACTION_LIST;
|
|
53
|
+
|
|
54
|
+
/** Customisable fields of a role agent. `codeModeAllow` was removed (R6); it is reported, not read. */
|
|
55
|
+
export interface RoleAgentCustomisation {
|
|
56
|
+
enabled?: boolean;
|
|
57
|
+
description?: string;
|
|
58
|
+
prompt?: string;
|
|
59
|
+
tierRange?: { floor?: string; ceiling?: string };
|
|
60
|
+
budget?: Record<string, number>;
|
|
61
|
+
deny?: AuthorityAction[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const FORBIDDEN_NAMES = new Set(["__proto__", "constructor", "prototype"]);
|
|
65
|
+
const AGENT_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
|
|
66
|
+
const COMMAND_NAME = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,63}$/;
|
|
67
|
+
const SCRIPT_NAME = /^[A-Za-z0-9][A-Za-z0-9_.-]*(?::[A-Za-z0-9_.-]+)*$/;
|
|
68
|
+
const MAX_DESCRIPTION = 1_000;
|
|
69
|
+
const MAX_PROMPT = 20_000;
|
|
70
|
+
|
|
71
|
+
function isRecord(v: unknown): v is Record<string, unknown> {
|
|
72
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function show(v: unknown): string {
|
|
76
|
+
let s: string;
|
|
77
|
+
try {
|
|
78
|
+
s = typeof v === "string" ? JSON.stringify(v) : (JSON.stringify(v) ?? String(v));
|
|
79
|
+
} catch {
|
|
80
|
+
s = `<${typeof v}>`;
|
|
81
|
+
}
|
|
82
|
+
return s.length <= 60 ? s : `${s.slice(0, 59)}…`;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function sanitizeDelegation(value: unknown): Sanitized<DelegationMode> {
|
|
86
|
+
if (value === undefined) return { value: undefined, issues: [] };
|
|
87
|
+
const hit = DELEGATION_MODES.find((m) => m === value);
|
|
88
|
+
if (hit !== undefined) return { value: hit, issues: [] };
|
|
89
|
+
return {
|
|
90
|
+
value: undefined,
|
|
91
|
+
issues: [{ path: "routing.delegation", message: `routing.delegation must be tiers|roles (got ${show(value)}); using tiers` }],
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function sanitizeExploration(value: unknown): Sanitized<Partial<ExplorationConfig>> {
|
|
96
|
+
const issues: RolesIssue[] = [];
|
|
97
|
+
if (value === undefined) return { value: undefined, issues };
|
|
98
|
+
if (!isRecord(value)) {
|
|
99
|
+
issues.push({ path: "routing.exploration", message: `routing.exploration must be an object (got ${show(value)}); ignored` });
|
|
100
|
+
return { value: undefined, issues };
|
|
101
|
+
}
|
|
102
|
+
const out: Partial<ExplorationConfig> = { requireDetection: "deterministic" };
|
|
103
|
+
const rate = value.rate;
|
|
104
|
+
if (rate !== undefined) {
|
|
105
|
+
if (typeof rate === "number" && Number.isFinite(rate) && rate >= 0 && rate <= EXPLORATION_MAX_RATE) out.rate = rate;
|
|
106
|
+
else {
|
|
107
|
+
issues.push({
|
|
108
|
+
path: "routing.exploration.rate",
|
|
109
|
+
message: `routing.exploration.rate must be a number from 0 to ${EXPLORATION_MAX_RATE} (got ${show(rate)}); using 0`,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (value.requireDetection !== undefined && value.requireDetection !== "deterministic") {
|
|
114
|
+
issues.push({
|
|
115
|
+
path: "routing.exploration.requireDetection",
|
|
116
|
+
message: `routing.exploration.requireDetection is fixed to "deterministic" (got ${show(value.requireDetection)}); ignored`,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return { value: out, issues };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function sanitizeRun(value: unknown): Sanitized<Partial<RunConfig>> {
|
|
123
|
+
const issues: RolesIssue[] = [];
|
|
124
|
+
if (value === undefined) return { value: undefined, issues };
|
|
125
|
+
if (!isRecord(value)) {
|
|
126
|
+
issues.push({ path: "routing.run", message: `routing.run must be an object (got ${show(value)}); ignored` });
|
|
127
|
+
return { value: undefined, issues };
|
|
128
|
+
}
|
|
129
|
+
const out: { scripts?: string[]; commands?: RunConfig["commands"]; timeoutMs?: number } = {};
|
|
130
|
+
|
|
131
|
+
if (value.scripts !== undefined) {
|
|
132
|
+
if (!Array.isArray(value.scripts)) {
|
|
133
|
+
issues.push({ path: "routing.run.scripts", message: `routing.run.scripts must be an array of script names (got ${show(value.scripts)}); using the defaults` });
|
|
134
|
+
} else {
|
|
135
|
+
const scripts: string[] = [];
|
|
136
|
+
value.scripts.forEach((s: unknown, i) => {
|
|
137
|
+
if (typeof s === "string" && SCRIPT_NAME.test(s) && !FORBIDDEN_NAMES.has(s)) {
|
|
138
|
+
if (!scripts.includes(s)) scripts.push(s);
|
|
139
|
+
} else {
|
|
140
|
+
issues.push({ path: `routing.run.scripts[${i}]`, message: `routing.run.scripts[${i}] is not a valid script name (got ${show(s)}); entry dropped` });
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
out.scripts = scripts;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (value.commands !== undefined) {
|
|
148
|
+
if (!isRecord(value.commands)) {
|
|
149
|
+
issues.push({ path: "routing.run.commands", message: `routing.run.commands must be an object (got ${show(value.commands)}); ignored` });
|
|
150
|
+
} else {
|
|
151
|
+
const commands: Record<string, { argv: readonly string[]; args?: readonly string[] }> = {};
|
|
152
|
+
for (const [name, entry] of Object.entries(value.commands)) {
|
|
153
|
+
const path = `routing.run.commands.${name}`;
|
|
154
|
+
if (!COMMAND_NAME.test(name) || FORBIDDEN_NAMES.has(name)) {
|
|
155
|
+
issues.push({ path, message: `${path}: the command name is not valid; entry dropped` });
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (!isRecord(entry)) {
|
|
159
|
+
issues.push({ path, message: `${path} must be an object with argv (got ${show(entry)}); entry dropped` });
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
const argv = entry.argv;
|
|
163
|
+
if (!Array.isArray(argv) || argv.length === 0 || !argv.every((a): a is string => typeof a === "string" && a !== "")) {
|
|
164
|
+
issues.push({ path: `${path}.argv`, message: `${path}.argv must be a non-empty array of non-empty strings; entry dropped` });
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const cmd: { argv: readonly string[]; args?: readonly string[] } = { argv: [...argv] };
|
|
168
|
+
if (entry.args !== undefined) {
|
|
169
|
+
if (Array.isArray(entry.args) && entry.args.every((a): a is string => typeof a === "string")) {
|
|
170
|
+
cmd.args = [...entry.args];
|
|
171
|
+
} else {
|
|
172
|
+
issues.push({ path: `${path}.args`, message: `${path}.args must be an array of strings; args ignored` });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
commands[name] = cmd;
|
|
176
|
+
}
|
|
177
|
+
out.commands = commands;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (value.timeoutMs !== undefined) {
|
|
182
|
+
const t = value.timeoutMs;
|
|
183
|
+
if (typeof t === "number" && Number.isInteger(t) && t >= RUN_TIMEOUT_BOUNDS.min && t <= RUN_TIMEOUT_BOUNDS.max) out.timeoutMs = t;
|
|
184
|
+
else {
|
|
185
|
+
issues.push({
|
|
186
|
+
path: "routing.run.timeoutMs",
|
|
187
|
+
message: `routing.run.timeoutMs must be an integer from ${RUN_TIMEOUT_BOUNDS.min} to ${RUN_TIMEOUT_BOUNDS.max} (got ${show(t)}); using ${DEFAULT_RUN_TIMEOUT_MS}`,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return { value: out, issues };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Why `entry` is not an acceptable work root, or undefined. An entry needs an absolute static
|
|
196
|
+
* prefix with at least one real path segment (so `*`, `/**`, `D:/**` are refused) in its
|
|
197
|
+
* canonical long form: a segment like `PROGRA~1` is an 8.3 short name that can alias another
|
|
198
|
+
* directory (R6/P-11).
|
|
199
|
+
*/
|
|
200
|
+
export function workRootProblem(entry: string): string | undefined {
|
|
201
|
+
if (entry === "") return "is empty";
|
|
202
|
+
const globAt = entry.search(/[*?[\]{}]/);
|
|
203
|
+
const prefix = globAt === -1 ? entry : entry.slice(0, globAt);
|
|
204
|
+
const normalised = prefix.replace(/\\/g, "/");
|
|
205
|
+
let rest: string;
|
|
206
|
+
if (/^[A-Za-z]:\//.test(normalised)) rest = normalised.slice(3);
|
|
207
|
+
else if (/^\/\/[^/]+\/[^/]+/.test(normalised)) rest = normalised.replace(/^\/\/[^/]+\/[^/]+/, "");
|
|
208
|
+
else if (normalised.startsWith("/") && !normalised.startsWith("//")) rest = normalised.slice(1);
|
|
209
|
+
else return "has no absolute static prefix";
|
|
210
|
+
// A segment cut by the first glob character is not static: `/a/b*` has static prefix `/a/`.
|
|
211
|
+
const segments = rest.split("/");
|
|
212
|
+
if (globAt !== -1) segments.pop();
|
|
213
|
+
const real = segments.filter((s) => s !== "");
|
|
214
|
+
if (real.length === 0) return "has no static directory before its first wildcard";
|
|
215
|
+
const all = entry.split(/[\\/]/);
|
|
216
|
+
if (all.some((s) => s === ".." || s === ".")) return "contains a . or .. segment";
|
|
217
|
+
// Every segment, wildcard ones and those after the first wildcard included (QA-P11-1-6).
|
|
218
|
+
if (all.some((s) => /~\d/.test(s))) return "contains an 8.3 short-name component; use the long form of the path";
|
|
219
|
+
return undefined;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function sanitizeWorkRoots(value: unknown): Sanitized<string[]> {
|
|
223
|
+
const issues: RolesIssue[] = [];
|
|
224
|
+
if (value === undefined) return { value: undefined, issues };
|
|
225
|
+
if (!Array.isArray(value)) {
|
|
226
|
+
issues.push({ path: "routing.workRoots", message: `routing.workRoots must be an array of absolute globs (got ${show(value)}); ignored` });
|
|
227
|
+
return { value: undefined, issues };
|
|
228
|
+
}
|
|
229
|
+
const out: string[] = [];
|
|
230
|
+
value.forEach((entry: unknown, i) => {
|
|
231
|
+
const path = `routing.workRoots[${i}]`;
|
|
232
|
+
if (typeof entry !== "string") {
|
|
233
|
+
issues.push({ path, message: `${path} must be a string (got ${show(entry)}); entry dropped` });
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const problem = workRootProblem(entry);
|
|
237
|
+
if (problem !== undefined) {
|
|
238
|
+
issues.push({ path, message: `${path} ${show(entry)} ${problem}; entry dropped` });
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (!out.includes(entry)) out.push(entry);
|
|
242
|
+
});
|
|
243
|
+
return { value: out, issues };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function sanitizeRoleAgents(value: unknown): Sanitized<Record<string, RoleAgentCustomisation>> {
|
|
247
|
+
const issues: RolesIssue[] = [];
|
|
248
|
+
if (value === undefined) return { value: undefined, issues };
|
|
249
|
+
if (!isRecord(value)) {
|
|
250
|
+
issues.push({ path: "roleAgents", message: `roleAgents must be an object (got ${show(value)}); ignored` });
|
|
251
|
+
return { value: undefined, issues };
|
|
252
|
+
}
|
|
253
|
+
const out: Record<string, RoleAgentCustomisation> = {};
|
|
254
|
+
for (const [name, raw] of Object.entries(value)) {
|
|
255
|
+
const base = `roleAgents.${name}`;
|
|
256
|
+
if (!AGENT_NAME.test(name) || name.includes("..") || FORBIDDEN_NAMES.has(name)) {
|
|
257
|
+
issues.push({ path: base, message: `${base}: the agent name is not valid; entry dropped` });
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (!isRecord(raw)) {
|
|
261
|
+
issues.push({ path: base, message: `${base} must be an object (got ${show(raw)}); entry dropped` });
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
const c: RoleAgentCustomisation = {};
|
|
265
|
+
for (const key of Object.keys(raw)) {
|
|
266
|
+
if (!["enabled", "description", "prompt", "tierRange", "budget", "deny"].includes(key)) {
|
|
267
|
+
issues.push({
|
|
268
|
+
path: `${base}.${key}`,
|
|
269
|
+
message: key === "codeModeAllow"
|
|
270
|
+
? `${base}.codeModeAllow is not supported (removed); ignored`
|
|
271
|
+
: `${base}.${key} is not a customisable field; ignored`,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (raw.enabled !== undefined) {
|
|
276
|
+
if (typeof raw.enabled === "boolean") c.enabled = raw.enabled;
|
|
277
|
+
else issues.push({ path: `${base}.enabled`, message: `${base}.enabled must be a boolean (got ${show(raw.enabled)}); ignored` });
|
|
278
|
+
}
|
|
279
|
+
for (const [key, max] of [["description", MAX_DESCRIPTION], ["prompt", MAX_PROMPT]] as const) {
|
|
280
|
+
const v = raw[key];
|
|
281
|
+
if (v === undefined) continue;
|
|
282
|
+
if (typeof v === "string" && v.trim() !== "" && v.length <= max) c[key] = v;
|
|
283
|
+
else issues.push({ path: `${base}.${key}`, message: `${base}.${key} must be a non-empty string of at most ${max} characters; ignored` });
|
|
284
|
+
}
|
|
285
|
+
if (raw.tierRange !== undefined) {
|
|
286
|
+
const tr = raw.tierRange;
|
|
287
|
+
const range: { floor?: string; ceiling?: string } = {};
|
|
288
|
+
if (isRecord(tr)) {
|
|
289
|
+
for (const k of ["floor", "ceiling"] as const) {
|
|
290
|
+
const v = tr[k];
|
|
291
|
+
if (v === undefined) continue;
|
|
292
|
+
if (typeof v === "string" && v !== "") range[k] = v;
|
|
293
|
+
else issues.push({ path: `${base}.tierRange.${k}`, message: `${base}.tierRange.${k} must be a tier name (got ${show(v)}); ignored` });
|
|
294
|
+
}
|
|
295
|
+
if (range.floor !== undefined || range.ceiling !== undefined) c.tierRange = range;
|
|
296
|
+
} else {
|
|
297
|
+
issues.push({ path: `${base}.tierRange`, message: `${base}.tierRange must be an object (got ${show(tr)}); ignored` });
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (raw.budget !== undefined) {
|
|
301
|
+
if (isRecord(raw.budget)) {
|
|
302
|
+
const budget: Record<string, number> = {};
|
|
303
|
+
for (const [k, v] of Object.entries(raw.budget)) {
|
|
304
|
+
if (FORBIDDEN_NAMES.has(k)) continue;
|
|
305
|
+
if (typeof v === "number" && Number.isFinite(v) && v > 0) budget[k] = v;
|
|
306
|
+
else issues.push({ path: `${base}.budget.${k}`, message: `${base}.budget.${k} must be a number > 0 (got ${show(v)}); entry dropped` });
|
|
307
|
+
}
|
|
308
|
+
c.budget = budget;
|
|
309
|
+
} else {
|
|
310
|
+
issues.push({ path: `${base}.budget`, message: `${base}.budget must be an object (got ${show(raw.budget)}); ignored` });
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (raw.deny !== undefined) {
|
|
314
|
+
if (Array.isArray(raw.deny)) {
|
|
315
|
+
const deny: AuthorityAction[] = [];
|
|
316
|
+
for (const a of raw.deny as unknown[]) {
|
|
317
|
+
const hit = AUTHORITY_ACTIONS.find((x) => x === a);
|
|
318
|
+
if (hit === undefined) issues.push({ path: `${base}.deny`, message: `${base}.deny entry ${show(a)} is not an action (${AUTHORITY_ACTIONS.join("|")}); entry dropped` });
|
|
319
|
+
else if (!deny.includes(hit)) deny.push(hit);
|
|
320
|
+
}
|
|
321
|
+
c.deny = deny;
|
|
322
|
+
} else {
|
|
323
|
+
issues.push({ path: `${base}.deny`, message: `${base}.deny must be an array of actions (got ${show(raw.deny)}); ignored` });
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
out[name] = c;
|
|
327
|
+
}
|
|
328
|
+
return { value: out, issues };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Applies a user's customisation to a shipped spec. Narrowing only: the tier range stays inside
|
|
333
|
+
* the shipped one (clamped, with an issue), `deny` can only remove actions, `enabled: true` cannot
|
|
334
|
+
* revive a disabled role, and a budget is within (0, 2 x shipped]. `tierOrder` lists tier names
|
|
335
|
+
* from cheapest to dearest. Never mutates `spec`.
|
|
336
|
+
*/
|
|
337
|
+
export function narrowRoleSpec(
|
|
338
|
+
spec: RoleSpec,
|
|
339
|
+
custom: RoleAgentCustomisation | undefined,
|
|
340
|
+
tierOrder: readonly string[],
|
|
341
|
+
): { spec: RoleSpec; issues: RolesIssue[] } {
|
|
342
|
+
const issues: RolesIssue[] = [];
|
|
343
|
+
if (custom === undefined) return { spec, issues };
|
|
344
|
+
const base = `roleAgents.${spec.agent}`;
|
|
345
|
+
const next: RoleSpec = {
|
|
346
|
+
...spec,
|
|
347
|
+
authority: { ...spec.authority, allow: [...spec.authority.allow], deny: [...spec.authority.deny] },
|
|
348
|
+
tierRange: { ...spec.tierRange },
|
|
349
|
+
budget: { ...spec.budget },
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
if (custom.enabled === false) next.enabled = false;
|
|
353
|
+
else if (custom.enabled === true && !spec.enabled) {
|
|
354
|
+
issues.push({ path: `${base}.enabled`, message: `${base}.enabled: true cannot enable a role that is disabled by default; ignored` });
|
|
355
|
+
}
|
|
356
|
+
if (custom.description !== undefined) next.description = custom.description;
|
|
357
|
+
if (custom.prompt !== undefined) next.prompt = custom.prompt;
|
|
358
|
+
|
|
359
|
+
if (custom.tierRange !== undefined) {
|
|
360
|
+
const lo = tierOrder.indexOf(spec.tierRange.floor);
|
|
361
|
+
const hi = tierOrder.indexOf(spec.tierRange.ceiling);
|
|
362
|
+
if (lo === -1 || hi === -1) {
|
|
363
|
+
issues.push({ path: `${base}.tierRange`, message: `${base}.tierRange: the shipped range is not in the active preset; ignored` });
|
|
364
|
+
} else {
|
|
365
|
+
const resolve = (key: "floor" | "ceiling"): number | undefined => {
|
|
366
|
+
const name = custom.tierRange?.[key];
|
|
367
|
+
if (name === undefined) return undefined;
|
|
368
|
+
const idx = tierOrder.indexOf(name);
|
|
369
|
+
if (idx === -1) {
|
|
370
|
+
issues.push({ path: `${base}.tierRange.${key}`, message: `${base}.tierRange.${key} ${show(name)} is not a tier (${tierOrder.join("|")}); ignored` });
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
373
|
+
const clamped = Math.min(Math.max(idx, lo), hi);
|
|
374
|
+
if (clamped !== idx) {
|
|
375
|
+
issues.push({
|
|
376
|
+
path: `${base}.tierRange.${key}`,
|
|
377
|
+
message: `${base}.tierRange.${key} ${show(name)} is outside the shipped range ${spec.tierRange.floor}..${spec.tierRange.ceiling}; clamped to ${tierOrder[clamped]}`,
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
return clamped;
|
|
381
|
+
};
|
|
382
|
+
const f = resolve("floor") ?? lo;
|
|
383
|
+
const c = resolve("ceiling") ?? hi;
|
|
384
|
+
if (f > c) {
|
|
385
|
+
issues.push({ path: `${base}.tierRange`, message: `${base}.tierRange floor is above its ceiling; ignored` });
|
|
386
|
+
} else {
|
|
387
|
+
next.tierRange = { floor: tierOrder[f]!, ceiling: tierOrder[c]! };
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (custom.deny !== undefined && custom.deny.length > 0) {
|
|
393
|
+
const denied = new Set(custom.deny);
|
|
394
|
+
next.authority = {
|
|
395
|
+
...next.authority,
|
|
396
|
+
allow: next.authority.allow.filter((a) => !denied.has(a)),
|
|
397
|
+
deny: [...new Set([...next.authority.deny, ...custom.deny])],
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (custom.budget !== undefined) {
|
|
402
|
+
const budget: Record<string, number> = { ...spec.budget };
|
|
403
|
+
for (const [k, v] of Object.entries(custom.budget)) {
|
|
404
|
+
const shipped = Object.hasOwn(spec.budget, k) ? spec.budget[k] : undefined;
|
|
405
|
+
if (shipped === undefined) {
|
|
406
|
+
issues.push({ path: `${base}.budget.${k}`, message: `${base}.budget.${k} is not a budget of this role; entry dropped` });
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (v > 2 * shipped) {
|
|
410
|
+
issues.push({ path: `${base}.budget.${k}`, message: `${base}.budget.${k} ${v} is above twice the shipped ${shipped}; clamped to ${2 * shipped}` });
|
|
411
|
+
budget[k] = 2 * shipped;
|
|
412
|
+
} else budget[k] = v;
|
|
413
|
+
}
|
|
414
|
+
next.budget = budget;
|
|
415
|
+
}
|
|
416
|
+
return { spec: next, issues };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Every issue of the new keys in a merged raw config (`routing` + root `roleAgents`). */
|
|
420
|
+
export function collectRolesIssues(rawRouting: unknown, rawRoleAgents: unknown): RolesIssue[] {
|
|
421
|
+
const r = isRecord(rawRouting) ? rawRouting : {};
|
|
422
|
+
return [
|
|
423
|
+
...sanitizeDelegation(r.delegation).issues,
|
|
424
|
+
...sanitizeExploration(r.exploration).issues,
|
|
425
|
+
...sanitizeRun(r.run).issues,
|
|
426
|
+
...sanitizeWorkRoots(r.workRoots).issues,
|
|
427
|
+
...sanitizeRoleAgents(rawRoleAgents).issues,
|
|
428
|
+
];
|
|
429
|
+
}
|