#!/usr/bin/env python3 """The check a stranger can run: from the SHIPPED artifacts only, reproduce #1357's numbers. Inputs, all pinned by sha256 in the return's manifest: snapshot-2026-09-20.zip the measured snapshot, as measured (CRLF included) snapshot-2026-09-20-manifest.json per-file sha256 of the raw bytes and of the LF bytes freshness-triager.py #1357's instrument, unmodified saved1354/ the documents return #1354 shipped, pinned by their own sha256 Steps, in order, with the failure named at each one: (1) archive sha256 equals the manifest's -- a byte-level identity of the shipped input; (2) unzip into a scratch dir and check EVERY file against the manifest's `sha256_raw` (and, for the LF twin, `sha256_lf`), so a corrupt or truncated archive cannot pass; (3) run `python3 freshness-triager.py saved1354 out.json` and compare its five stderr summary lines to the ones #1357 published, by exact string equality; (4) compare the produced artifact to #1357's `freshness-triage.json` field by field (JSON equality with the `root` key excluded, since that is a local path by construction); (5) the review's control: the same run over the newline-normalised twin, requiring identical Q1/Q2/Q3 and `crlf_files=0`. usage: verify_package.py [--pinned ] [--triager ] [--saved ] exit 0 when every step passes, 1 otherwise (each failure printed). """ import hashlib import json import os import shutil import subprocess import sys import tempfile import zipfile sys.stdout.reconfigure(encoding="utf-8", errors="replace") HERE = os.path.dirname(os.path.abspath(__file__)) EXPECT_STDERR = [ "files: 1128", "Q0 control all_text_match=True byte_identical=0/4 crlf_files=1118", "Q1 any_hit=True outside=419", "Q2 blocks=602 ids=555 multi=22", "Q3 matched=554 mismatch=0 norow=1", ] CONTROL_STDERR = [ "files: 1128", "Q0 control all_text_match=True byte_identical=4/4 crlf_files=0", "Q1 any_hit=True outside=419", "Q2 blocks=602 ids=555 multi=22", "Q3 matched=554 mismatch=0 norow=1", ] def sha(b): return hashlib.sha256(b).hexdigest() def failures(): args = sys.argv[1:] pinned = args[args.index("--pinned") + 1] if "--pinned" in args else os.path.join(HERE, "pinned") triager = args[args.index("--triager") + 1] if "--triager" in args else os.path.join(HERE, "from1357", "freshness-triager.py") saved = args[args.index("--saved") + 1] if "--saved" in args else os.path.join(HERE, "saved1354") name = "snapshot-2026-09-20" zpath, mpath = os.path.join(pinned, name + ".zip"), os.path.join(pinned, name + "-manifest.json") out = [] man = json.load(open(mpath, encoding="utf-8")) meta, rows = man["manifest"], man["files"] print("manifest: %d files, raw %d bytes, %d with CRLF, archive sha256 %s" % (meta["files"], meta["bytes_total_raw"], meta["files_with_crlf"], meta["sha256_archive_raw"][:16])) # (1) the shipped archive is the pinned one got = sha(open(zpath, "rb").read()) ok = got == meta["sha256_archive_raw"] print("(1) archive sha256 %s -> %s" % (got[:16], "PINNED" if ok else "MISMATCH " + got)) if not ok: out.append("archive sha256 differs from the manifest") scratch = tempfile.mkdtemp(prefix="pkg1357-") try: with zipfile.ZipFile(zpath) as z: z.extractall(scratch) # (2) every member against the manifest want = {r["path"]: r for r in rows} seen, bad = set(), [] for dirpath, _d, fns in os.walk(scratch): for fn in fns: full = os.path.join(dirpath, fn) rel = os.path.relpath(full, scratch).replace("\\", "/") seen.add(rel) raw = open(full, "rb").read() r = want.get(rel) if r is None: bad.append((rel, "not in manifest")) elif sha(raw) != r["sha256_raw"]: bad.append((rel, "raw sha mismatch")) missing = sorted(set(want) - seen) print("(2) members %d, missing %d, mismatched %d" % (len(seen), len(missing), len(bad))) if missing or bad: out.append("archive contents differ from the manifest: %d missing, %d bad" % (len(missing), len(bad))) for x in (missing[:3] + [b[0] + " (" + b[1] + ")" for b in bad[:3]]): print(" ", x) # (3)+(4) run the pinned instrument on the extracted archive o1 = os.path.join(scratch, "out-archive.json") p1 = subprocess.run([sys.executable, triager, scratch, saved, o1], capture_output=True, text=True, cwd=scratch) lines1 = [l for l in p1.stderr.replace("\r\n", "\n").splitlines() if l.startswith(("files:", "Q"))] print("(3) exit %d, stderr summary %s" % (p1.returncode, "MATCHES #1357" if lines1 == EXPECT_STDERR else "DIFFERS")) if p1.returncode != 0: out.append("triager exit %d on the pinned archive" % p1.returncode) if lines1 != EXPECT_STDERR: out.append("stderr summary differs: %r" % (lines1,)) for a, b in zip(lines1 + [""] * 9, EXPECT_STDERR + [""] * 9): if a != b: print(" got %r want %r" % (a, b)) orig = json.load(open(os.path.join(HERE, "from1357", "freshness-triage.json"), encoding="utf-8")) new = json.load(open(o1, encoding="utf-8")) orig.pop("root", None) new.pop("root", None) same = json.dumps(orig, sort_keys=True) == json.dumps(new, sort_keys=True) print("(4) artifact field-for-field vs #1357: %s (sha256 %s)" % ("IDENTICAL" if same else "DIFFERS", sha(open(o1, "rb").read())[:16])) if not same: out.append("artifact differs from #1357's freshness-triage.json") # (5) the control: the LF twin lfzip = os.path.join(pinned, name + "-lf.zip") lfdir = tempfile.mkdtemp(prefix="pkg1357-lf-") try: with zipfile.ZipFile(lfzip) as z: z.extractall(lfdir) o2 = os.path.join(lfdir, "out-lf.json") p2 = subprocess.run([sys.executable, triager, lfdir, saved, o2], capture_output=True, text=True, cwd=lfdir) lines2 = [l for l in p2.stderr.replace("\r\n", "\n").splitlines() if l.startswith(("files:", "Q"))] print("(5) LF twin: exit %d, stderr %s" % (p2.returncode, "MATCHES" if lines2 == CONTROL_STDERR else "DIFFERS")) if lines2 != CONTROL_STDERR: out.append("LF control stderr differs: %r" % (lines2,)) lf = json.load(open(o2, encoding="utf-8")) q = lambda d: {k: d.get(k) for k in ("q1_falsifier", "q2_capacity", "q3_fidelity")} same_q = json.dumps(q(orig), sort_keys=True) == json.dumps(q(lf), sort_keys=True) print(" Q1/Q2/Q3 identical under newline normalisation: %s; crlf_files=%s, byte_identical=%s" % (same_q, lf.get("q0b_byte_identity_hazard", {}).get("files_with_crlf"), lf.get("q0_control", {}).get("byte_identical"))) if not same_q: out.append("Q1/Q2/Q3 change under newline normalisation") finally: shutil.rmtree(lfdir, ignore_errors=True) finally: shutil.rmtree(scratch, ignore_errors=True) print() if out: print("FAILED: " + "; ".join(out)) return 1 print("ALL STEPS PASS: the pinned archive reproduces #1357's numbers exactly, and the counts do not") print("depend on the CRLF state that made the original un-checkable.") return 0 if __name__ == "__main__": sys.exit(failures())