#!/usr/bin/env python3 """Route 128 triage: is the reversion claim real, and is each flagged diff substantive? Reads only the server (no compute): return #1400's served `reversion-sweep.json` supplies the six flagged documents; for each, `GET .../history/research/` gives the version list, and the CONTENT of the served version and of the distinct revision in between is diffed line by line. A changed line is called *substantive* when it carries ledger/verdict vocabulary (a status, a rung, a verdict word, a proved range), and *filtering* otherwise (link stripping, reflow, source removal). Both controls the route names are re-run: the three documents review #154 named must fire, and a document whose content sha appears once must not. Writes `reversion-check.json` and prints a compact table. Every fetch is saved under `served/`. """ import difflib import hashlib import json import os import re import sys import urllib.error import urllib.request def norm(t): """Whitespace-normalized line, so reflowed paragraphs are not read as content change.""" return re.sub(r"\s+", " ", t).strip() SERVER = "https://solveathome.org" PROJECT = "/projects/twin-primes" HERE = os.path.dirname(os.path.abspath(__file__)) SERVED = os.path.join(HERE, "served") LEDGER = re.compile( r"\b(status|verdict|rung|accepted|rejected|verified|provisional|recorded|open|closed|" r"proved|refuted|withdrawn|superseded|final_rung|author_rung|decision)\b", re.I) CONTROLS = { # review #154's three: must fire "research/QUESTIONS.md", "research/fixed-endpoint-discrepancy.md", "research/global-factor-signs.md"} def token(): p = os.environ.get("SOLVEATHOME_TOKEN_FILE") if not p: sys.exit("SOLVEATHOME_TOKEN_FILE is not set") return open(p, encoding="utf-8").read().strip() TOK = token() def get(path, binary=False): url = path if path.startswith("http") else SERVER + path req = urllib.request.Request(url, method="GET") req.add_header("Authorization", "Bearer " + TOK) req.add_header("Accept", "application/json, text/plain, */*") req.add_header("User-Agent", "sah-triage-128/1.0.0 (deepseek-v4-flash)") try: with urllib.request.urlopen(req, timeout=120) as r: body = r.read() return r.status, (body if binary else body.decode("utf-8", "replace")) except urllib.error.HTTPError as e: return e.code, e.read().decode("utf-8", "replace") except Exception as e: return 0, "ERR " + repr(e)[:200] def save(rel, data): p = os.path.join(SERVED, rel) os.makedirs(os.path.dirname(p), exist_ok=True) mode = "wb" if isinstance(data, bytes) else "w" with open(p, mode, **({} if isinstance(data, bytes) else {"encoding": "utf-8", "newline": "\n"})) as fh: fh.write(data) return p def fetch_content(url_or_path, name): code, txt = get(url_or_path) if code != 200: return None save("content/%s" % name, txt) return txt def main(): os.makedirs(SERVED, exist_ok=True) # 1. the basis: return #1400 and its served sweep artifact code, rec = get(PROJECT + "/return/1400") print("GET return/1400 -> %s" % code) if code != 200: print(rec[:400]) return 1 rec = json.loads(rec) json.dump(rec, open(os.path.join(HERE, "return-1400.json"), "w", encoding="utf-8"), indent=1) shas = [] for f in (rec.get("files") or []): s = (f.get("sha256") or f.get("sha") or f.get("hash")) if isinstance(f, dict) else f if isinstance(s, str) and len(s) == 64: shas.append(s) print("return #1400 files:", len(shas), "status:", rec.get("status"), "outcome:", (rec.get("research") or {}).get("outcome")) sweep = None for sha in shas: c, b = get("/files/" + sha, binary=True) if c != 200: continue txt = b.decode("utf-8", "replace") nm = (rec.get("hashes") or {}).get(sha) or sha[:12] save("1400/%s" % nm, txt) if "reversion" in nm or "reversion" in txt[:400]: try: sweep = json.loads(txt) except Exception: pass if sweep is None: # #1400's hashes map may be sha->name; re-read by name for name in ("reversion-sweep.json",): for sha, nm in (rec.get("hashes") or {}).items(): if nm == name: c, b = get("/files/" + sha, binary=True) if c == 200: sweep = json.loads(b.decode("utf-8", "replace")) if sweep is None: print("!! sweep artifact not found on #1400; aborting") return 2 rows = sweep.get("documents") or sweep.get("rows") or sweep if isinstance(rows, dict): rows = [dict(v, document=k) for k, v in rows.items()] flagged = [r for r in rows if r.get("qualified") or r.get("reverted") or r.get("versions")] docs = [(r.get("document") or r.get("path") or r.get("name")) for r in flagged] docs = [d for d in docs if d] print("sweep: %d rows, %d flagged -> %s" % (len(rows), len(docs), docs)) # 2. controls: a single-version document must not fire single = [r for r in rows if not (r.get("qualified") or r.get("reverted"))] single_doc = (single[0].get("document") or single[0].get("path")) if single else None out = {"basis": 1400, "docs": [], "controls": {}} for doc in docs: name = doc.split("research/")[-1] c, hist = get(PROJECT + "/history/research/" + name) if c != 200: c2, hist = get("/history/research/" + name) if c2 != 200: out["docs"].append({"document": doc, "error": "history %s" % c}) continue h = json.loads(hist) save("history/%s.json" % name, hist) vers = h.get("versions") or h.get("history") or [] def _sha(v): u = v.get("content_url") or v.get("url") or "" return v.get("sha256") or v.get("sha") or (u.rstrip("/").split("/")[-1] if u else None) shas_by_ver = [(_sha(v), v.get("content_url") or v.get("url"), v.get("created_at") or v.get("at")) for v in vers] shas_by_ver = [t for t in shas_by_ver if t[0]] counts = {} for s, u, a in shas_by_ver: counts[s] = counts.get(s, 0) + 1 repeated = [s for s, n in counts.items() if n > 1] served_entry = shas_by_ver[-1] if shas_by_ver else (None, None, None) # newest = served # the revision thrown away = the newest version before the served one whose content differs thrown = None for s, u, a in reversed(shas_by_ver[:-1]): if s != served_entry[0]: thrown = (s, u, a) break served_txt = fetch_content(served_entry[1], name) if served_entry[1] else None thrown_txt = fetch_content(thrown[1], name + ".prev") if thrown and thrown[1] else None if served_txt is not None and hashlib.sha256(served_txt.encode()).hexdigest() != served_entry[0]: rec_out_note = "served content does not hash to its version sha" else: rec_out_note = None if thrown_txt is not None and thrown and \ hashlib.sha256(thrown_txt.encode()).hexdigest() != thrown[0]: rec_out_note = (rec_out_note or "") + "; thrown-away content does not hash to its sha" diff_lines = [] if served_txt and thrown_txt: dl = list(difflib.unified_diff(thrown_txt.splitlines(), served_txt.splitlines(), "verified", "served", n=0, lineterm="")) diff_lines = [l for l in dl if l[:1] in "+-" and not l.startswith(("+++", "---"))] subst = [l for l in diff_lines if LEDGER.search(l)] # The classification the return uses: whitespace-normalized line sets, so a re-wrapped # paragraph is not called a content change (a raw unified diff over-counts it). A = {norm(l) for l in (served_txt or "").splitlines() if norm(l)} B = {norm(l) for l in (thrown_txt or "").splitlines() if norm(l)} only_served, only_thrown = sorted(A - B), sorted(B - A) ledger_changed = [l for l in only_served + only_thrown if LEDGER.search(l)] headings = [l for l in only_served + only_thrown if l.startswith("#")] verdict = ("substantive" if (ledger_changed or headings) else ("reflow/filtering" if (only_served or only_thrown) else "identical")) rec_out = { "normalized_only_served": len(only_served), "normalized_only_thrown": len(only_thrown), "ledger_changed": len(ledger_changed), "headings_changed": len(headings), "verdict": verdict, "sample_ledger_changed": [l[:200] for l in ledger_changed[:4]], "document": doc, "versions": len(shas_by_ver), "repeated_sha": len(repeated) > 0, "changed_lines": len(diff_lines), "substantive_lines": len(subst), "class": ("substantive" if subst else ("filtering" if diff_lines else "identical")), "controls_named_by_review": doc in CONTROLS, "sample_substantive": [l.strip()[:160] for l in subst[:4]], "sample_removed": [l.strip()[:160] for l in diff_lines if l.startswith("-")][:6], "note": rec_out_note, "served_sha": served_entry[0], "thrown_sha": (thrown or (None,))[0], } out["docs"].append(rec_out) print(" %-46s vers=%-3d rep=%-5s raw=%-4d norm=%-3d/%-3d ledger=%-3d -> %-18s%s" % ( name, len(shas_by_ver), len(repeated) > 0, len(diff_lines), len(only_served), len(only_thrown), len(ledger_changed), verdict, " [review#154 control]" if doc in CONTROLS else "")) if single_doc: nm = single_doc.split("research/")[-1] c, hist = get(PROJECT + "/history/research/" + nm) if c == 200: h = json.loads(hist) vers = h.get("versions") or [] shas = [(v.get("sha256") or v.get("sha")) for v in vers] out["controls"]["single_version_doc"] = {"document": nm, "versions": len(shas), "repeated": len(shas) != len(set(shas))} print(" control %-38s versions=%d repeated=%s" % (nm, len(shas), len(shas) != len(set(shas)))) named = [d for d in out["docs"] if d.get("controls_named_by_review")] out["controls"]["review154_all_fire"] = bool(named) and all(d.get("repeated_sha") for d in named) out["summary"] = { "flagged": len(out["docs"]), "substantive": sum(1 for d in out["docs"] if d.get("verdict") == "substantive"), "filtering": sum(1 for d in out["docs"] if d.get("verdict") == "reflow/filtering"), "identical": sum(1 for d in out["docs"] if d.get("verdict") == "identical"), "raw_class_substantive": sum(1 for d in out["docs"] if d.get("class") == "substantive"), "uninspected_in_route": [d["document"] for d in out["docs"] if not d["controls_named_by_review"]], } json.dump(out, open(os.path.join(HERE, "reversion-check.json"), "w", encoding="utf-8"), indent=1) json.dump([{k: d.get(k) for k in ("document", "normalized_only_served", "normalized_only_thrown", "ledger_changed", "headings_changed", "verdict")} for d in out["docs"]], open(os.path.join(HERE, "reversion-check-normalized.json"), "w", encoding="utf-8"), indent=1) print(json.dumps(out["summary"], indent=1)) return 0 if __name__ == "__main__": sys.exit(main())