#!/usr/bin/env python3 """Verify the revision of research/history/staging/attack-kstar-01.md filed with job #2987. Run from a fresh directory; needs no state, no database and no credential (served documents are public). Checks, in order: V1 the served document's bytes hash to the `base` this revision names -- i.e. the text that was actually edited is the text on the server, so the revision is not applied on top of a different document (if it instead hashes to the revision's own `file`, the revision is already the served version: reported as integrated, and no further check is needed); V2 applying apply-revision.py to those exact bytes reproduces the filed `file` sha256, byte for byte; V3 the document invariants the finding asked for hold in the result: the ladder line has 17 entries and ends in 21, the 19#->43# row is in the table, and none of the counts the added row falsifies still says five/16/past-41; V4 with --instrument, the cited instrument (kstar_dfs.py from return #1144) still returns the frozen cells it is validated on: 13 29 -> 10, 13 31 -> 17, 17 31 -> 13; V5 the `s` column the finding asked to check, against the document's own P(2s) convention (P(n) = the largest prime <= n): every ladder row's s must satisfy P(2s) = its target primorial level, including the added row's s = 22, 23 -> 43. Usage: verify-revision.py --apply apply-revision.py --base --file [--path research/history/staging/attack-kstar-01.md] [--instrument ./kstar_dfs.py] [--workdir .] Exit 0 only when every check performed passes. """ import argparse import hashlib import json import os import re import subprocess import sys import urllib.request BASE_URL = "https://solveathome.org/projects/twin-primes" DOC = "research/history/staging/attack-kstar-01.md" FROZEN = [((13, 29), 10), ((13, 31), 17), ((17, 31), 13)] # committed cells (#1144 §2) def fetch(path): req = urllib.request.Request("%s/docs/%s" % (BASE_URL, path.lstrip("/")), method="GET") req.add_header("Accept", "text/markdown, */*") req.add_header("User-Agent", "Buffy-FreebuffDesktop") with urllib.request.urlopen(req, timeout=120) as r: return r.read() def main(): ap = argparse.ArgumentParser() ap.add_argument("--apply", required=True) ap.add_argument("--base", required=True) ap.add_argument("--file", required=True) ap.add_argument("--path", default=DOC) ap.add_argument("--instrument", default=None) ap.add_argument("--workdir", default=".") a = ap.parse_args() sys.stdout.reconfigure(newline="\n") checks, notes = {}, [] served = fetch(a.path) served_sha = hashlib.sha256(served).hexdigest() notes.append({"served_path": a.path, "served_sha256": served_sha, "bytes": len(served)}) if served_sha == a.file: checks["V1_served_is_the_revision"] = True notes.append("the served document already IS this revision: integrated, nothing to apply") print(json.dumps({"ok": True, "already_integrated": True, "checks": checks, "notes": notes}, indent=1, sort_keys=True)) return 0 checks["V1_served_bytes_are_the_base"] = (served_sha == a.base) if not checks["V1_served_bytes_are_the_base"]: print(json.dumps({"ok": False, "checks": checks, "notes": notes, "why": "served bytes are neither this revision nor the base it was " "derived from: the document changed since base"}, indent=1)) return 1 src = os.path.join(a.workdir, "served.base.md") out = os.path.join(a.workdir, "revised.md") with open(src, "wb") as fh: fh.write(served) p = subprocess.run([sys.executable, a.apply, "--in", src, "--out", out], capture_output=True, text=True) checks["V2_apply_exit_0"] = (p.returncode == 0) if p.returncode != 0: print(json.dumps({"ok": False, "checks": checks, "stderr": p.stderr[-2000:]}, indent=1)) return 1 revised = open(out, "rb").read() got = hashlib.sha256(revised).hexdigest() checks["V2_result_is_the_filed_revision"] = (got == a.file) notes.append({"reproduced_sha256": got, "filed_sha256": a.file, "bytes": len(revised)}) text = revised.decode("utf-8") ladder = re.search(r"Full ladder \(K\*\+1, (\d+) steps\): ([0-9 ]+)\.", text) values = ladder.group(2).split() if ladder else [] checks["V3_ladder_17_entries_ending_21"] = bool( ladder and int(ladder.group(1)) == 17 == len(values) and values[-1] == "21") checks["V3_new_row_present"] = ("| 19#→43# | 22, 23 | 20 | 21 | 618/150 = 4.1200 | 5.10 |" in text) checks["V3_no_stale_counts"] = not any(s in text for s in ( "five new steps", "five new C₂", "five new values", "five new certificates are", "16 doubling steps", "P(2s) = 41 is inferred")) rows = re.findall(r"^\| (\d+)#→(\d+)# \| ([^|]+)\| ([^|]+)\| ([^|]+)\| ([^|]+)\| ([^|]+)\| ([^|]+)\|$", text, re.M) def prev_prime(n): for m in range(n, 1, -1): if all(m % d for d in range(2, int(m ** 0.5) + 1)): return m return None s_ok, s_detail = bool(rows), [] for base_p, tgt_p, s_cell in ((r[0], r[1], r[2]) for r in rows): ss = [int(x) for x in re.findall(r"\d+", s_cell)] for s in ss: want = prev_prime(2 * s) ok = (want == int(tgt_p)) s_ok = s_ok and ok s_detail.append({"row": "%s#→%s#" % (base_p, tgt_p), "s": s, "P(2s)": want, "target": int(tgt_p), "consistent": ok}) checks["V5_s_column_matches_P2s_convention"] = s_ok notes.append({"P2s_rows": s_detail}) if a.instrument: for (P, PP), want in FROZEN: r = subprocess.run([sys.executable, a.instrument, str(P), str(PP)], capture_output=True, text=True, cwd=a.workdir) try: got_k = json.loads(r.stdout.strip().splitlines()[-1])["kstar"] except Exception: got_k = None checks["V4_frozen_%d_%d_kstar_%d" % (P, PP, want)] = (got_k == want) notes.append({"frozen": [P, PP], "kstar": got_k, "expected": want}) ok = all(checks.values()) print(json.dumps({"ok": ok, "checks": checks, "notes": notes}, indent=1, sort_keys=True)) return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())