shep-daemon 0.1.3

The supervision engine behind shep: process spawning, restart policy, the log plane, watch and cron restarts, and the RPC server
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
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
//! Daemon boot: layout, pidfile, control-socket bind, and the run/teardown
//! sequence
//!
//! Everything a daemon needs before it can accept its first connection:
//! creating (and tightening) `$SHEP_HOME`'s directory layout, recording its
//! own pid, binding the control socket — including recovering from a socket
//! file a crashed daemon left behind — and, once bound, reporting readiness
//! on an inherited pipe if the CLI daemonized us (spec §3). This module owns
//! the 0700 guarantee `crate::server::RpcServer`'s doc names as the boot
//! path's responsibility: `init_dirs` creates `run/` (and every other
//! layout directory) `0700` and *tightens* it back down if it already
//! exists looser, so the guarantee holds whether this is a first boot or a
//! restart onto a directory some other process touched.
//!
//! [`boot`] assembles every piece earlier tasks built (bus, supervisor,
//! muster roll, RPC context) into one [`RunningDaemon`]; [`RunningDaemon::run`]
//! serves connections until a signal or `KillDaemon` arrives, then tears
//! down in a load-bearing order — see its own doc for why.
//!
//! **No unsafe in this module.** [`BootOptions::ready_fd`] is
//! `Option<`[`std::fs::File`]`>`, not a raw descriptor: the CALLER adopts
//! the inherited readiness pipe (`unsafe fn` [`crate::sys::adopt_fd`],
//! IR-22's sole unsafe surface) before ever constructing a [`BootOptions`],
//! so [`boot`] only ever receives an already-owned handle and never
//! constructs one from a bare number itself. Every bind/probe/unlink/
//! signal-registration step in this module is plain safe std/tokio.
//!
//! (An earlier revision had `boot` perform that adoption inline, behind an
//! `unsafe` block at its own call site — see git history around commits
//! db02d9f/5c4f29b for that design, and f688ac2/9455d80 for the doc fallout
//! it caused. It moved here because `adopt_fd`'s ordering precondition is
//! process-wide — "call before THIS PROCESS opens any descriptor" — and
//! `boot` cannot discharge that on its own caller's behalf: `boot` is
//! `async`, so a tokio runtime with its own live poller fds already exists
//! by the time `boot` is ever called. The fix pushes adoption out to
//! somewhere that CAN discharge the precondition: the CLI's `main`, as its
//! literal first fd-touching statement, before a tokio runtime — or
//! anything else — exists (Phase 3). See [`crate::sys::adopt_fd`]'s own
//! `# Safety` section and rationale essay for the full contract.)

use core::fmt;
use core::time::Duration;
use std::ffi::OsString;
use std::io::ErrorKind;
use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt, PermissionsExt};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use serde::{Deserialize, Serialize};
use tokio::net::UnixListener;
use tokio::signal::unix::{SignalKind, signal};
use tokio::sync::{mpsc, oneshot, watch};
use tokio::task::JoinHandle;

use shep_core::paths::ShepPaths;
use shep_core::protocol::BusEvent;
use shep_core::selector::ProcessSelector;

use crate::bus::new_bus;
use crate::cron::DEFAULT_MAX_CRON_SLEEP;
use crate::dogs::{DogSpec, spawn_dog_watch};
use crate::extras::{Extras, ExtrasReports, spawn_extras_reporter};
use crate::rpc::RpcContext;
use crate::runner::ProcessRunner;
use crate::server::RpcServer;
use crate::snapshot::{self, FlockRegistry, SnapshotError, SnapshotWriter, spawn_snapshot_writer};
use crate::supervisor::{SupervisorBuilder, SupervisorError, SupervisorHandle};

/// Mode for every directory shep creates (spec §10: no other user, at all)
pub const DIR_MODE: u32 = 0o700;

/// Capacity of each of the two lifecycle-extra report channels.
///
/// Bounded rather than unbounded on purpose: a report producer that outruns
/// the reporting task should back-pressure (the enforcer's own send is
/// awaited) rather than grow a queue of restarts nobody has performed yet.
/// Generous enough that a whole flock breaching on one sampling pass fits.
const EXTRAS_REPORT_CAPACITY: usize = 64;

/// Creates `dir` (and any missing parents) at [`DIR_MODE`] directly, via
/// [`DirBuilderExt::mode`] rather than the std default (`0o777`, narrowed
/// only by whatever the process umask happens to strip).
///
/// This is the fix for a real TOCTOU: `create_dir_all` followed by a
/// separate `chmod` leaves a window where a *freshly created* directory
/// sits at its umask-derived mode — empirically `0o755` under the common
/// `umask 022`, and world-*writable* under `umask 0` (a misconfigured
/// systemd unit), which opens a pre-bind symlink race on the socket path
/// underneath it. Requesting `0o700` at creation has no group/other bits
/// for any ordinary umask to strip, so there is nothing left to narrow
/// after the fact — the directory is never wider than `DIR_MODE`, not even
/// for the instant between `mkdir` and a later `chmod`.
///
/// Does not touch a directory that already exists (the umask given to
/// `mkdir` only governs directories this call actually creates) — that
/// case is [`init_dirs`]'s `set_permissions` pass, not this function's job.
fn create_dir_at_dir_mode(dir: &Path) -> std::io::Result<()> {
    std::fs::DirBuilder::new()
        .mode(DIR_MODE)
        .recursive(true)
        .create(dir)
}

/// Creates `$SHEP_HOME` and its subdirectories, tightening loose modes
///
/// Runs on every boot, not just the first: a restart onto a layout that
/// already exists still forces every directory back to `DIR_MODE`, so a
/// looser mode left by an external touch (or an older shep version) never
/// survives a restart. A directory this call actually creates lands at
/// `DIR_MODE` immediately (via the private `create_dir_at_dir_mode`
/// helper); the `set_permissions` call below is what tightens one that was
/// already there.
///
/// # Errors
/// - [`BootError::Io`] — a directory could not be created or chmod'ed.
pub(crate) fn init_dirs(paths: &ShepPaths) -> Result<(), BootError> {
    for dir in [&paths.home, &paths.logs, &paths.pids, &paths.run] {
        create_dir_at_dir_mode(dir).map_err(|source| BootError::Io {
            path: dir.clone(),
            source,
        })?;
        std::fs::set_permissions(dir, std::fs::Permissions::from_mode(DIR_MODE)).map_err(
            |source| BootError::Io {
                path: dir.clone(),
                source,
            },
        )?;
    }
    Ok(())
}

/// The daemon's own pidfile: `$SHEP_HOME/pids/shepd.pid`
///
/// Public only for `tests/daemon_e2e.rs`, which reads the file back to check
/// what a booted daemon wrote there. `shep-cli` derives the same path from
/// `ShepPaths` itself rather than calling this.
#[must_use]
pub fn pidfile(paths: &ShepPaths) -> PathBuf {
    paths.pids.join("shepd.pid")
}

/// Writes the pidfile atomically (temp file in `pids/`, `fsync`, then
/// `rename`), matching [`crate::snapshot::write_atomic`]'s convention for
/// every other file this daemon writes. [`crate::snapshot::write_atomic`]
/// itself isn't reusable here — it's typed to serialize a
/// [`crate::snapshot::FlockSnapshot`] as JSON, not a bare pid — so this
/// inlines the same temp+rename shape rather than writing straight to the
/// final path with `std::fs::write` and risking a reader observing a
/// truncated file mid-write.
///
/// [`boot`] itself does NOT call this: it records its own pid through the
/// crate-private `PidfileLock::record` instead, writing in place into the
/// SAME open, locked file descriptor it already holds — a rename here
/// would swap in an unlocked inode and undo that lock's whole point (see
/// `PidfileLock`'s own doc, next to its definition in this file).
///
/// Test-only, and deliberately so. Its only remaining use is seeding a
/// fixture pidfile without contending for the boot-time lock. Exported, it
/// would be a footgun: a rename over the locked path by any outside caller
/// silently disarms `PidfileLock` for the rest of that daemon's life. The
/// Phase 3 CLI has no need for it either — it learns the daemon's pid from
/// the handshake's `HelloAck`, not from this file.
///
/// # Errors
/// - [`BootError::Io`] — the pidfile could not be written.
#[cfg(test)]
fn write_pidfile(paths: &ShepPaths, pid: u32) -> Result<(), BootError> {
    use std::io::Write;

    use tempfile::NamedTempFile;

    let path = pidfile(paths);
    let mut tmp = NamedTempFile::new_in(&paths.pids).map_err(|source| BootError::Io {
        path: paths.pids.clone(),
        source,
    })?;
    tmp.write_all(pid.to_string().as_bytes())
        .map_err(|source| BootError::Io {
            path: path.clone(),
            source,
        })?;
    tmp.as_file().sync_all().map_err(|source| BootError::Io {
        path: path.clone(),
        source,
    })?;
    tmp.persist(&path).map_err(|err| BootError::Io {
        path,
        source: err.error,
    })?;
    Ok(())
}

/// Reads the recorded daemon pid, if any
///
/// A missing pidfile reads as `None`, as does one whose contents are not a
/// valid pid (the daemon's own writes never produce that, so it only
/// happens to a file something else corrupted) — the pid is a best-effort
/// hint attached to [`BootError::AlreadyRunning`], not itself the source of
/// truth for whether a daemon is live.
///
/// # Errors
/// - [`BootError::Io`] — the pidfile exists but could not be read.
pub(crate) fn read_pidfile(paths: &ShepPaths) -> Result<Option<u32>, BootError> {
    let path = pidfile(paths);
    match std::fs::read_to_string(&path) {
        Ok(contents) => Ok(contents.trim().parse::<u32>().ok()),
        Err(err) if err.kind() == ErrorKind::NotFound => Ok(None),
        Err(source) => Err(BootError::Io { path, source }),
    }
}

/// This daemon's exclusive claim on `$SHEP_HOME`: an `flock(2)` held on the
/// pidfile for as long as this process is alive.
///
/// Exists to close a real race in [`bind_socket`]'s stale-socket recovery
/// (spec §6): two daemons racing to boot the same `$SHEP_HOME` can both hit
/// `EADDRINUSE` on a crashed predecessor's leftover socket file, both probe
/// it, both observe `ConnectionRefused` (correctly — the file IS stale),
/// and both proceed into the `remove_file` + rebind arm — B's `remove_file`
/// can then delete A's freshly-bound listener out from under it, and BOTH
/// end up with a live [`UnixListener`] on the same path, each unaware of
/// the other. That is two live daemons on one `$SHEP_HOME`, exactly the
/// case [`BootError::AlreadyRunning`] exists to prevent, defeated exactly
/// when the recovery path is what makes it matter.
///
/// `flock` closes it because the kernel serializes concurrent lockers
/// itself: [`Self::acquire`] uses `LOCK_EX | LOCK_NB`, so of any number of
/// processes racing to boot the same `$SHEP_HOME`, at most one ever holds
/// this lock at a time, and every other one fails IMMEDIATELY with
/// [`BootError::AlreadyRunning`] rather than proceeding into
/// [`bind_socket`]'s probe/recover logic at all — there is no window left
/// for two processes to be inside that logic concurrently, because only
/// the lock's single holder can be in there. [`boot`] acquires this BEFORE
/// calling [`bind_socket`] and keeps it for [`RunningDaemon`]'s whole
/// lifetime (dropped only at the end of [`RunningDaemon::run`]) — see this
/// type's own `acquire`/`record` docs for exactly what that does and does
/// not still leave [`bind_socket`]'s own probe responsible for.
///
/// A crashed daemon's lock needs no separate cleanup: `flock`'s locks are
/// owned by the OPEN FILE DESCRIPTION, which the kernel releases the
/// instant every fd referencing it closes — including on process death by
/// any signal, `SIGKILL` included, with no unlock call required. That is
/// exactly the property a pidfile-based "am I the only one" check needs
/// and a filesystem-existence check (`pidfile` alone) cannot give: a stale
/// pidfile from a crash still exists, but a stale LOCK on it does not.
#[derive(Debug)]
struct PidfileLock(nix::fcntl::Flock<std::fs::File>);

impl PidfileLock {
    /// Opens (creating if necessary) and takes an exclusive, non-blocking
    /// `flock` on `paths`'s pidfile.
    ///
    /// Deliberately does NOT truncate on open: a losing caller's
    /// [`BootError::AlreadyRunning`] still wants to read whatever pid a
    /// previous winner recorded (via [`Self::record`]) for its error's
    /// hint, and this call cannot yet know which one it will be.
    ///
    /// # Errors
    /// - [`BootError::AlreadyRunning`] — another process already holds this
    ///   lock (carries the pid recorded in the file, if any — the file
    ///   itself is left untouched either way).
    /// - [`BootError::Io`] — the pidfile could not be opened.
    fn acquire(paths: &ShepPaths) -> Result<Self, BootError> {
        let path = pidfile(paths);
        let file = std::fs::OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(false) // preserve any pid a previous winner recorded — see this fn's own doc
            .mode(0o600)
            .open(&path)
            .map_err(|source| BootError::Io {
                path: path.clone(),
                source,
            })?;
        match nix::fcntl::Flock::lock(file, nix::fcntl::FlockArg::LockExclusiveNonblock) {
            Ok(lock) => Ok(Self(lock)),
            Err((_file, nix::errno::Errno::EWOULDBLOCK)) => Err(BootError::AlreadyRunning {
                pid: read_pidfile(paths)?,
            }),
            Err((_file, errno)) => Err(BootError::Io {
                path,
                source: errno.into(),
            }),
        }
    }

    /// Overwrites the locked pidfile's content with `pid`, in place —
    /// truncate then write at offset 0, never a temp-file-plus-`rename`
    /// (contrast `write_pidfile`, this module's test-only helper): renaming
    /// a fresh inode over this path
    /// would swap in a file nothing has locked, silently ending this
    /// type's whole reason to exist for as long as the daemon keeps
    /// running afterward. A `flock` lock lives on the OPEN FILE
    /// DESCRIPTION, not the path, so it does not follow a rename.
    ///
    /// # Errors
    /// - [`BootError::Io`] — the write failed.
    fn record(&mut self, paths: &ShepPaths, pid: u32) -> Result<(), BootError> {
        use std::io::{Seek, SeekFrom, Write};

        let path = pidfile(paths);
        let file = &mut *self.0;
        file.set_len(0).map_err(|source| BootError::Io {
            path: path.clone(),
            source,
        })?;
        file.seek(SeekFrom::Start(0))
            .map_err(|source| BootError::Io {
                path: path.clone(),
                source,
            })?;
        file.write_all(pid.to_string().as_bytes())
            .map_err(|source| BootError::Io {
                path: path.clone(),
                source,
            })?;
        file.sync_all()
            .map_err(|source| BootError::Io { path, source })
    }
}

/// The socket this daemon binds: the layout default, or a config override
#[must_use]
pub(crate) fn socket_path(paths: &ShepPaths, override_path: Option<&Path>) -> PathBuf {
    match override_path {
        Some(path) => path.to_path_buf(),
        None => paths.socket.clone(),
    }
}

/// Warns (does not refuse) when `socket`'s directory is reachable by anyone
/// but its owner. The default layout's `run/` is always `0700` by the time
/// [`bind_socket`] runs (via [`init_dirs`]); this only fires for a
/// `[daemon].socket` override the operator pointed somewhere looser, which
/// forfeits the 0700 guarantee the security model otherwise rests on. That
/// is the operator's call to make, not this function's to block.
fn warn_if_socket_dir_is_loose(socket: &Path) {
    let Some(parent) = socket.parent() else {
        return;
    };
    let Ok(metadata) = std::fs::metadata(parent) else {
        return;
    };
    if metadata.permissions().mode() & 0o022 != 0 {
        tracing::warn!(
            path = %parent.display(),
            "control-socket directory is group- or world-writable; \
             the 0700 guarantee only covers the default $SHEP_HOME/run"
        );
    }
}

/// Binds the control socket, recovering from a crashed daemon's leftovers
///
/// # Errors
/// - [`BootError::AlreadyRunning`] — a live daemon answered on the socket.
/// - [`BootError::Io`] — bind, probe, or unlink failed.
pub(crate) fn bind_socket(paths: &ShepPaths, socket: &Path) -> Result<UnixListener, BootError> {
    // Ahead of the bind, because the kernel's own refusal names neither the
    // limit nor `$SHEP_HOME`. `sun_path` is 104 bytes on macOS and 108 on
    // Linux, and it holds a NUL terminator, so the usable length is one less.
    const SUN_PATH_CAPACITY: usize = if cfg!(target_os = "linux") { 108 } else { 104 };
    let len = socket.as_os_str().as_encoded_bytes().len();
    if len >= SUN_PATH_CAPACITY {
        return Err(BootError::SocketPathTooLong {
            path: socket.to_path_buf(),
            len,
            limit: SUN_PATH_CAPACITY - 1,
        });
    }
    warn_if_socket_dir_is_loose(socket);
    match UnixListener::bind(socket) {
        Ok(listener) => Ok(listener),
        Err(err) if err.kind() == ErrorKind::AddrInUse => {
            // EADDRINUSE only says the path exists. Probe it: a live daemon's
            // listener accepts at the kernel level even mid-accept, while a
            // file left behind by a crash (or a reboot) refuses. This is the
            // load-bearing step for the reboot-resurrect scenario (§13.4).
            //
            // Only one direction of that is proof, and the asymmetry is
            // deliberate. A socket answers for as long as ANY descriptor for
            // it stays open, and `fork` copies every descriptor a process
            // holds: a child a dying daemon forked and has not yet exec'd
            // goes on answering on its behalf until close-on-exec clears the
            // copy. So a refusal proves staleness, while an answer is only
            // grounds to refuse this boot — never evidence that a healthy
            // peer is there. Refusing a boot that could have proceeded costs
            // an operator one retry; binding over a socket a daemon is still
            // serving on costs two daemons one flock.
            match std::os::unix::net::UnixStream::connect(socket) {
                Ok(_) => Err(BootError::AlreadyRunning {
                    pid: read_pidfile(paths)?,
                }),
                Err(probe)
                    if matches!(
                        probe.kind(),
                        ErrorKind::ConnectionRefused | ErrorKind::NotFound
                    ) =>
                {
                    std::fs::remove_file(socket).map_err(|source| BootError::Io {
                        path: socket.to_path_buf(),
                        source,
                    })?;
                    UnixListener::bind(socket).map_err(|source| BootError::Io {
                        path: socket.to_path_buf(),
                        source,
                    })
                }
                Err(source) => Err(BootError::Io {
                    path: socket.to_path_buf(),
                    source,
                }),
            }
        }
        Err(source) => Err(BootError::Io {
            path: socket.to_path_buf(),
            source,
        }),
    }
}

/// Environment variable naming the inherited readiness descriptor.
///
/// Set by the CLI on the child it re-execs detached (spec §3); read back and
/// adopted (`unsafe fn` [`crate::sys::adopt_fd`]) by that same CLI, not by
/// anything in this crate — shep-daemon never parses this variable or sees
/// a raw fd number itself, only the already-adopted [`std::fs::File`] that
/// lands in [`BootOptions::ready_fd`].
///
/// Public with no caller yet, for the same reason [`crate::sys::adopt_fd`]
/// is: both halves of this handshake belong to `shep-cli` and neither is
/// written. Crate-private it has no use at all, and "unused constant" is a
/// worse description of it than this paragraph.
pub const READY_FD_ENV: &str = "SHEP_READY_FD";

/// What the daemonizing parent reads off the readiness pipe.
///
/// Crate-private even though the paragraph above is not, because `write_ready`
/// does use this type — so narrowing it costs nothing today. What would
/// reopen it is the CLI-side reader deciding to deserialize into this exact
/// struct rather than its own; until that exists, the wire format below is
/// the contract, not the Rust type.
// wire format: shep-cli parses this line; changing it is a breaking change
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct DaemonReady {
    /// This daemon's OS pid.
    pub(crate) pid: u32,
    /// This daemon's crate version.
    pub(crate) version: String,
}

/// Writes one newline-terminated JSON readiness line to `pipe` and closes
/// it — dropping `pipe` at the end of this call is the parent's own EOF
/// signal that there is nothing more to read.
///
/// Takes an already-adopted [`std::fs::File`], never a raw fd: adoption
/// (`unsafe fn` [`crate::sys::adopt_fd`]) is a fd-inheritance concern
/// (`sys.rs`'s rationale essay) with a process-wide ordering precondition
/// that only the CLI's `main` can discharge — this function never touches a
/// bare descriptor, only the safe `File` handed to it.
///
/// # Errors
/// - [`BootError::ReadyWrite`] — the write failed (carries the OS error).
fn write_ready(mut pipe: std::fs::File, ready: &DaemonReady) -> Result<(), BootError> {
    use std::io::Write;

    // DaemonReady is a plain {u32, String} pair: serde_json::to_string only
    // fails on things neither field can ever be (non-string map keys, NaN
    // floats), so this can't error in practice.
    let mut line = serde_json::to_string(ready).expect("DaemonReady always serializes");
    line.push('\n');
    pipe.write_all(line.as_bytes())
        .map_err(BootError::ReadyWrite)?;
    Ok(())
}

/// Options the CLI hands the daemon at boot.
#[derive(Debug, Default)]
pub struct BootOptions {
    /// Overrides the layout's default control-socket path.
    pub socket: Option<PathBuf>,
    /// The inherited readiness pipe, if the CLI daemonized us (see
    /// [`READY_FD_ENV`]) — already adopted into an owned
    /// [`std::fs::File`] by the CALLER before this struct is ever
    /// constructed.
    ///
    /// Adoption (`unsafe fn` [`crate::sys::adopt_fd`]) is deliberately not
    /// this crate's job: its ordering precondition ("call this before the
    /// process opens any descriptor of its own") is process-wide, and
    /// [`boot`] — already running inside a tokio runtime with its own live
    /// poller fds by the time it is called — cannot discharge that on its
    /// own caller's behalf. The intended caller is the CLI's `main`, as its
    /// literal first fd-touching statement, before a tokio runtime even
    /// exists (Phase 3). Because this field already carries a safe owned
    /// handle, [`boot`] never constructs a `File` from a raw number and
    /// this crate's unsafe stays confined to `sys.rs` (IR-22).
    pub ready_fd: Option<std::fs::File>,
    /// Restore the muster roll if one exists (spec §9's `shep muster`).
    pub restore: bool,
    /// Longest a cron worker parks before re-reading the wall clock, from
    /// `[daemon] max_cron_sleep`. Unset means the default (`cron`'s
    /// crate-private `DEFAULT_MAX_CRON_SLEEP`, applied by [`boot`] and
    /// nowhere else) — the same `Option` shape [`Self::socket`] uses, so
    /// nothing between `shep.toml` and here invents a value.
    pub max_cron_sleep: Option<Duration>,
    /// Where to report readiness once the muster restore has finished, for
    /// an init system supervising this process directly. `None` — the
    /// ordinary case — reports nothing.
    ///
    /// The resolved address rather than a bool, and not read from the
    /// environment inside this crate: `std::env::set_var` is `unsafe` in
    /// edition 2024 and this crate is `#![deny(unsafe_code)]`, so a boot
    /// test could not establish an ambient `$NOTIFY_SOCKET` to observe the
    /// ordering against. The CLI reads the variable
    /// ([`crate::notify::NOTIFY_SOCKET_ENV`]) once, where it already reads
    /// every other `SHEP_*` override.
    ///
    /// Distinct from [`Self::ready_fd`], which the two share nothing with
    /// but a name: that one answers a *parent shep process* that daemonized
    /// this one and is waiting to exit, and it is written the moment the
    /// socket binds so a slow muster cannot make that parent think the boot
    /// failed. This one answers an init system that is supervising this
    /// process itself, and is written last, so the unit goes green only
    /// once the flock is actually back. Both may be set, but in practice
    /// never are: whichever one is supervising, the other is not.
    pub notify_socket: Option<OsString>,
    /// Dogs to start once the flock is back, in the order given.
    ///
    /// Assembled by the caller from `[daemon] enabled_dogs` and
    /// `[daemon] adopted_dogs`, so shep-daemon never reads `shep.toml`
    /// itself — the same division [`Self::socket`] and
    /// [`Self::max_cron_sleep`] already follow.
    pub dogs: Vec<DogSpec>,
    /// Wipe the in-memory flock registry before [`RunningDaemon::run`]'s
    /// teardown writes the final muster roll, so that roll always describes
    /// an empty flock — regardless of whether the session ended through an
    /// explicit `Stop`/`Delete`/`KillDaemon` sequence or by a signal caught
    /// inside `run` itself, which no caller-level request can precede.
    ///
    /// `false` for every real `runtime`/`daemon` boot: the roll surviving
    /// with the flock's true running state is what lets `shep muster`
    /// restore it after a reboot. `true` only for `shep dev`'s isolated
    /// session, where nothing here should ever be worth mustering — see
    /// `crate::snapshot::FlockRegistry::clear`'s own doc for the shutdown
    /// gap this closes.
    pub delete_flock_on_shutdown: bool,
}

/// Brings the daemon up: signal handlers, layout, roll restore, bus,
/// supervisor, socket, readiness report.
///
/// Step order here is deliberate and load-bearing, not incidental:
/// 1. install signal handlers — before the socket exists, so there is no
///    window where the socket is already live but an ordinary `kill -USR2`
///    (SIGUSR2's default disposition is to terminate) would still kill the
///    daemon instead of rotating logs;
/// 2. layout, then the crate-private `PidfileLock::acquire` — this is the
///    FIRST thing that can fail with [`BootError::AlreadyRunning`], before
///    `bind_socket` ever runs, and it is what makes that call race-free
///    against another process booting the same `$SHEP_HOME` concurrently
///    (see `PidfileLock`'s own doc, next to its definition in this file) —
///    then socket bind (with stale-socket recovery, spec §6), then
///    `PidfileLock::record` into the now-held-for-this-process's-whole-life
///    lock;
/// 3. report readiness, now that the socket is actually bound (spec §3) —
///    not once the whole flock is restored, so a slow muster can't make the
///    parent think boot failed. `options.ready_fd`, if set, already names
///    an owned [`std::fs::File`] the CALLER adopted before ever
///    constructing [`BootOptions`] (see that field's own doc) — this step
///    is a plain write, no fd adoption happens inside `boot` itself;
/// 4. bus, supervisor, muster restore, [`BootOptions::dogs`], snapshot
///    writer, `RpcContext` — and the point where step 1's SIGUSR2 listener
///    is handed the supervisor it reopens through, this being the first
///    moment one exists. See `install_signals`'s own doc, next to its
///    definition in this file, for why that seam is a channel rather than
///    an argument, and why the gap between the two steps drops no signal.
///    The dogs come up strictly between the restore and the snapshot
///    writer, and both halves of that placement are load-bearing: after
///    the restore, because a metrics dog that started first would answer
///    for an empty flock for the whole restore window, and a bark dog
///    would raise a `process.start` alert for every sheep the roll brings
///    back; before step 5's readiness report, because `Type=notify` going
///    green is meant to mean the whole daemon — flock and dogs alike — is
///    up, the same reasoning that put the restore itself inside that
///    promise;
/// 5. report readiness to an init system supervising this process directly
///    ([`BootOptions::notify_socket`], [`crate::notify`]) — last of all,
///    which is the opposite of step 3 and deliberately so: that one answers
///    a parent shep waiting to exit, this one decides when a unit goes
///    green, and a unit that goes green at exec time describes a flock that
///    is not up yet.
///
/// # Errors
/// - [`BootError::Io`] — a boot filesystem step failed, or the OS refused
///   to register a signal handler.
/// - [`BootError::AlreadyRunning`] — another daemon already holds the
///   pidfile lock, or (belt-and-suspenders, for a peer not participating in
///   that lock) answered on the socket.
/// - [`BootError::ReadyWrite`] — the readiness line could not be written to
///   `options.ready_fd`.
/// - [`BootError::Snapshot`] — `options.restore` was set, a roll exists, but
///   it could not be read or parsed.
pub async fn boot<R: ProcessRunner>(
    runner: R,
    paths: ShepPaths,
    mut options: BootOptions,
) -> Result<RunningDaemon, BootError> {
    // Copied out up front, purely a `bool`, so nothing later in this fn has
    // to remember to read it off `options` before that struct is consumed.
    let delete_flock_on_shutdown = options.delete_flock_on_shutdown;

    // 1. Install signal handlers before the socket (or anything else
    //    observable) exists — see this fn's own doc.
    let (shutdown, shutdown_rx) = watch::channel(false);
    let shutdown = Arc::new(shutdown);
    let (signals, connect_supervisor) = install_signals(Arc::clone(&shutdown), paths.clone())?;

    // 2. Layout, then claim exclusive ownership of $SHEP_HOME BEFORE
    //    touching the socket at all — see `PidfileLock`'s own doc for why
    //    this is what actually closes the concurrent-boot race a bare
    //    probe-then-recover sequence can't. Held across the whole
    //    bind-and-recover sequence, and for the rest of this daemon's life
    //    (kept in `RunningDaemon`, dropped only at the end of `run`).
    init_dirs(&paths)?;
    let mut pidfile_lock = PidfileLock::acquire(&paths)?;
    let socket = socket_path(&paths, options.socket.as_deref());
    let listener = bind_socket(&paths, &socket)?;
    let pid = std::process::id();
    pidfile_lock.record(&paths, pid)?;

    // 3. Report readiness now that the socket is bound. `options.ready_fd`
    //    is already an owned File adopted by the caller — see this fn's
    //    own doc and `BootOptions::ready_fd`'s doc — so this is nothing
    //    more than a write. TAKEN out of `options` rather than moved out of
    //    it: a partial move leaves the struct unborrowable, and step 4 hands
    //    it whole to `max_cron_sleep` — see that fn's own doc for why the
    //    field is not picked out here.
    if let Some(pipe) = options.ready_fd.take() {
        let ready = DaemonReady {
            pid,
            version: env!("CARGO_PKG_VERSION").to_string(),
        };
        write_ready(pipe, &ready)?;
    }

    // 4. Bus, supervisor, muster restore, snapshot writer, context.
    let events = new_bus();
    // Spawned the moment the bus exists, ahead of the supervisor that will
    // ever emit anything onto it: this is a subscriber like the snapshot
    // writer, not a branch inside the engine (see `spawn_dog_watch`'s own
    // doc), and giving it a receiver early means it can never miss an
    // `Errored` a dog reaches during boot's own restore step.
    let dog_watch = spawn_dog_watch(events.subscribe(), paths.barks.clone());
    let (breach_tx, breach_rx) = mpsc::channel(EXTRAS_REPORT_CAPACITY);
    let (live_tx, live_rx) = mpsc::channel(EXTRAS_REPORT_CAPACITY);
    let extras = Extras::real(
        ExtrasReports {
            breaches: breach_tx,
            liveness: live_tx,
        },
        max_cron_sleep(&options),
    );
    // Taken before `extras` is moved into the builder. One `StatsState`, two
    // owners, for the reason `Extras::enforcer` is shared the same way: the
    // extras decide which sheep is watched and record the periodic CPU
    // baseline, the RPC layer reads a live sample against it, and a second
    // state would leave one of the two reading an empty watch set.
    let stats = Arc::clone(&extras.stats);
    let supervisor = SupervisorBuilder::new(runner, paths.clone(), events.clone())
        .extras(extras)
        .spawn();
    // Ordered, not stylistic: the reporter needs the handle the builder
    // returns, and the actor must never own a receiver a subsystem feeds.
    //
    // Its `JoinHandle` is discarded, which DETACHES the task rather than
    // stopping it — and that closes a cycle worth naming, because nothing
    // here breaks it. The reporter holds a `SupervisorHandle`, so the actor's
    // mailbox can never reach zero senders while the reporter lives; the
    // reporter itself only ends once BOTH report senders have dropped, and the
    // enforcer holding one of them lives as long as the actor's registry does.
    // What actually ends both is `RunningDaemon::run`'s explicit
    // `SupervisorHandle::shutdown` (teardown step 4), which stops the actor by
    // command instead of by sender count and drops the registry with it. That
    // call is load-bearing for this reason as well as for the kill ladder it
    // is named after; a future teardown that relied on senders going away
    // would hang here instead.
    spawn_extras_reporter(breach_rx, live_rx, supervisor.clone());

    // The other half of step 1's SIGUSR2 listener, which has been parked on
    // this since before the socket existed, waiting for the handle it reopens
    // through — see `install_signals`'s own doc for why the wait is what the
    // step order forces and why it drops no signal. An `Err` would mean that
    // listener is already gone, which cannot happen while `signals` — moved
    // into the `RunningDaemon` below, and the only thing that aborts it — is
    // still alive.
    let _ = connect_supervisor.send(supervisor.clone());

    let registry = FlockRegistry::new();

    if options.restore {
        restore_flock(&paths, &registry, &supervisor).await?;
    }

    // After the restore, before step 5's readiness report — see this fn's
    // own doc, step 4, for why both halves of that placement are
    // load-bearing. Never fails the boot: a dog that cannot be spawned is a
    // monitoring gap, not an outage, and `spawn_enabled_dogs` warns and
    // carries on rather than propagating anything here.
    crate::dogs::spawn_enabled_dogs(&options.dogs, &paths, &supervisor).await;

    let writer = spawn_snapshot_writer(
        paths.snapshot.clone(),
        supervisor.clone(),
        registry.clone(),
        events.subscribe(),
    );

    let ctx = RpcContext {
        supervisor,
        events,
        registry,
        snapshot_path: paths.snapshot.clone(),
        daemon_config: paths.daemon_config.clone(),
        paths: paths.clone(),
        daemon_version: env!("CARGO_PKG_VERSION").to_string(),
        pid,
        shutdown,
        stats,
    };

    // 5. The flock is back and the plane is assembled, so this daemon is
    //    now what a unit ordered `After=` it expects to find. A failure is
    //    a `warn!` and the boot continues: the daemon is fully functional
    //    and only systemd's knowledge of it is wrong, which systemd's own
    //    `TimeoutStartSec` reports honestly — killing a working daemon over
    //    an undeliverable datagram would be the worse outcome.
    if let Some(target) = options.notify_socket.as_deref()
        && let Err(err) = crate::notify::notify(target)
    {
        tracing::warn!(
            %err,
            "readiness could not be reported to $NOTIFY_SOCKET; the flock is up regardless"
        );
    }

    Ok(RunningDaemon {
        ctx,
        listener,
        writer,
        dog_watch,
        paths,
        socket,
        // Held from here into `RunningDaemon` — `watch::Sender::send` is a
        // silent no-op whenever the receiver count is zero (confirmed
        // against tokio's own source), and `ctx.shutdown()` is callable the
        // instant a caller has `Self::context`, racing ahead of `run` ever
        // being polled. See `RunningDaemon::shutdown_rx`'s own doc.
        shutdown_rx,
        // Held from here into `RunningDaemon` too, and through the whole of
        // `run` — its `Drop` aborts every signal-listener task on every
        // exit from this point on, including an early `?` return from a
        // later step in THIS function. See `SignalTasks`'s own doc.
        signals,
        // Held from here into `RunningDaemon` and through the whole of
        // `run`: this is what keeps `$SHEP_HOME` exclusively claimed for
        // this daemon's entire life, not just its boot — dropping it (at
        // the end of `run`, or on an early `?`-return from a LATER step in
        // THIS function) is the only thing that releases the `flock`. See
        // `PidfileLock`'s own doc.
        pidfile_lock,
        delete_flock_on_shutdown,
    })
}

/// The cron sleep bound this boot runs with: [`BootOptions::max_cron_sleep`],
/// or [`DEFAULT_MAX_CRON_SLEEP`] when `shep.toml` named none.
///
/// A named function rather than an `unwrap_or` inline in [`boot`] only so the
/// application has a seam a test can stand on. It is still the ONE place that
/// constant is applied, and a second application anywhere else is how two
/// supposedly identical constants drift apart: `shep-core` carries the floor
/// and never the default, the daemon carries the default and never the floor.
///
/// It reads the whole [`BootOptions`] rather than the one field because the
/// field is what [`boot`] would otherwise have to pick out, and picking the
/// wrong one there is a mistake no test in this crate could catch: the only
/// behavioural trace `max_cron_sleep` leaves is how often a cron worker wakes,
/// and a wakeup is observable only through the [`Clock`](crate::cron::Clock)
/// seam that [`Extras::real`] fixes to the system clock. Reading the struct
/// here leaves nothing at the call site to get wrong.
fn max_cron_sleep(options: &BootOptions) -> Duration {
    options.max_cron_sleep.unwrap_or(DEFAULT_MAX_CRON_SLEEP)
}

/// Reads the muster roll (if one exists) and starts every app it restores.
///
/// One line over [`snapshot::muster`], which holds the whole restore rule and
/// its rationale — a missing roll, a corrupt one, a rejected entry, an app
/// the flock already has. The `Muster` request an operator sends runs that
/// same function, so the restore that happens unattended after a reboot is
/// the one an operator exercises by hand.
///
/// What boot supplies that the operator's call does not is an empty flock:
/// nothing here can already be running, so every restorable app is started
/// and the names come back describing exactly what boot just did. It discards
/// them because there is no one at this end of a boot to report them to.
async fn restore_flock(
    paths: &ShepPaths,
    registry: &FlockRegistry,
    supervisor: &SupervisorHandle,
) -> Result<(), BootError> {
    snapshot::muster(&paths.snapshot, registry, supervisor).await?;
    Ok(())
}

/// A booted daemon, not yet serving: everything [`boot`] assembled, handed
/// back so the caller can read [`Self::context`] before driving [`Self::run`].
#[derive(Debug)]
pub struct RunningDaemon {
    ctx: RpcContext,
    listener: UnixListener,
    writer: SnapshotWriter,
    // Parks on a broadcast receiver until this handle aborts it (see
    // `spawn_dog_watch`'s own doc for why holding the handle, not sender
    // count, is what makes that deterministic). Stopped in `run`'s teardown
    // step 1 alongside `writer`: both are bus subscribers with no further
    // reason to run once serving ends.
    dog_watch: JoinHandle<()>,
    paths: ShepPaths,
    socket: PathBuf,
    // Held from `boot` onward, not created fresh in `run`: `watch::Sender::send`
    // is a silent no-op (`Err`, value left unchanged — confirmed against
    // tokio's own source) whenever the receiver count is zero. `ctx.shutdown()`
    // is callable the moment `boot` returns [`Self::context`], which can race
    // ahead of `run` ever being polled; without a receiver alive for that
    // whole window, an early `ctx.shutdown()` is silently lost and `run` hangs
    // forever waiting on a signal that already fired. (Caught by
    // `boot_restores_a_saved_flock_and_tears_down_in_order`, which calls
    // `ctx.shutdown()` immediately after `tokio::spawn(daemon.run())` with no
    // guaranteed ordering between the two.)
    shutdown_rx: watch::Receiver<bool>,
    // Installed by `boot` (not `run` — see `boot`'s own doc for why: SIGUSR2
    // must be handled before the socket exists), kept alive through `run`'s
    // whole serving lifetime, and dropped only once teardown finishes —
    // `SignalTasks`'s own `Drop` is what actually stops these tasks.
    signals: SignalTasks,
    // Acquired by `boot` before the socket was ever bound, kept alive
    // through `run`'s whole serving lifetime, and dropped only once
    // teardown finishes — releasing this `flock` is what lets a NEXT
    // daemon's own `PidfileLock::acquire` succeed. See that type's own doc.
    pidfile_lock: PidfileLock,
    // Copied from `BootOptions::delete_flock_on_shutdown` at boot time; see
    // that field's own doc. Consulted only in `run`'s teardown step 2.
    delete_flock_on_shutdown: bool,
}

impl RunningDaemon {
    /// Handles for driving this daemon from outside its run loop.
    ///
    /// Public only for `tests/daemon_e2e.rs`, which needs to shut a booted
    /// daemon down and force a roll write without a socket round-trip. The
    /// CLI boots and calls [`Self::run`]; it never reaches inside.
    #[must_use]
    pub fn context(&self) -> RpcContext {
        self.ctx.clone()
    }

    /// The control socket this daemon is bound to.
    ///
    /// Public only for the crate-root doc example, which connects a raw
    /// client to it; the CLI already knows the path it asked to bind.
    #[must_use]
    pub fn socket(&self) -> &Path {
        &self.socket
    }

    /// Serves until a signal or `KillDaemon`, then tears down in order.
    ///
    /// TEARDOWN ORDER IS LOAD-BEARING:
    /// 1. stop the snapshot writer, and the dog watch alongside it — nothing
    ///    may rewrite the roll from here on, and no bus subscriber has a
    ///    reason left to watch once serving ends;
    /// 2. write the final muster roll — records the flock AS IT WAS, still
    ///    running, unless [`BootOptions::delete_flock_on_shutdown`] asked
    ///    for the registry to be wiped first, in which case it records
    ///    nothing;
    /// 3. broadcast [`BusEvent::DaemonShutdown`] — subscribers learn before their sockets close;
    /// 4. [`SupervisorHandle::shutdown`] — the kill ladder on every online sheep;
    /// 5. unlink the socket, remove the pidfile (best-effort on both: a
    ///    failure removing one must not skip attempting the other).
    ///
    /// Steps 1-2 before 4 are the whole point: run them the other way round
    /// and the roll records a flock of stopped sheep, and `shep muster`
    /// after a reboot restores nothing — silently breaking spec §13.4, the
    /// flagship migration scenario. Step 1 specifically must precede step 4
    /// (not just step 2): the writer would otherwise still be alive to
    /// observe the kill ladder's own `Exit`/`Stop` events and overwrite the
    /// roll step 2 just wrote.
    ///
    /// Every one of these steps runs unconditionally once this fn starts —
    /// `boot` succeeding is what commits the daemon to owning the flock,
    /// the roll, the socket, and the pidfile, so nothing short of a panic
    /// may return from here without having attempted every step above.
    /// (`install_signals`'s registration used to run here, in `run`, where
    /// its own failure could `?`-exit before any of this teardown had a
    /// chance to run at all; it now happens inside `boot` instead, before
    /// any of the state teardown depends on is even created — see `boot`'s
    /// own doc.)
    ///
    /// # Errors
    /// - [`BootError::Io`] — a teardown filesystem step failed.
    pub async fn run(self) -> Result<(), BootError> {
        let RunningDaemon {
            ctx,
            listener,
            writer,
            dog_watch,
            paths,
            socket,
            shutdown_rx,
            // Kept alive (not `_`) until this fn returns: `signals` must
            // outlive the whole serving lifetime below, and only its `Drop`
            // (at the end of this scope) stops its tasks. The underscore
            // prefix suppresses the "unused" warning for a binding that
            // exists purely for its drop side effect.
            signals: _signals,
            // Same reasoning as `signals` above: this `flock` must stay
            // held for the whole serving lifetime below, released only by
            // its `Drop` at the end of this scope — that release is what
            // lets a future daemon's own boot succeed.
            pidfile_lock: _pidfile_lock,
            delete_flock_on_shutdown,
        } = self;

        // `shutdown_rx` is the receiver `boot` has kept alive since the
        // watch channel was created (see the field's own doc) — reused
        // here rather than a fresh `ctx.shutdown.subscribe()` precisely so
        // there is never a window with zero receivers between `boot`
        // returning and this line running.
        RpcServer::new(listener, ctx.clone())
            .serve(shutdown_rx)
            .await;

        // 1. Stop the snapshot writer FIRST — see this fn's doc. The dog
        //    watch stops alongside it: both are bus subscribers with
        //    nothing left to watch for once serving ends.
        writer.stop().await;
        dog_watch.abort();

        // 2. Write the final roll while every sheep is still online — UNLESS
        //    this boot asked for nothing to survive here at all
        //    (`delete_flock_on_shutdown`, `shep dev`'s own case): then the
        //    registry is wiped first, so this write already agrees with
        //    step 4's kill ladder that nothing here should come back. This
        //    runs regardless of how serving just ended — a signal caught
        //    inside `install_signals` ends things exactly the same way a
        //    `KillDaemon` request does, and no caller-level `Stop`/`Delete`
        //    pair can run ahead of that path (see
        //    `crate::snapshot::FlockRegistry::clear`'s own doc).
        if delete_flock_on_shutdown {
            ctx.registry.clear();
        }
        if let Err(err) = ctx.snapshot_now().await {
            tracing::warn!(%err, "final muster roll write failed");
        }

        // 3. Tell subscribers before their sockets close underneath them.
        let _ = ctx.events.send(BusEvent::DaemonShutdown);

        // 4. Kill ladder on every online sheep.
        ctx.supervisor.shutdown().await;

        // 5. Unlink what boot created — both attempted regardless, the
        // first failure (if any) wins so a socket-unlink error can't hide
        // a pidfile that was never even attempted.
        let unlink_socket = unlink_if_present(&socket);
        let unlink_pidfile = unlink_if_present(&pidfile(&paths));
        unlink_socket.and(unlink_pidfile)
    }
}

/// Removes `path`, treating "already gone" as success rather than an error —
/// teardown's job is to make sure it's gone, not to prove it was there.
fn unlink_if_present(path: &Path) -> Result<(), BootError> {
    match std::fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(err) if err.kind() == ErrorKind::NotFound => Ok(()),
        Err(source) => Err(BootError::Io {
            path: path.to_path_buf(),
            source,
        }),
    }
}

/// Live signal-listener tasks [`install_signals`] spawned, held so they are
/// properly stopped — not merely detached, see this type's own [`Drop`] impl
/// — on every exit from [`boot`] or [`RunningDaemon::run`] that follows a
/// successful `install_signals` call. That includes an early `?`-return from
/// a LATER step inside `boot` itself (a failed `bind_socket` after signals
/// were already installed, say): dropping the partially-built value in that
/// path must not leak a task per boot attempt, which is exactly what
/// happened before this type existed (a bare counter returned by value, with
/// the actual `JoinHandle`s discarded at the spawn site).
#[derive(Debug)]
struct SignalTasks {
    tasks: Vec<JoinHandle<()>>,
}

impl Drop for SignalTasks {
    fn drop(&mut self) {
        // `JoinHandle::drop` alone DETACHES a task rather than stopping it
        // (the same footgun `server.rs`'s `converse` doc calls out for the
        // bus forwarder) — every task this struct owns is explicitly
        // aborted here, which is the whole reason this type exists instead
        // of a bare `Vec<JoinHandle<()>>`.
        for task in &self.tasks {
            task.abort();
        }
    }
}

/// Installs SIGTERM/SIGINT/SIGQUIT (graceful shutdown, first signal starts
/// it — see below for what a repeat does) and SIGUSR2 (`shep reopen`'s
/// out-of-band form, spec §9).
///
/// SIGUSR2's DEFAULT disposition is to terminate the process, so installing
/// this handler is load-bearing on its own: without it, an operator's
/// `kill -USR2` — or a logrotate `postrotate` stanza — kills the daemon
/// instead of rotating logs. A signal carries no selector, so it can only
/// mean [`ProcessSelector::All`], and that is exactly what it asks the
/// supervisor for: every sheep's log pump swaps both of its file handles,
/// the same work `shep reopen all` does. There is no reply channel either,
/// so the outcome is logged rather than reported to anyone.
///
/// # The supervisor arrives after the handler does
///
/// Returned alongside the tasks: the sender that hands the SIGUSR2 listener
/// the [`SupervisorHandle`] it reopens through. This function runs at
/// [`boot`]'s step 1, deliberately before the socket — or anything else —
/// exists, and the supervisor is not built until step 4, so no handle can be
/// passed in here. The listener parks on the matching receiver instead, and
/// `boot` connects the two once it has a handle to give.
///
/// That wait costs no delivery. The `signal()` call below is what replaces
/// SIGUSR2's disposition, and it has already done so by the time this
/// function returns; tokio coalesces every notification arriving before the
/// first `poll` into one item that the first `recv()` then yields. A SIGUSR2
/// raced into the window between step 1 and step 4 is therefore served late,
/// never dropped. A `boot` that FAILS before step 4 drops the sender instead
/// and the listener ends — with the libc disposition still replaced, which
/// is the half that matters for a process on its way out.
///
/// **Each listener stays armed for the rest of the process's life
/// (Decision 3, 2026-08-08).** A signal handler, once installed, is
/// installed for good — `tokio` never uninstalls the underlying libc
/// disposition just because the [`tokio::signal::unix::Signal`] stream
/// polling it happens to stop. An earlier version of this function's
/// SIGTERM/SIGINT/SIGQUIT loop awaited exactly one signal and returned,
/// which left a real gap: a SECOND SIGTERM arriving during a slow
/// [`RunningDaemon::run`] teardown (the kill ladder waiting out
/// `kill_timeout` on a stuck sheep, say) had nowhere left to go — not
/// re-delivered to the now-finished task, and not killing the process
/// either, since installing ANY handler for a signal already replaced its
/// default terminate disposition. The daemon would sit there, unresponsive
/// to a second graceful request, with `SIGKILL` as the only remaining way
/// out. Looping keeps every listener polling for as long as the process
/// runs, so no delivery is ever silently dropped; a repeat is logged (see
/// the loop below) but does not otherwise change teardown, which is
/// already unconditional and already running (see
/// [`RunningDaemon::run`]'s own doc) — this crate does not invent an
/// escalation policy beyond "stay armed and observable" that nothing in
/// the spec or plan asks for.
///
/// # Errors
/// - [`BootError::Io`] — the OS refused to register a signal handler.
fn install_signals(
    shutdown: Arc<watch::Sender<bool>>,
    paths: ShepPaths,
) -> Result<(SignalTasks, oneshot::Sender<SupervisorHandle>), BootError> {
    let mut signals = SignalTasks {
        tasks: Vec::with_capacity(4),
    };

    for kind in [
        SignalKind::terminate(),
        SignalKind::interrupt(),
        SignalKind::quit(),
    ] {
        // An early return here drops `signals`, whose own `Drop` aborts
        // every task already pushed — registering the 2nd or 3rd kind
        // failing must not leak the 1st's already-spawned listener.
        let mut stream = signal(kind).map_err(|source| BootError::Io {
            path: paths.home.clone(),
            source,
        })?;
        let shutdown = Arc::clone(&shutdown);
        signals.tasks.push(tokio::spawn(async move {
            // Looped, not `stream.recv().await` once — see this fn's own
            // doc for why a single await left a real gap. `recv` returning
            // `None` would mean the stream itself closed (never observed
            // in practice — the same reasoning the SIGUSR2 loop below
            // already relies on), at which point this task has nothing
            // left to listen for and ending it is correct.
            let mut already_shutting_down = false;
            while stream.recv().await.is_some() {
                if already_shutting_down {
                    // Not escalated further on purpose: the brief asks
                    // only that a repeat signal during teardown be
                    // observable, not that it change teardown's own
                    // behavior (already unconditional, see
                    // `RunningDaemon::run`'s doc) — an operator who needs
                    // the daemon gone RIGHT NOW still has `SIGKILL`, which
                    // no handler installed here can intercept.
                    tracing::warn!(
                        ?kind,
                        "received a repeat shutdown signal while teardown is already \
                         underway; teardown continues unchanged (SIGKILL forces an \
                         immediate exit)"
                    );
                } else {
                    already_shutting_down = true;
                }
                let _ = shutdown.send(true);
            }
        }));
    }

    let mut usr2 = signal(SignalKind::user_defined2()).map_err(|source| BootError::Io {
        path: paths.home.clone(),
        source,
    })?;
    let (connect_supervisor, supervisor_rx) = oneshot::channel::<SupervisorHandle>();
    signals.tasks.push(tokio::spawn(async move {
        // Parked until `boot` reaches step 4 — see this fn's own doc for why
        // the wait loses no signal, and for what an `Err` here means.
        let Ok(supervisor) = supervisor_rx.await else {
            return;
        };
        while usr2.recv().await.is_some() {
            // A rotator that moved the whole log DIRECTORY needs it back at
            // `DIR_MODE`, and the pump this reaches is what puts it there —
            // its own open asks `mkdir` for the mode (see `open_append`).
            // Recreating it here as well would be a second owner of the same
            // guarantee, differing from the pump's for any sheep logging
            // outside the layout.
            match supervisor.reopen(ProcessSelector::All).await {
                Ok(reopened) => tracing::info!(
                    reopened = reopened.len(),
                    "SIGUSR2: every sheep's log files reopened"
                ),
                // An empty flock is an idle daemon's ordinary state, not
                // something a nightly `postrotate` should warn about.
                Err(SupervisorError::NotFound) => {
                    tracing::info!("SIGUSR2: no sheep to reopen");
                }
                // A signal carries no reply channel, so this log is the whole
                // report — nobody is waiting to be told.
                Err(err) => tracing::warn!(%err, "SIGUSR2: log reopen failed"),
            }
        }
    }));

    Ok((signals, connect_supervisor))
}

/// Error type returned from this module's boot steps
///
/// Wraps `io::Error` directly rather than stringifying it (contrast
/// [`shep_core::protocol::WireError`]) so callers keep the underlying OS
/// diagnostic via [`core::error::Error::source`]; that costs this enum
/// `Clone`/`PartialEq`/`Eq` (IR-19's documented exception for variants
/// wrapping `io::Error`).
///
/// `#[non_exhaustive]`: today's four variants cover filesystem setup, socket
/// claim, roll restore, and readiness reporting, and a future boot step —
/// socket-activation handoff, or cgroup setup — would add a fifth rather
/// than overloading [`Self::Io`], whose `path`/`source` shape is specific to
/// the steps that already exist, and shep-daemon is a published library an
/// out-of-tree matcher should not break for (IR-20).
#[non_exhaustive]
#[derive(Debug)]
pub enum BootError {
    /// A filesystem step failed (carries the path and the OS error)
    ///
    /// Deliberately has no `From<std::io::Error>`, and neither does any
    /// sibling: `ReadyWrite` wraps the same type, so one would make a bare
    /// `?` in this module pick a variant rather than report one. A caller
    /// that cannot name the path it was working on has not finished
    /// thinking about the error yet.
    Io {
        /// The path the failing step operated on
        path: PathBuf,
        /// The underlying OS error
        source: std::io::Error,
    },
    /// Another daemon already answers on this socket (carries its pid if recorded)
    AlreadyRunning {
        /// The pid recorded in the pidfile, if one was readable
        pid: Option<u32>,
    },
    /// The muster roll exists but could not be read or parsed on restore
    Snapshot(SnapshotError),
    /// `$SHEP_HOME` puts the control socket past the platform's `sun_path`
    /// limit, so no bind could ever succeed (carries the path and the limit)
    ///
    /// Checked before the bind rather than translated after it: the kernel's
    /// own `ENAMETOOLONG` names neither the limit nor the variable
    /// responsible, and an operator reading it has no way to know that the
    /// number is 104 here and 108 on Linux, nor that `$SHEP_HOME` is what
    /// feeds it.
    SocketPathTooLong {
        /// The socket path that would not fit
        path: PathBuf,
        /// Its length in bytes
        len: usize,
        /// This platform's `sun_path` capacity in bytes
        limit: usize,
    },
    /// Writing the readiness line to the caller-adopted readiness pipe
    /// failed (carries the OS error)
    ///
    /// Adoption itself (`unsafe fn` [`crate::sys::adopt_fd`]) is the
    /// caller's job, not `boot`'s — see [`BootOptions::ready_fd`]'s own
    /// doc — so `boot` has no error variant for a failed adoption; by the
    /// time `options.ready_fd` reaches here it is already a valid, owned
    /// [`std::fs::File`], and this variant covers only the plain IO write
    /// to it.
    ReadyWrite(std::io::Error),
}

impl fmt::Display for BootError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io { path, source } => {
                write!(f, "boot step failed for `{}`: {source}", path.display())
            }
            Self::AlreadyRunning { pid: Some(pid) } => {
                write!(f, "a shep daemon is already running (pid {pid})")
            }
            Self::AlreadyRunning { pid: None } => write!(f, "a shep daemon is already running"),
            Self::Snapshot(err) => write!(f, "muster roll restore failed: {err}"),
            Self::SocketPathTooLong { path, len, limit } => write!(
                f,
                "the control socket path is {len} bytes and this platform allows {limit}: `{}`. \
                 A unix socket path is bounded by the kernel, not by shep, so a shorter \
                 $SHEP_HOME is the only fix.",
                path.display()
            ),
            Self::ReadyWrite(err) => write!(f, "writing the readiness line failed: {err}"),
        }
    }
}

impl core::error::Error for BootError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            Self::Io { source, .. } => Some(source),
            Self::AlreadyRunning { .. } => None,
            // No source: nothing failed underneath, the path was refused
            // before any syscall was attempted.
            Self::SocketPathTooLong { .. } => None,
            Self::Snapshot(err) => Some(err),
            Self::ReadyWrite(err) => Some(err),
        }
    }
}

impl From<SnapshotError> for BootError {
    fn from(source: SnapshotError) -> Self {
        Self::Snapshot(source)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dogs::DogSpec;
    use crate::fake::{ProcScript, ScriptedRunner};
    use crate::snapshot::{FlockSnapshot, SNAPSHOT_VERSION, SavedApp};
    // the one crate-root fixture (IR-33)
    use crate::testing::{AnnouncingRunner, SharedRunner, capture_logs, test_paths};
    use shep_core::config::{AppConfig, ProbeConfig, ProbeKind, normalize};
    use shep_core::protocol::{DogSource, ProcessEventKind};
    use shep_core::status::ProcStatus;
    use shep_core::values::UpDuration;
    use std::os::unix::fs::PermissionsExt;
    use std::time::Duration;

    fn mode_of(path: &Path) -> u32 {
        std::fs::metadata(path).unwrap().permissions().mode() & 0o777
    }

    /// Serializes every test in this module that calls `boot()` and expects
    /// it to succeed — NOT just the ones that go on to call `daemon.run()`
    /// — against every OTHER such test, because `nix::sys::signal::raise`
    /// (and the real OS signal delivery it triggers) is NOT scoped to one
    /// test's own tokio runtime: every `Signal` stream registered anywhere
    /// in this test BINARY'S process receives it, regardless of which test
    /// spawned it or which runtime owns it.
    ///
    /// The rule is "calls `boot()` successfully", not "calls `run()`",
    /// because `install_signals` moved INTO `boot` (see `boot`'s own doc):
    /// a test whose `boot()` call succeeds has live signal listeners
    /// running from that point on, whether or not it ever calls `run()`.
    /// Proven concretely, not just reasoned about (Opus review follow-up,
    /// 2026-08-08): a `boot`-only test with no lock passed 10/10 runs in
    /// isolation and FAILED 10/10 runs alongside
    /// `sigterm_triggers_the_same_graceful_shutdown` — the exact same
    /// process-wide `raise(SIGTERM)` hazard, just reached through `boot`
    /// alone rather than through `run`. Task 10's e2e `Fixture` is the next
    /// `boot()` caller this crate will grow, so this is a real, standing
    /// tripwire, not a one-off.
    ///
    /// Proven load-bearing on the shutdown-signal front too (Opus review,
    /// 2026-08-08): reintroducing the fixed watch-receiver bug on purpose
    /// (dropping the initial `shutdown_rx` again) still made `boot::tests`
    /// PASS under the default PARALLEL test runner —
    /// `sigterm_triggers_the_same_graceful_shutdown`'s `raise(SIGTERM)`
    /// accidentally reached `boot_restores_a_saved_flock_and_tears_down_in_order`'s
    /// OWN daemon (hung on the reintroduced bug) too, on the SAME signal
    /// delivery, and rescued it — masking the regression. Only
    /// `--test-threads=1` (or, now, this lock) exposed it. Every test below
    /// that calls `boot()` takes this for its own duration so no two such
    /// tests can ever overlap and one can never rescue (or corrupt) another.
    ///
    /// `tokio::sync::Mutex`, not `std::sync::Mutex`: the guard is held
    /// across this fn's own `.await` points (`boot`, `run`, ...), and
    /// clippy's `await_holding_lock` correctly denies a blocking guard held
    /// there — an async-aware mutex has no such restriction.
    static SIGNAL_TEST_LOCK: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());

    #[test]
    fn init_dirs_creates_the_whole_layout_owner_only() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();
        for path in [&paths.home, &paths.logs, &paths.pids, &paths.run] {
            assert!(path.is_dir(), "{} was not created", path.display());
            assert_eq!(mode_of(path), DIR_MODE, "{}", path.display());
        }
        init_dirs(&paths).unwrap(); // idempotent: a restart must not fail here
    }

    #[test]
    fn a_fresh_dir_lands_at_dir_mode_with_no_separate_chmod() {
        // TOCTOU regression guard: this calls the raw creation primitive
        // ALONE, with no follow-up set_permissions in this test, so it can
        // only pass if DirBuilder's `.mode(DIR_MODE)` really lands the mode
        // at creation. A regression back to `create_dir_all` (0o777, narrowed
        // only by whatever the ambient umask strips) would still slip past
        // init_dirs_creates_the_whole_layout_owner_only above, because that
        // test only observes the mode after init_dirs' own chmod pass has
        // already run — it can't see the window this test targets.
        let dir = tempfile::tempdir().unwrap();
        let never_existed = dir.path().join("nested").join("run");
        create_dir_at_dir_mode(&never_existed).unwrap();
        assert_eq!(
            mode_of(&never_existed),
            DIR_MODE,
            "a freshly created dir must be DIR_MODE at creation, not after a later chmod"
        );
    }

    #[test]
    fn init_dirs_tightens_a_world_readable_runtime_dir() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        std::fs::create_dir_all(&paths.run).unwrap();
        std::fs::set_permissions(&paths.run, std::fs::Permissions::from_mode(0o755)).unwrap();
        init_dirs(&paths).unwrap();
        assert_eq!(
            mode_of(&paths.run),
            DIR_MODE,
            "a loose run dir must be tightened, not accepted"
        );
    }

    #[test]
    fn pidfile_round_trips_and_reports_absence() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();
        assert_eq!(read_pidfile(&paths).unwrap(), None);
        write_pidfile(&paths, 4242).unwrap();
        assert_eq!(read_pidfile(&paths).unwrap(), Some(4242));
        assert_eq!(pidfile(&paths), paths.pids.join("shepd.pid"));
    }

    #[test]
    fn socket_path_honors_a_config_override() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        assert_eq!(socket_path(&paths, None), paths.socket);
        let custom = dir.path().join("custom.sock");
        assert_eq!(socket_path(&paths, Some(&custom)), custom);
    }

    /// The kernel's own refusal is `ENAMETOOLONG` and names neither the
    /// limit nor the variable that produced it. An operator whose
    /// `$SHEP_HOME` is a few directories too deep has no way to guess that
    /// the number is 104 here and 108 on Linux.
    #[tokio::test]
    async fn an_over_length_socket_path_names_the_limit_and_the_variable() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();

        // Comfortably past both platforms' capacity, without depending on
        // which one this is.
        let long = dir.path().join("x".repeat(200));

        let err = bind_socket(&paths, &long).expect_err("a path this long cannot bind");
        assert!(
            matches!(err, BootError::SocketPathTooLong { .. }),
            "refused before the syscall, not translated after it: {err:?}"
        );

        let rendered = err.to_string();
        assert!(
            rendered.contains("$SHEP_HOME"),
            "the message names what to shorten: {rendered}"
        );
        assert!(
            rendered.contains("bytes"),
            "and the limit it is measured against: {rendered}"
        );
        assert!(
            !rendered.contains('\u{2014}') && !rendered.contains('\u{2013}'),
            "no em or en dash in copy a user reads: {rendered}"
        );
    }

    #[tokio::test]
    async fn bind_socket_binds_a_fresh_path() {
        // Real time: real socket IO (see the paused-clock rule).
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();
        let listener = bind_socket(&paths, &paths.socket).unwrap();
        assert!(paths.socket.exists());
        drop(listener);
    }

    /// Fabricates exactly what a crashed daemon leaves behind: a socket file
    /// at `socket` that nothing is listening on. Neither std nor tokio
    /// unlinks a `UnixListener`'s path on drop, so binding and dropping is
    /// the right shape — but it is NOT, on its own, enough to guarantee the
    /// second half of that sentence inside this test binary.
    ///
    /// macOS has no atomic `SOCK_CLOEXEC` (the descriptor is marked
    /// close-on-exec a moment AFTER `socket(2)` returns), and a `fork` copies
    /// the whole descriptor table, so any child another test spawns
    /// concurrently — the exec probes, the runner tests — can end up holding
    /// a duplicate of the listener below. For as long as that duplicate lives
    /// (until the child's `exec` or exit; measured at up to ~25ms) the socket
    /// object is NOT destroyed, `connect` to the path SUCCEEDS, and any
    /// prober is looking at a live socket. That is not a daemon bug — it is a
    /// lying fixture, and it is what made
    /// [`two_concurrent_boots_on_a_stale_socket_exactly_one_wins`] fail as
    /// `[AlreadyRunning, AlreadyRunning]`, both racers refused, roughly 1 run
    /// in 28 of this crate's suite under a saturated machine: the flock's
    /// loser refused correctly, and the flock's WINNER then found this
    /// leftover answering and refused too, exactly as `bind_socket` should.
    ///
    /// So establish the precondition instead of assuming it: don't return
    /// until the path actually refuses a connection. That verdict is stable
    /// once it lands — a refused socket is a destroyed socket, and only a
    /// fresh `bind` can make the path answer again, which nothing but the
    /// code under test does.
    ///
    /// Real sleeps, in a module whose default is a paused clock (IR-33): a
    /// descriptor's lifetime in ANOTHER process is not on any clock tokio can
    /// advance, and every caller of this helper is already a real-time test
    /// for the same reason (real socket IO).
    ///
    /// # Panics
    /// If the leftover never goes stale, or the probe fails for any reason
    /// other than nobody listening.
    #[track_caller]
    fn stale_socket_leftover(socket: &Path) {
        // Two nested loops for two different holders. The inner one waits out
        // the common case, a child that copied the descriptor mid-spawn and
        // drops it on `exec`. The outer one re-fabricates for the rarer one,
        // a child that forked inside the socket(2)-to-close-on-exec window
        // and therefore keeps the descriptor for its whole life: unlinking
        // detaches that socket from this path for good, and the next bind
        // starts clean.
        for _ in 0..20 {
            let _ = std::fs::remove_file(socket);
            drop(std::os::unix::net::UnixListener::bind(socket).unwrap());
            for _ in 0..40 {
                match std::os::unix::net::UnixStream::connect(socket) {
                    Err(refused)
                        if matches!(
                            refused.kind(),
                            ErrorKind::ConnectionRefused | ErrorKind::NotFound
                        ) =>
                    {
                        return;
                    }
                    Ok(_) => std::thread::sleep(Duration::from_millis(5)),
                    Err(other) => {
                        panic!("probing the fabricated leftover socket failed: {other}")
                    }
                }
            }
        }
        panic!(
            "{} never went stale: something kept answering on it",
            socket.display()
        );
    }

    #[tokio::test]
    async fn a_socket_left_by_a_crash_is_unlinked_and_rebound() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();
        stale_socket_leftover(&paths.socket);
        assert!(paths.socket.exists(), "the stale file must still be there");
        let listener = bind_socket(&paths, &paths.socket).unwrap();
        assert!(paths.socket.exists());
        drop(listener);
    }

    #[tokio::test]
    async fn a_live_socket_is_reported_as_already_running() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();
        let live = UnixListener::bind(&paths.socket).unwrap();
        write_pidfile(&paths, 4242).unwrap();
        assert!(matches!(
            bind_socket(&paths, &paths.socket),
            Err(BootError::AlreadyRunning { pid: Some(4242) })
        ));
        // The refused bind must be a pure probe: the live daemon's socket
        // file is untouched (not unlinked) and still answers, proving
        // bind_socket never reached the remove_file/rebind arm here.
        assert!(
            paths.socket.exists(),
            "a live daemon's socket must never be unlinked"
        );
        std::os::unix::net::UnixStream::connect(&paths.socket)
            .expect("the live listener must still be accepting after a refused bind");
        drop(live);
    }

    /// What one racer thread observed in
    /// [`two_concurrent_boots_on_a_stale_socket_exactly_one_wins`] — kept
    /// deliberately small and `'static` so it can cross the thread boundary
    /// without carrying a `RunningDaemon` (and the tokio resources inside
    /// it) out of the runtime that created it. See that test's own doc for
    /// why.
    #[derive(Debug)]
    enum RaceOutcome {
        Won { socket_still_accepts: bool },
        AlreadyRunning,
        Other(String),
    }

    #[test]
    fn two_concurrent_boots_on_a_stale_socket_exactly_one_wins() {
        // Pins Decision 2 (2026-08-08), closing double-bind race #3: two
        // daemons racing to boot the same $SHEP_HOME could both hit
        // `bind_socket`'s EADDRINUSE -> probe -> remove_file -> rebind arm
        // over a CRASHED predecessor's leftover socket file, both observe
        // `ConnectionRefused` (correctly — nobody's listening), and both
        // proceed into the recovery arm — loser's `remove_file` can delete
        // winner's freshly-bound listener out from under it, leaving BOTH
        // convinced they hold the sole live daemon. See `PidfileLock`'s own
        // doc for the full mechanism and why `flock` closes it.
        //
        // NOT `#[tokio::test]`, and each racer gets its OWN
        // `new_current_thread` runtime on its OWN `std::thread`: `boot`'s
        // own synchronous prefix (signals, dirs, the pidfile lock, socket
        // bind) never actually awaits a not-yet-ready future, so two
        // `boot()` calls driven as plain tokio TASKS on one runtime would
        // never really interleave — the executor would run the first one's
        // entire synchronous body to completion in a single poll before the
        // second ever got scheduled, proving nothing about real contention.
        // Real OS threads, synchronized to start together via a `Barrier`,
        // give the two racers genuine kernel-level concurrency over the
        // same `open`/`flock`/`bind`/`connect`/`remove_file` calls — the
        // actual shape of the bug this test pins.
        //
        // Looped: a race this timing-dependent isn't guaranteed to land on
        // the exact bad interleaving every single attempt (the fd-reuse
        // double-close that `sys.rs`'s
        // `a_closed_descriptor_is_refused_instead_of_adopted` now pins
        // structurally took 25 saturated workspace runs to show itself even
        // once) — running many trials inside one test call, and failing on
        // the first bad one, is what makes the revert-and-confirm-it-fails
        // check below actually reliable rather than a coin flip.
        //
        // Locked per SIGNAL_TEST_LOCK's rule for every trial that has a
        // winner: `blocking_lock` because this outer fn is plain sync, not
        // `#[tokio::test]`, so there is no surrounding runtime to `.await`
        // on when acquiring it.
        for _ in 0..25 {
            let _guard = SIGNAL_TEST_LOCK.blocking_lock();
            let dir = tempfile::tempdir().unwrap();
            let paths = test_paths(&dir);
            init_dirs(&paths).unwrap();
            // The crashed predecessor's leftover, and it must really be
            // leftover before the racers start: a socket another test's
            // mid-spawn child is briefly keeping alive would make the flock's
            // WINNER refuse too, for a reason that has nothing to do with the
            // race under test. See `stale_socket_leftover`'s own doc — that
            // exact false failure is why it exists.
            stale_socket_leftover(&paths.socket);

            let barrier = Arc::new(std::sync::Barrier::new(2));
            let handles: Vec<_> = (0..2)
                .map(|_| {
                    let paths = paths.clone();
                    let barrier = Arc::clone(&barrier);
                    std::thread::spawn(move || {
                        barrier.wait(); // both racers cross together
                        let rt = tokio::runtime::Builder::new_current_thread()
                            .enable_all()
                            .build()
                            .unwrap();
                        rt.block_on(async {
                            match boot(ScriptedRunner::new(vec![]), paths, BootOptions::default())
                                .await
                            {
                                Ok(daemon) => {
                                    // Checked (and `daemon` dropped) INSIDE
                                    // this racer's own runtime — see
                                    // `RaceOutcome`'s own doc for why
                                    // nothing tokio-shaped crosses the
                                    // thread boundary.
                                    let reachable =
                                        std::os::unix::net::UnixStream::connect(daemon.socket())
                                            .is_ok();
                                    RaceOutcome::Won {
                                        socket_still_accepts: reachable,
                                    }
                                }
                                Err(BootError::AlreadyRunning { .. }) => {
                                    RaceOutcome::AlreadyRunning
                                }
                                Err(other) => RaceOutcome::Other(other.to_string()),
                            }
                        })
                    })
                })
                .collect();
            let outcomes: Vec<RaceOutcome> =
                handles.into_iter().map(|h| h.join().unwrap()).collect();

            for outcome in &outcomes {
                if let RaceOutcome::Other(msg) = outcome {
                    panic!("a racer hit neither Ok nor AlreadyRunning: {msg}");
                }
            }

            let wins = outcomes
                .iter()
                .filter(|o| matches!(o, RaceOutcome::Won { .. }))
                .count();
            let already_running = outcomes
                .iter()
                .filter(|o| matches!(o, RaceOutcome::AlreadyRunning))
                .count();
            assert_eq!(
                wins, 1,
                "exactly one racer must win a boot on the same $SHEP_HOME: {outcomes:?}"
            );
            assert_eq!(
                already_running, 1,
                "the loser must be refused as AlreadyRunning, not silently succeed or hit some other error: {outcomes:?}"
            );
            assert!(
                matches!(
                    outcomes
                        .iter()
                        .find(|o| matches!(o, RaceOutcome::Won { .. }))
                        .unwrap(),
                    RaceOutcome::Won {
                        socket_still_accepts: true
                    }
                ),
                "the winner's own socket must still accept a connection, proving its bind \
                 wasn't the one the loser's remove_file clobbered: {outcomes:?}"
            );
        }
    }

    #[test]
    fn readiness_reports_pid_and_version_then_closes_the_pipe() {
        use std::io::Read;
        // Safe end-to-end, unlike the pre-Decision-1 version of this test:
        // `std::io::pipe` (stable 1.87, below this workspace's 1.88 floor)
        // hands back an owned `PipeWriter`, which converts into `File`
        // through the standard `OwnedFd` bridge — no `unsafe`, because this
        // test created the pipe itself and knows exactly what it is. There
        // is nothing left in this module for `sys::adopt_fd` to be called
        // on; see this file's own module doc.
        let (mut reader, writer) = std::io::pipe().unwrap();
        let pipe = std::fs::File::from(std::os::fd::OwnedFd::from(writer));
        let ready = DaemonReady {
            pid: 4242,
            version: "0.1.0".to_string(),
        };
        write_ready(pipe, &ready).unwrap();
        let mut line = String::new();
        reader.read_to_string(&mut line).unwrap();
        assert_eq!(line.trim_end(), serde_json::to_string(&ready).unwrap());
        assert!(line.ends_with('\n'), "the parent reads a line: {line:?}");
    }

    #[tokio::test]
    async fn boot_writes_readiness_to_the_callers_pipe_after_the_socket_is_bound() {
        // Real time: binds a real socket. Locked per SIGNAL_TEST_LOCK's rule
        // — any test whose `boot()` call succeeds has live signal listeners
        // from that point on.
        //
        // Decision 1 (2026-08-08) replaced `boot_refuses_a_stale_ready_fd_
        // before_touching_anything_else`, which lived here and drove a bad
        // fd number straight through `BootOptions` to pin that adoption was
        // refused before `bind_socket` ran. That guard is no longer
        // expressible through `boot`'s public API at all: `BootOptions::
        // ready_fd` is `Option<std::fs::File>` now, and there is no safe
        // way to hand this test a `File` that names a bad descriptor — the
        // type itself is the proof the fd was valid at construction time.
        // The BadFd-refusal behavior itself still exists and is still
        // tested, just one layer down: see `sys::tests::
        // a_fd_this_process_never_owned_is_refused`, which calls
        // `sys::adopt_fd` directly (that module's own job now). What THIS
        // test covers instead — real coverage that would otherwise be lost
        // entirely, since no other test drives a `Some` `ready_fd` through
        // `boot` at all — is the happy path: a caller-adopted pipe really
        // does receive the readiness line, and only after the socket is
        // genuinely bound (spec §3), exactly as `boot`'s own doc claims.
        use std::io::Read;
        let _guard = SIGNAL_TEST_LOCK.lock().await;
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);

        let (mut reader, writer) = std::io::pipe().unwrap();
        let pipe = std::fs::File::from(std::os::fd::OwnedFd::from(writer));

        let daemon = boot(
            ScriptedRunner::new(vec![]),
            paths.clone(),
            BootOptions {
                ready_fd: Some(pipe),
                ..BootOptions::default()
            },
        )
        .await
        .unwrap();
        assert!(
            paths.socket.exists(),
            "boot must bind the socket before it returns"
        );

        // `write_ready` closes its `File` at the end of `boot`'s own call
        // to it, so this read observes EOF (the readiness line, then
        // nothing) without blocking on a live writer.
        let mut line = String::new();
        reader.read_to_string(&mut line).unwrap();
        let ready: DaemonReady = serde_json::from_str(line.trim_end()).unwrap();
        assert_eq!(ready.pid, std::process::id());
        assert!(line.ends_with('\n'), "the parent reads a line: {line:?}");

        drop(daemon); // no run() needed; SignalTasks::drop stops the listeners
    }

    #[tokio::test]
    async fn boot_restores_a_saved_flock_and_tears_down_in_order() {
        // Real time: binds a real socket. Locked against
        // `sigterm_triggers_the_same_graceful_shutdown` — see
        // SIGNAL_TEST_LOCK's own doc.
        let _guard = SIGNAL_TEST_LOCK.lock().await;
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();
        // instances_running is a deliberately WRONG sentinel (99, not the 1
        // instance actually restored below): restore reads `app.instances`
        // to decide how many to start, never this historical count, so it
        // has no effect on the boot itself. Its only job is to make the
        // final-roll assertion below load-bearing: if teardown's roll write
        // is ever skipped (e.g. because a reordering runs it after the
        // supervisor has already stopped, when `snapshot_now` silently
        // no-ops rather than querying a dead engine), this stale 99 survives
        // untouched and the assertion catches it. A seeded `1` would have
        // matched the correct post-teardown value by coincidence and let a
        // skipped write pass unnoticed.
        let roll = FlockSnapshot {
            version: SNAPSHOT_VERSION,
            saved_at_ms: 0,
            apps: vec![SavedApp {
                app: AppConfig::minimal("web", "./srv"),
                instances_running: 99,
            }],
        };
        crate::snapshot::write_atomic(&paths.snapshot, &roll).unwrap();

        let daemon = boot(
            ScriptedRunner::new(vec![ProcScript::never_exits()]),
            paths.clone(),
            BootOptions {
                restore: true,
                ..BootOptions::default()
            },
        )
        .await
        .unwrap();
        let ctx = daemon.context();
        let flock = ctx.supervisor.list_checked().await.unwrap();
        assert_eq!(flock.len(), 1, "the muster roll must be back on its feet");
        assert_eq!(flock[0].name, "web");

        let run = tokio::spawn(daemon.run());
        ctx.shutdown();
        tokio::time::timeout(Duration::from_secs(5), run)
            .await
            .unwrap()
            .unwrap()
            .unwrap();

        // The roll written during teardown records the flock as it WAS.
        let final_roll = crate::snapshot::read(&paths.snapshot).unwrap();
        assert_eq!(
            final_roll.apps[0].instances_running, 1,
            "the roll must be written before the flock is killed, or muster restores nothing"
        );
        assert!(
            !paths.socket.exists(),
            "the socket is unlinked on a clean exit"
        );
        assert_eq!(read_pidfile(&paths).unwrap(), None);
    }

    /// fails if `delete_flock_on_shutdown` leaves anything in the final
    /// roll — `shep dev`'s own bug (Phase 15 review, Important 2). The
    /// signal path is what this pins: `ctx.shutdown()` ends `run` the exact
    /// way a caught `SIGTERM` does, WITHOUT going through any caller-level
    /// `Stop`/`Delete` request first, which is precisely the gap a CLI-side
    /// `tidy_up` flag alone cannot close. If `delete_flock_on_shutdown` is
    /// ever ignored — or is threaded through as `false` by mistake — this
    /// app survives into the roll exactly as
    /// `boot_restores_a_saved_flock_and_tears_down_in_order` above expects
    /// for the ordinary (non-`dev`) case, and this assertion catches it.
    #[tokio::test]
    async fn delete_flock_on_shutdown_clears_the_roll_even_on_a_signalled_exit() {
        let _guard = SIGNAL_TEST_LOCK.lock().await;
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();

        let daemon = boot(
            ScriptedRunner::new(vec![ProcScript::never_exits()]),
            paths.clone(),
            BootOptions {
                delete_flock_on_shutdown: true,
                ..BootOptions::default()
            },
        )
        .await
        .unwrap();
        let ctx = daemon.context();
        let held = shep_core::config::normalize(AppConfig::minimal("held", "./held")).unwrap();
        ctx.registry.record(std::slice::from_ref(&held));
        ctx.supervisor.start(vec![held]).await.unwrap();
        let flock = ctx.supervisor.list_checked().await.unwrap();
        assert_eq!(flock.len(), 1, "the held app must actually be up");

        let run = tokio::spawn(daemon.run());
        // The signal path, not a caller-level `Stop`/`Delete` pair: `run`'s
        // own `install_signals` handler flips this same watch on a real
        // `SIGTERM`, and never runs anything this test hasn't run either.
        ctx.shutdown();
        tokio::time::timeout(Duration::from_secs(5), run)
            .await
            .unwrap()
            .unwrap()
            .unwrap();

        let final_roll = crate::snapshot::read(&paths.snapshot).unwrap();
        assert!(
            final_roll.apps.is_empty(),
            "delete_flock_on_shutdown must leave the roll empty, not {:?}",
            final_roll.apps
        );
    }

    /// fails if the dogs come up before the muster restore, or not at all.
    /// The order half is the point: a metrics dog that starts first answers
    /// for an empty flock for the whole restore window, and a bark dog
    /// raises a start alert for every sheep the roll brought back. The
    /// assertion reads the ORDER the scripted runner was asked to spawn in
    /// — [`ScriptedRunner`] hands out pids as `FIRST_SCRIPTED_PID + index`,
    /// so a lower pid is an earlier spawn — which is the only place the
    /// sequence is observable.
    #[tokio::test]
    async fn boot_restores_the_flock_before_it_lets_the_dogs_out() {
        let _guard = SIGNAL_TEST_LOCK.lock().await;
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();
        let roll = FlockSnapshot {
            version: SNAPSHOT_VERSION,
            saved_at_ms: 0,
            apps: vec![SavedApp {
                app: AppConfig::minimal("web", "./srv"),
                instances_running: 1,
            }],
        };
        crate::snapshot::write_atomic(&paths.snapshot, &roll).unwrap();

        let daemon = boot(
            // Two scripts: the restored sheep's spawn, then the dog's.
            ScriptedRunner::new(vec![ProcScript::never_exits(), ProcScript::never_exits()]),
            paths.clone(),
            BootOptions {
                restore: true,
                dogs: vec![DogSpec {
                    name: "metrics".to_string(),
                    source: DogSource::BuiltIn,
                }],
                ..BootOptions::default()
            },
        )
        .await
        .unwrap();

        let ctx = daemon.context();
        let flock = ctx.supervisor.list_checked().await.unwrap();
        assert_eq!(flock.len(), 2, "the sheep and the dog must both be up");

        let sheep = flock
            .iter()
            .find(|p| p.name == "web")
            .expect("the restored sheep must be present");
        let dog = flock
            .iter()
            .find(|p| p.name == "metrics")
            .expect("the dog must be present");
        assert!(
            sheep.dog.is_none(),
            "the restored app must carry no dog marker"
        );
        assert_eq!(
            dog.dog,
            Some(DogSource::BuiltIn),
            "the dog entry must carry its source"
        );
        assert!(
            sheep.pid < dog.pid,
            "the sheep must be spawned before the dog: sheep={:?} dog={:?}",
            sheep.pid,
            dog.pid
        );

        drop(daemon); // no run() needed; SignalTasks::drop stops the listeners
    }

    /// fails if a dog that will not start takes the boot down with it. The
    /// dog is given no script to spawn against — `ScriptedRunner` answers
    /// `SpawnFailed("script exhausted")` for the first unscripted spawn — so
    /// the flock must still come up, and the daemon must still serve.
    ///
    /// NOT `#[tokio::test]`: `capture_logs` needs a synchronous closure to
    /// scope its subscriber to (see its own doc), so this drives `boot`
    /// through a `block_on` of its own inside that closure instead —
    /// `two_concurrent_boots_on_a_stale_socket_exactly_one_wins`, above,
    /// establishes the same `new_current_thread` pattern for the same
    /// reason.
    #[test]
    fn a_dog_that_will_not_start_does_not_fail_the_boot() {
        let _guard = SIGNAL_TEST_LOCK.blocking_lock();
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();

        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();

        let mut boot_result = None;
        let logs = capture_logs(|| {
            boot_result = Some(rt.block_on(boot(
                // No scripts queued: the dog's spawn is the first (and
                // only) one attempted, and finds nothing to pop.
                ScriptedRunner::new(vec![]),
                paths.clone(),
                BootOptions {
                    dogs: vec![DogSpec {
                        name: "metrics".to_string(),
                        source: DogSource::BuiltIn,
                    }],
                    ..BootOptions::default()
                },
            )));
        });
        let daemon = boot_result
            .unwrap()
            .expect("a dog that will not start must not fail the boot");

        let flock = rt
            .block_on(daemon.context().supervisor.list_checked())
            .unwrap();
        let dog = flock
            .iter()
            .find(|p| p.name == "metrics")
            .expect("the dog's entry must still be registered");
        assert_eq!(
            dog.status,
            ProcStatus::Errored,
            "a dog that could not spawn is errored, not silently absent"
        );
        assert!(
            logs.contains("metrics"),
            "the warning must name the dog that did not start: {logs:?}"
        );
        assert!(
            logs.contains("WARN"),
            "a dog failing to start is a warning, not silence: {logs:?}"
        );

        drop(daemon); // no run() needed; SignalTasks::drop stops the listeners
    }

    /// fails if this boot path answers a name collision the way
    /// `Request::EnableDog`'s own handler refuses to. `start_dog` is
    /// idempotent by NAME: enabling a dog under a name a sheep already
    /// holds comes back `Ok` over the SHEEP, not a started dog, and the RPC
    /// arm inspects that reply for the missing `dog` marker so it can
    /// refuse rather than report a success that never happened. Task 6
    /// flagged this exact gap on the boot path when it built the guard into
    /// the RPC arm alone — this pins that `spawn_enabled_dogs` closes it
    /// too, rather than logging the sheep's own info as though a dog had
    /// started.
    ///
    /// `#[test]` + `capture_logs`, not `#[tokio::test]`, for the same
    /// reason as `a_dog_that_will_not_start_does_not_fail_the_boot` above.
    #[test]
    fn a_dog_enabled_under_a_sheeps_name_does_not_start_and_does_not_fail_the_boot() {
        let _guard = SIGNAL_TEST_LOCK.blocking_lock();
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();
        let roll = FlockSnapshot {
            version: SNAPSHOT_VERSION,
            saved_at_ms: 0,
            apps: vec![SavedApp {
                app: AppConfig::minimal("metrics", "./srv"),
                instances_running: 1,
            }],
        };
        crate::snapshot::write_atomic(&paths.snapshot, &roll).unwrap();

        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();

        let mut boot_result = None;
        let logs = capture_logs(|| {
            boot_result = Some(rt.block_on(boot(
                // One script: the restored sheep's own spawn. `start_dog`
                // finds the name already registered and returns early
                // without ever touching the runner, so a second script
                // here would go unconsumed if that held.
                ScriptedRunner::new(vec![ProcScript::never_exits()]),
                paths.clone(),
                BootOptions {
                    restore: true,
                    dogs: vec![DogSpec {
                        name: "metrics".to_string(),
                        source: DogSource::BuiltIn,
                    }],
                    ..BootOptions::default()
                },
            )));
        });
        let daemon = boot_result
            .unwrap()
            .expect("a name collision must not fail the boot");

        let flock = rt
            .block_on(daemon.context().supervisor.list_checked())
            .unwrap();
        assert_eq!(
            flock.len(),
            1,
            "the collision must not register a second entry: {flock:?}"
        );
        assert!(
            flock[0].dog.is_none(),
            "the sheep must not be relabeled as a dog by a same-named enable: {:?}",
            flock[0]
        );
        assert!(
            logs.contains("metrics"),
            "the warning must name the collision: {logs:?}"
        );

        drop(daemon); // no run() needed; SignalTasks::drop stops the listeners
    }

    /// fails if the notification is sent before the muster restore finishes.
    /// That ordering is the entire reason `Type=notify` was chosen over
    /// `Type=simple`: a unit that goes green at exec time reports a flock
    /// that is not up yet, and a restore that hangs reads as a healthy
    /// service supervising nothing.
    ///
    /// **How it tells the two orders apart**, since a test that only checks
    /// the notification arrived would prove nothing about when: the restore
    /// announces its own spawn on the SAME socket, so what is asserted is
    /// the queue order of two datagrams rather than the presence of one.
    /// Reading only `READY=1` after `boot` returns would pass on a notify
    /// moved to the very top of `boot`, because the kernel keeps that
    /// datagram queued however early it was sent — confirmed by moving the
    /// send above step 4 and watching this case fail on the first `recv`.
    /// The flock is asserted afterwards too, so a `SPAWNED` from something
    /// other than a completed restore could not carry the case.
    #[tokio::test]
    async fn readiness_is_reported_only_once_the_roll_is_restored() {
        // Real time: binds a real socket, so this obeys SIGNAL_TEST_LOCK's
        // rule like every other successful `boot()` in this module.
        let _guard = SIGNAL_TEST_LOCK.lock().await;
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();
        crate::snapshot::write_atomic(
            &paths.snapshot,
            &FlockSnapshot {
                version: SNAPSHOT_VERSION,
                saved_at_ms: 0,
                apps: vec![SavedApp {
                    app: AppConfig::minimal("web", "./srv"),
                    instances_running: 1,
                }],
            },
        )
        .unwrap();

        // Inside the TempDir and short: macOS caps a unix socket path near
        // 97 characters, which `test_paths` already keeps this under.
        let notify_path = dir.path().join("n.sock");
        let listener = std::os::unix::net::UnixDatagram::bind(&notify_path).unwrap();
        // Bounded: a datagram that never arrives must fail this case, not
        // park it.
        listener
            .set_read_timeout(Some(Duration::from_secs(5)))
            .unwrap();

        // Both events land on the SAME socket, so the assertion is on their
        // ORDER rather than on their presence. This is the whole design of
        // the case: reading only READY=1 after `boot` returns would pass on
        // a notify moved to the TOP of `boot`, because the datagram is
        // queued by the kernel and is still there whenever the test looks.
        // A marker sent from inside the restore's own spawn is the only
        // thing that distinguishes the two orders. AF_UNIX SOCK_DGRAM
        // enqueues synchronously, and these two sends are strictly
        // sequential in program order, so the queue order is the program
        // order.
        let runner = AnnouncingRunner::new(
            ScriptedRunner::new(vec![ProcScript::never_exits()]),
            &notify_path,
        );

        let daemon = boot(
            runner,
            paths.clone(),
            BootOptions {
                restore: true,
                notify_socket: Some(notify_path.clone().into_os_string()),
                ..BootOptions::default()
            },
        )
        .await
        .unwrap();

        let mut buf = [0u8; 64];
        let read = listener.recv(&mut buf).unwrap();
        assert_eq!(
            &buf[..read],
            b"SPAWNED\n",
            "READY=1 arrived before the roll was restored: a unit that goes \
             green at exec time reports a flock that is not up yet, and a \
             restore that hangs reads as a healthy service supervising nothing"
        );
        let read = listener.recv(&mut buf).unwrap();
        assert_eq!(&buf[..read], b"READY=1\n");

        let ctx = daemon.context();
        let flock = ctx.supervisor.list_checked().await.unwrap();
        assert_eq!(flock.len(), 1, "the roll was actually restored");
        assert_eq!(flock[0].name, "web");

        ctx.shutdown();
        daemon.run().await.unwrap();
    }

    /// fails if an undeliverable readiness datagram takes the daemon down
    /// with it. Nothing is bound at the address, so the send errors — and
    /// the boot must still succeed, because what failed is the init
    /// system's *knowledge* of a daemon that is otherwise fully up. Systemd
    /// reports that honestly through its own `TimeoutStartSec`; killing a
    /// working supervisor over one datagram would be the worse outcome, and
    /// on a `?` here a reboot would leave the flock down instead of merely
    /// unannounced.
    #[tokio::test]
    async fn a_readiness_datagram_that_cannot_be_delivered_does_not_fail_the_boot() {
        let _guard = SIGNAL_TEST_LOCK.lock().await;
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);

        let daemon = boot(
            ScriptedRunner::new(vec![]),
            paths.clone(),
            BootOptions {
                // Bound by nothing, and never created by anything: the send
                // is an error, which is the whole premise of the case.
                notify_socket: Some(dir.path().join("nobody.sock").into_os_string()),
                ..BootOptions::default()
            },
        )
        .await
        .expect("a daemon nobody could be told about is still a daemon");

        // Up enough to serve, not merely constructed.
        assert!(daemon.context().supervisor.list_checked().await.is_ok());

        daemon.context().shutdown();
        daemon.run().await.unwrap();
    }

    #[tokio::test]
    async fn sigterm_triggers_the_same_graceful_shutdown() {
        // Real time + a real signal. Safe to raise here only because the
        // handler is installed first: SIGTERM's default action would kill
        // the test binary. tokio never uninstalls it, which is harmless.
        // Locked against `boot_restores_a_saved_flock_and_tears_down_in_order`
        // — see SIGNAL_TEST_LOCK's own doc: this raise() is process-wide and
        // would otherwise be observed by that test's daemon too.
        let _guard = SIGNAL_TEST_LOCK.lock().await;
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        init_dirs(&paths).unwrap();
        let daemon = boot(
            ScriptedRunner::new(vec![]),
            paths.clone(),
            BootOptions::default(),
        )
        .await
        .unwrap();
        // No sleep needed here (there was one; see git history): signal
        // handlers are installed inside `boot`, which this call already
        // `.await`ed to completion, so they are provably live the moment
        // `boot` returns — well before `run()`'s own task is ever polled.
        let run = tokio::spawn(daemon.run());
        nix::sys::signal::raise(nix::sys::signal::Signal::SIGTERM).unwrap();
        tokio::time::timeout(Duration::from_secs(5), run)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
        assert!(!paths.socket.exists());
    }

    #[tokio::test]
    async fn a_repeat_sigterm_is_observed_not_swallowed() {
        // Pins Decision 3 (2026-08-08): each shutdown-signal listener
        // `install_signals` spawns stays armed for the rest of the
        // process's life instead of returning after one `recv()`. Tests
        // `install_signals` directly rather than the whole `boot()`/`run()`
        // teardown — the bug and its fix live entirely in this one loop,
        // and driving a genuinely slow teardown here would only add
        // unrelated timing noise to what is otherwise a fully
        // deterministic check.
        //
        // Real time + real signals — see SIGNAL_TEST_LOCK's own doc: this
        // raise() is process-wide.
        let _guard = SIGNAL_TEST_LOCK.lock().await;
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (shutdown, mut shutdown_rx) = watch::channel(false);
        let shutdown = Arc::new(shutdown);
        // The SIGUSR2 half of the return is dropped unused: this case drives
        // the shutdown listeners only, and a dropped sender simply ends the
        // SIGUSR2 listener that is parked on its receiver (see
        // `install_signals`'s own doc) without disturbing the three below.
        let (signals, _connect_supervisor) = install_signals(shutdown, paths).unwrap();

        // First SIGTERM: starts shutdown, exactly as before this decision.
        nix::sys::signal::raise(nix::sys::signal::Signal::SIGTERM).unwrap();
        tokio::time::timeout(Duration::from_secs(5), shutdown_rx.changed())
            .await
            .unwrap()
            .unwrap();
        assert!(*shutdown_rx.borrow());

        // The regression this test pins: on the pre-fix code, the SIGTERM
        // listener task RETURNED after that one signal (see
        // `install_signals`'s own doc for the full history) — `is_finished`
        // would already read `true` here. Looped, it never finishes on its
        // own; only `SignalTasks::drop`'s `abort()` stops it.
        assert!(
            !signals.tasks[0].is_finished(),
            "the SIGTERM listener must still be polling after its first signal, not have exited"
        );

        // A second SIGTERM, into the same "already shutting down" state a
        // real slow teardown would still be in when a repeat arrives (the
        // watch channel is already `true`, exactly as `run()` would leave
        // it while its own teardown steps run). The pre-fix version would
        // have dropped this on the floor: no live task left to receive it,
        // and no process kill either, since installing this handler already
        // replaced SIGTERM's default terminate disposition.
        // `watch::Sender::send` marks its channel changed on every call
        // regardless of whether the value differs (confirmed against
        // tokio's own source, matching the precedent `RunningDaemon::
        // shutdown_rx`'s own doc already relies on for a different
        // scenario) — so a SECOND `changed()` resolving here, for a value
        // that was already `true`, is airtight proof the loop delivered
        // this signal all the way through to another `shutdown.send()`
        // call, not a timing coincidence.
        nix::sys::signal::raise(nix::sys::signal::Signal::SIGTERM).unwrap();
        tokio::time::timeout(Duration::from_secs(5), shutdown_rx.changed())
            .await
            .unwrap()
            .unwrap();
        assert!(
            !signals.tasks[0].is_finished(),
            "the SIGTERM listener must still be armed after a SECOND signal too"
        );

        drop(signals); // aborts the listener tasks (SignalTasks::drop)
    }

    // `boot` is the ONE place `DEFAULT_MAX_CRON_SLEEP` is applied — the CLI
    // half of the plumbing keeps the knob an `Option` all the way down, and
    // its own test pins that — so nothing else in this workspace would notice
    // a different fallback landing here. fails if the default is replaced (a
    // stray `Duration::from_secs(1)` would have every cron worker in the
    // daemon waking a minute more often than the constant says), and fails if
    // the configured value is dropped for one of `max_cron_sleep`'s own
    // invention.
    //
    // Whole `BootOptions` values rather than bare `Option`s, because that is
    // what `boot` hands over: the field this reads is the field the daemon
    // runs with, with no projection in between for a call site to get wrong.
    #[test]
    fn an_unset_max_cron_sleep_falls_back_to_the_daemons_own_default() {
        assert_eq!(
            max_cron_sleep(&BootOptions::default()),
            DEFAULT_MAX_CRON_SLEEP,
            "unset means the default"
        );
        assert_eq!(
            max_cron_sleep(&BootOptions {
                max_cron_sleep: Some(Duration::from_secs(300)),
                ..BootOptions::default()
            }),
            Duration::from_secs(300),
            "a configured value must reach the workers unchanged"
        );
    }

    // fails if `boot` never spawns the extras reporter. Nothing else in this
    // crate drives that call — every other reporter case constructs one by
    // hand — so dropping it here would leave a real daemon in which no memory
    // breach and no liveness failure ever restarts anything, with every unit
    // test still green.
    //
    // The whole production chain is what makes the claim: the actor arms a
    // liveness loop at the Online transition, the loop reports over the sender
    // `Extras::real` was built with, the reporter reads it, and
    // `extra_restart`'s guards let it through. Real time and a real
    // `OsProber` — a paused clock does not move a real TCP connect.
    #[tokio::test]
    async fn a_booted_daemon_restarts_a_sheep_whose_liveness_probe_fails() {
        // Real time: binds a real socket, so it takes the signal lock like
        // every other successful `boot()` here — see SIGNAL_TEST_LOCK's doc.
        let _guard = SIGNAL_TEST_LOCK.lock().await;
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);

        // Reserve a port, then release it: nothing ever listens there, so
        // every probe fails with a connection refusal, with no listener to
        // race and no port to reserve for real.
        let reserved = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = reserved.local_addr().unwrap();
        drop(reserved);

        let daemon = boot(
            ScriptedRunner::new(vec![ProcScript::never_exits(); 4]),
            paths.clone(),
            BootOptions::default(),
        )
        .await
        .unwrap();
        let ctx = daemon.context();
        let mut events = ctx.events.subscribe();
        let run = tokio::spawn(daemon.run());

        let mut app = AppConfig::minimal("web", "./srv");
        app.liveness_probe = Some(ProbeConfig {
            kind: ProbeKind::Tcp,
            target: addr.to_string(),
            // The loop floors anything shorter at one second, so a smaller
            // number here would be a lie about what this test waits for.
            interval: UpDuration::from_millis(1_000),
            timeout: UpDuration::from_millis(500),
            failure_threshold: 1,
        });
        ctx.supervisor
            .start(vec![normalize(app).unwrap()])
            .await
            .unwrap();

        let restarted = async {
            loop {
                match events.recv().await {
                    Ok(BusEvent::Process {
                        event: ProcessEventKind::Restart,
                        info,
                        ..
                    }) => return info,
                    Ok(_) => continue,
                    Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
                    Err(err) => panic!("the event stream closed before a restart: {err}"),
                }
            }
        };
        let info = tokio::time::timeout(Duration::from_secs(20), restarted)
            .await
            .expect("a failing liveness probe must restart its sheep");
        assert_eq!(info.id, 0);
        assert_eq!(info.restarts, 1);

        ctx.shutdown();
        tokio::time::timeout(Duration::from_secs(10), run)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }

    // fails if SIGUSR2 stops meaning `reopen all`: a listener that logs the
    // signal and reaches no pump, one wired to a narrower selector, or a
    // `boot` that never hands the listener the supervisor it reopens through
    // (the step-1/step-4 seam). Nothing else in this workspace drives that
    // seam — `shep reopen` reaches the same supervisor over the socket
    // instead, so every RPC-tier reopen test stays green with the signal path
    // dead.
    //
    // Both instances are asserted, not just one: `All` is the whole claim,
    // and a listener that reopened the first sheep it found would satisfy
    // half of it.
    #[tokio::test]
    async fn sigusr2_reopens_every_sheeps_log_files() {
        // Real time + a real signal, so it takes the lock like every other
        // successful `boot()` here — see SIGNAL_TEST_LOCK's own doc: this
        // raise() is process-wide. Raising SIGUSR2 is safe only because
        // `boot` below has already returned, having replaced a default
        // disposition that would otherwise kill the test binary outright.
        let _guard = SIGNAL_TEST_LOCK.lock().await;
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);

        // TWO scripts for two instances, counted against what a BROKEN
        // implementation demands rather than a working one: `ScriptedRunner`
        // answers `SpawnFailed("script exhausted")` once it runs out, which
        // lands that sheep `Errored` with no log pump at all — a state this
        // case could not tell apart from the pump nobody asked to reopen.
        // The `log_ctl_live` assertions below are the other half of that
        // guard: they say a pump is there to be reached before the signal is
        // ever raised.
        let runner = Arc::new(ScriptedRunner::new(vec![ProcScript::never_exits(); 2]));
        let daemon = boot(
            SharedRunner(Arc::clone(&runner)),
            paths.clone(),
            BootOptions::default(),
        )
        .await
        .unwrap();
        let ctx = daemon.context();
        let run = tokio::spawn(daemon.run());

        let mut web = AppConfig::minimal("web", "./srv");
        web.instances = 2;
        ctx.supervisor
            .start(vec![normalize(web).unwrap()])
            .await
            .unwrap();
        for instance in 0..2 {
            assert!(
                runner.log_ctl_live(instance),
                "instance {instance} must have a live log pump before the signal, or this \
                 case proves nothing"
            );
            assert_eq!(
                runner.reopens(instance),
                0,
                "instance {instance} must not have been reopened before the signal"
            );
        }

        nix::sys::signal::raise(nix::sys::signal::Signal::SIGUSR2).unwrap();

        // Polled rather than awaited: a signal has no reply channel, so
        // there is nothing to await on — the counters are the only place the
        // reopen becomes visible. Bounded, so a listener that never reaches a
        // pump fails here instead of hanging.
        let both_reopened = async {
            while runner.reopens(0) == 0 || runner.reopens(1) == 0 {
                tokio::time::sleep(Duration::from_millis(10)).await;
            }
        };
        tokio::time::timeout(Duration::from_secs(10), both_reopened)
            .await
            .expect("SIGUSR2 must reopen every sheep's log files");

        ctx.shutdown();
        tokio::time::timeout(Duration::from_secs(10), run)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
    }
}