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

def sub(old, new, note):
    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("## 5. The actual-sequence control\n\nTwelve scenarios through **the real driver**",
    "## 5. The actual-sequence control, against driver `2bd12fc8`\n\n"
    "**This table measures driver `2bd12fc8` and nothing else** (doyle, 2026-09-13). That driver has\n"
    "since been superseded twice -- `81dc4fe8`, then `f7b7773f` -- and the arms changed with it: case\n"
    "J alone went from 7 assertions to 12 when residual tracking was reworked onto run-bound identity.\n"
    "The 92/0 below stays true of the bytes it ran against and is **historical evidence, not a suite\n"
    "result for the current candidate**. The candidate's own measured results are Sec. 5a and 5b. A\n"
    "full-suite rerun on `f7b7773f` is **held by doyle** pending his source review.\n\n"
    "One superseded driver in that lineage, `f42d333b`, was **not preserved**. Its reported 7-arm 44/0\n"
    "therefore stands as a **reported result and not reproducible evidence** -- the subject bytes\n"
    "cannot be produced again. Recorded as a reproducibility gap per doyle; no reconstruction was\n"
    "attempted.\n\nTwelve scenarios through **the real driver**",
    'Sec.5 bound to 2bd12fc8 + f42d333b gap')

TABLE = io.open('rig-d2/residual-results-table.md', encoding='utf-8').read().rstrip('\n')
sub("## 6. Six defects the control caught",
    "## 5a. The focused residual control, against driver `f7b7773f` -- 57 pass / 3 fail\n\n"
    "Ten arms -- the residual-cleanup cases only -- through the **current** driver\n"
    "`f7b7773f6988768421fb4a4f19eeb6a6e12faec6136e7c1cad3032f0c1e2fde0`, frozen for the whole run and\n"
    "re-hashed unchanged afterwards. Same runner, same config-only diff refusal (**6 lines, 0 outside\n"
    "config** on every arm), and every count below read out of the run's own output by\n"
    "`rig-d2/fill_results.py` rather than typed in.\n\n"
    + TABLE + "\n\n"
    "**This 57/3 is preserved as run, with its three failures classified** (doyle, 2026-09-13). All\n"
    "three are RIG defects; none is a driver defect:\n\n"
    "- **R7 (4/1) -- a test pinned to prose.** The arm matched `reason=creation-time-unreadable`, a\n"
    "  spelling the driver stopped using at `81dc4fe8`. It reported a RENAME as a defect. The driver's\n"
    "  behaviour on that arm was correct throughout.\n"
    "- **R9 (4/0) -- passing, on a CONTAMINATED fixture.** Its capture carried swallowed generator\n"
    "  text and three `command not found` lines. It still refused for its own reason -- checked, not\n"
    "  assumed: `INCOMPLETE (pid 4001 has an unreadable creation timestamp [])` -- but the green was\n"
    "  not clean evidence.\n"
    "- **R10 (3/2) -- a VOID arm, not a red.** Its fixture never injected the impossible date; the arm\n"
    "  was handed an ordinary both-live scenario and the driver answered it correctly\n"
    "  (`RESIDUE_SUMMARY live=2 gone=0 unreadable=0`, `residual_state=LIVE`, exit 6). The arm never\n"
    "  reached its subject, so it measured nothing about the 30-February repair.\n\n"
    "**Both `R3` and `R5` reach `CLEANUP=VERIFIED` / `residual_state=CLEAR` / exit 0, by different\n"
    "routes** -- R3 because both tracked identities are measured GONE, R5 because the pid answers and\n"
    "the identity does not match, so ours is gone and a stranger is not manufactured into residue. An\n"
    "earlier statement of mine that R3 was the only arm reaching VERIFIED was wrong and is corrected\n"
    "here rather than annotated; `rig-d2/fill_results.py` carried the same wrong wording in R3's\n"
    "description and was corrected in the same pass.\n\n"
    "## 5b. The three corrected arms, rerun against the SAME unchanged driver\n\n"
    "Two rig repairs, no driver edit -- `fp-driver-d2.sh` was re-hashed before and after each rerun and\n"
    "is still `f7b7773f...`.\n\n"
    "1. **R7's reason-wording assertion was REMOVED and deliberately NOT re-pinned to the new prose**\n"
    "   (doyle's ruling): wording is the driver's to change, and an arm pinned to it reports a rename\n"
    "   as a defect. R7 now asserts the STATE and the verdict -- both attributed rows `UNREADABLE`,\n"
    "   `RESIDUE_SUMMARY live=0 gone=0 unreadable=2` so neither is counted gone nor live, no\n"
    "   `GONE-PID-REUSED` claim, `residual_state=UNREADABLE`, `CLEANUP=PENDING`, driver exit 6.\n"
    "2. **`rig-d2/make-d2-stubs.sh` was malformed.** In the `badstamp` arm a `printf` format string was\n"
    "   never closed before its newline, so the entire `impossible)` arm was swallowed as literal text\n"
    "   INSIDE that string. `impossible)` was therefore not a case arm at all: `FP_IDENT_MODE=impossible`\n"
    "   matched nothing, fell past the `esac`, and landed on the default live path -- which is exactly\n"
    "   what R10 was handed. The swallowed text was also EXECUTED as shell in R9's capture. This is the\n"
    "   same escape-layer class as defect 6 below.\n\n"
    "Before the reruns the regenerated stub was **exercised directly**, as doyle required, rather than\n"
    "only through the driver (stub `e09f829dfc6bb71531f7e683707aa1d5f2dd63ceb05feb1390b6e2a86667e826`,\n"
    "byte-identical to the one both reruns then used):\n\n"
    "| `FP_IDENT_MODE` | native exit | emitted identity record | shell errors |\n"
    "| --- | --- | --- | --- |\n"
    "| `badstamp` | 0 | `IDENT role=supervisor pid=4001 created= exe=[...] cmd=[...]` then `IDENT_PROBE=OK tracked=1` | none |\n"
    "| `impossible` | 0 | `IDENT role=supervisor pid=4001 created=2026-02-30T00:00:00.000Z ...` then `IDENT_PROBE=OK tracked=1` | none |\n\n"
    "| arm | driver exit | pass | fail | the driver's own stated reason |\n"
    "| --- | --- | --- | --- | --- |\n"
    "| **R7** | 6 | 7 | 0 | both rows `state=UNREADABLE`, `live=0 gone=0 unreadable=2`, `CLEANUP=PENDING` |\n"
    "| **R9** | 6 | 4 | 0 | `INCOMPLETE (pid 4001 has an unreadable creation timestamp [])` |\n"
    "| **R10** | 6 | 5 | 0 | `INCOMPLETE (pid 4001 has an unreadable creation timestamp [2026-02-30T00:00:00.000Z])` |\n\n"
    "R10's refusal **names the impossible value it rejected**, and R9's and R10's captures contain\n"
    "**zero** `command not found` lines. That is the first measurement in which the 30-February repair\n"
    "is exercised by an arm rather than by a standalone predicate test.\n\n"
    "## 6. Six defects the control caught",
    'Sec.5a and 5b inserted')

sub("\n## 7. The cleanup verdict, as doyle ruled it (01:12:44Z)",
    "\n## 6a. Two further defects -- caught by REVIEW, not by the control\n\n"
    "Both were found by doyle reading the source, against a suite that was **92/0 at the time**. They\n"
    "are listed apart from the six above because the provenance is the point: a green suite is not\n"
    "what surfaced them, and no arm that existed would have failed on either.\n\n"
    "7. **A partial capture reads as CLEAR under a re-query-only check.** The identity capture wrote\n"
    "   one supervisor row and then failed; the brain was never recorded and was still running. The one\n"
    "   pid that WAS recorded is genuinely absent, so a check asking only whether the recorded pids are\n"
    "   gone answers yes and reports a tidy box with a live daemon on it. Per doyle a capture may\n"
    "   support CLEAR only if it is COMPLETE: exit 0, exactly one `IDENT_PROBE=OK tracked=N`, N equal to\n"
    "   the rows written, and every row carrying a numeric pid, a readable creation time, a non-empty\n"
    "   exe and a non-empty command line. Anything else is UNREADABLE **including the case where every\n"
    "   recorded pid is absent**. Arm R8 is that case.\n"
    "8. **Shape is not validity.** `ts_readable` matched a character-class pattern, so\n"
    "   `2026-02-30T00:00:00.000Z` -- a day that does not exist -- read as a comparable timestamp and\n"
    "   could establish pid reuse, the one finding that turns a LIVE process into GONE. It now PARSES\n"
    "   the value with the calendar (`date -u -d`), rejecting the empty string first because an empty\n"
    "   argument to `date -d` is accepted as *now*. Measured discriminator, old predicate against new\n"
    "   on the same inputs: `2026-02-30` READABLE -> unreadable, while the real leap days `2024-02-29`\n"
    "   and `2028-02-29` stay READABLE under both, so the repair is calendar-aware rather than a\n"
    "   blanket rejection of late February. Arm R10 is that case -- and see Sec. 5b for why R10 did\n"
    "   not actually exercise it until the fixture was repaired.\n"
    "\n## 7. The cleanup verdict, as doyle ruled it (01:12:44Z)",
    'Sec.6a inserted')

sub("- Prerequisites remain outstanding and separate:",
    "- **`ts_readable` depends on GNU `date -u -d`** (measured against GNU coreutils 8.32 on this box).\n"
    "  doyle ruled this acceptable for a Windows/MSYS-specific diagnostic and asked for no portability\n"
    "  gate; it is recorded rather than assumed. On a BSD/macOS `date` the parse would fail and every\n"
    "  creation stamp would read UNREADABLE -- which refuses rather than fabricates a CLEAR, so it\n"
    "  fails safe, but every residual verdict on such a box would be UNREADABLE.\n"
    "- **The full suite has never run against `f7b7773f`.** What is measured on the current driver is\n"
    "  the ten residual arms of Sec. 5a and 5b. The other eleven cases are measured only on `2bd12fc8`.\n"
    "- Prerequisites remain outstanding and separate:",
    'Sec.8 ceilings')

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