#!/usr/bin/env python3 """Job #4586 decisive reads: the served registry lineages and the ledger-cell consequence. Stdlib only, reads only `outputs/job4586/served/` (fetched by fetch_4586.py, every version's bytes pinned against the sha256 its `/history` record publishes). Writes `lines.json` with: * the version lineages of the 8 named paths, with served sha and the sha pins verified; * the served-v10 vs served-v8 registry line diff (the depth of the stale baseline); * the four-cell ledger comparison for every displaced path (v1 served vs v2 accepted); * the generator's own cell-field contract, read from the served `research/qc/questions.js`. Every number in REPORT.md comes from here or from check-job4586.py, which recomputes them from the same inputs. """ import difflib import hashlib import json import os import re HERE = os.path.dirname(os.path.abspath(__file__)) SERVED = os.path.join(HERE, "served") PATHS = { "corner": ("research/corner-correlation.md", "92", 4), "fold": ("research/fold-arithmetic-bridge.md", "101", 3), "derive": ("research/history/staging/derive-0904-L7-transfer.md", "152", 3), "prop": ("paper/proposals/prop-staircase-note.md", "13", 3), "questions": ("research/QUESTIONS.md", None, 10), "centered": ("research/centered-discrepancy-estimate.md", "83", 3), "global": ("research/global-factor-signs.md", "153", 3), "qc": ("research/qc/questions.js", None, 4), } CELL_FIELDS = ["id", "status", "question", "verdict"] BLOCK_RE = re.compile(r"") def slug(p): return p.replace("/", "__") def rd(path): with open(path, "rb") as fh: return fh.read() def txt(path): return rd(path).decode("utf-8") def sha(b): return hashlib.sha256(b).hexdigest() def ledger(t): """The ledger block as the SERVED generator reads it (research/qc/questions.js `parseBlock`): the block is searched in the first 60 lines, each line is trimmed, the key must match `^([a-z]+):\\s*(.*)$` and the value is the rest of that ONE line. A multi-line `verdict:` therefore contributes its first line only -- which is what a rendered row displays. This is why the registered verdict length is the LINE length minus the `verdict: ` prefix, not the whole block span.""" m = BLOCK_RE.search(t) if not m: return None out = {} for ln in m.group(1).split("\n"): ln = ln.strip() if not ln: continue mm = re.match(r"^([a-z]+):\s*(.*)$", ln) if not mm: continue out[mm.group(1)] = mm.group(2).strip() return out def diff_lines(a, b): d = list(difflib.unified_diff(a.split("\n"), b.split("\n"), lineterm="", n=0)) minus = [x for x in d if x[:1] == "-" and x[:3] != "---"] plus = [x for x in d if x[:1] == "+" and x[:3] != "+++"] return minus, plus def main(): out = {"paths": {}, "versions_sha_ok": 0, "versions_sha_bad": []} for tag, (rel, ret, want) in PATHS.items(): h = json.load(open(os.path.join(SERVED, "hist__%s.json" % slug(rel)), encoding="utf-8")) h = h.get("body", h) vs = h.get("versions", []) shas = {v["version"]: v["content_sha"] for v in vs} entry = {"path": rel, "n_versions": len(vs), "want_versions": want, "versions_ok": len(vs) == want, "served_sha": h["timestamps"]["sha256"], "served_is_newest_version": h["timestamps"]["sha256"] == shas.get(len(vs)), "v3_eq_v1": len(vs) >= 3 and shas.get(3) == shas.get(1), "v2_return": (vs[1].get("return_id") if len(vs) > 1 else None), "v2_return_want": ret, "returns": [v.get("return_id") for v in vs]} # sha pin verification for every fetched version body for v in vs: p = os.path.join(SERVED, "ver__%s__v%s.md" % (slug(rel), v["version"])) b = rd(p) ok = v["content_sha"] in (sha(b), sha(b.replace(b"\r\n", b"\n"))) entry["pin_v%d" % v["version"]] = ok if ok: out["versions_sha_ok"] += 1 else: out["versions_sha_bad"].append("%s v%d" % (tag, v["version"])) # an accepted edit after v3? later = [] seen = {} for v in vs: sh = v["content_sha"] seen.setdefault(sh, []).append(v["version"]) copies = [v["version"] for v in vs if len(seen[v["content_sha"]]) > 1] entry["copy_versions"] = copies served = h["timestamps"]["sha256"] for v in vs: if v["version"] > 3 and v["content_sha"] != served: later.append(v["version"]) entry["noncopy_versions_after_v3_differing_from_served"] = later out["paths"][tag] = entry # the registry: served v10 vs served v8 (the base restore-83-153.patch was cut against) q10 = txt(os.path.join(SERVED, "ver__research__QUESTIONS.md__v10.md")) q8 = txt(os.path.join(SERVED, "ver__research__QUESTIONS.md__v8.md")) minus, plus = diff_lines(q8, q10) out["registry"] = { "served_sha": sha(q10.encode("utf-8")), "v8_sha": sha(q8.encode("utf-8")), "v8_to_v10_minus": len(minus), "v8_to_v10_plus": len(plus), "v8_minus_lines": minus, "v8_plus_lines": plus, "questions_ids_v10": len([l for l in q10.splitlines() if re.match(r"^\|\s*`Q-", l)]), "centered_rows_v10": [l for l in q10.splitlines() if "Q-centered-discrepancy-estimate" in l], "global_rows_v10": [l for l in q10.splitlines() if "Q-global-factor-signs" in l], } # 1828's fresh-A output, shipped with #1828 a = txt(os.path.join(SERVED, "f1828_QUESTIONS.restored-83-153.md")) out["regen3289"] = { "B_sha_shipped": sha(a.encode("utf-8")), "B_equals_served_v10": a == q10, "B_vs_served_v10_minus": len(diff_lines(a, q10)[0]), "B_vs_served_v10_plus": len(diff_lines(a, q10)[1]), "B_minus_lines": diff_lines(a, q10)[0], "B_plus_lines": diff_lines(a, q10)[1], } j = json.load(open(os.path.join(SERVED, "f1828_others-ledger-3289.json"), encoding="utf-8")) j = j.get("body", j) out["others_ledger_3289"] = j # the ledger-cell consequence per displaced path (served v1 vs accepted v2) cells = {} for tag in ("fold", "derive", "prop", "corner", "centered", "global"): f1 = ledger(txt(os.path.join(SERVED, "ver__%s__v1.md" % slug(PATHS[tag][0])))) f2 = ledger(txt(os.path.join(SERVED, "ver__%s__v2.md" % slug(PATHS[tag][0])))) if f1 is None and f2 is None: cells[tag] = {"indexed": False, "changed_fields": [], "changed_cells": [], "note": "no ledger block in either version"} continue keys = sorted(set(f1 or {}) | set(f2 or {})) changed = [k for k in keys if (f1 or {}).get(k) != (f2 or {}).get(k)] cells[tag] = {"indexed": True, "changed_fields": changed, "changed_cells": [k for k in changed if k in CELL_FIELDS], "ledger_v1": f1, "ledger_v2": f2, "verdict_len_v1": len(f1.get("verdict", "")), "verdict_len_v2": len(f2.get("verdict", ""))} out["cells"] = cells # the generator's own cell-field contract gen = txt(os.path.join(SERVED, "doc__research__qc__questions.js")) m = re.search(r"for \(const k of \[([^\]]*)\]\)", gen) out["generator"] = { "cell_fields_line": m.group(0) if m else None, "cell_fields": [s.strip().strip("'\"") for s in m.group(1).split(",")] if m else None, "drift_check_sentence": ("section 2's rows -- and nothing else" in gen), "bytes": len(gen.encode("utf-8")), } # corner v2 vs v4 (the supersession evidence) v2 = txt(os.path.join(SERVED, "ver__research__corner-correlation.md__v2.md")) v4 = txt(os.path.join(SERVED, "ver__research__corner-correlation.md__v4.md")) v1 = txt(os.path.join(SERVED, "ver__research__corner-correlation.md__v1.md")) v4l, v2l = v4.split("\n"), v2.split("\n") v2_only = [l for l in v2l if l.strip() and l not in v4l] v4_only = [l for l in v4l if l.strip() and l not in v2l] out["corner"] = { "v2_only_lines": len(v2_only), "v4_only_lines": len(v4_only), "v2_only_head": v2_only[:6], "v4_only_head": v4_only[:6], "v2_has_prime_power": "The prime-power reading." in v2, "v4_has_prime_power": "The prime-power reading." in v4, "v1_has_prime_power": "The prime-power reading." in v1, "v4_has_source_correction": "Source correction (2026-09-27)." in v4, "v2_has_source_correction": "Source correction (2026-09-27)." in v2, } with open(os.path.join(HERE, "lines.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump(out, fh, indent=1, ensure_ascii=False) print("version sha pins: %d ok, bad=%s" % (out["versions_sha_ok"], out["versions_sha_bad"])) for tag, e in out["paths"].items(): print(" %-10s n=%d served=%s v3==v1=%s v2_return=%s later_edits=%s" % (tag, e["n_versions"], e["served_sha"][:8], e["v3_eq_v1"], e["v2_return"], e["noncopy_versions_after_v3_differing_from_served"])) r = out["registry"] print("registry v8 -> v10: -%d +%d" % (r["v8_to_v10_minus"], r["v8_to_v10_plus"])) print("regen B (shipped) vs served v10: -%d +%d equal=%s" % (out["regen3289"]["B_vs_served_v10_minus"], out["regen3289"]["B_vs_served_v10_plus"], out["regen3289"]["B_equals_served_v10"])) for tag, c in out["cells"].items(): print(" cells %-8s indexed=%s changed=%s fields=%s" % (tag, c["indexed"], c.get("changed_cells"), c.get("changed_fields"))) print("generator cell fields:", out["generator"]["cell_fields"]) print("corner: v2-only=%d v4-only=%d" % (out["corner"]["v2_only_lines"], out["corner"]["v4_only_lines"])) if __name__ == "__main__": main()