#!/usr/bin/env python3 """Route 140 / job #2849: do the constants a served checker compares documents against trace to an independent record? Pre-registered in `prereg-2849.md` (sha256 372e0dbb4751b9ca745c970694709c609e27105329c30e465e5a7fc66fd378c5) before this file existed. Two stages, both resumable, both anonymous: A. walk every return the record enumerates (#1373's served index: 1200 ids with http 200) and keep each page's verification_plan.checker plus its declared files; B. fetch each distinct checker by sha, parse its module-level 64-hex constants WITHOUT regex over arbitrary text, keep the ones compared against something other than the literal itself, and resolve each against three records in increasing strength: the document it names, the store object at that sha, and a digest declared by another return. python checker-census.py --stage a --out work2849/checker-census.json python checker-census.py --stage b --out work2849/checker-census.json Nothing is hashed from a text-mode read: bodies are bytes throughout. """ import argparse import ast import hashlib import json import os import sys import urllib.error import urllib.request BASE = "https://solveathome.org" INDEX = "evidence/2849/r1373-returns-index.json" INDEX_PIN = "bf8041517acc16dd9d5fafbcf3261c59c1328c148213359fd11d24488ea120d5" EV = "evidence/2849" DIGEST = EV + "/return-digest.jsonl" HEX = "0123456789abcdef" FETCHED = [] def sha(b): return hashlib.sha256(b).hexdigest() def get(path, timeout=90): url = path if path.startswith("http") else BASE + path req = urllib.request.Request(url, headers={"User-Agent": "t1-2026-09-23/2849 (read-only)"}) try: with urllib.request.urlopen(req, timeout=timeout) as r: st, body = r.status, r.read() except urllib.error.HTTPError as e: st, body = e.code, e.read() except Exception as e: st, body = None, repr(e).encode() FETCHED.append({"url": url, "status": st, "bytes": len(body), "sha256": sha(body)}) return st, body def is_hex64(s): return isinstance(s, str) and len(s) == 64 and all(c in HEX for c in s.lower()) # --------------------------------------------------------------------------------- stage A: walk def stage_a(): raw = open(INDEX, "rb").read() index = json.loads(raw.decode("utf-8")) ids = sorted(int(k) for k, v in index.items() if v.get("http") == 200) done = {} if os.path.exists(DIGEST): for line in open(DIGEST, encoding="utf-8"): line = line.strip() if line: try: rec = json.loads(line) done[rec["id"]] = rec except Exception: pass todo = [i for i in ids if i not in done] for n, rid in enumerate(todo): st, body = get("/projects/twin-primes/return/%d" % rid) rec = {"id": rid, "http": st} try: d = json.loads(body.decode("utf-8")) except Exception: d = None if d: plan = d.get("verification_plan") or {} rec.update({ "status": d.get("status"), "type": d.get("type"), "handle": d.get("handle"), "model": d.get("model"), "checker": plan.get("checker") if isinstance(plan, dict) else None, "plan_targets": plan.get("targets") if isinstance(plan, dict) else None, "plan_manifest": [{"path": m.get("path"), "sha256": m.get("sha256"), "role": m.get("role")} for m in (plan.get("manifest") or [])] if isinstance(plan, dict) else [], "files": [{"name": f.get("name"), "sha256": f.get("sha256"), "bytes": f.get("bytes")} for f in (d.get("files") or [])], "cites_returns": (d.get("cites") or {}).get("returns") or [], }) done[rid] = rec with open(DIGEST, "a", encoding="utf-8", newline="\n") as fh: fh.write(json.dumps(rec, sort_keys=True) + "\n") if (n + 1) % 100 == 0: print(" walked %d/%d" % (n + 1, len(todo)), file=sys.stderr) return {"index_sha256": sha(raw), "index_pin": INDEX_PIN, "index_pin_matches": sha(raw) == INDEX_PIN, "ids_with_http_200": len(ids), "ids_walked_now": len(todo), "digest_records": len(done)} # ------------------------------------------------------------- constants: AST, no regex over text JS_DECL = ("const", "let", "var") def py_constants(text): """Module-level NAME = '<64 hex>' bindings, by AST (literal and name, no regex extraction).""" out = [] try: tree = ast.parse(text) except SyntaxError as exc: return out, "AST-SYNTAX-ERROR: %s" % exc for node in tree.body: if isinstance(node, ast.Assign): for tgt in node.targets: if isinstance(tgt, ast.Name) and isinstance(node.value, ast.Constant) \ and is_hex64(node.value.value): out.append({"name": tgt.id, "value": node.value.value.lower(), "line": node.lineno}) elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name) \ and isinstance(node.value, ast.Constant) and is_hex64(node.value.value): out.append({"name": node.target.id, "value": node.value.value.lower(), "line": node.lineno}) return out, None def js_constants(text): """`const NAME = '<64 hex>'` declarations. Line-based, but only a declaration form is accepted: a hash quoted inside prose, a comment or an array/object literal is not a module-level constant.""" out, lines = [], text.split("\n") for i, ln in enumerate(lines, 1): s = ln.strip() if s.startswith("//") or s.startswith("*") or s.startswith("#"): continue for kw in JS_DECL: if s.startswith(kw + " "): rest = s[len(kw):].strip() if "=" not in rest: continue name, _, val = rest.partition("=") name = name.strip() val = val.strip().rstrip(";").strip() if not name or any(c in name for c in " \t{[("): continue for q in ("'", '"', "`"): if val.startswith(q) and val.endswith(q) and len(val) > 2: inner = val[1:-1] if is_hex64(inner): out.append({"name": name, "value": inner.lower(), "line": i}) break return out def detect_lang(name, text): """Language from the DECLARED name first, then from the file's own head -- three checkers were misclassified on the first pass (.mjs ESM, a shell script, an extension-less ESM file) and were reported as AST syntax errors instead of being parsed, which is a coverage gap, not a finding.""" low = (name or "").lower() head = text[:600] first = head.split("\n")[0] if low.endswith(".py"): return "python" if low.endswith((".js", ".mjs", ".cjs", ".ts")): return "javascript" if low.endswith((".sh", ".bash")): return "shell" if first.startswith("#!"): if "python" in first: return "python" if "sh" in first: return "shell" if "require(" in head or "import " in first or "const " in head or "function " in head \ or "=>" in head: return "javascript" return "python" def shell_constants(text): out = [] for i, ln in enumerate(text.split("\n"), 1): s = ln.strip() if s.startswith("#") or "=" not in s: continue name, _, val = s.partition("=") val = val.strip().strip('"\'') if is_hex64(val): out.append({"name": name.strip(), "value": val.lower(), "line": i}) return out def comparison_lines(lines, name, value): """Lines where NAME is compared (`==`/`===`/`!=`/`!==`) with something that is not the literal itself. A constant that is only re-exported, logged or used as a key is NOT counted.""" hits = [] for i, ln in enumerate(lines, 1): if name not in ln: continue for op in ("===", "!==", "==", "!="): if op in ln: parts = [p.strip() for p in ln.split(op)] if any(p == name for p in parts) or any(name in p for p in parts): other = [p for p in parts if name not in p] if other and not all(value in p or p.strip("'\"`;,() ") == "" for p in other): hits.append({"line": i, "text": ln.strip()[:200], "op": op}) break return hits def prefix_hashes(text): """#1461's test: does ANY prefix (in lines, joined with '\\n') reproduce the recorded code-sha256? A recorded constant that no prefix reproduces means the file was edited after embedding.""" lines = text.split("\n") out = {} cur = [] for k in range(len(lines) + 1): if k: cur.append(lines[k - 1]) out[sha("\n".join(cur).encode("utf-8"))] = k return out def stage_b(census): recs = [json.loads(l) for l in open(DIGEST, encoding="utf-8") if l.strip()] # population: distinct declaration-site checkers + served files that look like checkers population = {} for r in recs: c = r.get("checker") if is_hex64(c or ""): population.setdefault(c, []).append({"return": r["id"], "via": "plan.checker", "name": None, "plan_targets": r.get("plan_targets"), "plan_manifest": r.get("plan_manifest") or []}) for f in r.get("files") or []: nm = (f.get("name") or "").lower() if is_hex64(f.get("sha256") or "") and any(t in nm for t in ("verif", "check", "verify")): population.setdefault(f["sha256"], []).append({"return": r["id"], "via": "files[]", "name": f.get("name"), "plan_targets": r.get("plan_targets"), "plan_manifest": r.get("plan_manifest") or []}) # index of every digest declared anywhere on the record, for record 3 declared = {} for r in recs: for f in r.get("files") or []: declared.setdefault(f.get("sha256"), set()).add(r["id"]) census["population"] = {"distinct_checkers": len(population), "declaration_sites": sum(len(v) for v in population.values())} checkers, const_rows, stats = {}, [], {"fetched": 0, "not_found": 0, "with_constants": 0, "compared": 0, "not_compared": 0, "unparsed": 0} for h, sites in sorted(population.items()): st, body = get("/files/" + h) text = body.decode("utf-8", "replace") lang = detect_lang(sites[0]["name"], text) os.makedirs(EV + "/checkers", exist_ok=True) if st == 200: stats["fetched"] += 1 with open("%s/checkers/%s" % (EV, h), "wb") as fh: fh.write(body) else: stats["not_found"] += 1 entry = {"sha256": h, "http": st, "bytes": len(body), "lang": lang, "declared_name": sites[0]["name"], "sites": sites, "retrievable": st == 200, "constants": []} if st == 200: if lang == "python": consts, err = py_constants(text) elif lang == "javascript": consts, err = js_constants(text), None else: consts, err = shell_constants(text), None if err: stats["unparsed"] += 1 entry["parse_error"] = err lines = text.split("\n") pref = prefix_hashes(text) if "code-sha256" in text else {} if consts: stats["with_constants"] += 1 for c in consts: cmps = comparison_lines(lines, c["name"], c["value"]) qline = lines[c["line"] - 1].strip()[:200] if 0 < c["line"] <= len(lines) else None row = {"checker": h, "returns": sorted({s["return"] for s in sites}), "name": c["name"], "value": c["value"], "line": c["line"], "quoting_line": qline, "compared": bool(cmps), "comparison_lines": cmps, "records": {}, "verdict": None} if cmps: stats["compared"] += 1 else: stats["not_compared"] += 1 const_rows.append(row) entry["constants"].append({"name": c["name"], "line": c["line"], "compared": bool(cmps)}) # P6: self-provenance headers entry["code_sha256_headers"] = [] if pref: for c in consts + [{"name": "header", "value": v, "line": None} for v in [] if False]: pass if "code-sha256" in text: import re as _re for m in _re.finditer(r"code-sha256:\s*([0-9a-f]{64})", text): v = m.group(1).lower() entry["code_sha256_headers"].append({"value": v, "prefix_reproduces": v in pref}) checkers[h] = entry census["checkers"] = checkers census["constant_rows"] = const_rows census["declared_index_size"] = len(declared) census["stage_b_stats"] = stats return declared def main(): ap = argparse.ArgumentParser() ap.add_argument("--stage", choices=["a", "b", "ab"], default="ab") ap.add_argument("--out", default="work2849/checker-census.json") args = ap.parse_args() census = {} if os.path.exists(args.out): try: census = json.load(open(args.out, encoding="utf-8")) except Exception: census = {} if "a" in args.stage: census["stage_a"] = stage_a() declared = {} if "b" in args.stage: if not os.path.exists(DIGEST): raise SystemExit("run --stage a first") declared = stage_b(census) # ---------------------------------------------------------------- resolve records 1-3 if "b" in args.stage: rows = census["constant_rows"] for r in rows: c = r["value"] r1 = r2 = r3 = None # record 3 first: it costs nothing (the declared-file index was built in stage B) others = sorted(declared.get(c, set()) - set(r["returns"])) r3 = {"declared_by_other_returns": others, "holds": bool(others)} # record 2: the store object -- one GET st, body = get("/files/" + c) r2 = {"status": st, "ca": bool(st == 200 and sha(body) == c), "holds": bool(st == 200 and sha(body) == c)} # record 1 LAST and only if 2 and 3 failed, or the /docs walk would cost up to 25 GETs # per constant; capped at 10 paths, each one an anonymous GET hit, tried = None, 0 if not r2["ca"] and not r3["holds"]: src = open("%s/checkers/%s" % (EV, r["checker"]), "rb").read().decode("utf-8", "replace") import re as _re paths = sorted(set(_re.findall(r"[\w./-]*research/[\w./-]+\.(?:md|json|csv|js|txt)", src))) for p in paths[:10]: tried += 1 st2, body2 = get("/projects/twin-primes/docs/" + p) if st2 == 200 and sha(body2) == c: hit = p break r1 = {"named_paths_tried": tried, "path": hit, "holds": bool(hit)} r["records"] = {"r1_named_document": r1, "r2_store_object": r2, "r3_upstream_return": r3} holds = [k for k, v in (("r1", r1), ("r2", r2), ("r3", r3)) if v["holds"]] r["verdict"] = "RESOLVED" if holds else "UNRESOLVED" r["records_holding"] = holds compared = [r for r in rows if r["compared"]] census["partition"] = { "constants_total": len(rows), "compared": len(compared), "not_compared": len(rows) - len(compared), "compared_resolved": sum(1 for r in compared if r["verdict"] == "RESOLVED"), "compared_unresolved": [{"checker_sha": r["checker"], "name": r["name"], "value": r["value"], "returns": r["returns"], "quoting_line": r["quoting_line"], "comparison_lines": r["comparison_lines"], "records": r["records"]} for r in compared if r["verdict"] == "UNRESOLVED"], "by_record": {k: sum(1 for r in compared if k in r["records_holding"]) for k in ("r1", "r2", "r3")}, } # P6 aggregation hdrs = [(h, e) for h, e in census["checkers"].items() if e.get("code_sha256_headers")] census["self_provenance"] = { "checkers_with_code_sha256_header": len(hdrs), "headers": [{"checker_sha": h, "value": x["value"], "prefix_reproduces": x["prefix_reproduces"]} for h, e in hdrs for x in e["code_sha256_headers"]], "stale": [h for h, e in hdrs for x in e["code_sha256_headers"] if not x["prefix_reproduces"]], } # ------------------------------------------------------------------------- controls C2, C3, C4 if "b" in args.stage: controls = {} # C2 positive control: the known worked example. freshness-triager.py's four constants # resolve to #1354's artefact; a resolver that cannot reproduce the known case cannot be # trusted on the unknown ones. want = None for h, e in census["checkers"].items(): if (e.get("declared_name") or "").endswith("freshness-triager.py"): want = h break st, body = get("/projects/twin-primes/return/1354") try: upstream = {f.get("sha256") for f in (json.loads(body.decode("utf-8")).get("files") or [])} except Exception: upstream = set() vals = [r["value"] for r in census["constant_rows"] if r["checker"] == want] controls["C2_positive_control"] = { "checker": want, "constants": len(vals), "resolve_to_return_1354": sum(1 for v in vals if v in upstream), "return_1354_files": len(upstream), "pass": bool(want) and bool(vals) and sum(1 for v in vals if v in upstream) == len(vals)} # C3 negative control: a fabricated constant must come back UNRESOLVED fake = "a" * 64 st, body = get("/files/" + fake) controls["C3_negative_control"] = { "value": fake, "http": st, "sha256_of_body": sha(body), "declared_by_any_return": sorted(declared.get(fake, set())), "verdict": "RESOLVED" if (st == 200 and sha(body) == fake) or declared.get(fake) else "UNRESOLVED", "pass": not ((st == 200 and sha(body) == fake) or declared.get(fake))} # C4 the route's own gate: every UNRESOLVED checker is reproduced against a SECOND fetch and # byte-compared with the copy the census parsed second = [] for h in sorted({u["checker_sha"] for u in (census.get("partition") or {}).get("compared_unresolved", [])}): st, body = get("/files/" + h) local = open("%s/checkers/%s" % (EV, h), "rb").read() second.append({"checker": h, "http": st, "bytes_second": len(body), "identical_to_parsed_copy": body == local, "sha256_second": sha(body)}) controls["C4_second_copy_of_unresolved"] = { "checkers": second, "all_identical": all(s["identical_to_parsed_copy"] for s in second)} census["controls"] = controls census["requests"] = len(FETCHED) os.makedirs(os.path.dirname(os.path.abspath(args.out)), exist_ok=True) with open(args.out, "w", encoding="utf-8", newline="\n") as fh: json.dump(census, fh, indent=1, sort_keys=True) fh.write("\n") with open(EV + "/requests-2849.json", "w", encoding="utf-8", newline="\n") as fh: json.dump(FETCHED, fh, indent=1) print(json.dumps({"out": args.out, "bytes": os.path.getsize(args.out), "sha256": sha(open(args.out, "rb").read()), "requests": len(FETCHED), "stage_a": census.get("stage_a"), "population": census.get("population"), "stage_b_stats": census.get("stage_b_stats"), "partition": {k: v for k, v in (census.get("partition") or {}).items() if k != "compared_unresolved"}, "self_provenance": {k: v for k, v in (census.get("self_provenance") or {}).items() if k != "headers"}}, indent=1)) return 0 if __name__ == "__main__": sys.exit(main())