Skip to content

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

Rust

test(cli): keep hook_scope and project routing tests off the home marker too

AkitaOnRails375c0ab318e4
diff --git a/crates/ai-memory-cli/src/commands/hook.rs b/crates/ai-memory-cli/src/commands/hook.rs
index 41e8d0b8..667979bf 100644
--- a/crates/ai-memory-cli/src/commands/hook.rs
+++ b/crates/ai-memory-cli/src/commands/hook.rs
@@ -33,8 +33,8 @@ use crate::server_profiles::{self, ProfileName, Rejection, ResolvedServer};
use sha2::{Digest as _, Sha256};
use super::hook_capture::{
- build_client, canonical_context, capture_policy, extract_cwd, get_handoff, hook_scope,
- marker_capture_policy, marker_query_suffix, marker_query_suffix_without_briefing,
+ HookScope, build_client, canonical_context, capture_policy, extract_cwd, get_handoff,
+ hook_scope, marker_capture_policy, marker_query_suffix, marker_query_suffix_without_briefing,
marker_requests_briefing, resolve_cwd_with_fallbacks, url_encode,
};
use super::hook_drain_process;
@@ -1739,8 +1739,16 @@ pub(crate) fn delivers_to_same_server(data_dir: &Path, source: &str, destination
/// A transcript import posts every event under one project, so an event the
/// hook would file under another project must not ride along.
pub(crate) fn routes_to_same_project(source: &str, destination: &str) -> bool {
- let scope = hook_scope(source, None);
- if scope != hook_scope(destination, None) {
+ routes_to_same_project_with(source, destination, |cwd| hook_scope(cwd, None))
+}
+
+fn routes_to_same_project_with(
+ source: &str,
+ destination: &str,
+ scope_of: impl Fn(&str) -> HookScope,
+) -> bool {
+ let scope = scope_of(source);
+ if scope != scope_of(destination) {
return false;
}
if scope.names_the_project() {
@@ -4383,6 +4391,14 @@ mod tests {
)
.unwrap();
let path = |dir: &Path| dir.to_string_lossy().into_owned();
+ // An empty home keeps a developer's own `~/.ai-memory.toml` (which
+ // applies to every cwd) out of the routing decision.
+ let home = tempfile::tempdir().unwrap();
+ let routes_to_same_project = |source: &str, destination: &str| {
+ routes_to_same_project_with(source, destination, |cwd| {
+ crate::commands::hook_capture::hook_scope_with_home(cwd, None, Some(home.path()))
+ })
+ };
assert!(!routes_to_same_project(
&path(&basename.join("sub1")),
&path(&basename.join("sub2"))
diff --git a/crates/ai-memory-cli/src/commands/hook_capture.rs b/crates/ai-memory-cli/src/commands/hook_capture.rs
index eab66d22..e6e0e9c4 100644
--- a/crates/ai-memory-cli/src/commands/hook_capture.rs
+++ b/crates/ai-memory-cli/src/commands/hook_capture.rs
@@ -431,6 +431,18 @@ pub fn hook_scope(cwd: &str, default_strategy: Option<&str>) -> HookScope {
hook_scope_from_selection(cwd, default_strategy, routing_selection(cwd).ok().flatten())
}
+/// [`hook_scope`] resolved against `home` instead of the operator's home
+/// directory, so tests can keep a developer's `~/.ai-memory.toml` out.
+#[cfg(test)]
+pub(crate) fn hook_scope_with_home(
+ cwd: &str,
+ default_strategy: Option<&str>,
+ home: Option<&std::path::Path>,
+) -> HookScope {
+ let selection = crate::marker::routing_selection_with_home(cwd, cwd, home, false);
+ hook_scope_from_selection(cwd, default_strategy, selection.ok().flatten())
+}
+
pub(crate) fn inspect_repository_coordinate(
identity_cwd: &str,
marker: &crate::marker::MarkerInspection,
@@ -1036,6 +1048,12 @@ mod tests {
}
}
+ /// Shadows [`super::hook_scope`] the same way, for the same reason.
+ fn hook_scope(cwd: &str, default_strategy: Option<&str>) -> HookScope {
+ let home = tempfile::TempDir::new().unwrap();
+ hook_scope_with_home(cwd, default_strategy, Some(home.path()))
+ }
+
async fn serve_once(status: &'static str, body: &'static str) -> String {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();