Skip to content

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

Rust

fix(uninstall): respect --only in apply_change; drop stale allow; add idempotency + scope tests

wlech4617abfaf1c5
diff --git a/crates/ai-memory-cli/src/commands/install_mcp.rs b/crates/ai-memory-cli/src/commands/install_mcp.rs
index 40fd3680..b5a38af5 100644
--- a/crates/ai-memory-cli/src/commands/install_mcp.rs
+++ b/crates/ai-memory-cli/src/commands/install_mcp.rs
@@ -64,8 +64,6 @@ pub fn run(config: &Config, args: InstallMcpArgs) -> Result<()> {
/// # Errors
/// Returns an error for `Pi` (no MCP config), for Claude Desktop on
/// unsupported OSes, or when `$HOME` can't be resolved.
-// used by uninstall in a later task
-#[allow(dead_code)]
pub(crate) fn mcp_config_path(client: crate::cli::McpClient) -> Result<PathBuf> {
use crate::cli::McpClient;
let home = dirs::home_dir().context("could not locate $HOME for config-file auto-detect")?;
diff --git a/crates/ai-memory-cli/src/commands/uninstall.rs b/crates/ai-memory-cli/src/commands/uninstall.rs
index d65c8c3c..87d8f92d 100644
--- a/crates/ai-memory-cli/src/commands/uninstall.rs
+++ b/crates/ai-memory-cli/src/commands/uninstall.rs
@@ -142,7 +142,15 @@ fn print_plan(plan: &[PlannedChange]) {
/// Re-run the matching stripper inside `apply_atomic` so the actual
/// write is atomic + backed up. The stripper is chosen by filename.
-fn apply_change(change: &PlannedChange, name: &str, url: &str) -> anyhow::Result<()> {
+/// `only` gates which strippers run in the JSON `else` branch so that
+/// `--only hooks` does not accidentally strip `mcpServers` from a
+/// shared file (e.g. `~/.gemini/settings.json`).
+fn apply_change(
+ change: &PlannedChange,
+ name: &str,
+ url: &str,
+ only: Option<crate::cli::UninstallOnly>,
+) -> anyhow::Result<()> {
match change {
PlannedChange::DeleteFile { path } => {
std::fs::remove_file(path).with_context(|| format!("deleting {}", path.display()))?;
@@ -152,20 +160,31 @@ fn apply_change(change: &PlannedChange, name: &str, url: &str) -> anyhow::Result
let file = path.file_name().and_then(|f| f.to_str()).unwrap_or("");
let outcome = apply_atomic(path, |existing| {
if file == "CLAUDE.md" || file == "AGENTS.md" {
+ // build_plan only puts these in the plan under
+ // Instructions; no gating needed here.
Ok(strip_instructions_block(existing).0)
} else if file == "config.toml" {
+ // build_plan only puts config.toml in the plan under
+ // Mcp; no gating needed here.
Ok(strip_mcp_toml(existing, name, url)?.0)
} else {
- // hooks settings/hooks.json OR an mcpServers JSON file:
- // run BOTH strippers; each is a no-op if its key is absent.
- let after_hooks = strip_ai_memory_hooks(existing)?.new_content;
- let mut out = after_hooks;
- for client in [
- crate::cli::McpClient::ClaudeCode,
- crate::cli::McpClient::OpenCode,
- crate::cli::McpClient::Openclaw,
- ] {
- out = strip_mcp_json(&out, client, name, url)?.0;
+ // hooks settings/hooks.json OR a shared file with both
+ // hooks and mcpServers (e.g. ~/.gemini/settings.json).
+ // Gate each stripper on the --only filter so that
+ // `--only hooks` never strips mcpServers and vice-versa.
+ use crate::cli::UninstallOnly;
+ let mut out = existing.to_string();
+ if only.is_none() || only == Some(UninstallOnly::Hooks) {
+ out = strip_ai_memory_hooks(&out)?.new_content;
+ }
+ if only.is_none() || only == Some(UninstallOnly::Mcp) {
+ for client in [
+ crate::cli::McpClient::ClaudeCode,
+ crate::cli::McpClient::OpenCode,
+ crate::cli::McpClient::Openclaw,
+ ] {
+ out = strip_mcp_json(&out, client, name, url)?.0;
+ }
}
Ok(out)
}
@@ -208,7 +227,7 @@ pub fn run(config: &Config, args: UninstallArgs) -> anyhow::Result<()> {
}
for change in &plan {
- apply_change(change, name, url)?;
+ apply_change(change, name, url, args.only)?;
}
let mut purge_refused = false;
diff --git a/crates/ai-memory-cli/tests/uninstall.rs b/crates/ai-memory-cli/tests/uninstall.rs
index 8940b48d..b285fb75 100644
--- a/crates/ai-memory-cli/tests/uninstall.rs
+++ b/crates/ai-memory-cli/tests/uninstall.rs
@@ -57,6 +57,76 @@ fn install_then_uninstall_round_trip_claude_hooks() {
}
}
+#[test]
+fn uninstall_apply_is_idempotent() {
+ let home = tempfile::tempdir().unwrap();
+ let claude = home.path().join(".claude");
+ std::fs::create_dir_all(&claude).unwrap();
+ std::fs::write(
+ claude.join("settings.json"),
+ r#"{"hooks":{"Stop":[{"matcher":"","hooks":[{"type":"command","command":"AI_MEMORY_HOOK_URL=http://h /x/stop.sh"}]}]}}"#,
+ )
+ .unwrap();
+
+ let run = || {
+ std::process::Command::new(bin())
+ .args(["uninstall", "--apply", "--only", "hooks", "--yes"])
+ .env("HOME", home.path())
+ .status()
+ .unwrap()
+ };
+
+ assert!(run().success(), "first uninstall");
+ // Count backups after first run.
+ let count_baks = || {
+ std::fs::read_dir(&claude)
+ .unwrap()
+ .flatten()
+ .filter(|e| e.file_name().to_string_lossy().contains(".bak-"))
+ .count()
+ };
+ let after_first = count_baks();
+ assert!(run().success(), "second uninstall (idempotent)");
+ assert_eq!(
+ count_baks(),
+ after_first,
+ "second run must not create a new backup"
+ );
+}
+
+#[test]
+fn only_hooks_preserves_mcp_in_same_file() {
+ // Gemini-style: hooks + mcpServers in one settings.json.
+ let home = tempfile::tempdir().unwrap();
+ let gem = home.path().join(".gemini");
+ std::fs::create_dir_all(&gem).unwrap();
+ std::fs::write(
+ gem.join("settings.json"),
+ r#"{"hooks":{"SessionStart":[{"matcher":"","hooks":[{"type":"command","command":"AI_MEMORY_HOOK_URL=http://h /x/session-start.sh"}]}]},"mcpServers":{"ai-memory":{"httpUrl":"http://127.0.0.1:49374/mcp"}}}"#,
+ )
+ .unwrap();
+
+ let status = std::process::Command::new(bin())
+ .args(["uninstall", "--apply", "--only", "hooks", "--yes"])
+ .env("HOME", home.path())
+ .status()
+ .unwrap();
+ assert!(status.success());
+
+ let v: serde_json::Value =
+ serde_json::from_str(&std::fs::read_to_string(gem.join("settings.json")).unwrap()).unwrap();
+ // Hooks removed...
+ assert!(
+ v["hooks"].get("SessionStart").is_none(),
+ "hook should be removed"
+ );
+ // ...but the MCP entry must SURVIVE because --only hooks.
+ assert!(
+ v["mcpServers"].get("ai-memory").is_some(),
+ "--only hooks must NOT touch mcpServers"
+ );
+}
+
#[test]
fn uninstall_dry_run_changes_nothing() {
let home = tempfile::tempdir().unwrap();