mbx 0.5.2

A build cache for Rust projects
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
//! End-to-end coverage for cached cargo commands.
//!
//! Each test drives the real binary over a throwaway project with no
//! dependencies, so nothing here needs the network.

use std::path::Path;
use std::process::Command;

fn write_project(directory: &Path) {
    write_named_project(directory, "fixture");
}

/// Write the fixture under `name`.
///
/// The name reaches the lockfile, and the lockfile is what the build identity
/// is keyed on, so two differently named fixtures are two different identities
/// -- which is what it takes to tell one checkout's artifacts from another's.
fn write_named_project(directory: &Path, name: &str) {
    std::fs::create_dir_all(directory.join("src")).unwrap();
    std::fs::write(
        directory.join("Cargo.toml"),
        format!("[package]\nname = \"{name}\"\nversion = \"0.1.0\"\nedition = \"2021\"\n"),
    )
    .unwrap();
    std::fs::write(
        directory.join("src/lib.rs"),
        "pub fn double(value: u32) -> u32 {\n    value * 2\n}\n",
    )
    .unwrap();
    // Manifest identity follows the lockfile, and cargo would otherwise write
    // it during the first build -- changing the identity between two runs that
    // are supposed to share a manifest.
    let status = Command::new(cargo())
        .current_dir(directory)
        .args(["generate-lockfile", "--offline"])
        .status()
        .expect("cargo should run");
    assert!(status.success(), "the fixture should resolve offline");
}

/// Write a workspace where one member depends on another.
fn write_dependent_project(directory: &Path) {
    std::fs::create_dir_all(directory.join("base/src")).unwrap();
    std::fs::create_dir_all(directory.join("above/src")).unwrap();
    std::fs::write(
        directory.join("Cargo.toml"),
        "[workspace]\nmembers = [\"base\", \"above\"]\nresolver = \"2\"\n",
    )
    .unwrap();
    std::fs::write(
        directory.join("base/Cargo.toml"),
        "[package]\nname = \"base\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
    )
    .unwrap();
    std::fs::write(
        directory.join("base/src/lib.rs"),
        "pub fn value() -> u32 { 0 }\n",
    )
    .unwrap();
    std::fs::write(
        directory.join("above/Cargo.toml"),
        "[package]\nname = \"above\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\nbase = { path = \"../base\" }\n",
    )
    .unwrap();
    std::fs::write(
        directory.join("above/src/lib.rs"),
        "pub fn doubled() -> u32 { base::value() * 2 }\n",
    )
    .unwrap();
    let status = Command::new(cargo())
        .current_dir(directory)
        .args(["generate-lockfile", "--offline"])
        .status()
        .expect("cargo should run");
    assert!(status.success(), "the fixture should resolve offline");
}

/// Write a workspace whose dependency builds before a member that fails.
fn write_partially_failing_project(directory: &Path) {
    std::fs::create_dir_all(directory.join("good/src")).unwrap();
    std::fs::create_dir_all(directory.join("bad/src")).unwrap();
    std::fs::write(
        directory.join("Cargo.toml"),
        "[workspace]\nmembers = [\"good\", \"bad\"]\nresolver = \"2\"\n",
    )
    .unwrap();
    std::fs::write(
        directory.join("good/Cargo.toml"),
        "[package]\nname = \"good\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
    )
    .unwrap();
    std::fs::write(directory.join("good/src/lib.rs"), "pub fn good() {}\n").unwrap();
    std::fs::write(
        directory.join("bad/Cargo.toml"),
        "[package]\nname = \"bad\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n[dependencies]\ngood = { path = \"../good\" }\n",
    )
    .unwrap();
    std::fs::write(
        directory.join("bad/src/lib.rs"),
        "use good as _;\ncompile_error!(\"expected failure\");\n",
    )
    .unwrap();
    let status = Command::new(cargo())
        .current_dir(directory)
        .args(["generate-lockfile", "--offline"])
        .status()
        .expect("cargo should run");
    assert!(status.success(), "the fixture should resolve offline");
}

fn cargo() -> std::ffi::OsString {
    std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into())
}

#[cfg(unix)]
#[test]
fn transparent_rustc_replaces_the_shim_process() {
    let directory = tempfile::tempdir().unwrap();
    let shim = mbx::session::install_shim(
        Path::new(env!("CARGO_BIN_EXE_mbx")),
        directory.path(),
        mbx::session::ShimLink::Tracking,
    )
    .unwrap();
    let pid_file = directory.path().join("compiler.pid");

    // Retried because exec of the binary behind the shim can transiently fail
    // with ETXTBSY: anyone holding it open for write blocks the exec, and a
    // sibling test that forks while cargo is writing it counts, since until
    // that child reaches its own exec the inherited descriptor (cloexec or not)
    // is a writer of this file too.
    let mut child = {
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
        loop {
            let attempt = Command::new(&shim)
                .arg("/bin/sh")
                .args(["-c", "printf '%s' \"$$\" > \"$1\"", "sh"])
                .arg(&pid_file)
                .spawn();
            match attempt {
                Err(error)
                    if error.kind() == std::io::ErrorKind::ExecutableFileBusy
                        && std::time::Instant::now() < deadline =>
                {
                    std::thread::sleep(std::time::Duration::from_millis(20));
                }
                other => break other.unwrap(),
            }
        }
    };
    let shim_pid = child.id();
    let status = child.wait().unwrap();

    assert!(status.success());
    assert_eq!(
        std::fs::read_to_string(pid_file).unwrap(),
        shim_pid.to_string()
    );
}

/// Build `project` against `store`, returning the run's statistics.
fn build(project: &Path, store: &Path, report: &Path) -> serde_json::Value {
    build_with(project, store, report, &[]).0
}

/// Build as `build` does, with `settings` added to the environment.
///
/// Returns what the build said on stderr alongside its statistics, because some
/// of what mbx reports -- a sweep, for one -- is only ever said there.
fn build_with(
    project: &Path,
    store: &Path,
    report: &Path,
    settings: &[(&str, &str)],
) -> (serde_json::Value, String) {
    let mut command = Command::new(env!("CARGO_BIN_EXE_mbx"));
    command
        .current_dir(project)
        .args(["build", "--offline"])
        .env("MBX_CACHE_DIR", store)
        .env("MBX_STATS_REPORT", report)
        // Cargo's own environment for this test would otherwise redirect the
        // fixture's output into this crate's target directory.
        .env_remove("CARGO_TARGET_DIR")
        // All three decide whether cargo compiles incrementally, so a test that
        // says nothing about it must not inherit an answer from the machine it
        // runs on. CARGO_INCREMENTAL is the one that bites: an enabled build
        // defers to it, and `Swatinem/rust-cache` sets it to 0 for the whole
        // job, so leaving it would make this suite pass locally and fail in CI.
        .env_remove("MBX_INCREMENTAL")
        .env_remove("CARGO_INCREMENTAL")
        .env_remove("CI")
        // Same reason: a test asserting the default cross-checkout behaviour
        // must not read an answer out of the developer's environment.
        .env_remove("MBX_SHARE_OUT_DIR")
        .env_remove("MBX_LEARNED_INCREMENTAL")
        // The C shims are on by default; a test that says nothing about them
        // must not inherit a different answer from the developer's shell. The
        // compiler variables matter just as much: this suite is itself run
        // through mbx, so without this a fixture would inherit the outer
        // session's shims and every build here would stand aside.
        .env_remove("MBX_CC")
        .env_remove("CC")
        .env_remove("CXX")
        .env_remove("HOST_CC")
        .env_remove("HOST_CXX")
        .env_remove("TARGET_CC")
        .env_remove("TARGET_CXX")
        .env_remove("MBX_REAL_CC")
        .env_remove("MBX_REAL_CXX");
    for (name, value) in settings {
        command.env(name, value);
    }
    let output = command.output().expect("mbx should run");
    assert!(
        output.status.success(),
        "build failed ({}): {}",
        output.status,
        String::from_utf8_lossy(&output.stderr)
    );
    let stats = std::fs::read(report).expect("a statistics report should be written");
    (
        serde_json::from_slice(&stats).expect("the report should be JSON"),
        String::from_utf8_lossy(&output.stderr).into_owned(),
    )
}

/// Run `mbx` against `store` and return its stdout.
fn mbx(store: &Path, arguments: &[&str]) -> String {
    let output = Command::new(env!("CARGO_BIN_EXE_mbx"))
        .args(arguments)
        .env("MBX_CACHE_DIR", store)
        .output()
        .expect("mbx should run");
    assert!(
        output.status.success(),
        "{arguments:?} failed ({}): {}",
        output.status,
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).into_owned()
}

/// The bytes `mbx gc` weighs against its budget.
fn store_bytes(store: &Path) -> u64 {
    tree_bytes(&store.join("actions/cas")) + tree_bytes(&store.join("actions/action-results"))
}

/// Total size of every file under `directory`.
fn tree_bytes(directory: &Path) -> u64 {
    let mut total = 0;
    let mut pending = vec![directory.to_path_buf()];
    while let Some(next) = pending.pop() {
        let Ok(listing) = std::fs::read_dir(&next) else {
            continue;
        };
        for entry in listing.flatten() {
            let metadata = entry.metadata().expect("the entry should be readable");
            if metadata.is_dir() {
                pending.push(entry.path());
            } else if metadata.is_file() {
                total += metadata.len();
            }
        }
    }
    total
}

fn count(stats: &serde_json::Value, field: &str) -> u64 {
    stats[field]
        .as_u64()
        .unwrap_or_else(|| panic!("{field} should be a number"))
}

/// Remove the outputs behind a target link so the next build must restore.
fn wipe_target(project: &Path) {
    let target = project.join("target");
    let outputs = std::fs::read_link(&target).unwrap_or(target);
    std::fs::remove_dir_all(outputs).unwrap();
}

fn corrupt_action_results(store: &Path) -> usize {
    let mut corrupted = 0;
    let mut pending = vec![store.join("actions/action-results")];
    while let Some(directory) = pending.pop() {
        let Ok(entries) = std::fs::read_dir(directory) else {
            continue;
        };
        for entry in entries.flatten() {
            if entry.file_type().is_ok_and(|kind| kind.is_dir()) {
                pending.push(entry.path());
            } else if std::fs::write(entry.path(), b"not json").is_ok() {
                corrupted += 1;
            }
        }
    }
    corrupted
}

#[test]
fn incremental_is_opt_in_and_reaches_cargo() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());

    build(
        project.path(),
        store.path(),
        &reports.path().join("default.json"),
    );
    assert_eq!(
        incremental_sessions(project.path()),
        0,
        "the default build should still force CARGO_INCREMENTAL=0"
    );

    let (stats, _) = build_with(
        project.path(),
        store.path(),
        &reports.path().join("incremental.json"),
        &[("MBX_INCREMENTAL", "1")],
    );
    assert!(
        incremental_sessions(project.path()) > 0,
        "cargo should have compiled the member incrementally: {stats}"
    );
}

#[test]
fn workspace_policy_reaches_cargo() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());
    std::fs::write(project.path().join(".mbx.toml"), "incremental = true\n").unwrap();

    let stats = build(
        project.path(),
        store.path(),
        &reports.path().join("workspace-policy.json"),
    );

    assert!(
        incremental_sessions(project.path()) > 0,
        "the checked-in workspace policy should reach cargo: {stats}"
    );
}

/// How many incremental sessions rustc left behind. Cargo creates the directory
/// either way, so its contents are the only evidence that anything used it.
fn incremental_sessions(project: &Path) -> usize {
    match std::fs::read_dir(project.join("target/debug/incremental")) {
        Ok(entries) => entries.count(),
        Err(_) => 0,
    }
}

/// Edit the fixture so it compiles to something new.
fn edit_project(project: &Path, revision: u32) {
    std::fs::write(
        project.join("src/lib.rs"),
        format!(
            "pub fn double(value: u32) -> u32 {{\n    value * 2 + {revision} - {revision}\n}}\n"
        ),
    )
    .unwrap();
}

/// Incremental state mbx is keeping for churning units, as opposed to the
/// `incremental/` directory cargo drives itself.
///
/// Directories only: the churn records that decide when to create one live
/// beside them as files, and every compilation writes one of those whether or
/// not it ever goes hot.
fn learned_sessions(project: &Path) -> usize {
    let target = project.join("target");
    let outputs = std::fs::read_link(&target).unwrap_or(target);
    match std::fs::read_dir(outputs.join("mbx-incremental")) {
        Ok(entries) => entries
            .flatten()
            .filter(|entry| entry.file_type().is_ok_and(|kind| kind.is_dir()))
            .count(),
        Err(_) => 0,
    }
}

fn compiled_incrementally(stats: &serde_json::Value) -> u64 {
    stats["compiler"]["incremental"]["invocations"]
        .as_u64()
        .unwrap_or(0)
}

/// A crate somebody is editing misses on every build no matter what the cache
/// does. After enough of those in a row, it gets to keep its own incremental
/// state -- which never reaches the store, because it describes one checkout's
/// edit history rather than its source.
#[test]
fn a_churning_crate_earns_its_own_incremental_state() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());

    let mut stats = build(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
    );
    for revision in 1..=4 {
        edit_project(project.path(), revision);
        stats = build(
            project.path(),
            store.path(),
            &reports.path().join(format!("edit-{revision}.json")),
        );
        // The streak has to build up first, so nothing is expected before it.
        if revision < 3 {
            assert_eq!(
                compiled_incrementally(&stats),
                0,
                "one or two edits is not a pattern yet: {stats}"
            );
        }
    }

    assert!(
        compiled_incrementally(&stats) > 0,
        "the edited crate should have compiled incrementally by now: {stats}"
    );
    assert!(
        learned_sessions(project.path()) > 0,
        "it should have left incremental state behind: {stats}"
    );
    assert_eq!(
        stats["stored_bytes"].as_u64(),
        Some(0),
        "an incremental artifact must never be published: {stats}"
    );
}

/// The same evidence that turns it on turns it off: once the content stops
/// moving, the unit compiles normally again and rejoins the shared cache.
#[test]
fn a_settled_crate_publishes_again() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());

    build(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
    );
    for revision in 1..=4 {
        edit_project(project.path(), revision);
        build(
            project.path(),
            store.path(),
            &reports.path().join(format!("edit-{revision}.json")),
        );
    }

    // Same content as the last build, so the key it recorded is the key this
    // compilation has: the churn is over.
    wipe_target(project.path());
    let settled = build(
        project.path(),
        store.path(),
        &reports.path().join("settled.json"),
    );
    assert_eq!(
        compiled_incrementally(&settled),
        0,
        "unchanged content should compile normally: {settled}"
    );
    assert!(
        settled["stored_bytes"].as_u64().unwrap_or(0) > 0,
        "and it should be published: {settled}"
    );

    wipe_target(project.path());
    let warm = build(
        project.path(),
        store.path(),
        &reports.path().join("warm.json"),
    );
    assert!(
        warm["hits"].as_u64().unwrap_or(0) > 0,
        "so a later build can restore it: {warm}"
    );
}

/// A compilation that failed left nothing behind to compare against, so the
/// retry that follows -- with nothing edited in between -- must not read as a
/// crate that had settled and drop it back to compiling from scratch.
#[test]
fn a_failed_build_does_not_cost_the_streak() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());

    build(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
    );
    for revision in 1..=3 {
        edit_project(project.path(), revision);
        build(
            project.path(),
            store.path(),
            &reports.path().join(format!("edit-{revision}.json")),
        );
    }

    // Break it, then retry the same broken source twice over.
    std::fs::write(project.path().join("src/lib.rs"), "fn broken( {\n").unwrap();
    for attempt in 0..2 {
        let failed = Command::new(env!("CARGO_BIN_EXE_mbx"))
            .current_dir(project.path())
            .args(["build", "--offline"])
            .env("MBX_CACHE_DIR", store.path())
            .env_remove("CARGO_TARGET_DIR")
            .env_remove("MBX_INCREMENTAL")
            .env_remove("CARGO_INCREMENTAL")
            .env_remove("CI")
            .output()
            .expect("mbx should run");
        assert!(!failed.status.success(), "attempt {attempt} should fail");
    }

    // One more real edit is all it should take to go hot.
    edit_project(project.path(), 4);
    let stats = build(
        project.path(),
        store.path(),
        &reports.path().join("recovered.json"),
    );

    assert!(
        compiled_incrementally(&stats) > 0,
        "the failures should not have reset what the edits established: {stats}"
    );
}

/// A fresh runner has no incremental state to reuse, so the trade is all cost.
#[test]
fn churn_earns_nothing_in_ci() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());

    let mut stats = build(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
    );
    for revision in 1..=4 {
        edit_project(project.path(), revision);
        stats = build_with(
            project.path(),
            store.path(),
            &reports.path().join(format!("edit-{revision}.json")),
            &[("CI", "true")],
        )
        .0;
    }

    assert_eq!(
        compiled_incrementally(&stats),
        0,
        "CI should have compiled every edit normally: {stats}"
    );
}

/// A crate's action key hashes the artifacts it links against, so rebuilding a
/// dependency changes it without anybody having touched the crate. Watching the
/// key instead of the sources would send the whole cone above an edited crate
/// hot, and stop all of it publishing -- the sharing loss this feature exists
/// to avoid.
#[test]
fn editing_one_crate_leaves_its_dependents_publishing() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_dependent_project(project.path());

    let mut stats = build(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
    );
    for revision in 1..=5 {
        std::fs::write(
            project.path().join("base/src/lib.rs"),
            format!("pub fn value() -> u32 {{ {revision} }}\n"),
        )
        .unwrap();
        stats = build(
            project.path(),
            store.path(),
            &reports.path().join(format!("edit-{revision}.json")),
        );
    }

    assert_eq!(
        compiled_incrementally(&stats),
        1,
        "only the edited crate should be compiling incrementally: {stats}"
    );
    assert!(
        stats["stored_bytes"].as_u64().unwrap_or(0) > 0,
        "the crate above it never changed, so it should still publish: {stats}"
    );
}

#[test]
fn learned_incremental_can_be_turned_off() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());

    let mut stats = build(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
    );
    for revision in 1..=4 {
        edit_project(project.path(), revision);
        stats = build_with(
            project.path(),
            store.path(),
            &reports.path().join(format!("edit-{revision}.json")),
            &[("MBX_LEARNED_INCREMENTAL", "0")],
        )
        .0;
    }

    assert_eq!(
        compiled_incrementally(&stats),
        0,
        "the setting should have kept every edit normal: {stats}"
    );
}

#[test]
fn restores_a_wiped_target_directory_from_the_store() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());

    let cold = build(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
    );
    assert!(
        cold["compiler"]["unconsulted"]["duration_ns"]
            .as_u64()
            .is_some_and(|duration| duration > 0),
        "the cold build should report time spent compiling: {cold}"
    );
    assert!(
        cold["slow_compilations"]
            .as_array()
            .is_some_and(|crates| !crates.is_empty()),
        "the cold build should identify its slowest uncached crates: {cold}"
    );
    assert_eq!(count(&cold, "hits"), 0, "a cold build cannot hit");
    assert!(
        count(&cold, "stored_bytes") > 0,
        "a cold build should publish its outputs: {cold}"
    );
    // A cold target directory leaves nothing to derive an action key from, so
    // these compilations run without the cache ever being consulted. Reported
    // apart from misses: counting them as zero of both says the cache was asked
    // and found nothing, which is not what happened.
    assert_eq!(
        count(&cold, "misses"),
        0,
        "a cold build looks nothing up, so it cannot miss: {cold}"
    );
    assert!(
        count(&cold, "unconsulted") > 0,
        "a cold build should report the compilations it had no key for: {cold}"
    );

    // Load-bearing, not cleanup: the wipe is what forces the warm build to
    // restore from the store. Left in place, cargo would find the cold build's
    // outputs and the test would pass without exercising the cache at all.
    wipe_target(project.path());

    let warm = build(
        project.path(),
        store.path(),
        &reports.path().join("warm.json"),
    );
    assert!(
        count(&warm, "hits") > 0,
        "the rebuilt target directory should be restored: {warm}"
    );
    // A hit that never lands on disk is still a broken restore, so check the
    // artifact itself rather than trusting the counter.
    assert!(
        project.path().join("target/debug/libfixture.rlib").exists(),
        "the restored artifact should be materialized: {warm}"
    );
    assert_eq!(
        count(&warm, "compiler_invocations_avoided"),
        count(&warm, "hits")
    );
    assert!(
        count(&warm, "estimated_compiler_duration_avoided_ns") > 0,
        "a warm build should report the compiler time recorded by its cold build: {warm}"
    );
}

#[test]
fn failed_prediction_lookups_are_timed_as_misses() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());
    build(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
    );
    assert!(corrupt_action_results(store.path()) > 0);
    wipe_target(project.path());

    let rebuilt = build(
        project.path(),
        store.path(),
        &reports.path().join("rebuilt.json"),
    );

    assert!(count(&rebuilt, "lookups") > 0, "{rebuilt}");
    assert!(
        rebuilt["compiler"]["miss"]["invocations"]
            .as_u64()
            .is_some_and(|invocations| invocations > 0),
        "{rebuilt}"
    );
    assert_eq!(count(&rebuilt, "unconsulted"), 0, "{rebuilt}");
}

#[test]
fn a_second_checkout_starts_warm() {
    let store = tempfile::tempdir().unwrap();
    let first = tempfile::tempdir().unwrap();
    let second = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(first.path());
    write_project(second.path());

    build(
        first.path(),
        store.path(),
        &reports.path().join("first.json"),
    );
    let warm = build(
        second.path(),
        store.path(),
        &reports.path().join("second.json"),
    );

    assert!(
        count(&warm, "hits") > 0,
        "a checkout at another path should reuse the first build: {warm}"
    );
}

/// How a fixture's library uses its build script's output.
#[derive(Clone, Copy)]
enum Generated {
    /// A cfg only, so the compilation never reads `OUT_DIR`.
    Cfg,
    /// Includes the generated file. `OUT_DIR` becomes an input, but only rustc
    /// records the path, so `--remap-path-prefix` can take it back out.
    Include,
    /// Keeps `OUT_DIR` in a string constant. That lands in the artifact itself,
    /// where no remapping reaches it.
    Text,
}

/// Write a fixture whose build script generates code, used as `generated` says.
fn write_generated_project(directory: &Path, generated: Generated) {
    std::fs::create_dir_all(directory.join("src")).unwrap();
    std::fs::write(
        directory.join("Cargo.toml"),
        "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n\n         [lints.rust]\nunexpected_cfgs = { level = \"allow\" }\n",
    )
    .unwrap();
    std::fs::write(
        directory.join("build.rs"),
        "use std::{env, fs, path::PathBuf};\n         fn main() {\n         \u{20}   let out = PathBuf::from(env::var(\"OUT_DIR\").unwrap());\n         \u{20}   fs::write(out.join(\"generated.rs\"), \"pub const VALUE: u32 = 7;\\n\").unwrap();\n         \u{20}   println!(\"cargo:rustc-cfg=generated\");\n         }\n",
    )
    .unwrap();
    let lib = match generated {
        Generated::Cfg => "#[cfg(generated)]\npub fn value() -> u32 { 7 }\n".to_string(),
        Generated::Include => {
            "include!(concat!(env!(\"OUT_DIR\"), \"/generated.rs\"));\npub fn value() -> u32 { VALUE }\n"
                .to_string()
        }
        Generated::Text => {
            "include!(concat!(env!(\"OUT_DIR\"), \"/generated.rs\"));\n\
             pub const WHERE: &str = env!(\"OUT_DIR\");\n\
             pub fn value() -> u32 { VALUE }\n"
                .to_string()
        }
    };
    std::fs::write(directory.join("src/lib.rs"), lib).unwrap();
    let status = Command::new(cargo())
        .current_dir(directory)
        .args(["generate-lockfile", "--offline"])
        .status()
        .expect("cargo should run");
    assert!(status.success());
}

/// A build script alone does not stop two checkouts sharing a cache. Consuming
/// `OUT_DIR` does, because its value is an absolute path the compilation reads
/// and could bake into the artifact.
#[test]
fn out_dir_decides_whether_two_checkouts_share() {
    for (generated, expect_hits) in [(Generated::Cfg, true), (Generated::Include, false)] {
        assert_eq!(
            two_checkouts_share(generated, &[]),
            expect_hits,
            "the default should not share a compilation that reads OUT_DIR"
        );
    }
}

/// With sharing on, the compilation is remapped so rustc records the
/// placeholder instead of the real `OUT_DIR`, and the include-only shape crosses
/// checkouts. The shape that keeps the value in a string still does not: the
/// remapping cannot reach into the artifact, and mbx reads the outputs rather
/// than assuming it can.
///
/// The pair is the test. Either half alone would pass for the wrong reason.
#[test]
fn sharing_out_dir_crosses_checkouts_only_where_the_artifact_allows_it() {
    let sharing = [("MBX_SHARE_OUT_DIR", "1")];
    for (generated, expect_hits) in [(Generated::Include, true), (Generated::Text, false)] {
        assert_eq!(
            two_checkouts_share(generated, &sharing),
            expect_hits,
            "sharing changed the wrong shape"
        );
    }
}

/// Build the same fixture in two checkouts, reporting whether the second one
/// reused anything from the first.
fn two_checkouts_share(generated: Generated, settings: &[(&str, &str)]) -> bool {
    let store = tempfile::tempdir().unwrap();
    let first = tempfile::tempdir().unwrap();
    let second = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_generated_project(first.path(), generated);
    write_generated_project(second.path(), generated);

    build_with(
        first.path(),
        store.path(),
        &reports.path().join("first.json"),
        settings,
    );
    let (stats, _) = build_with(
        second.path(),
        store.path(),
        &reports.path().join("second.json"),
        settings,
    );
    count(&stats, "hits") > 0
}

#[test]
fn an_empty_store_reports_nothing() {
    let store = tempfile::tempdir().unwrap();
    let output = Command::new(env!("CARGO_BIN_EXE_mbx"))
        .args(["cache", "stats"])
        .env("MBX_CACHE_DIR", store.path())
        .output()
        .expect("mbx should run");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("objects: 0"), "unexpected output: {stdout}");
}

#[test]
fn forwards_non_build_cargo_subcommands() {
    let root = tempfile::tempdir().unwrap();
    let store = tempfile::tempdir().unwrap();

    let output = Command::new(env!("CARGO_BIN_EXE_mbx"))
        .current_dir(root.path())
        .args(["new", "--vcs", "none", "new-project"])
        .env("MBX_CACHE_DIR", store.path())
        .env("MBX_GC_AUTO", "0")
        .env_remove("CARGO_TARGET_DIR")
        .output()
        .expect("mbx new should run");
    assert!(
        output.status.success(),
        "mbx new failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(root.path().join("new-project/Cargo.toml").is_file());

    let initialized = root.path().join("initialized-project");
    std::fs::create_dir(&initialized).unwrap();
    let output = Command::new(env!("CARGO_BIN_EXE_mbx"))
        .current_dir(&initialized)
        .args(["init", "--vcs", "none"])
        .env("MBX_CACHE_DIR", store.path())
        .env("MBX_GC_AUTO", "0")
        .env_remove("CARGO_TARGET_DIR")
        .output()
        .expect("mbx init should run");
    assert!(
        output.status.success(),
        "mbx init failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(initialized.join("Cargo.toml").is_file());
}

#[test]
fn a_build_records_the_checkout_it_ran_in() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());

    build(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
    );

    // The record is what later tells the collector this checkout exists, so a
    // build that leaves none has silently opted out of being protected.
    let records = store.path().join("actions/checkouts/v1");
    assert!(
        tree_bytes(&records) > 0,
        "the build should record its checkout under {}",
        records.display()
    );
}

#[test]
fn a_failed_build_records_the_compilations_it_completed() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    write_partially_failing_project(project.path());

    let output = Command::new(env!("CARGO_BIN_EXE_mbx"))
        .current_dir(project.path())
        .args(["build", "--workspace", "--offline"])
        .env("MBX_CACHE_DIR", store.path())
        .env("MBX_GC_AUTO", "0")
        .env_remove("CARGO_TARGET_DIR")
        .env_remove("CARGO_INCREMENTAL")
        .output()
        .expect("mbx should run");

    assert!(!output.status.success(), "the bad member should fail");
    assert!(
        tree_bytes(&store.path().join("actions/task-manifests/v1")) > 0,
        "the successful dependency should still be recorded as reachable"
    );
}

#[test]
fn a_build_sweeps_the_store_to_its_budget() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());

    let (_, stderr) = build_with(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
        // A one-byte budget swept every build: nothing this build stored can
        // stay, so the sweep is unambiguous.
        &[("MBX_GC_MAX_SIZE", "1"), ("MBX_GC_INTERVAL", "0")],
    );

    assert!(
        stderr.contains("mbx[gc]: evicted"),
        "the sweep should say what it evicted: {stderr}"
    );
    let stats = mbx(store.path(), &["cache", "stats"]);
    assert!(
        stats.contains("objects: 0"),
        "the store should be swept empty: {stats}"
    );
}

#[test]
fn automatic_sweeps_can_be_turned_off() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    write_project(project.path());

    let (_, stderr) = build_with(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
        &[
            ("MBX_GC_MAX_SIZE", "1"),
            ("MBX_GC_INTERVAL", "0"),
            ("MBX_GC_AUTO", "0"),
        ],
    );

    assert!(
        !stderr.contains("mbx[gc]:"),
        "no sweep should run: {stderr}"
    );
    let stats = mbx(store.path(), &["cache", "stats"]);
    assert!(
        !stats.contains("objects: 0"),
        "the store should be left alone: {stats}"
    );
}

/// Two checkouts, one deleted, a budget that fits only one of them.
///
/// Plain recency would keep the deleted checkout's newer artifacts and evict
/// the surviving one's, so the survivor coming back warm is the whole claim.
#[test]
fn deleting_a_checkout_releases_what_only_it_used() {
    let store = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    let surviving = tempfile::tempdir().unwrap();
    let deleted = tempfile::tempdir().unwrap();
    write_named_project(surviving.path(), "surviving");
    write_named_project(deleted.path(), "deleted");

    build(
        surviving.path(),
        store.path(),
        &reports.path().join("surviving.json"),
    );
    // Budget the store as it stood with one checkout in it, plus slack. The
    // slack matters: an action result is only dropped once its objects are
    // gone, which happens after eviction has already decided how deep to go, so
    // a budget set to the exact byte forces eviction one object further than
    // the arithmetic suggests. At a real budget that rounding is noise; at this
    // scale it is the whole margin.
    let budget = store_bytes(store.path()) + 4096;
    build(
        deleted.path(),
        store.path(),
        &reports.path().join("deleted.json"),
    );
    std::fs::remove_dir_all(deleted.path()).unwrap();

    mbx(store.path(), &["gc", "--max-size", &budget.to_string()]);

    // Load-bearing, not cleanup: the wipe is what forces the next build to go
    // to the store, which is the only way to see what survived the sweep.
    wipe_target(surviving.path());
    let warm = build(
        surviving.path(),
        store.path(),
        &reports.path().join("warm.json"),
    );

    assert!(
        count(&warm, "hits") > 0,
        "the surviving checkout should still be warm: {warm}"
    );
}

/// The per-compilation stream `mbx tui` reads.
mod session_events {
    use super::*;
    use std::path::PathBuf;

    /// Where session streams live, beside the rest of the store's bookkeeping.
    fn sessions_dir(store: &Path) -> PathBuf {
        store.join("actions/sessions/v1")
    }

    /// The events of the one session `store` recorded, in order.
    fn stream(store: &Path) -> Vec<serde_json::Value> {
        let directory = sessions_dir(store);
        let mut streams: Vec<PathBuf> = std::fs::read_dir(&directory)
            .expect("a session directory should exist")
            .flatten()
            .map(|entry| entry.path())
            .filter(|path| {
                path.extension()
                    .is_some_and(|extension| extension == "jsonl")
            })
            .collect();
        assert_eq!(
            streams.len(),
            1,
            "one build should record exactly one stream, found {streams:?}"
        );
        let contents = std::fs::read_to_string(streams.pop().unwrap()).unwrap();
        contents
            .lines()
            .map(|line| serde_json::from_str(line).expect("every line should be JSON"))
            .collect()
    }

    fn outcomes(events: &[serde_json::Value], kind: &str) -> usize {
        events
            .iter()
            .filter(|event| event["type"] == "action" && event["outcome"]["kind"] == kind)
            .count()
    }

    #[test]
    fn a_build_records_its_compilations_between_a_start_and_its_totals() {
        let store = tempfile::tempdir().unwrap();
        let project = tempfile::tempdir().unwrap();
        let reports = tempfile::tempdir().unwrap();
        write_project(project.path());

        let cold = build(
            project.path(),
            store.path(),
            &reports.path().join("cold.json"),
        );

        let events = stream(store.path());
        assert_eq!(events.first().unwrap()["type"], "session_started");
        assert_eq!(
            events.first().unwrap()["command"],
            serde_json::json!(["build", "--offline"])
        );
        let last = events.last().unwrap();
        assert_eq!(last["type"], "session_finished");
        // The stream's own totals are the summary's totals, so a reader of a
        // finished session never has to re-derive them from the rows.
        assert_eq!(last["stats"]["unconsulted"], cold["unconsulted"]);
        assert_eq!(last["stats"]["hits"], cold["hits"]);
        // A cold build compiles without a key to look up, and every one of
        // those compilations should appear as a row.
        assert_eq!(
            outcomes(&events, "unconsulted") as u64,
            count(&cold, "unconsulted"),
            "every unconsulted compilation should have a row: {events:?}"
        );
    }

    #[test]
    fn a_warm_build_records_a_row_per_hit_naming_its_crate() {
        let store = tempfile::tempdir().unwrap();
        let project = tempfile::tempdir().unwrap();
        let reports = tempfile::tempdir().unwrap();
        write_project(project.path());

        build(
            project.path(),
            store.path(),
            &reports.path().join("cold.json"),
        );
        // Load-bearing, not cleanup: without the wipe cargo finds the cold
        // build's outputs and the warm build has nothing to restore.
        wipe_target(project.path());
        let warm = build(
            project.path(),
            store.path(),
            &reports.path().join("warm.json"),
        );

        // Two builds, two streams; this reads the newest.
        let directory = sessions_dir(store.path());
        let mut streams: Vec<PathBuf> = std::fs::read_dir(&directory)
            .unwrap()
            .flatten()
            .map(|entry| entry.path())
            .filter(|path| {
                path.extension()
                    .is_some_and(|extension| extension == "jsonl")
            })
            .collect();
        streams.sort();
        let contents = std::fs::read_to_string(streams.last().unwrap()).unwrap();
        let events: Vec<serde_json::Value> = contents
            .lines()
            .map(|line| serde_json::from_str(line).unwrap())
            .collect();

        let hits = count(&warm, "hits");
        assert!(hits > 0, "the warm build should hit: {warm}");
        assert_eq!(
            outcomes(&events, "hit") as u64,
            hits,
            "every hit should have a row: {events:?}"
        );
        // The crate name is what the protocol bump was for: a row that cannot
        // say which crate it restored is not worth showing.
        assert!(
            events.iter().any(|event| {
                event["outcome"]["kind"] == "hit" && event["crate_name"] == "fixture"
            }),
            "a hit row should name the crate it restored: {events:?}"
        );
    }

    #[test]
    fn recording_can_be_turned_off() {
        let store = tempfile::tempdir().unwrap();
        let project = tempfile::tempdir().unwrap();
        let reports = tempfile::tempdir().unwrap();
        write_project(project.path());

        build_with(
            project.path(),
            store.path(),
            &reports.path().join("cold.json"),
            &[("MBX_EVENTS", "0")],
        );

        assert!(
            !sessions_dir(store.path()).exists(),
            "a build that records nothing should leave no session directory"
        );
    }
}

/// Managed target directories rest on a symlink standing in for `target`, and
/// Windows only lets a privileged or developer-mode process create one, so mbx
/// leaves the target directory where cargo put it there. These cover the
/// platforms where the feature is available.
#[cfg(unix)]
mod target_views {
    use super::*;

    fn managed(project: &Path) -> std::path::PathBuf {
        std::fs::read_link(project.join("target")).expect("target should be a link")
    }

    #[test]
    fn a_managed_target_directory_keeps_the_workspace_clean() {
        let store = tempfile::tempdir().unwrap();
        let project = tempfile::tempdir().unwrap();
        let reports = tempfile::tempdir().unwrap();
        write_project(project.path());

        build(
            project.path(),
            store.path(),
            &reports.path().join("cold.json"),
        );

        let directory = managed(project.path());
        assert!(
            directory.starts_with(store.path().join("targets")),
            "outputs should land under the managed root, not the workspace: {}",
            directory.display()
        );
        // The link is the point: a relocation that breaks the paths people type
        // would not be worth the disk it reclaims.
        assert!(
            project.path().join("target/debug/libfixture.rlib").exists(),
            "the workspace should still reach its own build outputs"
        );
    }

    #[test]
    fn a_managed_target_directory_still_hits_the_cache() {
        let store = tempfile::tempdir().unwrap();
        let first = tempfile::tempdir().unwrap();
        let second = tempfile::tempdir().unwrap();
        let reports = tempfile::tempdir().unwrap();
        write_project(first.path());
        write_project(second.path());

        build(
            first.path(),
            store.path(),
            &reports.path().join("first.json"),
        );
        let (warm, _) = build_with(
            second.path(),
            store.path(),
            &reports.path().join("second.json"),
            &[("MBX_TARGET_VIEWS", "1")],
        );

        // The shim maps the target directory out of its keys before anything
        // else, so moving it must not cost a single hit.
        assert!(
            count(&warm, "hits") > 0,
            "a relocated target directory should still reuse the first build: {warm}"
        );
    }

    #[test]
    fn deleting_a_checkout_frees_its_managed_target_directory() {
        let store = tempfile::tempdir().unwrap();
        let project = tempfile::tempdir().unwrap();
        let reports = tempfile::tempdir().unwrap();
        write_project(project.path());

        build_with(
            project.path(),
            store.path(),
            &reports.path().join("cold.json"),
            &[("MBX_TARGET_VIEWS", "1")],
        );
        let directory = managed(project.path());
        assert!(tree_bytes(&directory) > 0);

        // Outputs used to live inside the checkout and die with it. Now that
        // they outlive it, collecting them is mbx's job.
        std::fs::remove_dir_all(project.path()).unwrap();
        let output = mbx(store.path(), &["gc", "--max-size", "20GiB"]);

        assert!(
            !directory.exists(),
            "the target directory of a checkout that is gone should be freed"
        );
        assert!(
            output.contains("freed 1 target directories"),
            "gc should say what it freed: {output}"
        );
    }

    #[test]
    fn a_store_sweep_failure_still_frees_managed_target_directories() {
        let store = tempfile::tempdir().unwrap();
        let gone = tempfile::tempdir().unwrap();
        let current = tempfile::tempdir().unwrap();
        let reports = tempfile::tempdir().unwrap();
        write_named_project(gone.path(), "gone");
        write_named_project(current.path(), "current");

        build_with(
            gone.path(),
            store.path(),
            &reports.path().join("gone.json"),
            &[("MBX_TARGET_VIEWS", "1")],
        );
        let directory = managed(gone.path());
        std::fs::remove_dir_all(gone.path()).unwrap();

        // Make the due store sweep fail after it writes its throttle stamp.
        // Target collection is independent and must not be skipped with it.
        let cas = store.path().join("actions/cas/v1");
        std::fs::remove_dir_all(&cas).unwrap();
        std::fs::write(&cas, b"not a directory").unwrap();
        let (_, stderr) = build_with(
            current.path(),
            store.path(),
            &reports.path().join("current.json"),
            &[("MBX_TARGET_VIEWS", "1"), ("MBX_GC_INTERVAL", "0")],
        );

        assert!(stderr.contains("the store was not swept"), "{stderr}");
        assert!(
            !directory.exists(),
            "target collection should still run when store collection fails"
        );
    }

    #[test]
    fn explicit_gc_still_frees_targets_when_store_collection_fails() {
        let store = tempfile::tempdir().unwrap();
        let gone = tempfile::tempdir().unwrap();
        let reports = tempfile::tempdir().unwrap();
        write_project(gone.path());

        build_with(
            gone.path(),
            store.path(),
            &reports.path().join("gone.json"),
            &[("MBX_TARGET_VIEWS", "1")],
        );
        let directory = managed(gone.path());
        std::fs::remove_dir_all(gone.path()).unwrap();
        let cas = store.path().join("actions/cas/v1");
        std::fs::remove_dir_all(&cas).unwrap();
        std::fs::write(&cas, b"not a directory").unwrap();

        let output = Command::new(env!("CARGO_BIN_EXE_mbx"))
            .args(["gc", "--max-size", "20GiB"])
            .env("MBX_CACHE_DIR", store.path())
            .output()
            .expect("mbx should run");

        assert!(
            !output.status.success(),
            "the broken store should be reported"
        );
        assert!(
            String::from_utf8_lossy(&output.stdout).contains("freed 1 target directories"),
            "successful target collection should still be reported"
        );
        assert!(
            !directory.exists(),
            "explicit gc should collect targets independently of the store"
        );
    }

    #[test]
    fn a_noninteractive_build_never_removes_a_real_target_directory() {
        let store = tempfile::tempdir().unwrap();
        let project = tempfile::tempdir().unwrap();
        let reports = tempfile::tempdir().unwrap();
        write_project(project.path());
        // Establish real outputs without a prompt. The next command captures
        // its stdio, so it is non-interactive and must preserve them too.
        build_with(
            project.path(),
            store.path(),
            &reports.path().join("cold.json"),
            &[("MBX_TARGET_VIEWS", "0")],
        );
        assert!(project.path().join("target").is_dir());

        build(
            project.path(),
            store.path(),
            &reports.path().join("warm.json"),
        );

        assert!(
            std::fs::read_link(project.path().join("target")).is_err(),
            "existing build outputs are not ours to move"
        );
        assert!(project.path().join("target/debug/libfixture.rlib").exists());
    }
}

/// Build `project` into an explicit target directory, so two builds of the
/// same checkout differ only in where their outputs land.
fn build_into_target(
    project: &Path,
    store: &Path,
    target: &Path,
    settings: &[(&str, &str)],
) -> String {
    let mut command = Command::new(env!("CARGO_BIN_EXE_mbx"));
    command
        .current_dir(project)
        .args(["build", "--offline"])
        .env("MBX_CACHE_DIR", store)
        .env("CARGO_TARGET_DIR", target)
        .env_remove("MBX_INCREMENTAL")
        .env_remove("CARGO_INCREMENTAL")
        .env_remove("MBX_LEARNED_INCREMENTAL")
        .env_remove("CI")
        .env_remove("MBX_SHARE_OUT_DIR");
    for (name, value) in settings {
        command.env(name, value);
    }
    let output = command.output().expect("mbx should run");
    assert!(
        output.status.success(),
        "build failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stderr).into_owned()
}

/// Read the dep-info rustc wrote for the fixture's library.
fn dep_info_contents(target: &Path) -> String {
    let deps = target.join("debug/deps");
    let entry = std::fs::read_dir(&deps)
        .expect("deps directory should exist")
        .filter_map(Result::ok)
        .find(|entry| {
            let name = entry.file_name();
            let name = name.to_string_lossy();
            name.starts_with("fixture-") && name.ends_with(".d")
        })
        .expect("the library's dep-info should exist");
    std::fs::read_to_string(entry.path()).expect("dep-info should be readable")
}

/// A restored compilation must describe the checkout it was restored into.
///
/// Dep-info rules are keyed by absolute output paths, so a result stored
/// verbatim hands the next target directory rules naming the one that
/// published them. Cargo reads that file, and `MBX_VERIFY=1` compares it, so
/// the stale spelling is both a wrong artifact and a permanent divergence.
#[test]
fn a_restored_dep_info_names_the_target_directory_it_was_restored_into() {
    let store = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let first = tempfile::tempdir().unwrap();
    let second = tempfile::tempdir().unwrap();
    write_project(project.path());

    build_into_target(project.path(), store.path(), first.path(), &[]);
    build_into_target(project.path(), store.path(), second.path(), &[]);

    let restored = dep_info_contents(second.path());
    let foreign = first.path().to_string_lossy().into_owned();
    assert!(
        !restored.contains(&foreign),
        "restored dep-info still names the publishing target directory:\n{restored}"
    );
    assert!(
        restored.contains(&*second.path().to_string_lossy()),
        "restored dep-info should name this target directory:\n{restored}"
    );
}

/// The same compilation restored into a different target directory is what a
/// fresh compilation there would have produced.
///
/// Unix only, and the reason is worth stating. On Windows the compiled
/// artifact itself is not byte-identical between two target directories: with
/// everything else held constant -- one checkout, one set of sources,
/// incremental off -- the rlib still differs, because the debug information
/// records where the compilation wrote its objects. Nothing this fix does can
/// change that, and rewriting inside a compiled artifact would be corruption
/// rather than translation, so verification there reports a difference that is
/// real.
#[cfg(unix)]
#[test]
fn verification_is_clean_across_target_directories() {
    let store = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    let first = tempfile::tempdir().unwrap();
    let second = tempfile::tempdir().unwrap();
    write_project(project.path());

    build_into_target(project.path(), store.path(), first.path(), &[]);
    let report = reports.path().join("verify.json");
    let stderr = build_into_target(
        project.path(),
        store.path(),
        second.path(),
        &[
            ("MBX_VERIFY", "1"),
            ("MBX_STATS_REPORT", report.to_str().unwrap()),
        ],
    );
    let reported = stderr
        .lines()
        .filter(|line| line.contains("diverged"))
        .collect::<Vec<_>>()
        .join("\n");

    let stats: serde_json::Value =
        serde_json::from_slice(&std::fs::read(&report).expect("a report should be written"))
            .expect("the report should be JSON");
    assert!(
        count(&stats, "verifications") > 0,
        "the run should have verified something: {stats}"
    );
    assert_eq!(
        count(&stats, "divergences"),
        0,
        "a restore into another target directory diverged: {reported}"
    );
}
/// Write a fixture whose build script compiles C through `$CC`.
///
/// Deliberately hand-rolled rather than using the `cc` crate: this suite
/// resolves offline and takes no dependencies, and what is under test is the
/// shim the build script inherits, not the crate that would call it.
#[cfg(unix)]
fn write_c_project(directory: &Path) {
    std::fs::create_dir_all(directory.join("src")).unwrap();
    std::fs::create_dir_all(directory.join("include")).unwrap();
    std::fs::write(
        directory.join("Cargo.toml"),
        "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
    )
    .unwrap();
    std::fs::write(
        directory.join("include/hello.h"),
        "int hello_value(void);\n",
    )
    .unwrap();
    std::fs::write(
        directory.join("src/hello.c"),
        "#include \"hello.h\"\nint hello_value(void) { return 7; }\n",
    )
    .unwrap();
    std::fs::write(
        directory.join("build.rs"),
        r#"use std::{env, path::PathBuf, process::Command};

fn main() {
    let out = PathBuf::from(env::var("OUT_DIR").unwrap());
    // The `cc` crate reads HOST_CC before CC when host and target agree, and
    // that is the variable mbx sets; mirroring its precedence is what makes
    // this fixture exercise the same path a real build script takes.
    let compiler = env::var("HOST_CC")
        .or_else(|_| env::var("CC"))
        .unwrap_or_else(|_| "cc".into());
    let status = Command::new(&compiler)
        .arg("-O2")
        .arg("-Iinclude")
        .arg("-c")
        .arg("-o")
        .arg(out.join("hello.o"))
        .arg("src/hello.c")
        .status()
        .expect("the C compiler should run");
    assert!(status.success(), "the fixture's C should compile");
    println!("cargo:rerun-if-changed=src/hello.c");
}
"#,
    )
    .unwrap();
    std::fs::write(
        directory.join("src/lib.rs"),
        "pub fn value() -> u32 { 7 }\n",
    )
    .unwrap();
    let status = Command::new(cargo())
        .current_dir(directory)
        .args(["generate-lockfile", "--offline"])
        .status()
        .expect("cargo should run");
    assert!(status.success(), "the fixture should resolve offline");
}

/// Whether a C compiler is available to compile the fixture at all.
#[cfg(unix)]
fn has_c_compiler() -> bool {
    // Deliberately a real compilation rather than `cc -v`, which is what the
    // adapter's own identity probe runs. Sharing that signal would mean a
    // regression in the probe skipped these tests instead of failing them --
    // the feature would go dead and the suite would stay green.
    let Ok(directory) = tempfile::tempdir() else {
        return false;
    };
    let source = directory.path().join("probe.c");
    if std::fs::write(&source, "int probe(void) { return 0; }\n").is_err() {
        return false;
    }
    Command::new("cc")
        .arg("-c")
        .arg("-o")
        .arg(directory.path().join("probe.o"))
        .arg(&source)
        .output()
        .is_ok_and(|output| output.status.success())
}

/// Build the fixture in two fresh checkouts sharing one store, and report how
/// much the second one reused.
///
/// The hit count is the measure: a C compilation that crosses checkouts is one
/// more cached action than the same build without it. Compiler statistics are
/// keyed by outcome rather than by what was compiled, so they cannot tell the
/// C compile apart from the Rust one.
#[cfg(unix)]
fn warm_checkout_hits(settings: &[(&str, &str)]) -> u64 {
    let store = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    let first = tempfile::tempdir().unwrap();
    let second = tempfile::tempdir().unwrap();
    write_c_project(first.path());
    write_c_project(second.path());

    build_with(
        first.path(),
        store.path(),
        &reports.path().join("first.json"),
        settings,
    );
    let (warm, _) = build_with(
        second.path(),
        store.path(),
        &reports.path().join("second.json"),
        settings,
    );
    count(&warm, "hits")
}

/// A build script's C compilation is cached, and a second checkout restores it
/// rather than compiling again.
#[cfg(unix)]
#[test]
fn a_build_script_c_compilation_crosses_checkouts() {
    if !has_c_compiler() {
        return;
    }
    let cached = warm_checkout_hits(&[]);
    let uncached = warm_checkout_hits(&[("MBX_CC", "0")]);
    assert_eq!(
        cached,
        uncached + 1,
        "the C compilation should be exactly one more restored action"
    );
}

/// Write a fixture whose build script generates a header into `OUT_DIR` and
/// compiles several objects into that same directory.
///
/// This is the shape that a manifest counting every filename gets wrong: the
/// objects land beside the generated header, so what the key recorded depended
/// on how many sibling compilations had finished.
#[cfg(unix)]
fn write_generated_header_project(directory: &Path) {
    std::fs::create_dir_all(directory.join("src")).unwrap();
    std::fs::write(
        directory.join("Cargo.toml"),
        "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
    )
    .unwrap();
    for name in ["a", "b", "c"] {
        std::fs::write(
            directory.join(format!("src/{name}.c")),
            format!("#include \"config.h\"\nint {name}(void) {{ return CONFIG_V; }}\n"),
        )
        .unwrap();
    }
    std::fs::write(
        directory.join("build.rs"),
        r##"use std::{env, fs, path::PathBuf, process::Command};

fn main() {
    let out = PathBuf::from(env::var("OUT_DIR").unwrap());
    fs::write(out.join("config.h"), "#define CONFIG_V 7
").unwrap();
    let compiler = env::var("HOST_CC")
        .or_else(|_| env::var("CC"))
        .unwrap_or_else(|_| "cc".into());
    for name in ["a", "b", "c"] {
        let status = Command::new(&compiler)
            .arg(format!("-I{}", out.display()))
            .arg("-c")
            .arg("-o")
            .arg(out.join(format!("{name}.o")))
            .arg(format!("src/{name}.c"))
            .status()
            .expect("the C compiler should run");
        assert!(status.success());
    }
}
"##,
    )
    .unwrap();
    std::fs::write(
        directory.join("src/lib.rs"),
        "pub fn value() -> u32 { 7 }\n",
    )
    .unwrap();
    let status = Command::new(cargo())
        .current_dir(directory)
        .args(["generate-lockfile", "--offline"])
        .status()
        .expect("cargo should run");
    assert!(status.success(), "the fixture should resolve offline");
}

/// A header generated into `OUT_DIR` is cached like any other, even though the
/// build writes its objects into that same directory.
#[cfg(unix)]
#[test]
fn objects_landing_beside_a_generated_header_still_cross_checkouts() {
    if !has_c_compiler() {
        return;
    }
    let store = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    let first = tempfile::tempdir().unwrap();
    let second = tempfile::tempdir().unwrap();
    write_generated_header_project(first.path());
    write_generated_header_project(second.path());

    build_with(
        first.path(),
        store.path(),
        &reports.path().join("cold.json"),
        &[],
    );
    let (warm, _) = build_with(
        second.path(),
        store.path(),
        &reports.path().join("warm.json"),
        &[],
    );
    assert_eq!(
        count(&warm, "hits"),
        4,
        "all three C compilations and the Rust one should be restored: {warm}"
    );
}

/// A cached C warning names the checkout it is replayed in, not the one that
/// published it.
///
/// A compiler diagnostic names the file it is about, and a generated source
/// lives at an absolute path that differs per checkout, so replaying the stored
/// bytes verbatim would point the reader at somebody else's tree.
#[cfg(unix)]
#[test]
fn a_restored_c_diagnostic_names_the_checkout_it_is_replayed_in() {
    if !has_c_compiler() {
        return;
    }
    let store = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    let first = tempfile::tempdir().unwrap();
    let second = tempfile::tempdir().unwrap();
    write_warning_project(first.path());
    write_warning_project(second.path());

    build_with(
        first.path(),
        store.path(),
        &reports.path().join("cold.json"),
        &[],
    );
    let (warm, stderr) = build_with(
        second.path(),
        store.path(),
        &reports.path().join("warm.json"),
        &[],
    );
    assert!(
        count(&warm, "hits") > 0,
        "the second checkout should have restored the compilation: {warm}"
    );
    let diagnostic = stderr
        .lines()
        .find(|line| line.contains("CCWARN>>>"))
        .unwrap_or_default();
    assert!(
        !diagnostic.contains(&first.path().display().to_string()),
        "a replayed warning must not name the checkout that published it: {diagnostic}"
    );
}

/// A fixture whose build script compiles a generated source that warns, so the
/// diagnostic carries an absolute path.
#[cfg(unix)]
fn write_warning_project(directory: &Path) {
    std::fs::create_dir_all(directory.join("src")).unwrap();
    std::fs::write(
        directory.join("Cargo.toml"),
        "[package]\nname = \"fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
    )
    .unwrap();
    std::fs::write(
        directory.join("build.rs"),
        r##"use std::{env, fs, path::PathBuf, process::Command};

fn main() {
    let out = PathBuf::from(env::var("OUT_DIR").unwrap());
    let source = out.join("gen.c");
    fs::write(&source, "int value(void) { int unused = 1; return 7; }
").unwrap();
    let compiler = env::var("HOST_CC")
        .or_else(|_| env::var("CC"))
        .unwrap_or_else(|_| "cc".into());
    let output = Command::new(&compiler)
        .arg("-Wall")
        .arg("-c")
        .arg("-o")
        .arg(out.join("gen.o"))
        .arg(&source)
        .output()
        .expect("the C compiler should run");
    for line in String::from_utf8_lossy(&output.stderr).lines() {
        println!("cargo:warning=CCWARN>>>{line}");
    }
    assert!(output.status.success());
}
"##,
    )
    .unwrap();
    std::fs::write(
        directory.join("src/lib.rs"),
        "pub fn value() -> u32 { 7 }\n",
    )
    .unwrap();
    let status = Command::new(cargo())
        .current_dir(directory)
        .args(["generate-lockfile", "--offline"])
        .status()
        .expect("cargo should run");
    assert!(status.success(), "the fixture should resolve offline");
}

/// A prediction that no longer describes the tree must not strand the
/// compilation.
///
/// This adapter has no second way to build a key, so a stale prediction that
/// aborted the cache path would fail identically on every later build. The
/// recovery is what the test pins: compile, republish, and hit next time.
#[cfg(unix)]
#[test]
fn a_stale_prediction_recovers_instead_of_stranding_the_compilation() {
    if !has_c_compiler() {
        return;
    }
    let store = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    let project = tempfile::tempdir().unwrap();
    write_c_project(project.path());

    // Record a prediction that names the header.
    build_with(
        project.path(),
        store.path(),
        &reports.path().join("cold.json"),
        &[],
    );

    // Delete the header and stop including it. The command line is untouched,
    // so the invocation still resolves to the same prediction -- one that now
    // names a file that is gone.
    std::fs::remove_file(project.path().join("include/hello.h")).unwrap();
    std::fs::write(
        project.path().join("src/hello.c"),
        "int hello_value(void) { return 7; }\n",
    )
    .unwrap();

    let (recovered, _) = build_with(
        project.path(),
        store.path(),
        &reports.path().join("recover.json"),
        &[],
    );
    assert_eq!(
        recovered["bypasses"].get("cc-input-read"),
        None,
        "a stale prediction is not a bypass: {recovered}"
    );

    // A second checkout of the same modified sources shares the store but none
    // of the outputs, so what it restores is what the recovery republished.
    let second = tempfile::tempdir().unwrap();
    write_c_project(second.path());
    std::fs::remove_file(second.path().join("include/hello.h")).unwrap();
    std::fs::write(
        second.path().join("src/hello.c"),
        "int hello_value(void) { return 7; }\n",
    )
    .unwrap();
    let (warm, _) = build_with(
        second.path(),
        store.path(),
        &reports.path().join("warm.json"),
        &[],
    );
    assert_eq!(
        count(&warm, "hits"),
        2,
        "both the Rust and the C compilation should be restored: {warm}"
    );
}

/// A build that chose its own compiler keeps it.
///
/// `CC` is commonly exported machine-wide, so the shim standing aside is the
/// difference between redirecting a build the user configured and leaving it
/// alone.
#[cfg(unix)]
#[test]
fn an_existing_cc_setting_is_left_alone() {
    if !has_c_compiler() {
        return;
    }
    let chosen = which::which("cc").expect("cc should resolve");
    let preset = warm_checkout_hits(&[("HOST_CC", chosen.to_str().unwrap())]);
    let uncached = warm_checkout_hits(&[("MBX_CC", "0")]);
    assert_eq!(
        preset, uncached,
        "mbx should not have intercepted a compiler the build chose"
    );
}

/// The object a cache hit restores is the object the compiler produced.
#[cfg(unix)]
#[test]
fn a_restored_object_is_byte_identical_to_a_compiled_one() {
    if !has_c_compiler() {
        return;
    }
    let store = tempfile::tempdir().unwrap();
    let reports = tempfile::tempdir().unwrap();
    let first = tempfile::tempdir().unwrap();
    let second = tempfile::tempdir().unwrap();
    write_c_project(first.path());
    write_c_project(second.path());

    build_with(
        first.path(),
        store.path(),
        &reports.path().join("first.json"),
        &[],
    );
    build_with(
        second.path(),
        store.path(),
        &reports.path().join("second.json"),
        &[],
    );

    let compiled = find_object(first.path()).expect("the first checkout should have an object");
    let restored = find_object(second.path()).expect("the second checkout should have an object");
    assert_eq!(
        std::fs::read(&compiled).unwrap(),
        std::fs::read(&restored).unwrap(),
        "a restored object must match the one that was compiled"
    );
}

/// Find the fixture's object file beneath a checkout's target directory.
#[cfg(unix)]
fn find_object(project: &Path) -> Option<std::path::PathBuf> {
    let target = project.join("target");
    let root = std::fs::read_link(&target).unwrap_or(target);
    let mut pending = vec![root];
    while let Some(directory) = pending.pop() {
        for entry in std::fs::read_dir(&directory).ok()? {
            let entry = entry.ok()?;
            let path = entry.path();
            if path.is_dir() {
                pending.push(path);
            } else if path.file_name().is_some_and(|name| name == "hello.o") {
                return Some(path);
            }
        }
    }
    None
}