#!/usr/bin/env python3 # Job #627: build the two-row patch to research/QUESTIONS.md for Q-global-factor-signs. # # The served note (research/global-factor-signs.md, sha256(LF) 0a4f04bf...) IS audit #153's # revised file, so its ledger block reads status: ANSWERED with the revised verdict. The # served index still prints PARTIAL with the pre-revision verdict in both rows (59 and 476). # The generator's rule for a one-note id (served research/qc/questions.js e5890d25, summarise()) # is: displayed status = the single shared status, displayed verdict = the last (only) note's # verdict. So a regeneration must print ANSWERED plus the revised verdict, which is exactly # what audit #153's questions-rows.regenerated.txt says. # # This script patches the two served lines and proves the patch applies and lands on the # ledger-derived text. import difflib, hashlib, io, subprocess, sys SERVED = "served-QUESTIONS.md" EXPECTED = "file-b48a80c2.txt" # audit #153's questions-rows.regenerated.txt (LF) OUT = "QUESTIONS.corrected.md" PATCH = "questions-rows-59-476.patch" served = io.open(SERVED, encoding="utf-8").read().split("\n") exp = [l for l in io.open(EXPECTED, encoding="utf-8").read().replace("\r\n", "\n").split("\n") if l.strip()] assert len(exp) == 2, len(exp) row_a = exp[0] # the section-1 row: | C | `Q-global-factor-signs` ... row_b = exp[1] # the section-2 row: | `Q-global-factor-signs` | ANSWERED | ... idx_a = [i for i, l in enumerate(served) if l.startswith("| C | `Q-global-factor-signs`")] idx_b = [i for i, l in enumerate(served) if l.startswith("| `Q-global-factor-signs` |")] assert len(idx_a) == 1 and len(idx_b) == 1, (idx_a, idx_b) print("row A at line", idx_a[0] + 1, "row B at line", idx_b[0] + 1) # sanity: the served rows must be the POST-revision text with the PRE-revision status/verdict for i, name in ((idx_a[0], "A"), (idx_b[0], "B")): assert "| PARTIAL |" in served[i], (name, served[i][:120]) assert "on regular composite inputs" not in served[i], name assert "| ANSWERED |" in row_a and "| ANSWERED |" in row_b assert "on regular composite inputs" in row_a and "on regular composite inputs" in row_b new = list(served) new[idx_a[0]] = row_a new[idx_b[0]] = row_b io.open(OUT, "w", encoding="utf-8", newline="\n").write("\n".join(new)) diff = difflib.unified_diff(served, new, fromfile="a/research/QUESTIONS.md", tofile="b/research/QUESTIONS.md", n=0, lineterm="") patch_text = "\n".join(diff) + "\n" io.open(PATCH, "w", encoding="utf-8", newline="\n").write(patch_text) changed = sum(1 for l in patch_text.split("\n") if l.startswith("+") and not l.startswith("+++")) removed = sum(1 for l in patch_text.split("\n") if l.startswith("-") and not l.startswith("---")) print("patch lines: +%d -%d" % (changed, removed)) assert changed == 2 and removed == 2 # apply it for real, with patch(1), to a copy, and compare import os os.makedirs("pa/research", exist_ok=True) io.open("pa/research/QUESTIONS.md", "w", encoding="utf-8", newline="\n").write("\n".join(served)) r = subprocess.run(["patch", "--binary", "-p1", "-i", os.path.abspath(PATCH)], capture_output=True, text=True, cwd="pa") print("patch exit", r.returncode, r.stdout.strip(), r.stderr.strip()) got = io.open("pa/research/QUESTIONS.md", encoding="utf-8").read() want = io.open(OUT, encoding="utf-8").read() print("patch result == corrected file:", got == want) assert got == want print("served sha256(LF)", hashlib.sha256(("\n".join(served)).encode()).hexdigest()[:16]) print("patched sha256(LF)", hashlib.sha256(want.encode()).hexdigest()) print("served lines", len(served), "corrected lines", len(new))