acton-reactive 9.0.0

Acton Reactive is the main crate of the Acton framework, designed for building reactive, event-driven, and asynchronous systems. It provides intuitive abstractions to make working with distributed actors seamless and efficient.
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
/*
 * Copyright (c) 2024. Govcraft
 *
 * Licensed under either of
 *   * Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *   * MIT license: http://opensource.org/licenses/MIT
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the applicable License for the specific language governing permissions and
 * limitations under that License.
 */

//! The supervising actor's side of registration.
//!
//! These run on the supervisor's own task, reached from its message loop.
//! *Recording* a child is deliberately **not** `async`: it touches only data the
//! actor already owns, plus a single non-blocking write to the waiting caller's
//! cell. Making registration synchronous is the point of giving the supervisor
//! sole ownership of its registry, and it is what lets a handler register a
//! child of its own — a handler has `&mut ManagedActor<Started, _>` but cannot
//! hold it across an `await`.
//!
//! *Creating* a child does await, and it awaits on user code — the child's own
//! `before_start` hook — so it does not run here at all. The message loop hands
//! each start to its own task and carries on; the answer comes back as a
//! message, like every other answer in this system.
//!
//! They take `&mut self` rather than `&self` for a reason worth keeping: an
//! `async fn(&self)` produces a future holding `&Self`, which is `Send` only if
//! `Self: Sync` — that is, only if the user's model is `Sync`, a bound this
//! crate refuses to impose.

use std::fmt::Debug;

use tracing::{trace, warn};

use std::sync::Arc;

use tokio::sync::watch;

use super::plan::{evaluate, RestartPlan, SupervisionOutcome};
use super::registry::{PendingSlot, SlotState, StartRecorded, StartTicket};
use super::{
    BackoffDelay, ChildBlueprint, ChildIndex, ChildSpawner, NewSlot, SupervisedChild,
    SupervisionError, SupervisionRegistry, SupervisionState, SupervisionStatus, TypedSpawner,
};
use crate::actor::managed_actor::started::Started;
use crate::actor::{
    ActorConfig, Idle, ManagedActor, RestartGeneration, RestartLimitExceeded, RestartLimiter,
    RestartLimiterConfig,
};
use crate::common::config::CONFIG;
use crate::common::ActorHandle;
use crate::message::{
    ChildTerminated, RegisterSupervisedChild, RestartDue, SupervisedChildStarted,
    UnregisterSupervisedChild,
};
use crate::traits::ActorHandleInterface;

/// Builds the status channel a supervised child publishes through.
pub fn status_channel(
    child: &acton_ern::Ern,
    handle: Option<ActorHandle>,
) -> (
    watch::Sender<SupervisionStatus>,
    watch::Receiver<SupervisionStatus>,
) {
    watch::channel(SupervisionStatus::new(
        child.clone(),
        handle,
        RestartGeneration::FIRST,
        SupervisionState::Starting,
        0,
    ))
}

/// Builds one supervised child, then hands it to its supervisor.
///
/// A free function rather than a method because it runs on its own task and
/// must not borrow the actor. It is the whole reason a supervisor stays
/// responsive while a child starts: everything slow about creating an actor,
/// including the child's `before_start` hook, happens here.
///
/// # The handle is never dropped on the floor
///
/// Between `spawn` returning and the supervisor recording it, this task holds
/// the only handle to a live actor. Two things can go wrong, and both end the
/// same way:
///
/// - delivery fails, because the supervisor stopped or its inbox closed;
/// - delivery is refused by this actor's own cancellation token.
///
/// In either case the child is stopped here. Dropping the handle instead would
/// leave an actor running that nothing in the system can reach.
///
/// # A full inbox cannot wedge this
///
/// Delivery goes into the supervisor's *bounded* inbox, so it is worth being
/// explicit about why waiting for capacity cannot become a standoff. The
/// supervisor never waits on this task — it launched it and moved on — so the
/// cycle that would be needed for a deadlock does not exist. What is left is:
///
/// - **The supervisor is running.** It keeps taking messages, capacity frees
///   up, delivery lands.
/// - **The supervisor is stopping.** Its shutdown closes the inbox, which fails
///   a waiting send rather than leaving it parked; a cancelled token fails it
///   sooner still. Either way this task learns, and stops the child.
/// - **The supervisor is wedged in a handler of its own that never returns.**
///   Delivery waits, and the child stays reachable through this task the whole
///   time. That is a stalled actor, which stops every other message equally;
///   nothing here makes it worse, and nothing is lost when it resolves.
///
/// `ActorHandle::stop` does wait for this task, but indirectly: it waits on the
/// handle's tracker, which holds the supervisor's message loop, and that loop's
/// shutdown waits on the tracker holding this one. An outside caller waiting,
/// then — not the supervisor, whose loop is still free to run, close its inbox,
/// and let this task finish.
async fn start_supervised_child(
    ticket: StartTicket,
    runtime: crate::common::ActorRuntime,
    supervisor: ActorHandle,
) {
    let outcome = ticket.spawner.spawn(runtime, supervisor.clone()).await;
    let started = SupervisedChildStarted {
        child: ticket.ern,
        index: ticket.index,
        outcome: outcome.clone(),
    };

    // Targets the supervisor's own inbox, exactly as its other supervision
    // messages do. `try_send` rather than `send` because the difference between
    // "delivered" and "nobody to deliver to" decides whether a live child needs
    // stopping, and `send` reports neither.
    let envelope = supervisor.create_envelope(Some(supervisor.reply_address()));
    let delivery = envelope.try_send(started).await;

    let Err(undeliverable) = delivery else {
        return;
    };

    match outcome {
        Ok(handle) => {
            warn!(
                "Supervisor {} could not be told about child {} ({}); stopping the child rather than losing it",
                supervisor.id(),
                handle.id(),
                undeliverable
            );
            stop_stray_child(handle).await;
        }
        Err(error) => {
            // Nothing was created, so nothing is stranded. The caller learns
            // from the status channel closing with the supervisor.
            trace!(
                "Supervisor {} could not be told that a child failed to start ({}): {}",
                supervisor.id(),
                undeliverable,
                error
            );
        }
    }
}

/// Stops a child nobody supervises, within the shutdown deadline.
///
/// Bounded for the same reason `terminate_children` bounds its stops: a child
/// that will not stop must not hold a task open forever. A child that outlives
/// the deadline is logged rather than waited on.
async fn stop_stray_child(handle: ActorHandle) {
    let deadline = std::time::Duration::from_millis(CONFIG.timeouts.actor_shutdown);
    match tokio::time::timeout(deadline, handle.stop()).await {
        Ok(Ok(())) => trace!("Stopped unsupervised child {}", handle.id()),
        Ok(Err(error)) => warn!(
            "Unsupervised child {} reported an error while stopping: {error:?}",
            handle.id()
        ),
        Err(_) => warn!(
            "Unsupervised child {} did not stop within {} ms",
            handle.id(),
            CONFIG.timeouts.actor_shutdown
        ),
    }
}

/// Stops one sibling as a step of a group restart, within the shutdown
/// deadline.
///
/// Bounded for a reason particular to this caller: the stops are sequential, so
/// one child that will not go down would otherwise stop every child before it
/// from being asked, and hold the whole group open. Missing the deadline costs
/// that one child its restart, which is the smaller failure.
async fn stop_group_member(handle: ActorHandle) {
    let deadline = std::time::Duration::from_millis(CONFIG.timeouts.actor_shutdown);
    match tokio::time::timeout(deadline, handle.stop()).await {
        Ok(Ok(())) => trace!("Stopped {} for a group restart", handle.id()),
        Ok(Err(error)) => warn!(
            "Child {} reported an error while stopping for a group restart: {error:?}",
            handle.id()
        ),
        Err(_) => warn!(
            "Child {} did not stop within {} ms, so its group restart may be refused as stale",
            handle.id(),
            CONFIG.timeouts.actor_shutdown
        ),
    }
}

impl<Model: Default + Send + Debug + 'static> ManagedActor<Started, Model> {
    /// Builds the restart allowance one child will be held to.
    ///
    /// **A child's own setting wins; a child that set none inherits its
    /// supervisor's.** A child may therefore raise its own `max_restarts` and
    /// effectively decline its supervisor's policy, which is a deliberate
    /// choice rather than an oversight: the two settings are set by the same
    /// author, and a child that knows it is expensive to rebuild is the thing
    /// that knows it.
    ///
    /// One helper, reached from every registration path. The three paths could
    /// each resolve this inline and would agree today; they would stop agreeing
    /// the first time somebody edited one of them, and the symptom would be a
    /// child getting a different restart budget depending on which API
    /// supervised it — a difference nobody would predict from the signatures.
    ///
    /// The limiter is built **per slot**, not shared. A supervisor holding one
    /// limiter for all its children would let one noisy child eat a sibling's
    /// allowance and escalate a child that had never failed.
    fn resolve_limiter(&self, child: Option<&RestartLimiterConfig>) -> RestartLimiter {
        child.or(self.restart_limiter_config.as_ref()).map_or_else(
            RestartLimiter::default,
            |config| RestartLimiter::new(config.clone()),
        )
    }

    /// Records a child this actor should look after.
    ///
    /// Synchronous: no I/O, no await, nothing to block on.
    pub(crate) fn register_supervised_child(&mut self, message: &RegisterSupervisedChild) {
        let slot = NewSlot {
            ern: message.child.clone(),
            handle: message.handle.clone(),
            spawner: message.spawner.clone(),
            restart_policy: message.restart_policy,
            limiter: self.resolve_limiter(message.limiter.as_ref()),
            status: message.status.clone(),
        };

        let outcome = match self.supervision.register(slot) {
            Ok(index) => {
                trace!(
                    "Actor {} now supervises child {} at {}",
                    self.id(),
                    message.child,
                    index
                );
                Ok(())
            }
            Err(error) => {
                warn!(
                    "Actor {} rejected supervision of child {}: {}",
                    self.id(),
                    message.child,
                    error
                );
                Err(error)
            }
        };

        Self::report(message.outcome.as_ref(), outcome);
    }

    /// Stops looking after a child, returning its handle to the caller's care.
    ///
    /// The child is not stopped here. Releasing a child and stopping it are
    /// separate decisions, and only the caller knows which it wants.
    pub(crate) fn unregister_supervised_child(&mut self, message: &UnregisterSupervisedChild) {
        if message.liveness.receiver_count() == 0 {
            trace!(
                "Releasing child {} with no caller waiting on the result",
                message.child
            );
        }

        let outcome = match self.supervision.retire(&message.child) {
            Ok(handle) => {
                trace!(
                    "Actor {} released child {} (handle retained: {}, caller is stopping it: {})",
                    self.id(),
                    message.child,
                    handle.is_some(),
                    message.stopping
                );
                // A child on its way out should stop answering to its IPC
                // names; a child being released to keep serving must keep
                // them. This is the only side that can tell the difference and
                // the only side that holds the runtime.
                //
                // The whole statement is conditional rather than the body of a
                // helper, so that without the `ipc` feature there is no empty
                // function taking a `self` it never reads.
                #[cfg(feature = "ipc")]
                if message.stopping {
                    let forgotten = self.runtime.ipc_forget(&message.child);
                    if forgotten > 0 {
                        trace!(
                            "Actor {} dropped {} IPC name(s) for released child {}",
                            self.id(),
                            forgotten,
                            message.child
                        );
                    }
                }
                Ok(handle)
            }
            Err(error) => {
                warn!(
                    "Actor {} cannot release child {}: {}",
                    self.id(),
                    message.child,
                    error
                );
                Err(error)
            }
        };

        if message.outcome.set(outcome).is_err() {
            warn!("Release outcome cell was already set; ignoring the later result");
        }
    }

    /// Hands the result back to a waiting caller, if one is waiting.
    ///
    /// A failed `set` means the cell was already written, which can only happen
    /// if a caller reused it. Nothing is retried: the first answer stands.
    fn report(
        cell: Option<&crate::message::RegistrationOutcome>,
        outcome: Result<(), SupervisionError>,
    ) {
        if let Some(cell) = cell {
            if cell.set(outcome).is_err() {
                warn!("Supervision outcome cell was already set; ignoring the later result");
            }
        }
    }

    /// Every child this actor should stop when it shuts down.
    ///
    /// The union of three views that can legitimately disagree, deduplicated by
    /// identifier:
    ///
    /// - the registry, which is authoritative but only knows what this actor's
    ///   task has already processed;
    /// - `handle.children()`, which is written synchronously by `supervise()`
    ///   but only on the handle clone that call was made through;
    /// - `late_arrivals`, children whose start landed in the inbox after this
    ///   actor stopped reading it.
    ///
    /// No one of them is sufficient. A child supervised through a handle clone
    /// obtained after this actor started is absent from the task-local
    /// `children` map, because cloning a handle deep-copies that map. A child
    /// supervised from inside this actor's own handler is present there
    /// immediately, but its registration message may still be queued behind the
    /// very `Terminate` that triggered this shutdown. And a child this actor
    /// started itself is in neither until its start task's report is processed,
    /// which may never happen. Reading fewer views drops one of those children
    /// on the floor.
    pub(crate) fn shutdown_child_handles(&self, late_arrivals: Vec<ActorHandle>) -> Vec<ActorHandle> {
        let mut seen = std::collections::HashSet::new();
        let mut handles = Vec::new();

        for handle in self.supervision.live_handles() {
            if seen.insert(handle.id()) {
                handles.push(handle);
            }
        }

        for entry in self.handle.children() {
            let handle = entry.value().clone();
            if seen.insert(handle.id()) {
                handles.push(handle);
            }
        }

        for handle in late_arrivals {
            if seen.insert(handle.id()) {
                handles.push(handle);
            }
        }

        handles
    }

    /// This actor's record of its children.
    pub(crate) const fn supervision_mut(&mut self) -> &mut SupervisionRegistry {
        &mut self.supervision
    }

    /// Starts a child under this actor's supervision, recording it directly.
    ///
    /// Records **synchronously** into this actor's own registry: no message, no
    /// round trip, and nothing to wait on.
    ///
    /// # Not reachable from user code
    ///
    /// Crate-internal on purpose. Calling this needs `&mut self` held across an
    /// `await`, and no user-facing context provides that. A `mutate_on` handler
    /// returns a `'static` future that cannot borrow the actor, and all four
    /// lifecycle hooks take `&ManagedActor<Started, _>` rather than `&mut`. Only
    /// the framework's own message loop qualifies.
    ///
    /// It is kept because the restart engine will drive it from inside that
    /// loop. Making it public would ship a method nothing could call.
    /// [`supervise_deferred`](Self::supervise_deferred) is the public path: same
    /// outcome, with the `await` moved into the message loop where one is
    /// available.
    ///
    /// # Errors
    ///
    /// [`SupervisionError::DuplicateChild`] if this actor already supervises a
    /// child with that identifier. The freshly started child is stopped before
    /// returning, so a rejected registration leaves nothing running.
    pub(crate) async fn supervise_with<C>(
        &mut self,
        config: ActorConfig,
        configure: impl Fn(&mut ManagedActor<Idle, C>) + Send + Sync + 'static,
    ) -> Result<SupervisedChild, SupervisionError>
    where
        C: Default + Send + Debug + 'static,
    {
        let blueprint: Arc<ChildBlueprint<C>> = Arc::new(configure);
        let spawner: Arc<dyn ChildSpawner> =
            Arc::new(TypedSpawner::new(config.clone(), blueprint));

        let child_id = config.id();
        let restart_policy = spawner.restart_policy();
        let limiter = self.resolve_limiter(config.restart_limiter_config());
        let handle = spawner.spawn(self.runtime.clone(), self.handle.clone()).await?;
        let (status, receiver) = status_channel(&child_id, Some(handle.clone()));

        let slot = NewSlot {
            ern: child_id.clone(),
            handle: handle.clone(),
            spawner: Some(spawner),
            restart_policy,
            limiter,
            status,
        };

        // The borrow of the registry ends with this statement, so the stop below
        // does not hold it across an await.
        let registered = self.supervision.register(slot);
        if let Err(error) = registered {
            // Nothing is supervising this child, so it must not be left running.
            let _ = handle.stop().await;
            return Err(error);
        }

        Ok(SupervisedChild::new(child_id, self.id.clone(), receiver))
    }

    /// Places a child under this actor's supervision from inside its own
    /// handler, starting it on the next turn of the message loop.
    ///
    /// This is the one registration path a supervisor can use on itself.
    /// [`ActorHandle::supervise_with`] cannot be: it waits for an
    /// acknowledgement this actor cannot produce while the handler asking for it
    /// is still running. The crate-internal `ManagedActor::supervise_with`
    /// cannot be either: it holds `&mut self` across an `await`, and no
    /// user-facing context provides that.
    ///
    /// So this one does not await at all. It records the child, queues the
    /// start, and returns. On its next turn the message loop hands the queued
    /// start to its own task, before it takes another message.
    ///
    /// Callable from a [`mutate_on`] closure body and from [`mutate_on_sync`],
    /// which is the whole point: both hand you `&mut ManagedActor<Started, _>`
    /// synchronously.
    ///
    /// # Waiting for the child
    ///
    /// The returned [`SupervisedChild`] starts out in
    /// [`SupervisionState::Starting`], because nothing has been created yet.
    /// Await [`wait_running`] to get the handle once it is up. Do not await it
    /// inside the handler that called this: the start happens after the handler
    /// returns, so waiting there would wait forever.
    ///
    /// # What does not run on your task
    ///
    /// The child is built on a task of its own, so a child that is slow to
    /// start — a `before_start` hook doing real work — does not stop its
    /// supervisor from taking messages meanwhile. A child's startup may
    /// therefore depend on its supervisor making progress: it can send to it,
    /// and be answered.
    ///
    /// That is not true of [`supervise`], which awaits `start()` on the
    /// caller's task, so a handler that adopts a child does run that child's
    /// `before_start` on its own actor's task.
    ///
    /// The order this buys you is worth being precise about: this call returns
    /// before the child exists, and the child may still be starting when the
    /// next message is handled. What is settled by the time this returns is the
    /// child's name and its place in start order, not its existence.
    ///
    /// [`supervise`]: crate::common::ActorHandle::supervise
    ///
    /// # Errors
    ///
    /// [`SupervisionError::DuplicateChild`] if this actor already supervises
    /// that identifier. Reported here, synchronously, before anything is built:
    /// the registry already knows the name at this point, so a collision costs
    /// a rejected call rather than an actor started and then stopped again.
    ///
    /// A start that fails later cannot be reported here. It arrives on the
    /// status channel instead, as a terminal state carrying the reason, which
    /// [`wait_running`] returns rather than waiting through.
    ///
    /// [`ActorHandle::supervise_with`]: crate::common::ActorHandle::supervise_with
    /// [`mutate_on`]: crate::actor::ManagedActor::mutate_on
    /// [`mutate_on_sync`]: crate::actor::ManagedActor::mutate_on_sync
    /// [`wait_running`]: SupervisedChild::wait_running
    pub fn supervise_deferred<C>(
        &mut self,
        config: ActorConfig,
        configure: impl Fn(&mut ManagedActor<Idle, C>) + Send + Sync + 'static,
    ) -> Result<SupervisedChild, SupervisionError>
    where
        C: Default + Send + Debug + 'static,
    {
        let blueprint: Arc<ChildBlueprint<C>> = Arc::new(configure);
        // Resolved before the configuration is consumed, and through the shared
        // helper rather than inline, so this path cannot drift from the other
        // two.
        let limiter = self.resolve_limiter(config.restart_limiter_config());
        let spawner: Arc<dyn ChildSpawner> = Arc::new(TypedSpawner::new(config, blueprint));

        let child_id = spawner.child_id().clone();
        let restart_policy = spawner.restart_policy();
        // No handle: this child does not exist yet.
        let (status, receiver) = status_channel(&child_id, None);

        self.supervision.register_pending(PendingSlot {
            ern: child_id.clone(),
            spawner,
            restart_policy,
            limiter,
            status,
        })?;

        trace!(
            "Actor {} recorded child {} for a deferred start",
            self.id(),
            child_id
        );

        Ok(SupervisedChild::new(child_id, self.id.clone(), receiver))
    }

    /// Launches a start task for every child recorded by
    /// [`supervise_deferred`](Self::supervise_deferred) since the last turn.
    ///
    /// Driven from the message loop, ahead of the wait for the next message, so
    /// that a child a handler asked for is on its way before this actor takes
    /// anything else on.
    ///
    /// Synchronous, and that is the point. Building a child means running the
    /// child's `before_start` hook, which is user code of unbounded duration;
    /// awaiting it here would stop this actor from taking messages — including
    /// its own `Terminate` — until somebody else's hook finished. So each start
    /// gets its own task and reports back through the inbox, and this loop turn
    /// costs a few task spawns.
    ///
    /// Launches nothing once this actor is cancelled. What is left stays queued
    /// for [`cancel_unfinished_children`](Self::cancel_unfinished_children) to
    /// answer.
    pub(crate) fn launch_pending_starts(&mut self) {
        while !self.is_cancelled() {
            let Some(ticket) = self.supervision.begin_start() else {
                break;
            };

            trace!(
                "Actor {} is starting supervised child {}",
                self.id(),
                ticket.ern
            );

            let runtime = self.runtime.clone();
            let supervisor = self.handle.clone();
            self.start_tasks
                .spawn(start_supervised_child(ticket, runtime, supervisor));
        }
    }

    /// Decides what to do about a child that has just terminated, and starts
    /// doing it.
    ///
    /// Synchronous, and reached from the message loop **without** suppressing
    /// handler dispatch — see the interception site for why that asymmetry with
    /// the other supervision messages is deliberate.
    ///
    /// The decision itself lives in [`evaluate`], which is pure. This does the
    /// three things a decision cannot: record the new slot state, publish it,
    /// and arm a timer.
    pub(crate) fn record_child_terminated(&mut self, notice: &ChildTerminated) {
        let Some(index) = self.supervision.index_of(&notice.child_id) else {
            // Not a child of this actor's, or one already retired. Either way
            // there is no record to update; a user handler may still care.
            return;
        };
        let Some(snapshot) = self.supervision.snapshot(index) else {
            return;
        };

        let views = self.supervision.views();
        // The supervisor consults *its own* strategy, not the child's.
        // `for_supervised_child` builds a child's configuration and a reader may
        // reasonably expect the strategy set there to govern; it does not. What
        // to do about a failure is the supervising actor's policy.
        let strategy = self.supervision_strategy;
        let now = std::time::Instant::now();

        let Some(slot) = self.supervision.slot_mut(index) else {
            return;
        };
        let outcome = evaluate(notice, &snapshot, strategy, slot.limiter_mut(), &views, now);

        match outcome {
            // The supervisor asked for this stop, or has already acted on a
            // notice for this incarnation. Nothing to record.
            SupervisionOutcome::Ignore => {
                trace!(
                    "Actor {} expected child {} to stop; no restart considered",
                    self.id(),
                    notice.child_id
                );
            }
            SupervisionOutcome::GroupStopLanded { then_restart } => {
                self.record_group_stop_landed(index, &notice.child_id, then_restart);
            }
            SupervisionOutcome::Forget => self.record_child_down(index, &notice.child_id),
            SupervisionOutcome::Escalate(exceeded) => {
                self.record_child_escalated(index, &notice.child_id, exceeded);
            }
            SupervisionOutcome::Restart { plan, backoff } => {
                self.schedule_restart(&notice.child_id, &plan, backoff);
            }
        }
    }

    /// Records a sibling that has finished stopping for a group restart.
    ///
    /// The half of a group restart the message loop owns. The task sequencing
    /// the group asked for this stop and will ask for the restart; what it
    /// cannot do is move the slot, because slots belong to the actor.
    ///
    /// `then_restart` is the whole decision, made by
    /// [`evaluate`](super::plan::evaluate) from state this actor recorded when
    /// the stop was issued. A child on its way back waits in
    /// [`SlotState::AwaitingBackoff`], which is the only state
    /// [`queue_restart`](SupervisionRegistry::queue_restart) will accept a
    /// restart from. A child the group could not recreate goes down for good,
    /// through the same path as any other child that is not coming back — so it
    /// gives up its IPC names exactly as that one does.
    fn record_group_stop_landed(
        &mut self,
        index: ChildIndex,
        child: &acton_ern::Ern,
        then_restart: bool,
    ) {
        if !then_restart {
            trace!(
                "Actor {} stopped child {} for a group restart and is leaving it down",
                self.id(),
                child
            );
            self.record_child_down(index, child);
            return;
        }

        let Some(slot) = self.supervision.slot_mut(index) else {
            return;
        };
        slot.set_handle(None);
        slot.set_state(SlotState::AwaitingBackoff);
        slot.publish();

        trace!(
            "Actor {} has child {} down and waiting on its group's restart",
            self.id(),
            child
        );
    }

    /// Records a child that terminated and is not coming back.
    fn record_child_down(&mut self, index: ChildIndex, child: &acton_ern::Ern) {
        trace!("Actor {} is leaving child {} down", self.id(), child);
        self.supervision.mark_terminal(index, SlotState::Down, None);
        // Conditional at the call site rather than a helper with an empty body,
        // so that without the `ipc` feature nothing is compiled at all.
        #[cfg(feature = "ipc")]
        self.forget_ipc_names(index, child);
    }

    /// Records a child that used up its restart allowance.
    ///
    /// Terminal, and that is the point of handling it at all: `evaluate` has
    /// been able to return this since the decision layer landed, and a slot left
    /// in `AwaitingBackoff` for a restart that will never be arranged leaves
    /// every caller waiting on a status that cannot change.
    ///
    /// What the supervisor does *about* an escalation beyond giving up is
    /// [`Escalation`], set with
    /// [`ActorConfig::with_escalation`](crate::actor::ActorConfig::with_escalation)
    /// and read here. The bookkeeping below is the same either way; the policy
    /// decides only what happens after it.
    ///
    /// [`Escalation`]: super::Escalation
    fn record_child_escalated(
        &mut self,
        index: ChildIndex,
        child: &acton_ern::Ern,
        exceeded: RestartLimitExceeded,
    ) {
        warn!(
            "Actor {} is giving up on child {} ({}): {}",
            self.id(),
            child,
            self.escalation,
            exceeded
        );

        // Built from the exceeded limit rather than read back off the slot,
        // because the slot is about to be marked terminal and this has to
        // describe the moment the supervisor gave up.
        let stats = crate::actor::RestartStats {
            restarts_in_window: exceeded.attempts,
            consecutive_restarts: exceeded.attempts,
            window_secs: exceeded.window_secs,
            max_restarts: exceeded.max_restarts,
        };
        self.supervision.mark_terminal(
            index,
            SlotState::Escalated,
            Some(SupervisionError::RestartLimit {
                child: child.clone(),
                limit: exceeded,
            }),
        );
        #[cfg(feature = "ipc")]
        self.forget_ipc_names(index, child);

        self.apply_escalation(child, stats);
    }

    /// Carries out this supervisor's escalation policy.
    ///
    /// # Why there is no wildcard arm
    ///
    /// [`Escalation`](super::Escalation) is `#[non_exhaustive]`, which normally
    /// argues for one. It does not here, and the compiler says so: the
    /// attribute has no effect within the crate that defines the enum, so a
    /// wildcard is unreachable code rather than future-proofing.
    ///
    /// That is the better outcome anyway. Adding a variant breaks this match at
    /// compile time, which forces whoever adds it to decide what a supervisor
    /// should *do* about it — at the one place that can answer. A wildcard here
    /// would silently answer for them, and a new escalation policy that quietly
    /// behaved as the old default would be a bug nothing reported.
    ///
    /// Downstream crates matching on `Escalation` still need their own
    /// wildcard; the attribute is doing its job for them.
    fn apply_escalation(&self, child: &acton_ern::Ern, stats: crate::actor::RestartStats) {
        match self.escalation {
            super::Escalation::NotifyParent => self.notify_parent_of_escalation(child, stats),
            super::Escalation::StopSupervisor => self.stop_self_after_escalation(child),
        }
    }

    /// Tells this actor's own parent that it could not keep a child running.
    ///
    /// Sent to the parent directly rather than broadcast: the parent is the
    /// actor with a stake in this one's failures, and it is the actor that would
    /// have been told had this supervisor stopped instead.
    ///
    /// A supervisor with no parent has nobody to tell, which is not a failure —
    /// it is the top of a tree. The event is still logged by the caller.
    ///
    /// Delivery goes onto its own task because sending is `async` and this runs
    /// on the message loop. Nothing waits on the answer.
    fn notify_parent_of_escalation(&self, child: &acton_ern::Ern, stats: crate::actor::RestartStats) {
        let Some(parent) = self.parent.clone() else {
            trace!(
                "Actor {} has no parent to tell that it gave up on child {}",
                self.id(),
                child
            );
            return;
        };

        let notification = super::SupervisionEscalated::new(
            self.id.clone(),
            child.clone(),
            stats,
            crate::actor::TerminationReason::Normal,
        );

        trace!(
            "Actor {} is telling parent {} that it gave up on child {}",
            self.id(),
            parent.id(),
            child
        );

        tokio::spawn(async move {
            parent.send(notification).await;
        });
    }

    /// Stops this supervisor, cascading to the children it has left.
    ///
    /// The Erlang/OTP behaviour: a supervisor that cannot keep its children
    /// running terminates and lets its own supervisor deal with it. Its
    /// remaining children go down with it through the ordinary cascading
    /// shutdown, and its parent is told through the ordinary `ChildTerminated`
    /// every stopping actor sends — so this does not also notify the parent
    /// itself, which would report the same failure twice.
    ///
    /// Cancellation rather than a stop message, because the two differ in a way
    /// that matters here: a `Terminate` in the inbox is read after everything
    /// queued ahead of it, including restart requests for children this
    /// supervisor has just decided it is no longer going to look after.
    fn stop_self_after_escalation(&self, child: &acton_ern::Ern) {
        warn!(
            "Actor {} is stopping itself because it could not keep child {} running",
            self.id(),
            child
        );

        if let Some(token) = &self.cancellation_token {
            token.cancel();
        }
    }

    /// Drops the IPC names of a child that has reached a terminal state.
    ///
    /// Callers otherwise keep sending into a mailbox nobody reads and are told
    /// nothing; with the names gone they are told there is no such actor.
    ///
    /// # Engine-managed children only, and the reason is not arbitrary
    ///
    /// Restricted to children the supervisor holds a blueprint for — the ones
    /// registered through `supervise_with` and `supervise_deferred`. A child
    /// adopted through the legacy `supervise()` path keeps its names exactly as
    /// it does today.
    ///
    /// That asymmetry is the double-restart firewall wearing different clothes.
    /// The blueprint is what makes a child engine-managed, and blueprints only
    /// reach the registry through two APIs that have never appeared in a
    /// released version, so no existing program can observe any of this. It
    /// looks like an arbitrary special case without that reasoning, which is why
    /// the reasoning is here rather than in a commit message somebody would have
    /// to go looking for.
    ///
    /// `Retired` is deliberately not a caller of this. Three slot states are
    /// terminal and only two of them mean the child is gone:
    /// [`ActorHandle::release`] retires a slot and hands back a child that is
    /// still running and still legitimately reachable.
    ///
    /// [`ActorHandle::release`]: crate::common::ActorHandle::release
    #[cfg(feature = "ipc")]
    fn forget_ipc_names(&self, index: ChildIndex, child: &acton_ern::Ern) {
        let engine_managed = self
            .supervision
            .slot(index)
            .is_some_and(super::registry::ChildSlot::is_restartable);
        if !engine_managed {
            return;
        }

        let forgotten = self.runtime.ipc_forget(child);
        if forgotten > 0 {
            trace!(
                "Actor {} dropped {} IPC name(s) for departed child {}",
                self.id(),
                forgotten,
                child
            );
        }
    }

    /// Carries out a restart plan: stops what it says to stop, and arranges for
    /// what it says to start.
    ///
    /// # One task owns the whole sequence
    ///
    /// A group restart is stop-everything-then-start-everything, and both
    /// halves are ordered. Handing that to a single task rather than spreading
    /// it across the message loop is what keeps the ordering rules in one
    /// readable place, and it is why no group bookkeeping lives in the
    /// registry: there is no rendezvous to arrange.
    ///
    /// [`SupervisionStrategy::OneForOne`] falls out of the same code with
    /// nothing special about it — an empty `stop` and a single-entry `restart`.
    ///
    /// [`SupervisionStrategy::OneForOne`]: super::SupervisionStrategy::OneForOne
    fn schedule_restart(
        &mut self,
        child: &acton_ern::Ern,
        plan: &RestartPlan,
        backoff: BackoffDelay,
    ) {
        let stops = self.begin_group_stops(plan);
        let dues = self.park_group_for_restart(plan);

        trace!(
            "Actor {} will stop {} child(ren) and restart {} after {}, prompted by child {}",
            self.id(),
            stops.len(),
            dues.len(),
            backoff,
            child
        );

        self.spawn_group_restart(stops, dues, backoff);
    }

    /// Marks every child the plan stops and collects handles to stop them with.
    ///
    /// The state goes on before any stop is sent, so that a termination racing
    /// back into the inbox finds a slot that already knows the stop was asked
    /// for. `then_restart` is read straight off the plan: a child in `stop` but
    /// not in `restart` is one the supervisor had to take down for consistency
    /// and cannot recreate, and it must not be resurrected.
    ///
    /// The handle is deliberately **not** cleared here. It is what a cascading
    /// shutdown reaches these children through, and the stop this issues is not
    /// a reason to make them unreachable before they are actually down.
    fn begin_group_stops(&mut self, plan: &RestartPlan) -> Vec<ActorHandle> {
        let mut stops = Vec::with_capacity(plan.stop.len());
        for index in &plan.stop {
            let then_restart = plan.restart.contains(index);
            let Some(slot) = self.supervision.slot_mut(*index) else {
                continue;
            };
            let handle = slot.handle().cloned();
            slot.set_state(SlotState::ExpectedStop { then_restart });
            slot.publish();
            if let Some(handle) = handle {
                stops.push(handle);
            }
        }
        stops
    }

    /// Parks the children that are already down, and names every restart to ask
    /// for when the group's backoff elapses.
    ///
    /// Two groups reach [`SlotState::AwaitingBackoff`] and they arrive by
    /// different roads. A child that is *not* being stopped is already down —
    /// the child that failed, plus any sibling that was down when it did — so
    /// it is parked now. A child that is being stopped gets there when its
    /// termination lands, through
    /// [`SupervisionOutcome::GroupStopLanded`](super::plan::SupervisionOutcome::GroupStopLanded).
    ///
    /// The generation is captured here for both, and that is safe for the ones
    /// still stopping: a generation only advances in
    /// [`complete_start`](SupervisionRegistry::complete_start), which cannot run
    /// for a child on its way down. Captured early and checked late is the whole
    /// point — anything that moves a slot on in the meantime makes the restart
    /// stale, and [`record_restart_due`](Self::record_restart_due) refuses it.
    fn park_group_for_restart(&mut self, plan: &RestartPlan) -> Vec<RestartDue> {
        let mut dues = Vec::with_capacity(plan.restart.len());
        for index in &plan.restart {
            let being_stopped = plan.stop.contains(index);
            let Some(slot) = self.supervision.slot_mut(*index) else {
                continue;
            };
            if !being_stopped {
                slot.set_handle(None);
                slot.set_state(SlotState::AwaitingBackoff);
                slot.publish();
            }
            dues.push(RestartDue {
                child: slot.ern().clone(),
                index: *index,
                generation: slot.generation(),
            });
        }
        dues
    }

    /// Runs a group restart to completion on its own task.
    ///
    /// # Why the restarts cannot outrun the stops
    ///
    /// A child's `ChildTerminated` is delivered to its supervisor's inbox
    /// *before* [`ActorHandle::stop`] returns, and this task sends every
    /// [`RestartDue`] strictly after the last `stop` returned. The supervisor's
    /// inbox is a single-consumer FIFO, so it reads every termination — moving
    /// each slot to [`SlotState::AwaitingBackoff`] — before it reads the first
    /// restart request. The backoff sleep helps nothing here and is not what
    /// makes this safe; the ordering is.
    ///
    /// A child that misses the stop deadline is the one exception. Its
    /// termination may arrive after the restart request, in which case
    /// [`queue_restart`](SupervisionRegistry::queue_restart) refuses the stale
    /// request and the child stays down until the supervisor stops. That is
    /// preferable to waiting on it forever: a child that will not stop must not
    /// be able to hold a group restart open indefinitely.
    ///
    /// # Not tracked with the start tasks
    ///
    /// A start task holds the only handle to a live child, which is why a
    /// shutdown waits for those. This task holds handles to children it is
    /// stopping and then holds nothing at all. Putting it on that tracker would
    /// make every shutdown wait out a backoff whose default ceiling is 30
    /// seconds against a shutdown deadline of 10, so every shutdown with a
    /// pending restart would block for the full 10 and then log an overrun it
    /// did not earn. The rule that falls out is that the tracker is for tasks
    /// holding a live child in order to hand it over, not for every task the
    /// actor spawned.
    ///
    /// It watches the cancellation token instead, so a supervisor going down
    /// does not leave a task sleeping out a backoff for a restart that has
    /// already been abandoned. Even without that the message would simply fail
    /// delivery into a closed inbox; the token just makes it prompt.
    fn spawn_group_restart(
        &self,
        stops: Vec<ActorHandle>,
        dues: Vec<RestartDue>,
        backoff: BackoffDelay,
    ) {
        let supervisor = self.handle.clone();
        let token = self.cancellation_token.clone();
        let delay = backoff.duration();

        tokio::spawn(async move {
            // Reverse start order, each child fully down before the one before
            // it is asked. Sequential rather than concurrent because that
            // ordering is the entire content of the strategy: a later child
            // that depends on an earlier one has to go first.
            for handle in stops {
                stop_group_member(handle).await;
            }

            if let Some(token) = token {
                tokio::select! {
                    () = token.cancelled() => return,
                    () = tokio::time::sleep(delay) => {}
                }
            } else {
                tokio::time::sleep(delay).await;
            }

            // Start order, so an earlier child is back before the ones that
            // may depend on it. Delivery order into a FIFO inbox is what
            // carries that ordering to the supervisor.
            for due in dues {
                let envelope = supervisor.create_envelope(Some(supervisor.reply_address()));
                if let Err(undeliverable) = envelope.try_send(due).await {
                    trace!(
                        "Supervisor {} was gone before a restart came due ({})",
                        supervisor.id(),
                        undeliverable
                    );
                    return;
                }
            }
        });
    }

    /// Queues the restart a timer has just reported due.
    ///
    /// Synchronous, and it starts nothing: the slot goes onto the same
    /// pending-start queue a deferred first start uses, and the top of the next
    /// loop turn hands it to the same start task.
    pub(crate) fn record_restart_due(&mut self, due: &RestartDue) {
        if self
            .supervision
            .queue_restart(due.index, &due.child, due.generation)
        {
            trace!(
                "Actor {} queued a restart of child {} ({})",
                self.id(),
                due.child,
                due.generation
            );
        } else {
            // Not a failure. The slot was retired, restarted by another path,
            // or the supervisor began shutting down and settled it. A timer is
            // the one input that can arrive from a world that no longer exists.
            trace!(
                "Actor {} discarded a stale restart timer for child {} ({})",
                self.id(),
                due.child,
                due.generation
            );
        }
    }

    /// Records the outcome a start task reported.
    ///
    /// Synchronous, like every other piece of registration bookkeeping. The one
    /// case that needs an `await` — stopping a child this actor turns out not to
    /// supervise — is handed to its own task rather than done here, so that a
    /// child slow to stop cannot hold up the message loop.
    pub(crate) fn record_started_child(&mut self, message: &SupervisedChildStarted) {
        match &message.outcome {
            Ok(handle) => {
                let recorded = self.supervision.complete_start(
                    message.index,
                    &message.child,
                    handle.clone(),
                    std::time::Instant::now(),
                );
                if recorded.is_recorded() {
                    trace!(
                        "Actor {} now supervises child {} ({})",
                        self.id(),
                        message.child,
                        recorded
                    );
                    if recorded == StartRecorded::Restart {
                        #[cfg(feature = "ipc")]
                        self.rebind_ipc_names(&message.child, handle);
                    }
                } else {
                    // The slot moved on while the start was in flight, so
                    // nothing is supervising this child. It must not be left
                    // running with nobody holding it.
                    warn!(
                        "Actor {} no longer supervises child {}; stopping the incarnation it started",
                        self.id(),
                        message.child
                    );
                    self.stop_disowned_child(handle.clone());
                }
            }
            Err(error) => {
                warn!(
                    "Actor {} could not start supervised child {}: {}",
                    self.id(),
                    message.child,
                    error
                );
                self.supervision.fail_start(message.index, error);
            }
        }
    }

    /// Points a restarted child's IPC names at its new mailbox.
    ///
    /// A restart keeps the child's [`Ern`](acton_ern::Ern) and replaces its
    /// mailbox, and `ipc_expose` stored a handle *by value*. Nothing updated it,
    /// so before this an actor exposed under a chosen name became unreachable
    /// over IPC from its first restart onward and reported nothing: sends landed
    /// in a queue with no reader.
    #[cfg(feature = "ipc")]
    fn rebind_ipc_names(&self, child: &acton_ern::Ern, fresh: &ActorHandle) {
        let repointed = self.runtime.ipc_rebind(child, fresh);
        if repointed > 0 {
            trace!(
                "Actor {} repointed {} IPC name(s) at the new incarnation of {}",
                self.id(),
                repointed,
                child
            );
        }
    }

    /// Drops the IPC names of every engine-managed child, on the way down.
    ///
    /// The terminal-state sweep cannot reach these. A cascading shutdown sets
    /// the registry `shutting_down`, which makes every termination an expected
    /// stop, which makes `evaluate` return `Ignore`, so no slot ever reaches
    /// `Down` and nothing calls [`forget_ipc_names`](Self::forget_ipc_names).
    /// A supervisor that stops while its runtime keeps going would therefore
    /// leave its children stopped and their names still pointing at dead
    /// mailboxes — the exact condition the terminal sweep exists to remove,
    /// reached by a different road.
    ///
    /// Engine-managed children only, on the same reasoning as the terminal
    /// sweep.
    #[cfg(feature = "ipc")]
    pub(crate) fn forget_children_ipc_names(&self) {
        for child in self.supervision.engine_managed_children() {
            let forgotten = self.runtime.ipc_forget(&child);
            if forgotten > 0 {
                trace!(
                    "Actor {} dropped {} IPC name(s) for child {} while stopping",
                    self.id(),
                    forgotten,
                    child
                );
            }
        }
    }

    /// Stops a child this actor does not supervise, off its own task.
    ///
    /// Tracked rather than detached: a shutdown that did not wait for this would
    /// return while the child was still stopping.
    fn stop_disowned_child(&self, handle: ActorHandle) {
        self.start_tasks.spawn(async move {
            stop_stray_child(handle).await;
        });
    }

    /// Whether this actor has been told to shut down.
    ///
    /// `false` when there is no token, which cannot happen for a started actor
    /// — the message loop asserts it — but is the safe reading either way: an
    /// actor that cannot be cancelled has not been.
    fn is_cancelled(&self) -> bool {
        self.cancellation_token
            .as_ref()
            .is_some_and(tokio_util::sync::CancellationToken::is_cancelled)
    }

    /// Abandons every start that has not finished, on the way down.
    ///
    /// Two different situations, one answer. A queued child was never handed to
    /// anyone and now never will be. A child whose start is in flight may
    /// already exist, but this actor is no longer in a position to take it on;
    /// the task holding its handle finds the inbox closed and stops it. Either
    /// way the caller waiting on the status channel is told the supervisor
    /// stopped, rather than waiting for a start that cannot land.
    pub(crate) fn cancel_unfinished_children(&mut self) {
        let supervisor = self.id.clone();
        let abandoned = self.supervision.cancel_unfinished_starts(&supervisor);
        if abandoned > 0 {
            trace!(
                "Actor {} abandoned {} unfinished child start(s) while stopping",
                self.id(),
                abandoned
            );
        }
    }

    /// Takes the children whose start landed in an inbox nobody will read.
    ///
    /// The last gap in the hand-over. A start task that delivers successfully
    /// has done its job and stops holding the child, but if this actor's loop
    /// has already exited, that message is sitting in a queue that is about to
    /// be dropped — and with it the only handle to a running actor. Draining it
    /// here turns those into children the shutdown stops.
    ///
    /// Sound only because the in-flight starts were awaited first: with every
    /// start task finished, this queue has no writers left, and what is in it
    /// now is all there will ever be.
    ///
    /// Everything else in the inbox is discarded, which is what would have
    /// happened anyway: the receiver is dropped moments later, and every
    /// message it still holds goes with it.
    pub(crate) fn take_late_started_children(&mut self) -> Vec<ActorHandle> {
        let mut handles = Vec::new();

        while let Ok(envelope) = self.inbox.try_recv() {
            if let Some(started) = envelope
                .message
                .as_any()
                .downcast_ref::<SupervisedChildStarted>()
            {
                if let Ok(handle) = &started.outcome {
                    warn!(
                        "Actor {} stopped before adopting child {}; stopping it with the rest",
                        self.id(),
                        started.child
                    );
                    handles.push(handle.clone());
                }
            }
        }

        handles
    }

    /// Stops looking after a child, recording it directly. The child is stopped.
    ///
    /// Crate-internal for the same reason as
    /// [`supervise_with`](Self::supervise_with).
    ///
    /// # Errors
    ///
    /// [`SupervisionError::UnknownChild`] if this actor does not supervise it.
    pub(crate) async fn unsupervise(&mut self, child: &acton_ern::Ern) -> Result<(), SupervisionError> {
        let retired = self.supervision.retire(child);
        match retired {
            Ok(Some(handle)) => {
                let _ = handle.stop().await;
                Ok(())
            }
            Ok(None) => Ok(()),
            Err(error) => Err(error),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
    use std::time::Duration;

    use acton_ern::Ern;

    use super::super::registry::{ChildSlot, SlotState};
    use super::*;
    use crate::actor::{ChildIndex, RestartPolicy};
    use crate::common::{ActonApp, ActorRuntime};

    /// Long enough to be decisive, short enough that a hang is not a break.
    const PATIENCE: Duration = Duration::from_secs(5);

    #[derive(Debug, Default)]
    struct Supervisor;

    /// A spawner that refuses to build anything, and counts the attempts.
    ///
    /// The only way to reach the drain's failure path: `TypedSpawner::spawn` is
    /// infallible today, because `ManagedActor::start` cannot fail.
    #[derive(Debug)]
    struct RefusingSpawner {
        child: Ern,
        attempts: Arc<AtomicUsize>,
    }

    impl ChildSpawner for RefusingSpawner {
        fn child_id(&self) -> &Ern {
            &self.child
        }

        fn restart_policy(&self) -> RestartPolicy {
            RestartPolicy::Permanent
        }

        fn spawn(
            &self,
            _runtime: ActorRuntime,
            _parent: ActorHandle,
        ) -> std::pin::Pin<
            Box<
                dyn std::future::Future<Output = Result<ActorHandle, SupervisionError>>
                    + Send
                    + '_,
            >,
        > {
            self.attempts.fetch_add(1, Ordering::SeqCst);
            Box::pin(async move {
                Err(SupervisionError::ConfigRejected {
                    child: self.child.clone(),
                    reason: "this spawner never builds an actor".to_string(),
                })
            })
        }
    }

    /// A spawner that never finishes building its child.
    ///
    /// Stands in for the thing this whole step is about: a child whose
    /// `before_start` hook takes as long as it likes. Under the old shape the
    /// supervisor awaited this and stopped taking messages.
    #[derive(Debug)]
    struct NeverFinishingSpawner {
        child: Ern,
    }

    impl ChildSpawner for NeverFinishingSpawner {
        fn child_id(&self) -> &Ern {
            &self.child
        }

        fn restart_policy(&self) -> RestartPolicy {
            RestartPolicy::Permanent
        }

        fn spawn(
            &self,
            _runtime: ActorRuntime,
            _parent: ActorHandle,
        ) -> std::pin::Pin<
            Box<
                dyn std::future::Future<Output = Result<ActorHandle, SupervisionError>>
                    + Send
                    + '_,
            >,
        > {
            Box::pin(std::future::pending())
        }
    }

    /// Waits for `flag` to be set, up to a bounded time.
    ///
    /// Stopping a child nobody supervises happens on its own task, so the proof
    /// that it happened arrives a moment later.
    async fn wait_for_flag(flag: &Arc<AtomicBool>) -> bool {
        for _ in 0..300 {
            if flag.load(Ordering::SeqCst) {
                return true;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
        flag.load(Ordering::SeqCst)
    }

    /// A supervisor in the `Started` state whose message loop is *not* running.
    ///
    /// The drain is normally driven by that loop, which owns the actor. Taking
    /// the transition without spawning the loop is what lets a test drive the
    /// drain a turn at a time and look at the registry afterwards.
    fn supervisor(runtime: &mut ActorRuntime) -> ManagedActor<Started, Supervisor> {
        runtime.new_actor::<Supervisor>().into()
    }

    fn queue_refusal(
        actor: &mut ManagedActor<Started, Supervisor>,
        attempts: &Arc<AtomicUsize>,
    ) -> SupervisedChild {
        let child = actor
            .id()
            .add_part("worker")
            .expect("'worker' is a valid Ern part");
        let (status, receiver) = status_channel(&child, None);

        actor
            .supervision
            .register_pending(PendingSlot {
                ern: child.clone(),
                spawner: Arc::new(RefusingSpawner {
                    child: child.clone(),
                    attempts: Arc::clone(attempts),
                }),
                restart_policy: RestartPolicy::Permanent,
                limiter: RestartLimiter::default(),
                status,
            })
            .expect("the first registration of a name succeeds");

        SupervisedChild::new(child, actor.id().clone(), receiver)
    }

    /// Feeds one start task's report back to the supervisor by hand.
    ///
    /// These tests hold a `Started` actor whose message loop is not running, so
    /// nothing is draining the inbox the start task delivers to. This is the
    /// loop's supervision arm, minus the loop.
    async fn deliver_one_report(actor: &mut ManagedActor<Started, Supervisor>) {
        let envelope = tokio::time::timeout(PATIENCE, actor.inbox.recv())
            .await
            .expect("a start task must report back")
            .expect("the inbox is open");
        let started = envelope
            .message
            .as_any()
            .downcast_ref::<SupervisedChildStarted>()
            .expect("a start task sends nothing else")
            .clone();

        actor.record_started_child(&started);
    }

    #[tokio::test]
    async fn a_start_that_fails_reaches_the_caller_instead_of_stranding_it() {
        // **Fails by hanging** if a failed start only logs: the caller is
        // waiting on a status channel whose sender is alive and whose child
        // will never run. The timeout is the assertion.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);
        let attempts = Arc::new(AtomicUsize::new(0));
        let mut child = queue_refusal(&mut actor, &attempts);

        actor.launch_pending_starts();
        deliver_one_report(&mut actor).await;

        let error = tokio::time::timeout(PATIENCE, child.wait_running())
            .await
            .expect("a failed start must end the wait")
            .expect_err("the child was never created");

        assert!(
            matches!(error, SupervisionError::ConfigRejected { .. }),
            "the spawner's own reason must survive: {error}"
        );
        assert_eq!(attempts.load(Ordering::SeqCst), 1);
        assert!(!actor.supervision.has_pending_starts());
        assert_eq!(
            actor.supervision.index_of(child.ern()),
            None,
            "a failed start frees the name for another attempt"
        );
        assert!(
            actor.supervision.live_handles().is_empty(),
            "nothing was created, so there is nothing to stop"
        );
    }

    #[tokio::test]
    async fn launching_a_start_does_not_wait_for_it() {
        // The whole point of the start task. A spawner that never finishes must
        // not stop the supervisor from getting on with its turn, so this call
        // returns while the start is still in flight.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);
        let child = actor
            .id()
            .add_part("slow")
            .expect("'slow' is a valid Ern part");
        let (status, _receiver) = status_channel(&child, None);
        actor
            .supervision
            .register_pending(PendingSlot {
                ern: child.clone(),
                spawner: Arc::new(NeverFinishingSpawner {
                    child: child.clone(),
                }),
                restart_policy: RestartPolicy::Permanent,
                limiter: RestartLimiter::default(),
                status,
            })
            .expect("the first registration of a name succeeds");

        tokio::time::timeout(PATIENCE, async { actor.launch_pending_starts() })
            .await
            .expect("launching must not wait on the spawner");

        assert!(!actor.supervision.has_pending_starts());
        assert_eq!(
            actor.supervision.slot_of(&child).map(ChildSlot::state),
            Some(SlotState::Starting),
            "the slot records that somebody else is building it"
        );
    }

    #[tokio::test]
    async fn a_child_retired_before_its_turn_is_never_created() {
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);
        let attempts = Arc::new(AtomicUsize::new(0));
        let child = queue_refusal(&mut actor, &attempts);

        actor
            .supervision
            .retire(child.ern())
            .expect("the child is supervised, queued or not");
        actor.launch_pending_starts();

        assert_eq!(
            attempts.load(Ordering::SeqCst),
            0,
            "the spawner must not run for a child nobody supervises any more"
        );
        assert!(!actor.supervision.has_pending_starts());
    }

    #[tokio::test]
    async fn a_cancelled_supervisor_launches_nothing_new() {
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);
        let attempts = Arc::new(AtomicUsize::new(0));
        let mut child = queue_refusal(&mut actor, &attempts);
        actor
            .cancellation_token
            .as_ref()
            .expect("a started actor always has a token")
            .cancel();

        actor.launch_pending_starts();

        assert_eq!(
            attempts.load(Ordering::SeqCst),
            0,
            "a supervisor on its way down starts nothing new"
        );
        assert!(
            actor.supervision.has_pending_starts(),
            "what it did not launch stays queued for shutdown to answer"
        );

        // And shutdown answers it, rather than leaving the caller waiting.
        actor.cancel_unfinished_children();
        let error = tokio::time::timeout(PATIENCE, child.wait_running())
            .await
            .expect("shutdown must end the wait")
            .expect_err("the child was never created");
        assert!(
            matches!(error, SupervisionError::SupervisorStopped { .. }),
            "unexpected error: {error}"
        );
        assert!(!actor.supervision.has_pending_starts());
    }

    #[tokio::test]
    async fn shutdown_answers_a_start_that_is_still_in_flight() {
        // The case that only exists because starts run elsewhere: the slot is
        // neither queued nor running, and the caller is waiting on a child that
        // is genuinely being built right now.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);
        let child = actor
            .id()
            .add_part("slow")
            .expect("'slow' is a valid Ern part");
        let (status, receiver) = status_channel(&child, None);
        actor
            .supervision
            .register_pending(PendingSlot {
                ern: child.clone(),
                spawner: Arc::new(NeverFinishingSpawner {
                    child: child.clone(),
                }),
                restart_policy: RestartPolicy::Permanent,
                limiter: RestartLimiter::default(),
                status,
            })
            .expect("the first registration of a name succeeds");
        let mut waiting = SupervisedChild::new(child.clone(), actor.id().clone(), receiver);

        actor.launch_pending_starts();
        assert!(
            actor
                .supervision
                .slot_of(&child)
                .is_some_and(ChildSlot::is_starting),
            "the start really is in flight"
        );

        actor.cancel_unfinished_children();

        let error = tokio::time::timeout(PATIENCE, waiting.wait_running())
            .await
            .expect("a supervisor stopping mid-start must end the wait")
            .expect_err("the child never came up");
        assert!(
            matches!(error, SupervisionError::SupervisorStopped { .. }),
            "unexpected error: {error}"
        );
        assert_eq!(
            actor.supervision.index_of(&child),
            None,
            "and the record is settled rather than left half-started"
        );
    }

    #[tokio::test]
    async fn a_child_this_actor_stopped_supervising_is_stopped_too() {
        // The third way a started child can end up with nobody holding it: the
        // report arrives, but the slot was retired while the start was in
        // flight. Recording it would supervise a child nobody asked for;
        // dropping the handle would leave it running.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);
        let stopped = Arc::new(AtomicBool::new(false));

        let config = ActorConfig::for_supervised_child("worker", actor.handle.clone(), None)
            .expect("a name plus a live parent is a valid child configuration");
        let child_id = config.id();
        let (status, _receiver) = status_channel(&child_id, None);
        let blueprint: Arc<ChildBlueprint<Supervisor>> = {
            let stopped = Arc::clone(&stopped);
            Arc::new(move |child: &mut ManagedActor<Idle, Supervisor>| {
                let stopped = Arc::clone(&stopped);
                child.after_stop(move |_actor| {
                    let stopped = Arc::clone(&stopped);
                    async move {
                        stopped.store(true, Ordering::SeqCst);
                    }
                });
            })
        };

        actor
            .supervision
            .register_pending(PendingSlot {
                ern: child_id.clone(),
                spawner: Arc::new(TypedSpawner::new(config, blueprint)),
                restart_policy: RestartPolicy::Permanent,
                limiter: RestartLimiter::default(),
                status,
            })
            .expect("the first registration of a name succeeds");

        actor.launch_pending_starts();
        actor
            .supervision
            .retire(&child_id)
            .expect("the child is supervised while its start is in flight");

        deliver_one_report(&mut actor).await;

        assert!(
            wait_for_flag(&stopped).await,
            "the child was built, disowned, and then left running"
        );
        assert!(
            actor.supervision.live_handles().is_empty(),
            "and it was not recorded on the way past"
        );
    }

    // ---- whose limiter governs a child ----------------------------------
    //
    // Precedence is asserted here rather than through a running supervisor
    // because this is the level at which it is visible. From outside, two
    // different allowances only diverge once a child has actually failed the
    // smaller number of times, which turns a question about configuration into
    // a test about restart timing.

    /// A supervisor whose own configuration sets a restart allowance.
    fn supervisor_allowing(
        runtime: &mut ActorRuntime,
        max_restarts: u32,
    ) -> ManagedActor<Started, Supervisor> {
        let config = ActorConfig::new(
            Ern::with_root("pool").expect("'pool' is a valid Ern root"),
            None,
        )
        .with_restart_limiter(RestartLimiterConfig {
            max_restarts,
            ..RestartLimiterConfig::default()
        });

        runtime.new_actor_with_config::<Supervisor>(config).into()
    }

    /// Records a child under `actor`, optionally with an allowance of its own.
    fn queue_child(
        actor: &mut ManagedActor<Started, Supervisor>,
        name: &str,
        max_restarts: Option<u32>,
    ) -> Ern {
        let mut config = ActorConfig::for_supervised_child(name, actor.handle.clone(), None)
            .expect("a name plus a live parent is a valid child configuration");
        if let Some(max_restarts) = max_restarts {
            config = config.with_restart_limiter(RestartLimiterConfig {
                max_restarts,
                ..RestartLimiterConfig::default()
            });
        }
        let child_id = config.id();
        actor
            .supervise_deferred(config, |_child: &mut ManagedActor<Idle, Supervisor>| {})
            .expect("the first registration of a name succeeds");
        child_id
    }

    /// The allowance the supervisor actually recorded for a child.
    fn recorded_allowance(actor: &mut ManagedActor<Started, Supervisor>, child: &Ern) -> u32 {
        actor
            .supervision
            .slot_of_mut(child)
            .expect("the child is supervised")
            .limiter_mut()
            .stats()
            .max_restarts
    }

    #[tokio::test]
    async fn a_child_that_sets_no_allowance_inherits_its_supervisors() {
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor_allowing(&mut runtime, 9);

        let child = queue_child(&mut actor, "worker", None);

        assert_eq!(recorded_allowance(&mut actor, &child), 9);
    }

    #[tokio::test]
    async fn a_childs_own_allowance_overrides_its_supervisors() {
        // The consequence of the ruling, stated plainly: a child can decline
        // its supervisor's policy. Accepted deliberately — a child that knows
        // it is expensive to rebuild is the thing that knows it — so this is a
        // test of intended behaviour, not a loophole somebody left open.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor_allowing(&mut runtime, 2);

        let child = queue_child(&mut actor, "worker", Some(11));

        assert_eq!(
            recorded_allowance(&mut actor, &child),
            11,
            "the child's own setting wins where both are set"
        );
    }

    #[tokio::test]
    async fn a_child_burning_through_its_allowance_leaves_its_siblings_untouched() {
        // What per-slot limiter *instances* buy, and the one most likely to
        // regress silently if a limiter is ever hoisted onto the supervisor:
        // one noisy child must not spend a sibling's budget, or a child that
        // has never failed gets escalated for somebody else's crashes.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor_allowing(&mut runtime, 3);

        let noisy = queue_child(&mut actor, "noisy", None);
        let quiet = queue_child(&mut actor, "quiet", None);

        // Spend the noisy child's entire allowance.
        {
            let limiter = actor
                .supervision
                .slot_of_mut(&noisy)
                .expect("the child is supervised")
                .limiter_mut();
            for _ in 0..3 {
                limiter.can_restart().expect("within the allowance");
                let _ = limiter.record_restart();
            }
            assert!(
                limiter.can_restart().is_err(),
                "the noisy child is out of restarts"
            );
        }

        let quiet_limiter = actor
            .supervision
            .slot_of_mut(&quiet)
            .expect("the child is supervised")
            .limiter_mut();
        assert_eq!(
            quiet_limiter.restarts_in_window(),
            0,
            "a sibling's crashes are not charged to this child"
        );
        assert!(
            quiet_limiter.can_restart().is_ok(),
            "and it keeps its full allowance"
        );
    }

    // ---- the double-restart firewall -------------------------------------

    /// Registers a child the way the legacy `supervise()` path does: running,
    /// with **no blueprint**, so its supervisor has no recipe for rebuilding it.
    fn adopt_legacy_child(
        actor: &mut ManagedActor<Started, Supervisor>,
        name: &str,
    ) -> (Ern, watch::Receiver<SupervisionStatus>) {
        let child = actor
            .id()
            .add_part(name)
            .expect("a short name is a valid Ern part");
        let (status, receiver) = status_channel(&child, None);

        actor
            .supervision
            .register(NewSlot {
                ern: child.clone(),
                handle: handle_for(&child),
                spawner: None,
                restart_policy: RestartPolicy::Permanent,
                limiter: RestartLimiter::default(),
                status,
            })
            .expect("the first registration of a name succeeds");

        (child, receiver)
    }

    /// A real `ActorHandle` with a real mailbox, built without a runtime.
    fn handle_for(id: &Ern) -> ActorHandle {
        let (outbox, _inbox) = tokio::sync::mpsc::channel(8);
        ActorHandle::new(id.clone(), outbox)
    }

    #[tokio::test]
    async fn a_child_with_no_blueprint_is_left_down_without_spending_an_allowance() {
        // The firewall that makes the whole engine safe to ship: a child adopted
        // through the legacy `supervise()` path has no blueprint, so it is never
        // restarted, and no program written against a released version can have
        // a child restarted twice.
        //
        // **The load-bearing assertion here is the limiter, not the state.**
        // `evaluate` returns `Forget` for a blueprint-less slot at check 2,
        // *before* the limiter is consulted. Delete that check and the outcome
        // is still `Forget` — `plan_restart` filters on `restartable` too, so
        // the plan comes back empty — and the slot still reaches `Down`. What
        // changes is that the child is charged a restart it can never use, and
        // enough of those escalate a child the supervisor was never going to
        // rebuild. Measured: with check 2 removed, the state assertion below
        // still passes and the limiter assertion fails.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);
        let (child, receiver) = adopt_legacy_child(&mut actor, "legacy");

        // `Permanent` + `Normal` is the combination that *would* warrant a
        // restart, so the blueprint is the only thing standing in the way.
        actor.record_child_terminated(&ChildTerminated::new(
            child.clone(),
            crate::actor::TerminationReason::Normal,
            RestartPolicy::Permanent,
        ));

        let slot = actor
            .supervision
            .slot_of_mut(&child)
            .expect("the child is still recorded");
        assert_eq!(
            slot.state(),
            SlotState::Down,
            "a child nobody can rebuild is left down"
        );
        assert_eq!(
            slot.limiter_mut().restarts_in_window(),
            0,
            "and is not charged for a restart that was never going to happen"
        );

        assert_eq!(receiver.borrow().state(), SupervisionState::Down);
        assert!(
            !actor.supervision.has_pending_starts(),
            "nothing was queued to rebuild it"
        );
    }

    #[tokio::test]
    async fn a_child_with_a_blueprint_is_restarted_where_a_legacy_one_is_not() {
        // The other side of the same predicate, so the test above is shown to
        // be about the blueprint rather than about something incidental to the
        // fixture. Identical notification, identical policy, one difference.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);

        let engine_managed = queue_child(&mut actor, "worker", None);
        actor.launch_pending_starts();
        let handle = handle_for(&engine_managed);
        assert!(actor
            .supervision
            .complete_start(
                ChildIndex::new(0),
                &engine_managed,
                handle,
                std::time::Instant::now(),
            )
            .is_recorded());

        actor.record_child_terminated(&ChildTerminated::new(
            engine_managed.clone(),
            crate::actor::TerminationReason::Normal,
            RestartPolicy::Permanent,
        ));

        let slot = actor
            .supervision
            .slot_of_mut(&engine_managed)
            .expect("the child is still recorded");
        assert_eq!(
            slot.state(),
            SlotState::AwaitingBackoff,
            "a child with a blueprint is on its way back"
        );
        assert_eq!(
            slot.limiter_mut().restarts_in_window(),
            1,
            "and this one really is charged for it"
        );
    }

    // ---- group restarts ---------------------------------------------------

    /// A supervisor with a group strategy, one child it can rebuild at index 0
    /// and one adopted child it cannot at index 1, both running.
    ///
    /// The shape that makes `then_restart` observable: the adopted child is in
    /// the plan's `stop` list and not in its `restart` list, which is the only
    /// way that flag is ever `false`.
    fn group_of_one_managed_and_one_adopted(
        actor: &mut ManagedActor<Started, Supervisor>,
    ) -> (Ern, Ern) {
        actor.supervision_strategy = super::super::SupervisionStrategy::OneForAll;

        let managed = queue_child(actor, "worker", None);
        actor.launch_pending_starts();
        assert!(actor
            .supervision
            .complete_start(
                ChildIndex::new(0),
                &managed,
                handle_for(&managed),
                std::time::Instant::now(),
            )
            .is_recorded());

        let (adopted, _status) = adopt_legacy_child(actor, "adopted");

        (managed, adopted)
    }

    #[tokio::test]
    async fn a_group_restart_marks_a_sibling_it_cannot_rebuild_as_not_coming_back() {
        // `then_restart` is read off the plan, and this is the only case where
        // the two lists disagree. Setting it unconditionally to `true` leaves
        // this slot in `ExpectedStop { then_restart: true }`, which becomes
        // `AwaitingBackoff` when the stop lands and then waits forever for a
        // restart the plan never asked for — a caller watching it is told
        // `Restarting` for the rest of the supervisor's life.
        //
        // Measured as slot state rather than as "was it rebuilt", because it is
        // *not* rebuilt either way: no `RestartDue` is ever sent for a child
        // outside the plan's restart list. The resting state is the whole
        // observable difference.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);
        let (managed, adopted) = group_of_one_managed_and_one_adopted(&mut actor);

        actor.record_child_terminated(&ChildTerminated::new(
            managed,
            crate::actor::TerminationReason::Normal,
            RestartPolicy::Permanent,
        ));

        assert_eq!(
            actor
                .supervision
                .slot_of(&adopted)
                .expect("the adopted child is still recorded")
                .state(),
            SlotState::ExpectedStop {
                then_restart: false
            },
            "a sibling the supervisor holds no blueprint for is stopped and not promised back"
        );
    }

    #[tokio::test]
    async fn a_group_restart_marks_a_sibling_it_can_rebuild_as_coming_back() {
        // The other side of the same read, so the test above is shown to be
        // about the plan rather than about something incidental to the fixture.
        // Identical group, identical failure, one difference: this sibling has
        // a blueprint, so it is in both lists.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);
        actor.supervision_strategy = super::super::SupervisionStrategy::OneForAll;

        let failed = queue_child(&mut actor, "worker", None);
        let sibling = queue_child(&mut actor, "sibling", None);
        actor.launch_pending_starts();
        for (index, child) in [(0, &failed), (1, &sibling)] {
            assert!(actor
                .supervision
                .complete_start(
                    ChildIndex::new(index),
                    child,
                    handle_for(child),
                    std::time::Instant::now(),
                )
                .is_recorded());
        }

        actor.record_child_terminated(&ChildTerminated::new(
            failed,
            crate::actor::TerminationReason::Normal,
            RestartPolicy::Permanent,
        ));

        assert_eq!(
            actor
                .supervision
                .slot_of(&sibling)
                .expect("the sibling is still recorded")
                .state(),
            SlotState::ExpectedStop { then_restart: true },
        );
    }

    #[tokio::test]
    async fn a_landed_group_stop_moves_a_sibling_to_the_state_its_flag_names() {
        // The engine's half of the crux. `evaluate` reports which half of the
        // group a landed stop belongs to; this is what that report is *for*.
        // A child on its way back has to reach `AwaitingBackoff`, because that
        // is the only state `queue_restart` will accept a restart from — land
        // it anywhere else and the group's own restart is refused as stale.
        for (then_restart, expected) in [
            (true, SlotState::AwaitingBackoff),
            (false, SlotState::Down),
        ] {
            let mut runtime = ActonApp::launch_async().await;
            let mut actor = supervisor(&mut runtime);
            let (child, _receiver) = adopt_legacy_child(&mut actor, "sibling");
            actor
                .supervision
                .slot_of_mut(&child)
                .expect("the child is supervised")
                .set_state(SlotState::ExpectedStop { then_restart });

            actor.record_child_terminated(&ChildTerminated::new(
                child.clone(),
                crate::actor::TerminationReason::Normal,
                RestartPolicy::Permanent,
            ));

            assert_eq!(
                actor
                    .supervision
                    .slot_of(&child)
                    .expect("the child is still recorded")
                    .state(),
                expected,
                "a group stop with then_restart={then_restart} must come to rest in {expected}"
            );
        }
    }

    #[tokio::test]
    async fn a_supervisor_shutting_down_abandons_a_group_restart_instead_of_driving_it() {
        // A shutdown makes every termination expected, and that has to outrank
        // the group. Driving the group here would have the supervisor rebuild,
        // during its own shutdown, children it is in the middle of stopping.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);
        let (child, _receiver) = adopt_legacy_child(&mut actor, "sibling");
        actor
            .supervision
            .slot_of_mut(&child)
            .expect("the child is supervised")
            .set_state(SlotState::ExpectedStop { then_restart: true });
        actor.supervision.begin_shutdown();

        actor.record_child_terminated(&ChildTerminated::new(
            child.clone(),
            crate::actor::TerminationReason::Normal,
            RestartPolicy::Permanent,
        ));

        assert_eq!(
            actor
                .supervision
                .slot_of(&child)
                .expect("the child is still recorded")
                .state(),
            SlotState::ExpectedStop { then_restart: true },
            "a shutdown leaves the slot alone rather than parking it for a restart \
             that will never be performed"
        );
        assert!(
            !actor.supervision.has_pending_starts(),
            "and nothing is queued to come back"
        );
    }

    #[tokio::test]
    async fn a_report_that_lands_after_the_loop_stops_is_not_lost() {
        // The narrow window the drain exists for: delivery succeeded, so the
        // start task has let go of the child, but the loop is already over and
        // nothing will ever dispatch that message.
        let mut runtime = ActonApp::launch_async().await;
        let mut actor = supervisor(&mut runtime);
        let child = runtime.new_actor::<Supervisor>().start().await;
        let envelope = actor
            .handle
            .create_envelope(Some(actor.handle.reply_address()));
        envelope
            .try_send(SupervisedChildStarted {
                child: child.id(),
                index: ChildIndex::new(0),
                outcome: Ok(child.clone()),
            })
            .await
            .expect("the inbox is open");

        let late = actor.take_late_started_children();

        assert_eq!(late.len(), 1, "the child in the undelivered report");
        assert_eq!(late[0].id(), child.id());
        assert!(
            actor
                .shutdown_child_handles(late)
                .iter()
                .any(|handle| handle.id() == child.id()),
            "and it joins the children the shutdown stops"
        );
    }
}