mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 12:22:14 +03:00
* feat(eval): add router-eval harness (AIQ scoring, regression gate, Pareto search)
Extracts a standalone router-eval evaluation tool that replays routing
decisions (from NDJSON corpora or the usage_history/call_logs SQLite
tables) into an AIQ (success/latency/cost) score, compares baseline vs.
candidate router configs with a retained-run regression gate, and ranks
Pareto-optimal candidates across a search space — a sibling to the
existing eval:compression harness.
New scripts: scripts/router-eval/{index,compare,patch-compare,search,
trends}.ts, scripts/check/check-router-eval-regression.ts, and
src/lib/routerEval/index.ts, wired via 6 new package.json entries
(eval:router, eval:router:compare, eval:router:patch-compare,
eval:router:search, eval:router:trends, check:router-eval).
Reconstructed onto current release/v3.8.49 from the original ~142-commit
stale PR branch: only the genuinely new router-eval payload (17 files)
was extracted — the other ~560 changed files in the original diff were
base-drift already present on release in newer form. The new package.json
scripts now invoke `node --import tsx` instead of `bun`, matching the
`eval:compression` precedent (Bun is reserved for a closed 5-script
allowlist). The runtime-detection shim (`"Bun" in globalThis`) already
present in the harness gracefully falls back to better-sqlite3 under
Node, so no logic changes were needed there; the retained-run manifest's
previously-hardcoded `runtime: "bun"` field and matching CLI help text
were corrected to reflect the actual invocation.
All 27 existing router-eval unit tests pass unchanged.
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
* refactor(router-eval): decompose toRouterObservation below complexity gate
toRouterObservation had cyclomatic complexity 18 (gate max is 15). Extract
the per-field parsing/normalization into pure helpers (sampleId, model
fields, latency, cost derivation, success) so the entry point is a plain
sequential assembly of a RouterObservation. Behavior is unchanged — same
tests pass, same fallbacks, same precedence between input aliases.
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
---------
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
136 lines
3.7 KiB
TypeScript
136 lines
3.7 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { spawnSync } from "node:child_process";
|
|
|
|
const searchScript = "scripts/router-eval/search.ts";
|
|
const checkScript = "scripts/check/check-router-eval-regression.ts";
|
|
|
|
function writeObservation(file: string, configId: string, latencyMs: number, costUsd: number) {
|
|
writeFileSync(
|
|
file,
|
|
`${JSON.stringify({
|
|
sampleId: `${configId}-sample`,
|
|
configId,
|
|
selectedModel: "gpt-4.1",
|
|
expectedModel: "gpt-4.1",
|
|
latencyMs,
|
|
costUsd,
|
|
success: true,
|
|
})}\n`
|
|
);
|
|
}
|
|
|
|
function runSearch(
|
|
baseline: string,
|
|
candidateName: string,
|
|
candidate: string,
|
|
artifactDir: string,
|
|
runId: string
|
|
): string {
|
|
const result = spawnSync(
|
|
process.execPath,
|
|
[
|
|
"--import",
|
|
"tsx",
|
|
searchScript,
|
|
"--baseline",
|
|
baseline,
|
|
"--candidate",
|
|
`${candidateName}=${candidate}`,
|
|
"--artifact-dir",
|
|
artifactDir,
|
|
"--run-id",
|
|
runId,
|
|
"--objective",
|
|
"quality",
|
|
],
|
|
{ encoding: "utf8" }
|
|
);
|
|
assert.equal(result.status, 0);
|
|
const patch = join(artifactDir, runId, "router-config.patch.json");
|
|
assert.ok(readFileSync(patch, "utf8").includes("router-config-patch"));
|
|
return patch;
|
|
}
|
|
|
|
test("router eval retained chain runs search patches through the check wrapper gate", () => {
|
|
const dir = mkdtempSync(join(tmpdir(), "router-eval-e2e-chain-"));
|
|
const baseline = join(dir, "baseline.ndjson");
|
|
const oldCandidate = join(dir, "old.ndjson");
|
|
const newCandidate = join(dir, "new.ndjson");
|
|
const searchArtifacts = join(dir, "search-artifacts");
|
|
const gateArtifacts = join(dir, "gate-artifacts");
|
|
const runId = "chain-001";
|
|
|
|
writeObservation(baseline, "baseline", 150, 0.004);
|
|
writeObservation(oldCandidate, "old-router", 130, 0.004);
|
|
writeObservation(newCandidate, "new-router", 100, 0.003);
|
|
|
|
try {
|
|
const baselinePatch = runSearch(
|
|
baseline,
|
|
"old-router",
|
|
oldCandidate,
|
|
searchArtifacts,
|
|
"old-search"
|
|
);
|
|
const candidatePatch = runSearch(
|
|
baseline,
|
|
"new-router",
|
|
newCandidate,
|
|
searchArtifacts,
|
|
"new-search"
|
|
);
|
|
const result = spawnSync(
|
|
process.execPath,
|
|
[
|
|
"--import",
|
|
"tsx",
|
|
checkScript,
|
|
"--baseline",
|
|
baseline,
|
|
"--candidate",
|
|
newCandidate,
|
|
"--baseline-patch",
|
|
baselinePatch,
|
|
"--candidate-patch",
|
|
candidatePatch,
|
|
"--artifact-dir",
|
|
gateArtifacts,
|
|
"--run-id",
|
|
runId,
|
|
],
|
|
{ encoding: "utf8" }
|
|
);
|
|
|
|
assert.equal(result.status, 0);
|
|
const runDir = join(gateArtifacts, runId);
|
|
assert.ok(
|
|
readFileSync(join(runDir, "router-eval.json"), "utf8").includes("router-eval-comparison")
|
|
);
|
|
assert.ok(
|
|
readFileSync(join(runDir, "patch-comparison.json"), "utf8").includes(
|
|
"router-config-patch-comparison"
|
|
)
|
|
);
|
|
assert.ok(
|
|
readFileSync(join(runDir, "inputs", "baseline.patch.json"), "utf8").includes("old-router")
|
|
);
|
|
assert.ok(
|
|
readFileSync(join(runDir, "inputs", "candidate.patch.json"), "utf8").includes("new-router")
|
|
);
|
|
const manifest = JSON.parse(readFileSync(join(runDir, "manifest.json"), "utf8")) as {
|
|
kind?: string;
|
|
result?: { status?: number };
|
|
outputs?: { patchJson?: string };
|
|
};
|
|
assert.equal(manifest.kind, "router-eval-gate-run");
|
|
assert.equal(manifest.result?.status, 0);
|
|
assert.equal(manifest.outputs?.patchJson, "patch-comparison.json");
|
|
} finally {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|