"""Classify the regeneration: which rows changed, why, and which depend on git author dates. For every question id in the served and regenerated QUESTIONS.md, gather the ledger blocks of the notes that carry that id (from the corpus mirror), and decide for each CHANGED row whether the change is staleness (records/verdicts moved since the last regeneration) or a date-order flip (the same records, a different winner because git is absent so path order was used). """ import io import json import os import re import sys from collections import defaultdict sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace") HERE = os.environ.get("SAH_JOB_DIR") or os.path.dirname(os.path.abspath(__file__)) CORPUS = os.environ.get("SAH_CORPUS") or os.path.join(HERE, "corpus") BLOCK = re.compile(r"") ROW = re.compile(r"^\|(?P[^|]*)\|.*?`(?PQ-[A-Za-z0-9-]+)`(?P.*)$") def notes(): out = [] for root in (os.path.join(CORPUS, "research", "history", "staging"), os.path.join(CORPUS, "research")): if not os.path.isdir(root): continue for name in sorted(os.listdir(root)): if not name.endswith(".md"): continue p = os.path.join(root, name) if os.path.isdir(p): continue out.append(p) return out def blocks(): by_id = defaultdict(list) for p in notes(): try: src = open(p, encoding="utf-8", errors="replace").read() except OSError: continue head = "\n".join(src.split("\n")[:60]) m = BLOCK.search(head) if not m: continue f = {} for raw in m.group(1).split("\n"): ln = raw.strip() if not ln or ":" not in ln: continue k, v = ln.split(":", 1) f[k.strip()] = v.strip() if "id" in f: rel = os.path.relpath(p, os.path.join(CORPUS, "research")).replace(os.sep, "/") by_id[f["id"]].append({"file": rel, "status": f.get("status", ""), "verdict": f.get("verdict", "")}) return by_id def rows(path): out = {} for ln in open(path, encoding="utf-8", errors="replace"): m = ROW.match(ln.rstrip("\n")) if m: out[m.group("qid")] = ln.rstrip("\n") return out def main(): by_id = blocks() print("ledger blocks: %d notes, %d distinct ids" % ( sum(len(v) for v in by_id.values()), len(by_id))) multi = {k: v for k, v in by_id.items() if len(v) > 1} print("ids carried by more than one note: %d" % len(multi)) disagree = {k: v for k, v in multi.items() if len({(x["status"], x["verdict"]) for x in v}) > 1} print(" of those, records disagree (date matters): %d" % len(disagree)) # the served index is called QUESTIONS.served.md in the job directory and QUESTIONS.base.md in # the served artifact set; SAH_SERVED overrides both. cand = [os.environ.get("SAH_SERVED"), os.path.join(HERE, "QUESTIONS.served.md"), os.path.join(HERE, "QUESTIONS.base.md")] srv_path = next(p for p in cand if p and os.path.isfile(p)) gen_path = os.path.join(CORPUS, "research", "QUESTIONS.md") print("served index %s" % srv_path) print("regenerated %s" % gen_path) srv = rows(srv_path) gen = rows(gen_path) print("\nrows: served %d, regenerated %d" % (len(srv), len(gen))) changed, added, dropped = [], [], [] for qid in sorted(set(srv) | set(gen)): if qid not in srv: added.append(qid) elif qid not in gen: dropped.append(qid) elif srv[qid] != gen[qid]: changed.append(qid) print("changed %d, only-in-regenerated %d, only-in-served %d" % (len(changed), len(added), len(dropped))) print("\n--- changed rows ---") flips = [] for qid in changed: recs = by_id.get(qid, []) recfiles = sorted(r["file"] for r in recs) date_sensitive = len(recs) > 1 verdicts = {r["verdict"][:60] for r in recs} kind = "DATE-SENSITIVE" if date_sensitive else "single-record" if date_sensitive: flips.append(qid) print(" %-38s records=%d %-15s statuses=%s" % ( qid, len(recs), kind, sorted({r["status"] for r in recs}))) print(" served : %s" % srv[qid][:150]) print(" regenerated: %s" % gen[qid][:150]) print("\ndate-sensitive changed rows (%d): %s" % (len(flips), ", ".join(flips))) print("only in regenerated:", ", ".join(added)[:600]) print("only in served:", ", ".join(dropped)[:300]) mixed_gen = [q for q, ln in gen.items() if "MIXED" in ln] mixed_srv = [q for q, ln in srv.items() if "MIXED" in ln] print("\nMIXED rows: served %d, regenerated %d" % (len(mixed_srv), len(mixed_gen))) print(" regenerated MIXED ids:", ", ".join(sorted(mixed_gen))) if __name__ == "__main__": main()