"use strict";
const { spawnSync } = require("node:child_process");
const fs = require("node:fs");
const path = require("node:path");
const repositoryRoot = path.resolve(__dirname, "..");
const ARTIFACT = path.join("target", "ci", "lifecycle-v11", "slice-0-oracle-contract.json");
const CARGO = process.env.SYMFORGE_LIFECYCLE_CARGO_EXECUTABLE || "cargo";
const GIT = process.env.SYMFORGE_LIFECYCLE_GIT_EXECUTABLE || "git";
const SUITES = [
{
target: "tests/project_index_lifecycle_slice0.rs",
expected: "red",
args: ["test", "--test", "project_index_lifecycle_slice0", "--", "--ignored", "--test-threads=1"],
},
{
target: "src/watcher/mod.rs::tests",
expected: "green",
args: [
"test",
"--lib",
"watcher::tests::generation_before_root_split_cannot_authorize_root_a_reindex_into_root_b",
"--",
"--exact",
],
},
{
target: "src/daemon.rs::tests",
expected: "red",
args: [
"test",
"--lib",
"daemon::tests::concurrent_first_open_performs_exactly_one_cold_load",
"--",
"--exact",
"--ignored",
],
},
];
const NEWLINE = String.fromCharCode(10);
const MAX_REASON_BYTES = 512;
const RED_CASES = [
"capacity_refused_open_creates_no_slot_and_no_watcher",
"configured_capacity_bounds_the_process_not_each_load",
"daemon::tests::concurrent_first_open_performs_exactly_one_cold_load",
"empty_placeholder_publication_refuses_watcher_mutation",
"failed_reload_retains_the_recovery_observer",
"observer_replacement_gap_is_latched_as_non_current",
"old_observer_delivery_after_promotion_is_not_current",
"same_path_root_replacement_is_not_silently_adopted",
"snapshot_seed_is_not_queryable_before_verification",
"watcher_mutation_during_candidate_build_is_not_discarded",
"whole_project_publication_preserves_latest_siblings",
];
const RESOLVED_CASES = new Map([
[
"watcher::tests::generation_before_root_split_cannot_authorize_root_a_reindex_into_root_b",
{
slice: 1,
tasks: ["T028"],
defect: "2.8 root and generation authority can split",
fix: "src/watcher/mod.rs::effective_fence_generation reads one published generation",
},
],
]);
const EXPECTED_CASES = [...RED_CASES, ...RESOLVED_CASES.keys()];
function expectationFor(caseName) {
return RESOLVED_CASES.has(caseName) ? "green" : "red";
}
const OBSERVED_FOR_EXPECTED = { red: "failed", green: "passed" };
function git(...args) {
const result = spawnSync(GIT, args, { cwd: repositoryRoot, encoding: "utf8", shell: false });
if (result.status !== 0) throw new Error(`git ${args.join(" ")} failed`);
return result.stdout.trim();
}
function reasonFor(output, caseName) {
const marker = `---- ${caseName} stdout ----`;
const start = output.indexOf(marker);
if (start === -1) return null;
const lines = output.slice(start + marker.length).split("\n");
for (const line of lines) {
const text = line.trim();
if (!text || text.startsWith("thread '") || text.startsWith("note:")) continue;
if (text.startsWith("----")) break;
return text.length > MAX_REASON_BYTES ? `${text.slice(0, MAX_REASON_BYTES)}…` : text;
}
return null;
}
const SUITE_TIMEOUT_MS = 30 * 60 * 1000;
function runSuite(suite) {
const result = spawnSync(CARGO, suite.args, {
cwd: repositoryRoot,
encoding: "utf8",
shell: false,
maxBuffer: 64 * 1024 * 1024,
timeout: SUITE_TIMEOUT_MS,
});
const output = `${result.stdout || ""}${result.stderr || ""}`;
if (result.error && result.error.code === "ETIMEDOUT") {
throw new Error(`timed out after ${SUITE_TIMEOUT_MS} ms: ${suite.args.join(" ")}`);
}
if (result.error) {
throw new Error(`could not run: ${suite.args.join(" ")}: ${result.error.message}`);
}
if (result.signal) {
throw new Error(`killed by ${result.signal}: ${suite.args.join(" ")}`);
}
if (suite.expected === "red" && result.status === 0) {
throw new Error(
`exited 0 with no failing case, so nothing was preserved: ${suite.args.join(" ")}`,
);
}
const cases = [];
for (const match of output.matchAll(
/^test ([A-Za-z0-9_:]+) \.\.\. (ok|FAILED|ignored)(?:, [^\n]*)?$/gmu,
)) {
const [, caseName, outcome] = match;
cases.push({
case: caseName,
target: suite.target,
command: `${CARGO} ${suite.args.join(" ")}`,
expected: expectationFor(caseName),
observed: outcome === "FAILED" ? "failed" : outcome === "ok" ? "passed" : "ignored",
reason: outcome === "FAILED" ? reasonFor(output, caseName) : null,
resolved_by: RESOLVED_CASES.get(caseName) ?? null,
});
}
if (cases.length === 0) {
throw new Error(`no cases parsed from: ${suite.args.join(" ")}\n${output.slice(-2000)}`);
}
return cases;
}
const cases = SUITES.flatMap(runSuite).sort((left, right) => left.case.localeCompare(right.case));
const unexpected = cases.filter(
(entry) => entry.observed !== OBSERVED_FOR_EXPECTED[entry.expected],
);
const observedNames = cases.map((entry) => entry.case).sort();
const expectedNames = [...EXPECTED_CASES].sort();
const missing = expectedNames.filter((name) => !observedNames.includes(name));
const extra = observedNames.filter((name) => !expectedNames.includes(name));
const artifact = {
kind: "symforge.lifecycle.v11.slice0_oracle_contract",
schema_version: 2,
slice: 0,
release_commit: git("rev-parse", "--verify", "HEAD"),
release_tree: git("rev-parse", "--verify", "HEAD^{tree}"),
case_count: cases.length,
expected_case_count: EXPECTED_CASES.length,
red_case_count: RED_CASES.length,
resolved_case_count: RESOLVED_CASES.size,
missing_cases: missing,
unexpected_cases: extra,
cases,
status:
unexpected.length === 0 && missing.length === 0 && extra.length === 0
? "expected_outcomes_preserved"
: "unexpected_outcome",
};
fs.mkdirSync(path.join(repositoryRoot, path.dirname(ARTIFACT)), { recursive: true });
fs.writeFileSync(
path.join(repositoryRoot, ARTIFACT),
`${JSON.stringify(artifact, null, 2)}\n`,
"utf8",
);
for (const name of missing) {
process.stderr.write(`ERROR SLICE0_ORACLE_MISSING: ${name} was not run` + NEWLINE);
}
for (const name of extra) {
process.stderr.write(`ERROR SLICE0_ORACLE_EXTRA: ${name} is not in the expected roster` + NEWLINE);
}
if (unexpected.length > 0 || missing.length > 0 || extra.length > 0) {
for (const entry of unexpected) {
process.stderr.write(
`ERROR SLICE0_ORACLE_UNEXPECTED: ${entry.case} observed ${entry.observed}, expected ${entry.expected}\n`,
);
}
process.stderr.write(
"A Slice 0 positive control that stops failing is either a fix landed without its " +
"owning slice reclassifying it here, or a control gone vacuous. A resolved control "
+ "that stops passing is a regression of the fix that resolved it, and one observed "
+ "`ignored` has been silenced by a re-added attribute. A control that disappears "
+ "from the roster is a lost positive control. All need review.\n",
);
process.exitCode = 1;
} else {
process.stdout.write(
`slice 0 oracle contract: ${cases.length} controls preserved `
+ `(${RED_CASES.length} red, ${RESOLVED_CASES.size} resolved-green) -> ${ARTIFACT}\n`,
);
}