#!/usr/bin/env python3 """verify-gitapply.py -- independent check of lane.py's HIT / NOT-RECOVERABLE claims. INDEPENDENCE. lane.py applies patches with an applier written for this job. This checker uses a diff applier written by somebody else -- `git apply` -- on freshly fetched bytes, in a scratch repository, and compares sha256 of the result with the declared `revision_sha`. It shares no code with lane.py: different parser, different apply engine, different fetch path. Deterministic stdout (no timings, sorted); progress to stderr. python verify-gitapply.py > work/verify-gitapply.json """ import hashlib import json import os import re import shutil import subprocess import sys import tempfile import time import urllib.error import urllib.request from pathlib import Path BASE = "https://solveathome.org/projects/twin-primes" UA = "sah-verifier-3174/1.0" WORK = Path(__file__).resolve().parent LANE = json.loads((WORK / "lane.json").read_text(encoding="utf-8")) log = lambda *a: print(*a, file=sys.stderr, flush=True) def http(url, accept="application/json"): for attempt in range(8): req = urllib.request.Request(url, headers={"User-Agent": UA, "Accept": accept}) try: with urllib.request.urlopen(req, timeout=45) as f: return f.status, f.read() except urllib.error.HTTPError as e: if e.code in (404, 403, 410): return e.code, b"" wait = 1.0 try: wait = min(max(float(e.headers.get("Retry-After") or 1.0), 1.0), 30.0) + 0.5 except Exception: pass time.sleep(wait) except Exception: time.sleep(1.5 * (attempt + 1)) return 0, b"" def sha(b): return hashlib.sha256(b).hexdigest() def target_path(patch): """The +++ line carries a TAB plus a mtime stamp (`+++ b/out/x.md\t2026-09-09 17:24:26`), which is not part of the path -- taking the rest of the line verbatim made os.open fail with EINVAL on Windows (first run). git apply itself knows the convention; only this parser did not.""" m = re.search(r"^\+\+\+ (.+)$", patch, re.M) if not m: return "target.md" raw = m.group(1) raw = re.split(r"\t", raw)[0] raw = re.split(r" +(?=\d{4}-\d{2}-\d{2})", raw)[0] if raw.startswith("b/"): raw = raw[2:] return raw.strip() or "target.md" def normalize(patch, path): """Point the ---/+++ header at one file we create ourselves. DISCLOSED NORMALISATION. The record carries TWO header conventions: `--- a/x` / `+++ b/x`, and tool-generated scratch names (`--- agents/docs/paper/beta2-note.md` / `+++ revision/...`, `--- varE.md` / `+++ varE-revised.md`). git apply resolves the OLD name as an input path and refuses the second convention outright, which says nothing about the change itself. The hunk bodies -- everything that is re-hashed -- are untouched; only the two header lines are pinned to the single file this checker writes the served bytes into. """ lines, seen = patch.split("\n"), 0 for k, l in enumerate(lines): if seen == 0 and l.startswith("--- "): lines[k] = "--- a/" + path seen = 1 elif seen == 1 and l.startswith("+++ "): lines[k] = "+++ b/" + path seen = 2 break return "\n".join(lines), seen == 2 def git_apply(served_bytes, patch, path): """(applied, result_sha, detail). A real apply engine decides, not this script.""" with tempfile.TemporaryDirectory() as td: d = Path(td) subprocess.run(["git", "init", "-q"], cwd=d, capture_output=True) f = d / path f.parent.mkdir(parents=True, exist_ok=True) f.write_bytes(served_bytes) norm, ok = normalize(patch, path) pf = d / "change.patch" pf.write_bytes(norm.encode("utf-8")) r = subprocess.run(["git", "apply", "--verbose", "-p1", "change.patch"], cwd=d, capture_output=True, text=True, env=dict(os.environ, GIT_CONFIG_GLOBAL=os.devnull, GIT_CONFIG_SYSTEM=os.devnull)) if r.returncode != 0: return False, None, (r.stderr or r.stdout).strip().splitlines()[-1:] or ["failed"] return True, sha(f.read_bytes()), None out = {"checker": "work/verify-gitapply.py", "of": "work/lane.json", "engine": "git apply (independent of lane.py's applier)", "cases": {}} t0 = time.time() for i, v in sorted(LANE["verdicts"].items(), key=lambda kv: int(kv[0])): if v["verdict"] not in ("HIT", "NOT-ENTERED-TEXT"): continue st, body = http("%s/return/%s" % (BASE, i)) if st != 200: out["cases"][i] = {"fetched": st, "agrees": False} continue page = json.loads(body) patch = page.get("patch") or "" st2, doc = http("%s/docs/%s" % (BASE, v["revision_path"]), accept="text/plain, */*") if st2 != 200: out["cases"][i] = {"doc_fetch": st2, "agrees": False} continue applied, got, detail = git_apply(doc, patch, target_path(patch)) agree = applied and got == v["revision_sha"] # negative control on the same case: displace one byte of the base; git must fail or the # hash must move. A test that cannot fail is not a test. broken = bytearray(doc) idx = next((k for k, c in enumerate(broken) if c == ord("\n")), 0) broken[idx:idx + 1] = b"Q" b_applied, b_got, b_detail = git_apply(bytes(broken), patch, target_path(patch)) ctl = (not b_applied) or (b_got != got) out["cases"][i] = {"lane_verdict": v["verdict"], "lane_route": v["route"], "path": v["revision_path"], "declared_revision_sha": v["revision_sha"], "git_applied": applied, "git_result_sha": got, "agrees": agree, "detail": detail, "displaced_base_changes_outcome": ctl} log(" %-5s %-16s git_applied=%s agrees=%s" % (i, v["verdict"], applied, agree)) hits = [k for k, c in out["cases"].items() if c.get("lane_verdict") == "HIT"] neg = [k for k, c in out["cases"].items() if c.get("lane_verdict") == "NOT-ENTERED-TEXT"] out["summary"] = { "hit_cases": len(hits), "hit_agreements": sum(1 for k in hits if out["cases"][k]["agrees"]), "negative_cases": len(neg), "negative_agreements": sum( 1 for k in neg if out["cases"][k]["git_applied"] and not out["cases"][k]["agrees"]), "controls_that_can_fail": sum(1 for c in out["cases"].values() if c.get("displaced_base_changes_outcome")), "cases": len(out["cases"]), } json.dump(out, sys.stdout, indent=1, sort_keys=True, ensure_ascii=False) sys.stdout.write("\n") log("done in %.1fs; %s" % (time.time() - t0, out["summary"])) sys.exit(0 if out["summary"]["hit_agreements"] == out["summary"]["hit_cases"] else 1)