asupersync 0.3.0

Spec-first, cancel-correct, capability-secure async runtime for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
//! Actor abstraction for region-owned, message-driven concurrency.
//!
//! Actors in Asupersync are region-owned tasks that process messages from a
//! bounded mailbox. They integrate with the runtime's structured concurrency
//! model:
//!
//! - **Region-owned**: Actors are spawned within a region and cannot outlive it.
//! - **Cancel-safe mailbox**: Messages use the two-phase reserve/send pattern.
//! - **Lifecycle hooks**: `on_start` and `on_stop` for initialization and cleanup.
//!
//! # Example
//!
//! ```ignore
//! struct Counter {
//!     count: u64,
//! }
//!
//! impl Actor for Counter {
//!     type Message = u64;
//!
//!     async fn handle(&mut self, _cx: &Cx, msg: u64) {
//!         self.count += msg;
//!     }
//! }
//!
//! // In a scope:
//! let (handle, stored) = scope.spawn_actor(
//!     &mut state, &cx, Counter { count: 0 }, 32,
//! )?;
//! state.store_spawned_task(handle.task_id(), stored);
//!
//! // Send messages:
//! handle.send(&cx, 5).await?;
//! handle.send(&cx, 10).await?;
//!
//! // Stop the actor:
//! handle.stop();
//! let result = (&mut handle).join(&cx).await?;
//! assert_eq!(result.count, 15);
//! ```

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, Ordering};
use std::time::Duration;

use crate::channel::mpsc;
use crate::channel::mpsc::SendError;
use crate::cx::Cx;
use crate::runtime::{JoinError, SpawnError};
use crate::types::{CxInner, Outcome, RegionId, TaskId, Time};

/// Unique identifier for an actor.
///
/// For now this is a thin wrapper around the actor task's `TaskId`, which already
/// provides arena + generation semantics. Keeping a distinct type avoids mixing
/// actor IDs with generic tasks at call sites.
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ActorId(TaskId);

impl ActorId {
    /// Create an actor ID from a task ID.
    #[must_use]
    #[inline]
    pub const fn from_task(task_id: TaskId) -> Self {
        Self(task_id)
    }

    /// Returns the underlying task ID.
    #[must_use]
    #[inline]
    pub const fn task_id(self) -> TaskId {
        self.0
    }
}

impl std::fmt::Debug for ActorId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("ActorId").field(&self.0).finish()
    }
}

impl std::fmt::Display for ActorId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Preserve the compact, deterministic formatting of TaskId while keeping
        // a distinct type at the API level.
        write!(f, "{}", self.0)
    }
}

/// Lifecycle state for an actor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActorState {
    /// Actor constructed but not yet started.
    Created,
    /// Actor is running and processing messages.
    Running,
    /// Actor is stopping (cancellation requested / mailbox closed).
    Stopping,
    /// Actor has stopped and will not process further messages.
    Stopped,
}

#[derive(Debug)]
struct ActorStateCell {
    state: AtomicU8,
}

impl ActorStateCell {
    #[inline]
    fn new(state: ActorState) -> Self {
        Self {
            state: AtomicU8::new(Self::encode(state)),
        }
    }

    #[inline]
    fn load(&self) -> ActorState {
        Self::decode(self.state.load(Ordering::Acquire))
    }

    #[inline]
    fn store(&self, state: ActorState) {
        self.state.store(Self::encode(state), Ordering::Release);
    }

    #[inline]
    const fn encode(state: ActorState) -> u8 {
        match state {
            ActorState::Created => 0,
            ActorState::Running => 1,
            ActorState::Stopping => 2,
            ActorState::Stopped => 3,
        }
    }

    #[inline]
    const fn decode(value: u8) -> ActorState {
        match value {
            0 => ActorState::Created,
            1 => ActorState::Running,
            2 => ActorState::Stopping,
            _ => ActorState::Stopped,
        }
    }
}

/// Internal runtime state for an actor.
///
/// This is intentionally lightweight and non-opinionated; higher-level actor
/// features (mailbox policies, supervision trees, etc.) can extend this.
struct ActorCell<M> {
    mailbox: mpsc::Receiver<M>,
    state: Arc<ActorStateCell>,
}

/// A message-driven actor that processes messages from a bounded mailbox.
///
/// Actors are the unit of stateful, message-driven concurrency. Each actor:
/// - Owns mutable state (`self`)
/// - Receives messages sequentially (no data races)
/// - Runs inside a region (structured lifetime)
///
/// # Cancel Safety
///
/// When an actor is cancelled (region close, explicit abort), the runtime:
/// 1. Closes the mailbox (no new messages accepted)
/// 2. Calls `on_stop` for cleanup
/// 3. Returns the actor state to the caller via `ActorHandle::join`
pub trait Actor: Send + 'static {
    /// The type of messages this actor can receive.
    type Message: Send + 'static;

    /// Called once when the actor starts, before processing any messages.
    ///
    /// Use this for initialization that requires the capability context.
    /// The default implementation does nothing.
    fn on_start(&mut self, _cx: &Cx) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
        Box::pin(async {})
    }

    /// Handle a single message.
    ///
    /// This is called sequentially for each message in the mailbox.
    /// The actor has exclusive access to its state during handling.
    fn handle(
        &mut self,
        cx: &Cx,
        msg: Self::Message,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>>;

    /// Called once when the actor is stopping, after the mailbox is drained.
    ///
    /// Use this for cleanup. The default implementation does nothing.
    fn on_stop(&mut self, _cx: &Cx) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
        Box::pin(async {})
    }
}

/// Handle to a running actor, used to send messages and manage its lifecycle.
///
/// The handle owns:
/// - A sender for the actor's mailbox
/// - A task handle for join/abort operations
///
/// When the handle is dropped, the mailbox sender is dropped, which causes
/// the actor loop to exit after processing remaining messages.
#[derive(Debug)]
pub struct ActorHandle<A: Actor> {
    actor_id: ActorId,
    sender: mpsc::Sender<A::Message>,
    state: Arc<ActorStateCell>,
    task_id: TaskId,
    receiver: crate::channel::oneshot::Receiver<Result<A, JoinError>>,
    inner: std::sync::Weak<parking_lot::RwLock<CxInner>>,
    completed: bool,
}

impl<A: Actor> ActorHandle<A> {
    /// Send a message to the actor using two-phase reserve/send.
    ///
    /// Returns an error if the actor has stopped or the mailbox is full.
    pub async fn send(&self, cx: &Cx, msg: A::Message) -> Result<(), SendError<A::Message>> {
        self.sender.send(cx, msg).await
    }

    /// Try to send a message without blocking.
    ///
    /// Returns `Err(SendError::Full(msg))` if the mailbox is full, or
    /// `Err(SendError::Disconnected(msg))` if the actor has stopped.
    pub fn try_send(&self, msg: A::Message) -> Result<(), SendError<A::Message>> {
        self.sender.try_send(msg)
    }

    /// Returns a lightweight, clonable reference for sending messages.
    #[must_use]
    pub fn sender(&self) -> ActorRef<A::Message> {
        ActorRef {
            actor_id: self.actor_id,
            sender: self.sender.clone(),
            state: Arc::clone(&self.state),
        }
    }

    /// Returns the actor's unique identifier.
    #[must_use]
    pub const fn actor_id(&self) -> ActorId {
        self.actor_id
    }

    /// Returns the task ID of the actor's underlying task.
    #[must_use]
    pub fn task_id(&self) -> crate::types::TaskId {
        self.task_id
    }

    /// Signals the actor to stop gracefully.
    ///
    /// Sets the actor state to `Stopping`. The actor will continue processing
    /// any currently buffered messages in its mailbox. Once the mailbox is
    /// empty, the actor loop will exit and call `on_stop` before returning.
    ///
    /// Unlike [`abort`](Self::abort), this does NOT immediately request
    /// cancellation, allowing the actor to drain pending work. The mailbox is
    /// sealed immediately so new sends fail fast instead of extending shutdown.
    pub fn stop(&self) {
        self.state.store(ActorState::Stopping);
        self.sender.close_receiver();
    }

    /// Returns true if the actor has finished.
    #[must_use]
    pub fn is_finished(&self) -> bool {
        self.completed || self.receiver.is_ready() || self.receiver.is_closed()
    }

    /// Wait for the actor to finish and return its final state.
    ///
    /// Blocks until the actor loop completes (mailbox closed or cancelled),
    /// then returns the actor's final state or a join error.
    pub fn join<'a>(&'a mut self, _cx: &'a Cx) -> ActorJoinFuture<'a, A> {
        let cx_inner = self.inner.clone();
        let receiver = &mut self.receiver;
        let terminal_state = &mut self.completed;
        ActorJoinFuture {
            inner: receiver.recv_uninterruptible(),
            cx_inner,
            sender: self.sender.clone(),
            state: Arc::clone(&self.state),
            terminal_state,
            drop_abort_defused: false,
        }
    }

    /// Request the actor to stop immediately by aborting its task.
    ///
    /// Sets `cancel_requested` on the actor's context, causing the actor loop
    /// to exit at the next cancellation check point. The actor will call
    /// `on_stop` before returning.
    pub fn abort(&self) {
        self.state.store(ActorState::Stopping);
        self.sender.close_receiver();
        if let Some(inner) = self.inner.upgrade() {
            let cancel_waker = {
                let mut guard = inner.write();
                guard.cancel_requested = true;
                guard
                    .fast_cancel
                    .store(true, std::sync::atomic::Ordering::Release);
                if guard.cancel_reason.is_none() {
                    guard.cancel_reason = Some(crate::types::CancelReason::user("actor aborted"));
                }
                guard.cancel_waker.clone()
            };
            if let Some(waker) = cancel_waker {
                waker.wake_by_ref();
            }
        }
    }
}

/// Future returned by [`ActorHandle::join`].
///
/// This future aborts the actor if dropped before completion, ensuring correct
/// cleanup in races and timeouts.
pub struct ActorJoinFuture<'a, A: Actor> {
    inner: crate::channel::oneshot::RecvUninterruptibleFuture<'a, Result<A, JoinError>>,
    cx_inner: std::sync::Weak<parking_lot::RwLock<CxInner>>,
    sender: mpsc::Sender<A::Message>,
    state: Arc<ActorStateCell>,
    terminal_state: &'a mut bool,
    drop_abort_defused: bool,
}

impl<A: Actor> ActorJoinFuture<'_, A> {
    fn closed_reason(&self) -> crate::types::CancelReason {
        self.cx_inner
            .upgrade()
            .and_then(|inner| inner.read().cancel_reason.clone())
            .unwrap_or_else(|| crate::types::CancelReason::user("join channel closed"))
    }

    fn abort(&self) {
        self.state.store(ActorState::Stopping);
        self.sender.close_receiver();
        if let Some(inner) = self.cx_inner.upgrade() {
            let cancel_waker = {
                let mut guard = inner.write();
                guard.cancel_requested = true;
                guard
                    .fast_cancel
                    .store(true, std::sync::atomic::Ordering::Release);
                if guard.cancel_reason.is_none() {
                    guard.cancel_reason = Some(crate::types::CancelReason::user("actor aborted"));
                }
                guard.cancel_waker.clone()
            };
            if let Some(waker) = cancel_waker {
                waker.wake_by_ref();
            }
        }
    }
}

impl<A: Actor> std::future::Future for ActorJoinFuture<'_, A> {
    type Output = Result<A, JoinError>;

    fn poll(
        mut self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        let this = &mut *self;
        if *this.terminal_state {
            return std::task::Poll::Ready(Err(JoinError::PolledAfterCompletion));
        }

        match Pin::new(&mut this.inner).poll(cx) {
            std::task::Poll::Ready(Ok(res)) => {
                *this.terminal_state = true;
                this.drop_abort_defused = true;
                std::task::Poll::Ready(res)
            }
            std::task::Poll::Ready(Err(crate::channel::oneshot::RecvError::Closed)) => {
                *this.terminal_state = true;
                this.drop_abort_defused = true;
                let reason = this.closed_reason();
                std::task::Poll::Ready(Err(JoinError::Cancelled(reason)))
            }
            std::task::Poll::Ready(Err(crate::channel::oneshot::RecvError::Cancelled)) => {
                unreachable!("RecvUninterruptibleFuture cannot return Cancelled");
            }
            std::task::Poll::Ready(Err(
                crate::channel::oneshot::RecvError::PolledAfterCompletion,
            )) => {
                unreachable!(
                    "JoinFuture guards repolls before polling the inner oneshot recv future"
                )
            }
            std::task::Poll::Pending => std::task::Poll::Pending,
        }
    }
}

impl<A: Actor> Drop for ActorJoinFuture<'_, A> {
    fn drop(&mut self) {
        if !*self.terminal_state && !self.drop_abort_defused {
            if self.inner.receiver_finished() {
                return;
            }
            self.abort();
        }
    }
}

/// A lightweight, clonable reference to an actor's mailbox.
///
/// Use this to send messages to an actor from multiple locations without
/// needing to share the `ActorHandle`.
#[derive(Debug)]
pub struct ActorRef<M> {
    actor_id: ActorId,
    sender: mpsc::Sender<M>,
    state: Arc<ActorStateCell>,
}

// Manual Clone impl without requiring M: Clone, since all fields are
// independently clonable (ActorId is Copy, Sender<M> clones without M: Clone,
// and Arc is always Clone).
impl<M> Clone for ActorRef<M> {
    fn clone(&self) -> Self {
        Self {
            actor_id: self.actor_id,
            sender: self.sender.clone(),
            state: Arc::clone(&self.state),
        }
    }
}

impl<M: Send + 'static> ActorRef<M> {
    /// Send a message to the actor.
    pub async fn send(&self, cx: &Cx, msg: M) -> Result<(), SendError<M>> {
        self.sender.send(cx, msg).await
    }

    /// Reserve a slot in the mailbox (two-phase send: reserve -> commit).
    #[must_use]
    pub fn reserve<'a>(&'a self, cx: &'a Cx) -> mpsc::Reserve<'a, M> {
        self.sender.reserve(cx)
    }

    /// Try to send a message without blocking.
    pub fn try_send(&self, msg: M) -> Result<(), SendError<M>> {
        self.sender.try_send(msg)
    }

    /// Returns true if the actor has stopped (mailbox closed).
    #[must_use]
    pub fn is_closed(&self) -> bool {
        self.sender.is_closed()
    }

    /// Returns true if the actor is still alive (not fully stopped).
    ///
    /// Note: This is best-effort. The definitive shutdown signal is `ActorHandle::join()`.
    #[must_use]
    pub fn is_alive(&self) -> bool {
        self.state.load() != ActorState::Stopped
    }

    /// Returns the actor's unique identifier.
    #[must_use]
    pub const fn actor_id(&self) -> ActorId {
        self.actor_id
    }
}

// ============================================================================
// ActorContext: Actor-Specific Capability Extension
// ============================================================================

/// Configuration for actor mailbox.
#[derive(Debug, Clone, Copy)]
pub struct MailboxConfig {
    /// Maximum number of messages the mailbox can hold.
    pub capacity: usize,
    /// Whether to use backpressure (block senders) or drop oldest messages.
    pub backpressure: bool,
}

impl Default for MailboxConfig {
    fn default() -> Self {
        Self {
            capacity: DEFAULT_MAILBOX_CAPACITY,
            backpressure: true,
        }
    }
}

impl MailboxConfig {
    /// Create a mailbox config with the specified capacity.
    #[must_use]
    pub const fn with_capacity(capacity: usize) -> Self {
        Self {
            capacity,
            backpressure: true,
        }
    }
}

/// Messages that can be sent to a supervisor about child lifecycle events.
#[derive(Debug, Clone)]
pub enum SupervisorMessage {
    /// A supervised child actor has failed.
    ChildFailed {
        /// The ID of the failed child.
        child_id: ActorId,
        /// Description of the failure.
        reason: String,
    },
    /// A supervised child actor has stopped normally.
    ChildStopped {
        /// The ID of the stopped child.
        child_id: ActorId,
    },
}

/// Actor-specific capability context extending [`Cx`].
///
/// Provides actors with access to:
/// - Self-reference for tell() patterns
/// - Child management for supervision
/// - Self-termination controls
/// - Parent reference for escalation
///
/// All [`Cx`] methods are available through [`Deref`].
///
/// # Example
///
/// ```ignore
/// async fn handle(&mut self, ctx: &ActorContext<'_, MyMessage>, msg: MyMessage) {
///     // Access Cx methods directly
///     if ctx.is_cancel_requested() {
///         return;
///     }
///
///     // Use actor-specific capabilities
///     let my_id = ctx.self_actor_id();
///     ctx.trace("handling message");
/// }
/// ```
pub struct ActorContext<'a, M: Send + 'static> {
    /// Underlying capability context.
    cx: &'a Cx,
    /// Reference to this actor's mailbox sender.
    self_ref: ActorRef<M>,
    /// This actor's unique identifier.
    actor_id: ActorId,
    /// Parent supervisor reference (None for root actors).
    parent: Option<ActorRef<SupervisorMessage>>,
    /// IDs of children currently supervised by this actor.
    children: Vec<ActorId>,
    /// Whether this actor has been requested to stop.
    stopping: bool,
}

#[allow(clippy::elidable_lifetime_names)]
impl<'a, M: Send + 'static> ActorContext<'a, M> {
    /// Create a new actor context.
    ///
    /// This is typically called internally by the actor runtime.
    #[must_use]
    pub fn new(
        cx: &'a Cx,
        self_ref: ActorRef<M>,
        actor_id: ActorId,
        parent: Option<ActorRef<SupervisorMessage>>,
    ) -> Self {
        Self {
            cx,
            self_ref,
            actor_id,
            parent,
            children: Vec::new(),
            stopping: false,
        }
    }

    /// Returns this actor's unique identifier.
    ///
    /// Unlike `self_ref()`, this avoids cloning the actor reference and is
    /// useful for logging, debugging, or identity comparisons.
    #[must_use]
    pub const fn self_actor_id(&self) -> ActorId {
        self.actor_id
    }

    /// Returns the underlying actor ID (alias for `self_actor_id`).
    #[must_use]
    pub const fn actor_id(&self) -> ActorId {
        self.actor_id
    }

    // ========================================================================
    // Child Management Methods
    // ========================================================================

    /// Register a child actor as supervised by this actor.
    ///
    /// Called internally when spawning supervised children.
    pub fn register_child(&mut self, child_id: ActorId) {
        self.children.push(child_id);
    }

    /// Unregister a child actor (after it has stopped).
    ///
    /// Returns true if the child was found and removed.
    pub fn unregister_child(&mut self, child_id: ActorId) -> bool {
        if let Some(pos) = self.children.iter().position(|&id| id == child_id) {
            self.children.swap_remove(pos);
            true
        } else {
            false
        }
    }

    /// Returns the list of currently supervised child actor IDs.
    #[must_use]
    pub fn children(&self) -> &[ActorId] {
        &self.children
    }

    /// Returns true if this actor has any supervised children.
    #[must_use]
    pub fn has_children(&self) -> bool {
        !self.children.is_empty()
    }

    /// Returns the number of supervised children.
    #[must_use]
    pub fn child_count(&self) -> usize {
        self.children.len()
    }

    // ========================================================================
    // Self-Termination Methods
    // ========================================================================

    /// Request this actor to stop gracefully.
    ///
    /// Sets the stopping flag. The actor loop will exit after the current
    /// message is processed and the mailbox is drained.
    pub fn stop_self(&mut self) {
        self.stopping = true;
    }

    /// Returns true if this actor has been requested to stop.
    #[must_use]
    pub fn is_stopping(&self) -> bool {
        self.stopping
    }

    // ========================================================================
    // Parent Interaction Methods
    // ========================================================================

    /// Returns a reference to the parent supervisor, if any.
    ///
    /// Root actors spawned without supervision return `None`.
    #[must_use]
    pub fn parent(&self) -> Option<&ActorRef<SupervisorMessage>> {
        self.parent.as_ref()
    }

    /// Returns true if this actor has a parent supervisor.
    #[must_use]
    pub fn has_parent(&self) -> bool {
        self.parent.is_some()
    }

    /// Escalate an error to the parent supervisor.
    ///
    /// Sends a `SupervisorMessage::ChildFailed` to the parent if one exists.
    /// Does nothing if this is a root actor.
    pub async fn escalate(&self, reason: String) {
        if let Some(parent) = &self.parent {
            let msg = SupervisorMessage::ChildFailed {
                child_id: self.actor_id,
                reason,
            };
            // Best-effort: ignore send failures (parent may have stopped)
            let _ = parent.send(self.cx, msg).await;
        }
    }

    // ========================================================================
    // Cx Delegation Methods
    // ========================================================================

    /// Check for cancellation and return early if requested.
    ///
    /// This is a convenience method that checks both actor stopping
    /// and Cx cancellation.
    #[allow(clippy::result_large_err)]
    pub fn checkpoint(&self) -> Result<(), crate::error::Error> {
        if self.stopping {
            let reason = crate::types::CancelReason::user("actor stopping")
                .with_region(self.cx.region_id())
                .with_task(self.cx.task_id());
            return Err(crate::error::Error::cancelled(&reason));
        }
        self.cx.checkpoint()
    }

    /// Returns true if cancellation has been requested.
    ///
    /// Checks both actor stopping flag and Cx cancellation.
    #[must_use]
    pub fn is_cancel_requested(&self) -> bool {
        self.stopping || self.cx.checkpoint().is_err()
    }

    /// Returns the current budget.
    #[must_use]
    pub fn budget(&self) -> crate::types::Budget {
        self.cx.budget()
    }

    /// Returns the deadline from the budget, if set.
    #[must_use]
    pub fn deadline(&self) -> Option<Time> {
        self.cx.budget().deadline
    }

    /// Emit a trace event.
    pub fn trace(&self, event: &str) {
        self.cx.trace(event);
    }

    /// Returns a clonable reference to this actor's mailbox.
    ///
    /// Use this to give other actors a way to send messages to this actor.
    /// The `ActorRef<M>` type is always Clone regardless of whether M is Clone.
    #[must_use]
    pub fn self_ref(&self) -> ActorRef<M> {
        self.self_ref.clone()
    }

    /// Returns a reference to the underlying Cx.
    #[must_use]
    pub const fn cx(&self) -> &Cx {
        self.cx
    }
}

impl<M: Send + 'static> std::ops::Deref for ActorContext<'_, M> {
    type Target = Cx;

    fn deref(&self) -> &Self::Target {
        self.cx
    }
}

impl<M: Send + 'static> std::fmt::Debug for ActorContext<'_, M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ActorContext")
            .field("actor_id", &self.actor_id)
            .field("children", &self.children.len())
            .field("stopping", &self.stopping)
            .field("has_parent", &self.parent.is_some())
            .finish()
    }
}

/// The default mailbox capacity for actors.
pub const DEFAULT_MAILBOX_CAPACITY: usize = 64;

struct OnStopMaskGuard(Arc<parking_lot::RwLock<CxInner>>);

impl Drop for OnStopMaskGuard {
    fn drop(&mut self) {
        let mut g = self.0.write();
        g.mask_depth = g.mask_depth.saturating_sub(1);
    }
}

/// Internal: runs the actor message loop.
///
/// This function is the core of the actor runtime. It:
/// 1. Calls `on_start`
/// 2. Receives and handles messages until the mailbox is closed or cancelled
/// 3. Drains remaining buffered messages (no silent drops)
/// 4. Calls `on_stop`
/// 5. Returns the actor state
async fn run_actor_loop<A: Actor>(mut actor: A, cx: Cx, cell: &mut ActorCell<A::Message>) -> A {
    use crate::tracing_compat::debug;

    // Only transition to Running if stop() wasn't called before the actor started.
    // stop() sets Stopping before scheduling; we must honour that signal so the
    // poll_fn guard in the message loop can detect the pre-stop and break.
    if cell.state.load() != ActorState::Stopping {
        cell.state.store(ActorState::Running);
    }

    // Phase 1: Initialization
    // We always run on_start, even if cancelled or pre-stopped, because
    // it serves as the actor's initial setup and matches the expectation
    // that lifecycle hooks are symmetrically executed.
    cx.trace("actor::on_start");
    actor.on_start(&cx).await;

    // Phase 2: Message loop
    loop {
        // Check for cancellation
        if cx.checkpoint().is_err() {
            cx.trace("actor::cancel_requested");
            break;
        }

        let recv_result = std::future::poll_fn(|task_cx| {
            match cell.mailbox.poll_recv(&cx, task_cx) {
                std::task::Poll::Pending if cell.state.load() == ActorState::Stopping => {
                    // Graceful stop requested and mailbox is empty. Break the loop.
                    std::task::Poll::Ready(Err(crate::channel::mpsc::RecvError::Disconnected))
                }
                other => other,
            }
        })
        .await;

        match recv_result {
            Ok(msg) => {
                actor.handle(&cx, msg).await;
            }
            Err(crate::channel::mpsc::RecvError::Disconnected) => {
                // All senders dropped - graceful shutdown
                cx.trace("actor::mailbox_disconnected");
                break;
            }
            Err(crate::channel::mpsc::RecvError::Cancelled) => {
                // Cancellation requested
                cx.trace("actor::recv_cancelled");
                break;
            }
            Err(crate::channel::mpsc::RecvError::Empty) => {
                // Shouldn't happen with recv() (only try_recv), but handle gracefully
                break;
            }
        }
    }

    cell.state.store(ActorState::Stopping);

    let is_aborted = cx.checkpoint().is_err();

    // Phase 3: Drain remaining buffered messages.
    // Two-phase mailbox guarantee: no message silently dropped (unless aborted).
    // We seal the mailbox to prevent any new reservations or commits, then
    // process remaining messages if gracefully stopped. If aborted, we just
    // empty the mailbox to drop the messages.
    cell.mailbox.close();

    if is_aborted {
        while let Ok(_msg) = cell.mailbox.try_recv() {}
    } else {
        let mut drained: u64 = 0;
        while let Ok(msg) = cell.mailbox.try_recv() {
            actor.handle(&cx, msg).await;
            drained += 1;
        }
        if drained > 0 {
            debug!(drained = drained, "actor::mailbox_drained");
            cx.trace("actor::mailbox_drained");
        }
    }

    // Phase 4: Cleanup — mask cancellation so on_stop runs to completion.
    // Without masking, an aborted actor's on_stop could observe a stale
    // cancel_requested=true and bail early via cx.checkpoint().

    cx.trace("actor::on_stop");
    let inner = cx.inner.clone();
    {
        let mut guard = inner.write();
        assert!(
            guard.mask_depth < crate::types::task_context::MAX_MASK_DEPTH,
            "mask depth exceeded MAX_MASK_DEPTH ({}) in actor::on_stop: \
             this violates INV-MASK-BOUNDED and prevents cancellation from ever \
             being observed. Reduce nesting of masked sections.",
            crate::types::task_context::MAX_MASK_DEPTH
        );
        guard.mask_depth += 1;
    }
    let mask_guard = OnStopMaskGuard(inner);
    actor.on_stop(&cx).await;
    drop(mask_guard);

    actor
}

fn actor_cancel_join_error(cx: &Cx) -> JoinError {
    JoinError::Cancelled(
        cx.cancel_reason()
            .unwrap_or_else(|| crate::types::CancelReason::user("actor supervision cancelled")),
    )
}

fn supervised_restart_timestamp(cx: &Cx) -> u64 {
    cx.timer_driver().map_or_else(
        || crate::time::wall_now().as_nanos(),
        |td| td.now().as_nanos(),
    )
}

async fn wait_supervised_restart_delay(cx: &Cx, delay: Duration) -> Result<(), JoinError> {
    if cx.checkpoint().is_err() {
        return Err(actor_cancel_join_error(cx));
    }
    if delay.is_zero() {
        return Ok(());
    }

    let now = cx
        .timer_driver()
        .map_or_else(crate::time::wall_now, |td| td.now());
    let mut sleeper = crate::time::sleep(now, delay);
    std::future::poll_fn(|task_cx| {
        if cx.checkpoint().is_err() {
            return std::task::Poll::Ready(Err(actor_cancel_join_error(cx)));
        }
        Pin::new(&mut sleeper).poll(task_cx).map(|()| Ok(()))
    })
    .await
}

fn join_result_to_task_outcome<A>(result: &Result<A, JoinError>) -> Outcome<(), ()> {
    match result {
        Ok(_) => Outcome::Ok(()),
        Err(JoinError::Cancelled(reason)) => Outcome::Cancelled(reason.clone()),
        Err(JoinError::Panicked(payload)) => Outcome::Panicked(payload.clone()),
        Err(JoinError::PolledAfterCompletion) => {
            panic!("actor task produced JoinError::PolledAfterCompletion")
        }
    }
}

// Extension for Scope to spawn actors
impl<P: crate::types::Policy> crate::cx::Scope<'_, P> {
    /// Spawns a new actor in this scope with the given mailbox capacity.
    ///
    /// The actor runs as a region-owned task. Messages are delivered through
    /// a bounded MPSC channel with two-phase send semantics.
    ///
    /// # Arguments
    ///
    /// * `state` - Runtime state for task creation
    /// * `cx` - Capability context
    /// * `actor` - The actor instance
    /// * `mailbox_capacity` - Bounded mailbox size
    ///
    /// # Returns
    ///
    /// A tuple of `(ActorHandle, StoredTask)`. The `StoredTask` must be
    /// registered with the runtime via `state.store_spawned_task()`.
    pub fn spawn_actor<A: Actor>(
        &self,
        state: &mut crate::runtime::state::RuntimeState,
        cx: &Cx,
        actor: A,
        mailbox_capacity: usize,
    ) -> Result<(ActorHandle<A>, crate::runtime::stored_task::StoredTask), SpawnError> {
        use crate::channel::oneshot;
        use crate::cx::scope::CatchUnwind;
        use crate::runtime::stored_task::StoredTask;
        use crate::tracing_compat::{debug, debug_span};

        // Create the actor's mailbox
        let (msg_tx, msg_rx) = mpsc::channel::<A::Message>(mailbox_capacity);

        // Create oneshot for returning the actor state
        let (result_tx, result_rx) = oneshot::channel::<Result<A, JoinError>>();

        // Create task record
        let task_id = self.create_task_record(state)?;
        let actor_id = ActorId::from_task(task_id);
        let actor_state = Arc::new(ActorStateCell::new(ActorState::Created));

        let _span = debug_span!(
            "actor_spawn",
            task_id = ?task_id,
            region_id = ?self.region_id(),
            mailbox_capacity = mailbox_capacity,
        )
        .entered();
        debug!(
            task_id = ?task_id,
            region_id = ?self.region_id(),
            mailbox_capacity = mailbox_capacity,
            "actor spawned"
        );

        // Create child context
        let (_, child_cx) = self.build_child_task_cx(state, cx, task_id);

        // Link Cx to TaskRecord
        if let Some(record) = state.task_mut(task_id) {
            record.set_cx_inner(child_cx.inner.clone());
            record.set_cx(child_cx.clone());
        }

        let cx_for_send = child_cx.clone();
        let inner_weak = Arc::downgrade(&child_cx.inner);
        let state_for_task = Arc::clone(&actor_state);

        let mut cell = ActorCell {
            mailbox: msg_rx,
            state: Arc::clone(&actor_state),
        };

        // Create the actor loop future
        let wrapped = async move {
            let result = CatchUnwind {
                inner: Box::pin(run_actor_loop(actor, child_cx, &mut cell)),
            }
            .await;
            let outcome = match result {
                Ok(actor_final) => {
                    let _ = result_tx.send(&cx_for_send, Ok(actor_final));
                    Outcome::Ok(())
                }
                Err(payload) => {
                    let msg = crate::cx::scope::payload_to_string(&payload);
                    let panic_payload = crate::types::PanicPayload::new(msg);
                    let _ = result_tx.send(
                        &cx_for_send,
                        Err(JoinError::Panicked(panic_payload.clone())),
                    );
                    Outcome::Panicked(panic_payload)
                }
            };
            state_for_task.store(ActorState::Stopped);
            outcome
        };

        let stored = StoredTask::new_with_id(wrapped, task_id);

        let handle = ActorHandle {
            actor_id,
            sender: msg_tx,
            state: actor_state,
            task_id,
            receiver: result_rx,
            inner: inner_weak,
            completed: false,
        };

        Ok((handle, stored))
    }

    /// Spawns a supervised actor with explicit supervision semantics.
    ///
    /// Unlike `spawn_actor`, this method takes a factory closure that can
    /// produce new actor instances for restarts. The mailbox persists across
    /// restarts, so messages sent while a restartable failure is being handled
    /// are buffered for the next instance.
    ///
    /// Because [`Actor`] has no explicit error return channel, supervised
    /// crashes are treated as restartable failures when the strategy is
    /// [`crate::supervision::SupervisionStrategy::Restart`]. If supervision
    /// ultimately stops or escalates, the original panic payload is still
    /// surfaced as `JoinError::Panicked`.
    ///
    /// # Arguments
    ///
    /// * `state` - Runtime state for task creation
    /// * `cx` - Capability context
    /// * `factory` - Closure that creates actor instances (called on each restart)
    /// * `strategy` - Supervision strategy (Stop, Restart, Escalate)
    /// * `mailbox_capacity` - Bounded mailbox size
    pub fn spawn_supervised_actor<A, F>(
        &self,
        state: &mut crate::runtime::state::RuntimeState,
        cx: &Cx,
        mut factory: F,
        strategy: crate::supervision::SupervisionStrategy,
        mailbox_capacity: usize,
    ) -> Result<(ActorHandle<A>, crate::runtime::stored_task::StoredTask), SpawnError>
    where
        A: Actor,
        F: FnMut() -> A + Send + 'static,
    {
        use crate::channel::oneshot;
        use crate::runtime::stored_task::StoredTask;
        use crate::supervision::Supervisor;
        use crate::tracing_compat::{debug, debug_span};

        let actor = factory();
        let (msg_tx, msg_rx) = mpsc::channel::<A::Message>(mailbox_capacity);
        let (result_tx, result_rx) = oneshot::channel::<Result<A, JoinError>>();
        let task_id = self.create_task_record(state)?;
        let actor_id = ActorId::from_task(task_id);
        let actor_state = Arc::new(ActorStateCell::new(ActorState::Created));

        let _span = debug_span!(
            "supervised_actor_spawn",
            task_id = ?task_id,
            region_id = ?self.region_id(),
            mailbox_capacity = mailbox_capacity,
        )
        .entered();
        debug!(
            task_id = ?task_id,
            region_id = ?self.region_id(),
            "supervised actor spawned"
        );

        let (_, child_cx) = self.build_child_task_cx(state, cx, task_id);

        if let Some(record) = state.task_mut(task_id) {
            record.set_cx_inner(child_cx.inner.clone());
            record.set_cx(child_cx.clone());
        }

        let cx_for_send = child_cx.clone();
        let inner_weak = Arc::downgrade(&child_cx.inner);
        let region_id = self.region_id();
        let state_for_task = Arc::clone(&actor_state);

        let mut cell = ActorCell {
            mailbox: msg_rx,
            state: Arc::clone(&actor_state),
        };

        let wrapped = async move {
            let result = run_supervised_loop(
                actor,
                &mut factory,
                child_cx,
                &mut cell,
                Supervisor::new(strategy),
                task_id,
                region_id,
            )
            .await;
            let outcome = join_result_to_task_outcome(&result).map_err(|_| ());
            let _ = result_tx.send(&cx_for_send, result);
            state_for_task.store(ActorState::Stopped);
            outcome
        };

        let stored = StoredTask::new_with_id(wrapped, task_id);

        let handle = ActorHandle {
            actor_id,
            sender: msg_tx,
            state: actor_state,
            task_id,
            receiver: result_rx,
            inner: inner_weak,
            completed: false,
        };

        Ok((handle, stored))
    }
}

/// Outcome of a supervised actor run.
#[derive(Debug)]
pub enum SupervisedOutcome {
    /// Actor stopped normally (no failure).
    Stopped,
    /// Actor stopped after restart budget exhaustion.
    RestartBudgetExhausted {
        /// Total restarts before budget was exhausted.
        total_restarts: u32,
    },
    /// Failure was escalated to parent region.
    Escalated,
}

/// Internal: runs a supervised actor loop with restart support.
///
/// The mailbox receiver is shared across restarts — messages sent while the
/// actor is restarting are buffered and processed by the new instance.
async fn run_supervised_loop<A, F>(
    initial_actor: A,
    factory: &mut F,
    cx: Cx,
    cell: &mut ActorCell<A::Message>,
    mut supervisor: crate::supervision::Supervisor,
    task_id: TaskId,
    region_id: RegionId,
) -> Result<A, JoinError>
where
    A: Actor,
    F: FnMut() -> A,
{
    use crate::cx::scope::CatchUnwind;
    use crate::supervision::SupervisionDecision;
    use crate::types::Outcome;

    let mut current_actor = initial_actor;

    loop {
        // Run the actor until it finishes (normally or via panic)
        let result = CatchUnwind {
            inner: Box::pin(run_actor_loop(current_actor, cx.clone(), cell)),
        }
        .await;

        match result {
            Ok(actor_final) => {
                // Actor completed normally — no supervision needed
                return Ok(actor_final);
            }
            Err(payload) => {
                let msg = crate::cx::scope::payload_to_string(&payload);
                let panic_payload = crate::types::PanicPayload::new(msg);
                cx.trace("supervised_actor::failure");

                // Explicit shutdown wins over restart policy. If the owner has
                // already requested stop/abort, a panic during mailbox drain or
                // on_stop is terminal and must not resurrect the actor.
                if cell.state.load() == ActorState::Stopping || cx.checkpoint().is_err() {
                    cx.trace("supervised_actor::shutdown_panic");
                    return Err(JoinError::Panicked(panic_payload));
                }

                // Actors do not have a typed `Err` path. A crash is therefore
                // the only recoverable failure signal available to the actor
                // supervision layer, so present it to the generic supervisor as
                // a restartable failure while preserving the original payload to
                // surface if supervision ultimately stops or escalates.
                let outcome = Outcome::Err(());
                let now = supervised_restart_timestamp(&cx);
                let decision = supervisor.on_failure(task_id, region_id, None, &outcome, now);

                match decision {
                    SupervisionDecision::Restart { delay, .. } => {
                        cx.trace("supervised_actor::restart");

                        // Graceful shutdown may arrive after the crash but
                        // before the delayed restart starts running. That stop
                        // must suppress the restart rather than instantiate a
                        // fresh actor during shutdown.
                        if cell.state.load() == ActorState::Stopping {
                            cx.trace("supervised_actor::restart_suppressed");
                            return Err(JoinError::Panicked(panic_payload));
                        }

                        // Apply backoff delay if the supervisor computed one.
                        if let Some(backoff) = delay {
                            wait_supervised_restart_delay(&cx, backoff).await?;
                        }

                        if cell.state.load() == ActorState::Stopping || cx.checkpoint().is_err() {
                            cx.trace("supervised_actor::restart_suppressed");
                            return Err(JoinError::Panicked(panic_payload));
                        }

                        // Reset actor state so the restarted actor enters
                        // Running instead of staying in Stopping (which
                        // would cause it to exit immediately on empty
                        // mailbox).
                        cell.state.store(ActorState::Created);
                        current_actor = factory();
                    }
                    SupervisionDecision::Stop { .. } => {
                        cx.trace("supervised_actor::stopped");
                        return Err(JoinError::Panicked(panic_payload));
                    }
                    SupervisionDecision::Escalate { .. } => {
                        cx.trace("supervised_actor::escalated");
                        return Err(JoinError::Panicked(panic_payload));
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cx::macaroon::MacaroonToken;
    use crate::cx::registry::{RegistryCap, RegistryHandle};
    use crate::remote::{NodeId, RemoteCap};
    use crate::runtime::state::RuntimeState;
    use crate::security::key::AuthKey;
    use crate::types::Budget;
    use crate::types::SystemPressure;
    use crate::types::policy::FailFast;
    use std::sync::Arc;
    use std::task::{Context, Poll, Waker};

    fn init_test(name: &str) {
        crate::test_utils::init_test_logging();
        crate::test_phase!(name);
    }

    fn counting_waker(counter: Arc<std::sync::atomic::AtomicUsize>) -> Waker {
        struct CountingWaker {
            counter: Arc<std::sync::atomic::AtomicUsize>,
        }

        impl std::task::Wake for CountingWaker {
            fn wake(self: Arc<Self>) {
                self.counter.fetch_add(1, Ordering::SeqCst);
            }

            fn wake_by_ref(self: &Arc<Self>) {
                self.counter.fetch_add(1, Ordering::SeqCst);
            }
        }

        Waker::from(Arc::new(CountingWaker { counter }))
    }

    /// Simple counter actor for testing.
    #[derive(Debug)]
    struct Counter {
        count: u64,
        started: bool,
        stopped: bool,
    }

    impl Counter {
        fn new() -> Self {
            Self {
                count: 0,
                started: false,
                stopped: false,
            }
        }
    }

    impl Actor for Counter {
        type Message = u64;

        fn on_start(&mut self, _cx: &Cx) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
            self.started = true;
            Box::pin(async {})
        }

        fn handle(&mut self, _cx: &Cx, msg: u64) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
            self.count += msg;
            Box::pin(async {})
        }

        fn on_stop(&mut self, _cx: &Cx) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
            self.stopped = true;
            Box::pin(async {})
        }
    }

    fn assert_actor<A: Actor>() {}

    #[derive(Debug, Clone, PartialEq, Eq)]
    struct CapabilitySnapshot {
        same_registry: bool,
        same_remote: bool,
        same_io: bool,
        same_pressure: bool,
        same_macaroon: bool,
        has_timer: bool,
    }

    struct CapabilityProbeActor {
        snapshot: Arc<parking_lot::Mutex<Option<CapabilitySnapshot>>>,
        expected_registry: Arc<dyn RegistryCap>,
        expected_remote_node: String,
        expected_io: Arc<dyn crate::io::IoCap>,
        expected_pressure: Arc<SystemPressure>,
        expected_macaroon: Arc<MacaroonToken>,
    }

    impl Actor for CapabilityProbeActor {
        type Message = ();

        fn on_start(&mut self, cx: &Cx) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
            let child_registry = cx
                .registry_handle()
                .expect("actor child Cx must inherit registry")
                .as_arc();
            let child_io = cx
                .io_cap_handle()
                .expect("actor child Cx must inherit io capability");
            let child_pressure = cx
                .pressure_handle()
                .expect("actor child Cx must inherit system pressure");
            let child_macaroon = cx
                .macaroon_handle()
                .expect("actor child Cx must inherit macaroon");
            let remote_node = cx
                .remote()
                .map(|remote| remote.local_node().as_str().to_owned());

            *self.snapshot.lock() = Some(CapabilitySnapshot {
                same_registry: Arc::ptr_eq(&child_registry, &self.expected_registry),
                same_remote: remote_node.as_deref() == Some(self.expected_remote_node.as_str()),
                same_io: Arc::ptr_eq(&child_io, &self.expected_io),
                same_pressure: Arc::ptr_eq(&child_pressure, &self.expected_pressure),
                same_macaroon: Arc::ptr_eq(&child_macaroon, &self.expected_macaroon),
                has_timer: cx.has_timer(),
            });

            Box::pin(async {})
        }

        fn handle(&mut self, _cx: &Cx, _msg: ()) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
            Box::pin(async {})
        }
    }

    fn capability_rich_parent_cx(
        runtime: &crate::lab::LabRuntime,
        region: crate::types::RegionId,
    ) -> (
        Cx,
        Arc<dyn RegistryCap>,
        Arc<dyn crate::io::IoCap>,
        Arc<SystemPressure>,
        Arc<MacaroonToken>,
    ) {
        let registry = crate::cx::NameRegistry::new();
        let registry_handle = RegistryHandle::new(Arc::new(registry));
        let registry_arc = registry_handle.as_arc();
        let io_cap: Arc<dyn crate::io::IoCap> = Arc::new(crate::io::LabIoCap::new());
        let pressure = Arc::new(SystemPressure::with_headroom(0.25));
        let macaroon_token =
            MacaroonToken::mint(&AuthKey::from_seed(7), "scope:actor", "actor/tests");

        let parent_cx = Cx::new_with_drivers(
            region,
            crate::types::TaskId::new_for_test(77, 0),
            Budget::INFINITE,
            None,
            None,
            Some(Arc::clone(&io_cap)),
            runtime.state.timer_driver_handle(),
            None,
        )
        .with_registry_handle(Some(registry_handle))
        .with_remote_cap(RemoteCap::new().with_local_node(NodeId::new("actor-origin")))
        .with_pressure(Arc::clone(&pressure))
        .with_macaroon(macaroon_token);

        let macaroon = parent_cx
            .macaroon_handle()
            .expect("parent actor test Cx must retain macaroon");

        (parent_cx, registry_arc, io_cap, pressure, macaroon)
    }

    #[test]
    fn actor_trait_object_safety() {
        init_test("actor_trait_object_safety");

        // Verify Counter implements Actor with the right bounds
        assert_actor::<Counter>();

        crate::test_complete!("actor_trait_object_safety");
    }

    #[test]
    fn actor_handle_creation() {
        init_test("actor_handle_creation");

        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(root, Budget::INFINITE);

        let result = scope.spawn_actor(&mut state, &cx, Counter::new(), 32);
        assert!(result.is_ok(), "spawn_actor should succeed");

        let (handle, stored) = result.unwrap();
        state.store_spawned_task(handle.task_id(), stored);

        // Handle should have valid task ID
        let _tid = handle.task_id();

        // Actor should not be finished yet (not polled)
        assert!(!handle.is_finished());

        crate::test_complete!("actor_handle_creation");
    }

    #[test]
    fn spawn_actor_inherits_child_cx_capabilities() {
        init_test("spawn_actor_inherits_child_cx_capabilities");

        let mut runtime = crate::lab::LabRuntime::new(crate::lab::LabConfig::default());
        let region = runtime.state.create_root_region(Budget::INFINITE);
        let scope = crate::cx::Scope::<FailFast>::new(region, Budget::INFINITE);
        let (parent_cx, registry_arc, io_cap, pressure, macaroon) =
            capability_rich_parent_cx(&runtime, region);
        let snapshot = Arc::new(parking_lot::Mutex::new(None));

        let actor = CapabilityProbeActor {
            snapshot: Arc::clone(&snapshot),
            expected_registry: registry_arc,
            expected_remote_node: "actor-origin".to_string(),
            expected_io: io_cap,
            expected_pressure: pressure,
            expected_macaroon: macaroon,
        };

        let (handle, stored) = scope
            .spawn_actor(&mut runtime.state, &parent_cx, actor, 8)
            .expect("spawn actor");
        let task_id = handle.task_id();
        runtime.state.store_spawned_task(task_id, stored);

        runtime.scheduler.lock().schedule(task_id, 0);
        runtime.run_until_idle();

        let observed = snapshot
            .lock()
            .clone()
            .expect("actor on_start should capture inherited capabilities");
        assert_eq!(
            observed,
            CapabilitySnapshot {
                same_registry: true,
                same_remote: true,
                same_io: true,
                same_pressure: true,
                same_macaroon: true,
                has_timer: true,
            }
        );

        drop(handle);
        runtime.run_until_quiescent();

        crate::test_complete!("spawn_actor_inherits_child_cx_capabilities");
    }

    #[test]
    fn spawn_supervised_actor_inherits_child_cx_capabilities() {
        init_test("spawn_supervised_actor_inherits_child_cx_capabilities");

        let mut runtime = crate::lab::LabRuntime::new(crate::lab::LabConfig::default());
        let region = runtime.state.create_root_region(Budget::INFINITE);
        let scope = crate::cx::Scope::<FailFast>::new(region, Budget::INFINITE);
        let (parent_cx, registry_arc, io_cap, pressure, macaroon) =
            capability_rich_parent_cx(&runtime, region);
        let snapshot = Arc::new(parking_lot::Mutex::new(None));

        let snapshot_for_factory = Arc::clone(&snapshot);
        let strategy = crate::supervision::SupervisionStrategy::Stop;
        let (handle, stored) = scope
            .spawn_supervised_actor(
                &mut runtime.state,
                &parent_cx,
                move || CapabilityProbeActor {
                    snapshot: Arc::clone(&snapshot_for_factory),
                    expected_registry: Arc::clone(&registry_arc),
                    expected_remote_node: "actor-origin".to_string(),
                    expected_io: Arc::clone(&io_cap),
                    expected_pressure: Arc::clone(&pressure),
                    expected_macaroon: Arc::clone(&macaroon),
                },
                strategy,
                8,
            )
            .expect("spawn supervised actor");
        let task_id = handle.task_id();
        runtime.state.store_spawned_task(task_id, stored);

        runtime.scheduler.lock().schedule(task_id, 0);
        runtime.run_until_idle();

        let observed = snapshot
            .lock()
            .clone()
            .expect("supervised actor on_start should capture inherited capabilities");
        assert_eq!(
            observed,
            CapabilitySnapshot {
                same_registry: true,
                same_remote: true,
                same_io: true,
                same_pressure: true,
                same_macaroon: true,
                has_timer: true,
            }
        );

        drop(handle);
        runtime.run_until_quiescent();

        crate::test_complete!("spawn_supervised_actor_inherits_child_cx_capabilities");
    }

    #[test]
    fn actor_id_generation_distinct() {
        init_test("actor_id_generation_distinct");

        let id1 = ActorId::from_task(TaskId::new_for_test(1, 1));
        let id2 = ActorId::from_task(TaskId::new_for_test(1, 2));
        assert!(id1 != id2, "generation must distinguish actor reuse");

        crate::test_complete!("actor_id_generation_distinct");
    }

    #[test]
    fn actor_ref_is_cloneable() {
        init_test("actor_ref_is_cloneable");

        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(root, Budget::INFINITE);

        let (handle, stored) = scope
            .spawn_actor(&mut state, &cx, Counter::new(), 32)
            .unwrap();
        state.store_spawned_task(handle.task_id(), stored);

        // Get multiple refs
        let ref1 = handle.sender();
        let ref2 = ref1.clone();

        // Actor identity is preserved across clones
        assert_eq!(ref1.actor_id(), handle.actor_id());
        assert_eq!(ref2.actor_id(), handle.actor_id());

        // Actor is alive at creation time (even before first poll)
        assert!(ref1.is_alive());
        assert!(ref2.is_alive());

        // Both should be open
        assert!(!ref1.is_closed());
        assert!(!ref2.is_closed());

        crate::test_complete!("actor_ref_is_cloneable");
    }

    // ---- E2E Actor Scenarios ----

    use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};

    /// Observable counter actor: writes final count to shared state during on_stop.
    /// Used by E2E tests to verify actor behavior without needing join().
    struct ObservableCounter {
        count: u64,
        on_stop_count: Arc<AtomicU64>,
        started: Arc<AtomicBool>,
        stopped: Arc<AtomicBool>,
    }

    impl ObservableCounter {
        fn new(
            on_stop_count: Arc<AtomicU64>,
            started: Arc<AtomicBool>,
            stopped: Arc<AtomicBool>,
        ) -> Self {
            Self {
                count: 0,
                on_stop_count,
                started,
                stopped,
            }
        }
    }

    impl Actor for ObservableCounter {
        type Message = u64;

        fn on_start(&mut self, _cx: &Cx) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
            self.started.store(true, Ordering::SeqCst);
            Box::pin(async {})
        }

        fn handle(&mut self, _cx: &Cx, msg: u64) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
            self.count += msg;
            Box::pin(async {})
        }

        fn on_stop(&mut self, _cx: &Cx) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
            self.on_stop_count.store(self.count, Ordering::SeqCst);
            self.stopped.store(true, Ordering::SeqCst);
            Box::pin(async {})
        }
    }

    fn observable_state() -> (Arc<AtomicU64>, Arc<AtomicBool>, Arc<AtomicBool>) {
        (
            Arc::new(AtomicU64::new(u64::MAX)),
            Arc::new(AtomicBool::new(false)),
            Arc::new(AtomicBool::new(false)),
        )
    }

    /// E2E: Actor processes all messages sent before channel disconnect.
    /// Verifies: messages delivered, on_start called, on_stop called.
    #[test]
    fn actor_processes_all_messages() {
        init_test("actor_processes_all_messages");

        let mut runtime = crate::lab::LabRuntime::new(crate::lab::LabConfig::default());
        let region = runtime.state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(region, Budget::INFINITE);

        let (on_stop_count, started, stopped) = observable_state();
        let actor = ObservableCounter::new(on_stop_count.clone(), started.clone(), stopped.clone());

        let (handle, stored) = scope
            .spawn_actor(&mut runtime.state, &cx, actor, 32)
            .unwrap();
        let task_id = handle.task_id();
        runtime.state.store_spawned_task(task_id, stored);

        // Pre-fill mailbox with 5 messages (each adding 1)
        for _ in 0..5 {
            handle.try_send(1).unwrap();
        }

        // Drop handle to disconnect channel — actor will process buffered
        // messages via recv, then see Disconnected and stop gracefully.
        drop(handle);

        runtime.scheduler.lock().schedule(task_id, 0);
        runtime.run_until_quiescent();

        assert_eq!(
            on_stop_count.load(Ordering::SeqCst),
            5,
            "all messages processed"
        );
        assert!(started.load(Ordering::SeqCst), "on_start was called");
        assert!(stopped.load(Ordering::SeqCst), "on_stop was called");

        crate::test_complete!("actor_processes_all_messages");
    }

    /// E2E: Mailbox drain on cancellation.
    /// Pre-fills mailbox, cancels actor before it runs, verifies all messages
    /// are still processed during the drain phase (no silent drops).
    #[test]
    fn actor_drains_mailbox_on_cancel() {
        init_test("actor_drains_mailbox_on_cancel");

        let mut runtime = crate::lab::LabRuntime::new(crate::lab::LabConfig::default());
        let region = runtime.state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(region, Budget::INFINITE);

        let (on_stop_count, started, stopped) = observable_state();
        let actor = ObservableCounter::new(on_stop_count.clone(), started.clone(), stopped.clone());

        let (handle, stored) = scope
            .spawn_actor(&mut runtime.state, &cx, actor, 32)
            .unwrap();
        let task_id = handle.task_id();
        runtime.state.store_spawned_task(task_id, stored);

        // Pre-fill mailbox with 5 messages
        for _ in 0..5 {
            handle.try_send(1).unwrap();
        }

        // Cancel the actor BEFORE running.
        // The actor loop will: on_start → check cancel → break → drain → on_stop
        handle.stop();
        let stopped_ref = handle.sender();
        assert!(
            stopped_ref.is_closed(),
            "stop() seals the mailbox immediately"
        );
        assert!(
            matches!(handle.try_send(99), Err(SendError::Disconnected(99))),
            "stop() must reject new messages instead of extending shutdown"
        );

        runtime.scheduler.lock().schedule(task_id, 0);
        runtime.run_until_quiescent();

        // All 5 messages processed during drain phase
        assert_eq!(
            on_stop_count.load(Ordering::SeqCst),
            5,
            "drain processed all messages"
        );
        assert!(started.load(Ordering::SeqCst), "on_start was called");
        assert!(stopped.load(Ordering::SeqCst), "on_stop was called");

        crate::test_complete!("actor_drains_mailbox_on_cancel");
    }

    /// E2E: ActorRef liveness tracks actor lifecycle (Created -> Stopping -> Stopped).
    #[test]
    fn actor_ref_is_alive_transitions() {
        init_test("actor_ref_is_alive_transitions");

        let mut runtime = crate::lab::LabRuntime::new(crate::lab::LabConfig::default());
        let region = runtime.state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(region, Budget::INFINITE);

        let (on_stop_count, started, stopped) = observable_state();
        let actor = ObservableCounter::new(on_stop_count.clone(), started.clone(), stopped.clone());

        let (handle, stored) = scope
            .spawn_actor(&mut runtime.state, &cx, actor, 32)
            .unwrap();
        let task_id = handle.task_id();
        runtime.state.store_spawned_task(task_id, stored);

        let actor_ref = handle.sender();
        assert!(actor_ref.is_alive(), "created actor should be alive");
        assert_eq!(actor_ref.actor_id(), handle.actor_id());

        handle.stop();
        assert!(actor_ref.is_alive(), "stopping actor is still alive");

        runtime.scheduler.lock().schedule(task_id, 0);
        runtime.run_until_quiescent();

        assert!(
            handle.is_finished(),
            "actor should be finished after stop + run"
        );
        assert!(!actor_ref.is_alive(), "finished actor is not alive");

        // Sanity: the actor ran its hooks.
        assert!(started.load(Ordering::SeqCst), "on_start was called");
        assert!(stopped.load(Ordering::SeqCst), "on_stop was called");
        assert_ne!(
            on_stop_count.load(Ordering::SeqCst),
            u64::MAX,
            "on_stop_count updated"
        );

        crate::test_complete!("actor_ref_is_alive_transitions");
    }

    #[test]
    fn dropped_join_future_marks_actor_stopping_like_abort() {
        init_test("dropped_join_future_marks_actor_stopping_like_abort");

        let mut runtime = crate::lab::LabRuntime::new(crate::lab::LabConfig::default());
        let region = runtime.state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(region, Budget::INFINITE);

        let (on_stop_count, started, stopped) = observable_state();
        let actor = ObservableCounter::new(on_stop_count.clone(), started.clone(), stopped.clone());

        let (mut handle, stored) = scope
            .spawn_actor(&mut runtime.state, &cx, actor, 32)
            .unwrap();
        let task_id = handle.task_id();
        runtime.state.store_spawned_task(task_id, stored);

        runtime.scheduler.lock().schedule(task_id, 0);
        runtime.run_until_idle();
        assert_eq!(
            handle.state.load(),
            ActorState::Running,
            "actor should be running before join drop requests abort"
        );

        drop(handle.join(&cx));

        assert_eq!(
            handle.state.load(),
            ActorState::Stopping,
            "dropping join future should mirror ActorHandle::abort state transition"
        );
        assert!(
            matches!(handle.try_send(1), Err(SendError::Disconnected(1))),
            "join-drop abort must seal the mailbox immediately"
        );

        runtime.run_until_quiescent();
        assert!(
            handle.is_finished(),
            "actor should stop after join future drop"
        );
        assert!(started.load(Ordering::SeqCst), "on_start should have run");
        assert!(stopped.load(Ordering::SeqCst), "on_stop should have run");
        assert_eq!(
            on_stop_count.load(Ordering::SeqCst),
            0,
            "idle actor should stop without processing phantom messages"
        );

        crate::test_complete!("dropped_join_future_marks_actor_stopping_like_abort");
    }

    #[test]
    fn actor_stop_unblocks_pending_sender_with_disconnect() {
        init_test("actor_stop_unblocks_pending_sender_with_disconnect");

        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(root, Budget::INFINITE);

        let (handle, stored) = scope
            .spawn_actor(&mut state, &cx, Counter::new(), 1)
            .unwrap();
        state.store_spawned_task(handle.task_id(), stored);

        handle.try_send(1).expect("fill mailbox");
        let sender = handle.sender();
        let mut send_fut = Box::pin(sender.send(&cx, 2));
        let wake_count = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let waker = counting_waker(Arc::clone(&wake_count));
        let mut task_cx = Context::from_waker(&waker);

        let first_poll = send_fut.as_mut().poll(&mut task_cx);
        assert!(
            matches!(first_poll, Poll::Pending),
            "send should wait while the mailbox is full"
        );

        handle.stop();

        assert_eq!(
            wake_count.load(Ordering::SeqCst),
            1,
            "stop() must wake a sender blocked on mailbox capacity"
        );
        let second_poll = send_fut.as_mut().poll(&mut task_cx);
        assert!(
            matches!(second_poll, Poll::Ready(Err(SendError::Disconnected(2)))),
            "pending sender must fail fast once stop seals the mailbox"
        );

        crate::test_complete!("actor_stop_unblocks_pending_sender_with_disconnect");
    }

    /// E2E: Supervised actor crashes restart under Restart strategy.
    #[test]
    fn supervised_actor_panic_restarts_under_restart_strategy() {
        use std::sync::atomic::AtomicU32;

        #[derive(Debug)]
        struct PanickingCounter {
            count: u64,
            panic_on: u64,
            final_count: Arc<AtomicU64>,
        }

        impl Actor for PanickingCounter {
            type Message = u64;

            fn handle(
                &mut self,
                _cx: &Cx,
                msg: u64,
            ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
                assert!(msg != self.panic_on, "threshold exceeded: {msg}");
                self.count += msg;
                Box::pin(async {})
            }

            fn on_stop(&mut self, _cx: &Cx) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
                self.final_count.store(self.count, Ordering::SeqCst);
                Box::pin(async {})
            }
        }

        init_test("supervised_actor_panic_restarts_under_restart_strategy");

        let mut runtime = crate::lab::LabRuntime::new(crate::lab::LabConfig::default());
        let region = runtime.state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(region, Budget::INFINITE);

        let final_count = Arc::new(AtomicU64::new(u64::MAX));
        let restart_count = Arc::new(AtomicU32::new(0));
        let fc = final_count.clone();
        let rc = restart_count.clone();

        let strategy = crate::supervision::SupervisionStrategy::Restart(
            crate::supervision::RestartConfig::new(3, std::time::Duration::from_secs(60))
                .with_backoff(crate::supervision::BackoffStrategy::None),
        );

        let (mut handle, stored) = scope
            .spawn_supervised_actor(
                &mut runtime.state,
                &cx,
                move || {
                    rc.fetch_add(1, Ordering::SeqCst);
                    PanickingCounter {
                        count: 0,
                        panic_on: 999,
                        final_count: fc.clone(),
                    }
                },
                strategy,
                32,
            )
            .unwrap();
        let task_id = handle.task_id();
        runtime.state.store_spawned_task(task_id, stored);

        // Message sequence:
        // 1. Normal message (count += 1)
        // 2. Panic trigger
        // 3. Queued message that should run on the restarted actor instance
        handle.try_send(1).unwrap();
        handle.try_send(999).unwrap(); // triggers panic
        handle.try_send(1).unwrap();

        runtime.scheduler.lock().schedule(task_id, 0);
        runtime.run_until_idle();
        handle.abort();
        runtime.run_until_quiescent();

        let join = futures_lite::future::block_on(handle.join(&cx));
        let actor = join.expect("aborting the restarted actor should still return final state");
        assert_eq!(
            restart_count.load(Ordering::SeqCst),
            2,
            "panic must trigger exactly one supervised restart, got {} factory calls",
            restart_count.load(Ordering::SeqCst)
        );
        assert_eq!(
            actor.count, 1,
            "restarted actor should keep the post-crash message count"
        );
        assert_eq!(
            final_count.load(Ordering::SeqCst),
            1,
            "restarted actor should process the queued post-crash message before abort"
        );

        crate::test_complete!("supervised_actor_panic_restarts_under_restart_strategy");
    }

    #[test]
    fn supervised_restart_window_expires_without_timer_driver() {
        use std::thread;

        init_test("supervised_restart_window_expires_without_timer_driver");

        let cx = Cx::new(
            RegionId::testing_default(),
            TaskId::new_for_test(1, 1),
            Budget::INFINITE,
        );
        let mut supervisor =
            crate::supervision::Supervisor::new(crate::supervision::SupervisionStrategy::Restart(
                crate::supervision::RestartConfig::new(1, Duration::from_millis(2))
                    .with_backoff(crate::supervision::BackoffStrategy::None),
            ));
        let outcome = Outcome::Err(());
        let task_id = TaskId::new_for_test(2, 1);

        let first = supervisor.on_failure(
            task_id,
            RegionId::testing_default(),
            None,
            &outcome,
            supervised_restart_timestamp(&cx),
        );
        assert!(
            matches!(
                first,
                crate::supervision::SupervisionDecision::Restart { attempt: 1, .. }
            ),
            "first failure should allow a restart"
        );

        thread::sleep(Duration::from_millis(5));

        let second = supervisor.on_failure(
            task_id,
            RegionId::testing_default(),
            None,
            &outcome,
            supervised_restart_timestamp(&cx),
        );
        assert!(
            matches!(
                second,
                crate::supervision::SupervisionDecision::Restart { attempt: 1, .. }
            ),
            "wall-clock fallback must let the restart window expire without a timer driver"
        );

        crate::test_complete!("supervised_restart_window_expires_without_timer_driver");
    }

    #[test]
    fn supervised_actor_stop_prevents_restart_after_panic() {
        use std::sync::atomic::AtomicU32;

        #[derive(Debug)]
        struct StopThenPanicActor;

        impl Actor for StopThenPanicActor {
            type Message = ();

            fn handle(
                &mut self,
                _cx: &Cx,
                _msg: (),
            ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
                panic!("panic during shutdown");
            }
        }

        init_test("supervised_actor_stop_prevents_restart_after_panic");

        let mut runtime = crate::lab::LabRuntime::new(crate::lab::LabConfig::default());
        let region = runtime.state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(region, Budget::INFINITE);

        let restart_count = Arc::new(AtomicU32::new(0));
        let rc = Arc::clone(&restart_count);
        let strategy = crate::supervision::SupervisionStrategy::Restart(
            crate::supervision::RestartConfig::new(3, Duration::from_secs(60))
                .with_backoff(crate::supervision::BackoffStrategy::None),
        );

        let (mut handle, stored) = scope
            .spawn_supervised_actor(
                &mut runtime.state,
                &cx,
                move || {
                    rc.fetch_add(1, Ordering::SeqCst);
                    StopThenPanicActor
                },
                strategy,
                8,
            )
            .expect("spawn supervised actor");
        let task_id = handle.task_id();
        runtime.state.store_spawned_task(task_id, stored);

        handle.try_send(()).expect("queue panic message");
        handle.stop();

        runtime.scheduler.lock().schedule(task_id, 0);
        runtime.run_until_quiescent();

        assert_eq!(
            restart_count.load(Ordering::SeqCst),
            1,
            "explicit stop must suppress supervised restarts"
        );

        let join = futures_lite::future::block_on(handle.join(&cx));
        match join {
            Err(JoinError::Panicked(payload)) => {
                assert_eq!(
                    payload.message(),
                    "panic during shutdown",
                    "shutdown panic should surface without restarting"
                );
            }
            other => panic!("expected shutdown panic without restart, got {other:?}"),
        }

        crate::test_complete!("supervised_actor_stop_prevents_restart_after_panic");
    }

    #[test]
    fn supervised_actor_stop_during_restart_backoff_prevents_new_instance() {
        use std::sync::atomic::AtomicU32;

        #[derive(Debug)]
        struct DelayedRestartActor {
            starts: Arc<AtomicU32>,
        }

        impl Actor for DelayedRestartActor {
            type Message = ();

            fn on_start(&mut self, _cx: &Cx) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
                let starts = Arc::clone(&self.starts);
                Box::pin(async move {
                    starts.fetch_add(1, Ordering::SeqCst);
                })
            }

            fn handle(
                &mut self,
                _cx: &Cx,
                _msg: (),
            ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
                panic!("panic before delayed restart");
            }
        }

        init_test("supervised_actor_stop_during_restart_backoff_prevents_new_instance");

        let mut runtime = crate::lab::LabRuntime::new(crate::lab::LabConfig::default());
        let region = runtime.state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(region, Budget::INFINITE);

        let factory_count = Arc::new(AtomicU32::new(0));
        let starts = Arc::new(AtomicU32::new(0));
        let fc = Arc::clone(&factory_count);
        let starts_for_factory = Arc::clone(&starts);
        let strategy = crate::supervision::SupervisionStrategy::Restart(
            crate::supervision::RestartConfig::new(3, Duration::from_secs(60)).with_backoff(
                crate::supervision::BackoffStrategy::Fixed(Duration::from_secs(5)),
            ),
        );

        let (mut handle, stored) = scope
            .spawn_supervised_actor(
                &mut runtime.state,
                &cx,
                move || {
                    fc.fetch_add(1, Ordering::SeqCst);
                    DelayedRestartActor {
                        starts: Arc::clone(&starts_for_factory),
                    }
                },
                strategy,
                8,
            )
            .expect("spawn supervised actor");
        let task_id = handle.task_id();
        runtime.state.store_spawned_task(task_id, stored);

        handle.try_send(()).expect("queue panic message");

        runtime.scheduler.lock().schedule(task_id, 0);
        runtime.run_until_idle();
        assert_eq!(
            runtime.pending_timer_count(),
            1,
            "supervised actor should be waiting on restart backoff"
        );

        handle.stop();
        let report = runtime.run_with_auto_advance();

        assert!(
            matches!(
                report.termination,
                crate::lab::AutoAdvanceTermination::Quiescent
            ),
            "runtime should quiesce after stop suppresses restart: {report:?}"
        );
        assert_eq!(
            factory_count.load(Ordering::SeqCst),
            1,
            "graceful stop during backoff must prevent a replacement actor from being constructed"
        );
        assert_eq!(
            starts.load(Ordering::SeqCst),
            1,
            "graceful stop during backoff must prevent restarted actor lifecycle hooks from running"
        );

        let join = futures_lite::future::block_on(handle.join(&cx));
        match join {
            Err(JoinError::Panicked(payload)) => {
                assert_eq!(
                    payload.message(),
                    "panic before delayed restart",
                    "original panic should surface when restart is suppressed"
                );
            }
            other => panic!(
                "expected original panic when stop suppresses delayed restart, got {other:?}"
            ),
        }

        crate::test_complete!("supervised_actor_stop_during_restart_backoff_prevents_new_instance");
    }

    #[test]
    fn spawn_actor_panic_surfaces_as_task_outcome() {
        init_test("spawn_actor_panic_surfaces_as_task_outcome");

        #[derive(Debug)]
        struct PanicActor;

        impl Actor for PanicActor {
            type Message = ();

            fn handle(
                &mut self,
                _cx: &Cx,
                _msg: (),
            ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
                panic!("actor boom");
            }
        }

        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(root, Budget::INFINITE);

        let (mut handle, mut stored) = scope
            .spawn_actor(&mut state, &cx, PanicActor, 8)
            .expect("spawn actor");
        handle.try_send(()).expect("queue panic message");

        let waker = counting_waker(Arc::new(std::sync::atomic::AtomicUsize::new(0)));
        let mut poll_cx = Context::from_waker(&waker);
        match stored.poll(&mut poll_cx) {
            Poll::Ready(Outcome::Panicked(payload)) => {
                assert_eq!(payload.message(), "actor boom", "panic payload preserved");
            }
            other => panic!("panicking actor task must return Outcome::Panicked: {other:?}"),
        }

        let join = std::pin::pin!(handle.join(&cx));
        let mut join = join;
        match join.as_mut().poll(&mut poll_cx) {
            Poll::Ready(Err(JoinError::Panicked(payload))) => {
                assert_eq!(
                    payload.message(),
                    "actor boom",
                    "join preserves panic payload"
                );
            }
            other => panic!("join must surface actor panic: {other:?}"),
        }

        crate::test_complete!("spawn_actor_panic_surfaces_as_task_outcome");
    }

    #[test]
    fn spawn_supervised_actor_panic_surfaces_as_task_outcome() {
        init_test("spawn_supervised_actor_panic_surfaces_as_task_outcome");

        #[derive(Debug)]
        struct PanicActor;

        impl Actor for PanicActor {
            type Message = ();

            fn handle(
                &mut self,
                _cx: &Cx,
                _msg: (),
            ) -> Pin<Box<dyn Future<Output = ()> + Send + '_>> {
                panic!("supervised actor boom");
            }
        }

        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(root, Budget::INFINITE);

        let (mut handle, mut stored) = scope
            .spawn_supervised_actor(
                &mut state,
                &cx,
                || PanicActor,
                crate::supervision::SupervisionStrategy::Stop,
                8,
            )
            .expect("spawn supervised actor");
        handle.try_send(()).expect("queue panic message");

        let waker = counting_waker(Arc::new(std::sync::atomic::AtomicUsize::new(0)));
        let mut poll_cx = Context::from_waker(&waker);
        match stored.poll(&mut poll_cx) {
            Poll::Ready(Outcome::Panicked(payload)) => {
                assert_eq!(
                    payload.message(),
                    "supervised actor boom",
                    "panic payload preserved"
                );
            }
            other => {
                panic!("panicking supervised actor task must return Outcome::Panicked: {other:?}")
            }
        }

        let join = std::pin::pin!(handle.join(&cx));
        let mut join = join;
        match join.as_mut().poll(&mut poll_cx) {
            Poll::Ready(Err(JoinError::Panicked(payload))) => {
                assert_eq!(
                    payload.message(),
                    "supervised actor boom",
                    "join preserves panic payload"
                );
            }
            other => panic!("join must surface supervised actor panic: {other:?}"),
        }

        crate::test_complete!("spawn_supervised_actor_panic_surfaces_as_task_outcome");
    }

    #[test]
    fn supervised_restart_delay_honors_cancellation() {
        init_test("supervised_restart_delay_honors_cancellation");

        let cx = Cx::for_testing();
        cx.cancel_fast(crate::types::CancelKind::User);

        let mut delay = std::pin::pin!(wait_supervised_restart_delay(
            &cx,
            std::time::Duration::from_secs(60),
        ));
        let first_poll =
            futures_lite::future::block_on(futures_lite::future::poll_once(&mut delay));

        match first_poll {
            Some(Err(JoinError::Cancelled(reason))) => {
                assert_eq!(reason.kind, crate::types::CancelKind::User);
            }
            other => panic!("expected immediate cancellation, got {other:?}"),
        }

        crate::test_complete!("supervised_restart_delay_honors_cancellation");
    }

    /// E2E: Deterministic replay — same seed produces same actor execution.
    #[test]
    fn actor_deterministic_replay() {
        fn run_scenario(seed: u64) -> u64 {
            let config = crate::lab::LabConfig::new(seed);
            let mut runtime = crate::lab::LabRuntime::new(config);
            let region = runtime.state.create_root_region(Budget::INFINITE);
            let cx: Cx = Cx::for_testing();
            let scope = crate::cx::Scope::<FailFast>::new(region, Budget::INFINITE);

            let (on_stop_count, started, stopped) = observable_state();
            let actor = ObservableCounter::new(on_stop_count.clone(), started, stopped);

            let (handle, stored) = scope
                .spawn_actor(&mut runtime.state, &cx, actor, 32)
                .unwrap();
            let task_id = handle.task_id();
            runtime.state.store_spawned_task(task_id, stored);

            for i in 1..=10 {
                handle.try_send(i).unwrap();
            }
            drop(handle);

            runtime.scheduler.lock().schedule(task_id, 0);
            runtime.run_until_quiescent();

            on_stop_count.load(Ordering::SeqCst)
        }

        init_test("actor_deterministic_replay");

        // Run the same scenario twice with the same seed
        let result1 = run_scenario(0xDEAD_BEEF);
        let result2 = run_scenario(0xDEAD_BEEF);

        assert_eq!(
            result1, result2,
            "deterministic replay: same seed → same result"
        );
        assert_eq!(result1, 55, "sum of 1..=10");

        crate::test_complete!("actor_deterministic_replay");
    }

    // ---- ActorContext Tests ----

    #[test]
    fn actor_context_self_reference() {
        init_test("actor_context_self_reference");

        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(root, Budget::INFINITE);

        let (handle, stored) = scope
            .spawn_actor(&mut state, &cx, Counter::new(), 32)
            .unwrap();
        state.store_spawned_task(handle.task_id(), stored);

        // Create an ActorContext using the handle's sender
        let actor_ref = handle.sender();
        let actor_id = handle.actor_id();
        let ctx: ActorContext<'_, u64> = ActorContext::new(&cx, actor_ref, actor_id, None);

        // Test self_actor_id() - doesn't require Clone
        assert_eq!(ctx.self_actor_id(), actor_id);
        assert_eq!(ctx.actor_id(), actor_id);

        crate::test_complete!("actor_context_self_reference");
    }

    #[test]
    fn actor_context_child_management() {
        init_test("actor_context_child_management");

        let cx: Cx = Cx::for_testing();
        let (sender, _receiver) = mpsc::channel::<u64>(32);
        let actor_id = ActorId::from_task(TaskId::new_for_test(1, 1));
        let actor_ref = ActorRef {
            actor_id,
            sender,
            state: Arc::new(ActorStateCell::new(ActorState::Running)),
        };

        let mut ctx = ActorContext::new(&cx, actor_ref, actor_id, None);

        // Initially no children
        assert!(!ctx.has_children());
        assert_eq!(ctx.child_count(), 0);
        assert!(ctx.children().is_empty());

        // Register children
        let child1 = ActorId::from_task(TaskId::new_for_test(2, 1));
        let child2 = ActorId::from_task(TaskId::new_for_test(3, 1));

        ctx.register_child(child1);
        assert!(ctx.has_children());
        assert_eq!(ctx.child_count(), 1);

        ctx.register_child(child2);
        assert_eq!(ctx.child_count(), 2);

        // Unregister child
        assert!(ctx.unregister_child(child1));
        assert_eq!(ctx.child_count(), 1);

        // Unregistering non-existent child returns false
        assert!(!ctx.unregister_child(child1));

        crate::test_complete!("actor_context_child_management");
    }

    #[test]
    fn actor_context_stopping() {
        init_test("actor_context_stopping");

        let cx: Cx = Cx::for_testing();
        let (sender, _receiver) = mpsc::channel::<u64>(32);
        let actor_id = ActorId::from_task(TaskId::new_for_test(1, 1));
        let actor_ref = ActorRef {
            actor_id,
            sender,
            state: Arc::new(ActorStateCell::new(ActorState::Running)),
        };

        let mut ctx = ActorContext::new(&cx, actor_ref, actor_id, None);

        // Initially not stopping
        assert!(!ctx.is_stopping());
        assert!(ctx.checkpoint().is_ok());

        // Request stop
        ctx.stop_self();
        assert!(ctx.is_stopping());
        assert!(ctx.checkpoint().is_err());
        assert!(cx.checkpoint().is_ok());
        assert!(ctx.is_cancel_requested());

        crate::test_complete!("actor_context_stopping");
    }

    #[test]
    fn actor_context_parent_none() {
        init_test("actor_context_parent_none");

        let cx: Cx = Cx::for_testing();
        let (sender, _receiver) = mpsc::channel::<u64>(32);
        let actor_id = ActorId::from_task(TaskId::new_for_test(1, 1));
        let actor_ref = ActorRef {
            actor_id,
            sender,
            state: Arc::new(ActorStateCell::new(ActorState::Running)),
        };

        let ctx = ActorContext::new(&cx, actor_ref, actor_id, None);

        // Root actor has no parent
        assert!(!ctx.has_parent());
        assert!(ctx.parent().is_none());

        crate::test_complete!("actor_context_parent_none");
    }

    #[test]
    fn actor_context_cx_delegation() {
        init_test("actor_context_cx_delegation");

        let cx: Cx = Cx::for_testing();
        let (sender, _receiver) = mpsc::channel::<u64>(32);
        let actor_id = ActorId::from_task(TaskId::new_for_test(1, 1));
        let actor_ref = ActorRef {
            actor_id,
            sender,
            state: Arc::new(ActorStateCell::new(ActorState::Running)),
        };

        let ctx = ActorContext::new(&cx, actor_ref, actor_id, None);

        // Test Cx delegation via Deref
        let _budget = ctx.budget();
        ctx.trace("test_event");

        // Test cx() accessor
        let _cx_ref = ctx.cx();

        crate::test_complete!("actor_context_cx_delegation");
    }

    #[test]
    fn actor_context_debug() {
        init_test("actor_context_debug");

        let cx: Cx = Cx::for_testing();
        let (sender, _receiver) = mpsc::channel::<u64>(32);
        let actor_id = ActorId::from_task(TaskId::new_for_test(1, 1));
        let actor_ref = ActorRef {
            actor_id,
            sender,
            state: Arc::new(ActorStateCell::new(ActorState::Running)),
        };

        let ctx = ActorContext::new(&cx, actor_ref, actor_id, None);

        // Debug formatting should work
        let debug_str = format!("{ctx:?}");
        assert!(debug_str.contains("ActorContext"));
        assert!(debug_str.contains("actor_id"));

        crate::test_complete!("actor_context_debug");
    }

    // ---- Invariant Tests ----

    /// Invariant: `ActorStateCell` encode/decode roundtrips correctly for all
    /// valid states, and unknown u8 values map to `Stopped` (fail-safe).
    #[test]
    fn actor_state_cell_encode_decode_roundtrip() {
        init_test("actor_state_cell_encode_decode_roundtrip");

        let states = [
            ActorState::Created,
            ActorState::Running,
            ActorState::Stopping,
            ActorState::Stopped,
        ];

        for &state in &states {
            let cell = ActorStateCell::new(state);
            let loaded = cell.load();
            crate::assert_with_log!(loaded == state, "roundtrip", state, loaded);
        }

        // Unknown values (4+) should map to Stopped (fail-safe).
        for raw in 4_u8..=10 {
            let decoded = ActorStateCell::decode(raw);
            let is_stopped = decoded == ActorState::Stopped;
            crate::assert_with_log!(is_stopped, "unknown u8 -> Stopped", true, is_stopped);
        }

        crate::test_complete!("actor_state_cell_encode_decode_roundtrip");
    }

    /// Invariant: `MailboxConfig::default()` has documented capacity and
    /// backpressure enabled.
    #[test]
    fn mailbox_config_defaults() {
        init_test("mailbox_config_defaults");

        let config = MailboxConfig::default();
        crate::assert_with_log!(
            config.capacity == DEFAULT_MAILBOX_CAPACITY,
            "default capacity",
            DEFAULT_MAILBOX_CAPACITY,
            config.capacity
        );
        crate::assert_with_log!(
            config.backpressure,
            "backpressure enabled by default",
            true,
            config.backpressure
        );

        let custom = MailboxConfig::with_capacity(8);
        crate::assert_with_log!(
            custom.capacity == 8,
            "custom capacity",
            8usize,
            custom.capacity
        );
        crate::assert_with_log!(
            custom.backpressure,
            "with_capacity enables backpressure",
            true,
            custom.backpressure
        );

        crate::test_complete!("mailbox_config_defaults");
    }

    /// Invariant: `try_send` on a full mailbox returns an error without
    /// blocking, and the message is recoverable from the error.
    #[test]
    fn actor_try_send_full_mailbox_returns_error() {
        init_test("actor_try_send_full_mailbox_returns_error");

        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(root, Budget::INFINITE);

        // Create actor with capacity=2 mailbox.
        let (handle, stored) = scope
            .spawn_actor(&mut state, &cx, Counter::new(), 2)
            .unwrap();
        state.store_spawned_task(handle.task_id(), stored);

        // Fill the mailbox.
        let ok1 = handle.try_send(1).is_ok();
        crate::assert_with_log!(ok1, "first send ok", true, ok1);
        let ok2 = handle.try_send(2).is_ok();
        crate::assert_with_log!(ok2, "second send ok", true, ok2);

        // Third send should fail — mailbox full.
        let result = handle.try_send(3);
        let is_full = result.is_err();
        crate::assert_with_log!(is_full, "third send fails (full)", true, is_full);

        crate::test_complete!("actor_try_send_full_mailbox_returns_error");
    }

    /// Invariant: `ActorContext` with a parent supervisor set exposes it
    /// and reports `has_parent() == true`.
    #[test]
    fn actor_context_with_parent_supervisor() {
        init_test("actor_context_with_parent_supervisor");

        let cx: Cx = Cx::for_testing();

        // Create parent supervisor channel.
        let (parent_sender, _parent_receiver) = mpsc::channel::<SupervisorMessage>(8);
        let parent_id = ActorId::from_task(TaskId::new_for_test(10, 1));
        let parent_ref = ActorRef {
            actor_id: parent_id,
            sender: parent_sender,
            state: Arc::new(ActorStateCell::new(ActorState::Running)),
        };

        // Create child actor context with parent.
        let (child_sender, _child_receiver) = mpsc::channel::<u64>(32);
        let child_id = ActorId::from_task(TaskId::new_for_test(20, 1));
        let child_ref = ActorRef {
            actor_id: child_id,
            sender: child_sender,
            state: Arc::new(ActorStateCell::new(ActorState::Running)),
        };

        let ctx = ActorContext::new(&cx, child_ref, child_id, Some(parent_ref));

        let has_parent = ctx.has_parent();
        crate::assert_with_log!(has_parent, "has parent", true, has_parent);

        let parent = ctx.parent().expect("parent should be Some");
        let parent_id_matches = parent.actor_id() == parent_id;
        crate::assert_with_log!(
            parent_id_matches,
            "parent id matches",
            true,
            parent_id_matches
        );

        crate::test_complete!("actor_context_with_parent_supervisor");
    }

    // ---- Pure Data Type Tests (no runtime needed) ----

    #[test]
    fn actor_id_debug_format() {
        let id = ActorId::from_task(TaskId::new_for_test(5, 3));
        let dbg = format!("{id:?}");
        assert!(dbg.contains("ActorId"), "{dbg}");
    }

    #[test]
    fn actor_id_display_delegates_to_task_id() {
        let tid = TaskId::new_for_test(7, 2);
        let aid = ActorId::from_task(tid);
        assert_eq!(format!("{aid}"), format!("{tid}"));
    }

    #[test]
    fn actor_id_from_task_roundtrip() {
        let tid = TaskId::new_for_test(3, 1);
        let aid = ActorId::from_task(tid);
        assert_eq!(aid.task_id(), tid);
    }

    #[test]
    fn actor_id_copy_clone() {
        let id = ActorId::from_task(TaskId::new_for_test(1, 1));
        let copied = id; // Copy
        let cloned = id;
        assert_eq!(id, copied);
        assert_eq!(id, cloned);
    }

    #[test]
    fn actor_id_hash_consistency() {
        use crate::util::DetHasher;
        use std::hash::{Hash, Hasher};

        let id1 = ActorId::from_task(TaskId::new_for_test(4, 2));
        let id2 = ActorId::from_task(TaskId::new_for_test(4, 2));
        assert_eq!(id1, id2);

        let mut h1 = DetHasher::default();
        let mut h2 = DetHasher::default();
        id1.hash(&mut h1);
        id2.hash(&mut h2);
        assert_eq!(h1.finish(), h2.finish(), "equal IDs must hash equal");
    }

    #[test]
    fn actor_state_debug_all_variants() {
        for (state, expected) in [
            (ActorState::Created, "Created"),
            (ActorState::Running, "Running"),
            (ActorState::Stopping, "Stopping"),
            (ActorState::Stopped, "Stopped"),
        ] {
            let dbg = format!("{state:?}");
            assert_eq!(dbg, expected, "ActorState::{expected}");
        }
    }

    #[test]
    fn actor_state_clone_copy_eq() {
        let s = ActorState::Running;
        let copied = s;
        let cloned = s;
        assert_eq!(s, copied);
        assert_eq!(s, cloned);
    }

    #[test]
    fn actor_state_exhaustive_inequality() {
        let all = [
            ActorState::Created,
            ActorState::Running,
            ActorState::Stopping,
            ActorState::Stopped,
        ];
        for (i, a) in all.iter().enumerate() {
            for (j, b) in all.iter().enumerate() {
                if i == j {
                    assert_eq!(a, b);
                } else {
                    assert_ne!(a, b);
                }
            }
        }
    }

    #[test]
    fn actor_state_cell_sequential_transitions() {
        let cell = ActorStateCell::new(ActorState::Created);
        assert_eq!(cell.load(), ActorState::Created);

        cell.store(ActorState::Running);
        assert_eq!(cell.load(), ActorState::Running);

        cell.store(ActorState::Stopping);
        assert_eq!(cell.load(), ActorState::Stopping);

        cell.store(ActorState::Stopped);
        assert_eq!(cell.load(), ActorState::Stopped);
    }

    #[test]
    fn supervisor_message_debug_child_failed() {
        let msg = SupervisorMessage::ChildFailed {
            child_id: ActorId::from_task(TaskId::new_for_test(1, 1)),
            reason: "panicked".to_string(),
        };
        let dbg = format!("{msg:?}");
        assert!(dbg.contains("ChildFailed"), "{dbg}");
        assert!(dbg.contains("panicked"), "{dbg}");
    }

    #[test]
    fn supervisor_message_debug_child_stopped() {
        let msg = SupervisorMessage::ChildStopped {
            child_id: ActorId::from_task(TaskId::new_for_test(2, 1)),
        };
        let dbg = format!("{msg:?}");
        assert!(dbg.contains("ChildStopped"), "{dbg}");
    }

    #[test]
    fn supervisor_message_clone() {
        let msg = SupervisorMessage::ChildFailed {
            child_id: ActorId::from_task(TaskId::new_for_test(1, 1)),
            reason: "boom".to_string(),
        };
        let cloned = msg.clone();
        let (a, b) = (format!("{msg:?}"), format!("{cloned:?}"));
        assert_eq!(a, b);
    }

    #[test]
    fn supervised_outcome_debug_all_variants() {
        let variants: Vec<SupervisedOutcome> = vec![
            SupervisedOutcome::Stopped,
            SupervisedOutcome::RestartBudgetExhausted { total_restarts: 5 },
            SupervisedOutcome::Escalated,
        ];
        for v in &variants {
            let dbg = format!("{v:?}");
            assert!(!dbg.is_empty());
        }
        assert!(format!("{variants0:?}", variants0 = variants[0]).contains("Stopped"));
        assert!(format!("{variants1:?}", variants1 = variants[1]).contains('5'));
        assert!(format!("{variants2:?}", variants2 = variants[2]).contains("Escalated"));
    }

    #[test]
    fn mailbox_config_debug_clone_copy() {
        let cfg = MailboxConfig::default();
        let dbg = format!("{cfg:?}");
        assert!(dbg.contains("MailboxConfig"), "{dbg}");
        assert!(dbg.contains("64"), "{dbg}");

        let copied = cfg;
        let cloned = cfg;
        assert_eq!(copied.capacity, cfg.capacity);
        assert_eq!(cloned.backpressure, cfg.backpressure);
    }

    #[test]
    fn mailbox_config_zero_capacity() {
        let cfg = MailboxConfig::with_capacity(0);
        assert_eq!(cfg.capacity, 0);
        assert!(cfg.backpressure);
    }

    #[test]
    fn mailbox_config_max_capacity() {
        let cfg = MailboxConfig::with_capacity(usize::MAX);
        assert_eq!(cfg.capacity, usize::MAX);
    }

    #[test]
    fn default_mailbox_capacity_is_64() {
        assert_eq!(DEFAULT_MAILBOX_CAPACITY, 64);
    }

    #[test]
    fn actor_context_duplicate_child_registration() {
        let cx: Cx = Cx::for_testing();
        let (sender, _receiver) = mpsc::channel::<u64>(32);
        let actor_id = ActorId::from_task(TaskId::new_for_test(1, 1));
        let actor_ref = ActorRef {
            actor_id,
            sender,
            state: Arc::new(ActorStateCell::new(ActorState::Running)),
        };

        let mut ctx = ActorContext::new(&cx, actor_ref, actor_id, None);
        let child = ActorId::from_task(TaskId::new_for_test(2, 1));

        ctx.register_child(child);
        ctx.register_child(child); // duplicate
        assert_eq!(ctx.child_count(), 2, "register_child does not dedup");

        // Unregister removes first occurrence
        assert!(ctx.unregister_child(child));
        assert_eq!(ctx.child_count(), 1, "one copy remains");
        assert!(ctx.unregister_child(child));
        assert_eq!(ctx.child_count(), 0);
        assert!(!ctx.unregister_child(child), "nothing left to remove");
    }

    #[test]
    fn actor_context_stop_self_is_idempotent() {
        let cx: Cx = Cx::for_testing();
        let (sender, _receiver) = mpsc::channel::<u64>(32);
        let actor_id = ActorId::from_task(TaskId::new_for_test(1, 1));
        let actor_ref = ActorRef {
            actor_id,
            sender,
            state: Arc::new(ActorStateCell::new(ActorState::Running)),
        };

        let mut ctx = ActorContext::new(&cx, actor_ref, actor_id, None);
        ctx.stop_self();
        assert!(ctx.is_stopping());
        ctx.stop_self(); // idempotent
        assert!(ctx.is_stopping());
    }

    #[test]
    fn actor_context_self_ref_returns_working_ref() {
        let cx: Cx = Cx::for_testing();
        let (sender, _receiver) = mpsc::channel::<u64>(32);
        let actor_id = ActorId::from_task(TaskId::new_for_test(1, 1));
        let actor_ref = ActorRef {
            actor_id,
            sender,
            state: Arc::new(ActorStateCell::new(ActorState::Running)),
        };

        let ctx = ActorContext::new(&cx, actor_ref, actor_id, None);
        let self_ref = ctx.self_ref();
        assert_eq!(self_ref.actor_id(), actor_id);
        assert!(self_ref.is_alive());
    }

    #[test]
    fn actor_context_deadline_reflects_budget() {
        let cx: Cx = Cx::for_testing();
        let (sender, _receiver) = mpsc::channel::<u64>(32);
        let actor_id = ActorId::from_task(TaskId::new_for_test(1, 1));
        let actor_ref = ActorRef {
            actor_id,
            sender,
            state: Arc::new(ActorStateCell::new(ActorState::Running)),
        };

        let ctx = ActorContext::new(&cx, actor_ref, actor_id, None);
        // for_testing() Cx has INFINITE budget, which has no deadline
        assert!(ctx.deadline().is_none());
    }

    #[test]
    fn actor_handle_debug() {
        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(root, Budget::INFINITE);

        let (handle, stored) = scope
            .spawn_actor(&mut state, &cx, Counter::new(), 32)
            .unwrap();
        state.store_spawned_task(handle.task_id(), stored);

        let dbg = format!("{handle:?}");
        assert!(dbg.contains("ActorHandle"), "{dbg}");
    }

    #[test]
    fn actor_handle_second_join_fails_closed() {
        init_test("actor_handle_second_join_fails_closed");

        let mut runtime = crate::lab::LabRuntime::new(crate::lab::LabConfig::default());
        let region = runtime.state.create_root_region(Budget::INFINITE);
        let cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(region, Budget::INFINITE);

        let (mut handle, stored) = scope
            .spawn_actor(&mut runtime.state, &cx, Counter::new(), 32)
            .unwrap();
        let task_id = handle.task_id();
        runtime.state.store_spawned_task(task_id, stored);

        handle.stop();
        runtime.scheduler.lock().schedule(task_id, 0);
        runtime.run_until_quiescent();
        assert!(handle.is_finished(), "stopped actor should report finished");

        let final_state = futures_lite::future::block_on(handle.join(&cx)).expect("first join");
        assert_eq!(final_state.count, 0, "join should return final actor state");

        let second = futures_lite::future::block_on(handle.join(&cx));
        assert!(
            matches!(second, Err(JoinError::PolledAfterCompletion)),
            "second join must fail closed, got {second:?}"
        );

        crate::test_complete!("actor_handle_second_join_fails_closed");
    }

    #[test]
    fn actor_ref_debug() {
        let mut state = RuntimeState::new();
        let root = state.create_root_region(Budget::INFINITE);
        let cx: Cx = Cx::for_testing();
        let scope = crate::cx::Scope::<FailFast>::new(root, Budget::INFINITE);

        let (handle, stored) = scope
            .spawn_actor(&mut state, &cx, Counter::new(), 32)
            .unwrap();
        state.store_spawned_task(handle.task_id(), stored);

        let actor_ref = handle.sender();
        let dbg = format!("{actor_ref:?}");
        assert!(dbg.contains("ActorRef"), "{dbg}");
    }

    #[test]
    fn actor_state_cell_debug() {
        let cell = ActorStateCell::new(ActorState::Running);
        let dbg = format!("{cell:?}");
        assert!(dbg.contains("ActorStateCell"), "{dbg}");
    }

    #[test]
    fn actor_id_clone_copy_eq_hash() {
        use std::collections::HashSet;

        let id = ActorId::from_task(TaskId::new_for_test(1, 0));
        let dbg = format!("{id:?}");
        assert!(dbg.contains("ActorId"));

        let id2 = id;
        assert_eq!(id, id2);

        // Copy
        let id3 = id;
        assert_eq!(id, id3);

        // Hash
        let mut set = HashSet::new();
        set.insert(id);
        set.insert(ActorId::from_task(TaskId::new_for_test(2, 0)));
        assert_eq!(set.len(), 2);
    }

    #[test]
    fn actor_state_debug_clone_copy_eq() {
        let s = ActorState::Running;
        let dbg = format!("{s:?}");
        assert!(dbg.contains("Running"));

        let s2 = s;
        assert_eq!(s, s2);

        let s3 = s;
        assert_eq!(s, s3);

        assert_ne!(ActorState::Created, ActorState::Stopped);
    }

    #[test]
    fn mailbox_config_debug_clone_copy_default() {
        let c = MailboxConfig::default();
        let dbg = format!("{c:?}");
        assert!(dbg.contains("MailboxConfig"));

        let c2 = c;
        assert_eq!(c2.capacity, c.capacity);
        assert_eq!(c2.backpressure, c.backpressure);

        // Copy
        let c3 = c;
        assert_eq!(c3.capacity, c.capacity);
    }
}