#!/usr/bin/env python3 """Negative controls for the transfer986 verification package (job #1005). Originals are never modified: every mutation is written to controls// and the checker is run there, with the input and checker copied byte-identically. Each control records: exit code, whether the checker rejected, and the first AssertionError message (the checker's own defect detector). """ import hashlib import json import os import re import shutil import subprocess import sys SRC = "." OUT = "controls" CHECKER = "transfer986-checker.py" INPUT = "transfer986-input.json" TARGET = "transfer986-result.json" def sha(path): return hashlib.sha256(open(path, "rb").read()).hexdigest() def run_case(name, mutate, py="python3"): d = os.path.join(OUT, name) os.makedirs(d, exist_ok=True) shutil.copy(CHECKER, d) shutil.copy(INPUT, d) tgt = json.loads(open(TARGET).read()) mutate(tgt) open(os.path.join(d, TARGET), "w").write(json.dumps(tgt)) p = subprocess.run([py, CHECKER, INPUT, TARGET], cwd=d, capture_output=True, text=True) err = (p.stderr or "").strip().splitlines() msg = err[-1] if err else "" m = re.search(r"(AssertionError|Error|Exception)[^\n]*", msg) verdict = "REJECTED" if p.returncode != 0 else "ACCEPTED (control did not fire)" print(f"{name:34s} exit={p.returncode:>3} {verdict:32s} {m.group(0)[:70] if m else ''}") return p.returncode != 0 def main(): os.makedirs(OUT, exist_ok=True) print("checker sha:", sha(CHECKER)) print("input sha:", sha(INPUT)) print("target sha:", sha(TARGET)) print() ok = [] # 1. a single corrupted capacity digit in the delivered target def c1(t): r = t["rows"][3] r["uniform"]["per_prime"][0] += 1 ok.append(run_case("1_capacity_digit", c1)) # 2. the aggregate verdict itself def c2(t): t["H"] = 1 ok.append(run_case("2_aggregate_H", c2)) # 3. a missing record: one window row dropped def c3(t): t["rows"].pop(5) ok.append(run_case("3_missing_window", c3)) # 4. a control vector with a broken weight multiset (multiset invariant) def c4(t): c = t["rows"][2]["controls"][0]["weights"] c[0], c[1] = c[1] + 1, c[0] - 1 ok.append(run_case("4_control_multiset", c4)) # 5. a control vector that is not the seeded one, multiset preserved def c5(t): c = t["rows"][2]["controls"][4]["weights"] c[0], c[7] = c[7], c[0] ok.append(run_case("5_control_not_seeded", c5)) # 6. an argmax outside its phase domain def c6(t): t["rows"][1]["uniform"]["argmax"][0] = 10 ** 6 ok.append(run_case("6_argmax_out_of_domain", c6)) # 7. adjudicated flags flipped (the H rule's own gates) def c7(t): t["rows"][0]["counted"] = not t["rows"][0]["counted"] ok.append(run_case("7_counted_flag_flipped", c7)) # 8. the input's tested domain changed under the target's feet def c8(t): t["rows"][0]["a"] += 2 ok.append(run_case("8_window_domain_shift", c8)) # 9. the input's weight vector changed (S invariant) def c9(t): t["S"] += 1 ok.append(run_case("9_S_invariant", c9)) # environment: same package under a second interpreter print() ok.append(run_case("10_env_python3.12", lambda t: None, py="/opt/homebrew/bin/python3.12")) print() print(f"controls firing: {sum(ok)}/{len(ok)}") return 0 if all(ok) else 1 if __name__ == "__main__": sys.exit(main())