opencode-model-router 1.5.0 → 1.6.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/README.md +65 -9
- package/package.json +1 -1
- package/src/commands/output.ts +236 -0
- package/src/guard/enforce.ts +14 -1
- package/src/guard/guards.ts +26 -0
- package/src/guard/store.ts +46 -3
- package/src/index.ts +264 -341
- package/src/router/agent-options.ts +118 -0
- package/src/router/config.ts +277 -43
- package/src/router/idle-sweep.ts +35 -0
- package/src/router/jsonc.ts +1 -0
- package/src/router/prompts.ts +72 -0
- package/src/router/protocol.ts +1 -1
- package/src/router/sessions.ts +159 -22
- package/src/telemetry/trajectory.ts +48 -1
- package/src/verify/checker.ts +47 -1
- package/src/verify/deterministic.ts +13 -5
- package/src/verify/dispatch.ts +27 -2
- package/src/verify/gate.ts +25 -1
- package/src/verify/paths.ts +29 -0
- package/src/verify/timeout.ts +93 -0
- package/src/verify/wiring.ts +267 -0
- package/tiers.json +79 -6
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Registration-path agent options.
|
|
3
|
+
*
|
|
4
|
+
* `src/index.ts` turns each tier of the active preset into an opencode agent
|
|
5
|
+
* definition; this module owns the `options` bag on that definition. It is the
|
|
6
|
+
* only place that knows how a tier's provider-agnostic `effort` maps onto the
|
|
7
|
+
* provider-specific knob (`effort` for Anthropic, `reasoning_effort` for
|
|
8
|
+
* OpenAI) and what to do when a tier asks for something the provider cannot do.
|
|
9
|
+
*
|
|
10
|
+
* The display-path builder in `src/commands/output.ts` renders what a tier is
|
|
11
|
+
* configured with; this one decides what is actually sent. They are kept apart
|
|
12
|
+
* on purpose: `/tiers` should not warn, downgrade or drop keys.
|
|
13
|
+
*/
|
|
14
|
+
import type { TierConfig } from "./config";
|
|
15
|
+
import { EFFORT_LEVELS } from "./config";
|
|
16
|
+
import { isClaudeModel } from "./protocol";
|
|
17
|
+
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
// Warn-once
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
|
|
22
|
+
// Agent registration runs on every `config` hook, so an unqualified
|
|
23
|
+
// console.warn would repeat the same line for the life of the session. Keys are
|
|
24
|
+
// deliberately tight (tier name + offending value where the value matters), so
|
|
25
|
+
// two different mistakes are still two different warnings.
|
|
26
|
+
const warnedAgentOptionsEffort = new Set<string>();
|
|
27
|
+
|
|
28
|
+
export function warnAgentOptionsEffortOnce(key: string, message: string): void {
|
|
29
|
+
if (warnedAgentOptionsEffort.has(key)) return;
|
|
30
|
+
warnedAgentOptionsEffort.add(key);
|
|
31
|
+
console.warn(`[model-router] ${message}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function resetAgentOptionsEffortWarnings(): void {
|
|
35
|
+
warnedAgentOptionsEffort.clear();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function isEffortLevel(value: unknown): value is (typeof EFFORT_LEVELS)[number] {
|
|
39
|
+
return typeof value === "string" && EFFORT_LEVELS.some((level) => level === value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isOpenAIModel(model: string): boolean {
|
|
43
|
+
const s = model.toLowerCase();
|
|
44
|
+
return s.startsWith("openai/") || /\bgpt-/.test(s) || /(^|[/\-_])o[134]([/\-_]|$)/.test(s);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Provider-specific agent options for a tier, as registered with opencode.
|
|
49
|
+
*
|
|
50
|
+
* Precedence, highest first: explicit `thinking` (Anthropic) or explicit
|
|
51
|
+
* `reasoning.effort` (OpenAI), then the provider-agnostic `effort`. A key is
|
|
52
|
+
* only ever present when something asked for it — an unset `effort` leaves no
|
|
53
|
+
* trace in the returned object.
|
|
54
|
+
*/
|
|
55
|
+
export function buildAgentOptions(tier: TierConfig, tierName = tier.model): Record<string, unknown> {
|
|
56
|
+
const opts: Record<string, unknown> = {};
|
|
57
|
+
|
|
58
|
+
// Anthropic thinking config
|
|
59
|
+
if (tier.thinking?.budgetTokens != null) {
|
|
60
|
+
if (tier.thinking.budgetTokens) {
|
|
61
|
+
opts.budget_tokens = tier.thinking.budgetTokens;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// OpenAI reasoning config
|
|
66
|
+
if (tier.reasoning) {
|
|
67
|
+
if (tier.reasoning.effort) {
|
|
68
|
+
opts.reasoning_effort = tier.reasoning.effort;
|
|
69
|
+
}
|
|
70
|
+
if (tier.reasoning.summary) {
|
|
71
|
+
opts.reasoning_summary = tier.reasoning.summary;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const effort: unknown = tier.effort;
|
|
76
|
+
if (effort !== undefined) {
|
|
77
|
+
if (!isEffortLevel(effort)) {
|
|
78
|
+
// Loading tiers.json rejects this, but an override layer or a
|
|
79
|
+
// programmatic caller can still hand us junk. Ignore it rather than
|
|
80
|
+
// failing registration for the whole preset.
|
|
81
|
+
warnAgentOptionsEffortOnce(
|
|
82
|
+
`invalid:${tierName}:${String(effort)}`,
|
|
83
|
+
`tier ${tierName}: invalid effort '${String(effort)}' ignored`,
|
|
84
|
+
);
|
|
85
|
+
} else if (isClaudeModel(tier.model)) {
|
|
86
|
+
if (tier.thinking?.budgetTokens != null) {
|
|
87
|
+
warnAgentOptionsEffortOnce(
|
|
88
|
+
`anthropic-conflict:${tierName}`,
|
|
89
|
+
`tier ${tierName}: both thinking and effort set; explicit thinking wins`,
|
|
90
|
+
);
|
|
91
|
+
} else {
|
|
92
|
+
opts.effort = effort;
|
|
93
|
+
}
|
|
94
|
+
} else if (isOpenAIModel(tier.model)) {
|
|
95
|
+
if (tier.reasoning?.effort) {
|
|
96
|
+
warnAgentOptionsEffortOnce(
|
|
97
|
+
`openai-conflict:${tierName}`,
|
|
98
|
+
`tier ${tierName}: both reasoning.effort and effort set; explicit reasoning wins`,
|
|
99
|
+
);
|
|
100
|
+
} else if (effort === "xhigh" || effort === "max") {
|
|
101
|
+
warnAgentOptionsEffortOnce(
|
|
102
|
+
`openai-downgrade:${tierName}:${effort}`,
|
|
103
|
+
`tier ${tierName}: downgrading effort '${effort}' to 'high' because OpenAI reasoning_effort only supports low, medium, or high`,
|
|
104
|
+
);
|
|
105
|
+
opts.reasoning_effort = "high";
|
|
106
|
+
} else {
|
|
107
|
+
opts.reasoning_effort = effort;
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
warnAgentOptionsEffortOnce(
|
|
111
|
+
`unknown-provider:${tierName}`,
|
|
112
|
+
`tier ${tierName}: effort ignored for unknown provider family '${tier.model}'`,
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return opts;
|
|
118
|
+
}
|
package/src/router/config.ts
CHANGED
|
@@ -24,9 +24,51 @@ export interface ReasoningConfig {
|
|
|
24
24
|
summary?: "auto" | "always" | "never";
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Provider-agnostic reasoning effort for a tier.
|
|
29
|
+
*
|
|
30
|
+
* `xhigh` and `max` exist because Anthropic's adaptive models accept them;
|
|
31
|
+
* OpenAI's `reasoning_effort` stops at `high`, so the registration path
|
|
32
|
+
* downgrades those two with a warning (see `src/router/agent-options.ts`).
|
|
33
|
+
*/
|
|
34
|
+
export const EFFORT_LEVELS = ["low", "medium", "high", "xhigh", "max"] as const;
|
|
35
|
+
|
|
36
|
+
export type EffortLevel = (typeof EFFORT_LEVELS)[number];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Wording style of the default system prompt handed to a tier agent.
|
|
40
|
+
*
|
|
41
|
+
* `prescriptive` uses the enumerated `tierPrompts` from tiers.json; `goal-oriented`
|
|
42
|
+
* uses the goal + constraints defaults in `src/router/prompts.ts`; `auto` (the
|
|
43
|
+
* default when unset) picks goal-oriented for strong models and prescriptive for
|
|
44
|
+
* the rest.
|
|
45
|
+
*/
|
|
46
|
+
export const PROMPT_STYLES = ["prescriptive", "goal-oriented", "auto"] as const;
|
|
47
|
+
|
|
48
|
+
export type PromptStyle = (typeof PROMPT_STYLES)[number];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Shared model-generation pattern lists, matched case-insensitively as substrings
|
|
52
|
+
* of a tier's model ID.
|
|
53
|
+
*/
|
|
54
|
+
export interface ModelGenerationsConfig {
|
|
55
|
+
claude5x?: string[];
|
|
56
|
+
strong?: string[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const DEFAULT_CLAUDE5X_PATTERNS = ["claude-fable-5", "claude-mythos-5"];
|
|
60
|
+
// NOTE: strong is a superset of claude5x by construction — editing DEFAULT_CLAUDE5X_PATTERNS also widens strong. Deliberate: every Claude 5.x model is a strong model.
|
|
61
|
+
export const DEFAULT_STRONG_MODEL_PATTERNS = [...DEFAULT_CLAUDE5X_PATTERNS, "opus-4-8"];
|
|
62
|
+
|
|
27
63
|
export interface TierConfig {
|
|
28
64
|
model: string;
|
|
29
65
|
variant?: string;
|
|
66
|
+
/**
|
|
67
|
+
* Provider-agnostic effort. Loses to an explicit `thinking` budget on
|
|
68
|
+
* Anthropic and to an explicit `reasoning.effort` on OpenAI. When unset, no
|
|
69
|
+
* effort key is registered at all.
|
|
70
|
+
*/
|
|
71
|
+
effort?: EffortLevel;
|
|
30
72
|
thinking?: ThinkingConfig;
|
|
31
73
|
reasoning?: ReasoningConfig;
|
|
32
74
|
costRatio?: number;
|
|
@@ -35,6 +77,11 @@ export interface TierConfig {
|
|
|
35
77
|
description?: string;
|
|
36
78
|
steps?: number;
|
|
37
79
|
prompt?: string;
|
|
80
|
+
/**
|
|
81
|
+
* Wording style of the default prompt for this tier. Ignored when `prompt` is
|
|
82
|
+
* set — an explicit per-tier prompt always wins. Defaults to `auto`.
|
|
83
|
+
*/
|
|
84
|
+
promptStyle?: PromptStyle;
|
|
38
85
|
/** Optional use-case hints shown in `/tiers`. */
|
|
39
86
|
whenToUse?: string[];
|
|
40
87
|
}
|
|
@@ -57,7 +104,13 @@ export interface EnforcementConfig {
|
|
|
57
104
|
envGate?: string;
|
|
58
105
|
perTier?: Record<string, "off" | "advisory" | "enforced">;
|
|
59
106
|
guard?: { readDraftCap?: number; sameOpRetryCap?: number; blockSelfScript?: boolean; deliverableFirst?: boolean; budget?: number; blockScriptWrites?: boolean };
|
|
60
|
-
verify?: { require?: "never" | "whenDoDPresent" | "always"; requireExplicitDoD?: boolean; preferDeterministic?: boolean; graderPolicy?: "atLeastProducerTier"; graderTemperature?: number; minGraderTier?: string
|
|
107
|
+
verify?: { require?: "never" | "whenDoDPresent" | "always"; requireExplicitDoD?: boolean; preferDeterministic?: boolean; graderPolicy?: "atLeastProducerTier"; graderTemperature?: number; minGraderTier?: string | null;
|
|
108
|
+
/** Ceiling for one producer `session.prompt` turn, in ms. Default 600000. */
|
|
109
|
+
delegateTimeoutMs?: number;
|
|
110
|
+
/** Ceiling for one grader `session.prompt` turn, in ms. Default 60000. */
|
|
111
|
+
graderTimeoutMs?: number;
|
|
112
|
+
/** Ceiling for the whole acceptance gate, in ms. Default 90000. */
|
|
113
|
+
gateBudgetMs?: number };
|
|
61
114
|
escalate?: { floorTier?: string | null; ladder?: string[]; maxAttemptsPerTier?: number; maxTotalAttempts?: number; costCeiling?: { base?: string; multiple?: number } };
|
|
62
115
|
proportional?: { trivialBypass?: boolean; trivialClassifier?: string };
|
|
63
116
|
}
|
|
@@ -73,6 +126,14 @@ export interface RouterConfig {
|
|
|
73
126
|
modes?: Record<string, ModeConfig>;
|
|
74
127
|
/** Global default prompts per tier name. A preset-level tier.prompt overrides this. */
|
|
75
128
|
tierPrompts?: Record<string, string>;
|
|
129
|
+
/**
|
|
130
|
+
* Optional user overrides for the goal-oriented tier prompts. The defaults ship
|
|
131
|
+
* in code (`src/router/prompts.ts`); an entry here replaces the built-in for
|
|
132
|
+
* that tier name.
|
|
133
|
+
*/
|
|
134
|
+
tierPromptsGoalOriented?: Record<string, string>;
|
|
135
|
+
/** Shared model-generation pattern lists; see {@link ModelGenerationsConfig}. */
|
|
136
|
+
modelGenerations?: ModelGenerationsConfig;
|
|
76
137
|
/** Read-only tool-call caps per tier, enforced at runtime via tool.execute.after banner injection. */
|
|
77
138
|
tierCaps?: Record<string, number>;
|
|
78
139
|
enforcement?: EnforcementConfig;
|
|
@@ -228,21 +289,17 @@ export function resolvePresetName(
|
|
|
228
289
|
);
|
|
229
290
|
}
|
|
230
291
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const obj = raw as Record<string, unknown>;
|
|
292
|
+
/** True for a non-null, non-array object literal. */
|
|
293
|
+
function isPlainObject(v: unknown): v is Record<string, unknown> {
|
|
294
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
295
|
+
}
|
|
237
296
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
Array.isArray(obj.presets)
|
|
245
|
-
) {
|
|
297
|
+
/**
|
|
298
|
+
* Shape of every preset and every tier within it. Returns the presets map so
|
|
299
|
+
* the caller can hand it to validateActivePreset without re-narrowing.
|
|
300
|
+
*/
|
|
301
|
+
function validatePresets(obj: Record<string, unknown>): Record<string, unknown> {
|
|
302
|
+
if (!isPlainObject(obj.presets)) {
|
|
246
303
|
throw new Error("tiers.json: 'presets' must be a non-null object");
|
|
247
304
|
}
|
|
248
305
|
|
|
@@ -281,9 +338,38 @@ export function validateConfig(raw: unknown): RouterConfig {
|
|
|
281
338
|
`tiers.json: '${presetName}.${tierName}.whenToUse' must be an array`,
|
|
282
339
|
);
|
|
283
340
|
}
|
|
341
|
+
// A typo'd effort would otherwise load clean and be silently dropped at
|
|
342
|
+
// registration time, leaving a tier running at the provider default with
|
|
343
|
+
// only a warning nobody reads.
|
|
344
|
+
if (
|
|
345
|
+
t.effort !== undefined &&
|
|
346
|
+
!EFFORT_LEVELS.some((level) => level === t.effort)
|
|
347
|
+
) {
|
|
348
|
+
throw new Error(
|
|
349
|
+
`tiers.json: preset '${presetName}' tier '${tierName}': effort must be one of ${EFFORT_LEVELS.join(", ")}`,
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
// Same reasoning as effort: a typo'd style would otherwise load clean and
|
|
353
|
+
// silently fall back to the prescriptive prompt with nothing said.
|
|
354
|
+
if (
|
|
355
|
+
t.promptStyle !== undefined &&
|
|
356
|
+
!PROMPT_STYLES.some((style) => style === t.promptStyle)
|
|
357
|
+
) {
|
|
358
|
+
throw new Error(
|
|
359
|
+
`tiers.json: preset '${presetName}' tier '${tierName}': promptStyle must be one of ${PROMPT_STYLES.join("|")}`,
|
|
360
|
+
);
|
|
361
|
+
}
|
|
284
362
|
}
|
|
285
363
|
}
|
|
286
364
|
|
|
365
|
+
return presets;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** `activePreset` names a preset that exists. */
|
|
369
|
+
function validateActivePreset(
|
|
370
|
+
obj: Record<string, unknown>,
|
|
371
|
+
presets: Record<string, unknown>,
|
|
372
|
+
): void {
|
|
287
373
|
// `activePreset` has to name a preset that actually exists. It is the key most
|
|
288
374
|
// likely to be typo'd in a hand-edited override file, and without this the bad
|
|
289
375
|
// name loads clean and routing quietly runs on whatever the state file or the
|
|
@@ -301,7 +387,10 @@ export function validateConfig(raw: unknown): RouterConfig {
|
|
|
301
387
|
`tiers.json: 'activePreset' is '${activePresetName}', which is not a defined preset (defined: ${presetNames.join(", ")})`,
|
|
302
388
|
);
|
|
303
389
|
}
|
|
390
|
+
}
|
|
304
391
|
|
|
392
|
+
/** Top-level keys that are required, or optional with a fixed type. */
|
|
393
|
+
function validateCoreKeys(obj: Record<string, unknown>): void {
|
|
305
394
|
if (!Array.isArray(obj.rules)) {
|
|
306
395
|
throw new Error("tiers.json: 'rules' must be an array of strings");
|
|
307
396
|
}
|
|
@@ -311,14 +400,12 @@ export function validateConfig(raw: unknown): RouterConfig {
|
|
|
311
400
|
if (obj.antiNarration !== undefined && typeof obj.antiNarration !== "boolean") {
|
|
312
401
|
throw new Error("tiers.json: 'antiNarration' must be a boolean");
|
|
313
402
|
}
|
|
403
|
+
}
|
|
314
404
|
|
|
405
|
+
function validateModes(obj: Record<string, unknown>): void {
|
|
315
406
|
// Validate modes if present
|
|
316
407
|
if (obj.modes !== undefined) {
|
|
317
|
-
if (
|
|
318
|
-
typeof obj.modes !== "object" ||
|
|
319
|
-
obj.modes === null ||
|
|
320
|
-
Array.isArray(obj.modes)
|
|
321
|
-
) {
|
|
408
|
+
if (!isPlainObject(obj.modes)) {
|
|
322
409
|
throw new Error("tiers.json: 'modes' must be an object");
|
|
323
410
|
}
|
|
324
411
|
const modes = obj.modes as Record<string, unknown>;
|
|
@@ -339,14 +426,12 @@ export function validateConfig(raw: unknown): RouterConfig {
|
|
|
339
426
|
}
|
|
340
427
|
}
|
|
341
428
|
}
|
|
429
|
+
}
|
|
342
430
|
|
|
431
|
+
function validateTierCaps(obj: Record<string, unknown>): void {
|
|
343
432
|
// Validate tierCaps if present
|
|
344
433
|
if (obj.tierCaps !== undefined) {
|
|
345
|
-
if (
|
|
346
|
-
typeof obj.tierCaps !== "object" ||
|
|
347
|
-
obj.tierCaps === null ||
|
|
348
|
-
Array.isArray(obj.tierCaps)
|
|
349
|
-
) {
|
|
434
|
+
if (!isPlainObject(obj.tierCaps)) {
|
|
350
435
|
throw new Error("tiers.json: 'tierCaps' must be an object");
|
|
351
436
|
}
|
|
352
437
|
const tc = obj.tierCaps as Record<string, unknown>;
|
|
@@ -358,14 +443,12 @@ export function validateConfig(raw: unknown): RouterConfig {
|
|
|
358
443
|
}
|
|
359
444
|
}
|
|
360
445
|
}
|
|
446
|
+
}
|
|
361
447
|
|
|
448
|
+
function validateTierPrompts(obj: Record<string, unknown>): void {
|
|
362
449
|
// Validate tierPrompts if present
|
|
363
450
|
if (obj.tierPrompts !== undefined) {
|
|
364
|
-
if (
|
|
365
|
-
typeof obj.tierPrompts !== "object" ||
|
|
366
|
-
obj.tierPrompts === null ||
|
|
367
|
-
Array.isArray(obj.tierPrompts)
|
|
368
|
-
) {
|
|
451
|
+
if (!isPlainObject(obj.tierPrompts)) {
|
|
369
452
|
throw new Error("tiers.json: 'tierPrompts' must be an object");
|
|
370
453
|
}
|
|
371
454
|
const tp = obj.tierPrompts as Record<string, unknown>;
|
|
@@ -377,14 +460,49 @@ export function validateConfig(raw: unknown): RouterConfig {
|
|
|
377
460
|
}
|
|
378
461
|
}
|
|
379
462
|
}
|
|
463
|
+
}
|
|
380
464
|
|
|
465
|
+
function validateTierPromptsGoalOriented(obj: Record<string, unknown>): void {
|
|
466
|
+
// Validate tierPromptsGoalOriented if present
|
|
467
|
+
if (obj.tierPromptsGoalOriented !== undefined) {
|
|
468
|
+
if (!isPlainObject(obj.tierPromptsGoalOriented)) {
|
|
469
|
+
throw new Error("tiers.json: 'tierPromptsGoalOriented' must be an object");
|
|
470
|
+
}
|
|
471
|
+
const tp = obj.tierPromptsGoalOriented as Record<string, unknown>;
|
|
472
|
+
for (const [tierName, prompt] of Object.entries(tp)) {
|
|
473
|
+
if (typeof prompt !== "string") {
|
|
474
|
+
throw new Error(
|
|
475
|
+
`tiers.json: tierPromptsGoalOriented.'${tierName}' must be a string`,
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
function validateModelGenerations(obj: Record<string, unknown>): void {
|
|
483
|
+
// Validate modelGenerations if present. Element-level non-strings are tolerated
|
|
484
|
+
// and filtered at match time rather than rejected here, so one bad entry in an
|
|
485
|
+
// override file cannot drop the whole layer.
|
|
486
|
+
if (obj.modelGenerations !== undefined) {
|
|
487
|
+
if (!isPlainObject(obj.modelGenerations)) {
|
|
488
|
+
throw new Error("tiers.json: modelGenerations must be an object");
|
|
489
|
+
}
|
|
490
|
+
const modelGenerations = obj.modelGenerations as Record<string, unknown>;
|
|
491
|
+
for (const key of ["claude5x", "strong"] as const) {
|
|
492
|
+
if (
|
|
493
|
+
modelGenerations[key] !== undefined &&
|
|
494
|
+
!Array.isArray(modelGenerations[key])
|
|
495
|
+
) {
|
|
496
|
+
throw new Error(`tiers.json: modelGenerations.${key} must be an array`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function validateTaskPatterns(obj: Record<string, unknown>): void {
|
|
381
503
|
// Validate taskPatterns if present
|
|
382
504
|
if (obj.taskPatterns !== undefined) {
|
|
383
|
-
if (
|
|
384
|
-
typeof obj.taskPatterns !== "object" ||
|
|
385
|
-
obj.taskPatterns === null ||
|
|
386
|
-
Array.isArray(obj.taskPatterns)
|
|
387
|
-
) {
|
|
505
|
+
if (!isPlainObject(obj.taskPatterns)) {
|
|
388
506
|
throw new Error("tiers.json: 'taskPatterns' must be an object");
|
|
389
507
|
}
|
|
390
508
|
const tp = obj.taskPatterns as Record<string, unknown>;
|
|
@@ -396,14 +514,12 @@ export function validateConfig(raw: unknown): RouterConfig {
|
|
|
396
514
|
}
|
|
397
515
|
}
|
|
398
516
|
}
|
|
517
|
+
}
|
|
399
518
|
|
|
519
|
+
function validateEnforcement(obj: Record<string, unknown>): void {
|
|
400
520
|
// Validate enforcement if present (optional — absent means no enforcement)
|
|
401
521
|
if (obj.enforcement !== undefined) {
|
|
402
|
-
if (
|
|
403
|
-
typeof obj.enforcement !== "object" ||
|
|
404
|
-
obj.enforcement === null ||
|
|
405
|
-
Array.isArray(obj.enforcement)
|
|
406
|
-
) {
|
|
522
|
+
if (!isPlainObject(obj.enforcement)) {
|
|
407
523
|
throw new Error("tiers.json: enforcement must be an object");
|
|
408
524
|
}
|
|
409
525
|
const enforcement = obj.enforcement as Record<string, unknown>;
|
|
@@ -414,6 +530,13 @@ export function validateConfig(raw: unknown): RouterConfig {
|
|
|
414
530
|
);
|
|
415
531
|
}
|
|
416
532
|
}
|
|
533
|
+
if (enforcement.envGate !== undefined) {
|
|
534
|
+
if (typeof enforcement.envGate !== "string" || !enforcement.envGate) {
|
|
535
|
+
throw new Error(
|
|
536
|
+
"tiers.json: enforcement.envGate must be a non-empty string",
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
417
540
|
if (
|
|
418
541
|
enforcement.verify !== undefined &&
|
|
419
542
|
typeof enforcement.verify === "object" &&
|
|
@@ -428,6 +551,53 @@ export function validateConfig(raw: unknown): RouterConfig {
|
|
|
428
551
|
'tiers.json: enforcement.verify.graderPolicy must be "atLeastProducerTier"',
|
|
429
552
|
);
|
|
430
553
|
}
|
|
554
|
+
// `null` is the shipped default and means "no floor" — same as absent.
|
|
555
|
+
if (
|
|
556
|
+
verify.minGraderTier !== undefined &&
|
|
557
|
+
verify.minGraderTier !== null &&
|
|
558
|
+
typeof verify.minGraderTier !== "string"
|
|
559
|
+
) {
|
|
560
|
+
throw new Error(
|
|
561
|
+
"tiers.json: enforcement.verify.minGraderTier must be a string or null",
|
|
562
|
+
);
|
|
563
|
+
}
|
|
564
|
+
if (verify.graderTemperature !== undefined) {
|
|
565
|
+
if (
|
|
566
|
+
typeof verify.graderTemperature !== "number" ||
|
|
567
|
+
!Number.isFinite(verify.graderTemperature) ||
|
|
568
|
+
verify.graderTemperature < 0
|
|
569
|
+
) {
|
|
570
|
+
throw new Error(
|
|
571
|
+
"tiers.json: enforcement.verify.graderTemperature must be a number >= 0",
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
// Time-box ceilings. `>= 1` and not `>= 0`: a 0 or negative budget is
|
|
576
|
+
// almost always meant as "no timeout", and silently reading it as an
|
|
577
|
+
// immediately-expiring one would make every delegation fail. Reject it
|
|
578
|
+
// at the config boundary and say so, rather than guessing.
|
|
579
|
+
for (const key of [
|
|
580
|
+
"delegateTimeoutMs",
|
|
581
|
+
"graderTimeoutMs",
|
|
582
|
+
"gateBudgetMs",
|
|
583
|
+
] as const) {
|
|
584
|
+
const value = verify[key];
|
|
585
|
+
if (value !== undefined) {
|
|
586
|
+
if (!Number.isInteger(value) || (value as number) < 1) {
|
|
587
|
+
throw new Error(
|
|
588
|
+
`tiers.json: enforcement.verify.${key} must be an integer >= 1 (milliseconds)`,
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
if (
|
|
594
|
+
verify.requireExplicitDoD !== undefined &&
|
|
595
|
+
typeof verify.requireExplicitDoD !== "boolean"
|
|
596
|
+
) {
|
|
597
|
+
throw new Error(
|
|
598
|
+
"tiers.json: enforcement.verify.requireExplicitDoD must be a boolean",
|
|
599
|
+
);
|
|
600
|
+
}
|
|
431
601
|
}
|
|
432
602
|
if (
|
|
433
603
|
enforcement.escalate !== undefined &&
|
|
@@ -529,8 +699,75 @@ export function validateConfig(raw: unknown): RouterConfig {
|
|
|
529
699
|
throw new Error("enforcement.guard.blockScriptWrites must be a boolean");
|
|
530
700
|
}
|
|
531
701
|
}
|
|
702
|
+
for (const key of ["readDraftCap", "sameOpRetryCap"] as const) {
|
|
703
|
+
if (guard[key] !== undefined) {
|
|
704
|
+
if (
|
|
705
|
+
typeof guard[key] !== "number" ||
|
|
706
|
+
!Number.isInteger(guard[key]) ||
|
|
707
|
+
(guard[key] as number) < 0
|
|
708
|
+
) {
|
|
709
|
+
throw new Error(
|
|
710
|
+
`tiers.json: enforcement.guard.${key} must be an integer >= 0`,
|
|
711
|
+
);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
for (const key of ["blockSelfScript", "deliverableFirst"] as const) {
|
|
716
|
+
if (guard[key] !== undefined && typeof guard[key] !== "boolean") {
|
|
717
|
+
throw new Error(
|
|
718
|
+
`tiers.json: enforcement.guard.${key} must be a boolean`,
|
|
719
|
+
);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
if (
|
|
724
|
+
enforcement.proportional !== undefined &&
|
|
725
|
+
typeof enforcement.proportional === "object" &&
|
|
726
|
+
enforcement.proportional !== null
|
|
727
|
+
) {
|
|
728
|
+
const proportional = enforcement.proportional as Record<string, unknown>;
|
|
729
|
+
if (
|
|
730
|
+
proportional.trivialBypass !== undefined &&
|
|
731
|
+
typeof proportional.trivialBypass !== "boolean"
|
|
732
|
+
) {
|
|
733
|
+
throw new Error(
|
|
734
|
+
"tiers.json: enforcement.proportional.trivialBypass must be a boolean",
|
|
735
|
+
);
|
|
736
|
+
}
|
|
532
737
|
}
|
|
533
738
|
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Validate a raw parsed config. Strict and throwing by design: the bundled
|
|
743
|
+
* tiers.json must be valid on its own, and loadConfig turns a throw from an
|
|
744
|
+
* override layer into a warning plus a fallback rather than a crash.
|
|
745
|
+
*
|
|
746
|
+
* Section order matters and is preserved from when this was one function: a
|
|
747
|
+
* config with several problems reports the same first error it always did.
|
|
748
|
+
*/
|
|
749
|
+
export function validateConfig(raw: unknown): RouterConfig {
|
|
750
|
+
if (typeof raw !== "object" || raw === null) {
|
|
751
|
+
throw new Error("tiers.json: expected a JSON object at root");
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
const obj = raw as Record<string, unknown>;
|
|
756
|
+
|
|
757
|
+
if (typeof obj.activePreset !== "string" || !obj.activePreset) {
|
|
758
|
+
throw new Error("tiers.json: 'activePreset' must be a non-empty string");
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
const presets = validatePresets(obj);
|
|
762
|
+
validateActivePreset(obj, presets);
|
|
763
|
+
validateCoreKeys(obj);
|
|
764
|
+
validateModes(obj);
|
|
765
|
+
validateTierCaps(obj);
|
|
766
|
+
validateTierPrompts(obj);
|
|
767
|
+
validateTierPromptsGoalOriented(obj);
|
|
768
|
+
validateModelGenerations(obj);
|
|
769
|
+
validateTaskPatterns(obj);
|
|
770
|
+
validateEnforcement(obj);
|
|
534
771
|
|
|
535
772
|
return raw as RouterConfig;
|
|
536
773
|
}
|
|
@@ -542,9 +779,6 @@ export function validateConfig(raw: unknown): RouterConfig {
|
|
|
542
779
|
* override are skipped so they never blow away a base value.
|
|
543
780
|
*/
|
|
544
781
|
export function deepMerge(base: unknown, override: unknown): unknown {
|
|
545
|
-
const isPlainObject = (v: unknown): v is Record<string, unknown> =>
|
|
546
|
-
typeof v === "object" && v !== null && !Array.isArray(v);
|
|
547
|
-
|
|
548
782
|
if (!isPlainObject(base) || !isPlainObject(override)) {
|
|
549
783
|
return override;
|
|
550
784
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export const DEFAULT_IDLE_TTL_MS = 60 * 60_000;
|
|
2
|
+
export const IDLE_SWEEP_THROTTLE_MS = 5 * 60_000;
|
|
3
|
+
|
|
4
|
+
export type SweepFn = () => void;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Creates a small coordinator for opportunistic idle-TTL sweeps. It is intended
|
|
8
|
+
* to be invoked from an existing hook path; no timers are scheduled.
|
|
9
|
+
*
|
|
10
|
+
* Isolation: each sweeper runs inside its own try/catch, so a throwing sweeper
|
|
11
|
+
* neither propagates out of the coordinator nor prevents the remaining sweepers
|
|
12
|
+
* from running. A throttle window is consumed even when a sweeper throws.
|
|
13
|
+
*/
|
|
14
|
+
export function createIdleTtlSweeper(
|
|
15
|
+
sweepers: SweepFn[],
|
|
16
|
+
throttleMs = IDLE_SWEEP_THROTTLE_MS,
|
|
17
|
+
): (nowMs?: number) => boolean {
|
|
18
|
+
let lastSweepMs: number | null = null;
|
|
19
|
+
|
|
20
|
+
return (nowMs = Date.now()): boolean => {
|
|
21
|
+
if (lastSweepMs !== null && nowMs - lastSweepMs < throttleMs) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
lastSweepMs = nowMs;
|
|
26
|
+
for (const sweep of sweepers) {
|
|
27
|
+
try {
|
|
28
|
+
sweep();
|
|
29
|
+
} catch {
|
|
30
|
+
// A failing sweeper must not stop the others (best-effort maintenance).
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
};
|
|
35
|
+
}
|
package/src/router/jsonc.ts
CHANGED
|
@@ -46,6 +46,7 @@ export function stripJsonc(input: string): string {
|
|
|
46
46
|
if (c === "/" && input[i + 1] === "*") {
|
|
47
47
|
i += 2;
|
|
48
48
|
while (i < n && !(input[i] === "*" && input[i + 1] === "/")) i++;
|
|
49
|
+
if (i >= n) throw new Error("jsonc: unterminated block comment");
|
|
49
50
|
i += 2; // skip the closing */
|
|
50
51
|
continue;
|
|
51
52
|
}
|