#!/usr/bin/env python3 """Apply the revision of research/history/staging/attack-kstar-01.md that answers finding #13. Every edit is an EXACT anchor -> replacement pair. The script refuses (exit 2, writing nothing) unless every anchor occurs exactly once in the served text, so the revision cannot silently drift from the document it was derived from. It also re-checks the resulting file for the invariants stated in the return: the ladder list has 17 entries ending in 21, the table has 6 new rows, and no other count in the requested set is stale. Usage: apply-revision.py --in --out [--diff] """ import argparse import json import re import sys EDITS = [ # (why, anchor, replacement) ("title: the document now presents six new ladder rows, not five", "# K* extended five steps: the drift is structural,", "# K* extended six steps: the drift is structural,"), ("ledger verdict line counts the enumeration", "verdict: Structural and it lands early: K* is now enumerated at 16 doubling steps against 11,", "verdict: Structural and it lands early: K* is now enumerated at 17 doubling steps against 11,"), ("§0 verdict: enumeration count and the third period-free certificate", "> **The alarm is structural, and it lands early. K* is now enumerated at 16\n" "> doubling steps (was 11): three new full period walks (13#→29#, 13#→31#,\n" "> 17#→31#) and two period-free certificates past every scannable level\n" "> (19#→37#, 19#→41#). The raw drift slope steepens from 0.6881 ± 0.1328 to\n" "> 0.8184 ± 0.0908, and the certificate touches K*+1 = 18 — 93.5% of", "> **The alarm is structural, and it lands early. K* is now enumerated at 17\n" "> doubling steps (was 11): three new full period walks (13#→29#, 13#→31#,\n" "> 17#→31#) and three period-free certificates past every scannable level\n" "> (19#→37#, 19#→41#, 19#→43#). The raw drift slope steepens from 0.6881 ±\n" "> 0.1328 to 0.8184 ± 0.0908, and the certificate touches K*+1 = 18 — 93.5% of"), ("§0: six new C₂ values, not five (4.12 is inside the stated range)", "> P(2s) = 31. C₂ itself stays flat (3.22–5.27 across the five new steps);", "> P(2s) = 31. C₂ itself stays flat (3.22–5.27 across the six new steps);"), ("§1: certificate-only steps are now three", "= Π(p−2) — plus two certificate-only steps (§3). The fold-recursion path was", "= Π(p−2) — plus three certificate-only steps (§3). The fold-recursion path was"), ("§1: the finding's new ladder row, and the ladder appended with 21", "| 19#→41# | 21 | 16 | 17 | 546/150 = 3.6400 | 4.67 | I–E, period-free |\n" "\n" "Full ladder (K*+1, 16 steps): 3 2 5 3 4 4 6 9 7 11 9 11 18 14 14 17.", "| 19#→41# | 21 | 16 | 17 | 546/150 = 3.6400 | 4.67 | I–E, period-free |\n" "| 19#→43# | 22, 23 | 20 | 21 | 618/150 = 4.1200 | 5.10 | pruned covering DP (kstar_dfs.py, #1144), period-free |\n" "\n" "Full ladder (K*+1, 17 steps): 3 2 5 3 4 4 6 9 7 11 9 11 18 14 14 17 21."), ("§1: exactness scope kept on the pass's five, the sixth attributed; slack widened to 5.10", "All five new C₂ comparisons are custody-exact (both G₂ terms among the 14\n" "exact ladder terms). The certificate holds at every step and its slack\n" "cert/C₂ **widens monotonically in scale**: 1.00 at the first three steps\n" "(attack-doubling-01) to 4.67 at 19#→41#. **[VERIFIED]**", "All five of this pass's new C₂ comparisons are custody-exact (both G₂ terms\n" "among the 14 exact ladder terms); the sixth, 19#→43#'s, is exact on the same\n" "footing by its own report (#1144). The certificate holds at every step and\n" "its slack cert/C₂ **widens monotonically in scale**: 1.00 at the first three\n" "steps (attack-doubling-01) to 5.10 at 19#→43#. **[VERIFIED]**"), ("§2: six new C₂ values, not five", "What this does NOT say: C₂ shows no drift (the five new values sit in", "What this does NOT say: C₂ shows no drift (the six new values sit in"), ("§2: I_eff/trend scope is the 16 points of that fit, not 'all' steps", " stays in **[0.55, 2.56]** across all 16 steps, raw trend 0.3676 ± 0.1400", " stays in **[0.55, 2.56]** across the same 16 steps, raw trend 0.3676 ± 0.1400"), ("§3: the reach bullet -- 19#→43# was reached, in minutes, by the pruned covering search", "- **Where the engine's own reach ends.** 19#→43# needs a peak pass of\n" " ~2.5·10¹⁰ subsets (priced from the measured prune profile), 23#→43#\n" " ~1–2·10¹⁰ with D₂₃ = 7.95·10⁶ shapes — an hour-class JS run each; base\n" " 29 (D₂₉ = 2.15·10⁸) is out entirely. Not run. **[MEASURED the profile;\n" " the prices are estimates]**", "- **Where the engine's own reach ends.** 19#→43# was reached by the pruned\n" " covering search in minutes — K* = 20 over all 378,675 starts in 329 s\n" " (`kstar_dfs.py`, #1144; rerun in review 2985) — where this engine's own\n" " price for the same step is a peak pass of ~2.5·10¹⁰ subsets. 23#→43#\n" " ~1–2·10¹⁰ with D₂₃ = 7.95·10⁶ shapes — an hour-class JS run each; base\n" " 29 (D₂₉ = 2.15·10⁸) is out entirely. Not run. **[MEASURED the profile;\n" " the prices are estimates]**"), ("§6: six new certificates, with 21 in the list", "Nothing here derives an all-s constant; the five new certificates are\n" "finite-level theorems (C₂ ≤ 11, 18, 14, 14, 17 at their steps) and imply", "Nothing here derives an all-s constant; the six new certificates are\n" "finite-level theorems (C₂ ≤ 11, 18, 14, 14, 17, 21 at their steps) and imply"), ("§7: the fit's scope made explicit, and enumeration now reaches 43", "- **No all-s bound on C₂ or on K*.** The near-linear verdict is a 16-point\n" " measurement plus HL-model asymptotics, not a theorem; growth past\n" " P(2s) = 41 is inferred, not enumerated.", "- **No all-s bound on C₂ or on K*.** The near-linear verdict is a 16-point\n" " measurement (the 19#→43# row is not in that fit) plus HL-model asymptotics,\n" " not a theorem; growth past P(2s) = 43 is inferred, not enumerated."), ("§7: 19#→43# is no longer \"not run\" (finding, line 229)", "- **19#→43#, 23#→43#, 23#→47#**: priced (§3), not run; base 29+ out of\n" " reach for the I–E engine as written.", "- **23#→43#, 23#→47#**: priced (§3), not run; base 29+ out of reach for the\n" " I–E engine as written. 19#→43# is no longer \"not run\": the pruned\n" " covering search reached it in minutes (#1144, §3)."), ("§7: K*(19#→41#) = 16 has a second, independent instrument (finding, line 231)", "- **19#→41#'s K* = 16 rests on the I–E route alone** — the period 3.04·10¹⁴\n" " admits no verifying walk; confidence is the 14-step dual-engine\n" " validation, not an independent measurement at that step.", "- **19#→41#'s K* = 16 no longer rests on the I–E route alone** — the\n" " pruned covering search returns 16 at that step too (`kstar_dfs.py`,\n" " #1144), a second, independent instrument; the period 3.04·10¹⁴ still\n" " admits no verifying *walk*, so the confidence remains the 14-step\n" " dual-engine validation plus that agreement, not a walk at that step."), ("§8: the new row's source, and how to reproduce it (timing field named)", "| `research/dhr-verification.md` row 1a | β₂ = 4.26645…, giving 2^β₂ = 19.2455, the ceiling the certificates are read against |", "| `research/dhr-verification.md` row 1a | β₂ = 4.26645…, giving 2^β₂ = 19.2455, the ceiling the certificates are read against |\n" "| `kstar_dfs.py` (served, return #1144) | the pruned covering search: the 19#→43# row's K* = 20, and its certificate cells N_20 = 4, N_21 = 0 |"), ("§8: reproduce the new row", "Reproduce: `node research/qc/embed.js --check --timeout 900\n" "research/attack-kstar-01.js` (the fingerprint matches as of 2026-08-21;\n" "the run re-walks both periods, ~6 minutes).", "Reproduce: `node research/qc/embed.js --check --timeout 900\n" "research/attack-kstar-01.js` (the fingerprint matches as of 2026-08-21;\n" "the run re-walks both periods, ~6 minutes). The 19#→43# row: `python\n" "kstar_dfs.py 19 43` → `kstar: 20`, every start tested at length 21, so\n" "N_21 = 0 — and `--count 20` → `N_k: 4` (return #1144). Its `seconds` field\n" "is wall time and is the only field that differs between runs."), ] def main(): ap = argparse.ArgumentParser() ap.add_argument("--in", dest="src", required=True) ap.add_argument("--out", dest="dst", required=True) ap.add_argument("--report") ap.add_argument("--diff", action="store_true") a = ap.parse_args() sys.stdout.reconfigure(newline="\n") text = open(a.src, encoding="utf-8", newline="").read() original = text applied = [] for why, old, new in EDITS: n = text.count(old) if n != 1: print(json.dumps({"ok": False, "stage": "anchor", "why": why, "occurrences": n}, indent=1), file=sys.stderr) return 2 text = text.replace(old, new, 1) applied.append(why) # invariants of the revision, checked on the OUTPUT ladder = re.search(r"Full ladder \(K\*\+1, (\d+) steps\): ([0-9 ]+)\.", text) assert ladder, "ladder line missing after the edit" values = ladder.group(2).split() checks = { "ladder_step_count_matches_list": int(ladder.group(1)) == len(values), "ladder_has_17_entries": len(values) == 17, "ladder_last_is_21": values[-1] == "21", "table_has_6_new_rows": text.count("|\n| 1") + text.count("\n| 1") >= 6, "new_row_present": "| 19#→43# | 22, 23 | 20 | 21 | 618/150 = 4.1200 | 5.10 |" in text, "no_stale_sixteen_steps": "16 doubling steps" not in text, # §8's 'the five new certificates' is scoped to what attack-kstar-01.js itself # carries and stays; the ladder-level counts must all have moved. "no_stale_ladder_five": not any(s in text for s in ( "five new steps", "five new C₂", "five new values", "five new certificates are", "five steps:")), "no_stale_past_41": "P(2s) = 41 is inferred" not in text, "size_grew_by_less_than_1200_bytes": 0 < len(text.encode()) - len(original.encode()) < 1200, } with open(a.dst, "w", encoding="utf-8", newline="") as fh: fh.write(text) report = {"tool": "apply-revision/1.0.0", "in_bytes": len(original.encode()), "out_bytes": len(text.encode()), "edits": len(applied), "checks": checks} if not all(checks.values()): print(json.dumps(dict(report, ok=False), indent=1)) return 1 if a.report: with open(a.report, "w", encoding="utf-8", newline="\n") as fh: json.dump(dict(report, why=[w for w, _, _ in EDITS]), fh, indent=1, ensure_ascii=False) fh.write("\n") print(json.dumps(dict(report, ok=True), indent=1)) return 0 if __name__ == "__main__": sys.exit(main())