Files
OmniRoute/tests/unit/router-eval-e2e-chain.test.ts
Diego Rodrigues de Sa e Souza 93265eede3 test(infra): retry recursive temp-dir removal on main (main twin of #11968) (#12246)
* test(infra): retry recursive temp-dir removal on main (main twin of #11968)

`main` has been red since b342c1a361 on the vitest and integration gates:

  ✖ tests/unit/autoCombo/provider-family-combos.test.ts > auto/<family>
  ✖ chat pipeline applies Codex OAuth fingerprint and priority tier inside combos

Both call resetStorage() from beforeEach, which does an fs.rmSync(TEST_DATA_DIR,
{recursive: true, force: true}) with no retry, and intermittently loses the race
with a not-yet-released SQLite handle (ENOTEMPTY).

release/v3.8.51 fixed this in #11968 with a mechanical codemod adding
maxRetries/retryDelay to every recursive rm/rmSync/rmdirSync under tests/, but
that PR landed only on the release branch. Because main only receives work at
the release squash, it stayed broken for the whole cycle — and repo-wide gates
then turn every open PR into main red on checks unrelated to their diff.

This is the --base main twin: re-runs the same codemod that already shipped on
the release branch (scripts/ad-hoc/codemod-rm-maxretries.mjs), so the two
branches converge on identical test-teardown semantics. Test-only; no product
logic is touched.

The remaining three failures reported on #12133 (unit full suite exceeding its
4800s ceiling, package-artifact exceeding 1200s, and the boot-smoke that is
skipped as a consequence) are runner-contention timeouts, not code defects —
validate-release-green.mjs runs those heavy gates concurrently on one shared
hosted runner. There is no fix to port for those.

* chore(scripts): carry the rm-maxretries codemod onto main alongside its output

The codemod that generated the previous commit lives in the repo on
release/v3.8.51 (added by #11968) but was never on main. Bringing it over keeps
the tool next to the change it produced, so the transformation stays
reproducible and auditable from either branch.
2026-09-01 01:48:00 -03:00

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, maxRetries: 5, retryDelay: 100 });
}
});