import ast
d = r'C:/Users/decid/AppData/Local/Temp/claude/C--Users-decid-Documents-projects-spt-core/29212dec-e8e9-45d8-865e-c24e5432a269/scratchpad/'
s = open(d + 'mutate4.py', 'rb').read().decode('utf-8')
nl = '\r\n' if '\r\n' in s else '\n'

def swap(old, new):
    global s
    old = old.replace('\n', nl); new = new.replace('\n', nl)
    assert s.count(old) == 1, (old[:70], s.count(old))
    s = s.replace(old, new)

# M3-read: the tail now lives in read_records_counted_at
swap(r'''     b"    crate::jsonltail::read_at::<WarningRecord>(&records_file_at(perch_path), RECORD_KEEP)",
     b"    crate::jsonltail::read_at::<WarningRecord>(&records_file_at(perch_path), 64)",''',
     r'''     b"    if rows.len() > RECORD_KEEP {\r\n        rows.drain(..rows.len() - RECORD_KEEP);",
     b"    if rows.len() > 64 {\r\n        rows.drain(..rows.len() - 64);",''')
# R2-2: the well-formed filter now lives in read_records_counted_at
swap(r'''     b"        .filter(WarningRecord::is_well_formed)\r\n",
     b"",''',
     r'''     b"rows.into_iter().filter(WarningRecord::is_well_formed).collect();",
     b"rows.into_iter().collect();",''')
# 2c-1: re-anchor on the reporting rewrite
swap(r'''     b"    if raw_lines != records.len() {\r\n        let _ = crate::jsonltail::rewrite_at(&file, &records);",
     b"    let raw_lines = std::fs::read_to_string(&file).map(|s| s.lines().filter(|l| !l.trim().is_empty()).count()).unwrap_or(0);\r\n    if raw_lines != records.len() {\r\n        let _ = crate::jsonltail::rewrite_at(&file, &records);",''',
     r'''     b"    if raw_lines != records.len() {\r\n        if let Err(e) = crate::jsonltail::rewrite_at(&file, &records) {",
     b"    let raw_lines = std::fs::read_to_string(&file).map(|s| s.lines().filter(|l| !l.trim().is_empty()).count()).unwrap_or(0);\r\n    if raw_lines != records.len() {\r\n        if let Err(e) = crate::jsonltail::rewrite_at(&file, &records) {",''')
# 2c-2: `?` on the (first) rewrite
swap(r'''     b"        let _ = crate::jsonltail::rewrite_at(&file, &records);",
     b"        crate::jsonltail::rewrite_at(&file, &records)?;",''',
     r'''     b"        if let Err(e) = crate::jsonltail::rewrite_at(&file, &records) {\r\n            failed.push(e);\r\n        }\r\n    }\r\n    let pending",
     b"        crate::jsonltail::rewrite_at(&file, &records)?;\r\n    }\r\n    let pending",''')
# 2d-2: drop the report
add = r'''    ("2d-2 a failed rewrite is not reported", TW,
     b"        if let Err(e) = crate::jsonltail::rewrite_at(&file, &records) {\r\n            failed.push(e);\r\n        }\r\n    }\r\n    let pending",
     b"        if let Err(e) = crate::jsonltail::rewrite_at(&file, &records) {\r\n            let _ = e;\r\n        }\r\n    }\r\n    let pending",
     "-p spt-store --lib", "a_failed_rewrite_still_records_the_warning"),
]
'''.replace('\n', nl)
marker = ']' + nl + 'env ='
assert s.count(marker) == 1
s = s.replace(marker, add + 'env =', 1)
ast.parse(s)
open(d + 'mutate5.py', 'wb').write(s.encode('utf-8'))
print('mutate5 ok')
