mind-cli 0.48.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
//! Integration tests for update hooks and for an item's own hook declaration
//! sites.
//!
//! Each test drives the real `mind` binary against a hermetic, network-free
//! fixture (local git repo, isolated MIND_HOME/CLAUDE_HOME).
//!
//! Spec coverage:
//!   HOOK-121: a source's update hooks run at `upgrade`, never at `meld`, and
//!             are baselined at the meld commit so an unmoved source runs none
//!   HOOK-122: update hooks supersede install hooks on an update; a source that
//!             declares none re-runs its install hooks unchanged
//!   HOOK-124: an update hook's run is recorded under its own event, so it is
//!             not re-offered at the same commit and does not alias the install
//!             hook's record
//!   HOOK-55:  a recorded install hook the source no longer declares is not
//!             replayed from local state
//!   HOOK-56:  a consumer `--install-hook` override covers the update event too
//!   HOOK-125: an item's update hooks replace its install hooks on a re-install
//!   HOOK-130: `install:`/`update:`/`uninstall:` frontmatter on any kind
//!   HOOK-131: a scoped `mind.toml` in a skill's own directory
//!   HOOK-133: an item manifest is item content: copied into the store, hashed
//!   CLI-250:  `--accept-hooks=<events>` gates install and update separately
//!   CLI-251:  the hidden `--dangerously-skip-install-hook-check` still works

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-uh-{}-{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()
    }

    /// The registry identity a local-path meld gives this fixture:
    /// `local/<parent dir>/<repo dir>` (STO-13).
    fn source_id(&self) -> String {
        let owner = self
            .base
            .file_name()
            .expect("base has a name")
            .to_string_lossy();
        let repo = self
            .source
            .file_name()
            .expect("source has a name")
            .to_string_lossy();
        format!("local/{owner}/{repo}")
    }

    /// A shell command appending one line to an absolute sandbox path, so a
    /// test can count how many times a hook ran (not just whether it ran).
    fn tally(&self, name: &str) -> String {
        format!("echo ran >> {}", self.base.join(name).display())
    }

    /// How many times the hook writing `name` has run.
    fn runs(&self, name: &str) -> usize {
        std::fs::read_to_string(self.base.join(name))
            .map(|s| s.lines().count())
            .unwrap_or(0)
    }
}

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

// ---------------------------------------------------------------------------
// Source update hooks (HOOK-121, HOOK-122, HOOK-124)
// ---------------------------------------------------------------------------

/// A source that declares an update hook runs it at `upgrade` INSTEAD of
/// re-running its install hook, and does not run it at `meld`.
#[test]
fn source_update_hook_replaces_the_install_rerun_at_upgrade() {
    // spec: HOOK-121 HOOK-122
    let sb = Sandbox::new("update-src");
    let install = sb.tally("install.log");
    let update = sb.tally("update.log");
    sb.write_and_commit(
        "mind.toml",
        &format!(
            "[[hooks]]\nrun = \"{install}\"\n\n[[hooks]]\nrun = \"{update}\"\nevent = \"update\"\n"
        ),
    );

    let r = sb.mind(&[
        "meld",
        &sb.source_spec(),
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "meld: {}", r.stderr);
    assert_eq!(sb.runs("install.log"), 1, "meld runs the install hook");
    assert_eq!(
        sb.runs("update.log"),
        0,
        "an update hook must not run at meld: {}",
        r.stdout
    );

    // The source advances.
    sb.write_and_commit("README.md", "# fixture v2\n");
    assert!(sb.mind(&["sync"]).success, "sync must succeed");

    let r = sb.mind(&[
        "upgrade",
        "--yes",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("update.log"),
        1,
        "the update hook must run at upgrade: {}",
        r.stdout
    );
    assert_eq!(
        sb.runs("install.log"),
        1,
        "the install hook must NOT be re-run when an update hook exists: {}",
        r.stdout
    );
}

/// `--accept-hooks=<events>` accepts only the named events: `install` alone
/// leaves an update hook to the non-TTY skip, and `update` then runs it.
#[test]
fn accept_hooks_events_gate_install_and_update_separately() {
    // spec: CLI-250 HOOK-150
    let sb = Sandbox::new("accept-src");
    let install = sb.tally("install.log");
    let update = sb.tally("update.log");
    sb.write_and_commit(
        "mind.toml",
        &format!(
            "[[hooks]]\nrun = \"{install}\"\n\n[[hooks]]\nrun = \"{update}\"\nevent = \"update\"\n"
        ),
    );

    let r = sb.mind(&["meld", &sb.source_spec(), "--accept-hooks=update"]);
    assert!(r.success, "meld: {}", r.stderr);
    assert_eq!(
        sb.runs("install.log"),
        0,
        "--accept-hooks=update must not accept an install hook: {}",
        r.stdout
    );
    let r = sb.mind(&["meld", &sb.source_spec(), "--accept-hooks=install"]);
    assert!(r.success, "re-meld: {}", r.stderr);
    assert_eq!(
        sb.runs("install.log"),
        1,
        "the install hook runs: {}",
        r.stdout
    );

    sb.write_and_commit("README.md", "# fixture v2\n");
    let r = sb.mind(&["upgrade", "--yes", "--accept-hooks=install"]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("update.log"),
        0,
        "--accept-hooks=install must not accept an update hook: {}",
        r.stdout
    );

    let r = sb.mind(&["upgrade", "--yes", "--accept-hooks=update"]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("update.log"),
        1,
        "--accept-hooks=update runs the pending update hook: {}",
        r.stdout
    );
    assert_eq!(sb.runs("install.log"), 1);
}

/// The hidden pre-rename flag still runs install and update hooks unattended.
#[test]
fn legacy_install_hook_flag_still_runs_hooks() {
    // spec: CLI-251
    let sb = Sandbox::new("legacy-src");
    let install = sb.tally("install.log");
    let update = sb.tally("update.log");
    sb.write_and_commit(
        "mind.toml",
        &format!(
            "[[hooks]]\nrun = \"{install}\"\n\n[[hooks]]\nrun = \"{update}\"\nevent = \"update\"\n"
        ),
    );
    let r = sb.mind(&[
        "meld",
        &sb.source_spec(),
        "--dangerously-skip-install-hook-check",
    ]);
    assert!(r.success, "meld: {}", r.stderr);
    assert_eq!(sb.runs("install.log"), 1, "{}", r.stdout);

    sb.write_and_commit("README.md", "# fixture v2\n");
    let r = sb.mind(&["upgrade", "--yes", "--dangerously-skip-install-hook-check"]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(sb.runs("update.log"), 1, "{}", r.stdout);
}

/// A source that declares no update hook keeps the unchanged behavior: its
/// install hook is re-offered when the source advances.
#[test]
fn source_without_an_update_hook_reruns_its_install_hook() {
    // spec: HOOK-122 HOOK-11
    let sb = Sandbox::new("plain-src");
    let install = sb.tally("install.log");
    sb.write_and_commit("mind.toml", &format!("[[hooks]]\nrun = \"{install}\"\n"));

    let r = sb.mind(&[
        "meld",
        &sb.source_spec(),
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "meld: {}", r.stderr);
    assert_eq!(sb.runs("install.log"), 1);

    sb.write_and_commit("README.md", "# fixture v2\n");
    assert!(sb.mind(&["sync"]).success, "sync must succeed");
    let r = sb.mind(&[
        "upgrade",
        "--yes",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("install.log"),
        2,
        "an advanced source re-runs its install hook: {}",
        r.stdout
    );
}

/// An update hook's run is recorded against the commit it ran at, so a second
/// `upgrade` at the same commit does not run it again.
#[test]
fn source_update_hook_is_recorded_and_not_rerun_at_the_same_commit() {
    // spec: HOOK-124
    let sb = Sandbox::new("recorded-src");
    let update = sb.tally("update.log");
    sb.write_and_commit(
        "mind.toml",
        &format!("[[hooks]]\nrun = \"{update}\"\nevent = \"update\"\n"),
    );

    assert!(
        sb.mind(&[
            "meld",
            &sb.source_spec(),
            "--accept-hooks=install,update,uninstall",
        ])
        .success
    );
    sb.write_and_commit("README.md", "# fixture v2\n");
    assert!(sb.mind(&["sync"]).success);
    assert!(
        sb.mind(&[
            "upgrade",
            "--yes",
            "--accept-hooks=install,update,uninstall"
        ])
        .success
    );
    assert_eq!(sb.runs("update.log"), 1);

    // Nothing advanced: the recorded run-commit is current, so nothing pends.
    let r = sb.mind(&[
        "upgrade",
        "--yes",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "second upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("update.log"),
        1,
        "an update hook recorded at the current commit must not re-run: {}",
        r.stdout
    );
}

/// `meld` then `upgrade` with the source sitting on the very commit it was
/// melded at runs NO update hook: the update event means the source has moved,
/// and it has not. Only once it advances does the hook become pending.
#[test]
fn an_unmoved_source_runs_no_update_hook_on_the_first_upgrade() {
    // spec: HOOK-121
    let sb = Sandbox::new("baseline-src");
    let update = sb.tally("update.log");
    sb.write_and_commit(
        "mind.toml",
        &format!("[[hooks]]\nrun = \"{update}\"\nevent = \"update\"\n"),
    );

    let r = sb.mind(&[
        "meld",
        &sb.source_spec(),
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "meld: {}", r.stderr);
    assert_eq!(
        sb.runs("update.log"),
        0,
        "an update hook must not run at meld"
    );

    // Nothing moved. `sync` records the same commit; `upgrade` must find the
    // update hook settled at it.
    assert!(sb.mind(&["sync"]).success, "sync must succeed");
    let r = sb.mind(&[
        "upgrade",
        "--yes",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("update.log"),
        0,
        "an unmoved source must not run its update hook: {}\n{}",
        r.stdout,
        r.stderr
    );

    // `hooks list` reports the baseline as such, not as a run that happened.
    let r = sb.mind(&["hooks", "list", &sb.source_id()]);
    assert!(r.success, "hooks list: {}\n{}", r.stdout, r.stderr);
    assert!(
        r.stdout.contains("recorded at meld"),
        "the baseline must not be reported as a run: {}",
        r.stdout
    );

    // The source advances: now it is pending and runs.
    sb.write_and_commit("README.md", "# fixture v2\n");
    assert!(sb.mind(&["sync"]).success);
    let r = sb.mind(&[
        "upgrade",
        "--yes",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("update.log"),
        1,
        "an advanced source runs its update hook: {}",
        r.stdout
    );
}

/// One command declared for BOTH events is two records, keyed by event. An
/// install run at the current commit must not settle the update hook: keyed by
/// command alone, `hooks run --event update` would exit 0 having run nothing.
#[test]
fn an_install_run_does_not_settle_the_same_command_on_the_update_event() {
    // spec: HOOK-124
    let sb = Sandbox::new("aliased-src");
    let both = sb.tally("both.log");
    sb.write_and_commit(
        "mind.toml",
        &format!(
            "[[hooks]]\nrun = \"{both}\"\n\n[[hooks]]\nrun = \"{both}\"\nevent = \"update\"\n"
        ),
    );

    let r = sb.mind(&[
        "meld",
        &sb.source_spec(),
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "meld: {}", r.stderr);
    assert_eq!(sb.runs("both.log"), 1, "the install hook ran at meld");

    // Advance, then run the INSTALL hook on demand at the new commit, so its
    // record sits exactly at the source's current commit.
    sb.write_and_commit("README.md", "# fixture v2\n");
    assert!(sb.mind(&["sync"]).success);
    let r = sb.mind(&[
        "hooks",
        "run",
        &sb.source_id(),
        "--event",
        "install",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "hooks run install: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("both.log"),
        2,
        "the install hook ran at the new commit"
    );

    // The update hook's own record is still the meld baseline, so it is pending
    // at this commit and must run.
    let r = sb.mind(&[
        "hooks",
        "run",
        &sb.source_id(),
        "--event",
        "update",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "hooks run update: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("both.log"),
        3,
        "the update hook is not settled by the install hook's run: {}\n{}",
        r.stdout,
        r.stderr
    );
}

/// A `hooks run` whose every hook is held back by the pending filter says so,
/// names the commit, and prints the `--force` invocation, instead of exiting 0
/// in silence (which reads as "this source declares no hooks").
#[test]
fn hooks_run_reports_nothing_pending_instead_of_exiting_silently() {
    // spec: HOOK-126
    let sb = Sandbox::new("settled-src");
    let install = sb.tally("install.log");
    sb.write_and_commit("mind.toml", &format!("[[hooks]]\nrun = \"{install}\"\n"));

    assert!(
        sb.mind(&[
            "meld",
            &sb.source_spec(),
            "--accept-hooks=install,update,uninstall",
        ])
        .success
    );
    assert_eq!(sb.runs("install.log"), 1);

    let r = sb.mind(&[
        "hooks",
        "run",
        &sb.source_id(),
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "hooks run: {}\n{}", r.stdout, r.stderr);
    assert_eq!(sb.runs("install.log"), 1, "nothing pending, so nothing ran");
    let out = format!("{}{}", r.stdout, r.stderr);
    assert!(
        out.contains("nothing pending") && out.contains("--force"),
        "a run with nothing pending must say so and name --force: {out}"
    );

    // And `--force` does run it, which is what the note advertises.
    let r = sb.mind(&[
        "hooks",
        "run",
        &sb.source_id(),
        "--force",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "forced hooks run: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("install.log"),
        2,
        "--force re-runs the settled hook"
    );
}

/// A recorded install hook the source has since WITHDRAWN is not re-offered at
/// the next `upgrade`: the record says the command ran, never that the source
/// still stands behind it.
#[test]
fn a_withdrawn_install_hook_is_not_replayed_at_upgrade() {
    // spec: HOOK-55
    let sb = Sandbox::new("withdrawn-src");
    let install = sb.tally("install.log");
    sb.write_and_commit("mind.toml", &format!("[[hooks]]\nrun = \"{install}\"\n"));

    assert!(
        sb.mind(&[
            "meld",
            &sb.source_spec(),
            "--accept-hooks=install,update,uninstall",
        ])
        .success
    );
    assert_eq!(sb.runs("install.log"), 1);

    // The source withdraws the hook and advances.
    sb.write_and_commit("mind.toml", "[source]\ndescription = \"no hooks\"\n");
    assert!(sb.mind(&["sync"]).success);
    let r = sb.mind(&[
        "upgrade",
        "--yes",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("install.log"),
        1,
        "a command the source no longer declares must not be replayed: {}\n{}",
        r.stdout,
        r.stderr
    );
}

/// A curator's `[[discover.sources.hooks]]` update entry (DSC-61) is recorded
/// on the nested source at `meld` and offered at `upgrade`. It is declared in
/// the parent's manifest, so without the record it is invisible to the upgrade
/// selector and can never run at all.
#[test]
fn a_curated_update_hook_runs_at_upgrade_and_not_at_meld() {
    // spec: HOOK-127 HOOK-121
    let nested = Sandbox::new("curated-nested");
    let registry = Sandbox::new("curated-registry");
    let update = registry.tally("curated-update.log");
    registry.write_and_commit(
        "mind.toml",
        &format!(
            "[[discover.sources]]\nsource = \"{}\"\n\n\
             [[discover.sources.hooks]]\nrun = \"{update}\"\nevent = \"update\"\n",
            nested.source_spec()
        ),
    );

    let r = registry.mind(&[
        "meld",
        &registry.source_spec(),
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "meld: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        registry.runs("curated-update.log"),
        0,
        "an update hook must not run at meld: {}",
        r.stdout
    );

    // The nested source advances.
    nested.write_and_commit("README.md", "# fixture v2\n");
    assert!(registry.mind(&["sync"]).success, "sync must succeed");
    let r = registry.mind(&[
        "upgrade",
        "--yes",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        registry.runs("curated-update.log"),
        1,
        "the curated update hook must run at upgrade: {}\n{}",
        r.stdout,
        r.stderr
    );
}

/// A consumer who melded with `--install-hook` keeps their command on the
/// update path too: a source that moves its command to `event = "update"` does
/// not slip past the override, and the consumer's own command still runs.
#[test]
fn a_meld_install_hook_override_covers_the_update_event() {
    // spec: HOOK-56 HOOK-122
    let sb = Sandbox::new("override-src");
    let theirs = sb.tally("theirs.log");
    let mine = sb.tally("mine.log");
    sb.write_and_commit("mind.toml", &format!("[[hooks]]\nrun = \"{theirs}\"\n"));

    let r = sb.mind(&[
        "meld",
        &sb.source_spec(),
        "--install-hook",
        &mine,
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "meld: {}\n{}", r.stdout, r.stderr);
    assert_eq!(sb.runs("mine.log"), 1, "the consumer's command ran");
    assert_eq!(
        sb.runs("theirs.log"),
        0,
        "the author's command was replaced"
    );

    // The source moves the same command to the update event and advances.
    sb.write_and_commit(
        "mind.toml",
        &format!("[[hooks]]\nrun = \"{theirs}\"\nevent = \"update\"\n"),
    );
    assert!(sb.mind(&["sync"]).success);
    let r = sb.mind(&[
        "upgrade",
        "--yes",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("theirs.log"),
        0,
        "the override must cover the update event: {}\n{}",
        r.stdout,
        r.stderr
    );
    assert_eq!(
        sb.runs("mine.log"),
        2,
        "the consumer's command is what runs on the update path: {}\n{}",
        r.stdout,
        r.stderr
    );
}

// ---------------------------------------------------------------------------
// Item update hooks (HOOK-125)
// ---------------------------------------------------------------------------

/// An item's update hooks replace its install hooks when the item is
/// re-installed over an existing install; a first install runs the install
/// hooks.
#[test]
fn item_update_hook_replaces_the_install_hook_on_upgrade() {
    // spec: HOOK-125 HOOK-130
    let sb = Sandbox::new("item-src");
    let install = sb.tally("install.log");
    let update = sb.tally("update.log");
    sb.write_and_commit(
        "skills/scanner/SKILL.md",
        &format!(
            "---\ndescription: scanner\ninstall: {install}\nupdate: {update}\n---\n# scanner\n"
        ),
    );

    assert!(sb.mind(&["meld", &sb.source_spec()]).success);
    let r = sb.mind(&[
        "learn",
        "scanner",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "learn: {}\n{}", r.stdout, r.stderr);
    assert_eq!(sb.runs("install.log"), 1, "a first install runs `install`");
    assert_eq!(sb.runs("update.log"), 0, "a first install skips `update`");

    // The item's content changes upstream.
    sb.write_and_commit(
        "skills/scanner/SKILL.md",
        &format!(
            "---\ndescription: scanner\ninstall: {install}\nupdate: {update}\n---\n# scanner v2\n"
        ),
    );
    assert!(sb.mind(&["sync"]).success);
    let r = sb.mind(&[
        "upgrade",
        "--yes",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("update.log"),
        1,
        "an upgraded item runs its update hook: {}",
        r.stdout
    );
    assert_eq!(
        sb.runs("install.log"),
        1,
        "an upgraded item must not re-run its install hook: {}",
        r.stdout
    );
}

/// A `learn` naming an already-installed item installs nothing (CLI-157), so it
/// runs neither the install nor the update hooks: the re-install path that
/// swaps update hooks in is `upgrade`, not a repeat `learn`.
#[test]
fn relearning_an_installed_item_is_a_no_op_and_runs_no_hook() {
    // spec: HOOK-125 CLI-157
    let sb = Sandbox::new("relearn-src");
    let install = sb.tally("install.log");
    let update = sb.tally("update.log");
    sb.write_and_commit(
        "skills/scanner/SKILL.md",
        &format!(
            "---\ndescription: scanner\ninstall: {install}\nupdate: {update}\n---\n# scanner\n"
        ),
    );

    assert!(sb.mind(&["meld", &sb.source_spec()]).success);
    assert!(
        sb.mind(&[
            "learn",
            "scanner",
            "--accept-hooks=install,update,uninstall"
        ])
        .success
    );
    assert_eq!(sb.runs("install.log"), 1);
    assert_eq!(sb.runs("update.log"), 0);

    // Same item, same name, already installed: nothing is installed again.
    let r = sb.mind(&[
        "learn",
        "scanner",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "re-learn: {}\n{}", r.stdout, r.stderr);
    assert!(
        r.stdout.contains("already installed"),
        "a repeat learn is a no-op: {}",
        r.stdout
    );
    assert_eq!(
        sb.runs("update.log"),
        0,
        "a no-op learn runs no update hook: {}",
        r.stdout
    );
    assert_eq!(
        sb.runs("install.log"),
        1,
        "a no-op learn re-runs no install hook: {}",
        r.stdout
    );
}

// ---------------------------------------------------------------------------
// Where an item declares its hooks (HOOK-130, HOOK-131, HOOK-133)
// ---------------------------------------------------------------------------

/// A skill declares its install hook in its own `SKILL.md` frontmatter, with no
/// root manifest involved, and it runs at `learn`.
#[test]
fn skill_frontmatter_install_hook_runs_at_learn() {
    // spec: HOOK-130
    let sb = Sandbox::new("fm-src");
    sb.write_and_commit(
        "skills/scanner/SKILL.md",
        "---\ndescription: scanner\ninstall: touch installed.sentinel\n---\n# scanner\n",
    );

    assert!(sb.mind(&["meld", &sb.source_spec()]).success);
    let r = sb.mind(&[
        "learn",
        "scanner",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "learn: {}\n{}", r.stdout, r.stderr);
    let store = sb.mind_home.join("store").join("skill").join("scanner");
    assert!(
        store.join("installed.sentinel").exists(),
        "the frontmatter install hook must run in the item's store dir: {}",
        r.stdout
    );
}

/// A skill declares its hooks in a scoped `mind.toml` in its own directory. The
/// manifest is item content: it is copied into the store, and editing it
/// upstream is drift the next `upgrade` picks up.
#[test]
fn item_directory_manifest_hook_runs_and_is_item_content() {
    // spec: HOOK-131 HOOK-133
    let sb = Sandbox::new("manifest-src");
    let install = sb.tally("install.log");
    let update = sb.tally("update.log");
    sb.write_and_commit(
        "skills/scanner/SKILL.md",
        "---\ndescription: scanner\n---\n# scanner\n",
    );
    sb.write_and_commit(
        "skills/scanner/mind.toml",
        &format!("[[hooks]]\nrun = \"{install}\"\nname = \"Set up\"\n"),
    );

    assert!(sb.mind(&["meld", &sb.source_spec()]).success);
    let r = sb.mind(&[
        "learn",
        "scanner",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "learn: {}\n{}", r.stdout, r.stderr);
    assert_eq!(
        sb.runs("install.log"),
        1,
        "the item manifest's install hook must run: {}",
        r.stdout
    );
    let store = sb.mind_home.join("store").join("skill").join("scanner");
    assert!(
        store.join("mind.toml").exists(),
        "the item manifest is copied into the store with the rest of the item"
    );

    // spec: HOOK-133 -- editing the manifest upstream is drift like any other
    // file change, and the upgraded item picks up the new hook set.
    sb.write_and_commit(
        "skills/scanner/mind.toml",
        &format!("[[hooks]]\nrun = \"{update}\"\nevent = \"update\"\n"),
    );
    assert!(sb.mind(&["sync"]).success);
    let r = sb.mind(&[
        "upgrade",
        "--yes",
        "--accept-hooks=install,update,uninstall",
    ]);
    assert!(r.success, "upgrade: {}\n{}", r.stdout, r.stderr);
    assert!(
        r.stdout.contains("upgraded skill:scanner"),
        "an edited item manifest is drift: {}",
        r.stdout
    );
    assert_eq!(
        sb.runs("update.log"),
        1,
        "the newly declared update hook runs on the upgrade: {}",
        r.stdout
    );
    assert_eq!(
        sb.runs("install.log"),
        1,
        "the replaced install hook must not run again: {}",
        r.stdout
    );
}