cosca 0.7.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
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
2157
2158
2159
2160
2161
2162
2163
// Unit tests for dispatch root-detection and mechanism-selection logic.
// These test pure functions directly — no process-env mutation required.

use super::is_nested;
use crate::test_child::Console;
#[cfg(unix)]
use crate::test_groups::setuid;

/// Without the marker the process is the root (not nested).
#[skuld::test]
fn is_nested_without_marker_is_false() {
    assert!(!is_nested(false));
}

/// With the marker the process is already inside a contained tree (nested).
#[skuld::test]
fn is_nested_with_marker_is_true() {
    assert!(is_nested(true));
}

// unix_setup_for mutual-exclusivity tests (S3) =====

/// `ContainMode::Session` must select `UnixSetup::Session` (setsid) — never
/// `ProcessGroup`. This is the critical S3 invariant: calling both setsid AND
/// setpgid/process_group(0) on the same Command would cause EPERM.
#[cfg(unix)]
#[skuld::test]
fn unix_setup_for_session_selects_setsid() {
    use super::{unix_setup_for, UnixSetup};
    use crate::containment::ContainMode;
    assert_eq!(unix_setup_for(Some(ContainMode::Session)), UnixSetup::Session);
}

/// `ContainMode::Strongest` must select `UnixSetup::ProcessGroup` — never Session.
#[cfg(unix)]
#[skuld::test]
fn unix_setup_for_strongest_selects_process_group() {
    use super::{unix_setup_for, UnixSetup};
    use crate::containment::ContainMode;
    assert_eq!(unix_setup_for(Some(ContainMode::Strongest)), UnixSetup::ProcessGroup);
}

/// `ContainMode::TreeWalk` must select `UnixSetup::None` — NO pre-spawn process
/// group. TreeWalk exists to catch children that escape a process group via
/// `setsid`/`setpgid`, so the root must not be put in a group; teardown is by
/// identity at kill time.
#[cfg(unix)]
#[skuld::test]
fn unix_setup_for_treewalk_selects_none() {
    use super::{unix_setup_for, UnixSetup};
    use crate::containment::ContainMode;
    assert_eq!(unix_setup_for(Some(ContainMode::TreeWalk)), UnixSetup::None);
}

/// Uncontained (`None` mode) must select `UnixSetup::ProcessGroup` (the
/// prepare path gates on `mode.is_some()` before calling this, but the
/// default fallback is well-defined).
#[cfg(unix)]
#[skuld::test]
fn unix_setup_for_none_mode_selects_process_group() {
    use super::{unix_setup_for, UnixSetup};
    assert_eq!(unix_setup_for(None), UnixSetup::ProcessGroup);
}

// Attached actionability + nested delegation =====

#[skuld::test]
fn attached_is_actionable() {
    use super::Attached;
    // No teardown mechanism -> not actionable (the _tree guard rejects these).
    assert!(!Attached::None.is_actionable()); // uncontained / lone
    assert!(!Attached::Delegated.is_actionable());
    // Every real mechanism is actionable. Cheap variants are built inline; Cgroup/JobObject
    // need an OS handle, so a test-only constructor builds one (asserted on its own platform).
    assert!(Attached::TreeWalk(crate::identity::ProcessId::current()).is_actionable());
    #[cfg(unix)]
    assert!(Attached::ProcessGroup(0).is_actionable());
    #[cfg(target_os = "linux")]
    assert!(Attached::Cgroup(crate::containment::cgroup::CgroupLeaf::placeholder_for_test()).is_actionable());
    #[cfg(windows)]
    assert!(Attached::JobObject(crate::containment::windows::JobHandle::create_empty_for_test()).is_actionable());
}

/// `kill_tree`/`terminate_tree`'s check of a reaped root (`child::tree_call::root_number`) gates on this
/// predicate: a mechanism that reaches its tree through the root's number, with nothing of its own
/// that checks the root first.
#[cfg(unix)]
#[skuld::test]
fn attached_needs_root_check() {
    use super::Attached;
    assert!(!Attached::None.needs_root_check());
    assert!(!Attached::Delegated.needs_root_check());
    assert!(!Attached::TreeWalk(crate::identity::ProcessId::current()).needs_root_check());
    assert!(Attached::ProcessGroup(0).needs_root_check());
    #[cfg(target_os = "linux")]
    assert!(!Attached::Cgroup(crate::containment::cgroup::CgroupLeaf::placeholder_for_test()).needs_root_check());
}

/// The macOS half of `attached_needs_root_check`: an `FdMarker` needs the check with or without a
/// pgid, since every sweep walks from the root's pid.
///
/// Mutant: the check gated on the marker's pgid.
#[cfg(target_os = "macos")]
#[skuld::test]
fn attached_fd_marker_needs_root_check_with_or_without_a_pgid() {
    use std::os::fd::{AsFd, AsRawFd};

    use super::Attached;

    fn marker_with_pgid(pgid: Option<i32>) -> crate::containment::fdmarker::Marker {
        let (read, write) = std::io::pipe().expect("pipe");
        let handle = crate::containment::fdmarker::pipe_handle_of(write.as_fd()).expect("handle");
        let read_handle = crate::containment::fdmarker::pipe_handle_of(read.as_fd()).expect("read handle");
        let prepared = crate::containment::fdmarker::PreparedMarker {
            read: std::os::fd::OwnedFd::from(read),
            handle,
            read_handle,
            fd: write.as_fd().as_raw_fd(),
        };
        crate::containment::fdmarker::Marker::new(
            prepared,
            crate::containment::fdmarker::fdmarker_tests::inert_root(),
            pgid,
        )
    }

    assert!(Attached::FdMarker(marker_with_pgid(Some(1234))).needs_root_check());
    assert!(Attached::FdMarker(marker_with_pgid(None)).needs_root_check());
}

/// Drives the real `attach()` nested arms (not a hand-built variant): a nested
/// (`!is_root`) contained spawn must yield BOTH halves of the delegated pair —
/// `Containment::Delegated` and `Attached::Delegated` — for a kernel mechanism
/// (Strongest) and TreeWalk, so `containment()` predicts the `_tree` error.
#[skuld::test]
fn nested_attach_is_delegated() {
    use super::{attach, Attached, Prepared};
    use crate::containment::{ContainMode, Containment};

    fn spawn_trivial() -> std::process::Child {
        // attach()'s nested arms don't touch the child, so an exited child is fine. Held for
        // the fork itself: a fork landing while a `fdmarker_tests.rs` test's marker write end
        // is transiently open would inherit it into this not-yet-`exec`'d process, and a
        // concurrent sweep could then find and SIGKILL it.
        #[cfg(unix)]
        return crate::test_spawn::spawn(&mut std::process::Command::new("true")).expect("spawn true");
        #[cfg(windows)]
        return crate::test_spawn::spawn(std::process::Command::new("cmd").args(["/C", "exit"])).expect("spawn cmd");
    }

    for mode in [ContainMode::Strongest, ContainMode::TreeWalk] {
        let mut child = spawn_trivial();
        let prepared = Prepared {
            mode: Some(mode),
            is_root: false, // nested member
            #[cfg(target_os = "linux")]
            cgroup_leaf: None,
            #[cfg(all(target_os = "linux", feature = "tokio"))]
            teardown_warn_after: None,
            #[cfg(target_os = "macos")]
            marker: None,
            #[cfg(windows)]
            graceful: crate::containment::windows::mechanism_from_flags(crate::containment::windows::group_flags()),
            #[cfg(windows)]
            frozen: false,
        };
        #[cfg(windows)]
        let proc_handle = {
            use std::os::windows::io::AsRawHandle;
            child.as_raw_handle()
        };
        let crate::identity::Resolved::Found(id) = crate::identity::ProcessId::of(child.id()) else {
            panic!("the helper child must be readable");
        };
        let attachment = attach(
            #[cfg(unix)]
            child.id(),
            Ok(id),
            #[cfg(target_os = "linux")]
            crate::containment::cgroup::test_support::handle_of(
                child.id(),
                &crate::containment::cgroup::test_support::pidfd_of(child.id()),
            ),
            #[cfg(windows)]
            proc_handle,
            prepared,
        )
        .expect("attach nested");
        let (containment, attached) = (attachment.containment, attachment.attached);
        // Reap before asserting: `attached` is owned and independent of `child`, so a
        // failing assertion must not leak the helper (the nested arms don't touch it).
        _ = child.kill();
        _ = child.wait();
        assert_eq!(
            containment,
            Containment::Delegated,
            "nested member ({mode:?}) must report Containment::Delegated, got {containment:?}"
        );
        assert!(
            matches!(attached, Attached::Delegated),
            "nested member ({mode:?}) must be Attached::Delegated, got {attached:?}"
        );
    }
}

// kill_tree handle-backstop (treewalk / macOS fd-marker fault seam) =====

// A long-lived TreeWalk-contained child; no descendants, so with the root identity-kill
// seam-disabled the ONLY killer is kill_tree's handle backstop. Armed AFTER the fallible
// spawn so a spawn panic cannot leak the flag.
//
// On macOS, `ContainMode::TreeWalk` still attaches `Containment::FdMarker` (decision 2: the
// marker installs for every contained root regardless of requested mode) — its `sweep` calls
// `treewalk::kill_by_identity` directly, never `treewalk::hard_kill`, so `treewalk::fault`'s
// seam is never consumed there; `fdmarker::fault` provides the matching seam instead.
#[skuld::test]
fn sync_kill_tree_backstop_is_load_bearing() {
    #[cfg(not(target_os = "macos"))]
    use super::super::treewalk::fault;
    #[cfg(target_os = "macos")]
    use crate::containment::fdmarker::fault;
    let mut cmd = crate::test_child::command(Console::Hidden);
    #[cfg(unix)]
    cmd.args(["cat"]);
    #[cfg(windows)]
    cmd.args([crate::test_child::windows_more()]);
    cmd.stdin(crate::Stdio::pipe()).expect("set stdin pipe");
    #[cfg(windows)]
    cmd.stdout(crate::Stdio::null()).expect("set stdout null");
    cmd.contain_with(crate::ContainMode::TreeWalk);
    let mut child = cmd.spawn().expect("spawn");
    // Closed right after the kill: a no-op backstop exits 0.
    let stdin = child.stdin().expect("piped stdin");
    fault::set_force_root_kill_noop(true);
    let result = child.kill_tree();
    assert!(
        !fault::armed(),
        "seam not consumed — hard_kill did not run on the arming thread"
    );
    result.expect("kill_tree via backstop");
    drop(stdin);
    let status = child.wait().expect("reap");
    assert!(
        !status.success(),
        "the handle backstop must be what killed the root, got {status:?}"
    );
}

#[cfg(feature = "tokio")]
#[skuld::test]
async fn async_kill_tree_backstop_is_load_bearing() {
    #[cfg(not(target_os = "macos"))]
    use super::super::treewalk::fault;
    #[cfg(target_os = "macos")]
    use crate::containment::fdmarker::fault;
    let mut cmd = crate::test_child::tokio_command(Console::Hidden);
    #[cfg(unix)]
    cmd.args(["cat"]);
    #[cfg(windows)]
    cmd.args([crate::test_child::windows_more()]);
    cmd.stdin(crate::Stdio::pipe()).expect("set stdin pipe");
    #[cfg(windows)]
    cmd.stdout(crate::Stdio::null()).expect("set stdout null");
    cmd.contain_with(crate::ContainMode::TreeWalk);
    let mut child = cmd.spawn().expect("spawn");
    // Closed right after the kill: a no-op backstop exits 0.
    let stdin = child.stdin().expect("piped stdin");
    fault::set_force_root_kill_noop(true);
    let result = child.kill_tree();
    assert!(
        !fault::armed(),
        "seam not consumed — hard_kill did not run on the arming thread"
    );
    result.expect("kill_tree via backstop");
    drop(stdin);
    let status = child.wait().await.expect("reap");
    assert!(
        !status.success(),
        "the handle backstop must be what killed the root, got {status:?}"
    );
}

/// A `TreeWalk` walk that killed what it could and left a member is not a refusal to walk: the
/// handle backstop still runs and kills the root. A walk that left only the root is settled by that
/// backstop (`Ok`, the tree marked killed); one that left a descendant is still the error naming it.
/// (macOS `TreeWalk` mode is an fd marker, which has no such walk. The fault seam is Unix's; the
/// settling code is shared.)
/// Mutants: the incomplete walk's error is taken for a refusal to walk, so `kill_tree` returns it
/// without the backstop; the backstop's kill of the root does not settle a walk that left only the
/// root; it settles a walk that left a descendant.
#[cfg(all(unix, not(target_os = "macos")))]
#[skuld::test]
fn sync_kill_tree_after_an_incomplete_walk_still_runs_the_handle_backstop() {
    use super::super::treewalk::fault::{self, LeftBehind};
    for left in [LeftBehind::Root, LeftBehind::Descendant, LeftBehind::RootAndDescendant] {
        let mut cmd = crate::test_child::command(Console::Hidden);
        cmd.args(["cat"]);
        cmd.stdin(crate::Stdio::pipe()).expect("set stdin pipe");
        cmd.contain_with(crate::ContainMode::TreeWalk);
        let mut child = cmd.spawn().expect("spawn");
        // Closed right after the kill: a no-op backstop exits 0.
        let stdin = child.stdin().expect("piped stdin");
        fault::set_force_root_kill_noop(true);
        fault::force_incomplete_once(left);
        let result = child.kill_tree();
        match left {
            LeftBehind::Root => result.expect("only the root was left, and the backstop killed it"),
            LeftBehind::Descendant | LeftBehind::RootAndDescendant => {
                let err = result.expect_err("a descendant was left");
                assert!(matches!(err, crate::error::Error::MemberNotKilled { .. }), "{err:?}");
            }
        }
        drop(stdin);
        let status = child.wait().expect("reap");
        assert!(
            !status.success(),
            "{left:?}: the handle backstop must be what killed the root, got {status:?}"
        );
    }
}

#[cfg(all(unix, not(target_os = "macos"), feature = "tokio"))]
#[skuld::test]
async fn async_kill_tree_after_an_incomplete_walk_still_runs_the_handle_backstop() {
    use super::super::treewalk::fault::{self, LeftBehind};
    for left in [LeftBehind::Root, LeftBehind::Descendant, LeftBehind::RootAndDescendant] {
        let mut cmd = crate::test_child::tokio_command(Console::Hidden);
        cmd.args(["cat"]);
        cmd.stdin(crate::Stdio::pipe()).expect("set stdin pipe");
        cmd.contain_with(crate::ContainMode::TreeWalk);
        let mut child = cmd.spawn().expect("spawn");
        // Closed right after the kill: a no-op backstop exits 0.
        let stdin = child.stdin().expect("piped stdin");
        fault::set_force_root_kill_noop(true);
        fault::force_incomplete_once(left);
        let result = child.kill_tree();
        match left {
            LeftBehind::Root => result.expect("only the root was left, and the backstop killed it"),
            LeftBehind::Descendant | LeftBehind::RootAndDescendant => {
                let err = result.expect_err("a descendant was left");
                assert!(matches!(err, crate::error::Error::MemberNotKilled { .. }), "{err:?}");
            }
        }
        drop(stdin);
        let status = child.wait().await.expect("reap");
        assert!(
            !status.success(),
            "{left:?}: the handle backstop must be what killed the root, got {status:?}"
        );
    }
}

// What a tree kill comes to =====

/// `settle` over every kind of outcome, with and without a member or the root left, and with the
/// root not running or not: what it answers, and whether it marks the tree killed. A kill that did not
/// run marks nothing and answers with what its sweep by identity left.
///
/// A walk that saw the root running vouches for nothing, whatever ended the root afterwards (it may
/// have forked first), but the answer follows the root's end.
///
/// Mutants: a skipped kill marks; a skip drops its sweep's error; the root is dropped from the report
/// whatever the root's end; the root is kept when the root is not running; a root the backstop ended
/// marks the tree killed.
#[skuld::test]
fn settle_answers_and_marks_by_what_ran_what_was_left_and_the_root_s_end() {
    use super::{Attached, SettleOutcome, TreeKillOutcome, TreeKilled, Unkilled, UnvouchedKind};
    use crate::error::Error;
    let left = |pid| Error::MemberNotKilled {
        pid,
        source: Box::new(Error::Containment {
            detail: "refused".into(),
        }),
        note: None,
    };
    let unkilled = |members: &[u32], root: Option<u32>| Unkilled {
        members: members.iter().map(|pid| left(*pid)).collect(),
        root: root.map(left),
        resolved: Vec::new(),
    };
    let skipped = |holders| TreeKillOutcome::Skipped {
        action: "walk".into(),
        holders,
    };
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());

    // (outcome, root_ended, answer: Ok(outcome) or Err((first, others)), marked)
    type Answer = Result<SettleOutcome, (u32, Vec<u32>)>;
    let table: Vec<(TreeKillOutcome, bool, Answer, bool)> = vec![
        (TreeKillOutcome::Ran(Ok(())), false, Ok(SettleOutcome::Killed), true),
        (skipped(Ok(())), true, Ok(SettleOutcome::Skipped), false),
        (skipped(Err(unkilled(&[7], None))), true, Err((7, vec![])), false),
        (
            TreeKillOutcome::Ran(Err(unkilled(&[], Some(1)))),
            false,
            Err((1, vec![])),
            false,
        ),
        (
            TreeKillOutcome::Ran(Err(unkilled(&[], Some(1)))),
            true,
            Ok(SettleOutcome::Unvouched {
                kind: UnvouchedKind::RootSeenRunning,
            }),
            false,
        ),
        (
            TreeKillOutcome::Ran(Err(unkilled(&[2, 3], None))),
            true,
            Err((2, vec![3])),
            false,
        ),
        (
            TreeKillOutcome::Ran(Err(unkilled(&[2], Some(1)))),
            true,
            Err((2, vec![])),
            false,
        ),
        (
            TreeKillOutcome::Ran(Err(unkilled(&[2], Some(1)))),
            false,
            Err((2, vec![1])),
            false,
        ),
    ];
    let pid_of = |error: &Error| match error {
        Error::MemberNotKilled { pid, .. } => *pid,
        other => panic!("expected MemberNotKilled, got {other:?}"),
    };
    for (outcome, root_ended, want, marked) in table {
        let label = format!("{outcome:?}, root_ended {root_ended}");
        let killed = TreeKilled::default();

        let got = attached.settle(&killed, outcome, root_ended);

        let got = got.map_err(|left| (pid_of(&left.first), left.others.iter().map(pid_of).collect::<Vec<_>>()));
        assert_eq!(got, want, "{label}");
        assert_eq!(killed.is_set(), marked, "{label}");
    }
}

/// A walk that was not made, or found no root, killed nothing: `settle` answers it with an error
/// (`Unassessable`; the snapshot's error as it was) and marks nothing, for a reader of any kind.
///
/// Mutants: either is answered `Ok`; either marks the tree killed.
#[skuld::test]
fn settle_answers_a_walk_that_was_not_made_with_an_error_and_marks_nothing() {
    use super::{Attached, TreeKillOutcome, TreeKilled};
    use crate::error::Error;
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    for outcome in [
        TreeKillOutcome::NoRoot,
        TreeKillOutcome::Unwalked(Error::Unassessable {
            detail: "the snapshot could not be taken".into(),
            source: None,
        }),
    ] {
        let label = format!("{outcome:?}");
        let killed = TreeKilled::default();

        let left = attached
            .settle(&killed, outcome, true)
            .expect_err("nothing was killed, whatever became of the root");

        assert!(matches!(left.first, Error::Unassessable { .. }), "{label}: {left}");
        assert!(left.others.is_empty(), "{label}: {left}");
        assert!(!killed.is_set(), "{label}");
    }
}

/// A root that is gone leaves nothing to walk once this handle has killed the tree: a later kill or
/// drop that finds no root is settled, not an error. Mutant: it is `Unassessable` whatever the mark
/// says (the drop after a `kill_tree` and a `wait` then warns, on Windows).
#[skuld::test]
fn settle_answers_a_gone_root_as_killed_once_the_handle_killed_the_tree() {
    use super::{Attached, SettleOutcome, TreeKillOutcome, TreeKilled};
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    let killed = TreeKilled::default();
    attached
        .settle(&killed, TreeKillOutcome::Ran(Ok(())), false)
        .expect("the tree is killed");

    let later = attached
        .settle(&killed, TreeKillOutcome::NoRoot, false)
        .expect("the tree was killed already");

    assert_eq!(later, SettleOutcome::Killed);
}

// The tree's settlement: every state, every outcome =====

/// Where a tree's settlement stands: never settled, killed, left these members (their pids, in
/// order), or unvouched, with the kind it stores.
#[derive(Debug, PartialEq)]
enum Standing {
    Never,
    Killed,
    Left(Vec<u32>),
    Unvouched(super::UnvouchedKind),
}

/// The state of `killed`.
fn standing(killed: &super::TreeKilled) -> Standing {
    standing_of(&killed.lock())
}

/// `state`, as a `Standing`.
fn standing_of(state: &super::TreeState) -> Standing {
    use crate::containment::treewalk::Left;
    match state {
        super::TreeState::Never => Standing::Never,
        super::TreeState::Killed => Standing::Killed,
        super::TreeState::Unvouched(kind) => Standing::Unvouched(*kind),
        super::TreeState::Left(held) => {
            assert!(!held.is_empty(), "a tree left holds a member");
            Standing::Left(
                held.iter()
                    .map(|left| match left {
                        Left::Resolved(id) => id.pid(),
                        Left::Unread { pid, .. } => *pid,
                    })
                    .collect(),
            )
        }
    }
}

/// `next` over every state and every combination of facts that can occur: a walk or not, members left
/// with identities or none, something left running or not, the root seen running or not. A member
/// left with an identity is something left running; the root seen running is a walk's fact. The
/// state it answers is whole, the kind included: the latest fact names the kind (the root seen
/// running, then held members all gone, then something else left with no identity), and a settlement
/// with no new fact keeps the stored one.
///
/// Mutants: a fact that walked nothing vouches; a member seen running leaves a state as it was
/// (`Never` included: it means only that nothing has settled the tree); a held `Left` outlives its
/// members; an unvouched tree keeps its kind over a newer fact (a root seen running over
/// `HeldMembersGone`); a kind changes without a fact (a skipped kill, a gone root or a walk not made
/// over a stored kind).
#[skuld::test]
fn next_is_the_one_transition_of_state_and_facts() {
    use super::{TreeState, UnvouchedKind};
    use crate::containment::treewalk::Left;
    let unread = |pid| Left::Unread {
        pid,
        said: "its identity could not be read".into(),
    };
    let prev_names = [
        "Never",
        "Killed",
        "Left(7)",
        "Unvouched(HeldMembersGone)",
        "Unvouched(RootSeenRunning)",
        "Unvouched(Earlier)",
    ];
    let prev_at = |at: usize| match at {
        0 => TreeState::Never,
        1 => TreeState::Killed,
        2 => TreeState::Left(vec![unread(7)]),
        3 => TreeState::Unvouched(UnvouchedKind::HeldMembersGone),
        4 => TreeState::Unvouched(UnvouchedKind::RootSeenRunning),
        _ => TreeState::Unvouched(UnvouchedKind::Earlier),
    };
    // (label, walked, left_now, running, root_seen_running, next from each state of `prev_names`)
    type Row = (&'static str, bool, Vec<Left>, bool, bool, [Standing; 6]);
    let killed = || Standing::Killed;
    let left_8 = || Standing::Left(vec![8]);
    let held = || Standing::Unvouched(UnvouchedKind::HeldMembersGone);
    let root = || Standing::Unvouched(UnvouchedKind::RootSeenRunning);
    let earlier = || Standing::Unvouched(UnvouchedKind::Earlier);
    #[rustfmt::skip]
    let facts: Vec<Row> = vec![
        ("walk, nothing left", true, vec![], false, false,
            [killed(), killed(), killed(), killed(), killed(), killed()]),
        ("walk, something running with no identity", true, vec![], true, false,
            [earlier(), earlier(), earlier(), earlier(), earlier(), earlier()]),
        ("walk, the root seen running", true, vec![], true, true,
            [root(), root(), root(), root(), root(), root()]),
        ("no walk, nothing left (skipped kill, gone root, walk not made)", false, vec![], false, false,
            [Standing::Never, killed(), held(), held(), root(), earlier()]),
        ("no walk, something running with no identity", false, vec![], true, false,
            [earlier(), earlier(), earlier(), earlier(), earlier(), earlier()]),
        ("walk, member 8 left", true, vec![unread(8)], true, false,
            [left_8(), left_8(), left_8(), left_8(), left_8(), left_8()]),
        ("walk, member 8 left and the root seen running", true, vec![unread(8)], true, true,
            [left_8(), left_8(), left_8(), left_8(), left_8(), left_8()]),
        ("no walk, member 8 left", false, vec![unread(8)], true, false,
            [left_8(), left_8(), left_8(), left_8(), left_8(), left_8()]),
    ];
    for (label, walked, left_now, running, root_seen_running, wants) in facts {
        for (at, want) in wants.into_iter().enumerate() {
            let got = super::next(&prev_at(at), walked, left_now.clone(), running, root_seen_running);

            assert_eq!(standing_of(&got), want, "{} x {label}", prev_names[at]);
        }
    }
}

/// The five outcomes of the table, fresh.
fn table_outcomes() -> [(&'static str, super::TreeKillOutcome); 5] {
    use super::{TreeKillOutcome, Unkilled};
    [
        ("Ran(Ok)", TreeKillOutcome::Ran(Ok(()))),
        (
            "Ran(Err member 8)",
            TreeKillOutcome::Ran(Err(Unkilled {
                members: vec![member_left(8)],
                root: None,
                resolved: Vec::new(),
            })),
        ),
        ("NoRoot", TreeKillOutcome::NoRoot),
        (
            "Unwalked",
            TreeKillOutcome::Unwalked(crate::error::Error::Unassessable {
                detail: "the snapshot failed".into(),
                source: None,
            }),
        ),
        (
            "Skipped",
            TreeKillOutcome::Skipped {
                action: "walk the process table".into(),
                holders: Ok(()),
            },
        ),
    ]
}

/// A tree whose walk left the member `pid` (its identity read when `resolved`).
fn tree_left_with(pid: u32, resolved: Vec<crate::identity::ProcessId>) -> super::TreeKilled {
    use super::{Attached, TreeKillOutcome, TreeKilled, Unkilled};
    let killed = TreeKilled::default();
    Attached::TreeWalk(crate::identity::ProcessId::current())
        .settle(
            &killed,
            TreeKillOutcome::Ran(Err(Unkilled {
                members: vec![member_left(pid)],
                root: None,
                resolved,
            })),
            true,
        )
        .expect_err("a member was left");
    killed
}

/// What a `settle` answers, and the state it leaves.
type Want = (Result<super::SettleOutcome, Vec<String>>, Standing);

/// Each outcome of the table settled over a tree that `enter` makes fresh: the answer (the
/// errors' names, first to last) and the state left, against the row of `rows`. `enter` returns the
/// tree, the pid it holds (or 0), and what to run once the row has settled.
fn check_table(
    state: &str,
    rows: impl Fn(u32) -> [Want; 5],
    enter: impl Fn() -> (super::TreeKilled, u32, Box<dyn FnOnce()>),
) {
    use super::Attached;
    use crate::error::Error;
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    let name = |error: &Error| match error {
        Error::MemberNotKilled { pid, .. } => format!("pid {pid}"),
        Error::Unassessable { .. } => "unassessable".to_owned(),
        other => panic!("unexpected error {other:?}"),
    };
    for (at, (outcome_name, outcome)) in table_outcomes().into_iter().enumerate() {
        let (killed, pid, after) = enter();
        let (want_answer, want_standing) = rows(pid).into_iter().nth(at).expect("a row per outcome");

        let got = attached.settle(&killed, outcome, true).map_err(|left| {
            std::iter::once(&left.first)
                .chain(&left.others)
                .map(name)
                .collect::<Vec<_>>()
        });

        assert_eq!(got, want_answer, "{state} x {outcome_name}: answer");
        assert_eq!(standing(&killed), want_standing, "{state} x {outcome_name}: next state");
        after();
    }
}

fn gone() -> Vec<String> {
    vec!["unassessable".to_owned()]
}

/// A tree that holds the member `pid`, which no outcome shows killed: every answer starts with it, a
/// gone root adds nothing of its own, and the tree stays left.
fn holding_rows(pid: u32) -> [Want; 5] {
    let held = format!("pid {pid}");
    let two = vec![held.clone(), "pid 8".to_owned()];
    let walk_not_made = vec![held.clone(), "unassessable".to_owned()];
    [
        (Err(vec![held.clone()]), Standing::Left(vec![pid])),
        (Err(two), Standing::Left(vec![pid, 8])),
        (Err(vec![held.clone()]), Standing::Left(vec![pid])),
        (Err(walk_not_made), Standing::Left(vec![pid])),
        (Err(vec![held]), Standing::Left(vec![pid])),
    ]
}

/// `settle` over a tree no walk has settled, one that is killed, and one left an unread member: what
/// each outcome answers, and the state it leaves. The held member is answered first; a gone root adds
/// no error of its own once a walk covered the tree; a member seen running moves a killed tree to
/// left; the state is one value, so no row can be killed and holding at once.
///
/// Mutants: a gone root over a covered tree adds its own error; a tree stays killed while it holds a
/// member; an outcome skips the held member.
#[skuld::test]
fn settle_answers_each_outcome_over_a_tree_never_settled_killed_or_left_an_unread_member() {
    use super::{
        Attached,
        SettleOutcome::{Killed, Skipped},
        TreeKillOutcome, TreeKilled,
    };
    let nothing = || -> Box<dyn FnOnce()> { Box::new(|| {}) };

    check_table(
        "never settled",
        |_| {
            [
                (Ok(Killed), Standing::Killed),
                (Err(vec!["pid 8".to_owned()]), Standing::Left(vec![8])),
                (Err(gone()), Standing::Never),
                (Err(gone()), Standing::Never),
                (Ok(Skipped), Standing::Never),
            ]
        },
        || (TreeKilled::default(), 0, nothing()),
    );
    check_table(
        "killed",
        |_| {
            [
                (Ok(Killed), Standing::Killed),
                (Err(vec!["pid 8".to_owned()]), Standing::Left(vec![8])),
                (Ok(Killed), Standing::Killed),
                (Err(gone()), Standing::Killed),
                (Ok(Skipped), Standing::Killed),
            ]
        },
        || {
            let killed = TreeKilled::default();
            Attached::TreeWalk(crate::identity::ProcessId::current())
                .settle(&killed, TreeKillOutcome::Ran(Ok(())), true)
                .expect("a complete kill");
            (killed, 0, nothing())
        },
    );
    check_table("left, unread", holding_rows, || {
        (tree_left_with(7, Vec::new()), 7, nothing())
    });
}

/// A member whose kill by identity succeeds is gone after any outcome, but only an outcome that
/// walked shows the tree killed: the member ran on after its kill was refused and may have forked,
/// and killing it again does not reach its children. A gone root, a walk not made and a skipped kill
/// each walked nothing, so the answer is the outcome's own (a gone root says the tree could not be
/// walked) and the state is unvouched. Mutants: as the table above; a held member is not killed by
/// an outcome that did not run; an outcome that walked nothing settles the tree as killed or as
/// never settled.
#[skuld::test]
fn settle_answers_each_outcome_over_a_tree_left_a_member_that_can_be_killed() {
    use super::{
        SettleOutcome::{Killed, Unvouched},
        UnvouchedKind,
    };
    check_table(
        "left, killable",
        |_| {
            [
                (Ok(Killed), Standing::Killed),
                (Err(vec!["pid 8".to_owned()]), Standing::Left(vec![8])),
                (Err(gone()), Standing::Unvouched(UnvouchedKind::HeldMembersGone)),
                (Err(gone()), Standing::Unvouched(UnvouchedKind::HeldMembersGone)),
                (
                    Ok(Unvouched {
                        kind: UnvouchedKind::HeldMembersGone,
                    }),
                    Standing::Unvouched(UnvouchedKind::HeldMembersGone),
                ),
            ]
        },
        || {
            let (blocker, stdin) = crate::test_child::held_uncontained_blocker(false);
            let id = blocker.id().expect(crate::test_child::IDENTITY_READ);
            let killed = tree_left_with(id.pid(), vec![id]);
            let after: Box<dyn FnOnce()> = Box::new(move || {
                drop(stdin);
                crate::test_child::assert_killed("the member", blocker.wait().expect("reap the member"));
            });
            (killed, id.pid(), after)
        },
    );
}

/// A tree whose walk left a member that a gone root's settlement then killed again: unvouched. The
/// returned closure reaps the member, which is held by `stdin` until then.
fn unvouched_tree() -> (super::TreeKilled, Box<dyn FnOnce()>) {
    use super::{Attached, TreeKillOutcome};
    let (blocker, stdin) = crate::test_child::held_uncontained_blocker(false);
    let id = blocker.id().expect(crate::test_child::IDENTITY_READ);
    let killed = tree_left_with(id.pid(), vec![id]);
    Attached::TreeWalk(crate::identity::ProcessId::current())
        .settle(&killed, TreeKillOutcome::NoRoot, true)
        .expect_err("a gone root walked nothing");
    assert_eq!(
        standing(&killed),
        Standing::Unvouched(super::UnvouchedKind::HeldMembersGone)
    );
    let after: Box<dyn FnOnce()> = Box::new(move || {
        drop(stdin);
        crate::test_child::assert_killed("the member", blocker.wait().expect("reap the member"));
    });
    (killed, after)
}

/// A tree whose every member a walk left is gone, and no walk since has vouched for it, is
/// unvouched: only a walk that leaves nothing settles it as killed, a member seen running leaves it
/// holding that member, and any other outcome leaves it unvouched, a gone root answering with the
/// honest error each time. Mutants: an outcome that walked nothing settles it as killed or as never
/// settled; a walk that left nothing does not settle it as killed.
#[skuld::test]
fn settle_answers_each_outcome_over_an_unvouched_tree() {
    use super::{
        SettleOutcome::{Killed, Unvouched},
        UnvouchedKind,
    };
    check_table(
        "unvouched",
        |_| {
            [
                (Ok(Killed), Standing::Killed),
                (Err(vec!["pid 8".to_owned()]), Standing::Left(vec![8])),
                (Err(gone()), Standing::Unvouched(UnvouchedKind::HeldMembersGone)),
                (Err(gone()), Standing::Unvouched(UnvouchedKind::HeldMembersGone)),
                (
                    Ok(Unvouched {
                        kind: UnvouchedKind::HeldMembersGone,
                    }),
                    Standing::Unvouched(UnvouchedKind::HeldMembersGone),
                ),
            ]
        },
        || {
            let (killed, after) = unvouched_tree();
            (killed, 0, after)
        },
    );
}

/// A member whose kill by identity is refused stays left after any outcome. A setuid-root helper
/// refuses the kill of its caller.
#[cfg(unix)]
#[skuld::test]
fn settle_answers_each_outcome_over_a_tree_left_a_member_that_refuses_the_kill(
    #[fixture(setuid)] _group: &crate::test_groups::Group,
) {
    check_table("left, refusing", holding_rows, || {
        let (child, stdin) = crate::test_privilege::spawn_setuid_blocker();
        let id = child.id().expect(crate::test_child::IDENTITY_READ);
        let killed = tree_left_with(id.pid(), vec![id]);
        let after: Box<dyn FnOnce()> = Box::new(move || {
            // The helper exits at EOF: nothing killed it.
            drop(stdin);
            assert!(child.wait().expect("the helper exits on EOF").success());
        });
        (killed, id.pid(), after)
    });
}

/// A handle that is not a `TreeWalk` holds no members, so a kill that runs and leaves a member leaves
/// the tree unvouched (a kill happened, and the member seen running may have forked), whether the tree
/// was killed or never settled. Mutants: the tree stays killed; it stays or goes to never settled,
/// after which a gone root would say nothing was killed.
#[skuld::test]
fn a_kill_that_leaves_a_member_after_a_killed_tree_of_a_handle_that_is_not_a_walk_unvouches_it() {
    use super::{Attached, TreeKillOutcome, TreeKilled, Unkilled, UnvouchedKind};
    let attached = Attached::None;
    let killed = TreeKilled::default();
    attached
        .settle(&killed, TreeKillOutcome::Ran(Ok(())), true)
        .expect("a complete kill");
    assert_eq!(standing(&killed), Standing::Killed);

    let left = attached
        .settle(
            &killed,
            TreeKillOutcome::Ran(Err(Unkilled {
                members: vec![member_left(8)],
                root: None,
                resolved: Vec::new(),
            })),
            true,
        )
        .expect_err("a member was left");

    assert_eq!(pid_left(&left.first), 8);
    assert_eq!(standing(&killed), Standing::Unvouched(UnvouchedKind::Earlier));

    // The same for a skipped kill whose sweep of the holders left a member.
    attached
        .settle(&killed, TreeKillOutcome::Ran(Ok(())), true)
        .expect("a complete kill");
    assert_eq!(standing(&killed), Standing::Killed);
    let skipped_with_a_member = || TreeKillOutcome::Skipped {
        action: "walk".into(),
        holders: Err(Unkilled {
            members: vec![member_left(9)],
            root: None,
            resolved: Vec::new(),
        }),
    };
    attached
        .settle(&killed, skipped_with_a_member(), true)
        .expect_err("a member was left");
    assert_eq!(standing(&killed), Standing::Unvouched(UnvouchedKind::Earlier));

    // Over a tree never settled, the sweep that left the member unvouches it too.
    let never = TreeKilled::default();
    attached
        .settle(&never, skipped_with_a_member(), true)
        .expect_err("a member was left");
    assert_eq!(standing(&never), Standing::Unvouched(UnvouchedKind::Earlier));
}

// What a `TreeWalk` handle remembers of the members a kill left =====

/// A `MemberNotKilled` for `pid`, as a walk reports a member it left.
fn member_left(pid: u32) -> crate::error::Error {
    crate::error::Error::MemberNotKilled {
        pid,
        source: Box::new(crate::error::Error::Unassessable {
            detail: "its identity could not be read".into(),
            source: None,
        }),
        note: None,
    }
}

/// The pid a `MemberNotKilled` names.
fn pid_left(error: &crate::error::Error) -> u32 {
    match error {
        crate::error::Error::MemberNotKilled { pid, .. } => *pid,
        other => panic!("expected MemberNotKilled, got {other:?}"),
    }
}

/// A member whose identity was never read stays left: a later kill whose walk finds nothing does not
/// settle the tree as killed, but answers with the same member and marks nothing. Once a later kill
/// names it itself, it is not repeated.
///
/// Mutants: a later walk that finds less settles the tree as killed; the member is forgotten after
/// one report; the member is reported twice when the walk names it again.
#[skuld::test]
fn a_member_whose_identity_was_never_read_is_reported_again_until_a_kill_shows_it_killed() {
    use super::{Attached, TreeKillOutcome, TreeKilled, Unkilled};
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    let killed = TreeKilled::default();
    let left = |pids: &[u32]| {
        TreeKillOutcome::Ran(Err(Unkilled {
            members: pids.iter().copied().map(member_left).collect(),
            root: None,
            resolved: Vec::new(),
        }))
    };

    let first = attached
        .settle(&killed, left(&[7]), true)
        .expect_err("a member was left");
    assert_eq!(pid_left(&first.first), 7);

    let later = attached
        .settle(&killed, TreeKillOutcome::Ran(Ok(())), true)
        .expect_err("a walk that finds nothing does not show the member killed");
    assert_eq!((pid_left(&later.first), later.others.len()), (7, 0));
    assert!(!killed.is_set());

    let named_again = attached
        .settle(&killed, left(&[7, 8]), true)
        .expect_err("two members are left");
    assert_eq!(pid_left(&named_again.first), 7);
    assert_eq!(named_again.others.iter().map(pid_left).collect::<Vec<_>>(), [8]);
}

/// Whatever the next kill found, a held member is answered first: a gone root, a walk that could not
/// be made and a skipped kill each leave it reported, and none marks the tree. Mutant: the held
/// members are retried only when the new kill ran.
#[skuld::test]
fn a_held_member_is_answered_first_whatever_the_next_kill_found() {
    use super::{Attached, TreeKillOutcome, TreeKilled, Unkilled};
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    let unwalked = || {
        TreeKillOutcome::Unwalked(crate::error::Error::Unassessable {
            detail: "the snapshot failed".into(),
            source: None,
        })
    };
    let skipped = || TreeKillOutcome::Skipped {
        action: "walk the process table".into(),
        holders: Ok(()),
    };
    let outcomes: [(&str, &dyn Fn() -> TreeKillOutcome); 3] = [
        ("NoRoot", &|| TreeKillOutcome::NoRoot),
        ("Unwalked", &unwalked),
        ("Skipped", &skipped),
    ];
    for (label, outcome) in outcomes {
        let killed = TreeKilled::default();
        attached
            .settle(
                &killed,
                TreeKillOutcome::Ran(Err(Unkilled {
                    members: vec![member_left(7)],
                    root: None,
                    resolved: Vec::new(),
                })),
                true,
            )
            .expect_err("a member was left");

        let again = attached
            .settle(&killed, outcome(), true)
            .expect_err("the member is not shown killed");

        assert_eq!(pid_left(&again.first), 7, "{label}: {again}");
        assert!(!killed.is_set(), "{label}");
    }
}

/// A tree this handle already killed does not excuse a gone root from answering the member a later
/// kill left: the mark shortcut applies only when nothing is held. Mutant: `NoRoot` with the mark set
/// returns `Killed` before the held members are read.
#[skuld::test]
fn a_gone_root_over_a_marked_tree_still_answers_a_held_member() {
    use super::{Attached, SettleOutcome, TreeKillOutcome, TreeKilled, Unkilled};
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    let killed = TreeKilled::default();
    attached
        .settle(&killed, TreeKillOutcome::Ran(Ok(())), true)
        .expect("a complete kill");
    assert!(killed.is_set());
    attached
        .settle(
            &killed,
            TreeKillOutcome::Ran(Err(Unkilled {
                members: vec![member_left(7)],
                root: None,
                resolved: Vec::new(),
            })),
            true,
        )
        .expect_err("a later kill left a member");

    let again = attached
        .settle(&killed, TreeKillOutcome::NoRoot, true)
        .expect_err("the member is not shown killed");
    assert_eq!(pid_left(&again.first), 7, "{again}");

    let nothing_held = TreeKilled::default();
    attached
        .settle(&nothing_held, TreeKillOutcome::Ran(Ok(())), true)
        .expect("a complete kill");
    assert!(matches!(
        attached.settle(&nothing_held, TreeKillOutcome::NoRoot, true),
        Ok(SettleOutcome::Killed)
    ));
}

/// The early return of both `kill_tree`s: a walk not made is answered here, over a held member, with
/// the tree unmarked; every other walk is handed back untouched for the backstop.
#[skuld::test]
fn only_a_walk_not_made_is_answered_before_the_backstop() {
    use super::{Attached, TreeKillOutcome, TreeKilled, Unkilled};
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    let killed = TreeKilled::default();
    attached
        .settle(
            &killed,
            TreeKillOutcome::Ran(Err(Unkilled {
                members: vec![member_left(7)],
                root: None,
                resolved: Vec::new(),
            })),
            true,
        )
        .expect_err("a member was left");

    let handed_back = attached.unless_unwalked("kill_tree", &killed, TreeKillOutcome::Ran(Ok(())));
    assert!(
        matches!(handed_back, Ok(TreeKillOutcome::Ran(Ok(())))),
        "a walk made is the backstop's to settle"
    );

    let Err(failure) = attached.unless_unwalked(
        "kill_tree",
        &killed,
        TreeKillOutcome::Unwalked(crate::error::Error::Unassessable {
            detail: "the snapshot failed".into(),
            source: None,
        }),
    ) else {
        panic!("a walk not made is an error");
    };
    assert_eq!(pid_left(&failure.error), 7, "{:?}", failure.error);
    assert!(!failure.root_ended);
    assert!(!killed.is_set());
}

/// A member whose identity was read and whose kill was refused is killed by identity again by the
/// next kill that runs, and the tree is then settled and marked. Mutants: the retry does not kill it;
/// it kills it and still answers with the earlier error.
#[skuld::test]
fn a_member_whose_kill_was_refused_is_killed_again_by_identity_and_then_settled() {
    use super::{Attached, TreeKillOutcome, TreeKilled, Unkilled};
    let (blocker, stdin) = crate::test_child::held_uncontained_blocker(false);
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    let killed = TreeKilled::default();
    let id = blocker.id().expect(crate::test_child::IDENTITY_READ);

    let first = attached
        .settle(
            &killed,
            TreeKillOutcome::Ran(Err(Unkilled {
                members: vec![member_left(id.pid())],
                root: None,
                resolved: vec![id],
            })),
            true,
        )
        .expect_err("the member was left");
    assert_eq!(pid_left(&first.first), id.pid());
    assert!(!killed.is_set());

    attached
        .settle(&killed, TreeKillOutcome::Ran(Ok(())), true)
        .expect("the retry killed the member by its identity");

    assert!(killed.is_set());
    // Released after the kill: a retry that sent nothing lets the member exit on EOF, so the
    // assertion fails instead of the wait hanging.
    drop(stdin);
    crate::test_child::assert_killed("the member", blocker.wait().expect("reap the member"));
}

/// A gone root over members an earlier kill left, which are gone now, says so each time, in the same
/// words: the tree could not be walked, but "nothing was killed" and "killed again" are each false for
/// a member that exited on its own. `exits_on_its_own`: the member exits at EOF before the
/// settlement instead of being killed by it.
fn gone_root_answers_over_gone_members_each_time(exits_on_its_own: bool) {
    use super::{Attached, TreeKillOutcome, TreeKilled, Unkilled};
    let (blocker, stdin) = crate::test_child::held_uncontained_blocker(false);
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    let killed = TreeKilled::default();
    let id = blocker.id().expect(crate::test_child::IDENTITY_READ);
    attached
        .settle(
            &killed,
            TreeKillOutcome::Ran(Err(Unkilled {
                members: vec![member_left(id.pid())],
                root: None,
                resolved: vec![id],
            })),
            true,
        )
        .expect_err("the member was left");
    let mut held = Some(stdin);
    if exits_on_its_own {
        drop(held.take());
        blocker.wait().expect("the member exits on its own and is reaped");
    }

    let texts: Vec<String> = (0..2)
        .map(|_| {
            let left = attached
                .settle(&killed, TreeKillOutcome::NoRoot, true)
                .expect_err("a gone root walked nothing");
            assert!(left.others.is_empty(), "{left}");
            assert_eq!(
                standing(&killed),
                Standing::Unvouched(super::UnvouchedKind::HeldMembersGone)
            );
            left.first.to_string()
        })
        .collect();

    if !exits_on_its_own {
        drop(held.take());
        crate::test_child::assert_killed("the member", blocker.wait().expect("reap the member"));
    }
    assert_eq!(texts[0], texts[1]);
    for text in &texts {
        assert!(text.contains("are gone"), "{text}");
        assert!(!text.contains("nothing was killed"), "{text}");
        assert!(!text.contains("killed again"), "{text}");
    }
}

/// Mutant: the answer is `no_root_error`, or says the members were killed again, or changes with the
/// second call.
#[skuld::test]
fn a_gone_root_over_members_killed_by_the_settlement_says_they_are_gone_each_time() {
    gone_root_answers_over_gone_members_each_time(false);
}

/// Mutant: the answer says the members were killed.
#[skuld::test]
fn a_gone_root_over_a_member_that_exited_on_its_own_says_it_is_gone_each_time() {
    gone_root_answers_over_gone_members_each_time(true);
}

/// A drop that skips the kill over a tree it cannot vouch for warns of it, in words true of what
/// `settle` found: members an earlier kill left that are gone now (a held member that exited or was
/// killed by the settlement), or, when the cause is no longer known (members never identified, or
/// members since gone), only that something was left running and no walk
/// has checked what it forked. A tree that was killed, or never settled (a skipped kill has its own
/// warn), says nothing here.
///
/// Mutants: the drop is silent over an unvouched tree; a text speaks of members after a walk left only
/// the root; a text says "no longer tracked" of members known gone.
#[skuld::test]
fn a_drop_over_an_unvouched_tree_warns_of_what_settle_found() {
    use super::{Attached, TreeKillOutcome, TreeKilled, Unkilled};
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    let skipped = || TreeKillOutcome::Skipped {
        action: "walk the process table".into(),
        holders: Ok(()),
    };

    // Held members that the settlement found gone.
    let (blocker, stdin) = crate::test_child::held_uncontained_blocker(false);
    let id = blocker.id().expect(crate::test_child::IDENTITY_READ);
    let held_gone = tree_left_with(id.pid(), vec![id]);
    let warn = attached
        .settle_for_drop(&held_gone, skipped(), true)
        .expect("an unvouched tree is warned of");
    drop(stdin);
    crate::test_child::assert_killed("the member", blocker.wait().expect("reap the member"));
    assert!(warn.contains("could not be vouched for"), "{warn}");
    assert!(warn.contains("skipped the kill"), "{warn}");
    assert!(warn.contains("are gone"), "{warn}");
    assert!(!warn.contains("no longer tracked"), "{warn}");

    // Something left running that no settlement knows more of: a member of a handle that is not a walk,
    // which holds no identity for it.
    let root_left = || {
        TreeKillOutcome::Ran(Err(Unkilled {
            members: Vec::new(),
            root: Some(member_left(1)),
            resolved: Vec::new(),
        }))
    };
    let earlier = TreeKilled::default();
    Attached::None
        .settle(
            &earlier,
            TreeKillOutcome::Ran(Err(Unkilled {
                members: vec![member_left(8)],
                root: None,
                resolved: Vec::new(),
            })),
            true,
        )
        .expect_err("a member is running");
    let warn = attached
        .settle_for_drop(&earlier, skipped(), true)
        .expect("an unvouched tree is warned of");
    assert!(warn.contains("could not be vouched for"), "{warn}");
    assert!(warn.contains("skipped the kill"), "{warn}");
    assert!(warn.contains("left something running that it could not name"), "{warn}");
    assert!(!warn.contains("earlier kill"), "{warn}");
    assert!(!warn.contains("members"), "{warn}");

    // A walk that saw the root running and left it, whose end the call did not see: the root was seen
    // running, whatever the kind the tree had.
    let root_running = TreeKilled::default();
    attached
        .settle(&root_running, root_left(), false)
        .expect_err("the root is running");
    let warn = attached
        .settle_for_drop(&root_running, skipped(), true)
        .expect("an unvouched tree is warned of");
    assert!(warn.contains("found the root running"), "{warn}");
    assert!(!warn.contains("had ended"), "{warn}");
    assert!(!warn.contains("members"), "{warn}");

    // A walk that left only the root, then a `kill_tree` that answered `Ok`: the drop says what that
    // call found, every time, not that it skipped a kill over something an earlier kill left.
    let root_only = TreeKilled::default();
    attached.settle(&root_only, root_left(), true).expect("the root ended");
    for _ in 0..2 {
        let warn = attached
            .settle_for_drop(&root_only, skipped(), true)
            .expect("an unvouched tree is warned of");
        assert!(warn.contains("could not be vouched for"), "{warn}");
        assert!(warn.contains("found the root running"), "{warn}");
        assert!(warn.contains("never looked for"), "{warn}");
        assert!(!warn.contains("had ended"), "{warn}");
        assert!(!warn.contains("members"), "{warn}");
        assert!(!warn.contains("skipped the kill"), "{warn}");
        assert!(!warn.contains("earlier kill"), "{warn}");
    }

    // The drop's own walk saw the root running, and its root kill ended it: no later settlement
    // warns, so this one does.
    let warn = attached
        .settle_for_drop(
            &TreeKilled::default(),
            TreeKillOutcome::Ran(Err(Unkilled {
                members: Vec::new(),
                root: Some(member_left(1)),
                resolved: Vec::new(),
            })),
            true,
        )
        .expect("a walk that left the root is warned of");
    assert!(warn.contains("could not be vouched for"), "{warn}");
    assert!(warn.contains("found the root running"), "{warn}");
    assert!(warn.contains("never looked for"), "{warn}");
    assert!(!warn.contains("had ended"), "{warn}");
    assert!(!warn.contains("members"), "{warn}");
    assert!(!warn.contains("skipped the kill"), "{warn}");

    let killed = TreeKilled::default();
    attached
        .settle(&killed, TreeKillOutcome::Ran(Ok(())), true)
        .expect("a complete kill");
    assert_eq!(attached.settle_for_drop(&killed, skipped(), true), None);
    assert_eq!(attached.settle_for_drop(&TreeKilled::default(), skipped(), true), None);
}

/// A walk that saw the root running and left it un-vouches the tree, from any state and whatever
/// ended the root afterwards (the root may have forked between the snapshot and the backstop): a kill
/// happened, so a gone root afterwards says the tree could not be walked, not that nothing was
/// killed. The answer follows the root's end: `Ok` once it ended, the root's error while it runs. The
/// kind the state stores is the root seen running, whatever the kind it had and whether or not the
/// root had ended: the latest fact names it, so a gone root afterwards says the root was found running,
/// not that members an earlier kill left are gone.
///
/// Mutants: the state goes to or stays never settled, after which a gone root says nothing was
/// killed; the state stays or becomes killed once the backstop ended the root; an unvouched tree keeps
/// its kind over the walk that saw the root running.
#[skuld::test]
fn a_walk_that_saw_the_root_running_unvouches_the_tree_whatever_ended_it_afterwards() {
    use super::{Attached, TreeKillOutcome, TreeKilled, Unkilled, UnvouchedKind};
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    let root_left = || {
        TreeKillOutcome::Ran(Err(Unkilled {
            members: Vec::new(),
            root: Some(member_left(1)),
            resolved: Vec::new(),
        }))
    };
    for start in ["never", "killed", "root seen running", "held members gone", "earlier"] {
        for root_ended in [false, true] {
            let label = format!("{start}, root_ended {root_ended}");
            let mut after: Box<dyn FnOnce()> = Box::new(|| {});
            let killed = match start {
                "held members gone" => {
                    let (killed, reap) = unvouched_tree();
                    after = reap;
                    killed
                }
                _ => TreeKilled::default(),
            };
            if matches!(start, "killed") {
                attached
                    .settle(&killed, TreeKillOutcome::Ran(Ok(())), true)
                    .expect("a complete kill");
            }
            if start == "root seen running" {
                attached
                    .settle(&killed, root_left(), false)
                    .expect_err("the root is left");
            }
            if start == "earlier" {
                Attached::None
                    .settle(
                        &killed,
                        TreeKillOutcome::Ran(Err(Unkilled {
                            members: vec![member_left(8)],
                            root: None,
                            resolved: Vec::new(),
                        })),
                        true,
                    )
                    .expect_err("a member is running");
            }
            let want_start = match start {
                "never" => Standing::Never,
                "killed" => Standing::Killed,
                "root seen running" => Standing::Unvouched(UnvouchedKind::RootSeenRunning),
                "held members gone" => Standing::Unvouched(UnvouchedKind::HeldMembersGone),
                _ => Standing::Unvouched(UnvouchedKind::Earlier),
            };
            assert_eq!(standing(&killed), want_start, "{label}: start");

            let answer = attached.settle(&killed, root_left(), root_ended);

            match answer {
                Ok(outcome) => {
                    assert!(root_ended, "{label}: the root is running");
                    assert_eq!(
                        outcome,
                        super::SettleOutcome::Unvouched {
                            kind: super::UnvouchedKind::RootSeenRunning
                        },
                        "{label}"
                    );
                }
                Err(left) => {
                    assert!(!root_ended, "{label}: the root ended");
                    assert_eq!(pid_left(&left.first), 1, "{label}");
                }
            }
            assert_eq!(
                standing(&killed),
                Standing::Unvouched(UnvouchedKind::RootSeenRunning),
                "{label}"
            );

            let want = "found running";
            let mut texts = Vec::new();
            for _ in 0..2 {
                let gone = attached
                    .settle(&killed, TreeKillOutcome::NoRoot, true)
                    .expect_err("a gone root walked nothing");
                let text = gone.first.to_string();
                assert!(!text.contains("nothing was killed"), "{label}: {gone}");
                assert!(!text.contains("members"), "{label}: {gone}");
                assert!(text.contains(want), "{label}: {gone}");
                assert_eq!(
                    standing(&killed),
                    Standing::Unvouched(UnvouchedKind::RootSeenRunning),
                    "{label}"
                );
                texts.push(text);
            }
            assert_eq!(texts[0], texts[1], "{label}");
            after();
        }
    }
}

/// A member whose identity was read and that has exited since stops failing the call: nothing is
/// signalled, and the tree is settled. Mutant: the earlier error is returned whatever became of the
/// member.
#[skuld::test]
fn a_member_that_exited_since_it_was_left_stops_failing_the_call() {
    use super::{Attached, TreeKillOutcome, TreeKilled, Unkilled};
    let (blocker, stdin) = crate::test_child::held_uncontained_blocker(false);
    let attached = Attached::TreeWalk(crate::identity::ProcessId::current());
    let killed = TreeKilled::default();
    let id = blocker.id().expect(crate::test_child::IDENTITY_READ);
    attached
        .settle(
            &killed,
            TreeKillOutcome::Ran(Err(Unkilled {
                members: vec![member_left(id.pid())],
                root: None,
                resolved: vec![id],
            })),
            true,
        )
        .expect_err("the member was left");
    drop(stdin);
    blocker.wait().expect("the member exits on its own and is reaped");

    attached
        .settle(&killed, TreeKillOutcome::Ran(Ok(())), true)
        .expect("a member that is gone is not left");

    assert!(killed.is_set());
}

// A tree call's report names the call =====

/// The members a call's walk left besides the first are warned of under the name of the call that
/// made it, not under `kill_tree`. Mutant: `SweepFailure::of` words every call as `kill_tree`.
#[skuld::test]
fn a_sweeps_report_of_the_other_members_it_left_names_its_call() {
    use super::{Attached, SweepFailure, TreeKillOutcome, TreeKilled, Unkilled};
    use crate::error::Error;
    let member = |pid| Error::MemberNotKilled {
        pid,
        source: Box::new(Error::Containment {
            detail: "refused".into(),
        }),
        note: None,
    };
    crate::log_capture::install();
    let mark = crate::log_capture::mark();
    let failure = SweepFailure::of(
        "graceful_shutdown_tree",
        &Attached::None,
        &TreeKilled::default(),
        TreeKillOutcome::Ran(Err(Unkilled {
            members: vec![member(1), member(2)],
            root: None,
            resolved: Vec::new(),
        })),
        Ok(()),
        false,
    )
    .expect_err("two members were left");
    assert!(
        matches!(failure.error, Error::MemberNotKilled { pid: 1, .. }),
        "{:?}",
        failure.error
    );
    let warned = crate::log_capture::records_since_on_current_thread(mark, "pid 2 was not killed");
    assert_eq!(warned.len(), 1, "{warned:?}");
    assert!(warned[0].1.starts_with("graceful_shutdown_tree: "), "{warned:?}");
}

// fd marker install policy (macOS) =====

/// The install policy, exhaustively: every mode installs a marker for a ROOT spawn, no
/// nested spawn ever does (it inherits the root's), no uncontained spawn does, and an
/// elevation-derived spawn does not (its `sudo` wrapper closes every descriptor >= 3).
#[cfg(target_os = "macos")]
#[skuld::test]
fn marker_wanted_installs_for_every_contained_root_and_nothing_else() {
    use crate::containment::fdmarker::marker_wanted;
    use crate::containment::ContainMode;
    for mode in [ContainMode::Strongest, ContainMode::Session, ContainMode::TreeWalk] {
        assert!(
            marker_wanted(Some(mode), true, false),
            "{mode:?} root installs a marker"
        );
        assert!(
            !marker_wanted(Some(mode), false, false),
            "{mode:?} nested inherits, never installs"
        );
        assert!(
            !marker_wanted(Some(mode), true, true),
            "{mode:?} elevated root must not install"
        );
    }
    assert!(
        !marker_wanted(None, true, false),
        "an uncontained spawn installs no marker"
    );
}

/// The marker mechanism is actionable: `kill_tree`/`terminate_tree` act on it.
#[cfg(target_os = "macos")]
#[skuld::test]
fn attached_fd_marker_is_actionable() {
    use std::os::fd::{AsFd, AsRawFd};

    use super::Attached;
    let (read, write) = std::io::pipe().expect("pipe");
    let handle = crate::containment::fdmarker::pipe_handle_of(write.as_fd()).expect("handle");
    let read_handle = crate::containment::fdmarker::pipe_handle_of(read.as_fd()).expect("read handle");
    let prepared = crate::containment::fdmarker::PreparedMarker {
        read: std::os::fd::OwnedFd::from(read),
        handle,
        read_handle,
        fd: write.as_fd().as_raw_fd(),
    };
    let marker = crate::containment::fdmarker::Marker::new(
        prepared,
        crate::containment::fdmarker::fdmarker_tests::inert_root(),
        None,
    );
    assert!(Attached::FdMarker(marker).is_actionable());
}

/// `prepare` must thread the caller's reserved child fds through to the placement, or a user
/// fd mapping would dup2 over the marker in the child.
#[cfg(target_os = "macos")]
#[skuld::test]
fn prepare_places_the_marker_above_the_callers_reserved_fds() {
    use crate::containment::{ContainMode, ContainRequest, Nesting};
    // `prepare` makes the marker pipe, which a spawn does under `spawn_lock`.
    let _lock = crate::child::spawn::spawn_lock();
    let mut cmd = std::process::Command::new("/usr/bin/true");
    let prepared = super::prepare(
        &mut cmd,
        &mut crate::child::spawn::in_child::InChild::default(),
        &ContainRequest {
            mode: Some(ContainMode::Strongest),
            nesting: Nesting::Mark,
        },
        &crate::command::flags::FlagsRequest::default(),
        &[3, 4, 5, 6, 7, 8, 9, 10],
        false,
        &[],
    )
    .expect("prepare is infallible off Windows");
    let marker = prepared.marker.as_ref().expect("a contained macOS root gets a marker");
    assert!(
        marker.fd > 10,
        "prepare placed the marker on fd {}, which a user mapping would dup2 over",
        marker.fd
    );
}

/// A failed install degrades to the pre-existing mechanism rather than failing the spawn.
#[cfg(target_os = "macos")]
#[skuld::test]
fn a_failed_marker_install_leaves_prepare_without_one() {
    use crate::containment::fdmarker::fault::{lock_for_log_assertion, set_fault, Fault};
    use crate::containment::{ContainMode, ContainRequest, Nesting};
    // This test doesn't assert on log content itself, but triggering `Fault::Pipe` DOES emit
    // "fd marker: pipe() failed" into the shared `log_capture` buffer once any test has called
    // `log_capture::install()` — held for the whole body so it cannot land inside the sibling
    // fd-marker test module's fault-injection log-scanning test's scanned window on another
    // thread.
    let _serialize = lock_for_log_assertion();
    let mut cmd = std::process::Command::new("/usr/bin/true");
    set_fault(Some(Fault::Pipe));
    let prepared = super::prepare(
        &mut cmd,
        &mut crate::child::spawn::in_child::InChild::default(),
        &ContainRequest {
            mode: Some(ContainMode::Strongest),
            nesting: Nesting::Mark,
        },
        &crate::command::flags::FlagsRequest::default(),
        &[],
        false,
        &[],
    )
    .expect("prepare is infallible off Windows");
    assert!(prepared.marker.is_none(), "the forced failure must yield no marker");
}

/// An elevation-derived spawn must not install a marker: `sudo` closes every descriptor >= 3.
#[cfg(target_os = "macos")]
#[skuld::test]
fn prepare_installs_no_marker_for_an_elevation_derived_spawn() {
    use crate::containment::{ContainMode, ContainRequest, Nesting};
    let mut cmd = std::process::Command::new("/usr/bin/true");
    let prepared = super::prepare(
        &mut cmd,
        &mut crate::child::spawn::in_child::InChild::default(),
        &ContainRequest {
            mode: Some(ContainMode::Strongest),
            nesting: Nesting::Mark,
        },
        &crate::command::flags::FlagsRequest::default(),
        &[],
        true,
        &[],
    )
    .expect("prepare is infallible off Windows");
    assert!(
        prepared.marker.is_none(),
        "an elevated spawn must not claim marker containment"
    );
}

/// The spawn's identity read tells an OS refusal from a vanished pid: denial must not read as
/// vanishing, and absence must not read as denial.
#[cfg(windows)]
#[skuld::test]
fn spawn_identity_distinguishes_a_denied_pid_from_a_vanished_one() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(spawn_identity_distinguishes_a_denied_pid_from_a_vanished_one),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    use crate::child::spawn::{resolve_identity_unchecked, spawn_identity_error};
    use windows::Win32::System::Threading::PROCESS_SYNCHRONIZE;
    let child = crate::identity::windows_fixture::spawn_restricted(&done, PROCESS_SYNCHRONIZE.0);
    assert!(child.is_running(), "precondition: the subject must be live");

    let (read, cause) = resolve_identity_unchecked(&crate::identity::HostKernel, child.pid());
    let denied = spawn_identity_error(read, cause);
    assert!(
        matches!(denied, crate::error::Error::Unassessable { .. }),
        "a pid we may not query must not read as vanished: {denied:?}"
    );

    // Contrast: a pid no process holds.
    let (read, cause) = resolve_identity_unchecked(&crate::identity::HostKernel, 0xFFFF_FFF0);
    let absent = spawn_identity_error(read, cause);
    assert!(
        matches!(&absent, crate::error::Error::Io(e) if e.to_string().contains("reaped by another party")),
        "absence must not read as denial: {absent:?}"
    );
}

#[cfg(target_os = "macos")]
#[skuld::test]
fn wait_drained_is_unsupported_without_a_marker() {
    // Mirrors `require_contained`: a mechanism that cannot answer says so, rather than
    // implying a guarantee it has not got.
    use super::Attached;
    for attached in [Attached::None, Attached::Delegated] {
        let err = attached
            .wait_drained(Some(std::time::Instant::now()))
            .expect_err("a mechanism with no drain edge must be Unsupported");
        assert!(matches!(err, crate::error::Error::Unsupported { .. }), "got {err:?}");
    }
}

#[cfg(target_os = "macos")]
#[skuld::test]
fn wait_drained_reports_members_remain_then_all_markers_closed() {
    use super::Attached;
    use crate::containment::fdmarker::{pipe_handle_of, Marker, PreparedMarker};
    use crate::containment::TreeDrain;
    use std::os::fd::{AsFd, OwnedFd};

    // A real holder in a separate process, mirroring marker_eof_tests.rs's
    // spawn_marker_holder: the marker's write end lives in the child, not in-process, so
    // wait_drained observes a genuinely open, then genuinely closed, write end.
    let mut cmd = crate::test_child::command(Console::Hidden);
    cmd.executable("/bin/sh").args(["sh", "-c", "exec cat >/dev/null"]);
    cmd.fd(0, crate::Stdio::pipe_in()).expect("stdin pipe");
    cmd.fd(3, crate::Stdio::pipe_out()).expect("marker pipe");
    let mut child = cmd.spawn().expect("spawn /bin/sh");
    let marker = child.fd_read_end(3.into()).expect("marker read end");
    let stdin = child.fd_write_end(crate::Fd::STDIN).expect("stdin write end");

    // wait_drained now consults read_handle too (Marker::wait_drained calls
    // check_read_end_still_valid first, same as hard_kill/terminate) — so this uses the read
    // end's REAL current handle for both fields, standing in for install()'s full write-end
    // capture sequence without replicating it.
    let handle = pipe_handle_of(marker.as_fd()).expect("handle");
    let prepared = PreparedMarker {
        read: OwnedFd::from(marker),
        handle,
        read_handle: handle,
        fd: 3,
    };
    let attached = Attached::FdMarker(Marker::new(
        prepared,
        crate::containment::fdmarker::fdmarker_tests::inert_root(),
        None,
    ));

    assert_eq!(
        attached
            .wait_drained(Some(std::time::Instant::now()))
            .expect("bounded wait while the holder is alive"),
        TreeDrain::MembersRemain
    );

    drop(stdin); // cat's stdin EOFs, so cat exits, closing its inherited marker write end
    child.wait().expect("reap");

    assert_eq!(
        attached
            .wait_drained(None)
            .expect("unbounded wait after the holder exits"),
        TreeDrain::AllMarkersClosed
    );
}

/// The UAC-elevated attachment bypasses `mechanism_from_flags` entirely — nothing in the flag
/// matrix covers it — so its three fields are pinned here. `Unknown`, not the `None` a flagless
/// spawn yields nor the `OtherConsoleGroup` a suppressed one does: a copy-paste of either must
/// fail. What the dispatcher then does with the value is
/// `signal_refuses_a_child_cosca_did_not_create`, in `src/graceful_tests.rs`.
#[cfg(windows)]
#[skuld::test]
fn uac_elevated_attachment_has_no_in_process_route() {
    use super::{Attached, Attachment};
    use crate::containment::Containment;
    use crate::graceful::GracefulMechanism;

    let a = Attachment::uac_elevated();
    assert_eq!(a.containment, Containment::None);
    assert!(matches!(a.attached, Attached::None), "got {:?}", a.attached);
    assert_eq!(a.graceful, GracefulMechanism::Unknown);
}

// The cgroup placement decision =====
// `attach_tree` decides whether a Strongest root's leaf owns its tree. The child's own
// self-placement report is the oracle: a later `cgroup.procs` read lists only LIVE tasks, so a
// placed child that has already exited reads back as absent. Real filesystem, no cgroupfs: each
// leaf is a temp directory shaped with ordinary files. These tests are about what the decision
// does with a report, not how a child earns one: a failed write is made for real, in-process,
// but `Placed` is stored directly, since a successful write to anything but a real `cgroup.procs`
// is the very false report the live cgroup lane guards against.

/// What the child's `pre_exec` closure reported, if it ran at all.
#[cfg(target_os = "linux")]
#[derive(Clone, Copy)]
enum ChildReport {
    Placed,
    WriteFailed,
    NotReported,
}

/// Run the decision for a leaf at `leaf_path` whose child reported `report`.
#[cfg(target_os = "linux")]
fn decide(leaf_path: &std::path::Path, report: ChildReport) -> (crate::containment::Containment, super::Attached) {
    use crate::containment::cgroup::{place_self_in_cgroup_pre_exec, CgroupLeaf};

    let leaf = CgroupLeaf::for_test_at(leaf_path.to_path_buf());
    match report {
        // SAFETY: the slot's pipe lives as long as `leaf`.
        ChildReport::Placed => unsafe { leaf.placement_slot().report_placed_for_test() },
        ChildReport::WriteFailed => {
            // SAFETY: fd -1 is never writable, so the write fails with EBADF; closing -1 is a
            // no-op. The slot's pipe lives as long as `leaf`.
            _ = unsafe { place_self_in_cgroup_pre_exec(-1, leaf.placement_slot()) };
        }
        ChildReport::NotReported => {}
    }
    let prepared = super::Prepared {
        mode: Some(crate::containment::ContainMode::Strongest),
        is_root: true,
        cgroup_leaf: Some(leaf),
        #[cfg(all(target_os = "linux", feature = "tokio"))]
        teardown_warn_after: None,
    };
    super::attach_tree(
        std::process::id(),
        Ok(crate::identity::ProcessId::current()),
        crate::containment::cgroup::test_support::handle_of(
            std::process::id(),
            &crate::containment::cgroup::test_support::pidfd_of(std::process::id()),
        ),
        prepared,
    )
    .expect("attach_tree")
}

/// A leaf directory whose `cgroup.procs` lists some OTHER pid, never this test's.
#[cfg(target_os = "linux")]
fn leaf_listing_another_pid(dir: &std::path::Path) -> std::path::PathBuf {
    let leaf_path = dir.join("cosca-decision-leaf");
    std::fs::create_dir(&leaf_path).expect("create the leaf");
    let other = std::process::id().wrapping_add(1);
    std::fs::write(leaf_path.join("cgroup.procs"), format!("{other}\n")).expect("write cgroup.procs");
    leaf_path
}

/// A child that reported its write accepted, and has since exited: `cgroup.procs` no longer
/// lists it. Anything it forked is still in the leaf, so the leaf owns the tree.
#[cfg(target_os = "linux")]
#[skuld::test(labels = [crate::test_harness::CGROUP])]
fn a_placed_report_outranks_a_cgroup_procs_that_omits_the_child() {
    let dir = tempfile::tempdir().expect("tempdir");
    let leaf_path = leaf_listing_another_pid(dir.path());

    let (containment, attached) = decide(&leaf_path, ChildReport::Placed);

    assert_eq!(containment, crate::containment::Containment::CgroupV2);
    let super::Attached::Cgroup(leaf) = &attached else {
        panic!("got {attached:?}");
    };
    assert!(
        !leaf.holds_spawn_resources(),
        "a live child's leaf must not hold its cgroup.procs fd or report pipe past the verdict"
    );
    assert!(
        !leaf_path.join("cgroup.kill").exists(),
        "the leaf owns a live tree; nothing may kill it at spawn time"
    );
}

/// Membership that cannot be read is unknown, not absent — and the child's own report already
/// answers it.
#[cfg(target_os = "linux")]
#[skuld::test(labels = [crate::test_harness::CGROUP])]
fn a_placed_report_outranks_an_unreadable_cgroup_procs() {
    let dir = tempfile::tempdir().expect("tempdir");
    let leaf_path = dir.path().join("cosca-decision-leaf");
    std::fs::create_dir(&leaf_path).expect("create the leaf (no cgroup.procs inside)");

    let (containment, attached) = decide(&leaf_path, ChildReport::Placed);

    assert_eq!(containment, crate::containment::Containment::CgroupV2);
    assert!(matches!(attached, super::Attached::Cgroup(_)), "got {attached:?}");
}

/// A child whose write failed never entered the leaf, so neither did anything it forked: the
/// spawn degrades and the empty leaf is removed.
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_failed_write_degrades_and_removes_the_leaf() {
    let dir = tempfile::tempdir().expect("tempdir");
    let leaf_path = dir.path().join("cosca-decision-leaf");
    std::fs::create_dir(&leaf_path).expect("create the leaf");

    let (containment, attached) = decide(&leaf_path, ChildReport::WriteFailed);

    assert_eq!(containment, crate::containment::Containment::ProcessGroup);
    assert!(matches!(attached, super::Attached::ProcessGroup(_)), "got {attached:?}");
    assert!(!leaf_path.exists(), "the degrade must remove the leaf it created");
}

/// Removing a leaf the child never entered must never write `cgroup.kill`, even when the leaf
/// will not go away: whatever is in it, cosca did not put there. The leaf left behind is
/// reported.
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_degrade_never_kills_through_the_leaf() {
    crate::log_capture::install();
    for report in [ChildReport::WriteFailed, ChildReport::NotReported] {
        let dir = tempfile::tempdir().expect("tempdir");
        // A `cgroup.procs` file makes the directory non-empty, so its `rmdir` fails.
        let leaf_path = leaf_listing_another_pid(dir.path());

        let mark = crate::log_capture::mark();
        let (containment, _attached) = decide(&leaf_path, report);

        assert_eq!(containment, crate::containment::Containment::ProcessGroup);
        assert!(
            !leaf_path.join("cgroup.kill").exists(),
            "a degrade wrote cgroup.kill: it would kill a tree cosca had just been asked to contain"
        );
        let left_behind: Vec<String> = crate::log_capture::records_since(mark, &leaf_path.to_string_lossy())
            .into_iter()
            .filter(|record| record.contains("was not removed"))
            .collect();
        assert_eq!(
            left_behind.len(),
            1,
            "the unremoved leaf is reported once: {left_behind:?}"
        );
    }
}

// kill_on_drop's reach into the containment resource =====
// See `Attached::honor_kill_on_drop`.

/// A leaf standing in for a live contained tree: occupied (so `rmdir` fails and `Drop` would
/// reach for `cgroup.kill`) and carrying an empty `cgroup.kill` to read the write back from.
#[cfg(target_os = "linux")]
fn occupied_leaf_for_test(dir: &std::path::Path, name: &str) -> std::path::PathBuf {
    let leaf_path = dir.join(name);
    std::fs::create_dir(&leaf_path).expect("create the leaf");
    std::fs::write(leaf_path.join("occupant"), "").expect("stand in for the live tree");
    std::fs::write(leaf_path.join("cgroup.kill"), b"").expect("create cgroup.kill");
    leaf_path
}

#[cfg(target_os = "linux")]
fn cgroup_attachment_for_test(leaf_path: &std::path::Path) -> super::Attachment {
    super::Attachment {
        containment: crate::containment::Containment::CgroupV2,
        attached: super::Attached::Cgroup(crate::containment::cgroup::test_support::entered_leaf_at(
            leaf_path.to_path_buf(),
        )),
        graceful: crate::graceful::GracefulMechanism::Process,
    }
}

/// `kill_on_drop(false)` must disarm the cgroup leaf. Without it the handle's own teardown is
/// skipped and the leaf's `Drop` kills the tree anyway — the opposite of the opt-out.
#[cfg(target_os = "linux")]
#[skuld::test(labels = [crate::test_harness::CGROUP])]
fn kill_on_drop_false_disarms_a_cgroup_leaf() {
    let dir = tempfile::tempdir().expect("tempdir");
    let leaf_path = occupied_leaf_for_test(dir.path(), "cosca-kill-on-drop-false-leaf");

    let attachment = cgroup_attachment_for_test(&leaf_path);
    attachment.attached.honor_kill_on_drop(false).expect("disarm");
    drop(attachment);

    assert_eq!(
        std::fs::read(leaf_path.join("cgroup.kill")).expect("read cgroup.kill"),
        b"",
        "kill_on_drop(false) opts out of the teardown; a tree it killed anyway is the opposite"
    );
}

/// The same leaf under `kill_on_drop(true)` DOES fire `cgroup.kill`, so the test above pins
/// the disarm rather than an inert path.
#[cfg(target_os = "linux")]
#[skuld::test(labels = [crate::test_harness::CGROUP])]
fn kill_on_drop_true_leaves_a_cgroup_leaf_armed() {
    let dir = tempfile::tempdir().expect("tempdir");
    let leaf_path = occupied_leaf_for_test(dir.path(), "cosca-kill-on-drop-true-leaf");

    let attachment = cgroup_attachment_for_test(&leaf_path);
    attachment.attached.honor_kill_on_drop(true).expect("nothing to disarm");
    drop(attachment);

    assert_eq!(
        std::fs::read(leaf_path.join("cgroup.kill")).expect("read cgroup.kill"),
        b"1",
        "the default is still a kill-on-drop teardown"
    );
}

// ===== frozen attach (Windows) =====

#[cfg(windows)]
mod frozen_attach {
    use crate::containment::dispatch::{attach, Attached, Attachment, Prepared};
    use crate::containment::windows::fault::{force_next, Step};
    use crate::containment::{ContainMode, Containment};
    use crate::error::Error;
    use crate::test_frozen::{
        assert_not_released, limit_flags, raw, release, spawn_suspended_blocker, spawn_suspended_releasable,
        InitialThread,
    };
    use windows::Win32::System::JobObjects::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;

    const ERROR_ACCESS_DENIED: u32 = 5;

    fn prepared(mode: Option<ContainMode>, is_root: bool, frozen: bool) -> Prepared {
        Prepared {
            mode,
            is_root,
            graceful: crate::graceful::GracefulMechanism::None,
            frozen,
        }
    }

    fn attach_suspended(child: &std::process::Child, prepared: Prepared) -> Result<Attachment, Error> {
        let crate::identity::Resolved::Found(id) = crate::identity::ProcessId::of(child.id()) else {
            panic!("the suspended child must be readable");
        };
        attach(Ok(id), raw(child), prepared)
    }

    /// The identity read the OS refused, as the spawn hands it to the attach.
    fn refused_identity() -> Error {
        Error::Unassessable {
            detail: "the OS refused to report the spawned child's identity".into(),
            source: None,
        }
    }

    /// A job needs no identity: a Strongest root whose identity could not be read still gets one.
    ///
    /// Mutant: the attach fails early for want of an identity, whatever the mechanism.
    #[skuld::test]
    fn attach_assigns_a_strongest_roots_job_though_its_identity_could_not_be_read() {
        let child = spawn_suspended_blocker();
        let prepared = prepared(Some(ContainMode::Strongest), true, false);

        let attachment = attach(Err(refused_identity()), raw(&child), prepared).expect("attach");

        assert_eq!(attachment.containment, Containment::JobObject);
        dispose(child);
        drop(attachment);
    }

    /// A tree walk names its tree by the root's identity, so it fails the attach with the read's error.
    ///
    /// Mutant: the attach succeeds with a tree walk that has no root.
    #[skuld::test]
    fn attach_fails_a_tree_walk_root_with_the_identity_reads_error() {
        let child = spawn_suspended_blocker();

        let err = attach(
            Err(refused_identity()),
            raw(&child),
            prepared(Some(ContainMode::TreeWalk), true, false),
        )
        .err()
        .expect("a tree walk without an identity must fail the attach");

        assert!(matches!(err, Error::Unassessable { source: None, .. }), "got {err:?}");
        dispose(child);
    }

    fn dispose(mut child: std::process::Child) {
        child.kill().expect("kill the test child");
        child.wait().expect("reap the test child");
    }

    /// A frozen Strongest root is assigned its containment job and stays suspended.
    #[skuld::test]
    fn death_watch_attach_frozen_assigns_a_strongest_roots_job_and_leaves_the_thread_suspended() {
        let mut child = spawn_suspended_releasable();
        let thread = InitialThread::of(child.id());

        let attachment = attach_suspended(&child, prepared(Some(ContainMode::Strongest), true, true)).expect("attach");

        assert_eq!(attachment.containment, Containment::JobObject);
        let Attached::JobObject(job) = &attachment.attached else {
            panic!("expected a job, got {:?}", attachment.attached);
        };
        assert_ne!(
            limit_flags(job).0 & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE.0,
            0,
            "the job must kill on close: the frozen child never runs, so nothing else ends it"
        );
        assert_eq!(thread.suspend_count(), 1, "a frozen attach must not resume");
        drop(attachment);
        // The close's kill ended it. A close that killed nothing leaves a frozen child that never
        // runs, so it is released to run to its end, which `assert_not_released` rejects.
        release(&mut child, &thread);
        assert_not_released("the frozen child", child.wait().expect("reap the frozen child"));
    }

    /// A frozen root needs its job to be held, so a failed assignment fails the attach instead of
    /// falling back to `TreeWalk`, which would leave nothing to kill it.
    #[skuld::test]
    fn attach_frozen_fails_when_the_strongest_assignment_fails_instead_of_falling_back_to_treewalk() {
        let child = spawn_suspended_blocker();
        let _forced = force_next(Step::Assign, ERROR_ACCESS_DENIED);

        let err = attach_suspended(&child, prepared(Some(ContainMode::Strongest), true, true))
            .err()
            .expect("a frozen attach with no job must fail");

        assert!(matches!(err, Error::Containment { .. }), "got {err:?}");
        dispose(child);
    }

    /// The ordinary attach is unchanged: it resumes, and a failed assignment still degrades to
    /// `TreeWalk`.
    #[skuld::test]
    fn attach_without_hold_still_resumes_and_still_falls_back_to_treewalk() {
        let child = spawn_suspended_blocker();
        let thread = InitialThread::of(child.id());
        let _forced = force_next(Step::Assign, ERROR_ACCESS_DENIED);

        let attachment = attach_suspended(&child, prepared(Some(ContainMode::Strongest), true, false)).expect("attach");

        assert_eq!(attachment.containment, Containment::TreeWalk);
        assert_eq!(thread.suspend_count(), 0, "the ordinary attach resumes the child");
        dispose(child);
    }

    /// Every shape without a cosca job of its own (`TreeWalk` root, nested member, uncontained) is
    /// left frozen and unjobbed: `attach` adds no job and keeps the containment it would report if
    /// the child were not frozen.
    #[skuld::test]
    fn attach_frozen_gives_treewalk_nested_and_uncontained_shapes_no_job_and_leaves_them_suspended() {
        let shapes = [
            (
                "treewalk root",
                Some(ContainMode::TreeWalk),
                true,
                Containment::TreeWalk,
            ),
            (
                "nested strongest member",
                Some(ContainMode::Strongest),
                false,
                Containment::Delegated,
            ),
            (
                "nested treewalk member",
                Some(ContainMode::TreeWalk),
                false,
                Containment::Delegated,
            ),
            ("uncontained", None, false, Containment::None),
        ];
        for (name, mode, is_root, expected) in shapes {
            let child = spawn_suspended_blocker();
            let thread = InitialThread::of(child.id());

            let attachment = attach_suspended(&child, prepared(mode, is_root, true)).expect(name);

            assert_eq!(attachment.containment, expected, "{name}");
            assert!(
                !matches!(attachment.attached, Attached::JobObject(_)),
                "{name}: no job of its own"
            );
            assert_eq!(thread.suspend_count(), 1, "{name}: a frozen attach must not resume");
            drop(attachment);
            dispose(child);
        }
    }
}