mind-cli 0.42.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
//! End-to-end coverage of the NS-72/NS-73 prefix-safety guard on its two live
//! ingress points: `meld --namespace`/`-N` (a user-supplied prefix) and a
//! melded repo's `mind.toml` `[source].prefix` (a source-declared prefix).
//! Both funnel through `namespace::validate_prefix`, which rejects a prefix
//! carrying a security-blocked Unicode code point with a structured
//! `UnsafePrefix` error (spec/namespacing.md NS-72, NS-73).
//!
//! See CLAUDE.md: manual checks must be encoded as tests unless genuinely
//! impossible to automate. This suite drives the real `mind` binary against a
//! hermetic local-git fixture, no network.

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

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

/// A throwaway environment: a source git repo plus isolated MIND_HOME/CLAUDE_HOME.
struct Sandbox {
    base: PathBuf,
    source: PathBuf,
    mind_home: PathBuf,
    claude_home: PathBuf,
}

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

impl Sandbox {
    /// A source repo with one skill (`review`), committed.
    fn new() -> Sandbox {
        let n = COUNTER.fetch_add(1, Ordering::SeqCst);
        let base =
            std::env::temp_dir().join(format!("mind-prefix-guard-{}-{n}", std::process::id()));
        let _ = std::fs::remove_dir_all(&base);
        let source = base.join("agents");
        let sb = Sandbox {
            base: base.clone(),
            source: source.clone(),
            mind_home: base.join("mind"),
            claude_home: base.join("claude"),
        };
        write(
            &source.join("skills/review/SKILL.md"),
            "---\nname: review\ndescription: Review the diff for bugs\n---\n# review skill\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"]);
        std::fs::create_dir_all(&sb.mind_home).unwrap();
        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)
            .env_remove("MIND_AGENT_HOMES")
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .stdin(Stdio::null())
            .output()
            .expect("run mind");
        Run {
            stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
            stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
            success: out.status.success(),
        }
    }

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

    /// Write `mind.toml` at the source repo root and commit it, declaring
    /// `[source].prefix = <prefix>`. `prefix` is written as literal UTF-8, so
    /// a caller-supplied blocked Unicode code point lands in the TOML value
    /// as-is (a real character, not an escape sequence) -- exactly the
    /// payload the declared-prefix ingress must refuse.
    fn declare_prefix(&self, prefix: &str) {
        write(
            &self.source.join("mind.toml"),
            &format!("[source]\nprefix = \"{prefix}\"\n"),
        );
        git(&self.source, &["add", "-A"]);
        git(&self.source, &["commit", "-qm", "declare prefix"]);
    }
}

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(repo: &Path, args: &[&str]) {
    std::fs::create_dir_all(repo).unwrap();
    let status = Command::new("git")
        .args(args)
        .current_dir(repo)
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .expect("run git");
    assert!(status.success(), "git {args:?} failed");
}

/// The generic cause wording `unsafe_prefix_message` uses for the blocked
/// Unicode class, shared by every assertion below rather than pinned to a
/// specific code point, per the coupling with error.rs's generic ("an
/// invisible, bidi, or zero-width character") wording.
const BLOCKED_UNICODE_CAUSE: &str = "invisible, bidi, or zero-width character";

// spec: NS-72
#[test]
fn meld_namespace_with_bidi_override_is_refused_as_unsafe_prefix() {
    // The DSC-94-era code point (U+202E, a bidi override): already blocked
    // before NS-73 broadened the set, pinning the baseline `meld --namespace`
    // path still works.
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let bad_prefix = format!("pay{}oot", '\u{202E}');
    let r = sb.mind(&["meld", &spec, "--namespace", &bad_prefix, "--yes"]);
    assert!(
        !r.success,
        "meld --namespace with a bidi-override prefix must be refused"
    );
    assert!(
        r.stderr.contains(BLOCKED_UNICODE_CAUSE),
        "UnsafePrefix cause must be reported generically: {}",
        r.stderr
    );
    // Nothing installs: the guard must fire before the source is registered.
    let recall = sb.mind(&["recall"]).stdout;
    assert!(
        !recall.contains("review"),
        "a refused meld must not install anything: {recall}"
    );
}

// spec: NS-72
#[test]
fn meld_namespace_short_flag_with_bidi_override_is_refused() {
    // The `-N` short form goes through the identical `validate_prefix`
    // chokepoint as `--namespace`.
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let bad_prefix = format!("pay{}oot", '\u{202E}');
    let r = sb.mind(&["meld", &spec, "-N", &bad_prefix, "--yes"]);
    assert!(
        !r.success,
        "meld -N with a bidi-override prefix must be refused"
    );
    assert!(r.stderr.contains(BLOCKED_UNICODE_CAUSE), "{}", r.stderr);
}

// spec: NS-73
#[test]
fn meld_namespace_with_tag_block_character_is_refused_as_unsafe_prefix() {
    // The M5/NS-73 broadening's headline addition: a Unicode tag-block code
    // point (U+E0041, TAG LATIN SMALL LETTER A) renders as nothing at a
    // terminal, so a prefix carrying it would look clean while smuggling an
    // invisible payload into every namespaced ref. Refused the same way as a
    // bidi override.
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let bad_prefix = format!("acme{}", '\u{E0041}');
    let r = sb.mind(&["meld", &spec, "--namespace", &bad_prefix, "--yes"]);
    assert!(
        !r.success,
        "meld --namespace with a tag-block character in the prefix must be refused"
    );
    assert!(
        r.stderr.contains(BLOCKED_UNICODE_CAUSE),
        "UnsafePrefix cause must be reported generically: {}",
        r.stderr
    );
    let recall = sb.mind(&["recall"]).stdout;
    assert!(
        !recall.contains("review"),
        "a refused meld must not install anything: {recall}"
    );
}

// spec: NS-73
#[test]
fn meld_namespace_with_variation_selector_is_refused_as_unsafe_prefix() {
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let bad_prefix = format!("acme{}", '\u{FE0F}');
    let r = sb.mind(&["meld", &spec, "--namespace", &bad_prefix, "--yes"]);
    assert!(
        !r.success,
        "meld --namespace with a variation-selector character in the prefix must be refused"
    );
    assert!(r.stderr.contains(BLOCKED_UNICODE_CAUSE), "{}", r.stderr);
}

// spec: NS-72
#[test]
fn meld_declared_prefix_with_bidi_override_is_refused_on_load() {
    // A repo's own `mind.toml [source].prefix` carrying a blocked character is
    // rejected at mind.toml load time (mindfile.rs), independent of whether
    // the run is interactive -- so a non-TTY test harness still observes the
    // refusal, not a silent "no prefix" fallback.
    let sb = Sandbox::new();
    let bad_prefix = format!("pay{}oot", '\u{202E}');
    sb.declare_prefix(&bad_prefix);
    let spec = sb.source_spec();
    let r = sb.mind(&["meld", &spec, "--yes"]);
    assert!(
        !r.success,
        "meld of a source declaring an unsafe [source].prefix must be refused"
    );
    assert!(
        r.stderr.contains(BLOCKED_UNICODE_CAUSE),
        "UnsafePrefix cause must be reported generically: {}",
        r.stderr
    );
    let recall = sb.mind(&["recall"]).stdout;
    assert!(
        !recall.contains("review"),
        "a refused meld must not install anything: {recall}"
    );
}

// spec: NS-73
#[test]
fn meld_declared_prefix_with_tag_block_character_is_refused_on_load() {
    let sb = Sandbox::new();
    let bad_prefix = format!("acme{}", '\u{E0041}');
    sb.declare_prefix(&bad_prefix);
    let spec = sb.source_spec();
    let r = sb.mind(&["meld", &spec, "--yes"]);
    assert!(
        !r.success,
        "meld of a source declaring a tag-block [source].prefix must be refused"
    );
    assert!(
        r.stderr.contains(BLOCKED_UNICODE_CAUSE),
        "UnsafePrefix cause must be reported generically: {}",
        r.stderr
    );
    let recall = sb.mind(&["recall"]).stdout;
    assert!(
        !recall.contains("review"),
        "a refused meld must not install anything: {recall}"
    );
}

// spec: DSC-112
#[test]
fn an_alias_the_reserved_list_caught_up_with_warns_but_keeps_scanning() {
    // The reserved-word list is append-only: `workflow` joined it when the
    // workflow kind shipped, under sources already melded with that prefix.
    // A registry entry is never re-validated, so the prefix stays in effect;
    // the items are installed under it and failing every scanning verb would
    // take the user's whole lobe down over a naming problem. Warn instead.
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let meld = sb.mind(&["meld", &spec, "--namespace", "acme", "--yes"]);
    assert!(meld.success, "setup meld must succeed: {}", meld.stderr);

    // Rewrite the recorded namespace prefix to what a pre-reservation binary
    // would have accepted. Nothing in mind can produce this state today, which
    // is exactly why the guard has to read the registry rather than trust the
    // ingress. Only the `alias` field (the namespace prefix) is rewritten:
    // `as_alias` (the instance-identity alias) is already revalidated on load
    // by STO-68, which drops the whole entry, and this is about the prefix that
    // survives into the scan.
    let registry = sb.mind_home.join("sources.json");
    let text = std::fs::read_to_string(&registry).expect("read sources.json");
    let recorded = "\"alias\": \"acme\"";
    assert!(
        text.contains(recorded),
        "the namespace prefix must be recorded as `{recorded}` to be rewritten: {text}"
    );
    std::fs::write(&registry, text.replace(recorded, "\"alias\": \"workflow\"")).unwrap();

    let recall = sb.mind(&["recall"]);
    assert!(
        recall.success,
        "a reserved recorded prefix must not fail the scan: {} {}",
        recall.stdout, recall.stderr
    );
    assert!(
        recall
            .stderr
            .contains("'workflow' is a word mind now reserves"),
        "the scan must warn that the recorded prefix is now a reserved word: {}",
        recall.stderr
    );
    assert!(
        recall
            .stderr
            .contains("unmeld it, then re-meld with `--namespace <other>`"),
        "the warning must name the way to rename it: {}",
        recall.stderr
    );
    // Advisory only: the item is still there, under the prefix it was
    // installed with, as a SKILL named `workflow:review` (not read as a
    // kind-qualified workflow ref).
    let probe = sb.mind(&["probe", "--no-tui"]);
    assert!(
        probe.stdout.contains("skill:workflow:review"),
        "the items must still be listed under the recorded prefix: {}",
        probe.stdout
    );
}

// spec: DSC-112
#[test]
fn the_reserved_alias_warning_prints_once_per_source_per_run() {
    // The warning is about the source, not the scan, so it prints once per
    // run whatever the verb does: a sync, an upgrade with real changes, a
    // learn, a JSON listing. The per-process gate itself is pinned by the unit
    // test `the_reserved_prefix_warning_is_owed_once_per_source`; this pins the
    // CLI surface so a verb that grows a second scan cannot double it.
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let meld = sb.mind(&["meld", &spec, "--namespace", "acme", "--yes"]);
    assert!(meld.success, "setup meld must succeed: {}", meld.stderr);
    let registry = sb.mind_home.join("sources.json");
    let text = std::fs::read_to_string(&registry).expect("read sources.json");
    std::fs::write(
        &registry,
        text.replace("\"alias\": \"acme\"", "\"alias\": \"workflow\""),
    )
    .unwrap();
    // A real upstream change, so `upgrade` has something to sync and re-link.
    write(
        &sb.source.join("skills/review/SKILL.md"),
        "---\nname: review\ndescription: Review the diff, v2\n---\n# review skill v2\n",
    );
    write(
        &sb.source.join("skills/other/SKILL.md"),
        "---\nname: other\ndescription: Another skill\n---\n# other\n",
    );
    git(&sb.source, &["add", "-A"]);
    git(&sb.source, &["commit", "-qm", "v2"]);

    for args in [
        vec!["recall"],
        vec!["upgrade", "--yes"],
        vec!["learn", "skill:workflow:other", "--yes"],
        vec!["probe", "--no-tui"],
        vec!["sync", "--upgrade", "--yes"],
        vec!["introspect"],
        vec!["recall", "skill:workflow:review"],
        vec!["--json", "probe", "--no-tui"],
        vec!["upgrade", "skill:workflow:review", "--yes"],
    ] {
        let r = sb.mind(&args);
        assert!(
            r.success,
            "{args:?} must succeed: {} {}",
            r.stdout, r.stderr
        );
        assert_eq!(
            r.stderr.matches("now reserves").count(),
            1,
            "{args:?}: the warning must print exactly once: {}",
            r.stderr
        );
    }
}

// spec: DSC-112
#[test]
fn following_the_reserved_alias_remedy_clears_the_warning() {
    // The warning names a remedy; following it has to end the condition.
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let meld = sb.mind(&["meld", &spec, "--namespace", "acme", "--yes"]);
    assert!(meld.success, "setup meld must succeed: {}", meld.stderr);
    let registry = sb.mind_home.join("sources.json");
    let text = std::fs::read_to_string(&registry).expect("read sources.json");
    std::fs::write(
        &registry,
        text.replace("\"alias\": \"acme\"", "\"alias\": \"workflow\""),
    )
    .unwrap();
    assert!(
        sb.mind(&["recall"]).stderr.contains("now reserves"),
        "precondition: the warning fires before the remedy"
    );

    // A glob selector, since a namespaced meld's recorded name carries more
    // than the repo's own name.
    let unmeld = sb.mind(&["unmeld", "*agents*", "--yes"]);
    assert!(
        unmeld.success,
        "unmeld must succeed: {} {}",
        unmeld.stdout, unmeld.stderr
    );
    let remeld = sb.mind(&["meld", &spec, "--namespace", "acme2", "--yes"]);
    assert!(
        remeld.success,
        "re-meld under another prefix must succeed: {} {}",
        remeld.stdout, remeld.stderr
    );
    for args in [vec!["recall"], vec!["probe", "--no-tui"]] {
        let r = sb.mind(&args);
        assert!(
            r.success,
            "{args:?} must succeed: {} {}",
            r.stdout, r.stderr
        );
        assert!(
            !r.stderr.contains("reserves"),
            "{args:?}: the warning must be gone after the remedy: {}",
            r.stderr
        );
    }
    assert!(
        sb.mind(&["recall"]).stdout.contains("acme2:review"),
        "the item is installed under the new prefix"
    );
}

// spec: DSC-112
#[test]
fn a_melded_source_declaring_a_reserved_prefix_names_unmeld_as_the_remedy() {
    // The other half: a `[source].prefix` is re-validated at every mind.toml
    // load, so once the word is reserved the refusal is hard and lands on
    // every verb that scans the source. The consumer cannot override a value
    // the source declares, so the error has to name the source and the one
    // command that ends the condition, not the pre-meld "cannot be used as a
    // namespace prefix" wording.
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let meld = sb.mind(&["meld", &spec, "--yes"]);
    assert!(meld.success, "setup meld must succeed: {}", meld.stderr);

    // The source declares the prefix AFTER the meld, standing in for a binary
    // that accepted the word when the meld happened. A local path source is
    // read from its working tree, so this is what the next scan sees.
    sb.declare_prefix("workflow");

    // `must_fail` is false for the verbs that already degrade per source rather
    // than aborting (upgrade reports the source it could not check). What every
    // verb owes the user is the same either way: the actionable message.
    for (verb, must_fail) in [
        (vec!["recall"], true),
        (vec!["probe", "--no-tui"], true),
        (vec!["learn", "review"], true),
        (vec!["upgrade", "--no-sync", "--yes"], false),
        (vec!["introspect"], false),
    ] {
        let r = sb.mind(&verb);
        assert!(
            !must_fail || !r.success,
            "{verb:?} must refuse a source declaring a now-reserved prefix: {} {}",
            r.stdout,
            r.stderr
        );
        let combined = format!("{}{}", r.stdout, r.stderr);
        assert!(
            combined.contains("mind unmeld"),
            "{verb:?}: the error must name `mind unmeld <source>` as the remedy: {combined}"
        );
        assert!(
            combined.contains("agents"),
            "{verb:?}: the error must name the source it is about: {combined}"
        );
        assert!(
            combined.contains("workflow"),
            "{verb:?}: the error must name the offending prefix: {combined}"
        );
    }

    // And the remedy works: after unmeld, the verbs run again.
    let unmeld = sb.mind(&["unmeld", "agents", "--yes"]);
    assert!(
        unmeld.success,
        "the named remedy must work: {} {}",
        unmeld.stdout, unmeld.stderr
    );
    let recall = sb.mind(&["recall"]);
    assert!(
        recall.success,
        "after the remedy, scanning verbs must work again: {} {}",
        recall.stdout, recall.stderr
    );
}

/// The advisory warning is written by the SCAN, which runs under `--json` too.
/// A line on stdout there would corrupt the one document a `--json` caller
/// parses, so the warning has to be stderr-only and the document has to stay
/// whole.
// spec: DSC-112 CLI-217
#[test]
fn the_reserved_alias_warning_does_not_reach_the_json_document() {
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let meld = sb.mind(&["meld", &spec, "--namespace", "acme", "--yes"]);
    assert!(meld.success, "setup meld must succeed: {}", meld.stderr);
    let registry = sb.mind_home.join("sources.json");
    let text = std::fs::read_to_string(&registry).expect("read sources.json");
    std::fs::write(
        &registry,
        text.replace("\"alias\": \"acme\"", "\"alias\": \"workflow\""),
    )
    .unwrap();

    // Two catalog-reading verbs with a JSON stdout contract, one of which
    // (`probe`) scans every source twice over in its listing path.
    for args in [
        vec!["--json", "recall"],
        vec!["--json", "probe", "--no-tui"],
    ] {
        let r = sb.mind(&args);
        assert!(
            r.success,
            "{args:?} must still succeed under the warning: {} {}",
            r.stdout, r.stderr
        );
        let doc: serde_json::Value = serde_json::from_str(&r.stdout).unwrap_or_else(|e| {
            panic!(
                "{args:?}: stdout must be exactly one JSON document ({e}): {}",
                r.stdout
            )
        });
        assert!(doc.is_object(), "{args:?}: {doc}");
        assert!(
            !r.stdout.contains("reserve"),
            "{args:?}: the warning must not be on stdout: {}",
            r.stdout
        );
        assert!(
            r.stderr.contains("reserve"),
            "{args:?}: the warning must still be emitted, on stderr: {}",
            r.stderr
        );
    }
}

/// The condition is advisory, so the verbs that touch a source's git state have
/// to keep working under it: an operator who cannot sync or upgrade is not
/// getting advice, they are locked out by a naming problem.
// spec: DSC-112
#[test]
fn a_reserved_alias_does_not_block_sync_or_upgrade() {
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let meld = sb.mind(&["meld", &spec, "--namespace", "acme", "--yes"]);
    assert!(meld.success, "setup meld must succeed: {}", meld.stderr);
    let registry = sb.mind_home.join("sources.json");
    let text = std::fs::read_to_string(&registry).expect("read sources.json");
    std::fs::write(
        &registry,
        text.replace("\"alias\": \"acme\"", "\"alias\": \"workflow\""),
    )
    .unwrap();

    for args in [
        vec!["sync"],
        vec!["upgrade", "--no-sync", "--yes"],
        vec!["introspect"],
    ] {
        let r = sb.mind(&args);
        assert!(
            r.success,
            "{args:?} must not be blocked by an advisory prefix warning: {} {}",
            r.stdout, r.stderr
        );
    }

    // The installed item keeps its identity throughout: the warning renames
    // nothing on its own, so nothing is orphaned or re-linked behind the
    // operator's back.
    assert!(
        sb.claude_home.join("skills/workflow:review").exists(),
        "the item must still be linked under the prefix it was installed with"
    );
    let recall = sb.mind(&["recall"]);
    assert!(
        recall.stdout.contains("skill:workflow:review"),
        "the item must still be reported, as a skill: {}",
        recall.stdout
    );
}

/// The new variant is an error like any other, so a `--json` caller has to
/// reach it through the CLI-181 envelope with its own machine-readable kind,
/// not as a text line or as the generic `reserved-prefix` kind (which names a
/// different remedy).
// spec: DSC-112 CLI-181
#[test]
fn the_melded_reserved_prefix_error_has_its_own_json_envelope() {
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let meld = sb.mind(&["meld", &spec, "--yes"]);
    assert!(meld.success, "setup meld must succeed: {}", meld.stderr);
    sb.declare_prefix("workflow");

    let r = sb.mind(&["--json", "recall"]);
    assert!(!r.success, "the run must fail: {} {}", r.stdout, r.stderr);
    let doc: serde_json::Value = serde_json::from_str(&r.stdout)
        .unwrap_or_else(|e| panic!("stdout must be one JSON document ({e}): {}", r.stdout));
    assert_eq!(doc["schema"], 1, "the envelope must be schema 1: {doc}");
    assert_eq!(
        doc["error"]["kind"], "melded-source-reserved-prefix",
        "the melded-source case must be distinguishable from the pre-meld \
         `reserved-prefix` one, whose remedy is different: {doc}"
    );
    let msg = doc["error"]["message"].as_str().unwrap_or_default();
    for needle in ["agents", "workflow", "mind unmeld"] {
        assert!(
            msg.contains(needle),
            "the envelope message must name '{needle}': {doc}"
        );
    }
}

// spec: NS-72 NS-73
#[test]
fn meld_namespace_with_clean_prefix_still_succeeds() {
    // Control: a prefix with no blocked characters is unaffected by the
    // broadened guard.
    let sb = Sandbox::new();
    let spec = sb.source_spec();
    let r = sb.mind(&["meld", &spec, "--namespace", "acme", "--yes"]);
    assert!(
        r.success,
        "a clean prefix must still be accepted: {}",
        r.stderr
    );
    let recall = sb.mind(&["recall"]).stdout;
    assert!(
        recall.contains("acme:review"),
        "the clean prefix must apply normally: {recall}"
    );
}