import { createHash } from "node:crypto"; import { readdirSync, readFileSync } from "node:fs"; const hash = (bytes) => createHash("sha256").update(bytes).digest("hex"); const read = (path) => { const bytes = readFileSync(new URL(path, import.meta.url)); return { bytes, json: JSON.parse(bytes), sha256: hash(bytes) }; }; const aggregate = read("../src/data/k3-attnres-forward.json"); const compact = read("../src/data/k3-attnres-forward-compact.json"); const reproduction = read("../experiments/k3/attnres_forward/reproduction.json"); const manifest = read("../experiments/k3/attnres_forward/manifest.json"); const rawDirectory = new URL("../experiments/k3/attnres_forward/results/raw/", import.meta.url); const failures = []; const expect = (condition, message) => { if (!condition) failures.push(message); }; const close = (actual, expected, tolerance = 1e-15) => Math.abs(actual - expected) <= tolerance; expect(aggregate.sha256 === "f8df928adb8a563d851bb3c1abbf40bcada33626d9177821e4341d854980a097", "aggregate physical SHA-256 changed"); expect(compact.sha256 === "664f6d6226df7c0c9aba6314d54a1cb8ae90f6922016dfa7c7a823729606f0c1", "compact physical SHA-256 changed"); expect(reproduction.sha256 === "b029c333596dd1de957efc2b42f7ebfc1d32f241c687f06021c4d08ffa27b1d0", "reproduction physical SHA-256 changed"); expect(manifest.sha256 === "49546ed5baf36bcb30885062b7c671fafe4ccff2e606624cd7dbe23717f9a712", "manifest physical SHA-256 changed"); expect(aggregate.json.canonical_sha256_without_self === "eecf05c623e473ec5eba8afa54d555d2733a0af2fc5488398f1da095206d50c8", "aggregate canonical SHA-256 changed"); expect(compact.json.canonical_sha256_without_self === "c3e672adb6879f99efccf3bd2e0fabaab1a1159225a8ddaf4f5f9ea7a002784b", "compact canonical SHA-256 changed"); expect(reproduction.json.canonical_sha256_without_self === "57346df80c0d76bd1d306d5fa213feed74ac2094237c22ebcb49f16ea16437e0", "reproduction canonical SHA-256 changed"); expect(compact.json.protocol_id === "llm-atlas-k3-attnres-forward-training-v1", "protocol identity mismatch"); expect(compact.json.status === "forward_training_attenuation_established_within_reduced_protocol", "frozen status changed"); expect(compact.json.primary_step === 8000, "primary step changed"); expect(compact.json.spike_layers_1based.join(",") === "21,22,23,24,25", "spike window changed"); expect(compact.json.thresholds.material_relative_drop === 0.2, "attenuation threshold changed"); expect(compact.json.processed_target_bytes.formal_12_cells === 786432000, "formal target bytes changed"); expect(compact.json.processed_target_bytes.primary_replay === 65536000, "replay target bytes changed"); expect(compact.json.processed_target_bytes.total === 851968000, "new target bytes changed"); expect(compact.json.aggregate_sha256 === aggregate.json.canonical_sha256_without_self, "compact→aggregate canonical link mismatch"); expect(reproduction.json.scientific_payload_sha256 === compact.json.replay.scientific_payload_sha256, "reproduction→replay hash link mismatch"); expect(reproduction.json.passed && compact.json.replay.passed, "full replay is not exact"); expect(reproduction.json.post_result_grok_review.blocking_errors === 0, "post-result audit reports a blocking error"); expect(reproduction.json.post_result_grok_review.status_confirmed, "post-result audit did not confirm status"); expect(reproduction.json.post_result_grok_review.replay_confirmed, "post-result audit did not confirm replay"); expect(compact.json.metadata_warnings.length === 0, "historical pairing metadata warning appeared"); const rawNames = readdirSync(rawDirectory).filter((name) => name.endsWith(".json")).sort(); expect(rawNames.length === 13, "raw matrix is not 12 formal + 1 replay"); const expectedRaw = [ ...aggregate.json.input_files.formal.map((item) => item), { ...aggregate.json.input_files.replay, variant: "uniform_groups_6_7_forward", seed: 2026073001 }, ]; for (const item of expectedRaw) { const name = item.path.split("/").at(-1); const raw = read(`../experiments/k3/attnres_forward/results/raw/${name}`); expect(raw.sha256 === item.sha256, `${name} physical SHA-256 mismatch`); expect(/^[0-9a-f]{64}$/.test(raw.json.canonical_sha256_without_self), `${name} canonical self-hash missing`); expect(raw.json.forward_intervention.passed, `${name} forward audit failed`); expect(raw.json.forward_intervention.forward_calls === 8054, `${name} forward census changed`); expect(raw.json.target_bytes_seen === 65536000, `${name} target bytes changed`); } const primary = compact.json.primary; expect(primary.material_response_passed, "primary material response failed"); expect(primary.passed_cells === 6 && primary.required_cells === 6, "primary attenuation is not 6/6"); expect(primary.cells.every((cell) => cell.relative_drop >= 0.2 && cell.passed), "a primary attenuation cell fell below 20%"); expect(primary.quality.passed && primary.quality.passed_checks === 4, "BPC quality gate is not 4/4"); expect(close(primary.quality.mean_delta_bpc, 0.0065784582165467525), "mean BPC delta changed"); expect(close(Math.min(...primary.cells.filter((cell) => cell.metric === "spike_contrast").map((cell) => cell.relative_drop)), 0.6211080443849011), "minimum contrast drop changed"); expect(close(Math.min(...primary.cells.filter((cell) => cell.metric === "peak_normalized").map((cell) => cell.relative_drop)), 0.3234439150908039), "minimum peak drop changed"); expect(Object.values(compact.json.secondary_status).every((value) => value === "secondary_material_response"), "secondary status changed"); expect(compact.json.trajectories.length === 90, "trajectory cell count changed"); expect(compact.json.final_spectra.length === 15, "final spectrum count changed"); expect(compact.json.final_spectra.every((item) => item.normalized.length === 32), "a final spectrum is not 32 layers"); expect(compact.json.interaction.cells.length === 36, "interaction cell count changed"); expect(compact.json.interaction.summaries.length === 12, "interaction summary count changed"); expect(close( compact.json.interaction.summaries.find((item) => item.step === 8000 && item.metric === "spike_contrast").mean_interaction_residual, -0.3670379672721313, ), "final contrast I67 changed"); expect(close( compact.json.interaction.summaries.find((item) => item.step === 8000 && item.metric === "peak_normalized").mean_interaction_residual, -0.17044758609080035, ), "final peak I67 changed"); if (failures.length) { console.error(`FAIL K3 AttnRes forward-training data\n- ${failures.join("\n- ")}`); process.exit(1); } console.log(JSON.stringify({ protocol: compact.json.protocol_id, status: compact.json.status, rawRuns: rawNames.length, attenuation: `${primary.passed_cells}/${primary.required_cells}`, quality: `${primary.quality.passed_checks}/${primary.quality.required_checks}`, replayExact: compact.json.replay.passed, bytes: compact.json.processed_target_bytes, hashes: { aggregate: aggregate.sha256, compact: compact.sha256, reproduction: reproduction.sha256, }, }, null, 2)); console.log("PASS K3 AttnRes forward-training frozen data");