mind-cli 0.9.0

A manager for agent tooling (skills, agents, rules, tools) that melds arbitrary git repos and links items into your agent directories.
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
//! Integration tests for the recall/probe outdated marker's hash-error
//! handling (CLI-75) and the `--source` / `unmeld` glob-selector validation
//! (CLI-28, CLI-86).
//!
//! Each test drives the real `mind` binary against a hermetic fixture source
//! (a local git repo, no network), using isolated MIND_HOME / CLAUDE_HOME temp
//! dirs, exactly as tests/cli.rs does. This file is its own crate, so it carries
//! a minimal copy of that harness.

use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicU32, Ordering};

static COUNTER: AtomicU32 = AtomicU32::new(0);

// ---------------------------------------------------------------------------
// Minimal fixture harness (mirrors tests/cli.rs)
// ---------------------------------------------------------------------------

struct Sandbox {
    base: PathBuf,
    source: PathBuf,
    mind_home: PathBuf,
    claude_home: PathBuf,
}

struct Run {
    stdout: String,
    stderr: String,
    success: bool,
}

impl Sandbox {
    fn new(name: &str) -> Self {
        let n = COUNTER.fetch_add(1, Ordering::SeqCst);
        let base = std::env::temp_dir().join(format!("mind-rhash-{}-{n}", std::process::id()));
        let _ = std::fs::remove_dir_all(&base);
        let source = base.join(name);
        Sandbox {
            base: base.clone(),
            source,
            mind_home: base.join("mind"),
            claude_home: base.join("claude"),
        }
    }

    fn mind(&self, args: &[&str]) -> Run {
        let out = Command::new(env!("CARGO_BIN_EXE_mind"))
            .args(args)
            .env("MIND_HOME", &self.mind_home)
            .env("CLAUDE_HOME", &self.claude_home)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .stdin(Stdio::null())
            .output()
            .expect("run mind");
        Run {
            stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
            stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
            success: out.status.success(),
        }
    }

    fn source_spec(&self) -> String {
        self.source.to_string_lossy().into_owned()
    }

    /// Git-init the source repo so `meld` can clone it.
    fn git_init(&self) {
        git(
            &self.source,
            &["-c", "init.defaultBranch=main", "init", "-q"],
        );
        git(&self.source, &["config", "user.email", "t@t"]);
        git(&self.source, &["config", "user.name", "t"]);
        git(&self.source, &["add", "-A"]);
        git(&self.source, &["commit", "-qm", "initial"]);
    }
}

impl Drop for Sandbox {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.base);
    }
}

fn write(path: &Path, contents: &str) {
    std::fs::create_dir_all(path.parent().unwrap()).unwrap();
    std::fs::write(path, contents).unwrap();
}

fn git(dir: &Path, args: &[&str]) {
    let status = Command::new("git")
        .args(args)
        .current_dir(dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .expect("run git");
    assert!(status.success(), "git {args:?} failed in {dir:?}");
}

// ---------------------------------------------------------------------------
// CLI-75: a hash-computation error makes the outdated marker FLAG (not hide).
// ---------------------------------------------------------------------------

/// After learning a skill, make its source content impossible to hash (a
/// dangling symlink inside the skill dir makes `hash_path`'s file read fail),
/// then assert `recall` marks the item outdated rather than silently up to date.
///
/// This is the discrepancy CLI-75 calls out: `upgrade` aborts via `?` on a hash
/// error, but the best-effort listing marker cannot abort the whole listing, so
/// it errs toward flagging. The pre-fix code used `.ok().is_some_and(..)`, which
/// read a hash error as "up to date" and hid the drift.
/// spec: CLI-75
#[test]
fn recall_flags_outdated_when_source_content_cannot_be_hashed() {
    let sb = Sandbox::new("agents");
    let skill_dir = sb.source.join("skills/review");
    write(
        &skill_dir.join("SKILL.md"),
        "---\nname: review\ndescription: review the diff\n---\n# review\n",
    );
    sb.git_init();

    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec]).success, "meld failed");
    let learned = sb.mind(&["learn", "review"]);
    assert!(learned.success, "learn failed: {}", learned.stderr);

    // Sanity: a freshly learned item is NOT outdated.
    let before = sb.mind(&["recall"]);
    assert!(before.success, "recall failed: {}", before.stderr);
    assert!(
        before.stdout.contains("review"),
        "recall should list the learned skill: {}",
        before.stdout
    );
    assert!(
        !before.stdout.contains("outdated"),
        "a freshly learned item must not be marked outdated: {}",
        before.stdout
    );

    // Make the skill's source content unhashable: a dangling symlink inside the
    // skill dir is treated as a file by hash_path, so the read fails with ENOENT.
    // The catalog still lists the skill (its SKILL.md is intact), so the marker
    // path runs and must now flag the item.
    let dangling = skill_dir.join("broken-link");
    std::os::unix::fs::symlink(skill_dir.join("does-not-exist"), &dangling)
        .expect("create dangling symlink");

    let after = sb.mind(&["recall"]);
    assert!(
        after.success,
        "recall must not abort the listing on a hash error: stdout={} stderr={}",
        after.stdout, after.stderr
    );
    assert!(
        after.stdout.contains("review"),
        "the item must still be listed (best-effort marker, no abort): {}",
        after.stdout
    );
    assert!(
        after.stdout.contains("outdated"),
        "a hash-computation error must count as drift and flag the item: {}",
        after.stdout
    );
}

/// Shared setup for the hash-error marker tests: meld a one-skill source, learn
/// the skill, then plant a dangling symlink inside the source skill dir so its
/// content can no longer be hashed (the broken link is read as a file and fails
/// with ENOENT). The catalog still lists the skill because SKILL.md is intact, so
/// every outdated-marker site runs its hash check against an Err.
///
/// Returns the sandbox with the item already learned and the source poisoned.
fn sandbox_with_unhashable_learned_skill() -> Sandbox {
    let sb = Sandbox::new("agents");
    let skill_dir = sb.source.join("skills/review");
    write(
        &skill_dir.join("SKILL.md"),
        "---\nname: review\ndescription: review the diff\n---\n# review\n",
    );
    sb.git_init();

    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec]).success, "meld failed");
    let learned = sb.mind(&["learn", "review"]);
    assert!(learned.success, "learn failed: {}", learned.stderr);

    // Poison the source content: a dangling symlink inside the skill dir makes
    // hash_path's file read fail. SKILL.md remains, so the item still scans.
    let dangling = skill_dir.join("broken-link");
    std::os::unix::fs::symlink(skill_dir.join("does-not-exist"), &dangling)
        .expect("create dangling symlink");
    sb
}

// ---------------------------------------------------------------------------
// CLI-75: the OTHER marker surfaces (single-item recall, item-detail, probe,
// per-source listing via re-meld) must also flag a hash error as drift, not hide
// it. The existing test above only exercises the plain `recall` listing site.
// ---------------------------------------------------------------------------

/// `recall <item>` single-item lookup (the item-detail / status site). A hash
/// error must print the `status` line marking the item out of date. This site
/// uses the phrase "out of date" (not "outdated"), so we assert the exact phrase
/// to be sure we are reading the marker and not some incidental text.
/// spec: CLI-75
#[test]
fn recall_single_item_flags_outdated_when_source_unhashable() {
    let sb = sandbox_with_unhashable_learned_skill();

    // Sanity contrast: resolve the same item before poisoning would NOT be out of
    // date. We re-derive that by checking a fresh, unpoisoned sibling sandbox.
    let clean = Sandbox::new("agents");
    let clean_dir = clean.source.join("skills/review");
    write(
        &clean_dir.join("SKILL.md"),
        "---\nname: review\ndescription: review the diff\n---\n# review\n",
    );
    clean.git_init();
    let clean_spec = clean.source_spec();
    assert!(clean.mind(&["meld", &clean_spec]).success, "meld failed");
    assert!(clean.mind(&["learn", "review"]).success, "learn failed");
    let clean_detail = clean.mind(&["recall", "skill:review"]);
    assert!(clean_detail.success, "clean recall failed");
    assert!(
        clean_detail.stdout.contains("review"),
        "clean detail should show the item: {}",
        clean_detail.stdout
    );
    assert!(
        !clean_detail.stdout.contains("out of date"),
        "a freshly learned item must not be out of date: {}",
        clean_detail.stdout
    );

    // The poisoned source: single-item detail must flag it.
    let r = sb.mind(&["recall", "skill:review"]);
    assert!(
        r.success,
        "single-item recall must not abort on a hash error: stdout={} stderr={}",
        r.stdout, r.stderr
    );
    assert!(
        r.stdout.contains("review"),
        "the item detail must still print: {}",
        r.stdout
    );
    assert!(
        r.stdout.contains("out of date"),
        "a hash error must mark the single-item detail out of date: {}",
        r.stdout
    );
}

/// `probe <query>` row listing (the probe marker site). A hash error must append
/// the outdated token to the matching row, and probe must not abort.
/// spec: CLI-75
#[test]
fn probe_flags_outdated_when_source_unhashable() {
    // Clean contrast: a freshly learned, hashable item probes as NOT outdated.
    // This proves the assertion below discriminates outdated from up to date and
    // is not satisfied by some unrelated occurrence of the marker text.
    let clean = Sandbox::new("agents");
    write(
        &clean.source.join("skills/review/SKILL.md"),
        "---\nname: review\ndescription: review the diff\n---\n# review\n",
    );
    clean.git_init();
    let clean_spec = clean.source_spec();
    assert!(clean.mind(&["meld", &clean_spec]).success, "meld failed");
    assert!(clean.mind(&["learn", "review"]).success, "learn failed");
    let clean_probe = clean.mind(&["probe", "review"]);
    assert!(clean_probe.success, "clean probe failed");
    assert!(
        clean_probe.stdout.contains("review") && !clean_probe.stdout.contains("outdated"),
        "a freshly learned item must probe as not outdated: {}",
        clean_probe.stdout
    );

    let sb = sandbox_with_unhashable_learned_skill();

    let r = sb.mind(&["probe", "review"]);
    assert!(
        r.success,
        "probe must not abort on a hash error: stdout={} stderr={}",
        r.stdout, r.stderr
    );
    assert!(
        r.stdout.contains("review"),
        "probe must still list the matching item: {}",
        r.stdout
    );
    assert!(
        r.stdout.contains("outdated"),
        "a hash error must flag the probe row outdated: {}",
        r.stdout
    );
}

/// The per-source listing site is `source_status`, reached by re-running `meld`
/// on an already-melded source (it prints each item with its install state). A
/// hash error must flag the item outdated there too, and the re-meld must not
/// abort.
/// spec: CLI-75
#[test]
fn remeld_source_status_flags_outdated_when_source_unhashable() {
    // Clean contrast: re-melding a hashable source shows the item NOT outdated, so
    // the assertion below truly discriminates rather than always passing.
    let clean = Sandbox::new("agents");
    write(
        &clean.source.join("skills/review/SKILL.md"),
        "---\nname: review\ndescription: review the diff\n---\n# review\n",
    );
    clean.git_init();
    let clean_spec = clean.source_spec();
    assert!(clean.mind(&["meld", &clean_spec]).success, "meld failed");
    assert!(clean.mind(&["learn", "review"]).success, "learn failed");
    let clean_remeld = clean.mind(&["meld", &clean_spec]);
    assert!(clean_remeld.success, "clean re-meld failed");
    assert!(
        clean_remeld.stdout.contains("review") && !clean_remeld.stdout.contains("outdated"),
        "a freshly learned item must re-meld as not outdated: {}",
        clean_remeld.stdout
    );

    let sb = sandbox_with_unhashable_learned_skill();

    // Re-melding the already-melded source falls through to source_status.
    let spec = sb.source_spec();
    let r = sb.mind(&["meld", &spec]);
    assert!(
        r.success,
        "re-meld status view must not abort on a hash error: stdout={} stderr={}",
        r.stdout, r.stderr
    );
    assert!(
        r.stdout.contains("review"),
        "the source status view must still list the item: {}",
        r.stdout
    );
    assert!(
        r.stdout.contains("outdated"),
        "a hash error must flag the source-status row outdated: {}",
        r.stdout
    );
}

// ---------------------------------------------------------------------------
// CLI-28 / CLI-86: a malformed glob selector reports InvalidPattern, not
// SourceNotFound.
// ---------------------------------------------------------------------------

/// `unmeld '[bad'` carries glob metacharacters but is not a valid pattern. It
/// must report a clear invalid-pattern error rather than silently matching
/// nothing and surfacing as `SourceNotFound`.
/// spec: CLI-28
#[test]
fn unmeld_malformed_glob_reports_invalid_pattern_not_source_not_found() {
    let sb = Sandbox::new("agents");
    write(
        &sb.source.join("agents/dev.md"),
        "---\ndescription: dev agent\n---\n# dev\n",
    );
    sb.git_init();
    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec]).success, "meld failed");

    let r = sb.mind(&["unmeld", "[bad", "--yes"]);
    assert!(!r.success, "malformed glob must fail: {}", r.stdout);
    assert!(
        r.stderr.contains("not a valid glob selector"),
        "expected invalid-pattern error, got stderr={} stdout={}",
        r.stderr,
        r.stdout
    );
    assert!(
        !r.stderr.contains("no source named"),
        "malformed glob must NOT surface as SourceNotFound: {}",
        r.stderr
    );
}

/// The `recall --source '[bad'` filter shares the glob matcher, so a malformed
/// pattern must likewise report InvalidPattern rather than silently matching no
/// sources (which would read as an empty/uninformative listing).
/// spec: CLI-86
#[test]
fn recall_source_filter_malformed_glob_reports_invalid_pattern() {
    let sb = Sandbox::new("agents");
    write(
        &sb.source.join("agents/dev.md"),
        "---\ndescription: dev agent\n---\n# dev\n",
    );
    sb.git_init();
    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec]).success, "meld failed");

    let r = sb.mind(&["recall", "--source", "[bad"]);
    assert!(
        !r.success,
        "malformed --source glob must fail: {}",
        r.stdout
    );
    assert!(
        r.stderr.contains("not a valid glob selector"),
        "expected invalid-pattern error, got stderr={} stdout={}",
        r.stderr,
        r.stdout
    );
}

/// The `probe --source '[bad'` filter shares the same matcher and must report
/// InvalidPattern as well.
/// spec: CLI-86
#[test]
fn probe_source_filter_malformed_glob_reports_invalid_pattern() {
    let sb = Sandbox::new("agents");
    write(
        &sb.source.join("agents/dev.md"),
        "---\ndescription: dev agent\n---\n# dev\n",
    );
    sb.git_init();
    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec]).success, "meld failed");

    let r = sb.mind(&["probe", "--source", "[bad"]);
    assert!(
        !r.success,
        "malformed --source glob must fail: {}",
        r.stdout
    );
    assert!(
        r.stderr.contains("not a valid glob selector"),
        "expected invalid-pattern error, got stderr={} stdout={}",
        r.stderr,
        r.stdout
    );
}

/// A VALID glob selector is unaffected by the new validation step: `unmeld '*'`
/// still matches and removes the source.
/// spec: CLI-28
#[test]
fn unmeld_valid_glob_still_matches() {
    let sb = Sandbox::new("agents");
    write(
        &sb.source.join("agents/dev.md"),
        "---\ndescription: dev agent\n---\n# dev\n",
    );
    sb.git_init();
    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec]).success, "meld failed");

    let r = sb.mind(&["unmeld", "*", "--yes"]);
    assert!(
        r.success,
        "a valid glob must still match and unmeld: stdout={} stderr={}",
        r.stdout, r.stderr
    );
}