Skip to main content

trusty_memory/commands/
migrate.rs

1//! Handler for `trusty-memory migrate kuzu-memory`.
2//!
3//! Why: users currently running the kuzu-memory MCP server need a one-command
4//! switch to trusty-memory that rewrites every Claude settings file referring
5//! to the legacy server, with the same idempotency / atomic-write semantics as
6//! `trusty-search migrate mcp-vector-search`. Doing this by hand across global
7//! and per-project settings files is error-prone, so the binary owns it.
8//! What: `handle_migrate` walks every discovered Claude settings file via
9//! `trusty_common::claude_config`, swaps any `kuzu-memory` / `kuzu_memory`
10//! `mcpServers` entry for a canonical `trusty-memory` entry, and prints a
11//! summary table. `--dry-run` prints the plan without writing. `--config-only`
12//! is accepted for parity with `trusty-search migrate` (today the migration
13//! has no other phase so it is effectively a no-op flag).
14//! Test: unit tests cover (a) a vanilla rewrite preserving unrelated keys,
15//! (b) idempotency when a `trusty-memory` entry is already present, and
16//! (c) the no-op case when no legacy key is present.
17//!
18//! Run manually with: `cargo run -p trusty-memory -- migrate kuzu-memory --dry-run`.
19
20use anyhow::Result;
21use clap::ValueEnum;
22use colored::Colorize;
23use serde_json::Value;
24use std::path::{Path, PathBuf};
25use trusty_common::claude_config::{
26    default_settings_max_depth, discover_claude_settings, mcp_server_entry, write_json_atomic,
27};
28
29/// The MCP server keys (both spellings) we replace with `trusty-memory`.
30///
31/// Why: we have seen both `kuzu-memory` and `kuzu_memory` in the wild in
32/// `mcpServers` blocks; treat them as equivalent legacy aliases.
33/// What: a static slice scanned for membership inside each settings file.
34/// Test: `test_migrate_config_replaces_dashed_key` and
35/// `test_migrate_config_replaces_underscored_key` cover both forms.
36const LEGACY_MCP_KEYS: &[&str] = &["kuzu-memory", "kuzu_memory"];
37
38/// The canonical key written for the migrated trusty-memory MCP server.
39const TRUSTY_KEY: &str = "trusty-memory";
40
41/// What the user is migrating *from*.
42///
43/// Why: model the migration source as an enum (validated at parse time by
44/// clap) so additional sources can be added later without changing the
45/// CLI surface.
46/// What: two variants today — `kuzu-memory` (config migration) and
47/// `kuzu-data` (data migration from a kuzu-memory `store.redb`).
48/// Test: `cargo run -p trusty-memory -- migrate bogus` → clap rejects with
49/// a usage hint.
50#[derive(Debug, Clone, ValueEnum)]
51pub enum MigrateTarget {
52    /// Migrate from kuzu-memory (rewrites Claude `mcpServers` entries).
53    KuzuMemory,
54    /// Import entity/relation data from a kuzu-memory `store.redb` file into
55    /// a trusty-memory palace.
56    #[value(name = "kuzu-data")]
57    KuzuData,
58}
59
60/// Outcome of attempting to migrate one Claude settings file.
61///
62/// Why: the summary table distinguishes a real rewrite from an
63/// already-migrated no-op, a "no relevant key" skip, and a hard failure.
64/// What: enumerates the four terminal states of `migrate_config_file`.
65/// Test: unit tests assert `Migrated`, `AlreadyMigrated`, and `Skipped`.
66#[derive(Debug, PartialEq, Eq)]
67pub enum ConfigMigrateStatus {
68    /// The file contained a legacy key and was rewritten.
69    Migrated,
70    /// The file already contained a `trusty-memory` key — left untouched.
71    AlreadyMigrated,
72    /// No legacy key (and no `trusty-memory` key) — nothing to do.
73    Skipped,
74    /// An I/O or parse error occurred.
75    Failed(String),
76}
77
78/// Result of migrating one Claude settings file (path + terminal status).
79///
80/// Why: pairs the file path with its outcome so the summary renderer can
81/// print one line per file.
82/// What: returned by `migrate_config_file`.
83/// Test: unit tests inspect `status` after rewriting fixture files.
84#[derive(Debug)]
85pub struct ConfigMigrateResult {
86    pub path: PathBuf,
87    pub status: ConfigMigrateStatus,
88}
89
90/// Entry point for `trusty-memory migrate`.
91///
92/// Why: a single command that switches a machine from kuzu-memory to
93/// trusty-memory. `kuzu-memory` rewrites every Claude MCP settings file.
94/// `kuzu-data` imports entity/relation data from a kuzu-memory `store.redb`
95/// into a target palace.
96/// What: dispatches to the appropriate handler based on the `target` variant.
97/// The `_config_only` flag is accepted for CLI parity with
98/// `trusty-search migrate` but only applies to `kuzu-memory` (the config
99/// migration).
100/// Test: `migrate kuzu-memory --dry-run` enumerates without writing.
101pub fn handle_migrate(
102    target: MigrateTarget,
103    dry_run: bool,
104    _config_only: bool,
105    kuzu_from: Option<std::path::PathBuf>,
106    kuzu_palace: Option<String>,
107    kuzu_limit: Option<usize>,
108) -> Result<()> {
109    match target {
110        MigrateTarget::KuzuMemory => {
111            if dry_run {
112                println!("{} Dry run — no files will be modified.\n", "·".dimmed());
113            }
114            run_config_phase(dry_run)
115        }
116        MigrateTarget::KuzuData => {
117            let from = kuzu_from
118                .ok_or_else(|| anyhow::anyhow!("migrate kuzu-data requires --from <store.redb>"))?;
119            let palace = kuzu_palace
120                .ok_or_else(|| anyhow::anyhow!("migrate kuzu-data requires --palace <name>"))?;
121            crate::commands::kuzu_migrate::handle_kuzu_data_migrate(
122                &from, &palace, dry_run, kuzu_limit,
123            )
124        }
125    }
126}
127
128/// Scan + rewrite every Claude settings file.
129///
130/// Why: keeps the orchestration (scan → migrate → summarize) separate from
131/// the per-file surgery in `migrate_config_file`.
132/// What: locates settings files, migrates each, prints a summary table.
133/// Test: covered indirectly by `--dry-run` runs and the unit tests below.
134fn run_config_phase(dry_run: bool) -> Result<()> {
135    let home =
136        dirs::home_dir().ok_or_else(|| anyhow::anyhow!("could not determine home directory"))?;
137    println!(
138        "🔍 Scanning for Claude MCP settings under {}…",
139        home.display()
140    );
141
142    let files = discover_claude_settings(&home, default_settings_max_depth());
143    if files.is_empty() {
144        println!("{} No Claude settings files found.", "·".dimmed());
145        return Ok(());
146    }
147    println!("{} Found {} settings file(s).\n", "·".dimmed(), files.len());
148
149    let mut migrated = 0usize;
150    let mut already = 0usize;
151    let mut skipped = 0usize;
152    let mut failed = 0usize;
153
154    for (i, path) in files.iter().enumerate() {
155        let result = migrate_config_file(path, dry_run);
156        print_config_line(i + 1, files.len(), &result);
157        match result.status {
158            ConfigMigrateStatus::Migrated => migrated += 1,
159            ConfigMigrateStatus::AlreadyMigrated => already += 1,
160            ConfigMigrateStatus::Skipped => skipped += 1,
161            ConfigMigrateStatus::Failed(_) => failed += 1,
162        }
163    }
164
165    println!();
166    if dry_run {
167        println!(
168            "{} MCP config dry run: {} would migrate, {} already migrated, {} skipped, {} failed",
169            "·".dimmed(),
170            migrated,
171            already,
172            skipped,
173            failed
174        );
175    } else {
176        println!(
177            "{} MCP config: {} migrated, {} already migrated, {} skipped, {} failed",
178            "✓".green(),
179            migrated,
180            already,
181            skipped,
182            failed
183        );
184    }
185    Ok(())
186}
187
188/// Rewrite one Claude settings file, replacing any legacy kuzu-memory MCP
189/// server entry with a `trusty-memory` entry.
190///
191/// Why: this is the load-bearing surgery — it must preserve every unrelated
192/// JSON key, be idempotent across repeated runs, and never corrupt the file
193/// on failure (atomic write + `.bak` backup, courtesy of
194/// `trusty_common::claude_config::write_json_atomic`).
195/// What: parses the file as `serde_json::Value`, swaps the key inside
196/// `mcpServers`, then atomically rewrites the file.
197/// Test: `test_migrate_config_replaces_dashed_key`,
198/// `test_migrate_config_replaces_underscored_key`, and
199/// `test_migrate_config_idempotent` cover the rewrite, the alternate
200/// spelling, and the no-op-on-already-migrated paths.
201pub fn migrate_config_file(path: &Path, dry_run: bool) -> ConfigMigrateResult {
202    let fail = |msg: String| ConfigMigrateResult {
203        path: path.to_path_buf(),
204        status: ConfigMigrateStatus::Failed(msg),
205    };
206
207    let content = match std::fs::read_to_string(path) {
208        Ok(c) => c,
209        Err(e) => return fail(format!("read: {e}")),
210    };
211    let mut root: Value = match serde_json::from_str(&content) {
212        Ok(v) => v,
213        Err(e) => return fail(format!("parse: {e}")),
214    };
215
216    let servers = match root.get_mut("mcpServers").and_then(Value::as_object_mut) {
217        Some(s) => s,
218        // No mcpServers block at all — nothing to migrate.
219        None => {
220            return ConfigMigrateResult {
221                path: path.to_path_buf(),
222                status: ConfigMigrateStatus::Skipped,
223            }
224        }
225    };
226
227    // Idempotency: a trusty-memory entry already present means a previous run
228    // (or the user) already migrated this file — never double-migrate.
229    if servers.contains_key(TRUSTY_KEY) {
230        return ConfigMigrateResult {
231            path: path.to_path_buf(),
232            status: ConfigMigrateStatus::AlreadyMigrated,
233        };
234    }
235
236    let legacy_present = LEGACY_MCP_KEYS.iter().any(|k| servers.contains_key(*k));
237    if !legacy_present {
238        return ConfigMigrateResult {
239            path: path.to_path_buf(),
240            status: ConfigMigrateStatus::Skipped,
241        };
242    }
243
244    // Drop every legacy key and insert the canonical trusty-memory entry.
245    for k in LEGACY_MCP_KEYS {
246        servers.remove(*k);
247    }
248    servers.insert(
249        TRUSTY_KEY.to_string(),
250        mcp_server_entry(TRUSTY_KEY, &["serve"]),
251    );
252
253    if dry_run {
254        return ConfigMigrateResult {
255            path: path.to_path_buf(),
256            status: ConfigMigrateStatus::Migrated,
257        };
258    }
259
260    match write_json_atomic(path, &root) {
261        Ok(()) => ConfigMigrateResult {
262            path: path.to_path_buf(),
263            status: ConfigMigrateStatus::Migrated,
264        },
265        Err(e) => fail(format!("write: {e}")),
266    }
267}
268
269/// Render one config-migration result line for the summary table.
270///
271/// Why: keeps colorised, aligned output away from the orchestration logic.
272/// What: one line per file with a status glyph.
273/// Test: not unit-tested (pure formatting); covered by manual smoke runs.
274fn print_config_line(idx: usize, total: usize, r: &ConfigMigrateResult) {
275    let prefix = format!("[{idx}/{total}]");
276    let path = r.path.display().to_string();
277    match &r.status {
278        ConfigMigrateStatus::Migrated => println!("  {} {} {}", prefix.dimmed(), "✓".green(), path),
279        ConfigMigrateStatus::AlreadyMigrated => println!(
280            "  {} {} {} {}",
281            prefix.dimmed(),
282            "↻".cyan(),
283            path.dimmed(),
284            "(already migrated)".dimmed()
285        ),
286        ConfigMigrateStatus::Skipped => println!(
287            "  {} {} {} {}",
288            prefix.dimmed(),
289            "·".dimmed(),
290            path.dimmed(),
291            "(no kuzu-memory entry)".dimmed()
292        ),
293        ConfigMigrateStatus::Failed(msg) => println!(
294            "  {} {} {} {}",
295            prefix.dimmed(),
296            "✗".red(),
297            path.dimmed(),
298            format!("({msg})").red()
299        ),
300    }
301}
302
303#[cfg(test)]
304mod tests {
305    use super::*;
306
307    /// Why: the core surgery — a `kuzu-memory` key must be removed and the
308    /// canonical trusty-memory key inserted, while unrelated keys survive.
309    #[test]
310    fn test_migrate_config_replaces_dashed_key() {
311        let tmp = tempfile::tempdir().expect("tempdir");
312        let path = tmp.path().join("settings.local.json");
313        let input = serde_json::json!({
314            "theme": "dark",
315            "mcpServers": {
316                "kuzu-memory": {
317                    "command": "kuzu-memory",
318                    "args": ["serve"]
319                },
320                "other-server": { "command": "other" }
321            }
322        });
323        std::fs::write(&path, serde_json::to_string_pretty(&input).unwrap()).expect("write input");
324
325        let result = migrate_config_file(&path, false);
326        assert_eq!(result.status, ConfigMigrateStatus::Migrated);
327
328        let rewritten: Value =
329            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
330        let servers = rewritten["mcpServers"].as_object().unwrap();
331        assert!(
332            !servers.contains_key("kuzu-memory"),
333            "legacy key should be gone"
334        );
335        assert!(servers.contains_key("trusty-memory"), "trusty key missing");
336        assert!(
337            servers.contains_key("other-server"),
338            "unrelated server dropped"
339        );
340        assert_eq!(
341            rewritten["theme"], "dark",
342            "unrelated top-level key dropped"
343        );
344        assert_eq!(servers["trusty-memory"]["command"], "trusty-memory");
345        assert_eq!(servers["trusty-memory"]["args"][0], "serve");
346
347        // Backup preserves multi-dot filename: settings.local.json.bak
348        assert!(
349            path.with_file_name("settings.local.json.bak").exists(),
350            "backup file missing"
351        );
352    }
353
354    /// Why: the alternate `kuzu_memory` spelling must be treated identically.
355    #[test]
356    fn test_migrate_config_replaces_underscored_key() {
357        let tmp = tempfile::tempdir().expect("tempdir");
358        let path = tmp.path().join("settings.json");
359        let input = serde_json::json!({
360            "mcpServers": {
361                "kuzu_memory": { "command": "kuzu-memory", "args": ["serve"] }
362            }
363        });
364        std::fs::write(&path, serde_json::to_string_pretty(&input).unwrap()).expect("write input");
365
366        let result = migrate_config_file(&path, false);
367        assert_eq!(result.status, ConfigMigrateStatus::Migrated);
368
369        let rewritten: Value =
370            serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
371        let servers = rewritten["mcpServers"].as_object().unwrap();
372        assert!(!servers.contains_key("kuzu_memory"));
373        assert!(servers.contains_key("trusty-memory"));
374    }
375
376    /// Why: a file already carrying a `trusty-memory` entry must be left
377    /// untouched so repeated `migrate` runs are safe.
378    #[test]
379    fn test_migrate_config_idempotent() {
380        let tmp = tempfile::tempdir().expect("tempdir");
381        let path = tmp.path().join("settings.json");
382        let input = serde_json::json!({
383            "mcpServers": {
384                "trusty-memory": {
385                    "command": "trusty-memory",
386                    "args": ["serve"]
387                }
388            }
389        });
390        let serialized = serde_json::to_string_pretty(&input).unwrap();
391        std::fs::write(&path, &serialized).expect("write input");
392
393        let result = migrate_config_file(&path, false);
394        assert_eq!(result.status, ConfigMigrateStatus::AlreadyMigrated);
395
396        // File must be byte-for-byte unchanged.
397        assert_eq!(
398            std::fs::read_to_string(&path).unwrap(),
399            serialized,
400            "file should be untouched"
401        );
402        assert!(
403            !path.with_file_name("settings.json.bak").exists(),
404            "no backup should be written for a skipped file"
405        );
406    }
407
408    /// Why: a settings file with no `kuzu-memory` entry (and no `trusty-memory`
409    /// entry) is reported as `Skipped`, not `Migrated`, and not modified.
410    #[test]
411    fn test_migrate_config_skips_when_no_legacy_key() {
412        let tmp = tempfile::tempdir().expect("tempdir");
413        let path = tmp.path().join("settings.json");
414        let input = serde_json::json!({
415            "mcpServers": {
416                "some-other-server": { "command": "x" }
417            }
418        });
419        let serialized = serde_json::to_string_pretty(&input).unwrap();
420        std::fs::write(&path, &serialized).expect("write input");
421
422        let result = migrate_config_file(&path, false);
423        assert_eq!(result.status, ConfigMigrateStatus::Skipped);
424        assert_eq!(
425            std::fs::read_to_string(&path).unwrap(),
426            serialized,
427            "file must be untouched"
428        );
429    }
430
431    /// Why: `--dry-run` must report what *would* change without writing the
432    /// file to disk or producing a backup.
433    #[test]
434    fn test_migrate_config_dry_run_does_not_write() {
435        let tmp = tempfile::tempdir().expect("tempdir");
436        let path = tmp.path().join("settings.json");
437        let input = serde_json::json!({
438            "mcpServers": {
439                "kuzu-memory": { "command": "kuzu-memory", "args": ["serve"] }
440            }
441        });
442        let serialized = serde_json::to_string_pretty(&input).unwrap();
443        std::fs::write(&path, &serialized).expect("write input");
444
445        let result = migrate_config_file(&path, true);
446        assert_eq!(result.status, ConfigMigrateStatus::Migrated);
447
448        // File on disk must be byte-for-byte unchanged.
449        assert_eq!(
450            std::fs::read_to_string(&path).unwrap(),
451            serialized,
452            "dry run must not write the file"
453        );
454        assert!(
455            !path.with_file_name("settings.json.bak").exists(),
456            "dry run must not produce a backup"
457        );
458    }
459}