merlion-agent 0.1.14

Merlion Agent CLI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! `merlion uninstall` — remove Merlion Agent from the system.
//!
//! Stops + uninstalls the launchd / systemd gateway daemon, deletes
//! `~/.merlion/`, and (best-effort) tells the user how to remove the
//! installed binary. Three escape hatches:
//!
//! - `--yes` — skip the interactive confirmation prompt.
//! - `--keep-data` — leave `~/.merlion/` in place (useful before a
//!   reinstall when you want to preserve config + sessions).
//! - `--keep-binary` — don't print binary-removal instructions.
//!
//! The running binary deliberately does *not* try to delete itself: a
//! process unlinking its own executable is a footgun (works on Unix, fails
//! oddly elsewhere, and can race with package managers). Instead we
//! classify the path and print the appropriate `brew uninstall` /
//! `cargo uninstall` / "delete manually" line for the user to run.

use std::io::IsTerminal;
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Context, Result};
use clap::Args;
use dialoguer::console::style;
use dialoguer::{theme::ColorfulTheme, Confirm};

use crate::gateway_service;

/// `merlion uninstall` arguments. Three independent "keep X" flags let the
/// user opt out of any subset of the uninstall steps; `--yes` skips the
/// final confirmation prompt for scripted use.
#[derive(Debug, Args)]
pub struct UninstallArgs {
    /// Skip the "are you sure?" prompt. Required when stdin isn't a TTY.
    #[arg(long)]
    pub yes: bool,
    /// Don't delete `~/.merlion/`. Useful when you plan to reinstall and
    /// want to preserve config, sessions, memories, and skills.
    #[arg(long)]
    pub keep_data: bool,
    /// Don't print instructions for removing the installed binary.
    #[arg(long)]
    pub keep_binary: bool,
}

/// How the running binary was installed — drives which removal command we
/// suggest. Extracted as an enum (rather than just a string) so the pure
/// classification logic can be unit-tested without touching the
/// filesystem or shelling out.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BinaryRemoval {
    /// Installed via `cargo install` into `~/.cargo/bin/`.
    Cargo,
    /// Installed via Homebrew under `/opt/homebrew` (Apple Silicon) or
    /// `/usr/local` (Intel).
    Brew,
    /// Anywhere else — we can't infer the package manager, so the user
    /// has to delete it themselves.
    Manual,
}

/// Classify `exe_path` into one of the [`BinaryRemoval`] strategies.
///
/// `cargo_bin_dir` is the user's `~/.cargo/bin` (passed in rather than
/// resolved internally so tests can pin it to a fixture). The classification
/// is purely prefix-based — we don't stat anything.
pub fn classify_binary_path(exe_path: &Path, cargo_bin_dir: Option<&Path>) -> BinaryRemoval {
    if let Some(cargo_bin) = cargo_bin_dir {
        if exe_path.starts_with(cargo_bin) {
            return BinaryRemoval::Cargo;
        }
    }
    if exe_path.starts_with("/opt/homebrew/") || exe_path.starts_with("/usr/local/") {
        return BinaryRemoval::Brew;
    }
    BinaryRemoval::Manual
}

/// Run the uninstall flow. See [`UninstallArgs`] for option semantics.
pub async fn run(args: UninstallArgs) -> Result<()> {
    let home = merlion_config::merlion_home();
    let service_state = gateway_service::service_status().ok();
    let service_installed = matches!(
        service_state.as_ref(),
        Some(gateway_service::ServiceState::Stopped { .. })
            | Some(gateway_service::ServiceState::Running { .. })
            | Some(gateway_service::ServiceState::Unknown { .. })
    );

    print_plan(&home, service_installed, &args);

    if !args.yes {
        if !std::io::stdin().is_terminal() {
            return Err(anyhow!(
                "`merlion uninstall` needs a TTY for confirmation.\n\
                 Re-run with --yes to skip the prompt in non-interactive contexts."
            ));
        }
        let theme = ColorfulTheme::default();
        let ok = Confirm::with_theme(&theme)
            .with_prompt("Proceed with uninstall?")
            .default(false)
            .interact()
            .context("reading confirmation")?;
        if !ok {
            println!();
            println!("{}", style("Aborted — nothing was changed.").dim());
            return Ok(());
        }
    }

    println!();
    section_header("Stopping gateway service");
    if service_installed {
        match gateway_service::uninstall() {
            Ok(()) => {}
            Err(e) => {
                tracing::warn!(error = %e, "gateway uninstall failed; continuing");
                println!(
                    "  {} {}",
                    style("!").yellow().bold(),
                    style(format!("gateway uninstall failed: {e}")).dim()
                );
            }
        }
    } else {
        println!("  {}", style("(no gateway service installed)").dim());
    }

    println!();
    section_header("Removing data directory");
    if args.keep_data {
        println!(
            "  {} {}",
            style("·").dim(),
            style(format!(
                "--keep-data set; leaving {} in place",
                home.display()
            ))
            .dim()
        );
    } else {
        remove_home(&home)?;
    }

    println!();
    section_header("Removing binary");
    if args.keep_binary {
        println!(
            "  {} {}",
            style("·").dim(),
            style("--keep-binary set; leaving the installed binary alone").dim()
        );
    } else {
        report_binary_removal()?;
    }

    println!();
    println!("{}", style("Uninstall complete.").green().bold());
    println!(
        "  {} {}",
        style("Reinstall with:").dim(),
        style("brew install MerlionOS/merlion/merlion-agent").bold()
    );
    Ok(())
}

fn print_plan(home: &Path, service_installed: bool, args: &UninstallArgs) {
    println!();
    section_header("Merlion uninstall plan");
    println!(
        "  {}",
        style("The following actions will be performed:").dim()
    );
    println!();

    if service_installed {
        println!(
            "  {} stop and unregister the gateway daemon (launchd / systemd)",
            style("").cyan()
        );
    } else {
        println!(
            "  {} {}",
            style("·").dim(),
            style("no gateway daemon installed — nothing to stop").dim()
        );
    }

    if args.keep_data {
        println!(
            "  {} {}",
            style("·").dim(),
            style(format!("--keep-data: leave {} in place", home.display())).dim()
        );
    } else {
        println!(
            "  {} delete {} (config, sessions, memories, skills)",
            style("").cyan(),
            style(home.display().to_string()).bold()
        );
    }

    if args.keep_binary {
        println!(
            "  {} {}",
            style("·").dim(),
            style("--keep-binary: skip binary removal instructions").dim()
        );
    } else {
        println!(
            "  {} print instructions for removing the installed `merlion` binary",
            style("").cyan()
        );
    }
    println!();
}

fn section_header(title: &str) {
    println!(
        "{} {}",
        style("").cyan().bold(),
        style(title).cyan().bold()
    );
}

fn remove_home(home: &Path) -> Result<()> {
    if !home.exists() {
        println!("  {}", style("(no ~/.merlion to remove)").dim());
        return Ok(());
    }
    std::fs::remove_dir_all(home).with_context(|| format!("removing {}", home.display()))?;
    println!(
        "  {} {} {}",
        style("").green().bold(),
        style("removed").dim(),
        style(home.display().to_string()).bold()
    );
    Ok(())
}

fn report_binary_removal() -> Result<()> {
    let exe = match std::env::current_exe() {
        Ok(p) => p,
        Err(e) => {
            tracing::warn!(error = %e, "failed to resolve current_exe");
            println!(
                "  {} {}",
                style("!").yellow().bold(),
                style(format!("could not resolve current executable: {e}")).dim()
            );
            return Ok(());
        }
    };
    let cargo_bin = cargo_bin_dir();
    let strategy = classify_binary_path(&exe, cargo_bin.as_deref());
    match strategy {
        BinaryRemoval::Cargo => {
            println!(
                "  {} the running binary is at {}",
                style("·").dim(),
                style(exe.display().to_string()).bold()
            );
            println!(
                "  {} {}",
                style("→ run:").dim(),
                style("cargo uninstall merlion-agent").bold()
            );
        }
        BinaryRemoval::Brew => {
            println!(
                "  {} the running binary is at {}",
                style("·").dim(),
                style(exe.display().to_string()).bold()
            );
            println!(
                "  {} {}",
                style("→ run:").dim(),
                style("brew uninstall merlion-agent").bold()
            );
        }
        BinaryRemoval::Manual => {
            println!(
                "  {} the running binary is at {}",
                style("·").dim(),
                style(exe.display().to_string()).bold()
            );
            println!("  {} {}", style("").dim(), style("delete manually").bold());
        }
    }
    Ok(())
}

/// Best-effort lookup for `~/.cargo/bin`. Respects `CARGO_HOME` when set,
/// falls back to `$HOME/.cargo/bin`. Returns `None` when we can't even
/// figure out a home directory — in that case `classify_binary_path` skips
/// the cargo check entirely.
fn cargo_bin_dir() -> Option<PathBuf> {
    if let Ok(cargo_home) = std::env::var("CARGO_HOME") {
        if !cargo_home.is_empty() {
            return Some(PathBuf::from(cargo_home).join("bin"));
        }
    }
    dirs::home_dir().map(|h| h.join(".cargo").join("bin"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn classify_recognises_cargo_bin() {
        let cargo_bin = PathBuf::from("/home/alice/.cargo/bin");
        let exe = PathBuf::from("/home/alice/.cargo/bin/merlion");
        assert_eq!(
            classify_binary_path(&exe, Some(&cargo_bin)),
            BinaryRemoval::Cargo
        );
    }

    #[test]
    fn classify_recognises_apple_silicon_brew() {
        let exe = PathBuf::from("/opt/homebrew/bin/merlion");
        assert_eq!(classify_binary_path(&exe, None), BinaryRemoval::Brew);
    }

    #[test]
    fn classify_recognises_intel_brew() {
        let exe = PathBuf::from("/usr/local/bin/merlion");
        assert_eq!(classify_binary_path(&exe, None), BinaryRemoval::Brew);
    }

    #[test]
    fn classify_falls_back_to_manual() {
        let exe = PathBuf::from("/somewhere/exotic/merlion");
        assert_eq!(classify_binary_path(&exe, None), BinaryRemoval::Manual);
    }

    #[test]
    fn classify_prefers_cargo_over_brew_when_both_match() {
        // Pathological setup: cargo_bin lives under /usr/local. The cargo
        // check runs first so we should still suggest `cargo uninstall`.
        let cargo_bin = PathBuf::from("/usr/local/cargo/bin");
        let exe = PathBuf::from("/usr/local/cargo/bin/merlion");
        assert_eq!(
            classify_binary_path(&exe, Some(&cargo_bin)),
            BinaryRemoval::Cargo
        );
    }

    #[test]
    fn classify_ignores_unrelated_cargo_bin() {
        let cargo_bin = PathBuf::from("/home/alice/.cargo/bin");
        let exe = PathBuf::from("/opt/homebrew/bin/merlion");
        assert_eq!(
            classify_binary_path(&exe, Some(&cargo_bin)),
            BinaryRemoval::Brew
        );
    }

    #[test]
    fn classify_target_debug_is_manual() {
        // Running from `cargo run` inside the workspace — the dev binary
        // lives under `target/debug/`. No package manager owns it.
        let cargo_bin = PathBuf::from("/home/alice/.cargo/bin");
        let exe = PathBuf::from("/home/alice/code/merlion-agent/target/debug/merlion");
        assert_eq!(
            classify_binary_path(&exe, Some(&cargo_bin)),
            BinaryRemoval::Manual
        );
    }
}

// -----------------------------------------------------------------------------
// WIRING SPEC — apply to `crates/merlion-cli/src/main.rs`.
//
// 1. Add a module declaration near the other `mod` lines at the top of main.rs:
//
//        mod uninstall_cmd;
//
// 2. Add a new variant to the `Command` enum:
//
//        /// Remove Merlion Agent from the system.
//        ///
//        /// Stops and unregisters the gateway daemon (if installed), deletes
//        /// `~/.merlion/`, and prints the appropriate command to remove the
//        /// installed binary (`brew uninstall` / `cargo uninstall` /
//        /// "delete manually") based on where the running binary lives.
//        ///
//        /// Use `--keep-data` to preserve config + sessions for a later
//        /// reinstall, `--keep-binary` to skip binary-removal instructions,
//        /// and `--yes` to skip the interactive confirmation prompt.
//        Uninstall(uninstall_cmd::UninstallArgs),
//
// 3. Add a dispatch arm in the `match cli.command.unwrap_or(...)` block in
//    `main()`:
//
//        Command::Uninstall(args) => uninstall_cmd::run(args).await,
//
// 4. No new `use` lines are required at the top of `main.rs` — the args
//    struct is referenced through the `uninstall_cmd::` module path, and
//    `run` returns `anyhow::Result<()>` like the other async dispatch arms.
// -----------------------------------------------------------------------------