import os
M = r"C:/Users/decid/.claude-spt/accounts/alt/projects/C--Users-decid-Documents-projects-spt-core/memory"
main = os.path.join(M, "MEMORY.md"); gate = os.path.join(M, "GATE-TEST-INDEX.md"); judg = os.path.join(M, "JUDGEMENT-INDEX.md")
mb = open(main, "rb").read(); gb = open(gate, "rb").read(); jb = open(judg, "rb").read()
assert b"\r" not in mb and b"\r" not in jb, "CR in MEMORY/JUDG; abort"
assert gb.count(b"\r\n") == gb.count(b"\n") == gb.count(b"\r"), "GATE not uniformly CRLF"
GEOL = b"\r\n"
assert mb.endswith(b"\n")
lines = mb.split(b"\n")
J = [52,53,54,55,56,57,58,59]; G = [66,67,69,70]
assert lines[51] == b"## Judgement rules (binding)", lines[51]
assert lines[60].startswith(b"- \xf0\x9f\x94\x8e [JUDGEMENT-INDEX.md]"), lines[60][:40]
assert lines[65] == b"## Gate & test craft", lines[65]
assert lines[68].startswith(b"- [gate-craft rules banked 2026-09-08"), lines[68][:40]
assert lines[71].startswith(b"- \xf0\x9f\x94\x8e [GATE-TEST-INDEX.md]"), lines[71][:40]
jm = [lines[i] for i in J]; gm = [lines[i] for i in G]
assert all(l.startswith(b"- [") for l in jm+gm)
assert lines[52].startswith(b"- [a control must SEVER") and lines[59].startswith(b"- [never send a claim")
assert lines[66].startswith(b"- [job scope comes") and lines[67].startswith(b"- [probe the EVENT PATH") and lines[69].startswith(b"- [`gh run list --commit`") and lines[70].startswith(b"- [a run-level conclusion")
jptr = b"- [8 judgement entries banked 2026-09-06..09-09](JUDGEMENT-INDEX.md#moved-2026-09-09) MOVED VERBATIM 2026-09-09 (byte identity asserted per line in the move script; zero dropped): sever-the-branch, success-token-matches-panic, compound-spelling grep, Length-sum hardlinks, one-value column, stamp-from-previous-clock, out-of-usage peer, claim-before-check."
gptr = b"- [4 gate entries banked 2026-09-09](GATE-TEST-INDEX.md#moved-2026-09-09) MOVED VERBATIM 2026-09-09 (same method; content byte-identical, terminators CRLF to match that file; zero dropped): workflow-graph job scope, probe-the-event-path, gh run list --commit full sha, run-level conclusion vs attempt."
drop = set(J+G)
new_lines = []
for i, l in enumerate(lines):
    if i in drop:
        if i == J[0]: new_lines.append(jptr)
        if i == G[0]: new_lines.append(gptr)
        continue
    new_lines.append(l)
new_main = b"\n".join(new_lines)
kept = [l for l in new_lines if l not in (jptr, gptr)]
rebuilt = [None] * len(lines)
for i in drop: rebuilt[i] = lines[i]
it = iter(kept)
for i in range(len(lines)):
    if rebuilt[i] is None: rebuilt[i] = next(it)
assert next(it, None) is None and rebuilt == lines, "reconstruction mismatch (by index)"
ganchor = GEOL + b"## Moved 2026-09-09 (VERBATIM from MEMORY.md Gate & test craft, 4 entries)" + GEOL + b'<a id="moved-2026-09-09"></a>' + GEOL + GEOL.join(gm) + GEOL
janchor = b"\n## Moved 2026-09-09 (VERBATIM from MEMORY.md Judgement rules, 8 entries)\n" + b'<a id="moved-2026-09-09"></a>\n' + b"\n".join(jm) + b"\n"
assert b'id="moved-2026-09-09"' not in gb and b'id="moved-2026-09-09"' not in jb
new_gate = gb + ganchor; new_judg = jb + janchor
assert new_gate.startswith(gb) and new_judg.startswith(jb)
assert new_gate.count(b"\r\n") == new_gate.count(b"\n") == new_gate.count(b"\r"), "GATE terminators no longer uniform"
for l in gm: assert new_gate.count(l + GEOL) == 1 and l not in new_main, l[:50]
for l in jm: assert new_judg.count(l + b"\n") == 1 and l not in new_main, l[:50]
for i, l in enumerate(lines):
    if i not in drop: assert l in new_main
open(main, "wb").write(new_main); open(gate, "wb").write(new_gate); open(judg, "wb").write(new_judg)
mb2 = open(main, "rb").read(); gb2 = open(gate, "rb").read(); jb2 = open(judg, "rb").read()
assert mb2 == new_main and gb2 == new_gate and jb2 == new_judg
print("MOVED", len(gm), "gate +", len(jm), "judgement; MEMORY.md", len(mb), "->", len(mb2), "B; GATE", len(gb), "->", len(gb2), "; JUDG", len(jb), "->", len(jb2))
for l in gm+jm: print("  ", l[:60].decode("utf-8","replace"))
