heddle-cli 0.8.0

An AI-native version control system
Documentation
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
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
// SPDX-License-Identifier: Apache-2.0
//! Tests for the machine-readable command catalog.
//!
//! Extracted verbatim from `command_catalog.rs` (heddle#609 phase 1):
//! the `mod tests` body moved into this sibling file unchanged (de-indented
//! one level). It referenced the parent via `super::*` inline and continues
//! to do so as a sibling module -- pure code movement, no logic change.
//!
//! Gated by the `#[cfg(test)] mod tests;` declaration in the parent module.

use std::collections::BTreeSet;

use clap::Parser;

use super::*;

struct RuntimeContractParseSample {
    path: &'static [&'static str],
    argv_tail: &'static [&'static str],
}

// Representative parseable invocations for every runtime leaf command in
// CONTRACTS. The only intentionally skipped rows are non-runtime grouping
// contracts whose Clap variants require a subcommand, e.g. `thread`,
// `bridge git`, and `redact trust`.
const RUNTIME_CONTRACT_PARSE_SAMPLES: &[RuntimeContractParseSample] = &[
    sample(&["abort"], &["abort"]),
    sample(&["adopt"], &["adopt"]),
    sample(&["actor", "spawn"], &["actor", "spawn"]),
    sample(&["actor", "list"], &["actor", "list"]),
    sample(&["actor", "show"], &["actor", "show"]),
    sample(&["actor", "explain"], &["actor", "explain"]),
    sample(&["actor", "done"], &["actor", "done"]),
    sample(&["agent", "serve"], &["agent", "serve"]),
    sample(&["agent", "status"], &["agent", "status"]),
    sample(&["agent", "stop"], &["agent", "stop"]),
    sample(
        &["agent", "reserve"],
        &["agent", "reserve", "--thread", "main"],
    ),
    sample(
        &["agent", "heartbeat"],
        &["agent", "heartbeat", "--session", "session-1"],
    ),
    sample(
        &["agent", "capture"],
        &["agent", "capture", "--session", "session-1"],
    ),
    sample(
        &["agent", "ready"],
        &["agent", "ready", "--session", "session-1"],
    ),
    sample(
        &["agent", "release"],
        &["agent", "release", "--session", "session-1"],
    ),
    sample(&["agent", "list"], &["agent", "list"]),
    sample(
        &["agent", "task", "create"],
        &[
            "agent",
            "task",
            "create",
            "--task-id",
            "task-1",
            "--title",
            "Task one",
            "--thread",
            "feature/task-1",
        ],
    ),
    sample(&["agent", "task", "list"], &["agent", "task", "list"]),
    sample(
        &["agent", "task", "show"],
        &["agent", "task", "show", "task-1"],
    ),
    sample(
        &["agent", "task", "update"],
        &["agent", "task", "update", "task-1", "--status", "complete"],
    ),
    sample(
        &["agent", "fanout", "plan"],
        &[
            "agent",
            "fanout",
            "plan",
            "--title",
            "Coordinate lanes",
            "--lane",
            "feature/a=../a:Implement A",
        ],
    ),
    sample(
        &["agent", "fanout", "start"],
        &[
            "agent",
            "fanout",
            "start",
            "--title",
            "Coordinate lanes",
            "--lane",
            "feature/a=../a:Implement A",
        ],
    ),
    #[cfg(feature = "client")]
    sample(&["auth", "login"], &["auth", "login", "--no-browser"]),
    #[cfg(feature = "client")]
    sample(&["auth", "logout"], &["auth", "logout"]),
    #[cfg(feature = "client")]
    sample(&["auth", "status"], &["auth", "status"]),
    #[cfg(feature = "client")]
    sample(
        &["auth", "create-service-token"],
        &[
            "auth",
            "create-service-token",
            "github-ci-main",
            "--namespace",
            "heddle/platform",
        ],
    ),
    #[cfg(feature = "git-overlay")]
    sample(&["bridge", "git", "status"], &["bridge", "git", "status"]),
    #[cfg(feature = "git-overlay")]
    sample(&["bridge", "git", "init"], &["bridge", "git", "init"]),
    #[cfg(feature = "git-overlay")]
    sample(&["bridge", "git", "export"], &["bridge", "git", "export"]),
    #[cfg(feature = "git-overlay")]
    sample(&["bridge", "git", "import"], &["bridge", "git", "import"]),
    #[cfg(feature = "git-overlay")]
    sample(&["bridge", "git", "sync"], &["bridge", "git", "sync"]),
    #[cfg(feature = "git-overlay")]
    sample(
        &["bridge", "git", "reconcile"],
        &[
            "bridge",
            "git",
            "reconcile",
            "--prefer",
            "heddle",
            "--ref",
            "main",
        ],
    ),
    #[cfg(feature = "git-overlay")]
    sample(&["bridge", "git", "push"], &["bridge", "git", "push"]),
    #[cfg(feature = "git-overlay")]
    sample(&["bridge", "git", "pull"], &["bridge", "git", "pull"]),
    #[cfg(all(feature = "git-overlay", feature = "ingest"))]
    sample(
        &["bridge", "git", "reason"],
        &["bridge", "git", "reason", "--path", "."],
    ),
    sample(&["capture"], &["capture"]),
    sample(&["checkpoint"], &["checkpoint"]),
    sample(&["cherry-pick"], &["cherry-pick", "abc123"]),
    sample(&["clean"], &["clean"]),
    sample(&["clone"], &["clone", "remote", "local"]),
    sample(
        &["collapse"],
        &["collapse", "s1", "s2", "--into", "squashed"],
    ),
    sample(&["commit"], &["commit"]),
    sample(&["continue"], &["continue"]),
    sample(&["expand"], &["expand", "HEAD"]),
    sample(
        &["context", "set"],
        &["context", "set", "--path", "src/lib.rs"],
    ),
    sample(
        &["context", "get"],
        &["context", "get", "--path", "src/lib.rs"],
    ),
    sample(&["context", "list"], &["context", "list"]),
    sample(&["context", "history"], &["context", "history", "ctx-1"]),
    sample(&["context", "edit"], &["context", "edit", "ctx-1"]),
    sample(
        &["context", "supersede"],
        &["context", "supersede", "ctx-1", "--path", "src/lib.rs"],
    ),
    sample(
        &["context", "rm"],
        &["context", "rm", "--path", "src/lib.rs"],
    ),
    sample(&["context", "check"], &["context", "check"]),
    sample(&["context", "suggest"], &["context", "suggest"]),
    sample(&["context", "audit"], &["context", "audit"]),
    sample(&["daemon", "serve"], &["daemon", "serve"]),
    sample(&["daemon", "status"], &["daemon", "status"]),
    sample(&["daemon", "stop"], &["daemon", "stop"]),
    sample(&["diff"], &["diff"]),
    sample(
        &["discuss", "open"],
        &["discuss", "open", "src/lib.rs", "symbol", "body"],
    ),
    sample(
        &["discuss", "append"],
        &["discuss", "append", "discussion-1", "body"],
    ),
    sample(
        &["discuss", "resolve"],
        &["discuss", "resolve", "discussion-1", "--mode", "dismiss"],
    ),
    sample(&["discuss", "list"], &["discuss", "list"]),
    sample(&["discuss", "show"], &["discuss", "show", "discussion-1"]),
    sample(&["doctor"], &["doctor"]),
    sample(&["doctor", "docs"], &["doctor", "docs"]),
    sample(&["doctor", "schemas"], &["doctor", "schemas"]),
    sample(&["fetch"], &["fetch"]),
    sample(&["fsck"], &["fsck"]),
    sample(&["oplog", "recover"], &["oplog", "recover"]),
    #[cfg(feature = "git-overlay")]
    sample(&["git-overlay"], &["git-overlay"]),
    sample(&["help"], &["help"]),
    sample(&["hook", "list"], &["hook", "list"]),
    sample(&["hook", "install"], &["hook", "install", "pre-snapshot"]),
    sample(
        &["hook", "uninstall"],
        &["hook", "uninstall", "pre-snapshot"],
    ),
    sample(&["hook", "events"], &["hook", "events"]),
    sample(&["init"], &["init"]),
    sample(&["integration", "list"], &["integration", "list"]),
    sample(&["integration", "install"], &["integration", "install"]),
    sample(&["integration", "doctor"], &["integration", "doctor"]),
    sample(&["integration", "uninstall"], &["integration", "uninstall"]),
    sample(&["integration", "upgrade"], &["integration", "upgrade"]),
    sample(
        &["integration", "relay"],
        &["integration", "relay", "codex", "agent_done"],
    ),
    sample(&["log"], &["log"]),
    sample(&["maintenance", "inspect"], &["maintenance", "inspect"]),
    sample(&["maintenance", "run"], &["maintenance", "run"]),
    sample(&["maintenance", "gc"], &["maintenance", "gc"]),
    sample(&["maintenance", "index"], &["maintenance", "index"]),
    sample(&["maintenance", "monitor"], &["maintenance", "monitor"]),
    sample(&["merge"], &["merge", "feature"]),
    #[cfg(feature = "client")]
    sample(
        &["presence", "publish"],
        &["presence", "publish", "--session", "session-1"],
    ),
    sample(&["pull"], &["pull"]),
    sample(&["push"], &["push"]),
    sample(&["query"], &["query"]),
    sample(&["ready"], &["ready"]),
    sample(&["rebase"], &["rebase"]),
    sample(
        &["redact", "apply"],
        &[
            "redact",
            "apply",
            "HEAD",
            "--path",
            "secret.txt",
            "--reason",
            "secret",
        ],
    ),
    sample(&["redact", "list"], &["redact", "list"]),
    sample(&["redact", "show"], &["redact", "show", "redaction-1"]),
    sample(
        &["redact", "purge", "apply"],
        &[
            "redact",
            "purge",
            "apply",
            "HEAD",
            "--path",
            "secret.txt",
            "--force",
        ],
    ),
    sample(&["redact", "purge", "list"], &["redact", "purge", "list"]),
    sample(
        &["redact", "trust", "add"],
        &[
            "redact",
            "trust",
            "add",
            "--algorithm",
            "ed25519",
            "--public-key",
            "abcd",
        ],
    ),
    sample(&["redact", "trust", "list"], &["redact", "trust", "list"]),
    sample(
        &["redact", "trust", "remove"],
        &["redact", "trust", "remove", "abcd"],
    ),
    sample(&["remote", "list"], &["remote", "list"]),
    sample(
        &["remote", "add"],
        &["remote", "add", "origin", "localhost:9418"],
    ),
    sample(&["remote", "remove"], &["remote", "remove", "origin"]),
    sample(
        &["remote", "set-default"],
        &["remote", "set-default", "origin"],
    ),
    sample(&["remote", "show"], &["remote", "show", "origin"]),
    sample(&["resolve"], &["resolve"]),
    sample(&["retro"], &["retro"]),
    sample(&["revert"], &["revert", "HEAD"]),
    sample(&["review", "show"], &["review", "show"]),
    sample(
        &["review", "sign"],
        &[
            "review",
            "sign",
            "HEAD",
            "--kind",
            "read",
            "--public-key",
            "abcd",
            "--signature",
            "ef01",
            "--signed-at-unix",
            "1",
        ],
    ),
    sample(&["review", "next"], &["review", "next"]),
    sample(&["review", "health"], &["review", "health"]),
    sample(&["run"], &["run", "true"]),
    sample(&["schemas"], &["schemas"]),
    #[cfg(feature = "semantic")]
    sample(&["semantic", "hot"], &["semantic", "hot"]),
    sample(
        &["session", "start"],
        &[
            "session",
            "start",
            "--provider",
            "openai",
            "--model",
            "gpt-5",
        ],
    ),
    sample(
        &["session", "segment"],
        &[
            "session",
            "segment",
            "--provider",
            "openai",
            "--model",
            "gpt-5",
        ],
    ),
    sample(&["session", "end"], &["session", "end"]),
    sample(&["session", "show"], &["session", "show"]),
    sample(&["session", "list"], &["session", "list"]),
    sample(&["shell", "init"], &["shell", "init", "bash"]),
    sample(&["shell", "completion"], &["shell", "completion", "bash"]),
    sample(&["shell", "prompt"], &["shell", "prompt"]),
    sample(&["complete"], &["__complete", "threads"]),
    sample(&["land"], &["land"]),
    sample(&["show"], &["show", "HEAD"]),
    sample(&["start"], &["start", "feature"]),
    sample(&["stash", "push"], &["stash", "push"]),
    sample(&["stash", "list"], &["stash", "list"]),
    sample(&["stash", "pop"], &["stash", "pop"]),
    sample(&["stash", "apply"], &["stash", "apply"]),
    sample(&["stash", "drop"], &["stash", "drop"]),
    sample(&["stash", "clear"], &["stash", "clear"]),
    sample(&["stash", "show"], &["stash", "show"]),
    sample(&["status"], &["status"]),
    #[cfg(feature = "client")]
    sample(
        &["support", "grant"],
        &[
            "support",
            "grant",
            "support@heddle.dev",
            "--namespace",
            "heddle/platform",
            "--reason",
            "release verification",
        ],
    ),
    #[cfg(feature = "client")]
    sample(
        &["support", "list"],
        &["support", "list", "--namespace", "heddle/platform"],
    ),
    #[cfg(feature = "client")]
    sample(
        &["support", "revoke"],
        &["support", "revoke", "00000000-0000-0000-0000-000000000000"],
    ),
    sample(&["switch"], &["switch", "main"]),
    sample(&["sync"], &["sync"]),
    sample(&["thread", "create"], &["thread", "create", "feature"]),
    sample(&["thread", "current"], &["thread", "current"]),
    sample(&["thread", "switch"], &["thread", "switch", "feature"]),
    sample(&["thread", "cd"], &["thread", "cd", "feature"]),
    sample(&["thread", "list"], &["thread", "list"]),
    sample(&["thread", "show"], &["thread", "show"]),
    sample(&["thread", "captures"], &["thread", "captures"]),
    sample(
        &["thread", "rename"],
        &["thread", "rename", "old-feature", "new-feature"],
    ),
    sample(&["thread", "refresh"], &["thread", "refresh", "feature"]),
    sample(
        &["thread", "move"],
        &["thread", "move", "source", "dest", "--path", "src/lib.rs"],
    ),
    sample(&["thread", "absorb"], &["thread", "absorb", "feature"]),
    sample(&["thread", "resolve"], &["thread", "resolve", "feature"]),
    sample(&["thread", "promote"], &["thread", "promote", "feature"]),
    sample(&["thread", "drop"], &["thread", "drop", "feature"]),
    sample(
        &["thread", "approve"],
        &["thread", "approve", "source", "target"],
    ),
    sample(
        &["thread", "approvals"],
        &["thread", "approvals", "source", "target"],
    ),
    sample(
        &["thread", "revoke-approval"],
        &[
            "thread",
            "revoke-approval",
            "00000000-0000-0000-0000-000000000000",
        ],
    ),
    sample(
        &["thread", "check-merge"],
        &["thread", "check-merge", "source", "target"],
    ),
    sample(&["thread", "cleanup"], &["thread", "cleanup", "--merged"]),
    sample(&["thread", "marker", "list"], &["thread", "marker", "list"]),
    sample(
        &["thread", "marker", "create"],
        &["thread", "marker", "create", "checkpoint"],
    ),
    sample(
        &["thread", "marker", "delete"],
        &["thread", "marker", "delete", "checkpoint"],
    ),
    sample(
        &["thread", "marker", "show"],
        &["thread", "marker", "show", "checkpoint"],
    ),
    sample(&["timeline", "status"], &["timeline", "status"]),
    sample(
        &["timeline", "record-start"],
        &["timeline", "record-start", "--tool-call", "call-1"],
    ),
    sample(
        &["timeline", "record-finish"],
        &["timeline", "record-finish", "--tool-call", "call-1"],
    ),
    sample(
        &["timeline", "fork"],
        &["timeline", "fork", "--step", "tls-abc"],
    ),
    sample(
        &["timeline", "reset"],
        &["timeline", "reset", "--step", "tls-abc"],
    ),
    sample(&["timeline", "recover"], &["timeline", "recover"]),
    sample(&["transaction", "begin"], &["transaction", "begin"]),
    sample(
        &["transaction", "commit"],
        &["transaction", "commit", "tx-1"],
    ),
    sample(&["transaction", "abort"], &["transaction", "abort", "tx-1"]),
    sample(
        &["transaction", "status"],
        &["transaction", "status", "tx-1"],
    ),
    sample(&["verify"], &["verify"]),
    sample(
        &["visibility", "set"],
        &["visibility", "set", "HEAD", "--tier", "internal"],
    ),
    sample(
        &["visibility", "promote"],
        &["visibility", "promote", "HEAD", "--tier", "internal"],
    ),
    sample(&["visibility", "show"], &["visibility", "show", "HEAD"]),
    sample(&["visibility", "list"], &["visibility", "list"]),
    sample(&["try"], &["try", "true"]),
    sample(&["undo"], &["undo"]),
    sample(&["watch"], &["watch"]),
];

const fn sample(
    path: &'static [&'static str],
    argv_tail: &'static [&'static str],
) -> RuntimeContractParseSample {
    RuntimeContractParseSample { path, argv_tail }
}

#[test]
fn recommended_actions_parse_through_clap_or_registered_placeholders() {
    for action in [
        "",
        "heddle init",
        "heddle capture -m \"...\"",
        "heddle commit -m \"...\"",
        "heddle stash push -m \"...\"",
        "heddle capture -m \"Preserve raw Git operation work\"",
        "heddle switch <branch>",
        "heddle start feature/auth --path <dir>",
        "heddle clone <remote> <fresh-path>",
        "heddle clone <local-path> <path>",
        "heddle clone /tmp/source <path> --thread main",
        "heddle bridge git import --path <full-git-repo> --ref <ref>",
        "heddle thread promote main",
        "heddle thread resolve main",
    ] {
        validate_recommended_action(action)
            .unwrap_or_else(|err| panic!("expected `{action}` to validate: {err}"));
    }
    #[cfg(feature = "git-overlay")]
    {
        for action in [
            "heddle bridge git import --ref main",
            "heddle bridge git import --ref origin/main",
            "heddle merge origin/main --preview",
            "heddle bridge git reconcile --ref main --preview",
            "heddle bridge git reconcile --prefer heddle --ref main --preview",
        ] {
            validate_recommended_action(action)
                .unwrap_or_else(|err| panic!("expected `{action}` to validate: {err}"));
        }
    }
}

#[test]
fn recommended_action_templates_describe_display_only_placeholders() {
    let catalog = build_command_catalog();
    for placeholder in RECOMMENDED_ACTION_PLACEHOLDERS {
        assert!(
            recommended_action_template(placeholder).is_some(),
            "placeholder `{placeholder}` must have a structured template"
        );
    }
    for template in &catalog.recommended_action_templates {
        validate_recommended_action(&template.action).unwrap_or_else(|err| {
            panic!(
                "recommended action template `{}` must validate: {err}",
                template.action
            )
        });
    }

    let commit = catalog
        .recommended_action_templates
        .iter()
        .find(|template| template.action == "heddle commit -m \"...\"")
        .expect("commit placeholder should have a structured template");
    assert_eq!(
        commit.argv_template,
        vec!["heddle", "commit", "-m", "<message>"]
    );
    assert_eq!(commit.required_inputs, vec!["message"]);
    assert!(commit.agent_may_fill);

    let template = recommended_action_template("heddle checkpoint -m \"...\"")
        .expect("checkpoint placeholder should resolve");
    assert_eq!(
        template.argv_template,
        vec!["heddle", "checkpoint", "-m", "<message>"]
    );

    let switch = recommended_action_template("heddle switch <branch>")
        .expect("switch placeholder should resolve");
    assert_eq!(switch.argv_template, vec!["heddle", "switch", "<branch>"]);
    assert_eq!(switch.required_inputs, vec!["branch"]);
    assert!(!switch.agent_may_fill);

    let clone = recommended_action_template("heddle clone <remote> <fresh-path>")
        .expect("clone recovery placeholder should resolve");
    assert_eq!(
        clone.argv_template,
        vec!["heddle", "clone", "<remote>", "<fresh-path>"]
    );
    assert_eq!(clone.required_inputs, vec!["remote", "path"]);
    assert!(!clone.agent_may_fill);

    let start = recommended_action_template("heddle start feature/auth --path <dir>")
        .expect("start path placeholder should resolve");
    assert_eq!(
        start.argv_template,
        vec!["heddle", "start", "feature/auth", "--path", "<dir>"]
    );
    assert_eq!(start.required_inputs, vec!["dir"]);
    assert!(start.agent_may_fill);

    let local_clone = recommended_action_template("heddle clone <local-path> <path>")
        .expect("local clone recovery placeholder should resolve");
    assert_eq!(
        local_clone.argv_template,
        vec!["heddle", "clone", "<local-path>", "<path>"]
    );
    assert_eq!(local_clone.required_inputs, vec!["local_path", "path"]);
    assert!(!local_clone.agent_may_fill);

    let dynamic_clone =
        recommended_action_template("heddle clone /tmp/source <path> --thread main")
            .expect("dynamic clone recovery placeholder should resolve");
    assert_eq!(
        dynamic_clone.argv_template,
        vec![
            "heddle",
            "clone",
            "/tmp/source",
            "<path>",
            "--thread",
            "main"
        ]
    );
    assert_eq!(dynamic_clone.required_inputs, vec!["path"]);
    assert!(!dynamic_clone.agent_may_fill);

    let import =
        recommended_action_template("heddle bridge git import --path <full-git-repo> --ref <ref>")
            .expect("shallow import recovery placeholder should resolve");
    assert_eq!(
        import.argv_template,
        vec![
            "heddle",
            "bridge",
            "git",
            "import",
            "--path",
            "<full-git-repo>",
            "--ref",
            "<ref>"
        ]
    );
    assert_eq!(import.required_inputs, vec!["path", "ref"]);
    assert!(!import.agent_may_fill);

    let merge = recommended_action_template("heddle merge <thread> --git-commit")
        .expect("merge recovery placeholder should resolve");
    assert_eq!(
        merge.argv_template,
        vec!["heddle", "merge", "<thread>", "--git-commit"]
    );
    assert_eq!(merge.required_inputs, vec!["thread"]);
    assert!(!merge.agent_may_fill);
}

#[test]
fn action_fields_template_dirty_worktree_message_placeholders() {
    for (action, expected_argv_template) in [
        (
            "heddle commit -m \"...\"",
            vec!["heddle", "commit", "-m", "<message>"],
        ),
        (
            "heddle capture -m \"...\"",
            vec!["heddle", "capture", "-m", "<message>"],
        ),
        (
            "heddle stash push -m \"...\"",
            vec!["heddle", "stash", "push", "-m", "<message>"],
        ),
    ] {
        let fields = ActionFields::from_action(action);
        assert_eq!(fields.action.as_deref(), Some(action));
        let template = fields
            .template
            .unwrap_or_else(|| panic!("`{action}` should expose a structured template"));
        assert_eq!(template.argv_template, expected_argv_template);
        assert_eq!(template.required_inputs, vec!["message"]);
        assert!(template.agent_may_fill);
    }
}

#[test]
fn action_fields_template_argv_normalized_message_placeholders() {
    for (action, expected_argv_template) in [
        (
            "heddle commit -m ...",
            vec!["heddle", "commit", "-m", "<message>"],
        ),
        (
            "heddle capture -m ...",
            vec!["heddle", "capture", "-m", "<message>"],
        ),
        (
            "heddle stash push -m ...",
            vec!["heddle", "stash", "push", "-m", "<message>"],
        ),
    ] {
        let fields = ActionFields::from_action(action);
        assert_eq!(fields.action.as_deref(), Some(action));
        let template = fields
            .template
            .unwrap_or_else(|| panic!("`{action}` should expose a structured template"));
        assert_eq!(template.argv_template, expected_argv_template);
        assert_eq!(template.required_inputs, vec!["message"]);
        assert!(template.agent_may_fill);
    }
}

#[test]
fn display_only_recommended_actions_must_be_templated() {
    let err = validate_recommended_action("heddle switch <missing-template>")
        .expect_err("unregistered display placeholder should fail validation");
    assert!(
        err.contains("structured template"),
        "error should explain missing template: {err}"
    );

    assert!(
        recommended_action_template("heddle switch <missing-template>").is_none(),
        "unregistered display placeholder must not resolve to a fillable template"
    );
}

#[test]
fn recommended_action_validator_rejects_unknown_commands() {
    let err = validate_recommended_action("heddle definitely-not-a-command")
        .expect_err("unknown heddle command should fail validation");
    assert!(
        err.contains("definitely-not-a-command"),
        "error should name the bad command: {err}"
    );

    let err = validate_recommended_action("git status")
        .expect_err("raw git action must be explicitly registered");
    assert!(
        err.contains("registered as a placeholder"),
        "error should explain placeholder registration: {err}"
    );
}

#[test]
fn leading_dash_thread_breadcrumbs_pass_validation() {
    // A historical / `new_unchecked` thread id literally named `-foo` renders
    // breadcrumbs via the `=` (flag) and `--` (positional) forms; the
    // validator splits to argv and runs clap, which would reject the bare
    // `--thread -foo` form as an unknown flag. (heddle#464 round 8.)
    for action in [
        repo::RecommendedAction::Sync,
        repo::RecommendedAction::Ready,
        repo::RecommendedAction::Land,
        repo::RecommendedAction::Promote,
    ] {
        if let Some(cmd) = action.command("-foo") {
            validate_recommended_action(&cmd).unwrap_or_else(|err| {
                panic!("breadcrumb `{cmd}` must validate for a leading-dash id: {err}")
            });
        }
    }
}

#[test]
fn action_fields_fail_loudly_for_invalid_recommendations() {
    let panic = std::panic::catch_unwind(|| ActionFields::from_action("git status"));
    assert!(
        panic.is_err(),
        "ActionFields must not silently erase invalid action sidecars"
    );
}

#[test]
fn recommended_action_parser_supports_shell_quoted_arguments() {
    let template = recommended_action_template("heddle merge 'feature with spaces' --preview")
        .expect("single-quoted thread action should resolve to a template");
    assert_eq!(
        template.argv_template[1..],
        ["merge", "feature with spaces", "--preview"]
    );

    let template = recommended_action_template("heddle merge 'feature '\\''quoted'\\''' --preview")
        .expect("shell-quoted apostrophe should resolve to a template");
    assert_eq!(
        template.argv_template[1..],
        ["merge", "feature 'quoted'", "--preview"]
    );
}

#[test]
fn checked_action_builder_quotes_and_validates_from_argv() {
    let action = heddle_action(["merge", "feature with spaces", "--preview"]);
    assert_eq!(action, "heddle merge 'feature with spaces' --preview");
    let template =
        recommended_action_template(&action).expect("built action should resolve to a template");
    assert_eq!(
        template.argv_template[1..],
        ["merge", "feature with spaces", "--preview"]
    );

    let panic = std::panic::catch_unwind(|| checked_action_from_argv(["git", "status"]));
    assert!(
        panic.is_err(),
        "non-Heddle actions should not enter runtime advice sidecars"
    );
}

#[test]
fn command_contract_table_matches_clap_command_tree() {
    let raw_contract_paths = CONTRACTS
        .iter()
        .map(|entry| {
            entry
                .path
                .iter()
                .map(|part| (*part).to_string())
                .collect::<Vec<_>>()
        })
        .collect::<BTreeSet<_>>();
    assert_eq!(
        raw_contract_paths.len(),
        CONTRACTS.len(),
        "command contract table contains duplicate paths"
    );
    let active_contract_paths = active_command_contract_entries()
        .iter()
        .copied()
        .map(|entry| {
            entry
                .path
                .iter()
                .map(|part| (*part).to_string())
                .collect::<Vec<_>>()
        })
        .collect::<BTreeSet<_>>();

    let mut clap_paths = BTreeSet::new();
    collect_clap_command_paths(&Cli::command(), &mut Vec::new(), &mut clap_paths);

    let missing_contracts = clap_paths
        .difference(&active_contract_paths)
        .map(|path| path.join(" "))
        .collect::<Vec<_>>();
    assert!(
        missing_contracts.is_empty(),
        "Clap exposes command path(s) with no command contract: {missing_contracts:?}"
    );

    let stale_contracts = active_contract_paths
        .difference(&clap_paths)
        .map(|path| path.join(" "))
        .collect::<Vec<_>>();
    assert!(
        stale_contracts.is_empty(),
        "command contract table contains path(s) not exposed by Clap: {stale_contracts:?}"
    );
}

fn collect_clap_command_paths(
    command: &clap::Command,
    prefix: &mut Vec<String>,
    out: &mut BTreeSet<Vec<String>>,
) {
    for subcommand in command.get_subcommands() {
        prefix.push(subcommand.get_name().to_string());
        out.insert(prefix.clone());
        collect_clap_command_paths(subcommand, prefix, out);
        prefix.pop();
    }
}

#[test]
fn parsed_runtime_contract_lookup_matches_contract_table_for_parseable_commands() {
    let active_contracts = active_command_contract_entries();
    let sample_paths = RUNTIME_CONTRACT_PARSE_SAMPLES
        .iter()
        .map(|sample| sample.path.to_vec())
        .collect::<BTreeSet<_>>();
    assert_eq!(
        sample_paths.len(),
        RUNTIME_CONTRACT_PARSE_SAMPLES.len(),
        "runtime contract parse samples contain duplicate paths"
    );

    let child_contract_paths = contract_paths_with_children(active_contracts);
    let unsampled_contracts = active_contracts
        .iter()
        .filter(|entry| !child_contract_paths.contains(entry.path))
        .filter(|entry| !sample_paths.contains(entry.path))
        .map(|entry| entry.path.join(" "))
        .collect::<Vec<_>>();
    assert!(
        unsampled_contracts.is_empty(),
        "parseable leaf/runtime contract path(s) need parse samples: {unsampled_contracts:?}"
    );

    for sample in RUNTIME_CONTRACT_PARSE_SAMPLES {
        let expected = active_contracts
            .iter()
            .find(|entry| entry.path == sample.path)
            .unwrap_or_else(|| {
                panic!(
                    "runtime contract parse sample references missing contract `{}`",
                    sample.path.join(" ")
                )
            });
        let mut argv = vec!["heddle"];
        argv.extend_from_slice(sample.argv_tail);
        let cli = Cli::try_parse_from(argv.clone())
            .unwrap_or_else(|err| panic!("failed to parse sample {argv:?}: {err}"));
        let runtime = command_runtime_contract_for_command(&cli.command);

        assert_eq!(
            runtime.path,
            expected.path,
            "parsed sample {argv:?} must resolve to contract path `{}`",
            expected.path.join(" ")
        );
        assert_eq!(runtime.display, expected.path.join(" "));
        assert_eq!(runtime.display, command_path(&cli.command).join(" "));
    }
}

#[test]
fn json_compact_runtime_contract_is_projection_or_rejection() {
    let expected_compact = BTreeSet::from([
        "abort".to_string(),
        "capture".to_string(),
        "continue".to_string(),
        "land".to_string(),
        "merge".to_string(),
        "ready".to_string(),
        "status".to_string(),
        "sync".to_string(),
    ]);
    let actual_compact = active_command_contract_entries()
        .iter()
        .filter(|entry| entry.contract.supports_json_compact)
        .map(|entry| entry.path.join(" "))
        .collect::<BTreeSet<_>>();
    assert_eq!(actual_compact, expected_compact);

    let json_output_commands = active_command_contract_entries()
        .iter()
        .filter(|entry| entry.contract.supports_json)
        .map(|entry| entry.path.join(" "))
        .collect::<BTreeSet<_>>();
    let compact_rejections = active_command_contract_entries()
        .iter()
        .filter(|entry| entry.contract.supports_json && !entry.contract.supports_json_compact)
        .map(|entry| entry.path.join(" "))
        .collect::<BTreeSet<_>>();
    let classified_commands = actual_compact
        .union(&compact_rejections)
        .cloned()
        .collect::<BTreeSet<_>>();
    assert_eq!(
        classified_commands, json_output_commands,
        "every JSON-output command must either project json-compact or reject it before execution"
    );
    assert!(
        compact_rejections.contains("query"),
        "the harness must include commands that accept --output json but reject json-compact"
    );

    for sample in RUNTIME_CONTRACT_PARSE_SAMPLES {
        let mut argv = vec!["heddle", "--output", "json-compact"];
        argv.extend_from_slice(sample.argv_tail);
        let cli = Cli::try_parse_from(argv.clone())
            .unwrap_or_else(|err| panic!("failed to parse sample {argv:?}: {err}"));
        let runtime = command_runtime_contract_for_command(&cli.command);
        assert!(
            runtime.supports_json_compact || !expected_compact.contains(&runtime.display),
            "`{}` accepts json-compact at parse time but lacks an explicit compact projection; main must reject it before command execution",
            runtime.display
        );
        assert!(
            runtime.supports_json || !runtime.supports_json_compact,
            "`{}` cannot support json-compact without supporting json",
            runtime.display
        );
    }
}

fn contract_paths_with_children(
    entries: &[&'static CommandContractEntry],
) -> BTreeSet<Vec<&'static str>> {
    entries
        .iter()
        .filter(|candidate| {
            entries.iter().any(|entry| {
                entry.path.len() > candidate.path.len() && entry.path.starts_with(candidate.path)
            })
        })
        .map(|entry| entry.path.to_vec())
        .collect()
}

#[test]
fn command_contract_metadata_is_internally_consistent() {
    for entry in CONTRACTS {
        let display = entry.path.join(" ");
        let contract = entry.contract;
        let json_capable = matches!(contract.json_kind, "json" | "jsonl" | "json_or_jsonl");
        assert_eq!(
            contract.supports_json, json_capable,
            "`{display}` supports_json must agree with json_kind `{}`",
            contract.json_kind
        );
        assert!(
            matches!(
                contract.json_kind,
                "json" | "jsonl" | "json_or_jsonl" | "none"
            ),
            "`{display}` has unknown json_kind `{}`",
            contract.json_kind
        );
        assert!(
            matches!(
                contract.surface,
                "native" | "git_adapter" | "automation" | "admin" | "internal"
            ),
            "`{display}` has unknown product surface `{}`",
            contract.surface
        );
        assert!(
            matches!(
                contract.help_visibility,
                "everyday" | "advanced" | "git_adapter" | "hidden"
            ),
            "`{display}` has unknown help visibility `{}`",
            contract.help_visibility
        );
        if contract.help_visibility == "git_adapter" {
            assert_eq!(
                contract.surface, "git_adapter",
                "`{display}` Git adapter commands must live on the Git adapter surface"
            );
            assert!(
                contract.canonical_command.is_some(),
                "`{display}` Git-shaped aliases must name a canonical Heddle command"
            );
            assert!(
                contract.canonical_kind.is_some(),
                "`{display}` Git-shaped aliases must classify the canonical action"
            );
            assert!(
                contract.canonical_note.is_some(),
                "`{display}` Git-shaped aliases must explain the canonical action"
            );
        }
        if contract.help_visibility == "everyday" {
            assert!(
                contract.help_rank < 1000,
                "`{display}` everyday commands must choose an explicit help rank"
            );
        }
        if let Some(canonical) = contract.canonical_command {
            let canonical_kind = contract
                .canonical_kind
                .unwrap_or_else(|| panic!("`{display}` canonical command must have a kind"));
            assert!(
                matches!(
                    canonical_kind,
                    "direct_command" | "command_family" | "workflow" | "conceptual_home"
                ),
                "`{display}` has unknown canonical action kind `{canonical_kind}`"
            );
            assert!(
                raw_command_contract_for_path(canonical.split_whitespace()).is_some(),
                "`{display}` points at missing canonical command `{canonical}`"
            );
        } else {
            assert!(
                contract.canonical_kind.is_none() && contract.canonical_note.is_none(),
                "`{display}` cannot describe a canonical action without a canonical command"
            );
        }
        if contract.persists_op_id {
            assert!(
                contract.supports_op_id,
                "`{display}` cannot persist op-ids unless it supports op-id replay"
            );
            assert!(
                contract.mutates,
                "`{display}` cannot persist op-ids for an observe-only command"
            );
        }
        if contract.observe_only {
            assert!(
                !contract.mutates,
                "`{display}` cannot be both observe_only and mutating"
            );
            assert!(
                !contract.supports_op_id && !contract.persists_op_id,
                "`{display}` observe-only commands must not reserve op-id slots"
            );
            assert!(
                !contract.may_initialize
                    && !contract.may_import_git
                    && !contract.may_write_worktree
                    && !contract.may_move_ref
                    && !contract.destructive_requires_force
                    && !contract.writes_heddle_refs
                    && !contract.writes_git_refs
                    && !contract.writes_worktree
                    && !contract.writes_config
                    && !contract.writes_hooks
                    && !contract.network_io
                    && !contract.daemon_process
                    && !contract.object_gc
                    && !contract.external_command
                    && !contract.requires_git_executable
                    && !contract.destructive_data,
                "`{display}` observe-only commands must not advertise write side effects"
            );
        }
        assert!(
            !contract.requires_git_executable,
            "`{display}` must not require a `git` executable; Git-format work belongs in native/library code"
        );
        let effects = side_effects(contract);
        assert!(
            !effects.is_empty(),
            "`{display}` must advertise at least one side effect"
        );
        if contract.observe_only {
            assert_eq!(
                effects,
                vec!["observe_only"],
                "`{display}` observe-only side_effects must stay exact"
            );
        } else {
            for (flag, effect) in [
                (contract.may_initialize, "initialize"),
                (contract.may_import_git, "import_git"),
                (contract.writes_heddle_refs, "writes_heddle_refs"),
                (contract.writes_git_refs, "writes_git_refs"),
                (contract.writes_worktree, "writes_worktree"),
                (contract.writes_config, "writes_config"),
                (contract.writes_hooks, "writes_hooks"),
                (contract.network_io, "network_io"),
                (contract.daemon_process, "daemon_process"),
                (contract.object_gc, "object_gc"),
                (contract.external_command, "external_command"),
                (
                    contract.destructive_requires_force,
                    "destructive_requires_force",
                ),
                (contract.destructive_data, "destructive_data"),
            ] {
                assert_eq!(
                    effects.contains(&effect),
                    flag,
                    "`{display}` side_effects must mirror `{effect}`"
                );
            }
            if contract.may_write_worktree && !contract.writes_worktree {
                assert!(
                    effects.contains(&"may_write_worktree"),
                    "`{display}` side_effects must preserve flag-sensitive worktree writes"
                );
            }
        }
        assert_eq!(
            contract.may_move_ref,
            contract.writes_heddle_refs || contract.writes_git_refs,
            "`{display}` may_move_ref must summarize concrete ref dimensions"
        );
        if contract.destructive_requires_force {
            assert!(
                contract.mutates,
                "`{display}` destructive commands must be mutating commands"
            );
        }
        let schema_verbs = contract_schema_verbs(contract).collect::<Vec<_>>();
        let documented_schema_verbs =
            contract_documented_schema_verbs(contract).collect::<Vec<_>>();
        for verb in &documented_schema_verbs {
            assert!(
                schema_verbs.contains(verb),
                "`{display}` documents schema verb `{verb}` without registering it"
            );
        }
        for verb in contract.opaque_schema_verbs {
            assert!(
                schema_verbs.contains(verb),
                "`{display}` marks schema verb `{verb}` opaque without registering it"
            );
            assert!(
                documented_schema_verbs.contains(verb),
                "`{display}` marks schema verb `{verb}` opaque without documenting it"
            );
        }
        if !schema_verbs.is_empty() {
            assert!(
                contract.supports_json,
                "`{display}` registers JSON schema verbs but does not support JSON"
            );
        }
    }
}

#[cfg(not(feature = "git-overlay"))]
#[test]
fn native_only_catalog_excludes_git_overlay_commands() {
    let catalog = build_command_catalog();
    for display in [
        "bridge",
        "bridge git",
        "bridge git status",
        "bridge git init",
        "bridge git import",
        "bridge git export",
        "bridge git sync",
        "bridge git reconcile",
        "bridge git push",
        "bridge git pull",
        "bridge git reason",
        "git-overlay",
    ] {
        assert!(
            catalog.command_by_display(display).is_none(),
            "native-only catalog must not advertise git-overlay command `{display}`"
        );
        assert!(
            command_runtime_contract(display).is_none(),
            "native-only runtime contracts must not resolve git-overlay command `{display}`"
        );
    }
}

#[test]
fn json_kind_marks_streaming_command_surfaces() {
    let catalog = build_command_catalog();
    for (display, kind) in [
        ("watch", "jsonl"),
        ("rebase", "jsonl"),
        ("status", "json_or_jsonl"),
        ("thread show", "json_or_jsonl"),
    ] {
        let entry = catalog
            .commands
            .iter()
            .find(|entry| entry.display == display)
            .unwrap_or_else(|| panic!("missing command catalog entry for `{display}`"));
        assert_eq!(
            entry.json_kind, kind,
            "`{display}` must advertise its streaming JSON contract"
        );
    }
}

#[test]
fn hidden_clap_flags_are_present_in_machine_catalog() {
    fn walk_clap_hidden_flags(
        command: &clap::Command,
        path: &mut Vec<String>,
        hidden_flags: &mut Vec<(String, String)>,
    ) {
        let display = path.join(" ");
        for arg in command.get_arguments() {
            if !arg.is_hide_set() {
                continue;
            }
            if arg.get_long().is_some() || arg.get_short().is_some() {
                hidden_flags.push((display.clone(), arg.get_id().as_str().to_string()));
            }
        }

        for subcommand in command.get_subcommands() {
            path.push(subcommand.get_name().to_string());
            walk_clap_hidden_flags(subcommand, path, hidden_flags);
            path.pop();
        }
    }

    let clap = Cli::command();
    let catalog = build_command_catalog();
    let mut hidden_flags = Vec::new();
    walk_clap_hidden_flags(&clap, &mut Vec::new(), &mut hidden_flags);

    let mut failures = Vec::new();
    for (display, id) in hidden_flags {
        if display.is_empty() {
            let Some(option) = catalog.global_options.iter().find(|option| option.id == id) else {
                failures.push(format!("global hidden flag `{id}` missing from catalog"));
                continue;
            };
            if !option.hidden {
                failures.push(format!(
                    "global hidden flag `{id}` is cataloged but hidden=false"
                ));
            }
            continue;
        }

        let Some(entry) = catalog.command_by_display(&display) else {
            failures.push(format!(
                "{display}: hidden flag `{id}` command missing from catalog"
            ));
            continue;
        };
        let Some(option) = entry.options.iter().find(|option| option.id == id) else {
            failures.push(format!(
                "{display}: hidden flag `{id}` missing from catalog options"
            ));
            continue;
        };
        if !option.hidden {
            failures.push(format!(
                "{display}: hidden flag `{id}` is cataloged but hidden=false"
            ));
        }
    }

    assert!(
        failures.is_empty(),
        "every clap `hide = true` flag must remain machine-discoverable in \
         `heddle help --output json` with `hidden: true`:\n  {}",
        failures.join("\n  ")
    );
}

#[test]
fn json_discriminator_table_starts_with_bounded_command_slice() {
    // Wire-format-stable list. PR #251 instrumented the initial set;
    // heddle#272 swept the named-by-persona verbs (stack, goto, fork,
    // revert, purge, redact, stash, clean, discuss, context, review,
    // cherry-pick, bisect); heddle#641 swept the remaining verbs whose
    // runtime JSON already emits `output_kind` (abort, adopt, the agent
    // session verbs, bridge git push/pull, continue, daemon stop,
    // doctor, expand, fetch, land, log,
    // maintenance gc/index, merge --preview, pull, push, query, ready,
    // the remote family, start, switch, sync, and the thread lifecycle
    // verbs). Any further sweep MUST extend this list and document the
    // addition.
    //
    // `clone` appears twice because hosted `clone --output json`
    // emits two JSON records per invocation (a preliminary
    // `clone_connection` envelope followed by the final `clone`
    // payload); both discriminator values are advertised so agents
    // can route on either record. See heddle#272 (PR #281 r3).
    let displays = raw_json_discriminator_specs()
        .iter()
        .map(|(path, _)| path.join(" "))
        .collect::<Vec<_>>();
    assert_eq!(
        displays,
        vec![
            // heddle#641 swept every remaining verb whose runtime JSON
            // already carries `output_kind` (probed live + verified against
            // the emitting structs). The values are the RUNTIME truths, not
            // snake-cased display paths — see the overrides documented in
            // tests/cli_integration/output_kind_invariant.rs.
            "abort",
            "adopt",
            "actor spawn",
            "actor list",
            "actor show",
            "actor explain",
            "actor done",
            "agent serve",
            "agent status",
            "agent stop",
            "agent capture",
            "agent ready",
            "agent task create",
            "agent task list",
            "agent task show",
            "agent task update",
            "agent fanout plan",
            "agent fanout start",
            "auth logout",
            "auth status",
            "auth create-service-token",
            "bridge git status",
            "bridge git import",
            "bridge git sync",
            "bridge git reconcile",
            "bridge git push",
            "bridge git pull",
            "capture",
            "checkpoint",
            "cherry-pick",
            "clean",
            "clone",
            "clone",
            "expand",
            "commit",
            "continue",
            "context set",
            "context get",
            "context list",
            "context history",
            "context edit",
            "context supersede",
            "context rm",
            "context check",
            "context suggest",
            "context audit",
            "daemon stop",
            "diff",
            "discuss open",
            "discuss append",
            "discuss resolve",
            "discuss list",
            "discuss show",
            "doctor",
            "doctor docs",
            "doctor schemas",
            "fetch",
            "oplog recover",
            "help",
            "init",
            // `log` appears three times: the entry advertises `log`,
            // `log --reflog`, and `log --timeline` variants, mirroring
            // `undo`/`clone`.
            "log",
            "log",
            "log",
            "maintenance gc",
            "maintenance index",
            "merge",
            "pull",
            "push",
            "query",
            "query",
            "ready",
            "rebase",
            "redact apply",
            "redact list",
            "redact show",
            "redact purge apply",
            "redact purge list",
            "redact trust add",
            "redact trust list",
            "redact trust remove",
            "remote list",
            "remote add",
            "remote remove",
            "remote set-default",
            "remote show",
            "resolve",
            "revert",
            "review show",
            "review sign",
            "review next",
            "review health",
            "schemas",
            "land",
            "show",
            "start",
            "stash list",
            "stash show",
            "status",
            "support grant",
            "support list",
            "support revoke",
            "switch",
            "sync",
            "thread create",
            "thread switch",
            "thread list",
            "thread show",
            "thread rename",
            "thread refresh",
            "thread resolve",
            "thread promote",
            "thread drop",
            "thread revoke-approval",
            "thread cleanup",
            "thread marker list",
            "thread marker create",
            "thread marker delete",
            "thread marker show",
            "timeline status",
            "timeline record-start",
            "timeline record-finish",
            "timeline fork",
            "timeline reset",
            "timeline recover",
            "verify",
            "visibility set",
            "visibility promote",
            "visibility show",
            "visibility list",
            "undo",
            "undo",
            "undo",
        ]
    );
}

/// heddle#641 close-the-class conformance: every schema verb whose
/// JSON schema declares an `output_kind` property MUST have a
/// catalog discriminator whose value matches the schema's const.
///
/// Why this closes the gap: `schema_for_verb` only injects the
/// `output_kind` enum-const into a schema when the catalog
/// advertises a discriminator for that verb. A verb whose mirror
/// struct declares `output_kind` (because the runtime payload
/// emits it) but whose catalog entry lacks the discriminator
/// therefore surfaces here as a *const-less* `output_kind`
/// property — exactly the advertise-nothing gap that left ~111
/// schema-bearing commands with `json_discriminators: []`. A new
/// command that emits `output_kind` without registering its
/// discriminator fails this test; one that registers a
/// discriminator that diverges from the schema const fails the
/// mismatch arm. Runtime-vs-catalog equality is enforced
/// separately by `tests/cli_integration/output_kind_invariant.rs`.
#[test]
fn schema_output_kind_discriminators_are_complete_and_consistent() {
    use std::collections::BTreeSet;

    use crate::cli::commands::schema_for_verb;

    fn resolve_schema_ref<'a>(
        root: &'a serde_json::Value,
        reference: &str,
    ) -> &'a serde_json::Value {
        reference
            .strip_prefix("#/$defs/")
            .or_else(|| reference.strip_prefix("#/definitions/"))
            .and_then(|name| {
                root.get("$defs")
                    .or_else(|| root.get("definitions"))
                    .and_then(|defs| defs.get(name))
            })
            .unwrap_or_else(|| panic!("schema reference `{reference}` resolves"))
    }

    fn collect_output_kind_values<'a>(
        root: &'a serde_json::Value,
        schema: &'a serde_json::Value,
        values: &mut BTreeSet<String>,
    ) {
        if let Some(reference) = schema.get("$ref").and_then(|value| value.as_str()) {
            collect_output_kind_values(root, resolve_schema_ref(root, reference), values);
            return;
        }

        if let Some(enum_values) = schema
            .get("properties")
            .and_then(|properties| properties.get("output_kind"))
            .and_then(|property| property.get("enum"))
            .and_then(|values| values.as_array())
        {
            values.extend(
                enum_values
                    .iter()
                    .filter_map(|value| value.as_str())
                    .map(str::to_string),
            );
        } else if let Some(value) = schema
            .get("properties")
            .and_then(|properties| properties.get("output_kind"))
            .and_then(|property| property.get("const"))
            .and_then(|value| value.as_str())
        {
            values.insert(value.to_string());
        }

        for combinator in ["anyOf", "oneOf", "allOf"] {
            if let Some(schemas) = schema.get(combinator).and_then(|value| value.as_array()) {
                for schema in schemas {
                    collect_output_kind_values(root, schema, values);
                }
            }
        }
    }

    let mut missing = Vec::new();
    let mut mismatched = Vec::new();
    let mut checked = 0usize;

    for verb in schema_verbs() {
        let Some(schema) = schema_for_verb(verb) else {
            panic!("catalog schema verb `{verb}` has no registered schema");
        };
        let mut actual = BTreeSet::new();
        collect_output_kind_values(&schema, &schema, &mut actual);
        if actual.is_empty() {
            // The schema does not declare `output_kind` — the verb's
            // runtime payload genuinely lacks the discriminator (the
            // UNSWEPT_TODO rolldown in output_kind_invariant.rs).
            continue;
        };
        checked += 1;

        let mut expected_discriminators = command_json_discriminators_for_schema_verb(verb);
        if schema.get("anyOf").is_some() {
            expected_discriminators.extend(command_json_discriminators().into_iter().filter(
                |discriminator| {
                    discriminator.display == verb
                        && discriminator.schema_verb.as_deref() != Some(verb)
                },
            ));
        }
        let expected = expected_discriminators
            .into_iter()
            .filter(|discriminator| discriminator.field == "output_kind")
            .map(|discriminator| discriminator.value)
            .collect::<BTreeSet<_>>();
        if expected.is_empty() {
            missing.push(format!(
                "`{verb}`: schema declares an `output_kind` property but the \
                 catalog advertises no json_discriminator for it"
            ));
            continue;
        }

        if actual != expected {
            mismatched.push(format!(
                "`{verb}`: schema output_kind values {actual:?} != catalog discriminators {expected:?}"
            ));
        }
    }

    assert!(
        missing.is_empty() && mismatched.is_empty(),
        "Catalog json_discriminators drift from the schema `output_kind` contract. \
         Register the discriminator with `json_discriminator(Some(\"<verb>\"), \
         \"output_kind\", \"<runtime value>\")` on the command's catalog entry \
         (the value must match what the command actually emits).\n\nMissing:\n  - {}\n\nMismatched:\n  - {}",
        missing.join("\n  - "),
        mismatched.join("\n  - ")
    );
    assert!(
        checked >= 90,
        "expected the conformance sweep to inspect the full discriminator \
         surface (~95 schema verbs declare `output_kind` after #473 phases 1-2); only {checked} \
         were checked — the schema injection or verb collection likely regressed"
    );
}

#[test]
fn json_discriminator_metadata_is_internally_consistent() {
    let raw_discriminators = raw_json_discriminator_specs();
    // A single command path MAY advertise more than one
    // discriminator (e.g. `clone` carries both `clone` and
    // `clone_connection` because hosted `clone --output json`
    // emits a preliminary connection envelope before the final
    // payload — see heddle#272). But each (path, value) pair must
    // still be unique, otherwise two entries would advertise the
    // same wire-format token and agents couldn't tell them apart.
    let path_value_pairs = raw_discriminators
        .iter()
        .map(|(path, d)| (path.to_vec(), d.value))
        .collect::<BTreeSet<_>>();
    assert_eq!(
        path_value_pairs.len(),
        raw_discriminators.len(),
        "JSON discriminator table contains duplicate (path, value) pairs"
    );

    let mut schema_verb_values = std::collections::BTreeMap::new();
    for (path, discriminator) in raw_discriminators {
        let display = path.join(" ");
        let contract = raw_command_contract_for_path(path.iter().copied())
            .unwrap_or_else(|| panic!("JSON discriminator references unknown `{display}`"));
        assert!(
            contract.supports_json,
            "`{display}` advertises JSON discriminator `{}` but does not support JSON",
            discriminator.value
        );
        assert!(
            matches!(discriminator.field, "kind" | "output_kind"),
            "`{display}` advertises unsupported discriminator field `{}`",
            discriminator.field
        );
        assert!(
            !discriminator.value.is_empty(),
            "`{display}` discriminator value must be non-empty"
        );

        if let Some(schema_verb) = discriminator.schema_verb {
            let schema_value = (discriminator.field, discriminator.value);
            if let Some(previous) = schema_verb_values.insert(schema_verb, schema_value) {
                assert_eq!(
                    previous, schema_value,
                    "JSON discriminator schema verb `{schema_verb}` is registered with \
                     conflicting discriminator values"
                );
            }
            assert!(
                contract_schema_verbs(contract).any(|verb| verb == schema_verb),
                "`{display}` advertises discriminator schema verb `{schema_verb}` not present in its command contract"
            );
            assert!(
                discriminator.no_schema_reason.is_none(),
                "`{display}` cannot have both a schema verb and a no-schema reason"
            );
        } else {
            assert!(
                discriminator
                    .no_schema_reason
                    .is_some_and(|reason| !reason.is_empty()),
                "`{display}` discriminator without a schema verb must document why"
            );
        }
    }
}

#[test]
fn command_catalog_exposes_active_json_discriminator_metadata() {
    let catalog = build_command_catalog();
    let active = command_json_discriminators();
    for discriminator in &active {
        let entry = catalog
            .commands
            .iter()
            .find(|command| command.display == discriminator.display)
            .unwrap_or_else(|| {
                panic!(
                    "active JSON discriminator references missing command `{}`",
                    discriminator.display
                )
            });
        assert!(entry.supports_json);
        assert!(
            entry
                .json_discriminators
                .iter()
                .any(|entry_discriminator| entry_discriminator == discriminator),
            "`{}` catalog entry must expose its JSON discriminator metadata",
            discriminator.display
        );
    }

    let status = catalog
        .commands
        .iter()
        .find(|entry| entry.display == "status")
        .expect("status should be cataloged");
    assert_eq!(status.json_discriminators.len(), 1);
    assert_eq!(status.json_discriminators[0].field, "output_kind");
    assert_eq!(status.json_discriminators[0].value, "status");
}

fn raw_json_discriminator_specs() -> Vec<(&'static [&'static str], CommandJsonDiscriminatorSpec)> {
    CONTRACTS
        .iter()
        .flat_map(|entry| {
            contract_json_discriminators(entry.contract)
                .map(move |discriminator| (entry.path, discriminator))
        })
        .collect()
}

#[test]
fn catalog_option_lookup_includes_globals_and_finite_values() {
    let catalog = build_command_catalog();

    let start_options = catalog
        .options_for_display("start")
        .expect("start should be cataloged");
    let output = start_options
        .iter()
        .find(|option| option.long.as_deref() == Some("output"))
        .expect("global --output should be included in command options");
    assert_eq!(output.possible_values, vec!["json", "json-compact", "text"]);
    for command in &catalog.commands {
        assert!(
            !command
                .options
                .iter()
                .any(|option| option.long.as_deref() == Some("json")),
            "legacy --json should not be included in command options for {}",
            command.path.join(" ")
        );
    }
    assert!(
        start_options
            .iter()
            .any(|option| option.long.as_deref() == Some("help")),
        "generated --help should be included in command options"
    );

    let workspace = start_options
        .iter()
        .find(|option| option.long.as_deref() == Some("workspace"))
        .expect("start --workspace should be cataloged");
    assert_eq!(
        workspace.possible_values,
        vec!["auto", "materialized", "virtualized", "solid"]
    );

    let context_set_options = catalog
        .options_for_path(&["context".to_string(), "set".to_string()])
        .expect("context set should be cataloged");
    let scope = context_set_options
        .iter()
        .find(|option| option.long.as_deref() == Some("scope"))
        .expect("context set --scope should be cataloged");
    assert!(
        scope.possible_values.is_empty(),
        "context scope accepts open-ended values like symbol:<name>"
    );
    let kind = context_set_options
        .iter()
        .find(|option| option.long.as_deref() == Some("kind"))
        .expect("context set --kind should be cataloged");
    assert_eq!(
        kind.possible_values,
        vec!["constraint", "invariant", "rationale"]
    );

    let integration_install_options = catalog
        .options_for_display("integration install")
        .expect("integration install should be cataloged");
    let scope = integration_install_options
        .iter()
        .find(|option| option.long.as_deref() == Some("scope"))
        .expect("integration install --scope should be cataloged");
    assert_eq!(scope.possible_values, vec!["repo", "user"]);
    assert_eq!(scope.aliases, vec!["harness-install-scope"]);
}

#[test]
fn command_contract_table_drives_help_tiers() {
    let catalog = build_command_catalog();
    for (display, tier, surface, visibility, canonical, canonical_kind, executable) in [
        (
            "status", "everyday", "native", "everyday", None, None, false,
        ),
        (
            "verify", "everyday", "native", "everyday", None, None, false,
        ),
        (
            "commit", "everyday", "native", "everyday", None, None, false,
        ),
        ("land", "everyday", "native", "everyday", None, None, false),
        ("push", "everyday", "native", "everyday", None, None, false),
        (
            "capture", "advanced", "native", "advanced", None, None, false,
        ),
        (
            "thread create",
            "advanced",
            "native",
            "advanced",
            None,
            None,
            false,
        ),
        (
            "thread promote",
            "advanced",
            "native",
            "advanced",
            None,
            None,
            false,
        ),
        (
            "checkpoint",
            "advanced",
            "native",
            "advanced",
            None,
            None,
            false,
        ),
        (
            "switch",
            "advanced",
            "git_adapter",
            "git_adapter",
            Some("thread switch"),
            Some("direct_command"),
            false,
        ),
    ] {
        let entry = catalog
            .commands
            .iter()
            .find(|entry| entry.display == display)
            .unwrap_or_else(|| panic!("missing command catalog entry for `{display}`"));
        assert_eq!(entry.tier, tier);
        assert_eq!(entry.surface, surface);
        assert_eq!(entry.help_visibility, visibility);
        assert_eq!(entry.canonical_command.as_deref(), canonical);
        assert_eq!(
            entry
                .canonical_action
                .as_ref()
                .map(|action| action.kind.as_str()),
            canonical_kind
        );
        assert_eq!(
            entry
                .canonical_action
                .as_ref()
                .is_some_and(|action| action.executable),
            executable
        );
        assert_eq!(command_help_tier(display), tier);
        assert_eq!(command_surface(display), surface);
        assert_eq!(command_help_visibility(display), visibility);
        assert_eq!(command_canonical_command(display), canonical);
    }
    for (display, canonical, kind) in [
        ("stash pop", "undo", "conceptual_home"),
        ("fetch", "pull", "workflow"),
    ] {
        let entry = catalog
            .commands
            .iter()
            .find(|entry| entry.display == display)
            .unwrap_or_else(|| panic!("missing command catalog entry for `{display}`"));
        let action = entry
            .canonical_action
            .as_ref()
            .unwrap_or_else(|| panic!("`{display}` should expose a canonical action"));
        assert_eq!(action.command, canonical);
        assert_eq!(action.kind, kind);
        assert!(
            !action.executable,
            "`{display}` is not a direct command replacement"
        );
        assert!(!action.note.is_empty());
    }
    assert_eq!(command_help_tier("transaction"), "hidden");

    let thread_list = catalog
        .commands
        .iter()
        .find(|entry| entry.display == "thread list")
        .expect("thread list should be cataloged");
    assert_eq!(thread_list.tier, "advanced");
}

#[test]
fn parsed_command_op_id_support_reads_contract_table() {
    for (argv, expected) in [
        (vec!["heddle", "status"], false),
        (vec!["heddle", "commit", "-m", "checkpoint"], true),
        (vec!["heddle", "thread", "list"], false),
        (vec!["heddle", "thread", "drop", "feature"], true),
    ] {
        let cli = Cli::try_parse_from(argv.clone())
            .unwrap_or_else(|err| panic!("failed to parse {argv:?}: {err}"));
        let display = command_path(&cli.command).join(" ");
        assert_eq!(
            command_supports_op_id_for_command(&cli.command),
            expected,
            "`{display}` op-id support must come from its parsed command contract"
        );
        assert_eq!(
            command_supports_op_id(&display),
            expected,
            "`{display}` string lookup must agree with parsed command contract"
        );
    }
}

#[test]
fn parsed_command_json_support_reads_contract_table() {
    for (argv, expected) in [
        (vec!["heddle", "status"], true),
        (vec!["heddle", "help"], true),
        (vec!["heddle", "shell", "completion", "bash"], false),
        (vec!["heddle", "thread", "cd", "feature"], false),
    ] {
        let cli = Cli::try_parse_from(argv.clone())
            .unwrap_or_else(|err| panic!("failed to parse {argv:?}: {err}"));
        let display = command_path(&cli.command).join(" ");
        let entry = build_command_catalog()
            .commands
            .into_iter()
            .find(|entry| entry.display == display)
            .unwrap_or_else(|| panic!("missing command catalog entry for `{display}`"));
        assert_eq!(
            command_supports_json_for_command(&cli.command),
            expected,
            "`{display}` JSON support must come from its parsed command contract"
        );
        assert_eq!(entry.supports_json, expected);
    }
}

#[test]
fn parsed_command_runtime_contract_exposes_catalog_fields() {
    let cli = Cli::try_parse_from(["heddle", "thread", "drop", "feature"])
        .expect("thread drop should parse");
    let runtime = command_runtime_contract_for_command(&cli.command);
    let catalog = build_command_catalog();
    let entry = catalog
        .commands
        .iter()
        .find(|entry| entry.display == runtime.display)
        .expect("runtime command should be present in catalog");

    assert_eq!(runtime.path, vec!["thread", "drop"]);
    assert_eq!(runtime.supports_json, entry.supports_json);
    assert_eq!(runtime.supports_op_id, entry.supports_op_id);
    assert_eq!(runtime.persists_op_id, entry.persists_op_id);
    assert_eq!(
        runtime.uses_bootstrap_op_id_store,
        entry.op_id_store_scope == "bootstrap"
    );
    assert_eq!(runtime.help_visibility, entry.help_visibility);
    assert_eq!(runtime.help_rank, entry.help_rank);
    assert_eq!(runtime.surface, entry.surface);
}

#[test]
fn op_id_persistence_reads_contract_table() {
    let catalog = build_command_catalog();
    for (display, persists, store_scope) in [
        ("capture", false, "repository"),
        ("review sign", false, "repository"),
        ("commit", false, "repository"),
        ("status", false, "none"),
        ("init", false, "bootstrap"),
        ("adopt", false, "bootstrap"),
        ("clone", false, "bootstrap"),
        ("bridge git init", false, "bootstrap"),
    ] {
        let entry = catalog
            .commands
            .iter()
            .find(|entry| entry.display == display)
            .unwrap_or_else(|| panic!("missing command catalog entry for `{display}`"));
        assert_eq!(
            entry.persists_op_id, persists,
            "`{display}` op-id persistence must be cataloged"
        );
        assert_eq!(
            entry.op_id_store_scope, store_scope,
            "`{display}` op-id store scope must be cataloged"
        );
        assert_eq!(
            command_persists_op_id(display),
            persists,
            "`{display}` runtime op-id persistence must come from the contract table"
        );
        assert_eq!(
            command_uses_bootstrap_op_id_store(display),
            store_scope == "bootstrap",
            "`{display}` runtime op-id store scope must come from the contract table"
        );
        if persists {
            assert!(
                entry.supports_op_id,
                "`{display}` cannot persist op-ids unless it supports op-id replay"
            );
        }
    }
}

#[test]
fn feature_gated_command_roots_are_catalog_owned() {
    assert_eq!(
        feature_gated_command_roots(),
        &["auth", "presence", "support"]
    );
}