mind-cli 0.7.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
//! Implementation of `mind dump` (DUMP-1 through DUMP-8).
//!
//! Writes a super-source `mind.toml` that reproduces the current melded and
//! installed state, so melding the output recreates the same source set and
//! install selection.

use std::collections::{BTreeSet, HashMap};
use std::io::Write;
use std::path::PathBuf;

use serde::Serialize;

use crate::catalog;
use crate::error::{MindError, Result};
use crate::manifest::Manifest;
use crate::mindfile::MindToml;
use crate::paths::Paths;
use crate::source::{Registry, Source};

// ---------------------------------------------------------------------------
// Serializable shapes that round-trip through `toml::from_str::<MindToml>`.
// Field names match the serde renames in mindfile.rs exactly (deny_unknown_fields).
// ---------------------------------------------------------------------------

/// Top-level emitted document.
#[derive(Serialize)]
struct DumpDoc {
    source: DumpSource,
    discover: DumpDiscover,
}

/// `[source]` section of the emitted document.
#[derive(Serialize)]
struct DumpSource {
    description: String,
}

/// `[discover]` section of the emitted document.
#[derive(Serialize)]
struct DumpDiscover {
    sources: Vec<DumpEntry>,
}

/// One `[[discover.sources]]` entry in the emitted document.
///
/// Fields must match the serde renames of `mindfile::NestedSource` exactly so
/// `toml::from_str::<MindToml>` accepts the output (DUMP-7, DSC-30).
///
/// DUMP-1 / DUMP-4 / DSC-65: the pin is emitted as `pin-ref = <commit>` for
/// every source with a recorded commit, regardless of the source's pin kind.
/// This is the authoritative per-entry pin (DSC-65) that re-melding the output
/// will apply unconditionally. A source with no recorded commit (e.g. a
/// never-synced linked local source) emits no pin field.
#[derive(Serialize)]
struct DumpEntry {
    source: String,

    #[serde(rename = "as", skip_serializing_if = "Option::is_none")]
    alias: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    roots: Option<Vec<String>>,

    /// Pin: emit the recorded commit sha as `pin-ref` (DUMP-1, DUMP-4, DSC-65).
    /// Absent when no commit has been recorded for this source.
    #[serde(rename = "pin-ref", skip_serializing_if = "Option::is_none")]
    pin_ref: Option<String>,

    // Exactly one of these two is emitted per entry (never both, never neither).
    // When install_items is Some the install bool is absent; when install_items
    // is None the bool is emitted. The filtering logic guarantees mutual
    // exclusion.
    #[serde(skip_serializing_if = "Option::is_none")]
    install: Option<bool>,

    #[serde(rename = "install-items", skip_serializing_if = "Option::is_none")]
    install_items: Option<Vec<String>>,
}

// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------

/// Run `mind dump`.
///
/// - `paths` - resolved paths for this invocation
/// - `output` - write to this file instead of stdout when `Some`
/// - `whole_sources` - when true, emit `install = true` for every source
///   regardless of what is actually installed (DUMP-3)
pub fn run(paths: &Paths, output: Option<PathBuf>, whole_sources: bool) -> Result<()> {
    // spec: DUMP-1 DUMP-2 DUMP-3 DUMP-4 DUMP-5 DUMP-6 DUMP-7 DUMP-8

    let registry = Registry::load(paths)?;
    let manifest = Manifest::load(paths)?;

    // spec: DUMP-8 — with no melded sources, emit a valid super-source whose
    // [discover].sources is empty, and exit 0.
    let entries = build_entries(paths, &registry, &manifest, whole_sources)?;

    let doc = DumpDoc {
        source: DumpSource {
            description: "Generated by `mind dump`. Meld this file to reproduce the recorded \
                          source set and install selection."
                .to_string(),
        },
        discover: DumpDiscover { sources: entries },
    };

    let text = toml::to_string(&doc).map_err(|e| MindError::TomlWrite {
        path: output.clone().unwrap_or_else(|| PathBuf::from("<stdout>")),
        source: e,
    })?;

    match output {
        None => {
            // spec: DUMP-1 — stdout by default
            let stdout = std::io::stdout();
            let mut out = stdout.lock();
            out.write_all(text.as_bytes())
                .and_then(|_| out.flush())
                .map_err(|e| MindError::io("<stdout>", e))
        }
        Some(path) => {
            // spec: DUMP-1 — --output <path>
            std::fs::write(&path, &text).map_err(|e| MindError::io(&path, e))
        }
    }
}

// ---------------------------------------------------------------------------
// Core logic
// ---------------------------------------------------------------------------

/// Build the `[[discover.sources]]` entry list.
fn build_entries(
    paths: &Paths,
    registry: &Registry,
    manifest: &Manifest,
    whole_sources: bool,
) -> Result<Vec<DumpEntry>> {
    let mut entries = Vec::with_capacity(registry.sources.len());

    for source in &registry.sources {
        let entry = build_entry(paths, source, manifest, whole_sources)?;
        entries.push(entry);
    }

    Ok(entries)
}

/// Build one `[[discover.sources]]` entry for a single melded source.
fn build_entry(
    paths: &Paths,
    source: &Source,
    manifest: &Manifest,
    whole_sources: bool,
) -> Result<DumpEntry> {
    // spec: DUMP-1 — use source URL for remote sources; use URL (path) for local.
    // For local sources `source.url` holds the filesystem path (see parse_spec).
    let source_spec = source.url.clone();

    // spec: DUMP-4 — effective prefix: consumer alias wins over the source's
    // own [source].prefix (the same precedence as catalog::scan_source_at).
    let effective_alias = effective_prefix(paths, source);

    // spec: DUMP-4 — scan roots recorded at meld time (STO-17).
    let roots = source.roots.clone();

    // spec: DUMP-1 / DUMP-4 / DSC-65 — emit the recorded commit sha as `pin-ref`
    // for every source that has one. This is the authoritative per-entry pin
    // (DSC-65): a re-meld of the output will apply it unconditionally, pinning
    // each source to the exact revision recorded at dump time. A source with no
    // recorded commit (e.g. a never-synced linked local source) emits no pin.
    let pin_ref = source.commit.clone();

    // spec: DUMP-2 / DUMP-3 — item filtering.
    let (install, install_items) = if whole_sources {
        // spec: DUMP-3 — --whole-sources: always install = true.
        (Some(true), None)
    } else {
        compute_install_directive(paths, source, manifest)?
    };

    Ok(DumpEntry {
        source: source_spec,
        alias: effective_alias,
        roots,
        pin_ref,
        install,
        install_items,
    })
}

/// Compute the effective namespace prefix for a source, mirroring the
/// precedence used in `catalog::scan_source_at` (DUMP-4).
///
/// Returns `None` when neither the consumer alias nor the source's own
/// `[source].prefix` is set (or both are empty strings).
fn effective_prefix(paths: &Paths, source: &Source) -> Option<String> {
    // Consumer alias wins.
    if let Some(alias) = &source.alias {
        if !alias.is_empty() {
            return Some(alias.clone());
        }
        // An explicit empty alias suppresses the source's own prefix.
        return None;
    }
    // Fall back to the source's own [source].prefix from its mind.toml.
    let clone_root = source.clone_dir(paths);
    if let Ok(Some(mt)) = MindToml::load(&clone_root)
        && let Some(prefix) = mt.source.prefix
        && !prefix.is_empty()
    {
        return Some(prefix);
    }
    None
}

/// Compute the install directive for one source (DUMP-2).
///
/// Returns `(install, install_items)` where exactly one is `Some`.
///
/// - All offered items installed -> `(Some(true), None)`
/// - No offered items installed -> `(Some(false), None)`
/// - Proper subset installed -> `(None, Some(sorted_bare_refs))`
///
/// The `install_items = []` form is NEVER emitted; that case becomes
/// `install = false` (DUMP-2).
fn compute_install_directive(
    paths: &Paths,
    source: &Source,
    manifest: &Manifest,
) -> Result<(Option<bool>, Option<Vec<String>>)> {
    // Scan the source to find its offered items (bare names).
    let mut offered_items = Vec::new();
    catalog::scan_source(paths, source, &mut offered_items)?;

    // Build the set of offered bare "kind:name" refs from the catalog.
    let offered: BTreeSet<String> = offered_items
        .iter()
        .map(|item| format!("{}:{}", item.kind.as_str(), item.name))
        .collect();

    // Build the set of installed bare "kind:name" refs from the manifest that
    // belong to this source.
    // DUMP-5: items are listed by bare kind:name (catalog/source truth).
    // DUMP-6: items installed as within-source dependencies are included.
    let installed_for_source: HashMap<String, ()> = manifest
        .items
        .values()
        .filter(|item| item.source == source.name)
        .map(|item| {
            let bare_ref = format!("{}:{}", item.kind.as_str(), item.bare_name);
            (bare_ref, ())
        })
        .collect();

    // Intersect: only count installed items that the source actually offers
    // (an item installed from a previous catalog state but no longer offered
    // is excluded from the intersection).
    let installed_and_offered: BTreeSet<String> = offered
        .iter()
        .filter(|ref_| installed_for_source.contains_key(*ref_))
        .cloned()
        .collect();

    if offered.is_empty() {
        // Source offers nothing; emit install = false.
        return Ok((Some(false), None));
    }

    if installed_and_offered.is_empty() {
        // spec: DUMP-2 — none installed -> install = false.
        // Never emit install_items = [].
        return Ok((Some(false), None));
    }

    if installed_and_offered == offered {
        // spec: DUMP-2 — all offered items installed -> install = true.
        return Ok((Some(true), None));
    }

    // spec: DUMP-2 — proper subset -> install_items listing exactly those items.
    // Items are sorted deterministically (BTreeSet is already sorted).
    let items: Vec<String> = installed_and_offered.into_iter().collect();
    Ok((None, Some(items)))
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::ItemKind;
    use crate::manifest::InstalledItem;
    use crate::source::Source;

    // Helper: build a minimal Source struct for testing.
    fn make_source(name: &str, url: &str, commit: Option<&str>) -> Source {
        Source {
            name: name.to_string(),
            url: url.to_string(),
            host: "local".to_string(),
            owner: "test".to_string(),
            repo: name.split('/').next_back().unwrap_or(name).to_string(),
            commit: commit.map(str::to_string),
            description: None,
            alias: None,
            pin: crate::source::Pin::default(),
            roots: None,
            install_hooks: vec![],
            install_hook: None,
            install_hook_commit: None,
        }
    }

    // Helper: build a minimal InstalledItem.
    fn make_installed(kind: ItemKind, name: &str, bare: &str, source: &str) -> InstalledItem {
        InstalledItem {
            kind,
            name: name.to_string(),
            bare_name: bare.to_string(),
            source: source.to_string(),
            commit: "abc".to_string(),
            hash: "def".to_string(),
            store: "store/path".to_string(),
            links: vec![],
            description: None,
        }
    }

    // ----- Filtering logic (DUMP-2) -----

    #[test]
    fn filtering_all_installed_yields_install_true() {
        // spec: DUMP-2 — every offered item installed -> install = true.
        // (compute_install_directive requires disk access; we test the
        // all-installed branch via the emitted-TOML round-trip below.)
        //
        // Directly test the TOML emitter shape with a hand-crafted entry.
        let entry = DumpEntry {
            source: "/some/path".into(),
            alias: None,
            roots: None,
            pin_ref: None,
            install: Some(true),
            install_items: None,
        };
        let doc = DumpDoc {
            source: DumpSource {
                description: "test".into(),
            },
            discover: DumpDiscover {
                sources: vec![entry],
            },
        };
        let text = toml::to_string(&doc).unwrap();
        assert!(text.contains("install = true"), "must emit install = true");
        assert!(
            !text.contains("install-items"),
            "must NOT emit install-items when install = true"
        );
        // spec: DUMP-7 — parses back as a valid MindToml (DSC-3/DSC-30).
        let back: MindToml = toml::from_str(&text)
            .unwrap_or_else(|e| panic!("install=true output must parse as MindToml: {e}"));
        let ns = &back.discover.unwrap().sources[0];
        assert!(ns.install);
        assert!(ns.install_items.is_none());
    }

    #[test]
    fn filtering_none_installed_yields_install_false() {
        // spec: DUMP-2 — none installed -> install = false; never emit [].
        let entry = DumpEntry {
            source: "/some/path".into(),
            alias: None,
            roots: None,
            pin_ref: None,
            install: Some(false),
            install_items: None,
        };
        let doc = DumpDoc {
            source: DumpSource {
                description: "test".into(),
            },
            discover: DumpDiscover {
                sources: vec![entry],
            },
        };
        let text = toml::to_string(&doc).unwrap();
        assert!(
            text.contains("install = false"),
            "must emit install = false"
        );
        assert!(
            !text.contains("install-items"),
            "must NOT emit install-items when none installed"
        );
        // spec: DUMP-7
        let back: MindToml = toml::from_str(&text)
            .unwrap_or_else(|e| panic!("install=false output must parse as MindToml: {e}"));
        let ns = &back.discover.unwrap().sources[0];
        assert!(!ns.install);
        assert!(ns.install_items.is_none());
    }

    #[test]
    fn filtering_proper_subset_yields_install_items() {
        // spec: DUMP-2 DUMP-5 — proper subset -> install_items listing bare kind:name.
        // Never emit install_items = [].
        let items = vec!["agent:dev".to_string(), "skill:review".to_string()];
        let entry = DumpEntry {
            source: "/some/path".into(),
            alias: None,
            roots: None,
            pin_ref: None,
            install: None,
            install_items: Some(items.clone()),
        };
        let doc = DumpDoc {
            source: DumpSource {
                description: "test".into(),
            },
            discover: DumpDiscover {
                sources: vec![entry],
            },
        };
        let text = toml::to_string(&doc).unwrap();
        assert!(
            text.contains("install-items"),
            "must emit install-items for subset"
        );
        assert!(
            !text.contains("install = true"),
            "must NOT emit install = true when emitting install-items"
        );
        // spec: DUMP-7
        let back: MindToml = toml::from_str(&text)
            .unwrap_or_else(|e| panic!("install_items output must parse as MindToml: {e}"));
        let ns = &back.discover.unwrap().sources[0];
        assert!(
            !ns.install,
            "install bool must be false/absent for subset form"
        );
        let got = ns
            .install_items
            .as_deref()
            .expect("install_items must be present");
        assert_eq!(got, &["agent:dev", "skill:review"][..]);
    }

    #[test]
    fn empty_install_items_is_never_emitted() {
        // spec: DUMP-2 — install_items = [] is NEVER emitted; that case is
        // install = false.
        let entry = DumpEntry {
            source: "/some/path".into(),
            alias: None,
            roots: None,
            pin_ref: None,
            install: Some(false),
            install_items: None, // None, not Some([])
        };
        let doc = DumpDoc {
            source: DumpSource {
                description: "test".into(),
            },
            discover: DumpDiscover {
                sources: vec![entry],
            },
        };
        let text = toml::to_string(&doc).unwrap();
        assert!(
            !text.contains("install-items"),
            "install_items=[] must never appear; got: {text}"
        );
        assert!(text.contains("install = false"));
    }

    #[test]
    fn emitted_toml_parses_as_valid_mindtoml() {
        // spec: DUMP-7 — the emitted file is a valid super-source (parses under
        // DSC-3 and DSC-30 deny_unknown_fields) and carries [source].description.
        let doc = DumpDoc {
            source: DumpSource {
                description: "Generated by `mind dump`. Meld this file to reproduce the \
                              recorded source set and install selection."
                    .to_string(),
            },
            discover: DumpDiscover {
                sources: vec![
                    DumpEntry {
                        source: "/path/to/repo".into(),
                        alias: Some("pfx".into()),
                        roots: Some(vec!["packages".into()]),
                        pin_ref: Some("deadbeefdeadbeef1234".into()),
                        install: Some(true),
                        install_items: None,
                    },
                    DumpEntry {
                        source: "https://github.com/owner/repo".into(),
                        alias: None,
                        roots: None,
                        pin_ref: None,
                        install: None,
                        install_items: Some(vec!["skill:review".into(), "agent:dev".into()]),
                    },
                ],
            },
        };
        let text =
            toml::to_string(&doc).unwrap_or_else(|e| panic!("serialization must not fail: {e}"));
        // Must parse back cleanly without unknown-field errors.
        let back: MindToml = toml::from_str(&text)
            .unwrap_or_else(|e| panic!("emitted TOML must parse as MindToml: {e}\nTOML:\n{text}"));
        // [source].description is present.
        assert!(
            back.source.description.is_some(),
            "emitted TOML must carry [source].description"
        );
        // Only [discover].sources; no items of its own.
        assert!(back.items.is_empty(), "emitted TOML must have no [[items]]");
        let disc = back.discover.expect("must have [discover]");
        assert_eq!(disc.sources.len(), 2);
        // First entry: install = true, pin-ref (commit pin) present, alias present.
        let e0 = &disc.sources[0];
        assert!(e0.install, "first entry: install must be true");
        assert_eq!(e0.alias.as_deref(), Some("pfx"));
        assert_eq!(e0.roots.as_deref(), Some(&["packages".to_string()][..]));
        assert!(
            e0.pin_ref.is_some(),
            "pin-ref must be present for pinned entry"
        );
        // Second entry: install_items present, no install = true.
        let e1 = &disc.sources[1];
        assert!(!e1.install, "second entry: install must be false");
        let items = e1
            .install_items
            .as_deref()
            .expect("install_items must be present");
        assert_eq!(items, &["skill:review", "agent:dev"][..]);
    }

    #[test]
    fn spec_string_round_trips_for_local_path() {
        // Verify that the spec string we emit for a local source round-trips
        // through parse_spec back to the same URL.
        // spec: DUMP-1 DUMP-4
        let source = make_source("local/dev/agents", "/home/dev/agents", Some("abc123"));
        // The emitted spec is source.url for local sources.
        let spec = source.url.clone();
        let parsed = crate::source::parse_spec(&spec)
            .unwrap_or_else(|e| panic!("local spec must round-trip: {e}"));
        assert_eq!(
            parsed.url, source.url,
            "local path spec must round-trip through parse_spec"
        );
    }

    #[test]
    fn spec_string_round_trips_for_https_url() {
        // spec: DUMP-1 DUMP-4
        let url = "https://github.com/owner/repo";
        let source = make_source("github.com/owner/repo", url, None);
        let spec = source.url.clone();
        let parsed = crate::source::parse_spec(&spec)
            .unwrap_or_else(|e| panic!("https spec must round-trip: {e}"));
        assert_eq!(parsed.url, url);
    }

    #[test]
    fn pin_ref_emitted_when_commit_recorded_regardless_of_pin_kind() {
        // spec: DUMP-1 DUMP-4 DSC-65 — the pin is always emitted as `pin-ref`
        // (the recorded commit sha) when the source has a recorded commit,
        // regardless of the source's pin kind (FollowBranch, Tag, Ref, or
        // DefaultBranch). A source with no recorded commit emits no pin.
        let entry_with_commit = DumpEntry {
            source: "/a/b".into(),
            alias: None,
            roots: None,
            pin_ref: Some("deadbeefdeadbeef".into()),
            install: Some(true),
            install_items: None,
        };
        let entry_no_commit = DumpEntry {
            source: "/a/b".into(),
            alias: None,
            roots: None,
            pin_ref: None,
            install: Some(false),
            install_items: None,
        };

        let text_with = toml::to_string(&DumpDoc {
            source: DumpSource {
                description: "t".into(),
            },
            discover: DumpDiscover {
                sources: vec![entry_with_commit],
            },
        })
        .unwrap();
        let text_without = toml::to_string(&DumpDoc {
            source: DumpSource {
                description: "t".into(),
            },
            discover: DumpDiscover {
                sources: vec![entry_no_commit],
            },
        })
        .unwrap();

        assert!(
            text_with.contains("pin-ref"),
            "must emit pin-ref when a commit is recorded: {text_with}"
        );
        assert!(
            text_with.contains("deadbeefdeadbeef"),
            "must emit the recorded commit sha as pin-ref: {text_with}"
        );
        assert!(
            !text_with.contains("follow-branch"),
            "must NOT emit follow-branch (replaced by pin-ref): {text_with}"
        );
        assert!(
            !text_without.contains("pin-ref"),
            "must NOT emit pin-ref when no commit is recorded: {text_without}"
        );
        // Both must parse as valid MindToml (DUMP-7): NestedSource now accepts pin-ref.
        let _: MindToml = toml::from_str(&text_with)
            .unwrap_or_else(|e| panic!("pinned entry must parse: {e}\n{text_with}"));
        let _: MindToml = toml::from_str(&text_without)
            .unwrap_or_else(|e| panic!("unpinned entry must parse: {e}\n{text_without}"));
    }

    #[test]
    fn install_items_sorted_deterministically() {
        // spec: DUMP-5 — install_items are sorted (kind then name) so output is stable.
        let mut items = [
            "skill:review".to_string(),
            "agent:dev".to_string(),
            "rule:style".to_string(),
        ];
        items.sort();
        assert_eq!(items[0], "agent:dev");
        assert_eq!(items[1], "rule:style");
        assert_eq!(items[2], "skill:review");
    }

    #[test]
    fn emitted_toml_has_no_items_of_its_own() {
        // spec: DUMP-7 — the emitted file declares only [discover].sources;
        // no [[items]] or item globs of its own.
        let doc = DumpDoc {
            source: DumpSource {
                description: "Generated by `mind dump`. test".into(),
            },
            discover: DumpDiscover { sources: vec![] },
        };
        let text = toml::to_string(&doc).unwrap();
        let back: MindToml =
            toml::from_str(&text).unwrap_or_else(|e| panic!("empty doc must parse: {e}"));
        assert!(back.items.is_empty(), "must have no [[items]]");
        assert!(
            !back.is_authoritative(),
            "empty discover.sources must not be authoritative (only sources, no item globs)"
        );
    }

    #[test]
    fn whole_sources_entry_has_install_true() {
        // spec: DUMP-3 — --whole-sources always emits install = true.
        let entry = DumpEntry {
            source: "/a/b".into(),
            alias: None,
            roots: None,
            pin_ref: None,
            install: Some(true),
            install_items: None,
        };
        let text = toml::to_string(&DumpDoc {
            source: DumpSource {
                description: "t".into(),
            },
            discover: DumpDiscover {
                sources: vec![entry],
            },
        })
        .unwrap();
        assert!(text.contains("install = true"));
        let back: MindToml = toml::from_str(&text).unwrap();
        assert!(back.discover.unwrap().sources[0].install);
    }

    #[test]
    fn dependency_item_treated_like_any_installed_item() {
        // spec: DUMP-6 — an item installed only as a within-source dependency
        // is part of the installed set just like any other manifest entry.
        // Here we verify that bare_name is what goes into install_items, not
        // the effective name (which may be prefixed).
        let item = make_installed(ItemKind::Skill, "pfx-review", "review", "local/dev/agents");
        // The bare_name is "review", so install_items should list "skill:review".
        let bare_ref = format!("{}:{}", item.kind.as_str(), item.bare_name);
        assert_eq!(bare_ref, "skill:review");
    }

    #[test]
    fn dump_8_empty_registry_produces_valid_super_source() {
        // spec: DUMP-8 — with no melded sources the document is valid with
        // [discover].sources = [].
        let doc = DumpDoc {
            source: DumpSource {
                description: "Generated by `mind dump`. Meld this file to reproduce the \
                              recorded source set and install selection."
                    .to_string(),
            },
            discover: DumpDiscover { sources: vec![] },
        };
        let text = toml::to_string(&doc).unwrap();
        let back: MindToml =
            toml::from_str(&text).unwrap_or_else(|e| panic!("empty-registry doc must parse: {e}"));
        let disc = back.discover.expect("must have [discover]");
        assert!(disc.sources.is_empty(), "sources must be empty");
    }
}