#!/usr/bin/env python3 """verify-served-reversion.py -- the checker for job #2962's finite claim, offline. What it checks, from `reversion-evidence.json` alone (raw `/history` answers, served documents and version blobs for the 12 documents that carry a version record -- no network, no credential): 1. the STABLE REVERT RULE. For every one of the 12 documents it re-derives the classification from the version shas: a document is *reverted and still serving* when the newest version's content equals an earlier version's content, a distinct verified revision sits between them, and the served bytes hash to that newest version. Registered expectation: exactly 9 such documents, with the nine paths and the discarded return ids the return names. 2. the COSMETIC BOUNDARY, per document. For each of the 9, the checker diffs the served text against the discarded blob three ways -- raw, whitespace-normalised, and link-stripped as well -- and requires the registered counts exactly (e.g. corner-correlation.md 22/21/19) and at least one changed line under the strongest normalisation (a mirror filter that only reflows or strips links would leave 0; #1402 read two of these documents as cosmetic and is corrected here). 3. the REPAIR. research/fixed-endpoint-discrepancy.md must have 4 versions, be classified by the stable rule as reverted-then-revisioned (not as still-serving-reverted), and carry the re-application line in its served text -- while the fixed three-version shape, which this audit's first run used, must classify it as nothing at all (4 versions fall through its `len(vs) == 3` branch). Both readings are printed. 4. the REGISTRY, under both parsers. Row-level comparison of the served and discarded QUESTIONS.md layouts: the wide rule (`| `Q-...` | status |`) matches 554 rows in each layout, #1556's stricter rule (lowercase slug, `[A-Z]+` status, four cells) matches 482, and BOTH find exactly one flip -- `Q-shadow-prereg` served OPEN / discarded ANSWERED. The count is parser-scoped; the flip is not. 5. its own CONTROLS: a synthetic revert must fire, a synthetic three-distinct-revision document must not, a link-only text pair must count as changed raw but 0 after link stripping, a registry pair that is identical must yield no flip, and a row with an uppercase letter or a dot in its slug must be counted by the wide rule and dropped by the strict one. Exit codes: 0 PASS, 2 a registered value differs, 3 a control was not detected. python3 verify-served-reversion.py --bundle reversion-evidence.json [--audit served-reversion-audit.json] """ import argparse import difflib import hashlib import io import json import re import sys LINK = re.compile(r"\[([^\]]*)\]\([^)]*\)") ROW = re.compile(r"^\|\s*`(Q-[A-Za-z0-9._-]+)`\s*\|([^|]*)\|") MARKER = "Re-applied 2026-09-19" # Registered expectations -- the numbers the return states, before any check runs. REVERTED = { # path: (discarded_return, raw, ws, ws+link) "paper/beta2-note.md": ("20", 247, 228, 228), "paper/proposals/prop-staircase-note.md": ("13", 86, 85, 85), "research/QUESTIONS.md": ("80", 4, 4, 4), "research/centered-discrepancy-estimate.md": ("83", 6, 6, 6), "research/corner-correlation.md": ("92", 22, 21, 19), "research/fold-arithmetic-bridge.md": ("101", 69, 52, 52), "research/global-factor-signs.md": ("153", 4, 4, 4), "research/history/staging/derive-0904-L7-transfer.md": ("152", 2, 2, 2), "research/history/staging/xchan-at29-prereg.md": ("85", 4, 4, 4), } REPAIRED = {"path": "research/fixed-endpoint-discrepancy.md", "n_versions": 4, "discarded_return": "151", "newest_return": "1333", "marker": MARKER} TWO_VERSIONS = {"paper/wall-note.md": "1323", "research/OUTCOMES.md": "988"} REGISTRY = {"wide_rows": 554, "strict_rows": 482, "status_flips": [{"slug": "Q-shadow-prereg", "served": "OPEN", "discarded": "ANSWERED"}]} ROW_STRICT = re.compile(r"^\|\s*`(Q-[a-z0-9-]+)`\s*\|\s*([A-Z]+)\s*\|\s*([^|]*)\|\s*([^|]*)\|") TRACKED, UNTRACKED = 12, 1199 SUBSTANTIVE = {"substantive": 9, "cosmetic": 0, "identical": 0, "surviving_both_normalisations": 9} def ws(text): out = [] for ln in (text or "").splitlines(): s = re.sub(r"\s+", " ", ln).strip() if s: out.append(s) return out def ws_link(text): return [LINK.sub(r"\1", s) for s in ws(text)] def diffcount(x, y): return sum(1 for l in difflib.unified_diff(x, y, n=0, lineterm="") if l[:1] in "+-" and not l.startswith(("+++", "---"))) def stable_rule(shas, served_sha): """The return's rule, implemented here from the shas alone.""" n = len(shas) revert_at = [k + 1 for k in range(1, n) if shas[k] == shas[0] and shas[k - 1] != shas[0]] if n == 1: return "single_version_control", revert_at if n == 2: return "two_versions", revert_at if revert_at and shas[-1] == shas[0]: return ("reverted_and_still_serving" if served_sha == shas[-1] else "reverted_not_served", revert_at) if revert_at: return "reverted_then_revisioned", revert_at return "revisioned", revert_at def fixed_three_version_shape(shas): """What the audit's first run tested: exactly three versions, v3 == v1 != v2.""" if len(shas) != 3: return "not_three_versions" return "revert" if (shas[2] == shas[0] and shas[1] != shas[0]) else "no_revert" def rows_of(text, regex=None): out = {} for ln in (text or "").splitlines(): m = (regex or ROW).match(ln) if m: out[m.group(1)] = m.group(2).strip() return out def rows_of_strict(text): return rows_of(text, ROW_STRICT) def controls(): c = [] # positive: a re-import shape must fire kind, at = stable_rule(["a" * 4, "b" * 4, "a" * 4], "a" * 4) c.append({"name": "synthetic re-import fires", "detected": kind == "reverted_and_still_serving" and at == [3]}) # negative: three distinct revisions must not kind, at = stable_rule(["a" * 4, "b" * 4, "c" * 4], "c" * 4) c.append({"name": "synthetic genuine revision does not fire", "detected": kind == "revisioned" and at == []}) # negative: a link-only difference is cosmetic under ws+link, changed under raw x = ["see [the note](old/target.md) for the argument"] y = ["see [the note](new/target.md) for the argument"] c.append({"name": "link-only difference is raw-changed but cosmetic", "detected": diffcount(x, y) == 2 and diffcount(ws(x[0]), ws(y[0])) == 2 and diffcount(ws_link(x[0]), ws_link(y[0])) == 0}) # negative: identical registries flip nothing same = "| `Q-x` | OPEN | a |\n| `Q-y` | ANSWERED | b |\n" ra, rb = rows_of(same), rows_of(same) flips = [k for k in set(ra) & set(rb) if ra[k] != rb[k]] c.append({"name": "identical registry flips nothing", "detected": len(ra) == 2 and not flips}) # the two parsers differ by exactly the rows with an uppercase letter or a dot in the slug mixed = ("| `Q-DP1-mechanism` | ANSWERED | q | v |\n" "| `Q-shadow-prereg` | OPEN | q | v |\n") wide, strict = rows_of(mixed), rows_of_strict(mixed) c.append({"name": "wide rule counts an uppercase slug, strict rule drops it", "detected": sorted(wide) == ["Q-DP1-mechanism", "Q-shadow-prereg"] and sorted(strict) == ["Q-shadow-prereg"]}) return c def main(): ap = argparse.ArgumentParser() ap.add_argument("--bundle", required=True) ap.add_argument("--audit", default=None) a = ap.parse_args() b = json.load(io.open(a.bundle, encoding="utf-8")) bad, derived = [], {} docs = {d["path"]: d for d in b["documents"]} ctl = controls() if not all(x["detected"] for x in ctl): print(json.dumps({"verdict": "CONTROL-FAILED", "controls": ctl}, indent=1)) return 3 # --- 1. the rule over the version lists --------------------------------------------------------- kinds, still, revisioned = {}, [], [] for path, d in sorted(docs.items()): shas = [v["content_sha"] for v in d["versions"]] kind, at = stable_rule(shas, d["served_sha256"]) kinds[path] = kind if kind == "reverted_and_still_serving": still.append(path) if kind == "reverted_then_revisioned": revisioned.append(path) if not d["served_equals_newest_version"]: bad.append("%s: the served bytes are not its newest recorded version" % path) for sha, text in d["blobs"].items(): if hashlib.sha256(text.encode("utf-8")).hexdigest() != sha: bad.append("%s: blob %s does not hash to its sha" % (path, sha[:12])) derived["counts"] = {"tracked": len(docs), "reverted_and_still_serving": len(still), "reverted_then_revisioned": len(revisioned), "two_versions": sum(1 for k in kinds.values() if k == "two_versions")} if len(docs) != TRACKED: bad.append("tracked documents: %d, registered %d" % (len(docs), TRACKED)) if b.get("untracked_document_count") != UNTRACKED: bad.append("untracked documents: %s, registered %d" % (b.get("untracked_document_count"), UNTRACKED)) if sorted(still) != sorted(REVERTED): bad.append("still-serving set differs: %s" % sorted(set(still) ^ set(REVERTED))) # --- 2. the cosmetic boundary, per document ---------------------------------------------------- counts = {} for path, (ret, raw, nrm, nolink) in sorted(REVERTED.items()): d = docs.get(path) if not d: bad.append("no evidence for %s" % path) continue served = d["blobs"].get(d["served_sha256"]) disc_v = [v for v in d["versions"] if v.get("return_id") == ret] if served is None or not disc_v: bad.append("%s: served text or discarded revision missing (return %s)" % (path, ret)) continue disc = d["blobs"].get(disc_v[0]["content_sha"]) if disc is None: bad.append("%s: discarded blob missing" % path) continue got = (diffcount(served.splitlines(), disc.splitlines()), diffcount(ws(served), ws(disc)), diffcount(ws_link(served), ws_link(disc))) counts[path] = got if got != (raw, nrm, nolink): bad.append("%s: %s, registered %s" % (path, got, (raw, nrm, nolink))) if got[2] == 0: bad.append("%s: 0 changed lines after link stripping -- cosmetic" % path) derived["changed_lines"] = counts # --- 3. the repair ----------------------------------------------------------------------------- p = REPAIRED["path"] d = docs.get(p, {"versions": [], "blobs": {}, "served_sha256": None}) shas = [v["content_sha"] for v in d["versions"]] served = d["blobs"].get(d["served_sha256"]) or "" kind, at = stable_rule(shas, d["served_sha256"]) derived["repair"] = {"n_versions": len(shas), "stable_rule": kind, "revert_at": at, "fixed_shape": fixed_three_version_shape(shas), "marker_present": REPAIRED["marker"] in served} if len(shas) != REPAIRED["n_versions"] or kind != "reverted_then_revisioned": bad.append("%s: %d versions, stable rule %s" % (p, len(shas), kind)) if not derived["repair"]["marker_present"]: bad.append("%s: the re-application marker is not in the served text" % p) if [v.get("return_id") for v in d["versions"]].count(REPAIRED["newest_return"]) != 1: bad.append("%s: no single newest version carries return %s" % (p, REPAIRED["newest_return"])) # --- 4. the registry --------------------------------------------------------------------------- q = docs.get("research/QUESTIONS.md", {"versions": [], "blobs": {}, "served_sha256": None}) qserved = q["blobs"].get(q["served_sha256"]) or "" qdisc_v = [v for v in q["versions"] if v.get("return_id") == REVERTED["research/QUESTIONS.md"][0]] qdisc = q["blobs"].get(qdisc_v[0]["content_sha"]) if qdisc_v else "" ra, rb = rows_of(qserved), rows_of(qdisc or "") sa, sb = rows_of_strict(qserved), rows_of_strict(qdisc or "") flips = [{"slug": k, "served": ra[k], "discarded": rb[k]} for k in sorted(set(ra) & set(rb)) if ra[k] != rb[k]] sflips = [{"slug": k, "served": sa[k], "discarded": sb[k]} for k in sorted(set(sa) & set(sb)) if sa[k] != sb[k]] derived["registry"] = {"wide_rows": len(ra), "discarded_rows": len(rb), "strict_rows": len(sa), "strict_discarded_rows": len(sb), "only_in_served": sorted(set(ra) - set(rb)), "only_in_discarded": sorted(set(rb) - set(ra)), "status_flips": flips, "strict_status_flips": sflips} if (len(ra), len(rb), flips) != (REGISTRY["wide_rows"], REGISTRY["wide_rows"], REGISTRY["status_flips"]): bad.append("registry differs under the wide rule: %d/%d rows, flips %s" % (len(ra), len(rb), flips)) if (len(sa), len(sb), sflips) != (REGISTRY["strict_rows"], REGISTRY["strict_rows"], REGISTRY["status_flips"]): bad.append("registry differs under #1556's strict rule: %d/%d rows, flips %s" % (len(sa), len(sb), sflips)) for ret, newest in sorted(TWO_VERSIONS.items()): d2 = docs.get(ret, {"versions": []}) ids = [v.get("return_id") for v in d2["versions"]] if len(ids) != 2 or ids[-1] != newest: bad.append("%s: two_versions expectation %s not met: %s" % (ret, newest, ids)) # --- 5. the published audit must agree with the bytes ------------------------------------------ if a.audit: au = json.load(io.open(a.audit, encoding="utf-8")) agree = { "revert_count": au.get("revert_count") == len(still), "kinds": (au.get("kinds") or {}).get("three_version_revert") == len(still) and (au.get("kinds") or {}).get("many_versions") == 1 and (au.get("kinds") or {}).get("two_versions") == 2, "substantive_vs_cosmetic": au.get("substantive_vs_cosmetic") == SUBSTANTIVE, "registry": au.get("registry") == {"served_rows": len(ra), "discarded_rows": len(rb), "only_in_served": [], "only_in_discarded": [], "status_flips": flips}, "registry_is_parser_scoped": len(sa) == REGISTRY["strict_rows"] and sflips == flips, "discarded_returns": sorted(au.get("discarded_returns") or []) == sorted(r for r, _, _, _ in REVERTED.values()), } derived["audit_agreement"] = agree for k, ok in agree.items(): if not ok: bad.append("the published audit disagrees with the bytes on %s" % k) out = {"verdict": "PASS" if not bad else "FAIL", "differences": bad, "derived": derived, "controls": ctl, "controls_all_detected": all(x["detected"] for x in ctl)} print(json.dumps(out, indent=1, sort_keys=True)) return 0 if not bad else 2 if __name__ == "__main__": sys.exit(main())