mind-cli 0.8.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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
//! Integration tests for `mind dump` (DUMP-1 through DUMP-8).
//!
//! Each test drives the real `mind` binary against a hermetic fixture: a local
//! git repo melded by filesystem path, with `MIND_HOME`/`CLAUDE_HOME` pointed
//! at temp dirs. No network.

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 {
    /// Create a sandbox with a source repo that has one skill, one agent, and one rule.
    fn new(name: &str) -> Sandbox {
        Sandbox::build(name, true)
    }

    /// Create a sandbox with a source repo that has no items (pure super-source or registry).
    fn bare(name: &str) -> Sandbox {
        Sandbox::build(name, false)
    }

    fn build(name: &str, with_items: bool) -> Sandbox {
        let n = COUNTER.fetch_add(1, Ordering::SeqCst);
        let base = std::env::temp_dir().join(format!("mind-dump-{}-{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"),
        };

        if with_items {
            write_file(
                &source.join("skills/review/SKILL.md"),
                "---\nname: review\ndescription: Review skill\n---\n# review\n",
            );
            write_file(
                &source.join("agents/dev.md"),
                "---\nname: dev\ndescription: Dev agent\n---\n# dev\n",
            );
            write_file(
                &source.join("rules/style.md"),
                "---\ndescription: Style rule\n---\n# style\n",
            );
        } else {
            write_file(&source.join("README.md"), "# registry\n");
        }

        git_init(&source);
        sb
    }

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

    fn mind(&self, args: &[&str]) -> Run {
        let mut cmd = Command::new(env!("CARGO_BIN_EXE_mind"));
        cmd.args(args)
            .env("MIND_HOME", &self.mind_home)
            .env("CLAUDE_HOME", &self.claude_home)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .stdin(Stdio::null());
        let out = cmd.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(),
        }
    }

    /// Write a file under the source repo and commit it.
    fn write_and_commit(&self, rel: &str, contents: &str) {
        write_file(&self.source.join(rel), contents);
        git_commit(&self.source);
    }
}

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

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

fn git_init(dir: &Path) {
    for args in [
        vec!["-c", "init.defaultBranch=main", "init", "-q"],
        vec!["config", "user.email", "t@t"],
        vec!["config", "user.name", "t"],
        vec!["add", "-A"],
        vec!["commit", "-qm", "initial"],
    ] {
        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:?} in {dir:?}");
    }
}

fn git_commit(dir: &Path) {
    for args in [vec!["add", "-A"], vec!["commit", "-qm", "fixture"]] {
        let _ = Command::new("git")
            .args(&args)
            .current_dir(dir)
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status();
    }
}

/// Create a tag at the current HEAD of `dir`.
fn git_tag(dir: &Path, tag: &str) {
    let status = Command::new("git")
        .args(["tag", tag])
        .current_dir(dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .expect("run git tag");
    assert!(status.success(), "git tag {tag} in {dir:?}");
}

/// Force-move an existing tag to the current HEAD of `dir`.
fn git_move_tag(dir: &Path, tag: &str) {
    let status = Command::new("git")
        .args(["tag", "-f", tag])
        .current_dir(dir)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .expect("run git tag -f");
    assert!(status.success(), "git tag -f {tag} in {dir:?}");
}

// ---------------------------------------------------------------------------
// DUMP-8: empty registry
// ---------------------------------------------------------------------------

#[test]
fn dump_empty_registry_produces_valid_super_source() {
    // spec: DUMP-8 — with no melded sources, `mind dump` emits a valid
    // super-source whose [discover].sources is empty and exits 0.
    let sb = Sandbox::bare("empty");
    // Do not meld anything.
    let r = sb.mind(&["dump"]);
    assert!(
        r.success,
        "dump with no melded sources must exit 0: {} {}",
        r.stdout, r.stderr
    );
    // Output must be valid TOML with [discover].sources.
    let toml_text = &r.stdout;
    assert!(
        toml_text.contains("discover"),
        "must contain discover section: {toml_text}"
    );
    // Parses as a valid MindToml (DUMP-7 / DSC-3 / DSC-30).
    // We can check the TOML is at least parseable structurally by looking for the
    // key markers.
    assert!(
        toml_text.contains("description"),
        "must carry [source].description: {toml_text}"
    );
    // The sources list must be empty: no [[discover.sources]] entries.
    assert!(
        !toml_text.contains("[[discover.sources]]"),
        "no source entries should appear when registry is empty: {toml_text}"
    );
}

// ---------------------------------------------------------------------------
// DUMP-1: stdout by default; --output writes to file
// ---------------------------------------------------------------------------

#[test]
fn dump_writes_to_stdout_by_default() {
    // spec: DUMP-1 — without --output, dump writes to stdout.
    let sb = Sandbox::new("src");
    let meld = sb.mind(&["meld", &sb.source_spec(), "--link-only"]);
    assert!(meld.success, "meld failed: {}", meld.stderr);

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump must succeed: {} {}", r.stdout, r.stderr);
    assert!(!r.stdout.is_empty(), "dump must write to stdout by default");
    assert!(
        r.stdout.contains("discover"),
        "stdout must contain discover section: {}",
        r.stdout
    );
}

#[test]
fn dump_output_flag_writes_to_file() {
    // spec: DUMP-1 — --output <path> writes to the given file.
    let sb = Sandbox::new("src");
    let meld = sb.mind(&["meld", &sb.source_spec(), "--link-only"]);
    assert!(meld.success, "meld failed: {}", meld.stderr);

    let out_path = sb.base.join("dump.toml");
    let out_str = out_path.to_string_lossy().into_owned();
    let r = sb.mind(&["dump", "--output", &out_str]);
    assert!(
        r.success,
        "dump --output must succeed: {} {}",
        r.stdout, r.stderr
    );
    // stdout must be empty (output was redirected).
    assert!(
        r.stdout.is_empty(),
        "stdout must be empty when --output is given: {}",
        r.stdout
    );
    // The file must exist and contain the document.
    let content = std::fs::read_to_string(&out_path).expect("output file must exist");
    assert!(
        content.contains("discover"),
        "output file must contain discover section: {content}"
    );
    assert!(
        content.contains("description"),
        "output file must carry [source].description: {content}"
    );
}

// ---------------------------------------------------------------------------
// DUMP-2: item filtering
// ---------------------------------------------------------------------------

#[test]
fn dump_all_installed_yields_install_true() {
    // spec: DUMP-2 — every offered item installed -> install = true.
    let sb = Sandbox::new("src");
    let meld = sb.mind(&["meld", &sb.source_spec(), "--yes"]);
    assert!(meld.success, "meld failed: {} {}", meld.stdout, meld.stderr);

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);
    assert!(
        r.stdout.contains("install = true"),
        "all items installed must emit install = true: {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("install-items"),
        "must NOT emit install-items when all installed: {}",
        r.stdout
    );
}

#[test]
fn dump_none_installed_yields_install_false() {
    // spec: DUMP-2 — none installed -> install = false; never emit [].
    let sb = Sandbox::new("src");
    // Meld but do not install anything.
    let meld = sb.mind(&["meld", &sb.source_spec(), "--link-only"]);
    assert!(meld.success, "meld failed: {}", meld.stderr);

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);
    assert!(
        r.stdout.contains("install = false"),
        "no items installed must emit install = false: {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("install-items"),
        "must NOT emit install-items when none installed: {}",
        r.stdout
    );
    // install_items = [] must never appear (DUMP-2).
    assert!(
        !r.stdout.contains("install-items = []"),
        "empty install-items must never be emitted: {}",
        r.stdout
    );
}

#[test]
fn dump_proper_subset_yields_install_items() {
    // spec: DUMP-2 DUMP-5 — proper subset installed -> install_items listing
    // exactly those items by bare kind:name.
    let sb = Sandbox::new("src");
    // Install only skill:review (a proper subset of the three offered items).
    let meld = sb.mind(&["meld", &sb.source_spec(), "--link-only"]);
    assert!(meld.success, "meld failed: {}", meld.stderr);
    let learn = sb.mind(&["learn", "skill:review"]);
    assert!(
        learn.success,
        "learn skill:review failed: {} {}",
        learn.stdout, learn.stderr
    );

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);
    assert!(
        r.stdout.contains("install-items"),
        "proper subset must emit install-items: {}",
        r.stdout
    );
    assert!(
        r.stdout.contains("skill:review"),
        "install-items must contain skill:review: {}",
        r.stdout
    );
    // The non-installed items must NOT appear in install-items.
    assert!(
        !r.stdout.contains("agent:dev"),
        "agent:dev (not installed) must not appear in install-items: {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("rule:style"),
        "rule:style (not installed) must not appear in install-items: {}",
        r.stdout
    );
    // install = true must not be emitted alongside install-items.
    assert!(
        !r.stdout.contains("install = true"),
        "install = true must not be emitted with install-items: {}",
        r.stdout
    );
}

// ---------------------------------------------------------------------------
// DUMP-3: --whole-sources
// ---------------------------------------------------------------------------

#[test]
fn dump_whole_sources_always_emits_install_true() {
    // spec: DUMP-3 — --whole-sources emits install = true for every melded
    // source regardless of how many items are installed.
    let sb = Sandbox::new("src");
    // Install only a subset.
    let meld = sb.mind(&["meld", &sb.source_spec(), "--link-only"]);
    assert!(meld.success, "meld failed: {}", meld.stderr);
    let learn = sb.mind(&["learn", "skill:review"]);
    assert!(
        learn.success,
        "learn failed: {} {}",
        learn.stdout, learn.stderr
    );

    let r = sb.mind(&["dump", "--whole-sources"]);
    assert!(
        r.success,
        "dump --whole-sources failed: {} {}",
        r.stdout, r.stderr
    );
    assert!(
        r.stdout.contains("install = true"),
        "--whole-sources must emit install = true: {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("install-items"),
        "--whole-sources must NOT emit install-items: {}",
        r.stdout
    );
}

// ---------------------------------------------------------------------------
// DUMP-4: meld-time settings in each entry
// ---------------------------------------------------------------------------

#[test]
fn dump_emits_alias_when_melded_with_as() {
    // spec: DUMP-4 — the entry carries the `as` prefix from the consumer alias.
    let sb = Sandbox::new("src");
    let meld = sb.mind(&["meld", &sb.source_spec(), "--as", "mypfx", "--link-only"]);
    assert!(meld.success, "meld --as failed: {}", meld.stderr);

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);
    assert!(
        r.stdout.contains("as = \"mypfx\""),
        "emitted entry must carry as = \"mypfx\": {}",
        r.stdout
    );
}

#[test]
fn dump_emits_roots_when_set() {
    // spec: DUMP-4 — the entry carries the scan roots when set at meld time.
    let sb = Sandbox::new("src");
    // Meld with an explicit root (the source dir itself, so the scan still works).
    let meld = sb.mind(&["meld", &sb.source_spec(), "--root", ".", "--link-only"]);
    assert!(
        meld.success,
        "meld --root failed: {} {}",
        meld.stdout, meld.stderr
    );

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);
    assert!(
        r.stdout.contains("roots"),
        "emitted entry must carry roots when set: {}",
        r.stdout
    );
}

// ---------------------------------------------------------------------------
// DUMP-5: install_items are bare kind:name
// ---------------------------------------------------------------------------

#[test]
fn dump_install_items_are_bare_kind_name() {
    // spec: DUMP-5 — items in install_items use bare kind:name; the entry's `as`
    // prefix applies at re-install, not embedded in the ref.
    let sb = Sandbox::new("src");
    let meld = sb.mind(&["meld", &sb.source_spec(), "--as", "pfx", "--link-only"]);
    assert!(meld.success, "meld failed: {}", meld.stderr);
    // Install only skill:review (under the prefixed name pfx-review).
    let learn = sb.mind(&["learn", "skill:pfx-review"]);
    assert!(
        learn.success,
        "learn skill:pfx-review failed: {} {}",
        learn.stdout, learn.stderr
    );

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);
    // install-items must list the BARE name, not the prefixed name.
    assert!(
        r.stdout.contains("skill:review"),
        "install_items must use bare kind:name (skill:review, not pfx-review): {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("skill:pfx-review"),
        "prefixed name must NOT appear in install_items: {}",
        r.stdout
    );
}

// ---------------------------------------------------------------------------
// DUMP-6: dependency items counted
// ---------------------------------------------------------------------------

#[test]
fn dump_dependency_item_is_in_install_items() {
    // spec: DUMP-6 — an item installed as a within-source dependency (via
    // `requires:` or `{{ns:}}` tokens) is part of the installed set and
    // appears in install_items like any other.
    //
    // We build a source where skill:main `requires: agent:dep`, then install
    // only skill:main. The install flow also installs agent:dep as a
    // dependency. The dump must list both in install_items.
    let sb = Sandbox::bare("dep-src");
    sb.write_and_commit(
        "agents/dep.md",
        "---\nname: dep\ndescription: Dependency agent\n---\n# dep\n",
    );
    sb.write_and_commit(
        "skills/main/SKILL.md",
        "---\nname: main\ndescription: Main skill\nrequires: agent:dep\n---\n# main\n",
    );

    let meld = sb.mind(&["meld", &sb.source_spec(), "--link-only"]);
    assert!(meld.success, "meld failed: {}", meld.stderr);
    // Install main (which pulls in dep as a dependency).
    let learn = sb.mind(&["learn", "skill:main", "--yes"]);
    assert!(
        learn.success,
        "learn skill:main failed: {} {}",
        learn.stdout, learn.stderr
    );

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);
    // The dump must NOT emit install = true (not all items installed if the
    // source has two and only two are installed but only if they differ).
    // Since both are installed and both are offered, either install=true or
    // install_items listing both is valid. Check both appear.
    let has_both_main_and_dep = r.stdout.contains("skill:main") && r.stdout.contains("agent:dep");
    let has_install_true = r.stdout.contains("install = true");
    assert!(
        has_both_main_and_dep || has_install_true,
        "dump must account for the dependency item (agent:dep): {}",
        r.stdout
    );
}

// ---------------------------------------------------------------------------
// DUMP-7: emitted file is a valid super-source
// ---------------------------------------------------------------------------

#[test]
fn dump_output_is_valid_super_source() {
    // spec: DUMP-7 — the emitted file parses as a valid super-source
    // (DSC-3, DSC-30 deny_unknown_fields) with [source].description.
    let sb = Sandbox::new("src");
    let meld = sb.mind(&["meld", &sb.source_spec(), "--yes"]);
    assert!(meld.success, "meld failed: {} {}", meld.stdout, meld.stderr);

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);

    // Write the stdout to a temp file and meld it to verify it parses.
    let dump_path = sb.base.join("dumped.toml");
    std::fs::write(&dump_path, &r.stdout).expect("write dump output");

    // Verify structure manually since we cannot run `toml::from_str` from
    // an integration test directly. We assert the structural markers.
    let text = &r.stdout;
    assert!(
        text.contains("description"),
        "must have [source].description: {text}"
    );
    assert!(
        text.contains("discover"),
        "must have discover section: {text}"
    );
    assert!(
        !text.contains("[[items]]"),
        "must NOT have [[items]] of its own: {text}"
    );
}

// ---------------------------------------------------------------------------
// Round-trip: dump -> re-meld reproduces the same install set
// ---------------------------------------------------------------------------

#[test]
fn dump_roundtrip_remeld_reproduces_install_set() {
    // spec: DUMP-1 DUMP-2 DUMP-5 DUMP-7 — meld a source + install a subset,
    // dump to a file inside a new git repo (super-source), meld THAT repo into
    // a fresh environment, and assert the manifest reproduces the same install set.
    //
    // The dumped super-source references the original source by its filesystem
    // path, which still exists in the test, so the re-meld can clone it.

    let src = Sandbox::new("original-src");
    // Install only skill:review (a proper subset of the three offered items).
    let meld = src.mind(&["meld", &src.source_spec(), "--link-only"]);
    assert!(meld.success, "initial meld failed: {}", meld.stderr);
    let learn = src.mind(&["learn", "skill:review"]);
    assert!(
        learn.success,
        "learn failed: {} {}",
        learn.stdout, learn.stderr
    );

    // Dump to a temp directory as mind.toml (the super-source repo).
    let n = COUNTER.fetch_add(1, Ordering::SeqCst);
    let super_dir = src.base.join(format!("super-{n}"));
    std::fs::create_dir_all(&super_dir).expect("create super-source dir");
    let dump_path = super_dir.join("mind.toml");
    let dump_path_str = dump_path.to_string_lossy().into_owned();
    let dump_r = src.mind(&["dump", "--output", &dump_path_str]);
    assert!(
        dump_r.success,
        "dump --output failed: {} {}",
        dump_r.stdout, dump_r.stderr
    );
    assert!(dump_path.exists(), "dump output file must exist");

    // The super-source dir needs to be a git repo so `meld` can clone it.
    git_init(&super_dir);

    let super_spec = super_dir.to_string_lossy().into_owned();

    // Set up a second, fresh environment and meld the dumped super-source.
    let fresh_base = src.base.join(format!("fresh-{n}"));
    std::fs::create_dir_all(&fresh_base).expect("create fresh base");
    let fresh_mind_home = fresh_base.join("mind");
    let fresh_claude_home = fresh_base.join("claude");

    let remeld = Command::new(env!("CARGO_BIN_EXE_mind"))
        .args(["meld", &super_spec, "--yes"])
        .env("MIND_HOME", &fresh_mind_home)
        .env("CLAUDE_HOME", &fresh_claude_home)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .stdin(Stdio::null())
        .output()
        .expect("run mind meld on dumped super-source");

    let remeld_out = String::from_utf8_lossy(&remeld.stdout).into_owned();
    let remeld_err = String::from_utf8_lossy(&remeld.stderr).into_owned();

    // The re-meld should succeed and install exactly skill:review.
    assert!(
        remeld.status.success(),
        "re-meld of dumped super-source failed: {remeld_out} {remeld_err}"
    );

    // skill:review must be installed in the fresh environment.
    assert!(
        fresh_claude_home.join("skills/review").exists(),
        "skill:review must be installed in the reproduced environment: {:?}",
        fresh_claude_home
    );

    // agent:dev and rule:style must NOT be installed (they were not in the subset).
    assert!(
        !fresh_claude_home.join("agents/dev.md").exists(),
        "agent:dev (not in the subset) must NOT be installed in the reproduced environment"
    );
    assert!(
        !fresh_claude_home.join("rules/style.md").exists(),
        "rule:style (not in the subset) must NOT be installed in the reproduced environment"
    );
}

// ---------------------------------------------------------------------------
// DUMP-1 / DSC-65: pin-ref in the dump output pins to the exact commit
// ---------------------------------------------------------------------------

#[test]
fn dump_pin_ref_pins_to_exact_commit_not_new_head() {
    // spec: DUMP-1 DUMP-4 DSC-65 — dump emits `pin-ref = <commit>` for each
    // source. When the dump output is re-melded, the reproduced source sits at
    // the DUMPED commit, not the new HEAD that was added after the dump.
    // This proves the pin-ref is load-bearing for exact-revision reproduction.
    let src = Sandbox::new("evolving-src");
    // Install all items.
    let meld = src.mind(&["meld", &src.source_spec(), "--yes"]);
    assert!(meld.success, "initial meld failed: {}", meld.stderr);

    // Dump to record the current commit.
    let n = COUNTER.fetch_add(1, Ordering::SeqCst);
    let super_dir = src.base.join(format!("pinref-super-{n}"));
    std::fs::create_dir_all(&super_dir).expect("create super-source dir");
    let dump_path = super_dir.join("mind.toml");
    let dump_path_str = dump_path.to_string_lossy().into_owned();
    let dump_r = src.mind(&["dump", "--output", &dump_path_str]);
    assert!(
        dump_r.success,
        "dump --output failed: {} {}",
        dump_r.stdout, dump_r.stderr
    );

    // Verify the dump output contains `pin-ref` with the recorded commit.
    let dump_text = std::fs::read_to_string(&dump_path).expect("read dump output");
    assert!(
        dump_text.contains("pin-ref"),
        "dump output must contain pin-ref: {dump_text}"
    );

    // Advance the source by adding a new commit AFTER the dump.
    src.write_and_commit(
        "skills/new-skill/SKILL.md",
        "---\nname: new-skill\ndescription: New skill added after dump\n---\n# new\n",
    );

    // The super-source dir needs to be a git repo so `meld` can clone it.
    git_init(&super_dir);
    let super_spec = super_dir.to_string_lossy().into_owned();

    // Set up a fresh environment and meld the dumped super-source.
    let fresh_base = src.base.join(format!("pinref-fresh-{n}"));
    std::fs::create_dir_all(&fresh_base).expect("create fresh base");
    let fresh_mind_home = fresh_base.join("mind");
    let fresh_claude_home = fresh_base.join("claude");

    let remeld = Command::new(env!("CARGO_BIN_EXE_mind"))
        .args(["meld", &super_spec, "--yes"])
        .env("MIND_HOME", &fresh_mind_home)
        .env("CLAUDE_HOME", &fresh_claude_home)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .stdin(Stdio::null())
        .output()
        .expect("run mind meld on dumped super-source");

    let remeld_out = String::from_utf8_lossy(&remeld.stdout).into_owned();
    let remeld_err = String::from_utf8_lossy(&remeld.stderr).into_owned();
    assert!(
        remeld.status.success(),
        "re-meld of dumped super-source failed: {remeld_out} {remeld_err}"
    );

    // The new skill (added AFTER the dump) must NOT be installed in the
    // reproduced environment, because the pin-ref pins to the pre-advance commit.
    assert!(
        !fresh_claude_home.join("skills/new-skill").exists(),
        "skill added after dump must NOT appear in the reproduced environment \
         (pin-ref must pin to the pre-advance commit): {:?}",
        fresh_claude_home
    );

    // The pre-dump items must be present.
    assert!(
        fresh_claude_home.join("skills/review").exists(),
        "original skill:review must be installed in the reproduced environment"
    );

    // Verify the dump output contains the source URL (no github shorthands).
    assert!(
        dump_text.contains(&src.source_spec()),
        "dump must reference the source by its path: {dump_text}"
    );
    assert!(
        !dump_text.contains("github.com"),
        "dump must use local path (no github URL in this test): {dump_text}"
    );
}

// ---------------------------------------------------------------------------
// DUMP-2 boundary: "all offered installed" is decided by the offered SET, not a
// count. A source offering 3 items with only 2 installed must emit install-items
// of exactly those 2, NOT install = true.
// ---------------------------------------------------------------------------

#[test]
fn dump_two_of_three_installed_yields_subset_not_install_true() {
    // spec: DUMP-2 — the "all offered installed" branch keys on the offered set.
    // With 3 items offered and 2 installed, the result is a proper subset:
    // install-items listing exactly the 2, and never install = true.
    let sb = Sandbox::new("src"); // offers skill:review, agent:dev, rule:style
    let meld = sb.mind(&["meld", &sb.source_spec(), "--link-only"]);
    assert!(meld.success, "meld failed: {}", meld.stderr);
    let l1 = sb.mind(&["learn", "skill:review"]);
    assert!(
        l1.success,
        "learn skill:review: {} {}",
        l1.stdout, l1.stderr
    );
    let l2 = sb.mind(&["learn", "agent:dev"]);
    assert!(l2.success, "learn agent:dev: {} {}", l2.stdout, l2.stderr);

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);
    assert!(
        r.stdout.contains("install-items"),
        "two-of-three installed must emit install-items (not install = true): {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("install = true"),
        "must NOT emit install = true when only a subset is installed: {}",
        r.stdout
    );
    assert!(
        r.stdout.contains("skill:review") && r.stdout.contains("agent:dev"),
        "install-items must list exactly the two installed items: {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("rule:style"),
        "the un-installed rule:style must not appear in install-items: {}",
        r.stdout
    );
}

#[test]
fn dump_all_of_a_two_item_source_yields_install_true() {
    // spec: DUMP-2 — a source whose every offered item is installed yields
    // install = true regardless of how many items that is. Builds a source with
    // exactly two items and installs both; the offered set equals the installed
    // set, so install = true (not an install-items listing both).
    let sb = Sandbox::bare("two-item");
    sb.write_and_commit(
        "skills/alpha/SKILL.md",
        "---\nname: alpha\ndescription: Alpha skill\n---\n# alpha\n",
    );
    sb.write_and_commit(
        "agents/beta.md",
        "---\nname: beta\ndescription: Beta agent\n---\n# beta\n",
    );
    let meld = sb.mind(&["meld", &sb.source_spec(), "--yes"]);
    assert!(meld.success, "meld failed: {} {}", meld.stdout, meld.stderr);

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);
    assert!(
        r.stdout.contains("install = true"),
        "all offered items installed must emit install = true: {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("install-items"),
        "must NOT emit install-items when the offered set is fully installed: {}",
        r.stdout
    );
}

#[test]
fn dump_install_true_when_all_currently_offered_installed_despite_stale_manifest_item() {
    // spec: DUMP-2 — the directive intersects the manifest with what the catalog
    // currently OFFERS. An item recorded in the manifest but no longer offered by
    // the source is excluded from the comparison, so if every currently-offered
    // item is installed the result is install = true (not an install-items list
    // that would name the stale item). Uses a linked local source so the catalog
    // scans the live working tree: removing an item from the tree drops it from
    // the offered set while it remains in the manifest.
    let sb = Sandbox::new("stale-src"); // offers review, dev, style
    let meld = sb.mind(&["meld", &sb.source_spec(), "--yes"]);
    assert!(meld.success, "meld failed: {} {}", meld.stdout, meld.stderr);

    // Remove rule:style from the source's working tree. It stays in the manifest
    // (still "installed") but is no longer offered by the catalog.
    std::fs::remove_file(sb.source.join("rules/style.md")).expect("remove rule file");

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);
    // review + dev are the only offered items now, and both are installed, so the
    // offered set == the installed-and-offered set -> install = true.
    assert!(
        r.stdout.contains("install = true"),
        "all currently-offered items installed must yield install = true even with a stale manifest item: {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("install-items"),
        "a stale (no-longer-offered) manifest item must not force an install-items list: {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("rule:style"),
        "the no-longer-offered item must not appear in the dump: {}",
        r.stdout
    );
}

// ---------------------------------------------------------------------------
// Multiple sources in one dump: a 2-source registry with distinct directives
// (one full, one subset, one none) must all be emitted correctly in one dump.
// ---------------------------------------------------------------------------

#[test]
fn dump_multiple_sources_each_with_distinct_directive() {
    // spec: DUMP-1 DUMP-2 — a registry with three distinct sources, each with a
    // different install state, all emitted in one dump:
    //   full    -> install = true
    //   subset  -> install-items = [one of two]
    //   none    -> install = false
    let full = Sandbox::bare("full-src");
    full.write_and_commit(
        "skills/only/SKILL.md",
        "---\nname: only\ndescription: Only skill\n---\n# only\n",
    );
    // Subset and none sources are melded into the SAME mind/claude home as `full`,
    // so a single dump sees all three. Build them as plain repos here.
    let subset = Sandbox::bare("subset-src");
    subset.write_and_commit(
        "skills/keep/SKILL.md",
        "---\nname: keep\ndescription: Keep skill\n---\n# keep\n",
    );
    subset.write_and_commit(
        "agents/drop.md",
        "---\nname: drop\ndescription: Drop agent\n---\n# drop\n",
    );
    let none = Sandbox::bare("none-src");
    none.write_and_commit(
        "rules/unused.md",
        "---\ndescription: Unused rule\n---\n# unused\n",
    );

    // Meld all three into `full`'s home.
    let m1 = full.mind(&["meld", &full.source_spec(), "--yes"]);
    assert!(m1.success, "meld full failed: {} {}", m1.stdout, m1.stderr);
    let m2 = full.mind(&["meld", &subset.source_spec(), "--link-only"]);
    assert!(
        m2.success,
        "meld subset failed: {} {}",
        m2.stdout, m2.stderr
    );
    let m3 = full.mind(&["meld", &none.source_spec(), "--link-only"]);
    assert!(m3.success, "meld none failed: {} {}", m3.stdout, m3.stderr);
    // Install exactly one of subset-src's two items.
    let l = full.mind(&["learn", "skill:keep"]);
    assert!(
        l.success,
        "learn skill:keep failed: {} {}",
        l.stdout, l.stderr
    );

    let r = full.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);

    // All three sources must appear in the one dump.
    assert!(
        r.stdout.contains(&full.source_spec())
            && r.stdout.contains(&subset.source_spec())
            && r.stdout.contains(&none.source_spec()),
        "all three source specs must appear in the dump: {}",
        r.stdout
    );
    // The full source -> install = true; the subset -> install-items[keep];
    // the none -> install = false. Both true and false must coexist, and a
    // subset listing keep (not drop) must be present.
    assert!(
        r.stdout.contains("install = true"),
        "the fully-installed source must emit install = true: {}",
        r.stdout
    );
    assert!(
        r.stdout.contains("install = false"),
        "the un-installed source must emit install = false: {}",
        r.stdout
    );
    assert!(
        r.stdout.contains("skill:keep"),
        "the subset source must list skill:keep in install-items: {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("agent:drop"),
        "the un-installed agent:drop must not appear in install-items: {}",
        r.stdout
    );
}

// ---------------------------------------------------------------------------
// DUMP-4 prefix: `as` from a source's OWN [source].prefix when no consumer alias.
// ---------------------------------------------------------------------------

#[test]
fn dump_emits_alias_from_source_own_prefix_when_no_consumer_alias() {
    // spec: DUMP-4 — the emitted `as` comes from the source's own
    // [source].prefix when the consumer did not pass --as. (The alias path is
    // covered separately; this covers the [source].prefix fallback.)
    let sb = Sandbox::bare("prefixed-src");
    sb.write_and_commit("mind.toml", "[source]\nprefix = \"sp\"\n");
    sb.write_and_commit(
        "skills/widget/SKILL.md",
        "---\nname: widget\ndescription: Widget skill\n---\n# widget\n",
    );
    // Meld with NO --as, so the source's own prefix is the only prefix in effect.
    let meld = sb.mind(&["meld", &sb.source_spec(), "--link-only"]);
    assert!(meld.success, "meld failed: {} {}", meld.stdout, meld.stderr);

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);
    assert!(
        r.stdout.contains("as = \"sp\""),
        "dump must emit as = \"sp\" from the source's own [source].prefix: {}",
        r.stdout
    );
}

// ---------------------------------------------------------------------------
// pin-ref round-trip across pin kinds: a tag- or branch-pinned source still
// dumps as pin-ref = <exact commit>, and re-melding lands on that exact commit.
// ---------------------------------------------------------------------------

#[test]
fn dump_tag_pinned_source_dumps_exact_commit_pin_ref() {
    // spec: DUMP-1 DUMP-4 DSC-65 — a source melded with --pin-tag is dumped as
    // pin-ref = <the commit the tag resolved to>, not as pin-tag. Re-melding the
    // dump output pins to that exact commit even after the tag is later moved.
    let sb = Sandbox::new("tagged-src");
    // Tag the current commit, then meld at that tag.
    git_tag(&sb.source, "v1");
    let meld = sb.mind(&["meld", &sb.source_spec(), "--pin-tag", "v1", "--yes"]);
    assert!(
        meld.success,
        "meld --pin-tag failed: {} {}",
        meld.stdout, meld.stderr
    );

    // Dump.
    let n = COUNTER.fetch_add(1, Ordering::SeqCst);
    let super_dir = sb.base.join(format!("tag-super-{n}"));
    std::fs::create_dir_all(&super_dir).expect("create super dir");
    let dump_path = super_dir.join("mind.toml");
    let dump_path_str = dump_path.to_string_lossy().into_owned();
    let dr = sb.mind(&["dump", "--output", &dump_path_str]);
    assert!(dr.success, "dump failed: {} {}", dr.stdout, dr.stderr);

    let dump_text = std::fs::read_to_string(&dump_path).expect("read dump");
    assert!(
        dump_text.contains("pin-ref"),
        "tag-pinned source must dump as pin-ref (exact commit): {dump_text}"
    );
    assert!(
        !dump_text.contains("pin-tag"),
        "dump must NOT carry the original pin-tag kind: {dump_text}"
    );

    // Advance the source AND move the tag forward, then re-meld the dump.
    sb.write_and_commit(
        "skills/added-after/SKILL.md",
        "---\nname: added-after\ndescription: Added after dump\n---\n# a\n",
    );
    // Move v1 to the new tip; a pin-ref dump must ignore this and stay at the
    // pre-advance commit.
    git_move_tag(&sb.source, "v1");

    git_init(&super_dir);
    let super_spec = super_dir.to_string_lossy().into_owned();
    let fresh_base = sb.base.join(format!("tag-fresh-{n}"));
    std::fs::create_dir_all(&fresh_base).expect("fresh base");
    let fresh_mind = fresh_base.join("mind");
    let fresh_claude = fresh_base.join("claude");

    let remeld = Command::new(env!("CARGO_BIN_EXE_mind"))
        .args(["meld", &super_spec, "--yes"])
        .env("MIND_HOME", &fresh_mind)
        .env("CLAUDE_HOME", &fresh_claude)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .stdin(Stdio::null())
        .output()
        .expect("run remeld");
    let ro = String::from_utf8_lossy(&remeld.stdout).into_owned();
    let re = String::from_utf8_lossy(&remeld.stderr).into_owned();
    assert!(remeld.status.success(), "remeld failed: {ro} {re}");

    // The item added after the dump (and after the tag was moved) must NOT be
    // present: the pin-ref pins to the exact pre-advance commit.
    assert!(
        !fresh_claude.join("skills/added-after").exists(),
        "an item added after the dump must NOT appear (pin-ref must pin exactly): {fresh_claude:?}"
    );
    assert!(
        fresh_claude.join("skills/review").exists(),
        "the original skill:review must be present in the reproduced env"
    );
}

#[test]
fn dump_branch_followed_source_dumps_exact_commit_pin_ref() {
    // spec: DUMP-1 DUMP-4 DSC-65 — a source melded with --follow-branch is
    // dumped as pin-ref = <recorded commit>, not as follow-branch. Re-melding
    // lands on the exact commit even after the branch advances.
    let sb = Sandbox::new("branch-src");
    let meld = sb.mind(&[
        "meld",
        &sb.source_spec(),
        "--follow-branch",
        "main",
        "--yes",
    ]);
    assert!(
        meld.success,
        "meld --follow-branch failed: {} {}",
        meld.stdout, meld.stderr
    );

    let n = COUNTER.fetch_add(1, Ordering::SeqCst);
    let super_dir = sb.base.join(format!("branch-super-{n}"));
    std::fs::create_dir_all(&super_dir).expect("create super dir");
    let dump_path = super_dir.join("mind.toml");
    let dump_path_str = dump_path.to_string_lossy().into_owned();
    let dr = sb.mind(&["dump", "--output", &dump_path_str]);
    assert!(dr.success, "dump failed: {} {}", dr.stdout, dr.stderr);

    let dump_text = std::fs::read_to_string(&dump_path).expect("read dump");
    assert!(
        dump_text.contains("pin-ref"),
        "branch-followed source must dump as pin-ref (exact commit): {dump_text}"
    );
    assert!(
        !dump_text.contains("follow-branch"),
        "dump must NOT carry the original follow-branch kind: {dump_text}"
    );

    // Advance main, then re-meld; the pin-ref must keep the reproduction at the
    // recorded commit, not the new branch tip.
    sb.write_and_commit(
        "skills/late/SKILL.md",
        "---\nname: late\ndescription: Late skill\n---\n# late\n",
    );

    git_init(&super_dir);
    let super_spec = super_dir.to_string_lossy().into_owned();
    let fresh_base = sb.base.join(format!("branch-fresh-{n}"));
    std::fs::create_dir_all(&fresh_base).expect("fresh base");
    let fresh_mind = fresh_base.join("mind");
    let fresh_claude = fresh_base.join("claude");

    let remeld = Command::new(env!("CARGO_BIN_EXE_mind"))
        .args(["meld", &super_spec, "--yes"])
        .env("MIND_HOME", &fresh_mind)
        .env("CLAUDE_HOME", &fresh_claude)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .stdin(Stdio::null())
        .output()
        .expect("run remeld");
    let ro = String::from_utf8_lossy(&remeld.stdout).into_owned();
    let re = String::from_utf8_lossy(&remeld.stderr).into_owned();
    assert!(remeld.status.success(), "remeld failed: {ro} {re}");

    assert!(
        !fresh_claude.join("skills/late").exists(),
        "a skill added after the dump must NOT appear (pin-ref pins exactly): {fresh_claude:?}"
    );
    assert!(
        fresh_claude.join("skills/review").exists(),
        "the original skill:review must be present in the reproduced env"
    );
}

// ---------------------------------------------------------------------------
// Output parity: --output file and stdout produce byte-identical valid TOML.
// ---------------------------------------------------------------------------

#[test]
fn dump_stdout_and_output_file_are_byte_identical() {
    // spec: DUMP-1 DUMP-7 — the document written to --output must be byte-for-byte
    // the same as what dump writes to stdout for the same state.
    let sb = Sandbox::new("src");
    let meld = sb.mind(&["meld", &sb.source_spec(), "--link-only"]);
    assert!(meld.success, "meld failed: {}", meld.stderr);
    let learn = sb.mind(&["learn", "skill:review"]);
    assert!(
        learn.success,
        "learn failed: {} {}",
        learn.stdout, learn.stderr
    );

    let stdout_run = sb.mind(&["dump"]);
    assert!(
        stdout_run.success,
        "dump stdout failed: {}",
        stdout_run.stderr
    );

    let out_path = sb.base.join("parity.toml");
    let out_str = out_path.to_string_lossy().into_owned();
    let file_run = sb.mind(&["dump", "--output", &out_str]);
    assert!(
        file_run.success,
        "dump --output failed: {}",
        file_run.stderr
    );
    let file_content = std::fs::read_to_string(&out_path).expect("read output file");

    assert_eq!(
        stdout_run.stdout, file_content,
        "stdout and --output must be byte-identical:\n--- stdout ---\n{}\n--- file ---\n{}",
        stdout_run.stdout, file_content
    );
    assert!(
        !file_content.is_empty(),
        "the parity content must not be empty"
    );
}

// ---------------------------------------------------------------------------
// DUMP-8 / no-commit: a linked local source with no recorded commit emits NO
// pin (must never emit pin-ref = "").
// ---------------------------------------------------------------------------

#[test]
fn dump_linked_local_source_without_commit_emits_no_pin_ref() {
    // spec: DUMP-1 DUMP-8 — a linked local source that is not a git repo records
    // no commit. The dump must omit the pin field entirely, never emit
    // pin-ref = "".
    let n = COUNTER.fetch_add(1, Ordering::SeqCst);
    let base = std::env::temp_dir().join(format!("mind-dump-nogit-{}-{n}", std::process::id()));
    let _ = std::fs::remove_dir_all(&base);
    let nongit_src = base.join("nongit");
    // A plain (non-git) directory with one item; meld links it (no clone, no
    // recorded commit because head_commit fails on a non-repo).
    write_file(
        &nongit_src.join("skills/plain/SKILL.md"),
        "---\nname: plain\ndescription: Plain skill\n---\n# plain\n",
    );
    let mind_home = base.join("mind");
    let claude_home = base.join("claude");

    let run = |args: &[&str]| -> Run {
        let out = Command::new(env!("CARGO_BIN_EXE_mind"))
            .args(args)
            .env("MIND_HOME", &mind_home)
            .env("CLAUDE_HOME", &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(),
        }
    };

    let nongit_spec = nongit_src.to_string_lossy().into_owned();
    let meld = run(&["meld", &nongit_spec, "--link-only"]);
    assert!(
        meld.success,
        "meld of a non-git local dir must succeed: {} {}",
        meld.stdout, meld.stderr
    );

    let dump = run(&["dump"]);
    assert!(dump.success, "dump failed: {} {}", dump.stdout, dump.stderr);
    assert!(
        !dump.stdout.contains("pin-ref"),
        "a source with no recorded commit must emit no pin-ref: {}",
        dump.stdout
    );
    assert!(
        !dump.stdout.contains("pin-ref = \"\""),
        "must never emit an empty pin-ref: {}",
        dump.stdout
    );
    // The source must still be present in the dump (referenced), just unpinned.
    assert!(
        dump.stdout.contains(&nongit_spec),
        "the source must still be referenced in the dump: {}",
        dump.stdout
    );

    let _ = std::fs::remove_dir_all(&base);
}

// ---------------------------------------------------------------------------
// DUMP-6: a dependency installed via a {{ns:}} token (not `requires:`) is part
// of the installed set and appears in install-items.
// ---------------------------------------------------------------------------

#[test]
fn dump_token_dependency_item_is_in_install_items() {
    // spec: DUMP-6 — an item pulled in only as a within-source dependency via a
    // {{ns:}} reference token is in the installed set and is dumped like any
    // other. Build a source where skill:caller references {{ns:helper}}; install
    // only skill:caller and confirm agent:helper rides along into the dump.
    let sb = Sandbox::bare("tok-dep");
    sb.write_and_commit(
        "agents/helper.md",
        "---\nname: helper\ndescription: Helper agent\n---\n# helper\n",
    );
    // A third item that is NOT installed, so the source is a proper superset of
    // the installed set and the dump must use install-items (not install = true).
    sb.write_and_commit(
        "rules/extra.md",
        "---\ndescription: Extra rule\n---\n# extra\n",
    );
    sb.write_and_commit(
        "skills/caller/SKILL.md",
        "---\nname: caller\ndescription: Caller skill\n---\n# caller\n\nSee {{ns:helper}}.\n",
    );

    let meld = sb.mind(&["meld", &sb.source_spec(), "--link-only"]);
    assert!(meld.success, "meld failed: {} {}", meld.stdout, meld.stderr);
    let learn = sb.mind(&["learn", "skill:caller", "--yes"]);
    assert!(
        learn.success,
        "learn skill:caller failed: {} {}",
        learn.stdout, learn.stderr
    );

    let r = sb.mind(&["dump"]);
    assert!(r.success, "dump failed: {} {}", r.stdout, r.stderr);

    // DEP-1/DEP-3: the {{ns:helper}} token makes agent:helper a dependency of
    // skill:caller, so learning the caller installs the helper too.
    assert!(
        sb.claude_home.join("agents/helper.md").exists(),
        "the {{{{ns:helper}}}} token must pull agent:helper in as a dependency"
    );
    // Both caller and helper are installed (a proper subset, since rule:extra is
    // not), so the dump lists both in install-items by bare kind:name.
    assert!(
        r.stdout.contains("agent:helper") && r.stdout.contains("skill:caller"),
        "a token-pulled dependency must appear in install-items alongside its referrer: {}",
        r.stdout
    );
    assert!(
        !r.stdout.contains("rule:extra"),
        "the un-installed rule:extra must not appear in install-items: {}",
        r.stdout
    );
}

// ---------------------------------------------------------------------------
// Lock-mode: dump takes the Shared lock (read-only)
// ---------------------------------------------------------------------------

#[test]
fn dump_is_classified_as_shared_lock() {
    // spec: DUMP-1 — dump is read-only (registry + manifest + catalog only);
    // verify via the `lock_mode` unit-test path in main.rs that dump is Shared.
    // Here we verify at the CLI level that `mind dump --help` succeeds (i.e.
    // the command parses) and that the binary recognizes the subcommand.
    let sb = Sandbox::bare("lock-test");
    let help = Command::new(env!("CARGO_BIN_EXE_mind"))
        .args(["dump", "--help"])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .expect("run mind dump --help");
    assert!(
        help.status.success(),
        "`mind dump --help` must exit 0: {}",
        String::from_utf8_lossy(&help.stderr)
    );
    let help_out = String::from_utf8_lossy(&help.stdout).into_owned();
    assert!(
        help_out.contains("--output") || help_out.contains("--whole-sources"),
        "help text must mention dump flags: {help_out}"
    );

    // Confirm `mind dump` on an empty home exits 0 (DUMP-8).
    let r = sb.mind(&["dump"]);
    assert!(
        r.success,
        "`mind dump` with empty home must exit 0: {} {}",
        r.stdout, r.stderr
    );
    drop(sb);
}