merlion-agent 0.1.11

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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! `merlion backup` / `merlion import` — tar.gz archive + restore of
//! `~/.merlion/`. Useful for moving merlion between machines or rolling
//! back after a misconfiguration.
//!
//! Secrets (`.env`) are excluded by default; `--include-secrets` opts in.
//! `logs/` and any stray `target/` are always skipped — neither carries
//! durable state worth backing up.
//!
//! See the wiring spec at the bottom of this file for how this hooks into
//! `main.rs`.

// The module is intentionally not yet dispatched from `main.rs` — the wiring
// is documented at the bottom of the file. Suppress dead-code lints until the
// caller is wired up so `cargo clippy -- -D warnings` stays green.
#![allow(dead_code)]

use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use clap::Args;
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use flate2::Compression;
use tar::{Archive, Builder};

/// Top-level directory name inside the tarball. The archive contains a
/// single root entry called `merlion/` so the layout is obvious when a user
/// opens the tarball by hand.
const ARCHIVE_ROOT: &str = "merlion";

#[derive(Debug, Args)]
pub struct BackupArgs {
    /// Where to write the tarball. Default: ./merlion-backup-<timestamp>.tar.gz
    #[arg(long)]
    pub output: Option<PathBuf>,
    /// Include `~/.merlion/.env` (API keys). Off by default for safety.
    #[arg(long)]
    pub include_secrets: bool,
}

#[derive(Debug, Args)]
pub struct ImportArgs {
    /// Path to a tarball previously produced by `merlion backup`.
    pub archive: PathBuf,
    /// Overwrite `~/.merlion/` if it exists. Default is to refuse.
    #[arg(long)]
    pub force: bool,
}

pub async fn run_backup(args: BackupArgs) -> Result<()> {
    let home = merlion_config::merlion_home();
    let output = args.output.clone().unwrap_or_else(|| {
        let ts = chrono::Utc::now().format("%Y%m%d-%H%M%S");
        PathBuf::from(format!("merlion-backup-{ts}.tar.gz"))
    });
    let summary = backup_to(&home, &output, args.include_secrets)?;
    println!(
        "wrote {} ({} file(s), {})",
        output.display(),
        summary.file_count,
        humanize_bytes(summary.total_bytes),
    );
    if !summary.excluded.is_empty() {
        println!("excluded:");
        for p in &summary.excluded {
            println!("  - {}", p.display());
        }
    }
    Ok(())
}

pub async fn run_import(args: ImportArgs) -> Result<()> {
    let home = merlion_config::merlion_home();
    let summary = import_to(&home, &args.archive, args.force)?;
    if let Some(backup) = &summary.moved_existing_to {
        println!("moved existing {}{}", home.display(), backup.display());
    }
    println!(
        "restored to {} ({} file(s), {})",
        home.display(),
        summary.file_count,
        humanize_bytes(summary.total_bytes),
    );
    Ok(())
}

// ---------------------------------------------------------------------------
// Core impl — pure functions parameterised by `home` so tests can drive them
// against a tempdir without env mutation.
// ---------------------------------------------------------------------------

#[derive(Debug)]
struct BackupSummary {
    file_count: u64,
    total_bytes: u64,
    excluded: Vec<PathBuf>,
}

#[derive(Debug)]
struct ImportSummary {
    file_count: u64,
    total_bytes: u64,
    moved_existing_to: Option<PathBuf>,
}

fn backup_to(home: &Path, output: &Path, include_secrets: bool) -> Result<BackupSummary> {
    if !home.exists() {
        anyhow::bail!(
            "nothing to back up — {} does not exist. Run `merlion setup` first.",
            home.display()
        );
    }

    if let Some(parent) = output.parent() {
        if !parent.as_os_str().is_empty() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("create output dir {}", parent.display()))?;
        }
    }

    let out_file =
        File::create(output).with_context(|| format!("create archive {}", output.display()))?;
    let encoder = GzEncoder::new(BufWriter::new(out_file), Compression::default());
    let mut builder = Builder::new(encoder);
    builder.follow_symlinks(false);

    let mut file_count: u64 = 0;
    let mut total_bytes: u64 = 0;
    let mut excluded: Vec<PathBuf> = Vec::new();

    walk_and_add(
        home,
        home,
        &mut builder,
        include_secrets,
        &mut file_count,
        &mut total_bytes,
        &mut excluded,
    )?;

    let encoder = builder.into_inner().context("finalise tar")?;
    encoder.finish().context("finalise gzip")?;

    Ok(BackupSummary {
        file_count,
        total_bytes,
        excluded,
    })
}

fn walk_and_add(
    home: &Path,
    dir: &Path,
    builder: &mut Builder<GzEncoder<BufWriter<File>>>,
    include_secrets: bool,
    file_count: &mut u64,
    total_bytes: &mut u64,
    excluded: &mut Vec<PathBuf>,
) -> Result<()> {
    let entries = std::fs::read_dir(dir).with_context(|| format!("read_dir {}", dir.display()))?;
    for entry in entries {
        let entry = entry.with_context(|| format!("dir entry under {}", dir.display()))?;
        let path = entry.path();
        let rel = path
            .strip_prefix(home)
            .with_context(|| format!("strip_prefix {}", path.display()))?
            .to_path_buf();

        if should_skip(&rel, include_secrets) {
            excluded.push(rel.clone());
            continue;
        }

        let file_type = entry
            .file_type()
            .with_context(|| format!("file_type {}", path.display()))?;
        let archive_path = PathBuf::from(ARCHIVE_ROOT).join(&rel);

        if file_type.is_dir() {
            walk_and_add(
                home,
                &path,
                builder,
                include_secrets,
                file_count,
                total_bytes,
                excluded,
            )?;
        } else if file_type.is_file() {
            let mut f = File::open(&path).with_context(|| format!("open {}", path.display()))?;
            let meta = f
                .metadata()
                .with_context(|| format!("metadata {}", path.display()))?;
            *file_count += 1;
            *total_bytes += meta.len();
            builder
                .append_file(&archive_path, &mut f)
                .with_context(|| format!("append {}", path.display()))?;
        }
        // Skip symlinks and other special files — they're rarely meaningful
        // inside ~/.merlion and tar would need extra care to reproduce them.
    }
    Ok(())
}

fn should_skip(rel: &Path, include_secrets: bool) -> bool {
    let first = rel.components().next().and_then(|c| c.as_os_str().to_str());
    if matches!(first, Some("logs") | Some("target")) {
        return true;
    }
    if !include_secrets && rel == Path::new(".env") {
        return true;
    }
    false
}

fn import_to(home: &Path, archive: &Path, force: bool) -> Result<ImportSummary> {
    if !archive.exists() {
        anyhow::bail!("archive not found: {}", archive.display());
    }

    let mut moved_existing_to: Option<PathBuf> = None;
    if home.exists() {
        if !force {
            anyhow::bail!(
                "{} already exists. Pass --force to overwrite (the current dir will be renamed to <home>.bak-<timestamp>).",
                home.display()
            );
        }
        let ts = chrono::Utc::now().format("%Y%m%d-%H%M%S");
        let backup = home.with_file_name(format!(
            "{}.bak-{ts}",
            home.file_name()
                .and_then(|n| n.to_str())
                .unwrap_or(".merlion")
        ));
        std::fs::rename(home, &backup)
            .with_context(|| format!("rename {}{}", home.display(), backup.display()))?;
        moved_existing_to = Some(backup);
    }

    if let Some(parent) = home.parent() {
        if !parent.as_os_str().is_empty() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("create parent {}", parent.display()))?;
        }
    }
    std::fs::create_dir_all(home).with_context(|| format!("create {}", home.display()))?;

    let input =
        File::open(archive).with_context(|| format!("open archive {}", archive.display()))?;
    let decoder = GzDecoder::new(BufReader::new(input));
    let mut tar = Archive::new(decoder);

    let mut file_count: u64 = 0;
    let mut total_bytes: u64 = 0;

    for entry in tar.entries().context("read tar entries")? {
        let mut entry = entry.context("tar entry")?;
        let path_in_archive = entry.path().context("entry path")?.into_owned();

        let rel = match path_in_archive.strip_prefix(ARCHIVE_ROOT) {
            Ok(r) => r.to_path_buf(),
            Err(_) => path_in_archive.clone(),
        };
        if rel.as_os_str().is_empty() {
            continue;
        }

        let dest = home.join(&rel);
        let header_kind = entry.header().entry_type();
        if header_kind.is_dir() {
            std::fs::create_dir_all(&dest).with_context(|| format!("mkdir {}", dest.display()))?;
            continue;
        }
        if !header_kind.is_file() {
            continue;
        }
        if let Some(parent) = dest.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("mkdir {}", parent.display()))?;
        }
        let size = entry.header().size().unwrap_or(0);
        file_count += 1;
        total_bytes += size;
        entry
            .unpack(&dest)
            .with_context(|| format!("unpack {}", dest.display()))?;
    }

    Ok(ImportSummary {
        file_count,
        total_bytes,
        moved_existing_to,
    })
}

fn humanize_bytes(n: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = 1024 * KB;
    const GB: u64 = 1024 * MB;
    if n >= GB {
        format!("{:.2} GiB", n as f64 / GB as f64)
    } else if n >= MB {
        format!("{:.2} MiB", n as f64 / MB as f64)
    } else if n >= KB {
        format!("{:.2} KiB", n as f64 / KB as f64)
    } else {
        format!("{n} B")
    }
}

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

    fn write_file(path: &Path, contents: &str) {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).unwrap();
        }
        fs::write(path, contents).unwrap();
    }

    fn fixture_home(root: &Path) -> PathBuf {
        let home = root.join("home");
        write_file(
            &home.join("config.yaml"),
            "model:\n  id: openai:gpt-4o-mini\n",
        );
        write_file(&home.join(".env"), "OPENAI_API_KEY=sk-secret\n");
        write_file(&home.join("memory").join("a.md"), "hello memory\n");
        write_file(
            &home.join("skills").join("greet").join("SKILL.md"),
            "# greet\n",
        );
        write_file(&home.join("logs").join("agent.log"), "noise\n");
        write_file(&home.join("target").join("debug").join("junk"), "junk\n");
        home
    }

    #[test]
    fn roundtrip_preserves_file_contents() {
        let tmp = tempfile::tempdir().unwrap();
        let home = fixture_home(tmp.path());
        let archive = tmp.path().join("backup.tar.gz");

        let summary = backup_to(&home, &archive, /*include_secrets=*/ true).unwrap();
        assert!(archive.exists());
        // config.yaml, .env, memory/a.md, skills/greet/SKILL.md = 4 files.
        // logs/* and target/* are unconditionally skipped.
        assert_eq!(
            summary.file_count, 4,
            "files in archive: {}",
            summary.file_count
        );

        let restored = tmp.path().join("restored");
        let restore_summary = import_to(&restored, &archive, /*force=*/ false).unwrap();
        assert_eq!(restore_summary.file_count, 4);

        assert_eq!(
            fs::read_to_string(restored.join("config.yaml")).unwrap(),
            "model:\n  id: openai:gpt-4o-mini\n",
        );
        assert_eq!(
            fs::read_to_string(restored.join(".env")).unwrap(),
            "OPENAI_API_KEY=sk-secret\n",
        );
        assert_eq!(
            fs::read_to_string(restored.join("memory").join("a.md")).unwrap(),
            "hello memory\n",
        );
        assert_eq!(
            fs::read_to_string(restored.join("skills").join("greet").join("SKILL.md")).unwrap(),
            "# greet\n",
        );
        // logs and target must not have come along for the ride.
        assert!(!restored.join("logs").exists());
        assert!(!restored.join("target").exists());
    }

    #[test]
    fn secrets_excluded_by_default() {
        let tmp = tempfile::tempdir().unwrap();
        let home = fixture_home(tmp.path());
        let archive = tmp.path().join("backup.tar.gz");

        let summary = backup_to(&home, &archive, /*include_secrets=*/ false).unwrap();
        assert!(
            summary.excluded.iter().any(|p| p == Path::new(".env")),
            "expected .env in excluded list, got: {:?}",
            summary.excluded
        );

        let restored = tmp.path().join("restored");
        import_to(&restored, &archive, /*force=*/ false).unwrap();
        assert!(
            !restored.join(".env").exists(),
            "default backup must not contain .env"
        );
        assert!(restored.join("config.yaml").exists());
    }

    #[test]
    fn import_refuses_to_overwrite_without_force() {
        let tmp = tempfile::tempdir().unwrap();
        let home = fixture_home(tmp.path());
        let archive = tmp.path().join("backup.tar.gz");
        backup_to(&home, &archive, true).unwrap();

        // Pre-existing dir at the destination.
        let dest = tmp.path().join("dest");
        fs::create_dir_all(&dest).unwrap();
        fs::write(dest.join("preexisting.txt"), "keep me").unwrap();

        let err = import_to(&dest, &archive, /*force=*/ false).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("already exists"),
            "expected refusal message, got: {msg}"
        );
        // Original content must survive a refused import.
        assert_eq!(
            fs::read_to_string(dest.join("preexisting.txt")).unwrap(),
            "keep me"
        );
    }

    #[test]
    fn import_with_force_moves_existing_aside() {
        let tmp = tempfile::tempdir().unwrap();
        let home = fixture_home(tmp.path());
        let archive = tmp.path().join("backup.tar.gz");
        backup_to(&home, &archive, true).unwrap();

        let dest = tmp.path().join("dest");
        fs::create_dir_all(&dest).unwrap();
        fs::write(dest.join("preexisting.txt"), "keep me").unwrap();

        let summary = import_to(&dest, &archive, /*force=*/ true).unwrap();
        let backup = summary.moved_existing_to.expect("expected a side-rename");
        assert!(backup.exists(), "renamed-aside dir should exist");
        assert_eq!(
            fs::read_to_string(backup.join("preexisting.txt")).unwrap(),
            "keep me",
        );
        // Fresh contents from the archive landed in `dest`.
        assert!(dest.join("config.yaml").exists());
        assert!(!dest.join("preexisting.txt").exists());
    }
}

// -----------------------------------------------------------------------------
// Wiring spec — apply to `crates/merlion-cli/src/main.rs`.
//
// 1. Add a module declaration near the other `mod` lines at the top:
//
//        mod backup_cmd;
//
// 2. Add two new variants to the `Command` enum. Both use `clap::Args` so
//    they get the flag-style invocation (`merlion backup --output …`) without
//    a nested subcommand:
//
//        /// Archive ~/.merlion/ to a tar.gz for moving or rollback.
//        Backup(backup_cmd::BackupArgs),
//        /// Restore ~/.merlion/ from a `merlion backup` tarball.
//        Import(backup_cmd::ImportArgs),
//
// 3. Add dispatch arms in the `match cli.command.unwrap_or(...)` block in
//    `main`:
//
//        Command::Backup(args) => backup_cmd::run_backup(args).await,
//        Command::Import(args) => backup_cmd::run_import(args).await,
//
// -----------------------------------------------------------------------------