Skip to content

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

Rust

test(robustness): stop `cargo test --workspace` flaking on the process guard + autoscope settle

Djalma Júniorc010c57a2555
diff --git a/crates/ai-memory-cli/src/process_guard.rs b/crates/ai-memory-cli/src/process_guard.rs
index 2a3a31c0..ac5232fd 100644
--- a/crates/ai-memory-cli/src/process_guard.rs
+++ b/crates/ai-memory-cli/src/process_guard.rs
@@ -14,6 +14,18 @@ pub const BIN_NAME: &str = "ai-memory";
/// process and any threads of it).
#[must_use]
pub fn sibling_processes() -> Vec<sysinfo::Pid> {
+ // Test opt-out. The destructive-command tests would otherwise flake
+ // non-deterministically: a dev box (and a parallel test run) almost always
+ // has some *other* `ai-memory` process alive, which the real scan rightly
+ // refuses against. Two seams, neither reachable in a normal shipped run:
+ // - `cfg!(test)` — the crate's own in-process unit tests (reset / reindex
+ // / restore) skip the scan.
+ // - `AI_MEMORY_TEST_NO_PROCESS_GUARD` — a spawned `ai-memory` a test
+ // launches reads it from its env and skips. The dedicated, `#[ignore]`d
+ // guard test launches WITHOUT it, so the guard itself stays covered.
+ if cfg!(test) || std::env::var_os("AI_MEMORY_TEST_NO_PROCESS_GUARD").is_some() {
+ return Vec::new();
+ }
let mut sys = System::new();
sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
let me = sysinfo::Pid::from_u32(std::process::id());
diff --git a/crates/ai-memory-cli/tests/uninstall.rs b/crates/ai-memory-cli/tests/uninstall.rs
index 7d609365..a8c7024e 100644
--- a/crates/ai-memory-cli/tests/uninstall.rs
+++ b/crates/ai-memory-cli/tests/uninstall.rs
@@ -417,6 +417,10 @@ fn uninstall_purge_data_apply_wipes() {
.args(["uninstall", "--apply", "--yes", "--purge-data"])
.env("HOME", home.path())
.env("AI_MEMORY_DATA_DIR", data.path())
+ // Exercises the WIPE, not the live-process guard; opt out so an
+ // unrelated `ai-memory` on the machine can't make it flake. The
+ // dedicated guard test below does NOT set this.
+ .env("AI_MEMORY_TEST_NO_PROCESS_GUARD", "1")
.output()
.unwrap();
assert!(
@@ -451,6 +455,9 @@ fn uninstall_dry_run_previews_purge() {
.args(["uninstall", "--purge-data"]) // dry-run: no --apply
.env("HOME", home.path())
.env("AI_MEMORY_DATA_DIR", data.path())
+ // Dry-run still hits the purge guard before previewing; opt out so an
+ // unrelated live `ai-memory` can't flake the preview.
+ .env("AI_MEMORY_TEST_NO_PROCESS_GUARD", "1")
.output()
.unwrap();
assert!(out.status.success());
diff --git a/crates/ai-memory-mcp/tests/autoscope_stress.rs b/crates/ai-memory-mcp/tests/autoscope_stress.rs
index 97b1dc82..447484a5 100644
--- a/crates/ai-memory-mcp/tests/autoscope_stress.rs
+++ b/crates/ai-memory-mcp/tests/autoscope_stress.rs
@@ -346,13 +346,29 @@ async fn fire_hook_and_settle(
assert_eq!(resp.status(), StatusCode::ACCEPTED, "hook must accept");
// Drain the response body before settling so the connection releases.
let _ = response_body_text(resp).await;
- // `process_envelope` is `tokio::spawn`-ed; give it a generous window
- // to land the active_project write on a loaded machine. Empirically a
- // single yield is enough; we go higher for headroom in CI.
- for _ in 0..20 {
+ // `process_envelope` is `tokio::spawn`-ed; poll a read for THIS session
+ // until its write is actually visible, instead of guessing a fixed settle
+ // time. A fixed sleep races under heavy load — the observed flake was a
+ // loaded full-workspace run where a session's active-project write hadn't
+ // landed yet, so its read saw `{hits: []}`. Polling waits exactly as long
+ // as needed (normally one iteration), with a generous cap that only burns
+ // if the write genuinely never lands (a real bug the downstream assert
+ // then reports).
+ let expected = number_word(project_index);
+ for _ in 0..200 {
tokio::task::yield_now().await;
+ let probe = router
+ .clone()
+ .oneshot(mcp_query_request(user, Some(session_id)))
+ .await
+ .expect("settle probe");
+ if probe.status() == StatusCode::OK
+ && extract_tool_text(&response_body_text(probe).await).contains(expected)
+ {
+ return;
+ }
+ tokio::time::sleep(Duration::from_millis(5)).await;
}
- tokio::time::sleep(Duration::from_millis(15)).await;
}
// ────────────────────────────────────────────────────────────────────────────