Skip to content

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

Rust

fix(store): never prefix-match a stored repo_path equal to $HOME

Thiago Silvadabb81061bc2
diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rs
index 2ab2df62..76cc894f 100644
--- a/crates/ai-memory-hooks/src/router.rs
+++ b/crates/ai-memory-hooks/src/router.rs
@@ -506,11 +506,12 @@ async fn resolve_project_ids(
// The match is keyed on the actual cwd (`cwd_norm`), not the stored
// `repo_path`: `repo_path` is now the git root or None (issue #103),
// whereas cwd->parent matching needs the full deep path.
+ let home_dir = std::env::var("HOME").ok();
let proj = if project_override.is_none()
&& let Some(rp) = cwd_norm.as_deref().filter(|s| !s.is_empty())
&& let Some((parent_id, parent_name)) = state
.reader
- .find_project_by_cwd_prefix(ws, rp.to_string())
+ .find_project_by_cwd_prefix(ws, rp.to_string(), home_dir.as_deref())
.await
.map_err(|e| anyhow::anyhow!("find_project_by_cwd_prefix: {e}"))?
&& parent_name != project_name
diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rs
index c2d247f7..1bdb1f7f 100644
--- a/crates/ai-memory-store/src/reader.rs
+++ b/crates/ai-memory-store/src/reader.rs
@@ -3222,6 +3222,12 @@ impl ReaderPool {
/// LIKE '%\_%' ESCAPE '\\'` — refuses stored values containing
/// LIKE wildcards (`%`, `_`) so a stored `repo_path` can never
/// match unintended siblings.
+ /// - `?3 IS NULL OR repo_path <> ?3` — never matches a stored
+ /// `repo_path` equal to the operator's home directory (`home`).
+ /// Such a row would be a prefix catch-all for every project
+ /// beneath `$HOME`; the caller passes the server's own `$HOME`
+ /// (filesystem root `/` is already excluded by the
+ /// `length(repo_path) > 1` guard). A `None` `home` is a no-op.
/// - Input canonicalisation — trailing slashes stripped; cwds
/// containing dot-segments (`/foo/../bar`, `/./x`) are rejected
/// outright so a traversal-style path can't match a parent it
@@ -3237,7 +3243,9 @@ impl ReaderPool {
&self,
workspace_id: WorkspaceId,
cwd: String,
+ home: Option<&str>,
) -> StoreResult<Option<(ProjectId, String)>> {
+ let home = home.map(str::to_owned);
let cwd_norm = cwd.trim_end_matches('/').to_string();
if !is_safe_cwd_for_prefix_match(&cwd_norm) {
return Ok(None);
@@ -3257,10 +3265,11 @@ impl ReaderPool {
AND repo_path NOT LIKE '%/' \
AND repo_path NOT LIKE '%\\%%' ESCAPE '\\' \
AND repo_path NOT LIKE '%\\_%' ESCAPE '\\' \
+ AND (?3 IS NULL OR repo_path <> ?3) \
AND (?2 = repo_path OR ?2 LIKE repo_path || '/%') \
ORDER BY length(repo_path) DESC \
LIMIT 1",
- params![workspace_id.as_bytes(), cwd_norm],
+ params![workspace_id.as_bytes(), cwd_norm, home],
|row| {
let bytes: Vec<u8> = row.get(0)?;
let name: String = row.get(1)?;
@@ -4084,3 +4093,67 @@ fn open_read_only(path: &Path) -> StoreResult<Connection> {
conn.pragma_update(None, "busy_timeout", 5_000)?;
Ok(conn)
}
+
+#[cfg(test)]
+mod tests {
+ use crate::Store;
+
+ /// A stored `repo_path` equal to the operator's `$HOME` must never be
+ /// prefix-matched: such a row would be a catch-all parent for every
+ /// project beneath the home directory. A normal nested repo under
+ /// `$HOME` must still match.
+ #[tokio::test]
+ async fn prefix_match_skips_home_dir_but_keeps_nested_repo() {
+ let tmp = tempfile::TempDir::new().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let ws = store
+ .writer
+ .get_or_create_workspace("default")
+ .await
+ .unwrap();
+ // A "$HOME catch-all" project whose repo_path is the home dir.
+ store
+ .writer
+ .get_or_create_project(ws, "home", Some(String::from("/home/tester")))
+ .await
+ .unwrap();
+ // A real nested repo beneath the home dir.
+ let app_id = store
+ .writer
+ .get_or_create_project(ws, "app", Some(String::from("/home/tester/projects/app")))
+ .await
+ .unwrap();
+
+ // A cwd somewhere under $HOME but not under any real repo must NOT
+ // resolve to the $HOME catch-all row.
+ let matched = store
+ .reader
+ .find_project_by_cwd_prefix(
+ ws,
+ String::from("/home/tester/random/dir"),
+ Some("/home/tester"),
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ matched, None,
+ "a stored repo_path equal to $HOME must never be prefix-matched"
+ );
+
+ // A cwd inside a genuine nested repo still resolves to that repo.
+ let matched = store
+ .reader
+ .find_project_by_cwd_prefix(
+ ws,
+ String::from("/home/tester/projects/app/src"),
+ Some("/home/tester"),
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ matched.map(|(id, _)| id),
+ Some(app_id),
+ "a genuine nested repo under $HOME must still prefix-match"
+ );
+ }
+}