Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(wiki): keep the directory-event orphan guard rows-only too (#643)

Samir Hanna Verzac96c0dfcb0d0
diff --git a/crates/ai-memory-wiki/src/watcher.rs b/crates/ai-memory-wiki/src/watcher.rs
index 57a27cfb..086c63ee 100644
--- a/crates/ai-memory-wiki/src/watcher.rs
+++ b/crates/ai-memory-wiki/src/watcher.rs
@@ -259,7 +259,12 @@ async fn reindex_project_dir(
// behaviour that pass was about — quieter here only because it needs an
// event rather than firing every 30s. Checking once per directory also
// saves walking a tree whose every page is going to fail scope resolution.
- if let Err(e) = wiki.ensure_project_workspace(ws, proj).await {
+ //
+ // Rows only, for the same reason `reconcile` uses this form: the guard
+ // runs before `reindex_page` takes the mutation lock, so writing a
+ // `_meta.md` here could land it in a directory a concurrent project move
+ // is renaming away.
+ if let Err(e) = wiki.ensure_project_scope_rows(ws, proj).await {
debug!(
workspace = %ws,
project = %proj,
@@ -886,6 +891,45 @@ mod tests {
);
}
+ /// The directory-event orphan guard (#616 added it beside `reconcile`'s)
+ /// is the watcher's second caller that runs BEFORE `reindex_page` takes
+ /// the mutation lock, so it must stay rows-only for the same reason: a
+ /// `_meta.md` written from an unguarded path could land in a directory a
+ /// concurrent project move is renaming away. Pages found in the walk are
+ /// a different matter — `reindex_page` writes the manifest under the
+ /// guard, which is safe.
+ #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+ async fn directory_events_do_not_write_scope_manifests() {
+ let tmp = TempDir::new().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let ws = store.writer.get_or_create_workspace("acme").await.unwrap();
+ let proj = store
+ .writer
+ .get_or_create_project(ws, "webapp", None)
+ .await
+ .unwrap();
+ // The reader is what lets a manifest be written at all; without it
+ // attached this would pass for the wrong reason.
+ let wiki = Wiki::new(tmp.path(), store.writer.clone())
+ .unwrap()
+ .with_store_reader(store.reader.clone());
+
+ let ws_dir = tmp.path().join("wiki").join(ws.to_string());
+ let proj_dir = ws_dir.join(proj.to_string());
+ std::fs::create_dir_all(&proj_dir).unwrap();
+
+ assert!(
+ reindex_project_dir(&wiki, ws, proj, proj_dir.clone()).await,
+ "a scope the store knows is not an orphan"
+ );
+
+ assert!(
+ !ws_dir.join("_meta.md").exists(),
+ "the unguarded directory-event pre-check must not write files"
+ );
+ assert!(!proj_dir.join("_meta.md").exists());
+ }
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn ignores_own_atomic_tempfiles() {
// Quick unit test: tempfile prefix detection.
diff --git a/crates/ai-memory-wiki/src/wiki.rs b/crates/ai-memory-wiki/src/wiki.rs
index a7f93dec..ca6eed88 100644
--- a/crates/ai-memory-wiki/src/wiki.rs
+++ b/crates/ai-memory-wiki/src/wiki.rs
@@ -501,11 +501,12 @@ impl Wiki {
}
/// Ensure the store rows for a scope exist, without touching the wiki
- /// tree. Only the watcher's reconcile pass wants this: it walks
- /// directories that already exist (so their manifests were written when
- /// their first page landed, or by the startup backfill), and it runs
- /// outside the mutation guard — creating files there could drop a
- /// manifest into a directory a concurrent project move is renaming away.
+ /// tree. The watcher's two orphan guards want this — the reconcile pass
+ /// and the directory-event path. Both run before `reindex_page` takes
+ /// the mutation guard, so creating a file there could drop a manifest
+ /// into a directory a concurrent project move is renaming away, and both
+ /// only ever see directories that already exist, whose manifests were
+ /// written with their first page or by the startup backfill.
pub(crate) async fn ensure_project_scope_rows(
&self,
workspace_id: WorkspaceId,