import re, sys, os
os.chdir(r"C:\Users\decid\Documents\projects\spt-core\.worktrees\reclass-274")

NT = ".config/nextest.toml"; GY = ".github/workflows/golden.yml"; LG = "docs/FLAKE-LEDGER.md"
nt = open(NT, "rb").read(); gy = open(GY, "rb").read(); lg = open(LG, "rb").read()

HEAVY_RE = re.compile(rb"\(package\(spt-daemon\) & kind\(test\) & binary\(/\^\((.*?)\)\$/\)\)")
def heavy(b, label):
    m = HEAVY_RE.findall(b)
    assert len(m) == 1, f"{label}: expected exactly one spt-daemon HEAVY group, got {len(m)}"
    return m[0]
before_nt, before_gy = heavy(nt, NT), heavy(gy, GY)
assert before_nt == before_gy, "HEAVY spt-daemon groups differ BEFORE edit"
assert b"brain_resume_conn_deadlock" not in before_nt, "already a member"

OLD = b"attach|brain_swap|"; NEW = b"attach|brain_resume_conn_deadlock|brain_swap|"
for label, b in ((NT, nt), (GY, gy)):
    assert b.count(OLD) == 1, f"{label}: OLD anchor count {b.count(OLD)} != 1"
nt2 = nt.replace(OLD, NEW); gy2 = gy.replace(OLD, NEW)
after_nt, after_gy = heavy(nt2, NT), heavy(gy2, GY)
assert after_nt == after_gy, "HEAVY groups differ AFTER edit"
assert after_nt.split(b"|") == sorted(after_nt.split(b"|")) or True  # order is historical, not asserted
assert after_nt.replace(b"brain_resume_conn_deadlock|", b"") == before_nt, "edit changed more than the one member"

# nextest.toml stanza, inserted immediately before the override table that carries the filter.
NL = b"\r\n" if b"\r\n" in nt else b"\n"
stanza_lines = [
 "# v0.67.1 golden r1+r2 (doyle RCA-274-R1-LINUX, 2026-09-06): brain_resume_conn_deadlock",
 "# was NEVER a member — born 03c71093 (2026-07-09), SIX DAYS before the HEAVY-AT-BIRTH ruling,",
 "# and the sweep was only ever applied forward from that date. Each of its two timing cells",
 "# spins a REAL broker + SIX real PTY `yes` flood children and the two are byte-identical",
 "# rigs, so in the light pool they run CONCURRENTLY (12 floods + 2 brokers) inside a 3186-",
 "# tests-in-48s Phase A. Golden 34014574926 @34fdb848: att1 97 ticks vs the >=100 floor (twin",
 "# PASS in the same window), att2 88 and 97 (BOTH twins). The floor guards a deadlock whose",
 "# signature is ~0 ticks; 88-97 is a load-margin miss ~100x above it, and the deterministic",
 "# zero-subscriber guard in the same binary PASSED both attempts. HEAVY under this file's",
 "# own criterion; added to both <HEAVY> strings (this one and golden.yml's job-level `HEAVY`",
 "# env), extracted and asserted byte-equal before and after the edit by the edit script.",
 "# PRE-REGISTERED DISCRIMINATOR: serialized in Phase B on the quiet box these cells run one",
 "# at a time — a red THERE refutes the pool mechanism and re-opens the box/product question.",
]
stanza = NL.join(s.encode() for s in stanza_lines) + NL
anchor = b"[[profile.default.overrides]]" + NL + b"filter = '(package(spt-daemon)"
assert nt2.count(anchor) == 1, f"override anchor count {nt2.count(anchor)} != 1"
nt3 = nt2.replace(anchor, stanza + anchor)

# FLAKE-LEDGER row appended to the table (last line is a row).
LNL = b"\r\n" if b"\r\n" in lg else b"\n"
assert lg.rstrip(LNL).split(LNL)[-1].startswith(b"| "), "ledger does not end on a table row"
row = ("| `spt-daemon::brain_resume_conn_deadlock daemon_cursor_only_resume_keeps_heartbeat_live_respawn_interleave` (+ twin `…_steady_state` on att2) "
 "| 2× kitsubito Linux golden 34014574926 @34fdb848 (v0.67.1 docs-only patch #274): att1 `respawn_interleave` FAIL 6.822s 97 ticks/6s, twin PASS 6.809s in the same window; "
 "att2 (same sha, `rerun --failed`) BOTH twins FAIL — 88 and 97 ticks/6s, ordinals 1652/1655 "
 "| **LIGHT-POOL STARVATION OF A HEAVY-QUALIFYING BINARY — a classification miss, not the assert's mechanism.** The file spins a REAL broker + SIX real PTY `yes` floods per test and its two timing cells are byte-identical rigs, so Phase A runs them CONCURRENTLY (12 floods + 2 brokers) inside a 3186-tests-in-48s full-parallel window. "
 "It was in NEITHER `<HEAVY>` string at the sha and `git log -S` says it never was: born `03c71093` 2026-07-09, six days BEFORE the HEAVY-AT-BIRTH ruling (2026-07-15), which was applied forward and never back over pre-ruling files; sibling flood rig `input_ack_deadlock` IS heavy. "
 "The `>= 100` floor guards the SharedSend self-deadlock whose signature is ~0 ticks (header: healthy = hundreds); 88–97 is ~100× above it — a load-margin miss, and the deterministic `daemon_resume_leaves_zero_brain_subscribers` guard PASSED both attempts. Delta test: 5 files, ZERO `.rs` (CHANGELOG, Cargo version material, two docs-site pages) — the diff cannot reach the cell; Windows leg + n1 + twohost GREEN at the same sha (platform split = load tell). "
 "The `CONN_WRITE_RETIRED` BrokenPipe lines in the failure block are stamped mono_ms 6693+ — the rig's own `kill_pid` sweep after the 600ms settle + 6s window, teardown not cause. First sighting in ~2 months of goldens = the margin was thin the whole time. RCA: `RCA-274-R1-LINUX.md` (doyle, 2026-09-06); logs preserved `Documents\\spt-preserve\\v0671-golden-r1` "
 "| **HARDENED AT THE RECIPE (2026-09-06, this commit, config-only, folded on top of `34fdb848` for the v0.67.1 respin):** binary added to BOTH `<HEAVY>` strings (`.config/nextest.toml` override + `golden.yml` job-level `HEAVY` env; the two copies extracted and asserted byte-equal before and after by the edit script — the comment claiming \"exactly two places\" was MEASURED, ci.yml carries none). Same-sha rerun was tried ONCE under the named mechanism and its second red fired the pre-committed respin, never a third rerun. "
 "PRE-REGISTERED DISCRIMINATOR: in Phase B (serialized, quiet box) the twins run one at a time — a red THERE refutes the pool mechanism. Riders for hertz's next test lane (NOT this commit): re-sweep spt-daemon tests for other real-PTY/real-broker binaries outside `<HEAVY>` (report the population); `eprintln!` the tick count on green so the margin is visible before it reds |")
lg2 = lg.rstrip(LNL) + LNL + row.encode() + LNL

for path, b in ((NT, nt3), (GY, gy2), (LG, lg2)):
    open(path, "wb").write(b)
print("OK nextest.toml +stanza +member; golden.yml +member; ledger +1 row")
print("HEAVY equal after:", heavy(open(NT,'rb').read(), NT) == heavy(open(GY,'rb').read(), GY))
