import io

p = r"C:/Users/decid/Documents/projects/spt-core/.worktrees/conduit-w2/crates/spt-daemon/src/shellwake.rs"
s = io.open(p, encoding="utf-8").read()

anchor = """    fn fast_params() -> WakeParams {"""
assert anchor in s, "fast_params anchor"

test = '''    // [unit->REQ-SHELL-OWNER-ONLINE-RESTORE]
    // [unit->REQ-HAZARD-RESTART-STRANDS-PERSISTENT-SHELLS]
    // [unit->REQ-SHELL-PERSISTENT-BOOT-RESTORE]
    // THE SEAM releases#78 LEFT: the sweep runs while the owner endpoint is still
    // down (daemons boot before endpoints do), spends its one generation restoring
    // NOTHING, and the owner comes online afterwards with no rest edge anywhere in
    // the setup -- so neither the sweep nor the ADR-0048 cascade can cover it.
    //
    // Three arms in ONE fixture, and each is the answer to a case the others get
    // wrong (the BAROMETER W2 warning this milestone is held to: a single-instance
    // fixture cannot tell a correct trigger from one that restores everything):
    //
    // - the restart casualty of the now-online owner is RESTORED -- the row the
    //   ticket is about;
    // - a sibling force-killed THIS boot is NOT -- the refusal arm carried verbatim
    //   from the sweep, which an implementation that simply relaunches everything
    //   for an online owner would fail;
    // - a SECOND pass with the owner still online restores a freshly stranded
    //   instance NOT AT ALL -- the trigger is an EDGE, not the level "the owner is
    //   online". That is the arm no success case can express: a successful restore
    //   restamps itself out of eligibility, so only an instance that never had its
    //   edge can tell a level trigger from an edge one.
    #[test]
    fn an_owner_coming_online_after_a_spent_sweep_restores_only_its_restart_casualty() {
        let tmp = tempfile::tempdir().unwrap();
        let owlery = tmp.path();
        let boot = spt_store::proc::boot_instant_ms().expect("a boot instant on this platform");

        // The owner is OFFLINE at sweep time -- the ordering that strands the shell.
        let owner_dir = owlery.join("ling");
        std::fs::create_dir_all(&owner_dir).unwrap();
        let offline_owner = "{\\"status\\":\\"offline\\"}";
        std::fs::write(owner_dir.join("info.json"), offline_owner).unwrap();

        #[cfg(windows)]
        let noop = "cmd /c exit 0";
        #[cfg(unix)]
        let noop = "true";
        let adapters_dir = tmp.path().join("adapters");
        let src = tmp.path().join("srcs").join("mock-owner-online");
        std::fs::create_dir_all(&src).unwrap();
        std::fs::write(
            src.join("manifest.toml"),
            format!(
                "[adapter]\\nname = \\"mock-owner-online\\"\\nkind = \\"shell\\"\\nversion = \\"1\\"\\n\\
                 min_spt_core_version = \\"0\\"\\n\\n[shell]\\nspawn = \\'{noop}\\'\\n\\
                 persistent = true\\n"
            ),
        )
        .unwrap();
        spt_runtime::registry::register(&adapters_dir, &src, 1).unwrap();
        let registered = spt_runtime::registry::registered(&adapters_dir);

        // The restart shape, built with NO suspend/rest path anywhere: recorded
        // ONLINE over a dead pid, with a parked launch stamp.
        let mk = |launched_ms: u64| -> String {
            let id = spawn_record(owlery, "ling", "mock-owner-online", None).unwrap();
            let perch = spt_store::perch::resolve_shell_perch_path_in(owlery, "ling", &id);
            let mut i = shellinfo::read_shell_info(&perch).unwrap();
            i.status = SHELL_STATUS_ONLINE.to_string();
            shellinfo::write_shell_info(&perch, &i).unwrap();
            std::fs::write(perch.join(shellinfo::SHELL_PID_FILE), "2000000000").unwrap();
            let launch = shellinfo::ShellLaunch {
                pid_started_at: Some(1),
                launched_ms,
            };
            std::fs::write(
                perch.join(shellinfo::SHELL_LAUNCH_FILE),
                serde_json::to_string(&launch).unwrap(),
            )
            .unwrap();
            id
        };
        let stranded = mk(boot.saturating_sub(600_000)); // 10 min BEFORE boot
        let force_killed = mk(boot + 120_000); // 2 min AFTER boot

        // The daemon's own order: seed the edge, then spend the sweep.
        let mut edge = OwnerOnlineEdge::new();
        edge.seed(owlery);
        let swept = restore_persistent_shells_at_boot(owlery, &registered, &adapters_dir);
        assert!(
            swept.is_empty(),
            "the sweep is SPENT and restored nothing: its owner-online conjunct \\
             refused, correctly, and it never runs again this generation: {swept:?}"
        );

        // The owner comes online. No rest edge is emitted by bringup — this write is
        // the endpoint record changing, nothing more.
        std::fs::write(owner_dir.join("info.json"), "{}").unwrap();

        let set = Arc::new(WakeSet::new());
        let restored =
            restore_persistent_shells_on_owner_online(owlery, &registered, &adapters_dir, &mut edge, &set);

        assert!(
            restored.contains(&format!("ling/{stranded}")),
            "the owner came online after the sweep, so THIS is the trigger that owes \\
             the restore — the seam releases#228 reported: {restored:?}"
        );
        assert!(
            !restored.contains(&format!("ling/{force_killed}")),
            "an instance launched THIS boot is a force-kill, refused by the same \\
             discriminant the sweep uses — re-evaluating can only refuse MORE: \\
             {restored:?}"
        );

        // ...and the trigger is an EDGE: the owner stays online, a NEW casualty is
        // stranded, and the next pass does nothing. A level trigger would take it.
        let late = mk(boot.saturating_sub(600_000));
        let again =
            restore_persistent_shells_on_owner_online(owlery, &registered, &adapters_dir, &mut edge, &set);
        assert!(
            again.is_empty(),
            "no NEW owner-online transition ⇒ no restore attempt at all; this is what \\
             bounds a failing launch to one attempt per owner-online event instead of \\
             one every five seconds forever (ling/{late} stayed down): {again:?}"
        );
    }

'''

s = s.replace(anchor, test + anchor, 1)
io.open(p, "w", encoding="utf-8", newline="").write(s)
print("OK")
