"""The citation layer: every cited value on route 27's comparison surface, resolved to its source. `screen.py` builds the CELL layer and marks each cell with a `kind`: * `own` the artifact states the value as its own; * `cited` the artifact states the value as ANOTHER return's (or the corpus's), with `cited_from` naming that source; * `alt_instrument` an alternative reading of the same artifact, which the artifact itself reports as the wrong one. This file then resolves every `cited` cell against the cited source's own cells -- the question the reworked route-150 step asks. Three outcomes, kept apart: OK the cited source's own artifact states the same value at that cell; DRIFT it states a DIFFERENT value -- a citation that its source does not carry; UNCHECKED the cited source has no own statement for that cell at all (not a measurement, and the reason is recorded rather than the cell being dropped). The same comparison is done for PROSE claims of the shape ` ... (# ...)`, because that is how #645 states the p = 163 edge, and a prose claim that cannot be pinned to a cell is reported as UNPINNED with its quote rather than being ignored. """ import json import os import re import sys HERE = os.path.dirname(os.path.abspath(__file__)) RUN = os.path.dirname(HERE) RAW = os.path.join(HERE, "raw") CELLS = os.path.join(HERE, "cells.json") # which artifact each source label belongs to, so "the cited source's own cells" is well defined OWNER_PREFIX = [("return161", "return:161"), ("return627", "return:627"), ("622:", "return:622"), ("627:", "return:627"), ("637:", "return:637"), ("644:", "return:644"), ("645:", "return:645"), ("656:", "return:656"), ("corpus:U-FRAME", "corpus"), ("route27:", "route:27")] PROSE_CLAIM = re.compile(r"(\d+)\s*\(\s*#(\d{1,4})([^)]{0,80})\)") CELL_IN_WINDOW = re.compile(r"L\(T_?\{?(\d+)\}?\s*,\s*(\d+)\s*\)") def owner_of(label): for pre, own in OWNER_PREFIX: if label.startswith(pre): return own return None def owner_from_cited_from(text): """The cited source named by a free-text `cited_from` field.""" if not text: return None m = re.search(r"#(\d{1,4})", text) if m: return "return:%s" % m.group(1) if "corpus" in text.lower(): return "corpus" return None def load(p): with open(p, encoding="utf-8") as fh: return json.load(fh) def main(): cells = load(CELLS) own = {} # (owner, level, p) -> list of (label, value, locator) for c in cells: if c["kind"] == "own": o = owner_of(c["source"]) if o: own.setdefault((o, c["level"], c["p"]), []).append( (c["source"], c["value"], c["locator"])) edges, drift, unchecked = [], [], [] for c in cells: if c["kind"] != "cited": continue src = owner_from_cited_from(c.get("cited_from")) key = (src, c["level"], c["p"]) if src else None truth = own.get(key) if key else None if not src: verdict, detail = "UNCHECKED", "the citation names no return and no corpus" elif not truth: verdict, detail = "UNCHECKED", ("the cited source has no own statement for this cell in " "its served artifacts") elif all(t[1] == c["value"] for t in truth): verdict, detail = "OK", "the cited source states the same value" else: verdict, detail = "DRIFT", "the cited source states %s" % sorted({t[1] for t in truth}) rec = {"cell": {"level": c["level"], "p": c["p"]}, "value": c["value"], "citing": c["source"], "citing_locator": c["locator"], "cited_from": c.get("cited_from"), "resolved_to": src, "source_own": [{"source": t[0], "value": t[1], "locator": t[2]} for t in (truth or [])], "verdict": verdict, "detail": detail} edges.append(rec) if verdict == "DRIFT": drift.append(rec) elif verdict == "UNCHECKED": unchecked.append(rec) # ---- prose claims: ` ... (# ...)` ------------------------------------------------ bodies = [("161", os.path.join(RAW, "return161.served.json")), ("627", os.path.join(RAW, "return627.served.json"))] for rid in (622, 637, 644, 645, 656): bodies.append((str(rid), os.path.join(RUN, "job2961", "raw", "return%d.json" % rid))) bodies.append(("route27", os.path.join(RUN, "job2961", "raw", "route27.fetched.json"))) prose, unpinned = [], [] for rid, p in bodies: if not os.path.exists(p): continue b = load(p).get("body", {}) for field in ("report_md", "evidence_md", "finding", "contribution_md", "uncertainty_md"): text = b.get(field) if field == "evidence_md": text = (b.get("research") or {}).get("evidence_md") or text if not isinstance(text, str): continue for m in PROSE_CLAIM.finditer(text): value, source_id, tail = int(m.group(1)), int(m.group(2)), m.group(3) lo, hi = max(0, m.start() - 260), min(len(text), m.end() + 260) window = text[lo:hi].replace("\n", " ") cellm = CELL_IN_WINDOW.search(window) rec = {"citing": "return%s.%s" % (rid, field), "value": value, "cited_source": "return #%d" % source_id, "tail": tail.strip()[:60], "quote": m.group(0), "window": window[:400]} if not cellm: rec["verdict"] = "UNPINNED" rec["detail"] = "no L(T_x, p) cell named within the sentence's window" unpinned.append(rec) prose.append(rec) continue lvl, pp = int(cellm.group(1)), int(cellm.group(2)) truth = own.get(("return:%d" % source_id, lvl, pp)) rec["cell"] = {"level": lvl, "p": pp} rec["source_own"] = [{"source": t[0], "value": t[1], "locator": t[2]} for t in (truth or [])] if not truth: rec["verdict"] = "UNCHECKED" rec["detail"] = "the cited return's served artifacts state no value for this cell" unchecked.append(rec) elif all(t[1] == value for t in truth): rec["verdict"] = "OK" rec["detail"] = "the cited return states the same value" else: rec["verdict"] = "DRIFT" rec["detail"] = "the cited return states %s" % sorted({t[1] for t in truth}) drift.append(rec) prose.append(rec) # ---- scalar claims on the surface: not cells, but cited values all the same ------------------- r161 = load(os.path.join(RAW, "return161.served.json"))["body"] r27 = load(os.path.join(RUN, "job2961", "raw", "route27.fetched.json"))["body"] t161 = (r161.get("report_md") or "") + (r161.get("job_brief") or "") t27 = (r27.get("contribution_md") or "") + (r27.get("uncertainty_md") or "") maxv = max(c["value"] for c in cells if c["kind"] == "own") at_max = sorted({(c["level"], c["p"]) for c in cells if c["kind"] == "own" and c["value"] == maxv}) scalars = [ {"claim": "route 27 cites '#161's 1,307 measured entries'", "citing": "route27.contribution_md", "cited_source": "return #161", "present_in_citing": "1,307" in t27, "present_in_source": ("1,307" in t161) or ("1307" in t161), "source_locator": "return #161's report_md: '1307 entries computed (every tile-prime pair " "with p >= x, 7 <= p <= 1009)'", "nearest_source_quote": next((ln.strip() for ln in t161.splitlines() if "1307 entries" in ln), "")[:200], "verdict": "OK" if (("1,307" in t27) and (("1,307" in t161) or ("1307" in t161))) else "UNCHECKED"}, {"claim": "route 27 cites its diagonal to #161 ('#161's diagonal ... at folds 7..31')", "citing": "route27.contribution_md", "cited_source": "return #161", "present_in_citing": "#161's diagonal" in t27, "present_in_source": "2, 1, 2, 2, 2, 3, 2, 4" in t161, "source_locator": "return #161's report_md: 'The corrected diagonal ... = 2, 1, 2, 2, 2, 3, " "2, 4 at folds 7..31 reproduces'", "nearest_source_quote": next((ln.strip() for ln in t161.splitlines() if "corrected diagonal" in ln), "")[:200], "verdict": "OK" if "2, 1, 2, 2, 2, 3, 2, 4" in t161 else "UNCHECKED"}, {"claim": "route 27 calls L(T29,31) = 4 the largest single-prime capacity on record", "citing": "route27.contribution_md", "cited_source": "the whole surface (checked here)", "present_in_citing": "largest single-prime capacity on record" in t27, "present_in_source": True, "source_locator": "max over every own cell on the surface, computed from cells.json", "nearest_source_quote": "max of own cells = %d, attained at %s" % (maxv, ["T_%d,p=%d" % t for t in at_max]), "verdict": "OK" if (maxv == 4 and (29, 31) in at_max) else "DRIFT"}, ] # What the prose layer does NOT read. A citation screen that claimed to have resolved "every" # cited value while recognising one sentence shape would be asserting coverage it does not have, so # the other citation phrasings used on this record are COUNTED and listed here instead. OTHER_SHAPES = [r"corrected from", r"\bvs\b", r"published row", r"reported by", r"per #\d+", r"as #\d+", r"cited from", r"according to #\d+"] other_shapes = [] for rid, p in bodies: if not os.path.exists(p): continue b = load(p).get("body", {}) for field in ("report_md", "evidence_md", "finding", "contribution_md"): text = b.get(field) if field == "evidence_md": text = (b.get("research") or {}).get("evidence_md") or text if not isinstance(text, str): continue for pat in OTHER_SHAPES: for m in re.finditer(pat, text, re.I): other_shapes.append({"return": rid, "field": field, "pattern": pat, "quote": text[max(0, m.start() - 90):m.end() + 90] .replace("\n", " ")[:220]}) # The nine instrument-sensitive cells, named independently by two returns: #645's unshifted reading # lists the bank cells its convention flips, and #656's seam gate lists the cells a seam defect # flips. If those two sets are the same set, the alternative_instrument disagreements are a # property of the convention and not of any one instrument -- so it is computed, not asserted. unshifted = {(c["level"], c["p"]) for c in cells if c["source"] == "645:gates.bank.unshifted_wrong.mine"} seam = set() for k in load(os.path.join(RAW, "656-job1418-gates.json"))["gates"].get("seam_cells") or {}: m = re.match(r"^T(\d+)/p(\d+)$", k) if m: seam.add((int(m.group(1)), int(m.group(2)))) instrument = {"unshifted_wrong_cells": sorted(unshifted), "seam_cells": sorted(seam), "identical": unshifted == seam, "n": len(unshifted)} by_verdict = {} for e in edges: by_verdict[e["verdict"]] = by_verdict.get(e["verdict"], 0) + 1 out = { "cell_edges": len(edges), "cell_edges_by_verdict": by_verdict, "prose_claims": len(prose), "prose_claims_by_verdict": {v: sum(1 for r in prose if r["verdict"] == v) for v in sorted({r["verdict"] for r in prose})}, "unpinned_prose": unpinned, "drift": drift, "unchecked": unchecked, "edges": edges, "prose": prose, "scalar_claims": scalars, "instrument_cells": instrument, "other_citation_shapes": {"n": len(other_shapes), "shapes": OTHER_SHAPES, "examples": other_shapes[:12]}, "distinct_cited_sources": sorted({e["resolved_to"] or "unresolved" for e in edges}), } with open(os.path.join(HERE, "citations.json"), "w", encoding="utf-8") as fh: json.dump(out, fh, indent=1) print("cited cells resolved: %d %s" % (len(edges), by_verdict)) print("prose claims: %d %s" % (len(prose), out["prose_claims_by_verdict"])) print("DRIFT: %d" % len(drift)) for d in drift: print(" %s cell (T_%s, p=%s): cited %s as %s; the source states %s" % (d["citing"], d.get("cell", {}).get("level"), d.get("cell", {}).get("p"), d.get("cited_from") or d.get("cited_source"), d["value"], sorted({s["value"] for s in d["source_own"]}))) print("UNCHECKED: %d" % len(unchecked)) for u in unchecked[:10]: print(" %s -> %s : %s" % (u["citing"], u.get("cited_from") or u.get("cited_source"), u["detail"])) print("UNPINNED prose claims: %d" % len(unpinned)) for u in unpinned[:10]: print(" %s: '%s' (%s)" % (u["citing"], u["quote"], u["tail"])) print("scalar claims: %s" % [(s["verdict"], s["claim"][:44]) for s in scalars]) print("the %d instrument-sensitive cells: #645 unshifted == #656 seam_cells -> %s" % (instrument["n"], instrument["identical"])) print("citation phrasings NOT read by the prose layer: %d" % len(other_shapes)) return 0 if __name__ == "__main__": sys.exit(main())