aws-durable-execution-sdk 1.0.0

Rust SDK for AWS Lambda Durable Functions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
21211
21212
21213
21214
21215
21216
21217
21218
21219
21220
21221
21222
21223
21224
21225
21226
21227
21228
21229
21230
21231
21232
21233
21234
21235
21236
21237
21238
21239
21240
21241
21242
21243
21244
21245
21246
21247
21248
21249
21250
21251
21252
21253
21254
21255
21256
21257
21258
21259
21260
21261
21262
21263
21264
21265
21266
21267
21268
21269
21270
21271
21272
21273
21274
21275
21276
21277
21278
21279
21280
21281
21282
21283
21284
21285
21286
21287
21288
21289
21290
21291
21292
21293
21294
21295
21296
21297
21298
21299
21300
21301
21302
21303
21304
21305
21306
21307
21308
21309
21310
21311
21312
21313
21314
21315
21316
21317
21318
21319
21320
21321
21322
21323
21324
21325
21326
21327
21328
21329
21330
21331
21332
21333
21334
21335
21336
21337
21338
21339
21340
21341
21342
21343
21344
21345
21346
21347
21348
21349
21350
21351
21352
21353
21354
21355
21356
21357
21358
21359
21360
21361
21362
21363
21364
21365
21366
21367
21368
21369
21370
21371
21372
21373
21374
21375
21376
21377
21378
21379
21380
21381
21382
21383
21384
21385
21386
21387
21388
21389
21390
21391
21392
21393
21394
21395
21396
21397
21398
21399
21400
21401
21402
21403
21404
21405
21406
21407
21408
21409
21410
21411
21412
21413
21414
21415
21416
21417
21418
21419
21420
21421
21422
21423
21424
21425
21426
21427
21428
21429
21430
21431
21432
21433
21434
21435
21436
21437
21438
21439
21440
21441
21442
21443
21444
21445
21446
21447
21448
21449
21450
21451
21452
21453
21454
21455
21456
21457
21458
21459
21460
21461
21462
21463
21464
21465
21466
21467
21468
21469
21470
21471
21472
21473
21474
21475
21476
21477
21478
21479
21480
21481
21482
21483
21484
21485
21486
21487
21488
21489
21490
21491
21492
21493
21494
21495
21496
21497
21498
21499
21500
21501
21502
21503
21504
21505
21506
21507
21508
21509
21510
21511
21512
21513
21514
21515
21516
21517
21518
21519
21520
21521
21522
21523
21524
21525
21526
21527
21528
21529
21530
21531
21532
21533
21534
21535
21536
21537
21538
21539
21540
21541
21542
21543
21544
21545
21546
21547
21548
21549
21550
21551
21552
21553
21554
21555
21556
21557
21558
21559
21560
21561
21562
21563
21564
21565
21566
21567
21568
21569
21570
21571
21572
21573
21574
21575
21576
21577
21578
21579
21580
21581
21582
21583
21584
21585
21586
21587
21588
21589
21590
21591
21592
21593
21594
21595
21596
21597
21598
21599
21600
21601
21602
21603
21604
21605
21606
21607
21608
21609
21610
21611
21612
21613
21614
21615
21616
21617
21618
21619
21620
21621
21622
21623
21624
21625
21626
21627
21628
21629
21630
21631
21632
21633
21634
21635
21636
21637
21638
21639
21640
21641
21642
21643
21644
21645
21646
21647
21648
21649
21650
21651
21652
21653
21654
21655
21656
21657
21658
21659
21660
21661
21662
21663
21664
21665
21666
21667
21668
21669
21670
21671
21672
21673
21674
21675
21676
21677
21678
21679
21680
21681
21682
21683
21684
21685
21686
21687
21688
21689
21690
21691
21692
21693
21694
21695
21696
21697
21698
21699
21700
21701
21702
21703
21704
21705
21706
21707
21708
21709
21710
21711
21712
21713
21714
21715
21716
21717
21718
21719
21720
21721
21722
21723
21724
21725
21726
21727
21728
21729
21730
21731
21732
21733
21734
21735
21736
21737
21738
21739
21740
21741
21742
21743
21744
21745
21746
21747
21748
21749
21750
21751
21752
21753
21754
21755
21756
21757
21758
21759
21760
21761
21762
21763
21764
21765
21766
21767
21768
21769
21770
21771
21772
21773
21774
21775
21776
21777
21778
21779
21780
21781
21782
21783
21784
21785
21786
21787
21788
21789
21790
21791
21792
21793
21794
21795
21796
21797
21798
21799
21800
21801
21802
21803
21804
21805
21806
21807
21808
21809
21810
21811
21812
21813
21814
21815
21816
21817
21818
21819
21820
21821
21822
21823
21824
21825
21826
21827
21828
21829
21830
21831
21832
21833
21834
21835
21836
21837
21838
21839
21840
21841
21842
21843
21844
21845
21846
21847
21848
21849
21850
21851
21852
21853
21854
21855
21856
21857
21858
21859
21860
21861
21862
21863
21864
21865
21866
21867
21868
21869
21870
21871
21872
21873
21874
21875
21876
21877
21878
21879
21880
21881
21882
21883
21884
21885
21886
21887
21888
21889
21890
21891
21892
21893
21894
21895
21896
21897
21898
21899
21900
21901
21902
21903
21904
21905
21906
21907
21908
21909
21910
21911
21912
21913
21914
21915
21916
21917
21918
21919
21920
21921
21922
21923
21924
21925
21926
21927
21928
21929
21930
21931
21932
21933
21934
21935
21936
21937
21938
21939
21940
21941
21942
21943
21944
21945
21946
21947
21948
21949
21950
21951
21952
21953
21954
21955
21956
21957
21958
21959
21960
21961
21962
21963
21964
21965
21966
21967
21968
21969
21970
21971
21972
21973
21974
21975
21976
21977
21978
21979
21980
21981
21982
21983
21984
21985
21986
21987
21988
21989
21990
21991
21992
21993
21994
21995
21996
21997
21998
21999
22000
22001
22002
22003
22004
22005
22006
22007
22008
22009
22010
22011
22012
22013
22014
22015
22016
22017
22018
22019
22020
22021
22022
22023
22024
22025
22026
22027
22028
22029
22030
22031
22032
22033
22034
22035
22036
22037
22038
22039
22040
22041
22042
22043
22044
22045
22046
22047
22048
22049
22050
22051
22052
22053
22054
22055
22056
22057
22058
22059
22060
22061
22062
22063
22064
22065
22066
22067
22068
22069
22070
22071
22072
22073
22074
22075
22076
22077
22078
22079
22080
22081
22082
22083
22084
22085
22086
22087
22088
22089
22090
22091
22092
22093
22094
22095
22096
22097
22098
22099
22100
22101
22102
22103
22104
22105
22106
22107
22108
22109
22110
22111
22112
22113
22114
22115
22116
22117
22118
22119
22120
22121
22122
22123
22124
22125
22126
22127
22128
22129
22130
22131
22132
22133
22134
22135
22136
22137
22138
22139
22140
22141
22142
22143
22144
22145
22146
22147
22148
22149
22150
22151
22152
22153
22154
22155
22156
22157
22158
22159
22160
22161
22162
22163
22164
22165
22166
22167
22168
22169
22170
22171
22172
22173
22174
22175
22176
22177
22178
22179
22180
22181
22182
22183
22184
22185
22186
22187
22188
22189
22190
22191
22192
22193
22194
22195
22196
22197
22198
22199
22200
22201
22202
22203
22204
22205
22206
22207
22208
22209
22210
22211
22212
22213
22214
22215
22216
22217
22218
22219
22220
22221
22222
22223
22224
22225
22226
22227
22228
22229
22230
22231
22232
22233
22234
22235
22236
22237
22238
22239
22240
22241
22242
22243
22244
22245
22246
22247
22248
22249
22250
22251
22252
22253
22254
22255
22256
22257
22258
22259
22260
22261
22262
22263
22264
22265
22266
22267
22268
22269
22270
22271
22272
22273
22274
22275
22276
22277
22278
22279
22280
22281
22282
22283
22284
22285
22286
22287
22288
22289
22290
22291
22292
22293
22294
22295
22296
22297
22298
22299
22300
22301
22302
22303
22304
22305
22306
22307
22308
22309
//! In-process local testing runner (`test-util` feature).
//!
//! [`LocalRunner`] drives a durable handler to completion entirely in
//! memory: each simulated invocation goes through the **same service
//! function production uses**: the one [`wrap`](crate::wrap) builds:
//! fed a synthesized invocation envelope and backed by an internal
//! in-memory execution client instead of the Lambda checkpoint API.
//! Envelope parsing, bootstrap pagination, the suspension driver, wire
//! error mapping, and the response envelope are therefore the production
//! code paths, not a parallel reimplementation. When the handler
//! suspends, the runner advances the simulated backend (timers fire,
//! retry delays elapse, callbacks are delivered) and re-invokes, exactly
//! as the real service would, until the execution reaches a terminal
//! outcome.
//!
//! The runner also reproduces the **production task topology**: the
//! service future is awaited inline (under the caller's `block_on`, as
//! `lambda_runtime` does), never on a spawned task, so
//! `tokio::task::try_id()` is `None` at context-creation time and the
//! task-ownership guard behaves exactly as it does in deployment.
//!
//! By default the in-memory backend serves execution state in **multiple
//! pages** (2+ whenever the recorded history is large enough), matching
//! the paginating service; [`LocalRunner::single_page`] opts into the
//! single-page special case.
//!
//! The runner records every checkpointed operation so a test can assert on
//! the execution history via [`TestResult::operations`] and
//! [`TestOperation`].
//!
//! [`CloudRunner`] is the real-AWS counterpart: it invokes a deployed durable
//! function, polls the execution to a terminal state, and folds the recorded
//! service history into the SAME [`TestResult`] / [`TestOperation`] types, so a
//! test written against [`LocalRunner`] can be re-pointed at real AWS with only
//! the runner swapped.
//!
//! # Backend fidelity
//!
//! The in-memory client reproduces the behaviours the SDK depends on,
//! traced to this crate's own engine:
//!
//! - Checkpoint responses return whole-record operation updates that the
//!   SDK merges into its in-memory log (`client::merge_operations_into_log`
//!   → `CheckpointLog::insert` whole-record overwrite). A `Start` update
//!   carries no payload, so the merged record clears any carried result:
//!   the exact condition the `wait_for_condition` read-before-`Start` fix
//!   guards against.
//! - The backend keeps a virtual clock in whole seconds. It starts at 0
//!   for each run. A wait is due at the clock time it started plus its
//!   duration, and a retry is due at the clock time it was recorded plus
//!   its delay. Between invocations, each advance moves the clock to the
//!   earliest pending wait or retry end and fires every wait and retry due
//!   by then. So waits and retries end in time order, as on the service.
//! - Queued callback replies arrive before the clock moves. An advance that
//!   delivers a callback reply fires no timer.
//! - A chained invoke runs its registered target between invocations,
//!   before the virtual clock moves (see [`LocalRunner::register_function`]
//!   and [`LocalRunner::register_durable_function`]).
//! - Callback replies are queued before the run, or sent while it runs
//!   through [`OperationHandle::send_callback`]. The runner runs no
//!   callback timeout or heartbeat timer; [`CallbackReply::timeout`] and
//!   [`CallbackReply::heartbeat_timeout`] deliver the records those timers
//!   produce.
//! - `LocalRunner` moves time only between invocations. On the service, a
//!   wait or a condition check can end while other work runs, and the SDK
//!   continues it in the same invocation. Locally its record never changes
//!   during an invocation. So a local run can take more invocations than
//!   the same execution takes on the service.
//! - [`LocalRunner::checkpoint_latency`] delays each checkpoint response in
//!   tokio time. It does not move the virtual clock.
//! - The backend stores each operation's `ParentId` and the `ReplayChildren`
//!   option of a completed context. It omits the children of a completed
//!   context from the invocation input and from `GetDurableExecutionState`,
//!   as the service does, unless the context recorded `ReplayChildren`.
//!   Checkpoint responses and [`TestResult::operations`] keep every record.
//! - The backend rejects a checkpoint that exceeds a service size limit or
//!   a delay range, with the service's message.
//! - The backend rejects a chained-invoke `START` whose function name has
//!   no target registered on the runner, and an update whose operation
//!   type it does not know. Neither rejection changes any state.
//! - The backend also checks a checkpoint against the history rules of the
//!   JS SDK's test backend, and rejects it with that backend's message. It
//!   checks the whole request against the operations stored before it, and
//!   a rejected request changes no operation, no token and no history
//!   event. The rules are:
//!   - An update must not change the `Type`, `SubType`, `Name` or
//!     `ParentId` of a stored operation. A field the update omits passes.
//!   - The action must fit the type. `STEP` allows `START`, `FAIL`, `RETRY`
//!     and `SUCCEED`; `CONTEXT` allows `START`, `FAIL` and `SUCCEED`; `WAIT`
//!     allows `START` and `CANCEL`; `CALLBACK` and `CHAINED_INVOKE` allow only
//!     `START`; `EXECUTION` allows `SUCCEED` and `FAIL`.
//!   - The action must fit the stored status. A stored `STEP` may `START`
//!     only when `READY`, and may `FAIL`, `SUCCEED` or `RETRY` only when
//!     `STARTED` or `READY`. A stored `CONTEXT` cannot `START` again, and may
//!     close only when `STARTED`. A `WAIT` may `START` only when it is not
//!     stored, and `CANCEL` only when it is `STARTED`. A `CALLBACK` or
//!     `CHAINED_INVOKE` may `START` only when it is not stored.
//!   - A `ParentId` must name a stored `CONTEXT`, or a `CONTEXT` that an
//!     earlier update of the same request starts.
//!   - An ID may appear twice in one request only when the earlier update
//!     is a `STEP` or `CONTEXT` `START` and the later one is not a `START`.
//!
//!   It does not check the rest of the service's update model: the request
//!   model, the ID and name patterns, the options and payloads each action
//!   requires or forbids, and the count and position of `EXECUTION`
//!   updates.
//!
//!   The SDK treats a rejection as any other non-retryable checkpoint
//!   failure. So the execution fails with the error type
//!   `CheckpointUnrecoverableExecutionError`, and a test fails where the SDK
//!   would write a history that the JS SDK's test backend rejects.
//! - Each invocation input carries `UpdatedOperationIds`: the operations
//!   the backend changed between invocations, such as a fired wait, a
//!   retry that became ready, or a callback that received a reply.
//! - The backend records a history event for each operation update it
//!   accepts, for each wait, callback and chained invoke it settles, for
//!   each invocation, and for the start and the end of the execution (see
//!   [`TestResult::history_events`]). Invocation `n` has the request ID
//!   `local-invocation-{n}`.
//! - The backend reports operation start and end times as the run's start
//!   time plus virtual seconds. The invocation envelope carries them as
//!   epoch milliseconds, as the service does.
//!
//! # Runner divergences
//!
//! [`LocalRunner`] and [`CloudRunner`] return the same types, and a test can
//! be re-pointed between them by swapping the runner. The two agree on
//! operation type, status, result, error, parent, [`StepDetails::attempt`]
//! (both report completed retries), [`WaitDetails::duration`],
//! [`CallbackDetails::callback_id`], and the [`ExecutionStatus`] of a
//! succeeded or failed execution. A few behaviours still differ because one
//! runner observes an in-memory model and the other observes the recorded
//! execution history; assert around these when a test targets both runners:
//!
//! - **Truncated results.** When a result exceeds the recorded-history inline
//!   size limit, [`CloudRunner`] reports [`TestOperation::result`] as `None`
//!   (the payload is withheld); [`LocalRunner`] always holds the full
//!   in-memory result.
//! - **Timed-out / stopped executions.** [`CloudRunner`] reports a terminal
//!   `TimedOut` or `Stopped` execution as [`ExecutionStatus::TimedOut`] or
//!   [`ExecutionStatus::Stopped`], with the status name (or the recorded
//!   error) as the error type; [`LocalRunner`] has no execution timeout and
//!   no stop, so it reports only `Succeeded`, `Failed` or `Running`.
//! - **Transient operation statuses.** [`LocalRunner`] may surface the
//!   intermediate [`OperationStatus::Ready`] status it uses to model an
//!   elapsed retry delay; [`CloudRunner`]'s status vocabulary comes from the
//!   event stream and does not include [`OperationStatus::Ready`].
//! - **Replay children.** [`ContextDetails::replay_children`] is reported
//!   only by [`LocalRunner`]. No history event carries it, so under
//!   [`CloudRunner`] it is `false`.
//! - **Times.** [`LocalRunner`] times come from the virtual clock, starting
//!   at the time the run started: each reported time is the run's start time
//!   plus virtual seconds. [`CloudRunner`] times are the timestamps of the
//!   recorded history events.
//!
//! # Reacting to a run in progress
//!
//! [`LocalRunner::run`] and [`CloudRunner::run`] return a [`RunHandle`].
//! Before awaiting it, a test can take an [`OperationHandle`] with
//! [`RunHandle::operation`], wait until the operation reaches a
//! [`WaitingStatus`] with [`OperationHandle::wait_for_data`], read it, and
//! answer its callback with [`OperationHandle::send_callback`]. Under
//! [`LocalRunner`] it can also [`pause`](RunHandle::pause) and
//! [`resume`](RunHandle::resume) the run.
//!
//! The run moves on only while the test awaits a runner future: the
//! `RunHandle`, `wait_for_data`, or `pause`. So after `wait_for_data`
//! returns, the test can read the operation and reply before the run moves.
//! Await the operation controls first and the `RunHandle` last, as JS tests
//! do. While the `RunHandle` itself is awaited, the run does not wait for
//! other test code.
//!
//! Under [`LocalRunner`], a checkpoint that the handler sends while no
//! runner future waits is answered only once one waits, and no invocation
//! starts and the clock does not move meanwhile. [`CloudRunner`] cannot hold
//! the deployed function. It supports `wait_for_data` and the callback
//! controls only with [`InvocationType::Event`], and reads the whole history
//! every [`poll_interval`](CloudRunner::poll_interval). It makes up to three
//! attempts per `GetDurableExecution` call and per history page request, as
//! JS does.
//!
//! This test pauses a run while a step runs and while it waits for a
//! callback:
//!
//! ```
//! use aws_durable_execution_sdk as durable;
//! use durable::test_util::{CallbackReply, LocalRunner, WaitingStatus};
//! use std::time::Duration;
//!
//! async fn handler(
//!     event: serde_json::Value,
//!     ctx: durable::DurableContext,
//! ) -> Result<serde_json::Value, durable::BoxError> {
//!     let order_id = event
//!         .get("orderId")
//!         .and_then(serde_json::Value::as_str)
//!         .unwrap_or("order-1")
//!         .to_owned();
//!     let id = order_id.clone();
//!     // Slow enough that a test can pause while it runs. The pause takes
//!     // effect when this step checkpoints its result: that checkpoint is
//!     // kept, but the invocation stops there.
//!     let reservation = ctx
//!         .step(move |_| async move {
//!             tokio::time::sleep(Duration::from_millis(200)).await;
//!             Ok(format!("reserved-{id}"))
//!         })
//!         .name("reserve-stock")
//!         .await?;
//!     let approval = ctx
//!         .wait_for_callback::<String>(|_step_ctx, _callback_id| async {
//!             // In a real workflow, the callback ID would be sent to an
//!             // approver here.
//!             Ok(())
//!         })
//!         .name("manager-approval")
//!         .await?;
//!     let shipment = ctx
//!         .step(move |_| async move { Ok(format!("shipped-{order_id}")) })
//!         .name("ship-order")
//!         .await?;
//!     Ok(serde_json::json!({
//!         "reservation": reservation,
//!         "approval": approval,
//!         "shipment": shipment,
//!     }))
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), durable::BoxError> {
//! // Pause while "reserve-stock" runs. `pause` returns once the invocation
//! // stopped, which is when the step checkpoints its result.
//! let mut run = LocalRunner::new().run(handler, serde_json::json!({"orderId": "order-7"}));
//! let reserve_stock = run.operation("reserve-stock");
//! reserve_stock.wait_for_data(WaitingStatus::Started).await?;
//! run.pause().await?;
//!
//! // The step's result was kept, but the invocation stopped there: the
//! // approval was never requested, and the run does not finish while
//! // paused.
//! assert!(reserve_stock.snapshot().is_some_and(|op| op.succeeded()));
//! assert!(run.operation("manager-approval").snapshot().is_none());
//! assert!(tokio::time::timeout(Duration::from_millis(50), &mut run).await.is_err());
//!
//! // Resuming starts a new invocation, which replays "reserve-stock" and
//! // carries on to the approval.
//! run.resume().await?;
//! let approval = run.operation("manager-approval");
//! approval.wait_for_data(WaitingStatus::Submitted).await?;
//! approval.send_callback(CallbackReply::success(&"approved")?).await?;
//!
//! let result = run.await?;
//! assert_eq!(
//!     result.output(),
//!     Some(&serde_json::json!({
//!         "reservation": "reserved-order-7",
//!         "approval": "approved",
//!         "shipment": "shipped-order-7",
//!     }))
//! );
//!
//! // A pause while the run waits for its approval returns at once. The
//! // reply can still be sent, but the invocation it would start waits for
//! // `resume`.
//! let mut run = LocalRunner::new().run(handler, serde_json::json!({"orderId": "order-8"}));
//! let approval = run.operation("manager-approval");
//! approval.wait_for_data(WaitingStatus::Submitted).await?;
//! run.pause().await?;
//! approval.send_callback(CallbackReply::success(&"approved")?).await?;
//! assert!(tokio::time::timeout(Duration::from_millis(50), &mut run).await.is_err());
//! assert!(run.operation("ship-order").snapshot().is_none());
//!
//! run.resume().await?;
//! let result = run.await?;
//! assert_eq!(
//!     result.output(),
//!     Some(&serde_json::json!({
//!         "reservation": "reserved-order-8",
//!         "approval": "approved",
//!         "shipment": "shipped-order-8",
//!     }))
//! );
//! # Ok(())
//! # }
//! ```
//!
//! Runner failures (serialization, an unexpected envelope, an AWS error) are
//! returned as [`TestRunnerError`]. A [`TestResult`] always describes the
//! durable execution, including one that is still
//! [`Running`](ExecutionStatus::Running).
//!
//! # Examples
//!
//! ```
//! use aws_durable_execution_sdk as durable;
//! use durable::test_util::LocalRunner;
//!
//! # #[tokio::main]
//! # async fn main() {
//! let result = LocalRunner::new()
//!     .run(
//!         |event: i32, ctx: durable::DurableContext| async move {
//!             let doubled = ctx.step(move |_| async move { Ok(event * 2) }).await?;
//!             Ok::<_, durable::BoxError>(doubled)
//!         },
//!         21_i32,
//!     )
//!     .await.expect("runner");
//!
//! assert!(result.is_success());
//! assert_eq!(result.output(), Some(&42));
//! # }
//! ```

use std::collections::{BTreeMap, BTreeSet};
use std::pin::Pin;
use std::sync::{Arc, Mutex};

use serde::Serialize;
use serde::de::DeserializeOwned;

use aws_sdk_lambda::primitives::{Blob, DateTime};
use aws_sdk_lambda::types::{
    CallbackDetails as WireCallbackDetails, ChainedInvokeDetails,
    ContextDetails as WireContextDetails, ContextOptions, ErrorObject, Event, EventError,
    EventInput, EventResult, EventType, ExecutionStatus as WireExecutionStatus,
    InvocationCompletedDetails, InvocationType as WireInvocationType, Operation, OperationAction,
    OperationStatus as WireOperationStatus, OperationType, OperationUpdate, RetryDetails,
    StepDetails as WireStepDetails, builders::EventBuilder,
};

use crate::client::{CheckpointOutput, ExecutionClient, ExecutionClientError, GetStateOutput};
use crate::context::DurableContext;
use crate::{BoxError, OperationKind, OperationStatus};

mod live;

use self::live::{
    CloudControl, CloudShared, Engine, LiveState, LocalPhase, RunCore, RunState, Selector,
    callback_target, wake,
};
pub use self::live::{OperationHandle, RunHandle, WaitingStatus};
/// Default cap on the number of invocations the runner will drive before
/// declaring the execution stuck. Generous enough for deep timer/retry
/// chains, low enough to fail a non-terminating handler (e.g. a
/// `wait_for_condition` that never advances) instead of looping forever.
const DEFAULT_MAX_INVOCATIONS: usize = 100;

/// Default page size for simulated execution-state pagination. `1` maximizes
/// fidelity pressure: any history of two or more operations is served as 2+
/// pages (inline first page plus a `get_state` fetch), so every non-trivial
/// test exercises the bootstrap and checkpoint pagination paths by default:
/// the dimension where a missing-pagination defect was previously untestable
/// by construction.
const DEFAULT_STATE_PAGE_SIZE: usize = 1;

/// A single checkpointed operation recorded during a [`LocalRunner`] run.
///
/// Returned in order by [`TestResult::operations`]. All accessors return
/// borrowed views; construct these only through the runner.
///
/// # Examples
///
/// ```
/// use aws_durable_execution_sdk as durable;
/// use durable::test_util::LocalRunner;
///
/// # #[tokio::main]
/// # async fn main() {
/// let result = LocalRunner::new()
///     .run(
///         |_e: (), ctx: durable::DurableContext| async move {
///             ctx.step(|_| async { Ok(1_i32) }).name("only").await?;
///             Ok::<_, durable::BoxError>(())
///         },
///         (),
///     )
///     .await.expect("runner");
///
/// let ops = result.operations();
/// assert_eq!(ops.len(), 1);
/// assert_eq!(ops[0].kind(), durable::OperationKind::Step);
/// assert!(ops[0].succeeded());
/// assert_eq!(ops[0].name(), Some("only"));
/// # }
/// ```
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TestOperation {
    id: String,
    kind: RunnerKind,
    sub_type: Option<String>,
    status: OperationStatus,
    result: Option<String>,
    error: Option<crate::WireError>,
    name: Option<String>,
    parent_id: Option<String>,
    start_time: Option<std::time::SystemTime>,
    end_time: Option<std::time::SystemTime>,
    // The detail of every kind is stored, and `details()` returns the one
    // of `kind`. So a runner fills a kind's field without matching on the
    // kind first.
    step: StepDetails,
    wait: WaitDetails,
    callback: CallbackDetails,
    context: ContextDetails,
    /// The history events whose `id` is this operation's ID, in history
    /// order.
    events: Vec<Event>,
}

impl TestOperation {
    /// A record of `kind` with no result, error or detail, in status
    /// `Started`.
    fn started(id: String, kind: RunnerKind) -> Self {
        Self {
            id,
            kind,
            sub_type: None,
            status: OperationStatus::Started,
            result: None,
            error: None,
            name: None,
            parent_id: None,
            start_time: None,
            end_time: None,
            step: StepDetails {
                attempt: 0,
                next_attempt_time: None,
            },
            wait: WaitDetails {
                duration: None,
                scheduled_end_time: None,
            },
            callback: CallbackDetails { callback_id: None },
            context: ContextDetails {
                replay_children: false,
            },
            events: Vec::new(),
        }
    }

    /// The operation's wire ID (the SHA-256 hex of its positional path).
    pub fn id(&self) -> &str {
        &self.id
    }

    /// The history events of this operation: the events of
    /// [`TestResult::history_events`] whose `id` is this operation's
    /// [`id`](Self::id), in history order.
    pub fn events(&self) -> &[Event] {
        &self.events
    }

    /// The operation kind, such as [`OperationKind::Step`] or
    /// [`OperationKind::Callback`].
    pub fn kind(&self) -> OperationKind {
        self.kind.operation_kind()
    }

    /// The operation sub-type, e.g. `"Step"`, `"Wait"`, `"Map"`,
    /// `"WaitForCondition"`, or `None` if the operation carried none.
    ///
    /// The constants of [`OperationSubType`](crate::OperationSubType) hold
    /// the built-in values. A step, a callback, a child context or a
    /// `with_retry` block built with a `sub_type` setter records the custom
    /// value instead.
    pub fn sub_type(&self) -> Option<&str> {
        self.sub_type.as_deref()
    }

    /// The terminal-or-current status, such as
    /// [`OperationStatus::Succeeded`] or [`OperationStatus::TimedOut`].
    pub fn status(&self) -> OperationStatus {
        self.status
    }

    /// Returns `true` if the operation completed successfully.
    pub fn succeeded(&self) -> bool {
        self.status == OperationStatus::Succeeded
    }

    /// Returns `true` if the operation ended in a failed status.
    pub fn failed(&self) -> bool {
        self.status == OperationStatus::Failed
    }

    /// The serialized result payload, if the operation stored one.
    pub fn result(&self) -> Option<&str> {
        self.result.as_deref()
    }

    /// The [`result`](Self::result) payload decoded as JSON, the format
    /// [`JsonSerdes`](crate::JsonSerdes) writes.
    ///
    /// Returns `Ok(None)` when the operation stored no result, which
    /// includes a [`CloudRunner`] result that recorded history withheld
    /// because it was oversized, and the decoding error when the payload is
    /// not JSON of `T`.
    ///
    /// # Errors
    ///
    /// Returns the [`serde_json::Error`] of a payload that does not decode
    /// into `T`.
    pub fn result_json<T: DeserializeOwned>(&self) -> Result<Option<T>, serde_json::Error> {
        self.result.as_deref().map(serde_json::from_str).transpose()
    }

    /// Returns `true` for the child context of a `wait_for_callback`: an
    /// [`OperationKind::Context`] whose [`sub_type`](Self::sub_type) is
    /// `WaitForCallback`.
    pub fn is_wait_for_callback(&self) -> bool {
        self.kind == RunnerKind::Context
            && self.sub_type.as_deref() == Some(crate::callback::WFCB_SUB_TYPE)
    }

    /// The recorded duration of a wait, in whole seconds.
    ///
    /// `None` for an operation of any other kind, and for a wait that
    /// recorded no duration.
    pub fn wait_duration(&self) -> Option<std::time::Duration> {
        match self.details() {
            TestOperationDetails::Wait(wait) => wait.duration(),
            _ => None,
        }
    }

    /// The recorded error, if the operation stored one: its type, message,
    /// error data and stack trace.
    pub fn error(&self) -> Option<&crate::WireError> {
        self.error.as_ref()
    }

    /// The recorded error type, if the operation failed. Reads
    /// [`error`](Self::error).
    pub fn error_type(&self) -> Option<&str> {
        self.error.as_ref().and_then(crate::WireError::error_type)
    }

    /// The recorded error message, if the operation failed. Reads
    /// [`error`](Self::error).
    pub fn error_message(&self) -> Option<&str> {
        self.error
            .as_ref()
            .and_then(crate::WireError::error_message)
    }

    /// The operation's caller-assigned name, if any.
    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    /// The [`id`](Self::id) of the operation that contains this one, or
    /// `None` for a top-level operation.
    pub fn parent_id(&self) -> Option<&str> {
        self.parent_id.as_deref()
    }

    /// The time the operation started, or `None` when none was recorded.
    ///
    /// Under [`LocalRunner`] it is the virtual time the record was created,
    /// reported as the run's start time plus virtual seconds. Under
    /// [`CloudRunner`] it is the timestamp of the operation's first history
    /// event.
    pub fn start_time(&self) -> Option<std::time::SystemTime> {
        self.start_time
    }

    /// The time the operation ended, or `None` while it has not ended.
    ///
    /// Under [`LocalRunner`] it is the virtual time the status first became
    /// `Succeeded`, `Failed`, `TimedOut`, `Cancelled` or `Stopped`. Under
    /// [`CloudRunner`] it is the timestamp of the operation's terminal
    /// history event.
    pub fn end_time(&self) -> Option<std::time::SystemTime> {
        self.end_time
    }

    /// The detail of this operation's kind. The variant matches
    /// [`kind`](Self::kind): [`OperationKind::Step`] gives
    /// [`TestOperationDetails::Step`], and so on.
    pub fn details(&self) -> TestOperationDetails<'_> {
        match self.kind {
            RunnerKind::Context => TestOperationDetails::Context(&self.context),
            RunnerKind::Step => TestOperationDetails::Step(&self.step),
            RunnerKind::Wait => TestOperationDetails::Wait(&self.wait),
            RunnerKind::Callback => TestOperationDetails::Callback(&self.callback),
            RunnerKind::ChainedInvoke => TestOperationDetails::ChainedInvoke,
        }
    }
}

/// The kind of an operation that a runner records: every
/// [`OperationKind`] except [`OperationKind::Execution`], which no runner
/// stores as an operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RunnerKind {
    Context,
    Step,
    Wait,
    Callback,
    ChainedInvoke,
}

impl RunnerKind {
    /// The runner kind of `kind`, or `None` for
    /// [`OperationKind::Execution`].
    fn from_operation_kind(kind: OperationKind) -> Option<Self> {
        match kind {
            OperationKind::Execution => None,
            OperationKind::Context => Some(Self::Context),
            OperationKind::Step => Some(Self::Step),
            OperationKind::Wait => Some(Self::Wait),
            OperationKind::Callback => Some(Self::Callback),
            OperationKind::ChainedInvoke => Some(Self::ChainedInvoke),
        }
    }

    /// The public kind of this runner kind.
    pub(crate) fn operation_kind(self) -> OperationKind {
        match self {
            Self::Context => OperationKind::Context,
            Self::Step => OperationKind::Step,
            Self::Wait => OperationKind::Wait,
            Self::Callback => OperationKind::Callback,
            Self::ChainedInvoke => OperationKind::ChainedInvoke,
        }
    }
}

/// The detail of one [`TestOperation`], by kind. Returned by
/// [`TestOperation::details`].
///
/// # Examples
///
/// ```
/// use std::time::Duration;
///
/// use aws_durable_execution_sdk as durable;
/// use durable::test_util::{LocalRunner, TestOperationDetails};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), durable::BoxError> {
/// let result = LocalRunner::new()
///     .run(
///         |_e: (), ctx: durable::DurableContext| async move {
///             ctx.wait(Duration::from_secs(10)).name("pause").await?;
///             Ok::<_, durable::BoxError>(())
///         },
///         (),
///     )
///     .await?;
///
/// let wait = result.operation("pause").ok_or("no wait")?;
/// assert!(matches!(
///     wait.details(),
///     TestOperationDetails::Wait(w) if w.duration() == Some(Duration::from_secs(10))
/// ));
/// # Ok(())
/// # }
/// ```
///
/// No runner returns the execution itself as a [`TestOperation`], so the
/// enum has no variant for it:
///
/// ```rust,compile_fail
/// use aws_durable_execution_sdk::test_util::TestOperationDetails;
///
/// fn is_execution(details: TestOperationDetails<'_>) -> bool {
///     matches!(details, TestOperationDetails::Execution)
/// }
/// ```
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum TestOperationDetails<'a> {
    /// A child context.
    Context(&'a ContextDetails),
    /// A step.
    Step(&'a StepDetails),
    /// A wait.
    Wait(&'a WaitDetails),
    /// A callback.
    Callback(&'a CallbackDetails),
    /// A chained invoke.
    ChainedInvoke,
}

/// The detail of a step [`TestOperation`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct StepDetails {
    attempt: u32,
    next_attempt_time: Option<std::time::SystemTime>,
}

impl StepDetails {
    /// The number of completed retries recorded for this step (`0` when
    /// never retried). [`LocalRunner`] and [`CloudRunner`] report this
    /// value identically.
    pub fn attempt(&self) -> u32 {
        self.attempt
    }

    /// The time the step's next attempt was scheduled for, after a retry.
    /// `None` for a step that was never retried.
    ///
    /// A step retry and a `wait_for_condition` check that continued both
    /// set it. Under [`LocalRunner`] it is the virtual time the latest retry
    /// delay ended. Under [`CloudRunner`] it is the timestamp of the latest
    /// step event that carries a retry delay, plus that delay: a
    /// `StepFailed` for a step retry, or a `StepSucceeded` for a check that
    /// continued. The two runners report the same value.
    pub fn next_attempt_time(&self) -> Option<std::time::SystemTime> {
        self.next_attempt_time
    }
}

/// The detail of a wait [`TestOperation`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct WaitDetails {
    duration: Option<std::time::Duration>,
    scheduled_end_time: Option<std::time::SystemTime>,
}

impl WaitDetails {
    /// The recorded wait duration, in whole seconds, or `None` when no
    /// duration was recorded.
    pub fn duration(&self) -> Option<std::time::Duration> {
        self.duration
    }

    /// The time the wait was scheduled to end, or `None` when none was
    /// recorded.
    ///
    /// Under [`LocalRunner`] it is the virtual time the wait started plus
    /// its duration. Under [`CloudRunner`] it is the scheduled end of the
    /// `WaitStarted` event.
    pub fn scheduled_end_time(&self) -> Option<std::time::SystemTime> {
        self.scheduled_end_time
    }
}

/// The detail of a callback [`TestOperation`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct CallbackDetails {
    callback_id: Option<String>,
}

impl CallbackDetails {
    /// The callback ID the backend assigned, the ID the handler sees, or
    /// `None` when none was recorded.
    pub fn callback_id(&self) -> Option<&str> {
        self.callback_id.as_deref()
    }
}

/// The detail of a child context [`TestOperation`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ContextDetails {
    replay_children: bool,
}

impl ContextDetails {
    /// Whether the context closed with the `ReplayChildren` option. Only
    /// [`LocalRunner`] reports it; under [`CloudRunner`] it is `false`.
    pub fn replay_children(&self) -> bool {
        self.replay_children
    }
}

/// The status of the durable execution a [`TestResult`] describes, as the
/// service reports it.
///
/// [`Display`](std::fmt::Display) writes the variant name, such as
/// `TimedOut`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ExecutionStatus {
    /// The execution has not ended.
    Running,
    /// The handler returned a value.
    Succeeded,
    /// The handler returned an error.
    Failed,
    /// The execution exceeded its execution timeout.
    TimedOut,
    /// The execution was stopped.
    Stopped,
}

impl std::fmt::Display for ExecutionStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::Running => "Running",
            Self::Succeeded => "Succeeded",
            Self::Failed => "Failed",
            Self::TimedOut => "TimedOut",
            Self::Stopped => "Stopped",
        })
    }
}

impl ExecutionStatus {
    /// Converts the service's status. A status this enum does not list
    /// ended the execution in a way the runner cannot name, so it maps to
    /// `Failed`.
    fn from_wire(status: &WireExecutionStatus) -> Self {
        match status {
            WireExecutionStatus::Running => Self::Running,
            WireExecutionStatus::Succeeded => Self::Succeeded,
            WireExecutionStatus::TimedOut => Self::TimedOut,
            WireExecutionStatus::Stopped => Self::Stopped,
            _ => Self::Failed,
        }
    }
}

/// A failure of the test runner itself, not of the durable execution.
///
/// [`LocalRunner::run`] and [`CloudRunner::run`] return it when they cannot
/// describe the execution. A durable execution that fails is not a runner
/// error: it is an `Ok` [`TestResult`] with [`ExecutionStatus::Failed`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum TestRunnerError {
    /// The event did not serialize to JSON.
    EventSerialization(Arc<serde_json::Error>),
    /// The handler output did not decode into the output type.
    OutputDeserialization(Arc<serde_json::Error>),
    /// A response envelope carried a status other than `SUCCEEDED`,
    /// `FAILED` and `PENDING`. The value is the status, or `None` when the
    /// envelope carried no string status.
    UnexpectedEnvelope(Option<String>),
    /// A Lambda API call failed.
    Aws(Arc<dyn std::error::Error + Send + Sync>),
    /// The invoke response carried no `DurableExecutionArn`.
    NoExecutionArn,
    /// The runner does not support the named control, for example
    /// [`RunHandle::pause`] under [`CloudRunner`].
    Unsupported(&'static str),
    /// No operation of the run matches the [`OperationHandle`].
    OperationNotFound,
    /// The execution ended before the operation reached the status
    /// [`OperationHandle::wait_for_data`] waited for.
    OperationNotReached(WaitingStatus),
    /// The operation is neither a callback nor a `wait_for_callback`
    /// context.
    NotACallback,
    /// The callback is not waiting for a reply.
    CallbackNotPending,
    /// The callback has no heartbeat timeout.
    NoHeartbeatTimeout,
    /// The named control needs a run in progress, and the run already
    /// finished.
    RunFinished(&'static str),
}

impl std::fmt::Display for TestRunnerError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::EventSerialization(e) => {
                write!(f, "durable: test runner: event serialization failed: {e}")
            }
            Self::OutputDeserialization(e) => {
                write!(
                    f,
                    "durable: test runner: output deserialization failed: {e}"
                )
            }
            Self::UnexpectedEnvelope(status) => write!(
                f,
                "durable: test runner: unexpected response envelope status: {status:?}"
            ),
            Self::Aws(e) => write!(f, "durable: test runner: AWS call failed: {e}"),
            Self::NoExecutionArn => f.write_str(
                "durable: test runner: the invoke response carried no DurableExecutionArn",
            ),
            Self::Unsupported(what) => write!(
                f,
                "durable: test runner: {what} is not supported by this runner"
            ),
            Self::OperationNotFound => {
                f.write_str("durable: test runner: no operation matches this handle")
            }
            Self::OperationNotReached(status) => write!(
                f,
                "durable: test runner: the operation did not reach {status:?} before the \
                 execution ended"
            ),
            Self::NotACallback => f.write_str(
                "durable: test runner: the operation is not a callback or a wait_for_callback \
                 context",
            ),
            Self::CallbackNotPending => {
                f.write_str("durable: test runner: the callback is not waiting for a reply")
            }
            Self::NoHeartbeatTimeout => {
                f.write_str("durable: test runner: the callback has no heartbeat timeout")
            }
            Self::RunFinished(what) => write!(
                f,
                "durable: test runner: {what} needs a run in progress; call it before awaiting \
                 the RunHandle"
            ),
        }
    }
}

impl std::error::Error for TestRunnerError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::EventSerialization(e) | Self::OutputDeserialization(e) => Some(e.as_ref()),
            Self::Aws(e) => Some(e.as_ref()),
            Self::UnexpectedEnvelope(_)
            | Self::NoExecutionArn
            | Self::Unsupported(_)
            | Self::OperationNotFound
            | Self::OperationNotReached(_)
            | Self::NotACallback
            | Self::CallbackNotPending
            | Self::NoHeartbeatTimeout
            | Self::RunFinished(_) => None,
        }
    }
}

/// The outcome of a [`LocalRunner::run`] or [`CloudRunner::run`]: the
/// execution status, the deserialized handler output (on success) and the
/// recorded operation history.
///
/// # Examples
///
/// ```
/// use aws_durable_execution_sdk as durable;
/// use durable::test_util::{ExecutionStatus, LocalRunner};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), durable::BoxError> {
/// let result = LocalRunner::new()
///     .run(
///         |_e: (), _ctx: durable::DurableContext| async move {
///             Ok::<_, durable::BoxError>("hi".to_owned())
///         },
///         (),
///     )
///     .await?;
///
/// assert_eq!(result.status(), ExecutionStatus::Succeeded);
/// assert!(result.is_success());
/// assert_eq!(result.output().map(String::as_str), Some("hi"));
/// assert_eq!(result.invocation_count(), 1);
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
#[non_exhaustive]
pub struct TestResult<O> {
    status: ExecutionStatus,
    output: Option<O>,
    error: Option<crate::WireError>,
    operations: Vec<TestOperation>,
    invocations: usize,
    history_events: Vec<Event>,
    invocation_records: Vec<TestInvocation>,
}

impl<O> TestResult<O> {
    /// A result whose operations, history events and invocation records
    /// come from `operations` and `history_events`: each operation gets the
    /// events with its ID, and each `InvocationCompleted` event becomes a
    /// [`TestInvocation`].
    fn with_history(
        status: ExecutionStatus,
        output: Option<O>,
        error: Option<crate::WireError>,
        mut operations: Vec<TestOperation>,
        invocations: usize,
        history_events: Vec<Event>,
    ) -> Self {
        for op in &mut operations {
            op.events = history_events
                .iter()
                .filter(|event| event.id() == Some(op.id.as_str()))
                .cloned()
                .collect();
        }
        let invocation_records = history_events
            .iter()
            .filter(|event| event.event_type() == Some(&EventType::InvocationCompleted))
            .map(TestInvocation::from_event)
            .collect();
        Self {
            status,
            output,
            error,
            operations,
            invocations,
            history_events,
            invocation_records,
        }
    }

    /// The status of the execution.
    ///
    /// Under [`LocalRunner`], a run that ends because nothing can advance,
    /// or because [`max_invocations`](LocalRunner::max_invocations) is
    /// reached, is [`ExecutionStatus::Running`]. Under [`CloudRunner`], the
    /// service status maps one to one, and a run that reaches a configured
    /// [`max_poll_attempts`](CloudRunner::max_poll_attempts) is
    /// [`ExecutionStatus::Running`].
    pub fn status(&self) -> ExecutionStatus {
        self.status
    }

    /// Returns `true` if the status is [`ExecutionStatus::Succeeded`].
    pub fn is_success(&self) -> bool {
        self.status == ExecutionStatus::Succeeded
    }

    /// Returns `true` if the status is [`ExecutionStatus::Failed`],
    /// [`ExecutionStatus::TimedOut`] or [`ExecutionStatus::Stopped`].
    pub fn is_failure(&self) -> bool {
        matches!(
            self.status,
            ExecutionStatus::Failed | ExecutionStatus::TimedOut | ExecutionStatus::Stopped
        )
    }

    /// Returns `true` if the status is [`ExecutionStatus::Running`]: the
    /// handler suspended and never reached a terminal outcome within the
    /// runner's budget (e.g. it awaits a callback the test never delivered,
    /// or it never terminates).
    pub fn is_suspended(&self) -> bool {
        self.status == ExecutionStatus::Running
    }

    /// The deserialized handler output on success, or `None` otherwise.
    pub fn output(&self) -> Option<&O> {
        self.output.as_ref()
    }

    /// Consumes the result and returns the owned handler output on success.
    #[must_use]
    pub fn into_output(self) -> Option<O> {
        self.output
    }

    /// The execution's error: on failure, the recorded error with its type,
    /// message and error data; while running, the reason the run ended.
    /// `None` on success.
    pub fn error(&self) -> Option<&crate::WireError> {
        self.error.as_ref()
    }

    /// The wire error type on failure (e.g. `"StepError"`), or `None`.
    /// Reads [`error`](Self::error).
    pub fn error_type(&self) -> Option<&str> {
        self.error.as_ref().and_then(crate::WireError::error_type)
    }

    /// The wire error message on failure, or `None`. Reads
    /// [`error`](Self::error).
    pub fn error_message(&self) -> Option<&str> {
        self.error
            .as_ref()
            .and_then(crate::WireError::error_message)
    }

    /// The recorded operation history, in first-checkpoint order.
    pub fn operations(&self) -> &[TestOperation] {
        &self.operations
    }

    /// The first operation in [`operations`](Self::operations) order whose
    /// [`name`](TestOperation::name) is `name`.
    pub fn operation(&self, name: &str) -> Option<&TestOperation> {
        self.operations.iter().find(|op| op.name() == Some(name))
    }

    /// Every operation whose [`name`](TestOperation::name) is `name`, in
    /// [`operations`](Self::operations) order.
    pub fn operations_named<'a>(
        &'a self,
        name: &'a str,
    ) -> impl Iterator<Item = &'a TestOperation> + 'a {
        self.operations
            .iter()
            .filter(move |op| op.name() == Some(name))
    }

    /// Every operation whose [`parent_id`](TestOperation::parent_id) is the
    /// [`id`](TestOperation::id) of `parent`, in
    /// [`operations`](Self::operations) order.
    pub fn children<'a>(
        &'a self,
        parent: &'a TestOperation,
    ) -> impl Iterator<Item = &'a TestOperation> + 'a {
        self.operations
            .iter()
            .filter(move |op| op.parent_id() == Some(parent.id()))
    }

    /// The number of invocations that drove this execution.
    ///
    /// For [`LocalRunner`] this is the count of simulated invocations; for
    /// [`CloudRunner`] it is the number of recorded `InvocationCompleted`
    /// events (falling back to the poll count when none are recorded).
    pub fn invocation_count(&self) -> usize {
        self.invocations
    }

    /// The history events of the execution, in history order, as the
    /// service's `Event` model type.
    ///
    /// [`CloudRunner`] returns the events of `GetDurableExecutionHistory`
    /// unchanged. [`LocalRunner`] synthesizes the events. They follow the
    /// service's event types and order: one event for each operation update
    /// the backend accepts, for each wait, callback and chained invoke the
    /// backend settles, for each invocation (`InvocationCompleted`), and for
    /// the start and the end of the execution. The local events are a model
    /// of the service's history. The `history` conformance suite is the
    /// check against the service.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk as durable;
    /// use durable::lambda_types::EventType;
    /// use durable::test_util::LocalRunner;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), durable::BoxError> {
    /// let result = LocalRunner::new()
    ///     .run(
    ///         |_e: (), ctx: durable::DurableContext| async move {
    ///             ctx.step(|_| async { Ok(1_i32) }).await?;
    ///             Ok::<_, durable::BoxError>(())
    ///         },
    ///         (),
    ///     )
    ///     .await?;
    ///
    /// let types: Vec<_> = result
    ///     .history_events()
    ///     .iter()
    ///     .filter_map(|event| event.event_type().cloned())
    ///     .collect();
    /// assert_eq!(
    ///     types,
    ///     [
    ///         EventType::ExecutionStarted,
    ///         EventType::StepStarted,
    ///         EventType::StepSucceeded,
    ///         EventType::InvocationCompleted,
    ///         EventType::ExecutionSucceeded,
    ///     ]
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn history_events(&self) -> &[Event] {
        &self.history_events
    }

    /// One record per handler invocation: one per `InvocationCompleted`
    /// event of [`history_events`](Self::history_events), in history order.
    ///
    /// Under [`LocalRunner`], invocation `n` (counting from 1) has the
    /// request ID `local-invocation-{n}`, and the list has
    /// [`invocation_count`](Self::invocation_count) records.
    pub fn invocations(&self) -> &[TestInvocation] {
        &self.invocation_records
    }

    /// The operations as a table with the columns `columns` enables.
    ///
    /// [`Display`](std::fmt::Display) for `TestResult` prints this table
    /// with every column. The output is:
    ///
    /// 1. `No operations found.` when [`operations`](Self::operations) is
    ///    empty.
    /// 2. Otherwise a header line with the enabled column names, in the
    ///    order `parentId`, `name`, `type`, `subType`, `status`,
    ///    `startTime`, `endTime`, `duration`, then one line per operation in
    ///    [`operations`](Self::operations) order.
    ///
    /// Cells are joined by ` | `, and every line ends with a newline. A time
    /// prints as ISO 8601 UTC with three fractional digits, as JS
    /// `toISOString` prints it. The duration is the end time minus the
    /// start time in whole milliseconds, such as `1000ms`. An absent value
    /// prints `-`.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk as durable;
    /// use durable::test_util::{LocalRunner, PrintColumns};
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), durable::BoxError> {
    /// let result = LocalRunner::new()
    ///     .run(
    ///         |_e: (), ctx: durable::DurableContext| async move {
    ///             ctx.step(|_| async { Ok(1_i32) }).name("fetch").await?;
    ///             Ok::<_, durable::BoxError>(())
    ///         },
    ///         (),
    ///     )
    ///     .await?;
    ///
    /// let mut columns = PrintColumns::default();
    /// columns.parent_id = false;
    /// columns.sub_type = false;
    /// columns.start_time = false;
    /// columns.end_time = false;
    /// columns.duration = false;
    /// assert_eq!(
    ///     result.display_with(columns).to_string(),
    ///     "name | type | status\nfetch | STEP | SUCCEEDED\n"
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn display_with(&self, columns: PrintColumns) -> impl std::fmt::Display + '_ {
        OperationTable {
            operations: &self.operations,
            columns,
        }
    }
}

impl<O> std::fmt::Display for TestResult<O> {
    /// Prints [`display_with`](Self::display_with) with every column.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.display_with(PrintColumns::default()).fmt(f)
    }
}

/// One handler invocation of an execution, read from its
/// `InvocationCompleted` history event.
///
/// Returned in order by [`TestResult::invocations`].
///
/// # Examples
///
/// ```
/// use aws_durable_execution_sdk as durable;
/// use durable::test_util::LocalRunner;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), durable::BoxError> {
/// let result = LocalRunner::new()
///     .run(
///         |_e: (), _ctx: durable::DurableContext| async move {
///             Ok::<_, durable::BoxError>(())
///         },
///         (),
///     )
///     .await?;
///
/// let invocation = &result.invocations()[0];
/// assert_eq!(invocation.request_id(), Some("local-invocation-1"));
/// assert!(invocation.error().is_none());
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TestInvocation {
    start_time: Option<std::time::SystemTime>,
    end_time: Option<std::time::SystemTime>,
    request_id: Option<String>,
    error: Option<crate::WireError>,
}

impl TestInvocation {
    /// The record of an `InvocationCompleted` event, with the fields of its
    /// `InvocationCompletedDetails`. An event without details gives a
    /// record with every field absent.
    fn from_event(event: &Event) -> Self {
        let details = event.invocation_completed_details();
        let time = |t: &DateTime| std::time::SystemTime::try_from(*t).ok();
        Self {
            start_time: details.and_then(|d| time(d.start_timestamp())),
            end_time: details.and_then(|d| time(d.end_timestamp())),
            request_id: details.map(|d| d.request_id().to_owned()),
            error: details
                .and_then(InvocationCompletedDetails::error)
                .and_then(EventError::payload)
                .map(wire_error_from_object),
        }
    }

    /// The time the invocation started, or `None` when it lies outside the
    /// range of `SystemTime`.
    pub fn start_time(&self) -> Option<std::time::SystemTime> {
        self.start_time
    }

    /// The time the invocation ended, or `None` when it lies outside the
    /// range of `SystemTime`.
    pub fn end_time(&self) -> Option<std::time::SystemTime> {
        self.end_time
    }

    /// The request ID of the invocation.
    pub fn request_id(&self) -> Option<&str> {
        self.request_id.as_deref()
    }

    /// The error the invocation ended with, or `None` when it returned a
    /// `SUCCEEDED` or `PENDING` result.
    ///
    /// Under [`LocalRunner`], an invocation fault (the invocation itself
    /// failed, for example on a retryable checkpoint error) has no error
    /// type and its message is the fault's text. A `FAILED` result has the
    /// result's error type, message, error data and stack trace.
    pub fn error(&self) -> Option<&crate::WireError> {
        self.error.as_ref()
    }
}

/// The columns [`TestResult::display_with`] prints. Each field enables one
/// column; [`PrintColumns::default`] enables every column, as JS
/// `TestResult.print` does.
///
/// # Examples
///
/// ```
/// use aws_durable_execution_sdk::test_util::PrintColumns;
///
/// let mut columns = PrintColumns::default();
/// columns.duration = false;
/// assert!(columns.name);
/// ```
#[expect(clippy::struct_excessive_bools)] // reason: one flag per JS column, as JS's print config has
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct PrintColumns {
    /// The `parentId` column: [`TestOperation::parent_id`].
    pub parent_id: bool,
    /// The `name` column: [`TestOperation::name`].
    pub name: bool,
    /// The `type` column: the [`wire_str`](OperationKind::wire_str) of
    /// [`TestOperation::kind`].
    pub kind: bool,
    /// The `subType` column: [`TestOperation::sub_type`].
    pub sub_type: bool,
    /// The `status` column: the [`wire_str`](OperationStatus::wire_str) of
    /// [`TestOperation::status`].
    pub status: bool,
    /// The `startTime` column: [`TestOperation::start_time`].
    pub start_time: bool,
    /// The `endTime` column: [`TestOperation::end_time`].
    pub end_time: bool,
    /// The `duration` column: the end time minus the start time.
    pub duration: bool,
}

impl Default for PrintColumns {
    /// Every column enabled.
    fn default() -> Self {
        Self {
            parent_id: true,
            name: true,
            kind: true,
            sub_type: true,
            status: true,
            start_time: true,
            end_time: true,
            duration: true,
        }
    }
}

/// The table [`TestResult::display_with`] returns.
struct OperationTable<'a> {
    operations: &'a [TestOperation],
    columns: PrintColumns,
}

impl std::fmt::Display for OperationTable<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.operations.is_empty() {
            return f.write_str("No operations found.\n");
        }
        let c = self.columns;
        let enabled = [
            (c.parent_id, "parentId"),
            (c.name, "name"),
            (c.kind, "type"),
            (c.sub_type, "subType"),
            (c.status, "status"),
            (c.start_time, "startTime"),
            (c.end_time, "endTime"),
            (c.duration, "duration"),
        ];
        let header: Vec<&str> = enabled
            .iter()
            .filter(|(on, _)| *on)
            .map(|(_, name)| *name)
            .collect();
        writeln!(f, "{}", header.join(" | "))?;
        for op in self.operations {
            let start = op.start_time.map(unix_millis);
            let end = op.end_time.map(unix_millis);
            let duration = duration_cell(start, end);
            let cells = [
                (c.parent_id, op.parent_id().unwrap_or("-").to_owned()),
                (c.name, op.name().unwrap_or("-").to_owned()),
                (c.kind, op.kind().wire_str().to_owned()),
                (c.sub_type, op.sub_type().unwrap_or("-").to_owned()),
                (c.status, op.status().wire_str().to_owned()),
                (c.start_time, start.map_or_else(dash, iso_millis)),
                (c.end_time, end.map_or_else(dash, iso_millis)),
                (c.duration, duration),
            ];
            let row: Vec<String> = cells
                .into_iter()
                .filter(|(on, _)| *on)
                .map(|(_, cell)| cell)
                .collect();
            writeln!(f, "{}", row.join(" | "))?;
        }
        Ok(())
    }
}

/// The duration cell: `end - start` in whole milliseconds, or `-` unless
/// both times exist.
///
/// Each time is at most `u64::MAX` seconds from the epoch, about `2^74`
/// milliseconds. So the difference fits an `i128` and cannot overflow.
fn duration_cell(start: Option<i128>, end: Option<i128>) -> String {
    match (start, end) {
        (Some(start), Some(end)) => format!("{}ms", end - start),
        _ => dash(),
    }
}

/// The cell of an absent value.
fn dash() -> String {
    "-".to_owned()
}

/// The signed whole milliseconds of `time` from the Unix epoch, truncated
/// toward zero.
///
/// A `Duration` holds at most `u64::MAX` seconds. So the milliseconds fit
/// an `i128` for every `SystemTime`, and the result needs no `Option`. The
/// time cells narrow the value only when they format it, so a time that
/// cannot be printed still gives the duration cell its exact value.
fn unix_millis(time: std::time::SystemTime) -> i128 {
    let millis =
        |d: std::time::Duration| i128::from(d.as_secs()) * 1000 + i128::from(d.subsec_millis());
    match time.duration_since(std::time::UNIX_EPOCH) {
        Ok(after) => millis(after),
        Err(before) => -millis(before.duration()),
    }
}

/// `millis` from the Unix epoch as JS `toISOString` prints it: ISO 8601 UTC
/// with exactly three fractional digits. A value whose seconds do not fit
/// an `i64`, or whose year the formatter does not support, prints `-`.
fn iso_millis(millis: i128) -> String {
    // Euclidean division keeps the remainder in 0..=999, so a time before
    // the epoch borrows from the seconds, as an ISO timestamp does.
    let remainder = millis.rem_euclid(1000);
    i64::try_from(millis.div_euclid(1000))
        .ok()
        .and_then(|seconds| {
            DateTime::from_secs(seconds)
                .fmt(aws_sdk_lambda::primitives::DateTimeFormat::DateTime)
                .ok()
        })
        .map_or_else(dash, |text| {
            // `Format::DateTime` writes RFC 3339 in UTC, which always ends
            // in `Z`. So a successful format always has the suffix.
            #[expect(clippy::expect_used)] // reason: Format::DateTime always ends in Z
            let whole = text.strip_suffix('Z').expect("Format::DateTime ends in Z");
            format!("{whole}.{remainder:03}Z")
        })
}

/// One reply that the simulated backend delivers to a pending callback.
///
/// Queue a reply with [`LocalRunner::callback`], for the next pending
/// callback in order, or with [`LocalRunner::callback_for`], for the
/// callback or `wait_for_callback` with a given name. The runner delivers
/// queued replies between invocations, before it moves its virtual clock.
///
/// # Examples
///
/// ```
/// use aws_durable_execution_sdk as durable;
/// use durable::test_util::{CallbackFailure, CallbackReply, LocalRunner};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), durable::BoxError> {
/// let runner = LocalRunner::new()
///     .callback_for("approval", CallbackReply::success(&true)?)
///     .callback(CallbackReply::failure(CallbackFailure::new(
///         "Declined",
///         "the reviewer said no",
///     )));
/// let result = runner
///     .run(
///         |_e: (), ctx: durable::DurableContext| async move {
///             let audit = ctx.create_callback::<bool>().name("audit").await?;
///             let approval = ctx.create_callback::<bool>().name("approval").await?;
///             let approved = approval.result().await?;
///             let audited = audit.result().await.is_ok();
///             Ok::<_, durable::BoxError>((approved, audited))
///         },
///         (),
///     )
///     .await?;
///
/// assert_eq!(result.output(), Some(&(true, false)));
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct CallbackReply {
    kind: CallbackReplyKind,
}

/// The record a [`CallbackReply`] gives a callback.
#[derive(Debug, Clone)]
enum CallbackReplyKind {
    /// `Succeeded` with this payload.
    Success(String),
    /// `Failed` with the reported error fields.
    Failure(CallbackFailure),
    /// `TimedOut`, as a callback timeout records it.
    Timeout,
    /// `TimedOut`, as a missed heartbeat records it.
    HeartbeatTimeout,
}

/// The message the JS local checkpoint server records for a callback
/// timeout.
const CALLBACK_TIMEOUT_MESSAGE: &str = "Callback timed out";
/// The error type the SDK reads as a heartbeat timeout.
const CALLBACK_HEARTBEAT_ERROR_TYPE: &str = "Callback.Heartbeat";
/// The message the JS local checkpoint server records for a missed
/// heartbeat.
const CALLBACK_HEARTBEAT_MESSAGE: &str = "Callback timed out on heartbeat";

impl CallbackReply {
    /// A success whose payload is `value` encoded as JSON.
    ///
    /// # Errors
    ///
    /// Returns the encoding error when `value` fails to serialize.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::{CallbackReply, LocalRunner};
    ///
    /// # fn main() -> Result<(), aws_durable_execution_sdk::BoxError> {
    /// let runner = LocalRunner::new().callback(CallbackReply::success(&"approved")?);
    /// # drop(runner);
    /// # Ok(())
    /// # }
    /// ```
    pub fn success<T: Serialize>(value: &T) -> Result<Self, BoxError> {
        let payload = serde_json::to_string(value)?;
        Ok(Self {
            kind: CallbackReplyKind::Success(payload),
        })
    }

    /// A success whose payload is `payload`, unchanged.
    ///
    /// The handler decodes the payload with the callback's serdes, so with
    /// the default JSON serdes the payload must be JSON.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::{CallbackReply, LocalRunner};
    ///
    /// let runner = LocalRunner::new().callback(CallbackReply::raw(r#""approved""#));
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn raw(payload: impl Into<String>) -> Self {
        Self {
            kind: CallbackReplyKind::Success(payload.into()),
        }
    }

    /// An external failure.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::{CallbackFailure, CallbackReply, LocalRunner};
    ///
    /// let failure = CallbackFailure::new("Declined", "no").with_error_data(r#"{"code":7}"#);
    /// let runner = LocalRunner::new().callback(CallbackReply::failure(failure));
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn failure(failure: CallbackFailure) -> Self {
        Self {
            kind: CallbackReplyKind::Failure(failure),
        }
    }

    /// A timeout, as the JS local checkpoint server records one.
    ///
    /// The callback becomes `TimedOut` with the message
    /// `Callback timed out`.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::{CallbackReply, LocalRunner};
    ///
    /// let runner = LocalRunner::new().callback(CallbackReply::timeout());
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn timeout() -> Self {
        Self {
            kind: CallbackReplyKind::Timeout,
        }
    }

    /// A heartbeat timeout, as the service records one.
    ///
    /// The callback becomes `TimedOut` with the error type
    /// `Callback.Heartbeat` and the message
    /// `Callback timed out on heartbeat`.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::{CallbackReply, LocalRunner};
    ///
    /// let runner = LocalRunner::new().callback(CallbackReply::heartbeat_timeout());
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn heartbeat_timeout() -> Self {
        Self {
            kind: CallbackReplyKind::HeartbeatTimeout,
        }
    }

    /// Writes this reply into a pending callback record, as the table in
    /// the `LocalRunner` callback docs states.
    fn deliver_to(self, op: &mut StoredOp, now: u64) {
        op.result = None;
        op.error_type = None;
        op.error_message = None;
        op.error_data = None;
        op.stack_trace = Vec::new();
        match self.kind {
            CallbackReplyKind::Success(payload) => {
                op.set_status(OperationStatus::Succeeded, now);
                op.result = Some(payload);
            }
            CallbackReplyKind::Failure(failure) => {
                op.set_status(OperationStatus::Failed, now);
                op.error_type = failure.error_type;
                op.error_message = failure.error_message;
                op.error_data = failure.error_data;
                op.stack_trace = failure.stack_trace;
            }
            CallbackReplyKind::Timeout => {
                op.set_status(OperationStatus::TimedOut, now);
                op.error_message = Some(CALLBACK_TIMEOUT_MESSAGE.to_owned());
            }
            CallbackReplyKind::HeartbeatTimeout => {
                op.set_status(OperationStatus::TimedOut, now);
                op.error_type = Some(CALLBACK_HEARTBEAT_ERROR_TYPE.to_owned());
                op.error_message = Some(CALLBACK_HEARTBEAT_MESSAGE.to_owned());
            }
        }
        op.callback_delivered = true;
    }
}

/// The error an external system reports for a callback, as JS `ErrorObject`.
///
/// Each of the four fields is absent until it is set.
/// [`CallbackFailure::default`] has every field absent.
///
/// # Examples
///
/// ```
/// use aws_durable_execution_sdk::test_util::CallbackFailure;
///
/// let failure = CallbackFailure::new("Declined", "no")
///     .with_error_data(r#"{"code":7}"#)
///     .with_stack_trace(vec!["at review".to_owned()]);
/// # drop(failure);
/// ```
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct CallbackFailure {
    error_type: Option<String>,
    error_message: Option<String>,
    error_data: Option<String>,
    stack_trace: Vec<String>,
}

impl CallbackFailure {
    /// A failure with this error type and message.
    #[must_use]
    pub fn new(error_type: impl Into<String>, error_message: impl Into<String>) -> Self {
        Self {
            error_type: Some(error_type.into()),
            error_message: Some(error_message.into()),
            ..Self::default()
        }
    }

    /// Sets the opaque error data.
    #[must_use]
    pub fn with_error_data(mut self, error_data: impl Into<String>) -> Self {
        self.error_data = Some(error_data.into());
        self
    }

    /// Sets the stack trace frames.
    #[must_use]
    pub fn with_stack_trace(mut self, frames: Vec<String>) -> Self {
        self.stack_trace = frames;
        self
    }

    /// The service `ErrorObject` of this failure, with each field it sets.
    fn error_object(&self) -> ErrorObject {
        let mut builder = ErrorObject::builder()
            .set_error_type(self.error_type.clone())
            .set_error_message(self.error_message.clone())
            .set_error_data(self.error_data.clone());
        for frame in &self.stack_trace {
            builder = builder.stack_trace(frame);
        }
        builder.build()
    }
}

/// The callback replies a runner queues before a run.
#[derive(Debug, Clone, Default)]
struct QueuedReplies {
    /// Replies for the next pending callback, in queue order.
    fifo: std::collections::VecDeque<CallbackReply>,
    /// Replies for a named callback or `wait_for_callback`, in queue order.
    named: Vec<(String, CallbackReply)>,
}

/// A type-erased plain invoke target: it takes the invoke's input JSON and
/// returns the output JSON.
type PlainTarget = Arc<
    dyn Fn(String) -> Pin<Box<dyn Future<Output = Result<String, BoxError>> + Send>> + Send + Sync,
>;

/// A type-erased durable invoke target: it runs the handler as a nested
/// execution with the given runner settings and the invoke's input JSON.
type DurableTarget = Arc<
    dyn Fn(LocalRunner, String) -> Pin<Box<dyn Future<Output = NestedOutcome> + Send>>
        + Send
        + Sync,
>;

/// A target registered for `ctx.invoke` with
/// [`LocalRunner::register_function`] or
/// [`LocalRunner::register_durable_function`].
#[derive(Clone)]
enum RegisteredFunction {
    /// A plain function, run once inline.
    Plain(PlainTarget),
    /// A durable handler, run as a nested execution on a fresh backend.
    Durable(DurableTarget),
}

impl std::fmt::Debug for RegisteredFunction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Plain(_) => f.write_str("RegisteredFunction::Plain"),
            Self::Durable(_) => f.write_str("RegisteredFunction::Durable"),
        }
    }
}

/// The outcome of an invoke target, as the caller's invoke record stores
/// it.
#[derive(Debug, Clone, PartialEq, Eq)]
enum NestedOutcome {
    /// The target succeeded. The string is the raw result JSON; `None`
    /// records `"null"`.
    Succeeded(Option<String>),
    /// The target failed with this wire error: its type, message, error
    /// data and stack trace.
    Failed(crate::WireError),
    /// A nested durable execution suspended. The invoke stays `Started`.
    Suspended,
}

impl NestedOutcome {
    /// The outcome of a failed plain target, with the wire error of the
    /// production handler mapping.
    fn from_error(err: BoxError) -> Self {
        Self::Failed(crate::wire_error_from_box_error(err))
    }
}

/// Drives a durable handler to completion in memory, simulating the backend.
///
/// Construct with [`LocalRunner::new`], optionally pass the handler's
/// [`Options`](crate::Options) with [`options`](Self::options), queue
/// callback replies with [`callback`](Self::callback) and
/// [`callback_for`](Self::callback_for), and register the targets of
/// `ctx.invoke` with [`register_function`](Self::register_function) and
/// [`register_durable_function`](Self::register_durable_function), then
/// drive a handler with [`run`](Self::run).
///
/// # Callback replies
///
/// A pending callback is a callback record in `Pending` or `Started` that
/// has not received a reply. Between invocations the runner visits the
/// pending callbacks in first-checkpoint order. For each one it delivers
/// the first queued [`callback_for`](Self::callback_for) reply that names
/// it, else the front [`callback`](Self::callback) reply, else nothing. A
/// named reply names a callback when the callback has that name, or when
/// the callback's parent is a `wait_for_callback` with that name.
///
/// | Reply | Status | Stored fields |
/// | --- | --- | --- |
/// | [`CallbackReply::success`] | `Succeeded` | the result is the JSON of the value |
/// | [`CallbackReply::raw`] | `Succeeded` | the result is the payload |
/// | [`CallbackReply::failure`] | `Failed` | each field set in the [`CallbackFailure`] |
/// | [`CallbackReply::timeout`] | `TimedOut` | the message `Callback timed out` |
/// | [`CallbackReply::heartbeat_timeout`] | `TimedOut` | the type `Callback.Heartbeat` and the message `Callback timed out on heartbeat` |
///
/// # Examples
///
/// ```
/// use aws_durable_execution_sdk as durable;
/// use durable::test_util::{CallbackReply, LocalRunner};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), durable::BoxError> {
/// // Deliver a value to the first callback the handler awaits.
/// let runner = LocalRunner::new().callback(CallbackReply::success(&true)?);
/// let result = runner
///     .run(
///         |_e: (), ctx: durable::DurableContext| async move {
///             let cb = ctx.create_callback::<bool>().await?;
///             let approved = cb.result().await?;
///             Ok::<_, durable::BoxError>(approved)
///         },
///         (),
///     )
///     .await?;
///
/// assert_eq!(result.output(), Some(&true));
///
/// // Deliver a value to the `wait_for_callback` named "approve".
/// let runner = LocalRunner::new().callback_for("approve", CallbackReply::success(&7_u32)?);
/// let result = runner
///     .run(
///         |_e: (), ctx: durable::DurableContext| async move {
///             let value = ctx
///                 .wait_for_callback::<u32>(|_step_ctx, _callback_id| async { Ok(()) })
///                 .name("approve")
///                 .await?;
///             Ok::<_, durable::BoxError>(value)
///         },
///         (),
///     )
///     .await?;
///
/// assert_eq!(result.output(), Some(&7));
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct LocalRunner {
    max_invocations: usize,
    callback_replies: QueuedReplies,
    /// The targets of `ctx.invoke`, by exact function name (see
    /// [`register_function`](Self::register_function) and
    /// [`register_durable_function`](Self::register_durable_function)).
    functions: BTreeMap<String, RegisteredFunction>,
    /// Initial-state page size. When set, the synthesized invocation
    /// envelope carries at most this many history operations inline plus a
    /// pagination marker, so the context fetches the remainder via
    /// `get_state`: the paginating service is the DEFAULT
    /// ([`DEFAULT_STATE_PAGE_SIZE`]); [`LocalRunner::single_page`] opts
    /// into the single-page special case (`None`).
    initial_page_size: Option<usize>,
    /// Checkpoint-response page size. When set, the backend's checkpoint
    /// response includes `next_marker` once the total stored operations
    /// exceed this threshold, simulating a paginated checkpoint response
    /// (the default; see [`DEFAULT_STATE_PAGE_SIZE`]). This exercises the
    /// checkpoint pagination path in `DurableContext::checkpoint_updates`.
    checkpoint_page_size: Option<usize>,
    /// The [`Options`](crate::Options) each run passes to
    /// [`wrap`](crate::wrap), with the transport replaced by the in-memory
    /// backend. [`Options::default()`](crate::Options::default) unless set
    /// with [`options`](Self::options).
    options: crate::Options,
    /// Number of upcoming checkpoint calls the backend rejects with a
    /// non-retryable error (fault injection; see
    /// [`fail_next_checkpoints`](Self::fail_next_checkpoints)).
    checkpoint_failures: usize,
    /// Number of checkpoint calls to let through before the injected
    /// failures start (see
    /// [`fail_checkpoints_after`](Self::fail_checkpoints_after)).
    checkpoint_failure_skip: usize,
    /// Whether the injected checkpoint failures are retryable (invocation
    /// fault; the runner re-invokes) rather than non-retryable (execution
    /// failure, with no further checkpoint).
    checkpoint_failures_retryable: bool,
    /// How long the backend waits before it applies and answers each
    /// checkpoint request (see
    /// [`checkpoint_latency`](Self::checkpoint_latency)). Zero by default.
    checkpoint_latency: std::time::Duration,
    /// The number of checkpoint requests answered with a token before the
    /// one answered without (see
    /// [`withdraw_checkpoints_after`](Self::withdraw_checkpoints_after)).
    withdraw_after: Option<usize>,
}

impl Default for LocalRunner {
    fn default() -> Self {
        Self::new()
    }
}

impl LocalRunner {
    /// Creates a runner with default settings and no queued callback
    /// replies.
    ///
    /// By default the simulated backend **paginates execution state**
    /// (both the initial invocation envelope and checkpoint responses
    /// split into 2+ pages once history is large enough), matching the
    /// real service. Use [`single_page`](Self::single_page) for the
    /// explicit single-page special case.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::LocalRunner;
    ///
    /// let runner = LocalRunner::new();
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            max_invocations: DEFAULT_MAX_INVOCATIONS,
            callback_replies: QueuedReplies::default(),
            functions: BTreeMap::new(),
            initial_page_size: Some(DEFAULT_STATE_PAGE_SIZE),
            checkpoint_page_size: Some(DEFAULT_STATE_PAGE_SIZE),
            options: crate::Options::default(),
            checkpoint_failures: 0,
            checkpoint_failure_skip: 0,
            checkpoint_failures_retryable: false,
            checkpoint_latency: std::time::Duration::ZERO,
            withdraw_after: None,
        }
    }

    /// Disables state pagination: the invocation envelope carries the full
    /// history inline and checkpoint responses never set a pagination
    /// marker.
    ///
    /// This is the **special case**: the real service paginates, and the
    /// runner does too by default. Reach for this only when a test
    /// specifically targets single-page behavior.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::LocalRunner;
    ///
    /// let runner = LocalRunner::new().single_page();
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn single_page(mut self) -> Self {
        self.initial_page_size = None;
        self.checkpoint_page_size = None;
        self
    }

    /// Sets the maximum number of invocations the runner will drive before
    /// returning a suspended result.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::LocalRunner;
    ///
    /// let runner = LocalRunner::new().max_invocations(10);
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn max_invocations(mut self, max: usize) -> Self {
        self.max_invocations = max.max(1);
        self
    }

    /// Sets the initial-state page size for pagination testing.
    ///
    /// When set, the runner truncates the checkpoint log passed to each
    /// invocation to at most this many operations, simulating the service
    /// embedding only the first page in `InitialExecutionState`. The
    /// context then calls `get_state` to fetch the rest.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::LocalRunner;
    ///
    /// let runner = LocalRunner::new().initial_page_size(1);
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn initial_page_size(mut self, size: usize) -> Self {
        self.initial_page_size = Some(size.max(1));
        self
    }

    /// Sets the checkpoint-response page size for pagination testing.
    ///
    /// When set, the backend's checkpoint response includes a pagination
    /// marker once total stored operations exceed this threshold, forcing
    /// the context to paginate via `get_state`.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::LocalRunner;
    ///
    /// let runner = LocalRunner::new().checkpoint_page_size(1);
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn checkpoint_page_size(mut self, size: usize) -> Self {
        self.checkpoint_page_size = Some(size.max(1));
        self
    }

    /// Sets the [`Options`](crate::Options) every run passes to
    /// [`wrap`](crate::wrap).
    ///
    /// Every field applies as in production, except the transport.
    /// `sdk_config`, `lambda_client` and `execution_client` are ignored: the
    /// runner always uses its in-memory backend. So a test can pass the same
    /// `Options` its production handler uses.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::Options;
    /// use aws_durable_execution_sdk::test_util::LocalRunner;
    ///
    /// // Coalesce checkpoints in a 20 ms window.
    /// let runner = LocalRunner::new().options(
    ///     Options::builder()
    ///         .checkpoint_delay(std::time::Duration::from_millis(20))
    ///         .build()
    ///         .expect("valid options"),
    /// );
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn options(mut self, options: crate::Options) -> Self {
        self.options = options;
        self
    }

    /// Makes the simulated backend reject the next `count` checkpoint
    /// calls with a non-retryable error, persisting nothing for them,
    /// exactly like a permanent service-side rejection.
    ///
    /// A non-retryable checkpoint failure never reaches the handler: the
    /// SDK sends no further checkpoint and fails the execution. Use this
    /// to test that model, and to assert no record-transition lifecycle
    /// event is emitted for the rejected write (see
    /// [`crate::observability`]).
    ///
    /// It is [`fail_checkpoints_after(0, count)`](Self::fail_checkpoints_after).
    ///
    /// One checkpoint fault applies at a time: this call replaces the fault
    /// that an earlier `fail_next_checkpoints`, `fail_checkpoints_after` or
    /// `fail_checkpoints_after_retryable` call set.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::LocalRunner;
    ///
    /// let runner = LocalRunner::new().fail_next_checkpoints(1);
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn fail_next_checkpoints(self, count: usize) -> Self {
        self.fail_checkpoints_after(0, count)
    }

    /// Like [`fail_next_checkpoints`](Self::fail_next_checkpoints), but
    /// lets the first `skip` checkpoint calls through before rejecting
    /// the following `count` non-retryably.
    ///
    /// Use it to reject a specific write: a step's live path writes START
    /// first and its outcome second, so `fail_checkpoints_after(1, 1)`
    /// rejects exactly the step's outcome write, while the `START` before
    /// it persists.
    ///
    /// One checkpoint fault applies at a time: this call replaces the fault
    /// that an earlier `fail_next_checkpoints`, `fail_checkpoints_after` or
    /// `fail_checkpoints_after_retryable` call set.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::LocalRunner;
    ///
    /// let runner = LocalRunner::new().fail_checkpoints_after(1, 1);
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn fail_checkpoints_after(mut self, skip: usize, count: usize) -> Self {
        self.checkpoint_failure_skip = skip;
        self.checkpoint_failures = count;
        self.checkpoint_failures_retryable = false;
        self
    }

    /// Like [`fail_checkpoints_after`](Self::fail_checkpoints_after), but
    /// the injected failures are RETRYABLE: simulating a transient
    /// failure that exhausted the transport's own retries.
    ///
    /// A retryable checkpoint failure fails the invocation, and the SDK
    /// sends no further checkpoint in it; the runner then re-invokes,
    /// exactly as the durable service does, and replay resumes from the
    /// recorded state.
    ///
    /// One checkpoint fault applies at a time: this call replaces the fault
    /// that an earlier `fail_next_checkpoints`, `fail_checkpoints_after` or
    /// `fail_checkpoints_after_retryable` call set.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::LocalRunner;
    ///
    /// let runner = LocalRunner::new().fail_checkpoints_after_retryable(1, 1);
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn fail_checkpoints_after_retryable(mut self, skip: usize, count: usize) -> Self {
        self.checkpoint_failure_skip = skip;
        self.checkpoint_failures = count;
        self.checkpoint_failures_retryable = true;
        self
    }

    /// Answers checkpoint request number `n + 1` of the execution without a
    /// token, once.
    ///
    /// The count is 1-based over the checkpoint requests the execution
    /// receives, across invocations. The backend applies that request and
    /// returns no `CheckpointToken`, as the service does when it accepts no
    /// further checkpoints from an invocation. The SDK then sends nothing
    /// more and answers `PENDING`. The runner invokes the function again at
    /// once, as after an invocation fault, and the next invocation replays
    /// the accepted updates.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::LocalRunner;
    ///
    /// let runner = LocalRunner::new().withdraw_checkpoints_after(1);
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn withdraw_checkpoints_after(mut self, n: usize) -> Self {
        self.withdraw_after = Some(n);
        self
    }

    /// Delays each checkpoint response, as JS `checkpointDelay`, to expose races.
    /// Not the SDK's coalescing window.
    ///
    /// The backend waits `latency` in tokio time before it applies and
    /// answers each checkpoint request. The default is zero, which answers
    /// at once. The latency does not move the backend's virtual clock, so
    /// waits and retry delays end at the same virtual times as without it.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::time::Duration;
    ///
    /// use aws_durable_execution_sdk::test_util::LocalRunner;
    ///
    /// let runner = LocalRunner::new().checkpoint_latency(Duration::from_millis(50));
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn checkpoint_latency(mut self, latency: std::time::Duration) -> Self {
        self.checkpoint_latency = latency;
        self
    }

    /// Queues `reply` for the next pending callback, in order.
    ///
    /// A pending callback that a [`callback_for`](Self::callback_for)
    /// reply names takes that reply instead. See
    /// [Callback replies](Self#callback-replies).
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::{CallbackReply, LocalRunner};
    ///
    /// let runner = LocalRunner::new()
    ///     .callback(CallbackReply::raw(r#""first""#))
    ///     .callback(CallbackReply::timeout());
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn callback(mut self, reply: CallbackReply) -> Self {
        self.callback_replies.fifo.push_back(reply);
        self
    }

    /// Queues `reply` for the pending callback named `name`, or for the
    /// callback of the `wait_for_callback` named `name`.
    ///
    /// When no pending callback matches by the end of the run, the
    /// suspended result's message names `name`. See
    /// [Callback replies](Self#callback-replies).
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::{CallbackReply, LocalRunner};
    ///
    /// let runner = LocalRunner::new()
    ///     .callback_for("cb-a", CallbackReply::raw(r#""A""#))
    ///     .callback_for("cb-b", CallbackReply::raw(r#""B""#));
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn callback_for(mut self, name: impl Into<String>, reply: CallbackReply) -> Self {
        self.callback_replies.named.push((name.into(), reply));
        self
    }

    /// Registers `function` as the plain target of `ctx.invoke(name, ..)`.
    ///
    /// The name matches exactly: no qualifier, alias or ARN handling. A
    /// later registration of either kind under the same name replaces this
    /// one.
    ///
    /// When an invocation suspends, the runner runs every pending invoke
    /// whose target is registered, once, in the order the invokes were
    /// first checkpointed, before the virtual clock moves. A plain target
    /// decodes its input from the invoke's payload, runs, and encodes its
    /// output:
    ///
    /// - `Ok(value)` records the invoke `Succeeded` with the JSON of
    ///   `value`. `Ok(())` records `null`.
    /// - `Err(err)` records the invoke `Failed` with the error fields a
    ///   failing handler records: a [`TypedError`](crate::TypedError) keeps
    ///   its type and error data, and any other error records the type
    ///   `Error`.
    /// - An input that does not decode, or an output that does not encode,
    ///   records the invoke `Failed` with the type `Error` and a message
    ///   that starts `deserialize input:` or `serialize output:`.
    ///
    /// The target runs once per invoke, whatever its outcome. A replay of
    /// the invoke reads the recorded outcome.
    ///
    /// An invoke of a name that no registration matches fails the run: the
    /// backend rejects the invoke's checkpoint, and the result's
    /// [`error_message`](TestResult::error_message) names the function.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk as durable;
    /// use durable::test_util::LocalRunner;
    ///
    /// # #[tokio::main]
    /// # async fn main() {
    /// let result = LocalRunner::new()
    ///     .register_function("double", |x: i32| async move { Ok(x * 2) })
    ///     .run(
    ///         |n: i32, ctx: durable::DurableContext| async move {
    ///             let doubled = ctx.invoke::<i32>("double", n).await?;
    ///             Ok::<_, durable::BoxError>(doubled)
    ///         },
    ///         21_i32,
    ///     )
    ///     .await.expect("runner");
    ///
    /// assert_eq!(result.output(), Some(&42));
    /// # }
    /// ```
    #[must_use]
    pub fn register_function<I, O, F, Fut>(mut self, name: impl Into<String>, function: F) -> Self
    where
        I: DeserializeOwned + Send + 'static,
        O: Serialize + Send + 'static,
        F: Fn(I) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
    {
        let function = Arc::new(function);
        let target: PlainTarget = Arc::new(move |input: String| {
            let function = Arc::clone(&function);
            Box::pin(async move {
                let input: I = serde_json::from_str(&input)
                    .map_err(|e| -> BoxError { format!("deserialize input: {e}").into() })?;
                let output = function(input).await?;
                serde_json::to_string(&output)
                    .map_err(|e| -> BoxError { format!("serialize output: {e}").into() })
            })
        });
        self.functions
            .insert(name.into(), RegisteredFunction::Plain(target));
        self
    }

    /// Registers `handler` as the durable target of `ctx.invoke(name, ..)`.
    ///
    /// Name matching, replacement, and the time a target runs follow
    /// [`register_function`](Self::register_function). The handler runs as
    /// its own durable execution, driven to its end before the caller's
    /// next invocation:
    ///
    /// - The target runs once per invoke, whatever its outcome. A replay of
    ///   the invoke reads the recorded outcome.
    /// - The nested execution has a fresh backend and its own virtual
    ///   clock. It uses a copy of this runner's settings taken when the
    ///   caller's run starts: the [`Options`](crate::Options), the page
    ///   sizes, [`max_invocations`](Self::max_invocations) and
    ///   [`checkpoint_latency`](Self::checkpoint_latency). It has no
    ///   registered functions, no queued callback replies and no injected
    ///   checkpoint failures.
    /// - A nested execution that succeeds records the invoke `Succeeded`
    ///   with the handler's result, or `null` when the envelope carries
    ///   none. One that fails records the invoke `Failed` with the
    ///   handler's wire error: its type, message, error data and stack
    ///   trace. One that suspends, for example on a callback no reply
    ///   answers, leaves the invoke `Started`.
    /// - When the caller's run then ends suspended, because nothing can
    ///   advance, and at least one chained invoke is still `Started`, the
    ///   result's [`error_message`](TestResult::error_message) ends with
    ///   `; chained invokes still pending for: ` and the function names.
    ///   Each name is in double quotes, the names are separated by `, `,
    ///   they appear in the order the invokes were stored, and each name
    ///   appears once. This part comes after the part that names
    ///   unmatched queued callback replies. For example:
    ///   `; chained invokes still pending for: "approval", "audit"`.
    /// - The nested execution's invocations do not count in the caller's
    ///   [`invocation_count`](TestResult::invocation_count), and its
    ///   operations do not appear in the caller's
    ///   [`operations`](TestResult::operations).
    ///
    /// # Examples
    ///
    /// ```
    /// use std::time::Duration;
    ///
    /// use aws_durable_execution_sdk as durable;
    /// use durable::test_util::LocalRunner;
    ///
    /// # #[tokio::main]
    /// # async fn main() {
    /// let result = LocalRunner::new()
    ///     .register_durable_function("workflow", |n: u32, ctx: durable::DurableContext| async move {
    ///         let next = ctx.step(move |_| async move { Ok(n + 1) }).await?;
    ///         ctx.wait(Duration::from_secs(5)).await?;
    ///         Ok::<_, durable::BoxError>(next * 2)
    ///     })
    ///     .run(
    ///         |n: u32, ctx: durable::DurableContext| async move {
    ///             let out = ctx.invoke::<u32>("workflow", n).await?;
    ///             Ok::<_, durable::BoxError>(out)
    ///         },
    ///         1_u32,
    ///     )
    ///     .await.expect("runner");
    ///
    /// assert_eq!(result.output(), Some(&4));
    /// # }
    /// ```
    #[must_use]
    pub fn register_durable_function<E, O, F, Fut>(
        mut self,
        name: impl Into<String>,
        handler: F,
    ) -> Self
    where
        E: Serialize + DeserializeOwned + Send + 'static,
        O: Serialize + DeserializeOwned + Send + 'static,
        F: Fn(E, DurableContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<O, BoxError>> + Send,
    {
        let handler = Arc::new(handler);
        let target: DurableTarget = Arc::new(move |runner: LocalRunner, input: String| {
            let handler = Arc::clone(&handler);
            Box::pin(async move {
                let backend = Arc::new(runner.new_backend());
                let nested = move |event: E, ctx: DurableContext| handler(event, ctx);
                runner.drive_nested(backend, nested, input).await
            })
        });
        self.functions
            .insert(name.into(), RegisteredFunction::Durable(target));
        self
    }

    /// The settings of a nested durable run: a copy of this runner with no
    /// registered functions, no queued callback replies and the fault
    /// injection of [`LocalRunner::new`].
    fn nested_settings(&self) -> Self {
        let defaults = Self::new();
        let mut nested = self.clone();
        nested.functions = defaults.functions;
        nested.callback_replies = defaults.callback_replies;
        nested.checkpoint_failures = defaults.checkpoint_failures;
        nested.checkpoint_failure_skip = defaults.checkpoint_failure_skip;
        nested.checkpoint_failures_retryable = defaults.checkpoint_failures_retryable;
        nested.withdraw_after = defaults.withdraw_after;
        nested
    }

    /// Starts a run of `handler` with `event`, and returns its
    /// [`RunHandle`].
    ///
    /// The run starts nothing until the test polls one of its futures.
    /// Awaiting the handle drives `handler` to a terminal outcome. Before
    /// that, the test can wait for an operation and answer its callback
    /// through [`RunHandle::operation`], and pause the run with
    /// [`RunHandle::pause`] (see "Reacting to a run in progress" in the
    /// module docs).
    ///
    /// Each simulated invocation goes through the **production service
    /// function** ([`wrap`](crate::wrap)'s body) fed a synthesized
    /// invocation envelope, so envelope parsing, bootstrap pagination, the
    /// suspension driver, and wire error mapping are the exact code
    /// production runs. The service future is awaited inline, the same
    /// task topology `lambda_runtime` produces under `block_on`, so
    /// task-ownership behavior matches deployment.
    ///
    /// The event is serialized once and embedded in each invocation's
    /// envelope, mirroring the way the service re-delivers the input
    /// payload on every re-invocation.
    ///
    /// # Errors
    ///
    /// Awaiting the handle returns these runner errors, the run failures:
    ///
    /// - [`TestRunnerError::EventSerialization`] when `event` does not
    ///   serialize.
    /// - [`TestRunnerError::OutputDeserialization`] when the output of a
    ///   succeeded execution does not decode into `O`.
    /// - [`TestRunnerError::UnexpectedEnvelope`] when a response envelope
    ///   has a status other than `SUCCEEDED`, `FAILED` and `PENDING`.
    ///
    /// A [`wait_for_data`](OperationHandle::wait_for_data) pending when the
    /// run fails returns the run failure. One first polled after the run
    /// failed returns the stored operation when it already reached the
    /// requested status, and the run failure otherwise.
    /// [`send_callback`](OperationHandle::send_callback),
    /// [`send_callback_heartbeat`](OperationHandle::send_callback_heartbeat),
    /// [`pause`](RunHandle::pause) and [`resume`](RunHandle::resume) return
    /// the run failure.
    ///
    /// An invocation fault is not a runner error: the runner invokes again,
    /// as the service does.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk as durable;
    /// use durable::test_util::LocalRunner;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), durable::BoxError> {
    /// let result = LocalRunner::new()
    ///     .run(
    ///         |n: u32, ctx: durable::DurableContext| async move {
    ///             let v = ctx.step(move |_| async move { Ok(n + 1) }).await?;
    ///             Ok::<_, durable::BoxError>(v)
    ///         },
    ///         41_u32,
    ///     )
    ///     .await?;
    /// assert_eq!(result.output(), Some(&42));
    /// # Ok(())
    /// # }
    /// ```
    pub fn run<E, O, F, Fut>(&self, handler: F, event: E) -> RunHandle<O>
    where
        E: Serialize + DeserializeOwned + Send + 'static,
        O: Serialize + DeserializeOwned + Send + 'static,
        F: Fn(E, DurableContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<O, BoxError>> + Send,
    {
        self.run_configured(handler, event, |_backend| {})
    }

    /// [`run`](Self::run), with `configure` applied to the run's backend
    /// before the first invocation. Internal seam: a unit test plans the
    /// backend's checkpoint answers for a run it controls through the
    /// returned handle.
    #[expect(clippy::needless_pass_by_value)] // reason: the run takes the event, as the service delivers it
    fn run_configured<E, O, F, Fut>(
        &self,
        handler: F,
        event: E,
        configure: impl FnOnce(&Backend),
    ) -> RunHandle<O>
    where
        E: Serialize + DeserializeOwned + Send + 'static,
        O: Serialize + DeserializeOwned + Send + 'static,
        F: Fn(E, DurableContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<O, BoxError>> + Send,
    {
        let live = Arc::new(LiveState::new(BackendState::new(
            self.callback_replies.clone(),
        )));
        let backend = Arc::new(self.backend_on(StateCell::Live(Arc::clone(&live))));
        configure(&backend);
        let engine = Engine::Local;
        // Serialize the event once; the envelope re-delivers it per
        // invocation.
        let event_json = match serde_json::to_string(&event) {
            Ok(json) => json,
            Err(e) => {
                return RunHandle::from_shared(RunCore::failed(
                    live,
                    engine,
                    TestRunnerError::EventSerialization(Arc::new(e)),
                ));
            }
        };
        let runner = self.clone();
        let driver = async move {
            let result: TestResult<O> = runner
                .drive(backend, handler, event_json)
                .await?
                .into_test_result()?;
            Ok(Box::new(result) as Box<dyn std::any::Any + Send>)
        };
        RunHandle::from_shared(RunCore::new(live, engine, Box::pin(driver)))
    }

    /// A fresh backend configured from this runner's settings: queued
    /// callback replies, checkpoint page size, registered function names,
    /// injected checkpoint failures and checkpoint latency.
    fn new_backend(&self) -> Backend {
        self.backend_on(StateCell::own(BackendState::new(
            self.callback_replies.clone(),
        )))
    }

    /// [`new_backend`](Self::new_backend) with its records in `cell`.
    fn backend_on(&self, cell: StateCell) -> Backend {
        let mut backend = Backend::on(cell, self.checkpoint_page_size);
        backend.registered_functions = self.functions.keys().cloned().collect();
        backend.checkpoint_failures_remaining.store(
            self.checkpoint_failures,
            std::sync::atomic::Ordering::SeqCst,
        );
        backend.checkpoint_failures_skip.store(
            self.checkpoint_failure_skip,
            std::sync::atomic::Ordering::SeqCst,
        );
        backend.checkpoint_failures_retryable.store(
            self.checkpoint_failures_retryable,
            std::sync::atomic::Ordering::SeqCst,
        );
        backend.checkpoint_latency = self.checkpoint_latency;
        backend.withdraw_at = self.withdraw_after.map(|n| n.saturating_add(1));
        backend
    }

    /// The invoke → parse → advance loop over an externally supplied
    /// backend, with the event as JSON. Internal seam: unit tests inject a
    /// backend they retain a handle to, so they can assert on
    /// transport-level facts (e.g. how many `get_state` fetches pagination
    /// forced).
    #[cfg(test)]
    async fn run_on_backend<E, O, F, Fut>(
        &self,
        backend: Arc<Backend>,
        handler: F,
        event_json: String,
    ) -> Result<TestResult<O>, TestRunnerError>
    where
        E: Serialize + DeserializeOwned + Send + 'static,
        O: Serialize + DeserializeOwned + Send + 'static,
        F: Fn(E, DurableContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<O, BoxError>> + Send,
    {
        self.drive(backend, handler, event_json)
            .await?
            .into_test_result()
    }

    /// Drives one execution on `backend` and returns its end with the
    /// handler output still raw JSON. [`run_on_backend`](Self::run_on_backend)
    /// decodes the output; a nested durable run keeps it raw.
    async fn drive<E, O, F, Fut>(
        &self,
        backend: Arc<Backend>,
        handler: F,
        event_json: String,
    ) -> Result<RawRun, TestRunnerError>
    where
        E: Serialize + DeserializeOwned + Send + 'static,
        O: Serialize + DeserializeOwned + Send + 'static,
        F: Fn(E, DurableContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<O, BoxError>> + Send,
    {
        let client: Arc<dyn ExecutionClient> = Arc::clone(&backend) as Arc<dyn ExecutionClient>;

        // The handler runs behind the SAME service function production
        // registers with the Lambda runtime, with the caller's `Options`:
        // only the transport is faked. The three transport fields are set
        // directly, so `build()`'s check that rejects `execution_client`
        // together with `lambda_client` does not apply.
        let mut options = self.options.clone();
        options.sdk_config = None;
        options.lambda_client = None;
        options.execution_client = Some(client);
        let service = crate::wrap(handler, options);
        self.drive_service(backend, service, event_json).await
    }

    /// Drives a nested durable run and returns its outcome.
    async fn drive_nested<E, O, F, Fut>(
        &self,
        backend: Arc<Backend>,
        handler: F,
        event_json: String,
    ) -> NestedOutcome
    where
        E: Serialize + DeserializeOwned + Send + 'static,
        O: Serialize + DeserializeOwned + Send + 'static,
        F: Fn(E, DurableContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<O, BoxError>> + Send,
    {
        // `drive` runs the handler behind `crate::wrap`. Every envelope
        // `wrap` returns has the status SUCCEEDED, FAILED or PENDING. The
        // only runner error `drive_service` returns is `UnexpectedEnvelope`,
        // for any other status. So `drive` returns no runner error here.
        #[expect(clippy::expect_used)]
        let run = self
            .drive(backend, handler, event_json)
            .await
            .expect("a run behind `wrap` returns no runner error");
        run.nested_outcome()
    }

    /// The invoke → parse → advance loop of [`drive`](Self::drive), with the
    /// service function as a parameter. Internal seam: a unit test passes a
    /// service that returns an envelope `wrap` never produces.
    #[expect(clippy::too_many_lines)] // reason: one loop over the invocations reads better as one flow
    async fn drive_service<S>(
        &self,
        backend: Arc<Backend>,
        service: S,
        event_json: String,
    ) -> Result<RawRun, TestRunnerError>
    where
        S: Fn(
            lambda_runtime::LambdaEvent<crate::InvocationPayload>,
        ) -> crate::BoxedInvocationFuture,
    {
        // A nested durable run uses the settings this run starts with.
        let nested = self.nested_settings();

        let mut invocations = 0_usize;
        // The most recent invocation fault (Lambda runtime error), kept so
        // an execution that never terminates because every invocation
        // faults reports the fault rather than a generic budget message.
        let mut last_invocation_fault: Option<String> = None;

        backend.record_execution_started(&event_json);
        loop {
            invocations += 1;
            if invocations > self.max_invocations {
                backend.close();
                return Ok(RawRun {
                    end: RawEnd::Suspended {
                        error_type: last_invocation_fault
                            .is_some()
                            .then(|| "RuntimeError".to_owned()),
                        message: last_invocation_fault.map_or_else(
                            || {
                                format!(
                                    "execution did not terminate within {} invocations",
                                    self.max_invocations
                                )
                            },
                            |fault| {
                                format!(
                                    "execution did not terminate within {} invocations; \
                                     last invocation fault: {fault}",
                                    self.max_invocations
                                )
                            },
                        ),
                    },
                    operations: backend.snapshot_operations(),
                    invocations: invocations - 1,
                    events: backend.events(),
                });
            }

            // The hold rule: an invocation starts only while a runner
            // future waits and the run is not paused. The check and the
            // start of the invocation are one transition on the run state, so
            // a `pause` either sees the invocation in progress or keeps it
            // from starting.
            backend.admit_invocation().await;
            let payload = build_envelope(&backend, &event_json, self.initial_page_size);
            let mut context = lambda_runtime::Context::default();
            context.request_id = format!("local-invocation-{invocations}");
            let started_at = backend.now();
            let lambda_event = lambda_runtime::LambdaEvent::new(
                crate::InvocationPayload(payload),
                context.clone(),
            );

            // Await the service future INLINE, never on a spawned task.
            // This reproduces the production topology (`lambda_runtime`
            // awaits the handler under `block_on`), so
            // `tokio::task::try_id()` is `None` at context-creation time
            // and the task-ownership guard behaves as it does deployed.
            let response = service(lambda_event).await;
            // An accepted `EXECUTION` update ends the execution, as on the
            // service, even when the invocation then failed.
            let checkpointed = backend.execution_result();
            // A terminal envelope ends the run, and so does an accepted
            // `EXECUTION` update when the invocation then failed: a reply
            // after this point is refused, in the same transition that ends
            // the invocation.
            let closing = match &response {
                Ok(envelope) => {
                    envelope.0.get("Status").and_then(serde_json::Value::as_str) != Some("PENDING")
                }
                Err(_) => checkpointed.is_some(),
            };
            let response = match response {
                Ok(envelope) => envelope.0,
                Err(e) => {
                    // The invocation itself failed (a Lambda runtime
                    // error), e.g. a retryable checkpoint failure that
                    // exhausted transport retries (issue #43), or a
                    // bootstrap failure. The durable service re-invokes
                    // on an invocation fault, so the runner does too;
                    // `max_invocations` bounds a persistent fault.
                    let fault = e.to_string();
                    backend.finish_invocation(
                        closing,
                        started_at,
                        &context.request_id,
                        Some(&crate::WireError::new(None::<String>, Some(fault.as_str()))),
                    );
                    if checkpointed.is_some() {
                        let end = RawEnd::Succeeded(checkpointed);
                        backend.record_execution_end(&end);
                        return Ok(RawRun {
                            end,
                            operations: backend.snapshot_operations(),
                            invocations,
                            events: backend.events(),
                        });
                    }
                    last_invocation_fault = Some(fault);
                    continue;
                }
            };

            let status = response.get("Status").and_then(serde_json::Value::as_str);
            backend.finish_invocation(
                closing,
                started_at,
                &context.request_id,
                (status == Some("FAILED"))
                    .then(|| envelope_wire_error(&response))
                    .as_ref(),
            );
            let end = match status {
                Some("SUCCEEDED") => RawEnd::Succeeded(
                    match response.get("Result").and_then(serde_json::Value::as_str) {
                        // The result is too large for the envelope: the SDK
                        // checkpointed it as the `EXECUTION` update.
                        Some("") => Some(checkpointed.unwrap_or_default()),
                        result => result.map(ToOwned::to_owned),
                    },
                ),
                Some("FAILED") => RawEnd::Failed(envelope_wire_error(&response)),
                Some("PENDING") => {
                    // A withdrawn checkpoint token ends the invocation as an
                    // invocation fault does: the next one starts at once.
                    let withdrawn = backend.take_withdrawal();
                    if self.between_invocations(&backend, &nested, withdrawn).await {
                        continue;
                    }
                    RawEnd::Suspended {
                        error_type: None,
                        message: suspended_message(
                            &backend.unmatched_reply_names(),
                            &backend.pending_invoke_names(),
                        ),
                    }
                }
                other => {
                    return Err(TestRunnerError::UnexpectedEnvelope(
                        other.map(ToOwned::to_owned),
                    ));
                }
            };
            backend.record_execution_end(&end);
            return Ok(RawRun {
                end,
                operations: backend.snapshot_operations(),
                invocations,
                events: backend.events(),
            });
        }
    }

    /// Decides, after a `PENDING` invocation, whether the next invocation
    /// starts. Returns `false` when the execution is stuck: nothing can
    /// advance it.
    ///
    /// `start` is set when the invocation's checkpoint token was withdrawn.
    /// A callback reply sent while the run was in progress also starts the
    /// next invocation. Otherwise pending invokes run before the clock moves,
    /// and then the backend advances (timers, retries, queued callback
    /// replies).
    ///
    /// While the run is paused, the backend keeps advancing, and the
    /// invocation those completions would start waits for `resume`. Each
    /// decision is one transition of [`Backend::between_step`] on the run
    /// state, so the hold rule, a live reply and the clock advance are
    /// decided together.
    async fn between_invocations(&self, backend: &Backend, nested: &Self, mut start: bool) -> bool {
        let mut invokes_tried = false;
        loop {
            match backend.between_step(&mut start, invokes_tried).await {
                BetweenStep::Start => return true,
                BetweenStep::Stuck => return false,
                BetweenStep::RunInvokes => {
                    invokes_tried = true;
                    // Pending invokes run before the clock moves. When one
                    // of them changed its record, the next invocation starts
                    // without an advance.
                    if self.run_pending_invokes(backend, nested).await {
                        return true;
                    }
                }
            }
        }
    }

    /// Runs each pending invoke with a registered target once, inline, in
    /// stored order, and records its outcome. Returns whether at least one
    /// record changed.
    ///
    /// The backend lock is not held while a target runs: the pending
    /// invokes are collected first, then each target is awaited.
    async fn run_pending_invokes(&self, backend: &Backend, nested: &Self) -> bool {
        let mut changed = false;
        for (id, target, input) in backend.take_pending_invokes(&self.functions) {
            let outcome = match target {
                RegisteredFunction::Plain(function) => match function(input).await {
                    Ok(json) => NestedOutcome::Succeeded(Some(json)),
                    Err(err) => NestedOutcome::from_error(err),
                },
                RegisteredFunction::Durable(handler) => handler(nested.clone(), input).await,
            };
            changed |= backend.record_invoke_outcome(&id, outcome);
        }
        changed
    }
}

/// One decision of [`LocalRunner::between_invocations`] (see
/// [`between_decision`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BetweenStep {
    /// Start the next invocation.
    Start,
    /// Nothing can advance the execution: the run ends suspended.
    Stuck,
    /// Run the pending invokes, then decide again.
    RunInvokes,
}

/// What one [`between_decision`] decided.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Decided {
    /// The step [`LocalRunner::between_invocations`] takes.
    Step(BetweenStep),
    /// A paused run advanced: decide again at once.
    Again,
    /// Wait for the run state to change: no runner future waits, or the
    /// run is paused and nothing can advance.
    Wait,
}

/// How one driven execution ended, with the handler output still raw JSON.
#[derive(Debug)]
struct RawRun {
    end: RawEnd,
    operations: Vec<TestOperation>,
    invocations: usize,
    /// The history events the backend recorded.
    events: Vec<Event>,
}

/// The end of a driven execution (see [`RawRun`]).
#[derive(Debug)]
enum RawEnd {
    /// A `SUCCEEDED` envelope, with its raw `Result` string.
    Succeeded(Option<String>),
    /// A `FAILED` envelope, with the wire error of its `Error` object.
    Failed(crate::WireError),
    /// The execution did not terminate.
    Suspended {
        error_type: Option<String>,
        message: String,
    },
}

impl RawRun {
    /// The [`TestResult`] of a top-level run: the output decoded into `O`.
    fn into_test_result<O: DeserializeOwned>(self) -> Result<TestResult<O>, TestRunnerError> {
        let Self {
            end,
            operations,
            invocations,
            events,
        } = self;
        let (status, output, error) = match end {
            RawEnd::Succeeded(raw) => {
                let output = serde_json::from_str::<O>(raw.as_deref().unwrap_or("null"))
                    .map_err(|e| TestRunnerError::OutputDeserialization(Arc::new(e)))?;
                (ExecutionStatus::Succeeded, Some(output), None)
            }
            RawEnd::Failed(wire) => {
                // A missing `ErrorType` reads `Error`, the fallback the
                // handler mapping uses.
                let error = crate::WireError::new(
                    Some(wire.error_type().unwrap_or("Error")),
                    Some(wire.error_message().unwrap_or_default()),
                )
                .with_error_data(wire.error_data())
                .with_stack_trace(wire.stack_trace().to_vec());
                (ExecutionStatus::Failed, None, Some(error))
            }
            RawEnd::Suspended {
                error_type,
                message,
            } => (
                ExecutionStatus::Running,
                None,
                Some(crate::WireError::new(error_type, Some(message))),
            ),
        };
        Ok(TestResult::with_history(
            status,
            output,
            error,
            operations,
            invocations,
            events,
        ))
    }

    /// The outcome a nested durable run records on the caller's invoke.
    fn nested_outcome(self) -> NestedOutcome {
        match self.end {
            RawEnd::Succeeded(raw) => NestedOutcome::Succeeded(raw),
            RawEnd::Failed(wire) => NestedOutcome::Failed(wire),
            RawEnd::Suspended { .. } => NestedOutcome::Suspended,
        }
    }
}

/// The wire error of a `FAILED` envelope's `Error` object: its
/// `ErrorType`, `ErrorMessage`, `ErrorData` and `StackTrace`.
fn envelope_wire_error(response: &serde_json::Value) -> crate::WireError {
    let error = response.get("Error");
    let field = |key: &str| {
        error
            .and_then(|e| e.get(key))
            .and_then(serde_json::Value::as_str)
    };
    crate::WireError::new(field("ErrorType"), field("ErrorMessage"))
        .with_error_data(field("ErrorData"))
        .with_stack_trace(error.and_then(crate::parse_stack_trace).unwrap_or_default())
}

/// The execution ARN the local runner stamps on every synthesized envelope.
const LOCAL_EXECUTION_ARN: &str = "arn:aws:lambda:us-west-2:000000000000:function:local-test";

/// Synthesizes the durable invocation envelope for one simulated
/// invocation, in the exact wire shape the service delivers and
/// [`wrap`](crate::wrap) parses: `DurableExecutionArn`, `CheckpointToken`,
/// and `InitialExecutionState.Operations` with the customer input embedded
/// in the leading `Execution` operation's `ExecutionDetails.InputPayload`.
/// `UpdatedOperationIds` lists the records the backend changed between
/// invocations, and building the envelope clears that list (see
/// [`Backend::take_updated_ids`]).
///
/// When `initial_page_size` is set and the recorded history exceeds it,
/// only the first page of history rides inline and
/// `InitialExecutionState.NextMarker` signals the truncation: the
/// production bootstrap path then fetches the remainder via `get_state`,
/// exactly as it does against the paginating service.
fn build_envelope(
    backend: &Backend,
    event_json: &str,
    initial_page_size: Option<usize>,
) -> serde_json::Value {
    let history = backend.envelope_history();
    let (page, next_marker) = match initial_page_size {
        Some(size) if history.len() > size => (
            history.get(..size).unwrap_or(&history).to_vec(),
            Some(format!("initial-marker-{}", history.len())),
        ),
        _ => (history, None),
    };

    let mut operations = Vec::with_capacity(page.len() + 1);
    operations.push(serde_json::json!({
        "Id": EXECUTION_OPERATION_ID,
        "Type": "EXECUTION",
        "Status": "STARTED",
        "StartTimestamp": virtual_epoch_millis(backend.base(), 0),
        "ExecutionDetails": { "InputPayload": event_json }
    }));
    operations.extend(page);

    let initial_state = match next_marker {
        Some(marker) => serde_json::json!({
            "Operations": operations,
            "NextMarker": marker,
        }),
        None => serde_json::json!({ "Operations": operations }),
    };

    serde_json::json!({
        "DurableExecutionArn": LOCAL_EXECUTION_ARN,
        "CheckpointToken": backend.current_token(),
        "InitialExecutionState": initial_state,
        "UpdatedOperationIds": backend.take_updated_ids(),
    })
}

/// Default delay between execution-status polls, as JS.
const DEFAULT_POLL_INTERVAL: std::time::Duration = std::time::Duration::from_secs(1);

/// How [`CloudRunner`] invokes the function.
///
/// `DryRun` is not offered: it returns no execution ARN.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum InvocationType {
    /// A synchronous invoke. It returns after the execution ended or the
    /// invocation reached its timeout.
    #[default]
    RequestResponse,
    /// An asynchronous invoke. It returns once the execution started.
    Event,
}

impl InvocationType {
    /// The service model value of this invocation type.
    fn to_wire(self) -> WireInvocationType {
        match self {
            Self::RequestResponse => WireInvocationType::RequestResponse,
            Self::Event => WireInvocationType::Event,
        }
    }
}

/// The fields of one `Invoke` request that [`CloudRunner`] sends.
#[derive(Debug, Clone, PartialEq, Eq)]
struct InvokeRequest {
    function_name: String,
    qualifier: String,
    invocation_type: WireInvocationType,
    payload: Vec<u8>,
}

/// The Lambda calls [`CloudRunner::run`] and its live controls make. They
/// call only this trait, so a unit test can supply a fake that returns
/// scripted outputs and errors.
trait CloudApi {
    /// Sends `Invoke`.
    fn send_invoke(
        &self,
        request: InvokeRequest,
    ) -> impl Future<Output = Result<aws_sdk_lambda::operation::invoke::InvokeOutput, BoxError>> + Send;

    /// Sends `GetDurableExecution` with the execution data.
    fn send_get_durable_execution(
        &self,
        execution_arn: &str,
    ) -> impl Future<
        Output = Result<
            aws_sdk_lambda::operation::get_durable_execution::GetDurableExecutionOutput,
            BoxError,
        >,
    > + Send;

    /// Sends `GetDurableExecutionHistory` with the execution data, for the
    /// page that `marker` names.
    fn send_get_durable_execution_history(
        &self,
        execution_arn: &str,
        marker: Option<&str>,
    ) -> impl Future<
        Output = Result<
            aws_sdk_lambda::operation::get_durable_execution_history::GetDurableExecutionHistoryOutput,
            BoxError,
        >,
    > + Send;

    /// Sends `SendDurableExecutionCallbackSuccess` with `result` as the
    /// payload.
    fn send_callback_success(
        &self,
        callback_id: &str,
        result: Vec<u8>,
    ) -> impl Future<Output = Result<(), BoxError>> + Send;

    /// Sends `SendDurableExecutionCallbackFailure` with `error`.
    fn send_callback_failure(
        &self,
        callback_id: &str,
        error: ErrorObject,
    ) -> impl Future<Output = Result<(), BoxError>> + Send;

    /// Sends `SendDurableExecutionCallbackHeartbeat`.
    fn send_callback_heartbeat(
        &self,
        callback_id: &str,
    ) -> impl Future<Output = Result<(), BoxError>> + Send;
}

impl CloudApi for crate::LambdaClient {
    async fn send_invoke(
        &self,
        request: InvokeRequest,
    ) -> Result<aws_sdk_lambda::operation::invoke::InvokeOutput, BoxError> {
        Ok(self
            .invoke()
            .function_name(request.function_name)
            .invocation_type(request.invocation_type)
            .qualifier(request.qualifier)
            .payload(Blob::new(request.payload))
            .send()
            .await?)
    }

    async fn send_get_durable_execution(
        &self,
        execution_arn: &str,
    ) -> Result<aws_sdk_lambda::operation::get_durable_execution::GetDurableExecutionOutput, BoxError>
    {
        Ok(self
            .get_durable_execution()
            .durable_execution_arn(execution_arn)
            .include_execution_data(true)
            .send()
            .await?)
    }

    async fn send_get_durable_execution_history(
        &self,
        execution_arn: &str,
        marker: Option<&str>,
    ) -> Result<
        aws_sdk_lambda::operation::get_durable_execution_history::GetDurableExecutionHistoryOutput,
        BoxError,
    > {
        Ok(self
            .get_durable_execution_history()
            .durable_execution_arn(execution_arn)
            .include_execution_data(true)
            .set_marker(marker.map(ToOwned::to_owned))
            .send()
            .await?)
    }

    async fn send_callback_success(
        &self,
        callback_id: &str,
        result: Vec<u8>,
    ) -> Result<(), BoxError> {
        self.send_durable_execution_callback_success()
            .callback_id(callback_id)
            .result(Blob::new(result))
            .send()
            .await?;
        Ok(())
    }

    async fn send_callback_failure(
        &self,
        callback_id: &str,
        error: ErrorObject,
    ) -> Result<(), BoxError> {
        self.send_durable_execution_callback_failure()
            .callback_id(callback_id)
            .error(error)
            .send()
            .await?;
        Ok(())
    }

    async fn send_callback_heartbeat(&self, callback_id: &str) -> Result<(), BoxError> {
        self.send_durable_execution_callback_heartbeat()
            .callback_id(callback_id)
            .send()
            .await?;
        Ok(())
    }
}

/// The [`TestRunnerError::Aws`] of a failed Lambda call.
fn aws_error(err: BoxError) -> TestRunnerError {
    TestRunnerError::Aws(Arc::from(err))
}

/// Drives a durable function against **real AWS** and returns the same
/// [`TestResult`] / [`TestOperation`] types [`LocalRunner`] returns.
///
/// `CloudRunner` invokes a deployed durable function, with the
/// [`invocation_type`](Self::invocation_type) `RequestResponse` by default,
/// polls [`GetDurableExecution`] until the execution reaches a terminal
/// state, then folds the recorded [`GetDurableExecutionHistory`] event
/// stream into per-operation [`TestOperation`]s. Because both runners return
/// the same types, a test can be re-pointed from in-memory to real AWS by
/// swapping the runner. The runner reads only `DurableExecutionArn` from the
/// invoke response and ignores its `FunctionError` and `Payload`: the
/// history is the source of the result.
///
/// By default the Lambda client is built from the ambient credentials and
/// region (the same way the production entry point builds it). Configure the
/// region with [`region`](Self::region), or supply the client with
/// [`lambda_client`](Self::lambda_client).
///
/// A failed Lambda call or an invoke response without an execution ARN
/// is a [`TestRunnerError`]. A durable execution that fails, times out or
/// stops is an `Ok` [`TestResult`] with that [`ExecutionStatus`].
///
/// The runner paces and retries its reads as the JS history poller does. It
/// waits one [`poll_interval`](Self::poll_interval) before each
/// `GetDurableExecution` poll, and one interval before each history page
/// that follows a page with a next marker. Each `GetDurableExecution` call
/// and each history page request makes up to three attempts on any error:
/// the second starts 1000 ms after the first fails, the third 1450 ms after
/// the second fails, and the third attempt's error is the call's error.
/// `Invoke` and the callback calls make one attempt.
///
/// [`GetDurableExecution`]: https://docs.aws.amazon.com/lambda/latest/api/API_GetDurableExecution.html
/// [`GetDurableExecutionHistory`]: https://docs.aws.amazon.com/lambda/latest/api/API_GetDurableExecutionHistory.html
///
/// # Examples
///
/// ```no_run
/// use aws_durable_execution_sdk as durable;
/// use durable::test_util::CloudRunner;
/// use std::time::Duration;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), durable::BoxError> {
/// let result = CloudRunner::new("my-durable-function")
///     .region("us-west-2")
///     .poll_interval(Duration::from_secs(3))
///     .run::<i32>(21_i32)
///     .await?;
///
/// if result.is_success() {
///     assert_eq!(result.output(), Some(&42));
///     for op in result.operations() {
///         println!("{} {} {}", op.id(), op.kind(), op.status());
///     }
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct CloudRunner {
    function_name: String,
    region: Option<String>,
    qualifier: String,
    poll_interval: std::time::Duration,
    max_poll_attempts: Option<usize>,
    invocation_type: InvocationType,
    lambda_client: Option<crate::LambdaClient>,
}

impl CloudRunner {
    /// Creates a runner targeting the named deployed durable function.
    ///
    /// `function_name` may be a function name or a fully-qualified ARN. The
    /// qualifier defaults to `$LATEST` (durable functions require a qualified
    /// invocation); override it with [`qualifier`](Self::qualifier).
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::CloudRunner;
    ///
    /// let runner = CloudRunner::new("order-processor");
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn new(function_name: impl Into<String>) -> Self {
        Self {
            function_name: function_name.into(),
            region: None,
            qualifier: String::from("$LATEST"),
            poll_interval: DEFAULT_POLL_INTERVAL,
            max_poll_attempts: None,
            invocation_type: InvocationType::default(),
            lambda_client: None,
        }
    }

    /// Sets the AWS region used to build the Lambda client.
    ///
    /// When unset, the region is resolved from the ambient environment
    /// (`AWS_REGION`, profile, or instance metadata). Ignored when
    /// [`lambda_client`](Self::lambda_client) is set.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::CloudRunner;
    ///
    /// let runner = CloudRunner::new("f").region("us-west-2");
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn region(mut self, region: impl Into<String>) -> Self {
        self.region = Some(region.into());
        self
    }

    /// Sets the invocation qualifier (function version or alias).
    ///
    /// Defaults to `$LATEST`.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::CloudRunner;
    ///
    /// let runner = CloudRunner::new("f").qualifier("PROD");
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn qualifier(mut self, qualifier: impl Into<String>) -> Self {
        self.qualifier = qualifier.into();
        self
    }

    /// Sets the delay between execution-status polls.
    ///
    /// Defaults to 1 second.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::CloudRunner;
    /// use std::time::Duration;
    ///
    /// let runner = CloudRunner::new("f").poll_interval(Duration::from_secs(5));
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn poll_interval(mut self, interval: std::time::Duration) -> Self {
        self.poll_interval = interval;
        self
    }

    /// Sets the maximum number of `GetDurableExecution` polls. A run that
    /// reaches it while the execution still runs returns a [`TestResult`]
    /// with [`ExecutionStatus::Running`]. A value of `0` counts as `1`.
    ///
    /// By default there is no limit: the runner polls until the execution
    /// ends or a `GetDurableExecution` poll fails.
    ///
    /// A poll counts once however many attempts it makes (see
    /// [`CloudRunner`]), and a history read of a live control counts once
    /// however many pages and attempts it makes. The history read that
    /// builds the final [`TestResult`] does not count.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::CloudRunner;
    ///
    /// let runner = CloudRunner::new("f").max_poll_attempts(30);
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn max_poll_attempts(mut self, max: usize) -> Self {
        self.max_poll_attempts = Some(max.max(1));
        self
    }

    /// Sets how the runner invokes the function.
    ///
    /// Default `RequestResponse`, as JS. A test that answers callbacks while the
    /// execution runs sets `InvocationType::Event`, as every JS callback example does.
    /// Under `RequestResponse`, waiting for an operation's data and the live
    /// callback controls return an error and make no Lambda call.
    ///
    /// # Examples
    ///
    /// ```
    /// use aws_durable_execution_sdk::test_util::{CloudRunner, InvocationType};
    ///
    /// let runner = CloudRunner::new("f").invocation_type(InvocationType::Event);
    /// # drop(runner);
    /// ```
    #[must_use]
    pub fn invocation_type(mut self, invocation_type: InvocationType) -> Self {
        self.invocation_type = invocation_type;
        self
    }

    /// Use this client for every call. `region` is then ignored.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use aws_durable_execution_sdk as durable;
    /// use durable::test_util::CloudRunner;
    ///
    /// # #[tokio::main]
    /// # async fn main() {
    /// let config = aws_config::load_from_env().await;
    /// let runner = CloudRunner::new("f").lambda_client(durable::LambdaClient::new(&config));
    /// # drop(runner);
    /// # }
    /// ```
    #[must_use]
    pub fn lambda_client(mut self, client: crate::LambdaClient) -> Self {
        self.lambda_client = Some(client);
        self
    }

    /// Starts a run with `event`, and returns its [`RunHandle`].
    ///
    /// The run starts nothing until the test polls one of its futures.
    /// Awaiting the handle invokes the durable function, polls it to a
    /// terminal state, and returns the folded [`TestResult`]. Under
    /// [`InvocationType::Event`] the test can wait for an operation and
    /// answer its callback before that, through [`RunHandle::operation`].
    ///
    /// The event is serialized to JSON and delivered as the invocation
    /// payload. The handler output (on success) is deserialized into `O`,
    /// the only type parameter: name it with `run::<O>(event)`.
    ///
    /// # Errors
    ///
    /// Awaiting the handle returns these runner errors, the run failures.
    /// Only the run's own calls cause one:
    ///
    /// - [`TestRunnerError::EventSerialization`] when `event` does not
    ///   serialize.
    /// - [`TestRunnerError::Aws`] when `Invoke` fails, when the third
    ///   attempt of a `GetDurableExecution` poll fails, or when the history
    ///   read that builds the [`TestResult`] fails.
    /// - [`TestRunnerError::NoExecutionArn`] when the invoke response has no
    ///   execution ARN.
    /// - [`TestRunnerError::OutputDeserialization`] when the output of a
    ///   succeeded execution does not decode into `O`.
    ///
    /// A [`wait_for_data`](OperationHandle::wait_for_data) pending when the
    /// run fails returns the run failure. One first polled after the run
    /// failed returns the operation in the newest history read when it
    /// already reached the requested status, and the run failure otherwise.
    ///
    /// A [`send_callback`](OperationHandle::send_callback) or
    /// [`send_callback_heartbeat`](OperationHandle::send_callback_heartbeat)
    /// returns the run failure when the run fails before the invoke
    /// returned the execution ARN. Once the ARN is known, it goes on after
    /// a run failure and returns its own outcome.
    ///
    /// [`pause`](RunHandle::pause) and [`resume`](RunHandle::resume), every
    /// control under [`InvocationType::RequestResponse`], and
    /// [`CallbackReply::timeout`] and [`CallbackReply::heartbeat_timeout`]
    /// return their [`TestRunnerError::Unsupported`] error, also after a run
    /// failure.
    ///
    /// A failed history read or callback call of a control returns
    /// [`TestRunnerError::Aws`] from that control only. It is not a run
    /// failure: the run goes on, and the other controls are not affected.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use aws_durable_execution_sdk as durable;
    /// use durable::test_util::CloudRunner;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), durable::BoxError> {
    /// let result = CloudRunner::new("echo-fn")
    ///     .run::<String>("hello".to_owned())
    ///     .await?;
    /// assert!(result.is_success() || result.is_failure() || result.is_suspended());
    /// # Ok(())
    /// # }
    /// ```
    pub fn run<O>(&self, event: impl Serialize) -> RunHandle<O>
    where
        O: DeserializeOwned + Send + 'static,
    {
        let runner = self.clone();
        self.start(
            async move { Arc::new(runner.resolve_client().await) },
            serde_json::to_vec(&event),
        )
    }

    /// The run of [`run`](Self::run) over the [`CloudApi`] `api`. Internal
    /// seam: unit tests pass a fake.
    #[cfg(test)]
    fn run_with<A, O>(&self, api: Arc<A>, payload: Vec<u8>) -> RunHandle<O>
    where
        A: CloudApi + Send + Sync + 'static,
        O: DeserializeOwned + Send + 'static,
    {
        self.start(async move { api }, Ok(payload))
    }

    /// Starts a run whose client `resolve` returns, with the serialized
    /// event `payload`.
    fn start<A, O, R>(
        &self,
        resolve: R,
        payload: Result<Vec<u8>, serde_json::Error>,
    ) -> RunHandle<O>
    where
        A: CloudApi + Send + Sync + 'static,
        O: DeserializeOwned + Send + 'static,
        R: Future<Output = Arc<A>> + Send + 'static,
    {
        // A `CloudRunner` run keeps no backend records.
        let live = Arc::new(LiveState::new(BackendState::new(QueuedReplies::default())));
        let cloud = Arc::new(CloudShared::<A>::new(self, Arc::clone(&live)));
        let engine = Engine::Cloud(Arc::clone(&cloud) as Arc<dyn CloudControl>);
        let payload = match payload {
            Ok(payload) => payload,
            Err(e) => {
                return RunHandle::from_shared(RunCore::failed(
                    live,
                    engine,
                    TestRunnerError::EventSerialization(Arc::new(e)),
                ));
            }
        };
        let runner = self.clone();
        let driver = async move {
            let api = resolve.await;
            let result: TestResult<O> = runner.drive(&cloud, api, payload).await?;
            Ok(Box::new(result) as Box<dyn std::any::Any + Send>)
        };
        RunHandle::from_shared(RunCore::new(live, engine, Box::pin(driver)))
    }

    /// The invoke → poll → history → fold flow of [`run`](Self::run), over
    /// any [`CloudApi`]. The driver's `GetDurableExecution` calls spend the
    /// run's poll budget, which the live controls share (see
    /// [`max_poll_attempts`](Self::max_poll_attempts)).
    async fn drive<A: CloudApi + Send + Sync + 'static, O: DeserializeOwned>(
        &self,
        cloud: &CloudShared<A>,
        api: Arc<A>,
        payload: Vec<u8>,
    ) -> Result<TestResult<O>, TestRunnerError> {
        // 1. Invoke: start the durable execution and capture its ARN. The
        // `FunctionError` and `Payload` of a synchronous invoke are ignored;
        // the history is the source of the result.
        let invoke_output = api
            .send_invoke(self.build_invoke(payload))
            .await
            .map_err(aws_error)?;
        let execution_arn = invoke_output
            .durable_execution_arn()
            .ok_or(TestRunnerError::NoExecutionArn)?
            .to_owned();
        cloud.set_target(Arc::clone(&api), execution_arn.clone());
        let api = &*api;

        // 2. Poll GetDurableExecution until terminal, or until a configured
        // budget is spent.
        let mut polls = 0_usize;
        let terminal = loop {
            if !cloud.budget_spent() {
                tokio::time::sleep(self.poll_interval).await;
                if cloud.take_poll() {
                    polls += 1;
                    let out = with_read_retries(|| api.send_get_durable_execution(&execution_arn))
                        .await
                        .map_err(aws_error)?;
                    if !matches!(out.status(), WireExecutionStatus::Running) {
                        break out;
                    }
                    continue;
                }
            }
            let (events, operations) = cloud.driver_history(api, &execution_arn).await?;
            return Ok(TestResult::with_history(
                ExecutionStatus::Running,
                None,
                Some(crate::WireError::new(
                    None::<String>,
                    Some(format!(
                        "execution {execution_arn} still running after {} polls",
                        cloud.used()
                    )),
                )),
                operations,
                polls,
                events,
            ));
        };

        // 3. Retrieve the recorded history and fold it into operations.
        let (events, operations) = cloud.driver_history(api, &execution_arn).await?;
        let invocation_events = invocations_from_history(&events);
        let invocations = if invocation_events == 0 {
            polls
        } else {
            invocation_events
        };

        // 4. Map the terminal status into the result. The history's terminal
        // execution event is the source of the output and the error. The
        // `GetDurableExecution` output is used only when the history holds
        // no terminal execution event.
        let status = ExecutionStatus::from_wire(terminal.status());
        let history_end = terminal_execution_event(&events);
        let (output, error) = if status == ExecutionStatus::Succeeded {
            let payload = match history_end.and_then(Event::execution_succeeded_details) {
                Some(details) => event_result_payload(details.result()),
                None => terminal.result().map(ToOwned::to_owned),
            };
            let output = deserialize_output::<O>(payload.as_deref())
                .map_err(|e| TestRunnerError::OutputDeserialization(Arc::new(e)))?;
            (Some(output), None)
        } else {
            let status_name = status.to_string();
            let error_object = history_end
                .and_then(terminal_event_error)
                .or_else(|| terminal.error());
            let error = error_object.map_or_else(
                || {
                    crate::WireError::new(
                        Some(status_name.as_str()),
                        Some(format!("execution ended with status {status_name}")),
                    )
                },
                |err| {
                    crate::WireError::new(
                        Some(err.error_type().unwrap_or(&status_name)),
                        Some(err.error_message().unwrap_or_default()),
                    )
                    .with_error_data(err.error_data())
                    .with_stack_trace(err.stack_trace().to_vec())
                },
            );
            (None, Some(error))
        };
        Ok(TestResult::with_history(
            status,
            output,
            error,
            operations,
            invocations,
            events,
        ))
    }

    /// The `Invoke` request for `payload`, with this runner's function,
    /// qualifier and invocation type.
    fn build_invoke(&self, payload: Vec<u8>) -> InvokeRequest {
        InvokeRequest {
            function_name: self.function_name.clone(),
            qualifier: self.qualifier.clone(),
            invocation_type: self.invocation_type.to_wire(),
            payload,
        }
    }

    /// The client `run` uses: the configured
    /// [`lambda_client`](Self::lambda_client) unchanged, or else one built
    /// from the ambient config with the [`region`](Self::region) override.
    async fn resolve_client(&self) -> crate::LambdaClient {
        if let Some(client) = &self.lambda_client {
            return client.clone();
        }
        let mut loader = aws_config::defaults(aws_config::BehaviorVersion::latest());
        if let Some(region) = &self.region {
            loader = loader.region(aws_sdk_lambda::config::Region::new(region.clone()));
        }
        let config = loader.load().await;
        crate::LambdaClient::new(&config)
    }
}

/// The attempts [`with_read_retries`] makes, as JS
/// `HistoryPoller.getMaxRetryAttempts`.
const READ_ATTEMPTS: u32 = 3;

/// The wait after `failed` failed attempts of a read, as JS
/// `callWithRetries`: `(failed² - 1) × 150 + 1000` ms, so 1000 ms after the
/// first failure and 1450 ms after the second.
fn read_retry_delay(failed: u32) -> std::time::Duration {
    std::time::Duration::from_millis(u64::from(failed * failed - 1) * 150 + 1000)
}

/// Calls `call` until it succeeds, up to [`READ_ATTEMPTS`] times, waiting
/// [`read_retry_delay`] after each failed attempt but the last. Any error
/// fails an attempt, and the last attempt's error is the result.
async fn with_read_retries<T, F, Fut>(mut call: F) -> Result<T, BoxError>
where
    F: FnMut() -> Fut,
    Fut: Future<Output = Result<T, BoxError>>,
{
    let mut failed = 0;
    loop {
        match call().await {
            Ok(value) => return Ok(value),
            Err(err) => {
                failed += 1;
                if failed >= READ_ATTEMPTS {
                    return Err(err);
                }
                tokio::time::sleep(read_retry_delay(failed)).await;
            }
        }
    }
}

/// Retrieves the full paginated execution history. Each page request makes
/// the attempts of [`with_read_retries`], and a page that follows a page
/// with a next marker is requested `poll_interval` after that page arrived.
async fn fetch_history<A: CloudApi>(
    api: &A,
    execution_arn: &str,
    poll_interval: std::time::Duration,
) -> Result<Vec<Event>, TestRunnerError> {
    let mut events = Vec::new();
    let mut marker: Option<String> = None;
    loop {
        let page = marker.as_deref();
        let out = with_read_retries(|| api.send_get_durable_execution_history(execution_arn, page))
            .await
            .map_err(aws_error)?;
        events.extend(out.events().iter().cloned());
        match out.next_marker() {
            Some(m) if !m.is_empty() => marker = Some(m.to_owned()),
            _ => break,
        }
        tokio::time::sleep(poll_interval).await;
    }
    Ok(events)
}

/// The last terminal execution event of the history: `ExecutionSucceeded`,
/// `ExecutionFailed`, `ExecutionTimedOut` or `ExecutionStopped`.
fn terminal_execution_event(events: &[Event]) -> Option<&Event> {
    events.iter().rev().find(|e| {
        matches!(
            e.event_type(),
            Some(
                EventType::ExecutionSucceeded
                    | EventType::ExecutionFailed
                    | EventType::ExecutionTimedOut
                    | EventType::ExecutionStopped
            )
        )
    })
}

/// The error object a failed, timed-out or stopped terminal execution event
/// carries.
fn terminal_event_error(event: &Event) -> Option<&ErrorObject> {
    let error = match event.event_type() {
        Some(EventType::ExecutionFailed) => event
            .execution_failed_details()
            .and_then(aws_sdk_lambda::types::ExecutionFailedDetails::error),
        Some(EventType::ExecutionTimedOut) => event
            .execution_timed_out_details()
            .and_then(aws_sdk_lambda::types::ExecutionTimedOutDetails::error),
        Some(EventType::ExecutionStopped) => event
            .execution_stopped_details()
            .and_then(aws_sdk_lambda::types::ExecutionStoppedDetails::error),
        _ => None,
    };
    error.and_then(EventError::payload)
}

/// Deserializes the handler output from the terminal result
/// string, tolerating a double-encoded (JSON-string-of-JSON) payload.
fn deserialize_output<O: DeserializeOwned>(result: Option<&str>) -> Result<O, serde_json::Error> {
    let raw = result.unwrap_or("null");
    match serde_json::from_str::<O>(raw) {
        Ok(value) => Ok(value),
        Err(first) => match serde_json::from_str::<String>(raw) {
            Ok(inner) => serde_json::from_str::<O>(&inner),
            Err(_) => Err(first),
        },
    }
}

/// Folds a chronological execution-event stream into per-operation
/// [`TestOperation`]s in first-appearance order.
///
/// Only operation-scoped events (`Step*`, `Wait*`, `Context*`, `Callback*`,
/// `ChainedInvoke*`) with an operation id are folded; execution-level and
/// `InvocationCompleted` events are ignored here (the execution outcome comes
/// from [`terminal_execution_event`], the invocation count from
/// [`invocations_from_history`]). Later transitions overwrite earlier ones, so
/// the recorded `status` is the operation's terminal-or-latest state.
fn fold_history(events: &[Event]) -> Vec<TestOperation> {
    use std::collections::HashMap;

    let mut order: Vec<String> = Vec::new();
    let mut acc: HashMap<String, TestOperation> = HashMap::new();

    for event in events {
        let Some(kind) = event_family_op_type(event.event_type()) else {
            continue;
        };
        let Some(id) = event.id() else { continue };

        // The start time is the timestamp of the operation's first event.
        // It is set once, when that event creates the record. So a first
        // event without a representable timestamp leaves it `None`, and a
        // later event does not fill it.
        let op = acc.entry(id.to_owned()).or_insert_with(|| {
            order.push(id.to_owned());
            TestOperation {
                start_time: event_time(event),
                ..TestOperation::started(id.to_owned(), kind)
            }
        });

        if op.sub_type.is_none() {
            op.sub_type = event.sub_type().map(ToOwned::to_owned);
        }
        if op.name.is_none() {
            op.name = event.name().map(ToOwned::to_owned);
        }
        if op.parent_id.is_none() {
            op.parent_id = event.parent_id().map(ToOwned::to_owned);
        }
        apply_event_transition(op, event);
        // The end time is the timestamp of the terminal event. A retried
        // step records `StepFailed`, and a `wait_for_condition` check that
        // continued records `StepSucceeded`, and then `StepStarted`, so a
        // later non-terminal event clears the end time of an earlier one.
        op.end_time = if is_terminal_status(op.status) {
            event_time(event)
        } else {
            None
        };
    }

    order.iter().filter_map(|id| acc.get(id).cloned()).collect()
}

/// Applies a single event's transition (status, result, error, attempt) to the
/// accumulating operation record.
fn apply_event_transition(op: &mut TestOperation, event: &Event) {
    match event.event_type() {
        Some(EventType::WaitStarted) => {
            op.status = OperationStatus::Started;
            let details = event.wait_started_details();
            op.wait.duration = details
                .and_then(|d| u64::try_from(d.duration()).ok())
                .map(std::time::Duration::from_secs);
            op.wait.scheduled_end_time = details
                .map(aws_sdk_lambda::types::WaitStartedDetails::scheduled_end_timestamp)
                .and_then(|t| std::time::SystemTime::try_from(*t).ok());
        }
        Some(EventType::CallbackStarted) => {
            op.status = OperationStatus::Started;
            op.callback.callback_id = event
                .callback_started_details()
                .map(|d| d.callback_id().to_owned());
        }
        Some(
            EventType::StepStarted | EventType::ContextStarted | EventType::ChainedInvokeStarted,
        ) => op.status = OperationStatus::Started,
        Some(EventType::StepSucceeded) => {
            op.status = OperationStatus::Succeeded;
            if let Some(details) = event.step_succeeded_details() {
                op.result = event_result_payload(details.result());
                set_attempt(op, details.retry_details());
                // A `wait_for_condition` check that continued records
                // `StepSucceeded` with a retry delay.
                set_next_attempt_time(op, event, details.retry_details());
            }
        }
        Some(EventType::StepFailed) => {
            op.status = OperationStatus::Failed;
            if let Some(details) = event.step_failed_details() {
                set_attempt(op, details.retry_details());
                set_next_attempt_time(op, event, details.retry_details());
            }
        }
        Some(EventType::WaitSucceeded) => op.status = OperationStatus::Succeeded,
        Some(EventType::WaitCancelled) => op.status = OperationStatus::Cancelled,
        Some(EventType::ContextSucceeded) => {
            op.status = OperationStatus::Succeeded;
            op.result = event
                .context_succeeded_details()
                .and_then(|d| event_result_payload(d.result()));
        }
        Some(EventType::CallbackSucceeded) => {
            op.status = OperationStatus::Succeeded;
            op.result = event
                .callback_succeeded_details()
                .and_then(|d| event_result_payload(d.result()));
        }
        Some(EventType::ChainedInvokeSucceeded) => {
            op.status = OperationStatus::Succeeded;
            op.result = event
                .chained_invoke_succeeded_details()
                .and_then(|d| event_result_payload(d.result()));
        }
        Some(
            EventType::ContextFailed | EventType::CallbackFailed | EventType::ChainedInvokeFailed,
        ) => op.status = OperationStatus::Failed,
        Some(EventType::CallbackTimedOut | EventType::ChainedInvokeTimedOut) => {
            op.status = OperationStatus::TimedOut;
        }
        Some(EventType::ChainedInvokeStopped) => op.status = OperationStatus::Stopped,
        _ => {}
    }
    set_error(op, operation_event_error(event));
}

/// The event's timestamp, or `None` when it has none or it lies outside the
/// range of `SystemTime`.
fn event_time(event: &Event) -> Option<std::time::SystemTime> {
    event
        .event_timestamp()
        .and_then(|t| std::time::SystemTime::try_from(*t).ok())
}

/// Sets the next attempt time of a step from the retry details of its
/// `StepFailed` or `StepSucceeded` event `event`.
///
/// The latest step event that carries a retry delay sets the next attempt
/// time; one without a delay keeps it. A carried delay whose time cannot
/// be represented sets `None`, so the value never describes an older retry.
fn set_next_attempt_time(op: &mut TestOperation, event: &Event, retry: Option<&RetryDetails>) {
    if let Some(delay) = retry.and_then(RetryDetails::next_attempt_delay_seconds) {
        op.step.next_attempt_time = next_attempt_time(event, delay);
    }
}

/// The next attempt time of a step event that carries a retry delay (a
/// `StepFailed` of a step retry, or a `StepSucceeded` of a
/// `wait_for_condition` check that continued): its timestamp plus the
/// delay `delay_seconds`. `None` when the event has no timestamp, the
/// delay is negative, or the sum overflows `SystemTime`.
fn next_attempt_time(event: &Event, delay_seconds: i32) -> Option<std::time::SystemTime> {
    let delay = std::time::Duration::from_secs(u64::try_from(delay_seconds).ok()?);
    event_time(event)?.checked_add(delay)
}

/// The `EventError` an operation event carries.
///
/// Eight operation event types carry an error: `StepFailed`, `WaitCancelled`,
/// `ContextFailed`, `CallbackFailed`, `CallbackTimedOut`,
/// `ChainedInvokeFailed`, `ChainedInvokeTimedOut` and `ChainedInvokeStopped`.
/// Every one of them is read here, in one place. So a transition arm cannot
/// set the status of an error-bearing event and drop its error record.
fn operation_event_error(event: &Event) -> Option<&EventError> {
    match event.event_type()? {
        EventType::StepFailed => event.step_failed_details()?.error(),
        EventType::WaitCancelled => event.wait_cancelled_details()?.error(),
        EventType::ContextFailed => event.context_failed_details()?.error(),
        EventType::CallbackFailed => event.callback_failed_details()?.error(),
        EventType::CallbackTimedOut => event.callback_timed_out_details()?.error(),
        EventType::ChainedInvokeFailed => event.chained_invoke_failed_details()?.error(),
        EventType::ChainedInvokeTimedOut => event.chained_invoke_timed_out_details()?.error(),
        EventType::ChainedInvokeStopped => event.chained_invoke_stopped_details()?.error(),
        _ => None,
    }
}

/// Extracts the JSON payload string from an optional `EventResult`.
fn event_result_payload(result: Option<&EventResult>) -> Option<String> {
    result.and_then(EventResult::payload).map(ToOwned::to_owned)
}

/// Records the error of the current event onto the op: its type, message,
/// error data and stack trace, or no error.
///
/// The op holds the error of its latest event. A retried step records
/// `StepFailed` with an error and then `StepStarted` or `StepSucceeded`
/// without one, and a `wait_for_condition` check that continued records
/// `StepSucceeded` without one. Keeping the earlier error would report a succeeded step with
/// the error of a failed attempt. So an event without an error clears it, as
/// `LocalRunner` clears a stored error when a retry starts.
fn set_error(op: &mut TestOperation, error: Option<&EventError>) {
    op.error = error
        .and_then(EventError::payload)
        .map(wire_error_from_object);
}

/// The wire error of a service `ErrorObject`, with every field.
fn wire_error_from_object(obj: &ErrorObject) -> crate::WireError {
    crate::WireError::new(obj.error_type(), obj.error_message())
        .with_error_data(obj.error_data())
        .with_stack_trace(obj.stack_trace().to_vec())
}

/// Records the number of completed retries from an optional `RetryDetails`
/// onto the op.
///
/// `RetryDetails.current_attempt` is 1-based (a first-try success reports `1`),
/// while [`StepDetails::attempt`] reports completed retries (a first-try
/// success reports `0`). The two are reconciled here so both runners agree.
/// `saturating_sub` guards the conversion: a non-positive `current_attempt`
/// maps to `0` without underflow.
fn set_attempt(op: &mut TestOperation, retry: Option<&RetryDetails>) {
    if let Some(details) = retry {
        let one_based = u32::try_from(details.current_attempt().max(0)).unwrap_or(0);
        op.step.attempt = one_based.saturating_sub(1);
    }
}

/// Maps an event type to the [`RunnerKind`] of its operation, or
/// `None` for execution-level / invocation-level events that are not folded
/// into operations.
fn event_family_op_type(event_type: Option<&EventType>) -> Option<RunnerKind> {
    match event_type? {
        EventType::StepStarted | EventType::StepSucceeded | EventType::StepFailed => {
            Some(RunnerKind::Step)
        }
        EventType::WaitStarted | EventType::WaitSucceeded | EventType::WaitCancelled => {
            Some(RunnerKind::Wait)
        }
        EventType::ContextStarted | EventType::ContextSucceeded | EventType::ContextFailed => {
            Some(RunnerKind::Context)
        }
        EventType::CallbackStarted
        | EventType::CallbackSucceeded
        | EventType::CallbackFailed
        | EventType::CallbackTimedOut => Some(RunnerKind::Callback),
        EventType::ChainedInvokeStarted
        | EventType::ChainedInvokeSucceeded
        | EventType::ChainedInvokeFailed
        | EventType::ChainedInvokeTimedOut
        | EventType::ChainedInvokeStopped => Some(RunnerKind::ChainedInvoke),
        _ => None,
    }
}

/// Counts `InvocationCompleted` events: the number of times the service
/// invoked the function to drive the execution.
fn invocations_from_history(events: &[Event]) -> usize {
    events
        .iter()
        .filter(|e| matches!(e.event_type(), Some(EventType::InvocationCompleted)))
        .count()
}

/// A stored operation record inside the in-memory backend.
#[derive(Debug, Clone)]
struct StoredOp {
    id: String,
    kind: RunnerKind,
    sub_type: Option<String>,
    status: OperationStatus,
    name: Option<String>,
    result: Option<String>,
    error_type: Option<String>,
    error_message: Option<String>,
    /// Opaque error payload recorded with a failure, round-tripped
    /// verbatim like the production backend does.
    error_data: Option<String>,
    /// Recorded stack trace frames, round-tripped verbatim.
    stack_trace: Vec<String>,
    attempt: i32,
    callback_id: Option<String>,
    /// The `HeartbeatTimeoutSeconds` of a callback's `START`.
    heartbeat_seconds: Option<i32>,
    wait_seconds: Option<i32>,
    /// The virtual time, in seconds, at which a started wait ends or a
    /// pending retry may run again. Set by a WAIT `START` and a STEP
    /// `RETRY`. Firing the operation keeps the value.
    due_at: Option<u64>,
    /// The virtual time, in seconds, at which the record was created.
    start_at: u64,
    /// The virtual time, in seconds, at which the status first became
    /// terminal (see [`StoredOp::set_status`]), or `None` while it never
    /// has.
    end_at: Option<u64>,
    callback_delivered: bool,
    /// The `ParentId` of the update that created the record. Later updates
    /// do not change it.
    parent_id: Option<String>,
    /// The `ReplayChildren` option of the update that closed a context.
    /// A close without the option records `false`.
    replay_children: bool,
    /// The function name of a chained invoke, from its `START`.
    invoke_function: Option<String>,
    /// The input payload of a chained invoke, from its `START`.
    invoke_input: Option<String>,
    /// Whether the runner already ran the invoke's target. A target runs
    /// once per invoke, whatever its outcome.
    invoke_dispatched: bool,
}

/// A fresh `Started` record for an operation seen for the first time; the
/// caller merges name/sub-type metadata and the action's outcome fields.
fn new_started_op(
    id: String,
    kind: RunnerKind,
    sub_type: Option<String>,
    parent_id: Option<String>,
    now: u64,
) -> StoredOp {
    StoredOp {
        id,
        kind,
        sub_type,
        status: OperationStatus::Started,
        name: None,
        result: None,
        error_type: None,
        error_message: None,
        error_data: None,
        stack_trace: Vec::new(),
        attempt: 0,
        callback_id: None,
        heartbeat_seconds: None,
        wait_seconds: None,
        due_at: None,
        start_at: now,
        end_at: None,
        callback_delivered: false,
        parent_id,
        replay_children: false,
        invoke_function: None,
        invoke_input: None,
        invoke_dispatched: false,
    }
}

impl StoredOp {
    /// Sets the status. Every status write of the backend goes through
    /// this method.
    ///
    /// `end_at` is the time the operation ended. A terminal status ends
    /// the operation, so the first terminal status records `now` as
    /// `end_at`. A later write keeps that first value. Because every path
    /// calls this method, a new status path cannot forget `end_at`.
    fn set_status(&mut self, status: OperationStatus, now: u64) {
        self.status = status;
        if self.end_at.is_none() && is_terminal_status(status) {
            self.end_at = Some(now);
        }
    }
}

/// Whether `status` ends an operation: `Succeeded`, `Failed`, `TimedOut`,
/// `Cancelled` or `Stopped`.
fn is_terminal_status(status: OperationStatus) -> bool {
    matches!(
        status,
        OperationStatus::Succeeded
            | OperationStatus::Failed
            | OperationStatus::TimedOut
            | OperationStatus::Cancelled
            | OperationStatus::Stopped
    )
}

/// Decrements `counter` by one if it is above zero, and returns whether it
/// did.
///
/// `AtomicUsize::fetch_update` does this in one call. Rust 1.99 deprecates
/// it, and CI denies warnings. Its replacement, `try_update`, is unstable
/// on the 1.94.1 MSRV. So this function uses a `compare_exchange` loop,
/// which compiles without warnings on both toolchains.
fn atomic_decrement_if_positive(counter: &std::sync::atomic::AtomicUsize) -> bool {
    let mut current = counter.load(std::sync::atomic::Ordering::SeqCst);
    while current > 0 {
        match counter.compare_exchange(
            current,
            current - 1,
            std::sync::atomic::Ordering::SeqCst,
            std::sync::atomic::Ordering::SeqCst,
        ) {
            Ok(_) => return true,
            Err(actual) => current = actual,
        }
    }
    false
}

/// Mutable state of the in-memory backend, guarded by a single mutex.
#[derive(Debug)]
struct BackendState {
    /// Operations in first-checkpoint order.
    ops: Vec<StoredOp>,
    token_counter: u64,
    callback_counter: u64,
    /// Queued external callback replies (see [`Backend::advance`]).
    callback_replies: QueuedReplies,
    /// The virtual time in seconds. It starts at 0 and moves only in
    /// [`Backend::advance`], between invocations.
    now: u64,
    /// The wall-clock time the run started. A reported time is `base`
    /// plus a virtual time (see [`BackendState::local_time`]).
    base: std::time::SystemTime,
    /// The IDs of the records [`Backend::advance`] changed since the last
    /// envelope, in change order. The next envelope sends them as
    /// `UpdatedOperationIds` and clears them (see
    /// [`Backend::take_updated_ids`]). A checkpoint call never adds to it.
    updated_ids: Vec<String>,
    /// The history events, in the order of their causes (see
    /// [`BackendState::push_event`]).
    events: Vec<Event>,
    /// The event ID of the last event in `events`, or 0 before the first.
    last_event_id: i32,
    /// The payload of the accepted `EXECUTION` update: a result the SDK
    /// checkpointed because it is over the response limit. No operation
    /// stores it.
    execution_result: Option<String>,
}

impl BackendState {
    /// The records of a new run, with the queued callback replies
    /// `callback_replies`.
    fn new(callback_replies: QueuedReplies) -> Self {
        Self {
            ops: Vec::new(),
            token_counter: 0,
            callback_counter: 0,
            callback_replies,
            now: 0,
            base: std::time::SystemTime::now(),
            updated_ids: Vec::new(),
            events: Vec::new(),
            last_event_id: 0,
            execution_result: None,
        }
    }
}

/// Where a [`Backend`] keeps its records.
#[derive(Debug)]
enum StateCell {
    /// In its own mutex: a nested durable target and a unit test's
    /// injected backend, which no run control observes.
    Own(Mutex<BackendState>),
    /// In the run's state (see [`RunState::local`]), so every decision of
    /// the run's controls, its driver and its backend is one transition on
    /// one mutex.
    Live(Arc<LiveState>),
}

impl StateCell {
    /// Records in their own mutex.
    fn own(records: BackendState) -> Self {
        Self::Own(Mutex::new(records))
    }
}

/// The locked records of a [`Backend`].
enum StateGuard<'a> {
    Own(std::sync::MutexGuard<'a, BackendState>),
    Live(std::sync::MutexGuard<'a, RunState>),
}

impl StateGuard<'_> {
    /// [`RunState::recheck`] for the records of a top-level run. A backend
    /// with its own records has no controls.
    fn recheck(&mut self) -> Vec<std::task::Waker> {
        match self {
            Self::Own(_) => Vec::new(),
            Self::Live(state) => state.recheck(),
        }
    }
}

impl std::ops::Deref for StateGuard<'_> {
    type Target = BackendState;

    fn deref(&self) -> &BackendState {
        match self {
            Self::Own(guard) => guard,
            Self::Live(guard) => &guard.local.records,
        }
    }
}

impl std::ops::DerefMut for StateGuard<'_> {
    fn deref_mut(&mut self) -> &mut BackendState {
        match self {
            Self::Own(guard) => guard,
            Self::Live(guard) => &mut guard.local.records,
        }
    }
}

/// The in-memory execution client. Shared (via `Arc`) between the runner and
/// the durable context. Debug-printable and `Send + Sync` as the
/// `ExecutionClient` trait requires.
#[derive(Debug)]
struct Backend {
    state: StateCell,
    /// When set, the checkpoint response will include `next_marker` when
    /// the number of updated operations exceeds this value, simulating a
    /// paginated checkpoint response.
    checkpoint_page_size: Option<usize>,
    /// Counts `get_state` calls: lets tests assert that pagination
    /// actually forced a state fetch (or, under `single_page`, that none
    /// occurred).
    get_state_calls: std::sync::atomic::AtomicUsize,
    /// Counts `checkpoint` calls: lets tests assert that checkpoint
    /// coalescing (`checkpoint_delay`) actually reduced the number of
    /// writes.
    checkpoint_calls: std::sync::atomic::AtomicUsize,
    /// Number of upcoming `checkpoint` calls to reject, before touching
    /// any state (fault injection; see
    /// [`LocalRunner::fail_next_checkpoints`] and
    /// [`LocalRunner::fail_checkpoints_after`]).
    checkpoint_failures_remaining: std::sync::atomic::AtomicUsize,
    /// Number of `checkpoint` calls to let through before the injected
    /// failures begin.
    checkpoint_failures_skip: std::sync::atomic::AtomicUsize,
    /// Whether injected checkpoint failures are retryable (see
    /// [`LocalRunner::fail_checkpoints_after_retryable`]).
    checkpoint_failures_retryable: std::sync::atomic::AtomicBool,
    /// How long each `checkpoint` call waits, in tokio time, before it
    /// applies and answers the request (see
    /// [`LocalRunner::checkpoint_latency`]).
    checkpoint_latency: std::time::Duration,
    /// The names registered on the runner as invoke targets. A chained
    /// invoke `START` for any other name is rejected.
    registered_functions: BTreeSet<String>,
    /// The 1-based number of the checkpoint request answered without a
    /// token (see [`LocalRunner::withdraw_checkpoints_after`]).
    withdraw_at: Option<usize>,
    /// Set when a request was answered without a token, and taken by the
    /// runner after the invocation answered `PENDING`.
    withdrew: std::sync::atomic::AtomicBool,
    /// Per-call checkpoint plan (in-crate test seam): while non-empty,
    /// each `checkpoint` call pops and obeys the front entry instead of
    /// the counter-based injection (see
    /// [`Backend::plan_checkpoint_calls`]).
    #[cfg(test)]
    checkpoint_plan: Mutex<std::collections::VecDeque<PlannedCheckpoint>>,
}

/// One planned behavior for an upcoming [`Backend`] `checkpoint` call
/// (in-crate test seam; see [`Backend::plan_checkpoint_calls`]).
#[cfg(test)]
#[derive(Debug)]
enum PlannedCheckpoint {
    /// The call proceeds normally against the simulated store.
    Pass,
    /// The call is rejected with a non-retryable error, persisting
    /// nothing. When `gate` is set, the rejection is held in flight until
    /// the test adds a permit to the semaphore, so the test can order
    /// events (e.g. dropping a contributor) against the in-flight write
    /// deterministically.
    FailNonRetryable {
        gate: Option<Arc<tokio::sync::Semaphore>>,
    },
    /// The call applies the updates as [`Self::Pass`] does, then holds its
    /// answer until the test adds a permit to `gate`. So the SDK merges
    /// the response only after the release.
    PassGated { gate: Arc<tokio::sync::Semaphore> },
    /// The call applies the updates as [`Self::Pass`] does, then answers
    /// with a retryable error carrying the message, as when the service
    /// recorded a write whose response was lost.
    PassThenRetryableError(String),
}

impl Backend {
    /// A backend with its own records, as a unit test injects it.
    #[cfg(test)]
    fn new(callback_replies: QueuedReplies, checkpoint_page_size: Option<usize>) -> Self {
        Self::on(
            StateCell::own(BackendState::new(callback_replies)),
            checkpoint_page_size,
        )
    }

    /// A backend whose records are in `state`.
    fn on(state: StateCell, checkpoint_page_size: Option<usize>) -> Self {
        Self {
            state,
            checkpoint_page_size,
            get_state_calls: std::sync::atomic::AtomicUsize::new(0),
            checkpoint_calls: std::sync::atomic::AtomicUsize::new(0),
            checkpoint_failures_remaining: std::sync::atomic::AtomicUsize::new(0),
            checkpoint_failures_skip: std::sync::atomic::AtomicUsize::new(0),
            checkpoint_failures_retryable: std::sync::atomic::AtomicBool::new(false),
            checkpoint_latency: std::time::Duration::ZERO,
            registered_functions: BTreeSet::new(),
            withdraw_at: None,
            withdrew: std::sync::atomic::AtomicBool::new(false),
            #[cfg(test)]
            checkpoint_plan: Mutex::new(std::collections::VecDeque::new()),
        }
    }

    /// Queues a per-call plan for upcoming `checkpoint` calls (in-crate
    /// test seam). While entries remain, each call pops and obeys the
    /// front entry, pass, fail retryably, or fail non-retryably (with an
    /// optional in-flight gate), instead of the counter-based injection.
    /// Once the plan is exhausted, calls fall back to the counters.
    #[cfg(test)]
    fn plan_checkpoint_calls(&self, plan: Vec<PlannedCheckpoint>) {
        self.checkpoint_plan
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .extend(plan);
    }

    /// Queues `response` as the answer to an upcoming checkpoint call, after
    /// the calls queued before it. Only
    /// [`TestResponse::AcceptThenRetryableError`] has a meaning for the
    /// backend: it applies the call's updates, then answers with a
    /// retryable error.
    #[cfg(test)]
    fn enqueue_response(&self, response: crate::client::TestResponse) {
        let crate::client::TestResponse::AcceptThenRetryableError(message) = response else {
            unreachable!("the LocalRunner backend answers only AcceptThenRetryableError");
        };
        self.plan_checkpoint_calls(vec![PlannedCheckpoint::PassThenRetryableError(message)]);
    }

    /// The payload of the `EXECUTION` update the backend accepted, if any.
    fn execution_result(&self) -> Option<String> {
        self.lock().execution_result.clone()
    }

    /// Pops the next planned checkpoint behavior, if a plan is queued.
    #[cfg(test)]
    fn next_planned_checkpoint(&self) -> Option<PlannedCheckpoint> {
        self.checkpoint_plan
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .pop_front()
    }

    /// The number of `get_state` calls the SDK has made against this
    /// backend.
    #[cfg(test)]
    fn get_state_call_count(&self) -> usize {
        self.get_state_calls
            .load(std::sync::atomic::Ordering::SeqCst)
    }

    /// The number of `checkpoint` calls the SDK has made against this
    /// backend.
    #[cfg(test)]
    fn checkpoint_call_count(&self) -> usize {
        self.checkpoint_calls
            .load(std::sync::atomic::Ordering::SeqCst)
    }

    fn lock(&self) -> StateGuard<'_> {
        match &self.state {
            StateCell::Own(state) => StateGuard::Own(
                state
                    .lock()
                    .unwrap_or_else(std::sync::PoisonError::into_inner),
            ),
            StateCell::Live(live) => StateGuard::Live(live.lock()),
        }
    }

    /// The run's state cell of a top-level run (see [`RunHandle`]). A
    /// nested durable target and a unit test's injected backend have none,
    /// so the hold rule does not apply to them.
    fn live(&self) -> Option<&Arc<LiveState>> {
        match &self.state {
            StateCell::Own(_) => None,
            StateCell::Live(live) => Some(live),
        }
    }

    /// The current checkpoint token string.
    fn current_token(&self) -> String {
        let state = self.lock();
        format!("local-token-{}", state.token_counter)
    }

    /// Returns the IDs [`Backend::advance`] changed since the last call, in
    /// change order, and leaves the list empty.
    fn take_updated_ids(&self) -> Vec<String> {
        std::mem::take(&mut self.lock().updated_ids)
    }

    /// Renders the recorded history as envelope-wire JSON operation
    /// objects: the exact shape `parse_inline_operations` reads from
    /// `InitialExecutionState.Operations`.
    ///
    /// The history omits the records the service omits from the invocation
    /// input (see [`prune_replay_children`]).
    fn envelope_history(&self) -> Vec<serde_json::Value> {
        let state = self.lock();
        prune_replay_children(&state.ops)
            .into_iter()
            .map(|op| stored_op_envelope_json(op, state.base))
            .collect()
    }

    /// Builds the `Operation` records `GetDurableExecutionState` returns:
    /// the current state without the records the service omits (see
    /// [`prune_replay_children`]).
    fn build_pruned_operations(&self) -> Vec<Operation> {
        let state = self.lock();
        prune_replay_children(&state.ops)
            .into_iter()
            .filter_map(|op| build_operation(op, state.base))
            .collect()
    }

    /// The virtual time.
    fn now(&self) -> u64 {
        self.lock().now
    }

    /// The wall-clock time the run started.
    fn base(&self) -> std::time::SystemTime {
        self.lock().base
    }

    /// The recorded history events.
    fn events(&self) -> Vec<Event> {
        self.lock().events.clone()
    }

    /// Records `ExecutionStarted`, with `input` as its input payload.
    fn record_execution_started(&self, input: &str) {
        // `ExecutionStartedDetails` requires `ExecutionTimeout`. The local
        // backend has no execution timeout, so the event records 0.
        #[expect(clippy::expect_used)] // reason: the one required field is set
        let details = aws_sdk_lambda::types::ExecutionStartedDetails::builder()
            .input(EventInput::builder().payload(input).build())
            .execution_timeout(0)
            .build()
            .expect("the execution timeout is set");
        self.lock().push_event(
            execution_event(EventType::ExecutionStarted).execution_started_details(details),
        );
    }

    /// Records `InvocationCompleted` for an invocation that started at the
    /// virtual time `started_at` and ends now, with `error` as its error.
    fn record_invocation_completed(
        state: &mut BackendState,
        started_at: u64,
        request_id: &str,
        error: Option<&crate::WireError>,
    ) {
        #[expect(clippy::expect_used)] // reason: the three required fields are set
        let details = InvocationCompletedDetails::builder()
            .start_timestamp(state.event_time(started_at))
            .end_timestamp(state.event_time(state.now))
            .request_id(request_id)
            .set_error(error.map(event_error))
            .build()
            .expect("the timestamps and the request ID are set");
        state.push_event(
            Event::builder()
                .event_type(EventType::InvocationCompleted)
                .invocation_completed_details(details),
        );
    }

    /// Records `ExecutionSucceeded` or `ExecutionFailed` for a run that
    /// ended with `end`. A suspended run records nothing.
    fn record_execution_end(&self, end: &RawEnd) {
        let event = match end {
            RawEnd::Succeeded(result) => execution_event(EventType::ExecutionSucceeded)
                .execution_succeeded_details(
                    aws_sdk_lambda::types::ExecutionSucceededDetails::builder()
                        .set_result(event_result(result.as_deref()))
                        .build(),
                ),
            RawEnd::Failed(wire) => execution_event(EventType::ExecutionFailed)
                .execution_failed_details(
                    aws_sdk_lambda::types::ExecutionFailedDetails::builder()
                        .error(event_error(wire))
                        .build(),
                ),
            RawEnd::Suspended { .. } => return,
        };
        self.lock().push_event(event);
    }

    /// Snapshots the recorded operations as public [`TestOperation`]s.
    fn snapshot_operations(&self) -> Vec<TestOperation> {
        snapshot_of(&self.lock())
    }

    /// Advances the simulated backend between invocations. Returns `true`
    /// if any operation was advanced.
    ///
    /// The steps run in this order:
    ///
    /// 1. Queued callback replies go to pending callbacks (see
    ///    [`take_callback_deliveries`]). If at least one was delivered, the
    ///    advance ends here: the clock does not move and no timer fires. A
    ///    callback reply the service already holds arrives before any later
    ///    timer.
    /// 2. Otherwise the clock moves to the earliest end among timed
    ///    operations (see [`timed_due_at`]). With no timed operation, the
    ///    advance returns `false`.
    /// 3. Every timed operation due by the new time fires: a wait becomes
    ///    `Succeeded` and a pending retry becomes `Ready`, so the next
    ///    invocation re-executes the body.
    ///
    /// The ID of each record whose status changes is appended to
    /// `BackendState::updated_ids`. An advance changes each record at most
    /// once, so the list has no duplicates.
    ///
    /// Backend-behaviour anchors (this crate's own replay logic):
    /// - `wait`: `src/wait.rs` replay returns at once for `Succeeded` and
    ///   suspends for `Started`.
    /// - `step`/`wait_for_condition` retry: `src/step.rs` and
    ///   `src/wait_for_condition.rs` replay suspend for `Pending` and fall
    ///   through to live execution for `Ready`.
    /// - callback: a `Started` callback resolves to `Succeeded` (with a
    ///   payload), `Failed` or `TimedOut` (`src/callback.rs` replay).
    fn advance(&self) -> bool {
        advance_records(&mut self.lock())
    }

    /// The names of the queued named replies that no callback took, in
    /// queue order, each name once.
    fn unmatched_reply_names(&self) -> Vec<String> {
        let state = self.lock();
        let mut names: Vec<String> = Vec::new();
        for (name, _) in &state.callback_replies.named {
            if !names.contains(name) {
                names.push(name.clone());
            }
        }
        names
    }

    /// Marks every `Started` chained invoke whose target is registered and
    /// has not run as run, and returns its ID, target and input (the empty
    /// string when absent), in stored order.
    fn take_pending_invokes(
        &self,
        functions: &BTreeMap<String, RegisteredFunction>,
    ) -> Vec<(String, RegisteredFunction, String)> {
        let mut state = self.lock();
        state
            .ops
            .iter_mut()
            .filter(|op| {
                op.kind == RunnerKind::ChainedInvoke
                    && op.status == OperationStatus::Started
                    && !op.invoke_dispatched
            })
            .filter_map(|op| {
                let target = functions.get(op.invoke_function.as_deref()?)?.clone();
                op.invoke_dispatched = true;
                Some((
                    op.id.clone(),
                    target,
                    op.invoke_input.clone().unwrap_or_default(),
                ))
            })
            .collect()
    }

    /// Records `outcome` on the chained invoke `id`, and adds `id` to the
    /// updated IDs. Returns whether the record changed: a suspended nested
    /// run leaves it `Started`.
    fn record_invoke_outcome(&self, id: &str, outcome: NestedOutcome) -> bool {
        let mut state = self.lock();
        let BackendState {
            ops,
            updated_ids,
            now,
            ..
        } = &mut *state;
        // The runner took `id` from the stored records, and the backend
        // never removes a record. No checkpoint reaches this backend while
        // a target runs, because the caller's handler is not running. So
        // the record is still stored.
        #[expect(clippy::expect_used)]
        let op = ops
            .iter_mut()
            .find(|op| op.id == id)
            .expect("a dispatched invoke record stays stored");
        match outcome {
            NestedOutcome::Succeeded(result) => {
                op.set_status(OperationStatus::Succeeded, *now);
                op.result = Some(result.unwrap_or_else(|| "null".to_owned()));
            }
            NestedOutcome::Failed(wire) => {
                op.set_status(OperationStatus::Failed, *now);
                op.error_type = wire.error_type().map(ToOwned::to_owned);
                op.error_message = wire.error_message().map(ToOwned::to_owned);
                op.error_data = wire.error_data().map(ToOwned::to_owned);
                op.stack_trace = wire.stack_trace().to_vec();
            }
            NestedOutcome::Suspended => return false,
        }
        updated_ids.push(op.id.clone());
        // A recorded outcome is `Succeeded` or `Failed`, so the invoke has
        // a settled event.
        if let Some(event) = settled_event(op) {
            state.push_event(event);
        }
        // The waiting `wait_for_data` calls see the outcome in the same
        // transition.
        let wakers = state.recheck();
        drop(state);
        wake(wakers);
        true
    }

    /// The function names of the `Started` chained invokes, in stored
    /// order, each name once.
    fn pending_invoke_names(&self) -> Vec<String> {
        let state = self.lock();
        let mut names: Vec<String> = Vec::new();
        for op in &state.ops {
            if op.kind == RunnerKind::ChainedInvoke
                && op.status == OperationStatus::Started
                && let Some(name) = &op.invoke_function
                && !names.contains(name)
            {
                names.push(name.clone());
            }
        }
        names
    }
}

/// Snapshots the recorded operations as public [`TestOperation`]s.
fn snapshot_of(state: &BackendState) -> Vec<TestOperation> {
    state
        .ops
        .iter()
        .map(|op| {
            let mut out = TestOperation::started(op.id.clone(), op.kind);
            out.sub_type.clone_from(&op.sub_type);
            out.status = op.status;
            out.result.clone_from(&op.result);
            out.error = stored_wire_error(op);
            out.name.clone_from(&op.name);
            out.parent_id.clone_from(&op.parent_id);
            out.start_time = state.local_time(op.start_at);
            out.end_time = op.end_at.and_then(|t| state.local_time(t));
            // `due_at` is the wait's scheduled end and the step's next
            // attempt time. Only a step `RETRY` sets it on a step.
            let due = op.due_at.and_then(|t| state.local_time(t));
            out.step.attempt = u32::try_from(op.attempt.max(0)).unwrap_or(0);
            out.step.next_attempt_time = due;
            out.wait.scheduled_end_time = due;
            out.wait.duration = op
                .wait_seconds
                .and_then(|seconds| u64::try_from(seconds).ok())
                .map(std::time::Duration::from_secs);
            out.callback.callback_id.clone_from(&op.callback_id);
            out.context.replay_children = op.replay_children;
            out
        })
        .collect()
}

/// [`Backend::advance`] on the records `state`.
fn advance_records(state: &mut BackendState) -> bool {
    // Split the borrow so the reply queues and the records can be
    // changed in the same pass.
    let BackendState {
        ops,
        callback_replies,
        now,
        updated_ids,
        ..
    } = &mut *state;

    // A named reply can match through the parent record, so the
    // matching pass reads `ops` while the delivery pass writes it.
    // The events of the settled records, appended after the pass.
    let mut events = Vec::new();
    let deliveries = take_callback_deliveries(ops, callback_replies);
    if deliveries.iter().any(Option::is_some) {
        // `deliveries` holds one entry per record, in record order, so
        // zipping pairs each reply with the record it matched.
        for (op, reply) in ops.iter_mut().zip(deliveries) {
            if let Some(reply) = reply {
                reply.deliver_to(op, *now);
                updated_ids.push(op.id.clone());
                events.extend(settled_event(op));
            }
        }
    } else {
        let Some(next) = ops.iter().filter_map(timed_due_at).min() else {
            return false;
        };
        *now = next;
        for op in ops.iter_mut() {
            if timed_due_at(op).is_some_and(|due| due <= next) {
                let fired = if op.kind == RunnerKind::Wait {
                    OperationStatus::Succeeded
                } else {
                    OperationStatus::Ready
                };
                op.set_status(fired, next);
                updated_ids.push(op.id.clone());
                events.extend(settled_event(op));
            }
        }
    }
    for event in events {
        state.push_event(event);
    }
    true
}

/// The progress decision between invocations when nothing holds the run:
/// start the next invocation for `start`, ask for the pending invokes once,
/// then advance the backend with `advance`, else the run is stuck.
fn progress_decision(
    start: bool,
    invokes_tried: bool,
    advance: impl FnOnce() -> bool,
) -> BetweenStep {
    if start {
        return BetweenStep::Start;
    }
    if !invokes_tried {
        return BetweenStep::RunInvokes;
    }
    // Advance the simulated backend (timers, retries, callbacks). If
    // nothing can advance, the execution is genuinely stuck.
    if advance() {
        BetweenStep::Start
    } else {
        BetweenStep::Stuck
    }
}

/// The decision of one [`Backend::between_step`] of a top-level run, as one
/// transition on the run state `state`, so no control starts or stops
/// waiting, no reply is published and no pause or resume happens while it
/// is made.
///
/// While no control lets the run move, it decides to wait. It takes a
/// published live reply into `start`. While paused it advances the
/// records, or decides to wait for `resume` when nothing can advance.
/// Otherwise it makes the [`progress_decision`], and closes the run when it
/// is stuck. The wakers of the `wait_for_data` calls an advance made ready
/// are returned, to wake after the lock is released.
fn between_decision(
    state: &mut RunState,
    start: &mut bool,
    invokes_tried: bool,
) -> (Decided, Vec<std::task::Waker>) {
    if !state.hold_open() {
        return (Decided::Wait, Vec::new());
    }
    *start |= std::mem::take(&mut state.local.invocation_due);
    if state.local.paused {
        if advance_records(&mut state.local.records) {
            *start = true;
            return (Decided::Again, state.recheck());
        }
        return (Decided::Wait, Vec::new());
    }
    let mut advanced = false;
    let step = progress_decision(*start, invokes_tried, || {
        advanced = advance_records(&mut state.local.records);
        advanced
    });
    if step == BetweenStep::Stuck {
        state.local.phase = LocalPhase::Closed;
    }
    let wakers = if advanced {
        state.recheck()
    } else {
        Vec::new()
    };
    (Decided::Step(step), wakers)
}

/// Delivers `reply` to the pending callback that `selector` targets (see
/// [`callback_target`]) while the run is in progress, as a queued reply is
/// delivered: the record, the next envelope's `UpdatedOperationIds` and the
/// history event. Returns the wakers of the `wait_for_data` calls the reply
/// made ready, to wake after the lock is released.
///
/// It is one transition on the run state `state`: the target is chosen, the
/// outcome committed (see [`RunState::commit`]) and the reply published.
/// So the driver's next decision (see [`between_decision`]) either sees the
/// reply and starts the next invocation without moving the clock, or closed
/// the run before it, and the reply is refused with
/// [`TestRunnerError::CallbackNotPending`].
fn reply_live(
    state: &mut RunState,
    selector: &Selector,
    reply: CallbackReply,
) -> Result<Vec<std::task::Waker>, TestRunnerError> {
    let closed = state.local.phase == LocalPhase::Closed;
    let target =
        callback_target(selector, &snapshot_of(&state.local.records)).map(|op| op.id.clone());
    let records = &mut state.local.records;
    let now = records.now;
    let candidate = match target {
        Ok(id) => records
            .ops
            .iter()
            .position(|op| op.id == id && !closed && is_pending_callback(op))
            .ok_or(TestRunnerError::CallbackNotPending),
        Err(err) => Err(err),
    };
    let index = state.commit(candidate)?;
    let records = &mut state.local.records;
    // The commit above found the record at `index` in this transition, and
    // a record is never removed.
    #[expect(clippy::expect_used)]
    let op = records
        .ops
        .get_mut(index)
        .expect("the committed callback record is stored");
    reply.deliver_to(op, now);
    let event = settled_event(op);
    let id = op.id.clone();
    records.updated_ids.push(id);
    if let Some(event) = event {
        records.push_event(event);
    }
    state.local.invocation_due = true;
    Ok(state.recheck())
}

/// Checks a heartbeat for the callback that `selector` targets, as one
/// transition on the run state `state`: it must be pending, as for
/// [`reply_live`], and its `START` must have set a positive heartbeat
/// timeout. The outcome is committed (see [`RunState::commit`]).
fn check_heartbeat(state: &mut RunState, selector: &Selector) -> Result<(), TestRunnerError> {
    let closed = state.local.phase == LocalPhase::Closed;
    let records = &state.local.records;
    let candidate = callback_target(selector, &snapshot_of(records)).and_then(|target| {
        let op = records
            .ops
            .iter()
            .find(|op| op.id == target.id)
            .filter(|op| !closed && is_pending_callback(op))
            .ok_or(TestRunnerError::CallbackNotPending)?;
        if op.heartbeat_seconds.unwrap_or(0) > 0 {
            Ok(())
        } else {
            Err(TestRunnerError::NoHeartbeatTimeout)
        }
    });
    state.commit(candidate)
}

/// The message of a suspended result. It names every queued named reply
/// that no pending callback matched, so a test sees a misspelled or wrong
/// name. Then it names the function of every chained invoke still
/// pending.
fn suspended_message(unmatched_names: &[String], pending_invokes: &[String]) -> String {
    let quote = |names: &[String]| {
        names
            .iter()
            .map(|name| format!("\"{name}\""))
            .collect::<Vec<_>>()
            .join(", ")
    };
    let mut message = "execution suspended with no pending timer, retry, or callback \
                       to advance (missing callback outcome?)"
        .to_owned();
    if !unmatched_names.is_empty() {
        message.push_str("; no pending callback matched the queued replies for: ");
        message.push_str(&quote(unmatched_names));
    }
    if !pending_invokes.is_empty() {
        message.push_str("; chained invokes still pending for: ");
        message.push_str(&quote(pending_invokes));
    }
    message
}

/// Whether `op` is a pending callback: a callback record in `Pending` or
/// `Started` that has not received a reply.
fn is_pending_callback(op: &StoredOp) -> bool {
    op.kind == RunnerKind::Callback
        && !op.callback_delivered
        && matches!(
            op.status,
            OperationStatus::Pending | OperationStatus::Started
        )
}

/// Whether a named reply for `name` matches the callback `op`.
///
/// It matches when the callback has the name, or when the callback's parent
/// is a stored `wait_for_callback` context with the name. A plain child
/// context's name does not match: the JS test runner reads the callback of
/// a context only for the `WAIT_FOR_CALLBACK` sub-type.
fn reply_name_matches(ops: &[StoredOp], op: &StoredOp, name: &str) -> bool {
    if op.name.as_deref() == Some(name) {
        return true;
    }
    let Some(parent_id) = op.parent_id.as_deref() else {
        return false;
    };
    ops.iter().any(|parent| {
        parent.id == parent_id
            && parent.kind == RunnerKind::Context
            && parent.sub_type.as_deref() == Some(crate::callback::WFCB_SUB_TYPE)
            && parent.name.as_deref() == Some(name)
    })
}

/// Removes from `replies` the reply each pending callback receives, and
/// returns one entry per record of `ops`, in the same order: the reply the
/// record receives, or `None`.
///
/// The pending callbacks are visited in stored order. Each one takes the
/// first queued named reply that matches it (see [`reply_name_matches`]);
/// else the front FIFO reply; else nothing.
fn take_callback_deliveries(
    ops: &[StoredOp],
    replies: &mut QueuedReplies,
) -> Vec<Option<CallbackReply>> {
    ops.iter()
        .map(|op| {
            if !is_pending_callback(op) {
                return None;
            }
            let named = replies
                .named
                .iter()
                .position(|(name, _)| reply_name_matches(ops, op, name));
            match named {
                Some(position) => Some(replies.named.remove(position).1),
                None => replies.fifo.pop_front(),
            }
        })
        .collect()
}

/// The virtual time at which `op` ends, if it is timed: a wait in `Started`
/// or a step (or `wait_for_condition` check) in `Pending` with a due time.
/// Firing changes the status, which removes the operation from the timed
/// set.
fn timed_due_at(op: &StoredOp) -> Option<u64> {
    match (op.kind, op.status) {
        (RunnerKind::Wait, OperationStatus::Started)
        | (RunnerKind::Step, OperationStatus::Pending) => op.due_at,
        _ => None,
    }
}

/// Converts a wire duration in seconds to the clock's unit. A missing or
/// negative value counts as 0, as in the JS and Python checkpoint servers.
fn whole_seconds(value: Option<i32>) -> u64 {
    u64::from(value.unwrap_or(0).max(0).unsigned_abs())
}

impl BackendState {
    /// The reported time of the virtual time `t`: the run's start time plus
    /// `t` seconds. A sum past the range of `SystemTime` reports `None`.
    fn local_time(&self, t: u64) -> Option<std::time::SystemTime> {
        self.base.checked_add(std::time::Duration::from_secs(t))
    }

    /// The event timestamp of the virtual time `t`: the run's start time
    /// plus `t` seconds, as [`local_time`](Self::local_time) reports it.
    ///
    /// `DateTime` counts seconds in an `i64`, so the sum cannot leave its
    /// range: `t` is a sum of `i32` wait and retry durations.
    fn event_time(&self, t: u64) -> DateTime {
        virtual_date_time(self.base, t)
    }

    /// Appends the event `builder` describes, with the next event ID
    /// (counting from 1) and the current virtual time as its timestamp.
    fn push_event(&mut self, builder: EventBuilder) {
        self.last_event_id = self.last_event_id.saturating_add(1);
        let event = builder
            .event_id(self.last_event_id)
            .event_timestamp(self.event_time(self.now))
            .build();
        self.events.push(event);
    }

    /// The history event of an accepted `update` of the stored op `op`, as
    /// the table of [`TestResult::history_events`] states, or `None` when
    /// the update has no event of its own (an `EXECUTION` update, or an
    /// action the table does not list).
    #[expect(clippy::too_many_lines)] // reason: one arm per row of the table
    fn update_event(&self, op: &StoredOp, update: &OperationUpdate) -> Option<EventBuilder> {
        let error = || stored_wire_error(op).as_ref().map(event_error);
        let result = || event_result(op.result.as_deref());
        let event = match (op.kind, update.action()) {
            (RunnerKind::Step, OperationAction::Start) => op_event(op, EventType::StepStarted)
                .step_started_details(aws_sdk_lambda::types::StepStartedDetails::builder().build()),
            // `attempt` counts completed retries, and `CurrentAttempt` is
            // 1-based.
            (RunnerKind::Step, OperationAction::Succeed) => op_event(op, EventType::StepSucceeded)
                .step_succeeded_details(
                    aws_sdk_lambda::types::StepSucceededDetails::builder()
                        .set_result(result())
                        .retry_details(retry_details(op.attempt.saturating_add(1), None))
                        .build(),
                ),
            (RunnerKind::Step, OperationAction::Fail) => op_event(op, EventType::StepFailed)
                .step_failed_details(
                    aws_sdk_lambda::types::StepFailedDetails::builder()
                        .set_error(error())
                        .retry_details(retry_details(op.attempt.saturating_add(1), None))
                        .build(),
                ),
            // A `RETRY` has already counted the attempt in `attempt`, so
            // `attempt` is the 1-based number of that attempt. A `RETRY`
            // with an error is a failed step attempt, which the service
            // records as `StepFailed`. A `RETRY` without an error is a
            // `wait_for_condition` check that asked to continue, which the
            // service records as `StepSucceeded` with the update's payload,
            // the new state.
            (RunnerKind::Step, OperationAction::Retry) => {
                let retry = retry_details(
                    op.attempt,
                    update
                        .step_options()
                        .and_then(aws_sdk_lambda::types::StepOptions::next_attempt_delay_seconds),
                );
                match error() {
                    Some(error) => op_event(op, EventType::StepFailed).step_failed_details(
                        aws_sdk_lambda::types::StepFailedDetails::builder()
                            .error(error)
                            .retry_details(retry)
                            .build(),
                    ),
                    None => op_event(op, EventType::StepSucceeded).step_succeeded_details(
                        aws_sdk_lambda::types::StepSucceededDetails::builder()
                            .set_result(event_result(update.payload()))
                            .retry_details(retry)
                            .build(),
                    ),
                }
            }
            (RunnerKind::Context, OperationAction::Start) => {
                op_event(op, EventType::ContextStarted).context_started_details(
                    aws_sdk_lambda::types::ContextStartedDetails::builder().build(),
                )
            }
            (RunnerKind::Context, OperationAction::Succeed) => {
                op_event(op, EventType::ContextSucceeded).context_succeeded_details(
                    aws_sdk_lambda::types::ContextSucceededDetails::builder()
                        .set_result(result())
                        .build(),
                )
            }
            (RunnerKind::Context, OperationAction::Fail) => op_event(op, EventType::ContextFailed)
                .context_failed_details(
                    aws_sdk_lambda::types::ContextFailedDetails::builder()
                        .set_error(error())
                        .build(),
                ),
            (RunnerKind::Wait, OperationAction::Start) => {
                // A missing duration counts as 0, as the stored due time
                // does (see [`whole_seconds`]).
                let duration = op.wait_seconds.unwrap_or(0);
                #[expect(clippy::expect_used)] // reason: both required fields are set
                let details = aws_sdk_lambda::types::WaitStartedDetails::builder()
                    .duration(duration)
                    .scheduled_end_timestamp(
                        self.event_time(self.now.saturating_add(whole_seconds(Some(duration)))),
                    )
                    .build()
                    .expect("the duration and the scheduled end are set");
                op_event(op, EventType::WaitStarted).wait_started_details(details)
            }
            (RunnerKind::Wait, OperationAction::Cancel) => op_event(op, EventType::WaitCancelled)
                .wait_cancelled_details(
                    aws_sdk_lambda::types::WaitCancelledDetails::builder()
                        .set_error(error())
                        .build(),
                ),
            (RunnerKind::Callback, OperationAction::Start) => {
                let options = update.callback_options();
                // A callback `START` assigns the callback ID when the
                // record has none (see `apply_update`).
                #[expect(clippy::expect_used)] // reason: the callback ID is set
                let details = aws_sdk_lambda::types::CallbackStartedDetails::builder()
                    .set_callback_id(op.callback_id.clone())
                    .set_timeout(
                        options.map(aws_sdk_lambda::types::CallbackOptions::timeout_seconds),
                    )
                    .set_heartbeat_timeout(
                        options
                            .map(aws_sdk_lambda::types::CallbackOptions::heartbeat_timeout_seconds),
                    )
                    .build()
                    .expect("a callback START sets the callback ID");
                op_event(op, EventType::CallbackStarted).callback_started_details(details)
            }
            (RunnerKind::ChainedInvoke, OperationAction::Start) => {
                // A `START` without `ChainedInvokeOptions` names no
                // function, and records the empty name.
                #[expect(clippy::expect_used)] // reason: the function name is set
                let details = aws_sdk_lambda::types::ChainedInvokeStartedDetails::builder()
                    .function_name(op.invoke_function.clone().unwrap_or_default())
                    .set_input(
                        op.invoke_input
                            .as_deref()
                            .map(|p| EventInput::builder().payload(p).build()),
                    )
                    .build()
                    .expect("the function name is set");
                op_event(op, EventType::ChainedInvokeStarted)
                    .chained_invoke_started_details(details)
            }
            _ => return None,
        };
        Some(event)
    }

    /// Checks a checkpoint request against the history rules (see
    /// [`validate_history_rules`]), then applies its updates in order and
    /// returns the records the response carries.
    ///
    /// The rules read the state stored before the request. So a rejected
    /// request changes no operation, no token and no history event.
    fn apply_request(
        &mut self,
        updates: &[OperationUpdate],
        kinds: Vec<OperationKind>,
    ) -> Result<Vec<Operation>, String> {
        validate_history_rules(&self.ops, updates, &kinds)?;
        self.token_counter += 1;
        let mut out = Vec::with_capacity(updates.len());
        for (update, kind) in updates.iter().zip(kinds) {
            if let Some(stored) = self.apply_update(kind, update)
                && let Some(op) = build_operation(&stored, self.base)
            {
                out.push(op);
            }
        }
        Ok(out)
    }

    /// Finds a stored op by wire ID.
    fn find_mut(&mut self, id: &str) -> Option<&mut StoredOp> {
        self.ops.iter_mut().find(|op| op.id == id)
    }

    /// Applies a single `OperationUpdate` to the store, returning the
    /// resulting stored op (cloned) for the checkpoint response.
    #[expect(clippy::too_many_lines)] // reason: one match over the update actions reads better as one flow
    fn apply_update(&mut self, kind: OperationKind, update: &OperationUpdate) -> Option<StoredOp> {
        // An `EXECUTION` update carries the execution's result. It is kept
        // apart, so it never appears as an operation.
        let Some(kind) = RunnerKind::from_operation_kind(kind) else {
            self.execution_result = update.payload().map(ToOwned::to_owned);
            return None;
        };
        let id = update.id().to_owned();
        let sub_type = update.sub_type().map(ToOwned::to_owned);
        let action = update.action().clone();
        let name = update.name().map(ToOwned::to_owned);
        let payload = update.payload().map(ToOwned::to_owned);
        let (err_type, err_msg, err_data, err_trace) =
            update.error().map_or((None, None, None, Vec::new()), |e| {
                (
                    e.error_type().map(ToOwned::to_owned),
                    e.error_message().map(ToOwned::to_owned),
                    e.error_data().map(ToOwned::to_owned),
                    e.stack_trace().to_vec(),
                )
            });
        let wait_seconds = update
            .wait_options()
            .and_then(aws_sdk_lambda::types::WaitOptions::wait_seconds);
        let retry_delay = update
            .step_options()
            .and_then(aws_sdk_lambda::types::StepOptions::next_attempt_delay_seconds);
        let parent_id = update.parent_id().map(ToOwned::to_owned);
        let heartbeat_seconds = update
            .callback_options()
            .map(aws_sdk_lambda::types::CallbackOptions::heartbeat_timeout_seconds);
        let invoke_function = update
            .chained_invoke_options()
            .map(|options| options.function_name().to_owned());
        let replay_children = update
            .context_options()
            .and_then(ContextOptions::replay_children)
            .unwrap_or(false);
        // Read the clock before the mutable op borrow below.
        let now = self.now;

        // Ensure the op exists. The parent comes from the creating update
        // only, as on the service.
        if self.find_mut(&id).is_none() {
            self.ops.push(new_started_op(
                id.clone(),
                kind,
                sub_type.clone(),
                parent_id,
                now,
            ));
        }

        // Decide callback-id assignment before taking the mutable op borrow
        // (assigning self.callback_counter later would conflict with it).
        let needs_callback_id = matches!(action, OperationAction::Start)
            && kind == RunnerKind::Callback
            && self
                .ops
                .iter()
                .find(|o| o.id == id)
                .is_some_and(|o| o.callback_id.is_none());
        let new_callback_id = if needs_callback_id {
            self.callback_counter += 1;
            Some(format!("local-callback-{}", self.callback_counter))
        } else {
            None
        };

        let op = self.find_mut(&id)?;

        // Keep metadata current.
        if op.sub_type.is_none() {
            op.sub_type = sub_type;
        }
        if name.is_some() {
            op.name = name;
        }

        match action {
            OperationAction::Start => {
                // A Start carries no payload: clear any prior result so the
                // whole-record merge in the SDK clobbers the carried value
                // (the wait_for_condition read-before-Start invariant).
                op.result = None;
                op.error_type = None;
                op.error_message = None;
                op.error_data = None;
                op.stack_trace = Vec::new();
                if op.kind == RunnerKind::Wait {
                    op.set_status(OperationStatus::Started, now);
                    op.wait_seconds = wait_seconds;
                    op.due_at = Some(now.saturating_add(whole_seconds(wait_seconds)));
                } else if op.kind == RunnerKind::Callback {
                    // The service creates a callback in `Started` and keeps
                    // it there until an external outcome. The backend
                    // assigns the callback id here.
                    op.set_status(OperationStatus::Started, now);
                    if let Some(cb) = new_callback_id {
                        op.callback_id = Some(cb);
                    }
                    op.heartbeat_seconds = heartbeat_seconds;
                } else {
                    op.set_status(OperationStatus::Started, now);
                    if op.kind == RunnerKind::ChainedInvoke {
                        op.invoke_function = invoke_function;
                        op.invoke_input = payload;
                    }
                }
            }
            OperationAction::Succeed => {
                op.set_status(OperationStatus::Succeeded, now);
                op.result = payload;
                if op.kind == RunnerKind::Context {
                    op.replay_children = replay_children;
                }
            }
            OperationAction::Fail => {
                op.set_status(OperationStatus::Failed, now);
                if op.kind == RunnerKind::Context {
                    op.replay_children = replay_children;
                }
                op.error_type = err_type;
                op.error_message = err_msg;
                op.error_data = err_data;
                op.stack_trace = err_trace;
            }
            OperationAction::Retry => {
                op.set_status(OperationStatus::Pending, now);
                op.attempt += 1;
                op.due_at = Some(now.saturating_add(whole_seconds(retry_delay)));
                op.error_type = err_type;
                op.error_message = err_msg;
                op.error_data = err_data;
                op.stack_trace = err_trace;
                // wait_for_condition carries per-attempt state in the payload.
                if payload.is_some() {
                    op.result = payload;
                }
            }
            _ => {}
        }

        let stored = op.clone();
        if let Some(event) = self.update_event(&stored, update) {
            self.push_event(event);
        }
        Some(stored)
    }
}

impl Backend {
    /// Applies and answers one checkpoint request with no latency. With
    /// `records`, the request is applied to those locked records, in the
    /// caller's transition; else the backend locks its records.
    fn checkpoint_now<'a>(
        &'a self,
        updates: &[OperationUpdate],
        records: Option<&mut BackendState>,
    ) -> Pin<Box<dyn Future<Output = Result<CheckpointOutput, ExecutionClientError>> + Send + 'a>>
    {
        let number = self
            .checkpoint_calls
            .fetch_add(1, std::sync::atomic::Ordering::SeqCst)
            .saturating_add(1);
        let answer = self.checkpoint_counted(updates, records);
        if self.withdraw_at != Some(number) {
            return answer;
        }
        // `LocalRunner::withdraw_checkpoints_after`: the request is applied,
        // and its answer carries no token.
        Box::pin(async move {
            let output = answer.await?;
            self.withdrew
                .store(true, std::sync::atomic::Ordering::SeqCst);
            Ok(CheckpointOutput {
                checkpoint_token: None,
                ..output
            })
        })
    }

    /// Returns once an invocation may start under the hold rule, and starts
    /// it in the same transition (see [`RunState::admit_invocation`]).
    /// Without a run state it returns at once.
    async fn admit_invocation(&self) {
        if let Some(live) = self.live() {
            live.wait_until(RunState::admit_invocation).await;
        }
    }

    /// Records that the invocation that started at the virtual time
    /// `started_at` ended, as one transition: its `InvocationCompleted`
    /// event with `error`, the run's invocation phase and the pauses that
    /// waited for it (see [`RunState::end_invocation`]), and the recheck of
    /// the waiting `wait_for_data` calls. Then it wakes the controls.
    /// `closing` is set for a terminal envelope: the run ends, so a reply
    /// or heartbeat after this step is refused.
    fn finish_invocation(
        &self,
        closing: bool,
        started_at: u64,
        request_id: &str,
        error: Option<&crate::WireError>,
    ) {
        let Some(live) = self.live() else {
            Self::record_invocation_completed(&mut self.lock(), started_at, request_id, error);
            return;
        };
        let wakers = {
            let mut state = live.lock();
            Self::record_invocation_completed(
                &mut state.local.records,
                started_at,
                request_id,
                error,
            );
            let mut wakers = state.end_invocation(closing);
            wakers.extend(state.recheck());
            wakers
        };
        wake(wakers);
        live.wake_all();
    }

    /// Records that the run ends, so a later reply or heartbeat is refused.
    fn close(&self) {
        if let Some(live) = self.live() {
            live.lock().local.phase = LocalPhase::Closed;
        }
    }

    /// One decision of [`LocalRunner::between_invocations`]. A top-level run
    /// makes each [`between_decision`] as one transition on the run state,
    /// and waits for the state to change while the decision is to wait. A
    /// nested run has no run state and no hold rule, so it never waits.
    async fn between_step(&self, start: &mut bool, invokes_tried: bool) -> BetweenStep {
        let Some(live) = self.live() else {
            return progress_decision(*start, invokes_tried, || self.advance());
        };
        loop {
            let notified = live.notify.notified();
            let mut notified = std::pin::pin!(notified);
            notified.as_mut().enable();
            let (step, wakers) = between_decision(&mut live.lock(), start, invokes_tried);
            wake(wakers);
            match step {
                Decided::Step(step) => return step,
                Decided::Again => {}
                Decided::Wait => notified.await,
            }
        }
    }

    /// Applies and answers one checkpoint request of a top-level run, under
    /// the hold rule: the request waits until a control lets the run move.
    /// The hold check, the request, a pause's tokenless answer and the
    /// recheck of the waiting `wait_for_data` calls are one transition on
    /// the run state.
    async fn checkpoint_held(
        &self,
        live: &LiveState,
        updates: &[OperationUpdate],
    ) -> Result<CheckpointOutput, ExecutionClientError> {
        let (answer, tokenless, wakers) = loop {
            let notified = live.notify.notified();
            let mut notified = std::pin::pin!(notified);
            notified.as_mut().enable();
            {
                let mut state = live.lock();
                if state.hold_open() {
                    let answer = self.checkpoint_now(updates, Some(&mut state.local.records));
                    let tokenless = state.take_tokenless();
                    let wakers = state.recheck();
                    break (answer, tokenless, wakers);
                }
            }
            notified.await;
        };
        wake(wakers);
        let output = answer.await?;
        if tokenless {
            self.withdrew
                .store(true, std::sync::atomic::Ordering::SeqCst);
            return Ok(CheckpointOutput {
                checkpoint_token: None,
                ..output
            });
        }
        Ok(output)
    }

    /// Whether a request was answered without a token since the last call.
    fn take_withdrawal(&self) -> bool {
        self.withdrew
            .swap(false, std::sync::atomic::Ordering::SeqCst)
    }

    /// [`Self::checkpoint_now`] after the request was counted.
    fn checkpoint_counted<'a>(
        &'a self,
        updates: &[OperationUpdate],
        records: Option<&mut BackendState>,
    ) -> Pin<Box<dyn Future<Output = Result<CheckpointOutput, ExecutionClientError>> + Send + 'a>>
    {
        // In-crate test seam: a per-call plan (see
        // [`Backend::plan_checkpoint_calls`]) takes precedence over the
        // counter-based injection below while entries remain.
        #[cfg(test)]
        if let Some(planned) = self.next_planned_checkpoint() {
            match planned {
                PlannedCheckpoint::Pass => {}
                PlannedCheckpoint::PassGated { gate } => {
                    let answer = self.checkpoint_unplanned(updates, records);
                    return Box::pin(async move {
                        drop(gate.acquire().await);
                        answer.await
                    });
                }
                PlannedCheckpoint::PassThenRetryableError(message) => {
                    let answer = self.checkpoint_unplanned(updates, records);
                    return Box::pin(async move {
                        // The answer is replaced whatever it was: the test
                        // sees only the lost response.
                        drop(answer.await);
                        Err(ExecutionClientError::new(
                            crate::ExecutionClientErrorScope::Invocation,
                            message,
                        ))
                    });
                }
                PlannedCheckpoint::FailNonRetryable { gate } => {
                    return Box::pin(async move {
                        if let Some(gate) = gate {
                            // Hold the failing write in flight until the
                            // test releases it: lets a test drop a
                            // contributor deterministically BEFORE the
                            // failure publishes.
                            let permit = gate.acquire().await;
                            drop(permit);
                        }
                        Err(ExecutionClientError::new_non_retryable(
                            "planned non-retryable checkpoint failure (test plan)",
                        ))
                    });
                }
            }
        }
        self.checkpoint_unplanned(updates, records)
    }

    /// [`Self::checkpoint_now`] after the per-call plan: the injected
    /// faults, then the request against the simulated store.
    fn checkpoint_unplanned<'a>(
        &'a self,
        updates: &[OperationUpdate],
        records: Option<&mut BackendState>,
    ) -> Pin<Box<dyn Future<Output = Result<CheckpointOutput, ExecutionClientError>> + Send + 'a>>
    {
        // Injected fault (`LocalRunner::fail_next_checkpoints` /
        // `fail_checkpoints_after[_retryable]`): after letting `skip`
        // calls through, reject BEFORE touching any state, so the
        // rejected write persists nothing, exactly like a service-side
        // rejection.
        let skipped = atomic_decrement_if_positive(&self.checkpoint_failures_skip);
        if !skipped && atomic_decrement_if_positive(&self.checkpoint_failures_remaining) {
            let retryable = self
                .checkpoint_failures_retryable
                .load(std::sync::atomic::Ordering::SeqCst);
            return Box::pin(async move {
                if retryable {
                    Err(ExecutionClientError::from_retryable(
                        "injected retryable checkpoint failure \
                         (LocalRunner::fail_checkpoints_after_retryable)"
                            .to_owned(),
                    ))
                } else {
                    Err(ExecutionClientError::new_non_retryable(
                        "injected checkpoint failure (LocalRunner::fail_next_checkpoints)",
                    ))
                }
            });
        }
        // The service rejects a request that breaks a size limit or a
        // delay range before it changes any state. So the check runs before
        // the state lock, and a rejected request stores nothing and keeps
        // the token.
        if let Err(message) = validate_service_limits(updates) {
            return Box::pin(async move { Err(ExecutionClientError::new_non_retryable(&message)) });
        }
        // An invoke of a name the runner has no target for can never
        // complete. So the backend rejects it before any state change, and
        // the run fails with a message naming the function.
        if let Some(name) = updates
            .iter()
            .filter(|update| {
                update.r#type() == &OperationType::ChainedInvoke
                    && update.action() == &OperationAction::Start
            })
            .filter_map(OperationUpdate::chained_invoke_options)
            .map(aws_sdk_lambda::types::ChainedInvokeOptions::function_name)
            .find(|name| !self.registered_functions.contains(*name))
        {
            let message = format!(
                "No function found for function name \"{name}\". Register it on the LocalRunner."
            );
            return Box::pin(async move { Err(ExecutionClientError::new_non_retryable(&message)) });
        }
        // The service rejects an update of a type it does not know, before
        // any state change. So every type converts before the state lock;
        // an unknown type stores nothing and keeps the token.
        let Some(kinds) = update_kinds(updates) else {
            return Box::pin(async {
                Err(ExecutionClientError::new_non_retryable(
                    "Invalid operation type.",
                ))
            });
        };
        // The request, the token and the response page are read in one
        // locked section of the records.
        let mut own = None;
        let records = match records {
            Some(records) => records,
            None => &mut **own.insert(self.lock()),
        };
        let updated_ops = match records.apply_request(updates, kinds) {
            Ok(ops) => ops,
            Err(message) => {
                return Box::pin(
                    async move { Err(ExecutionClientError::new_non_retryable(&message)) },
                );
            }
        };
        let token = format!("local-token-{}", records.token_counter);

        // If a checkpoint page size is configured and the total stored
        // operations exceed it, simulate a genuinely paginated response:
        // return only the FIRST PAGE of the full execution state (mirroring
        // the service's NewExecutionState) and set next_marker. Operations
        // beyond the page, including the ones this call just updated when
        // they fall outside it, are only observable through the follow-up
        // get_state fetch, so a caller that ignores the marker genuinely
        // misses state.
        let (response_ops, next_marker) = if let Some(page_size) = self.checkpoint_page_size {
            let all_ops: Vec<Operation> = records
                .ops
                .iter()
                .filter_map(|op| build_operation(op, records.base))
                .collect();
            if all_ops.len() > page_size {
                let first_page: Vec<Operation> =
                    all_ops.get(..page_size).unwrap_or(&all_ops).to_vec();
                let marker = format!("page-marker-{}", all_ops.len());
                (first_page, Some(marker))
            } else {
                (updated_ops, None)
            }
        } else {
            (updated_ops, None)
        };

        Box::pin(async move {
            Ok(CheckpointOutput {
                checkpoint_token: Some(token),
                updated_operations: response_ops,
                next_marker,
            })
        })
    }
}

impl ExecutionClient for Backend {
    fn checkpoint(
        &self,
        _execution_arn: &str,
        _checkpoint_token: &str,
        updates: Vec<OperationUpdate>,
    ) -> Pin<Box<dyn Future<Output = Result<CheckpointOutput, ExecutionClientError>> + Send + '_>>
    {
        // `tokio::time::sleep` panics on a runtime built without the time
        // driver, and the SDK itself needs no timer. So the default zero
        // latency answers without creating a sleep.
        let latency = self.checkpoint_latency;
        if let Some(live) = self.live() {
            return Box::pin(async move {
                if !latency.is_zero() {
                    tokio::time::sleep(latency).await;
                }
                self.checkpoint_held(live, &updates).await
            });
        }
        if latency.is_zero() {
            return self.checkpoint_now(&updates, None);
        }
        Box::pin(async move {
            tokio::time::sleep(latency).await;
            self.checkpoint_now(&updates, None).await
        })
    }

    fn get_state(
        &self,
        _execution_arn: &str,
        _checkpoint_token: &str,
        _marker: Option<&str>,
    ) -> Pin<Box<dyn Future<Output = Result<GetStateOutput, ExecutionClientError>> + Send + '_>>
    {
        self.get_state_calls
            .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        let operations = self.build_pruned_operations();
        Box::pin(async move { Ok(GetStateOutput::new(operations, None)) })
    }
}

/// The kind of each update, in order, or `None` when an update has a type
/// that [`OperationKind`] does not list.
fn update_kinds(updates: &[OperationUpdate]) -> Option<Vec<OperationKind>> {
    updates
        .iter()
        .map(|update| OperationKind::from_wire(update.r#type()))
        .collect()
}

/// Checks a checkpoint request against the history rules of JS's checkpoint
/// server, and returns JS's message for the first rule it breaks.
///
/// `ops` is the state stored before the request, and `kinds` holds the kind
/// of each update. The checks run in JS's order:
///
/// 1. One pass over the request checks repeated IDs and parents (see
///    [`validate_parents_and_repeats`]).
/// 2. Then each update in turn is checked against the operation stored
///    before the request: first its identity, then its action against its
///    type, then its action against the stored status.
///
/// Every check compares with the stored state, never with an earlier
/// update of the same request.
fn validate_history_rules(
    ops: &[StoredOp],
    updates: &[OperationUpdate],
    kinds: &[OperationKind],
) -> Result<(), String> {
    validate_parents_and_repeats(ops, updates, kinds)?;
    for (update, kind) in updates.iter().zip(kinds) {
        let stored = ops.iter().find(|op| op.id == update.id());
        if let Some(stored) = stored {
            validate_identity(update, *kind, stored)?;
        }
        validate_action_for_type(*kind, update.action())?;
        validate_status_transition(*kind, update.action(), stored)?;
    }
    Ok(())
}

/// Checks the repeated IDs and the parents of a request, in request order.
///
/// 1. An ID may repeat only when the latest earlier update of that ID is a
///    STEP or CONTEXT `START` and the repeat is not a `START`.
/// 2. A supplied `ParentId` must name a CONTEXT that an earlier update of
///    the request starts, or a stored CONTEXT. An earlier `START` of the ID
///    takes precedence over the stored record, as in JS.
///
/// The repeat check of an update runs before its parent check.
fn validate_parents_and_repeats(
    ops: &[StoredOp],
    updates: &[OperationUpdate],
    kinds: &[OperationKind],
) -> Result<(), String> {
    let mut started: BTreeMap<&str, OperationKind> = BTreeMap::new();
    let mut last_seen: BTreeMap<&str, (OperationKind, &OperationAction)> = BTreeMap::new();
    for (update, kind) in updates.iter().zip(kinds) {
        let id = update.id();
        let action = update.action();
        if let Some((last_kind, last_action)) = last_seen.get(id) {
            let allowed = matches!(last_kind, OperationKind::Step | OperationKind::Context)
                && **last_action == OperationAction::Start
                && *action != OperationAction::Start;
            if !allowed {
                return Err("Cannot update the same operation twice in a single request.".into());
            }
        }
        if let Some(parent_id) = update.parent_id() {
            let parent_kind = started.get(parent_id).copied().or_else(|| {
                ops.iter()
                    .find(|op| op.id == parent_id)
                    .map(|op| op.kind.operation_kind())
            });
            if parent_kind != Some(OperationKind::Context) {
                return Err("Invalid parent operation id.".into());
            }
        }
        if *action == OperationAction::Start {
            started.insert(id, *kind);
        }
        last_seen.insert(id, (*kind, action));
    }
    Ok(())
}

/// Checks that an update keeps the `Type`, `SubType`, `Name` and
/// `ParentId` of the stored operation. A field the update omits passes.
fn validate_identity(
    update: &OperationUpdate,
    kind: OperationKind,
    stored: &StoredOp,
) -> Result<(), String> {
    let inconsistent = |field: &str| {
        Err(format!(
            "{field} value in OperationUpdate is inconsistent with existing operation. \
             Check for non-determinism."
        ))
    };
    let differs = |supplied: Option<&str>, stored: Option<&String>| {
        supplied.is_some_and(|value| Some(value) != stored.map(String::as_str))
    };
    if kind != stored.kind.operation_kind() {
        return inconsistent("Type");
    }
    if differs(update.sub_type(), stored.sub_type.as_ref()) {
        return inconsistent("SubType");
    }
    if differs(update.name(), stored.name.as_ref()) {
        return inconsistent("Name");
    }
    if differs(update.parent_id(), stored.parent_id.as_ref()) {
        return inconsistent("ParentId");
    }
    Ok(())
}

/// Checks that `action` is one of the actions JS allows for `kind`.
fn validate_action_for_type(kind: OperationKind, action: &OperationAction) -> Result<(), String> {
    use OperationAction as A;
    let allowed = match kind {
        OperationKind::Step => matches!(action, A::Start | A::Fail | A::Retry | A::Succeed),
        OperationKind::Context => matches!(action, A::Start | A::Fail | A::Succeed),
        OperationKind::Wait => matches!(action, A::Start | A::Cancel),
        OperationKind::Callback | OperationKind::ChainedInvoke => matches!(action, A::Start),
        OperationKind::Execution => matches!(action, A::Succeed | A::Fail),
    };
    if allowed {
        Ok(())
    } else {
        Err("Invalid action for the given operation type.".into())
    }
}

/// Checks that `action` is valid for the status of the stored operation,
/// with the message of JS's validator for `kind`. `stored` is `None` when
/// no operation has the update's ID.
///
/// [`validate_action_for_type`] has already accepted the action, so only
/// the actions of `kind` reach this function.
fn validate_status_transition(
    kind: OperationKind,
    action: &OperationAction,
    stored: Option<&StoredOp>,
) -> Result<(), String> {
    use OperationStatus as S;
    let status = stored.map(|op| op.status);
    let message = match (kind, action) {
        (OperationKind::Step, OperationAction::Start) => status
            .is_some_and(|s| s != S::Ready)
            .then_some("Invalid current STEP state to start."),
        (OperationKind::Step, OperationAction::Retry) => status
            .is_some_and(|s| !matches!(s, S::Started | S::Ready))
            .then_some("Invalid current STEP state to retry."),
        (OperationKind::Step, _) => status
            .is_some_and(|s| !matches!(s, S::Started | S::Ready))
            .then_some("Invalid current STEP state to close."),
        (OperationKind::Context, OperationAction::Start) => status
            .is_some()
            .then_some("Cannot start a CONTEXT operation that already exists."),
        (OperationKind::Context, _) => status
            .is_some_and(|s| s != S::Started)
            .then_some("Invalid current CONTEXT state to close."),
        (OperationKind::Wait, OperationAction::Start) => status
            .is_some()
            .then_some("Cannot start a WAIT operation that already exists."),
        (OperationKind::Wait, _) => (status != Some(S::Started)).then_some(
            "Cannot cancel a WAIT operation that does not exist or has already completed.",
        ),
        (OperationKind::Callback, _) => status
            .is_some()
            .then_some("Cannot start a CALLBACK operation that already exists."),
        (OperationKind::ChainedInvoke, _) => status
            .is_some()
            .then_some("Cannot start a CHAINED_INVOKE operation that already exists."),
        (OperationKind::Execution, _) => None,
    };
    message.map_or(Ok(()), |message| Err(message.to_owned()))
}

/// Whether `stored` carries any error field. A callback failure may set
/// only some of the four fields, so any one of them makes an error object.
fn has_error_fields(stored: &StoredOp) -> bool {
    stored.error_type.is_some()
        || stored.error_message.is_some()
        || stored.error_data.is_some()
        || !stored.stack_trace.is_empty()
}

/// The error a stored op carries, or `None` when it has no error field
/// (see [`has_error_fields`]).
fn stored_wire_error(stored: &StoredOp) -> Option<crate::WireError> {
    has_error_fields(stored).then(|| {
        crate::WireError::new(
            stored.error_type.as_deref(),
            stored.error_message.as_deref(),
        )
        .with_error_data(stored.error_data.as_deref())
        .with_stack_trace(stored.stack_trace.clone())
    })
}

/// The service `ErrorObject` of a wire error, with every field it has.
fn error_object(wire: &crate::WireError) -> ErrorObject {
    let mut builder = ErrorObject::builder()
        .set_error_type(wire.error_type().map(ToOwned::to_owned))
        .set_error_message(wire.error_message().map(ToOwned::to_owned))
        .set_error_data(wire.error_data().map(ToOwned::to_owned));
    for frame in wire.stack_trace() {
        builder = builder.stack_trace(frame);
    }
    builder.build()
}

/// The `EventError` of a history event that carries `wire`.
fn event_error(wire: &crate::WireError) -> EventError {
    EventError::builder().payload(error_object(wire)).build()
}

/// The `EventResult` of a history event that carries `payload`.
fn event_result(payload: Option<&str>) -> Option<EventResult> {
    payload.map(|p| EventResult::builder().payload(p).build())
}

/// The ID of the execution operation of every synthesized envelope.
const EXECUTION_OPERATION_ID: &str = "execution";

/// An execution-level history event of `event_type`, with the ID of the
/// execution operation.
fn execution_event(event_type: EventType) -> EventBuilder {
    Event::builder()
        .event_type(event_type)
        .id(EXECUTION_OPERATION_ID)
}

/// A history event of `event_type` for the stored op `op`, with its ID,
/// name, sub-type and parent ID.
fn op_event(op: &StoredOp, event_type: EventType) -> EventBuilder {
    Event::builder()
        .event_type(event_type)
        .id(&op.id)
        .set_name(op.name.clone())
        .set_sub_type(op.sub_type.clone())
        .set_parent_id(op.parent_id.clone())
}

/// The retry details of a step event: the 1-based `current_attempt` and
/// the retry delay.
fn retry_details(current_attempt: i32, next_attempt_delay_seconds: Option<i32>) -> RetryDetails {
    RetryDetails::builder()
        .current_attempt(current_attempt)
        .set_next_attempt_delay_seconds(next_attempt_delay_seconds)
        .build()
}

/// The history event of a record the backend settled between invocations:
/// a fired wait, a callback that received a reply, or a chained invoke whose
/// target returned. A step whose retry delay elapsed (`Ready`) has none.
fn settled_event(op: &StoredOp) -> Option<EventBuilder> {
    let error = || stored_wire_error(op).as_ref().map(event_error);
    let result = || event_result(op.result.as_deref());
    let event = match (op.kind, op.status) {
        (RunnerKind::Wait, OperationStatus::Succeeded) => op_event(op, EventType::WaitSucceeded)
            .wait_succeeded_details(
                aws_sdk_lambda::types::WaitSucceededDetails::builder()
                    .set_duration(op.wait_seconds)
                    .build(),
            ),
        (RunnerKind::Callback, OperationStatus::Succeeded) => {
            op_event(op, EventType::CallbackSucceeded).callback_succeeded_details(
                aws_sdk_lambda::types::CallbackSucceededDetails::builder()
                    .set_result(result())
                    .build(),
            )
        }
        (RunnerKind::Callback, OperationStatus::Failed) => op_event(op, EventType::CallbackFailed)
            .callback_failed_details(
                aws_sdk_lambda::types::CallbackFailedDetails::builder()
                    .set_error(error())
                    .build(),
            ),
        (RunnerKind::Callback, OperationStatus::TimedOut) => {
            op_event(op, EventType::CallbackTimedOut).callback_timed_out_details(
                aws_sdk_lambda::types::CallbackTimedOutDetails::builder()
                    .set_error(error())
                    .build(),
            )
        }
        (RunnerKind::ChainedInvoke, OperationStatus::Succeeded) => {
            op_event(op, EventType::ChainedInvokeSucceeded).chained_invoke_succeeded_details(
                aws_sdk_lambda::types::ChainedInvokeSucceededDetails::builder()
                    .set_result(result())
                    .build(),
            )
        }
        (RunnerKind::ChainedInvoke, OperationStatus::Failed) => {
            op_event(op, EventType::ChainedInvokeFailed).chained_invoke_failed_details(
                aws_sdk_lambda::types::ChainedInvokeFailedDetails::builder()
                    .set_error(error())
                    .build(),
            )
        }
        _ => return None,
    };
    Some(event)
}

/// The `DateTime` of the virtual time `t`: the run's start time `base`
/// plus `t` seconds, the time [`TestOperation::start_time`] and
/// [`TestOperation::end_time`] report.
///
/// `DateTime` counts seconds in an `i64`, so the sum cannot leave its
/// range: `t` is a sum of `i32` wait and retry durations. The sum
/// saturates, so no conversion panics.
fn virtual_date_time(base: std::time::SystemTime, t: u64) -> DateTime {
    let base = DateTime::from(base);
    DateTime::from_secs_and_nanos(
        base.secs().saturating_add(t.cast_signed()),
        base.subsec_nanos(),
    )
}

/// The epoch milliseconds of the virtual time `t`: the run's start time
/// `base` in epoch milliseconds plus `t` seconds. The envelope carries
/// operation times in this unit, as the service does. The arithmetic
/// saturates, so no conversion panics.
fn virtual_epoch_millis(base: std::time::SystemTime, t: u64) -> u64 {
    let base_ms = base
        .duration_since(std::time::SystemTime::UNIX_EPOCH)
        .map_or(0, |d| u64::try_from(d.as_millis()).unwrap_or(u64::MAX));
    base_ms.saturating_add(t.saturating_mul(1000))
}

/// Builds an SDK `Operation` from a stored op, placing result/error in the
/// details bucket the SDK reads for that operation type. The start and end
/// times are the run's start time `base` plus the record's virtual times.
fn build_operation(stored: &StoredOp, base: std::time::SystemTime) -> Option<Operation> {
    let error_obj = stored_wire_error(stored).map(|wire| error_object(&wire));

    let mut builder = Operation::builder()
        .id(&stored.id)
        .r#type(OperationType::from(stored.kind.operation_kind().wire_str()))
        .status(WireOperationStatus::from(stored.status.wire_str()))
        .start_timestamp(virtual_date_time(base, stored.start_at));
    if let Some(end_at) = stored.end_at {
        builder = builder.end_timestamp(virtual_date_time(base, end_at));
    }

    if let Some(ref st) = stored.sub_type {
        builder = builder.sub_type(st);
    }
    if let Some(ref n) = stored.name {
        builder = builder.name(n);
    }
    if let Some(ref parent) = stored.parent_id {
        builder = builder.parent_id(parent);
    }

    match stored.kind {
        RunnerKind::Step => {
            let mut sd = WireStepDetails::builder().attempt(stored.attempt);
            if let Some(r) = &stored.result {
                sd = sd.result(r);
            }
            if let Some(e) = error_obj.clone() {
                sd = sd.error(e);
            }
            builder = builder.step_details(sd.build());
        }
        RunnerKind::Context => {
            let mut cd = WireContextDetails::builder();
            if let Some(r) = &stored.result {
                cd = cd.result(r);
            }
            if let Some(e) = error_obj.clone() {
                cd = cd.error(e);
            }
            if stored.replay_children {
                cd = cd.replay_children(true);
            }
            builder = builder.context_details(cd.build());
        }
        RunnerKind::Callback => {
            let mut cb = WireCallbackDetails::builder();
            if let Some(id) = &stored.callback_id {
                cb = cb.callback_id(id);
            }
            if let Some(r) = &stored.result {
                cb = cb.result(r);
            }
            if let Some(e) = error_obj.clone() {
                cb = cb.error(e);
            }
            builder = builder.callback_details(cb.build());
        }
        RunnerKind::ChainedInvoke => {
            let mut ci = ChainedInvokeDetails::builder();
            if let Some(r) = &stored.result {
                ci = ci.result(r);
            }
            if let Some(e) = error_obj.clone() {
                ci = ci.error(e);
            }
            builder = builder.chained_invoke_details(ci.build());
        }
        RunnerKind::Wait => {}
    }

    builder.build().ok()
}

/// Renders a stored op as an envelope-wire JSON operation object, placing
/// result/error in the details bucket `parse_single_operation` reads for
/// that operation type. Mirrors [`build_operation`] field-for-field so the
/// inline envelope page and the `get_state` fetch describe identical
/// records. The start and end times are epoch milliseconds: the run's start
/// time `base` plus the record's virtual times.
fn stored_op_envelope_json(stored: &StoredOp, base: std::time::SystemTime) -> serde_json::Value {
    let mut obj = serde_json::Map::new();
    obj.insert("Id".to_owned(), serde_json::json!(stored.id));
    obj.insert(
        "Type".to_owned(),
        serde_json::json!(stored.kind.operation_kind().wire_str()),
    );
    obj.insert(
        "Status".to_owned(),
        serde_json::json!(stored.status.wire_str()),
    );
    obj.insert(
        "StartTimestamp".to_owned(),
        serde_json::json!(virtual_epoch_millis(base, stored.start_at)),
    );
    if let Some(end_at) = stored.end_at {
        obj.insert(
            "EndTimestamp".to_owned(),
            serde_json::json!(virtual_epoch_millis(base, end_at)),
        );
    }
    if let Some(st) = &stored.sub_type {
        obj.insert("SubType".to_owned(), serde_json::json!(st));
    }
    if let Some(n) = &stored.name {
        obj.insert("Name".to_owned(), serde_json::json!(n));
    }
    if let Some(parent) = &stored.parent_id {
        obj.insert("ParentId".to_owned(), serde_json::json!(parent));
    }

    let error_obj = has_error_fields(stored).then(|| {
        let mut err = serde_json::Map::new();
        if let Some(t) = &stored.error_type {
            err.insert("ErrorType".to_owned(), serde_json::json!(t));
        }
        if let Some(m) = &stored.error_message {
            err.insert("ErrorMessage".to_owned(), serde_json::json!(m));
        }
        if let Some(d) = &stored.error_data {
            err.insert("ErrorData".to_owned(), serde_json::json!(d));
        }
        if !stored.stack_trace.is_empty() {
            err.insert(
                "StackTrace".to_owned(),
                serde_json::json!(stored.stack_trace),
            );
        }
        serde_json::Value::Object(err)
    });

    let mut details = serde_json::Map::new();
    if let Some(r) = &stored.result {
        details.insert("Result".to_owned(), serde_json::json!(r));
    }
    if let Some(e) = error_obj {
        details.insert("Error".to_owned(), e);
    }

    match stored.kind {
        RunnerKind::Step => {
            details.insert("Attempt".to_owned(), serde_json::json!(stored.attempt));
            obj.insert("StepDetails".to_owned(), serde_json::Value::Object(details));
        }
        RunnerKind::Context => {
            if stored.replay_children {
                details.insert("ReplayChildren".to_owned(), serde_json::json!(true));
            }
            obj.insert(
                "ContextDetails".to_owned(),
                serde_json::Value::Object(details),
            );
        }
        RunnerKind::Callback => {
            if let Some(id) = &stored.callback_id {
                details.insert("CallbackId".to_owned(), serde_json::json!(id));
            }
            obj.insert(
                "CallbackDetails".to_owned(),
                serde_json::Value::Object(details),
            );
        }
        RunnerKind::ChainedInvoke => {
            obj.insert(
                "ChainedInvokeDetails".to_owned(),
                serde_json::Value::Object(details),
            );
        }
        RunnerKind::Wait => {}
    }

    serde_json::Value::Object(obj)
}

/// Returns the stored records the service sends in the invocation input and
/// in `GetDurableExecutionState`, in stored order.
///
/// The service omits every descendant of a completed context that did not
/// record `ReplayChildren`, because the context's result replaces them on
/// replay. The rules are:
///
/// 1. A record without a parent is kept.
/// 2. Otherwise the walk goes up the parents. A parent that is not stored
///    keeps the record. A parent that is a `Succeeded` or `Failed` context
///    without `ReplayChildren` omits the record. Any other parent continues
///    the walk. So a started context under a pruning ancestor is omitted
///    with its children.
/// 3. A crafted `ParentId` can form a cycle. So the walk takes at most
///    `ops.len()` steps, and a record whose walk reaches the bound is kept.
///
/// Checkpoint responses are not pruned, because the service does not prune
/// them.
fn prune_replay_children(ops: &[StoredOp]) -> Vec<&StoredOp> {
    let find = |id: &str| ops.iter().find(|op| op.id == id);
    let omitted = |op: &StoredOp| {
        let mut parent_id = op.parent_id.as_deref();
        for _ in 0..ops.len() {
            let Some(parent) = parent_id.and_then(find) else {
                return false;
            };
            if parent.kind == RunnerKind::Context
                && matches!(
                    parent.status,
                    OperationStatus::Succeeded | OperationStatus::Failed
                )
                && !parent.replay_children
            {
                return true;
            }
            parent_id = parent.parent_id.as_deref();
        }
        false
    };
    ops.iter().filter(|op| !omitted(op)).collect()
}

/// The smallest delay, in seconds, the service accepts for
/// `WaitOptions.WaitSeconds` and `StepOptions.NextAttemptDelaySeconds`.
const MIN_DELAY_SECONDS: i32 = 1;

/// The largest delay, in seconds, the service accepts for the same fields.
const MAX_DELAY_SECONDS: i32 = 31_622_400;

/// The most delay violations one rejection message lists.
const MAX_REPORTED_VIOLATIONS: usize = 10;

/// The largest rendering of an update that carries an error.
const ERROR_OBJECT_LIMIT: usize = 262_144;

/// Checks a checkpoint request against the service limits, and returns the
/// service's rejection message for the first failed check.
///
/// The check has two passes, in the order the service runs them.
///
/// 1. The model pass checks every delay field of every update against the
///    range 1 to 31 622 400. It collects every violation and reports at
///    most 10 of them in one message. When it finds any violation, the
///    second pass does not run.
/// 2. The size pass visits the updates in request order and stops at the
///    first violation. An update that carries an error is measured as a
///    whole (see [`render_update`]), as the JS checkpoint server measures
///    it. Then a payload is measured in bytes against the limit of its
///    operation type (see [`payload_limit`]).
fn validate_service_limits(updates: &[OperationUpdate]) -> Result<(), String> {
    let mut entries = Vec::new();
    for (index, update) in updates.iter().enumerate() {
        let pos = index + 1;
        let delays = [
            (
                "waitOptions.waitSeconds",
                update
                    .wait_options()
                    .and_then(aws_sdk_lambda::types::WaitOptions::wait_seconds),
            ),
            (
                "stepOptions.nextAttemptDelaySeconds",
                update
                    .step_options()
                    .and_then(aws_sdk_lambda::types::StepOptions::next_attempt_delay_seconds),
            ),
        ];
        for (path, value) in delays {
            let Some(value) = value else { continue };
            let constraint = if value < MIN_DELAY_SECONDS {
                "greater than or equal to 1"
            } else if value > MAX_DELAY_SECONDS {
                "less than or equal to 31622400"
            } else {
                continue;
            };
            entries.push(format!(
                "Value '{value}' at 'updates.{pos}.member.{path}' failed to satisfy \
                 constraint: Member must have value {constraint}"
            ));
        }
    }
    if !entries.is_empty() {
        entries.truncate(MAX_REPORTED_VIOLATIONS);
        let count = if entries.len() == 1 {
            "1 validation error detected".to_owned()
        } else {
            format!("{} validation errors detected", entries.len())
        };
        return Err(format!("{count}: {}", entries.join("; ")));
    }

    for update in updates {
        if update.error().is_some() && render_update(update).len() > ERROR_OBJECT_LIMIT {
            return Err("Error object size must be less than or equal to 262144 bytes.".to_owned());
        }
        if let (Some(payload), Some((limit, what))) =
            (update.payload(), payload_limit(update.r#type()))
            && payload.len() > limit
        {
            return Err(format!(
                "{what} size must be less than or equal to {limit} bytes."
            ));
        }
    }
    Ok(())
}

/// The payload limit in bytes for an operation type, and the name the
/// service's message uses for that payload. WAIT and CALLBACK payloads
/// have no limit.
fn payload_limit(op_type: &OperationType) -> Option<(usize, &'static str)> {
    match op_type {
        OperationType::Step => Some((262_144, "STEP output payload")),
        OperationType::Context => Some((262_144, "CONTEXT output payload")),
        OperationType::ChainedInvoke => Some((1_048_576, "CHAINED_INVOKE input payload")),
        OperationType::Execution => Some((6_291_456, "Execution output payload")),
        _ => None,
    }
}

/// Renders `update` as the JSON object the service model describes, with
/// every absent member omitted and every enum as its wire string.
///
/// `OperationUpdate` has no serializer, so the object is built from its
/// getters. The size pass of [`validate_service_limits`] measures this
/// rendering for an update that carries an error.
fn render_update(update: &OperationUpdate) -> String {
    use serde_json::{Map, Value};

    fn insert<T: Into<Value>>(map: &mut Map<String, Value>, key: &str, value: Option<T>) {
        if let Some(value) = value {
            map.insert(key.to_owned(), value.into());
        }
    }

    // A present error or options object stays in the rendering even when
    // all its members are absent, as `JSON.stringify` keeps an empty object.
    let mut out = Map::new();
    insert(&mut out, "Id", Some(update.id()));
    insert(&mut out, "ParentId", update.parent_id());
    insert(&mut out, "Name", update.name());
    insert(&mut out, "Type", Some(update.r#type().as_str()));
    insert(&mut out, "SubType", update.sub_type());
    insert(&mut out, "Action", Some(update.action().as_str()));
    insert(&mut out, "Payload", update.payload());
    if let Some(error) = update.error() {
        let mut map = Map::new();
        insert(&mut map, "ErrorType", error.error_type());
        insert(&mut map, "ErrorMessage", error.error_message());
        insert(&mut map, "ErrorData", error.error_data());
        insert(&mut map, "StackTrace", error.stack_trace.clone());
        out.insert("Error".to_owned(), Value::Object(map));
    }
    if let Some(options) = update.context_options() {
        let mut map = Map::new();
        insert(&mut map, "ReplayChildren", options.replay_children());
        out.insert("ContextOptions".to_owned(), Value::Object(map));
    }
    if let Some(options) = update.step_options() {
        let mut map = Map::new();
        insert(
            &mut map,
            "NextAttemptDelaySeconds",
            options.next_attempt_delay_seconds(),
        );
        out.insert("StepOptions".to_owned(), Value::Object(map));
    }
    if let Some(options) = update.wait_options() {
        let mut map = Map::new();
        insert(&mut map, "WaitSeconds", options.wait_seconds());
        out.insert("WaitOptions".to_owned(), Value::Object(map));
    }
    if let Some(options) = update.callback_options() {
        let mut map = Map::new();
        // The model types these fields as `i32` with 0 meaning "not
        // specified", and the wire serializer omits a 0. So the rendering
        // omits a 0 too.
        let nonzero = |value: i32| (value != 0).then_some(value);
        insert(
            &mut map,
            "TimeoutSeconds",
            nonzero(options.timeout_seconds()),
        );
        insert(
            &mut map,
            "HeartbeatTimeoutSeconds",
            nonzero(options.heartbeat_timeout_seconds()),
        );
        out.insert("CallbackOptions".to_owned(), Value::Object(map));
    }
    if let Some(options) = update.chained_invoke_options() {
        let mut map = Map::new();
        insert(&mut map, "FunctionName", Some(options.function_name()));
        insert(&mut map, "TenantId", options.tenant_id());
        out.insert("ChainedInvokeOptions".to_owned(), Value::Object(map));
    }
    Value::Object(out).to_string()
}

#[cfg(test)]
#[expect(clippy::indexing_slicing)]
// reason: test assertions index known-length op vectors
// Tests deliberately spawn foreign (unblessed) tasks to exercise runtime
// behavior; production spawning is confined to the src/task.rs gateway.
#[expect(clippy::disallowed_methods)]
mod tests {
    use super::live::{
        CLOUD_PAUSE_UNSUPPORTED, CLOUD_TIMEOUT_REPLY_UNSUPPORTED, ControlKind, ControlPhase,
        ControlRole, DriverFuture, HistoryStart, Registration, RunEnd,
        SEND_CALLBACK_HEARTBEAT_REQUEST_RESPONSE_UNSUPPORTED,
        SEND_CALLBACK_REQUEST_RESPONSE_UNSUPPORTED, WAIT_FOR_DATA_REQUEST_RESPONSE_UNSUPPORTED,
        await_in_flight, status_reached,
    };
    use super::*;
    use crate::builders::map_parallel::{CompletionConfig, CompletionDecision, CompletionOutcome};
    use crate::{RetryDecision, StepError};
    use serde::{Deserialize, Serialize};
    use std::sync::atomic::{AtomicU32, Ordering};

    /// The completed retries of a step operation, from its details, or
    /// `None` for another kind.
    fn step_attempt(op: &TestOperation) -> Option<u32> {
        match op.details() {
            TestOperationDetails::Step(step) => Some(step.attempt()),
            _ => None,
        }
    }

    #[test]
    fn local_runner_failed_envelope_without_type_reads_error() {
        let response = serde_json::json!({
            "Status": "FAILED",
            "Error": { "ErrorMessage": "boom" },
        });
        let result: TestResult<()> = RawRun {
            end: RawEnd::Failed(envelope_wire_error(&response)),
            operations: Vec::new(),
            invocations: 1,
            events: Vec::new(),
        }
        .into_test_result()
        .expect("runner");
        assert!(result.is_failure());
        assert_eq!(result.error_type(), Some("Error"));
        assert_eq!(result.error_message(), Some("boom"));

        // An `Error` object with neither type nor message reads type `Error`
        // and an empty message.
        let response = serde_json::json!({ "Status": "FAILED", "Error": {} });
        let result: TestResult<()> = RawRun {
            end: RawEnd::Failed(envelope_wire_error(&response)),
            operations: Vec::new(),
            invocations: 1,
            events: Vec::new(),
        }
        .into_test_result()
        .expect("runner");
        assert_eq!(result.error_type(), Some("Error"));
        assert_eq!(result.error_message(), Some(""));
    }

    // 1. step success

    #[tokio::test]
    async fn step_success_returns_output_and_records_op() {
        let result = LocalRunner::new()
            .run(
                |n: i32, ctx: DurableContext| async move {
                    let v = ctx
                        .step(move |_| async move { Ok(n + 1) })
                        .name("inc")
                        .await?;
                    Ok::<_, BoxError>(v)
                },
                41_i32,
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&42));
        assert_eq!(result.invocation_count(), 1);
        assert_eq!(result.operations().len(), 1);
        assert!(result.operations()[0].succeeded());
        assert_eq!(result.operations()[0].name(), Some("inc"));
    }

    // 1. step failure

    #[tokio::test]
    async fn step_failure_propagates_and_records_failed_op() {
        // A strategy that never retries so the failure is permanent.
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let strategy = |_e: &StepError, _a: u32| RetryDecision::Stop;
                    let v: i32 = ctx
                        .step(|_| async { Err("boom".into()) })
                        .retry_strategy(strategy)
                        .await?;
                    Ok::<_, BoxError>(v)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_failure());
        // The execution record re-records the step failure's own recorded
        // identity: a plain boxed error carries no user type, so the step
        // record's generic "Error" passes through the boundary unchanged
        // (the "StepError" registry name is only the fallback for an
        // operation error with no attached wire identity).
        assert_eq!(result.error_type(), Some("Error"));
        assert!(result.operations().iter().any(TestOperation::failed));
    }

    // 1. step retry via RetryStrategyConfig

    #[tokio::test]
    async fn step_retry_config_drives_retries_to_success() {
        // A config-based strategy (no hand-written closure): 3 total
        // attempts, 1s deterministic delays. The step fails on attempts 1
        // and 2 and succeeds on attempt 3, exercising the config through
        // real suspend/replay cycles.
        let attempts = Arc::new(AtomicU32::new(0));
        let attempts_seen = Arc::clone(&attempts);

        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let attempts_seen = Arc::clone(&attempts_seen);
                    async move {
                        let attempts_seen = Arc::clone(&attempts_seen);
                        let config = crate::builders::RetryStrategyConfig::builder()
                            .max_attempts(3)
                            .initial_delay(std::time::Duration::from_secs(1))
                            .max_delay(std::time::Duration::from_secs(1))
                            .jitter(crate::builders::JitterStrategy::None)
                            .build();
                        let v: i32 = ctx
                            .step(move |sc| {
                                let attempts_seen = Arc::clone(&attempts_seen);
                                async move {
                                    attempts_seen.fetch_add(1, Ordering::SeqCst);
                                    if sc.attempt() < 3 {
                                        Err(format!("attempt {} failed", sc.attempt()).into())
                                    } else {
                                        Ok(7)
                                    }
                                }
                            })
                            .name("flaky-config")
                            .retry_strategy_config(config)
                            .await?;
                        Ok::<_, BoxError>(v)
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&7));
        // Body executed exactly 3 times (attempts 1, 2, 3).
        assert_eq!(attempts.load(Ordering::SeqCst), 3);
        assert!(result.invocation_count() >= 3);
    }

    #[tokio::test]
    async fn step_retry_config_max_attempts_exhausts_and_fails() {
        // max_attempts(2): the body runs exactly twice, then the error
        // propagates instead of retrying a third time.
        let attempts = Arc::new(AtomicU32::new(0));
        let attempts_seen = Arc::clone(&attempts);

        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let attempts_seen = Arc::clone(&attempts_seen);
                    async move {
                        let attempts_seen = Arc::clone(&attempts_seen);
                        let config = crate::builders::RetryStrategyConfig::builder()
                            .max_attempts(2)
                            .initial_delay(std::time::Duration::from_secs(1))
                            .jitter(crate::builders::JitterStrategy::None)
                            .build();
                        let v: i32 = ctx
                            .step(move |_| {
                                let attempts_seen = Arc::clone(&attempts_seen);
                                async move {
                                    attempts_seen.fetch_add(1, Ordering::SeqCst);
                                    Err("always fails".into())
                                }
                            })
                            .name("always-fails")
                            .retry_strategy_config(config)
                            .await?;
                        Ok::<_, BoxError>(v)
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_failure());
        assert_eq!(attempts.load(Ordering::SeqCst), 2);
        assert!(result.operations().iter().any(TestOperation::failed));
    }

    /// A test error type the `retry_if` `LocalRunner` tests match by type.
    #[derive(Debug)]
    struct Transient(u32);

    impl std::fmt::Display for Transient {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "transient on attempt {}", self.0)
        }
    }

    impl std::error::Error for Transient {}

    /// Describes an awaited step error: `Step(ExecutionFailed)` plus the
    /// source's message, or the error itself for any other shape.
    fn describe_step_failure(err: &crate::OperationError) -> String {
        match err.kind() {
            crate::OperationErrorKind::Step(step) => match step.kind() {
                crate::StepErrorKind::ExecutionFailed => format!(
                    "ExecutionFailed: {}",
                    std::error::Error::source(step).map_or_else(String::new, ToString::to_string)
                ),
                other => format!("other step kind: {other}"),
            },
            _ => format!("not a step error: {err:#}"),
        }
    }

    #[tokio::test]
    async fn step_retry_if_false_fails_after_one_attempt() {
        // A predicate that rejects every error stops at the first failure,
        // although max_attempts(3) would allow two retries.
        let attempts = Arc::new(AtomicU32::new(0));
        let attempts_seen = Arc::clone(&attempts);

        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let attempts_seen = Arc::clone(&attempts_seen);
                    async move {
                        let config = crate::builders::RetryStrategyConfig::builder()
                            .max_attempts(3)
                            .retry_if(|_| false)
                            .build();
                        let outcome = ctx
                            .step(move |_| {
                                let attempts_seen = Arc::clone(&attempts_seen);
                                async move {
                                    attempts_seen.fetch_add(1, Ordering::SeqCst);
                                    Err::<i32, BoxError>("always fails".into())
                                }
                            })
                            .name("filtered")
                            .retry_strategy_config(config)
                            .await;
                        let err = outcome.expect_err("the step fails");
                        Ok::<_, BoxError>(describe_step_failure(&err))
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            result.output().map(String::as_str),
            Some("ExecutionFailed: always fails")
        );
        assert_eq!(attempts.load(Ordering::SeqCst), 1);
        let op = result.operation("filtered").expect("the step is recorded");
        assert_eq!(op.status(), OperationStatus::Failed);
    }

    #[tokio::test]
    async fn step_retry_if_retries_matching_error_until_cap() {
        // A predicate that accepts `Transient` retries it until the cap,
        // and the awaited error carries the third attempt's `Transient`.
        let attempts = Arc::new(AtomicU32::new(0));
        let attempts_seen = Arc::clone(&attempts);

        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let attempts_seen = Arc::clone(&attempts_seen);
                    async move {
                        let config = crate::builders::RetryStrategyConfig::builder()
                            .max_attempts(3)
                            .initial_delay(std::time::Duration::from_secs(1))
                            .jitter(crate::builders::JitterStrategy::None)
                            .retry_if(|err| {
                                std::error::Error::source(err)
                                    .and_then(|e| e.downcast_ref::<Transient>())
                                    .is_some()
                            })
                            .build();
                        let outcome = ctx
                            .step(move |sc| {
                                let attempts_seen = Arc::clone(&attempts_seen);
                                async move {
                                    attempts_seen.fetch_add(1, Ordering::SeqCst);
                                    Err::<i32, BoxError>(Box::new(Transient(sc.attempt())))
                                }
                            })
                            .name("transient")
                            .retry_strategy_config(config)
                            .await;
                        let err = outcome.expect_err("the step fails");
                        // On the live path the source is the third
                        // attempt's own `Transient`.
                        let third = match err.kind() {
                            crate::OperationErrorKind::Step(step) => {
                                std::error::Error::source(step)
                                    .and_then(|e| e.downcast_ref::<Transient>())
                                    .map(|t| t.0)
                            }
                            _ => None,
                        };
                        Ok::<_, BoxError>((describe_step_failure(&err), third))
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(attempts.load(Ordering::SeqCst), 3);
        assert_eq!(
            result.output(),
            Some(&(
                "ExecutionFailed: transient on attempt 3".to_owned(),
                Some(3)
            ))
        );
        let op = result.operation("transient").expect("the step is recorded");
        assert_eq!(op.status(), OperationStatus::Failed);
    }

    #[tokio::test]
    async fn wait_for_callback_submitter_retry_if() {
        // A submitter that fails with "boom" and a predicate that rejects
        // it: the submitter runs once and the operation fails.
        let submits = Arc::new(AtomicU32::new(0));
        let submits_seen = Arc::clone(&submits);

        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let submits_seen = Arc::clone(&submits_seen);
                    async move {
                        let config = crate::builders::RetryStrategyConfig::builder()
                            .max_attempts(3)
                            .initial_delay(std::time::Duration::from_secs(1))
                            .jitter(crate::builders::JitterStrategy::None)
                            .retry_if(|_| false)
                            .build();
                        let outcome = ctx
                            .wait_for_callback::<u32>(move |_step_ctx, _callback_id| {
                                let submits_seen = Arc::clone(&submits_seen);
                                async move {
                                    if submits_seen.fetch_add(1, Ordering::SeqCst) == 0 {
                                        Err::<(), BoxError>("boom".into())
                                    } else {
                                        Ok(())
                                    }
                                }
                            })
                            .name("approval")
                            .submitter_retry_config(config)
                            .await;
                        Ok::<_, BoxError>(outcome.is_err())
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&true), "the operation fails");
        assert_eq!(submits.load(Ordering::SeqCst), 1);
        let op = result
            .operation("approval")
            .expect("the operation is recorded");
        assert_eq!(op.status(), OperationStatus::Failed);
    }

    #[tokio::test]
    async fn with_retry_retry_if_matches_recorded_message() {
        // Attempt 1 fails with "transient" and attempt 2 succeeds. The
        // predicate reads the attempt's record through `wire()`, so it
        // answers the same live and when replay asks it again about
        // attempt 1 after the backoff suspension.
        let block_runs = Arc::new(AtomicU32::new(0));
        let block_runs_seen = Arc::clone(&block_runs);
        let answers = Arc::new(Mutex::new(Vec::new()));
        let answers_seen = Arc::clone(&answers);

        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let block_runs_seen = Arc::clone(&block_runs_seen);
                    let answers_seen = Arc::clone(&answers_seen);
                    async move {
                        let config = crate::builders::RetryStrategyConfig::builder()
                            .max_attempts(3)
                            .initial_delay(std::time::Duration::from_secs(1))
                            .jitter(crate::builders::JitterStrategy::None)
                            .retry_if(move |err| {
                                let matched = std::error::Error::source(err)
                                    .and_then(|e| e.downcast_ref::<crate::OperationError>())
                                    .and_then(crate::OperationError::wire)
                                    .and_then(crate::WireError::error_message)
                                    .is_some_and(|message| message.contains("transient"));
                                answers_seen.lock().expect("answers").push(matched);
                                matched
                            })
                            .build();
                        let value = ctx
                            .with_retry(move |_child, _attempt| {
                                let block_runs_seen = Arc::clone(&block_runs_seen);
                                async move {
                                    if block_runs_seen.fetch_add(1, Ordering::SeqCst) == 0 {
                                        Err::<u32, BoxError>("transient".into())
                                    } else {
                                        Ok(9)
                                    }
                                }
                            })
                            .name("block")
                            .retry_strategy_config(config)
                            .await?;
                        Ok::<_, BoxError>(value)
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&9));
        assert_eq!(block_runs.load(Ordering::SeqCst), 2, "two attempts ran");
        assert!(result.operation("attempt-1").expect("attempt 1").failed());
        assert!(
            result
                .operation("attempt-2")
                .expect("attempt 2")
                .succeeded()
        );
        assert!(result.invocation_count() >= 2, "the backoff suspended");
        let answers = answers.lock().expect("answers").clone();
        assert!(
            answers.len() >= 2,
            "the predicate is asked live and again on replay: {answers:?}"
        );
        assert!(answers.iter().all(|matched| *matched), "{answers:?}");
    }

    // 1. step retry (retry strategy honored)

    #[tokio::test]
    async fn step_retry_succeeds_on_third_attempt() {
        // Fails on attempts 1 and 2, succeeds on attempt 3. Each retry
        // suspends and the runner elapses the retry delay before re-invoking.
        let attempts = Arc::new(AtomicU32::new(0));
        let attempts_seen = Arc::clone(&attempts);

        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let attempts_seen = Arc::clone(&attempts_seen);
                    async move {
                        let attempts_seen = Arc::clone(&attempts_seen);
                        let strategy = |_e: &StepError, attempt: u32| {
                            if attempt >= 3 {
                                RetryDecision::Stop
                            } else {
                                RetryDecision::retry(std::time::Duration::from_secs(1))
                            }
                        };
                        let v: i32 = ctx
                            .step(move |sc| {
                                let attempts_seen = Arc::clone(&attempts_seen);
                                async move {
                                    attempts_seen.fetch_add(1, Ordering::SeqCst);
                                    if sc.attempt() < 3 {
                                        Err(format!("attempt {} failed", sc.attempt()).into())
                                    } else {
                                        Ok(99)
                                    }
                                }
                            })
                            .name("flaky")
                            .retry_strategy(strategy)
                            .await?;
                        Ok::<_, BoxError>(v)
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&99));
        // Body executed exactly 3 times (attempts 1, 2, 3).
        assert_eq!(attempts.load(Ordering::SeqCst), 3);
        // The step took multiple invocations (one per retry suspension).
        assert!(result.invocation_count() >= 3);
        let step = result
            .operations()
            .iter()
            .find(|o| o.name() == Some("flaky"))
            .expect("flaky step recorded");
        assert!(step.succeeded());
        assert_eq!(
            step_attempt(step),
            Some(2),
            "backend recorded 2 completed retries"
        );
    }

    // 2. wait then resume across invocations

    #[tokio::test]
    async fn wait_resumes_across_invocations() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let before = ctx
                        .step(|_| async { Ok("before".to_owned()) })
                        .name("before")
                        .await?;
                    ctx.wait(std::time::Duration::from_mins(5))
                        .name("nap")
                        .await?;
                    let after = ctx
                        .step(|_| async { Ok("after".to_owned()) })
                        .name("after")
                        .await?;
                    Ok::<_, BoxError>(format!("{before}-{after}"))
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output().map(String::as_str), Some("before-after"));
        // The wait forced at least a second invocation.
        assert!(result.invocation_count() >= 2);
        let wait_op = result
            .operations()
            .iter()
            .find(|o| o.kind() == OperationKind::Wait)
            .expect("wait op recorded");
        assert!(wait_op.succeeded());
    }

    // 3. wait_for_condition state carry (exercises the merge-back clobber)

    #[derive(Clone, Serialize, Deserialize)]
    struct Counter {
        count: u32,
    }

    #[tokio::test]
    async fn wait_for_condition_carries_state_across_attempts() {
        // The check increments the carried count each attempt; the strategy
        // completes once count reaches 3. If the carried state were clobbered
        // to initial_state each attempt (the pre-554ac90 bug), count would
        // never advance and the runner would exhaust its invocation budget.
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let strategy = |state: Counter, _attempt: u32| {
                        if state.count >= 3 {
                            crate::builders::wait_for_condition::WaitDecision::complete()
                        } else {
                            crate::builders::wait_for_condition::WaitDecision::continue_with(
                                std::time::Duration::from_secs(1),
                            )
                        }
                    };
                    let final_state = ctx
                        .wait_for_condition(
                            |_sc, state: Counter| async move {
                                Ok(Counter {
                                    count: state.count + 1,
                                })
                            },
                            Counter { count: 0 },
                        )
                        .name("poll")
                        .wait_strategy_fn(strategy)
                        .await?;
                    Ok::<_, BoxError>(final_state.count)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            result.output(),
            Some(&3),
            "carried state must advance across attempts (merge-back clobber must not reset it)"
        );
    }

    /// A `Serdes<String>` whose `serialize` upper-cases the string and whose
    /// `deserialize` is the identity.
    struct UpperSerdes;

    impl crate::Serdes<String> for UpperSerdes {
        // reason: a test serdes that completes at once, in the async-fn form user code writes
        #[expect(clippy::unused_async_trait_impl)]
        async fn serialize(
            &self,
            value: String,
            _context: crate::serdes::SerdesContext,
        ) -> Result<String, BoxError> {
            Ok(value.to_uppercase())
        }

        // reason: a test serdes that completes at once, in the async-fn form user code writes
        #[expect(clippy::unused_async_trait_impl)]
        async fn deserialize(
            &self,
            wire: String,
            _context: crate::serdes::SerdesContext,
        ) -> Result<String, BoxError> {
            Ok(wire)
        }
    }

    #[tokio::test]
    async fn wfc_first_check_sees_round_tripped_initial_state() {
        // The first check receives the initial state after a serdes round
        // trip, as JS does, so `UpperSerdes` shows it "A", not "a".
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let seen = Arc::new(Mutex::new(None::<String>));
                    let seen_check = Arc::clone(&seen);
                    ctx.wait_for_condition(
                        move |_sc, state: String| {
                            let seen = Arc::clone(&seen_check);
                            async move {
                                *seen.lock().expect("lock") = Some(state.clone());
                                Ok(state)
                            }
                        },
                        "a".to_owned(),
                    )
                    .wait_strategy_fn(|_state, _attempt| {
                        crate::builders::wait_for_condition::WaitDecision::complete()
                    })
                    .name("poll")
                    .serdes(UpperSerdes)
                    .await?;
                    let received = seen.lock().expect("lock").clone();
                    Ok::<_, BoxError>(received)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&Some("A".to_owned())));
    }

    #[tokio::test]
    async fn wait_strategy_config_completes_on_predicate() {
        // The config-derived strategy (issue #35) completes once its
        // completion predicate matches the state the check produced.
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let final_state = ctx
                        .wait_for_condition(
                            |_sc, state: Counter| async move {
                                Ok(Counter {
                                    count: state.count + 1,
                                })
                            },
                            Counter { count: 0 },
                        )
                        .name("poll")
                        .wait_strategy(
                            crate::builders::wait_for_condition::WaitStrategy::builder(
                                |state: &Counter| state.count >= 3,
                            )
                            .initial_delay(std::time::Duration::from_secs(1))
                            .jitter(crate::builders::JitterStrategy::None)
                            .build(),
                        )
                        .await?;
                    Ok::<_, BoxError>(final_state.count)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            result.output(),
            Some(&3),
            "the operation must complete when the predicate first matches"
        );
    }

    #[tokio::test]
    async fn wait_strategy_config_exhausts_at_max_attempts() {
        // A predicate that never matches must exhaust at max_attempts with
        // a MaxChecksExceeded failure, not poll forever.
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let final_state = ctx
                        .wait_for_condition(
                            |_sc, state: Counter| async move {
                                Ok(Counter {
                                    count: state.count + 1,
                                })
                            },
                            Counter { count: 0 },
                        )
                        .name("never-ready")
                        .wait_strategy(
                            crate::builders::wait_for_condition::WaitStrategy::builder(
                                |_state: &Counter| false,
                            )
                            .max_attempts(2)
                            .initial_delay(std::time::Duration::from_secs(1))
                            .jitter(crate::builders::JitterStrategy::None)
                            .build(),
                        )
                        .await?;
                    Ok::<_, BoxError>(final_state.count)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(
            !result.is_success(),
            "an unmatched predicate must exhaust, not succeed"
        );
        // The execution records the condition's recorded message.
        assert_eq!(
            result.error_message(),
            Some("max attempts exceeded: 2 checks completed (max_attempts = 2)"),
            "failure must name exhaustion with the attempt count"
        );
    }

    /// REGRESSION TEST: exhaustion keeps its `MaxChecksExceeded`
    /// classification across a terminal replay.
    ///
    /// The handler catches the exhaustion error and then suspends on a
    /// wait, so the next invocation replays the failed operation from its
    /// checkpoint. Both the live catch and the replayed catch must
    /// classify as `MaxChecksExceeded` with the same `checks()` value:
    /// the returned output comes from the final (replaying) invocation,
    /// so a replay misclassified as `CheckFailed` fails the execution.
    #[tokio::test]
    async fn wait_strategy_exhaustion_classification_survives_replay() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let outcome = ctx
                        .wait_for_condition(
                            |_sc, state: Counter| async move {
                                Ok(Counter {
                                    count: state.count + 1,
                                })
                            },
                            Counter { count: 0 },
                        )
                        .name("never-ready")
                        .wait_strategy(
                            crate::builders::wait_for_condition::WaitStrategy::builder(
                                |_state: &Counter| false,
                            )
                            .max_attempts(2)
                            .initial_delay(std::time::Duration::from_secs(1))
                            .jitter(crate::builders::JitterStrategy::None)
                            .build(),
                        )
                        .await;

                    let Err(err) = outcome else {
                        return Err("expected exhaustion".into());
                    };
                    let checks = match err.kind() {
                        crate::OperationErrorKind::WaitForCondition(wfc) => match wfc.kind() {
                            crate::WaitForConditionErrorKind::MaxChecksExceeded(details) => {
                                details.checks()
                            }
                            other => {
                                return Err(
                                    format!("expected MaxChecksExceeded, got: {other:?}").into()
                                );
                            }
                        },
                        other => {
                            return Err(
                                format!("expected WaitForCondition error, got: {other:?}").into()
                            );
                        }
                    };

                    // Suspend so a later invocation replays the failed
                    // operation as a frozen terminal outcome.
                    ctx.wait(std::time::Duration::from_secs(1)).await?;
                    Ok::<_, BoxError>(checks)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            result.output(),
            Some(&2),
            "the replayed error must report the same check count as the live one"
        );
        assert!(
            result.invocation_count() >= 3,
            "the exhaustion must have been replayed at least once (got {} invocations)",
            result.invocation_count()
        );
    }

    // 4. child contexts

    #[tokio::test]
    async fn child_context_returns_nested_result() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let v = ctx
                        .run_in_child_context(|child| async move {
                            let a = child.step(|_| async { Ok(10) }).await?;
                            let b = child.step(|_| async { Ok(20) }).await?;
                            Ok(a + b)
                        })
                        .name("branch")
                        .await?;
                    Ok::<_, BoxError>(v)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&30));
        // Parent context op + two child steps recorded.
        assert!(
            result
                .operations()
                .iter()
                .any(|o| o.kind() == OperationKind::Context)
        );
        let step_count = result
            .operations()
            .iter()
            .filter(|o| o.kind() == OperationKind::Step)
            .count();
        assert_eq!(step_count, 2);
    }

    // 5. parallel and map basics

    #[tokio::test]
    async fn parallel_runs_branches_and_collects_results() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let branches = vec![
                        crate::Branch::new("a", |c: DurableContext| async move {
                            let v = c.step(|_| async { Ok(1) }).await?;
                            Ok(v)
                        }),
                        crate::Branch::new("b", |c: DurableContext| async move {
                            let v = c.step(|_| async { Ok(2) }).await?;
                            Ok(v)
                        }),
                    ];
                    let results: Vec<i32> = ctx.parallel(branches).name("fan").await?;
                    Ok::<_, BoxError>(results.into_iter().sum::<i32>())
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&3));
    }

    #[tokio::test]
    async fn map_applies_over_items() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let items = vec![1_i32, 2, 3];
                    let out: Vec<i32> = ctx
                        .map(items, |child, item, _idx| async move {
                            let scaled = child.step(move |_| async move { Ok(item * 10) }).await?;
                            Ok(scaled)
                        })
                        .name("scale")
                        .max_concurrency(2)
                        .completion(CompletionConfig::default())
                        .await?;
                    Ok::<_, BoxError>(out.into_iter().sum::<i32>())
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&60));
    }

    /// Replay of a predicate-completed batch is stable: the execution
    /// suspends mid-batch (a timer inside an item), resumes on a later
    /// invocation, replays the already-settled item from its checkpoint
    /// record without re-running its body, and the custom predicate then
    /// ends the batch early with the `CUSTOM_COMPLETION_SUCCEEDED` reason.
    #[tokio::test]
    async fn map_completion_predicate_survives_replay() {
        let item0_runs = Arc::new(AtomicU32::new(0));
        let item0_runs_handle = Arc::clone(&item0_runs);

        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let item0_runs = Arc::clone(&item0_runs_handle);
                    async move {
                        let batch = ctx
                            .map(vec![10_u32, 20, 30], move |child, item, idx| {
                                let item0_runs = Arc::clone(&item0_runs);
                                async move {
                                    if idx == 1 {
                                        // Suspends the batch mid-run, forcing a
                                        // second invocation that replays item 0.
                                        child
                                            .wait(std::time::Duration::from_secs(1))
                                            .name("stall")
                                            .await?;
                                    }
                                    let value = child
                                        .step(move |_| async move {
                                            if idx == 0 {
                                                item0_runs.fetch_add(1, Ordering::SeqCst);
                                            }
                                            Ok(item)
                                        })
                                        .name("work")
                                        .await?;
                                    Ok(value)
                                }
                            })
                            .name("predicated")
                            .max_concurrency(1)
                            .completion(
                                CompletionConfig::builder()
                                    .completion_predicate(|stats| {
                                        if stats.settled() >= 2 {
                                            CompletionDecision::complete(
                                                CompletionOutcome::Succeeded,
                                            )
                                        } else {
                                            CompletionDecision::Continue
                                        }
                                    })
                                    .build()?,
                            )
                            .await_batch()
                            .await?;
                        Ok::<_, BoxError>(format!(
                            "{}:{}",
                            batch.reason.as_str(),
                            batch.items.len()
                        ))
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        // The predicate fired after two settles; item 2 never started.
        assert_eq!(
            result.output(),
            Some(&"CUSTOM_COMPLETION_SUCCEEDED:2".to_owned())
        );
        // The timer forced at least one suspension, so the batch spanned
        // several invocations and item 0 was replayed at least once.
        assert!(
            result.invocation_count() >= 2,
            "expected a replay, got {} invocation(s)",
            result.invocation_count()
        );
        // Replay returned item 0's recorded result instead of re-running
        // its step body.
        assert_eq!(
            item0_runs.load(Ordering::SeqCst),
            1,
            "item 0's step body must run exactly once across replays"
        );
    }

    /// Completion-trigger evaluation is deterministic under replay for a
    /// CONCURRENT batch: on resume, recorded-terminal children settle
    /// first, before any resumed (live) branch joins.
    ///
    /// Three items at `max_concurrency(2)`: items 0 and 2 succeed on the
    /// first invocation, item 1 parks on a timer and fails after resume.
    /// The predicate `failed() > succeeded()` must never fire: on the
    /// resumed invocation the two recorded successes settle before item 1's
    /// live failure joins, so the predicate sees 2 succeeded before it sees
    /// 1 failed, regardless of how the scheduler orders the join events. If
    /// resumed item 1's failure could join before item 0's recorded success,
    /// the predicate would see 1 failed / 0 succeeded and stop the batch,
    /// and item 2, whose success is already in the checkpoint log, would be
    /// dropped from the result.
    #[tokio::test]
    async fn map_completion_predicate_replay_order_is_canonical() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let batch = ctx
                        .map(vec![10_u32, 20, 30], |child, item, idx| async move {
                            if idx == 1 {
                                // Parks the branch, forcing a second
                                // invocation where items 0 and 2 are
                                // recorded-terminal while item 1 resumes
                                // live, and then fails.
                                child
                                    .wait(std::time::Duration::from_secs(1))
                                    .name("stall")
                                    .await?;
                                return Err("item 1 fails after resume".into());
                            }
                            Ok(item)
                        })
                        .name("order-sensitive")
                        .max_concurrency(2)
                        .completion(
                            CompletionConfig::builder()
                                // A predicate-only config: only the
                                // predicate could stop early.
                                .completion_predicate(|stats| {
                                    if stats.failed() > stats.succeeded() {
                                        CompletionDecision::complete(CompletionOutcome::Succeeded)
                                    } else {
                                        CompletionDecision::Continue
                                    }
                                })
                                .build()?,
                        )
                        .await_batch()
                        .await?;
                    Ok::<_, BoxError>(format!(
                        "{}:{}:{}:{}",
                        batch.reason.as_str(),
                        batch.items.len(),
                        batch.success_count(),
                        batch.failure_count()
                    ))
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        // All three items are in the result, the lone failure never
        // outnumbered the recorded successes, and the batch ran to
        // completion.
        assert_eq!(result.output(), Some(&"ALL_COMPLETED:3:2:1".to_owned()));
        // The timer forced a suspension, so the second invocation really did
        // mix recorded-terminal children with a resumed live child.
        assert!(
            result.invocation_count() >= 2,
            "expected a replay, got {} invocation(s)",
            result.invocation_count()
        );
    }

    /// Reversed live settlement order PLUS a genuine suspension: item 1
    /// succeeds before the delayed item 0 fails (both on the first
    /// invocation), item 2 parks and only succeeds after resume. The
    /// predicate `failed() > succeeded() + 1` stays false on every snapshot,
    /// live and on resume, so the batch runs to completion with every
    /// item's outcome in the result, on both sides of the suspension
    /// boundary.
    #[tokio::test]
    async fn map_predicate_reversed_settlement_with_suspension_replays_stably() {
        let gate = Arc::new(tokio::sync::Notify::new());
        let gate_handle = Arc::clone(&gate);

        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let gate = Arc::clone(&gate_handle);
                    async move {
                        let batch = ctx
                            .map(vec![10_u32, 20, 30], move |child, item, idx| {
                                let gate = Arc::clone(&gate);
                                async move {
                                    match idx {
                                        0 => {
                                            // The gate and the yields are
                                            // not SDK operations: they run
                                            // under a held activity permit,
                                            // so a waiting sibling cannot
                                            // suspend the invocation here.
                                            child
                                                .hold_activity(async {
                                                    gate.notified().await;
                                                    for _ in 0..16 {
                                                        tokio::task::yield_now().await;
                                                    }
                                                })
                                                .await;
                                            Err("item 0 fails last".into())
                                        }
                                        1 => {
                                            gate.notify_one();
                                            Ok(item)
                                        }
                                        _ => {
                                            // Parks the batch mid-run,
                                            // forcing a resumed invocation
                                            // that replays items 0 and 1
                                            // from their records.
                                            child
                                                .wait(std::time::Duration::from_secs(1))
                                                .name("stall")
                                                .await?;
                                            Ok(item)
                                        }
                                    }
                                }
                            })
                            .name("reversed-then-suspend")
                            .max_concurrency(3)
                            .completion(
                                CompletionConfig::builder()
                                    // Never true here: one failure can never
                                    // outnumber the successes by two.
                                    .completion_predicate(|stats| {
                                        if stats.failed() > stats.succeeded() + 1 {
                                            CompletionDecision::complete(
                                                CompletionOutcome::Succeeded,
                                            )
                                        } else {
                                            CompletionDecision::Continue
                                        }
                                    })
                                    .build()?,
                            )
                            .await_batch()
                            .await?;
                        Ok::<_, BoxError>(format!(
                            "{}:{}:{}:{}",
                            batch.reason.as_str(),
                            batch.items.len(),
                            batch.success_count(),
                            batch.failure_count()
                        ))
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        // No trigger fired on either side of the suspension: all three
        // outcomes, including recorded items replayed after the resume,
        // are in the result.
        assert_eq!(result.output(), Some(&"ALL_COMPLETED:3:2:1".to_owned()));
        // The timer forced a suspension, so the batch really did span a
        // replay of the reversed-order recorded outcomes.
        assert!(
            result.invocation_count() >= 2,
            "expected a replay, got {} invocation(s)",
            result.invocation_count()
        );
    }

    // An arbitrary user error returned as BoxError from a child closure
    // surfaces with its message intact through the operation's error type,
    // with no error-conversion ceremony at the boundary.
    #[tokio::test]
    async fn child_closure_boxerror_message_propagates() {
        #[derive(Debug)]
        struct CustomBoundaryError;
        impl std::fmt::Display for CustomBoundaryError {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "custom-boundary-failure")
            }
        }
        impl std::error::Error for CustomBoundaryError {}

        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let v: i32 = ctx
                        .run_in_child_context(|_child| async move {
                            // Plain `?`-free early return of an arbitrary error;
                            // no map_err, no ChildFnError.
                            Err(CustomBoundaryError)?;
                            Ok(0)
                        })
                        .name("boundary")
                        .await?;
                    Ok::<_, BoxError>(v)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(
            !result.is_success(),
            "child failure must surface as an error"
        );
        let msg = result.error_message().unwrap_or_default();
        assert!(
            msg.contains("custom-boundary-failure"),
            "user error message must survive the boundary: {msg}"
        );
    }

    // 6. callback replies

    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Approval {
        approved: bool,
    }

    /// `success(&v)` in the issue's test table.
    fn success<T: Serialize>(value: &T) -> CallbackReply {
        CallbackReply::success(value).expect("encodes")
    }

    /// The single callback record of `result`.
    fn callback_record<O>(result: &TestResult<O>) -> &TestOperation {
        result
            .operations()
            .iter()
            .find(|o| o.kind() == OperationKind::Callback)
            .expect("callback op recorded")
    }

    #[tokio::test]
    async fn callback_reply_success_delivers_json() {
        let result = LocalRunner::new()
            .callback(success(&Approval { approved: true }))
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<Approval>().name("approval").await?;
                    let approval = cb.result().await?;
                    Ok::<_, BoxError>(approval.approved)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&true));
        assert!(result.invocation_count() >= 2);
    }

    #[tokio::test]
    async fn callback_reply_timeout_surfaces_error() {
        let result = LocalRunner::new()
            .callback(CallbackReply::timeout())
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<Approval>().name("approval").await?;
                    let approval = cb.result().await?;
                    Ok::<_, BoxError>(approval.approved)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_failure());
        assert_eq!(result.error_type(), Some("CallbackError"));
        let cb_op = callback_record(&result);
        assert_eq!(cb_op.status(), OperationStatus::TimedOut);
        assert_eq!(cb_op.error_message(), Some("Callback timed out"));
    }

    #[tokio::test]
    async fn callback_reply_raw_is_not_encoded() {
        let result = LocalRunner::new()
            .callback(CallbackReply::raw("\"hi\""))
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<String>().await?;
                    Ok::<_, BoxError>(cb.result().await?)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output().map(String::as_str), Some("hi"));
    }

    /// The fields a handler reads from a callback failure: whether the kind
    /// is an external failure, then the wire type, message and data.
    type FailureFields = (bool, Option<String>, Option<String>, Option<String>);

    #[tokio::test]
    async fn callback_reply_failure_reaches_create_callback() {
        let result = LocalRunner::new()
            .callback(CallbackReply::failure(
                CallbackFailure::new("Declined", "no").with_error_data("{\"code\":7}"),
            ))
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<String>().name("cb").await?;
                    let Err(err) = cb.result().await else {
                        return Err::<FailureFields, BoxError>("callback succeeded".into());
                    };
                    let external = matches!(
                        err.kind(),
                        crate::OperationErrorKind::Callback(e)
                            if matches!(e.kind(), crate::CallbackErrorKind::ExternalFailure)
                    );
                    let wire = err.wire();
                    let field = |read: fn(&crate::WireError) -> Option<&str>| {
                        wire.and_then(read).map(ToOwned::to_owned)
                    };
                    Ok((
                        external,
                        field(crate::WireError::error_type),
                        field(crate::WireError::error_message),
                        field(crate::WireError::error_data),
                    ))
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            result.output(),
            Some(&(
                true,
                Some("Declined".to_owned()),
                Some("no".to_owned()),
                Some("{\"code\":7}".to_owned()),
            ))
        );
        let cb_op = callback_record(&result);
        assert_eq!(cb_op.status(), OperationStatus::Failed);
        assert_eq!(cb_op.error_type(), Some("Declined"));
        assert_eq!(cb_op.error_message(), Some("no"));
    }

    #[tokio::test]
    async fn callback_reply_failure_reaches_wait_for_callback() {
        let result = LocalRunner::new()
            .callback(CallbackReply::failure(CallbackFailure::new(
                "Declined", "no",
            )))
            .run(
                |(), ctx: DurableContext| async move {
                    let outcome = ctx
                        .wait_for_callback::<String>(|_step_ctx, _callback_id| async { Ok(()) })
                        .name("approve")
                        .await;
                    let Err(err) = outcome else {
                        return Err::<bool, BoxError>("wait_for_callback succeeded".into());
                    };
                    Ok(matches!(
                        err.kind(),
                        crate::OperationErrorKind::Callback(e)
                            if matches!(e.kind(), crate::CallbackErrorKind::ExternalFailure)
                    ))
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            result.output(),
            Some(&true),
            "kind is Callback(ExternalFailure)"
        );
        let cb_op = callback_record(&result);
        assert_eq!(cb_op.status(), OperationStatus::Failed);
        assert_eq!(cb_op.error_type(), Some("Declined"));
    }

    /// What a `wait_for_callback` handler saw in one invocation: the
    /// `CallbackErrorKind` name, then the wire type, message and data.
    type WfcbObservation = (String, Option<String>, Option<String>, Option<String>);

    /// How the submitter of [`run_wfcb_classification`] ends.
    #[derive(Clone, Copy)]
    enum Submitter {
        /// It succeeds.
        Succeeds,
        /// It fails with a plain error, `submission failed`.
        FailsUntyped,
        /// It fails with `TypedError::with_type("PaymentError", "declined")`.
        FailsTyped,
    }

    /// The run of a `wait_for_callback` named `approve` whose failure the
    /// handler observes live and on replay.
    struct WfcbClassification {
        result: TestResult<String>,
        /// One entry per invocation that read the failure.
        seen: Vec<WfcbObservation>,
        /// The `{:#}` text of the live error's source, when that source is
        /// a `Step(ExecutionFailed)` operation error.
        live_step_source: Option<String>,
    }

    /// Runs a handler that calls `wait_for_callback` and records the kind
    /// and wire fields of its error outside any step, so a replay records
    /// them again. When the kind is `expected`, the handler waits one
    /// second, so a later invocation replays the failure, and returns
    /// `output`; any other kind returns the error.
    async fn run_wfcb_classification(
        runner: LocalRunner,
        submitter: Submitter,
        expected: &'static str,
        output: &'static str,
    ) -> WfcbClassification {
        let seen: Arc<Mutex<Vec<WfcbObservation>>> = Arc::default();
        let live_source: Arc<Mutex<Option<String>>> = Arc::default();
        let (seen_in, live_in) = (Arc::clone(&seen), Arc::clone(&live_source));
        let result = runner
            .run(
                move |(), ctx: DurableContext| {
                    let (seen, live_source) = (Arc::clone(&seen_in), Arc::clone(&live_in));
                    async move {
                        let outcome = ctx
                            .wait_for_callback::<String>(move |_step_ctx, _id| async move {
                                match submitter {
                                    Submitter::Succeeds => Ok(()),
                                    Submitter::FailsUntyped => Err("submission failed".into()),
                                    Submitter::FailsTyped => Err(Box::new(
                                        crate::TypedError::with_type("PaymentError", "declined"),
                                    )
                                        as BoxError),
                                }
                            })
                            .name("approve")
                            .submitter_retry(|_, _| RetryDecision::Stop)
                            .await;
                        let Err(err) = outcome else {
                            return Err::<String, BoxError>("wait_for_callback succeeded".into());
                        };
                        let kind = match err.kind() {
                            crate::OperationErrorKind::Callback(e) => format!("{:?}", e.kind()),
                            other => format!("{other:?}"),
                        };
                        let field = |read: fn(&crate::WireError) -> Option<&str>| {
                            err.wire().and_then(read).map(ToOwned::to_owned)
                        };
                        {
                        let mut seen = seen.lock().expect("not poisoned");
                        if seen.is_empty() {
                            // The first invocation that reads the failure
                            // is the live one.
                            let step = std::error::Error::source(&err)
                                .and_then(std::error::Error::source)
                                .and_then(|s| s.downcast_ref::<crate::OperationError>())
                                .filter(|op| {
                                    matches!(
                                        op.kind(),
                                        crate::OperationErrorKind::Step(e)
                                            if matches!(e.kind(), crate::StepErrorKind::ExecutionFailed)
                                    )
                                });
                            *live_source.lock().expect("not poisoned") =
                                step.map(|op| format!("{op:#}"));
                        }
                        seen.push((
                            kind.clone(),
                            field(crate::WireError::error_type),
                            field(crate::WireError::error_message),
                            field(crate::WireError::error_data),
                        ));
                        }
                        if kind != expected {
                            return Err(err.into());
                        }
                        ctx.wait(std::time::Duration::from_secs(1)).await?;
                        Ok(output.to_owned())
                    }
                },
                (),
            )
            .await
            .expect("runner");
        let seen = seen.lock().expect("not poisoned").clone();
        let live_step_source = live_source.lock().expect("not poisoned").clone();
        WfcbClassification {
            result,
            seen,
            live_step_source,
        }
    }

    /// The recorded outer FAIL type of the `wait_for_callback` named
    /// `approve`.
    fn approve_error_type(result: &TestResult<String>) -> Option<&str> {
        result
            .operation("approve")
            .expect("the wait_for_callback is recorded")
            .error_type()
    }

    #[tokio::test]
    async fn wfcb_external_failure_same_kind_live_and_replay() {
        let runner = LocalRunner::new().callback(CallbackReply::failure(
            CallbackFailure::new("PaymentDeclined", "card declined")
                .with_error_data(r#"{"code":7}"#),
        ));
        let run =
            run_wfcb_classification(runner, Submitter::Succeeds, "ExternalFailure", "external")
                .await;
        assert!(run.result.is_success(), "{:?}", run.result.error_message());
        assert_eq!(run.result.output().map(String::as_str), Some("external"));
        let entry = (
            "ExternalFailure".to_owned(),
            Some("Callback.ExternalFailure".to_owned()),
            Some("card declined".to_owned()),
            Some(r#"{"code":7}"#.to_owned()),
        );
        assert_eq!(run.seen, [entry.clone(), entry]);
        assert_eq!(
            callback_record(&run.result).error_type(),
            Some("PaymentDeclined")
        );
        assert_eq!(
            approve_error_type(&run.result),
            Some("Callback.ExternalFailure")
        );
    }

    /// An external failure with no message, or an empty one, gives the
    /// outer `Callback.ExternalFailure` record the message
    /// `Callback failed`, live and on replay, and leaves the callback's
    /// own record unchanged.
    #[tokio::test]
    async fn wfcb_external_failure_without_message_same_kind_live_and_replay() {
        let cases = [
            (CallbackFailure::default(), None, None, None),
            (
                CallbackFailure::new("Declined", "").with_error_data("d"),
                Some("Declined"),
                Some(""),
                Some("d"),
            ),
        ];
        for (failure, stored_type, stored_message, data) in cases {
            let runner = LocalRunner::new().callback(CallbackReply::failure(failure));
            let run =
                run_wfcb_classification(runner, Submitter::Succeeds, "ExternalFailure", "external")
                    .await;
            assert!(run.result.is_success(), "{:?}", run.result.error_message());
            let entry = (
                "ExternalFailure".to_owned(),
                Some("Callback.ExternalFailure".to_owned()),
                Some("Callback failed".to_owned()),
                data.map(ToOwned::to_owned),
            );
            assert_eq!(run.seen, [entry.clone(), entry]);
            let stored = callback_record(&run.result);
            assert_eq!(stored.error_type(), stored_type);
            assert_eq!(stored.error_message(), stored_message);
            assert_eq!(stored.error().and_then(crate::WireError::error_data), data);
            let outer = run
                .result
                .operation("approve")
                .expect("the wait_for_callback is recorded");
            assert_eq!(outer.error_type(), Some("Callback.ExternalFailure"));
            assert_eq!(outer.error_message(), Some("Callback failed"));
        }
    }

    #[tokio::test]
    async fn wfcb_submitter_failure_same_kind_live_and_replay() {
        // The recorded message omits a `TypedError`'s name; the live
        // source's alternate `Display` keeps it.
        for (submitter, text, live_text) in [
            (
                Submitter::FailsUntyped,
                "submission failed",
                "operation error: step: execution failed: submission failed",
            ),
            (
                Submitter::FailsTyped,
                "declined",
                "operation error: step: execution failed: PaymentError: declined",
            ),
        ] {
            let run = run_wfcb_classification(
                LocalRunner::new(),
                submitter,
                "SubmitterFailed",
                "submitter",
            )
            .await;
            assert!(run.result.is_success(), "{:?}", run.result.error_message());
            let kinds: Vec<&str> = run.seen.iter().map(|e| e.0.as_str()).collect();
            assert_eq!(kinds, ["SubmitterFailed", "SubmitterFailed"], "{text}");
            assert_eq!(run.seen[0], run.seen[1], "{text}");
            assert_eq!(
                approve_error_type(&run.result),
                Some("Callback.SubmitterFailed")
            );
            let message = run
                .result
                .operation("approve")
                .and_then(TestOperation::error_message)
                .unwrap_or_default();
            assert_eq!(message, text);
            let live = run
                .live_step_source
                .expect("the live source is the step error");
            assert_eq!(live, live_text);
        }
    }

    #[tokio::test]
    async fn wfcb_timeout_same_kind_live_and_replay() {
        let runner = LocalRunner::new().callback(CallbackReply::timeout());
        let run =
            run_wfcb_classification(runner, Submitter::Succeeds, "TimedOut", "timed out").await;
        assert!(run.result.is_success(), "{:?}", run.result.error_message());
        let entry = (
            "TimedOut".to_owned(),
            Some("Callback.Timeout".to_owned()),
            Some("Callback timed out".to_owned()),
            None,
        );
        assert_eq!(run.seen, [entry.clone(), entry]);
        assert_eq!(approve_error_type(&run.result), Some("Callback.Timeout"));
        assert_eq!(
            run.result
                .operation("approve")
                .and_then(TestOperation::error_message),
            Some("Callback timed out")
        );
    }

    #[tokio::test]
    async fn wfcb_heartbeat_timeout_same_kind_live_and_replay() {
        let runner = LocalRunner::new().callback(CallbackReply::heartbeat_timeout());
        let run = run_wfcb_classification(
            runner,
            Submitter::Succeeds,
            "HeartbeatTimedOut",
            "heartbeat",
        )
        .await;
        assert!(run.result.is_success(), "{:?}", run.result.error_message());
        let entry = (
            "HeartbeatTimedOut".to_owned(),
            Some("Callback.Heartbeat".to_owned()),
            Some("Callback timed out on heartbeat".to_owned()),
            None,
        );
        assert_eq!(run.seen, [entry.clone(), entry]);
        assert_eq!(approve_error_type(&run.result), Some("Callback.Heartbeat"));
        assert_eq!(
            run.result
                .operation("approve")
                .and_then(TestOperation::error_message),
            Some("Callback timed out on heartbeat")
        );
    }

    #[tokio::test]
    async fn wfcb_unmarked_decode_failure_same_kind_live_and_replay() {
        let runner = LocalRunner::new().callback(CallbackReply::raw("not json"));
        let run = run_wfcb_classification(
            runner,
            Submitter::Succeeds,
            "DeserializationFailed",
            "undecodable",
        )
        .await;
        assert!(run.result.is_success(), "{:?}", run.result.error_message());
        let kinds: Vec<&str> = run.seen.iter().map(|e| e.0.as_str()).collect();
        assert_eq!(kinds, ["DeserializationFailed", "DeserializationFailed"]);
        assert_eq!(run.seen[0], run.seen[1]);
        // The outer context records the raw payload, byte for byte, and the
        // decode after it fails.
        let approve = run
            .result
            .operation("approve")
            .expect("the wait_for_callback is recorded");
        assert_eq!(approve.status(), OperationStatus::Succeeded);
        assert_eq!(approve.result(), Some("not json"));
        assert_eq!(approve.error_type(), None);
    }

    /// `PassthroughSerdes` on `wait_for_callback` returns the raw reply the
    /// external caller sent, which is not JSON.
    #[tokio::test]
    async fn wfcb_passthrough_reads_raw_reply() {
        let result = LocalRunner::new()
            .callback(CallbackReply::raw("approved"))
            .run(
                |(), ctx: DurableContext| async move {
                    let approval = ctx
                        .wait_for_callback::<String>(|_step_ctx, _id| async { Ok(()) })
                        .name("approve")
                        .serdes(crate::PassthroughSerdes)
                        .await?;
                    Ok::<_, BoxError>(approval)
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(result.output().map(String::as_str), Some("approved"));
    }

    #[tokio::test]
    async fn callback_for_targets_named_callbacks() {
        let result = LocalRunner::new()
            .callback_for("cb-a", CallbackReply::raw("\"A\""))
            .callback_for("cb-b", CallbackReply::raw("\"B\""))
            .run(
                |(), ctx: DurableContext| async move {
                    let b = ctx.create_callback::<String>().name("cb-b").await?;
                    let a = ctx.create_callback::<String>().name("cb-a").await?;
                    let a = a.result().await?;
                    let b = b.result().await?;
                    Ok::<_, BoxError>(format!("a={a} b={b}"))
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output().map(String::as_str), Some("a=A b=B"));
    }

    /// The recorded subtypes of rows 10 to 14: a step and a callback without
    /// a setter keep the built-in subtype, a custom subtype is recorded on
    /// the operation and on each of its events, and `wait_for_callback`
    /// keeps its three built-in subtypes.
    #[tokio::test]
    async fn step_and_callback_record_custom_sub_type() {
        use crate::{OperationSubType, StepContext};

        let result = LocalRunner::new()
            .callback_for("plain-cb", success(&1_u32))
            .callback_for("approval", success(&2_u32))
            .callback_for("wfcb", success(&3_u32))
            .run(
                |(), ctx: DurableContext| async move {
                    let plain = ctx.step(|_| async { Ok(1_u32) }).name("plain").await?;
                    let paid = ctx
                        .step(|step_ctx: StepContext| async move {
                            if step_ctx.attempt() == 1 {
                                Err::<u32, BoxError>("transient".into())
                            } else {
                                Ok(2)
                            }
                        })
                        .name("pay")
                        .sub_type(OperationSubType::new("Payment")?)
                        .retry_strategy(|_err, attempt| {
                            if attempt >= 2 {
                                RetryDecision::Stop
                            } else {
                                RetryDecision::retry(std::time::Duration::from_secs(1))
                            }
                        })
                        .await?;
                    let cb = ctx.create_callback::<u32>().name("plain-cb").await?;
                    let a = cb.result().await?;
                    let cb = ctx
                        .create_callback::<u32>()
                        .name("approval")
                        .sub_type(OperationSubType::new("Approval")?)
                        .await?;
                    let b = cb.result().await?;
                    let c = ctx
                        .wait_for_callback::<u32>(|_step_ctx, _callback_id| async { Ok(()) })
                        .name("wfcb")
                        .await?;
                    Ok::<_, BoxError>(plain + paid + a + b + c)
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&9));

        let op = |name: &str| result.operation(name).expect("the operation is recorded");
        // Row 10.
        assert_eq!(op("plain").sub_type(), Some("Step"));
        // Row 11: the step retried once, and the operation and every event
        // of it carry the custom subtype.
        let pay = op("pay");
        assert_eq!(pay.sub_type(), Some("Payment"));
        assert_eq!(step_attempt(pay), Some(1));
        assert!(pay.events().len() >= 3, "{:?}", pay.events());
        for event in pay.events() {
            assert_eq!(event.sub_type(), Some("Payment"), "{event:?}");
        }
        // Rows 12 and 13.
        assert_eq!(op("plain-cb").sub_type(), Some("Callback"));
        assert_eq!(op("approval").sub_type(), Some("Approval"));
        // Row 14.
        let wfcb = op("wfcb");
        assert_eq!(wfcb.sub_type(), Some("WaitForCallback"));
        let mut children: Vec<(OperationKind, Option<&str>)> = result
            .operations()
            .iter()
            .filter(|child| child.parent_id() == Some(wfcb.id()))
            .map(|child| (child.kind(), child.sub_type()))
            .collect();
        children.sort_by_key(|(kind, _)| kind.wire_str());
        assert_eq!(
            children,
            [
                (OperationKind::Callback, Some("Callback")),
                (OperationKind::Step, Some("Step"))
            ]
        );
    }

    /// The recorded subtypes of cases A to E: a child context and a
    /// `with_retry` block without a setter keep `RunInChildContext`, a
    /// custom subtype is recorded on a succeeded and on a failed child
    /// context, and on `with_retry` it labels the block only.
    #[tokio::test]
    async fn child_and_with_retry_record_custom_sub_type() {
        use crate::OperationSubType;

        let work_runs = Arc::new(AtomicU32::new(0));
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let work_runs = Arc::clone(&work_runs);
                    async move {
                        let checkout = || OperationSubType::new("Checkout");
                        // Case A.
                        let case_a = ctx
                            .run_in_child_context(|_child| async { Ok(1_u32) })
                            .name("case-a")
                            .await?;
                        // Case B.
                        let case_b = ctx
                            .run_in_child_context(|_child| async { Ok(2_u32) })
                            .name("case-b")
                            .sub_type(checkout()?)
                            .await?;
                        // Case C.
                        let case_c = ctx
                            .run_in_child_context(|_child| async {
                                Err::<u32, BoxError>("declined".into())
                            })
                            .name("case-c")
                            .sub_type(checkout()?)
                            .await;
                        assert!(case_c.is_err(), "case C must fail");
                        // Case D: the step fails on its first run, so attempt
                        // 1 fails and attempt 2 succeeds.
                        let case_d = ctx
                            .with_retry(move |child, _attempt| {
                                let work_runs = Arc::clone(&work_runs);
                                async move {
                                    let v = child
                                        .step(move |_| async move {
                                            if work_runs.fetch_add(1, Ordering::SeqCst) == 0 {
                                                Err::<u32, BoxError>("transient".into())
                                            } else {
                                                Ok(4)
                                            }
                                        })
                                        .name("work")
                                        .retry_strategy(|_err, _attempt| RetryDecision::Stop)
                                        .await?;
                                    Ok(v)
                                }
                            })
                            .name("block")
                            .sub_type(checkout()?)
                            .retry_strategy(|_err, attempt| {
                                if attempt >= 2 {
                                    RetryDecision::Stop
                                } else {
                                    RetryDecision::retry(std::time::Duration::from_secs(1))
                                }
                            })
                            .await?;
                        // Case E.
                        let case_e = ctx
                            .with_retry(|_child, _attempt| async { Ok(5_u32) })
                            .name("case-e")
                            .await?;
                        Ok::<_, BoxError>(case_a + case_b + case_d + case_e)
                    }
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&12));

        let op = |name: &str| result.operation(name).expect("the operation is recorded");
        assert_eq!(op("case-a").sub_type(), Some("RunInChildContext"));
        assert_eq!(op("case-b").sub_type(), Some("Checkout"));
        assert!(op("case-b").succeeded());
        let case_c_op = op("case-c");
        assert!(case_c_op.failed(), "{case_c_op:?}");
        assert_eq!(case_c_op.sub_type(), Some("Checkout"));
        let block = op("block");
        assert!(block.succeeded(), "{block:?}");
        assert_eq!(block.sub_type(), Some("Checkout"));
        assert!(op("attempt-1").failed());
        assert!(op("attempt-2").succeeded());
        assert_eq!(op("attempt-1").sub_type(), Some("RunInChildContext"));
        assert_eq!(op("attempt-2").sub_type(), Some("RunInChildContext"));
        assert_eq!(op("block-backoff-1").sub_type(), Some("Wait"));
        let works: Vec<Option<&str>> = result
            .operations()
            .iter()
            .filter(|o| o.name() == Some("work"))
            .map(TestOperation::sub_type)
            .collect();
        assert_eq!(works, [Some("Step"), Some("Step")]);
        assert_eq!(op("case-e").sub_type(), Some("RunInChildContext"));
    }

    #[tokio::test]
    async fn callback_for_targets_wait_for_callback_by_context_name() {
        let result = LocalRunner::new()
            .callback_for("second", success(&2_u32))
            .callback_for("first", success(&1_u32))
            .run(
                |(), ctx: DurableContext| async move {
                    let branches: Vec<crate::Branch<u32>> = ["first", "second"]
                        .into_iter()
                        .map(|name| {
                            crate::Branch::new(name, move |child: DurableContext| async move {
                                let value = child
                                    .wait_for_callback::<u32>(|_step_ctx, _callback_id| async {
                                        Ok(())
                                    })
                                    .name(name)
                                    .await?;
                                Ok(value)
                            })
                        })
                        .collect();
                    Ok::<_, BoxError>(ctx.parallel(branches).name("both").await?)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&vec![1, 2]));
    }

    #[tokio::test]
    async fn callback_for_is_delivered_before_fifo() {
        let result = LocalRunner::new()
            .callback(success(&"fifo"))
            .callback_for("named", success(&"named"))
            .run(
                |(), ctx: DurableContext| async move {
                    let named = ctx.create_callback::<String>().name("named").await?;
                    let other = ctx.create_callback::<String>().name("other").await?;
                    let a = named.result().await?;
                    let b = other.result().await?;
                    Ok::<_, BoxError>(format!("named={a} other={b}"))
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            result.output().map(String::as_str),
            Some("named=named other=fifo")
        );
    }

    #[tokio::test]
    async fn callback_for_ignores_plain_child_context_name() {
        let result = LocalRunner::new()
            .callback_for("box", success(&1_u32))
            .run(
                |(), ctx: DurableContext| async move {
                    let value = ctx
                        .run_in_child_context(|child| async move {
                            let cb = child.create_callback::<u32>().await?;
                            Ok(cb.result().await?)
                        })
                        .name("box")
                        .await?;
                    Ok::<_, BoxError>(value)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_suspended(), "{:?}", result.error_message());
    }

    #[tokio::test]
    async fn callback_for_unused_name_is_reported() {
        let result = LocalRunner::new()
            .callback_for("missing", success(&1_u32))
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<u32>().name("cb").await?;
                    Ok::<_, BoxError>(cb.result().await?)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_suspended(), "{:?}", result.error_message());
        let message = result.error_message().unwrap_or_default();
        assert!(
            message.ends_with("; no pending callback matched the queued replies for: \"missing\""),
            "{message}"
        );
    }

    /// Rule 8 of the issue: the unused names appear in queue order, each
    /// once, and a run with no unused name keeps the old message.
    #[test]
    fn suspended_message_lists_unused_names_once_in_queue_order() {
        let backend = Backend::new(
            QueuedReplies {
                fifo: std::collections::VecDeque::new(),
                named: vec![
                    ("missing".to_owned(), success(&1_u32)),
                    ("other".to_owned(), success(&2_u32)),
                    ("missing".to_owned(), success(&3_u32)),
                ],
            },
            None,
        );
        assert_eq!(
            suspended_message(&backend.unmatched_reply_names(), &[]),
            "execution suspended with no pending timer, retry, or callback to advance \
             (missing callback outcome?); no pending callback matched the queued replies \
             for: \"missing\", \"other\""
        );
        assert_eq!(
            suspended_message(&[], &[]),
            "execution suspended with no pending timer, retry, or callback to advance \
             (missing callback outcome?)"
        );
    }

    #[tokio::test]
    async fn callback_reply_failure_carries_stack_trace() {
        let result = LocalRunner::new()
            .callback(CallbackReply::failure(
                CallbackFailure::new("Declined", "no")
                    .with_stack_trace(vec!["at a".into(), "at b".into()]),
            ))
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<String>().name("cb").await?;
                    let Err(err) = cb.result().await else {
                        return Err::<Vec<String>, BoxError>("callback succeeded".into());
                    };
                    Ok(err
                        .wire()
                        .map(|wire| wire.stack_trace().to_vec())
                        .unwrap_or_default())
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            result.output(),
            Some(&vec!["at a".to_owned(), "at b".to_owned()])
        );
    }

    #[tokio::test]
    async fn callback_reply_default_failure_has_no_fields() {
        let result = LocalRunner::new()
            .callback(CallbackReply::failure(CallbackFailure::default()))
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<String>().name("cb").await?;
                    Ok::<_, BoxError>(cb.result().await?)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_failure(), "{:?}", result.error_message());
        assert_eq!(result.error_message(), Some("Callback failed"));
        let cb_op = callback_record(&result);
        assert_eq!(cb_op.status(), OperationStatus::Failed);
        assert_eq!(cb_op.error_type(), None);
        assert_eq!(cb_op.error_message(), None);
    }

    /// A failure that sets only the error data or only the stack trace
    /// still reaches the handler with that field (see `has_error_fields`).
    #[tokio::test]
    async fn callback_reply_failure_with_only_data_or_trace_keeps_it() {
        let read = |failure: CallbackFailure| async move {
            LocalRunner::new()
                .callback(CallbackReply::failure(failure))
                .run(
                    |(), ctx: DurableContext| async move {
                        let cb = ctx.create_callback::<String>().name("cb").await?;
                        let Err(err) = cb.result().await else {
                            return Err::<(Option<String>, Vec<String>), BoxError>(
                                "callback succeeded".into(),
                            );
                        };
                        let wire = err.wire();
                        Ok((
                            wire.and_then(crate::WireError::error_data)
                                .map(ToOwned::to_owned),
                            wire.map(|w| w.stack_trace().to_vec()).unwrap_or_default(),
                        ))
                    },
                    (),
                )
                .await
                .expect("runner")
        };

        let data_only = read(CallbackFailure::default().with_error_data("d")).await;
        assert_eq!(
            data_only.output(),
            Some(&(Some("d".to_owned()), Vec::new()))
        );
        let trace_only =
            read(CallbackFailure::default().with_stack_trace(vec!["at a".to_owned()])).await;
        assert_eq!(trace_only.output(), Some(&(None, vec!["at a".to_owned()])));
    }

    #[tokio::test]
    async fn callback_reply_heartbeat_timeout_reaches_handler() {
        let result = LocalRunner::new()
            .callback(CallbackReply::heartbeat_timeout())
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx
                        .create_callback::<u32>()
                        .name("cb")
                        .heartbeat_timeout(std::time::Duration::from_secs(60))
                        .await?;
                    let Err(err) = cb.result().await else {
                        return Err::<bool, BoxError>("callback succeeded".into());
                    };
                    Ok(matches!(
                        err.kind(),
                        crate::OperationErrorKind::Callback(e)
                            if matches!(e.kind(), crate::CallbackErrorKind::HeartbeatTimedOut)
                    ))
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&true));
        let cb_op = callback_record(&result);
        assert_eq!(cb_op.status(), OperationStatus::TimedOut);
        assert_eq!(cb_op.error_type(), Some("Callback.Heartbeat"));
        assert_eq!(
            cb_op.error_message(),
            Some("Callback timed out on heartbeat")
        );
    }

    /// A value whose `Serialize` impl always fails.
    struct Refuses;

    impl Serialize for Refuses {
        fn serialize<S: serde::Serializer>(&self, _serializer: S) -> Result<S::Ok, S::Error> {
            Err(serde::ser::Error::custom("refuse"))
        }
    }

    #[test]
    fn callback_reply_success_returns_the_encoding_error() {
        let err = CallbackReply::success(&Refuses).expect_err("encoding fails");
        assert!(err.to_string().contains("refuse"), "{err}");
    }

    /// A callback result created via the public `ctx.create_callback` API
    /// composes with a real step future inside `try_join_all`. This exercises
    /// the full builder→callback→combinator path through replay, ensuring
    /// regressions in the public surface are caught.
    #[tokio::test]
    async fn callback_result_composes_with_step_in_combinator() {
        let result = LocalRunner::new()
            .callback(success(&"from-callback".to_owned()))
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<String>().name("approval").await?;

                    let step_future = ctx
                        .step(|_| async { Ok("from-step".to_owned()) })
                        .name("work")
                        .spawn();

                    let results = ctx.try_join_all([cb.result(), step_future]).await?;

                    Ok::<_, BoxError>(results)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(
            result.is_success(),
            "handler should succeed: {:?}",
            result.error_message()
        );
        let output = result.output().expect("should have output");
        assert_eq!(output.len(), 2);
        assert_eq!(output[0], "from-callback");
        assert_eq!(output[1], "from-step");
        // At least 2 invocations: first suspends on the callback, second
        // replays with the settled outcome and completes.
        assert!(result.invocation_count() >= 2);
    }

    // 7. replay determinism

    #[tokio::test]
    async fn replay_is_deterministic() {
        // The same handler + event yields identical operation IDs and
        // results across two independent runs.
        let make_run = || async {
            LocalRunner::new()
                .run(
                    |(), ctx: DurableContext| async move {
                        let a = ctx.step(|_| async { Ok(1) }).name("a").await?;
                        ctx.wait(std::time::Duration::from_mins(1))
                            .name("w")
                            .await?;
                        let b = ctx.step(|_| async { Ok(2) }).name("b").await?;
                        Ok::<_, BoxError>(a + b)
                    },
                    (),
                )
                .await
                .expect("runner")
        };

        let first = make_run().await;
        let second = make_run().await;

        assert!(first.is_success());
        assert!(second.is_success());
        assert_eq!(first.output(), second.output());

        let ids_first: Vec<&str> = first.operations().iter().map(TestOperation::id).collect();
        let ids_second: Vec<&str> = second.operations().iter().map(TestOperation::id).collect();
        assert_eq!(ids_first, ids_second, "operation IDs must be deterministic");

        let results_first: Vec<Option<&str>> = first
            .operations()
            .iter()
            .map(TestOperation::result)
            .collect();
        let results_second: Vec<Option<&str>> = second
            .operations()
            .iter()
            .map(TestOperation::result)
            .collect();
        assert_eq!(results_first, results_second);
    }

    // CloudRunner pure-mapping tests (no AWS)

    use aws_sdk_lambda::types::{
        CallbackTimedOutDetails, EventError, EventResult, RetryDetails, StepFailedDetails,
        StepSucceededDetails,
    };

    #[test]
    fn fold_history_folds_step_lifecycle() {
        let events = vec![
            Event::builder()
                .event_type(EventType::StepStarted)
                .id("op-1")
                .name("charge")
                .event_id(1)
                .build(),
            Event::builder()
                .event_type(EventType::StepSucceeded)
                .id("op-1")
                .event_id(2)
                .step_succeeded_details(
                    StepSucceededDetails::builder()
                        .result(EventResult::builder().payload("\"ok\"").build())
                        .retry_details(RetryDetails::builder().current_attempt(0).build())
                        .build(),
                )
                .build(),
        ];
        let ops = fold_history(&events);
        assert_eq!(ops.len(), 1);
        assert_eq!(ops[0].kind(), OperationKind::Step);
        assert_eq!(ops[0].status(), OperationStatus::Succeeded);
        assert_eq!(ops[0].result(), Some("\"ok\""));
        assert_eq!(ops[0].name(), Some("charge"));
        assert_eq!(ops[0].id(), "op-1");
        assert!(ops[0].succeeded());
    }

    #[test]
    fn fold_history_records_retry_attempt() {
        let events = vec![
            Event::builder()
                .event_type(EventType::StepStarted)
                .id("s")
                .event_id(1)
                .build(),
            Event::builder()
                .event_type(EventType::StepFailed)
                .id("s")
                .event_id(2)
                .step_failed_details(
                    StepFailedDetails::builder()
                        .error(
                            EventError::builder()
                                .payload(
                                    ErrorObject::builder()
                                        .error_type("StepError")
                                        .error_message("boom")
                                        .build(),
                                )
                                .build(),
                        )
                        .retry_details(RetryDetails::builder().current_attempt(1).build())
                        .build(),
                )
                .build(),
            Event::builder()
                .event_type(EventType::StepSucceeded)
                .id("s")
                .event_id(3)
                .step_succeeded_details(
                    StepSucceededDetails::builder()
                        .result(EventResult::builder().payload("99").build())
                        .retry_details(RetryDetails::builder().current_attempt(2).build())
                        .build(),
                )
                .build(),
        ];
        let ops = fold_history(&events);
        assert_eq!(ops.len(), 1);
        assert_eq!(ops[0].status(), OperationStatus::Succeeded);
        assert_eq!(
            step_attempt(&ops[0]),
            Some(1),
            "current_attempt 2 normalizes to 1 completed retry"
        );
        assert_eq!(ops[0].result(), Some("99"));
        assert!(
            ops[0].error().is_none(),
            "the failed attempt's error is not kept: {:?}",
            ops[0].error()
        );

        // A retry that has started but not finished is `Started`, with no
        // error: the `StepStarted` arm sets the status, and its event
        // carries no error.
        let ops = fold_history(
            &events[..2]
                .iter()
                .chain(&events[..1])
                .cloned()
                .collect::<Vec<_>>(),
        );
        assert_eq!(ops[0].status(), OperationStatus::Started);
        assert!(ops[0].error().is_none(), "{:?}", ops[0].error());
    }

    #[test]
    fn fold_history_normalizes_attempt_to_completed_retries() {
        // A first-try success reports current_attempt=1, which must normalize
        // to 0 completed retries so LocalRunner and CloudRunner agree.
        let first_try = vec![
            Event::builder()
                .event_type(EventType::StepStarted)
                .id("s")
                .event_id(1)
                .build(),
            Event::builder()
                .event_type(EventType::StepSucceeded)
                .id("s")
                .event_id(2)
                .step_succeeded_details(
                    StepSucceededDetails::builder()
                        .result(EventResult::builder().payload("\"ok\"").build())
                        .retry_details(RetryDetails::builder().current_attempt(1).build())
                        .build(),
                )
                .build(),
        ];
        assert_eq!(
            step_attempt(&fold_history(&first_try)[0]),
            Some(0),
            "first-try success (current_attempt=1) is 0 completed retries"
        );

        // An unexpected current_attempt=0 must not underflow or panic.
        let zero = vec![
            Event::builder()
                .event_type(EventType::StepStarted)
                .id("s")
                .event_id(1)
                .build(),
            Event::builder()
                .event_type(EventType::StepSucceeded)
                .id("s")
                .event_id(2)
                .step_succeeded_details(
                    StepSucceededDetails::builder()
                        .result(EventResult::builder().payload("\"ok\"").build())
                        .retry_details(RetryDetails::builder().current_attempt(0).build())
                        .build(),
                )
                .build(),
        ];
        assert_eq!(
            step_attempt(&fold_history(&zero)[0]),
            Some(0),
            "non-positive current_attempt saturates to 0"
        );
    }

    #[test]
    fn fold_history_maps_callback_timed_out() {
        let events = vec![
            Event::builder()
                .event_type(EventType::CallbackStarted)
                .id("cb")
                .event_id(1)
                .build(),
            Event::builder()
                .event_type(EventType::CallbackTimedOut)
                .id("cb")
                .event_id(2)
                .callback_timed_out_details(
                    CallbackTimedOutDetails::builder()
                        .error(
                            EventError::builder()
                                .payload(
                                    ErrorObject::builder()
                                        .error_type("CallbackError")
                                        .error_message("timed out")
                                        .build(),
                                )
                                .build(),
                        )
                        .build(),
                )
                .build(),
        ];
        let ops = fold_history(&events);
        assert_eq!(ops.len(), 1);
        assert_eq!(ops[0].kind(), OperationKind::Callback);
        assert_eq!(ops[0].status(), OperationStatus::TimedOut);
        assert_eq!(ops[0].error_type(), Some("CallbackError"));
        // The `CallbackStarted` event above has no `CallbackStartedDetails`,
        // so the callback reports no ID.
        assert!(
            matches!(ops[0].details(), TestOperationDetails::Callback(c) if c.callback_id().is_none()),
            "{:?}",
            ops[0].details()
        );
    }

    #[test]
    fn fold_history_skips_execution_and_invocation_events() {
        let events = vec![
            Event::builder()
                .event_type(EventType::ExecutionStarted)
                .event_id(1)
                .build(),
            Event::builder()
                .event_type(EventType::InvocationCompleted)
                .event_id(2)
                .build(),
            Event::builder()
                .event_type(EventType::StepSucceeded)
                .id("op")
                .event_id(3)
                .step_succeeded_details(
                    StepSucceededDetails::builder()
                        .result(EventResult::builder().payload("1").build())
                        .build(),
                )
                .build(),
            Event::builder()
                .event_type(EventType::InvocationCompleted)
                .event_id(4)
                .build(),
            Event::builder()
                .event_type(EventType::ExecutionSucceeded)
                .event_id(5)
                .build(),
        ];
        let ops = fold_history(&events);
        assert_eq!(ops.len(), 1, "only the operation-scoped event is folded");
        assert_eq!(ops[0].kind(), OperationKind::Step);
        assert_eq!(
            step_attempt(&ops[0]),
            Some(0),
            "a step event without RetryDetails reports attempt 0"
        );
        assert_eq!(invocations_from_history(&events), 2);
    }

    #[test]
    fn fold_history_preserves_first_appearance_order() {
        let events = vec![
            Event::builder()
                .event_type(EventType::StepStarted)
                .id("first")
                .event_id(1)
                .build(),
            Event::builder()
                .event_type(EventType::WaitStarted)
                .id("second")
                .event_id(2)
                .build(),
            Event::builder()
                .event_type(EventType::WaitSucceeded)
                .id("second")
                .event_id(3)
                .build(),
            Event::builder()
                .event_type(EventType::StepSucceeded)
                .id("first")
                .event_id(4)
                .step_succeeded_details(StepSucceededDetails::builder().build())
                .build(),
        ];
        let ops = fold_history(&events);
        let ids: Vec<&str> = ops.iter().map(TestOperation::id).collect();
        assert_eq!(ids, vec!["first", "second"]);
        assert_eq!(ops[1].kind(), OperationKind::Wait);
        assert_eq!(ops[1].status(), OperationStatus::Succeeded);
        // The `WaitStarted` event above has no `WaitStartedDetails`, so the
        // wait reports no duration.
        assert!(
            matches!(ops[1].details(), TestOperationDetails::Wait(w) if w.duration().is_none()),
            "{:?}",
            ops[1].details()
        );
    }

    /// An operation whose history holds no started event keeps the
    /// defaults of [`TestOperation::started`]: attempt 0, no wait duration,
    /// no callback ID, and `replay_children` false.
    #[test]
    fn fold_history_defaults_without_started_events() {
        let events = vec![
            Event::builder()
                .event_type(EventType::StepSucceeded)
                .id("s")
                .event_id(1)
                .build(),
            Event::builder()
                .event_type(EventType::WaitSucceeded)
                .id("w")
                .event_id(2)
                .build(),
            Event::builder()
                .event_type(EventType::CallbackSucceeded)
                .id("cb")
                .event_id(3)
                .build(),
            Event::builder()
                .event_type(EventType::ContextSucceeded)
                .id("c")
                .event_id(4)
                .build(),
        ];
        let ops = fold_history(&events);
        assert_eq!(ops.len(), 4);
        assert!(
            matches!(ops[0].details(), TestOperationDetails::Step(s) if s.attempt() == 0),
            "{:?}",
            ops[0].details()
        );
        assert!(
            matches!(ops[1].details(), TestOperationDetails::Wait(w) if w.duration().is_none()),
            "{:?}",
            ops[1].details()
        );
        assert!(
            matches!(ops[2].details(), TestOperationDetails::Callback(c) if c.callback_id().is_none()),
            "{:?}",
            ops[2].details()
        );
        assert!(
            matches!(ops[3].details(), TestOperationDetails::Context(c) if !c.replay_children()),
            "{:?}",
            ops[3].details()
        );
        for op in &ops {
            assert_eq!(op.status(), OperationStatus::Succeeded);
            assert!(op.result().is_none(), "{:?}", op.result());
            assert!(op.error().is_none(), "{:?}", op.error());
            assert!(op.parent_id().is_none());
        }
    }

    #[test]
    fn deserialize_output_handles_plain_missing_and_double_encoded() {
        assert_eq!(deserialize_output::<i32>(Some("42")).unwrap(), 42);
        assert_eq!(deserialize_output::<()>(None).unwrap(), ());
        // Double-encoded: a JSON string whose contents are the JSON value.
        assert_eq!(deserialize_output::<i32>(Some("\"7\"")).unwrap(), 7);
        assert!(deserialize_output::<i32>(Some("not json")).is_err());
    }

    /// A payload that is not a JSON string returns the error of decoding it
    /// as `O`. A JSON string whose content does not decode returns the
    /// error of decoding that content.
    #[test]
    fn deserialize_output_returns_the_error_of_the_decoded_value() {
        let plain = deserialize_output::<u32>(Some("{\"a\":1}")).expect_err("not a u32");
        let expected = serde_json::from_str::<u32>("{\"a\":1}").expect_err("not a u32");
        assert_eq!(plain.to_string(), expected.to_string());

        let inner = deserialize_output::<u32>(Some("\"x\"")).expect_err("not a u32");
        let expected = serde_json::from_str::<u32>("x").expect_err("not a u32");
        assert_eq!(inner.to_string(), expected.to_string());
    }

    #[test]
    fn execution_status_and_family_maps() {
        // Each row: the service status, the status it maps to, and the
        // `Display` text of that status. Every variant appears once.
        let rows = [
            (
                WireExecutionStatus::Running,
                ExecutionStatus::Running,
                "Running",
            ),
            (
                WireExecutionStatus::Succeeded,
                ExecutionStatus::Succeeded,
                "Succeeded",
            ),
            (
                WireExecutionStatus::Failed,
                ExecutionStatus::Failed,
                "Failed",
            ),
            (
                WireExecutionStatus::TimedOut,
                ExecutionStatus::TimedOut,
                "TimedOut",
            ),
            (
                WireExecutionStatus::Stopped,
                ExecutionStatus::Stopped,
                "Stopped",
            ),
        ];
        for (wire, status, text) in rows {
            assert_eq!(ExecutionStatus::from_wire(&wire), status, "{wire:?}");
            assert_eq!(status.to_string(), text);
        }
        assert_eq!(
            event_family_op_type(Some(&EventType::ChainedInvokeSucceeded)),
            Some(RunnerKind::ChainedInvoke)
        );
        assert_eq!(
            event_family_op_type(Some(&EventType::ExecutionStarted)),
            None
        );
        assert_eq!(event_family_op_type(None), None);
    }

    #[test]
    fn operation_kind_from_wire_round_trips() {
        for kind in [
            OperationKind::Step,
            OperationKind::Wait,
            OperationKind::Context,
            OperationKind::Callback,
            OperationKind::ChainedInvoke,
            OperationKind::Execution,
        ] {
            assert_eq!(
                OperationKind::from_wire(&OperationType::from(kind.wire_str())),
                Some(kind)
            );
        }
        assert_eq!(
            OperationKind::from_wire(&OperationType::from("BOGUS")),
            None
        );
    }

    #[tokio::test]
    async fn step_reports_kind_and_status() {
        let result = LocalRunner::new()
            .run(
                |_e: (), ctx: DurableContext| async move {
                    ctx.step(|_| async { Ok(1_i32) }).name("only").await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        let ops = result.operations();
        assert_eq!(ops.len(), 1);
        assert_eq!(ops[0].kind(), OperationKind::Step);
        assert_eq!(ops[0].status(), OperationStatus::Succeeded);
        assert!(ops[0].succeeded());
    }

    #[tokio::test]
    async fn unknown_operation_type_is_rejected() {
        let backend = Backend::new(QueuedReplies::default(), None);
        let token = backend.current_token();
        let update = OperationUpdate::builder()
            .id("bogus")
            .r#type(OperationType::from("BOGUS"))
            .action(OperationAction::Start)
            .build()
            .expect("valid update");
        let e = backend
            .checkpoint("arn", &token, vec![update])
            .await
            .expect_err("an unknown operation type is rejected");
        assert_eq!(e.to_string(), "durable: client: Invalid operation type.");
        assert!(!e.is_retryable());
        assert!(backend.snapshot_operations().is_empty());
        assert_eq!(backend.current_token(), token);
    }

    // Pagination tests

    /// A two-page initial state replays all operations: the runner calls
    /// `get_state` when the history exceeds `initial_page_size`, and the
    /// handler sees results from prior invocations without re-executing.
    #[tokio::test]
    async fn two_page_initial_state_replays_all_operations() {
        // Use a static counter to detect re-execution of the first step.
        static FIRST_STEP_EXECUTIONS: AtomicU32 = AtomicU32::new(0);
        FIRST_STEP_EXECUTIONS.store(0, Ordering::SeqCst);

        let result = LocalRunner::new()
            .initial_page_size(1) // Only 1 op per "page" in initial state
            .run(
                |_event: (), ctx: DurableContext| async move {
                    // First step: should only execute on the first invocation.
                    let a = ctx
                        .step(|_| async {
                            FIRST_STEP_EXECUTIONS.fetch_add(1, Ordering::SeqCst);
                            Ok(10_i32)
                        })
                        .name("first")
                        .await?;

                    // Wait triggers a re-invocation boundary.
                    ctx.wait(std::time::Duration::from_secs(1))
                        .name("pause")
                        .await?;

                    // Second step on re-invocation: initial state now has
                    // 2+ ops, exceeding page_size=1. The runner must fetch
                    // the full state via get_state so `a` replays correctly.
                    let b = ctx
                        .step(move |_| async move { Ok(a + 32) })
                        .name("second")
                        .await?;

                    Ok::<_, BoxError>(b)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(
            result.is_success(),
            "handler failed: {:?}",
            result.error_message()
        );
        assert_eq!(result.output(), Some(&42));
        // The first step must execute exactly once (not re-executed on replay).
        assert_eq!(
            FIRST_STEP_EXECUTIONS.load(Ordering::SeqCst),
            1,
            "first step must execute exactly once; pagination must not cause re-execution"
        );
        // Should take 2 invocations: first runs both steps+wait, second replays
        // the wait (now elapsed) and runs the second step.
        assert!(
            result.invocation_count() >= 2,
            "expected at least 2 invocations"
        );
    }

    /// A two-page checkpoint response is fully consumed: when the backend
    /// returns `next_marker` in a checkpoint response, `checkpoint_updates`
    /// calls `get_state` to merge remaining operations, ensuring subsequent
    /// reads see all state.
    #[tokio::test]
    async fn two_page_checkpoint_response_is_fully_consumed() {
        // When checkpoint_page_size is 1, the second checkpoint response
        // (which sees 2+ stored ops) includes a marker, triggering
        // pagination in checkpoint_updates.
        let result = LocalRunner::new()
            .checkpoint_page_size(1) // Trigger pagination after 1 stored op
            .run(
                |_event: (), ctx: DurableContext| async move {
                    // First step: creates 1 stored op. Checkpoint response
                    // has no marker (1 op <= page_size=1 threshold).
                    let a = ctx.step(|_| async { Ok(10_i32) }).name("step-a").await?;

                    // Second step: creates 2nd stored op. The checkpoint
                    // response now returns next_marker (2 ops > 1), so
                    // checkpoint_updates must call get_state to merge all
                    // operations.
                    let b = ctx
                        .step(move |_| async move { Ok(a + 5) })
                        .name("step-b")
                        .await?;

                    // Third step proves subsequent operations see the full
                    // state, including operations merged via get_state.
                    let c = ctx
                        .step(move |_| async move { Ok(b + 27) })
                        .name("step-c")
                        .await?;

                    Ok::<_, BoxError>(c)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(
            result.is_success(),
            "handler failed: {:?}",
            result.error_message()
        );
        assert_eq!(result.output(), Some(&42));
        assert_eq!(result.operations().len(), 3);
    }

    /// A truncated checkpoint page genuinely requires the second page: the
    /// backend returns only the FIRST stored operation in the callback
    /// Start checkpoint response (plus a marker), so the backend-assigned
    /// `callback_id` is observable only through the follow-up `get_state`
    /// merge in `checkpoint_updates`. If the marker were ignored, the
    /// handler would see an empty callback id and fail.
    #[tokio::test]
    async fn truncated_checkpoint_page_requires_second_page() {
        let result = LocalRunner::new()
            .checkpoint_page_size(1)
            .callback(success(&"approved".to_owned()))
            .run(
                |_event: (), ctx: DurableContext| async move {
                    // First stored operation: fills page 1.
                    let base = ctx.step(|_| async { Ok(1_i32) }).name("filler").await?;

                    // Second stored operation: the callback. Its Start
                    // checkpoint response is truncated to page 1 (the
                    // filler step), so the callback_id below arrives only
                    // via the paginated get_state merge.
                    let cb = ctx.create_callback::<String>().name("approval").await?;
                    if cb.id().is_empty() {
                        return Err::<String, BoxError>(
                            "callback id missing: second checkpoint page was not consumed".into(),
                        );
                    }

                    let approval = cb.result().await?;
                    Ok(format!("{base}:{approval}"))
                },
                (),
            )
            .await
            .expect("runner");

        assert!(
            result.is_success(),
            "handler failed: {:?}",
            result.error_message()
        );
        assert_eq!(result.output(), Some(&"1:approved".to_owned()));
    }

    /// Combining both pagination modes: a handler with paginated initial
    /// state AND paginated checkpoint responses still runs correctly.
    #[tokio::test]
    async fn combined_initial_and_checkpoint_pagination() {
        let result = LocalRunner::new()
            .initial_page_size(1)
            .checkpoint_page_size(2)
            .run(
                |_event: (), ctx: DurableContext| async move {
                    let a = ctx.step(|_| async { Ok(1_i32) }).name("a").await?;
                    ctx.wait(std::time::Duration::from_secs(1))
                        .name("timer")
                        .await?;
                    let b = ctx
                        .step(move |_| async move { Ok(a + 1) })
                        .name("b")
                        .await?;
                    let c = ctx
                        .step(move |_| async move { Ok(b + 1) })
                        .name("c")
                        .await?;
                    Ok::<_, BoxError>(c)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(
            result.is_success(),
            "handler failed: {:?}",
            result.error_message()
        );
        assert_eq!(result.output(), Some(&3));
    }

    /// Tests that the Backend with `checkpoint_page_size` returns a
    /// `next_marker` when operations exceed the page size, simulating
    /// a two-page checkpoint response. The caller (`DurableContext`)
    /// should then paginate via `get_state`.
    #[tokio::test]
    async fn backend_paginated_checkpoint_returns_marker() {
        let backend = Arc::new(Backend::new(QueuedReplies::default(), Some(1)));

        // Checkpoint two operations so we exceed page_size=1.
        let updates = vec![
            OperationUpdate::builder()
                .id("op-1")
                .r#type(OperationType::Step)
                .action(OperationAction::Start)
                .build()
                .unwrap(),
            OperationUpdate::builder()
                .id("op-2")
                .r#type(OperationType::Step)
                .action(OperationAction::Start)
                .build()
                .unwrap(),
        ];

        let result = backend.checkpoint("arn:test", "token-0", updates).await;
        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(
            output.next_marker.is_some(),
            "expected a next_marker when operations exceed page_size"
        );

        // Verify that get_state returns all operations (used for pagination).
        let full_state = backend.get_state("arn:test", "token-0", None).await;
        assert!(full_state.is_ok());
        let full_state = full_state.unwrap();
        assert_eq!(
            full_state.operations.len(),
            2,
            "get_state must return all operations for pagination"
        );
    }

    /// Tests that the shared production bootstrap helper
    /// (`resolve_bootstrap_log`) follows the pagination marker: given a
    /// truncated first page plus a marker, it fetches the complete state
    /// via `get_state`; without a marker it uses the first page as-is and
    /// never calls `get_state`.
    #[tokio::test]
    async fn two_page_bootstrap_replays_all_operations() {
        use crate::client::{
            InMemoryExecutionClient, operations_to_checkpoint_log, resolve_bootstrap_log,
        };

        // Full backend state: two steps. Page 1 delivers only step-1.
        let all_ops = vec![
            Operation::builder()
                .id("step-1")
                .r#type(OperationType::Step)
                .status(WireOperationStatus::Succeeded)
                .start_timestamp(DateTime::from_secs(0))
                .step_details(
                    WireStepDetails::builder()
                        .result("\"page1-result\"")
                        .build(),
                )
                .build()
                .unwrap(),
            Operation::builder()
                .id("step-2")
                .r#type(OperationType::Step)
                .status(WireOperationStatus::Succeeded)
                .start_timestamp(DateTime::from_secs(0))
                .step_details(
                    WireStepDetails::builder()
                        .result("\"page2-result\"")
                        .build(),
                )
                .build()
                .unwrap(),
        ];

        let first_page: Vec<Operation> = all_ops.get(..1).unwrap_or(&all_ops).to_vec();

        let client = Arc::new(InMemoryExecutionClient::new(all_ops));

        // Marker present: the helper must fetch the complete state.
        let log = resolve_bootstrap_log(
            client.as_ref(),
            "arn:test",
            "token",
            crate::client::BootstrapState {
                log: operations_to_checkpoint_log(&first_page),
                input_payload: Some("{}".to_owned()),
                execution: None,
            },
            Some("marker-1"),
        )
        .await
        .unwrap()
        .log;

        assert!(
            log.get("step-1").is_some(),
            "step-1 from page 1 must be in the log"
        );
        assert!(
            log.get("step-2").is_some(),
            "step-2 from page 2 must be in the log"
        );

        let r1 = log.get("step-1").unwrap();
        assert_eq!(r1.result.as_deref(), Some("\"page1-result\""));

        let r2 = log.get("step-2").unwrap();
        assert_eq!(r2.result.as_deref(), Some("\"page2-result\""));

        let calls_after_marker = *client
            .get_state_call_count
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        assert_eq!(
            calls_after_marker, 1,
            "marker must trigger exactly one get_state fetch"
        );

        // No marker: the first page is complete, no get_state call, and
        // the log holds only page-1 operations.
        let log = resolve_bootstrap_log(
            client.as_ref(),
            "arn:test",
            "token",
            crate::client::BootstrapState {
                log: operations_to_checkpoint_log(&first_page),
                input_payload: Some("{}".to_owned()),
                execution: None,
            },
            None,
        )
        .await
        .unwrap()
        .log;

        assert!(log.get("step-1").is_some());
        assert!(
            log.get("step-2").is_none(),
            "without a marker the helper must not fetch further pages"
        );
        let calls_after_no_marker = *client
            .get_state_call_count
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        assert_eq!(
            calls_after_no_marker, 1,
            "no-marker path must not call get_state again"
        );
    }

    // Harness fidelity: production task topology

    /// The runner awaits the invocation INLINE under the caller's
    /// `block_on` (the production `lambda_runtime` topology), so the context
    /// is created where `tokio::task::try_id()` is `None`, not on a spawned
    /// task. The handler itself then runs as the invocation's root task,
    /// which the SDK blesses, exactly as in production: so it has a task ID,
    /// and its durable operations pass the ownership guard.
    #[tokio::test]
    async fn runner_topology_matches_production_inline_block_on() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    // Same task visibility production gives the handler:
                    // the SDK's root task.
                    let on_root_task = tokio::task::try_id().is_some();
                    let v = ctx.step(|_| async { Ok(1_i32) }).await?;
                    Ok::<_, BoxError>((on_root_task, v))
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            result.output(),
            Some(&(true, 1)),
            "handler must run on the SDK's root task, matching the deployed topology"
        );
    }

    /// Under the production topology, a durable operation invoked from a
    /// bare user `tokio::spawn` (unblessed) is rejected by the ownership
    /// guard: the runner must reproduce that, not mask it.
    #[tokio::test]
    async fn runner_rejects_durable_ops_from_unblessed_spawned_task() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let foreign = ctx.clone();
                    let joined: Result<i32, String> = tokio::spawn(async move {
                        foreign
                            .step(|_| async { Ok(7_i32) })
                            .await
                            // `{:#}` flattens the frame and its chain.
                            .map_err(|e| format!("{e:#}"))
                    })
                    .await
                    .map_err(|e| e.to_string())?;
                    let v = joined.map_err(BoxError::from)?;
                    Ok::<_, BoxError>(v)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(
            result.is_failure(),
            "unblessed tokio::spawn must fail the ownership check as it does in production"
        );
        let msg = result.error_message().unwrap_or_default();
        assert!(
            msg.starts_with("context usage error") && msg.contains(".spawn()"),
            "ownership rejection should carry the production guidance: {msg}"
        );
    }

    // Harness fidelity: state pagination is the DEFAULT

    /// By default the backend serves execution state in 2+ pages: a
    /// re-invocation with two or more recorded operations gets a truncated
    /// inline envelope page plus `NextMarker`, and checkpoint responses
    /// paginate: both force real `get_state` fetches through the
    /// production pagination paths.
    #[tokio::test]
    async fn default_state_pagination_forces_get_state_fetches() {
        let runner = LocalRunner::new();
        let backend = Arc::new(Backend::new(
            QueuedReplies::default(),
            runner.checkpoint_page_size,
        ));

        let result = runner
            .run_on_backend(
                Arc::clone(&backend),
                |(), ctx: DurableContext| async move {
                    let a = ctx.step(|_| async { Ok(1_i32) }).name("a").await?;
                    // Suspend so the next invocation bootstraps from a
                    // multi-operation (hence multi-page) history.
                    ctx.wait(std::time::Duration::from_secs(1))
                        .name("cooldown")
                        .await?;
                    let b = ctx
                        .step(move |_| async move { Ok(a + 1) })
                        .name("b")
                        .await?;
                    Ok::<_, BoxError>(b)
                },
                "null".to_owned(),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&2));
        assert!(
            result.invocation_count() >= 2,
            "wait must force a re-invocation"
        );
        assert!(
            backend.get_state_call_count() >= 2,
            "multi-page default must force get_state fetches for both the bootstrap \
             (envelope NextMarker) and checkpoint pagination paths; saw {}",
            backend.get_state_call_count()
        );
    }

    /// `single_page()` is the explicit special case: the full history rides
    /// inline in every envelope and no checkpoint response paginates, so
    /// the SDK never needs a `get_state` fetch.
    #[tokio::test]
    async fn single_page_never_fetches_state() {
        let runner = LocalRunner::new().single_page();
        let backend = Arc::new(Backend::new(
            QueuedReplies::default(),
            runner.checkpoint_page_size,
        ));

        let result = runner
            .run_on_backend(
                Arc::clone(&backend),
                |(), ctx: DurableContext| async move {
                    let a = ctx.step(|_| async { Ok(1_i32) }).name("a").await?;
                    ctx.wait(std::time::Duration::from_secs(1))
                        .name("cooldown")
                        .await?;
                    let b = ctx
                        .step(move |_| async move { Ok(a + 1) })
                        .name("b")
                        .await?;
                    Ok::<_, BoxError>(b)
                },
                "null".to_owned(),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            backend.get_state_call_count(),
            0,
            "single-page mode must never require a get_state fetch"
        );
    }

    // Checkpoint coalescing (`checkpoint_delay`)

    /// A handler with two concurrently-spawned steps, a suspension, and a
    /// post-resume step. `executions` counts step-body runs so replay
    /// fidelity is observable from outside.
    fn coalescing_probe_handler(
        executions: Arc<std::sync::atomic::AtomicUsize>,
    ) -> impl Fn((), DurableContext) -> Pin<Box<dyn Future<Output = Result<i32, BoxError>> + Send>>
    + Send
    + Sync
    + 'static {
        move |(), ctx: DurableContext| {
            let executions = Arc::clone(&executions);
            Box::pin(async move {
                let e1 = Arc::clone(&executions);
                let e2 = Arc::clone(&executions);
                let e3 = Arc::clone(&executions);

                // Two concurrent steps: their START checkpoints (and their
                // SUCCEED checkpoints) land within one coalescing window.
                let a = ctx
                    .step(move |_| {
                        let e = e1;
                        async move {
                            e.fetch_add(1, Ordering::SeqCst);
                            Ok(1_i32)
                        }
                    })
                    .name("a")
                    .spawn();
                let b = ctx
                    .step(move |_| {
                        let e = e2;
                        async move {
                            e.fetch_add(1, Ordering::SeqCst);
                            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
                            Ok(2_i32)
                        }
                    })
                    .name("b")
                    .spawn();
                let (ra, rb) = tokio::join!(a, b);
                let sum = ra? + rb?;

                // Suspension boundary: the coalesced SUCCEED checkpoints
                // above must have landed by now, or replay after resume
                // would re-execute the bodies.
                ctx.wait(std::time::Duration::from_secs(1))
                    .name("pause")
                    .await?;

                let c = ctx
                    .step(move |_| {
                        let e = e3;
                        async move {
                            e.fetch_add(1, Ordering::SeqCst);
                            Ok(sum + 39)
                        }
                    })
                    .name("c")
                    .await?;
                Ok::<_, BoxError>(c)
            })
        }
    }

    /// `checkpoint_delay` coalesces concurrent checkpoints into fewer API
    /// calls, and everything that must land before the suspension still
    /// does: the spawned step bodies run exactly once across the
    /// suspend/resume boundary, proving their coalesced checkpoints
    /// flushed before the invocation reported PENDING.
    #[tokio::test(start_paused = true)]
    async fn checkpoint_delay_coalesces_and_flushes_before_suspension() {
        // Baseline: identical handler with the default zero window. Step
        // `b` returns 10 ms after `a`, so without a window their SUCCEED
        // updates go in separate requests.
        let baseline_execs = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let baseline_runner = LocalRunner::new().single_page();
        let baseline_backend = Arc::new(Backend::new(QueuedReplies::default(), None));
        let baseline = baseline_runner
            .run_on_backend(
                Arc::clone(&baseline_backend),
                coalescing_probe_handler(Arc::clone(&baseline_execs)),
                "null".to_owned(),
            )
            .await
            .expect("runner");
        assert!(baseline.is_success(), "{:?}", baseline.error_message());
        assert_eq!(baseline.output(), Some(&42));

        // Coalesced: same handler with a checkpoint delay window.
        let coalesced_execs = Arc::new(std::sync::atomic::AtomicUsize::new(0));
        let coalesced_runner = LocalRunner::new().single_page().options(
            crate::Options::builder()
                .checkpoint_delay(std::time::Duration::from_millis(50))
                .build()
                .expect("valid options"),
        );
        let coalesced_backend = Arc::new(Backend::new(QueuedReplies::default(), None));
        let coalesced = coalesced_runner
            .run_on_backend(
                Arc::clone(&coalesced_backend),
                coalescing_probe_handler(Arc::clone(&coalesced_execs)),
                "null".to_owned(),
            )
            .await
            .expect("runner");
        assert!(coalesced.is_success(), "{:?}", coalesced.error_message());
        assert_eq!(
            coalesced.output(),
            Some(&42),
            "coalescing must not change the handler's result"
        );

        // Coalescing must have merged the concurrent steps' checkpoints
        // into fewer API calls than the zero-window baseline.
        let baseline_calls = baseline_backend.checkpoint_call_count();
        let coalesced_calls = coalesced_backend.checkpoint_call_count();
        assert!(
            coalesced_calls < baseline_calls,
            "expected fewer checkpoint calls with coalescing: \
             coalesced={coalesced_calls}, baseline={baseline_calls}"
        );

        // Flush-before-suspension: each step body ran exactly once. A
        // checkpoint held past the PENDING boundary would force the
        // resumed invocation to re-execute the body.
        assert_eq!(
            coalesced_execs.load(Ordering::SeqCst),
            3,
            "each step body must run exactly once across suspend/resume"
        );
        assert_eq!(
            baseline_execs.load(Ordering::SeqCst),
            3,
            "baseline sanity: each step body runs exactly once"
        );
    }

    /// Runs `handler` on a retained backend and returns the number of
    /// checkpoint calls the backend received.
    async fn checkpoint_calls<O, F, Fut>(runner: &LocalRunner, handler: F) -> usize
    where
        O: Serialize + DeserializeOwned + Send + 'static,
        F: Fn((), DurableContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<O, BoxError>> + Send,
    {
        let backend = Arc::new(Backend::new(
            QueuedReplies::default(),
            runner.checkpoint_page_size,
        ));
        let result = runner
            .run_on_backend(Arc::clone(&backend), handler, "null".to_owned())
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        backend.checkpoint_call_count()
    }

    /// Two top-level steps awaited one after the other send four requests:
    /// one for each `START` and `SUCCEED` update.
    #[tokio::test(start_paused = true)]
    async fn top_level_steps_awaited_in_sequence_send_separate_requests() {
        let handler = |(), ctx: DurableContext| async move {
            let a = ctx.step(|_| async { Ok(1_i32) }).name("a").await?;
            let b = ctx
                .step(move |_| async move { Ok(a + 1) })
                .name("b")
                .await?;
            Ok::<_, BoxError>(b)
        };
        let calls = checkpoint_calls(&LocalRunner::new().single_page(), handler).await;
        assert_eq!(calls, 4, "one request per START and SUCCEED");
    }

    /// A transport that fails every call and counts how often it was used.
    struct CountingClient {
        calls: std::sync::atomic::AtomicUsize,
    }

    impl ExecutionClient for CountingClient {
        fn checkpoint(
            &self,
            _execution_arn: &str,
            _checkpoint_token: &str,
            _updates: Vec<OperationUpdate>,
        ) -> Pin<Box<dyn Future<Output = Result<CheckpointOutput, ExecutionClientError>> + Send + '_>>
        {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Box::pin(async {
                Err(ExecutionClientError::new(
                    crate::client::ExecutionClientErrorScope::Execution,
                    "must not be called",
                ))
            })
        }

        fn get_state(
            &self,
            _execution_arn: &str,
            _checkpoint_token: &str,
            _marker: Option<&str>,
        ) -> Pin<Box<dyn Future<Output = Result<GetStateOutput, ExecutionClientError>> + Send + '_>>
        {
            self.calls.fetch_add(1, Ordering::SeqCst);
            Box::pin(async {
                Err(ExecutionClientError::new(
                    crate::client::ExecutionClientErrorScope::Execution,
                    "must not be called",
                ))
            })
        }
    }

    /// A one-step handler that returns 42.
    async fn one_step_handler((): (), ctx: DurableContext) -> Result<i32, BoxError> {
        Ok(ctx.step(|_| async { Ok(42_i32) }).name("only").await?)
    }

    /// The runner replaces the transport in the supplied `Options` with its
    /// in-memory backend, so a test can pass its production `Options`.
    #[tokio::test]
    async fn runner_ignores_the_transport_in_options() {
        // Case 1: an offline Lambda client. It has no credentials, so a run
        // that used it would fail.
        let conf = aws_sdk_lambda::config::Config::builder()
            .behavior_version(aws_sdk_lambda::config::BehaviorVersion::latest())
            .region(aws_sdk_lambda::config::Region::new("us-east-1"))
            .build();
        let lambda_options = crate::Options::builder()
            .lambda_client(aws_sdk_lambda::Client::from_conf(conf))
            .build()
            .expect("valid options");
        let result = LocalRunner::new()
            .options(lambda_options)
            .run(one_step_handler, ())
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&42));

        // Case 2: a caller-supplied execution client that fails every call.
        let counting = Arc::new(CountingClient {
            calls: std::sync::atomic::AtomicUsize::new(0),
        });
        let client_options = crate::Options::builder()
            .execution_client(Arc::clone(&counting) as Arc<dyn ExecutionClient>)
            .build()
            .expect("valid options");
        let result = LocalRunner::new()
            .options(client_options)
            .run(one_step_handler, ())
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&42));
        assert_eq!(
            counting.calls.load(Ordering::SeqCst),
            0,
            "the runner must never call the supplied execution client"
        );
    }

    /// `checkpoint_delay` with a whole-handler completion: the terminal
    /// SUCCEEDED envelope is only reported after the end-of-invocation
    /// flush drains the buffer, so a sequential handler under a large
    /// delay window still completes with all operations recorded.
    #[tokio::test(start_paused = true)]
    async fn checkpoint_delay_completes_sequential_handlers() {
        let result = LocalRunner::new()
            .single_page()
            .options(
                crate::Options::builder()
                    .checkpoint_delay(std::time::Duration::from_millis(20))
                    .build()
                    .expect("valid options"),
            )
            .run(
                |n: i32, ctx: DurableContext| async move {
                    let v = ctx
                        .step(move |_| async move { Ok(n + 1) })
                        .name("inc")
                        .await?;
                    Ok::<_, BoxError>(v)
                },
                41_i32,
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&42));
        // The step's START and SUCCEED records both landed.
        assert!(
            result
                .operations()
                .iter()
                .any(|op| op.status == OperationStatus::Succeeded
                    && op.name.as_deref() == Some("inc")),
            "the step's coalesced checkpoints must be recorded: {:?}",
            result.operations()
        );
    }

    /// A non-retryable failure of a detached batch flush whose only
    /// contributor was dropped before the rejection arrived fails the
    /// execution in this invocation. The failure is recorded where the
    /// response arrives, so no reader is needed. The SDK sends nothing
    /// after it: the later live step's START never reaches the backend,
    /// and no terminal FAIL is written, so the orphan's record stays at its
    /// accepted START.
    #[tokio::test]
    async fn nonretryable_failure_of_dropped_contributor_fails_execution() {
        let runner = LocalRunner::new();
        let backend = Arc::new(Backend::new(
            QueuedReplies::default(),
            runner.checkpoint_page_size,
        ));

        // Call plan:
        //   1. orphan START: passes.
        //   2. orphan SUCCEED (detached batch flush): held at the gate
        //      until the handler releases it, then rejected non-retryably.
        //      The handler drops the contributor while the write is held,
        //      so the rejection has no reader.
        // Nothing is sent after the rejection.
        let gate = Arc::new(tokio::sync::Semaphore::new(0));
        backend.plan_checkpoint_calls(vec![
            PlannedCheckpoint::Pass,
            PlannedCheckpoint::FailNonRetryable {
                gate: Some(Arc::clone(&gate)),
            },
        ]);

        let body_runs = Arc::new(AtomicU32::new(0));
        let body_runs_h = Arc::clone(&body_runs);
        let backend_h = Arc::clone(&backend);
        let gate_h = Arc::clone(&gate);

        let result = runner
            .run_on_backend(
                Arc::clone(&backend),
                move |(), ctx: DurableContext| {
                    let body_runs = Arc::clone(&body_runs_h);
                    let backend = Arc::clone(&backend_h);
                    let gate = Arc::clone(&gate_h);
                    async move {
                        let body_runs_step = Arc::clone(&body_runs);
                        let orphan = ctx
                            .step(move |_| {
                                let body_runs = Arc::clone(&body_runs_step);
                                async move {
                                    body_runs.fetch_add(1, Ordering::SeqCst);
                                    Ok("orphaned-outcome".to_owned())
                                }
                            })
                            .name("orphan")
                            .spawn();

                        // Wait until the orphan's SUCCEED write is IN
                        // FLIGHT (call 2 reached the backend, held at the
                        // gate), then drop the contributor and release the
                        // gate: the rejection has no reader.
                        while backend.checkpoint_call_count() < 2 {
                            tokio::time::sleep(std::time::Duration::from_millis(2)).await;
                        }
                        drop(orphan);
                        gate.add_permits(1);

                        // A later LIVE operation: its START write is never
                        // sent, because the failure ends the drive and
                        // stops every later write.
                        let live: String = ctx
                            .step(|_| async { Ok("live".to_owned()) })
                            .name("live")
                            .await?;
                        Ok::<_, BoxError>(live)
                    }
                },
                "null".to_owned(),
            )
            .await
            .expect("runner");

        assert_eq!(
            body_runs.load(Ordering::SeqCst),
            1,
            "the orphaned body runs exactly once"
        );
        assert_eq!(
            result.invocation_count(),
            1,
            "the non-retryable rejection is deterministic: the execution \
             must die in this invocation, not re-invoke into the same \
             rejection"
        );
        assert!(
            result.is_failure(),
            "the execution must fail; got {:?} / {:?}",
            result.error_type(),
            result.error_message()
        );
        assert_eq!(
            result.error_type(),
            Some(crate::error::CHECKPOINT_EXECUTION_FAILED_ERROR_TYPE)
        );

        // No terminal FAIL: the orphan's record stays at its accepted
        // START, and nothing is sent after the rejection.
        let ops = result.operations();
        let orphan_op = ops
            .iter()
            .find(|op| op.name.as_deref() == Some("orphan"))
            .expect("the orphaned step's operation record exists");
        assert_eq!(orphan_op.status, OperationStatus::Started);
        assert!(
            ops.iter().all(|op| op.name.as_deref() != Some("live")),
            "the live step's START is never sent"
        );
        assert_eq!(backend.checkpoint_call_count(), 2);
    }

    /// The change of a write whose caller dropped it, and that no
    /// operation task owns, is reported before `on_invocation_end`.
    ///
    /// Call 2, the `SUCCEED`, is applied but its answer is held until the
    /// handler dropped the write. So the SDK merges that response only
    /// after the drop, and only the drain before the end hook delivers it.
    #[tokio::test]
    async fn dropped_contributor_change_reported_before_invocation_end() {
        let recorder = crate::plugin::test_support::Changes::default();
        let runner = LocalRunner::new().options(
            crate::Options::builder()
                .plugin(recorder.clone())
                .build()
                .expect("valid options"),
        );
        let backend = Arc::new(Backend::new(
            QueuedReplies::default(),
            runner.checkpoint_page_size,
        ));
        let gate = Arc::new(tokio::sync::Semaphore::new(0));
        backend.plan_checkpoint_calls(vec![
            PlannedCheckpoint::Pass,
            PlannedCheckpoint::PassGated {
                gate: Arc::clone(&gate),
            },
        ]);
        let backend_h = Arc::clone(&backend);
        let gate_h = Arc::clone(&gate);
        let result = runner
            .run_on_backend(
                Arc::clone(&backend),
                move |(), ctx: DurableContext| {
                    let backend = Arc::clone(&backend_h);
                    let gate = Arc::clone(&gate_h);
                    async move {
                        let update = |action: OperationAction| {
                            OperationUpdate::builder()
                                .id("orphan-id")
                                .name("orphan")
                                .r#type(OperationType::Step)
                                .action(action)
                                .build()
                                .expect("valid update")
                        };
                        ctx.checkpoint_updates(vec![update(OperationAction::Start)])
                            .await?;
                        let write =
                            ctx.submit_checkpoint_updates(vec![update(OperationAction::Succeed)]);
                        while backend.checkpoint_call_count() < 2 {
                            tokio::time::sleep(std::time::Duration::from_millis(2)).await;
                        }
                        drop(write);
                        gate.add_permits(1);
                        Ok::<_, BoxError>("done".to_owned())
                    }
                },
                "null".to_owned(),
            )
            .await
            .expect("runner");
        assert_eq!(result.output().map(String::as_str), Some("done"));
        let timeline = recorder.timeline();
        assert!(
            timeline.ends_with(&[
                "change:orphan:SUCCEEDED".to_owned(),
                "invocation_end:Succeeded".to_owned(),
            ]),
            "{timeline:?}"
        );
    }

    /// A non-retryable failure of a detached batch flush whose contributor
    /// was dropped fails the execution, and the plugins' end hook runs
    /// once, with `Failed`. The failure ends the drive before the handler
    /// records an invocation fault, and no terminal FAIL is written, so
    /// the orphan's record stays at its accepted START.
    #[tokio::test]
    async fn end_hook_failed_after_a_dropped_contributor_failure() {
        let plugin = crate::plugin::test_support::Hooks::default();
        let runner = LocalRunner::new().options(
            crate::Options::builder()
                .plugin(plugin.clone())
                .build()
                .expect("valid options"),
        );
        let backend = Arc::new(Backend::new(
            QueuedReplies::default(),
            runner.checkpoint_page_size,
        ));
        // 1. the orphan's START passes. 2. its SUCCEED is held at the gate,
        // then rejected non-retryably after the contributor was dropped.
        let gate = Arc::new(tokio::sync::Semaphore::new(0));
        backend.plan_checkpoint_calls(vec![
            PlannedCheckpoint::Pass,
            PlannedCheckpoint::FailNonRetryable {
                gate: Some(Arc::clone(&gate)),
            },
        ]);
        let backend_h = Arc::clone(&backend);
        let gate_h = Arc::clone(&gate);
        let result = runner
            .run_on_backend(
                Arc::clone(&backend),
                move |(), ctx: DurableContext| {
                    let backend = Arc::clone(&backend_h);
                    let gate = Arc::clone(&gate_h);
                    async move {
                        let orphan = ctx
                            .step(|_| async { Ok("orphaned-outcome".to_owned()) })
                            .name("orphan")
                            .spawn();
                        while backend.checkpoint_call_count() < 2 {
                            tokio::time::sleep(std::time::Duration::from_millis(2)).await;
                        }
                        drop(orphan);
                        gate.add_permits(1);
                        // The rejection ends the drive while this sleeps.
                        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
                        ctx.coordinator().record_invocation_fault(
                            crate::error::CHECKPOINT_INVOCATION_FAILED_ERROR_TYPE,
                            "injected invocation fault".to_owned(),
                        );
                        std::future::pending::<()>().await;
                        Ok::<_, BoxError>(())
                    }
                },
                "null".to_owned(),
            )
            .await
            .expect("runner");
        assert!(result.is_failure(), "{:?}", result.error_message());
        assert_eq!(result.invocation_count(), 1);
        assert_eq!(
            result.error_type(),
            Some(crate::error::CHECKPOINT_EXECUTION_FAILED_ERROR_TYPE)
        );
        let ends = plugin.ends();
        let [end] = ends.as_slice() else {
            unreachable!("one end info, got {}", ends.len());
        };
        assert_eq!(end.status(), crate::InvocationStatus::Failed);
        assert_eq!(end.execution_result(), None);
        assert_eq!(
            end.execution_error().and_then(crate::WireError::error_type),
            Some(crate::error::CHECKPOINT_EXECUTION_FAILED_ERROR_TYPE)
        );
        let orphan = result
            .operations()
            .iter()
            .find(|op| op.name.as_deref() == Some("orphan"))
            .map(|op| op.status);
        assert_eq!(orphan, Some(OperationStatus::Started));
    }

    // Harness fidelity: production wire-error mapping

    /// The runner reports the SAME wire error message production reports.
    /// The execution propagates the child's failure, so it records the
    /// child's recorded message, which is the body's raw message.
    #[tokio::test]
    async fn child_failure_wire_error_matches_production_raw_message() {
        let result: TestResult<i32> = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let v = ctx
                        .run_in_child_context(|_child| async move {
                            Err::<i32, BoxError>("boom-child".into())
                        })
                        .await?;
                    Ok::<_, BoxError>(v)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_failure());
        // The execution record re-records the child-context record's own
        // recorded identity ("Error", the body error's fallback type for a
        // plain boxed error) rather than degrading it to the outer kind's
        // registry name: recorded identity passes through boundaries.
        assert_eq!(result.error_type(), Some("Error"));
        assert_eq!(
            result.error_message(),
            Some("boom-child"),
            "the execution records the child's recorded message"
        );
    }

    // ---- Virtual clock (waits and retry delays end in time order) ----

    /// The shorter of two raced waits wins, because the clock moves to the
    /// earliest end and fires only what is due then.
    #[tokio::test]
    async fn virtual_clock_race_returns_shorter_wait() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let slow = ctx
                        .run_in_child_context(|child| async move {
                            child.wait(std::time::Duration::from_secs(60)).await?;
                            Ok("slow".to_owned())
                        })
                        .name("slow")
                        .future();
                    let fast = ctx
                        .run_in_child_context(|child| async move {
                            child.wait(std::time::Duration::from_secs(1)).await?;
                            Ok("fast".to_owned())
                        })
                        .name("fast")
                        .future();
                    Ok::<_, BoxError>(ctx.race([slow, fast]).await?)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output().map(String::as_str), Some("fast"));
        assert_eq!(result.invocation_count(), 2);
    }

    /// A retry delayed by 300 seconds loses a race against a 1 second wait.
    ///
    /// The test fails on a backend that fires every timer in one advance. Such a
    /// backend fires the retry and the wait together. The next invocation
    /// re-runs the step body while the wait child replays, and the step's
    /// child settles first, so the race
    /// returns `"retried"`.
    #[tokio::test]
    async fn virtual_clock_orders_retry_delay_against_wait() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let retried = ctx
                        .run_in_child_context(|child| async move {
                            child
                                .step(|step_ctx| async move {
                                    if step_ctx.attempt() == 1 {
                                        return Err::<String, BoxError>("first attempt".into());
                                    }
                                    Ok("retried".to_owned())
                                })
                                .retry_strategy(|_err, _attempt| {
                                    RetryDecision::retry(std::time::Duration::from_secs(300))
                                })
                                .await
                                .map_err(BoxError::from)
                        })
                        .future();
                    let waited = ctx
                        .run_in_child_context(|child| async move {
                            child.wait(std::time::Duration::from_secs(1)).await?;
                            Ok("waited".to_owned())
                        })
                        .future();
                    Ok::<_, BoxError>(ctx.race([retried, waited]).await?)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output().map(String::as_str), Some("waited"));
        assert_eq!(result.invocation_count(), 2);
    }

    /// The clock orders timers by due time, not by duration: a 6 second
    /// wait started at 10 ends at 16, after a 15 second wait started at 0.
    #[tokio::test]
    async fn virtual_clock_orders_by_due_time_not_duration() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let a = ctx
                        .run_in_child_context(|child| async move {
                            child.wait(std::time::Duration::from_secs(10)).await?;
                            child.wait(std::time::Duration::from_secs(6)).await?;
                            Ok("a".to_owned())
                        })
                        .name("a")
                        .future();
                    let b = ctx
                        .run_in_child_context(|child| async move {
                            child.wait(std::time::Duration::from_secs(15)).await?;
                            Ok("b".to_owned())
                        })
                        .name("b")
                        .future();
                    Ok::<_, BoxError>(ctx.race([a, b]).await?)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output().map(String::as_str), Some("b"));
        assert_eq!(result.invocation_count(), 3);
    }

    /// Waits due at the same time fire in the same advance.
    #[tokio::test]
    async fn virtual_clock_fires_equal_waits_together() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let first = ctx.wait(std::time::Duration::from_secs(10)).spawn();
                    let second = ctx.wait(std::time::Duration::from_secs(10)).spawn();
                    let (first, second) = tokio::join!(first, second);
                    first?;
                    second?;
                    Ok("done".to_owned())
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output().map(String::as_str), Some("done"));
        assert_eq!(result.invocation_count(), 2);
    }

    /// A queued callback reply arrives before the clock moves, so it wins
    /// a race against an hour-long wait, and the wait never fires.
    #[tokio::test]
    async fn virtual_clock_delivers_callback_before_moving_clock() {
        let result = LocalRunner::new()
            .callback(success(&"cb"))
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<String>().await?;
                    let timeout = ctx
                        .run_in_child_context(|child| async move {
                            child
                                .wait(std::time::Duration::from_secs(3600))
                                .name("hour")
                                .await?;
                            Ok("timeout".to_owned())
                        })
                        .future();
                    Ok::<_, BoxError>(ctx.race([cb.result(), timeout]).await?)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output().map(String::as_str), Some("cb"));
        assert_eq!(result.invocation_count(), 2);
        let hour = result
            .operations()
            .iter()
            .find(|op| op.name() == Some("hour"))
            .expect("the hour wait is recorded");
        assert_eq!(hour.status(), OperationStatus::Started);
    }

    /// A WAIT `START` update with the given duration.
    fn wait_start(id: &str, wait_seconds: i32) -> OperationUpdate {
        OperationUpdate::builder()
            .id(id)
            .r#type(OperationType::Wait)
            .action(OperationAction::Start)
            .wait_options(
                aws_sdk_lambda::types::WaitOptions::builder()
                    .wait_seconds(wait_seconds)
                    .build(),
            )
            .build()
            .expect("valid update")
    }

    /// A STEP `RETRY` update, with `NextAttemptDelaySeconds` when `delay`
    /// is set.
    fn step_retry(id: &str, delay: Option<i32>) -> OperationUpdate {
        let mut builder = OperationUpdate::builder()
            .id(id)
            .r#type(OperationType::Step)
            .action(OperationAction::Retry);
        if let Some(delay) = delay {
            builder = builder.step_options(
                aws_sdk_lambda::types::StepOptions::builder()
                    .next_attempt_delay_seconds(delay)
                    .build(),
            );
        }
        builder.build().expect("valid update")
    }

    /// A CALLBACK `START` update.
    fn callback_start(id: &str) -> OperationUpdate {
        OperationUpdate::builder()
            .id(id)
            .r#type(OperationType::Callback)
            .action(OperationAction::Start)
            .build()
            .expect("valid update")
    }

    /// A backend at virtual time `now`, seeded with `updates`.
    fn seeded_backend(
        callback_replies: Vec<CallbackReply>,
        now: u64,
        updates: &[OperationUpdate],
    ) -> Backend {
        let backend = Backend::new(
            QueuedReplies {
                fifo: callback_replies.into(),
                named: Vec::new(),
            },
            None,
        );
        {
            let mut state = backend.lock();
            state.now = now;
            for update in updates {
                let kind = OperationKind::from_wire(update.r#type()).expect("a known type");
                state.apply_update(kind, update).expect("update applies");
            }
        }
        backend
    }

    /// The status of stored op `id`, and the backend's virtual time.
    fn status_and_now(backend: &Backend, id: &str) -> (OperationStatus, u64) {
        let state = backend.lock();
        let op = state
            .ops
            .iter()
            .find(|op| op.id == id)
            .expect("op is stored");
        (op.status, state.now)
    }

    /// A callback `START` stores `Started`, but `advance` still settles a
    /// callback whose status is `Pending`. This test sets the status to
    /// `Pending` directly, because no update produces it.
    #[test]
    fn advance_settles_pending_callback() {
        let backend = seeded_backend(vec![CallbackReply::raw("\"x\"")], 0, &[callback_start("c")]);
        backend
            .lock()
            .ops
            .iter_mut()
            .find(|op| op.id == "c")
            .expect("op is stored")
            .status = OperationStatus::Pending;
        assert!(backend.advance());
        assert_eq!(
            status_and_now(&backend, "c"),
            (OperationStatus::Succeeded, 0)
        );
    }

    /// `advance` follows the virtual clock: one assertion block per row of
    /// the case table in the issue.
    #[test]
    fn advance_follows_virtual_clock() {
        // Rows 1 and 2: the earlier wait fires first, then the later one.
        let backend = seeded_backend(Vec::new(), 0, &[wait_start("a", 5), wait_start("b", 9)]);
        assert!(backend.advance(), "row 1");
        assert_eq!(
            status_and_now(&backend, "a"),
            (OperationStatus::Succeeded, 5)
        );
        assert_eq!(status_and_now(&backend, "b"), (OperationStatus::Started, 5));
        assert!(backend.advance(), "row 2");
        assert_eq!(
            status_and_now(&backend, "b"),
            (OperationStatus::Succeeded, 9)
        );

        // Row 3: a retry and a wait due at the same time fire together.
        let backend = seeded_backend(
            Vec::new(),
            0,
            &[step_retry("r", Some(3)), wait_start("w", 3)],
        );
        assert!(backend.advance(), "row 3");
        assert_eq!(status_and_now(&backend, "r"), (OperationStatus::Ready, 3));
        assert_eq!(
            status_and_now(&backend, "w"),
            (OperationStatus::Succeeded, 3)
        );

        // Row 4: a queued callback reply arrives and the clock stays put.
        let backend = seeded_backend(
            vec![CallbackReply::raw("\"x\"")],
            0,
            &[callback_start("c"), wait_start("w", 100)],
        );
        assert!(backend.advance(), "row 4");
        assert_eq!(
            status_and_now(&backend, "c"),
            (OperationStatus::Succeeded, 0)
        );
        assert_eq!(status_and_now(&backend, "w"), (OperationStatus::Started, 0));

        // Row 5: nothing to deliver and nothing timed.
        let backend = seeded_backend(Vec::new(), 0, &[callback_start("c")]);
        assert!(!backend.advance(), "row 5");
        assert_eq!(status_and_now(&backend, "c"), (OperationStatus::Started, 0));

        // Row 6: a zero-second wait is due at once.
        let backend = seeded_backend(Vec::new(), 7, &[wait_start("w", 0)]);
        assert!(backend.advance(), "row 6");
        assert_eq!(
            status_and_now(&backend, "w"),
            (OperationStatus::Succeeded, 7)
        );

        // Row 7: negative durations count as 0.
        let backend = seeded_backend(
            Vec::new(),
            7,
            &[wait_start("w", -5), step_retry("r", Some(-5))],
        );
        assert!(backend.advance(), "row 7");
        assert_eq!(
            status_and_now(&backend, "w"),
            (OperationStatus::Succeeded, 7)
        );
        assert_eq!(status_and_now(&backend, "r"), (OperationStatus::Ready, 7));

        // Row 8: a retry without `StepOptions` is due at once.
        let backend = seeded_backend(Vec::new(), 4, &[step_retry("r", None)]);
        assert!(backend.advance(), "row 8");
        assert_eq!(status_and_now(&backend, "r"), (OperationStatus::Ready, 4));
    }

    /// The statuses of the step records of `backend`.
    fn step_statuses(backend: &Backend) -> Vec<OperationStatus> {
        backend
            .snapshot_operations()
            .iter()
            .filter(|op| op.kind() == OperationKind::Step)
            .map(TestOperation::status)
            .collect()
    }

    /// `checkpoint_latency` holds each checkpoint request for the latency
    /// before the backend applies it, and changes nothing else.
    #[tokio::test(start_paused = true)]
    async fn checkpoint_latency_delays_each_checkpoint() {
        let runner = LocalRunner::new().checkpoint_latency(std::time::Duration::from_millis(50));
        let backend = Arc::new(runner.new_backend());

        // The handler runs on its own task, so each step lets that task run
        // before it reads the backend: after it moves the clock, and before
        // the first move, so the handler reaches its first checkpoint.
        let settle = || async {
            for _ in 0..16 {
                tokio::task::yield_now().await;
            }
        };
        let steps = async {
            settle().await;
            tokio::time::advance(std::time::Duration::from_millis(49)).await;
            settle().await;
            assert!(step_statuses(&backend).is_empty(), "START not applied yet");
            tokio::time::advance(std::time::Duration::from_millis(1)).await;
            settle().await;
            assert_eq!(step_statuses(&backend), [OperationStatus::Started]);
            tokio::time::advance(std::time::Duration::from_millis(49)).await;
            settle().await;
            assert_eq!(step_statuses(&backend), [OperationStatus::Started]);
            tokio::time::advance(std::time::Duration::from_millis(1)).await;
            settle().await;
            assert_eq!(step_statuses(&backend), [OperationStatus::Succeeded]);
        };
        let (delayed, ()) = tokio::join!(
            runner.run_on_backend(Arc::clone(&backend), one_step_handler, "null".to_owned()),
            steps
        );
        let delayed = delayed.expect("runner");

        let baseline = LocalRunner::new()
            .run(one_step_handler, ())
            .await
            .expect("runner");
        assert!(delayed.is_success(), "{:?}", delayed.error_message());
        assert_eq!(delayed.output(), baseline.output());
        // Each run reports times from its own start, so the comparison
        // drops the times.
        assert_eq!(
            format!("{:?}", without_times(delayed.operations())),
            format!("{:?}", without_times(baseline.operations()))
        );
    }

    /// `operations` with every reported time removed, so two runs that
    /// started at different wall-clock times compare equal.
    fn without_times(operations: &[TestOperation]) -> Vec<TestOperation> {
        operations
            .iter()
            .cloned()
            .map(|mut op| {
                op.start_time = None;
                op.end_time = None;
                op.step.next_attempt_time = None;
                op.wait.scheduled_end_time = None;
                for event in &mut op.events {
                    event.event_timestamp = None;
                }
                op
            })
            .collect()
    }

    /// With the default zero latency the backend creates no tokio timer, so
    /// a runtime built without the time driver still runs the handler.
    /// `tokio::time::sleep` cannot run on such a runtime, and the run then
    /// never ends. So the run happens on its own thread, and the test fails
    /// when no result arrives within 10 seconds.
    #[test]
    fn zero_checkpoint_latency_needs_no_timer() {
        let (sender, receiver) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let runtime = tokio::runtime::Builder::new_current_thread()
                .build()
                .expect("runtime builds");
            let result = runtime
                .block_on(LocalRunner::new().run(one_step_handler, ()))
                .expect("runner");
            // The receiver is gone only after the test already failed.
            let _ = sender.send((result.is_success(), result.output().copied()));
        });
        let outcome = receiver
            .recv_timeout(std::time::Duration::from_secs(10))
            .expect("the run ends without a time driver");
        assert_eq!(outcome, (true, Some(42)));
    }

    // Issue #90: `ParentId` and `ReplayChildren` in the backend, and pruning.

    /// A map whose results exceed the checkpoint size limit records the
    /// batch with `ReplayChildren`. The wait forces a replay, which must
    /// rebuild the result from the item children.
    #[tokio::test]
    async fn oversized_map_result_replays_with_replay_children() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let items = ctx
                        .map(vec![0_u32, 1], |child, _item, _idx| async move {
                            let big = child
                                .step(|_| async { Ok("x".repeat(150 * 1024)) })
                                .name("big")
                                .await?;
                            Ok(big)
                        })
                        .name("oversized")
                        .await?;
                    ctx.wait(std::time::Duration::from_secs(1))
                        .name("pause")
                        .await?;
                    Ok::<_, BoxError>(items.iter().map(String::len).collect::<Vec<_>>())
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&vec![153_600, 153_600]));
        assert_eq!(result.invocation_count(), 2);
    }

    /// A child context whose result exceeds the checkpoint size limit
    /// records `ReplayChildren`. The wait forces a replay, which must rerun
    /// the child from its recorded steps.
    #[tokio::test]
    async fn oversized_child_result_replays_with_replay_children() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let joined = ctx
                        .run_in_child_context(|child| async move {
                            let a = child
                                .step(|_| async { Ok("x".repeat(150 * 1024)) })
                                .name("a")
                                .await?;
                            let b = child
                                .step(|_| async { Ok("x".repeat(150 * 1024)) })
                                .name("b")
                                .await?;
                            Ok(format!("{a}{b}"))
                        })
                        .name("oversized")
                        .await?;
                    ctx.wait(std::time::Duration::from_secs(1))
                        .name("pause")
                        .await?;
                    Ok::<_, BoxError>(joined.len())
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&307_200));
    }

    // Issue #137: summaries of oversized children and batches.

    /// An oversized child records its summary, and the replay after the
    /// wait runs the body again and ignores the summary.
    #[tokio::test]
    async fn child_summary_ignored_on_replay() {
        let calls = Arc::new(AtomicU32::new(0));
        let handler_calls = Arc::clone(&calls);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let calls = Arc::clone(&handler_calls);
                    async move {
                        let big = ctx
                            .run_in_child_context(|_child| async { Ok("x".repeat(300 * 1024)) })
                            .name("big")
                            .summary(move |s: &String| {
                                calls.fetch_add(1, Ordering::SeqCst);
                                format!("len={}", s.len())
                            })
                            .await?;
                        ctx.wait(std::time::Duration::from_secs(1))
                            .name("pause")
                            .await?;
                        Ok::<_, BoxError>(big.len())
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&307_200));
        assert_eq!(result.invocation_count(), 2);
        assert_eq!(
            calls.load(Ordering::SeqCst),
            1,
            "the replay calls no summary"
        );
        let big = result.operation("big").expect("the child's record");
        assert_eq!(big.result(), Some("len=307200"));
        assert!(
            matches!(big.details(), TestOperationDetails::Context(c) if c.replay_children()),
            "{:?}",
            big.details()
        );
    }

    /// The batch record `m` of an oversized batch, parsed as JSON, with
    /// the assertions every oversized batch summary shares.
    fn oversized_batch_record(result: &TestResult<Vec<usize>>, type_name: &str) {
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&vec![153_600, 153_600]));
        let text = result
            .operation("m")
            .and_then(TestOperation::result)
            .expect("the batch record has a payload");
        let record: serde_json::Value = serde_json::from_str(text).expect("the record is JSON");
        assert_eq!(record["type"], type_name, "{text}");
        assert_eq!(record["summary"], "ok=2", "{text}");
        assert_eq!(record["totalCount"], 2, "{text}");
        assert_eq!(record["itemStatuses"], "SS", "{text}");
    }

    /// An oversized map stores the caller's summary under the key
    /// `summary` of its `MapResult` record, and replays from the record.
    #[tokio::test]
    async fn map_summary_stored_under_summary_key() {
        let calls = Arc::new(AtomicU32::new(0));
        let handler_calls = Arc::clone(&calls);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let calls = Arc::clone(&handler_calls);
                    async move {
                        let items = ctx
                            .map(vec![0_u32, 1], |child, _item, _idx| async move {
                                Ok(child
                                    .step(|_| async { Ok("x".repeat(150 * 1024)) })
                                    .name("big")
                                    .await?)
                            })
                            .name("m")
                            .summary(move |r| {
                                calls.fetch_add(1, Ordering::SeqCst);
                                format!("ok={}", r.success_count())
                            })
                            .await?;
                        ctx.wait(std::time::Duration::from_secs(1))
                            .name("pause")
                            .await?;
                        Ok::<_, BoxError>(items.iter().map(String::len).collect::<Vec<_>>())
                    }
                },
                (),
            )
            .await
            .expect("runner");
        oversized_batch_record(&result, "MapResult");
        assert_eq!(result.invocation_count(), 2);
        assert_eq!(
            calls.load(Ordering::SeqCst),
            1,
            "the write calls the summary once, and the replay calls none"
        );
    }

    /// An oversized parallel stores the caller's summary under the key
    /// `summary` of its `ParallelResult` record.
    #[tokio::test]
    async fn parallel_summary_stored_under_summary_key() {
        let calls = Arc::new(AtomicU32::new(0));
        let handler_calls = Arc::clone(&calls);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let calls = Arc::clone(&handler_calls);
                    async move {
                        let branch = |name: &'static str| {
                            crate::Branch::new(name, |child| async move {
                                Ok(child
                                    .step(|_| async { Ok("x".repeat(150 * 1024)) })
                                    .name("big")
                                    .await?)
                            })
                        };
                        let items = ctx
                            .parallel(vec![branch("a"), branch("b")])
                            .name("m")
                            .summary(move |r| {
                                calls.fetch_add(1, Ordering::SeqCst);
                                format!("ok={}", r.success_count())
                            })
                            .await?;
                        ctx.wait(std::time::Duration::from_secs(1))
                            .name("pause")
                            .await?;
                        Ok::<_, BoxError>(items.iter().map(String::len).collect::<Vec<_>>())
                    }
                },
                (),
            )
            .await
            .expect("runner");
        oversized_batch_record(&result, "ParallelResult");
        assert_eq!(result.invocation_count(), 2);
        assert_eq!(
            calls.load(Ordering::SeqCst),
            1,
            "the write calls the summary once, and the replay calls none"
        );
    }

    /// A batch whose record fits never calls the summary, and records the
    /// normal batch payload.
    #[tokio::test]
    async fn map_summary_not_called_under_limit() {
        let calls = Arc::new(AtomicU32::new(0));
        let handler_calls = Arc::clone(&calls);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let calls = Arc::clone(&handler_calls);
                    async move {
                        let items = ctx
                            .map(vec![0_u32, 1], |_child, item, _idx| async move { Ok(item) })
                            .name("m")
                            .summary(move |_r| {
                                calls.fetch_add(1, Ordering::SeqCst);
                                "unused".to_owned()
                            })
                            .await?;
                        Ok::<_, BoxError>(items)
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&vec![0, 1]));
        assert_eq!(calls.load(Ordering::SeqCst), 0);
        let text = result
            .operation("m")
            .and_then(TestOperation::result)
            .expect("the batch record has a payload");
        let record: serde_json::Value = serde_json::from_str(text).expect("the record is JSON");
        assert_eq!(record["completionReason"], "ALL_COMPLETED", "{text}");
        assert_eq!(record["all"].as_array().map(Vec::len), Some(2), "{text}");
        assert!(record.get("summary").is_none(), "{text}");
    }

    /// A `with_retry` block's summary becomes the payload of the block's
    /// own oversized record.
    #[tokio::test]
    async fn with_retry_summary_forwarded_to_block() {
        let calls = Arc::new(AtomicU32::new(0));
        let handler_calls = Arc::clone(&calls);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let calls = Arc::clone(&handler_calls);
                    async move {
                        let big = ctx
                            .with_retry(|_attempt_ctx, _attempt| async {
                                Ok("x".repeat(300 * 1024))
                            })
                            .name("block")
                            .summary(move |s: &String| {
                                calls.fetch_add(1, Ordering::SeqCst);
                                format!("len={}", s.len())
                            })
                            .await?;
                        Ok::<_, BoxError>(big.len())
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            result.operation("block").and_then(TestOperation::result),
            Some("len=307200")
        );
        // The attempt context records its oversized result without the
        // summary, so the summary ran once, for the block's own record.
        assert_eq!(
            result
                .operation("attempt-1")
                .and_then(TestOperation::result),
            Some("")
        );
        assert_eq!(calls.load(Ordering::SeqCst), 1);
    }

    /// A `with_retry` block whose result fits never calls its summary.
    #[tokio::test]
    async fn with_retry_summary_not_called_under_limit() {
        let calls = Arc::new(AtomicU32::new(0));
        let handler_calls = Arc::clone(&calls);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let calls = Arc::clone(&handler_calls);
                    async move {
                        let value = ctx
                            .with_retry(|_attempt_ctx, _attempt| async { Ok("small".to_owned()) })
                            .name("block")
                            .summary(move |_s: &String| {
                                calls.fetch_add(1, Ordering::SeqCst);
                                "unused".to_owned()
                            })
                            .await?;
                        Ok::<_, BoxError>(value)
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output().map(String::as_str), Some("small"));
        assert_eq!(calls.load(Ordering::SeqCst), 0);
    }

    /// A virtual `with_retry` block writes no record of its own, so its summary
    /// is never called, even for an oversized result.
    #[tokio::test]
    async fn virtual_with_retry_never_calls_summary() {
        let calls = Arc::new(AtomicU32::new(0));
        let handler_calls = Arc::clone(&calls);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let calls = Arc::clone(&handler_calls);
                    async move {
                        let big = ctx
                            .with_retry(|_attempt_ctx, _attempt| async {
                                Ok("x".repeat(300 * 1024))
                            })
                            .name("block")
                            .virtual_context()
                            .summary(move |_s: &String| {
                                calls.fetch_add(1, Ordering::SeqCst);
                                "unused".to_owned()
                            })
                            .await?;
                        Ok::<_, BoxError>(big.len())
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&307_200));
        assert_eq!(calls.load(Ordering::SeqCst), 0);
        assert_eq!(
            result
                .operation("block-attempt-1")
                .and_then(TestOperation::result),
            Some("")
        );
    }

    /// An oversized `Flat` map calls its summary once, when it writes the
    /// record, and the replay after the wait runs the items again to
    /// rebuild the result without calling the summary.
    #[tokio::test]
    async fn flat_map_summary_called_once_and_items_rebuilt() {
        let calls = Arc::new(AtomicU32::new(0));
        let bodies = Arc::new(AtomicU32::new(0));
        let (handler_calls, handler_bodies) = (Arc::clone(&calls), Arc::clone(&bodies));
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let calls = Arc::clone(&handler_calls);
                    let bodies = Arc::clone(&handler_bodies);
                    async move {
                        let items = ctx
                            .map(vec![0_u32, 1], move |child, _item, _idx| {
                                bodies.fetch_add(1, Ordering::SeqCst);
                                async move {
                                    Ok(child.step(|_| async { Ok("x".repeat(150 * 1024)) }).await?)
                                }
                            })
                            .name("m")
                            .nesting(crate::builders::map_parallel::NestingMode::Flat)
                            .summary(move |r| {
                                calls.fetch_add(1, Ordering::SeqCst);
                                format!("ok={}", r.success_count())
                            })
                            .await?;
                        ctx.wait(std::time::Duration::from_secs(1))
                            .name("pause")
                            .await?;
                        Ok::<_, BoxError>(items.iter().map(String::len).collect::<Vec<_>>())
                    }
                },
                (),
            )
            .await
            .expect("runner");

        oversized_batch_record(&result, "MapResult");
        assert_eq!(result.invocation_count(), 2);
        assert_eq!(calls.load(Ordering::SeqCst), 1);
        assert_eq!(
            bodies.load(Ordering::SeqCst),
            4,
            "each Flat item runs once live and once to rebuild the result"
        );
    }

    /// The summary set on a map or parallel reaches the batch record
    /// through `.await_batch()` and `.spawn_batch()`, which build the
    /// batch apart from `.await`.
    #[tokio::test]
    async fn batch_summary_kept_by_await_batch_and_spawn_batch() {
        for (kind, spawn) in [
            ("map", false),
            ("map", true),
            ("parallel", false),
            ("parallel", true),
        ] {
            let result = LocalRunner::new()
                .run(
                    move |(), ctx: DurableContext| async move {
                        let big = || async { Ok("x".repeat(150 * 1024)) };
                        let batch = if kind == "map" {
                            let builder = ctx
                                .map(vec![0_u32, 1], move |child, _item, _idx| async move {
                                    Ok(child.step(move |_| big()).name("big").await?)
                                })
                                .name("m")
                                .summary(|r| format!("ok={}", r.success_count()));
                            if spawn {
                                builder.spawn_batch().await?
                            } else {
                                builder.await_batch().await?
                            }
                        } else {
                            let branch = |name: &'static str| {
                                crate::Branch::new(name, move |child| async move {
                                    Ok(child.step(move |_| big()).name("big").await?)
                                })
                            };
                            let builder = ctx
                                .parallel(vec![branch("a"), branch("b")])
                                .name("m")
                                .summary(|r| format!("ok={}", r.success_count()));
                            if spawn {
                                builder.spawn_batch().await?
                            } else {
                                builder.await_batch().await?
                            }
                        };
                        let items = batch.into_result()?;
                        Ok::<_, BoxError>(items.results().map(String::len).collect::<Vec<_>>())
                    },
                    (),
                )
                .await
                .expect("runner");
            let type_name = if kind == "map" {
                "MapResult"
            } else {
                "ParallelResult"
            };
            oversized_batch_record(&result, type_name);
        }
    }

    /// The service creates a callback in `Started`, so an open callback
    /// reports `OperationStatus::Started`, as under `CloudRunner`.
    #[tokio::test]
    async fn open_callback_reports_started() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<String>().name("approval").await?;
                    let value = cb.result().await?;
                    Ok::<_, BoxError>(value)
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_suspended(), "{:?}", result.error_message());
        let callback = result
            .operations()
            .iter()
            .find(|op| op.kind() == OperationKind::Callback)
            .expect("the callback is recorded");
        assert_eq!(callback.status(), OperationStatus::Started);
    }

    /// One update for the pruning tests. `replay_children` sets the
    /// context option when given.
    fn tree_update(
        id: &str,
        op_type: OperationType,
        action: OperationAction,
        parent: Option<&str>,
        replay_children: Option<bool>,
    ) -> OperationUpdate {
        let is_fail = action == OperationAction::Fail;
        let mut builder = OperationUpdate::builder()
            .id(id)
            .r#type(op_type)
            .action(action);
        if let Some(parent) = parent {
            builder = builder.parent_id(parent);
        }
        if let Some(replay) = replay_children {
            builder =
                builder.context_options(ContextOptions::builder().replay_children(replay).build());
        }
        if is_fail {
            builder = builder.error(ErrorObject::builder().error_message("boom").build());
        }
        builder.build().unwrap()
    }

    fn ctx_op(
        id: &str,
        action: OperationAction,
        parent: Option<&str>,
        replay_children: Option<bool>,
    ) -> OperationUpdate {
        tree_update(id, OperationType::Context, action, parent, replay_children)
    }

    fn step_op(id: &str, action: OperationAction, parent: Option<&str>) -> OperationUpdate {
        tree_update(id, OperationType::Step, action, parent, None)
    }

    /// Applies `updates` to a fresh backend.
    fn tree_backend(updates: &[OperationUpdate]) -> Backend {
        let backend = Backend::new(QueuedReplies::default(), None);
        {
            let mut state = backend.lock();
            for update in updates {
                let kind = OperationKind::from_wire(update.r#type()).expect("a known type");
                state.apply_update(kind, update).expect("update applies");
            }
        }
        backend
    }

    fn envelope_ids(backend: &Backend) -> Vec<String> {
        backend
            .envelope_history()
            .iter()
            .map(|op| op["Id"].as_str().expect("Id is a string").to_owned())
            .collect()
    }

    async fn get_state_ops(backend: &Backend) -> Vec<Operation> {
        backend
            .get_state("arn:test", "token-0", None)
            .await
            .expect("get_state succeeds")
            .operations
    }

    /// Asserts that the envelope history and `get_state` hold exactly
    /// `kept`, and that `snapshot_operations` holds every stored record.
    async fn assert_pruned(updates: &[OperationUpdate], kept: &[&str], all: &[&str]) {
        let backend = tree_backend(updates);
        assert_eq!(envelope_ids(&backend), kept, "envelope history");
        let state_ids: Vec<String> = get_state_ops(&backend)
            .await
            .iter()
            .map(|op| op.id().to_owned())
            .collect();
        assert_eq!(state_ids, kept, "get_state");
        let snapshot_ids: Vec<String> = backend
            .snapshot_operations()
            .iter()
            .map(|op| op.id().to_owned())
            .collect();
        assert_eq!(snapshot_ids, all, "snapshot_operations");
    }

    #[tokio::test]
    async fn prune_replay_children_top_level_kept() {
        assert_pruned(
            &[step_op("S", OperationAction::Succeed, None)],
            &["S"],
            &["S"],
        )
        .await;
    }

    #[tokio::test]
    async fn prune_replay_children_completed_context_omits_child() {
        assert_pruned(
            &[
                ctx_op("C", OperationAction::Succeed, None, Some(false)),
                step_op("S", OperationAction::Succeed, Some("C")),
            ],
            &["C"],
            &["C", "S"],
        )
        .await;
    }

    #[tokio::test]
    async fn prune_replay_children_failed_context_omits_child() {
        assert_pruned(
            &[
                ctx_op("C", OperationAction::Fail, None, Some(false)),
                step_op("S", OperationAction::Fail, Some("C")),
            ],
            &["C"],
            &["C", "S"],
        )
        .await;
    }

    #[tokio::test]
    async fn prune_replay_children_started_context_keeps_child() {
        assert_pruned(
            &[
                ctx_op("C", OperationAction::Start, None, None),
                step_op("S", OperationAction::Succeed, Some("C")),
            ],
            &["C", "S"],
            &["C", "S"],
        )
        .await;
    }

    #[tokio::test]
    async fn prune_replay_children_replay_children_keeps_child() {
        assert_pruned(
            &[
                ctx_op("C", OperationAction::Succeed, None, Some(true)),
                step_op("S", OperationAction::Succeed, Some("C")),
            ],
            &["C", "S"],
            &["C", "S"],
        )
        .await;
    }

    #[tokio::test]
    async fn prune_replay_children_ancestor_omits_replay_children_context() {
        assert_pruned(
            &[
                ctx_op("A", OperationAction::Succeed, None, Some(false)),
                ctx_op("B", OperationAction::Succeed, Some("A"), Some(true)),
                step_op("S", OperationAction::Succeed, Some("B")),
            ],
            &["A"],
            &["A", "B", "S"],
        )
        .await;
    }

    #[tokio::test]
    async fn prune_replay_children_ancestor_omits_started_context() {
        assert_pruned(
            &[
                ctx_op("A", OperationAction::Succeed, None, Some(false)),
                ctx_op("B", OperationAction::Start, Some("A"), None),
                step_op("S", OperationAction::Succeed, Some("B")),
            ],
            &["A"],
            &["A", "B", "S"],
        )
        .await;
    }

    #[tokio::test]
    async fn prune_replay_children_missing_parent_kept() {
        assert_pruned(
            &[step_op("S", OperationAction::Succeed, Some("ghost"))],
            &["S"],
            &["S"],
        )
        .await;
    }

    #[tokio::test]
    async fn prune_replay_children_cyclic_parents_terminate() {
        assert_pruned(
            &[
                ctx_op("X", OperationAction::Start, Some("Y"), None),
                ctx_op("Y", OperationAction::Start, Some("X"), None),
            ],
            &["X", "Y"],
            &["X", "Y"],
        )
        .await;
    }

    /// The service does not prune checkpoint responses, and the SDK merges
    /// a response into its log. So the response keeps a child that the
    /// same call's context close would omit from the invocation input.
    #[tokio::test]
    async fn checkpoint_response_is_not_pruned() {
        let backend = Backend::new(QueuedReplies::default(), None);
        let output = backend
            .checkpoint(
                "arn:test",
                "token-0",
                vec![
                    ctx_op("C", OperationAction::Start, None, None),
                    step_op("S", OperationAction::Succeed, Some("C")),
                    ctx_op("C", OperationAction::Succeed, None, None),
                ],
            )
            .await
            .expect("checkpoint succeeds");
        let ids: Vec<&str> = output
            .updated_operations
            .iter()
            .map(Operation::id)
            .collect();
        assert_eq!(ids, ["C", "S", "C"]);
    }

    /// A paginated checkpoint response returns the first page of the full
    /// state. That page is not pruned either. So with a page size of 2 the
    /// page holds `C` and its child `S`, which were stored first, and not
    /// the top-level step `T`.
    #[tokio::test]
    async fn paginated_checkpoint_response_is_not_pruned() {
        let backend = Backend::new(QueuedReplies::default(), Some(2));
        let output = backend
            .checkpoint(
                "arn:test",
                "token-0",
                vec![
                    ctx_op("C", OperationAction::Start, None, None),
                    step_op("S", OperationAction::Succeed, Some("C")),
                    ctx_op("C", OperationAction::Succeed, None, None),
                    step_op("T", OperationAction::Succeed, None),
                ],
            )
            .await
            .expect("checkpoint succeeds");
        assert!(output.next_marker.is_some(), "the response is paginated");
        let ids: Vec<&str> = output
            .updated_operations
            .iter()
            .map(Operation::id)
            .collect();
        assert_eq!(ids, ["C", "S"]);
    }

    /// A context closed with `FAIL` and `ReplayChildren: true` records the
    /// option. So both renderers carry it, and both keep the child.
    #[tokio::test]
    async fn failed_context_with_replay_children_keeps_child() {
        let updates = [
            ctx_op("C", OperationAction::Start, None, None),
            step_op("S", OperationAction::Fail, Some("C")),
            ctx_op("C", OperationAction::Fail, None, Some(true)),
        ];
        assert_pruned(&updates, &["C", "S"], &["C", "S"]).await;

        let backend = tree_backend(&updates);
        let history = backend.envelope_history();
        let context = history.iter().find(|op| op["Id"] == "C").expect("C");
        assert_eq!(context["Status"], "FAILED");
        assert_eq!(context["ContextDetails"]["ReplayChildren"], true);

        let ops = get_state_ops(&backend).await;
        let context = ops.iter().find(|op| op.id() == "C").expect("C");
        assert_eq!(context.status(), &WireOperationStatus::Failed);
        assert_eq!(
            context
                .context_details()
                .and_then(WireContextDetails::replay_children),
            Some(true)
        );
    }

    #[tokio::test]
    async fn parent_id_and_replay_children_are_rendered() {
        let backend = tree_backend(&[
            ctx_op("C", OperationAction::Start, None, None),
            step_op("S", OperationAction::Succeed, Some("C")),
            ctx_op("C", OperationAction::Succeed, None, Some(true)),
        ]);
        let history = backend.envelope_history();
        let context = history.iter().find(|op| op["Id"] == "C").expect("C");
        let step = history.iter().find(|op| op["Id"] == "S").expect("S");
        assert_eq!(step["ParentId"], "C");
        assert!(context.get("ParentId").is_none());
        assert_eq!(context["ContextDetails"]["ReplayChildren"], true);

        let ops = get_state_ops(&backend).await;
        let context = ops.iter().find(|op| op.id() == "C").expect("C");
        let step = ops.iter().find(|op| op.id() == "S").expect("S");
        assert_eq!(step.parent_id(), Some("C"));
        assert_eq!(context.parent_id(), None);
        assert_eq!(
            context
                .context_details()
                .and_then(WireContextDetails::replay_children),
            Some(true)
        );
    }

    #[tokio::test]
    async fn parent_id_is_set_once() {
        let backend = tree_backend(&[
            step_op("S", OperationAction::Start, Some("P")),
            step_op("S", OperationAction::Succeed, Some("Q")),
        ]);
        assert_eq!(backend.envelope_history()[0]["ParentId"], "P");
        assert_eq!(get_state_ops(&backend).await[0].parent_id(), Some("P"));
    }

    #[tokio::test]
    async fn replay_children_set_only_on_context_close() {
        let backend = tree_backend(&[
            ctx_op("C", OperationAction::Start, None, Some(true)),
            step_op("S", OperationAction::Succeed, Some("C")),
        ]);
        let context_has_replay_children = |history: &[serde_json::Value]| {
            history
                .iter()
                .find(|op| op["Id"] == "C")
                .expect("C")
                .get("ContextDetails")
                .and_then(|details| details.get("ReplayChildren"))
                .is_some()
        };
        // A START ignores the option.
        assert!(!context_has_replay_children(&backend.envelope_history()));
        assert_eq!(
            get_state_ops(&backend).await[0]
                .context_details()
                .and_then(WireContextDetails::replay_children),
            None
        );

        // A close without the option records `false`, so the child goes.
        backend
            .lock()
            .apply_update(
                OperationKind::Context,
                &ctx_op("C", OperationAction::Succeed, None, None),
            )
            .expect("update applies");
        assert!(!context_has_replay_children(&backend.envelope_history()));
        assert_eq!(envelope_ids(&backend), ["C"]);
        let ops = get_state_ops(&backend).await;
        assert_eq!(ops.iter().map(Operation::id).collect::<Vec<_>>(), ["C"]);
        assert_eq!(
            ops[0]
                .context_details()
                .and_then(WireContextDetails::replay_children),
            None
        );
    }

    #[test]
    fn envelope_uses_service_type_spelling() {
        let backend = tree_backend(&[
            tree_update("s", OperationType::Step, OperationAction::Start, None, None),
            tree_update("w", OperationType::Wait, OperationAction::Start, None, None),
            tree_update(
                "c",
                OperationType::Context,
                OperationAction::Start,
                None,
                None,
            ),
            tree_update(
                "b",
                OperationType::Callback,
                OperationAction::Start,
                None,
                None,
            ),
            tree_update(
                "i",
                OperationType::ChainedInvoke,
                OperationAction::Start,
                None,
                None,
            ),
        ]);
        let envelope = build_envelope(&backend, "{}", None);
        let ops = envelope["InitialExecutionState"]["Operations"]
            .as_array()
            .expect("Operations is an array");
        let types: Vec<&str> = ops
            .iter()
            .map(|op| op["Type"].as_str().expect("Type is a string"))
            .collect();
        assert_eq!(
            types,
            [
                "EXECUTION",
                "STEP",
                "WAIT",
                "CONTEXT",
                "CALLBACK",
                "CHAINED_INVOKE"
            ]
        );
        assert_eq!(ops[0]["Status"], "STARTED");
    }

    /// An update of `op_type` and `action` with id `id`, carrying `payload`
    /// when given (service-limit tests).
    fn limit_update(
        id: &str,
        op_type: OperationType,
        action: OperationAction,
        payload: Option<String>,
    ) -> OperationUpdate {
        OperationUpdate::builder()
            .id(id)
            .r#type(op_type)
            .action(action)
            .set_payload(payload)
            .build()
            .expect("valid update")
    }

    /// A STEP `FAIL` update whose error message is `message`, carrying
    /// `payload` when given.
    fn step_fail_with(id: &str, message: String, payload: Option<String>) -> OperationUpdate {
        OperationUpdate::builder()
            .id(id)
            .r#type(OperationType::Step)
            .action(OperationAction::Fail)
            .set_payload(payload)
            .error(ErrorObject::builder().error_message(message).build())
            .build()
            .expect("valid update")
    }

    /// The model-pass entry for the value `value` at update `pos` and
    /// field `path`.
    fn delay_entry(pos: usize, path: &str, value: i32) -> String {
        let constraint = if value < 1 {
            "greater than or equal to 1"
        } else {
            "less than or equal to 31622400"
        };
        format!(
            "Value '{value}' at 'updates.{pos}.member.{path}' failed to satisfy \
             constraint: Member must have value {constraint}"
        )
    }

    const WAIT_PATH: &str = "waitOptions.waitSeconds";
    const RETRY_PATH: &str = "stepOptions.nextAttemptDelaySeconds";

    /// The single-entry message of the model pass.
    fn one_error(entry: &str) -> String {
        format!("1 validation error detected: {entry}")
    }

    /// A STEP `FAIL` update with a 1 000-byte error and a payload that
    /// makes the whole rendering `total` bytes.
    fn step_fail_rendered_at(total: usize) -> OperationUpdate {
        let message = "e".repeat(1_000);
        let base = render_update(&step_fail_with("f", message.clone(), Some(String::new()))).len();
        let update = step_fail_with("f", message, Some("x".repeat(total - base)));
        assert_eq!(render_update(&update).len(), total);
        update
    }

    /// Sends `updates` in one checkpoint call to a fresh backend. With
    /// `expected` set, asserts the rejection message, that nothing was
    /// stored and that the token did not change. Without it, asserts the
    /// call succeeded and stored one record per distinct update ID.
    async fn check_limit_row(row: usize, updates: Vec<OperationUpdate>, expected: Option<String>) {
        let backend = seeded_backend(Vec::new(), 0, &[]);
        let token = backend.current_token();
        let ids: std::collections::HashSet<String> =
            updates.iter().map(|u| u.id().to_owned()).collect();
        let result =
            ExecutionClient::checkpoint(&backend, LOCAL_EXECUTION_ARN, &token, updates).await;
        let error = result.err().map(|err| err.to_string());
        if let Some(expected) = expected {
            assert_eq!(
                error,
                Some(format!("durable: client: {expected}")),
                "row {row}"
            );
            assert!(backend.snapshot_operations().is_empty(), "row {row}");
            assert_eq!(backend.current_token(), token, "row {row}");
        } else {
            assert_eq!(error, None, "row {row}");
            assert_eq!(backend.snapshot_operations().len(), ids.len(), "row {row}");
        }
    }

    /// The delay rows of the case table: rows 1 to 7 and 19 to 21.
    fn delay_rows() -> Vec<(usize, Vec<OperationUpdate>, Option<String>)> {
        let retry_with_wait = OperationUpdate::builder()
            .id("r")
            .r#type(OperationType::Step)
            .action(OperationAction::Retry)
            .wait_options(
                aws_sdk_lambda::types::WaitOptions::builder()
                    .wait_seconds(0)
                    .build(),
            )
            .step_options(
                aws_sdk_lambda::types::StepOptions::builder()
                    .next_attempt_delay_seconds(0)
                    .build(),
            )
            .build()
            .expect("valid update");
        let eleven: Vec<OperationUpdate> =
            (1..=11).map(|i| wait_start(&format!("w{i}"), 0)).collect();
        let ten: Vec<String> = (1..=10).map(|pos| delay_entry(pos, WAIT_PATH, 0)).collect();
        let step_start = limit_update("s", OperationType::Step, OperationAction::Start, None);
        let big_step = limit_update(
            "s",
            OperationType::Step,
            OperationAction::Succeed,
            Some("x".repeat(262_145)),
        );
        vec![
            (
                1,
                vec![wait_start("w", 0)],
                Some(one_error(&delay_entry(1, WAIT_PATH, 0))),
            ),
            (2, vec![wait_start("w", 1)], None),
            (3, vec![wait_start("w", 31_622_400)], None),
            (
                4,
                vec![wait_start("w", 31_622_401)],
                Some(one_error(&delay_entry(1, WAIT_PATH, 31_622_401))),
            ),
            (
                5,
                vec![step_retry("r", Some(0))],
                Some(one_error(&delay_entry(1, RETRY_PATH, 0))),
            ),
            (
                6,
                vec![step_start, wait_start("w", 0)],
                Some(one_error(&delay_entry(2, WAIT_PATH, 0))),
            ),
            (
                7,
                vec![big_step, wait_start("w", 0)],
                Some(one_error(&delay_entry(2, WAIT_PATH, 0))),
            ),
            (
                19,
                vec![wait_start("a", 0), wait_start("b", 31_622_401)],
                Some(format!(
                    "2 validation errors detected: {}; {}",
                    delay_entry(1, WAIT_PATH, 0),
                    delay_entry(2, WAIT_PATH, 31_622_401)
                )),
            ),
            (
                20,
                eleven,
                Some(format!("10 validation errors detected: {}", ten.join("; "))),
            ),
            (
                21,
                vec![retry_with_wait],
                Some(format!(
                    "2 validation errors detected: {}; {}",
                    delay_entry(1, WAIT_PATH, 0),
                    delay_entry(1, RETRY_PATH, 0)
                )),
            ),
        ]
    }

    /// The size rows of the case table: rows 8 to 18.
    fn size_rows() -> Vec<(usize, Vec<OperationUpdate>, Option<String>)> {
        let step = |payload: String| {
            vec![limit_update(
                "s",
                OperationType::Step,
                OperationAction::Succeed,
                Some(payload),
            )]
        };
        let step_message =
            "STEP output payload size must be less than or equal to 262144 bytes.".to_owned();
        let error_message =
            "Error object size must be less than or equal to 262144 bytes.".to_owned();
        let one = |op_type: OperationType, action: OperationAction, len: usize| {
            vec![limit_update("o", op_type, action, Some("x".repeat(len)))]
        };
        vec![
            (8, step("x".repeat(262_144)), None),
            (9, step("x".repeat(262_145)), Some(step_message.clone())),
            (10, step("é".repeat(131_073)), Some(step_message)),
            (
                11,
                one(OperationType::Context, OperationAction::Succeed, 262_145),
                Some("CONTEXT output payload size must be less than or equal to 262144 bytes.".to_owned()),
            ),
            (12, one(OperationType::ChainedInvoke, OperationAction::Start, 1_048_576), None),
            (
                13,
                one(OperationType::ChainedInvoke, OperationAction::Start, 1_048_577),
                Some(
                    "CHAINED_INVOKE input payload size must be less than or equal to 1048576 bytes."
                        .to_owned(),
                ),
            ),
            (
                14,
                one(OperationType::Execution, OperationAction::Succeed, 6_291_457),
                Some("Execution output payload size must be less than or equal to 6291456 bytes.".to_owned()),
            ),
            (
                15,
                vec![step_fail_with("f", "e".repeat(262_145), None)],
                Some(error_message.clone()),
            ),
            (16, one(OperationType::Callback, OperationAction::Start, 300_000), None),
            (17, vec![step_fail_rendered_at(262_145)], Some(error_message)),
            (18, vec![step_fail_rendered_at(262_144)], None),
        ]
    }

    /// The backend applies the service limits: one call per row of the
    /// case table in the issue.
    #[tokio::test]
    async fn service_limits_reject_each_case() {
        let mut rows = delay_rows();
        rows.extend(size_rows());
        rows.sort_by_key(|(row, _, _)| *row);
        assert_eq!(
            rows.iter().map(|(row, _, _)| *row).collect::<Vec<_>>(),
            (1..=21).collect::<Vec<_>>()
        );
        for (row, updates, expected) in rows {
            check_limit_row(row, updates, expected).await;
        }
    }

    /// Row 17: the error alone is under the limit, but the whole update is
    /// measured, so the update is rejected.
    #[tokio::test]
    async fn error_under_limit_whole_update_over_is_rejected() {
        check_limit_row(
            17,
            vec![step_fail_rendered_at(262_145)],
            Some("Error object size must be less than or equal to 262144 bytes.".to_owned()),
        )
        .await;
    }

    /// Row 18: a whole update of exactly the limit is accepted.
    #[tokio::test]
    async fn whole_update_at_limit_is_accepted() {
        check_limit_row(18, vec![step_fail_rendered_at(262_144)], None).await;
    }

    /// A step result over the STEP payload limit fails the execution with
    /// the service's message.
    #[tokio::test]
    async fn oversized_step_result_fails_execution() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let out = ctx.step(|_| async { Ok("x".repeat(300_000)) }).await?;
                    Ok::<_, BoxError>(out.len())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_failure(), "{:?}", result.error_message());
        let message = result.error_message().unwrap_or_default();
        assert!(
            message
                .contains("STEP output payload size must be less than or equal to 262144 bytes."),
            "{message}"
        );
    }

    /// A step result whose JSON is exactly the STEP payload limit succeeds.
    #[tokio::test]
    async fn step_result_at_the_limit_succeeds() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let out = ctx.step(|_| async { Ok("x".repeat(262_142)) }).await?;
                    Ok::<_, BoxError>(out.len())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
    }

    /// The rendering of rule 3.1 holds every present member under its wire
    /// name, keeps an empty options object, and omits absent members and a
    /// callback timeout of 0.
    #[test]
    fn render_update_renders_every_present_member() {
        let full = OperationUpdate::builder()
            .id("i")
            .parent_id("p")
            .name("n")
            .r#type(OperationType::Step)
            .sub_type("st")
            .action(OperationAction::Fail)
            .payload("pl")
            .error(
                ErrorObject::builder()
                    .error_type("et")
                    .error_message("em")
                    .error_data("ed")
                    .stack_trace("s1")
                    .build(),
            )
            .context_options(ContextOptions::builder().replay_children(true).build())
            .step_options(
                aws_sdk_lambda::types::StepOptions::builder()
                    .next_attempt_delay_seconds(2)
                    .build(),
            )
            .wait_options(
                aws_sdk_lambda::types::WaitOptions::builder()
                    .wait_seconds(3)
                    .build(),
            )
            .callback_options(
                aws_sdk_lambda::types::CallbackOptions::builder()
                    .timeout_seconds(4)
                    .heartbeat_timeout_seconds(5)
                    .build(),
            )
            .chained_invoke_options(
                aws_sdk_lambda::types::ChainedInvokeOptions::builder()
                    .function_name("fn")
                    .tenant_id("t")
                    .build()
                    .expect("valid options"),
            )
            .build()
            .expect("valid update");
        let rendered: serde_json::Value =
            serde_json::from_str(&render_update(&full)).expect("rendering is JSON");
        assert_eq!(
            rendered,
            serde_json::json!({
                "Id": "i", "ParentId": "p", "Name": "n", "Type": "STEP",
                "SubType": "st", "Action": "FAIL", "Payload": "pl",
                "Error": {
                    "ErrorType": "et", "ErrorMessage": "em",
                    "ErrorData": "ed", "StackTrace": ["s1"]
                },
                "ContextOptions": { "ReplayChildren": true },
                "StepOptions": { "NextAttemptDelaySeconds": 2 },
                "WaitOptions": { "WaitSeconds": 3 },
                "CallbackOptions": { "TimeoutSeconds": 4, "HeartbeatTimeoutSeconds": 5 },
                "ChainedInvokeOptions": { "FunctionName": "fn", "TenantId": "t" }
            })
        );

        let sparse = OperationUpdate::builder()
            .id("i")
            .r#type(OperationType::Callback)
            .action(OperationAction::Start)
            .error(ErrorObject::builder().build())
            .context_options(ContextOptions::builder().build())
            .step_options(aws_sdk_lambda::types::StepOptions::builder().build())
            .wait_options(aws_sdk_lambda::types::WaitOptions::builder().build())
            .callback_options(aws_sdk_lambda::types::CallbackOptions::builder().build())
            .chained_invoke_options(
                aws_sdk_lambda::types::ChainedInvokeOptions::builder()
                    .function_name("fn")
                    .build()
                    .expect("valid options"),
            )
            .build()
            .expect("valid update");
        let rendered: serde_json::Value =
            serde_json::from_str(&render_update(&sparse)).expect("rendering is JSON");
        assert_eq!(
            rendered,
            serde_json::json!({
                "Id": "i", "Type": "CALLBACK", "Action": "START", "Error": {},
                "ContextOptions": {}, "StepOptions": {}, "WaitOptions": {},
                "CallbackOptions": {}, "ChainedInvokeOptions": { "FunctionName": "fn" }
            })
        );
    }

    // History rules: identity, actions, statuses, parents and repeated IDs.

    /// A builder for an update of `op_type` and `action` with id `id`
    /// (history-rule tests).
    fn rule_update(
        id: &str,
        op_type: OperationType,
        action: OperationAction,
    ) -> aws_sdk_lambda::types::builders::OperationUpdateBuilder {
        OperationUpdate::builder()
            .id(id)
            .r#type(op_type)
            .action(action)
    }

    /// Builds the update of `builder`.
    fn built(builder: aws_sdk_lambda::types::builders::OperationUpdateBuilder) -> OperationUpdate {
        builder.build().expect("valid update")
    }

    /// The text that follows the field name in every identity message.
    const INCONSISTENT: &str = " value in OperationUpdate is inconsistent with existing operation. Check for non-determinism.";

    /// Seeds a backend with `seed`, sets each `(id, status)` of `statuses`
    /// on the stored op, and sends `updates` in one checkpoint call.
    ///
    /// With `expected` set, asserts the call fails with a non-retryable
    /// error carrying that message, and that the stored operations, the
    /// token and the history did not change. Without it, asserts the call
    /// succeeded.
    async fn check_rule_row(
        row: &str,
        seed: &[OperationUpdate],
        statuses: &[(&str, OperationStatus)],
        updates: Vec<OperationUpdate>,
        expected: Option<&str>,
    ) {
        let backend = seeded_backend(Vec::new(), 0, seed);
        {
            let mut state = backend.lock();
            for (id, status) in statuses {
                let op = state.find_mut(id).expect("the op is seeded");
                op.status = *status;
            }
        }
        let token = backend.current_token();
        let ops_before = format!("{:?}", backend.lock().ops);
        let events_before = format!("{:?}", backend.events());
        let result =
            ExecutionClient::checkpoint(&backend, LOCAL_EXECUTION_ARN, &token, updates).await;
        match expected {
            Some(expected) => {
                assert!(result.is_err(), "row {row}: accepted");
                let err = result.expect_err("the call is rejected");
                assert_eq!(
                    err.to_string(),
                    format!("durable: client: {expected}"),
                    "row {row}"
                );
                assert!(!err.is_retryable(), "row {row}");
                assert_eq!(format!("{:?}", backend.lock().ops), ops_before, "row {row}");
                assert_eq!(backend.current_token(), token, "row {row}");
                assert_eq!(
                    format!("{:?}", backend.events()),
                    events_before,
                    "row {row}"
                );
            }
            None => {
                assert_eq!(result.err().map(|e| e.to_string()), None, "row {row}");
            }
        }
    }

    /// The case of #147: a STEP update reaches the ID of a map item's
    /// CONTEXT record. The backend rejects it, and keeps the record.
    #[tokio::test]
    async fn step_update_at_a_context_id_is_rejected() {
        let seed = [
            built(
                rule_update("map", OperationType::Context, OperationAction::Start).sub_type("Map"),
            ),
            built(
                rule_update("item", OperationType::Context, OperationAction::Start)
                    .sub_type("MapIteration")
                    .name("map-item-0")
                    .parent_id("map"),
            ),
        ];
        let update = built(
            rule_update("item", OperationType::Step, OperationAction::Succeed)
                .sub_type("Step")
                .name("second")
                .payload("2"),
        );
        check_rule_row(
            "step at context",
            &seed,
            &[],
            vec![update],
            Some(&format!("Type{INCONSISTENT}")),
        )
        .await;
    }

    /// JS's message for an action outside the type's set.
    const INVALID_ACTION: &str = "Invalid action for the given operation type.";
    /// JS's message for a bad parent.
    const INVALID_PARENT: &str = "Invalid parent operation id.";
    /// JS's message for a forbidden repeated ID.
    const REPEATED_ID: &str = "Cannot update the same operation twice in a single request.";
    /// JS's message for a WAIT `CANCEL` that finds no started WAIT.
    const CANCEL_WAIT: &str =
        "Cannot cancel a WAIT operation that does not exist or has already completed.";

    /// Runs each `(row, updates, expected)` of `rows` on its own backend
    /// seeded with `seed` (see [`check_rule_row`]).
    async fn check_rule_rows(
        seed: &[OperationUpdate],
        rows: Vec<(&str, Vec<OperationUpdate>, Option<String>)>,
    ) {
        for (row, updates, expected) in rows {
            check_rule_row(row, seed, &[], updates, expected.as_deref()).await;
        }
    }

    /// The identity message for `field`.
    fn inconsistent(field: &str) -> String {
        format!("{field}{INCONSISTENT}")
    }

    /// Test 1.1: each identity field equal, changed, and omitted, and the
    /// order in which the fields are checked.
    #[tokio::test]
    async fn identity_rules() {
        use OperationAction as A;
        use OperationType as T;
        let seed = [
            built(rule_update("p", T::Context, A::Start)),
            built(rule_update("q", T::Context, A::Start)),
            built(
                rule_update("c", T::Context, A::Start)
                    .sub_type("Child")
                    .name("n")
                    .parent_id("p"),
            ),
            built(rule_update("bare", T::Context, A::Start)),
        ];
        let close = |id: &str| rule_update(id, T::Context, A::Succeed);
        let rows = vec![
            (
                "all equal",
                vec![built(close("c").sub_type("Child").name("n").parent_id("p"))],
                None,
            ),
            ("all omitted", vec![built(close("c"))], None),
            (
                "type changed",
                vec![built(rule_update("c", T::Step, A::Succeed))],
                Some(inconsistent("Type")),
            ),
            (
                "subtype changed",
                vec![built(close("c").sub_type("Other"))],
                Some(inconsistent("SubType")),
            ),
            (
                "subtype set on a record without one",
                vec![built(close("bare").sub_type("Child"))],
                Some(inconsistent("SubType")),
            ),
            (
                "name changed",
                vec![built(close("c").name("m"))],
                Some(inconsistent("Name")),
            ),
            (
                "name set on a record without one",
                vec![built(close("bare").name("n"))],
                Some(inconsistent("Name")),
            ),
            (
                "parent changed",
                vec![built(close("c").parent_id("q"))],
                Some(inconsistent("ParentId")),
            ),
            (
                "parent set on a record without one",
                vec![built(close("bare").parent_id("p"))],
                Some(inconsistent("ParentId")),
            ),
            (
                "type before name",
                vec![built(rule_update("c", T::Step, A::Succeed).name("m"))],
                Some(inconsistent("Type")),
            ),
            (
                "subtype before name",
                vec![built(close("c").sub_type("Other").name("m"))],
                Some(inconsistent("SubType")),
            ),
            (
                "name before parent",
                vec![built(close("c").name("m").parent_id("q"))],
                Some(inconsistent("Name")),
            ),
            (
                "identity before action",
                vec![built(rule_update("c", T::Context, A::Retry).name("m"))],
                Some(inconsistent("Name")),
            ),
            (
                "an earlier update of the request is not a stored record",
                vec![
                    built(rule_update("new", T::Step, A::Start).name("a")),
                    built(rule_update("new", T::Step, A::Succeed).name("b")),
                ],
                None,
            ),
        ];
        check_rule_rows(&seed, rows).await;
    }

    /// Test 1.2: every action of every type, with no stored operation.
    #[tokio::test]
    async fn action_sets() {
        use OperationAction as A;
        use OperationType as T;
        // For each type: the actions it allows, and the message of an
        // allowed action that needs a stored operation.
        type Row<'a> = (T, &'a [A], Option<(A, &'a str)>);
        let actions = [A::Start, A::Succeed, A::Fail, A::Retry, A::Cancel];
        let table: [Row<'_>; 6] = [
            (T::Step, &[A::Start, A::Succeed, A::Fail, A::Retry], None),
            (T::Context, &[A::Start, A::Succeed, A::Fail], None),
            (
                T::Wait,
                &[A::Start, A::Cancel],
                Some((A::Cancel, CANCEL_WAIT)),
            ),
            (T::Callback, &[A::Start], None),
            (T::ChainedInvoke, &[A::Start], None),
            (T::Execution, &[A::Succeed, A::Fail], None),
        ];
        for (op_type, allowed, needs_record) in table {
            for action in &actions {
                let expected = if allowed.contains(action) {
                    needs_record
                        .as_ref()
                        .filter(|(a, _)| a == action)
                        .map(|(_, message)| (*message).to_owned())
                } else {
                    Some(INVALID_ACTION.to_owned())
                };
                let row = format!("{op_type:?} {action:?}");
                let update = built(rule_update("o", op_type.clone(), action.clone()));
                check_rule_row(&row, &[], &[], vec![update], expected.as_deref()).await;
            }
        }
    }

    /// The action is checked before the status: a forbidden action on a
    /// stored operation gets the action message, not the status message.
    #[tokio::test]
    async fn action_is_checked_before_status() {
        let seed = [built(rule_update(
            "s",
            OperationType::Step,
            OperationAction::Succeed,
        ))];
        let update = built(rule_update(
            "s",
            OperationType::Step,
            OperationAction::Cancel,
        ));
        check_rule_row(
            "cancel a step",
            &seed,
            &[],
            vec![update],
            Some(INVALID_ACTION),
        )
        .await;
    }

    /// Test 1.3: every allowed action of every type against every status
    /// of a stored operation.
    #[tokio::test]
    async fn status_transitions() {
        use OperationAction as A;
        use OperationStatus as S;
        use OperationType as T;
        let statuses = [
            S::Started,
            S::Pending,
            S::Ready,
            S::Succeeded,
            S::Failed,
            S::Cancelled,
            S::TimedOut,
            S::Stopped,
        ];
        // For each type and action: the statuses that allow it, and JS's
        // message for every other status.
        let table: [(T, A, &[S], Option<&str>); 12] = [
            (
                T::Step,
                A::Start,
                &[S::Ready],
                Some("Invalid current STEP state to start."),
            ),
            (
                T::Step,
                A::Succeed,
                &[S::Started, S::Ready],
                Some("Invalid current STEP state to close."),
            ),
            (
                T::Step,
                A::Fail,
                &[S::Started, S::Ready],
                Some("Invalid current STEP state to close."),
            ),
            (
                T::Step,
                A::Retry,
                &[S::Started, S::Ready],
                Some("Invalid current STEP state to retry."),
            ),
            (
                T::Context,
                A::Start,
                &[],
                Some("Cannot start a CONTEXT operation that already exists."),
            ),
            (
                T::Context,
                A::Succeed,
                &[S::Started],
                Some("Invalid current CONTEXT state to close."),
            ),
            (
                T::Context,
                A::Fail,
                &[S::Started],
                Some("Invalid current CONTEXT state to close."),
            ),
            (
                T::Wait,
                A::Start,
                &[],
                Some("Cannot start a WAIT operation that already exists."),
            ),
            (T::Wait, A::Cancel, &[S::Started], Some(CANCEL_WAIT)),
            (
                T::Callback,
                A::Start,
                &[],
                Some("Cannot start a CALLBACK operation that already exists."),
            ),
            (
                T::ChainedInvoke,
                A::Start,
                &[],
                Some("Cannot start a CHAINED_INVOKE operation that already exists."),
            ),
            (T::Execution, A::Succeed, &statuses, None),
        ];
        for (op_type, action, allowed, message) in table {
            // The backend stores no `EXECUTION` record (it keeps the
            // payload apart), so an `EXECUTION` update never meets a
            // stored status.
            if op_type == T::Execution {
                let update = built(rule_update("o", op_type.clone(), action.clone()));
                check_rule_row("Execution Succeed", &[], &[], vec![update], message).await;
                continue;
            }
            // The seed creates the record. Its status is then set directly.
            let seed = [built(rule_update("o", op_type.clone(), A::Start))];
            for status in statuses {
                let expected = (!allowed.contains(&status)).then_some(message).flatten();
                let row = format!("{op_type:?} {action:?} at {status:?}");
                let update = built(rule_update("o", op_type.clone(), action.clone()));
                check_rule_row(&row, &seed, &[("o", status)], vec![update], expected).await;
            }
        }
        // EXECUTION FAIL is allowed too.
        let update = built(rule_update("o", T::Execution, A::Fail));
        check_rule_row("Execution Fail", &[], &[], vec![update], None).await;
    }

    /// Test 1.4: the parent rules.
    #[tokio::test]
    async fn parent_rules() {
        use OperationAction as A;
        use OperationType as T;
        let seed = [
            built(rule_update("ctx", T::Context, A::Start)),
            built(rule_update("step", T::Step, A::Start)),
        ];
        let child = |parent: &str| built(rule_update("a", T::Step, A::Start).parent_id(parent));
        let parent_error = Some(INVALID_PARENT.to_owned());
        let rows = vec![
            (
                "omitted",
                vec![built(rule_update("a", T::Step, A::Start))],
                None,
            ),
            ("stored CONTEXT", vec![child("ctx")], None),
            (
                "CONTEXT started earlier in the request",
                vec![built(rule_update("n", T::Context, A::Start)), child("n")],
                None,
            ),
            ("missing", vec![child("nope")], parent_error.clone()),
            ("stored STEP", vec![child("step")], parent_error.clone()),
            (
                "STEP started earlier in the request",
                vec![built(rule_update("n", T::Step, A::Start)), child("n")],
                parent_error.clone(),
            ),
            (
                "CONTEXT closed, not started, earlier in the request",
                vec![built(rule_update("n", T::Context, A::Succeed)), child("n")],
                parent_error.clone(),
            ),
            (
                "the operation itself",
                vec![built(rule_update("a", T::Context, A::Start).parent_id("a"))],
                parent_error.clone(),
            ),
            (
                "CONTEXT started later in the request",
                vec![child("n"), built(rule_update("n", T::Context, A::Start))],
                parent_error.clone(),
            ),
            (
                "an earlier START in the request wins over the stored record",
                vec![built(rule_update("ctx", T::Step, A::Start)), child("ctx")],
                parent_error,
            ),
        ];
        check_rule_rows(&seed, rows).await;
    }

    /// Test 1.5: every allowed and forbidden form of a repeated ID.
    #[tokio::test]
    async fn repeated_id_rules() {
        use OperationAction as A;
        use OperationType as T;
        let u = |op_type: T, action: A| built(rule_update("r", op_type, action));
        let repeated = Some(REPEATED_ID.to_owned());
        let rows = vec![
            (
                "STEP START then SUCCEED",
                vec![u(T::Step, A::Start), u(T::Step, A::Succeed)],
                None,
            ),
            (
                "STEP START then FAIL",
                vec![u(T::Step, A::Start), u(T::Step, A::Fail)],
                None,
            ),
            (
                "STEP START then RETRY",
                vec![u(T::Step, A::Start), u(T::Step, A::Retry)],
                None,
            ),
            (
                "CONTEXT START then SUCCEED",
                vec![u(T::Context, A::Start), u(T::Context, A::Succeed)],
                None,
            ),
            (
                "CONTEXT START then FAIL",
                vec![u(T::Context, A::Start), u(T::Context, A::Fail)],
                None,
            ),
            (
                "STEP START then START",
                vec![u(T::Step, A::Start), u(T::Step, A::Start)],
                repeated.clone(),
            ),
            (
                "CONTEXT START then START",
                vec![u(T::Context, A::Start), u(T::Context, A::Start)],
                repeated.clone(),
            ),
            (
                "STEP SUCCEED then FAIL",
                vec![u(T::Step, A::Succeed), u(T::Step, A::Fail)],
                repeated.clone(),
            ),
            (
                "STEP START, SUCCEED, then FAIL",
                vec![
                    u(T::Step, A::Start),
                    u(T::Step, A::Succeed),
                    u(T::Step, A::Fail),
                ],
                repeated.clone(),
            ),
            (
                "WAIT START then CANCEL",
                vec![u(T::Wait, A::Start), u(T::Wait, A::Cancel)],
                repeated.clone(),
            ),
            (
                "CALLBACK START twice",
                vec![u(T::Callback, A::Start), u(T::Callback, A::Start)],
                repeated.clone(),
            ),
            (
                "CHAINED_INVOKE START twice",
                vec![u(T::ChainedInvoke, A::Start), u(T::ChainedInvoke, A::Start)],
                repeated.clone(),
            ),
            (
                "EXECUTION SUCCEED then FAIL",
                vec![u(T::Execution, A::Succeed), u(T::Execution, A::Fail)],
                repeated.clone(),
            ),
            (
                "the repeat is checked before the parent",
                vec![
                    u(T::Step, A::Start),
                    built(rule_update("r", T::Step, A::Start).parent_id("nope")),
                ],
                repeated,
            ),
        ];
        check_rule_rows(&[], rows).await;
    }

    /// The repeated-ID and parent pass covers the whole request before any
    /// update is checked on its own. So a repeat later in the request wins
    /// over a status error of an earlier update.
    #[tokio::test]
    async fn request_pass_runs_before_update_checks() {
        use OperationAction as A;
        use OperationType as T;
        let seed = [built(rule_update("s", T::Step, A::Succeed))];
        let updates = vec![
            built(rule_update("s", T::Step, A::Succeed)),
            built(rule_update("w", T::Wait, A::Start)),
            built(rule_update("w", T::Wait, A::Start)),
        ];
        check_rule_row(
            "repeat after a status error",
            &seed,
            &[],
            updates,
            Some(REPEATED_ID),
        )
        .await;
    }

    /// Test 2: a request whose first update is valid and whose second is
    /// rejected stores neither, keeps the token and records no event.
    #[tokio::test]
    async fn rejected_request_applies_no_update() {
        use OperationAction as A;
        use OperationType as T;
        let seed = [built(rule_update("ctx", T::Context, A::Start))];
        let updates = vec![
            built(rule_update("a", T::Step, A::Start)),
            built(rule_update("ctx", T::Context, A::Start)),
        ];
        check_rule_row(
            "second update rejected",
            &seed,
            &[],
            updates,
            Some("Cannot start a CONTEXT operation that already exists."),
        )
        .await;
        // `check_rule_row` compared the whole store, so `a` is not stored.
    }

    /// Test 3: the SDK sends a STEP update to an ID that holds a pruned
    /// WAIT record. The backend rejects it with a non-retryable error, and
    /// the run ends with `CheckpointUnrecoverableExecutionError`.
    ///
    /// A correct handler builds no forbidden update, and the SDK checks the
    /// identity of every record it can see. So the test hides the WAIT
    /// from the SDK: it stores the WAIT as the child of a closed CONTEXT
    /// without `ReplayChildren`, which the invocation input omits.
    #[tokio::test]
    async fn run_with_a_rejected_update_ends_unrecoverable() {
        use OperationAction as A;
        use OperationType as T;
        let handler = |(), ctx: DurableContext| async move {
            let value = ctx.step(|_| async { Ok(1_u32) }).name("s").await?;
            Ok::<_, BoxError>(value)
        };
        // The ID the handler's step uses.
        let probe = LocalRunner::new().run(handler, ()).await.expect("runner");
        let step_id = probe.operations()[0].id().to_owned();

        let backend = Arc::new(seeded_backend(
            Vec::new(),
            0,
            &[
                built(rule_update("hidden", T::Context, A::Start)),
                built(rule_update(&step_id, T::Wait, A::Start).parent_id("hidden")),
                built(rule_update("hidden", T::Context, A::Succeed)),
            ],
        ));
        let result = LocalRunner::new()
            .run_on_backend(Arc::clone(&backend), handler, "null".to_owned())
            .await
            .expect("runner");
        assert!(result.is_failure(), "{result}");
        assert_eq!(
            result.error_type(),
            Some("CheckpointUnrecoverableExecutionError")
        );
        assert_eq!(
            result.error_message(),
            Some(
                format!("[Unrecoverable Execution] Checkpoint failed: Type{INCONSISTENT}").as_str()
            )
        );
        // The WAIT record is unchanged.
        let state = backend.lock();
        let hidden = state
            .ops
            .iter()
            .find(|op| op.id == step_id)
            .expect("the WAIT is stored");
        assert_eq!(hidden.kind, RunnerKind::Wait);
        assert_eq!(hidden.status, OperationStatus::Started);
    }

    /// Sends `updates` in one checkpoint call, which must succeed.
    async fn checkpoint_ok(backend: &Backend, updates: Vec<OperationUpdate>) {
        let token = backend.current_token();
        let result =
            ExecutionClient::checkpoint(backend, LOCAL_EXECUTION_ARN, &token, updates).await;
        assert_eq!(result.err().map(|err| err.to_string()), None);
    }

    /// The `UpdatedOperationIds` of a freshly built envelope.
    fn envelope_updated_ids(backend: &Backend) -> serde_json::Value {
        build_envelope(backend, "{}", None)["UpdatedOperationIds"].clone()
    }

    #[tokio::test]
    async fn updated_ids_empty_on_first_envelope() {
        let backend = seeded_backend(Vec::new(), 0, &[]);
        checkpoint_ok(&backend, vec![wait_start("w", 5)]).await;
        assert_eq!(envelope_updated_ids(&backend), serde_json::json!([]));
    }

    #[tokio::test]
    async fn updated_ids_list_fired_wait() {
        let backend = seeded_backend(Vec::new(), 0, &[]);
        checkpoint_ok(&backend, vec![wait_start("w", 5)]).await;
        assert!(backend.advance());
        assert_eq!(envelope_updated_ids(&backend), serde_json::json!(["w"]));
    }

    #[tokio::test]
    async fn updated_ids_cleared_after_envelope() {
        let backend = seeded_backend(Vec::new(), 0, &[]);
        checkpoint_ok(&backend, vec![wait_start("w", 5)]).await;
        assert!(backend.advance());
        assert_eq!(envelope_updated_ids(&backend), serde_json::json!(["w"]));
        assert_eq!(envelope_updated_ids(&backend), serde_json::json!([]));
    }

    #[tokio::test]
    async fn updated_ids_list_readied_retry_and_delivered_callback() {
        let backend = seeded_backend(vec![CallbackReply::raw("\"ok\"")], 0, &[]);
        checkpoint_ok(
            &backend,
            vec![step_retry("s", Some(5)), callback_start("c")],
        )
        .await;
        assert!(backend.advance(), "delivers the reply");
        assert_eq!(envelope_updated_ids(&backend), serde_json::json!(["c"]));
        assert!(backend.advance(), "fires the retry");
        assert_eq!(envelope_updated_ids(&backend), serde_json::json!(["s"]));
    }

    /// Epoch milliseconds count 0 for a start time before the epoch,
    /// saturate a start time past `u64::MAX` milliseconds, and saturate the
    /// sum with the virtual seconds.
    #[test]
    fn virtual_epoch_millis_saturates() {
        let epoch = std::time::SystemTime::UNIX_EPOCH;
        let second = std::time::Duration::from_secs(1);
        assert_eq!(virtual_epoch_millis(epoch + second, 2), 3_000);
        let before_epoch = epoch
            .checked_sub(second)
            .expect("in range on this platform");
        assert_eq!(virtual_epoch_millis(before_epoch, 0), 0);
        let far = epoch
            .checked_add(std::time::Duration::from_secs(u64::MAX / 1000 + 1))
            .expect("in range on this platform");
        assert_eq!(virtual_epoch_millis(far, 0), u64::MAX);
        assert_eq!(virtual_epoch_millis(epoch + second, u64::MAX), u64::MAX);
    }

    /// The envelope and `get_state` render each record's start and end
    /// times as the run's start time plus the record's virtual seconds.
    #[tokio::test]
    async fn envelope_renders_virtual_timestamps() {
        let before = std::time::SystemTime::now();
        let backend = seeded_backend(Vec::new(), 0, &[]);
        checkpoint_ok(&backend, vec![step_op("s", OperationAction::Start, None)]).await;
        checkpoint_ok(&backend, vec![step_op("s", OperationAction::Succeed, None)]).await;
        checkpoint_ok(&backend, vec![wait_start("w", 10)]).await;
        assert!(backend.advance(), "fires the wait");

        let envelope = build_envelope(&backend, "{}", None);
        let ops = envelope["InitialExecutionState"]["Operations"]
            .as_array()
            .expect("Operations is an array");
        let millis = |id: &str, field: &str| {
            ops.iter()
                .find(|op| op["Id"] == id)
                .and_then(|op| op[field].as_u64())
                .expect("the record has an integer timestamp field")
        };
        let e = millis(EXECUTION_OPERATION_ID, "StartTimestamp");
        let before_ms = u64::try_from(
            before
                .duration_since(std::time::SystemTime::UNIX_EPOCH)
                .expect("after the epoch")
                .as_millis(),
        )
        .expect("fits in u64");
        assert!(e >= before_ms, "execution start {e} before {before_ms}");
        assert_eq!(
            (millis("s", "StartTimestamp"), millis("s", "EndTimestamp")),
            (e, e)
        );
        assert_eq!(
            (millis("w", "StartTimestamp"), millis("w", "EndTimestamp")),
            (e, e + 10_000)
        );

        let state = get_state_ops(&backend).await;
        let times = |id: &str| {
            let op = state
                .iter()
                .find(|op| op.id() == id)
                .expect("get_state holds the record");
            (
                *op.start_timestamp(),
                *op.end_timestamp().expect("an end timestamp"),
            )
        };
        let (step_start, step_end) = times("s");
        assert_eq!(step_start, step_end);
        assert!(step_start >= DateTime::from(before));
        let (wait_start_time, wait_end_time) = times("w");
        assert_eq!(wait_end_time.secs() - wait_start_time.secs(), 10);
        assert_eq!(wait_end_time.subsec_nanos(), wait_start_time.subsec_nanos());
    }

    #[tokio::test]
    async fn updated_ids_skip_checkpoint_changes() {
        let backend = seeded_backend(Vec::new(), 0, &[]);
        checkpoint_ok(
            &backend,
            vec![limit_update(
                "s",
                OperationType::Step,
                OperationAction::Start,
                None,
            )],
        )
        .await;
        checkpoint_ok(
            &backend,
            vec![limit_update(
                "s",
                OperationType::Step,
                OperationAction::Succeed,
                Some("1".to_owned()),
            )],
        )
        .await;
        assert_eq!(envelope_updated_ids(&backend), serde_json::json!([]));
    }

    // Chained invokes against registered targets.

    /// The one chained invoke record of `result`.
    fn invoke_record<O>(result: &TestResult<O>) -> &TestOperation {
        let invokes: Vec<&TestOperation> = result
            .operations()
            .iter()
            .filter(|op| op.kind() == OperationKind::ChainedInvoke)
            .collect();
        assert_eq!(invokes.len(), 1, "one chained invoke record");
        invokes[0]
    }

    /// The wire type and message of an invoke error, as a handler sees it.
    fn wire_type_and_message(err: &crate::OperationError) -> (Option<String>, Option<String>) {
        let wire = err.wire();
        (
            wire.and_then(crate::WireError::error_type)
                .map(ToOwned::to_owned),
            wire.and_then(crate::WireError::error_message)
                .map(ToOwned::to_owned),
        )
    }

    #[tokio::test]
    async fn invoke_registered_function_succeeds() {
        let result = LocalRunner::new()
            .register_function("double", |x: i32| async move { Ok(x * 2) })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let doubled = ctx.invoke::<i32>("double", 21).await?;
                    Ok::<_, BoxError>(doubled)
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(result.output(), Some(&42), "{:?}", result.error_message());
        let op = invoke_record(&result);
        assert_eq!(op.status(), OperationStatus::Succeeded);
        assert_eq!(op.result(), Some("42"));
        assert_eq!(result.invocation_count(), 2);
    }

    #[tokio::test]
    async fn invoke_registered_function_failure_keeps_typed_error() {
        let result = LocalRunner::new()
            .register_function("pay", |(): ()| async move {
                Err::<(), BoxError>(crate::TypedError::with_type("PaymentError", "declined").into())
            })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let err = ctx
                        .invoke::<()>("pay", ())
                        .await
                        .expect_err("the invoke fails");
                    Ok::<_, BoxError>(wire_type_and_message(&err))
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(
            result.output(),
            Some(&(Some("PaymentError".to_owned()), Some("declined".to_owned()))),
            "{:?}",
            result.error_message()
        );
        let op = invoke_record(&result);
        assert_eq!(op.status(), OperationStatus::Failed);
        assert_eq!(op.error_type(), Some("PaymentError"));
        assert_eq!(op.error_message(), Some("declined"));
    }

    #[tokio::test]
    async fn invoke_plain_error_records_error_type() {
        let result = LocalRunner::new()
            .register_function(
                "pay",
                |(): ()| async move { Err::<(), BoxError>("boom".into()) },
            )
            .run(
                |(): (), ctx: DurableContext| async move {
                    let err = ctx
                        .invoke::<()>("pay", ())
                        .await
                        .expect_err("the invoke fails");
                    Ok::<_, BoxError>(wire_type_and_message(&err))
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(
            result.output(),
            Some(&(Some("Error".to_owned()), Some("boom".to_owned()))),
            "{:?}",
            result.error_message()
        );
    }

    #[tokio::test]
    async fn invoke_unit_result_records_null() {
        let result = LocalRunner::new()
            .register_function("notify", |_: i32| async move { Ok(()) })
            .run(
                |(): (), ctx: DurableContext| async move {
                    ctx.invoke::<()>("notify", 1).await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        let op = invoke_record(&result);
        assert_eq!(op.status(), OperationStatus::Succeeded);
        assert_eq!(op.result(), Some("null"));
    }

    #[tokio::test]
    async fn invoke_unregistered_function_fails_run() {
        let result = LocalRunner::new()
            .run(
                |(): (), ctx: DurableContext| async move {
                    ctx.invoke::<()>("child-fn", ()).await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_failure(), "{:?}", result.error_message());
        let message = result.error_message().unwrap_or_default();
        assert!(
            message.contains(
                "No function found for function name \"child-fn\". Register it on the LocalRunner."
            ),
            "{message}"
        );
    }

    #[tokio::test]
    async fn invoke_input_decode_failure_fails_invoke() {
        let result = LocalRunner::new()
            .register_function("len", |s: String| async move { Ok(s.len()) })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let err = ctx
                        .invoke::<usize>("len", 5_i32)
                        .await
                        .expect_err("the invoke fails");
                    Ok::<_, BoxError>(wire_type_and_message(&err))
                },
                (),
            )
            .await
            .expect("runner");
        let (error_type, message) = result.into_output().expect("the handler succeeds");
        assert_eq!(error_type.as_deref(), Some("Error"));
        let message = message.unwrap_or_default();
        assert!(message.starts_with("deserialize input:"), "{message}");
    }

    #[tokio::test]
    async fn invoke_output_encode_failure_fails_invoke() {
        let result = LocalRunner::new()
            .register_function(
                "map",
                |(): ()| async move { Ok(BTreeMap::from([((1, 2), 3)])) },
            )
            .run(
                |(): (), ctx: DurableContext| async move {
                    let err = ctx
                        .invoke::<serde_json::Value>("map", ())
                        .await
                        .expect_err("the invoke fails");
                    Ok::<_, BoxError>(wire_type_and_message(&err).1)
                },
                (),
            )
            .await
            .expect("runner");
        let message = result
            .into_output()
            .expect("the handler succeeds")
            .unwrap_or_default();
        assert!(message.starts_with("serialize output:"), "{message}");
    }

    #[tokio::test]
    async fn invoke_target_runs_once() {
        let calls = Arc::new(AtomicU32::new(0));
        let target_calls = Arc::clone(&calls);
        let result = LocalRunner::new()
            .register_function("count", move |(): ()| {
                let calls = Arc::clone(&target_calls);
                async move { Ok(calls.fetch_add(1, Ordering::SeqCst) + 1) }
            })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let count = ctx.invoke::<u32>("count", ()).await?;
                    ctx.wait(std::time::Duration::from_secs(1)).await?;
                    ctx.step(|_| async { Ok(()) }).await?;
                    Ok::<_, BoxError>(count)
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(calls.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn invoke_completes_before_the_clock_moves() {
        let result = LocalRunner::new()
            .register_function("fast", |(): ()| async move { Ok("invoked") })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let waited = ctx
                        .run_in_child_context(|child| async move {
                            child.wait(std::time::Duration::from_secs(1)).await?;
                            Ok("waited".to_owned())
                        })
                        .future();
                    let invoked = ctx
                        .run_in_child_context(|child| async move {
                            Ok(child.invoke::<String>("fast", ()).await?)
                        })
                        .future();
                    let winner = ctx.race([waited, invoked]).await?;
                    Ok::<_, BoxError>(winner)
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(
            result.output().map(String::as_str),
            Some("invoked"),
            "{:?}",
            result.error_message()
        );
    }

    #[tokio::test]
    async fn invoke_durable_function_steps_and_waits() {
        let result = LocalRunner::new()
            .register_durable_function("workflow", |n: u32, ctx: DurableContext| async move {
                let next = ctx.step(move |_| async move { Ok(n + 1) }).await?;
                ctx.wait(std::time::Duration::from_secs(5)).await?;
                let doubled = ctx.step(move |_| async move { Ok(next * 2) }).await?;
                Ok::<_, BoxError>(doubled)
            })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let out = ctx.invoke::<u32>("workflow", 1).await?;
                    Ok::<_, BoxError>(out)
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(result.output(), Some(&4), "{:?}", result.error_message());
        let op = invoke_record(&result);
        assert_eq!(op.status(), OperationStatus::Succeeded);
        assert_eq!(op.result(), Some("4"));
        assert_eq!(result.invocation_count(), 2);
    }

    #[tokio::test]
    async fn invoke_durable_function_failure_keeps_type() {
        let result = LocalRunner::new()
            .register_durable_function("pay", |(): (), _ctx: DurableContext| async move {
                Err::<(), BoxError>(
                    crate::TypedError::with_type("PaymentError", "declined")
                        .with_error_data("{\"code\":7}")
                        .into(),
                )
            })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let err = ctx
                        .invoke::<()>("pay", ())
                        .await
                        .expect_err("the invoke fails");
                    let wire = err.wire();
                    Ok::<_, BoxError>((
                        wire.and_then(crate::WireError::error_type)
                            .map(ToOwned::to_owned),
                        wire.and_then(crate::WireError::error_data)
                            .map(ToOwned::to_owned),
                    ))
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(
            result.output(),
            Some(&(
                Some("PaymentError".to_owned()),
                Some("{\"code\":7}".to_owned())
            )),
            "{:?}",
            result.error_message()
        );
        let op = invoke_record(&result);
        assert_eq!(op.status(), OperationStatus::Failed);
        assert_eq!(op.error_message(), Some("declined"));
    }

    /// A durable target that waits twice, so its run needs three
    /// invocations.
    async fn slow_target((): (), ctx: DurableContext) -> Result<(), BoxError> {
        ctx.wait(std::time::Duration::from_secs(1)).await?;
        ctx.wait(std::time::Duration::from_secs(1)).await?;
        Ok(())
    }

    /// A caller that invokes `"slow"`.
    async fn invoke_slow((): (), ctx: DurableContext) -> Result<(), BoxError> {
        ctx.invoke::<()>("slow", ()).await?;
        Ok(())
    }

    #[tokio::test]
    async fn nested_invocations_do_not_count() {
        let result = LocalRunner::new()
            .register_durable_function("slow", slow_target)
            .run(invoke_slow, ())
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.invocation_count(), 2);
        assert_eq!(result.operations().len(), 1);
    }

    #[tokio::test]
    async fn durable_target_runs_once() {
        let calls = Arc::new(AtomicU32::new(0));
        let target_calls = Arc::clone(&calls);
        let result = LocalRunner::new()
            .register_durable_function("count", move |(): (), _ctx: DurableContext| {
                target_calls.fetch_add(1, Ordering::SeqCst);
                async move { Ok::<_, BoxError>(()) }
            })
            .run(
                |(): (), ctx: DurableContext| async move {
                    ctx.invoke::<()>("count", ()).await?;
                    ctx.wait(std::time::Duration::from_secs(1)).await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.invocation_count(), 3);
        assert_eq!(calls.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn suspended_durable_target_leaves_invoke_started() {
        let result = LocalRunner::new()
            .register_durable_function("approval", |(): (), ctx: DurableContext| async move {
                let cb = ctx.create_callback::<bool>().await?;
                let approved = cb.result().await?;
                Ok::<_, BoxError>(approved)
            })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let approved = ctx.invoke::<bool>("approval", ()).await?;
                    Ok::<_, BoxError>(approved)
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_suspended(), "{:?}", result.error_message());
        assert_eq!(invoke_record(&result).status(), OperationStatus::Started);
        let message = result.error_message().unwrap_or_default();
        assert!(
            message.ends_with("; chained invokes still pending for: \"approval\""),
            "{message}"
        );
    }

    /// A suspended durable target stays `Started`, and the runner does not
    /// run it again when a later invocation leaves it pending.
    #[tokio::test]
    async fn suspended_durable_target_runs_once() {
        let calls = Arc::new(AtomicU32::new(0));
        let target_calls = Arc::clone(&calls);
        let result = LocalRunner::new()
            .register_durable_function("approval", move |(): (), ctx: DurableContext| {
                target_calls.fetch_add(1, Ordering::SeqCst);
                async move {
                    let cb = ctx.create_callback::<bool>().await?;
                    let approved = cb.result().await?;
                    Ok::<_, BoxError>(approved)
                }
            })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let approval = ctx.invoke::<bool>("approval", ()).spawn();
                    ctx.wait(std::time::Duration::from_secs(1)).await?;
                    let approved = approval.await?;
                    Ok::<_, BoxError>(approved)
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_suspended(), "{:?}", result.error_message());
        assert_eq!(result.invocation_count(), 2);
        assert_eq!(calls.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn durable_target_does_not_see_registered_functions() {
        let result = LocalRunner::new()
            .register_function("leaf", |(): ()| async move { Ok(1_u32) })
            .register_durable_function("mid", |(): (), ctx: DurableContext| async move {
                let leaf = ctx.invoke::<u32>("leaf", ()).await?;
                Ok::<_, BoxError>(leaf)
            })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let err = ctx
                        .invoke::<u32>("mid", ())
                        .await
                        .expect_err("the invoke fails");
                    Ok::<_, BoxError>(wire_type_and_message(&err).1)
                },
                (),
            )
            .await
            .expect("runner");
        let message = result
            .into_output()
            .expect("the handler succeeds")
            .unwrap_or_default();
        assert!(
            message.contains("No function found for function name \"leaf\""),
            "{message}"
        );
    }

    #[tokio::test]
    async fn durable_target_uses_outer_settings() {
        let result = LocalRunner::new()
            .max_invocations(2)
            .register_durable_function("slow", slow_target)
            .run(invoke_slow, ())
            .await
            .expect("runner");
        assert!(result.is_suspended(), "{:?}", result.error_message());
        assert_eq!(invoke_record(&result).status(), OperationStatus::Started);
        let message = result.error_message().unwrap_or_default();
        assert!(
            message.ends_with("; chained invokes still pending for: \"slow\""),
            "{message}"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn durable_target_uses_outer_checkpoint_latency() -> Result<(), BoxError> {
        let result = LocalRunner::new()
            .checkpoint_latency(std::time::Duration::from_millis(100))
            .register_durable_function("timed", |(): (), ctx: DurableContext| async move {
                let before = tokio::time::Instant::now();
                ctx.step(|_| async { Ok(()) }).name("s").await?;
                let elapsed = tokio::time::Instant::now().duration_since(before);
                Ok::<_, BoxError>(u64::try_from(elapsed.as_millis())?)
            })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let millis = ctx.invoke::<u64>("timed", ()).await?;
                    Ok::<_, BoxError>(millis)
                },
                (),
            )
            .await?;
        assert_eq!(result.output(), Some(&200), "{:?}", result.error_message());
        Ok(())
    }

    #[tokio::test]
    async fn later_registration_replaces_earlier_kind() {
        let result = LocalRunner::new()
            .register_function("t", |(): ()| async move { Ok(1_u32) })
            .register_durable_function("t", |(): (), _ctx: DurableContext| async move {
                Ok::<_, BoxError>(2_u32)
            })
            .run(
                |(): (), ctx: DurableContext| async move {
                    let out = ctx.invoke::<u32>("t", ()).await?;
                    Ok::<_, BoxError>(out)
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(result.output(), Some(&2), "{:?}", result.error_message());
    }

    /// An error whose wire record carries a stack trace, as an external
    /// callback failure read back from the wire does.
    fn error_with_stack_trace() -> BoxError {
        let wire = crate::WireError::new(Some("VendorError"), Some("denied"))
            .with_error_data(Some("opaque"))
            .with_stack_trace(vec!["frame-1".to_owned(), "frame-2".to_owned()]);
        crate::OperationError::from_kind(crate::OperationErrorKind::Callback(
            crate::error::CallbackError::new(
                crate::CallbackErrorKind::ExternalFailure,
                Some(crate::error::ReplayedFailure::source_from(wire.clone())),
            ),
        ))
        .with_wire(wire)
        .into()
    }

    /// A caller that invokes `"fail"` and returns the invoke error's wire
    /// record.
    async fn invoke_fail_wire(
        (): (),
        ctx: DurableContext,
    ) -> Result<(Option<String>, Option<String>, Vec<String>), BoxError> {
        let err = ctx
            .invoke::<()>("fail", ())
            .await
            .expect_err("the invoke fails");
        let wire = err.wire().cloned().unwrap_or_default();
        Ok((
            wire.error_type().map(ToOwned::to_owned),
            wire.error_data().map(ToOwned::to_owned),
            wire.stack_trace().to_vec(),
        ))
    }

    fn expected_fail_wire() -> (Option<String>, Option<String>, Vec<String>) {
        (
            Some("VendorError".to_owned()),
            Some("opaque".to_owned()),
            vec!["frame-1".to_owned(), "frame-2".to_owned()],
        )
    }

    #[tokio::test]
    async fn invoke_plain_failure_keeps_stack_trace() {
        let result = LocalRunner::new()
            .register_function("fail", |(): ()| async move {
                Err::<(), BoxError>(error_with_stack_trace())
            })
            .run(invoke_fail_wire, ())
            .await
            .expect("runner");
        assert_eq!(
            result.output(),
            Some(&expected_fail_wire()),
            "{:?}",
            result.error_message()
        );
    }

    #[tokio::test]
    async fn invoke_durable_failure_keeps_stack_trace() {
        let result = LocalRunner::new()
            .register_durable_function("fail", |(): (), _ctx: DurableContext| async move {
                Err::<(), BoxError>(error_with_stack_trace())
            })
            .run(invoke_fail_wire, ())
            .await
            .expect("runner");
        assert_eq!(
            result.output(),
            Some(&expected_fail_wire()),
            "{:?}",
            result.error_message()
        );
    }

    #[test]
    fn envelope_wire_error_reads_every_field() {
        let response = serde_json::json!({
            "Status": "FAILED",
            "Error": {
                "ErrorType": "T",
                "ErrorMessage": "m",
                "ErrorData": "d",
                "StackTrace": ["a", "b"],
            },
        });
        assert_eq!(
            envelope_wire_error(&response),
            crate::WireError::new(Some("T"), Some("m"))
                .with_error_data(Some("d"))
                .with_stack_trace(vec!["a".to_owned(), "b".to_owned()])
        );
    }

    /// A chained invoke `START` for `function`, with ID `id`.
    fn invoke_start(id: &str, function: &str) -> OperationUpdate {
        OperationUpdate::builder()
            .id(id)
            .r#type(OperationType::ChainedInvoke)
            .action(OperationAction::Start)
            .payload("1")
            .chained_invoke_options(
                aws_sdk_lambda::types::ChainedInvokeOptions::builder()
                    .function_name(function)
                    .build()
                    .expect("valid options"),
            )
            .build()
            .expect("valid update")
    }

    #[test]
    fn pending_invoke_names_list_each_name_once_in_stored_order() {
        let backend = seeded_backend(
            Vec::new(),
            0,
            &[
                invoke_start("i1", "b"),
                invoke_start("i2", "a"),
                invoke_start("i3", "b"),
                invoke_start("i4", "c"),
            ],
        );
        assert!(backend.record_invoke_outcome("i4", NestedOutcome::Succeeded(None)));
        assert_eq!(backend.pending_invoke_names(), ["b", "a"]);
    }

    #[test]
    fn invoke_completion_is_listed_in_updated_ids() {
        let backend = seeded_backend(Vec::new(), 0, &[invoke_start("i", "double")]);
        assert_eq!(envelope_updated_ids(&backend), serde_json::json!([]));
        assert!(backend.record_invoke_outcome("i", NestedOutcome::Succeeded(Some("2".to_owned()))));
        assert_eq!(envelope_updated_ids(&backend), serde_json::json!(["i"]));
    }

    // Operation detail, execution status and runner errors

    /// The only operation with `kind` in `result`.
    fn only_of_kind<O>(result: &TestResult<O>, kind: OperationKind) -> &TestOperation {
        let mut ops = result.operations().iter().filter(|op| op.kind() == kind);
        let op = ops.next().expect("an operation of the kind");
        assert!(ops.next().is_none(), "one operation of kind {kind}");
        op
    }

    #[tokio::test]
    async fn wait_reports_duration() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    ctx.wait(std::time::Duration::from_secs(10)).await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        let op = only_of_kind(&result, OperationKind::Wait);
        assert!(matches!(
            op.details(),
            TestOperationDetails::Wait(w) if w.duration() == Some(std::time::Duration::from_secs(10))
        ));
    }

    #[tokio::test]
    async fn callback_reports_id_seen_by_handler() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<u32>().await?;
                    Ok::<_, BoxError>(cb.id().to_owned())
                },
                (),
            )
            .await
            .expect("runner");
        let output = result.output().expect("the handler returned the ID");
        let op = only_of_kind(&result, OperationKind::Callback);
        assert!(
            matches!(
                op.details(),
                TestOperationDetails::Callback(c) if c.callback_id() == Some(output.as_str())
            ),
            "{:?}",
            op.details()
        );
    }

    #[tokio::test]
    async fn map_children_are_its_items() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let out = ctx
                        .map(vec![1_u32, 2, 3], |child, item, _idx| async move {
                            Ok(child.step(move |_| async move { Ok(item) }).await?)
                        })
                        .name("items")
                        .await?;
                    Ok::<_, BoxError>(out)
                },
                (),
            )
            .await
            .expect("runner");
        let map_op = result.operation("items").expect("the map operation");
        let children: Vec<_> = result.children(map_op).collect();
        assert_eq!(children.len(), 3);
        for child in children {
            assert_eq!(child.sub_type(), Some("MapIteration"));
            assert_eq!(child.parent_id(), Some(map_op.id()));
        }
    }

    #[tokio::test]
    async fn replay_children_is_reported() {
        // The handler of `oversized_map_result_replays_with_replay_children`.
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let items = ctx
                        .map(vec![0_u32, 1], |child, _item, _idx| async move {
                            let big = child
                                .step(|_| async { Ok("x".repeat(150 * 1024)) })
                                .name("big")
                                .await?;
                            Ok(big)
                        })
                        .name("oversized")
                        .await?;
                    ctx.wait(std::time::Duration::from_secs(1))
                        .name("pause")
                        .await?;
                    Ok::<_, BoxError>(items.iter().map(String::len).collect::<Vec<_>>())
                },
                (),
            )
            .await
            .expect("runner");
        let replay_children = |op: &TestOperation| match op.details() {
            TestOperationDetails::Context(c) => Some(c.replay_children()),
            _ => None,
        };
        let map_op = result.operation("oversized").expect("the map operation");
        assert_eq!(replay_children(map_op), Some(true));
        let items: Vec<_> = result.children(map_op).collect();
        assert_eq!(items.len(), 2);
        for item in items {
            assert_eq!(replay_children(item), Some(false));
        }
    }

    #[tokio::test]
    async fn details_match_kind() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    ctx.step(|_| async { Ok(1_u32) }).name("s").await?;
                    ctx.wait(std::time::Duration::from_secs(1)).await?;
                    let _cb = ctx.create_callback::<u32>().await?;
                    ctx.run_in_child_context(|_child| async { Ok(2_u32) })
                        .await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        let kinds: BTreeSet<String> = result
            .operations()
            .iter()
            .map(|op| op.kind().to_string())
            .collect();
        assert_eq!(kinds.len(), 4, "{kinds:?}");
        for op in result.operations() {
            let matches = match op.details() {
                TestOperationDetails::Context(_) => op.kind() == OperationKind::Context,
                TestOperationDetails::Step(_) => op.kind() == OperationKind::Step,
                TestOperationDetails::Wait(_) => op.kind() == OperationKind::Wait,
                TestOperationDetails::Callback(_) => op.kind() == OperationKind::Callback,
                TestOperationDetails::ChainedInvoke => op.kind() == OperationKind::ChainedInvoke,
            };
            assert!(matches, "{:?} has details {:?}", op.kind(), op.details());
        }
        let step = result.operation("s").expect("the step");
        assert!(!matches!(step.details(), TestOperationDetails::Wait(_)));
    }

    #[test]
    fn details_variant_for_every_kind() {
        // The name of a `details()` variant, so each row below can state
        // the one variant its kind must give.
        fn variant(details: TestOperationDetails<'_>) -> &'static str {
            match details {
                TestOperationDetails::Context(_) => "Context",
                TestOperationDetails::Step(_) => "Step",
                TestOperationDetails::Wait(_) => "Wait",
                TestOperationDetails::Callback(_) => "Callback",
                TestOperationDetails::ChainedInvoke => "ChainedInvoke",
            }
        }
        let rows = [
            (RunnerKind::Context, OperationKind::Context, "Context"),
            (RunnerKind::Step, OperationKind::Step, "Step"),
            (RunnerKind::Wait, OperationKind::Wait, "Wait"),
            (RunnerKind::Callback, OperationKind::Callback, "Callback"),
            (
                RunnerKind::ChainedInvoke,
                OperationKind::ChainedInvoke,
                "ChainedInvoke",
            ),
        ];
        for (kind, public, expected) in rows {
            let op = TestOperation::started("id".to_owned(), kind);
            assert_eq!(variant(op.details()), expected, "{kind:?}");
            assert_eq!(op.kind(), public, "{kind:?}");
            assert_eq!(RunnerKind::from_operation_kind(public), Some(kind));
        }
        assert_eq!(
            RunnerKind::from_operation_kind(OperationKind::Execution),
            None
        );
    }

    /// The error a step body or a handler returns in the error data tests.
    fn bad_error() -> BoxError {
        Box::new(crate::TypedError::with_type("Bad", "no").with_error_data("{\"a\":1}"))
    }

    #[tokio::test]
    async fn failed_step_error_keeps_data() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let _ = ctx
                        .step(|_| async { Err::<u32, BoxError>(bad_error()) })
                        .name("bad")
                        .retry_strategy(|_err, _attempt| RetryDecision::Stop)
                        .await;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        let error = result
            .operation("bad")
            .and_then(TestOperation::error)
            .expect("the step error");
        assert_eq!(error.error_type(), Some("Bad"));
        assert_eq!(error.error_message(), Some("no"));
        assert_eq!(error.error_data(), Some("{\"a\":1}"));
    }

    #[tokio::test]
    async fn test_result_error_keeps_data() {
        let result = LocalRunner::new()
            .run(
                |(), _ctx: DurableContext| async move { Err::<(), BoxError>(bad_error()) },
                (),
            )
            .await
            .expect("runner");
        let error = result.error().expect("the execution error");
        assert_eq!(error.error_type(), Some("Bad"));
        assert_eq!(error.error_message(), Some("no"));
        assert_eq!(error.error_data(), Some("{\"a\":1}"));
    }

    #[tokio::test]
    async fn operation_lookup_by_name() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    ctx.step(|_| async { Ok(1_u32) }).name("process").await?;
                    ctx.step(|_| async { Ok(2_u32) }).name("charge").await?;
                    ctx.step(|_| async { Ok(3_u32) }).name("process").await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        let ops = result.operations();
        assert_eq!(ops.len(), 3);
        assert_eq!(
            result.operation("charge").map(TestOperation::id),
            Some(ops[1].id())
        );
        assert_eq!(result.operations_named("process").count(), 2);
        assert_eq!(
            result
                .operations_named("process")
                .nth(1)
                .map(TestOperation::id),
            Some(ops[2].id())
        );
        assert!(result.operation("absent").is_none());
    }

    #[test]
    fn checkpoint_fault_setters_replace_all_fault_fields() {
        let fault = |r: &LocalRunner| {
            (
                r.checkpoint_failure_skip,
                r.checkpoint_failures,
                r.checkpoint_failures_retryable,
                r.withdraw_after,
            )
        };
        let next = LocalRunner::new()
            .withdraw_checkpoints_after(4)
            .fail_checkpoints_after_retryable(2, 3)
            .fail_next_checkpoints(1);
        assert_eq!(fault(&next), (0, 1, false, Some(4)));

        let after = LocalRunner::new()
            .withdraw_checkpoints_after(4)
            .fail_checkpoints_after_retryable(2, 3)
            .fail_checkpoints_after(5, 6);
        assert_eq!(fault(&after), (5, 6, false, Some(4)));

        let retryable = LocalRunner::new()
            .withdraw_checkpoints_after(4)
            .fail_checkpoints_after(2, 3)
            .fail_next_checkpoints(7)
            .fail_checkpoints_after_retryable(5, 6);
        assert_eq!(fault(&retryable), (5, 6, true, Some(4)));
    }

    #[test]
    fn typed_accessors_read_folded_cloud_history() {
        use aws_sdk_lambda::types::WaitStartedDetails;
        #[derive(Debug, PartialEq, serde::Deserialize)]
        struct Order {
            id: u32,
        }
        let events = vec![
            Event::builder()
                .event_type(EventType::StepStarted)
                .id("step")
                .event_id(1)
                .build(),
            Event::builder()
                .event_type(EventType::StepSucceeded)
                .id("step")
                .event_id(2)
                .step_succeeded_details(
                    StepSucceededDetails::builder()
                        .result(EventResult::builder().payload(r#"{"id":7}"#).build())
                        .build(),
                )
                .build(),
            Event::builder()
                .event_type(EventType::ContextStarted)
                .id("wfcb")
                .sub_type("WaitForCallback")
                .event_id(3)
                .build(),
            Event::builder()
                .event_type(EventType::StepStarted)
                .id("custom")
                .sub_type("WaitForCallback")
                .event_id(4)
                .build(),
            Event::builder()
                .event_type(EventType::WaitStarted)
                .id("wait")
                .event_id(5)
                .wait_started_details(
                    WaitStartedDetails::builder()
                        .duration(10)
                        .scheduled_end_timestamp(DateTime::from_secs(10))
                        .build()
                        .expect("wait details"),
                )
                .build(),
            Event::builder()
                .event_type(EventType::WaitSucceeded)
                .id("bare-wait")
                .event_id(6)
                .build(),
        ];
        let ops = fold_history(&events);
        assert_eq!(ops.len(), 5, "{ops:?}");
        let (step, wfcb, custom, wait, bare_wait) = (&ops[0], &ops[1], &ops[2], &ops[3], &ops[4]);

        assert_eq!(
            step.result_json::<Order>().expect("decodes"),
            Some(Order { id: 7 })
        );
        assert!(step.result_json::<String>().is_err());
        assert_eq!(wfcb.result_json::<Order>().expect("no result"), None);

        assert!(wfcb.is_wait_for_callback());
        assert!(!custom.is_wait_for_callback());
        assert!(!step.is_wait_for_callback());

        assert_eq!(
            wait.wait_duration(),
            Some(std::time::Duration::from_secs(10))
        );
        assert_eq!(bare_wait.wait_duration(), None);
        assert_eq!(step.wait_duration(), None);

        // A non-wait reports no duration even when its wait detail holds
        // one: the accessor reads the detail of the operation's kind.
        let mut not_a_wait = TestOperation::started("s".to_owned(), RunnerKind::Step);
        not_a_wait.wait.duration = Some(std::time::Duration::from_secs(3));
        assert_eq!(not_a_wait.wait_duration(), None);
    }

    #[test]
    fn fold_history_fills_detail_fields() {
        use aws_sdk_lambda::types::{CallbackStartedDetails, WaitStartedDetails};
        let events = vec![
            Event::builder()
                .event_type(EventType::WaitStarted)
                .id("w")
                .parent_id("p")
                .event_id(1)
                .wait_started_details(
                    WaitStartedDetails::builder()
                        .duration(10)
                        .scheduled_end_timestamp(DateTime::from_secs(10))
                        .build()
                        .expect("wait details"),
                )
                .build(),
            Event::builder()
                .event_type(EventType::CallbackStarted)
                .id("c")
                .event_id(2)
                .callback_started_details(
                    CallbackStartedDetails::builder()
                        .callback_id("cb-1")
                        .build()
                        .expect("callback details"),
                )
                .build(),
            Event::builder()
                .event_type(EventType::StepStarted)
                .id("s")
                .event_id(3)
                .build(),
            Event::builder()
                .event_type(EventType::StepFailed)
                .id("s")
                .event_id(4)
                .step_failed_details(
                    StepFailedDetails::builder()
                        .error(
                            EventError::builder()
                                .payload(
                                    ErrorObject::builder()
                                        .error_type("Bad")
                                        .error_message("no")
                                        .error_data("{\"a\":1}")
                                        .stack_trace("frame")
                                        .build(),
                                )
                                .build(),
                        )
                        .retry_details(RetryDetails::builder().current_attempt(1).build())
                        .build(),
                )
                .build(),
            // A later event of the wait carries no parent. The parent of the
            // first event stays.
            Event::builder()
                .event_type(EventType::WaitSucceeded)
                .id("w")
                .event_id(5)
                .build(),
            Event::builder()
                .event_type(EventType::ContextStarted)
                .id("x")
                .event_id(6)
                .build(),
        ];
        let ops = fold_history(&events);
        assert_eq!(ops.len(), 4);
        assert_eq!(ops[0].parent_id(), Some("p"));
        // No history event carries `ReplayChildren`, so a folded context
        // reports `false`.
        assert!(
            matches!(
                ops[3].details(),
                TestOperationDetails::Context(c) if !c.replay_children()
            ),
            "{:?}",
            ops[3].details()
        );
        assert!(matches!(
            ops[0].details(),
            TestOperationDetails::Wait(w) if w.duration() == Some(std::time::Duration::from_secs(10))
        ));
        assert!(matches!(
            ops[1].details(),
            TestOperationDetails::Callback(c) if c.callback_id() == Some("cb-1")
        ));
        let error = ops[2].error().expect("the step error");
        assert_eq!(error.error_type(), Some("Bad"));
        assert_eq!(error.error_data(), Some("{\"a\":1}"));
        assert_eq!(error.stack_trace(), ["frame".to_owned()]);
    }

    /// One event per operation event type that carries an `EventError`,
    /// with its operation's kind and the status the event sets. Each error
    /// has every field set.
    fn error_event_rows() -> Vec<(Event, OperationKind, OperationStatus)> {
        use aws_sdk_lambda::types::{
            CallbackFailedDetails, ChainedInvokeFailedDetails, ChainedInvokeStoppedDetails,
            ChainedInvokeTimedOutDetails, ContextFailedDetails, WaitCancelledDetails,
        };
        let error = || {
            EventError::builder()
                .payload(
                    ErrorObject::builder()
                        .error_type("Kind")
                        .error_message("msg")
                        .error_data("{\"d\":1}")
                        .stack_trace("f1")
                        .stack_trace("f2")
                        .build(),
                )
                .build()
        };
        let base =
            |event_type: EventType| Event::builder().event_type(event_type).id("op").event_id(1);
        vec![
            (
                base(EventType::StepFailed)
                    .step_failed_details(StepFailedDetails::builder().error(error()).build())
                    .build(),
                OperationKind::Step,
                OperationStatus::Failed,
            ),
            (
                base(EventType::WaitCancelled)
                    .wait_cancelled_details(WaitCancelledDetails::builder().error(error()).build())
                    .build(),
                OperationKind::Wait,
                OperationStatus::Cancelled,
            ),
            (
                base(EventType::ContextFailed)
                    .context_failed_details(ContextFailedDetails::builder().error(error()).build())
                    .build(),
                OperationKind::Context,
                OperationStatus::Failed,
            ),
            (
                base(EventType::CallbackFailed)
                    .callback_failed_details(
                        CallbackFailedDetails::builder().error(error()).build(),
                    )
                    .build(),
                OperationKind::Callback,
                OperationStatus::Failed,
            ),
            (
                base(EventType::CallbackTimedOut)
                    .callback_timed_out_details(
                        CallbackTimedOutDetails::builder().error(error()).build(),
                    )
                    .build(),
                OperationKind::Callback,
                OperationStatus::TimedOut,
            ),
            (
                base(EventType::ChainedInvokeFailed)
                    .chained_invoke_failed_details(
                        ChainedInvokeFailedDetails::builder().error(error()).build(),
                    )
                    .build(),
                OperationKind::ChainedInvoke,
                OperationStatus::Failed,
            ),
            (
                base(EventType::ChainedInvokeTimedOut)
                    .chained_invoke_timed_out_details(
                        ChainedInvokeTimedOutDetails::builder()
                            .error(error())
                            .build(),
                    )
                    .build(),
                OperationKind::ChainedInvoke,
                OperationStatus::TimedOut,
            ),
            (
                base(EventType::ChainedInvokeStopped)
                    .chained_invoke_stopped_details(
                        ChainedInvokeStoppedDetails::builder()
                            .error(error())
                            .build(),
                    )
                    .build(),
                OperationKind::ChainedInvoke,
                OperationStatus::Stopped,
            ),
        ]
    }

    /// Every operation event type that carries an `EventError` keeps the
    /// whole record: type, message, data and stack trace. One row per type.
    #[test]
    fn fold_history_keeps_every_event_error_field() {
        for (event, kind, status) in error_event_rows() {
            let label = format!("{:?}", event.event_type());
            let ops = fold_history(std::slice::from_ref(&event));
            assert_eq!(ops.len(), 1, "{label}");
            assert_eq!(ops[0].kind(), kind, "{label}");
            assert_eq!(ops[0].status(), status, "{label}");
            assert!(ops[0].error().is_some(), "{label}: no error");
            let wire = ops[0].error().expect("the folded error");
            assert_eq!(wire.error_type(), Some("Kind"), "{label}");
            assert_eq!(wire.error_message(), Some("msg"), "{label}");
            assert_eq!(wire.error_data(), Some("{\"d\":1}"), "{label}");
            assert_eq!(
                wire.stack_trace(),
                ["f1".to_owned(), "f2".to_owned()],
                "{label}"
            );
            assert_eq!(ops[0].error_type(), Some("Kind"), "{label}");
            assert_eq!(ops[0].error_message(), Some("msg"), "{label}");
        }
    }

    /// Asserts the predicates of `result` agree with its status.
    fn assert_predicates_agree<O>(result: &TestResult<O>) {
        let status = result.status();
        assert_eq!(result.is_success(), status == ExecutionStatus::Succeeded);
        assert_eq!(
            result.is_failure(),
            matches!(
                status,
                ExecutionStatus::Failed | ExecutionStatus::TimedOut | ExecutionStatus::Stopped
            )
        );
        assert_eq!(result.is_suspended(), status == ExecutionStatus::Running);
    }

    #[tokio::test]
    async fn local_status_matches_predicates() {
        let ok = LocalRunner::new()
            .run(
                |(), _ctx: DurableContext| async { Ok::<_, BoxError>(1_u32) },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(ok.status(), ExecutionStatus::Succeeded);
        assert_predicates_agree(&ok);

        let failed = LocalRunner::new()
            .run(
                |(), _ctx: DurableContext| async { Err::<u32, BoxError>("boom".into()) },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(failed.status(), ExecutionStatus::Failed);
        assert_predicates_agree(&failed);

        let running = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<u32>().await?;
                    cb.result().await.map_err(BoxError::from)
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(running.status(), ExecutionStatus::Running);
        assert_predicates_agree(&running);
    }

    /// An event whose `Serialize` always fails.
    #[derive(Debug, Deserialize)]
    struct Unserializable;

    impl Serialize for Unserializable {
        fn serialize<S: serde::Serializer>(&self, _serializer: S) -> Result<S::Ok, S::Error> {
            Err(serde::ser::Error::custom("refuse"))
        }
    }

    /// Asserts that `source` is a `serde_json::Error` with the text of `e`.
    fn assert_json_source(err: &TestRunnerError, e: &serde_json::Error) {
        let source = std::error::Error::source(err).expect("a source");
        let json = source
            .downcast_ref::<serde_json::Error>()
            .expect("the source is the serde_json error");
        assert_eq!(json.to_string(), e.to_string());
    }

    #[tokio::test]
    async fn event_serialization_is_a_runner_error() {
        let err = LocalRunner::new()
            .run(
                |_e: Unserializable, _ctx: DurableContext| async { Ok::<_, BoxError>(()) },
                Unserializable,
            )
            .await
            .expect_err("a runner error");
        let e = match &err {
            TestRunnerError::EventSerialization(e) => Some(e),
            _ => None,
        }
        .expect("an EventSerialization error");
        assert_eq!(
            err.to_string(),
            format!("durable: test runner: event serialization failed: {e}")
        );
        assert!(e.to_string().contains("refuse"), "{e}");
        assert_json_source(&err, e);
    }

    #[tokio::test]
    async fn output_decode_is_a_runner_error() {
        // `run` ties the handler output to `O`, so the test drives a handler
        // that returns a string and decodes its output as `u32`.
        let runner = LocalRunner::new();
        let err = runner
            .drive(
                Arc::new(runner.new_backend()),
                |(), _ctx: DurableContext| async { Ok::<_, BoxError>("text".to_owned()) },
                "null".to_owned(),
            )
            .await
            .expect("the run ends")
            .into_test_result::<u32>()
            .expect_err("a runner error");
        let e = match &err {
            TestRunnerError::OutputDeserialization(e) => Some(e),
            _ => None,
        }
        .expect("an OutputDeserialization error");
        assert_eq!(
            err.to_string(),
            format!("durable: test runner: output deserialization failed: {e}")
        );
        assert_json_source(&err, e);
    }

    #[tokio::test]
    async fn unexpected_envelope_is_a_runner_error() {
        let runner = LocalRunner::new();
        let service = |_event: lambda_runtime::LambdaEvent<crate::InvocationPayload>| {
            let response: crate::BoxedInvocationFuture = Box::pin(async {
                Ok(crate::InvocationResponse(
                    serde_json::json!({ "Status": "WEIRD" }),
                ))
            });
            response
        };
        let err = runner
            .drive_service(Arc::new(runner.new_backend()), service, "null".to_owned())
            .await
            .expect_err("a runner error");
        assert!(
            matches!(&err, TestRunnerError::UnexpectedEnvelope(Some(s)) if s == "WEIRD"),
            "{err:?}"
        );
        assert_eq!(
            err.to_string(),
            "durable: test runner: unexpected response envelope status: Some(\"WEIRD\")"
        );
        assert!(std::error::Error::source(&err).is_none());
    }

    #[test]
    fn invocation_type_default_is_request_response() {
        let request = CloudRunner::new("f").build_invoke(Vec::new());
        assert_eq!(request.invocation_type, WireInvocationType::RequestResponse);
    }

    #[test]
    fn invoke_request_uses_configured_type() {
        let request = CloudRunner::new("f")
            .invocation_type(InvocationType::Event)
            .build_invoke(Vec::new());
        assert_eq!(request.invocation_type, WireInvocationType::Event);
    }

    #[tokio::test]
    async fn configured_client_is_used() {
        let config = aws_sdk_lambda::Config::builder()
            .region(aws_sdk_lambda::config::Region::new("eu-west-3"))
            .behavior_version(aws_sdk_lambda::config::BehaviorVersion::latest())
            .build();
        let client = crate::LambdaClient::from_conf(config);
        let resolved = CloudRunner::new("f")
            .lambda_client(client)
            .region("us-east-1")
            .resolve_client()
            .await;
        assert_eq!(
            resolved.config().region().map(ToString::to_string),
            Some("eu-west-3".to_owned())
        );
    }

    /// A Lambda error with a fixed text.
    #[derive(Debug)]
    struct FakeError(&'static str);

    impl std::fmt::Display for FakeError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.write_str(self.0)
        }
    }

    impl std::error::Error for FakeError {}

    type GetExecutionOutput =
        aws_sdk_lambda::operation::get_durable_execution::GetDurableExecutionOutput;
    type GetHistoryOutput =
        aws_sdk_lambda::operation::get_durable_execution_history::GetDurableExecutionHistoryOutput;
    type InvokeOutput = aws_sdk_lambda::operation::invoke::InvokeOutput;

    /// The execution ARN every scripted invoke returns.
    const ARN: &str = "fake-execution-arn";

    /// One recorded `GetDurableExecution` request, with the tokio time it
    /// was made.
    #[derive(Debug, Clone, PartialEq, Eq)]
    struct ExecutionCall {
        execution_arn: String,
        at: tokio::time::Instant,
    }

    /// One recorded `GetDurableExecutionHistory` request.
    #[derive(Debug, Clone, PartialEq, Eq)]
    struct HistoryCall {
        execution_arn: String,
        marker: Option<String>,
    }

    /// One scripted history page: its events and its next marker.
    type HistoryPage = Result<(Vec<Event>, Option<String>), BoxError>;

    /// A recording [`CloudApi`] that returns scripted outputs and errors,
    /// and records the arguments of every request.
    ///
    /// `Invoke` returns `invoke` once. `GetDurableExecution` pops the front
    /// of `executions`, and returns `RUNNING` once the queue is empty.
    /// `GetDurableExecutionHistory` pops the front of `history`; a request
    /// with no scripted page left panics, so a test notices an extra fetch.
    ///
    /// With [`repeat_history`](Self::repeat_history), a history request
    /// with no scripted page left gets the repeated page instead. `calls`
    /// records every request in order.
    struct FakeApi {
        invoke: Mutex<Option<Result<InvokeOutput, BoxError>>>,
        executions: Mutex<std::collections::VecDeque<Result<GetExecutionOutput, BoxError>>>,
        history: Mutex<std::collections::VecDeque<HistoryPage>>,
        repeated_history: Mutex<Option<Result<Vec<Event>, &'static str>>>,
        invokes: Mutex<Vec<InvokeRequest>>,
        execution_calls: Mutex<Vec<ExecutionCall>>,
        history_calls: Mutex<Vec<HistoryCall>>,
        history_times: Mutex<Vec<tokio::time::Instant>>,
        calls: Mutex<Vec<FakeCall>>,
    }

    /// One request a [`FakeApi`] received.
    #[derive(Debug, Clone, PartialEq)]
    enum FakeCall {
        Invoke,
        GetExecution,
        History,
        CallbackSuccess(String, Vec<u8>),
        CallbackFailure(String, ErrorObject),
        Heartbeat(String),
    }

    impl FakeApi {
        /// A fake whose invoke returns [`ARN`] and whose history is one
        /// empty page.
        fn new() -> Self {
            Self {
                invoke: Mutex::new(Some(Ok(invoke_output(Some(ARN))))),
                executions: Mutex::new(std::collections::VecDeque::new()),
                history: Mutex::new(std::collections::VecDeque::from([Ok((Vec::new(), None))])),
                repeated_history: Mutex::new(None),
                invokes: Mutex::new(Vec::new()),
                execution_calls: Mutex::new(Vec::new()),
                history_calls: Mutex::new(Vec::new()),
                history_times: Mutex::new(Vec::new()),
                calls: Mutex::new(Vec::new()),
            }
        }

        /// Serves `page` (events, or a fake error with this text) to every
        /// history request after the scripted pages ran out.
        fn repeat_history(self, page: Result<Vec<Event>, &'static str>) -> Self {
            *self.repeated_history.lock().expect("lock") = Some(page);
            self
        }

        fn calls(&self) -> Vec<FakeCall> {
            self.calls.lock().expect("lock").clone()
        }

        fn record(&self, call: FakeCall) {
            self.calls.lock().expect("lock").push(call);
        }

        fn with_invoke(self, invoke: Result<InvokeOutput, BoxError>) -> Self {
            *self.invoke.lock().expect("lock") = Some(invoke);
            self
        }

        fn with_execution(self, execution: Result<GetExecutionOutput, BoxError>) -> Self {
            self.executions.lock().expect("lock").push_back(execution);
            self
        }

        /// Replaces the scripted history with one page that has no next
        /// marker.
        fn with_history(self, history: Result<Vec<Event>, BoxError>) -> Self {
            self.with_history_pages(vec![history.map(|events| (events, None))])
        }

        /// Replaces the scripted history with `pages`, served in order.
        fn with_history_pages(self, pages: Vec<HistoryPage>) -> Self {
            *self.history.lock().expect("lock") = pages.into();
            self
        }

        fn execution_call_count(&self) -> usize {
            self.execution_calls.lock().expect("lock").len()
        }

        fn invokes(&self) -> Vec<InvokeRequest> {
            self.invokes.lock().expect("lock").clone()
        }

        fn execution_arns(&self) -> Vec<String> {
            self.execution_calls
                .lock()
                .expect("lock")
                .iter()
                .map(|call| call.execution_arn.clone())
                .collect()
        }

        fn history_calls(&self) -> Vec<HistoryCall> {
            self.history_calls.lock().expect("lock").clone()
        }

        /// The milliseconds after `start` of every `GetDurableExecution`
        /// request.
        fn execution_ms(&self, start: tokio::time::Instant) -> Vec<u128> {
            self.execution_calls
                .lock()
                .expect("lock")
                .iter()
                .map(|call| (call.at - start).as_millis())
                .collect()
        }

        /// The milliseconds after `start` and the marker of every
        /// `GetDurableExecutionHistory` request.
        fn history_ms(&self, start: tokio::time::Instant) -> Vec<(u128, Option<String>)> {
            let times = self.history_times.lock().expect("lock").clone();
            times
                .iter()
                .zip(self.history_calls())
                .map(|(at, call)| ((*at - start).as_millis(), call.marker))
                .collect()
        }
    }

    /// `(ms, None)` for each of `ms`: history requests without a marker.
    fn unmarked(ms: &[u128]) -> Vec<(u128, Option<String>)> {
        ms.iter().map(|ms| (*ms, None)).collect()
    }

    impl CloudApi for FakeApi {
        #[expect(clippy::unused_async_trait_impl)] // reason: the fake answers at once
        async fn send_invoke(&self, request: InvokeRequest) -> Result<InvokeOutput, BoxError> {
            self.record(FakeCall::Invoke);
            self.invokes.lock().expect("lock").push(request);
            self.invoke
                .lock()
                .expect("lock")
                .take()
                .expect("one invoke per run")
        }

        #[expect(clippy::unused_async_trait_impl)] // reason: the fake answers at once
        async fn send_get_durable_execution(
            &self,
            execution_arn: &str,
        ) -> Result<GetExecutionOutput, BoxError> {
            self.record(FakeCall::GetExecution);
            self.execution_calls
                .lock()
                .expect("lock")
                .push(ExecutionCall {
                    execution_arn: execution_arn.to_owned(),
                    at: tokio::time::Instant::now(),
                });
            self.executions
                .lock()
                .expect("lock")
                .pop_front()
                .unwrap_or_else(|| Ok(execution_output(WireExecutionStatus::Running, None)))
        }

        #[expect(clippy::unused_async_trait_impl)] // reason: the fake answers at once
        async fn send_get_durable_execution_history(
            &self,
            execution_arn: &str,
            marker: Option<&str>,
        ) -> Result<GetHistoryOutput, BoxError> {
            self.record(FakeCall::History);
            self.history_calls.lock().expect("lock").push(HistoryCall {
                execution_arn: execution_arn.to_owned(),
                marker: marker.map(ToOwned::to_owned),
            });
            self.history_times
                .lock()
                .expect("lock")
                .push(tokio::time::Instant::now());
            let scripted = self.history.lock().expect("lock").pop_front();
            let (events, next_marker) = match scripted {
                Some(page) => page?,
                None => match self
                    .repeated_history
                    .lock()
                    .expect("lock")
                    .clone()
                    .expect("a scripted history page")
                {
                    Ok(events) => (events, None),
                    Err(text) => return Err(fake_error(text)),
                },
            };
            Ok(GetHistoryOutput::builder()
                .set_events(Some(events))
                .set_next_marker(next_marker)
                .build()
                .expect("history output"))
        }

        #[expect(clippy::unused_async_trait_impl)] // reason: the fake answers at once
        async fn send_callback_success(
            &self,
            callback_id: &str,
            result: Vec<u8>,
        ) -> Result<(), BoxError> {
            self.record(FakeCall::CallbackSuccess(callback_id.to_owned(), result));
            Ok(())
        }

        #[expect(clippy::unused_async_trait_impl)] // reason: the fake answers at once
        async fn send_callback_failure(
            &self,
            callback_id: &str,
            error: ErrorObject,
        ) -> Result<(), BoxError> {
            self.record(FakeCall::CallbackFailure(callback_id.to_owned(), error));
            Ok(())
        }

        #[expect(clippy::unused_async_trait_impl)] // reason: the fake answers at once
        async fn send_callback_heartbeat(&self, callback_id: &str) -> Result<(), BoxError> {
            self.record(FakeCall::Heartbeat(callback_id.to_owned()));
            Ok(())
        }
    }

    /// The history requests of one fetch of a single page.
    fn one_history_call() -> Vec<HistoryCall> {
        vec![HistoryCall {
            execution_arn: ARN.to_owned(),
            marker: None,
        }]
    }

    fn invoke_output(arn: Option<&str>) -> InvokeOutput {
        InvokeOutput::builder()
            .status_code(200)
            .set_durable_execution_arn(arn.map(ToOwned::to_owned))
            .build()
    }

    fn execution_output(status: WireExecutionStatus, result: Option<&str>) -> GetExecutionOutput {
        GetExecutionOutput::builder()
            .durable_execution_arn(ARN)
            .durable_execution_name("name")
            .function_arn("function")
            .start_timestamp(DateTime::from_secs(0))
            .status(status)
            .set_result(result.map(ToOwned::to_owned))
            .build()
            .expect("execution output")
    }

    fn fake_error(text: &'static str) -> BoxError {
        Box::new(FakeError(text))
    }

    /// Asserts `err` is an `Aws` error whose source is the fake error `text`.
    fn assert_aws(err: &TestRunnerError, text: &str) {
        assert!(matches!(err, TestRunnerError::Aws(_)), "{err:?}");
        let display = err.to_string();
        assert!(
            display.starts_with("durable: test runner: AWS call failed: ")
                && display.ends_with(text),
            "{display}"
        );
        let source = std::error::Error::source(err).expect("a source");
        assert_eq!(
            source.downcast_ref::<FakeError>().map(|e| e.0),
            Some(text),
            "the source is the fake error"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn fold_terminal_timed_out_status() {
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(WireExecutionStatus::TimedOut, None))),
        );
        let result = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert_eq!(result.status(), ExecutionStatus::TimedOut);
        assert!(result.is_failure());
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_invoke_error_is_aws() {
        let api = Arc::new(FakeApi::new().with_invoke(Err(fake_error("fake invoke"))));
        let err = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect_err("a runner error");
        assert_aws(&err, "fake invoke");
        assert_eq!(api.invokes().len(), 1);
        assert_eq!(api.execution_call_count(), 0);
        assert_eq!(api.history_calls(), Vec::<HistoryCall>::new());
    }

    /// Every `GetDurableExecution` attempt fails: the poll makes three
    /// attempts, 1000 ms and then 1450 ms apart, and the run returns the
    /// third error.
    #[tokio::test(start_paused = true)]
    async fn cloud_get_execution_error_is_aws() {
        let start = tokio::time::Instant::now();
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Err(fake_error("fake get 1")))
                .with_execution(Err(fake_error("fake get 2")))
                .with_execution(Err(fake_error("fake get 3"))),
        );
        let err = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect_err("a runner error");
        assert_aws(&err, "fake get 3");
        assert_eq!(api.execution_arns(), vec![ARN.to_owned(); 3]);
        assert_eq!(api.execution_ms(start), [1000, 2000, 3450]);
        assert_eq!(api.history_calls(), Vec::<HistoryCall>::new());
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_get_execution_succeeds_on_the_third_attempt() {
        let start = tokio::time::Instant::now();
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Err(fake_error("fake get 1")))
                .with_execution(Err(fake_error("fake get 2")))
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("5"),
                ))),
        );
        let result = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert!(result.is_success());
        assert_eq!(result.output(), Some(&5));
        assert_eq!(api.execution_ms(start), [1000, 2000, 3450]);
        assert_eq!(api.history_ms(start), unmarked(&[3450]));
    }

    /// Every history attempt fails: the page request makes three attempts,
    /// and the run returns the third error.
    #[tokio::test(start_paused = true)]
    async fn cloud_get_history_error_is_aws() {
        let start = tokio::time::Instant::now();
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("5"),
                )))
                .with_history_pages(vec![
                    Err(fake_error("fake history 1")),
                    Err(fake_error("fake history 2")),
                    Err(fake_error("fake history 3")),
                ]),
        );
        let err = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect_err("a runner error");
        assert_aws(&err, "fake history 3");
        assert_eq!(api.execution_ms(start), [1000]);
        assert_eq!(api.history_ms(start), unmarked(&[1000, 2000, 3450]));
    }

    /// The page after a marker starts one poll interval after the marked
    /// page, and its retries repeat the marker after their retry delays.
    #[tokio::test(start_paused = true)]
    async fn cloud_history_retry_repeats_the_marker() {
        let start = tokio::time::Instant::now();
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("5"),
                )))
                .with_history_pages(vec![
                    Ok((Vec::new(), Some("m1".to_owned()))),
                    Err(fake_error("fake m1 1")),
                    Err(fake_error("fake m1 2")),
                    Ok((Vec::new(), None)),
                ]),
        );
        let result = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert!(result.is_success());
        assert_eq!(api.execution_ms(start), [1000]);
        let m1 = Some("m1".to_owned());
        assert_eq!(
            api.history_ms(start),
            [
                (1000, None),
                (2000, m1.clone()),
                (3000, m1.clone()),
                (4450, m1)
            ]
        );
    }

    /// A status poll that retries counts once against the poll budget.
    #[tokio::test(start_paused = true)]
    async fn cloud_retried_poll_counts_once() {
        let start = tokio::time::Instant::now();
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Err(fake_error("fake get 1")))
                .with_execution(Err(fake_error("fake get 2")))
                .with_execution(Ok(execution_output(WireExecutionStatus::Running, None)))
                .with_execution(Ok(execution_output(WireExecutionStatus::Running, None))),
        );
        let result = CloudRunner::new("f")
            .max_poll_attempts(2)
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert_eq!(result.status(), ExecutionStatus::Running);
        assert_eq!(api.execution_ms(start), [1000, 2000, 3450, 4450]);
        assert_eq!(api.history_ms(start), unmarked(&[4450]));
        assert_eq!(result.invocation_count(), 2);
        assert_eq!(
            result.error().and_then(crate::WireError::error_message),
            Some("execution fake-execution-arn still running after 2 polls")
        );
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_missing_execution_arn_is_runner_error() {
        let api = Arc::new(FakeApi::new().with_invoke(Ok(invoke_output(None))));
        let err = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect_err("a runner error");
        assert!(matches!(err, TestRunnerError::NoExecutionArn), "{err:?}");
        assert_eq!(
            err.to_string(),
            "durable: test runner: the invoke response carried no DurableExecutionArn"
        );
        assert_eq!(api.execution_call_count(), 0);
        assert_eq!(api.history_calls(), Vec::<HistoryCall>::new());
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_ignores_invoke_function_error_and_payload_for_both_invocation_types() {
        use aws_sdk_lambda::types::{EventResult, ExecutionSucceededDetails};
        for invocation_type in [InvocationType::RequestResponse, InvocationType::Event] {
            let invoke = InvokeOutput::builder()
                .status_code(200)
                .durable_execution_arn(ARN)
                .function_error("Unhandled")
                .payload(Blob::new("{\"bogus\":true}"))
                .build();
            let history = vec![
                Event::builder()
                    .event_type(EventType::ExecutionSucceeded)
                    .event_id(1)
                    .execution_succeeded_details(
                        ExecutionSucceededDetails::builder()
                            .result(EventResult::builder().payload("5").build())
                            .build(),
                    )
                    .build(),
            ];
            let api = Arc::new(
                FakeApi::new()
                    .with_invoke(Ok(invoke))
                    // The GetDurableExecution result differs from the history's,
                    // so the assertion below shows the history wins.
                    .with_execution(Ok(execution_output(
                        WireExecutionStatus::Succeeded,
                        Some("7"),
                    )))
                    .with_history(Ok(history)),
            );
            let result = CloudRunner::new("f")
                .invocation_type(invocation_type)
                .run_with::<_, u32>(Arc::clone(&api), Vec::new())
                .await
                .expect("runner");
            assert_eq!(
                result.status(),
                ExecutionStatus::Succeeded,
                "{invocation_type:?}"
            );
            assert_eq!(result.output(), Some(&5), "{invocation_type:?}");
            assert_eq!(api.invokes().len(), 1);
            assert_eq!(api.invokes()[0].invocation_type, invocation_type.to_wire());
            assert_eq!(api.execution_arns(), vec![ARN.to_owned()]);
            assert_eq!(api.history_calls(), one_history_call());
        }
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_reads_terminal_error_from_history_for_each_terminal_event() {
        use aws_sdk_lambda::types::{
            EventError, ExecutionFailedDetails, ExecutionStoppedDetails, ExecutionTimedOutDetails,
        };
        let history_error = || {
            EventError::builder()
                .payload(
                    ErrorObject::builder()
                        .error_type("HistoryError")
                        .error_message("from history")
                        .error_data("{\"code\":1}")
                        .stack_trace("frame-1")
                        .stack_trace("frame-2")
                        .build(),
                )
                .build()
        };
        let failed = Event::builder()
            .event_type(EventType::ExecutionFailed)
            .execution_failed_details(
                ExecutionFailedDetails::builder()
                    .error(history_error())
                    .build(),
            );
        let timed_out = Event::builder()
            .event_type(EventType::ExecutionTimedOut)
            .execution_timed_out_details(
                ExecutionTimedOutDetails::builder()
                    .error(history_error())
                    .build(),
            );
        let stopped = Event::builder()
            .event_type(EventType::ExecutionStopped)
            .execution_stopped_details(
                ExecutionStoppedDetails::builder()
                    .error(history_error())
                    .build(),
            );
        let rows = [
            (WireExecutionStatus::Failed, ExecutionStatus::Failed, failed),
            (
                WireExecutionStatus::TimedOut,
                ExecutionStatus::TimedOut,
                timed_out,
            ),
            (
                WireExecutionStatus::Stopped,
                ExecutionStatus::Stopped,
                stopped,
            ),
        ];
        for (wire_status, status, event) in rows {
            // GetDurableExecution reports a different error, so the
            // assertions below show the history's terminal event wins.
            let execution = GetExecutionOutput::builder()
                .durable_execution_arn(ARN)
                .durable_execution_name("name")
                .function_arn("function")
                .start_timestamp(DateTime::from_secs(0))
                .status(wire_status)
                .error(
                    ErrorObject::builder()
                        .error_type("ExecutionError")
                        .error_message("from GetDurableExecution")
                        .error_data("{\"code\":2}")
                        .stack_trace("other-frame")
                        .build(),
                )
                .build()
                .expect("execution output");
            let api = Arc::new(
                FakeApi::new()
                    .with_execution(Ok(execution))
                    .with_history(Ok(vec![event.event_id(1).build()])),
            );
            let result = CloudRunner::new("f")
                .run_with::<_, u32>(Arc::clone(&api), Vec::new())
                .await
                .expect("runner");
            assert_eq!(result.status(), status);
            assert!(result.is_failure(), "{status}");
            let error = result.error().expect("an error");
            assert_eq!(error.error_type(), Some("HistoryError"), "{status}");
            assert_eq!(error.error_message(), Some("from history"), "{status}");
            assert_eq!(error.error_data(), Some("{\"code\":1}"), "{status}");
            assert_eq!(error.stack_trace(), ["frame-1", "frame-2"], "{status}");
        }
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_poll_interval_defaults_to_one_second() {
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(WireExecutionStatus::Running, None)))
                .with_execution(Ok(execution_output(WireExecutionStatus::Running, None)))
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("1"),
                ))),
        );
        let result = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert!(result.is_success());
        let calls = api.execution_calls.lock().expect("lock").clone();
        assert_eq!(calls.len(), 3);
        assert!(
            calls.iter().all(|call| call.execution_arn == ARN),
            "{calls:?}"
        );
        for pair in calls.windows(2) {
            assert_eq!(pair[1].at - pair[0].at, std::time::Duration::from_secs(1));
        }
        assert_eq!(api.history_calls(), one_history_call());
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_default_polling_continues_past_150_fetches() {
        let mut api = FakeApi::new();
        for _ in 0..200 {
            api = api.with_execution(Ok(execution_output(WireExecutionStatus::Running, None)));
        }
        let api = Arc::new(api.with_execution(Ok(execution_output(
            WireExecutionStatus::Succeeded,
            Some("1"),
        ))));
        let result = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert_eq!(result.status(), ExecutionStatus::Succeeded);
        assert_eq!(api.execution_call_count(), 201);
        assert_eq!(api.history_calls(), one_history_call());
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_configured_poll_limit_ends_running() {
        let api = Arc::new(FakeApi::new());
        let result = CloudRunner::new("f")
            .max_poll_attempts(3)
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert_eq!(result.status(), ExecutionStatus::Running);
        assert_eq!(api.execution_arns(), vec![ARN.to_owned(); 3]);
        assert_eq!(api.history_calls(), one_history_call());
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_records_request_arguments_and_follows_history_markers() {
        use aws_sdk_lambda::types::{EventResult, ExecutionSucceededDetails, StepStartedDetails};
        let step_started = |id: &str| {
            Event::builder()
                .event_type(EventType::StepStarted)
                .event_id(1)
                .id(id)
                .name(id)
                .step_started_details(StepStartedDetails::builder().build())
                .build()
        };
        let succeeded = Event::builder()
            .event_type(EventType::ExecutionSucceeded)
            .event_id(3)
            .execution_succeeded_details(
                ExecutionSucceededDetails::builder()
                    .result(EventResult::builder().payload("5").build())
                    .build(),
            )
            .build();
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(WireExecutionStatus::Running, None)))
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("5"),
                )))
                .with_history_pages(vec![
                    Ok((vec![step_started("first")], Some("page-2".to_owned()))),
                    Ok((vec![step_started("second")], Some("page-3".to_owned()))),
                    // An empty marker ends the history as an absent one does.
                    Ok((vec![succeeded], Some(String::new()))),
                ]),
        );
        let result = CloudRunner::new("my-fn")
            .qualifier("live")
            .invocation_type(InvocationType::Event)
            .run_with::<_, u32>(Arc::clone(&api), b"{\"x\":1}".to_vec())
            .await
            .expect("runner");
        assert_eq!(result.output(), Some(&5));
        assert_eq!(
            api.invokes(),
            vec![InvokeRequest {
                function_name: "my-fn".to_owned(),
                qualifier: "live".to_owned(),
                invocation_type: WireInvocationType::Event,
                payload: b"{\"x\":1}".to_vec(),
            }]
        );
        assert_eq!(api.execution_arns(), vec![ARN.to_owned(); 2]);
        let marker = |m: Option<&str>| HistoryCall {
            execution_arn: ARN.to_owned(),
            marker: m.map(ToOwned::to_owned),
        };
        assert_eq!(
            api.history_calls(),
            vec![marker(None), marker(Some("page-2")), marker(Some("page-3"))]
        );
        let ids: Vec<&str> = result.operations().iter().map(TestOperation::id).collect();
        assert_eq!(ids, ["first", "second"], "every page is folded");
    }

    // Rule 4 coverage of the runner error mappings and defaults

    #[tokio::test]
    async fn cloud_event_serialization_is_a_runner_error() {
        // `run` serializes the event before it resolves a client, so this
        // test makes no Lambda call.
        let err = CloudRunner::new("f")
            .run::<u32>(Unserializable)
            .await
            .expect_err("a runner error");
        let e = match &err {
            TestRunnerError::EventSerialization(e) => Some(e),
            _ => None,
        }
        .expect("an EventSerialization error");
        assert_eq!(
            err.to_string(),
            format!("durable: test runner: event serialization failed: {e}")
        );
        assert!(e.to_string().contains("refuse"), "{e}");
        assert_json_source(&err, e);
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_output_decode_is_a_runner_error() {
        use aws_sdk_lambda::types::{EventResult, ExecutionSucceededDetails};
        let succeeded = Event::builder()
            .event_type(EventType::ExecutionSucceeded)
            .event_id(1)
            .execution_succeeded_details(
                ExecutionSucceededDetails::builder()
                    .result(EventResult::builder().payload("{\"a\":1}").build())
                    .build(),
            )
            .build();
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("{\"a\":1}"),
                )))
                .with_history(Ok(vec![succeeded])),
        );
        let err = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect_err("a runner error");
        let e = match &err {
            TestRunnerError::OutputDeserialization(e) => Some(e),
            _ => None,
        }
        .expect("an OutputDeserialization error");
        assert_eq!(
            err.to_string(),
            format!("durable: test runner: output deserialization failed: {e}")
        );
        assert_json_source(&err, e);
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_success_without_terminal_event_reads_execution_result() {
        // The history holds no terminal execution event, so the output
        // comes from the `GetDurableExecution` result.
        let api = Arc::new(FakeApi::new().with_execution(Ok(execution_output(
            WireExecutionStatus::Succeeded,
            Some("9"),
        ))));
        let result = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert_eq!(result.output(), Some(&9));
        assert!(result.error().is_none());
    }

    /// The history's `ExecutionSucceeded` event wins even when it carries no
    /// result: the output decodes from `null`, not from the
    /// `GetDurableExecution` result.
    #[tokio::test(start_paused = true)]
    async fn cloud_success_terminal_event_without_result_is_null() {
        use aws_sdk_lambda::types::ExecutionSucceededDetails;
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("9"),
                )))
                .with_history(Ok(vec![
                    Event::builder()
                        .event_type(EventType::ExecutionSucceeded)
                        .event_id(1)
                        .execution_succeeded_details(ExecutionSucceededDetails::builder().build())
                        .build(),
                ])),
        );
        let result = CloudRunner::new("f")
            .run_with::<_, Option<u32>>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert_eq!(result.status(), ExecutionStatus::Succeeded);
        assert_eq!(result.output(), Some(&None));
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_terminal_error_falls_back_to_execution_error_then_status_name() {
        use aws_sdk_lambda::types::{ExecutionFailedDetails, ExecutionStoppedDetails};
        let execution = |status: WireExecutionStatus, error: Option<ErrorObject>| {
            GetExecutionOutput::builder()
                .durable_execution_arn(ARN)
                .durable_execution_name("name")
                .function_arn("function")
                .start_timestamp(DateTime::from_secs(0))
                .status(status)
                .set_error(error)
                .build()
                .expect("execution output")
        };
        let execution_error = || {
            ErrorObject::builder()
                .error_type("ExecutionError")
                .error_message("from GetDurableExecution")
                .error_data("{\"code\":2}")
                .stack_trace("other-frame")
                .build()
        };
        let from_execution =
            crate::WireError::new(Some("ExecutionError"), Some("from GetDurableExecution"))
                .with_error_data(Some("{\"code\":2}"))
                .with_stack_trace(vec!["other-frame".to_owned()]);
        let rows: Vec<(&str, GetExecutionOutput, Vec<Event>, crate::WireError)> = vec![
            (
                "no terminal event: the GetDurableExecution error",
                execution(WireExecutionStatus::Failed, Some(execution_error())),
                Vec::new(),
                from_execution.clone(),
            ),
            (
                "a terminal event without an error: the GetDurableExecution error",
                execution(WireExecutionStatus::Failed, Some(execution_error())),
                vec![
                    Event::builder()
                        .event_type(EventType::ExecutionFailed)
                        .event_id(1)
                        .execution_failed_details(ExecutionFailedDetails::builder().build())
                        .build(),
                ],
                from_execution,
            ),
            (
                "no error anywhere: the status name",
                execution(WireExecutionStatus::Stopped, None),
                Vec::new(),
                crate::WireError::new(Some("Stopped"), Some("execution ended with status Stopped")),
            ),
            (
                "an error without type or message: the status name and an empty message",
                execution(WireExecutionStatus::Stopped, None),
                vec![
                    Event::builder()
                        .event_type(EventType::ExecutionStopped)
                        .event_id(1)
                        .execution_stopped_details(
                            ExecutionStoppedDetails::builder()
                                .error(
                                    EventError::builder()
                                        .payload(ErrorObject::builder().error_data("d").build())
                                        .build(),
                                )
                                .build(),
                        )
                        .build(),
                ],
                crate::WireError::new(Some("Stopped"), Some("")).with_error_data(Some("d")),
            ),
        ];
        for (label, execution, history, expected) in rows {
            let api = Arc::new(
                FakeApi::new()
                    .with_execution(Ok(execution))
                    .with_history(Ok(history)),
            );
            let result = CloudRunner::new("f")
                .run_with::<_, u32>(Arc::clone(&api), Vec::new())
                .await
                .expect("runner");
            assert_eq!(result.error(), Some(&expected), "{label}");
        }
    }

    #[test]
    fn terminal_execution_event_is_the_last_one() {
        let event = |event_type: EventType, id: i32| {
            Event::builder().event_type(event_type).event_id(id).build()
        };
        let events = [
            event(EventType::ExecutionFailed, 1),
            event(EventType::ExecutionSucceeded, 2),
            event(EventType::InvocationCompleted, 3),
        ];
        assert_eq!(
            terminal_execution_event(&events).map(Event::event_id),
            Some(2)
        );
        assert!(terminal_execution_event(&events[2..]).is_none());
    }

    #[test]
    fn terminal_event_error_of_a_succeeded_event_is_none() {
        let event = Event::builder()
            .event_type(EventType::ExecutionSucceeded)
            .event_id(1)
            .build();
        assert!(terminal_event_error(&event).is_none());
    }

    #[test]
    fn unknown_wire_execution_status_is_failed() {
        assert_eq!(
            ExecutionStatus::from_wire(&WireExecutionStatus::from("NotAStatus")),
            ExecutionStatus::Failed
        );
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_poll_limit_result_reports_polls_and_history() {
        use aws_sdk_lambda::types::StepStartedDetails;
        let api = Arc::new(FakeApi::new().with_history(Ok(vec![
            Event::builder()
                .event_type(EventType::StepStarted)
                .event_id(1)
                .id("s")
                .step_started_details(StepStartedDetails::builder().build())
                .build(),
        ])));
        let result = CloudRunner::new("f")
            .max_poll_attempts(2)
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert_eq!(result.status(), ExecutionStatus::Running);
        assert_eq!(result.invocation_count(), 2);
        let error = result.error().expect("the reason the run ended");
        assert_eq!(error.error_type(), None);
        assert_eq!(
            error.error_message(),
            Some(format!("execution {ARN} still running after 2 polls").as_str())
        );
        let ids: Vec<&str> = result.operations().iter().map(TestOperation::id).collect();
        assert_eq!(ids, ["s"]);
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_poll_limit_history_error_is_aws() {
        let api = Arc::new(
            FakeApi::new()
                .with_history_pages(Vec::new())
                .repeat_history(Err("fake history")),
        );
        let err = CloudRunner::new("f")
            .max_poll_attempts(1)
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect_err("a runner error");
        assert_aws(&err, "fake history");
        assert_eq!(api.execution_call_count(), 1);
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_poll_limit_of_zero_counts_as_one() {
        let api = Arc::new(FakeApi::new());
        let result = CloudRunner::new("f")
            .max_poll_attempts(0)
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert_eq!(result.status(), ExecutionStatus::Running);
        assert_eq!(api.execution_call_count(), 1);
    }

    #[tokio::test]
    async fn resolve_client_without_a_client_uses_the_region() {
        let resolved = CloudRunner::new("f")
            .region("eu-west-3")
            .resolve_client()
            .await;
        assert_eq!(
            resolved.config().region().map(ToString::to_string),
            Some("eu-west-3".to_owned())
        );
    }

    /// `run` with a configured client sends each request through the
    /// `LambdaClient` implementation of [`CloudApi`], with every argument
    /// the runner sets. Each rule answers one request, so a request with
    /// a wrong argument matches no rule and fails the run.
    #[tokio::test]
    async fn lambda_client_sends_each_request_with_its_arguments() {
        use aws_sdk_lambda::types::{EventResult, ExecutionSucceededDetails};
        use aws_smithy_mocks::{RuleMode, mock, mock_client};
        let invoke = mock!(aws_sdk_lambda::Client::invoke)
            .match_requests(|req| {
                req.function_name() == Some("my-fn")
                    && req.qualifier() == Some("live")
                    && req.invocation_type() == Some(&WireInvocationType::Event)
                    && req.payload().map(Blob::as_ref) == Some(b"{\"x\":1}".as_slice())
            })
            .sequence()
            .output(|| invoke_output(Some(ARN)))
            .build();
        let execution = mock!(aws_sdk_lambda::Client::get_durable_execution)
            .match_requests(|req| {
                req.durable_execution_arn() == Some(ARN)
                    && req.include_execution_data() == Some(true)
            })
            .sequence()
            .output(|| execution_output(WireExecutionStatus::Succeeded, Some("5")))
            .build();
        let first_page = mock!(aws_sdk_lambda::Client::get_durable_execution_history)
            .match_requests(|req| {
                req.durable_execution_arn() == Some(ARN)
                    && req.include_execution_data() == Some(true)
                    && req.marker().is_none()
            })
            .sequence()
            .output(|| {
                GetHistoryOutput::builder()
                    .set_events(Some(Vec::new()))
                    .next_marker("page-2")
                    .build()
                    .expect("history output")
            })
            .build();
        let second_page = mock!(aws_sdk_lambda::Client::get_durable_execution_history)
            .match_requests(|req| {
                req.durable_execution_arn() == Some(ARN)
                    && req.include_execution_data() == Some(true)
                    && req.marker() == Some("page-2")
            })
            .sequence()
            .output(|| {
                GetHistoryOutput::builder()
                    .set_events(Some(vec![
                        Event::builder()
                            .event_type(EventType::ExecutionSucceeded)
                            .event_id(1)
                            .execution_succeeded_details(
                                ExecutionSucceededDetails::builder()
                                    .result(EventResult::builder().payload("6").build())
                                    .build(),
                            )
                            .build(),
                    ]))
                    .build()
                    .expect("history output")
            })
            .build();
        let client = mock_client!(
            aws_sdk_lambda,
            RuleMode::MatchAny,
            [&invoke, &execution, &first_page, &second_page]
        );
        let result = CloudRunner::new("my-fn")
            .qualifier("live")
            .invocation_type(InvocationType::Event)
            .poll_interval(std::time::Duration::ZERO)
            .lambda_client(client)
            .run::<u32>(serde_json::json!({ "x": 1 }))
            .await
            .expect("runner");
        assert_eq!(result.output(), Some(&6));
        assert_eq!(invoke.num_calls(), 1);
        assert_eq!(execution.num_calls(), 1);
        assert_eq!(first_page.num_calls(), 1);
        assert_eq!(second_page.num_calls(), 1);
    }

    #[tokio::test]
    async fn lambda_client_invoke_error_is_aws() {
        use aws_sdk_lambda::operation::invoke::InvokeError;
        use aws_smithy_mocks::{mock, mock_client};
        let invoke = mock!(aws_sdk_lambda::Client::invoke).then_error(|| {
            InvokeError::ResourceNotFoundException(
                aws_sdk_lambda::types::error::ResourceNotFoundException::builder().build(),
            )
        });
        let err = CloudRunner::new("f")
            .lambda_client(mock_client!(aws_sdk_lambda, [&invoke]))
            .run::<u32>(())
            .await
            .expect_err("a runner error");
        assert!(matches!(err, TestRunnerError::Aws(_)), "{err:?}");
        assert!(std::error::Error::source(&err).is_some());
        assert_eq!(invoke.num_calls(), 1);
    }

    /// A `GetDurableExecution` failure of the `LambdaClient` implementation
    /// of [`CloudApi`] is `TestRunnerError::Aws` after three attempts, and
    /// its source is the SDK error of that call.
    #[tokio::test(start_paused = true)]
    async fn lambda_client_get_execution_error_is_aws() {
        use aws_sdk_lambda::operation::get_durable_execution::GetDurableExecutionError;
        use aws_smithy_mocks::{RuleMode, mock, mock_client};
        let invoke = mock!(aws_sdk_lambda::Client::invoke)
            .sequence()
            .output(|| invoke_output(Some(ARN)))
            .build();
        let execution = mock!(aws_sdk_lambda::Client::get_durable_execution)
            .sequence()
            .error(|| {
                GetDurableExecutionError::ResourceNotFoundException(
                    aws_sdk_lambda::types::error::ResourceNotFoundException::builder().build(),
                )
            })
            .times(3)
            .build();
        let history = mock!(aws_sdk_lambda::Client::get_durable_execution_history)
            .then_output(|| GetHistoryOutput::builder().build().expect("history output"));
        let client = mock_client!(
            aws_sdk_lambda,
            RuleMode::MatchAny,
            [&invoke, &execution, &history]
        );
        let err = CloudRunner::new("f")
            .lambda_client(client)
            .run::<u32>(())
            .await
            .expect_err("a runner error");
        assert!(matches!(err, TestRunnerError::Aws(_)), "{err:?}");
        let source = std::error::Error::source(&err).expect("a source");
        let sdk = source
            .downcast_ref::<aws_sdk_lambda::error::SdkError<
                GetDurableExecutionError,
                aws_sdk_lambda::config::http::HttpResponse,
            >>()
            .expect("the GetDurableExecution SDK error");
        assert!(
            matches!(
                sdk.as_service_error(),
                Some(GetDurableExecutionError::ResourceNotFoundException(_))
            ),
            "{sdk:?}"
        );
        assert_eq!(invoke.num_calls(), 1);
        assert_eq!(execution.num_calls(), 3);
        assert_eq!(history.num_calls(), 0);
    }

    /// A `GetDurableExecutionHistory` failure of the `LambdaClient`
    /// implementation of [`CloudApi`] is `TestRunnerError::Aws` after three
    /// attempts, and its source is the SDK error of that call.
    #[tokio::test(start_paused = true)]
    async fn lambda_client_get_history_error_is_aws() {
        use aws_sdk_lambda::operation::get_durable_execution_history::GetDurableExecutionHistoryError;
        use aws_smithy_mocks::{RuleMode, mock, mock_client};
        let invoke = mock!(aws_sdk_lambda::Client::invoke)
            .sequence()
            .output(|| invoke_output(Some(ARN)))
            .build();
        let execution = mock!(aws_sdk_lambda::Client::get_durable_execution)
            .sequence()
            .output(|| execution_output(WireExecutionStatus::Succeeded, Some("5")))
            .build();
        let history = mock!(aws_sdk_lambda::Client::get_durable_execution_history)
            .sequence()
            .error(|| {
                GetDurableExecutionHistoryError::ResourceNotFoundException(
                    aws_sdk_lambda::types::error::ResourceNotFoundException::builder().build(),
                )
            })
            .times(3)
            .build();
        let client = mock_client!(
            aws_sdk_lambda,
            RuleMode::MatchAny,
            [&invoke, &execution, &history]
        );
        let err = CloudRunner::new("f")
            .lambda_client(client)
            .run::<u32>(())
            .await
            .expect_err("a runner error");
        assert!(matches!(err, TestRunnerError::Aws(_)), "{err:?}");
        let source = std::error::Error::source(&err).expect("a source");
        let sdk = source
            .downcast_ref::<aws_sdk_lambda::error::SdkError<
                GetDurableExecutionHistoryError,
                aws_sdk_lambda::config::http::HttpResponse,
            >>()
            .expect("the GetDurableExecutionHistory SDK error");
        assert!(
            matches!(
                sdk.as_service_error(),
                Some(GetDurableExecutionHistoryError::ResourceNotFoundException(
                    _
                ))
            ),
            "{sdk:?}"
        );
        assert_eq!(invoke.num_calls(), 1);
        assert_eq!(execution.num_calls(), 1);
        assert_eq!(history.num_calls(), 3);
    }

    #[test]
    fn no_execution_arn_has_no_source() {
        assert!(std::error::Error::source(&TestRunnerError::NoExecutionArn).is_none());
    }

    #[tokio::test]
    async fn unexpected_envelope_without_a_string_status() {
        let runner = LocalRunner::new();
        let service = |_event: lambda_runtime::LambdaEvent<crate::InvocationPayload>| {
            let response: crate::BoxedInvocationFuture = Box::pin(async {
                Ok(crate::InvocationResponse(
                    serde_json::json!({ "Status": 3 }),
                ))
            });
            response
        };
        let err = runner
            .drive_service(Arc::new(runner.new_backend()), service, "null".to_owned())
            .await
            .expect_err("a runner error");
        assert!(
            matches!(&err, TestRunnerError::UnexpectedEnvelope(None)),
            "{err:?}"
        );
        assert_eq!(
            err.to_string(),
            "durable: test runner: unexpected response envelope status: None"
        );
    }

    #[test]
    fn local_failed_envelope_keeps_data_and_stack_trace() {
        let response = serde_json::json!({
            "Status": "FAILED",
            "Error": {
                "ErrorType": "T",
                "ErrorMessage": "m",
                "ErrorData": "d",
                "StackTrace": ["a", "b"],
            },
        });
        let result: TestResult<()> = RawRun {
            end: RawEnd::Failed(envelope_wire_error(&response)),
            operations: Vec::new(),
            invocations: 1,
            events: Vec::new(),
        }
        .into_test_result()
        .expect("runner");
        assert_eq!(result.status(), ExecutionStatus::Failed);
        assert_eq!(
            result.error(),
            Some(
                &crate::WireError::new(Some("T"), Some("m"))
                    .with_error_data(Some("d"))
                    .with_stack_trace(vec!["a".to_owned(), "b".to_owned()])
            )
        );
    }

    #[tokio::test]
    async fn local_succeeded_operation_has_no_error() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    ctx.step(|_| async { Ok(1_u32) }).name("ok").await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        let step = result.operation("ok").expect("the step");
        assert!(step.error().is_none(), "{:?}", step.error());
        assert!(result.error().is_none());
    }

    #[test]
    fn snapshot_reports_stack_trace_and_drops_a_negative_wait() {
        let step = |action: OperationAction, error: Option<ErrorObject>| {
            OperationUpdate::builder()
                .id("s")
                .r#type(OperationType::Step)
                .action(action)
                .set_error(error)
                .build()
                .expect("valid update")
        };
        let backend = seeded_backend(
            Vec::new(),
            0,
            &[
                step(OperationAction::Start, None),
                step(
                    OperationAction::Fail,
                    Some(
                        ErrorObject::builder()
                            .error_type("T")
                            .stack_trace("frame")
                            .build(),
                    ),
                ),
                wait_start("w", -5),
            ],
        );
        let ops = backend.snapshot_operations();
        let step_op = ops.iter().find(|op| op.id() == "s").expect("the step");
        assert_eq!(
            step_op.error().map(crate::WireError::stack_trace),
            Some(["frame".to_owned()].as_slice())
        );
        let wait_op = ops.iter().find(|op| op.id() == "w").expect("the wait");
        assert!(
            matches!(wait_op.details(), TestOperationDetails::Wait(w) if w.duration().is_none()),
            "{:?}",
            wait_op.details()
        );
    }

    #[test]
    fn fold_history_drops_a_negative_wait_duration() {
        use aws_sdk_lambda::types::WaitStartedDetails;
        let event = Event::builder()
            .event_type(EventType::WaitStarted)
            .id("w")
            .event_id(1)
            .wait_started_details(
                WaitStartedDetails::builder()
                    .duration(-1)
                    .scheduled_end_timestamp(DateTime::from_secs(0))
                    .build()
                    .expect("wait details"),
            )
            .build();
        let ops = fold_history(std::slice::from_ref(&event));
        assert!(
            matches!(ops[0].details(), TestOperationDetails::Wait(w) if w.duration().is_none()),
            "{:?}",
            ops[0].details()
        );
    }

    /// Runs `item` as the only item of a Flat map named `m`, and asserts
    /// that the record named `record` names the map as its parent.
    ///
    /// A Flat item writes no record of its own. So every operation inside it
    /// must record the batch parent as `ParentId`, or its parent is missing
    /// from the history.
    async fn assert_flat_item_records_batch_parent<F, Fut, O>(
        runner: LocalRunner,
        record: &str,
        item: F,
    ) where
        F: Fn(DurableContext) -> Fut + Clone + Send + Sync + 'static,
        Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
        O: Serialize + DeserializeOwned + Send + Sync + 'static,
    {
        use crate::builders::map_parallel::NestingMode;
        let result = runner
            .run(
                move |(), ctx: DurableContext| {
                    let item = item.clone();
                    async move {
                        let out = ctx
                            .map(vec![1_u32], move |child, _item, _idx| item(child))
                            .name("m")
                            .nesting(NestingMode::Flat)
                            .await?;
                        Ok::<_, BoxError>(out)
                    }
                },
                (),
            )
            .await
            .expect("runner");

        assert!(result.is_success(), "{:?}", result.error_message());
        let m = result.operation("m").expect("map record");
        let op = result.operation(record).expect("record");
        assert_eq!(op.parent_id(), Some(m.id()), "{record}");
    }

    #[tokio::test]
    async fn flat_item_child_context_records_batch_parent() {
        assert_flat_item_records_batch_parent(LocalRunner::new(), "nested", |child| async move {
            child
                .run_in_child_context(|nested| async move {
                    Ok(nested
                        .step(|_| async { Ok(1_u32) })
                        .name("inner-step")
                        .await?)
                })
                .name("nested")
                .await
                .map_err(BoxError::from)
        })
        .await;
    }

    #[tokio::test]
    async fn flat_item_combinator_records_batch_parent() {
        assert_flat_item_records_batch_parent(LocalRunner::new(), "gather", |child| async move {
            let a = child.step(|_| async { Ok(1_u32) }).name("a").future();
            let b = child.step(|_| async { Ok(2_u32) }).name("b").future();
            child
                .try_join_all([a, b])
                .name("gather")
                .await
                .map_err(BoxError::from)
        })
        .await;
    }

    #[tokio::test]
    async fn flat_item_nested_map_records_batch_parent() {
        assert_flat_item_records_batch_parent(LocalRunner::new(), "inner", |child| async move {
            child
                .map(vec![1_u32], |grandchild, item, _idx| async move {
                    Ok(grandchild
                        .step(move |_| async move { Ok(item) })
                        .name("inner-step")
                        .await?)
                })
                .name("inner")
                .await
                .map_err(BoxError::from)
        })
        .await;
    }

    #[tokio::test]
    async fn flat_item_callback_records_batch_parent() {
        assert_flat_item_records_batch_parent(LocalRunner::new(), "cb", |child| async move {
            let cb = child.create_callback::<String>().name("cb").await?;
            Ok(cb.id().to_owned())
        })
        .await;
    }

    #[tokio::test]
    async fn flat_item_wait_for_callback_records_batch_parent() {
        let runner = LocalRunner::new()
            .callback_for("approval", CallbackReply::success(&"ok").expect("encodes"));
        assert_flat_item_records_batch_parent(runner, "approval", |child| async move {
            child
                .wait_for_callback::<String>(|_step_ctx, _callback_id| async { Ok(()) })
                .name("approval")
                .await
                .map_err(BoxError::from)
        })
        .await;
    }

    #[tokio::test]
    async fn flat_item_step_records_batch_parent() {
        assert_flat_item_records_batch_parent(LocalRunner::new(), "s", |child| async move {
            child
                .step(|_| async { Ok(1_u32) })
                .name("s")
                .await
                .map_err(BoxError::from)
        })
        .await;
    }

    /// The detail of a step operation, or `None` for another kind.
    fn step_details(op: &TestOperation) -> Option<&StepDetails> {
        match op.details() {
            TestOperationDetails::Step(step) => Some(step),
            _ => None,
        }
    }

    /// The detail of a wait operation, or `None` for another kind.
    fn wait_details(op: &TestOperation) -> Option<&WaitDetails> {
        match op.details() {
            TestOperationDetails::Wait(wait) => Some(wait),
            _ => None,
        }
    }

    /// A `TestResult` holding `operations`, built without a run (the
    /// crate-private constructor of the table tests).
    fn result_with(operations: Vec<TestOperation>) -> TestResult<()> {
        TestResult {
            status: ExecutionStatus::Succeeded,
            output: Some(()),
            error: None,
            operations,
            invocations: 0,
            history_events: Vec::new(),
            invocation_records: Vec::new(),
        }
    }

    /// Every column except the ones `disabled` names.
    fn columns_without(disabled: &[&str]) -> PrintColumns {
        let on = |name: &str| !disabled.contains(&name);
        PrintColumns {
            parent_id: on("parent_id"),
            name: on("name"),
            kind: on("kind"),
            sub_type: on("sub_type"),
            status: on("status"),
            start_time: on("start_time"),
            end_time: on("end_time"),
            duration: on("duration"),
        }
    }

    /// Only the columns `enabled` names.
    fn columns_only(enabled: &[&str]) -> PrintColumns {
        let on = |name: &str| enabled.contains(&name);
        PrintColumns {
            parent_id: on("parent_id"),
            name: on("name"),
            kind: on("kind"),
            sub_type: on("sub_type"),
            status: on("status"),
            start_time: on("start_time"),
            end_time: on("end_time"),
            duration: on("duration"),
        }
    }

    #[tokio::test]
    async fn wait_times_follow_virtual_clock() {
        let result = LocalRunner::new()
            .run(
                |_e: (), ctx: DurableContext| async move {
                    ctx.step(|_| async { Ok(1_i32) }).name("a").await?;
                    ctx.wait(std::time::Duration::from_secs(10))
                        .name("pause")
                        .await?;
                    ctx.step(|_| async { Ok(2_i32) }).name("b").await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success());
        let a = result.operation("a").expect("step a");
        let wait = result.operation("pause").expect("the wait");
        let b = result.operation("b").expect("step b");
        let start = result.operations()[0].start_time().expect("a start time");
        let at = |n: u64| Some(start + std::time::Duration::from_secs(n));
        assert_eq!(wait.start_time(), at(0));
        assert_eq!(wait.end_time(), at(10));
        assert_eq!(
            wait_details(wait)
                .expect("wait details")
                .scheduled_end_time(),
            at(10)
        );
        assert_eq!(b.start_time(), at(10));
        assert_eq!(a.end_time(), a.start_time());
    }

    #[tokio::test]
    async fn retried_step_reports_next_attempt_time() {
        let result = LocalRunner::new()
            .run(
                |_e: (), ctx: DurableContext| async move {
                    ctx.step(|step| async move {
                        if step.attempt() == 1 {
                            return Err::<i32, BoxError>("first attempt fails".into());
                        }
                        Ok(7)
                    })
                    .name("flaky")
                    .retry_strategy(|_err, _attempt| {
                        RetryDecision::retry(std::time::Duration::from_secs(30))
                    })
                    .await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error());
        let step = result.operation("flaky").expect("the step");
        let start = result.operations()[0].start_time().expect("start time");
        let at30 = Some(start + std::time::Duration::from_secs(30));
        assert_eq!(
            step_details(step)
                .expect("step details")
                .next_attempt_time(),
            at30
        );
        assert_eq!(step.end_time(), at30);
    }

    #[tokio::test]
    async fn step_without_retry_has_no_next_attempt_time() {
        let result = LocalRunner::new()
            .run(
                |_e: (), ctx: DurableContext| async move {
                    ctx.step(|_| async { Ok(1_i32) }).name("once").await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        let step = result.operation("once").expect("the step");
        assert_eq!(
            step_details(step)
                .expect("step details")
                .next_attempt_time(),
            None
        );
    }

    #[test]
    fn end_time_keeps_the_first_terminal_status() {
        // A record that ends at virtual time 5 keeps that end time when a
        // later write at time 9 sets another terminal status.
        let mut op = new_started_op("x".to_owned(), RunnerKind::Step, None, None, 1);
        op.set_status(OperationStatus::Pending, 3);
        assert_eq!(op.end_at, None);
        op.set_status(OperationStatus::Succeeded, 5);
        op.set_status(OperationStatus::Failed, 9);
        assert_eq!((op.start_at, op.end_at), (1, Some(5)));
    }

    #[test]
    fn every_terminal_status_sets_end_at() {
        for (status, terminal) in [
            (OperationStatus::Started, false),
            (OperationStatus::Pending, false),
            (OperationStatus::Ready, false),
            (OperationStatus::Succeeded, true),
            (OperationStatus::Failed, true),
            (OperationStatus::TimedOut, true),
            (OperationStatus::Cancelled, true),
            (OperationStatus::Stopped, true),
        ] {
            let mut op = new_started_op("x".to_owned(), RunnerKind::Step, None, None, 0);
            op.set_status(status, 4);
            assert_eq!(op.end_at, terminal.then_some(4), "{status:?}");
        }
    }

    #[tokio::test]
    async fn callback_reply_and_invoke_outcome_set_end_time() {
        // A delivered callback reply and a chained invoke outcome both end
        // their records at the clock time of the advance.
        let result = LocalRunner::new()
            .callback(CallbackReply::success(&true).expect("reply"))
            .register_function("target", |_input: serde_json::Value| async move {
                Ok::<_, BoxError>(serde_json::json!(1))
            })
            .run(
                |_e: (), ctx: DurableContext| async move {
                    ctx.wait(std::time::Duration::from_secs(4))
                        .name("w")
                        .await?;
                    let cb = ctx.create_callback::<bool>().name("cb").await?;
                    cb.result().await?;
                    ctx.invoke::<serde_json::Value>("target", serde_json::json!(0))
                        .name("call")
                        .await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error());
        let start = result.operations()[0].start_time().expect("start time");
        let at4 = Some(start + std::time::Duration::from_secs(4));
        assert_eq!(result.operation("cb").expect("callback").end_time(), at4);
        assert_eq!(result.operation("call").expect("invoke").end_time(), at4);
    }

    #[test]
    fn local_time_past_the_system_time_range_is_none() {
        let backend = Backend::new(QueuedReplies::default(), None);
        let state = backend.lock();
        assert_eq!(state.local_time(0), Some(state.base));
        assert_eq!(state.local_time(u64::MAX), None);
    }

    #[test]
    fn fold_history_fills_times() {
        use aws_sdk_lambda::types::WaitStartedDetails;
        let events = vec![
            Event::builder()
                .event_type(EventType::WaitStarted)
                .id("w")
                .event_id(1)
                .event_timestamp(DateTime::from_secs(100))
                .wait_started_details(
                    WaitStartedDetails::builder()
                        .duration(10)
                        .scheduled_end_timestamp(DateTime::from_secs(110))
                        .build()
                        .expect("wait details"),
                )
                .build(),
            Event::builder()
                .event_type(EventType::WaitSucceeded)
                .id("w")
                .event_id(2)
                .event_timestamp(DateTime::from_secs(110))
                .build(),
        ];
        let ops = fold_history(&events);
        let at = |n: u64| Some(std::time::UNIX_EPOCH + std::time::Duration::from_secs(n));
        assert_eq!(ops[0].start_time(), at(100));
        assert_eq!(ops[0].end_time(), at(110));
        assert_eq!(
            wait_details(&ops[0])
                .expect("wait details")
                .scheduled_end_time(),
            at(110)
        );
    }

    #[test]
    fn fold_history_derives_next_attempt_time() {
        use aws_sdk_lambda::types::{RetryDetails, StepFailedDetails};
        let failed = |id: i32, at: i64, delay: Option<i32>| {
            let mut retry = RetryDetails::builder().current_attempt(1);
            if let Some(delay) = delay {
                retry = retry.next_attempt_delay_seconds(delay);
            }
            Event::builder()
                .event_type(EventType::StepFailed)
                .id("s")
                .event_id(id)
                .event_timestamp(DateTime::from_secs(at))
                .step_failed_details(
                    StepFailedDetails::builder()
                        .retry_details(retry.build())
                        .build(),
                )
                .build()
        };
        let at = |n: u64| Some(std::time::UNIX_EPOCH + std::time::Duration::from_secs(n));
        let ops = fold_history(&[failed(1, 100, Some(30))]);
        assert_eq!(
            step_details(&ops[0]).expect("step").next_attempt_time(),
            at(130)
        );
        // A later failure without a delay keeps the earlier time, and the
        // latest failure with a delay replaces it.
        let ops = fold_history(&[failed(1, 100, Some(30)), failed(2, 200, None)]);
        assert_eq!(
            step_details(&ops[0]).expect("step").next_attempt_time(),
            at(130)
        );
        let ops = fold_history(&[failed(1, 100, Some(30)), failed(2, 200, Some(5))]);
        assert_eq!(
            step_details(&ops[0]).expect("step").next_attempt_time(),
            at(205)
        );
        // No delay, a negative delay or no timestamp gives no time.
        let ops = fold_history(&[failed(1, 100, None)]);
        assert_eq!(
            step_details(&ops[0]).expect("step").next_attempt_time(),
            None
        );
        let ops = fold_history(&[failed(1, 100, Some(-1))]);
        assert_eq!(
            step_details(&ops[0]).expect("step").next_attempt_time(),
            None
        );
        let untimed = Event::builder()
            .event_type(EventType::StepFailed)
            .id("s")
            .event_id(1)
            .step_failed_details(
                StepFailedDetails::builder()
                    .retry_details(
                        RetryDetails::builder()
                            .current_attempt(1)
                            .next_attempt_delay_seconds(30)
                            .build(),
                    )
                    .build(),
            )
            .build();
        let ops = fold_history(&[untimed]);
        assert_eq!(
            step_details(&ops[0]).expect("step").next_attempt_time(),
            None
        );
    }

    #[test]
    fn fold_history_keeps_start_time_of_an_untimed_first_event() {
        // The first event carries no timestamp and a later one does. The
        // start time is the first event's timestamp, so it stays `None`.
        let untimed = Event::builder()
            .event_type(EventType::StepStarted)
            .id("s")
            .event_id(1)
            .build();
        let timed = Event::builder()
            .event_type(EventType::StepSucceeded)
            .id("s")
            .event_id(2)
            .event_timestamp(DateTime::from_secs(200))
            .build();
        let ops = fold_history(&[untimed, timed]);
        assert_eq!(ops[0].start_time(), None);
        assert_eq!(
            ops[0].end_time(),
            Some(std::time::UNIX_EPOCH + std::time::Duration::from_secs(200))
        );
    }

    #[test]
    fn fold_history_clears_next_attempt_time_when_the_latest_is_unrepresentable() {
        use aws_sdk_lambda::types::{RetryDetails, StepFailedDetails};
        let failed = |id: i32, at: Option<i64>, delay: i32| {
            let mut event = Event::builder()
                .event_type(EventType::StepFailed)
                .id("s")
                .event_id(id)
                .step_failed_details(
                    StepFailedDetails::builder()
                        .retry_details(
                            RetryDetails::builder()
                                .current_attempt(1)
                                .next_attempt_delay_seconds(delay)
                                .build(),
                        )
                        .build(),
                );
            if let Some(at) = at {
                event = event.event_timestamp(DateTime::from_secs(at));
            }
            event.build()
        };
        let next = |events: &[Event]| {
            step_details(&fold_history(events)[0])
                .expect("step")
                .next_attempt_time()
        };
        let earlier = failed(1, Some(100), 30);
        assert!(next(std::slice::from_ref(&earlier)).is_some());
        // The latest failure carries a delay but no timestamp.
        assert_eq!(next(&[earlier.clone(), failed(2, None, 5)]), None);
        // The latest failure carries a negative delay.
        assert_eq!(next(&[earlier.clone(), failed(2, Some(200), -1)]), None);
        // The latest failure's timestamp plus its delay overflows.
        let last = std::time::UNIX_EPOCH
            .checked_add(std::time::Duration::from_secs(i64::MAX.unsigned_abs()))
            .expect("in the SystemTime range on this platform");
        assert!(
            last.checked_add(std::time::Duration::from_secs(5))
                .is_none()
        );
        assert_eq!(
            std::time::SystemTime::try_from(DateTime::from_secs(i64::MAX)).ok(),
            Some(last)
        );
        assert_eq!(next(&[earlier, failed(2, Some(i64::MAX), 5)]), None);
    }

    #[test]
    fn fold_history_clears_end_time_when_a_retry_starts() {
        let event = |id: i32, kind: EventType, at: i64| {
            Event::builder()
                .event_type(kind)
                .id("s")
                .event_id(id)
                .event_timestamp(DateTime::from_secs(at))
                .build()
        };
        let at = |n: u64| Some(std::time::UNIX_EPOCH + std::time::Duration::from_secs(n));
        let failed = [
            event(1, EventType::StepStarted, 100),
            event(2, EventType::StepFailed, 101),
        ];
        assert_eq!(fold_history(&failed)[0].end_time(), at(101));
        let retried = [
            event(1, EventType::StepStarted, 100),
            event(2, EventType::StepFailed, 101),
            event(3, EventType::StepStarted, 131),
        ];
        let ops = fold_history(&retried);
        assert_eq!(ops[0].start_time(), at(100));
        assert_eq!(ops[0].end_time(), None);
    }

    #[tokio::test]
    async fn display_prints_js_table() {
        use std::fmt::Write as _;
        let result = LocalRunner::new()
            .run(
                |_e: (), ctx: DurableContext| async move {
                    ctx.step(|_| async { Ok(1_i32) }).name("fetch").await?;
                    ctx.run_in_child_context(|child| async move {
                        child.step(|_| async { Ok(2_i32) }).name("inner").await?;
                        Ok(())
                    })
                    .name("group")
                    .await?;
                    ctx.wait(std::time::Duration::from_secs(1))
                        .name("cooldown")
                        .await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        let g = result.operation("group").expect("the context").id();
        let expected = format!(
            "parentId | name | type | subType | status | duration\n\
             - | fetch | STEP | Step | SUCCEEDED | 0ms\n\
             - | group | CONTEXT | RunInChildContext | SUCCEEDED | 0ms\n\
             {g} | inner | STEP | Step | SUCCEEDED | 0ms\n\
             - | cooldown | WAIT | Wait | SUCCEEDED | 1000ms\n"
        );
        assert_eq!(
            result
                .display_with(columns_without(&["start_time", "end_time"]))
                .to_string(),
            expected
        );
        let cell = |t: Option<std::time::SystemTime>| iso_millis(unix_millis(t.expect("a time")));
        let mut full =
            "parentId | name | type | subType | status | startTime | endTime | duration\n"
                .to_owned();
        for (op, duration) in result
            .operations()
            .iter()
            .zip(["0ms", "0ms", "0ms", "1000ms"])
        {
            writeln!(
                full,
                "{} | {} | {} | {} | {} | {} | {} | {duration}",
                op.parent_id().unwrap_or("-"),
                op.name().unwrap_or("-"),
                op.kind().wire_str(),
                op.sub_type().unwrap_or("-"),
                op.status().wire_str(),
                cell(op.start_time()),
                cell(op.end_time()),
            )
            .expect("write to a String");
        }
        assert_eq!(format!("{result}"), full);
    }

    #[tokio::test]
    async fn display_with_selects_columns() {
        let result = LocalRunner::new()
            .run(
                |_e: (), ctx: DurableContext| async move {
                    ctx.step(|_| async { Ok(1_i32) }).name("fetch").await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(
            result
                .display_with(columns_only(&["name", "status"]))
                .to_string(),
            "name | status\nfetch | SUCCEEDED\n"
        );
    }

    #[tokio::test]
    async fn display_prints_dash_for_absent_values() {
        let result = LocalRunner::new()
            .run(
                |_e: (), ctx: DurableContext| async move {
                    ctx.step(|_| async { Ok(1_i32) }).await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(
            result
                .display_with(columns_only(&["parent_id", "name"]))
                .to_string(),
            "parentId | name\n- | -\n"
        );
    }

    #[test]
    fn display_prints_dash_for_absent_times_and_sub_type() {
        // A record with no sub-type, no start and no end prints `-` in
        // those cells and in the duration.
        let op = TestOperation::started("x".to_owned(), RunnerKind::Step);
        let only_start = TestOperation {
            start_time: Some(std::time::UNIX_EPOCH),
            ..TestOperation::started("y".to_owned(), RunnerKind::Step)
        };
        let result = result_with(vec![op, only_start]);
        assert_eq!(
            result
                .display_with(columns_only(&[
                    "sub_type",
                    "start_time",
                    "end_time",
                    "duration"
                ]))
                .to_string(),
            "subType | startTime | endTime | duration\n\
             - | - | - | -\n\
             - | 1970-01-01T00:00:00.000Z | - | -\n"
        );
    }

    #[test]
    fn display_preserves_millisecond_fraction() {
        let op = TestOperation {
            start_time: Some(std::time::UNIX_EPOCH + std::time::Duration::from_millis(100_123)),
            ..TestOperation::started("x".to_owned(), RunnerKind::Step)
        };
        let result = result_with(vec![op]);
        let table = result
            .display_with(columns_only(&["start_time"]))
            .to_string();
        assert_eq!(table, "startTime\n1970-01-01T00:01:40.123Z\n");
        assert!(table.ends_with(".123Z\n"));
    }

    #[test]
    fn display_truncates_times_before_the_epoch_toward_zero() {
        // 1.5 ms before the epoch truncates to -1 ms, which prints as the
        // last millisecond of 1969 with the Euclidean remainder 999.
        let before = std::time::UNIX_EPOCH - std::time::Duration::from_micros(1_500);
        assert_eq!(unix_millis(before), -1);
        assert_eq!(iso_millis(-1), "1969-12-31T23:59:59.999Z");
        // A year the formatter cannot print prints `-`.
        assert_eq!(iso_millis(i128::from(i64::MAX)), "-");
        // Seconds that do not fit an `i64` print `-`.
        assert_eq!(iso_millis(i128::MAX), "-");
        assert_eq!(iso_millis(i128::MIN), "-");
    }

    #[test]
    fn unix_millis_keeps_the_i64_endpoints_and_beyond() {
        // The `i64::MIN` endpoint is a valid millisecond value. Narrowing
        // its magnitude to `i64` before negation would reject it.
        let min_span = std::time::Duration::from_millis(i64::MIN.unsigned_abs());
        let at_min = std::time::UNIX_EPOCH
            .checked_sub(min_span)
            .expect("in the SystemTime range on this platform");
        assert_eq!(unix_millis(at_min), i128::from(i64::MIN));
        let max_span = std::time::Duration::from_millis(i64::MAX.unsigned_abs());
        let at_max = std::time::UNIX_EPOCH
            .checked_add(max_span)
            .expect("in the SystemTime range on this platform");
        assert_eq!(unix_millis(at_max), i128::from(i64::MAX));
        // One millisecond past each endpoint is still exact.
        let one = std::time::Duration::from_millis(1);
        assert_eq!(
            unix_millis(at_min.checked_sub(one).expect("in range")),
            i128::from(i64::MIN) - 1
        );
        assert_eq!(
            unix_millis(at_max.checked_add(one).expect("in range")),
            i128::from(i64::MAX) + 1
        );
    }

    #[test]
    fn display_prints_duration_for_times_beyond_the_i64_millisecond_range() {
        // Both times exist, but neither fits `i64` milliseconds and neither
        // time cell can be formatted. The duration still prints exactly.
        let span = std::time::Duration::from_millis(i64::MAX.unsigned_abs())
            + std::time::Duration::from_millis(1);
        let op = TestOperation {
            start_time: std::time::UNIX_EPOCH.checked_sub(span),
            end_time: std::time::UNIX_EPOCH.checked_add(span),
            ..TestOperation::started("x".to_owned(), RunnerKind::Step)
        };
        assert!(op.start_time.is_some() && op.end_time.is_some());
        let table = result_with(vec![op])
            .display_with(columns_only(&["start_time", "end_time", "duration"]))
            .to_string();
        assert_eq!(
            table,
            "startTime | endTime | duration\n- | - | 18446744073709551616ms\n"
        );
    }

    #[test]
    fn display_prints_duration_across_the_whole_i64_range() {
        // Both times fit `i64` milliseconds, but `end - start` is about
        // 2^64 ms and does not fit an `i64`. The cell prints it exactly.
        let span = std::time::Duration::from_millis(i64::MAX.unsigned_abs());
        let op = TestOperation {
            start_time: std::time::UNIX_EPOCH.checked_sub(span),
            end_time: std::time::UNIX_EPOCH.checked_add(span),
            ..TestOperation::started("x".to_owned(), RunnerKind::Step)
        };
        assert!(op.start_time.is_some() && op.end_time.is_some());
        let table = result_with(vec![op])
            .display_with(columns_only(&["duration"]))
            .to_string();
        assert_eq!(table, "duration\n18446744073709551614ms\n");
    }

    #[test]
    fn display_empty_result() {
        let result = result_with(Vec::new());
        assert_eq!(format!("{result}"), "No operations found.\n");
    }

    #[test]
    fn print_columns_default_enables_every_column() {
        assert_eq!(PrintColumns::default(), columns_without(&[]));
    }

    // History events and invocation records.

    /// The event types of `events`, in order.
    fn event_types(events: &[Event]) -> Vec<EventType> {
        events
            .iter()
            .filter_map(|event| event.event_type().cloned())
            .collect()
    }

    /// The history events and the operations of a result.
    fn history_of<O>(result: &TestResult<O>) -> (Vec<Event>, Vec<TestOperation>) {
        (
            result.history_events().to_vec(),
            result.operations().to_vec(),
        )
    }

    /// Two steps: each step's `events()` holds only its own events.
    #[tokio::test]
    async fn operation_events_are_filtered_by_id() {
        let result = LocalRunner::new()
            .run(
                |(): (), ctx: DurableContext| async move {
                    ctx.step(|_| async { Ok(1_u32) }).name("a").await?;
                    ctx.step(|_| async { Ok(2_u32) }).name("b").await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        for name in ["a", "b"] {
            let op = result.operation(name).expect("the step");
            assert_eq!(
                event_types(op.events()),
                [EventType::StepStarted, EventType::StepSucceeded],
                "{name}"
            );
            assert!(
                op.events().iter().all(|e| e.id() == Some(op.id())),
                "{name}"
            );
        }
    }

    /// A synthetic event of `event_type` for the operation `id`, at `secs`.
    fn synthetic_event(event_id: i32, event_type: EventType, id: Option<&str>, secs: i64) -> Event {
        Event::builder()
            .event_id(event_id)
            .event_type(event_type)
            .set_id(id.map(ToOwned::to_owned))
            .event_timestamp(DateTime::from_secs(secs))
            .build()
    }

    /// An `ExecutionSucceeded` event whose result is `1`.
    fn execution_succeeded_event(event_id: i32) -> Event {
        Event::builder()
            .event_id(event_id)
            .event_type(EventType::ExecutionSucceeded)
            .execution_succeeded_details(
                aws_sdk_lambda::types::ExecutionSucceededDetails::builder()
                    .result(EventResult::builder().payload("1").build())
                    .build(),
            )
            .build()
    }

    /// `CloudRunner` returns the fetched history unchanged.
    #[tokio::test(start_paused = true)]
    async fn fold_history_keeps_service_events() {
        let events = vec![
            synthetic_event(1, EventType::ExecutionStarted, Some("execution"), 0),
            synthetic_event(2, EventType::StepStarted, Some("s"), 1),
            execution_succeeded_event(3),
        ];
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("1"),
                )))
                .with_history(Ok(events.clone())),
        );
        let result = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert_eq!(result.history_events(), events.as_slice());
        let step = result.operations().first().expect("the step");
        assert_eq!(step.events(), events.get(1..2).expect("the step event"));
    }

    /// `CloudRunner` returns the events of every history page, in page
    /// order.
    #[tokio::test(start_paused = true)]
    async fn cloud_history_keeps_all_pages() {
        let events = vec![
            synthetic_event(1, EventType::ExecutionStarted, Some("execution"), 0),
            synthetic_event(2, EventType::StepStarted, Some("s"), 1),
            synthetic_event(3, EventType::StepSucceeded, Some("s"), 2),
            synthetic_event(4, EventType::InvocationCompleted, None, 3),
            execution_succeeded_event(5),
        ];
        let page = |range: std::ops::Range<usize>| events.get(range).expect("a page").to_vec();
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("1"),
                )))
                .with_history_pages(vec![
                    Ok((page(0..2), Some("m1".to_owned()))),
                    Ok((page(2..4), Some("m2".to_owned()))),
                    Ok((page(4..5), None)),
                ]),
        );
        let result = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        assert_eq!(result.history_events(), events.as_slice());
        let markers: Vec<Option<String>> = api
            .history_calls()
            .into_iter()
            .map(|call| call.marker)
            .collect();
        assert_eq!(
            markers,
            [None, Some("m1".to_owned()), Some("m2".to_owned())]
        );
    }

    /// An `InvocationCompleted` event with details.
    fn invocation_completed_event(
        event_id: i32,
        start: i64,
        end: i64,
        request_id: &str,
        error: Option<&crate::WireError>,
    ) -> Event {
        Event::builder()
            .event_id(event_id)
            .event_type(EventType::InvocationCompleted)
            .invocation_completed_details(
                InvocationCompletedDetails::builder()
                    .start_timestamp(DateTime::from_secs(start))
                    .end_timestamp(DateTime::from_secs(end))
                    .request_id(request_id)
                    .set_error(error.map(event_error))
                    .build()
                    .expect("details"),
            )
            .build()
    }

    /// `CloudRunner` builds one record per `InvocationCompleted` event, in
    /// history order, and ignores the other events.
    #[tokio::test(start_paused = true)]
    async fn fold_history_builds_invocations() {
        let failure = crate::WireError::new(Some("Bad"), Some("no"));
        let events = vec![
            synthetic_event(1, EventType::ExecutionStarted, Some("execution"), 0),
            invocation_completed_event(2, 10, 11, "r1", None),
            invocation_completed_event(3, 20, 22, "r2", Some(&failure)),
            execution_succeeded_event(4),
        ];
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("1"),
                )))
                .with_history(Ok(events)),
        );
        let result = CloudRunner::new("f")
            .run_with::<_, u32>(Arc::clone(&api), Vec::new())
            .await
            .expect("runner");
        let at = |secs: u64| Some(std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs));
        let records: Vec<_> = result
            .invocations()
            .iter()
            .map(|r| {
                (
                    r.start_time(),
                    r.end_time(),
                    r.request_id().map(ToOwned::to_owned),
                    r.error().cloned(),
                )
            })
            .collect();
        assert_eq!(
            records,
            [
                (at(10), at(11), Some("r1".to_owned()), None),
                (at(20), at(22), Some("r2".to_owned()), Some(failure)),
            ]
        );
        assert_eq!(result.invocation_count(), 2);
    }

    /// An `InvocationCompleted` event without details gives a record with
    /// every field absent.
    #[test]
    fn invocation_without_details_has_no_fields() {
        let record = TestInvocation::from_event(&synthetic_event(
            1,
            EventType::InvocationCompleted,
            None,
            0,
        ));
        assert_eq!(record.start_time(), None);
        assert_eq!(record.end_time(), None);
        assert_eq!(record.request_id(), None);
        assert_eq!(record.error(), None);
    }

    /// One wait: a pending invocation, then a succeeded one, one second
    /// later.
    #[tokio::test]
    async fn invocations_record_pending_then_succeeded() {
        let result = LocalRunner::new()
            .run(
                |(): (), ctx: DurableContext| async move {
                    ctx.wait(std::time::Duration::from_secs(1)).await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        let records = result.invocations();
        let ids: Vec<_> = records.iter().map(TestInvocation::request_id).collect();
        assert_eq!(
            ids,
            [Some("local-invocation-1"), Some("local-invocation-2")]
        );
        let first = records.first().expect("the first record");
        let second = records.get(1).expect("the second record");
        assert_eq!(
            second.start_time(),
            first
                .start_time()
                .map(|t| t + std::time::Duration::from_secs(1))
        );
        assert!(records.iter().all(|r| r.error().is_none()));
        assert_eq!(result.invocation_count(), 2);
        assert_eq!(result.invocation_count(), records.len());
    }

    /// An invocation fault records its message and no error type.
    #[tokio::test]
    async fn invocations_record_retrying_fault() {
        let result = LocalRunner::new()
            .fail_checkpoints_after_retryable(1, 1)
            .run(
                |(): (), ctx: DurableContext| async move {
                    ctx.step(|_| async { Ok(1_u32) }).name("a").await?;
                    ctx.step(|_| async { Ok(2_u32) }).name("b").await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error());
        let errors: Vec<_> = result
            .invocations()
            .iter()
            .filter_map(TestInvocation::error)
            .collect();
        assert_eq!(errors.len(), 1, "{errors:?}");
        let fault = errors.first().expect("the fault");
        assert!(
            fault
                .error_message()
                .is_some_and(|m| m.contains("injected retryable checkpoint failure")),
            "{fault:?}"
        );
        assert_eq!(fault.error_type(), None);
        assert_eq!(result.invocation_count(), result.invocations().len());
    }

    /// A `FAILED` invocation records the handler's error.
    #[tokio::test]
    async fn invocations_record_failed() {
        let result = LocalRunner::new()
            .run(
                |(): (), _ctx: DurableContext| async move { Err::<(), BoxError>("boom".into()) },
                (),
            )
            .await
            .expect("runner");
        let records = result.invocations();
        assert_eq!(records.len(), 1);
        assert_eq!(
            records
                .first()
                .and_then(TestInvocation::error)
                .and_then(crate::WireError::error_message),
            Some("boom")
        );
    }

    /// A `FAILED` envelope's stack trace reaches both the
    /// `InvocationCompleted` event and the invocation record.
    #[tokio::test]
    async fn invocations_record_failed_stack_trace() {
        let result = LocalRunner::new()
            .run(
                |(): (), _ctx: DurableContext| async move {
                    Err::<(), BoxError>(error_with_stack_trace())
                },
                (),
            )
            .await
            .expect("runner");
        let frames = ["frame-1".to_owned(), "frame-2".to_owned()];
        let event_frames = result
            .history_events()
            .iter()
            .find(|e| e.event_type() == Some(&EventType::InvocationCompleted))
            .and_then(Event::invocation_completed_details)
            .and_then(InvocationCompletedDetails::error)
            .and_then(crate::lambda_types::EventError::payload)
            .map(|payload| payload.stack_trace().to_vec());
        assert_eq!(event_frames.as_deref(), Some(frames.as_slice()));
        let records = result.invocations();
        assert_eq!(records.len(), 1);
        assert_eq!(
            records
                .first()
                .and_then(TestInvocation::error)
                .map(crate::WireError::stack_trace),
            Some(frames.as_slice())
        );
    }

    /// A run that reaches `max_invocations` with work pending has no
    /// execution end event.
    #[tokio::test]
    async fn max_invocations_run_has_no_execution_end_event() {
        let result = LocalRunner::new()
            .max_invocations(1)
            .run(
                |(): (), ctx: DurableContext| async move {
                    ctx.wait(std::time::Duration::from_secs(1)).await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_suspended());
        let message = result.error_message().unwrap_or_default();
        assert!(
            message.contains("did not terminate within 1 invocations"),
            "{message}"
        );
        let types = event_types(result.history_events());
        assert!(
            !types.contains(&EventType::ExecutionSucceeded)
                && !types.contains(&EventType::ExecutionFailed),
            "{types:?}"
        );
        assert_eq!(types.first(), Some(&EventType::ExecutionStarted));
        assert_eq!(result.invocation_count(), 1);
    }

    /// The handler sees the local request ID of its invocation.
    #[tokio::test]
    async fn handler_sees_local_request_id() {
        let result = LocalRunner::new()
            .run(
                |(): (), ctx: DurableContext| async move {
                    Ok::<_, BoxError>(ctx.lambda_context().request_id.clone())
                },
                (),
            )
            .await
            .expect("runner");
        assert_eq!(
            result.output().map(String::as_str),
            Some("local-invocation-1")
        );
    }

    /// A run that ends with work still pending has no execution end event.
    #[tokio::test]
    async fn suspended_run_has_no_execution_end_event() {
        let result = LocalRunner::new()
            .run(
                |(): (), ctx: DurableContext| async move {
                    let cb = ctx.create_callback::<u32>().await?;
                    cb.result().await?;
                    Ok::<_, BoxError>(())
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_suspended());
        assert_eq!(
            event_types(result.history_events()),
            [
                EventType::ExecutionStarted,
                EventType::CallbackStarted,
                EventType::InvocationCompleted,
            ]
        );
        // A callback without a timeout or a heartbeat sends no options.
        let details = result
            .history_events()
            .get(1)
            .and_then(Event::callback_started_details)
            .expect("callback started details");
        assert_eq!(details.timeout(), None);
        assert_eq!(details.heartbeat_timeout(), None);
    }

    /// The events a direct checkpoint of `updates` records.
    async fn checkpoint_events(updates: Vec<OperationUpdate>) -> Vec<Event> {
        let backend = Backend::new(QueuedReplies::default(), None);
        backend
            .checkpoint("arn", "token", updates)
            .await
            .expect("checkpoint accepted");
        backend.events()
    }

    /// An `EXECUTION` update produces no event of its own.
    #[tokio::test]
    async fn execution_update_has_no_event() {
        let update = OperationUpdate::builder()
            .id("execution")
            .r#type(OperationType::Execution)
            .action(OperationAction::Succeed)
            .payload("1")
            .build()
            .expect("valid update");
        assert_eq!(checkpoint_events(vec![update]).await, []);
    }

    /// A WAIT `START` without wait options records a duration of 0.
    #[tokio::test]
    async fn wait_start_without_duration_records_zero() {
        let update = OperationUpdate::builder()
            .id("w")
            .r#type(OperationType::Wait)
            .action(OperationAction::Start)
            .build()
            .expect("valid update");
        let events = checkpoint_events(vec![update]).await;
        let event = events.first().expect("the event");
        let details = event.wait_started_details().expect("wait started details");
        assert_eq!(details.duration(), 0);
        assert_eq!(
            Some(details.scheduled_end_timestamp()),
            event.event_timestamp()
        );
    }

    /// A `CHAINED_INVOKE` `START` without options records the empty function
    /// name.
    #[tokio::test]
    async fn invoke_start_without_options_records_no_function() {
        let update = OperationUpdate::builder()
            .id("i")
            .r#type(OperationType::ChainedInvoke)
            .action(OperationAction::Start)
            .build()
            .expect("valid update");
        let events = checkpoint_events(vec![update]).await;
        let details = events
            .first()
            .and_then(Event::chained_invoke_started_details)
            .expect("chained invoke started details");
        assert_eq!(details.function_name(), "");
        assert_eq!(details.input(), None);
    }

    /// The owner of a row's event: the execution, an invocation, or the
    /// operation with a name.
    #[derive(Debug, Clone, Copy)]
    enum Owner {
        Execution,
        Invocation,
        Op(&'static str),
    }

    /// One event a row selects: the first event of `event_type`, its owner,
    /// and the expected detail fields given the owner's operation.
    struct EventCheck {
        event_type: EventType,
        owner: Owner,
        expected: fn(Option<&TestOperation>) -> serde_json::Value,
    }

    type RowFuture = Pin<Box<dyn Future<Output = (Vec<Event>, Vec<TestOperation>)>>>;

    /// One row of the table: its number, its setup, and its checks.
    struct EventRow {
        row: usize,
        setup: fn() -> RowFuture,
        checks: Vec<EventCheck>,
    }

    /// Renders an `EventInput` as JSON.
    fn input_json(input: Option<&EventInput>) -> serde_json::Value {
        input.map_or(
            serde_json::Value::Null,
            |i| serde_json::json!({ "payload": i.payload(), "truncated": i.truncated() }),
        )
    }

    /// Renders an `EventResult` as JSON.
    fn result_json(result: Option<&EventResult>) -> serde_json::Value {
        result.map_or(
            serde_json::Value::Null,
            |r| serde_json::json!({ "payload": r.payload(), "truncated": r.truncated() }),
        )
    }

    /// Renders an `EventError` as JSON.
    fn error_json(error: Option<&EventError>) -> serde_json::Value {
        error.map_or(serde_json::Value::Null, |e| {
            serde_json::json!({
                "payload": e.payload().map(|p| serde_json::json!({
                    "type": p.error_type(),
                    "message": p.error_message(),
                    "data": p.error_data(),
                    "stack": p.stack_trace(),
                })),
                "truncated": e.truncated(),
            })
        })
    }

    /// Renders `RetryDetails` as JSON.
    fn retry_json(retry: Option<&RetryDetails>) -> serde_json::Value {
        retry.map_or(serde_json::Value::Null, |r| {
            serde_json::json!({
                "current_attempt": r.current_attempt(),
                "next_attempt_delay_seconds": r.next_attempt_delay_seconds(),
            })
        })
    }

    /// The seconds from the event's timestamp to `t`.
    fn secs_after(event: &Event, t: &DateTime) -> i64 {
        t.secs() - event.event_timestamp().expect("a timestamp").secs()
    }

    /// Every field of the detail struct of the event's type, as JSON. A
    /// timestamp renders as the seconds after the event's timestamp.
    #[expect(clippy::too_many_lines)] // reason: one arm per event type
    fn details_json(event: &Event) -> serde_json::Value {
        use serde_json::json;
        match event.event_type().expect("an event type") {
            EventType::ExecutionStarted => {
                let d = event.execution_started_details().expect("details");
                {
                    json!({ "input": input_json(d.input()), "execution_timeout": d.execution_timeout() })
                }
            }
            EventType::ExecutionSucceeded => {
                let d = event.execution_succeeded_details().expect("details");
                json!({ "result": result_json(d.result()) })
            }
            EventType::ExecutionFailed => {
                let d = event.execution_failed_details().expect("details");
                json!({ "error": error_json(d.error()) })
            }
            EventType::StepStarted => {
                event.step_started_details().expect("details");
                json!({})
            }
            EventType::StepSucceeded => {
                let d = event.step_succeeded_details().expect("details");
                {
                    json!({ "result": result_json(d.result()), "retry_details": retry_json(d.retry_details()) })
                }
            }
            EventType::StepFailed => {
                let d = event.step_failed_details().expect("details");
                {
                    json!({ "error": error_json(d.error()), "retry_details": retry_json(d.retry_details()) })
                }
            }
            EventType::ContextStarted => {
                event.context_started_details().expect("details");
                json!({})
            }
            EventType::ContextSucceeded => {
                let d = event.context_succeeded_details().expect("details");
                json!({ "result": result_json(d.result()) })
            }
            EventType::ContextFailed => {
                let d = event.context_failed_details().expect("details");
                json!({ "error": error_json(d.error()) })
            }
            EventType::WaitStarted => {
                let d = event.wait_started_details().expect("details");
                {
                    json!({
                        "duration": d.duration(),
                        "scheduled_end_after": secs_after(event, d.scheduled_end_timestamp()),
                    })
                }
            }
            EventType::WaitSucceeded => {
                let d = event.wait_succeeded_details().expect("details");
                json!({ "duration": d.duration() })
            }
            EventType::WaitCancelled => {
                let d = event.wait_cancelled_details().expect("details");
                json!({ "error": error_json(d.error()) })
            }
            EventType::CallbackStarted => {
                let d = event.callback_started_details().expect("details");
                {
                    json!({
                        "callback_id": d.callback_id(),
                        "timeout": d.timeout(),
                        "heartbeat_timeout": d.heartbeat_timeout(),
                    })
                }
            }
            EventType::CallbackSucceeded => {
                let d = event.callback_succeeded_details().expect("details");
                json!({ "result": result_json(d.result()) })
            }
            EventType::CallbackFailed => {
                let d = event.callback_failed_details().expect("details");
                json!({ "error": error_json(d.error()) })
            }
            EventType::CallbackTimedOut => {
                let d = event.callback_timed_out_details().expect("details");
                json!({ "error": error_json(d.error()) })
            }
            EventType::ChainedInvokeStarted => {
                let d = event.chained_invoke_started_details().expect("details");
                {
                    json!({
                        "function_name": d.function_name(),
                        "tenant_id": d.tenant_id(),
                        "input": input_json(d.input()),
                        "executed_version": d.executed_version(),
                        "durable_execution_arn": d.durable_execution_arn(),
                    })
                }
            }
            EventType::ChainedInvokeSucceeded => {
                let d = event.chained_invoke_succeeded_details().expect("details");
                json!({ "result": result_json(d.result()) })
            }
            EventType::ChainedInvokeFailed => {
                let d = event.chained_invoke_failed_details().expect("details");
                json!({ "error": error_json(d.error()) })
            }
            EventType::InvocationCompleted => {
                let d = event.invocation_completed_details().expect("details");
                {
                    json!({
                        "start_after": secs_after(event, d.start_timestamp()),
                        "end_after": secs_after(event, d.end_timestamp()),
                        "request_id": d.request_id(),
                        "error": error_json(d.error()),
                    })
                }
            }
            // The local backend records no other type, so the comparison
            // with the expected fields fails.
            _ => serde_json::Value::Null,
        }
    }

    /// The number of `*_details()` getters of `event` that return a value.
    fn details_count(event: &Event) -> usize {
        [
            event.execution_started_details().is_some(),
            event.execution_succeeded_details().is_some(),
            event.execution_failed_details().is_some(),
            event.execution_timed_out_details().is_some(),
            event.execution_stopped_details().is_some(),
            event.context_started_details().is_some(),
            event.context_succeeded_details().is_some(),
            event.context_failed_details().is_some(),
            event.wait_started_details().is_some(),
            event.wait_succeeded_details().is_some(),
            event.wait_cancelled_details().is_some(),
            event.step_started_details().is_some(),
            event.step_succeeded_details().is_some(),
            event.step_failed_details().is_some(),
            event.chained_invoke_started_details().is_some(),
            event.chained_invoke_succeeded_details().is_some(),
            event.chained_invoke_failed_details().is_some(),
            event.chained_invoke_timed_out_details().is_some(),
            event.chained_invoke_stopped_details().is_some(),
            event.callback_started_details().is_some(),
            event.callback_succeeded_details().is_some(),
            event.callback_failed_details().is_some(),
            event.callback_timed_out_details().is_some(),
            event.invocation_completed_details().is_some(),
        ]
        .into_iter()
        .filter(|set| *set)
        .count()
    }

    /// The JSON of an error payload with these fields and no stack trace.
    fn error_payload(
        error_type: Option<&str>,
        message: Option<&str>,
        data: Option<&str>,
    ) -> serde_json::Value {
        serde_json::json!({
            "payload": { "type": error_type, "message": message, "data": data, "stack": [] },
            "truncated": null,
        })
    }

    /// The JSON of an `EventResult` or `EventInput` with `payload`.
    fn payload(payload: &str) -> serde_json::Value {
        serde_json::json!({ "payload": payload, "truncated": null })
    }

    /// The JSON of retry details.
    fn retry(current_attempt: i32, delay: Option<i32>) -> serde_json::Value {
        serde_json::json!({ "current_attempt": current_attempt, "next_attempt_delay_seconds": delay })
    }

    /// The message the SDK records for [`bad_no`]: the wrapped error's
    /// message, without the type name, which the record carries as its
    /// type.
    const BAD_NO_MESSAGE: &str = "no";

    /// The error `Bad` / `no`.
    fn bad_no() -> BoxError {
        crate::TypedError::with_type("Bad", "no").into()
    }

    /// A callback `cb` with a 60 s timeout and a 10 s heartbeat, run with
    /// `reply` queued.
    fn callback_row(reply: CallbackReply) -> RowFuture {
        Box::pin(async move {
            let result = LocalRunner::new()
                .callback(reply)
                .run(
                    |(): (), ctx: DurableContext| async move {
                        let cb = ctx
                            .create_callback::<u32>()
                            .name("cb")
                            .timeout(std::time::Duration::from_secs(60))
                            .heartbeat_timeout(std::time::Duration::from_secs(10))
                            .await?;
                        Ok::<_, BoxError>(cb.result().await?)
                    },
                    (),
                )
                .await
                .expect("runner");
            history_of(&result)
        })
    }

    /// A handler run once on a fresh runner, with event `()`.
    fn handler_row<O, F, Fut>(handler: F) -> RowFuture
    where
        O: Serialize + DeserializeOwned + Send + 'static,
        F: Fn((), DurableContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<O, BoxError>> + Send + 'static,
    {
        Box::pin(async move {
            let result = LocalRunner::new().run(handler, ()).await.expect("runner");
            history_of(&result)
        })
    }

    /// The table of rule 5: one row per cause, with the detail fields of
    /// rule 4.
    #[expect(clippy::too_many_lines)] // reason: one entry per row of the table
    fn event_rows() -> Vec<EventRow> {
        use serde_json::json;
        vec![
            EventRow {
                row: 1,
                setup: || {
                    Box::pin(async {
                        let result = LocalRunner::new()
                            .run(
                                |_e: serde_json::Value, _ctx: DurableContext| async move {
                                    Ok::<_, BoxError>(1_u32)
                                },
                                json!({ "k": 1 }),
                            )
                            .await
                            .expect("runner");
                        history_of(&result)
                    })
                },
                checks: vec![
                    EventCheck {
                        event_type: EventType::ExecutionStarted,
                        owner: Owner::Execution,
                        expected: |_| json!({ "input": payload("{\"k\":1}"), "execution_timeout": 0 }),
                    },
                    EventCheck {
                        event_type: EventType::ExecutionSucceeded,
                        owner: Owner::Execution,
                        expected: |_| json!({ "result": payload("1") }),
                    },
                ],
            },
            EventRow {
                row: 2,
                setup: || {
                    handler_row(|(), ctx: DurableContext| async move {
                        Ok(ctx.step(|_| async { Ok(7_u32) }).name("s").await?)
                    })
                },
                checks: vec![
                    EventCheck {
                        event_type: EventType::StepStarted,
                        owner: Owner::Op("s"),
                        expected: |_| json!({}),
                    },
                    EventCheck {
                        event_type: EventType::StepSucceeded,
                        owner: Owner::Op("s"),
                        expected: |_| json!({ "result": payload("7"), "retry_details": retry(1, None) }),
                    },
                ],
            },
            EventRow {
                row: 3,
                setup: || {
                    handler_row(|(), ctx: DurableContext| async move {
                        ctx.step(|_| async { Err::<u32, BoxError>(bad_no()) })
                            .name("s")
                            .retry_strategy(|_err, _attempt| RetryDecision::Stop)
                            .await?;
                        Ok(())
                    })
                },
                checks: vec![EventCheck {
                    event_type: EventType::StepFailed,
                    owner: Owner::Op("s"),
                    expected: |_| {
                        json!({
                            "error": error_payload(Some("Bad"), Some(BAD_NO_MESSAGE), None),
                            "retry_details": retry(1, None),
                        })
                    },
                }],
            },
            EventRow {
                row: 4,
                setup: || {
                    handler_row(|(), ctx: DurableContext| async move {
                        Ok(ctx
                            .step(|step| async move {
                                if step.attempt() == 1 {
                                    return Err::<u32, BoxError>(bad_no());
                                }
                                Ok(1)
                            })
                            .name("s")
                            .retry_strategy(|_err, _attempt| {
                                RetryDecision::retry(std::time::Duration::from_secs(5))
                            })
                            .await?)
                    })
                },
                checks: vec![EventCheck {
                    event_type: EventType::StepFailed,
                    owner: Owner::Op("s"),
                    expected: |_| {
                        json!({
                            "error": error_payload(Some("Bad"), Some(BAD_NO_MESSAGE), None),
                            "retry_details": retry(1, Some(5)),
                        })
                    },
                }],
            },
            EventRow {
                row: 5,
                setup: || {
                    handler_row(|(), ctx: DurableContext| async move {
                        Ok(ctx
                            .run_in_child_context(|_child| async move { Ok(2_u32) })
                            .name("c")
                            .await?)
                    })
                },
                checks: vec![
                    EventCheck {
                        event_type: EventType::ContextStarted,
                        owner: Owner::Op("c"),
                        expected: |_| json!({}),
                    },
                    EventCheck {
                        event_type: EventType::ContextSucceeded,
                        owner: Owner::Op("c"),
                        expected: |_| json!({ "result": payload("2") }),
                    },
                ],
            },
            EventRow {
                row: 6,
                setup: || {
                    handler_row(|(), ctx: DurableContext| async move {
                        Ok(ctx
                            .run_in_child_context(
                                |_child| async move { Err::<u32, BoxError>(bad_no()) },
                            )
                            .name("c")
                            .await?)
                    })
                },
                checks: vec![EventCheck {
                    event_type: EventType::ContextFailed,
                    owner: Owner::Op("c"),
                    expected: |_| json!({ "error": error_payload(Some("Bad"), Some(BAD_NO_MESSAGE), None) }),
                }],
            },
            EventRow {
                row: 7,
                setup: || {
                    handler_row(|(), ctx: DurableContext| async move {
                        Ok(ctx
                            .wait(std::time::Duration::from_secs(3))
                            .name("w")
                            .await?)
                    })
                },
                checks: vec![
                    EventCheck {
                        event_type: EventType::WaitStarted,
                        owner: Owner::Op("w"),
                        expected: |_| json!({ "duration": 3, "scheduled_end_after": 3 }),
                    },
                    EventCheck {
                        event_type: EventType::WaitSucceeded,
                        owner: Owner::Op("w"),
                        expected: |_| json!({ "duration": 3 }),
                    },
                ],
            },
            EventRow {
                row: 8,
                setup: || {
                    Box::pin(async {
                        let backend = Backend::new(QueuedReplies::default(), None);
                        let start = OperationUpdate::builder()
                            .id("w")
                            .name("w")
                            .r#type(OperationType::Wait)
                            .action(OperationAction::Start)
                            .wait_options(
                                aws_sdk_lambda::types::WaitOptions::builder()
                                    .wait_seconds(3)
                                    .build(),
                            )
                            .build()
                            .expect("valid update");
                        let cancel = OperationUpdate::builder()
                            .id("w")
                            .name("w")
                            .r#type(OperationType::Wait)
                            .action(OperationAction::Cancel)
                            .build()
                            .expect("valid update");
                        for update in [start, cancel] {
                            backend
                                .checkpoint("arn", "token", vec![update])
                                .await
                                .expect("checkpoint accepted");
                        }
                        (backend.events(), backend.snapshot_operations())
                    })
                },
                checks: vec![EventCheck {
                    event_type: EventType::WaitCancelled,
                    owner: Owner::Op("w"),
                    expected: |_| json!({ "error": null }),
                }],
            },
            EventRow {
                row: 9,
                setup: || callback_row(CallbackReply::success(&4_u32).expect("encodes")),
                checks: vec![
                    EventCheck {
                        event_type: EventType::CallbackStarted,
                        owner: Owner::Op("cb"),
                        expected: |op| {
                            let callback_id = match op.expect("the callback").details() {
                                TestOperationDetails::Callback(c) => {
                                    c.callback_id().map(ToOwned::to_owned)
                                }
                                _ => None,
                            };
                            json!({ "callback_id": callback_id, "timeout": 60, "heartbeat_timeout": 10 })
                        },
                    },
                    EventCheck {
                        event_type: EventType::CallbackSucceeded,
                        owner: Owner::Op("cb"),
                        expected: |_| json!({ "result": payload("4") }),
                    },
                ],
            },
            EventRow {
                row: 10,
                setup: || callback_row(CallbackReply::raw("\"r\"")),
                checks: vec![EventCheck {
                    event_type: EventType::CallbackSucceeded,
                    owner: Owner::Op("cb"),
                    expected: |_| json!({ "result": payload("\"r\"") }),
                }],
            },
            EventRow {
                row: 11,
                setup: || {
                    callback_row(CallbackReply::failure(
                        CallbackFailure::new("Declined", "no").with_error_data("d"),
                    ))
                },
                checks: vec![EventCheck {
                    event_type: EventType::CallbackFailed,
                    owner: Owner::Op("cb"),
                    expected: |_| json!({ "error": error_payload(Some("Declined"), Some("no"), Some("d")) }),
                }],
            },
            EventRow {
                row: 12,
                setup: || callback_row(CallbackReply::timeout()),
                checks: vec![EventCheck {
                    event_type: EventType::CallbackTimedOut,
                    owner: Owner::Op("cb"),
                    expected: |_| json!({ "error": error_payload(None, Some("Callback timed out"), None) }),
                }],
            },
            EventRow {
                row: 13,
                setup: || callback_row(CallbackReply::heartbeat_timeout()),
                checks: vec![EventCheck {
                    event_type: EventType::CallbackTimedOut,
                    owner: Owner::Op("cb"),
                    expected: |_| {
                        json!({
                            "error": error_payload(
                                Some("Callback.Heartbeat"),
                                Some("Callback timed out on heartbeat"),
                                None,
                            ),
                        })
                    },
                }],
            },
            EventRow {
                row: 14,
                setup: || {
                    Box::pin(async {
                        let result = LocalRunner::new()
                            .register_function("target", |_x: i32| async move {
                                Ok::<_, BoxError>(6_i32)
                            })
                            .run(
                                |(): (), ctx: DurableContext| async move {
                                    Ok(ctx.invoke::<i32>("target", 5).name("call").await?)
                                },
                                (),
                            )
                            .await
                            .expect("runner");
                        history_of(&result)
                    })
                },
                checks: vec![
                    EventCheck {
                        event_type: EventType::ChainedInvokeStarted,
                        owner: Owner::Op("call"),
                        expected: |_| {
                            json!({
                                "function_name": "target",
                                "tenant_id": null,
                                "input": payload("5"),
                                "executed_version": null,
                                "durable_execution_arn": null,
                            })
                        },
                    },
                    EventCheck {
                        event_type: EventType::ChainedInvokeSucceeded,
                        owner: Owner::Op("call"),
                        expected: |_| json!({ "result": payload("6") }),
                    },
                ],
            },
            EventRow {
                row: 15,
                setup: || {
                    Box::pin(async {
                        let result = LocalRunner::new()
                            .register_function("target", |_x: i32| async move {
                                Err::<i32, BoxError>(bad_no())
                            })
                            .run(
                                |(): (), ctx: DurableContext| async move {
                                    Ok(ctx.invoke::<i32>("target", 5).name("call").await?)
                                },
                                (),
                            )
                            .await
                            .expect("runner");
                        history_of(&result)
                    })
                },
                checks: vec![EventCheck {
                    event_type: EventType::ChainedInvokeFailed,
                    owner: Owner::Op("call"),
                    expected: |_| json!({ "error": error_payload(Some("Bad"), Some(BAD_NO_MESSAGE), None) }),
                }],
            },
            EventRow {
                row: 16,
                setup: || {
                    handler_row(|(), _ctx: DurableContext| async move {
                        Err::<(), BoxError>(
                            crate::TypedError::with_type("Bad", "no")
                                .with_error_data("d")
                                .into(),
                        )
                    })
                },
                checks: vec![
                    EventCheck {
                        event_type: EventType::InvocationCompleted,
                        owner: Owner::Invocation,
                        expected: |_| {
                            json!({
                                "start_after": 0,
                                "end_after": 0,
                                "request_id": "local-invocation-1",
                                "error": error_payload(Some("Bad"), Some(BAD_NO_MESSAGE), Some("d")),
                            })
                        },
                    },
                    EventCheck {
                        event_type: EventType::ExecutionFailed,
                        owner: Owner::Execution,
                        expected: |_| json!({ "error": error_payload(Some("Bad"), Some(BAD_NO_MESSAGE), Some("d")) }),
                    },
                ],
            },
            EventRow {
                row: 17,
                setup: || handler_row(poll_handler),
                checks: vec![EventCheck {
                    event_type: EventType::StepSucceeded,
                    owner: Owner::Op("poll"),
                    expected: |_| json!({ "result": payload("1"), "retry_details": retry(1, Some(1)) }),
                }],
            },
        ]
    }

    /// A `wait_for_condition` named `poll` whose check adds 1 to a state
    /// that starts at 0, and whose strategy continues with a 1 s delay
    /// while the state is below 2.
    async fn poll_handler((): (), ctx: DurableContext) -> Result<i32, BoxError> {
        use crate::builders::wait_for_condition::WaitDecision;
        Ok(ctx
            .wait_for_condition(|_ctx, state: i32| async move { Ok(state + 1) }, 0)
            .wait_strategy_fn(|state: i32, _attempt| {
                if state < 2 {
                    WaitDecision::continue_with(std::time::Duration::from_secs(1))
                } else {
                    WaitDecision::complete()
                }
            })
            .name("poll")
            .await?)
    }

    /// A `StepSucceeded` that carries a retry delay, as the service records
    /// a `wait_for_condition` check that continued, sets the next attempt
    /// time, and a later `StepSucceeded` without a delay keeps it.
    #[tokio::test]
    async fn fold_history_reads_the_delay_of_a_step_succeeded() {
        use aws_sdk_lambda::types::{
            EventResult, RetryDetails, StepStartedDetails, StepSucceededDetails,
        };
        let started = |id: i32, at: i64| {
            Event::builder()
                .event_type(EventType::StepStarted)
                .id("s")
                .event_id(id)
                .event_timestamp(DateTime::from_secs(at))
                .step_started_details(StepStartedDetails::builder().build())
                .build()
        };
        let succeeded = |id: i32, at: i64, result: &str, attempt: i32, delay: Option<i32>| {
            Event::builder()
                .event_type(EventType::StepSucceeded)
                .id("s")
                .event_id(id)
                .event_timestamp(DateTime::from_secs(at))
                .step_succeeded_details(
                    StepSucceededDetails::builder()
                        .result(EventResult::builder().payload(result).build())
                        .retry_details(
                            RetryDetails::builder()
                                .current_attempt(attempt)
                                .set_next_attempt_delay_seconds(delay)
                                .build(),
                        )
                        .build(),
                )
                .build()
        };
        let ops = fold_history(&[
            started(1, 100),
            succeeded(2, 100, "1", 1, Some(1)),
            started(3, 101),
            succeeded(4, 101, "2", 2, None),
        ]);
        let op = ops.first().expect("one operation");
        let step = step_details(op).expect("step");
        assert_eq!(
            (
                op.status(),
                op.result(),
                step.attempt(),
                step.next_attempt_time()
            ),
            (
                OperationStatus::Succeeded,
                Some("2"),
                1,
                Some(std::time::UNIX_EPOCH + std::time::Duration::from_secs(101)),
            )
        );

        // `CloudRunner`'s fold of `LocalRunner`'s history reports the next
        // attempt time `LocalRunner` reports.
        let result = LocalRunner::new()
            .run(poll_handler, ())
            .await
            .expect("runner");
        let local = result.operation("poll").expect("the condition");
        let folded = fold_history(result.history_events());
        let cloud = folded
            .iter()
            .find(|op| op.name() == Some("poll"))
            .expect("the folded condition");
        let local_time = step_details(local).expect("step").next_attempt_time();
        assert!(local_time.is_some());
        assert_eq!(
            step_details(cloud).expect("step").next_attempt_time(),
            local_time
        );
    }

    /// Each cause of rule 5 records its event, with the operation's
    /// identity and exactly the detail fields of rule 4.
    #[tokio::test]
    async fn local_events_cover_every_cause_and_detail() {
        let rows = event_rows();
        assert_eq!(rows.len(), 17);
        for EventRow { row, setup, checks } in rows {
            let (events, operations) = setup().await;
            for check in checks {
                let context = format!("row {row}, {:?}", check.event_type);
                let event = events
                    .iter()
                    .find(|e| e.event_type() == Some(&check.event_type))
                    .expect("the row records the event");
                let op = match check.owner {
                    Owner::Op(name) => Some(
                        operations
                            .iter()
                            .find(|op| op.name() == Some(name))
                            .expect("the row records the operation"),
                    ),
                    Owner::Execution | Owner::Invocation => None,
                };
                let identity = (
                    event.id(),
                    event.name(),
                    event.sub_type(),
                    event.parent_id(),
                );
                let expected_identity = match (check.owner, op) {
                    (Owner::Op(_), Some(op)) => {
                        (Some(op.id()), op.name(), op.sub_type(), op.parent_id())
                    }
                    (Owner::Execution, _) => (Some(EXECUTION_OPERATION_ID), None, None, None),
                    _ => (None, None, None, None),
                };
                assert_eq!(identity, expected_identity, "{context}");
                assert_eq!(details_json(event), (check.expected)(op), "{context}");
                assert_eq!(details_count(event), 1, "{context}");
            }
        }
    }

    /// The kind of an operation error, as text, for a handler to record.
    fn child_kind(err: &crate::OperationError) -> String {
        match err.kind() {
            crate::OperationErrorKind::ChildContext(child) => {
                format!("ChildContext({:?})", child.kind())
            }
            other => format!("{other:?}"),
        }
    }

    /// A virtual child `v` inside child `outer` writes no record: its step
    /// names `outer` as its parent, and replays after the wait without
    /// running again.
    #[tokio::test]
    async fn virtual_child_writes_no_context_record() {
        let runs = Arc::new(AtomicU32::new(0));
        let handler_runs = Arc::clone(&runs);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let runs = Arc::clone(&handler_runs);
                    async move {
                        let value = ctx
                            .run_in_child_context(move |outer| async move {
                                let value = outer
                                    .run_in_child_context(move |v| async move {
                                        Ok(v.step(move |_| async move {
                                            runs.fetch_add(1, Ordering::SeqCst);
                                            Ok(7_u32)
                                        })
                                        .name("inner")
                                        .await?)
                                    })
                                    .name("v")
                                    .virtual_context()
                                    .await?;
                                outer
                                    .wait(std::time::Duration::from_secs(1))
                                    .name("pause")
                                    .await?;
                                Ok(value)
                            })
                            .name("outer")
                            .await?;
                        Ok::<_, BoxError>(value)
                    }
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&7));
        assert!(result.operation("v").is_none(), "{result}");
        let outer = result.operation("outer").expect("outer");
        let inner = result.operation("inner").expect("inner");
        assert_eq!(inner.parent_id(), Some(outer.id()));
        assert_eq!(result.invocation_count(), 2);
        assert_eq!(runs.load(Ordering::SeqCst), 1);
    }

    /// A virtual child whose body fails returns `ChildContext(ChildFailed)`
    /// and writes no CONTEXT record.
    #[tokio::test]
    async fn virtual_child_failure_is_child_failed() {
        let kind: Arc<Mutex<Option<String>>> = Arc::default();
        let handler_kind = Arc::clone(&kind);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let kind = Arc::clone(&handler_kind);
                    async move {
                        let err = ctx
                            .run_in_child_context(|_v| async {
                                Err::<u32, BoxError>("nope".into())
                            })
                            .name("v")
                            .virtual_context()
                            .await
                            .expect_err("the body fails");
                        *kind.lock().expect("lock") = Some(child_kind(&err));
                        Ok::<_, BoxError>(())
                    }
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            kind.lock().expect("lock").as_deref(),
            Some("ChildContext(ChildFailed)")
        );
        assert!(
            result
                .operations()
                .iter()
                .all(|op| op.kind() != OperationKind::Context),
            "{result}"
        );
    }

    /// A virtual child's step and wait replay: the step body runs once
    /// over the two invocations the wait causes.
    #[tokio::test]
    async fn virtual_child_replays_its_operations() {
        let runs = Arc::new(AtomicU32::new(0));
        let handler_runs = Arc::clone(&runs);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let runs = Arc::clone(&handler_runs);
                    async move {
                        let value = ctx
                            .run_in_child_context(move |child| async move {
                                let value = child
                                    .step(move |_| async move {
                                        runs.fetch_add(1, Ordering::SeqCst);
                                        Ok(3_u32)
                                    })
                                    .name("s")
                                    .await?;
                                child
                                    .wait(std::time::Duration::from_secs(1))
                                    .name("w")
                                    .await?;
                                Ok(value)
                            })
                            .name("v")
                            .virtual_context()
                            .await?;
                        Ok::<_, BoxError>(value)
                    }
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&3));
        assert_eq!(result.invocation_count(), 2);
        assert_eq!(runs.load(Ordering::SeqCst), 1);
    }

    /// A summary set on a virtual child is never called, even for an
    /// oversized result.
    #[tokio::test]
    async fn virtual_child_never_calls_summary() {
        let calls = Arc::new(AtomicU32::new(0));
        let handler_calls = Arc::clone(&calls);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let calls = Arc::clone(&handler_calls);
                    async move {
                        let big = ctx
                            .run_in_child_context(|_v| async { Ok("x".repeat(300 * 1024)) })
                            .name("v")
                            .virtual_context()
                            .summary(move |_s: &String| {
                                calls.fetch_add(1, Ordering::SeqCst);
                                "unused".to_owned()
                            })
                            .await?;
                        Ok::<_, BoxError>(big.len())
                    }
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&307_200));
        assert_eq!(calls.load(Ordering::SeqCst), 0);
        assert!(result.operations().is_empty(), "{result}");
    }

    /// Runs, inside child `outer`, a virtual `with_retry` block named
    /// `name` whose attempt 1 fails and whose later attempts succeed, with
    /// a strategy that stops at attempt `stop_at`. Returns the run and the
    /// kind of the block's error, when it failed, with whether its source
    /// is the error of attempt 2.
    async fn virtual_with_retry_run(
        name: Option<&'static str>,
        fail_until: u32,
    ) -> (TestResult<u32>, Option<(String, bool)>) {
        let failure: Arc<Mutex<Option<(String, bool)>>> = Arc::default();
        let handler_failure = Arc::clone(&failure);
        let result = LocalRunner::new()
            .run(
                move |(), ctx: DurableContext| {
                    let failure = Arc::clone(&handler_failure);
                    async move {
                        let value = ctx
                            .run_in_child_context(move |outer| async move {
                                let block = outer
                                    .with_retry(move |_attempt_ctx, attempt| async move {
                                        if attempt <= fail_until {
                                            return Err::<u32, BoxError>(
                                                format!("fail on attempt {attempt}").into(),
                                            );
                                        }
                                        Ok(attempt)
                                    })
                                    .virtual_context()
                                    .retry_strategy(|_err, attempt| {
                                        if attempt >= 2 {
                                            RetryDecision::Stop
                                        } else {
                                            RetryDecision::retry(std::time::Duration::from_secs(1))
                                        }
                                    });
                                let block = match name {
                                    Some(name) => block.name(name),
                                    None => block,
                                };
                                match block.await {
                                    Ok(value) => Ok(value),
                                    Err(err) => {
                                        let last_is_attempt_2 = std::error::Error::source(&err)
                                            .and_then(std::error::Error::source)
                                            .and_then(|s| s.downcast_ref::<crate::OperationError>())
                                            .is_some_and(|last| {
                                                format!("{last:#}").contains("fail on attempt 2")
                                            });
                                        *failure.lock().expect("lock") =
                                            Some((child_kind(&err), last_is_attempt_2));
                                        Ok(0)
                                    }
                                }
                            })
                            .name("outer")
                            .await?;
                        Ok::<_, BoxError>(value)
                    }
                },
                (),
            )
            .await
            .expect("runner");
        let failure = failure.lock().expect("lock").clone();
        (result, failure)
    }

    /// An unnamed virtual `with_retry` block writes no record at its slot:
    /// its attempts name `outer` as their parent, and its wait is unnamed.
    #[tokio::test]
    async fn virtual_with_retry_writes_no_block_record() {
        let (result, failure) = virtual_with_retry_run(None, 1).await;
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(failure, None);
        assert_eq!(result.output(), Some(&2));
        let outer = result.operation("outer").expect("outer");
        let attempt = result.operation("attempt-1").expect("attempt-1");
        assert_eq!(attempt.parent_id(), Some(outer.id()));
        assert!(result.operation("attempt-2").is_some(), "{result}");
        let contexts: Vec<_> = result
            .operations()
            .iter()
            .filter(|op| op.kind() == OperationKind::Context)
            .filter_map(TestOperation::name)
            .collect();
        assert_eq!(contexts, ["outer", "attempt-1", "attempt-2"], "{result}");
        let waits: Vec<_> = result
            .operations()
            .iter()
            .filter(|op| op.kind() == OperationKind::Wait)
            .collect();
        assert_eq!(waits.len(), 1, "{result}");
        assert!(waits.iter().all(|w| w.name().is_none()), "{result}");
        assert!(
            waits.iter().all(|w| w.parent_id() == Some(outer.id())),
            "{result}"
        );
    }

    /// An exhausted virtual block fails with `ChildContext(ChildFailed)`
    /// whose source is the error of the last attempt, attempt 2.
    #[tokio::test]
    async fn virtual_with_retry_exhaustion_kind() {
        let (result, failure) = virtual_with_retry_run(None, 2).await;
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(
            failure,
            Some(("ChildContext(ChildFailed)".to_owned(), true)),
            "{result}"
        );
    }

    /// A virtual block named `b` names its attempts `b-attempt-N` and its
    /// waits `b-backoff-N`, under `outer`.
    #[tokio::test]
    async fn named_virtual_with_retry_prefixes_children() {
        let (result, failure) = virtual_with_retry_run(Some("b"), 1).await;
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(failure, None);
        let outer = result.operation("outer").expect("outer");
        let attempt = result.operation("b-attempt-1").expect("b-attempt-1");
        assert_eq!(attempt.parent_id(), Some(outer.id()));
        assert!(result.operation("b-attempt-2").is_some(), "{result}");
        assert!(result.operation("b-backoff-1").is_some(), "{result}");
        assert!(result.operation("b").is_none(), "{result}");
    }

    /// `.spawn()` of a virtual child and of a virtual `with_retry` block
    /// runs them, and writes no record for either.
    #[tokio::test]
    async fn spawned_virtual_child_and_block_write_no_record() {
        let result = LocalRunner::new()
            .run(
                |(), ctx: DurableContext| async move {
                    let child = ctx
                        .run_in_child_context(|v| async move {
                            Ok(v.step(|_| async { Ok(2_u32) }).name("s").await?)
                        })
                        .name("v")
                        .virtual_context()
                        .spawn();
                    let block = ctx
                        .with_retry(|_attempt_ctx, _attempt| async { Ok(3_u32) })
                        .name("b")
                        .virtual_context()
                        .spawn();
                    Ok::<_, BoxError>(child.await? + block.await?)
                },
                (),
            )
            .await
            .expect("runner");
        assert!(result.is_success(), "{:?}", result.error_message());
        assert_eq!(result.output(), Some(&5));
        assert!(result.operation("v").is_none(), "{result}");
        assert!(result.operation("b").is_none(), "{result}");
        assert_eq!(
            result.operation("s").and_then(TestOperation::parent_id),
            None
        );
        assert_eq!(
            result
                .operation("b-attempt-1")
                .and_then(TestOperation::parent_id),
            None
        );
    }

    // ---- Live control: RunHandle and OperationHandle (LocalRunner) ----

    /// The pause-resume handler: step `reserve-stock`, the
    /// `wait_for_callback` `manager-approval`, and step `ship-order`. The
    /// first step takes 200 ms, so a test can pause while it runs.
    async fn pause_resume_handler(
        event: serde_json::Value,
        ctx: DurableContext,
    ) -> Result<serde_json::Value, BoxError> {
        let order_id = event
            .get("orderId")
            .and_then(serde_json::Value::as_str)
            .unwrap_or("order-1")
            .to_owned();
        let id = order_id.clone();
        let reservation = ctx
            .step(move |_| async move {
                tokio::time::sleep(std::time::Duration::from_millis(200)).await;
                Ok(format!("reserved-{id}"))
            })
            .name("reserve-stock")
            .await?;
        let approval = ctx
            .wait_for_callback::<String>(|_step_ctx, _callback_id| async { Ok(()) })
            .name("manager-approval")
            .await?;
        let shipment = ctx
            .step(move |_| async move { Ok(format!("shipped-{order_id}")) })
            .name("ship-order")
            .await?;
        Ok(serde_json::json!({
            "reservation": reservation,
            "approval": approval,
            "shipment": shipment,
        }))
    }

    /// The output of the pause-resume handler for `order`.
    fn pause_resume_output(order: &str) -> serde_json::Value {
        serde_json::json!({
            "reservation": format!("reserved-{order}"),
            "approval": "approved",
            "shipment": format!("shipped-{order}"),
        })
    }

    /// The state cell of a run with no backend records.
    fn new_live() -> Arc<LiveState> {
        Arc::new(LiveState::new(BackendState::new(QueuedReplies::default())))
    }

    /// Asserts that `run` does not finish within 50 ms.
    async fn assert_run_waits<O: DeserializeOwned + Send + 'static>(run: &mut RunHandle<O>) {
        assert!(
            tokio::time::timeout(std::time::Duration::from_millis(50), run)
                .await
                .is_err(),
            "the run waits"
        );
    }

    /// The handler of the race tests: a callback named `approval` races a
    /// one-hour wait, and the handler returns which won.
    async fn callback_races_one_hour((): (), ctx: DurableContext) -> Result<String, BoxError> {
        let callback = ctx
            .run_in_child_context(|child| async move {
                let cb = child.create_callback::<String>().name("approval").await?;
                cb.result().await?;
                Ok("callback".to_owned())
            })
            .name("callback-branch")
            .future();
        let wait = ctx
            .run_in_child_context(|child| async move {
                child
                    .wait(std::time::Duration::from_secs(3600))
                    .name("one-hour")
                    .await?;
                Ok("wait".to_owned())
            })
            .name("wait-branch")
            .future();
        Ok(ctx.race([callback, wait]).await?)
    }

    /// A `wait_for_callback` named `approve` with a submitter that does
    /// nothing.
    async fn wait_for_approve((): (), ctx: DurableContext) -> Result<u32, BoxError> {
        Ok(ctx
            .wait_for_callback::<u32>(|_step_ctx, _callback_id| async { Ok(()) })
            .name("approve")
            .await?)
    }

    /// Steps named by `names`, in order, each returning its index.
    fn steps_named(
        names: &'static [&'static str],
    ) -> impl Fn((), DurableContext) -> Pin<Box<dyn Future<Output = Result<(), BoxError>> + Send>>
    + Send
    + Sync
    + 'static {
        move |(), ctx| {
            Box::pin(async move {
                for (index, name) in names.iter().enumerate() {
                    ctx.step(move |_| async move { Ok(index) })
                        .name(*name)
                        .await?;
                }
                Ok(())
            })
        }
    }

    #[tokio::test]
    async fn reply_chosen_from_callback_id() {
        let run = LocalRunner::new().run(
            |(), ctx: DurableContext| async move {
                let cb = ctx.create_callback::<String>().name("approval").await?;
                Ok::<_, BoxError>(cb.result().await?)
            },
            (),
        );
        let approval = run.operation("approval");
        let op = approval
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        let id = match op.details() {
            TestOperationDetails::Callback(details) => details.callback_id(),
            _ => None,
        }
        .expect("a callback ID")
        .to_owned();
        approval
            .send_callback(CallbackReply::raw(format!("\"{id}\"")))
            .await
            .expect("sent");
        let result = run.await.expect("runner");
        assert_eq!(result.output(), Some(&id));
    }

    #[tokio::test]
    async fn wait_for_callback_submitted_then_reply() {
        let run = LocalRunner::new().run(wait_for_approve, ());
        let approve = run.operation("approve");
        let op = approve
            .wait_for_data(WaitingStatus::Submitted)
            .await
            .expect("submitted");
        assert_eq!(op.kind(), OperationKind::Context);
        assert_eq!(op.status(), OperationStatus::Started);
        approve.send_callback(success(&5_u32)).await.expect("sent");
        let result = run.await.expect("runner");
        assert_eq!(result.output(), Some(&5));
    }

    #[tokio::test]
    async fn submitted_differs_from_completed() {
        let run = LocalRunner::new().run(wait_for_approve, ());
        let approve = run.operation("approve");
        let submitted = approve
            .wait_for_data(WaitingStatus::Submitted)
            .await
            .expect("submitted");
        approve.send_callback(success(&5_u32)).await.expect("sent");
        let completed = approve
            .wait_for_data(WaitingStatus::Completed)
            .await
            .expect("completed");
        assert_eq!(submitted.status(), OperationStatus::Started);
        assert_eq!(completed.status(), OperationStatus::Succeeded);
        let result = run.await.expect("runner");
        assert_eq!(
            result.operation("approve").map(TestOperation::status),
            Some(OperationStatus::Succeeded)
        );
    }

    #[tokio::test]
    async fn reply_after_wait_for_data_beats_a_one_hour_wait() {
        let run = LocalRunner::new().run(callback_races_one_hour, ());
        let approval = run.operation("approval");
        approval
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        approval
            .send_callback(success(&"yes".to_owned()))
            .await
            .expect("sent");
        let result = run.await.expect("runner");
        assert_eq!(result.output().map(String::as_str), Some("callback"));
    }

    #[tokio::test]
    async fn wait_wins_without_a_reply() {
        let result = LocalRunner::new()
            .run(callback_races_one_hour, ())
            .await
            .expect("runner");
        assert_eq!(result.output().map(String::as_str), Some("wait"));
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn checkpoint_held_while_no_runner_future_waits() {
        let run = LocalRunner::new().run(steps_named(&["a", "b"]), ());
        let a = run.operation("a");
        let b = run.operation("b");
        a.wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        assert_eq!(
            a.snapshot().map(|op| op.status()),
            Some(OperationStatus::Started)
        );
        assert!(b.snapshot().is_none());
        let result = run.await.expect("runner");
        assert_eq!(result.status(), ExecutionStatus::Succeeded);
    }

    #[tokio::test]
    async fn pause_mid_step_keeps_the_checkpoint() {
        let mut run = LocalRunner::new().run(
            pause_resume_handler,
            serde_json::json!({"orderId": "order-7"}),
        );
        let reserve = run.operation("reserve-stock");
        let approval = run.operation("manager-approval");
        reserve
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        run.pause().await.expect("paused");
        assert_eq!(
            reserve.snapshot().map(|op| op.status()),
            Some(OperationStatus::Succeeded)
        );
        assert!(approval.snapshot().is_none());
        assert_run_waits(&mut run).await;
        run.resume().await.expect("resumed");
        approval
            .wait_for_data(WaitingStatus::Submitted)
            .await
            .expect("submitted");
        approval
            .send_callback(success(&"approved".to_owned()))
            .await
            .expect("sent");
        let result = run.await.expect("runner");
        assert_eq!(result.output(), Some(&pause_resume_output("order-7")));
    }

    #[tokio::test]
    async fn pause_while_idle_holds_the_next_invocation() {
        let mut run = LocalRunner::new().run(
            pause_resume_handler,
            serde_json::json!({"orderId": "order-8"}),
        );
        let approval = run.operation("manager-approval");
        approval
            .wait_for_data(WaitingStatus::Submitted)
            .await
            .expect("submitted");
        run.pause().await.expect("paused");
        approval
            .send_callback(success(&"approved".to_owned()))
            .await
            .expect("sent");
        assert_run_waits(&mut run).await;
        assert!(run.operation("ship-order").snapshot().is_none());
        run.resume().await.expect("resumed");
        let result = run.await.expect("runner");
        assert_eq!(result.output(), Some(&pause_resume_output("order-8")));
    }

    /// `handler` wrapped so that each invocation increments `counter` on
    /// entry.
    fn counting<E: 'static, O, Fut>(
        counter: &Arc<AtomicU32>,
        handler: fn(E, DurableContext) -> Fut,
    ) -> impl Fn(E, DurableContext) -> Fut + Send + Sync + 'static
    where
        Fut: Future<Output = Result<O, BoxError>> + 'static,
    {
        let counter = Arc::clone(counter);
        move |event, ctx| {
            counter.fetch_add(1, Ordering::SeqCst);
            handler(event, ctx)
        }
    }

    async fn cool_down((): (), ctx: DurableContext) -> Result<String, BoxError> {
        ctx.wait(std::time::Duration::from_secs(3600))
            .name("cool-down")
            .await?;
        Ok("done".to_owned())
    }

    #[tokio::test]
    async fn pause_holds_invocation_after_wait_completes() {
        let counter = Arc::new(AtomicU32::new(0));
        let mut run = LocalRunner::new().run(counting(&counter, cool_down), ());
        let wait = run.operation("cool-down");
        wait.wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        run.pause().await.expect("paused");
        assert_eq!(counter.load(Ordering::SeqCst), 1);
        wait.wait_for_data(WaitingStatus::Completed)
            .await
            .expect("completed while paused");
        assert_run_waits(&mut run).await;
        assert_eq!(counter.load(Ordering::SeqCst), 1);
        let op = wait.snapshot().expect("the wait");
        assert_eq!(op.status(), OperationStatus::Succeeded);
        assert_eq!(
            op.end_time(),
            op.start_time()
                .map(|t| t + std::time::Duration::from_secs(3600))
        );
        run.resume().await.expect("resumed");
        let result = run.await.expect("runner");
        assert_eq!(result.output().map(String::as_str), Some("done"));
        assert_eq!(result.status(), ExecutionStatus::Succeeded);
        assert_eq!(counter.load(Ordering::SeqCst), 2);
        assert_eq!(result.invocations().len(), 2);
    }

    async fn two_waits((): (), ctx: DurableContext) -> Result<String, BoxError> {
        let branches: Vec<crate::Branch<()>> = vec![
            crate::Branch::new("one-hour-branch", |child| async move {
                child
                    .wait(std::time::Duration::from_secs(3600))
                    .name("one hour")
                    .await?;
                Ok(())
            }),
            crate::Branch::new("two-hours-branch", |child| async move {
                child
                    .wait(std::time::Duration::from_secs(7200))
                    .name("two hours")
                    .await?;
                Ok(())
            }),
        ];
        ctx.parallel(branches).await?;
        Ok("done".to_owned())
    }

    #[tokio::test]
    async fn pause_keeps_completing_waits_in_due_order() {
        let counter = Arc::new(AtomicU32::new(0));
        let mut run = LocalRunner::new().run(counting(&counter, two_waits), ());
        let one = run.operation("one hour");
        let two = run.operation("two hours");
        two.wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        run.pause().await.expect("paused");
        let (one_done, two_done) = tokio::time::timeout(std::time::Duration::from_secs(1), async {
            tokio::join!(
                one.wait_for_data(WaitingStatus::Completed),
                two.wait_for_data(WaitingStatus::Completed)
            )
        })
        .await
        .expect("both waits complete while paused");
        let one_done = one_done.expect("one hour");
        let two_done = two_done.expect("two hours");
        let after = |op: &TestOperation, seconds| {
            op.start_time()
                .map(|t| t + std::time::Duration::from_secs(seconds))
        };
        assert_eq!(one_done.status(), OperationStatus::Succeeded);
        assert_eq!(one_done.end_time(), after(&one_done, 3600));
        assert_eq!(two_done.status(), OperationStatus::Succeeded);
        assert_eq!(two_done.end_time(), after(&two_done, 7200));
        assert!(one_done.end_time() < two_done.end_time());
        assert_run_waits(&mut run).await;
        assert_eq!(counter.load(Ordering::SeqCst), 1);
        run.resume().await.expect("resumed");
        let result = run.await.expect("runner");
        assert_eq!(result.output().map(String::as_str), Some("done"));
        assert_eq!(counter.load(Ordering::SeqCst), 2);
    }

    /// A parallel of a step that takes 200 ms and a one-hour wait.
    async fn slow_step_beside_a_long_wait((): (), ctx: DurableContext) -> Result<String, BoxError> {
        let branches: Vec<crate::Branch<()>> = vec![
            crate::Branch::new("slow-branch", |child| async move {
                child
                    .step(|_| async {
                        tokio::time::sleep(std::time::Duration::from_millis(200)).await;
                        Ok(1_u32)
                    })
                    .name("slow")
                    .await?;
                Ok(())
            }),
            crate::Branch::new("wait-branch", |child| async move {
                child
                    .wait(std::time::Duration::from_secs(3600))
                    .name("long")
                    .await?;
                Ok(())
            }),
        ];
        ctx.parallel(branches).await?;
        Ok("done".to_owned())
    }

    /// A pause that waits for the invocation to end stops counting as a
    /// waiting runner future when the invocation ends, so the paused run
    /// advances nothing until another runner future waits.
    #[tokio::test]
    async fn pause_mid_step_does_not_advance_a_started_wait() {
        let counter = Arc::new(AtomicU32::new(0));
        let mut run = LocalRunner::new().run(counting(&counter, slow_step_beside_a_long_wait), ());
        let slow = run.operation("slow");
        let long = run.operation("long");
        let (slow_started, long_started) = tokio::join!(
            slow.wait_for_data(WaitingStatus::Started),
            long.wait_for_data(WaitingStatus::Started)
        );
        slow_started.expect("the step started");
        long_started.expect("the wait started");
        run.pause().await.expect("paused");
        assert_eq!(run.shared.live.lock().local.phase, LocalPhase::Idle);
        assert_eq!(
            long.snapshot().map(|op| op.status()),
            Some(OperationStatus::Started),
            "the wait did not advance before the pause returned"
        );
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;
        assert_eq!(
            long.snapshot().map(|op| op.status()),
            Some(OperationStatus::Started),
            "nothing advances while no runner future waits"
        );
        let done = long
            .wait_for_data(WaitingStatus::Completed)
            .await
            .expect("a waiting runner future lets the wait complete");
        assert_eq!(done.status(), OperationStatus::Succeeded);
        assert_run_waits(&mut run).await;
        assert_eq!(counter.load(Ordering::SeqCst), 1);
        run.resume().await.expect("resumed");
        let result = run.await.expect("runner");
        assert_eq!(result.output().map(String::as_str), Some("done"));
    }

    /// A driver that never finishes and counts its polls.
    fn counting_driver(polls: Arc<AtomicU32>) -> DriverFuture {
        Box::pin(std::future::poll_fn(move |_| {
            polls.fetch_add(1, Ordering::SeqCst);
            std::task::Poll::Pending
        }))
    }

    /// A `LocalRunner` run on the state cell `live` whose driver never
    /// finishes and counts its polls.
    fn counting_run(live: &Arc<LiveState>) -> (Arc<RunCore>, Arc<AtomicU32>) {
        let polls = Arc::new(AtomicU32::new(0));
        let shared = RunCore::new(
            Arc::clone(live),
            Engine::Local,
            counting_driver(Arc::clone(&polls)),
        );
        (shared, polls)
    }

    /// Adds a control of `kind` to `live` and marks it pending, as a polled
    /// runner future that returned `Pending`.
    fn pending_control(live: &LiveState, kind: ControlKind) -> u64 {
        let mut state = live.lock();
        let id = state.add_control(kind);
        assert!(state.register(id, std::task::Waker::noop()).is_some());
        id
    }

    /// A `wait_for_data` control for the operation `id` and `status`.
    fn data_control(id: &str, status: WaitingStatus) -> ControlKind {
        ControlKind::Data {
            selector: Selector::Id(id.to_owned()),
            status,
        }
    }

    /// A backend of a top-level run, with its records in the run state.
    fn live_backend() -> (Arc<Backend>, Arc<LiveState>) {
        let live = new_live();
        let backend = Backend::on(StateCell::Live(Arc::clone(&live)), None);
        (Arc::new(backend), live)
    }

    /// Stores a started wait `id` that ends at `due` seconds.
    fn store_wait(backend: &Backend, id: &str, due: u64) {
        backend.lock().ops.push(StoredOp {
            due_at: Some(due),
            ..new_started_op(id.to_owned(), RunnerKind::Wait, None, None, 0)
        });
    }

    /// Stores a pending callback `id`, with a heartbeat timeout.
    fn store_callback(backend: &Backend, id: &str) {
        backend.lock().ops.push(StoredOp {
            callback_id: Some(format!("cb-{id}")),
            heartbeat_seconds: Some(60),
            ..new_started_op(id.to_owned(), RunnerKind::Callback, None, None, 0)
        });
    }

    fn stored_status(backend: &Backend, id: &str) -> OperationStatus {
        backend
            .lock()
            .ops
            .iter()
            .find(|op| op.id == id)
            .map(|op| op.status)
            .expect("stored")
    }

    /// Pause and admission are transitions on one state, in either order.
    /// A pause first keeps the invocation from starting. An admission
    /// first makes the pause wait for that invocation's end, and the
    /// invocation's end makes the pause ready.
    #[test]
    fn pause_and_admission_are_ordered_transitions() {
        let mut cx = std::task::Context::from_waker(std::task::Waker::noop());

        // Pause first.
        let live = new_live();
        let (shared, _polls) = counting_run(&live);
        let run = RunHandle::<u32>::from_shared(Arc::clone(&shared));
        pending_control(&live, ControlKind::Run);
        let mut pause = std::pin::pin!(run.pause());
        assert!(matches!(
            pause.as_mut().poll(&mut cx),
            std::task::Poll::Ready(Ok(()))
        ));
        assert!(!live.lock().admit_invocation(), "a paused run admits none");
        assert_eq!(live.lock().local.phase, LocalPhase::Idle);

        // Admission first.
        let live = new_live();
        let (shared, _polls) = counting_run(&live);
        let run = RunHandle::<u32>::from_shared(Arc::clone(&shared));
        pending_control(&live, ControlKind::Run);
        assert!(live.lock().admit_invocation());
        assert!(!live.lock().admit_invocation(), "one invocation at a time");
        let mut pause = std::pin::pin!(run.pause());
        assert!(pause.as_mut().poll(&mut cx).is_pending());
        assert_eq!(
            live.lock().local.phase,
            LocalPhase::Invoking {
                tokenless_requested: true
            },
            "the pause asks for a tokenless answer in its own transition"
        );
        assert!(live.lock().take_tokenless());
        assert!(!live.lock().take_tokenless(), "taken once");
        wake(live.lock().end_invocation(false));
        assert!(matches!(
            pause.as_mut().poll(&mut cx),
            std::task::Poll::Ready(Ok(()))
        ));
        assert!(!live.lock().admit_invocation(), "still paused");
    }

    /// An invocation's end makes every pending pause ready in the same
    /// transition, so none of them lets the run move when the driver decides
    /// what to advance next.
    #[test]
    fn invocation_end_makes_every_pause_ready_before_the_decision() {
        let (backend, live) = live_backend();
        store_wait(&backend, "w", 3600);
        let first = pending_control(&live, ControlKind::Pause);
        let second = pending_control(&live, ControlKind::Pause);
        assert!(live.lock().admit_invocation());
        backend.finish_invocation(false, 0, "r", None);
        {
            let state = live.lock();
            assert!(state.pause_done(first) && state.pause_done(second));
            assert_eq!(
                state
                    .local
                    .records
                    .events
                    .last()
                    .and_then(Event::event_type),
                Some(&EventType::InvocationCompleted),
                "the end event is recorded in the same transition"
            );
        }
        assert!(!live.lock().hold_open());
        let mut start = false;
        assert_eq!(
            between_decision(&mut live.lock(), &mut start, true).0,
            Decided::Wait
        );
        assert_eq!(backend.now(), 0, "no control waits: the clock holds");
    }

    /// A control that is ready, or no longer exists, is not registered and
    /// does not poll the driver. A control that is not ready is registered
    /// and polls it. After a run failure no control registers, so none
    /// polls the driver.
    #[test]
    fn a_ready_control_neither_registers_nor_polls() {
        let (backend, live) = live_backend();
        let (shared, polls) = counting_run(&live);
        backend.lock().ops.push(new_started_op(
            "x".to_owned(),
            RunnerKind::Step,
            None,
            None,
            0,
        ));
        let ready = live
            .lock()
            .add_control(data_control("x", WaitingStatus::Started));
        let waiting = live
            .lock()
            .add_control(data_control("ghost", WaitingStatus::Started));
        assert!(live.lock().recheck().is_empty(), "nothing was pending yet");
        shared.drive(ready, std::task::Waker::noop());
        assert!(!live.lock().hold_open());
        assert_eq!(polls.load(Ordering::SeqCst), 0);

        let pause = live.lock().add_control(ControlKind::Pause);
        drop(live.lock().end_invocation(false));
        shared.drive(pause, std::task::Waker::noop());
        assert!(!live.lock().hold_open(), "a ready pause does not wait");
        assert_eq!(polls.load(Ordering::SeqCst), 0);

        shared.leave(waiting);
        shared.drive(waiting, std::task::Waker::noop());
        assert_eq!(polls.load(Ordering::SeqCst), 0, "a dropped control");

        let other = live
            .lock()
            .add_control(data_control("ghost", WaitingStatus::Started));
        shared.drive(other, std::task::Waker::noop());
        assert!(live.lock().hold_open());
        assert_eq!(polls.load(Ordering::SeqCst), 1);

        live.lock().failure = Some(TestRunnerError::NoExecutionArn);
        let late = live.lock().add_control(ControlKind::Run);
        shared.drive(late, std::task::Waker::noop());
        assert_eq!(polls.load(Ordering::SeqCst), 1);
    }

    /// Dropping a pending control of each `LocalRunner` kind closes the hold
    /// it opened, so it no longer lets the run move. A pending `CloudRunner`
    /// control never opens the hold.
    #[test]
    fn a_dropped_control_no_longer_lets_the_run_move() {
        for kind in [
            ControlKind::Run,
            data_control("x", WaitingStatus::Started),
            ControlKind::Pause,
        ] {
            let live = new_live();
            let (shared, _polls) = counting_run(&live);
            let id = pending_control(&live, kind);
            assert!(live.lock().hold_open());
            shared.leave(id);
            assert!(!live.lock().hold_open());
            assert!(!live.lock().admit_invocation());
        }
        let live = new_live();
        pending_control(&live, ControlKind::Cloud);
        assert!(!live.lock().hold_open());
    }

    /// The hold check and the clock advance are one transition: without a
    /// pending control the clock does not move, with one it moves in the
    /// same step.
    #[test]
    fn the_clock_advances_only_in_the_step_that_sees_a_waiter() {
        let (backend, live) = live_backend();
        store_wait(&backend, "w", 3600);
        let mut start = false;
        let decide = |start: &mut bool| between_decision(&mut live.lock(), start, true).0;
        assert_eq!(decide(&mut start), Decided::Wait);
        assert_eq!(backend.now(), 0, "no waiter: the clock holds");
        assert_eq!(stored_status(&backend, "w"), OperationStatus::Started);
        let run = pending_control(&live, ControlKind::Run);
        assert_eq!(decide(&mut start), Decided::Step(BetweenStep::Start));
        assert_eq!(backend.now(), 3600);
        assert_eq!(stored_status(&backend, "w"), OperationStatus::Succeeded);
        // Paused: an advance decides again at once, and with nothing to
        // advance the driver waits for `resume`. Without a waiter, nothing
        // advances.
        store_wait(&backend, "w2", 7200);
        live.lock().local.paused = true;
        live.lock().controls.remove(&run);
        assert_eq!(decide(&mut start), Decided::Wait);
        assert_eq!(backend.now(), 3600);
        pending_control(&live, ControlKind::Run);
        assert_eq!(decide(&mut start), Decided::Again);
        assert!(start, "the advance starts the held invocation");
        assert_eq!(backend.now(), 7200);
        assert_eq!(decide(&mut start), Decided::Wait, "wait for resume");
        live.lock().local.paused = false;
        assert_eq!(decide(&mut start), Decided::Step(BetweenStep::Start));
        // Nothing can advance: the first decision asks for the pending
        // invokes, the next one ends the run and closes it.
        let mut start = false;
        assert_eq!(
            between_decision(&mut live.lock(), &mut start, false).0,
            Decided::Step(BetweenStep::RunInvokes)
        );
        assert_eq!(live.lock().local.phase, LocalPhase::Idle);
        assert_eq!(decide(&mut start), Decided::Step(BetweenStep::Stuck));
        assert_eq!(live.lock().local.phase, LocalPhase::Closed);
    }

    /// A paused advance that makes the only pending `wait_for_data` ready
    /// closes the hold in the same transition: the second due wait and the
    /// clock stay until another control waits.
    #[test]
    fn a_waiter_made_ready_by_an_advance_stops_the_next_advance() {
        let (backend, live) = live_backend();
        store_wait(&backend, "one", 3600);
        store_wait(&backend, "two", 7200);
        live.lock().local.paused = true;
        pending_control(&live, data_control("one", WaitingStatus::Completed));
        let mut start = false;
        let (decided, wakers) = between_decision(&mut live.lock(), &mut start, true);
        assert_eq!(decided, Decided::Again);
        assert_eq!(wakers.len(), 1, "the waiter is woken with its snapshot");
        assert!(!live.lock().hold_open());
        assert_eq!(
            between_decision(&mut live.lock(), &mut start, true).0,
            Decided::Wait
        );
        assert_eq!(backend.now(), 3600);
        assert_eq!(stored_status(&backend, "two"), OperationStatus::Started);
        pending_control(&live, ControlKind::Run);
        assert_eq!(
            between_decision(&mut live.lock(), &mut start, true).0,
            Decided::Again
        );
        assert_eq!(backend.now(), 7200);
    }

    /// A live reply is one transition with its record change. A reply
    /// before the driver's decision starts the next invocation without
    /// moving the clock; a reply after the driver closed the run is refused
    /// and changes nothing.
    #[test]
    fn a_live_reply_starts_the_next_invocation_or_is_refused() {
        let reply_c = |live: &LiveState| {
            reply_live(
                &mut live.lock(),
                &Selector::Id("c".to_owned()),
                CallbackReply::raw("1"),
            )
        };
        let (backend, live) = live_backend();
        store_callback(&backend, "c");
        store_wait(&backend, "w", 3600);
        pending_control(&live, ControlKind::Run);
        reply_c(&live).expect("pending");
        let mut start = false;
        assert_eq!(
            between_decision(&mut live.lock(), &mut start, true).0,
            Decided::Step(BetweenStep::Start)
        );
        assert_eq!(backend.now(), 0, "the reply starts it, not the clock");
        assert!(
            !live.lock().local.invocation_due,
            "the decision took the reply"
        );

        let (backend, live) = live_backend();
        store_callback(&backend, "c");
        pending_control(&live, ControlKind::Run);
        let mut start = false;
        assert_eq!(
            between_decision(&mut live.lock(), &mut start, true).0,
            Decided::Step(BetweenStep::Stuck)
        );
        let err = reply_c(&live).expect_err("the run ends");
        assert!(
            matches!(err, TestRunnerError::CallbackNotPending),
            "{err:?}"
        );
        assert_eq!(stored_status(&backend, "c"), OperationStatus::Started);
        assert_eq!(backend.lock().updated_ids, Vec::<String>::new());
        assert!(!live.lock().local.invocation_due);
    }

    /// A terminal envelope and the invocation budget close the run too, and
    /// a closed run refuses a heartbeat to a callback that is still
    /// `Started`, as it refuses a reply.
    #[test]
    fn the_run_closes_at_its_end() {
        let heartbeat_c =
            |live: &LiveState| check_heartbeat(&mut live.lock(), &Selector::Id("c".to_owned()));
        let (backend, live) = live_backend();
        store_callback(&backend, "c");
        heartbeat_c(&live).expect("an open run");
        backend.finish_invocation(false, 0, "r", None);
        assert_eq!(
            live.lock().local.phase,
            LocalPhase::Idle,
            "a PENDING invocation keeps it open"
        );
        backend.finish_invocation(true, 0, "r", None);
        assert_eq!(live.lock().local.phase, LocalPhase::Closed);
        assert!(matches!(
            heartbeat_c(&live),
            Err(TestRunnerError::CallbackNotPending)
        ));
        backend.finish_invocation(false, 0, "r", None);
        assert_eq!(live.lock().local.phase, LocalPhase::Closed, "closed stays");

        let (backend, live) = live_backend();
        store_callback(&backend, "c");
        backend.close();
        assert_eq!(live.lock().local.phase, LocalPhase::Closed);
        assert!(matches!(
            heartbeat_c(&live),
            Err(TestRunnerError::CallbackNotPending)
        ));
        assert_eq!(stored_status(&backend, "c"), OperationStatus::Started);
    }

    /// A commit decides against the failure recorded before it, and only
    /// that one: an outcome committed first stays, a later commit returns
    /// the first failure, and a second failure does not replace it.
    #[test]
    fn a_commit_orders_an_outcome_against_the_first_failure() {
        let live = new_live();
        assert_eq!(live.lock().commit(Ok(1_u32)).expect("no failure"), 1);
        let (winner, _) = live.lock().record_failure(TestRunnerError::NoExecutionArn);
        assert!(matches!(winner, TestRunnerError::NoExecutionArn));
        let (winner, _) = live
            .lock()
            .record_failure(TestRunnerError::OperationNotFound);
        assert!(matches!(winner, TestRunnerError::NoExecutionArn));
        assert!(matches!(
            live.lock().commit(Ok(2_u32)),
            Err(TestRunnerError::NoExecutionArn)
        ));
    }

    /// A handler that creates the callback `c`, then waits an hour, and never
    /// awaits the callback's result.
    async fn callback_then_wait((): (), ctx: DurableContext) -> Result<u32, BoxError> {
        ctx.create_callback::<u32>().name("c").await?;
        ctx.wait(std::time::Duration::from_secs(3600))
            .name("w")
            .await?;
        Ok(1)
    }

    /// A reply to a callback that is still `Started` after the run decided to
    /// end is refused: after a terminal envelope, and after the invocation
    /// budget ran out.
    #[tokio::test]
    async fn a_reply_after_the_run_ended_is_refused() {
        for runner in [LocalRunner::new(), LocalRunner::new().max_invocations(1)] {
            let mut run = runner.run(callback_then_wait, ());
            let c = run.operation("c");
            let result = (&mut run).await.expect("runner");
            assert_eq!(
                c.snapshot().map(|op| op.status()),
                Some(OperationStatus::Started)
            );
            let err = c
                .send_callback(CallbackReply::success(&1_u32).expect("encodes"))
                .await
                .expect_err("the run ended");
            assert!(
                matches!(err, TestRunnerError::CallbackNotPending),
                "{:?}: {err:?}",
                result.status()
            );
        }
    }

    /// A handler that waits for the callback `c` and returns its result.
    async fn await_callback_c((): (), ctx: DurableContext) -> Result<u32, BoxError> {
        let cb = ctx.create_callback::<u32>().name("c").await?;
        Ok(cb.result().await?)
    }

    #[tokio::test]
    async fn pause_and_resume_are_idempotent_but_reject_after_run() {
        let mut run = LocalRunner::new().run(steps_named(&["s"]), ());
        run.pause().await.expect("pause");
        run.pause().await.expect("pause again");
        run.resume().await.expect("resume");
        run.resume().await.expect("resume again");
        let result = (&mut run).await.expect("runner");
        assert_eq!(result.status(), ExecutionStatus::Succeeded);
        let pause = run.pause().await.expect_err("after the run");
        let resume = run.resume().await.expect_err("after the run");
        assert!(
            matches!(pause, TestRunnerError::RunFinished("pause")),
            "{pause:?}"
        );
        assert!(
            matches!(resume, TestRunnerError::RunFinished("resume")),
            "{resume:?}"
        );
        assert_eq!(
            pause.to_string(),
            "durable: test runner: pause needs a run in progress; call it before awaiting the \
             RunHandle"
        );
        assert_eq!(
            resume.to_string(),
            "durable: test runner: resume needs a run in progress; call it before awaiting the \
             RunHandle"
        );
    }

    #[tokio::test]
    async fn send_callback_errors() {
        let run = LocalRunner::new().run(
            |(), ctx: DurableContext| async move {
                ctx.step(|_| async { Ok(1_u32) }).name("s").await?;
                let cb = ctx.create_callback::<u32>().name("c").await?;
                Ok::<_, BoxError>(cb.result().await?)
            },
            (),
        );
        let c = run.operation("c");
        c.wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        let step = run
            .operation("s")
            .send_callback(success(&1_u32))
            .await
            .expect_err("a step");
        assert!(matches!(step, TestRunnerError::NotACallback), "{step:?}");
        c.send_callback(success(&1_u32)).await.expect("sent");
        let again = c
            .send_callback(success(&1_u32))
            .await
            .expect_err("answered");
        assert!(
            matches!(again, TestRunnerError::CallbackNotPending),
            "{again:?}"
        );
        let ghost = run
            .operation("ghost")
            .send_callback(success(&1_u32))
            .await
            .expect_err("no operation");
        assert!(
            matches!(ghost, TestRunnerError::OperationNotFound),
            "{ghost:?}"
        );
        run.await.expect("runner");
    }

    #[tokio::test]
    async fn heartbeat_rules() {
        let run = LocalRunner::new().run(
            |(), ctx: DurableContext| async move {
                let h = ctx
                    .create_callback::<u32>()
                    .name("h")
                    .heartbeat_timeout(std::time::Duration::from_secs(60))
                    .await?;
                let n = ctx.create_callback::<u32>().name("n").await?;
                Ok::<_, BoxError>(h.result().await? + n.result().await?)
            },
            (),
        );
        let h = run.operation("h");
        let n = run.operation("n");
        n.wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        h.send_callback_heartbeat()
            .await
            .expect("h has a heartbeat");
        assert_eq!(
            h.snapshot().map(|op| op.status()),
            Some(OperationStatus::Started)
        );
        let err = n.send_callback_heartbeat().await.expect_err("no heartbeat");
        assert!(
            matches!(err, TestRunnerError::NoHeartbeatTimeout),
            "{err:?}"
        );
        h.send_callback(success(&1_u32)).await.expect("sent");
        n.send_callback(success(&2_u32)).await.expect("sent");
        let result = run.await.expect("runner");
        assert_eq!(result.output(), Some(&3));
    }

    #[tokio::test]
    async fn heartbeat_to_an_answered_callback_is_not_pending() {
        let run = LocalRunner::new().run(
            |(), ctx: DurableContext| async move {
                let h = ctx
                    .create_callback::<u32>()
                    .name("h")
                    .heartbeat_timeout(std::time::Duration::from_secs(60))
                    .await?;
                Ok::<_, BoxError>(h.result().await?)
            },
            (),
        );
        let h = run.operation("h");
        h.wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        h.send_callback(success(&1_u32)).await.expect("sent");
        let err = h.send_callback_heartbeat().await.expect_err("answered");
        assert!(
            matches!(err, TestRunnerError::CallbackNotPending),
            "{err:?}"
        );
        run.await.expect("runner");
    }

    #[tokio::test]
    async fn wait_for_data_not_reached() {
        let run = LocalRunner::new().run(steps_named(&["s"]), ());
        let ghost = run.operation("ghost");
        let (result, waited) = tokio::join!(run, ghost.wait_for_data(WaitingStatus::Started));
        result.expect("runner");
        let err = waited.expect_err("never reached");
        assert!(
            matches!(
                err,
                TestRunnerError::OperationNotReached(WaitingStatus::Started)
            ),
            "{err:?}"
        );
    }

    #[tokio::test]
    async fn late_unmatched_waiter_returns_not_reached() {
        let mut run = LocalRunner::new().run(steps_named(&["s"]), ());
        (&mut run).await.expect("runner");
        let err = run
            .operation("ghost")
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect_err("never reached");
        assert!(
            matches!(
                err,
                TestRunnerError::OperationNotReached(WaitingStatus::Started)
            ),
            "{err:?}"
        );
    }

    #[tokio::test]
    async fn wait_for_callback_started_by_callback_child() {
        let run = LocalRunner::new().run(wait_for_approve, ());
        let context = run
            .operation("approve")
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        assert_eq!(context.kind(), OperationKind::Context);
        assert_eq!(context.status(), OperationStatus::Started);
        let callback = run
            .operation_by_index(1)
            .snapshot()
            .expect("the callback child");
        assert_eq!(callback.kind(), OperationKind::Callback);
        assert_eq!(callback.parent_id(), Some(context.id()));
        run.operation("approve")
            .send_callback(success(&1_u32))
            .await
            .expect("sent");
        run.await.expect("runner");
    }

    #[test]
    fn wait_for_callback_without_callback_child_is_not_pending() {
        let context = TestOperation {
            sub_type: Some(crate::callback::WFCB_SUB_TYPE.to_owned()),
            name: Some("approve".to_owned()),
            ..TestOperation::started("ctx".to_owned(), RunnerKind::Context)
        };
        let plain = TestOperation {
            name: Some("plain".to_owned()),
            ..TestOperation::started("p".to_owned(), RunnerKind::Context)
        };
        let ops = [context, plain];
        let err = callback_target(&Selector::Name("approve".to_owned(), 0), &ops)
            .expect_err("no callback child");
        assert!(
            matches!(err, TestRunnerError::CallbackNotPending),
            "{err:?}"
        );
        let err = callback_target(&Selector::Name("plain".to_owned(), 0), &ops)
            .expect_err("a plain context");
        assert!(matches!(err, TestRunnerError::NotACallback), "{err:?}");
    }

    #[tokio::test]
    async fn send_callback_records_updated_id_and_history_event() {
        let run = LocalRunner::new().run(
            |(), ctx: DurableContext| async move {
                let cb = ctx.create_callback::<u32>().name("c").await?;
                Ok::<_, BoxError>(cb.result().await?)
            },
            (),
        );
        let c = run.operation("c");
        let op = c
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        c.send_callback(success(&1_u32)).await.expect("sent");
        let updated = run.shared.live.lock().local.records.updated_ids.clone();
        assert!(updated.contains(&op.id().to_owned()), "{updated:?}");
        let result = run.await.expect("runner");
        assert_eq!(result.output(), Some(&1));
        let events = result.history_events();
        let position = |event_type: EventType| {
            events
                .iter()
                .position(|e| e.event_type() == Some(&event_type) && e.id() == Some(op.id()))
                .expect("the event")
        };
        let started = position(EventType::CallbackStarted);
        let succeeded = position(EventType::CallbackSucceeded);
        assert!(started < succeeded);
        assert!(
            events
                .iter()
                .skip(succeeded)
                .any(|e| e.event_type() == Some(&EventType::InvocationCompleted)),
            "an invocation completes after the reply"
        );
    }

    /// Sets a flag when dropped.
    struct DropFlag(Arc<std::sync::atomic::AtomicBool>);

    impl Drop for DropFlag {
        fn drop(&mut self) {
            self.0.store(true, Ordering::SeqCst);
        }
    }

    #[test]
    fn dropping_last_handle_drops_driver() {
        let dropped = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let flag = DropFlag(Arc::clone(&dropped));
        let driver: DriverFuture = Box::pin(async move {
            let _flag = flag;
            std::future::pending::<()>().await;
            Err(TestRunnerError::NoExecutionArn)
        });
        let run = RunHandle::<u32>::from_shared(RunCore::new(new_live(), Engine::Local, driver));
        let op = run.operation("x");
        drop(run);
        assert!(!dropped.load(Ordering::SeqCst));
        drop(op);
        assert!(dropped.load(Ordering::SeqCst));
    }

    #[tokio::test]
    async fn wait_for_data_after_run_returns_final_state() {
        let mut run = LocalRunner::new().run(steps_named(&["s"]), ());
        let s = run.operation("s");
        (&mut run).await.expect("runner");
        let op = s
            .wait_for_data(WaitingStatus::Completed)
            .await
            .expect("completed");
        assert_eq!(op.status(), OperationStatus::Succeeded);
    }

    #[tokio::test]
    async fn operation_handle_outlives_run_handle() {
        let run = LocalRunner::new().run(steps_named(&["a", "b"]), ());
        let b = run.operation("b");
        drop(run);
        let op = b
            .wait_for_data(WaitingStatus::Completed)
            .await
            .expect("completed");
        assert_eq!(op.status(), OperationStatus::Succeeded);
    }

    #[test]
    fn handle_auto_traits() {
        fn assert_send_unpin<T: Send + Unpin>() {}
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_unpin::<RunHandle<u32>>();
        assert_send_sync::<OperationHandle>();
    }

    #[test]
    fn runner_error_texts() {
        let cases = [
            (
                TestRunnerError::Unsupported("pause"),
                "pause is not supported by this runner",
            ),
            (
                TestRunnerError::OperationNotFound,
                "no operation matches this handle",
            ),
            (
                TestRunnerError::OperationNotReached(WaitingStatus::Submitted),
                "the operation did not reach Submitted before the execution ended",
            ),
            (
                TestRunnerError::NotACallback,
                "the operation is not a callback or a wait_for_callback context",
            ),
            (
                TestRunnerError::CallbackNotPending,
                "the callback is not waiting for a reply",
            ),
            (
                TestRunnerError::NoHeartbeatTimeout,
                "the callback has no heartbeat timeout",
            ),
            (
                TestRunnerError::RunFinished("pause"),
                "pause needs a run in progress; call it before awaiting the RunHandle",
            ),
        ];
        for (err, text) in cases {
            assert_eq!(err.to_string(), format!("durable: test runner: {text}"));
            assert!(std::error::Error::source(&err).is_none(), "{err:?}");
        }
    }

    #[tokio::test]
    async fn operation_selectors() {
        let mut run = LocalRunner::new().run(steps_named(&["x", "y", "x"]), ());
        let first = run.operation("x");
        let second = run.operation_by_index(1);
        let third = run.operation_by_name_and_index("x", 1);
        let result = (&mut run).await.expect("runner");
        let ids: Vec<&str> = result.operations().iter().map(TestOperation::id).collect();
        assert_eq!(ids.len(), 3, "{ids:?}");
        let (first_id, second_id, third_id) = (
            ids.first().expect("first"),
            ids.get(1).expect("second"),
            ids.get(2).expect("third"),
        );
        let id_of = |handle: &OperationHandle| handle.snapshot().map(|op| op.id().to_owned());
        assert_eq!(id_of(&first).as_deref(), Some(*first_id));
        assert_eq!(id_of(&second).as_deref(), Some(*second_id));
        assert_eq!(
            second
                .snapshot()
                .and_then(|op| op.name().map(ToOwned::to_owned)),
            Some("y".to_owned())
        );
        assert_eq!(id_of(&third).as_deref(), Some(*third_id));
        assert_eq!(
            id_of(&run.operation_by_id(third_id)).as_deref(),
            Some(*third_id)
        );
        assert!(run.operation_by_id("ghost").snapshot().is_none());
        assert!(run.operation_by_index(3).snapshot().is_none());
    }

    #[tokio::test]
    async fn run_error_reaches_every_runner_future() {
        let run = LocalRunner::new().run(
            |_event: Unserializable, _ctx: DurableContext| async move { Ok::<_, BoxError>(()) },
            Unserializable,
        );
        let a = run.operation("a");
        let b = run.operation("b");
        let (paused, a_done, b_done) = tokio::join!(
            run.pause(),
            a.wait_for_data(WaitingStatus::Started),
            b.wait_for_data(WaitingStatus::Completed)
        );
        let resumed = run.resume().await;
        let sent = a.send_callback(success(&1_u32)).await;
        let beat = a.send_callback_heartbeat().await;
        let result = run.await;
        let texts = [
            result.expect_err("run").to_string(),
            paused.expect_err("pause").to_string(),
            a_done.expect_err("a").to_string(),
            b_done.expect_err("b").to_string(),
            resumed.expect_err("resume").to_string(),
            sent.expect_err("send").to_string(),
            beat.expect_err("heartbeat").to_string(),
        ];
        for text in &texts {
            assert!(
                text.starts_with("durable: test runner: event serialization failed"),
                "{text}"
            );
            assert_eq!(text, &texts[0]);
        }
    }

    #[tokio::test]
    async fn plain_run_await_returns_test_result() {
        let result = LocalRunner::new()
            .run(
                |_event: serde_json::Value, ctx: DurableContext| async move {
                    let name = ctx
                        .step(|_| async { Ok("world".to_owned()) })
                        .name("fetch-name")
                        .await?;
                    ctx.wait(std::time::Duration::from_secs(2))
                        .name("cooldown")
                        .await?;
                    let greeting = ctx
                        .step(move |_| async move { Ok(format!("hello, {name}")) })
                        .name("format")
                        .await?;
                    Ok::<_, BoxError>(greeting)
                },
                serde_json::json!({}),
            )
            .await
            .expect("runner");
        assert_eq!(result.status(), ExecutionStatus::Succeeded);
        assert_eq!(result.output().map(String::as_str), Some("hello, world"));
        let ops: Vec<(Option<&str>, OperationKind, OperationStatus)> = result
            .operations()
            .iter()
            .map(|op| (op.name(), op.kind(), op.status()))
            .collect();
        assert_eq!(
            ops,
            [
                (
                    Some("fetch-name"),
                    OperationKind::Step,
                    OperationStatus::Succeeded
                ),
                (
                    Some("cooldown"),
                    OperationKind::Wait,
                    OperationStatus::Succeeded
                ),
                (
                    Some("format"),
                    OperationKind::Step,
                    OperationStatus::Succeeded
                ),
            ]
        );
    }

    #[tokio::test]
    async fn run_handle_polled_after_completion_stays_pending() {
        let mut run = LocalRunner::new().run(steps_named(&["s"]), ());
        (&mut run).await.expect("runner");
        let poll = std::future::poll_fn(|cx| {
            std::task::Poll::Ready(Pin::new(&mut run).poll(cx).is_pending())
        })
        .await;
        assert!(poll, "the result was already taken");
    }

    /// A `LocalRunner` run that completes the step `s` and then returns an
    /// output that does not decode. A wait pending when the run fails
    /// returns that error. A later wait returns `s` for every status `s`
    /// reached, and the error for an operation that never existed. The
    /// callback, heartbeat, `pause` and `resume` controls keep returning
    /// the error.
    #[tokio::test]
    async fn local_waits_after_run_failure_use_snapshot_first() {
        let mut run = LocalRunner::new().run(
            |(), ctx: DurableContext| async move {
                ctx.step(|_| async { Ok(1_u32) }).name("s").await?;
                Ok::<_, BoxError>(SerializedAs::Text)
            },
            (),
        );
        let s = run.operation("s");
        let ghost = run.operation("ghost");
        let (pending, end) = tokio::join!(ghost.wait_for_data(WaitingStatus::Started), &mut run);
        let err = end.expect_err("the output does not decode");
        assert!(
            matches!(err, TestRunnerError::OutputDeserialization(_)),
            "{err:?}"
        );
        assert_same_error(&pending.expect_err("the pending wait"), &err);
        for status in [
            WaitingStatus::Started,
            WaitingStatus::Submitted,
            WaitingStatus::Completed,
        ] {
            let op = s.wait_for_data(status).await.expect("stored data");
            assert_eq!(op.status(), OperationStatus::Succeeded);
            assert_eq!(op.name(), Some("s"));
        }
        assert_same_error(
            &ghost
                .wait_for_data(WaitingStatus::Started)
                .await
                .expect_err("never reached"),
            &err,
        );
        assert_same_error(
            &s.send_callback(CallbackReply::raw("1"))
                .await
                .expect_err("send_callback"),
            &err,
        );
        assert_same_error(
            &s.send_callback_heartbeat().await.expect_err("heartbeat"),
            &err,
        );
        assert_same_error(&run.pause().await.expect_err("pause"), &err);
        assert_same_error(&run.resume().await.expect_err("resume"), &err);
        assert_eq!(
            s.snapshot().map(|op| op.status()),
            Some(OperationStatus::Succeeded),
            "the failure keeps the records"
        );
    }

    /// An output that serializes as a string and deserializes only from a
    /// number.
    #[derive(Debug)]
    enum SerializedAs {
        Text,
    }

    impl Serialize for SerializedAs {
        fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            serializer.serialize_str("text")
        }
    }

    impl<'de> Deserialize<'de> for SerializedAs {
        fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            u32::deserialize(deserializer).map(|_| Self::Text)
        }
    }

    #[test]
    fn waiting_status_default_is_started() {
        assert_eq!(WaitingStatus::default(), WaitingStatus::Started);
    }

    #[test]
    fn status_matching_follows_js() {
        let op = |id: &str, kind, status| TestOperation {
            status,
            ..TestOperation::started(id.to_owned(), kind)
        };
        let step = op("s", RunnerKind::Step, OperationStatus::Pending);
        assert!(status_reached(&step, WaitingStatus::Started, &[]));
        assert!(!status_reached(&step, WaitingStatus::Submitted, &[]));
        for status in [
            OperationStatus::Succeeded,
            OperationStatus::Failed,
            OperationStatus::Cancelled,
            OperationStatus::TimedOut,
            OperationStatus::Stopped,
        ] {
            let done = op("s", RunnerKind::Step, status);
            assert!(status_reached(&done, WaitingStatus::Submitted, &[]));
            assert!(status_reached(&done, WaitingStatus::Completed, &[]));
        }
        let context = TestOperation {
            sub_type: Some(crate::callback::WFCB_SUB_TYPE.to_owned()),
            ..op("ctx", RunnerKind::Context, OperationStatus::Started)
        };
        let child = |id: &str, kind, status| TestOperation {
            parent_id: Some("ctx".to_owned()),
            ..op(id, kind, status)
        };
        let all = |ops: &[TestOperation]| {
            [
                WaitingStatus::Started,
                WaitingStatus::Submitted,
                WaitingStatus::Completed,
            ]
            .map(|status| status_reached(&context, status, ops))
        };
        // A wait child does not count.
        assert_eq!(
            all(&[child("w", RunnerKind::Wait, OperationStatus::Succeeded)]),
            [false, false, false]
        );
        assert_eq!(
            all(&[child("cb", RunnerKind::Callback, OperationStatus::Started)]),
            [true, false, false]
        );
        assert_eq!(
            all(&[
                child("cb", RunnerKind::Callback, OperationStatus::Started),
                child("sub", RunnerKind::Step, OperationStatus::Succeeded),
            ]),
            [true, true, false]
        );
        // A finished callback whose context has not finished yet is not
        // `Completed`: the context's own end comes after it.
        assert_eq!(
            all(&[child(
                "cb",
                RunnerKind::Callback,
                OperationStatus::Succeeded
            )]),
            [true, true, false]
        );
        let finished = |status| TestOperation {
            status,
            ..context.clone()
        };
        for status in [OperationStatus::Succeeded, OperationStatus::Failed] {
            let done = finished(status);
            let terminal_callback = [child(
                "cb",
                RunnerKind::Callback,
                OperationStatus::Succeeded,
            )];
            assert!(status_reached(
                &done,
                WaitingStatus::Completed,
                &terminal_callback
            ));
            // A finished context whose callback never ended, such as one
            // whose submitter failed, is not `Completed`, as in JS.
            let open_callback = [
                child("cb", RunnerKind::Callback, OperationStatus::Started),
                child("sub", RunnerKind::Step, OperationStatus::Failed),
            ];
            assert!(status_reached(
                &done,
                WaitingStatus::Submitted,
                &open_callback
            ));
            assert!(!status_reached(
                &done,
                WaitingStatus::Completed,
                &open_callback
            ));
        }
        // A plain child context's status is its own.
        let plain = op("p", RunnerKind::Context, OperationStatus::Started);
        assert!(status_reached(&plain, WaitingStatus::Started, &[]));
        assert!(!status_reached(&plain, WaitingStatus::Submitted, &[]));
    }

    // ---- Live control under CloudRunner ----

    /// A history in which the callback `c`, named `approval`, started with
    /// the callback ID `cb-1`.
    fn callback_started_history() -> Vec<Event> {
        vec![
            Event::builder()
                .event_type(EventType::CallbackStarted)
                .id("c")
                .name("approval")
                .event_id(1)
                .callback_started_details(
                    aws_sdk_lambda::types::CallbackStartedDetails::builder()
                        .callback_id("cb-1")
                        .build()
                        .expect("callback details"),
                )
                .build(),
        ]
    }

    /// A fake whose every history read returns `history`.
    fn fake_with_history(history: Vec<Event>) -> Arc<FakeApi> {
        Arc::new(
            FakeApi::new()
                .with_history_pages(Vec::new())
                .repeat_history(Ok(history)),
        )
    }

    /// A cloud runner with `InvocationType::Event`.
    fn event_runner() -> CloudRunner {
        CloudRunner::new("f").invocation_type(InvocationType::Event)
    }

    /// The number of full-history fetches in `calls` (every history is one
    /// page).
    fn history_fetches(calls: &[FakeCall]) -> usize {
        calls
            .iter()
            .filter(|call| **call == FakeCall::History)
            .count()
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_reply_after_wait_for_data() {
        let api = fake_with_history(callback_started_history());
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        approval
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        let before = api.calls();
        assert_eq!(history_fetches(&before), 1, "{before:?}");
        approval
            .send_callback(CallbackReply::raw("1"))
            .await
            .expect("sent");
        let calls = api.calls();
        let after = calls.get(before.len()..).expect("later calls");
        assert_eq!(
            after,
            [
                FakeCall::History,
                FakeCall::CallbackSuccess("cb-1".to_owned(), b"1".to_vec())
            ],
            "{calls:?}"
        );
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_failure_sends_error_object() {
        let api = fake_with_history(callback_started_history());
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        run.operation("approval")
            .send_callback(CallbackReply::failure(
                CallbackFailure::new("T", "m")
                    .with_error_data("d")
                    .with_stack_trace(vec!["f".into()]),
            ))
            .await
            .expect("sent");
        let expected = ErrorObject::builder()
            .error_type("T")
            .error_message("m")
            .error_data("d")
            .stack_trace("f")
            .build();
        assert_eq!(
            api.calls().last(),
            Some(&FakeCall::CallbackFailure("cb-1".to_owned(), expected))
        );
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_timeout_reply_and_pause_are_unsupported() {
        let api = fake_with_history(callback_started_history());
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        for reply in [CallbackReply::timeout(), CallbackReply::heartbeat_timeout()] {
            let err = approval.send_callback(reply).await.expect_err("timeout");
            assert!(
                matches!(
                    err,
                    TestRunnerError::Unsupported(
                        "CallbackReply::timeout and heartbeat_timeout under CloudRunner"
                    )
                ),
                "{err:?}"
            );
        }
        for err in [
            run.pause().await.expect_err("pause"),
            run.resume().await.expect_err("resume"),
        ] {
            assert!(
                matches!(
                    err,
                    TestRunnerError::Unsupported("pause and resume under CloudRunner")
                ),
                "{err:?}"
            );
        }
        assert_eq!(api.calls(), Vec::<FakeCall>::new());
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_heartbeat_calls_service() {
        // Row 1: no poll limit.
        let api = fake_with_history(callback_started_history());
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        approval
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        let before = api.calls().len();
        approval.send_callback_heartbeat().await.expect("heartbeat");
        let calls = api.calls();
        assert_eq!(
            calls.get(before..).expect("later calls"),
            [FakeCall::History, FakeCall::Heartbeat("cb-1".to_owned())],
            "{calls:?}"
        );
        drop(run);

        // Row 2: a budget of two polls.
        let api = fake_with_history(callback_started_history());
        let run = event_runner()
            .max_poll_attempts(2)
            .run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        approval
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        approval.send_callback_heartbeat().await.expect("heartbeat");
        approval
            .send_callback_heartbeat()
            .await
            .expect("heartbeat from the last fold");
        let result = run.await.expect("runner");
        assert_eq!(result.status(), ExecutionStatus::Running);
        let heartbeat = FakeCall::Heartbeat("cb-1".to_owned());
        assert_eq!(
            api.calls(),
            [
                FakeCall::Invoke,
                FakeCall::History,
                FakeCall::History,
                heartbeat.clone(),
                heartbeat,
                FakeCall::History,
            ]
        );
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_wait_for_data_requires_event_invocation() {
        let api = fake_with_history(callback_started_history());
        let run = CloudRunner::new("f").run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        let wait = approval
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect_err("wait_for_data");
        let send = approval
            .send_callback(CallbackReply::raw("1"))
            .await
            .expect_err("send_callback");
        let heartbeat = approval
            .send_callback_heartbeat()
            .await
            .expect_err("heartbeat");
        let unsupported = |err: &TestRunnerError| match err {
            TestRunnerError::Unsupported(text) => Some(*text),
            _ => None,
        };
        assert_eq!(
            unsupported(&wait),
            Some("wait_for_data with InvocationType::RequestResponse (use InvocationType::Event)"),
            "{wait:?}"
        );
        assert_eq!(
            unsupported(&send),
            Some("send_callback with InvocationType::RequestResponse (use InvocationType::Event)"),
            "{send:?}"
        );
        assert_eq!(
            unsupported(&heartbeat),
            Some(
                "send_callback_heartbeat with InvocationType::RequestResponse \
                 (use InvocationType::Event)"
            ),
            "{heartbeat:?}"
        );
        assert_eq!(
            send.to_string(),
            "durable: test runner: send_callback with InvocationType::RequestResponse \
             (use InvocationType::Event) is not supported by this runner"
        );
        assert_eq!(api.calls(), Vec::<FakeCall>::new());
        drop(run);
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_configured_poll_budget_is_shared() {
        let api = fake_with_history(callback_started_history());
        let run = event_runner()
            .max_poll_attempts(4)
            .run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let ghost = run.operation("ghost");
        let (result, waited) = tokio::join!(run, ghost.wait_for_data(WaitingStatus::Started));
        assert_eq!(result.expect("runner").status(), ExecutionStatus::Running);
        let err = waited.expect_err("never reached");
        assert!(
            matches!(
                err,
                TestRunnerError::OperationNotReached(WaitingStatus::Started)
            ),
            "{err:?}"
        );
        let calls = api.calls();
        let polls = calls
            .iter()
            .filter(|call| matches!(call, FakeCall::GetExecution | FakeCall::History))
            .count();
        // Four budgeted polls, plus the history read of the final result.
        assert_eq!(polls, 5, "{calls:?}");
        assert_eq!(calls.last(), Some(&FakeCall::History));
    }

    /// A waiter whose history read fails returns its own error, and the
    /// run goes on. A waiter pending when a status poll fails on its third
    /// attempt, and the `RunHandle`, return that run failure.
    #[tokio::test(start_paused = true)]
    async fn cloud_run_failure_is_not_replaced_by_a_control_failure() {
        let running = || Ok(execution_output(WireExecutionStatus::Running, None));
        let api = Arc::new(
            FakeApi::new()
                .with_execution(running())
                .with_execution(running())
                .with_execution(running())
                .with_execution(Err(fake_error("fake execution 1")))
                .with_execution(Err(fake_error("fake execution 2")))
                .with_execution(Err(fake_error("fake execution 3")))
                .with_history_pages(vec![
                    Err(fake_error("fake history 1")),
                    Err(fake_error("fake history 2")),
                    Err(fake_error("fake history 3")),
                ])
                .repeat_history(Ok(Vec::new())),
        );
        let start = tokio::time::Instant::now();
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        let ghost = run.operation("ghost");
        let (end, (own, pending)) = tokio::join!(run, async {
            let own = approval.wait_for_data(WaitingStatus::Started).await;
            let pending = ghost.wait_for_data(WaitingStatus::Completed).await;
            (own, pending)
        });
        assert_aws(&own.expect_err("the waiter's read fails"), "fake history 3");
        let end = end.expect_err("the status poll fails");
        assert_aws(&end, "fake execution 3");
        let pending = pending.expect_err("the pending waiter gets the run failure");
        assert_aws(&pending, "fake execution 3");
        assert_eq!(pending.to_string(), end.to_string());
        assert_eq!(
            api.execution_ms(start),
            [1000, 2000, 3000, 4000, 5000, 6450]
        );
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_snapshot_is_last_fold() {
        let step = |event_type| {
            Event::builder()
                .event_type(event_type)
                .id("s")
                .name("s")
                .event_id(1)
                .build()
        };
        let api = Arc::new(
            FakeApi::new()
                .with_history_pages(vec![Ok((vec![step(EventType::StepStarted)], None))])
                .repeat_history(Ok(vec![step(EventType::StepSucceeded)])),
        );
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let s = run.operation("s");
        assert!(s.snapshot().is_none());
        let op = s
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        assert_eq!(op.status(), OperationStatus::Started);
        assert_eq!(
            s.snapshot().map(|op| op.status()),
            Some(OperationStatus::Started)
        );
        drop(run);
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_wait_for_data_not_reached_on_terminal() {
        let mut history = callback_started_history();
        history.push(
            Event::builder()
                .event_type(EventType::ExecutionSucceeded)
                .event_id(2)
                .build(),
        );
        let api = fake_with_history(history);
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let err = run
            .operation("ghost")
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect_err("never reached");
        assert!(
            matches!(
                err,
                TestRunnerError::OperationNotReached(WaitingStatus::Started)
            ),
            "{err:?}"
        );
        drop(run);
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_wait_for_data_polls_until_reached() {
        let api = Arc::new(
            FakeApi::new()
                .with_history_pages(vec![Ok((Vec::new(), None))])
                .repeat_history(Ok(callback_started_history())),
        );
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let op = run
            .operation("approval")
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started on the second read");
        assert_eq!(op.id(), "c");
        assert_eq!(history_fetches(&api.calls()), 2);
        drop(run);
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_wait_for_data_after_the_run_reads_the_last_fold() {
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(WireExecutionStatus::Succeeded, None)))
                .with_history_pages(vec![Ok((callback_started_history(), None))]),
        );
        let mut run = event_runner().run_with::<_, ()>(Arc::clone(&api), Vec::new());
        (&mut run).await.expect("runner");
        let calls = api.calls().len();
        let op = run
            .operation("approval")
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("in the final history");
        assert_eq!(op.id(), "c");
        assert_eq!(api.calls().len(), calls, "no further call");
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_send_callback_needs_a_started_callback() {
        let mut history = callback_started_history();
        history.push(
            Event::builder()
                .event_type(EventType::CallbackSucceeded)
                .id("c")
                .event_id(2)
                .build(),
        );
        let api = fake_with_history(history);
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let err = run
            .operation("approval")
            .send_callback(CallbackReply::raw("1"))
            .await
            .expect_err("answered");
        assert!(
            matches!(err, TestRunnerError::CallbackNotPending),
            "{err:?}"
        );
        drop(run);
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_heartbeat_needs_a_callback_id() {
        let api = fake_with_history(vec![
            Event::builder()
                .event_type(EventType::CallbackStarted)
                .id("c")
                .name("approval")
                .event_id(1)
                .build(),
        ]);
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let err = run
            .operation("approval")
            .send_callback_heartbeat()
            .await
            .expect_err("no callback ID");
        assert!(
            matches!(err, TestRunnerError::CallbackNotPending),
            "{err:?}"
        );
        drop(run);
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_spent_budget_without_a_fold_is_not_found() {
        /// A fake whose `GetDurableExecution` never answers.
        struct HangingExecution(FakeApi);
        impl CloudApi for HangingExecution {
            async fn send_invoke(&self, request: InvokeRequest) -> Result<InvokeOutput, BoxError> {
                self.0.send_invoke(request).await
            }
            async fn send_get_durable_execution(
                &self,
                _execution_arn: &str,
            ) -> Result<GetExecutionOutput, BoxError> {
                std::future::pending().await
            }
            async fn send_get_durable_execution_history(
                &self,
                execution_arn: &str,
                marker: Option<&str>,
            ) -> Result<GetHistoryOutput, BoxError> {
                self.0
                    .send_get_durable_execution_history(execution_arn, marker)
                    .await
            }
            async fn send_callback_success(
                &self,
                id: &str,
                result: Vec<u8>,
            ) -> Result<(), BoxError> {
                self.0.send_callback_success(id, result).await
            }
            async fn send_callback_failure(
                &self,
                id: &str,
                error: ErrorObject,
            ) -> Result<(), BoxError> {
                self.0.send_callback_failure(id, error).await
            }
            async fn send_callback_heartbeat(&self, id: &str) -> Result<(), BoxError> {
                self.0.send_callback_heartbeat(id).await
            }
        }
        let api = Arc::new(HangingExecution(FakeApi::new()));
        let mut run = event_runner()
            .max_poll_attempts(1)
            .run_with::<_, u32>(Arc::clone(&api), Vec::new());
        // The driver spends the budget of one poll on a `GetDurableExecution`
        // that never answers.
        assert!(
            tokio::time::timeout(std::time::Duration::from_secs(5), &mut run)
                .await
                .is_err()
        );
        let err = run
            .operation("approval")
            .send_callback_heartbeat()
            .await
            .expect_err("no history read yet");
        assert!(matches!(err, TestRunnerError::OperationNotFound), "{err:?}");
        assert_eq!(api.0.history_calls(), Vec::<HistoryCall>::new());
    }

    /// The SDK error a `GetDurableExecutionHistory` call returns for an
    /// execution the service does not show.
    fn history_not_found() -> BoxError {
        use aws_sdk_lambda::operation::get_durable_execution_history::GetDurableExecutionHistoryError;
        Box::new(aws_sdk_lambda::error::SdkError::service_error(
            GetDurableExecutionHistoryError::ResourceNotFoundException(
                aws_sdk_lambda::types::error::ResourceNotFoundException::builder()
                    .message("Durable Execution does not exist")
                    .build(),
            ),
            aws_sdk_lambda::config::http::HttpResponse::new(
                404.try_into().expect("a status code"),
                aws_smithy_types::body::SdkBody::empty(),
            ),
        ))
    }

    /// A fake whose history reads answer `ResourceNotFoundException` while
    /// `not_found` is above zero, then as `api` does. `reads` records the
    /// time of every history read.
    struct LateExecution {
        api: FakeApi,
        not_found: Mutex<u32>,
        reads: Mutex<Vec<tokio::time::Instant>>,
    }

    impl LateExecution {
        /// A fake whose first `not_found` history reads fail, and whose
        /// later reads return a started callback `approval`.
        fn new(not_found: u32) -> Arc<Self> {
            Arc::new(Self {
                api: FakeApi::new()
                    .with_history_pages(Vec::new())
                    .repeat_history(Ok(callback_started_history())),
                not_found: Mutex::new(not_found),
                reads: Mutex::new(Vec::new()),
            })
        }

        /// The milliseconds after `start` of every history read.
        fn read_ms(&self, start: tokio::time::Instant) -> Vec<u128> {
            self.reads
                .lock()
                .expect("lock")
                .iter()
                .map(|at| (*at - start).as_millis())
                .collect()
        }
    }

    /// Asserts `err` is the `Aws` error of [`history_not_found`].
    fn assert_history_not_found(err: &TestRunnerError) {
        use aws_sdk_lambda::operation::get_durable_execution_history::GetDurableExecutionHistoryError;
        assert!(matches!(err, TestRunnerError::Aws(_)), "{err:?}");
        let source = std::error::Error::source(err).expect("a source");
        let sdk = source
            .downcast_ref::<aws_sdk_lambda::error::SdkError<
                GetDurableExecutionHistoryError,
                aws_sdk_lambda::config::http::HttpResponse,
            >>()
            .expect("the GetDurableExecutionHistory SDK error");
        assert!(
            matches!(
                sdk.as_service_error(),
                Some(GetDurableExecutionHistoryError::ResourceNotFoundException(
                    _
                ))
            ),
            "{sdk:?}"
        );
    }
    impl CloudApi for LateExecution {
        async fn send_invoke(&self, request: InvokeRequest) -> Result<InvokeOutput, BoxError> {
            self.api.send_invoke(request).await
        }
        async fn send_get_durable_execution(
            &self,
            execution_arn: &str,
        ) -> Result<GetExecutionOutput, BoxError> {
            self.api.send_get_durable_execution(execution_arn).await
        }
        async fn send_get_durable_execution_history(
            &self,
            execution_arn: &str,
            marker: Option<&str>,
        ) -> Result<GetHistoryOutput, BoxError> {
            self.reads
                .lock()
                .expect("lock")
                .push(tokio::time::Instant::now());
            {
                let mut not_found = self.not_found.lock().expect("lock");
                if *not_found > 0 {
                    *not_found -= 1;
                    return Err(history_not_found());
                }
            }
            self.api
                .send_get_durable_execution_history(execution_arn, marker)
                .await
        }
        async fn send_callback_success(&self, id: &str, payload: Vec<u8>) -> Result<(), BoxError> {
            self.api.send_callback_success(id, payload).await
        }
        async fn send_callback_failure(
            &self,
            id: &str,
            error: ErrorObject,
        ) -> Result<(), BoxError> {
            self.api.send_callback_failure(id, error).await
        }
        async fn send_callback_heartbeat(&self, id: &str) -> Result<(), BoxError> {
            self.api.send_callback_heartbeat(id).await
        }
    }

    /// A `ResourceNotFoundException` fails a history attempt as any error
    /// does, before and after the first successful read: a waiter retries it
    /// twice, and the third one is that waiter's error only. A later waiter
    /// still reads the history.
    #[tokio::test(start_paused = true)]
    async fn cloud_wait_for_data_retries_a_not_found() {
        let start = tokio::time::Instant::now();
        let api = LateExecution::new(2);
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        let op = approval
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("the callback started");
        assert_eq!(op.kind(), OperationKind::Callback);
        assert_eq!(api.read_ms(start), [1000, 2000, 3450]);
        *api.not_found.lock().expect("lock") = 3;
        let err = approval
            .wait_for_data(WaitingStatus::Completed)
            .await
            .expect_err("the third not-found is an AWS error");
        assert_history_not_found(&err);
        assert_eq!(
            api.read_ms(start),
            [1000, 2000, 3450, 4450, 5450, 6900],
            "six requests in all"
        );
        let op = approval
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("the run goes on");
        assert_eq!(op.id(), "c");
        assert_eq!(
            api.read_ms(start),
            [1000, 2000, 3450, 4450, 5450, 6900, 7900]
        );
        assert!(run.shared.terminal_error().is_none());
        drop(run);
    }

    /// Three `ResourceNotFoundException` answers before the first read are
    /// the waiter's error, and the waiter reads no fourth answer.
    #[tokio::test(start_paused = true)]
    async fn cloud_wait_for_data_not_found_three_times_is_aws() {
        let start = tokio::time::Instant::now();
        let api = LateExecution::new(3);
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let err = run
            .operation("approval")
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect_err("the third not-found is an AWS error");
        assert_history_not_found(&err);
        assert_eq!(api.read_ms(start), [1000, 2000, 3450]);
        drop(run);
    }

    /// A waiter whose every history attempt fails returns the third error.
    /// It is the waiter's only: the driver keeps its one-second status
    /// polls, and the run succeeds.
    #[tokio::test(start_paused = true)]
    async fn cloud_wait_for_data_history_error_is_aws() {
        let start = tokio::time::Instant::now();
        let running = || Ok(execution_output(WireExecutionStatus::Running, None));
        let api = Arc::new(
            FakeApi::new()
                .with_execution(running())
                .with_execution(running())
                .with_execution(running())
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("7"),
                )))
                .with_history_pages(vec![
                    Err(fake_error("fake history 1")),
                    Err(fake_error("fake history 2")),
                    Err(fake_error("fake history 3")),
                    Ok((Vec::new(), None)),
                ]),
        );
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        let (result, waited) = tokio::join!(run, approval.wait_for_data(WaitingStatus::Started));
        let waited = waited.expect_err("the waiter fails");
        assert_aws(&waited, "fake history 3");
        let result = result.expect("the run succeeds");
        assert_eq!(result.output(), Some(&7));
        assert_eq!(api.execution_ms(start), [1000, 2000, 3000, 4000]);
        assert_eq!(api.history_ms(start), unmarked(&[1000, 2000, 3450, 4000]));
    }

    /// A fake whose callback calls fail with `fake success`, `fake failure`
    /// and `fake heartbeat`. With a barrier, each callback call waits at it
    /// before it fails, so two calls fail together. The counter counts the
    /// callback calls, and the inner fake records each one in its `calls`.
    struct FailingCallbacks(FakeApi, Option<Arc<tokio::sync::Barrier>>, AtomicU32);

    impl FailingCallbacks {
        async fn meet(&self, call: FakeCall) {
            self.0.record(call);
            self.2.fetch_add(1, Ordering::SeqCst);
            if let Some(barrier) = &self.1 {
                barrier.wait().await;
            }
        }
    }

    /// A [`FailingCallbacks`] whose every history read returns a started
    /// callback `approval`.
    fn failing_callbacks(barrier: Option<Arc<tokio::sync::Barrier>>) -> Arc<FailingCallbacks> {
        Arc::new(FailingCallbacks(
            FakeApi::new()
                .with_history_pages(Vec::new())
                .repeat_history(Ok(callback_started_history())),
            barrier,
            AtomicU32::new(0),
        ))
    }

    /// A [`failing_callbacks`] fake whose first status poll answers
    /// `SUCCEEDED` with the output `7`.
    fn failing_callbacks_then_success(
        barrier: Option<Arc<tokio::sync::Barrier>>,
    ) -> Arc<FailingCallbacks> {
        let api = failing_callbacks(barrier);
        api.0
            .executions
            .lock()
            .expect("lock")
            .push_back(Ok(execution_output(
                WireExecutionStatus::Succeeded,
                Some("7"),
            )));
        api
    }

    /// A [`FakeApi`] whose first three status poll attempts fail with
    /// `fake execution 1`, `2` and `3`, so the poll at 1000 ms fails the
    /// run at 3450 ms, and whose every history read returns a started
    /// callback `approval`.
    fn failing_polls() -> FakeApi {
        FakeApi::new()
            .with_execution(Err(fake_error("fake execution 1")))
            .with_execution(Err(fake_error("fake execution 2")))
            .with_execution(Err(fake_error("fake execution 3")))
            .with_history_pages(Vec::new())
            .repeat_history(Ok(callback_started_history()))
    }

    /// Asserts that `err` is `Unsupported` with `text`.
    #[track_caller]
    fn assert_unsupported(err: &TestRunnerError, text: &str) {
        assert!(
            matches!(err, TestRunnerError::Unsupported(t) if *t == text),
            "{err:?}"
        );
    }

    /// Asserts that `err` is `expected`: the same variant and text.
    #[track_caller]
    fn assert_same_error(err: &TestRunnerError, expected: &TestRunnerError) {
        assert_eq!(
            std::mem::discriminant(err),
            std::mem::discriminant(expected),
            "{err:?}"
        );
        assert_eq!(err.to_string(), expected.to_string());
    }

    impl CloudApi for FailingCallbacks {
        async fn send_invoke(&self, request: InvokeRequest) -> Result<InvokeOutput, BoxError> {
            self.0.send_invoke(request).await
        }
        async fn send_get_durable_execution(
            &self,
            execution_arn: &str,
        ) -> Result<GetExecutionOutput, BoxError> {
            self.0.send_get_durable_execution(execution_arn).await
        }
        async fn send_get_durable_execution_history(
            &self,
            execution_arn: &str,
            marker: Option<&str>,
        ) -> Result<GetHistoryOutput, BoxError> {
            self.0
                .send_get_durable_execution_history(execution_arn, marker)
                .await
        }
        async fn send_callback_success(&self, id: &str, result: Vec<u8>) -> Result<(), BoxError> {
            self.meet(FakeCall::CallbackSuccess(id.to_owned(), result))
                .await;
            Err(fake_error("fake success"))
        }
        async fn send_callback_failure(
            &self,
            id: &str,
            error: ErrorObject,
        ) -> Result<(), BoxError> {
            self.meet(FakeCall::CallbackFailure(id.to_owned(), error))
                .await;
            Err(fake_error("fake failure"))
        }
        async fn send_callback_heartbeat(&self, id: &str) -> Result<(), BoxError> {
            self.meet(FakeCall::Heartbeat(id.to_owned())).await;
            Err(fake_error("fake heartbeat"))
        }
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_callback_error_is_aws() {
        // Each callback API error belongs to its call: the call returns it,
        // a later control still works, and the run succeeds.
        for (case, text) in [
            (0_u8, "fake success"),
            (1, "fake failure"),
            (2, "fake heartbeat"),
        ] {
            let start = tokio::time::Instant::now();
            let api = failing_callbacks_then_success(None);
            let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
            let approval = run.operation("approval");
            let err = match case {
                0 => approval.send_callback(CallbackReply::raw("1")).await,
                1 => {
                    approval
                        .send_callback(CallbackReply::failure(CallbackFailure::default()))
                        .await
                }
                _ => approval.send_callback_heartbeat().await,
            }
            .expect_err("the callback call fails");
            assert_aws(&err, text);
            // The control reads the history at once, then makes its one
            // callback call.
            assert_eq!(api.0.history_ms(start), unmarked(&[0]));
            let calls = api.0.calls();
            assert!(
                matches!(
                    (case, calls.as_slice()),
                    (
                        0,
                        [
                            FakeCall::Invoke,
                            FakeCall::History,
                            FakeCall::CallbackSuccess(..)
                        ]
                    ) | (
                        1,
                        [
                            FakeCall::Invoke,
                            FakeCall::History,
                            FakeCall::CallbackFailure(..)
                        ]
                    ) | (
                        2,
                        [FakeCall::Invoke, FakeCall::History, FakeCall::Heartbeat(_)]
                    )
                ),
                "{calls:?}"
            );
            assert_eq!(api.2.load(Ordering::SeqCst), 1, "one callback call");
            let later = approval
                .wait_for_data(WaitingStatus::Started)
                .await
                .expect("a later wait reaches the callback");
            assert_eq!(later.id(), "c");
            let result = run.await.expect("the run succeeds");
            assert!(result.is_success());
            assert_eq!(result.output(), Some(&7));
            assert_eq!(api.2.load(Ordering::SeqCst), 1, "no other callback call");
        }
    }

    /// The run failures before the invoke published the execution ARN:
    /// an event that does not serialize, a failed `Invoke` and an invoke
    /// response without an ARN. Under `InvocationType::Event` a wait and a
    /// callback control return the run failure, because no target exists.
    /// Under `RequestResponse` they return their `Unsupported` errors, and
    /// so do timeout replies, `pause` and `resume` under both. No history or
    /// callback call is made.
    #[tokio::test(start_paused = true)]
    async fn cloud_pre_target_run_failures_keep_their_cause() {
        for row in 0_u8..3 {
            for runner in [event_runner(), CloudRunner::new("f")] {
                let api = failing_callbacks(None);
                let mut run = match row {
                    0 => runner.start::<_, u32, _>(
                        {
                            let api = Arc::clone(&api);
                            async move { api }
                        },
                        serde_json::to_vec(&Unserializable),
                    ),
                    1 => {
                        *api.0.invoke.lock().expect("lock") = Some(Err(fake_error("fake invoke")));
                        runner.run_with::<_, u32>(Arc::clone(&api), Vec::new())
                    }
                    _ => {
                        *api.0.invoke.lock().expect("lock") = Some(Ok(invoke_output(None)));
                        runner.run_with::<_, u32>(Arc::clone(&api), Vec::new())
                    }
                };
                let approval = run.operation("approval");
                let end = (&mut run).await.expect_err("the run fails");
                match row {
                    0 => assert!(
                        matches!(end, TestRunnerError::EventSerialization(_)),
                        "{end:?}"
                    ),
                    1 => assert_aws(&end, "fake invoke"),
                    _ => assert!(matches!(end, TestRunnerError::NoExecutionArn), "{end:?}"),
                }
                let wait = approval
                    .wait_for_data(WaitingStatus::Started)
                    .await
                    .expect_err("wait_for_data");
                let send = approval
                    .send_callback(CallbackReply::raw("1"))
                    .await
                    .expect_err("send_callback");
                let heartbeat = approval
                    .send_callback_heartbeat()
                    .await
                    .expect_err("heartbeat");
                let event = runner.invocation_type == InvocationType::Event;
                if event {
                    for err in [&wait, &send, &heartbeat] {
                        assert_same_error(err, &end);
                    }
                } else {
                    assert_unsupported(&wait, WAIT_FOR_DATA_REQUEST_RESPONSE_UNSUPPORTED);
                    assert_unsupported(&send, SEND_CALLBACK_REQUEST_RESPONSE_UNSUPPORTED);
                    assert_unsupported(
                        &heartbeat,
                        SEND_CALLBACK_HEARTBEAT_REQUEST_RESPONSE_UNSUPPORTED,
                    );
                }
                for reply in [CallbackReply::timeout(), CallbackReply::heartbeat_timeout()] {
                    let err = approval.send_callback(reply).await.expect_err("timeout");
                    let text = if event {
                        CLOUD_TIMEOUT_REPLY_UNSUPPORTED
                    } else {
                        SEND_CALLBACK_REQUEST_RESPONSE_UNSUPPORTED
                    };
                    assert_unsupported(&err, text);
                }
                for err in [
                    run.pause().await.expect_err("pause"),
                    run.resume().await.expect_err("resume"),
                ] {
                    assert_unsupported(&err, CLOUD_PAUSE_UNSUPPORTED);
                }
                assert_same_error(&(&mut run).await.expect_err("the run failure"), &end);
                let calls = api.0.calls();
                assert!(
                    calls.iter().all(|call| matches!(call, FakeCall::Invoke)),
                    "{calls:?}"
                );
                assert_eq!(api.2.load(Ordering::SeqCst), 0);
            }
        }
    }

    /// A callback API failure after the driver returned a `Running` result
    /// (its poll budget spent) fails only that call. A heartbeat fails with
    /// its own error, a reached wait reads the last fold, the `Unsupported`
    /// controls keep their errors, and the consumed `RunHandle` stays
    /// pending.
    #[tokio::test(start_paused = true)]
    async fn cloud_callback_error_after_the_result_fails_only_that_call() {
        let api = failing_callbacks(None);
        let mut run = event_runner()
            .max_poll_attempts(2)
            .run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        approval
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        let result = (&mut run).await.expect("the result");
        assert_eq!(result.status(), ExecutionStatus::Running);
        let history = history_fetches(&api.0.calls());
        let err = approval
            .send_callback(CallbackReply::raw("1"))
            .await
            .expect_err("the callback call fails");
        assert_aws(&err, "fake success");
        let err = approval
            .send_callback_heartbeat()
            .await
            .expect_err("the heartbeat fails");
        assert_aws(&err, "fake heartbeat");
        assert_eq!(api.2.load(Ordering::SeqCst), 2, "both calls were made");
        let op = approval
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("the last fold");
        assert_eq!(op.id(), "c");
        assert_eq!(
            history_fetches(&api.0.calls()),
            history,
            "the spent budget starts no read"
        );
        let err = approval
            .send_callback(CallbackReply::timeout())
            .await
            .expect_err("timeout reply");
        assert_unsupported(&err, CLOUD_TIMEOUT_REPLY_UNSUPPORTED);
        for err in [
            run.pause().await.expect_err("pause"),
            run.resume().await.expect_err("resume"),
        ] {
            assert_unsupported(&err, CLOUD_PAUSE_UNSUPPORTED);
        }
        assert!(run.shared.terminal_error().is_none());
        let pending = std::future::poll_fn(|cx| {
            std::task::Poll::Ready(Pin::new(&mut run).poll(cx).is_pending())
        })
        .await;
        assert!(pending, "the result was already taken");
    }

    /// Two callback calls that fail together return their own errors: both
    /// calls are made, and the run succeeds.
    #[tokio::test(start_paused = true)]
    async fn cloud_concurrent_callback_errors_are_separate() {
        let api = failing_callbacks_then_success(Some(Arc::new(tokio::sync::Barrier::new(2))));
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        let (success, failure) = tokio::join!(
            approval.send_callback(CallbackReply::raw("1")),
            approval.send_callback(CallbackReply::failure(CallbackFailure::default())),
        );
        assert_aws(&success.expect_err("success fails"), "fake success");
        assert_aws(&failure.expect_err("failure fails"), "fake failure");
        assert_eq!(api.2.load(Ordering::SeqCst), 2, "both calls were sent");
        let calls = api.0.calls();
        assert!(
            calls
                .iter()
                .any(|call| matches!(call, FakeCall::CallbackSuccess(..)))
                && calls
                    .iter()
                    .any(|call| matches!(call, FakeCall::CallbackFailure(..))),
            "{calls:?}"
        );
        let result = run.await.expect("the run succeeds");
        assert_eq!(result.output(), Some(&7));
    }

    /// A callback whose history read fails on its third attempt returns
    /// that error and makes no callback call. A second callback reads the
    /// history at once and sends its reply, and the run succeeds.
    #[tokio::test(start_paused = true)]
    async fn cloud_callback_history_error_fails_only_that_call() {
        let start = tokio::time::Instant::now();
        let running = || Ok(execution_output(WireExecutionStatus::Running, None));
        let api = Arc::new(
            FakeApi::new()
                .with_execution(running())
                .with_execution(running())
                .with_execution(Ok(execution_output(
                    WireExecutionStatus::Succeeded,
                    Some("7"),
                )))
                .with_history_pages(vec![
                    Err(fake_error("fake history 1")),
                    Err(fake_error("fake history 2")),
                    Err(fake_error("fake history 3")),
                ])
                .repeat_history(Ok(callback_started_history())),
        );
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        let err = approval
            .send_callback(CallbackReply::raw("1"))
            .await
            .expect_err("the history read fails");
        assert_aws(&err, "fake history 3");
        assert_eq!(api.history_ms(start), unmarked(&[0, 1000, 2450]));
        assert!(
            !api.calls()
                .iter()
                .any(|call| matches!(call, FakeCall::CallbackSuccess(..))),
            "no callback call"
        );
        approval
            .send_callback(CallbackReply::raw("2"))
            .await
            .expect("the second reply is sent");
        assert_eq!(api.history_ms(start), unmarked(&[0, 1000, 2450, 2450]));
        assert_eq!(
            api.calls().last(),
            Some(&FakeCall::CallbackSuccess("cb-1".to_owned(), b"2".to_vec()))
        );
        let result = run.await.expect("the run succeeds");
        assert_eq!(result.output(), Some(&7));
    }

    /// After a run failure past the invoke, a late wait decides from the
    /// last fold: for each of a status poll that fails on its third
    /// attempt, a failed final history read and an output that does not
    /// decode, a wait for a reached status returns the stored operation and
    /// one for an unreached status returns the run failure. Neither makes a
    /// Lambda call or spends a poll.
    #[tokio::test(start_paused = true)]
    async fn cloud_late_wait_after_post_target_failure_uses_the_last_fold() {
        for row in 0_u8..3 {
            let api = Arc::new(match row {
                0 => failing_polls(),
                1 => FakeApi::new()
                    .with_execution(Ok(execution_output(
                        WireExecutionStatus::Succeeded,
                        Some("7"),
                    )))
                    .with_history_pages(vec![Ok((callback_started_history(), None))])
                    .repeat_history(Err("fake final")),
                _ => FakeApi::new()
                    .with_execution(Ok(execution_output(
                        WireExecutionStatus::Succeeded,
                        Some("\"text\""),
                    )))
                    .with_history_pages(Vec::new())
                    .repeat_history(Ok(callback_started_history())),
            });
            let mut run = event_runner()
                .max_poll_attempts(10)
                .run_with::<_, u32>(Arc::clone(&api), Vec::new());
            let approval = run.operation("approval");
            approval
                .send_callback_heartbeat()
                .await
                .expect("the heartbeat stores a fold");
            let end = (&mut run).await.expect_err("the run fails");
            match row {
                0 => assert_aws(&end, "fake execution 3"),
                1 => assert_aws(&end, "fake final"),
                _ => assert!(
                    matches!(end, TestRunnerError::OutputDeserialization(_)),
                    "{end:?}"
                ),
            }
            let calls = api.calls();
            let used = run.shared.live.lock().cloud.used;
            let op = approval
                .wait_for_data(WaitingStatus::Started)
                .await
                .expect("the fold reaches Started");
            assert_eq!(op.id(), "c");
            let err = approval
                .wait_for_data(WaitingStatus::Completed)
                .await
                .expect_err("the fold does not reach Completed");
            assert_same_error(&err, &end);
            assert_eq!(api.calls(), calls, "row {row}: no Lambda call");
            assert_eq!(run.shared.live.lock().cloud.used, used, "no poll spent");
        }
    }

    /// The fake behind a [`post_failure_callbacks`] row.
    trait Recorded {
        fn fake(&self) -> &FakeApi;
    }

    impl Recorded for FakeApi {
        fn fake(&self) -> &FakeApi {
            self
        }
    }

    impl Recorded for FailingCallbacks {
        fn fake(&self) -> &FakeApi {
            &self.0
        }
    }

    /// How much poll budget a [`post_failure_callbacks`] row leaves.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    enum Budget {
        /// No limit: a callback control reads the history.
        Remaining,
        /// Spent, after a wait stored a fold.
        SpentWithFold,
        /// Spent by the driver's one poll, with no fold.
        SpentWithoutFold,
    }

    /// Runs `api`, whose status poll at 1000 ms fails the run, with
    /// `budget`, then sends a success reply, a failure reply and a
    /// heartbeat, and checks each control's calls. With budget remaining,
    /// it also checks that a read made after the run failure became the
    /// fold that `snapshot()` returns. Returns the controls' outcomes.
    async fn post_failure_callbacks<A>(
        api: Arc<A>,
        budget: Budget,
    ) -> Vec<Result<(), TestRunnerError>>
    where
        A: CloudApi + Recorded + Send + Sync + 'static,
    {
        let runner = match budget {
            Budget::Remaining => event_runner(),
            Budget::SpentWithFold => event_runner().max_poll_attempts(2),
            Budget::SpentWithoutFold => event_runner().max_poll_attempts(1),
        };
        let mut run = runner.run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        if budget == Budget::SpentWithFold {
            approval
                .wait_for_data(WaitingStatus::Started)
                .await
                .expect("the wait stores a fold");
        }
        let end = (&mut run).await.expect_err("the run fails");
        assert_aws(&end, "fake execution 3");
        if budget == Budget::Remaining {
            assert!(
                approval.snapshot().is_none(),
                "no fold before the callbacks"
            );
        }
        let mut outcomes = Vec::new();
        for case in 0_u8..3 {
            let before = api.fake().calls().len();
            outcomes.push(match case {
                0 => approval.send_callback(CallbackReply::raw("1")).await,
                1 => {
                    approval
                        .send_callback(CallbackReply::failure(CallbackFailure::default()))
                        .await
                }
                _ => approval.send_callback_heartbeat().await,
            });
            let calls = api.fake().calls();
            let new = calls.get(before..).expect("later calls");
            let read = budget == Budget::Remaining;
            let made = match (case, new) {
                (_, []) => budget == Budget::SpentWithoutFold,
                (0, [FakeCall::History, FakeCall::CallbackSuccess(id, payload)]) => {
                    read && id == "cb-1" && payload == b"1"
                }
                (0, [FakeCall::CallbackSuccess(id, payload)]) => {
                    !read && id == "cb-1" && payload == b"1"
                }
                (1, [FakeCall::History, FakeCall::CallbackFailure(id, _)])
                | (2, [FakeCall::History, FakeCall::Heartbeat(id)]) => read && id == "cb-1",
                (1, [FakeCall::CallbackFailure(id, _)]) | (2, [FakeCall::Heartbeat(id)]) => {
                    !read && id == "cb-1"
                }
                _ => false,
            };
            assert!(made, "{budget:?} case {case}: {new:?}");
        }
        if budget == Budget::Remaining {
            // A successful read after the run failure became the fold.
            assert_eq!(
                approval.snapshot().map(|op| op.id().to_owned()).as_deref(),
                Some("c"),
                "the post-failure read is the fold"
            );
        }
        assert_same_error(&(&mut run).await.expect_err("the run failure stays"), &end);
        outcomes
    }

    /// After a run failure past the invoke, a callback control follows its
    /// normal path: it reads the history while budget remains, else uses
    /// the last fold, makes its callback call and returns its own outcome.
    /// With the budget spent and no fold it returns `OperationNotFound`
    /// without a call. The `RunHandle` keeps the run failure.
    #[tokio::test(start_paused = true)]
    async fn cloud_callback_started_after_run_failure_returns_its_own_outcome() {
        for budget in [Budget::Remaining, Budget::SpentWithFold] {
            for outcome in post_failure_callbacks(Arc::new(failing_polls()), budget).await {
                outcome.expect("the callback call succeeds");
            }
            let api = Arc::new(FailingCallbacks(failing_polls(), None, AtomicU32::new(0)));
            let outcomes = post_failure_callbacks(Arc::clone(&api), budget).await;
            for (outcome, text) in
                outcomes
                    .iter()
                    .zip(["fake success", "fake failure", "fake heartbeat"])
            {
                assert_aws(outcome.as_ref().expect_err("the call fails"), text);
            }
            assert_eq!(api.2.load(Ordering::SeqCst), 3);
        }
        for outcome in
            post_failure_callbacks(Arc::new(failing_polls()), Budget::SpentWithoutFold).await
        {
            let err = outcome.expect_err("no fold");
            assert!(matches!(err, TestRunnerError::OperationNotFound), "{err:?}");
        }
    }

    /// A fake whose status polls and history are those of
    /// [`failing_polls`], and whose heartbeat fails with `fake heartbeat`.
    /// With `fail_success`, a callback success call fails with
    /// `fake success` after the inner fake recorded it. One call is held at
    /// a gate after it signals `entered`: the history read with the 0-based
    /// index `gate_history`, or, without one, every callback success call.
    struct GatedApi {
        inner: FakeApi,
        gate_history: Option<usize>,
        fail_success: bool,
        history_reads: std::sync::atomic::AtomicUsize,
        gate: tokio::sync::Semaphore,
        entered: tokio::sync::Notify,
    }

    impl GatedApi {
        fn new(gate_history: Option<usize>, fail_success: bool) -> Self {
            Self {
                inner: failing_polls(),
                gate_history,
                fail_success,
                history_reads: std::sync::atomic::AtomicUsize::new(0),
                gate: tokio::sync::Semaphore::new(0),
                entered: tokio::sync::Notify::new(),
            }
        }

        async fn hold(&self) {
            self.entered.notify_one();
            drop(self.gate.acquire().await.expect("the gate stays open"));
        }
    }

    impl CloudApi for GatedApi {
        async fn send_invoke(&self, request: InvokeRequest) -> Result<InvokeOutput, BoxError> {
            self.inner.send_invoke(request).await
        }
        async fn send_get_durable_execution(
            &self,
            execution_arn: &str,
        ) -> Result<GetExecutionOutput, BoxError> {
            self.inner.send_get_durable_execution(execution_arn).await
        }
        async fn send_get_durable_execution_history(
            &self,
            execution_arn: &str,
            marker: Option<&str>,
        ) -> Result<GetHistoryOutput, BoxError> {
            let index = self.history_reads.fetch_add(1, Ordering::SeqCst);
            if self.gate_history == Some(index) {
                self.hold().await;
            }
            self.inner
                .send_get_durable_execution_history(execution_arn, marker)
                .await
        }
        async fn send_callback_success(&self, id: &str, result: Vec<u8>) -> Result<(), BoxError> {
            if self.gate_history.is_none() {
                self.hold().await;
            }
            self.inner.send_callback_success(id, result).await?;
            if self.fail_success {
                return Err(fake_error("fake success"));
            }
            Ok(())
        }
        async fn send_callback_failure(
            &self,
            id: &str,
            error: ErrorObject,
        ) -> Result<(), BoxError> {
            self.inner.send_callback_failure(id, error).await
        }
        #[expect(clippy::unused_async_trait_impl)] // reason: the fake fails at once
        async fn send_callback_heartbeat(&self, _: &str) -> Result<(), BoxError> {
            Err(fake_error("fake heartbeat"))
        }
    }

    /// A wait whose history read is outstanding when a status poll fails
    /// the run returns the run failure and drops the read. A callback whose
    /// history read or callback call is outstanding then stays pending while
    /// the call is held, and once it is released returns its own outcome.
    #[tokio::test(start_paused = true)]
    async fn cloud_outstanding_controls_follow_the_run_failure_rules() {
        let api = Arc::new(GatedApi::new(Some(0), false));
        let mut run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        let (end, waited) = tokio::join!(&mut run, approval.wait_for_data(WaitingStatus::Started));
        let end = end.expect_err("the status poll fails");
        assert_aws(&end, "fake execution 3");
        assert_same_error(&waited.expect_err("the waiter gets the run failure"), &end);
        api.gate.add_permits(1);
        tokio::time::sleep(std::time::Duration::from_secs(10)).await;
        assert_eq!(
            api.inner.history_calls(),
            Vec::<HistoryCall>::new(),
            "the held read was dropped"
        );

        for (gate_history, fail_success) in [
            (Some(0), false),
            (Some(0), true),
            (None, false),
            (None, true),
        ] {
            let api = Arc::new(GatedApi::new(gate_history, fail_success));
            let mut run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
            let approval = run.operation("approval");
            let send = approval.send_callback(CallbackReply::raw("1"));
            let mut send = std::pin::pin!(send);
            tokio::select! {
                biased;
                outcome = &mut send => panic!("the call is held: {outcome:?}"),
                () = api.entered.notified() => {}
            }
            let end = (&mut run).await.expect_err("the status poll fails");
            assert_aws(&end, "fake execution 3");
            let case = format!("{gate_history:?}/{fail_success}");
            assert!(
                tokio::time::timeout(std::time::Duration::from_secs(3600), &mut send)
                    .await
                    .is_err(),
                "{case}: the callback stays pending while its call is held"
            );
            api.gate.add_permits(1);
            let outcome = send.await;
            if fail_success {
                assert_aws(&outcome.expect_err("the call fails"), "fake success");
            } else {
                outcome.expect("the call succeeds");
            }
            assert_eq!(
                api.inner.calls().last(),
                Some(&FakeCall::CallbackSuccess("cb-1".to_owned(), b"1".to_vec())),
                "{case}"
            );
            assert_same_error(&(&mut run).await.expect_err("the run failure stays"), &end);
        }
    }

    // ---- Live control: the hold rule and wakeups ----

    /// A step `a` that takes 200 ms, then a step `b`.
    async fn slow_then_fast((): (), ctx: DurableContext) -> Result<(), BoxError> {
        ctx.step(|_| async {
            tokio::time::sleep(std::time::Duration::from_millis(200)).await;
            Ok(1_u32)
        })
        .name("a")
        .await?;
        ctx.step(|_| async { Ok(2_u32) }).name("b").await?;
        Ok(())
    }

    #[tokio::test]
    async fn dropped_wait_for_data_stops_counting() {
        let run = LocalRunner::new().run(slow_then_fast, ());
        let a = run.operation("a");
        a.wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        // The waiter never completes. When the timeout drops it, nothing
        // waits any more, while the body of `a` still runs.
        let ghost = run.operation("ghost");
        assert!(
            tokio::time::timeout(
                std::time::Duration::from_millis(20),
                ghost.wait_for_data(WaitingStatus::Started)
            )
            .await
            .is_err()
        );
        tokio::time::sleep(std::time::Duration::from_millis(400)).await;
        assert_eq!(
            a.snapshot().map(|op| op.status()),
            Some(OperationStatus::Started),
            "the SUCCEED of `a` is held"
        );
        run.await.expect("runner");
    }

    #[tokio::test]
    async fn dropped_run_handle_stops_counting() {
        let mut run = LocalRunner::new().run(slow_then_fast, ());
        let a = run.operation("a");
        a.wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        assert!(
            tokio::time::timeout(std::time::Duration::from_millis(20), &mut run)
                .await
                .is_err()
        );
        drop(run);
        tokio::time::sleep(std::time::Duration::from_millis(400)).await;
        assert_eq!(
            a.snapshot().map(|op| op.status()),
            Some(OperationStatus::Started),
            "the SUCCEED of `a` is held"
        );
        let op = a
            .wait_for_data(WaitingStatus::Completed)
            .await
            .expect("the waiter drives the run on");
        assert_eq!(op.status(), OperationStatus::Succeeded);
    }

    #[tokio::test]
    async fn wait_for_data_returns_the_snapshot_at_the_recheck() {
        // The run itself keeps waiting, so the step's SUCCEED follows its
        // START at once. The waiter still reports the step as the START
        // left it.
        let run = LocalRunner::new().run(steps_named(&["a"]), ());
        let a = run.operation("a");
        let (result, op) = tokio::join!(run, a.wait_for_data(WaitingStatus::Started));
        result.expect("runner");
        assert_eq!(
            op.expect("started").status(),
            OperationStatus::Started,
            "the snapshot taken when the START was applied"
        );
    }

    #[tokio::test]
    async fn waiter_on_another_task_sees_the_run_end() {
        let run = LocalRunner::new().run(steps_named(&["s"]), ());
        let ghost = run.operation("ghost");
        let waiter = tokio::spawn(async move { ghost.wait_for_data(WaitingStatus::Started).await });
        tokio::task::yield_now().await;
        run.await.expect("runner");
        let err = tokio::time::timeout(std::time::Duration::from_secs(1), waiter)
            .await
            .expect("the run end wakes the waiter")
            .expect("joined")
            .expect_err("never reached");
        assert!(
            matches!(
                err,
                TestRunnerError::OperationNotReached(WaitingStatus::Started)
            ),
            "{err:?}"
        );
    }

    #[tokio::test]
    async fn reply_wakes_a_waiting_wait_for_data() {
        let run = LocalRunner::new().run(
            |(), ctx: DurableContext| async move {
                let cb = ctx.create_callback::<u32>().name("c").await?;
                Ok::<_, BoxError>(cb.result().await?)
            },
            (),
        );
        let c = run.operation("c");
        c.wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        // Paused, the run moves no further, so only the reply can make the
        // waiter ready.
        run.pause().await.expect("paused");
        let completed = run.operation("c");
        let waiter =
            tokio::spawn(async move { completed.wait_for_data(WaitingStatus::Completed).await });
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
        c.send_callback(success(&4_u32)).await.expect("sent");
        let op = tokio::time::timeout(std::time::Duration::from_secs(1), waiter)
            .await
            .expect("the reply wakes the waiter")
            .expect("joined")
            .expect("completed");
        assert_eq!(op.status(), OperationStatus::Succeeded);
        run.resume().await.expect("resumed");
        assert_eq!(run.await.expect("runner").output(), Some(&4));
    }

    async fn wait_then_step((): (), ctx: DurableContext) -> Result<u32, BoxError> {
        ctx.wait(std::time::Duration::from_secs(60))
            .name("w")
            .await?;
        Ok(ctx.step(|_| async { Ok(1_u32) }).name("after").await?)
    }

    #[tokio::test]
    async fn pause_after_the_last_checkpoint_keeps_the_next_invocation_token() {
        let mut run = LocalRunner::new().run(wait_then_step, ());
        run.operation("w")
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        // The invocation still runs, and sends no further checkpoint.
        run.pause().await.expect("paused");
        run.resume().await.expect("resumed");
        let result = (&mut run).await.expect("runner");
        assert_eq!(result.output(), Some(&1));
        assert_eq!(
            result.invocations().len(),
            2,
            "the step after the wait keeps its token"
        );
    }

    #[test]
    fn handle_debug_names_the_types() {
        let run = RunHandle::<u32>::from_shared(RunCore::failed(
            new_live(),
            Engine::Local,
            TestRunnerError::NoExecutionArn,
        ));
        let op = run.operation("x");
        let text = format!("{run:?} {op:?}");
        for name in [
            "RunHandle",
            "RunCore",
            "OperationHandle",
            "failure: Some(NoExecutionArn)",
        ] {
            assert!(text.contains(name), "{text}");
        }
        assert_eq!(format!("{:?}", RunEnd::Running), "Running");
        assert_eq!(format!("{:?}", RunEnd::Finished(None)), "Finished");
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_invoke_error_reaches_wait_for_data() {
        let api = Arc::new(FakeApi::new().with_invoke(Err(fake_error("fake invoke"))));
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let a = run.operation("a");
        let err = a
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect_err("the invoke failed");
        assert_aws(&err, "fake invoke");
        let err = run.await.expect_err("the run failed");
        assert_aws(&err, "fake invoke");
    }

    #[tokio::test(start_paused = true)]
    async fn cloud_run_handle_reads_the_last_fold() {
        let api = Arc::new(
            FakeApi::new()
                .with_execution(Ok(execution_output(WireExecutionStatus::Succeeded, None)))
                .with_history_pages(vec![Ok((callback_started_history(), None))]),
        );
        let mut run = event_runner().run_with::<_, ()>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        assert!(approval.snapshot().is_none());
        (&mut run).await.expect("runner");
        assert_eq!(
            approval.snapshot().map(|op| op.id().to_owned()),
            Some("c".to_owned()),
            "the final history read is the last fold"
        );
    }

    /// A waker that sets a flag.
    struct FlagWaker(Arc<std::sync::atomic::AtomicBool>);

    impl std::task::Wake for FlagWaker {
        fn wake(self: Arc<Self>) {
            self.0.store(true, Ordering::SeqCst);
        }
    }

    #[test]
    fn driver_end_wakes_a_future_that_found_it_busy() {
        let woken = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let other = std::task::Waker::from(Arc::new(FlagWaker(Arc::clone(&woken))));
        let cell: Arc<std::sync::OnceLock<std::sync::Weak<RunCore>>> =
            Arc::new(std::sync::OnceLock::new());
        let in_driver = Arc::clone(&cell);
        let driver: DriverFuture = Box::pin(async move {
            // Another runner future polls while this poll runs: it finds the
            // driver busy and returns without polling it.
            let shared = in_driver
                .get()
                .and_then(std::sync::Weak::upgrade)
                .expect("set");
            let id = shared.live.lock().add_control(ControlKind::Run);
            shared.drive(id, &other);
            Err(TestRunnerError::NoExecutionArn)
        });
        let shared = RunCore::new(new_live(), Engine::Local, driver);
        cell.set(Arc::downgrade(&shared)).expect("unset");
        let id = shared.live.lock().add_control(ControlKind::Run);
        shared.drive(id, std::task::Waker::noop());
        assert!(shared.ended());
        assert!(
            woken.load(Ordering::SeqCst),
            "the driver's end wakes the future that found it busy"
        );
    }

    #[tokio::test]
    async fn pause_before_the_first_invocation_holds_it() {
        let counter = Arc::new(AtomicU32::new(0));
        let mut run = LocalRunner::new().run(counting(&counter, cool_down), ());
        run.pause().await.expect("paused");
        assert_run_waits(&mut run).await;
        assert_eq!(counter.load(Ordering::SeqCst), 0, "no invocation started");
        run.resume().await.expect("resumed");
        let result = run.await.expect("runner");
        assert_eq!(result.output().map(String::as_str), Some("done"));
        assert_eq!(counter.load(Ordering::SeqCst), 2);
    }

    /// Arms the commit seam of `run`: the next runner future that commits an
    /// outcome finds this run failure recorded just before its commit, as a
    /// concurrent failure of the run would record it.
    fn fail_at_next_commit<O>(run: &RunHandle<O>) {
        run.shared.live.lock().fail_at_commit = Some(TestRunnerError::NoExecutionArn);
    }

    /// Asserts that `outcome` is the error [`fail_at_next_commit`] records.
    #[track_caller]
    fn assert_failure_won<T: std::fmt::Debug>(outcome: Result<T, TestRunnerError>) {
        let err = outcome.expect_err("the run failure wins");
        assert!(matches!(err, TestRunnerError::NoExecutionArn), "{err:?}");
    }

    /// A `LocalRunner` run of [`await_callback_c`], paused while `c` waits
    /// for its reply, so nothing moves until the test acts.
    async fn paused_callback_run() -> (RunHandle<u32>, OperationHandle) {
        let run = LocalRunner::new().run(await_callback_c, ());
        let c = run.operation("c");
        c.wait_for_data(WaitingStatus::Started)
            .await
            .expect("started");
        run.pause().await.expect("paused");
        (run, c)
    }

    /// A run failure recorded after a `wait_for_data` that started while the
    /// run was active became ready, and before it returned, is what it
    /// returns, under `LocalRunner` and under `CloudRunner`. A wait first
    /// polled after that failure returns the reached data.
    #[tokio::test(start_paused = true)]
    async fn a_run_failure_before_wait_commit_beats_ready_data() {
        let (run, c) = paused_callback_run().await;
        let mut waiter = std::pin::pin!(c.wait_for_data(WaitingStatus::Completed));
        let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
        assert!(waiter.as_mut().poll(&mut cx).is_pending());
        c.send_callback(CallbackReply::success(&1_u32).expect("encodes"))
            .await
            .expect("pending");
        assert!(
            run.shared
                .live
                .lock()
                .controls
                .values()
                .any(|control| matches!(control.phase, ControlPhase::Ready(_))),
            "the reply made the waiter ready"
        );
        fail_at_next_commit(&run);
        let poll = waiter.as_mut().poll(&mut cx);
        assert!(
            matches!(
                poll,
                std::task::Poll::Ready(Err(TestRunnerError::NoExecutionArn))
            ),
            "a ready waiter returns the run failure: {poll:?}"
        );
        let op = c
            .wait_for_data(WaitingStatus::Completed)
            .await
            .expect("a later wait returns the reached data");
        assert_eq!(op.status(), OperationStatus::Succeeded);

        let api = fake_with_history(callback_started_history());
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        let approval = run.operation("approval");
        fail_at_next_commit(&run);
        assert_failure_won(approval.wait_for_data(WaitingStatus::Started).await);
        assert_eq!(history_fetches(&api.calls()), 1);
        let op = approval
            .wait_for_data(WaitingStatus::Started)
            .await
            .expect("a later wait returns the last fold");
        assert_eq!(op.id(), "c");
        assert_eq!(history_fetches(&api.calls()), 1, "no further read");
    }

    /// A run failure recorded just before the commit also wins over every
    /// other outcome of a `LocalRunner` control that started while the run
    /// was active: a reached status, a not-reached status, a reply, a
    /// refused reply, a heartbeat, `pause` and `resume`. A control whose
    /// outcome loses changes nothing.
    #[tokio::test]
    async fn a_terminal_error_before_commit_beats_every_local_control() {
        let (run, c) = paused_callback_run().await;
        fail_at_next_commit(&run);
        assert_failure_won(c.wait_for_data(WaitingStatus::Started).await);

        let (run, c) = paused_callback_run().await;
        fail_at_next_commit(&run);
        assert_failure_won(
            c.send_callback(CallbackReply::success(&1_u32).expect("encodes"))
                .await,
        );
        assert_eq!(
            c.snapshot().map(|op| op.status()),
            Some(OperationStatus::Started),
            "the losing reply is not applied"
        );
        assert!(!run.shared.live.lock().local.invocation_due);

        let (run, _c) = paused_callback_run().await;
        fail_at_next_commit(&run);
        assert_failure_won(
            run.operation("ghost")
                .send_callback(CallbackReply::raw("1"))
                .await,
        );

        let (run, c) = paused_callback_run().await;
        fail_at_next_commit(&run);
        assert_failure_won(c.send_callback_heartbeat().await);

        let (run, _c) = paused_callback_run().await;
        fail_at_next_commit(&run);
        assert_failure_won(run.pause().await);

        let (run, _c) = paused_callback_run().await;
        fail_at_next_commit(&run);
        assert_failure_won(run.resume().await);
        assert!(
            run.shared.live.lock().local.paused,
            "the losing resume changes nothing"
        );

        let mut run = LocalRunner::new().run(steps_named(&["s"]), ());
        let ghost = run.operation("ghost");
        (&mut run).await.expect("runner");
        fail_at_next_commit(&run);
        assert_failure_won(ghost.wait_for_data(WaitingStatus::Started).await);
    }

    /// A `LocalRunner` `pause` that waits for its invocation to end returns
    /// a run failure recorded after that end and before it returned.
    #[test]
    fn a_terminal_error_before_commit_beats_a_ready_pause() {
        let live = new_live();
        let (shared, _polls) = counting_run(&live);
        let run = RunHandle::<u32>::from_shared(shared);
        pending_control(&live, ControlKind::Run);
        assert!(live.lock().admit_invocation());
        let mut pause = std::pin::pin!(run.pause());
        let mut cx = std::task::Context::from_waker(std::task::Waker::noop());
        assert!(pause.as_mut().poll(&mut cx).is_pending());
        drop(live.lock().end_invocation(false));
        fail_at_next_commit(&run);
        let poll = pause.as_mut().poll(&mut cx);
        assert!(
            matches!(
                poll,
                std::task::Poll::Ready(Err(TestRunnerError::NoExecutionArn))
            ),
            "a ready pause returns the run failure: {poll:?}"
        );
    }

    /// A `CloudRunner` callback control, a `RequestResponse` wait and
    /// `pause` commit nothing, so a concurrent run failure armed at the next
    /// commit changes none of their outcomes: a reply, a refused reply, a
    /// heartbeat and the `Unsupported` errors.
    #[tokio::test(start_paused = true)]
    async fn cloud_callbacks_return_their_own_outcome_past_a_run_failure() {
        let api = fake_with_history(callback_started_history());
        let run = event_runner().run_with::<_, u32>(Arc::clone(&api), Vec::new());
        fail_at_next_commit(&run);
        run.operation("approval")
            .send_callback(CallbackReply::raw("1"))
            .await
            .expect("the reply is sent");
        assert_eq!(
            api.calls().last(),
            Some(&FakeCall::CallbackSuccess("cb-1".to_owned(), b"1".to_vec()))
        );
        let err = run
            .operation("ghost")
            .send_callback(CallbackReply::raw("1"))
            .await
            .expect_err("no such operation");
        assert!(matches!(err, TestRunnerError::OperationNotFound), "{err:?}");
        run.operation("approval")
            .send_callback_heartbeat()
            .await
            .expect("the heartbeat is sent");
        assert_eq!(
            api.calls().last(),
            Some(&FakeCall::Heartbeat("cb-1".to_owned()))
        );
        assert_unsupported(
            &run.pause().await.expect_err("pause"),
            CLOUD_PAUSE_UNSUPPORTED,
        );
        assert!(
            run.shared.live.lock().fail_at_commit.is_some(),
            "not consumed"
        );
        assert!(run.shared.terminal_error().is_none());

        let api = fake_with_history(callback_started_history());
        let run = CloudRunner::new("f").run_with::<_, u32>(Arc::clone(&api), Vec::new());
        fail_at_next_commit(&run);
        assert_unsupported(
            &run.operation("approval")
                .wait_for_data(WaitingStatus::Started)
                .await
                .expect_err("RequestResponse"),
            WAIT_FOR_DATA_REQUEST_RESPONSE_UNSUPPORTED,
        );
        assert_eq!(api.calls(), Vec::<FakeCall>::new());
        assert!(
            run.shared.live.lock().fail_at_commit.is_some(),
            "not consumed"
        );
    }

    /// A handler that creates the callback `c` with a heartbeat timeout,
    /// then waits an hour, and never awaits the callback's result.
    async fn heartbeat_callback_then_wait((): (), ctx: DurableContext) -> Result<u32, BoxError> {
        ctx.create_callback::<u32>()
            .name("c")
            .heartbeat_timeout(std::time::Duration::from_secs(60))
            .await?;
        ctx.wait(std::time::Duration::from_secs(3600))
            .name("w")
            .await?;
        Ok(1)
    }

    /// A heartbeat to a callback that is still `Started` after the run
    /// decided to end is refused as a reply is: after a terminal envelope,
    /// and after the invocation budget ran out.
    #[tokio::test]
    async fn a_heartbeat_after_the_run_ended_is_refused() {
        for runner in [LocalRunner::new(), LocalRunner::new().max_invocations(1)] {
            let mut run = runner.run(heartbeat_callback_then_wait, ());
            let c = run.operation("c");
            let result = (&mut run).await.expect("runner");
            assert_eq!(
                c.snapshot().map(|op| op.status()),
                Some(OperationStatus::Started)
            );
            let err = c
                .send_callback_heartbeat()
                .await
                .expect_err("the run ended");
            assert!(
                matches!(err, TestRunnerError::CallbackNotPending),
                "{:?}: {err:?}",
                result.status()
            );
        }
    }

    /// The `CloudRunner` core of a run with no driver, for transition tests.
    fn cloud_core() -> (Arc<RunCore>, Arc<CloudShared<FakeApi>>) {
        let live = new_live();
        let cloud = Arc::new(CloudShared::<FakeApi>::new(
            &event_runner(),
            Arc::clone(&live),
        ));
        let core = RunCore::new(
            live,
            Engine::Cloud(Arc::clone(&cloud) as Arc<dyn CloudControl>),
            Box::pin(std::future::pending()),
        );
        (core, cloud)
    }

    /// The history event `event_type` of the step `s`.
    fn step_s_event(event_type: EventType) -> Event {
        Event::builder()
            .event_type(event_type)
            .id("s")
            .name("s")
            .event_id(1)
            .build()
    }

    /// A history read that finishes after a newer one does not replace the
    /// newer fold, and still returns its own operations to its caller.
    #[test]
    fn an_older_history_read_does_not_replace_a_newer_fold() {
        let (core, cloud) = cloud_core();
        let older = cloud.begin_request();
        let newer = cloud.begin_request();
        cloud
            .finish_history(
                None,
                newer,
                Ok(vec![step_s_event(EventType::StepSucceeded)]),
            )
            .expect("newer");
        let (_, own) = cloud
            .finish_history(None, older, Ok(vec![step_s_event(EventType::StepStarted)]))
            .expect("older");
        assert_eq!(
            own.first().map(|op| op.status),
            Some(OperationStatus::Started)
        );
        let fold = core.operations().expect("a fold");
        assert_eq!(
            fold.first().map(|op| op.status),
            Some(OperationStatus::Succeeded),
            "the newer fold stays"
        );
    }

    /// A failed callback call and a failed history read of two controls
    /// return their own errors and record no run failure. The failed read
    /// leaves the fold as it is, and later reads and calls still start.
    #[test]
    fn failed_cloud_calls_do_not_record_a_run_failure() {
        let (core, cloud) = cloud_core();
        let first = core.live.lock().add_control(ControlKind::Cloud);
        let second = core.live.lock().add_control(ControlKind::Cloud);
        let read = cloud.begin_request();
        cloud
            .finish_history(None, read, Ok(vec![step_s_event(EventType::StepStarted)]))
            .expect("a fold");
        let one = cloud.begin_request();
        let two = cloud.begin_request();
        let err = |text: &str| aws_error(Box::new(std::io::Error::other(text.to_owned())));
        let a = cloud
            .finish_call(first, one, Err(err("first")))
            .expect_err("failed");
        let b = cloud
            .finish_history(
                Some((second, ControlRole::Callback)),
                two,
                Err(err("second")),
            )
            .expect_err("failed");
        assert!(a.to_string().ends_with("first"), "{a}");
        assert!(b.to_string().ends_with("second"), "{b}");
        assert!(core.terminal_error().is_none(), "no run failure");
        let fold = core.operations().expect("the fold stays");
        assert_eq!(
            fold.first().map(|op| op.status),
            Some(OperationStatus::Started)
        );
        assert_eq!(core.live.lock().cloud.fold_request, read);
        for role in [ControlRole::Wait, ControlRole::Callback] {
            assert!(matches!(
                cloud.begin_history(role),
                Ok(HistoryStart::Read(ticket)) if ticket > two
            ));
        }
        assert!(cloud.begin_request() > two);
    }

    /// A pending wait whose Lambda call never answers is woken by a run
    /// failure, and returns it. A callback control is woken too, and keeps
    /// awaiting its call.
    #[test]
    fn a_failure_wakes_an_in_flight_call() {
        for role in [ControlRole::Wait, ControlRole::Callback] {
            let (core, cloud) = cloud_core();
            let registration = Registration::new(&core, ControlKind::Cloud);
            let request = cloud.begin_request();
            let woken = Arc::new(std::sync::atomic::AtomicBool::new(false));
            let waker = std::task::Waker::from(Arc::new(FlagWaker(Arc::clone(&woken))));
            let mut cx = std::task::Context::from_waker(&waker);
            let mut call = std::pin::pin!(await_in_flight(
                &registration,
                role,
                request,
                std::future::pending::<()>()
            ));
            assert!(call.as_mut().poll(&mut cx).is_pending());
            assert!(matches!(
                core.live
                    .lock()
                    .controls
                    .get(&registration.id)
                    .map(|control| &control.phase),
                Some(ControlPhase::InFlight { .. })
            ));
            let (_, wakers) = core
                .live
                .lock()
                .record_failure(TestRunnerError::NoExecutionArn);
            wake(wakers);
            assert!(woken.load(Ordering::SeqCst), "{role:?}");
            let poll = call.as_mut().poll(&mut cx);
            match role {
                ControlRole::Wait => assert!(matches!(
                    poll,
                    std::task::Poll::Ready(Err(TestRunnerError::NoExecutionArn))
                )),
                ControlRole::Callback => assert!(poll.is_pending(), "{poll:?}"),
            }
        }
    }

    /// A run failure recorded while the wait polls its Lambda call, which
    /// then stays pending, ends the wait on that same poll with the run
    /// failure, and the wait is not left in flight. A callback control
    /// stays in flight.
    #[test]
    fn a_failure_recorded_during_the_call_poll_ends_a_wait() {
        for role in [ControlRole::Wait, ControlRole::Callback] {
            let (core, cloud) = cloud_core();
            let registration = Registration::new(&core, ControlKind::Cloud);
            let request = cloud.begin_request();
            let call = {
                let core = Arc::clone(&core);
                std::future::poll_fn(move |_| {
                    let (_, wakers) = core
                        .live
                        .lock()
                        .record_failure(TestRunnerError::NoExecutionArn);
                    wake(wakers);
                    std::task::Poll::<()>::Pending
                })
            };
            let waker = std::task::Waker::noop();
            let mut cx = std::task::Context::from_waker(waker);
            let mut wait = std::pin::pin!(await_in_flight(&registration, role, request, call));
            let poll = wait.as_mut().poll(&mut cx);
            let in_flight = matches!(
                core.live
                    .lock()
                    .controls
                    .get(&registration.id)
                    .map(|control| &control.phase),
                Some(ControlPhase::InFlight { .. })
            );
            match role {
                ControlRole::Wait => {
                    assert!(
                        matches!(
                            poll,
                            std::task::Poll::Ready(Err(TestRunnerError::NoExecutionArn))
                        ),
                        "{poll:?}"
                    );
                    assert!(!in_flight, "the wait is not left in flight");
                }
                ControlRole::Callback => {
                    assert!(poll.is_pending(), "{poll:?}");
                    assert!(in_flight, "the callback stays in flight");
                }
            }
        }
    }

    /// A pending wait whose Lambda call becomes ready only after a run
    /// failure was recorded returns the run failure without taking the
    /// call's output. A wait's read finished after the failure stores no
    /// fold, while a callback control's read still becomes the fold.
    #[test]
    fn a_run_failure_drops_a_wait_call_that_became_ready() {
        let (core, cloud) = cloud_core();
        let registration = Registration::new(&core, ControlKind::Cloud);
        let request = cloud.begin_request();
        let ready = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let taken = Arc::new(std::sync::atomic::AtomicBool::new(false));
        let call = {
            let ready = Arc::clone(&ready);
            let taken = Arc::clone(&taken);
            std::future::poll_fn(move |_| {
                if ready.load(Ordering::SeqCst) {
                    taken.store(true, Ordering::SeqCst);
                    std::task::Poll::Ready(())
                } else {
                    std::task::Poll::Pending
                }
            })
        };
        let waker = std::task::Waker::noop();
        let mut cx = std::task::Context::from_waker(waker);
        let mut wait = std::pin::pin!(await_in_flight(
            &registration,
            ControlRole::Wait,
            request,
            call
        ));
        assert!(wait.as_mut().poll(&mut cx).is_pending());
        let (_, wakers) = core
            .live
            .lock()
            .record_failure(TestRunnerError::NoExecutionArn);
        wake(wakers);
        ready.store(true, Ordering::SeqCst);
        assert!(matches!(
            wait.as_mut().poll(&mut cx),
            std::task::Poll::Ready(Err(TestRunnerError::NoExecutionArn))
        ));
        assert!(!taken.load(Ordering::SeqCst), "the ready call is dropped");

        let err = cloud
            .finish_history(
                Some((registration.id, ControlRole::Wait)),
                request,
                Ok(vec![step_s_event(EventType::StepStarted)]),
            )
            .expect_err("the run failure wins");
        assert!(matches!(err, TestRunnerError::NoExecutionArn), "{err:?}");
        assert!(core.operations().is_none(), "the wait stores no fold");
        let read = cloud.begin_request();
        cloud
            .finish_history(
                Some((registration.id, ControlRole::Callback)),
                read,
                Ok(vec![step_s_event(EventType::StepStarted)]),
            )
            .expect("a callback read");
        assert!(core.operations().is_some(), "the callback read is the fold");
    }

    /// A history read starts only within the budget. A wait starts none
    /// after the run finished or failed, and returns the fold or the run
    /// failure. A callback control starts one after either, within the
    /// budget.
    #[test]
    fn a_history_read_starts_within_the_budget() {
        let (core, cloud) = cloud_core();
        core.live.lock().cloud.limit = Some(1);
        assert!(matches!(
            cloud.begin_history(ControlRole::Wait),
            Ok(HistoryStart::Read(_))
        ));
        assert!(matches!(
            cloud.begin_history(ControlRole::Callback),
            Ok(HistoryStart::Done(None))
        ));
        core.live.lock().cloud.limit = None;
        core.live.lock().end = RunEnd::Finished(None);
        assert!(matches!(
            cloud.begin_history(ControlRole::Wait),
            Ok(HistoryStart::Done(None))
        ));
        assert!(matches!(
            cloud.begin_history(ControlRole::Callback),
            Ok(HistoryStart::Read(_))
        ));

        let (core, cloud) = cloud_core();
        core.live.lock().cloud.limit = Some(2);
        core.live.lock().failure = Some(TestRunnerError::NoExecutionArn);
        assert!(matches!(
            cloud.begin_history(ControlRole::Wait),
            Err(TestRunnerError::NoExecutionArn)
        ));
        assert_eq!(core.live.lock().cloud.used, 0, "the wait spent nothing");
        assert!(matches!(
            cloud.begin_history(ControlRole::Callback),
            Ok(HistoryStart::Read(_))
        ));
        assert!(matches!(
            cloud.begin_history(ControlRole::Callback),
            Ok(HistoryStart::Read(_))
        ));
        assert!(matches!(
            cloud.begin_history(ControlRole::Callback),
            Ok(HistoryStart::Done(None))
        ));
    }

    // ---- Oversized handler results ----

    /// A handler whose serialized result is `n` bytes: `n - 2` letters and
    /// two quotes. It builds the string when it runs, so a run of one
    /// invocation builds it once.
    fn sized_handler(
        n: usize,
    ) -> impl Fn((), DurableContext) -> std::future::Ready<Result<String, BoxError>>
    + Send
    + Sync
    + 'static {
        move |(), _ctx: DurableContext| std::future::ready(Ok("a".repeat(n - 2)))
    }

    /// Asserts that `result` succeeded with an output of `n - 2` letters,
    /// comparing the lengths first so a failure does not print 6 MB.
    fn assert_full_output(result: &TestResult<String>, n: usize) {
        assert!(result.is_success(), "{:?}", result.error_message());
        let output = result.output().expect("an output");
        assert_eq!(output.len(), n - 2);
        assert!(
            output.bytes().all(|b| b == b'a'),
            "the output is the result"
        );
    }

    /// A result up to the limit is inline; above it, up to the backend's
    /// maximum, it is checkpointed and read back; above the maximum the
    /// service rejects it. No run lists an `EXECUTION` operation.
    #[tokio::test]
    async fn oversized_result_round_trips() {
        for (n, checkpointed) in [(6_291_406, false), (6_291_407, true), (6_291_456, true)] {
            let backend = Arc::new(Backend::new(QueuedReplies::default(), None));
            let result: TestResult<String> = LocalRunner::new()
                .run_on_backend(Arc::clone(&backend), sized_handler(n), "null".to_owned())
                .await
                .expect("runner");
            assert_full_output(&result, n);
            assert_eq!(
                backend.execution_result().map(|stored| stored.len()),
                checkpointed.then_some(n),
                "n = {n}"
            );
            assert!(
                result
                    .operations()
                    .iter()
                    .all(|op| op.kind() != OperationKind::Execution),
                "n = {n}"
            );
        }

        let n = 6_291_457;
        let backend = Arc::new(Backend::new(QueuedReplies::default(), None));
        let result: TestResult<String> = LocalRunner::new()
            .run_on_backend(Arc::clone(&backend), sized_handler(n), "null".to_owned())
            .await
            .expect("runner");
        assert!(result.is_failure());
        assert!(result.output().is_none());
        assert_eq!(
            result.error_type(),
            Some(crate::error::CHECKPOINT_EXECUTION_FAILED_ERROR_TYPE)
        );
        let message = result.error_message().unwrap_or_default();
        assert!(
            message.starts_with("[Unrecoverable Execution] Checkpoint failed: "),
            "{message}"
        );
        assert!(
            message.contains(
                "Execution output payload size must be less than or equal to 6291456 bytes."
            ),
            "{message}"
        );
        assert_eq!(backend.execution_result(), None);
        assert!(
            result
                .operations()
                .iter()
                .all(|op| op.kind() != OperationKind::Execution)
        );
    }

    /// The backend accepts the `EXECUTION` update and the answer is lost:
    /// the invocation fails, but the run ends `SUCCEEDED` with the full
    /// output after one invocation. The end hook saw
    /// `Retrying`.
    #[tokio::test]
    async fn oversized_result_accepted_then_error_ends_execution() {
        let n = 6_291_407;
        let plugin = crate::plugin::test_support::Hooks::default();
        let runner = LocalRunner::new().options(
            crate::Options::builder()
                .plugin(plugin.clone())
                .build()
                .expect("valid options"),
        );
        let backend = Arc::new(Backend::new(QueuedReplies::default(), None));
        backend.enqueue_response(crate::client::TestResponse::AcceptThenRetryableError(
            "lost response".into(),
        ));
        let result: TestResult<String> = runner
            .run_on_backend(Arc::clone(&backend), sized_handler(n), "null".to_owned())
            .await
            .expect("runner");
        assert_full_output(&result, n);
        assert_eq!(result.invocation_count(), 1);
        let ends = plugin.ends();
        let [end] = ends.as_slice() else {
            unreachable!("one end info, got {}", ends.len());
        };
        assert_eq!(end.status(), crate::InvocationStatus::Retrying);
        assert_eq!(
            end.execution_error().and_then(crate::WireError::error_type),
            Some(crate::error::CHECKPOINT_INVOCATION_FAILED_ERROR_TYPE)
        );
    }

    /// A handler that creates the callback `c`, with a heartbeat timeout,
    /// and then returns a result one byte over the response limit, without
    /// awaiting the callback.
    async fn callback_then_oversized((): (), ctx: DurableContext) -> Result<String, BoxError> {
        ctx.create_callback::<u32>()
            .name("c")
            .heartbeat_timeout(std::time::Duration::from_secs(60))
            .await?;
        Ok("a".repeat(crate::LAMBDA_RESPONSE_SIZE_LIMIT - 1))
    }

    /// The backend accepts the `EXECUTION` update of a live run and the
    /// answer is lost: the run ends `SUCCEEDED` and is closed, so a reply
    /// and a heartbeat to the callback still `Started` are refused.
    #[tokio::test]
    async fn oversized_result_accepted_then_error_closes_the_run() {
        let n = crate::LAMBDA_RESPONSE_SIZE_LIMIT + 1;
        let mut run = LocalRunner::new().run_configured(callback_then_oversized, (), |backend| {
            // The callback's `START` passes; the `EXECUTION` write is
            // accepted and its answer lost.
            backend.plan_checkpoint_calls(vec![PlannedCheckpoint::Pass]);
            backend.enqueue_response(crate::client::TestResponse::AcceptThenRetryableError(
                "lost response".into(),
            ));
        });
        let c = run.operation("c");
        let result = (&mut run).await.expect("runner");
        assert_full_output(&result, n);
        assert_eq!(result.invocation_count(), 1);
        assert_eq!(
            c.snapshot().map(|op| op.status()),
            Some(OperationStatus::Started)
        );
        let err = c
            .send_callback(CallbackReply::success(&1_u32).expect("encodes"))
            .await
            .expect_err("the run ended");
        assert!(
            matches!(err, TestRunnerError::CallbackNotPending),
            "{err:?}"
        );
        let err = c
            .send_callback_heartbeat()
            .await
            .expect_err("the run ended");
        assert!(
            matches!(err, TestRunnerError::CallbackNotPending),
            "{err:?}"
        );
        assert_eq!(
            c.snapshot().map(|op| op.status()),
            Some(OperationStatus::Started),
            "the refused reply changed nothing"
        );
    }

    /// An envelope with an empty `Result` and no checkpointed result reads
    /// the empty string, which fails to decode as the output.
    #[tokio::test]
    async fn empty_result_without_checkpoint_fails_to_decode() {
        let runner = LocalRunner::new();
        let service = |_event: lambda_runtime::LambdaEvent<crate::InvocationPayload>| {
            let response: crate::BoxedInvocationFuture = Box::pin(async {
                Ok(crate::InvocationResponse(
                    serde_json::json!({ "Status": "SUCCEEDED", "Result": "" }),
                ))
            });
            response
        };
        let err = runner
            .drive_service(Arc::new(runner.new_backend()), service, "null".to_owned())
            .await
            .expect("a run")
            .into_test_result::<serde_json::Value>()
            .expect_err("the empty string is not JSON");
        assert!(
            matches!(err, TestRunnerError::OutputDeserialization(_)),
            "{err:?}"
        );
    }

    /// The backend answers only `AcceptThenRetryableError` from a queued
    /// `TestResponse`.
    #[test]
    #[should_panic(expected = "answers only AcceptThenRetryableError")]
    fn backend_refuses_other_queued_responses() {
        let backend = Backend::new(QueuedReplies::default(), None);
        backend.enqueue_response(crate::client::TestResponse::RetryableError("x".into()));
    }
}