#!/usr/bin/env python3 """make-table.py — build TABLE.md from the helpers' answers (evidence/helpers/resolved-*.json) and marks.py. A cell with an entry in marks.py carries the MAIN SESSION's words: it was written after the source was read by eye. Every other cell carries the helper's words and the mark A; its pointers were opened by resolve-helper.py (the file and the line exist; the name the helper gave stands at the line). A dead pointer is dropped here. Read-only but for TABLE.md. """ import json, os, re, subprocess, sys import marks HERE = os.path.dirname(os.path.abspath(__file__)); AUDIT = os.path.dirname(HERE) REPO = os.path.abspath(os.path.join(HERE, "..", "..", "..", "..")); WS = os.path.dirname(REPO) PREFIX = {"AG": os.path.join(WS, "felhom-agent"), "CT": os.path.join(WS, "felhom-controller", "controller"), "CAT": os.path.join(WS, "app-catalog-felhom.eu"), "HUB": os.path.join(REPO, "hub", "internal"), "E": REPO, "AR": os.path.join(REPO, "documentation", "architecture"), "RB": os.path.join(REPO, "documentation", "runbooks"), "EV": os.path.join(AUDIT, "evidence")} TOK = re.compile(r"`((?:AG|CT|CAT|HUB|E|AR|RB|EV)/[^`:\s]+)(?::(\d+)(?:-(\d+))?)?`") R = {h: json.load(open(os.path.join(AUDIT, "evidence", "helpers", f"resolved-{h}.json")))["endings"] for h in ("controller", "agent", "hub")} Q = ["what ends, who starts it", "what the thing created", "what the ending removes and keeps on purpose", "what it leaves by accident", "can it be undone, until when", "who is told", "half-way state", "how do we know"] dropped = [] def live(tok): m = TOK.fullmatch(tok) pre, rest = m.group(1).split("/", 1) p = os.path.join(PREFIX[pre], rest) if not os.path.exists(p): return False if m.group(2): n = sum(1 for _ in open(p, encoding="utf-8", errors="replace")) return int(m.group(3) or m.group(2)) <= n return True def split(ans, eid, q): """helper answer -> (plain text, [live pointers])""" ptrs = [] for m in TOK.finditer(ans): t = m.group(0) if live(t): if t not in ptrs: ptrs.append(t) else: dropped.append(f"{eid}-{q} {t}") t = TOK.sub("", ans) t = re.sub(r"`~?:\d+(?:-\d+)?`", "", t) # a short pointer the resolver could not place t = re.sub(r"\(\s*(?:called|creator|see)?\s*[,;]?\s*\)", "", t) t = re.sub(r"\s+([,.;:)])", r"\1", t) t = re.sub(r"\(\s+", "(", t) t = re.sub(r"([,;:])(?:\s*[,;:])+", r"\1", t) t = re.sub(r"\s{2,}", " ", t).replace("|", "/").strip(" ;,·") t = re.sub(r"\(\)", "", t) if len(t) > 380: cut = max(t.rfind(". ", 0, 380), t.rfind("; ", 0, 380)) t = t[:cut + 1 if cut > 150 else 377].rstrip(" ;") + " …" return t, ptrs def named(m): kind, why = m["named"] if kind != "grep": return kind, f"architecture: {why}" r = subprocess.run(["/usr/bin/grep", "-rEic", "--include=*.md", why, PREFIX["AR"]], capture_output=True, text=True) files = [l for l in r.stdout.split("\n") if l and not l.endswith(":0")] lines = sum(int(l.rsplit(":", 1)[1]) for l in files) return ("MENTIONED" if lines else "UNNAMED"), f"architecture folder: {lines} lines in {len(files)} files match `/{why}/` (a machine count, not a reading)" def main(): out = [open(os.path.join(HERE, "table-head.md"), encoding="utf-8").read().rstrip(), ""] seedmap, bad = {}, [] body = [] for eid, m in sorted(marks.M.items()): e = R[m["src"][0]][m["src"][1]] st, why = named(m) worst = "A" rows = [] for q in range(1, 9): if q in m["o"]: mark, text, ptr = m["o"][q] if ptr is None: _, ps = split(e["answers"].get(str(q), ""), eid, q) ptr = " ".join(ps[:4]) else: mark = "A" text, ps = split(e["answers"].get(str(q), ""), eid, q) ptr = " ".join(ps[:4]) if q == 8 and eid in marks.DRILL: text = marks.DRILL[eid] + " · " + text ptr = ("`EV/drill/diff-all.txt` " + ptr).strip() if mark == "A" and "`" not in ptr: # borrow the code path from question 3, then 1 for alt in (3, 1): _, ps = split(e["answers"].get(str(alt), ""), eid, alt) if ps: ptr = " ".join(ps[:2]) + f" (the code path of question {alt})" break if mark == "A" and "`" not in ptr: bad.append(f"{eid}-{q}: A with no pointer") if not text: bad.append(f"{eid}-{q}: empty answer") for t in TOK.findall(ptr): pass rows.append(f"| {q} | {text} | {mark} | {ptr} |") worst = max(worst, mark, key="ADKN".index) seedmap.setdefault(m["seed"], []).append((eid, st, worst)) body += [f"### {eid} — {m['name']} · {m['seed']} · {st}", "", f"{why} · helper section: {m['src'][0]} „{e['title'].strip()}”", "", "| # | Answer | Mark | Pointer |", "|---|---|---|---|"] + rows + ["", f"**Hand step in a runbook?** {m['rb']}", ""] out += ["## The seed kinds, and where each ending is", "", "| Seed kind | Endings (worst mark) | Named · mentioned · named nowhere |", "|---|---|---|"] for seed in ("App", "Drive", "Copies", "Box", "Household", "Access", "Versions", "Outside"): v = seedmap.get(seed, []) c = {s: sum(1 for x in v if x[1] == s) for s in ("NAMED", "MENTIONED", "UNNAMED")} out.append(f"| {seed} | " + ", ".join(f"{x[0]} ({x[2]})" for x in v) + f" | {c['NAMED']} · {c['MENTIONED']} · {c['UNNAMED']} |") out += ["", "## Seed endings that no code path carries", "", "| Ending | Mark | Answer | Pointer |", "|---|---|---|---|"] out += [f"| {a} | {b} | {c} | {d} |" for a, b, c, d in marks.NO_DOOR] out += ["", "## Not in the table", "", "**Tiny endings that cannot grow (named once, not tabled):** " + " · ".join(marks.TINY) + ".", "", "**Left out for another reason:**", ""] + [f"- {a} — {b}." for a, b in marks.LEFT_OUT] out += ["", "---", ""] + body open(os.path.join(AUDIT, "TABLE.md"), "w", encoding="utf-8").write("\n".join(out).rstrip() + "\n") print(f"endings {len(marks.M)} · dead pointers dropped {len(dropped)} · broken {len(bad)}") for b in bad + dropped: print(" " + b) return 1 if bad else 0 sys.exit(main())