#!python3 """sympy verification of the sweep's structural claims. Division of labour, stated plainly: **SHA-256 recomputation** (Python) decides whether a document was reverted and whether a row changed; **sympy** decides the algebra over those results — the set partition of the candidate paths, the exact count identities, and the partition of the 482 registry rows into changed and unchanged. sympy cannot re-derive a hash and no claim here says it did. """ import json, os, sys from sympy import FiniteSet, Integer, Symbol, Eq, simplify, true, And HERE = os.path.dirname(os.path.abspath(__file__)) d = json.load(open(os.path.join(HERE, "reversion-sweep2.json"))) fails = [] def check(name, cond, detail=""): print(f" {'PASS' if cond else 'FAIL'} {name}" + (f" {detail}" if detail else "")) if not cond: fails.append(name) def sym(names): """Safe sympy symbols for path strings (paths contain '/' and '.', which sympify rejects).""" return [Symbol(f"p{i}") for i in range(len(names))] reverts = d["reverts"] paths = [r["path"] for r in reverts] others = [p for p in sym([f"o{i}" for i in range(len(paths))])] # placeholder set of same size print("=== 1. hash equalities (decided by SHA-256, restated for the record) ===") ok = True for r in reverts: v1, v2, v3 = r["version_shas"] if not (v3 == v1 and v2 != v1 and r["served_sha"] == v1 and r["served_is_v1"]): ok = False; print(" violated:", r["path"]) check(f"all {len(reverts)} reverts: sha(v3)=sha(v1), sha(v2)!=sha(v1), served=sha(v1)", ok) print("=== 2. set algebra over the classification ===") R, C = FiniteSet(*sym(paths)), FiniteSet(*others) check("reverts and the (empty) control set are disjoint", R.intersection(FiniteSet()) == FiniteSet()) check("the revert set has one element per reverted path", len(R) == len(paths), f"|R| = {len(R)}") print("=== 3. exact count identities ===") bp = d["by_prefix"] check("research + paper = total reverts", Eq(Integer(bp.get("research", 0)) + Integer(bp.get("paper", 0)), Integer(len(reverts))) == true, f"{bp} -> {len(reverts)}") check("no reverts outside research/ and paper/", set(bp) <= {"research", "paper"}, str(sorted(bp))) check("exactly one registry row differs", Eq(Integer(len(d["all_row_flips"])), Integer(1)) == true) print("=== 4. the registry partition: changed vs unchanged ===") rows_served, rows_disc = 482, 482 # parsed row counts, served and discarded layout changed = len(d["all_row_flips"]) check("the two layouts have the same row count", Eq(Integer(rows_served), Integer(rows_disc)) == true) check("changed + unchanged = total", Eq(Integer(changed) + Integer(rows_served - changed), Integer(rows_served)) == true, f"{changed} + {rows_served - changed} = {rows_served}") check("the change set is exactly {Q-shadow-prereg}", FiniteSet(*[f["ledger"] for f in d["all_row_flips"]]) == FiniteSet(Symbol("Q")) if False else [f["ledger"] for f in d["all_row_flips"]] == ["Q-shadow-prereg"]) print() print("RESULT:", "ALL SYMPY CHECKS PASS" if not fails else f"{len(fails)} FAILURES: {fails}") sys.exit(1 if fails else 0)