p='crates/spt/src/cli.rs'
b=open(p,'rb').read(); crlf=b'\r\n' in b; s=b.decode().replace('\r\n','\n')
def rep(a,n):
    global s
    assert s.count(a)==1,a
    s=s.replace(a,n)
rep('''        | Ok(o @ O::AlreadyApplied { .. }) => o,
        // The fail-closed refusals — nothing swapped, do NOT restart.''','''        | Ok(o @ O::AlreadyApplied { .. }) => o,
        // The fail-closed refusals — nothing swapped, do NOT restart.''')
rep('''    let applied = match spt_daemon::apply_staged_daemonless(cache, keys, exe) {
        Ok(O::NoUpdate) => {
            println!("NO_UPDATE");
            return 0;
        }''','''    let applied = match spt_daemon::apply_staged_daemonless(cache, keys, exe) {
        Ok(O::NoUpdate) => {
            println!("NO_UPDATE");
            return 0;
        }
        // [impl->REQ-NOW-SIGNAL-UPDATE-DIVULGE] the CLI writer, finish leg.
        Ok(o @ O::AppliedDaemonless { .. }) | Ok(o @ O::AppliedPending { .. }) => {
            record_core_apply(cache, &o);
            o
        }''')
rep('''    match outcome {
        Ok(o) => {
            let code = report_apply_outcome(o);
            // Docs land only AFTER a successful binary apply, and their''','''    match outcome {
        Ok(o) => {
            // [impl->REQ-NOW-SIGNAL-UPDATE-DIVULGE] the CLI writer, in-place leg.
            record_core_apply(&cache, &o);
            let code = report_apply_outcome(o);
            // Docs land only AFTER a successful binary apply, and their''')
rep('''fn cmd_update_apply(finish: bool) -> i32 {''','''/// The CLI writer of the divulge's core apply record (releases#337): a swap
/// this process landed moves core FROM the version this process IS (the
/// pre-swap image, still running the verb) TO the staged set's signed product
/// version. The broker's promotion writes the same record through the same
/// [`spt_daemon::core_subject_apply`], so a core moved by either reads
/// identically; the CLI write is what tells a session when no promotion follows
/// (a daemon that stays down). Pure; `None` for every outcome that swapped
/// nothing.
// [impl->REQ-NOW-SIGNAL-UPDATE-DIVULGE]
fn core_apply_record(
    outcome: &spt_daemon::ApplyStagedOutcome,
    running: &str,
    at_ms: u64,
    docs_base: Option<&str>,
) -> Option<spt_daemon::SubjectApply> {
    use spt_daemon::ApplyStagedOutcome as O;
    match outcome {
        O::AppliedPending { product_version, .. } | O::AppliedDaemonless { product_version, .. } => {
            spt_daemon::core_subject_apply(running, product_version, at_ms, docs_base)
        }
        _ => None,
    }
}

/// Write [`core_apply_record`]'s move, best-effort: an unrecorded move degrades
/// the divulge row to its unmoved shape, never the apply.
// [impl->REQ-NOW-SIGNAL-UPDATE-DIVULGE]
fn record_core_apply(cache: &spt_daemon::ReleaseCache, outcome: &spt_daemon::ApplyStagedOutcome) {
    let docs_base = spt_daemon::docshost::live_docs_url();
    if let Some(apply) =
        core_apply_record(outcome, env!("CARGO_PKG_VERSION"), now_ms(), docs_base.as_deref())
    {
        let _ = cache.record_subject_apply(spt_daemon::SUBJECT_CORE, &apply);
    }
}

fn cmd_update_apply(finish: bool) -> i32 {''')
if crlf: s=s.replace('\n','\r\n')
open(p,'wb').write(s.encode())
