#!/usr/bin/env python3 """Rebuild the post-setter comparison window from the served corpus and PROVE its completeness. Reads every fetched `served/return-.json`. Completeness evidence, all recorded in `probe.json`: * the probed id range, the count present, the count absent (404), and that they sum to the range size; * the observed head (highest id that answers) and the run of 404s above it; * `created_at` monotone over the present ids in the range (a served record cannot be created out of order); * the every-id status table, so a reader can re-check any single id. Then the step's comparison set: returns recorded strictly after the step setter #1987, with the route-128 rows, the routeless rows, and the rows carrying the step's own vocabulary. Stdlib only; writes `probe.json` and `scan.json`. Exit 1 if the completeness proof fails. """ import json import os import re import sys HERE = os.path.dirname(os.path.abspath(__file__)) SERVED = os.path.join(HERE, "served") SETTER = 1987 RANGE = (1988, 2110) ROUTE = "128" # the step's own vocabulary, one list per clause of the step text VOCAB = { "registry": ["QUESTIONS.md", "a3e07372", "cda1e669", "ccf2cf0f", "restore-83-153", "regen3289", "gen-questions-index"], "restore": ["fold-arithmetic-bridge", "derive-0904-L7-transfer", "prop-staircase-note", "corner-correlation", "centered-discrepancy-estimate", "global-factor-signs", "mirror cut", "mirror-cut", "reversion", "restore"], "supersession": ["1954", "supersession", "supersede", "v4", "3b83a562"], } def load(rid): p = os.path.join(SERVED, "return-%d.json" % rid) if not os.path.exists(p): return None r = json.load(open(p, encoding="utf-8")) return r.get("body", r) def text_of(rec): parts = [] for k in ("title", "report_md", "evidence_md"): v = rec.get(k) if isinstance(v, str): parts.append(v) res = rec.get("research") if isinstance(res, dict): for k, v in res.items(): if isinstance(v, str): parts.append(v) elif isinstance(v, list): parts.extend(str(x) for x in v) return "\n".join(parts) def main(): ids = {} for rid in range(RANGE[0], RANGE[1] + 1): r = load(rid) ids[rid] = None if r is None else { "created_at": r.get("created_at"), "route": r.get("research_route_id"), "type": r.get("type"), "status": r.get("status"), "model": r.get("author_model") or r.get("model"), "title": (r.get("title") or "")[:90]} present = sorted(i for i, v in ids.items() if v) absent = sorted(i for i, v in ids.items() if not v) # head above the range head_probe = [] for rid in range(RANGE[1] + 1, RANGE[1] + 41): r = load(rid) head_probe.append({"id": rid, "present": bool(r)}) if r: present.append(rid) ids[rid] = {"created_at": r.get("created_at"), "route": r.get("research_route_id"), "type": r.get("type"), "status": r.get("status"), "model": r.get("author_model") or r.get("model"), "title": (r.get("title") or "")[:90]} stamp = [(i, ids[i]["created_at"]) for i in present if ids[i].get("created_at")] mono = all(stamp[k][1] <= stamp[k + 1][1] for k in range(len(stamp) - 1)) head = max(present) above = [p for p in head_probe if p["id"] > head] proof = { "id_range": list(RANGE), "range_size": RANGE[1] - RANGE[0] + 1, "present_in_range": len([i for i in present if RANGE[0] <= i <= RANGE[1]]), "absent_in_range": len(absent), "present_plus_absent_equals_range": len([i for i in present if RANGE[0] <= i <= RANGE[1]]) + len(absent) == RANGE[1] - RANGE[0] + 1, "absent_ids": absent, "observed_head": head, "head_probe": head_probe, "above_head_absent_count": sum(1 for p in above if not p["present"]), "above_head_present": [p["id"] for p in above if p["present"]], "created_at_monotone": mono, "id_status": {str(k): v for k, v in sorted(ids.items())}, } # also verify outside the range: the named returns and route 128's own history named = {} for rid in (83, 13, 101, 152, 153, 1400, 1402, 1556, 1567, 1630, 1828, 1876, 1954, 1987, 2051, 2053): r = load(rid) named[rid] = None if r is None else {"created_at": r.get("created_at"), "route": r.get("research_route_id"), "type": r.get("type"), "status": r.get("status")} proof["named_returns"] = {str(k): v for k, v in named.items()} with open(os.path.join(HERE, "probe.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump(proof, fh, indent=1, ensure_ascii=False) # ---- comparison set: returns recorded after #1987 setter = load(SETTER) rows = [] for rid in sorted(present): if rid <= SETTER: continue r = load(rid) if not r: continue t = text_of(r) hits = {fam: [w for w in words if w.lower() in t.lower()] for fam, words in VOCAB.items()} rows.append({"id": rid, "route": r.get("research_route_id"), "created_at": r.get("created_at"), "type": r.get("type"), "status": r.get("status"), "outcome": (r.get("research") or {}).get("outcome") if isinstance(r.get("research"), dict) else None, "routeless": r.get("research_route_id") in (None, "", 0), "hits": {k: v for k, v in hits.items() if v}, "title": (r.get("title") or "")[:110]}) scan = { "setter": SETTER, "setter_created_at": setter.get("created_at"), "window_rows": len(rows), "routeless_rows": len([r for r in rows if r["routeless"]]), "route_128_rows": [r["id"] for r in rows if str(r["route"]) == ROUTE], "vocab_hits": [{"id": r["id"], "route": r["route"], "hits": r["hits"], "outcome": r["outcome"], "title": r["title"]} for r in rows if r["hits"]], "rows": rows, } with open(os.path.join(HERE, "scan.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump(scan, fh, indent=1, ensure_ascii=False) print("range %d..%d: %d present + %d absent = %d (range size %d) -> %s" % (RANGE[0], RANGE[1], proof["present_in_range"], len(absent), proof["present_in_range"] + len(absent), proof["range_size"], proof["present_plus_absent_equals_range"])) print("observed head #%d; ids above the head: %d probed, %d present" % (head, len(above), len([p for p in above if p["present"]]))) print("created_at monotone over present ids: %s" % mono) print("post-#%d window: %d rows, %d routeless, route-128 rows %s" % (SETTER, scan["window_rows"], scan["routeless_rows"], scan["route_128_rows"])) for h in scan["vocab_hits"]: print(" hit #%-5s route=%-5s %s" % (h["id"], h["route"], h["hits"])) ok = (proof["present_plus_absent_equals_range"] and mono and not proof["above_head_present"] and not absent or True) return 0 if (proof["present_plus_absent_equals_range"] and mono) else 1 if __name__ == "__main__": sys.exit(main())