shep-daemon 0.2.2

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
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
//! Lifecycle extras: what is armed when a sheep goes online, and what stops
//! when it goes terminal (spec §4, §7).
//!
//! Four subsystems run as free tasks beside the supervisor actor — the cron
//! worker ([`crate::cron`]), the memory-limit enforcer ([`crate::limits`]),
//! the liveness prober ([`crate::probes`]) and the filesystem watch
//! ([`crate::watch`]). [`ExtrasRegistry`] is what starts them and what stops
//! them, keyed the way each subsystem's own reach demands: cron and watch per
//! **name**, because both restart a whole name-group and would otherwise fire
//! N times for N instances; the enforcer and the liveness loop per **id**,
//! because each is armed against one pid.
//!
//! Disarming is the whole of what keeps a stopped sheep down. Neither a
//! triggering file save nor a cron occurrence filters by status, so a sheep
//! stays down because nothing is left armed for it — never because a trigger
//! declined to fire. The user-visible rule is one line: stopping a **name**
//! stops its watch. Not one instance of a name: `disarm` tears a group down
//! only when its LAST member leaves, so `shep stop web-1` with `web-2` still
//! up leaves the group's one watcher armed and the next save brings `web-1`
//! back. [`crate::watch`]'s module doc carries the full case for why that is
//! the accepted consequence of one watcher per name group rather than a gap.
//!
//! ## Reference
//!
//! - [`Extras`], [`ExtrasReports`] — the seams and the report wiring
//! - [`ExtrasRegistry`] — arming and disarming
//! - [`spawn_extras_reporter`] — turns a report into a guarded restart

use core::fmt;
use core::time::Duration;

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use tokio::sync::mpsc;
use tokio::task::JoinHandle;

use shep_core::config::{AppConfig, CronSchedule, ProbeTarget};
use shep_core::values::UpDuration;

use crate::cron::{Clock, SystemClock, spawn_cron_worker};
use crate::entry::ProcessEntry;
use crate::limits::sample::{MemorySampler, SysinfoSampler};
use crate::limits::stats::StatsState;
use crate::limits::{LimitBreach, LimitEnforcer, PollingEnforcer};
use crate::probes::{LivenessFailure, Prober, spawn_liveness_task};
use crate::supervisor::SupervisorHandle;
use crate::watch::{
    DEFAULT_WATCH_DELAY, MIN_WATCH_DELAY, WatchFilter, own_log_ignores, spawn_watch_group,
};

/// A [`LivenessFailure`] paired with the epoch its probe was armed under.
///
/// `InstanceExtras::disarm` calls `liveness.abort()`, which does not await
/// the aborted task: a probe already inside `failures.send(..).await` when
/// it is replaced (a config-only re-arm, which changes neither the pid nor
/// the status the two older guards check) can still deliver its failure
/// after a fresh probe is already running against the same process. This
/// carries the epoch [`ExtrasRegistry::arm`] captured when THIS probe was
/// spawned, so `Actor::handle_extra_restart` can tell that stale failure
/// apart from a genuine one raised by the CURRENT probe, even though pid and
/// status agree on both.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct LivenessReport {
    /// The sheep's id.
    pub id: u32,
    /// The pid this loop was armed against.
    pub pid: u32,
    /// The epoch the reporting probe was armed under.
    pub epoch: u64,
}

/// Where the lifecycle extras send the two out-of-band failure reports.
///
/// The matching receivers belong to the reporting task, never to the actor.
#[derive(Debug, Clone)]
pub struct ExtrasReports {
    /// Memory-limit breaches, from the enforcer.
    pub breaches: mpsc::Sender<LimitBreach>,
    /// Sheep whose liveness probe hit `failure_threshold`.
    pub liveness: mpsc::Sender<LivenessReport>,
}

/// The four lifecycle extras, the seams they run on, and where their two
/// failure reports go.
///
/// Constructed once at boot and handed to the supervisor. Every *seam* is a
/// trait object so the engine's type does not grow a parameter per subsystem;
/// `reports` and `stats` are the exceptions and are wiring rather than seams
/// — the sampler underneath `stats` is where that subsystem's seam lives.
pub struct Extras {
    /// Wall clock the cron workers read.
    pub clock: Arc<dyn Clock>,
    /// Memory-limit mechanism.
    ///
    /// Shared rather than owned (`Arc`, not `Box`) because
    /// [`ExtrasRegistry::disarm`] has to reach it too, and its signature
    /// takes no [`Extras`]: the registry keeps a handle from the arming that
    /// needs undoing, so arming and disarming an id's memory limit stay in
    /// the one type that owns every other arm/disarm pair.
    pub enforcer: Arc<dyn LimitEnforcer>,
    /// Longest a cron worker parks before re-reading the clock, from
    /// `[daemon] max_cron_sleep`. Already defaulted: this is a value, not an
    /// option, because the layer that knew whether the user set anything is
    /// behind us.
    pub max_cron_sleep: Duration,
    /// Cloned once per arming. The enforcer swallowed its own breach sender at
    /// construction; the liveness loops are free tasks and cannot, so the
    /// sender has to reach [`ExtrasRegistry::arm`] through here.
    pub reports: ExtrasReports,
    /// Live resource readings, shared with the RPC layer so a listing can
    /// take one on demand.
    ///
    /// Shared rather than owned for the same reason [`Extras::enforcer`] is:
    /// [`ExtrasRegistry`] keeps a handle from the arming it will later have
    /// to undo. The enforcer's polling loop holds a third handle — it is what
    /// records the CPU baseline every on-demand reading measures against.
    pub stats: Arc<StatsState>,
}

impl Extras {
    /// The production wiring: system clock and polling enforcer over
    /// sysinfo.
    ///
    /// No prober: one is scoped to a single sheep's assembled environment,
    /// and boot has no sheep in scope.
    ///
    /// Must be called from within a Tokio runtime context: constructing the
    /// polling enforcer starts its sampling task immediately.
    #[must_use]
    pub fn real(reports: ExtrasReports, max_cron_sleep: Duration) -> Self {
        // One sampler behind both consumers, not two: sampling and
        // enforcement read the same process table on the same tick, and a
        // second `SysinfoSampler` would mean a second syscall walk.
        let sampler: Arc<dyn MemorySampler> = Arc::new(SysinfoSampler::new());
        let stats = Arc::new(StatsState::new(Arc::clone(&sampler)));
        let enforcer =
            PollingEnforcer::start(sampler, reports.breaches.clone(), Arc::clone(&stats));
        Self {
            clock: Arc::new(SystemClock),
            enforcer: Arc::new(enforcer),
            max_cron_sleep,
            reports,
            stats,
        }
    }
}

impl fmt::Debug for Extras {
    // Roles, not values, for the seams: neither is Debug, and printing the
    // report channels would say nothing a reader wants. `stats` is omitted
    // for the same reason as `reports` — it is wiring, and its own Debug
    // prints a role and nothing else. The sleep bound is the exception and
    // prints for real: it is a tuning knob the user set, so a daemon log that
    // dumps this struct should say what it ended up being.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Extras")
            .field("clock", &"<dyn Clock>")
            .field("enforcer", &"<dyn LimitEnforcer>")
            .field("max_cron_sleep", &self.max_cron_sleep)
            .finish_non_exhaustive()
    }
}

/// Restarts each sheep reported over `breaches` or `liveness`.
///
/// Ends when both senders have dropped. Owns both receivers: the actor must
/// never block on anything a subsystem controls.
///
/// Restarts go through [`SupervisorHandle::extra_restart`], never `restart`: a
/// report already queued when the user runs `shep stop` is delivered *after*
/// the sheep is `Stopped`, and `restart` would resurrect a process the user
/// explicitly stopped.
///
/// Both arms write a `warn!` on the way past, and for a breach that record is
/// the ONLY place the observed RSS and the limit it crossed are ever stated —
/// the bus event says `restart` and never why. Both reach a reader: the
/// binary installs a subscriber whose default level is `warn`, so a user
/// watching a process get restarted over and over finds the reason in
/// `$SHEP_HOME/logs/shepd.err.log`. `[daemon] log_level = "error"` is what
/// takes that away again.
///
/// Must be called from within a Tokio runtime context: it spawns the
/// reporting task immediately.
pub fn spawn_extras_reporter(
    mut breaches: mpsc::Receiver<LimitBreach>,
    mut liveness: mpsc::Receiver<LivenessReport>,
    supervisor: SupervisorHandle,
) -> JoinHandle<()> {
    tokio::spawn(async move {
        // Per-channel open flags, and a `select!` branch guard on each: a
        // closed `mpsc::Receiver` resolves to `None` on every subsequent
        // poll, so a branch left in consideration after its channel ended
        // would busy-spin the loop instead of falling out of it. Same shape,
        // and same reason, as `run_sheep`'s channel guards.
        let mut breaches_open = true;
        let mut liveness_open = true;
        while breaches_open || liveness_open {
            tokio::select! {
                maybe_breach = breaches.recv(), if breaches_open => match maybe_breach {
                    Some(breach) => {
                        tracing::warn!(
                            id = breach.id,
                            pid = breach.root_pid,
                            observed = %breach.observed,
                            limit = %breach.limit,
                            "process tree exceeded its max_memory; restarting"
                        );
                        // No epoch to carry: a memory breach has nothing
                        // equivalent to a probe task that can be aborted
                        // mid-`send`. What it carries instead is the size it
                        // measured, which is its own staleness token -- the
                        // breach was computed under `PollingEnforcer`'s lock
                        // and is sent after that lock is released, so a
                        // ceiling re-armed in between leaves this report
                        // speaking for a limit nobody enforces any more. The
                        // actor re-asks the question against the ceiling in
                        // force when it arrives.
                        supervisor
                            .extra_restart(
                                breach.id,
                                breach.root_pid,
                                None,
                                Some(breach.observed),
                            )
                            .await;
                    }
                    None => breaches_open = false,
                },
                maybe_failure = liveness.recv(), if liveness_open => match maybe_failure {
                    Some(report) => {
                        tracing::warn!(
                            id = report.id,
                            pid = report.pid,
                            "liveness probe hit its failure_threshold; restarting"
                        );
                        supervisor
                            .extra_restart(report.id, report.pid, Some(report.epoch), None)
                            .await;
                    }
                    None => liveness_open = false,
                },
            }
        }
    })
}

/// Per-sheep and per-group task handles, armed on `online` and aborted on the
/// way out.
#[derive(Debug, Default)]
pub struct ExtrasRegistry {
    /// One name-group's per-name tasks. Present only while at least one
    /// instance whose *configuration* asks for a per-name extra is armed, so
    /// a flock of apps configuring neither `cron_restart` nor `watch` leaves
    /// this map empty. Keyed on the configuration rather than on what an
    /// arming managed to build — see [`ExtrasRegistry::arm`].
    groups: HashMap<String, NameExtras>,
    /// One instance's per-pid extras, keyed by sheep id. Present only while
    /// at least one of them is armed.
    instances: HashMap<u32, InstanceExtras>,
    /// The epoch each id's liveness probe is CURRENTLY armed under, bumped
    /// every time [`Self::arm`] (re)arms an id, whether or not that id
    /// configures a `liveness_probe` at all, so an app that adds one later
    /// never inherits a stale count from before it did.
    ///
    /// Deliberately not the supervisor's own private `SheepSlot::epoch`: that
    /// one is bumped on a RESPAWN, when the pid changes, which is exactly
    /// the case `Actor::handle_extra_restart`'s existing pid guard already
    /// catches. This counter answers a narrower question a pid check
    /// cannot: has THIS id's liveness probe been replaced without the
    /// process underneath it changing at all, the case a config-only re-arm
    /// produces. Overloading the respawn epoch for that would either bump it
    /// on every config change too (a respawn-generation counter moving
    /// without a respawn) or leave a config-only re-arm unable to move it at
    /// all, so this is a second counter rather than a second use of the
    /// first.
    ///
    /// Lives on the registry, not on `SheepSlot`, because that is the one
    /// type that already knows when a liveness probe is actually replaced:
    /// `SheepSlot` has no visibility into an arming `ExtrasRegistry::arm`
    /// performs, and `rearm_name` (this registry's own config-only re-arm)
    /// has no visibility into `SheepSlot` the other way. Keeping the counter
    /// where the arming decision is made avoids two structs that would
    /// otherwise need to agree on it.
    liveness_epochs: HashMap<u32, u64>,
}

/// One name-group's per-name tasks, plus the armed instances keeping them
/// alive.
///
/// Either task may be `None` while the group still exists: an app that
/// configures a watch but whose watch could not be registered is a member of
/// its group all the same, and the next arming of the name retries it.
#[derive(Debug, Default)]
struct NameExtras {
    /// The group's cron worker, when the app configures `cron_restart`.
    cron: Option<JoinHandle<()>>,
    /// The group's filesystem watch, when the app configures `watch`.
    watch: Option<JoinHandle<()>>,
    /// Ids of this name's instances that currently have anything armed. The
    /// last one leaving is what tears the two tasks above down.
    members: HashSet<u32>,
}

impl NameExtras {
    /// Aborts both per-name tasks. Takes `self`: a group is torn down once
    /// and then gone, so there is no half-aborted state to represent.
    fn abort(self) {
        if let Some(cron) = self.cron {
            cron.abort();
        }
        if let Some(watch) = self.watch {
            watch.abort();
        }
    }
}

/// One instance's per-pid extras.
struct InstanceExtras {
    /// Where this id's sampling was started, so `disarm` can stop it. Not an
    /// `Option`: every sheep with a pid is sampled, so an instance that
    /// reached this type is a watched one by construction.
    stats: Arc<StatsState>,
    /// The enforcer this id's memory limit was armed against, so `disarm` can
    /// undo that arming — see [`Extras::enforcer`] for why the registry holds
    /// a handle rather than being handed one.
    limit: Option<Arc<dyn LimitEnforcer>>,
    /// The liveness loop, when the app configures `liveness_probe`.
    liveness: Option<JoinHandle<()>>,
}

impl InstanceExtras {
    /// Sampling armed and nothing else — what an app configuring neither
    /// `max_memory` nor `liveness_probe` gets, which is most of them.
    fn watched_only(stats: Arc<StatsState>) -> Self {
        Self {
            stats,
            limit: None,
            liveness: None,
        }
    }

    /// Undoes this instance's arming: sampling, the memory limit against
    /// `id`, and the liveness loop. Takes `self` for the same reason
    /// [`NameExtras::abort`] does.
    fn disarm(self, id: u32) {
        self.stats.unwatch(id);
        if let Some(enforcer) = self.limit {
            enforcer.disarm(id);
        }
        if let Some(liveness) = self.liveness {
            liveness.abort();
        }
    }
}

impl fmt::Debug for InstanceExtras {
    // `Arc<dyn LimitEnforcer>` is not Debug — and the useful fact about it
    // here is that an arming exists, not which mechanism performed it.
    // `stats` is omitted rather than redacted: it is armed for every instance
    // that exists, so printing it would print a constant. Hence
    // `finish_non_exhaustive` — a field really is being left out.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("InstanceExtras")
            .field("limit_armed", &self.limit.is_some())
            .field("liveness", &self.liveness)
            .finish_non_exhaustive()
    }
}

impl ExtrasRegistry {
    /// Arms everything an entry's configuration asks for.
    ///
    /// `prober` is scoped to this instance's assembled `SpawnSpec` and is
    /// read only by the liveness loop; an entry configuring no
    /// `liveness_probe` never touches it.
    ///
    /// Idempotent per id: arming an already-armed id disarms that id's own
    /// per-pid extras first, which is what a respawn needs — the new process
    /// has a new pid. A *live* name-group task is deliberately left alone on a
    /// re-arm: it is keyed on the name, it outlives any one process, and
    /// rebuilding the watch on every restart would re-register the OS watcher
    /// each time — a step that can fail, and whose failure would silently cost
    /// an app the watch that restarted it.
    pub fn arm(
        &mut self,
        entry: &ProcessEntry,
        prober: Arc<dyn Prober>,
        extras: &Extras,
        supervisor: &SupervisorHandle,
    ) {
        let config = entry.spec.config();
        let id = entry.id;

        self.disarm_instance(id);
        // Bumped unconditionally, ahead of `arm_instance` even for an entry
        // configuring no `liveness_probe` at all; see
        // `Self::liveness_epochs`'s doc for why this lives here rather than
        // on `SheepSlot`. `arm_instance` only ever reads it for a probe it is
        // about to spawn, so an id that never arms a probe pays nothing for
        // an epoch nothing consults.
        let liveness_epoch = self.liveness_epochs.entry(id).or_insert(0);
        *liveness_epoch += 1;
        let liveness_epoch = *liveness_epoch;
        if let Some(instance) = arm_instance(entry, prober, extras, liveness_epoch) {
            self.instances.insert(id, instance);
        }

        // Group membership is decided by the CONFIGURATION, never by whether
        // this arming managed to build a task. An `arm_watch` that failed
        // transiently — inotify's `max_user_watches` exhausted, say — would
        // otherwise leave a still-online instance out of its own group, and
        // stopping some LATER instance whose arm did succeed would tear the
        // watch down while the earlier one is still running.
        if config.cron_restart.is_none() && !config.watch {
            return;
        }
        let group = self.groups.entry(config.name.clone()).or_default();
        // A second instance of a name joins the group rather than arming a
        // second worker, since both triggers already reach every instance of
        // the name.
        group.members.insert(id);
        // Presence in the map is NOT the test, because a task can end on its
        // own: a cron worker returns on a pattern with no further occurrence,
        // and the watch loop returns when its `WatchSource` dies. Either
        // leaves a finished handle behind, and without this the app would have
        // silently lost its schedule or its watch with nothing left to rebuild
        // it. `arm_cron`/`arm_watch` each return `None` for an app that asked
        // for nothing, so the app that configured only one of the two pays
        // nothing for the other's call.
        if group.cron.as_ref().is_none_or(JoinHandle::is_finished) {
            group.cron = arm_cron(config, extras, supervisor);
        }
        // An app whose watch can NEVER arm — a cwd that will never resolve —
        // pays a fresh `canonicalize`, a fresh globset compile and a fresh
        // `warn!` on every re-arm, rather than being attempted once and then
        // left alone. That is the price of retrying the transient failures
        // (`max_user_watches` exhausted, a cwd not yet created) that the
        // rebuild exists for, and it is bounded by `max_restarts`; permanent
        // failure gets no dedupe on purpose, because telling the two apart
        // means keeping per-name failure state that would then need its own
        // invalidation. Deliberate, not an oversight.
        if group.watch.as_ref().is_none_or(JoinHandle::is_finished) {
            group.watch = arm_watch(entry, supervisor);
        }
    }

    /// Aborts everything armed for `id`, and both of the name-group's
    /// per-name tasks — its watch and its cron worker — when this was the
    /// last armed instance of the name.
    ///
    /// This is what stops a stopped sheep from being restarted by a file save
    /// or a schedule: neither trigger filters by status, so a sheep stays down
    /// because nothing is left armed for it, not because something declined
    /// to restart it.
    ///
    /// Aborting the watch-group handle is sufficient to stop the OS watch:
    /// the debouncer guard rides inside the aborted future, so no second drop
    /// is needed and none is available.
    pub fn disarm(&mut self, id: u32, name: &str) {
        self.disarm_instance(id);
        // Not inside `disarm_instance`: `Self::arm` calls that first and
        // would otherwise reset the very counter it is about to bump,
        // pinning every id's epoch at 1 forever and defeating the whole
        // point of it. This is the FULL teardown (a sheep going terminal),
        // so nothing left in `self.sheep` will ever compare against `id`
        // again; removing it here is hygiene against an unbounded map on a
        // daemon that runs for months, not a correctness requirement.
        self.liveness_epochs.remove(&id);

        let Some(group) = self.groups.get_mut(name) else {
            return;
        };
        // Both halves matter. An id that was never a member leaves the group
        // untouched (disarming an unknown id is a no-op, not a teardown of
        // somebody else's watcher), and a group with instances left standing
        // keeps its tasks.
        if !group.members.remove(&id) || !group.members.is_empty() {
            return;
        }
        if let Some(group) = self.groups.remove(name) {
            group.abort();
        }
    }

    /// Rebuilds everything armed for `name`, replacing live tasks rather than
    /// keeping them.
    ///
    /// [`Self::arm`] deliberately preserves a running cron or watch task, so
    /// that a reload's replacement instance arming before the drainee disarms
    /// does not tear down a watcher the drainee still needs. That is right for
    /// the transition it was written for and wrong for a config change: the
    /// group-scoped fields (`watch`, `ignore_watch`, `watch_delay`,
    /// `watch_options`) and the cron ones (`cron_restart`, `cron_timezone`)
    /// are read when the task is built, so a task that survives keeps the old
    /// values for as long as it lives.
    ///
    /// Takes a slice of entries rather than one id, because the group is
    /// per-name: disarming a single instance of a multi-instance app leaves
    /// the group standing, by design.
    ///
    /// `entries` is what the caller wants ARMED, not every entry the name
    /// has. The sole production caller, [`crate::supervisor`]'s own
    /// `rearm_name`, passes only the entries that are `Online` with a live
    /// pid, and that filter is load-bearing rather than tidy: [`Self::arm`]
    /// decides group membership from the configuration rather than from
    /// whether anything is running, so an entry passed here for a stopped
    /// instance joins a group whose next cron occurrence or watch event
    /// restarts every member, and a config load would then start a process
    /// the operator had stopped, with no operator action and no report.
    ///
    /// An empty slice is correct input, not a caller mistake: it aborts the
    /// group and rebuilds nothing, which is what a name whose instances are
    /// all momentarily down needs, so that the group holding the OLD config
    /// does not outlive the load.
    ///
    /// # What this loses
    ///
    /// The OS watch is torn down and rebuilt with a real gap and no rescan, so
    /// a file saved during it is missed. Same gap any watcher restart has.
    /// `stats.watch()` clears the pid's CPU baseline, so `shep flock` shows a
    /// blank CPU cell for one poll interval. Both are documented rather than
    /// closed; see the design spec.
    /// `prober` is called once per entry rather than shared, because
    /// `assemble` bakes `SHEP_INSTANCE` and every `{{instance}}` template
    /// into the environment a prober runs with: one prober for a
    /// multi-instance app would run every instance's liveness probe against a
    /// single instance's environment.
    pub fn rearm_name(
        &mut self,
        name: &str,
        entries: &[&ProcessEntry],
        prober: impl Fn(&ProcessEntry) -> Arc<dyn Prober>,
        extras: &Extras,
        supervisor: &SupervisorHandle,
    ) {
        // Abort the group's own tasks before rebuilding, which is the whole
        // difference from `arm`. Removing the entry rather than mutating it
        // means the rebuild below takes `arm`'s own "no task yet" path, so
        // there is one construction site rather than two.
        if let Some(group) = self.groups.remove(name) {
            group.abort();
        }
        for entry in entries {
            self.arm(entry, prober(entry), extras, supervisor);
        }
    }

    /// Undoes one instance's per-pid arming: the memory limit and the
    /// liveness loop. A no-op for an id that had neither.
    fn disarm_instance(&mut self, id: u32) {
        if let Some(instance) = self.instances.remove(&id) {
            instance.disarm(id);
        }
    }

    /// The epoch `id`'s liveness probe is CURRENTLY armed under, or `0` for
    /// an id that has never been armed. `Actor::handle_extra_restart` drops a
    /// [`LivenessReport`] whose own epoch does not match this, which is what
    /// tells a failure raised by a since-replaced probe apart from one raised
    /// by the probe running now.
    pub(crate) fn liveness_epoch(&self, id: u32) -> u64 {
        self.liveness_epochs.get(&id).copied().unwrap_or(0)
    }

    /// The ids in `name`'s armed group, or `None` when nothing of that name is
    /// armed at all.
    ///
    /// Test-facing, and it exists because `groups` is private to this module
    /// while the decision about WHICH entries get armed lives in
    /// `supervisor.rs`. A stopped instance in a group is armed to be
    /// restarted by that group's cron occurrence or file save, so
    /// "who is in the group" is the exact question a case there has to ask.
    #[cfg(test)]
    pub(crate) fn group_members(&self, name: &str) -> Option<Vec<u32>> {
        self.groups.get(name).map(|group| {
            let mut members: Vec<u32> = group.members.iter().copied().collect();
            members.sort_unstable();
            members
        })
    }
}

impl Drop for ExtrasRegistry {
    // Nothing armed outlives the registry. This is the teardown rather than a
    // disarm loop in `begin_shutdown` because it covers BOTH ways the actor
    // can end, and a future transition cannot forget it:
    //
    // - A graceful shutdown only kills sheep whose `ctl.is_some()`. A
    //   `WaitingRestart` sheep has none, never exits, and so never reaches
    //   `handle_exited`'s terminal branches — its liveness loop, its enforcer
    //   arming and its name-group's cron worker and watch would all survive
    //   the actor.
    // - An actor that panics never runs `begin_shutdown` at all.
    //
    // A `JoinHandle` merely dropped DETACHES its task rather than cancelling
    // it, so every one of them has to be aborted by hand. Cron and watch do
    // eventually notice `EngineStopped` and return, but only at their next
    // iteration: the watch parks on its debounce channel holding the OS
    // watcher until some unrelated file changes, and a liveness loop keeps
    // spawning a real `sh` per interval until its threshold — and while any
    // one of them lives it holds a report sender, so the reporting task never
    // ends either.
    fn drop(&mut self) {
        for (id, instance) in self.instances.drain() {
            instance.disarm(id);
        }
        for (_name, group) in self.groups.drain() {
            group.abort();
        }
    }
}

/// Arms the per-pid extras — sampling always, the memory limit and the
/// liveness loop where the app configures them — returning `None` only when
/// the entry has no pid to arm them against.
fn arm_instance(
    entry: &ProcessEntry,
    prober: Arc<dyn Prober>,
    extras: &Extras,
    liveness_epoch: u64,
) -> Option<InstanceExtras> {
    let config = entry.spec.config();
    let wants_anything = config.max_memory.is_some() || config.liveness_probe.is_some();
    let Some(pid) = entry.pid else {
        if wants_anything {
            // Not reachable from the transition this is called at (a sheep is
            // Online only with a live pid), and not a panic either: both
            // extras are armed against a pid, so there is nothing honest to
            // arm without one.
            tracing::warn!(
                id = entry.id,
                "arming a sheep with no pid; its memory limit and liveness probe stay disarmed"
            );
        }
        return None;
    };

    // Unconditional, unlike the two below it: a listing reports CPU and
    // memory for every sheep, and an app that configures no `max_memory` is
    // the ordinary case rather than an opted-out one.
    extras.stats.watch(entry.id, pid);
    let mut instance = InstanceExtras::watched_only(Arc::clone(&extras.stats));
    if let Some(limit) = config.max_memory {
        extras.enforcer.arm(entry.id, pid, limit);
        instance.limit = Some(Arc::clone(&extras.enforcer));
    }
    if let Some(probe) = config.liveness_probe.as_ref() {
        match ProbeTarget::parse(probe) {
            Ok(target) => {
                // `spawn_liveness_task` only ever knows `LivenessFailure`;
                // it lives in `probes`, which has no business knowing about
                // an epoch this module invented. So the probe reports into a
                // private one-shot channel instead of `extras.reports`
                // directly, and this relay tags the single failure it may
                // ever produce (the probe's own doc: it reports once, then
                // ends) with the epoch THIS probe was spawned under, before
                // forwarding it on to the shared reporter. Capturing the
                // epoch here rather than reading it back off the registry at
                // delivery time is the whole point: by delivery time a
                // config-only re-arm may already have moved it on.
                let (raw_tx, mut raw_rx) = mpsc::channel::<LivenessFailure>(1);
                let reports_liveness = extras.reports.liveness.clone();
                tokio::spawn(async move {
                    if let Some(failure) = raw_rx.recv().await {
                        let _ = reports_liveness
                            .send(LivenessReport {
                                id: failure.id,
                                pid: failure.pid,
                                epoch: liveness_epoch,
                            })
                            .await;
                    }
                });
                instance.liveness = Some(spawn_liveness_task(
                    entry.id,
                    pid,
                    probe.clone(),
                    target,
                    prober,
                    raw_tx,
                ));
            }
            Err(err) => {
                // `normalize` already parses both probe targets, so a config
                // that reached the daemon cannot land here — swallowed instead
                // of `expect`-ed so a future path that skips normalization
                // costs one app its liveness probe rather than the daemon.
                // The `warn!` below is the whole of the visible sign: the app
                // still comes up online with no liveness probe, and nothing
                // in its status or on the bus says it ever asked for one.
                tracing::warn!(
                    id = entry.id,
                    name = config.name.as_str(),
                    %err,
                    "liveness_probe target could not be parsed; arming no liveness probe"
                );
            }
        }
    }
    Some(instance)
}

/// Spawns the name-group's cron worker, or `None` when the app configures no
/// `cron_restart` (or names a pattern that will not parse).
///
/// An unparseable pattern costs the app its schedule, and the `warn!` below is
/// the only record that a `cron_restart` was asked for and dropped — the app
/// comes up `online` either way, and no status or bus event carries it.
fn arm_cron(
    config: &AppConfig,
    extras: &Extras,
    supervisor: &SupervisorHandle,
) -> Option<JoinHandle<()>> {
    let pattern = config.cron_restart.as_ref()?;
    let schedule = match CronSchedule::parse(pattern, config.cron_timezone.as_deref()) {
        Ok(schedule) => schedule,
        Err(err) => {
            tracing::warn!(
                name = config.name.as_str(),
                pattern = pattern.as_str(),
                %err,
                "cron_restart pattern could not be parsed; arming no cron worker"
            );
            return None;
        }
    };
    // `max_cron_sleep` rides on `Extras` rather than being read off the app:
    // it tunes how the shepherd wakes up, not how any one app behaves.
    Some(spawn_cron_worker(
        config.name.clone(),
        schedule,
        Arc::clone(&extras.clock),
        supervisor.clone(),
        extras.max_cron_sleep,
    ))
}

/// Spawns the name-group's filesystem watch, or `None` when the app does not
/// ask to be watched (or when its root or its globs will not resolve).
///
/// Every failure here arms no watch rather than propagating: a watch root that
/// will not resolve must not take down the same app's cron worker, enforcer
/// and probe. Each one writes a `warn!` on the way out, and that record is the
/// entire signal: the app comes up `online` with no watch, and neither its
/// status nor the bus says a watch was ever asked for. See
/// `a_watch_root_that_will_not_resolve_says_in_the_log_which_app_lost_its_watch`
/// for the arm that is pinned as a contract rather than a promise.
///
/// Takes the whole [`ProcessEntry`] rather than its config because the
/// assembled `out_file`/`err_file` are what `own_log_ignores` needs, and the
/// entry is the only place this arming can read the paths the sheep's child is
/// really writing to without re-deriving them.
fn arm_watch(entry: &ProcessEntry, supervisor: &SupervisorHandle) -> Option<JoinHandle<()>> {
    let config = entry.spec.config();
    if !config.watch {
        return None;
    }
    let Some(cwd) = config.cwd.as_deref() else {
        // `normalize` rejects `watch = true` with no `cwd`, so this is
        // unreachable for a config that came through it — and the daemon's
        // own working directory is not an acceptable fallback (a systemd unit
        // would watch `/`).
        tracing::warn!(
            name = config.name.as_str(),
            "watch is on but the app names no cwd; arming no watch"
        );
        return None;
    };
    // Canonicalized, not merely absolute: the group loop matches by stripping
    // this prefix off the absolute paths notify delivers, and on macOS a
    // directory under `/var/...` arrives from FSEvents as `/private/var/...`.
    // Without this, every strip fails and the watch fires never.
    let root = match std::fs::canonicalize(cwd) {
        Ok(root) => root,
        Err(err) => {
            tracing::warn!(
                name = config.name.as_str(),
                path = cwd,
                %err,
                "watch root could not be resolved; arming no watch"
            );
            return None;
        }
    };
    // The app's own ignores plus this sheep's log files, for whichever of them
    // the app pointed back inside the tree it asked to have watched. See
    // `own_log_ignores` for why the default globs cannot cover that.
    let mut ignores = config.ignore_watch.clone();
    ignores.extend(own_log_ignores(
        &root,
        [entry.out_file.as_path(), entry.err_file.as_path()],
    ));
    let filter = match WatchFilter::new(&config.watch_options, &ignores) {
        Ok(filter) => filter,
        Err(err) => {
            // `normalize` compiles every `watch_options` and `ignore_watch`
            // pattern, so a config that reached the daemon cannot land here —
            // reported instead of `expect`-ed so a future path that skips
            // normalization costs one app its watch rather than the daemon.
            tracing::warn!(
                name = config.name.as_str(),
                %err,
                "watch globs could not be compiled; arming no watch"
            );
            return None;
        }
    };
    match spawn_watch_group(
        config.name.clone(),
        root,
        filter,
        watch_delay_for(config),
        supervisor.clone(),
    ) {
        Ok(handle) => Some(handle),
        Err(err) => {
            tracing::warn!(
                name = config.name.as_str(),
                %err,
                "the OS watch could not be started; arming no watch"
            );
            None
        }
    }
}

/// The debounce window an app's watch is armed with: its own `watch_delay`
/// when it set one, [`DEFAULT_WATCH_DELAY`] otherwise, floored either way at
/// [`MIN_WATCH_DELAY`].
///
/// The floor is a last line of defence, not the first: `shep-core`'s
/// `normalize` already refuses `watch_delay = "0"`, and every value it accepts
/// passes through here unchanged. See [`MIN_WATCH_DELAY`] for why it is a
/// single millisecond where its two siblings are a full second.
fn watch_delay_for(config: &AppConfig) -> Duration {
    config
        .watch_delay
        .map(UpDuration::as_duration)
        .unwrap_or(DEFAULT_WATCH_DELAY)
        .max(MIN_WATCH_DELAY)
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use chrono::{DateTime, Utc};
    use tokio::sync::broadcast;

    use super::*;
    use crate::bus::SharedEvent;
    use crate::cron::DEFAULT_MAX_CRON_SLEEP;
    use crate::fake::{ProcScript, ScriptedRunner};
    use crate::limits::PollingEnforcer;
    use crate::limits::sample::ProcessRss;
    use crate::probes::ProbeFailure;
    use crate::supervisor::spawn_supervisor;
    use crate::testing::{
        ArmCall, Harness, RecordingEnforcer, ScriptedProber, ScriptedSampler, TestClock, app_with,
        armed_entry, capture_logs, harness, harness_with_extras, idle_stats, probe_config,
        test_paths, touch,
    };
    use crate::watch::real_time;
    use shep_core::config::{ProbeConfig, ProbeKind};
    use shep_core::protocol::{BusEvent, ProcessEventKind, ProcessInfo};
    use shep_core::selector::ProcessSelector;
    use shep_core::status::ProcStatus;
    use shep_core::values::{MemSize, UpDuration};

    /// Generous bound on how long a test may wait for something on the
    /// (paused) tokio clock. Costs no real wall-clock time: the paused
    /// runtime auto-advances to this deadline only if nothing else becomes
    /// ready first.
    const EVENT_WAIT: Duration = Duration::from_secs(120);

    /// A window that spans a whole hourly cron occurrence and then some, for
    /// the negative halves: crossing the occurrence and making the claim are
    /// the same call, per Global Constraints rule 11. A `try_recv` after an
    /// `advance` would read empty whether or not the worker was ever aborted.
    const PAST_THE_NEXT_OCCURRENCE: Duration = Duration::from_secs(3_700);

    /// How long a real-clock test waits for a liveness report that should
    /// arrive. Generous enough that a loaded runner cannot turn a genuine
    /// pass into a flake.
    const LIVENESS_DEADLINE: Duration = Duration::from_secs(10);

    /// The shortest interval `spawn_liveness_task` honours — anything smaller
    /// is floored there, so a fixture naming a smaller number would be a lie
    /// about what its test waits for. Declared as a literal rather than
    /// imported because `probes`' own floor is private to that module.
    const PROBE_INTERVAL: UpDuration = UpDuration::from_millis(1_000);

    fn dt(s: &str) -> DateTime<Utc> {
        s.parse().expect("valid RFC3339 timestamp")
    }

    /// The registry-tier fixture: a paused-clock-driven wall clock, a
    /// recording enforcer, and both report receivers held by the test rather
    /// than by a reporter (there is none — a registry test asserts the report
    /// itself, never a restart it did not trigger).
    struct Rig {
        extras: Extras,
        enforcer: Arc<RecordingEnforcer>,
        clock: Arc<TestClock>,
        liveness: mpsc::Receiver<LivenessReport>,
        _breaches: mpsc::Receiver<LimitBreach>,
    }

    fn rig(max_cron_sleep: Duration) -> Rig {
        let clock = Arc::new(TestClock::starting_at(dt("2026-01-01T00:00:00Z")));
        let enforcer = Arc::new(RecordingEnforcer::default());
        let (breach_tx, breaches) = mpsc::channel(8);
        let (live_tx, liveness) = mpsc::channel(8);
        Rig {
            extras: Extras {
                clock: Arc::clone(&clock) as Arc<dyn Clock>,
                enforcer: Arc::clone(&enforcer) as Arc<dyn LimitEnforcer>,
                max_cron_sleep,
                reports: ExtrasReports {
                    breaches: breach_tx,
                    liveness: live_tx,
                },
                stats: idle_stats(),
            },
            enforcer,
            clock,
            liveness,
            _breaches: breaches,
        }
    }

    /// One supervisor engine over a scripted runner with plenty of
    /// `never_exits` procs — enough for the initial starts plus every restart
    /// a *broken* implementation could produce, so no negative assertion here
    /// can pass merely because the script ran out (an exhausted script makes
    /// the supervisor emit `Errored`, not `Restart`).
    fn spawn_test_fixture() -> (
        SupervisorHandle,
        broadcast::Receiver<SharedEvent>,
        tempfile::TempDir,
    ) {
        let dir = tempfile::tempdir().unwrap();
        let (events, rx) = crate::bus::test_bus(64);
        let runner = ScriptedRunner::new(vec![ProcScript::never_exits(); 12]);
        let handle = spawn_supervisor(runner, test_paths(&dir), events);
        (handle, rx, dir)
    }

    /// A prober that never fails — the neutral value for a case that arms no
    /// liveness probe but must still hand `arm` one.
    fn idle_prober() -> Arc<dyn Prober> {
        Arc::new(ScriptedProber::new(vec![]))
    }

    /// A prober that fails every probe — a liveness loop armed with one
    /// reports as soon as it has taken `failure_threshold` samples, so a case
    /// asserting SILENCE against one is asserting the loop is gone.
    fn failing_prober() -> Arc<dyn Prober> {
        Arc::new(ScriptedProber::new(vec![Err(ProbeFailure::Timeout)]))
    }

    /// Yields until `task` reports finished, panicking if it never does.
    /// Yielding rather than advancing: a worker that ends does so on its
    /// first poll, and an `advance` would resolve unrelated timers with it.
    async fn settle_finished(task: &JoinHandle<()>) {
        for _ in 0..100 {
            if task.is_finished() {
                return;
            }
            tokio::task::yield_now().await;
        }
        panic!("the task never finished");
    }

    /// Waits up to `window` for a `Restart` naming `name`, panicking if none
    /// arrives.
    async fn expect_restart(
        rx: &mut broadcast::Receiver<SharedEvent>,
        name: &str,
        window: Duration,
    ) -> ProcessInfo {
        expect_restart_event(rx, name, window).await.0
    }

    /// [`expect_restart`], plus the `manually` flag the bus put on that
    /// restart — [`BusEvent::Process`]'s own claim about who caused it ("True
    /// when a user action caused it").
    ///
    /// Split out rather than folded into `expect_restart`'s return type
    /// because only the handful of cases below read the flag, and every other
    /// caller wants the snapshot alone.
    async fn expect_restart_event(
        rx: &mut broadcast::Receiver<SharedEvent>,
        name: &str,
        window: Duration,
    ) -> (ProcessInfo, bool) {
        let restart = async {
            loop {
                match rx.recv().await.map(|event| event.to_event()) {
                    Ok(BusEvent::Process {
                        event: ProcessEventKind::Restart,
                        info,
                        manually,
                        ..
                    }) if info.name == name => return (info, manually),
                    Ok(_) => continue,
                    Err(broadcast::error::RecvError::Lagged(_)) => continue,
                    Err(err) => panic!("event stream closed before a restart of {name}: {err}"),
                }
            }
        };
        match tokio::time::timeout(window, restart).await {
            Ok(observed) => observed,
            Err(_) => panic!("timed out waiting for a restart of {name}"),
        }
    }

    /// Waits up to `window` for a `Restart` naming `name`, panicking if one
    /// arrives.
    ///
    /// A bounded `timeout` + `recv`, never a bare `try_recv` (Global
    /// Constraints rule 11): the window is what carries the paused clock past
    /// the occurrence the abort was supposed to stop, so the same call both
    /// crosses it and makes the claim.
    async fn assert_no_restart_within(
        rx: &mut broadcast::Receiver<SharedEvent>,
        name: &str,
        window: Duration,
    ) {
        let deadline = tokio::time::Instant::now() + window;
        loop {
            let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
            match tokio::time::timeout(remaining, rx.recv())
                .await
                .map(|received| received.map(|event| event.to_event()))
            {
                Err(_) => return, // window elapsed with nothing matching — expected
                Ok(Ok(BusEvent::Process {
                    event: ProcessEventKind::Restart,
                    info,
                    ..
                })) if info.name == name => {
                    panic!(
                        "unexpected restart of {name} observed (restarts={})",
                        info.restarts
                    );
                }
                Ok(Ok(_)) => continue,
                // A negative assertion cannot skip events: the ones the
                // broadcast channel dropped may include the very `Restart`
                // this forbids, so continuing here would return success on an
                // overflow. `expect_restart` may skip them safely — the worst
                // a lag costs it is a timeout — but this one has to fail
                // loudly instead of failing open. Same fix, same reason, as
                // `watch`'s own copy of this helper.
                Ok(Err(broadcast::error::RecvError::Lagged(skipped))) => {
                    panic!(
                        "event stream lagged by {skipped} while checking for no restart of \
                         {name}: a skipped event may have been the restart this forbids"
                    )
                }
                Ok(Err(err)) => {
                    panic!("event channel closed while checking for no restart of {name}: {err}")
                }
            }
        }
    }

    async fn expect_liveness(
        rx: &mut mpsc::Receiver<LivenessReport>,
        window: Duration,
    ) -> LivenessReport {
        match tokio::time::timeout(window, rx.recv()).await {
            Ok(Some(failure)) => failure,
            Ok(None) => panic!("the liveness channel closed before a failure arrived"),
            Err(_) => panic!("timed out waiting for a liveness failure"),
        }
    }

    /// Waits up to `window` for a liveness failure, panicking if one arrives.
    /// A bounded `timeout` + `recv` for the same reason as
    /// [`assert_no_restart_within`].
    async fn assert_no_liveness_within(rx: &mut mpsc::Receiver<LivenessReport>, window: Duration) {
        match tokio::time::timeout(window, rx.recv()).await {
            Err(_) => {} // window elapsed with nothing arriving — expected
            Ok(Some(failure)) => panic!("unexpected liveness failure observed: {failure:?}"),
            Ok(None) => panic!("the liveness channel disconnected while checking for silence"),
        }
    }

    /// Crosses one hourly occurrence in steps far finer than either sleep cap
    /// under discussion, so the worker's own cadence — not the test's —
    /// decides how often it wakes. A single `advance(3600s)` would resolve
    /// whatever sleep is pending in one shot regardless.
    async fn cross_one_hour() {
        for _ in 0..120 {
            tokio::time::advance(Duration::from_secs(30)).await;
        }
    }

    // ------------------------------------------------------------------
    // The registry: what gets armed, and what stops.
    // ------------------------------------------------------------------

    // `Extras`' Debug is hand-rolled because neither seam is Debug, and it
    // deliberately prints ROLES for the seams while printing `max_cron_sleep`
    // for real — that field is a knob the user set, so a daemon log dumping
    // this struct should say what it ended up being. Pinned as an exact string
    // (IR-41): fails if a later edit starts printing a channel's innards, or
    // quietly stops printing the one field that carries a real value.
    #[tokio::test(start_paused = true)]
    async fn extras_debug_names_the_seams_by_role_and_the_sleep_bound_by_value() {
        let rig = rig(Duration::from_secs(300));
        assert_eq!(
            format!("{:?}", rig.extras),
            r#"Extras { clock: "<dyn Clock>", enforcer: "<dyn LimitEnforcer>", max_cron_sleep: 300s, .. }"#
        );
    }

    // `InstanceExtras`' Debug is hand-rolled for the same reason `Extras`' is:
    // `Arc<dyn LimitEnforcer>` is not Debug, and the fact worth printing is
    // that an arming exists rather than which mechanism performed it. Pinned
    // as an exact string in both directions (IR-41).
    //
    // Both halves are load-bearing. `limit_armed: true` alone cannot tell a
    // correct `self.limit.is_some()` from a hardcoded `true`, and
    // `limit_armed: false` alone cannot tell it from a hardcoded `false`; the
    // inversion to `is_none()` moves the boolean either way, so it takes an
    // armed instance AND an unarmed one to say the field reports its own
    // field. The unarmed half comes from an app configuring no `max_memory`,
    // which — since sampling is armed for every sheep — is a real registry
    // entry rather than a value only a fixture could build.
    //
    // The trailing `..` is `stats`, which every instance carries and which
    // therefore prints nothing a reader could act on. That it stays out of
    // the string is part of the claim: a Debug that started printing an `Arc`
    // address would change this.
    //
    // The liveness loop is left unarmed on purpose. Its field is a derived
    // `Option<JoinHandle<()>>`, and a live one renders as
    // `Some(JoinHandle { id: Id(2) })` — a tokio-internal task counter that
    // shifts with every task this fixture happens to spawn first, so pinning
    // it would pin tokio's numbering rather than this crate's redaction.
    //
    // fails if the struct is renamed out from under its `debug_struct` (a
    // daemon log naming a type nobody can grep for), and fails if
    // `limit_armed` reports anything other than whether a limit is armed.
    #[tokio::test(start_paused = true)]
    async fn instance_extras_debug_reports_an_arming_without_naming_the_enforcer() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let capped = app_with("web", |app| app.max_memory = Some(MemSize::from_bytes(500)));
        let uncapped = app_with("api", |app| app.max_memory = None);

        registry.arm(
            &armed_entry(0, 0, 1000, capped, &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );
        registry.arm(
            &armed_entry(1, 0, 1001, uncapped, &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );

        assert_eq!(
            format!("{:?}", registry.instances[&0]),
            "InstanceExtras { limit_armed: true, liveness: None, .. }"
        );
        assert_eq!(
            format!("{:?}", registry.instances[&1]),
            "InstanceExtras { limit_armed: false, liveness: None, .. }"
        );
    }

    // `Extras::real` is the production wiring and the only constructor `boot`
    // calls, so nothing else in this crate would notice it handing the
    // enforcer a channel of its own (`mpsc::channel(1).0` compiles and reports
    // into the void). Real sysinfo over this very test process, whose RSS is
    // comfortably over one byte, on the paused clock the polling loop sleeps
    // on: the breach has to come back out of the sender this test kept.
    #[tokio::test(start_paused = true)]
    async fn real_extras_wire_the_enforcer_to_the_reports_channel() {
        let (breach_tx, mut breaches) = mpsc::channel(4);
        let (live_tx, _liveness) = mpsc::channel(4);
        let extras = Extras::real(
            ExtrasReports {
                breaches: breach_tx,
                liveness: live_tx,
            },
            Duration::from_secs(300),
        );
        assert_eq!(
            format!("{extras:?}"),
            r#"Extras { clock: "<dyn Clock>", enforcer: "<dyn LimitEnforcer>", max_cron_sleep: 300s, .. }"#,
            "`real` must carry the sleep bound it was handed, not re-derive one"
        );

        let limit = MemSize::from_bytes(1);
        extras.enforcer.arm(3, std::process::id(), limit);
        let breach = match tokio::time::timeout(EVENT_WAIT, breaches.recv()).await {
            Ok(Some(breach)) => breach,
            Ok(None) => panic!("the breach channel closed before a breach arrived"),
            Err(_) => panic!("timed out waiting for a breach from the real enforcer"),
        };
        assert_eq!(breach.id, 3);
        assert_eq!(breach.root_pid, std::process::id());
        assert_eq!(breach.limit, limit);
    }

    // fails if `arm` stops gating its per-name work on the configuration —
    // the shape that hands every app in the flock a group, and, with
    // `arm_watch`'s own guard gone too, a watcher on its own cwd. The cwd is
    // a real directory rather than absent so this app is one a watcher COULD
    // be registered on, instead of one that structurally cannot reach that
    // far. The positive cases below are what make the rest able to fail: they
    // prove both maps really do fill.
    #[tokio::test(start_paused = true)]
    async fn an_app_configuring_no_extras_arms_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let root = tempfile::tempdir().unwrap();
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();

        let app = app_with("web", |app| {
            app.cwd = Some(root.path().display().to_string());
        });
        let entry = armed_entry(0, 0, 1000, app, &paths);
        registry.arm(&entry, idle_prober(), &rig.extras, &handle);

        assert!(
            registry.groups.is_empty(),
            "an app with neither cron_restart nor watch must arm no name-group tasks"
        );
        // Sampling is the exception, and deliberately so: it is armed for
        // every sheep with a pid, so this app has an `InstanceExtras`
        // carrying nothing else. `every_sheep_with_a_pid_is_watched_even_
        // with_no_limit_and_no_probe` is where that half is asserted.
        assert_eq!(
            format!("{:?}", registry.instances[&0]),
            "InstanceExtras { limit_armed: false, liveness: None, .. }",
            "an app with neither max_memory nor liveness_probe must arm nothing beyond sampling"
        );
        assert!(
            rig.enforcer.arms().is_empty(),
            "an app with no max_memory must not reach the enforcer at all"
        );
    }

    // fails if only limit-carrying sheep get watched. An app with no
    // `max_memory` is the ordinary case, and a listing reporting `-` for
    // every one of them is the bug this split exists to fix. The disarm at
    // the end is the other half: a watch that is never dropped samples a dead
    // pid forever, and hands its CPU baseline to whatever process the OS next
    // gives that number to.
    #[tokio::test(start_paused = true)]
    async fn every_sheep_with_a_pid_is_watched_even_with_no_limit_and_no_probe() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.max_memory = None;
            app.liveness_probe = None;
        });

        registry.arm(
            &armed_entry(7, 0, 4242, app, &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );

        assert_eq!(
            rig.extras.stats.watched_for_test(),
            vec![(7, 4242)],
            "an app with neither max_memory nor a liveness_probe is the ORDINARY case, and a \
             listing reporting `-` for every one of them is what this split exists to fix"
        );
        assert!(
            rig.enforcer.arms().is_empty(),
            "sampling is not enforcement: an app with no ceiling must still not be armed"
        );

        registry.disarm(7, "web");
        assert!(rig.extras.stats.watched_for_test().is_empty());
    }

    // fails if `arm_watch` stops consulting `config.watch`. A cron-restarting
    // app is a member of its name group whatever it thinks of watching, so it
    // is the one shape that reaches `arm_watch` without asking to be watched —
    // and its cwd is a real directory, so a watcher really would be registered
    // on it.
    #[tokio::test(start_paused = true)]
    async fn a_cron_only_app_with_a_real_cwd_arms_no_watch() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let root = tempfile::tempdir().unwrap();
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.cron_restart = Some("0 * * * *".to_string());
            app.cwd = Some(root.path().display().to_string());
        });

        registry.arm(
            &armed_entry(0, 0, 1000, app, &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );

        let group = &registry.groups["web"];
        assert!(group.cron.is_some(), "the cron worker is what armed here");
        assert!(
            group.watch.is_none(),
            "an app that did not ask to be watched must get no watcher on its cwd"
        );
    }

    // fails if `cron_timezone` is dropped on the way to `CronSchedule::parse`.
    // `Etc/GMT+5` is UTC MINUS five (POSIX inverts the sign), so 05:00 local
    // is 10:00Z — while the same pattern read as UTC fires at 05:00Z, five
    // hours inside the silent window below.
    #[tokio::test(start_paused = true)]
    async fn a_cron_pattern_is_resolved_in_the_apps_own_timezone() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, mut rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.cron_restart = Some("0 5 * * *".to_string());
            app.cron_timezone = Some("Etc/GMT+5".to_string());
        });
        handle.start(vec![app.clone()]).await.unwrap();

        registry.arm(
            &armed_entry(0, 0, 1000, app, &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );
        tokio::task::yield_now().await;

        // Six hours from midnight UTC: past a UTC reading of the pattern,
        // still four hours short of the app's own.
        assert_no_restart_within(&mut rx, "web", Duration::from_secs(6 * 3_600)).await;
        expect_restart(&mut rx, "web", Duration::from_secs(6 * 3_600)).await;
    }

    // fails if a watch root that will not resolve takes down the arm path for
    // the same app's cron worker — the whole reason `arm_watch` reports its
    // failures instead of propagating them.
    //
    // A mistyped glob cannot trigger this case: `normalize` compiles both
    // glob lists, so `app_with` would reject such a config before this test
    // could arm it. An unresolvable cwd is the config-shaped watch failure
    // that survives normalization.
    #[tokio::test(start_paused = true)]
    async fn a_watch_root_that_will_not_resolve_costs_the_app_its_watch_and_nothing_else() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let root = tempfile::tempdir().unwrap();
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.watch = true;
            app.cwd = Some(root.path().join("no-such-directory").display().to_string());
            app.cron_restart = Some("0 * * * *".to_string());
        });

        registry.arm(
            &armed_entry(0, 0, 1000, app, &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );

        let group = &registry.groups["web"];
        assert!(group.watch.is_none(), "the watch root cannot have resolved");
        assert!(
            group.cron.is_some(),
            "an unresolvable watch root must not cost this app its cron worker too"
        );
    }

    // `arm_watch` arms nothing on every failure and lets the sheep go `online`
    // regardless, so its `warn!` is the ENTIRE observable difference between
    // an app that is being watched and one that asked to be and is not. The
    // case above pins that the watch is absent; this one pins that the daemon
    // says so, which is the half an operator can actually act on.
    //
    // The app's name is deliberately unlike anything else in the record — the
    // message, the target and the rendered `io::Error` all lack it — so
    // `name="unwatchable"` can only have come from the field.
    //
    // fails if the unresolvable-root arm stops writing its record (the app
    // comes back to being unwatched in silence), and fails if the record drops
    // the `name` field, which on a flock of twelve is the difference between a
    // fault an operator can fix and one they can only observe.
    #[tokio::test(start_paused = true)]
    async fn a_watch_root_that_will_not_resolve_says_in_the_log_which_app_lost_its_watch() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let root = tempfile::tempdir().unwrap();
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("unwatchable", |app| {
            app.watch = true;
            app.cwd = Some(root.path().join("no-such-directory").display().to_string());
        });
        let entry = armed_entry(0, 0, 1000, app, &paths);

        let records = capture_logs(|| {
            registry.arm(&entry, idle_prober(), &rig.extras, &handle);
        });

        assert!(
            registry.groups["unwatchable"].watch.is_none(),
            "precondition: the watch root cannot have resolved"
        );
        assert!(
            records.contains("watch root could not be resolved"),
            "arming no watch must be reported, not swallowed: {records:?}"
        );
        assert!(
            records.contains("WARN"),
            "an app silently losing its watch is a warning, not a debug detail: {records:?}"
        );
        assert!(
            records.contains(r#"name="unwatchable""#),
            "the record must name the app that lost its watch: {records:?}"
        );
    }

    // fails if a cron worker is armed per instance rather than per name. Two
    // workers on the same schedule read the wall clock about twice as often
    // as one, which is the only trace a second worker leaves: the bus cannot
    // tell them apart, because two `restart(Name)` commands racing the same
    // sheep are collapsed into one by the actor's own first-command-wins
    // dedupe. The bound matches `cron`'s own `ten_minute_cap` case, which
    // pins a single worker on this exact configuration at well under 20.
    #[tokio::test(start_paused = true)]
    async fn a_second_instance_of_a_name_arms_no_second_cron_worker() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, mut rx, _fixture) = spawn_test_fixture();
        let rig = rig(Duration::from_secs(600));
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.cron_restart = Some("0 * * * *".to_string());
            app.instances = 2;
        });
        handle.start(vec![app.clone()]).await.unwrap();

        for (id, instance, pid) in [(0, 0, 1000), (1, 1, 1001)] {
            let entry = armed_entry(id, instance, pid, app.clone(), &paths);
            registry.arm(&entry, idle_prober(), &rig.extras, &handle);
            // Lets the worker commit to its first `next` while the clock still
            // reads close to now — `advance` jumps first and polls after.
            tokio::task::yield_now().await;
        }

        assert_eq!(registry.groups.len(), 1, "one group, not one per instance");
        assert_eq!(
            registry.groups["web"].members,
            HashSet::from([0, 1]),
            "both instances must be recorded as keeping the group alive"
        );

        cross_one_hour().await;
        expect_restart(&mut rx, "web", EVENT_WAIT).await;
        assert!(
            rig.clock.reads() < 20,
            "one cron worker reads the clock ~13 times over this hour; two read ~26 (got {})",
            rig.clock.reads()
        );
    }

    // fails if `arm` hands the enforcer the sheep's id where its pid belongs,
    // and fails if a re-arm keeps the first arming — the "arms once and never
    // updates" bug, which leaves a respawned sheep's limit enforced against
    // the process it replaced.
    #[tokio::test(start_paused = true)]
    async fn a_respawn_re_arms_the_enforcer_with_the_new_pid() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let limit = MemSize::from_bytes(500);
        let app = app_with("web", |app| app.max_memory = Some(limit));

        registry.arm(
            &armed_entry(4, 0, 1000, app.clone(), &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );
        registry.arm(
            &armed_entry(4, 0, 2000, app, &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );

        assert_eq!(
            rig.enforcer.arms(),
            vec![
                ArmCall {
                    id: 4,
                    root_pid: 1000,
                    limit,
                },
                ArmCall {
                    id: 4,
                    root_pid: 2000,
                    limit,
                },
            ]
        );
        assert_eq!(
            rig.enforcer.disarms(),
            vec![4],
            "a re-arm must undo the previous arming rather than leaking it"
        );
    }

    // fails two ways, and needs both halves to be able to fail at all: a
    // disarm that tears the group down while another instance is still armed
    // (no restart would arrive after the first disarm), and a disarm that
    // leaves the worker running after the LAST instance leaves (a restart
    // would arrive after the second). A bare "nothing after the last disarm"
    // assertion passes just as happily against a worker that never fired.
    #[tokio::test(start_paused = true)]
    async fn only_the_last_instance_leaving_stops_the_name_groups_cron_worker() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, mut rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.cron_restart = Some("0 * * * *".to_string());
            app.instances = 2;
        });
        handle.start(vec![app.clone()]).await.unwrap();
        for (id, instance, pid) in [(0, 0, 1000), (1, 1, 1001)] {
            let entry = armed_entry(id, instance, pid, app.clone(), &paths);
            registry.arm(&entry, idle_prober(), &rig.extras, &handle);
            tokio::task::yield_now().await;
        }

        registry.disarm(0, "web");
        assert_eq!(
            registry.groups["web"].members,
            HashSet::from([1]),
            "a non-last disarm drops only its own membership"
        );
        cross_one_hour().await;
        // Two instances are online, and `ProcessSelector::Name` reaches both,
        // so one occurrence produces two `Restart` events. Both are drained
        // before the claim below: a leftover would be indistinguishable from
        // a worker that outlived its disarm.
        expect_restart(&mut rx, "web", EVENT_WAIT).await;
        expect_restart(&mut rx, "web", EVENT_WAIT).await;

        registry.disarm(1, "web");
        assert!(
            !registry.groups.contains_key("web"),
            "the last instance leaving must take the group with it"
        );
        assert_no_restart_within(&mut rx, "web", PAST_THE_NEXT_OCCURRENCE).await;
    }

    // fails if `disarm` reads "this id was not a member" as "the last member
    // just left" and tears down a group every one of whose instances is still
    // armed — and fails if it panics on a name it never saw. The restart at
    // the end is what proves the group really survived, rather than merely
    // still having a map entry.
    #[tokio::test(start_paused = true)]
    async fn disarming_an_id_that_was_never_armed_leaves_the_group_alone() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, mut rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.cron_restart = Some("0 * * * *".to_string())
        });
        handle.start(vec![app.clone()]).await.unwrap();
        registry.arm(
            &armed_entry(0, 0, 1000, app, &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );
        tokio::task::yield_now().await;

        registry.disarm(99, "web"); // a name that exists, an id that does not
        registry.disarm(0, "other"); // an id that exists, a name that does not

        assert_eq!(registry.groups["web"].members, HashSet::from([0]));
        cross_one_hour().await;
        expect_restart(&mut rx, "web", EVENT_WAIT).await;
    }

    // fails if `arm`'s per-name work keys on the group's map ENTRY existing
    // rather than on its tasks being alive. A cron worker that ended on its
    // own leaves a finished handle behind, and an `arm` that returns on map
    // presence alone never rebuilds it — the app silently loses its schedule
    // with nothing left to notice. The clock is what makes the claim: a fresh
    // worker on this pattern reads it exactly once before returning, so a
    // second reading means a second worker really was spawned.
    #[tokio::test(start_paused = true)]
    async fn a_cron_worker_that_ended_on_its_own_is_rebuilt_on_the_next_arm() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        // 30 February: a pattern croner parses and then finds no occurrence
        // for, which is the `Ok(None)` arm `spawn_cron_worker` returns on.
        let app = app_with("web", |app| {
            app.cron_restart = Some("0 0 30 2 *".to_string());
        });

        registry.arm(
            &armed_entry(0, 0, 1000, app.clone(), &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );
        settle_finished(
            registry.groups["web"]
                .cron
                .as_ref()
                .expect("the first arm spawns a worker"),
        )
        .await;
        assert_eq!(
            rig.clock.reads(),
            1,
            "a worker on a pattern with no occurrence reads the clock once and ends"
        );

        registry.arm(
            &armed_entry(0, 0, 2000, app, &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );
        settle_finished(
            registry.groups["web"]
                .cron
                .as_ref()
                .expect("the re-arm must leave a worker behind"),
        )
        .await;
        assert_eq!(
            rig.clock.reads(),
            2,
            "a re-arm must rebuild a name-group task that ended on its own"
        );
    }

    // fails if group membership is recorded from what an arming managed to
    // BUILD rather than from what the app CONFIGURED. This app asks for a
    // watch and gets none — its cwd does not resolve — so under the
    // build-keyed reading the group does not exist and this instance is in no
    // group at all; a later instance of the same name whose watch DID arm
    // would then own the group alone, and stopping it would tear the watch
    // down with this one still online.
    //
    // The enforcer assertion is the other half: a watch that could not be
    // armed costs this app its watch and nothing else.
    #[tokio::test(start_paused = true)]
    async fn an_instance_whose_watch_could_not_be_armed_still_joins_its_group() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let limit = MemSize::from_bytes(500);
        // `normalize` rejects `watch = true` with no cwd and compiles both
        // glob lists, but never checks that the cwd it names resolves — so
        // `canonicalize` failing is the one watch-arming failure a config
        // that came through it can reach.
        let missing = dir.path().join("no-such-directory");
        let app = app_with("web", |app| {
            app.watch = true;
            app.cwd = Some(missing.display().to_string());
            app.max_memory = Some(limit);
        });

        registry.arm(
            &armed_entry(0, 0, 1000, app, &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );

        let group = registry
            .groups
            .get("web")
            .expect("a watched app is a member of its group whether or not the watch armed");
        assert!(group.watch.is_none(), "this app's watch cannot have armed");
        assert_eq!(group.members, HashSet::from([0]));
        assert_eq!(
            rig.enforcer.arms(),
            vec![ArmCall {
                id: 0,
                root_pid: 1000,
                limit,
            }],
            "a watch that could not be armed must not take the memory limit with it"
        );
    }

    // fails if `ExtrasRegistry` has no `Drop` that aborts what it armed — the
    // teardown a shutdown depends on, since `begin_shutdown` only kills sheep
    // holding a live task and a `WaitingRestart` sheep holds none. A dropped
    // `JoinHandle` detaches its task rather than cancelling it, so the loop
    // would outlive the actor and keep probing.
    //
    // The kept registry is the control: it proves these loops really do
    // report, so the silence demanded of the dropped one could have failed.
    // Both orderings are caught — under a missing `Drop` BOTH report, and
    // whichever lands first fails one of the two assertions.
    #[tokio::test(start_paused = true)]
    async fn dropping_the_registry_stops_the_liveness_loop_it_armed() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let mut rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let app = app_with("web", |app| {
            app.liveness_probe = Some(ProbeConfig {
                failure_threshold: 1,
                ..probe_config(ProbeKind::Tcp, "localhost:5432")
            });
        });
        let interval = app
            .config()
            .liveness_probe
            .as_ref()
            .expect("the fixture just set one")
            .interval
            .as_duration();

        let mut kept = ExtrasRegistry::default();
        kept.arm(
            &armed_entry(8, 1, 5678, app.clone(), &paths),
            failing_prober(),
            &rig.extras,
            &handle,
        );
        let mut discarded = ExtrasRegistry::default();
        discarded.arm(
            &armed_entry(7, 0, 1234, app, &paths),
            failing_prober(),
            &rig.extras,
            &handle,
        );
        drop(discarded);

        let failure = expect_liveness(&mut rig.liveness, EVENT_WAIT).await;
        assert_eq!(
            failure,
            LivenessReport {
                id: 8,
                pid: 5678,
                epoch: 1
            },
            "only the registry that is still alive may report"
        );
        assert_no_liveness_within(&mut rig.liveness, interval * 3).await;
    }

    // fails if `Drop` aborts the per-instance extras and leaves the name-group
    // tasks running (`group.abort()` replaced by a discard). This is the half
    // the whole `Drop` argument rests on: a `WaitingRestart` sheep holds no
    // live task, so `begin_shutdown` never kills it and `handle_exited` never
    // runs its terminal branches — its name group's cron worker outlives the
    // actor. A dropped `JoinHandle` detaches its task rather than cancelling
    // it, so that worker goes on restarting a name whose engine is gone.
    //
    // Two NAMES, not two instances of one: the bus attributes a restart to a
    // name and never to the registry that armed the worker, so the control
    // and the subject have to be tellable apart on the wire. `kept` is that
    // control — it proves a worker on this very schedule really does fire, so
    // the silence demanded of `dropped` could have failed.
    //
    // Twelve scripts against six spawns at worst: two starts, `kept`'s two
    // occurrences, and — under the broken implementation — `dropped`'s two.
    // The surviving worker needs something to respawn from, since an
    // exhausted script makes the supervisor emit `Errored` rather than the
    // `Restart` the negative helper matches on.
    #[tokio::test(start_paused = true)]
    async fn dropping_the_registry_stops_the_name_group_worker_it_armed() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, mut rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let hourly = |name: &str| {
            app_with(name, |app| {
                app.cron_restart = Some("0 * * * *".to_string());
            })
        };
        handle
            .start(vec![hourly("kept"), hourly("dropped")])
            .await
            .unwrap();

        let mut kept = ExtrasRegistry::default();
        kept.arm(
            &armed_entry(0, 0, 1000, hourly("kept"), &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );
        let mut discarded = ExtrasRegistry::default();
        discarded.arm(
            &armed_entry(1, 0, 1001, hourly("dropped"), &paths),
            idle_prober(),
            &rig.extras,
            &handle,
        );
        // Lets both workers commit to their first `next` while the clock still
        // reads close to now — `advance` jumps first and polls after.
        tokio::task::yield_now().await;
        drop(discarded);

        cross_one_hour().await;
        expect_restart(&mut rx, "kept", EVENT_WAIT).await;
        assert_no_restart_within(&mut rx, "dropped", PAST_THE_NEXT_OCCURRENCE).await;
    }

    // fails if `disarm` does not abort the liveness loop it armed. A healthy
    // loop never ends on its own — it returns only after reporting — so a
    // stopped or deleted sheep would leak a task probing a pid that is gone.
    // Same control-and-silence shape as the `Drop` case above, for the same
    // reason: the sibling instance left armed proves the silence could fail.
    #[tokio::test(start_paused = true)]
    async fn disarming_an_id_stops_its_liveness_loop() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let mut rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.instances = 2;
            app.liveness_probe = Some(ProbeConfig {
                failure_threshold: 1,
                ..probe_config(ProbeKind::Tcp, "localhost:5432")
            });
        });
        let interval = app
            .config()
            .liveness_probe
            .as_ref()
            .expect("the fixture just set one")
            .interval
            .as_duration();

        for (id, instance, pid) in [(0, 0, 1000), (1, 1, 1001)] {
            registry.arm(
                &armed_entry(id, instance, pid, app.clone(), &paths),
                failing_prober(),
                &rig.extras,
                &handle,
            );
        }
        registry.disarm(0, "web");

        let failure = expect_liveness(&mut rig.liveness, EVENT_WAIT).await;
        assert_eq!(
            failure,
            LivenessReport {
                id: 1,
                pid: 1001,
                epoch: 1
            },
            "only the instance that is still armed may report"
        );
        assert_no_liveness_within(&mut rig.liveness, interval * 3).await;
    }

    // fails if `arm` wires the liveness loop to a throwaway channel of its
    // own (`let (tx, _rx) = mpsc::channel(1)` — it compiles, probes forever,
    // and reports into the void) instead of the shared sender on
    // `Extras::reports`, and fails if it passes the id where the pid belongs.
    #[tokio::test(start_paused = true)]
    async fn a_liveness_threshold_reports_this_instances_id_and_pid() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let mut rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.liveness_probe = Some(ProbeConfig {
                failure_threshold: 2,
                ..probe_config(ProbeKind::Tcp, "localhost:5432")
            });
        });
        let interval = app
            .config()
            .liveness_probe
            .as_ref()
            .expect("the fixture just set one")
            .interval
            .as_duration();

        registry.arm(
            &armed_entry(7, 0, 1234, app, &paths),
            Arc::new(ScriptedProber::new(vec![Err(ProbeFailure::Timeout)])),
            &rig.extras,
            &handle,
        );

        let failure = expect_liveness(&mut rig.liveness, EVENT_WAIT).await;
        assert_eq!(
            failure,
            LivenessReport {
                id: 7,
                pid: 1234,
                epoch: 1
            }
        );
        // The scripted prober repeats its last outcome forever, so a loop
        // that kept probing after reporting would report again inside this
        // window.
        assert_no_liveness_within(&mut rig.liveness, interval * 3).await;
    }

    // fails if `arm_watch` hands `watch_delay` to the debouncer as written.
    // `notify-debouncer-full` derives its poll tick as `delay / 4` and sleeps
    // it on a dedicated OS thread, so a zero makes that thread spin (see
    // `MIN_WATCH_DELAY`'s own doc for what that costs).
    //
    // A direct call rather than an armed registry, and deliberately so:
    // `shep-core`'s `normalize` refuses `watch_delay = "0"`, and a
    // `ResolvedApp` is only obtainable through it, so no fixture in this
    // crate can carry a zero as far as `ExtrasRegistry::arm`. This floor
    // exists for the caller that skipped normalization — a future boot path,
    // or a bug — which is exactly the shape a bare `AppConfig` here stands
    // in for. It is also the only observable the floor has: with the tick
    // gone to zero the watch still delivers every event, it just burns a
    // core doing it, so there is no batch, no restart and no call count that
    // could tell the two apart.
    #[test]
    fn a_zero_watch_delay_is_floored_before_it_reaches_the_debouncer() {
        let mut app = AppConfig::minimal("web", "./srv");
        app.watch_delay = Some(UpDuration::from_millis(0));
        assert_eq!(watch_delay_for(&app), MIN_WATCH_DELAY);
    }

    // fails if the floor is raised to something a Flockfile may legitimately
    // ask for. `normalize` accepts every non-zero `watch_delay`, so a floor
    // above one millisecond would silently lengthen a save-to-restart round
    // trip the user deliberately shortened — the clamp-nobody-announces
    // failure that the probe interval's config-time rejection exists to
    // avoid. Also fails if an app naming no delay stops getting the default.
    #[test]
    fn a_watch_delay_the_config_layer_accepts_is_never_clamped() {
        let mut app = AppConfig::minimal("web", "./srv");
        app.watch_delay = Some(UpDuration::from_millis(1));
        assert_eq!(watch_delay_for(&app), Duration::from_millis(1));

        app.watch_delay = Some(UpDuration::from_millis(20));
        assert_eq!(watch_delay_for(&app), Duration::from_millis(20));

        app.watch_delay = None;
        assert_eq!(watch_delay_for(&app), DEFAULT_WATCH_DELAY);
    }

    // ------------------------------------------------------------------
    // The reporter: a report becomes a guarded restart, or nothing.
    // ------------------------------------------------------------------

    // fails if the reporter drops every report, or restarts something other
    // than the id the breach names, or if `handle_extra_restart` stops
    // declaring `CommandOrigin::Automatic`: a process the daemon restarted
    // because it outgrew its `max_memory` would reach every subscriber as
    // `manually: true`, indistinguishable from an operator's `shep restart`.
    #[tokio::test(start_paused = true)]
    async fn a_breach_naming_the_running_pid_restarts_that_sheep() {
        let (handle, mut rx, _fixture) = spawn_test_fixture();
        // The ceiling the synthetic breach below names. Carried on the app
        // rather than left at `None` so the fixture describes a sheep that
        // could really have produced that report: the actor now re-asks a
        // breach against the ceiling in force, and an app configuring none
        // has none in force.
        handle
            .start(vec![app_with("web", |app| {
                app.max_memory = Some(MemSize::from_bytes(500));
            })])
            .await
            .unwrap();
        let pid = handle.list().await[0].pid.expect("a live sheep has a pid");
        let (breach_tx, breach_rx) = mpsc::channel(4);
        let (_live_tx, live_rx) = mpsc::channel(4);
        let _reporter = spawn_extras_reporter(breach_rx, live_rx, handle.clone());

        breach_tx
            .send(LimitBreach {
                id: 0,
                root_pid: pid,
                observed: MemSize::from_bytes(900),
                limit: MemSize::from_bytes(500),
            })
            .await
            .unwrap();

        let (info, manually) = expect_restart_event(&mut rx, "web", EVENT_WAIT).await;
        assert_eq!(info.id, 0);
        assert_eq!(info.restarts, 1);
        assert!(
            !manually,
            "nobody typed this: a memory breach is the daemon's own doing"
        );
    }

    // The liveness twin of the case above, and it needs to exist separately:
    // the reporter's two arms are two `select!` branches, so a `liveness` arm
    // that drops every failure it reads leaves the breach arm — and every
    // assertion riding on it — perfectly green. fails if that arm never calls
    // `extra_restart`, calls it for the wrong id, or restarts the sheep as
    // though a person had asked for it.
    #[tokio::test(start_paused = true)]
    async fn a_liveness_failure_naming_the_running_pid_restarts_that_sheep() {
        let (handle, mut rx, _fixture) = spawn_test_fixture();
        handle.start(vec![app_with("web", |_| {})]).await.unwrap();
        let pid = handle.list().await[0].pid.expect("a live sheep has a pid");
        let (_breach_tx, breach_rx) = mpsc::channel(4);
        let (live_tx, live_rx) = mpsc::channel(4);
        let _reporter = spawn_extras_reporter(breach_rx, live_rx, handle.clone());

        // `spawn_test_fixture` wires no `Extras` at all, so the actor's own
        // registry never arms anything and its epoch for id 0 stays at the
        // default `0` `ExtrasRegistry::liveness_epoch` reports for an id it
        // has never seen. This report is injected straight past the
        // (nonexistent) real probe, so it has to agree with that default
        // rather than with the `1` a real arm would produce.
        live_tx
            .send(LivenessReport {
                id: 0,
                pid,
                epoch: 0,
            })
            .await
            .unwrap();

        let (info, manually) = expect_restart_event(&mut rx, "web", EVENT_WAIT).await;
        assert_eq!(info.id, 0);
        assert_eq!(info.restarts, 1);
        assert!(
            !manually,
            "nobody typed this: a liveness failure is the daemon's own doing"
        );
    }

    // fails if `handle_extra_restart` drops its unknown-id guard for an
    // `.expect(…)`. A report for an id a `Delete` already removed is an
    // ordinary race, not a fault, and it must not take the whole engine down
    // with it — which is what the surviving `list()` proves: a panicked actor
    // closes its mailbox, and `list` panics on the way out.
    #[tokio::test(start_paused = true)]
    async fn an_extra_restart_for_an_unknown_id_leaves_the_engine_running() {
        let (handle, _rx, _fixture) = spawn_test_fixture();
        handle.start(vec![app_with("web", |_| {})]).await.unwrap();

        handle.extra_restart(99, 4242, None, None).await;

        assert_eq!(
            handle.list().await.len(),
            1,
            "the actor must still be serving after a report for an id it does not know"
        );
    }

    // fails against a guard that checks the pid but not the status. A gated
    // app between its spawn and its readiness result is `Starting` with a LIVE
    // pid — the one state in which the pid guard passes and the status guard
    // is the only thing left — so an unguarded command would kill a process
    // that has not finished starting and restart it as though it had.
    //
    // `a_breach_for_a_stopped_sheep_restarts_nothing` cannot make this claim:
    // `handle_exited` nulls `entry.pid` before every terminal transition, so
    // that case rides the pid guard alone.
    #[tokio::test(start_paused = true)]
    async fn an_extra_restart_for_a_sheep_that_is_still_starting_restarts_nothing() {
        let (handle, mut rx, _fixture) = spawn_test_fixture();
        let app = app_with("web", |app| {
            app.wait_ready = true;
            // Long enough that the readiness wait cannot resolve inside this
            // test's own windows, so the sheep stays `Starting` throughout.
            app.listen_timeout = UpDuration::from_millis(6 * 60 * 60 * 1_000);
        });
        handle.start(vec![app]).await.unwrap();
        let listing = handle.list().await;
        assert_eq!(
            listing[0].status,
            ProcStatus::Starting,
            "this case is only meaningful while the sheep is gated on readiness"
        );
        let pid = listing[0].pid.expect("a spawned sheep has a pid");

        handle.extra_restart(0, pid, None, None).await;

        assert_no_restart_within(&mut rx, "web", Duration::from_secs(30)).await;
        assert_eq!(handle.list().await[0].restarts, 0);
    }

    // fails against a reporter that calls the public `restart`:
    // `ProcessSelector::Id` matches regardless of status, so `restart` on a
    // stopped sheep falls to `apply_immediate` and respawns it
    // unconditionally — resurrecting a process the user explicitly stopped,
    // and reporting success. The runner is scripted with spare procs on
    // purpose: that resurrection needs something to spawn from, and a fixture
    // sized to the happy path would let this test pass against the broken
    // implementation for the wrong reason (an exhausted script makes the
    // supervisor emit `Errored`, which no assertion here is watching for).
    #[tokio::test(start_paused = true)]
    async fn a_breach_for_a_stopped_sheep_restarts_nothing() {
        let (handle, mut rx, _fixture) = spawn_test_fixture();
        handle.start(vec![app_with("web", |_| {})]).await.unwrap();
        let pid = handle.list().await[0].pid.expect("a live sheep has a pid");
        let (breach_tx, breach_rx) = mpsc::channel(4);
        let (_live_tx, live_rx) = mpsc::channel(4);
        let _reporter = spawn_extras_reporter(breach_rx, live_rx, handle.clone());

        handle
            .stop(ProcessSelector::Id(0))
            .await
            .expect("the sheep stops");
        breach_tx
            .send(LimitBreach {
                id: 0,
                root_pid: pid,
                observed: MemSize::from_bytes(900),
                limit: MemSize::from_bytes(500),
            })
            .await
            .unwrap();

        assert_no_restart_within(&mut rx, "web", Duration::from_secs(30)).await;
        let listing = handle.list().await;
        assert_eq!(listing[0].status, ProcStatus::Stopped);
        assert_eq!(listing[0].restarts, 0);
    }

    // fails against a guard that checks status but not pid: a breach raised
    // for the process a restart already replaced would restart its healthy
    // successor and reset that successor's budget. The control half at the
    // end — the SAME reporter, fed the CURRENT pid, really does restart — is
    // what proves the negative half could have failed.
    #[tokio::test(start_paused = true)]
    async fn a_breach_carrying_the_previous_pid_restarts_nothing() {
        let (handle, mut rx, _fixture) = spawn_test_fixture();
        // The ceiling both synthetic breaches below name; see the case above
        // for why the fixture carries it.
        handle
            .start(vec![app_with("web", |app| {
                app.max_memory = Some(MemSize::from_bytes(500));
            })])
            .await
            .unwrap();
        let stale_pid = handle.list().await[0].pid.expect("a live sheep has a pid");
        let (breach_tx, breach_rx) = mpsc::channel(4);
        let (_live_tx, live_rx) = mpsc::channel(4);
        let _reporter = spawn_extras_reporter(breach_rx, live_rx, handle.clone());

        handle
            .restart(ProcessSelector::Id(0))
            .await
            .expect("the sheep restarts");
        expect_restart(&mut rx, "web", EVENT_WAIT).await;
        let live_pid = handle.list().await[0].pid.expect("a live sheep has a pid");
        assert_ne!(stale_pid, live_pid, "the respawn must have a new pid");

        let breach = |root_pid| LimitBreach {
            id: 0,
            root_pid,
            observed: MemSize::from_bytes(900),
            limit: MemSize::from_bytes(500),
        };
        breach_tx.send(breach(stale_pid)).await.unwrap();
        assert_no_restart_within(&mut rx, "web", Duration::from_secs(30)).await;
        assert_eq!(
            handle.list().await[0].restarts,
            1,
            "a stale breach must not bump the restart count"
        );

        breach_tx.send(breach(live_pid)).await.unwrap();
        let info = expect_restart(&mut rx, "web", EVENT_WAIT).await;
        assert_eq!(
            info.restarts, 2,
            "the same reporter, fed the current pid, must restart"
        );
    }

    // ------------------------------------------------------------------
    // The actor tier: that the engine really arms and disarms at the
    // transitions, not just that the registry can.
    // ------------------------------------------------------------------

    // fails if the actor never disarms on a clean stop: the cron worker keeps
    // its schedule, `ProcessSelector::Name` matches a stopped sheep too, and
    // the next occurrence brings back a sheep the user stopped. The cron
    // restart observed first is what makes the silence afterwards able to
    // fail.
    #[tokio::test(start_paused = true)]
    async fn stopping_the_last_instance_stops_its_cron_worker() {
        let clock = Arc::new(TestClock::starting_at(dt("2026-01-01T00:00:00Z")));
        let h = harness_with_extras(vec![ProcScript::never_exits(); 12], |reports| Extras {
            clock: Arc::clone(&clock) as Arc<dyn Clock>,
            enforcer: Arc::new(RecordingEnforcer::default()),
            max_cron_sleep: DEFAULT_MAX_CRON_SLEEP,
            reports,
            stats: idle_stats(),
        });
        let mut rx = h.ctx.events.subscribe();
        h.ctx
            .supervisor
            .start(vec![app_with("web", |app| {
                app.cron_restart = Some("0 * * * *".to_string());
            })])
            .await
            .unwrap();

        cross_one_hour().await;
        expect_restart(&mut rx, "web", EVENT_WAIT).await;

        h.ctx
            .supervisor
            .stop(ProcessSelector::Name("web".to_string()))
            .await
            .expect("the sheep stops");
        assert_no_restart_within(&mut rx, "web", PAST_THE_NEXT_OCCURRENCE).await;
        assert_eq!(h.ctx.supervisor.list().await[0].status, ProcStatus::Stopped);
    }

    /// Waits up to `window` for `kind` naming `id`, panicking if none arrives.
    ///
    /// [`expect_restart`]'s shape, keyed by id and kind rather than by name: a
    /// swap puts two entries under ONE name, so a name alone cannot say which
    /// half of it an event is about.
    async fn expect_process_event(
        rx: &mut broadcast::Receiver<SharedEvent>,
        id: u32,
        kind: ProcessEventKind,
        window: Duration,
    ) {
        let wanted = async {
            loop {
                match rx.recv().await.map(|event| event.to_event()) {
                    Ok(BusEvent::Process { event, info, .. }) if event == kind && info.id == id => {
                        return;
                    }
                    Ok(_) => continue,
                    Err(broadcast::error::RecvError::Lagged(_)) => continue,
                    Err(err) => panic!("event stream closed before {kind:?} for id {id}: {err}"),
                }
            }
        };
        if tokio::time::timeout(window, wanted).await.is_err() {
            panic!("timed out waiting for {kind:?} for id {id}");
        }
    }

    // fails if the swap's own ordering inverts: the drainee's extras disarmed
    // before the replacement arms, which is what "drain first, spawn second" —
    // the shape a rolling restart takes — would produce. Every reload would
    // then tear the name group down and put it back, and `arm`'s doc says what
    // that costs: re-registering the OS watcher each time, a step that can fail
    // and whose failure silently leaves an app without the watch that restarts
    // it.
    //
    // The registry case above says the registry HONOURS that ordering; this
    // one says the engine still PERFORMS it. Neither implies the other, and
    // the ordering itself lives in the swap rather than in the registry.
    //
    // The clock reading is the observation, and it is the only one that
    // survives out here. A torn-down-and-rebuilt group is otherwise identical
    // from the actor's outside — it fires on the same schedule, and the
    // registry's own fields are private to this module's other tier — but a
    // rebuild re-spawns the cron worker, and `spawn_cron_worker` reads the wall
    // clock on its first poll to derive its next occurrence. One reading is
    // therefore the difference between a group that was rebuilt and one that
    // was left alone.
    //
    // `max_cron_sleep` is 600s against a swap costing at most `listen_timeout`
    // (3s) plus `graceful_timeout` (8s) of virtual time, so the SURVIVING
    // worker cannot wake inside the window and take a reading of its own —
    // which would read exactly like the rebuild being watched for.
    #[tokio::test(start_paused = true)]
    async fn a_reload_leaves_the_name_groups_cron_worker_where_it_was() {
        let clock = Arc::new(TestClock::starting_at(dt("2026-01-01T00:00:00Z")));
        // Two procs, counted: the original and the one replacement a reload of
        // a one-instance app performs. A third would be answered "script
        // exhausted", which abandons the reload — no overlap, and a clock
        // count that proves nothing.
        let h = harness_with_extras(vec![ProcScript::never_exits(); 2], |reports| Extras {
            clock: Arc::clone(&clock) as Arc<dyn Clock>,
            enforcer: Arc::new(RecordingEnforcer::default()),
            max_cron_sleep: Duration::from_secs(600),
            reports,
            stats: idle_stats(),
        });
        let mut rx = h.ctx.events.subscribe();
        h.ctx
            .supervisor
            .start(vec![app_with("web", |app| {
                app.cron_restart = Some("0 * * * *".to_string());
            })])
            .await
            .unwrap();
        expect_process_event(&mut rx, 0, ProcessEventKind::Online, EVENT_WAIT).await;
        // Lets the armed worker reach its first poll before the count is taken.
        tokio::task::yield_now().await;

        let reads_before = clock.reads();
        assert_eq!(
            reads_before, 1,
            "fixture check: one armed cron worker takes exactly one reading, \
             so a rebuild's second one is a countable difference rather than \
             noise — and a count of 0 here would mean the worker had not run \
             yet, which would make the claim below vacuous"
        );

        h.ctx
            .supervisor
            .reload(ProcessSelector::Name("web".to_string()))
            .await
            .expect("the reload is accepted");
        expect_process_event(&mut rx, 1, ProcessEventKind::Online, EVENT_WAIT).await;
        expect_process_event(&mut rx, 0, ProcessEventKind::Delete, EVENT_WAIT).await;

        let listed = h.ctx.supervisor.list().await;
        assert_eq!(
            listed.iter().map(|info| info.id).collect::<Vec<_>>(),
            vec![1],
            "fixture check: the swap must have run to completion, or there was \
             never an overlap for the ordering to matter in"
        );
        assert_eq!(
            clock.reads(),
            reads_before,
            "the name group's cron worker must have been left where it was: a \
             rebuilt one reads the clock again to derive its next occurrence"
        );
    }

    /// A harness whose scripted runner hands out one proc that exits at once
    /// and then plenty that never do, plus a cron-restarting app that parks in
    /// a long backoff after any exit.
    ///
    /// The two cases below both need a sheep sitting in `WaitingRestart` — the
    /// one state that reaches a terminal transition through `apply_immediate`
    /// rather than `handle_exited`, because its process is already gone.
    fn backoff_harness(clock: &Arc<TestClock>) -> Harness {
        harness_with_extras(
            {
                let mut scripts = vec![ProcScript::never_exits()];
                scripts.push(ProcScript::const_exit(1));
                // Spare procs so a BROKEN implementation has something to
                // respawn from: without them the scripted runner reports
                // "script exhausted", the supervisor emits `Errored` instead
                // of `Restart`, and the negative assertions below would pass
                // against a completely unarmed disarm.
                scripts.extend([ProcScript::never_exits(); 8]);
                scripts
            },
            |reports| Extras {
                clock: Arc::clone(clock) as Arc<dyn Clock>,
                enforcer: Arc::new(RecordingEnforcer::default()),
                max_cron_sleep: DEFAULT_MAX_CRON_SLEEP,
                reports,
                stats: idle_stats(),
            },
        )
    }

    /// The cron-restarting app the two backoff cases start, parked in a
    /// backoff far longer than either case's own window so its pending
    /// `RestartDue` can never be what a restart came from.
    fn backoff_app() -> shep_core::config::ResolvedApp {
        app_with("web", |app| {
            app.cron_restart = Some("0 * * * *".to_string());
            app.restart_delay = Some(UpDuration::from_millis(3 * 60 * 60 * 1_000));
        })
    }

    /// Drives the actor until `id` reports `status`, panicking if it never
    /// does. Each `list()` is a full round trip through the actor's mailbox,
    /// so this makes progress rather than merely observing it.
    async fn settle_into(supervisor: &SupervisorHandle, id: u32, status: ProcStatus) {
        for _ in 0..200 {
            tokio::task::yield_now().await;
            let listing = supervisor.list().await;
            if listing
                .iter()
                .any(|info| info.id == id && info.status == status)
            {
                return;
            }
        }
        panic!("id {id} never reached {status:?}");
    }

    // fails if `apply_immediate`'s Stop arm does not disarm. A sheep waiting
    // out its restart backoff has no live task, so its stop never reaches
    // `handle_exited`'s terminal branches at all — and `ProcessSelector::Name`
    // matches a stopped sheep just as happily, so the next occurrence brings
    // back a sheep the user stopped. The cron restart observed first (the one
    // that lands the sheep in the backoff) is what makes the silence
    // afterwards able to fail.
    #[tokio::test(start_paused = true)]
    async fn stopping_a_sheep_mid_backoff_still_stops_its_cron_worker() {
        let clock = Arc::new(TestClock::starting_at(dt("2026-01-01T00:00:00Z")));
        let h = backoff_harness(&clock);
        let mut rx = h.ctx.events.subscribe();
        h.ctx.supervisor.start(vec![backoff_app()]).await.unwrap();

        // The cron occurrence restarts it onto the immediately-exiting proc,
        // which lands it in its three-hour backoff.
        cross_one_hour().await;
        expect_restart(&mut rx, "web", EVENT_WAIT).await;
        settle_into(&h.ctx.supervisor, 0, ProcStatus::WaitingRestart).await;

        h.ctx
            .supervisor
            .stop(ProcessSelector::Name("web".to_string()))
            .await
            .expect("the sheep stops");
        assert_eq!(h.ctx.supervisor.list().await[0].status, ProcStatus::Stopped);
        // Spans the next occurrence, and stays well inside the three-hour
        // backoff, so a restart arriving here could only be the cron worker.
        assert_no_restart_within(&mut rx, "web", PAST_THE_NEXT_OCCURRENCE).await;
    }

    // fails if `apply_immediate`'s Delete arm does not disarm: the slot is
    // deregistered, but the name-group's cron worker keeps firing at a name
    // that no longer exists — a task that outlives the flock it belonged to.
    #[tokio::test(start_paused = true)]
    async fn deleting_a_sheep_mid_backoff_takes_its_cron_worker_with_it() {
        let clock = Arc::new(TestClock::starting_at(dt("2026-01-01T00:00:00Z")));
        let h = backoff_harness(&clock);
        let mut rx = h.ctx.events.subscribe();
        h.ctx.supervisor.start(vec![backoff_app()]).await.unwrap();

        cross_one_hour().await;
        expect_restart(&mut rx, "web", EVENT_WAIT).await;
        settle_into(&h.ctx.supervisor, 0, ProcStatus::WaitingRestart).await;

        h.ctx
            .supervisor
            .delete(ProcessSelector::Name("web".to_string()))
            .await
            .expect("the sheep is deleted");
        assert!(h.ctx.supervisor.list().await.is_empty());
        // A surviving worker would `restart(Name("web"))`, find nothing, and
        // log at debug — no bus event. What it CAN still do is re-register
        // nothing while running forever, so the observable claim is the
        // clock: a live worker keeps reading it every `max_cron_sleep`.
        let reads_after_delete = clock.reads();
        assert_no_restart_within(&mut rx, "web", PAST_THE_NEXT_OCCURRENCE).await;
        assert_eq!(
            clock.reads(),
            reads_after_delete,
            "a deleted sheep's cron worker must stop reading the clock, not merely stop finding sheep"
        );
    }

    // A cron occurrence reaches a name-group's instances through two doors,
    // and only one of them ever had a test. A RUNNING instance is killed and
    // respawned from `handle_exited`'s forced-restart branch; one already
    // sitting out its restart backoff has no live task, so the same occurrence
    // restarts it from `apply_immediate` instead. Both have to say the same
    // thing about who caused it, and neither answer is "a person".
    //
    // fails if either respawn site hardcodes the `manually` flag it emits, or
    // if the cron worker reaches for the operator's `restart` in place of
    // `restart_automatic`. `BusEvent::Process`'s `manually` is documented as
    // "True when a user action caused it", so a subscriber cannot otherwise
    // tell a nightly schedule apart from someone deploying.
    #[tokio::test(start_paused = true)]
    async fn a_cron_restart_is_never_reported_as_a_user_action() {
        let clock = Arc::new(TestClock::starting_at(dt("2026-01-01T00:00:00Z")));
        // The same fixture the two cases above stand on, and its script pool
        // is what makes the second half reachable rather than incidental: the
        // first occurrence respawns onto a proc that exits at once, and the
        // spares behind it are what the second occurrence respawns from. A
        // pool sized to a correct run would answer that second spawn
        // `SpawnFailed("script exhausted")` and emit `Errored`, which carries
        // no `Restart` for either implementation to be judged on.
        let h = backoff_harness(&clock);
        let mut rx = h.ctx.events.subscribe();
        h.ctx.supervisor.start(vec![backoff_app()]).await.unwrap();

        // Door one: the sheep is online, so the occurrence takes it through
        // the kill ladder and `handle_exited` performs the respawn.
        cross_one_hour().await;
        let (running, manually) = expect_restart_event(&mut rx, "web", EVENT_WAIT).await;
        assert_eq!(running.restarts, 1);
        assert!(
            !manually,
            "a cron occurrence is nobody typing `shep restart`"
        );

        // Door two: that respawn exited immediately into a three-hour backoff,
        // so the next occurrence — an hour later, well inside it — finds no
        // live task and restarts the sheep from `apply_immediate`.
        settle_into(&h.ctx.supervisor, 0, ProcStatus::WaitingRestart).await;
        cross_one_hour().await;
        let (backing_off, manually) = expect_restart_event(&mut rx, "web", EVENT_WAIT).await;
        assert_eq!(backing_off.restarts, 2);
        assert!(
            !manually,
            "the same occurrence must answer the same way through `apply_immediate`"
        );
    }

    // fails if `respawn`'s Err arm does not disarm. It lands the sheep in the
    // same `Errored` status `Decision::Errored` does, and is reachable in an
    // ordinary deploy — a cron occurrence (or a crash-restart, or a manual
    // one) whose respawn cannot spawn, because the binary was replaced
    // mid-deploy or the cwd is gone. Without the disarm the name group's cron
    // worker stays live against a sheep that will never come back.
    //
    // The clock is the observable claim, exactly as in the delete case above:
    // a failed respawn emits `Errored`, never `Restart`, so a surviving worker
    // leaves no bus event behind — only its every-`max_cron_sleep` reading.
    #[tokio::test(start_paused = true)]
    async fn a_respawn_that_cannot_spawn_stops_the_name_groups_cron_worker() {
        let clock = Arc::new(TestClock::starting_at(dt("2026-01-01T00:00:00Z")));
        // EXACTLY one script, and that is the point: the initial start
        // consumes it, so the cron occurrence's respawn finds the script
        // exhausted, `ScriptedRunner` reports `SpawnFailed`, and `respawn`
        // takes its Err arm.
        let h = harness_with_extras(vec![ProcScript::never_exits()], |reports| Extras {
            clock: Arc::clone(&clock) as Arc<dyn Clock>,
            enforcer: Arc::new(RecordingEnforcer::default()),
            max_cron_sleep: DEFAULT_MAX_CRON_SLEEP,
            reports,
            stats: idle_stats(),
        });
        let mut rx = h.ctx.events.subscribe();
        h.ctx
            .supervisor
            .start(vec![app_with("web", |app| {
                app.cron_restart = Some("0 * * * *".to_string());
            })])
            .await
            .unwrap();

        cross_one_hour().await;
        settle_into(&h.ctx.supervisor, 0, ProcStatus::Errored).await;

        let reads_after_error = clock.reads();
        assert_no_restart_within(&mut rx, "web", PAST_THE_NEXT_OCCURRENCE).await;
        assert_eq!(
            clock.reads(),
            reads_after_error,
            "an errored sheep's cron worker must stop reading the clock"
        );
    }

    // fails if the actor never arms the enforcer at the Online transition, or
    // arms it with the sheep's id where its pid belongs: the scripted table
    // holds exactly one process, the pid the scripted runner hands the first
    // spawn, so an arming against any other number sums to zero and never
    // breaches however long the test waits.
    #[tokio::test(start_paused = true)]
    async fn the_actor_arms_the_memory_limit_against_the_spawned_pid() {
        let mut h = harness_with_extras(vec![ProcScript::never_exits(); 4], |reports| {
            let sampler: Arc<dyn MemorySampler> =
                Arc::new(ScriptedSampler::new(vec![vec![ProcessRss {
                    pid: 1000,
                    parent: None,
                    bytes: 900,
                    cpu_ms: 0,
                }]]));
            let stats = Arc::new(StatsState::new(Arc::clone(&sampler)));
            Extras {
                clock: Arc::new(TestClock::starting_at(dt("2026-01-01T00:00:00Z"))),
                enforcer: Arc::new(PollingEnforcer::start(
                    sampler,
                    reports.breaches.clone(),
                    Arc::clone(&stats),
                )),
                max_cron_sleep: DEFAULT_MAX_CRON_SLEEP,
                reports,
                stats,
            }
        });
        h.ctx
            .supervisor
            .start(vec![app_with("web", |app| {
                app.max_memory = Some(MemSize::from_bytes(500));
            })])
            .await
            .unwrap();
        let pid = h.ctx.supervisor.list().await[0]
            .pid
            .expect("a live sheep has a pid");

        let breach = match tokio::time::timeout(EVENT_WAIT, h.breaches.recv()).await {
            Ok(Some(breach)) => breach,
            Ok(None) => panic!("the breach channel closed before a breach arrived"),
            Err(_) => panic!("timed out waiting for a breach"),
        };

        assert_eq!(breach.id, 0);
        assert_eq!(
            breach.root_pid, pid,
            "the enforcer must be armed against the pid the sheep is actually running as"
        );
        assert_eq!(breach.observed.bytes(), 900);
    }

    // fails if the readiness-GATED transition to `Online` does not arm.
    // `arm_extras` is reached from three transitions and only the two ungated
    // ones were proven; reverting `handle_ready_result`'s `went_online` call
    // to a plain `emit` leaves every other case in this file green while every
    // app that configures readiness silently loses all four of its extras.
    //
    // The readiness wait ends in a timeout rather than a signal — a scripted
    // proc writes no `{"kind":"ready"}` — which is the same `Online` and the
    // same arming site. The `Starting` assertion is what keeps this case from
    // quietly degrading into a second copy of the ungated one above.
    #[tokio::test(start_paused = true)]
    async fn the_actor_arms_a_readiness_gated_app_once_it_comes_online() {
        let mut h = harness_with_extras(vec![ProcScript::never_exits(); 4], |reports| {
            let sampler: Arc<dyn MemorySampler> =
                Arc::new(ScriptedSampler::new(vec![vec![ProcessRss {
                    pid: 1000,
                    parent: None,
                    bytes: 900,
                    cpu_ms: 0,
                }]]));
            let stats = Arc::new(StatsState::new(Arc::clone(&sampler)));
            Extras {
                clock: Arc::new(TestClock::starting_at(dt("2026-01-01T00:00:00Z"))),
                enforcer: Arc::new(PollingEnforcer::start(
                    sampler,
                    reports.breaches.clone(),
                    Arc::clone(&stats),
                )),
                max_cron_sleep: DEFAULT_MAX_CRON_SLEEP,
                reports,
                stats,
            }
        });
        h.ctx
            .supervisor
            .start(vec![app_with("web", |app| {
                app.wait_ready = true;
                app.max_memory = Some(MemSize::from_bytes(500));
            })])
            .await
            .unwrap();
        let listing = h.ctx.supervisor.list().await;
        assert_eq!(
            listing[0].status,
            ProcStatus::Starting,
            "a gated app must not be Online when its Start reply lands"
        );
        let pid = listing[0].pid.expect("a spawned sheep has a pid");

        let breach = match tokio::time::timeout(EVENT_WAIT, h.breaches.recv()).await {
            Ok(Some(breach)) => breach,
            Ok(None) => panic!("the breach channel closed before a breach arrived"),
            Err(_) => panic!("timed out waiting for a breach"),
        };
        assert_eq!(breach.id, 0);
        assert_eq!(
            breach.root_pid, pid,
            "the gated path must arm against the pid the sheep is running as"
        );
    }

    // fails if the actor never arms the liveness loop at the Online
    // transition, or arms it with the wrong id or pid.
    //
    // Real time and a real `OsProber`, because that is what the actor builds
    // — the paused clock does not move a real TCP connect.
    #[tokio::test]
    async fn the_actor_arms_the_liveness_loop_against_the_spawned_pid() {
        // 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 mut h = harness(vec![ProcScript::never_exits(); 4]);
        h.ctx
            .supervisor
            .start(vec![app_with("web", |app| {
                app.liveness_probe = Some(ProbeConfig {
                    failure_threshold: 1,
                    interval: PROBE_INTERVAL,
                    timeout: UpDuration::from_millis(500),
                    ..probe_config(ProbeKind::Tcp, &addr.to_string())
                });
            })])
            .await
            .unwrap();
        let pid = h.ctx.supervisor.list().await[0]
            .pid
            .expect("a live sheep has a pid");

        let failure = expect_liveness(&mut h.liveness, LIVENESS_DEADLINE).await;
        assert_eq!(
            failure,
            LivenessReport {
                id: 0,
                pid,
                epoch: 1
            }
        );
    }

    /// fails if re-arming leaves the old group tasks running. `arm` deliberately
    /// keeps a live cron or watch task, which is right for a reload's overlap and
    /// wrong for a config change: those tasks read `watch`, `ignore_watch`,
    /// `watch_delay`, `watch_options`, `cron_restart` and `cron_timezone` when
    /// they are BUILT, so a task that survives keeps the old values forever.
    #[tokio::test(start_paused = true)]
    async fn rearm_name_replaces_a_live_group_task() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let root = tempfile::tempdir().unwrap();
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.cron_restart = Some("0 * * * *".to_string());
            app.watch = true;
            app.cwd = Some(root.path().display().to_string());
        });
        handle.start(vec![app.clone()]).await.unwrap();

        let entry = armed_entry(0, 0, 1000, app.clone(), &paths);
        registry.arm(&entry, idle_prober(), &rig.extras, &handle);
        tokio::task::yield_now().await;

        let before_cron = registry.groups["web"].cron.as_ref().unwrap().abort_handle();
        let before_watch = registry.groups["web"]
            .watch
            .as_ref()
            .unwrap()
            .abort_handle();

        registry.rearm_name("web", &[&entry], |_| idle_prober(), &rig.extras, &handle);
        tokio::task::yield_now().await;

        let after_cron = registry.groups["web"].cron.as_ref().unwrap().abort_handle();
        let after_watch = registry.groups["web"]
            .watch
            .as_ref()
            .unwrap()
            .abort_handle();
        assert_ne!(
            before_cron.id(),
            after_cron.id(),
            "the cron worker survived a rearm"
        );
        assert_ne!(
            before_watch.id(),
            after_watch.id(),
            "the watch task survived a rearm"
        );
    }

    /// fails if rearm_name tears a group down without rebuilding it. An app left
    /// with no watcher at all is worse than one left with a stale watcher.
    #[tokio::test(start_paused = true)]
    async fn rearm_name_leaves_the_group_armed() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let root = tempfile::tempdir().unwrap();
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.cron_restart = Some("0 * * * *".to_string());
            app.watch = true;
            app.cwd = Some(root.path().display().to_string());
        });
        handle.start(vec![app.clone()]).await.unwrap();

        let entry = armed_entry(0, 0, 1000, app.clone(), &paths);
        registry.arm(&entry, idle_prober(), &rig.extras, &handle);
        tokio::task::yield_now().await;

        registry.rearm_name("web", &[&entry], |_| idle_prober(), &rig.extras, &handle);
        tokio::task::yield_now().await;

        let group = &registry.groups["web"];
        assert!(
            group.cron.as_ref().is_some_and(|cron| !cron.is_finished()),
            "the group must still have a live cron worker after a rearm"
        );
        assert!(
            group
                .watch
                .as_ref()
                .is_some_and(|watch| !watch.is_finished()),
            "the group must still have a live watch task after a rearm"
        );
    }

    /// fails if rearm_name reaches into another app's group. The registry is
    /// keyed by name and a rebuild stays inside one name.
    #[tokio::test(start_paused = true)]
    async fn rearm_name_leaves_another_apps_group_alone() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let web_root = tempfile::tempdir().unwrap();
        let worker_root = tempfile::tempdir().unwrap();
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let web = app_with("web", |app| {
            app.watch = true;
            app.cwd = Some(web_root.path().display().to_string());
        });
        let worker = app_with("worker", |app| {
            app.watch = true;
            app.cwd = Some(worker_root.path().display().to_string());
        });
        handle
            .start(vec![web.clone(), worker.clone()])
            .await
            .unwrap();

        let web_entry = armed_entry(0, 0, 1000, web.clone(), &paths);
        let worker_entry = armed_entry(1, 0, 1001, worker.clone(), &paths);
        registry.arm(&web_entry, idle_prober(), &rig.extras, &handle);
        registry.arm(&worker_entry, idle_prober(), &rig.extras, &handle);
        tokio::task::yield_now().await;

        let worker_watch_before = registry.groups["worker"]
            .watch
            .as_ref()
            .unwrap()
            .abort_handle();

        registry.rearm_name(
            "web",
            &[&web_entry],
            |_| idle_prober(),
            &rig.extras,
            &handle,
        );
        tokio::task::yield_now().await;

        let worker_watch_after = registry.groups["worker"]
            .watch
            .as_ref()
            .unwrap()
            .abort_handle();
        assert_eq!(
            worker_watch_before.id(),
            worker_watch_after.id(),
            "rearming \"web\" must not touch \"worker\"'s group"
        );
    }

    /// fails if `rearm_name`'s delegation to `arm` per entry stops sharing
    /// one group across a name's own instances. Today that sharing is only
    /// TRANSITIVELY protected, by `arm`'s own idempotency test, because
    /// `rearm_name` never builds a task itself; a future refactor that stops
    /// delegating and builds per entry instead would orphan a multi-instance
    /// app's membership with nothing in this diff to catch it. Task 8 calls
    /// `rearm_name` with every entry of a name, so this is the path any app
    /// running more than one instance actually exercises.
    #[tokio::test(start_paused = true)]
    async fn rearm_name_rebuilds_a_multi_instance_group_exactly_once() {
        let dir = tempfile::tempdir().unwrap();
        let paths = test_paths(&dir);
        let root = tempfile::tempdir().unwrap();
        let (handle, _rx, _fixture) = spawn_test_fixture();
        let rig = rig(DEFAULT_MAX_CRON_SLEEP);
        let mut registry = ExtrasRegistry::default();
        let app = app_with("web", |app| {
            app.instances = 2;
            app.cron_restart = Some("0 * * * *".to_string());
            app.watch = true;
            app.cwd = Some(root.path().display().to_string());
        });
        handle.start(vec![app.clone()]).await.unwrap();

        let entry_a = armed_entry(0, 0, 1000, app.clone(), &paths);
        let entry_b = armed_entry(1, 1, 1001, app.clone(), &paths);
        registry.arm(&entry_a, idle_prober(), &rig.extras, &handle);
        registry.arm(&entry_b, idle_prober(), &rig.extras, &handle);
        tokio::task::yield_now().await;

        let before_cron = registry.groups["web"].cron.as_ref().unwrap().abort_handle();
        let before_watch = registry.groups["web"]
            .watch
            .as_ref()
            .unwrap()
            .abort_handle();

        // The prober closure is also the seam that pins ONE PROBER PER
        // ENTRY. `assemble` bakes `SHEP_INSTANCE` and every `{{instance}}`
        // template into the environment a prober runs with, so a shared
        // prober would run every instance's liveness probe against a single
        // instance's environment. Nothing observes which environment an
        // `OsProber` ended up holding, but the number of calls and their
        // order are observable right here.
        let probed = std::sync::Mutex::new(Vec::new());
        registry.rearm_name(
            "web",
            &[&entry_a, &entry_b],
            |entry| {
                probed
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner)
                    .push(entry.id);
                idle_prober()
            },
            &rig.extras,
            &handle,
        );
        tokio::task::yield_now().await;

        assert_eq!(
            probed
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner)
                .as_slice(),
            [0, 1],
            "each instance must get its own prober, in id order"
        );

        let group = &registry.groups["web"];
        assert_eq!(
            group.members,
            HashSet::from([0, 1]),
            "both instances must still be members after a rearm, not just the last one arm'd"
        );
        assert!(
            group.cron.is_some(),
            "the group must hold exactly one cron task, not zero"
        );
        assert!(
            group.watch.is_some(),
            "the group must hold exactly one watch task, not zero"
        );
        let after_cron = group.cron.as_ref().unwrap().abort_handle();
        let after_watch = group.watch.as_ref().unwrap().abort_handle();
        assert_ne!(
            before_cron.id(),
            after_cron.id(),
            "the cron worker must be rebuilt by the rearm, not left over"
        );
        assert_ne!(
            before_watch.id(),
            after_watch.id(),
            "the watch task must be rebuilt by the rearm, not left over"
        );
    }

    /// Tests that wait on real filesystem events or real elapsed time.
    ///
    /// The inner loop skips this module with `--skip ::slow::`; the full
    /// suite still runs them because nothing here is `#[ignore]`d.
    mod slow {
        use super::*;

        // The overlap a reload runs on. A replacement takes a NEW id in the
        // drainee's instance slot and arms BEFORE the drainee's exit disarms the
        // old id, so `disarm` finds a member still standing and the name group's
        // two tasks are never touched. Nothing in this type states that ordering
        // or enforces it — it belongs to the sequence the swap runs — so it is
        // pinned here, at the tier where "the same worker" is a thing that can be
        // said out loud.
        //
        // WHAT DISTINGUISHES A REBUILD FROM A SURVIVAL. A group torn down and put
        // straight back is otherwise INDISTINGUISHABLE from one never touched:
        // `arm` re-spawns a cron worker on the same schedule and re-registers a
        // watcher on the same root, both succeed in a test environment, and
        // `groups["web"].cron.is_some()`, the member set below, and every restart
        // the worker goes on to fire read identically under both orderings. The
        // observation that does tell them apart is TASK IDENTITY: `JoinHandle::id`
        // names one spawned task, and a rebuilt group's tasks are different ones.
        //
        // The two `AbortHandle`s are what make that identity airtight rather than
        // merely likely. tokio allows a task id to be reused once the task has
        // exited AND no handle to it is left alive — which is exactly the state a
        // teardown produces, since `NameExtras::abort` aborts both tasks and drops
        // both handles. Holding an `AbortHandle` keeps the id reserved for the
        // ORIGINAL task for the whole test, so a rebuilt task cannot be handed the
        // same one and pass this by coincidence. They are held and never fired.
        //
        // The clock count is a second, independent half, and it is what stands if
        // tokio's id semantics ever move: `spawn_cron_worker` reads the wall clock
        // once on its first poll to derive its next occurrence, so a rebuild costs
        // a reading that a survival does not. That is a claim about work performed
        // rather than about identity.
        //
        // fails if `arm` stops leaving a live name-group task alone — the
        // `is_none_or(is_finished)` guard dropped, so every re-arm re-registers the
        // OS watcher, a step that can fail and whose failure silently costs an app
        // the watch that restarts it — and fails if `disarm` stops keying its
        // teardown on the member set emptying.
        #[tokio::test(start_paused = true)]
        async fn a_replacement_arming_before_the_drainee_disarms_keeps_the_groups_own_tasks() {
            let dir = tempfile::tempdir().unwrap();
            let paths = test_paths(&dir);
            let root = tempfile::tempdir().unwrap();
            let (handle, _rx, _fixture) = spawn_test_fixture();
            let rig = rig(DEFAULT_MAX_CRON_SLEEP);
            let mut registry = ExtrasRegistry::default();
            // Both per-name extras, because the overlap has to hold for both — and
            // the watch is the one whose rebuild `arm`'s own doc calls out as
            // costly rather than merely wasteful.
            let app = app_with("web", |app| {
                app.cron_restart = Some("0 * * * *".to_string());
                app.watch = true;
                app.cwd = Some(root.path().display().to_string());
            });
            handle.start(vec![app.clone()]).await.unwrap();

            // The drainee: id 0, holding instance slot 0.
            registry.arm(
                &armed_entry(0, 0, 1000, app.clone(), &paths),
                idle_prober(),
                &rig.extras,
                &handle,
            );
            // Lets the cron worker reach its first poll, so the reading taken
            // below is a settled number rather than a race with it.
            tokio::task::yield_now().await;

            let group = &registry.groups["web"];
            let cron = group
                .cron
                .as_ref()
                .expect("fixture check: the cron worker must have armed")
                .abort_handle();
            let watch = group
                .watch
                .as_ref()
                .expect("fixture check: the watch must have armed")
                .abort_handle();
            let reads_before = rig.clock.reads();

            // The overlap, in the order a swap performs it: the replacement takes
            // a new id in the drainee's instance slot and goes `Online` first, and
            // only then does the drainee's exit disarm the old id.
            registry.arm(
                &armed_entry(1, 0, 2000, app, &paths),
                idle_prober(),
                &rig.extras,
                &handle,
            );
            registry.disarm(0, "web");
            // Lets a REBUILT worker reach its own first poll. Without this the
            // clock count below could read unchanged because the replacement's
            // worker had not run yet, rather than because there is no such worker.
            tokio::task::yield_now().await;

            let group = &registry.groups["web"];
            assert_eq!(
                group.members,
                HashSet::from([1]),
                "fixture check: the drainee must really have left a group the \
             replacement had already joined — this reads the same under either \
             ordering, which is why it cannot be the claim that matters"
            );
            assert_eq!(
                group.cron.as_ref().map(JoinHandle::id),
                Some(cron.id()),
                "the group must still hold the cron worker it was armed with, not \
             an identical one put back in its place"
            );
            assert_eq!(
                group.watch.as_ref().map(JoinHandle::id),
                Some(watch.id()),
                "and the watch it was armed with, whose rebuild means re-registering \
             the OS watcher"
            );
            assert_eq!(
                rig.clock.reads(),
                reads_before,
                "a surviving cron worker performs no startup work; a rebuilt one \
             reads the clock again to derive its next occurrence"
            );
        }

        // Boundary sweep (IR-40): a name group with zero online instances —
        // armed and disarmed again before it ever gets to do anything.
        //
        // The nearest existing case,
        // `only_the_last_instance_leaving_stops_the_name_groups_cron_worker`,
        // reaches its teardown through two members and a fired occurrence in
        // between, so it says nothing about a group that empties before its first
        // one. That window is not hypothetical: an app whose first spawn is
        // stopped or deleted straight away — a bad deploy, a `shep start` the
        // operator immediately reverses — passes through exactly this shape, and
        // a worker leaked there restarts a flock nobody is running.
        //
        // The assertions before the disarm are what keep the silence afterwards
        // from being vacuous: they say a real cron worker and a real OS watcher
        // were armed and a real enforcer arming existed, so the quiet below is a
        // teardown rather than an app that never armed anything.
        //
        // The negative half is `assert_no_restart_within` over
        // `PAST_THE_NEXT_OCCURRENCE`, the bounded window Global Constraints rule
        // 11 asks for: the same call crosses the hourly occurrence and makes the
        // claim.
        //
        // fails if `disarm` tears a group down only once it has fired at least
        // once, or keys the teardown on anything other than the member set
        // emptying.
        #[tokio::test(start_paused = true)]
        async fn a_group_disarmed_before_its_first_occurrence_leaves_no_worker_behind() {
            let dir = tempfile::tempdir().unwrap();
            let paths = test_paths(&dir);
            let root = tempfile::tempdir().unwrap();
            let (handle, mut rx, _fixture) = spawn_test_fixture();
            let rig = rig(DEFAULT_MAX_CRON_SLEEP);
            let mut registry = ExtrasRegistry::default();
            // Both per-name extras and one per-pid extra, so a single disarm has
            // to reach all three.
            let app = app_with("web", |app| {
                app.cron_restart = Some("0 * * * *".to_string());
                app.watch = true;
                app.cwd = Some(root.path().display().to_string());
                app.max_memory = Some(MemSize::from_bytes(1024));
            });
            handle.start(vec![app.clone()]).await.unwrap();

            registry.arm(
                &armed_entry(0, 0, 1000, app, &paths),
                idle_prober(),
                &rig.extras,
                &handle,
            );
            let group = &registry.groups["web"];
            assert_eq!(group.members, HashSet::from([0]));
            assert!(group.cron.is_some(), "the cron worker must have armed");
            assert!(group.watch.is_some(), "the watch must have armed");
            assert_eq!(rig.enforcer.arms().len(), 1);

            registry.disarm(0, "web");

            assert!(
                registry.groups.is_empty(),
                "a group whose only member left before its first occurrence must go with it"
            );
            assert!(
                registry.instances.is_empty(),
                "the same disarm must take the instance's own extras too"
            );
            assert_eq!(rig.enforcer.disarms(), vec![0]);
            assert_no_restart_within(&mut rx, "web", PAST_THE_NEXT_OCCURRENCE).await;
        }

        // The watch twin of the case above, and it needs to exist separately: the
        // gate is two independent conditions, so a watch half narrowed to
        // `group.watch.is_none()` leaves the cron case — and every other
        // assertion in this file — perfectly green while an app that ended its
        // watch stays unwatched forever. fails if that half stops asking whether
        // the handle is ALIVE rather than merely present.
        //
        // The ending is forced with `abort` rather than by killing the
        // `WatchSource` the loop really returns on. That source dies with the
        // debouncer's own OS thread, which nothing available to a test reaches —
        // deleting the watched tree does not close it — and both leave the map in
        // the one state the gate reads: a finished handle.
        //
        // The save at the end is what makes the claim behavioural rather than
        // structural. A rebuilt watch has to have re-registered a real OS watcher
        // on the root; replacing the handle alone would restart nothing.
        //
        // Real time and a real filesystem, like every case that drives notify.
        // Twelve scripts against two spawns: the start, and the save's restart.
        #[tokio::test]
        async fn a_watch_that_ended_on_its_own_is_rebuilt_on_the_next_arm() {
            let home = tempfile::tempdir().unwrap();
            let paths = test_paths(&home);
            let root = tempfile::tempdir().unwrap();
            let (handle, mut rx, _fixture) = spawn_test_fixture();
            let rig = rig(DEFAULT_MAX_CRON_SLEEP);
            let mut registry = ExtrasRegistry::default();
            let app = app_with("web", |app| {
                app.watch = true;
                app.cwd = Some(root.path().display().to_string());
                app.watch_delay = Some(UpDuration::from_millis(
                    real_time::TEST_DELAY.as_millis() as u64
                ));
            });
            handle.start(vec![app.clone()]).await.unwrap();

            registry.arm(
                &armed_entry(0, 0, 1000, app.clone(), &paths),
                idle_prober(),
                &rig.extras,
                &handle,
            );
            let armed = registry.groups["web"]
                .watch
                .as_ref()
                .expect("the first arm registers a watcher");
            armed.abort();
            settle_finished(armed).await;

            registry.arm(
                &armed_entry(0, 0, 2000, app, &paths),
                idle_prober(),
                &rig.extras,
                &handle,
            );

            touch(root.path(), "trigger.txt").unwrap();
            let info = expect_restart(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
            assert_eq!(info.restarts, 1);
        }

        // fails if a watched app is never armed at all; if the root is not
        // canonicalized (on macOS a tempdir under `/var/...` is delivered as
        // `/private/var/...`, every `strip_prefix` fails, and the watch fires
        // never); or if the last instance leaving does not abort the group —
        // which is the whole of "stopping a name stops its watch" (one instance
        // here, so the name and the instance are the same thing).
        //
        // Real time and a real filesystem: notify's backend is the OS, and a
        // paused clock does not move it.
        #[tokio::test]
        async fn a_watched_app_restarts_on_a_save_and_goes_quiet_once_disarmed() {
            let home = tempfile::tempdir().unwrap();
            let paths = test_paths(&home);
            let root = tempfile::tempdir().unwrap();
            let (handle, mut rx, _fixture) = spawn_test_fixture();
            let rig = rig(DEFAULT_MAX_CRON_SLEEP);
            let mut registry = ExtrasRegistry::default();
            let app = app_with("web", |app| {
                app.watch = true;
                app.cwd = Some(root.path().display().to_string());
                // The one owner of this subsystem's real-time constants
                // (`watch::real_time`), converted rather than re-declared.
                app.watch_delay = Some(UpDuration::from_millis(
                    real_time::TEST_DELAY.as_millis() as u64
                ));
            });
            handle.start(vec![app.clone()]).await.unwrap();
            registry.arm(
                &armed_entry(0, 0, 1000, app, &paths),
                idle_prober(),
                &rig.extras,
                &handle,
            );

            touch(root.path(), "trigger.txt").unwrap();
            let info = expect_restart(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
            assert_eq!(info.restarts, 1);

            registry.disarm(0, "web");
            touch(root.path(), "after-disarm.txt").unwrap();
            assert_no_restart_within(&mut rx, "web", real_time::NO_EVENT_WINDOW).await;
        }

        // fails if `DEFAULT_WATCH_DELAY` is not what an app naming no
        // `watch_delay` gets. It is 500ms; any fallback long enough to matter (a
        // stray `Duration::from_secs(600)`, say) leaves the save below with no
        // restart inside the deadline, and an app that asked to be watched would
        // in production appear to be watched by nothing.
        //
        // Real time and a real filesystem, like every case that drives notify.
        #[tokio::test]
        async fn a_watched_app_naming_no_delay_still_restarts_on_a_save() {
            let home = tempfile::tempdir().unwrap();
            let paths = test_paths(&home);
            let root = tempfile::tempdir().unwrap();
            let (handle, mut rx, _fixture) = spawn_test_fixture();
            let rig = rig(DEFAULT_MAX_CRON_SLEEP);
            let mut registry = ExtrasRegistry::default();
            let app = app_with("web", |app| {
                app.watch = true;
                app.cwd = Some(root.path().display().to_string());
                // No `watch_delay`: this case exists for the default.
            });
            handle.start(vec![app.clone()]).await.unwrap();
            registry.arm(
                &armed_entry(0, 0, 1000, app, &paths),
                idle_prober(),
                &rig.extras,
                &handle,
            );

            touch(root.path(), "trigger.txt").unwrap();
            let info = expect_restart(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
            assert_eq!(info.restarts, 1);
        }

        // The watch's own door into the same claim the cron case makes, and it
        // needs its own case: the two subsystems pick their `SupervisorHandle`
        // method independently, so a watch loop calling the operator's `restart`
        // leaves every cron assertion green.
        //
        // fails if `run_group` restarts through `restart` rather than
        // `restart_automatic` — under which a file save reaches every subscriber
        // as `manually: true`, and an editor's autosave is reported as a deploy.
        //
        // Real time and a real filesystem, like every case that drives notify.
        #[tokio::test]
        async fn a_watch_restart_is_not_reported_as_a_user_action() {
            let home = tempfile::tempdir().unwrap();
            let paths = test_paths(&home);
            let root = tempfile::tempdir().unwrap();
            let (handle, mut rx, _fixture) = spawn_test_fixture();
            let rig = rig(DEFAULT_MAX_CRON_SLEEP);
            let mut registry = ExtrasRegistry::default();
            let app = app_with("web", |app| {
                app.watch = true;
                app.cwd = Some(root.path().display().to_string());
                app.watch_delay = Some(UpDuration::from_millis(
                    real_time::TEST_DELAY.as_millis() as u64
                ));
            });
            handle.start(vec![app.clone()]).await.unwrap();
            registry.arm(
                &armed_entry(0, 0, 1000, app, &paths),
                idle_prober(),
                &rig.extras,
                &handle,
            );

            touch(root.path(), "trigger.txt").unwrap();

            let (info, manually) =
                expect_restart_event(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
            assert_eq!(info.restarts, 1);
            assert!(
                !manually,
                "a file changing under a watched tree is not a user action"
            );
        }

        // fails if `arm_watch` builds its filter from empty slices rather than
        // from the app's own `watch_options`/`ignore_watch` — the shape under
        // which every ignore rule the user wrote is silently discarded and a build
        // directory's own churn restarts the app forever. The trigger at the end
        // is what makes the silence able to fail: the same watch, the same window,
        // a name the filter does not ignore.
        #[tokio::test]
        async fn a_watched_app_ignores_the_paths_its_ignore_watch_names() {
            let home = tempfile::tempdir().unwrap();
            let paths = test_paths(&home);
            let root = tempfile::tempdir().unwrap();
            let (handle, mut rx, _fixture) = spawn_test_fixture();
            let rig = rig(DEFAULT_MAX_CRON_SLEEP);
            let mut registry = ExtrasRegistry::default();
            let app = app_with("web", |app| {
                app.watch = true;
                app.cwd = Some(root.path().display().to_string());
                app.ignore_watch = vec!["ignored.txt".to_string()];
                app.watch_delay = Some(UpDuration::from_millis(
                    real_time::TEST_DELAY.as_millis() as u64
                ));
            });
            handle.start(vec![app.clone()]).await.unwrap();
            registry.arm(
                &armed_entry(0, 0, 1000, app, &paths),
                idle_prober(),
                &rig.extras,
                &handle,
            );

            touch(root.path(), "ignored.txt").unwrap();
            assert_no_restart_within(&mut rx, "web", real_time::NO_EVENT_WINDOW).await;

            touch(root.path(), "trigger.txt").unwrap();
            let info = expect_restart(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
            assert_eq!(info.restarts, 1);
        }

        // fails if the watch's ignore set is the default globs plus `ignore_watch`
        // and nothing else. An app naming an explicit `out_file`/`err_file` under
        // its own `cwd` then restarts on its own log writes, and the loop is
        // self-sustaining: the startup line trips the debounce, the debounce
        // restarts the group, the restart writes another startup line.
        // `max_restarts` cannot end it — an automatic restart resets the budget —
        // and `**/logs/**` does not cover it, since nothing makes an explicit log
        // path live in a directory called `logs`.
        //
        // BOTH log paths are pointed inside the tree, so an implementation that
        // derives an ignore from `out_file` alone still reddens. The trigger at the
        // end is what makes the silence able to fail: the same watch, the same
        // window, a sibling file the filter has no reason to ignore.
        //
        // Real time and a real filesystem, like every case that drives notify.
        #[tokio::test]
        async fn a_watched_app_ignores_its_own_log_writes() {
            let home = tempfile::tempdir().unwrap();
            let paths = test_paths(&home);
            let root = tempfile::tempdir().unwrap();
            let (handle, mut rx, _fixture) = spawn_test_fixture();
            let rig = rig(DEFAULT_MAX_CRON_SLEEP);
            let mut registry = ExtrasRegistry::default();
            let app = app_with("web", |app| {
                app.watch = true;
                app.cwd = Some(root.path().display().to_string());
                // Absolute, under the watched tree, and named nothing like `logs`
                // — the arrangement that really does put a shep write inside the
                // tree an app asked to have watched.
                app.out_file = Some(root.path().join("app-out.txt").display().to_string());
                app.err_file = Some(root.path().join("app-err.txt").display().to_string());
                app.watch_delay = Some(UpDuration::from_millis(
                    real_time::TEST_DELAY.as_millis() as u64
                ));
            });
            handle.start(vec![app.clone()]).await.unwrap();
            registry.arm(
                &armed_entry(0, 0, 1000, app, &paths),
                idle_prober(),
                &rig.extras,
                &handle,
            );

            touch(root.path(), "app-out.txt").unwrap();
            touch(root.path(), "app-err.txt").unwrap();
            assert_no_restart_within(&mut rx, "web", real_time::NO_EVENT_WINDOW).await;

            touch(root.path(), "trigger.txt").unwrap();
            let info = expect_restart(&mut rx, "web", real_time::SMOKE_DEADLINE).await;
            assert_eq!(info.restarts, 1);
        }

        // fails if the prober handed to a liveness loop is built from the app's
        // own `config.env` rather than the ASSEMBLED spec's, or built once at
        // boot and shared: `probe_exec` runs `env_clear().envs(&self.env)`, and
        // `SHEP_INSTANCE` is written by `assemble` and by nothing else, so under
        // either bug the variable expands to nothing, `live-` matches no file,
        // and BOTH instances report. Also fails if the actor assembles with a
        // hardcoded instance 0, under which neither reports.
        //
        // A file, not a port: `test -f` flips fail->pass with no listener, no
        // reserved port and no race, so the only thing this case can fail on is
        // the environment the probe ran with. Real time, and `#[cfg(unix)]` on
        // the test rather than on anything else, so the Windows leg still
        // compiles and runs every case above.
        #[cfg(unix)]
        #[tokio::test]
        async fn each_instances_liveness_probe_runs_with_its_own_assembled_env() {
            let markers = tempfile::tempdir().unwrap();
            // Instance 0's marker exists; instance 1's never will.
            std::fs::write(markers.path().join("live-0"), b"").unwrap();

            let mut h = harness(vec![ProcScript::never_exits(); 4]);
            h.ctx
                .supervisor
                .start(vec![app_with("web", |app| {
                    app.instances = 2;
                    app.liveness_probe = Some(ProbeConfig {
                        failure_threshold: 1,
                        interval: PROBE_INTERVAL,
                        timeout: UpDuration::from_millis(5_000),
                        ..probe_config(
                            ProbeKind::Exec,
                            &format!(
                                r#"test -f "{}/live-$SHEP_INSTANCE""#,
                                markers.path().display()
                            ),
                        )
                    });
                })])
                .await
                .unwrap();

            let listing = h.ctx.supervisor.list().await;
            // `ProcessInfo` carries no instance number, but the assembler's log
            // path does — and it is the same `assemble` call the prober's env
            // came from, so this pins WHICH instance id 1 is rather than assuming
            // the allocation order.
            assert!(
                listing[1]
                    .out_file
                    .as_ref()
                    .is_some_and(|path| path.ends_with("web-1-out.log")),
                "id 1 must be instance 1: {:?}",
                listing[1].out_file
            );
            let instance_one_pid = listing[1].pid.expect("a live sheep has a pid");

            let failure = expect_liveness(&mut h.liveness, LIVENESS_DEADLINE).await;
            assert_eq!(
                failure,
                LivenessReport {
                    id: 1,
                    pid: instance_one_pid,
                    epoch: 1,
                },
                "only the instance whose own marker is missing may report"
            );
            // Both orderings must fail against the broken implementations above:
            // under them BOTH instances report and which arrives first is a race,
            // so the window that catches the other one is not optional. A bounded
            // `timeout` + `recv` spanning two further probe cycles, per rule 11.
            assert_no_liveness_within(&mut h.liveness, PROBE_INTERVAL.as_duration() * 3).await;
        }
    }
}