mirror of
https://github.com/ranxianglei/billion-context.git
synced 2026-12-03 15:55:30 +08:00
Root cause of the 7-red test matrix: #3145 sharding concentrated the port families into shards 2/3 while #2816 concurrency let them bind simultaneously on one runner — cross-file 18787 collisions and shared ephemeral-port TOCTOU. Fixes: - scripts/live-files.mjs: shared LIVE_FILES table + zonePortForSlot bands (18787+(slot+1)*32; first band above the default base so bare 18787 is never a CI target) - tests/preload-zone-slot.mjs: CI-only preload assigns each live file its band via BILI_ZONE_PORT (product support already in src/config.ts) - run-ci-tests.mjs: preload injected for live suites - port-zone-live: band-aware base + waitForBandFree at sandbox entry (teardown SIGKILL returns before kernel release) + full XDG isolation (a dev box's real config/index.db makes children queue on real locks — the #3044 fail-fast then never fires) + squatter leak closes on failure - claude-native: band-aware helpers (ZONE_BAND_BASE from process.env; planClaudeNativeBootstrap({}) stays on the exported default), squatter test flipped to the #2877 own-lane refusal contract (was in the original red set — #2877 superseded #1660's attach-by-default), foreign-listener squatter kills connections (unhandled ECONNRESET crashed the runner), freePort TOCTOU bounded retry - auto-restart: stubs exit 91 on EADDRINUSE with bounded scenario retries; SIGTERM-survivor escalation test is POSIX-only (Windows terminates uncatchably — was a w24 red) - ensureDistBuilt (claude-native/port-zone-live/index-entry-guard): rebuild when src is newer than dist — a stale dist exercises last week's product code (the #3044 'hang' was exactly this)
293 lines
13 KiB
JavaScript
293 lines
13 KiB
JavaScript
#!/usr/bin/env node
|
|
// #2816: CI test scheduling. Baseline logs prove the old runner default
|
|
// (availableParallelism()-1) was ALREADY concurrent: the sum of per-test
|
|
// durations (~630s ubuntu / ~1110s windows) fits inside the step wall
|
|
// (373s / 688s) only if several files ran at once. This pins concurrency=4
|
|
// explicitly. A two-phase prototype (serial "live" phase + concurrent pool)
|
|
// was measured first: the serial phase idled the other cores for 3-4 min
|
|
// without buying stability — live/process suites have run mixed with the rest
|
|
// on dedicated runners for months without #1248-class flakes (#1248 gates
|
|
// loaded local sandboxes via env, not CI), and 4 slots dilute live-live
|
|
// pairing (P(two live in flight) ≈ C(4,2)·(32/613)² ≈ 5%, vs ≈ 8% at the old
|
|
// c=3). Same tests, assertions and env as `npm test` — only the scheduler
|
|
// changes. Local `npm test` is untouched (this script is CI-only).
|
|
//
|
|
// LIVE_FILES is a tripwire, not a gate: every process-spawning suite must be
|
|
// registered there so adding a new one fails the job until someone reviews
|
|
// its concurrency assumptions (#1248) instead of silently riding c=4.
|
|
//
|
|
// #3029: attribution + bounded flake recovery for "swallowed" file-level
|
|
// failures. When a test child dies SILENTLY (non-zero exit, no stderr — OOM
|
|
// kill, tsx/esbuild worker death, race exit under concurrency), node --test
|
|
// collapses it to one file-level `not ok <file>` whose only payload is
|
|
// `error: 'test failed'` and zero subtests; NO reporter can surface an error
|
|
// the child never emitted. On failure this runner therefore: captures exit
|
|
// code + signal (previously discarded by an empty catch) plus a resource
|
|
// snapshot (free/total mem, loadavg, node version, platform); detects the
|
|
// swallowed shape (a top-level `not ok … *.test.ts` — successful files promote
|
|
// their inner tests to top level by test name and never emit a file-level
|
|
// entry); re-runs each such file in ISOLATION for attribution; then retries
|
|
// the whole phase ONCE and exits 0 only if that retry is green (a confirmed
|
|
// transient). Concrete assertion failures (subtests ran and reported a real
|
|
// message) are NOT the swallowed shape → they fail hard immediately, no retry,
|
|
// so a real regression is never masked.
|
|
import { execFileSync, spawn } from "node:child_process";
|
|
import { LIVE_FILES } from "./live-files.mjs";
|
|
import { readdirSync, readFileSync } from "node:fs";
|
|
import { freemem, loadavg, totalmem } from "node:os";
|
|
import { join } from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
const root = join(fileURLToPath(new URL(".", import.meta.url)), "..");
|
|
const testsDir = join(root, "tests");
|
|
|
|
// Process-spawning suites (grep ground truth: spawn/exec*/child_process/Worker
|
|
// references below). Conservative by design — a false positive just costs a
|
|
// registration entry; an unregistered spawner fails the job by design. New
|
|
// spawning suites MUST be added here after reviewing their concurrency
|
|
// assumptions.
|
|
// LIVE_FILES lives in scripts/live-files.mjs so the zone-slot preload can
|
|
// share the exact same table (#3182: per-file zone port bands).
|
|
|
|
const SPAWN_RE = /\bspawn[A-Za-z]*\s*\(|child_process|execFile|execSync|spawnSync|new Worker\(/;
|
|
|
|
const allFiles = readdirSync(testsDir)
|
|
.filter((f) => f.endsWith(".test.ts"))
|
|
.sort()
|
|
.map((f) => `tests/${f}`);
|
|
|
|
const baseToFile = new Map(allFiles.map((f) => [f.split("/").pop(), f]));
|
|
|
|
function die(msg) {
|
|
console.error(`::error::${msg}`);
|
|
console.error(`[ci-tests] ${msg}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
// #3143: shard parsing + balanced partitioning. CI_TEST_SHARD="i/N" selects this
|
|
// leg's slice; absent means "run everything" (local / back-compat, unchanged).
|
|
// The partition is DETERMINISTIC and TOTAL: LPT bin-packing over a line-count
|
|
// weight (cheap, runtime-derived, self-maintaining for new files) puts every file
|
|
// in exactly ONE shard, so the union of all shards == allFiles and NOTHING is ever
|
|
// skipped. Same input => same partition; a newly-added file only re-partitions the
|
|
// bins, it never drops another. Balance targets the HEAVIEST shard (the wall clock),
|
|
// not equal file counts — the one 7k-line monster must not pile onto one leg.
|
|
function parseShard() {
|
|
const raw = (process.env.CI_TEST_SHARD ?? "").trim();
|
|
if (raw === "") return null;
|
|
const m = raw.match(/^(\d+)\s*\/\s*(\d+)$/);
|
|
if (!m) die(`CI_TEST_SHARD must look like "<i>/<N>" (got "${raw}")`);
|
|
const i = Number(m[1]);
|
|
const n = Number(m[2]);
|
|
if (n < 1 || i < 1 || i > n) die(`CI_TEST_SHARD "${raw}" out of range (need 1 <= i <= N)`);
|
|
return { i, n };
|
|
}
|
|
|
|
function partitionAll(files, n) {
|
|
const weight = new Map();
|
|
for (const f of files) {
|
|
let txt = "";
|
|
try { txt = readFileSync(join(root, f), "utf8"); } catch { /* unreadable => weight floor 1 */ }
|
|
weight.set(f, Math.max(1, txt.split("\n").length));
|
|
}
|
|
const order = [...files].sort(
|
|
(a, b) => weight.get(b) - weight.get(a) || (a < b ? -1 : a > b ? 1 : 0),
|
|
);
|
|
const bins = Array.from({ length: n }, () => []);
|
|
const load = new Array(n).fill(0);
|
|
for (const f of order) {
|
|
let best = 0;
|
|
for (let k = 1; k < n; k++) if (load[k] < load[best]) best = k; // lowest index breaks ties
|
|
bins[best].push(f);
|
|
load[best] += weight.get(f);
|
|
}
|
|
return bins;
|
|
}
|
|
|
|
for (const f of LIVE_FILES) {
|
|
if (!allFiles.includes(f)) die(`LIVE_FILES entry ${f} does not exist`);
|
|
}
|
|
const unregistered = allFiles.filter(
|
|
(f) => !LIVE_FILES.includes(f) && SPAWN_RE.test(readFileSync(join(root, f), "utf8")),
|
|
);
|
|
if (unregistered.length > 0) {
|
|
die(
|
|
`process-spawning test files missing from LIVE_FILES in scripts/run-ci-tests.mjs: ` +
|
|
`${unregistered.join(", ")}. Register them there after reviewing their ` +
|
|
`concurrency assumptions (#1248).`,
|
|
);
|
|
}
|
|
|
|
// #3143: select this leg's slice AFTER the global tripwire above (which still scans
|
|
// the FULL set in every shard, so an unregistered spawner fails whichever shard you
|
|
// inspect). Unsharded => the whole suite, exactly as before.
|
|
const shard = parseShard();
|
|
let selectedFiles = allFiles;
|
|
if (shard) {
|
|
selectedFiles = partitionAll(allFiles, shard.n)[shard.i - 1];
|
|
console.log(
|
|
`[ci-tests] shard ${shard.i}/${shard.n}: ${selectedFiles.length}/${allFiles.length} files ` +
|
|
`(union of all shards == allFiles — nothing skipped)`,
|
|
);
|
|
}
|
|
|
|
// Parity with the old `npm test` step: its pretest hook builds kernel/ and
|
|
// pi-subagents/ dist bundles that the suites import. On Windows npm is a .cmd
|
|
// batch file, which CreateProcess cannot exec directly (EINVAL) — route it
|
|
// through cmd.exe via shell:true; the args are fixed literals, so there is no
|
|
// quoting surface.
|
|
const npmCmd = process.platform === "win32" ? "npm.cmd" : "npm";
|
|
console.log("[ci-tests] pretest (kernel + pi-subagents build)");
|
|
execFileSync(npmCmd, ["run", "pretest"], { cwd: root, stdio: "inherit", shell: process.platform === "win32" });
|
|
|
|
// Loader chain the suites depend on (storage-baseline preload, #2671/#2803,
|
|
// then tsx). Shared by the main phase and the isolated diagnostic re-run so
|
|
// the latter sees the SAME environment as the in-phase run.
|
|
const PRELOAD_ARGS = ["--import", "./tests/preload-storage-baseline.mjs", "--import", "./tests/preload-zone-slot.mjs", "--import", "tsx"];
|
|
|
|
const MAX_CAPTURE = 32 * 1024 * 1024; // bound in-memory capture; real runs are MB-scale
|
|
|
|
function runNodeTest(files, concurrency, label) {
|
|
const t0 = Date.now();
|
|
console.log(`[ci-tests] ${label}: ${files.length} files, concurrency=${concurrency}`);
|
|
return new Promise((resolve) => {
|
|
const child = spawn(
|
|
process.execPath,
|
|
[...PRELOAD_ARGS, "--test", "--test-concurrency", String(concurrency), "--test-reporter=tap", ...files],
|
|
{ cwd: root },
|
|
);
|
|
const out = [];
|
|
const err = [];
|
|
let outLen = 0;
|
|
let errLen = 0;
|
|
child.stdout.on("data", (d) => {
|
|
if (outLen < MAX_CAPTURE) {
|
|
out.push(d);
|
|
outLen += d.length;
|
|
}
|
|
process.stdout.write(d);
|
|
});
|
|
child.stderr.on("data", (d) => {
|
|
if (errLen < MAX_CAPTURE) {
|
|
err.push(d);
|
|
errLen += d.length;
|
|
}
|
|
process.stderr.write(d);
|
|
});
|
|
child.on("error", (e) => {
|
|
console.error(`[ci-tests] ${label}: spawn error: ${e.message}`);
|
|
resolve({ code: -1, signal: null, out: "", err: "", ms: Date.now() - t0 });
|
|
});
|
|
child.on("close", (code, signal) => {
|
|
resolve({
|
|
code,
|
|
signal,
|
|
out: Buffer.concat(out).toString("utf8"),
|
|
err: Buffer.concat(err).toString("utf8"),
|
|
ms: Date.now() - t0,
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function resourceSnapshot() {
|
|
const mi = (n) => `${Math.round(n / 1048576)}MiB`;
|
|
return (
|
|
`freemem=${mi(freemem())} totalmem=${mi(totalmem())} ` +
|
|
`loadavg=${loadavg().map((x) => x.toFixed(2)).join("/")} ` +
|
|
`node=${process.version} platform=${process.platform}/${process.arch}`
|
|
);
|
|
}
|
|
|
|
// A top-level `not ok N - <path ending .test.ts>` can only come from a file
|
|
// whose child DIED: successful files promote their inner tests to top level by
|
|
// test name and never emit a file-level entry. Pairing that with the YAML
|
|
// `error: 'test failed'` marker isolates the swallowed/silent-death shape from
|
|
// real assertion failures (named by test name, carrying a real message).
|
|
function extractSwallowedFiles(output) {
|
|
const lines = output.split(/\r?\n/);
|
|
const found = new Set();
|
|
for (let i = 0; i < lines.length; i++) {
|
|
const m = lines[i].match(/^not ok \d+ - (.+?)\s*$/);
|
|
if (!m) continue;
|
|
const window = lines.slice(i, i + 20).join("\n");
|
|
if (!/error:\s*'test failed'/.test(window)) continue;
|
|
const base = m[1].trim().split(/[\\/]/).pop();
|
|
const rel = baseToFile.get(base);
|
|
if (rel) found.add(rel);
|
|
}
|
|
return [...found];
|
|
}
|
|
|
|
function runIsolated(fileRel) {
|
|
const t0 = Date.now();
|
|
console.log(`[ci-tests] DIAGNOSTIC isolated re-run (serial): ${fileRel} — full output below`);
|
|
return new Promise((resolve) => {
|
|
const child = spawn(process.execPath, [...PRELOAD_ARGS, "--test", "--test-reporter=tap", fileRel], { cwd: root });
|
|
child.stdout.on("data", (d) => process.stdout.write(d));
|
|
child.stderr.on("data", (d) => process.stderr.write(d));
|
|
child.on("error", (e) => {
|
|
console.error(`[ci-tests] isolated re-run ${fileRel}: spawn error: ${e.message}`);
|
|
resolve({ code: -1, signal: null, ms: Date.now() - t0 });
|
|
});
|
|
child.on("close", (code, signal) => resolve({ code, signal, ms: Date.now() - t0 }));
|
|
});
|
|
}
|
|
|
|
(async () => {
|
|
const first = await runNodeTest(selectedFiles, 4, "all");
|
|
if (first.code === 0) {
|
|
console.log(`[ci-tests] all ok in ${(first.ms / 1000).toFixed(1)}s`);
|
|
return;
|
|
}
|
|
console.error(`[ci-tests] all FAILED after ${(first.ms / 1000).toFixed(1)}s (exit=${first.code} signal=${first.signal})`);
|
|
console.error(`[ci-tests] resource snapshot: ${resourceSnapshot()}`);
|
|
const errTrim = first.err.replace(/\s+/g, " ").trim();
|
|
if (errTrim) {
|
|
console.error(
|
|
`[ci-tests] child stderr captured (${first.err.length} chars) — may carry a V8/native fatal (e.g. OOM 'Reached heap limit'): ` +
|
|
`${errTrim.slice(0, 2000)}`,
|
|
);
|
|
} else {
|
|
console.error(
|
|
"[ci-tests] child wrote nothing to stderr (node --test reports test failures on the TAP stdout above; nothing extra to surface here)",
|
|
);
|
|
}
|
|
const swallowed = extractSwallowedFiles(first.out);
|
|
if (swallowed.length === 0) {
|
|
console.error(
|
|
"[ci-tests] failure carries concrete sub-test assertions (not the swallowed 'test failed' shape) " +
|
|
"-> failing WITHOUT auto-retry so a real regression is never masked.",
|
|
);
|
|
process.exit(first.code ?? 1);
|
|
}
|
|
console.error(
|
|
`[ci-tests] swallowed file-level failure(s) — zero subtests, payload only 'test failed': ` +
|
|
`${swallowed.join(", ")} (#3029)`,
|
|
);
|
|
for (const rel of swallowed) {
|
|
const r = await runIsolated(rel);
|
|
console.error(
|
|
`[ci-tests] isolated re-run ${rel}: exit=${r.code} signal=${r.signal} (${(r.ms / 1000).toFixed(1)}s) -> ` +
|
|
(r.code === 0
|
|
? "PASSED in isolation (transient / concurrency flake likely)"
|
|
: "REPRODUCED in isolation (deterministic break)"),
|
|
);
|
|
}
|
|
console.error("[ci-tests] retrying full phase once to confirm flake (#3029)");
|
|
const retry = await runNodeTest(selectedFiles, 4, "all (retry)");
|
|
if (retry.code === 0) {
|
|
console.log(
|
|
`[ci-tests] FLAKE CONFIRMED: first run failed on ${swallowed.join(", ")}, full retry green in ` +
|
|
`${(retry.ms / 1000).toFixed(1)}s -> treating as transient (#3029). If it recurs, check ` +
|
|
`node${process.version.replace(/^v/, "").split(".")[0]}-${process.platform} + tsx load + concurrency=4 (see snapshot above).`,
|
|
);
|
|
process.exit(0);
|
|
}
|
|
console.error(`[ci-tests] full retry also FAILED (exit=${retry.code} signal=${retry.signal}) -> genuine failure, not a flake`);
|
|
process.exit(retry.code ?? 1);
|
|
})().catch((e) => {
|
|
console.error(`[ci-tests] runner crashed: ${e && e.stack ? e.stack : e}`);
|
|
process.exit(1);
|
|
});
|