Skip to content

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

Rust

test(cli): keep a developer's harness store relocations out of the tests

Samir Hanna Verza2bcfddd40bfa
diff --git a/crates/ai-memory-cli/src/commands/backfill.rs b/crates/ai-memory-cli/src/commands/backfill.rs
index 4d322719..79905273 100644
--- a/crates/ai-memory-cli/src/commands/backfill.rs
+++ b/crates/ai-memory-cli/src/commands/backfill.rs
@@ -44,7 +44,7 @@ use ai_memory_workstream::{
wait_for_transcript_flush,
};
-use super::doctor::SCANNED_HARNESSES;
+use super::doctor::{SCANNED_HARNESSES, relocated_session_dir};
use super::run;
use crate::config::Config;
use crate::http_client::{ServerEndpoint, get_json, post_json};
@@ -267,12 +267,23 @@ async fn collect_local_sessions(
home: &Path,
cwd: &Path,
only_session: Option<&str>,
+) -> Vec<SessionRef> {
+ collect_local_sessions_with(home, cwd, only_session, relocated_session_dir).await
+}
+
+/// [`collect_local_sessions`] with the relocation lookup passed in, for the
+/// same reason as `doctor::scan_local_with`: tests pass `|_| None` so a
+/// developer's `CLAUDE_CONFIG_DIR` cannot hide a fixture planted under a
+/// temporary `$HOME`.
+async fn collect_local_sessions_with(
+ home: &Path,
+ cwd: &Path,
+ only_session: Option<&str>,
+ session_dir_for: impl Fn(ManagedHarness) -> Option<PathBuf>,
) -> Vec<SessionRef> {
let mut out = Vec::new();
for &harness in SCANNED_HARNESSES {
- let session_dir = build_launch_plan(harness, None, Vec::new(), None)
- .ok()
- .and_then(|plan| plan.session_dir);
+ let session_dir = session_dir_for(harness);
let Ok(sessions) = list_native_sessions(
harness,
home,
@@ -756,7 +767,7 @@ mod tests {
});
std::fs::write(session_dir.join("foreign.jsonl"), format!("{foreign}\n")).unwrap();
- let found = collect_local_sessions(home.path(), cwd.path(), None).await;
+ let found = collect_local_sessions_with(home.path(), cwd.path(), None, |_| None).await;
let claude: Vec<_> = found
.iter()
.filter(|s| s.harness == ManagedHarness::Claude)
@@ -768,7 +779,8 @@ mod tests {
);
// `--session` narrows to one id.
- let only = collect_local_sessions(home.path(), cwd.path(), Some("nope")).await;
+ let only =
+ collect_local_sessions_with(home.path(), cwd.path(), Some("nope"), |_| None).await;
assert!(only.is_empty(), "no session matches the filter: {only:?}");
}
diff --git a/crates/ai-memory-cli/src/commands/doctor.rs b/crates/ai-memory-cli/src/commands/doctor.rs
index 97c5d81b..a1c240b9 100644
--- a/crates/ai-memory-cli/src/commands/doctor.rs
+++ b/crates/ai-memory-cli/src/commands/doctor.rs
@@ -20,7 +20,7 @@
//! for the captured side, and the local side is read-only.
use std::collections::BTreeMap;
-use std::path::Path;
+use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};
use anyhow::{Context, Result};
@@ -175,15 +175,35 @@ pub(crate) fn build_rows(
/// Read-only. A harness whose store is unreadable, absent, or unsupported
/// simply contributes nothing — the command never invents a gap it cannot see.
pub(crate) async fn scan_local(home: &Path, cwd: &Path, since_days: u32) -> Vec<LocalScan> {
+ scan_local_with(home, cwd, since_days, relocated_session_dir).await
+}
+
+/// Where `harness` keeps its sessions when the environment relocates its home
+/// (`CLAUDE_CONFIG_DIR`, `CODEX_HOME`, `KIMI_CODE_HOME`, …), via the same
+/// launch-plan resolver `ai-memory run` uses. `None` means the default
+/// `$HOME`-relative store.
+pub(crate) fn relocated_session_dir(harness: ManagedHarness) -> Option<PathBuf> {
+ build_launch_plan(harness, None, Vec::new(), None)
+ .ok()
+ .and_then(|plan| plan.session_dir)
+}
+
+/// [`scan_local`] with the relocation lookup passed in. The lookup reads the
+/// process environment, so a test that plants a fixture under a temporary
+/// `$HOME` passes `|_| None`: otherwise a developer's `CLAUDE_CONFIG_DIR`
+/// wins over that `$HOME` and the fixture is never found.
+async fn scan_local_with(
+ home: &Path,
+ cwd: &Path,
+ since_days: u32,
+ session_dir_for: impl Fn(ManagedHarness) -> Option<PathBuf>,
+) -> Vec<LocalScan> {
let recent_cutoff = recent_cutoff(SystemTime::now(), since_days);
let mut scans = Vec::new();
for &harness in SCANNED_HARNESSES {
- // Honor harness home relocations (CODEX_HOME, KIMI_CODE_HOME, …) via the
- // same launch-plan resolver `ai-memory run` uses; fall back to the
- // default $HOME-relative store when a probe plan cannot be built.
- let session_dir = build_launch_plan(harness, None, Vec::new(), None)
- .ok()
- .and_then(|plan| plan.session_dir);
+ // Honor harness home relocations (see `relocated_session_dir`); fall
+ // back to the default $HOME-relative store when there is none.
+ let session_dir = session_dir_for(harness);
let Ok(sessions) =
list_native_sessions(harness, home, cwd, session_dir.as_deref(), SCAN_LIMIT).await
else {
@@ -478,7 +498,7 @@ mod tests {
});
std::fs::write(session_dir.join("foreign.jsonl"), format!("{foreign}\n")).unwrap();
- let scans = scan_local(home.path(), cwd.path(), 0).await;
+ let scans = scan_local_with(home.path(), cwd.path(), 0, |_| None).await;
let claude = scans
.iter()
.find(|s| s.agent == AgentKind::ClaudeCode)
@@ -488,7 +508,7 @@ mod tests {
// And a project with no local stores yields no scans at all.
let empty_home = tempfile::tempdir().unwrap();
- let none = scan_local(empty_home.path(), cwd.path(), 0).await;
+ let none = scan_local_with(empty_home.path(), cwd.path(), 0, |_| None).await;
assert!(none.is_empty(), "no stores should mean no scans: {none:?}");
}
}
diff --git a/crates/ai-memory-cli/src/commands/run.rs b/crates/ai-memory-cli/src/commands/run.rs
index 360ecd87..a56fe6f2 100644
--- a/crates/ai-memory-cli/src/commands/run.rs
+++ b/crates/ai-memory-cli/src/commands/run.rs
@@ -2342,7 +2342,10 @@ mod tests {
rollout("prepared", "prepared", &cwd);
rollout("nested", "nested", &temp.path().join("other-checkout"));
rollout("concurrent", "concurrent-newer", &cwd);
- let plan = build_launch_plan(ManagedHarness::Codex, None, Vec::new(), None).unwrap();
+ let mut plan = build_launch_plan(ManagedHarness::Codex, None, Vec::new(), None).unwrap();
+ // The plan reads the process environment: a developer's CODEX_HOME
+ // would send discovery away from the rollouts planted under `temp`.
+ plan.session_dir = None;
let status = |linked: bool, native: &str| ManagedRunStatus {
run_id: ManagedRunId::new(),
workstream_id: WorkstreamId::new(),
@@ -2418,13 +2421,18 @@ mod tests {
)
.unwrap();
- let utility = build_launch_plan(
+ let mut utility = build_launch_plan(
ManagedHarness::Codex,
None,
vec![OsString::from("--version")],
None,
)
.unwrap();
+ // Both plans read the process environment. With a developer's
+ // CODEX_HOME set, discovery would look away from the rollout planted
+ // under `temp`, and the `is_none()` below would pass for that reason
+ // instead of the passthrough one.
+ utility.session_dir = None;
assert_eq!(utility.mode, LaunchMode::Passthrough);
assert!(
resolve_native_session_after_run(
@@ -2440,7 +2448,8 @@ mod tests {
.is_none()
);
- let session = build_launch_plan(ManagedHarness::Codex, None, Vec::new(), None).unwrap();
+ let mut session = build_launch_plan(ManagedHarness::Codex, None, Vec::new(), None).unwrap();
+ session.session_dir = None;
assert_eq!(
resolve_native_session_after_run(
&session,
diff --git a/crates/ai-memory-cli/tests/suite/backfill_failures.rs b/crates/ai-memory-cli/tests/suite/backfill_failures.rs
index 4cc8cd39..c5b6c29e 100644
--- a/crates/ai-memory-cli/tests/suite/backfill_failures.rs
+++ b/crates/ai-memory-cli/tests/suite/backfill_failures.rs
@@ -45,7 +45,6 @@ async fn import_failures_are_errors_in_human_json_and_quiet_modes() {
for mode in ["human", "json", "quiet"] {
let data = tempfile::tempdir().unwrap();
let mut cmd = crate::e2e_support::hermetic(env!("CARGO_BIN_EXE_ai-memory"));
- cmd.env_remove("CODEX_HOME");
cmd.current_dir(&cwd)
.env("AI_MEMORY_HOME", home.path())
.env("AI_MEMORY_DATA_DIR", data.path())
diff --git a/crates/ai-memory-cli/tests/suite/e2e_support.rs b/crates/ai-memory-cli/tests/suite/e2e_support.rs
index d8dc0a03..e515f767 100644
--- a/crates/ai-memory-cli/tests/suite/e2e_support.rs
+++ b/crates/ai-memory-cli/tests/suite/e2e_support.rs
@@ -20,11 +20,28 @@ use serde_json::Value;
const BIN: &str = env!("CARGO_BIN_EXE_ai-memory");
+/// Harness store relocations the child would otherwise honor over the test's
+/// `$HOME` — the variables `environment_session_dir_with` in
+/// `ai-memory-workstream/src/harness.rs` reads. A developer running Claude Code
+/// with a relocated profile exports `CLAUDE_CONFIG_DIR`, and then `backfill`
+/// and `doctor` look there instead of at the transcripts the test planted.
+const HARNESS_STORE_OVERRIDES: &[&str] = &[
+ "CLAUDE_CONFIG_DIR",
+ "CODEX_HOME",
+ "GROK_HOME",
+ "KIMI_CODE_HOME",
+ "KIRO_HOME",
+ "PI_CODING_AGENT_DIR",
+ "PI_CODING_AGENT_SESSION_DIR",
+ "XDG_DATA_HOME",
+];
+
/// Start from a clean, hermetic environment: drop every ambient `AI_MEMORY_*`
/// var (a developer box or this project's own MCP config may export
-/// `AI_MEMORY_AUTH_TOKEN`, `AI_MEMORY_SERVER_URL`, scope names, …) so the child
-/// sees only what the test sets. Without this the spawned server would inherit
-/// an auth token and reject the test's own requests.
+/// `AI_MEMORY_AUTH_TOKEN`, `AI_MEMORY_SERVER_URL`, scope names, …) and every
+/// harness store relocation, so the child sees only what the test sets.
+/// Without this the spawned server would inherit an auth token and reject the
+/// test's own requests.
pub fn hermetic(program: &str) -> Command {
let mut cmd = Command::new(program);
for (key, _) in std::env::vars_os() {
@@ -32,6 +49,9 @@ pub fn hermetic(program: &str) -> Command {
cmd.env_remove(key);
}
}
+ for key in HARNESS_STORE_OVERRIDES {
+ cmd.env_remove(key);
+ }
cmd
}
diff --git a/crates/ai-memory-cli/tests/suite/removal.rs b/crates/ai-memory-cli/tests/suite/removal.rs
index c2b0e4f7..348516ed 100644
--- a/crates/ai-memory-cli/tests/suite/removal.rs
+++ b/crates/ai-memory-cli/tests/suite/removal.rs
@@ -45,7 +45,10 @@ fn command_with_home(home: &Path) -> Command {
.env_remove("KIRO_HOME")
// Keep Claude installer/removal tests inside their temp HOME unless a
// test explicitly opts into a relocated config root.
- .env_remove("CLAUDE_CONFIG_DIR");
+ .env_remove("CLAUDE_CONFIG_DIR")
+ // A host-level PI_CODING_AGENT_DIR would send the pi/omp extension
+ // uninstall to the developer's real agent dir instead of the sandbox.
+ .env_remove("PI_CODING_AGENT_DIR");
command
}