#!/usr/bin/env python3 """verify-4245.py -- job #4245, route 82 step check: is the T31 lag-spectrum step still open? Standard library only, no network, deterministic. Reads, by basename in the current directory: step-4245.json the issued step, extracted from the assignment brief (one JSON object) research-4245.json the research block this return files (its next_step must be that object) route82.json /projects/twin-primes/research-routes/82 via this run's api-get.py wrapper r1394.json r1355.json r1296.json r1026.json r1025.json /return/ through the same wrapper t31.json t31.diag.json t29.json t29.diag.json timing-probe.json analysis-1355.txt (served by #1355) t31.log spectrum.log analysis-1296.txt (served by #1296) Claims C1..C7 and three controls; exit 0 only when every claim is ok and every control is detected. Nothing here runs the step, re-derives a T31 number, or predicts the repayment at T31. """ from __future__ import annotations import hashlib import json import re import sys EXPECT_STEP_SHA = "751622af32ee8956b8ce41b1773d698db6f9931895e83f7b6ad6b55552776766" RAW = ["t31.json", "t31.diag.json", "t29.json", "t29.diag.json", "timing-probe.json", "analysis-1355.txt", "t31.log", "spectrum.log", "analysis-1296.txt"] # local file name -> (return wrapper, the name that return declares for the bytes) DECLARED_BY = {"t31.json": ("r1355", "t31.json"), "t31.diag.json": ("r1355", "t31.diag.json"), "t29.json": ("r1355", "t29.json"), "t29.diag.json": ("r1355", "t29.diag.json"), "timing-probe.json": ("r1355", "timing-probe.json"), "analysis-1355.txt": ("r1355", "analysis.txt"), "t31.log": ("r1296", "t31.log"), "spectrum.log": ("r1296", "spectrum.log"), "analysis-1296.txt": ("r1296", "analysis.txt")} LAG2_PAT = re.compile(r"lambda2|lambda_2|lag[-_ ]?2\b|repay|\bk\s*=\s*2\b", re.I) def canon(obj) -> bytes: return json.dumps(obj, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode() def sha_bytes(b: bytes) -> str: return hashlib.sha256(b).hexdigest() def read_bytes(name: str) -> bytes: with open(name, "rb") as fh: return fh.read() def unwrap(name: str): """api-get.py writes {'code','path','body'} with body the server's JSON as a string.""" return json.loads(json.loads(read_bytes(name))["body"]) def read_text(name: str) -> str: return read_bytes(name).decode("utf-8", errors="replace") def parse_spectrum(text: str): """spectrum.log is a JSON array followed by runner lines; decode the array by prefix.""" return json.loads(text.split("# T23")[0]) def claim_steps(step, research, route): sha = sha_bytes(canon(step)) ok = sha == EXPECT_STEP_SHA ok = ok and sha_bytes(canon(research["next_step"])) == EXPECT_STEP_SHA ok = ok and sha_bytes(canon(route["next_step"])) == EXPECT_STEP_SHA ok = ok and research["route_id"] == 82 and route["id"] == "82" return ok, sha def claim_provenance(returns): bad = [] for name in RAW: wrapper, declared_name = DECLARED_BY[name] decl = {f["name"]: f["sha256"] for f in returns[wrapper]["files"]} if sha_bytes(read_bytes(name)) != decl.get(declared_name): bad.append(name) return not bad, bad def fold_map(folds): return {f["p"]: (f["m"], f["K2"], f["lambda"]) for f in folds} def claim_anchors(spectrum, t31, t31diag, t31log): e31 = [x for x in spectrum if x["x"] == 31][0] f31 = fold_map(e31["folds"])[31] log_m = re.search(r"x = 31\s+D = (\d+) \(expected (\d+)\)\s+max gap = (\d+)", t31log) checks = { "D_json": e31["D"] == t31["D"] == t31diag["D"] == 6226553025, "D_log": bool(log_m) and log_m.group(1) == log_m.group(2) == "6226553025", "maxgap": e31["maxgap"] == t31["maxgap"] == t31diag["maxgap"] == 348, "K2_p31": f31[1] == 2647568 and f31[0] == 269774040, "lambda_p31": abs(f31[2] - 0.22651371568724044) < 1e-15, "repeat_ratio": (e31["repeat"]["R"] == 469311107 and abs(e31["repeat"]["E_repeat"] - 730672506.399613) < 1e-6 and abs(e31["repeat"]["ratio"] - 0.6423002136929024) < 1e-15), "dict_agreement": fold_map(t31["folds"]) == fold_map(t31diag["folds"]), } detail = {"spectrum_wall_s": e31["wall_s"], "t31_json_wall_s": t31["wall_s"], "t31_diag_wall_s": t31diag["wall_s"], "workers_diag": t31diag.get("workers"), "chunk_diag": t31diag.get("chunk"), "checks": checks} return all(checks.values()), detail def claim_two_runs(spectrum, t31): e31 = [x for x in spectrum if x["x"] == 31][0] a, b = fold_map(e31["folds"]), fold_map(t31["folds"]) common = sorted(set(a) & set(b)) bad = [p for p in common if a[p][0] != b[p][0] or a[p][1] != b[p][1] or abs(a[p][2] - b[p][2]) > 1e-9] return (not bad and len(common) >= 30), {"common_folds": len(common), "mismatched": bad} def claim_s3(spectrum): by = {x["x"]: fold_map(x["folds"]) for x in spectrum} r29 = by[31][29][2] / by[29][29][2] r31 = by[31][31][2] / by[29][31][2] ok = (1.0 < r29 < 1.683 and 1.0 < r31 < 1.527 and by[31][29][2] > by[23][29][2] and by[31][31][2] > by[23][31][2]) return ok, {"ratio_T31_over_T29_p29": round(r29, 6), "ratio_T31_over_T29_p31": round(r31, 6), "T23_29": by[23][29][2], "T29_29": by[29][29][2], "T31_29": by[31][29][2], "T23_31": by[23][31][2], "T29_31": by[29][31][2], "T31_31": by[31][31][2], "bounds_from_step": [1.683, 1.527]} def lag2_hits(files_text): return [{"file": n, "line": i, "text": ln.strip()[:90]} for n, t in files_text.items() for i, ln in enumerate(t.splitlines(), 1) if LAG2_PAT.search(ln)] def claim_negative(files_text, returns): hits = lag2_hits(files_text) names = sorted({f["name"] for r in returns.values() for f in (r.get("files") or [])}) probes = ("lagspectrum", "closure", "lag-") named = [n for n in names if any(p in n.lower() for p in probes)] r1394_files = returns["r1394"].get("files") or [] return (not hits and not named and len(r1394_files) == 0), { "lag2_pattern_hits": hits, "served_names": len(names), "instrument_like_names": named, "r1394_file_count": len(r1394_files)} def claim_route(route): ev = sorted(int(e["return_id"]) for e in route["events"]) jobs = {j["id"]: j["status"] for j in route["jobs"]} ok = (route["state"] == "active" and route["revision"] == 5 and route["last_return_id"] == "1394" and ev == [1025, 1026, 1296, 1355, 1394] and jobs.get("2783") == "expired" and jobs.get("2736") == "returned" and jobs.get("4245") == "assigned") return ok, {"state": route["state"], "revision": route["revision"], "last_return_id": route["last_return_id"], "events": ev, "jobs": jobs} def main() -> int: sys.stdout.reconfigure(newline="\n", encoding="utf-8") step = json.loads(read_text("step-4245.json")) research = json.loads(read_text("research-4245.json")) route = unwrap("route82.json") returns = {n: unwrap(n + ".json") for n in ("r1394", "r1355", "r1296", "r1026", "r1025")} files_text = {n: read_text(n) for n in RAW} c1, step_sha = claim_steps(step, research, route) c2, c2_bad = claim_provenance(returns) spectrum = parse_spectrum(files_text["spectrum.log"]) c3, c3_detail = claim_anchors(spectrum, json.loads(files_text["t31.json"]), json.loads(files_text["t31.diag.json"]), files_text["t31.log"]) c4, c4_detail = claim_two_runs(spectrum, json.loads(files_text["t31.json"])) c5, c5_detail = claim_s3(spectrum) c6, c6_detail = claim_negative(files_text, returns) c7, c7_detail = claim_route(route) # controls: each mutation must be detected by the predicate it targets mut = json.loads(json.dumps(step)) mut["success"] = mut["success"].replace("0.64230", "0.64231") ctl_step = sha_bytes(canon(mut)) != EXPECT_STEP_SHA and sha_bytes(canon(step)) == EXPECT_STEP_SHA sp_mut = json.loads(json.dumps(spectrum)) e = [x for x in sp_mut if x["x"] == 31][0] for f in e["folds"]: if f["p"] == 31: f["lambda"] += 1e-6 ctl_lambda = (not claim_anchors(sp_mut, json.loads(files_text["t31.json"]), json.loads(files_text["t31.diag.json"]), files_text["t31.log"])[0] and claim_anchors(spectrum, json.loads(files_text["t31.json"]), json.loads(files_text["t31.diag.json"]), files_text["t31.log"])[0]) ft_mut = dict(files_text) ft_mut["spectrum.log"] = files_text["spectrum.log"] + "\n# Lambda2(T31,29) = 1.30 (invented)\n" ctl_scan = bool(lag2_hits(ft_mut)) and not lag2_hits(files_text) claims = { "C1": {"ok": bool(c1), "detail": {"step_canon_sha256": step_sha, "expected": EXPECT_STEP_SHA, "equal_in_brief_route_and_research": bool(c1)}}, "C2": {"ok": bool(c2), "detail": {"raw_files": len(RAW), "mismatched": c2_bad}}, "C3": {"ok": bool(c3), "detail": c3_detail}, "C4": {"ok": bool(c4), "detail": c4_detail}, "C5": {"ok": bool(c5), "detail": c5_detail}, "C6": {"ok": bool(c6), "detail": c6_detail}, "C7": {"ok": bool(c7), "detail": c7_detail}, } controls = {"mutated_step_0.64230_to_0.64231_detected": bool(ctl_step), "mutated_lambda_1e-6_detected": bool(ctl_lambda), "injected_lag2_line_detected": bool(ctl_scan)} all_ok = all(v["ok"] for v in claims.values()) and all(controls.values()) out = {"tool": "verify-4245/1", "all_ok": all_ok, "claims": claims, "controls": controls} sys.stdout.write(json.dumps(out, sort_keys=True, indent=1, ensure_ascii=False) + "\n") return 0 if all_ok else 1 if __name__ == "__main__": sys.exit(main())