fix(store): a stopped heal leaves no copy and no rebuild, and the bound names the heal

verify_and_heal copied the database aside before looking at the stop, so a
verification or an open that was stopped right after finding a drift left a
.mismatch copy behind. The look now comes first (heal_drift), before anything
is done: a stopped heal leaves the database and the folder exactly as they
were. The rebuild and its verification stay unstoppable once begun, since
stopped half way the live ledger would be part replayed.

INVARIANT 10's close bound said that heal was one pass over the journal and
named no term for it. It is about six (three in the rebuild, three in the
verification) plus the copy and one hash of each stored file, so it is now the
fourth named exception to the bound, with that cost, and 'an open stops
anywhere' says where it cannot.

Test: a stop before the heal leaves no copy and no rebuild; the same drift
heals when not stopped. It fails with the look moved back after the copy.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-20 20:20:24 -07:00
1 parent 30e5fcf6da
commit 49ae7c0d13
4 files changed
+128 -30

No files matched your search

+66
View File
@@ -883,6 +883,72 @@ fn vendor_1099_tags_replay_and_project() {
}
}
/// A stop asked before a heal begins leaves nothing behind (cancel INVARIANT
/// 4, store.rs `heal_drift`): no `.mismatch` copy of the database and no
/// rebuild, the database exactly as it was (still drifted), and the stop is
/// the answer. Healed with no stop, the same drift heals.
#[test]
fn a_stop_before_a_heal_leaves_no_copy_and_no_rebuild() {
let dir = test_dir("heal-stopped");
let mut store = build_books(&dir);
let e = store
.ledger()
.posted_entries()
.next()
.map(|e| e.id())
.unwrap();
store
.attach_receipt(Some(e), "r.png", "image/png", b"receipt", None)
.unwrap();
drop(store);
let db = dir.join(ledger_store::db_file());
let conn = rusqlite::Connection::open(&db).unwrap();
conn.execute("UPDATE attachments SET entry_id = NULL", [])
.unwrap();
drop(conn);
let mut store = Store::open(&dir).unwrap();
let Err(StoreError::VerifyMismatch(why)) = store.verify() else {
panic!("the drift wasn't found");
};
let files = |dir: &std::path::Path| {
let mut names: Vec<String> = std::fs::read_dir(dir)
.unwrap()
.filter_map(|d| d.ok())
.map(|d| d.file_name().to_string_lossy().into_owned())
.collect();
names.sort();
names
};
let (names, db_bytes) = (files(&dir), std::fs::read(&db).unwrap());
{
// The stop arrives after the drift was found, before the heal.
let _stop = ledger_store::cancel::scope(std::sync::Arc::new(
std::sync::atomic::AtomicBool::new(true),
));
assert!(matches!(
store.heal_drift(why.clone()),
Err(StoreError::Cancelled)
));
}
assert_eq!(files(&dir), names, "a stopped heal left a copy behind");
assert_eq!(
std::fs::read(&db).unwrap(),
db_bytes,
"a stopped heal rebuilt the database"
);
assert!(
matches!(store.verify(), Err(StoreError::VerifyMismatch(_))),
"still drifted: nothing was rebuilt"
);
// Not stopped, the same drift heals, keeping its copy.
let report = store.verify_and_heal().unwrap();
assert!(report.healed.is_some());
assert!(
files(&dir).iter().any(|n| n.contains(".mismatch.")),
"the divergent database is kept for forensics"
);
}
#[test]
fn the_whole_projection_is_checked_and_healed() {
let dir = test_dir("heal");