aither-sandbox 0.26.0

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

// Style lints that cost readability in test fixtures.
#![allow(
    clippy::assert_is_empty,
    clippy::cast_possible_truncation,
    clippy::cloned_ref_to_slice_refs,
    clippy::format_push_string,
    clippy::literal_string_with_formatting_args,
    clippy::similar_names,
    clippy::significant_drop_tightening,
    clippy::too_many_lines,
    clippy::type_complexity,
    clippy::unused_self,
    clippy::write_with_newline
)]

use std::collections::BTreeMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};

use super::sandboxed::within;
use super::*;

pub use crate::test_git::git as git_in;
use crate::test_git::succeeds as git_succeeds;

/// A repository with one commit and a linked worktree on the branch
/// `subagent/<namespace>/work`, both inside `root`; returns the worktree.
pub fn repository_with_linked_worktree(root: &Path, namespace: &str) -> PathBuf {
    let repository = root.join("repository");
    std::fs::create_dir_all(&repository).unwrap();
    git_in(&repository, &["init", "--initial-branch=main"]);
    git_in(&repository, &["commit", "--allow-empty", "-m", "initial"]);
    add_worktree(&repository, root, namespace)
}

/// Adds the worktree `<parent>/<namespace>` on the branch
/// `subagent/<namespace>/work` to `repository`.
fn add_worktree(repository: &Path, parent: &Path, namespace: &str) -> PathBuf {
    let worktree = parent.join(namespace);
    let branch = format!("subagent/{namespace}/work");
    git_in(
        repository,
        &[
            "worktree",
            "add",
            "-b",
            branch.as_str(),
            worktree.to_str().unwrap(),
        ],
    );
    worktree
}

async fn main_launch(
    root: &Path,
    grant: &SubagentGrant,
    history: &mut LaunchHistory,
) -> Result<ResolvedGrant, SubagentGrantError> {
    let writable = [dunce::canonicalize(root).unwrap()];
    let spaces_root = writable[0].join("subagent-spaces");
    grant
        .resolve(
            Launcher {
                writable: &writable,
                committing: None,
                spaces_root: &spaces_root,
                spelling: Spelling::host(),
            },
            history,
        )
        .await
}

/// What the sandbox's git does for one command that is not just run.
enum Reply {
    /// Prints these bytes and exits with this code instead of running.
    Print { code: i32, stdout: Vec<u8> },
    /// Never finishes (a long `sleep` stands in for it).
    Hang,
}

/// Plays a subagent's sandbox: git run unsandboxed against the subagent's
/// repository, or a lie told in its place.
#[derive(Default)]
pub struct TestGit {
    replies: Mutex<Vec<(String, Reply)>>,
    hooks: Mutex<Vec<(String, Box<dyn Fn() + Send + Sync>)>>,
    ran: Mutex<Vec<Vec<String>>>,
    hung: Mutex<Vec<u32>>,
}

impl TestGit {
    fn new() -> Arc<Self> {
        Arc::default()
    }

    fn replying(self: Arc<Self>, command: &str, reply: Reply) -> Arc<Self> {
        self.replies.lock().unwrap().push((command.into(), reply));
        self
    }

    fn before(
        self: Arc<Self>,
        command: &str,
        hook: impl Fn() + Send + Sync + 'static,
    ) -> Arc<Self> {
        self.hooks
            .lock()
            .unwrap()
            .push((command.into(), Box::new(hook)));
        self
    }

    /// Every command run so far.
    fn ran(&self) -> Vec<Vec<String>> {
        self.ran.lock().unwrap().clone()
    }

    fn ran_command(&self, command: &str) -> bool {
        self.ran().iter().any(|args| args[0] == command)
    }

    fn git(self: &Arc<Self>) -> SubagentGit {
        SubagentGit::new(self.clone())
    }
}

impl SandboxedGitImpl for TestGit {
    fn run<'a>(
        &'a self,
        run: SandboxedRun<'a>,
    ) -> Pin<Box<dyn Future<Output = Result<SandboxedExit, ExportError>> + Send + 'a>> {
        Box::pin(async move {
            let args: Vec<String> = run.args.iter().map(ToString::to_string).collect();
            self.ran.lock().unwrap().push(args.clone());
            let command = args[0].as_str();
            for (hooked, hook) in self.hooks.lock().unwrap().iter() {
                if hooked == command {
                    hook();
                }
            }
            let reply = {
                let replies = self.replies.lock().unwrap();
                replies
                    .iter()
                    .find(|(replied, _)| replied == command)
                    .map(|(_, reply)| match reply {
                        Reply::Print { code, stdout } => Reply::Print {
                            code: *code,
                            stdout: stdout.clone(),
                        },
                        Reply::Hang => Reply::Hang,
                    })
            };
            match reply {
                Some(Reply::Print { code, stdout }) => {
                    for chunk in stdout.chunks(4096) {
                        run.stdout
                            .send(chunk.to_vec())
                            .await
                            .map_err(|_| ExportError::OutputClosed)?;
                    }
                    return Ok(SandboxedExit {
                        code: Some(code),
                        stderr: String::new(),
                    });
                }
                Some(Reply::Hang) => {
                    let mut child = async_process::Command::new("sleep")
                        .arg("60")
                        .kill_on_drop(true)
                        .spawn()?;
                    self.hung.lock().unwrap().push(child.id());
                    let status = child.status().await?;
                    return Ok(SandboxedExit {
                        code: status.code(),
                        stderr: String::new(),
                    });
                }
                None => {}
            }

            let mut command = async_process::Command::new("git");
            command
                .envs(crate::test_git::ISOLATION)
                .arg("--git-dir")
                .arg(run.repository)
                .args(&args)
                .stdin(async_process::Stdio::piped())
                .stdout(async_process::Stdio::piped())
                .stderr(async_process::Stdio::piped())
                .kill_on_drop(true);
            let mut child = command.spawn()?;
            let mut stdin = child.stdin.take().expect("piped");
            let mut stdout = child.stdout.take().expect("piped");
            let stdin_bytes = run.stdin;
            let feed = async move {
                use futures_lite::io::AsyncWriteExt as _;
                let _ = stdin.write_all(&stdin_bytes).await;
            };
            let forward = async {
                use futures_lite::io::AsyncReadExt as _;
                let mut buffer = vec![0_u8; 8192];
                loop {
                    let read = stdout.read(&mut buffer).await?;
                    if read == 0 {
                        return Ok::<_, ExportError>(());
                    }
                    run.stdout
                        .send(buffer[..read].to_vec())
                        .await
                        .map_err(|_| ExportError::OutputClosed)?;
                }
            };
            let (forwarded, ()) = futures::join!(forward, feed);
            forwarded?;
            let output = child.output().await?;
            Ok(SandboxedExit {
                code: output.status.code(),
                stderr: String::from_utf8_lossy(&output.stderr).trim().to_string(),
            })
        })
    }
}

/// A repository, its subagent worktree and a ledger over them.
struct Fixture {
    dir: tempfile::TempDir,
    /// The worktree, as the subagent sees it.
    worktree: PathBuf,
    /// The main repository's git directory.
    common: PathBuf,
    /// The worktree's entry in it.
    entry: PathBuf,
    /// The subagent's repository.
    subagent: PathBuf,
    resolved: ResolvedGrant,
    stub: Arc<TestGit>,
    ledger: CommitLedger,
}

const ONE_MIB: u64 = 1024 * 1024;

fn limits() -> ImportLimits {
    ImportLimits {
        bytes: ImportBudget::new(256 * ONE_MIB),
        time: Duration::from_secs(60),
    }
}

impl Fixture {
    async fn new(namespace: &str) -> Self {
        Self::with(namespace, TestGit::new(), limits()).await
    }

    async fn with(namespace: &str, stub: Arc<TestGit>, limits: ImportLimits) -> Self {
        let dir = tempfile::tempdir().unwrap();
        let worktree = repository_with_linked_worktree(dir.path(), namespace);
        Self::granted(dir, worktree, stub, limits).await
    }

    async fn granted(
        dir: tempfile::TempDir,
        worktree: PathBuf,
        stub: Arc<TestGit>,
        limits: ImportLimits,
    ) -> Self {
        let resolved = main_launch(
            dir.path(),
            &SubagentGrant::new().with_committing_worktree(&worktree),
            &mut LaunchHistory::default(),
        )
        .await
        .expect("a worktree on a namespaced branch can be granted commit");
        let committing = &resolved.committing[0];
        let ledger = CommitLedger::new(&resolved.committing, stub.git(), limits)
            .expect("one committing worktree");
        Self {
            worktree: committing.worktree.clone(),
            common: committing.common_dir.clone(),
            entry: committing.git_dir.clone(),
            subagent: committing.repository.clone(),
            dir,
            resolved,
            stub,
            ledger,
        }
    }

    /// Commits as the subagent does, in its worktree.
    fn commit(&self, subject: &str) -> String {
        git_in(&self.worktree, &["commit", "--allow-empty", "-m", subject]);
        git_in(&self.worktree, &["rev-parse", "HEAD"])
    }

    /// Whether the main repository holds the object.
    fn main_has(&self, id: &str) -> bool {
        git_succeeds(&self.common, &["cat-file", "-e", id])
    }

    fn main(&self, args: &[&str]) -> String {
        git_in(&self.common, args)
    }

    fn pack_files(&self) -> Vec<String> {
        let mut names: Vec<String> = std::fs::read_dir(self.common.join("objects/pack"))
            .unwrap()
            .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
            .collect();
        names.sort();
        names
    }

    /// The directories in the main object store that are not loose-object
    /// fan-outs, `pack` or `info`: where a quarantine would be.
    fn stray_object_directories(&self) -> Vec<String> {
        std::fs::read_dir(self.common.join("objects"))
            .unwrap()
            .map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
            .filter(|name| name != "pack" && name != "info" && name.len() != 2)
            .collect()
    }

    fn branch(&self, namespace: &str) -> String {
        format!("refs/heads/subagent/{namespace}/work")
    }
}

/// Every file under `root` with its content, except below `except`.
fn contents(root: &Path, except: &Path) -> BTreeMap<PathBuf, Vec<u8>> {
    fn walk(directory: &Path, root: &Path, except: &Path, into: &mut BTreeMap<PathBuf, Vec<u8>>) {
        for entry in std::fs::read_dir(directory).unwrap() {
            let path = entry.unwrap().path();
            if path.starts_with(except) {
                continue;
            }
            if path.is_dir() {
                walk(&path, root, except, into);
            } else {
                into.insert(
                    path.strip_prefix(root).unwrap().to_path_buf(),
                    std::fs::read(&path).unwrap(),
                );
            }
        }
    }
    let mut into = BTreeMap::new();
    walk(root, root, except, &mut into);
    into
}

// ---- 1. the grant's layout ------------------------------------------------

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn a_commit_grant_writes_the_workstrees_own_repository_and_nothing_in_the_main_one() {
    let dir = tempfile::tempdir().unwrap();
    let worktree = repository_with_linked_worktree(dir.path(), "alpha");
    let common = dunce::canonicalize(dir.path().join("repository/.git")).unwrap();
    let entry = common.join("worktrees/alpha");
    let head_before = std::fs::read(entry.join("HEAD")).unwrap();
    let index_before = std::fs::read(entry.join("index")).unwrap();

    let resolved = main_launch(
        dir.path(),
        &SubagentGrant::new().with_committing_worktree(&worktree),
        &mut LaunchHistory::default(),
    )
    .await
    .expect("a worktree on a namespaced branch can be granted commit");

    let subagent = entry.join("subagent");
    assert_eq!(
        resolved.committing[0].writable(),
        [subagent.clone()],
        "the commit grant makes the worktree's repository the one writable path"
    );
    let granted: Vec<&PathBuf> = resolved
        .writable
        .iter()
        .filter(|path| path.starts_with(&common))
        .collect();
    assert_eq!(granted, [&subagent], "nothing else of the main repository");
    assert_eq!(
        std::fs::read_to_string(worktree.join(".git")).unwrap(),
        "gitdir: ../repository/.git/worktrees/alpha/subagent\n",
        "the worktree's .git file names the repository, relatively"
    );
    assert_eq!(std::fs::read(entry.join("HEAD")).unwrap(), head_before);
    assert_eq!(std::fs::read(entry.join("index")).unwrap(), index_before);
    let config = std::fs::read_to_string(subagent.join("config")).unwrap();
    assert!(config.contains("repositoryformatversion = 0"), "{config}");
    assert!(config.contains("path = \"../../../config\""), "{config}");
    assert!(config.contains("[remote \"main-repository\"]"), "{config}");
    assert_eq!(
        std::fs::read_to_string(subagent.join("objects/info/alternates")).unwrap(),
        "../../../../objects\n"
    );
    assert_eq!(
        dunce::canonicalize(git_in(&worktree, &["rev-parse", "--absolute-git-dir"])).unwrap(),
        subagent,
        "git in the worktree uses the subagent's repository"
    );
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn a_sha256_repository_works_end_to_end() {
    let dir = tempfile::tempdir().unwrap();
    let repository = dir.path().join("repository");
    std::fs::create_dir_all(&repository).unwrap();
    git_in(
        &repository,
        &["init", "--initial-branch=main", "--object-format=sha256"],
    );
    git_in(&repository, &["commit", "--allow-empty", "-m", "initial"]);
    let worktree = add_worktree(&repository, dir.path(), "sha");
    let fixture = Fixture::granted(dir, worktree, TestGit::new(), limits()).await;
    let config = std::fs::read_to_string(fixture.subagent.join("config")).unwrap();
    assert!(config.contains("objectformat"), "{config}");
    assert_eq!(
        git_in(&fixture.worktree, &["rev-parse", "--show-object-format"]),
        "sha256"
    );

    let mark = fixture.ledger.mark().await.unwrap();
    let commit = fixture.commit("on sha256");
    assert_eq!(commit.len(), 64);
    assert!(!fixture.main_has(&commit));
    let commits = fixture.ledger.since(&mark).await.expect("the import works");
    assert_eq!(commits.len(), 1, "{commits:?}");
    assert_eq!(commits[0].commit, commit);
    assert!(fixture.main_has(&commit));
}

// ---- 2. a commit lands only in the subagent's repository -------------------

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn a_commit_lands_only_in_the_subagents_repository_and_the_main_one_stays_sound() {
    let fixture = Fixture::new("beta").await;
    let before = contents(&fixture.common, &fixture.subagent);
    let commit = fixture.commit("private work");
    assert_eq!(
        contents(&fixture.common, &fixture.subagent),
        before,
        "the main repository, its refs and the worktree's entry are as they were"
    );
    assert!(!fixture.main_has(&commit));
    assert!(git_succeeds(
        &fixture.worktree,
        &["cat-file", "-e", commit.as_str()]
    ));

    // While the commit exists only in the subagent's repository, the main
    // agent's repository works as it always does.
    let other = fixture.dir.path().join("other");
    std::fs::create_dir_all(&other).unwrap();
    git_in(&other, &["init", "--initial-branch=main"]);
    git_in(&other, &["commit", "--allow-empty", "-m", "elsewhere"]);
    git_in(
        &fixture.common,
        &[
            "fetch",
            other.to_str().unwrap(),
            "main:refs/remotes/other/main",
        ],
    );
    fixture.main(&["repack", "-a", "-d"]);
    fixture.main(&["log", "--all", "--oneline"]);
    fixture.main(&["fsck", "--strict"]);
    fixture.main(&["gc"]);
}

/// The control for the test above: a ref of the main repository that names an
/// object only another repository has is what made the main agent's fetch,
/// repack, gc and fsck fail when a subagent's refs lived there.
// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn a_ref_naming_an_object_the_main_store_lacks_breaks_the_main_repository() {
    let fixture = Fixture::new("gamma").await;
    let commit = fixture.commit("private work");
    let reference = fixture.common.join(fixture.branch("gamma"));
    std::fs::write(&reference, format!("{commit}\n")).unwrap();
    for args in [&["repack", "-a", "-d"][..], &["fsck", "--strict"]] {
        assert!(
            !git_succeeds(&fixture.common, args),
            "git {args:?} must fail when a ref names an object only the subagent has"
        );
    }
}

// ---- 3. git elsewhere is untouched -----------------------------------------

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn git_elsewhere_is_untouched_by_the_redirected_worktree() {
    let fixture = Fixture::new("delta").await;
    fixture.commit("private work");
    let other = fixture.dir.path().join("other");
    std::fs::create_dir_all(&other).unwrap();
    git_in(&other, &["init", "--initial-branch=main"]);
    git_in(&other, &["commit", "--allow-empty", "-m", "elsewhere"]);
    assert!(git_in(&other, &["log", "--oneline"]).ends_with("elsewhere"));
    let clone = fixture.dir.path().join("clone");
    git_in(
        fixture.dir.path(),
        &["clone", other.to_str().unwrap(), clone.to_str().unwrap()],
    );
    assert!(git_in(&clone, &["log", "--oneline"]).ends_with("elsewhere"));

    // A submodule added in the worktree keeps its git directory in the
    // subagent's repository.
    git_in(
        &fixture.worktree,
        &[
            "-c",
            "protocol.file.allow=always",
            "submodule",
            "add",
            other.to_str().unwrap(),
            "sub",
        ],
    );
    let sub_git = std::fs::read_to_string(fixture.worktree.join("sub/.git")).unwrap();
    assert!(
        sub_git.contains("subagent/modules/sub"),
        "the submodule's git directory is in the subagent's repository: {sub_git}"
    );
    git_in(&fixture.worktree, &["commit", "-m", "add the submodule"]);
}

// ---- 4. since imports -------------------------------------------------------

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn since_imports_the_commits_and_a_second_since_runs_no_pack_export() {
    let fixture = Fixture::new("epsilon").await;
    let mark = fixture.ledger.mark().await.unwrap();
    let first = fixture.commit("first");
    let second = fixture.commit("second");
    assert!(!fixture.main_has(&first));

    let commits = fixture.ledger.since(&mark).await.expect("the import works");
    assert_eq!(
        commits
            .iter()
            .map(|commit| commit.subject.as_str())
            .collect::<Vec<_>>(),
        ["first", "second"]
    );
    assert!(fixture.main_has(&first) && fixture.main_has(&second));
    assert_eq!(
        fixture.main(&["rev-parse", &fixture.branch("epsilon")]),
        second,
        "the namespace ref moved"
    );
    fixture.main(&["fsck", "--strict", "--no-dangling"]);
    assert_eq!(
        git_in(&fixture.subagent, &["count-objects", "-v"])
            .lines()
            .find(|line| line.starts_with("count:"))
            .unwrap(),
        "count: 0",
        "the subagent's repository keeps no loose copy of an imported object"
    );

    let packs = fixture.pack_files();
    let exports = fixture
        .stub
        .ran()
        .iter()
        .filter(|args| args[0] == "pack-objects")
        .count();
    let again = fixture
        .ledger
        .since(&mark)
        .await
        .expect("nothing to import");
    assert_eq!(again.len(), 2);
    assert_eq!(fixture.pack_files(), packs, "a second since adds no pack");
    assert_eq!(
        fixture
            .stub
            .ran()
            .iter()
            .filter(|args| args[0] == "pack-objects")
            .count(),
        exports,
        "a second since runs no pack export"
    );
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn merging_the_subagents_work_keeps_its_objects_after_its_repository_is_removed() {
    let fixture = Fixture::new("zeta").await;
    let mark = fixture.ledger.mark().await.unwrap();
    let commit = fixture.commit("to merge");
    fixture.ledger.since(&mark).await.unwrap();
    fixture.main(&["update-ref", "refs/heads/main", &commit]);
    std::fs::remove_dir_all(&fixture.subagent).unwrap();
    fixture.main(&["fsck", "--strict", "--no-dangling"]);
    fixture.main(&["gc", "--prune=now"]);
    assert!(fixture.main_has(&commit));
    assert!(!fixture.main(&["log", "--oneline", "main", "-1"]).is_empty());
}

// ---- 5-8. what the import refuses -------------------------------------------

/// The subagent plants `evil` under the id of `good`, then commits a tree
/// that names that id.
fn forge_object(fixture: &Fixture) {
    use std::io::Write as _;
    let good = fixture.dir.path().join("good.txt");
    std::fs::write(&good, "good\n").unwrap();
    let id = git_in(&fixture.worktree, &["hash-object", good.to_str().unwrap()]);
    let path = fixture
        .subagent
        .join("objects")
        .join(&id[..2])
        .join(&id[2..]);
    std::fs::create_dir_all(path.parent().unwrap()).unwrap();
    let mut encoder = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
    encoder.write_all(b"blob 5\0evil\n").unwrap();
    std::fs::write(&path, encoder.finish().unwrap()).unwrap();
    git_in(
        &fixture.worktree,
        &[
            "update-index",
            "--add",
            "--cacheinfo",
            &format!("100644,{id},good.txt"),
        ],
    );
    git_in(&fixture.worktree, &["commit", "-m", "forged"]);
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn a_forged_object_under_a_predicted_id_is_rejected_and_leaves_nothing_behind() {
    let fixture = Fixture::new("eta").await;
    let mark = fixture.ledger.mark().await.unwrap();
    forge_object(&fixture);
    let packs = fixture.pack_files();
    let refs = fixture.main(&["for-each-ref"]);

    let error = fixture
        .ledger
        .since(&mark)
        .await
        .expect_err("the forged object cannot be imported");
    assert!(
        matches!(error, CommitLedgerError::Rejected { .. }),
        "{error}"
    );
    assert_eq!(fixture.pack_files(), packs, "no pack and no tmp_pack_*");
    assert_eq!(fixture.main(&["for-each-ref"]), refs, "no ref moved");
    assert!(fixture.stray_object_directories().is_empty());
}

/// A pack of one blob, which holds none of the commits a report names.
fn unrelated_pack(directory: &Path) -> Vec<u8> {
    use std::io::Write as _;
    git_in(directory, &["init", "--quiet"]);
    let blob = directory.join("blob.txt");
    std::fs::write(&blob, "unrelated\n").unwrap();
    let id = git_in(directory, &["hash-object", "-w", blob.to_str().unwrap()]);
    let mut child = crate::test_git::command()
        .args(["pack-objects", "--stdout"])
        .current_dir(directory)
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .spawn()
        .unwrap();
    writeln!(child.stdin.take().unwrap(), "{id}").unwrap();
    child.wait_with_output().unwrap().stdout
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn a_pack_that_omits_a_reported_tip_is_incomplete_and_moves_no_ref() {
    let scratch = tempfile::tempdir().unwrap();
    let pack = unrelated_pack(scratch.path());
    let stub = TestGit::new().replying(
        "pack-objects",
        Reply::Print {
            code: 0,
            stdout: pack,
        },
    );
    let fixture = Fixture::with("theta", stub, limits()).await;
    let mark = fixture.ledger.mark().await.unwrap();
    let commit = fixture.commit("lost in transit");
    let refs = fixture.main(&["for-each-ref"]);

    let error = fixture
        .ledger
        .since(&mark)
        .await
        .expect_err("the tip never arrived");
    assert!(
        matches!(&error, CommitLedgerError::Incomplete { detail, .. } if detail.contains(&commit)),
        "{error}"
    );
    assert_eq!(fixture.main(&["for-each-ref"]), refs);
    assert!(!fixture.main_has(&commit));
    assert!(fixture.stray_object_directories().is_empty());
}

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn a_report_naming_a_ref_outside_the_namespace_is_refused_before_anything_runs() {
    let id = "a".repeat(40);
    let lies: [(&str, Reply); 3] = [
        (
            "for-each-ref",
            Reply::Print {
                code: 0,
                stdout: format!("{id} refs/heads/main\n").into_bytes(),
            },
        ),
        (
            "for-each-ref",
            Reply::Print {
                code: 0,
                stdout: b"not-an-id refs/heads/subagent/iota/work\n".to_vec(),
            },
        ),
        (
            "symbolic-ref",
            Reply::Print {
                code: 0,
                stdout: b"refs/heads/main\n".to_vec(),
            },
        ),
    ];
    for (command, reply) in lies {
        let stub = TestGit::new().replying(command, reply);
        let fixture = Fixture::with("iota", stub, limits()).await;
        let refs = fixture.main(&["for-each-ref"]);
        // The run start imports what the repository holds, so it meets the
        // lie; it does not keep the run from starting, but says so.
        let mark = fixture
            .ledger
            .mark()
            .await
            .expect("a lying repository does not keep a run from starting");
        assert!(
            matches!(mark.setbacks(), [CommitLedgerError::Report { .. }]),
            "{:?}",
            mark.setbacks()
        );
        let error = fixture
            .ledger
            .since(&mark)
            .await
            .expect_err("the report is still a lie");
        assert!(matches!(error, CommitLedgerError::Report { .. }), "{error}");
        assert_eq!(fixture.main(&["for-each-ref"]), refs);
        assert!(
            !fixture.stub.ran_command("pack-objects"),
            "nothing runs after a bad report"
        );
    }
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn a_pack_over_the_byte_budget_is_rejected_and_nothing_is_left_behind() {
    let tight = ImportLimits {
        bytes: ImportBudget::new(2048),
        time: Duration::from_secs(60),
    };
    let fixture = Fixture::with("kappa", TestGit::new(), tight).await;
    let mark = fixture.ledger.mark().await.unwrap();
    // Incompressible content, so the pack is as large as the file.
    let noise: Vec<u8> = (0_u32..100_000)
        .map(|n| (n.wrapping_mul(2_654_435_761) >> 13) as u8)
        .collect();
    std::fs::write(fixture.worktree.join("noise.bin"), noise).unwrap();
    git_in(&fixture.worktree, &["add", "noise.bin"]);
    git_in(&fixture.worktree, &["commit", "-m", "heavy"]);
    let packs = fixture.pack_files();

    let error = fixture
        .ledger
        .since(&mark)
        .await
        .expect_err("the pack is over the budget");
    assert!(
        matches!(&error, CommitLedgerError::Rejected { stderr, .. } if stderr.contains("exceeds")),
        "{error}"
    );
    assert_eq!(fixture.pack_files(), packs);
    assert!(fixture.stray_object_directories().is_empty());
}

// ---- 9. a detached HEAD ------------------------------------------------------

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn detached_work_is_listed_mirrored_into_the_entry_and_kept_by_gc() {
    let fixture = Fixture::new("lambda").await;
    let mark = fixture.ledger.mark().await.unwrap();
    git_in(&fixture.worktree, &["switch", "--detach"]);
    let detached = fixture.commit("detached work");
    let commits = fixture.ledger.since(&mark).await.expect("the import works");
    assert_eq!(commits.len(), 1, "{commits:?}");
    assert_eq!(commits[0].commit, detached);
    assert_eq!(
        std::fs::read_to_string(fixture.entry.join("HEAD"))
            .unwrap()
            .trim(),
        detached,
        "the entry's HEAD holds the detached commit"
    );
    fixture.main(&["gc", "--prune=now"]);
    assert!(fixture.main_has(&detached), "gc keeps the detached commit");

    git_in(&fixture.worktree, &["switch", "subagent/lambda/work"]);
    fixture.ledger.since(&mark).await.unwrap();
    assert_eq!(
        std::fs::read_to_string(fixture.entry.join("HEAD"))
            .unwrap()
            .trim(),
        "ref: refs/heads/subagent/lambda/work",
        "re-attaching restores the symbolic HEAD"
    );
}

// ---- 10. forged graph, nested alternate -------------------------------------

/// A commit-graph file that lists `commit` with `tree` and no parent.
fn graph_hiding_the_parent(commit: &str, tree: &str) -> Vec<u8> {
    let commit = hex::decode(commit).unwrap();
    let tree = hex::decode(tree).unwrap();
    let mut fanout = [0_u32; 256];
    for slot in fanout.iter_mut().skip(usize::from(commit[0])) {
        *slot = 1;
    }
    let mut data = Vec::new();
    data.extend_from_slice(b"CGPH");
    data.extend_from_slice(&[1, 1, 3, 0]);
    let header = 8 + 4 * 12;
    let oidf = header;
    let oidl = oidf + 256 * 4;
    let cdat = oidl + 20;
    let end = cdat + 36;
    for (id, offset) in [
        (b"OIDF", oidf),
        (b"OIDL", oidl),
        (b"CDAT", cdat),
        (b"\0\0\0\0", end),
    ] {
        data.extend_from_slice(id);
        data.extend_from_slice(&u64::try_from(offset).unwrap().to_be_bytes());
    }
    for slot in fanout {
        data.extend_from_slice(&slot.to_be_bytes());
    }
    data.extend_from_slice(&commit);
    data.extend_from_slice(&tree);
    data.extend_from_slice(&0x7000_0000_u32.to_be_bytes());
    data.extend_from_slice(&0x7000_0000_u32.to_be_bytes());
    // Generation 1 in the high bits, then the commit time.
    data.extend_from_slice(&(1_u32 << 2).to_be_bytes());
    data.extend_from_slice(&1_700_000_000_u32.to_be_bytes());
    // Git does not check the trailing checksum when it loads the file.
    data.extend_from_slice(&[0; 20]);
    data
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn a_forged_commit_graph_and_a_nested_alternate_change_nothing_the_main_repository_reads() {
    let fixture = Fixture::new("mu").await;
    let mark = fixture.ledger.mark().await.unwrap();
    let parent = fixture.commit("parent only the subagent has");
    let child = fixture.commit("child");
    let tree = git_in(&fixture.worktree, &["rev-parse", "HEAD^{tree}"]);
    let before = (
        fixture.main(&["log", "--all", "--oneline"]),
        fixture.main(&["rev-list", "--all"]),
    );

    // A graph that says the child has no parent, and an alternate naming a
    // directory of the subagent's choosing.
    let info = fixture.subagent.join("objects/info");
    std::fs::write(
        info.join("commit-graph"),
        graph_hiding_the_parent(&child, &tree),
    )
    .unwrap();
    let extra = fixture.dir.path().join("extra-objects");
    std::fs::create_dir_all(&extra).unwrap();
    let alternates = info.join("alternates");
    let mut content = std::fs::read_to_string(&alternates).unwrap();
    content.push_str(&format!("{}\n", extra.display()));
    std::fs::write(&alternates, content).unwrap();
    assert_eq!(
        (
            fixture.main(&["log", "--all", "--oneline"]),
            fixture.main(&["rev-list", "--all"]),
        ),
        before,
        "the main repository reads nothing of the subagent's"
    );

    let refs = fixture.main(&["for-each-ref"]);
    match fixture.ledger.since(&mark).await {
        Ok(_) => {
            assert!(fixture.main_has(&parent) && fixture.main_has(&child));
            fixture.main(&["fsck", "--strict", "--no-dangling"]);
        }
        Err(CommitLedgerError::Rejected { .. } | CommitLedgerError::Incomplete { .. }) => {
            assert_eq!(fixture.main(&["for-each-ref"]), refs, "no partial ref move");
            assert!(!fixture.main_has(&child));
        }
        Err(other) => panic!("a forged graph must end in Rejected or Incomplete: {other}"),
    }
}

// ---- 11. compare and swap ---------------------------------------------------

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn a_ref_that_moved_in_the_main_repository_during_the_import_is_reported_and_left_alone() {
    let common = Arc::new(Mutex::new(None::<(PathBuf, String)>));
    let hook_target = common.clone();
    let stub = TestGit::new().before("pack-objects", move || {
        let guard = hook_target.lock().unwrap();
        let (common, reference) = guard.as_ref().expect("set up");
        let moved = git_in(common, &["commit-tree", "-m", "moved", "HEAD^{tree}"]);
        git_in(common, &["update-ref", reference, &moved]);
    });
    let fixture = Fixture::with("nu", stub, limits()).await;
    *common.lock().unwrap() = Some((fixture.common.clone(), fixture.branch("nu")));
    let mark = fixture.ledger.mark().await.unwrap();
    fixture.commit("raced");

    let error = fixture
        .ledger
        .since(&mark)
        .await
        .expect_err("the ref moved under the import");
    assert!(matches!(error, CommitLedgerError::Moved { .. }), "{error}");
    fixture.main(&["fsck", "--strict", "--no-dangling"]);
    assert!(fixture.stray_object_directories().is_empty());
}

// ---- 12. cancellation and several repositories -----------------------------

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn dropping_since_mid_index_leaves_no_quarantine() {
    let stub = TestGit::new().replying("pack-objects", Reply::Hang);
    let fixture = Fixture::with("xi", stub, limits()).await;
    let mark = fixture.ledger.mark().await.unwrap();
    fixture.commit("never imported");
    let outcome =
        futures_lite::future::or(async { Some(fixture.ledger.since(&mark).await) }, async {
            async_io::Timer::after(Duration::from_millis(1500)).await;
            None
        })
        .await;
    assert!(outcome.is_none(), "the export hangs");
    assert!(
        fixture.stub.ran_command("pack-objects"),
        "the import reached the export"
    );
    assert!(fixture.stray_object_directories().is_empty());
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn one_rejected_repository_does_not_stop_the_others_and_is_named() {
    let dir = tempfile::tempdir().unwrap();
    let repository = dir.path().join("repository");
    std::fs::create_dir_all(&repository).unwrap();
    git_in(&repository, &["init", "--initial-branch=main"]);
    git_in(&repository, &["commit", "--allow-empty", "-m", "initial"]);
    let first = add_worktree(&repository, dir.path(), "good");
    let second = add_worktree(&repository, dir.path(), "bad");
    let resolved = main_launch(
        dir.path(),
        &SubagentGrant::new()
            .with_committing_worktree(&first)
            .with_committing_worktree(&second),
        &mut LaunchHistory::default(),
    )
    .await
    .unwrap();
    let stub = TestGit::new();
    let ledger = CommitLedger::new(&resolved.committing, stub.git(), limits()).unwrap();
    let mark = ledger.mark().await.unwrap();

    git_in(&first, &["commit", "--allow-empty", "-m", "fine"]);
    let fine = git_in(&first, &["rev-parse", "HEAD"]);
    // The second worktree's commit names a blob that is not in its pack.
    let bad_repository = resolved
        .committing
        .iter()
        .find(|worktree| worktree.namespace == "bad")
        .unwrap()
        .repository
        .clone();
    let missing_blob = "b".repeat(40);
    let tree = {
        use std::io::Write as _;
        let mut child = crate::test_git::command()
            .args(["mktree", "--missing"])
            .current_dir(&second)
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .spawn()
            .unwrap();
        write!(
            child.stdin.take().unwrap(),
            "100644 blob {missing_blob}\tmissing.txt\n"
        )
        .unwrap();
        String::from_utf8(child.wait_with_output().unwrap().stdout)
            .unwrap()
            .trim()
            .to_string()
    };
    let broken = git_in(&second, &["commit-tree", "-m", "broken", &tree]);
    git_in(
        &second,
        &["update-ref", "refs/heads/subagent/bad/work", &broken],
    );

    let error = ledger
        .since(&mark)
        .await
        .expect_err("one repository is broken");
    assert!(
        error.to_string().contains(bad_repository.to_str().unwrap()),
        "the error names the failing repository: {error}"
    );
    assert!(
        git_succeeds(
            &dunce::canonicalize(repository.join(".git")).unwrap(),
            &["cat-file", "-e", &fine]
        ),
        "the other repository's import completed"
    );
}

// ---- 13. relocation ---------------------------------------------------------

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn moving_the_whole_tree_keeps_the_worktree_working() {
    let fixture = Fixture::new("omicron").await;
    fixture.commit("before the move");
    let moved = fixture.dir.path().join("moved");
    std::fs::create_dir_all(&moved).unwrap();
    for name in ["repository", "omicron"] {
        std::fs::rename(fixture.dir.path().join(name), moved.join(name)).unwrap();
    }
    let worktree = moved.join("omicron");
    assert!(git_in(&worktree, &["log", "--oneline"]).contains("before the move"));
    git_in(
        &worktree,
        &["commit", "--allow-empty", "-m", "after the move"],
    );
    git_in(&worktree, &["status", "--short"]);
}

// ---- 14. re-grants ----------------------------------------------------------

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn a_converted_worktree_is_granted_again_with_a_fresh_history_and_reuses_its_repository() {
    let fixture = Fixture::new("pi").await;
    let commit = fixture.commit("kept across grants");
    let again = main_launch(
        fixture.dir.path(),
        &SubagentGrant::new().with_committing_worktree(&fixture.worktree),
        &mut LaunchHistory::default(),
    )
    .await
    .expect("a converted worktree can be granted again");
    assert_eq!(again.committing, fixture.resolved.committing);
    assert_eq!(
        git_in(&fixture.worktree, &["rev-parse", "HEAD"]),
        commit,
        "the repository was reused as it stood"
    );
}

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn a_converted_worktree_that_names_another_worktrees_repository_is_refused() {
    let dir = tempfile::tempdir().unwrap();
    let first = repository_with_linked_worktree(dir.path(), "rho");
    let repository = dir.path().join("repository");
    let second = add_worktree(&repository, dir.path(), "sigma");
    main_launch(
        dir.path(),
        &SubagentGrant::new()
            .with_committing_worktree(&first)
            .with_committing_worktree(&second),
        &mut LaunchHistory::default(),
    )
    .await
    .unwrap();
    // The second worktree's .git file now names the first one's repository.
    let first_git = std::fs::read_to_string(first.join(".git")).unwrap();
    std::fs::write(second.join(".git"), first_git).unwrap();
    let error = main_launch(
        dir.path(),
        &SubagentGrant::new().with_committing_worktree(&second),
        &mut LaunchHistory::default(),
    )
    .await
    .expect_err("the entry names another worktree");
    assert!(
        matches!(error, SubagentGrantError::MalformedGitData { .. }),
        "{error}"
    );
}

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn a_commit_grant_under_a_plain_granted_ancestor_is_refused() {
    let dir = tempfile::tempdir().unwrap();
    let repository = dir.path().join("repository");
    std::fs::create_dir_all(&repository).unwrap();
    git_in(&repository, &["init", "--initial-branch=main"]);
    git_in(&repository, &["commit", "--allow-empty", "-m", "initial"]);
    let shared = dir.path().join("shared");
    std::fs::create_dir_all(&shared).unwrap();
    let worktree = add_worktree(&repository, &shared, "tau");

    let mut history = LaunchHistory::default();
    main_launch(
        dir.path(),
        &SubagentGrant::new().with_worktree(&shared),
        &mut history,
    )
    .await
    .unwrap();
    let error = main_launch(
        dir.path(),
        &SubagentGrant::new().with_committing_worktree(&worktree),
        &mut history,
    )
    .await
    .expect_err("a subagent could swap the worktree for a symlink");
    assert!(
        matches!(error, SubagentGrantError::WorktreeExposed { .. }),
        "{error}"
    );
    // Neither order lets a plain grant of the ancestor in afterwards.
    let committed = tempfile::tempdir().unwrap();
    let inner = repository_with_linked_worktree(committed.path(), "upsilon");
    let mut history = LaunchHistory::default();
    main_launch(
        committed.path(),
        &SubagentGrant::new().with_committing_worktree(&inner),
        &mut history,
    )
    .await
    .unwrap();
    let error = main_launch(
        committed.path(),
        &SubagentGrant::new().with_worktree(committed.path()),
        &mut history,
    )
    .await
    .expect_err("the committed worktree lies inside");
    assert!(
        matches!(
            error,
            SubagentGrantError::WorktreeExposed { .. } | SubagentGrantError::GitDataExposed { .. }
        ),
        "{error}"
    );
}

// ---- 15. the exporter's time bound ------------------------------------------

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn an_exporter_that_never_finishes_is_killed_at_the_time_bound() {
    let stub = TestGit::new().replying("pack-objects", Reply::Hang);
    let quick = ImportLimits {
        bytes: ImportBudget::new(256 * ONE_MIB),
        time: Duration::from_millis(800),
    };
    let fixture = Fixture::with("phi", stub, quick).await;
    let mark = fixture.ledger.mark().await.unwrap();
    fixture.commit("stuck");
    let error = fixture
        .ledger
        .since(&mark)
        .await
        .expect_err("the export never ends");
    assert!(
        matches!(
            &error,
            CommitLedgerError::Export {
                source: ExportError::TimedOut(_),
                ..
            }
        ),
        "{error}"
    );
    let pids = fixture.stub.hung.lock().unwrap().clone();
    assert!(!pids.is_empty());
    for pid in pids {
        let mut gone = false;
        for _ in 0..40 {
            // A killed process nobody has reaped yet is a zombie: gone.
            let state = std::process::Command::new("ps")
                .args(["-o", "stat=", "-p", &pid.to_string()])
                .output()
                .unwrap();
            let state = String::from_utf8_lossy(&state.stdout);
            if state.trim().is_empty() || state.trim().starts_with('Z') {
                gone = true;
                break;
            }
            async_io::Timer::after(Duration::from_millis(100)).await;
        }
        assert!(gone, "the exporter's process {pid} is gone");
    }
    assert!(fixture.stray_object_directories().is_empty());
}

// ---- the #227 guarantees that still hold -------------------------------------

#[tokio::test]
async fn a_branch_outside_a_subagent_namespace_cannot_be_granted_commit() {
    let dir = tempfile::tempdir().unwrap();
    let repository = dir.path().join("repository");
    std::fs::create_dir_all(&repository).unwrap();
    git_in(&repository, &["init", "--initial-branch=main"]);
    git_in(&repository, &["commit", "--allow-empty", "-m", "initial"]);
    let worktree = dir.path().join("feature");
    git_in(
        &repository,
        &[
            "worktree",
            "add",
            "-b",
            "feature",
            worktree.to_str().unwrap(),
        ],
    );
    let error = main_launch(
        dir.path(),
        &SubagentGrant::new().with_committing_worktree(&worktree),
        &mut LaunchHistory::default(),
    )
    .await
    .expect_err("a plain branch is not a subagent namespace");
    assert!(
        matches!(error, SubagentGrantError::BranchOutsideNamespace { .. }),
        "{error}"
    );
}

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn a_namespace_holding_another_branch_is_not_the_worktrees_own() {
    let dir = tempfile::tempdir().unwrap();
    let worktree = repository_with_linked_worktree(dir.path(), "beta2");
    git_in(
        &dir.path().join("repository"),
        &["branch", "subagent/beta2/other", "main"],
    );
    let error = main_launch(
        dir.path(),
        &SubagentGrant::new().with_committing_worktree(&worktree),
        &mut LaunchHistory::default(),
    )
    .await
    .expect_err("a shared namespace is refused");
    assert!(
        matches!(error, SubagentGrantError::SharedNamespace { .. }),
        "{error}"
    );
}

/// A subagent rewrites its repository's configuration to run a program for
/// every signed commit `git log` shows, plants such a commit, and points its
/// worktree's `.git` file at a decoy. The host reads the main repository only,
/// with its own configuration, and runs nothing.
#[cfg(unix)]
#[tokio::test]
async fn the_ledger_reads_only_the_main_repository_and_runs_no_program_the_subagent_chose() {
    use std::os::unix::fs::PermissionsExt as _;

    let fixture = Fixture::new("gamma2").await;
    let mark = fixture.ledger.mark().await.unwrap();
    git_in(
        &fixture.worktree,
        &["commit", "--allow-empty", "-m", "real work"],
    );

    let marker = fixture.dir.path().join("ran");
    let program = fixture.dir.path().join("gpg-program");
    std::fs::write(
        &program,
        format!("#!/bin/sh\ntouch '{}'\nexit 1\n", marker.display()),
    )
    .unwrap();
    std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755)).unwrap();
    // A signed commit in the subagent's repository, on its branch.
    let tree = git_in(&fixture.worktree, &["rev-parse", "HEAD^{tree}"]);
    let parent = git_in(&fixture.worktree, &["rev-parse", "HEAD"]);
    let signed = format!(
        "tree {tree}\nparent {parent}\nauthor a <a@a> 0 +0000\ncommitter a <a@a> 0 +0000\ngpgsig -----BEGIN PGP SIGNATURE-----\n \n -----END PGP SIGNATURE-----\n\nsigned decoy\n"
    );
    let object = fixture.dir.path().join("signed-commit");
    std::fs::write(&object, signed).unwrap();
    let signed = git_in(
        fixture.dir.path(),
        &[
            "--git-dir",
            fixture.subagent.to_str().unwrap(),
            "hash-object",
            "-t",
            "commit",
            "-w",
            object.to_str().unwrap(),
        ],
    );
    git_in(
        fixture.dir.path(),
        &[
            "--git-dir",
            fixture.subagent.to_str().unwrap(),
            "-c",
            "core.fsmonitor=false",
            "update-ref",
            &fixture.branch("gamma2"),
            &signed,
        ],
    );
    let mut config = std::fs::read_to_string(fixture.subagent.join("config")).unwrap();
    config.push_str(&format!(
        "[log]\n\tshowSignature = true\n[gpg]\n\tprogram = {}\n[core]\n\tfsmonitor = {}\n",
        program.display(),
        program.display()
    ));
    std::fs::write(fixture.subagent.join("config"), config).unwrap();

    let decoy = fixture.dir.path().join("decoy");
    std::fs::create_dir_all(&decoy).unwrap();
    git_in(&decoy, &["init", "--initial-branch=main"]);
    std::fs::write(
        fixture.worktree.join(".git"),
        format!("gitdir: {}\n", decoy.join(".git").display()),
    )
    .unwrap();

    let commits = fixture
        .ledger
        .since(&mark)
        .await
        .expect("the real repository is read");
    let subjects: Vec<&str> = commits.iter().map(|c| c.subject.as_str()).collect();
    assert_eq!(subjects, ["real work", "signed decoy"]);
    assert!(
        !marker.exists(),
        "the ledger ran a program the subagent chose"
    );
}

/// Commits made on another branch of the namespace, or left behind by
/// resetting the worktree's branch and detaching `HEAD`, are still listed;
/// a branch moved back to where it was lists nothing.
// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn the_ledger_lists_commits_whatever_head_does() {
    let fixture = Fixture::new("delta2").await;
    let worktree = &fixture.worktree;
    let mark = fixture.ledger.mark().await.unwrap();

    git_in(worktree, &["switch", "-c", "subagent/delta2/side"]);
    git_in(
        worktree,
        &["commit", "--allow-empty", "-m", "on a side branch"],
    );
    git_in(worktree, &["switch", "subagent/delta2/work"]);
    git_in(
        worktree,
        &["commit", "--allow-empty", "-m", "detached later"],
    );
    git_in(worktree, &["switch", "--detach"]);
    git_in(worktree, &["branch", "-f", "subagent/delta2/work", "main"]);

    let mut subjects: Vec<String> = fixture
        .ledger
        .since(&mark)
        .await
        .unwrap()
        .into_iter()
        .map(|commit| commit.subject)
        .collect();
    subjects.sort();
    assert_eq!(subjects, ["detached later", "on a side branch"]);
}

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn a_plain_grant_cannot_reach_git_data_commits_are_listed_from() {
    let dir = tempfile::tempdir().unwrap();
    let worktree = repository_with_linked_worktree(dir.path(), "epsilon2");
    let mut history = LaunchHistory::default();
    main_launch(
        dir.path(),
        &SubagentGrant::new().with_committing_worktree(&worktree),
        &mut history,
    )
    .await
    .unwrap();
    let objects_info = dir.path().join("repository/.git/objects/info");
    std::fs::create_dir_all(&objects_info).unwrap();
    let error = main_launch(
        dir.path(),
        &SubagentGrant::new().with_worktree(&objects_info),
        &mut history,
    )
    .await
    .expect_err("the object store's info directory stays read-only");
    assert!(
        matches!(error, SubagentGrantError::GitDataExposed { .. }),
        "{error}"
    );
}

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn git_data_a_subagent_could_write_is_not_trusted_for_a_commit_grant() {
    let dir = tempfile::tempdir().unwrap();
    // The repository itself lies inside a directory handed out earlier.
    let shared = dir.path().join("shared");
    std::fs::create_dir_all(&shared).unwrap();
    let worktree = repository_with_linked_worktree(&shared, "zeta2");
    let mut history = LaunchHistory::default();
    main_launch(
        dir.path(),
        &SubagentGrant::new().with_worktree(&shared),
        &mut history,
    )
    .await
    .unwrap();
    let error = main_launch(
        dir.path(),
        &SubagentGrant::new().with_committing_worktree(&worktree),
        &mut history,
    )
    .await
    .expect_err("a subagent could have rewritten that repository");
    assert!(
        matches!(error, SubagentGrantError::GitDataExposed { .. }),
        "{error}"
    );
}

// ---- the review of the third implementation ----------------------------------

/// `future`'s output, or a panic if it has not finished by `seconds`: a test
/// of something that must not hang fails instead of hanging.
async fn finishes_within<T>(seconds: u64, future: impl Future<Output = T>) -> T {
    within(Duration::from_secs(seconds), future)
        .await
        .unwrap_or_else(|| panic!("did not finish within {seconds} seconds"))
}

fn quick(millis: u64) -> ImportLimits {
    ImportLimits {
        bytes: ImportBudget::new(256 * ONE_MIB),
        time: Duration::from_millis(millis),
    }
}

#[cfg(unix)]
fn make_fifo(path: &Path) {
    let status = std::process::Command::new("mkfifo")
        .arg(path)
        .status()
        .expect("mkfifo runs");
    assert!(status.success());
}

/// Whether the process is gone, a killed one nobody has reaped yet counting
/// as gone.
async fn process_is_gone(pid: u32) -> bool {
    for _ in 0..50 {
        let state = std::process::Command::new("ps")
            .args(["-o", "stat=", "-p", &pid.to_string()])
            .output()
            .unwrap();
        let state = String::from_utf8_lossy(&state.stdout);
        if state.trim().is_empty() || state.trim().starts_with('Z') {
            return true;
        }
        async_io::Timer::after(Duration::from_millis(100)).await;
    }
    false
}

// A commit grant is refused where no shell can carry its shim.
#[cfg(unix)]
#[tokio::test]
async fn every_question_to_the_subagents_repository_is_bounded_in_time_and_its_process_killed() {
    // Each sandboxed command of an import, in the order the import meets them.
    let hangs = [
        ("for-each-ref", None),
        ("symbolic-ref", None),
        (
            "rev-parse",
            Some((
                "symbolic-ref",
                Reply::Print {
                    code: 1,
                    stdout: Vec::new(),
                },
            )),
        ),
    ];
    for (command, before) in hangs {
        let mut stub = TestGit::new();
        if let Some((earlier, reply)) = before {
            stub = stub.replying(earlier, reply);
        }
        let stub = stub.replying(command, Reply::Hang);
        let fixture = Fixture::with("chi", stub, quick(700)).await;
        let mark = finishes_within(20, fixture.ledger.mark())
            .await
            .expect("a stalled repository does not keep a run from starting");
        assert!(
            matches!(
                mark.setbacks(),
                [CommitLedgerError::Export {
                    source: ExportError::TimedOut(_),
                    ..
                }]
            ),
            "{command}: {:?}",
            mark.setbacks()
        );
        let error = finishes_within(20, fixture.ledger.since(&mark))
            .await
            .expect_err("the question never gets an answer");
        assert!(
            matches!(
                &error,
                CommitLedgerError::Export {
                    source: ExportError::TimedOut(_),
                    ..
                }
            ),
            "{command}: {error}"
        );
        assert!(
            error
                .to_string()
                .contains(fixture.subagent.to_str().unwrap()),
            "the error names the repository whose commits stay: {error}"
        );
        let pids = fixture.stub.hung.lock().unwrap().clone();
        assert!(!pids.is_empty(), "{command} was asked");
        for pid in pids {
            assert!(process_is_gone(pid).await, "{command}: process {pid} lives");
        }
    }
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn the_tidying_after_an_import_is_bounded_in_time() {
    let stub = TestGit::new().replying("prune-packed", Reply::Hang);
    let fixture = Fixture::with("psi", stub, quick(700)).await;
    let mark = fixture.ledger.mark().await.unwrap();
    let commit = fixture.commit("imported before the tidying hangs");
    let listed = finishes_within(20, fixture.ledger.since(&mark))
        .await
        .expect("the commits arrived; only the tidying stalled");
    assert_eq!(listed.len(), 1);
    assert!(fixture.main_has(&commit));
    let pids = fixture.stub.hung.lock().unwrap().clone();
    for pid in pids {
        assert!(process_is_gone(pid).await, "process {pid} lives");
    }
}

#[tokio::test]
#[cfg(unix)]
async fn a_fifo_planted_in_the_subagents_repository_cannot_hang_the_main_agent() {
    // One planted as a ref file, one behind a config include: every git in
    // the repository reads both.
    for plant in ["packed-refs", "include"] {
        let fixture = Fixture::with("omega", TestGit::new(), quick(700)).await;
        let mark = finishes_within(20, fixture.ledger.mark()).await.unwrap();
        fixture.commit("while the fifo waits");
        if plant == "packed-refs" {
            make_fifo(&fixture.subagent.join("packed-refs"));
        } else {
            make_fifo(&fixture.subagent.join("hang"));
            let config = fixture.subagent.join("config");
            let mut text = std::fs::read_to_string(&config).unwrap();
            text.push_str("[include]\n\tpath = hang\n");
            std::fs::write(&config, text).unwrap();
        }
        let error = finishes_within(20, fixture.ledger.since(&mark))
            .await
            .expect_err("the repository cannot be read");
        assert!(
            matches!(
                &error,
                CommitLedgerError::Export {
                    source: ExportError::TimedOut(_),
                    ..
                }
            ),
            "{plant}: {error}"
        );
        let next = finishes_within(20, fixture.ledger.mark())
            .await
            .expect("the next run starts all the same");
        assert_eq!(next.setbacks().len(), 1, "{plant}");
    }
}

/// A repository with its worktree four directories below the common ancestor,
/// where the relative hooks path of the subagent's repository resolves, from
/// the directory the host runs git in, into a directory a subagent may be
/// granted.
fn deep_layout(root: &Path) -> (PathBuf, PathBuf) {
    let repository = root.join("r");
    std::fs::create_dir_all(&repository).unwrap();
    git_in(&repository, &["init", "--initial-branch=main"]);
    git_in(&repository, &["commit", "--allow-empty", "-m", "initial"]);
    let parent = root.join("a/b/c");
    std::fs::create_dir_all(&parent).unwrap();
    let worktree = add_worktree(&repository, &parent, "wt");
    (repository, worktree)
}

#[cfg(unix)]
fn planted_hook(directory: &Path, marker: &Path) {
    use std::os::unix::fs::PermissionsExt as _;
    std::fs::create_dir_all(directory).unwrap();
    let hook = directory.join("reference-transaction");
    std::fs::write(&hook, format!("#!/bin/sh\ntouch '{}'\n", marker.display())).unwrap();
    std::fs::set_permissions(&hook, std::fs::Permissions::from_mode(0o755)).unwrap();
}

#[tokio::test]
#[cfg(unix)]
async fn host_git_runs_no_hook_a_subagent_could_have_placed() {
    let dir = tempfile::tempdir().unwrap();
    let (repository, worktree) = deep_layout(dir.path());
    let marker = dir.path().join("hook-ran");
    // Where `../../../../r/.git/hooks`, spelled for the worktree, lands when
    // it is resolved against the repository's creation directory instead.
    planted_hook(&repository.join("r/.git/hooks"), &marker);
    // The main repository's own hooks are off for the import as well.
    let own_marker = dir.path().join("own-hook-ran");
    planted_hook(&repository.join(".git/hooks"), &own_marker);

    let resolved = main_launch(
        dir.path(),
        &SubagentGrant::new().with_committing_worktree(&worktree),
        &mut LaunchHistory::default(),
    )
    .await
    .expect("the grant is given");
    assert!(
        !marker.exists(),
        "creating the repository ran a hook from a path resolved against the wrong directory"
    );

    let ledger = CommitLedger::new(&resolved.committing, TestGit::new().git(), limits()).unwrap();
    let mark = ledger.mark().await.unwrap();
    git_in(&worktree, &["commit", "--allow-empty", "-m", "imported"]);
    // The subagent's own commit ran the main repository's hooks, as its
    // configuration says; that is not the import.
    assert!(own_marker.exists());
    std::fs::remove_file(&own_marker).unwrap();
    let listed = ledger.since(&mark).await.unwrap();
    assert_eq!(listed.len(), 1);
    assert!(!marker.exists(), "the import ran a placed hook");
    assert!(!own_marker.exists(), "the import ran the repository's hook");
}

/// Plants a commit the import refuses, as the subagent.
async fn poisoned() -> (Fixture, CommitMark) {
    let fixture = Fixture::new("poisoned").await;
    let mark = fixture.ledger.mark().await.unwrap();
    forge_object(&fixture);
    (fixture, mark)
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn a_repository_whose_commits_cannot_be_imported_does_not_lock_the_worktree() {
    let (fixture, mark) = poisoned().await;
    let refs = fixture.main(&["for-each-ref"]);
    let error = fixture
        .ledger
        .since(&mark)
        .await
        .expect_err("the forged commit is refused");
    let message = error.to_string();
    assert!(
        message.contains(fixture.subagent.to_str().unwrap())
            && message.contains("deleting that directory discards them"),
        "the error says where the commits stay and how to discard them: {message}"
    );

    // The next subagent on the worktree starts all the same, from what the
    // main repository holds, and the main agent is told what stays behind.
    let next = fixture
        .ledger
        .mark()
        .await
        .expect("an earlier failed import does not keep the worktree from running");
    assert!(
        matches!(next.setbacks(), [CommitLedgerError::Rejected { .. }]),
        "{:?}",
        next.setbacks()
    );
    assert_eq!(
        fixture.main(&["for-each-ref"]),
        refs,
        "nothing was dropped or moved"
    );
    assert!(
        fixture.subagent.exists(),
        "the unimported commits are still in place"
    );

    // Deleting the repository and granting the worktree again gives it a
    // fresh one, and its commits import.
    std::fs::remove_dir_all(&fixture.subagent).unwrap();
    let resolved = main_launch(
        fixture.dir.path(),
        &SubagentGrant::new().with_committing_worktree(&fixture.worktree),
        &mut LaunchHistory::default(),
    )
    .await
    .expect("a worktree whose repository was deleted is granted again");
    let ledger = CommitLedger::new(&resolved.committing, fixture.stub.git(), limits()).unwrap();
    let fresh = ledger.mark().await.unwrap();
    assert!(fresh.setbacks().is_empty(), "{:?}", fresh.setbacks());
    let commit = fixture.commit("after the reset");
    let listed = ledger.since(&fresh).await.unwrap();
    assert_eq!(
        listed.iter().map(|c| c.commit.as_str()).collect::<Vec<_>>(),
        [commit.as_str()]
    );
    assert!(fixture.main_has(&commit));
}

/// A symbolic HEAD move cannot ride the refs transaction — symref-update
/// cannot rewrite a worktree entry's HEAD — so it follows the committed
/// refs through symbolic-ref, which takes HEAD's lock like any ref write:
/// the refs are already committed when the lock refuses the mirror, and
/// the next import heals it.
// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn a_locked_head_stops_a_symbolic_mirror_but_not_the_refs() {
    let fixture = Fixture::new("locked").await;
    let mark = fixture.ledger.mark().await.unwrap();
    // A branch inside the granted namespace moves the report's HEAD while
    // leaving the entry's referent untouched.
    git_in(
        &fixture.worktree,
        &["checkout", "-b", "subagent/locked/other"],
    );
    let commit = fixture.commit("moved");
    let lock = fixture.entry.join("HEAD.lock");
    std::fs::write(&lock, "").unwrap();
    fixture
        .ledger
        .since(&mark)
        .await
        .expect_err("the locked HEAD refuses the mirror");
    assert_eq!(
        fixture.main(&["rev-parse", "refs/heads/subagent/locked/other"]),
        commit,
        "the namespace refs committed, the HEAD mirror alone is behind"
    );
    assert_eq!(
        std::fs::read_to_string(fixture.entry.join("HEAD"))
            .unwrap()
            .trim(),
        "ref: refs/heads/subagent/locked/work",
        "the entry's HEAD stayed where it was"
    );

    std::fs::remove_file(&lock).unwrap();
    let listed = fixture.ledger.since(&mark).await.unwrap();
    assert_eq!(listed.len(), 1);
    assert_eq!(listed[0].commit, commit);
    assert_eq!(
        std::fs::read_to_string(fixture.entry.join("HEAD"))
            .unwrap()
            .trim(),
        "ref: refs/heads/subagent/locked/other"
    );
}

/// When an import also moves the ref the entry's HEAD points at, HEAD
/// moves in a follow-up transaction — updating its referent already
/// counts as updating it. A lock on it still freezes the whole import:
/// the referent update itself needs HEAD's lock. Removing it recovers.
// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn a_locked_head_blocks_an_import_moving_its_referent_too() {
    let fixture = Fixture::new("referent").await;
    let mark = fixture.ledger.mark().await.unwrap();
    let commit = fixture.commit("moved");
    // Detaching changes the entry's HEAD while the import also updates
    // its referent: HEAD cannot ride this transaction, but its lock still
    // covers the referent update.
    git_in(&fixture.worktree, &["checkout", "--detach"]);
    let refs = fixture.main(&["for-each-ref"]);
    let lock = fixture.entry.join("HEAD.lock");
    std::fs::write(&lock, "").unwrap();
    fixture
        .ledger
        .since(&mark)
        .await
        .expect_err("the locked HEAD refuses the import");
    assert_eq!(
        fixture.main(&["for-each-ref"]),
        refs,
        "the namespace refs did not move either"
    );

    std::fs::remove_file(&lock).unwrap();
    let listed = fixture.ledger.since(&mark).await.unwrap();
    assert_eq!(listed.len(), 1);
    assert_eq!(listed[0].commit, commit);
    assert_eq!(
        std::fs::read_to_string(fixture.entry.join("HEAD"))
            .unwrap()
            .trim(),
        commit,
        "the entry's HEAD was detached at the commit once the lock went"
    );
}

#[tokio::test]
#[cfg(unix)]
async fn one_worktree_that_cannot_be_prepared_does_not_hold_back_the_others() {
    let dir = tempfile::tempdir().unwrap();
    let repository = dir.path().join("repository");
    std::fs::create_dir_all(&repository).unwrap();
    git_in(&repository, &["init", "--initial-branch=main"]);
    git_in(&repository, &["commit", "--allow-empty", "-m", "initial"]);
    let good = add_worktree(&repository, dir.path(), "kept");
    let bad = add_worktree(&repository, dir.path(), "stuck");
    let resolved = main_launch(
        dir.path(),
        &SubagentGrant::new()
            .with_committing_worktree(&good)
            .with_committing_worktree(&bad),
        &mut LaunchHistory::default(),
    )
    .await
    .unwrap();
    git_in(
        &good,
        &["commit", "--allow-empty", "-m", "left by an earlier holder"],
    );
    let left = git_in(&good, &["rev-parse", "HEAD"]);
    let stuck = resolved
        .committing
        .iter()
        .find(|worktree| worktree.namespace == "stuck")
        .unwrap();
    make_fifo(&stuck.repository.join("packed-refs"));

    let ledger = CommitLedger::new(&resolved.committing, TestGit::new().git(), quick(700)).unwrap();
    let mark = finishes_within(20, ledger.mark()).await.unwrap();
    assert_eq!(mark.setbacks().len(), 1);
    assert!(
        mark.setbacks()[0]
            .to_string()
            .contains(stuck.repository.to_str().unwrap())
    );
    assert!(
        git_succeeds(&stuck.common_dir, &["cat-file", "-e", &left]),
        "the other worktree was carried through its import"
    );
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn an_import_never_removes_a_directory_it_did_not_create() {
    let fixture = Fixture::new("sweep").await;
    let objects = fixture.common.join("objects");
    // Git's own receive-pack quarantine, and another import's, long idle.
    let mut left = Vec::new();
    for name in ["tmp_objdir-incoming-abc123", "aither-import-someone-else"] {
        let directory = objects.join(name);
        std::fs::create_dir_all(directory.join("pack")).unwrap();
        std::fs::File::open(&directory)
            .unwrap()
            .set_modified(std::time::SystemTime::now() - Duration::from_secs(3600))
            .unwrap();
        left.push(directory);
    }
    let mark = fixture.ledger.mark().await.unwrap();
    fixture.commit("imported past them");
    fixture.ledger.since(&mark).await.unwrap();
    for directory in left {
        assert!(directory.exists(), "{} was removed", directory.display());
    }
}

#[tokio::test]
#[cfg(unix)]
async fn killing_the_update_of_the_refs_would_leave_locks_but_letting_it_finish_does_not() {
    let dir = tempfile::tempdir().unwrap();
    let repository = dir.path().join("repository");
    std::fs::create_dir_all(&repository).unwrap();
    git_in(&repository, &["init", "--initial-branch=main"]);
    git_in(&repository, &["commit", "--allow-empty", "-m", "initial"]);
    let git_dir = repository.join(".git");
    let head = git_in(&repository, &["rev-parse", "HEAD"]);

    for (to_completion, branch) in [(false, "killed"), (true, "finished")] {
        let lock = git_dir.join(format!("refs/heads/{branch}.lock"));
        let (commands, input) = async_channel::bounded(1);
        // The transaction is prepared, holding the lock, and not committed.
        commands
            .send(format!("start\ncreate refs/heads/{branch} {head}\nprepare\n").into_bytes())
            .await
            .unwrap();
        let mut update = host_git::HostGit::in_repository(&git_dir, &["update-ref", "--stdin"]);
        if to_completion {
            update = update.finish_when_dropped();
        }
        // The future is dropped as soon as the lock exists, as a cancelled
        // import drops it.
        let outcome = futures_lite::future::or(
            async {
                let _ = update.output(input).await;
                false
            },
            async {
                for _ in 0..100 {
                    if lock.exists() {
                        return true;
                    }
                    async_io::Timer::after(Duration::from_millis(50)).await;
                }
                false
            },
        )
        .await;
        assert!(outcome, "git took the lock");
        drop(commands);

        let mut released = false;
        for _ in 0..60 {
            if !lock.exists() {
                released = true;
                break;
            }
            async_io::Timer::after(Duration::from_millis(100)).await;
        }
        assert_eq!(
            released, to_completion,
            "{branch}: a killed git leaves its lock, one left to finish removes it"
        );
    }
}

#[test]
fn a_budget_handle_changes_the_setting_for_every_clone() {
    let budget = ImportBudget::new(10);
    let other = budget.clone();
    other.set(0);
    assert_eq!(budget.get(), 0);
    assert_eq!(budget.limit(), None, "zero is unbounded");
    budget.set(7);
    assert_eq!(other.limit(), Some(7));
}

/// A commit holding a file of incompressible bytes.
fn commit_noise(fixture: &Fixture) {
    let noise: Vec<u8> = (0_u32..100_000)
        .map(|n| (n.wrapping_mul(2_654_435_761) >> 13) as u8)
        .collect();
    std::fs::write(fixture.worktree.join("noise.bin"), noise).unwrap();
    git_in(&fixture.worktree, &["add", "noise.bin"]);
    git_in(&fixture.worktree, &["commit", "-m", "heavy"]);
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn a_byte_budget_of_zero_bounds_nothing() {
    let limits = ImportLimits {
        bytes: ImportBudget::new(0),
        time: Duration::from_secs(60),
    };
    let fixture = Fixture::with("zero", TestGit::new(), limits).await;
    let mark = fixture.ledger.mark().await.unwrap();
    commit_noise(&fixture);
    let listed = fixture
        .ledger
        .since(&mark)
        .await
        .expect("no byte bound refuses nothing, reports and pack alike");
    assert_eq!(listed.len(), 1);
}

// Sandboxed git runs only off Windows: a package's ancestors carry
// traverse alone, and git resolves its paths through a read on each.
#[cfg(unix)]
#[tokio::test]
async fn the_byte_budget_is_read_at_each_import() {
    let budget = ImportBudget::new(2048);
    let limits = ImportLimits {
        bytes: budget.clone(),
        time: Duration::from_secs(60),
    };
    let fixture = Fixture::with("live", TestGit::new(), limits).await;
    let mark = fixture.ledger.mark().await.unwrap();
    commit_noise(&fixture);
    let error = fixture
        .ledger
        .since(&mark)
        .await
        .expect_err("over the budget");
    assert!(
        matches!(error, CommitLedgerError::Rejected { .. }),
        "{error}"
    );
    // The operator raises it; the ledger already running sees that.
    budget.set(256 * ONE_MIB);
    let listed = fixture.ledger.since(&mark).await.unwrap();
    assert_eq!(listed.len(), 1);
}

#[tokio::test]
#[cfg(unix)]
async fn a_gitfile_the_subagent_replaced_is_never_followed_or_waited_on() {
    let fixture = Fixture::new("gitfile").await;
    let dot_git = fixture.worktree.join(".git");
    let grant = SubagentGrant::new().with_committing_worktree(&fixture.worktree);
    let hostile: [(&str, Box<dyn Fn()>); 3] = [
        (
            "a fifo",
            Box::new(|| {
                std::fs::remove_file(&dot_git).unwrap();
                make_fifo(&dot_git);
            }),
        ),
        (
            "a symlink to an endless file",
            Box::new(|| {
                std::fs::remove_file(&dot_git).unwrap();
                std::os::unix::fs::symlink("/dev/zero", &dot_git).unwrap();
            }),
        ),
        (
            "a file too long for a gitdir line",
            Box::new(|| {
                std::fs::remove_file(&dot_git).unwrap();
                std::fs::write(&dot_git, vec![b'x'; 1024 * 1024]).unwrap();
            }),
        ),
    ];
    for (what, replace) in hostile {
        replace();
        // A fresh history has to read the file to learn where the data is.
        let fresh = finishes_within(
            20,
            main_launch(fixture.dir.path(), &grant, &mut LaunchHistory::default()),
        )
        .await;
        assert!(
            matches!(fresh, Err(SubagentGrantError::Inspect { .. })),
            "{what}: {fresh:?}"
        );
        // A history that holds the layout does not read it, and the file
        // naming the repository is written back.
        let mut history = LaunchHistory::default();
        history
            .committed
            .push(fixture.resolved.committing[0].clone());
        finishes_within(20, main_launch(fixture.dir.path(), &grant, &mut history))
            .await
            .unwrap_or_else(|error| panic!("{what}: {error}"));
        git_in(&fixture.worktree, &["status", "--short"]);
    }
}