From 5f97aad6a2caf84d54b216ffc4c710999f90ce63 Mon Sep 17 00:00:00 2001 From: Weksa Date: Tue, 10 Nov 2026 07:49:15 +0000 Subject: [PATCH] run: create removes the inventory copy when a later step fails Co-Authored-By: Claude Sonnet 5.5 --- crates/weksa/src/run.rs | 34 ++++++++++++++++++++++----------- crates/weksa/tests/golf_loop.rs | 29 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/crates/weksa/src/run.rs b/crates/weksa/src/run.rs index 452846b..1beb442 100644 --- a/crates/weksa/src/run.rs +++ b/crates/weksa/src/run.rs @@ -428,7 +428,11 @@ fn copy_new(from: &Path, to: &Path) -> Result<(), RunError> { .create_new(true) .open(to) .map_err(io)?; - std::io::copy(&mut source, &mut dest).map_err(io)?; + if let Err(e) = std::io::copy(&mut source, &mut dest) { + // The file is ours (create_new made it): a half copy must not block a retry. + let _ = std::fs::remove_file(to); + return Err(io(e)); + } Ok(()) } @@ -472,16 +476,24 @@ impl RunStores { // The run works on its own copy; the caller's store is never opened again. let head = root.join(INVENTORY_FILE); copy_new(Path::new(&record.inventory), &head)?; - let record = RunRecord { - corpus_digest: file_digest(&record.corpus).ok_or(RunError::Store)?, - entity_digest: file_digest(&record.entities).ok_or(RunError::Store)?, - inventory_digest: file_digest_at(&head).ok_or(RunError::Store)?, - ..record - }; - cache - .put(format!("run:{label}"), &record) - .map_err(|_| RunError::Store)?; - Self::with(root, cache, record) + // The copy is ours from here: any later failure takes it away again, or + // it would refuse every retry. + let pinned = (|| { + let record = RunRecord { + corpus_digest: file_digest(&record.corpus).ok_or(RunError::Store)?, + entity_digest: file_digest(&record.entities).ok_or(RunError::Store)?, + inventory_digest: file_digest_at(&head).ok_or(RunError::Store)?, + ..record + }; + cache + .put(format!("run:{label}"), &record) + .map_err(|_| RunError::Store)?; + Self::with(root, cache, record) + })(); + if pinned.is_err() { + let _ = std::fs::remove_file(&head); + } + pinned } /// Reopens the run at `root` from its record, refusing when a pinned store diff --git a/crates/weksa/tests/golf_loop.rs b/crates/weksa/tests/golf_loop.rs index 5257d1d..e2da558 100644 --- a/crates/weksa/tests/golf_loop.rs +++ b/crates/weksa/tests/golf_loop.rs @@ -257,6 +257,35 @@ fn create_refuses_a_root_that_holds_an_inventory_and_never_truncates_it() { assert_eq!(std::fs::read(&dao.inventory).unwrap(), bytes); } +#[test] +fn a_create_that_fails_leaves_no_inventory_and_the_retry_succeeds() { + let dao = dao(); + let root = dao.root.join("retry"); + std::fs::create_dir_all(&root).unwrap(); + let create = |inventory: &Path, entities: &Path| { + RunStores::create( + &root, + "x", + RunPaths { + corpus: &dao.corpus, + inventory, + entities, + }, + "fake-model", + "bag", + ) + }; + // A directory opens for reading and fails mid-copy, after the destination exists. + let dir = dao.root.join("a-directory"); + std::fs::create_dir_all(&dir).unwrap(); + assert!(create(&dir, &dao.entities).is_err()); + assert!(!root.join("inventory.cc").exists()); + // A directory as the entities store fails after the inventory was copied. + assert!(create(&dao.inventory, &dir).is_err()); + assert!(!root.join("inventory.cc").exists()); + assert!(create(&dao.inventory, &dao.entities).is_ok()); +} + /// `weksa.loop_run` as it was before the run kept its own inventory copy: keys /// 0 to 10. #[derive(Clone, cultcache_rs::DatabaseEntry)]