import io

# ---------- CONTEXT.md ----------
p = r"C:/Users/decid/Documents/projects/spt-core/.worktrees/conduit-w2/CONTEXT.md"
s = io.open(p, encoding="utf-8").read()

old = "**A node restart is covered by a boot sweep, not by the owner's own online edge** (BAROMETER W2, releases#78):"
new = "**A node restart is covered by a boot sweep, and an owner that comes online after that sweep by an owner-online edge** (BAROMETER W2 releases#78; CONDUIT W2 releases#228):"
assert old in s, "context lead anchor"
s = s.replace(old, new, 1)

old2 = ("A shell force-killed *during* this boot is launched after it, can never satisfy the predicate, "
        "and so is never spontaneously relaunched \u2014 the deploy/quarantine ruling of KNOWN-HAZARDS 2.6 "
        "preserved by construction. <!-- [doc->REQ-SHELL-PERSISTENT-BOOT-RESTORE] -->")
assert old2 in s, "context tail anchor"

new2 = ("A shell force-killed *during* this boot is launched after it, can never satisfy the predicate, "
        "and so is never spontaneously relaunched \u2014 the deploy/quarantine ruling of KNOWN-HAZARDS 2.6 "
        "preserved by construction. <!-- [doc->REQ-SHELL-PERSISTENT-BOOT-RESTORE] -->\n"
        "  **Daemons boot before endpoints do, so the sweep is not the only trigger** (releases#228): an owner "
        "that is not yet online when the sweep runs is skipped by that very conjunct, and \u2014 the sweep being "
        "once per daemon generation, and bringup from offline emitting no rest edge \u2014 nothing revisited it, so "
        "its shells stayed down until a human relinked. The reconcile loop therefore runs the **same four "
        "conditions** for every owner that has gone **offline\u2192online since the last pass**. The trigger is that "
        "**edge, never the level \u201cthe owner is online\u201d**: a successful restore restamps itself out of "
        "eligibility, but a *failing* launch under a level trigger would be retried every reconcile tick for as "
        "long as the owner stayed up, where the edge bounds it to one attempt per owner-online event. "
        "Re-evaluating later in a generation can only refuse **more**, never less \u2014 every launch path restamps "
        "the birth stamp, a kill never does, and the boot instant is fixed for the machine boot \u2014 so the "
        "force-kill ruling above is preserved identically on the second trigger. <!-- [doc->REQ-SHELL-OWNER-ONLINE-RESTORE] -->")
s = s.replace(old2, new2, 1)
io.open(p, "w", encoding="utf-8", newline="").write(s)
print("CONTEXT.md ok")

# ---------- docs/KNOWN-HAZARDS.md ----------
p = r"C:/Users/decid/Documents/projects/spt-core/.worktrees/conduit-w2/docs/KNOWN-HAZARDS.md"
s = io.open(p, encoding="utf-8").read()

old3 = "**(b) The restart shape reaches a restoration path:** a once-per-daemon-generation boot sweep relaunches an instance when **all four** hold"
new3 = "**(b) The restart shape reaches a restoration path:** a once-per-daemon-generation boot sweep \u2014 and, for an owner that comes online after that sweep has run, an **owner offline\u2192online edge** in the reconcile loop running the identical body (releases#228) \u2014 relaunches an instance when **all four** hold"
assert old3 in s, "KH entry anchor"
s = s.replace(old3, new3, 1)

old4 = ("The boot instant is derived (Linux `btime`; Windows now \u2212 `GetTickCount64`), so a named slack constant "
        "absorbs derivation jitter and keeps a process launched moments after boot from reading as a corpse from "
        "the previous one.")
assert old4 in s, "KH slack anchor"
new4 = (old4 + " **The once-ness of the sweep is scheduling, not safety** (ruled releases#228): the "
        "predates-boot discriminant is what forbids relaunching a force-killed shell, and it is monotone within a "
        "machine boot \u2014 every launch restamps, a kill never does \u2014 so a second trigger evaluating the same "
        "conjuncts can only refuse more. The second trigger is an **edge**, not the level \u201cowner is online\u201d, which "
        "is what bounds a failing launch to one attempt per owner-online event instead of one per reconcile tick.")
s = s.replace(old4, new4, 1)

old5 = ("| 2.7 | A node restart must not permanently strand every `persistent` shell: (a) the daemon heals a "
        "recorded `online` the derivation contradicts, write-guarded on an actual change, and (b) a "
        "once-per-generation boot sweep relaunches an instance only when ALL of")
assert old5 in s, "KH table anchor"
new5 = ("| 2.7 | A node restart must not permanently strand every `persistent` shell: (a) the daemon heals a "
        "recorded `online` the derivation contradicts, write-guarded on an actual change, and (b) a "
        "once-per-generation boot sweep PLUS an owner offline->online edge in the reconcile loop (releases#228 \u2014 "
        "the sweep alone strands the shells of any owner not yet online when it ran) relaunch an instance only "
        "when ALL of")
s = s.replace(old5, new5, 1)

old6 = ("| `shellwake::{heal_stale_online_records,restore_persistent_shells_at_boot,launch_predates_boot}`, "
        "`proc::boot_instant_ms`, `shellinfo::{record_shell_launch,read_shell_launch}` |")
assert old6 in s, "KH mapping row anchor"
new6 = ("| `shellwake::{heal_stale_online_records,restore_persistent_shells_at_boot,"
        "restore_persistent_shells_on_owner_online,OwnerOnlineEdge,launch_predates_boot}`, "
        "`proc::boot_instant_ms`, `shellinfo::{record_shell_launch,read_shell_launch}` |")
s = s.replace(old6, new6, 1)

old7 = ("- **spt-core mapping:** `spt_daemon::shellwake::{heal_stale_online_records, "
        "restore_persistent_shells_at_boot, launch_predates_boot, BOOT_RESTORE_SLACK_MS}`;")
assert old7 in s, "KH mapping bullet anchor"
new7 = ("- **spt-core mapping:** `spt_daemon::shellwake::{heal_stale_online_records, "
        "restore_persistent_shells_at_boot, restore_persistent_shells_on_owner_online, OwnerOnlineEdge, "
        "launch_predates_boot, BOOT_RESTORE_SLACK_MS}`;")
s = s.replace(old7, new7, 1)

io.open(p, "w", encoding="utf-8", newline="").write(s)
print("KNOWN-HAZARDS.md ok")

# ---------- docs/MANIFEST.md ----------
p = r"C:/Users/decid/Documents/projects/spt-core/.worktrees/conduit-w2/docs/MANIFEST.md"
s = io.open(p, encoding="utf-8").read()

old8 = ("`persistent` auto-onlines the shell with its owner, and across a **node restart** \u2014 where nothing breaks "
        "the link, so the instance's record survives the reboot still claiming it is up \u2014 the daemon heals that "
        "record and a **once-per-daemon-generation boot sweep** relaunches the instance if its adapter is "
        "`persistent`, its owner endpoint is online, it is down in fact, and its recorded launch predates the boot "
        "instant.")
assert old8 in s, "MANIFEST sleep/wake anchor"
new8 = ("`persistent` auto-onlines the shell with its owner, and across a **node restart** \u2014 where nothing breaks "
        "the link, so the instance's record survives the reboot still claiming it is up \u2014 the daemon heals that "
        "record and a **once-per-daemon-generation boot sweep** relaunches the instance if its adapter is "
        "`persistent`, its owner endpoint is online, it is down in fact, and its recorded launch predates the boot "
        "instant. Because daemons boot before endpoints do, the same four conditions are re-run for any owner that "
        "goes **offline\u2192online since the last reconcile pass** (releases#228) \u2014 without that edge, an owner that "
        "was still down when the sweep ran keeps its shells down until someone relinks by hand. It is an edge and "
        "not the level \u201cthe owner is online\u201d so that a failing launch is attempted once per owner-online event "
        "rather than once per tick.")
s = s.replace(old8, new8, 1)

old9 = "persistent = true                  # auto-online whenever the owner endpoint is online; a node restart is covered by the boot sweep (see Sleep/wake)"
assert old9 in s, "MANIFEST field-line anchor"
new9 = "persistent = true                  # auto-online whenever the owner endpoint is online; a node restart is covered by the boot sweep, and an owner coming online after it by the owner-online edge (see Sleep/wake)"
s = s.replace(old9, new9, 1)

io.open(p, "w", encoding="utf-8", newline="").write(s)
print("MANIFEST.md ok")
