#!/usr/bin/env python3 """Apply job #3001's revision to paper/anchored-note.md, or refuse and change nothing. Every edit is an anchored exact-substring replacement with an expected occurrence count. The script refuses before writing anything if any anchor is missing or occurs a different number of times, so a served copy that has moved on cannot be silently mangled: it either applies all eight edits to the bytes it was given, or it exits non-zero with the diff it was looking for. It never reflows, never re-wraps and never touches any other byte. The eight edits answer two findings: * review #219 / return #1060 -- the two-class parity floor, read in the right dimension, plus the four Draft 2 items (the §9.1 Theorem 1 constant, the abstract's Proposition 1 part label, the Appendix A orphan row, and Appendix B item 7's +0.0172); * finding #54 / review #260 / return #1537 -- Lemma 3 of research/natal-cap-19-calm-lemma.md. Usage: apply-revision.py --in --out Exit 0 only when every edit applied and every postcondition holds. """ import argparse import hashlib import json import re import sys # The dimension-2 reading, quoted from paper/wall-note.md §2 Face 1 (served). FLOOR_D2 = ("The floor must be read in the sieve dimension the object sits in: this is a " "dimension-2, position-uniform problem, so the operative parity floor is 8 — the " "parity floor of Selberg's Λ² at κ = 2, and also the best constant in print that " "survives the position quantifier (Riesel–Vaughan Lemma 5) — against the constant " "1.28 needed at @17 (`paper/wall-note.md` §2 Face 1; `research/natal-cap-10-sieve-cap.md` " "§1.4, §1.5; Selberg's examples). No κ = 2 extremal example is in print, so 8 is best " "known rather than proven. The number 2 is the dimension-1, whole-range figure, " "reachable only through A = {p+2} plus equidistribution of primes in arithmetic " "progressions; it remains a valid *a fortiori* lower bound and it is not the figure " "this face is barred by. The margin is therefore 8/1.28 = 6.25×, not 2/1.28 = 1.56×, " "and the floor-2 reading is withdrawn.") EDITS = [ # --- review #219 / return #1060: the parity floor, in the right dimension ----------- ("parity floor, §6", "Two-class sieves carry a parity floor of 2 against the constant 1.28 needed at @17 " "(`research/natal-cap-10-sieve-cap.md`; Selberg's examples).", FLOOR_D2, 1), ("parity floor, Appendix A row", "| parity floor 2, needed 1.28 at @17 | §6 | `research/natal-cap-10-sieve-cap.md` lines 195–201, 312 |", "| dimension-2 parity floor 8, needed 1.28 at @17; margin 8/1.28 = 6.25× | §6 | " "`paper/wall-note.md` §2 Face 1; `research/natal-cap-10-sieve-cap.md` §1.4 (interval-uniform 8, " "Riesel–Vaughan Lemma 5), §1.5 (the dimension-1 floor 2) |", 1), # --- review #219: the four Draft 2 items ------------------------------------------- ("§9.1 Theorem 1 constant", "Rosser–Schoenfeld supplies the constant; not written out numerically", "Rosser–Schoenfeld supplies the constant; written out numerically as c′ = 0.6045 (§3)", 1), ("abstract, Proposition 1 part label", "(Proposition 1(i)); the factor is ≍ ln²W if Var/E stays bounded away from 0 and ∞", "(Proposition 1(ii)); the factor is ≍ ln²W if Var/E stays bounded away from 0 and ∞", 1), ("Appendix A orphan row deleted", "| e^{3025}, 1,682 (self-consistent readings) | §5.1 | `paper/wall-note.md` §2 Face 1, lines 262–277 |\n", "", 1), ("Appendix B item 7 residual", "the residuals at @7 through @19 are −0.1005, +0.0686, +0.0172, +0.0136, +0.0161.", "the residuals at @7 through @19 are −0.1005, +0.0686, +0.0173, +0.0136, +0.0161.", 1), # --- finding #54 / review #260 / return #1537 -------------------------------------- ("Appendix B item 6, Lemma 3 corrected by #1537", "which is the same error at the source (report #67, item 1.1); the valid identities there", "which is the same error at the source (report #67, item 1.1), corrected by return #1537 " "(`calm-lemma-lemma3.patch`, accepted 2026-09-23) — that patch is not yet applied to the served " "copy, which still carries the old step, so the source line lands only when it is; the valid " "identities there", 1), ("Appendix C item, Lemma 3 corrected by #1537", "- `research/natal-cap-19-calm-lemma.md`, Lemma 3: the congruence-modulo-W step needs correction " "(Appendix B, item 6); ", "- `research/natal-cap-19-calm-lemma.md`, Lemma 3: the congruence-modulo-W step is corrected by " "return #1537 (Appendix B, item 6; accepted, not yet applied to the served copy); ", 1), ] # The bare ^{...} / _{...} forms the site shows as text, counted outside TeX delimiters. BARE_MATH = re.compile(r"(? bare_math_count(anchor): bad.append(name) return bad # What the result must satisfy, re-derived from the OUTPUT text -- never from the edit list, # which would only restate what the script already believes it did. POSTCONDITIONS = { "dimension2_floor_present": lambda t, o: "operative parity floor is 8" in t, "old_floor_sentence_gone": lambda t, o: "carry a parity floor of 2 against" not in t, "old_floor_row_gone": lambda t, o: "parity floor 2, needed 1.28" not in t, "old_floor_withdrawn": lambda t, o: "the floor-2 reading is withdrawn" in t, "theorem1_constant_numeric": lambda t, o: "written out numerically as c′ = 0.6045 (§3)" in t, "no_stale_not_written_out": lambda t, o: "not written out numerically" not in t, "prop1_part_label_is_ii": lambda t, o: "(Proposition 1(ii))" in t, # the only surviving "(i)" for Proposition 1 is the ε ≥ 1/W falsifier row in §9.1 "prop1_part_label_i_only_in_row": lambda t, o: t.count("Proposition 1(i)") == 1, "orphan_row_gone": lambda t, o: "3025" not in t and "1,682" not in t, # +0.0172 survives exactly once, as the §9 record of Draft 1's own error -- not as a live number "residual_0172_history_only": lambda t, o: t.count("+0.0172") == 0 and t.count("0.0172") == 1, "residual_0173_in_appendix_b": lambda t, o: "the residuals at @7 through @19 are −0.1005, +0.0686, +0.0173" in t, "lemma3_corrected_by_1537_twice": lambda t, o: t.count("#1537") == 2, "lemma3_needs_correction_gone": lambda t, o: "needs correction (Appendix B, item 6)" not in t, # no edit introduces a bare-math mark, and the result carries no more than the served file "edits_introduce_no_bare_math": lambda t, o: not edits_add_bare_math(), "bare_math_not_increased": lambda t, o: bare_math_count(t) <= bare_math_count(o), "heading_count_unchanged": lambda t, o: sum(1 for l in t.splitlines() if l.startswith("#")) == sum(1 for l in o.splitlines() if l.startswith("#")), } def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--in", dest="src", required=True) ap.add_argument("--out", dest="out", required=True) a = ap.parse_args() raw = open(a.src, "rb").read() text = original = raw.decode("utf-8") if "\r\n" in text: raise SystemExit("refusing: the served file is not LF-only, so line-anchored edits are unsafe") sys.stdout.reconfigure(newline="\n", encoding="utf-8", errors="replace") applied, refused = [], [] for name, anchor, replacement, want in EDITS: got = text.count(anchor) if got != want: refused.append({"edit": name, "found": got, "expected": want, "anchor": anchor[:120]}) continue text = text.replace(anchor, replacement) applied.append({"edit": name, "occurrences": got, "bytes": len(replacement.encode("utf-8")) - len(anchor.encode("utf-8"))}) if refused: print(json.dumps({"ok": False, "refused": refused, "why": "anchor mismatch: refusing to write anything"}, indent=1, ensure_ascii=False)) return 2 checks = {name: bool(fn(text, original)) for name, fn in POSTCONDITIONS.items()} ok = all(checks.values()) out_bytes = text.encode("utf-8") if ok: with open(a.out, "wb") as fh: fh.write(out_bytes) print(json.dumps({ "ok": ok, "edits": applied, "checks": checks, "in_bytes": len(raw), "out_bytes": len(out_bytes), # a pre-existing condition of the served file, measured and reported rather than fixed "bare_math_lines_in": len(bare_math_lines(original)), "bare_math_lines_out": len(bare_math_lines(text)), "bare_math_marks_in": bare_math_count(original), "bare_math_marks_out": bare_math_count(text), "in_sha256": hashlib.sha256(raw).hexdigest(), "out_sha256": hashlib.sha256(out_bytes).hexdigest(), "written": a.out if ok else None, }, indent=1, ensure_ascii=False)) return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())