import sys
SCR = r'C:/Users/decid/AppData/Local/Temp/claude/C--Users-decid-Documents-projects-spt-core/646ca2c9-443e-4993-8707-ba17cf7e661f/scratchpad/'
p = 'crates/spt-daemon/src/resting.rs'
s = open(p, encoding='utf-8', newline='').read()
crlf = '\r\n' in s
s = s.replace('\r\n', '\n')
head = open(SCR + 'w1_head.rs', encoding='utf-8').read()
b = s.index('/// The wake-edge freshness-pull marker file')
s = head + '\n' + s[b:]
a = s.index("/// Apply one event against a perch's durable resting record")
b = s.index("/// Arm the endpoint's echo-gate sentinel")
new_apply = open(SCR + 'w1_apply.rs', encoding='utf-8').read()
s = s[:a] + new_apply + '\n' + s[b:]


def rep(old, new):
    global s
    assert s.count(old) == 1, old
    s = s.replace(old, new)


rep('''pub fn daemon_rest_event(
    id: &str,
    event: RestEvent,
    node_auto_suspend_ms: Option<u64>,
) -> Result<Option<EdgeReport>, String> {
    daemon_rest_event_with_liveness(id, event, node_auto_suspend_ms, None)
}''', '''pub fn daemon_rest_event(
    id: &str,
    event: RestEvent,
    node_auto_suspend: Option<AutoSuspend>,
) -> Result<Option<EdgeReport>, String> {
    daemon_rest_event_with_liveness(id, event, node_auto_suspend, None)
}''')
rep('''    node_auto_suspend_ms: Option<u64>,
    alive_hint: Option<bool>,
) -> Result<Option<EdgeReport>, String> {
    let perch_path = perch::resolve_perch_path(id, ParentHint::Infer);
    let report = apply_event_with_liveness(
        &perch_path,
        event,
        node_auto_suspend_ms,
        now_ms(),''', '''    node_auto_suspend: Option<AutoSuspend>,
    alive_hint: Option<bool>,
) -> Result<Option<EdgeReport>, String> {
    let perch_path = perch::resolve_perch_path(id, ParentHint::Infer);
    let report = apply_event_with_liveness(
        &perch_path,
        event,
        node_auto_suspend,
        sibling_view(id),
        now_ms(),''')
rep('''    /// The endpoint leg of the auto-suspend knob chain.
    pub auto_suspend_after_ms: Option<u64>,
}''', '''    /// The endpoint leg of the auto-suspend knob chain.
    pub auto_suspend: Option<AutoSuspend>,
    /// The activation counter this instance last took.
    pub activation: u64,
}''')
rep('''        auto_suspend_after_ms: rec.auto_suspend_after_ms,
    })''', '''        auto_suspend: rec.auto_suspend,
        activation: rec.activation,
    })''')
if crlf:
    s = s.replace('\n', '\r\n')
open(p, 'w', encoding='utf-8', newline='').write(s)
print('ok')
