Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(serve): heal catch-all repo_path rows on startup
Thiago Silvabc2e4817c191
diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rsindex b13720f5..eb5a4dfc 100644--- a/crates/ai-memory-cli/src/commands/serve.rs+++ b/crates/ai-memory-cli/src/commands/serve.rs@@ -78,6 +78,21 @@ pub async fn run(config: &Config, args: ServeArgs) -> Result<()> {let store = Store::open(&config.data_dir).with_context(|| format!("opening store at {}", config.data_dir.display()))?;+ // One-shot legacy heal (issue #103): NULL out any project repo_path that+ // is a known prefix-match catch-all ($HOME or filesystem root) so existing+ // broken installs self-correct on upgrade. Uses the same $HOME source as+ // the router's match-time guard so heal and guard agree on its meaning.+ let healed = store+ .writer+ .heal_catch_all_repo_paths(std::env::var("HOME").ok())+ .await?;+ if healed > 0 {+ tracing::info!(+ healed,+ "healed catch-all project repo_path rows ($HOME / filesystem root)"+ );+ }+// Run any outstanding wiki-structure migrations before the watcher starts// so file moves and renames are never raced by the reconciler.let wiki_root = config.data_dir.join("wiki");diff --git a/crates/ai-memory-store/src/ops.rs b/crates/ai-memory-store/src/ops.rsindex 46cb74fa..9fe9a5ef 100644--- a/crates/ai-memory-store/src/ops.rs+++ b/crates/ai-memory-store/src/ops.rs@@ -145,6 +145,21 @@ pub fn get_or_create_project(Ok(id)}+/// NULL out `repo_path` values that act as prefix-match catch-alls: the+/// operator's home directory (`home`, when provided) and filesystem root+/// (`/`). Such rows would prefix-match every project nested beneath them+/// (issue #103). Idempotent; returns the number of rows healed. Targets+/// ONLY those two sentinels -- get_or_create_project never re-populates a+/// NULLed repo_path, so a broader heal would be destructive.+pub fn heal_catch_all_repo_paths(conn: &mut Connection, home: Option<&str>) -> StoreResult<u64> {+ let n = conn.execute(+ "UPDATE projects SET repo_path = NULL \+ WHERE repo_path = '/' OR (?1 IS NOT NULL AND repo_path = ?1)",+ params![home],+ )?;+ Ok(u64::try_from(n).unwrap_or(0))+}+fn scheduler_state_table_exists(conn: &Connection) -> StoreResult<bool> {Ok(conn.query_row(@@ -2869,4 +2884,52 @@ mod tests {);assert_eq!(count("\"ui refresh\""), 1, "sub-token phrase must match");}++ /// Issue #103 legacy heal: a one-shot startup pass NULLs `repo_path`+ /// rows equal to the two prefix-match catch-alls ($HOME and `/`) so+ /// existing broken installs self-correct on upgrade. Nested and NULL+ /// rows are left untouched -- only the two sentinels are healed.+ #[test]+ fn heal_catch_all_repo_paths_nulls_home_and_root_only() {+ let (_tmp, mut conn, ws, _proj) = fresh_db();++ let read_repo_path = |conn: &Connection, id: &ProjectId| -> Option<String> {+ conn.query_row(+ "SELECT repo_path FROM projects WHERE id = ?1",+ params![id.as_bytes()],+ |row| row.get(0),+ )+ .unwrap()+ };++ let home = get_or_create_project(&mut conn, &ws, "home", Some("/home/tester")).unwrap();+ let root = get_or_create_project(&mut conn, &ws, "root", Some("/")).unwrap();+ let nested =+ get_or_create_project(&mut conn, &ws, "app", Some("/home/tester/projects/app"))+ .unwrap();+ let none = get_or_create_project(&mut conn, &ws, "none", None).unwrap();++ let healed = heal_catch_all_repo_paths(&mut conn, Some("/home/tester")).unwrap();+ assert_eq!(healed, 2, "only the $HOME and `/` rows should be healed");++ assert_eq!(+ read_repo_path(&conn, &home),+ None,+ "$HOME row must be NULLed"+ );+ assert_eq!(read_repo_path(&conn, &root), None, "`/` row must be NULLed");+ assert_eq!(+ read_repo_path(&conn, &nested),+ Some("/home/tester/projects/app".to_string()),+ "nested project must be preserved"+ );+ assert_eq!(read_repo_path(&conn, &none), None, "NULL row stays NULL");++ // Idempotent: a second pass over a healed DB changes nothing.+ assert_eq!(+ heal_catch_all_repo_paths(&mut conn, Some("/home/tester")).unwrap(),+ 0,+ "re-running the heal must be a no-op"+ );+ }}diff --git a/crates/ai-memory-store/src/writer.rs b/crates/ai-memory-store/src/writer.rsindex 90c988d7..24276138 100644--- a/crates/ai-memory-store/src/writer.rs+++ b/crates/ai-memory-store/src/writer.rs@@ -116,6 +116,10 @@ pub(crate) enum WriteCmd {hard_delete_after_days: i64,reply: oneshot::Sender<StoreResult<usize>>,},+ HealCatchAllRepoPaths {+ home: Option<String>,+ reply: oneshot::Sender<StoreResult<u64>>,+ },StoreEmbedding {page_id: PageId,vector_bytes: Vec<u8>,@@ -559,6 +563,19 @@ impl WriterHandle {rx.await.map_err(|_| StoreError::WriterClosed)?}+ /// NULL out catch-all project `repo_path` rows ($HOME and filesystem+ /// root) so existing installs self-heal on upgrade. Idempotent;+ /// returns the number of rows healed.+ ///+ /// # Errors+ /// Returns [`StoreError::WriterClosed`] or propagates SQL errors.+ pub async fn heal_catch_all_repo_paths(&self, home: Option<String>) -> StoreResult<u64> {+ let (tx, rx) = oneshot::channel();+ self.send(WriteCmd::HealCatchAllRepoPaths { home, reply: tx })+ .await?;+ rx.await.map_err(|_| StoreError::WriterClosed)?+ }+/// Delete a project and all its data in one atomic transaction.////// ON DELETE CASCADE propagates the delete through pages, sessions,@@ -1064,6 +1081,10 @@ fn worker_loop(mut conn: Connection, mut rx: mpsc::Receiver<WriteCmd>) {let result = ops::hard_delete_decayed_pages(&mut conn, hard_delete_after_days);send_or_warn(reply, result, "hard_delete_decayed_pages");}+ WriteCmd::HealCatchAllRepoPaths { home, reply } => {+ let result = ops::heal_catch_all_repo_paths(&mut conn, home.as_deref());+ send_or_warn(reply, result, "heal_catch_all_repo_paths");+ }WriteCmd::StoreEmbedding {page_id,vector_bytes,