#!/usr/bin/env python3 """Job #4489 (route 141 step check): premise checks ONLY, no step work. The step under review (set by #1822) asks: (a) map the 17 route-114 queue rows #1453 SKIPPED to served corpus paths by content (preimage matching of each hunk's context/'-' lines), then per-path `git apply --check --include` against the matched path and its /history versions; (b) for the 4 MOVED rows, `git merge-file current ` and label clean-merge / conflict hunks. This script does NOT map any preimage and does NOT merge anything. It reads the population from #1453's own served artifact (`apply-check.json`, fetched by sha from the host root) and checks the step's *premises*: 1. the population itself: which rows are the 17 skipped ones; 2. whether any skipped row's named target is a served corpus path NOW (if one became served, the mapping question changes); 3. whether the four moved rows' version counts have advanced since #1822's reading, since the step is stated against "current bytes". Controls, so that a 404 is a fact about the URL and not about the instrument (the defect #1453 shipped and #1822 caught): a path known to be served must answer 200, and an invented path must answer 404. If either control fails the probe result is void and is reported as void. Anonymous, read-only, public endpoints only. Writes premise-check.json. """ from __future__ import annotations import glob import json import os import urllib.error import urllib.request HERE = os.path.dirname(os.path.abspath(__file__)) SRC = os.path.normpath(os.path.join(HERE, "..", "..", "sources")) BASE = "https://solveathome.org/projects/twin-primes" POP = glob.glob(os.path.join(SRC, "r141-artifacts", "apply-check.json.d", "*.txt"))[0] LIST = glob.glob(os.path.join(SRC, "r141-artifacts", "served-listing.json.d", "*.txt"))[0] MAPPED_ROWS = [12, 30, 97, 132] # the step's four "moved rows" FITTING = {12: "v1", 30: "v1", 132: "v1-v7", 97: "v1, v3, v6-v8"} def get(path): """Return (status, body_text). Never raises on HTTP status.""" req = urllib.request.Request(BASE + path, headers={"User-Agent": "sah-stepcheck/1.0"}) try: with urllib.request.urlopen(req, timeout=30) as r: return r.status, r.read().decode("utf-8", "replace") except urllib.error.HTTPError as e: return e.code, "" except Exception as e: # transport failure return 0, "TRANSPORT: %s" % e pop = json.load(open(POP, encoding="utf-8")) listing = json.load(open(LIST, encoding="utf-8")) paths_at_snapshot = set(listing["paths"]) rows = pop["rows"] skipped = [r for r in rows if r.get("applies") is None] served_rows = [r for r in rows if r.get("applies") is not None] out = { "tool": "premise-check/1.0", "job": 4489, "route": 141, "population": pop.get("population"), "counts": {"rows": len(rows), "skipped": len(skipped), "served_path_rows": len(served_rows), "declared_n_skipped": pop.get("n_skipped")}, "snapshot_listing": {"at": listing["at"], "n": listing["n"], "sha256": listing["sha256"]}, "controls": {}, "skipped_targets": [], "moved_rows_history": [], } # --- controls ------------------------------------------------------------- c_ok_status, _ = get("/docs/README.md") c_bad_status, _ = get("/docs/research/zzz-no-such-path-4489.md") out["controls"] = { "positive_known_served": {"path": "/docs/README.md", "status": c_ok_status, "expected": 200, "holds": c_ok_status == 200}, "negative_invented": {"path": "/docs/research/zzz-no-such-path-4489.md", "status": c_bad_status, "expected": 404, "holds": c_bad_status == 404}, } controls_hold = out["controls"]["positive_known_served"]["holds"] and \ out["controls"]["negative_invented"]["holds"] out["controls_hold"] = controls_hold # --- 1/2. the 17 skipped rows and their targets --------------------------- for r in skipped: p = r["path"] in_snapshot = p in paths_at_snapshot status, _ = get("/docs/" + p) out["skipped_targets"].append({ "return": r["return"], "path": p, "patch_chars": r.get("patch_chars"), "patch_kind": r.get("patch_kind"), "in_1453_snapshot_listing": in_snapshot, "docs_status_now": status, "served_now": status == 200, "verdict_void_without_controls": not controls_hold, }) served_now = [t for t in out["skipped_targets"] if t["served_now"]] out["skipped_named_target_served_now"] = len(served_now) out["skipped_named_target_in_snapshot_listing"] = sum( 1 for t in out["skipped_targets"] if t["in_1453_snapshot_listing"]) # --- 3. the four moved rows: has the baseline moved again? ---------------- for rid in MAPPED_ROWS: rr = [r for r in served_rows if r["return"] == rid] p = rr[0]["path"] if rr else None if p is None: out["moved_rows_history"].append({"return": rid, "path": None, "note": "no served-path row with this id"}) continue status, body = get("/history/" + p) # GOTCHA, recorded because it nearly produced a false finding: this endpoint # returns BOTH `publications` and `versions`, and they are not the same list. # The version numbering a patch's fitting base is quoted against (#1822's # "v1-v7", "v6-v8") is `versions`. Reading `publications` instead reports a # shrinking history of 3-4 entries and would have said the baseline moved. versions = pubs = None if status == 200 and body: try: j = json.loads(body) versions = j.get("versions") pubs = j.get("publications") except Exception: versions = pubs = None out["moved_rows_history"].append({ "return": rid, "path": p, "history_status": status, "versions_count_now": len(versions) if versions is not None else None, "publications_count_now": len(pubs) if pubs is not None else None, "latest_version_index": len(versions) if versions else None, "latest_version_id": (versions[-1].get("id") if versions else None), "latest_version_prepared_at": (versions[-1].get("prepared_at") if versions else None), "n_versions_at_2026_09_26_by_1822": { 12: "at least 2 (moved by #1785 to v2)", 30: "at least 2 (moved by #1093 to v2)", 132: "at least 8 (moved by #1764 to v8)", 97: "at least 9 (conflicts named at v5 and v9)"}[rid], "fitting_version_named_by_1453_1822": FITTING[rid], }) with open(os.path.join(HERE, "premise-check.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump(out, fh, indent=1, ensure_ascii=False, sort_keys=True) fh.write("\n") print("controls:", json.dumps(out["controls"])) print("controls hold:", controls_hold) print("rows %d = served-path %d + skipped %d (declared n_skipped %s)" % (len(rows), len(served_rows), len(skipped), pop.get("n_skipped"))) print("skipped rows whose named target is in #1453's snapshot listing: %d" % out["skipped_named_target_in_snapshot_listing"]) print("skipped rows whose named target answers 200 under /docs NOW: %d" % len(served_now)) for t in out["skipped_targets"]: print(" ret %-4s %-44s snapshot=%-5s now=%s" % (t["return"], t["path"][:44], t["in_1453_snapshot_listing"], t["docs_status_now"])) print("four moved rows (history now; `versions` is the version list, `publications` is not):") for m in out["moved_rows_history"]: print(" ret %-4s %-40s versions=%-4s publications=%-4s latest=%s (%s) | fitting: %s | #1822 saw: %s" % (m["return"], (m["path"] or "")[:40], m["versions_count_now"], m["publications_count_now"], m["latest_version_id"], m["latest_version_prepared_at"], m["fitting_version_named_by_1453_1822"], m["n_versions_at_2026_09_26_by_1822"]))