Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(wiki): keep in-flight atomic staging files out of the commit
Rudy Mizrahi Celeklie4f7a00afc07
diff --git a/crates/ai-memory-wiki/src/git.rs b/crates/ai-memory-wiki/src/git.rsindex ff1419af..2dd4b308 100644--- a/crates/ai-memory-wiki/src/git.rs+++ b/crates/ai-memory-wiki/src/git.rs@@ -922,7 +922,7 @@ fn stage_working_tree(root: &Path, index: &mut git2::Index) -> Result<Vec<PathBu["*"].iter(),IndexAddOption::DEFAULT,Some(&mut |path: &Path, _: &[u8]| {- if is_ledger_file(root, path) {+ if is_ledger_file(root, path) || is_in_flight_write(path) {return 1;}touched.push(path.to_path_buf());@@ -932,6 +932,23 @@ fn stage_working_tree(root: &Path, index: &mut git2::Index) -> Result<Vec<PathBuOk(touched)}+/// True when `rel`'s basename belongs to a write still in flight: a+/// `.ai-memory-tmp.` staging file or a `.ai-memory-delete.` parked page.+///+/// These exist only between a write starting and its rename, so committing one+/// records a page body that may never be installed. `Wiki::apply_batch` holds+/// one staging file per page across an `await`, and `commit_all` takes only the+/// commit lock, never the mutation lock, so a checkpoint can land in that+/// window. The watcher already ignores the same prefix+/// ([`crate::watcher`]'s `is_tempfile`).+fn is_in_flight_write(rel: &Path) -> bool {+ rel.file_name()+ .and_then(|name| name.to_str())+ .is_some_and(|name| {+ name.starts_with(".ai-memory-tmp.") || name.starts_with(".ai-memory-delete.")+ })+}+/// True when `rel` (relative to the wiki root) is a raw hook event ledger:/// a `log.md` / `log-YYYY-MM.md` file whose content also opens with a hook log/// entry.@@ -1009,7 +1026,9 @@ fn stage_paths(index.add_all([spec.as_str()].iter(),IndexAddOption::DISABLE_PATHSPEC_MATCH,- Some(&mut |path: &Path, _: &[u8]| i32::from(is_ledger_file(root, path))),+ Some(&mut |path: &Path, _: &[u8]| {+ i32::from(is_ledger_file(root, path) || is_in_flight_write(path))+ }),)?;untrack_ledgers(root, index, Some(&spec))?;} else if abs.is_file() {@@ -1946,6 +1965,55 @@ mod tests {(tmp, root, adapter)}+ /// `apply_batch` keeps one `.ai-memory-tmp.*` staging file per page on disk+ /// across an await, and `commit_all` takes only the commit lock — so a+ /// sweep landing in that window must not capture a write in flight.+ #[test]+ fn a_sweep_never_stages_an_in_flight_atomic_write() {+ let (_tmp, root, adapter) = committed(&[("ws/proj/a.md", "a")]);+ write(&root, "ws/proj/b.md", "b");+ write(+ &root,+ "ws/proj/.ai-memory-tmp.aB3x9Q",+ "a body still being written",+ );+ write(+ &root,+ "ws/proj/.ai-memory-delete.b.md",+ "a page parked for deletion",+ );++ assert!(+ adapter+ .commit_all("sweep with writes in flight")+ .unwrap()+ .is_some()+ );++ assert_eq!(head_blob(&adapter, "ws/proj/b.md").as_deref(), Some("b"));+ assert_eq!(head_blob(&adapter, "ws/proj/.ai-memory-tmp.aB3x9Q"), None);+ assert_eq!(head_blob(&adapter, "ws/proj/.ai-memory-delete.b.md"), None);+ }++ /// The same for a path-scoped commit, whose directory pathspec reaches+ /// `stage_paths`' own `add_all` rather than the sweep's.+ #[test]+ fn a_path_scoped_commit_never_stages_an_in_flight_atomic_write() {+ let (_tmp, root, adapter) = committed(&[("ws/proj/a.md", "a")]);+ write(&root, "ws/proj/c.md", "c");+ write(+ &root,+ "ws/proj/.ai-memory-tmp.cD4y0R",+ "a body still being written",+ );+ adapter.mark_written(&root.join("ws/proj"));++ adapter.commit_all("scoped with a write in flight").unwrap();++ assert_eq!(head_blob(&adapter, "ws/proj/c.md").as_deref(), Some("c"));+ assert_eq!(head_blob(&adapter, "ws/proj/.ai-memory-tmp.cD4y0R"), None);+ }+#[test]fn a_commit_stages_the_reported_paths_only() {let (_tmp, root, adapter) = committed(&[("page-7.md", "page 7"), ("page-8.md", "page 8")]);