cosca 0.8.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
//! Per-OS containment dispatch (two-phase: prepare before spawn, attach after).

use crate::containment::mechanism::{Contains, Mechanism};
use crate::containment::{ContainMode, ContainRequest, Nesting, Skip, Tree};
use crate::error::{Error, Failures};
#[cfg(unix)]
use crate::signal::RootState;
use std::sync::Arc;

/// The pre-spawn containment decision produced by `prepare` (env-marker root
/// detection plus per-OS pre-spawn setup).
pub(crate) struct Prepared {
    pub mode: Option<ContainMode>,
    pub is_root: bool,
    /// Pre-created cgroup leaf (Linux only). `Some` means the child must be
    /// placed in the cgroup by the in-child placement step; `None` means fall back
    /// to the process-group mechanism.
    #[cfg(target_os = "linux")]
    pub cgroup_leaf: Option<crate::containment::cgroup::CgroupLeaf>,
    /// The marker pipe for a contained macOS root. `None` for a nested member, an
    /// uncontained or elevation-derived spawn, or a failed install (which degrades to the
    /// process group).
    #[cfg(target_os = "macos")]
    pub marker: Option<crate::containment::fdmarker::PreparedMarker>,
    /// The cooperative-signal mechanism implied by the creation-flag word this spawn passed to
    /// `CreateProcessW`. Set by whichever backend chose that word, so the derivation reads what
    /// the OS was actually given.
    #[cfg(windows)]
    pub graceful: crate::graceful::GracefulMechanism,
    /// Whether the child is created suspended and `attach` must leave it so (see
    /// [`Command::spawn_suspended`](crate::Command::spawn_suspended)).
    #[cfg(windows)]
    pub frozen: bool,
}

/// A child the spawn has just forked, as the spawn holds it: its pid, and the pidfd the handshake
/// gave it from before `exec`. The pidfd names the child whoever reaps it and whoever takes its
/// number, so everything about the child's exit asks it, and the pid only labels.
#[cfg(target_os = "linux")]
#[derive(Debug, Clone, Copy)]
pub(crate) struct ChildHandle<'a> {
    pub(crate) pid: u32,
    pub(crate) pidfd: std::os::fd::BorrowedFd<'a>,
}

/// Linux 6.9+: `SIGKILL`, through a root's pidfd, the root's process group while the group has a
/// member. `pid` only labels the log. `Err` is the kernel's refusal of the kill, with the group's
/// members possibly left running: `EINVAL` when it refuses the flag (expected below 6.9), in which
/// case nothing is sent, as the group's number may name another group.
#[cfg(target_os = "linux")]
pub(crate) fn kill_group_through_pidfd(
    pidfd: std::os::fd::BorrowedFd<'_>,
    pid: Option<u32>,
) -> Result<(), nix::errno::Errno> {
    use nix::errno::Errno;
    let named = crate::child::spawn::named(pid);
    match crate::containment::unix::pidfd_signal_group(pidfd, nix::sys::signal::Signal::SIGKILL) {
        Ok(()) => log::debug!("spawn teardown: sent SIGKILL to the process group of {named} through its pidfd"),
        Err(Errno::ESRCH) => log::debug!("spawn teardown: the process group of {named} has no member left"),
        Err(e) => return Err(e),
    }
    Ok(())
}

impl Prepared {
    /// The cooperative-signal mechanism for a child spawned from this decision. The `cfg` lives
    /// here, once, so no spawn path carries one for it: every non-Windows child cosca owns can
    /// be sent an identity-bound `SIGTERM`, whatever its containment.
    pub(crate) fn graceful_mechanism(&self) -> crate::graceful::GracefulMechanism {
        #[cfg(windows)]
        return self.graceful;
        #[cfg(not(windows))]
        return crate::graceful::GracefulMechanism::Process;
    }

    /// End the placement exchange of a spawn that failed while the caller still holds its child:
    /// take the verdict, as `attach` would, so the leaf answers only for the tree and
    /// never for the child the caller will reap. Nothing is taken without a leaf, or once the
    /// verdict is taken. A child the leaf does not hold is only logged: the leaf answers for the
    /// tree either way.
    pub(crate) fn settle_verdict(&mut self, #[cfg(target_os = "linux")] child: ChildHandle<'_>) {
        #[cfg(target_os = "linux")]
        if let Some(leaf) = self.cgroup_leaf.as_mut().filter(|leaf| leaf.holds_verdict_to_take()) {
            if let Err(e) = leaf.take_placement(child) {
                log::debug!(
                    "cgroup v2: a failed spawn's child is not placed in its leaf: {report}",
                    report = crate::error::Report(&e)
                );
            }
        }
    }

    /// Whether the root's process group is its containment: the root is a Session or process-group
    /// root, and no cgroup leaf took it, as `attach` would decide. On Linux the leaf's verdict is
    /// taken first ([`settle_verdict`](Self::settle_verdict)).
    #[cfg(unix)]
    pub(crate) fn group_is_containment(&self) -> bool {
        #[cfg(target_os = "linux")]
        let taken_by_a_leaf = self.cgroup_leaf.as_ref().is_some_and(|leaf| leaf.child_entered());
        #[cfg(not(target_os = "linux"))]
        let taken_by_a_leaf = false;
        self.is_root && !taken_by_a_leaf && matches!(self.mode, Some(mode) if !matches!(mode, ContainMode::TreeWalk))
    }

    /// Linux 6.9+: `SIGKILL`, through `child`'s pidfd, the process group of a spawn that failed
    /// before its attach, while the group has a member. The pidfd names the group exactly, whoever
    /// reaped the root. Only a root whose group is its containment is sent it: a Session root, or a
    /// process-group root with no cgroup leaf or one its leaf did not take, as `attach` would decide.
    /// The leaf's verdict is settled first ([`settle_verdict`](Self::settle_verdict)); a leaf that
    /// took the child kills it with the leaf. `Err` is the kernel's refusal of the kill, with the
    /// group's members possibly left running: `EINVAL` when it refuses the flag (expected below
    /// 6.9), in which case nothing is sent, as the group's number may name another group.
    #[cfg(target_os = "linux")]
    pub(crate) fn kill_group_before_attach(&self, child: ChildHandle<'_>) -> Result<(), nix::errno::Errno> {
        if !self.group_is_containment() {
            return Ok(());
        }
        kill_group_through_pidfd(child.pidfd, Some(child.pid))
    }

    /// Watch the elevation front `pid` this spawn forked: its cgroup leaf, if it has one, records
    /// the front's place before its removal (see `CgroupLeaf::watch_front`).
    pub(crate) fn watch_front(&mut self, pid: u32) {
        #[cfg(target_os = "linux")]
        if let Some(leaf) = self.cgroup_leaf.as_mut() {
            leaf.watch_front(pid);
        }
        #[cfg(not(target_os = "linux"))]
        let _ = pid;
    }
}

/// What a spawn achieved, beyond the child handle itself: the tree it owns and the
/// cooperative-signal mechanism, which are independent axes (see [`crate::graceful`]).
///
/// Bundled rather than threaded as separate positional arguments so the spawn constructors do
/// not grow an argument per spawn fact, and so a new fact is a compile error at every
/// construction site rather than a silently diverging path.
pub(crate) struct Attachment {
    pub tree: Tree,
    pub graceful: crate::graceful::GracefulMechanism,
}

#[cfg(windows)]
impl Attachment {
    /// The attachment for a UAC-elevated spawn, which `ShellExecuteEx("runas")` creates through
    /// a system service with creation flags this process never sees and never chose.
    ///
    /// The mechanism is `Unknown`, so [`crate::graceful::signal`] refuses this child rather than
    /// addressing a console control event to its pid. `OtherConsoleGroup` would claim a route to
    /// a group known to exist, and `None` that no cooperative signal exists from any process
    /// ever; cosca can make neither claim about a child it did not create.
    pub(crate) fn uac_elevated() -> Attachment {
        Attachment {
            tree: Tree::none(),
            graceful: crate::graceful::GracefulMechanism::Unknown,
        }
    }
}

/// Who tears a child down. The caller prefixes the teardown's log lines and names its own event in
/// them.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum TeardownCaller {
    /// A `Child`'s own drop.
    Drop,
    /// A helper that owns the child and failed after its spawn (`Command::status`, `output`, `read`,
    /// `SuspendedChild::resume`).
    FailedRun,
    /// A spawn's teardown of the child it holds, when its identity read or its attach fails, and the
    /// cleanup of a child the handshake abandons.
    #[cfg_attr(
        not(any(target_os = "linux", windows)),
        allow(dead_code, reason = "only the Linux and Windows spawns tear a child down")
    )]
    FailedSpawn,
    /// The cleanup of an elevated spawn that cannot finish.
    #[cfg(unix)]
    FinishElevated,
}

impl TeardownCaller {
    /// The prefix of the caller's log lines.
    pub(crate) fn label(self) -> &'static str {
        match self {
            TeardownCaller::Drop => "Child::drop",
            TeardownCaller::FailedRun => "teardown after a failed run",
            TeardownCaller::FailedSpawn => "teardown after a failed spawn",
            #[cfg(unix)]
            TeardownCaller::FinishElevated => "finish_elevated",
        }
    }

    /// How a log line of the caller refers to its own event: a drop is "this drop", any other
    /// teardown is "this teardown".
    #[cfg(unix)]
    pub(crate) fn this_event(self) -> &'static str {
        match self {
            TeardownCaller::Drop => "this drop",
            TeardownCaller::FailedSpawn | TeardownCaller::FailedRun | TeardownCaller::FinishElevated => "this teardown",
        }
    }
}

/// What a `Child`'s drop knows about its root, for its report and for what it does to the root: the
/// one look at it. The mechanism's kill decides on [`look`](Self::look), the same look the report
/// is worded from.
#[cfg(unix)]
#[derive(Debug)]
pub(crate) struct DropView {
    /// The caller's name, prefixing its log lines.
    pub(crate) caller: TeardownCaller,
    pub(crate) root_pid: u32,
    /// The root's state as the look found it: the handle's own answer
    /// ([`RootState`](crate::signal::RootState)), or `Reaped` when the root's number shows the reap
    /// the handle could not.
    pub(crate) root: RootState,
    /// This handle already killed the tree completely.
    pub(crate) tree_killed: bool,
}

#[cfg(unix)]
impl DropView {
    /// Read the state a drop or a failed-spawn cleanup decides on: the root's number first, then
    /// the handle's own answer ([`RootState`](crate::signal::RootState)). The handle is exact
    /// where the number is not: a foreign reap followed by a same-tick reuse still reads as this
    /// root by its start token. A root with no identity (`id` is `None`) has nothing the number can
    /// be compared with, so only a number that names no process shows it reaped, and the handle
    /// decides alone otherwise.
    ///
    /// Reading emits no `warn`: the caller reports once, with a
    /// [`DropReport`](crate::child::drop_report::DropReport). `caller` names itself in log lines.
    pub(crate) fn read(
        caller: TeardownCaller,
        root_pid: u32,
        id: Option<crate::identity::ProcessId>,
        root: impl FnOnce() -> RootState,
        tree: &Tree,
    ) -> DropView {
        let label = caller.label();
        let now = crate::child::root_identity_now(root_pid);
        let state = root();
        let handle_reaped = matches!(state, RootState::Reaped { .. });
        let root = if crate::child::root_reaped(handle_reaped, id, now) {
            // The number shows the reap where the handle could not say; say so in the log.
            match &state {
                RootState::Unknown(e) => log::debug!(
                    "{label}: the root's own handle could not say whether it was reaped ({report}), but its number shows it reaped",
                    report = crate::error::Report(e)
                ),
                RootState::Unpinned => log::debug!(
                    "{label}: the root is not pinned by this process, but its number shows it reaped"
                ),
                RootState::Unreaped | RootState::Reaped { .. } => {}
            }
            match state {
                reaped @ RootState::Reaped { .. } => reaped,
                // Only the number shows the reap, so no reap of this handle's own.
                RootState::Unreaped | RootState::Unknown(_) | RootState::Unpinned => RootState::Reaped { own: false },
            }
        } else {
            state
        };
        let view = DropView {
            caller,
            root_pid,
            root,
            tree_killed: tree.is_killed(),
        };
        if now.is_unknown() && !handle_reaped {
            if view.unsettled() {
                log::debug!(
                    "{label}: the root's number ({root_pid}) could not be read either; kills by that number are skipped"
                );
            } else {
                log::debug!(
                    "{label}: the root's number ({root_pid}) could not be read; treating the root as not reaped"
                );
            }
        }
        view
    }

    /// The handle could not settle whether the root is still ours to act on, and nothing else did:
    /// `Unknown`, or `Unpinned`.
    pub(crate) fn unsettled(&self) -> bool {
        matches!(self.root, RootState::Unknown(_) | RootState::Unpinned)
    }

    /// This process does not pin the root (macOS: launchd holds it), and nothing else shows it
    /// reaped: its pid is not ours to signal or wait on.
    pub(crate) fn unpinned_root(&self) -> bool {
        matches!(self.root, RootState::Unpinned)
    }

    /// Whether the root itself is neither signalled nor waited on: it is reaped, or this process
    /// does not pin it (so its pid is not ours).
    pub(crate) fn leaves_root_alone(&self) -> bool {
        matches!(self.root, RootState::Reaped { .. } | RootState::Unpinned)
    }

    /// The look the mechanism's kill decides on: the one this view is worded from.
    pub(crate) fn look(&self) -> RootState {
        self.root.clone()
    }

    /// Why the root's number may not be trusted, for a log line or a note, with the error of a handle
    /// that could not say after it.
    pub(crate) fn why_number_untrusted(&self) -> String {
        match &self.root {
            RootState::Reaped { .. } => "the root is already reaped".to_owned(),
            untrusted => crate::signal::why_untrusted_with_cause(untrusted),
        }
    }
}

/// Where a `Child` handle's tree stands after the kills and drops that settled it
/// ([`TreeKilled::settle`] alone writes it): one state under one lock, so a reader sees what the last
/// settlement left. `settle` holds the lock across its kills by identity, which take a `Kernel` and
/// a `ProcessId` and never reach a `TreeKilled`, so the lock is not re-entered.
///
/// A drop that skips a number-named kill reads it to see whether it has anything to warn about
/// ([`DropView`]); a caller that has just run a kill reads the kill's own [`SettleOutcome`].
#[derive(Debug, Default)]
pub(crate) struct TreeKilled {
    state: std::sync::Mutex<TreeState>,
}

/// The settlement of a tree ([`TreeKilled`]): each state is a fact the handle knows, and [`next`]
/// moves them by one rule. Only a walk that leaves nothing vouches for the tree; only a member seen
/// running un-vouches it. A settlement, from any state:
///
/// - leaves members with identities (held ones still left, or new ones): `Left`;
/// - is a walk (`Ran`) that leaves nothing, held members included: `Killed`;
/// - leaves something running that holds no identity (a member of a handle that is not a `TreeWalk`,
///   or a walk that left the root, whatever then ended it): `Unvouched`, from any state;
/// - otherwise (a gone root, a walk not made, or a skipped kill whose sweep left nothing: none saw a
///   member running) keeps the state, except that `Left` whose held members all dropped out is
///   `Unvouched`. A held member is held because its kill was refused: it ran on and may have forked,
///   and killing it again by identity does not reach its children.
///
/// The kind of an `Unvouched` state is the latest fact:
///
/// - a walk that saw the root running names [`RootSeenRunning`](UnvouchedKind::RootSeenRunning),
///   whether or not the root had ended;
/// - held members that all dropped out name [`HeldMembersGone`](UnvouchedKind::HeldMembersGone);
/// - something else left running with no identity names [`Earlier`](UnvouchedKind::Earlier), from
///   any previous state.
///
/// A settlement with no new fact keeps the stored kind.
#[derive(Debug, Default)]
enum TreeState {
    /// Nothing has settled the tree: no kill left anything running, and no walk left nothing.
    #[default]
    Never,
    /// A walk left nothing: vouched for.
    Killed,
    /// A walk left these members, never none, so every later kill and drop answers them first: the
    /// root's death reparents them out of a later walk, so the handle kills the ones whose identity
    /// was read again and reports the others again.
    Left(Vec<crate::containment::treewalk::Left>),
    /// A kill ran, and no member it left is held now (each is gone, or the handle never held an
    /// identity for it), but no walk since has vouched for the tree: a member seen running may have
    /// forked, and its children were never looked for. The kind says what was found where the tree
    /// became unvouched: every text over this state is chosen by it.
    Unvouched(UnvouchedKind),
}

/// The state after a settlement, by the rule on [`TreeState`]. The facts: `walked`, the outcome was a
/// walk that ran; `left_now`, the members it left with identities; `running`, the outcome left
/// something running (the root included, whatever then ended it), not merely an error for a walk it
/// did not make (a gone root's or an unmade walk's error is not a member seen running);
/// `root_seen_running`, the walk left the root (whatever then ended it), which is `running`.
fn next(
    prev: &TreeState,
    walked: bool,
    left_now: Vec<crate::containment::treewalk::Left>,
    running: bool,
    root_seen_running: bool,
) -> TreeState {
    debug_assert!(
        !root_seen_running || running,
        "a root seen running is something left running"
    );
    if !left_now.is_empty() {
        return TreeState::Left(left_now);
    }
    // The kind the facts name, for a state they leave unvouched. Something running with no identity
    // and no root seen is `Earlier` from any state; with nothing running, the only way to be
    // unvouched is `Left` whose held members all dropped out.
    let named = if root_seen_running {
        UnvouchedKind::RootSeenRunning
    } else if running {
        UnvouchedKind::Earlier
    } else {
        UnvouchedKind::HeldMembersGone
    };
    match prev {
        _ if walked && !running => TreeState::Killed,
        TreeState::Never if !running => TreeState::Never,
        TreeState::Killed if !running => TreeState::Killed,
        TreeState::Unvouched(kept) if !running => TreeState::Unvouched(*kept),
        TreeState::Never | TreeState::Killed | TreeState::Left(_) | TreeState::Unvouched(_) => {
            TreeState::Unvouched(named)
        }
    }
}

impl TreeKilled {
    fn lock(&self) -> std::sync::MutexGuard<'_, TreeState> {
        self.state.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// Whether the tree is killed. Only a Unix drop reads it (`DropView`), and the tests.
    #[cfg(any(unix, test))]
    pub(crate) fn is_set(&self) -> bool {
        matches!(*self.lock(), TreeState::Killed)
    }
}

/// Which signal a tree path sends.
#[cfg(unix)]
#[derive(Debug, Clone, Copy)]
pub(crate) enum TreeSignal {
    Kill,
    Term,
}

/// A tree kill that did not complete, and every member it left running. It reports none of them:
/// [`Tree::settle`] decides what the root's own death leaves, and the caller reports that, once.
#[derive(Debug)]
pub(crate) struct Unkilled {
    /// Every member left but the root. For a walk, in the order it found them; for a mechanism that
    /// is not a walk, its one error.
    pub(crate) members: Vec<Error>,
    /// The root, when a walk left it. Only a `TreeWalk` walk can: the root's own death then settles it.
    /// Boxed so that a `Result` of an `Unkilled` stays small.
    pub(crate) root: Option<Box<Error>>,
    /// The members of `members` whose identity a `TreeWalk` walk read, and whose kill was refused: a
    /// later kill can kill them again by it. The others were never read.
    pub(crate) resolved: Vec<crate::identity::ProcessId>,
}

impl Unkilled {
    /// The first member left: a member before the root. A kill that completed is not an `Unkilled`,
    /// so there is one.
    #[cfg(unix)]
    pub(crate) fn first(&self) -> &Error {
        self.members
            .first()
            .or(self.root.as_deref())
            .expect("an incomplete kill left a member")
    }

    /// [`first`](Self::first), owned, for the tests that read what a walk returned.
    #[cfg(test)]
    pub(crate) fn into_error(self) -> Error {
        self.members
            .into_iter()
            .next()
            .or(self.root.map(|root| *root))
            .expect("an incomplete kill left a member")
    }

    /// What is still left once the root's own handle has had its say: `Ok` when nothing is, else the
    /// error that holds every member left, in order ([`Error::MembersLeft`]), with the errors that
    /// name no member as its `others` (a [`Error::Several`] when there are two or more). With no
    /// member, the first error that names none is the answer, in its own variant, with the others as
    /// its peers ([`Error::with_peers`]) (see
    /// [`Child::kill_tree`](crate::Child::kill_tree) for the contract).
    /// `root_ended`: the root is not running, so a root the walk left is not left.
    pub(crate) fn unsettled(self, root_ended: bool) -> Result<(), Error> {
        let root = self.root.filter(|_| !root_ended).map(|root| *root);
        let left = self.members.into_iter().chain(root);
        let (mut members, mut unnamed) = (Vec::new(), Vec::new());
        for error in left {
            if matches!(error, Error::MemberNotKilled { .. }) {
                members.push(Arc::new(error));
            } else {
                unnamed.push(error);
            }
        }
        match Failures::collect(members) {
            Some(members) => {
                let unnamed = Failures::collect(unnamed.into_iter().map(Arc::new));
                Err(Error::members_left(members, unnamed.map(Failures::into_cause)))
            }
            // No member: the first error is the answer, in its own variant, and the others its peers.
            None => {
                let mut unnamed = unnamed.into_iter();
                match unnamed.next() {
                    None => Ok(()),
                    Some(first) => Err(first.with_peers(unnamed.map(Arc::new))),
                }
            }
        }
    }
}

impl From<Error> for Unkilled {
    fn from(error: Error) -> Unkilled {
        Unkilled {
            members: vec![error],
            root: None,
            resolved: Vec::new(),
        }
    }
}

/// How a [`Tree::settle`] that found nothing left came out, by the state it left ([`TreeKilled`]):
/// the answer `Ok` does not say the tree is vouched for, this does.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SettleOutcome {
    /// The kill ran, or found the killed tree already gone, and the state left is killed: nothing is
    /// left for the caller to report, and the tree is vouched for.
    Killed,
    /// The kill was skipped (and the sweep by identity of a macOS fd marker left nothing): it killed
    /// nothing, and the state left is not unvouched.
    Skipped,
    /// The state left is [`Unvouched`](TreeState::Unvouched), whatever the call did: a skipped kill
    /// over a tree a skip moved there from [`Left`](TreeState::Left), or a walk that left only the
    /// root, whose own end then made the answer `Ok`. `kind` says what was found.
    Unvouched { kind: UnvouchedKind },
}

/// What a settlement found of a tree it cannot vouch for ([`SettleOutcome::Unvouched`]), as far as
/// the settlement knows.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum UnvouchedKind {
    /// The tree held members an earlier kill left, which are gone now.
    HeldMembersGone,
    /// A walk saw the root running and left it. If the root's own kill then ended it, the call was
    /// `Ok`. What the root forked meanwhile was never looked for.
    RootSeenRunning,
    /// A kill, this settlement's own or an earlier one, left something running that it could not name
    /// (members the handle holds no identity for, or members since gone), and the settlement
    /// no longer knows which.
    Earlier,
}

impl UnvouchedKind {
    /// What the settlement found, as the clause of a text about a tree it cannot vouch for.
    fn found(self) -> &'static str {
        match self {
            UnvouchedKind::HeldMembersGone => {
                "the members an earlier kill left are gone now, but no later walk has checked what they forked"
            }
            UnvouchedKind::RootSeenRunning => {
                "the walk found the root running, and what the root forked meanwhile was never looked for"
            }
            UnvouchedKind::Earlier => {
                "a kill left something running that it could not name, and no later walk has checked what it forked"
            }
        }
    }

    /// The text of a cleanup's or a drop's report over a tree it cannot vouch for.
    pub(crate) fn unvouched_text(self) -> String {
        format!("{}, so the tree could not be vouched for", self.found())
    }

    /// The drop's warn for a settlement over a tree it cannot vouch for: a kind that came of a skipped
    /// kill says so.
    fn drop_text(self) -> String {
        match self {
            UnvouchedKind::HeldMembersGone | UnvouchedKind::Earlier => {
                format!("the drop skipped the kill, and {}", self.unvouched_text())
            }
            UnvouchedKind::RootSeenRunning => self.unvouched_text(),
        }
    }

    /// What a gone root answers over a tree of this kind: the tree could not be walked, but not
    /// nothing was killed.
    fn gone_root_text(self) -> &'static str {
        match self {
            UnvouchedKind::HeldMembersGone => {
                "the members an earlier kill left are gone; the tree could not be walked from its gone root, so \
                 what they forked is unknown"
            }
            UnvouchedKind::RootSeenRunning => {
                "the root an earlier walk found running is gone; the tree could not be walked from it, so what it \
                 forked is unknown"
            }
            UnvouchedKind::Earlier => {
                "a kill left something running that it could not name, and no walk since has checked what it \
                 forked; the tree could not be walked from its gone root"
            }
        }
    }
}

/// What a tree kill did ([`Contains::kill`](crate::containment::Contains::kill)): the one value [`Tree::settle`] turns into what
/// is left and into the handle's state ([`TreeKilled`]).
#[derive(Debug)]
pub(crate) enum TreeKillOutcome {
    /// The kill ran; `Err` is what it left.
    Ran(Result<(), Unkilled>),
    /// The mechanism answered without a kill that goes on to the root's backstop: its tree is empty
    /// (a Linux process group with no member left, or one this handle's own reap left empty), so
    /// there is nothing to kill, or only the marker holders a macOS fd marker names by identity were.
    /// The call that sent it ends with this answer; a drop goes on to the root. `Err` is what it left.
    #[cfg_attr(
        not(unix),
        allow(dead_code, reason = "only a Unix mechanism answers by the root's number")
    )]
    Answered(Result<(), Unkilled>),
    /// A `TreeWalk` walk could not be made (the snapshot could not be taken, or the root exists but
    /// could not be queried), so NOTHING was killed, the root included: killing the root anyway would
    /// reparent its descendants out of the ppid walk, so a retry could no longer find them. The
    /// error says why. Every other failure is a kill that was attempted, a `TreeWalk`'s
    /// [`Error::MemberNotKilled`] included.
    Unwalked(Error),
    /// A `TreeWalk` walk found its root gone, or holding its pid no longer: there is no tree to walk
    /// from, so this kill killed nothing. It is [`Error::Unassessable`]: [`no_root_error`](Self::no_root_error)
    /// over a tree never settled, [`no_root_after_kills_error`](Self::no_root_after_kills_error) over
    /// an unvouched one, worded by what the settlement knows of it ([`UnvouchedKind`]).
    NoRoot,
    /// The root's number is untrusted, so the kill skipped what names the tree by it. The tree is
    /// not killed. The caller that has the root's handle acts on [`Skip::root_alone`].
    #[cfg_attr(
        not(unix),
        allow(dead_code, reason = "only a Unix mechanism skips by the root's number")
    )]
    Skipped(Skip),
}

/// A kill that ran: what [`Tree::kill`] and the paths that send by hand return.
impl From<Result<(), Unkilled>> for TreeKillOutcome {
    fn from(result: Result<(), Unkilled>) -> TreeKillOutcome {
        TreeKillOutcome::Ran(result)
    }
}

impl TreeKillOutcome {
    /// What [`NoRoot`](Self::NoRoot) is as an error: nothing was killed, because no walk ran.
    pub(crate) fn no_root_error() -> Error {
        Error::Unassessable {
            detail: "the root is gone or no longer holds its pid, so no walk ran and nothing was killed".to_owned(),
            source: None,
        }
    }

    /// What a [`NoRoot`](Self::NoRoot) is as an error over a tree an earlier kill left unvouched
    /// (`kind` says what it found): the tree could not be walked, but not nothing was killed.
    pub(crate) fn no_root_after_kills_error(kind: UnvouchedKind) -> Error {
        Error::Unassessable {
            detail: kind.gone_root_text().to_owned(),
            source: None,
        }
    }

    /// What the kill left: `Ok` for one that ran and left nothing, or a skip whose sweep left
    /// nothing. A walk that was not made or found no root left its error, as the only member.
    pub(crate) fn into_result(self) -> Result<(), Unkilled> {
        match self {
            TreeKillOutcome::Ran(result) | TreeKillOutcome::Answered(result) => result,
            TreeKillOutcome::Skipped(skip) => skip.holders.unwrap_or(Ok(())).map_err(Unkilled::from),
            TreeKillOutcome::Unwalked(error) => Err(Unkilled::from(error)),
            TreeKillOutcome::NoRoot => Err(Unkilled::from(Self::no_root_error())),
        }
    }

    /// Whether the kill reached the front it was made for: it ran and left nothing, the root
    /// included. The one predicate of every gate that asks, with a cgroup
    /// front, whether the cgroup kill ended it.
    #[cfg(unix)]
    pub(crate) fn completed(&self) -> bool {
        matches!(self, TreeKillOutcome::Ran(Ok(())) | TreeKillOutcome::Answered(Ok(())))
    }

    /// Why the kill did not leave the tree killed, or `None` when it [`completed`](Self::completed):
    /// the first member a kill left, or what a skip left undone. For the text of a gate or a log line
    /// that reads it: its error is worded as the whole chain, through [`Report`](crate::error::Report).
    #[cfg(unix)]
    pub(crate) fn incomplete(&self) -> Option<String> {
        if self.completed() {
            return None;
        }
        let word = |error: &Error| crate::error::Report(error).to_string();
        Some(match self {
            TreeKillOutcome::Ran(Err(left)) | TreeKillOutcome::Answered(Err(left)) => word(left.first()),
            TreeKillOutcome::Skipped(Skip {
                holders: Some(Err(error)),
                ..
            }) => word(error),
            TreeKillOutcome::Skipped(Skip { action, .. }) => {
                format!("the kill was skipped, and would {action}")
            }
            TreeKillOutcome::Unwalked(error) => word(error),
            TreeKillOutcome::NoRoot => word(&Self::no_root_error()),
            TreeKillOutcome::Ran(Ok(())) | TreeKillOutcome::Answered(Ok(())) => {
                unreachable!("a completed kill has nothing left")
            }
        })
    }

    /// What the kill skipped, as an action, when it was skipped.
    #[cfg(unix)]
    pub(crate) fn skipped(&self) -> Option<&str> {
        match self {
            TreeKillOutcome::Skipped(skip) => Some(&skip.action),
            TreeKillOutcome::Ran(_)
            | TreeKillOutcome::Answered(_)
            | TreeKillOutcome::Unwalked(_)
            | TreeKillOutcome::NoRoot => None,
        }
    }

    /// What a drop did not do, because the root's number is untrusted and this handle had not already
    /// killed the tree: the tree it names may outlive the drop.
    #[cfg(unix)]
    pub(crate) fn left_undone(&self, view: &DropView) -> Option<String> {
        self.skipped().filter(|_| !view.tree_killed).map(str::to_owned)
    }

    /// Why and what a skipped kill left undone, for a cleanup's note.
    #[cfg(unix)]
    pub(crate) fn skip_note(&self, view: &DropView) -> Option<String> {
        self.skipped().map(|action| {
            format!(
                "{}, so its number may name another process, and the kill would {action}",
                view.why_number_untrusted()
            )
        })
    }

    /// A drop's or a cleanup's `debug` record of a kill the mechanism skipped, which logs nothing
    /// above that: the caller's one report says what the skip left undone. A reaped root's skip is
    /// the drop's warn, and an unsettled root's is part of its state's.
    #[cfg(unix)]
    pub(crate) fn log_skip(&self, view: &DropView) {
        let TreeKillOutcome::Skipped(skip) = self else {
            return;
        };
        // The drop's own look at the root words it, as its warn does.
        let why = view.why_number_untrusted();
        let (label, this, action) = (view.caller.label(), view.caller.this_event(), &skip.action);
        if view.tree_killed {
            log::debug!("{label}: {why} and this handle already killed the tree, so {this} does not {action}");
        } else if !matches!(view.root, RootState::Reaped { .. }) {
            log::debug!("{label}: {why}, so {this} does not {action}");
        }
    }
}

/// A settlement's answer and the members it killed again, which the kill's wait adds to its wait
/// ([`KillWait::reaching`](crate::containment::KillWait::reaching)).
pub(crate) struct Settled<A = Result<SettleOutcome, Error>> {
    pub(crate) answer: A,
    pub(crate) killed_again: Vec<crate::identity::ProcessId>,
}

impl<A> Settled<A> {
    /// An answer that killed nothing again.
    #[cfg(unix)]
    pub(crate) fn of(answer: A) -> Settled<A> {
        Settled {
            answer,
            killed_again: Vec::new(),
        }
    }

    /// The same members, with the answer `f` makes of this one.
    pub(crate) fn map<B>(self, f: impl FnOnce(A) -> B) -> Settled<B> {
        Settled {
            answer: f(self.answer),
            killed_again: self.killed_again,
        }
    }
}

/// A `kill_tree` sweep that failed, and whether the root is dead all the same: its handle backstop
/// ended it. A caller that waits for the root before it returns the error reads that.
pub(crate) struct SweepFailure {
    pub(crate) error: Error,
    pub(crate) root_ended: bool,
}

/// A failure before the backstop ran, or that sent it nowhere: the root is not known dead.
impl From<Error> for SweepFailure {
    fn from(error: Error) -> SweepFailure {
        SweepFailure {
            error,
            root_ended: false,
        }
    }
}

/// A `kill_tree` sweep's answer, with the wait for what it killed: a caller that returns once the
/// members have exited [`wait`](Self::wait)s, and one that sends alone takes the [`answer`](Self::answer).
pub(crate) struct Swept<'a> {
    /// The call the sweep is a step of, which a warn about its wait names.
    op: &'static str,
    answer: Result<(), SweepFailure>,
    wait: crate::containment::KillWait<'a>,
}

impl<'a> Swept<'a> {
    pub(crate) fn new(
        op: &'static str,
        answer: Result<(), SweepFailure>,
        wait: crate::containment::KillWait<'a>,
    ) -> Swept<'a> {
        Swept { op, answer, wait }
    }

    /// The answer of a call that killed nothing, so it waits for nothing.
    pub(crate) fn unwaited(op: &'static str, answer: Result<(), SweepFailure>) -> Swept<'a> {
        Swept::new(op, answer, crate::containment::KillWait::none())
    }

    /// The sweep's answer, for a caller that sends and does not wait.
    pub(crate) fn answer(self) -> Result<(), SweepFailure> {
        self.answer
    }

    /// The sweep's answer once every member it killed has exited. A kill that failed is the answer
    /// over a wait that failed: the wait's error is logged. A wait that failed is a failure that
    /// keeps the record of the root's kill, as a kill that failed does.
    pub(crate) fn wait(self) -> Result<(), SweepFailure> {
        let root_ended = self.wait.waits_for_root();
        let waited = self.wait.wait();
        Self::after_wait(self.op, self.answer, waited, root_ended)
    }

    /// [`wait`](Self::wait), awaited, with no runtime thread blocked.
    #[cfg(feature = "tokio")]
    pub(crate) async fn wait_async(self) -> Result<(), SweepFailure> {
        let root_ended = self.wait.waits_for_root();
        let waited = self.wait.wait_async().await;
        Self::after_wait(self.op, self.answer, waited, root_ended)
    }

    fn after_wait(
        op: &str,
        answer: Result<(), SweepFailure>,
        waited: Result<(), Error>,
        root_ended: bool,
    ) -> Result<(), SweepFailure> {
        crate::containment::tree_kill::answer_after_wait(op, answer, waited, |error| SweepFailure { error, root_ended })
    }
}

impl SweepFailure {
    /// Whether the root is not running after a kill of it through its handle: it was killed, or was
    /// already dead ([`ChildFate::not_running`](crate::error::ChildFate::not_running)'s meaning). A
    /// kill that could not be verified (`Sent::Unverified`) may have left the root running.
    pub(crate) fn killed_root(sent: &Result<crate::signal::Sent, Error>) -> bool {
        use crate::signal::Sent;
        matches!(sent, Ok(Sent::Delivered | Sent::Gone))
    }

    /// The handle backstop's answer from its root kill's `sent`, and whether it ended the root.
    pub(crate) fn backstop_of(sent: Result<crate::signal::Sent, Error>) -> (Result<(), Error>, bool) {
        let ended = Self::killed_root(&sent);
        (sent.map(drop), ended)
    }

    /// The answer of a `kill_tree` sweep: the mechanism's error, settled by the root's death
    /// ([`Tree::settle`]), with the failure of the backstop, the handle's own kill of `root`. The
    /// backstop is part of the kill, so its failure is a member left like the walk's; the contract is
    /// [`Child::kill_tree`](crate::Child::kill_tree)'s. `root_ended`: the backstop ended the root.
    pub(crate) fn of(
        tree: &Tree,
        walk: TreeKillOutcome,
        backstop: Result<(), Error>,
        root: u32,
        root_ended: bool,
    ) -> Settled<Result<(), SweepFailure>> {
        tree.settle_for_kill_tree(walk, root_ended).map(|walk| {
            let error = match (walk, backstop) {
                (Ok(()), Ok(())) => return Ok(()),
                (Err(walk), Ok(())) => walk,
                (walk, Err(backstop)) => Self::with_backstop(tree, walk.err(), backstop, root),
            };
            Err(SweepFailure { error, root_ended })
        })
    }

    /// What the mechanism left, `walk`, with the failed `backstop` of `root` (see [`of`](Self::of)).
    fn with_backstop(tree: &Tree, walk: Option<Error>, backstop: Error, root: u32) -> Error {
        let backstop = Arc::new(backstop);
        let root_member = |source| Error::MemberNotKilled { pid: root, source };
        match walk {
            Some(Error::MembersLeft { members, others }) => {
                let mut root_seen = false;
                let mut members: Vec<Arc<Error>> = members
                    .iter()
                    .map(|member| match &**member {
                        Error::MemberNotKilled { pid, source } if *pid == root => {
                            root_seen = true;
                            Arc::new(root_member(
                                Failures::new(Arc::clone(&backstop), [Arc::clone(source)]).into_cause(),
                            ))
                        }
                        _ => Arc::clone(member),
                    })
                    .collect();
                if !root_seen {
                    members.push(Arc::new(root_member(backstop)));
                }
                Error::members_left(Failures::collect(members).expect("a member was left"), others)
            }
            Some(other) => other.with_peers([Arc::new(root_member(backstop))]),
            None => tree.root_left(root_member(backstop)),
        }
    }
}

impl TreeKilled {
    /// What a tree kill's `outcome` comes to once the root's own handle has had its say: the only place
    /// that writes the state. The mechanism decides what a kill left held
    /// ([`Contains::kill_left`](crate::containment::Contains::kill_left)); the settlement records it.
    /// `mechanism`: what names the tree, `None` for a tree that is none or delegated, which holds
    /// nothing. `root_ended`: the root is not running (killed, or already dead), so a root the walk
    /// left is not left. `Ok` when nothing is left, else the error that holds every member left
    /// ([`Unkilled::unsettled`]).
    ///
    /// It decides under the one lock of the state, by it and the outcome, and leaves the state
    /// by the rule on [`TreeState`]. The members a `TreeWalk` state holds are answered first, whatever
    /// the outcome. A gone root over a killed tree is `Ok`, and over held members still left it adds no
    /// error of its own; over a tree never settled it is [`TreeKillOutcome::no_root_error`], and over
    /// an unvouched one (held members that are all gone, or left so earlier) it is
    /// [`TreeKillOutcome::no_root_after_kills_error`].
    ///
    /// `kill_tree` reads `root_ended` from its backstop, which it runs for any walk; a drop or a
    /// failed spawn's cleanup reads it from the fate of the root kill it made after the walk.
    pub(crate) fn settle(
        &self,
        mechanism: Option<&Mechanism>,
        outcome: impl Into<TreeKillOutcome>,
        root_ended: bool,
    ) -> Settled {
        let outcome = outcome.into();
        let mut state = self.lock();
        let was_killed = matches!(*state, TreeState::Killed);
        // Copied, not taken: the state is written once, at the end, so an unwind between leaves it as it was.
        let held = match &*state {
            TreeState::Left(held) => held.clone(),
            TreeState::Never | TreeState::Killed | TreeState::Unvouched(_) => Vec::new(),
        };
        let gone_root = matches!(outcome, TreeKillOutcome::NoRoot);
        let ran = matches!(outcome, TreeKillOutcome::Ran(_) | TreeKillOutcome::Answered(_));
        // What a walk not made or a gone root leaves is its error, not a member seen running.
        let looked_for_no_member = matches!(outcome, TreeKillOutcome::NoRoot | TreeKillOutcome::Unwalked(_));
        // A walk already covered the tree unless it was never settled: a gone root adds no error of its own.
        let covered = gone_root && !matches!(*state, TreeState::Never);
        let result = if covered { Ok(()) } else { outcome.into_result() };
        let crate::containment::mechanism::KillLeftOutcome {
            mut result,
            left: left_now,
            killed: killed_again,
        } = match mechanism {
            Some(mechanism) => mechanism.kill_left(result, held),
            None => crate::containment::mechanism::nothing_held(result),
        };
        // A member the walk saw running un-vouches the tree, whatever ended it afterwards: it may have
        // forked first. Only the answer follows the root's end.
        let running = result.is_err() && !looked_for_no_member;
        let root_seen_running = matches!(&result, Err(left) if left.root.is_some());
        let after = next(&state, ran, left_now, running, root_seen_running);
        if gone_root && result.is_ok() && !was_killed {
            // What an earlier walk left is gone or unknown, but nothing walked the tree for what it forked.
            let error = match after {
                TreeState::Unvouched(kind) => TreeKillOutcome::no_root_after_kills_error(kind),
                TreeState::Never | TreeState::Killed | TreeState::Left(_) => {
                    debug_assert!(
                        false,
                        "a gone root over held or unvouched members leaves the tree unvouched"
                    );
                    // Release: the gone-root answer of a tree no kill left unvouched.
                    TreeKillOutcome::no_root_error()
                }
            };
            result = Err(Unkilled::from(error));
        }
        let unsettled = match result {
            Ok(()) => Ok(()),
            Err(left) => left.unsettled(root_ended),
        };
        // The answer follows the state left: `Killed` only over a vouched tree. A gone root answers
        // `Ok` only over a killed tree.
        let answer = unsettled.map(|()| match &after {
            TreeState::Unvouched(kind) => SettleOutcome::Unvouched { kind: *kind },
            _ if ran || gone_root => {
                debug_assert!(matches!(after, TreeState::Killed), "`Killed` answers a killed tree");
                SettleOutcome::Killed
            }
            _ => SettleOutcome::Skipped,
        });
        *state = after;
        Settled { answer, killed_again }
    }

    /// [`settle`](Self::settle) for a `kill_tree` call: the error holds every member left, so the call
    /// reports each of them once, in it.
    pub(crate) fn settle_for_kill_tree(
        &self,
        mechanism: Option<&Mechanism>,
        outcome: impl Into<TreeKillOutcome>,
        root_ended: bool,
    ) -> Settled<Result<(), Error>> {
        self.settle(mechanism, outcome, root_ended)
            .map(|answer| answer.map(drop))
    }

    /// `outcome` for a `kill_tree` that goes on to the root's backstop: a `TreeWalk` that could not
    /// walk killed nothing, and the root's death would strand the descendants beyond a retry, so it is
    /// settled here and returned as the error with the tree intact. Any other `outcome` is handed back.
    pub(crate) fn unless_unwalked(
        &self,
        mechanism: Option<&Mechanism>,
        outcome: TreeKillOutcome,
    ) -> Result<TreeKillOutcome, Settled<SweepFailure>> {
        if !matches!(outcome, TreeKillOutcome::Unwalked(_)) {
            return Ok(outcome);
        }
        let settled = self.settle_for_kill_tree(mechanism, outcome, false);
        Err(settled.map(|answer| match answer {
            Err(error) => SweepFailure::from(error),
            // The walk's error is a member left, and the root is not ended.
            Ok(()) => unreachable!("a walk not made leaves its error"),
        }))
    }

    /// [`settle`](Self::settle) for a drop: what is left when the tree is not settled, the text of the
    /// drop's warn (worded here and only when it will be logged) and the error, if the settlement's
    /// was one.
    pub(crate) fn settle_for_drop(
        &self,
        mechanism: Option<&Mechanism>,
        outcome: impl Into<TreeKillOutcome>,
        root_ended: bool,
    ) -> Option<TreeLeft> {
        let outcome = outcome.into();
        let after = match &outcome {
            // A drop cannot retry a walk that was not made.
            TreeKillOutcome::Unwalked(_) => "; the root is killed regardless, so its descendants may be orphaned",
            TreeKillOutcome::Ran(_)
            | TreeKillOutcome::Answered(_)
            | TreeKillOutcome::NoRoot
            | TreeKillOutcome::Skipped(_) => "",
        };
        let (left, failure) = match self.settle(mechanism, outcome, root_ended).answer {
            Err(left) => (crate::error::Report(&left).to_string(), Some(left)),
            Ok(SettleOutcome::Unvouched { kind }) => (kind.drop_text(), None),
            Ok(SettleOutcome::Killed | SettleOutcome::Skipped) => return None,
        };
        Some(TreeLeft {
            text: format!("contained-tree teardown did not fully succeed: {left}{after}"),
            failure,
        })
    }
}

/// What a drop or a failed run's teardown found left of its contained tree
/// ([`TreeKilled::settle_for_drop`]).
pub(crate) struct TreeLeft {
    /// The text of the teardown's warn.
    pub(crate) text: String,
    /// The tree kill's failure, as an error: absent when the tree is only not vouched for, which is a
    /// state, not a failure.
    pub(crate) failure: Option<Error>,
}

/// Returns `true` when the current process is the outermost contained root
/// (i.e. the env marker is absent). Pure function: takes the marker-presence
/// flag so it can be unit-tested without touching the process environment.
pub(crate) fn is_nested(marker_present: bool) -> bool {
    marker_present
}

/// What the spawn's identity read said of a child it cannot verify (macOS leaves such a child alone).
#[cfg_attr(
    not(target_os = "macos"),
    allow(dead_code, reason = "only macOS leaves a child unverified")
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RootIdentity {
    /// The root was already reaped (someone else reaped it, and its pid may be reused).
    Gone,
    /// The read was refused or could not be trusted.
    Unknown,
}

/// Which Unix setup action to apply to a root `Command` for a given mode.
/// Pure function: used by `prepare` and unit-tested separately to verify
/// mutual exclusivity: Session → setsid only; Strongest/default → pgroup
/// only; TreeWalk → neither (it must catch process-group escapees).
#[cfg(unix)]
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum UnixSetup {
    /// Add the `setsid` step to the spawn's in-child hook (ContainMode::Session). Must NOT be
    /// combined with ProcessGroup on the same Command (EPERM on a session leader).
    Session,
    /// Apply `process_group(0)` (ContainMode::Strongest or default).
    ProcessGroup,
    /// Apply NO pre-spawn grouping (ContainMode::TreeWalk). TreeWalk's whole
    /// point is to catch children that `setsid`/`setpgid` out of a process group,
    /// so it must not put the root in a group itself; teardown is by identity.
    None,
}

/// Decide which Unix mechanism to apply for `mode` (root spawns only).
/// Keeping this as a pure function makes the mutual-exclusivity invariant
/// directly unit-testable without inspecting `std::process::Command` internals.
#[cfg(unix)]
pub(crate) fn unix_setup_for(mode: Option<ContainMode>) -> UnixSetup {
    match mode {
        Some(ContainMode::Session) => UnixSetup::Session,
        Some(ContainMode::TreeWalk) => UnixSetup::None,
        _ => UnixSetup::ProcessGroup,
    }
}

/// The Windows pre-spawn containment decision, shared by the std `prepare` path and the raw
/// `CreateProcessW` backend. Encodes ONLY the decision; the caller applies it (`creation_flags`
/// onto the command, the root env marker, `clear_std_handle_inheritance`).
#[cfg(windows)]
pub(crate) struct WindowsContain {
    /// Creation flags to OR into the spawn: `root_flags` (suspend + new group) for a Strongest
    /// root, `group_flags` (new group only) otherwise; `0` when uncontained (`mode` is `None`).
    pub creation_flags: u32,
    /// Whether this spawn must set the inherited root marker so descendants join THIS group.
    pub marker_env: bool,
}

/// Decide the Windows pre-spawn containment flags + marker for `req`/`is_root`. Pure — directly
/// unit-testable. Returns `{ creation_flags: 0, marker_env: false }` when `req.mode` is `None`.
#[cfg(windows)]
pub(crate) fn windows_contain_setup(req: &ContainRequest, is_root: bool) -> WindowsContain {
    let Some(mode) = req.mode else {
        return WindowsContain {
            creation_flags: 0,
            marker_env: false,
        };
    };
    let creation_flags = if is_root && !matches!(mode, ContainMode::TreeWalk) {
        // Strongest root: suspend + new process group (job assigned in attach).
        crate::containment::windows::root_flags()
    } else {
        // TreeWalk root (no suspend, no job — identity teardown) and all nested spawns:
        // CREATE_NEW_PROCESS_GROUP only, so `terminate` can CTRL_BREAK the root's group.
        crate::containment::windows::group_flags()
    };
    WindowsContain {
        creation_flags,
        marker_env: is_root && req.nesting == Nesting::Mark,
    }
}

/// Phase 1 (before spawn): env-marker root detection + pre-spawn OS setup. On Unix, what the child
/// does for itself between `fork` and `exec` goes into `steps`, which the caller registers.
///
/// `teardown_warn_after` is the spawn policy's: the cgroup leaf made here warns once this long into
/// each of its waits for its drain, whoever drops or abandons it (principle 2).
///
/// `reserved_fds` and `marker_suppressed` drive the macOS fd-marker install only; every
/// other platform ignores them. `flags` is the caller's creation-flag request, which only
/// Windows can honour.
///
/// Fallible only on Windows, where the composed creation-flag word may name a reserved bit. The
/// composition runs at the very top — before anything is mutated, so a refusal leaves nothing to
/// unwind, and above the uncontained early return, so an uncontained spawn carries the caller's
/// flags too.
#[allow(
    clippy::too_many_arguments,
    reason = "each Unix platform reads its own subset of the arguments"
)]
pub(crate) fn prepare(
    std_cmd: &mut std::process::Command,
    #[cfg(unix)] steps: &mut crate::child::spawn::in_child::InChild,
    req: &ContainRequest,
    #[cfg_attr(
        not(windows),
        allow(
            unused_variables,
            reason = "only Windows can honour the caller's creation-flag request"
        )
    )]
    flags: &crate::command::flags::FlagsRequest,
    #[cfg_attr(
        not(target_os = "macos"),
        allow(unused_variables, reason = "drives the macOS fd-marker install only")
    )]
    reserved_fds: &[i32],
    #[cfg_attr(
        not(target_os = "macos"),
        allow(unused_variables, reason = "drives the macOS fd-marker install only")
    )]
    marker_suppressed: bool,
    #[cfg_attr(
        not(windows),
        allow(unused_variables, reason = "only the Windows path composes env ops into the request")
    )]
    env_ops: &[crate::command::EnvOp],
    #[cfg_attr(
        not(target_os = "linux"),
        allow(unused_variables, reason = "only a Linux cgroup leaf waits for its drain")
    )]
    teardown_warn_after: Option<std::time::Duration>,
) -> Result<Prepared, Error> {
    let mode = req.mode;
    // Read once, above every branch, so the word is composed exactly once per spawn and no two
    // compositions can disagree. Reading it for an uncontained spawn too is unobservable.
    //
    // On Unix cosca never sets this variable in its own process, so it can only change here if the
    // user `set_var`s it concurrently, which is the user's data race. On Windows `set_var` is sound
    // from any thread, so the marker comes from one snapshot, and a contained child's whole
    // environment is built from that same snapshot below: the decision and the child agree.
    #[cfg(windows)]
    let snapshot = crate::child::spawn::windows_raw::env_snapshot::EnvSnapshot::read()?;
    #[cfg(windows)]
    let marker_present = snapshot
        .var(std::ffi::OsStr::new(crate::containment::NESTED_ENV))
        .is_some();
    #[cfg(not(windows))]
    let marker_present = std::env::var_os(crate::containment::NESTED_ENV).is_some();
    let is_root = !is_nested(marker_present);

    #[cfg(windows)]
    let graceful = {
        use std::os::windows::process::CommandExt;
        let spawn =
            crate::command::flags::windows_spawn(req, *flags, is_root, crate::command::flags::SpawnBackend::Std)?;
        std_cmd.creation_flags(spawn.creation_flags);
        // Sound for THIS derivation and no other: `mechanism_from_flags` reads only
        // CREATE_NEW_PROCESS_GROUP / DETACHED_PROCESS / CREATE_NEW_CONSOLE / CREATE_NO_WINDOW,
        // and std adds and removes none of them. The word is what cosca supplies, never what
        // `CreateProcessW` receives — see `crate::command::flags`.
        crate::containment::windows::mechanism_from_flags(spawn.creation_flags)
    };

    if mode.is_none() {
        return Ok(Prepared {
            mode: None,
            is_root: false,
            #[cfg(target_os = "linux")]
            cgroup_leaf: None,
            #[cfg(target_os = "macos")]
            marker: None,
            #[cfg(windows)]
            graceful,
            #[cfg(windows)]
            frozen: flags.suspended,
        });
    }
    let marker_env = is_root && req.nesting == Nesting::Mark;
    // Windows: hand std the whole environment, rebuilt from the snapshot the marker was read from,
    // so std does not read this process's environment again at `CreateProcessW`. The marker is an
    // op after the user's, named as std names it; it survives a user `env_clear`.
    #[cfg(windows)]
    {
        use crate::child::spawn::windows_raw::{child_ops, resolve::ChildEnv};
        let env = ChildEnv::capture(&snapshot, &child_ops(env_ops, marker_env));
        std_cmd.env_clear();
        std_cmd.envs(env.captured_vars().expect("a capture is never inherited"));
    }
    // Elsewhere: set AFTER any user env ops (env_clear) have been applied to std_cmd by the spawn
    // engine, so the marker survives env_clear. `env` appends.
    #[cfg(not(windows))]
    if marker_env {
        std_cmd.env(crate::containment::NESTED_ENV, "1");
    }

    // Linux: session mode or (cgroup v2 + process group).
    // Mechanism selection via `unix_setup_for` (setsid/pgroup mutual exclusivity):
    // Session → setsid only; Strongest/TreeWalk → process_group(0) + try cgroup.
    #[cfg(target_os = "linux")]
    {
        if is_root && mode.is_some() {
            match unix_setup_for(mode) {
                UnixSetup::Session => {
                    // Session: setsid only — no process_group(0) (would EPERM).
                    crate::containment::unix::set_session(steps);
                    return Ok(Prepared {
                        mode,
                        is_root,
                        cgroup_leaf: None, // cgroup not used for Session
                    });
                }
                UnixSetup::None => {
                    // TreeWalk: NO process group / setsid / cgroup — teardown is
                    // by identity so we can catch process-group escapees.
                    return Ok(Prepared {
                        mode,
                        is_root,
                        cgroup_leaf: None,
                    });
                }
                UnixSetup::ProcessGroup => {
                    // Strongest: set a new process group + try cgroup.
                    crate::containment::unix::set_process_group(std_cmd);
                }
            }

            // A failed leaf creation degrades this spawn to the process group set above. The
            // decision is unchanged; only the silence is — `log_degrade` names the step that
            // failed and the kernel's reason for it.
            let leaf = match crate::containment::cgroup::try_create_leaf(teardown_warn_after) {
                Ok(leaf) => Some(leaf),
                Err(e) => {
                    crate::containment::cgroup::log_degrade(&e);
                    None
                }
            };
            if let Some(ref l) = leaf {
                // The child's self-placement step. A failed placement (e.g. EBUSY — "no internal
                // processes" rule) returns Ok, so the spawn proceeds and `attach` falls back to the
                // already-configured process group. Only a report the child cannot send fails the
                // spawn. Both the sync and the tokio spawn add this one step through `prepare`.
                // SAFETY: the leaf, which owns both the fd and the report channel, stays in
                // `Prepared` until after the spawn.
                unsafe { steps.place(l.procs_fd(), l.placement_slot()) };
            }
            return Ok(Prepared {
                mode,
                is_root,
                cgroup_leaf: leaf,
            });
        }
        return Ok(Prepared {
            mode,
            is_root,
            cgroup_leaf: None,
        });
    }

    // Non-Linux Unix: process group or session (mutually exclusive).
    // Mechanism selection via `unix_setup_for`: Session → setsid only;
    // Strongest (= ProcessGroup on macOS) → process_group(0).
    #[cfg(all(unix, not(target_os = "linux")))]
    if is_root && mode.is_some() {
        match unix_setup_for(mode) {
            UnixSetup::Session => crate::containment::unix::set_session(steps),
            UnixSetup::ProcessGroup => crate::containment::unix::set_process_group(std_cmd),
            UnixSetup::None => {} // TreeWalk: no pre-spawn grouping (identity teardown)
        }
    }

    // macOS: install the inherited-fd marker for every contained root, regardless of the
    // requested mode (decision: the marker survives what the mode-specific mechanism above
    // does not). `marker_wanted` gates on root/mode/suppression; `install` degrades to `None`
    // on any failure rather than failing the spawn.
    #[cfg(target_os = "macos")]
    let marker = if crate::containment::fdmarker::marker_wanted(mode, is_root, marker_suppressed) {
        crate::containment::fdmarker::install(steps, reserved_fds)
    } else {
        None
    };

    // Windows: stop the child inheriting this process's std handles. Contained spawns only, and
    // below the composition above so a refused spawn has mutated nothing. The creation-flag word
    // itself is applied at the top, since an uncontained spawn never reaches this point.
    #[cfg(windows)]
    crate::containment::windows::clear_std_handle_inheritance();

    // On `target_os = "linux"` only, every path through the `#[cfg(target_os = "linux")]` block
    // above returns, so this expression is unreachable there — but it is the live tail expression
    // on every other target.
    #[cfg_attr(
        target_os = "linux",
        expect(unreachable_code, reason = "the linux block above returns on every path")
    )]
    Ok(Prepared {
        mode,
        is_root,
        #[cfg(target_os = "linux")]
        cgroup_leaf: None,
        #[cfg(target_os = "macos")]
        marker,
        #[cfg(windows)]
        graceful,
        #[cfg(windows)]
        frozen: flags.suspended,
    })
}

/// Why a child could not be contained, and what failed in the cleanup the attach made of it (on
/// Windows, the kill of the job after a failed resume): the failed spawn returns the cause, with the
/// cleanup beside it (`cleanup` of [`Error::MayHaveStarted`]).
#[derive(Debug)]
pub(crate) struct AttachFailure {
    pub(crate) cause: Error,
    pub(crate) cleanup: Option<Failures>,
}

impl From<Error> for AttachFailure {
    fn from(cause: Error) -> AttachFailure {
        AttachFailure { cause, cleanup: None }
    }
}

/// Phase 2 (after spawn, before the `OwnedChild` adopts): attach the mechanism and bundle it with the
/// spawn's other achieved facts. Consumes `prepared` so Linux cgroup leaf ownership transfers
/// cleanly to the cgroup mechanism without requiring interior mutability — hence the
/// cooperative-signal mechanism is read off it first.
///
/// `id` is the identity the spawn read of the root, or the error of the read that failed. A
/// mechanism that names the tree by the root's identity (a tree walk, the macOS fd marker) fails the
/// attach with that error; one that attaches by pid or handle (a cgroup, a process group, a Job
/// Object) does not need it.
pub(crate) fn attach(
    #[cfg(unix)] pid: u32,
    id: Result<crate::identity::ProcessId, Error>,
    #[cfg(target_os = "linux")] child: ChildHandle<'_>,
    #[cfg(windows)] proc_handle: std::os::windows::io::RawHandle,
    prepared: Prepared,
) -> Result<Attachment, AttachFailure> {
    let graceful = prepared.graceful_mechanism();
    let tree = attach_tree(
        #[cfg(unix)]
        pid,
        id,
        #[cfg(target_os = "linux")]
        child,
        #[cfg(windows)]
        proc_handle,
        prepared,
    )?;
    Ok(Attachment { tree, graceful })
}

/// The tree-teardown half of [`attach`]: which mechanism owns this child's tree.
fn attach_tree(
    #[cfg(unix)] pid: u32,
    id: Result<crate::identity::ProcessId, Error>,
    #[cfg(target_os = "linux")] child: ChildHandle<'_>,
    #[cfg(windows)] proc_handle: std::os::windows::io::RawHandle,
    prepared: Prepared,
) -> Result<Tree, AttachFailure> {
    use crate::containment::treewalk::Walker;
    #[cfg(unix)]
    use crate::containment::unix::Group;
    // Linux: session, or cgroup v2 / process group.
    #[cfg(target_os = "linux")]
    {
        let mut prepared = prepared;
        if prepared.mode.is_some() {
            if prepared.is_root {
                // TreeWalk root: no kernel container / process group; teardown is
                // by identity: the one the spawn verified against the child's handle.
                if matches!(prepared.mode, Some(ContainMode::TreeWalk)) {
                    return Ok(Tree::contained(Mechanism::Walk(Walker::new(id?))));
                }

                let raw_pid = pid;
                debug_assert!(
                    raw_pid <= i32::MAX as u32,
                    "pid {raw_pid} exceeds i32::MAX; pgid cast would truncate"
                );
                let pgid = raw_pid as i32;

                // Session mode: setsid was applied pre-spawn; no cgroup.
                // pgid == sid == pid for the session leader; killpg works.
                if matches!(prepared.mode, Some(ContainMode::Session)) {
                    return Ok(Tree::contained(Mechanism::Group(Group::session(pgid))));
                }

                // Strongest: cgroup v2 if available, else process group.
                if let Some(mut leaf) = prepared.cgroup_leaf.take() {
                    // The pre_exec write can fail (EBUSY — the "no internal processes" rule
                    // when the supervisor is itself an undelegated leaf). The child's own
                    // report of that write decides membership; re-reading cgroup.procs cannot,
                    // because it lists only live tasks and a placed child may already have
                    // exited. Taking the verdict waits for that report — `spawn` returning does
                    // not mean the child has made it — then releases the leaf's fd and pipe.
                    match leaf.take_placement(child) {
                        Ok(()) => {
                            return Ok(Tree::contained(Mechanism::Cgroup(leaf)));
                        }
                        // Nothing of the child's is in the leaf: its final report is not Placed. The
                        // process group set pre-spawn is the real container; the leaf is
                        // removed without writing cgroup.kill.
                        Err(reason) => {
                            crate::containment::cgroup::log_degrade(&reason);
                            leaf.remove_unentered();
                            return Ok(Tree::contained(Mechanism::Group(Group::process_group(pgid))));
                        }
                    }
                }
                // No cgroup leaf: fall back to process group (set pre-spawn).
                return Ok(Tree::contained(Mechanism::Group(Group::process_group(pgid))));
            } else {
                // Nested member: it joined the ancestor's cgroup/process group (or the
                // root's tree-walk) rather than creating its own, so it owns no teardown —
                // the outermost root tears the whole tree down.
                return Ok(Tree::delegated());
            }
        }
    }

    // Non-Linux Unix: process group or session.
    // For Session: setsid was called pre-spawn (not process_group(0)); the child
    // is a session leader with pgid == pid. Teardown via killpg is identical —
    // the group mechanism is reused since the pgroup == session leader's pid.
    // For Strongest (= ProcessGroup on macOS): process_group(0) was called pre-spawn.
    #[cfg(all(unix, not(target_os = "linux")))]
    {
        if prepared.mode.is_some() {
            if prepared.is_root {
                // macOS fd marker: installed for every contained root regardless of mode, so
                // it takes priority over the mode-specific mechanism below — it is what
                // survives setsid/reparenting/exec that the mode-specific mechanism does not.
                #[cfg(target_os = "macos")]
                if let Some(marker) = prepared.marker {
                    let pgid = match prepared.mode {
                        Some(ContainMode::TreeWalk) => None,
                        _ => {
                            debug_assert!(
                                pid <= i32::MAX as u32,
                                "pid {pid} exceeds i32::MAX; pgid cast would truncate"
                            );
                            Some(pid as i32)
                        }
                    };
                    return Ok(Tree::contained(Mechanism::Marker(
                        crate::containment::fdmarker::Marker::new(marker, id?, pgid),
                    )));
                }
                // TreeWalk root: no process group; identity teardown.
                if matches!(prepared.mode, Some(ContainMode::TreeWalk)) {
                    return Ok(Tree::contained(Mechanism::Walk(Walker::new(id?))));
                }
                let raw_pid = pid;
                debug_assert!(
                    raw_pid <= i32::MAX as u32,
                    "pid {raw_pid} exceeds i32::MAX; pgid cast would truncate"
                );
                let pgid = raw_pid as i32;
                let group = match prepared.mode {
                    Some(ContainMode::Session) => Group::session(pgid),
                    _ => Group::process_group(pgid),
                };
                return Ok(Tree::contained(Mechanism::Group(group)));
            } else {
                // Nested member: it joined the ancestor's process group (or the root's
                // tree-walk) rather than creating its own, so it owns no teardown — the
                // outermost root tears the whole tree down.
                #[cfg(target_os = "macos")]
                if prepared.marker.is_some() {
                    log::warn!(
                        "fd marker: a nested spawn's prepare() unexpectedly produced a marker; \
                         a nested member must inherit the root's marker, never create its own \
                         — discarding it"
                    );
                    debug_assert!(
                        false,
                        "a nested member inherits the root's marker and must never create one"
                    );
                }
                return Ok(Tree::delegated());
            }
        }
    }

    // Windows: Job Object (strongest available on this OS), or TreeWalk.
    #[cfg(windows)]
    {
        if prepared.mode.is_some() && prepared.is_root {
            // TreeWalk root: no job (spawned with CREATE_NEW_PROCESS_GROUP only);
            // identity teardown, with CTRL_BREAK to the group as cooperative term.
            if matches!(prepared.mode, Some(ContainMode::TreeWalk)) {
                return Ok(Tree::contained(Mechanism::Walk(Walker::new(id?))));
            }
            // A frozen root is assigned its job and left suspended. It gets no `TreeWalk` fallback:
            // a frozen root without a job would have nothing to kill it if its owner died.
            if prepared.frozen {
                return match crate::containment::windows::assign_held(proc_handle) {
                    Ok(job) => Ok(Tree::contained(Mechanism::Job(job))),
                    Err(e) => Err(Error::Containment {
                        detail: "the frozen child could not be assigned to its job".to_owned(),
                        source: Some(Error::io_cause(e)),
                    }
                    .into()),
                };
            }
            match crate::containment::windows::attach_job(proc_handle) {
                Ok(Some(job)) => return Ok(Tree::contained(Mechanism::Job(job))),
                Ok(None) => {
                    // Job assignment failed: fall back to the universal TreeWalk
                    // mechanism rather than silently yielding no containment. The
                    // root was spawned with CREATE_NEW_PROCESS_GROUP (root_flags),
                    // so `terminate`'s CTRL_BREAK still reaches the group.
                    return Ok(Tree::contained(Mechanism::Walk(Walker::new(id?))));
                }
                Err(failure) => return Err(failure),
            }
        } else if prepared.mode.is_some() {
            // Nested member: it inherits the ancestor's job (or the root's tree-walk; no
            // new job is created), so it owns no teardown — the outermost root's job tears
            // the whole tree down.
            return Ok(Tree::delegated());
        }
    }

    // Uncontained (or unsupported platform).
    let _ = prepared;
    Ok(Tree::none())
}

#[cfg(test)]
#[path = "dispatch_tests.rs"]
mod dispatch_tests;