Skip to content

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

Rust
128 KB
Raw Blame
//! `ai-memory uninstall` — the symmetric inverse of install-hooks /
//! install-mcp / install-instructions. Detects ai-memory's wiring in
//! every supported agent's config and removes only that, never
//! third-party entries. Optional `--purge-data` wipes wiki/db/raw via
//! the reset path. Docker teardown is printed, never executed.
//!
//! Design: docs/superpowers/specs/2026-05-24-uninstall-command-design.md

use crate::cli::McpClient;
use crate::cli::UninstallArgs;
use crate::commands::apply_shared::mutate_json;
use crate::commands::apply_shared::mutate_toml;
use crate::commands::apply_shared::{PrivateBackup, apply_atomic_with_backup};
use crate::commands::path_util::{
    agent_config_home, claude_config_dir, claude_config_paths, home_dir,
};
use crate::commands::user_service::{self, ServiceManager};
use crate::commands::{agent_plugins, data_purge, install_hooks, install_mcp, openclaw_plugin};
use crate::config::Config;
use ai_memory_core::routing_skills::{
    AGENTS_SKILL_DIR, CLAUDE_SKILL_DIR, COPILOT_SKILL_DIR, DEVIN_SKILL_DIR, GITHUB_SKILL_DIR,
    GROK_HANDOFF_RULE_FILENAME, GROK_HANDOFF_RULE_MARKER, GROK_RULES_DIR, GROK_SKILL_DIR,
    MANAGED_MARKER, MANAGED_SKILLS, SKILLS_DIR,
};
use ai_memory_core::{MARKER_END, MARKER_START, find_marker_line};
use anyhow::{Context, Result};
use std::io::IsTerminal;
use std::path::{Path, PathBuf};

const LEGACY_ORPHAN_TAIL_LF: &str =
    "` markers without\ndisturbing the rest of the file.\n<!-- ai-memory:end -->\n";
const LEGACY_ORPHAN_TAIL_CRLF: &str =
    "` markers without\r\ndisturbing the rest of the file.\r\n<!-- ai-memory:end -->\r\n";

/// One rewrite operation to apply to a config file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RewriteOp {
    /// CLAUDE.md / AGENTS.md routing block.
    Instructions,
    /// Standard JSON hook table under `hooks`.
    HooksJson(HookConfigShape),
    /// Antigravity CLI named hook group under top-level `ai-memory`.
    AntigravityHooksJson,
    /// Zero hooks.json — `hooks` array entries whose `id` carries the
    /// `ai-memory-` prefix.
    ZeroHooksJson,
    /// Kimi Code `[[hooks]]` rules inside config.toml.
    KimiCodeHooksToml,
    /// Kiro CLI v2 agent-config hooks with exact generated command signatures.
    KiroCliV2HooksJson,
    /// Kiro CLI v3 standalone hooks with exact generated names and commands.
    KiroCliV3HooksJson,
    /// MCP JSON config for one client shape.
    McpJson(McpClient),
    /// Codex TOML MCP config.
    McpToml,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum HookConfigShape {
    /// Standard agent configs: `{ "hooks": { "Event": [...] } }`.
    NestedHooksKey,
    /// Devin `hooks.v1.json`: the file itself is `{ "Event": [...] }`.
    FlatHookFile,
}

/// Generated files that uninstall may delete after content re-validation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DeleteKind {
    OpenCodePlugin,
    OpenCode2Plugin,
    PiExtension,
    OmpExtension,
    OpenClawPackageJson,
    OpenClawManifest,
    OpenClawEntrypoint,
    KiroCliV3Hooks,
    ClineHook,
    ManagedSkill,
    GrokHandoffRule,
    AutowireSentinel,
}

impl DeleteKind {
    const fn label(self) -> &'static str {
        match self {
            Self::OpenCodePlugin => "OpenCode plugin",
            Self::OpenCode2Plugin => "OpenCode 2 plugin",
            Self::PiExtension => "Pi extension",
            Self::OmpExtension => "OMP extension",
            Self::OpenClawPackageJson => "OpenClaw package manifest",
            Self::OpenClawManifest => "OpenClaw plugin manifest",
            Self::OpenClawEntrypoint => "OpenClaw plugin entrypoint",
            Self::KiroCliV3Hooks => "Kiro CLI v3 hook file",
            Self::ClineHook => "Cline CLI hook file",
            Self::ManagedSkill => "managed Agent Skill",
            Self::GrokHandoffRule => "managed Grok first-turn handoff rule",
            Self::AutowireSentinel => "auto-wire sentinel",
        }
    }
}

/// One file the uninstall will touch, plus what it will do to it.
#[derive(Debug)]
enum PlannedChange {
    /// JSON/TOML rewrite removing the listed items (events or server names).
    Rewrite {
        path: PathBuf,
        removed: Vec<String>,
        ops: Vec<RewriteOp>,
    },
    /// Whole-file delete, limited to generated files whose contents still
    /// prove they are ai-memory-owned at apply time.
    DeleteFile { path: PathBuf, kind: DeleteKind },
}

fn push_rewrite(plan: &mut Vec<PlannedChange>, path: PathBuf, removed: Vec<String>, op: RewriteOp) {
    if removed.is_empty() {
        return;
    }
    for change in plan.iter_mut() {
        if let PlannedChange::Rewrite {
            path: existing,
            removed: existing_removed,
            ops,
        } = change
            && *existing == path
        {
            existing_removed.extend(removed);
            if !ops.contains(&op) {
                ops.push(op);
            }
            return;
        }
    }
    plan.push(PlannedChange::Rewrite {
        path,
        removed,
        ops: vec![op],
    });
}

/// The OMP agent dirs an ai-memory install may have written to, active first:
/// the one OMP loads now (`--profile`, `OMP_PROFILE`, `PI_PROFILE`, else
/// `PI_CODING_AGENT_DIR`), the default profile's, where earlier releases put
/// the extension whenever `PI_CODING_AGENT_DIR` was set, the active one as
/// earlier releases resolved it (they ignored `PI_CONFIG_DIR` and wrote under
/// `~/.omp`), and `~/.omp/agent`, where `install-mcp` always wrote the MCP
/// entry. A profile name OMP refuses is reported and skipped instead of
/// aborting every other agent's cleanup.
fn omp_agent_dirs(home: Option<&Path>, profile: Option<&str>) -> Vec<PathBuf> {
    let Some(home) = home else {
        return Vec::new();
    };
    let env = |name: &str| std::env::var_os(name);
    let without_config_dir = |name: &str| (name != "PI_CONFIG_DIR").then(|| env(name)).flatten();
    let mut dirs = Vec::with_capacity(4);
    match ai_memory_workstream::omp_agent_dir(home, profile, env) {
        Ok(dir) => dirs.push(dir),
        Err(error) => eprintln!("warning: skipping the active OMP profile: {error:#}"),
    }
    let fallbacks = [
        ai_memory_workstream::omp_agent_dir(home, Some("default"), env).ok(),
        ai_memory_workstream::omp_agent_dir(home, profile, without_config_dir).ok(),
        Some(home.join(".omp").join("agent")),
    ];
    for dir in fallbacks.into_iter().flatten() {
        if !dirs.contains(&dir) {
            dirs.push(dir);
        }
    }
    dirs
}

fn push_generated_delete(plan: &mut Vec<PlannedChange>, path: PathBuf, kind: DeleteKind) {
    if generated_file_is_ours(&path, kind) {
        plan.push(PlannedChange::DeleteFile { path, kind });
    }
}

/// Build the full removal plan by reading each existing config file and
/// running the matching pure stripper. Missing files / no-matches
/// produce no entry. `name`/`url` identify the MCP server.
///
/// Harness homes (`CLAUDE_CONFIG_DIR`, `CODEX_HOME`, `COPILOT_HOME`,
/// `GROK_HOME`, `APPDATA`) come
/// from `env`, the environment `Config::load` captured (invariant #1).
fn build_plan(
    args: &UninstallArgs,
    data_dir: &Path,
    env: &crate::config::RuntimeEnv,
) -> anyhow::Result<Vec<PlannedChange>> {
    let mut plan = Vec::new();
    let want = |k: crate::cli::UninstallOnly| args.only.is_none() || args.only == Some(k);
    let name = args.mcp_name.as_deref();
    let url = args.mcp_url.as_str();
    let home = home_dir();
    let claude_config_dir = claude_config_dir(env.var_os("CLAUDE_CONFIG_DIR"));
    let omp_dirs = if want(crate::cli::UninstallOnly::Hooks) || want(crate::cli::UninstallOnly::Mcp)
    {
        omp_agent_dirs(home.as_deref(), args.profile.as_deref())
    } else {
        Vec::new()
    };

    // ---- Hooks (JSON configs) ----
    if want(crate::cli::UninstallOnly::Hooks) {
        let mut hook_files: Vec<_> = claude_config_paths(
            home.as_deref(),
            claude_config_dir.as_deref(),
            Path::new(".claude/settings.json"),
            Path::new("settings.json"),
        )
        .into_iter()
        .map(|path| (path, HookConfigShape::NestedHooksKey))
        .collect();
        // A `--scope project` install lives in the checkout's gitignored
        // `.claude/settings.local.json`; sweep the one for the current
        // directory, as the Kiro project configs below are.
        let cwd = std::env::current_dir().context("getting CWD for hook removal")?;
        let project_local = install_hooks::project_claude_settings_local(&cwd);
        if !hook_files.iter().any(|(path, _)| *path == project_local) {
            hook_files.push((project_local, HookConfigShape::NestedHooksKey));
        }
        hook_files.extend([
            // Copilot CLI's standalone file keeps flat entries under the usual
            // `hooks` key next to its `version: 1`, like Cursor's.
            (
                install_hooks::copilot_cli_hooks_path_in(env.var_os("COPILOT_HOME"))?,
                HookConfigShape::NestedHooksKey,
            ),
            (
                install_hooks::codex_hooks_path(env)?,
                HookConfigShape::NestedHooksKey,
            ),
            (
                install_hooks::cursor_hooks_path()?,
                HookConfigShape::NestedHooksKey,
            ),
            (
                install_hooks::gemini_settings_path()?,
                HookConfigShape::NestedHooksKey,
            ),
            (
                install_hooks::command_code_settings_path()?,
                HookConfigShape::NestedHooksKey,
            ),
            // Grok's ~/.grok/hooks/ai-memory.json shares Claude Code's
            // JSON shape, so the same strip pass removes our entries.
            (
                install_hooks::grok_hooks_path()?,
                HookConfigShape::NestedHooksKey,
            ),
            // Devin's default hooks.v1.json is flat: the file itself is the
            // event map. Its config.json alternative stores the same entries
            // under the usual hooks key.
            (
                install_hooks::devin_hooks_path()?,
                HookConfigShape::FlatHookFile,
            ),
            (
                install_hooks::devin_config_path()?,
                HookConfigShape::NestedHooksKey,
            ),
            // Droid's hooks.json is the event map itself, like Devin's
            // hooks.v1.json.
            (
                install_hooks::droid_hooks_path()?,
                HookConfigShape::FlatHookFile,
            ),
            // Muse keeps its hooks under the usual `hooks` key of the
            // settings.json its MCP servers live in, beside `schema_version`.
            (
                install_hooks::muse_settings_path(env)?,
                HookConfigShape::NestedHooksKey,
            ),
            (
                install_hooks::codebuddy_settings_path()?,
                HookConfigShape::NestedHooksKey,
            ),
            (
                install_hooks::ibm_bob_settings_path()?,
                HookConfigShape::NestedHooksKey,
            ),
            (
                install_hooks::qwen_code_settings_path()?,
                HookConfigShape::NestedHooksKey,
            ),
            (
                install_hooks::qoder_settings_path()?,
                HookConfigShape::NestedHooksKey,
            ),
        ]);
        for (path, shape) in hook_files {
            if !path.exists() {
                continue;
            }
            let content = std::fs::read_to_string(&path)
                .with_context(|| format!("reading {}", path.display()))?;
            let removal = match shape {
                HookConfigShape::NestedHooksKey => strip_ai_memory_hooks(&content)?,
                HookConfigShape::FlatHookFile => strip_ai_memory_hooks_flat(&content)?,
            };
            push_rewrite(
                &mut plan,
                path,
                removal.removed_events,
                RewriteOp::HooksJson(shape),
            );
        }

        let mut kiro_configs =
            install_hooks::list_kiro_cli_agent_configs(&install_hooks::kiro_cli_agents_dir()?)?;
        kiro_configs.extend(install_hooks::list_kiro_cli_agent_configs(
            &cwd.join(".kiro/agents"),
        )?);
        kiro_configs.sort();
        kiro_configs.dedup();
        for path in kiro_configs {
            let content = std::fs::read_to_string(&path)
                .with_context(|| format!("reading {}", path.display()))?;
            let removal = strip_kiro_cli_v2_hooks(&content)?;
            push_rewrite(
                &mut plan,
                path,
                removal.removed_events,
                RewriteOp::KiroCliV2HooksJson,
            );
        }

        let mut kiro_v3_paths = vec![install_hooks::kiro_cli_v3_hooks_path()?];
        kiro_v3_paths.push(cwd.join(".kiro/hooks/ai-memory.json"));
        kiro_v3_paths.sort();
        kiro_v3_paths.dedup();
        for path in kiro_v3_paths {
            if !path.exists() {
                continue;
            }
            if generated_file_is_ours(&path, DeleteKind::KiroCliV3Hooks) {
                push_generated_delete(&mut plan, path, DeleteKind::KiroCliV3Hooks);
                continue;
            }
            let content = std::fs::read_to_string(&path)
                .with_context(|| format!("reading {}", path.display()))?;
            let removal = strip_kiro_cli_v3_hooks(&content)?;
            push_rewrite(
                &mut plan,
                path,
                removal.removed_events,
                RewriteOp::KiroCliV3HooksJson,
            );
        }

        let antigravity = install_hooks::antigravity_hooks_path()?;
        if antigravity.exists() {
            let content = std::fs::read_to_string(&antigravity)
                .with_context(|| format!("reading {}", antigravity.display()))?;
            let removal = strip_antigravity_hooks(&content)?;
            push_rewrite(
                &mut plan,
                antigravity,
                removal.removed_events,
                RewriteOp::AntigravityHooksJson,
            );
        }

        let zero = install_hooks::zero_hooks_path()?;
        if zero.exists() {
            let content = std::fs::read_to_string(&zero)
                .with_context(|| format!("reading {}", zero.display()))?;
            let removal = strip_zero_hooks(&content)?;
            push_rewrite(
                &mut plan,
                zero,
                removal.removed_events,
                RewriteOp::ZeroHooksJson,
            );
        }

        let kimi_code = install_hooks::kimi_code_config_path()?;
        if kimi_code.exists() {
            let content = std::fs::read_to_string(&kimi_code)
                .with_context(|| format!("reading {}", kimi_code.display()))?;
            let removal = strip_kimi_code_hooks(&content)?;
            push_rewrite(
                &mut plan,
                kimi_code,
                removal.removed_events,
                RewriteOp::KimiCodeHooksToml,
            );
        }

        let mut cline_hook_dirs = vec![install_hooks::cline_hooks_path_in(env.var_os("HOME"))?];
        cline_hook_dirs.push(cwd.join(".cline").join("hooks"));
        cline_hook_dirs.sort();
        cline_hook_dirs.dedup();
        for dir in cline_hook_dirs {
            for (event, _) in install_hooks::CLINE_HOOK_EVENTS {
                for extension in ["sh", "ps1"] {
                    push_generated_delete(
                        &mut plan,
                        dir.join(format!("{event}.{extension}")),
                        DeleteKind::ClineHook,
                    );
                }
            }
        }

        let plugin = install_hooks::opencode_plugin_path()?;
        push_generated_delete(&mut plan, plugin, DeleteKind::OpenCodePlugin);

        let plugin2 = install_hooks::opencode2_plugin_path()?;
        push_generated_delete(&mut plan, plugin2, DeleteKind::OpenCode2Plugin);

        for dir in &omp_dirs {
            let omp = dir.join("extensions").join("ai-memory-omp.ts");
            let legacy_omp = omp.with_file_name("ai-memory.ts");
            push_generated_delete(&mut plan, omp, DeleteKind::OmpExtension);
            push_generated_delete(&mut plan, legacy_omp, DeleteKind::OmpExtension);
        }

        let pi = install_hooks::pi_extension_path()?;
        push_generated_delete(&mut plan, pi.clone(), DeleteKind::PiExtension);

        let legacy_pi = pi.with_file_name("ai-memory.ts");
        if legacy_pi != pi {
            push_generated_delete(&mut plan, legacy_pi, DeleteKind::PiExtension);
        }

        let openclaw_dir = openclaw_plugin::default_plugin_dir()?;
        push_generated_delete(
            &mut plan,
            openclaw_dir.join(openclaw_plugin::PACKAGE_JSON),
            DeleteKind::OpenClawPackageJson,
        );
        push_generated_delete(
            &mut plan,
            openclaw_dir.join(openclaw_plugin::MANIFEST_JSON),
            DeleteKind::OpenClawManifest,
        );
        push_generated_delete(
            &mut plan,
            openclaw_dir.join(openclaw_plugin::ENTRYPOINT_TS),
            DeleteKind::OpenClawEntrypoint,
        );
    }

    // ---- MCP (per client) ----
    if want(crate::cli::UninstallOnly::Mcp) {
        use crate::cli::McpClient::*;
        for client in [
            ClaudeCode,
            Codex,
            Grok,
            OpenCode,
            OpenCode2,
            Cursor,
            ClaudeDesktop,
            GeminiCli,
            Openclaw,
            Omp,
            AntigravityCli,
            Zero,
            Zcode,
            VsCodeCopilot,
            Zed,
            Devin,
            KimiCode,
            KiroCli,
            CommandCode,
            IbmBob,
            QwenCode,
            Qoder,
            CopilotCli,
            Droid,
            Swival,
            Muse,
            PrimeAgent,
            Cline,
            Freebuff,
        ] {
            let paths = if matches!(client, ClaudeCode) {
                claude_config_paths(
                    home.as_deref(),
                    claude_config_dir.as_deref(),
                    Path::new(".claude.json"),
                    Path::new(".claude.json"),
                )
            } else if matches!(client, Codex) {
                // Older installs wrote the Codex MCP entry to ~/.codex/config.toml
                // even with CODEX_HOME set, so sweep that file too.
                claude_config_paths(
                    home.as_deref(),
                    agent_config_home(env.var_os("CODEX_HOME")).as_deref(),
                    Path::new(".codex/config.toml"),
                    Path::new("config.toml"),
                )
            } else if matches!(client, Omp) {
                omp_dirs.iter().map(|dir| dir.join("mcp.json")).collect()
            } else if matches!(client, Muse) {
                vec![install_hooks::muse_settings_path(env)?]
            } else {
                let Ok(path) = install_mcp::mcp_config_path_with(client, &|name| env.var_os(name))
                else {
                    continue;
                };
                vec![path]
            };
            for path in paths {
                if !path.exists() {
                    continue;
                }
                let content = std::fs::read_to_string(&path)
                    .with_context(|| format!("reading {}", path.display()))?;
                let (_new, removed) = if matches!(client, Codex | Grok) {
                    strip_mcp_toml(&content, name, url)?
                } else {
                    strip_mcp_json_client(&content, client, name, url)?
                };
                let op = if matches!(client, Codex | Grok) {
                    RewriteOp::McpToml
                } else {
                    RewriteOp::McpJson(client)
                };
                push_rewrite(&mut plan, path, removed, op);
            }
        }
    }

    // ---- Instructions (cwd CLAUDE.md / AGENTS.md) ----
    if want(crate::cli::UninstallOnly::Instructions) {
        let cwd = std::env::current_dir().context("getting CWD for instruction removal")?;
        for name_md in ["CLAUDE.md", "AGENTS.md"] {
            let path = cwd.join(name_md);
            if !path.exists() {
                continue;
            }
            let content = std::fs::read_to_string(&path)
                .with_context(|| format!("reading {}", path.display()))?;
            let (_new, found) = strip_instructions_block(&content);
            if found {
                push_rewrite(
                    &mut plan,
                    path,
                    vec!["instruction block".to_string()],
                    RewriteOp::Instructions,
                );
            }
        }
    }

    // ---- Managed Agent Skills (project + global roots) ----
    if want(crate::cli::UninstallOnly::Skills) {
        let cwd = std::env::current_dir().context("getting CWD for skill removal")?;
        let appdata = env.var_os("APPDATA").map(PathBuf::from);
        let grok_home = install_mcp::grok_home_in(env.var_os("GROK_HOME")).ok();
        let copilot_home = install_mcp::copilot_home_in(env.var_os("COPILOT_HOME")).ok();
        for root in skill_roots(
            &cwd,
            home.as_deref(),
            appdata.as_deref(),
            grok_home.as_deref(),
            copilot_home.as_deref(),
            claude_config_dir.as_deref(),
        ) {
            for skill in MANAGED_SKILLS {
                push_generated_delete(
                    &mut plan,
                    root.join(skill.relative_path),
                    DeleteKind::ManagedSkill,
                );
            }
        }
    }

    // The Grok first-turn rule is written by both `install-hooks --agent grok`
    // and `install-skills --scope global --agent grok`. Sweep it with either
    // concern so uninstall of hooks or skills does not leave the always-on rule.
    if (want(crate::cli::UninstallOnly::Skills) || want(crate::cli::UninstallOnly::Hooks))
        && let Ok(grok_home) = install_mcp::grok_home()
    {
        push_generated_delete(
            &mut plan,
            grok_home
                .join(GROK_RULES_DIR)
                .join(GROK_HANDOFF_RULE_FILENAME),
            DeleteKind::GrokHandoffRule,
        );
    }

    // ---- Auto-wire sentinels (ai-memory's own state) ----
    // `ai-memory run` skips wiring while a sentinel exists, so one left behind
    // would stop the next managed launch from reinstalling what this removes.
    // All of them go: one keyed on a config home this environment cannot see
    // costs only an idempotent re-apply.
    if want(crate::cli::UninstallOnly::Hooks) || want(crate::cli::UninstallOnly::Mcp) {
        let dir = crate::commands::run_autowire::autowire_state_dir(data_dir);
        let mut sentinels: Vec<PathBuf> = std::fs::read_dir(&dir)
            .into_iter()
            .flatten()
            .filter_map(|entry| entry.ok().map(|entry| entry.path()))
            .collect();
        sentinels.sort();
        for path in sentinels {
            push_generated_delete(&mut plan, path, DeleteKind::AutowireSentinel);
        }
    }

    Ok(plan)
}

fn skill_roots(
    cwd: &Path,
    home: Option<&Path>,
    appdata: Option<&Path>,
    grok_home: Option<&Path>,
    copilot_home: Option<&Path>,
    claude_config_dir: Option<&Path>,
) -> Vec<PathBuf> {
    let mut roots = Vec::with_capacity(14);
    push_unique_skill_root(&mut roots, cwd.join(CLAUDE_SKILL_DIR).join(SKILLS_DIR));
    push_unique_skill_root(&mut roots, cwd.join(AGENTS_SKILL_DIR).join(SKILLS_DIR));
    push_unique_skill_root(&mut roots, cwd.join(DEVIN_SKILL_DIR).join(SKILLS_DIR));
    push_unique_skill_root(&mut roots, cwd.join(GROK_SKILL_DIR).join(SKILLS_DIR));
    push_unique_skill_root(&mut roots, cwd.join(GITHUB_SKILL_DIR).join(SKILLS_DIR));
    if let Some(home) = home {
        push_unique_skill_root(&mut roots, home.join(CLAUDE_SKILL_DIR).join(SKILLS_DIR));
        push_unique_skill_root(&mut roots, home.join(AGENTS_SKILL_DIR).join(SKILLS_DIR));
        push_unique_skill_root(&mut roots, home.join(DEVIN_SKILL_DIR).join(SKILLS_DIR));
        let grok_root = grok_home
            .map(Path::to_path_buf)
            .unwrap_or_else(|| home.join(GROK_SKILL_DIR));
        push_unique_skill_root(&mut roots, grok_root.join(SKILLS_DIR));
        let copilot_root = copilot_home
            .map(Path::to_path_buf)
            .unwrap_or_else(|| home.join(COPILOT_SKILL_DIR));
        push_unique_skill_root(&mut roots, copilot_root.join(SKILLS_DIR));
        for root in super::install_skills::antigravity_global_skill_roots(home) {
            push_unique_skill_root(&mut roots, root);
        }
    }
    // Windows global Devin installs live under %APPDATA%\devin\skills, not
    // $HOME/.devin/skills — sweep it too or uninstall orphans those skills.
    if let Some(appdata) = appdata {
        push_unique_skill_root(&mut roots, appdata.join("devin").join(SKILLS_DIR));
    }
    // With CLAUDE_CONFIG_DIR set, global Claude Code skills install to
    // `$CLAUDE_CONFIG_DIR/skills`. Sweep it alongside ~/.claude/skills —
    // installs may predate the env var.
    if let Some(claude_config_dir) = claude_config_dir {
        push_unique_skill_root(&mut roots, claude_config_dir.join(SKILLS_DIR));
    }
    roots
}

fn push_unique_skill_root(roots: &mut Vec<PathBuf>, root: PathBuf) {
    if !roots.iter().any(|existing| existing == &root) {
        roots.push(root);
    }
}

/// Print the plan, one line per file, mirroring `reset`'s dry-run style.
fn print_plan(plan: &[PlannedChange]) {
    if plan.is_empty() {
        println!("Nothing to remove. ai-memory wiring not found.");
        return;
    }
    for change in plan {
        match change {
            PlannedChange::Rewrite { path, removed, .. } => {
                println!(
                    "would remove {} from {}",
                    removed.join(", "),
                    path.display()
                );
            }
            PlannedChange::DeleteFile { path, kind } => {
                println!("would delete {} ({})", path.display(), kind.label());
            }
        }
    }
}

/// Re-run the planned strippers inside `apply_atomic` so the actual write is
/// atomic + backed up. Planning records exact operations per file, so shared
/// files such as `~/.gemini/settings.json` only apply the selected concerns.
fn apply_change(
    change: &PlannedChange,
    private_backup: Option<&PrivateBackup>,
    name: Option<&str>,
    url: &str,
) -> anyhow::Result<()> {
    match change {
        PlannedChange::DeleteFile { path, kind } => {
            if !path.exists() {
                return Ok(());
            }
            if !generated_file_is_ours(path, *kind) {
                println!(
                    "skipped {} because it no longer looks like an ai-memory-generated {}",
                    path.display(),
                    kind.label()
                );
                return Ok(());
            }
            std::fs::remove_file(path).with_context(|| format!("deleting {}", path.display()))?;
            println!("✓ deleted {}", path.display());
            if *kind == DeleteKind::ManagedSkill {
                remove_empty_skill_dirs(path)?;
            }
            if *kind == DeleteKind::GrokHandoffRule
                && let Some(parent) = path.parent()
            {
                remove_dir_if_empty(parent)?;
            }
            if *kind == DeleteKind::ClineHook
                && let Some(parent) = path.parent()
            {
                remove_dir_if_empty(parent)?;
            }
        }
        PlannedChange::Rewrite { path, ops, .. } => {
            let outcome = apply_atomic_with_backup(path, private_backup, |existing| {
                let mut out = existing.to_string();
                for op in ops {
                    out = match *op {
                        RewriteOp::Instructions => strip_instructions_block(&out).0,
                        RewriteOp::HooksJson(HookConfigShape::NestedHooksKey) => {
                            strip_ai_memory_hooks(&out)?.new_content
                        }
                        RewriteOp::HooksJson(HookConfigShape::FlatHookFile) => {
                            strip_ai_memory_hooks_flat(&out)?.new_content
                        }
                        RewriteOp::AntigravityHooksJson => {
                            strip_antigravity_hooks(&out)?.new_content
                        }
                        RewriteOp::ZeroHooksJson => strip_zero_hooks(&out)?.new_content,
                        RewriteOp::KimiCodeHooksToml => strip_kimi_code_hooks(&out)?.new_content,
                        RewriteOp::KiroCliV2HooksJson => strip_kiro_cli_v2_hooks(&out)?.new_content,
                        RewriteOp::KiroCliV3HooksJson => strip_kiro_cli_v3_hooks(&out)?.new_content,
                        RewriteOp::McpJson(client) => {
                            strip_mcp_json_client(&out, client, name, url)?.0
                        }
                        RewriteOp::McpToml => strip_mcp_toml(&out, name, url)?.0,
                    };
                }
                Ok(out)
            })?;
            println!("✓ {} {}", outcome.verb(), path.display());
        }
    }
    Ok(())
}

fn remove_empty_skill_dirs(skill_file: &Path) -> Result<()> {
    let Some(skill_dir) = skill_file.parent() else {
        return Ok(());
    };
    let root = skill_dir.parent().map(Path::to_path_buf);

    remove_dir_if_empty(skill_dir)?;
    if let Some(root) = root {
        remove_dir_if_empty(&root)?;
    }

    Ok(())
}

fn remove_dir_if_empty(path: &Path) -> Result<()> {
    if !path.is_dir() {
        return Ok(());
    }

    let mut entries =
        std::fs::read_dir(path).with_context(|| format!("reading {}", path.display()))?;
    if entries.next().is_some() {
        return Ok(());
    }

    std::fs::remove_dir(path).with_context(|| format!("removing {}", path.display()))?;
    println!("✓ removed empty directory {}", path.display());
    Ok(())
}

/// Run the `uninstall` subcommand.
///
/// # Errors
/// Returns an error if a config file is malformed or a removal write
/// fails. Absent files / nothing-to-remove are not errors.
pub fn run(config: &Config, args: UninstallArgs) -> anyhow::Result<()> {
    let name = args.mcp_name.clone();
    let url = args.mcp_url.clone();

    let plan = build_plan(&args, &config.data_dir, &config.runtime_env)?;
    print_plan(&plan);
    // The marketplace plugins carry hooks, MCP and skills together, so only a
    // full uninstall removes them; the harness's own CLI does the removal.
    let plugins = if args.only.is_none() {
        installed_plugins(config)
    } else {
        Vec::new()
    };
    for plugin in &plugins {
        let (program, plugin_args) = plugin.harness.uninstall_command();
        println!("would run `{program} {}`", plugin_args.join(" "));
    }
    let service = if args.only.is_none() {
        setup_service(config)
    } else {
        None
    };
    if let Some((_, removal)) = &service {
        print_service_plan(removal);
    }
    let remove_service = matches!(&service, Some((_, user_service::Removal::Removed(_))));
    let hooks_removed = args.only.is_none() || args.only == Some(crate::cli::UninstallOnly::Hooks);
    if hooks_removed {
        // Only the current checkout's project file is reachable from here.
        println!(
            "note: run `ai-memory uninstall --only hooks --apply` inside every other checkout that \
             used `install-hooks --scope project`; their .claude/settings.local.json keeps calling \
             this binary and data dir."
        );
    }
    if args.purge_data {
        for path in data_purge::purge_preview(&config.data_dir) {
            println!("would purge {}", path.display());
        }
    }
    if !args.apply {
        println!("(dry-run; pass --apply to remove)");
        return Ok(());
    }
    if plan.is_empty() && plugins.is_empty() && !args.purge_data && !remove_service {
        return Ok(());
    }

    if std::io::stdin().is_terminal() && !args.yes {
        eprint!("Proceed with removal? [y/N] ");
        use std::io::Write as _;
        std::io::stderr().flush().ok();
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        if !matches!(line.trim().to_lowercase().as_str(), "y" | "yes") {
            println!("aborted.");
            return Ok(());
        }
    }

    stop_service_guarding_purge(
        service.as_ref(),
        args.purge_data,
        &config.data_dir,
        &user_service::SystemRunner,
        crate::process_guard::sibling_processes,
        std::time::Duration::from_secs(5),
    )?;

    // The current checkout's `.claude/settings.local.json` keeps its backup
    // under the data dir, as `install-hooks --scope project` does: a sibling
    // `.bak-<ts>` would sit untracked in the repository.
    let project_local = std::env::current_dir()
        .ok()
        .map(|cwd| install_hooks::project_claude_settings_local(&cwd));
    for change in &plan {
        let private_backup = match change {
            PlannedChange::Rewrite { path, .. } if project_local.as_ref() == Some(path) => Some(
                install_hooks::project_settings_backup(&config.data_dir, path),
            ),
            _ => None,
        };
        apply_change(change, private_backup.as_ref(), name.as_deref(), &url)?;
    }

    uninstall_plugins(&plugins, &user_service::SystemRunner);

    // Removing the hooks removes the readers of the stored bearer, so leaving
    // it on disk would strand a live credential (#552). An uninstall that
    // keeps the hooks (`--only mcp|instructions|skills`) keeps it: the native
    // hook, the shell hooks and the generated TypeScript integrations (the Pi
    // one also bridges MCP) all still read it. So does `--only hooks` while a
    // Claude Code or Muse bridge entry remains, since the bridge reads the same
    // file and `install-mcp --apply --auth-token` leaves the key out of those
    // entries. Best-effort: an unremovable file must not fail a teardown that
    // otherwise succeeded.
    let bridge_keeps_key = args.only == Some(crate::cli::UninstallOnly::Hooks)
        && install_mcp::bridge_entry_remains(name.as_deref().unwrap_or("ai-memory"));
    if hooks_removed
        && !bridge_keeps_key
        && let Err(error) = crate::config::clear_hook_auth_token(&config.data_dir)
    {
        eprintln!(
            "ai-memory uninstall warning: could not remove the stored auth token under {}: {error}",
            config.data_dir.display()
        );
    }
    // Server-profile tokens (#992) are read by the same hooks, so the same
    // reasoning applies; the registry of URLs stays. The root bearer the
    // server itself reads (`[auth].bearer_token_file`) is not a hook's and
    // stays with the server's config.
    if let Err(error) =
        crate::server_profiles::clear_tokens(&config.data_dir, config.auth.token_file_path())
    {
        eprintln!(
            "ai-memory uninstall warning: could not remove the stored server-profile tokens under {}: {error}",
            config.data_dir.display()
        );
    }

    if args.purge_data {
        for path in data_purge::purge_data_dirs(&config.data_dir)? {
            println!("✓ purged {}", path.display());
        }
    }

    print_docker_hint(args.purge_data);

    Ok(())
}

/// The ai-memory marketplace plugins installed for this user.
fn installed_plugins(config: &Config) -> Vec<agent_plugins::InstalledPlugin> {
    let Some(home) = crate::commands::run::native_home(config) else {
        return Vec::new();
    };
    agent_plugins::installed(&home, &|name| {
        config
            .runtime_env
            .process_env()
            .iter()
            .find(|(key, _)| key.as_os_str() == name)
            .map(|(_, value)| value.clone())
    })
}

/// Remove each plugin through its harness's CLI. A failure is reported with
/// the command to run, and never stops the rest of the teardown.
fn uninstall_plugins(
    plugins: &[agent_plugins::InstalledPlugin],
    runner: &dyn user_service::CommandRunner,
) {
    for plugin in plugins {
        let (program, args) = plugin.harness.uninstall_command();
        let shown = format!("{program} {}", args.join(" "));
        match runner.run(program, &args) {
            Ok(outcome) if outcome.success => println!("✓ {shown}"),
            Ok(outcome) => eprintln!(
                "ai-memory uninstall warning: `{shown}` failed ({}); run it yourself",
                outcome.stderr.trim()
            ),
            Err(error) => eprintln!(
                "ai-memory uninstall warning: could not run `{shown}` ({error:#}); run it yourself"
            ),
        }
    }
}

/// The user service `setup` installs on this platform and what uninstall
/// would do about it; `None` on a platform without one or with no home.
fn setup_service(config: &Config) -> Option<(ServiceManager, user_service::Removal)> {
    let manager = ServiceManager::current()?;
    let home = crate::commands::run::native_home(config)?;
    let xdg_config_home = config
        .runtime_env
        .process_env()
        .iter()
        .find(|(key, _)| key == "XDG_CONFIG_HOME")
        .map(|(_, value)| PathBuf::from(value));
    let path = user_service::unit_path(manager, &home, xdg_config_home.as_deref());
    Some((
        manager,
        user_service::planned_removal_for(
            manager,
            &path,
            &config.data_dir,
            &user_service::SystemRunner,
        ),
    ))
}

/// Stop the user service `setup` installed, refusing a data purge while any
/// other ai-memory process is alive.
///
/// All-or-nothing: the live-process check runs first, with the service's own
/// process left out because this uninstall is about to stop it, so a sibling
/// (a `run` session, a second server) refuses the purge before the service
/// is stopped or anything else is touched (matches reset's guard-at-top).
/// After the stop the check runs again without that exception: a service that
/// did not exit still holds the data. Wiring-only uninstall stays unguarded;
/// it edits agent config files the server never touches.
fn stop_service_guarding_purge(
    service: Option<&(ServiceManager, user_service::Removal)>,
    purge_data: bool,
    data_dir: &Path,
    runner: &dyn user_service::CommandRunner,
    siblings: impl Fn() -> Vec<sysinfo::Pid>,
    exit_wait: std::time::Duration,
) -> Result<()> {
    let to_stop = match service {
        Some((manager, user_service::Removal::Removed(path))) => Some((*manager, path)),
        _ => None,
    };
    let service_pids = if purge_data {
        to_stop.map_or_else(Vec::new, |(manager, _)| {
            service_pids(manager, data_dir, runner)
        })
    } else {
        Vec::new()
    };
    if purge_data {
        let others: Vec<sysinfo::Pid> = siblings()
            .into_iter()
            .filter(|pid| !service_pids.contains(&pid.as_u32()))
            .collect();
        if !others.is_empty() {
            anyhow::bail!(crate::process_guard::busy_message("purge data", &others));
        }
    }
    if let Some((manager, path)) = to_stop {
        match user_service::remove(manager, path, data_dir, runner)? {
            user_service::Removal::Removed(path) => {
                println!("✓ stopped and removed the user service {}", path.display());
            }
            other => print_service_plan(&other),
        }
    }
    if purge_data && to_stop.is_some() {
        // `launchctl bootout` can return before the process has exited, so
        // give the service's own process a bounded moment to go before the
        // recheck refuses on it.
        let mut alive = siblings();
        if !service_pids.is_empty() {
            let deadline = std::time::Instant::now() + exit_wait;
            while alive.iter().any(|pid| service_pids.contains(&pid.as_u32()))
                && std::time::Instant::now() < deadline
            {
                std::thread::sleep(std::time::Duration::from_millis(50));
                alive = siblings();
            }
        }
        if !alive.is_empty() {
            anyhow::bail!(crate::process_guard::busy_message("purge data", &alive));
        }
    }
    Ok(())
}

/// The running service's processes, as its manager reports them: the main
/// process for systemd and launchd, and the host's supervisor and server on
/// Windows. Empty when it is not running or the manager cannot say.
fn service_pids(
    manager: ServiceManager,
    data_dir: &Path,
    runner: &dyn user_service::CommandRunner,
) -> Vec<u32> {
    if manager == ServiceManager::WindowsRun {
        return crate::commands::service_host::live_pids(
            &crate::commands::service_host::state_file(data_dir),
        );
    }
    service_main_pid(manager, runner).into_iter().collect()
}

/// The running service's main process, as its manager reports it; `None`
/// when it is not running or the manager cannot say.
fn service_main_pid(
    manager: ServiceManager,
    runner: &dyn user_service::CommandRunner,
) -> Option<u32> {
    let pid = match manager {
        ServiceManager::SystemdUser => {
            let out = runner
                .run(
                    "systemctl",
                    &[
                        "--user",
                        "show",
                        "--property=MainPID",
                        "--value",
                        user_service::SYSTEMD_UNIT_NAME,
                    ],
                )
                .ok()
                .filter(|out| out.success)?;
            out.stdout.trim().parse::<u32>().ok()?
        }
        ServiceManager::Launchd => {
            // `launchctl list <label>` prints a dictionary with a
            // `"PID" = 123;` line while the job runs.
            let out = runner
                .run("launchctl", &["list", user_service::LAUNCHD_LABEL])
                .ok()
                .filter(|out| out.success)?;
            out.stdout.lines().find_map(|line| {
                line.trim()
                    .strip_prefix("\"PID\" =")?
                    .trim()
                    .trim_end_matches(';')
                    .trim()
                    .parse::<u32>()
                    .ok()
            })?
        }
        ServiceManager::WindowsRun => return None,
    };
    (pid != 0).then_some(pid)
}

fn print_service_plan(removal: &user_service::Removal) {
    match removal {
        user_service::Removal::Absent => {}
        user_service::Removal::Removed(path) => {
            println!("would stop and remove the user service {}", path.display());
        }
        user_service::Removal::NotOurs(path) => println!(
            "leaving {}: `ai-memory setup` did not write it, or it was edited since; remove it \
             yourself if it runs ai-memory",
            path.display()
        ),
    }
}

/// Print the manual Docker teardown steps (never executed). When the
/// data was purged locally, note that; otherwise remind how to wipe it.
fn print_docker_hint(data_purged: bool) {
    println!();
    println!("Wiring removed. ai-memory's server + data live in its container/volume —");
    println!("tear those down manually:");
    println!("  docker compose -f docker/docker-compose.yml down -v");
    println!("  docker volume rm ai-memory-data   # if you used the default volume");
    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)."
        );
    }
}

/// Remove the `<!-- ai-memory:start -->`…`<!-- ai-memory:end -->`
/// block (inclusive) from a CLAUDE.md / AGENTS.md. Returns the new
/// content and whether a block was found. Inverse of
/// `install_instructions::merge_instructions_block`: an install
/// followed by an uninstall round-trips to the original file.
fn strip_instructions_block(content: &str) -> (String, bool) {
    // Line-anchored so an inline mention of the marker strings inside the
    // managed block can't be matched as the real end delimiter (which would
    // leave an orphan tail behind, breaking the install->uninstall
    // round-trip).
    let Some(start) = find_marker_line(content, MARKER_START, 0) else {
        return (content.to_string(), false);
    };
    let Some(end_pos) = find_marker_line(content, MARKER_END, start) else {
        return (content.to_string(), false);
    };
    let end = end_pos + MARKER_END.len();
    // Consume a trailing newline after the end marker if present.
    let after = if content.as_bytes().get(end..end + 2) == Some(b"\r\n") {
        end + 2
    } else if content.as_bytes().get(end).copied() == Some(b'\n') {
        end + 1
    } else {
        end
    };
    let mut head = content[..start].to_string();
    let tail = strip_legacy_orphan_tail(&content[after..]);
    // When the block sat at EOF, install added a blank-line separator
    // before it; drop that artifact so install→uninstall round-trips.
    if tail.is_empty() && head.ends_with("\n\n") {
        head.pop();
    }
    (format!("{head}{tail}"), true)
}

fn strip_legacy_orphan_tail(tail: &str) -> &str {
    let mut rest = tail;
    loop {
        if let Some(stripped) = rest.strip_prefix(LEGACY_ORPHAN_TAIL_LF) {
            rest = stripped;
        } else if let Some(stripped) = rest.strip_prefix(LEGACY_ORPHAN_TAIL_CRLF) {
            rest = stripped;
        } else {
            return rest;
        }
    }
}

/// True when a hook command string was written by ai-memory. Legacy script
/// commands carry the unconditional `AI_MEMORY_HOOK_URL=` env prefix; native
/// commands invoke the `ai-memory hook --event ... --server-url ...` subcommand.
/// Keep both signatures narrow so hook overlays and uninstall do not remove
/// unrelated hooks that happen to use the same event names or script basenames.
///
/// The executable name is matched in both its hyphenated (`ai-memory`) and
/// underscored (`ai_memory`) forms: a native command uses the current
/// executable's path, and on Windows a test binary (and some packaging) names
/// it with an underscore, so recognizing only the hyphen made a reapply
/// non-idempotent — it reported `Updated` and failed to dedup its own prior
/// entries (#740). The full `hook --event … --agent … --server-url …` argv
/// signature still gates the match, so this does not broaden to unrelated
/// commands whose path merely contains the string.
pub(crate) fn hook_command_is_ours(command: &str) -> bool {
    if command.contains("AI_MEMORY_HOOK_URL=") {
        return true;
    }
    let lower = command.to_ascii_lowercase();
    (lower.contains("ai-memory") || lower.contains("ai_memory"))
        && lower.contains(" hook --event ")
        && lower.contains(" --agent ")
        && lower.contains(" --server-url ")
}

fn hook_entry_is_ours(entry: &serde_json::Value) -> bool {
    let Some(command) = entry.get("command").and_then(|c| c.as_str()) else {
        return false;
    };
    if hook_command_is_ours(command) {
        return true;
    }
    let lower = command.to_ascii_lowercase();
    if !(lower.contains("ai-memory") || lower.contains("ai_memory")) {
        return false;
    }
    let Some(args) = entry.get("args").and_then(|a| a.as_array()) else {
        return false;
    };
    let tokens: Vec<&str> = args.iter().filter_map(|v| v.as_str()).collect();
    tokens.contains(&"hook")
        && tokens.contains(&"--event")
        && tokens.contains(&"--agent")
        && tokens.contains(&"--server-url")
}

/// Result of stripping ai-memory entries from a hooks JSON file.
struct HookRemoval {
    new_content: String,
    removed_events: Vec<String>,
}

/// Remove ai-memory commands from one hook entry. Returns `(removed_any,
/// remove_entry)`. Flat entries are removed whole; nested entries only lose the
/// matching inner commands and survive when third-party inner hooks remain.
fn strip_hook_entry(entry: &mut serde_json::Value) -> (bool, bool) {
    if hook_entry_is_ours(entry) {
        return (true, true);
    }
    if let Some(inner) = entry.get_mut("hooks").and_then(|h| h.as_array_mut()) {
        let before = inner.len();
        inner.retain(|h| !hook_entry_is_ours(h));
        let removed = inner.len() != before;
        return (removed, inner.is_empty());
    }
    (false, false)
}

fn strip_hook_events(
    hooks: &mut serde_json::Map<String, serde_json::Value>,
    removed_events: &mut Vec<String>,
) {
    let events: Vec<String> = hooks.keys().cloned().collect();
    for event in events {
        let Some(arr) = hooks.get_mut(&event).and_then(|v| v.as_array_mut()) else {
            continue;
        };
        let mut removed_from_event = false;
        arr.retain_mut(|entry| {
            let (removed, remove_entry) = strip_hook_entry(entry);
            removed_from_event |= removed;
            !remove_entry
        });
        if removed_from_event {
            removed_events.push(event.clone());
        }
        if arr.is_empty() {
            hooks.remove(&event);
        }
    }
}

/// Remove ai-memory hook entries from a settings/hooks JSON document.
/// Preserves third-party entries (including siblings under the same
/// event). Prunes an event key when emptied and the `hooks` object
/// when emptied. Detection is by signature, so stale event keys
/// outside the current vocabulary are caught too.
fn strip_ai_memory_hooks(content: &str) -> Result<HookRemoval> {
    let mut removed_events = Vec::new();
    let new_content = mutate_json(content, |root| {
        let Some(hooks) = root.get_mut("hooks").and_then(|h| h.as_object_mut()) else {
            return Ok(());
        };
        strip_hook_events(hooks, &mut removed_events);
        if hooks.is_empty() {
            root.remove("hooks");
        }
        Ok(())
    })?;
    Ok(HookRemoval {
        new_content,
        removed_events,
    })
}

/// Remove ai-memory's entries from Zero's hooks.json `hooks` array. Zero
/// hook entries are objects with an `id`; install writes ours with the
/// `ai-memory-` prefix (issue #156), so the prefix IS the ownership
/// signature — third-party hooks in the same file keep their ids and
/// survive untouched. The top-level `enabled` flag is left alone.
fn strip_zero_hooks(content: &str) -> Result<HookRemoval> {
    let mut removed_events = Vec::new();
    let new_content = mutate_json(content, |root| {
        let Some(hooks) = root.get_mut("hooks").and_then(|v| v.as_array_mut()) else {
            return Ok(());
        };
        hooks.retain(|hook| {
            let ours = hook
                .get("id")
                .and_then(|v| v.as_str())
                .is_some_and(|id| id.starts_with("ai-memory-"));
            if ours && let Some(id) = hook.get("id").and_then(|v| v.as_str()) {
                removed_events.push(id.to_string());
            }
            !ours
        });
        Ok(())
    })?;
    Ok(HookRemoval {
        new_content,
        removed_events,
    })
}

fn strip_kiro_cli_v2_hooks(content: &str) -> Result<HookRemoval> {
    let mut removed_events = Vec::new();
    let new_content = mutate_json(content, |root| {
        let Some(hooks) = root
            .get_mut("hooks")
            .and_then(|value| value.as_object_mut())
        else {
            return Ok(());
        };
        let events: Vec<String> = hooks.keys().cloned().collect();
        for event in events {
            let Some(entries) = hooks.get_mut(&event).and_then(|value| value.as_array_mut()) else {
                continue;
            };
            let original_len = entries.len();
            entries.retain(|entry| {
                !entry
                    .get("command")
                    .and_then(serde_json::Value::as_str)
                    .is_some_and(install_hooks::is_ai_memory_kiro_hook_command)
            });
            if entries.len() != original_len {
                removed_events.push(event.clone());
            }
            if entries.is_empty() {
                hooks.remove(&event);
            }
        }
        if hooks.is_empty() {
            root.remove("hooks");
        }
        Ok(())
    })?;
    Ok(HookRemoval {
        new_content,
        removed_events,
    })
}

fn strip_kiro_cli_v3_hooks(content: &str) -> Result<HookRemoval> {
    let mut removed_events = Vec::new();
    let new_content = mutate_json(content, |root| {
        let Some(hooks) = root
            .get_mut("hooks")
            .and_then(serde_json::Value::as_array_mut)
        else {
            return Ok(());
        };
        hooks.retain(|entry| {
            if !install_hooks::is_ai_memory_kiro_v3_hook_entry(entry) {
                return true;
            }
            let label = entry
                .get("trigger")
                .and_then(serde_json::Value::as_str)
                .unwrap_or("unknown");
            removed_events.push(format!("kiro-cli-v3.{label}"));
            false
        });
        Ok(())
    })?;
    Ok(HookRemoval {
        new_content,
        removed_events,
    })
}

/// Remove ai-memory hook entries from Devin's `hooks.v1.json`, whose root
/// object is the hook-event map. This is intentionally separate from
/// `strip_ai_memory_hooks` so we never infer a flat shape for other agents.
fn strip_ai_memory_hooks_flat(content: &str) -> Result<HookRemoval> {
    let mut removed_events = Vec::new();
    let new_content = mutate_json(content, |root| {
        strip_hook_events(root, &mut removed_events);
        Ok(())
    })?;
    Ok(HookRemoval {
        new_content,
        removed_events,
    })
}

/// Remove ai-memory's named Antigravity CLI hook group entries. The group
/// name alone is not enough to prove ownership; every removed entry must still
/// carry ai-memory's hook command signature.
fn strip_antigravity_hooks(content: &str) -> Result<HookRemoval> {
    let mut removed_events = Vec::new();
    let new_content = mutate_json(content, |root| {
        let Some(group) = root.get_mut("ai-memory").and_then(|g| g.as_object_mut()) else {
            return Ok(());
        };
        let events: Vec<String> = group.keys().cloned().collect();
        for event in events {
            let Some(arr) = group.get_mut(&event).and_then(|v| v.as_array_mut()) else {
                continue;
            };
            let mut removed_from_event = false;
            arr.retain_mut(|entry| {
                let (removed, remove_entry) = strip_hook_entry(entry);
                removed_from_event |= removed;
                !remove_entry
            });
            if removed_from_event {
                removed_events.push(format!("ai-memory.{event}"));
            }
            if arr.is_empty() {
                group.remove(&event);
            }
        }
        if group.is_empty() {
            root.remove("ai-memory");
        }
        Ok(())
    })?;
    Ok(HookRemoval {
        new_content,
        removed_events,
    })
}

/// Remove ai-memory's `[[hooks]]` rules from Kimi Code's config.toml. The
/// same file holds the user's providers/model settings and any third-party
/// rules, so ownership is proven by the hook command signature alone and
/// everything else round-trips untouched.
fn strip_kimi_code_hooks(content: &str) -> Result<HookRemoval> {
    let mut removed_events = Vec::new();
    let new_content = mutate_toml(content, |doc| {
        let Some(hooks) = doc
            .get_mut("hooks")
            .and_then(toml_edit::Item::as_array_of_tables_mut)
        else {
            return Ok(());
        };
        for index in (0..hooks.len()).rev() {
            let Some(table) = hooks.get_mut(index) else {
                continue;
            };
            let ours = table
                .get("command")
                .and_then(toml_edit::Item::as_str)
                .is_some_and(hook_command_is_ours);
            if !ours {
                continue;
            }
            let event = table
                .get("event")
                .and_then(toml_edit::Item::as_str)
                .unwrap_or("unknown")
                .to_string();
            removed_events.push(format!("kimi-code.{event}"));
            hooks.remove(index);
        }
        Ok(())
    })?;
    // Index-descending removal collects events in reverse document order.
    removed_events.reverse();
    Ok(HookRemoval {
        new_content,
        removed_events,
    })
}

fn generated_file_is_ours(path: &Path, kind: DeleteKind) -> bool {
    let Ok(content) = std::fs::read_to_string(path) else {
        return false;
    };
    match kind {
        DeleteKind::OpenCodePlugin => {
            content.contains("Auto-generated by `ai-memory install-hooks --agent opencode --apply`")
                && content.contains("const AGENT = \"open-code\";")
        }
        DeleteKind::OpenCode2Plugin => {
            content
                .contains("Auto-generated by `ai-memory install-hooks --agent opencode2 --apply`")
                && content.contains("const AGENT = \"opencode2\";")
        }
        DeleteKind::OmpExtension => {
            content.contains("Auto-generated by `ai-memory install-hooks --agent omp --apply`")
                && content.contains("const AGENT = \"omp\";")
        }
        // Auto-wire writes its sentinels empty; anything else in that
        // directory is not one of them.
        DeleteKind::AutowireSentinel => content.is_empty(),
        DeleteKind::PiExtension => {
            content.contains("Auto-generated by `ai-memory install-hooks --agent pi --apply`")
                && content.contains("const AGENT = \"pi\";")
                && content.contains("pi.registerTool")
        }
        DeleteKind::OpenClawEntrypoint => {
            content.contains("Auto-generated by `ai-memory install-hooks --agent openclaw --apply`")
                && content.contains("definePluginEntry")
                && content.contains("id: \"ai-memory\"")
        }
        DeleteKind::OpenClawPackageJson => serde_json::from_str::<serde_json::Value>(&content)
            .ok()
            .is_some_and(|v| {
                v.get("name").and_then(|name| name.as_str()) == Some(openclaw_plugin::PACKAGE_NAME)
                    && v.get("private").and_then(|private| private.as_bool()) == Some(true)
                    && v.get("type").and_then(|ty| ty.as_str()) == Some("module")
                    && v.pointer("/openclaw/extensions")
                        .and_then(|extensions| extensions.as_array())
                        .is_some_and(|extensions| {
                            extensions.len() == 1
                                && extensions[0].as_str()
                                    == Some(&format!("./{}", openclaw_plugin::ENTRYPOINT_TS))
                        })
            }),
        DeleteKind::OpenClawManifest => serde_json::from_str::<serde_json::Value>(&content)
            .ok()
            .is_some_and(|v| {
                v.get("id").and_then(|id| id.as_str()) == Some(openclaw_plugin::PLUGIN_ID)
                    && v.get("name").and_then(|name| name.as_str()) == Some("ai-memory")
                    && v.pointer("/activation/onCapabilities")
                        .and_then(|capabilities| capabilities.as_array())
                        .is_some_and(|capabilities| {
                            capabilities
                                .iter()
                                .any(|entry| entry.as_str() == Some("hook"))
                        })
                    && v.pointer("/configSchema/additionalProperties")
                        .and_then(|additional| additional.as_bool())
                        == Some(false)
            }),
        DeleteKind::KiroCliV3Hooks => serde_json::from_str::<serde_json::Value>(&content)
            .ok()
            .and_then(|value| value.as_object().cloned())
            .is_some_and(|root| {
                root.len() == 2
                    && root.get("version").and_then(serde_json::Value::as_str) == Some("v1")
                    && root
                        .get("hooks")
                        .and_then(serde_json::Value::as_array)
                        .is_some_and(|hooks| {
                            !hooks.is_empty()
                                && hooks
                                    .iter()
                                    .all(install_hooks::is_ai_memory_kiro_v3_hook_entry)
                        })
            }),
        DeleteKind::ClineHook => {
            content.contains("Generated by ai-memory install-hooks --agent cline")
                && content.contains("'ai-memory'")
                && content.contains("'hook'")
                && content.contains("'--agent' 'cline'")
        }
        DeleteKind::ManagedSkill => content.contains(MANAGED_MARKER),
        DeleteKind::GrokHandoffRule => content.contains(GROK_HANDOFF_RULE_MARKER),
    }
}

/// Where the servers object lives in each JSON client's config.
/// (Codex is TOML — handled separately in Task 5.)
fn mcp_servers_path(client: McpClient) -> Option<&'static [&'static str]> {
    match client {
        McpClient::ClaudeCode
        | McpClient::ClaudeDesktop
        | McpClient::Cursor
        | McpClient::GeminiCli
        | McpClient::QwenCode
        | McpClient::Qoder
        | McpClient::Omp
        | McpClient::AntigravityCli
        | McpClient::KimiCode
        | McpClient::KiroCli
        | McpClient::CommandCode
        | McpClient::IbmBob
        | McpClient::CopilotCli
        | McpClient::Droid
        | McpClient::Swival
        | McpClient::Devin
        | McpClient::PrimeAgent
        | McpClient::Cline
        | McpClient::Freebuff => Some(&["mcpServers"]),
        McpClient::OpenCode => Some(&["mcp"]),
        McpClient::OpenCode2 => Some(&["mcp", "servers"]),
        McpClient::Openclaw | McpClient::Zero | McpClient::Zcode => Some(&["mcp", "servers"]),
        McpClient::VsCodeCopilot => Some(&["servers"]),
        McpClient::Zed => Some(&["context_servers"]),
        // Muse reads `mcpServers` or the legacy `mcp_servers`;
        // `strip_mcp_json` sweeps both.
        McpClient::Muse => Some(&["mcpServers"]),
        McpClient::Codex | McpClient::Grok | McpClient::Pi | McpClient::Dsh | McpClient::Hermes => {
            None
        }
    }
}

/// True when an MCP server entry is ai-memory's: its url/httpUrl/serverUrl
/// equals the endpoint, or it is an ai-memory-owned / `mcp-remote` stdio
/// bridge whose args contain the endpoint. The key/name alone is intentionally
/// not enough: users may have unrelated entries named `ai-memory`, and
/// uninstall must not remove them unless the endpoint also matches.
fn mcp_entry_is_ours(key: &str, entry: &serde_json::Value, name: Option<&str>, url: &str) -> bool {
    if name.is_some_and(|name| key != name) {
        return false;
    }
    for field in ["url", "httpUrl", "serverUrl"] {
        if entry.get(field).and_then(|v| v.as_str()) == Some(url) {
            return true;
        }
    }
    if entry.pointer("/transport/url").and_then(|v| v.as_str()) == Some(url) {
        return true;
    }
    // Our stdio bridge, in any client's shape (`command` + `args`, or
    // OpenCode's single `command` array), aimed at this server.
    if install_mcp::bridge_entry_url(entry) == Some(url) {
        return true;
    }
    if let Some(args) = entry.get("args").and_then(|a| a.as_array()) {
        let has_remote = args.iter().any(|a| a.as_str() == Some("mcp-remote"));
        let has_url = args.iter().any(|a| a.as_str() == Some(url));
        if has_remote && has_url {
            return true;
        }
    }
    false
}

/// A TOML `[mcp_servers.<name>]` table (Codex, Grok) aimed at `url`: an HTTP
/// entry by its `url`, or our stdio bridge by `command` + `args`.
fn toml_mcp_entry_targets(table: &dyn toml_edit::TableLike, url: &str) -> bool {
    if table.get("url").and_then(|u| u.as_str()) == Some(url) {
        return true;
    }
    let as_json = serde_json::json!({
        "command": table.get("command").and_then(|c| c.as_str()),
        "args": table
            .get("args")
            .and_then(|a| a.as_array())
            .map(|a| a.iter().filter_map(|v| v.as_str()).collect::<Vec<_>>()),
    });
    install_mcp::bridge_entry_url(&as_json) == Some(url)
}

/// URL forms uninstall matches for `client`: the endpoint as given, plus
/// for clients whose installer appends a schema flavor, the form
/// `install-mcp` actually writes (`--mcp-url` keeps the unflavored default).
/// Every other client keeps exact-match semantics.
fn mcp_url_candidates(_client: McpClient, url: &str) -> Vec<String> {
    let mut candidates = vec![url.to_string()];
    // Every marker, for every client: `install-mcp --flavor` can write any of
    // them into any client's config, so keying this off the client's built-in
    // default would strand the entries an operator pinned by hand. Candidates
    // that do not appear in the file simply match nothing.
    for marker in install_mcp::FLAVOR_MARKERS {
        let flavored = install_mcp::flavored_mcp_url_for_marker(url, marker);
        if !candidates.contains(&flavored) {
            candidates.push(flavored);
        }
    }
    candidates
}

/// [`strip_mcp_json`] over each URL candidate form of the client.
fn strip_mcp_json_client(
    content: &str,
    client: McpClient,
    name: Option<&str>,
    url: &str,
) -> Result<(String, Vec<String>)> {
    let mut new_content = content.to_string();
    let mut removed = Vec::new();
    for candidate in mcp_url_candidates(client, url) {
        let (next, mut hits) = if matches!(client, McpClient::Zed) {
            strip_zed_mcp_jsonc(&new_content, name, &candidate)?
        } else {
            strip_mcp_json(&new_content, client, name, &candidate)?
        };
        new_content = next;
        removed.append(&mut hits);
    }
    Ok((new_content, removed))
}

/// Remove matching entries from Zed's JSONC settings while preserving user
/// comments, trailing commas, and unrelated formatting.
fn strip_zed_mcp_jsonc(
    content: &str,
    name: Option<&str>,
    url: &str,
) -> Result<(String, Vec<String>)> {
    use jsonc_parser::ParseOptions;
    use jsonc_parser::cst::CstRootNode;

    let root = CstRootNode::parse(content, &ParseOptions::default())
        .context("parsing Zed settings.json as JSONC")?;
    let Some(settings) = root.object_value() else {
        return Ok((content.to_string(), Vec::new()));
    };
    let Some(servers) = settings.object_value("context_servers") else {
        return Ok((content.to_string(), Vec::new()));
    };

    let mut removed = Vec::new();
    for property in servers.properties() {
        let Some(key) = property.name().and_then(|key| key.decoded_value().ok()) else {
            continue;
        };
        let Some(entry) = property.to_serde_value() else {
            continue;
        };
        if mcp_entry_is_ours(&key, &entry, name, url) {
            property.remove();
            removed.push(key);
        }
    }
    if servers.properties().is_empty()
        && let Some(context_servers) = settings.get("context_servers")
    {
        context_servers.remove();
    }
    Ok((root.to_string(), removed))
}

/// Remove ai-memory's MCP server from a JSON client config. Returns
/// the new content and the names removed. Prunes the (possibly nested)
/// servers object and its parents if they empty.
fn strip_mcp_json(
    content: &str,
    client: McpClient,
    name: Option<&str>,
    url: &str,
) -> Result<(String, Vec<String>)> {
    let Some(path) = mcp_servers_path(client) else {
        return Ok((content.to_string(), Vec::new()));
    };
    let legacy: &[&str] = &["mcp_servers"];
    let paths: Vec<&[&str]> = if client == McpClient::Muse {
        vec![path, legacy]
    } else {
        vec![path]
    };
    let mut removed = Vec::new();
    let new_content = mutate_json(content, |root| {
        for path in &paths {
            strip_mcp_json_at(root, path, name, url, &mut removed);
        }
        Ok(())
    })?;
    Ok((new_content, removed))
}

/// [`strip_mcp_json`] for the server map at one key path.
fn strip_mcp_json_at(
    root: &mut serde_json::Map<String, serde_json::Value>,
    path: &[&str],
    name: Option<&str>,
    url: &str,
    removed: &mut Vec<String>,
) {
    let mut cursor: &mut serde_json::Map<String, serde_json::Value> = root;
    for (depth, key) in path.iter().enumerate() {
        let is_last = depth == path.len() - 1;
        if is_last {
            let Some(servers) = cursor.get_mut(*key).and_then(|v| v.as_object_mut()) else {
                return;
            };
            let keys: Vec<String> = servers.keys().cloned().collect();
            for k in keys {
                let ours = servers
                    .get(&k)
                    .is_some_and(|e| mcp_entry_is_ours(&k, e, name, url));
                if ours {
                    servers.remove(&k);
                    removed.push(k);
                }
            }
            if servers.is_empty() {
                cursor.remove(*key);
            }
        } else {
            let Some(next) = cursor.get_mut(*key).and_then(|v| v.as_object_mut()) else {
                return;
            };
            cursor = next;
        }
    }
}

/// Remove ai-memory's Codex MCP table by name or `url`. Returns new
/// content and removed names. Preserves comments + other tables.
fn strip_mcp_toml(content: &str, name: Option<&str>, url: &str) -> Result<(String, Vec<String>)> {
    use toml_edit::{Item, Value};

    let mut removed = Vec::new();
    let new_content = mutate_toml(content, |doc| {
        let Some(servers_item) = doc.get_mut("mcp_servers") else {
            return Ok(());
        };
        let mut remove_mcp_servers = false;
        match servers_item {
            Item::Table(servers) => {
                let keys: Vec<String> = servers.iter().map(|(k, _)| k.to_string()).collect();
                for k in keys {
                    let matches_url = servers
                        .get(&k)
                        .and_then(|item| item.as_table_like())
                        .is_some_and(|t| toml_mcp_entry_targets(t, url));
                    if name.is_none_or(|name| k == name) && matches_url {
                        servers.remove(&k);
                        removed.push(k);
                    }
                }
            }
            Item::Value(Value::InlineTable(servers)) => {
                let keys: Vec<String> = servers.iter().map(|(k, _)| k.to_string()).collect();
                for k in keys {
                    let matches_url = servers
                        .get(&k)
                        .and_then(|value| value.as_inline_table())
                        .is_some_and(|table| toml_mcp_entry_targets(table, url));
                    if name.is_none_or(|name| k == name) && matches_url {
                        servers.remove(&k);
                        removed.push(k);
                    }
                }
                if servers.is_empty() {
                    remove_mcp_servers = true;
                }
            }
            _ => {}
        }
        if remove_mcp_servers {
            doc.remove("mcp_servers");
        }
        Ok(())
    })?;
    Ok((new_content, removed))
}

#[cfg(test)]
mod tests {
    use super::*;
    #[cfg(unix)]
    use std::time::Duration;

    /// Answers `systemctl`/`launchctl` from a script and records each call.
    struct ServiceRunner {
        calls: std::cell::RefCell<Vec<String>>,
        main_pid: &'static str,
    }

    impl user_service::CommandRunner for ServiceRunner {
        fn run(&self, program: &str, args: &[&str]) -> Result<user_service::CommandOutcome> {
            let line = format!("{program} {}", args.join(" "));
            self.calls.borrow_mut().push(line.clone());
            let stdout = if line.contains("MainPID") {
                self.main_pid.to_owned()
            } else if line.starts_with("launchctl list") {
                format!(
                    "{{\n\t\"Label\" = \"x\";\n\t\"PID\" = {};\n}};\n",
                    self.main_pid
                )
            } else {
                String::new()
            };
            Ok(user_service::CommandOutcome {
                success: true,
                stdout,
                stderr: String::new(),
            })
        }
    }

    /// A unit `setup` rendered, written where `uninstall` finds it.
    #[cfg(unix)]
    fn setup_unit(root: &Path) -> (ServiceManager, user_service::Removal) {
        let manager = ServiceManager::SystemdUser;
        let unit = user_service::render(
            manager,
            &user_service::ServiceSpec {
                exe: root.join("bin").join("ai-memory"),
                data_dir: root.join("data"),
                config_path: root.join("config.toml"),
                home: root.to_path_buf(),
                xdg_config_home: None,
            },
        )
        .unwrap();
        std::fs::create_dir_all(unit.path.parent().unwrap()).unwrap();
        std::fs::write(&unit.path, &unit.contents).unwrap();
        (manager, user_service::planned_removal(&unit.path))
    }

    /// Adversarial: a live sibling (pid 777) refuses `--purge-data` BEFORE
    /// the service is stopped; the old order stopped it first and only then
    /// refused, leaving the user without a server and with nothing purged.
    /// Controls: the service's own process is not a sibling, and a purge
    /// with only that process alive stops the service and proceeds.
    #[cfg(unix)]
    #[test]
    fn purge_guard_refuses_a_live_sibling_before_stopping_the_service() {
        let root = tempfile::tempdir().unwrap();
        let service = setup_unit(root.path());
        assert!(matches!(service.1, user_service::Removal::Removed(_)));
        let runner = ServiceRunner {
            calls: std::cell::RefCell::default(),
            main_pid: "4242",
        };
        let pids = |raw: &[u32]| {
            raw.iter()
                .copied()
                .map(sysinfo::Pid::from_u32)
                .collect::<Vec<_>>()
        };

        let error = stop_service_guarding_purge(
            Some(&service),
            true,
            root.path(),
            &runner,
            || pids(&[4242, 777]),
            Duration::ZERO,
        )
        .unwrap_err()
        .to_string();
        assert!(error.contains("777") && !error.contains("4242"), "{error}");
        assert!(
            !runner.calls.borrow().iter().any(|c| c.contains("disable")),
            "the service must still run: {:?}",
            runner.calls.borrow()
        );

        let alive = std::cell::RefCell::new(pids(&[4242]));
        stop_service_guarding_purge(
            Some(&service),
            true,
            root.path(),
            &runner,
            || alive.replace(Vec::new()),
            Duration::ZERO,
        )
        .unwrap();
        assert!(
            runner
                .calls
                .borrow()
                .iter()
                .any(|c| c == "systemctl --user disable --now ai-memory.service")
        );
    }

    /// A service that did not exit after the stop still blocks the purge.
    #[cfg(unix)]
    #[test]
    fn purge_guard_rechecks_after_stopping_the_service() {
        let root = tempfile::tempdir().unwrap();
        let service = setup_unit(root.path());
        let runner = ServiceRunner {
            calls: std::cell::RefCell::default(),
            main_pid: "4242",
        };
        let error = stop_service_guarding_purge(
            Some(&service),
            true,
            root.path(),
            &runner,
            || vec![sysinfo::Pid::from_u32(4242)],
            Duration::from_millis(120),
        )
        .unwrap_err()
        .to_string();
        assert!(error.contains("4242"), "{error}");
    }

    /// A service whose process takes a moment to exit after the stop (as
    /// `launchctl bootout` allows) does not block the purge.
    #[cfg(unix)]
    #[test]
    fn purge_guard_waits_for_the_stopped_service_to_exit() {
        let root = tempfile::tempdir().unwrap();
        let service = setup_unit(root.path());
        let runner = ServiceRunner {
            calls: std::cell::RefCell::default(),
            main_pid: "4242",
        };
        // Alive at the pre-check and for two polls after the stop, then gone.
        let polls = std::cell::Cell::new(0);
        stop_service_guarding_purge(
            Some(&service),
            true,
            root.path(),
            &runner,
            || {
                polls.set(polls.get() + 1);
                if polls.get() <= 3 {
                    vec![sysinfo::Pid::from_u32(4242)]
                } else {
                    Vec::new()
                }
            },
            Duration::from_secs(2),
        )
        .unwrap();
        assert!(polls.get() >= 4, "the recheck must poll again");
    }

    #[test]
    fn service_main_pid_reads_systemd_and_launchd() {
        let runner = |main_pid| ServiceRunner {
            calls: std::cell::RefCell::default(),
            main_pid,
        };
        assert_eq!(
            service_main_pid(ServiceManager::SystemdUser, &runner("4242\n")),
            Some(4242)
        );
        assert_eq!(
            service_main_pid(ServiceManager::SystemdUser, &runner("0\n")),
            None,
            "MainPID=0 means not running"
        );
        assert_eq!(
            service_main_pid(ServiceManager::Launchd, &runner("515")),
            Some(515)
        );
    }

    /// The skill sweep finds a global Copilot CLI skill under the COPILOT_HOME
    /// `Config::load` captured (invariant #1), not one re-read from the live
    /// process.
    #[test]
    fn skill_sweep_uses_the_captured_copilot_home() {
        let copilot_home = tempfile::tempdir().unwrap();
        let skill = copilot_home
            .path()
            .join(SKILLS_DIR)
            .join(MANAGED_SKILLS[0].relative_path);
        std::fs::create_dir_all(skill.parent().unwrap()).unwrap();
        std::fs::write(&skill, MANAGED_SKILLS[0].content).unwrap();
        let env = crate::config::RuntimeEnv::with_process_env_for_tests(&[(
            "COPILOT_HOME",
            copilot_home.path().to_str().unwrap(),
        )]);
        let args = UninstallArgs {
            apply: false,
            purge_data: false,
            only: Some(crate::cli::UninstallOnly::Skills),
            mcp_name: None,
            mcp_url: String::new(),
            yes: false,
            profile: None,
        };
        let data = tempfile::tempdir().unwrap();
        let plan = build_plan(&args, data.path(), &env).unwrap();
        assert!(
            plan.iter().any(|change| matches!(
                change,
                PlannedChange::DeleteFile { path, .. } if *path == skill
            )),
            "{plan:?}"
        );
    }

    /// Each installed plugin is removed through its own harness CLI, and one
    /// failure does not stop the next.
    #[test]
    fn uninstall_removes_each_plugin_through_its_harness() {
        use crate::commands::agent_plugins::test_support::{
            RecordingRunner, install_claude_plugin, install_codex_plugin,
        };
        let home = tempfile::tempdir().unwrap();
        let config = Config {
            home_dir: Some(home.path().to_string_lossy().into_owned()),
            ..Config::default()
        };
        assert!(installed_plugins(&config).is_empty());
        install_claude_plugin(home.path(), "3.0.0", false);
        install_codex_plugin(home.path());
        let plugins = installed_plugins(&config);
        assert_eq!(plugins.len(), 2, "a disabled plugin is still removed");

        let runner = RecordingRunner::new(Some(1));
        uninstall_plugins(&plugins, &runner);
        assert_eq!(
            *runner.calls.borrow(),
            vec![
                "claude plugin uninstall ai-memory@ai-memory",
                "codex plugin remove ai-memory@ai-memory"
            ]
        );
    }

    /// The Windows global Devin skill root (`%APPDATA%\devin\skills`) is
    /// swept alongside the cwd/home roots — `install-skills --scope global`
    /// writes there on Windows, so uninstall must plan its removal too.
    #[test]
    fn skill_roots_include_windows_appdata_devin_root() {
        let cwd = Path::new("/repo");
        let home = Path::new("/home/alice");
        let appdata = Path::new("C:/Users/Alice/AppData/Roaming");

        let roots = skill_roots(cwd, Some(home), Some(appdata), None, None, None);
        assert!(
            roots.contains(&appdata.join("devin").join(SKILLS_DIR)),
            "{roots:?}"
        );

        let without = skill_roots(cwd, Some(home), None, None, None, None);
        assert_eq!(
            without.len(),
            12,
            "no phantom root when APPDATA is unset (claude/agents/devin/grok/copilot × \
             project+global, plus Antigravity's two global roots)"
        );
        assert!(
            without.contains(&cwd.join(GROK_SKILL_DIR).join(SKILLS_DIR)),
            "{without:?}"
        );
        assert!(
            without.contains(&home.join(GROK_SKILL_DIR).join(SKILLS_DIR)),
            "{without:?}"
        );
    }

    /// `$CLAUDE_CONFIG_DIR/skills` is swept *in addition to*
    /// `~/.claude/skills` — installs may predate the env var, so uninstall
    /// must plan removal from both candidate roots.
    #[test]
    fn skill_roots_sweep_claude_config_dir_root_alongside_home() {
        let roots = skill_roots(
            Path::new("/repo"),
            Some(Path::new("/home/alice")),
            None,
            None,
            None,
            Some(Path::new("/stores/claude")),
        );
        assert!(
            roots.contains(&PathBuf::from("/stores/claude/skills")),
            "{roots:?}"
        );
        assert!(
            roots.contains(&PathBuf::from("/home/alice/.claude/skills")),
            "{roots:?}"
        );
    }

    #[test]
    fn skill_roots_use_injected_grok_home_override() {
        let roots = skill_roots(
            Path::new("/repo"),
            Some(Path::new("/home/alice")),
            None,
            Some(Path::new("/custom/grok")),
            None,
            None,
        );
        assert!(
            roots.contains(&PathBuf::from("/custom/grok/skills")),
            "{roots:?}"
        );
        assert!(!roots.contains(&PathBuf::from("/home/alice/.grok/skills")));
    }

    /// `.github/skills` is usually committed team content, so sweeping it must
    /// only ever plan the deletion of ai-memory's own marker-bearing files.
    #[test]
    fn github_skill_root_sweep_spares_unmanaged_same_name_skills() {
        let tmp = tempfile::tempdir().unwrap();
        let skill = MANAGED_SKILLS[0];
        let user = tmp
            .path()
            .join("user/.github/skills")
            .join(skill.relative_path);
        std::fs::create_dir_all(user.parent().unwrap()).unwrap();
        std::fs::write(&user, "---\nname: team-skill\n---\nteam-authored\n").unwrap();
        let mut plan = Vec::new();
        push_generated_delete(&mut plan, user.clone(), DeleteKind::ManagedSkill);
        assert!(
            plan.is_empty(),
            "an unmanaged skill must never be planned: {plan:?}"
        );

        let managed = tmp
            .path()
            .join("ours/.github/skills")
            .join(skill.relative_path);
        std::fs::create_dir_all(managed.parent().unwrap()).unwrap();
        std::fs::write(&managed, skill.content).unwrap();
        push_generated_delete(&mut plan, managed.clone(), DeleteKind::ManagedSkill);
        assert_eq!(plan.len(), 1, "the managed control must be planned");
    }

    #[test]
    fn grok_handoff_rule_sweep_spares_unmanaged_file() {
        let tmp = tempfile::tempdir().unwrap();
        let user = tmp.path().join("rules").join("ai-memory-handoff.md");
        std::fs::create_dir_all(user.parent().unwrap()).unwrap();
        std::fs::write(&user, "# my grok rule\n").unwrap();
        let mut plan = Vec::new();
        push_generated_delete(&mut plan, user.clone(), DeleteKind::GrokHandoffRule);
        assert!(
            plan.is_empty(),
            "an unmanaged Grok rule must never be planned: {plan:?}"
        );

        let managed = tmp
            .path()
            .join("ours")
            .join("rules")
            .join("ai-memory-handoff.md");
        std::fs::create_dir_all(managed.parent().unwrap()).unwrap();
        std::fs::write(&managed, GROK_HANDOFF_RULE_MARKER).unwrap();
        push_generated_delete(&mut plan, managed.clone(), DeleteKind::GrokHandoffRule);
        assert_eq!(plan.len(), 1, "the managed Grok rule must be planned");
    }

    #[test]
    fn generated_grok_handoff_rule_detection_requires_marker() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("ai-memory-handoff.md");
        std::fs::write(&path, "# user rule\n").unwrap();
        assert!(!generated_file_is_ours(&path, DeleteKind::GrokHandoffRule));
        std::fs::write(&path, format!("{GROK_HANDOFF_RULE_MARKER}\n# rule\n")).unwrap();
        assert!(generated_file_is_ours(&path, DeleteKind::GrokHandoffRule));
    }

    /// `install-skills --agent copilot-cli` writes `.github/skills` in the
    /// project and `$COPILOT_HOME/skills` globally; uninstall must sweep both.
    #[test]
    fn skill_roots_sweep_copilot_cli_project_and_relocated_home() {
        let roots = skill_roots(
            Path::new("/repo"),
            Some(Path::new("/home/alice")),
            None,
            None,
            Some(Path::new("/custom/copilot")),
            None,
        );
        assert!(
            roots.contains(&PathBuf::from("/repo/.github/skills")),
            "{roots:?}"
        );
        assert!(
            roots.contains(&PathBuf::from("/custom/copilot/skills")),
            "{roots:?}"
        );
        assert!(!roots.contains(&PathBuf::from("/home/alice/.copilot/skills")));

        let default_home = skill_roots(
            Path::new("/repo"),
            Some(Path::new("/home/alice")),
            None,
            None,
            None,
            None,
        );
        assert!(
            default_home.contains(&PathBuf::from("/home/alice/.copilot/skills")),
            "{default_home:?}"
        );
    }

    #[test]
    fn skill_roots_sweep_both_antigravity_global_roots() {
        let roots = skill_roots(
            Path::new("/repo"),
            Some(Path::new("/home/alice")),
            None,
            None,
            None,
            None,
        );
        for root in [
            "/home/alice/.gemini/config/skills",
            "/home/alice/.gemini/antigravity-cli/skills",
        ] {
            assert!(roots.contains(&PathBuf::from(root)), "{root}: {roots:?}");
        }
    }

    #[test]
    fn strip_instructions_round_trips_with_install_append() {
        let original = "# Title\n";
        // Mirror install_instructions::merge append behavior:
        let block = format!("{MARKER_START}\nBODY\n{MARKER_END}\n");
        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"
        );
    }

    /// Regression: a managed block whose body mentions the end marker
    /// inline must be stripped up to the REAL delimiter, not the inline
    /// mention — otherwise an orphan tail survives the uninstall.
    #[test]
    fn strip_ignores_inline_marker_mention() {
        let original = "# Title\n";
        let block =
            format!("{MARKER_START}\nsee the `{MARKER_END}` marker inline\nbody\n{MARKER_END}\n");
        let installed = format!("{original}\n{block}");
        let (stripped, found) = strip_instructions_block(&installed);
        assert!(found);
        assert_eq!(
            stripped, original,
            "no orphan tail after the real end marker"
        );
    }

    #[test]
    fn strip_consumes_crlf_after_end_marker() {
        let content = format!("# Top\r\n\r\n{MARKER_START}\r\nBODY\r\n{MARKER_END}\r\nMore\r\n");
        let (stripped, found) = strip_instructions_block(&content);
        assert!(found);
        assert_eq!(stripped, "# Top\r\n\r\nMore\r\n");
    }

    #[test]
    fn strip_removes_exact_legacy_orphan_tail() {
        let content =
            format!("# Top\n\n{MARKER_START}\nBODY\n{MARKER_END}\n{LEGACY_ORPHAN_TAIL_LF}More\n");
        let (stripped, found) = strip_instructions_block(&content);
        assert!(found);
        assert_eq!(stripped, "# Top\n\nMore\n");
    }

    #[test]
    fn strip_removes_repeated_legacy_orphan_tails() {
        let content = format!(
            "# Top\n\n{MARKER_START}\nBODY\n{MARKER_END}\n{LEGACY_ORPHAN_TAIL_LF}{LEGACY_ORPHAN_TAIL_CRLF}More\n"
        );
        let (stripped, found) = strip_instructions_block(&content);
        assert!(found);
        assert_eq!(stripped, "# Top\n\nMore\n");
    }

    #[test]
    fn strip_instructions_preserves_surrounding_content() {
        let content = format!("# Top\n\n{MARKER_START}\nBODY\n{MARKER_END}\n\nMore notes.\n");
        let (stripped, found) = strip_instructions_block(&content);
        assert!(found);
        assert!(stripped.contains("# Top"));
        assert!(stripped.contains("More notes."));
        assert!(!stripped.contains("BODY"));
        assert!(!stripped.contains(MARKER_START));
    }

    #[test]
    fn strip_instructions_no_block_is_noop() {
        let content = "# Just a readme\n";
        let (stripped, found) = strip_instructions_block(content);
        assert!(!found);
        assert_eq!(stripped, content);
    }

    #[test]
    fn hook_signature_matches_no_auth_default() {
        let cmd = "AI_MEMORY_HOOK_URL=http://127.0.0.1:49374 /home/u/.local/share/ai-memory/hooks/claude-code/stop.sh";
        assert!(hook_command_is_ours(cmd));
    }

    #[test]
    fn hook_signature_matches_with_auth_and_custom_prefix() {
        let cmd = "AI_MEMORY_HOOK_URL=http://lan:49374 AI_MEMORY_AUTH_TOKEN=abc /etc/custom/session-start.sh";
        assert!(hook_command_is_ours(cmd));
    }

    #[test]
    fn hook_signature_matches_native_posix_command() {
        let cmd = "'/home/alice/.cargo/bin/ai-memory' --data-dir '/tmp/custom data' hook --event session-start --agent claude-code --server-url http://h:49374";
        assert!(hook_command_is_ours(cmd));
    }

    #[test]
    fn hook_signature_matches_native_windows_command() {
        let cmd = r#""C:\Users\alice\bin\ai-memory.exe" --data-dir "C:\Users\alice\AppData\Local\ai-memory" hook --event session-start --agent claude-code --server-url "http://h:49374""#;
        assert!(hook_command_is_ours(cmd));
    }

    /// #515 gave Codex's Windows command a leading `& ` call operator.
    /// `hook_command_is_ours` matches on substrings, so the prefix is
    /// harmless — but if that ever became a prefix match, uninstall would
    /// silently stop finding Codex hooks and leave them behind.
    #[test]
    fn hook_signature_matches_a_powershell_call_operator_command() {
        let cmd = r#"& "C:\Users\alice\bin\ai-memory.exe" --data-dir "C:\Users\alice\AppData\Local\ai-memory" hook --event session-start --agent codex --server-url "http://h:49374""#;
        assert!(hook_command_is_ours(cmd));
    }

    #[test]
    fn hook_signature_matches_underscore_executable_name() {
        // On Windows a test binary (and some packaging) names the executable
        // `ai_memory` rather than `ai-memory`; the native command then uses that
        // path. Recognizing only the hyphen made a reapply non-idempotent (#740).
        let cmd = r#""C:\Users\alice\target\debug\ai_memory.exe" hook --event session-start --agent kimi-code --server-url "http://h:49374""#;
        assert!(hook_command_is_ours(cmd));
        // The argv signature still gates it: an unrelated `ai_memory`-named tool
        // without our hook subcommand is not claimed.
        assert!(!hook_command_is_ours(
            r#""C:\bin\ai_memory_helper.exe" --do-something-else"#
        ));
    }

    #[test]
    fn hook_signature_rejects_third_party_with_generic_name() {
        // A user's own hook that happens to be named stop.sh — no prefix.
        assert!(!hook_command_is_ours("/usr/local/bin/my-stop.sh"));
        assert!(!hook_command_is_ours("/opt/tools/hooks/session-start.sh"));
        assert!(!hook_command_is_ours(
            "/usr/local/bin/something hook --event stop --agent claude-code --server-url http://h"
        ));
    }

    #[test]
    fn strip_hooks_nested_removes_ours_keeps_third_party() {
        let content = r#"{
      "hooks": {
        "SessionStart": [
          {"matcher":"","hooks":[{"type":"command","command":"AI_MEMORY_HOOK_URL=http://h /x/session-start.sh"}]}
        ],
        "Notification": [
          {"matcher":"","hooks":[{"type":"command","command":"/usr/bin/notify.sh"}]}
        ]
      }
    }"#;
        let out = strip_ai_memory_hooks(content).unwrap();
        assert_eq!(out.removed_events, vec!["SessionStart".to_string()]);
        let v: serde_json::Value = serde_json::from_str(&out.new_content).unwrap();
        assert!(v["hooks"].get("SessionStart").is_none(), "our event pruned");
        assert!(v["hooks"].get("Notification").is_some(), "third-party kept");
    }

    #[test]
    fn strip_hooks_flat_cursor_shape() {
        let content = r#"{
      "version": 1,
      "hooks": {
        "stop": [
          {"type":"command","command":"AI_MEMORY_HOOK_URL=http://h /x/stop.sh","matcher":""}
        ]
      }
    }"#;
        let out = strip_ai_memory_hooks(content).unwrap();
        assert_eq!(out.removed_events, vec!["stop".to_string()]);
        let v: serde_json::Value = serde_json::from_str(&out.new_content).unwrap();
        assert!(v["hooks"].get("stop").is_none());
        assert_eq!(v["version"], 1, "sibling top-level key preserved");
    }

    #[test]
    fn strip_hooks_prunes_emptied_hooks_object() {
        let content = r#"{"hooks":{"Stop":[{"type":"command","command":"AI_MEMORY_HOOK_URL=x /a/stop.sh"}]}}"#;
        let out = strip_ai_memory_hooks(content).unwrap();
        let v: serde_json::Value = serde_json::from_str(&out.new_content).unwrap();
        assert!(v.get("hooks").is_none(), "emptied hooks object removed");
    }

    #[test]
    fn strip_hooks_preserves_third_party_with_generic_basename() {
        let content = r#"{
      "hooks": {
        "Stop": [
          {"matcher":"","hooks":[{"type":"command","command":"AI_MEMORY_HOOK_URL=x /a/stop.sh"}]},
          {"matcher":"","hooks":[{"type":"command","command":"/home/u/scripts/stop.sh"}]}
        ]
      }
    }"#;
        let out = strip_ai_memory_hooks(content).unwrap();
        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")
        );
    }

    #[test]
    fn strip_hooks_nested_mixed_inner_commands_preserves_user_hook() {
        let content = r#"{
      "hooks": {
        "Stop": [
          {"matcher":"","hooks":[
            {"type":"command","command":"AI_MEMORY_HOOK_URL=x /a/stop.sh"},
            {"type":"command","command":"/home/u/scripts/my-stop.sh"}
          ]}
        ]
      }
    }"#;

        let out = strip_ai_memory_hooks(content).unwrap();
        assert_eq!(out.removed_events, vec!["Stop".to_string()]);
        let v: serde_json::Value = serde_json::from_str(&out.new_content).unwrap();
        let inner = v["hooks"]["Stop"][0]["hooks"].as_array().unwrap();
        assert_eq!(inner.len(), 1, "third-party inner hook must survive");
        assert_eq!(
            inner[0]["command"].as_str(),
            Some("/home/u/scripts/my-stop.sh")
        );
    }

    #[test]
    fn strip_hooks_removes_exec_form_ours_preserves_exec_third_party_and_sibling() {
        let content = r#"{
      "hooks": {
        "SessionStart": [
          {"matcher":"","hooks":[
            {"type":"command","command":"C:\\bin\\ai-memory.exe","args":["hook","--event","session-start","--agent","claude-code","--server-url","http://h"]},
            {"type":"command","command":"C:\\bin\\third-party.exe","args":["hook","--event","session-start","--agent","claude-code","--server-url","http://h"]}
          ]},
          {"matcher":"Tool","hooks":[
            {"type":"command","command":"C:\\bin\\other.exe","args":["--keep"]}
          ]}
        ],
        "Stop": [
          {"matcher":"","hooks":[{"type":"command","command":"\"C:\\bin\\ai-memory.exe\" hook --event stop --agent claude-code --server-url \"http://h\""}]}
        ]
      }
    }"#;

        let out = strip_ai_memory_hooks(content).unwrap();
        assert_eq!(
            out.removed_events,
            vec!["SessionStart".to_string(), "Stop".to_string()]
        );
        let v: serde_json::Value = serde_json::from_str(&out.new_content).unwrap();
        assert!(
            v["hooks"].get("Stop").is_none(),
            "legacy string hook removed"
        );
        let entries = v["hooks"]["SessionStart"].as_array().unwrap();
        assert_eq!(entries.len(), 2, "outer sibling group preserved");
        let first_inner = entries[0]["hooks"].as_array().unwrap();
        assert_eq!(
            first_inner.len(),
            1,
            "only ai-memory inner exec hook removed"
        );
        assert_eq!(
            first_inner[0]["command"].as_str(),
            Some(r"C:\bin\third-party.exe")
        );
        assert_eq!(
            entries[1]["hooks"][0]["command"].as_str(),
            Some(r"C:\bin\other.exe")
        );
    }

    #[test]
    fn strip_hooks_no_hooks_key_is_noop() {
        let content = r#"{"unrelated":true}"#;
        let out = strip_ai_memory_hooks(content).unwrap();
        assert!(out.removed_events.is_empty());
    }

    // Issue #156: uninstall removes only ai-memory's entries from Zero's
    // hooks.json, keyed by the id prefix, and leaves everything else alone.
    #[test]
    fn strip_zero_hooks_removes_only_prefixed_ids() {
        let content = r#"{"enabled": true, "hooks": [
            {"id": "my-custom-hook", "event": "beforeTool",
             "command": "/usr/bin/true", "args": [], "enabled": true},
            {"id": "ai-memory-session-start", "event": "sessionStart",
             "command": "/usr/local/bin/ai-memory", "args": ["hook"], "enabled": true},
            {"id": "ai-memory-post-tool-use", "event": "afterTool",
             "command": "/usr/local/bin/ai-memory", "args": ["hook"], "enabled": true}
        ]}"#;
        let removal = strip_zero_hooks(content).unwrap();
        assert_eq!(
            removal.removed_events,
            vec!["ai-memory-session-start", "ai-memory-post-tool-use"]
        );
        let root: serde_json::Value = serde_json::from_str(&removal.new_content).unwrap();
        let hooks = root["hooks"].as_array().unwrap();
        assert_eq!(hooks.len(), 1);
        assert_eq!(hooks[0]["id"], serde_json::json!("my-custom-hook"));
        assert_eq!(
            root["enabled"],
            serde_json::json!(true),
            "the top-level enabled flag is not ours to touch"
        );
    }

    // Devin's hooks.v1.json: the root object IS the event map (no "hooks"
    // wrapper). Mirrors strip_zero_hooks_removes_only_prefixed_ids above,
    // but for Devin's flat shape and command-signature ownership (entries
    // have no "id" field to prefix-match on).
    #[test]
    fn strip_ai_memory_hooks_flat_removes_only_ours_and_preserves_third_party() {
        let content = r#"{
            "SessionStart": [
                {"type": "command", "command": "AI_MEMORY_HOOK_URL=http://h /x/session-start.sh"}
            ],
            "PostToolUse": [
                {"type": "command", "command": "AI_MEMORY_HOOK_URL=http://h /x/post-tool-use.sh"},
                {"type": "command", "command": "/home/u/scripts/my-logger.sh"}
            ]
        }"#;
        let out = strip_ai_memory_hooks_flat(content).unwrap();
        assert_eq!(
            out.removed_events,
            vec!["SessionStart".to_string(), "PostToolUse".to_string()]
        );
        let v: serde_json::Value = serde_json::from_str(&out.new_content).unwrap();
        assert!(v.get("SessionStart").is_none(), "emptied event key removed");
        let post_tool_use = v["PostToolUse"].as_array().unwrap();
        assert_eq!(post_tool_use.len(), 1, "only ours removed");
        assert_eq!(
            post_tool_use[0]["command"].as_str(),
            Some("/home/u/scripts/my-logger.sh")
        );
        assert!(
            v.get("hooks").is_none(),
            "flat file must not gain a nested 'hooks' wrapper"
        );
    }

    #[test]
    fn strip_antigravity_hooks_removes_only_signed_entries() {
        let content = r#"{
          "ai-memory": {
            "PreInvocation": [
              {"type":"command","command":"AI_MEMORY_HOOK_URL=http://h /x/session-start.sh"},
              {"type":"command","command":"/usr/bin/user-hook"}
            ],
            "Stop": [
              {"type":"command","command":"AI_MEMORY_HOOK_URL=http://h /x/stop.sh"}
            ]
          },
          "my-group": {
            "Stop": [{"type":"command","command":"/usr/bin/other"}]
          }
        }"#;

        let out = strip_antigravity_hooks(content).unwrap();
        assert_eq!(
            out.removed_events,
            vec![
                "ai-memory.PreInvocation".to_string(),
                "ai-memory.Stop".to_string()
            ]
        );
        let v: serde_json::Value = serde_json::from_str(&out.new_content).unwrap();
        assert_eq!(v["ai-memory"]["PreInvocation"].as_array().unwrap().len(), 1);
        assert!(v["ai-memory"].get("Stop").is_none());
        assert!(v.get("my-group").is_some());
    }

    #[test]
    fn strip_antigravity_hooks_preserves_mixed_nested_user_hook() {
        let content = r#"{
          "ai-memory": {
            "Stop": [
              {"matcher":"","hooks":[
                {"type":"command","command":"AI_MEMORY_HOOK_URL=http://h /x/stop.sh"},
                {"type":"command","command":"/usr/bin/user-stop"}
              ]}
            ]
          }
        }"#;

        let out = strip_antigravity_hooks(content).unwrap();
        assert_eq!(out.removed_events, vec!["ai-memory.Stop".to_string()]);
        let v: serde_json::Value = serde_json::from_str(&out.new_content).unwrap();
        let inner = v["ai-memory"]["Stop"][0]["hooks"].as_array().unwrap();
        assert_eq!(inner.len(), 1);
        assert_eq!(inner[0]["command"].as_str(), Some("/usr/bin/user-stop"));
    }

    #[test]
    fn generated_file_detection_rejects_user_files_at_ours_path() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("ai-memory.ts");
        std::fs::write(&path, "// my personal plugin named ai-memory\n").unwrap();

        assert!(!generated_file_is_ours(&path, DeleteKind::OpenCodePlugin));

        std::fs::write(
            &path,
            "// Auto-generated by `ai-memory install-hooks --agent opencode --apply`.\nconst AGENT = \"open-code\";\n",
        )
        .unwrap();
        assert!(generated_file_is_ours(&path, DeleteKind::OpenCodePlugin));
    }

    #[test]
    fn generated_cline_hook_detection_requires_the_full_signature() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("TaskStart.sh");
        std::fs::write(
            &path,
            "#!/bin/sh\n# Generated by ai-memory install-hooks --agent cline.\nexec 'other' 'hook' '--agent' 'cline'\n",
        )
        .unwrap();
        assert!(!generated_file_is_ours(&path, DeleteKind::ClineHook));

        std::fs::write(
            &path,
            "#!/bin/sh\n# Generated by ai-memory install-hooks --agent cline.\nexec 'ai-memory' 'hook' '--agent' 'cline'\n",
        )
        .unwrap();
        assert!(generated_file_is_ours(&path, DeleteKind::ClineHook));
    }

    #[test]
    fn generated_openclaw_package_detection_requires_our_shape() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("package.json");
        std::fs::write(&path, r#"{"name":"@ai-memory/openclaw-plugin"}"#).unwrap();

        assert!(!generated_file_is_ours(
            &path,
            DeleteKind::OpenClawPackageJson
        ));

        std::fs::write(&path, openclaw_plugin::package_json()).unwrap();
        assert!(generated_file_is_ours(
            &path,
            DeleteKind::OpenClawPackageJson
        ));

        std::fs::write(
            &path,
            r#"{"name":"@ai-memory/openclaw-plugin","version":"0.0.1","private":true,"type":"module","openclaw":{"extensions":["./index.ts"]}}"#,
        )
        .unwrap();
        assert!(
            generated_file_is_ours(&path, DeleteKind::OpenClawPackageJson),
            "older generated package versions should still uninstall"
        );
    }

    #[test]
    fn generated_openclaw_manifest_detection_requires_our_shape() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("openclaw.plugin.json");
        std::fs::write(
            &path,
            r#"{"id":"ai-memory","name":"ai-memory","description":"custom user plugin"}"#,
        )
        .unwrap();

        assert!(!generated_file_is_ours(&path, DeleteKind::OpenClawManifest));

        std::fs::write(&path, openclaw_plugin::manifest_json()).unwrap();
        assert!(generated_file_is_ours(&path, DeleteKind::OpenClawManifest));

        std::fs::write(
            &path,
            r#"{"id":"ai-memory","name":"ai-memory","description":"older generated text","activation":{"onCapabilities":["hook"]},"configSchema":{"type":"object","additionalProperties":false,"properties":{}}}"#,
        )
        .unwrap();
        assert!(
            generated_file_is_ours(&path, DeleteKind::OpenClawManifest),
            "older generated manifest descriptions should still uninstall"
        );
    }

    #[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,
            Some("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());
        assert!(v["mcpServers"].get("other").is_some());
    }

    #[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,
            None,
            "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"
        );
    }

    #[test]
    fn strip_mcp_name_only_does_not_remove_user_entry() {
        let content = r#"{"mcpServers":{"ai-memory":{"url":"http://example.invalid/mcp"}}}"#;
        let (out, removed) = strip_mcp_json(
            content,
            McpClient::ClaudeCode,
            Some("ai-memory"),
            "http://127.0.0.1:49374/mcp",
        )
        .unwrap();

        assert!(removed.is_empty());
        let v: serde_json::Value = serde_json::from_str(&out).unwrap();
        assert!(v["mcpServers"].get("ai-memory").is_some());
    }

    #[test]
    fn strip_mcp_antigravity_server_url() {
        let content = r#"{"mcpServers":{"mem":{"serverUrl":"http://127.0.0.1:49374/mcp"},"other":{"serverUrl":"http://x"}}}"#;
        let (out, removed) = strip_mcp_json(
            content,
            McpClient::AntigravityCli,
            None,
            "http://127.0.0.1:49374/mcp",
        )
        .unwrap();

        assert_eq!(removed, vec!["mem".to_string()]);
        let v: serde_json::Value = serde_json::from_str(&out).unwrap();
        assert!(v["mcpServers"].get("mem").is_none());
        assert!(v["mcpServers"].get("other").is_some());
    }

    #[test]
    fn strip_mcp_omp_root_servers() {
        let content = r#"{"mcpServers":{"ai-memory":{"type":"http","url":"http://127.0.0.1:49374/mcp","enabled":true}}}"#;
        let (out, removed) = strip_mcp_json(
            content,
            McpClient::Omp,
            Some("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.get("mcpServers").is_none());
    }

    #[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,
            None,
            "http://127.0.0.1:49374/mcp",
        )
        .unwrap();
        assert_eq!(removed, vec!["weird-name".to_string()]);
    }

    #[test]
    fn strip_mcp_claude_code_session_aware_bridge() {
        let content = r#"{"mcpServers":{"ai-memory":{"type":"stdio","command":"ai-memory","args":["mcp-bridge","--server-url","http://127.0.0.1:49374/mcp"]},"other":{"command":"other","args":["mcp-bridge","--server-url","http://127.0.0.1:49374/mcp"]}}}"#;
        let (out, removed) = strip_mcp_json(
            content,
            McpClient::ClaudeCode,
            None,
            "http://127.0.0.1:49374/mcp",
        )
        .unwrap();

        assert_eq!(removed, vec!["ai-memory".to_string()]);
        let value: serde_json::Value = serde_json::from_str(&out).unwrap();
        assert!(value["mcpServers"].get("ai-memory").is_none());
        assert!(value["mcpServers"].get("other").is_some());
    }

    #[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,
            Some("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());
    }

    #[test]
    fn strip_mcp_vscode_copilot_root_servers() {
        let content = r#"{"servers":{"ai-memory":{"type":"http","url":"http://127.0.0.1:49374/mcp"},"other":{"type":"http","url":"http://x"}}}"#;
        let (out, removed) = strip_mcp_json(
            content,
            McpClient::VsCodeCopilot,
            Some("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["servers"].get("ai-memory").is_none());
        assert!(v["servers"].get("other").is_some());
    }

    /// Copilot CLI keeps servers under the root `mcpServers` map; the strip
    /// must remove only ai-memory's entry and leave e.g. a `github` server.
    #[test]
    fn strip_mcp_copilot_cli_root_mcp_servers() {
        let content = r#"{"mcpServers":{"ai-memory":{"type":"http","url":"http://127.0.0.1:49374/mcp","tools":["*"]},"github":{"type":"http","url":"https://api.githubcopilot.com/mcp/"}}}"#;
        let (out, removed) = strip_mcp_json(
            content,
            McpClient::CopilotCli,
            Some("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());
        assert!(v["mcpServers"].get("github").is_some());
    }

    /// Prime Agent's settings.json carries every other Prime Agent setting
    /// beside `mcpServers`; the strip removes only ai-memory's entry.
    #[test]
    fn strip_mcp_prime_agent_keeps_other_settings_and_servers() {
        let content = r#"{"defaultModel":"glm","mcpServers":{"ai-memory":{"type":"http","url":"http://127.0.0.1:49374/mcp","bearerTokenEnvVar":"AI_MEMORY_AUTH_TOKEN"},"search":{"type":"http","url":"https://search.example/mcp"}}}"#;
        let (out, removed) = strip_mcp_json(
            content,
            McpClient::PrimeAgent,
            Some("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());
        assert!(v["mcpServers"].get("search").is_some());
        assert_eq!(v["defaultModel"], "glm");
    }

    #[test]
    fn strip_mcp_zed_context_servers_preserves_other_settings() {
        let content = r#"{
  // Keep this user comment.
  "theme": "One Dark",
  "context_servers": {
    "ai-memory": { "url": "http://127.0.0.1:49374/mcp" },
    // Keep this sibling comment.
    "other": { "url": "http://x" },
  },
}"#;
        let (out, removed) = strip_mcp_json_client(
            content,
            McpClient::Zed,
            Some("ai-memory"),
            "http://127.0.0.1:49374/mcp",
        )
        .unwrap();

        assert_eq!(removed, vec!["ai-memory".to_string()]);
        assert!(out.contains("// Keep this user comment."));
        assert!(out.contains("// Keep this sibling comment."));
        let root =
            jsonc_parser::cst::CstRootNode::parse(&out, &jsonc_parser::ParseOptions::default())
                .unwrap();
        let value = root.to_serde_value().unwrap();
        assert_eq!(value["theme"], "One Dark");
        assert!(value["context_servers"].get("ai-memory").is_none());
        assert!(value["context_servers"].get("other").is_some());
    }

    #[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,
            Some("ai-memory"),
            "http://127.0.0.1:49374/mcp",
        )
        .unwrap();
        assert!(removed.is_empty());
    }

    /// The stdio bridge entries `install-mcp` writes on a token-file install
    /// are ours when they target this server, in every client shape, and a
    /// bridge aimed at another server is not.
    #[test]
    fn bridge_entries_are_recognised_by_their_upstream_url() {
        let url = "http://127.0.0.1:49374/mcp";
        let bridge = |target: &str| {
            serde_json::json!({
                "type": "stdio",
                "command": "ai-memory",
                "args": ["mcp-bridge", "--no-session-id", "--server-url", target],
            })
        };
        let opencode = serde_json::json!({
            "type": "local",
            "command": ["ai-memory", "mcp-bridge", "--no-session-id", "--server-url", url],
            "enabled": true,
        });
        let cline = serde_json::json!({
            "transport": {
                "type": "stdio",
                "command": "ai-memory",
                "args": ["mcp-bridge", "--no-session-id", "--server-url", url],
            },
            "disabled": false,
            "autoApprove": [],
        });
        assert!(mcp_entry_is_ours(
            "ai-memory",
            &bridge(url),
            Some("ai-memory"),
            url
        ));
        assert!(mcp_entry_is_ours(
            "ai-memory",
            &opencode,
            Some("ai-memory"),
            url
        ));
        assert!(mcp_entry_is_ours(
            "ai-memory",
            &cline,
            Some("ai-memory"),
            url
        ));
        assert!(!mcp_entry_is_ours(
            "ai-memory",
            &bridge("https://team.example/mcp"),
            Some("ai-memory"),
            url
        ));
        let foreign =
            serde_json::json!({"command": "other", "args": ["mcp-bridge", "--server-url", url]});
        assert!(!mcp_entry_is_ours(
            "ai-memory",
            &foreign,
            Some("ai-memory"),
            url
        ));

        let toml = format!(
            "[mcp_servers.ai-memory]\ncommand = \"ai-memory\"\nargs = [\"mcp-bridge\", \"--no-session-id\", \"--server-url\", \"{url}\"]\n\n[mcp_servers.other]\ncommand = \"other\"\n"
        );
        let (out, removed) = strip_mcp_toml(&toml, Some("ai-memory"), url).unwrap();
        assert_eq!(removed, vec!["ai-memory".to_string()]);
        assert!(out.contains("[mcp_servers.other]"), "{out}");
        let (_, kept) =
            strip_mcp_toml(&toml, Some("ai-memory"), "https://team.example/mcp").unwrap();
        assert!(kept.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, Some("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]"));
        assert!(!out.contains("[mcp_servers.ai-memory]"));
    }

    #[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, None, "http://127.0.0.1:49374/mcp").unwrap();
        assert_eq!(removed, vec!["custom".to_string()]);
        assert!(!out.contains("custom"));
    }

    #[test]
    fn strip_mcp_toml_inline_table_by_url_under_custom_name() {
        let content = "mcp_servers = { custom = { url = \"http://127.0.0.1:49374/mcp\" }, other = { url = \"http://x\" } }\n";
        let (out, removed) = strip_mcp_toml(content, None, "http://127.0.0.1:49374/mcp").unwrap();
        assert_eq!(removed, vec!["custom".to_string()]);
        assert!(!out.contains("custom"));
        assert!(out.contains("other"));
    }

    #[test]
    fn strip_mcp_toml_inline_table_prunes_when_empty() {
        let content = "mcp_servers = { ai-memory = { url = \"http://127.0.0.1:49374/mcp\" } }\n";
        let (out, removed) = strip_mcp_toml(content, None, "http://127.0.0.1:49374/mcp").unwrap();
        assert_eq!(removed, vec!["ai-memory".to_string()]);
        assert!(!out.contains("mcp_servers"));
    }

    #[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, Some("ai-memory"), "http://127.0.0.1:49374/mcp").unwrap();
        assert!(removed.is_empty());
    }

    #[test]
    fn strip_kimi_code_hooks_removes_only_ours_preserving_config() {
        let content = r#"model = "kimi-k2"

[providers.kimi]
api_key = "sk-user-key"

[[hooks]]
event = "SessionStart"
command = "AI_MEMORY_HOOK_URL=http://h /x/session-start.sh"

[[hooks]]
event = "SessionStart"
matcher = "*"
command = "/usr/bin/user-session-start"
timeout = 10

[[hooks]]
event = "Stop"
command = "'/usr/local/bin/ai-memory' hook --event stop --agent kimi-code --server-url http://h:49374"
"#;
        let out = strip_kimi_code_hooks(content).unwrap();
        assert_eq!(
            out.removed_events,
            vec![
                "kimi-code.SessionStart".to_string(),
                "kimi-code.Stop".to_string()
            ]
        );
        let doc: toml_edit::DocumentMut = out.new_content.parse().unwrap();
        assert_eq!(doc.get("model").and_then(|m| m.as_str()), Some("kimi-k2"));
        assert_eq!(
            doc.get("providers")
                .and_then(|p| p.get("kimi"))
                .and_then(|k| k.get("api_key"))
                .and_then(|k| k.as_str()),
            Some("sk-user-key")
        );
        let hooks = doc
            .get("hooks")
            .and_then(toml_edit::Item::as_array_of_tables)
            .expect("third-party hooks must survive");
        assert_eq!(hooks.len(), 1, "only ai-memory rules removed");
        let kept = hooks.get(0).unwrap();
        assert_eq!(
            kept.get("command").and_then(|c| c.as_str()),
            Some("/usr/bin/user-session-start")
        );
        assert_eq!(kept.get("matcher").and_then(|m| m.as_str()), Some("*"));
        assert_eq!(kept.get("timeout").and_then(|t| t.as_integer()), Some(10));
    }

    #[test]
    fn strip_kimi_code_hooks_all_ours_leaves_no_hooks_section() {
        let content = "[providers.kimi]\napi_key = \"sk-user-key\"\n\n[[hooks]]\nevent = \"Stop\"\ncommand = \"AI_MEMORY_HOOK_URL=x /a/stop.sh\"\n";
        let out = strip_kimi_code_hooks(content).unwrap();
        assert_eq!(out.removed_events, vec!["kimi-code.Stop".to_string()]);
        assert!(
            !out.new_content.contains("hooks"),
            "emptied [[hooks]] array must serialize to nothing: {}",
            out.new_content
        );
        assert!(out.new_content.contains("[providers.kimi]"));
    }

    #[test]
    fn strip_kimi_code_hooks_no_hooks_key_is_noop() {
        let content = "model = \"kimi-k2\"\n";
        let out = strip_kimi_code_hooks(content).unwrap();
        assert!(out.removed_events.is_empty());
        assert_eq!(out.new_content, content);
    }

    #[test]
    fn strip_mcp_kimi_code_root_servers() {
        let content = r#"{"mcpServers":{"ai-memory":{"url":"http://127.0.0.1:49374/mcp","headers":{"X-Token":"t"}},"other":{"url":"http://x"}}}"#;
        let (out, removed) = strip_mcp_json(
            content,
            McpClient::KimiCode,
            Some("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());
        assert!(v["mcpServers"].get("other").is_some());
    }

    #[test]
    fn strip_mcp_swival_root_servers_preserves_siblings() {
        let content = r#"{"mcpServers":{"ai-memory":{"type":"http","url":"http://127.0.0.1:49374/mcp"},"other":{"command":"other-mcp"}}}"#;
        let (out, removed) = strip_mcp_json_client(
            content,
            McpClient::Swival,
            Some("ai-memory"),
            "http://127.0.0.1:49374/mcp",
        )
        .unwrap();

        assert_eq!(removed, vec!["ai-memory".to_string()]);
        let value: serde_json::Value = serde_json::from_str(&out).unwrap();
        assert!(value["mcpServers"].get("ai-memory").is_none());
        assert_eq!(value["mcpServers"]["other"]["command"], "other-mcp");
    }

    #[test]
    fn mcp_url_candidates_covers_every_installable_flavor() {
        // Was keyed off the client's built-in default. `install-mcp --flavor`
        // can now write any marker into any client's config, so candidates are
        // keyed off what the installer can write instead — otherwise an entry
        // an operator pinned by hand survives `uninstall`.
        assert_eq!(
            mcp_url_candidates(McpClient::KimiCode, "http://127.0.0.1:49374/mcp"),
            vec![
                "http://127.0.0.1:49374/mcp".to_string(),
                "http://127.0.0.1:49374/mcp?flavor=moonshot".to_string(),
                "http://127.0.0.1:49374/mcp?flavor=bedrock".to_string(),
                "http://127.0.0.1:49374/mcp?flavor=gemini".to_string(),
            ]
        );
        // An already-flavored URL is not duplicated into the list.
        assert_eq!(
            mcp_url_candidates(
                McpClient::KimiCode,
                "http://127.0.0.1:49374/mcp?flavor=moonshot"
            )[0],
            "http://127.0.0.1:49374/mcp?flavor=moonshot".to_string()
        );
        assert_eq!(
            mcp_url_candidates(
                McpClient::KimiCode,
                "http://127.0.0.1:49374/mcp?flavor=moonshot"
            )
            .iter()
            .filter(|c| c.ends_with("flavor=moonshot"))
            .count(),
            1
        );
        // The case the broadening exists for: Command Code has no default
        // flavor, but an operator on a Vertex-backed model pins one.
        assert!(
            mcp_url_candidates(McpClient::CommandCode, "https://memory.example/mcp")
                .contains(&"https://memory.example/mcp?flavor=gemini".to_string())
        );
        // Kiro's own default is still matched.
        assert!(
            mcp_url_candidates(McpClient::KiroCli, "https://memory.example/mcp")
                .contains(&"https://memory.example/mcp?flavor=bedrock".to_string())
        );
    }

    /// install-mcp writes the flavored URL while `--mcp-url` defaults
    /// unflavored — the strip must still match.
    #[test]
    fn strip_mcp_kimi_code_matches_flavored_url_with_default_mcp_url() {
        let content = r#"{"mcpServers":{"ai-memory":{"url":"http://127.0.0.1:49374/mcp?flavor=moonshot","headers":{"X-Token":"t"}},"other":{"url":"http://x"}}}"#;
        let (out, removed) = strip_mcp_json_client(
            content,
            McpClient::KimiCode,
            Some("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());
        assert!(v["mcpServers"].get("other").is_some());
    }

    #[test]
    fn strip_mcp_kiro_matches_bedrock_flavored_url() {
        let content = r#"{"mcpServers":{"ai-memory":{"url":"https://memory.example/mcp?flavor=bedrock"},"other":{"url":"https://other.example/mcp"}}}"#;
        let (out, removed) = strip_mcp_json_client(
            content,
            McpClient::KiroCli,
            Some("ai-memory"),
            "https://memory.example/mcp",
        )
        .unwrap();
        assert_eq!(removed, vec!["ai-memory".to_string()]);
        let value: serde_json::Value = serde_json::from_str(&out).unwrap();
        assert!(value["mcpServers"].get("ai-memory").is_none());
        assert!(value["mcpServers"].get("other").is_some());
    }

    #[test]
    fn kiro_v3_uninstall_ownership_requires_exact_name_trigger_and_command() {
        let tmp = tempfile::TempDir::new().unwrap();
        let path = tmp.path().join("ai-memory.json");
        let command = "'/usr/bin/ai-memory' hook --event session-start --agent kiro-cli --server-url https://memory.example";
        std::fs::write(
            &path,
            serde_json::to_string(&serde_json::json!({
                "version": "v1",
                "hooks": [{
                    "name": "ai-memory-session-start",
                    "trigger": "SessionStart",
                    "action": {"type": "command", "command": command}
                }]
            }))
            .unwrap(),
        )
        .unwrap();
        assert!(generated_file_is_ours(&path, DeleteKind::KiroCliV3Hooks));

        let shared = serde_json::json!({
            "version": "v1",
            "hooks": [
                {
                    "name": "ai-memory-session-start",
                    "trigger": "SessionStart",
                    "action": {"type": "command", "command": command}
                },
                {
                    "name": "audit",
                    "trigger": "PreToolUse",
                    "action": {"type": "command", "command": "audit-tool"}
                }
            ]
        });
        let removal = strip_kiro_cli_v3_hooks(&serde_json::to_string(&shared).unwrap()).unwrap();
        assert_eq!(removal.removed_events, ["kiro-cli-v3.SessionStart"]);
        let after: serde_json::Value = serde_json::from_str(&removal.new_content).unwrap();
        assert_eq!(after["hooks"].as_array().unwrap().len(), 1);
        assert_eq!(after["hooks"][0]["name"], "audit");
    }

    /// The plan matched the flavored Kimi Code entry but apply dispatched
    /// the unflavored stripper — the CLI reported success and left the
    /// entry behind. Drive `apply_change` with the exact URL install-mcp
    /// writes so plan and apply can never diverge again.
    #[test]
    fn apply_change_removes_kimi_code_flavored_mcp_entry() {
        let tmp = tempfile::TempDir::new().unwrap();
        let path = tmp.path().join("mcp.json");
        std::fs::write(
            &path,
            r#"{"mcpServers":{"ai-memory":{"url":"http://127.0.0.1:49374/mcp?flavor=moonshot"},"other":{"url":"http://x"}}}"#,
        )
        .unwrap();

        apply_change(
            &PlannedChange::Rewrite {
                path: path.clone(),
                removed: vec!["ai-memory".to_string()],
                ops: vec![RewriteOp::McpJson(McpClient::KimiCode)],
            },
            None,
            Some("ai-memory"),
            "http://127.0.0.1:49374/mcp",
        )
        .unwrap();

        let v: serde_json::Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
        assert!(v["mcpServers"].get("ai-memory").is_none());
        assert!(v["mcpServers"].get("other").is_some());
    }
}