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
//! AWS Durable Execution SDK for Rust.
//!
//! This crate provides a Rust implementation of the AWS Lambda Durable
//! Functions SDK, enabling long-running orchestrations that survive Lambda
//! invocation timeouts through automatic checkpointing and deterministic
//! replay.
//!
//! # Overview
//!
//! A durable function is a Lambda function whose progress is automatically
//! checkpointed. If the function is interrupted, it restarts and replays
//! recorded results instead of re-executing operations. The SDK guarantees
//! deterministic replay as long as operations are created in a consistent
//! order across invocations.
//!
//! # Quick start
//!
//! ```no_run
//! use aws_durable_execution_sdk as durable;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Deserialize, Serialize)]
//! struct Order { id: String }
//!
//! #[tokio::main]
//! async fn main() -> Result<(), lambda_runtime::Error> {
//!     durable::run(|event: Order, ctx: durable::DurableContext| async move {
//!         let result = ctx.step(|_step_ctx| async move {
//!             Ok(format!("processed {}", event.id))
//!         }).name("process")
//!           .await?;
//!         Ok(result)
//!     }).await
//! }
//! ```
//!
//! # Determinism contract
//!
//! Each durable operation has an **operation ID**: the identity that ties a
//! call site to its recorded result. The SDK mints IDs synchronously at the
//! call site, in the order the handler creates operations, and replay hands
//! each recorded result to the operation that claims the same ID. Two kinds
//! of operation are exceptions. A [`DurableContext::with_retry`] builder
//! claims no ID at the call: the block claims its ID when the builder
//! becomes its future (`.await`, `.future()` or `.spawn()`). A
//! combinator ([`DurableContext::try_join_all`],
//! [`DurableContext::join_all`], [`DurableContext::select_ok`],
//! [`DurableContext::race`]) claims its ID when it is
//! first awaited or polled, or when it is spawned. That point is in the
//! handler's own control flow, so it is deterministic as long as the handler
//! awaits the combinator where it awaited it on the first run. A combinator
//! that is dropped before that point claims no ID and records nothing. A
//! combinator passed to another combinator must already have claimed its ID:
//! start the inner combinator with `.spawn()` and pass the returned handle.
//! A still-unclaimed one fails the execution with a context usage error. A
//! `map`,
//! `parallel` or `parallel_mixed` claims one ID from its caller, its own,
//! and numbers its items inside the batch: item `i` gets the ID of item `i`
//! in the batch's namespace, however the items are scheduled. Replay is
//! therefore correct only if the handler creates the same operations in the
//! same order on every invocation:
//!
//! 1. Keep the control flow that decides which operations to create a pure
//!    function of the event and of results the SDK already recorded.
//! 2. Never create durable operations while iterating a `HashMap` or
//!    `HashSet`; their iteration order changes between runs. Sort into a
//!    `Vec` first, or use a collection with deterministic iteration order
//!    such as `BTreeMap`, `BTreeSet`, or the `indexmap` crate's `IndexMap`.
//! 3. Do not use `tokio::select!` over durable futures; use
//!    [`DurableContext::race`] or [`DurableContext::select_ok`], which
//!    record which branch won.
//! 4. Do not use `tokio::spawn` to run a durable operation in the
//!    background; use `.spawn()` on the operation's builder, which keeps
//!    the spawned work inside the execution's replay accounting. Dropping
//!    the handle `.spawn()` returns loses only the result: the operation
//!    keeps running until it settles, until the scope it was started in
//!    closes, or until the invocation ends (see [`DurableFuture`]). An
//!    operation claimed, spawned or awaited on a task or thread that does
//!    not own the context, plugin hook work included, fails the execution
//!    with a context usage error. No operation update whose checkpoint
//!    admission is requested after the error is recorded is written, so an
//!    operation whose next update is refused keeps the last state written,
//!    which can be `STARTED`.
//! 5. On suspension, the user future is dropped, so do not rely on `Drop`
//!    ordering for correctness between durable operations. For example, a
//!    guard that unlocks a resource in `Drop` runs the moment the execution
//!    suspends on a `ctx.wait(...)`, not after the wait completes; release
//!    resources in a step instead of a destructor.
//!
//! # Observability
//!
//! The SDK instruments the operation lifecycle through the [`tracing`]
//! facade: spans around the handler, each child body, each live step body
//! and each `wait_for_condition` check, and `DEBUG`
//! events at operation start/finish/replay/retry and execution
//! start/resume/suspend. The span names, event names, and field names are a
//! documented, stable contract, including how to bridge it to
//! OpenTelemetry, in the [`observability`] module.
//!
//! # Instrumentation plugins
//!
//! A plugin observes each invocation through hooks for the invocation, each
//! operation and each user function; register its factory with
//! [`Options::builder().plugin(..)`](OptionsBuilder::plugin). The [`plugin`]
//! module documents the hooks and the order in which they run.

// Test code asserts with `unwrap`/`expect` pervasively; hoisting the
// override here keeps the panic-policy lints (deny in workspace lints)
// focused on production code without repeating the allow at every test
// module.
#![cfg_attr(test, allow(clippy::unwrap_used))]
#![cfg_attr(test, allow(clippy::expect_used))]

// Compiles the README's Rust examples as doctests, so a README example that
// stops compiling fails `cargo test` instead of rotting silently.
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
mod readme_doctests {}

pub mod builders;
pub(crate) mod callback;
pub(crate) mod checkpoint_coalescer;
pub(crate) mod child;
pub(crate) mod client;
pub(crate) mod combinator;
mod context;
pub(crate) mod coordinator;
pub(crate) mod driver;
mod engine;
mod error;
mod future;
pub(crate) mod invoke;
pub(crate) mod map_parallel; // public types re-exported from crate::builders::map_parallel
pub mod observability;
mod operation;
mod operation_sub_type;
mod options;
pub mod plugin;
pub mod serdes;
pub(crate) mod start;
pub(crate) mod step;
mod task;
#[cfg(feature = "test-util")]
pub mod test_util;
pub(crate) mod tracing_layer;
pub(crate) mod wait;
pub(crate) mod wait_for_condition;
pub(crate) mod with_retry;

pub use self::tracing_layer::ReplayFilterLayer;

pub use self::client::{
    CheckpointOutput, ExecutionClient, ExecutionClientError, ExecutionClientErrorScope,
    GetStateOutput, LambdaExecutionClient,
};
pub use self::context::{DurableContext, StepContext};
pub use self::error::{
    CallbackError, CallbackErrorKind, ChildContextError, ChildContextErrorKind, CombinatorError,
    CombinatorErrorKind, InvokeError, InvokeErrorKind, JoinFailed, MaxChecksExceeded,
    NonDeterministicExecutionError, NonDeterministicExecutionErrorKind, OperationError,
    OperationErrorKind, OperationMismatch, ReplayedFailure, StepError, StepErrorKind, TypedError,
    UnexpectedStatus, UnrecognizedStatus, WaitError, WaitErrorKind, WaitForConditionError,
    WaitForConditionErrorKind, WireError,
};
pub use self::future::{Branch, BranchHandle, BranchKey, BranchSet, BranchValue, DurableFuture};
pub use self::operation::{InvocationStatus, OperationKind, OperationStatus};
pub use self::operation_sub_type::{InvalidOperationSubType, OperationSubType};
pub use self::options::{Options, OptionsBuilder, OptionsValidationError};
pub use self::serdes::{JsonSerdes, PassthroughSerdes, PayloadSerializer, Serdes};
pub use self::step::StepSemantics;

// Re-export rule: every foreign type in the public surface is re-exported.
pub use aws_config::SdkConfig;
pub use aws_sdk_lambda::Client as LambdaClient;
pub use lambda_runtime::{self, Context as LambdaContext};

/// The service model types of the checkpoint transport.
///
/// [`ExecutionClient`] speaks the service model types of `aws-sdk-lambda`
/// on purpose: the SDK already depends on that crate, and a transport for
/// another backend builds the same records the service returns. This
/// module re-exports exactly the types that [`Operation`] and
/// [`OperationUpdate`] name in their fields, directly or nested, and
/// [`DateTime`], the type of their timestamp fields.
///
/// With the `test-util` feature, the module also re-exports `Event`, the
/// service's history event that `test_util::TestResult::history_events`
/// returns, and every type that `Event` names in its fields, directly or
/// nested.
///
/// These types are those of the `aws-sdk-lambda` release this crate
/// depends on. They change when the crate moves to a new `aws-sdk-lambda`
/// release.
///
/// [`Operation`]: lambda_types::Operation
/// [`OperationUpdate`]: lambda_types::OperationUpdate
/// [`DateTime`]: lambda_types::DateTime
pub mod lambda_types {
    pub use aws_sdk_lambda::primitives::DateTime;
    pub use aws_sdk_lambda::types::{
        CallbackDetails, CallbackOptions, ChainedInvokeDetails, ChainedInvokeOptions,
        ContextDetails, ContextOptions, ErrorObject, ExecutionDetails, Operation, OperationAction,
        OperationStatus, OperationType, OperationUpdate, StepDetails, StepOptions, WaitDetails,
        WaitOptions,
    };
    #[cfg(feature = "test-util")]
    pub use aws_sdk_lambda::types::{
        CallbackFailedDetails, CallbackStartedDetails, CallbackSucceededDetails,
        CallbackTimedOutDetails, ChainedInvokeFailedDetails, ChainedInvokeStartedDetails,
        ChainedInvokeStoppedDetails, ChainedInvokeSucceededDetails, ChainedInvokeTimedOutDetails,
        ContextFailedDetails, ContextStartedDetails, ContextSucceededDetails, Event, EventError,
        EventInput, EventResult, EventType, ExecutionFailedDetails, ExecutionStartedDetails,
        ExecutionStoppedDetails, ExecutionSucceededDetails, ExecutionTimedOutDetails,
        InvocationCompletedDetails, RetryDetails, StepFailedDetails, StepStartedDetails,
        StepSucceededDetails, WaitCancelledDetails, WaitStartedDetails, WaitSucceededDetails,
    };
}

use serde::{Deserialize, Serialize};
use std::future::Future;
use std::time::Duration;

/// Boxed error type: `Box<dyn std::error::Error + Send + Sync>`.
///
/// This is the error type handler and step closures return.
///
/// # Examples
///
/// ```no_run
/// use aws_durable_execution_sdk as durable;
///
/// async fn handler(
///     _event: serde_json::Value,
///     ctx: durable::DurableContext,
/// ) -> Result<String, durable::BoxError> {
///     let value = ctx.step(|_| async { Ok(42) }).await?;
///     Ok(format!("got {value}"))
/// }
/// ```
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;

/// Installs the default Lambda subscriber with replayed records suppressed.
///
/// The level, the `RUST_LOG` directives and the choice of format follow
/// `lambda_runtime::tracing::init_default_subscriber`, and the `fmt` layer
/// is filtered by [`ReplayFilterLayer`]. So a record between operations is
/// written once per execution, not again on every replay. The level and
/// the `RUST_LOG` directives decide which records are written. The SDK's
/// replay-tracking spans stay enabled whatever they say, so suppression
/// also holds for a `WARN` or `ERROR` threshold.
///
/// The level comes from `AWS_LAMBDA_LOG_LEVEL`, else `RUST_LOG`, else
/// `INFO`. A level that does not parse falls back to `INFO`. The subscriber
/// writes JSON when `AWS_LAMBDA_LOG_FORMAT` is `json` in any case, and text
/// without the time otherwise. It writes to stdout: this function is
/// [`init_default_subscriber_with_writer`]`(std::io::stdout)`.
///
/// The JSON record is the SDK's own, not `lambda_runtime`'s. Each record is
/// one JSON object on one line, with its keys at the top level in the order
/// the JavaScript SDK writes them: `requestId`, `timestamp`, `level`,
/// `executionArn`, `tenantId`, `operationId`, `operationName`, `attempt`,
/// the record's other fields, then `message`. A correlation key comes from
/// the event, else from the nearest SDK span that carries it, and a key
/// without a value is left out. `timestamp` is the UTC time as
/// `YYYY-MM-DDTHH:MM:SS.sssZ`, `level` is the record's level, and `message`
/// is the event's message, or `""`. `isReplay` is not written. See
/// [`observability`] for the record of a step body.
///
/// The SDK never installs a subscriber implicitly. [`run`], [`wrap`] and
/// `LocalRunner` install none; only a call to this function or to
/// [`init_default_subscriber_with_writer`] does.
///
/// # Errors
///
/// Returns the `try_init` error when a global subscriber is already
/// installed, for example by an earlier call.
///
/// # Example
///
/// ```no_run
/// use aws_durable_execution_sdk as durable;
///
/// # async fn handler(
/// #     _event: serde_json::Value,
/// #     _ctx: durable::DurableContext,
/// # ) -> Result<(), durable::BoxError> {
/// #     Ok(())
/// # }
/// #[tokio::main]
/// async fn main() -> Result<(), lambda_runtime::Error> {
///     durable::init_default_subscriber()?;
///     durable::run(handler).await
/// }
/// ```
pub fn init_default_subscriber() -> Result<(), BoxError> {
    init_default_subscriber_with_writer(std::io::stdout)
}

/// Installs the default Lambda subscriber with replayed records suppressed,
/// writing to `writer`.
///
/// This mirrors `lambda_runtime::tracing::init_default_subscriber_with_writer`,
/// whose documentation recommends a non-blocking writer for high-throughput
/// functions, because a write to stdout can briefly block the runtime. It
/// reads the level, the directives and the format exactly as
/// [`init_default_subscriber`] does, writes the same JSON record, and its
/// `fmt` layer is filtered by [`ReplayFilterLayer`].
///
/// The SDK never installs a subscriber implicitly. [`run`], [`wrap`] and
/// `LocalRunner` install none; only a call to this function or to
/// [`init_default_subscriber`] does.
///
/// # Errors
///
/// Returns the `try_init` error when a global subscriber is already
/// installed, for example by an earlier call.
pub fn init_default_subscriber_with_writer<W>(writer: W) -> Result<(), BoxError>
where
    W: for<'w> lambda_runtime::tracing::subscriber::fmt::MakeWriter<'w> + Send + Sync + 'static,
{
    use lambda_runtime::tracing::subscriber::util::SubscriberInitExt as _;

    let settings = tracing_layer::LogSettings::from_env();
    tracing_layer::default_subscriber(
        &settings.level,
        &settings.directives,
        &settings.format,
        writer,
    )
    .try_init()?;
    Ok(())
}

/// Retry decision returned by a retry-strategy closure.
///
/// Tells the engine whether to retry a failed step and, if so, how long
/// to wait before the next attempt. Retry strategies are installed with
/// [`StepBuilder::retry_strategy`](crate::builders::StepBuilder::retry_strategy) and
/// [`WaitForCallbackBuilder::submitter_retry`](crate::builders::WaitForCallbackBuilder::submitter_retry).
///
/// # Examples
///
/// ```
/// use aws_durable_execution_sdk::RetryDecision;
/// use std::time::Duration;
///
/// let retry = RetryDecision::retry(Duration::from_secs(1));
/// let stop = RetryDecision::Stop;
/// # drop(retry);
/// # drop(stop);
/// ```
///
/// The enum and its `Retry` variant are `#[non_exhaustive]`. Build a retry
/// with [`RetryDecision::retry`], and match it with `..`, as in
/// `RetryDecision::Retry { delay, .. }`. A caller cannot build the variant
/// with struct syntax:
///
/// ```rust,compile_fail
/// use std::time::Duration;
/// use aws_durable_execution_sdk::RetryDecision;
///
/// let _ = RetryDecision::Retry { delay: Duration::from_secs(1) };
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RetryDecision {
    /// Retry after the specified delay.
    #[non_exhaustive]
    Retry {
        /// Duration to wait before retrying.
        delay: Duration,
    },
    /// Do not retry; propagate the error.
    Stop,
}

impl RetryDecision {
    /// A [`Retry`](Self::Retry) decision that waits `delay` before the next
    /// attempt.
    #[must_use]
    pub fn retry(delay: Duration) -> Self {
        Self::Retry { delay }
    }
}

/// A boxed retry strategy that decides whether to retry a failed step.
///
/// Receives the step error and the attempt number (starting from 1), and
/// returns a [`RetryDecision`].
///
/// Crate-internal: the boxing is an implementation detail. Public setters
/// ([`StepBuilder::retry_strategy`], [`WaitForCallbackBuilder::submitter_retry`])
/// take a generic closure and box it here.
pub(crate) type RetryStrategy = Box<dyn Fn(&StepError, u32) -> RetryDecision + Send + Sync>;

/// Starts the durable function runtime with the given handler.
///
/// This is the primary entry point. It configures the Lambda runtime with
/// durable execution support using default [`Options`], then runs the
/// handler for each invocation. Equivalent to calling [`run_with_options`]
/// with [`Options::default`].
///
/// To customize the runtime, keep this entry point's shape but swap the
/// function: for custom [`Options`] — a preconfigured Lambda client, your
/// own [`SdkConfig`], or checkpoint tuning — use [`run_with_options`]; to
/// add middleware around the service function itself, use [`wrap`], which
/// returns the service function without starting the runtime. Custom
/// serialization is configured per operation, not through [`Options`]:
/// pass a [`Serdes`] to an operation builder's `.serdes(...)`, sharing one
/// implementation across operations behind an `Arc<S>` (which itself
/// implements the trait) when they should serialize alike.
///
/// The handler closure is called once per invocation. It receives the
/// deserialized event and a [`DurableContext`] for performing durable
/// operations. Per invocation, the runtime parses the durable envelope into
/// a checkpoint log, constructs a [`DurableContext`] seeded with that log,
/// and drives the handler closure so that completed operations replay from
/// the log instead of re-executing.
///
/// # How handler failures are reported
///
/// When the handler returns `Err`, the runtime reports the failure *inside a
/// successful Lambda invocation response*, as a `FAILED` status envelope
/// that the durable execution service reads. The invocation itself does not
/// error. This is required by the durable service protocol, and it inverts
/// the usual Lambda observability signals:
///
/// - the `CloudWatch` `Errors` metric for the function does not fire,
/// - dead-letter queues and `OnFailure` destinations do not trigger,
/// - X-Ray does not mark the trace as an error.
///
/// Handler failures surface through the durable execution status instead:
/// poll `GetDurableExecution` for a `FAILED` status, or alarm on the
/// durable-execution metrics (for example, executions that reach a failed
/// terminal state) rather than on Lambda invocation errors.
///
/// # Oversized results
///
/// A serialized result over 6 MB minus 50 bytes (6 291 406 bytes) does not
/// fit the Lambda response. The SDK checkpoints it as the execution result,
/// and the envelope carries an empty `Result`, as in JS.
///
/// # Errors
///
/// Returns an error if the Lambda runtime fails to start or encounters an
/// unrecoverable error during execution.
///
/// # Examples
///
/// ```no_run
/// use aws_durable_execution_sdk as durable;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct MyEvent { name: String }
///
/// #[tokio::main]
/// async fn main() -> Result<(), lambda_runtime::Error> {
///     durable::run(|event: MyEvent, ctx: durable::DurableContext| async move {
///         Ok(format!("Hello, {}!", event.name))
///     }).await
/// }
/// ```
pub async fn run<F, E, Fut, O>(handler: F) -> Result<(), lambda_runtime::Error>
where
    F: Fn(E, DurableContext) -> Fut + Send + Sync + 'static,
    E: for<'de> Deserialize<'de> + Send + 'static,
    Fut: Future<Output = Result<O, BoxError>> + Send,
    O: Serialize + Send + 'static,
{
    run_with_options(handler, Options::default()).await
}

/// Starts the durable function runtime with the given handler and options.
///
/// Like [`run`], but applies the supplied [`Options`], for example a
/// preconfigured Lambda client or your own [`SdkConfig`], to every
/// invocation. Equivalent to registering [`wrap`] with the Lambda
/// runtime yourself:
/// `lambda_runtime::run(lambda_runtime::service_fn(wrap(handler, options)))`.
/// Serialization is not an [`Options`] concern: set a [`Serdes`] per
/// operation via the builders' `.serdes(...)`, sharing an `Arc<S>` across
/// operations that should serialize alike.
///
/// The handler closure is called once per invocation. It receives the
/// deserialized event and a [`DurableContext`] for performing durable
/// operations.
///
/// Handler failures are reported the same way as [`run`]: inside a
/// successful Lambda invocation response, surfacing through the durable
/// execution status (`GetDurableExecution`) rather than as Lambda
/// invocation errors. See [`run`] for the observability implications.
///
/// # Errors
///
/// Returns an error if the Lambda runtime fails to start or encounters an
/// unrecoverable error during execution.
///
/// # Examples
///
/// ```no_run
/// use aws_durable_execution_sdk as durable;
/// use serde::Deserialize;
///
/// #[derive(Deserialize)]
/// struct MyEvent { name: String }
///
/// #[tokio::main]
/// async fn main() -> Result<(), lambda_runtime::Error> {
///     let options = durable::Options::default();
///     durable::run_with_options(
///         |event: MyEvent, ctx: durable::DurableContext| async move {
///             Ok(format!("Hello, {}!", event.name))
///         },
///         options,
///     ).await
/// }
/// ```
pub async fn run_with_options<F, E, Fut, O>(
    handler: F,
    options: Options,
) -> Result<(), lambda_runtime::Error>
where
    F: Fn(E, DurableContext) -> Fut + Send + Sync + 'static,
    E: for<'de> Deserialize<'de> + Send + 'static,
    Fut: Future<Output = Result<O, BoxError>> + Send,
    O: Serialize + Send + 'static,
{
    lambda_runtime::run(lambda_runtime::service_fn(wrap(handler, options))).await
}

/// Parsed and validated durable invocation envelope fields.
#[derive(Debug)]
struct InvocationEnvelope {
    execution_arn: String,
    checkpoint_token: String,
    /// The top-level `UpdatedOperationIds`: the operations the service
    /// changed since the previous invocation. Non-string entries are
    /// skipped; an absent field or a value that is not an array is empty.
    updated_operation_ids: Vec<String>,
}

/// Returns `true` when the payload looks like a durable invocation envelope
/// (contains at least one of the expected top-level keys). Used to distinguish
/// "the service sent an envelope but something is wrong" (an error naming the
/// bad field) from "this payload has no envelope shape at all" (rejected at
/// the entry points with a message describing the expected envelope).
fn has_envelope_shape(payload: &serde_json::Value) -> bool {
    payload.get("DurableExecutionArn").is_some()
        || payload.get("CheckpointToken").is_some()
        || payload.get("InitialExecutionState").is_some()
}

/// Parses and validates the durable invocation envelope.
///
/// When the envelope shape is present (any of the expected top-level keys
/// exist), this function requires `DurableExecutionArn` and
/// `CheckpointToken` to be present and to be strings. A missing or
/// mistyped field is an immediate error naming the field, rather than
/// silently defaulting to an empty string.
///
/// When the envelope shape is absent (none of the expected keys), returns
/// `None`: callers decide whether that's acceptable.
fn parse_envelope(
    payload: &serde_json::Value,
) -> Result<Option<InvocationEnvelope>, lambda_runtime::Error> {
    if !has_envelope_shape(payload) {
        return Ok(None);
    }

    let execution_arn = match payload.get("DurableExecutionArn") {
        Some(v) => v
            .as_str()
            .ok_or_else(|| {
                lambda_runtime::Error::from(
                    "malformed invocation envelope: \"DurableExecutionArn\" is present but is not \
                     a string"
                        .to_owned(),
                )
            })?
            .to_owned(),
        None => {
            return Err(lambda_runtime::Error::from(
                "malformed invocation envelope: required field \"DurableExecutionArn\" is missing"
                    .to_owned(),
            ));
        }
    };

    let checkpoint_token = match payload.get("CheckpointToken") {
        Some(v) => v
            .as_str()
            .ok_or_else(|| {
                lambda_runtime::Error::from(
                    "malformed invocation envelope: \"CheckpointToken\" is present but is not a \
                     string"
                        .to_owned(),
                )
            })?
            .to_owned(),
        None => {
            return Err(lambda_runtime::Error::from(
                "malformed invocation envelope: required field \"CheckpointToken\" is missing"
                    .to_owned(),
            ));
        }
    };

    let updated_operation_ids = payload
        .get("UpdatedOperationIds")
        .and_then(serde_json::Value::as_array)
        .map(|ids| {
            ids.iter()
                .filter_map(serde_json::Value::as_str)
                .map(str::to_owned)
                .collect()
        })
        .unwrap_or_default();

    Ok(Some(InvocationEnvelope {
        execution_arn,
        checkpoint_token,
        updated_operation_ids,
    }))
}

/// Reads the input payload of the inline EXECUTION operation.
///
/// The service embeds the customer payload as a JSON string in
/// `ExecutionDetails.InputPayload` of the EXECUTION operation in
/// `InitialExecutionState.Operations`. This function reads the first
/// operation of that list whose `Type` is `EXECUTION`.
///
/// - No such operation returns `PageInput::NoExecutionOperation`. The
///   operation may be on a later page, which
///   [`client::resolve_bootstrap_log`] fetches.
/// - An absent or JSON `null` `InputPayload` returns
///   `PageInput::Execution(None)`.
/// - A string `InputPayload` returns `PageInput::Execution(Some(value))`.
/// - Any other `InputPayload` fails the invocation. So a malformed
///   envelope fails before pagination or the handler runs.
fn inline_input_payload(
    payload: &serde_json::Value,
) -> Result<client::PageInput, lambda_runtime::Error> {
    let execution_op = payload
        .get("InitialExecutionState")
        .and_then(|s| s.get("Operations"))
        .and_then(serde_json::Value::as_array)
        .and_then(|ops| {
            ops.iter().find(|op| {
                op.get("Type")
                    .and_then(serde_json::Value::as_str)
                    .is_some_and(|t| t.eq_ignore_ascii_case("EXECUTION"))
            })
        });
    let Some(op) = execution_op else {
        return Ok(client::PageInput::NoExecutionOperation);
    };
    match op
        .get("ExecutionDetails")
        .and_then(|d| d.get("InputPayload"))
    {
        None | Some(serde_json::Value::Null) => Ok(client::PageInput::Execution(None)),
        Some(serde_json::Value::String(value)) => {
            Ok(client::PageInput::Execution(Some(value.clone())))
        }
        Some(_) => Err(lambda_runtime::Error::from(
            "malformed invocation envelope: InputPayload is not a string".to_owned(),
        )),
    }
}

/// Parses the customer's original event from the execution input payload.
///
/// `input_payload` is the `InputPayload` of the first EXECUTION operation
/// of the whole execution state, which [`client::resolve_bootstrap_log`]
/// returns after it follows the pagination marker. An absent payload parses
/// as `{}`, as the JS SDK does. A payload that does not parse fails the
/// invocation.
///
/// The envelope is always required: [`run`] and [`wrap`] reject an
/// envelope-free payload in `parse_envelope`, before this function runs.
/// Local testing goes through [`LocalRunner`](test_util::LocalRunner),
/// which invokes the [`wrap`]-produced service with synthesized envelopes,
/// so this function runs on the local path exactly as it does in
/// production.
fn extract_customer_input<E>(input_payload: Option<&str>) -> Result<E, lambda_runtime::Error>
where
    E: for<'de> Deserialize<'de>,
{
    serde_json::from_str(input_payload.unwrap_or("{}"))
        .map_err(|e| lambda_runtime::Error::from(format!("deserialize customer input: {e}")))
}

/// Derives the wire failure record from a handler-level `BoxError`.
///
/// Attempts to downcast to `OperationError` for structured extraction.
/// Any other error records the type `Error`, as the JS SDK records a
/// plain `Error`, unless a `TypedError` in its chain names the type. The
/// message is the error's recorded message: the message of the operation
/// failure it propagates, else its flattened chain, which never includes
/// a `TypedError`'s type name (see [`error::wire_message`]).
fn wire_error_from_box_error(err: BoxError) -> WireError {
    match err.downcast::<OperationError>() {
        Ok(op_err) => wire_error_from_operation_error(&op_err),
        Err(other) => error::wire_error_for(&*other, "Error"),
    }
}

/// Serializes the handler's `Ok` value for the `SUCCEEDED` envelope.
///
/// A value that fails to serialize records the type `Error` and the
/// `serde_json::Error` message, the same rule as an untyped handler
/// error.
fn serialize_handler_result<T: Serialize>(result: &T) -> Result<String, WireError> {
    serde_json::to_string(result).map_err(|e| error::wire_error_for(&e, "Error"))
}

/// Derives the wire failure record from an `OperationError`.
///
/// The wire type comes from the error's attached wire identity when it
/// has one, else from the kind's registry name. For a callback external
/// failure, the attached wire is already normalized. From
/// `create_callback`, it keeps the external type, error data and stack
/// trace. From `wait_for_callback`, it has the type
/// `Callback.ExternalFailure` and keeps the callback error data. Both
/// keep a nonempty message and otherwise use `Callback failed`.
fn wire_error_from_operation_error(err: &OperationError) -> WireError {
    if let OperationErrorKind::Callback(cb_err) = err.kind()
        && matches!(cb_err.kind(), CallbackErrorKind::ExternalFailure)
        && let Some(wire) = err.wire()
    {
        // Report the operation's recorded failure fields unchanged.
        return wire.clone();
    }
    error::wire_error_for(err, err.kind().wire_type_name())
}

/// Parses a wire `StackTrace` array (of strings) from an error object.
fn parse_stack_trace(error: &serde_json::Value) -> Option<Vec<String>> {
    let frames: Vec<String> = error
        .get("StackTrace")?
        .as_array()?
        .iter()
        .filter_map(serde_json::Value::as_str)
        .map(String::from)
        .collect();
    if frames.is_empty() {
        None
    } else {
        Some(frames)
    }
}

/// Parses inline operations from the durable invocation envelope into a
/// checkpoint log.
///
/// The service embeds the execution state in
/// `InitialExecutionState.Operations` as a JSON array of operation objects.
/// Each has `Id`, `Type`, `Status`, and type-specific details (e.g.,
/// `StepDetails`). On first invocation the array is empty or contains only
/// the execution-start operation; on re-invocation it contains all prior
/// checkpointed operations.
fn parse_inline_operations(payload: &serde_json::Value) -> (engine::CheckpointLog, Option<String>) {
    let initial_state = payload.get("InitialExecutionState");

    // Check for a pagination marker indicating more pages of operations.
    // Extracted independently of `Operations`: the service may omit the
    // Operations array on the first page (e.g. when a large customer
    // payload displaces it) while still supplying a marker, and the
    // remaining pages must still be fetched.
    let next_marker = initial_state
        .and_then(|s| s.get("NextMarker"))
        .and_then(serde_json::Value::as_str)
        .filter(|m| !m.is_empty())
        .map(String::from);

    // A missing or non-array `Operations` field is an empty first page.
    // Skip the first operation (Execution type: the invocation context)
    // and parse remaining step/wait/etc. operations into records.
    let records: Vec<(String, engine::CheckpointRecord)> = initial_state
        .and_then(|s| s.get("Operations"))
        .and_then(serde_json::Value::as_array)
        .map(|ops| ops.iter().filter_map(parse_single_operation).collect())
        .unwrap_or_default();

    (engine::CheckpointLog::from_records(records), next_marker)
}

/// Parses a timestamp of the invocation payload.
///
/// The payload carries a timestamp as an ISO-8601 string. An integer is
/// read as milliseconds since the Unix epoch, as the Python SDK writes it.
/// Any other JSON type, a string that does not parse, or a value outside
/// the range of `SystemTime` gives `None`.
pub(crate) fn parse_envelope_timestamp(value: &serde_json::Value) -> Option<std::time::SystemTime> {
    use aws_sdk_lambda::primitives::{DateTime, DateTimeFormat};
    let parsed = match value {
        serde_json::Value::String(s) => DateTime::from_str(s, DateTimeFormat::DateTime).ok()?,
        serde_json::Value::Number(n) => DateTime::from_millis(n.as_i64()?),
        _ => return None,
    };
    to_system_time(parsed)
}

/// Converts a service timestamp to a `SystemTime`. A value outside the
/// range of `SystemTime` on this platform gives `None`, so an out-of-range
/// timestamp reads as a missing one.
pub(crate) fn to_system_time<T: TryInto<std::time::SystemTime>>(
    timestamp: T,
) -> Option<std::time::SystemTime> {
    timestamp.try_into().ok()
}

/// Maps an inline `Status` string to a checkpoint status, ignoring case.
/// A status this SDK does not know keeps its raw value.
fn parse_inline_status(status_str: &str) -> engine::CheckpointStatus {
    match status_str.to_ascii_uppercase().as_str() {
        "STARTED" => engine::CheckpointStatus::Started,
        "SUCCEEDED" => engine::CheckpointStatus::Succeeded,
        "FAILED" => engine::CheckpointStatus::Failed,
        "PENDING" => engine::CheckpointStatus::Pending,
        "READY" => engine::CheckpointStatus::Ready,
        "CANCELLED" => engine::CheckpointStatus::Cancelled,
        "TIMED_OUT" => engine::CheckpointStatus::TimedOut,
        "STOPPED" => engine::CheckpointStatus::Stopped,
        _ => engine::CheckpointStatus::Unknown(status_str.to_owned()),
    }
}

/// Builds the plugin info of an inline `EXECUTION` operation, or returns
/// `None` when `op` has no string `Id` or `Type`, or is of another type.
///
/// The status, `ParentId` and timestamps are read as
/// `parse_single_operation` reads them.
fn execution_info_from_json(op: &serde_json::Value) -> Option<plugin::OperationInfo> {
    let id = op.get("Id").and_then(serde_json::Value::as_str)?;
    let op_type = op.get("Type").and_then(serde_json::Value::as_str)?;
    let status = parse_inline_status(
        op.get("Status")
            .and_then(serde_json::Value::as_str)
            .unwrap_or("STARTED"),
    );
    plugin::OperationInfo::execution(
        id,
        op_type,
        &status,
        op.get("ParentId")
            .and_then(serde_json::Value::as_str)
            .map(String::from),
        op.get("StartTimestamp").and_then(parse_envelope_timestamp),
        op.get("EndTimestamp").and_then(parse_envelope_timestamp),
    )
}

/// Reads the inline page of the bootstrap state and its `NextMarker`.
///
/// A non-string `InputPayload` fails, as [`inline_input_payload`] says.
fn inline_bootstrap_state(
    payload: &serde_json::Value,
) -> Result<(client::BootstrapState, Option<String>), lambda_runtime::Error> {
    let input_payload = inline_input_payload(payload)?.into_payload();
    let (log, next_marker) = parse_inline_operations(payload);
    let state = client::BootstrapState {
        log,
        input_payload,
        execution: inline_execution_info(payload),
    };
    Ok((state, next_marker))
}

/// The plugin info of the first inline operation of type `EXECUTION`, the
/// operation [`inline_input_payload`] reads the input from.
fn inline_execution_info(payload: &serde_json::Value) -> Option<plugin::OperationInfo> {
    payload
        .get("InitialExecutionState")
        .and_then(|s| s.get("Operations"))
        .and_then(serde_json::Value::as_array)
        .and_then(|ops| {
            ops.iter().find(|op| {
                op.get("Type")
                    .and_then(serde_json::Value::as_str)
                    .is_some_and(|t| t.eq_ignore_ascii_case("EXECUTION"))
            })
        })
        .and_then(execution_info_from_json)
}

/// Parses a single operation JSON object into a checkpoint record.
#[expect(clippy::too_many_lines)] // reason: sequential detail extraction reads better as one flow
fn parse_single_operation(op: &serde_json::Value) -> Option<(String, engine::CheckpointRecord)> {
    let id = op.get("Id").and_then(serde_json::Value::as_str)?;
    let op_type = op.get("Type").and_then(serde_json::Value::as_str)?;
    // Skip the Execution context operation (wire format: "EXECUTION").
    if op_type.eq_ignore_ascii_case("Execution") {
        return None;
    }
    let status_str = op
        .get("Status")
        .and_then(serde_json::Value::as_str)
        .unwrap_or("STARTED");
    // The backend sends status in UPPER_CASE (wire format). `TIMED_OUT` is
    // the only wire spelling for a timeout, per the smithy model and the
    // Python and JS SDKs; a `TIMEDOUT` value therefore falls to `Unknown`
    // like any other unrecognized status (issue #45), carrying the raw
    // value as received so replay can name it when it fails the execution.
    let status = parse_inline_status(status_str);

    // Extract step details.
    let step_details = op.get("StepDetails");
    let result = step_details
        .and_then(|d| d.get("Result"))
        .and_then(serde_json::Value::as_str)
        .map(String::from);
    let error = step_details.and_then(|d| d.get("Error"));
    let error_type = error
        .and_then(|e| e.get("ErrorType"))
        .and_then(serde_json::Value::as_str)
        .map(String::from);
    let error_message = error
        .and_then(|e| e.get("ErrorMessage"))
        .and_then(serde_json::Value::as_str)
        .map(String::from);
    let error_data = error
        .and_then(|e| e.get("ErrorData"))
        .and_then(serde_json::Value::as_str)
        .map(String::from);
    let stack_trace = error.and_then(parse_stack_trace);
    #[expect(clippy::cast_possible_truncation)] // reason: attempt ≤ MAX_ATTEMPTS (small)
    #[expect(clippy::cast_sign_loss)] // reason: clamped to non-negative
    let attempt = step_details
        .and_then(|d| d.get("Attempt"))
        .and_then(serde_json::Value::as_i64)
        .map_or(0, |a| a.clamp(0, i64::from(u32::MAX)) as u32);

    // A wait's scheduled end, or a step's next attempt.
    let due_at = op
        .get("WaitDetails")
        .and_then(|d| d.get("ScheduledEndTimestamp"))
        .or_else(|| step_details.and_then(|d| d.get("NextAttemptTimestamp")))
        .and_then(parse_envelope_timestamp);

    // Parse ChainedInvokeDetails (for invoke operations).
    let invoke_details = op.get("ChainedInvokeDetails");
    let invoke_result = invoke_details
        .and_then(|d| d.get("Result"))
        .and_then(serde_json::Value::as_str)
        .map(String::from);
    let invoke_error = invoke_details.and_then(|d| d.get("Error"));
    let invoke_error_type = invoke_error
        .and_then(|e| e.get("ErrorType"))
        .and_then(serde_json::Value::as_str)
        .map(String::from);
    let invoke_error_message = invoke_error
        .and_then(|e| e.get("ErrorMessage"))
        .and_then(serde_json::Value::as_str)
        .map(String::from);
    let invoke_error_data = invoke_error
        .and_then(|e| e.get("ErrorData"))
        .and_then(serde_json::Value::as_str)
        .map(String::from);
    let invoke_stack_trace = invoke_error.and_then(parse_stack_trace);

    // Parse ContextDetails for child context operations.
    let context_details = op.get("ContextDetails");
    let replay_children = context_details
        .and_then(|d| d.get("ReplayChildren"))
        .and_then(serde_json::Value::as_bool)
        .unwrap_or(false);

    // Parse CallbackDetails for callback operations.
    let callback_details = op.get("CallbackDetails");
    let callback_id = callback_details
        .and_then(|d| d.get("CallbackId"))
        .and_then(serde_json::Value::as_str)
        .map(String::from);

    // Also check for result in ContextDetails (child context success payload).
    let result = result.or_else(|| {
        context_details
            .and_then(|d| d.get("Result"))
            .and_then(serde_json::Value::as_str)
            .map(String::from)
    });

    // Also check for errors in ContextDetails (child context failure).
    let context_error = context_details.and_then(|d| d.get("Error"));
    let error_type = error_type.or_else(|| {
        context_error
            .and_then(|e| e.get("ErrorType"))
            .and_then(serde_json::Value::as_str)
            .map(String::from)
    });
    let error_message = error_message.or_else(|| {
        context_error
            .and_then(|e| e.get("ErrorMessage"))
            .and_then(serde_json::Value::as_str)
            .map(String::from)
    });
    let error_data = error_data.or_else(|| {
        context_error
            .and_then(|e| e.get("ErrorData"))
            .and_then(serde_json::Value::as_str)
            .map(String::from)
    });
    let stack_trace = stack_trace.or_else(|| context_error.and_then(parse_stack_trace));

    // Also check for result in CallbackDetails (callback success payload).
    let result = result.or_else(|| {
        callback_details
            .and_then(|d| d.get("Result"))
            .and_then(serde_json::Value::as_str)
            .map(String::from)
    });

    // Also check for errors in CallbackDetails (callback failure).
    let callback_error = callback_details.and_then(|d| d.get("Error"));
    let error_type = error_type.or_else(|| {
        callback_error
            .and_then(|e| e.get("ErrorType"))
            .and_then(serde_json::Value::as_str)
            .map(String::from)
    });
    let error_message = error_message.or_else(|| {
        callback_error
            .and_then(|e| e.get("ErrorMessage"))
            .and_then(serde_json::Value::as_str)
            .map(String::from)
    });
    let error_data = error_data.or_else(|| {
        callback_error
            .and_then(|e| e.get("ErrorData"))
            .and_then(serde_json::Value::as_str)
            .map(String::from)
    });
    let stack_trace = stack_trace.or_else(|| callback_error.and_then(parse_stack_trace));

    Some((
        id.to_owned(),
        engine::CheckpointRecord {
            id: id.to_owned(),
            status,
            result,
            error_type,
            error_message,
            error_data,
            stack_trace,
            attempt,
            invoke_result,
            invoke_error_type,
            invoke_error_message,
            invoke_error_data,
            invoke_stack_trace,
            replay_children,
            callback_id,
            op_type: Some(op_type.to_owned()),
            sub_type: op
                .get("SubType")
                .and_then(serde_json::Value::as_str)
                .map(String::from),
            op_name: op
                .get("Name")
                .and_then(serde_json::Value::as_str)
                .map(String::from),
            due_at,
            parent_id: op
                .get("ParentId")
                .and_then(serde_json::Value::as_str)
                .map(String::from),
            start_timestamp: op.get("StartTimestamp").and_then(parse_envelope_timestamp),
            end_timestamp: op.get("EndTimestamp").and_then(parse_envelope_timestamp),
        },
    ))
}

/// The raw invocation payload `lambda_runtime` hands to the service
/// functions [`wrap`] produces.
///
/// Opaque by design: the payload is the durable invocation envelope the
/// service sends, and its JSON representation is an implementation detail
/// between the runtime and the SDK. Callers never construct or inspect
/// one: `lambda_runtime::service_fn` deserializes it, and the service
/// function consumes it. Keeping the inner value private is what keeps
/// `serde_json` out of the crate's public API.
#[derive(Debug, serde::Deserialize)]
pub struct InvocationPayload(pub(crate) serde_json::Value);

/// The durable response envelope one invocation of a [`wrap`]-produced
/// service function resolves with.
///
/// Opaque by design: the envelope carries the invocation's status
/// (`SUCCEEDED`, `FAILED`, or suspension) for the durable execution
/// service to read, and its JSON representation is an implementation
/// detail between the SDK and the service. Callers hand it back to
/// `lambda_runtime` unexamined.
#[derive(Debug, serde::Serialize)]
pub struct InvocationResponse(pub(crate) serde_json::Value);

/// Boxed, pinned future of one Lambda invocation of a wrapped durable
/// handler: the return type of the service functions [`wrap`] produces.
///
/// Crate-internal: rustdoc renders the alias transparently, and callers
/// only ever name the `impl Fn` the wrappers return.
pub(crate) type BoxedInvocationFuture = std::pin::Pin<
    Box<dyn Future<Output = Result<InvocationResponse, lambda_runtime::Error>> + Send>,
>;

/// Creates a Lambda service function with durable execution support.
///
/// Unlike [`run`], this does not start the runtime: it returns a service
/// function suitable for passing to `lambda_runtime::run`. Use this for
/// composable setups where you need additional middleware or custom
/// runtime configuration.
///
/// The service function reports handler failures inside a *successful*
/// Lambda invocation response, a `FAILED` status envelope the durable
/// execution service reads, never as a Lambda invocation error. Middleware
/// wrapped around this service therefore sees `Ok` for failed handlers, and
/// Lambda-level error signals (the `Errors` metric, DLQs and `OnFailure`
/// destinations, X-Ray error status) do not fire. Monitor the durable
/// execution status (`GetDurableExecution`) instead; see [`run`] for
/// details.
///
/// A serialized result over 6 MB minus 50 bytes (6 291 406 bytes) is
/// checkpointed as the execution result, and the envelope carries an empty
/// `Result`.
///
/// # Errors
///
/// Returns an error if configuration is invalid.
///
/// # Examples
///
/// ```no_run
/// use aws_durable_execution_sdk as durable;
/// use serde::{Deserialize, Serialize};
///
/// #[derive(Deserialize)]
/// struct MyEvent { name: String }
///
/// #[tokio::main]
/// async fn main() -> Result<(), lambda_runtime::Error> {
///     let service = durable::wrap(
///         |event: MyEvent, ctx: durable::DurableContext| async move {
///             Ok(format!("Hello, {}!", event.name))
///         },
///         durable::Options::default(),
///     );
///     lambda_runtime::run(lambda_runtime::service_fn(service)).await
/// }
/// ```
pub fn wrap<F, E, Fut, O>(
    handler: F,
    options: Options,
) -> impl Fn(lambda_runtime::LambdaEvent<InvocationPayload>) -> BoxedInvocationFuture + Send + Sync
where
    F: Fn(E, DurableContext) -> Fut + Send + Sync + 'static,
    E: for<'de> Deserialize<'de> + Send + 'static,
    Fut: Future<Output = Result<O, BoxError>> + Send,
    O: Serialize + Send + 'static,
{
    use std::sync::Arc as StdArc;

    // Consume Options once, at wrap time. The execution client is resolved a
    // single time here and reused across every invocation (cold-start best
    // practice).
    let Options {
        sdk_config,
        lambda_client,
        execution_client,
        checkpoint_delay,
        plugins,
        plugin_child_operations_depth,
    } = options;
    // The checkpoint buffer window: a configured delay is the coalescing
    // window; without one, writes batch behind the single-writer lock with
    // a zero window and are never held back.
    let checkpoint_buffer_window = checkpoint_delay.unwrap_or(Duration::ZERO);
    // A supplied transport replaces the Lambda transport; `build()` already
    // rejected it together with `lambda_client` or `sdk_config`.
    let preset_client: Option<StdArc<dyn ExecutionClient>> = execution_client.or_else(|| {
        base_lambda_client_from_options(sdk_config, lambda_client)
            .map(|c| StdArc::new(LambdaExecutionClient::new(c)) as StdArc<dyn ExecutionClient>)
    });
    wrap_with_provider(
        handler,
        ClientProvider::new(preset_client),
        checkpoint_buffer_window,
        plugin_child_operations_depth,
        plugins.into(),
    )
}

/// Body of [`wrap`]: builds the per-invocation service function on top of
/// an already-resolved [`ClientProvider`]. The `test-util` `LocalRunner`
/// calls [`wrap`] with its
/// in-memory backend as the execution client, so it runs the same envelope
/// parsing, pagination, driver, and error-mapping code as production.
#[expect(clippy::too_many_lines)] // reason: the invocation's steps read in order as one flow
fn wrap_with_provider<F, E, Fut, O>(
    handler: F,
    provider: ClientProvider,
    checkpoint_buffer_window: Duration,
    plugin_child_operations_depth: u32,
    plugin_factories: std::sync::Arc<plugin::Factories>,
) -> impl Fn(lambda_runtime::LambdaEvent<InvocationPayload>) -> BoxedInvocationFuture + Send + Sync
where
    F: Fn(E, DurableContext) -> Fut + Send + Sync + 'static,
    E: for<'de> Deserialize<'de> + Send + 'static,
    Fut: Future<Output = Result<O, BoxError>> + Send,
    O: Serialize + Send + 'static,
{
    use std::sync::Arc as StdArc;

    let handler = StdArc::new(handler);
    let provider = StdArc::new(provider);

    move |event: lambda_runtime::LambdaEvent<InvocationPayload>| -> std::pin::Pin<
        Box<dyn Future<Output = Result<InvocationResponse, lambda_runtime::Error>> + Send>,
    > {
        let handler = StdArc::clone(&handler);
        let provider = StdArc::clone(&provider);
        let plugin_factories = StdArc::clone(&plugin_factories);
        Box::pin(async move {
            let (InvocationPayload(raw_payload), lambda_ctx) = event.into_parts();

            // Parse and validate the durable invocation envelope.
            let envelope = parse_envelope(&raw_payload)?.ok_or_else(|| {
                lambda_runtime::Error::from(
                    "invocation payload is not a durable execution envelope \
                         (missing DurableExecutionArn, CheckpointToken, and \
                         InitialExecutionState)"
                        .to_owned(),
                )
            })?;
            let execution_arn = envelope.execution_arn;
            let checkpoint_token = envelope.checkpoint_token;
            let updated_operation_ids = envelope.updated_operation_ids;

            // Read the inline input and the inline page. A non-string
            // `InputPayload` fails here, before pagination or the handler.
            let (first_page, initial_marker) = inline_bootstrap_state(&raw_payload)?;

            // Reuse the execution client resolved once at wrap time (built
            // from the ambient default at most once when no client was
            // supplied via Options).
            let exec_client = provider.get().await;

            // If the initial state was paginated, fetch remaining pages. A
            // retryable error fails the invocation, so the service
            // re-invokes. A non-retryable error (a KMS misconfiguration)
            // fails the same way on every invocation, so it fails the
            // execution before the handler runs, as in JS.
            let (checkpoint_log, input_payload, execution) = match client::resolve_bootstrap_log(
                exec_client.as_ref(),
                &execution_arn,
                &checkpoint_token,
                first_page,
                initial_marker.as_deref(),
            )
            .await
            {
                Ok(state) => (StdArc::new(state.log), state.input_payload, state.execution),
                Err(e) if e.is_retryable() => {
                    return Err(lambda_runtime::Error::from(format!(
                        "failed to paginate initial state: {e}"
                    )));
                }
                Err(e) => {
                    return Ok(failed_envelope(&WireError::new(
                        Some(client::bootstrap_error_type(&e)),
                        Some(client::client_error_record_message(&e)),
                    )));
                }
            };

            // The input is parsed after pagination, because it may be on a
            // later page. An absent payload parses as `{}`.
            let customer_input: E = extract_customer_input(input_payload.as_deref())?;

            // The plugins of this invocation. The local holds them until the
            // invocation future returns, so every plugin instance is dropped
            // when its invocation returns. Nothing is built without a
            // registered factory. Their `Drop` catches a plugin destructor
            // panic, so dropping them never changes the outcome.
            let plugins = create_invocation_plugins(
                &plugin_factories,
                &lambda_ctx,
                &execution_arn,
                input_payload.as_deref(),
                &checkpoint_log,
                execution.as_ref(),
                updated_operation_ids,
            );
            // When this future is dropped before it returns, the hook
            // tasks are aborted and no panic of theirs escapes.
            let _shut_down = plugins.as_ref().map(plugin::ShutDownOnDrop::new);

            // The start hooks run before the handler, and every one of
            // them finishes first.
            if let Some(plugins) = &plugins {
                plugins.invocation_start().await;
            }
            // The end info reads the log as it is at the end.
            let end_log = StdArc::clone(&checkpoint_log);

            let ctx = DurableContext::new_root_with_client_and_defaults(
                execution_arn,
                lambda_ctx,
                checkpoint_log,
                exec_client,
                checkpoint_token,
                checkpoint_buffer_window,
                plugin_child_operations_depth,
                plugins.clone(),
            );

            // Retained past the handler move so the post-outcome flush can
            // drain the checkpoint coalescing buffer (a cheap Arc clone).
            let flush_ctx = ctx.clone();
            let coordinator = StdArc::clone(ctx.coordinator());
            // A factory error or a start hook error whose destructor
            // panicked fails the invocation. The plugins were created before
            // the coordinator existed, so such a panic is recorded here,
            // which ends the drive. Every later one is recorded at once.
            if let Some(plugins) = &plugins {
                plugins.attach(&coordinator);
            }
            let remaining = ctx.remaining_time();
            let replay_span = ctx.replay_span();

            // Lifecycle event: exactly one of `execution_started` /
            // `execution_resumed` per invocation, emitted while the
            // handler's `durable_execution` span is entered so it is a
            // span event of that span (which is what lets the documented
            // tracing-opentelemetry bridge export it on the execution
            // span). See `crate::observability`.
            {
                let _execution_scope = replay_span.enter();
                tracing_layer::invocation_begin_event(
                    ctx.is_replaying(),
                    ctx.execution_arn(),
                    &ctx.lambda_context().request_id,
                );
            }

            // Run the handler through the driver which handles suspension.
            // The root task runs inside the handler-level span, then inside
            // the invocation's frames, so user log events between operations
            // carry the execution ARN and the live `isReplay` flag, and a
            // span a `wrap_invocation` enters is a child of that span.
            //
            // The handler runs as the root context's body, so an operation
            // called on a child context that escaped its closure fails the
            // execution with a context usage error.
            //
            // The root adapter owns everything it captures (the handler, the
            // event, the context and the span), so it is `'static` and the
            // driver can run it as the invocation's root task, while the
            // handler's own future needs no `'static` bound.
            let task_ownership = StdArc::clone(ctx.task_ownership());
            let root = context::run_body(context::ActiveBody::Context(String::new()), async move {
                match (handler)(customer_input, ctx).await {
                    Ok(result) => serialize_handler_result(&result),
                    Err(e) => Err(wire_error_from_box_error(e)),
                }
            });
            // An invocation-failing panic (of the handler, of the
            // checkpoint client, or of any task the SDK started) returns
            // here only after the driver cleaned up every task, and leaves
            // the SDK with its original payload. When several panic, the
            // first one recorded is resumed. A fatal error that the driver's
            // arbitration reads takes precedence over a panic, so the driver
            // then returns the fatal outcome instead. Between the cleanup and the resume, only the
            // plugins' drain and end hooks run, with `Retrying`.
            //
            // Everything from here to the response runs inside every
            // plugin's `wrap_invocation`, so the wraps are active while the
            // end hooks run. The future returns its outcome as a value and
            // never resumes a panic itself. So it finishes, and is dropped
            // inside the wraps, before the contexts and plugins are dropped
            // and before any panic resumes.
            //
            // The root task's own context is built from the same frames and
            // the handler-level span. That span covers the root task's polls
            // and drop, not the processing after the drive.
            let frames = plugin::Frames::invocation(plugins.clone());
            let root_cx = task::TaskContext::new(frames.clone(), replay_span);
            let finished = plugin::Frames::instrument(
                &frames,
                CatchFinished::new(finish_invocation(
                    root,
                    root_cx,
                    task_ownership,
                    coordinator,
                    &flush_ctx,
                    remaining,
                    plugins.as_deref(),
                    &end_log,
                )),
            )
            .await;
            // The frames hold the plugins too. They are dropped before
            // `respond` can resume a panic, so no plugin destructor runs
            // while it unwinds.
            drop(frames);
            respond(finished, flush_ctx, plugins)
        })
    }
}

/// Test-only hooks into `finish_invocation`.
#[cfg(test)]
mod test_hooks {
    tokio::task_local! {
        /// When set, `finish_invocation` returns `Pending` once right after
        /// `drive_invocation` returns, and sets the flag at that poll, so a
        /// wrap's code after `next()` runs in a post-drive poll.
        pub(crate) static PENDING_AFTER_DRIVE:
            std::sync::Arc<std::sync::atomic::AtomicBool>;
    }

    pub(crate) async fn pending_once_after_drive() {
        let Ok(flag) = PENDING_AFTER_DRIVE.try_with(std::sync::Arc::clone) else {
            return;
        };
        let mut pending = true;
        std::future::poll_fn(|cx| {
            if std::mem::take(&mut pending) {
                flag.store(true, std::sync::atomic::Ordering::SeqCst);
                cx.waker().wake_by_ref();
                std::task::Poll::Pending
            } else {
                std::task::Poll::Ready(())
            }
        })
        .await;
    }

    /// What `pending_holding_guard` holds and reports.
    pub(crate) struct HeldGuard {
        /// Set at the poll that returns `Pending`.
        pub(crate) reached: std::sync::Arc<std::sync::atomic::AtomicBool>,
        /// Makes the value the pending future holds.
        pub(crate) make: Box<dyn Fn() -> Box<dyn Send> + Send + Sync>,
    }

    tokio::task_local! {
        /// When set, `finish_invocation` returns `Pending` once right after
        /// `drive_invocation` returns, while it holds a value that `make`
        /// built, and sets `reached` at that poll. A test drops the
        /// post-start future there, with the value still held.
        pub(crate) static PENDING_HOLDING_GUARD: std::sync::Arc<HeldGuard>;
    }

    pub(crate) async fn pending_holding_guard() {
        let Ok(held) = PENDING_HOLDING_GUARD.try_with(std::sync::Arc::clone) else {
            return;
        };
        let guard = (held.make)();
        let mut pending = true;
        std::future::poll_fn(|cx| {
            if std::mem::take(&mut pending) {
                held.reached
                    .store(true, std::sync::atomic::Ordering::SeqCst);
                cx.waker().wake_by_ref();
                std::task::Poll::Pending
            } else {
                std::task::Poll::Ready(())
            }
        })
        .await;
        drop(guard);
    }
}

/// How the part of an invocation after its start hooks finished.
enum Finished {
    /// The response, and a late panic that fails the invocation instead.
    Respond(
        Result<InvocationResponse, lambda_runtime::Error>,
        Option<Box<dyn std::any::Any + Send + 'static>>,
    ),
    /// The drive ended with an invocation-failing panic.
    Panicked(Box<dyn std::any::Any + Send + 'static>),
}

/// Drives the handler, flushes, runs the end hooks and builds the
/// response. It returns a panic as a value, so its caller resumes it only
/// after this future was dropped inside the plugins' wraps.
#[expect(
    clippy::too_many_arguments,
    reason = "the root task's future and its context are passed apart, as `spawn_root` takes them"
)]
async fn finish_invocation<R>(
    root: R,
    root_cx: task::TaskContext,
    task_ownership: std::sync::Arc<driver::TaskOwnership>,
    coordinator: std::sync::Arc<coordinator::InvocationCoordinator>,
    flush_ctx: &DurableContext,
    remaining: Option<Duration>,
    plugins: Option<&plugin::InvocationPlugins>,
    end_log: &engine::CheckpointLog,
) -> Finished
where
    R: Future<Output = Result<String, WireError>> + Send + 'static,
{
    let (outcome, fatal_won) = match driver::drive_invocation(
        root,
        root_cx,
        task_ownership,
        coordinator,
        flush_ctx.clone(),
        remaining,
    )
    .await
    {
        Ok(driven) => driven,
        Err(payload) => {
            let end = InvocationEnd::panicked(&*payload);
            // A late panic never replaces this one: without a
            // response, `end_invocation` discards every late panic
            // and returns none.
            end_invocation(plugins, end_log, flush_ctx, end, None, false)
                .await
                .into_iter()
                .for_each(task::discard_panic);
            return Finished::Panicked(payload);
        }
    };
    // `fatal_won` says whether a fatal error won the drive's own
    // arbitration, as `drive_invocation` decided it. The late-panic policy
    // uses it and never reads the coordinator's fatal slot, which code
    // after the arbitration can still fill: a plugin claim during the flush
    // (a wrap's code after `next()`) or in an end hook records a context
    // usage error there. Such a claim cannot shift an operation ID the
    // execution uses: a claim that can shift a used ID happens before a
    // later operation of the same drive mints its ID, so it is recorded
    // before the arbitration. A claim after it comes after the last
    // operation of this invocation, and the next invocation replays from
    // scratch.
    #[cfg(test)]
    test_hooks::pending_once_after_drive().await;
    #[cfg(test)]
    test_hooks::pending_holding_guard().await;

    // Unconditional flush point of the checkpoint buffering
    // contract (`checkpoint_delay`):
    // whatever the outcome, suspension (PENDING), completion
    // (SUCCEEDED), or failure (FAILED), every buffered checkpoint
    // is written, and every in-flight batched write is awaited,
    // BEFORE the envelope reports the invocation's state to the
    // service, so buffering can never hold a checkpoint past the
    // end of the invocation. A no-op when nothing is buffered.
    // A `Fault` outcome skips the flush: the invocation fails, and the
    // service invokes the function again. After a failed write the flush
    // sends nothing (see `DurableContext::flush_pending_checkpoints`).
    if !matches!(outcome, driver::InvocationOutcome::Fault { .. }) {
        flush_ctx.flush_pending_checkpoints().await;
    }
    // A checkpoint write that failed after the arbitration, in the flush,
    // decides the outcome over the handler's result, as one recorded
    // during the drive does. A fatal error that other code recorded after
    // the arbitration does not.
    let outcome = match flush_ctx.coordinator().write_failure() {
        Some(failure) if !fatal_won => failure.into_outcome(),
        _ => outcome,
    };

    // Convert outcome to the durable response envelope.
    //
    // ENVELOPE CONTRACT: do not "fix" the `Ok` below. Every outcome,
    // including a handler failure, is reported inside a *successful*
    // Lambda invocation response: the durable execution service reads
    // the `Status` field of this envelope to record the execution
    // result, and it can only do that when the invocation itself
    // succeeds. Returning `Err` here would make the service treat the
    // invocation as a runtime fault and retry it, rather than marking
    // the execution FAILED with the handler's error.
    //
    // The one deliberate exception is `Fault`, a retryable
    // checkpoint failure, where retry-as-a-runtime-fault is
    // exactly the intended recovery, so it maps to `Err` inside
    // `outcome_envelope`.
    //
    // The observable consequence, which is intentional: a handler
    // failure does not increment the Lambda `Errors` metric, does not
    // route to a DLQ or OnFailure destination, and does not mark the
    // X-Ray trace as an error. Operators must monitor the durable
    // execution status (`GetDurableExecution` /
    // `ListDurableExecutionsByFunction`) instead. See the rustdoc on
    // [`run`] and [`wrap`].
    //
    // A result over the response limit is checkpointed as the execution's
    // own `SUCCEED` update instead, after the flush, as in JS. This is a
    // response site like the others: `end_invocation` below drains the
    // change queue and runs the end hooks once with the end it returns.
    let (response, end) = match outcome {
        driver::InvocationOutcome::Complete(serialized)
            if serialized.len() > LAMBDA_RESPONSE_SIZE_LIMIT =>
        {
            checkpoint_oversized_result(flush_ctx, serialized).await
        }
        outcome => outcome_envelope(outcome, flush_ctx),
    };
    let (response, end) = driver::withdrawal_answer(flush_ctx, response, end);
    let late = end_invocation(plugins, end_log, flush_ctx, end, Some(&response), fatal_won).await;
    Finished::Respond(response, late)
}

/// Returns the response, unless a panic fails the invocation: then the
/// panic resumes instead. The plugins are dropped before it resumes, so no
/// plugin destructor runs while it unwinds. The contexts hold them too, so
/// `ctx` is dropped as well.
fn respond(
    finished: Finished,
    ctx: DurableContext,
    plugins: Option<std::sync::Arc<plugin::InvocationPlugins>>,
) -> Result<InvocationResponse, lambda_runtime::Error> {
    let payload = match finished {
        Finished::Respond(response, None) => return response,
        Finished::Respond(response, Some(payload)) => {
            drop_contained(response);
            payload
        }
        Finished::Panicked(payload) => payload,
    };
    // The first panic wins. A destructor that panics while it is dropped
    // here is a second panic, so it is discarded.
    drop_contained(ctx);
    drop_contained(plugins);
    std::panic::resume_unwind(payload)
}

/// Drops `value` inside a catch, and discards a panic of its destructor.
fn drop_contained<T>(value: T) {
    if let Err(payload) =
        std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || drop(value)))
    {
        task::discard_panic(payload);
    }
}

/// The post-start invocation future, polled inside a catch. Any panic of a
/// poll, from SDK code or from a subscriber, becomes
/// [`Finished::Panicked`], so the explicit drop order in [`respond`] holds
/// on every path.
struct CatchFinished<F> {
    /// `None` once it finished or panicked.
    inner: Option<std::pin::Pin<Box<F>>>,
}

impl<F> CatchFinished<F> {
    fn new(inner: F) -> Self {
        Self {
            inner: Some(Box::pin(inner)),
        }
    }
}

impl<F: Future<Output = Finished>> Future for CatchFinished<F> {
    type Output = Finished;

    fn poll(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Finished> {
        let this = self.get_mut();
        let Some(inner) = this.inner.as_mut() else {
            return std::task::Poll::Pending;
        };
        match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| inner.as_mut().poll(cx))) {
            Ok(std::task::Poll::Pending) => std::task::Poll::Pending,
            Ok(std::task::Poll::Ready(finished)) => {
                this.inner = None;
                std::task::Poll::Ready(finished)
            }
            Err(payload) => {
                // The future is torn down now, inside the caller's wraps.
                // A second panic from its destructor is discarded.
                if let Some(inner) = this.inner.take() {
                    drop_contained(inner);
                }
                std::task::Poll::Ready(Finished::Panicked(payload))
            }
        }
    }
}

/// How an invocation ended, for the plugins' end hooks. It is set where
/// the response is built.
pub(crate) struct InvocationEnd {
    pub(crate) status: InvocationStatus,
    pub(crate) result: Option<String>,
    pub(crate) error: Option<WireError>,
}

impl InvocationEnd {
    /// The end of an invocation that `payload`, an invocation-failing
    /// panic, ended: `Retrying`, with an error that has no type and the
    /// panic's message.
    pub(crate) fn panicked(payload: &(dyn std::any::Any + Send)) -> Self {
        Self {
            status: InvocationStatus::Retrying,
            result: None,
            error: Some(WireError::new(
                None::<String>,
                Some(panic_payload_text(payload)),
            )),
        }
    }

    /// The end of an invocation that failed with the Lambda error
    /// `message`: `Retrying`, with the fault's type.
    fn retrying(error_type: impl Into<String>, message: String) -> Self {
        Self {
            status: InvocationStatus::Retrying,
            result: None,
            error: Some(WireError::new(Some(error_type.into()), Some(message))),
        }
    }
}

/// The text of a panic payload: the payload itself when it is a
/// `&'static str` or a `String`, and a fixed text otherwise. It does not
/// consume the payload.
pub(crate) fn panic_payload_text(payload: &(dyn std::any::Any + Send)) -> String {
    if let Some(text) = payload.downcast_ref::<&'static str>() {
        (*text).to_owned()
    } else if let Some(text) = payload.downcast_ref::<String>() {
        text.clone()
    } else {
        "the invocation panicked with a payload that is not a string".to_owned()
    }
}

/// Drains the plugins' hook work and runs their end hooks once, when the
/// invocation has plugins. The end info reads `log` as it is now.
///
/// Returns the late panic that fails the invocation instead of `response`
/// (see [`plugin::LatePanics`]). `response` is `None` when a panic already
/// ends the invocation. A late panic is discarded when the invocation
/// already fails: with that panic, with a Lambda error, or with a fatal
/// error that won the drive's arbitration (`fatal_won`, the snapshot taken
/// during the arbitration and returned by the drive), which the drive keeps
/// over a panic too.
async fn end_invocation(
    plugins: Option<&plugin::InvocationPlugins>,
    log: &engine::CheckpointLog,
    ctx: &DurableContext,
    end: InvocationEnd,
    response: Option<&Result<InvocationResponse, lambda_runtime::Error>>,
    fatal_won: bool,
) -> Option<Box<dyn std::any::Any + Send + 'static>> {
    let plugins = plugins?;
    // Every change set still queued, including one whose writer was
    // dropped and one from the end-of-invocation flush, reaches
    // `on_operation_change` before `on_invocation_end`.
    ctx.report_operation_changes().await;
    plugins
        .invocation_end(
            log,
            end,
            ctx.remaining_time(),
            late_panics(response, fatal_won),
        )
        .await
}

/// What a late panic does at the end of an invocation that returns
/// `response`, given whether a fatal error won the drive's arbitration
/// (see [`end_invocation`]).
fn late_panics(
    response: Option<&Result<InvocationResponse, lambda_runtime::Error>>,
    fatal_won: bool,
) -> plugin::LatePanics {
    match response {
        Some(Ok(_)) if !fatal_won => plugin::LatePanics::Fail,
        _ => plugin::LatePanics::Discard,
    }
}

/// Creates the plugins of one invocation from the bootstrap state.
///
/// The invocation info is built only when a factory is registered. Its
/// input is `{}` when the execution has none, its operations map holds the
/// `EXECUTION` operation, and its X-Ray trace header comes from the Lambda
/// context, never from the process environment.
fn create_invocation_plugins(
    factories: &plugin::Factories,
    lambda_ctx: &lambda_runtime::Context,
    execution_arn: &str,
    input_payload: Option<&str>,
    checkpoint_log: &engine::CheckpointLog,
    execution: Option<&plugin::OperationInfo>,
    updated_operation_ids: Vec<String>,
) -> Option<std::sync::Arc<plugin::InvocationPlugins>> {
    plugin::create_plugins(factories, execution.cloned(), || {
        plugin::InvocationInfo::new(
            lambda_ctx.request_id.clone(),
            execution_arn.to_owned(),
            input_payload.unwrap_or("{}").to_owned(),
            plugin::operations_map(checkpoint_log, execution),
            updated_operation_ids,
            execution.and_then(plugin::OperationInfo::start_timestamp),
            !checkpoint_log.has_records(),
            lambda_ctx.xray_trace_id.clone(),
        )
    })
}

/// Converts the driver's invocation outcome into the durable response
/// envelope, emitting the `execution_suspended` lifecycle event on the
/// suspension path (see [`crate::observability`]).
///
/// The FAILED status deliberately travels in the envelope, not as a Lambda
/// invocation error: see the envelope contract note at the call site. The
/// `Fault` outcome is the deliberate inverse: a retryable checkpoint
/// failure returns `Err`, failing the Lambda invocation itself so the
/// durable service re-invokes (issue #43).
///
/// It also returns how the invocation ended, for the plugins' end hooks.
pub(crate) fn outcome_envelope(
    outcome: driver::InvocationOutcome,
    ctx: &DurableContext,
) -> (
    Result<InvocationResponse, lambda_runtime::Error>,
    InvocationEnd,
) {
    match outcome {
        driver::InvocationOutcome::Complete(serialized) => (
            Ok(InvocationResponse(serde_json::json!({
                "Status": "SUCCEEDED",
                "Result": serialized
            }))),
            InvocationEnd {
                status: InvocationStatus::Succeeded,
                result: Some(serialized),
                error: None,
            },
        ),
        driver::InvocationOutcome::Pending => {
            // Emitted while the handler's `durable_execution` span is
            // entered. The root task, whose `TaskContext` entered that span
            // around every poll and the drop of the handler future, has
            // already ended, but the context still holds the span handle,
            // so the event is a span event of the execution span, matching
            // the documented OpenTelemetry bridge (see
            // `crate::observability`).
            let execution_span = ctx.replay_span();
            let _execution_scope = execution_span.enter();
            tracing_layer::execution_suspended_event(
                ctx.execution_arn(),
                &ctx.lambda_context().request_id,
            );
            (
                Ok(InvocationResponse(serde_json::json!({
                    "Status": "PENDING"
                }))),
                InvocationEnd {
                    status: InvocationStatus::Pending,
                    result: None,
                    error: None,
                },
            )
        }
        driver::InvocationOutcome::Fault {
            error_type,
            message,
        } => {
            // A retryable checkpoint or serdes failure: fail the invocation
            // itself (no envelope, no further writes). The service
            // re-invokes, which is the same recovery as an interruption.
            // The type reaches only the end hooks.
            (
                Err(lambda_runtime::Error::from(message.clone())),
                InvocationEnd::retrying(error_type, message),
            )
        }
        driver::InvocationOutcome::Failed { error } => (
            Ok(failed_envelope(&error)),
            InvocationEnd {
                status: InvocationStatus::Failed,
                result: None,
                error: Some(error),
            },
        ),
    }
}

/// Lambda's response limit minus 50 bytes for the envelope, as in JS.
const LAMBDA_RESPONSE_SIZE_LIMIT: usize = 6 * 1024 * 1024 - 50;

/// Checkpoints `serialized`, a handler result over
/// [`LAMBDA_RESPONSE_SIZE_LIMIT`], as the execution's own `SUCCEED`
/// update, and returns the response and the end for the plugins' end
/// hooks.
///
/// The update's ID is the wire hash of `execution-result-<epoch ms>`, as
/// in JS. It may read the clock, because a successful write ends the
/// execution and no later invocation looks the ID up.
///
/// - Accepted: `{"Status":"SUCCEEDED","Result":""}`, and the end
///   `Succeeded` with the full result.
/// - A retryable failure fails the invocation with the message of
///   [`checkpoint_invocation_failure_message`](error::checkpoint_invocation_failure_message),
///   `[Unrecoverable Invocation] Checkpoint failed: <message>`, and the end
///   is `Retrying` with that message.
/// - A non-retryable failure answers `FAILED` with the record of
///   [`checkpoint_execution_failure_wire`](error::checkpoint_execution_failure_wire),
///   and the end is `Failed` with the same error.
/// - A withdrawn checkpoint token answers `PENDING`.
async fn checkpoint_oversized_result(
    ctx: &DurableContext,
    serialized: String,
) -> (
    Result<InvocationResponse, lambda_runtime::Error>,
    InvocationEnd,
) {
    #[expect(clippy::expect_used)]
    // reason: `duration_since` fails only for a clock set before 1970,
    // which the Lambda runtime's clock never is.
    let epoch_ms = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("the clock is after the Unix epoch")
        .as_millis();
    #[expect(clippy::expect_used)]
    // reason: the ID, type and action, the builder's required fields, are set.
    let update = aws_sdk_lambda::types::OperationUpdate::builder()
        .id(engine::compute_wire_id_public(&format!(
            "execution-result-{epoch_ms}"
        )))
        .r#type(aws_sdk_lambda::types::OperationType::Execution)
        .action(aws_sdk_lambda::types::OperationAction::Succeed)
        .payload(serialized.clone())
        .build()
        .expect("the update's required fields are set");
    match ctx.checkpoint_updates_after_drive(vec![update]).await {
        Ok(_) => (
            Ok(InvocationResponse(serde_json::json!({
                "Status": "SUCCEEDED",
                "Result": ""
            }))),
            InvocationEnd {
                status: InvocationStatus::Succeeded,
                result: Some(serialized),
                error: None,
            },
        ),
        Err(context::WriteError::Failed(e)) if e.is_retryable() => {
            let message = error::checkpoint_invocation_failure_message(&e);
            (
                Err(lambda_runtime::Error::from(message.clone())),
                InvocationEnd::retrying(error::CHECKPOINT_INVOCATION_FAILED_ERROR_TYPE, message),
            )
        }
        Err(context::WriteError::Failed(e)) => {
            let error = error::checkpoint_execution_failure_wire(&e);
            (
                Ok(failed_envelope(&error)),
                InvocationEnd {
                    status: InvocationStatus::Failed,
                    result: None,
                    error: Some(error),
                },
            )
        }
        Err(context::WriteError::Stopped) => {
            outcome_envelope(driver::InvocationOutcome::Pending, ctx)
        }
    }
}

/// Builds the `FAILED` envelope that records `error` as the execution's
/// failure.
fn failed_envelope(error: &WireError) -> InvocationResponse {
    let mut error_map = serde_json::Map::new();
    error_map.insert(
        "ErrorType".to_owned(),
        serde_json::Value::String(error.error_type().unwrap_or("Error").to_owned()),
    );
    error_map.insert(
        "ErrorMessage".to_owned(),
        serde_json::Value::String(error.error_message().unwrap_or_default().to_owned()),
    );
    if let Some(data) = error.error_data() {
        error_map.insert(
            "ErrorData".to_owned(),
            serde_json::Value::String(data.to_owned()),
        );
    }
    if !error.stack_trace().is_empty() {
        error_map.insert(
            "StackTrace".to_owned(),
            serde_json::json!(error.stack_trace()),
        );
    }
    InvocationResponse(serde_json::json!({
        "Status": "FAILED",
        "Error": serde_json::Value::Object(error_map)
    }))
}

/// Resolves the base Lambda client from the caller's [`Options`].
///
/// Precedence: a supplied `lambda_client` is used directly; otherwise a
/// supplied `sdk_config` builds one; otherwise `None`, which defers to the
/// ambient default resolved once at first use by [`ClientProvider`].
pub(crate) fn base_lambda_client_from_options(
    sdk_config: Option<SdkConfig>,
    lambda_client: Option<aws_sdk_lambda::Client>,
) -> Option<aws_sdk_lambda::Client> {
    match (lambda_client, sdk_config) {
        (Some(client), _) => Some(client),
        (None, Some(config)) => Some(aws_sdk_lambda::Client::new(&config)),
        (None, None) => None,
    }
}

/// Connect timeout of the Lambda client the SDK builds by default, as in
/// the JS SDK.
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
/// Read timeout of the Lambda client the SDK builds by default, as in the
/// JS SDK.
const DEFAULT_READ_TIMEOUT: Duration = Duration::from_secs(50);
/// Attempt timeout of the Lambda client the SDK builds by default, as the
/// JS SDK's request timeout.
const DEFAULT_ATTEMPT_TIMEOUT: Duration = Duration::from_secs(55);

/// Builds the Lambda client the SDK uses when the caller supplies neither
/// a client nor a config.
///
/// The client takes every setting from `config` and adds three timeouts.
/// A read timeout restarts on every byte received, so it does not bound a
/// response that arrives slowly. The attempt timeout bounds it. So a lost
/// or slow checkpoint response fails the call after 55 s, rather than
/// blocking until the Lambda function times out.
fn default_lambda_client(config: &SdkConfig) -> aws_sdk_lambda::Client {
    use aws_sdk_lambda::config::timeout::TimeoutConfig;
    let conf = aws_sdk_lambda::config::Builder::from(config)
        .timeout_config(
            TimeoutConfig::builder()
                .connect_timeout(DEFAULT_CONNECT_TIMEOUT)
                .read_timeout(DEFAULT_READ_TIMEOUT)
                .operation_attempt_timeout(DEFAULT_ATTEMPT_TIMEOUT)
                .build(),
        )
        .build();
    aws_sdk_lambda::Client::from_conf(conf)
}

/// Supplies the execution client for every invocation of a [`wrap`]-ed
/// handler, building it at most once and reusing it thereafter.
///
/// When [`Options`] supplied a client (or an SDK config), it is captured as
/// `preset` and returned on every call. Otherwise the ambient default config
/// is loaded lazily on the first invocation and the resulting client is
/// cached, so no per-invocation client construction or config load occurs.
pub(crate) struct ClientProvider {
    preset: Option<std::sync::Arc<dyn ExecutionClient>>,
    default_cell: tokio::sync::OnceCell<std::sync::Arc<dyn ExecutionClient>>,
}

impl ClientProvider {
    /// Creates a provider. `preset` is the client resolved from `Options`, or
    /// `None` to defer to the ambient default on first use.
    pub(crate) fn new(preset: Option<std::sync::Arc<dyn ExecutionClient>>) -> Self {
        Self {
            preset,
            default_cell: tokio::sync::OnceCell::new(),
        }
    }

    /// Returns the shared execution client, building the ambient-default one
    /// exactly once when no client was preset.
    pub(crate) async fn get(&self) -> std::sync::Arc<dyn ExecutionClient> {
        use std::sync::Arc as StdArc;
        if let Some(preset) = &self.preset {
            return StdArc::clone(preset);
        }
        let client = self
            .default_cell
            .get_or_init(|| async {
                let aws_config =
                    aws_config::load_defaults(aws_config::BehaviorVersion::latest()).await;
                let lambda_client = default_lambda_client(&aws_config);
                StdArc::new(LambdaExecutionClient::new(lambda_client))
                    as StdArc<dyn ExecutionClient>
            })
            .await;
        StdArc::clone(client)
    }
}

#[cfg(test)]
mod tests {
    use std::future::IntoFuture;

    use super::*;

    /// `RetryDecision::retry` builds the `Retry` variant with the delay.
    #[test]
    fn retry_decision_constructor_builds_retry() {
        assert!(matches!(
            RetryDecision::retry(Duration::from_secs(3)),
            RetryDecision::Retry { delay } if delay == Duration::from_secs(3)
        ));
    }

    #[test]
    fn due_at_from_inline_timestamps() {
        let payload = serde_json::json!({
            "InitialExecutionState": {
                "Operations": [
                    { "Id": "w1", "Type": "WAIT", "Status": "STARTED",
                      "WaitDetails": { "ScheduledEndTimestamp": "2026-01-01T00:00:00Z" } },
                    { "Id": "s1", "Type": "STEP", "Status": "PENDING",
                      "StepDetails": { "NextAttemptTimestamp": "2026-01-01T00:00:05.5Z" } },
                    { "Id": "w2", "Type": "WAIT", "Status": "STARTED",
                      "WaitDetails": { "ScheduledEndTimestamp": 1_767_225_600_000_i64 } },
                    { "Id": "w3", "Type": "WAIT", "Status": "STARTED",
                      "WaitDetails": { "ScheduledEndTimestamp": "not a date" } },
                    { "Id": "w4", "Type": "WAIT", "Status": "STARTED",
                      "WaitDetails": { "ScheduledEndTimestamp": 1.5 } },
                    { "Id": "w5", "Type": "WAIT", "Status": "STARTED",
                      "WaitDetails": { "ScheduledEndTimestamp": true } },
                ]
            }
        });
        let (log, _) = parse_inline_operations(&payload);
        let due = |id: &str| log.get(id).expect("parsed").due_at;
        let new_year = std::time::UNIX_EPOCH + Duration::from_hours(490_896);
        assert_eq!(due("w1"), Some(new_year));
        assert_eq!(due("s1"), Some(new_year + Duration::from_millis(5_500)));
        assert_eq!(due("w2"), Some(new_year), "epoch milliseconds");
        assert_eq!(due("w3"), None, "a string that does not parse");
        assert_eq!(due("w4"), None, "a number that is not an integer");
        assert_eq!(due("w5"), None, "another JSON type");
    }

    /// A timestamp that no platform's `SystemTime` can hold.
    struct OutOfRange;

    impl TryFrom<OutOfRange> for std::time::SystemTime {
        type Error = &'static str;

        fn try_from(_: OutOfRange) -> Result<Self, Self::Error> {
            Err("outside the range of SystemTime")
        }
    }

    /// Both `due_at` mappings convert through `to_system_time`. A timestamp
    /// in range converts, and one outside the range of `SystemTime` gives
    /// `None`, as a missing timestamp does.
    #[test]
    fn to_system_time_maps_an_out_of_range_timestamp_to_none() {
        let millis = aws_sdk_lambda::primitives::DateTime::from_millis(1_500);
        assert_eq!(
            to_system_time(millis),
            Some(std::time::UNIX_EPOCH + Duration::from_millis(1_500))
        );
        assert_eq!(to_system_time(OutOfRange), None);
    }

    #[derive(Debug)]
    struct PlainUserError;

    impl std::fmt::Display for PlainUserError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "plain user failure")
        }
    }

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

    #[test]
    fn handler_error_records_no_stack_trace() {
        let wire = wire_error_from_box_error(Box::new(PlainUserError));
        assert_eq!(wire.error_type(), Some("Error"));
        assert_eq!(wire.error_message(), Some("plain user failure"));
        assert_eq!(wire.stack_trace(), [] as [String; 0]);
    }

    /// A value whose `Serialize` implementation always fails.
    struct Refuse;

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

    #[test]
    fn handler_result_serialization_failure_records_error() {
        let Err(wire) = serialize_handler_result(&Refuse) else {
            unreachable!("a refusing value must not serialize");
        };
        assert_eq!(wire.error_type(), Some("Error"));
        assert!(
            wire.error_message().unwrap_or_default().contains("refused"),
            "{:?}",
            wire.error_message()
        );
        assert_eq!(wire.stack_trace(), [] as [String; 0]);
    }

    /// Verifies that `tokio::join!` accepts `IntoFuture` operands directly.
    ///
    /// Since tokio 1.23+, `tokio::join!` desugars through `.await` which
    /// uses `IntoFuture`. This means operation builders can be passed
    /// directly to `tokio::join!` without calling `.future()` first.
    #[tokio::test]
    async fn tokio_join_accepts_into_future() {
        // Verify compilation: IntoFuture is accepted by tokio::join!
        fn check_into_future<T: IntoFuture>(_t: T) {}
        let ctx = DurableContext::test_context();
        check_into_future(ctx.step(|_| async { Ok(1i32) }));
        // NOTE: cannot actually tokio::join! the builders because they
        // todo!() at runtime, but the type-level verification above
        // plus the external rustc test confirms IntoFuture acceptance.
    }

    /// Verifies that `wrap()` produces a service function compatible with
    /// `lambda_runtime::service_fn`. This is a compile-time + type-level
    /// test: the returned closure has the correct signature.
    #[test]
    fn wrap_returns_callable_service_function() {
        fn assert_send_sync<T: Send + Sync>(_t: &T) {}

        // Verify that wrap() compiles and returns something Send + Sync.
        let service = wrap(
            |_event: serde_json::Value, _ctx: DurableContext| async move {
                Ok::<String, BoxError>("hello".to_owned())
            },
            Options::default(),
        );

        // The service must be Send + Sync (required by lambda_runtime::run).
        assert_send_sync(&service);

        // Verify the closure can be called (type-level check; we cannot
        // actually invoke without a real Lambda event envelope but the
        // fact that `service` is accepted by `service_fn` is proven by
        // the Send + Sync + correct return type checks above).
        drop(service);
    }

    // Service-level entry-point envelope tests
    //
    // These invoke the `wrap`-produced service end to end, covering the
    // entry-point envelope handling that the `parse_envelope` unit tests
    // alone cannot reach.

    /// Offline `Options`: a Lambda client built from a static config so the
    /// service never loads ambient AWS configuration. The client is only
    /// exercised by the happy-path test, which makes no AWS calls (single
    /// inline state page, no checkpointed operations).
    fn offline_options() -> Options {
        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();
        Options::builder()
            .lambda_client(aws_sdk_lambda::Client::from_conf(conf))
            .build()
            .expect("offline options build")
    }

    /// Invokes the `wrap`-produced echo service with the given payload.
    async fn invoke_wrap_service(
        payload: serde_json::Value,
    ) -> Result<serde_json::Value, lambda_runtime::Error> {
        let service = wrap(
            |event: serde_json::Value, _ctx: DurableContext| async move {
                Ok::<serde_json::Value, BoxError>(event)
            },
            offline_options(),
        );
        let event = lambda_runtime::LambdaEvent::new(
            InvocationPayload(payload),
            lambda_runtime::Context::default(),
        );
        service(event).await.map(|response| response.0)
    }

    #[tokio::test]
    async fn wrap_service_missing_arn_fails() {
        let payload = serde_json::json!({
            "CheckpointToken": "token-abc",
            "InitialExecutionState": { "Operations": [] }
        });
        let err = invoke_wrap_service(payload).await.expect_err("must fail");
        assert!(
            err.to_string().contains("DurableExecutionArn"),
            "error should name the missing field, got: {err}"
        );
    }

    #[tokio::test]
    async fn wrap_service_missing_token_fails() {
        let payload = serde_json::json!({
            "DurableExecutionArn": "arn:aws:lambda:us-east-1:123456789012:function:test",
            "InitialExecutionState": { "Operations": [] }
        });
        let err = invoke_wrap_service(payload).await.expect_err("must fail");
        assert!(
            err.to_string().contains("CheckpointToken"),
            "error should name the missing field, got: {err}"
        );
    }

    #[tokio::test]
    async fn wrap_service_envelope_free_payload_fails() {
        // An envelope-free payload fails fast at the entry point. There is
        // no raw-payload fallback on the service paths, with or without
        // `test-util`; local testing goes through `LocalRunner` instead.
        let payload = serde_json::json!({ "count": 42 });
        let err = invoke_wrap_service(payload).await.expect_err("must fail");
        assert!(
            err.to_string().contains("not a durable execution envelope"),
            "error should describe the missing envelope, got: {err}"
        );
    }

    #[tokio::test]
    async fn wrap_service_valid_envelope_succeeds() {
        let payload = serde_json::json!({
            "DurableExecutionArn": "arn:aws:lambda:us-east-1:123456789012:function:test",
            "CheckpointToken": "token-abc",
            "InitialExecutionState": {
                "Operations": [{
                    "Id": "root",
                    "Type": "Execution",
                    "Status": "STARTED",
                    "ExecutionDetails": { "InputPayload": "{\"count\":42}" }
                }]
            }
        });
        let response = invoke_wrap_service(payload).await.expect("must succeed");
        assert_eq!(
            response.get("Status").and_then(serde_json::Value::as_str),
            Some("SUCCEEDED"),
            "unexpected response: {response}"
        );
        let result_json = response
            .get("Result")
            .and_then(serde_json::Value::as_str)
            .expect("Result should be a serialized JSON string");
        let echoed: serde_json::Value = serde_json::from_str(result_json).unwrap();
        assert_eq!(echoed, serde_json::json!({ "count": 42 }));
    }

    /// A handler panic leaves the service function with its original
    /// payload, after the SDK cleaned up its tasks, and no envelope is
    /// produced.
    #[tokio::test]
    // reason: the test calls the service on its own task so the panic is
    // observable as that task's `JoinError`.
    #[expect(clippy::disallowed_methods)]
    async fn handler_panic_leaves_sdk_with_original_payload() {
        #[expect(clippy::panic)] // reason: the handler panic under test
        fn boom() -> Result<(), BoxError> {
            panic!("root boom")
        }
        let client: std::sync::Arc<dyn ExecutionClient> =
            std::sync::Arc::new(client::InMemoryExecutionClient::new(Vec::new()));
        let service = wrap_with_provider(
            |_event: serde_json::Value, _ctx: DurableContext| async move { boom() },
            ClientProvider::new(Some(client)),
            Duration::ZERO,
            0,
            std::sync::Arc::from(Vec::new()),
        );
        let payload = serde_json::json!({
            "DurableExecutionArn": "arn:aws:lambda:us-east-1:1:function:test",
            "CheckpointToken": "token-abc",
            "InitialExecutionState": {
                "Operations": [{
                    "Id": "root",
                    "Type": "Execution",
                    "Status": "STARTED",
                    "ExecutionDetails": { "InputPayload": "{}" }
                }]
            }
        });
        let event = lambda_runtime::LambdaEvent::new(
            InvocationPayload(payload),
            lambda_runtime::Context::default(),
        );
        let join_err = tokio::spawn(service(event))
            .await
            .expect_err("the handler panic leaves the service, and no envelope is produced");
        assert!(join_err.is_panic(), "a panic, not a cancellation");
        let payload = join_err.into_panic();
        assert_eq!(payload.downcast_ref::<&str>(), Some(&"root boom"));
    }

    // Bootstrap state read errors

    /// An envelope with the input operation and a `NextMarker`, so the
    /// service must read the next page with `GetDurableExecutionState`.
    fn paginated_envelope() -> serde_json::Value {
        serde_json::json!({
            "DurableExecutionArn": "arn:aws:lambda:us-east-1:1:function:test",
            "CheckpointToken": "token-abc",
            "InitialExecutionState": {
                "Operations": [{
                    "Id": "root",
                    "Type": "Execution",
                    "Status": "STARTED",
                    "ExecutionDetails": { "InputPayload": "{}" }
                }],
                "NextMarker": "page-2"
            }
        })
    }

    /// Invokes a `wrap` service whose Lambda client is `sdk_client` with
    /// [`paginated_envelope`]. Returns the result and whether the handler
    /// ran.
    async fn invoke_paginated(
        sdk_client: aws_sdk_lambda::Client,
    ) -> (Result<serde_json::Value, lambda_runtime::Error>, bool) {
        invoke_paginated_with(
            Options::builder()
                .lambda_client(sdk_client)
                .build()
                .expect("options build"),
        )
        .await
    }

    /// Invokes a `wrap` service built from `options` with
    /// [`paginated_envelope`]. Returns the result and whether the handler
    /// ran.
    async fn invoke_paginated_with(
        options: Options,
    ) -> (Result<serde_json::Value, lambda_runtime::Error>, bool) {
        use std::sync::atomic::{AtomicBool, Ordering};

        let ran = std::sync::Arc::new(AtomicBool::new(false));
        let handler_ran = std::sync::Arc::clone(&ran);
        let service = wrap(
            move |_event: serde_json::Value, _ctx: DurableContext| {
                handler_ran.store(true, Ordering::SeqCst);
                async move { Ok::<(), BoxError>(()) }
            },
            options,
        );
        let event = lambda_runtime::LambdaEvent::new(
            InvocationPayload(paginated_envelope()),
            lambda_runtime::Context::default(),
        );
        let result = service(event).await.map(|response| response.0);
        (result, ran.load(Ordering::SeqCst))
    }

    /// A KMS error on the bootstrap state read fails the execution with a
    /// `FAILED` envelope, as in JS, and the handler does not run.
    #[tokio::test]
    async fn bootstrap_kms_error_returns_failed_envelope() {
        use aws_smithy_mocks::{mock, mock_client};

        let rule =
            mock!(aws_sdk_lambda::Client::get_durable_execution_state).then_http_response(|| {
                client::mock_aws_error_response(400, "KMSAccessDeniedException", "kms denied")
            });
        let (result, ran) = invoke_paginated(mock_client!(aws_sdk_lambda, [&rule])).await;

        let response = result.expect("a KMS error returns an envelope");
        assert_eq!(
            response.get("Status").and_then(serde_json::Value::as_str),
            Some("FAILED"),
            "unexpected response: {response}"
        );
        let error = response
            .get("Error")
            .expect("a FAILED envelope has an Error");
        assert_eq!(
            error.get("ErrorType").and_then(serde_json::Value::as_str),
            Some("KMSAccessDeniedException")
        );
        assert_eq!(
            error
                .get("ErrorMessage")
                .and_then(serde_json::Value::as_str),
            Some("kms denied")
        );
        assert!(!ran, "the handler must not run");
    }

    /// The KMS scenario through the public `LambdaExecutionClient` passed as
    /// `execution_client`: the `FAILED` record carries the AWS error code
    /// and the service's message, read from the `SdkError` source.
    #[tokio::test]
    async fn bootstrap_kms_error_records_aws_error_code() {
        use aws_smithy_mocks::{mock, mock_client};

        let rule =
            mock!(aws_sdk_lambda::Client::get_durable_execution_state).then_http_response(|| {
                client::mock_aws_error_response(400, "KMSAccessDeniedException", "kms denied")
            });
        let client = mock_client!(aws_sdk_lambda, [&rule]);
        let (result, ran) = invoke_paginated_with(
            Options::builder()
                .execution_client(std::sync::Arc::new(LambdaExecutionClient::new(client)))
                .build()
                .expect("options build"),
        )
        .await;

        let response = result.expect("a KMS error returns an envelope");
        assert_eq!(
            response.get("Status").and_then(serde_json::Value::as_str),
            Some("FAILED"),
            "unexpected response: {response}"
        );
        let error = response
            .get("Error")
            .expect("a FAILED envelope has an Error");
        assert_eq!(
            error.get("ErrorType").and_then(serde_json::Value::as_str),
            Some("KMSAccessDeniedException")
        );
        assert_eq!(
            error
                .get("ErrorMessage")
                .and_then(serde_json::Value::as_str),
            Some("kms denied")
        );
        assert!(!ran, "the handler must not run");
    }

    /// A non-retryable checkpoint rejection through the public
    /// `LambdaExecutionClient` records the execution's error as
    /// `CheckpointUnrecoverableExecutionError` with the service's message.
    #[tokio::test]
    async fn lambda_checkpoint_rejection_records_the_service_message() {
        use aws_sdk_lambda::operation::checkpoint_durable_execution::CheckpointDurableExecutionOutput;
        use aws_smithy_mocks::{RuleMode, mock, mock_client};

        let rejected = mock!(aws_sdk_lambda::Client::checkpoint_durable_execution)
            .then_http_response(|| {
                client::mock_aws_error_response(
                    400,
                    "InvalidParameterValueException",
                    "bad payload",
                )
            });
        let accepted =
            mock!(aws_sdk_lambda::Client::checkpoint_durable_execution).then_output(|| {
                CheckpointDurableExecutionOutput::builder()
                    .checkpoint_token("tok-2")
                    .build()
            });
        let client = mock_client!(aws_sdk_lambda, RuleMode::Sequential, [&rejected, &accepted]);
        let service = wrap(
            |_event: serde_json::Value, ctx: DurableContext| async move {
                let value: u32 = ctx.step(|_| async { Ok(7_u32) }).name("one").await?;
                Ok::<u32, BoxError>(value)
            },
            Options::builder()
                .execution_client(std::sync::Arc::new(LambdaExecutionClient::new(client)))
                .build()
                .expect("options build"),
        );
        let event = lambda_runtime::LambdaEvent::new(
            InvocationPayload(serde_json::json!({
                "DurableExecutionArn": "arn:aws:lambda:us-east-1:1:function:test",
                "CheckpointToken": "token-abc",
                "InitialExecutionState": {
                    "Operations": [{
                        "Id": "root",
                        "Type": "Execution",
                        "Status": "STARTED",
                        "ExecutionDetails": { "InputPayload": "{}" }
                    }]
                }
            })),
            lambda_runtime::Context::default(),
        );
        let response = service(event)
            .await
            .map(|response| response.0)
            .expect("a rejected checkpoint returns an envelope");

        assert_eq!(
            response.get("Status").and_then(serde_json::Value::as_str),
            Some("FAILED"),
            "unexpected response: {response}"
        );
        let error = response
            .get("Error")
            .expect("a FAILED envelope has an Error");
        assert_eq!(
            error.get("ErrorType").and_then(serde_json::Value::as_str),
            Some("CheckpointUnrecoverableExecutionError")
        );
        assert_eq!(
            error
                .get("ErrorMessage")
                .and_then(serde_json::Value::as_str),
            Some("[Unrecoverable Execution] Checkpoint failed: bad payload")
        );
    }

    /// A retryable error on the bootstrap state read, a persistent 503 or a
    /// plain 403, fails the invocation, as in JS.
    #[tokio::test]
    async fn bootstrap_retryable_error_fails_the_invocation() {
        use aws_smithy_mocks::{mock, mock_client};

        for status in [503, 403] {
            let rule = mock!(aws_sdk_lambda::Client::get_durable_execution_state)
                .sequence()
                .http_status(status, None)
                .repeatedly()
                .build();
            let (result, ran) = invoke_paginated(mock_client!(aws_sdk_lambda, [&rule])).await;

            let err = result.expect_err("a retryable error fails the invocation");
            assert!(
                err.to_string().contains("failed to paginate initial state"),
                "status {status}: {err}"
            );
            assert!(!ran, "the handler must not run");
        }
    }

    // Status parsing tests (issue #45)

    /// Builds a minimal step operation JSON with the given wire status.
    fn step_op_with_status(status: &str) -> serde_json::Value {
        serde_json::json!({
            "Id": "op-1",
            "Type": "Step",
            "Status": status,
            "StepDetails": {}
        })
    }

    #[test]
    fn parse_status_timed_out_wire_value_maps_to_timed_out() {
        let (_, record) = parse_single_operation(&step_op_with_status("TIMED_OUT")).unwrap();
        assert_eq!(record.status, engine::CheckpointStatus::TimedOut);
    }

    /// `TIMEDOUT` is not a wire value, the smithy model and the Python and
    /// JS SDKs spell it `TIMED_OUT` only, so the parser must not accept it
    /// (the removed pre-#45 arm did).
    #[test]
    fn parse_status_timedout_without_underscore_is_unrecognized() {
        let (_, record) = parse_single_operation(&step_op_with_status("TIMEDOUT")).unwrap();
        assert_eq!(
            record.status,
            engine::CheckpointStatus::Unknown("TIMEDOUT".to_owned())
        );
    }

    /// An unrecognized status maps to `Unknown` carrying the raw value as
    /// received (original casing), never to a guessed known status.
    #[test]
    fn parse_unknown_status_carries_raw_value() {
        let (_, record) = parse_single_operation(&step_op_with_status("Paused")).unwrap();
        assert_eq!(
            record.status,
            engine::CheckpointStatus::Unknown("Paused".to_owned())
        );
    }

    /// Every modeled wire status still maps explicitly, and the parser
    /// stays case-insensitive for them.
    #[test]
    fn parse_known_statuses_map_explicitly() {
        let cases = [
            ("STARTED", engine::CheckpointStatus::Started),
            ("PENDING", engine::CheckpointStatus::Pending),
            ("READY", engine::CheckpointStatus::Ready),
            ("SUCCEEDED", engine::CheckpointStatus::Succeeded),
            ("FAILED", engine::CheckpointStatus::Failed),
            ("CANCELLED", engine::CheckpointStatus::Cancelled),
            ("TIMED_OUT", engine::CheckpointStatus::TimedOut),
            ("STOPPED", engine::CheckpointStatus::Stopped),
            ("Succeeded", engine::CheckpointStatus::Succeeded),
        ];
        for (wire, expected) in cases {
            let (_, record) = parse_single_operation(&step_op_with_status(wire)).unwrap();
            assert_eq!(record.status, expected, "wire status {wire:?}");
        }
    }

    // CallbackDetails parsing tests

    #[test]
    fn parse_callback_details_extracts_result() {
        let op = serde_json::json!({
            "Id": "abc123",
            "Type": "Callback",
            "Status": "SUCCEEDED",
            "CallbackDetails": {
                "CallbackId": "cb-42",
                "Result": "\"hello from callback\""
            }
        });

        let parsed = parse_single_operation(&op);
        assert!(parsed.is_some());
        let (id, record) = parsed.unwrap();
        assert_eq!(id, "abc123");
        assert_eq!(record.status, engine::CheckpointStatus::Succeeded);
        assert_eq!(record.callback_id.as_deref(), Some("cb-42"));
        assert_eq!(record.result.as_deref(), Some("\"hello from callback\""));
    }

    #[test]
    fn parse_callback_details_extracts_error() {
        let op = serde_json::json!({
            "Id": "abc456",
            "Type": "Callback",
            "Status": "FAILED",
            "CallbackDetails": {
                "CallbackId": "cb-99",
                "Error": {
                    "ErrorType": "NotApproved",
                    "ErrorMessage": "request was denied"
                }
            }
        });

        let parsed = parse_single_operation(&op);
        assert!(parsed.is_some());
        let (id, record) = parsed.unwrap();
        assert_eq!(id, "abc456");
        assert_eq!(record.status, engine::CheckpointStatus::Failed);
        assert_eq!(record.callback_id.as_deref(), Some("cb-99"));
        assert_eq!(record.error_type.as_deref(), Some("NotApproved"));
        assert_eq!(record.error_message.as_deref(), Some("request was denied"));
    }

    #[test]
    fn parse_callback_details_result_does_not_override_step_result() {
        // StepDetails.Result takes priority; CallbackDetails.Result is
        // only a fallback for callback-type operations without step data.
        let op = serde_json::json!({
            "Id": "abc789",
            "Type": "Callback",
            "Status": "SUCCEEDED",
            "StepDetails": {
                "Result": "\"from step\""
            },
            "CallbackDetails": {
                "CallbackId": "cb-1",
                "Result": "\"from callback\""
            }
        });

        let parsed = parse_single_operation(&op);
        assert!(parsed.is_some());
        let (_, record) = parsed.unwrap();
        assert_eq!(record.result.as_deref(), Some("\"from step\""));
    }

    #[test]
    fn parse_inline_operations_handles_callback_succeeded() {
        let payload = serde_json::json!({
            "InitialExecutionState": {
                "Operations": [
                    {
                        "Id": "exec-0",
                        "Type": "Execution",
                        "Status": "STARTED"
                    },
                    {
                        "Id": "wire-id-1",
                        "Type": "Callback",
                        "Status": "SUCCEEDED",
                        "CallbackDetails": {
                            "CallbackId": "cb-id-123",
                            "Result": "\"payload\""
                        }
                    }
                ]
            }
        });

        let (log, marker) = parse_inline_operations(&payload);
        assert!(marker.is_none());
        let record = log.get("wire-id-1");
        assert!(record.is_some());
        let record = record.unwrap();
        assert_eq!(record.callback_id.as_deref(), Some("cb-id-123"));
        assert_eq!(record.result.as_deref(), Some("\"payload\""));
    }

    /// When `InitialExecutionState` includes a `NextMarker`, the parser
    /// returns it alongside the parsed operations so the caller can
    /// paginate.
    #[test]
    fn parse_inline_operations_returns_next_marker() {
        let payload = serde_json::json!({
            "InitialExecutionState": {
                "Operations": [
                    {
                        "Id": "exec-0",
                        "Type": "Execution",
                        "Status": "STARTED"
                    },
                    {
                        "Id": "wire-id-1",
                        "Type": "Step",
                        "Status": "SUCCEEDED",
                        "StepDetails": {
                            "Attempt": 1,
                            "Result": "\"hello\""
                        }
                    }
                ],
                "NextMarker": "page-token-2"
            }
        });

        let (log, marker) = parse_inline_operations(&payload);
        // The first page's operation is still parsed.
        let record = log.get("wire-id-1");
        assert!(record.is_some());
        // The marker signals that more pages are available.
        assert_eq!(marker, Some("page-token-2".to_owned()));
    }

    /// An empty `NextMarker` is treated as no marker (no pagination needed).
    #[test]
    fn parse_inline_operations_ignores_empty_marker() {
        let payload = serde_json::json!({
            "InitialExecutionState": {
                "Operations": [
                    {
                        "Id": "exec-0",
                        "Type": "Execution",
                        "Status": "STARTED"
                    }
                ],
                "NextMarker": ""
            }
        });

        let (_log, marker) = parse_inline_operations(&payload);
        assert_eq!(marker, None);
    }

    /// A payload with a `NextMarker` but no `Operations` array still
    /// reports the marker: the service may omit `Operations` on the first
    /// page (e.g. when a large customer payload displaces it), and the
    /// remaining pages must still be fetched rather than silently skipped.
    #[test]
    fn parse_inline_operations_keeps_marker_without_operations() {
        let payload = serde_json::json!({
            "InitialExecutionState": {
                "NextMarker": "page-token-1"
            }
        });

        let (log, marker) = parse_inline_operations(&payload);
        // No operations yet: the first page is empty.
        assert!(!log.has_records());
        // But the marker must survive so bootstrap pagination runs.
        assert_eq!(marker, Some("page-token-1".to_owned()));
    }

    /// Helper to build a Step operation for `resolve_bootstrap_log` tests.
    fn make_test_step_op(id: &str, result: &str) -> aws_sdk_lambda::types::Operation {
        aws_sdk_lambda::types::Operation::builder()
            .id(id)
            .r#type(aws_sdk_lambda::types::OperationType::Step)
            .status(aws_sdk_lambda::types::OperationStatus::Succeeded)
            .start_timestamp(aws_sdk_lambda::primitives::DateTime::from_secs(0))
            .step_details(
                aws_sdk_lambda::types::StepDetails::builder()
                    .result(result)
                    .build(),
            )
            .build()
            .unwrap()
    }

    /// Helper to build an Execution operation carrying `input`.
    fn make_test_execution_op(input: &str) -> aws_sdk_lambda::types::Operation {
        aws_sdk_lambda::types::Operation::builder()
            .id("execution")
            .r#type(aws_sdk_lambda::types::OperationType::Execution)
            .status(aws_sdk_lambda::types::OperationStatus::Started)
            .start_timestamp(aws_sdk_lambda::primitives::DateTime::from_secs(0))
            .execution_details(
                aws_sdk_lambda::types::ExecutionDetails::builder()
                    .input_payload(input)
                    .build(),
            )
            .build()
            .unwrap()
    }

    /// When `initial_marker` is `Some`, `resolve_bootstrap_log` calls
    /// `get_state` (count == 1) and returns a log built from the full
    /// paginated state, and the input of the fetched Execution operation.
    #[tokio::test]
    async fn resolve_bootstrap_log_paginates_when_marker_present() {
        let all_ops = vec![
            make_test_execution_op("{\"fetched\":true}"),
            make_test_step_op("step-1", "\"r1\""),
            make_test_step_op("step-2", "\"r2\""),
        ];
        let client = client::InMemoryExecutionClient::new(all_ops);

        // The inline page is empty and holds no Execution operation.
        let inline_log = engine::CheckpointLog::empty();

        let result = client::resolve_bootstrap_log(
            &client,
            "arn:test",
            "token",
            client::BootstrapState {
                log: inline_log,
                input_payload: None,
                execution: None,
            },
            Some("page-2-marker"),
        )
        .await;

        assert!(result.is_ok());
        let client::BootstrapState {
            log,
            input_payload: input,
            execution,
        } = result.unwrap();
        let execution = execution.expect("the fetched Execution operation");
        assert_eq!(execution.id(), "execution");
        assert_eq!(execution.kind(), Some(OperationKind::Execution));
        assert_eq!(execution.status(), Some(OperationStatus::Started));
        assert_eq!(
            execution.start_timestamp(),
            Some(std::time::UNIX_EPOCH),
            "the fetched start timestamp"
        );
        assert_eq!(
            input.as_deref(),
            Some("{\"fetched\":true}"),
            "the input must come from the fetched Execution operation"
        );
        assert!(
            log.get("execution").is_none(),
            "the Execution operation must not enter the log"
        );
        // Full state from get_state is used.
        assert!(log.get("step-1").is_some(), "step-1 must be in the log");
        assert!(
            log.get("step-2").is_some(),
            "step-2 must be in the log (from page 2)"
        );

        // get_state was called exactly once.
        let count = *client.get_state_call_count.lock().unwrap();
        assert_eq!(
            count, 1,
            "get_state must be called exactly once when marker is present"
        );
    }

    /// When `initial_marker` is `None`, `resolve_bootstrap_log` does NOT
    /// call `get_state` (count == 0) and returns the inline log as-is.
    #[tokio::test]
    async fn resolve_bootstrap_log_skips_pagination_when_no_marker() {
        let client = client::InMemoryExecutionClient::new(Vec::new());

        let inline_log = engine::CheckpointLog::empty();
        // Insert a record to prove the inline log is returned unchanged.
        inline_log.insert(
            "existing-op".to_owned(),
            engine::CheckpointRecord {
                id: "existing-op".to_owned(),
                status: engine::CheckpointStatus::Succeeded,
                result: Some("\"inline\"".to_owned()),
                error_type: None,
                error_message: None,
                error_data: None,
                stack_trace: None,
                attempt: 1,
                invoke_result: None,
                invoke_error_type: None,
                invoke_error_message: None,
                invoke_error_data: None,
                invoke_stack_trace: None,
                replay_children: false,
                callback_id: None,
                op_type: None,
                sub_type: None,
                due_at: None,
                op_name: None,
                parent_id: None,
                start_timestamp: None,
                end_timestamp: None,
            },
        );

        let inline_execution = execution_info_from_json(&serde_json::json!({
            "Id": "inline-exec",
            "Type": "EXECUTION",
            "Status": "STARTED",
            "StartTimestamp": 4000
        }));
        assert!(inline_execution.is_some());
        let result = client::resolve_bootstrap_log(
            &client,
            "arn:test",
            "token",
            client::BootstrapState {
                log: inline_log,
                input_payload: Some("\"inline-input\"".to_owned()),
                execution: inline_execution,
            },
            None,
        )
        .await;

        assert!(result.is_ok());
        let client::BootstrapState {
            log,
            input_payload: input,
            execution,
        } = result.unwrap();
        // The inline execution is returned unchanged.
        let execution = execution.expect("the inline Execution operation");
        assert_eq!(execution.id(), "inline-exec");
        assert_eq!(
            execution.start_timestamp(),
            Some(std::time::UNIX_EPOCH + Duration::from_secs(4))
        );
        // The inline input is returned unchanged.
        assert_eq!(input.as_deref(), Some("\"inline-input\""));
        // The inline log is returned as-is.
        assert!(
            log.get("existing-op").is_some(),
            "inline op must be preserved"
        );

        // get_state was NOT called.
        let count = *client.get_state_call_count.lock().unwrap();
        assert_eq!(
            count, 0,
            "get_state must not be called when no marker is present"
        );
    }

    // Options consumption: client resolution + reuse

    /// A supplied `sdk_config` measurably alters client construction: the
    /// resolved Lambda client carries the region from that config.
    #[test]
    fn sdk_config_measurably_alters_client_construction() {
        let sdk_config = SdkConfig::builder()
            .behavior_version(aws_config::BehaviorVersion::latest())
            .region(aws_sdk_lambda::config::Region::new("eu-west-1"))
            .build();
        let client = base_lambda_client_from_options(Some(sdk_config), None)
            .expect("sdk_config must yield a client");
        assert_eq!(
            client.config().region().map(ToString::to_string),
            Some("eu-west-1".to_owned()),
            "the supplied sdk_config's region must flow into the built client"
        );
    }

    /// A supplied `lambda_client` is the one used (not a default-constructed
    /// one): the resolved client preserves the supplied client's region.
    #[test]
    fn supplied_lambda_client_is_the_one_used() {
        let conf = aws_sdk_lambda::config::Config::builder()
            .behavior_version(aws_sdk_lambda::config::BehaviorVersion::latest())
            .region(aws_sdk_lambda::config::Region::new("ap-south-1"))
            .build();
        let supplied = aws_sdk_lambda::Client::from_conf(conf);
        let resolved = base_lambda_client_from_options(None, Some(supplied))
            .expect("lambda_client must be returned");
        assert_eq!(
            resolved.config().region().map(ToString::to_string),
            Some("ap-south-1".to_owned()),
            "the supplied lambda_client must be used verbatim, not replaced"
        );
    }

    /// With neither `sdk_config` nor `lambda_client`, resolution defers to the
    /// ambient default (returns `None` so `ClientProvider` builds it lazily).
    #[test]
    fn no_options_defers_client_to_ambient_default() {
        assert!(base_lambda_client_from_options(None, None).is_none());
    }

    /// `ClientProvider` reuses a preset execution client across calls rather
    /// than rebuilding one per invocation: two `get()` calls return the same
    /// `Arc` allocation.
    #[tokio::test]
    async fn client_provider_reuses_preset_across_invocations() {
        use crate::client::InMemoryExecutionClient;
        use std::sync::Arc as StdArc;

        let preset: StdArc<dyn ExecutionClient> =
            StdArc::new(InMemoryExecutionClient::new(Vec::new()));
        let provider = ClientProvider::new(Some(StdArc::clone(&preset)));

        let first = provider.get().await;
        let second = provider.get().await;
        assert!(
            StdArc::ptr_eq(&first, &second),
            "the client must be reused, not rebuilt, across invocations"
        );
        assert!(
            StdArc::ptr_eq(&first, &preset),
            "the reused client must be exactly the one supplied via Options"
        );
    }

    // Envelope validation tests

    #[test]
    fn parse_envelope_valid_payload() {
        let payload = serde_json::json!({
            "DurableExecutionArn": "arn:aws:lambda:us-west-2:123456789012:function:test",
            "CheckpointToken": "tok-abc",
            "InitialExecutionState": { "Operations": [] }
        });
        let result = parse_envelope(&payload);
        assert!(result.is_ok());
        let envelope = result.unwrap().expect("envelope should be Some");
        assert_eq!(
            envelope.execution_arn,
            "arn:aws:lambda:us-west-2:123456789012:function:test"
        );
        assert_eq!(envelope.checkpoint_token, "tok-abc");
    }

    #[test]
    fn parse_envelope_missing_arn_errors() {
        let payload = serde_json::json!({
            "CheckpointToken": "tok-abc",
            "InitialExecutionState": { "Operations": [] }
        });
        let err = parse_envelope(&payload).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("DurableExecutionArn") && msg.contains("missing"),
            "error should name the missing field, got: {msg}"
        );
    }

    #[test]
    fn parse_envelope_missing_token_errors() {
        let payload = serde_json::json!({
            "DurableExecutionArn": "arn:test",
            "InitialExecutionState": { "Operations": [] }
        });
        let err = parse_envelope(&payload).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("CheckpointToken") && msg.contains("missing"),
            "error should name the missing field, got: {msg}"
        );
    }

    #[test]
    fn parse_envelope_arn_wrong_type_errors() {
        let payload = serde_json::json!({
            "DurableExecutionArn": 12345,
            "CheckpointToken": "tok-abc",
            "InitialExecutionState": { "Operations": [] }
        });
        let err = parse_envelope(&payload).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("DurableExecutionArn") && msg.contains("not a string"),
            "error should note the type mismatch, got: {msg}"
        );
    }

    #[test]
    fn parse_envelope_token_wrong_type_errors() {
        let payload = serde_json::json!({
            "DurableExecutionArn": "arn:test",
            "CheckpointToken": ["not", "a", "string"],
            "InitialExecutionState": { "Operations": [] }
        });
        let err = parse_envelope(&payload).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("CheckpointToken") && msg.contains("not a string"),
            "error should note the type mismatch, got: {msg}"
        );
    }

    #[test]
    fn envelope_updated_operation_ids_are_read() {
        let ids = |extra: serde_json::Value| {
            let mut payload = serde_json::json!({
                "DurableExecutionArn": "arn:test",
                "CheckpointToken": "tok",
            });
            if let (Some(map), serde_json::Value::Object(extra)) = (payload.as_object_mut(), extra)
            {
                map.extend(extra);
            }
            parse_envelope(&payload)
                .unwrap()
                .unwrap()
                .updated_operation_ids
        };
        assert_eq!(
            ids(serde_json::json!({ "UpdatedOperationIds": ["a", 7, "b"] })),
            ["a", "b"]
        );
        assert_eq!(ids(serde_json::json!({})), Vec::<String>::new());
        assert_eq!(
            ids(serde_json::json!({ "UpdatedOperationIds": "a" })),
            Vec::<String>::new()
        );
    }

    #[test]
    fn inline_operation_parent_and_timestamps() {
        let op = serde_json::json!({
            "Id": "s",
            "Type": "STEP",
            "Status": "STARTED",
            "ParentId": "p",
            "StartTimestamp": 1500,
            "EndTimestamp": "later"
        });
        let (_, record) = parse_single_operation(&op).unwrap();
        assert_eq!(record.parent_id.as_deref(), Some("p"));
        assert_eq!(
            record.start_timestamp,
            Some(std::time::UNIX_EPOCH + Duration::from_millis(1500))
        );
        assert_eq!(record.end_timestamp, None);
    }

    /// The inline path keeps the wire tokens of a type and a status this
    /// SDK version does not know.
    #[test]
    fn unknown_type_and_status_keep_wire_tokens() {
        let op = serde_json::json!({
            "Id": "f",
            "Type": "FUTURE_OP",
            "Status": "FUTURE_STATUS"
        });
        let (id, record) = parse_single_operation(&op).unwrap();
        let info = plugin::OperationInfo::from_record(&id, &record);
        assert_eq!(info.kind(), None);
        assert_eq!(info.status(), None);
        assert_eq!(info.type_str(), Some("FUTURE_OP"));
        assert_eq!(info.status_str(), Some("FUTURE_STATUS"));
    }

    /// `execution_info_from_json` reads an inline `EXECUTION` operation, and
    /// pagination takes the operation from the first fetched page when the
    /// inline page lacks it.
    #[tokio::test]
    async fn bootstrap_state_reads_execution() {
        let inline = execution_info_from_json(&serde_json::json!({
            "Id": "exec",
            "Type": "EXECUTION",
            "Status": "STARTED",
            "StartTimestamp": 2000
        }))
        .expect("an inline EXECUTION operation");
        assert_eq!(
            inline.start_timestamp(),
            Some(std::time::UNIX_EPOCH + Duration::from_secs(2))
        );

        let fetched_execution = aws_sdk_lambda::types::Operation::builder()
            .id("exec")
            .r#type(aws_sdk_lambda::types::OperationType::Execution)
            .status(aws_sdk_lambda::types::OperationStatus::Started)
            .start_timestamp(aws_sdk_lambda::primitives::DateTime::from_secs(3))
            .build()
            .unwrap();
        let client = client::InMemoryExecutionClient::new(vec![
            fetched_execution,
            make_test_step_op("s1", "\"r\""),
        ]);
        let state = client::resolve_bootstrap_log(
            &client,
            "arn:test",
            "token",
            client::BootstrapState {
                log: engine::CheckpointLog::empty(),
                input_payload: None,
                execution: None,
            },
            Some("m"),
        )
        .await
        .unwrap();
        assert_eq!(
            state.execution.and_then(|e| e.start_timestamp()),
            Some(std::time::UNIX_EPOCH + Duration::from_secs(3))
        );
    }

    /// An inline operation of another type, or without an `Id` or a
    /// `Type`, is no `EXECUTION` operation. A missing `Status` reads as
    /// `STARTED`.
    #[test]
    fn execution_info_from_json_rejects_other_operations() {
        assert!(
            execution_info_from_json(&serde_json::json!({ "Id": "s", "Type": "STEP" })).is_none()
        );
        assert!(execution_info_from_json(&serde_json::json!({ "Type": "EXECUTION" })).is_none());
        assert!(execution_info_from_json(&serde_json::json!({ "Id": "e" })).is_none());
        let info = execution_info_from_json(&serde_json::json!({
            "Id": "e",
            "Type": "Execution",
            "ParentId": "p",
            "EndTimestamp": 5000
        }))
        .expect("the PascalCase spelling is an EXECUTION operation");
        assert_eq!(info.status(), Some(OperationStatus::Started));
        assert_eq!(info.parent_id(), Some("p"));
        assert_eq!(
            info.end_timestamp(),
            Some(std::time::UNIX_EPOCH + Duration::from_secs(5))
        );
    }

    /// A factory that records every `InvocationInfo` it receives and
    /// returns no plugin.
    #[derive(Clone, Default)]
    struct RecordingFactory(std::sync::Arc<std::sync::Mutex<Vec<plugin::InvocationInfo>>>);

    impl plugin::PluginFactory for RecordingFactory {
        fn create_plugin(
            &self,
            info: &plugin::InvocationInfo,
        ) -> Result<Option<Box<dyn plugin::Plugin>>, BoxError> {
            self.0.lock().unwrap().push(info.clone());
            Ok(None)
        }
    }

    /// Invokes a `wrap` service with the in-memory transport and `factory`,
    /// and returns the infos the factory received.
    async fn infos_for(
        payload: serde_json::Value,
        lambda_ctx: lambda_runtime::Context,
    ) -> Vec<plugin::InvocationInfo> {
        let factory = RecordingFactory::default();
        let options = Options::builder()
            .execution_client(std::sync::Arc::new(client::InMemoryExecutionClient::new(
                Vec::new(),
            )))
            .plugin(factory.clone())
            .build()
            .unwrap();
        let service = wrap(
            |_event: serde_json::Value, _ctx: DurableContext| async move {
                Ok::<serde_json::Value, BoxError>(serde_json::Value::Null)
            },
            options,
        );
        service(lambda_runtime::LambdaEvent::new(
            InvocationPayload(payload),
            lambda_ctx,
        ))
        .await
        .unwrap();
        factory.0.lock().unwrap().clone()
    }

    fn envelope_with(operations: &serde_json::Value) -> serde_json::Value {
        serde_json::json!({
            "DurableExecutionArn": "arn:test",
            "CheckpointToken": "tok",
            "InitialExecutionState": { "Operations": operations }
        })
    }

    #[tokio::test]
    async fn operations_map_contains_execution() {
        let payload = envelope_with(&serde_json::json!([
            {
                "Id": "exec-1",
                "Type": "EXECUTION",
                "Status": "STARTED",
                "StartTimestamp": 7000,
                "ExecutionDetails": { "InputPayload": "{}" }
            },
            { "Id": "s1", "Type": "STEP", "Status": "SUCCEEDED", "ParentId": "exec-1" },
            { "Id": "c1", "Type": "CONTEXT", "Status": "STARTED" }
        ]));
        let infos = infos_for(payload, lambda_runtime::Context::default()).await;
        assert_eq!(infos.len(), 1);
        let operations = infos.first().unwrap().operations();
        let exec = operations.get("exec-1").expect("the EXECUTION operation");
        assert_eq!(exec.kind(), Some(OperationKind::Execution));
        assert_eq!(exec.status(), Some(OperationStatus::Started));
        assert_eq!(exec.status_str(), Some("STARTED"));
        assert_eq!(
            exec.start_timestamp(),
            Some(std::time::UNIX_EPOCH + Duration::from_secs(7))
        );
        assert_eq!(
            operations
                .get("s1")
                .and_then(plugin::OperationInfo::parent_id),
            Some("exec-1")
        );
        assert_eq!(
            operations.get("c1").map(plugin::OperationInfo::parent_id),
            Some(None)
        );
        assert_eq!(
            infos.first().unwrap().execution_start_timestamp(),
            Some(std::time::UNIX_EPOCH + Duration::from_secs(7))
        );
        assert!(!infos.first().unwrap().is_first_invocation());
    }

    #[tokio::test]
    async fn invocation_info_reads_lambda_context() {
        let payload = || {
            envelope_with(&serde_json::json!([{
                "Id": "exec-1",
                "Type": "EXECUTION",
                "Status": "STARTED",
                "ExecutionDetails": { "InputPayload": "{\"k\":1}" }
            }]))
        };
        let mut lambda_ctx = lambda_runtime::Context::default();
        lambda_ctx.request_id = "r-1".to_owned();
        lambda_ctx.xray_trace_id = Some("Root=1-abc".to_owned());
        let infos = infos_for(payload(), lambda_ctx).await;
        let info = infos.first().unwrap();
        assert_eq!(info.request_id(), "r-1");
        assert_eq!(info.x_ray_trace_id(), Some("Root=1-abc"));
        assert_eq!(info.execution_arn(), "arn:test");
        assert_eq!(info.execution_input(), "{\"k\":1}");
        assert!(info.is_first_invocation());

        let mut lambda_ctx = lambda_runtime::Context::default();
        lambda_ctx.xray_trace_id = None;
        let infos = infos_for(payload(), lambda_ctx).await;
        assert_eq!(infos.first().unwrap().x_ray_trace_id(), None);
    }

    #[tokio::test]
    async fn invocation_info_defaults_input() {
        let payload = envelope_with(&serde_json::json!([{
            "Id": "exec-1",
            "Type": "EXECUTION",
            "Status": "STARTED"
        }]));
        let infos = infos_for(payload, lambda_runtime::Context::default()).await;
        assert_eq!(infos.first().unwrap().execution_input(), "{}");
    }

    /// A plugin whose destructor panics with `plugin drop`.
    struct PanickingDropPlugin;

    impl plugin::Plugin for PanickingDropPlugin {}

    impl Drop for PanickingDropPlugin {
        #[expect(clippy::panic)] // reason: the destructor panic under test
        fn drop(&mut self) {
            panic!("plugin drop");
        }
    }

    /// An error whose destructor panics with `error drop`.
    #[derive(Debug)]
    struct PanickingDropError;

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

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

    impl Drop for PanickingDropError {
        #[expect(clippy::panic)] // reason: the destructor panic under test
        fn drop(&mut self) {
            panic!("error drop");
        }
    }

    /// A factory that returns a plugin whose destructor panics, or, with
    /// `failing`, an error whose destructor panics.
    struct PanickingDropFactory {
        failing: bool,
    }

    impl plugin::PluginFactory for PanickingDropFactory {
        fn create_plugin(
            &self,
            _info: &plugin::InvocationInfo,
        ) -> Result<Option<Box<dyn plugin::Plugin>>, BoxError> {
            if self.failing {
                Err(Box::new(PanickingDropError))
            } else {
                Ok(Some(Box::new(PanickingDropPlugin)))
            }
        }
    }

    /// A `wrap` service with the in-memory transport, `factory` and
    /// `handler`, and an envelope for it.
    fn service_with_factory<F, Fut>(
        factory: PanickingDropFactory,
        handler: F,
    ) -> (
        impl Fn(lambda_runtime::LambdaEvent<InvocationPayload>) -> BoxedInvocationFuture,
        lambda_runtime::LambdaEvent<InvocationPayload>,
    )
    where
        F: Fn(serde_json::Value, DurableContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<serde_json::Value, BoxError>> + Send,
    {
        let options = Options::builder()
            .execution_client(std::sync::Arc::new(client::InMemoryExecutionClient::new(
                Vec::new(),
            )))
            .plugin(factory)
            .build()
            .unwrap();
        let payload = envelope_with(&serde_json::json!([{
            "Id": "exec-1",
            "Type": "EXECUTION",
            "Status": "STARTED",
            "ExecutionDetails": { "InputPayload": "{}" }
        }]));
        let event = lambda_runtime::LambdaEvent::new(
            InvocationPayload(payload),
            lambda_runtime::Context::default(),
        );
        (wrap(handler, options), event)
    }

    /// A plugin destructor that panics after a successful handler is
    /// logged and discarded, and the invocation still reports success.
    #[tokio::test]
    async fn plugin_drop_panic_keeps_successful_outcome() {
        let (service, event) = service_with_factory(
            PanickingDropFactory { failing: false },
            |_event, _ctx| async move { Ok(serde_json::json!("done")) },
        );
        let json = service(event).await.unwrap().0;
        assert_eq!(
            json.get("Status"),
            Some(&serde_json::json!("SUCCEEDED")),
            "{json}"
        );
        assert_eq!(
            json.get("Result"),
            Some(&serde_json::json!("\"done\"")),
            "{json}"
        );
    }

    /// A plugin destructor that panics while the handler's panic is about
    /// to resume does not abort, and the handler's own payload leaves the
    /// SDK.
    #[tokio::test]
    // reason: the test calls the service on its own task so the panic is
    // observable as that task's `JoinError`.
    #[expect(clippy::disallowed_methods)]
    async fn plugin_drop_panic_while_handler_panics_resumes_handler_panic() {
        #[expect(clippy::panic)] // reason: the handler panic under test
        fn boom() -> Result<serde_json::Value, BoxError> {
            panic!("root boom")
        }
        let (service, event) = service_with_factory(
            PanickingDropFactory { failing: false },
            |_event, _ctx| async move { boom() },
        );
        let join_err = tokio::spawn(service(event))
            .await
            .expect_err("the handler panic leaves the service");
        assert!(join_err.is_panic(), "a panic, not a cancellation");
        let payload = join_err.into_panic();
        assert_eq!(payload.downcast_ref::<&str>(), Some(&"root boom"));
    }

    /// A factory error whose destructor panics fails the invocation with
    /// that panic, which the invocation coordinator recorded.
    #[tokio::test]
    // reason: the test calls the service on its own task so the panic is
    // observable as that task's `JoinError`.
    #[expect(clippy::disallowed_methods)]
    async fn factory_error_destructor_panic_fails_the_invocation() {
        let (service, event) = service_with_factory(
            PanickingDropFactory { failing: true },
            |_event, _ctx| async move { Ok(serde_json::Value::Null) },
        );
        let join_err = tokio::spawn(service(event))
            .await
            .expect_err("the destructor panic leaves the service");
        assert!(join_err.is_panic(), "a panic, not a cancellation");
        let payload = join_err.into_panic();
        assert_eq!(payload.downcast_ref::<&str>(), Some(&"error drop"));
    }

    // The invocation end hooks, through the `wrap` service.

    use plugin::test_support::{self as hooks, Hooks};

    /// The service returns the `SUCCEEDED` envelope at the deadline cut-off
    /// even though the end hook never finishes.
    #[tokio::test(start_paused = true)]
    #[expect(clippy::panic)] // reason: reports that no attempt captured the warning
    async fn end_hook_cut_off_before_deadline() {
        for _attempt in 0..5 {
            let (buffer, _guard) = hooks::capture();
            let plugin = Hooks::default().on_end(|_| Box::pin(std::future::pending()));
            let (service, event) = hooks::service(
                vec![plugin],
                hooks::deadline_in(1_000),
                |_e, _ctx| async move { Ok(serde_json::json!("ok")) },
            );
            let json = service(event).await.unwrap().0;
            assert_eq!(json.get("Status"), Some(&serde_json::json!("SUCCEEDED")));
            let warnings = hooks::warnings(&buffer);
            let [_] = hooks::with_message(
                &warnings,
                "plugin on_invocation_end hooks did not finish before the invocation deadline; \
                 the response returns without them",
            )[..] else {
                continue;
            };
            return;
        }
        panic!("no attempt captured the warning");
    }

    #[tokio::test(start_paused = true)]
    async fn end_hook_unbounded_without_deadline() {
        let flag = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let plugin = {
            let flag = std::sync::Arc::clone(&flag);
            Hooks::default().on_end(move |_| {
                let flag = std::sync::Arc::clone(&flag);
                Box::pin(async move {
                    tokio::time::sleep(Duration::from_millis(100)).await;
                    flag.store(true, std::sync::atomic::Ordering::SeqCst);
                    Ok(())
                })
            })
        };
        let (service, event) = hooks::service(vec![plugin], 0, |_e, _ctx| async move {
            Ok(serde_json::Value::Null)
        });
        service(event).await.unwrap();
        assert!(flag.load(std::sync::atomic::Ordering::SeqCst));
    }

    /// The end info is built once per end, whatever the number of plugins:
    /// the log is projected the same number of times with one plugin as
    /// with three.
    #[tokio::test]
    async fn end_info_projects_operations_once() {
        async fn thousand_steps(
            _event: serde_json::Value,
            ctx: DurableContext,
        ) -> Result<serde_json::Value, BoxError> {
            for i in 1..=1_000_u32 {
                ctx.step(move |_| async move { Ok(i) }).await?;
            }
            Ok(serde_json::Value::Null)
        }
        // The steps are recorded, so the log holds them at the end: the
        // in-memory transport returns no operation state.
        let recorded = || {
            (1..=1_000_u32)
                .map(|i| {
                    serde_json::json!({
                        "Id": engine::compute_wire_id_public(&i.to_string()),
                        "Type": "STEP",
                        "SubType": "Step",
                        "Status": "SUCCEEDED",
                        "StepDetails": { "Result": i.to_string() }
                    })
                })
                .collect::<Vec<_>>()
        };
        let mut growths = Vec::new();
        for count in [1, 3] {
            let plugins: Vec<Hooks> = (0..count).map(|_| Hooks::default()).collect();
            let (service, event) =
                hooks::service_with(plugins.clone(), 0, recorded(), thousand_steps);
            let before = engine::projections();
            service(event).await.unwrap();
            growths.push(engine::projections() - before);
            for plugin in &plugins {
                let ends = plugin.ends();
                let [end] = ends.as_slice() else {
                    unreachable!("one end info per plugin, got {}", ends.len());
                };
                assert_eq!(end.operations().len(), 1_001);
            }
        }
        let [one, three] = growths.as_slice() else {
            unreachable!("two runs");
        };
        assert_eq!(one, three);
    }

    /// The string at `pointer` in `json`.
    fn json_str<'a>(json: &'a serde_json::Value, pointer: &str) -> Option<&'a str> {
        json.pointer(pointer).and_then(serde_json::Value::as_str)
    }

    /// Runs `root` through `finish_invocation` on a context whose
    /// coalescer holds an unflushed step `SUCCEED`, over a client that
    /// rejects the first request non-retryably. Returns the response, the
    /// end hook's info and the updates the client received.
    async fn finish_with_failing_flush<R>(
        root: impl FnOnce(DurableContext) -> R,
    ) -> (
        serde_json::Value,
        plugin::InvocationEndInfo,
        Vec<aws_sdk_lambda::types::OperationUpdate>,
    )
    where
        R: Future<Output = Result<String, WireError>> + Send + 'static,
    {
        let client = std::sync::Arc::new(client::InMemoryExecutionClient::new(Vec::new()));
        client.enqueue_checkpoint_response(client::TestResponse::NonRetryableError(
            "flush down".to_owned(),
        ));
        let hook = Hooks::default();
        let plugins = hooks::plugins_of(hook.clone());
        let ctx = DurableContext::new_root_with_client_and_defaults(
            "arn:aws:lambda:us-east-1:1:function:test".to_owned(),
            lambda_runtime::Context::default(),
            std::sync::Arc::new(engine::CheckpointLog::empty()),
            std::sync::Arc::clone(&client) as std::sync::Arc<dyn ExecutionClient>,
            "t0".to_owned(),
            Duration::from_secs(60),
            0,
            None,
        );
        let update = aws_sdk_lambda::types::OperationUpdate::builder()
            .id("s")
            .name("s")
            .r#type(aws_sdk_lambda::types::OperationType::Step)
            .action(aws_sdk_lambda::types::OperationAction::Succeed)
            .payload("1")
            .build()
            .expect("all required fields set");
        ctx.buffer_unflushed(vec![update]);
        let end_log = engine::CheckpointLog::empty();
        let finished = finish_invocation(
            root(ctx.clone()),
            task::TaskContext::current(),
            std::sync::Arc::clone(ctx.task_ownership()),
            std::sync::Arc::clone(ctx.coordinator()),
            &ctx,
            None,
            Some(&plugins),
            &end_log,
        )
        .await;
        let Finished::Respond(response, _late) = finished else {
            unreachable!("the invocation answers");
        };
        let ends = hook.ends();
        let [end] = ends.as_slice() else {
            unreachable!("one end hook, got {}", ends.len());
        };
        (
            response.expect("an envelope").0,
            end.clone(),
            client.recorded_updates(),
        )
    }

    /// A batch that fails in the end-of-invocation flush with an
    /// execution-scoped error, after the handler returned `Err("boom")`,
    /// decides the outcome: `FAILED` with
    /// `CheckpointUnrecoverableExecutionError`, the end hook receives it,
    /// and no `FAIL` is sent.
    #[tokio::test]
    async fn flush_failure_decides_over_handler_error() {
        let (json, end, updates) = finish_with_failing_flush(|_ctx| async {
            Err(WireError::new(Some("Error"), Some("boom")))
        })
        .await;
        assert_eq!(json_str(&json, "/Status"), Some("FAILED"), "{json}");
        assert_eq!(
            json_str(&json, "/Error/ErrorType"),
            Some(error::CHECKPOINT_EXECUTION_FAILED_ERROR_TYPE)
        );
        assert_eq!(end.status(), InvocationStatus::Failed);
        assert_eq!(
            end.execution_error().and_then(WireError::error_type),
            Some(error::CHECKPOINT_EXECUTION_FAILED_ERROR_TYPE)
        );
        assert_eq!(updates.len(), 1, "only the flushed SUCCEED: {updates:?}");
        assert!(
            updates
                .iter()
                .all(|u| u.action() != &aws_sdk_lambda::types::OperationAction::Fail),
            "{updates:?}"
        );
    }

    /// A fatal error that won the drive's arbitration keeps the outcome
    /// when the end-of-invocation flush then fails.
    #[tokio::test]
    async fn flush_failure_after_a_winning_fatal_keeps_the_fatal() {
        let (json, end, _updates) = finish_with_failing_flush(|ctx| async move {
            ctx.coordinator()
                .record_fatal("UsageError".to_owned(), "misuse".to_owned());
            std::future::pending().await
        })
        .await;
        assert_eq!(json_str(&json, "/Status"), Some("FAILED"), "{json}");
        assert_eq!(json_str(&json, "/Error/ErrorType"), Some("UsageError"));
        assert_eq!(
            end.execution_error().and_then(WireError::error_type),
            Some("UsageError")
        );
    }

    /// A late panic fails only an invocation that returns a response and
    /// where no fatal error won the drive. A Lambda error, a fatal error and
    /// a panic each already fail the invocation.
    #[test]
    fn late_panics_fail_only_a_response_without_a_fatal_error() {
        use crate::plugin::LatePanics;
        let ok = Ok(InvocationResponse(serde_json::Value::Null));
        let err = Err(lambda_runtime::Error::from("fault"));
        assert_eq!(late_panics(Some(&ok), false), LatePanics::Fail);
        assert_eq!(late_panics(Some(&err), false), LatePanics::Discard);
        assert_eq!(late_panics(None, false), LatePanics::Discard);
        assert_eq!(late_panics(Some(&ok), true), LatePanics::Discard);
    }

    /// Runs `future` on its own task and returns its panic payload.
    #[expect(clippy::disallowed_methods)]
    // reason: the test calls the service on its own task so the panic is
    // observable as that task's `JoinError`.
    async fn panic_payload_of<Fut>(future: Fut) -> Box<dyn std::any::Any + Send>
    where
        Fut: Future + Send + 'static,
        Fut::Output: Send,
    {
        match tokio::spawn(future).await {
            Ok(_) => unreachable!("the service must panic"),
            Err(join_err) => join_err.into_panic(),
        }
    }

    #[tokio::test]
    #[expect(clippy::panic)] // reason: the handler panics under test
    async fn end_hook_retrying_after_panic() {
        let messages = [
            "boom",
            "boom",
            "the invocation panicked with a payload that is not a string",
        ];
        for (case, message) in messages.into_iter().enumerate() {
            let plugin = Hooks::default();
            let (service, event) =
                hooks::service(vec![plugin.clone()], 0, move |_e, _ctx| async move {
                    match case {
                        0 => panic!("boom"),
                        1 => panic!("{}", String::from("boom")),
                        _ => std::panic::panic_any(42_u32),
                    }
                });
            let resumed = panic_payload_of(service(event)).await;
            let original = match case {
                0 => resumed.downcast_ref::<&str>() == Some(&"boom"),
                1 => resumed.downcast_ref::<String>().map(String::as_str) == Some("boom"),
                _ => resumed.downcast_ref::<u32>() == Some(&42),
            };
            assert!(original, "case {case}: the original payload leaves the SDK");
            let ends = plugin.ends();
            let [end] = ends.as_slice() else {
                unreachable!("one end info, got {}", ends.len());
            };
            assert_eq!(end.status(), InvocationStatus::Retrying);
            assert_eq!(end.execution_result(), None);
            let error = end.execution_error().expect("a panic end has an error");
            assert_eq!(error.error_type(), None);
            assert_eq!(error.error_message(), Some(message));
        }
    }

    #[tokio::test]
    #[expect(clippy::panic)] // reason: the handler panic under test
    async fn end_hook_after_panic_runs_after_cleanup() {
        struct Guard(std::sync::Arc<std::sync::atomic::AtomicBool>);
        impl Drop for Guard {
            fn drop(&mut self) {
                self.0.store(true, std::sync::atomic::Ordering::SeqCst);
            }
        }
        let dropped = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let seen = std::sync::Arc::new(std::sync::Mutex::new(None));
        let plugin = {
            let dropped = std::sync::Arc::clone(&dropped);
            let seen = std::sync::Arc::clone(&seen);
            Hooks::default().on_end(move |_| {
                *seen.lock().unwrap() = Some(dropped.load(std::sync::atomic::Ordering::SeqCst));
                Box::pin(std::future::ready(Ok(())))
            })
        };
        let (service, event) = hooks::service(vec![plugin], 0, move |_e, ctx| {
            let dropped = std::sync::Arc::clone(&dropped);
            async move {
                let _held = ctx
                    .step(move |_| {
                        let guard = Guard(std::sync::Arc::clone(&dropped));
                        async move {
                            let _guard = guard;
                            std::future::pending::<()>().await;
                            Ok(0_u32)
                        }
                    })
                    .spawn();
                tokio::time::sleep(Duration::from_millis(10)).await;
                panic!("boom")
            }
        });
        let payload = panic_payload_of(service(event)).await;
        assert_eq!(payload.downcast_ref::<&str>(), Some(&"boom"));
        assert_eq!(*seen.lock().unwrap(), Some(true));
    }

    /// The plugins are dropped before the panic resumes, so no plugin
    /// destructor runs while it unwinds: every context, which holds them,
    /// is dropped first.
    #[tokio::test]
    #[expect(clippy::panic)] // reason: the handler panic under test
    async fn plugins_drop_before_the_panic_resumes() {
        struct Probe(std::sync::Arc<std::sync::Mutex<Option<bool>>>);
        impl plugin::Plugin for Probe {}
        impl Drop for Probe {
            fn drop(&mut self) {
                *self.0.lock().unwrap() = Some(std::thread::panicking());
            }
        }
        struct ProbeFactory(std::sync::Arc<std::sync::Mutex<Option<bool>>>);
        impl plugin::PluginFactory for ProbeFactory {
            fn create_plugin(
                &self,
                _info: &plugin::InvocationInfo,
            ) -> Result<Option<Box<dyn plugin::Plugin>>, BoxError> {
                Ok(Some(Box::new(Probe(std::sync::Arc::clone(&self.0)))))
            }
        }
        let unwinding = std::sync::Arc::new(std::sync::Mutex::new(None));
        let options = Options::builder()
            .execution_client(std::sync::Arc::new(client::InMemoryExecutionClient::new(
                Vec::new(),
            )))
            .plugin(ProbeFactory(std::sync::Arc::clone(&unwinding)))
            .build()
            .unwrap();
        let service = wrap(
            |_e: serde_json::Value, _ctx: DurableContext| async move {
                panic!("boom");
                #[expect(unreachable_code)] // reason: names the handler's output type
                Ok::<serde_json::Value, BoxError>(serde_json::Value::Null)
            },
            options,
        );
        let event = lambda_runtime::LambdaEvent::new(
            InvocationPayload(envelope_with(&serde_json::json!([{
                "Id": "exec-1",
                "Type": "EXECUTION",
                "Status": "STARTED",
                "ExecutionDetails": { "InputPayload": "{}" }
            }]))),
            lambda_runtime::Context::default(),
        );
        let payload = panic_payload_of(service(event)).await;
        assert_eq!(payload.downcast_ref::<&str>(), Some(&"boom"));
        assert_eq!(*unwinding.lock().unwrap(), Some(false));
    }

    #[tokio::test]
    #[expect(clippy::panic)] // reason: the hook and handler panics under test, and the report of no capture
    async fn panicking_end_hook_keeps_the_original_panic() {
        for _attempt in 0..5 {
            let (buffer, _guard) = hooks::capture();
            let faulty = Hooks::default().named("Faulty").on_end(|_| panic!("hook"));
            let healthy = Hooks::default();
            let (service, event) =
                hooks::service(vec![faulty, healthy.clone()], 0, |_e, _ctx| async move {
                    panic!("boom")
                });
            let payload = panic_payload_of(service(event)).await;
            assert_eq!(payload.downcast_ref::<&str>(), Some(&"boom"));
            let ends = healthy.ends();
            let [end] = ends.as_slice() else {
                unreachable!("one end info, got {}", ends.len());
            };
            assert_eq!(end.status(), InvocationStatus::Retrying);
            let warnings = hooks::warnings(&buffer);
            let [warning] =
                hooks::with_message(&warnings, "plugin hook panicked; the panic is ignored")[..]
            else {
                continue;
            };
            assert!(
                hooks::field(warning, "plugin").ends_with("Faulty (plugins[0])"),
                "{warning}"
            );
            assert_eq!(hooks::field(warning, "hook"), "on_invocation_end");
            return;
        }
        panic!("no attempt captured the warning");
    }

    /// The plugin of the post-drive claim tests. `AfterNext` claims once,
    /// in the code `wrap_invocation` runs after `next()`, in the first poll
    /// after the drive ended (the coordinator's phase is no longer
    /// `Running`). `OperationStart` claims in `on_operation_start`, while
    /// the handler runs.
    #[derive(Clone, Copy, PartialEq)]
    enum ClaimAt {
        AfterNext,
        OperationStart,
    }

    type ContextSlot = std::sync::Arc<std::sync::Mutex<Option<DurableContext>>>;

    #[derive(Clone)]
    struct ClaimPlugin {
        at: ClaimAt,
        slot: ContextSlot,
        claims: std::sync::Arc<std::sync::atomic::AtomicUsize>,
        /// Set by `test_hooks::pending_once_after_drive` in the poll it
        /// makes return `Pending`; `AfterNext` claims only after that.
        armed: std::sync::Arc<std::sync::atomic::AtomicBool>,
    }

    impl ClaimPlugin {
        fn new(at: ClaimAt) -> Self {
            Self {
                at,
                slot: std::sync::Arc::default(),
                claims: std::sync::Arc::default(),
                armed: std::sync::Arc::default(),
            }
        }

        fn claims(&self) -> usize {
            self.claims.load(std::sync::atomic::Ordering::SeqCst)
        }

        fn claim_once(&self, ctx: &DurableContext) {
            if self
                .claims
                .fetch_add(1, std::sync::atomic::Ordering::SeqCst)
                == 0
            {
                drop(ctx.wait(Duration::from_secs(1)));
            }
        }
    }

    impl plugin::Plugin for ClaimPlugin {
        fn on_operation_start(&self, _info: &plugin::OperationInfo) -> plugin::HookWork {
            if self.at == ClaimAt::OperationStart
                && let Some(ctx) = self.slot.lock().unwrap().clone()
            {
                self.claim_once(&ctx);
            }
            Ok(None)
        }

        fn wrap_invocation(&self, _info: &plugin::InvocationInfo, next: &mut dyn FnMut()) {
            next();
            let ctx = self.slot.lock().unwrap().clone();
            if self.at == ClaimAt::AfterNext
                && let Some(ctx) = ctx
                && ctx.coordinator().phase() != coordinator::Phase::Running
                && self.armed.load(std::sync::atomic::Ordering::SeqCst)
            {
                self.claim_once(&ctx);
            }
        }
    }

    impl plugin::PluginFactory for ClaimPlugin {
        fn create_plugin(
            &self,
            _info: &plugin::InvocationInfo,
        ) -> Result<Option<Box<dyn plugin::Plugin>>, BoxError> {
            Ok(Some(Box::new(self.clone())))
        }
    }

    /// An end hook error whose destructor panics with "late drop".
    #[derive(Debug)]
    struct LateDropError;

    impl std::fmt::Display for LateDropError {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.write_str("late drop error")
        }
    }

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

    impl Drop for LateDropError {
        #[expect(clippy::panic)] // reason: the destructor panic under test
        fn drop(&mut self) {
            panic!("late drop");
        }
    }

    /// The first execution's event, with only the `EXECUTION` operation.
    fn first_invocation_event() -> lambda_runtime::LambdaEvent<InvocationPayload> {
        lambda_runtime::LambdaEvent::new(
            InvocationPayload(envelope_with(&serde_json::json!([{
                "Id": "exec-1",
                "Type": "EXECUTION",
                "Status": "STARTED",
                "ExecutionDetails": { "InputPayload": "{}" }
            }]))),
            lambda_runtime::Context::default(),
        )
    }

    /// A claim after the drive cannot change the late-panic policy, which
    /// uses the value of whether a fatal error won the drive's arbitration.
    /// The crate-private pending point makes `finish_invocation` return
    /// `Pending` once right after the drive returns, and `wrap_invocation`
    /// calls `wait` after `next()` in that poll, before the end hooks run.
    /// The claim records the context usage error, but the end hook's error,
    /// whose destructor panics, still fails the invocation with that late
    /// panic. The only WARN is the end hook's error.
    #[tokio::test]
    // reason: the test calls the service on its own task so the panic is
    // observable as that task's `JoinError`.
    #[expect(clippy::disallowed_methods)]
    async fn late_panic_after_post_drive_claim_fails_the_invocation() {
        let (buffer, _guard) = hooks::capture();
        let claimer = ClaimPlugin::new(ClaimAt::AfterNext);
        let claims_at_end = std::sync::Arc::new(std::sync::Mutex::new(None));
        let failing = {
            let claimer = claimer.clone();
            let claims_at_end = std::sync::Arc::clone(&claims_at_end);
            Hooks::default().on_end(move |_| {
                *claims_at_end.lock().unwrap() = Some(claimer.claims());
                Box::pin(async { Err(Box::new(LateDropError) as BoxError) })
            })
        };
        let options = Options::builder()
            .execution_client(std::sync::Arc::new(client::InMemoryExecutionClient::new(
                Vec::new(),
            )))
            .plugin(claimer.clone())
            .plugin(failing)
            .build()
            .unwrap();
        let slot = std::sync::Arc::clone(&claimer.slot);
        let service = wrap(
            move |_e: serde_json::Value, ctx: DurableContext| {
                *slot.lock().unwrap() = Some(ctx);
                async { Ok::<_, BoxError>(serde_json::json!("done")) }
            },
            options,
        );
        // The pending point arms the claim only in the poll it makes return
        // `Pending` after `drive_invocation` returned.
        let joined = tokio::spawn(test_hooks::PENDING_AFTER_DRIVE.scope(
            std::sync::Arc::clone(&claimer.armed),
            service(first_invocation_event()),
        ))
        .await;
        let payload = match joined {
            Ok(response) => unreachable!(
                "the late panic must fail the invocation: {:?}",
                response.map(|r| r.0)
            ),
            Err(join_err) => join_err.into_panic(),
        };
        assert_eq!(payload.downcast_ref::<&str>(), Some(&"late drop"));
        assert_eq!(
            *claims_at_end.lock().unwrap(),
            Some(1),
            "the claim ran after next() in the post-drive pending poll, before the end hooks"
        );
        let ctx = claimer
            .slot
            .lock()
            .unwrap()
            .take()
            .expect("the handler ran");
        let fatal = ctx
            .coordinator()
            .fatal_error()
            .map(|fatal| fatal.error_message);
        assert!(
            fatal
                .as_deref()
                .is_some_and(|m| m.contains("inside plugin code")),
            "the claim records the context usage error: {fatal:?}"
        );
        // Exactly one plugin WARN: the end hook's error. The claim logs
        // nothing.
        let warnings = hooks::warnings(&buffer);
        assert_eq!(
            hooks::with_message(&warnings, "plugin hook failed; the error is ignored").len(),
            1,
            "{warnings:?}"
        );
        assert_eq!(warnings.len(), 1, "no claim WARN: {warnings:?}");
    }

    /// Hook work that calls `wait` after the driver's arbitration and
    /// before `drive_invocation` returns (the crate-private hook) records
    /// the context usage error, and does not change the late-panic policy:
    /// the policy uses the value the arbitration returned, so the end
    /// hook's error, whose destructor panics, still fails the invocation
    /// with that late panic.
    #[tokio::test]
    // reason: the test calls the service on its own task so the panic is
    // observable as that task's `JoinError`.
    #[expect(clippy::disallowed_methods)]
    async fn fatal_after_driver_arbitration_keeps_the_late_panic() {
        let failing = Hooks::default()
            .on_end(|_| Box::pin(async { Err(Box::new(LateDropError) as BoxError) }));
        let options = Options::builder()
            .execution_client(std::sync::Arc::new(client::InMemoryExecutionClient::new(
                Vec::new(),
            )))
            .plugin(failing)
            .build()
            .unwrap();
        let slot: ContextSlot = std::sync::Arc::default();
        let service = {
            let slot = std::sync::Arc::clone(&slot);
            wrap(
                move |_e: serde_json::Value, ctx: DurableContext| {
                    *slot.lock().unwrap() = Some(ctx);
                    async { Ok::<_, BoxError>(serde_json::json!("done")) }
                },
                options,
            )
        };
        let hook: std::sync::Arc<dyn Fn(&coordinator::InvocationCoordinator) + Send + Sync> = {
            let slot = std::sync::Arc::clone(&slot);
            std::sync::Arc::new(move |_coordinator| {
                let ctx = slot.lock().unwrap().clone().expect("the handler ran");
                // What hook work does: plugin code that calls `wait`.
                plugin::call_plugin(|| drop(ctx.wait(Duration::from_secs(1))))
                    .expect("the claim does not panic");
            })
        };
        let joined = tokio::spawn(
            driver::test_hooks::AFTER_ARBITRATION.scope(hook, service(first_invocation_event())),
        )
        .await;
        let payload = match joined {
            Ok(response) => unreachable!(
                "the late panic must fail the invocation: {:?}",
                response.map(|r| r.0)
            ),
            Err(join_err) => join_err.into_panic(),
        };
        assert_eq!(payload.downcast_ref::<&str>(), Some(&"late drop"));
        let ctx = slot.lock().unwrap().take().expect("the handler ran");
        let fatal = ctx
            .coordinator()
            .fatal_error()
            .map(|fatal| fatal.error_message);
        assert!(
            fatal
                .as_deref()
                .is_some_and(|m| m.contains("inside plugin code")),
            "the claim after the arbitration records the context usage error: {fatal:?}"
        );
    }

    /// `on_invocation_end` calling `wait` on a context the handler
    /// captured records the context usage error, and the response stays
    /// `SUCCEEDED` with `"done"`: the response was built before the end
    /// hooks, and no operation of the invocation follows them.
    #[tokio::test]
    async fn claim_in_end_hook_leaves_response_unchanged() {
        let slot: ContextSlot = std::sync::Arc::default();
        let kept: ContextSlot = std::sync::Arc::default();
        let plugin = {
            let slot = std::sync::Arc::clone(&slot);
            Hooks::default().on_end(move |_| {
                if let Some(ctx) = slot.lock().unwrap().as_ref() {
                    drop(ctx.wait(Duration::from_secs(1)));
                }
                Box::pin(std::future::ready(Ok(())))
            })
        };
        let handler_slot = std::sync::Arc::clone(&slot);
        let handler_kept = std::sync::Arc::clone(&kept);
        let (service, event) = hooks::service(vec![plugin], 0, move |_e, ctx| {
            *handler_slot.lock().unwrap() = Some(ctx.clone());
            *handler_kept.lock().unwrap() = Some(ctx);
            async move { Ok(serde_json::json!("done")) }
        });
        let json = service(event).await.unwrap().0;
        let ctx = kept.lock().unwrap().take().expect("the handler ran");
        drop(slot.lock().unwrap().take());
        let fatal = ctx
            .coordinator()
            .fatal_error()
            .map(|fatal| fatal.error_message);
        assert!(
            fatal
                .as_deref()
                .is_some_and(|m| m.contains("inside plugin code")),
            "the end-hook claim records the context usage error: {fatal:?}"
        );
        assert_eq!(
            json.get("Status"),
            Some(&serde_json::json!("SUCCEEDED")),
            "{json}"
        );
        assert_eq!(
            json.get("Result"),
            Some(&serde_json::json!("\"done\"")),
            "{json}"
        );
    }

    /// An operation that plugin code claims while the handler runs (here
    /// in `on_operation_start`) fails the execution with the context usage
    /// error.
    #[tokio::test]
    async fn a_claim_while_the_handler_runs_fails_the_execution() {
        let claimer = ClaimPlugin::new(ClaimAt::OperationStart);
        let options = Options::builder()
            .execution_client(std::sync::Arc::new(client::InMemoryExecutionClient::new(
                Vec::new(),
            )))
            .plugin(claimer.clone())
            .build()
            .unwrap();
        let slot = std::sync::Arc::clone(&claimer.slot);
        let service = wrap(
            move |_e: serde_json::Value, ctx: DurableContext| {
                *slot.lock().unwrap() = Some(ctx.clone());
                async move {
                    let value = ctx.step(|_| async { Ok(1_u32) }).name("s").await?;
                    Ok::<_, BoxError>(serde_json::json!(value))
                }
            },
            options,
        );
        let json = service(first_invocation_event()).await.unwrap().0;
        drop(claimer.slot.lock().unwrap().take());
        assert_eq!(claimer.claims(), 1, "on_operation_start claimed once");
        assert_eq!(
            json.get("Status"),
            Some(&serde_json::json!("FAILED")),
            "{json}"
        );
        assert!(json.to_string().contains("inside plugin code"), "{json}");
    }

    type Log = std::sync::Arc<std::sync::Mutex<Vec<String>>>;

    /// Logs `inv enter` and `inv exit` around each `wrap_invocation`.
    struct InvocationWrapLog(Log);

    impl plugin::Plugin for InvocationWrapLog {
        fn wrap_invocation(&self, _info: &plugin::InvocationInfo, next: &mut dyn FnMut()) {
            self.0.lock().unwrap().push("inv enter".to_owned());
            next();
            self.0.lock().unwrap().push("inv exit".to_owned());
        }
    }

    impl plugin::PluginFactory for InvocationWrapLog {
        fn create_plugin(
            &self,
            _info: &plugin::InvocationInfo,
        ) -> Result<Option<Box<dyn plugin::Plugin>>, BoxError> {
            Ok(Some(Box::new(Self(std::sync::Arc::clone(&self.0)))))
        }
    }

    /// A guard whose destructor logs `guard drop` and panics.
    struct PanicsOnDrop(Log);

    impl Drop for PanicsOnDrop {
        #[expect(clippy::panic)] // reason: the destructor panic under test
        fn drop(&mut self) {
            self.0.lock().unwrap().push("guard drop".to_owned());
            panic!("guard drop");
        }
    }

    /// The post-start future, pending right after the drive (the
    /// crate-private pending point) while it holds a guard whose `Drop`
    /// panics, is dropped. The drop runs inside `wrap_invocation`, the
    /// panic is caught there, and it propagates from the drop after the
    /// wrap returned (rule 10.6).
    #[tokio::test]
    async fn pending_invocation_future_destructor_panic() {
        let log: Log = std::sync::Arc::default();
        let reached = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let held = std::sync::Arc::new(test_hooks::HeldGuard {
            reached: std::sync::Arc::clone(&reached),
            make: Box::new({
                let log = std::sync::Arc::clone(&log);
                move || Box::new(PanicsOnDrop(std::sync::Arc::clone(&log))) as Box<dyn Send>
            }),
        });
        let options = Options::builder()
            .execution_client(std::sync::Arc::new(client::InMemoryExecutionClient::new(
                Vec::new(),
            )))
            .plugin(InvocationWrapLog(std::sync::Arc::clone(&log)))
            .build()
            .unwrap();
        let service = wrap(
            |_e: serde_json::Value, _ctx: DurableContext| async {
                Ok::<_, BoxError>(serde_json::json!("done"))
            },
            options,
        );
        let mut invocation = Box::pin(
            test_hooks::PENDING_HOLDING_GUARD.scope(held, service(first_invocation_event())),
        );
        // Poll until the pending point holds the guard, and no further: the
        // next poll would resume past it.
        tokio::time::timeout(
            Duration::from_secs(5),
            std::future::poll_fn(|cx| {
                assert!(
                    invocation.as_mut().poll(cx).is_pending(),
                    "the invocation stays pending at the pending point"
                );
                if reached.load(std::sync::atomic::Ordering::SeqCst) {
                    std::task::Poll::Ready(())
                } else {
                    std::task::Poll::Pending
                }
            }),
        )
        .await
        .expect("the post-start future reaches the pending point within 5 s");
        log.lock().unwrap().push("dropping".to_owned());
        let dropped = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(invocation)));
        let payload = dropped.expect_err("the destructor panic propagates");
        assert_eq!(payload.downcast_ref::<&str>(), Some(&"guard drop"));
        let log = log.lock().unwrap().clone();
        let at = log.iter().position(|l| l == "dropping").expect("logged");
        assert_eq!(
            log.get(at..).expect("in range"),
            ["dropping", "inv enter", "guard drop", "inv exit"],
            "{log:?}"
        );
    }

    /// An invocation that fails before the plugins are created calls no
    /// factory.
    #[tokio::test]
    async fn failed_input_calls_no_factory() {
        let factory = RecordingFactory::default();
        let options = Options::builder()
            .execution_client(std::sync::Arc::new(client::InMemoryExecutionClient::new(
                Vec::new(),
            )))
            .plugin(factory.clone())
            .build()
            .unwrap();
        let service = wrap(
            |_event: u32, _ctx: DurableContext| async move { Ok::<u32, BoxError>(0) },
            options,
        );
        let payload = envelope_with(&serde_json::json!([{
            "Id": "exec-1",
            "Type": "EXECUTION",
            "ExecutionDetails": { "InputPayload": "\"not a number\"" }
        }]));
        let result = service(lambda_runtime::LambdaEvent::new(
            InvocationPayload(payload),
            lambda_runtime::Context::default(),
        ))
        .await;
        assert!(result.is_err());
        assert!(factory.0.lock().unwrap().is_empty());
    }

    #[test]
    fn parse_envelope_no_envelope_returns_none() {
        // A plain customer event with no envelope fields.
        let payload = serde_json::json!({ "order_id": "abc-123" });
        let result = parse_envelope(&payload).unwrap();
        assert!(result.is_none(), "non-envelope payload should return None");
    }

    #[test]
    fn extract_customer_input_from_valid_envelope() {
        let payload = serde_json::json!({
            "DurableExecutionArn": "arn:test",
            "CheckpointToken": "tok",
            "InitialExecutionState": {
                "Operations": [{
                    "Id": "execution",
                    "Type": "EXECUTION",
                    "ExecutionDetails": {
                        "InputPayload": "\"hello\""
                    }
                }]
            }
        });
        let input = inline_input_payload(&payload).unwrap().into_payload();
        let result: Result<String, _> = extract_customer_input(input.as_deref());
        assert_eq!(result.unwrap(), "hello");
    }

    #[test]
    fn extract_customer_input_defaults_to_empty_object() {
        // An envelope whose Execution operation has no InputPayload parses
        // as `{}`, as the JS SDK does.
        let payload = serde_json::json!({
            "DurableExecutionArn": "arn:test",
            "CheckpointToken": "tok",
            "InitialExecutionState": {
                "Operations": [{ "Id": "execution", "Type": "EXECUTION" }]
            }
        });
        let input = inline_input_payload(&payload).unwrap();
        assert_eq!(input, client::PageInput::Execution(None));
        let result: serde_json::Value =
            extract_customer_input(input.into_payload().as_deref()).unwrap();
        assert_eq!(result, serde_json::json!({}));
    }

    /// The `InputPayload` cases of `inline_input_payload`: no Execution
    /// operation, a `null`, a string, and a non-string value.
    #[test]
    fn inline_input_payload_cases() {
        let envelope = |ops: serde_json::Value| {
            serde_json::json!({
                "DurableExecutionArn": "arn:test",
                "CheckpointToken": "tok",
                "InitialExecutionState": { "Operations": ops }
            })
        };
        let step_only = envelope(serde_json::json!([{ "Id": "s1", "Type": "STEP" }]));
        assert_eq!(
            inline_input_payload(&step_only).unwrap(),
            client::PageInput::NoExecutionOperation
        );
        let no_operations = serde_json::json!({
            "DurableExecutionArn": "arn:test",
            "CheckpointToken": "tok",
            "InitialExecutionState": { "NextMarker": "m-1" }
        });
        assert_eq!(
            inline_input_payload(&no_operations).unwrap(),
            client::PageInput::NoExecutionOperation
        );
        // The first EXECUTION operation is read, wherever it sits.
        let later = envelope(serde_json::json!([
            { "Id": "s1", "Type": "STEP", "ExecutionDetails": { "InputPayload": "\"wrong\"" } },
            { "Id": "e", "Type": "EXECUTION", "ExecutionDetails": { "InputPayload": "\"right\"" } }
        ]));
        assert_eq!(
            inline_input_payload(&later).unwrap(),
            client::PageInput::Execution(Some("\"right\"".to_owned()))
        );
        let null = envelope(serde_json::json!([
            { "Id": "e", "Type": "EXECUTION", "ExecutionDetails": { "InputPayload": null } }
        ]));
        assert_eq!(
            inline_input_payload(&null).unwrap(),
            client::PageInput::Execution(None)
        );
        let number = envelope(serde_json::json!([
            { "Id": "e", "Type": "EXECUTION", "ExecutionDetails": { "InputPayload": 5 } }
        ]));
        let err = inline_input_payload(&number).unwrap_err();
        assert!(
            err.to_string().contains("InputPayload is not a string"),
            "got: {err}"
        );
    }

    #[test]
    fn extract_customer_input_parse_failure_errors() {
        let err = extract_customer_input::<serde_json::Value>(Some("not json")).unwrap_err();
        assert!(
            err.to_string().contains("deserialize customer input"),
            "got: {err}"
        );
    }

    // Bootstrap input cases, driven through `wrap`.

    /// A test transport that returns configured pages by marker and records
    /// each `get_state` call's marker.
    struct PagedTransport {
        pages: std::collections::HashMap<String, GetStateOutput>,
        markers: std::sync::Mutex<Vec<Option<String>>>,
    }

    impl PagedTransport {
        fn new(pages: Vec<(&str, Vec<aws_sdk_lambda::types::Operation>, Option<&str>)>) -> Self {
            Self {
                pages: pages
                    .into_iter()
                    .map(|(marker, ops, next)| {
                        (
                            marker.to_owned(),
                            GetStateOutput::new(ops, next.map(str::to_owned)),
                        )
                    })
                    .collect(),
                markers: std::sync::Mutex::new(Vec::new()),
            }
        }

        fn markers(&self) -> Vec<Option<String>> {
            self.markers.lock().unwrap().clone()
        }
    }

    impl ExecutionClient for PagedTransport {
        fn checkpoint(
            &self,
            _execution_arn: &str,
            _checkpoint_token: &str,
            _updates: Vec<aws_sdk_lambda::types::OperationUpdate>,
        ) -> std::pin::Pin<
            Box<dyn Future<Output = Result<CheckpointOutput, ExecutionClientError>> + Send + '_>,
        > {
            Box::pin(async {
                Ok(CheckpointOutput::new(
                    Some("token-next".to_owned()),
                    Vec::new(),
                    None,
                ))
            })
        }

        fn get_state(
            &self,
            _execution_arn: &str,
            _checkpoint_token: &str,
            marker: Option<&str>,
        ) -> std::pin::Pin<
            Box<dyn Future<Output = Result<GetStateOutput, ExecutionClientError>> + Send + '_>,
        > {
            self.markers.lock().unwrap().push(marker.map(str::to_owned));
            let page = marker.and_then(|m| self.pages.get(m)).map_or_else(
                || GetStateOutput::new(Vec::new(), None),
                |p| GetStateOutput::new(p.operations.clone(), p.next_marker().map(str::to_owned)),
            );
            Box::pin(async move { Ok(page) })
        }
    }

    /// Drives the `wrap`-produced echo service over `transport` and
    /// reports the response and whether the handler ran.
    async fn invoke_echo_over(
        payload: serde_json::Value,
        transport: std::sync::Arc<dyn ExecutionClient>,
    ) -> (
        Result<serde_json::Value, lambda_runtime::Error>,
        std::sync::Arc<std::sync::atomic::AtomicBool>,
    ) {
        let ran = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
        let ran_in_handler = std::sync::Arc::clone(&ran);
        let options = Options::builder()
            .execution_client(transport)
            .build()
            .expect("options build");
        let service = wrap(
            move |event: serde_json::Value, _ctx: DurableContext| {
                let ran = std::sync::Arc::clone(&ran_in_handler);
                async move {
                    ran.store(true, std::sync::atomic::Ordering::SeqCst);
                    Ok::<serde_json::Value, BoxError>(event)
                }
            },
            options,
        );
        let event = lambda_runtime::LambdaEvent::new(
            InvocationPayload(payload),
            lambda_runtime::Context::default(),
        );
        (service(event).await.map(|response| response.0), ran)
    }

    fn bootstrap_envelope(state: &serde_json::Value) -> serde_json::Value {
        serde_json::json!({
            "DurableExecutionArn": "arn:aws:lambda:us-east-1:1:function:test",
            "CheckpointToken": "token-abc",
            "InitialExecutionState": state
        })
    }

    fn assert_succeeded_with(response: &serde_json::Value, expected: &serde_json::Value) {
        assert_eq!(
            response.get("Status").and_then(serde_json::Value::as_str),
            Some("SUCCEEDED"),
            "unexpected response: {response}"
        );
        let result: serde_json::Value = serde_json::from_str(
            response
                .get("Result")
                .and_then(serde_json::Value::as_str)
                .expect("a Result string"),
        )
        .expect("the Result parses");
        assert_eq!(&result, expected, "unexpected response: {response}");
    }

    /// Rows 1 and 5: an absent or `null` `InputPayload` runs the handler
    /// on `{}`.
    #[tokio::test]
    async fn absent_input_payload_runs_on_empty_object() {
        for details in [
            serde_json::json!({}),
            serde_json::json!({ "InputPayload": null }),
        ] {
            let payload = bootstrap_envelope(&serde_json::json!({
                "Operations": [{
                    "Id": "execution",
                    "Type": "EXECUTION",
                    "Status": "STARTED",
                    "ExecutionDetails": details
                }]
            }));
            let transport = std::sync::Arc::new(PagedTransport::new(Vec::new()));
            let (response, _) =
                invoke_echo_over(payload, std::sync::Arc::clone(&transport) as _).await;
            assert_succeeded_with(&response.expect("must succeed"), &serde_json::json!({}));
            assert!(transport.markers().is_empty(), "no marker, no get_state");
        }
    }

    /// Row 2: the input is read from a later page.
    #[tokio::test]
    async fn input_payload_found_after_pagination() {
        let payload = bootstrap_envelope(&serde_json::json!({ "NextMarker": "m-1" }));
        let transport = std::sync::Arc::new(PagedTransport::new(vec![(
            "m-1",
            vec![make_test_execution_op("{\"count\":1}")],
            None,
        )]));
        let (response, _) = invoke_echo_over(payload, std::sync::Arc::clone(&transport) as _).await;
        assert_succeeded_with(
            &response.expect("must succeed"),
            &serde_json::json!({ "count": 1 }),
        );
        assert_eq!(transport.markers(), vec![Some("m-1".to_owned())]);
    }

    /// Row 3: an inline input is kept when later pages are fetched.
    #[tokio::test]
    async fn paginated_input_matches_first_page() {
        let payload = bootstrap_envelope(&serde_json::json!({
            "Operations": [{
                "Id": "execution",
                "Type": "EXECUTION",
                "Status": "STARTED",
                "ExecutionDetails": { "InputPayload": "{\"a\":1}" }
            }],
            "NextMarker": "m-1"
        }));
        let transport = std::sync::Arc::new(PagedTransport::new(vec![(
            "m-1",
            vec![make_test_step_op("s1", "\"r1\"")],
            None,
        )]));
        let (response, _) = invoke_echo_over(payload, std::sync::Arc::clone(&transport) as _).await;
        assert_succeeded_with(
            &response.expect("must succeed"),
            &serde_json::json!({ "a": 1 }),
        );
        assert_eq!(transport.markers(), vec![Some("m-1".to_owned())]);
    }

    /// Rows 4 and 6: an input that does not parse, and a non-string input,
    /// fail the invocation before the handler runs.
    #[tokio::test]
    async fn malformed_input_payload_fails_the_invocation() {
        for (input, expected) in [
            (serde_json::json!("not json"), "deserialize customer input"),
            (serde_json::json!(5), "InputPayload is not a string"),
        ] {
            let payload = bootstrap_envelope(&serde_json::json!({
                "Operations": [{
                    "Id": "execution",
                    "Type": "EXECUTION",
                    "Status": "STARTED",
                    "ExecutionDetails": { "InputPayload": input }
                }]
            }));
            let transport = std::sync::Arc::new(PagedTransport::new(Vec::new()));
            let (response, ran) =
                invoke_echo_over(payload, std::sync::Arc::clone(&transport) as _).await;
            let err = response.expect_err("must fail");
            assert!(err.to_string().contains(expected), "got: {err}");
            assert!(
                !ran.load(std::sync::atomic::Ordering::SeqCst),
                "the handler must not run"
            );
            assert!(transport.markers().is_empty(), "no marker, no get_state");
        }
    }

    /// Row 1's envelope without a `CheckpointToken` fails in
    /// `parse_envelope`, before the input defaults to `{}`.
    #[tokio::test]
    async fn missing_checkpoint_token_fails_before_input_default() {
        let payload = serde_json::json!({
            "DurableExecutionArn": "arn:aws:lambda:us-east-1:1:function:test",
            "InitialExecutionState": {
                "Operations": [{
                    "Id": "execution",
                    "Type": "EXECUTION",
                    "Status": "STARTED",
                    "ExecutionDetails": {}
                }]
            }
        });
        let transport = std::sync::Arc::new(PagedTransport::new(Vec::new()));
        let (response, ran) = invoke_echo_over(payload, transport).await;
        let err = response.expect_err("must fail");
        assert!(err.to_string().contains("CheckpointToken"), "got: {err}");
        assert!(
            !ran.load(std::sync::atomic::Ordering::SeqCst),
            "the handler must not run"
        );
    }

    // Default client timeouts

    #[test]
    fn default_client_sets_connect_and_read_timeouts() {
        let config = SdkConfig::builder()
            .behavior_version(aws_config::BehaviorVersion::latest())
            .build();
        let client = default_lambda_client(&config);
        let timeouts = client
            .config()
            .timeout_config()
            .expect("the default client has a timeout config");
        assert_eq!(timeouts.connect_timeout(), Some(Duration::from_secs(5)));
        assert_eq!(timeouts.read_timeout(), Some(Duration::from_secs(50)));
        assert_eq!(
            timeouts.operation_attempt_timeout(),
            Some(Duration::from_secs(55))
        );
    }

    #[test]
    fn supplied_config_keeps_its_timeouts() {
        use aws_sdk_lambda::config::timeout::TimeoutConfig;
        let config = SdkConfig::builder()
            .behavior_version(aws_config::BehaviorVersion::latest())
            .timeout_config(
                TimeoutConfig::builder()
                    .connect_timeout(Duration::from_secs(1))
                    .build(),
            )
            .build();
        let client = base_lambda_client_from_options(Some(config), None)
            .expect("sdk_config must yield a client");
        let timeouts = client.config().timeout_config().expect("a timeout config");
        assert_eq!(timeouts.connect_timeout(), Some(Duration::from_secs(1)));
        assert_eq!(timeouts.read_timeout(), None);

        let conf = aws_sdk_lambda::config::Config::builder()
            .behavior_version(aws_sdk_lambda::config::BehaviorVersion::latest())
            .timeout_config(
                TimeoutConfig::builder()
                    .read_timeout(Duration::from_secs(7))
                    .build(),
            )
            .build();
        let supplied = aws_sdk_lambda::Client::from_conf(conf);
        let client = base_lambda_client_from_options(None, Some(supplied))
            .expect("lambda_client must be returned");
        let timeouts = client.config().timeout_config().expect("a timeout config");
        assert_eq!(timeouts.read_timeout(), Some(Duration::from_secs(7)));
        assert_eq!(timeouts.connect_timeout(), None);
        assert_eq!(timeouts.operation_attempt_timeout(), None);
    }

    // ---- Oversized handler results ----

    /// A durable invocation envelope with no recorded operation.
    fn oversized_envelope() -> serde_json::Value {
        serde_json::json!({
            "DurableExecutionArn": "arn:aws:lambda:us-east-1:1:function:test",
            "CheckpointToken": "token-abc",
            "InitialExecutionState": { "Operations": [] }
        })
    }

    /// Runs a `wrap` service whose handler returns `"a".repeat(n - 2)`, so
    /// the serialized result is `n` bytes, over `client`, with `hooks`
    /// registered. Returns the response and the end infos.
    async fn run_sized_result(
        n: usize,
        client: std::sync::Arc<client::InMemoryExecutionClient>,
    ) -> (
        Result<serde_json::Value, lambda_runtime::Error>,
        Vec<plugin::InvocationEndInfo>,
    ) {
        let hooks = Hooks::default();
        let options = Options::builder()
            .execution_client(client)
            .plugin(hooks.clone())
            .build()
            .expect("valid options");
        // The handler runs once, so the result string is built once.
        let service = wrap(
            move |_event: serde_json::Value, _ctx: DurableContext| async move {
                Ok::<String, BoxError>("a".repeat(n - 2))
            },
            options,
        );
        let response = service(lambda_runtime::LambdaEvent::new(
            InvocationPayload(oversized_envelope()),
            lambda_runtime::Context::default(),
        ))
        .await
        .map(|response| response.0);
        (response, hooks.ends())
    }

    /// Whether `serialized` is the serialized result of
    /// [`run_sized_result`]: `n` bytes, quotes around `n - 2` letters. It
    /// checks the bytes in place, so no second 6 MB string is built.
    fn is_sized_result(serialized: &str, n: usize) -> bool {
        serialized.len() == n
            && serialized
                .strip_prefix('"')
                .and_then(|inner| inner.strip_suffix('"'))
                .is_some_and(|inner| inner.bytes().all(|b| b == b'a'))
    }

    /// The `EXECUTION` updates `client` recorded.
    fn execution_updates(
        client: &client::InMemoryExecutionClient,
    ) -> Vec<aws_sdk_lambda::types::OperationUpdate> {
        client
            .recorded_updates()
            .into_iter()
            .filter(|update| update.r#type() == &aws_sdk_lambda::types::OperationType::Execution)
            .collect()
    }

    /// A result one byte over the limit is written as one `EXECUTION`
    /// `SUCCEED` update with the result as payload and a hashed ID, and
    /// the envelope carries an empty `Result`.
    #[tokio::test]
    async fn oversized_result_envelope_is_empty() {
        let n = LAMBDA_RESPONSE_SIZE_LIMIT + 1;
        assert_eq!(n, 6_291_407);
        let client = std::sync::Arc::new(client::InMemoryExecutionClient::new(Vec::new()));
        let (response, ends) = run_sized_result(n, std::sync::Arc::clone(&client)).await;
        let response = response.expect("an envelope");
        assert_eq!(
            response,
            serde_json::json!({ "Status": "SUCCEEDED", "Result": "" })
        );
        let updates = execution_updates(&client);
        let [update] = updates.as_slice() else {
            unreachable!("one EXECUTION update, got {}", updates.len());
        };
        assert_eq!(
            update.action(),
            &aws_sdk_lambda::types::OperationAction::Succeed
        );
        let payload = update.payload().unwrap_or_default();
        assert_eq!(payload.len(), n);
        assert!(
            is_sized_result(payload, n),
            "the payload is the serialized result"
        );
        let id = update.id();
        assert_eq!(id.len(), 64, "{id}");
        assert!(
            id.chars()
                .all(|c| c.is_ascii_digit() || ('a'..='f').contains(&c)),
            "{id}"
        );
        let [end] = ends.as_slice() else {
            unreachable!("one end, got {}", ends.len());
        };
        assert_eq!(end.status(), InvocationStatus::Succeeded);
        let recorded = end.execution_result().unwrap_or_default();
        assert_eq!(recorded.len(), n);
        assert!(
            is_sized_result(recorded, n),
            "the end info holds the full result"
        );
    }

    /// A failed write of the oversized result: a non-retryable failure
    /// answers `FAILED` with `CheckpointUnrecoverableExecutionError`, and a
    /// retryable one fails the invocation. The end hook runs once with the
    /// matching status and error.
    #[tokio::test]
    async fn oversized_result_write_failures() {
        let n = LAMBDA_RESPONSE_SIZE_LIMIT + 1;

        let client = std::sync::Arc::new(client::InMemoryExecutionClient::new(Vec::new()));
        client.enqueue_checkpoint_response(client::TestResponse::NonRetryableError(
            "rejected".to_owned(),
        ));
        let (response, ends) = run_sized_result(n, std::sync::Arc::clone(&client)).await;
        let response = response.expect("an envelope");
        assert_eq!(json_str(&response, "/Status"), Some("FAILED"), "{response}");
        assert_eq!(
            json_str(&response, "/Error/ErrorType"),
            Some(error::CHECKPOINT_EXECUTION_FAILED_ERROR_TYPE)
        );
        let message = json_str(&response, "/Error/ErrorMessage").unwrap_or_default();
        assert!(
            message.starts_with("[Unrecoverable Execution] Checkpoint failed: "),
            "{message}"
        );
        let [end] = ends.as_slice() else {
            unreachable!("one end, got {}", ends.len());
        };
        assert_eq!(end.status(), InvocationStatus::Failed);
        assert_eq!(end.execution_result(), None);
        assert_eq!(
            end.execution_error().and_then(WireError::error_type),
            Some(error::CHECKPOINT_EXECUTION_FAILED_ERROR_TYPE)
        );
        assert_eq!(
            end.execution_error().and_then(WireError::error_message),
            Some(message)
        );

        let client = std::sync::Arc::new(client::InMemoryExecutionClient::new(Vec::new()));
        client.enqueue_checkpoint_response(client::TestResponse::RetryableError(
            "throttled".to_owned(),
        ));
        let (response, ends) = run_sized_result(n, std::sync::Arc::clone(&client)).await;
        let err = response.expect_err("the invocation fails");
        assert_eq!(
            err.to_string(),
            "[Unrecoverable Invocation] Checkpoint failed: throttled"
        );
        let [end] = ends.as_slice() else {
            unreachable!("one end, got {}", ends.len());
        };
        assert_eq!(end.status(), InvocationStatus::Retrying);
        assert_eq!(end.execution_result(), None);
        assert_eq!(
            end.execution_error().and_then(WireError::error_type),
            Some(error::CHECKPOINT_INVOCATION_FAILED_ERROR_TYPE)
        );
        assert_eq!(
            end.execution_error().and_then(WireError::error_message),
            Some("[Unrecoverable Invocation] Checkpoint failed: throttled")
        );
    }

    /// The oversized write answers `PENDING` when the checkpoint token was
    /// withdrawn before it, and sends nothing.
    #[tokio::test]
    async fn oversized_result_after_withdrawal_is_pending() {
        let client = std::sync::Arc::new(client::InMemoryExecutionClient::new(Vec::new()));
        let ctx = DurableContext::new_root_with_client_and_defaults(
            "arn:aws:lambda:us-east-1:1:function:test".to_owned(),
            lambda_runtime::Context::default(),
            std::sync::Arc::new(engine::CheckpointLog::empty()),
            std::sync::Arc::clone(&client) as std::sync::Arc<dyn ExecutionClient>,
            "t0".to_owned(),
            Duration::ZERO,
            0,
            None,
        );
        ctx.coordinator()
            .withdraw(ctx.execution_arn(), "request", || {});
        let (response, end) = checkpoint_oversized_result(&ctx, "\"a\"".to_owned()).await;
        assert_eq!(
            response.expect("an envelope").0,
            serde_json::json!({ "Status": "PENDING" })
        );
        assert_eq!(end.status, InvocationStatus::Pending);
        assert_eq!(client.recorded_updates().len(), 0);
    }

    /// A handler error with a 7 MB message answers `FAILED` and is never
    /// checkpointed: only a result is size-checked.
    #[tokio::test]
    async fn oversized_error_is_not_checkpointed() {
        let client = std::sync::Arc::new(client::InMemoryExecutionClient::new(Vec::new()));
        let options = Options::builder()
            .execution_client(std::sync::Arc::clone(&client) as std::sync::Arc<dyn ExecutionClient>)
            .build()
            .expect("valid options");
        // The handler runs once, so the message is built once.
        let service = wrap(
            |_event: serde_json::Value, _ctx: DurableContext| async {
                Err::<String, BoxError>("e".repeat(7 * 1024 * 1024).into())
            },
            options,
        );
        let response = service(lambda_runtime::LambdaEvent::new(
            InvocationPayload(oversized_envelope()),
            lambda_runtime::Context::default(),
        ))
        .await
        .expect("an envelope")
        .0;
        assert_eq!(json_str(&response, "/Status"), Some("FAILED"));
        assert_eq!(execution_updates(&client).len(), 0);
    }

    /// A result of exactly the limit stays inline, with no write.
    #[tokio::test]
    async fn result_at_limit_stays_inline() {
        let n = LAMBDA_RESPONSE_SIZE_LIMIT;
        assert_eq!(n, 6_291_406);
        let client = std::sync::Arc::new(client::InMemoryExecutionClient::new(Vec::new()));
        let (response, _ends) = run_sized_result(n, std::sync::Arc::clone(&client)).await;
        let response = response.expect("an envelope");
        assert_eq!(json_str(&response, "/Status"), Some("SUCCEEDED"));
        let result = json_str(&response, "/Result").unwrap_or_default();
        assert_eq!(result.len(), n);
        assert!(is_sized_result(result, n), "the result is inline");
        assert_eq!(client.recorded_updates().len(), 0);
    }
}