coremlit 0.2.0

Safe, synchronous CoreML runtime for macOS (CPU/GPU/Neural Engine) with opt-in on-device multimodal pipelines: speech (Whisper STT, forced alignment, speaker diarization, Silero VAD), AudioSet sound-event tagging, and audio/text/image embeddings (CLAP, granite, SigLIP)
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
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
//! Golden feature-map test — pins the mono-crate flat feature contract.
//!
//! The restructure collapsed five crates into feature-gated modules and renamed
//! each per-crate feature to a flat one (`FEATURE_MAP.md`). This test PINS that
//! contract against its three sources of truth, so a renamed, dropped,
//! re-composed, or cross-kit-leaking feature — or a silently dropped CI combo or
//! clippy flag — cannot land:
//!
//!   1. `Cargo.toml` `[features]` — the exact feature-name set AND the exact
//!      dependency set of every feature (a leak like `whisper` pulling `vad`
//!      changes a set and reds). BOTH manifests: this crate's and the sibling
//!      `coremlit-parity`'s, which owns the three third-party parity oracles.
//!   2. `FEATURE_MAP.md`'s rename table — parsed (not substring-scanned) so a
//!      removed/altered bare-crate row reds even if the token survives elsewhere
//!      in the doc.
//!   3. `.github/workflows/ci.yml` — the curated `--features` combo matrices,
//!      parsed structurally PER JOB and compared as exact sets, so dropping OR
//!      commenting out any curated combo (including the bare-core `""`) reds.
//!      Two jobs carry one: `features` (this crate) and `parity` (the oracles).
//!      Those two jobs' clippy steps are pinned as well — the package,
//!      `--no-deps`, `--all-targets` and `-- -D warnings` on every arm, the
//!      `if: ${{ !cancelled() }}`, and the `clippy` component installed ahead of
//!      the step — because a row whose lint step lost a flag still runs green
//!      while it lints less (#158).
//!
//!      Five more pieces of that file are pinned that no matrix row reaches: the
//!      `check` job's toolchain install (the `rustfmt` and `clippy` components,
//!      asked for ahead of the `cargo fmt` and `cargo clippy` steps that need
//!      them), its fmt step (`--all --check`, the second of which is what makes
//!      it a gate), its clippy step (`--all-targets --all-features`, `-- -D
//!      warnings`, and no `-p`, since it is the one pass that lints the whole
//!      workspace with every feature on), its doc step (`--no-deps
//!      --all-features` under `RUSTDOCFLAGS: -D warnings`), and the `if:` of
//!      every `model-tests` step after the staging step, which is what keeps one
//!      red step from marking the checks after it `skipped`, together with the
//!      `id:` the closing `Gate ledger` must read for each of them and the
//!      roster of checks every shard runs.
//!
//! The oracle features (`speaker-oracle`, `clap-oracle`, `vad-bundled`) are NOT
//! this crate's any more — `dia` and `textclap` are unpublished git sources that
//! `cargo publish` rejects, so they and their nine parity binaries moved to the
//! never-published `coremlit-parity` package. `align-oracle` did NOT move: it
//! only turns on a feature of `asry`, a dependency this crate keeps either way.
//!
//! Hermetic: pure file reads (via `CARGO_MANIFEST_DIR`), no models, no cargo
//! invocation, no feature needs enabling.
//!
//! The package ships this test, `Cargo.toml` and `FEATURE_MAP.md`, but neither
//! `.github/workflows/ci.yml` nor the sibling `coremlit-parity` package, so a
//! `cargo test` from the published tarball finds neither of those. The pins that
//! read them skip there, each naming the missing file on stderr. Inside a
//! workspace a missing file is a failure, so deleting the workflow does not
//! quietly turn its pins off.

// The workspace-root anchor, FOUND by searching upward for the `[workspace]`
// manifest rather than counted in `../` hops — see its module doc.
#[path = "support/workspace_root.rs"]
#[allow(dead_code)]
mod workspace_root;

use std::{collections::BTreeSet, path::Path};

/// Read a file addressed relative to the crate manifest directory — one the
/// published tarball carries too.
fn read_rel(rel: &str) -> String {
  std::fs::read_to_string(Path::new(env!("CARGO_MANIFEST_DIR")).join(rel))
    .unwrap_or_else(|e| panic!("read {rel}: {e}"))
}

fn manifest() -> String {
  read_rel("Cargo.toml")
}

/// What a pin prints when the repository is not there to read.
fn skip_notice(rel: &str) -> String {
  format!("feature_map: skipped — {rel} is not in this source tree (a published tarball?)")
}

/// A file of the repository outside this crate's directory, addressed from the
/// workspace root, or `None` — said by name on stderr — when there is no
/// workspace root to address it from.
///
/// No root is what a `cargo test` from the published tarball looks like: the
/// package carries neither `.github/workflows/ci.yml` nor the sibling
/// `coremlit-parity` package, and nothing above it declares a `[workspace]`. A
/// root that IS there makes the file's absence a failure and not a skip, so a
/// deleted workflow reds its pins instead of silently ending them.
fn repo_file(root: Option<&Path>, rel: &str) -> Option<String> {
  let Some(root) = root else {
    eprintln!("{}", skip_notice(rel));
    return None;
  };
  let path = root.join(rel);
  Some(std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {e}", path.display())))
}

/// The sibling `coremlit-parity` manifest, found from the workspace root, or
/// `None` outside the repository.
fn parity_manifest() -> Option<String> {
  repo_file(
    workspace_root::try_workspace_root().as_deref(),
    "coremlit-parity/Cargo.toml",
  )
}

/// `.github/workflows/ci.yml`, at the workspace root — found, not counted — or
/// `None` outside the repository.
fn ci_yml() -> Option<String> {
  repo_file(
    workspace_root::try_workspace_root().as_deref(),
    ".github/workflows/ci.yml",
  )
}

/// The intended flat feature graph — the single in-test source that both the
/// name-set and the per-feature dependency-set assertions are driven from. Any
/// drift in `Cargo.toml` (a renamed feature, a dropped dep, or a CROSS-KIT LEAK
/// such as adding `"vad"` to `whisper`) changes a set here and reds.
fn expected_features() -> Vec<(&'static str, Vec<&'static str>)> {
  vec![
    ("default", vec![]),
    ("serde", vec!["dep:serde", "windit?/serde"]),
    ("tracing", vec!["dep:tracing"]),
    (
      "whisper",
      vec![
        "dep:libc",
        "dep:mach2",
        "dep:rand",
        "dep:serde_json",
        "dep:tokenizers",
        "dep:unicode_categories",
      ],
    ),
    (
      "nl-recognizer",
      vec!["whisper", "dep:objc2-natural-language"],
    ),
    // `dep:humantime` is `AlignerOptions`'s canonical `Display`; `dep:serde_json`
    // and `dep:serde` read a model's own `{token: id}` vocabulary
    // (`vocab::Vocabulary`), refusing a repeated token; `dep:tokenizers` parses
    // the tokenizer document asry's seam parses again at load, to read back the
    // columns the seam reserves. asry's `emissions` feature already pulls
    // `tokenizers` 0.23, which depends on serde and serde_json, so those entries
    // add no crate version to the lockfile.
    (
      "align",
      vec![
        "dep:asry",
        "dep:humantime",
        "dep:serde_json",
        "dep:serde",
        "dep:tokenizers",
      ],
    ),
    ("align-oracle", vec!["align", "asry/alignment"]),
    ("speaker", vec!["dep:diaric"]),
    ("vad", vec!["dep:zuoer"]),
    // `serde_json` is `embeddings::tokenizer_guard`, shared with `siglip`: the
    // guard reads a post-processor's structure back out through its serde
    // representation, because `TemplateProcessing` exposes no accessor for it.
    // Not a cross-kit leak — `tokenizers` already depends on `serde_json`, so
    // the row adds an entry here without adding a crate to the graph.
    (
      "clap",
      vec![
        "dep:rustfft",
        "dep:tokenizers",
        "dep:windit",
        "dep:serde_json",
      ],
    ),
    (
      "granite",
      // `dep:serde_json`: the BPE merge table read back out of the loaded model
      // for token_index's separatorless fast lane (#72); tokenizers depends on
      // serde_json itself, so no crate is added to the graph.
      vec![
        "dep:tokenizers",
        "dep:windit",
        "windit/text",
        "dep:sha2",
        "dep:serde_json",
      ],
    ),
    (
      "ced",
      vec!["dep:rustfft", "dep:soundevents-dataset", "dep:windit"],
    ),
    ("lid", vec!["dep:rustfft", "dep:windit"]),
    // No `windit`: this door embeds ONE window and the caller owns any
    // windowing or averaging across several of them, so the shared window
    // engine that `ced`/`lid`/`granite` need has nothing to do here.
    ("identity", vec!["dep:rustfft"]),
    // `dep:serde_json`: the same shared `embeddings::tokenizer_guard` as `clap`.
    (
      "siglip",
      vec!["dep:tokenizers", "dep:pixon", "dep:sha2", "dep:serde_json"],
    ),
    // `face`'s only dependency is `sha2`, and it is there for one reason:
    // `FaceEmbedder::load` hashes the artifact directory it loads, so an
    // embedding carries the identity of the WEIGHTS that produced it rather
    // than of a schema two unrelated artifacts could share (issue #138 §3).
    // The exact dep set is the assertion — a second one reds this row.
    ("face", vec!["dep:sha2"]),
    // The ONE `commercial-` feature. It adds no dependency at all — the artifact
    // it gates is a manifest of four constants — so the exact set is `["face"]`
    // and nothing else, and a second entry here would mean the gate had started
    // pulling code rather than protecting bytes.
    ("commercial-face-arcface", vec!["face"]),
  ]
}

/// The intended feature graph of the sibling `coremlit-parity` package — the
/// oracle half of the contract, pinned with the same exact-set discipline. Each
/// oracle rides its OWN feature (so one row's `ort` build is not forced on the
/// others) and turns on exactly the `coremlit` module feature it measures; a
/// leak like `clap-oracle` pulling `coremlit/speaker` changes a set and reds.
fn expected_parity_features() -> Vec<(&'static str, Vec<&'static str>)> {
  vec![
    ("default", vec![]),
    (
      "speaker-oracle",
      vec![
        "coremlit/speaker",
        "dep:dia",
        "dep:diaric",
        "dia/ort",
        "dia/bundled-segmentation",
      ],
    ),
    ("clap-oracle", vec!["coremlit/clap", "dep:textclap"]),
    (
      "vad-bundled",
      vec!["coremlit/vad", "dep:silero", "silero/bundled"],
    ),
  ]
}

/// The former per-crate kits and the flat module-feature each bare crate maps
/// to. Drives the rename-table row check, so a REMOVED bare-crate row reds.
const BARE_CRATE_MAP: &[(&str, &str)] = &[
  ("whisperkit", "whisper"),
  ("alignkit", "align"),
  ("speakerkit", "speaker"),
  ("vadkit", "vad"),
  ("clapkit", "clap"),
];

/// The curated CI feature combos the mono-crate restructure committed to — the
/// EXACT intended set of `jobs.features.strategy.matrix.features` entries in
/// `.github/workflows/ci.yml`, as raw (unquoted) combo strings. The empty
/// string is a real member: the bare-core `default = []` run (ci.yml `- ""`).
/// `ci_feature_combos` parses the ACTIVE matrix and the test asserts exact set
/// equality against this, so removing OR commenting out any entry (the bare-core
/// `""` included) drops it from the parsed set and reds.
const INTENDED_CI_COMBOS: &[&str] = &[
  "", // bare core / none (`default = []`)
  "whisper",
  "align",
  "speaker",
  "speaker,serde",
  "vad",
  "whisper,vad",
  "align-oracle",
  "clap",
  "granite",
  "siglip",
  "ced",
  "lid",
  "identity",
  "identity,speaker",
  "face",
  // NOT folded into the two all-on rows below, and that is the point of a
  // `commercial-` gate: an opt-in that the crate's own "everything on"
  // configuration turns on is not an opt-in. Its own row is what builds the
  // four gate suites and runs their hermetic halves.
  "commercial-face-arcface",
  "whisper,align,speaker,vad,clap,granite,siglip,ced,lid,identity,face,serde,tracing,nl-recognizer",
  "whisper,align-oracle,speaker,vad,clap,granite,siglip,ced,lid,identity,face,serde,tracing,nl-recognizer",
];

/// The curated combos of ci.yml's `parity` job — one row per third-party oracle
/// plus an all-on row, run against `coremlit-parity`. Same exact-set discipline
/// as [`INTENDED_CI_COMBOS`]: deleting or commenting out a row reds, so an
/// oracle cannot quietly stop being built once it no longer rides this crate's
/// own feature matrix.
const INTENDED_PARITY_CI_COMBOS: &[&str] = &[
  "speaker-oracle",
  "clap-oracle",
  "vad-bundled",
  "speaker-oracle,clap-oracle,vad-bundled",
];

/// The text of the `[features]` table (its lines, blank/comment lines included).
fn features_block(manifest: &str) -> String {
  let mut out = String::new();
  let mut in_features = false;
  for line in manifest.lines() {
    if line.starts_with('[') {
      in_features = line.trim() == "[features]";
      continue;
    }
    if in_features {
      out.push_str(line);
      out.push('\n');
    }
  }
  out
}

/// The feature *names* declared in the `[features]` block. A feature key sits at
/// column 0; an array's continuation lines are indented (and so skipped).
fn feature_names(block: &str) -> BTreeSet<String> {
  let mut names = BTreeSet::new();
  for line in block.lines() {
    if line.starts_with(char::is_whitespace) {
      continue;
    }
    let trimmed = line.trim_start();
    if trimmed.is_empty() || trimmed.starts_with('#') {
      continue;
    }
    if let Some((key, _)) = line.split_once('=') {
      let key = key.trim();
      if !key.is_empty() && !key.contains(char::is_whitespace) {
        names.insert(key.to_string());
      }
    }
  }
  names
}

/// The dependency set of one feature — the quoted entries of its `[..]` value,
/// robust to a value spread over multiple (indented) lines.
fn feature_deps(block: &str, feature: &str) -> BTreeSet<String> {
  let mut collecting = false;
  let mut buf = String::new();
  for line in block.lines() {
    if collecting {
      buf.push('\n');
      buf.push_str(line);
      if line.contains(']') {
        break;
      }
      continue;
    }
    if line.starts_with(char::is_whitespace) {
      continue;
    }
    let Some((key, rest)) = line.split_once('=') else {
      continue;
    };
    if key.trim() != feature {
      continue;
    }
    collecting = true;
    buf.push_str(rest);
    if rest.contains(']') {
      break;
    }
  }
  // Quoted contents are the odd-indexed pieces of a split on '"'.
  buf
    .split('"')
    .skip(1)
    .step_by(2)
    .map(str::to_string)
    .collect()
}

/// The column `jobs.<name>:` keys sit at in `.github/workflows/ci.yml`.
const JOB_INDENT: usize = 2;

/// Parse the ACTIVE `jobs.<job>.strategy.matrix.features` list from a ci.yml
/// text into the set of `--features` combo strings that job runs.
///
/// Structural, not substring, and SCOPED TO ONE JOB: it enters at the
/// `  <job>:` key, leaves at the next key in that column (so a later job's
/// matrix cannot be folded into this one's set — the split into `features` +
/// `parity` made that a live confusion), and within the job takes the
/// `features:` key that FOLLOWS `matrix:` (so the `features` JOB name and the
/// `cargo build --features` step cannot be mistaken for it). It collects each
/// `- "..."` item's inner value (the empty `- ""` is the empty-string member),
/// SKIPS any line whose first non-space char is `#` (a commented-out
/// `# - "..."` entry does NOT count as present, and does not end the list), and
/// stops at the first dedent to the key's column or left of it.
fn ci_feature_combos(yaml: &str, job: &str) -> BTreeSet<String> {
  let job_key = format!("{job}:");
  let mut combos = BTreeSet::new();
  let mut in_job = false;
  let mut seen_matrix = false;
  let mut key_indent: Option<usize> = None;
  for line in yaml.lines() {
    let indent = line.len() - line.trim_start().len();
    let trimmed = line.trim_start();
    // Comments and blanks are transparent everywhere: a commented-out entry is
    // skipped (not counted) WITHOUT terminating the list or the job.
    if trimmed.is_empty() || trimmed.starts_with('#') {
      continue;
    }
    if !in_job {
      in_job = indent == JOB_INDENT && trimmed == job_key;
      continue;
    }
    // A dedent to the job's own column (or left of it) ends the job.
    if indent <= JOB_INDENT {
      break;
    }
    let Some(ki) = key_indent else {
      if trimmed == "matrix:" {
        seen_matrix = true;
      } else if seen_matrix && trimmed == "features:" {
        key_indent = Some(indent);
      }
      continue;
    };
    // A dedent to the key's column (or left of it) ends the list.
    if indent <= ki {
      break;
    }
    if let Some(inner) = trimmed.strip_prefix("- ").and_then(quoted_inner) {
      combos.insert(inner);
    }
  }
  combos
}

/// The text between the first pair of `"` in `s` (`""` → the empty string).
fn quoted_inner(s: &str) -> Option<String> {
  let start = s.find('"')?;
  let rest = &s[start + 1..];
  let end = rest.find('"')?;
  Some(rest[..end].to_string())
}

/// A pinned combo list as an owned set.
fn owned(combos: &[&str]) -> BTreeSet<String> {
  combos.iter().map(|s| (*s).to_string()).collect()
}

/// The intended `features`-job combos as an owned set.
fn intended_ci_combos() -> BTreeSet<String> {
  owned(INTENDED_CI_COMBOS)
}

/// What the clippy step of one matrix job must run. The `features` job lints
/// `coremlit` under each curated combo, and its bare-core row (`""`) takes the
/// arm with no `--features`; the `parity` job lints `coremlit-parity` under each
/// oracle feature, and no row of that matrix is empty.
struct IntendedClippy {
  job: &'static str,
  package: &'static str,
  empty_arm: bool,
}

const FEATURES_CLIPPY: IntendedClippy = IntendedClippy {
  job: "features",
  package: "coremlit",
  empty_arm: true,
};

const PARITY_CLIPPY: IntendedClippy = IntendedClippy {
  job: "parity",
  package: "coremlit-parity",
  empty_arm: false,
};

/// The `if:` a clippy step carries. A step's default condition is `success()`,
/// so without it a red test step above hides the lint verdict.
const CLIPPY_STEP_CONDITION: &str = "${{ !cancelled() }}";

/// The argument that aims a clippy command at the matrix row's features.
const MATRIX_FEATURES_ARG: &str = r#"--features "${{ matrix.features }}""#;

/// One `key: value` entry of a step, with the lines under it: the inline value
/// first, then any deeper lines (a `run: |` script, a `with:` map).
type CiEntry = (String, Vec<String>);

/// `line` up to a trailing ` #` comment, which both YAML and the shell the `run`
/// scripts feed read as one.
fn without_comment(line: &str) -> &str {
  line
    .split_once(" #")
    .map_or(line, |(code, _)| code)
    .trim_end()
}

/// The lines of one ci.yml job as `(indent, text)`, scoped the way
/// [`ci_feature_combos`] scopes it — from its `  <job>:` key to the next line at
/// that column or left of it — with blank and comment lines dropped (a
/// commented-out step is not a step) and trailing comments cut.
fn ci_job_lines<'a>(yaml: &'a str, job: &str) -> Vec<(usize, &'a str)> {
  let job_key = format!("{job}:");
  let mut lines = Vec::new();
  let mut in_job = false;
  for line in yaml.lines() {
    let trimmed = line.trim_start();
    if trimmed.is_empty() || trimmed.starts_with('#') {
      continue;
    }
    let indent = line.len() - trimmed.len();
    let text = without_comment(trimmed);
    if !in_job {
      in_job = indent == JOB_INDENT && text == job_key;
      continue;
    }
    if indent <= JOB_INDENT {
      break;
    }
    lines.push((indent, text));
  }
  lines
}

/// `key: value` as an entry whose body starts with the inline value. A
/// block-scalar indicator (`|`, `>`) is not a value: the lines below it are.
fn ci_entry(text: &str) -> CiEntry {
  let (key, value) = text.split_once(':').unwrap_or((text, ""));
  let value = value.trim();
  let body = if value.is_empty() || value.starts_with(['|', '>']) {
    Vec::new()
  } else {
    vec![value.to_string()]
  };
  (key.trim().to_string(), body)
}

/// The steps of a job's `steps:` list, each as its entries. Empty when the key is
/// absent or the list does not open with a `- ` item, so a reshaped job reds the
/// checks below instead of being passed over.
fn ci_steps(job: &[(usize, &str)]) -> Vec<Vec<CiEntry>> {
  let Some(at) = job.iter().position(|&(_, text)| text == "steps:") else {
    return Vec::new();
  };
  let steps_indent = job[at].0;
  let mut steps: Vec<Vec<CiEntry>> = Vec::new();
  let mut first_indent = None;
  for &(indent, text) in &job[at + 1..] {
    if indent <= steps_indent {
      break;
    }
    let item_indent = *first_indent.get_or_insert(indent);
    // A `- ` item opens a step, and its first entry sits two columns in, where
    // every later entry of the step sits too.
    let opens_step = indent == item_indent && text.starts_with("- ");
    if opens_step {
      steps.push(Vec::new());
    }
    let Some(step) = steps.last_mut() else {
      return Vec::new();
    };
    if opens_step || indent == item_indent + 2 {
      step.push(ci_entry(if opens_step { &text[2..] } else { text }));
    } else if let Some((_, body)) = step.last_mut() {
      body.push(text.to_string());
    }
  }
  steps
}

/// The lines under `key:` in a step, empty when the step has no such entry.
fn ci_body<'a>(step: &'a [CiEntry], key: &str) -> &'a [String] {
  step
    .iter()
    .find(|(name, _)| name == key)
    .map_or(&[][..], |(_, body)| body.as_slice())
}

/// The shell commands of a step's `run` script, with a line that ends in a
/// backslash joined to the one after it, so a re-wrapped command reads as one.
fn ci_commands(step: &[CiEntry]) -> Vec<String> {
  let mut commands: Vec<String> = Vec::new();
  let mut joining = false;
  for line in ci_body(step, "run") {
    let (text, continues) = line
      .strip_suffix('\\')
      .map_or((line.as_str(), false), |head| (head.trim_end(), true));
    match commands.last_mut() {
      Some(last) if joining => {
        last.push(' ');
        last.push_str(text);
      }
      _ => commands.push(text.to_string()),
    }
    joining = continues;
  }
  commands
}

/// Whether a shell command invokes `cargo <subcommand>`.
fn runs_cargo(command: &str, subcommand: &str) -> bool {
  let tokens: Vec<&str> = command.split_whitespace().collect();
  tokens.windows(2).any(|pair| pair == ["cargo", subcommand])
}

/// Whether a shell command invokes `cargo clippy`.
fn runs_clippy(command: &str) -> bool {
  runs_cargo(command, "clippy")
}

/// Whether one of `steps` asks the toolchain install for `component`: a
/// `components:` entry under its `with:`, which is a comma-separated list.
fn installs_component(steps: &[Vec<CiEntry>], component: &str) -> bool {
  steps.iter().any(|step| {
    ci_body(step, "with").iter().any(|line| {
      line.strip_prefix("components:").is_some_and(|list| {
        list
          .split(',')
          .any(|installed| installed.trim().trim_matches(['"', '\'']) == component)
      })
    })
  })
}

/// What, if anything, has drifted in one job's clippy step, named. In order:
/// exactly one step runs `cargo clippy`; it carries `if: ${{ !cancelled() }}`; a
/// step ahead of it installs `components: clippy`; it has a command with
/// `--features "${{ matrix.features }}"` and, when the matrix has an empty row,
/// one without `--features`; and EVERY clippy command in it carries
/// `cargo clippy -p <package>`, `--no-deps` and `--all-targets` ahead of the `--`
/// and `-D warnings` after it.
///
/// Only the step's `run` lines are read. Its `name:` spells `--all-targets` as
/// well, so a read of the whole step would keep a dropped flag looking present.
fn clippy_step_drift(yaml: &str, want: &IntendedClippy) -> Result<(), String> {
  let job = want.job;
  let steps = ci_steps(&ci_job_lines(yaml, job));
  let clippy: Vec<usize> = steps
    .iter()
    .enumerate()
    .filter(|(_, step)| ci_commands(step).iter().any(|command| runs_clippy(command)))
    .map(|(at, _)| at)
    .collect();
  let [at] = clippy[..] else {
    return Err(format!(
      "ci.yml `{job}` job: expected exactly one step running `cargo clippy`, found {} among {} \
       parsed step(s) — the clippy step was dropped, duplicated or commented out, or the job's \
       `steps:` list changed shape",
      clippy.len(),
      steps.len()
    ));
  };
  let step = &steps[at];

  let condition = ci_body(step, "if");
  if condition.first().map(String::as_str) != Some(CLIPPY_STEP_CONDITION) {
    return Err(format!(
      "ci.yml `{job}` job: the clippy step must carry `if: {CLIPPY_STEP_CONDITION}` so a red \
       test step above cannot hide its verdict, found {condition:?}"
    ));
  }

  if !installs_component(&steps[..at], "clippy") {
    return Err(format!(
      "ci.yml `{job}` job: no step ahead of the clippy step installs `components: clippy`"
    ));
  }

  let commands: Vec<String> = ci_commands(step)
    .into_iter()
    .filter(|command| runs_clippy(command))
    .collect();
  let on_matrix = |command: &str| command.contains(MATRIX_FEATURES_ARG);
  if !commands.iter().any(|command| on_matrix(command)) {
    return Err(format!(
      "ci.yml `{job}` job: no clippy command in the step carries `{MATRIX_FEATURES_ARG}`, so \
       the matrix row's features are never linted"
    ));
  }
  if want.empty_arm && commands.iter().all(|command| on_matrix(command)) {
    return Err(format!(
      "ci.yml `{job}` job: no clippy command in the step runs without `--features`, so the \
       bare-core row (`\"\"`) has nothing to lint it"
    ));
  }
  for command in &commands {
    let arm = if on_matrix(command) {
      "the `--features` arm"
    } else {
      "the empty-features arm"
    };
    let tokens: Vec<&str> = command.split_whitespace().collect();
    let separator = tokens.iter().position(|token| *token == "--");
    let (cargo_args, lint_args) = tokens.split_at(separator.unwrap_or(tokens.len()));
    let package_args = format!("cargo clippy -p {}", want.package);
    let needs = [
      (
        package_args.as_str(),
        cargo_args
          .windows(4)
          .any(|run| run == ["cargo", "clippy", "-p", want.package]),
      ),
      ("--no-deps", cargo_args.contains(&"--no-deps")),
      ("--all-targets", cargo_args.contains(&"--all-targets")),
      (
        "-- -D warnings",
        lint_args.windows(2).any(|pair| pair == ["-D", "warnings"]),
      ),
    ];
    if let Some((need, _)) = needs.iter().find(|(_, present)| !present) {
      return Err(format!(
        "ci.yml `{job}` job: {arm} of the clippy step must carry `{need}`, found: {command}"
      ));
    }
  }
  Ok(())
}

/// Panic with [`clippy_step_drift`]'s message, if it has one.
fn assert_clippy_step(yaml: &str, want: &IntendedClippy) {
  if let Err(drift) = clippy_step_drift(yaml, want) {
    panic!("{drift}");
  }
}

/// The `check` job: the one pass that lints and documents the WHOLE workspace
/// with every feature on, `coremlit-parity`'s oracle features included. No
/// matrix row stands in for it — each `features` and `parity` row lints one
/// package under one feature set — so its three steps are pinned on their own.
const CHECK_JOB: &str = "check";

/// The position, among the `check` job's parsed `steps`, of the one that runs
/// `cargo <subcommand>`, or what is wrong with how many there are.
fn check_job_step_at(steps: &[Vec<CiEntry>], subcommand: &str) -> Result<usize, String> {
  let found: Vec<usize> = steps
    .iter()
    .enumerate()
    .filter(|(_, step)| {
      ci_commands(step)
        .iter()
        .any(|command| runs_cargo(command, subcommand))
    })
    .map(|(at, _)| at)
    .collect();
  let [at] = found[..] else {
    return Err(format!(
      "ci.yml `{CHECK_JOB}` job: expected exactly one step running `cargo {subcommand}`, found {} \
       among {} parsed step(s) — the step was dropped, duplicated or commented out, or the job's \
       `steps:` list changed shape",
      found.len(),
      steps.len()
    ));
  };
  Ok(at)
}

/// The `check` job's one step that runs `cargo <subcommand>`, or what is wrong
/// with how many there are.
fn check_job_step(yaml: &str, subcommand: &str) -> Result<Vec<CiEntry>, String> {
  let steps = ci_steps(&ci_job_lines(yaml, CHECK_JOB));
  let at = check_job_step_at(&steps, subcommand)?;
  Ok(steps.into_iter().nth(at).expect("`at` indexes `steps`"))
}

/// Whether a `cargo` argument aims the command at one package.
fn names_a_package(arg: &str) -> bool {
  matches!(arg, "-p" | "--package") || arg.starts_with("--package=")
}

/// What, if anything, has drifted in the `check` job's fmt step, named. In order:
/// exactly one step runs `cargo fmt`, and EVERY fmt command in it carries `--all`
/// ahead of any `--` and `--check` on either side of it.
///
/// `--check` is what makes the step a gate: without it `cargo fmt` rewrites the
/// files on the runner and exits zero, so the job passes over every unformatted
/// file. It is also a `rustfmt` flag, so `cargo fmt --all -- --check` reads the
/// same as `cargo fmt --all --check`. `--all` is held as the job's contract,
/// every workspace member by name, and not for a measured difference: at this
/// virtual workspace root the bare `cargo fmt` reaches the same files today. It
/// is a `cargo fmt` flag only, so after the `--` it is not the flag.
///
/// Only the step's `run` lines are read, as for the other `check` steps.
fn check_fmt_drift(yaml: &str) -> Result<(), String> {
  let step = check_job_step(yaml, "fmt")?;
  for command in ci_commands(&step)
    .into_iter()
    .filter(|command| runs_cargo(command, "fmt"))
  {
    let tokens: Vec<&str> = command.split_whitespace().collect();
    let separator = tokens.iter().position(|token| *token == "--");
    let (cargo_args, _) = tokens.split_at(separator.unwrap_or(tokens.len()));
    let needs = [
      ("--all", cargo_args.contains(&"--all")),
      ("--check", tokens.contains(&"--check")),
    ];
    if let Some((need, _)) = needs.iter().find(|(_, present)| !present) {
      return Err(format!(
        "ci.yml `{CHECK_JOB}` job: the fmt step must carry `{need}`, found: {command}"
      ));
    }
  }
  Ok(())
}

/// What, if anything, has drifted in the `check` job's clippy step, named. In
/// order: exactly one step runs `cargo clippy`, and EVERY clippy command in it
/// names no package and carries `--all-targets` and `--all-features` ahead of the
/// `--` and `-D warnings` after it.
///
/// No package, because this is the one lint pass over the whole workspace:
/// `--all-features` reaches the oracle features only through `coremlit-parity`,
/// which a `-p coremlit` run leaves out. `--all-targets` with `--all-features` is
/// also what compiles the `harness = false` benches, which neither `cargo test`
/// nor `clippy --tests` reaches and each of which declares `required-features`.
///
/// Only the step's `run` lines are read, as for the other jobs' clippy steps.
fn check_clippy_drift(yaml: &str) -> Result<(), String> {
  let step = check_job_step(yaml, "clippy")?;
  for command in ci_commands(&step)
    .into_iter()
    .filter(|command| runs_clippy(command))
  {
    let tokens: Vec<&str> = command.split_whitespace().collect();
    let separator = tokens.iter().position(|token| *token == "--");
    let (cargo_args, lint_args) = tokens.split_at(separator.unwrap_or(tokens.len()));
    if cargo_args.iter().any(|arg| names_a_package(arg)) {
      return Err(format!(
        "ci.yml `{CHECK_JOB}` job: the clippy step must lint the whole workspace, but it names a \
         package, so `--all-features` no longer reaches `coremlit-parity`'s oracle features, \
         found: {command}"
      ));
    }
    let needs = [
      ("--all-targets", cargo_args.contains(&"--all-targets")),
      ("--all-features", cargo_args.contains(&"--all-features")),
      (
        "-- -D warnings",
        lint_args.windows(2).any(|pair| pair == ["-D", "warnings"]),
      ),
    ];
    if let Some((need, _)) = needs.iter().find(|(_, present)| !present) {
      return Err(format!(
        "ci.yml `{CHECK_JOB}` job: the clippy step must carry `{need}`, found: {command}"
      ));
    }
  }
  Ok(())
}

/// Whether a rustc or rustdoc flag string denies warnings.
fn denies_warnings(flags: &str) -> bool {
  let tokens: Vec<&str> = flags
    .trim()
    .trim_matches(['"', '\''])
    .split_whitespace()
    .collect();
  tokens.contains(&"-Dwarnings") || tokens.windows(2).any(|pair| pair == ["-D", "warnings"])
}

/// What, if anything, has drifted in the `check` job's doc step, named. In order:
/// exactly one step runs `cargo doc`, EVERY doc command in it carries `--no-deps`
/// and `--all-features`, and its `env:` sets `RUSTDOCFLAGS` to deny warnings.
///
/// `RUSTDOCFLAGS` is what makes the step a gate: rustdoc reports a broken
/// intra-doc link as a warning, so without `-D warnings` the step passes over
/// every one of them. `--all-features` is what documents the feature-gated
/// modules at all.
fn check_doc_drift(yaml: &str) -> Result<(), String> {
  let step = check_job_step(yaml, "doc")?;
  for command in ci_commands(&step)
    .into_iter()
    .filter(|command| runs_cargo(command, "doc"))
  {
    let tokens: Vec<&str> = command.split_whitespace().collect();
    for need in ["--no-deps", "--all-features"] {
      if !tokens.contains(&need) {
        return Err(format!(
          "ci.yml `{CHECK_JOB}` job: the doc step must carry `{need}`, found: {command}"
        ));
      }
    }
  }
  let flags = ci_body(&step, "env")
    .iter()
    .find_map(|line| line.strip_prefix("RUSTDOCFLAGS:"));
  if !flags.is_some_and(denies_warnings) {
    return Err(format!(
      "ci.yml `{CHECK_JOB}` job: the doc step's `env:` must set `RUSTDOCFLAGS: -D warnings`, so \
       a broken intra-doc link fails the job instead of passing it, found: {flags:?}"
    ));
  }
  Ok(())
}

/// What, if anything, has drifted in the toolchain the `check` job installs,
/// named. In order: exactly one step runs `cargo fmt` and a step ahead of it
/// installs `components: rustfmt`; exactly one step runs `cargo clippy` and a
/// step ahead of it installs `components: clippy`.
///
/// The job runs both tools, and a toolchain step that does not ask for their
/// components leaves each to whatever the runner image carries, so the job can
/// pass on one image and fail on the next. The `features` and `parity` jobs'
/// clippy steps are held to the same rule by [`clippy_step_drift`].
fn check_components_drift(yaml: &str) -> Result<(), String> {
  let steps = ci_steps(&ci_job_lines(yaml, CHECK_JOB));
  for (subcommand, component) in [("fmt", "rustfmt"), ("clippy", "clippy")] {
    let at = check_job_step_at(&steps, subcommand)?;
    if !installs_component(&steps[..at], component) {
      return Err(format!(
        "ci.yml `{CHECK_JOB}` job: no step ahead of the `cargo {subcommand}` step installs \
         `components: {component}`, so the job relies on the runner image for it"
      ));
    }
  }
  Ok(())
}

/// The `model-tests` job: one shard per model kit, each staging its kit's models
/// and then running independent checks over them.
const MODEL_TESTS_JOB: &str = "model-tests";

/// What every check after the staging step carries.
///
/// GitHub's default step condition is `success()`, so one red step marks every
/// step after it `skipped`, and `skipped` is silent: this job once ran for weeks
/// with gate steps that never executed. `!cancelled()` makes a check independent
/// of the ones above it; `steps.download.outcome != 'failure'` keeps the one real
/// dependency, since nothing can run without the staged artifacts.
const GATE_CONDITION: &str = "${{ !cancelled() && steps.download.outcome != 'failure' }}";

/// What the closing `Gate ledger` carries: `!cancelled()` alone, so it reports
/// even when the download died and took every check with it.
const LEDGER_CONDITION: &str = "${{ !cancelled() }}";

/// The checks every `model-tests` shard runs between its staging step and the
/// closing ledger, each by the start of its `name:`.
///
/// Every shard runs the same list, which is what lets one ledger and one set of
/// pins cover them all. A check taken out of it, or renamed, leaves a shard that
/// gates less while every guard above it still holds.
const MODEL_TESTS_CHECKS: [&str; 5] = [
  "Verify staged overlay ordering",
  "Verify staged artifact checksums",
  "fp16 graph sweep",
  "fp16 sweep inventory",
  "Model gates",
];

/// A step as a drift message names it: its `name:`, else its `uses:`, else the
/// first line of its `run`.
fn step_label(step: &[CiEntry]) -> String {
  ["name", "uses", "run"]
    .iter()
    .find_map(|key| ci_body(step, key).first())
    .cloned()
    .unwrap_or_else(|| "(an unnamed step)".to_string())
}

/// What, if anything, has drifted in the guards of the `model-tests` job, named.
/// In order: a step has `id: download`, the staging step every guard reads the
/// outcome of; the last step is the `Gate ledger`, carrying `if: ${{ !cancelled()
/// }}` alone; some step between the two runs `cargo test`; EVERY step between
/// them carries `if: ${{ !cancelled() && steps.download.outcome != 'failure' }}`;
/// each of them has an `id:` that the ledger reads as `=${{ steps.<id>.outcome
/// }}`; and the checks in [`MODEL_TESTS_CHECKS`] are among them.
///
/// Every step after staging is covered, not only those whose script says `cargo
/// test`: the checks are independent of each other, and a step that lost its
/// condition is skipped by a failure above it whatever it runs. The `cargo test`
/// clause is the vacuum guard — a job reshaped until no kit test is left in it
/// would satisfy every other clause.
///
/// The ledger is what reports a check that never ran, so a check whose id it does
/// not read is one nothing watches: ci.yml's own note calls forgetting the entry
/// the benign direction, and this reds it at authoring time. Only the ledger
/// step's own lines are read, so an entry that is commented out does not count.
fn model_tests_guard_drift(yaml: &str) -> Result<(), String> {
  let job = MODEL_TESTS_JOB;
  let steps = ci_steps(&ci_job_lines(yaml, job));
  let Some(staged) = steps
    .iter()
    .position(|step| ci_body(step, "id").first().map(String::as_str) == Some("download"))
  else {
    return Err(format!(
      "ci.yml `{job}` job: no step has `id: download` among {} parsed step(s) — every guard \
       reads the staging outcome through that id, and without it nothing short-circuits; the \
       staging step was renamed, dropped or commented out, or the job's `steps:` list changed \
       shape",
      steps.len()
    ));
  };
  let Some((ledger, checks)) = steps[staged + 1..].split_last() else {
    return Err(format!(
      "ci.yml `{job}` job: no step follows the staging step, so there is no `Gate ledger` and \
       no check to guard"
    ));
  };
  let is_the_ledger = ci_body(ledger, "name")
    .first()
    .is_some_and(|name| name.starts_with("Gate ledger"));
  if !is_the_ledger {
    return Err(format!(
      "ci.yml `{job}` job: the last step must be the `Gate ledger`, which is what reports a \
       check that never ran, found `{}`",
      step_label(ledger)
    ));
  }
  let condition = ci_body(ledger, "if");
  if condition.first().map(String::as_str) != Some(LEDGER_CONDITION) {
    return Err(format!(
      "ci.yml `{job}` job: the `Gate ledger` step must carry `if: {LEDGER_CONDITION}` alone, so \
       it reports even when the download died and took every check with it, found {condition:?}"
    ));
  }
  let runs_a_kit_test = checks.iter().any(|step| {
    ci_commands(step)
      .iter()
      .any(|command| runs_cargo(command, "test"))
  });
  if !runs_a_kit_test {
    return Err(format!(
      "ci.yml `{job}` job: no step between the staging step and the `Gate ledger` runs `cargo \
       test`, so the guards pin nothing about the kit's tests — they were dropped or commented \
       out, or the job's `steps:` list changed shape"
    ));
  }
  for step in checks {
    let condition = ci_body(step, "if");
    if condition.first().map(String::as_str) != Some(GATE_CONDITION) {
      return Err(format!(
        "ci.yml `{job}` job: the step `{}` must carry `if: {GATE_CONDITION}` — GitHub's default \
         condition is `success()`, so without it one red step above marks this check `skipped`, \
         and `skipped` is silent — found {condition:?}",
        step_label(step)
      ));
    }
  }
  let ledger_lines: Vec<&String> = ledger.iter().flat_map(|(_, body)| body).collect();
  for step in checks {
    let Some(id) = ci_body(step, "id").first() else {
      return Err(format!(
        "ci.yml `{job}` job: the step `{}` has no `id:`, so the `Gate ledger` step cannot report \
         whether it ran",
        step_label(step)
      ));
    };
    let entry = format!("=${{{{ steps.{id}.outcome }}}}");
    if !ledger_lines.iter().any(|line| line.contains(&entry)) {
      return Err(format!(
        "ci.yml `{job}` job: the `Gate ledger` step never reads step `{id}` (`{entry}`), so the \
         check `{}` could be skipped without the shard saying so",
        step_label(step)
      ));
    }
  }
  for name in MODEL_TESTS_CHECKS {
    let present = checks.iter().any(|step| {
      ci_body(step, "name")
        .first()
        .is_some_and(|label| label.starts_with(name))
    });
    if !present {
      return Err(format!(
        "ci.yml `{job}` job: no step between the staging step and the `Gate ledger` is named \
         `{name}`, which every shard runs — it was removed, renamed or commented out, or the \
         job's `steps:` list changed shape"
      ));
    }
  }
  Ok(())
}

/// Panic with a pin's drift message, if it has one, at the calling pin's line.
#[track_caller]
fn assert_no_drift(drift: Result<(), String>) {
  if let Err(drift) = drift {
    panic!("{drift}");
  }
}

/// Parse ONLY the "## Rename table" section of `FEATURE_MAP.md` into rows of
/// trimmed cells. Scoped to that section, so the separate curated-CI-combo table
/// lower in the doc cannot satisfy a rename-row assertion, and a bare token
/// elsewhere in the prose cannot stand in for a removed row.
fn rename_table_rows(doc: &str) -> Vec<Vec<String>> {
  let mut rows = Vec::new();
  let mut in_table = false;
  for line in doc.lines() {
    if let Some(heading) = line.strip_prefix("## ") {
      in_table = heading.contains("Rename table");
      continue;
    }
    if !in_table {
      continue;
    }
    let line = line.trim();
    if !line.starts_with('|') {
      continue;
    }
    let cells: Vec<String> = line
      .trim_matches('|')
      .split('|')
      .map(|c| c.trim().to_string())
      .collect();
    // Skip the header row and the `|---|---|` separator row.
    if cells.iter().any(|c| c == "Old crate") {
      continue;
    }
    if cells
      .iter()
      .all(|c| !c.is_empty() && c.chars().all(|ch| ch == '-'))
    {
      continue;
    }
    rows.push(cells);
  }
  rows
}

fn unbacktick(cell: &str) -> &str {
  cell.trim_matches('`')
}

/// `Cargo.toml` `[features]` names match the pinned flat set exactly — no
/// renamed, added, or dropped feature.
#[test]
fn feature_names_match_the_pinned_set() {
  let actual = feature_names(&features_block(&manifest()));
  let expected: BTreeSet<String> = expected_features()
    .iter()
    .map(|(name, _)| (*name).to_string())
    .collect();
  assert_eq!(
    actual, expected,
    "Cargo.toml [features] names drifted from the pinned flat feature set (FEATURE_MAP.md)"
  );
}

/// The oracle features are GONE from this crate — they belong to
/// `coremlit-parity` now. A re-added `speaker-oracle` here would drag the
/// unpublished `dia` git source back into the publishable manifest, which is the
/// exact thing this split exists to prevent, so it is pinned as an absence.
#[test]
fn oracle_features_are_not_this_crates() {
  let names = feature_names(&features_block(&manifest()));
  for moved in ["speaker-oracle", "clap-oracle", "vad-bundled"] {
    assert!(
      !names.contains(moved),
      "`{moved}` is declared on coremlit again — it belongs to coremlit-parity, whose \
       oracle deps (`dia`, `textclap`) are unpublished git sources cargo publish rejects"
    );
  }
  assert!(
    names.contains("align-oracle"),
    "`align-oracle` must STAY on coremlit: it only enables a feature of `asry`, a dependency \
     this crate has either way, so moving it would relocate code without removing a git dep"
  );
}

/// `coremlit-parity`'s `[features]` names match its pinned set exactly.
#[test]
fn parity_feature_names_match_the_pinned_set() {
  let Some(parity) = parity_manifest() else {
    return;
  };
  let actual = feature_names(&features_block(&parity));
  let expected: BTreeSet<String> = expected_parity_features()
    .iter()
    .map(|(name, _)| (*name).to_string())
    .collect();
  assert_eq!(
    actual, expected,
    "coremlit-parity [features] names drifted from the pinned oracle feature set"
  );
}

/// Each `coremlit-parity` feature's dependency set matches its pinned set —
/// same exact-set discipline, so an oracle that quietly stops enabling its
/// `coremlit` module feature (or starts enabling another kit's) reds.
#[test]
fn parity_feature_deps_are_pinned_with_no_cross_oracle_leakage() {
  let Some(parity) = parity_manifest() else {
    return;
  };
  let block = features_block(&parity);
  for (name, deps) in expected_parity_features() {
    let actual = feature_deps(&block, name);
    let expected: BTreeSet<String> = deps.iter().map(|d| (*d).to_string()).collect();
    assert_eq!(
      actual, expected,
      "coremlit-parity feature `{name}` dependency set drifted"
    );
  }
}

/// `coremlit-parity` must never be publishable: it exists to hold the two
/// unpublished git oracles, so `publish = false` is load-bearing, not tidiness.
#[test]
fn parity_crate_is_never_published() {
  let Some(parity) = parity_manifest() else {
    return;
  };
  assert!(
    parity.lines().any(|l| l.trim() == "publish = false"),
    "coremlit-parity must declare `publish = false` — it depends on unpublished git sources"
  );
}

/// Every former per-crate feature name is gone (renamed away by the table).
#[test]
fn old_per_crate_feature_names_are_gone() {
  let names = feature_names(&features_block(&manifest()));
  for old in ["dia", "dia-oracle", "parity-oracle", "vadkit", "bundled"] {
    assert!(
      !names.contains(old),
      "old per-crate feature `{old}` is still declared — the rename table maps it away"
    );
  }
}

/// Each feature's dependency set matches its pinned set exactly. Exact-set
/// equality catches cross-kit LEAKAGE (e.g. adding `"vad"` to `whisper` adds an
/// entry the pinned `whisper` set does not have) as well as a dropped
/// composition edge (e.g. `nl-recognizer` losing `whisper`).
#[test]
fn feature_deps_are_pinned_with_no_cross_kit_leakage() {
  let block = features_block(&manifest());
  for (name, deps) in expected_features() {
    let actual = feature_deps(&block, name);
    let expected: BTreeSet<String> = deps.iter().map(|d| (*d).to_string()).collect();
    assert_eq!(
      actual, expected,
      "feature `{name}` dependency set drifted (cross-kit leakage or a dropped/added dep)"
    );
  }
}

/// The `FEATURE_MAP.md` rename table maps every former kit's BARE crate to its
/// module feature. Parsed structurally (crate cell + `(crate)` cell + feature
/// cell), so removing or altering a bare-crate row reds even when the feature
/// token still appears elsewhere in the doc (the flat-feature list, a `dia`-style
/// feature row, or the prose).
#[test]
fn rename_table_pins_every_bare_crate_row() {
  let rows = rename_table_rows(&read_rel("FEATURE_MAP.md"));
  assert!(
    rows.len() >= BARE_CRATE_MAP.len(),
    "rename-table parse found only {} row(s) — the parser or the table shape broke",
    rows.len()
  );
  for (kit, feature) in BARE_CRATE_MAP {
    let found = rows
      .iter()
      .any(|r| r.len() >= 3 && r[0] == *kit && r[1] == "(crate)" && unbacktick(&r[2]) == *feature);
    assert!(
      found,
      "FEATURE_MAP.md rename table must map bare crate `{kit}` | (crate) | `{feature}` \
       (a removed or altered bare-crate row)"
    );
  }
}

/// Assert two combo sets are equal, naming the symmetric difference so a drift
/// reports exactly what moved (a `""` member prints as an empty string).
fn assert_combo_sets_eq(actual: &BTreeSet<String>, expected: &BTreeSet<String>, what: &str) {
  let missing: Vec<&String> = expected.difference(actual).collect();
  let unexpected: Vec<&String> = actual.difference(expected).collect();
  assert!(
    missing.is_empty() && unexpected.is_empty(),
    "{what} drifted from the pinned curated set — missing (pinned but not in the \
     active matrix): {missing:?}; unexpected (in the active matrix but not pinned): {unexpected:?}"
  );
}

/// `ci.yml`'s ACTIVE feature matrix is EXACTLY the curated combo set. The parsed
/// `matrix.features` set is compared for exact equality with `INTENDED_CI_COMBOS`
/// (not substring containment), so removing a combo, commenting one out, or
/// adding an unexpected one all red — the bare-core `""` included.
#[test]
fn ci_pins_the_curated_feature_combos() {
  let Some(ci) = ci_yml() else {
    return;
  };
  assert_combo_sets_eq(
    &ci_feature_combos(&ci, "features"),
    &intended_ci_combos(),
    "ci.yml `features` job matrix",
  );
}

/// The same exact-set pin for ci.yml's `parity` job — the only place the three
/// third-party oracles are still built in CI now that they are not on this
/// crate's feature matrix. Dropping a row here would silently stop building an
/// oracle; the job scoping is what keeps this set distinct from the one above.
#[test]
fn ci_pins_the_curated_parity_combos() {
  let Some(ci) = ci_yml() else {
    return;
  };
  assert_combo_sets_eq(
    &ci_feature_combos(&ci, "parity"),
    &owned(INTENDED_PARITY_CI_COMBOS),
    "ci.yml `parity` job matrix",
  );
}

/// The pins above say which combos run; this one says what each row's lint step
/// runs. `ci.yml`'s `features` job must keep the clippy step that lints every
/// combo as itself over every target (#158): `cargo clippy -p coremlit --no-deps
/// --all-targets [--features <combo>] -- -D warnings` on both of its arms,
/// guarded by `if: ${{ !cancelled() }}`, with the `clippy` component installed
/// ahead of it. A dropped step, `--all-targets` or `-D warnings` leaves every row
/// green while it lints less, so each reds here, naming the job and what dropped.
#[test]
fn ci_pins_the_features_job_clippy_step() {
  let Some(ci) = ci_yml() else {
    return;
  };
  assert_clippy_step(&ci, &FEATURES_CLIPPY);
}

/// The same pin for the `parity` job's step, which lints `coremlit-parity` under
/// each oracle feature. `--all-targets` is what makes it lint anything: that
/// package's library is an empty stub and all of its code is test targets.
#[test]
fn ci_pins_the_parity_job_clippy_step() {
  let Some(ci) = ci_yml() else {
    return;
  };
  assert_clippy_step(&ci, &PARITY_CLIPPY);
}

/// The `check` job's clippy step is the one lint pass over the whole workspace
/// with every feature on, and the one that compiles the `harness = false`
/// benches: `cargo clippy --all-targets --all-features -- -D warnings`, with no
/// `-p`. A dropped flag or a narrowed package leaves the job green while it
/// lints less, so each reds here, naming the job and what dropped.
#[test]
fn ci_pins_the_check_job_clippy_step() {
  let Some(ci) = ci_yml() else {
    return;
  };
  assert_no_drift(check_clippy_drift(&ci));
}

/// The `check` job's fmt step: `cargo fmt --all --check`, the one place an
/// unformatted file turns the job red. Without `--check` the step reformats the
/// runner's checkout and passes, so a dropped flag reds here, naming the job and
/// what dropped.
#[test]
fn ci_pins_the_check_job_fmt_step() {
  let Some(ci) = ci_yml() else {
    return;
  };
  assert_no_drift(check_fmt_drift(&ci));
}

/// The `check` job's doc step: `cargo doc --no-deps --all-features` under
/// `RUSTDOCFLAGS: -D warnings`, the one place a broken intra-doc link turns the
/// job red.
#[test]
fn ci_pins_the_check_job_doc_step() {
  let Some(ci) = ci_yml() else {
    return;
  };
  assert_no_drift(check_doc_drift(&ci));
}

/// The `check` job installs the components of the two tools it runs, `rustfmt`
/// for `cargo fmt` and `clippy` for `cargo clippy`, ahead of the steps that run
/// them, instead of leaving both to the runner image.
#[test]
fn ci_pins_the_check_job_components() {
  let Some(ci) = ci_yml() else {
    return;
  };
  assert_no_drift(check_components_drift(&ci));
}

/// Every `model-tests` step after staging carries [`GATE_CONDITION`] and the
/// closing `Gate ledger` carries [`LEDGER_CONDITION`] alone. Without them one
/// red step marks every check after it `skipped`, which the job's own comment
/// records as a month in which four gate steps never ran. This is the structural
/// read of the job: a condition commented out of a step, or an unguarded step
/// added after the staging step, reds here by that step's name.
///
/// It also holds the ledger to its job: every check has an `id:` the ledger
/// reads, and the checks every shard runs are all there. All of it is read from
/// the parsed steps, so a commented-out `if:` or ledger entry does not count.
#[test]
fn ci_pins_the_model_tests_step_guards() {
  let Some(ci) = ci_yml() else {
    return;
  };
  assert_no_drift(model_tests_guard_drift(&ci));
}

/// Outside the repository a pin reads nothing and says so by name.
#[test]
fn a_repo_file_is_skipped_by_name_outside_the_source_tree() {
  assert_eq!(repo_file(None, ".github/workflows/ci.yml"), None);
  assert_eq!(
    skip_notice(".github/workflows/ci.yml"),
    "feature_map: skipped — .github/workflows/ci.yml is not in this source tree (a published \
     tarball?)"
  );
}

/// With a root to address it from, a repository file is read.
#[test]
fn a_repo_file_is_read_from_the_root_it_is_given() {
  let root = Path::new(env!("CARGO_MANIFEST_DIR"));
  let text = repo_file(Some(root), "Cargo.toml").expect("a root yields the file");
  assert!(text.contains("[package]"), "read the wrong file: {text}");
}

/// A root that IS there makes a missing file a failure, not a skip: the tarball
/// is the one place these files may be absent, and a deleted workflow must red
/// its pins instead of quietly ending them.
#[test]
#[should_panic(expected = "no-such-file.yml")]
fn a_repo_file_missing_inside_the_source_tree_is_a_failure() {
  let root = Path::new(env!("CARGO_MANIFEST_DIR"));
  let _ = repo_file(Some(root), "no-such-file.yml");
}

/// A well-formed TWO-JOB snippet whose active `features:` lists are exactly the
/// intended sets — the fixture the mutation cases below perturb. Its surrounding
/// keys (a preceding job carrying a `cargo build --features` step, the `features`
/// JOB name above `matrix:`, the `steps:` dedent below, and a SECOND job with its
/// own `matrix.features`) prove the parser enters at the right `features:`, stops
/// at the dedent, and does not fold one job's rows into the other's.
const DOCTORED_MATRIX: &str = r#"
jobs:
  check:
    runs-on: macos-15
    steps:
      - run: cargo build --features whisper --examples
  features:
    runs-on: macos-15
    strategy:
      fail-fast: false
      matrix:
        features:
          - ""
          - "whisper"
          - "align"
          - "speaker"
          - "speaker,serde"
          - "vad"
          - "whisper,vad"
          - "align-oracle"
          - "clap"
          - "granite"
          - "siglip"
          - "ced"
          - "lid"
          - "identity"
          - "identity,speaker"
          - "face"
          - "commercial-face-arcface"
          - "whisper,align,speaker,vad,clap,granite,siglip,ced,lid,identity,face,serde,tracing,nl-recognizer"
          - "whisper,align-oracle,speaker,vad,clap,granite,siglip,ced,lid,identity,face,serde,tracing,nl-recognizer"
    steps:
      - uses: actions/checkout@v7
  parity:
    runs-on: macos-15
    strategy:
      fail-fast: false
      matrix:
        features:
          - "speaker-oracle"
          - "clap-oracle"
          - "vad-bundled"
          - "speaker-oracle,clap-oracle,vad-bundled"
    steps:
      - uses: actions/checkout@v7
"#;

/// The parser reads the intended set from the well-formed fixture — a guard on
/// the mutation cases below (each perturbs this same fixture).
#[test]
fn ci_combo_parser_reads_the_wellformed_matrix() {
  assert_combo_sets_eq(
    &ci_feature_combos(DOCTORED_MATRIX, "features"),
    &intended_ci_combos(),
    "well-formed doctored matrix",
  );
}

/// Job scoping is real, not incidental: the same fixture yields the PARITY set
/// for the `parity` job, and neither job's rows appear in the other's set. A
/// parser that merged every `matrix.features` in the file would fail both.
#[test]
fn ci_combo_parser_scopes_each_job_separately() {
  let features = ci_feature_combos(DOCTORED_MATRIX, "features");
  let parity = ci_feature_combos(DOCTORED_MATRIX, "parity");
  assert_combo_sets_eq(
    &parity,
    &owned(INTENDED_PARITY_CI_COMBOS),
    "well-formed doctored parity matrix",
  );
  assert!(
    features.is_disjoint(&parity),
    "the two jobs' parsed combo sets bled into each other: {features:?} vs {parity:?}"
  );
}

/// An unknown job name parses to the EMPTY set rather than silently falling
/// back to the first matrix in the file — otherwise a renamed job would keep
/// passing its pin against another job's rows.
#[test]
fn ci_combo_parser_returns_empty_for_an_absent_job() {
  assert!(
    ci_feature_combos(DOCTORED_MATRIX, "no-such-job").is_empty(),
    "an absent job must parse to no combos at all"
  );
}

/// Deleting the bare-core `- ""` entry drops it from the parsed set — the
/// set-equality check must red (the R2 gap: the bare-core run was unpinned).
#[test]
fn ci_combo_check_reds_when_bare_core_is_deleted() {
  let doctored = DOCTORED_MATRIX.replace("          - \"\"\n", "");
  assert_ne!(
    ci_feature_combos(&doctored, "features"),
    intended_ci_combos(),
    "deleting the bare-core `- \"\"` entry must make the parsed set differ from the pinned set"
  );
}

/// Commenting out the bare-core `- ""` entry (`# - ""`) must red: comment lines
/// are skipped (not counted), so the parsed set loses `""` — this is what the
/// old substring `.contains()` check silently ACCEPTED.
#[test]
fn ci_combo_check_reds_when_bare_core_is_commented_out() {
  let doctored = DOCTORED_MATRIX.replace("          - \"\"\n", "          # - \"\"\n");
  assert_ne!(
    ci_feature_combos(&doctored, "features"),
    intended_ci_combos(),
    "commenting out the bare-core `- \"\"` entry must make the parsed set differ from the pinned set"
  );
}

/// Dropping any other curated combo (`whisper,vad`) must red as well.
#[test]
fn ci_combo_check_reds_when_a_combo_is_dropped() {
  let doctored = DOCTORED_MATRIX.replace("          - \"whisper,vad\"\n", "");
  assert_ne!(
    ci_feature_combos(&doctored, "features"),
    intended_ci_combos(),
    "dropping the `whisper,vad` combo must make the parsed set differ from the pinned set"
  );
}

/// A well-formed three-job snippet carrying the clippy steps as ci.yml spells
/// them — the fixture the clippy mutation cases below perturb. Around the two
/// real steps sit what a loose reader would take for them: a `check` job whose
/// own `cargo clippy` line must not stand in for a `features` job that lost its
/// step, and step names that spell the flags the `run` lines might drop.
const DOCTORED_CLIPPY_JOBS: &str = r#"
jobs:
  check:
    runs-on: macos-15
    steps:
      - uses: actions/checkout@v7
      - run: cargo clippy --all-targets --all-features -- -D warnings
  features:
    runs-on: macos-15
    steps:
      - uses: actions/checkout@v7
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: "cargo test -p coremlit (features: ${{ matrix.features }})"
        run: |
          if [ -z "${{ matrix.features }}" ]; then
            cargo test -p coremlit
          else
            cargo test -p coremlit --features "${{ matrix.features }}"
          fi
      - name: "cargo clippy -p coremlit --all-targets (features: ${{ matrix.features }})"
        if: ${{ !cancelled() }}
        run: |
          if [ -z "${{ matrix.features }}" ]; then
            cargo clippy -p coremlit --no-deps --all-targets -- -D warnings
          else
            cargo clippy -p coremlit --no-deps --all-targets --features "${{ matrix.features }}" -- -D warnings
          fi
  parity:
    runs-on: macos-15
    steps:
      - uses: actions/checkout@v7
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: "cargo test -p coremlit-parity (features: ${{ matrix.features }})"
        run: cargo test -p coremlit-parity --features "${{ matrix.features }}"
      - name: "cargo clippy -p coremlit-parity --all-targets (features: ${{ matrix.features }})"
        if: ${{ !cancelled() }}
        run: cargo clippy -p coremlit-parity --no-deps --all-targets --features "${{ matrix.features }}" -- -D warnings
"#;

/// The clippy pin reads the well-formed fixture clean — a guard on the mutation
/// cases below, each of which perturbs this same fixture.
#[test]
fn ci_clippy_check_reads_the_wellformed_steps() {
  assert_clippy_step(DOCTORED_CLIPPY_JOBS, &FEATURES_CLIPPY);
  assert_clippy_step(DOCTORED_CLIPPY_JOBS, &PARITY_CLIPPY);
}

/// Each perturbation of the fixture reds the pin of the job it lands in, and the
/// message names that job and what dropped. A case replaces the FIRST match, so a
/// `features`-job case that landed in `parity` instead leaves its pin green and
/// fails here. Every `--all-targets` case leaves the step's `name:` spelling the
/// flag: the `run` lines are what must carry it.
#[test]
fn ci_clippy_check_reds_on_each_dropped_piece() {
  struct Mutation {
    what: &'static str,
    from: &'static str,
    to: &'static str,
    pin: &'static IntendedClippy,
    names: &'static [&'static str],
  }
  let mutations = [
    Mutation {
      what: "the empty arm drops --all-targets",
      from: "cargo clippy -p coremlit --no-deps --all-targets -- -D warnings",
      to: "cargo clippy -p coremlit --no-deps -- -D warnings",
      pin: &FEATURES_CLIPPY,
      names: &["`features`", "the empty-features arm", "`--all-targets`"],
    },
    Mutation {
      what: "the features arm drops --all-targets",
      from: "cargo clippy -p coremlit --no-deps --all-targets --features",
      to: "cargo clippy -p coremlit --no-deps --features",
      pin: &FEATURES_CLIPPY,
      names: &["`features`", "the `--features` arm", "`--all-targets`"],
    },
    Mutation {
      what: "the features arm drops -D warnings",
      from: "--all-targets --features \"${{ matrix.features }}\" -- -D warnings",
      to: "--all-targets --features \"${{ matrix.features }}\"",
      pin: &FEATURES_CLIPPY,
      names: &["`features`", "the `--features` arm", "`-- -D warnings`"],
    },
    Mutation {
      what: "the empty arm drops --no-deps",
      from: "cargo clippy -p coremlit --no-deps --all-targets -- -D warnings",
      to: "cargo clippy -p coremlit --all-targets -- -D warnings",
      pin: &FEATURES_CLIPPY,
      names: &["`features`", "the empty-features arm", "`--no-deps`"],
    },
    Mutation {
      what: "a trailing comment stands in for --all-targets",
      from: "cargo clippy -p coremlit --no-deps --all-targets -- -D warnings",
      to: "cargo clippy -p coremlit --no-deps -- -D warnings # --all-targets",
      pin: &FEATURES_CLIPPY,
      names: &["`features`", "`--all-targets`"],
    },
    Mutation {
      what: "the features arm stops reading the matrix row",
      from: "--all-targets --features \"${{ matrix.features }}\" -- -D warnings",
      to: "--all-targets -- -D warnings",
      pin: &FEATURES_CLIPPY,
      names: &["`features`", "--features \"${{ matrix.features }}\""],
    },
    Mutation {
      what: "the empty arm is gone",
      from: "            cargo clippy -p coremlit --no-deps --all-targets -- -D warnings\n",
      to: "",
      pin: &FEATURES_CLIPPY,
      names: &["`features`", "without `--features`"],
    },
    Mutation {
      what: "the parity step drops --all-targets",
      from: "-p coremlit-parity --no-deps --all-targets",
      to: "-p coremlit-parity --no-deps",
      pin: &PARITY_CLIPPY,
      names: &["`parity`", "`--all-targets`"],
    },
    Mutation {
      what: "the parity step lints the wrong package",
      from: "run: cargo clippy -p coremlit-parity",
      to: "run: cargo clippy -p coremlit",
      pin: &PARITY_CLIPPY,
      names: &["`parity`", "`cargo clippy -p coremlit-parity`"],
    },
    Mutation {
      what: "the features step loses its condition",
      from: "        if: ${{ !cancelled() }}\n",
      to: "",
      pin: &FEATURES_CLIPPY,
      names: &["`features`", "`if: ${{ !cancelled() }}`"],
    },
    Mutation {
      what: "the parity step's condition becomes always()",
      from: "        if: ${{ !cancelled() }}\n        run: cargo clippy",
      to: "        if: ${{ always() }}\n        run: cargo clippy",
      pin: &PARITY_CLIPPY,
      names: &["`parity`", "`if: ${{ !cancelled() }}`"],
    },
    Mutation {
      what: "the features job stops installing clippy",
      from: "components: clippy",
      to: "components: rustfmt",
      pin: &FEATURES_CLIPPY,
      names: &["`features`", "`components: clippy`"],
    },
  ];
  for case in mutations {
    let doctored = DOCTORED_CLIPPY_JOBS.replacen(case.from, case.to, 1);
    assert_ne!(
      doctored, DOCTORED_CLIPPY_JOBS,
      "{}: the case matched nothing in the fixture",
      case.what
    );
    let Err(drift) = clippy_step_drift(&doctored, case.pin) else {
      panic!(
        "{}: the pin still reads the doctored ci.yml as intended",
        case.what
      );
    };
    for name in case.names {
      assert!(
        drift.contains(name),
        "{}: the message must name {name}, got: {drift}",
        case.what
      );
    }
  }
}

/// A job that lost its clippy step reds although the `check` job above it and the
/// `parity` job below it still run `cargo clippy`: the pin is scoped to its own
/// job, as the matrix pins are, and the other job's pin does not notice.
#[test]
fn ci_clippy_check_reds_when_a_job_loses_its_step() {
  let (head, tail) = DOCTORED_CLIPPY_JOBS
    .split_once("      - name: \"cargo clippy -p coremlit --all-targets")
    .expect("the fixture's `features` clippy step");
  let (_, parity) = tail
    .split_once("  parity:")
    .expect("the fixture's `parity` job");
  let doctored = format!("{head}  parity:{parity}");
  let Err(drift) = clippy_step_drift(&doctored, &FEATURES_CLIPPY) else {
    panic!("a `features` job without a clippy step still passed its pin");
  };
  assert!(
    drift.contains("`features`") && drift.contains("exactly one step running `cargo clippy`"),
    "the message must name the job and the missing step, got: {drift}"
  );
  assert_clippy_step(&doctored, &PARITY_CLIPPY);
}

/// Commenting the clippy commands out removes them: a comment line is not a
/// command, as a commented-out matrix entry is not a combo.
#[test]
fn ci_clippy_check_reds_when_the_commands_are_commented_out() {
  let doctored = DOCTORED_CLIPPY_JOBS
    .lines()
    .map(|line| {
      if line.contains("cargo clippy -p coremlit --no-deps") {
        format!("# {line}")
      } else {
        line.to_string()
      }
    })
    .collect::<Vec<_>>()
    .join("\n");
  assert_ne!(doctored, DOCTORED_CLIPPY_JOBS);
  let Err(drift) = clippy_step_drift(&doctored, &FEATURES_CLIPPY) else {
    panic!("a `features` job with its clippy commands commented out still passed its pin");
  };
  assert!(
    drift.contains("`features`") && drift.contains("exactly one step running `cargo clippy`"),
    "the message must name the job and the missing step, got: {drift}"
  );
}

/// A re-wrapped command is the same command: a backslash-continued `cargo clippy`
/// reads as one line, so reformatting the step alone does not red the pin.
#[test]
fn ci_clippy_check_reads_a_rewrapped_command() {
  let doctored = DOCTORED_CLIPPY_JOBS.replacen(
    "--no-deps --all-targets --features \"${{ matrix.features }}\" -- -D warnings",
    "--no-deps --all-targets \\\n              --features \"${{ matrix.features }}\" -- -D warnings",
    1,
  );
  assert_ne!(doctored, DOCTORED_CLIPPY_JOBS);
  assert_clippy_step(&doctored, &FEATURES_CLIPPY);
}

/// `fixture` with the first `from` at or after the `  <job>:` key replaced by
/// `to`, so a case lands in the job it names and not in a lookalike ahead of it.
fn doctored_in(fixture: &str, job: &str, from: &str, to: &str) -> String {
  let key = format!("\n  {job}:\n");
  let at = fixture
    .find(&key)
    .unwrap_or_else(|| panic!("the fixture has no `{job}` job"));
  let (head, tail) = fixture.split_at(at);
  format!("{head}{}", tail.replacen(from, to, 1))
}

/// A perturbed fixture and the pieces the message of the pin that reds it must
/// name.
struct Doctored {
  what: &'static str,
  yaml: String,
  names: &'static [&'static str],
}

/// Require every doctored fixture to red `pin`, its message naming each piece.
/// A case that changed nothing in the fixture, or that landed in a lookalike job
/// and left the pin green, fails here instead of passing for a pin that works.
fn assert_each_drift_reds(fixture: &str, cases: &[Doctored], pin: fn(&str) -> Result<(), String>) {
  for case in cases {
    assert_ne!(
      case.yaml, fixture,
      "{}: the case matched nothing in the fixture",
      case.what
    );
    let Err(drift) = pin(&case.yaml) else {
      panic!(
        "{}: the pin still reads the doctored ci.yml as intended",
        case.what
      );
    };
    for name in case.names {
      assert!(
        drift.contains(name),
        "{}: the message must name {name}, got: {drift}",
        case.what
      );
    }
  }
}

/// A well-formed `check` job as ci.yml spells it, between two jobs that carry
/// the same toolchain, format, lint and doc steps: the fixture the mutation
/// cases below perturb. A `check` job that lost a step must not read as intact
/// for what a neighbour still runs, so each case lands in `check` and the pin
/// has to red there.
const DOCTORED_CHECK_JOBS: &str = r#"
jobs:
  docs:
    runs-on: macos-15
    steps:
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt
      - run: cargo fmt --all --check
      - run: cargo clippy --all-targets --all-features -- -D warnings
      - run: cargo doc --no-deps --all-features
        env:
          RUSTDOCFLAGS: -D warnings
  check:
    runs-on: macos-15
    steps:
      - uses: actions/checkout@v7
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy, rustfmt
      - run: cargo fmt --all --check
      - run: cargo clippy --all-targets --all-features -- -D warnings
      - run: cargo doc --no-deps --all-features
        env:
          RUSTDOCFLAGS: -D warnings
      - run: cargo build --features whisper --examples
  features:
    runs-on: macos-15
    steps:
      - run: cargo clippy --all-targets --all-features -- -D warnings
"#;

/// The `check` fixture's fmt step as one `run` line.
const CHECK_FMT_STEP: &str = "      - run: cargo fmt --all --check\n";

/// The `check` fixture's clippy step as one `run` line.
const CHECK_CLIPPY_STEP: &str =
  "      - run: cargo clippy --all-targets --all-features -- -D warnings\n";

/// The `check` fixture's doc step, its `env:` included.
const CHECK_DOC_STEP: &str = "      - run: cargo doc --no-deps --all-features\n        env:\n          RUSTDOCFLAGS: -D warnings\n";

/// The pins read the well-formed fixture clean — a guard on the mutation cases
/// below, each of which perturbs this same fixture.
#[test]
fn ci_check_job_pins_read_the_wellformed_steps() {
  assert_no_drift(check_fmt_drift(DOCTORED_CHECK_JOBS));
  assert_no_drift(check_clippy_drift(DOCTORED_CHECK_JOBS));
  assert_no_drift(check_doc_drift(DOCTORED_CHECK_JOBS));
  assert_no_drift(check_components_drift(DOCTORED_CHECK_JOBS));
}

/// Each perturbation of the `check` job's fmt step reds its pin, and the message
/// names the job and what dropped.
#[test]
fn ci_check_fmt_pin_reds_on_each_dropped_piece() {
  let in_check = |from: &str, to: &str| doctored_in(DOCTORED_CHECK_JOBS, "check", from, to);
  let cases = [
    Doctored {
      what: "the fmt step is gone",
      yaml: in_check(CHECK_FMT_STEP, ""),
      names: &["`check`", "exactly one step running `cargo fmt`"],
    },
    Doctored {
      what: "the fmt step is commented out",
      yaml: in_check(CHECK_FMT_STEP, "      # - run: cargo fmt --all --check\n"),
      names: &["`check`", "exactly one step running `cargo fmt`"],
    },
    Doctored {
      what: "a second fmt step is added",
      yaml: in_check(
        "      - run: cargo build",
        &format!("{CHECK_FMT_STEP}      - run: cargo build"),
      ),
      names: &["`check`", "exactly one step running `cargo fmt`", "found 2"],
    },
    Doctored {
      what: "the step drops --check",
      yaml: in_check("cargo fmt --all --check", "cargo fmt --all"),
      names: &["`check`", "`--check`"],
    },
    Doctored {
      what: "the step drops --all",
      yaml: in_check("cargo fmt --all --check", "cargo fmt --check"),
      names: &["`check`", "`--all`"],
    },
    Doctored {
      what: "--all rides after the separator, where rustfmt rejects it",
      yaml: in_check("cargo fmt --all --check", "cargo fmt -- --all --check"),
      names: &["`check`", "`--all`"],
    },
    Doctored {
      what: "a trailing comment stands in for --check",
      yaml: in_check("cargo fmt --all --check", "cargo fmt --all # --check"),
      names: &["`check`", "`--check`"],
    },
    Doctored {
      what: "the step's name spells the flag its run line dropped",
      yaml: in_check(
        CHECK_FMT_STEP,
        "      - name: cargo fmt --all --check\n        run: cargo fmt --all\n",
      ),
      names: &["`check`", "`--check`"],
    },
  ];
  assert_each_drift_reds(DOCTORED_CHECK_JOBS, &cases, check_fmt_drift);
}

/// A re-spelled command is the same command: `--check` handed to `rustfmt` after
/// the separator, the two flags in the other order, and a backslash-continued
/// script all still read as the step the pin intends.
#[test]
fn ci_check_fmt_pin_reads_a_respelled_command() {
  for spelling in [
    "      - run: cargo fmt --all -- --check\n",
    "      - run: cargo fmt --check --all\n",
    "      - run: |\n          cargo fmt --all \\\n            --check\n",
  ] {
    let doctored = doctored_in(DOCTORED_CHECK_JOBS, "check", CHECK_FMT_STEP, spelling);
    assert_ne!(doctored, DOCTORED_CHECK_JOBS);
    assert_no_drift(check_fmt_drift(&doctored));
  }
}

/// Each perturbation of the `check` job's clippy step reds its pin, and the
/// message names the job and what dropped.
#[test]
fn ci_check_clippy_pin_reds_on_each_dropped_piece() {
  let in_check = |from: &str, to: &str| doctored_in(DOCTORED_CHECK_JOBS, "check", from, to);
  let cases = [
    Doctored {
      what: "the clippy step is gone",
      yaml: in_check(CHECK_CLIPPY_STEP, ""),
      names: &["`check`", "exactly one step running `cargo clippy`"],
    },
    Doctored {
      what: "the clippy step is commented out",
      yaml: in_check(
        CHECK_CLIPPY_STEP,
        "      # - run: cargo clippy --all-targets --all-features -- -D warnings\n",
      ),
      names: &["`check`", "exactly one step running `cargo clippy`"],
    },
    Doctored {
      what: "a second clippy step is added",
      yaml: in_check(
        "      - run: cargo build",
        "      - run: cargo clippy --all-targets --all-features -- -D warnings\n      - run: cargo build",
      ),
      names: &[
        "`check`",
        "exactly one step running `cargo clippy`",
        "found 2",
      ],
    },
    Doctored {
      what: "the step drops --all-targets",
      yaml: in_check(
        "cargo clippy --all-targets --all-features",
        "cargo clippy --all-features",
      ),
      names: &["`check`", "`--all-targets`"],
    },
    Doctored {
      what: "the step drops --all-features",
      yaml: in_check(
        "cargo clippy --all-targets --all-features",
        "cargo clippy --all-targets",
      ),
      names: &["`check`", "`--all-features`"],
    },
    Doctored {
      what: "the step drops -D warnings",
      yaml: in_check("--all-features -- -D warnings", "--all-features"),
      names: &["`check`", "`-- -D warnings`"],
    },
    Doctored {
      what: "the step narrows to one package",
      yaml: in_check(
        "cargo clippy --all-targets",
        "cargo clippy -p coremlit --all-targets",
      ),
      names: &["`check`", "the whole workspace", "names a package"],
    },
    Doctored {
      what: "a trailing comment stands in for --all-features",
      yaml: in_check(
        "cargo clippy --all-targets --all-features -- -D warnings",
        "cargo clippy --all-targets -- -D warnings # --all-features",
      ),
      names: &["`check`", "`--all-features`"],
    },
    Doctored {
      what: "the step's name spells the flag its run line dropped",
      yaml: in_check(
        CHECK_CLIPPY_STEP,
        "      - name: cargo clippy --all-targets --all-features -- -D warnings\n        run: cargo clippy --all-targets -- -D warnings\n",
      ),
      names: &["`check`", "`--all-features`"],
    },
  ];
  assert_each_drift_reds(DOCTORED_CHECK_JOBS, &cases, check_clippy_drift);
}

/// Each perturbation of the `check` job's doc step reds its pin, and the
/// message names the job and what dropped.
#[test]
fn ci_check_doc_pin_reds_on_each_dropped_piece() {
  let in_check = |from: &str, to: &str| doctored_in(DOCTORED_CHECK_JOBS, "check", from, to);
  let cases = [
    Doctored {
      what: "the doc step is gone",
      yaml: in_check(CHECK_DOC_STEP, ""),
      names: &["`check`", "exactly one step running `cargo doc`"],
    },
    Doctored {
      what: "the doc step is commented out",
      yaml: in_check(
        "      - run: cargo doc --no-deps --all-features\n",
        "      # - run: cargo doc --no-deps --all-features\n",
      ),
      names: &["`check`", "exactly one step running `cargo doc`"],
    },
    Doctored {
      what: "a second doc step is added",
      yaml: in_check(
        "      - run: cargo build",
        &format!("{CHECK_DOC_STEP}      - run: cargo build"),
      ),
      names: &["`check`", "exactly one step running `cargo doc`", "found 2"],
    },
    Doctored {
      what: "the step drops --no-deps",
      yaml: in_check(
        "cargo doc --no-deps --all-features",
        "cargo doc --all-features",
      ),
      names: &["`check`", "`--no-deps`"],
    },
    Doctored {
      what: "the step drops --all-features",
      yaml: in_check("cargo doc --no-deps --all-features", "cargo doc --no-deps"),
      names: &["`check`", "`--all-features`"],
    },
    Doctored {
      what: "RUSTDOCFLAGS is gone",
      yaml: in_check("        env:\n          RUSTDOCFLAGS: -D warnings\n", ""),
      names: &["`check`", "`RUSTDOCFLAGS: -D warnings`"],
    },
    Doctored {
      what: "RUSTDOCFLAGS stops denying warnings",
      yaml: in_check("RUSTDOCFLAGS: -D warnings", "RUSTDOCFLAGS: --cfg docsrs"),
      names: &["`check`", "`RUSTDOCFLAGS: -D warnings`"],
    },
    Doctored {
      what: "a trailing comment stands in for -D warnings",
      yaml: in_check(
        "RUSTDOCFLAGS: -D warnings",
        "RUSTDOCFLAGS: --cfg docsrs # -D warnings",
      ),
      names: &["`check`", "`RUSTDOCFLAGS: -D warnings`"],
    },
  ];
  assert_each_drift_reds(DOCTORED_CHECK_JOBS, &cases, check_doc_drift);
}

/// A re-spelled flag is the same flag: `RUSTDOCFLAGS` quoted, carrying other
/// flags, and denying warnings as `-Dwarnings` still reads as the pin intends.
#[test]
fn ci_check_doc_pin_reads_a_respelled_flag() {
  let doctored = doctored_in(
    DOCTORED_CHECK_JOBS,
    "check",
    "RUSTDOCFLAGS: -D warnings",
    "RUSTDOCFLAGS: \"--cfg docsrs -Dwarnings\"",
  );
  assert_ne!(doctored, DOCTORED_CHECK_JOBS);
  assert_no_drift(check_doc_drift(&doctored));
}

/// The `check` job's toolchain step, `with:` map included, as the fixture
/// spells it.
const CHECK_TOOLCHAIN_STEP: &str = "      - uses: dtolnay/rust-toolchain@stable\n        with:\n          components: clippy, rustfmt\n";

/// Each perturbation of the `check` job's toolchain install reds its pin, and the
/// message names the job and the component no step ahead of the tool installs.
#[test]
fn ci_check_components_pin_reds_on_each_dropped_piece() {
  let in_check = |from: &str, to: &str| doctored_in(DOCTORED_CHECK_JOBS, "check", from, to);
  let cases = [
    Doctored {
      what: "the toolchain step asks for no components",
      yaml: in_check("        with:\n          components: clippy, rustfmt\n", ""),
      names: &["`check`", "`cargo fmt`", "`components: rustfmt`"],
    },
    Doctored {
      what: "the toolchain step stops asking for rustfmt",
      yaml: in_check("components: clippy, rustfmt", "components: clippy"),
      names: &["`check`", "`cargo fmt`", "`components: rustfmt`"],
    },
    Doctored {
      what: "the toolchain step stops asking for clippy",
      yaml: in_check("components: clippy, rustfmt", "components: rustfmt"),
      names: &["`check`", "`cargo clippy`", "`components: clippy`"],
    },
    Doctored {
      what: "the components line is only a comment",
      yaml: in_check(
        "          components: clippy, rustfmt\n",
        "          # components: clippy, rustfmt\n",
      ),
      names: &["`check`", "`components: rustfmt`"],
    },
    Doctored {
      what: "a trailing comment stands in for a component",
      yaml: in_check(
        "components: clippy, rustfmt",
        "components: clippy # rustfmt",
      ),
      names: &["`check`", "`components: rustfmt`"],
    },
    Doctored {
      what: "another input names the component",
      yaml: in_check(
        "components: clippy, rustfmt",
        "toolchain: rustfmt\n          components: clippy",
      ),
      names: &["`check`", "`components: rustfmt`"],
    },
    Doctored {
      what: "the toolchain step comes after the steps that need it",
      yaml: in_check(
        &format!("{CHECK_TOOLCHAIN_STEP}      - run: cargo fmt --all --check\n"),
        &format!("      - run: cargo fmt --all --check\n{CHECK_TOOLCHAIN_STEP}"),
      ),
      names: &["`check`", "no step ahead of", "`components: rustfmt`"],
    },
    Doctored {
      what: "the toolchain step is commented out",
      yaml: in_check(
        CHECK_TOOLCHAIN_STEP,
        "      # - uses: dtolnay/rust-toolchain@stable\n      #   with:\n      #     components: clippy, rustfmt\n",
      ),
      names: &["`check`", "`components: rustfmt`"],
    },
    Doctored {
      what: "the fmt step is gone",
      yaml: in_check("      - run: cargo fmt --all --check\n", ""),
      names: &["`check`", "exactly one step running `cargo fmt`"],
    },
    Doctored {
      what: "the clippy step is gone",
      yaml: in_check(CHECK_CLIPPY_STEP, ""),
      names: &["`check`", "exactly one step running `cargo clippy`"],
    },
  ];
  assert_each_drift_reds(DOCTORED_CHECK_JOBS, &cases, check_components_drift);
}

/// A re-spelled list is the same list: a different order, no space after the
/// comma, and quoted entries all still read as the components the pin intends.
#[test]
fn ci_check_components_pin_reads_a_respelled_list() {
  for list in [
    "rustfmt, clippy",
    "clippy,rustfmt",
    "\"clippy, rustfmt\"",
    "'clippy', 'rustfmt'",
  ] {
    let doctored = doctored_in(
      DOCTORED_CHECK_JOBS,
      "check",
      "components: clippy, rustfmt",
      &format!("components: {list}"),
    );
    assert_ne!(doctored, DOCTORED_CHECK_JOBS);
    assert_no_drift(check_components_drift(&doctored));
  }
}

/// A well-formed `model-tests` job as ci.yml spells its shape, with a lookalike
/// job that is itself compliant ahead of it and a job with an unguarded test
/// step after it: the fixture the mutation cases below perturb. A step before the
/// staging step carries no condition, as in ci.yml.
const DOCTORED_MODEL_TESTS_JOBS: &str = r#"
jobs:
  staging:
    runs-on: macos-15
    steps:
      - name: Stage this kit's models
        id: download
        uses: ./.github/actions/stage-models
      - name: Model gates
        id: gates
        if: ${{ !cancelled() && steps.download.outcome != 'failure' }}
        run: cargo test -p coremlit -- --ignored
      - name: Gate ledger (a gate that never ran proves nothing)
        if: ${{ !cancelled() }}
        run: echo ledger
  model-tests:
    name: model-tests (${{ matrix.kit }})
    runs-on: macos-15
    strategy:
      fail-fast: false
      matrix:
        include:
          - kit: whisper
    steps:
      - uses: actions/checkout@v7
      - uses: dtolnay/rust-toolchain@stable
      - name: Stage this kit's models
        id: download
        uses: ./.github/actions/stage-models
        with:
          kit: ${{ matrix.kit }}
      - name: Verify staged overlay ordering
        id: overlay
        if: ${{ !cancelled() && steps.download.outcome != 'failure' }}
        run: ./check-overlay.sh
      - name: Verify staged artifact checksums
        id: checksums
        if: ${{ !cancelled() && steps.download.outcome != 'failure' }}
        run: |
          set -euo pipefail
          shasum -a 256 -c CHECKSUMS.sha256
      - name: fp16 graph sweep
        id: fp16_guards
        if: ${{ !cancelled() && steps.download.outcome != 'failure' }}
        run: cargo test -p coremlit --test fp16_guards
      - name: fp16 sweep inventory
        id: fp16_inventory
        if: ${{ !cancelled() && steps.download.outcome != 'failure' }}
        run: tests/fp16_sweep_inventory.sh
      - name: Model gates
        id: gates
        if: ${{ !cancelled() && steps.download.outcome != 'failure' }}
        env:
          KIT: ${{ matrix.kit }}
        run: |
          set -euo pipefail
          cargo test -p coremlit --features "$features" -- --ignored
      - name: Gate ledger (a gate that never ran proves nothing)
        if: ${{ !cancelled() }}
        env:
          LEDGER: |
            overlay-ordering=${{ steps.overlay.outcome }}
            artifact-checksums=${{ steps.checksums.outcome }}
            fp16-graph-sweep=${{ steps.fp16_guards.outcome }}
            fp16-sweep-inventory=${{ steps.fp16_inventory.outcome }}
            model-gates=${{ steps.gates.outcome }}
        run: |
          set -euo pipefail
          printf '%s\n' "$LEDGER"
  parity:
    runs-on: macos-15
    steps:
      - run: cargo test -p coremlit-parity
"#;

/// The condition line every `model-tests` check carries in the fixture.
const MODEL_TESTS_GUARD_LINE: &str =
  "        if: ${{ !cancelled() && steps.download.outcome != 'failure' }}\n";

/// The pin reads the well-formed fixture clean — a guard on the mutation cases
/// below, each of which perturbs this same fixture.
#[test]
fn ci_model_tests_pin_reads_the_wellformed_job() {
  assert_no_drift(model_tests_guard_drift(DOCTORED_MODEL_TESTS_JOBS));
}

/// Each perturbation of the `model-tests` job reds its pin, and the message names
/// the job and the step or piece that drifted. A case lands in `model-tests` and
/// not in the compliant job ahead of it, so a renamed staging id or a dropped
/// ledger reds although that job still has both.
#[test]
fn ci_model_tests_pin_reds_on_each_dropped_piece() {
  let in_job =
    |from: &str, to: &str| doctored_in(DOCTORED_MODEL_TESTS_JOBS, "model-tests", from, to);
  let guarded = |id: &str| format!("        id: {id}\n{MODEL_TESTS_GUARD_LINE}");
  let without_tests = doctored_in(
    &in_job(
      "run: cargo test -p coremlit --test fp16_guards",
      "run: echo fp16",
    ),
    "model-tests",
    "          cargo test -p coremlit --features \"$features\" -- --ignored\n",
    "          echo gates\n",
  );
  let inventory = format!(
    "      - name: fp16 sweep inventory\n        id: fp16_inventory\n{MODEL_TESTS_GUARD_LINE}        \
     run: tests/fp16_sweep_inventory.sh\n"
  );
  let ledger_entry = "            model-gates=${{ steps.gates.outcome }}\n";
  let cases = [
    Doctored {
      what: "the staging step's id is renamed",
      yaml: in_job("id: download", "id: staged"),
      names: &["`model-tests`", "`id: download`"],
    },
    Doctored {
      what: "a check loses its condition",
      yaml: in_job(&guarded("gates"), "        id: gates\n"),
      names: &[
        "`model-tests`",
        "`Model gates`",
        "steps.download.outcome != 'failure'",
      ],
    },
    Doctored {
      what: "a check's condition loses the staging clause",
      yaml: in_job(
        &guarded("fp16_guards"),
        "        id: fp16_guards\n        if: ${{ !cancelled() }}\n",
      ),
      names: &["`model-tests`", "`fp16 graph sweep`"],
    },
    Doctored {
      what: "a check's condition becomes always()",
      yaml: in_job(
        &guarded("checksums"),
        "        id: checksums\n        if: ${{ always() && steps.download.outcome != 'failure' }}\n",
      ),
      names: &["`model-tests`", "`Verify staged artifact checksums`"],
    },
    Doctored {
      what: "a check's condition is only a comment",
      yaml: in_job(
        &guarded("gates"),
        "        id: gates\n        # if: ${{ !cancelled() && steps.download.outcome != 'failure' }}\n",
      ),
      names: &["`model-tests`", "`Model gates`"],
    },
    Doctored {
      what: "a check is added without a condition",
      yaml: in_job(
        "      - name: Model gates\n",
        "      - run: cargo test -p coremlit --test newcheck\n      - name: Model gates\n",
      ),
      names: &["`model-tests`", "`cargo test -p coremlit --test newcheck`"],
    },
    Doctored {
      what: "the ledger loses its condition",
      yaml: in_job(
        "      - name: Gate ledger (a gate that never ran proves nothing)\n        if: ${{ !cancelled() }}\n",
        "      - name: Gate ledger (a gate that never ran proves nothing)\n",
      ),
      names: &["`model-tests`", "`Gate ledger`", "alone"],
    },
    Doctored {
      what: "the ledger waits on the download",
      yaml: in_job(
        "        if: ${{ !cancelled() }}\n        env:\n          LEDGER",
        "        if: ${{ !cancelled() && steps.download.outcome != 'failure' }}\n        env:\n          LEDGER",
      ),
      names: &["`model-tests`", "`Gate ledger`", "alone"],
    },
    Doctored {
      what: "a step is added after the ledger",
      yaml: in_job(
        "  parity:\n",
        "      - run: cargo test -p coremlit --test late\n  parity:\n",
      ),
      names: &["`model-tests`", "the last step", "`Gate ledger`"],
    },
    Doctored {
      what: "the ledger is no longer the Gate ledger",
      yaml: in_job(
        "Gate ledger (a gate that never ran proves nothing)",
        "Verdict summary",
      ),
      names: &["`model-tests`", "the last step", "`Gate ledger`"],
    },
    Doctored {
      what: "no kit test is left between staging and the ledger",
      yaml: without_tests,
      names: &["`model-tests`", "`cargo test`"],
    },
    Doctored {
      what: "a check loses its id",
      yaml: in_job(
        "      - name: Model gates\n        id: gates\n",
        "      - name: Model gates\n",
      ),
      names: &["`model-tests`", "`Model gates`", "`id:`"],
    },
    Doctored {
      what: "the ledger no longer reads a check",
      yaml: in_job(ledger_entry, ""),
      names: &["`model-tests`", "`Gate ledger`", "`gates`", "`Model gates`"],
    },
    Doctored {
      what: "the ledger's entry for a check is only a comment",
      yaml: in_job(
        ledger_entry,
        "            # model-gates=${{ steps.gates.outcome }}\n",
      ),
      names: &["`model-tests`", "`Gate ledger`", "`gates`"],
    },
    Doctored {
      what: "the ledger reads the check under another id",
      yaml: in_job(
        ledger_entry,
        "            model-gates=${{ steps.gate.outcome }}\n",
      ),
      names: &["`model-tests`", "`Gate ledger`", "`gates`"],
    },
    Doctored {
      what: "a check is dropped",
      yaml: in_job(&inventory, ""),
      names: &[
        "`model-tests`",
        "`fp16 sweep inventory`",
        "every shard runs",
      ],
    },
    Doctored {
      what: "a check is renamed",
      yaml: in_job("name: fp16 sweep inventory", "name: fp16 inventory sweep"),
      names: &["`model-tests`", "`fp16 sweep inventory`"],
    },
    Doctored {
      what: "a check is commented out",
      yaml: in_job(
        &inventory,
        "      # - name: fp16 sweep inventory\n      #   id: fp16_inventory\n      #   if: ${{ !cancelled() && steps.download.outcome != 'failure' }}\n      #   run: tests/fp16_sweep_inventory.sh\n",
      ),
      names: &["`model-tests`", "`fp16 sweep inventory`"],
    },
  ];
  assert_each_drift_reds(DOCTORED_MODEL_TESTS_JOBS, &cases, model_tests_guard_drift);
}

/// A job with no `model-tests` key parses to no steps and reds, rather than the
/// pin reading another job's steps in its place.
#[test]
fn ci_model_tests_pin_reds_when_the_job_is_absent() {
  let renamed = DOCTORED_MODEL_TESTS_JOBS.replacen("  model-tests:\n", "  model-shards:\n", 1);
  assert_ne!(renamed, DOCTORED_MODEL_TESTS_JOBS);
  let Err(drift) = model_tests_guard_drift(&renamed) else {
    panic!("a ci.yml with no `model-tests` job still passed its pin");
  };
  assert!(
    drift.contains("`model-tests`") && drift.contains("`id: download`"),
    "the message must name the job and the missing staging step, got: {drift}"
  );
}