Skip to content

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

Rust

feat(uninstall): all-or-nothing --purge-data guard + shared wipe; apply test

wlech2c2f945355ab
diff --git a/crates/ai-memory-cli/src/commands/uninstall.rs b/crates/ai-memory-cli/src/commands/uninstall.rs
index 87d8f92d..579bedbb 100644
--- a/crates/ai-memory-cli/src/commands/uninstall.rs
+++ b/crates/ai-memory-cli/src/commands/uninstall.rs
@@ -11,7 +11,7 @@ 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::{install_hooks, install_mcp};
+use crate::commands::{data_purge, install_hooks, install_mcp};
use crate::config::{Config, DEFAULT_MCP_URL};
use ai_memory_core::{MARKER_END, MARKER_START};
use anyhow::{Context, Result};
@@ -214,6 +214,17 @@ pub fn run(config: &Config, args: UninstallArgs) -> anyhow::Result<()> {
return Ok(());
}
+ // All-or-nothing: when we're going to purge data, refuse before touching
+ // anything if an ai-memory process is alive (matches reset's guard-at-top).
+ // Wiring-only uninstall stays unguarded — it edits agent config files the
+ // server never touches.
+ if args.purge_data {
+ let siblings = crate::process_guard::sibling_processes();
+ if !siblings.is_empty() {
+ anyhow::bail!(crate::process_guard::busy_message("purge data", &siblings));
+ }
+ }
+
if std::io::stdin().is_terminal() && !args.yes {
eprint!("Proceed with removal? [y/N] ");
use std::io::Write as _;
@@ -230,35 +241,14 @@ pub fn run(config: &Config, args: UninstallArgs) -> anyhow::Result<()> {
apply_change(change, name, url, args.only)?;
}
- let mut purge_refused = false;
if args.purge_data {
- let siblings = crate::process_guard::sibling_processes();
- if !siblings.is_empty() {
- eprintln!(
- "{}",
- crate::process_guard::busy_message("purge data", &siblings)
- );
- eprintln!("wiring was removed, but data was NOT purged (process alive).");
- purge_refused = true;
- } else {
- for sub in ["wiki", "db", "raw"] {
- let path = config.data_dir.join(sub);
- if path.exists() {
- std::fs::remove_dir_all(&path)
- .with_context(|| format!("removing {}", path.display()))?;
- std::fs::create_dir_all(&path)
- .with_context(|| format!("recreating {}", path.display()))?;
- println!("✓ purged {}", path.display());
- }
- }
+ for path in data_purge::purge_data_dirs(&config.data_dir)? {
+ println!("✓ purged {}", path.display());
}
}
- print_docker_hint(args.purge_data && !purge_refused);
+ print_docker_hint(args.purge_data);
- if purge_refused {
- anyhow::bail!("uninstall completed wiring removal but could not purge data");
- }
Ok(())
}
diff --git a/crates/ai-memory-cli/tests/uninstall.rs b/crates/ai-memory-cli/tests/uninstall.rs
index b285fb75..7f4d11cc 100644
--- a/crates/ai-memory-cli/tests/uninstall.rs
+++ b/crates/ai-memory-cli/tests/uninstall.rs
@@ -145,3 +145,38 @@ fn uninstall_dry_run_changes_nothing() {
let after = std::fs::read_to_string(claude.join("settings.json")).unwrap();
assert_eq!(after, original, "dry-run must not modify the file");
}
+
+#[test]
+fn uninstall_purge_data_apply_wipes() {
+ let home = tempfile::tempdir().unwrap();
+ let data = tempfile::tempdir().unwrap();
+ for sub in ["wiki", "db", "raw"] {
+ std::fs::create_dir_all(data.path().join(sub)).unwrap();
+ std::fs::write(data.path().join(sub).join("f.txt"), b"x").unwrap();
+ }
+ std::fs::create_dir_all(data.path().join("logs")).unwrap();
+ std::fs::write(data.path().join("logs/app.log"), b"l").unwrap();
+
+ let out = Command::new(bin())
+ .args(["uninstall", "--apply", "--yes", "--purge-data"])
+ .env("HOME", home.path())
+ .env("AI_MEMORY_DATA_DIR", data.path())
+ .output()
+ .unwrap();
+ assert!(
+ out.status.success(),
+ "stderr: {}",
+ String::from_utf8_lossy(&out.stderr)
+ );
+
+ for sub in ["wiki", "db", "raw"] {
+ assert!(data.path().join(sub).is_dir(), "{sub} dir should remain");
+ assert!(
+ !data.path().join(sub).join("f.txt").exists(),
+ "{sub} emptied"
+ );
+ }
+ assert!(data.path().join("logs/app.log").exists(), "logs preserved");
+ let stdout = String::from_utf8(out.stdout).unwrap();
+ assert!(stdout.contains("✓ purged"), "stdout was: {stdout}");
+}