#!/usr/bin/env python3 """Regenerate research/QUESTIONS.md from the corpus **as served now** (job #3758's live clause). The previous run's job #3754 shipped this revision at corpus state A (sha 48703f4d...). Four of the 680 manifest inputs have changed on the server since. This script refetches every manifest path, records the served shas in corpus-manifest-now.txt, refreshes the mirror for the paths that moved, and runs the corpus's own generator unmodified, printing the sha of the file it produces. python regen-now.py refetch 680 paths, refresh mirror, run node, print sha python regen-now.py --verify no fetch: mirror must match corpus-manifest-now.txt, then run node Nothing outside this directory is written; the generator is never modified. """ 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.path.dirname(os.path.abspath(__file__)) MIRROR = os.path.join(HERE, "corpus") OLD = os.path.join(HERE, "corpus-manifest.txt") if not os.path.isfile(OLD): OLD = os.path.join(HERE, "corpus-manifest-stateA.txt") # served under this name here NOW = os.path.join(HERE, "corpus-manifest-now.txt") SERVED_INDEX = os.path.join(HERE, "QUESTIONS.served.md") # sha d47cc818..., fetched from /docs OLD_REVISION = "48703f4ddb64ba1c6f94282536d686a5b4aa98d1eb947505f24b0823b65adf65" # job #3754 / return #1741 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 read_manifest(path): out = {} with open(path, encoding="utf-8") as fh: for line in fh: line = line.rstrip("\n") if line: sha, rel = line.split(" ", 1) out[rel] = sha return out def main(): verify_only = "--verify" in sys.argv[1:] old = read_manifest(OLD) print("old manifest (corpus state A, job #3754): %d files" % len(old)) if verify_only: now = read_manifest(NOW) bad = 0 for rel, sha in sorted(now.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 corpus-manifest-now.txt: %d files, %d mismatched" % (len(now), bad)) if bad: return 2 else: def one(rel): code, data = fetch(rel) return rel, code, (sha256(data) if code == 200 else ""), data moved = [] ok = bad = 0 rows = {} with cf.ThreadPoolExecutor(max_workers=8) as ex: for rel, code, got, data in ex.map(one, sorted(old)): if code != 200: bad += 1 print(" FETCH FAILED %s http=%s" % (rel, code)) continue rows[rel] = got ok += 1 if got != old[rel]: moved.append((rel, old[rel], got)) # every fetched file is written, so a reader who starts from this directory alone # builds the mirror the generator needs (the changed ones are reported above) dst = os.path.join(MIRROR, *rel.split("/")) os.makedirs(os.path.dirname(dst), exist_ok=True) with open(dst, "wb") as fh: fh.write(data) print("refetch as served now: %d verified against the server, %d unreadable, %d changed " "since corpus state A" % (ok, bad, len(moved))) for rel, was, now_sha in moved: print(" CHANGED %s %s -> %s" % (rel, was[:16], now_sha[:16])) if bad: return 2 with open(NOW, "w", encoding="utf-8", newline="\n") as fh: for rel in sorted(rows): fh.write("%s %s\n" % (rows[rel], rel)) print("wrote corpus-manifest-now.txt (%d lines)" % len(rows)) 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()[:400])) if p.returncode: return 3 out = os.path.join(MIRROR, "research", "QUESTIONS.md") data = open(out, "rb").read() got = sha256(data) served = open(SERVED_INDEX, "rb").read() print(json.dumps({ "generated": "research/QUESTIONS.md", "bytes": len(data), "sha256": got, "served_index_sha256": sha256(served), "served_index_bytes": len(served), "previous_revision_sha256": OLD_REVISION, "differs_from_served_index": got != sha256(served), "differs_from_previous_revision": got != OLD_REVISION, }, indent=1)) return 0 if __name__ == "__main__": raise SystemExit(main())