capsule-runtime 0.8.0

The runtime that executes Murmur capsules. Runs packaged AI agents in isolation, enforcing WASI capability grants on a Wasmtime component sandbox.
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
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
//! Launching a delegated child as an operating-system process of the parent's own runtime.
//!
//! The daemon referees and hands back an approval; this is what turns that approval into a
//! running capsule. The parent's runtime composes a directory for the child beneath its own
//! accessible workdir, builds the child's environment from the child's declaration rather than
//! its own, and starts the `mur` binary as a subprocess. The child's runtime registers with the
//! daemon for itself (see [`crate::registration`]), so the parent never states what the child
//! holds — it only names which artifact is running.
//!
//! Three properties fall out of the child being a process rather than a thread:
//!
//! * A daemon crash takes no child with it. Nothing the child needs to keep running lives in the
//!   daemon's address space.
//! * Each child has its own process environment and working directory, so a native subprocess
//!   started by one child inherits nothing a sibling shares.
//! * A child that declares the `sealed` containment floor enters a mount namespace of its own,
//!   because the containment machinery installs per process and the child *is* a process.
//!
//! **A child joins its parent's formation; it never starts one.** A parent session that belongs to
//! a formation hands its id to every child as [`FORMATION_ID_ENV`], beside the spawner handle and
//! independent of it, so a launch that names no lineage still carries it. A parent in no
//! formation hands none. Inheriting the id is not a grant: the child gets no formation channel, no
//! formation token, no callee and no formation lifeline, so its door has no membership and refuses
//! every formation token.
//!
//! **A child dies with the process that spawned it.** Every child, in a formation or not, is
//! handed a spawner lifeline of its own ([`crate::lifeline::ChildLifeline`], named by
//! [`SPAWNER_LIFELINE_ENV`]): the read end of a pipe whose only write end this process holds.
//! When this process ends, by any means, the kernel closes the write end and the child winds down
//! as a first `SIGTERM` would wind it down. A child that delegates hands its own children a
//! lifeline of its own the same way, so a grandchild ends once its parent has. A launch whose
//! lifeline cannot be created is refused rather than started without one.
//!
//! **The approval travels on the child's standard input.** Not on the argument vector and not in
//! the environment: both are readable from `/proc/<pid>` by any process running as the same user,
//! which is exactly what a sibling capsule's shell tool is.

use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, ExitStatus, Stdio};
use std::sync::{mpsc, Arc, Condvar, Mutex};
use std::time::{Duration, Instant};

use crate::delegation::{
    self, CompletionAddress, DelegationOutcome, DelegationStatus, Reporter, Spawner, SpawnerHandle,
    SPAWNER_ENV,
};
use crate::errors::RuntimeError;
use crate::formation::{FormationId, FORMATION_ID_ENV};
use crate::host_warnings::HOST_WARNINGS_REPORTED_ENV;
use crate::lifeline::SPAWNER_LIFELINE_ENV;
use crate::mac_token;
use crate::spawn_credential::SpawnApproval;
use crate::workdir_fs::WorkdirRoot;

/// Environment variable naming the `mur` binary to launch children with.
///
/// Defaults to [`std::env::current_exe`], which is correct in production: the process doing the
/// launching *is* `mur`. A test harness that runs inside its own test binary sets this to the
/// built `mur` instead.
pub const MUR_BINARY_ENV: &str = "MURMUR_MUR_BINARY";

/// The hidden `mur run` flag that puts every door token the session minted on its `--json`
/// readiness line, under `tokens`. Passed only by a launcher reading that line on a pipe nothing
/// else holds — a parent runtime launching a delegated child, and `mur run --roster` launching a
/// peer — because it is the one `mur run` output that carries a token.
pub const READINESS_TOKENS_FLAG: &str = "--readiness-tokens";

/// The hidden `mur run` flag carrying `<st_dev>:<st_ino>` of the directory a delegating parent
/// created for its child, which the child's staging compares with the `--workdir` it opened
/// before writing anything there (`E-RUN-051` on a mismatch).
pub const WORKDIR_IDENTITY_FLAG: &str = "--workdir-identity";

/// `(st_dev, st_ino)` in [`WORKDIR_IDENTITY_FLAG`]'s `<dev>:<ino>` form.
#[must_use]
pub fn format_workdir_identity(identity: (u64, u64)) -> String {
    format!("{}:{}", identity.0, identity.1)
}

/// Parses [`WORKDIR_IDENTITY_FLAG`]'s `<dev>:<ino>` value.
pub fn parse_workdir_identity(value: &str) -> Result<(u64, u64), String> {
    let (dev, ino) = value
        .split_once(':')
        .ok_or_else(|| format!("{WORKDIR_IDENTITY_FLAG} takes <dev>:<ino>, not {value:?}"))?;
    let number = |part: &str| {
        part.parse::<u64>()
            .map_err(|_| format!("{WORKDIR_IDENTITY_FLAG} takes <dev>:<ino>, not {value:?}"))
    };
    Ok((number(dev)?, number(ino)?))
}

/// How long the parent waits for a child to print its `--json` launch line.
///
/// Generous, because it bounds more than a handshake: a script capsule prints the line when its
/// run has *finished*, and an agent capsule compiles its driver component before it binds a port,
/// which on a cold host is the slowest thing in a launch.
const CHILD_READY_TIMEOUT: Duration = Duration::from_secs(180);

/// Owner-only, so a sibling capsule running as the same user cannot list or read a child's
/// directory even though both sit under one parent.
#[cfg(unix)]
const CHILD_DIR_MODE: u32 = 0o700;

/// How many of the child's last stderr lines are kept to explain a launch that never reported.
///
/// The child's diagnostics are the operator's, so every line is echoed to this process's stderr as
/// it arrives; this bound is only what a *failure* quotes back, so a child that logged for an hour
/// before dying does not turn into an unbounded error string.
const CHILD_STDERR_TAIL_LINES: usize = 20;

/// How long a post-mortem read of the tail waits for the drain to reach the end of the pipe.
///
/// Bounds a thread being scheduled, not the child: by the time anything asks for a dead child's
/// tail the exit has already closed the write end, so the drain is at most one wake-up away.
/// A pipe some surviving grandchild still holds open would never reach EOF at all, and the bound
/// is what stops that inheriting process from wedging the parent's launch or its watcher.
const CHILD_STDERR_DRAIN_TIMEOUT: Duration = Duration::from_secs(5);

/// How often the completion watcher asks whether the child is still running.
///
/// It polls rather than blocking on `wait`, because the process handle is shared with
/// [`LaunchedChild::shutdown`] and [`Drop`]: a watcher blocked inside `wait` would hold the lock
/// those need, and a second `wait` on one child is not a thing two owners can both do.
const CHILD_WATCH_INTERVAL: Duration = Duration::from_millis(100);

/// How long a child sent `SIGTERM` on purpose is given to wind down before it is `SIGKILL`ed.
///
/// The child's own `SIGTERM` teardown cancels its task and writes `session_end`, so within this
/// grace its trace says how it ended. Kept under `mur stop`'s default `--timeout`, so a parent
/// that is stopping can end its children and still exit inside its own grace.
///
/// Shorter than the child's own [`crate::runtime::TERMINATE_TEARDOWN_DEADLINE`]: a child whose
/// teardown runs past this grace (a long async-hook drain) is `SIGKILL`ed before it writes
/// `session_end`, and `mur trace show` reports its trace as ended without one.
pub const CHILD_END_GRACE: Duration = Duration::from_secs(5);

/// How often a child that has been sent `SIGTERM` is polled for its exit during
/// [`CHILD_END_GRACE`].
const CHILD_END_POLL: Duration = Duration::from_millis(20);

/// What the parent's runtime needs in order to start one approved child.
///
/// Carries no manifest and no capability declaration: what the child holds is decided by the
/// daemon from the child's own registry manifest, both at the `POST /spawn` that granted `grant`
/// and again at the `POST /register` the child performs for itself.
#[derive(Debug)]
pub struct ChildLaunchRequest {
    /// The parent's own records directory. The child's work folder is created in its
    /// [`CHILDREN_DIR`], with no link followed, outside the parent's work folder.
    pub parent_records_root: WorkdirRoot,
    pub capsule_name: String,
    pub capsule_version: String,
    /// The approval `POST /spawn` returned, spent by the child's own registration.
    pub grant: SpawnApproval,
    /// The child's `capabilities.env.allow`. Names listed here are copied from the parent's
    /// process environment into the child's; every other host variable the parent holds is
    /// absent from the child, because the child's environment is built from a cleared one.
    pub child_env_allow: Vec<String>,
    /// Base URL of the daemon the child registers with.
    pub roost_url: String,
    /// The session this child belongs to, or `None` for a launch that is not a delegation.
    ///
    /// `Some` is what turns the launch into a delegation: the launcher mints a delegation id and
    /// injects a [`SpawnerHandle`] as [`SPAWNER_ENV`], which is where the child reads the lineage
    /// it records in its own `session_start`. The watcher that reports for a child which could
    /// not report for itself starts only when that spawner also names a
    /// [`CompletionAddress`] — a parent waiting on its own connection wants no completion.
    /// `None` injects nothing and starts nothing.
    pub spawner: Option<Spawner>,
    /// How long the completion watcher observes this child before ending it, or `None` to observe
    /// it for as long as it runs.
    ///
    /// Meaningful only alongside a [`CompletionAddress`], because the watcher that enforces it
    /// starts only for a spawner that names one. On expiry the watcher stops the process and
    /// posts a [`DelegationStatus::Terminated`] outcome naming the bound, which is the only thing
    /// that tells a parent about a child that never ends.
    pub completion_deadline: Option<Duration>,
    /// The parent session's formation, which the child joins: injected as [`FORMATION_ID_ENV`]
    /// when `Some`, whether or not [`Self::spawner`] is. Inherited from the parent and never
    /// minted here; `None` — a parent in no formation — injects nothing.
    pub formation_id: Option<FormationId>,
    /// The parent session's deadline, handed to the child as
    /// [`crate::session_status::INHERITED_DEADLINE_FLAG`] and its moment, so the child's own runtime
    /// ends it there. `None` bounds nothing. A script-capsule child takes no session deadline from
    /// it, and ends with its parent through its spawner lifeline.
    pub deadline: Option<crate::session_status::SessionDeadline>,
}

/// How a child's process ended, as the watcher sees it.
enum Ending {
    /// The process exited on its own. `None` when it was reaped by someone who did not keep the
    /// status.
    Exited(Option<ExitStatus>),
    /// The delegation was ended on purpose rather than by the child: by the parent, through
    /// [`LaunchedChild::shutdown`] or `Drop`, or by the completion watcher at its deadline.
    Deliberate,
}

/// The one owner of the child's process handle.
///
/// Shared behind a lock between [`LaunchedChild`] — which kills and reaps — and the completion
/// watcher, which only ever asks whether the process is still there. Single ownership of the
/// `Child` is what keeps `pid()`, `shutdown()` and `Drop` behaving as they did: nothing else
/// takes the handle, and nothing else waits on it.
struct ChildProcess {
    /// `None` once [`LaunchedChild::shutdown`] or `Drop` has reaped the process.
    child: Option<Child>,
    /// Set before the kill by whoever ended the delegation on purpose, so a crash is never read
    /// as a termination. It does not say *who*: the completion watcher sets it at its own
    /// deadline too, and tells the two apart by a flag of its own.
    deliberate: bool,
    /// The exit status, once anyone has observed it.
    status: Option<ExitStatus>,
    /// When `SIGTERM` was sent, so a child signalled ahead of its reap — see
    /// [`LaunchedChild::begin_ending`] — is given one [`CHILD_END_GRACE`] in all, not one per
    /// call.
    terminated_at: Option<Instant>,
    /// The write end of the child's spawner lifeline, open for as long as the child is this
    /// process's to end. Closed once the child is observed ended, when it is ended deliberately
    /// — by [`LaunchedChild::shutdown`], `Drop` or the completion watcher's deadline — or when
    /// this process exits. Nothing writes to it.
    #[cfg(unix)]
    lifeline: Option<crate::lifeline::ChildLifeline>,
}

impl ChildProcess {
    /// Close the write end of the child's spawner lifeline, if this still holds it.
    fn close_lifeline(&mut self) {
        #[cfg(unix)]
        if let Some(mut lifeline) = self.lifeline.take() {
            lifeline.close();
        }
    }

    /// How the process ended, or `None` while it is still running.
    ///
    /// Reaps a process that exited on its own, which is what makes a later `wait` in `shutdown`
    /// return the cached status rather than block.
    fn poll(&mut self) -> Option<Ending> {
        if self.deliberate {
            // A child still winding down from `SIGTERM` has not ended yet: whatever it records
            // meanwhile must land before the watcher records the ending over it.
            return (!self.is_being_ended()).then_some(Ending::Deliberate);
        }
        let Some(child) = self.child.as_mut() else {
            // Reaped by `Drop` after [`LaunchedChild::has_exited`] had already seen the exit:
            // the child ended on its own.
            return Some(Ending::Exited(self.status));
        };
        let ending = match child.try_wait() {
            Ok(Some(status)) => {
                self.status = Some(status);
                Ending::Exited(Some(status))
            }
            Ok(None) => return None,
            // The handle is unusable; treating the child as gone is the only outcome that lets
            // the delegation be reported at all.
            Err(_) => Ending::Exited(self.status),
        };
        self.close_lifeline();
        Some(ending)
    }

    /// Mark the ending deliberate and send `SIGTERM` to a child that has not exited, once. A
    /// child already seen exited ended on its own and stays so; one found exited here is reaped
    /// instead, so the signal can never reach a reused pid: an unreaped child's pid stays its own.
    ///
    /// Marked under the same lock as the signal, so the watcher never reads a deliberate ending
    /// that has not been signalled.
    #[allow(unsafe_code)]
    fn terminate(&mut self) {
        if self.status.is_none() {
            self.deliberate = true;
        }
        if self.terminated_at.is_some() {
            return;
        }
        let Some(child) = self.child.as_mut() else {
            return;
        };
        if let Ok(Some(status)) = child.try_wait() {
            self.status = Some(status);
            return;
        }
        #[cfg(unix)]
        if let Ok(pid) = libc::pid_t::try_from(child.id()) {
            // SAFETY: `kill` takes no pointers. `pid` is this process's own unreaped child:
            // `try_wait` above saw it running, under the lock every reap of it takes, so the pid
            // cannot have been reused by an unrelated process.
            unsafe {
                libc::kill(pid, libc::SIGTERM);
            }
        }
        #[cfg(not(unix))]
        let _ = child.kill();
        self.terminated_at = Some(Instant::now());
    }

    /// Whether the child has been sent `SIGTERM` on purpose and not yet reaped: it is winding
    /// down, and anything it reports meanwhile is about an ending its parent chose.
    fn is_being_ended(&self) -> bool {
        self.deliberate && self.status.is_none() && self.terminated_at.is_some()
    }
}

/// End the child: `SIGTERM`, up to [`CHILD_END_GRACE`] from that signal for it to exit, then
/// `SIGKILL`; reap it, then close its lifeline. A child that has already exited is only reaped.
/// Idempotent.
///
/// The lock is held only to signal and to record the reap, never across the grace, so `pid`,
/// `poll` and [`LaunchedChild::is_being_ended`] still answer while the child winds down. A second
/// call during that wait returns at once and leaves the reap to the first. The lifeline stays
/// open until the child is reaped, so a child winding down from `SIGTERM` never also reads its
/// spawner as gone.
fn end_process(process: &Mutex<ChildProcess>) -> Result<(), std::io::Error> {
    let (mut child, signalled) = {
        let mut process = lock(process);
        process.terminate();
        let Some(child) = process.child.take() else {
            // Taken by an ending still waiting out the grace, which closes the lifeline itself
            // once it has reaped the child.
            if process.status.is_some() || process.terminated_at.is_none() {
                process.close_lifeline();
            }
            return Ok(());
        };
        (child, process.terminated_at)
    };
    let waited = match signalled {
        None => child.wait(),
        Some(signalled) => loop {
            match child.try_wait() {
                Ok(Some(status)) => break Ok(status),
                Ok(None) if signalled.elapsed() < CHILD_END_GRACE => {
                    std::thread::sleep(CHILD_END_POLL);
                }
                // Past the grace, or the handle cannot be polled: `kill` on an already-exited
                // process is not an error worth surfacing, and the wait is what retires the entry
                // in the process table.
                _ => {
                    let _ = child.kill();
                    break child.wait();
                }
            }
        },
    };
    let mut process = lock(process);
    process.close_lifeline();
    match waited {
        Ok(status) => {
            process.status = Some(status);
            Ok(())
        }
        // The handle is gone and nothing is left to wait for: the ending is over, with no status.
        Err(error) => {
            process.terminated_at = None;
            Err(error)
        }
    }
}

/// A running child capsule and the handles to end it.
pub struct LaunchedChild {
    /// The directory the parent created for this child in its records directory's
    /// [`CHILDREN_DIR`], and passed as its `--workdir`: the child's work folder. Its trace, its
    /// `completion.json` and an agent child's `out/` are in [`Self::records_dir`].
    pub workdir: PathBuf,
    /// [`Self::workdir`], held, reached from the parent's records root with no link followed and
    /// sharing its refusal latch: every runtime read of a script child's result goes through it.
    pub(crate) workdir_root: WorkdirRoot,
    /// The session id the child's runtime minted for itself.
    pub session_id: String,
    /// The child's records directory, as the child reported it on its readiness line: where its
    /// trace, its `completion.json` and an agent child's `out/` are. Never derived from
    /// [`Self::workdir`].
    pub records_dir: PathBuf,
    /// [`Self::records_dir`], held, sharing [`Self::workdir_root`]'s refusal latch: every read and
    /// write of the child's `completion.json` and every read of an agent child's result goes
    /// through it.
    pub(crate) records_root: WorkdirRoot,
    /// The child's A2A endpoint, `http://host:port`. Empty for a script capsule, which binds no
    /// port and has already finished by the time it reports.
    pub capsule_url: String,
    /// The argument vector the parent built, in order, starting with the binary.
    ///
    /// Exposed so a caller — a test, notably — can assert what was passed without reading
    /// `/proc`. It carries no token: the approval goes in on standard input.
    pub argv: Vec<String>,
    /// The complete environment the parent built for this child, in insertion order. Also carries
    /// no token.
    pub env: Vec<(String, String)>,
    /// The id this launch's completion reports under, `None` when no spawner was supplied.
    ///
    /// Minted here, injected into the child as part of [`SPAWNER_ENV`], and echoed back on the
    /// completion — the one value that joins a delegation to the task it produces at the parent.
    pub delegation_id: Option<String>,
    /// The operator token of a child that declares `network.authentication`, read from
    /// `tokens.operator` on its readiness line. The parent presents it on every call to the
    /// child's door. Never printed by `Debug`.
    pub door_token: Option<crate::door_auth::DoorToken>,
    /// The formation the child reports it joined, read from `formation_id` on its readiness
    /// line. `None` when the line carries no such key, or a value that is not a formation id.
    pub formation_id: Option<FormationId>,
    /// The id the child gave the task its parent delivered, once [`crate::delegation_plane`] has
    /// delivered one: what names the child's per-task result file when its parent reads the
    /// answer. `None` until then.
    pub(crate) task_id: Option<String>,
    process: Arc<Mutex<ChildProcess>>,
    /// The child's last [`CHILD_STDERR_TAIL_LINES`] lines, retained so a crash can say why.
    stderr_tail: Arc<StderrTail>,
    /// When the child process was started, for the completion's `duration_ms`.
    started: Instant,
    /// The completion watcher's thread, for a launch whose spawner names an address.
    watcher: Option<std::thread::JoinHandle<()>>,
}

impl std::fmt::Debug for LaunchedChild {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LaunchedChild")
            .field("workdir", &self.workdir)
            .field("session_id", &self.session_id)
            .field("records_dir", &self.records_dir)
            .field("capsule_url", &self.capsule_url)
            .field("delegation_id", &self.delegation_id)
            .field("formation_id", &self.formation_id)
            .field(
                "door_token",
                &self.door_token.as_ref().map(|_| "<redacted>"),
            )
            .field("pid", &self.pid())
            .finish()
    }
}

impl LaunchedChild {
    /// The child's operating-system process id. `0` once it has been reaped.
    pub fn pid(&self) -> u32 {
        lock(&self.process)
            .child
            .as_ref()
            .map(Child::id)
            .unwrap_or(0)
    }

    /// The child's last [`CHILD_STDERR_TAIL_LINES`] lines, oldest first.
    ///
    /// The same lines a crash completion quotes back, exposed so a caller can read what the child
    /// said without scraping the stderr they were already echoed to.
    pub fn stderr_tail(&self) -> Vec<String> {
        self.stderr_tail.lines()
    }

    /// Mark the ending deliberate and send the child `SIGTERM`, without waiting for it.
    ///
    /// A parent ending several children calls this on each before [`Self::shutdown`] or `Drop` on
    /// any, so they wind down together and the whole ending waits one [`CHILD_END_GRACE`] rather
    /// than one per child. A child already exited is left to be reaped.
    pub fn begin_ending(&self) {
        lock(&self.process).terminate();
    }

    /// End the child — `SIGTERM`, up to [`CHILD_END_GRACE`] to exit, then `SIGKILL` — and reap
    /// it. Idempotent: a second call, or a call after `Drop` has already run, does nothing.
    ///
    /// Marks the ending as deliberate, so the watcher records the delegation as `terminated` and
    /// posts nothing: the only party that would be told is the party that did it.
    pub fn shutdown(&mut self) -> Result<(), RuntimeError> {
        end_process(&self.process).map_err(|error| {
            RuntimeError::Runtime(format!("failed to reap child capsule: {error}"))
        })
    }

    /// Whether the child is winding down from a deliberate `SIGTERM` and has not been reaped.
    ///
    /// The door asks this of a completion that arrives for a delegation still in the set: such a
    /// completion is the child's own account of a cancel its parent's side chose, posted before
    /// the launcher's `terminated` record replaces it, and it is not the delegation's outcome.
    pub fn is_being_ended(&self) -> bool {
        lock(&self.process).is_being_ended()
    }

    /// Whether the completion watcher behind this child has finished: it has seen the child end
    /// and recorded, and posted, whatever it records and posts. `true` for a child no watcher
    /// observes.
    ///
    /// A delegating task that ends a child asks this before it lets the child's handle go, so the
    /// child's `completion.json` says how it ended before the parent's own process can exit.
    pub fn watcher_finished(&self) -> bool {
        self.watcher
            .as_ref()
            .is_none_or(std::thread::JoinHandle::is_finished)
    }

    /// Whether the child's process has ended, by its own exit or by being ended. The completion
    /// watcher's own poll: a process that exited on its own is reaped and its lifeline closed.
    ///
    /// Never blocks: a delegating task asks this while it gives a child that has just reported
    /// its outcome a moment to finish exiting.
    pub fn has_exited(&self) -> bool {
        lock(&self.process).poll().is_some()
    }
}

impl Drop for LaunchedChild {
    /// Ends and reaps as [`LaunchedChild::shutdown`] does, so a parent that returns early —
    /// including by panicking — leaves no orphaned capsule process behind holding a port and a
    /// directory. Deliberate on the same terms. A child that has already exited is only reaped.
    fn drop(&mut self) {
        let _ = end_process(&self.process);
    }
}

/// A mutex this crate holds only for the length of one field access, so a poisoned lock is
/// recovered from rather than turned into a panic in a `Drop`.
fn lock<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
    mutex
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}

/// Where a child of this parent goes: `<parent records dir>/children/<name>-<16 hex>`.
///
/// The 16 hex characters are fresh per call, so delegating the same capsule name and version twice
/// yields two directories rather than one shared one. In the parent's records directory, outside
/// its work folder, so `mur trace rm` and `trace.retain` remove a child's work folder with the
/// parent's records. `<name>` is the bare part of a full `capsule_name`, so the directory is one
/// segment whatever the scope.
pub fn child_workdir_for(parent_records_dir: &Path, capsule_name: &str) -> PathBuf {
    parent_records_dir.join(child_workdir_rel(capsule_name))
}

/// Every child's directory sits directly beneath this one, relative to the parent's records
/// directory.
pub(crate) const CHILDREN_DIR: &str = "children";

/// [`child_workdir_for`] relative to the parent's records directory: `children/<name>-<16 hex>`.
fn child_workdir_rel(capsule_name: &str) -> PathBuf {
    let suffix = mac_token::random_hex(8).unwrap_or_else(|_| {
        // The OS CSPRNG is not expected to fail; a nanosecond clock reading still distinguishes
        // two delegations rather than collapsing them onto one directory.
        format!(
            "{:016x}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|elapsed| elapsed.as_nanos() as u64)
                .unwrap_or(0)
        )
    });
    Path::new(CHILDREN_DIR).join(format!(
        "{}-{suffix}",
        murmur_artifact::tool_name_of(capsule_name)
    ))
}

/// Create the child's directory, start `mur` on it, and wait for the child to report itself.
///
/// The child is handed a spawner lifeline of its own; a launch whose lifeline cannot be created
/// starts nothing.
pub fn launch_child_capsule(request: ChildLaunchRequest) -> Result<LaunchedChild, RuntimeError> {
    // First, so a launch that cannot have one creates nothing: a child without a lifeline would
    // outlive this process.
    #[cfg(unix)]
    let mut lifeline = crate::lifeline::ChildLifeline::new().map_err(|error| {
        RuntimeError::Runtime(format!(
            "the child capsule's spawner lifeline could not be created: {error}"
        ))
    })?;
    let workdir_rel = child_workdir_rel(&request.capsule_name);
    let workdir_root = create_child_dir(&request.parent_records_root, &workdir_rel)?;
    let identity = workdir_root.identity()?;
    let workdir = workdir_root.path().to_path_buf();

    let binary = mur_binary()?;
    let mut argv: Vec<String> = vec![
        binary.display().to_string(),
        "run".to_string(),
        "--capsule".to_string(),
        request.capsule_name.clone(),
        "--capsule-version".to_string(),
        request.capsule_version.clone(),
        "--workdir".to_string(),
        workdir.display().to_string(),
        WORKDIR_IDENTITY_FLAG.to_string(),
        format_workdir_identity(identity),
        "--json".to_string(),
        READINESS_TOKENS_FLAG.to_string(),
        "--no-env-file".to_string(),
        "--spawn-grant-stdin".to_string(),
    ];
    if let Some(deadline) = request.deadline {
        argv.push(crate::session_status::INHERITED_DEADLINE_FLAG.to_string());
        argv.push(deadline.at_unix_millis().to_string());
    }
    // Minted here, once per launch: a caller holding one `Spawner` that delegates twice gets two
    // delegation ids, and neither child can report under the other's.
    let handle = request
        .spawner
        .as_ref()
        .map(|spawner| SpawnerHandle::for_delegation(spawner, delegation::new_delegation_id()));
    #[cfg_attr(not(unix), allow(unused_mut))]
    let mut env = child_environment(&request, handle.as_ref());
    // Recorded among the runtime-owned names, just ahead of the spawner handle it sits beside
    // (or the formation id, or the end), exactly as `hand_to` sets it in the child.
    #[cfg(unix)]
    if let Some((name, value)) = lifeline.env_pair() {
        let at = env
            .iter()
            .position(|(key, _)| key == SPAWNER_ENV || key == FORMATION_ID_ENV)
            .unwrap_or(env.len());
        env.insert(at, (name.to_string(), value));
    }
    let started = Instant::now();

    // The grant's one and only appearance outside the parent's memory: one line on a pipe that is
    // closed immediately afterwards. Failing to write it is fatal — the child would otherwise sit
    // waiting on a line that will never come.
    let StartedProcess {
        child,
        stderr_tail,
        stdin_written: write_result,
    } = start_process(&ProcessLaunch {
        binary: &binary,
        args: &argv[1..],
        cwd: Some(&workdir),
        env: ProcessEnv::Cleared(&env),
        stdin_line: Some(request.grant.expose()),
        stderr_prefix: None,
        inherit_output: false,
        own_process_group: false,
        inherit_fd: None,
        #[cfg(unix)]
        lifeline: Some(Lifeline::Child(&lifeline)),
    })
    .map_err(|error| {
        RuntimeError::Runtime(format!(
            "failed to start the child capsule process '{}': {error}",
            binary.display()
        ))
    })?;
    #[cfg(unix)]
    lifeline.spawned();
    let unclaimed = UnclaimedChild {
        process: Arc::new(Mutex::new(ChildProcess {
            child: Some(child),
            deliberate: false,
            status: None,
            terminated_at: None,
            #[cfg(unix)]
            lifeline: Some(lifeline),
        })),
        claimed: false,
    };
    if let Err(error) = write_result {
        return Err(RuntimeError::Runtime(format!(
            "failed to hand the child its launch grant: {error}"
        )));
    }

    let readiness = read_readiness(&unclaimed.process, &workdir, &stderr_tail, &workdir_root)?;
    let mut launched = LaunchedChild {
        workdir,
        workdir_root,
        session_id: readiness.session_id,
        records_dir: readiness.records_dir,
        records_root: readiness.records_root,
        capsule_url: readiness.capsule_url,
        argv,
        env,
        delegation_id: handle.as_ref().map(|handle| handle.delegation_id.clone()),
        door_token: readiness.door_token,
        formation_id: readiness.formation_id,
        task_id: None,
        process: unclaimed.claim(),
        stderr_tail,
        started,
        watcher: None,
    };

    // Started only for a launch somebody wants told about. A lineage-only handle names no
    // address, so a child whose parent waits on the connection it already holds is launched with
    // no watcher behind it and posts nothing anywhere.
    if let Some(handle) = handle {
        if let Some(address) = handle.report_to.clone() {
            launched.watcher = watch_for_completion(
                &launched,
                handle,
                address,
                &request.capsule_name,
                &request.capsule_version,
                request.completion_deadline,
            );
        }
    }
    Ok(launched)
}

/// A started child process not yet held by a [`LaunchedChild`]: dropped unclaimed, it ends and
/// reaps the child as `Drop for LaunchedChild` does, so a launch that fails, or panics, between
/// starting the process and reading its readiness line leaves no capsule process behind.
struct UnclaimedChild {
    process: Arc<Mutex<ChildProcess>>,
    claimed: bool,
}

impl UnclaimedChild {
    /// The process, for a [`LaunchedChild`] that ends it from here on.
    fn claim(mut self) -> Arc<Mutex<ChildProcess>> {
        self.claimed = true;
        Arc::clone(&self.process)
    }
}

impl Drop for UnclaimedChild {
    fn drop(&mut self) {
        if !self.claimed {
            let _ = end_process(&self.process);
        }
    }
}

/// What a child's `--json` readiness line reports, validated.
struct Readiness {
    session_id: String,
    records_dir: PathBuf,
    /// `records_dir`, held, sharing the parent root's refusal latch.
    records_root: WorkdirRoot,
    door_token: Option<crate::door_auth::DoorToken>,
    formation_id: Option<FormationId>,
    /// `http://host:port`, or empty for a script capsule.
    capsule_url: String,
}

/// Read the child's readiness line and validate what it reports. The `Err` refuses the
/// delegation; ending the child is the caller's.
fn read_readiness(
    process: &Mutex<ChildProcess>,
    workdir: &Path,
    stderr_tail: &StderrTail,
    latch_of: &WorkdirRoot,
) -> Result<Readiness, RuntimeError> {
    let line = first_json_line(process, workdir, stderr_tail)?;
    let report: serde_json::Value = serde_json::from_str(&line).map_err(|error| {
        RuntimeError::Runtime(format!(
            "the child capsule's first --json line did not parse: {error}"
        ))
    })?;
    let session_id = report
        .get("session_id")
        .and_then(serde_json::Value::as_str)
        .ok_or_else(|| {
            RuntimeError::Runtime(
                "the child capsule's --json line carried no session_id".to_string(),
            )
        })?;
    let url = report
        .get("url")
        .and_then(serde_json::Value::as_str)
        .unwrap_or_default();

    let records_dir = readiness_records_dir(&report, session_id)?;
    let records_root =
        WorkdirRoot::open_sharing_latch_absolute(&records_dir, latch_of).map_err(|error| {
            RuntimeError::Runtime(format!(
                "the child capsule's records directory could not be opened: {error}"
            ))
        })?;
    Ok(Readiness {
        session_id: session_id.to_string(),
        records_dir,
        records_root,
        door_token: readiness_door_token(&report),
        formation_id: report
            .get("formation_id")
            .and_then(serde_json::Value::as_str)
            .and_then(|id| FormationId::parse(id).ok()),
        // A script capsule binds no port and reports an empty url; promoting that to `http://`
        // would manufacture an address nothing answers on.
        capsule_url: if url.is_empty() {
            String::new()
        } else {
            format!("http://{url}")
        },
    })
}

/// The records directory a child's `--json` readiness line carries under `records_dir`: an
/// absolute path in this host's `~/.murmur/records`, ending in the child's own session id.
/// Anything else refuses the delegation rather than reading the child's outcome from somewhere
/// the line named.
pub(crate) fn readiness_records_dir(
    report: &serde_json::Value,
    session_id: &str,
) -> Result<PathBuf, RuntimeError> {
    let refused = |why: String| {
        RuntimeError::Runtime(format!(
            "the child capsule's --json line {why}, so its outcome has nowhere to be read from"
        ))
    };
    let reported = report
        .get("records_dir")
        .and_then(serde_json::Value::as_str)
        .map(PathBuf::from)
        .ok_or_else(|| refused("carried no records_dir".to_string()))?;
    let records_root = crate::records_dir::records_dir_root()
        .map_err(|error| refused(format!("named a records directory and this host's {error}")))?;
    let well_formed = reported.is_absolute()
        && reported.starts_with(&records_root)
        && reported.file_name().and_then(|name| name.to_str()) == Some(session_id)
        && !reported
            .components()
            .any(|part| matches!(part, std::path::Component::ParentDir));
    if !well_formed {
        return Err(refused(format!(
            "named {} as its records directory, which is not {}/<workdir root>/{session_id}",
            reported.display(),
            records_root.display()
        )));
    }
    Ok(reported)
}

/// The operator token a child's `--json` readiness line carries under `tokens.operator`, present
/// only when the child declares `network.authentication` and was launched with
/// [`READINESS_TOKENS_FLAG`].
pub(crate) fn readiness_door_token(
    report: &serde_json::Value,
) -> Option<crate::door_auth::DoorToken> {
    report
        .pointer("/tokens/operator")
        .and_then(serde_json::Value::as_str)
        .filter(|token| !token.is_empty())
        .map(|token| crate::door_auth::DoorToken::new(token.to_string()))
}

/// Report for a child that ended without reporting for itself, and stop one that never ends.
///
/// Polls the shared process handle rather than blocking on `wait`, so it never contends with
/// [`LaunchedChild::shutdown`] or `Drop` for ownership of the `Child`. The handle stays with the
/// delegating task, which ends the child through it; this thread observes, and reaps a child that
/// exits on its own.
///
/// `deadline` bounds how long the child is watched, from the instant this watcher starts — which
/// is the instant the child reported itself ready, not the instant its process was spawned.
/// Getting to that point is bounded separately by [`CHILD_READY_TIMEOUT`], and folding a cold
/// host's staging time into the delegation deadline would end a child for being slow to start
/// rather than slow to work. On expiry the watcher stops the child itself and posts a
/// [`DelegationStatus::Terminated`] outcome naming the bound — the deadline is the watcher's own,
/// held in a local flag rather than read back off [`ChildProcess::deliberate`], so an ending the
/// *parent* chose stays distinguishable from one this thread imposed. `None` watches for as long
/// as the child runs.
///
/// What it does when the process ends on its own is decided by what the child left behind:
///
/// * A completion the child already delivered is left alone — exactly one completion per
///   delegation reaches the parent.
/// * A completion the child recorded but could not deliver is retried once, and the file is
///   rewritten with the result. Nothing is retried after that.
/// * No completion at all means the child died without a word: the watcher builds one with
///   `status: crashed`, carrying the exit status and the child's bounded stderr tail, and posts
///   it in the child's place.
/// * An ending the parent chose is recorded as `terminated` and posted to nobody, because the
///   only party that would be told is the party that did it.
fn watch_for_completion(
    launched: &LaunchedChild,
    handle: SpawnerHandle,
    address: CompletionAddress,
    capsule_name: &str,
    capsule_version: &str,
    deadline: Option<Duration>,
) -> Option<std::thread::JoinHandle<()>> {
    let process = Arc::clone(&launched.process);
    let stderr_tail = Arc::clone(&launched.stderr_tail);
    let workdir = launched.workdir.clone();
    let records_root = launched.records_root.clone();
    let session_id = launched.session_id.clone();
    let capsule_name = capsule_name.to_string();
    let capsule_version = capsule_version.to_string();
    let started = launched.started;

    let watching_from = Instant::now();
    let spawned = std::thread::Builder::new().spawn(move || {
        let expires_at = deadline.map(|bound| watching_from + bound);
        // Set here and read nowhere else: `ChildProcess::deliberate` is about to be set too, by
        // this thread, so it can no longer say who chose the ending.
        let mut expired = false;
        let ending = loop {
            if expires_at.is_some_and(|at| Instant::now() >= at) {
                let _ = end_process(&process);
                expired = true;
                break Ending::Deliberate;
            }
            if let Some(ending) = lock(&process).poll() {
                break ending;
            }
            std::thread::sleep(CHILD_WATCH_INTERVAL);
        };
        let duration_ms = started.elapsed().as_millis().try_into().unwrap_or(u64::MAX);

        // A child ended on purpose may have recorded its own `canceled` completion while it wound
        // down from `SIGTERM`. That record is not the outcome: the delegation was ended, and the
        // launcher's record below is written over it.
        let recorded = match ending {
            Ending::Exited(_) => delegation::read_completion(&records_root),
            Ending::Deliberate => None,
        };
        if let Some(recorded) = recorded {
            if recorded.delivered {
                return;
            }
            let retried =
                delegation::report_completion(&handle, &address, recorded, &records_root);
            if !retried.delivered {
                // One retry, then the file and the line above it are the record.
                crate::runtime_err!(
                    "[capsule-runtime] delegation {}: the child's completion is undelivered after one retry",
                    handle.delegation_id
                );
            }
            return;
        }

        let outcome = |status: DelegationStatus, detail: String| DelegationOutcome {
            delegation_id: handle.delegation_id.clone(),
            capsule_name: capsule_name.clone(),
            capsule_version: capsule_version.clone(),
            session_id: session_id.clone(),
            status,
            // The launcher reports for a child that recorded nothing, so it makes no claim about
            // a result file it never saw named.
            result_path: None,
            workdir: workdir.display().to_string(),
            duration_ms,
            detail: Some(detail),
            reported_by: Reporter::Launcher,
            delivered: false,
            delivery_error: None,
        };

        match ending {
            // The deadline is the parent's runtime acting on the parent's behalf while the parent
            // is elsewhere, so unlike an ending the parent chose by hand this one is posted: the
            // delegating agent asked for an outcome and this is the outcome.
            Ending::Deliberate if expired => {
                let bound = deadline.map(|bound| bound.as_secs()).unwrap_or_default();
                delegation::report_completion(
                    &handle,
                    &address,
                    outcome(
                        DelegationStatus::Terminated,
                        format!(
                            "the capsule was still running after {bound}s and was ended at the \
                             delegation deadline"
                        ),
                    ),
                    &records_root,
                );
            }
            Ending::Deliberate => {
                delegation::record_terminated(
                    &records_root,
                    outcome(
                        DelegationStatus::Terminated,
                        delegation::PARENT_ENDED_DETAIL.to_string(),
                    ),
                );
            }
            Ending::Exited(status) => {
                let status_text = match status {
                    Some(status) => status.to_string(),
                    None => "unknown exit status".to_string(),
                };
                let tail = stderr_tail.lines_at_end().join("\n");
                let detail = if tail.is_empty() {
                    format!(
                        "the child process ended without recording a completion ({status_text})"
                    )
                } else {
                    format!(
                        "the child process ended without recording a completion ({status_text}); \
                         its last stderr lines were:\n{tail}"
                    )
                };
                delegation::report_completion(
                    &handle,
                    &address,
                    outcome(DelegationStatus::Crashed, detail),
                    &records_root,
                );
            }
        }
    });
    match spawned {
        Ok(watcher) => Some(watcher),
        Err(error) => {
            crate::runtime_err!(
                "[capsule-runtime] delegation {}: the child's completion watcher could not be \
                 started: {error}",
                launched.delegation_id.as_deref().unwrap_or_default()
            );
            None
        }
    }
}

/// The child's complete environment, built from a cleared one.
///
/// `capabilities.env.allow` is the child's own, not the parent's: a sibling's declaration reaches
/// nothing here, and a variable the parent holds but the child did not declare is simply absent.
/// The runtime-owned names — `PATH`, `HOME`, `LOGNAME` and `USER` ([`crate::shell::ACCOUNT_ENV`]),
/// `MURMUR_ROOST_URL`, [`HOST_WARNINGS_REPORTED_ENV`], [`SPAWNER_ENV`] on a delegated launch and
/// [`FORMATION_ID_ENV`] for a parent in a formation, in that order — are applied last, so a child
/// cannot displace the daemon URL it is required to register with, the handle it reports its
/// outcome to, or the formation it joins, by allowlisting the name. `PATH`, `HOME` and the account
/// names carry this process's values when it holds them and are absent when it does not, so a
/// child's harness and commands run as the account this process runs as.
/// [`HOST_WARNINGS_REPORTED_ENV`] is always `1`: the host-level warnings were printed by the
/// process the operator started, so a child does not repeat them, and its manifest cannot choose
/// otherwise.
///
/// Two kinds of name are never copied from this process's environment, whatever the child
/// allowlists:
///
/// * A formation member's names ([`crate::formation::is_member_grant_env`]): inheriting a
///   formation is not a grant, so a member's child holds no channel, no token, no callee and no
///   formation lifeline.
/// * [`SPAWNER_LIFELINE_ENV`]: a value here names a descriptor of this process's own, the lifeline
///   its own spawner handed it. The child holds a spawner lifeline of its own instead, which
///   [`launch_child_capsule`] hands it alongside this environment.
pub(crate) fn child_environment(
    request: &ChildLaunchRequest,
    handle: Option<&SpawnerHandle>,
) -> Vec<(String, String)> {
    let mut env: Vec<(String, String)> = Vec::new();
    for key in &request.child_env_allow {
        if let Ok(value) = std::env::var(key) {
            env.push((key.clone(), value));
        }
    }
    env.retain(|(key, _)| {
        !matches!(key.as_str(), "PATH" | "HOME" | "MURMUR_ROOST_URL")
            && !crate::shell::ACCOUNT_ENV.contains(&key.as_str())
            && key != SPAWNER_ENV
            && key != FORMATION_ID_ENV
            && key != SPAWNER_LIFELINE_ENV
            && key != HOST_WARNINGS_REPORTED_ENV
            && !crate::formation::is_member_grant_env(key)
    });

    if let Ok(path) = std::env::var("PATH") {
        env.push(("PATH".to_string(), path));
    }
    if let Ok(home) = std::env::var("HOME") {
        env.push(("HOME".to_string(), home));
    }
    env.extend(
        crate::shell::HostAccountEnv::from_host()
            .set()
            .iter()
            .cloned(),
    );
    env.push(("MURMUR_ROOST_URL".to_string(), request.roost_url.clone()));
    env.push(crate::host_warnings::reported_env_pair());
    // Last, with the other runtime-owned names, and only for a launch that has a spawner: a child
    // nobody wants told holds no handle at all, whatever this process's own environment carries
    // and whatever the child declared.
    if let Some(handle) = handle {
        env.push((SPAWNER_ENV.to_string(), handle.to_env_value()));
    }
    // A parent in no formation hands none on, whatever this process's own environment carries.
    if let Some(formation_id) = &request.formation_id {
        let (name, value) = formation_id.env_pair();
        env.push((name.to_string(), value));
    }
    env
}

/// The binary a child is started from: [`MUR_BINARY_ENV`] when set, else this process's own image.
pub(crate) fn mur_binary() -> Result<PathBuf, RuntimeError> {
    if let Some(path) = std::env::var_os(MUR_BINARY_ENV) {
        if !path.is_empty() {
            return Ok(PathBuf::from(path));
        }
    }
    std::env::current_exe().map_err(|error| {
        RuntimeError::Runtime(format!(
            "cannot locate the mur binary to launch a child capsule with: {error}"
        ))
    })
}

/// Creates `rel` beneath `records_root`, [`CHILDREN_DIR`] and the directory itself both at
/// [`CHILD_DIR_MODE`], no link followed, and returns it held, reached the same way: its identity
/// is what the child's `--workdir` is checked against. A link on the way is
/// [`RuntimeError::WorkdirPathRefused`].
fn create_child_dir(records_root: &WorkdirRoot, rel: &Path) -> Result<WorkdirRoot, RuntimeError> {
    records_root.create_dir_all(CHILDREN_DIR, CHILD_DIR_MODE)?;
    records_root.create_dir_all(rel, CHILD_DIR_MODE)?;
    records_root
        .open_beneath(&rel.to_string_lossy())?
        .ok_or_else(|| {
            RuntimeError::Runtime(format!(
                "the child capsule's directory {} vanished after its creation",
                records_root.display_path(rel)
            ))
        })
}

/// The child's stderr as the parent keeps it: every line echoed to this process's stderr as it
/// arrives, with the last [`CHILD_STDERR_TAIL_LINES`] retained so a child that dies before
/// reporting can say why.
pub(crate) struct StderrTail {
    state: Mutex<StderrState>,
    /// Notified once [`StderrState::at_eof`] is set.
    drained: Condvar,
}

#[derive(Default)]
struct StderrState {
    lines: Vec<String>,
    /// Whether the pipe has been read to its end, which the child's exit is what causes.
    at_eof: bool,
}

impl StderrTail {
    /// What the child has said so far, without waiting for anything. For a child that is still
    /// running, which is the only state in which "so far" is the question being asked.
    pub(crate) fn lines(&self) -> Vec<String> {
        lock(&self.state).lines.clone()
    }

    /// What a child that has ended left behind, read only once the pipe is at its end.
    ///
    /// The exit that closes the child's stdout closes its stderr in the same breath, so the thread
    /// that notices the ending and the thread still draining stderr are racing, and the tail is
    /// empty for as long as the drain has not caught up. Reading it unsynchronised turns "refused
    /// because the declared floor is unmeetable here" into a refusal that names no reason —
    /// the one case the tail is kept for. Bounded by [`CHILD_STDERR_DRAIN_TIMEOUT`].
    pub(crate) fn lines_at_end(&self) -> Vec<String> {
        let (state, _) = self
            .drained
            .wait_timeout_while(lock(&self.state), CHILD_STDERR_DRAIN_TIMEOUT, |state| {
                !state.at_eof
            })
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        state.lines.clone()
    }

    /// Mark the pipe finished and wake every post-mortem reader waiting on it.
    fn finish(&self) {
        lock(&self.state).at_eof = true;
        self.drained.notify_all();
    }
}

/// Start draining the child's stderr into a [`StderrTail`].
///
/// The child's diagnostics belong to the operator running the parent, so nothing is swallowed —
/// this only remembers, in addition to printing. `prefix`, when given, is written in front of
/// every echoed line and kept off the remembered ones.
fn drain_stderr(child: &mut Child, prefix: Option<&str>) -> Arc<StderrTail> {
    let tail = Arc::new(StderrTail {
        state: Mutex::new(StderrState::default()),
        drained: Condvar::new(),
    });
    let Some(stderr) = child.stderr.take() else {
        // No pipe is an end already reached: a reader that waited here would wait out the whole
        // bound for lines that can never arrive.
        tail.finish();
        return tail;
    };
    let collector = Arc::clone(&tail);
    let prefix = prefix.unwrap_or_default().to_string();
    std::thread::spawn(move || {
        for line in BufReader::new(stderr).lines().map_while(Result::ok) {
            crate::runtime_err!("{prefix}{line}");
            let mut state = lock(&collector.state);
            if state.lines.len() == CHILD_STDERR_TAIL_LINES {
                state.lines.remove(0);
            }
            state.lines.push(line);
        }
        collector.finish();
    });
    tail
}

/// Read the child's first standard-output line, then keep draining the pipe.
///
/// The drain matters: a child whose stdout pipe fills blocks forever, and a capsule that logs
/// after its launch line would otherwise deadlock against a parent that had stopped reading.
fn first_json_line(
    process: &Mutex<ChildProcess>,
    workdir: &Path,
    stderr_tail: &StderrTail,
) -> Result<String, RuntimeError> {
    let stdout = lock(process)
        .child
        .as_mut()
        .and_then(|child| child.stdout.take())
        .ok_or_else(|| {
            RuntimeError::Runtime("the child process exposed no standard output".to_string())
        })?;

    let rx = relay_readiness_line(stdout, AfterReadiness::Stdout);

    match rx.recv_timeout(CHILD_READY_TIMEOUT) {
        Ok(Some(line)) => Ok(line),
        Ok(None) => {
            let status = lock(process)
                .child
                .as_mut()
                .and_then(|child| child.wait().ok())
                .map(|status| status.to_string())
                .unwrap_or_else(|| "unknown".to_string());
            // The child's own refusal — an unmeetable containment floor, a registration the
            // daemon declined — is written to its stderr and is the only thing that explains this
            // failure, so it is quoted rather than replaced by a generic "did not start".
            let reason = stderr_tail.lines_at_end().join("\n");
            Err(RuntimeError::Runtime(format!(
                "the child capsule '{}' exited without reporting a launch line ({status}): {reason}",
                workdir.display()
            )))
        }
        Err(_) => Err(RuntimeError::Runtime(format!(
            "the child capsule '{}' did not report a launch line within {}s",
            workdir.display(),
            CHILD_READY_TIMEOUT.as_secs()
        ))),
    }
}

/// How a launched `mur run` process's environment is built.
pub(crate) enum ProcessEnv<'a> {
    /// Cleared, then exactly these pairs: a delegated child, which holds nothing of the parent's
    /// that it did not declare.
    Cleared(&'a [(String, String)]),
    /// This process's own, with `set` applied over it and every name in `remove` taken out: a
    /// formation member, which the operator launched and which reads the operator's environment.
    Inherited {
        set: &'a [(String, String)],
        remove: &'a [&'a str],
    },
}

/// Where a launched process's standard output goes once its readiness line has been read.
pub(crate) enum AfterReadiness {
    /// To this process's standard output, as written.
    Stdout,
    /// To this process's standard error, one line at a time behind this prefix.
    PrefixedStderr(String),
}

/// One `mur run` subprocess to start.
pub(crate) struct ProcessLaunch<'a> {
    pub(crate) binary: &'a Path,
    /// The argument vector after the binary.
    pub(crate) args: &'a [String],
    /// The working directory, or `None` for this process's own.
    pub(crate) cwd: Option<&'a Path>,
    pub(crate) env: ProcessEnv<'a>,
    /// One line written to the process's standard input, which is then closed; `None` gives it a
    /// null standard input.
    pub(crate) stdin_line: Option<&'a str>,
    /// Written in front of every stderr line echoed to this process's stderr.
    pub(crate) stderr_prefix: Option<&'a str>,
    /// Hand the process this process's own standard output and error rather than pipes. Its
    /// stdout is then not read here, and its stderr tail stays empty.
    pub(crate) inherit_output: bool,
    /// Start the process as the leader of a process group of its own, so a signal meant for this
    /// process's group — a terminal's `^C` — does not reach it, and its whole tree can be
    /// signalled at once.
    pub(crate) own_process_group: bool,
    /// One descriptor of this process's to hand the process at the same number: a formation
    /// member's channel. It is close-on-exec here, so no other process this one starts inherits
    /// it, and the flag is cleared in the started process alone, just before it execs.
    pub(crate) inherit_fd: Option<i32>,
    /// The lifeline whose read end the process is handed. Every lifeline end is created
    /// close-on-exec, and this is the one the process's spawn clears it on.
    #[cfg(unix)]
    pub(crate) lifeline: Option<Lifeline<'a>>,
}

/// The lifeline a started process is handed: a formation member's, or a delegated child's.
#[cfg(unix)]
#[derive(Clone, Copy)]
pub(crate) enum Lifeline<'a> {
    Member(&'a crate::lifeline::MemberLifeline),
    Child(&'a crate::lifeline::ChildLifeline),
}

#[cfg(unix)]
impl Lifeline<'_> {
    fn hand_to(self, command: &mut Command) {
        match self {
            Lifeline::Member(lifeline) => lifeline.hand_to(command),
            Lifeline::Child(lifeline) => lifeline.hand_to(command),
        }
    }
}

/// A process [`start_process`] started, with its stderr already being drained.
pub(crate) struct StartedProcess {
    /// Its standard output, unless inherited, is still piped and unread.
    pub(crate) child: Child,
    pub(crate) stderr_tail: Arc<StderrTail>,
    /// Whether [`ProcessLaunch::stdin_line`] was written: `Ok` when there was none to write.
    pub(crate) stdin_written: Result<(), String>,
}

/// Start one `mur run` subprocess: spawn it, write its standard-input line, and start draining its
/// stderr. The one spawn site for a delegated child and a formation member alike.
pub(crate) fn start_process(launch: &ProcessLaunch<'_>) -> std::io::Result<StartedProcess> {
    let mut command = Command::new(launch.binary);
    command
        .args(launch.args)
        .stdin(if launch.stdin_line.is_some() {
            Stdio::piped()
        } else {
            Stdio::null()
        })
        .stdout(if launch.inherit_output {
            Stdio::inherit()
        } else {
            Stdio::piped()
        })
        .stderr(if launch.inherit_output {
            Stdio::inherit()
        } else {
            Stdio::piped()
        });
    if let Some(cwd) = launch.cwd {
        command.current_dir(cwd);
    }
    match launch.env {
        ProcessEnv::Cleared(pairs) => {
            command.env_clear();
            for (key, value) in pairs {
                command.env(key, value);
            }
        }
        ProcessEnv::Inherited { set, remove } => {
            for name in remove {
                command.env_remove(name);
            }
            for (key, value) in set {
                command.env(key, value);
            }
        }
    }
    #[cfg(unix)]
    if launch.own_process_group {
        use std::os::unix::process::CommandExt;
        command.process_group(0);
    }
    #[cfg(unix)]
    if let Some(fd) = launch.inherit_fd {
        keep_across_exec(&mut command, fd);
    }
    // No fd hygiene (`mark_inherited_fds_cloexec`) here: it fails the spawn outright on a kernel
    // without `CLOSE_RANGE_CLOEXEC`, and a lifeline needs none, since both ends are created
    // close-on-exec.
    // The lifeline's `pre_exec` is the last one registered.
    #[cfg(unix)]
    if let Some(lifeline) = launch.lifeline {
        lifeline.hand_to(&mut command);
    }

    let mut child = command.spawn()?;
    let stdin_written = match launch.stdin_line {
        None => Ok(()),
        Some(line) => child
            .stdin
            .take()
            .ok_or_else(|| "the process exposed no standard input".to_string())
            .and_then(|mut stdin| writeln!(stdin, "{line}").map_err(|error| error.to_string())),
    };
    let stderr_tail = drain_stderr(&mut child, launch.stderr_prefix);
    Ok(StartedProcess {
        child,
        stderr_tail,
        stdin_written,
    })
}

/// Clear `FD_CLOEXEC` on `fd` in the started process alone, after everything else that runs before
/// its `exec`, so that process inherits `fd` and no other does.
#[cfg(unix)]
#[allow(unsafe_code)]
fn keep_across_exec(command: &mut Command, fd: i32) {
    use std::os::unix::process::CommandExt;
    // SAFETY: the closure runs in the forked child between `fork` and `exec`, where only
    // async-signal-safe calls are allowed; it makes two `fcntl` calls on an integer descriptor,
    // which allocate nothing and take no lock.
    unsafe {
        command.pre_exec(move || {
            let flags = libc::fcntl(fd, libc::F_GETFD);
            if flags == -1 || libc::fcntl(fd, libc::F_SETFD, flags & !libc::FD_CLOEXEC) == -1 {
                return Err(std::io::Error::last_os_error());
            }
            Ok(())
        });
    }
}

/// Read a launched process's first standard-output line on a thread of its own, and hand it back
/// on the returned channel: `Some(line)` without its line ending, or `None` when the stream ended
/// first.
///
/// The thread then keeps draining the pipe to `after` for as long as the process lives. The drain
/// matters: a process whose stdout pipe fills blocks forever, and a capsule that logs after its
/// launch line would otherwise deadlock against a reader that had stopped reading. This thread
/// outlives the readiness line by the whole life of the process, so it is the one relay most
/// likely to meet a reader that has already gone: it relays each line and keeps draining either
/// way.
pub(crate) fn relay_readiness_line(
    stdout: std::process::ChildStdout,
    after: AfterReadiness,
) -> mpsc::Receiver<Option<String>> {
    let (tx, rx) = mpsc::channel::<Option<String>>();
    std::thread::spawn(move || {
        let mut reader = BufReader::new(stdout);
        let mut line = String::new();
        let first = match reader.read_line(&mut line) {
            Ok(0) | Err(_) => None,
            Ok(_) => Some(line.trim_end().to_string()),
        };
        let _ = tx.send(first);
        let mut rest = String::new();
        while let Ok(read) = reader.read_line(&mut rest) {
            if read == 0 {
                break;
            }
            match &after {
                AfterReadiness::Stdout => {
                    crate::diagnostic::raw_to_stdout(&rest);
                }
                AfterReadiness::PrefixedStderr(prefix) => {
                    crate::runtime_err!("{prefix}{}", rest.trim_end_matches(['\r', '\n']));
                }
            }
            rest.clear();
        }
    });
    rx
}

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

    #[test]
    fn a_child_directory_is_unique_per_delegation() {
        let parent = PathBuf::from("/tmp/parent");
        let first = child_workdir_for(&parent, "worker");
        let second = child_workdir_for(&parent, "worker");

        assert_ne!(first, second);
        assert_eq!(first.parent(), Some(parent.join(CHILDREN_DIR).as_path()));
        assert!(first
            .file_name()
            .unwrap()
            .to_string_lossy()
            .starts_with("worker-"));
        // `<name>-<16 hex>`
        assert_eq!(
            first.file_name().unwrap().to_string_lossy().len(),
            "worker-".len() + 16
        );
    }

    /// A full name's scope adds no directory level: the child of `local/worker` sits beside the
    /// child of `worker`.
    #[test]
    fn a_child_directory_is_named_by_the_bare_part_of_a_full_name() {
        let parent = PathBuf::from("/tmp/parent");
        let scoped = child_workdir_for(&parent, "local/worker");

        assert_eq!(scoped.parent(), Some(parent.join(CHILDREN_DIR).as_path()));
        let name = scoped.file_name().unwrap().to_string_lossy().into_owned();
        assert!(name.starts_with("worker-"), "{name}");
        assert_eq!(name.len(), "worker-".len() + 16);
    }

    fn request(env_allow: &[&str], spawner: Option<Spawner>) -> ChildLaunchRequest {
        ChildLaunchRequest {
            parent_records_root: WorkdirRoot::open(&std::env::temp_dir()).unwrap(),
            capsule_name: "worker".to_string(),
            capsule_version: "0.1.0".to_string(),
            grant: SpawnApproval::new("msa1.token".to_string()),
            child_env_allow: env_allow.iter().map(|name| name.to_string()).collect(),
            roost_url: "http://127.0.0.1:7700".to_string(),
            spawner,
            completion_deadline: None,
            formation_id: None,
            deadline: None,
        }
    }

    /// A handle naming some *other* delegation, for the two cases that prove a child cannot be
    /// handed this process's own.
    ///
    /// Readable rather than nonsense, because `SPAWNER_ENV` is process-wide and every other test
    /// in this binary shares it: an unreadable value here refuses an unrelated `stage_session`
    /// running beside it.
    fn decoy() -> String {
        SpawnerHandle {
            session_id: "ses_decoy".to_string(),
            context_id: "ctx_decoy".to_string(),
            delegation_id: "dlg_decoy".to_string(),
            report_to: None,
        }
        .to_env_value()
    }

    fn spawner() -> Spawner {
        Spawner {
            session_id: "ses_parent".to_string(),
            context_id: "ctx_parent".to_string(),
            report_to: Some(CompletionAddress {
                url: "http://127.0.0.1:7000".to_string(),
            }),
        }
    }

    #[test]
    fn a_childs_environment_is_built_from_its_own_declaration() {
        std::env::set_var("MURMUR_CHILD_LAUNCH_TEST_A", "a");
        std::env::set_var("MURMUR_CHILD_LAUNCH_TEST_B", "b");

        let env = child_environment(&request(&["MURMUR_CHILD_LAUNCH_TEST_A"], None), None);
        let names: Vec<&str> = env.iter().map(|(key, _)| key.as_str()).collect();

        assert!(names.contains(&"MURMUR_CHILD_LAUNCH_TEST_A"));
        assert!(!names.contains(&"MURMUR_CHILD_LAUNCH_TEST_B"));
        assert!(names.contains(&"MURMUR_ROOST_URL"));
        assert!(!env.iter().any(|(_, value)| value.contains("msa1.token")));
    }

    #[test]
    fn a_runtime_owned_name_cannot_be_displaced_by_a_declaration() {
        std::env::set_var("MURMUR_ROOST_URL", "http://127.0.0.1:1");

        let env = child_environment(&request(&["MURMUR_ROOST_URL"], None), None);
        let urls: Vec<&String> = env
            .iter()
            .filter(|(key, _)| key == "MURMUR_ROOST_URL")
            .map(|(_, value)| value)
            .collect();

        assert_eq!(urls, vec!["http://127.0.0.1:7700"]);
    }

    /// The injected handle is the one the parent composed, whatever this process's own
    /// environment holds and whatever the child declared.
    #[test]
    fn the_spawner_handle_is_injected_last_and_cannot_be_displaced() {
        std::env::set_var(SPAWNER_ENV, decoy());
        let handle = SpawnerHandle::for_delegation(&spawner(), "dlg_0001".to_string());

        let env = child_environment(&request(&[SPAWNER_ENV], Some(spawner())), Some(&handle));
        let injected: Vec<&String> = env
            .iter()
            .filter(|(key, _)| key == SPAWNER_ENV)
            .map(|(_, value)| value)
            .collect();

        assert_eq!(injected.len(), 1, "{env:?}");
        assert_eq!(
            SpawnerHandle::parse(injected[0]).expect("the injected value is a handle"),
            handle
        );
        assert_eq!(
            env.last().map(|(key, _)| key.as_str()),
            Some(SPAWNER_ENV),
            "the handle is applied in the runtime-owned tail: {env:?}"
        );
        std::env::remove_var(SPAWNER_ENV);
    }

    /// A launch with no spawner injects nothing, even from a decoy the launching process holds.
    #[test]
    fn a_launch_without_a_spawner_injects_no_handle() {
        std::env::set_var(SPAWNER_ENV, decoy());

        let env = child_environment(&request(&[SPAWNER_ENV], None), None);

        assert!(!env.iter().any(|(key, _)| key == SPAWNER_ENV), "{env:?}");
        std::env::remove_var(SPAWNER_ENV);
    }

    /// A child's formation is the one the parent's request names, applied after the spawner
    /// handle, whatever this process's own environment holds and whatever the child declared.
    #[test]
    fn the_formation_id_is_injected_last_and_cannot_be_displaced() {
        let _guard = crate::formation::FORMATION_ENV_LOCK
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let decoy = FormationId::mint();
        std::env::set_var(FORMATION_ID_ENV, decoy.as_str());
        let injected = |env: &[(String, String)]| -> Vec<String> {
            env.iter()
                .filter(|(key, _)| key == FORMATION_ID_ENV)
                .map(|(_, value)| value.clone())
                .collect()
        };

        let outside = child_environment(&request(&[FORMATION_ID_ENV], None), None);
        assert!(injected(&outside).is_empty(), "{outside:?}");

        let other = FormationId::mint();
        let handle = SpawnerHandle::for_delegation(&spawner(), "dlg_0001".to_string());
        for (spawner, handle) in [(Some(spawner()), Some(&handle)), (None, None)] {
            let mut member = request(&[FORMATION_ID_ENV], spawner);
            member.formation_id = Some(other.clone());
            let env = child_environment(&member, handle);
            assert_eq!(injected(&env), vec![other.as_str().to_string()], "{env:?}");
            assert_eq!(
                env.last(),
                Some(&(FORMATION_ID_ENV.to_string(), other.as_str().to_string())),
                "the formation id is the last runtime-owned name: {env:?}"
            );
        }
        std::env::remove_var(FORMATION_ID_ENV);
    }

    /// Every child is told its launcher already printed the host-level warnings, once, whatever
    /// this process's own environment holds and whatever the child declared.
    #[test]
    fn every_child_is_told_host_warnings_were_reported() {
        std::env::set_var(HOST_WARNINGS_REPORTED_ENV, "0");
        let handle = SpawnerHandle::for_delegation(&spawner(), "dlg_0001".to_string());
        for (spawner, handle) in [(Some(spawner()), Some(&handle)), (None, None)] {
            for allow in [&[][..], &[HOST_WARNINGS_REPORTED_ENV][..]] {
                let env = child_environment(&request(allow, spawner.clone()), handle);
                let values: Vec<&str> = env
                    .iter()
                    .filter(|(key, _)| key == HOST_WARNINGS_REPORTED_ENV)
                    .map(|(_, value)| value.as_str())
                    .collect();
                assert_eq!(values, vec!["1"], "{env:?}");
            }
        }
        std::env::remove_var(HOST_WARNINGS_REPORTED_ENV);
    }

    fn values_of<'a>(env: &'a [(String, String)], name: &str) -> Vec<&'a str> {
        env.iter()
            .filter(|(key, _)| key == name)
            .map(|(_, value)| value.as_str())
            .collect()
    }

    /// A child runs as the account its parent runs as: one `USER` and one `LOGNAME`, the parent's,
    /// whether or not the child allowlists either name.
    #[test]
    fn a_child_carries_the_parents_account_names() {
        let _guard = crate::shell::ACCOUNT_ENV_LOCK
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        std::env::set_var("USER", "murmur-parent-user");
        std::env::set_var("LOGNAME", "murmur-parent-login");
        for allow in [&[][..], &["USER"][..], &["USER", "LOGNAME"][..]] {
            let env = child_environment(&request(allow, None), None);
            assert_eq!(
                values_of(&env, "USER"),
                vec!["murmur-parent-user"],
                "{env:?}"
            );
            assert_eq!(
                values_of(&env, "LOGNAME"),
                vec!["murmur-parent-login"],
                "{env:?}"
            );
        }
    }

    /// A parent that holds neither account name hands neither on, and none is made up.
    #[test]
    fn a_child_of_a_parent_without_account_names_has_none() {
        let _guard = crate::shell::ACCOUNT_ENV_LOCK
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        let saved: Vec<_> = crate::shell::ACCOUNT_ENV
            .iter()
            .map(|name| (*name, std::env::var_os(name)))
            .collect();
        for name in crate::shell::ACCOUNT_ENV {
            std::env::remove_var(name);
        }
        let env = child_environment(&request(&["USER", "LOGNAME"], None), None);
        for (name, value) in saved {
            if let Some(value) = value {
                std::env::set_var(name, value);
            }
        }
        assert!(values_of(&env, "USER").is_empty(), "{env:?}");
        assert!(values_of(&env, "LOGNAME").is_empty(), "{env:?}");
    }

    /// A child's exit closes its stdout and its stderr together, so the reader that reports the
    /// ending can reach the tail before the drain has appended to it. The post-mortem read is the
    /// one that must not: an empty tail is a refusal that names no reason.
    #[test]
    fn a_tail_read_after_the_end_waits_for_the_drain_to_catch_up() {
        let tail = Arc::new(StderrTail {
            state: Mutex::new(StderrState::default()),
            drained: Condvar::new(),
        });

        let writer = Arc::clone(&tail);
        std::thread::spawn(move || {
            std::thread::sleep(Duration::from_millis(200));
            lock(&writer.state)
                .lines
                .push("error[E-CAP-003]: not achievable on this host".to_string());
            writer.finish();
        });

        assert!(tail.lines().is_empty());
        assert_eq!(
            tail.lines_at_end(),
            vec!["error[E-CAP-003]: not achievable on this host".to_string()]
        );
    }

    /// A child that exposed no stderr pipe has nothing to wait for, so the bound is never spent on
    /// lines that cannot arrive.
    #[test]
    fn a_tail_with_no_pipe_behind_it_is_at_its_end_already() {
        let tail = StderrTail {
            state: Mutex::new(StderrState::default()),
            drained: Condvar::new(),
        };
        tail.finish();

        let started = Instant::now();
        assert!(tail.lines_at_end().is_empty());
        assert!(
            started.elapsed() < CHILD_STDERR_DRAIN_TIMEOUT,
            "{started:?}"
        );
    }

    #[test]
    fn child_launch_reads_the_operator_token_from_the_readiness_line() {
        let report = serde_json::json!({
            "url": "localhost:1",
            "session_id": "ses_x",
            "tokens": {"operator": "mdt1.a.b", "watcher": "mdt1.c.d"},
        });
        assert_eq!(
            readiness_door_token(&report).map(|token| token.expose().to_string()),
            Some("mdt1.a.b".to_string())
        );
        let public = serde_json::json!({"url": "localhost:1", "session_id": "ses_x"});
        assert_eq!(readiness_door_token(&public), None);
    }

    #[test]
    fn child_launch_debug_prints_no_door_token() {
        let child = LaunchedChild {
            workdir: PathBuf::from("/tmp/child"),
            workdir_root: WorkdirRoot::open(&std::env::temp_dir()).unwrap(),
            session_id: "ses_child".to_string(),
            records_dir: PathBuf::new(),
            records_root: WorkdirRoot::open(&std::env::temp_dir()).unwrap(),
            capsule_url: "http://localhost:1".to_string(),
            argv: Vec::new(),
            env: Vec::new(),
            delegation_id: None,
            formation_id: None,
            task_id: None,
            door_token: Some(crate::door_auth::DoorToken::new(
                "mdt1.secretpayload.secretmac".to_string(),
            )),
            process: Arc::new(Mutex::new(ChildProcess {
                child: None,
                deliberate: false,
                status: None,
                terminated_at: None,
                #[cfg(unix)]
                lifeline: None,
            })),
            stderr_tail: Arc::new(StderrTail {
                state: Mutex::new(StderrState::default()),
                drained: Condvar::new(),
            }),
            started: Instant::now(),
            watcher: None,
        };
        let debug = format!("{child:?}");
        assert!(!debug.contains("secretpayload"), "{debug}");
        assert!(debug.contains("door_token"), "{debug}");
    }

    /// The code of `start_process`, with its comments taken out.
    fn start_process_code() -> String {
        let source = include_str!("child_launch.rs");
        let start = source
            .find("pub(crate) fn start_process(")
            .expect("start_process is defined here");
        let end = start
            + source[start..]
                .find("\n}\n")
                .expect("start_process ends at a closing brace");
        source[start..end]
            .lines()
            .filter(|line| !line.trim_start().starts_with("//"))
            .collect::<Vec<_>>()
            .join("\n")
    }

    /// The one spawn site for delegated children and formation members registers no fd hygiene:
    /// `mark_inherited_fds_cloexec` is a `close_range(CLOSE_RANGE_CLOEXEC)`, which fails the spawn
    /// outright on a kernel below 5.11. The lifeline's two-`fcntl` `pre_exec` is the last one
    /// registered.
    #[test]
    fn start_process_registers_no_fd_hygiene() {
        let code = start_process_code();
        for name in [
            "prepare_enforcement",
            "mark_inherited_fds_cloexec",
            "close_range",
        ] {
            assert!(!code.contains(name), "start_process names {name}:\n{code}");
        }
        let handed = code
            .rfind("lifeline.hand_to")
            .expect("the lifeline is handed");
        let kept = code
            .find("keep_across_exec")
            .expect("an inherited descriptor is kept");
        assert!(kept < handed, "the lifeline is registered last:\n{code}");
        assert!(
            !code[handed..].contains("pre_exec"),
            "nothing is registered after the lifeline:\n{code}"
        );
    }

    #[cfg(unix)]
    fn process_with_lifeline(program: &str, args: &[&str]) -> (ChildProcess, i32) {
        let lifeline = crate::lifeline::ChildLifeline::new().unwrap();
        let write = lifeline.write_fd().unwrap();
        let child = Command::new(program).args(args).spawn().unwrap();
        (
            ChildProcess {
                child: Some(child),
                deliberate: false,
                status: None,
                terminated_at: None,
                lifeline: Some(lifeline),
            },
            write,
        )
    }

    /// Ending a child deliberately closes the write end of its lifeline after the reap.
    #[cfg(unix)]
    #[test]
    fn ending_a_child_closes_its_lifeline() {
        let (process, _) = process_with_lifeline("sleep", &["30"]);
        let process = Mutex::new(process);
        end_process(&process).unwrap();
        assert!(lock(&process).lifeline.is_none());
        assert!(lock(&process).status.is_some());
        end_process(&process).unwrap();
    }

    /// A child ended on purpose is sent `SIGTERM` first and given the chance to wind down: a
    /// child that exits on the signal is reaped with its own exit status, well inside the grace.
    #[cfg(unix)]
    #[test]
    fn ending_a_child_sends_sigterm_and_waits_for_it_to_wind_down() {
        let dir = tempfile::tempdir().unwrap();
        let marker = dir.path().join("wound-down");
        let script = format!(
            "trap 'echo wound-down > {}; exit 7' TERM; sleep 30 & wait",
            marker.display()
        );
        let (process, _) = process_with_lifeline("bash", &["-c", &script]);
        let process = Mutex::new(process);
        // Long enough for bash to install its trap before the signal arrives.
        std::thread::sleep(Duration::from_millis(300));
        let began = Instant::now();
        end_process(&process).unwrap();
        assert!(
            began.elapsed() < CHILD_END_GRACE,
            "took {:?}",
            began.elapsed()
        );
        assert_eq!(
            std::fs::read_to_string(&marker).unwrap().trim(),
            "wound-down"
        );
        assert_eq!(
            lock(&process).status.and_then(|status| status.code()),
            Some(7)
        );
        assert!(lock(&process).lifeline.is_none());
    }

    /// A child that ignores `SIGTERM` is killed once [`CHILD_END_GRACE`] has passed, and no later.
    #[cfg(unix)]
    #[test]
    fn a_child_that_ignores_sigterm_is_killed_at_the_grace() {
        use std::os::unix::process::ExitStatusExt as _;
        let (process, _) =
            process_with_lifeline("bash", &["-c", "trap '' TERM; while :; do sleep 0.1; done"]);
        let process = Mutex::new(process);
        std::thread::sleep(Duration::from_millis(300));
        let began = Instant::now();
        end_process(&process).unwrap();
        let took = began.elapsed();
        assert!(took >= CHILD_END_GRACE, "killed before the grace: {took:?}");
        assert!(
            took < CHILD_END_GRACE + Duration::from_secs(5),
            "took {took:?}"
        );
        assert_eq!(
            lock(&process).status.and_then(|status| status.signal()),
            Some(libc::SIGKILL)
        );
    }

    /// Children signalled together with [`LaunchedChild::begin_ending`] share one grace: ending
    /// two that both ignore `SIGTERM` takes one [`CHILD_END_GRACE`], not two.
    #[cfg(unix)]
    #[test]
    fn children_signalled_together_wait_one_grace_between_them() {
        let wedged = || {
            let (process, _) =
                process_with_lifeline("bash", &["-c", "trap '' TERM; while :; do sleep 0.1; done"]);
            LaunchedChild {
                workdir: PathBuf::from("/tmp/child"),
                workdir_root: WorkdirRoot::open(&std::env::temp_dir()).unwrap(),
                session_id: "ses_child".to_string(),
                records_dir: PathBuf::new(),
                records_root: WorkdirRoot::open(&std::env::temp_dir()).unwrap(),
                capsule_url: String::new(),
                argv: Vec::new(),
                env: Vec::new(),
                delegation_id: None,
                formation_id: None,
                task_id: None,
                door_token: None,
                process: Arc::new(Mutex::new(process)),
                stderr_tail: Arc::new(StderrTail {
                    state: Mutex::new(StderrState::default()),
                    drained: Condvar::new(),
                }),
                started: Instant::now(),
                watcher: None,
            }
        };
        let children = vec![wedged(), wedged()];
        std::thread::sleep(Duration::from_millis(300));
        let began = Instant::now();
        for child in &children {
            child.begin_ending();
        }
        drop(children);
        let took = began.elapsed();
        assert!(took >= CHILD_END_GRACE, "took {took:?}");
        assert!(
            took < CHILD_END_GRACE * 2,
            "two children waited a grace each: {took:?}"
        );
    }

    /// A child observed to have exited on its own has its lifeline closed by the observer.
    #[cfg(unix)]
    #[test]
    fn a_child_seen_exited_has_its_lifeline_closed() {
        let (mut process, _) = process_with_lifeline("true", &[]);
        let deadline = Instant::now() + Duration::from_secs(10);
        let ending = loop {
            if let Some(ending) = process.poll() {
                break ending;
            }
            assert!(Instant::now() < deadline, "`true` never exited");
            std::thread::sleep(Duration::from_millis(20));
        };
        assert!(matches!(ending, Ending::Exited(Some(_))));
        assert!(process.lifeline.is_none());
    }

    /// A delegated child lives exactly as long as its handle or its own run. Dropping the handle
    /// of a running child kills it and closes the write end of its lifeline; a child that exits on
    /// its own is seen exited by [`LaunchedChild::has_exited`], which closes the write end too,
    /// and its handle's drop then only reaps it.
    #[cfg(unix)]
    #[test]
    #[allow(unsafe_code)]
    fn a_childs_lifeline_closes_when_its_handle_drops_or_its_end_is_seen() {
        let launched = |program: &str, args: &[&str]| {
            let (process, write) = process_with_lifeline(program, args);
            let process = Arc::new(Mutex::new(process));
            let child = LaunchedChild {
                workdir: PathBuf::from("/tmp/child"),
                workdir_root: WorkdirRoot::open(&std::env::temp_dir()).unwrap(),
                session_id: "ses_child".to_string(),
                records_dir: PathBuf::new(),
                records_root: WorkdirRoot::open(&std::env::temp_dir()).unwrap(),
                capsule_url: String::new(),
                argv: Vec::new(),
                env: Vec::new(),
                delegation_id: None,
                formation_id: None,
                task_id: None,
                door_token: None,
                process: Arc::clone(&process),
                stderr_tail: Arc::new(StderrTail {
                    state: Mutex::new(StderrState::default()),
                    drained: Condvar::new(),
                }),
                started: Instant::now(),
                watcher: None,
            };
            (child, process, write)
        };
        let is_open = |fd: i32| {
            // SAFETY: `F_GETFD` takes and returns integers.
            unsafe { libc::fcntl(fd, libc::F_GETFD) >= 0 }
        };

        // Held: the child runs and the write end stays open for as long as the handle does.
        let (child, process, write) = launched("sleep", &["30"]);
        assert!(!child.has_exited());
        assert!(is_open(write), "a held child's write end is open");
        drop(child);
        assert!(
            lock(&process).lifeline.is_none(),
            "dropping the handle closes it"
        );
        assert!(
            lock(&process).deliberate,
            "a running child's drop is deliberate"
        );
        // A status is set only by a `wait` that returned, so the child is reaped and gone.
        assert!(lock(&process).status.is_some(), "and reaps the child");

        // Ran to its end: seen exited, the write end closes without the handle being dropped.
        let (child, process, _) = launched("true", &[]);
        let deadline = Instant::now() + Duration::from_secs(10);
        while !child.has_exited() {
            assert!(Instant::now() < deadline, "`true` never exited");
            std::thread::sleep(Duration::from_millis(20));
        }
        assert!(
            lock(&process).lifeline.is_none(),
            "seeing the end closes it"
        );
        drop(child);
        assert!(
            !lock(&process).deliberate,
            "a child that ended on its own is not recorded as ended by its parent"
        );
    }

    /// A child its parent ends on purpose is recorded `terminated` by the launcher, in the words
    /// every such record uses, and posted to nobody: the address here answers nothing.
    #[cfg(unix)]
    #[test]
    fn a_child_its_parent_ends_is_recorded_in_the_shared_words() {
        let dir = tempfile::tempdir().unwrap();
        let (process, _) = process_with_lifeline("sleep", &["30"]);
        let mut child = LaunchedChild {
            workdir: dir.path().to_path_buf(),
            workdir_root: WorkdirRoot::open(dir.path()).unwrap(),
            session_id: "ses_child".to_string(),
            records_dir: dir.path().to_path_buf(),
            records_root: WorkdirRoot::open(dir.path()).unwrap(),
            capsule_url: String::new(),
            argv: Vec::new(),
            env: Vec::new(),
            delegation_id: Some("dlg_watched".to_string()),
            formation_id: None,
            task_id: None,
            door_token: None,
            process: Arc::new(Mutex::new(process)),
            stderr_tail: Arc::new(StderrTail {
                state: Mutex::new(StderrState::default()),
                drained: Condvar::new(),
            }),
            started: Instant::now(),
            watcher: None,
        };
        let watcher = watch_for_completion(
            &child,
            SpawnerHandle::for_delegation(&spawner(), "dlg_watched".to_string()),
            CompletionAddress {
                url: "http://127.0.0.1:1".to_string(),
            },
            "worker",
            "0.1.0",
            None,
        )
        .expect("the watcher starts");

        child.shutdown().unwrap();
        watcher.join().unwrap();

        let recorded = delegation::read_completion(&WorkdirRoot::open(dir.path()).unwrap())
            .expect("the ending is recorded");
        assert_eq!(recorded.status, DelegationStatus::Terminated);
        assert_eq!(recorded.reported_by, Reporter::Launcher);
        assert!(!recorded.delivered);
        assert_eq!(
            recorded.detail.as_deref(),
            Some(delegation::PARENT_ENDED_DETAIL)
        );
    }

    /// A child that records its own completion while it winds down from `SIGTERM` does not
    /// decide the outcome of a delegation its parent ended: the launcher's `terminated` record is
    /// written over it, and the child's is never re-posted.
    #[cfg(unix)]
    #[test]
    fn a_completion_the_child_records_while_being_ended_is_overwritten_by_the_launcher() {
        let dir = tempfile::tempdir().unwrap();
        let own = DelegationOutcome {
            delegation_id: "dlg_watched".to_string(),
            capsule_name: "worker".to_string(),
            capsule_version: "0.1.0".to_string(),
            session_id: "ses_child".to_string(),
            status: DelegationStatus::Error,
            result_path: None,
            workdir: dir.path().display().to_string(),
            duration_ms: 1,
            detail: None,
            reported_by: Reporter::Child,
            delivered: false,
            delivery_error: None,
        };
        let staged = dir.path().join("own-record.json");
        std::fs::write(&staged, serde_json::to_vec(&own).unwrap()).unwrap();
        let script = format!(
            "trap 'cp {} {}; exit 0' TERM; sleep 30 & wait",
            staged.display(),
            dir.path().join(delegation::COMPLETION_FILE).display()
        );
        let (process, _) = process_with_lifeline("bash", &["-c", &script]);
        let mut child = LaunchedChild {
            workdir: dir.path().to_path_buf(),
            workdir_root: WorkdirRoot::open(dir.path()).unwrap(),
            session_id: "ses_child".to_string(),
            records_dir: dir.path().to_path_buf(),
            records_root: WorkdirRoot::open(dir.path()).unwrap(),
            capsule_url: String::new(),
            argv: Vec::new(),
            env: Vec::new(),
            delegation_id: Some("dlg_watched".to_string()),
            formation_id: None,
            task_id: None,
            door_token: None,
            process: Arc::new(Mutex::new(process)),
            stderr_tail: Arc::new(StderrTail {
                state: Mutex::new(StderrState::default()),
                drained: Condvar::new(),
            }),
            started: Instant::now(),
            watcher: None,
        };
        let watcher = watch_for_completion(
            &child,
            SpawnerHandle::for_delegation(&spawner(), "dlg_watched".to_string()),
            CompletionAddress {
                url: "http://127.0.0.1:1".to_string(),
            },
            "worker",
            "0.1.0",
            None,
        )
        .expect("the watcher starts");
        std::thread::sleep(Duration::from_millis(300));

        child.shutdown().unwrap();
        watcher.join().unwrap();
        let recorded = delegation::read_completion(&WorkdirRoot::open(dir.path()).unwrap())
            .expect("the ending is recorded");
        assert_eq!(recorded.status, DelegationStatus::Terminated);
        assert_eq!(recorded.reported_by, Reporter::Launcher);
        assert!(!recorded.delivered);
        assert_eq!(recorded.delivery_error, None);
    }
}