shep-daemon 0.7.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
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
//! Portable RPC dispatch: verb routing, typed errors, per-call deadlines
//!
//! `dispatch` is the one function the connection layer calls per request
//! envelope. Everything here compiles and tests on every platform: no
//! `cfg(unix)`, no sockets, no bytes on a wire. [`RpcContext`] bundles the
//! daemon-wide handles a request handler may touch; `Outcome` tells the
//! caller what to do next (reply, forward bus events, or begin shutdown).
//!
//! Every envelope gets a `budget`: its own `deadline_ms`, clamped to
//! `MAX_DEADLINE_MS` so a peer cannot pin a daemon task open, or
//! `DEFAULT_DEADLINE_MS` when it sent none.

use core::future::Future;
use core::time::Duration;

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};

use tokio::sync::watch;

use shep_core::config::graph::BootPlan;
use shep_core::config::{DeclaredApp, NormalizeError, ResolvedApp, normalize_all};
use shep_core::paths::ShepPaths;
use shep_core::protocol::{
    Envelope, Lamb, PROTOCOL_VERSION, ProcessInfo, Reply, Request, Response, RpcError,
    RpcErrorCode, SelectorSpec, SheepApplied, SheepRefusal,
};
use shep_core::selector::ProcessSelector;
use shep_core::signals::OperatorSignal;
use shep_core::status::ProcStatus;

use crate::bus::{Bus, TopicFilter};
use crate::dogs::DogSpec;
use crate::limits::stats::StatsState;
use crate::secrets::ProviderSecrets;
use crate::snapshot::{FlockRegistry, SnapshotError, write_atomic};
use crate::supervisor::{Applied, BatchPolicy, ConnId, SupervisorError, SupervisorHandle};

/// Deadline applied when a client sends none (spec §6: 5s default).
pub(crate) const DEFAULT_DEADLINE_MS: u64 = 5_000;
/// Ceiling on a client-supplied deadline: a peer cannot pin a daemon task open.
pub(crate) const MAX_DEADLINE_MS: u64 = 60_000;

/// Every dog name this shepherd may hold a section for, running or not.
///
/// Seeded at boot from the CLI, which owns `shep.toml`. A
/// `Request::EnableDog` adds a name, so a dog adopted against a running
/// shepherd needs no reload. Never shrunk: `shep disable` leaves the
/// section in `dogs.toml`, where a disabled dog still wants configuring.
/// A set because the only question asked is membership, behind a mutex
/// because every connection holds a clone.
#[derive(Debug, Clone)]
pub(crate) struct KnownDogs {
    names: Arc<Mutex<BTreeSet<String>>>,
}

impl KnownDogs {
    /// Wraps a boot-time seed.
    pub(crate) fn new(names: BTreeSet<String>) -> Self {
        Self {
            names: Arc::new(Mutex::new(names)),
        }
    }

    /// Whether this shepherd may hold a section for `name`.
    pub(crate) fn contains(&self, name: &str) -> bool {
        self.lock().contains(name)
    }

    /// Records that `name` is a dog this shepherd knows about.
    pub(crate) fn insert(&self, name: &str) {
        self.lock().insert(name.to_owned());
    }

    /// A poisoned lock is recovered rather than propagated, as
    /// [`crate::dogs::DogRefusals`] does: these are names with no invariant
    /// a panic mid-write could have broken.
    fn lock(&self) -> MutexGuard<'_, BTreeSet<String>> {
        self.names.lock().unwrap_or_else(PoisonError::into_inner)
    }
}

/// Everything a request handler may touch; one clone per connection.
///
/// Every clone shares the same supervisor engine, event bus sender, flock
/// registry, and shutdown signal. The connection layer builds one from the
/// daemon's shared state and hands it to `dispatch` once per envelope.
///
/// Public because `tests/daemon_e2e.rs` holds one to drive [`Self::shutdown`]
/// and [`Self::snapshot_now`] without going through the socket. Its fields
/// are not.
#[derive(Clone, Debug)]
pub struct RpcContext {
    /// The supervisor engine this daemon is running.
    pub(crate) supervisor: SupervisorHandle,
    /// The daemon-wide event bus; `Subscribe` compiles a [`TopicFilter`] the
    /// connection layer hands to [`crate::bus::spawn_forwarder`] alongside a
    /// receiver off this sender.
    pub(crate) events: Bus,
    /// The muster roll's in-memory app registry; `Start` records into it.
    pub(crate) registry: FlockRegistry,
    /// Where [`Self::snapshot_now`] writes the muster roll.
    pub(crate) snapshot_path: PathBuf,
    /// Where `DogConfig` reads a dog's `[<name>]` section from: this home's
    /// `dogs.toml`, not `shep.toml`. The key carries no prefix.
    ///
    /// Re-read per request rather than held as parsed config, so `shep
    /// disable X && shep enable X` picks up an edited section
    /// (`crate::dogs::dog_section`).
    pub(crate) dogs_config: PathBuf,
    /// Every dog name this shepherd may hold a section for, running or
    /// not. See [`KnownDogs`].
    pub(crate) known_dogs: KnownDogs,
    /// Names from [`crate::boot::BootOptions::dogs`], the spawn list this
    /// daemon booted with, rather than [`Self::known_dogs`]' wider set of
    /// dogs that merely exist.
    ///
    /// Held rather than re-read from `shep.toml`, which this daemon never
    /// reads: a later boot plan (rebuilt at shutdown, or for a staged start)
    /// needs the same spawn list `boot` used, and this is where it survives
    /// between requests.
    pub(crate) dog_names: Vec<String>,
    /// Which of [`Self::dog_names`] run before every sheep rather than
    /// after the flock, from `[daemon] boot_first_dogs`.
    ///
    /// Held for the same reason as [`Self::dog_names`]: rebuilding the boot
    /// plan later needs to know which dogs were promoted, and this daemon
    /// has no other way to ask, since it never reads `shep.toml` itself. A
    /// name absent from [`Self::dog_names`] is inert here, not an error.
    pub(crate) boot_first_dogs: Vec<String>,
    /// This daemon's `$SHEP_HOME` layout, for assembling a dog's app config.
    pub(crate) paths: ShepPaths,
    /// This daemon's crate version, echoed in the handshake.
    pub(crate) daemon_version: String,
    /// Which dogs this daemon has refused at the handshake, and how often.
    ///
    /// Written and read by the connection layer's handshake, to decide
    /// whether a refused dog earns its one restart from disk or has already
    /// had it ([`crate::dogs::DogRefusals`]).
    pub(crate) dog_refusals: crate::dogs::DogRefusals,
    /// What has connected to this daemon's socket, by peer pid.
    ///
    /// Written by the connection layer, the one place that can see a peer's
    /// credentials, and read by [`crate::dogs::record_silent_dog`]. It tells
    /// a dog that never reached the socket apart from one that reached it and
    /// did not name itself: two silences with opposite fixes.
    pub(crate) peer_contacts: crate::dogs::PeerContacts,
    /// This daemon's OS pid, echoed in the handshake.
    pub(crate) pid: u32,
    /// Flips to `true` to start graceful daemon shutdown; see [`Self::shutdown`].
    pub(crate) shutdown: Arc<watch::Sender<bool>>,
    /// The live resource readings [`with_live_stats`] takes a sample from.
    ///
    /// The same state the supervisor's extras hold: they decide which sheep
    /// is watched and record the periodic CPU baseline, and this side reads
    /// against it.
    pub(crate) stats: Arc<StatsState>,
    /// What provider dogs have pushed, written by `PutSecrets` here and
    /// read per spawn by the supervisor actor. One registry, two owners,
    /// for the reason `stats` above gives.
    pub(crate) provider_secrets: Arc<ProviderSecrets>,
}

/// Where a muster roll landed and what it recorded.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SavedRoll {
    /// The path written.
    pub path: PathBuf,
    /// How many apps the roll records.
    pub apps: u32,
}

impl RpcContext {
    /// Asks the daemon to begin graceful shutdown.
    ///
    /// Only flips the watch signal; the connection layer runs the kill
    /// ladder and closes listeners once it observes this go `true`.
    /// `dispatch` never calls it: `KillDaemon` reports the intent through
    /// `Outcome::Shutdown`, so the caller triggers it after the reply is on
    /// the wire.
    pub fn shutdown(&self) {
        let _ = self.shutdown.send(true);
    }

    /// Announces that these dogs' `dogs.toml` sections changed.
    ///
    /// The one place a `config.dog.<name>` frame comes from: the publisher
    /// has to be inside the daemon process, because that is where the bus
    /// is. The CLI's other two writers of `dogs.toml` say nothing.
    ///
    /// Public because the caller is `shep`'s own boot, which runs the
    /// migration before this daemon exists.
    pub fn announce_dog_config(&self, dogs: &[String]) {
        crate::bus::publish_dog_config_changed(&self.events, dogs);
    }

    /// Writes the muster roll now, reporting what it recorded.
    ///
    /// `None` means the supervisor engine has already stopped: there is
    /// nothing left to record and the shutdown path has already written the
    /// final roll.
    ///
    /// # Errors
    /// - [`SnapshotError`] as `write_atomic` reports it.
    pub async fn save_roll_now(&self) -> Result<Option<SavedRoll>, SnapshotError> {
        let Ok(infos) = self.supervisor.list_checked().await else {
            return Ok(None);
        };
        let roll = self.registry.roll(&infos, crate::now_ms());
        write_atomic(&self.snapshot_path, &roll)?;
        Ok(Some(SavedRoll {
            path: self.snapshot_path.clone(),
            // `u32` matches `SavedApp::instances_running`; a flock large
            // enough to overflow it has other problems.
            apps: u32::try_from(roll.apps.len()).unwrap_or(u32::MAX),
        }))
    }

    /// Writes the muster roll now, discarding what it recorded.
    ///
    /// # Errors
    /// - [`SnapshotError`] as `write_atomic` reports it.
    pub async fn snapshot_now(&self) -> Result<(), SnapshotError> {
        self.save_roll_now().await.map(|_| ())
    }
}

/// What the connection layer must do with a dispatched request.
#[derive(Debug)]
pub(crate) enum Outcome {
    /// Send this reply and keep reading.
    Reply(Reply),
    /// Send this reply, then start forwarding events through `filter`.
    Subscribe {
        /// The `Subscribed` (or error) reply to send first.
        reply: Reply,
        /// Compiled topic matcher for [`crate::bus::spawn_forwarder`].
        filter: TopicFilter,
    },
    /// Send this reply, then trigger daemon shutdown and close.
    Shutdown(Reply),
}

/// The deadline this envelope gets: its own, clamped, or the default.
#[must_use]
pub(crate) fn budget(deadline_ms: Option<u64>) -> Duration {
    // clamp's lower bound is 1ms so a literal `0` means "expire immediately"
    // rather than silently becoming "no deadline at all".
    Duration::from_millis(
        deadline_ms
            .unwrap_or(DEFAULT_DEADLINE_MS)
            .clamp(1, MAX_DEADLINE_MS),
    )
}

/// Dispatches one request envelope against `ctx`, returning what the
/// connection layer must do with the result.
///
/// The deadline [`budget`] computes bounds the reply, not the actor's work:
/// dropping the work future only stops the daemon waiting on the supervisor,
/// and a command already handed to a sheep-owning task runs to completion.
/// So a `DeadlineExceeded` reply to `Start` means no answer within the
/// budget, not that nothing happened; a client that retries must reconcile
/// with `ListFlock`.
pub(crate) async fn dispatch(envelope: Envelope, conn: ConnId, ctx: &RpcContext) -> Outcome {
    let id = envelope.id;
    with_deadline(
        id,
        budget(envelope.deadline_ms),
        run(id, conn, envelope.body, ctx),
    )
    .await
}

// `+ Send`: awaited inside the per-connection `tokio::spawn`, so the bound is
// stated rather than inferred.
async fn with_deadline<F: Future<Output = Outcome> + Send>(
    id: u64,
    budget: Duration,
    work: F,
) -> Outcome {
    match tokio::time::timeout(budget, work).await {
        Ok(outcome) => outcome,
        Err(_) => Outcome::Reply(Reply {
            id,
            result: Err(RpcError {
                code: RpcErrorCode::DeadlineExceeded,
                message: format!(
                    "the request deadline of {} ms expired before the daemon finished",
                    budget.as_millis()
                ),
                daemon_version: None,
            }),
        }),
    }
}

async fn run(id: u64, conn: ConnId, request: Request, ctx: &RpcContext) -> Outcome {
    let reply = |result| Outcome::Reply(Reply { id, result });
    match request {
        Request::Ping => reply(Ok(Response::Pong)),
        // One of the two verbs that pays for a live reading; `with_live_stats`
        // says why every lifecycle verb below goes without.
        Request::ListFlock => match ctx.supervisor.list_checked().await {
            Ok(infos) => reply(Ok(Response::Flock(with_dog_contact(
                &ctx.dog_refusals,
                with_live_stats(&ctx.stats, infos).await,
            )))),
            Err(err) => reply(Err(rpc_error(&err))),
        },
        // The other one. Sampled after the selector has narrowed the
        // listing, so the join below runs over the matched rows alone.
        Request::Describe { selector } => match selector_of(selector) {
            Err(err) => reply(Err(err)),
            Ok(selector) => match ctx.supervisor.list_checked().await {
                Err(err) => reply(Err(rpc_error(&err))),
                Ok(infos) => {
                    // The rule `Actor::matching_ids` applies to every
                    // lifecycle verb, repeated here because this filter is
                    // over `ProcessInfo`s: a dog is not a flock member, so a
                    // sweep passes it by and an exact selector reaches it.
                    let exact = selector.is_exact();
                    let hits: Vec<_> = infos
                        .into_iter()
                        .filter(|i| exact || i.dog.is_none())
                        .filter(|i| selector.matches(&i.name, i.id, i.fold.as_deref(), i.instance))
                        .collect();
                    if hits.is_empty() {
                        reply(Err(not_found()))
                    } else {
                        let hits = with_live_stats(&ctx.stats, hits).await;
                        let hits = with_lambs(&ctx.stats, hits).await;
                        reply(Ok(Response::Described(with_dog_contact(
                            &ctx.dog_refusals,
                            hits,
                        ))))
                    }
                }
            },
        },
        // Peer input is untrusted: re-normalize before anything is registered.
        //
        // `AllOrNothing` no longer means the whole request is atomic: the
        // stages are one `Command::Start` each, so a refusal in stage 1 leaves
        // stage 0 running and nothing rolls it back. The refusal names those
        // apps; `start_in_stages` argues why they are left alone.
        Request::Start { apps } => match normalize_all(apps) {
            Err(err) => reply(Err(RpcError {
                code: RpcErrorCode::InvalidConfig,
                message: err.to_string(),
                daemon_version: None,
            })),
            Ok(resolved) => match staged_plan(ctx, &resolved) {
                Err(err) => reply(Err(err)),
                Ok(plan) => {
                    ctx.registry.record(&resolved);
                    match crate::boot_order::start_in_stages(
                        &plan,
                        &resolved,
                        &ctx.supervisor,
                        &ctx.events,
                        BatchPolicy::AllOrNothing,
                    )
                    .await
                    {
                        Ok(infos) => reply(Ok(Response::Started(infos))),
                        Err(err) => reply(Err(rpc_error(&err))),
                    }
                }
            },
        },
        // The membership half of `Start` with none of the spawning, and the
        // same untrusted-peer rule. Recorded in the registry as `Start` is:
        // an added app is a flock member that happens to be stopped, so a
        // `shep save` after a `shep add` has to write it.
        //
        // The cycle refusal is shared and the stages are not: a document that
        // cannot be started is one to refuse at the door an operator is
        // standing at, and `add` starts nothing to order.
        Request::Add { apps } => match normalize_all(apps) {
            Err(err) => reply(Err(RpcError {
                code: RpcErrorCode::InvalidConfig,
                message: err.to_string(),
                daemon_version: None,
            })),
            Ok(resolved) => match staged_plan(ctx, &resolved) {
                Err(err) => reply(Err(err)),
                Ok(_) => {
                    ctx.registry.record(&resolved);
                    match ctx.supervisor.register_at_rest(resolved).await {
                        Ok(infos) => reply(Ok(Response::Added(infos))),
                        Err(err) => reply(Err(rpc_error(&err))),
                    }
                }
            },
        },
        // Re-normalized for the reason `Start` is, plus one of its own: an
        // unnormalized config would report every default it did not spell out
        // as a difference. Nothing is recorded, since this answers a question
        // and must not change what the next `shep save` writes.
        Request::ConfigDrift { apps } => match normalize_all(apps) {
            Err(err) => reply(Err(RpcError {
                code: RpcErrorCode::InvalidConfig,
                message: err.to_string(),
                daemon_version: None,
            })),
            Ok(resolved) => match ctx.supervisor.config_drift(resolved).await {
                Ok(drifted) => reply(Ok(Response::Drifted(drifted))),
                Err(err) => reply(Err(rpc_error(&err))),
            },
        },
        Request::Stop { selector } => {
            selector_call(id, selector, |s| ctx.supervisor.stop(s), Response::Stopped).await
        }
        // Forward, dependencies first, when the selector matches more than
        // one sheep. Not reverse-stop then forward-start: the rolling version
        // puts the whole fold down at once in the middle, and forward-only
        // never does.
        Request::Restart { selector } => restart_request(id, selector, ctx).await,
        // Staged like `Restart` above and for its reason. Which of the two
        // reloads an app gets is still `ReloadMode::of`'s call: ordering
        // decides when a stage begins and nothing about the swap inside it.
        //
        // `Reloading` still names an acceptance, and a single-target reload
        // still answers before the first replacement is spawned. A walk of
        // several stages is what that costs: it holds for the swaps of every
        // app another matched app waits on, so the reply lands that much
        // later, and a fold deep enough outlives the request budget and is
        // abandoned by `with_deadline` with its last stages unreloaded.
        // `staged_start_deadline` is how `shep start` buys the room for the
        // same walk; `shep reload` sends `RELOAD_DEADLINE`, which is this
        // module's own 60s ceiling and so the most it can buy.
        Request::Reload { selector } => reload_request(id, selector, ctx).await,
        Request::Reopen { selector } => {
            selector_call(
                id,
                selector,
                |s| ctx.supervisor.reopen(s),
                Response::Reopened,
            )
            .await
        }
        Request::Flush { selector } => {
            selector_call(id, selector, |s| ctx.supervisor.flush(s), Response::Flushed).await
        }
        Request::Trigger {
            selector,
            action,
            params,
        } => trigger(id, selector, action, params, ctx).await,
        Request::Signal { selector, signal } => signal_request(id, selector, signal, ctx).await,
        Request::SendLine { selector, line } => {
            // Refused here, not silently split by the writer: a line
            // carrying a newline would be delivered as two commands where the
            // operator typed one. `\r` too, since CRLF reaches a shell as a
            // command with a stray carriage return in it.
            if line.contains(['\n', '\r']) {
                return reply(Err(RpcError {
                    code: RpcErrorCode::InvalidConfig,
                    message: "a line may not contain a newline or a carriage return; \
                              send one line per request"
                        .to_string(),
                    daemon_version: None,
                }));
            }
            match selector_of(selector) {
                Ok(selector) => match ctx.supervisor.send_line(selector, line).await {
                    Ok(rows) => reply(Ok(Response::SentLine(rows))),
                    Err(err) => reply(Err(rpc_error(&err))),
                },
                Err(err) => reply(Err(err)),
            }
        }
        Request::Delete { selector } => match selector_of(selector) {
            Err(err) => reply(Err(err)),
            Ok(selector) => match ctx.supervisor.delete(selector).await {
                Ok(ids) => reply(Ok(Response::Deleted(ids))),
                Err(err) => reply(Err(rpc_error(&err))),
            },
        },
        Request::Scale { name, count } => match ctx.supervisor.scale(&name, count).await {
            Ok(scaled) => {
                // Recorded unconditionally: without it `shep stock web 4`
                // then `shep save` writes `instances = 2` and the next reboot
                // undoes the scale. Unconditionally, since a partial scale-up
                // leaves real instances the roll has to know about too.
                let achieved = scaled.achieved();
                let requested = scaled.requested;
                ctx.registry.record(&[scaled.app]);
                match scaled.shortfall {
                    None => reply(Ok(Response::Scaled(scaled.instances))),
                    // Non-zero exit: the operator asked for four and has
                    // three. The sentence names both numbers, so a reader can
                    // tell a scale that achieved nothing from one that nearly
                    // finished.
                    Some(message) => reply(Err(RpcError {
                        code: RpcErrorCode::SpawnFailed,
                        message: format!(
                            "scaled {name} to {achieved} of {requested} requested; \
                             the next instance would not spawn: {message}"
                        ),
                        daemon_version: None,
                    })),
                }
            }
            Err(err) => reply(Err(rpc_error(&err))),
        },
        // Scoped to `conn`, which is what makes a smit ephemeral: the
        // connection layer forgets this one's marks in its own tail. `smit`
        // arrives already validated by `Smit`'s hand-written `Deserialize`,
        // so the only refusal left here is a name nothing holds.
        Request::SetSmit { sheep, smit } => {
            match ctx.supervisor.set_smit(conn, &sheep, smit).await {
                Ok(infos) => reply(Ok(Response::SmitPainted(infos))),
                Err(err) => reply(Err(rpc_error(&err))),
            }
        }
        Request::SaveRoll => match ctx.save_roll_now().await {
            Ok(Some(saved)) => reply(Ok(Response::RollSaved {
                // Lossy, as `to_info` treats log paths: a non-UTF-8 roll
                // path degrades one field rather than the whole reply.
                path: saved.path.to_string_lossy().into_owned(),
                apps: saved.apps,
            })),
            Ok(None) => reply(Err(RpcError {
                code: RpcErrorCode::Internal,
                message: "the supervisor engine has stopped; no roll was written".to_string(),
                daemon_version: None,
            })),
            Err(err) => reply(Err(RpcError {
                code: RpcErrorCode::Internal,
                message: err.to_string(),
                daemon_version: None,
            })),
        },
        // The same restore `boot` runs, called the same way
        // (`crate::snapshot::muster`).
        Request::Muster => {
            match crate::snapshot::muster(
                &ctx.snapshot_path,
                &ctx.registry,
                &ctx.supervisor,
                &ctx.events,
                &ctx.dog_names,
                &ctx.boot_first_dogs,
            )
            .await
            {
                Err(err) => reply(Err(RpcError {
                    code: RpcErrorCode::Internal,
                    message: err.to_string(),
                    daemon_version: None,
                })),
                Ok(names) => match ctx.supervisor.list_checked().await {
                    Err(err) => reply(Err(rpc_error(&err))),
                    // Every sheep of every app the roll restored, not only
                    // the ones this call spawned (`Response::Mustered`).
                    Ok(infos) => reply(Ok(Response::Mustered(
                        infos
                            .into_iter()
                            .filter(|info| names.contains(&info.name))
                            .collect(),
                    ))),
                },
            }
        }
        // Re-read per request, never cached: `shep disable X && shep enable
        // X` bounces a dog to reload its configuration, and a copy taken at
        // boot would answer with the section as it was. A dog subscribed to
        // `config.dog.<name>` reaches this same arm without going down.
        Request::DogConfig { name } => match crate::dogs::dog_section(&ctx.dogs_config, &name) {
            Ok(toml) => reply(Ok(Response::DogSection { toml: toml.into() })),
            Err(err) => reply(Err(RpcError {
                code: RpcErrorCode::InvalidConfig,
                message: err.to_string(),
                daemon_version: None,
            })),
        },
        Request::EnableDog { name, source } => {
            let spec = DogSpec { name, source };
            match crate::dogs::dog_app(&spec, &ctx.paths) {
                Err(err) => reply(Err(RpcError {
                    code: RpcErrorCode::InvalidConfig,
                    message: err.to_string(),
                    daemon_version: None,
                })),
                Ok(app) => {
                    // Read before `start_dog` takes the app. An operator
                    // reading the dog's log during an upgrade is usually
                    // asking which file the spawn resolved to.
                    let script = app.config().script.clone();
                    match ctx.supervisor.start_dog(app, spec.source).await {
                        // `start_dog` is idempotent by name, so what comes
                        // back is whatever already holds it. An unmarked entry
                        // means a sheep holds it: nothing was spawned, so the
                        // refusal has nothing to undo.
                        Ok(info) if info.dog.is_none() => reply(Err(RpcError {
                            code: RpcErrorCode::InvalidConfig,
                            message: format!(
                                "a sheep is already registered as `{}`; rename it or give the dog another name",
                                spec.name
                            ),
                            daemon_version: None,
                        })),
                        Ok(info) => {
                            // The one place this daemon learns of a dog it
                            // was not told about at boot. `shep adopt` and
                            // `shep enable` both arrive here, and both have
                            // just written the name into `shep.toml`, which
                            // this crate does not read. Recorded on the
                            // success arm only: the refusal above is a
                            // sheep holding the name, and that is not a dog
                            // to remember.
                            ctx.known_dogs.insert(&info.name);
                            // Wording is about the binary this shepherd
                            // resolved, not about a spawn having happened:
                            // `start_dog` is idempotent by name, so this may
                            // be a dog that was already running.
                            crate::dogs::narrate(
                                &ctx.events,
                                &info,
                                &format!(
                                    "shep has this dog enabled, running the binary at {script}"
                                ),
                            )
                            .await;
                            reply(Ok(Response::DogStarted(info)))
                        }
                        Err(err) => reply(Err(rpc_error(&err))),
                    }
                }
            }
        }
        // Through `delete` with an exact `Name` selector: disabling a dog
        // reuses the stop-then-deregister path every sheep takes rather than
        // opening a second way to end a supervised process.
        Request::DisableDog { name } => {
            match ctx.supervisor.delete(ProcessSelector::Name(name)).await {
                Ok(ids) => reply(Ok(Response::Deleted(ids))),
                Err(err) => reply(Err(rpc_error(&err))),
            }
        }
        Request::Subscribe { topics } => match TopicFilter::new(&topics) {
            Ok(filter) => Outcome::Subscribe {
                reply: Reply {
                    id,
                    result: Ok(Response::Subscribed),
                },
                filter,
            },
            Err(err) => reply(Err(RpcError {
                code: RpcErrorCode::InvalidConfig,
                message: err.to_string(),
                daemon_version: None,
            })),
        },
        Request::DogStaleness => {
            let (stale, pending) = dog_staleness(ctx).await;
            reply(Ok(Response::DogStaleness { stale, pending }))
        }
        Request::HandoverFitness => reply(Ok(Response::HandoverFitness {
            refusal: handover_refusal(ctx).await,
        })),
        Request::KillDaemon => Outcome::Shutdown(Reply {
            id,
            result: Ok(Response::ShuttingDown),
        }),
        // The acting half of `ConfigDrift` above, and the one arm here that
        // changes a running flock's config without replacing anything.
        Request::ApplyConfig { apps, reset } => match duplicate_name(&apps) {
            Some(name) => reply(Err(RpcError {
                code: RpcErrorCode::InvalidConfig,
                message: NormalizeError::DuplicateName(name).to_string(),
                daemon_version: None,
            })),
            None => match ctx.supervisor.apply_config(apps, reset).await {
                Ok(applied) => {
                    // Recorded unconditionally, as the `Scale` arm above is:
                    // an apply that reached the stored spec must reach the
                    // roll too. An app whose merge produced no honest config
                    // carries `None` and is skipped rather than invented.
                    let recorded: Vec<ResolvedApp> =
                        applied.iter().filter_map(|a| a.app.clone()).collect();
                    ctx.registry.record(&recorded);
                    reply(Ok(Response::Applied(
                        applied.into_iter().map(SheepApplied::from).collect(),
                    )))
                }
                Err(err) => reply(Err(rpc_error(&err))),
            },
        },
        // The two config-pane reads and writes. Neither takes a selector:
        // a pane edits one sheep, so an unknown name is `NotFound` here
        // rather than an empty match.
        Request::SheepConfig { name } => match ctx.supervisor.sheep_config(name.clone()).await {
            Ok(Some(view)) => reply(Ok(Response::SheepConfig(Box::new(view)))),
            Ok(None) => reply(Err(RpcError {
                code: RpcErrorCode::NotFound,
                message: format!("no sheep named {name}"),
                daemon_version: None,
            })),
            Err(err) => reply(Err(rpc_error(&err))),
        },
        Request::SetSheepEnv { name, key, value } => {
            match ctx
                .supervisor
                .set_sheep_env(name.clone(), key.clone(), value)
                .await
            {
                // Recorded exactly as `Start`, `Add`, `Scale` and
                // `ApplyConfig` record theirs, and for the reason
                // `Scale`'s arm gives: the muster roll is written from the
                // registry (`snapshot::muster`) and nothing on the restore
                // path reads the override store, so an env edit that
                // skipped this would survive a `shep daemon reload` (the
                // handover blob carries `pending`) and vanish on a cold
                // restart. The same field class behaving differently
                // depending on which request set it is the bug.
                Ok(Some(app)) => {
                    ctx.registry.record(&[app]);
                    reply(Ok(Response::SheepEnvSet { name, key }))
                }
                Ok(None) => reply(Err(RpcError {
                    code: RpcErrorCode::NotFound,
                    message: format!("no sheep named {name}"),
                    daemon_version: None,
                })),
                Err(err) => reply(Err(rpc_error(&err))),
            }
        }
        Request::SetSheepEnvBatch {
            name,
            entries,
            force,
            dry_run,
        } => {
            let values: BTreeMap<String, String> = entries
                .iter()
                .map(|(key, value)| (key.clone(), value.as_str().to_string()))
                .collect();
            match ctx
                .supervisor
                .set_sheep_env_batch(name.clone(), values, force, dry_run)
                .await
            {
                // Recorded for `SetSheepEnv`'s reason: the muster roll is
                // written from the registry and nothing on the restore path
                // reads the override store. `app` is `None` for a dry run,
                // for a refused collision, and for a batch every key of
                // which was already held, none of which wrote anything to
                // record.
                Ok(Some(batch)) => {
                    if let Some(app) = batch.app {
                        ctx.registry.record(&[app]);
                    }
                    reply(Ok(Response::SheepEnvBatch {
                        name,
                        set: batch.set,
                        unchanged: batch.unchanged,
                        collisions: batch.collisions,
                    }))
                }
                Ok(None) => reply(Err(RpcError {
                    code: RpcErrorCode::NotFound,
                    message: format!("no sheep named {name}"),
                    daemon_version: None,
                })),
                Err(err) => reply(Err(rpc_error(&err))),
            }
        }
        Request::SetSheepField { name, key, value } => {
            match ctx
                .supervisor
                .set_sheep_field(name.clone(), key.clone(), value)
                .await
            {
                // Recorded exactly as `SetSheepEnv` records its own, and
                // for that arm's reason: the muster roll is written from
                // the registry and nothing on the restore path reads the
                // override store, so a field edit that skipped this would
                // survive a `shep daemon reload` and vanish on a cold
                // restart.
                Ok(Some(set)) => {
                    ctx.registry.record(&[set.app]);
                    reply(Ok(Response::SheepFieldSet {
                        name,
                        key,
                        pending: set.pending,
                    }))
                }
                Ok(None) => reply(Err(RpcError {
                    code: RpcErrorCode::NotFound,
                    message: format!("no sheep named {name}"),
                    daemon_version: None,
                })),
                Err(err) => reply(Err(rpc_error(&err))),
            }
        }
        // The inverse of every other config door's guard: `dogs.toml`
        // holds dogs' sections and nothing else, so what this one refuses
        // is a sheep's name, not merely a dog no one has heard of. Asked
        // before the file is opened, so a mistyped name leaves no stray
        // table behind for a dog that will never exist.
        //
        // Guarded on `known_dogs`, not on the running flock: a guard on the
        // flock alone would refuse the dog most in need of configuring, one
        // that is disabled or has never started. The running flock is still
        // consulted too, as a widening, because a dog adopted and enabled
        // since this shepherd booted is not yet in the list the CLI handed
        // over at boot.
        //
        // Written here rather than through the supervisor: `dogs.toml` is
        // not supervisor state, and the file's path and the bus are both
        // already in scope here. The daemon, not the client, writes it
        // because the daemon is the only publisher of `config.dog.<name>`,
        // and a section written with nothing publishing that topic leaves
        // a running dog reading the old one.
        Request::SetDogConfig { name, toml } => {
            // A stopped engine runs no dogs, so "not running" is the honest
            // answer and `known_dogs` is left carrying the guard alone.
            let running_dog = || async {
                ctx.supervisor
                    .list_checked()
                    .await
                    .unwrap_or_default()
                    .iter()
                    .any(|info| info.name == name && info.dog.is_some())
            };
            if !ctx.known_dogs.contains(&name) && !running_dog().await {
                return reply(Err(RpcError {
                    code: RpcErrorCode::NotFound,
                    message: format!(
                        "no dog named {name}; `shep adopt` or `shep enable` makes one known \
                         to this shepherd"
                    ),
                    daemon_version: None,
                }));
            }
            match crate::dogs::set_dog_section(&ctx.dogs_config, &name, toml.as_str()) {
                Ok(()) => {
                    crate::bus::publish_dog_config_changed(
                        &ctx.events,
                        std::slice::from_ref(&name),
                    );
                    reply(Ok(Response::DogConfigSet { name }))
                }
                Err(err) => reply(Err(RpcError {
                    code: RpcErrorCode::InvalidConfig,
                    message: err.to_string(),
                    daemon_version: None,
                })),
            }
        }
        // A provider dog's push. Unguarded by design, for the reason the
        // variant's own doc gives.
        //
        // Both NAMES and every entry KEY are checked against the store's own
        // grammar: one outside it is a name no `{{secret:...}}` reference
        // could reach, so storing under it would answer `accepted` and
        // resolve nothing. Values are capped at the same `MAX_VALUE_BYTES`
        // the operator's own store enforces, since without it a socket peer
        // sets the shepherd's memory and, with `persist`, rewrites and
        // fsyncs whatever it sent on every later push.
        //
        // One offender refuses the whole push, as the namespace check
        // already does: a partial store is one a dog would report `accepted`
        // for and a spawn would refuse on, and the dog has the whole set to
        // send again.
        //
        // Handled here rather than through the supervisor: the registry is
        // not supervisor state, and this arm writes the cache file, which
        // the actor must never wait on. See `crate::secrets`.
        Request::PutSecrets {
            namespace,
            environment,
            entries,
        } => {
            let refused = [
                ("namespace", namespace.as_str()),
                ("environment", environment.as_str()),
            ]
            .into_iter()
            .chain(entries.keys().map(|key| ("key", key.as_str())))
            .find(|(_, value)| !shep_core::secrets::is_name(value));
            if let Some((field, value)) = refused {
                return reply(Err(RpcError {
                    code: RpcErrorCode::InvalidConfig,
                    message: format!(
                        "`{value}` is not a valid {field}: a name is 1 to \
                         {max} bytes of `[A-Za-z0-9._-]` and may not start with `.`",
                        max = shep_core::secrets::MAX_KEY_BYTES
                    ),
                    daemon_version: None,
                }));
            }
            // The key, never the value: an error message is the one place a
            // pushed value must not turn up (IR-41).
            let oversized = entries
                .iter()
                .map(|(key, value)| (key, value.as_str().len()))
                .find(|(_, len)| *len > shep_core::secrets::MAX_VALUE_BYTES);
            if let Some((key, len)) = oversized {
                return reply(Err(RpcError {
                    code: RpcErrorCode::InvalidConfig,
                    message: format!(
                        "the value for `{key}` is {len} bytes, over the {max}-byte limit",
                        max = shep_core::secrets::MAX_VALUE_BYTES
                    ),
                    daemon_version: None,
                }));
            }
            let values = entries
                .into_iter()
                .map(|(key, value)| (key, value.as_str().to_owned()))
                .collect();
            match ctx.provider_secrets.put(
                &namespace,
                &environment,
                values,
                persists(&ctx.dogs_config, &namespace),
            ) {
                Ok(accepted) => reply(Ok(Response::SecretsPut { accepted })),
                // The values are already in memory and already resolvable;
                // what failed is only their surviving a restart. Reported
                // rather than swallowed, so a dog whose operator asked for
                // a cache learns it is not getting one.
                Err(err) => reply(Err(RpcError {
                    code: RpcErrorCode::Internal,
                    message: format!(
                        "{namespace}/{environment} is stored, but the cache could not be \
                         written: {err}"
                    ),
                    daemon_version: None,
                })),
            }
        }
        // The catch-all `#[serde(other)]` lands on the wire, not a panic:
        // a client newer than this daemon gets a named refusal instead of
        // a dropped connection, and the connection keeps serving requests
        // this daemon does know. `daemon_version` stays `None` here: that
        // field is reserved for a `ProtocolMismatch` refusal, the only point
        // where `HelloAck::daemon_version` never reaches the client, and this
        // reply follows a successful handshake that already delivered it.
        Request::Unrecognized => reply(Err(RpcError {
            code: RpcErrorCode::Unsupported,
            message: format!(
                "this shepherd speaks protocol {PROTOCOL_VERSION} and does not \
                 implement the request the client sent"
            ),
            daemon_version: None,
        })),
        // A `Request` variant this crate added but this match forgot to wire
        // up: `Request::Unrecognized` above already answers a verb this
        // daemon has never heard of, so reaching this arm is our own bug,
        // not a hostile or newer client (IR-47).
        _ => reply(Err(RpcError {
            code: RpcErrorCode::Internal,
            message: "this daemon does not implement that request".to_string(),
            daemon_version: None,
        })),
    }
}

/// Whether `namespace`'s push may reach the cache file: the `persist` key
/// of its own `[<namespace>]` table in `dogs.toml`, or `true`.
///
/// Read per push rather than at boot, so an operator who edits the file
/// does not have to restart the shepherd for it to take.
///
/// Every way of not finding a boolean answers `true`: no file, no section,
/// no key, a key of some other type, or a file that will not parse. The
/// cache is what makes a pushed value survive a restart, and a dog whose
/// config says nothing about it wants one. An operator who does not says
/// so explicitly.
fn persists(dogs_config: &Path, namespace: &str) -> bool {
    let Ok(source) = std::fs::read_to_string(dogs_config) else {
        return true;
    };
    let Ok(config) = shep_core::config::DogsConfig::load(Some(&source)) else {
        return true;
    };
    config
        .dog
        .get(namespace)
        .and_then(|table| table.get("persist"))
        .and_then(toml::Value::as_bool)
        .unwrap_or(true)
}

/// The stages `apps` starts in, or the cycle that refuses the whole batch.
///
/// The graph spans the batch AND the registered flock: a cycle can close
/// through a sheep this request does not carry, so a Flockfile naming an
/// `api` that waits for `db` is a cycle against a flock whose `db` already
/// waits for `api`, with neither document showing one on its own.
///
/// The stages it answers with cover the batch alone. Everything else in the
/// graph is registered already and is not this request's to start; it is
/// there to be ordered around and to close a cycle.
///
/// Refused rather than warned about, which is the opposite of what a boot
/// does with the same graph in `snapshot::muster`. A boot has nobody at the
/// keyboard and must not strand a machine over a typo; an operator typed
/// this and is there to fix it.
///
/// # Errors
///
/// - [`RpcErrorCode::InvalidConfig`]: the graph holds a cycle ONE OF THIS
///   BATCH'S APPS IS IN, named as the path to break. A knot standing
///   elsewhere in the registry is left to whichever request drew it. The
///   same code `normalize_all`'s own refusal carries a few lines up, so it
///   reaches the operator as `ExitCode::InvalidConfig`.
fn staged_plan(ctx: &RpcContext, apps: &[ResolvedApp]) -> Result<BootPlan, RpcError> {
    let mut edges = ctx.registry.depends_on_by_name();
    // A dog is a node with no edges of its own, so an edge naming one
    // resolves instead of reading as a typo. `or_default` rather than
    // `insert`, for `nodes_for_with_dogs`' reason: a sheep already holding
    // that name is the node, and a second one would be started twice.
    // Where the dogs sort is immaterial here, since they are running before
    // any request arrives and the stages are filtered to the batch anyway.
    for dog in &ctx.dog_names {
        edges.entry(dog.clone()).or_default();
    }
    // The batch's own edges win over whatever the registry holds for the same
    // name: this document is the newer statement about it.
    for app in apps {
        edges.insert(app.config().name.clone(), app.config().depends_on.clone());
    }
    let plan = crate::boot_order::plan_for_names(&edges);
    let batch: BTreeSet<&str> = apps.iter().map(|app| app.config().name.as_str()).collect();
    // Only a cycle this batch is in, for the reason the `unresolved` loop
    // below gives: the graph spans the whole registry, so taking the first
    // cycle refuses a request over a knot no app it names is part of. Two
    // Flockfile loads, neither drawing a cycle, can leave one standing
    // elsewhere in the flock, and `shep start` would then refuse every app
    // in the fold with an error naming apps the operator never mentioned.
    // A cycle that closes THROUGH the batch still has a batch member in it
    // and is still refused.
    // Membership, not the path: `cycles` holds one representative path per
    // knot, so a knot of three reached by two edges names two of them and a
    // batch holding only the third would pass a test against the path.
    // `knots` is index-aligned with `cycles`, so the path is still what the
    // message renders.
    let cycle = plan
        .knots
        .iter()
        .position(|knot| knot.iter().any(|name| batch.contains(name.as_str())))
        .and_then(|knot| plan.cycles.get(knot));
    if let Some(cycle) = cycle {
        return Err(RpcError {
            code: RpcErrorCode::InvalidConfig,
            message: format!(
                "dependency cycle: {}",
                shep_core::config::graph::render_cycle(cycle)
            ),
            daemon_version: None,
        });
    }
    // Warned, not refused: a dependency on an app whose Flockfile lives in
    // another repository is legitimate, and the boot path takes the same view
    // in `snapshot::warn_about_the_graph`. Only edges this batch drew, so a
    // request is never blamed for the rest of the flock's.
    for unresolved in &plan.unresolved {
        if batch.contains(unresolved.dependent.as_str()) {
            tracing::warn!(
                sheep = %unresolved.dependent,
                missing = %unresolved.missing,
                "a dependency names nothing this flock has; starting without it"
            );
        }
    }
    Ok(BootPlan {
        stages: plan
            .stages
            .iter()
            .map(|stage| {
                stage
                    .iter()
                    .filter(|name| batch.contains(name.as_str()))
                    .cloned()
                    .collect::<Vec<String>>()
            })
            .filter(|stage| !stage.is_empty())
            .collect(),
        unresolved: plan.unresolved,
        cycles: Vec::new(),
        knots: Vec::new(),
    })
}

/// The first name two entries of an `ApplyConfig` share, if any.
///
/// `handle_apply_config` reads the override store once for the whole request
/// and writes it once at the end, so a second entry of the same name merges
/// against the store as the first entry found it: the first entry's record is
/// overwritten and nothing says so.
///
/// Refused whole rather than per app, since a document naming one app twice
/// is malformed rather than partly wrong.
///
/// Linear in a `BTreeSet`, matching `normalize_all`: a request carries the
/// apps one Flockfile declared.
fn duplicate_name(apps: &[DeclaredApp]) -> Option<String> {
    let mut seen = BTreeSet::new();
    apps.iter()
        .find(|app| !seen.insert(app.config.name.as_str()))
        .map(|app| app.config.name.clone())
}

/// The wire form of one app's load, with the merged config dropped.
///
/// `Applied` carries the whole merged [`ResolvedApp`] because `rpc.rs` hands
/// it to the registry; [`SheepApplied`] does not. A client has no use for the
/// config, and `env` is in it, so the conversion is where the config stops.
impl From<Applied> for SheepApplied {
    fn from(applied: Applied) -> Self {
        Self::new(
            applied.name,
            applied.applied,
            applied.pending,
            applied.refused,
        )
    }
}

/// The dogs this daemon has given up on, and the dogs it is still waiting to
/// hear from.
///
/// Stale is [`DogRefusals::stale`](crate::dogs::DogRefusals::stale): refused,
/// restarted from the binary on disk, refused again. A version cannot answer
/// it, since two dog builds differing only in protocol report the same one.
///
/// Pending has two sources: a dog refused once is mid-restart, and a
/// supervised dog that has never handshaken has not been asked yet, which is
/// what a carried dog is between the exec and its reconnect. Only a dog with
/// a process counts. `shep daemon reload` polls this every 50ms, so the
/// silent-dog ladder must stay on a clock rather than be driven from here.
async fn dog_staleness(ctx: &RpcContext) -> (Vec<String>, Vec<String>) {
    let stale = ctx.dog_refusals.stale();
    let mut pending = ctx.dog_refusals.restarting();
    // A stopped engine has no dogs left to wait on, so its rows are not
    // worth an error: the refusal record above is still the honest answer.
    if let Ok(infos) = ctx.supervisor.list_checked().await {
        pending.extend(crate::dogs::silent_dogs(&infos, &ctx.dog_refusals));
    }
    pending.sort();
    pending.dedup();
    (stale, pending)
}

/// Why this shepherd cannot hand its flock to a successor in place, or
/// `None` when it can.
///
/// The sentence is rendered here rather than as a structured reason on the
/// wire, for the reason [`Response::HandoverFitness`] gives: the client does
/// nothing with it but print it.
///
/// An engine that has stopped is a refusal too, not an error: the caller
/// asked whether to signal a shepherd.
#[cfg(unix)]
async fn handover_refusal(ctx: &RpcContext) -> Option<String> {
    match ctx.supervisor.handover_fitness().await {
        Ok(crate::handover::Fitness::Carryable) => None,
        Ok(crate::handover::Fitness::Refused(reason)) => Some(reason.to_string()),
        Err(err) => Some(format!(
            "this shepherd could not check whether its flock can be handed over ({err})"
        )),
    }
}

/// Windows has no `execve`, so there is no image for a successor to become
/// and every flock is refused.
///
/// A refusal rather than an unimplemented request: this one is answered, and
/// the answer sends `shep daemon reload` to the stop-and-start arm.
#[cfg(windows)]
#[expect(
    clippy::unused_async,
    reason = "one signature for both platforms; the unix arm awaits the supervisor"
)]
async fn handover_refusal(_ctx: &RpcContext) -> Option<String> {
    Some(
        "this shepherd runs on Windows, which has no `execve`, so its flock cannot be handed to \
         a successor in place"
            .to_string(),
    )
}

/// Fills in each running sheep's live CPU and memory.
///
/// Sampled here rather than inside the supervisor: the actor must never
/// block, and the reading is a syscall walk over the host's whole process
/// table, so it runs on a blocking-pool thread.
///
/// Joined by pid, not by id: [`StatsState`] keys on the root pid it was armed
/// against, which is the number [`ProcessInfo::pid`] carries. Only `ListFlock`
/// and `Describe` call this; the lifecycle verbs answer with [`ProcessInfo`]
/// too, but none of them is where an operator reads resource usage.
async fn with_live_stats(stats: &Arc<StatsState>, mut infos: Vec<ProcessInfo>) -> Vec<ProcessInfo> {
    let stats = Arc::clone(stats);
    let Ok(sample) = tokio::task::spawn_blocking(move || stats.sample_now()).await else {
        // The blocking pool is gone or the task panicked: report the flock
        // without stats rather than fail a listing over a decoration.
        return infos;
    };
    for info in &mut infos {
        if let Some(reading) = info.pid.and_then(|pid| sample.get(&pid)) {
            info.cpu_percent = reading.cpu_percent;
            info.memory_bytes = Some(reading.memory_bytes);
        }
    }
    infos
}

/// Fills in each dog's two connection facts, which no sheep has and the
/// supervisor does not hold: whether it has ever answered this shepherd, and
/// whether this shepherd has given up on it.
///
/// Connection state lives in [`DogRefusals`](crate::dogs::DogRefusals) on the
/// RPC context, so it is joined here as `with_live_stats` is: two map lookups
/// per row, and `stale()` called once for the whole listing. Both fields,
/// because a dog spawned a moment ago and one this shepherd has stopped
/// restarting are both `handshook: Some(false)` with a live process.
///
/// Applied to `ListFlock` and `Describe` alone. A sheep is skipped rather
/// than set to `Some(false)`, having no handshake with this shepherd at all.
fn with_dog_contact(
    refusals: &crate::dogs::DogRefusals,
    mut infos: Vec<ProcessInfo>,
) -> Vec<ProcessInfo> {
    let stale = refusals.stale();
    for info in &mut infos {
        if info.dog.is_some() {
            info.handshook = Some(refusals.has_handshook(&info.name));
            info.dog_stale = Some(stale.contains(&info.name));
        }
    }
    infos
}

/// Fills each row's `lambs` from a fresh walk of the process table.
///
/// Applied to `Describe` and to nothing else: the walk is a second pass over
/// every process on the machine, and a flock listing is the thing an operator
/// leaves running in a loop.
///
/// A row with no pid is left `None` rather than `Some(vec![])`, which is the
/// "not walked" case the field's own doc distinguishes from "walked and
/// empty".
async fn with_lambs(stats: &Arc<StatsState>, mut infos: Vec<ProcessInfo>) -> Vec<ProcessInfo> {
    if infos.iter().all(|info| info.pid.is_none()) {
        // Nothing to walk for: skip the table refresh entirely rather than
        // pay for it and assign `None` anyway.
        return infos;
    }
    let stats = Arc::clone(stats);
    let pids: Vec<u32> = infos.iter().filter_map(|info| info.pid).collect();
    let Ok(walked) = tokio::task::spawn_blocking(move || {
        // One index for the whole reply: `describe all` walks the machine's
        // process table once, not once per row.
        let index = stats.lamb_index();
        pids.into_iter()
            .map(|pid| (pid, stats.lambs_of(&index, pid)))
            .collect::<HashMap<u32, Vec<Lamb>>>()
    })
    .await
    else {
        // The blocking pool is gone or the task panicked: describe the sheep
        // without their trees rather than fail the request over a decoration.
        return infos;
    };
    for info in &mut infos {
        if let Some(lambs) = info.pid.and_then(|pid| walked.get(&pid)) {
            info.lambs = Some(lambs.clone());
        }
    }
    infos
}

fn rpc_error(err: &SupervisorError) -> RpcError {
    match err {
        SupervisorError::NotFound => not_found(),
        SupervisorError::SpawnFailed(msg) => RpcError {
            code: RpcErrorCode::SpawnFailed,
            message: msg.clone(),
            daemon_version: None,
        },
        // The same code as `SpawnFailed`: `RpcErrorCode` is versioned, and a
        // client predating a new code cannot decode the reply at all. The
        // bare payload rather than `err.to_string()`, since this message
        // already opens with "nothing in this batch was registered".
        SupervisorError::CannotStart(msg) => RpcError {
            code: RpcErrorCode::SpawnFailed,
            message: msg.clone(),
            daemon_version: None,
        },
        // `Internal`, an unexpected daemon-side failure, and no code of its
        // own since a client predating a new one could not decode the reply.
        // `err.to_string()` rather than the bare payload: `Display` is the
        // only thing distinguishing the two once they share a code.
        SupervisorError::ReopenFailed(_) | SupervisorError::FlushFailed(_) => RpcError {
            code: RpcErrorCode::Internal,
            message: err.to_string(),
            daemon_version: None,
        },
        // `Internal` under protest: an app already being reloaded is a
        // conflict the caller can act on, and the wire has no code for one.
        // `Display` names the app, which is the part that says what to do.
        SupervisorError::ReloadInFlight(_) => RpcError {
            code: RpcErrorCode::Internal,
            message: err.to_string(),
            daemon_version: None,
        },
        // Every `InvalidScale` is something the caller can ask differently: a
        // count of `0`, a dog, or an app whose earlier scale is still
        // shutting instances down. That last one is a conflict, like
        // `ReloadInFlight`, and the wire has no code for one.
        SupervisorError::InvalidScale(msg) => RpcError {
            code: RpcErrorCode::InvalidConfig,
            message: msg.clone(),
            daemon_version: None,
        },
        // `InvalidConfig`, like `InvalidScale` above and for its reason: a
        // request aimed at a dog is one the caller can aim elsewhere. The
        // bare payload is the same sentence `apply_one` puts in front of an
        // operator whose Flockfile named a dog, so the two doors read alike.
        SupervisorError::IsADog(msg) => RpcError {
            code: RpcErrorCode::InvalidConfig,
            message: msg.clone(),
            daemon_version: None,
        },
        // `InvalidConfig`, like `InvalidScale` above and for its reason:
        // this is something the caller asked for that it can ask
        // differently, and telling an operator "unexpected daemon-side
        // failure" about their own env key would send them to the wrong
        // place entirely. The bare payload is `normalize`'s own refusal,
        // which already names the key.
        SupervisorError::InvalidEnv(msg) => RpcError {
            code: RpcErrorCode::InvalidConfig,
            message: msg.clone(),
            daemon_version: None,
        },
        // `InvalidConfig`, beside `InvalidEnv` above and for its reason:
        // every shape that reaches it is the caller's own key or value,
        // which it can ask differently. The bare payload rather than
        // `err.to_string()`, again like `InvalidEnv`: the message already
        // names the field.
        SupervisorError::InvalidField(msg) => RpcError {
            code: RpcErrorCode::InvalidConfig,
            message: msg.clone(),
            daemon_version: None,
        },
        // `Internal`, on the same rule the log-maintenance pair above
        // states: an override store that cannot be read or written is an
        // unexpected daemon-side failure, and there is no code for it that
        // a client predating this build could decode. `err.to_string()`
        // rather than the bare payload, so the reader is told the store was
        // the thing that failed and not the request.
        SupervisorError::Overrides(_) => RpcError {
            code: RpcErrorCode::Internal,
            message: err.to_string(),
            daemon_version: None,
        },
        SupervisorError::EngineStopped => RpcError {
            code: RpcErrorCode::Internal,
            message: "the supervisor engine has stopped".to_string(),
            daemon_version: None,
        },
    }
}

/// A restart or reload the selector matched more than one sheep for, grouped
/// into the stages it walks.
struct OrderedWalk {
    /// The matched names, dependencies first, one `Vec` per stage.
    stages: Vec<Vec<String>>,
    /// The matched names another matched name waits for.
    ///
    /// The only ones a stage has to be held for: a sheep nothing in this
    /// request depends on can settle after the walk has moved on, and waiting
    /// for it would let one slow app that nobody waits for cost every later
    /// stage its bound. The same gate `start_in_stages` builds.
    depended_on: BTreeSet<String>,
}

/// How `selector` is walked, or `None` when it names at most one sheep and
/// there is nothing to order.
///
/// A single-target `shep restart web` therefore goes out as the one
/// supervisor call it has always been, deadlines and refusal and all. Two
/// matches or more is where a fold's shape starts to matter.
///
/// The match is computed the way `Actor::matching_ids` computes it, dogs and
/// all, so the walk covers what the supervisor would have matched. It is
/// still a second pass over a listing the actor has since moved on from: a
/// sheep that exits between the two is one this walk names and the supervisor
/// no longer matches, which costs a `NotFound` warning for that name.
fn ordered_walk(
    ctx: &RpcContext,
    selector: &ProcessSelector,
    flock: &[ProcessInfo],
) -> Option<OrderedWalk> {
    let exact = selector.is_exact();
    let matched: BTreeSet<String> = flock
        .iter()
        .filter(|info| exact || info.dog.is_none())
        .filter(|info| selector.matches(&info.name, info.id, info.fold.as_deref(), info.instance))
        .map(|info| info.name.clone())
        .collect();
    if matched.len() < 2 {
        return None;
    }

    let edges = ctx.registry.depends_on_by_name();
    let plan = crate::boot_order::plan_for_names(&edges);
    let mut stages: Vec<Vec<String>> = plan
        .stages
        .iter()
        .map(|stage| {
            stage
                .iter()
                .filter(|name| matched.contains(name.as_str()))
                .cloned()
                .collect::<Vec<String>>()
        })
        .filter(|stage| !stage.is_empty())
        .collect();
    // A matched name the registry does not hold has no node in the plan, and
    // dropping it here would leave a sheep the operator named unrestarted
    // while the reply says otherwise. It goes last, since nothing here can say
    // what waits for it. A `shep dev` teardown clearing the registry under a
    // live flock is the shape that gets here; a dog does not, since a selector
    // reaching one is exact and an exact selector matches one name.
    let placed: BTreeSet<&str> = stages.iter().flatten().map(String::as_str).collect();
    let unplaced: Vec<String> = matched
        .iter()
        .filter(|name| !placed.contains(name.as_str()))
        .cloned()
        .collect();
    if !unplaced.is_empty() {
        stages.push(unplaced);
    }

    let depended_on = matched
        .iter()
        .flat_map(|name| matched_dependencies(&edges, name, &matched))
        .collect();
    Some(OrderedWalk {
        stages,
        depended_on,
    })
}

/// Every name in `matched` that `start` waits for, however many hops away.
///
/// The transitive walk is the point. A direct-edge intersection loses a
/// dependency whose intermediate the selector missed: with `web -> mid ->
/// db` matched at the ends only, `mid` is not in `matched`, so `web` reads
/// as waiting for nothing and its stage restarts against a `db` no stage
/// held. A regex or a fold selector is how that shape arrives; `All` cannot
/// draw it, since it matches every hop.
///
/// `start` itself is never returned, so a name in a knot does not end up
/// waiting for its own stage.
fn matched_dependencies(
    edges: &BTreeMap<String, Vec<String>>,
    start: &str,
    matched: &BTreeSet<String>,
) -> BTreeSet<String> {
    let mut found = BTreeSet::new();
    let mut seen: BTreeSet<&str> = [start].into_iter().collect();
    let mut frontier: Vec<&str> = vec![start];
    while let Some(name) = frontier.pop() {
        for dependency in edges.get(name).into_iter().flatten() {
            if !seen.insert(dependency.as_str()) {
                continue;
            }
            if matched.contains(dependency.as_str()) {
                found.insert(dependency.clone());
            }
            frontier.push(dependency.as_str());
        }
    }
    found
}

/// How long ONE INSTANCE gets to settle, before the stage's slack.
///
/// A restart waits out `listen_timeout`; a reload waits out
/// `graceful_timeout` as well, which is the pair its own swap is already
/// bounded by (`Actor::arm_reload_deadline`). A name the registry does not
/// hold falls back to nothing at all, leaving its stage the slack: nothing
/// else here knows what that sheep's deadlines are, and a stage that
/// advances early is better than one that hangs on a guess.
fn settle_bound(ctx: &RpcContext, name: &str, reloading: bool) -> Duration {
    let (readiness, drain) = ctx.registry.timeouts_of(name).unwrap_or_default();
    let drain = if reloading { drain } else { Duration::ZERO };
    readiness + drain
}

/// The longest bound any of a restart stage's members asks for.
///
/// One instance's worth, unlike [`reload_stage_bound`]: a restart's
/// instances go down and come back together, so the stage costs the
/// slowest of them rather than the sum.
fn stage_bound(ctx: &RpcContext, stage: &[String]) -> Duration {
    stage
        .iter()
        .map(|name| settle_bound(ctx, name, false))
        .max()
        .unwrap_or_default()
        + crate::boot_order::STAGE_SLACK
}

/// [`stage_bound`] for a reload, sized by the swaps each member still owes.
///
/// `advance_reload` replaces one instance at a time, so a three-instance app
/// costs three drains and three readiness waits, not one of each. A per-app
/// bound is under a third of that at the defaults, so the stage times out,
/// logs, and lets the dependant reload against a dependency that is still
/// half swapped, which is the failure the walk exists to prevent, reached
/// quietly.
///
/// The counts come from `waiting`, which is the instances the reload
/// answered as `Online`. An instance a failed earlier reload left up and not
/// serving reads `Starting` there and is missing from the count, so this
/// bound can only be short, never long: such a stage can advance early and
/// can never hang.
fn reload_stage_bound(ctx: &RpcContext, waiting: &BTreeMap<String, usize>) -> Duration {
    waiting
        .iter()
        .map(|(name, swaps)| {
            settle_bound(ctx, name, true).saturating_mul(u32::try_from(*swaps).unwrap_or(u32::MAX))
        })
        .max()
        .unwrap_or_default()
        + crate::boot_order::STAGE_SLACK
}

/// `Restart`'s arm: ordered when the selector matches several sheep, the
/// plain supervisor call when it matches one.
///
/// The refused half of the reply is a walk's alone: the supervisor refuses a
/// selector matching one app whole, so that arm answers `Err` and has
/// nothing to name.
async fn restart_request(id: u64, spec: SelectorSpec, ctx: &RpcContext) -> Outcome {
    let result = match selector_of(spec) {
        Err(err) => Err(err),
        Ok(selector) => match walk_for(ctx, &selector).await {
            None => ctx
                .supervisor
                .restart(selector)
                .await
                .map(|accepted| Response::Restarted {
                    accepted,
                    refused: Vec::new(),
                })
                .map_err(|err| rpc_error(&err)),
            Some(walk) => restart_in_stages(ctx, &walk)
                .await
                .map(|(accepted, refused)| Response::Restarted { accepted, refused }),
        },
    };
    Outcome::Reply(Reply { id, result })
}

/// `Reload`'s arm, mirroring [`restart_request`] down to the refused half of
/// its reply.
async fn reload_request(id: u64, spec: SelectorSpec, ctx: &RpcContext) -> Outcome {
    let result = match selector_of(spec) {
        Err(err) => Err(err),
        Ok(selector) => match walk_for(ctx, &selector).await {
            None => ctx
                .supervisor
                .reload(selector)
                .await
                .map(|accepted| Response::Reloading {
                    accepted,
                    refused: Vec::new(),
                })
                .map_err(|err| rpc_error(&err)),
            Some(walk) => reload_in_stages(ctx, &walk)
                .await
                .map(|(accepted, refused)| Response::Reloading { accepted, refused }),
        },
    };
    Outcome::Reply(Reply { id, result })
}

/// [`ordered_walk`] over a fresh listing, or `None` when there is nothing to
/// order, which includes an actor that could not answer: the supervisor call
/// the caller falls back to reports that failure itself, in its own words.
async fn walk_for(ctx: &RpcContext, selector: &ProcessSelector) -> Option<OrderedWalk> {
    let flock = ctx.supervisor.list_checked().await.ok()?;
    ordered_walk(ctx, selector, &flock)
}

/// Restarts each stage's members at once, then holds the walk until the ones
/// a later stage waits for are back.
///
/// A member that fails is warned about and the walk continues, the rule
/// `start_in_stages` takes under `BatchPolicy::PerApp` and for its reason: a
/// fold half restarted is worse than a fold restarted around one bad app.
///
/// Every member that was refused is named in the second half of the answer,
/// the rule `reload_in_stages` states in full: a walk that restarted
/// something still answers `Ok`, and exit 0 with a row silently missing is
/// what carrying the names replaces.
///
/// # Errors
///
/// - The first member's refusal, when no member restarted at all. A request
///   that moved nothing has to say so, and a selector matching one dog that
///   is mid-shutdown would otherwise answer `Ok` with an empty table.
async fn restart_in_stages(
    ctx: &RpcContext,
    walk: &OrderedWalk,
) -> Result<(Vec<ProcessInfo>, Vec<SheepRefusal>), RpcError> {
    let mut restarted = Vec::new();
    let mut refused: Vec<SheepRefusal> = Vec::new();
    let mut refusal = None;
    for stage in &walk.stages {
        // Subscribed before the calls for the reason `start_in_stages` gives:
        // a receiver taken afterwards starts past a fast sheep's `Online`.
        let rx = ctx.events.subscribe();
        // Concurrently inside a stage, serially across them. Awaiting members
        // in turn would make the walk cost the SUM of their kill ladders and
        // readiness deadlines where an unordered restart costs the longest
        // one; `stop_in_reverse` carries the same note.
        let outcomes = futures_util::future::join_all(stage.iter().map(|name| async move {
            (
                name,
                ctx.supervisor
                    .restart(ProcessSelector::Name(name.clone()))
                    .await,
            )
        }))
        .await;
        for (name, outcome) in outcomes {
            match outcome {
                Ok(infos) => restarted.extend(infos),
                // A `NotFound` here is not necessarily a defect: the walk
                // was planned from a listing the actor has since moved on
                // from, so a sheep that exited in between is one this names
                // and the supervisor no longer matches. The message says so
                // rather than sending an operator looking for a bug.
                Err(err) => {
                    tracing::warn!(
                        sheep = %name,
                        %err,
                        "a sheep did not restart in its stage; it may have left the flock since \
                         the walk was planned"
                    );
                    // Every one, where `refusal` below keeps the first:
                    // that one is only ever read when nothing restarted.
                    refused.push(SheepRefusal::new(name.clone(), err.to_string()));
                    refusal.get_or_insert(err);
                }
            }
        }

        let waiting: BTreeSet<String> = stage
            .iter()
            .filter(|name| walk.depended_on.contains(name.as_str()))
            .cloned()
            .collect();
        if waiting.is_empty() {
            continue;
        }
        let bound = stage_bound(ctx, stage);
        let unsettled = crate::boot_order::await_stage(rx, waiting, bound, &ctx.supervisor).await;
        warn_about_unsettled(&unsettled);
    }
    finished(restarted, refusal).map(|rows| (rows, refused))
}

/// [`restart_in_stages`] for a reload: same walk, a different call and a
/// different definition of done.
///
/// A stage is done when every member has emitted a `Reloaded` for each
/// instance the reload accepted, or a `ReloadAbandoned` for the app. Which of
/// the two reloads an app gets is still `ReloadMode::of`'s call, and the
/// abandonment path is untouched: this waits on it rather than around it.
///
/// Only the instances the reload answered as `Online` are counted, since
/// those are the ones `reload_eligible` lets a swap replace. An instance a
/// failed earlier reload left up and not serving is `Starting` here and is
/// undercounted, so a stage holding one can advance while its last swap is
/// still running; the alternative is counting instances no swap will ever
/// reach and paying the bound on every ordinary reload.
///
/// Every member that was refused is named in the second half of the answer,
/// so the client can print the apps this walk went around and exit non-zero
/// over them. A walk that reloaded something still answers `Ok`: refusing
/// forty apps because one is busy is the whole-selector rule this walk
/// exists to get away from, and exit 0 with a row silently missing is what
/// carrying the names replaces.
///
/// # Errors
///
/// - The first member's refusal, when no member was accepted. `ReloadInFlight`
///   is the one an operator meets: the supervisor refuses a selector whole
///   when any app it names is already reloading, and a staged walk asks per
///   app, so an app already reloading now refuses its own stage while the
///   rest of the fold goes ahead. Its stage is still held for the reload
///   already running, which a dependant has the same reason to wait out.
async fn reload_in_stages(
    ctx: &RpcContext,
    walk: &OrderedWalk,
) -> Result<(Vec<ProcessInfo>, Vec<SheepRefusal>), RpcError> {
    let mut accepted = Vec::new();
    let mut refused: Vec<SheepRefusal> = Vec::new();
    let mut refusal = None;
    for stage in &walk.stages {
        let rx = ctx.events.subscribe();
        let outcomes = futures_util::future::join_all(stage.iter().map(|name| async move {
            (
                name,
                ctx.supervisor
                    .reload(ProcessSelector::Name(name.clone()))
                    .await,
            )
        }))
        .await;
        let mut waiting: BTreeMap<String, usize> = BTreeMap::new();
        for (name, outcome) in outcomes {
            match outcome {
                Ok(infos) => {
                    if walk.depended_on.contains(name.as_str()) {
                        let swaps = infos
                            .iter()
                            .filter(|info| info.status == ProcStatus::Online)
                            .count();
                        if swaps > 0 {
                            waiting.insert(name.clone(), swaps);
                        }
                    }
                    accepted.extend(infos);
                }
                // `restart_in_stages`' note about a `NotFound` applies here
                // too.
                Err(err) => {
                    // An app already reloading is refused per app now, so it
                    // contributes no swaps of its own and the stage would
                    // return at once, letting a dependant swap against a
                    // dependency that is mid-swap. The reload in flight is
                    // still a reload this stage's dependants have to wait
                    // out, so it is waited for as one: any `Reloaded` or the
                    // `ReloadAbandoned` that ends it finishes the name.
                    //
                    // One swap, and a clustered app owes one per instance.
                    // Nothing here can see how far the reload in flight has
                    // got, so a three-instance dependency finishes this wait
                    // at whichever swap lands next and a dependant can go a
                    // swap or two early. Accepted, for `reload_in_stages`'
                    // own reason: a wait sized by a count this side is
                    // guessing at can hang, and an early stage cannot.
                    if matches!(err, SupervisorError::ReloadInFlight(_))
                        && walk.depended_on.contains(name.as_str())
                    {
                        waiting.insert(name.clone(), 1);
                    }
                    tracing::warn!(
                        sheep = %name,
                        %err,
                        "a sheep did not reload in its stage; it may have left the flock since \
                         the walk was planned"
                    );
                    // The daemon log is not somewhere a deploy script looks,
                    // so the name rides back on the reply as well.
                    refused.push(SheepRefusal::new(name.clone(), err.to_string()));
                    refusal.get_or_insert(err);
                }
            }
        }

        if waiting.is_empty() {
            continue;
        }
        let bound = reload_stage_bound(ctx, &waiting);
        let unsettled = crate::boot_order::await_reloads(rx, waiting, bound).await;
        warn_about_unsettled(&unsettled);
    }
    finished(accepted, refusal).map(|rows| (rows, refused))
}

/// A stage that ran out of time, named. Nothing is retried and the walk goes
/// on: a stage held to its bound has already cost the operator that wait, and
/// stopping here would leave the fold half done.
fn warn_about_unsettled(unsettled: &BTreeSet<String>) {
    if unsettled.is_empty() {
        return;
    }
    let names: Vec<&str> = unsettled.iter().map(String::as_str).collect();
    tracing::warn!(
        unsettled = ?names,
        "a stage did not settle inside its bound; advancing anyway"
    );
}

/// The walk's answer: the rows it moved, sorted as every operator-facing
/// listing is, or the refusal it kept when it moved nothing.
fn finished(
    mut rows: Vec<ProcessInfo>,
    refusal: Option<SupervisorError>,
) -> Result<Vec<ProcessInfo>, RpcError> {
    match refusal {
        Some(err) if rows.is_empty() => Err(rpc_error(&err)),
        _ => {
            // Sorted here rather than left in stage order: each supervisor
            // call sorts its own answer, and a table stitched from several is
            // otherwise ordered by the graph, which is not what an operator
            // reading a flock listing expects.
            shep_core::protocol::sort_flock(&mut rows);
            Ok(rows)
        }
    }
}

fn not_found() -> RpcError {
    RpcError {
        code: RpcErrorCode::NotFound,
        message: "selector matched no registered sheep".to_string(),
        daemon_version: None,
    }
}

fn selector_of(spec: SelectorSpec) -> Result<ProcessSelector, RpcError> {
    ProcessSelector::try_from(spec).map_err(|err| RpcError {
        code: RpcErrorCode::InvalidConfig,
        message: err.to_string(),
        daemon_version: None,
    })
}

/// The helper every selector-in, flock-out verb shares: convert the selector,
/// call the supervisor, map the hits through the passed `Response`
/// constructor.
///
/// The future bound is stated, not inferred, because the whole chain is
/// awaited inside the per-connection `tokio::spawn`.
async fn selector_call<F, Fut>(
    id: u64,
    spec: SelectorSpec,
    call: F,
    ok: fn(Vec<ProcessInfo>) -> Response,
) -> Outcome
where
    F: FnOnce(ProcessSelector) -> Fut + Send,
    Fut: Future<Output = Result<Vec<ProcessInfo>, SupervisorError>> + Send,
{
    let result = match selector_of(spec) {
        Ok(selector) => call(selector).await.map(ok).map_err(|err| rpc_error(&err)),
        Err(err) => Err(err),
    };
    Outcome::Reply(Reply { id, result })
}

/// `Trigger`'s own resolve-then-map path. [`selector_call`] cannot serve it:
/// that helper maps `Vec<ProcessInfo>`, and `Response::Triggered` carries
/// `Vec<ActionReply>`, a row `ProcessInfo` cannot hold a reply body on.
///
/// How long each app gets to answer is `AppConfig::action_timeout`, one value
/// per matched sheep, read where the wait is armed (`Actor::begin_action`).
/// `shep_core::config::normalize` refuses only a value no caller could ever
/// outlast; one past the default budget is accepted, and the caller's own
/// deadline decides whether that pays off.
async fn trigger(
    id: u64,
    spec: SelectorSpec,
    action: String,
    params: Option<String>,
    ctx: &RpcContext,
) -> Outcome {
    let result = match selector_of(spec) {
        Err(err) => Err(err),
        Ok(selector) => ctx
            .supervisor
            .trigger(selector, action, params)
            .await
            .map(Response::Triggered)
            .map_err(|err| rpc_error(&err)),
    };
    Outcome::Reply(Reply { id, result })
}

/// `Signal`'s own resolve-then-map path, mirroring [`trigger`]. The signal
/// name is re-validated here even though the CLI validated it too: peer input
/// is untrusted, the rule `Request::Start` follows a few arms up.
async fn signal_request(id: u64, spec: SelectorSpec, signal: String, ctx: &RpcContext) -> Outcome {
    let result = match OperatorSignal::parse(&signal) {
        None => Err(RpcError {
            code: RpcErrorCode::InvalidConfig,
            message: format!(
                "`{signal}` is not a signal shep will send; accepted: {}",
                OperatorSignal::ACCEPTED.join(", ")
            ),
            daemon_version: None,
        }),
        Some(sig) => match selector_of(spec) {
            Err(err) => Err(err),
            Ok(selector) => ctx
                .supervisor
                .signal(selector, sig)
                .await
                .map(Response::Signalled)
                .map_err(|err| rpc_error(&err)),
        },
    };
    Outcome::Reply(Reply { id, result })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fake::{FIRST_SCRIPTED_PID, ProcScript};
    use crate::limits::MEMORY_POLL_INTERVAL;
    use crate::testing::{
        Harness, SCRIPTED_TREE_BYTES, harness, harness_identifying, harness_with_stats, identity,
    };
    use shep_core::config::{AppConfig, ApplyGroup, DeclaredApp, ResetDepth, apply_group};
    use shep_core::protocol::{
        ActionOutcome, ActionReply, DogSource, ProcessEventKind, Request, Response, RpcErrorCode,
        SelectorSpec,
    };
    use shep_core::values::UpDuration;
    use std::collections::{BTreeMap, BTreeSet};
    use tokio::time::Instant;

    /// Dispatches on a connection of its own, shadowing [`super::dispatch`]
    /// so no case here has to name a [`ConnId`]. One fresh id per call:
    /// nothing here spans two requests on the same connection.
    async fn dispatch(envelope: Envelope, ctx: &RpcContext) -> Outcome {
        super::dispatch(envelope, ConnId::next(), ctx).await
    }

    fn envelope(id: u64, body: Request) -> Envelope {
        Envelope {
            id,
            deadline_ms: None,
            body,
        }
    }

    fn reply_of(outcome: Outcome) -> Reply {
        match outcome {
            Outcome::Reply(reply) | Outcome::Subscribe { reply, .. } | Outcome::Shutdown(reply) => {
                reply
            }
        }
    }

    #[tokio::test(start_paused = true)]
    async fn ping_answers_pong_on_the_same_envelope_id() {
        let h = harness(vec![]);
        let reply = reply_of(dispatch(envelope(9, Request::Ping), &h.ctx).await);
        assert_eq!(reply.id, 9);
        assert_eq!(reply.result.unwrap(), Response::Pong);
    }

    #[tokio::test(start_paused = true)]
    async fn start_registers_the_config_and_lists_it() {
        let h = harness(vec![ProcScript::never_exits()]);
        let started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Started(infos) = started.result.unwrap() else {
            panic!("expected started")
        };
        assert_eq!(infos.len(), 1);
        assert_eq!(infos[0].status, ProcStatus::Online);

        // The roll can only be built if Start recorded the config.
        let roll = h.ctx.registry.roll(&infos, 0);
        assert_eq!(roll.apps.len(), 1);
        assert_eq!(roll.apps[0].app.script, "./srv");

        let listed = reply_of(dispatch(envelope(2, Request::ListFlock), &h.ctx).await);
        let Response::Flock(flock) = listed.result.unwrap() else {
            panic!("expected flock")
        };
        assert_eq!(flock.len(), 1);
    }

    #[tokio::test(start_paused = true)]
    async fn start_re_normalizes_untrusted_peer_config() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(reply.result.unwrap_err().code, RpcErrorCode::InvalidConfig);
    }

    /// The harness is scripted with no processes, which is the forcing
    /// mechanism: `ScriptedRunner::spawn` refuses with `script exhausted`
    /// once the list is empty, so a build that routed this at `do_start`
    /// lands `Errored` rather than `Stopped` with no pid.
    #[tokio::test(start_paused = true)]
    async fn add_registers_a_stopped_member_and_spawns_nothing() {
        let h = harness(vec![]);
        let added = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Add {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Added(infos) = added.result.unwrap() else {
            panic!("expected added")
        };
        assert_eq!(infos.len(), 1);
        assert_eq!(infos[0].status, ProcStatus::Stopped);
        assert_eq!(infos[0].pid, None, "nothing was spawned");

        // The roll can only be built if `Add` recorded the config, and an app
        // registered and never started is precisely the one a roll would
        // otherwise forget.
        let roll = h.ctx.registry.roll(&infos, 0);
        assert_eq!(roll.apps.len(), 1);
        assert_eq!(roll.apps[0].app.script, "./srv");
        assert_eq!(roll.apps[0].instances_running, 0);

        let listed = reply_of(dispatch(envelope(2, Request::ListFlock), &h.ctx).await);
        let Response::Flock(flock) = listed.result.unwrap() else {
            panic!("expected flock")
        };
        assert_eq!(
            flock.len(),
            1,
            "it is a member of the flock, just a still one"
        );
    }

    /// fails if `Add` trusts what a peer sent it. Same rule as `Start`: the
    /// socket is the boundary, and an empty name is the shape `normalize`
    /// refuses.
    #[tokio::test(start_paused = true)]
    async fn add_re_normalizes_untrusted_peer_config() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Add {
                        apps: vec![AppConfig::minimal("", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(reply.result.unwrap_err().code, RpcErrorCode::InvalidConfig);
    }

    /// The sheep is online, the case that matters: re-running `shep add
    /// Flockfile.toml` after editing the file must not stop a service. One
    /// script, so a second spawn would fail rather than pass quietly.
    #[tokio::test(start_paused = true)]
    async fn a_second_add_leaves_a_running_sheep_exactly_as_it_was() {
        let h = harness(vec![ProcScript::never_exits()]);
        let started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Started(before) = started.result.unwrap() else {
            panic!("expected started")
        };

        let added = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Add {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Added(after) = added.result.unwrap() else {
            panic!("expected added")
        };
        assert_eq!(after.len(), 1);
        assert_eq!(
            after[0].id, before[0].id,
            "the same sheep, not a second one"
        );
        assert_eq!(after[0].status, ProcStatus::Online, "still running");
        assert_eq!(after[0].pid, before[0].pid, "the same process");

        let listed = reply_of(dispatch(envelope(3, Request::ListFlock), &h.ctx).await);
        let Response::Flock(flock) = listed.result.unwrap() else {
            panic!("expected flock")
        };
        assert_eq!(flock.len(), 1, "one row, not two");
    }

    /// An app that waits on `depends_on`, for the staged-start cases below.
    fn waiting_on(name: &str, depends_on: &[&str]) -> AppConfig {
        let mut app = AppConfig::minimal(name, "./srv");
        app.depends_on = depends_on.iter().map(|n| (*n).to_string()).collect();
        app
    }

    #[tokio::test(start_paused = true)]
    async fn a_start_runs_its_batch_in_dependency_order() {
        // fails if `Start` hands the whole batch to one `start` call: the
        // reply would carry the apps in the order the request listed them,
        // which is the reverse of the order they have to come up in.
        let h = harness(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
        let started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![waiting_on("api", &["db"]), waiting_on("db", &[])],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Started(infos) = started.result.unwrap() else {
            panic!("expected started")
        };
        let order: Vec<&str> = infos.iter().map(|info| info.name.as_str()).collect();
        assert_eq!(order, ["db", "api"], "stage order, not request order");
    }

    #[tokio::test(start_paused = true)]
    async fn a_cycle_closing_through_a_registered_sheep_is_refused_too() {
        // fails if the graph spans only the incoming batch: neither document
        // shows a cycle on its own, and the flock is where the edge back is.
        let h = harness(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
        let first = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![waiting_on("db", &["api"])],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(
            first.result.is_ok(),
            "one app, one edge to a name nobody has"
        );

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Start {
                        apps: vec![waiting_on("api", &["db"])],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let err = reply.result.unwrap_err();
        assert_eq!(err.code, RpcErrorCode::InvalidConfig);
        assert!(
            err.message.contains("api") && err.message.contains("db"),
            "both ends of the cycle must be named: {}",
            err.message
        );
    }

    #[tokio::test(start_paused = true)]
    async fn a_batch_in_a_knot_the_named_path_leaves_out_is_still_refused() {
        // fails if the cycle check tests the batch against the representative
        // PATH instead of the knot's members: `plan` reports one path per
        // knot, so a knot of three reached through two edges names only two
        // of them, and the third starts into a dependency that can never be
        // satisfied while the operator is told nothing.
        let h = harness(vec![ProcScript::never_exits(); 3]);
        let _started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: ["api", "db", "web"]
                            .iter()
                            .map(|name| AppConfig::minimal(name, "./srv"))
                            .collect(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        // `api -> {db, web}`, `db -> api`, `web -> api`: one knot holding all
        // three, planted through `ApplyConfig` the way the sibling test does.
        for (name, waits_for) in [
            ("api", vec!["db", "web"]),
            ("db", vec!["api"]),
            ("web", vec!["api"]),
        ] {
            let mut config = AppConfig::minimal(name, "./srv");
            config.depends_on = waits_for.iter().map(|n| (*n).to_string()).collect();
            let reply = reply_of(
                dispatch(
                    envelope(
                        2,
                        Request::ApplyConfig {
                            apps: vec![DeclaredApp {
                                config,
                                declared: ["depends_on"].iter().map(|k| (*k).to_string()).collect(),
                                declared_env: BTreeSet::new(),
                            }],
                            reset: ResetDepth::None,
                        },
                    ),
                    &h.ctx,
                )
                .await,
            );
            assert!(reply.result.is_ok(), "the load itself draws no cycle");
        }

        let mut web = AppConfig::minimal("web", "./srv");
        web.depends_on = vec!["api".to_string()];
        let reply =
            reply_of(dispatch(envelope(3, Request::Start { apps: vec![web] }), &h.ctx).await);
        let err = reply.result.unwrap_err();
        assert_eq!(err.code, RpcErrorCode::InvalidConfig);
        assert!(
            err.message.starts_with("dependency cycle:"),
            "web is in the knot even though the reported path omits it: {}",
            err.message
        );
        assert!(
            !err.message.contains("web"),
            "the message still renders the representative path, which is what \
             an operator breaks: {}",
            err.message
        );
    }

    #[tokio::test(start_paused = true)]
    async fn a_knot_no_app_in_the_batch_is_in_does_not_refuse_the_batch() {
        // fails if the cycle check takes the first cycle in the graph: the
        // graph spans the whole registry, so a knot two earlier Flockfile
        // loads left standing elsewhere in the flock would wedge `shep
        // start` and `shep add` for every app, with an error naming apps the
        // operator never mentioned.
        let h = harness(vec![
            ProcScript::never_exits(),
            ProcScript::never_exits(),
            ProcScript::never_exits(),
        ]);
        let _started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![
                            AppConfig::minimal("api", "./srv"),
                            AppConfig::minimal("db", "./srv"),
                        ],
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        // The production door the brief names: `load_one` sends
        // `ApplyConfig` for every app the flock already has, and that arm
        // records the merged config with no cycle check of its own, so two
        // loads neither of which draws a cycle can leave one behind.
        for (name, waits_for) in [("api", "db"), ("db", "api")] {
            let mut config = AppConfig::minimal(name, "./srv");
            config.depends_on = vec![waits_for.to_string()];
            let reply = reply_of(
                dispatch(
                    envelope(
                        2,
                        Request::ApplyConfig {
                            apps: vec![DeclaredApp {
                                config,
                                declared: ["depends_on"].iter().map(|k| (*k).to_string()).collect(),
                                declared_env: BTreeSet::new(),
                            }],
                            reset: ResetDepth::None,
                        },
                    ),
                    &h.ctx,
                )
                .await,
            );
            assert!(reply.result.is_ok(), "the load itself draws no cycle");
        }
        let edges = h.ctx.registry.depends_on_by_name();
        assert_eq!(edges.get("api"), Some(&vec!["db".to_string()]));
        assert_eq!(
            edges.get("db"),
            Some(&vec!["api".to_string()]),
            "the knot is really in the registry, or this test proves nothing"
        );

        let reply = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(
            reply.result.is_ok(),
            "a batch drawing no cycle must start: {:?}",
            reply.result.unwrap_err()
        );
    }

    #[tokio::test(start_paused = true)]
    async fn an_add_whose_cycle_closes_through_the_flock_is_refused_too() {
        // fails if the cycle check rides on the spawning half: `add` and
        // `start` are one path, and a document `add` registered would refuse
        // the moment anything started it. `normalize_all` already catches a
        // cycle drawn inside one document; only the flock closes this one.
        let h = harness(vec![]);
        let first = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Add {
                        apps: vec![waiting_on("db", &["api"])],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(
            first.result.is_ok(),
            "one app, one edge to a name nobody has"
        );

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Add {
                        apps: vec![waiting_on("api", &["db"])],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let err = reply.result.unwrap_err();
        assert_eq!(err.code, RpcErrorCode::InvalidConfig);
        assert!(
            err.message.contains(" -> "),
            "the cycle must be named as a path: {}",
            err.message
        );
    }

    #[tokio::test(start_paused = true)]
    async fn a_stage_never_covers_a_sheep_the_request_did_not_carry() {
        // fails if the stages are taken from the whole graph rather than
        // filtered to the batch: `db` is already up, and a stage naming it
        // would start it a second time.
        let h = harness(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
        let first = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![waiting_on("db", &[])],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(first.result.is_ok(), "db starts on its own");

        let second = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Start {
                        apps: vec![waiting_on("api", &["db"])],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Started(infos) = second.result.unwrap() else {
            panic!("expected started")
        };
        let order: Vec<&str> = infos.iter().map(|info| info.name.as_str()).collect();
        assert_eq!(order, ["api"], "only what this request carried");

        let listed = reply_of(dispatch(envelope(3, Request::ListFlock), &h.ctx).await);
        let Response::Flock(flock) = listed.result.unwrap() else {
            panic!("expected flock")
        };
        assert_eq!(flock.len(), 2, "one row each, not a second db");
    }

    /// One app as a Flockfile would declare it: the config, plus the keys
    /// the document literally wrote. `declared` is what an apply keys on, so
    /// a fixture that left it empty would declare nothing and apply nothing.
    fn declared(name: &str, script: &str, keys: &[&str]) -> DeclaredApp {
        DeclaredApp {
            config: AppConfig::minimal(name, script),
            declared: keys.iter().map(|k| (*k).to_string()).collect(),
            declared_env: BTreeSet::new(),
        }
    }

    /// `handle_apply_config` reads the override store once for the whole
    /// file, so a second entry of the same name merges against the store as
    /// the first entry found it and the first entry's record is lost.
    /// `normalize_all` refuses a duplicate on the `Start` path and is not on
    /// this one.
    #[tokio::test(start_paused = true)]
    async fn apply_config_refuses_a_request_naming_one_app_twice() {
        let h = harness(vec![ProcScript::never_exits()]);
        let _started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::ApplyConfig {
                        apps: vec![
                            declared("web", "./one", &["script"]),
                            declared("web", "./two", &["script"]),
                        ],
                        reset: ResetDepth::None,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let err = reply.result.unwrap_err();
        assert_eq!(err.code, RpcErrorCode::InvalidConfig);
        assert!(err.message.contains("web"), "the name is named: {err:?}");

        // Refused BEFORE anything was touched, which is the half an error
        // code alone does not prove: the flock still runs what `Start`
        // registered, not either of the two scripts the request carried.
        let listed = reply_of(dispatch(envelope(3, Request::ListFlock), &h.ctx).await);
        let Response::Flock(flock) = listed.result.unwrap() else {
            panic!("expected flock")
        };
        let roll = h.ctx.registry.roll(&flock, 0);
        assert_eq!(roll.apps[0].app.script, "./srv");
    }

    /// The `Scale` arm's reasoning, applied to this one: a change that
    /// reached the stored spec and not the roll is undone by the next
    /// reboot.
    #[tokio::test(start_paused = true)]
    async fn apply_config_records_what_it_applied_in_the_registry() {
        let h = harness(vec![ProcScript::never_exits()]);
        let _started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::ApplyConfig {
                        apps: vec![DeclaredApp {
                            config: {
                                let mut app = AppConfig::minimal("web", "./srv");
                                app.max_restarts = 99;
                                app
                            },
                            declared: ["max_restarts"].iter().map(|k| (*k).to_string()).collect(),
                            declared_env: BTreeSet::new(),
                        }],
                        reset: ResetDepth::None,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Applied(report) = reply.result.unwrap() else {
            panic!("expected applied")
        };
        assert_eq!(report.len(), 1);
        assert_eq!(report[0].name, "web");
        assert_eq!(report[0].applied, vec!["max_restarts".to_string()]);
        assert_eq!(report[0].refused, None);

        let listed = reply_of(dispatch(envelope(3, Request::ListFlock), &h.ctx).await);
        let Response::Flock(flock) = listed.result.unwrap() else {
            panic!("expected flock")
        };
        let roll = h.ctx.registry.roll(&flock, 0);
        assert_eq!(roll.apps[0].app.max_restarts, 99);
    }

    /// One app that cannot be applied must not cost the rest of the file its
    /// load, so a miss is a per-app refusal inside an `Ok`, never an `Err`.
    #[tokio::test(start_paused = true)]
    async fn apply_config_refuses_an_unregistered_app_inside_the_reply() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::ApplyConfig {
                        apps: vec![declared("ghost", "./srv", &["script"])],
                        reset: ResetDepth::None,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Applied(report) = reply.result.unwrap() else {
            panic!("expected applied")
        };
        assert_eq!(report.len(), 1);
        let refused = report[0].refused.as_deref().unwrap_or_default();
        assert!(
            refused.contains("ghost"),
            "the refusal names the app: {refused}"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn a_selector_matching_nothing_is_not_found() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Stop {
                        selector: SelectorSpec::Name("ghost".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(reply.result.unwrap_err().code, RpcErrorCode::NotFound);
    }

    #[tokio::test(start_paused = true)]
    async fn a_bad_peer_regex_is_invalid_config_not_a_panic() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Describe {
                        selector: SelectorSpec::Regex("((".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(reply.result.unwrap_err().code, RpcErrorCode::InvalidConfig);
    }

    #[tokio::test(start_paused = true)]
    async fn describe_filters_by_fold() {
        let h = harness(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
        let mut api = AppConfig::minimal("api", "./a");
        api.fold = Some("backend".to_string());
        dispatch(
            envelope(
                1,
                Request::Start {
                    apps: vec![api, AppConfig::minimal("web", "./w")],
                },
            ),
            &h.ctx,
        )
        .await;
        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Describe {
                        selector: SelectorSpec::Fold("backend".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Described(hits) = reply.result.unwrap() else {
            panic!("expected described")
        };
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].name, "api");
    }

    /// Fails if `Reopen` is left to `run`'s catch-all arm, which answers
    /// `Internal` for a request this daemon implements, or if it is routed
    /// to another verb's supervisor call: `Stop` would stop the sheep.
    #[tokio::test(start_paused = true)]
    async fn reopen_routes_to_the_supervisor_and_leaves_the_sheep_running() {
        let h = harness(vec![ProcScript::never_exits()]);
        dispatch(
            envelope(
                1,
                Request::Start {
                    apps: vec![AppConfig::minimal("web", "./srv")],
                },
            ),
            &h.ctx,
        )
        .await;

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Reopen {
                        selector: SelectorSpec::Name("web".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Reopened(infos) = reply.result.unwrap() else {
            panic!("expected reopened")
        };
        assert_eq!(infos.len(), 1);
        assert_eq!(infos[0].status, ProcStatus::Online);

        let listed = reply_of(dispatch(envelope(3, Request::ListFlock), &h.ctx).await);
        let Response::Flock(flock) = listed.result.unwrap() else {
            panic!("expected flock")
        };
        assert_eq!(
            flock[0].status,
            ProcStatus::Online,
            "a reopen must not disturb the sheep it reopens"
        );
    }

    /// Fails if the arm is routed to another verb's supervisor call while
    /// keeping `Response::Reloading`, which no assertion on the reply alone
    /// can see. What separates a reload is the flock it leaves behind: two
    /// entries in one instance slot, the drainee `Stopping` under its
    /// original id and a replacement `Starting` under a new one.
    ///
    /// The mid-swap state is not a race: nothing advances the clock, and
    /// `ListFlock` is queued to an actor that runs `handle_reload` to
    /// completion before it takes another message. Three scripts, of which a
    /// correct run uses two; the third is sized for the spawn a broken arm
    /// performs, so it lands as a live entry rather than as `Errored`.
    #[tokio::test(start_paused = true)]
    async fn reload_routes_to_the_supervisor_and_starts_a_swap() {
        let h = harness(vec![ProcScript::never_exits(); 3]);
        dispatch(
            envelope(
                1,
                Request::Start {
                    apps: vec![AppConfig::minimal("web", "./srv")],
                },
            ),
            &h.ctx,
        )
        .await;

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Reload {
                        selector: SelectorSpec::Name("web".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Reloading { accepted, .. } = reply.result.unwrap() else {
            panic!("expected reloading")
        };
        assert_eq!(accepted.len(), 1);
        assert_eq!(
            accepted[0].status,
            ProcStatus::Online,
            "the answer is the flock as it stood when the reload was accepted"
        );

        let listed = reply_of(dispatch(envelope(3, Request::ListFlock), &h.ctx).await);
        let Response::Flock(flock) = listed.result.unwrap() else {
            panic!("expected flock")
        };
        assert_eq!(
            flock.len(),
            2,
            "a swap in progress is two entries in one instance slot, not one: {flock:?}"
        );
        assert_eq!(flock[0].id, accepted[0].id);
        assert_eq!(flock[0].status, ProcStatus::Stopping);
        assert_ne!(
            flock[1].id, accepted[0].id,
            "the replacement takes a new id"
        );
        assert_eq!(flock[1].status, ProcStatus::Starting);
    }

    /// `AppConfig`'s own default `kill_timeout`, which every app built by
    /// `AppConfig::minimal` here carries.
    const DEFAULT_KILL_TIMEOUT: Duration = Duration::from_millis(1600);

    /// A `db` and an `api` that waits for it, started through the ordinary
    /// `Start` arm so the registry holds the edge an ordered walk reads.
    ///
    /// `api` is started first, and alone, deliberately: it takes the lower
    /// id, and it sorts first by name as well, so neither the id order a
    /// batch verb resolves in nor the name order a reload queues in matches
    /// the dependency order. Started together, the staged `Start` would give
    /// `db` the lower id and an unordered restart would pass by accident.
    async fn start_api_before_the_db_it_waits_for(h: &Harness) {
        let mut api = AppConfig::minimal("api", "./api");
        api.depends_on = vec!["db".to_string()];
        for (id, app) in [(1, api), (2, AppConfig::minimal("db", "./db"))] {
            let started =
                reply_of(dispatch(envelope(id, Request::Start { apps: vec![app] }), &h.ctx).await);
            let Response::Started(infos) = started.result.unwrap() else {
                panic!("expected started")
            };
            assert_eq!(infos.len(), 1, "each app comes up before the next");
        }
    }

    /// The names `kind` was published for, in the order the bus carried them.
    fn names_for(
        rx: &mut tokio::sync::broadcast::Receiver<crate::bus::SharedEvent>,
        kind: ProcessEventKind,
    ) -> Vec<String> {
        let mut names = Vec::new();
        while let Ok(event) = rx.try_recv() {
            let shep_core::protocol::BusEvent::Process {
                event: seen, info, ..
            } = &*event
            else {
                continue;
            };
            if *seen == kind {
                names.push(info.name.clone());
            }
        }
        names
    }

    /// fails if a fold restarts as one batch, which restarts `api` against a
    /// database that has not come back. Four scripts: two for the pair's
    /// first spawn and two for their respawns.
    #[tokio::test(start_paused = true)]
    async fn a_restart_matching_several_walks_the_stages_forward() {
        let h = harness(vec![ProcScript::never_exits(); 4]);
        start_api_before_the_db_it_waits_for(&h).await;

        let mut rx = h.ctx.events.subscribe();
        let reply = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Restart {
                        selector: SelectorSpec::All,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Restarted {
            accepted: infos, ..
        } = reply.result.unwrap()
        else {
            panic!("expected restarted")
        };
        assert_eq!(infos.len(), 2, "both sheep restart: {infos:?}");

        assert_eq!(names_for(&mut rx, ProcessEventKind::Restart), ["db", "api"]);
    }

    /// fails if a fold reloads as one batch, which swaps `api` while the
    /// database it waits for is still swapping. Four scripts: two for the
    /// pair's first spawn and two for the replacements each swap spawns.
    #[tokio::test(start_paused = true)]
    async fn a_reload_matching_several_walks_the_stages_forward() {
        let h = harness(vec![ProcScript::never_exits(); 4]);
        start_api_before_the_db_it_waits_for(&h).await;

        let mut rx = h.ctx.events.subscribe();
        let reply = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Reload {
                        selector: SelectorSpec::All,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Reloading { accepted, .. } = reply.result.unwrap() else {
            panic!("expected reloading")
        };
        assert_eq!(accepted.len(), 2, "both sheep are accepted: {accepted:?}");

        assert_eq!(names_for(&mut rx, ProcessEventKind::Reload), ["db", "api"]);
    }

    /// fails if the ordered walk answers a selector it matched nothing for
    /// with an empty table. A restart that moved nothing is `NotFound`, which
    /// is what makes `shep restart typo` exit non-zero.
    #[tokio::test(start_paused = true)]
    async fn a_restart_naming_nothing_the_flock_holds_is_still_not_found() {
        let h = harness(vec![ProcScript::never_exits(); 2]);
        start_api_before_the_db_it_waits_for(&h).await;

        let reply = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Restart {
                        selector: SelectorSpec::Name("typo".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(reply.result.unwrap_err().code, RpcErrorCode::NotFound);
    }

    /// fails if a stage awaits its members in turn, which costs a restart the
    /// SUM of their kill ladders where an unordered one costs the longest.
    /// Two sheep with no edge between them are one stage, and both ignore
    /// SIGTERM, so each burns its whole 1600ms ladder. Virtual time under
    /// `start_paused`, which advances only when every task is idle, so the
    /// two shapes are exact rather than close.
    #[tokio::test(start_paused = true)]
    async fn one_stage_restarts_its_members_at_the_same_time() {
        let h = harness(vec![
            ProcScript::ignores_signals(),
            ProcScript::ignores_signals(),
            ProcScript::never_exits(),
            ProcScript::never_exits(),
        ]);
        let started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![
                            AppConfig::minimal("alpha", "./a"),
                            AppConfig::minimal("zulu", "./z"),
                        ],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(started.result.is_ok(), "both apps come up");

        let began = Instant::now();
        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Restart {
                        selector: SelectorSpec::All,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let spent = began.elapsed();

        let Response::Restarted {
            accepted: infos, ..
        } = reply.result.unwrap()
        else {
            panic!("expected restarted")
        };
        assert_eq!(infos.len(), 2, "both sheep restart: {infos:?}");
        assert!(
            spent < DEFAULT_KILL_TIMEOUT * 2,
            "one stage's ladders must overlap; spent {spent:?} on two of them"
        );
    }

    /// fails if a matched name with no node in the plan is dropped from the
    /// walk, which would answer `Ok` for a sheep nothing restarted. The
    /// registry is what the stages are built from, and it holds no dog and
    /// nothing a `shep dev` teardown cleared.
    #[tokio::test(start_paused = true)]
    async fn a_matched_sheep_the_registry_does_not_hold_still_restarts() {
        let h = harness(vec![ProcScript::never_exits(); 4]);
        start_api_before_the_db_it_waits_for(&h).await;
        h.ctx.registry.clear();

        let mut rx = h.ctx.events.subscribe();
        let reply = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Restart {
                        selector: SelectorSpec::All,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Restarted {
            accepted: infos, ..
        } = reply.result.unwrap()
        else {
            panic!("expected restarted")
        };
        assert_eq!(infos.len(), 2, "both sheep restart: {infos:?}");
        assert_eq!(
            names_for(&mut rx, ProcessEventKind::Restart)
                .into_iter()
                .collect::<BTreeSet<_>>(),
            ["api".to_string(), "db".to_string()]
                .into_iter()
                .collect::<BTreeSet<_>>(),
        );
    }

    /// Every process event the receiver holds, as `(kind, name)` in the
    /// order the bus carried them.
    fn events_in_order(
        rx: &mut tokio::sync::broadcast::Receiver<crate::bus::SharedEvent>,
    ) -> Vec<(ProcessEventKind, String)> {
        let mut seen = Vec::new();
        while let Ok(event) = rx.try_recv() {
            if let shep_core::protocol::BusEvent::Process {
                event: kind, info, ..
            } = &*event
            {
                seen.push((*kind, info.name.clone()));
            }
        }
        seen
    }

    /// fails if a stage drops its wait for an app whose reload was refused:
    /// `ReloadInFlight` is per app now, so a busy dependency contributes
    /// nothing to `waiting`, the stage returns at once, and the dependant
    /// swaps against a dependency that is mid-swap. Four scripts: two for the
    /// pair's first spawn and two for the replacements.
    #[tokio::test(start_paused = true)]
    async fn a_stage_still_waits_for_an_app_whose_reload_was_refused() {
        let h = harness(vec![ProcScript::never_exits(); 4]);
        start_api_before_the_db_it_waits_for(&h).await;

        // Accepted and still swapping: a single-target reload answers before
        // the replacement is up, which is what leaves `db` in flight.
        let first = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Reload {
                        selector: SelectorSpec::Name("db".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(first.result.is_ok(), "the first reload is accepted");

        let mut rx = h.ctx.events.subscribe();
        let reply = reply_of(
            dispatch(
                envelope(
                    4,
                    Request::Reload {
                        selector: SelectorSpec::All,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Reloading { accepted, .. } = reply.result.unwrap() else {
            panic!("expected reloading")
        };
        let names: Vec<&str> = accepted.iter().map(|info| info.name.as_str()).collect();
        assert_eq!(names, ["api"], "db is already reloading and is refused");

        let seen = events_in_order(&mut rx);
        let swapped = seen
            .iter()
            .position(|(kind, name)| *kind == ProcessEventKind::Reloaded && name == "db")
            .unwrap_or_else(|| panic!("db never finished its swap: {seen:?}"));
        let dependant = seen
            .iter()
            .position(|(kind, name)| *kind == ProcessEventKind::Reload && name == "api")
            .unwrap_or_else(|| panic!("api never reloaded: {seen:?}"));
        assert!(
            swapped < dependant,
            "api must wait out the refused stage's swap: {seen:?}"
        );
    }

    /// fails if a staged restart drops the name of an app it went around: the
    /// walk asks per app, so one the flock no longer holds is refused on its
    /// own and the reply would otherwise be a success with that app's row
    /// silently missing.
    ///
    /// The refusal is planted the way the real one arrives. `walk_for` reads
    /// a listing and `restart_in_stages` calls the supervisor per member
    /// afterwards, so a sheep that leaves the flock in between is named by
    /// the walk and no longer matched by the supervisor; deleting `db`
    /// between the two calls is that interleaving, held still. Going through
    /// `Request::Restart` instead would take both halves inside one handler
    /// with nothing able to run between them. Three scripts: two for the
    /// pair's first spawn and one for `api`'s respawn.
    #[tokio::test(start_paused = true)]
    async fn a_staged_restart_names_the_app_it_could_not_restart() {
        let h = harness(vec![ProcScript::never_exits(); 3]);
        start_api_before_the_db_it_waits_for(&h).await;

        let walk = walk_for(&h.ctx, &ProcessSelector::All)
            .await
            .expect("two sheep are an ordered walk");
        let deleted = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Delete {
                        selector: SelectorSpec::Name("db".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(deleted.result.is_ok(), "db leaves the flock");

        let (accepted, refused) = restart_in_stages(&h.ctx, &walk)
            .await
            .expect("api still restarted, so the walk answers Ok");
        let names: Vec<&str> = accepted.iter().map(|info| info.name.as_str()).collect();
        assert_eq!(names, ["api"], "the rest of the fold still restarts");
        let refused_names: Vec<&str> = refused.iter().map(|row| row.name.as_str()).collect();
        assert_eq!(refused_names, ["db"], "and the one that did not is named");
        assert!(
            refused[0].reason.contains("no registered sheep"),
            "with the shepherd's own reason: {:?}",
            refused[0].reason
        );
    }

    /// fails if a staged restart keeps only the first refusal, which is what
    /// the walk did with the error before `refused` existed: two apps gone
    /// from the flock have to produce two names, not one.
    ///
    /// The same interleaving as the sibling above, over three apps with two
    /// of them deleted, so `api` still restarts and the answer is the
    /// partial one a client has to render. Four scripts: three for the first
    /// spawns and one for `api`'s respawn.
    #[tokio::test(start_paused = true)]
    async fn a_staged_restart_collects_every_refusal_and_not_just_the_first() {
        let h = harness(vec![ProcScript::never_exits(); 4]);
        start_api_before_the_db_it_waits_for(&h).await;
        let started = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Start {
                        apps: vec![AppConfig::minimal("cache", "./cache")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(started.result.is_ok(), "cache comes up beside the pair");

        let walk = walk_for(&h.ctx, &ProcessSelector::All)
            .await
            .expect("three sheep are an ordered walk");
        for (id, name) in [(4, "db"), (5, "cache")] {
            let deleted = reply_of(
                dispatch(
                    envelope(
                        id,
                        Request::Delete {
                            selector: SelectorSpec::Name(name.to_string()),
                        },
                    ),
                    &h.ctx,
                )
                .await,
            );
            assert!(deleted.result.is_ok(), "{name} leaves the flock");
        }

        let (accepted, refused) = restart_in_stages(&h.ctx, &walk)
            .await
            .expect("api still restarted, so the walk answers Ok");
        let names: Vec<&str> = accepted.iter().map(|info| info.name.as_str()).collect();
        assert_eq!(names, ["api"], "the one app still in the flock restarts");
        // A set, not a list: which stage each deleted app fell in is
        // `plan_for_names`' business and not what this pins.
        let refused_names: BTreeSet<&str> = refused.iter().map(|row| row.name.as_str()).collect();
        assert_eq!(
            refused_names,
            ["cache", "db"].into_iter().collect::<BTreeSet<&str>>(),
            "both are named, not just whichever was refused first"
        );
    }

    /// fails if a staged reload drops the name of an app it went around: the
    /// walk asks per app, so a busy one is refused on its own and the reply
    /// would otherwise be a success with that app's row silently missing.
    /// Four scripts, as the sibling above.
    #[tokio::test(start_paused = true)]
    async fn a_staged_reload_names_the_app_it_could_not_reload() {
        let h = harness(vec![ProcScript::never_exits(); 4]);
        start_api_before_the_db_it_waits_for(&h).await;

        let first = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Reload {
                        selector: SelectorSpec::Name("db".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(first.result.is_ok(), "the first reload is accepted");

        let reply = reply_of(
            dispatch(
                envelope(
                    4,
                    Request::Reload {
                        selector: SelectorSpec::All,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Reloading { accepted, refused } = reply.result.unwrap() else {
            panic!("expected reloading")
        };
        let names: Vec<&str> = accepted.iter().map(|info| info.name.as_str()).collect();
        assert_eq!(names, ["api"], "the rest of the fold still reloads");
        let refused_names: Vec<&str> = refused.iter().map(|row| row.name.as_str()).collect();
        assert_eq!(refused_names, ["db"], "and the one that did not is named");
        assert!(
            refused[0].reason.contains("already being reloaded"),
            "with the shepherd's own reason: {:?}",
            refused[0].reason
        );
    }

    /// fails if a reload stage's bound ignores how many instances are still
    /// to swap: `advance_reload` replaces one at a time, so a three-instance
    /// app costs three drains and three readiness waits, and a per-app bound
    /// abandons the stage a third of the way through with the dependant
    /// reloading against a half-swapped dependency.
    #[tokio::test(start_paused = true)]
    async fn a_reload_stage_is_bounded_by_the_swaps_it_is_waiting_for() {
        let h = harness(vec![ProcScript::never_exits()]);
        let mut web = AppConfig::minimal("web", "./web");
        web.listen_timeout = UpDuration::from_millis(4_000);
        web.graceful_timeout = UpDuration::from_millis(6_000);
        let started =
            reply_of(dispatch(envelope(1, Request::Start { apps: vec![web] }), &h.ctx).await);
        assert!(started.result.is_ok(), "web comes up: {started:?}");

        let one = [("web".to_string(), 1)].into_iter().collect();
        let three = [("web".to_string(), 3)].into_iter().collect();
        assert_eq!(
            reload_stage_bound(&h.ctx, &one),
            Duration::from_secs(15),
            "one swap is a drain plus a readiness wait, plus the stage slack"
        );
        assert_eq!(
            reload_stage_bound(&h.ctx, &three),
            Duration::from_secs(35),
            "three swaps are three of each, and the slack is spent once"
        );
    }

    /// fails if the walk's wait follows one hop only: with `web -> mid ->
    /// db` registered and a selector matching the two ends alone, `mid` is
    /// not matched, so a one-hop intersection answers that nothing is
    /// depended on and `web` restarts against a `db` no stage ever waited
    /// for. That is the failure the ordered walk exists to prevent.
    #[tokio::test(start_paused = true)]
    async fn a_walk_waits_for_a_dependency_it_reaches_through_an_unmatched_hop() {
        let h = harness(vec![ProcScript::never_exits(); 3]);
        let mut web = AppConfig::minimal("web", "./web");
        web.depends_on = vec!["mid".to_string()];
        let mut mid = AppConfig::minimal("mid", "./mid");
        mid.depends_on = vec!["db".to_string()];
        // One request each, the way `start_api_before_the_db_it_waits_for`
        // does: a three-stage batch outlasts the default request budget.
        for (id, app) in [(1, AppConfig::minimal("db", "./db")), (2, mid), (3, web)] {
            let started =
                reply_of(dispatch(envelope(id, Request::Start { apps: vec![app] }), &h.ctx).await);
            assert!(started.result.is_ok(), "the chain comes up: {started:?}");
        }

        let listed = reply_of(dispatch(envelope(4, Request::ListFlock), &h.ctx).await);
        let Response::Flock(flock) = listed.result.unwrap() else {
            panic!("expected flock")
        };
        let selector = selector_of(SelectorSpec::Regex("^(web|db)$".to_string())).unwrap();
        let walk = ordered_walk(&h.ctx, &selector, &flock).expect("two names matched");

        assert_eq!(
            walk.stages,
            vec![vec!["db".to_string()], vec!["web".to_string()]],
            "the unmatched hop is not restarted, and the ends keep their order"
        );
        assert_eq!(
            walk.depended_on,
            ["db".to_string()].into_iter().collect::<BTreeSet<_>>(),
            "web reaches db through mid, so db's stage has to be held"
        );
    }

    /// The daemon's code becomes the CLI's exit status and its message is
    /// all that is printed. Fails if either refusal answers a code that is
    /// not `Internal`, since neither has one of its own and
    /// `SupervisorError`'s `Display` is what tells them apart. Fails too if
    /// `ReloadInFlight`'s arm drops the app's name, which says which reload
    /// to wait for.
    #[test]
    fn a_refused_reload_is_internal_and_says_which_refusal_it_was() {
        let in_flight = rpc_error(&SupervisorError::ReloadInFlight("web".to_string()));
        assert_eq!(in_flight.code, RpcErrorCode::Internal);
        assert_eq!(in_flight.message, "web is already being reloaded");

        let shutting_down = rpc_error(&SupervisorError::EngineStopped);
        assert_eq!(shutting_down.code, RpcErrorCode::Internal);
        assert_eq!(shutting_down.message, "the supervisor engine has stopped");
    }

    /// Fails if `Reload` skips the selector conversion, or converts it
    /// without reporting the failure: a peer regex the daemon cannot compile
    /// is the client's usage error. `reload_request` converts before it can
    /// ask what the selector matched, and an arm that answered `Reloading`
    /// off an unconverted selector could still lose it.
    #[tokio::test(start_paused = true)]
    async fn a_bad_reload_selector_is_invalid_config() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Reload {
                        selector: SelectorSpec::Regex("((".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(reply.result.unwrap_err().code, RpcErrorCode::InvalidConfig);
    }

    /// Fails if `Reopen` skips the selector conversion, or converts it
    /// without reporting the failure: a peer regex the daemon cannot compile
    /// is the client's usage error, not an internal one.
    #[tokio::test(start_paused = true)]
    async fn a_bad_reopen_selector_is_invalid_config() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Reopen {
                        selector: SelectorSpec::Regex("((".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(reply.result.unwrap_err().code, RpcErrorCode::InvalidConfig);
    }

    /// `TimedOut` rather than `NoChannel` is the assertion that matters: the
    /// action was really delivered and waited on, and `web`'s 3s
    /// `action_timeout` elapsed inside the request's own budget. Raise it
    /// past [`DEFAULT_DEADLINE_MS`] and the reply becomes `DeadlineExceeded`
    /// instead, which names no sheep; that ordering is pinned right below in
    /// `an_oversized_action_timeout_loses_the_race`.
    ///
    /// Nothing answers, because the harness keeps no handle on its runner.
    #[tokio::test(start_paused = true)]
    async fn trigger_routes_to_the_flock_and_reports_each_match_within_the_budget() {
        // Two apps, not one: ids start at 0, so a single-app harness would
        // give `web` id 0, indistinguishable from a row-mapping bug that
        // leaves the field's default. `other` first pushes `web` to id 1.
        let h = harness(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
        let mut web = AppConfig::minimal("web", "./srv");
        web.channel = true;
        let started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("other", "./o"), web],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Started(started) = started.result.unwrap() else {
            panic!("expected started")
        };
        let web_id = started
            .iter()
            .find(|i| i.name == "web")
            .expect("web registered")
            .id;
        assert_ne!(web_id, 0, "the test's own premise: web must not be id 0");

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Trigger {
                        selector: SelectorSpec::Name("web".to_string()),
                        action: "gc".to_string(),
                        params: None,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Response::Triggered(rows) = reply.result.unwrap() else {
            panic!("expected triggered")
        };
        assert_eq!(
            rows,
            vec![ActionReply {
                id: web_id,
                name: "web".to_string(),
                outcome: ActionOutcome::TimedOut,
            }]
        );
    }

    /// A bad signal name must be refused at the dispatch boundary with
    /// `InvalidConfig`: an operator who typed `SIGHUPP` needs the accepted
    /// list, and only this arm has it.
    #[tokio::test]
    async fn a_signal_name_outside_the_grammar_is_refused_with_the_accepted_list() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Signal {
                        selector: SelectorSpec::All,
                        signal: "SIGHUPP".to_string(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let err = reply.result.unwrap_err();
        assert_eq!(err.code, RpcErrorCode::InvalidConfig);
        assert!(err.message.contains("SIGHUPP"), "{}", err.message);
        assert!(err.message.contains("SIGHUP"), "{}", err.message);
        assert!(err.message.contains("SIGUSR2"), "{}", err.message);
    }

    /// Refused at the dispatch boundary, so it never reaches `send_line`.
    /// There is no sheep in this fixture to answer it, so a `NotFound` here
    /// would mean the refusal was skipped rather than that it fired.
    #[tokio::test]
    async fn a_line_carrying_a_newline_is_refused_before_it_reaches_the_supervisor() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::SendLine {
                        selector: SelectorSpec::All,
                        line: "reload\nrm -rf /".to_string(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let err = reply.result.unwrap_err();
        assert_eq!(err.code, RpcErrorCode::InvalidConfig);
        assert!(err.message.contains("newline"), "{}", err.message);
    }

    /// `web`'s `action_timeout` is set past the 5s default budget and the
    /// request carries no deadline of its own, so under the paused clock
    /// `dispatch`'s own `with_deadline` wins and the reply is
    /// `DeadlineExceeded` rather than a `Triggered` row.
    /// `shep_core::config::normalize` refuses only a timeout no caller could
    /// ever satisfy, so anything under that has to lose this race.
    #[tokio::test(start_paused = true)]
    async fn an_oversized_action_timeout_loses_the_race() {
        let h = harness(vec![ProcScript::never_exits()]);
        let mut web = AppConfig::minimal("web", "./srv");
        web.channel = true;
        web.action_timeout = UpDuration::from_millis(9_000); // > DEFAULT_DEADLINE_MS (5s)
        reply_of(dispatch(envelope(1, Request::Start { apps: vec![web] }), &h.ctx).await);

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Trigger {
                        selector: SelectorSpec::Name("web".to_string()),
                        action: "gc".to_string(),
                        params: None,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(
            reply.result.unwrap_err().code,
            RpcErrorCode::DeadlineExceeded,
            "an action_timeout past the caller's default budget must lose that race, not \
             report an honest TimedOut row nobody can reach"
        );
    }

    /// Fails if `Trigger` skips the selector conversion, or converts it
    /// without reporting the failure: a peer regex the daemon cannot compile
    /// is the client's usage error, not an internal one.
    #[tokio::test(start_paused = true)]
    async fn a_bad_trigger_selector_is_invalid_config() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Trigger {
                        selector: SelectorSpec::Regex("((".to_string()),
                        action: "gc".to_string(),
                        params: None,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(reply.result.unwrap_err().code, RpcErrorCode::InvalidConfig);
    }

    /// A selector matching no registered sheep is a whole-request
    /// `NotFound`, kept separate from a per-row `NoChannel`, which only
    /// appears inside a non-empty match.
    #[tokio::test(start_paused = true)]
    async fn a_trigger_matching_nothing_is_not_found() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Trigger {
                        selector: SelectorSpec::Name("ghost".to_string()),
                        action: "gc".to_string(),
                        params: None,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(reply.result.unwrap_err().code, RpcErrorCode::NotFound);
    }

    /// Fails if the `ReopenFailed | FlushFailed` arm answers any other code:
    /// `SpawnFailed` exits 7 and reads as "could not start it". Fails too if
    /// it sends the bare payload instead of `err.to_string()`, since once the
    /// two share a wire code `Display` is all that tells a reader which half
    /// of the log plane failed.
    #[test]
    fn a_log_plane_failure_is_internal_and_says_which_half_failed() {
        let reopen = rpc_error(&SupervisorError::ReopenFailed(
            "web (id 0): could not reopen /logs/web-out.log: Permission denied".to_string(),
        ));
        assert_eq!(reopen.code, RpcErrorCode::Internal);
        assert_eq!(
            reopen.message,
            "log reopen failed: web (id 0): could not reopen \
             /logs/web-out.log: Permission denied"
        );

        let flush = rpc_error(&SupervisorError::FlushFailed(
            "/logs/web-out.log: Permission denied".to_string(),
        ));
        assert_eq!(flush.code, RpcErrorCode::Internal);
        assert_eq!(
            flush.message,
            "log flush failed: /logs/web-out.log: Permission denied"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn subscribe_hands_back_a_compiled_filter() {
        let h = harness(vec![]);
        let outcome = dispatch(
            envelope(
                1,
                Request::Subscribe {
                    topics: vec!["process.*".to_string()],
                },
            ),
            &h.ctx,
        )
        .await;
        let Outcome::Subscribe { reply, filter } = outcome else {
            panic!("expected subscribe")
        };
        assert_eq!(reply.result.unwrap(), Response::Subscribed);
        assert_eq!(filter.patterns(), ["process.*"]);

        let bad = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Subscribe {
                        topics: vec!["[".to_string()],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(bad.result.unwrap_err().code, RpcErrorCode::InvalidConfig);
    }

    #[tokio::test(start_paused = true)]
    async fn kill_daemon_asks_for_shutdown_without_taking_the_engine_down_itself() {
        let mut h = harness(vec![]);
        let Outcome::Shutdown(reply) = dispatch(envelope(1, Request::KillDaemon), &h.ctx).await
        else {
            panic!("expected a shutdown outcome")
        };
        assert_eq!(reply.result.unwrap(), Response::ShuttingDown);
        // Dispatch only reports the intent; the connection layer triggers it.
        assert!(!*h.shutdown_rx.borrow_and_update());
        h.ctx.shutdown();
        assert!(h.shutdown_rx.changed().await.is_ok());
        assert!(*h.shutdown_rx.borrow());
    }

    #[test]
    fn budgets_default_and_clamp() {
        assert_eq!(budget(None), Duration::from_millis(DEFAULT_DEADLINE_MS));
        assert_eq!(budget(Some(250)), Duration::from_millis(250));
        assert_eq!(budget(Some(0)), Duration::from_millis(1));
        assert_eq!(
            budget(Some(u64::MAX)),
            Duration::from_millis(MAX_DEADLINE_MS)
        );
    }

    #[tokio::test(start_paused = true)]
    async fn envelope_deadline_ms_actually_bounds_the_reply() {
        // Drives a real envelope's `deadline_ms` through `dispatch` into
        // `budget`. `Stop` on an `ignores_signals()` sheep waits the full
        // 1600ms `kill_timeout` ladder, far past this 1ms deadline, while a
        // build passing `budget(None)` would take the 5s default and pass.
        let h = harness(vec![ProcScript::ignores_signals()]);
        dispatch(
            envelope(
                1,
                Request::Start {
                    apps: vec![AppConfig::minimal("web", "./srv")],
                },
            ),
            &h.ctx,
        )
        .await;

        let reply = reply_of(
            dispatch(
                Envelope {
                    id: 2,
                    deadline_ms: Some(1),
                    body: Request::Stop {
                        selector: SelectorSpec::Name("web".to_string()),
                    },
                },
                &h.ctx,
            )
            .await,
        );
        let err = reply.result.unwrap_err();
        assert_eq!(
            err.code,
            RpcErrorCode::DeadlineExceeded,
            "a 1ms client deadline against a 1600ms kill ladder must expire, not {err:?}"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn work_past_its_deadline_answers_deadline_exceeded() {
        // Driven at the deadline seam with a future that never finishes: the
        // paused clock auto-advances the moment the test parks, so this is
        // instant and exact.
        let outcome = with_deadline(
            5,
            Duration::from_millis(250),
            std::future::pending::<Outcome>(),
        )
        .await;
        let reply = reply_of(outcome);
        assert_eq!(reply.id, 5);
        let err = reply.result.unwrap_err();
        assert_eq!(err.code, RpcErrorCode::DeadlineExceeded);
        assert!(err.message.contains("250 ms"), "{}", err.message);
    }

    /// The assertion reads the file the reply named and compares its app
    /// count against the number the reply claimed, so a handler answering
    /// `apps: 0` for a two-app flock reddens here.
    #[tokio::test]
    async fn save_roll_writes_the_file_it_names_and_counts_what_it_recorded() {
        let h = harness(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
        reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![
                            AppConfig::minimal("web", "./srv"),
                            AppConfig::minimal("worker", "./work"),
                        ],
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        let reply = reply_of(dispatch(envelope(2, Request::SaveRoll), &h.ctx).await);
        let Ok(Response::RollSaved { path, apps }) = reply.result else {
            panic!("expected RollSaved, got {:?}", reply.result)
        };
        assert_eq!(apps, 2);

        let roll = crate::snapshot::read(std::path::Path::new(&path)).unwrap();
        assert_eq!(roll.apps.len(), 2, "the reply's count must match the file");
        assert_eq!(path, h.ctx.snapshot_path.display().to_string());
    }

    /// fails if the muster roll keeps the pre-scale count. This is the test for the
    /// bug that is invisible until a reboot: the roll is what `shep muster` reads,
    /// so a scale missing from it is a scale that silently reverts.
    #[tokio::test]
    async fn a_scale_is_recorded_in_the_roll_the_next_muster_reads() {
        let h = harness(vec![ProcScript::never_exits(); 4]);
        let mut app = AppConfig::minimal("web", "./srv");
        app.instances = 2;
        reply_of(dispatch(envelope(1, Request::Start { apps: vec![app] }), &h.ctx).await);

        reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Scale {
                        name: "web".to_string(),
                        count: 4,
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        let reply = reply_of(dispatch(envelope(3, Request::SaveRoll), &h.ctx).await);
        let Ok(Response::RollSaved { path, .. }) = reply.result else {
            panic!("expected RollSaved, got {:?}", reply.result)
        };
        let roll = crate::snapshot::read(std::path::Path::new(&path)).unwrap();
        assert_eq!(roll.apps[0].app.instances, 4);
    }

    /// `web` at two instances, scaled to four, with one script left so the
    /// first new spawn succeeds and the second fails. Three instances are
    /// then running: a roll saying `2` stops one at the next muster, a roll
    /// saying `4` brings up a count that never ran. Only `3` is the truth,
    /// and it gets there only if the handler records off the `Err` path too.
    ///
    /// The reply is asserted as well as the roll: recording what the daemon
    /// did must not turn "three of four" into a success.
    #[tokio::test]
    async fn a_partial_scale_is_recorded_in_the_roll_and_still_reported_short() {
        let h = harness(vec![ProcScript::never_exits(); 3]);
        let mut app = AppConfig::minimal("web", "./srv");
        app.instances = 2;
        reply_of(dispatch(envelope(1, Request::Start { apps: vec![app] }), &h.ctx).await);

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Scale {
                        name: "web".to_string(),
                        count: 4,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let err = reply.result.unwrap_err();
        assert_eq!(err.code, RpcErrorCode::SpawnFailed);
        assert!(
            err.message.contains("3 of 4"),
            "the operator has to be told both numbers: {}",
            err.message
        );

        let saved = reply_of(dispatch(envelope(3, Request::SaveRoll), &h.ctx).await);
        let Ok(Response::RollSaved { path, .. }) = saved.result else {
            panic!("expected RollSaved, got {:?}", saved.result)
        };
        let roll = crate::snapshot::read(std::path::Path::new(&path)).unwrap();
        assert_eq!(
            roll.apps[0].app.instances, 3,
            "the roll must hold the three instances really running — not the \
             pre-scale two, and not the four that were asked for"
        );
    }

    /// Fails if the handler forwards `snapshot_now`'s engine-stopped `Ok(())`
    /// as a success. A save that wrote nothing and said "saved" is the
    /// failure mode an operator reboots into.
    #[tokio::test]
    async fn save_roll_against_a_stopped_engine_is_an_error_not_a_silent_success() {
        let h = harness(vec![]);
        h.ctx.supervisor.shutdown().await;

        let reply = reply_of(dispatch(envelope(1, Request::SaveRoll), &h.ctx).await);
        let err = reply.result.unwrap_err();
        assert_eq!(err.code, RpcErrorCode::Internal);
        assert!(
            err.message.contains("engine"),
            "the operator must be told why nothing was written: {}",
            err.message
        );
    }

    /// Assembling a flock that is already assembled starts nothing, so a
    /// reply naming only what this call spawned cannot be told from "the roll
    /// was empty".
    ///
    /// One script: `web`'s first start consumes it, so a muster that started
    /// the roll's apps unconditionally would exhaust the pool and land a
    /// second, `Errored` `web` in the listing. The count and the name
    /// assertion are what catch it.
    #[tokio::test]
    async fn a_second_muster_still_reports_the_flock_the_roll_restored() {
        let h = harness(vec![ProcScript::never_exits()]);
        reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        reply_of(dispatch(envelope(2, Request::SaveRoll), &h.ctx).await);

        let reply = reply_of(dispatch(envelope(3, Request::Muster), &h.ctx).await);
        let Ok(Response::Mustered(infos)) = reply.result else {
            panic!("expected Mustered, got {:?}", reply.result)
        };
        assert_eq!(
            infos.len(),
            1,
            "the sheep the roll restores, not the ones this call spawned"
        );
        assert_eq!(infos[0].name, "web");
        assert_eq!(infos[0].status, ProcStatus::Online);
    }

    /// Starts `web` through `h` and returns nothing: no case below asserts
    /// on the start reply itself.
    async fn start_web(h: &Harness) {
        reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
    }

    /// The flock a `ListFlock` on `ctx` answers with.
    ///
    /// # Panics
    ///
    /// If the reply is anything but `Flock`, which is a fixture bug.
    async fn list_flock(ctx: &RpcContext, id: u64) -> Vec<ProcessInfo> {
        let reply = reply_of(dispatch(envelope(id, Request::ListFlock), ctx).await);
        let Ok(Response::Flock(infos)) = reply.result else {
            panic!("expected Flock, got {:?}", reply.result)
        };
        infos
    }

    /// Without a live sample the fields come back `None` for a running sheep,
    /// which a reader renders as `-` and an operator reads as "shep cannot
    /// see it".
    #[tokio::test]
    async fn list_flock_carries_a_live_memory_reading_for_a_running_sheep() {
        // The harness's sampler is scripted, so the number below is the
        // fixture's and not the machine's; this asserts the plumbing, not
        // sysinfo. `ScriptedRunner` hands out `FIRST_SCRIPTED_PID`, and the
        // scripted reading describes a tree rooted at that same pid.
        let h = harness_with_stats(vec![ProcScript::never_exits()]);
        start_web(&h).await;

        let infos = list_flock(&h.ctx, 2).await;
        assert_eq!(infos[0].pid, Some(FIRST_SCRIPTED_PID));
        assert_eq!(infos[0].memory_bytes, Some(SCRIPTED_TREE_BYTES));
        assert_eq!(
            infos[0].cpu_percent, None,
            "no periodic baseline has been recorded, and a number invented \
             from the read's own window is worse than an empty cell"
        );
    }

    /// A baseline exists here, so a real number has to come back, and the
    /// second listing says which window produced it: 1500 CPU-ms over the
    /// 15 s since the baseline is 10%, while the same counter over the
    /// millisecond since the previous listing is hundreds of percent.
    #[tokio::test]
    async fn list_flock_measures_cpu_from_the_periodic_baseline_not_from_the_previous_listing() {
        let h = harness_with_stats(vec![ProcScript::never_exits()]);
        start_web(&h).await;
        // A baseline dated one poll interval back, which is what the tick
        // would have left behind had one fired: the clock here is real, so a
        // test that waited for the enforcer's own tick would wait 15 s.
        let last_tick = Instant::now()
            .checked_sub(MEMORY_POLL_INTERVAL)
            .expect("the monotonic clock is older than one poll interval");
        h.stats.record_baseline_now(last_tick);

        let first = list_flock(&h.ctx, 2).await[0]
            .cpu_percent
            .expect("a baseline exists, so a running sheep has a CPU figure");
        let second = list_flock(&h.ctx, 3).await[0]
            .cpu_percent
            .expect("a baseline exists, so a running sheep has a CPU figure");

        assert!(
            (5.0..=10.05).contains(&first),
            "1500 CPU-ms over the 15 s since the baseline is 10%; got {first}"
        );
        assert!(
            (second - first).abs() < 1.0,
            "the second listing divided by the gap between the two LISTINGS \
             rather than by the window since the tick: {first} then {second}"
        );
    }

    /// `Describe` is the second of the two verbs an operator reads resource
    /// usage from, and an implementation wired into `ListFlock` alone passes
    /// every other case here.
    #[tokio::test]
    async fn describe_carries_a_live_reading_too() {
        let h = harness_with_stats(vec![ProcScript::never_exits()]);
        start_web(&h).await;

        let described = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Describe {
                        selector: SelectorSpec::Name("web".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Ok(Response::Described(infos)) = described.result else {
            panic!("expected Described, got {:?}", described.result)
        };
        assert_eq!(infos[0].memory_bytes, Some(SCRIPTED_TREE_BYTES));
    }

    /// The join is keyed on the pid a reading was taken against; one falling
    /// back to the id, or to the first reading in the sample, would print one
    /// sheep's resource use against another.
    ///
    /// Two sheep, and both are needed: stopping a sheep unwatches it, so a
    /// listing holding only the stopped one leaves the sample empty and every
    /// join misses.
    #[tokio::test]
    async fn a_sheep_with_no_pid_reports_no_stats() {
        let h = harness_with_stats(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
        start_web(&h).await;
        reply_of(
            dispatch(
                envelope(
                    2,
                    Request::Start {
                        apps: vec![AppConfig::minimal("worker", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Stop {
                        selector: SelectorSpec::Name("worker".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        let infos = list_flock(&h.ctx, 4).await;
        let named = |name: &str| {
            infos
                .iter()
                .find(|info| info.name == name)
                .unwrap_or_else(|| panic!("{name} is missing from the listing"))
        };
        // The scripted table describes the first spawn's pid and no other,
        // so this is the one row carrying a reading, and the one a fallback
        // join would hand to its neighbour.
        assert_eq!(named("web").pid, Some(FIRST_SCRIPTED_PID));
        assert_eq!(named("web").memory_bytes, Some(SCRIPTED_TREE_BYTES));

        assert_eq!(named("worker").pid, None);
        assert_eq!(named("worker").memory_bytes, None);
        assert_eq!(named("worker").cpu_percent, None);
    }

    /// A 5.77 ms syscall walk over the host's whole process table, on every
    /// `start`, buys a reading nobody reads there.
    ///
    /// Asserted on `Started` rather than on `Stopped`: a stopped sheep has no
    /// pid, so its row comes back empty whether or not the verb sampled and
    /// the assertion would hold for either implementation.
    #[tokio::test]
    async fn a_lifecycle_reply_carries_no_stats() {
        let h = harness_with_stats(vec![ProcScript::never_exits()]);
        let started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Ok(Response::Started(infos)) = started.result else {
            panic!("expected Started, got {:?}", started.result)
        };
        assert_eq!(
            infos[0].pid,
            Some(FIRST_SCRIPTED_PID),
            "a row with no pid would report no stats however the verb behaved"
        );
        assert_eq!(
            infos[0].memory_bytes, None,
            "only `flock` and `describe` take a live sample"
        );
        assert_eq!(infos[0].cpu_percent, None);
    }

    /// Enables `name` as a built-in dog through the real dispatch path,
    /// returning the entry it registered.
    async fn enable_dog(ctx: &RpcContext, id: u64, name: &str) -> ProcessInfo {
        let reply = reply_of(
            dispatch(
                envelope(
                    id,
                    Request::EnableDog {
                        name: name.to_string(),
                        source: DogSource::BuiltIn,
                    },
                ),
                ctx,
            )
            .await,
        );
        let Ok(Response::DogStarted(info)) = reply.result else {
            panic!("expected DogStarted, got {:?}", reply.result)
        };
        info
    }

    /// Starts one sheep named `web` carrying a secret env value, which is
    /// the fixture the config-pane cases below all want.
    async fn start_web_with_a_secret(ctx: &RpcContext) {
        let mut config = AppConfig::minimal("web", "./srv");
        config
            .env
            .insert("DB_PASS".to_string(), "hunter2".to_string());
        let started =
            reply_of(dispatch(envelope(1, Request::Start { apps: vec![config] }), ctx).await);
        assert!(started.result.is_ok(), "{:?}", started.result);
    }

    /// A dog runs at the daemon's own trust level and its binary is what
    /// `shep adopt` vetted, so a parked `PATH`, `LD_PRELOAD` or
    /// `DYLD_INSERT_LIBRARIES` for its next respawn would run arbitrary
    /// code at that level. Refused at the daemon, not at a caller, because
    /// the socket is already live. Asserts the store as well as the code:
    /// a refusal that still wrote would be the same hole.
    #[tokio::test(start_paused = true)]
    async fn a_dog_is_refused_an_env_override_rather_than_given_one() {
        let h = harness(vec![ProcScript::never_exits()]);
        let dog = enable_dog(&h.ctx, 1, "bark").await;

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SetSheepEnv {
                        name: "bark".to_string(),
                        key: "PATH".to_string(),
                        value: Some("/tmp/evil".to_string().into()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Err(err) = reply.result else {
            panic!("a dog was given an env override")
        };
        assert_eq!(err.code, RpcErrorCode::InvalidConfig);
        assert!(err.message.contains("bark is a dog"), "{}", err.message);

        assert!(
            shep_core::overrides::get(&h.ctx.paths.overrides, "bark")
                .unwrap()
                .is_none(),
            "the refusal still wrote the store"
        );
        let described = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Describe {
                        selector: SelectorSpec::Name("bark".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Ok(Response::Described(infos)) = described.result else {
            panic!("expected Described")
        };
        assert_eq!(infos[0].id, dog.id);
        assert_eq!(infos[0].pending, None, "the refusal still parked a config");
    }

    /// No other request hands a client a config at all, so this would be a
    /// read surface that exists for dogs and nothing else. A dog's config
    /// is what `shep adopt` vetted, not something an operator edits here.
    #[tokio::test(start_paused = true)]
    async fn a_dogs_config_is_not_readable_through_the_sheep_config_pane() {
        let h = harness(vec![ProcScript::never_exits()]);
        enable_dog(&h.ctx, 1, "bark").await;

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SheepConfig {
                        name: "bark".to_string(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Err(err) = reply.result else {
            panic!("a dog's config was served to a pane")
        };
        assert_eq!(err.code, RpcErrorCode::InvalidConfig);
        assert!(err.message.contains("bark is a dog"), "{}", err.message);
    }

    /// Reads one sheep's config view, for the cases that assert on it.
    async fn sheep_config_view(
        ctx: &RpcContext,
        id: u64,
        name: &str,
    ) -> shep_core::protocol::SheepConfigView {
        let reply = reply_of(
            dispatch(
                envelope(
                    id,
                    Request::SheepConfig {
                        name: name.to_string(),
                    },
                ),
                ctx,
            )
            .await,
        );
        match reply.result {
            Ok(Response::SheepConfig(view)) => *view,
            other => panic!("expected SheepConfig, got {other:?}"),
        }
    }

    /// Both halves matter: a pane that cannot name the keys cannot offer
    /// to edit them, and one handed the values has put a secret on a
    /// socket for nothing (IR-41).
    #[tokio::test(start_paused = true)]
    async fn sheep_config_answers_with_env_emptied_and_its_keys_listed() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SheepConfig {
                        name: "web".to_string(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Ok(Response::SheepConfig(view)) = reply.result else {
            panic!("expected SheepConfig")
        };
        assert_eq!(view.name, "web");
        assert!(view.config.env.is_empty());
        assert_eq!(view.env_keys, ["DB_PASS"]);
    }

    /// A pane asking about a sheep deleted out from under it is normal,
    /// not a daemon fault. `Internal` would send an operator looking for
    /// a bug in the shepherd.
    #[tokio::test(start_paused = true)]
    async fn sheep_config_for_an_unknown_name_is_not_found_not_internal() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::SheepConfig {
                        name: "ghost".to_string(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Err(err) = reply.result else {
            panic!("expected a refusal")
        };
        assert_eq!(err.code, RpcErrorCode::NotFound);
    }

    /// The running process was handed its environment at spawn and cannot
    /// be handed another, so an edit reported as applied would be one the
    /// operator believes is in force when it is not.
    #[tokio::test(start_paused = true)]
    async fn set_sheep_env_writes_the_store_and_parks_env_until_a_respawn() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SetSheepEnv {
                        name: "web".to_string(),
                        key: "NEW".to_string(),
                        value: Some("1".to_string().into()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(
            matches!(reply.result, Ok(Response::SheepEnvSet { .. })),
            "{:?}",
            reply.result
        );

        let stored = shep_core::overrides::get(&h.ctx.paths.overrides, "web")
            .unwrap()
            .unwrap();
        assert_eq!(stored.fields["env"]["NEW"], "1");

        let described = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Describe {
                        selector: SelectorSpec::Name("web".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Ok(Response::Described(infos)) = described.result else {
            panic!("expected Described")
        };
        assert_eq!(
            infos[0].pending.as_deref(),
            Some(["env".to_string()].as_slice())
        );
    }

    /// The CFG column reads `ProcessEntry::overridden`, so an `env` key
    /// left in the store's field set after its last value is gone marks a
    /// sheep that no longer differs from its Flockfile.
    #[tokio::test(start_paused = true)]
    async fn removing_the_last_env_override_stops_marking_the_sheep() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        for (id, value) in [(2, Some("1".to_string().into())), (3, None)] {
            let reply = reply_of(
                dispatch(
                    envelope(
                        id,
                        Request::SetSheepEnv {
                            name: "web".to_string(),
                            key: "NEW".to_string(),
                            value,
                        },
                    ),
                    &h.ctx,
                )
                .await,
            );
            assert!(reply.result.is_ok(), "{:?}", reply.result);
        }

        let stored = shep_core::overrides::get(&h.ctx.paths.overrides, "web")
            .unwrap()
            .unwrap();
        assert!(!stored.fields.contains_key("env"), "{stored:?}");

        let reply = reply_of(
            dispatch(
                envelope(
                    4,
                    Request::SheepConfig {
                        name: "web".to_string(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Ok(Response::SheepConfig(view)) = reply.result else {
            panic!("expected SheepConfig")
        };
        assert!(view.overridden.is_empty(), "{view:?}");
    }

    /// The muster roll is written from the `FlockRegistry`, and nothing on
    /// the restore path reads the override store, so a handler that parks
    /// a config without recording it looks correct in every live test and
    /// forgets the edit on the next cold start. Asserts the roll rather
    /// than the registry's own accessor, since the roll is what `shep
    /// muster` actually restores from.
    #[tokio::test(start_paused = true)]
    async fn a_set_env_reaches_the_roll_a_cold_restart_would_come_back_on() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SetSheepEnv {
                        name: "web".to_string(),
                        key: "NEW".to_string(),
                        value: Some("1".to_string().into()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(reply.result.is_ok(), "{:?}", reply.result);

        let infos = list_flock(&h.ctx, 3).await;
        let roll = h.ctx.registry.roll(&infos, 0);
        let web = roll
            .apps
            .iter()
            .find(|entry| entry.app.name == "web")
            .expect("web is in the roll");
        assert_eq!(web.app.env.get("NEW").map(String::as_str), Some("1"));
    }

    /// The sibling above asserts the registry; this asserts the file, which
    /// is what a cold boot actually reads. A parked edit moves no process, so
    /// the bus says nothing and the roll's only other schedule is the
    /// graceful shutdown a `SIGKILL` never reaches. The store keeps the edit
    /// either way and `overridden_for` reads it back, so a stale roll is not
    /// a lost edit but a divergent one: the sheep comes back on the old value
    /// under a CFG cell claiming the operator set a new one.
    #[tokio::test(start_paused = true)]
    async fn a_parked_env_edit_reaches_the_roll_before_a_hard_stop_can_lose_it() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        // The roll as a `shep save` left it, so the baseline is on disk
        // whatever the writer does, and the writer subscribing after the
        // start's own events so the edit below is all it has left to react
        // to.
        h.ctx.save_roll_now().await.unwrap();
        let baseline = crate::snapshot::read(&h.ctx.snapshot_path).unwrap();
        assert_eq!(baseline.apps[0].app.env.get("NEW"), None);
        let writer = crate::snapshot::spawn_snapshot_writer(
            h.ctx.snapshot_path.clone(),
            h.ctx.supervisor.clone(),
            h.ctx.registry.clone(),
            h.ctx.events.subscribe(),
        );
        let settle = std::time::Duration::from_millis(crate::snapshot::SNAPSHOT_DEBOUNCE_MS * 2);
        tokio::time::sleep(settle).await;

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SetSheepEnv {
                        name: "web".to_string(),
                        key: "NEW".to_string(),
                        value: Some("1".to_string().into()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(reply.result.is_ok(), "{:?}", reply.result);
        tokio::time::sleep(settle).await;

        // The hard stop: the writer goes where a `SIGKILL` would have taken
        // it, and no graceful `save_roll_now` runs after this line.
        writer.stop().await;

        let (events, _rx) = crate::bus::test_bus(64);
        let cold = crate::supervisor::spawn_supervisor(
            crate::fake::ScriptedRunner::new(vec![ProcScript::never_exits()]),
            h.ctx.paths.clone(),
            events.clone(),
        );
        // The cold registry, not `sheep_config`: that view clears `env` on
        // its way out, and the registry is what the restored sheep was
        // started from.
        let cold_registry = crate::snapshot::FlockRegistry::new();
        let restored = crate::snapshot::muster(
            &h.ctx.snapshot_path,
            &cold_registry,
            &cold,
            &events,
            &[],
            &[],
        )
        .await
        .unwrap();
        assert_eq!(restored, vec!["web".to_string()]);

        let listed = cold.list().await;
        let came_back = cold_registry.roll(&listed, 0);
        assert_eq!(
            came_back.apps[0].app.env.get("NEW").map(String::as_str),
            Some("1"),
            "the sheep must come back on the edit its CFG cell claims: {:?}",
            listed[0].overridden
        );
        cold.shutdown().await;
    }

    /// `map.remove` is a no-op for a key the app's own config supplied, so
    /// without a tombstone the store comes back empty and the removal
    /// lives only in `ProcessEntry::pending`, a change the operator just
    /// made.
    #[tokio::test(start_paused = true)]
    async fn removing_a_key_the_operator_never_set_is_still_recorded() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SetSheepEnv {
                        name: "web".to_string(),
                        key: "DB_PASS".to_string(),
                        value: None,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(reply.result.is_ok(), "{:?}", reply.result);

        let stored = shep_core::overrides::get(&h.ctx.paths.overrides, "web")
            .unwrap()
            .expect("the removal is recorded");
        assert_eq!(
            stored.fields["env"]["DB_PASS"],
            serde_json::Value::Null,
            "a removal is a tombstone, not an absence"
        );

        let view = sheep_config_view(&h.ctx, 3, "web").await;
        assert_eq!(view.overridden, ["env"]);
        assert!(!view.env_keys.contains(&"DB_PASS".to_string()));
    }

    /// Two things must compose: `merge_declared`'s env loop must skip a
    /// key held in `overridden_env` so the file's value does not come
    /// back, and `establish_env`, which runs after, must not spend the
    /// tombstone the way it spends a valued override, or `overridden`
    /// stops naming `env` while the sheep still differs from its file.
    /// Loaded twice on purpose: a single load would pass against a build
    /// that spent the tombstone and leaned on `declared_env` alone.
    #[tokio::test(start_paused = true)]
    async fn a_removed_key_stays_removed_and_stays_reported_across_reloads() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        let removed = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SetSheepEnv {
                        name: "web".to_string(),
                        key: "DB_PASS".to_string(),
                        value: None,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(removed.result.is_ok(), "{:?}", removed.result);

        // The Flockfile still declares the key the operator removed, which
        // is the whole point: a deploy re-runs the same file.
        for id in [3, 4] {
            let loaded = reply_of(
                dispatch(
                    envelope(
                        id,
                        Request::ApplyConfig {
                            apps: vec![DeclaredApp {
                                config: {
                                    let mut app = AppConfig::minimal("web", "./srv");
                                    app.env
                                        .insert("DB_PASS".to_string(), "fromfile".to_string());
                                    app
                                },
                                declared: ["name", "script"]
                                    .iter()
                                    .map(|k| (*k).to_string())
                                    .collect(),
                                declared_env: ["DB_PASS"]
                                    .iter()
                                    .map(|k| (*k).to_string())
                                    .collect(),
                            }],
                            reset: ResetDepth::None,
                        },
                    ),
                    &h.ctx,
                )
                .await,
            );
            let Ok(Response::Applied(report)) = loaded.result else {
                panic!("expected Applied")
            };
            assert_eq!(report[0].refused, None);

            let stored = shep_core::overrides::get(&h.ctx.paths.overrides, "web")
                .unwrap()
                .expect("the removal is still recorded");
            assert_eq!(
                stored.fields["env"]["DB_PASS"],
                serde_json::Value::Null,
                "load {id} spent the tombstone"
            );

            let view = sheep_config_view(&h.ctx, id + 10, "web").await;
            assert!(
                !view.env_keys.contains(&"DB_PASS".to_string()),
                "load {id} put the file's value back"
            );
            assert_eq!(view.overridden, ["env"], "load {id} stopped reporting it");
        }
    }

    /// Two halves, and the second is the one a refactor breaks:
    /// `SHEP_NAME` is injected per instance and refused in a hand-written
    /// env, so this is a real refusal an operator can meet from a
    /// free-text pane, and a handler that wrote first would leave a
    /// stored override for a config the daemon will not accept.
    #[tokio::test(start_paused = true)]
    async fn an_env_key_normalize_refuses_is_invalid_config_and_writes_nothing() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SetSheepEnv {
                        name: "web".to_string(),
                        key: "SHEP_NAME".to_string(),
                        value: Some("impostor".to_string().into()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Err(err) = reply.result else {
            panic!("a reserved env key was accepted")
        };
        assert_eq!(err.code, RpcErrorCode::InvalidConfig);
        assert!(
            shep_core::overrides::get(&h.ctx.paths.overrides, "web")
                .unwrap()
                .is_none(),
            "the refusal still wrote the store"
        );
    }

    /// Neither the caller's request nor a refusal they can act on by
    /// asking differently, which is why it gets a variant of its own
    /// rather than sharing `InvalidEnv`'s.
    #[tokio::test(start_paused = true)]
    async fn an_unreadable_override_store_is_internal_not_a_bad_request() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;
        std::fs::write(&h.ctx.paths.overrides, "{ this is not json").unwrap();

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SetSheepEnv {
                        name: "web".to_string(),
                        key: "NEW".to_string(),
                        value: Some("1".to_string().into()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Err(err) = reply.result else {
            panic!("an unreadable store was reported as success")
        };
        assert_eq!(err.code, RpcErrorCode::Internal);
        assert!(
            err.message.contains("overrides store unusable"),
            "{}",
            err.message
        );
    }

    /// Sends one `SetSheepField` and hands back the reply.
    async fn set_field(
        ctx: &RpcContext,
        id: u64,
        name: &str,
        key: &str,
        value: serde_json::Value,
    ) -> Result<Response, RpcError> {
        reply_of(
            dispatch(
                envelope(
                    id,
                    Request::SetSheepField {
                        name: name.to_string(),
                        key: key.to_string(),
                        value,
                    },
                ),
                ctx,
            )
            .await,
        )
        .result
    }

    /// The same edit through `ApplyConfig` at `ResetDepth::File` moves the
    /// field and spends the override in `merge_declared`, so the key drops
    /// from `overridden` and the pane's `*` marker never appears for a
    /// value the operator just set. Asserted through both `SheepConfig`,
    /// which the pane reads, and `ListFlock`, which the CFG column reads,
    /// because the request alone can be correct while both derived views
    /// are wrong.
    #[tokio::test(start_paused = true)]
    async fn a_field_edit_is_reported_as_an_operator_override() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        let view = sheep_config_view(&h.ctx, 2, "web").await;
        assert!(
            !view.overridden.contains(&"max_restarts".to_string()),
            "nothing is overridden before the edit: {:?}",
            view.overridden
        );

        let reply = set_field(&h.ctx, 3, "web", "max_restarts", serde_json::json!(40)).await;
        assert!(
            matches!(reply, Ok(Response::SheepFieldSet { .. })),
            "{reply:?}"
        );

        let stored = shep_core::overrides::get(&h.ctx.paths.overrides, "web")
            .unwrap()
            .expect("the edit is recorded");
        assert_eq!(stored.fields["max_restarts"], 40);

        let view = sheep_config_view(&h.ctx, 4, "web").await;
        assert_eq!(view.config.max_restarts, 40, "the pane shows the new value");
        assert!(
            view.overridden.contains(&"max_restarts".to_string()),
            "the `*` marker reads this: {:?}",
            view.overridden
        );

        // The CFG column's own source, which is a different code path from
        // the pane's and is the half that was silently wrong.
        let infos = list_flock(&h.ctx, 5).await;
        let web = infos
            .iter()
            .find(|info| info.name == "web")
            .expect("web is in the flock");
        assert!(
            web.overridden
                .as_deref()
                .is_some_and(|fields| fields.contains(&"max_restarts".to_string())),
            "{:?}",
            web.overridden
        );
    }

    /// The four-way apply classification governs this door: a `Live`
    /// field is in force now, a `NeedsRespawn` field parks and says so,
    /// and the pane's cost column promises exactly this.
    #[tokio::test(start_paused = true)]
    async fn a_live_field_applies_now_and_a_respawn_field_parks() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        let reply = set_field(&h.ctx, 2, "web", "max_restarts", serde_json::json!(40)).await;
        let Ok(Response::SheepFieldSet { pending, .. }) = reply else {
            panic!("{reply:?}")
        };
        assert!(!pending, "max_restarts is Live and is in force now");
        let view = sheep_config_view(&h.ctx, 3, "web").await;
        assert!(
            !view.pending.contains(&"max_restarts".to_string()),
            "{:?}",
            view.pending
        );

        let reply = set_field(&h.ctx, 4, "web", "script", serde_json::json!("./next")).await;
        let Ok(Response::SheepFieldSet { pending, .. }) = reply else {
            panic!("{reply:?}")
        };
        assert!(pending, "script needs a respawn");
        let view = sheep_config_view(&h.ctx, 5, "web").await;
        assert!(
            view.pending.contains(&"script".to_string()),
            "{:?}",
            view.pending
        );
        // And the Live edit is still in force beside the parked one.
        assert_eq!(view.config.max_restarts, 40);
        assert_eq!(
            view.overridden,
            ["max_restarts", "script"],
            "both are the operator's"
        );
    }

    /// One of two cases where `pending` carries information `apply_group`
    /// alone cannot: `reached_spec` builds a subset of the config, running
    /// plus the one field that reaches, and `normalize` checks fields
    /// against each other. `watch` needs a `cwd`, and a `cwd` still parked
    /// is not on that subset, so a `Live` field parks anyway.
    #[tokio::test(start_paused = true)]
    async fn a_live_field_whose_subset_will_not_normalize_parks_instead() {
        let h = harness(vec![ProcScript::never_exits()]);
        // No `cwd`, which is what makes `watch` refusable on its own.
        let started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(started.result.is_ok(), "{:?}", started.result);

        // `cwd` is NeedsRespawn, so this parks and the running spec still
        // has none.
        let reply = set_field(&h.ctx, 2, "web", "cwd", serde_json::json!("/srv")).await;
        let Ok(Response::SheepFieldSet { pending, .. }) = reply else {
            panic!("{reply:?}")
        };
        assert!(pending, "cwd needs a respawn");

        // `watch` is Live, so `apply_group` alone predicts "in force now".
        // The merge is valid (it carries the parked `cwd`) but the
        // subset is `running + watch`, which is a watch with no directory.
        let reply = set_field(&h.ctx, 3, "web", "watch", serde_json::json!(true)).await;
        let Ok(Response::SheepFieldSet { pending, .. }) = reply else {
            panic!("{reply:?}")
        };
        assert_eq!(
            apply_group("watch"),
            ApplyGroup::Live,
            "the premise: apply_group predicts this one applies now"
        );
        assert!(
            pending,
            "a Live field the running child cannot be given still parks"
        );

        // And the pane's own durable marker agrees, so an operator who
        // misses the status line still sees it on the row.
        let view = sheep_config_view(&h.ctx, 4, "web").await;
        assert!(
            view.pending.contains(&"watch".to_string()),
            "{:?}",
            view.pending
        );
    }

    /// `autostart` is `ApplyGroup::NextSpawn`, so `apply_group` predicts a
    /// respawn is needed, but `snapshot::restorable` reads it at muster or
    /// boot rather than at spawn, so it is in force the moment it lands on
    /// the stored spec. `kill_signal` is its group-mate and is genuinely
    /// read at spawn, asserted alongside it to pin the carve-out rather
    /// than the whole group.
    #[tokio::test(start_paused = true)]
    async fn autostart_reports_in_force_and_its_group_mate_reports_pending() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        for key in ["autostart", "kill_signal"] {
            assert_eq!(
                apply_group(key),
                ApplyGroup::NextSpawn,
                "the premise: both are the same group"
            );
        }

        let reply = set_field(&h.ctx, 2, "web", "autostart", serde_json::json!(false)).await;
        let Ok(Response::SheepFieldSet { pending, .. }) = reply else {
            panic!("{reply:?}")
        };
        assert!(
            !pending,
            "autostart is read at muster, so a restart would do nothing"
        );

        let reply = set_field(&h.ctx, 3, "web", "kill_signal", serde_json::json!("SIGINT")).await;
        let Ok(Response::SheepFieldSet { pending, .. }) = reply else {
            panic!("{reply:?}")
        };
        assert!(pending, "kill_signal really is read at a spawn");
    }

    /// `depends_on` shares `autostart`'s carve-out above, and the pane is
    /// the door that shows it: a field reported pending puts a `!` on the
    /// row and sends the operator to `shep reload` for a value the next
    /// ordered walk reads off the stored spec regardless.
    #[tokio::test(start_paused = true)]
    async fn depends_on_reports_in_force_the_way_autostart_does() {
        // fails if `depends_on` is left in the ordinary NextSpawn arm.
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        assert_eq!(
            apply_group("depends_on"),
            ApplyGroup::NextSpawn,
            "the premise: the carve-out is against its own group"
        );

        let reply = set_field(&h.ctx, 2, "web", "depends_on", serde_json::json!(["db"])).await;
        let Ok(Response::SheepFieldSet { pending, .. }) = reply else {
            panic!("{reply:?}")
        };
        assert!(
            !pending,
            "an ordered walk reads depends_on off the stored spec, so a restart would do nothing"
        );
    }

    /// The same hole `a_dog_is_refused_an_env_override_rather_than_given_one`
    /// closes for `env`, sharper here since this door reaches `script` and
    /// `args` directly and a dog runs at the daemon's own trust level.
    /// Asserts the store as well as the code: a refusal that still wrote
    /// would be the same hole with a better error.
    #[tokio::test(start_paused = true)]
    async fn a_dog_is_refused_a_config_field_rather_than_given_one() {
        let h = harness(vec![ProcScript::never_exits()]);
        let dog = enable_dog(&h.ctx, 1, "bark").await;

        let reply = set_field(&h.ctx, 2, "bark", "script", serde_json::json!("/tmp/evil")).await;
        let Err(err) = reply else {
            panic!("a dog was given a config field")
        };
        assert_eq!(err.code, RpcErrorCode::InvalidConfig);
        assert!(err.message.contains("bark is a dog"), "{}", err.message);
        assert!(
            shep_core::overrides::get(&h.ctx.paths.overrides, "bark")
                .unwrap()
                .is_none(),
            "the refusal still wrote the store"
        );
        drop(dog);
    }

    /// `env` would be replaced wholesale by a request carrying one value,
    /// wiping every other key; `instances` and `name` are Structural, and
    /// the count moves through `shep stock`.
    #[tokio::test(start_paused = true)]
    async fn env_and_the_structural_fields_are_refused_by_this_door() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        for (id, key, value) in [
            (2, "env", serde_json::json!({ "A": "1" })),
            (3, "instances", serde_json::json!(4)),
            (4, "name", serde_json::json!("other")),
        ] {
            let reply = set_field(&h.ctx, id, "web", key, value).await;
            let Err(err) = reply else {
                panic!("{key} was accepted")
            };
            assert_eq!(err.code, RpcErrorCode::InvalidConfig, "{key}");
            assert!(err.message.contains(key), "{key}: {}", err.message);
        }
        assert!(
            shep_core::overrides::get(&h.ctx.paths.overrides, "web")
                .unwrap()
                .is_none(),
            "a refusal still wrote the store"
        );
    }

    /// Three shapes, and each is the caller's: a key `AppConfig` has no
    /// field for, a value that will not deserialize into the field it
    /// names, and a value that deserializes and then fails `normalize`.
    #[tokio::test(start_paused = true)]
    async fn a_field_this_build_refuses_is_invalid_config_and_writes_nothing() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        for (id, key, value) in [
            (2, "no_such_field", serde_json::json!(1)),
            (3, "max_restarts", serde_json::json!("forty")),
            (
                4,
                "cron_restart",
                serde_json::json!("not a cron expression"),
            ),
        ] {
            let reply = set_field(&h.ctx, id, "web", key, value).await;
            let Err(err) = reply else {
                panic!("{key} was accepted")
            };
            assert_eq!(err.code, RpcErrorCode::InvalidConfig, "{key}");
            assert!(
                shep_core::overrides::get(&h.ctx.paths.overrides, "web")
                    .unwrap()
                    .is_none(),
                "{key}: the refusal still wrote the store"
            );
        }
    }

    #[tokio::test(start_paused = true)]
    async fn an_unreadable_store_is_internal_for_a_field_edit_too() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;
        std::fs::write(&h.ctx.paths.overrides, "{ this is not json").unwrap();

        let reply = set_field(&h.ctx, 2, "web", "max_restarts", serde_json::json!(40)).await;
        let Err(err) = reply else {
            panic!("an unreadable store was reported as success")
        };
        assert_eq!(err.code, RpcErrorCode::Internal);
        assert!(
            err.message.contains("overrides store unusable"),
            "{}",
            err.message
        );
    }

    /// The muster roll is a registry record `rpc.rs` writes, not
    /// something the supervisor does. Nothing on the restore path reads
    /// the override store, so an edit that skipped it would survive a
    /// `shep daemon reload` and vanish on a cold restart.
    #[tokio::test(start_paused = true)]
    async fn a_field_edit_reaches_the_muster_roll() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web_with_a_secret(&h.ctx).await;

        let reply = set_field(&h.ctx, 2, "web", "max_restarts", serde_json::json!(40)).await;
        assert!(reply.is_ok(), "{reply:?}");

        let infos = list_flock(&h.ctx, 3).await;
        let roll = h.ctx.registry.roll(&infos, 0);
        let web = roll
            .apps
            .iter()
            .find(|entry| entry.app.name == "web")
            .expect("web is in the roll");
        assert_eq!(web.app.max_restarts, 40);
    }

    /// `Request` is `#[non_exhaustive]` with dispatch ending in a
    /// wildcard, so a variant missing its arm compiles and passes every
    /// other test, silently refused at runtime instead. The list below is
    /// hand-written and nothing makes it exhaustive: a new variant is
    /// covered only if its author adds it here.
    #[tokio::test(start_paused = true)]
    async fn every_new_variant_reaches_an_arm_and_not_the_wildcard() {
        let h = harness(vec![]);
        let requests = [
            Request::SheepConfig {
                name: "ghost".to_string(),
            },
            Request::SetSheepEnv {
                name: "ghost".to_string(),
                key: "K".to_string(),
                value: None,
            },
            Request::SetSheepField {
                name: "ghost".to_string(),
                key: "max_restarts".to_string(),
                value: serde_json::json!(1),
            },
            Request::SetDogConfig {
                name: "ghost".to_string(),
                toml: String::new().into(),
            },
            Request::PutSecrets {
                namespace: "ghost".to_string(),
                environment: "production".to_string(),
                entries: BTreeMap::new(),
            },
            Request::SetSheepEnvBatch {
                name: "ghost".to_string(),
                entries: BTreeMap::new(),
                force: false,
                dry_run: true,
            },
        ];
        for (id, request) in requests.into_iter().enumerate() {
            let named = format!("{request:?}");
            let reply = reply_of(
                dispatch(
                    envelope(
                        u64::try_from(id).expect("an index into `requests` fits a u64"),
                        request,
                    ),
                    &h.ctx,
                )
                .await,
            );
            let Err(err) = reply.result else {
                continue;
            };
            assert_ne!(
                err.message, "this daemon does not implement that request",
                "{named} fell through to the wildcard"
            );
        }
    }

    /// A handler that answered `Deleted(vec![])` without stopping anything
    /// passes every type-level test and leaves the dog running after `shep
    /// disable` reported success.
    #[tokio::test(start_paused = true)]
    async fn disabling_a_dog_stops_it_and_takes_it_off_the_listing() {
        let h = harness(vec![ProcScript::never_exits()]);
        let info = enable_dog(&h.ctx, 1, "bark").await;
        assert_eq!(info.dog, Some(DogSource::BuiltIn));

        let disabled = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::DisableDog {
                        name: "bark".to_string(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(disabled.result.unwrap(), Response::Deleted(vec![info.id]));
        assert!(h.ctx.supervisor.list().await.is_empty());
    }

    /// The file is written after the harness built its context, so a reader
    /// that cached at boot answers the empty string here.
    #[tokio::test(start_paused = true)]
    async fn a_dog_config_request_reads_the_file_as_it_stands_now() {
        let h = harness(vec![]);
        std::fs::write(&h.ctx.dogs_config, "[bark]\ndebounce = \"30s\"\n").unwrap();
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::DogConfig {
                        name: "bark".to_string(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Ok(Response::DogSection { toml }) = reply.result else {
            panic!("expected DogSection, got {:?}", reply.result)
        };
        assert!(toml.as_str().contains("30s"));
    }

    /// This door differs from a CLI writing the file directly because it
    /// publishes: a running dog subscribed to `config.dog.<name>` is the
    /// only reader that finds out a section moved. Asserted on a
    /// subscriber rather than the publish call, the way the dog
    /// contract's own `bark_subscribes_to_its_own_config_topic` does,
    /// since a publisher that ran proves nothing about what a dog hears.
    #[tokio::test(start_paused = true)]
    async fn set_dog_config_writes_the_file_and_a_subscriber_hears_about_it() {
        let h = harness(vec![ProcScript::never_exits()]);
        enable_dog(&h.ctx, 1, "bark").await;
        let mut sub = h.ctx.events.subscribe();

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SetDogConfig {
                        name: "bark".to_string(),
                        toml: "poll = \"30s\"\n".to_string().into(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert_eq!(
            reply.result.unwrap(),
            Response::DogConfigSet {
                name: "bark".to_string()
            }
        );

        let text = std::fs::read_to_string(&h.ctx.dogs_config).unwrap();
        assert!(text.contains("poll = \"30s\""), "{text}");

        // The dog's own spawn narrates itself on the same bus, so the
        // topic wanted here is not necessarily the first frame waiting.
        let mut topics = Vec::new();
        while let Ok(Ok(event)) = tokio::time::timeout(Duration::from_secs(5), sub.recv()).await {
            topics.push(event.to_event().topic().into_owned());
            if topics
                .last()
                .is_some_and(|topic| topic == "config.dog.bark")
            {
                break;
            }
        }
        assert!(
            topics.iter().any(|topic| topic == "config.dog.bark"),
            "{topics:?}"
        );
    }

    /// The dog most in need of configuring is the one that is switched
    /// off: an operator adopts a dog, sets its webhook, and only then
    /// enables it. A guard on `supervisor.list()` would refuse exactly
    /// that dog, and this one has never been started at all.
    #[tokio::test(start_paused = true)]
    async fn a_dog_that_is_adopted_and_never_started_can_still_be_configured() {
        let mut h = harness(vec![]);
        h.ctx.known_dogs = KnownDogs::new(["otel".to_string()].into_iter().collect());

        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::SetDogConfig {
                        name: "otel".to_string(),
                        toml: "endpoint = \"http://127.0.0.1:4317\"\n".to_string().into(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        assert_eq!(
            reply.result.unwrap(),
            Response::DogConfigSet {
                name: "otel".to_string()
            }
        );
        let text = std::fs::read_to_string(&h.ctx.dogs_config).unwrap();
        assert!(text.contains("4317"), "{text}");
    }

    /// A dog an operator has enabled but that is not up right now
    /// (crashed, stopped, or simply not spawned on this boot) is still a
    /// dog whose section this shepherd holds. The harness starts nothing,
    /// so `bark` is known and absent from the flock.
    #[tokio::test(start_paused = true)]
    async fn a_dog_that_is_enabled_but_not_running_can_still_be_configured() {
        let h = harness(vec![]);
        assert!(h.ctx.supervisor.list().await.is_empty());

        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::SetDogConfig {
                        name: "bark".to_string(),
                        toml: "poll = \"30s\"\n".to_string().into(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        assert_eq!(
            reply.result.unwrap(),
            Response::DogConfigSet {
                name: "bark".to_string()
            }
        );
    }

    /// A dog adopted and enabled since this shepherd started is not in
    /// the list the CLI handed over at boot (refreshed only by a `shep
    /// daemon reload`), so a guard that stopped there would refuse a dog
    /// that is up and answering right now.
    #[tokio::test(start_paused = true)]
    async fn a_dog_adopted_since_boot_is_reached_through_the_running_flock() {
        let mut h = harness(vec![ProcScript::never_exits()]);
        h.ctx.known_dogs = KnownDogs::new(BTreeSet::new());
        enable_dog(&h.ctx, 1, "bark").await;

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::SetDogConfig {
                        name: "bark".to_string(),
                        toml: "poll = \"30s\"\n".to_string().into(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        assert_eq!(
            reply.result.unwrap(),
            Response::DogConfigSet {
                name: "bark".to_string()
            }
        );
    }

    /// The boot-time list is a snapshot, so a dog adopted against a running
    /// shepherd is in neither half of the old guard once it is off the
    /// flock, and both of these were refused:
    ///
    /// - adopt, disable, configure, enable
    /// - adopt, the dog crashes for want of config, configure
    ///
    /// The second is bark's own situation on a fresh install, and
    /// `docs/dogs.md` promises configure-then-enable works.
    #[tokio::test(start_paused = true)]
    async fn a_dog_adopted_since_boot_stays_configurable_once_it_is_disabled() {
        let mut h = harness(vec![ProcScript::never_exits()]);
        h.ctx.known_dogs = KnownDogs::new(BTreeSet::new());
        enable_dog(&h.ctx, 1, "bark").await;
        let disabled = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::DisableDog {
                        name: "bark".to_string(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(disabled.result.is_ok(), "{:?}", disabled.result);
        assert!(
            !h.ctx
                .supervisor
                .list()
                .await
                .iter()
                .any(|info| info.name == "bark"),
            "the flock must not hold it, or the widening answers and the test means nothing"
        );

        let reply = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::SetDogConfig {
                        name: "bark".to_string(),
                        toml: "poll = \"30s\"\n".to_string().into(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        assert_eq!(
            reply.result.unwrap(),
            Response::DogConfigSet {
                name: "bark".to_string()
            }
        );
    }

    /// This is the inverse of the guard every other config door carries:
    /// `dogs.toml` holds dogs' sections and nothing else, so it has to
    /// refuse a sheep's name, one nobody registered with it. Refused
    /// before the file opens, so a mistyped name leaves no stray table
    /// behind for a dog that will never exist.
    #[tokio::test(start_paused = true)]
    async fn setting_a_dogs_config_over_a_sheeps_name_is_refused_and_writes_nothing() {
        let h = harness(vec![ProcScript::never_exits()]);
        let started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(started.result.is_ok(), "{:?}", started.result);
        std::fs::write(&h.ctx.dogs_config, "[bark]\npoll = \"60s\"\n").unwrap();

        for (id, name) in [(2, "web"), (3, "ghost")] {
            let reply = reply_of(
                dispatch(
                    envelope(
                        id,
                        Request::SetDogConfig {
                            name: name.to_string(),
                            toml: "poll = \"30s\"\n".to_string().into(),
                        },
                    ),
                    &h.ctx,
                )
                .await,
            );
            let Err(err) = reply.result else {
                panic!("{name} is not a dog and must be refused")
            };
            assert_eq!(err.code, RpcErrorCode::NotFound, "{err:?}");
            assert!(err.message.contains(name), "{err:?}");
        }

        assert_eq!(
            std::fs::read_to_string(&h.ctx.dogs_config).unwrap(),
            "[bark]\npoll = \"60s\"\n"
        );
    }

    /// `otel` is outside the harness's built-in dogs, so the guard gets past
    /// `known_dogs` and reaches the running-flock widening, which is the half
    /// that has to answer on a stopped engine.
    #[tokio::test]
    async fn setting_a_dogs_config_against_a_stopped_engine_is_refused_not_a_panic() {
        let h = harness(vec![]);
        h.ctx.supervisor.shutdown().await;

        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::SetDogConfig {
                        name: "otel".to_string(),
                        toml: "poll = \"30s\"\n".to_string().into(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        let Err(err) = reply.result else {
            panic!("otel is not a known dog and must be refused")
        };
        assert_eq!(err.code, RpcErrorCode::NotFound, "{err:?}");
    }

    /// Sends `entries` for `namespace` in `production` and returns the reply.
    async fn push(ctx: &RpcContext, id: u64, namespace: &str, entries: &[(&str, &str)]) -> Reply {
        reply_of(
            dispatch(
                envelope(
                    id,
                    Request::PutSecrets {
                        namespace: namespace.to_string(),
                        environment: "production".to_string(),
                        entries: entries
                            .iter()
                            .map(|(key, value)| ((*key).to_string(), (*value).to_string().into()))
                            .collect(),
                    },
                ),
                ctx,
            )
            .await,
        )
    }

    /// The registry the arm writes is the one the supervisor resolves
    /// against, so this asserts on the shared handle and not on the count
    /// alone: an arm that answered `accepted` off its own request without
    /// storing anything would pass on the reply and fail here.
    #[tokio::test(start_paused = true)]
    async fn a_push_lands_in_the_registry_the_supervisor_reads() {
        let h = harness(vec![]);
        let reply = push(&h.ctx, 1, "vercel", &[("API_KEY", "sk_live"), ("B", "2")]).await;
        assert_eq!(reply.result.unwrap(), Response::SecretsPut { accepted: 2 });

        let snapshot = h.ctx.provider_secrets.snapshot();
        assert_eq!(
            snapshot.values["vercel"]["API_KEY"]["production"],
            "sk_live"
        );
        assert!(snapshot.pushed["vercel"].contains("production"));
    }

    /// A namespace with a `/` in it is the exact shape `SecretRef::parse`
    /// splits on, so storing under it would answer `accepted` for values
    /// no `{{secret:...}}` reference could ever name.
    #[tokio::test(start_paused = true)]
    async fn a_namespace_outside_the_grammar_is_refused_and_stores_nothing() {
        let h = harness(vec![]);
        let reply = push(&h.ctx, 1, "ver/cel", &[("A", "1")]).await;

        let Err(err) = reply.result else {
            panic!("a namespace carrying a separator must be refused")
        };
        assert_eq!(err.code, RpcErrorCode::InvalidConfig, "{err:?}");
        assert!(h.ctx.provider_secrets.pushed().is_empty());
    }

    /// A key with a `/` in it is as unreachable as a namespace with one:
    /// `SecretRef::parse` splits on the separator, so `{{secret:ns/A/B}}`
    /// names a namespace `ns` and a key `A/B` that `parse` refuses. The two
    /// names were checked and the keys were not.
    #[tokio::test(start_paused = true)]
    async fn an_entry_key_outside_the_grammar_refuses_the_whole_push() {
        let h = harness(vec![]);
        let reply = push(&h.ctx, 1, "vercel", &[("GOOD", "1"), ("A/B", "2")]).await;

        let Err(err) = reply.result else {
            panic!("a key carrying a separator must be refused")
        };
        assert_eq!(err.code, RpcErrorCode::InvalidConfig, "{err:?}");
        assert!(err.message.contains("A/B"), "names the key: {err:?}");
        assert!(
            h.ctx.provider_secrets.pushed().is_empty(),
            "the whole push is refused, not the one key"
        );
    }

    /// `MAX_VALUE_BYTES` guards `secrets::set`, so an operator cannot put a
    /// blob in the store. Without the same cap here a socket peer sets the
    /// shepherd's memory, and with `persist` on, what it sent is
    /// re-serialized and fsynced whole on every later push.
    #[tokio::test(start_paused = true)]
    async fn an_oversized_value_refuses_the_whole_push_without_quoting_it() {
        let h = harness(vec![]);
        let huge = "x".repeat(shep_core::secrets::MAX_VALUE_BYTES + 1);
        let reply = push(&h.ctx, 1, "vercel", &[("BIG", &huge)]).await;

        let Err(err) = reply.result else {
            panic!("a value over the cap must be refused")
        };
        assert_eq!(err.code, RpcErrorCode::InvalidConfig, "{err:?}");
        assert!(err.message.contains("BIG"), "names the key: {err:?}");
        assert!(
            !err.message.contains("xxxx"),
            "never the value itself: {err:?}"
        );
        assert!(h.ctx.provider_secrets.pushed().is_empty());
    }

    /// The cap is a ceiling, not a refusal of anything long: a 4096-bit RSA
    /// key in PEM is 3272 bytes, which is what the limit was sized for.
    #[tokio::test(start_paused = true)]
    async fn a_value_at_the_cap_is_accepted() {
        let h = harness(vec![]);
        let big = "x".repeat(shep_core::secrets::MAX_VALUE_BYTES);
        assert!(
            push(&h.ctx, 1, "vercel", &[("BIG", &big)])
                .await
                .result
                .is_ok()
        );
    }

    /// The same refusal for the other name, since an environment outside
    /// the grammar is just as unreachable as a namespace.
    #[tokio::test(start_paused = true)]
    async fn an_environment_outside_the_grammar_is_refused() {
        let h = harness(vec![]);
        let reply = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::PutSecrets {
                        namespace: "vercel".to_string(),
                        environment: String::new(),
                        entries: BTreeMap::new(),
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        let Err(err) = reply.result else {
            panic!("an empty environment must be refused")
        };
        assert_eq!(err.code, RpcErrorCode::InvalidConfig, "{err:?}");
    }

    /// The arm reads `persist` from the file on every push, so a dog whose
    /// operator turned it off never has its values written to disk. Asserts
    /// on the file rather than on the reply: the push succeeds either way,
    /// and the whole point of the setting is what is NOT there.
    #[tokio::test(start_paused = true)]
    async fn persist_false_in_dogs_toml_keeps_the_values_off_disk() {
        let h = harness(vec![]);
        std::fs::write(&h.ctx.dogs_config, "[vercel]\npersist = false\n").unwrap();

        assert!(
            push(&h.ctx, 1, "vercel", &[("A", "1")])
                .await
                .result
                .is_ok()
        );

        assert!(!h.ctx.paths.secrets_cache.exists());
        assert!(h.ctx.provider_secrets.pushed().contains_key("vercel"));
    }

    /// The default, and the half the test above cannot prove: a dog whose
    /// section says nothing gets a cache.
    #[tokio::test(start_paused = true)]
    async fn a_dog_that_says_nothing_about_persist_gets_a_cache() {
        let h = harness(vec![]);
        assert!(
            push(&h.ctx, 1, "vercel", &[("A", "1")])
                .await
                .result
                .is_ok()
        );
        assert!(h.ctx.paths.secrets_cache.exists());
    }

    /// Both halves: a filter that excluded dogs outright would leave `shep
    /// describe bark` unable to answer at all, and a listing that includes
    /// them puts a row in the flock table with nowhere to go.
    #[tokio::test(start_paused = true)]
    async fn describe_sweeps_past_a_dog_and_still_answers_when_one_is_named() {
        let h = harness(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
        let started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(started.result.is_ok(), "{:?}", started.result);
        let dog = enable_dog(&h.ctx, 2, "bark").await;

        let swept = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Describe {
                        selector: SelectorSpec::All,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Ok(Response::Described(hits)) = swept.result else {
            panic!("expected Described, got {:?}", swept.result)
        };
        assert_eq!(
            hits.iter().map(|i| i.name.as_str()).collect::<Vec<_>>(),
            vec!["web"],
            "`all` is the flock, not the kennel"
        );

        let named = reply_of(
            dispatch(
                envelope(
                    4,
                    Request::Describe {
                        selector: SelectorSpec::Name("bark".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Ok(Response::Described(hits)) = named.result else {
            panic!("expected Described, got {:?}", named.result)
        };
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].id, dog.id);
    }

    /// `start_dog` is idempotent by name, so the squatter comes back as an
    /// `Ok`, and a caller that trusted it would print "bark enabled", write
    /// `enabled_dogs = ["bark"]`, and never have a dog.
    #[tokio::test(start_paused = true)]
    async fn enabling_a_dog_over_a_sheeps_name_is_refused_rather_than_faked() {
        let h = harness(vec![ProcScript::never_exits()]);
        let started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("bark", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        assert!(started.result.is_ok(), "{:?}", started.result);

        let reply = reply_of(
            dispatch(
                envelope(
                    2,
                    Request::EnableDog {
                        name: "bark".to_string(),
                        source: DogSource::BuiltIn,
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Err(err) = reply.result else {
            panic!("expected a refusal, got {:?}", reply.result)
        };
        assert_eq!(err.code, RpcErrorCode::InvalidConfig);
        assert!(
            err.message.contains("bark"),
            "the refusal names the collision: {}",
            err.message
        );
        let listed = h.ctx.supervisor.list().await;
        assert_eq!(listed.len(), 1, "nothing was started: {listed:?}");
        assert_eq!(listed[0].dog, None);
    }

    /// The split is a cost decision (`with_lambs`) and nothing else enforces
    /// it: both arms build their rows from the same `snapshot_all`, so a
    /// helper applied in the wrong place looks correct at every other level.
    #[tokio::test(start_paused = true)]
    async fn only_describe_carries_a_lamb_tree() {
        // A process table where FIRST_SCRIPTED_PID really has a child, so a
        // walk that runs finds something and a walk that does not is
        // distinguishable from one that found nothing.
        let h = harness_identifying(
            vec![ProcScript::never_exits()],
            vec![
                identity(FIRST_SCRIPTED_PID, None, "srv"),
                identity(FIRST_SCRIPTED_PID + 1, Some(FIRST_SCRIPTED_PID), "node"),
            ],
        );
        reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );

        let listed = reply_of(dispatch(envelope(2, Request::ListFlock), &h.ctx).await);
        let Ok(Response::Flock(rows)) = listed.result else {
            panic!("expected a flock listing");
        };
        assert!(
            rows.iter().all(|row| row.lambs.is_none()),
            "ListFlock must not walk the process table"
        );

        let described = reply_of(
            dispatch(
                envelope(
                    3,
                    Request::Describe {
                        selector: SelectorSpec::Name("web".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Ok(Response::Described(rows)) = described.result else {
            panic!("expected a describe listing");
        };
        assert_eq!(
            rows[0].lambs,
            Some(vec![Lamb::new(FIRST_SCRIPTED_PID + 1, "node")])
        );
    }

    /// Registers one built-in dog on `ctx`'s supervisor, the same way
    /// `spawn_enabled_dogs` does at boot.
    async fn start_dog(ctx: &RpcContext, name: &str) -> ProcessInfo {
        let spec = DogSpec {
            name: name.to_string(),
            source: DogSource::BuiltIn,
        };
        let app = crate::dogs::dog_app(&spec, &ctx.paths).expect("the dog fixture must assemble");
        ctx.supervisor
            .start_dog(app, DogSource::BuiltIn)
            .await
            .expect("the dog fixture must start")
    }

    /// The two lists `Request::DogStaleness` answers with.
    async fn staleness(ctx: &RpcContext) -> (Vec<String>, Vec<String>) {
        let reply = reply_of(dispatch(envelope(1, Request::DogStaleness), ctx).await);
        let Ok(Response::DogStaleness { stale, pending }) = reply.result else {
            panic!("expected a dog staleness answer");
        };
        (stale, pending)
    }

    /// The sheep is the point, not scenery: a reader that walked every row
    /// instead of every dog row would hold an operator's reload open waiting
    /// for `web` to handshake, which a sheep never does.
    #[tokio::test]
    async fn a_flock_of_ordinary_sheep_has_nothing_stale_and_nothing_pending() {
        let h = harness(vec![ProcScript::never_exits()]);
        let started = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Start {
                        apps: vec![AppConfig::minimal("web", "./srv")],
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        started.result.expect("the sheep must start");

        assert_eq!(staleness(&h.ctx).await, (Vec::new(), Vec::new()));
    }

    /// `shep flock` printed `(o.o) online`, restarts 0, for a dog whose own
    /// log was filling with protocol refusals: `status` answers a question
    /// the operator was not asking. Both halves are asserted, since losing
    /// the liveness would be the same defect pointed the other way.
    #[tokio::test]
    async fn a_listing_says_which_dogs_have_answered_this_shepherd() {
        let h = harness(vec![ProcScript::never_exits(), ProcScript::never_exits()]);
        start_dog(&h.ctx, "metrics").await;

        let silent = list_flock(&h.ctx, 1).await;
        let dog = silent
            .iter()
            .find(|info| info.name == "metrics")
            .expect("the dog must be listed");
        assert_eq!(dog.handshook, Some(false));
        assert_eq!(
            dog.status,
            ProcStatus::Online,
            "the process is up, and the listing still says so"
        );

        h.ctx.dog_refusals.handshook("metrics");
        let talking = list_flock(&h.ctx, 2).await;
        assert_eq!(
            talking
                .iter()
                .find(|info| info.name == "metrics")
                .expect("still listed")
                .handshook,
            Some(true)
        );
    }

    /// A sheep does not speak this protocol at all, so it has no handshake to
    /// report and `None` is the only honest answer. `Some(false)` here would
    /// paint every sheep in the flock as broken.
    #[tokio::test]
    async fn a_sheep_carries_no_handshake_fact_at_all() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_web(&h).await;

        let infos = list_flock(&h.ctx, 1).await;
        assert_eq!(infos[0].name, "web");
        assert_eq!(infos[0].handshook, None);
        assert_eq!(
            infos[0].dog_stale, None,
            "a sheep is never given up on, because it was never asked to answer"
        );
    }

    /// Both rows are `handshook: Some(false)` with a live process. One needs
    /// nothing done about it, the dog having been spawned a moment ago; the
    /// other is a dog this shepherd will never restart again.
    #[tokio::test]
    async fn a_listing_says_which_silent_dogs_this_shepherd_gave_up_on() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_dog(&h.ctx, "metrics").await;

        let waiting = list_flock(&h.ctx, 1).await;
        let dog = waiting
            .iter()
            .find(|info| info.name == "metrics")
            .expect("the dog must be listed");
        assert_eq!(dog.handshook, Some(false));
        assert_eq!(
            dog.dog_stale,
            Some(false),
            "a dog that has not answered YET is not one this shepherd gave up on"
        );

        // The ladder, driven the same way `a_dog_being_restarted_is_pending_
        // and_then_stale` drives it: one refusal buys the restart, the second
        // is the give-up.
        h.ctx.dog_refusals.refused("metrics");
        h.ctx.dog_refusals.refused("metrics");

        let given_up = list_flock(&h.ctx, 2).await;
        let dog = given_up
            .iter()
            .find(|info| info.name == "metrics")
            .expect("still listed");
        assert_eq!(dog.dog_stale, Some(true));
        assert_eq!(
            dog.status,
            ProcStatus::Online,
            "the process is still up, and the listing still says so"
        );

        // And it heals: a dog that gets in clears everything held against
        // it, so the listing must stop reporting the give-up.
        h.ctx.dog_refusals.handshook("metrics");
        let talking = list_flock(&h.ctx, 3).await;
        let dog = talking
            .iter()
            .find(|info| info.name == "metrics")
            .expect("still listed");
        assert_eq!(dog.handshook, Some(true));
        assert_eq!(dog.dog_stale, Some(false));
    }

    /// fails if `describe` answers a different question from `flock` about
    /// the same dog. It is the other verb an operator reads a listing from,
    /// and the one `shep describe <dog>` reaches by name.
    #[tokio::test]
    async fn describe_carries_the_handshake_fact_too() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_dog(&h.ctx, "metrics").await;

        let described = reply_of(
            dispatch(
                envelope(
                    1,
                    Request::Describe {
                        selector: SelectorSpec::Name("metrics".to_string()),
                    },
                ),
                &h.ctx,
            )
            .await,
        );
        let Ok(Response::Described(rows)) = described.result else {
            panic!("expected a describe listing");
        };
        assert_eq!(rows[0].handshook, Some(false));
    }

    /// A carried dog holds that state for the whole gap between the exec and
    /// its reconnect, so a report taken while it holds would read "nothing
    /// stale" as "every dog came back".
    #[tokio::test]
    async fn a_dog_that_has_not_handshaken_is_pending() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_dog(&h.ctx, "metrics").await;

        assert_eq!(
            staleness(&h.ctx).await,
            (Vec::new(), vec!["metrics".to_string()])
        );

        h.ctx.dog_refusals.handshook("metrics");
        assert_eq!(
            staleness(&h.ctx).await,
            (Vec::new(), Vec::new()),
            "a dog talking to this shepherd is settled and is not worth reporting"
        );
    }

    /// A refused dog passes through this state on its way to being stale, so
    /// a reader that treated it as settled would report every stale dog as
    /// healthy. Drives the ladder rather than asserting on the record,
    /// because the claim is about what a caller over the wire sees.
    #[tokio::test]
    async fn a_dog_being_restarted_is_pending_and_then_stale() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_dog(&h.ctx, "metrics").await;
        h.ctx.dog_refusals.handshook("metrics");

        h.ctx.dog_refusals.refused("metrics");
        assert_eq!(
            staleness(&h.ctx).await,
            (Vec::new(), vec!["metrics".to_string()]),
            "one refusal buys a restart; it does not condemn the dog"
        );

        h.ctx.dog_refusals.refused("metrics");
        assert_eq!(
            staleness(&h.ctx).await,
            (vec!["metrics".to_string()], Vec::new()),
            "a stale dog is a finding, not something still to wait on"
        );
    }

    /// A dog with no process, out of its restart budget, parked in a backoff
    /// or stopped by an operator, cannot handshake, so waiting on one would
    /// make every later reload pay the whole budget for a dog already
    /// reported broken everywhere else.
    #[tokio::test]
    async fn a_dog_that_has_stopped_running_is_not_waited_on() {
        let h = harness(vec![ProcScript::never_exits()]);
        start_dog(&h.ctx, "metrics").await;
        assert_eq!(staleness(&h.ctx).await.1, vec!["metrics".to_string()]);

        h.ctx
            .supervisor
            .stop(ProcessSelector::Name("metrics".to_string()))
            .await
            .expect("the dog must stop");

        assert_eq!(
            staleness(&h.ctx).await,
            (Vec::new(), Vec::new()),
            "a dog that is not running has nothing to answer with"
        );
    }
}