#!/usr/bin/env python3 """registry_cells -- what a document restoration does to a generated registry row. Written in the job #4466 step check (route 128). Route 128's object is the served *registry*: ``research/QUESTIONS.md`` is generated by ``research/qc/questions.js`` (``gen-questions-index.js``) from the ```` block at the top of each note. Two facts decide every "does restoring the accepted revision change the registry?" question, and both are easy to get wrong: * **A row is built from four ledger fields only.** ``parseBlock`` in the served generator validates exactly ``id | status | question | verdict``, and ``renderQuestions`` writes a section-2 row out of those four (plus a TODO list and file links). The note's *body* is never read. So a body-only difference between the served and the accepted revision cannot move any registry row -- which is why a route that flags "the body changed" must not also expect a registry change. * **A shared id is a group.** Several notes may carry one id; the row reads ``MIXED`` when the group disagrees on the status *or* the verdict, and the displayed verdict comes from the newest record. So the comparison is over the cell fields of the served vs the accepted revision, not over the whole file. ``ledger_fields`` / ``cell_diff`` implement (1); ``classify_lineage`` implements the mirror-cut detector route 128 runs on: a served content sha equal to an EARLIER version's sha with a *distinct* verified revision in between is the re-cut signature, and any version after that earlier one (an applied audit, e.g. return #1954 on ``corner-correlation.md``) means a wholesale restore of the in-between revision would drop it. Boundary: byte and field comparison plus exact set algebra. It makes no claim about whether a restoration *should* be applied, and it does not run the generator. The cell fields are read from the served generator's source; if that generator changes its field set, ``CELL_FIELDS`` must be re-read from it. Test suite: ``tests/test_registry_cells.py``. """ from __future__ import annotations import re LEDGER_RE = re.compile(r"") # The fields research/qc/questions.js parses and renders a row from. CELL_FIELDS = ("id", "status", "question", "verdict") # Fields the generator validates but that do not reach a section-2 row. OTHER_FIELDS = ("todo", "parity") def ledger_fields(text): """The ```` block as a dict, or None when a note carries none. ``parity``/``todo`` and any other ``key: value`` line are kept, because a caller comparing two revisions wants to see every changed field, not only the cell fields. """ m = LEDGER_RE.search(text) if not m: return None out = {} for line in m.group(1).split("\n"): if ":" in line: k, v = line.split(":", 1) out[k.strip()] = v.strip() return out def cell_diff(served_text, accepted_text): """Which registry cell fields a wholesale restore of ``accepted_text`` changes. Returns ``{"changed_fields": [...], "changed_cells": [...], "indexed": bool}``. ``indexed`` is False when neither revision carries a ledger block: such a note is only listed in the index's "notes with no ledger block" section, which the generator's own drift check explicitly does not compare. """ a = ledger_fields(served_text) b = ledger_fields(accepted_text) if a is None and b is None: return {"changed_fields": [], "changed_cells": [], "indexed": False} keys = sorted(set(a or {}) | set(b or {})) changed = [k for k in keys if (a or {}).get(k) != (b or {}).get(k)] cells = [k for k in changed if k in CELL_FIELDS] return {"changed_fields": changed, "changed_cells": cells, "indexed": True} def classify_lineage(versions): """Classify a ``/history/`` ``versions`` list (served order, oldest first). Each entry carries ``version``, ``content_sha`` and ``return_id``. Returns:: {"n": int, "served_sha": str|None, "served_return_id": str|None, "content_from": int|None, # largest earlier version whose sha == served "reverted": bool, # content_from is not None (the mirror cut) "copy_versions": [int], # versions that re-import an earlier sha "accepted": {"version","return_id","content_sha"}|None} ``reverted`` is route 128's signature: the newest version's sha equals an EARLIER version's sha (the cut re-imported a pre-cut file, so the newest version publishes an old sha). ``accepted`` is the newest recorded revision that is not a copy and whose content is not what is served -- the accepted revision the served edition is not serving. """ n = len(versions) if n == 0: return {"n": 0, "served_sha": None, "served_return_id": None, "content_from": None, "reverted": False, "copy_versions": [], "accepted": None} sha = {v["version"]: v["content_sha"] for v in versions} ret = {v["version"]: v.get("return_id") for v in versions} served = sha[n] seen = {sha[1]} copies = [] for i in range(2, n + 1): if sha[i] in seen: copies.append(i) seen.add(sha[i]) content_from = None for i in range(n - 1, 0, -1): if sha[i] == served: content_from = i break accepted = None for i in range(n - 1, 0, -1): if i in copies: continue if ret[i] and sha[i] != served: accepted = {"version": i, "return_id": ret[i], "content_sha": sha[i]} break return {"n": n, "served_sha": served, "served_return_id": ret.get(n), "content_from": content_from, "reverted": content_from is not None, "copy_versions": copies, "accepted": accepted} def edits_after(versions, target_version): """Versions recorded after ``target_version`` that are real edits, not re-cut copies. A copy (a version whose sha repeats an earlier version's) is the mirror cut, not an edit. A non-copy version with a different sha after the target is an edit a wholesale restore of the target would drop. Returns ``[version numbers]``. """ info = classify_lineage(versions) sha = {v["version"]: v["content_sha"] for v in versions} target_sha = sha.get(target_version) return [i for i in range(target_version + 1, info["n"] + 1) if i not in info["copy_versions"] and sha[i] != target_sha] def registry_row_fields(row_line): """id/status/question/verdict of a rendered section-2 row, or None. Section-2 rows read ``| `Q-id` | status | question | verdict | todo | records |``; section-1 rows (``| C | `Q-id` question | status | verdict |``) do not match. """ m = re.match(r"^\|\s*`([^`]+)`\s*\|([^|]*)\|([^|]*)\|([^|]*)\|", row_line) if not m: return None return {"id": m.group(1), "status": m.group(2).strip(), "question": m.group(3).strip(), "verdict": m.group(4).strip()}