#!/usr/bin/env python3 """Reproduce job #3754's revision of research/QUESTIONS.md from the server, end to end. python regenerate.py verify the existing mirror against corpus-manifest.txt, run the corpus's own generator, check the output sha python regenerate.py --fresh refetch every manifest path from the server first The manifest is the contract: every input file is fetched by name and checked against the sha256 recorded when the revision was produced, so the run is reproducible from the served corpus alone. The generator is the corpus's own `research/gen-questions-index.js`, unmodified: this script fetches and runs it, it does not reimplement it. """ import argparse import concurrent.futures as cf import hashlib import io import json import os import subprocess import sys import urllib.error import urllib.request sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace") SERVER = "https://solveathome.org" HERE = os.environ.get("SAH_JOB_DIR") or os.path.dirname(os.path.abspath(__file__)) MANIFEST = os.path.join(HERE, "corpus-manifest.txt") # The mirror lives in ./corpus beside this script; SAH_CORPUS overrides it when the scripts are # served next to their evidence and the mirror is kept elsewhere. MIRROR = os.environ.get("SAH_CORPUS") or os.path.join(HERE, "corpus") EXPECTED = "48703f4ddb64ba1c6f94282536d686a5b4aa98d1eb947505f24b0823b65adf65" EXPECTED_BYTES = 616860 BASE_SHA = "d47cc818ba743a989de21c32f99fa948c530f9320d25426c57a0438e50424b29" def credential(): path = os.path.join(os.environ.get("LOCALAPPDATA", ""), "solveathome", "credentials", "twin-primes.token") with open(path, encoding="utf-8") as fh: return fh.read().strip() TOKEN = credential() def fetch(rel): req = urllib.request.Request(SERVER + "/projects/twin-primes/docs/" + rel, method="GET") req.add_header("Authorization", "Bearer " + TOKEN) req.add_header("Accept", "application/json") req.add_header("X-Model", "deepseek/deepseek-v4-flash") req.add_header("X-Effort", "max") req.add_header("User-Agent", "Buffy-FreebuffDesktop") try: with urllib.request.urlopen(req, timeout=120) as r: return r.status, r.read() except urllib.error.HTTPError as e: return e.code, e.read() def sha256(data): return hashlib.sha256(data).hexdigest() def main(): ap = argparse.ArgumentParser() ap.add_argument("--fresh", action="store_true") a = ap.parse_args() manifest = {} for line in open(MANIFEST, encoding="utf-8"): line = line.rstrip("\n") if not line: continue sha, rel = line.split(" ", 1) manifest[rel] = sha print("manifest: %d files" % len(manifest)) if a.fresh: def one(rel): code, data = fetch(rel) return rel, code, sha256(data) if code == 200 else "" ok = bad = 0 with cf.ThreadPoolExecutor(max_workers=8) as ex: for rel, code, got in ex.map(one, sorted(manifest)): if code != 200 or got != manifest[rel]: bad += 1 print(" MISMATCH %s http=%s expected=%s got=%s" % (rel, code, manifest[rel][:16], got[:16])) else: ok += 1 print("fresh fetch: %d verified, %d mismatched" % (ok, bad)) if bad: return 2 else: bad = 0 for rel, sha in sorted(manifest.items()): p = os.path.join(MIRROR, *rel.split("/")) got = sha256(open(p, "rb").read()) if os.path.isfile(p) else "" if got != sha: bad += 1 print(" MISMATCH %s" % rel) print("mirror verified against the manifest: %d files, %d mismatched" % (len(manifest), bad)) if bad: return 2 p = subprocess.run(["node", "research/gen-questions-index.js"], cwd=MIRROR, capture_output=True, encoding="utf-8", errors="replace") print("generator exit %d: %s" % (p.returncode, (p.stdout + p.stderr).strip()[:300])) if p.returncode: return 3 out = os.path.join(MIRROR, "research", "QUESTIONS.md") data = open(out, "rb").read() got = sha256(data) print(json.dumps({ "generated": os.path.relpath(out, MIRROR).replace(os.sep, "/"), "bytes": len(data), "sha256": got, "expected_sha256": EXPECTED, "expected_bytes": EXPECTED_BYTES, "matches_revision": got == EXPECTED and len(data) == EXPECTED_BYTES, "base_shipped_by_server": BASE_SHA, "base_differs_from_revision": got != BASE_SHA, }, indent=1)) return 0 if got == EXPECTED else 4 if __name__ == "__main__": raise SystemExit(main())