run: create removes the inventory copy when a later step fails

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Weksa 2026-11-10 07:49:15 +00:00
commit 5f97aad6a2
2 changed files with 52 additions and 11 deletions

View file

@ -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

View file

@ -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)]