"""W10 negative control: re-add the pre-fix AND at the hook drain.
usage: mutate_w10.py apply|restore (restore asserts sha256 byte identity)"""
import hashlib, os, shutil, sys
F = 'crates/spt/src/api/delivery.rs'
BK = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'w10-delivery-backup.rs')
SHA = BK + '.sha'
ANCHOR = '''    // [impl->REQ-DELIVERY-WINDOW-IS-ACTIVITY-AXIS-ONLY]
    // ADR-0028 W2: the hook poll is the BUSY-window surface — it must SUPPRESS'''
PREFIX = '''    // NEGATIVE CONTROL: the pre-fix AND, restored verbatim in effect.
    let include_deferred = include_deferred
        && !matches!(
            spt_daemon::resting::read_rest(&perch_path).map(|r| r.state),
            Some(spt_daemon::resting::RestState::Dormant | spt_daemon::resting::RestState::Suspended)
        );
'''
sha = lambda p: hashlib.sha256(open(p, 'rb').read()).hexdigest()
if sys.argv[1] == 'apply':
    shutil.copy2(F, BK); open(SHA, 'w').write(sha(F))
    s = open(F, encoding='utf-8').read(); assert s.count(ANCHOR) == 1
    open(F, 'w', encoding='utf-8', newline='\n').write(s.replace(ANCHOR, PREFIX + ANCHOR)); print('mutated')
else:
    shutil.copy2(BK, F); assert sha(F) == open(SHA).read().strip(); print('restored byte-identical')
