cargo-tops 0.4.0

init / check / gate for the Rust-TOPS protocol. Orchestrates existing Cargo tools; does not replace them.
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
#![cfg_attr(
    test,
    expect(
        clippy::needless_pass_by_value,
        reason = "this crate already violates the strict table"
    )
)]
#![cfg_attr(
    test,
    expect(clippy::panic, reason = "this crate already violates the strict table")
)]
#![expect(
    elided_lifetimes_in_paths,
    reason = "this crate already violates the strict table"
)]
use anyhow::{anyhow, Context, Result};
use clap::{Parser, Subcommand};
use std::fmt::Write as _;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitCode};
use std::time::SystemTime;

mod config;
mod coverage_floor;
mod crap_ratchet;
mod credit;
mod exemptions;
mod facts;
mod floors;
mod init_meta;
mod laws;
mod profile_data;
mod profiles;
mod schema;
mod scope;
mod skills;
mod steps;
mod strict_lints;

#[cfg(test)]
mod parse_oracle;

use config::TopsConfig;
use facts::{Outcome, Run};
use steps::{Kind, MutationPlan, Step};

#[derive(Parser)]
#[command(
    name = "cargo-tops",
    bin_name = "cargo-tops",
    version,
    about = "Rust-TOPS protocol CLI: init, check, gate. Policy and sequencing only — not a test engine."
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

/// The flags of `init`.
#[derive(clap::Args)]
struct InitArgs {
    /// Crate class: one of the profiles in `profiles/` (default: library-core)
    #[arg(long, default_value = "library-core")]
    class: String,
    /// Directory to initialize (default: cwd)
    #[arg(long, default_value = ".")]
    path: PathBuf,
    /// Crate name written into rust-tops.yaml (default: directory name)
    #[arg(long)]
    name: Option<String>,
    /// Overwrite existing protocol files
    #[arg(long)]
    force: bool,
    /// Also write portable agent skills, CLAUDE.md, and docs/REVIEW.md
    #[arg(long)]
    skills: bool,
    /// Also write the Bend laws kit and turn the laws layer on
    #[arg(long)]
    laws: bool,
}

#[derive(Subcommand)]
enum Commands {
    /// Write rust-tops.yaml, rust-tops.lock, AGENTS.md, RUST-TOPS.md and the drop-in tool configs, read from the crate's Cargo.toml
    Init(InitArgs),
    /// Validate rust-tops.yaml against the protocol schema (typed check)
    Check {
        #[arg(long, default_value = ".")]
        path: PathBuf,
    },
    /// Run enabled Tier 0–1 tools in protocol order, then the Bend laws layer
    Gate {
        #[arg(long, default_value = ".")]
        path: PathBuf,
        /// Only run layers at or below this tier (default 1)
        #[arg(long, default_value_t = 1)]
        max_tier: u8,
        /// Print commands without running them
        #[arg(long)]
        dry_run: bool,
    },
    /// Record the last gate's CRAP report as the baseline the crap ratchet reads
    CrapBaseline {
        #[arg(long, default_value = ".")]
        path: PathBuf,
    },
    /// Print the package version, the protocol it implements, and the pins in ./rust-tops.lock
    Version,
}

/// The protocol version this build implements.
const PROTOCOL_VERSION: &str = "1.2.0";

/// What `cargo tops version` prints: package, protocol, then the `[tools]`
/// pins of the lock text, or one line saying why there are none.
/// `lock` is `None` when `./rust-tops.lock` does not exist.
fn version_text(lock: Option<&str>) -> String {
    let mut lines = vec![
        format!("cargo-tops {}", env!("CARGO_PKG_VERSION")),
        format!("protocol {PROTOCOL_VERSION}"),
    ];
    match lock.map(str::parse::<toml::Table>) {
        None => lines.push("tool pins: none (no ./rust-tops.lock)".to_string()),
        Some(Err(err)) => {
            lines.push(format!(
                "tool pins: ./rust-tops.lock is not valid TOML: {err}"
            ));
        }
        Some(Ok(table)) => {
            lines.push("tool pins (./rust-tops.lock):".to_string());
            let pins = table.get("tools").and_then(toml::Value::as_table);
            for (name, pin) in pins.into_iter().flatten() {
                lines.push(format!("  {name} = {}", pin.as_str().unwrap_or("?")));
            }
        }
    }
    lines.push(String::new());
    lines.join("\n")
}

/// `cargo tops version`: reads `./rust-tops.lock` from the current directory.
fn print_version() {
    let lock = fs::read_to_string("rust-tops.lock").ok();
    print!("{}", version_text(lock.as_deref()));
}

fn main() -> ExitCode {
    match run() {
        Ok(code) => code,
        Err(err) => {
            eprintln!("error: {err:#}");
            ExitCode::from(2)
        }
    }
}

/// The argv clap parses. `cargo tops …` execs this binary with argv[1] ==
/// "tops"; `cargo-tops …` does not. Drop that one word so both forms parse.
fn subcommand_argv(mut args: Vec<std::ffi::OsString>) -> Vec<std::ffi::OsString> {
    if args.get(1).and_then(|arg| arg.to_str()) == Some("tops") {
        args.remove(1);
    }
    args
}

fn run() -> Result<ExitCode> {
    let cli = Cli::parse_from(subcommand_argv(std::env::args_os().collect()));
    match cli.command {
        Commands::Init(args) => {
            init(&args)?;
            Ok(ExitCode::SUCCESS)
        }
        Commands::Check { path } => {
            let cfg = load_config(&path)?;
            check(&cfg)?;
            println!(
                "ok: {} ({}) matches rust-tops {}",
                cfg.crate_.name, cfg.crate_.class, cfg.protocol.version
            );
            Ok(ExitCode::SUCCESS)
        }
        Commands::Gate {
            path,
            max_tier,
            dry_run,
        } => {
            let cfg = load_config(&path)?;
            check(&cfg)?;
            gate(&path, &cfg, max_tier, dry_run)
        }
        Commands::CrapBaseline { path } => {
            let cfg = load_config(&path)?;
            floors::record_baseline(&path, &cfg)?;
            Ok(ExitCode::SUCCESS)
        }
        Commands::Version => {
            print_version();
            Ok(ExitCode::SUCCESS)
        }
    }
}

/// `rust-tops.yaml` under `path`, typed, with a class profile, and valid
/// against `schema/rust-tops.schema.json`. Any of the three failing is a
/// `check` failure.
fn load_config(path: &Path) -> Result<TopsConfig> {
    let file = config_path(path);
    let raw = fs::read_to_string(&file)
        .with_context(|| format!("missing {}; run `cargo tops init`", file.display()))?;
    let cfg = parse_config(&file, &raw)?;
    profiles::profile_yaml(&cfg.crate_.class, &cfg.crate_.name)
        .with_context(|| format!("{}: crate.class", file.display()))?;
    let doc = facts::parse_yaml(&raw)
        .map_err(|err| anyhow!("invalid YAML: {}: {err}", file.display()))?;
    let schema: serde_json::Value =
        serde_json::from_str(profiles::SCHEMA_JSON).context("bundled rust-tops.schema.json")?;
    let errors = schema::validate(&schema, &doc);
    if !errors.is_empty() {
        return Err(anyhow!(
            "{} does not match schema/rust-tops.schema.json:\n  {}",
            file.display(),
            errors.join("\n  ")
        ));
    }
    Ok(cfg)
}

/// The typed decode alone, with YAML 1.2 booleans.
fn parse_config(file: &Path, raw: &str) -> Result<TopsConfig> {
    // YAML 1.2 booleans: only true/false. Unquoted yes/no/on/off/y/n stay strings.
    serde_saphyr::from_str_with_options(
        raw,
        serde_saphyr::options! {
            strict_booleans: true,
        },
    )
    .with_context(|| format!("invalid YAML: {}", file.display()))
}

fn config_path(dir: &Path) -> PathBuf {
    dir.join("rust-tops.yaml")
}

fn check(cfg: &TopsConfig) -> Result<()> {
    if cfg.protocol.id != "rust-tops" {
        return Err(anyhow!("protocol.id must be rust-tops"));
    }
    if cfg.protocol.version.is_empty() {
        return Err(anyhow!("protocol.version is required"));
    }
    if cfg.crate_.name.is_empty() {
        return Err(anyhow!("crate.name is required"));
    }
    if cfg
        .gates
        .crap
        .threshold_agent
        .is_some_and(|agent| agent < 1.0)
    {
        return Err(anyhow!("gates.crap.threshold_agent must be >= 1"));
    }
    if cfg.gates.mutation.unexplained_survivors_max != 0 && cfg.crate_.class == "library-core" {
        return Err(anyhow!(
            "library-core cannot raise unexplained_survivors_max above 0"
        ));
    }
    Ok(())
}

#[expect(clippy::too_many_lines, reason = "over the kit line threshold")]
fn init(args: &InitArgs) -> Result<()> {
    let (dir, class, name) = (
        args.path.as_path(),
        args.class.as_str(),
        args.name.as_deref(),
    );
    let (force, skills, laws) = (args.force, args.skills, args.laws);
    let fallback = dir_name(dir).unwrap_or_else(|| "unnamed".to_string());
    let rustc = lock_rustc(dir)?;
    let plan = init_meta::plan(&init_meta::Request {
        dir,
        name_flag: name,
        dir_name: &fallback,
        lock_rustc: &rustc,
        laws,
    })?;
    let crate_name = plan.name.as_str();
    let msrv = plan.settings.msrv.as_deref().unwrap_or(&rustc);
    // An unknown class fails here, before any directory or file is written.
    let yaml = with_init_exemptions(&profiles::profile_yaml_with(
        class,
        crate_name,
        &plan.settings,
    )?);

    // The directory named on the command line may sit behind a link; the
    // directories below it may not, and `write_file` makes those.
    fs::create_dir_all(dir).with_context(|| format!("create {}", dir.display()))?;
    write_file(dir, "rust-tops.yaml", &yaml, force)?;
    write_file(dir, "rust-tops.lock", profiles::LOCK_TEMPLATE, force)?;
    if plan.write_toolchain {
        write_file(
            dir,
            "rust-toolchain.toml",
            &profiles::rust_toolchain_toml(msrv),
            force,
        )?;
    }
    write_file(dir, "AGENTS.md", profiles::AGENTS_MD, force)?;
    write_file(
        dir,
        "RUST-TOPS.md",
        &profiles::adoption_record(&yaml),
        force,
    )?;
    write_file(dir, "clippy.toml", &profiles::clippy_toml(class)?, force)?;
    write_file(dir, ".config/nextest.toml", profiles::NEXTEST_TOML, force)?;
    write_file(dir, ".cargo/mutants.toml", profiles::MUTANTS_TOML, force)?;
    write_file(dir, ".cargo-crap.toml", profiles::CARGO_CRAP_TOML, force)?;
    write_file(dir, "deny.toml", profiles::DENY_TOML, force)?;
    write_file(
        dir,
        ".github/workflows/rust-tops.yml",
        &profiles::workflow_yml(class),
        force,
    )?;
    if laws {
        write_file(dir, laws::PACK, profiles::LAWS_BEND, force)?;
        write_file(dir, laws::LOCK, profiles::BEND_LOCK, force)?;
        if write_file(dir, laws::FETCH, profiles::FETCH_BEND_SH, force)? {
            make_executable(&dir.join(laws::FETCH))?;
        }
    }
    if skills {
        skills::emit(dir, force)?;
    }
    print_init_notes(&plan, &yaml);

    println!(
        "initialized rust-tops for {crate_name} ({class}) in {}",
        dir.display()
    );
    println!("next: cargo tops check && cargo tops gate");
    Ok(())
}

/// The `rustc` pin that stands in for an absent `rust-version`: the lock
/// already in `dir`, else the template `init` writes.
fn lock_rustc(dir: &Path) -> Result<String> {
    let there = fs::read_to_string(dir.join("rust-tops.lock")).ok();
    there
        .as_deref()
        .and_then(init_meta::lock_rustc)
        .or_else(|| init_meta::lock_rustc(profiles::LOCK_TEMPLATE))
        .ok_or_else(|| anyhow!("rust-tops.lock has no [tools] rustc"))
}

/// One line for each value `init` chose without the crate saying it.
fn print_init_notes(plan: &init_meta::Plan, yaml: &str) {
    for note in &plan.notes {
        println!("{note}");
    }
    let policy = facts::parse_yaml(yaml)
        .ok()
        .and_then(|doc| {
            doc.pointer("/crate/unsafe_policy")?
                .as_str()
                .map(str::to_string)
        })
        .unwrap_or_default();
    if let Some(note) = init_meta::unsafe_note(&plan.settings, &policy) {
        println!("{note}");
    }
    if !plan.write_toolchain {
        println!("note: rust-toolchain.toml not written; the crate or a directory above it, up to the workspace root, already pins a toolchain");
    }
}

/// The last path component of `dir`, which need not exist yet.
fn dir_name(dir: &Path) -> Option<String> {
    let full = dir
        .canonicalize()
        .or_else(|_| std::path::absolute(dir))
        .ok()?;
    full.file_name().map(|s| s.to_string_lossy().into_owned())
}

/// Writes `root/rel`. Returns whether the file was written (false when it
/// existed and `force` was off).
///
/// A symlink at the file is refused, with or without `force`, and so is a
/// dangling one: the write never follows a link. So is a link at any
/// directory between `root` and the file. `force` removes the old file and
/// writes a new one with `create_new`, which fails rather than follow a link
/// that appears in between.
///
/// [`Parent::open`] says how the directories are made and how far that
#[expect(
    clippy::fn_params_excessive_bools,
    reason = "already in this crate when the strict table was turned on"
)]
/// holds against a link swapped in while init runs.
pub(crate) fn write_file(
    root: &Path,
    rel: impl AsRef<Path>,
    contents: &str,
    force: bool,
) -> Result<bool> {
    let rel = rel.as_ref();
    let path = root.join(rel);
    let name = rel
        .file_name()
        .ok_or_else(|| anyhow!("refusing to write {}: no file name", path.display()))?;
    let parent = Parent::open(root, rel, &path)?;
    match parent.entry(name) {
        Entry::Symlink => {
            return Err(anyhow!(
                "refusing to write {}: it is a symlink, and init does not follow one",
                path.display()
            ));
        }
        Entry::Other if !force => {
            println!("skip existing {}", path.display());
            return Ok(false);
        }
        Entry::Other => parent
            .remove(name)
            .with_context(|| format!("remove {}", path.display()))?,
        // Nothing there, or nothing that can be read: `create_new` below
        // either makes the file or fails, and never follows a link.
        Entry::Absent => {}
    }
    let mut file = parent
        .create_new(name)
        .with_context(|| format!("create {}", path.display()))?;
    std::io::Write::write_all(&mut file, contents.as_bytes())
        .with_context(|| format!("write {}", path.display()))?;
    println!("wrote {}", path.display());
    Ok(true)
}

/// What is at a name in a [`Parent`], without following a link there.
enum Entry {
    Absent,
    Symlink,
    Other,
}

fn refused(path: &Path, dir: &Path) -> anyhow::Error {
    anyhow!(
        "refusing to write {}: {} is a symlink or not a directory, and init does not follow one",
        path.display(),
        dir.display()
    )
}

/// The directory a [`write_file`] writes into, held open on Unix.
#[cfg(unix)]
struct Parent(std::os::fd::OwnedFd);

#[cfg(unix)]
impl Parent {
    /// Opens `root`, then each directory between it and `rel`'s file,
    /// making the ones that are missing.
    ///
    /// `root` is opened by path, so a link at `root` itself is followed.
    /// Below it nothing is looked up by path again: each directory is made
    /// with `mkdirat` and opened with `O_NOFOLLOW` relative to the one
    /// above, and the file is made relative to the last. A link at any of
    /// those names, there before the walk or swapped in during it, fails
    /// the open and the write is refused. So does a name that is gone again
    /// by the time it is opened.
    fn open(root: &Path, rel: &Path, path: &Path) -> Result<Self> {
        use rustix::fs::{mkdirat, openat, Mode, OFlags, CWD};
        use rustix::io::Errno;
        // `union`, not `|`: on disjoint bits a mutated `^` is the same set,
        // a mutant no test can catch.
        let flags = OFlags::RDONLY
            .union(OFlags::DIRECTORY)
            .union(OFlags::CLOEXEC);
        let mut fd = openat(CWD, root, flags, Mode::empty())
            .with_context(|| format!("open {}", root.display()))?;
        let mut dir = root.to_path_buf();
        for part in rel.parent().map(Path::components).into_iter().flatten() {
            let name = part.as_os_str();
            dir.push(name);
            match mkdirat(&fd, name, Mode::from_raw_mode(0o777)) {
                Ok(()) | Err(Errno::EXIST) => {}
                Err(err) => return Err(err).with_context(|| format!("create {}", dir.display())),
            }
            fd = match openat(&fd, name, flags.union(OFlags::NOFOLLOW), Mode::empty()) {
                Ok(next) => next,
                Err(Errno::LOOP | Errno::NOTDIR | Errno::NOENT) => return Err(refused(path, &dir)),
                Err(err) => return Err(err).with_context(|| format!("open {}", dir.display())),
            };
        }
        Ok(Self(fd))
    }

    fn entry(&self, name: &std::ffi::OsStr) -> Entry {
        use rustix::fs::{statat, AtFlags, FileType};
        match statat(&self.0, name, AtFlags::SYMLINK_NOFOLLOW) {
            Ok(stat) if FileType::from_raw_mode(stat.st_mode) == FileType::Symlink => {
                Entry::Symlink
            }
            Ok(_) => Entry::Other,
            Err(_) => Entry::Absent,
        }
    }

    fn remove(&self, name: &std::ffi::OsStr) -> std::io::Result<()> {
        rustix::fs::unlinkat(&self.0, name, rustix::fs::AtFlags::empty()).map_err(Into::into)
    }

    fn create_new(&self, name: &std::ffi::OsStr) -> std::io::Result<fs::File> {
        use rustix::fs::{openat, Mode, OFlags};
        let flags = OFlags::WRONLY
            .union(OFlags::CREATE)
            .union(OFlags::EXCL)
            .union(OFlags::NOFOLLOW)
            .union(OFlags::CLOEXEC);
        let fd = openat(&self.0, name, flags, Mode::from_raw_mode(0o666))?;
        Ok(fs::File::from(fd))
    }
}

/// The directory a [`write_file`] writes into, as a path off Unix.
#[cfg(not(unix))]
type Parent = PathParent;

/// The path walk. It is built on Unix too, so its tests run there.
#[cfg_attr(all(unix, not(test)), expect(dead_code, reason = "the walk off Unix"))]
struct PathParent(PathBuf);

#[cfg_attr(all(unix, not(test)), expect(dead_code, reason = "the walk off Unix"))]
impl PathParent {
    /// Each directory between `root` and the file is made with `create_dir`
    /// before anything looks at it, and a name it could not make must be a
    /// real directory. This walk goes by path, so the window is still open:
    /// a directory removed and replaced by a link after it was checked and
    /// before the file is created is followed.
    fn open(root: &Path, rel: &Path, path: &Path) -> Result<Self> {
        let mut dir = root.to_path_buf();
        for part in rel.parent().map(Path::components).into_iter().flatten() {
            dir.push(part);
            if fs::create_dir(&dir).is_err()
                && !fs::symlink_metadata(&dir).is_ok_and(|meta| meta.is_dir())
            {
                return Err(refused(path, &dir));
            }
        }
        Ok(Self(dir))
    }

    fn entry(&self, name: &std::ffi::OsStr) -> Entry {
        match fs::symlink_metadata(self.0.join(name)) {
            Ok(meta) if meta.file_type().is_symlink() => Entry::Symlink,
            Ok(_) => Entry::Other,
            Err(_) => Entry::Absent,
        }
    }

    fn remove(&self, name: &std::ffi::OsStr) -> std::io::Result<()> {
        fs::remove_file(self.0.join(name))
    }

    fn create_new(&self, name: &std::ffi::OsStr) -> std::io::Result<fs::File> {
        fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(self.0.join(name))
    }
}

#[cfg(unix)]
fn make_executable(path: &Path) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;
    fs::set_permissions(path, fs::Permissions::from_mode(0o755))
        .with_context(|| format!("chmod {}", path.display()))
}

#[cfg(not(unix))]
fn make_executable(_path: &Path) -> Result<()> {
    Ok(())
}

/// What a layer runs.
enum Plan {
    /// Each chain runs on its own. Inside a chain only a pass continues.
    Chains(Vec<Vec<Step>>),
    /// The diff, judged, then `cargo mutants` on a non-empty diff.
    Mutation(MutationPlan),
}

#[expect(
    clippy::fn_params_excessive_bools,
    reason = "already in this crate when the strict table was turned on"
)]
#[expect(clippy::too_many_lines, reason = "over the kit line threshold")]
fn gate(dir: &Path, cfg: &TopsConfig, max_tier: u8, dry_run: bool) -> Result<ExitCode> {
    // A gate resolves `cargo metadata` afresh, once per project.
    laws::begin_gate();
    let enabled: Vec<&config::Layer> = cfg
        .layers
        .iter()
        .filter(|l| l.enabled && l.tier.unwrap_or(0) <= max_tier)
        .collect();

    if enabled.is_empty() {
        return Err(anyhow!("no enabled layers at or below tier {max_tier}"));
    }

    println!(
        "rust-tops gate: {} ({}) max-tier={max_tier}",
        cfg.crate_.name, cfg.crate_.class
    );

    // The laws layer reads the reports the other layers write, so it runs
    // last. Its judge is fetched first: a missing Bend fails before work.
    let laws_on = enabled.iter().any(|l| l.id == "laws");
    let judge = if laws_on && !dry_run {
        match laws::ensure_bend(dir) {
            Ok(judge) => Some(judge),
            Err(msg) => {
                println!("\nfail: {msg}");
                return Ok(ExitCode::from(1));
            }
        }
    } else {
        None
    };

    // The laws report, the mutation diff, the LCOV and the CRAP report share
    // one directory, which a nonempty `CARGO_TARGET_DIR` moves. Steps run
    // with `dir` as the working directory, so it must not be relative to the
    // caller's.
    let report = std::path::absolute(laws::artifact_dir(dir))
        .with_context(|| format!("resolve the report dir of {}", dir.display()))?;
    // Enabled layers above the range are not judged, so they cannot fail
    // the gate. Name them so the run says what it left out.
    let above: Vec<&str> = cfg
        .layers
        .iter()
        .filter(|l| l.enabled && l.tier.unwrap_or(0) > max_tier)
        .map(|l| l.id.as_str())
        .collect();
    if !above.is_empty() {
        println!(
            "above --max-tier {max_tier}, not judged: {}",
            above.join(", ")
        );
    }
    // A layer turned off still shows in the run: it is not run, not omitted.
    let disabled: Vec<&str> = cfg
        .layers
        .iter()
        .filter(|l| !l.enabled)
        .map(|l| l.id.as_str())
        .collect();
    if !disabled.is_empty() {
        println!("not run (enabled: false): {}", disabled.join(", "));
    }

    let started = SystemTime::now();
    let mut runs: Vec<Run> = Vec::new();
    // Enabled, in range, and no plan: a skip is never a pass (H-37).
    let mut not_run: Vec<String> = Vec::new();
    let mut listed = 0;
    let tests = TestCommands::new(&enabled, &report);
    let crap_pin = if enabled.iter().any(|layer| layer.id == "crap") {
        tool_pin(dir, "cargo-crap")?
    } else {
        None
    };
    // Layers that add no command, each with the layer whose run it takes.
    let mut shared: Vec<(&str, &str)> = Vec::new();
    for layer in enabled.iter().filter(|l| l.id != "laws") {
        if let Some(from) = tests.shared_from(&layer.id) {
            println!(
                "\n==> [{}] no command of its own: it takes the {from} run's outcome",
                layer.id
            );
            shared.push((&layer.id, from));
            continue;
        }
        let Some(plan) = plan_for(layer, &report) else {
            let tier = layer.tier.unwrap_or(0);
            println!(
                "\nNotRun [{}]: enabled at tier {tier} (<= --max-tier {max_tier}) and no command is mapped",
                layer.id
            );
            not_run.push(format!(
                "{}: NotRun (enabled at tier {tier}, no command mapped; disable it or lower --max-tier)",
                layer.id
            ));
            continue;
        };
        let chains = match plan {
            Plan::Chains(chains) => chains,
            Plan::Mutation(plan) => {
                println!(
                    "\n==> [{}] {} && {}",
                    layer.id,
                    plan.diff.display(),
                    plan.mutants.display()
                );
                listed += 1;
                if !dry_run {
                    runs.push(run_mutation(dir, &layer.id, &plan)?);
                }
                continue;
            }
        };
        let pin = crap_pin.as_deref().filter(|_| layer.id == "crap");
        for chain in chains {
            let mut shown: Vec<String> = pin.map(|_| CRAP_VERSION.join(" ")).into_iter().collect();
            shown.extend(chain.iter().map(Step::display));
            println!("\n==> [{}] {}", layer.id, shown.join(" && "));
            if let Some(pin) = pin {
                println!(
                    "    `{}` must print `cargo-crap {pin}`, the {TOOL_LOCK} pin",
                    CRAP_VERSION.join(" ")
                );
            }
            listed += 1;
            if !dry_run {
                runs.push(match pin {
                    Some(pin) => run_pinned_crap(dir, &layer.id, &chain, pin)?,
                    None => run_chain(dir, &layer.id, &chain)?,
                });
            }
        }
    }

    let total = fs::read_to_string(dir.join(laws::PACK))
        .map(|pack| facts::pack_law_count(&pack))
        .unwrap_or_else(|_| facts::pack_law_count(profiles::LAWS_BEND));

    if dry_run {
        if laws_on {
            println!(
                "\n==> [laws] sh {} (only if Bend from {} is not installed)",
                laws::FETCH,
                laws::LOCK
            );
            println!("\n==> [laws] bend {}/cases.bend", laws::report_display(dir));
        }
        println!();
        println!("laws evaluated: 0/{total} (dry run: bend did not run)");
        // Nothing ran, so the count is short for every layer: a dry run
        // lists the commands and is never a pass.
        let (count, _) = command_count(0, &enabled, &[], false, max_tier, &shared);
        println!("{count}");
        if not_run.is_empty() {
            println!("dry-run: {listed} command(s) listed, 0 run; a dry run is not a pass");
        } else {
            println!(
                "dry-run: {listed} command(s) listed, 0 run; {} layer(s) NotRun, so the gate would fail",
                not_run.len()
            );
        }
        for n in not_run.iter().chain([&count]) {
            println!("  - {n}");
        }
        return Ok(ExitCode::from(1));
    }

    for &(layer, from) in &shared {
        let run = tests.take(layer, from, &runs);
        println!("[{layer}] took the {from} run's outcome: {}", run.outcome);
        runs.push(run);
    }
    let mut failed: Vec<String> = not_run;
    failed.extend(runs.iter().filter_map(|run| {
        match shared.iter().find(|(layer, _)| *layer == run.layer) {
            Some((_, from)) => run.outcome.fails_the_gate().then(|| {
                format!(
                    "{}: no command of its own; the {from} run it took is {}",
                    run.layer, run.outcome
                )
            }),
            None => run_failure(run),
        }
    }));
    // The coverage floors and the CRAP ratchet, on the reports just written.
    failed.extend(floors::judge(dir, cfg, &runs, &report));
    let mut bend_ran = false;
    let evaluated = match judge {
        Some(judge) => {
            // A report from an earlier gate must not be counted for this one.
            let cases = report.join("cases.json");
            match fs::remove_file(&cases) {
                Err(err) if err.kind() != std::io::ErrorKind::NotFound => {
                    return Err(err).with_context(|| format!("remove {}", cases.display()));
                }
                _ => {}
            }
            let ctx = facts::Ctx {
                dir,
                cfg,
                yaml: fs::read_to_string(config_path(dir))
                    .ok()
                    .and_then(|raw| facts::parse_yaml(&raw).ok())
                    .unwrap_or_default(),
                max_tier,
                runs: &runs,
                artifacts: &report,
                started,
                workflows: facts::load_workflows(dir),
            };
            failed.extend(laws::run(&judge, &ctx)?);
            // `cases.json` was removed above, so one here means bend ran.
            let report = laws_evaluated(&cases);
            bend_ran = report.is_some();
            let (checked, of) = report.unwrap_or((0, total));
            format!("laws evaluated: {checked}/{of}")
        }
        None => format!("laws evaluated: 0/{total} (laws layer off)"),
    };

    // A layer that took another's run ran no command of its own.
    let ran = runs.len() - shared.len();
    let vacuous = runs
        .iter()
        .filter(|run| run.outcome == Outcome::Vacuous)
        .count();
    let (count, short) = command_count(
        ran_commands(&runs, bend_ran, &shared),
        &enabled,
        &runs,
        bend_ran,
        max_tier,
        &shared,
    );
    if short {
        failed.push(count.clone());
    }
    println!();
    println!("{evaluated}");
    println!("{count}");
    if failed.is_empty() {
        if vacuous == 0 {
            println!("ok: {ran} command(s) passed");
        } else {
            println!(
                "ok: {} command(s) passed, {vacuous} vacuous (counted for no law)",
                ran - vacuous
            );
        }
        Ok(ExitCode::SUCCESS)
    } else {
        println!("fail: {} problem(s), {ran} command(s) run", failed.len());
        for f in &failed {
            println!("  - {f}");
        }
        Ok(ExitCode::from(1))
    }
}

/// Commands that ran: each run whose tool was found, and `bend` once when it
#[expect(
    clippy::fn_params_excessive_bools,
    reason = "already in this crate when the strict table was turned on"
)]
/// judged the laws layer. A layer that took another layer's run ran none.
fn ran_commands(runs: &[Run], bend_ran: bool, shared: &[(&str, &str)]) -> usize {
    runs.iter()
        .filter(|run| run.outcome != Outcome::ToolMissing)
        .filter(|run| !shared.iter().any(|(layer, _)| *layer == run.layer))
        .count()
        + usize::from(bend_ran)
}

/// The test commands of the enabled layers in range, which decide whether a
/// layer runs its own or takes another layer's run ([`credit`]). A layer
/// that is off or above the range has no command here, so nothing takes its
/// run.
struct TestCommands {
    coverage_in_range: bool,
    coverage: Vec<String>,
    unit: Vec<String>,
    integration: Vec<String>,
}

impl TestCommands {
    fn new(enabled: &[&config::Layer], report: &Path) -> Self {
        let argv = |id: &str| {
            enabled
                .iter()
                .find(|layer| layer.id == id)
                .and_then(|layer| plan_for(layer, report))
                .and_then(|plan| match plan {
                    Plan::Chains(chains) => chains
                        .into_iter()
                        .flatten()
                        .find(|step| step.kind == Kind::Tool),
                    Plan::Mutation(_) => None,
                })
                .map(|step| step.argv)
                .unwrap_or_default()
        };
        Self {
            coverage_in_range: enabled.iter().any(|layer| layer.id == "coverage"),
            coverage: argv("coverage"),
            unit: argv("unit"),
            integration: argv("integration"),
        }
    }

    fn query<'a>(&'a self, layer: &'a str, runs: &[Run]) -> credit::Query<'a> {
        let outcome = |id: &str| {
            runs.iter()
                .find(|run| run.layer == id)
                .map(|run| match run.outcome {
                    Outcome::Passed => credit::Outcome::Passed,
                    Outcome::ToolMissing => credit::Outcome::ToolMissing,
                    // A test run is never Vacuous; if one were, it is no pass.
                    Outcome::Failed | Outcome::Vacuous => credit::Outcome::Failed,
                })
        };
        credit::Query {
            layer,
            coverage_in_range: self.coverage_in_range,
            coverage_argv: &self.coverage,
            coverage_outcome: outcome("coverage"),
            unit_argv: &self.unit,
            integration_argv: &self.integration,
            unit_outcome: outcome("unit"),
        }
    }

    /// The layer whose run `layer` takes, or `None` when it runs its own.
    /// Credit from coverage is only possible in range, and from `unit` only
    /// out of it, so the range names the source.
    fn shared_from(&self, layer: &str) -> Option<&'static str> {
        if credit::runs_itself(&self.query(layer, &[])) {
            None
        } else if self.coverage_in_range {
            Some("coverage")
        } else {
            Some("unit")
        }
    }

    /// The run `layer` records once `from` has run: `from`'s command, and the
    /// outcome [`credit::recorded_outcome`] gives. A source that did not run
    /// is not a pass.
    fn take(&self, layer: &str, from: &str, runs: &[Run]) -> Run {
        let outcome = match credit::recorded_outcome(&self.query(layer, runs)) {
            Some(credit::Outcome::Passed) => Outcome::Passed,
            Some(credit::Outcome::ToolMissing) => Outcome::ToolMissing,
            Some(credit::Outcome::Failed) | None => Outcome::Failed,
        };
        let source = runs.iter().find(|run| run.layer == from);
        Run {
            layer: layer.to_string(),
            cmd: source.map(|run| run.cmd.clone()).unwrap_or_default(),
            step: source.map(|run| run.step.clone()).unwrap_or_default(),
            outcome,
        }
    }
}

/// The `count:` line, and whether it is short: an enabled layer in range
/// ran no command (NotRun, a missing tool, or a dry run). Every run belongs
/// to one enabled layer, so fewer commands than layers is always short. A
#[expect(
    clippy::fn_params_excessive_bools,
    reason = "already in this crate when the strict table was turned on"
)]
/// short count fails the gate.
fn command_count(
    ran: usize,
    enabled: &[&config::Layer],
    runs: &[Run],
    bend_ran: bool,
    max_tier: u8,
    shared: &[(&str, &str)],
) -> (String, bool) {
    let none: Vec<&str> = enabled
        .iter()
        .map(|layer| layer.id.as_str())
        .filter(|id| match *id {
            "laws" => !bend_ran,
            _ => !runs
                .iter()
                .any(|run| run.layer == *id && run.outcome != Outcome::ToolMissing),
        })
        .collect();
    let mut line = format!(
        "count: {ran} command(s) ran; {} of {} enabled layer(s) at tier <= {max_tier} ran one",
        enabled.len() - none.len(),
        enabled.len()
    );
    // A layer that took another's run is counted when that run happened,
    // with the run named.
    for from in ["coverage", "unit"] {
        let took: Vec<&str> = shared
            .iter()
            .filter(|(_, source)| *source == from)
            .map(|(layer, _)| *layer)
            .collect();
        if !took.is_empty() {
            #[expect(
                clippy::let_underscore_must_use,
                reason = "already in this crate when the strict table was turned on"
            )]
            let _ = write!(line, "; {} took the {from} run", took.join(", "));
        }
    }
    if none.is_empty() {
        (line, false)
    } else {
        (format!("{line}; none ran for: {}", none.join(", ")), true)
    }
}

/// `(checked, total)` from the `cases.json` the laws layer just wrote. A law
/// is evaluated when `bend` got a case for it.
fn laws_evaluated(cases: &Path) -> Option<(usize, usize)> {
    let raw = fs::read_to_string(cases).ok()?;
    let report: serde_json::Value = serde_json::from_str(&raw).ok()?;
    #[expect(
        clippy::indexing_slicing,
        reason = "already in this crate when the strict table was turned on"
    )]
    let laws = report["laws"].as_array()?;
    let checked = laws.iter().filter(|law| law["status"] == "checked").count();
    Some((checked, laws.len()))
}

fn run_failure(run: &Run) -> Option<String> {
    match run.outcome {
        Outcome::Passed | Outcome::Vacuous => None,
        Outcome::Failed => Some(format!("{}: `{}` failed", run.layer, run.step)),
        Outcome::ToolMissing => {
            let missing = steps::MissingTool {
                binary: steps::tool_binary(std::slice::from_ref(&run.step))
                    .unwrap_or_else(|| run.step.clone()),
            };
            Some(format!("{}: {missing}", run.layer))
        }
    }
}

/// The steps that ran, joined, and the outcome of the last one.
fn finish(layer: &str, ran: &[String], outcome: Outcome) -> Run {
    Run {
        layer: layer.to_string(),
        cmd: ran.join(" && "),
        step: ran.last().cloned().unwrap_or_default(),
        outcome,
    }
}

fn run_chain(dir: &Path, layer: &str, chain: &[Step]) -> Result<Run> {
    let mut ran = Vec::new();
    let mut outcome = Outcome::Passed;
    for step in chain {
        ran.push(step.display());
        outcome = run_step(dir, step)?;
        if !steps::chain_continues(outcome) {
            break;
        }
    }
    Ok(finish(layer, &ran, outcome))
}

/// The tool pins a repo keeps at its root, beside `rust-tops.yaml`. `init`
/// does not write one. Adopters copy `config/rust-tops.lock.example`.
pub(crate) const TOOL_LOCK: &str = "rust-tops.lock";

/// The command whose first line must name the pinned cargo-crap.
const CRAP_VERSION: [&str; 3] = ["cargo", "crap", "--version"];

/// The `[tools]` version that `dir/rust-tops.lock` pins for `tool`. No lock,
/// or no line for `tool`, is `None`. A lock that does not parse is an error.
fn tool_pin(dir: &Path, tool: &str) -> Result<Option<String>> {
    let path = dir.join(TOOL_LOCK);
    let raw = match fs::read_to_string(&path) {
        Ok(raw) => raw,
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(err) => return Err(err).with_context(|| format!("read {}", path.display())),
    };
    let lock: toml::Table =
        toml::from_str(&raw).with_context(|| format!("parse {}", path.display()))?;
    Ok(lock
        .get("tools")
        .and_then(|tools| tools.get(tool))
        .and_then(toml::Value::as_str)
        .map(str::to_string))
}

/// The crap chain, run only when `cargo crap --version` prints
/// `cargo-crap <pin>`. It asks the binary that `cargo crap` runs, so another
/// version earlier on `PATH` fails the layer before a report is written. A
/// cargo-crap that is not there is left to the chain, which reports it
/// missing.
fn run_pinned_crap(dir: &Path, layer: &str, chain: &[Step], pin: &str) -> Result<Run> {
    let version = CRAP_VERSION.join(" ");
    let argv = CRAP_VERSION.map(String::from);
    if steps::check_tool(&argv).is_err() {
        return run_chain(dir, layer, chain);
    }
    let output = Command::new(CRAP_VERSION[0])
        .args(&CRAP_VERSION[1..])
        .current_dir(dir)
        .output()
        .with_context(|| format!("spawn: {version}"))?;
    let stdout = String::from_utf8_lossy(&output.stdout);
    let printed = stdout.lines().next().unwrap_or_default().trim();
    let want = format!("cargo-crap {pin}");
    if !output.status.success() || printed != want {
        println!(
            "Failed [{layer}]: `{version}` printed `{printed}`, not `{want}` from {TOOL_LOCK}"
        );
        return Ok(finish(layer, &[version], Outcome::Failed));
    }
    let mut run = run_chain(dir, layer, chain)?;
    run.cmd = format!("{version} && {}", run.cmd);
    Ok(run)
}

/// `cargo-mutants` is looked up before the diff, so an empty diff cannot
/// hide a missing tool. A failed diff is `Failed`; nothing waives it. An
/// empty one is `Vacuous` and `cargo mutants` does not run.
fn run_mutation(dir: &Path, layer: &str, plan: &MutationPlan) -> Result<Run> {
    if let Err(missing) = steps::check_tool(&plan.mutants.argv) {
        println!("{missing}");
        return Ok(finish(
            layer,
            &[plan.mutants.display()],
            Outcome::ToolMissing,
        ));
    }
    let mut ran = vec![plan.diff.display()];
    let diff_ok = run_step(dir, &plan.diff)? == Outcome::Passed;
    let diff = steps::judge_diff(diff_ok, &plan.diff_path);
    let Some(mutants) = plan.after_diff(diff) else {
        if diff == Outcome::Vacuous && has_origin_main(dir) {
            println!(
                "Vacuous: the diff from `git merge-base origin/main HEAD` is empty; \
                 cargo mutants did not run and no law counts this layer"
            );
        } else if diff == Outcome::Vacuous {
            println!(
                "Vacuous: no origin/main; the mutation diff is empty; \
                 cargo mutants did not run and no law counts this layer"
            );
        } else {
            println!("Failed: the mutation diff did not succeed; a failed diff is never waived");
        }
        return Ok(finish(layer, &ran, diff));
    };
    ran.push(mutants.display());
    let outcome = run_step(dir, mutants)?;
    Ok(finish(layer, &ran, outcome))
}

/// Whether `refs/remotes/origin/main` exists, for the Vacuous wording only.
fn has_origin_main(dir: &Path) -> bool {
    Command::new("git")
        .args([
            "rev-parse",
            "--verify",
            "--quiet",
            "refs/remotes/origin/main",
        ])
        .current_dir(dir)
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .status()
        .is_ok_and(|status| status.success())
}

fn run_step(dir: &Path, step: &Step) -> Result<Outcome> {
    match step.kind {
        Kind::Artifact => {
            return Ok(step.artifact_path().map_or(Outcome::Failed, |path| {
                steps::artifact_outcome(&dir.join(path))
            }));
        }
        Kind::Tool => {
            if let Err(missing) = steps::check_tool(&step.argv) {
                println!("{missing}");
                return Ok(Outcome::ToolMissing);
            }
        }
        Kind::Command => {}
    }
    let Some((program, args)) = step.program() else {
        return Ok(Outcome::Failed);
    };
    match Command::new(program).args(args).current_dir(dir).status() {
        Ok(status) if status.success() => Ok(Outcome::Passed),
        Ok(_) => Ok(Outcome::Failed),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
            println!(
                "{}",
                steps::MissingTool {
                    binary: program.to_string()
                }
            );
            Ok(Outcome::ToolMissing)
        }
        Err(err) => Err(err).with_context(|| format!("spawn: {}", step.display())),
    }
}

fn tool(argv: &[&str]) -> Vec<Step> {
    vec![Step::tool(argv.iter().copied())]
}

/// `report` is [`laws::artifact_dir`]: the coverage LCOV, the CRAP report
/// and the mutation diff are written there.
#[expect(clippy::too_many_lines, reason = "over the kit line threshold")]
fn plan_for(layer: &config::Layer, report: &Path) -> Option<Plan> {
    let chains = match layer.id.as_str() {
        "static-lint" => vec![
            tool(&["cargo", "fmt", "--all", "--", "--check"]),
            tool(&[
                "cargo",
                "clippy",
                "--workspace",
                "--all-targets",
                "--all-features",
                "--",
                "-D",
                "warnings",
            ]),
            // `security-advisory` runs `advisories`; each kind runs once.
            tool(&["cargo", "deny", "check", "bans", "licenses", "sources"]),
        ],
        "unit" | "integration" => vec![tool(&[
            "cargo",
            "nextest",
            "run",
            "--workspace",
            "--all-features",
            "--profile",
            "ci",
        ])],
        "doc" => vec![tool(&[
            "cargo",
            "test",
            "--doc",
            "--workspace",
            "--all-features",
        ])],
        "coverage" => vec![steps::coverage_steps(report)],
        "crap" => {
            let lcov = report.join(steps::LCOV).display().to_string();
            let crap = report.join(steps::CRAP).display().to_string();
            vec![vec![
                Step::tool([
                    "cargo",
                    "crap",
                    "--lcov",
                    lcov.as_str(),
                    "--format",
                    "json",
                    "--output",
                    crap.as_str(),
                ]),
                Step::artifact(crap),
            ]]
        }
        "mutation" => return Some(Plan::Mutation(MutationPlan::new(report))),
        "property" => vec![tool(&[
            "cargo",
            "nextest",
            "run",
            "--workspace",
            "--all-features",
            "--profile",
            "ci",
            "--no-tests=fail",
            "property",
        ])],
        "security-advisory" => vec![tool(&["cargo", "deny", "check", "advisories"])],
        "formal-kani" => vec![tool(&["cargo", "kani", "--workspace"])],
        _ => return None,
    };
    Some(Plan::Chains(chains))
}

/// The profile YAML with its `exemptions:` block replaced by
/// [`steps::init_exemptions`], which is empty. An exemption is a dated choice
/// the adopter makes, never one `init` makes for them.
#[expect(clippy::too_many_lines, reason = "over the kit line threshold")]
fn with_init_exemptions(yaml: &str) -> String {
    let mut block = String::new();
    match steps::init_exemptions() {
        [] => block.push_str("exemptions: []\n"),
        entries => {
            block.push_str("exemptions:\n");
            for entry in entries {
                #[expect(
                    clippy::let_underscore_must_use,
                    reason = "already in this crate when the strict table was turned on"
                )]
                let _ = writeln!(
                    block,
                    "  - {{ layer: {}, expires: \"{}\" }}",
                    entry.layer, entry.expires
                );
            }
        }
    }
    let mut out = String::new();
    let mut placed = false;
    let mut skipping = false;
    for line in yaml.lines() {
        if skipping {
            let inside = line.trim().is_empty() || line.starts_with([' ', '\t', '-']);
            if inside {
                continue;
            }
            skipping = false;
        }
        if line.split('#').next().map(str::trim_end) == Some("exemptions:") {
            out.push_str(&block);
            placed = true;
            skipping = true;
            continue;
        }
        out.push_str(line);
        out.push('\n');
    }
    if !placed {
        out.push_str(&block);
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Every file in `profiles/`.
    const PROFILE_CLASSES: [&str; 8] = [
        "library-core",
        "parser-codec",
        "no-std-embedded",
        "unsafe-kernel",
        "binary-cli",
        "library-util",
        "ffi-bridge",
        "workspace-root",
    ];

    fn init_yaml(class: &str) -> String {
        with_init_exemptions(&profiles::profile_yaml(class, "x").expect("profile merges"))
    }

    fn layer(id: &str) -> config::Layer {
        serde_saphyr::from_str(&format!(
            "{{ id: {id}, enabled: true, tier: 0, runner: x }}"
        ))
        .expect("layer")
    }

    fn chains(id: &str) -> Vec<Vec<Step>> {
        match plan_for(&layer(id), Path::new("/r")) {
            Some(Plan::Chains(chains)) => chains,
            _ => panic!("{id} has no chains"),
        }
    }

    fn tool_step(chain: &[Step]) -> &Step {
        chain
            .iter()
            .find(|step| step.kind == Kind::Tool)
            .expect("a tool step")
    }

    /// Cargo passes the subcommand name; the bare binary does not. The
    /// `cargo_argv` journey runs whichever `cargo-tops` Cargo finds first,
    /// so this rule has its own oracle here.
    #[test]
    fn cargo_s_tops_word_is_dropped_and_nothing_else_is() {
        let argv = |words: &[&str]| -> Vec<std::ffi::OsString> {
            words.iter().map(std::ffi::OsString::from).collect()
        };
        let cases: [(&[&str], &[&str]); 4] = [
            (&["cargo-tops", "tops", "gate"], &["cargo-tops", "gate"]),
            (
                &["cargo-tops", "gate", "tops"],
                &["cargo-tops", "gate", "tops"],
            ),
            (&["cargo-tops", "check"], &["cargo-tops", "check"]),
            (&["cargo-tops"], &["cargo-tops"]),
        ];
        for (given, want) in cases {
            assert_eq!(subcommand_argv(argv(given)), argv(want), "{given:?}");
        }
    }

    #[test]
    fn the_g0_strings_name_the_tool_they_run() {
        let coverage = "mkdir -p target/coverage && cargo llvm-cov nextest --workspace --all-features --profile ci --lcov --output-path target/coverage/lcov.info && test -s target/coverage/lcov.info";
        let mutation = "sh -c 'git diff origin/main...HEAD --unified=0 > /tmp/rust-tops.diff; cargo mutants --in-diff /tmp/rust-tops.diff -j 2'";
        assert_eq!(
            steps::tool_binary(&[coverage.to_string()]).as_deref(),
            Some("cargo-llvm-cov")
        );
        assert_eq!(
            steps::tool_binary(&[mutation.to_string()]).as_deref(),
            Some("cargo-mutants")
        );
    }

    #[test]
    fn coverage_and_crap_check_their_own_binary() {
        let coverage = chains("coverage");
        assert_eq!(coverage.len(), 1);
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let tops_idx_1285_44 = coverage[0].clone();
        assert_eq!(
            steps::tool_binary(&tool_step(&tops_idx_1285_44).argv).as_deref(),
            Some("cargo-llvm-cov")
        );
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let tops_idx_1293_13 = coverage[0].clone();
        assert_eq!(
            tops_idx_1293_13.last().map(|step| step.kind),
            Some(Kind::Artifact)
        );
        let crap = chains("crap");
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let tops_idx_1302_44 = crap[0].clone();
        assert_eq!(
            steps::tool_binary(&tool_step(&tops_idx_1302_44).argv).as_deref(),
            Some("cargo-crap")
        );
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let tops_idx_1310_13 = crap[0].clone();
        assert_eq!(
            tops_idx_1310_13.last().and_then(Step::artifact_path),
            Some(Path::new("/r/crap.json"))
        );
    }

    #[test]
    fn coverage_and_crap_write_under_the_artifact_dir() {
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let coverage: Vec<String> = chains("coverage")[0].iter().map(Step::display).collect();
        assert_eq!(
            coverage,
            [
                "mkdir -p /r",
                "cargo llvm-cov nextest --workspace --all-features --profile ci --lcov --output-path /r/lcov.info",
                "test -s /r/lcov.info",
                "cargo llvm-cov report --json --summary-only --output-path /r/coverage.json",
                "test -s /r/coverage.json",
            ]
        );
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let crap: Vec<String> = chains("crap")[0].iter().map(Step::display).collect();
        assert_eq!(
            crap,
            [
                "cargo crap --lcov /r/lcov.info --format json --output /r/crap.json",
                "test -s /r/crap.json",
            ]
        );
    }

    fn argv(words: &str) -> Vec<String> {
        words.split_whitespace().map(str::to_string).collect()
    }

    const COVERAGE: &str = "cargo llvm-cov nextest --workspace --all-features --profile ci --lcov";
    const UNIT: &str = "cargo nextest run --workspace --all-features --profile ci";

    #[expect(
        clippy::fn_params_excessive_bools,
        reason = "already in this crate when the strict table was turned on"
    )]
    fn tests_with(in_range: bool, coverage: &str, integration: &str) -> TestCommands {
        TestCommands {
            coverage_in_range: in_range,
            coverage: argv(coverage),
            unit: argv(UNIT),
            integration: argv(integration),
        }
    }

    fn run(layer: &str, outcome: Outcome) -> Run {
        Run {
            layer: layer.to_string(),
            cmd: format!("{layer} cmd"),
            step: format!("{layer} step"),
            outcome,
        }
    }

    #[test]
    fn the_shipped_test_commands_share_one_coverage_run() {
        let layers: Vec<config::Layer> = ["unit", "integration", "coverage", "property"]
            .into_iter()
            .map(layer)
            .collect();
        let enabled: Vec<&config::Layer> = layers.iter().collect();
        let tests = TestCommands::new(&enabled, Path::new("/r"));
        assert!(tests.coverage_in_range);
        assert!(
            credit::coverage_grants_credit(&tests.coverage),
            "{:?}",
            tests.coverage
        );
        assert_eq!(tests.unit, tests.integration);
        for id in ["unit", "integration", "property"] {
            assert_eq!(tests.shared_from(id), Some("coverage"), "{id}");
        }
        for id in ["coverage", "doc", "static-lint", "crap"] {
            assert_eq!(tests.shared_from(id), None, "{id}");
        }
        // Out of range, nothing names a coverage command and none is shared.
        let unit_only: Vec<&config::Layer> = enabled
            .iter()
            .copied()
            .filter(|l| l.id != "coverage")
            .collect();
        let tests = TestCommands::new(&unit_only, Path::new("/r"));
        assert!(!tests.coverage_in_range);
        assert!(tests.coverage.is_empty());
        assert_eq!(tests.shared_from("integration"), Some("unit"));
        assert_eq!(tests.shared_from("unit"), None);
        assert_eq!(tests.shared_from("property"), None);
    }

    #[test]
    fn a_coverage_command_missing_a_flag_shares_nothing() {
        for missing in ["--workspace", "--all-features", "--profile ci"] {
            let coverage = COVERAGE.replace(&format!(" {missing}"), "");
            let tests = tests_with(true, &coverage, UNIT);
            for id in ["unit", "integration", "property"] {
                assert_eq!(tests.shared_from(id), None, "{missing}: {id}");
            }
        }
        let tests = tests_with(
            true,
            &COVERAGE.replace("--profile ci", "--profile default"),
            UNIT,
        );
        assert_eq!(tests.shared_from("unit"), None);
    }

    #[test]
    fn integration_takes_unit_only_when_its_command_is_unit_s() {
        let tests = tests_with(false, COVERAGE, UNIT);
        assert_eq!(tests.shared_from("integration"), Some("unit"));
        let tests = tests_with(false, COVERAGE, &format!("{UNIT} --test it"));
        assert_eq!(tests.shared_from("integration"), None);
        // Unit off or out of range: no unit command, so integration runs.
        let mut tests = tests_with(false, COVERAGE, UNIT);
        tests.unit.clear();
        assert_eq!(tests.shared_from("integration"), None);
    }

    #[test]
    fn a_shared_layer_records_its_source_run_and_no_source_is_no_pass() {
        let tests = tests_with(true, COVERAGE, UNIT);
        for outcome in [Outcome::Passed, Outcome::Failed, Outcome::ToolMissing] {
            let took = tests.take("unit", "coverage", &[run("coverage", outcome)]);
            assert_eq!(took.outcome, outcome);
            assert_eq!(took.layer, "unit");
            assert_eq!(took.cmd, "coverage cmd");
            assert_eq!(took.step, "coverage step");
        }
        let took = tests.take("unit", "coverage", &[run("coverage", Outcome::Vacuous)]);
        assert_eq!(took.outcome, Outcome::Failed);
        let took = tests.take("property", "coverage", &[run("unit", Outcome::Passed)]);
        assert_eq!(took.outcome, Outcome::Failed);
        assert_eq!(took.cmd, "");
        let tests = tests_with(false, COVERAGE, UNIT);
        let took = tests.take("integration", "unit", &[run("unit", Outcome::Failed)]);
        assert_eq!(took.outcome, Outcome::Failed);
        assert_eq!(took.cmd, "unit cmd");
    }

    #[test]
    fn a_shared_run_is_not_a_command_and_its_source_is_named() {
        let runs = [
            run("coverage", Outcome::Passed),
            run("unit", Outcome::Passed),
            run("doc", Outcome::ToolMissing),
        ];
        assert_eq!(ran_commands(&runs, false, &[("unit", "coverage")]), 1);
        assert_eq!(ran_commands(&runs, true, &[]), 3);
        let layers: Vec<config::Layer> =
            ["coverage", "unit", "doc"].into_iter().map(layer).collect();
        let enabled: Vec<&config::Layer> = layers.iter().collect();
        let (line, short) = command_count(1, &enabled, &runs, false, 1, &[("unit", "coverage")]);
        assert!(short);
        assert_eq!(
            line,
            "count: 1 command(s) ran; 2 of 3 enabled layer(s) at tier <= 1 ran one; unit took the coverage run; none ran for: doc"
        );
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let (line, short) =
            command_count(2, &enabled[..2], &runs, false, 1, &[("unit", "coverage")]);
        assert!(!short);
        assert!(
            line.ends_with("ran one; unit took the coverage run"),
            "{line}"
        );
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let (line, _) = command_count(2, &enabled[..2], &runs, false, 1, &[("unit", "unit")]);
        assert!(line.ends_with("ran one; unit took the unit run"), "{line}");
    }

    #[test]
    fn the_mutation_diff_uses_merge_base_and_the_report_dir() {
        let report = Path::new("/work/target/rust-tops");
        let Some(Plan::Mutation(plan)) = plan_for(&layer("mutation"), report) else {
            panic!("mutation has no plan");
        };
        assert_eq!(plan.diff_path, report.join("rust-tops.diff"));
        let script = plan.diff.argv.join(" ");
        assert!(
            script.contains("git merge-base origin/main HEAD"),
            "{script}"
        );
        assert!(script.contains("refs/remotes/origin/main"), "{script}");
        assert!(script.contains("&& git diff"), "{script}");
        assert!(!script.contains("mutants"), "{script}");
        assert!(!script.contains("/tmp"), "{script}");
        assert!(plan
            .mutants
            .argv
            .contains(&"/work/target/rust-tops/rust-tops.diff".to_string()));
        assert_eq!(
            steps::tool_binary(&plan.mutants.argv).as_deref(),
            Some("cargo-mutants")
        );
    }

    #[test]
    fn no_mapped_layer_writes_under_tmp() {
        for id in [
            "static-lint",
            "unit",
            "integration",
            "doc",
            "coverage",
            "crap",
            "property",
            "security-advisory",
        ] {
            for chain in chains(id) {
                for step in chain {
                    assert!(!step.display().contains("/tmp"), "{id}: {}", step.display());
                }
            }
        }
    }

    #[test]
    fn unmapped_layers_have_no_plan() {
        for id in [
            "acceptance",
            "fuzz",
            "miri",
            "sanitizer",
            "bench-criterion",
            "bench-callgrind",
            "density-audit",
            "laws",
        ] {
            assert!(plan_for(&layer(id), Path::new("/r")).is_none(), "{id}");
        }
    }

    #[test]
    fn formal_kani_runs_cargo_kani() {
        let kani = chains("formal-kani");
        assert_eq!(kani.len(), 1);
        #[expect(
            clippy::indexing_slicing,
            reason = "already in this crate when the strict table was turned on"
        )]
        let step = tool_step(&kani[0]);
        assert_eq!(step.argv, ["cargo", "kani", "--workspace"]);
        assert_eq!(
            steps::tool_binary(&step.argv).as_deref(),
            Some("cargo-kani")
        );
    }

    #[test]
    fn init_turns_acceptance_and_laws_off_and_keeps_kani_at_tier_4() {
        for class in PROFILE_CLASSES {
            let yaml = init_yaml(class);
            let doc = facts::parse_yaml(&yaml).expect("init yaml parses");
            let layer = |id: &str| {
                #[expect(
                    clippy::indexing_slicing,
                    reason = "already in this crate when the strict table was turned on"
                )]
                doc["layers"]
                    .as_array()
                    .and_then(|layers| layers.iter().find(|l| l["id"] == id))
                    .cloned()
                    .unwrap_or_else(|| panic!("{class}: no {id} layer"))
            };
            #[expect(
                clippy::indexing_slicing,
                reason = "already in this crate when the strict table was turned on"
            )]
            let tops_idx_1593_24 = layer("acceptance")["enabled"].clone();
            assert_eq!(tops_idx_1593_24, false, "{class}");
            #[expect(
                clippy::indexing_slicing,
                reason = "already in this crate when the strict table was turned on"
            )]
            let tops_idx_1598_24 = layer("formal-kani")["tier"].clone();
            assert_eq!(tops_idx_1598_24, 4, "{class}");
            #[expect(
                clippy::indexing_slicing,
                reason = "already in this crate when the strict table was turned on"
            )]
            let tops_idx_1603_24 = layer("laws")["enabled"].clone();
            assert_eq!(tops_idx_1603_24, false, "{class}");
        }
    }

    #[test]
    fn a_failed_diff_is_failed_even_over_a_stale_diff() {
        let dir = std::env::temp_dir().join(format!("cargo-tops-diff-{}", std::process::id()));
        fs::create_dir_all(&dir).expect("mkdir");
        let stale = dir.join("stale.diff");
        fs::write(&stale, "+changed\n").expect("write");
        let empty = dir.join("empty.diff");
        fs::write(&empty, "").expect("write");
        assert_eq!(steps::judge_diff(false, &stale), Outcome::Failed);
        assert_eq!(steps::judge_diff(false, &empty), Outcome::Failed);
        assert_eq!(steps::judge_diff(true, &empty), Outcome::Vacuous);
        assert_eq!(steps::judge_diff(true, &stale), Outcome::Passed);
        assert_eq!(
            steps::judge_diff(true, &dir.join("absent")),
            Outcome::Failed
        );
        #[expect(
            clippy::let_underscore_must_use,
            reason = "already in this crate when the strict table was turned on"
        )]
        let _ = fs::remove_dir_all(&dir);
        assert!(!Outcome::Vacuous.fails_the_gate());
        assert!(!Outcome::Vacuous.counts_for_a_law());
    }

    #[test]
    fn init_yaml_has_no_exemption_for_every_class() {
        for class in PROFILE_CLASSES {
            let yaml = init_yaml(class);
            let doc = facts::parse_yaml(&yaml).expect("init yaml parses");
            #[expect(
                clippy::indexing_slicing,
                reason = "already in this crate when the strict table was turned on"
            )]
            let tops_idx_1641_24 = doc["exemptions"].clone();
            assert_eq!(tops_idx_1641_24, serde_json::json!([]), "{class}");
            assert!(!yaml.contains("generated protobuf"), "{class}");
            #[expect(
                clippy::indexing_slicing,
                reason = "already in this crate when the strict table was turned on"
            )]
            let tops_idx_1647_21 = doc["layers"].clone();
            assert!(tops_idx_1647_21.as_array().is_some_and(|l| !l.is_empty()));
        }
    }

    #[test]
    fn the_exemptions_block_is_replaced_in_place() {
        let yaml = "a: 1\nexemptions:\n  - layer: crap\n    expires: \"2027-01-01\"\nb: 2\n";
        assert_eq!(with_init_exemptions(yaml), "a: 1\nexemptions: []\nb: 2\n");
        assert_eq!(with_init_exemptions("a: 1\n"), "a: 1\nexemptions: []\n");
    }

    /// The directories are held open, so a directory swapped for a link
    /// after the walk is not where the file goes: it lands in the real
    /// directory, wherever that now is, and nothing goes behind the link.
    #[cfg(unix)]
    #[test]
    fn a_directory_swapped_for_a_link_after_the_walk_is_not_followed() {
        let root =
            std::env::temp_dir().join(format!("cargo-tops-parent-swap-{}", std::process::id()));
        #[expect(
            clippy::let_underscore_must_use,
            reason = "already in this crate when the strict table was turned on"
        )]
        let _ = fs::remove_dir_all(&root);
        let repo = root.join("repo");
        let outside = root.join("outside");
        fs::create_dir_all(outside.join("workflows")).expect("mkdir outside");
        fs::create_dir_all(&repo).expect("mkdir repo");
        let rel = Path::new(".github/workflows/rust-tops.yml");
        let parent = Parent::open(&repo, rel, &repo.join(rel)).expect("walk");
        fs::rename(repo.join(".github"), repo.join("github-aside")).expect("move aside");
        std::os::unix::fs::symlink(&outside, repo.join(".github")).expect("link");
        let name = std::ffi::OsStr::new("rust-tops.yml");
        assert!(matches!(parent.entry(name), Entry::Absent));
        drop(parent.create_new(name).expect("create"));
        assert!(repo.join("github-aside/workflows/rust-tops.yml").is_file());
        assert_eq!(
            fs::read_dir(outside.join("workflows"))
                .expect("read outside")
                .count(),
            0
        );
        // A walk that starts now meets the link and refuses it.
        let err = Parent::open(&repo, rel, &repo.join(rel))
            .err()
            .expect("refused");
        assert!(
            err.to_string()
                .contains("is a symlink or not a directory, and init does not follow one"),
            "{err}"
        );
        #[expect(
            clippy::let_underscore_must_use,
            reason = "already in this crate when the strict table was turned on"
        )]
        let _ = fs::remove_dir_all(&root);
    }

    /// `create_new` makes a file and nothing else. A name taken by a file,
    /// a link or a dangling link fails it, and what a link points at is left
    /// as it was.
    #[cfg(unix)]
    fn taken_names_fail(
        dir: &Path,
        outside: &Path,
        create: impl Fn(&std::ffi::OsStr) -> std::io::Result<fs::File>,
    ) {
        fs::write(dir.join("file.md"), "old\n").expect("write file");
        std::os::unix::fs::symlink(outside, dir.join("link.md")).expect("link");
        std::os::unix::fs::symlink(dir.join("gone"), dir.join("dangling.md")).expect("link");
        for name in ["file.md", "link.md", "dangling.md"] {
            assert!(create(std::ffi::OsStr::new(name)).is_err(), "{name}");
        }
        assert_eq!(
            fs::read_to_string(dir.join("file.md")).expect("read"),
            "old\n"
        );
        assert_eq!(fs::read_to_string(outside).expect("read"), "keep\n");
        assert!(!dir.join("gone").exists());
        drop(create(std::ffi::OsStr::new("new.md")).expect("create"));
        assert!(dir.join("new.md").is_file());
    }

    /// `write_file` removes an old file before `create_new`, so only a name
    /// taken in between reaches it. Both walks refuse it.
    #[cfg(unix)]
    #[test]
    fn create_new_refuses_a_name_that_is_taken_on_either_walk() {
        let root =
            std::env::temp_dir().join(format!("cargo-tops-create-new-{}", std::process::id()));
        #[expect(
            clippy::let_underscore_must_use,
            reason = "already in this crate when the strict table was turned on"
        )]
        let _ = fs::remove_dir_all(&root);
        fs::create_dir_all(&root).expect("mkdir");
        let outside = root.join("outside.md");
        fs::write(&outside, "keep\n").expect("write outside");
        let rel = Path::new("docs/a.md");
        for held in [true, false] {
            let repo = root.join(if held { "held" } else { "path" });
            fs::create_dir_all(&repo).expect("mkdir repo");
            let dir = repo.join("docs");
            if held {
                let parent = Parent::open(&repo, rel, &repo.join(rel)).expect("walk");
                taken_names_fail(&dir, &outside, |name| parent.create_new(name));
            } else {
                let parent = PathParent::open(&repo, rel, &repo.join(rel)).expect("walk");
                taken_names_fail(&dir, &outside, |name| parent.create_new(name));
            }
        }
        #[expect(
            clippy::let_underscore_must_use,
            reason = "already in this crate when the strict table was turned on"
        )]
        let _ = fs::remove_dir_all(&root);
    }

    /// A link or a regular file where a directory should be is refused by
    /// the held walk and by the path walk, and nothing is made behind it.
    #[cfg(unix)]
    #[test]
    fn a_link_or_a_file_where_a_directory_should_be_is_refused_by_either_walk() {
        let root =
            std::env::temp_dir().join(format!("cargo-tops-walk-refuse-{}", std::process::id()));
        #[expect(
            clippy::let_underscore_must_use,
            reason = "already in this crate when the strict table was turned on"
        )]
        let _ = fs::remove_dir_all(&root);
        let repo = root.join("repo");
        let outside = root.join("outside");
        fs::create_dir_all(&outside).expect("mkdir outside");
        fs::create_dir_all(&repo).expect("mkdir repo");
        std::os::unix::fs::symlink(&outside, repo.join("linked")).expect("link");
        fs::write(repo.join("file"), "keep\n").expect("write file");
        for (at, dir) in [("linked/sub/a.md", "linked"), ("file/sub/a.md", "file")] {
            let (rel, path) = (Path::new(at), repo.join(at));
            let want = format!(
                "refusing to write {}: {} is a symlink or not a directory, and init does not follow one",
                path.display(),
                repo.join(dir).display()
            );
            let held = Parent::open(&repo, rel, &path)
                .err()
                .expect("held walk refused");
            let walked = PathParent::open(&repo, rel, &path)
                .err()
                .expect("path walk refused");
            assert_eq!(held.to_string(), want);
            assert_eq!(walked.to_string(), want);
        }
        assert_eq!(fs::read_dir(&outside).expect("read outside").count(), 0);
        assert_eq!(
            fs::read_to_string(repo.join("file")).expect("read"),
            "keep\n"
        );
        #[expect(
            clippy::let_underscore_must_use,
            reason = "already in this crate when the strict table was turned on"
        )]
        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn version_text_names_package_protocol_and_each_tools_pin() {
        let pkg = env!("CARGO_PKG_VERSION");
        assert_eq!(
            version_text(None),
            format!("cargo-tops {pkg}\nprotocol 1.2.0\ntool pins: none (no ./rust-tops.lock)\n")
        );
        let lock = "[tools]\nrustc = \"1.99.1\"\n[notes]\nx = \"y\"\n";
        assert_eq!(
            version_text(Some(lock)),
            format!(
                "cargo-tops {pkg}\nprotocol 1.2.0\ntool pins (./rust-tops.lock):\n  rustc = 1.99.1\n"
            )
        );
        assert!(version_text(Some("[tools\n")).contains("is not valid TOML"));
    }

    /// The path walk `write_file` takes off Unix, run here: it makes the
    /// missing directories, walks real ones again, and tells a file from a
    /// link without following it.
    #[cfg(unix)]
    #[test]
    fn the_path_walk_makes_directories_and_reads_what_is_at_a_name() {
        let root =
            std::env::temp_dir().join(format!("cargo-tops-path-walk-{}", std::process::id()));
        #[expect(
            clippy::let_underscore_must_use,
            reason = "already in this crate when the strict table was turned on"
        )]
        let _ = fs::remove_dir_all(&root);
        let repo = root.join("repo");
        fs::create_dir_all(&repo).expect("mkdir repo");
        let rel = Path::new("a/b/f.md");
        let parent = PathParent::open(&repo, rel, &repo.join(rel)).expect("walk");
        assert!(repo.join("a/b").is_dir());
        let again = PathParent::open(&repo, rel, &repo.join(rel)).expect("walk real dirs again");
        let name = std::ffi::OsStr::new("f.md");
        assert!(matches!(parent.entry(name), Entry::Absent));
        drop(parent.create_new(name).expect("create"));
        assert!(matches!(again.entry(name), Entry::Other));
        parent.remove(name).expect("remove");
        assert!(!repo.join(rel).exists());
        assert!(matches!(parent.entry(name), Entry::Absent));
        std::os::unix::fs::symlink(root.join("gone"), repo.join("a/b/l.md")).expect("link");
        assert!(matches!(
            parent.entry(std::ffi::OsStr::new("l.md")),
            Entry::Symlink
        ));
        #[expect(
            clippy::let_underscore_must_use,
            reason = "already in this crate when the strict table was turned on"
        )]
        let _ = fs::remove_dir_all(&root);
    }
}