#!/usr/bin/env python3 """Clause 1 of finding #2587: does #321's verdict-line hunk still apply to the served document? The finding states the test: "git apply --check of #321's patch still succeeds on the served file". This script runs exactly that test — forward and reverse, with git and with patch(1) — in a scratch tree holding the served bytes at the patch's own repository path, and it FAILS ITSELF unless the result is the one the return reports: the patch does not apply either way, and the served verdict line is the hunk's added line scoped further. python check-clause1.py inputs are looked for beside this script SAH_JOB_DIR= python check-clause1.py Exit 0 only if every claim below is measured true; exit 1 with the failing claim printed. """ import hashlib import io import os import subprocess import sys 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__)) DOC = "research/history/staging/item-x-offset.md" SCRATCH = os.path.join(HERE, "scratch-clause1") PATCH = os.path.join(HERE, "only-item-x-offset.patch") FULL_PATCH = os.path.join(HERE, "registry-712.patch") SERVED = os.path.join(HERE, "item-x-offset.served.md") HUNK_EXTRACT_SHA = "50292c5b895e8fcff406cad441145a2393ce414b72e488c6368b556026782e66" fails = [] def check(label, cond, detail=""): print("%-58s %s%s" % (label, "OK" if cond else "FAIL", (" " + detail) if detail else "")) if not cond: fails.append(label) def sha(p): return hashlib.sha256(open(p, "rb").read()).hexdigest() def main(): served = open(SERVED, "rb").read() print("served document %s %d bytes sha256 %s" % (DOC, len(served), hashlib.sha256(served).hexdigest())) # the extracted hunk, and (when the full patch is carried) the extraction re-derived check("extracted hunk is the served sha", sha(PATCH) == HUNK_EXTRACT_SHA, sha(PATCH)[:16]) if os.path.isfile(FULL_PATCH): lines = open(FULL_PATCH, encoding="utf-8", newline="").read().split("\n") keep, cur = [], False for ln in lines: if ln.startswith("--- a/"): cur = ln[len("--- a/"):].strip() == DOC if cur: keep.append(ln) text = "\n".join(keep).rstrip("\n") + "\n" check("the hunk re-extracts from registry-712.patch byte for byte", hashlib.sha256(text.encode()).hexdigest() == HUNK_EXTRACT_SHA) else: print("registry-712.patch not carried here; extraction check skipped (full patch 98b79bc6...)") dst = os.path.join(SCRATCH, *DOC.split("/")) os.makedirs(os.path.dirname(dst), exist_ok=True) with open(dst, "wb") as fh: fh.write(served) print("scratch tree holds the served bytes at %s" % DOC) # absolute: git is run with cwd=SCRATCH, so a relative patch path would not be found and the # exit code would say "could not open" rather than "does not apply" (caught by the assertions # below on the first run of this checker). patch_abs = os.path.abspath(PATCH) results = {} for label, extra in (("forward", []), ("reverse", ["-R"])): cmd = ["git", "apply", "--check", "-p1"] + extra + [patch_abs] p = subprocess.run(cmd, cwd=SCRATCH, capture_output=True, encoding="utf-8", errors="replace") results[label] = p.returncode print(" git apply --check %-8s exit %d | %s" % (label, p.returncode, (p.stderr or p.stdout).strip().split("\n")[0][:110])) p = subprocess.run(["patch", "--dry-run", "-p1", "-i", patch_abs], cwd=SCRATCH, capture_output=True, encoding="utf-8", errors="replace") print(" patch --dry-run -p1 exit %d | %s" % (p.returncode, (p.stdout + p.stderr).strip().split("\n")[-1][:110])) check("git apply --check forward fails (patch does not apply)", results["forward"] == 1) check("git apply --check reverse fails (the added line is not served either)", results["reverse"] == 1) check("patch(1) agrees", p.returncode != 0) # the substance: the served verdict line contains the hunk's added clause, scoped further plus = [ln[1:] for ln in open(PATCH, encoding="utf-8", newline="").read().split("\n") if ln.startswith("+") and not ln.startswith("+++")][0] served_lines = served.decode("utf-8").split("\n") verdict = [ln for ln in served_lines if ln.startswith("verdict: ")][0] clause = "the blind test at @37 was sealed alone and is now scored in xchan-at37-score.md" check("the served verdict carries the hunk's @37 clause", clause in verdict) check("the served verdict is not the hunk's line verbatim (scoped later)", verdict != plus) check("the served verdict scopes the sentence to @31", verdict.startswith("verdict: At @31 the best available description was")) check("the hunk's own text would drop that scoping", plus.startswith("verdict: The best available description is")) print(" served verdict (%d chars): %s" % (len(verdict), verdict[:150] + "...")) print() if fails: print("CLAUSE 1: %d claim(s) FAILED: %s" % (len(fails), ", ".join(fails))) return 1 print("CLAUSE 1: all claims measured true -- the hunk's content is present and the patch no " "longer applies in either direction") return 0 if __name__ == "__main__": raise SystemExit(main())