teamctl 0.9.0

Declarative CLI for running persistent AI agent teams.
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
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
//! End-to-end integration test for the `teamctl` binary.
//!
//! Intentionally avoids `tmux` + `claude` so it runs on CI without a TTY:
//! drives only `validate` and `send` (which talk to SQLite directly), then
//! walks the mailbox to confirm the message landed.

use std::fs;
use std::process::Command;

use tempfile::tempdir;

fn bin() -> std::path::PathBuf {
    env!("CARGO_BIN_EXE_teamctl").into()
}

fn seed_compose(root: &std::path::Path) {
    fs::write(
        root.join("team-compose.yaml"),
        r#"
version: 2
broker:
  type: sqlite
  path: state/mailbox.db
supervisor:
  type: tmux
  tmux_prefix: a-
projects:
  - file: projects/hello.yaml
"#,
    )
    .unwrap();
    fs::create_dir_all(root.join("projects")).unwrap();
    fs::write(
        root.join("projects/hello.yaml"),
        r#"
version: 2
project:
  id: hello
  name: Hello
  cwd: .
channels:
  - name: all
    members: "*"
managers:
  manager:
    runtime: claude-code
    model: claude-opus-4-8
    can_dm: [dev]
    can_broadcast: [all]
workers:
  dev:
    runtime: claude-code
    model: claude-sonnet-4-6
    reports_to: manager
    can_dm: [manager]
    can_broadcast: [all]
"#,
    )
    .unwrap();
}

#[test]
fn validate_passes_on_clean_compose() {
    let tmp = tempdir().unwrap();
    seed_compose(tmp.path());
    let out = Command::new(bin())
        .args(["--root", tmp.path().to_str().unwrap(), "validate"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("1 project"), "got: {stdout}");
    assert!(stdout.contains("2 agent sessions"), "got: {stdout}");
}

// ── T-325: init Stage 4 emits cascading role_prompt list form ───────────
// The init skill writes role_prompt: as a YAML list. Managers cascade
// `[_base.md, <name>.md]`; workers cascade
// `[_base.md, _worker.md, <name>.md]` (asymmetric tier-shape by owner
// direction, matching the merged #295 dogfood convention). The render
// layer already concats list form at boot (covered in team-core tests);
// this test pins the CLI-level contract — validate accepts what the
// skill emits, so the skill's emission round-trips through the rest of
// the stack without schema regression.
#[test]
fn validate_accepts_cascading_role_prompt_list_form() {
    let tmp = tempdir().unwrap();
    fs::write(
        tmp.path().join("team-compose.yaml"),
        r#"
version: 2
broker:
  type: sqlite
  path: state/mailbox.db
supervisor:
  type: tmux
  tmux_prefix: cascade-
projects:
  - file: projects/cascade.yaml
"#,
    )
    .unwrap();
    fs::create_dir_all(tmp.path().join("projects")).unwrap();
    fs::write(
        tmp.path().join("projects/cascade.yaml"),
        r#"
version: 2
project:
  id: cascade
  name: Cascade
  cwd: .
channels:
  - name: all
    members: "*"
managers:
  pm:
    runtime: claude-code
    model: claude-opus-4-8
    role_prompt:
      - roles/_base.md
      - roles/pm.md
    can_dm: [eng]
    can_broadcast: [all]
workers:
  eng:
    runtime: claude-code
    model: claude-sonnet-4-6
    reports_to: pm
    role_prompt:
      - roles/_base.md
      - roles/_worker.md
      - roles/eng.md
    can_dm: [pm]
    can_broadcast: [all]
"#,
    )
    .unwrap();

    let out = Command::new(bin())
        .args(["--root", tmp.path().to_str().unwrap(), "validate"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "validate should accept cascade list-form role_prompt; stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("1 project"), "got: {stdout}");
    assert!(stdout.contains("2 agent sessions"), "got: {stdout}");
}

#[test]
fn validate_fails_on_unknown_dm_target() {
    let tmp = tempdir().unwrap();
    seed_compose(tmp.path());
    let path = tmp.path().join("projects/hello.yaml");
    let contents = fs::read_to_string(&path)
        .unwrap()
        .replace("can_dm: [dev]", "can_dm: [ghost]");
    fs::write(&path, contents).unwrap();

    let out = Command::new(bin())
        .args(["--root", tmp.path().to_str().unwrap(), "validate"])
        .output()
        .unwrap();
    assert!(!out.status.success());
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(
        stderr.contains("unknown agent `ghost`"),
        "stderr was: {stderr}"
    );
}

#[test]
fn send_injects_into_mailbox() {
    let tmp = tempdir().unwrap();
    seed_compose(tmp.path());

    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "send",
            "hello:manager",
            "hi there",
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let db = tmp.path().join("state/mailbox.db");
    let conn = rusqlite::Connection::open(&db).unwrap();
    let (sender, recipient, text): (String, String, String) = conn
        .query_row(
            "SELECT sender, recipient, text FROM messages ORDER BY id DESC LIMIT 1",
            [],
            |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
        )
        .unwrap();
    assert_eq!(sender, "cli");
    assert_eq!(recipient, "hello:manager");
    assert_eq!(text, "hi there");
}

// ── T-091: --help surfaces the version ──────────────────────────────────

#[test]
fn help_header_includes_version_from_cargo_pkg_version() {
    // The clap `version` attribute on the top-level Cli derive plus a
    // help_template that includes `{name} {version}` puts the version
    // line at the top of `teamctl --help`. Pinning it here so a future
    // template edit can't silently drop the line; the assertion uses
    // CARGO_PKG_VERSION so it tracks the crate version automatically.
    let out = Command::new(bin()).arg("--help").output().unwrap();
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    let expected = format!("teamctl {}", env!("CARGO_PKG_VERSION"));
    assert!(
        stdout.contains(&expected),
        "expected `{expected}` in --help output; got:\n{stdout}"
    );
}

#[test]
fn version_flag_still_prints_version() {
    // Companion to the help-header test: `--version` keeps working and
    // produces the same string the help header surfaces.
    let out = Command::new(bin()).arg("--version").output().unwrap();
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(
        stdout.contains(env!("CARGO_PKG_VERSION")),
        "expected version in --version output; got: {stdout}"
    );
}

// ── T-050: teamctl init template/force coverage ─────────────────────────

#[test]
fn init_blank_template_scaffolds_minimal_tree() {
    // Pins the `blank` template's surface so a future template
    // refactor can't silently drop its files. Asserts (a) every
    // declared file lands at `.team/<relpath>` and (b) the resulting
    // tree validates.
    let tmp = tempdir().unwrap();
    let out = Command::new(bin())
        .current_dir(tmp.path())
        .args(["init", "starter", "--template", "blank", "--yes"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "init blank stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // Blank scaffolds no agents, so `lead_attach_target` → None and the
    // `Next:` block must omit the attach/bot hints. Guards the agentless
    // branch end-to-end (complements the agent-bearing ideate assertion).
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        !stdout.contains("teamctl attach"),
        "agentless blank must not suggest attach; got:\n{stdout}"
    );
    assert!(
        !stdout.contains("teamctl bot setup"),
        "agentless blank must not suggest bot setup; got:\n{stdout}"
    );

    let team_dir = tmp.path().join("starter/.team");
    assert!(
        team_dir.is_dir(),
        "expected .team/ at {}",
        team_dir.display()
    );
    assert!(
        team_dir.join("team-compose.yaml").is_file(),
        "blank template must include team-compose.yaml"
    );
    assert!(
        team_dir.join("projects/main.yaml").is_file(),
        "blank template must include projects/main.yaml"
    );
    assert!(
        team_dir.join(".env.example").is_file(),
        "blank template must include .env.example (from _common)"
    );
    assert!(
        team_dir.join(".gitignore").is_file(),
        "blank template must include .gitignore (from _common)"
    );

    // The scaffolded tree must validate. Exercises the substitution
    // pass + the schema together, so a typo in the template body
    // surfaces here rather than at first user-run.
    let validate = Command::new(bin())
        .args(["--root", team_dir.to_str().unwrap(), "validate"])
        .output()
        .unwrap();
    assert!(
        validate.status.success(),
        "blank template validate stderr: {}",
        String::from_utf8_lossy(&validate.stderr)
    );
}

#[test]
fn init_essentials_template_scaffolds_two_project_tree() {
    // T-206: `essentials` ships a two-project layout — blank `main`
    // for the operator + `ops` with the `ops` agent. Pins the
    // file shape so a future template refactor can't silently drop
    // any of the seven files, and asserts the tree validates so a
    // typo in the ops agent's compose surfaces here.
    let tmp = tempdir().unwrap();
    let out = Command::new(bin())
        .current_dir(tmp.path())
        .args(["init", "starter", "--template", "essentials", "--yes"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "init essentials stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let team_dir = tmp.path().join("starter/.team");
    for relpath in [
        "team-compose.yaml",
        "projects/main.yaml",
        "projects/ops.yaml",
        "roles/ops.md",
        ".env.example",
        ".gitignore",
        "README.md",
    ] {
        assert!(
            team_dir.join(relpath).is_file(),
            "essentials template must include {relpath}"
        );
    }

    let validate = Command::new(bin())
        .args(["--root", team_dir.to_str().unwrap(), "validate"])
        .output()
        .unwrap();
    assert!(
        validate.status.success(),
        "essentials template validate stderr: {}",
        String::from_utf8_lossy(&validate.stderr)
    );
}

#[test]
fn init_ideate_and_build_template_scaffolds_with_subagents() {
    // T-382: the `ideate-and-build` template is the flagship showcase —
    // its role prompts reference a stable of sub-agents that must
    // actually ship. Pin the file shape so a template refactor can't
    // silently drop a sub-agent markdown (which would leave the roles
    // pointing at nothing), and assert the tree validates.
    let tmp = tempdir().unwrap();
    let out = Command::new(bin())
        .current_dir(tmp.path())
        .args(["init", "studio", "--template", "ideate-and-build", "--yes"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "init ideate-and-build stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // The hero-path `Next:` block must wire the derived lead manager into
    // the attach hint end-to-end. `init` derives it from the just-written
    // compose on disk — a different parse path than the `lead_attach_target`
    // unit tests exercise. ideate-and-build's workers report to `executor`,
    // so the hint is `teamctl attach main:executor`, not the back-channel
    // `compass`; an agent-bearing template also offers `teamctl bot setup`.
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(
        stdout.contains("teamctl attach main:executor"),
        "Next: block must suggest attaching to the lead manager; got:\n{stdout}"
    );
    assert!(
        stdout.contains("teamctl bot setup"),
        "Next: block must offer bot setup for an agent-bearing template; got:\n{stdout}"
    );

    let team_dir = tmp.path().join("studio/.team");
    for relpath in [
        "team-compose.yaml",
        "projects/main.yaml",
        "charter.md",
        "subagents/code-investigator.md",
        "subagents/implementer.md",
        "subagents/test-author.md",
        "subagents/qa-tester.md",
        "subagents/pr-narrator.md",
        "subagents/code-roaster.md",
        "subagents/memory-writer.md",
        "subagents/product-researcher.md",
        "subagents/feasibility-analyst.md",
        "subagents/deep-research.md",
        "subagents/learn.md",
        "subagents/pr-summarizer.md",
        "subagents/ideator.md",
        "subagents/code-review.md",
        "subagents/security-review.md",
    ] {
        assert!(
            team_dir.join(relpath).is_file(),
            "ideate-and-build template must include {relpath}"
        );
    }

    let validate = Command::new(bin())
        .args(["--root", team_dir.to_str().unwrap(), "validate"])
        .output()
        .unwrap();
    assert!(
        validate.status.success(),
        "ideate-and-build template validate stderr: {}",
        String::from_utf8_lossy(&validate.stderr)
    );
}

#[test]
fn ideate_and_build_template_renders_per_agent_subagents() {
    // T-382: the `subagents:` declared in the template compose must
    // resolve through the real render path into Claude Code's `--agents`
    // JSON — not just exist as files. Load the shipped template compose
    // straight from the crate assets and assert each agent gets exactly
    // its declared stable (and the executor, which declares none, gets
    // nothing). This is the authoritative check that the template's
    // role-prompt sub-agent references are backed by real config.
    let template =
        std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("assets/templates/ideate-and-build");
    let compose = team_core::compose::Compose::load(&template).unwrap();

    let expect_names = |agent: &str, expected: &[&str]| {
        let h = compose
            .agents()
            .find(|h| h.agent == agent)
            .unwrap_or_else(|| panic!("agent `{agent}` not found in template compose"));
        let json = team_core::render::render_subagents(&compose, h)
            .unwrap()
            .unwrap_or_else(|| panic!("agent `{agent}` rendered no sub-agents"));
        let v: serde_json::Value = serde_json::from_str(&json).unwrap();
        let obj = v.as_object().expect("agents json is an object");
        let mut got: Vec<&str> = obj.keys().map(String::as_str).collect();
        got.sort_unstable();
        let mut want = expected.to_vec();
        want.sort_unstable();
        assert_eq!(got, want, "sub-agent set for `{agent}`");
    };

    let engineer_stable = [
        "code-investigator",
        "implementer",
        "test-author",
        "qa-tester",
        "pr-narrator",
        "code-roaster",
        "code-review",
        "security-review",
    ];
    expect_names("engineer_1", &engineer_stable);
    expect_names("engineer_2", &engineer_stable);
    expect_names(
        "compass",
        &[
            "memory-writer",
            "code-investigator",
            "product-researcher",
            "feasibility-analyst",
            "deep-research",
            "learn",
            "ideator",
        ],
    );
    // The Executor gets a single sub-agent: pr-summarizer, so it can turn
    // an engineer's ready PR into a plain-language summary for the operator.
    expect_names("executor", &["pr-summarizer"]);
}

#[test]
fn init_yes_without_template_defaults_to_essentials() {
    // T-206: the non-interactive default changed from `solo` to
    // `essentials`. Pin the contract — `--yes` with no `--template`
    // must land the operator on the `essentials` two-project shape,
    // not the bare blank tree.
    let tmp = tempdir().unwrap();
    let out = Command::new(bin())
        .current_dir(tmp.path())
        .args(["init", "starter", "--yes"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "init --yes (no template) stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    let team_dir = tmp.path().join("starter/.team");
    // The `ops` project file + `ops` role are essentials-only
    // markers; their presence proves the default landed on
    // essentials, not blank.
    assert!(
        team_dir.join("projects/ops.yaml").is_file(),
        "--yes default must land essentials (projects/ops.yaml missing)"
    );
    assert!(
        team_dir.join("roles/ops.md").is_file(),
        "--yes default must land essentials (roles/ops.md missing)"
    );
}

#[test]
fn init_template_guided_with_yes_errors() {
    // T-206: `guided` execs `claude /teamctl:init` after an
    // interactive confirm-intent prompt, so it can't run under
    // `--yes`. The CLI rejects the combo up front with a clear
    // message rather than silently flipping to a different template.
    let tmp = tempdir().unwrap();
    let out = Command::new(bin())
        .current_dir(tmp.path())
        .args(["init", "starter", "--template", "guided", "--yes"])
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "`--template guided --yes` must exit non-zero; stdout: {} stderr: {}",
        String::from_utf8_lossy(&out.stdout),
        String::from_utf8_lossy(&out.stderr)
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("guided") && stderr.contains("interactive"),
        "expected error to name `guided` + `interactive`; got: {stderr}"
    );
    // And no `.team/` directory was created — the failure path
    // doesn't leave a partial scaffold behind.
    assert!(
        !tmp.path().join("starter/.team").exists(),
        "rejected --template guided --yes must not create a .team/ tree"
    );
}

#[test]
fn init_force_overwrites_existing_dot_team_cleanly() {
    // The refusal path (no `--force` → exit non-zero, leave existing
    // tree intact) is covered elsewhere. This pins the positive
    // path: `--force` removes the prior `.team/` entirely (no orphan
    // files survive) and lays down the new template fresh.
    let tmp = tempdir().unwrap();

    // First init — seed with `essentials` so we have a richer tree
    // (the `roles/ops.md` marker doubles as the wipe-check).
    let out = Command::new(bin())
        .current_dir(tmp.path())
        .args(["init", "myteam", "--template", "essentials", "--yes"])
        .output()
        .unwrap();
    assert!(out.status.success());

    let team_dir = tmp.path().join("myteam/.team");
    let sentinel = team_dir.join("sentinel-must-not-survive.txt");
    fs::write(&sentinel, "this file should be wiped by --force").unwrap();
    assert!(sentinel.exists(), "sentinel seeded for the test");

    // Second init with --force on the same target.
    let out = Command::new(bin())
        .current_dir(tmp.path())
        .args(["init", "myteam", "--template", "blank", "--force", "--yes"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "init --force stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );

    // Sentinel from the prior tree is gone — `--force` did a clean
    // remove-then-recreate, not a merge.
    assert!(
        !sentinel.exists(),
        "sentinel survived --force; .team/ was not cleanly replaced"
    );

    // The new template's structure is in place.
    assert!(team_dir.join("team-compose.yaml").is_file());
    assert!(team_dir.join("projects/main.yaml").is_file());
    // The prior `essentials` template's roles/ops.md must be
    // gone — `blank` has no roles/ so a stale file there would prove
    // --force merged rather than replaced.
    assert!(
        !team_dir.join("roles/ops.md").exists(),
        "prior essentials template's roles/ops.md should be wiped"
    );
}

// ── T-033: cli `teamctl approve` after TTL elapsed ──────────────────────

#[test]
fn approve_after_ttl_elapsed_returns_no_pending_error() {
    // Pin the contract that `teamctl approve` cannot resurrect a row that
    // `teamctl gc` has already moved to a terminal state. The CLI's
    // `WHERE status='pending'` clause is what enforces this; the test
    // would fail if a future change relaxed it (e.g. dropped the status
    // pin, or pre-loaded the row before the gc check).
    let tmp = tempdir().unwrap();
    seed_compose(tmp.path());

    // Bootstrap the mailbox so we can write directly. `seed_compose`
    // doesn't create state/, so the directory has to come up first.
    let db = tmp.path().join("state/mailbox.db");
    std::fs::create_dir_all(db.parent().unwrap()).unwrap();
    let conn = rusqlite::Connection::open(&db).unwrap();
    team_core::mailbox::ensure(&conn).unwrap();

    // Seed a pending approval whose TTL is already in the past
    // (requested_at = expires_at = T-1h, T-30m). delivered_at = NULL so
    // gc routes it to `undeliverable`; the test would still pass against
    // `expired` if delivered_at were set.
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs_f64();
    let requested_at = now - 3600.0;
    let expires_at = now - 1800.0;
    conn.execute(
        "INSERT INTO approvals (project_id, agent_id, action, summary, status,
                                requested_at, expires_at)
         VALUES ('hello', 'manager', 'publish', 'old request', 'pending', ?1, ?2)",
        rusqlite::params![requested_at, expires_at],
    )
    .unwrap();
    let id: i64 = conn.last_insert_rowid();
    drop(conn);

    // Run gc — flips the row to `undeliverable` (delivered_at IS NULL).
    let gc_out = Command::new(bin())
        .args(["--root", tmp.path().to_str().unwrap(), "gc"])
        .output()
        .unwrap();
    assert!(
        gc_out.status.success(),
        "gc stderr: {}",
        String::from_utf8_lossy(&gc_out.stderr)
    );

    // Confirm the row is no longer pending after gc.
    let conn = rusqlite::Connection::open(&db).unwrap();
    let status: String = conn
        .query_row(
            "SELECT status FROM approvals WHERE id = ?1",
            rusqlite::params![id],
            |r| r.get(0),
        )
        .unwrap();
    assert_eq!(
        status, "undeliverable",
        "gc should mark expired-undelivered row"
    );
    drop(conn);

    // Now `teamctl approve <id>` must fail with the canonical error and
    // must not flip the terminal-state fields back.
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "approve",
            &id.to_string(),
        ])
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "approve on terminal row should exit non-zero"
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains(&format!("no pending approval with id {id}")),
        "expected canonical error, got: {stderr}"
    );

    // Row's terminal-state fields unchanged. With the T-036 ordering
    // fix in place (status pin first, delivered_at flip second), the
    // CLI must NOT have flipped delivered_at on this terminal row —
    // the invariant is `undeliverable ↔ delivered_at IS NULL`, and
    // breaking it would mean the CLI's status-check came after the
    // delivered_at write again.
    let conn = rusqlite::Connection::open(&db).unwrap();
    let (status, decided_by, delivered_at): (String, Option<String>, Option<f64>) = conn
        .query_row(
            "SELECT status, decided_by, delivered_at FROM approvals WHERE id = ?1",
            rusqlite::params![id],
            |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
        )
        .unwrap();
    assert_eq!(status, "undeliverable");
    assert!(
        decided_by.is_none() || decided_by.as_deref() != Some("cli"),
        "cli should not have stamped decided_by on a terminal row"
    );
    assert!(
        delivered_at.is_none(),
        "delivered_at must stay NULL on undeliverable row (invariant); got {delivered_at:?}"
    );
}

// ── T-035 PR B: reload --dry-run ────────────────────────────────────────

#[test]
fn reload_dry_run_with_no_prior_lists_added_and_does_not_apply() {
    // No `state/applied.json` on disk → every agent in the compose
    // shows up as `added (dry run)`. Crucially, the dry-run path
    // must not write `state/applied.json`, must not render env/mcp
    // files, and must not invoke tmux. We assert all four.
    let tmp = tempdir().unwrap();
    seed_compose(tmp.path());

    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "reload",
            "--dry-run",
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(
        stdout.contains("added") && stdout.contains("(dry run)"),
        "expected added/(dry run) lines, got: {stdout}"
    );
    assert!(
        stdout.contains("hello:manager"),
        "expected hello:manager in plan, got: {stdout}"
    );
    assert!(
        stdout.contains("hello:dev"),
        "expected hello:dev in plan, got: {stdout}"
    );

    // Side-effect-free: applied.json must not exist after dry-run.
    let applied = tmp.path().join("state/applied.json");
    assert!(
        !applied.exists(),
        "dry-run wrote applied.json at {}",
        applied.display()
    );
    // Render outputs also must not have been written.
    let envs = tmp.path().join("state/envs");
    assert!(
        !envs.exists(),
        "dry-run rendered env files at {}",
        envs.display()
    );
}

// ── T-133: scoped <project-name> arg on up/down/reload ──────────────────

fn seed_two_projects(root: &std::path::Path) {
    fs::write(
        root.join("team-compose.yaml"),
        r#"
version: 2
broker:
  type: sqlite
  path: state/mailbox.db
supervisor:
  type: tmux
  tmux_prefix: a-
projects:
  - file: projects/alpha.yaml
  - file: projects/beta.yaml
"#,
    )
    .unwrap();
    fs::create_dir_all(root.join("projects")).unwrap();
    fs::write(
        root.join("projects/alpha.yaml"),
        r#"
version: 2
project:
  id: alpha
  name: Alpha
  cwd: .
managers:
  manager:
    runtime: claude-code
    model: claude-opus-4-8
    can_dm: [dev]
workers:
  dev:
    runtime: claude-code
    model: claude-sonnet-4-6
    reports_to: manager
    can_dm: [manager]
"#,
    )
    .unwrap();
    fs::write(
        root.join("projects/beta.yaml"),
        r#"
version: 2
project:
  id: beta
  name: Beta
  cwd: .
managers:
  manager:
    runtime: claude-code
    model: claude-opus-4-8
    can_dm: [dev]
workers:
  dev:
    runtime: claude-code
    model: claude-sonnet-4-6
    reports_to: manager
    can_dm: [manager]
"#,
    )
    .unwrap();
}

#[test]
fn reload_dry_run_with_unknown_project_lists_known_and_exits_nonzero() {
    // Resolution miss: error names the rejected input AND lists every
    // available project.id so the operator can copy-paste a fix.
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "reload",
            "--dry-run",
            "ghost",
        ])
        .output()
        .unwrap();
    assert!(!out.status.success(), "expected nonzero exit");
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(stderr.contains("ghost"), "names rejected input: {stderr}");
    assert!(stderr.contains("alpha"), "lists alpha: {stderr}");
    assert!(stderr.contains("beta"), "lists beta: {stderr}");
}

#[test]
fn reload_dry_run_scoped_to_project_lists_only_that_project() {
    // The plan covers the whole compose, but the scoped run filters
    // it down to the named project. alpha's two agents land as
    // `added`; beta's never appear in the output.
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "reload",
            "--dry-run",
            "alpha",
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("alpha:manager"), "got: {stdout}");
    assert!(stdout.contains("alpha:dev"), "got: {stdout}");
    assert!(
        !stdout.contains("beta:manager"),
        "scoped plan must omit beta: {stdout}"
    );
    assert!(
        !stdout.contains("beta:dev"),
        "scoped plan must omit beta: {stdout}"
    );
}

#[test]
fn reload_dry_run_resolves_project_by_filename_stem() {
    // Filename fallback: operator types `alpha` (matches both file
    // stem and project.id here, but the filename path is exercised
    // when project.id differs from the stem — covered in unit
    // tests). Pinning the integration round-trip so the resolver is
    // wired into the reload subcommand.
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "reload",
            "--dry-run",
            "beta",
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("beta:manager"), "got: {stdout}");
    assert!(!stdout.contains("alpha:"), "got: {stdout}");
}

#[test]
fn reload_no_arg_unchanged_lists_every_project() {
    // Back-compat pin: omitting the arg keeps the original behavior
    // — every agent across every project shows in the plan.
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "reload",
            "--dry-run",
        ])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("alpha:manager"), "got: {stdout}");
    assert!(stdout.contains("beta:manager"), "got: {stdout}");
}

// ── T-010: source-aware override warning ─────────────────────────────────

/// Run `teamctl validate` against `cwd` with a clean env, returning stderr.
/// `extra_env` lets each test inject the override under test (TEAMCTL_ROOT,
/// TEAMCTL_QUIET, ...). `home` isolates the registered-context store at
/// `$HOME/.config/teamctl/contexts.json`.
fn run_validate_with_env(
    cwd: &std::path::Path,
    home: &std::path::Path,
    extra_env: &[(&str, &str)],
    explicit_root: Option<&std::path::Path>,
) -> String {
    let mut cmd = Command::new(bin());
    cmd.env_clear()
        .env("HOME", home)
        .env("PATH", std::env::var_os("PATH").unwrap_or_default())
        .current_dir(cwd);
    for (k, v) in extra_env {
        cmd.env(k, v);
    }
    if let Some(r) = explicit_root {
        cmd.args(["--root", r.to_str().unwrap(), "validate"]);
    } else {
        cmd.arg("validate");
    }
    let out = cmd.output().unwrap();
    assert!(
        out.status.success(),
        "validate exited non-zero: stderr={}",
        String::from_utf8_lossy(&out.stderr)
    );
    String::from_utf8(out.stderr).unwrap()
}

/// Lay out a `.team/`-style root at `<dir>/.team/` (so cwd walk-up will find it).
fn seed_dot_team(dir: &std::path::Path) -> std::path::PathBuf {
    let root = dir.join(".team");
    fs::create_dir_all(&root).unwrap();
    seed_compose(&root);
    root
}

/// Strip ANSI colour codes so assertions are stable regardless of TTY.
fn strip_ansi(s: &str) -> String {
    let re = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();
    re.replace_all(s, "").to_string()
}

#[test]
fn warn_a_walk_up_silent() {
    let tmp = tempdir().unwrap();
    let home = tempdir().unwrap();
    let root = seed_dot_team(tmp.path());
    let _ = root; // walk-up will find it from cwd
    let stderr = run_validate_with_env(tmp.path(), home.path(), &[], None);
    let clean = strip_ansi(&stderr);
    assert!(
        !clean.contains("warning:"),
        "walk-up must not warn; stderr was: {clean}"
    );
}

#[test]
fn warn_b_env_root_warns() {
    let tmp = tempdir().unwrap();
    let home = tempdir().unwrap();
    let root = seed_dot_team(tmp.path());
    // CWD is also a valid walk-up target — warning still fires because the
    // resolved root came from env, not walk-up.
    let stderr = run_validate_with_env(
        tmp.path(),
        home.path(),
        &[("TEAMCTL_ROOT", root.to_str().unwrap())],
        None,
    );
    let clean = strip_ansi(&stderr);
    assert!(
        clean.contains("warning:") && clean.contains("TEAMCTL_ROOT"),
        "expected env warning; stderr was: {clean}"
    );
}

#[test]
fn warn_b_empty_env_root_treated_as_unset() {
    // `TEAMCTL_ROOT=""` (exported empty) should fall through to walk-up
    // rather than errorring on `canonicalize("")`.
    let tmp = tempdir().unwrap();
    let home = tempdir().unwrap();
    let _ = seed_dot_team(tmp.path());
    let stderr = run_validate_with_env(tmp.path(), home.path(), &[("TEAMCTL_ROOT", "")], None);
    let clean = strip_ansi(&stderr);
    assert!(
        !clean.contains("warning:"),
        "empty TEAMCTL_ROOT must fall through silently to walk-up; stderr was: {clean}"
    );
}

#[test]
fn warn_c_explicit_root_silent() {
    let tmp = tempdir().unwrap();
    let home = tempdir().unwrap();
    let root = seed_dot_team(tmp.path());
    // Even with TEAMCTL_ROOT in env, --root on the CLI is the deliberate intent.
    let stderr = run_validate_with_env(
        tmp.path(),
        home.path(),
        &[("TEAMCTL_ROOT", "/definitely/not/this")],
        Some(&root),
    );
    let clean = strip_ansi(&stderr);
    assert!(
        !clean.contains("warning:"),
        "--root must not warn; stderr was: {clean}"
    );
}

#[test]
fn warn_d_registered_context_no_longer_resolves_root() {
    // T-008: the registered-context fallback was retired. With no `.team/`
    // walked up to from cwd and a registered context pointing at a real
    // `.team/`, root resolution must error rather than silently fall back.
    let tmp = tempdir().unwrap();
    let unrelated_cwd = tempdir().unwrap();
    let home = tempdir().unwrap();
    let root = seed_dot_team(tmp.path());

    let cfg_dir = home.path().join(".config/teamctl");
    fs::create_dir_all(&cfg_dir).unwrap();
    let store = format!(
        r#"{{"current":"demo","contexts":{{"demo":"{}"}}}}"#,
        root.display()
    );
    fs::write(cfg_dir.join("contexts.json"), store).unwrap();

    let mut cmd = Command::new(bin());
    cmd.env_clear()
        .env("HOME", home.path())
        .env("PATH", std::env::var_os("PATH").unwrap_or_default())
        .current_dir(unrelated_cwd.path())
        .arg("validate");
    let out = cmd.output().unwrap();
    assert!(
        !out.status.success(),
        "validate must fail when no `.team/` is reachable from cwd"
    );
    let stderr = strip_ansi(&String::from_utf8_lossy(&out.stderr));
    assert!(
        stderr.contains("no `.team/team-compose.yaml`"),
        "expected no-team error, not a context fallback; stderr was: {stderr}"
    );
}

#[test]
fn context_subcommand_emits_deprecation_warning() {
    // T-008: every `teamctl context …` invocation should print a one-line
    // deprecation note to stderr while still doing its (now-cosmetic) job.
    let home = tempdir().unwrap();
    let mut cmd = Command::new(bin());
    cmd.env_clear()
        .env("HOME", home.path())
        .env("PATH", std::env::var_os("PATH").unwrap_or_default())
        .args(["context", "ls"]);
    let out = cmd.output().unwrap();
    assert!(
        out.status.success(),
        "context ls must still succeed: stderr={}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stderr = strip_ansi(&String::from_utf8_lossy(&out.stderr));
    assert!(
        stderr.contains("`teamctl context` is deprecated"),
        "expected deprecation warning; stderr was: {stderr}"
    );
}

#[test]
fn init_with_name_creates_team_folder_that_validates() {
    // T-045 / T-206: `teamctl init my-team --yes` should produce a
    // tree that `teamctl --root my-team/.team validate` accepts.
    // Default template under `--yes` is `essentials` post-T-206 —
    // two projects (`main` + `ops`) with a single `ops` agent.
    let tmp = tempdir().unwrap();
    let home = tempdir().unwrap();

    let init = Command::new(bin())
        .env_clear()
        .env("HOME", home.path())
        .env("PATH", std::env::var_os("PATH").unwrap_or_default())
        .current_dir(tmp.path())
        .args(["init", "my-team", "--yes"])
        .output()
        .unwrap();
    assert!(
        init.status.success(),
        "init failed: stderr={}",
        String::from_utf8_lossy(&init.stderr)
    );

    let team_dir = tmp.path().join("my-team/.team");
    for f in [
        "team-compose.yaml",
        "projects/main.yaml",
        "projects/ops.yaml",
        "roles/ops.md",
        ".env.example",
        ".gitignore",
        "README.md",
    ] {
        assert!(team_dir.join(f).is_file(), "missing scaffolded file: {f}");
    }

    let validate = Command::new(bin())
        .env_clear()
        .env("HOME", home.path())
        .env("PATH", std::env::var_os("PATH").unwrap_or_default())
        .args(["--root", team_dir.to_str().unwrap(), "validate"])
        .output()
        .unwrap();
    assert!(
        validate.status.success(),
        "validate failed: stderr={}",
        String::from_utf8_lossy(&validate.stderr)
    );
    let stdout = String::from_utf8_lossy(&validate.stdout);
    // Essentials ships 2 projects (`main` + `ops`) and 1 agent
    // (`ops`). The validate summary surfaces both counts.
    assert!(
        stdout.contains("ok") && stdout.contains("2 projects") && stdout.contains("1 agent"),
        "unexpected validate output: {stdout}"
    );
}

#[test]
fn init_refuses_existing_team_without_force() {
    let tmp = tempdir().unwrap();
    let home = tempdir().unwrap();

    let run_init = |extra: &[&str]| -> std::process::Output {
        let mut args = vec!["init", "my-team", "--yes"];
        args.extend(extra);
        Command::new(bin())
            .env_clear()
            .env("HOME", home.path())
            .env("PATH", std::env::var_os("PATH").unwrap_or_default())
            .current_dir(tmp.path())
            .args(args)
            .output()
            .unwrap()
    };

    let first = run_init(&[]);
    assert!(first.status.success(), "first init must succeed");

    let second = run_init(&[]);
    assert!(
        !second.status.success(),
        "second init without --force must refuse"
    );
    let stderr = String::from_utf8_lossy(&second.stderr);
    assert!(
        stderr.contains("already exists") && stderr.contains("--force"),
        "expected refusal hint in stderr, got: {stderr}"
    );

    let third = run_init(&["--force"]);
    assert!(
        third.status.success(),
        "init --force must overwrite: stderr={}",
        String::from_utf8_lossy(&third.stderr)
    );
}

// ── T-241: `teamctl adjust` wrapper ────────────────────────────────────

#[test]
fn adjust_yes_flag_errors_clearly_with_actionable_message() {
    // T-241 DoD: `teamctl adjust --yes` must reject early with a
    // clear, actionable error. Pinned end-to-end through the real
    // binary so the clap surface + the runtime check are both
    // exercised. Hermetic PATH so a stray `claude` on the dev box
    // can't accidentally make this test pass by exec'ing instead of
    // erroring.
    let empty = tempdir().unwrap();
    let out = Command::new(bin())
        .env("PATH", empty.path())
        .args(["adjust", "--yes"])
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "adjust --yes must exit non-zero; stderr: {}",
        String::from_utf8_lossy(&out.stderr),
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("interactive-only"),
        "expected `interactive-only` in stderr, got: {stderr}"
    );
    assert!(
        stderr.contains("--yes"),
        "expected `--yes` in stderr, got: {stderr}"
    );
}

// ── T-062: `teamctl ui` wrapper ────────────────────────────────────────

#[test]
fn ui_with_no_prompt_and_no_binary_prints_install_hint_and_exits_zero() {
    // End-to-end: drive the real binary with a hermetic PATH that
    // contains no `teamctl-ui`, and confirm `--no-prompt` short-circuits
    // cleanly. This pins the contract that scripted/CI use of
    // `teamctl ui --no-prompt` is exit-0 + hint-on-stderr — never
    // blocks, never installs, never errors.
    let empty = tempdir().unwrap();
    let out = Command::new(bin())
        .env("PATH", empty.path())
        .args(["ui", "--no-prompt"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "ui --no-prompt must exit 0 even when teamctl-ui is missing; stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("teamctl-ui is not installed"),
        "expected install hint on stderr, got: {stderr}"
    );
    assert!(
        stderr.contains("cargo install teamctl-ui"),
        "expected install command in hint, got: {stderr}"
    );
}

#[test]
fn warn_e_quiet_silences_env() {
    let tmp = tempdir().unwrap();
    let home = tempdir().unwrap();
    let root = seed_dot_team(tmp.path());
    let stderr = run_validate_with_env(
        tmp.path(),
        home.path(),
        &[
            ("TEAMCTL_ROOT", root.to_str().unwrap()),
            ("TEAMCTL_QUIET", "1"),
        ],
        None,
    );
    let clean = strip_ansi(&stderr);
    assert!(
        !clean.contains("warning:"),
        "TEAMCTL_QUIET=1 must silence; stderr was: {clean}"
    );
}

// ── T-305: per-agent scope on up/down/reload ────────────────────────────
//
// `seed_two_projects` gives alpha {manager, dev} + beta {manager, dev}.
// These exercise the CLI surface + selector resolution without tmux:
// `reload --dry-run` for the force path, and the error/usage paths
// (which short-circuit before the supervisor is ever touched).

#[test]
fn reload_dry_run_scoped_to_agent_forces_only_that_agent() {
    // `reload <project> <agent>` force-restarts exactly that agent —
    // the line is `(forced)`, not the diff path's `added`/`changed`.
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "reload",
            "--dry-run",
            "alpha",
            "dev",
        ])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(
        stdout.contains("reloaded · alpha:dev (forced) (dry run)"),
        "force line for the scoped agent: {stdout}"
    );
    assert!(
        !stdout.contains("alpha:manager"),
        "must not touch the unscoped sibling: {stdout}"
    );
    assert!(
        !stdout.contains("beta:"),
        "must not touch other projects: {stdout}"
    );
}

#[test]
fn reload_dry_run_except_agent_forces_the_complement() {
    // `reload <project> --except <agent>` force-restarts every agent
    // in the project EXCEPT the named one.
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "reload",
            "--dry-run",
            "alpha",
            "--except",
            "dev",
        ])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(
        stdout.contains("reloaded · alpha:manager (forced) (dry run)"),
        "complement is forced: {stdout}"
    );
    assert!(
        !stdout.contains("alpha:dev"),
        "the excepted agent is left alone: {stdout}"
    );
    assert!(
        !stdout.contains("beta:"),
        "other projects untouched: {stdout}"
    );
}

#[test]
fn reload_project_only_keeps_the_unchanged_diff_path() {
    // Back-compat pin: `<project>`-only reload (no agent selector) is
    // the existing diff path — agents land as `added` (no prior
    // snapshot), never force-restarted.
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "reload",
            "--dry-run",
            "alpha",
        ])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(stdout.contains("added   · alpha:manager"), "got: {stdout}");
    assert!(
        !stdout.contains("(forced)"),
        "project-only must not force-restart: {stdout}"
    );
}

#[test]
fn scoped_unknown_agent_errors_listing_valid_names() {
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "reload",
            "--dry-run",
            "alpha",
            "ghost",
        ])
        .output()
        .unwrap();
    assert!(!out.status.success(), "unknown agent must exit nonzero");
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(stderr.contains("ghost"), "names the bad input: {stderr}");
    assert!(stderr.contains("manager"), "lists valid agents: {stderr}");
    assert!(stderr.contains("dev"), "lists valid agents: {stderr}");
}

#[test]
fn except_unknown_agent_also_errors() {
    // A typo'd exclusion fails loudly rather than silently acting on
    // every agent.
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "reload",
            "--dry-run",
            "alpha",
            "--except",
            "ghost",
        ])
        .output()
        .unwrap();
    assert!(!out.status.success());
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(stderr.contains("ghost"), "names the bad input: {stderr}");
}

#[test]
fn positional_list_and_except_are_mutually_exclusive() {
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "down",
            "alpha",
            "dev",
            "--except",
            "manager",
        ])
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "supplying both forms must be a usage error"
    );
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(
        stderr.contains("cannot be used with"),
        "clap conflict message: {stderr}"
    );
}

#[test]
fn except_requires_a_project() {
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "down",
            "--except",
            "dev",
        ])
        .output()
        .unwrap();
    assert!(
        !out.status.success(),
        "--except without a project is invalid"
    );
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(
        stderr.contains("PROJECT"),
        "clap names the missing required arg: {stderr}"
    );
}

#[test]
fn except_every_agent_reports_empty_scope() {
    // Excepting everyone resolves to "act on nothing" — a graceful
    // no-op line, not an error.
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args([
            "--root",
            tmp.path().to_str().unwrap(),
            "reload",
            "--dry-run",
            "alpha",
            "--except",
            "manager",
            "--except",
            "dev",
        ])
        .output()
        .unwrap();
    assert!(out.status.success());
    let stdout = String::from_utf8(out.stdout).unwrap();
    assert!(
        stdout.contains("no agents in scope for project alpha"),
        "got: {stdout}"
    );
}

#[test]
fn help_documents_all_scope_levels_consistently() {
    // Acceptance criterion: `--help` for each of up/down/reload
    // documents no-arg = all projects, <project>, <project> <agent>…,
    // and <project> --except <agent>….
    for cmd in ["up", "down", "reload"] {
        let out = Command::new(bin()).args([cmd, "--help"]).output().unwrap();
        assert!(out.status.success(), "{cmd} --help failed");
        let help = String::from_utf8(out.stdout).unwrap();
        assert!(
            help.contains("every project"),
            "{cmd} --help missing all-projects form: {help}"
        );
        assert!(
            help.contains("every agent in it"),
            "{cmd} --help missing project-only form: {help}"
        );
        assert!(
            help.contains("just those agents"),
            "{cmd} --help missing positional-list form: {help}"
        );
        assert!(
            help.contains("--except"),
            "{cmd} --help missing --except form: {help}"
        );
    }
}

// ── T-310: id-charset validation — qa PoC class end-to-end ──────────────
//
// A `project.id` (or agent id) containing shell-metacharacter content
// must be rejected by `teamctl validate` AND must bail before
// `build_up_command` ever runs on `teamctl up` / `reload` / `down`.
// The unit tests in `team-core::validate` pin the validator itself;
// these integration tests pin the criterion-2 wiring: the supervisor
// is never reached when a compose carries a bad id.

fn seed_compose_with_evil_project_id(root: &std::path::Path) {
    fs::write(
        root.join("team-compose.yaml"),
        r#"
version: 2
broker:
  type: sqlite
  path: state/mailbox.db
supervisor:
  type: tmux
  tmux_prefix: a-
projects:
  - file: projects/evil.yaml
"#,
    )
    .unwrap();
    fs::create_dir_all(root.join("projects")).unwrap();
    // qa PoC class: shell-metacharacter content in `project.id`.
    fs::write(
        root.join("projects/evil.yaml"),
        r#"
version: 2
project:
  id: "evil; touch /tmp/teamctl-t310-pwn"
  name: Evil
  cwd: .
managers:
  manager:
    runtime: claude-code
    model: claude-opus-4-8
workers:
  dev:
    runtime: claude-code
    model: claude-sonnet-4-6
    reports_to: manager
"#,
    )
    .unwrap();
}

#[test]
fn validate_rejects_project_id_with_shell_metacharacters() {
    // `teamctl validate` exit-nonzero with a clear, actionable error
    // naming the rejected id (acceptance criterion 1).
    let tmp = tempdir().unwrap();
    seed_compose_with_evil_project_id(tmp.path());
    let out = Command::new(bin())
        .args(["--root", tmp.path().to_str().unwrap(), "validate"])
        .output()
        .unwrap();
    assert!(!out.status.success(), "must exit nonzero");
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(
        stderr.contains("evil; touch /tmp/teamctl-t310-pwn"),
        "names the rejected id: {stderr}"
    );
    assert!(
        stderr.contains("disallowed characters"),
        "explains the rule: {stderr}"
    );
}

#[test]
fn down_bails_on_evil_project_id_before_reaching_supervisor() {
    // Criterion 2: a shell-meta `project.id` can no longer reach the
    // shell via `build_up_command` on `teamctl down`. Pre-T-310 this
    // command was NOT validate-gated, so a malicious compose flowed
    // unquoted into `sh -c`. Now `down` validates + bails first.
    //
    // The negative existence check is the security pin: the injected
    // `touch /tmp/teamctl-t310-pwn` must NOT have run.
    let pwn_marker = std::path::PathBuf::from("/tmp/teamctl-t310-pwn");
    let _ = std::fs::remove_file(&pwn_marker);

    let tmp = tempdir().unwrap();
    seed_compose_with_evil_project_id(tmp.path());
    let out = Command::new(bin())
        .args(["--root", tmp.path().to_str().unwrap(), "down"])
        .output()
        .unwrap();
    assert!(!out.status.success(), "down must bail on validation error");
    let stderr = String::from_utf8(out.stderr).unwrap();
    assert!(
        stderr.contains("disallowed characters"),
        "down must surface the id-charset error: {stderr}"
    );
    assert!(
        !pwn_marker.exists(),
        "INJECTION FIRED — `build_up_command` was reached: marker {} exists",
        pwn_marker.display()
    );
    // Best-effort cleanup if a regression ever creates it.
    let _ = std::fs::remove_file(&pwn_marker);
}

#[test]
fn validate_accepts_existing_conformant_id_shapes() {
    // Acceptance criterion 5 (no regression for conformant teams):
    // the existing `seed_two_projects` shape passes validate cleanly.
    let tmp = tempdir().unwrap();
    seed_two_projects(tmp.path());
    let out = Command::new(bin())
        .args(["--root", tmp.path().to_str().unwrap(), "validate"])
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "conformant ids must validate: stderr={}",
        String::from_utf8_lossy(&out.stderr)
    );
}