mind-cli 0.28.1

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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Integration tests for item build hooks with `--dangerously-skip-build-hook-check`.
//!
//! Before HOOK-74, build hooks could not run in a non-TTY context at all (HOOK-72
//! skips them). These tests exercise the new headless path and its invariants:
//!
//! - (a) build hook runs during `learn --dangerously-skip-build-hook-check`
//! - (b) a failing build hook rolls the install back (HOOK-71)
//! - (c) the build hook re-runs when the item is reinstalled/upgraded (HOOK-73)
//! - (d) without the flag, a non-TTY context still skips the build hook (HOOK-72)

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

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

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) -> Sandbox {
        let n = COUNTER.fetch_add(1, Ordering::SeqCst);
        let base = std::env::temp_dir().join(format!("mind-bld-{}-{n}", std::process::id()));
        let _ = std::fs::remove_dir_all(&base);
        let source = base.join(name);
        let sb = Sandbox {
            base: base.clone(),
            source: source.clone(),
            mind_home: base.join("mind"),
            claude_home: base.join("claude"),
        };
        write(&source.join("README.md"), "# fixture\n");
        git(&source, &["-c", "init.defaultBranch=main", "init", "-q"]);
        git(&source, &["config", "user.email", "t@t"]);
        git(&source, &["config", "user.name", "t"]);
        git(&source, &["add", "-A"]);
        git(&source, &["commit", "-qm", "initial"]);
        sb
    }

    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 write_and_commit(&self, rel: &str, contents: &str) {
        write(&self.source.join(rel), contents);
        git(&self.source, &["add", "-A"]);
        git(&self.source, &["commit", "-qm", "fixture"]);
    }

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

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:?}");
}

// ---------------------------------------------------------------------------
// (a) build hook runs and produces side effects under the flag (HOOK-71/74)
// ---------------------------------------------------------------------------

#[test]
fn build_hook_runs_when_flag_is_given() {
    // spec: HOOK-71 HOOK-74
    // With --dangerously-skip-build-hook-check, the build hook must execute
    // during `learn`. The hook creates a sentinel file inside the tool's staging
    // directory, which ends up in the store after the swap (HOOK-71: the hook
    // runs in staging, its output lands in the store atomically on success).
    let sb = Sandbox::new("bld");
    let sentinel_name = "built.txt";

    // A tool whose build hook writes a sentinel file.
    let toml = format!(
        concat!(
            "[[items]]\n",
            "kind = \"tool\"\n",
            "name = \"mytool\"\n",
            "path = \"tools/mytool\"\n",
            "build = \"touch {sentinel}\"\n",
        ),
        sentinel = sentinel_name
    );
    sb.write_and_commit(
        "tools/mytool/TOOL.md",
        "---\ndescription: test tool\n---\n# mytool\n",
    );
    sb.write_and_commit("mind.toml", &toml);

    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec, "--link-only"]).success);

    let learn = sb.mind(&[
        "learn",
        "tool:mytool",
        "--dangerously-skip-build-hook-check",
    ]);
    assert!(
        learn.success,
        "learn with --dangerously-skip-build-hook-check must succeed: {} {}",
        learn.stdout, learn.stderr
    );

    // The sentinel file must exist in the store (proving the hook ran in staging
    // and its output was swapped into the store, HOOK-71).
    let store_sentinel = sb.mind_home.join("store/tool/mytool").join(sentinel_name);
    assert!(
        store_sentinel.exists(),
        "build hook must have created {sentinel_name} in the store: {store_sentinel:?}"
    );
}

// ---------------------------------------------------------------------------
// (b) failing build hook rolls back the install (HOOK-71)
// ---------------------------------------------------------------------------

#[test]
fn failing_build_hook_rolls_back_install() {
    // spec: HOOK-71 HOOK-74
    // A build hook that exits non-zero is a hard stop: the staging copy is
    // discarded and the live install is untouched. With the flag the hook runs
    // (not skipped), so the failure is observable.
    let sb = Sandbox::new("fail");

    let toml = concat!(
        "[[items]]\n",
        "kind = \"tool\"\n",
        "name = \"badtool\"\n",
        "path = \"tools/badtool\"\n",
        "build = \"exit 7\"\n",
    );
    sb.write_and_commit(
        "tools/badtool/TOOL.md",
        "---\ndescription: failing build\n---\n# badtool\n",
    );
    sb.write_and_commit("mind.toml", toml);

    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec, "--link-only"]).success);

    let learn = sb.mind(&[
        "learn",
        "tool:badtool",
        "--dangerously-skip-build-hook-check",
    ]);
    assert!(
        !learn.success,
        "learn must fail when the build hook exits non-zero (HOOK-71): {} {}",
        learn.stdout, learn.stderr
    );

    // The store copy must be absent: staging discarded, live install untouched.
    let store_path = sb.mind_home.join("store/tool/badtool");
    assert!(
        !store_path.exists(),
        "store copy must not exist after a failed build hook (HOOK-71): {store_path:?}"
    );

    // The error output must surface the hook failure.
    let combined = format!("{}{}", learn.stdout, learn.stderr);
    assert!(
        combined.contains("exit 7") || combined.contains("failed") || combined.contains("build"),
        "failure output must reference the build hook error: {combined}"
    );
}

// ---------------------------------------------------------------------------
// (c) build hook re-runs on reinstall and upgrade (HOOK-73)
// ---------------------------------------------------------------------------

#[test]
fn build_hook_reruns_on_reinstall() {
    // spec: HOOK-73 HOOK-74
    // Every (re)install rebuilds the store copy from staging, so the build hook
    // runs again. A counter file in the tool dir lets us observe the number of
    // runs: the hook appends a line each time it executes.
    let sb = Sandbox::new("rerun");
    let run_log = sb.base.join("run.log");
    let log_path = run_log.display().to_string();

    let toml = format!(
        concat!(
            "[[items]]\n",
            "kind = \"tool\"\n",
            "name = \"counter\"\n",
            "path = \"tools/counter\"\n",
            "build = \"echo RAN >> {log}\"\n",
        ),
        log = log_path
    );
    sb.write_and_commit(
        "tools/counter/TOOL.md",
        "---\ndescription: counter tool\n---\n# counter\n",
    );
    sb.write_and_commit("mind.toml", &toml);

    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec, "--link-only"]).success);

    // First learn: build hook runs once.
    let learn1 = sb.mind(&[
        "learn",
        "tool:counter",
        "--dangerously-skip-build-hook-check",
    ]);
    assert!(
        learn1.success,
        "first learn must succeed: {} {}",
        learn1.stdout, learn1.stderr
    );
    let lines_after_first = std::fs::read_to_string(&run_log)
        .unwrap_or_default()
        .lines()
        .filter(|l| l.contains("RAN"))
        .count();
    assert_eq!(
        lines_after_first, 1,
        "build hook must have run once after the first learn (HOOK-73)"
    );

    // Forget and re-learn (reinstall): build hook runs again.
    assert!(sb.mind(&["forget", "tool:counter"]).success);
    let learn2 = sb.mind(&[
        "learn",
        "tool:counter",
        "--dangerously-skip-build-hook-check",
    ]);
    assert!(
        learn2.success,
        "second learn must succeed: {} {}",
        learn2.stdout, learn2.stderr
    );
    let lines_after_second = std::fs::read_to_string(&run_log)
        .unwrap_or_default()
        .lines()
        .filter(|l| l.contains("RAN"))
        .count();
    assert_eq!(
        lines_after_second, 2,
        "build hook must re-run on reinstall (HOOK-73): total runs should be 2"
    );
}

#[test]
fn build_hook_reruns_on_upgrade() {
    // spec: HOOK-73 HOOK-74
    // `upgrade` reinstalls the item from the new staging copy, so the build hook
    // runs again whenever the item is upgraded. A log file lets us count runs.
    let sb = Sandbox::new("upg");
    let run_log = sb.base.join("upg.log");
    let log_path = run_log.display().to_string();

    let toml = format!(
        concat!(
            "[[items]]\n",
            "kind = \"tool\"\n",
            "name = \"utool\"\n",
            "path = \"tools/utool\"\n",
            "build = \"echo RAN >> {log}\"\n",
        ),
        log = log_path
    );
    sb.write_and_commit(
        "tools/utool/TOOL.md",
        "---\ndescription: upgradeable tool\n---\n# utool\n",
    );
    sb.write_and_commit("mind.toml", &toml);

    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec, "--link-only"]).success);

    // First install.
    let learn = sb.mind(&["learn", "tool:utool", "--dangerously-skip-build-hook-check"]);
    assert!(
        learn.success,
        "initial learn must succeed: {} {}",
        learn.stdout, learn.stderr
    );
    assert_eq!(
        std::fs::read_to_string(&run_log)
            .unwrap_or_default()
            .lines()
            .filter(|l| l.contains("RAN"))
            .count(),
        1,
        "build hook ran once after initial learn"
    );

    // Modify the tool to create a new hash, then upgrade.
    sb.write_and_commit(
        "tools/utool/TOOL.md",
        "---\ndescription: upgradeable tool v2\n---\n# utool\n",
    );
    let upgrade = sb.mind(&["upgrade", "--yes", "--dangerously-skip-build-hook-check"]);
    assert!(
        upgrade.success,
        "upgrade must succeed: {} {}",
        upgrade.stdout, upgrade.stderr
    );

    let total_runs = std::fs::read_to_string(&run_log)
        .unwrap_or_default()
        .lines()
        .filter(|l| l.contains("RAN"))
        .count();
    assert_eq!(
        total_runs, 2,
        "build hook must re-run on upgrade (HOOK-73): total runs should be 2, got {total_runs}"
    );
}

// ---------------------------------------------------------------------------
// (d) without the flag, non-TTY still skips (HOOK-72 regression guard)
// ---------------------------------------------------------------------------

#[test]
fn build_hook_is_skipped_without_flag_in_non_tty() {
    // spec: HOOK-72
    // Without --dangerously-skip-build-hook-check, the non-TTY (stdin=null)
    // context must skip the build hook and still succeed (the item installs
    // unbuilt). The sentinel file that the hook would create must NOT exist.
    let sb = Sandbox::new("skip");
    let sentinel_name = "should_not_exist.txt";

    let toml = format!(
        concat!(
            "[[items]]\n",
            "kind = \"tool\"\n",
            "name = \"skiptool\"\n",
            "path = \"tools/skiptool\"\n",
            "build = \"touch {sentinel}\"\n",
        ),
        sentinel = sentinel_name
    );
    sb.write_and_commit(
        "tools/skiptool/TOOL.md",
        "---\ndescription: skip tool\n---\n# skiptool\n",
    );
    sb.write_and_commit("mind.toml", &toml);

    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec, "--link-only"]).success);

    // No --dangerously-skip-build-hook-check: hook must be skipped.
    let learn = sb.mind(&["learn", "tool:skiptool"]);
    assert!(
        learn.success,
        "learn without the flag must still succeed (item installs unbuilt, HOOK-72): {} {}",
        learn.stdout, learn.stderr
    );

    // The item must be in the store (it installed, just not built).
    assert!(
        sb.mind_home.join("store/tool/skiptool").exists(),
        "item must be in the store even when its build hook was skipped"
    );

    // The sentinel must not exist: the hook did not run.
    let store_sentinel = sb.mind_home.join("store/tool/skiptool").join(sentinel_name);
    assert!(
        !store_sentinel.exists(),
        "build hook must NOT run in a non-TTY context without the flag (HOOK-72): {store_sentinel:?}"
    );

    // A note about the skipped hook must appear in output.
    let combined = format!("{}{}", learn.stdout, learn.stderr);
    assert!(
        combined.contains("skipped") && combined.contains("build"),
        "output must mention that the build hook was skipped (HOOK-72): {combined}"
    );
}

#[test]
fn build_hook_output_streams_like_every_other_hook() {
    // spec: HOOK-30
    // Build hooks are not a separate execution path: `install::run_build_hook`
    // delegates to `hook::run_hook`, so they inherit their streams like source
    // and item hooks do. This pins the routing to the right fd, not the
    // timing: a capture-and-replay that routed each stream back to its own fd
    // would also pass. A build hook is the likeliest one to be slow and chatty
    // (it is a build), so a regression here that reintroduced capture-and-dump
    // would hit exactly the case streaming is for, and nothing else in the
    // suite would notice.
    let sb = Sandbox::new("bld-stream");
    let toml = concat!(
        "[[items]]\n",
        "kind = \"tool\"\n",
        "name = \"mytool\"\n",
        "path = \"tools/mytool\"\n",
        "build = \"echo BUILD-ON-STDOUT; echo BUILD-ON-STDERR 1>&2\"\n",
    );
    sb.write_and_commit(
        "tools/mytool/TOOL.md",
        "---\ndescription: test tool\n---\n# mytool\n",
    );
    sb.write_and_commit("mind.toml", toml);

    let spec = sb.source_spec();
    assert!(sb.mind(&["meld", &spec, "--link-only"]).success);
    let learn = sb.mind(&[
        "learn",
        "tool:mytool",
        "--dangerously-skip-build-hook-check",
    ]);
    assert!(
        learn.success,
        "learn failed: {} {}",
        learn.stdout, learn.stderr
    );

    // The frame is mind's own output, so it is on stdout ...
    assert!(
        learn.stdout.contains("====== (hook: build) ======")
            && learn.stdout.contains("====== (end hook: build) ======"),
        "the build hook must be framed: {}",
        learn.stdout
    );
    // ... the hook's stdout reaches stdout ...
    assert!(
        learn.stdout.contains("BUILD-ON-STDOUT"),
        "the build hook's stdout must reach mind's stdout: {}",
        learn.stdout
    );
    // ... and its stderr reaches stderr, not replayed onto stdout. A captured
    // implementation would put both on stdout, which is what this catches.
    assert!(
        learn.stderr.contains("BUILD-ON-STDERR"),
        "the build hook's stderr must reach mind's stderr: {}",
        learn.stderr
    );
    assert!(
        !learn.stdout.contains("BUILD-ON-STDERR"),
        "and must not be copied onto stdout: {}",
        learn.stdout
    );
}