import io, sys

P = 'd2-integration-receipt.md'
s = io.open(P, encoding='utf-8').read()


def sub(old, new, note='edit'):
    global s
    n = s.count(old)
    if n != 1:
        sys.exit('ANCHOR %r matched %d times (%s)' % (old[:70], n, note))
    s = s.replace(old, new)
    print('ok  ' + note)


sub("| `all-run-1541dac6.txt` (superseded full suite) | `2bd12fc8` | 12 | 624 s | 52 s |",
    "| `all-run-1541dac6.txt` (superseded full suite) | `1541dac6` (see below) | 12 | 624 s | 52 s |")

sub("The residual arms cost more per tally (71 s) than the full suite's mixed average (49 s) because each",
    "**The first row's attribution was wrong when this table was written, and the correction matters more\n"
    "than the error** (doyle caught it, 2026-09-13). I bound `all-run-1541dac6.txt` to `2bd12fc8`; it is\n"
    "the run of `1541dac6`, a different driver. Trying to bind it by its RECORDED subject hash then\n"
    "surfaced the real defect: **no run output records the subject hash it ran against.** Searching\n"
    "every preserved run output for a 64-hex string returns nothing -- not in `all-run.txt`, not in\n"
    "`residual-run.txt`, not in the single-arm reruns. The only thing tying any of those runs to a\n"
    "driver is a filename label I typed, which is exactly the kind of binding that cannot be audited.\n"
    "The durations above stand as measured; the attributions rest on filenames and on this receipt.\n\n"
    "`1541dac6` is also **not preserved** in `history/`, so it joins `f42d333b` as a lineage link whose\n"
    "bytes cannot be produced again. That is two unpreserved links, both recorded as reproducibility\n"
    "gaps rather than reconstructed.\n\n"
    "**Proposed rig repair, not made and not authorized:** have the runner stamp\n"
    "`SUBJECT_SHA256=<hash>` into the first lines of its own output, so every future run binds itself\n"
    "to its subject without anyone's memory. Not done here because `rig-d2/run-d2.sh` is executing the\n"
    "integration-control pass as this is written, and there are no edits beneath a running suite.\n\n"
    "The residual arms cost more per tally (71 s) than the full suite's mixed average (49 s) because each")

io.open(P, 'w', encoding='utf-8', newline='\n').write(s)
print('WROTE ' + P)
