Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
test(uninstall): install→uninstall round-trip + dry-run integration tests; changelog
wlecha9eb77fadde7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bf09b2e6..e5750eb1 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## [Unreleased]+### Added+- `ai-memory uninstall` — removes ai-memory's hooks, MCP registration, and+ CLAUDE.md/AGENTS.md instruction block across all detected agents (dry-run by+ default; `--apply` to execute, with timestamped backups). `--purge-data`+ wipes wiki/db/raw via the reset guard. `--only hooks|mcp|instructions` to+ narrow. Docker/volume teardown is printed as a hint, not executed.+## [0.1.3] - 2026-05-24### Addeddiff --git a/crates/ai-memory-cli/src/commands/uninstall.rs b/crates/ai-memory-cli/src/commands/uninstall.rsindex 78bec6d8..d65c8c3c 100644--- a/crates/ai-memory-cli/src/commands/uninstall.rs+++ b/crates/ai-memory-cli/src/commands/uninstall.rs@@ -6,17 +6,17 @@//!//! Design: docs/superpowers/specs/2026-05-24-uninstall-command-design.md-use std::path::PathBuf;-use std::io::IsTerminal;-use anyhow::{Context, Result};-use ai_memory_core::{MARKER_END, MARKER_START};+use crate::cli::McpClient;+use crate::cli::UninstallArgs;+use crate::commands::apply_shared::apply_atomic;use crate::commands::apply_shared::mutate_json;use crate::commands::apply_shared::mutate_toml;-use crate::commands::apply_shared::apply_atomic;use crate::commands::{install_hooks, install_mcp};-use crate::cli::McpClient;-use crate::cli::UninstallArgs;use crate::config::{Config, DEFAULT_MCP_URL};+use ai_memory_core::{MARKER_END, MARKER_START};+use anyhow::{Context, Result};+use std::io::IsTerminal;+use std::path::PathBuf;/// One file the uninstall will touch, plus what it will do to it.#[derive(Debug)]@@ -67,7 +67,15 @@ fn build_plan(args: &UninstallArgs) -> anyhow::Result<Vec<PlannedChange>> {// ---- MCP (per client) ----if want(crate::cli::UninstallOnly::Mcp) {use crate::cli::McpClient::*;- for client in [ClaudeCode, Codex, OpenCode, Cursor, ClaudeDesktop, GeminiCli, Openclaw] {+ for client in [+ ClaudeCode,+ Codex,+ OpenCode,+ Cursor,+ ClaudeDesktop,+ GeminiCli,+ Openclaw,+ ] {let Ok(path) = install_mcp::mcp_config_path(client) else {continue;};@@ -119,7 +127,11 @@ fn print_plan(plan: &[PlannedChange]) {for change in plan {match change {PlannedChange::Rewrite { path, removed } => {- println!("would remove {} from {}", removed.join(", "), path.display());+ println!(+ "would remove {} from {}",+ removed.join(", "),+ path.display()+ );}PlannedChange::DeleteFile { path } => {println!("would delete {}", path.display());@@ -133,8 +145,7 @@ fn print_plan(plan: &[PlannedChange]) {fn apply_change(change: &PlannedChange, name: &str, url: &str) -> anyhow::Result<()> {match change {PlannedChange::DeleteFile { path } => {- std::fs::remove_file(path)- .with_context(|| format!("deleting {}", path.display()))?;+ std::fs::remove_file(path).with_context(|| format!("deleting {}", path.display()))?;println!("✓ deleted {}", path.display());}PlannedChange::Rewrite { path, .. } => {@@ -243,7 +254,9 @@ fn print_docker_hint(data_purged: bool) {println!(" rm -f bin/ai-memory # the wrapper script, if installed");if !data_purged {println!();- println!("Local data dir was left intact. To wipe it: `ai-memory reset --confirm` (or re-run with --purge-data).");+ println!(+ "Local data dir was left intact. To wipe it: `ai-memory reset --confirm` (or re-run with --purge-data)."+ );}}@@ -363,12 +376,7 @@ fn mcp_servers_path(client: McpClient) -> Option<&'static [&'static str]> {/// True when an MCP server entry is ai-memory's: keyed by the expected/// name, OR its url/httpUrl equals the endpoint, OR it is a/// `mcp-remote` stdio shim whose args contain the endpoint.-fn mcp_entry_is_ours(- key: &str,- entry: &serde_json::Value,- name: &str,- url: &str,-) -> bool {+fn mcp_entry_is_ours(key: &str, entry: &serde_json::Value, name: &str, url: &str) -> bool {if key == name {return true;}@@ -471,7 +479,10 @@ mod tests {let installed = format!("{original}\n{block}");let (stripped, found) = strip_instructions_block(&installed);assert!(found);- assert_eq!(stripped, original, "uninstall must restore the original file");+ assert_eq!(+ stripped, original,+ "uninstall must restore the original file"+ );}#[test]@@ -570,7 +581,12 @@ mod tests {let v: serde_json::Value = serde_json::from_str(&out.new_content).unwrap();let arr = v["hooks"]["Stop"].as_array().unwrap();assert_eq!(arr.len(), 1, "only ours removed");- assert!(arr[0]["hooks"][0]["command"].as_str().unwrap().contains("/home/u/scripts/stop.sh"));+ assert!(+ arr[0]["hooks"][0]["command"]+ .as_str()+ .unwrap()+ .contains("/home/u/scripts/stop.sh")+ );}#[test]@@ -583,7 +599,13 @@ mod tests {#[test]fn strip_mcp_claude_by_name_keeps_others() {let content = r#"{"mcpServers":{"ai-memory":{"type":"http","url":"http://127.0.0.1:49374/mcp"},"other":{"url":"http://x"}}}"#;- let (out, removed) = strip_mcp_json(content, McpClient::ClaudeCode, "ai-memory", "http://127.0.0.1:49374/mcp").unwrap();+ let (out, removed) = strip_mcp_json(+ content,+ McpClient::ClaudeCode,+ "ai-memory",+ "http://127.0.0.1:49374/mcp",+ )+ .unwrap();assert_eq!(removed, vec!["ai-memory".to_string()]);let v: serde_json::Value = serde_json::from_str(&out).unwrap();assert!(v["mcpServers"].get("ai-memory").is_none());@@ -593,23 +615,48 @@ mod tests {#[test]fn strip_mcp_by_endpoint_under_custom_name() {let content = r#"{"mcpServers":{"my-mem":{"url":"http://127.0.0.1:49374/mcp"}}}"#;- let (out, removed) = strip_mcp_json(content, McpClient::ClaudeCode, "ai-memory", "http://127.0.0.1:49374/mcp").unwrap();- assert_eq!(removed, vec!["my-mem".to_string()], "matched by endpoint, not name");+ let (out, removed) = strip_mcp_json(+ content,+ McpClient::ClaudeCode,+ "ai-memory",+ "http://127.0.0.1:49374/mcp",+ )+ .unwrap();+ assert_eq!(+ removed,+ vec!["my-mem".to_string()],+ "matched by endpoint, not name"+ );let v: serde_json::Value = serde_json::from_str(&out).unwrap();- assert!(v.get("mcpServers").is_none(), "emptied servers object pruned");+ assert!(+ v.get("mcpServers").is_none(),+ "emptied servers object pruned"+ );}#[test]fn strip_mcp_claude_desktop_mcp_remote_args() {let content = r#"{"mcpServers":{"weird-name":{"command":"npx","args":["-y","mcp-remote","http://127.0.0.1:49374/mcp"]}}}"#;- let (_out, removed) = strip_mcp_json(content, McpClient::ClaudeDesktop, "ai-memory", "http://127.0.0.1:49374/mcp").unwrap();+ let (_out, removed) = strip_mcp_json(+ content,+ McpClient::ClaudeDesktop,+ "ai-memory",+ "http://127.0.0.1:49374/mcp",+ )+ .unwrap();assert_eq!(removed, vec!["weird-name".to_string()]);}#[test]fn strip_mcp_openclaw_nested_servers() {let content = r#"{"mcp":{"servers":{"ai-memory":{"url":"http://127.0.0.1:49374/mcp"}}}}"#;- let (out, removed) = strip_mcp_json(content, McpClient::Openclaw, "ai-memory", "http://127.0.0.1:49374/mcp").unwrap();+ let (out, removed) = strip_mcp_json(+ content,+ McpClient::Openclaw,+ "ai-memory",+ "http://127.0.0.1:49374/mcp",+ )+ .unwrap();assert_eq!(removed, vec!["ai-memory".to_string()]);let v: serde_json::Value = serde_json::from_str(&out).unwrap();assert!(v["mcp"].get("servers").is_none());@@ -618,14 +665,21 @@ mod tests {#[test]fn strip_mcp_no_match_is_noop() {let content = r#"{"mcpServers":{"other":{"url":"http://x"}}}"#;- let (_out, removed) = strip_mcp_json(content, McpClient::ClaudeCode, "ai-memory", "http://127.0.0.1:49374/mcp").unwrap();+ let (_out, removed) = strip_mcp_json(+ content,+ McpClient::ClaudeCode,+ "ai-memory",+ "http://127.0.0.1:49374/mcp",+ )+ .unwrap();assert!(removed.is_empty());}#[test]fn strip_mcp_toml_by_name_keeps_comments_and_tables() {let content = "# my codex config\n[other]\nkeep = true\n\n[mcp_servers.ai-memory]\nurl = \"http://127.0.0.1:49374/mcp\"\n";- let (out, removed) = strip_mcp_toml(content, "ai-memory", "http://127.0.0.1:49374/mcp").unwrap();+ let (out, removed) =+ strip_mcp_toml(content, "ai-memory", "http://127.0.0.1:49374/mcp").unwrap();assert_eq!(removed, vec!["ai-memory".to_string()]);assert!(out.contains("# my codex config"));assert!(out.contains("[other]"));@@ -635,7 +689,8 @@ mod tests {#[test]fn strip_mcp_toml_by_url_under_custom_name() {let content = "[mcp_servers.custom]\nurl = \"http://127.0.0.1:49374/mcp\"\n";- let (out, removed) = strip_mcp_toml(content, "ai-memory", "http://127.0.0.1:49374/mcp").unwrap();+ let (out, removed) =+ strip_mcp_toml(content, "ai-memory", "http://127.0.0.1:49374/mcp").unwrap();assert_eq!(removed, vec!["custom".to_string()]);assert!(!out.contains("custom"));}@@ -643,7 +698,8 @@ mod tests {#[test]fn strip_mcp_toml_no_match_is_noop() {let content = "[mcp_servers.other]\nurl = \"http://x\"\n";- let (_out, removed) = strip_mcp_toml(content, "ai-memory", "http://127.0.0.1:49374/mcp").unwrap();+ let (_out, removed) =+ strip_mcp_toml(content, "ai-memory", "http://127.0.0.1:49374/mcp").unwrap();assert!(removed.is_empty());}}diff --git a/crates/ai-memory-cli/tests/uninstall.rs b/crates/ai-memory-cli/tests/uninstall.rsnew file mode 100644index 00000000..8940b48d--- /dev/null+++ b/crates/ai-memory-cli/tests/uninstall.rs@@ -0,0 +1,77 @@+//! End-to-end: install hooks into a temp HOME, then uninstall, and+//! assert the file round-trips (our entries gone, third-party intact).++use std::process::Command;++fn bin() -> &'static str {+ env!("CARGO_BIN_EXE_ai-memory")+}++#[test]+fn install_then_uninstall_round_trip_claude_hooks() {+ let home = tempfile::tempdir().unwrap();+ let claude = home.path().join(".claude");+ std::fs::create_dir_all(&claude).unwrap();+ // Pre-seed a third-party hook we must NOT touch.+ std::fs::write(+ claude.join("settings.json"),+ r#"{"hooks":{"Notification":[{"matcher":"","hooks":[{"type":"command","command":"/usr/bin/n.sh"}]}]}}"#,+ )+ .unwrap();++ // Install ai-memory hooks for Claude Code.+ let status = Command::new(bin())+ .args(["install-hooks", "--agent", "claude-code", "--apply"])+ .env("HOME", home.path())+ .status()+ .unwrap();+ assert!(status.success(), "install-hooks failed");++ // Uninstall (hooks only) and verify.+ let status = Command::new(bin())+ .args(["uninstall", "--apply", "--only", "hooks", "--yes"])+ .env("HOME", home.path())+ .status()+ .unwrap();+ assert!(status.success(), "uninstall failed");++ let after: serde_json::Value =+ serde_json::from_str(&std::fs::read_to_string(claude.join("settings.json")).unwrap())+ .unwrap();+ // Third-party hook survived.+ assert!(after["hooks"]["Notification"].is_array());+ // None of our events remain.+ for ours in [+ "SessionStart",+ "SessionEnd",+ "PreToolUse",+ "PostToolUse",+ "Stop",+ "PreCompact",+ "UserPromptSubmit",+ ] {+ assert!(+ after["hooks"].get(ours).is_none(),+ "{ours} should be removed"+ );+ }+}++#[test]+fn uninstall_dry_run_changes_nothing() {+ let home = tempfile::tempdir().unwrap();+ let claude = home.path().join(".claude");+ std::fs::create_dir_all(&claude).unwrap();+ let original = r#"{"hooks":{"Stop":[{"matcher":"","hooks":[{"type":"command","command":"AI_MEMORY_HOOK_URL=x /a/stop.sh"}]}]}}"#;+ std::fs::write(claude.join("settings.json"), original).unwrap();++ let status = Command::new(bin())+ .args(["uninstall", "--only", "hooks"]) // no --apply+ .env("HOME", home.path())+ .status()+ .unwrap();+ assert!(status.success());++ let after = std::fs::read_to_string(claude.join("settings.json")).unwrap();+ assert_eq!(after, original, "dry-run must not modify the file");+}