Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(server): match a profile's roots case-insensitively on Windows paths
Rudy Mizrahi Celekli80d14c71a495
diff --git a/crates/ai-memory-cli/src/marker.rs b/crates/ai-memory-cli/src/marker.rsindex 94c25e94..50f23872 100644--- a/crates/ai-memory-cli/src/marker.rs+++ b/crates/ai-memory-cli/src/marker.rs@@ -326,11 +326,17 @@ impl HomeRoutes {}impl PathSelector {+ /// A drive-letter or UNC root names a filesystem that compares names+ /// case-insensitively, so its components must fold ASCII case.+ fn is_windows(&self) -> bool {+ matches!(self.root, PathRoot::Drive(_) | PathRoot::Unc(_, _))+ }+fn same_selector(&self, other: &Self) -> bool {if !self.root.same_root(&other.root) || self.components.len() != other.components.len() {return false;}- let windows = matches!(self.root, PathRoot::Drive(_) | PathRoot::Unc(_, _));+ let windows = self.is_windows();self.components.iter().zip(&other.components)@@ -373,7 +379,7 @@ impl PathSelector {if !self.root.same_root(&cwd.root) || self.components.len() > cwd.components.len() {return false;}- let windows = matches!(self.root, PathRoot::Drive(_) | PathRoot::Unc(_, _));+ let windows = self.is_windows();self.components.iter().zip(&cwd.components)@@ -387,6 +393,27 @@ impl PathSelector {}}+/// Whether `root` contains `candidate`, comparing whole components and+/// folding ASCII case under a Windows root — the rule [`PathSelector`]+/// already applies to the home marker's path routes.+///+/// `pub(crate)`: `server_profiles::resolve` admits a marker against a+/// profile's `roots` with the same rule, so one checkout cannot be routed by+/// a path route yet refused by a root that covers it. `Path::starts_with`+/// alone compares components byte-exactly, which on a case-insensitive+/// filesystem rejects `C:\work\team-b` against a root spelled+/// `C:\Work\team-b`. A path that is not a Windows selector keeps the+/// byte-exact comparison rather than being folded.+pub(crate) fn path_contains(root: &Path, candidate: &Path) -> bool {+ if let Ok(root_selector) = parse_absolute_selector(&root.to_string_lossy())+ && root_selector.is_windows()+ && let Ok(candidate_selector) = parse_absolute_selector(&candidate.to_string_lossy())+ {+ return root_selector.contains(&candidate_selector);+ }+ candidate.starts_with(root)+}+fn parse_absolute_selector(raw: &str) -> Result<PathSelector, ()> {let replaced = raw.replace('\\', "/");let (root, rest) = if let Some(rest) = replaced.strip_prefix("//") {@@ -2563,4 +2590,38 @@ project = "infra" # this is finefind_server_selection(tmp.path().to_str().unwrap(), Some(tmp.path())).unwrap();assert_eq!(selection.name, None);}++ /// A profile's `roots` must admit a checkout whose drive-letter cwd+ /// differs from the registered root only in case — the spelling a+ /// harness reports on a case-insensitive filesystem. The selector logic+ /// is textual, so these assertions hold on every host.+ #[test]+ fn path_contains_folds_case_under_a_windows_root_only() {+ assert!(path_contains(+ Path::new(r"C:\Work\team-b"),+ Path::new(r"C:\work\team-b\repo")+ ));+ assert!(path_contains(+ Path::new(r"\\Server\Share\Work"),+ Path::new(r"\\server\share\work\repo")+ ));+ // Folding case must not also loosen the component boundary.+ assert!(!path_contains(+ Path::new(r"C:\Work\team-b"),+ Path::new(r"C:\work\team-bb\repo")+ ));+ assert!(!path_contains(+ Path::new(r"C:\Work\team-b"),+ Path::new(r"D:\work\team-b\repo")+ ));+ // Control: a POSIX root stays case-sensitive.+ assert!(!path_contains(+ Path::new("/work/Team-B"),+ Path::new("/work/team-b/repo")+ ));+ assert!(path_contains(+ Path::new("/work/team-b"),+ Path::new("/work/team-b/repo")+ ));+ }}diff --git a/crates/ai-memory-cli/src/server_profiles.rs b/crates/ai-memory-cli/src/server_profiles.rsindex 471dd249..ab8ed036 100644--- a/crates/ai-memory-cli/src/server_profiles.rs+++ b/crates/ai-memory-cli/src/server_profiles.rs@@ -305,7 +305,9 @@ pub(crate) fn read_token(data_dir: &Path, name: &ProfileName) -> Option<String>/// `marker_dir` is the directory of the marker that declared the key, in the/// same lexical namespace as the hook's cwd (see `marker::absolute_normalized`);/// `roots` are compared against it component-wise, so `~/work/team-b` admits-/// `~/work/team-b/repo` but not `~/work/team-bb`.+/// `~/work/team-b/repo` but not `~/work/team-bb`. A Windows root folds ASCII+/// case, since the filesystem does and the cwd a harness reports can differ+/// in case from the spelling the root was registered with.pub(crate) fn resolve(data_dir: &Path,raw_name: &str,@@ -328,7 +330,7 @@ pub(crate) fn resolve(.roots.iter().filter_map(|root| expand_root(root, home))- .any(|root| marker_dir.starts_with(&root));+ .any(|root| crate::marker::path_contains(&root, &marker_dir));if !admitted {return Err(Rejection::OutsideRoots);}@@ -699,6 +701,42 @@ mod tests {);}+ /// Case folding is scoped to Windows roots: on a case-sensitive+ /// filesystem `work/Team-B` and `work/team-b` are different directories+ /// and a root over one must not admit the other.+ #[cfg(unix)]+ #[test]+ fn a_posix_root_still_matches_case_sensitively() {+ let dd = tempfile::tempdir().unwrap();+ let home = dd.path().join("home");+ add(+ dd.path(),+ &name("b"),+ "https://b.example",+ &["~/work/Team-B".to_owned()],+ Some("tok-b"),+ )+ .unwrap();+ assert!(+ resolve(+ dd.path(),+ "b",+ &home.join("work").join("Team-B").join("repo"),+ Some(&home)+ )+ .is_ok()+ );+ assert_eq!(+ resolve(+ dd.path(),+ "b",+ &home.join("work").join("team-b").join("repo"),+ Some(&home)+ ),+ Err(Rejection::OutsideRoots)+ );+ }+#[test]fn add_preserves_other_profiles_and_comments_and_remove_deletes_the_token() {let dd = tempfile::tempdir().unwrap();