opencode-model-router 1.14.0 → 2.0.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 +173 -0
- package/README.md +80 -28
- package/package.json +8 -3
- package/server.ts +3 -0
- package/src/compat/child-session.ts +27 -0
- package/src/compat/v2-client.ts +183 -0
- package/src/compat/v2-hooks.ts +376 -0
- package/src/index.ts +427 -85
- package/src/router/config.ts +225 -15
- package/src/router/protocol.ts +12 -2
- package/src/v2.ts +24 -0
- package/src/verify/baseline.ts +143 -25
- package/src/verify/batch.ts +2226 -0
- package/src/verify/checker.ts +1 -1
- package/src/verify/deterministic.ts +1378 -20
- package/src/verify/directives.ts +207 -0
- package/src/verify/dispatch.ts +387 -90
- package/src/verify/dod.ts +8 -1
- package/src/verify/exec.ts +654 -35
- package/src/verify/gate.ts +16 -1
- package/src/verify/paths.ts +9 -1
- package/src/verify/pending.ts +1834 -0
- package/src/verify/reference.ts +2117 -0
- package/src/verify/risk.ts +271 -0
- package/src/verify/runner.ts +5315 -0
- package/src/verify/slot.ts +1603 -0
- package/src/verify/tree.ts +215 -11
- package/src/verify/types.ts +301 -4
- package/src/verify/wiring.ts +1959 -52
- package/tiers.json +21 -19
package/src/router/config.ts
CHANGED
|
@@ -6,11 +6,12 @@ import {
|
|
|
6
6
|
renameSync,
|
|
7
7
|
writeFileSync,
|
|
8
8
|
} from "node:fs";
|
|
9
|
-
import { homedir } from "node:os";
|
|
9
|
+
import { availableParallelism, homedir } from "node:os";
|
|
10
10
|
import { dirname, join } from "node:path";
|
|
11
11
|
import { fileURLToPath } from "node:url";
|
|
12
12
|
import { parseJsonc } from "./jsonc";
|
|
13
13
|
import type { DelegateInstructionsPolicy } from "./instructions";
|
|
14
|
+
import type { PluginLogger } from "./logger";
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Filename of the optional user overrides file (global and project copies share
|
|
@@ -123,19 +124,49 @@ export interface EnforcementConfig {
|
|
|
123
124
|
envGate?: string;
|
|
124
125
|
perTier?: Record<string, "off" | "advisory" | "enforced">;
|
|
125
126
|
guard?: { readDraftCap?: number; sameOpRetryCap?: number; blockSelfScript?: boolean; deliverableFirst?: boolean; budget?: number; blockScriptWrites?: boolean };
|
|
126
|
-
verify?: { require?: "never" | "whenDoDPresent" | "always"; requireExplicitDoD?: boolean; preferDeterministic?: boolean; graderPolicy?: "atLeastProducerTier"; graderTemperature?: number; minGraderTier?: string | null;
|
|
127
|
+
verify?: { require?: "never" | "whenDoDPresent" | "always"; requireExplicitDoD?: boolean; preferDeterministic?: boolean; graderPolicy?: "atLeastProducerTier"; graderTemperature?: number | null; graderTemperatureModels?: string[]; minGraderTier?: string | null;
|
|
127
128
|
/** Ceiling for one producer `session.prompt` turn, in ms. Default 600000. */
|
|
128
129
|
delegateTimeoutMs?: number;
|
|
129
130
|
/** Reject unavailable verification. Default false; never escalates it. */
|
|
130
131
|
strictUnverifiable?: boolean;
|
|
131
|
-
/**
|
|
132
|
+
/** Deprecated: `false` maps to `failureRecheck: false` (explicit `failureRecheck` wins). No default. */
|
|
132
133
|
testBaseline?: boolean;
|
|
133
|
-
/**
|
|
134
|
+
/** Bounds the whole git-only reference capture, in ms. Default 15000. */
|
|
134
135
|
baselineTimeoutMs?: number;
|
|
135
136
|
/** Override tier ceilings: fast 60000 / medium 180000 / heavy 600000 ms. */
|
|
136
137
|
graderTimeoutMs?: number;
|
|
137
|
-
/**
|
|
138
|
-
gateBudgetMs?: number
|
|
138
|
+
/** Deadline for every synchronous verification, in ms. Default 90000. */
|
|
139
|
+
gateBudgetMs?: number;
|
|
140
|
+
/** Which tests a verification runs. Default "affected". */
|
|
141
|
+
testScope?: "affected" | "full";
|
|
142
|
+
/** Worker cap passed to runners that support one (integer >= 1). Default 2. */
|
|
143
|
+
maxWorkers?: number;
|
|
144
|
+
/** Run verification commands at below-normal priority. Default true. */
|
|
145
|
+
lowPriority?: boolean;
|
|
146
|
+
/** Machine-wide verification slots (integer >= 1). Default max(1, floor(cores / 8)). */
|
|
147
|
+
maxConcurrentVerifications?: number;
|
|
148
|
+
/** Mode for dispatches without a `VERIFY:` directive. Default "deferred". */
|
|
149
|
+
defaultVerify?: "deferred" | "required";
|
|
150
|
+
/** Longest wait for the reference capture before the producer starts, in ms (0 = never wait). Default 5000. */
|
|
151
|
+
captureWaitMs?: number;
|
|
152
|
+
/** Also run deferred verifications in the background. Default false. */
|
|
153
|
+
background?: boolean;
|
|
154
|
+
/** How long an unverified delegation stays verifiable, in ms (integer >= 1). Default 3600000. */
|
|
155
|
+
pendingTtlMs?: number;
|
|
156
|
+
/**
|
|
157
|
+
* Maximum wait for a verification slot, in ms (0 = no wait). Default 60000.
|
|
158
|
+
*
|
|
159
|
+
* Residual (QA-1.4-21): a lock whose owner is not provably dead is reclaimed
|
|
160
|
+
* only by a caller that waits or stays alive through ~8 s of observation, so
|
|
161
|
+
* a very short wait may give up on a slot that a longer one would reclaim.
|
|
162
|
+
*/
|
|
163
|
+
slotWaitMs?: number;
|
|
164
|
+
/** Coalescing window, in ms (0 = no batching). Default 2000. */
|
|
165
|
+
batchWindowMs?: number;
|
|
166
|
+
/** Capture a reference and recheck scoped failures against it. Default true. */
|
|
167
|
+
failureRecheck?: boolean;
|
|
168
|
+
/** Budget for the reference re-run, in ms (integer >= 1). Default 60000. */
|
|
169
|
+
recheckTimeoutMs?: number };
|
|
139
170
|
escalate?: { floorTier?: string | null; ladder?: string[]; maxAttemptsPerTier?: number; maxTotalAttempts?: number; costCeiling?: { base?: string; multiple?: number } };
|
|
140
171
|
proportional?: { trivialBypass?: boolean; trivialClassifier?: string };
|
|
141
172
|
}
|
|
@@ -673,6 +704,12 @@ function validateTaskPatterns(obj: Record<string, unknown>): void {
|
|
|
673
704
|
}
|
|
674
705
|
}
|
|
675
706
|
|
|
707
|
+
/**
|
|
708
|
+
* Largest delay `setTimeout` honours (2^31 - 1 ms). Node and bun clamp any
|
|
709
|
+
* larger delay to 1 ms, turning a "huge" budget into an immediate timeout.
|
|
710
|
+
*/
|
|
711
|
+
const MAX_TIMER_MS = 2_147_483_647;
|
|
712
|
+
|
|
676
713
|
function validateEnforcement(obj: Record<string, unknown>): void {
|
|
677
714
|
// Validate enforcement if present (optional — absent means no enforcement)
|
|
678
715
|
if (obj.enforcement !== undefined) {
|
|
@@ -694,12 +731,20 @@ function validateEnforcement(obj: Record<string, unknown>): void {
|
|
|
694
731
|
);
|
|
695
732
|
}
|
|
696
733
|
}
|
|
697
|
-
if (
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
enforcement.verify
|
|
701
|
-
|
|
734
|
+
if (enforcement.verify !== undefined) {
|
|
735
|
+
// A non-object (including `null`) would skip every check below, and an
|
|
736
|
+
// override `verify: null` would erase the whole bundled block on merge.
|
|
737
|
+
if (!isPlainObject(enforcement.verify)) {
|
|
738
|
+
throw new Error("tiers.json: enforcement.verify must be an object");
|
|
739
|
+
}
|
|
702
740
|
const verify = enforcement.verify as Record<string, unknown>;
|
|
741
|
+
// An own `__proto__`/`constructor`/`prototype` key is never read, but a
|
|
742
|
+
// later `Object.assign` copy would reparent through it; reject it here.
|
|
743
|
+
for (const key of ["__proto__", "constructor", "prototype"] as const) {
|
|
744
|
+
if (Object.prototype.hasOwnProperty.call(verify, key)) {
|
|
745
|
+
throw new Error(`tiers.json: enforcement.verify must not contain the key "${key}"`);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
703
748
|
if (verify.testBaseline !== undefined && typeof verify.testBaseline !== "boolean") {
|
|
704
749
|
throw new Error("tiers.json: enforcement.verify.testBaseline must be a boolean");
|
|
705
750
|
}
|
|
@@ -724,17 +769,28 @@ function validateEnforcement(obj: Record<string, unknown>): void {
|
|
|
724
769
|
"tiers.json: enforcement.verify.minGraderTier must be a string or null",
|
|
725
770
|
);
|
|
726
771
|
}
|
|
727
|
-
if (verify.graderTemperature !== undefined) {
|
|
772
|
+
if (verify.graderTemperature !== undefined && verify.graderTemperature !== null) {
|
|
728
773
|
if (
|
|
729
774
|
typeof verify.graderTemperature !== "number" ||
|
|
730
775
|
!Number.isFinite(verify.graderTemperature) ||
|
|
731
776
|
verify.graderTemperature < 0
|
|
732
777
|
) {
|
|
733
778
|
throw new Error(
|
|
734
|
-
"tiers.json: enforcement.verify.graderTemperature must be a number >= 0",
|
|
779
|
+
"tiers.json: enforcement.verify.graderTemperature must be a number >= 0 or null",
|
|
735
780
|
);
|
|
736
781
|
}
|
|
737
782
|
}
|
|
783
|
+
if (verify.graderTemperatureModels !== undefined) {
|
|
784
|
+
if (!Array.isArray(verify.graderTemperatureModels)) {
|
|
785
|
+
throw new Error("tiers.json: enforcement.verify.graderTemperatureModels must be an array");
|
|
786
|
+
}
|
|
787
|
+
for (const entry of verify.graderTemperatureModels) {
|
|
788
|
+
const slash = typeof entry === "string" ? entry.indexOf("/") : -1;
|
|
789
|
+
if (typeof entry !== "string" || slash < 1 || !entry.slice(0, slash).trim() || !entry.slice(slash + 1).trim()) {
|
|
790
|
+
throw new Error("tiers.json: enforcement.verify.graderTemperatureModels entries must be non-empty provider/model strings");
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
}
|
|
738
794
|
// Time-box ceilings. `>= 1` and not `>= 0`: a 0 or negative budget is
|
|
739
795
|
// almost always meant as "no timeout", and silently reading it as an
|
|
740
796
|
// immediately-expiring one would make every delegation fail. Reject it
|
|
@@ -744,16 +800,66 @@ function validateEnforcement(obj: Record<string, unknown>): void {
|
|
|
744
800
|
"graderTimeoutMs",
|
|
745
801
|
"gateBudgetMs",
|
|
746
802
|
"baselineTimeoutMs",
|
|
803
|
+
"pendingTtlMs",
|
|
804
|
+
"recheckTimeoutMs",
|
|
747
805
|
] as const) {
|
|
748
806
|
const value = verify[key];
|
|
749
807
|
if (value !== undefined) {
|
|
750
|
-
if (
|
|
808
|
+
if (
|
|
809
|
+
!Number.isInteger(value) ||
|
|
810
|
+
(value as number) < 1 ||
|
|
811
|
+
(value as number) > MAX_TIMER_MS
|
|
812
|
+
) {
|
|
751
813
|
throw new Error(
|
|
752
|
-
`tiers.json: enforcement.verify.${key} must be an integer >= 1 (milliseconds)`,
|
|
814
|
+
`tiers.json: enforcement.verify.${key} must be an integer >= 1 and <= ${MAX_TIMER_MS} (milliseconds)`,
|
|
753
815
|
);
|
|
754
816
|
}
|
|
755
817
|
}
|
|
756
818
|
}
|
|
819
|
+
// Waits/windows where 0 is meaningful ("no wait", "no batching").
|
|
820
|
+
for (const key of ["captureWaitMs", "slotWaitMs", "batchWindowMs"] as const) {
|
|
821
|
+
const value = verify[key];
|
|
822
|
+
if (
|
|
823
|
+
value !== undefined &&
|
|
824
|
+
(!Number.isInteger(value) || (value as number) < 0 || (value as number) > MAX_TIMER_MS)
|
|
825
|
+
) {
|
|
826
|
+
throw new Error(
|
|
827
|
+
`tiers.json: enforcement.verify.${key} must be an integer >= 0 and <= ${MAX_TIMER_MS} (milliseconds)`,
|
|
828
|
+
);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
// Safe integers only: `1e21` passes `Number.isInteger` but stringifies to
|
|
832
|
+
// `1e+21`, which no test runner parses as a worker count.
|
|
833
|
+
for (const key of ["maxWorkers", "maxConcurrentVerifications"] as const) {
|
|
834
|
+
const value = verify[key];
|
|
835
|
+
if (value !== undefined && (!Number.isSafeInteger(value) || (value as number) < 1)) {
|
|
836
|
+
throw new Error(`tiers.json: enforcement.verify.${key} must be an integer >= 1`);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
for (const key of ["lowPriority", "background", "failureRecheck"] as const) {
|
|
840
|
+
const value = verify[key];
|
|
841
|
+
if (value !== undefined && typeof value !== "boolean") {
|
|
842
|
+
throw new Error(`tiers.json: enforcement.verify.${key} must be a boolean`);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
if (
|
|
846
|
+
verify.testScope !== undefined &&
|
|
847
|
+
verify.testScope !== "affected" &&
|
|
848
|
+
verify.testScope !== "full"
|
|
849
|
+
) {
|
|
850
|
+
throw new Error(
|
|
851
|
+
'tiers.json: enforcement.verify.testScope must be "affected" or "full"',
|
|
852
|
+
);
|
|
853
|
+
}
|
|
854
|
+
if (
|
|
855
|
+
verify.defaultVerify !== undefined &&
|
|
856
|
+
verify.defaultVerify !== "deferred" &&
|
|
857
|
+
verify.defaultVerify !== "required"
|
|
858
|
+
) {
|
|
859
|
+
throw new Error(
|
|
860
|
+
'tiers.json: enforcement.verify.defaultVerify must be "deferred" or "required"',
|
|
861
|
+
);
|
|
862
|
+
}
|
|
757
863
|
if (
|
|
758
864
|
verify.requireExplicitDoD !== undefined &&
|
|
759
865
|
typeof verify.requireExplicitDoD !== "boolean"
|
|
@@ -1169,3 +1275,107 @@ export function normalizeEnforcement(
|
|
|
1169
1275
|
): { mode: "off" | "advisory" | "enforced" } {
|
|
1170
1276
|
return { mode: e?.mode ?? "advisory" };
|
|
1171
1277
|
}
|
|
1278
|
+
|
|
1279
|
+
/** Fully defaulted verification budget (§1.4). */
|
|
1280
|
+
export interface VerifyBudget {
|
|
1281
|
+
testScope: "affected" | "full";
|
|
1282
|
+
maxWorkers: number;
|
|
1283
|
+
lowPriority: boolean;
|
|
1284
|
+
maxConcurrentVerifications: number;
|
|
1285
|
+
defaultVerify: "deferred" | "required";
|
|
1286
|
+
captureWaitMs: number;
|
|
1287
|
+
background: boolean;
|
|
1288
|
+
pendingTtlMs: number;
|
|
1289
|
+
slotWaitMs: number;
|
|
1290
|
+
batchWindowMs: number;
|
|
1291
|
+
failureRecheck: boolean;
|
|
1292
|
+
recheckTimeoutMs: number;
|
|
1293
|
+
baselineTimeoutMs: number;
|
|
1294
|
+
gateBudgetMs: number;
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
export interface ResolveVerifyBudgetOptions {
|
|
1298
|
+
/** Core count; defaults to `os.availableParallelism()`. Injected by tests. */
|
|
1299
|
+
cores?: number;
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
let warnedTestBaselineDeprecated = false;
|
|
1303
|
+
|
|
1304
|
+
/** Test-only: re-arm the once-per-process `testBaseline` deprecation warning. */
|
|
1305
|
+
export function resetVerifyBudgetWarnings(): void {
|
|
1306
|
+
warnedTestBaselineDeprecated = false;
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
/**
|
|
1310
|
+
* Logs the once-per-process deprecation warning for `enforcement.verify.testBaseline`
|
|
1311
|
+
* (any value) through the plugin logger. Call once after every `loadConfig()`.
|
|
1312
|
+
* Kept apart from `resolveVerifyBudget` so that function stays pure.
|
|
1313
|
+
*/
|
|
1314
|
+
export function warnDeprecatedVerifyKeys(
|
|
1315
|
+
cfg: RouterConfig | undefined,
|
|
1316
|
+
logger: PluginLogger,
|
|
1317
|
+
): void {
|
|
1318
|
+
const v = cfg?.enforcement?.verify;
|
|
1319
|
+
if (
|
|
1320
|
+
warnedTestBaselineDeprecated ||
|
|
1321
|
+
!isPlainObject(v) ||
|
|
1322
|
+
!Object.prototype.hasOwnProperty.call(v, "testBaseline") ||
|
|
1323
|
+
v.testBaseline === undefined
|
|
1324
|
+
) {
|
|
1325
|
+
return;
|
|
1326
|
+
}
|
|
1327
|
+
warnedTestBaselineDeprecated = true;
|
|
1328
|
+
logger.warn(
|
|
1329
|
+
"enforcement.verify.testBaseline is deprecated; use enforcement.verify.failureRecheck",
|
|
1330
|
+
{ key: "testBaseline" },
|
|
1331
|
+
);
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
/**
|
|
1335
|
+
* The single place verification-budget defaults are applied. Pure and
|
|
1336
|
+
* synchronous: no module state, no logging (see `warnDeprecatedVerifyKeys`).
|
|
1337
|
+
* Reads own properties only, so a value inherited through a prototype is never
|
|
1338
|
+
* applied. A non-finite or `< 1` core count (including `Infinity`) counts as 1.
|
|
1339
|
+
*/
|
|
1340
|
+
export function resolveVerifyBudget(
|
|
1341
|
+
cfg: RouterConfig | undefined,
|
|
1342
|
+
opts: ResolveVerifyBudgetOptions = {},
|
|
1343
|
+
): VerifyBudget {
|
|
1344
|
+
const raw = cfg?.enforcement?.verify;
|
|
1345
|
+
const v: Record<string, unknown> = isPlainObject(raw) ? raw : {};
|
|
1346
|
+
const own = <T>(key: string): T | undefined =>
|
|
1347
|
+
Object.prototype.hasOwnProperty.call(v, key) && v[key] !== undefined
|
|
1348
|
+
? (v[key] as T)
|
|
1349
|
+
: undefined;
|
|
1350
|
+
|
|
1351
|
+
const testBaseline = own<boolean>("testBaseline");
|
|
1352
|
+
|
|
1353
|
+
const failureRecheck =
|
|
1354
|
+
own<boolean>("failureRecheck") ?? (testBaseline === false ? false : true);
|
|
1355
|
+
|
|
1356
|
+
const cores = opts.cores ?? availableParallelism();
|
|
1357
|
+
const coreCount = Number.isFinite(cores) && cores >= 1 ? Math.floor(cores) : 1;
|
|
1358
|
+
|
|
1359
|
+
// QA-1.6-8: waiting for the reference capture longer than the capture itself
|
|
1360
|
+
// may take is pointless, so the wait never exceeds baselineTimeoutMs.
|
|
1361
|
+
const baselineTimeoutMs = own<number>("baselineTimeoutMs") ?? 15_000;
|
|
1362
|
+
const captureWaitMs = Math.min(own<number>("captureWaitMs") ?? 5000, baselineTimeoutMs);
|
|
1363
|
+
|
|
1364
|
+
return {
|
|
1365
|
+
testScope: own<"affected" | "full">("testScope") ?? "affected",
|
|
1366
|
+
maxWorkers: own<number>("maxWorkers") ?? 2,
|
|
1367
|
+
lowPriority: own<boolean>("lowPriority") ?? true,
|
|
1368
|
+
maxConcurrentVerifications:
|
|
1369
|
+
own<number>("maxConcurrentVerifications") ?? Math.max(1, Math.floor(coreCount / 8)),
|
|
1370
|
+
defaultVerify: own<"deferred" | "required">("defaultVerify") ?? "deferred",
|
|
1371
|
+
captureWaitMs,
|
|
1372
|
+
background: own<boolean>("background") ?? false,
|
|
1373
|
+
pendingTtlMs: own<number>("pendingTtlMs") ?? 3_600_000,
|
|
1374
|
+
slotWaitMs: own<number>("slotWaitMs") ?? 60_000,
|
|
1375
|
+
batchWindowMs: own<number>("batchWindowMs") ?? 2000,
|
|
1376
|
+
failureRecheck,
|
|
1377
|
+
recheckTimeoutMs: own<number>("recheckTimeoutMs") ?? 60_000,
|
|
1378
|
+
baselineTimeoutMs,
|
|
1379
|
+
gateBudgetMs: own<number>("gateBudgetMs") ?? 90_000,
|
|
1380
|
+
};
|
|
1381
|
+
}
|
package/src/router/protocol.ts
CHANGED
|
@@ -264,6 +264,13 @@ export const CLAUDE_ANTI_NARRATION = [
|
|
|
264
264
|
// Assembled system prompt (pure — no side effects)
|
|
265
265
|
// ---------------------------------------------------------------------------
|
|
266
266
|
|
|
267
|
+
/**
|
|
268
|
+
* Description of the `delegate` tool. It is not parsed as dispatch text, so it shows the literal,
|
|
269
|
+
* working `VERIFY:required` form (QA-2.3-3).
|
|
270
|
+
*/
|
|
271
|
+
export const DELEGATE_TOOL_DESCRIPTION =
|
|
272
|
+
"Delegate a task to a tier subagent (fast | medium | heavy). Required-mode delegations and DoDs without testsPass are verified independently (deterministic checks, or a grader at >= the producer tier in a fresh session) before return: an accepted result on PASS, an honest 'unmet' status on FAIL. A DoD containing testsPass is deferred by default as a whole (its build, lint, run and criteria checks too) and returns UNVERIFIED with a `[router] unverified \u00b7 vrf_\u2026` footer, a risk level and a handle; verify it with router_verify, or put `VERIFY:required` in the task for a synchronous gate. Optionally pass an [acceptance]...[/acceptance] block to define the Definition of Done.";
|
|
273
|
+
|
|
267
274
|
/**
|
|
268
275
|
* Builds the DoD / Acceptance block protocol section shown when enforcement is ON.
|
|
269
276
|
* Pure: no side-effects, no I/O.
|
|
@@ -275,7 +282,7 @@ export function buildDoDProtocolSection(cfg: RouterConfig): string {
|
|
|
275
282
|
: "If you omit the block, a minimal DoD is auto-inferred from the task type.";
|
|
276
283
|
return [
|
|
277
284
|
"### Acceptance / Definition of Done (enforcement is ON)",
|
|
278
|
-
"Non-trivial delegations are
|
|
285
|
+
"Non-trivial delegations are verified independently (producer \u2260 grader; grader \u2265 producer tier). Required-mode delegations and DoDs without testsPass are gated before return; a DoD containing testsPass defers by default as a whole (its build, lint, run and criteria checks too) and returns unverified with a `[router] unverified \u00b7 vrf_\u2026` footer, a risk level and a handle. Attach an acceptance block to your dispatch so the gate knows what \"done\" means:",
|
|
279
286
|
"",
|
|
280
287
|
"[acceptance]",
|
|
281
288
|
"check: testsPass",
|
|
@@ -287,7 +294,10 @@ export function buildDoDProtocolSection(cfg: RouterConfig): string {
|
|
|
287
294
|
"[/acceptance]",
|
|
288
295
|
"",
|
|
289
296
|
"- check kinds: testsPass | buildPasses | lintClean | fileExists path=\u2026 | schemaMatch path=\u2026 schema=\u2026 | run command=\"\u2026\" expect=\u2026",
|
|
290
|
-
"-
|
|
297
|
+
"- cwd: <dir> in the block when the producer works outside this directory (e.g. a git worktree); checks, tests and the grader run there.",
|
|
298
|
+
"- testsPass runs only the tests affected by the producer's changes (the full suite is CI's job); prefer it over a hand-written full-suite run command. A failure that also fails at the dispatch-time reference is excused as pre-existing.",
|
|
299
|
+
"- Per dispatch you may add `VERIFY:<mode>` as one token, <mode> being `required` or `deferred` (default deferred: returns at once with a handle and a risk level) and `VERIFY_WAIT:<n>s`. Pick required when later work depends on this delegation, or call router_verify before building on a medium/high-risk deferred result; unverified delegations stay listed in the prompt until verified or expired (1 h, or a restart); absent from the list does not mean verified.",
|
|
300
|
+
"- Command allowlist (first-token basename): npm, npx, pnpm, yarn, bun, node, tsc, tsx, vitest, jest, eslint, prettier, pytest (plus exactly `uv run pytest`). No shell chaining, redirection, substitution or newlines; interpreter inline-eval/print flags are forbidden. buildPasses probes a build script, then root tsconfig.json (npx tsc --noEmit). Unavailable checks produce acceptance caveats, not producer escalation; strictUnverifiable restores rejection.",
|
|
291
301
|
"- " + omitLine,
|
|
292
302
|
"- A failing DoD causes the result to be rejected and retried/escalated, not silently accepted.",
|
|
293
303
|
].join("\n");
|
package/src/v2.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Plugin } from "@opencode/plugin";
|
|
2
|
+
import type { PluginInput } from "@opencode-ai/plugin";
|
|
3
|
+
import ModelRouterPlugin from "./index";
|
|
4
|
+
import { createV2Runtime } from "./compat/v2-client";
|
|
5
|
+
import { registerV2Hooks } from "./compat/v2-hooks";
|
|
6
|
+
|
|
7
|
+
/** Keep the routing engine shared; translate only the host API at this boundary. */
|
|
8
|
+
export default {
|
|
9
|
+
id: "opencode-model-router",
|
|
10
|
+
server: ModelRouterPlugin,
|
|
11
|
+
async setup(ctx: Plugin.Context) {
|
|
12
|
+
const runtime = createV2Runtime(ctx);
|
|
13
|
+
// The engine consumes directory/client only. V2 has no legacy HTTP server
|
|
14
|
+
// URL or Bun shell context; do not invent either for the adapter.
|
|
15
|
+
const input = {
|
|
16
|
+
directory: ctx.location.directory,
|
|
17
|
+
worktree: ctx.location.project.directory,
|
|
18
|
+
client: runtime.client,
|
|
19
|
+
routerChildRunner: runtime.childRunner,
|
|
20
|
+
};
|
|
21
|
+
const hooks = await ModelRouterPlugin(input as unknown as PluginInput);
|
|
22
|
+
return registerV2Hooks(ctx, hooks, runtime);
|
|
23
|
+
},
|
|
24
|
+
} satisfies Plugin.Plugin & { server: typeof ModelRouterPlugin };
|
package/src/verify/baseline.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
// Runner-independent comparison with opportunistic text/JSON adapters.
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
ExecResult, FailureClassification, JudgeScoped, RecheckOutcome, ScopedOutcome, TestsPassJudgement,
|
|
4
|
+
} from "./types";
|
|
5
|
+
import type { RunResult } from "./runner";
|
|
6
|
+
import { scrubText } from "../guard/scrub";
|
|
3
7
|
|
|
4
8
|
export interface TestObservation {
|
|
5
9
|
code: number;
|
|
@@ -8,10 +12,18 @@ export interface TestObservation {
|
|
|
8
12
|
complete: boolean;
|
|
9
13
|
}
|
|
10
14
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
/**
|
|
16
|
+
* T7 u4: the stable ReferenceState "none" reasons. The store (dispatch.ts) produces all but
|
|
17
|
+
* `untracked`, which is also runDeterministic's default when a gate carries no reference.
|
|
18
|
+
*/
|
|
19
|
+
export const REFERENCE_NONE = {
|
|
20
|
+
failed: "the dispatch-time capture failed or timed out",
|
|
21
|
+
contaminated: "an edit was observed in an overlapping directory before the capture resolved",
|
|
22
|
+
untracked: "the dispatch was not tracked",
|
|
23
|
+
gateBudget: "the capture had not resolved within the gate budget",
|
|
24
|
+
/** The dispatch's DoD had no allowlisted testsPass check (or verify.require was "never"). */
|
|
25
|
+
notRequested: "no testsPass check was declared at dispatch",
|
|
26
|
+
} as const;
|
|
15
27
|
|
|
16
28
|
export function observeTests(result: ExecResult): TestObservation {
|
|
17
29
|
const text = (result.stdout + "\n" + result.stderr).replace(/\x1b\[[0-9;]*m/g, "");
|
|
@@ -54,25 +66,131 @@ export function observeTests(result: ExecResult): TestObservation {
|
|
|
54
66
|
};
|
|
55
67
|
}
|
|
56
68
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (
|
|
72
|
-
|
|
73
|
-
|
|
69
|
+
/**
|
|
70
|
+
* The id-space file key of a RunResult id (deterministic.ts header, T5): the part before the
|
|
71
|
+
* earliest " > " or "::" separator, else the whole (bare-file) id; "/" separators. The earliest
|
|
72
|
+
* wins so a pytest parametrisation such as `t.py::test[1 > 0]` keys to `t.py`, and a vitest name
|
|
73
|
+
* containing "::" keys to its file.
|
|
74
|
+
*/
|
|
75
|
+
export function fileKeyOfId(id: string): string {
|
|
76
|
+
const cuts = [id.indexOf(" > "), id.indexOf("::")].filter(i => i >= 0);
|
|
77
|
+
const file = cuts.length > 0 ? id.slice(0, Math.min(...cuts)) : id;
|
|
78
|
+
return file.replace(/\\/g, "/");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** T7 <ids>: at most 10 ids, then " (+<k> more)". */
|
|
82
|
+
export function formatIds(ids: readonly string[]): string {
|
|
83
|
+
if (ids.length === 0) return "(none)";
|
|
84
|
+
const shown = ids.slice(0, 10).join(", ");
|
|
85
|
+
return ids.length > 10 ? `${shown} (+${ids.length - 10} more)` : shown;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const unverifiable = (reason: string, failures?: FailureClassification): TestsPassJudgement =>
|
|
89
|
+
failures ? { ok: false, unverifiable: true, reason, failures } : { ok: false, unverifiable: true, reason };
|
|
90
|
+
|
|
91
|
+
/** T5.5: classify each scoped failing id against an exact recheck. */
|
|
92
|
+
function classify(c: RunResult, recheck: Extract<RecheckOutcome, { kind: "exact" }>): FailureClassification {
|
|
93
|
+
const r = recheck.result;
|
|
94
|
+
const ran = new Set(recheck.ranFiles);
|
|
95
|
+
const absent = new Set(recheck.absentFiles);
|
|
96
|
+
const refFailing = new Set(r?.failingIds ?? []);
|
|
97
|
+
const refFileKeys = new Set((r?.failingIds ?? []).map(fileKeyOfId));
|
|
98
|
+
const introduced: string[] = [];
|
|
99
|
+
const preexisting: string[] = [];
|
|
100
|
+
const unknown: string[] = [];
|
|
101
|
+
for (const x of c.failingIds) {
|
|
102
|
+
const f = fileKeyOfId(x);
|
|
103
|
+
const bare = f === x.replace(/\\/g, "/");
|
|
104
|
+
if (absent.has(f)) introduced.push(x);
|
|
105
|
+
else if (!ran.has(f)) unknown.push(x);
|
|
106
|
+
// A bare-file id (collection error now) is never pre-existing; its file ran, so it is introduced.
|
|
107
|
+
else if (bare) introduced.push(x);
|
|
108
|
+
else if (refFailing.has(x)) preexisting.push(x);
|
|
109
|
+
else if (c.source === "report") introduced.push(x);
|
|
110
|
+
else if (!refFileKeys.has(f)) introduced.push(x);
|
|
111
|
+
else unknown.push(x);
|
|
74
112
|
}
|
|
75
|
-
|
|
76
|
-
// unchanged. Under-excuse rather than silently replace an old failure by a new one.
|
|
77
|
-
return unavailable(`baseline exit=${before.code}, count=${before.count ?? "unknown"}; cannot prove failures predate dispatch`);
|
|
113
|
+
return { introduced, preexisting, unknown };
|
|
78
114
|
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The verdict algebra (deterministic.ts header, T5-T7). Pure and total: a failure passes only when
|
|
118
|
+
* proven pre-existing at an exact reference with a complete scoped inventory (G1), and fails only
|
|
119
|
+
* when at least one id is proven introduced (G2).
|
|
120
|
+
*/
|
|
121
|
+
export const judgeScoped: JudgeScoped = (scoped: ScopedOutcome, recheck: RecheckOutcome | undefined): TestsPassJudgement => {
|
|
122
|
+
switch (scoped.kind) {
|
|
123
|
+
case "no-affected":
|
|
124
|
+
return { ok: true, unverifiable: false, note: scoped.note };
|
|
125
|
+
case "unverifiable":
|
|
126
|
+
return unverifiable(`testsPass: scoping impossible (${scoped.code}): ${scoped.reason}`);
|
|
127
|
+
case "slot-busy":
|
|
128
|
+
return unverifiable(scoped.deadlineCut
|
|
129
|
+
? "gate budget exhausted waiting for the verification slot"
|
|
130
|
+
: `verification slot busy (waited ${scoped.waitedMs}ms)`);
|
|
131
|
+
case "timed-out":
|
|
132
|
+
return unverifiable(`testsPass timed out after ${scoped.boundMs}ms`);
|
|
133
|
+
case "aborted":
|
|
134
|
+
return unverifiable(`testsPass: ${scoped.reason}`);
|
|
135
|
+
case "error":
|
|
136
|
+
return unverifiable(`testsPass check errored: ${scrubText(scoped.reason)}`);
|
|
137
|
+
case "ran":
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
const c = scoped.result;
|
|
141
|
+
const note = c.note ?? "no details";
|
|
142
|
+
if (c.complete && !c.collectionError && c.failingIds.length === 0) {
|
|
143
|
+
const runner = scoped.spec?.runner ?? "full";
|
|
144
|
+
const total = c.total ?? "unknown";
|
|
145
|
+
const evidence = `testsPass: affected tests passed (${runner}, ${total} tests)`;
|
|
146
|
+
return c.total === 0
|
|
147
|
+
? { ok: true, unverifiable: false, evidence, note: "testsPass: no affected tests ran" }
|
|
148
|
+
: { ok: true, unverifiable: false, evidence };
|
|
149
|
+
}
|
|
150
|
+
const observed = `; observed failures: ${formatIds(c.failingIds)}`;
|
|
151
|
+
if (c.failingIds.length === 0) {
|
|
152
|
+
return c.collectionError
|
|
153
|
+
? unverifiable(`testsPass: collection error without failing test files: ${note}${observed}`)
|
|
154
|
+
: unverifiable(`testsPass: the scoped result is incomplete: ${note} (exit ${scoped.exitCode})`);
|
|
155
|
+
}
|
|
156
|
+
if (recheck === undefined) {
|
|
157
|
+
return unverifiable(c.collectionError
|
|
158
|
+
? `testsPass: collection error without failing test files: ${note}${observed}`
|
|
159
|
+
: `testsPass: cannot attribute failures: no failing test file identified, recheck not attempted${observed}`);
|
|
160
|
+
}
|
|
161
|
+
switch (recheck.kind) {
|
|
162
|
+
case "approximate": {
|
|
163
|
+
const causes = recheck.inexactReasons.map(r => (r.path ? `${r.cause} ${r.path}` : r.cause));
|
|
164
|
+
return unverifiable(`testsPass: cannot attribute failures: the dispatch reference is approximate (${formatIds(causes)})${observed}`);
|
|
165
|
+
}
|
|
166
|
+
case "unusable":
|
|
167
|
+
return unverifiable(recheck.cause === "no-reference"
|
|
168
|
+
? `testsPass: no reference: pre-existing failures cannot be told apart (${recheck.reason})${observed}`
|
|
169
|
+
: `testsPass: cannot attribute failures: reference unusable (${recheck.cause}): ${recheck.reason}${observed}`);
|
|
170
|
+
case "disabled":
|
|
171
|
+
return unverifiable(`testsPass: cannot attribute failures: failureRecheck is off, pre-existing failures cannot be told apart${observed}`);
|
|
172
|
+
case "timed-out":
|
|
173
|
+
return unverifiable(`testsPass: cannot attribute failures: the reference rerun timed out after ${recheck.boundMs}ms${observed}`);
|
|
174
|
+
case "skipped-deadline":
|
|
175
|
+
return unverifiable(`testsPass: gate budget exhausted before recheck${observed}`);
|
|
176
|
+
case "exact":
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
const failures = classify(c, recheck);
|
|
180
|
+
if (failures.introduced.length > 0) {
|
|
181
|
+
const reason = `testsPass: introduced failures: ${formatIds(failures.introduced)}${observed}`;
|
|
182
|
+
return failures.preexisting.length > 0
|
|
183
|
+
? { ok: false, unverifiable: false, reason, note: `testsPass: also failing at the dispatch reference: ${formatIds(failures.preexisting)}`, failures }
|
|
184
|
+
: { ok: false, unverifiable: false, reason, failures };
|
|
185
|
+
}
|
|
186
|
+
if (failures.unknown.length === 0 && c.complete && !c.collectionError) {
|
|
187
|
+
return {
|
|
188
|
+
ok: true, unverifiable: false, failures,
|
|
189
|
+
note: `testsPass: no worse than before; pre-existing failures: ${formatIds(failures.preexisting)}; suite is NOT green (affected tests checked against the exact dispatch reference)`,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
if (failures.unknown.length > 0) {
|
|
193
|
+
return unverifiable(`testsPass: cannot prove failures predate dispatch: ${formatIds(failures.unknown)}${observed}`, failures);
|
|
194
|
+
}
|
|
195
|
+
return unverifiable(`testsPass: the scoped failure inventory is incomplete (${c.note ?? "collection error"}); known failures predate dispatch, others may not${observed}`, failures);
|
|
196
|
+
};
|