#!/usr/bin/env python3 """lane.py -- job #3174, route 114 rev 19: was the revision ever entered? Pre-registered in work/prereg-3174.md BEFORE this file existed (and before any return page of the population was fetched). Reads are anonymous public project data; no Authorization, no document edited, no credential. stdout is the artifact: deterministic JSON, no timings, no timestamps, sorted keys -- two runs must be byte-identical. Progress, rates and elapsed time go to stderr. python lane.py > work/lane.json """ import hashlib import json import os import re import sys import time import urllib.error import urllib.request from concurrent.futures import ThreadPoolExecutor from pathlib import Path BASE = "https://solveathome.org/projects/twin-primes" UA = "sah-research-agent/1.0" WORK = Path(__file__).resolve().parent CACHE = WORK / "cache" RET, HIST, DOCS = CACHE / "returns", CACHE / "history", CACHE / "docs" ID_LO, ID_HI, WORKERS = 2, 1660, 4 # range/parallelism overridable for controls only ID_LO = int(os.environ.get("LANE_LO", ID_LO)) ID_HI = int(os.environ.get("LANE_HI", ID_HI)) WORKERS = int(os.environ.get("LANE_WORKERS", WORKERS)) RETRY_429 = int(os.environ.get("LANE_RETRY_429", 8)) for d in (RET, HIST, DOCS): d.mkdir(parents=True, exist_ok=True) log = lambda *a: print(*a, file=sys.stderr, flush=True) # progress -> stderr, never the artifact UNRESOLVED = [] # ids/urls whose status is neither 200 nor 404: NEVER absence BUDGET = {"waited_s": 0.0} def http(url, accept="application/json"): """(status, body). 404 is a FACT about the resource; 0 means "did not find out" and is reported as unknown, never as absence. 429 carries Retry-After and is honoured. WHY this matters (measured, 2026-09-24): the first walk of this job used 20 workers and no 429 handling, so the server's rate limiter turned 577 ids into "absent" -- the audit lane came out at 112 instead of 126 and hop-0 at 8 instead of 33. A failed request read as a fact is the exact defect this project keeps filing; the population must not be built that way. """ for attempt in range(RETRY_429): 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"" # a real answer about the resource wait = 0.0 if e.code == 429: try: wait = float(e.headers.get("Retry-After") or 0) except Exception: wait = 0.0 wait = min(max(wait, 1.0), 30.0) + 0.5 else: wait = 1.5 * (attempt + 1) BUDGET["waited_s"] += wait time.sleep(wait) except Exception: wait = 1.5 * (attempt + 1) BUDGET["waited_s"] += wait time.sleep(wait) return 0, b"" def cache_key(s): return hashlib.sha256(s.encode("utf-8")).hexdigest()[:20] def get_return(i): p = RET / ("%d.json" % i) if p.exists(): try: return i, json.loads(p.read_text(encoding="utf-8")) except Exception: p.unlink() absent = RET / ("%d.404" % i) if absent.exists(): return i, None code, b = http("%s/return/%d" % (BASE, i)) if code == 404: absent.write_text("404\n", encoding="utf-8") return i, None if code != 200: UNRESOLVED.append({"resource": "return/%d" % i, "status": code}) return i, "UNRESOLVED" p.write_bytes(b) return i, json.loads(b) def get_history(path): p = HIST / (cache_key(path) + ".json") if not p.exists(): code, b = http("%s/history/%s" % (BASE, path)) if code != 200: return {"path": path, "fetch_status": code, "versions": []} p.write_bytes(b) try: return json.loads(p.read_text(encoding="utf-8")) except Exception: return {"path": path, "fetch_status": "unparseable", "versions": []} def get_doc(path): """(bytes, status) of the CURRENT served text. status 404 = not served by the corpus; status 0 = we did not find out, which is reported as unknown and never as "unserved".""" p = DOCS / (cache_key(path) + ".bin") if p.exists(): return p.read_bytes(), 200 absent = DOCS / (cache_key(path) + ".404") if absent.exists(): return None, 404 code, b = http("%s/docs/%s" % (BASE, path), accept="text/plain, */*") if code == 200 and b: p.write_bytes(b) return b, 200 if code == 404: absent.write_text("404\n", encoding="utf-8") return None, 404 UNRESOLVED.append({"resource": "docs/%s" % path, "status": code}) return None, code def sha(b): return hashlib.sha256(b).hexdigest() # ----------------------------------------------------------------- the diff applier HUNK = re.compile(r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@") def parse_patch(text): """(headers, hunks). headers = the ---/+++ path lines; hunks carry their +/-/space body.""" headers, hunks, cur = [], [], None for line in text.split("\n"): if line.startswith("--- ") or line.startswith("+++ ") or line.startswith("diff --git"): headers.append(line) cur = None continue m = HUNK.match(line) if m: cur = {"a": int(m.group(1)), "ac": int(m.group(2)) if m.group(2) is not None else 1, "c": int(m.group(3)), "cc": int(m.group(4)) if m.group(4) is not None else 1, "body": []} hunks.append(cur) continue if cur is None: continue if line.startswith("+"): cur["body"].append(("+", line[1:])) continue elif line.startswith("-"): cur["body"].append(("-", line[1:])) elif line.startswith(" "): cur["body"].append((" ", line[1:])) elif line.startswith("\\"): continue # "\ No newline at end of file": no line content elif line == "": # A CONTEXT line is (" " + text); a bare empty line is the trailing newline of the # patch text, or a malformed body. Reading it as context appends a phantom line to # every pre-image and makes every hunk mismatch -- C3 caught exactly this on the first # run (synthetic expected "a\nb\nCC\nd\nextra\ne\n", got refusal "pre-image mismatch"). cur = None # Declared @@ counts are checked, not trusted: git apply calls a patch whose counts disagree # with its body corrupt (measured on return 294, which this applier applied happily). A # lenient applier can certify a patch no real engine accepts, so the disagreement travels with # the verdict instead of being silently repaired. for h in hunks: n_pre = sum(1 for op, _ in h["body"] if op in (" ", "-")) n_post = sum(1 for op, _ in h["body"] if op in (" ", "+")) h["counts_ok"] = (n_pre == h["ac"] and n_post == h["cc"]) return headers, hunks def apply_patch(base_text, hunks): """(result_text, 'ok') only when EVERY hunk's pre-image matches; else (None, reason).""" base = base_text.split("\n") out, pos = [], 0 for h in hunks: pre = [t for op, t in h["body"] if op in (" ", "-")] start = max(h["a"] - 1, 0) if h["a"] else 0 if start < pos: return None, "hunk overlap" if start + len(pre) > len(base): return None, "hunk past end of file" if base[start:start + len(pre)] != pre: return None, "pre-image mismatch" out.extend(base[pos:start]) for op, t in h["body"]: if op in (" ", "+"): out.append(t) pos = start + len(pre) out.extend(base[pos:]) return "\n".join(out), "ok" def is_whole_file_add(headers, hunks): if any(h.startswith("--- /dev/null") for h in headers): return True return (len(hunks) == 1 and hunks[0]["a"] == 0 and hunks[0]["ac"] == 0 and all(op in ("+", "\\") for op, _ in hunks[0]["body"])) # ----------------------------------------------------------------- fetch the lane t0 = time.time() ids = list(range(ID_LO, ID_HI)) results = {} with ThreadPoolExecutor(max_workers=WORKERS) as ex: for i, j in ex.map(get_return, ids): results[i] = j log("walked ids %d..%d in %.1fs; %d pages present, %d 404, %d unresolved, waited %.0fs on limits" % (ID_LO, ID_HI - 1, time.time() - t0, sum(1 for v in results.values() if isinstance(v, dict)), sum(1 for v in results.values() if v is None), sum(1 for v in results.values() if v == "UNRESOLVED"), BUDGET["waited_s"])) # NEVER build a population over an unknown: a missing page is reported, not treated as absent. if any(v == "UNRESOLVED" or v is None for v in results.values()): log("NOTE: %d ids unread; a 404 is only absence when the server said 404" % sum(1 for v in results.values() if v == "UNRESOLVED")) audits = {i: j for i, j in results.items() if isinstance(j, dict) and j.get("type") == "audit"} def field(j, k): v = j.get(k) return v if v not in ("", None) else None # /history per distinct revision_path, only for audits that carry a revision_sha paths = sorted({field(j, "revision_path") for j in audits.values() if field(j, "revision_path")}) hist, hist_raw = {}, {} for p in paths: h = get_history(p) hist_raw[p] = h hist[p] = {v.get("content_sha") for v in (h.get("versions") or []) if v.get("content_sha")} log("history rows for %d distinct revision_paths" % len(paths)) with_rs = {i: j for i, j in audits.items() if field(j, "revision_sha")} noserved = {i: j for i, j in with_rs.items() if field(j, "revision_sha") not in hist.get(field(j, "revision_path") or "", set())} hop0 = {i: j for i, j in with_rs.items() if i not in noserved} log("audits %d | with revision_sha %d | no served row %d | hop0 %d" % (len(audits), len(with_rs), len(noserved), len(hop0))) # ----------------------------------------------------------------- controls doc_cache = {} doc_status = {} def doc_for(path): if path not in doc_cache: b, st = get_doc(path) doc_cache[path] = b doc_status[path] = st return doc_cache[path] # C1: does /docs serve exactly the bytes the /history row describes? R2 depends on this, so it # is the control that matters. (An earlier form of C1 asked instead whether the served text # hashes to a HOP-0 audit's revision_sha, which is a strictly weaker question -- a hop-0 audit may # name an older row -- and it scored 1/5 on the smoke range, which is how the distinction showed # up. Both numbers are reported.) doc_identity = {} for p in paths: vs = hist_raw[p].get("versions") or [] last_sha = vs[-1].get("content_sha") if vs else None b = doc_for(p) served = sha(b) if b else None doc_identity[p] = {"served_sha256": served, "fetch_status": doc_status.get(p), "last_version_sha": last_sha, "versions": len(vs), # THREE states, kept apart on purpose: a path with NO rows is not a # mismatch, it is a path the ledger says nothing about (the first version # of this control conflated the two and called 23 zero-row paths mismatched). "no_versions": len(vs) == 0, "match": bool(served and last_sha and served == last_sha)} c1_checked = sum(1 for v in doc_identity.values() if v["served_sha256"]) c1_match = sorted(p for p, v in doc_identity.items() if v["match"]) c1_zero = sorted(p for p, v in doc_identity.items() if v["no_versions"]) c1_mismatch = sorted(p for p, v in doc_identity.items() if v["served_sha256"] and v["last_version_sha"] and not v["match"]) c1_nodoc = sorted(p for p, v in doc_identity.items() if not v["served_sha256"]) c1b = sorted(i for i, j in hop0.items() if doc_identity.get(field(j, "revision_path") or "", {}).get("served_sha256") == field(j, "revision_sha")) log("C1: served text == last /history row on %d of %d served paths; %d paths have ZERO ledger " "rows; %d genuine mismatches; %d unserved; %d/%d hop-0 audits name the CURRENT text" % (len(c1_match), c1_checked, len(c1_zero), len(c1_mismatch), len(c1_nodoc), len(c1b), len(hop0))) # C3: the applier on a hand-computed synthetic pair, and its refusal case syn_base = "a\nb\nc\nd\ne\n" syn_patch = "--- a/x.md\n+++ b/x.md\n@@ -2,3 +2,4 @@\n b\n-c\n+CC\n d\n+extra\n" _h, _k = parse_patch(syn_patch) syn_got, syn_why = apply_patch(syn_base, _k) syn_expect = "a\nb\nCC\nd\nextra\ne\n" syn_displaced, syn_displaced_why = apply_patch("a\nB\nc\nd\ne\n", _k) c3 = {"parse_ok": len(_k) == 1, "expected": syn_expect, "got": syn_got, "matches": syn_got == syn_expect, "refuses_when_displaced": syn_displaced is None, "refusal_reason": syn_displaced_why} # ----------------------------------------------------------------- the experiment per, tally = {}, {"HIT": 0, "NOT-ENTERED-TEXT": 0, "UNDECIDABLE": 0} c2 = {"flipped_byte_fails": None} for i, j in sorted(noserved.items()): path, rs = field(j, "revision_path"), field(j, "revision_sha") patch = j.get("patch") or "" rec = {"revision_path": path, "revision_sha": rs, "revision_base_sha": field(j, "revision_base_sha"), "patch_bytes": len(patch.encode("utf-8")), "status": j.get("status")} headers, hunks = parse_patch(patch) rec["hunks"] = len(hunks) rec["hunk_counts_consistent"] = bool(hunks) and all(h["counts_ok"] for h in hunks) if not patch.strip() or not hunks: rec.update(verdict="UNDECIDABLE", route="none", why="no parseable hunk in the patch") per[i] = rec tally["UNDECIDABLE"] += 1 continue hit = None if is_whole_file_add(headers, hunks): body = [t for op, t in hunks[0]["body"] if op == "+"] for variant, text in (("trailing-newline", "\n".join(body) + "\n"), ("no-trailing", "\n".join(body))): if sha(text.encode("utf-8")) == rs: hit = ("R1-whole-file-add", variant) break if hit is None: rec.update(verdict="NOT-ENTERED-TEXT", route="R1-whole-file-add", why="hash mismatch") else: rec.update(verdict="HIT", route=hit[0], variant=hit[1], why="sha256(recovered)==revision_sha") else: b = doc_for(path) if path else None if b is None: st = doc_status.get(path) rec.update(verdict="UNDECIDABLE", route="R3", doc_status=st, why=("no served document text at revision_path (404)" if st == 404 else "document text NOT OBTAINED (status %s) -- unknown, not unserved" % st)) else: try: base_text = b.decode("utf-8") enc = "utf-8" except UnicodeDecodeError: base_text, enc = b.decode("utf-8", "replace"), "utf-8/replace" got, why = apply_patch(base_text, hunks) rec["served_doc_bytes"] = len(b) rec["served_doc_decoding"] = enc rec["served_doc_sha256"] = sha(b) rec["declared_base_equals_served"] = (field(j, "revision_base_sha") == sha(b)) if got is None: rec.update(verdict="UNDECIDABLE", route="R2", why=why) elif sha(got.encode("utf-8")) == rs: rec.update(verdict="HIT", route="R2-base-is-served-text", why="clean apply and sha256(recovered)==revision_sha") else: rec.update(verdict="NOT-ENTERED-TEXT", route="R2-base-is-served-text", why="clean apply, hash mismatch") if c2["flipped_byte_fails"] is None: bad = got[:-1] + ("!" if got[-1:] != "!" else "?") c2["flipped_byte_fails"] = sha(bad.encode("utf-8")) != rs tally[rec["verdict"]] += 1 per[i] = rec # C4: #1609's control, independently reproduced -- the raw patch digest is NOT revision_sha c4_raw_eq = sum(1 for j in audits.values() if field(j, "revision_sha") and j.get("patch") and sha((j["patch"]).encode("utf-8")) == field(j, "revision_sha")) artifact = { "artifact": "lane.json", "job_id": 3174, "route": 114, "request": { "question": ("Over the audits whose revision_sha names no served version row of its own " "revision_path, was the revision ever entered?"), "prereg": "work/prereg-3174.md", "instrument": "work/lane.py", }, "lane": {"ids_walked": [ID_LO, ID_HI - 1], "audits": len(audits), "with_revision_sha": len(with_rs), "no_served_row": len(noserved), "hop0": len(hop0)}, "reproduction": { "predecessor": "#1609 (job 3158) measured the audit lane at 126 with a no-served-row class of 91", "no_served_row_here": len(noserved), "matches_91": len(noserved) == 91, }, "controls": { "C1_docs_serve_the_history_row_bytes": { "paths_total": len(paths), "paths_served": c1_checked, "match": len(c1_match), "paths_with_zero_ledger_rows": c1_zero, "genuine_mismatch": c1_mismatch, "unserved": c1_nodoc, "hop0_audits_naming_the_current_text": len(c1b), "hop0_audits_total": len(hop0)}, "C2_flipped_byte_fails": c2["flipped_byte_fails"], "C3_applier_synthetic": c3, "C4_patch_digest_equals_revision_sha": {"count": c4_raw_eq, "of": len(audits)}, }, "doc_identity": {p: doc_identity[p] for p in sorted(doc_identity)}, "tally": tally, "verdicts": {str(i): per[i] for i in sorted(per)}, } artifact["unresolved"] = sorted(UNRESOLVED, key=lambda d: (d["resource"], str(d["status"]))) artifact["complete"] = not UNRESOLVED json.dump(artifact, sys.stdout, indent=1, sort_keys=True, ensure_ascii=False) sys.stdout.write("\n") log("done in %.1fs; tally %s; unresolved %d" % (time.time() - t0, tally, len(UNRESOLVED))) if UNRESOLVED: log("REFUSING: %d resources ended without an answer; the artifact above is NOT a population" % len(UNRESOLVED)) sys.exit(2)