shepherd-cli 6.4.9

The canonical shepherd command-line interface over the per-project registry, run artifacts, and sprint pipeline.
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
use std::path::{Path, PathBuf};
use std::process::{Command, Output};
use std::time::{SystemTime, UNIX_EPOCH};

fn binary() -> &'static str {
    env!("CARGO_BIN_EXE_shepherd")
}

fn fixture(label: &str) -> PathBuf {
    let nonce = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("clock is after epoch")
        .as_nanos();
    let root = std::env::temp_dir().join(format!(
        "shepherd-wave-c-bootstrap-{label}-{}-{nonce:x}",
        std::process::id()
    ));
    std::fs::create_dir_all(&root).expect("create fixture root");
    let status = Command::new("git")
        .args(["init", "--quiet"])
        .current_dir(&root)
        .status()
        .expect("initialize fixture git repository");
    assert!(status.success(), "git init must succeed");
    root
}

fn invoke(root: &Path, args: &[&str]) -> Output {
    Command::new(binary())
        .args(args)
        .current_dir(root)
        .env("SHEPHERD_HOME", root.join("isolated-home"))
        .output()
        .expect("run shepherd binary")
}

fn invoke_with_path(root: &Path, args: &[&str], path: &str) -> Output {
    Command::new(binary())
        .args(args)
        .current_dir(root)
        .env("SHEPHERD_HOME", root.join("isolated-home"))
        .env("PATH", path)
        .output()
        .expect("run shepherd binary with an overridden PATH")
}

/// The inherited `PATH`, minus any directory that already answers
/// `shepherd`. Doctor itself shells out to `git` to resolve the primary
/// root, so a test that wants "nothing resolves `shepherd`" cannot simply
/// hand it an empty `PATH` — that would break `git` resolution too, and
/// the failure would be `context()` erroring out, not the fact under test.
/// Filtering, rather than emptying, keeps every other tool on `PATH`
/// reachable regardless of whether this machine happens to have a real
/// `shepherd` installed somewhere.
fn path_without_any_shepherd() -> String {
    let real = std::env::var_os("PATH").unwrap_or_default();
    let filtered: Vec<PathBuf> = std::env::split_paths(&real)
        .filter(|dir| !dir.join("shepherd").exists() && !dir.join("shepherd.exe").exists())
        .collect();
    std::env::join_paths(filtered)
        .expect("join filtered PATH")
        .to_string_lossy()
        .into_owned()
}

fn text(bytes: &[u8]) -> String {
    String::from_utf8_lossy(bytes).into_owned()
}

fn cleanup(root: &Path) {
    std::fs::remove_dir_all(root).expect("remove fixture");
}

fn registry_path(root: &Path) -> PathBuf {
    root.join(".shepherd/shepherd.db")
}

fn identity_path(root: &Path) -> PathBuf {
    root.join(".shepherd/project.json")
}

fn project_identity_id(path: &Path) -> String {
    let bytes = std::fs::read(path).expect("read project identity");
    let value: serde_json::Value =
        serde_json::from_slice(&bytes).expect("project identity must be valid JSON");
    value["id"]
        .as_str()
        .expect("project identity id must be a string")
        .to_owned()
}

fn count_project_rows(database: &Path) -> i64 {
    let connection = rusqlite::Connection::open(database).expect("open registry");
    connection
        .query_row("SELECT COUNT(*) FROM projects", [], |row| row.get(0))
        .expect("count projects rows")
}

#[test]
fn init_refuses_unconfirmed_mutation_then_materializes_only_layout_v5_roots() {
    let root = fixture("init");

    let refused = invoke(&root, &["init"]);
    assert_eq!(refused.status.code(), Some(2));
    assert_eq!(
        text(&refused.stderr),
        "ERROR: init is mutating; re-run with --confirm\n"
    );
    assert!(!root.join(".shepherd").exists());

    let initialized = invoke(&root, &["init", "--confirm"]);
    assert_eq!(
        initialized.status.code(),
        Some(0),
        "stderr={}",
        text(&initialized.stderr)
    );
    assert!(
        text(&initialized.stdout).starts_with("initialized layout-v5 namespace: "),
        "stdout={}",
        text(&initialized.stdout)
    );
    for directory in ["docs", "ctx", "runs"] {
        assert!(
            root.join(".shepherd").join(directory).is_dir(),
            "{directory} must be part of the approved layout-v5 project shape"
        );
    }
    assert!(root.join(".shepherd/shepherd.toml").is_file());
    assert!(root.join(".shepherd/shepherd.db").is_file());
    for retired in ["graph", "reports", "audits", "plans", "dispatch"] {
        assert!(
            !root.join(".shepherd").join(retired).exists(),
            "init must not recreate retired root {retired}"
        );
    }

    // The complete artifact set: project identity, and its matching row.
    // GI1 — a namespace that lacks either of these cannot dispatch, and the
    // gate this replaces never looked at either one.
    let identity = identity_path(&root);
    let identity_metadata =
        std::fs::symlink_metadata(&identity).expect("project identity must exist");
    assert!(
        identity_metadata.is_file(),
        "project identity must be a regular file"
    );
    let id = project_identity_id(&identity);
    let parsed = uuid::Uuid::parse_str(&id).expect("project identity id must be a valid uuid");
    assert_eq!(
        parsed.get_version_num(),
        7,
        "project identity id must be a uuid v7"
    );

    let database = registry_path(&root);
    let connection = rusqlite::Connection::open(&database).expect("open registry");
    let rows: Vec<String> = connection
        .prepare("SELECT id FROM projects")
        .expect("prepare projects query")
        .query_map([], |row| row.get(0))
        .expect("query projects")
        .collect::<Result<Vec<_>, _>>()
        .expect("collect project rows");
    assert_eq!(
        rows,
        vec![id],
        "the projects table must hold exactly one row matching the identity file"
    );

    let repeated = invoke(&root, &["init", "--confirm"]);
    assert_eq!(
        repeated.status.code(),
        Some(0),
        "stderr={}",
        text(&repeated.stderr)
    );
    cleanup(&root);
}

#[test]
fn config_reads_typed_defaults_and_requires_confirmation_to_create_its_document() {
    let root = fixture("config");
    let expected = std::fs::canonicalize(&root)
        .expect("canonical root")
        .join(".shepherd/shepherd.toml");

    let path = invoke(&root, &["config", "path"]);
    assert_eq!(path.status.code(), Some(0), "stderr={}", text(&path.stderr));
    assert_eq!(text(&path.stdout), format!("{}\n", expected.display()));
    assert!(!root.join(".shepherd").exists(), "path is read-only");

    let value = invoke(&root, &["config", "get", "paths.runs"]);
    assert_eq!(
        value.status.code(),
        Some(0),
        "stderr={}",
        text(&value.stderr)
    );
    assert_eq!(text(&value.stdout), ".shepherd/runs\n");
    assert!(!root.join(".shepherd").exists(), "get is read-only");

    let refused = invoke(&root, &["config", "init"]);
    assert_eq!(refused.status.code(), Some(2));
    assert_eq!(
        text(&refused.stderr),
        "ERROR: config init is mutating; re-run with --confirm\n"
    );
    assert!(!root.join(".shepherd").exists());

    let initialized = invoke(&root, &["config", "init", "--confirm"]);
    assert_eq!(
        initialized.status.code(),
        Some(0),
        "stderr={}",
        text(&initialized.stderr)
    );
    assert!(root.join(".shepherd/shepherd.toml").is_file());
    let show = invoke(&root, &["config", "show"]);
    assert_eq!(show.status.code(), Some(0), "stderr={}", text(&show.stderr));
    assert!(text(&show.stdout).contains("\"paths\""));
    cleanup(&root);
}

#[test]
fn home_and_doctor_are_read_only_until_explicitly_confirmed() {
    let root = fixture("home-doctor");
    let home = root.join("isolated-home");

    let which = invoke(&root, &["home", "which"]);
    assert_eq!(
        which.status.code(),
        Some(0),
        "stderr={}",
        text(&which.stderr)
    );
    assert_eq!(text(&which.stdout), format!("{}\n", home.display()));
    assert!(!home.exists(), "home which must not create the path");

    let show = invoke(&root, &["home", "show"]);
    assert_eq!(show.status.code(), Some(0), "stderr={}", text(&show.stderr));
    assert_eq!(text(&show.stdout), format!("home: {}\n", home.display()));
    assert!(!home.exists(), "home show must not create the path");

    let refused = invoke(&root, &["home", "init"]);
    assert_eq!(refused.status.code(), Some(2));
    assert_eq!(
        text(&refused.stderr),
        "ERROR: home init is mutating; re-run with --confirm\n"
    );
    assert!(!home.exists());

    let sick = invoke(&root, &["doctor", "--json"]);
    assert_eq!(sick.status.code(), Some(3));
    let report: serde_json::Value = serde_json::from_slice(&sick.stdout).expect("doctor JSON");
    assert_eq!(report["ok"], false);
    assert!(
        !root.join(".shepherd").exists(),
        "doctor must not bootstrap"
    );

    let initialized = invoke(&root, &["home", "init", "--confirm"]);
    assert_eq!(
        initialized.status.code(),
        Some(0),
        "stderr={}",
        text(&initialized.stderr)
    );
    assert!(home.is_dir());
    assert!(
        !home.join("profiles").exists(),
        "unused profile authority must not be recreated"
    );
    assert!(
        !home.join("templates").exists(),
        "user templates have no native resolver and must not be recreated"
    );

    let project = invoke(&root, &["init", "--confirm"]);
    assert_eq!(
        project.status.code(),
        Some(0),
        "stderr={}",
        text(&project.stderr)
    );
    let healthy = invoke(&root, &["doctor", "--json"]);
    assert_eq!(
        healthy.status.code(),
        Some(0),
        "stderr={}",
        text(&healthy.stderr)
    );
    let report: serde_json::Value = serde_json::from_slice(&healthy.stdout).expect("doctor JSON");
    assert_eq!(report["ok"], true);
    assert!(report["registry_schema"].as_u64().is_some());
    cleanup(&root);
}

#[test]
fn init_user_option_bootstraps_only_the_separately_resolved_user_home() {
    let root = fixture("init-user");
    let home = root.join("isolated-home");
    let initialized = invoke(&root, &["init", "--confirm", "--user"]);
    assert_eq!(
        initialized.status.code(),
        Some(0),
        "stderr={}",
        text(&initialized.stderr)
    );
    assert!(home.is_dir());
    assert!(!home.join("profiles").exists());
    assert!(!home.join("templates").exists());
    assert!(
        !home.join("runs").exists(),
        "project roots never leak into user home"
    );
    cleanup(&root);
}

#[cfg(unix)]
#[test]
fn init_refuses_a_symlink_namespace_without_touching_its_target() {
    use std::os::unix::fs::symlink;

    let root = fixture("symlink");
    let outside = root.join("outside");
    std::fs::create_dir_all(&outside).expect("create outside target");
    symlink(&outside, root.join(".shepherd")).expect("create namespace symlink");

    let result = invoke(&root, &["init", "--confirm"]);
    assert_ne!(result.status.code(), Some(0));
    assert!(!outside.join("docs").exists());
    assert!(!outside.join("shepherd.toml").exists());
    assert!(!outside.join("shepherd.db").exists());
    cleanup(&root);
}

#[cfg(unix)]
#[test]
fn init_refuses_an_existing_config_symlink_instead_of_reporting_success() {
    use std::os::unix::fs::symlink;

    let root = fixture("config-symlink");
    let namespace = root.join(".shepherd");
    std::fs::create_dir_all(&namespace).expect("create real namespace");
    let outside = root.join("outside-shepherd.toml");
    let original = b"# external config must remain untouched\n";
    std::fs::write(&outside, original).expect("write external config");
    symlink(&outside, namespace.join("shepherd.toml")).expect("create config symlink");

    let result = invoke(&root, &["init", "--confirm"]);
    assert_ne!(result.status.code(), Some(0));
    assert!(text(&result.stderr).contains("symbolic") || text(&result.stderr).contains("link"));
    assert_eq!(
        std::fs::read(&outside).expect("read external config"),
        original
    );
    assert!(!namespace.join("shepherd.db").exists());
    cleanup(&root);
}

#[test]
fn top_level_migrate_stays_an_explicit_dry_run_until_confirmation() {
    let root = fixture("migrate");
    let namespace = root.join(".shepherd");
    std::fs::create_dir_all(namespace.join("runs/v645")).expect("create canonical run root");
    std::fs::create_dir_all(namespace.join("docs/specs")).expect("create legacy docs root");
    std::fs::write(
        namespace.join("runs/v645/run.json"),
        b"{\"run\":\"v645\",\"schema_version\":1,\"status\":\"planned\"}\n",
    )
    .expect("write run document");
    std::fs::write(namespace.join("docs/specs/legacy.md"), b"legacy\n")
        .expect("write legacy document");

    let result = invoke(&root, &["migrate", "--dry-run"]);
    assert_eq!(
        result.status.code(),
        Some(0),
        "stderr={}",
        text(&result.stderr)
    );
    let report: serde_json::Value = serde_json::from_slice(&result.stdout).expect("migrate JSON");
    assert_eq!(report["schema"], "shepherd-migrate-v5");
    assert!(report["execution"].is_null());
    assert!(namespace.join("docs/specs/legacy.md").is_file());
    assert!(!namespace.join("docs/legacy.md").exists());
    cleanup(&root);
}

// GI2 — doctor is honest: a namespace whose identity vanished after a
// healthy `init` must fail loudly, not report `status: ok`.
#[test]
fn doctor_fails_loudly_on_a_namespace_missing_project_identity() {
    let root = fixture("doctor-missing-identity");
    let initialized = invoke(&root, &["init", "--confirm"]);
    assert_eq!(
        initialized.status.code(),
        Some(0),
        "stderr={}",
        text(&initialized.stderr)
    );

    std::fs::remove_file(identity_path(&root)).expect("remove project identity");

    let sick = invoke(&root, &["doctor"]);
    assert_eq!(sick.status.code(), Some(3));
    let stdout = text(&sick.stdout);
    assert!(
        !stdout.contains("status: ok"),
        "doctor must not report ok on a namespace missing identity: {stdout}"
    );
    assert!(
        stdout.contains("project identity is absent"),
        "doctor must name the missing identity: {stdout}"
    );

    let sick_json = invoke(&root, &["doctor", "--json"]);
    assert_eq!(sick_json.status.code(), Some(3));
    let report: serde_json::Value = serde_json::from_slice(&sick_json.stdout).expect("doctor JSON");
    assert_eq!(report["ok"], false);
    cleanup(&root);
}

// GI3 — scaffolding is atomic: a conflicting `project.json` (here, a
// directory sitting where the identity file must go) must leave nothing
// this invocation created behind, and must never touch what was already
// there.
#[test]
fn init_rolls_back_everything_it_created_when_identity_cannot_be_written() {
    let root = fixture("atomic-rollback");
    let namespace = root.join(".shepherd");
    std::fs::create_dir_all(namespace.join("project.json"))
        .expect("pre-create a conflicting project.json directory");

    let result = invoke(&root, &["init", "--confirm"]);
    assert_ne!(
        result.status.code(),
        Some(0),
        "stdout={}",
        text(&result.stdout)
    );

    for directory in ["docs", "ctx", "runs"] {
        assert!(
            !namespace.join(directory).exists(),
            "{directory} must be rolled back after a failed init"
        );
    }
    assert!(
        !namespace.join("shepherd.toml").exists(),
        "shepherd.toml must be rolled back after a failed init"
    );
    assert!(
        !namespace.join("shepherd.db").exists(),
        "shepherd.db must be rolled back after a failed init"
    );
    assert!(
        namespace.join("project.json").is_dir(),
        "the pre-existing project.json directory must survive untouched"
    );
    cleanup(&root);
}

// GI4 — a repeated `init --confirm` is idempotent, and a namespace that
// already has `shepherd.toml` and `shepherd.db` but lost its identity heals
// to exactly one row, rather than duplicating it.
#[test]
fn init_is_idempotent_and_heals_a_namespace_missing_only_identity() {
    let root = fixture("idempotent-heal");
    let database = registry_path(&root);
    let identity = identity_path(&root);

    let first = invoke(&root, &["init", "--confirm"]);
    assert_eq!(
        first.status.code(),
        Some(0),
        "stderr={}",
        text(&first.stderr)
    );
    let first_id = project_identity_id(&identity);
    assert_eq!(count_project_rows(&database), 1);

    let repeated = invoke(&root, &["init", "--confirm"]);
    assert_eq!(
        repeated.status.code(),
        Some(0),
        "stderr={}",
        text(&repeated.stderr)
    );
    assert_eq!(
        project_identity_id(&identity),
        first_id,
        "a repeated init must not mint a new identity"
    );
    assert_eq!(
        count_project_rows(&database),
        1,
        "a repeated init must not duplicate the projects row"
    );

    // Simulate the operator's partially-scaffolded state: `shepherd.toml`
    // and `shepherd.db` present, identity absent, no registered row.
    std::fs::remove_file(&identity).expect("remove project identity");
    rusqlite::Connection::open(&database)
        .expect("open registry")
        .execute("DELETE FROM projects", [])
        .expect("clear projects row");

    let healed = invoke(&root, &["init", "--confirm"]);
    assert_eq!(
        healed.status.code(),
        Some(0),
        "stderr={}",
        text(&healed.stderr)
    );
    assert!(identity.is_file(), "heal must recreate project identity");
    assert_eq!(
        count_project_rows(&database),
        1,
        "heal must register exactly one project row"
    );
    cleanup(&root);
}

// GD1 — doctor reports install integrity. A binary that is a real, native,
// byte-for-byte copy of `shepherd` but deliberately back-dated years into
// the past must be reported as stale when it is what `PATH` resolves,
// because a version-only check cannot tell it apart from the binary
// running this very check: both report the identical `shepherd-cli`
// version throughout the incident this gate exists to catch.
#[test]
fn doctor_reports_a_stale_shepherd_resolved_from_path() {
    let root = fixture("stale-path");
    let initialized = invoke(&root, &["init", "--confirm"]);
    assert_eq!(
        initialized.status.code(),
        Some(0),
        "stderr={}",
        text(&initialized.stderr)
    );

    let scratch = root.join("scratch-path");
    std::fs::create_dir_all(&scratch).expect("create scratch PATH directory");
    let stale = scratch.join("shepherd");
    std::fs::copy(binary(), &stale).expect("copy the native binary into the scratch PATH entry");
    let ancient = SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000_000);
    std::fs::OpenOptions::new()
        .write(true)
        .open(&stale)
        .expect("open the stale copy to back-date it")
        .set_modified(ancient)
        .expect("back-date the stale binary's mtime");

    let path = format!(
        "{}:{}",
        scratch.display(),
        std::env::var("PATH").unwrap_or_default()
    );

    let json = invoke_with_path(&root, &["doctor", "--json"], &path);
    assert_eq!(json.status.code(), Some(0), "stderr={}", text(&json.stderr));
    let report: serde_json::Value = serde_json::from_slice(&json.stdout).expect("doctor JSON");
    assert_eq!(
        report["ok"], true,
        "a stale PATH binary is an environment finding, never a namespace defect: {report}"
    );
    assert_eq!(
        report["resolved_shepherd_path"].as_str().map(PathBuf::from),
        Some(stale.clone()),
        "doctor must resolve PATH in order and find the scratch entry first: {report}"
    );
    assert_eq!(
        report["resolved_shepherd_native"], true,
        "a byte-for-byte copy of the native binary must classify as native: {report}"
    );
    let skew = report["resolved_shepherd_skew_seconds"]
        .as_i64()
        .unwrap_or_else(|| {
            panic!("doctor must report a numeric skew for a resolvable stale binary: {report}")
        });
    assert!(
        skew < 0,
        "a binary back-dated to 2001 must read as stale relative to the freshly built test binary: {report}"
    );

    let text_report = invoke_with_path(&root, &["doctor"], &path);
    assert_eq!(
        text_report.status.code(),
        Some(0),
        "stderr={}",
        text(&text_report.stderr)
    );
    let rendered = text(&text_report.stdout);
    assert!(
        rendered.contains("stale") || rendered.contains("older"),
        "doctor's text report must name the skew: {rendered}"
    );

    cleanup(&root);
}

// GD1 companion — a checkout with nothing on `PATH` at all is a real,
// common developer workflow (`cargo run` / `cargo test` only). Doctor must
// still produce a sensible report, not a crash, and must not fail a
// healthy namespace purely because nothing is installed system-wide.
#[test]
fn doctor_reports_a_sensible_result_when_nothing_answers_shepherd_on_path() {
    let root = fixture("no-path-binary");
    let initialized = invoke(&root, &["init", "--confirm"]);
    assert_eq!(
        initialized.status.code(),
        Some(0),
        "stderr={}",
        text(&initialized.stderr)
    );

    let path = path_without_any_shepherd();

    let json = invoke_with_path(&root, &["doctor", "--json"], &path);
    assert_eq!(json.status.code(), Some(0), "stderr={}", text(&json.stderr));
    let report: serde_json::Value = serde_json::from_slice(&json.stdout).expect("doctor JSON");
    assert_eq!(
        report["ok"], true,
        "no PATH binary at all must not fail a healthy namespace: {report}"
    );
    assert!(
        report["resolved_shepherd_path"].is_null(),
        "nothing on PATH must resolve to nothing: {report}"
    );
    assert!(
        report["warnings"]
            .as_array()
            .expect("warnings must be an array")
            .iter()
            .any(|warning| warning.as_str().is_some_and(|text| text.contains("PATH"))),
        "doctor must still say something about the missing PATH binary: {report}"
    );
    cleanup(&root);
}

/// Recursively collects every `*.rs` file under `dir`. Dependency-free by
/// design (`std::fs` only) — used solely by the sole-inserter invariant
/// test below.
fn collect_rs_files(dir: &Path) -> Vec<PathBuf> {
    let mut files = Vec::new();
    for entry in
        std::fs::read_dir(dir).unwrap_or_else(|error| panic!("read_dir {}: {error}", dir.display()))
    {
        let entry = entry.expect("directory entry");
        let path = entry.path();
        if entry.file_type().expect("entry file type").is_dir() {
            files.extend(collect_rs_files(&path));
        } else if path.extension().is_some_and(|extension| extension == "rs") {
            files.push(path);
        }
    }
    files
}

// Five commands resolve "the current project" by taking the single row out
// of `projects` (`SELECT id FROM projects ORDER BY id LIMIT 1`) rather than
// resolving by identity. That is only safe because exactly one production
// call site ever inserts a row: `wave_c_bootstrap.rs`'s register path. The
// moment a second inserter exists anywhere under `crates/*/src/`, a
// namespace can hold two rows and all five call sites start picking a
// project alphabetically instead of by identity — a failure mode that has
// already been misdiagnosed once this sprint, when a test fixture hand-
// inserted a competing row. This test makes the "exactly one inserter"
// invariant load-bearing instead of implicit.
#[test]
fn wave_c_bootstrap_remains_the_sole_production_inserter_of_projects() {
    let workspace_root = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../..")
        .canonicalize()
        .expect("canonicalize workspace root from CARGO_MANIFEST_DIR");
    let crates_dir = workspace_root.join("crates");

    let mut sources = Vec::new();
    for entry in std::fs::read_dir(&crates_dir).expect("read crates/ directory") {
        let entry = entry.expect("crates/ directory entry");
        if !entry.file_type().expect("crate entry file type").is_dir() {
            continue;
        }
        let src_dir = entry.path().join("src");
        if src_dir.is_dir() {
            sources.extend(collect_rs_files(&src_dir));
        }
    }

    let mut offenders: Vec<String> = sources
        .into_iter()
        .filter(|path| {
            std::fs::read_to_string(path)
                .map(|contents| {
                    contents
                        .to_ascii_uppercase()
                        .contains("INSERT INTO PROJECTS")
                })
                .unwrap_or(false)
        })
        .map(|path| {
            path.strip_prefix(&workspace_root)
                .unwrap_or(path.as_path())
                .to_string_lossy()
                .replace('\\', "/")
        })
        .collect();
    offenders.sort();

    assert_eq!(
        offenders,
        vec!["crates/cli/src/cmd/wave_c_bootstrap.rs".to_string()],
        "found {} production `INSERT INTO projects` writer(s): {offenders:?}. Five commands \
         resolve \"the current project\" with `SELECT id FROM projects ORDER BY id LIMIT 1` \
         (crates/cli/src/cmd/wave_h_execution.rs:563, crates/cli/src/cmd/wave_d_planning.rs:785, \
         crates/cli/src/cmd/wave_e_coordination.rs:121, crates/cli/src/cmd/wave_g_coordination.rs:541, \
         crates/cli/src/cmd/wave_f_knowledge.rs:409), so a second inserter makes all five pick a \
         project alphabetically instead of by identity. The correct fix is a shared resolver \
         keyed to `.shepherd/project.json`, not another `INSERT INTO projects` call site.",
        offenders.len(),
    );
}