#!/usr/bin/env python3 """bundle-evidence.py -- the raw bytes behind this return, in one file, for an independent checker. `reversion-audit.py` reads the serving layer live (1,211 `/history` reads). Its classification is a function of exactly these bytes for the 12 documents that carry a version record: each document's `/history` answer, its served `/docs` bytes, and every version blob. This script pulls those 18+ blob texts OUT OF THE AUDIT'S OWN CACHE -- offline, no network, no credential -- and writes them into `reversion-evidence.json`, so a reader who does not have (or does not want to use) a token can still re-derive every count in the return from the raw material. The blobs the audit never needed (the non-newest revisions of documents that were not classified as reverts) are not in the cache; `--fill` fetches exactly those, once, and caches them like every other read. The default is offline, so a re-run writes the same bundle with no network at all. python3 bundle-evidence.py [--fill] # writes job2962/reversion-evidence.json """ import hashlib import io import json import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import sahget # noqa: E402 BASE = "https://solveathome.org/projects/twin-primes" def main(): audit = json.load(io.open(os.path.join(HERE, "served-reversion-audit.json"), encoding="utf-8")) paths = [r["path"] for r in audit["reverts"]] for group in (audit.get("non_revert_version_tracked") or {}).values(): for r in group: paths.append(r["path"]) paths = sorted(set(paths)) fill = "--fill" in sys.argv g = sahget.Get(os.path.dirname(HERE)) docs = [] for p in paths: code, data, _ = g.get("%s/history/%s" % (BASE, p), offline=not fill) if code != 200: raise SystemExit("history %s -> HTTP %s" % (p, code)) h = json.loads(data) vs = h.get("versions") or [] scode, sdata, _ = g.get("%s/docs/%s" % (BASE, p), offline=not fill) if scode != 200: raise SystemExit("docs %s -> HTTP %s" % (p, scode)) served_sha = hashlib.sha256(sdata).hexdigest() blobs = {} for v in vs: sha = v.get("content_sha") if not sha or sha in blobs: continue bcode, bdata, _ = g.get("https://solveathome.org/files/%s" % sha, offline=not fill) if bcode != 200: raise SystemExit("blob %s (%s) -> HTTP %s" % (p, sha[:12], bcode)) blobs[sha] = bdata.decode("utf-8", "replace") docs.append({ "path": p, "versions": [{"version": v.get("version"), "content_sha": v.get("content_sha"), "return_id": v.get("return_id"), "verified_by": v.get("verified_by") or [], "summary": (v.get("summary") or "")[:200], "created_at": v.get("created_at")} for v in vs], "served_sha256": served_sha, "served_equals_newest_version": bool(vs) and served_sha == vs[-1].get("content_sha"), "blobs": blobs, }) inv = audit.get("inventory") or {} n_files = inv.get("files") if isinstance(inv.get("files"), int) else None out = { "schema": "reversion-evidence/1", "endpoint": BASE, "source": ("the audit cache of this run: each `/history/` answer, the served `/docs` " "bytes, and every version blob, for the documents that carry a version record"), "tracked_document_count": len(docs), "untracked_document_count": (n_files - len(docs)) if n_files else None, "walked_file_count": n_files, "documents": docs, } dest = os.path.join(HERE, "reversion-evidence.json") with io.open(dest, "w", encoding="utf-8", newline="\n") as fh: json.dump(out, fh, indent=1, sort_keys=True) fh.write("\n") print(json.dumps({"wrote": dest, "bytes": os.path.getsize(dest), "documents": len(docs), "blobs": sum(len(d["blobs"]) for d in docs), "network_calls": g.calls}, indent=1)) return 0 if __name__ == "__main__": sys.exit(main())