Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
test(cli): extract shared e2e spawn helpers into e2e_support
AkitaOnRailsdad08e3f56c5
diff --git a/crates/ai-memory-cli/tests/suite/backfill_e2e.rs b/crates/ai-memory-cli/tests/suite/backfill_e2e.rsindex f154eae4..1a0123b8 100644--- a/crates/ai-memory-cli/tests/suite/backfill_e2e.rs+++ b/crates/ai-memory-cli/tests/suite/backfill_e2e.rs@@ -33,13 +33,16 @@/// lives in the slow tier (`cargo tf` / CI), not the everyday loop.mod slow {use std::fs;- use std::net::TcpListener;use std::path::{Path, PathBuf};- use std::process::{Child, Command, Stdio};+ use std::process::Stdio;use std::time::{Duration, Instant};use serde_json::{Value, json};+ use crate::e2e_support::{+ ServerGuard, free_port, hermetic, run_cli, session_count, write_jsonl,+ };+const BIN: &str = env!("CARGO_BIN_EXE_ai-memory");const WORKSPACE: &str = "backfill-e2e-ws";const PROJECT: &str = "backfill-e2e-proj";@@ -55,50 +58,6 @@ mod slow {format!("sk-canary{CANARY_MARKER}0123456789abcdef")}- /// 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 this test sets. Without this the spawned- /// server would inherit an auth token and reject the test's own requests.- fn hermetic(program: &str) -> Command {- let mut cmd = Command::new(program);- for (key, _) in std::env::vars_os() {- if key.to_string_lossy().starts_with("AI_MEMORY_") {- cmd.env_remove(key);- }- }- cmd- }-- /// Kill the spawned server when the test ends, pass or fail.- struct ServerGuard(Child);- impl Drop for ServerGuard {- fn drop(&mut self) {- let _ = self.0.kill();- let _ = self.0.wait();- }- }-- /// A free loopback port. The brief unbind→rebind race is acceptable for a- /// slow-tier test and is the same approach the shell smoke test uses.- fn free_port() -> u16 {- TcpListener::bind("127.0.0.1:0")- .expect("bind ephemeral port")- .local_addr()- .expect("local addr")- .port()- }-- /// Write the JSONL lines (each already a `Value`) as one transcript file.- fn write_jsonl(path: &Path, lines: &[Value]) {- let mut body = String::new();- for line in lines {- body.push_str(&line.to_string());- body.push('\n');- }- fs::write(path, body).expect("write transcript");- }-/// Recursively test whether any file under `root` contains `needle` (bytes,/// so it also catches the secret inside the binary SQLite db).fn tree_contains(root: &Path, needle: &str) -> Option<PathBuf> {@@ -123,31 +82,6 @@ mod slow {None}- /// Sum the server's per-agent session counts for the scope. A 404 (the- /// no-create scope lookup for a project that has never been written to)- /// counts as zero — the pre-import state.- async fn session_count(client: &reqwest::Client, base: &str) -> u64 {- let resp = client- .get(format!("{base}/admin/sessions/by-agent"))- .query(&[("workspace", WORKSPACE), ("project", PROJECT)])- .send()- .await- .expect("by-agent request");- if !resp.status().is_success() {- return 0;- }- let body: Value = resp.json().await.expect("by-agent json");- body["by_agent"]- .as_array()- .map(|agents| {- agents- .iter()- .filter_map(|a| a["sessions"].as_u64())- .sum::<u64>()- })- .unwrap_or(0)- }-/// Call a memory tool over the stateless Streamable-HTTP `/mcp` transport/// (the default `serve` mode: no `initialize` handshake needed) and return/// the joined text of the tool result.@@ -186,31 +120,6 @@ mod slow {.join("\n")}- /// Run a subcommand of the built binary to completion, returning its stdout.- /// The scope/server/home environment is shared with the spawned server so- /// the client talks to the same store the way a real install does.- fn run_cli(args: &[&str], data_dir: &Path, home: &Path, cwd: &Path, base: &str) -> String {- let out = hermetic(BIN)- .args(args)- .current_dir(cwd)- .env("AI_MEMORY_DATA_DIR", data_dir)- .env("AI_MEMORY_HOME", home)- .env("AI_MEMORY_SERVER_URL", base)- .env("AI_MEMORY_EMBEDDING_PROVIDER", "none")- .env("RUST_LOG", "off")- .output()- .unwrap_or_else(|e| panic!("spawn `{}`: {e}", args.join(" ")));- assert!(- out.status.success(),- "`{}` failed: {}\nstdout: {}\nstderr: {}",- args.join(" "),- out.status,- String::from_utf8_lossy(&out.stdout),- String::from_utf8_lossy(&out.stderr),- );- String::from_utf8(out.stdout).expect("stdout utf8")- }-#[tokio::test]async fn backfill_imports_local_history_into_an_empty_store_and_is_searchable() {let data_dir = tempfile::tempdir().expect("data dir");@@ -301,7 +210,7 @@ mod slow {// Phase 1 — the store is empty before backfill.assert_eq!(- session_count(&client, &base).await,+ session_count(&client, &base, WORKSPACE, PROJECT).await,0,"a brand-new project store must start with no sessions",);@@ -318,7 +227,7 @@ mod slow {],data_dir.path(),home.path(),- &cwd,+ Some(&cwd),&base,)).expect("backfill --json report");@@ -339,14 +248,14 @@ mod slow {// it acks, but poll briefly to stay robust against any lag.let deadline = Instant::now() + Duration::from_secs(10);loop {- if session_count(&client, &base).await >= 1 {+ if session_count(&client, &base, WORKSPACE, PROJECT).await >= 1 {break;}assert!(Instant::now() < deadline, "imported session never appeared");tokio::time::sleep(Duration::from_millis(150)).await;}assert_eq!(- session_count(&client, &base).await,+ session_count(&client, &base, WORKSPACE, PROJECT).await,1,"exactly one session must exist after import",);@@ -395,7 +304,7 @@ mod slow {],data_dir.path(),home.path(),- &cwd,+ Some(&cwd),&base,)).expect("second backfill --json report");@@ -408,7 +317,7 @@ mod slow {"the second run must import nothing: {report2}");assert_eq!(- session_count(&client, &base).await,+ session_count(&client, &base, WORKSPACE, PROJECT).await,1,"the re-run must not duplicate the imported session",);diff --git a/crates/ai-memory-cli/tests/suite/doctor_e2e.rs b/crates/ai-memory-cli/tests/suite/doctor_e2e.rsindex 884f99f1..cf373863 100644--- a/crates/ai-memory-cli/tests/suite/doctor_e2e.rs+++ b/crates/ai-memory-cli/tests/suite/doctor_e2e.rs@@ -36,111 +36,19 @@/// lives in the slow tier (`cargo tf` / CI), not the everyday loop.mod slow {use std::fs;- use std::net::TcpListener;- use std::path::Path;- use std::process::{Child, Command, Stdio};+ use std::process::Stdio;use std::time::{Duration, Instant};use serde_json::{Value, json};+ use crate::e2e_support::{+ ServerGuard, free_port, hermetic, run_cli, session_count, write_jsonl,+ };+const BIN: &str = env!("CARGO_BIN_EXE_ai-memory");const WORKSPACE: &str = "doctor-e2e-ws";const PROJECT: &str = "doctor-e2e-proj";- /// 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 this test sets. Without this the spawned- /// server would inherit an auth token and reject the test's own requests.- fn hermetic(program: &str) -> Command {- let mut cmd = Command::new(program);- for (key, _) in std::env::vars_os() {- if key.to_string_lossy().starts_with("AI_MEMORY_") {- cmd.env_remove(key);- }- }- cmd- }-- /// Kill the spawned server when the test ends, pass or fail.- struct ServerGuard(Child);- impl Drop for ServerGuard {- fn drop(&mut self) {- let _ = self.0.kill();- let _ = self.0.wait();- }- }-- /// A free loopback port. The brief unbind→rebind race is acceptable for a- /// slow-tier test and is the same approach the shell smoke test uses.- fn free_port() -> u16 {- TcpListener::bind("127.0.0.1:0")- .expect("bind ephemeral port")- .local_addr()- .expect("local addr")- .port()- }-- /// Write the JSONL lines (each already a `Value`) as one transcript file.- fn write_jsonl(path: &Path, lines: &[Value]) {- let mut body = String::new();- for line in lines {- body.push_str(&line.to_string());- body.push('\n');- }- fs::write(path, body).expect("write transcript");- }-- /// Sum the server's per-agent session counts for the scope. A 404 (the- /// no-create scope lookup for a project that has never been written to)- /// counts as zero — the pre-import state.- async fn session_count(client: &reqwest::Client, base: &str) -> u64 {- let resp = client- .get(format!("{base}/admin/sessions/by-agent"))- .query(&[("workspace", WORKSPACE), ("project", PROJECT)])- .send()- .await- .expect("by-agent request");- if !resp.status().is_success() {- return 0;- }- let body: Value = resp.json().await.expect("by-agent json");- body["by_agent"]- .as_array()- .map(|agents| {- agents- .iter()- .filter_map(|a| a["sessions"].as_u64())- .sum::<u64>()- })- .unwrap_or(0)- }-- /// Run a subcommand of the built binary to completion, returning its stdout.- /// The scope/server/home environment is shared with the spawned server so- /// the client talks to the same store the way a real install does.- fn run_cli(args: &[&str], data_dir: &Path, home: &Path, cwd: &Path, base: &str) -> String {- let out = hermetic(BIN)- .args(args)- .current_dir(cwd)- .env("AI_MEMORY_DATA_DIR", data_dir)- .env("AI_MEMORY_HOME", home)- .env("AI_MEMORY_SERVER_URL", base)- .env("AI_MEMORY_EMBEDDING_PROVIDER", "none")- .env("RUST_LOG", "off")- .output()- .unwrap_or_else(|e| panic!("spawn `{}`: {e}", args.join(" ")));- assert!(- out.status.success(),- "`{}` failed: {}\nstdout: {}\nstderr: {}",- args.join(" "),- out.status,- String::from_utf8_lossy(&out.stdout),- String::from_utf8_lossy(&out.stderr),- );- String::from_utf8(out.stdout).expect("stdout utf8")- }-/// The row for one agent kind in a `doctor --json` report, or `None`.fn find_row<'a>(report: &'a Value, agent: &str) -> Option<&'a Value> {report["rows"]@@ -238,7 +146,7 @@ mod slow {// Precondition: nothing captured yet for this scope.assert_eq!(- session_count(&client, &base).await,+ session_count(&client, &base, WORKSPACE, PROJECT).await,0,"a brand-new project store must start with no captured sessions",);@@ -258,7 +166,7 @@ mod slow {],data_dir.path(),home.path(),- &cwd,+ Some(&cwd),&base,)).expect("doctor --json report");@@ -299,7 +207,7 @@ mod slow {],data_dir.path(),home.path(),- &cwd,+ Some(&cwd),&base,);assert!(@@ -324,7 +232,7 @@ mod slow {],data_dir.path(),home.path(),- &cwd,+ Some(&cwd),&base,)).expect("backfill --json report");@@ -336,7 +244,7 @@ mod slow {// The captured count is now nonzero (poll briefly for commit lag).let deadline = Instant::now() + Duration::from_secs(10);loop {- if session_count(&client, &base).await >= 1 {+ if session_count(&client, &base, WORKSPACE, PROJECT).await >= 1 {break;}assert!(Instant::now() < deadline, "captured session never appeared");@@ -358,7 +266,7 @@ mod slow {],data_dir.path(),home.path(),- &cwd,+ Some(&cwd),&base,)).expect("second doctor --json report");@@ -394,7 +302,7 @@ mod slow {],data_dir.path(),home.path(),- &cwd,+ Some(&cwd),&base,);assert!(diff --git a/crates/ai-memory-cli/tests/suite/e2e_support.rs b/crates/ai-memory-cli/tests/suite/e2e_support.rsnew file mode 100644index 00000000..d8dc0a03--- /dev/null+++ b/crates/ai-memory-cli/tests/suite/e2e_support.rs@@ -0,0 +1,135 @@+//! Shared spawned-server helpers for the CLI end-to-end suites.+//!+//! `backfill_e2e`, `doctor_e2e`, and `message_e2e` each drive the shipped+//! `ai-memory` binary against a real `ai-memory serve` child, exactly as an+//! operator would. These are the pieces they all share: a hermetic `Command`,+//! the server-lifetime guard, ephemeral port allocation, the `by-agent`+//! session count, and the subcommand runner.+//!+//! Kept deliberately cfg-agnostic (no `#![cfg(unix)]`) so the non-unix+//! `message_e2e` can use it too. The genuinely POSIX-specific fixtures — the+//! native `~/.claude/projects/<enc-cwd>/` transcript layout — stay in the+//! individual `#[cfg(unix)]` files.++use std::fs;+use std::net::TcpListener;+use std::path::Path;+use std::process::{Child, Command};++use serde_json::Value;++const BIN: &str = env!("CARGO_BIN_EXE_ai-memory");++/// 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.+pub fn hermetic(program: &str) -> Command {+ let mut cmd = Command::new(program);+ for (key, _) in std::env::vars_os() {+ if key.to_string_lossy().starts_with("AI_MEMORY_") {+ cmd.env_remove(key);+ }+ }+ cmd+}++/// Kill the spawned server when the test ends, pass or fail.+pub struct ServerGuard(pub Child);+impl Drop for ServerGuard {+ fn drop(&mut self) {+ let _ = self.0.kill();+ let _ = self.0.wait();+ }+}++/// A free loopback port. The brief unbind→rebind race is acceptable for a+/// slow-tier test and is the same approach the shell smoke test uses.+pub fn free_port() -> u16 {+ TcpListener::bind("127.0.0.1:0")+ .expect("bind ephemeral port")+ .local_addr()+ .expect("local addr")+ .port()+}++/// Write the JSONL lines (each already a `Value`) as one transcript file.+///+/// Only the POSIX-gated fixtures plant transcripts, so this is unused on+/// non-unix targets where those modules are compiled out.+#[cfg_attr(not(unix), allow(dead_code))]+pub fn write_jsonl(path: &Path, lines: &[Value]) {+ let mut body = String::new();+ for line in lines {+ body.push_str(&line.to_string());+ body.push('\n');+ }+ fs::write(path, body).expect("write transcript");+}++/// Sum the server's per-agent session counts for the scope. A 404 (the+/// no-create scope lookup for a project that has never been written to) counts+/// as zero — the pre-import / pre-existence state.+pub async fn session_count(+ client: &reqwest::Client,+ base: &str,+ workspace: &str,+ project: &str,+) -> u64 {+ let resp = client+ .get(format!("{base}/admin/sessions/by-agent"))+ .query(&[("workspace", workspace), ("project", project)])+ .send()+ .await+ .expect("by-agent request");+ if !resp.status().is_success() {+ return 0;+ }+ let body: Value = resp.json().await.expect("by-agent json");+ body["by_agent"]+ .as_array()+ .map(|agents| {+ agents+ .iter()+ .filter_map(|a| a["sessions"].as_u64())+ .sum::<u64>()+ })+ .unwrap_or(0)+}++/// Run a subcommand of the built binary to completion, returning its stdout.+/// The scope/server/home environment is shared with the spawned server so the+/// client talks to the same store the way a real install does. Pass `cwd` when+/// the subcommand resolves the current project from its working directory+/// (backfill/doctor); pass `None` to inherit the test's own cwd (message).+pub fn run_cli(+ args: &[&str],+ data_dir: &Path,+ home: &Path,+ cwd: Option<&Path>,+ base: &str,+) -> String {+ let mut cmd = hermetic(BIN);+ cmd.args(args);+ if let Some(cwd) = cwd {+ cmd.current_dir(cwd);+ }+ let out = cmd+ .env("AI_MEMORY_DATA_DIR", data_dir)+ .env("AI_MEMORY_HOME", home)+ .env("AI_MEMORY_SERVER_URL", base)+ .env("AI_MEMORY_EMBEDDING_PROVIDER", "none")+ .env("RUST_LOG", "off")+ .output()+ .unwrap_or_else(|e| panic!("spawn `{}`: {e}", args.join(" ")));+ assert!(+ out.status.success(),+ "`{}` failed: {}\nstdout: {}\nstderr: {}",+ args.join(" "),+ out.status,+ String::from_utf8_lossy(&out.stdout),+ String::from_utf8_lossy(&out.stderr),+ );+ String::from_utf8(out.stdout).expect("stdout utf8")+}diff --git a/crates/ai-memory-cli/tests/suite/main.rs b/crates/ai-memory-cli/tests/suite/main.rsindex f4338ab0..15a6e194 100644--- a/crates/ai-memory-cli/tests/suite/main.rs+++ b/crates/ai-memory-cli/tests/suite/main.rs@@ -9,6 +9,7 @@ mod autoscope_env;mod backfill_e2e;mod completions;mod doctor_e2e;+mod e2e_support;mod hook_drain;mod hook_payload;mod marker_scope;diff --git a/crates/ai-memory-cli/tests/suite/message_e2e.rs b/crates/ai-memory-cli/tests/suite/message_e2e.rsindex 2c1b6c4c..d344e408 100644--- a/crates/ai-memory-cli/tests/suite/message_e2e.rs+++ b/crates/ai-memory-cli/tests/suite/message_e2e.rs@@ -28,13 +28,14 @@/// Spawns a server and several subprocesses: seconds, not milliseconds, so this/// lives in the slow tier (`cargo tf` / CI), not the everyday loop.mod slow {- use std::net::TcpListener;use std::path::Path;- use std::process::{Child, Command, Stdio};+ use std::process::Stdio;use std::time::{Duration, Instant};use serde_json::{Value, json};+ use crate::e2e_support::{ServerGuard, free_port, hermetic, run_cli, session_count};+const BIN: &str = env!("CARGO_BIN_EXE_ai-memory");const WORKSPACE: &str = "message-e2e-ws";/// The sender project (the baked "current project" of the spawned server).@@ -44,65 +45,6 @@ mod slow {/// A third, uninvolved project — it must never see A→B mail.const BYSTANDER: &str = "message-e2e-c";- /// 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 this test sets. Without this the spawned- /// server would inherit an auth token and reject the test's own requests.- fn hermetic(program: &str) -> Command {- let mut cmd = Command::new(program);- for (key, _) in std::env::vars_os() {- if key.to_string_lossy().starts_with("AI_MEMORY_") {- cmd.env_remove(key);- }- }- cmd- }-- /// Kill the spawned server when the test ends, pass or fail.- struct ServerGuard(Child);- impl Drop for ServerGuard {- fn drop(&mut self) {- let _ = self.0.kill();- let _ = self.0.wait();- }- }-- /// A free loopback port. The brief unbind→rebind race is acceptable for a- /// slow-tier test and is the same approach the shell smoke test uses.- fn free_port() -> u16 {- TcpListener::bind("127.0.0.1:0")- .expect("bind ephemeral port")- .local_addr()- .expect("local addr")- .port()- }-- /// Sum the server's per-agent session counts for a scope. A 404 (the- /// no-create scope lookup for a project that has never been written to)- /// counts as zero — the pre-existence state.- async fn session_count(client: &reqwest::Client, base: &str, project: &str) -> u64 {- let resp = client- .get(format!("{base}/admin/sessions/by-agent"))- .query(&[("workspace", WORKSPACE), ("project", project)])- .send()- .await- .expect("by-agent request");- if !resp.status().is_success() {- return 0;- }- let body: Value = resp.json().await.expect("by-agent json");- body["by_agent"]- .as_array()- .map(|agents| {- agents- .iter()- .filter_map(|a| a["sessions"].as_u64())- .sum::<u64>()- })- .unwrap_or(0)- }-/// Fire one `session-start` hook for `project`, then wait until the server/// has committed it — which both creates the project scope (so a later send/// can resolve it with the no-create lookup) and is the docs' "an agent ran@@ -139,7 +81,7 @@ mod slow {);let deadline = Instant::now() + Duration::from_secs(10);loop {- if session_count(client, base, project).await >= 1 {+ if session_count(client, base, WORKSPACE, project).await >= 1 {break;}assert!(@@ -150,30 +92,6 @@ mod slow {}}- /// Run a subcommand of the built binary to completion, returning its stdout.- /// The scope/server/home environment is shared with the spawned server so- /// the client talks to the same store the way a real install does.- fn run_cli(args: &[&str], data_dir: &Path, home: &Path, base: &str) -> String {- let out = hermetic(BIN)- .args(args)- .env("AI_MEMORY_DATA_DIR", data_dir)- .env("AI_MEMORY_HOME", home)- .env("AI_MEMORY_SERVER_URL", base)- .env("AI_MEMORY_EMBEDDING_PROVIDER", "none")- .env("RUST_LOG", "off")- .output()- .unwrap_or_else(|e| panic!("spawn `{}`: {e}", args.join(" ")));- assert!(- out.status.success(),- "`{}` failed: {}\nstdout: {}\nstderr: {}",- args.join(" "),- out.status,- String::from_utf8_lossy(&out.stdout),- String::from_utf8_lossy(&out.stderr),- );- String::from_utf8(out.stdout).expect("stdout utf8")- }-/// `ai-memory message send` A→B with `--json`, returning the new message id.fn send(data_dir: &Path, home: &Path, base: &str, subject: &str, body: &str) -> String {let out = run_cli(@@ -195,6 +113,7 @@ mod slow {],data_dir,home,+ None,base,);let report: Value = serde_json::from_str(&out).expect("send --json report");@@ -218,7 +137,7 @@ mod slow {if outbox {args.push("--outbox");}- let out = run_cli(&args, data_dir, home, base);+ let out = run_cli(&args, data_dir, home, None, base);serde_json::from_str(&out).expect("list --json array")}@@ -328,6 +247,7 @@ mod slow {],data_dir.path(),home.path(),+ None,&base,);assert!(@@ -360,6 +280,7 @@ mod slow {],data_dir.path(),home.path(),+ None,&base,);let second: Value = serde_json::from_str(&second_pop).expect("second pop --json");@@ -397,6 +318,7 @@ mod slow {],data_dir.path(),home.path(),+ None,&base,);let cancel: Value = serde_json::from_str(&cancel).expect("cancel --json");