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
//! Child context operation execution engine.
//!
//! Implements `run_in_child_context`: live path (run child closure,
//! serialize result, checkpoint), replay path (return frozen result from
//! checkpoint log), and error grading (`ChildFnError` → `ChildContextError`).
//!
//! Wire shape:
//! - `OperationType`: `Context`
//! - `OperationSubType`: the caller's subtype, or `RunInChildContext`
//! - Actions: Start → (Succeed | Fail)
//! - Payload on Succeed: serialized child result
//! - Error on Fail: `{ Type, Message }` from the child function error
//! - `ParentId`: wire ID of the parent context's prefix (if child of child)
use std::future::Future;
use aws_sdk_lambda::types::{OperationAction, OperationType, OperationUpdate};
use tracing::Instrument as _;
use crate::Serdes;
use crate::checkpoint_coalescer::ScopeClose;
use crate::context::{DurableContext, SubmittedWrite, run_body};
use crate::engine::{CheckpointStatus, OperationId};
use crate::error::{
ChildContextError, ChildContextErrorKind, ChildFnError, OperationError, OperationErrorKind,
};
use crate::plugin::UserBody;
use crate::serdes::SerdesContext;
use crate::start::StartStep;
/// Wire sub-type for child context operations.
pub(crate) const CHILD_SUB_TYPE: &str = "RunInChildContext";
/// Maximum checkpoint payload size in bytes (256KB). Payloads exceeding
/// this trigger `ReplayChildren` mode: the child result is not stored in
/// the checkpoint; instead the child body is re-executed on replay.
pub(crate) const CHECKPOINT_SIZE_LIMIT_BYTES: usize = 256 * 1024;
/// The caller's summary of an oversized child result (see
/// [`ChildBuilder::summary`](crate::builders::ChildBuilder::summary)).
pub(crate) type ChildSummary<O> = Box<dyn Fn(&O) -> String + Send + Sync>;
/// Internal state for child context execution passed from the builder.
///
/// Generic over the body closure `F` and, through its future's output,
/// the body's error type `E`, so the body runs **without type erasure**:
/// `run_in_child_context` instantiates it with the user closure directly
/// (`E = BoxError`), and `with_retry` with the concrete retry-loop future
/// (`E = ChildFnError`). The one erasure point is the builder's
/// `.future()` / `into_future`, which boxes the whole execution future
/// once inside [`DurableFuture`](crate::DurableFuture).
pub(crate) struct ChildExecution<O, F, S> {
pub(crate) ctx: DurableContext,
pub(crate) op_id: OperationId,
pub(crate) name: Option<String>,
/// The resolved subtype: the caller's, or `RunInChildContext`.
pub(crate) sub_type: crate::OperationSubType,
pub(crate) serdes: S,
pub(crate) closure: F,
pub(crate) user_function: UserFunctionPolicy,
/// The payload of an oversized result's record; `None` records `""`.
pub(crate) summary: Option<ChildSummary<O>>,
pub(crate) _marker: std::marker::PhantomData<fn() -> O>,
}
/// Whether a child's body is a user function. Set by the caller, never
/// inferred from the child's name.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum UserFunctionPolicy {
/// A frame, wraps and user-function hooks.
Hooks,
/// None of them: a `with_retry` attempt. The block, with its own record
/// or virtual, is the user function.
None,
}
impl<O, F, Fut, E, S> ChildExecution<O, F, S>
where
O: Send + 'static,
F: FnOnce(DurableContext) -> Fut + Send + 'static,
Fut: Future<Output = Result<O, E>> + Send + 'static,
E: Into<ChildFnError> + 'static,
S: Serdes<O>,
{
/// Executes the child context operation.
///
/// Thin generic wrapper: the ONLY code monomorphized per call site.
/// The replay/checkpoint state machine lives in the non-generic
/// [`ChildCore`] / [`ChildAfter`] halves (generic over the result type
/// `O` only); this wrapper just polls the user's concrete future
/// between them, in a fresh child context under the child namespace's
/// replay-aware span.
///
/// Tests call it; the SDK builds every operation future with the start
/// step form (see [`crate::future::DurableFuture::for_start_step`]).
#[cfg(test)]
pub(crate) async fn execute(self) -> Result<O, OperationError> {
let (core, closure, user_function) = self.split();
let started = core.start();
Self::run(core, closure, user_function, started).await
}
/// Runs the child's start step now, on the caller's task, and returns
/// the future of the rest of the operation (see [`crate::start`]).
pub(crate) fn execute_started(
self,
) -> impl Future<Output = Result<O, OperationError>> + Send + 'static {
let (core, closure, user_function) = self.split();
let started = core.start();
Self::run(core, closure, user_function, started)
}
/// Executes the child as a virtual child context: the body runs in a
/// context with its own operation namespace, and nothing is recorded
/// for the child itself.
///
/// The caller has already rejected a record at the child's ID (see
/// [`DurableContext::reject_record_at_virtual_child`]). The body runs
/// in every invocation, inline on the awaiting task, as a user
/// function, between `on_operation_start` and `on_operation_end` with
/// `is_virtual` true. The child writes no update, emits no
/// `operation_replayed` event, and closes no scope when the body
/// returns, so an operation the body started and did not await keeps
/// running in the caller's scope.
pub(crate) async fn execute_virtual(self) -> Result<O, OperationError> {
let Self {
ctx,
op_id,
name,
sub_type,
serdes,
closure,
..
} = self;
let wire_id = op_id.wire();
let sub_type = sub_type.as_str();
let child_ctx = ctx.new_virtual_child(op_id.positional(), name.as_deref());
let rec = child_info(&ctx, wire_id, sub_type, name.as_deref()).with_virtual(true);
let info = std::sync::Arc::new(rec.clone().with_replay(child_ctx.is_replaying()));
if let Some(plugins) = ctx.plugins() {
crate::plugin::operation_started(plugins, &info);
}
let span = child_ctx.replay_span();
let body = UserBody::Context(child_ctx.prefix().to_owned());
let result = crate::plugin::run_user_function(
ctx.shared_plugins(),
info,
body,
span,
move || async move { (closure)(child_ctx).await.map_err(Into::into) },
ChildFnError::failure_wire,
)
.await
.into_parts();
// The end hooks report the caller's replay state after the body: true
// when the caller's next operation already has a record.
let ended = |status, error| {
if let Some(plugins) = ctx.plugins() {
let info = rec
.clone()
.with_status(status)
.with_error(error)
.with_replay(ctx.is_replaying());
crate::plugin::operation_ended(plugins, &info);
}
};
match result {
Ok(value) => {
let serdes_ctx = SerdesContext::new(wire_id, ctx.execution_arn());
match serialize_value(&ctx, name.as_deref(), value, &serdes, serdes_ctx.clone())
.await
{
Ok(serialized) => {
ended(crate::OperationStatus::Succeeded, None);
deserialize_value(&ctx, name.as_deref(), serialized, &serdes, serdes_ctx)
.await
}
Err(op_err) => {
let wire = crate::error::serialization_failure_wire(&op_err);
ended(crate::OperationStatus::Failed, Some(wire));
Err(op_err)
}
}
}
Err((child_err, wire)) => {
ended(crate::OperationStatus::Failed, Some(wire));
Err(OperationError::from_kind(OperationErrorKind::ChildContext(
ChildContextError::new(
ChildContextErrorKind::ChildFailed,
Some(child_err.into_source()),
),
)))
}
}
}
/// Splits the execution into its non-generic core and the closure.
fn split(self) -> (ChildCore<O, S>, F, UserFunctionPolicy) {
let Self {
ctx,
op_id,
name,
sub_type,
serdes,
closure,
user_function,
summary,
_marker,
} = self;
let core = ChildCore {
ctx,
op_id,
name,
sub_type,
serdes,
summary,
_marker: std::marker::PhantomData,
};
(core, closure, user_function)
}
/// The rest of the operation after its start step `started`.
async fn run(
core: ChildCore<O, S>,
closure: F,
user_function: UserFunctionPolicy,
started: Result<StartStep, OperationError>,
) -> Result<O, OperationError> {
match core.before(started?).await? {
ChildPrelude::Done(result) => result,
ChildPrelude::Run {
child_ctx,
mode,
info,
start,
after,
} => {
// Run the child closure inside the child namespace's
// replay-aware span: on a resume, nested operations can
// still be replaying while the parent is live, and the child
// span's isReplay flag (kept current by the child's own
// mints) is what lets a filter suppress the body's pre-wait
// log lines exactly once. A live body's `Runner` owns the
// span and enters it outside its hooks and wraps on every
// poll and drop; a reconstruction or a `with_retry` attempt
// runs no user function, so its body is instrumented with
// the span instead.
//
// The scope encloses the closure call, so code the closure
// runs before it returns its future is checked too.
let child_span = child_ctx.replay_span();
let body = UserBody::Context(child_ctx.prefix().to_owned());
// The child's scope closes when the body returns, and also
// when this future is dropped before that, because the
// operation that owns the child was stopped. An operation
// the child started and did not await then writes no further
// checkpoint, and its task stops (see `Admission`). The
// child's own terminal update is written through the parent
// context, whose owners do not include this child, so it is
// admitted.
let scope = ScopeClose::new(after.ctx.admission(), &after.wire_id);
let result = if mode == ChildRunMode::Live
&& user_function == UserFunctionPolicy::Hooks
{
// A live body is a user function. The runner enters the
// span outside its hooks and wraps, so a span a wrap
// enters is a child of it. The scope closes inside the
// body future, so it is closed before the wire and the
// end hooks.
crate::plugin::run_user_function(
after.ctx.shared_plugins(),
info,
body,
child_span,
move || async move {
let result = (closure)(child_ctx).await.map_err(Into::into);
drop(scope);
result
},
ChildFnError::failure_wire,
)
.await
.into_parts()
.map_err(|(error, wire)| (error, Some(wire)))
} else {
// A reconstruction, or a `with_retry` attempt: no frame
// and no user-function hook.
let result = run_body(body.into(), async move { (closure)(child_ctx).await })
.instrument(child_span)
.await
.map_err(|error| (error.into(), None));
drop(scope);
result
};
after.settle(mode, start, result).await
}
}
}
}
/// The pre-closure half of a child context: task-ownership check, replay
/// resolution, and the START checkpoint. Generic only over the result type
/// `O`, no user closure reaches this state machine, so its substantial
/// replay/checkpoint logic compiles once per result type instead of once
/// per call site.
struct ChildCore<O, S> {
ctx: DurableContext,
op_id: OperationId,
name: Option<String>,
sub_type: crate::OperationSubType,
serdes: S,
summary: Option<ChildSummary<O>>,
_marker: std::marker::PhantomData<fn() -> O>,
}
/// Why the child body must run: a fresh live execution, or a re-execution
/// reconstructing a result too large to checkpoint (`ReplayChildren`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ChildRunMode {
/// Live path: checkpoint the outcome afterwards.
Live,
/// `ReplayChildren` path: round-trip the result, checkpoint nothing.
/// The body runs in a rebuilding context, where an operation never
/// resolves unless replay can return its record without running it
/// (see [`crate::start::rebuild_holds`]).
Reconstruct,
}
/// What [`ChildCore::before`] decided: the operation is already resolved
/// from the checkpoint log, or the body must run in the prepared child
/// context.
enum ChildPrelude<O, S> {
/// Resolved without running the body (replayed success or failure).
Done(Result<O, OperationError>),
/// The body must run in `child_ctx`; `after` settles the outcome under
/// the semantics of `mode`.
Run {
/// The fresh child context (chained prefix) the body receives.
child_ctx: DurableContext,
/// Live execution vs `ReplayChildren` reconstruction.
mode: ChildRunMode,
/// The info `on_operation_start` received, with the replay flag
/// set to the child context's `is_replaying()` when the body
/// starts (JS: `isReplay: childReplayMode !== ExecutionMode`); the
/// user-function hooks receive this value.
info: std::sync::Arc<crate::plugin::OperationInfo>,
/// The `START` write of this cycle, still in flight. The outcome
/// write waits for it. `None` when the record already existed.
start: Option<SubmittedWrite>,
/// The post-closure half that settles the outcome.
after: ChildAfter<O, S>,
},
}
/// The post-closure half of a child context: outcome checkpointing (live)
/// or result round-tripping (`ReplayChildren`). Generic only over the
/// result type.
struct ChildAfter<O, S> {
ctx: DurableContext,
wire_id: String,
name: Option<String>,
sub_type: crate::OperationSubType,
serdes: S,
summary: Option<ChildSummary<O>>,
_marker: std::marker::PhantomData<fn() -> O>,
}
impl<O, S> ChildCore<O, S>
where
O: Send + 'static,
S: Serdes<O>,
{
/// The start step of the child (see [`crate::start`]): admission, task
/// ownership, the replay identity check, and, without a record, the
/// `START`, queued, then `on_operation_start`, then released, as JS
/// orders them. A record that re-enters the body fires the replay
/// start hook here.
fn start(&self) -> Result<StartStep, OperationError> {
let wire_id = self.op_id.wire();
crate::start::start_step(
&self.ctx,
&crate::start::StartSpec {
op_id: &self.op_id,
operation_type: "Context",
sub_type: self.sub_type.as_str(),
name: self.name.as_deref(),
order: crate::start::StartOrder::QueueThenHook,
},
|view| match view.status {
CheckpointStatus::Succeeded | CheckpointStatus::Failed => {
crate::start::Cycle::Record
}
// Started/Pending/Ready: re-enter the child body.
_ => crate::start::Cycle::Resume,
},
|| {
build_update(
wire_id,
self.name.as_deref(),
self.sub_type.as_str(),
OperationAction::Start,
&self.ctx,
)
},
|plugins, is_replay| {
let info = child_info(
&self.ctx,
wire_id,
self.sub_type.as_str(),
self.name.as_deref(),
);
let info = if is_replay {
info.with_replay(true)
} else {
info.with_status(crate::OperationStatus::Started)
.with_replay(false)
};
crate::plugin::operation_started(plugins, &info);
},
)
}
/// Runs everything that follows the start step `started` and precedes
/// the child closure: replay resolution, or the child context for the
/// body (live, or `ReplayChildren` reconstruction).
///
/// The body does not wait for a queued `START`: the outcome write does
/// (see [`ChildAfter::settle`]), as in JS.
async fn before(self, started: StartStep) -> Result<ChildPrelude<O, S>, OperationError> {
let positional_id = self.op_id.positional().to_owned();
let wire_id = self.op_id.wire().to_owned();
let serdes_ctx = SerdesContext::new(&wire_id, self.ctx.execution_arn());
let mut mode = ChildRunMode::Live;
// The info `on_operation_start` received, when it fired: the start
// step built it from the same fields (see `ChildCore::start`).
let mut start_info = None;
let start = match started {
StartStep::Refused => return std::future::pending().await,
StartStep::Queued(write) => Some(write),
StartStep::Recorded(view) => match view.status {
CheckpointStatus::Succeeded if view.replay_children => {
// Recorded success whose result was too large to
// checkpoint: re-execute the body to reconstruct it.
mode = ChildRunMode::Reconstruct;
None
}
CheckpointStatus::Succeeded => {
return Ok(ChildPrelude::Done(
self.replay_succeeded(&positional_id, &wire_id, view.attempt, &serdes_ctx)
.await,
));
}
CheckpointStatus::Failed => {
return Ok(ChildPrelude::Done(self.replay_failed(
&positional_id,
&wire_id,
view.attempt,
)));
}
// Started/Pending/Ready: re-enter child body (resume path).
// The start step fired the replay start hook.
_ => {
start_info = Some(
child_info(
&self.ctx,
&wire_id,
self.sub_type.as_str(),
self.name.as_deref(),
)
.with_replay(true),
);
None
}
},
};
if start.is_some() {
// No record: the start step queued `START` and fired the live
// start hook.
start_info = Some(
child_info(
&self.ctx,
&wire_id,
self.sub_type.as_str(),
self.name.as_deref(),
)
.with_status(crate::OperationStatus::Started)
.with_replay(false),
);
}
// 4. Create child context with chained prefix.
let Self {
ctx,
name,
sub_type,
serdes,
summary,
..
} = self;
let child_ctx = ctx.new_child_rebuilding(
&positional_id,
name.as_deref(),
mode == ChildRunMode::Reconstruct,
);
// A reconstruction fires no start hook; its info is never read,
// because only a live body reaches the runner.
let info = start_info
.unwrap_or_else(|| child_info(&ctx, &wire_id, sub_type.as_str(), name.as_deref()));
// The user-function hooks see the child context's replay state at
// the body's start, not the record-based flag of the start hook: a
// resumed child with no inner record yet starts live.
let info = info.with_replay(child_ctx.is_replaying());
Ok(ChildPrelude::Run {
child_ctx,
mode,
info: std::sync::Arc::new(info),
start,
after: ChildAfter {
ctx,
wire_id,
name,
sub_type,
serdes,
summary,
_marker: std::marker::PhantomData,
},
})
}
/// Replay path for a recorded success: returns the frozen result from
/// the checkpoint log without re-running the body (the `ReplayChildren`
/// branch re-executes instead, so it is deliberately not a replay
/// event). Decodes the payload FIRST, then emits `operation_replayed`:
/// a corrupt payload or failing serdes surfaces as an error without
/// claiming a recorded outcome was returned.
async fn replay_succeeded(
self,
positional_id: &str,
wire_id: &str,
attempt: u32,
serdes_ctx: &SerdesContext,
) -> Result<O, OperationError> {
let payload = self.ctx.checkpoint_result_payload(positional_id);
let value = replay_success(
&self.ctx,
self.name.as_deref(),
&self.serdes,
payload,
serdes_ctx.clone(),
)
.await?;
self.ctx.emit_operation_replayed(
wire_id,
self.name.as_deref(),
"Context",
Some(self.sub_type.as_str()),
attempt,
);
Ok(value)
}
/// Replay path for a recorded failure: emits `operation_replayed` and
/// returns the frozen error from the checkpoint log.
fn replay_failed(
&self,
positional_id: &str,
wire_id: &str,
attempt: u32,
) -> Result<O, OperationError> {
self.ctx.emit_operation_replayed(
wire_id,
self.name.as_deref(),
"Context",
Some(self.sub_type.as_str()),
attempt,
);
let wire = self
.ctx
.checkpoint_wire_error(positional_id)
.unwrap_or_default();
Err(replay_failure(wire, wire_id))
}
}
impl<O, S> ChildAfter<O, S>
where
O: Send + 'static,
S: Serdes<O>,
{
/// Settles the child body's outcome.
///
/// Live mode checkpoints it: success through [`build_succeed_update`]
/// (which routes large payloads to `ReplayChildren` mode), failure
/// through [`checkpoint_live_failure`]. Reconstruct mode
/// (`ReplayChildren` replay) round-trips the result through
/// serialization for consistency with the checkpointed path and
/// checkpoints nothing.
///
/// A live outcome write first waits for `start`, the cycle's `START`
/// write. When that write fails, the invocation ends and no outcome is
/// written.
#[expect(clippy::too_many_lines)] // reason: the two modes and both outcomes read better in one match
async fn settle(
self,
mode: ChildRunMode,
start: Option<SubmittedWrite>,
result: Result<O, (ChildFnError, Option<crate::error::WireError>)>,
) -> Result<O, OperationError> {
let Self {
ctx,
wire_id,
name,
sub_type,
serdes,
summary,
..
} = self;
let sub_type = sub_type.as_str();
let serdes_ctx = SerdesContext::new(&wire_id, ctx.execution_arn());
match (mode, result) {
(ChildRunMode::Reconstruct, Ok(value)) => {
// Round-trip through serialization for consistency.
let serialized =
serialize_value(&ctx, name.as_deref(), value, &serdes, serdes_ctx.clone())
.await?;
deserialize_value(&ctx, name.as_deref(), serialized, &serdes, serdes_ctx).await
}
(ChildRunMode::Reconstruct, Err((child_err, _))) => Err(OperationError::from_kind(
OperationErrorKind::ChildContext(ChildContextError::new(
ChildContextErrorKind::ChildFailed,
Some(child_err.into_source()),
)),
)),
(ChildRunMode::Live, Ok(value)) => {
// Success: serialize and checkpoint.
//
// A result serialization failure is a LOCAL, deterministic,
// user-facing failure, so it stays catchable, but the
// terminal FAIL is persisted FIRST (issue #43). The closure
// already ran, so its side effects need a recorded outcome:
// with the FAIL recorded, replay yields it instead of
// re-running the closure, and a handler that catches the
// error branches on a decision replay reproduces.
let serialized = match serialize_value(
&ctx,
name.as_deref(),
value,
&serdes,
serdes_ctx.clone(),
)
.await
{
Ok(serialized) => serialized,
Err(op_err) => {
ctx.await_retained_start(&wire_id, start, OperationAction::Fail)
.await?;
let wire = crate::error::serialization_failure_wire(&op_err);
let update = build_child_fail_update(
&wire_id,
name.as_deref(),
sub_type,
&ctx,
&wire,
);
if let Err(client_err) = ctx.checkpoint_updates(vec![update]).await {
return ctx.checkpoint_failure_unrecoverable(client_err).await;
}
child_failed(&ctx, &wire_id, sub_type, name.as_deref(), &wire);
return Err(op_err
.with_operation(&wire_id, CheckpointStatus::Failed.wire_str())
.with_wire(wire));
}
};
// A result too large to checkpoint is decoded before the
// write, so the summary sees the value the caller gets. Its
// decode error is routed only after the write, because the
// body ran and its success needs a record.
let (payload, decoded) = if serialized.len() > CHECKPOINT_SIZE_LIMIT_BYTES {
let decoded = ctx
.hold_activity(serdes.deserialize(serialized, serdes_ctx.clone()))
.await;
let payload = match (&decoded, &summary) {
(Ok(value), Some(summary)) => summary(value),
_ => String::new(),
};
(payload, Some(decoded))
} else {
(serialized, None)
};
ctx.await_retained_start(&wire_id, start, OperationAction::Succeed)
.await?;
let update = build_succeed_update(
&wire_id,
name.as_deref(),
sub_type,
&ctx,
&payload,
decoded.is_some(),
);
// The end hook reports the payload the update carries: the
// summary, or `""`, for a `ReplayChildren` result.
let result = update.payload().map(str::to_owned);
if let Err(err) = ctx.checkpoint_updates(vec![update]).await {
return ctx.checkpoint_failure_unrecoverable(err).await;
}
if let Some(plugins) = ctx.plugins() {
let info = child_info(&ctx, &wire_id, sub_type, name.as_deref())
.with_status(crate::OperationStatus::Succeeded)
.with_result(result)
.with_replay(false);
crate::plugin::operation_ended(plugins, &info);
}
match decoded {
Some(Ok(value)) => Ok(value),
Some(Err(err)) => {
Err(deserialize_failure(&ctx, name.as_deref(), &wire_id, err).await)
}
// Round-trip deserialize for consistency (first-run ==
// replay).
None => {
deserialize_value(&ctx, name.as_deref(), payload, &serdes, serdes_ctx).await
}
}
}
(ChildRunMode::Live, Err((child_err, wire))) => {
// Failure: checkpoint FAIL with error details. A runner's
// wire is the one its end hooks saw.
ctx.await_retained_start(&wire_id, start, OperationAction::Fail)
.await?;
Err(checkpoint_live_failure(
&ctx,
name.as_deref(),
sub_type,
&wire_id,
child_err,
wire,
)
.await)
}
}
}
}
/// The plugin info of the child context `wire_id`, created in `ctx`.
fn child_info(
ctx: &DurableContext,
wire_id: &str,
sub_type: &str,
name: Option<&str>,
) -> crate::plugin::OperationInfo {
crate::plugin::OperationInfo::for_operation(
ctx,
wire_id,
crate::OperationKind::Context,
sub_type,
name,
)
}
/// Fires `on_operation_end` for a child context whose `FAIL` with `wire`
/// was written.
fn child_failed(
ctx: &DurableContext,
wire_id: &str,
sub_type: &str,
name: Option<&str>,
wire: &crate::error::WireError,
) {
if let Some(plugins) = ctx.plugins() {
let info = child_info(ctx, wire_id, sub_type, name)
.with_status(crate::OperationStatus::Failed)
.with_error(Some(wire.clone()))
.with_replay(false);
crate::plugin::operation_ended(plugins, &info);
}
}
/// Builds an `OperationUpdate` for child context operations.
fn build_update(
wire_id: &str,
name: Option<&str>,
sub_type: &str,
action: OperationAction,
ctx: &DurableContext,
) -> OperationUpdate {
let mut builder = OperationUpdate::builder()
.id(wire_id.to_owned())
.r#type(OperationType::Context)
.sub_type(sub_type.to_owned())
.action(action);
if let Some(n) = name {
builder = builder.name(n);
}
// Set parent wire ID if this is a nested child (child-of-child).
if let Some(parent_wire) = ctx.parent_wire_id() {
builder = builder.parent_id(parent_wire);
}
// build() is infallible here: all required fields (id, type, action) set.
#[expect(clippy::expect_used)] // reason: all required fields are set above
builder
.build()
.expect("all required OperationUpdate fields set")
}
/// Builds the Succeed `OperationUpdate` for a live child result with
/// `payload`. An `oversized` result switches the operation to
/// `ReplayChildren` mode: `payload` is then its summary, or `""`, the
/// backend preserves the child operations, and the body re-executes on
/// replay to reconstruct the result. Within the plugin child operations
/// depth of `ctx`, a result that fits also carries `ReplayChildren`.
fn build_succeed_update(
wire_id: &str,
name: Option<&str>,
sub_type: &str,
ctx: &DurableContext,
payload: &str,
oversized: bool,
) -> OperationUpdate {
let mut succeed_builder = OperationUpdate::builder()
.id(wire_id.to_owned())
.r#type(OperationType::Context)
.sub_type(sub_type.to_owned())
.action(OperationAction::Succeed);
if let Some(n) = name {
succeed_builder = succeed_builder.name(n);
}
if let Some(parent_wire) = ctx.parent_wire_id() {
succeed_builder = succeed_builder.parent_id(parent_wire);
}
succeed_builder = succeed_builder.payload(payload.to_owned());
// Within the plugin child operations depth, the record keeps its
// children for later invocations.
if oversized || ctx.preserves_children(1) {
succeed_builder = succeed_builder.context_options(
aws_sdk_lambda::types::ContextOptions::builder()
.replay_children(true)
.build(),
);
}
#[expect(clippy::expect_used)] // reason: all required fields are set above
succeed_builder
.build()
.expect("all required OperationUpdate fields set")
}
/// Checkpoints a live child failure and returns the graded
/// `ChildContextError`. When the `FAIL` write fails, the failure is
/// recorded where its response arrives, and this never returns (see
/// [`DurableContext::checkpoint_failure_unrecoverable`]).
async fn checkpoint_live_failure(
ctx: &DurableContext,
name: Option<&str>,
sub_type: &str,
wire_id: &str,
child_err: ChildFnError,
wire: Option<crate::error::WireError>,
) -> OperationError {
// Derive the wire failure from the error the body returned: the type
// is the body error's recorded type, the message is its recorded
// message (see `wire_message`), and `error_data`/`stack_trace` pass through from its cause
// chain, so an inner failure's payload survives this boundary.
// A body without a runner (a `with_retry` attempt) has no wire yet.
let wire = wire.unwrap_or_else(|| child_err.failure_wire());
let update = build_child_fail_update(wire_id, name, sub_type, ctx, &wire);
// A rejected FAIL write fails the invocation, and the child writes
// nothing more.
if let Err(client_err) = ctx.checkpoint_updates(vec![update]).await {
return ctx.checkpoint_failure_unrecoverable(client_err).await;
}
child_failed(ctx, wire_id, sub_type, name, &wire);
OperationError::from_kind(OperationErrorKind::ChildContext(ChildContextError::new(
ChildContextErrorKind::ChildFailed,
Some(child_err.into_source()),
)))
.with_operation(wire_id, CheckpointStatus::Failed.wire_str())
.with_wire(wire)
}
/// Builds a child-context `FAIL` update carrying `wire` as its error.
/// `ctx` is the context the child record belongs to; within its plugin
/// child operations depth, the update carries `ReplayChildren`.
fn build_child_fail_update(
wire_id: &str,
name: Option<&str>,
sub_type: &str,
ctx: &DurableContext,
wire: &crate::error::WireError,
) -> OperationUpdate {
let mut fail_builder = OperationUpdate::builder()
.id(wire_id.to_owned())
.r#type(OperationType::Context)
.sub_type(sub_type.to_owned())
.action(OperationAction::Fail)
.error(wire.to_error_object());
if let Some(n) = name {
fail_builder = fail_builder.name(n);
}
if let Some(parent_wire) = ctx.parent_wire_id() {
fail_builder = fail_builder.parent_id(parent_wire);
}
// Within the plugin child operations depth, the record keeps its
// children for later invocations.
if ctx.preserves_children(1) {
fail_builder = fail_builder.context_options(
aws_sdk_lambda::types::ContextOptions::builder()
.replay_children(true)
.build(),
);
}
#[expect(clippy::expect_used)] // reason: all required fields are set above
fail_builder
.build()
.expect("all required OperationUpdate fields set")
}
/// Replays a successful child context result from the checkpoint log.
async fn replay_success<O, S: Serdes<O>>(
ctx: &DurableContext,
name: Option<&str>,
serdes: &S,
result: Option<String>,
serdes_ctx: SerdesContext,
) -> Result<O, OperationError> {
let payload = result.ok_or_else(|| {
child_internal_error("checkpointed Succeeded operation has no result payload")
})?;
deserialize_value(ctx, name, payload, serdes, serdes_ctx).await
}
/// Replays a failed child context result from the checkpoint log.
///
/// The recorded failure fields travel on the synthetic source rather
/// than being folded into a message, so the recorded `error_type` (and
/// `error_data`, when present) stays programmatically recoverable.
///
/// A record whose `error_type` is the serialization discriminator
/// ([`crate::error::SERIALIZATION_FAILED_ERROR_TYPE`]) reconstructs
/// `ChildContextErrorKind::Internal`, the kind the live serialization
/// path yielded after persisting that record, so replay reproduces the
/// recorded failure's classification (issue #43). Every other record is
/// a closure failure and reconstructs `ChildFailed`.
pub(crate) fn replay_failure(wire: crate::error::WireError, wire_id: &str) -> OperationError {
let kind = if wire.error_type() == Some(crate::error::SERIALIZATION_FAILED_ERROR_TYPE) {
ChildContextErrorKind::Internal
} else {
ChildContextErrorKind::ChildFailed
};
OperationError::from_kind(OperationErrorKind::ChildContext(ChildContextError::new(
kind,
Some(crate::error::ReplayedFailure::source_from(wire.clone())),
)))
.with_operation(wire_id, CheckpointStatus::Failed.wire_str())
.with_wire(wire)
}
/// Serializes a value through the configured serdes (ownership transfers;
/// the serdes decides where its work runs).
///
/// A marked error ends the invocation
/// ([`DurableContext::route_serdes_error`]); any other error becomes
/// `Internal`.
async fn serialize_value<O, S: Serdes<O>>(
ctx: &DurableContext,
name: Option<&str>,
value: O,
serdes: &S,
serdes_ctx: SerdesContext,
) -> Result<String, OperationError> {
let operation_id = serdes_ctx.operation_id().to_owned();
match ctx.hold_activity(serdes.serialize(value, serdes_ctx)).await {
Ok(serialized) => Ok(serialized),
Err(err) => {
let e = ctx
.route_serdes_error("serialize", &operation_id, name, err)
.await;
Err(child_internal_error(&format!(
"serialize result: {}",
crate::error::wire_message(&*e)
)))
}
}
}
/// Deserializes a wire payload through the configured serdes.
///
/// A marked error ends the invocation
/// ([`DurableContext::route_serdes_error`]); any other error becomes
/// `Internal`.
async fn deserialize_value<O, S: Serdes<O>>(
ctx: &DurableContext,
name: Option<&str>,
payload: String,
serdes: &S,
serdes_ctx: SerdesContext,
) -> Result<O, OperationError> {
let operation_id = serdes_ctx.operation_id().to_owned();
match ctx
.hold_activity(serdes.deserialize(payload, serdes_ctx))
.await
{
Ok(value) => Ok(value),
Err(err) => Err(deserialize_failure(ctx, name, &operation_id, err).await),
}
}
/// The error of a failed decode of the result of `operation_id`. A marked
/// error ends the invocation ([`DurableContext::route_serdes_error`]); any
/// other error becomes `Internal`.
async fn deserialize_failure(
ctx: &DurableContext,
name: Option<&str>,
operation_id: &str,
err: crate::BoxError,
) -> OperationError {
let e = ctx
.route_serdes_error("deserialize", operation_id, name, err)
.await;
child_internal_error(&format!(
"deserialize result: {}",
crate::error::wire_message(&*e)
))
}
/// Constructs a `ChildContextError::Internal` wrapped as an
/// `OperationError`; the message becomes the source frame, keeping the
/// kind a pure classification.
fn child_internal_error(message: &str) -> OperationError {
OperationError::from_kind(OperationErrorKind::ChildContext(ChildContextError::new(
ChildContextErrorKind::Internal,
Some(message.to_owned().into()),
)))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::context::DurableContext;
use crate::engine::{CheckpointLog, CheckpointRecord, CheckpointStatus};
use std::future::IntoFuture;
use std::sync::Arc;
use std::time::Duration;
#[tokio::test]
async fn child_failure_keeps_typed_error_data() {
let client = Arc::new(crate::client::InMemoryExecutionClient::new(Vec::new()));
let ctx = DurableContext::new_root_with_client(
"arn:test".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::empty()),
Arc::clone(&client) as Arc<dyn crate::client::ExecutionClient>,
"token0".to_owned(),
);
let result: Result<i32, OperationError> = ctx
.run_in_child_context(|child| async move {
let value = child
.step(|_| async {
Err::<i32, crate::BoxError>(Box::new(
crate::TypedError::with_type("PaymentError", "declined")
.with_error_data(r#"{"code":42}"#),
))
})
.retry_strategy(|_err, _attempt| crate::RetryDecision::Stop)
.await?;
Ok(value)
})
.await;
assert!(result.is_err(), "the child must fail");
let updates = client.recorded_updates();
let fail = updates
.iter()
.find(|u| {
matches!(u.action(), OperationAction::Fail)
&& matches!(u.r#type(), OperationType::Context)
})
.expect("the child records a FAIL update");
let error = fail
.error()
.expect("the FAIL update carries an error object");
assert_eq!(error.error_data(), Some(r#"{"code":42}"#));
assert!(error.stack_trace().is_empty(), "{error:?}");
}
/// Runs `body` as a child context against a fresh in-memory client and
/// returns the `ErrorType` and `ErrorMessage` of the child FAIL update.
async fn child_fail_record<F, Fut>(body: F) -> (Option<String>, Option<String>)
where
F: FnOnce(DurableContext) -> Fut + Send + 'static,
Fut: Future<Output = Result<i32, crate::BoxError>> + Send + 'static,
{
let client = Arc::new(crate::client::InMemoryExecutionClient::new(Vec::new()));
let ctx = DurableContext::new_root_with_client(
"arn:test".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::empty()),
Arc::clone(&client) as Arc<dyn crate::client::ExecutionClient>,
"token0".to_owned(),
);
let result: Result<i32, OperationError> = ctx.run_in_child_context(body).await;
assert!(result.is_err(), "the child must fail");
let updates = client.recorded_updates();
let fail = updates
.iter()
.find(|u| {
matches!(u.action(), OperationAction::Fail)
&& matches!(u.r#type(), OperationType::Context)
})
.expect("the child records a FAIL update");
let error = fail
.error()
.expect("the FAIL update carries an error object");
(
error.error_type().map(str::to_owned),
error.error_message().map(str::to_owned),
)
}
/// A failed child records the type and message of the error its body
/// returned, not of the SDK's internal carrier. A propagated
/// operation failure records that operation's recorded message. One
/// row per case: a plain error, a `TypedError`, a propagated step
/// failure (untyped and typed), a propagated external callback
/// failure, and a `TypedError` around a propagated step failure.
#[tokio::test]
async fn child_failure_records_body_error_type() {
// Row 1: a plain error records the fallback type "Error".
let (ty, msg) = child_fail_record(|_child| async { Err("plain boom".into()) }).await;
assert_eq!(ty.as_deref(), Some("Error"));
assert_eq!(msg.as_deref(), Some("plain boom"));
// Row 2: a TypedError records the user's type.
let (ty, msg) = child_fail_record(|_child| async {
Err(crate::TypedError::with_type("PaymentError", "declined").into())
})
.await;
assert_eq!(ty.as_deref(), Some("PaymentError"));
assert_eq!(msg.as_deref(), Some("declined"));
// Row 3: a propagated untyped step failure records the step's
// recorded type, "Error".
let (ty, msg) = child_fail_record(|child| async move {
let v = child
.step(|_| async { Err::<i32, crate::BoxError>("step boom".into()) })
.retry_strategy(|_err, _attempt| crate::RetryDecision::Stop)
.await?;
Ok(v)
})
.await;
assert_eq!(ty.as_deref(), Some("Error"));
assert_eq!(msg.as_deref(), Some("step boom"));
// Row 4: a propagated typed step failure records the user's type.
let (ty, msg) = child_fail_record(|child| async move {
let v = child
.step(|_| async {
Err::<i32, crate::BoxError>(Box::new(crate::TypedError::with_type(
"PaymentError",
"declined",
)))
})
.retry_strategy(|_err, _attempt| crate::RetryDecision::Stop)
.await?;
Ok(v)
})
.await;
assert_eq!(ty.as_deref(), Some("PaymentError"));
assert_eq!(msg.as_deref(), Some("declined"));
// Row 5: a propagated external callback failure records the type
// the callback failure carries.
let (ty, msg) = child_fail_record(|_child| async {
let wire = crate::error::WireError::new(Some("VendorError"), Some("denied"));
let err = OperationError::from_kind(OperationErrorKind::Callback(
crate::error::CallbackError::new(
crate::error::CallbackErrorKind::ExternalFailure,
Some(crate::error::ReplayedFailure::source_from(wire.clone())),
),
))
.with_wire(wire);
Err(err.into())
})
.await;
assert_eq!(ty.as_deref(), Some("VendorError"));
assert_eq!(msg.as_deref(), Some("denied"));
// Row 6: a TypedError around a propagated step failure records the
// user's type and the step's recorded message.
let (ty, msg) = child_fail_record(|child| async move {
let v = child
.step(|_| async { Err::<i32, crate::BoxError>("step boom".into()) })
.retry_strategy(|_err, _attempt| crate::RetryDecision::Stop)
.await
.map_err(|e| crate::TypedError::with_type("PaymentError", e))?;
Ok(v)
})
.await;
assert_eq!(ty.as_deref(), Some("PaymentError"));
assert_eq!(msg.as_deref(), Some("step boom"));
}
/// A root context over `records` whose transport is a fresh in-memory
/// client, with the recorder's `p0` and `p1` as the invocation's
/// plugins and no checkpoint buffer window.
fn uf_ctx(
hooks: &crate::plugin::recorder::UfHooks,
records: Vec<(String, CheckpointRecord)>,
) -> (DurableContext, Arc<crate::client::InMemoryExecutionClient>) {
let client = Arc::new(crate::client::InMemoryExecutionClient::new(Vec::new()));
let ctx = DurableContext::new_root_with_client_and_defaults(
"arn:test".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::from_records(records)),
Arc::clone(&client) as Arc<dyn crate::client::ExecutionClient>,
"token0".to_owned(),
Duration::ZERO,
0,
Some(hooks.plugins()),
);
(ctx, client)
}
/// A record of the operation at `positional_id` with `status` and no
/// payload.
fn record_at(positional_id: &str, status: CheckpointStatus) -> (String, CheckpointRecord) {
let (wire_id, mut record) = succeeded_record(positional_id, "null");
record.status = status;
record.result = None;
(wire_id, record)
}
/// The fatal error message `ctx`'s invocation recorded.
fn fatal_message(ctx: &DurableContext) -> String {
ctx.coordinator()
.fatal_error()
.expect("fatal recorded")
.error_message
}
/// Rule 8.2: a failing child, `map` item and `parallel` branch derive
/// their wire under `ActiveBody::Outcome`, so a wait that the error's
/// `Display` calls on the captured child context fails the execution
/// with a context usage error naming the operation. The `Display`
/// runs once. That record precedes the operation's `FAIL`, so the
/// `FAIL` is refused, no terminal update is written, and the
/// operation never resolves.
#[tokio::test]
async fn failing_child_item_branch_display_calls_its_context() {
use crate::builders::map_parallel::CompletionConfig;
use crate::plugin::recorder::{Counted, UfHooks};
let claims_wait = |dc: &Counted, child: DurableContext| -> crate::BoxError {
dc.sharing(move || drop(child.wait(Duration::from_secs(1))))
.boxed()
};
let terminal = |client: &crate::client::InMemoryExecutionClient| -> Vec<String> {
client
.recorded_updates()
.into_iter()
.filter(|u| matches!(u.action(), OperationAction::Fail | OperationAction::Succeed))
.map(|u| u.name().unwrap_or("").to_owned())
.collect()
};
let unresolved = |what: &str| format!("the {what}'s FAIL is refused, so it never resolves");
// A child.
let (ctx, client) = uf_ctx(&UfHooks::default(), Vec::new());
let dc = Counted::new();
let body_dc = dc.clone();
let child = ctx
.run_in_child_context(move |child| async move {
Err::<u32, crate::BoxError>(claims_wait(&body_dc, child))
})
.name("c");
assert!(
tokio::time::timeout(Duration::from_millis(50), child.into_future())
.await
.is_err(),
"{}",
unresolved("child")
);
assert_eq!(dc.calls(), 1, "child");
assert_eq!(terminal(&client), Vec::<String>::new(), "child");
let message = fatal_message(&ctx);
assert!(
message.contains("while the SDK records the outcome of operation \"1\""),
"{message}"
);
// A `map` item.
let (ctx, client) = uf_ctx(&UfHooks::default(), Vec::new());
let dc = Counted::new();
let body_dc = dc.clone();
let batch = ctx
.map(vec![1_u32], move |child, _item, _idx| {
let error = claims_wait(&body_dc, child);
async move { Err::<u32, crate::BoxError>(error) }
})
.name("m")
.completion(CompletionConfig::with_tolerated_failure_count(1))
.await_batch();
assert!(
tokio::time::timeout(Duration::from_millis(50), batch)
.await
.is_err(),
"{}",
unresolved("map item")
);
assert_eq!(dc.calls(), 1, "map item");
assert_eq!(terminal(&client), Vec::<String>::new(), "map item");
let message = fatal_message(&ctx);
assert!(
message.contains("while the SDK records the outcome of operation \"1-1\""),
"{message}"
);
// A `parallel` branch.
let (ctx, client) = uf_ctx(&UfHooks::default(), Vec::new());
let dc = Counted::new();
let body_dc = dc.clone();
let branch = crate::Branch::new("b", move |child| async move {
Err::<u32, crate::BoxError>(claims_wait(&body_dc, child))
});
let batch = ctx
.parallel(vec![branch])
.name("p")
.completion(CompletionConfig::with_tolerated_failure_count(1))
.await_batch();
assert!(
tokio::time::timeout(Duration::from_millis(50), batch)
.await
.is_err(),
"{}",
unresolved("parallel branch")
);
assert_eq!(dc.calls(), 1, "parallel branch");
assert_eq!(terminal(&client), Vec::<String>::new(), "parallel branch");
let message = fatal_message(&ctx);
assert!(
message.contains("while the SDK records the outcome of operation \"1-1\""),
"{message}"
);
}
/// Rule 7.3: the user-function hooks and wraps of a child get the
/// child context's replay flag when the body starts: false in the
/// invocation that starts it, true in the one that resumes it over
/// its inner wait. The wraps and the end hook get the start hook's
/// info.
#[tokio::test]
async fn child_body_info_has_child_replay_flag() {
use crate::plugin::recorder::UfHooks;
let child = |ctx: &DurableContext| {
ctx.run_in_child_context(|child| async move {
child.wait(Duration::from_secs(1)).name("w").await?;
Ok(5_u32)
})
.name("c")
};
// Invocation 1: the child suspends at its wait.
let hooks = UfHooks::default();
let (ctx, _client) = uf_ctx(&hooks, Vec::new());
let signal = Arc::clone(ctx.coordinator());
let ownership = Arc::clone(ctx.task_ownership());
let outcome =
crate::driver::test_support::outcome_of(ctx.clone(), ownership, signal, child(&ctx))
.await;
assert_eq!(outcome, crate::driver::InvocationOutcome::Pending);
let start = hooks.find("c", "uf_start");
assert!(!start.replay);
assert!(!hooks.hooks_of("c").contains(&"uf_end"));
// Invocation 2: the child is STARTED and its wait succeeded.
let hooks = UfHooks::default();
let records = vec![
record_at("1", CheckpointStatus::Started),
record_at("1-1", CheckpointStatus::Succeeded),
];
let (ctx, _client) = uf_ctx(&hooks, records);
assert_eq!(child(&ctx).await.unwrap(), 5);
let start = hooks.find("c", "uf_start");
assert!(start.replay);
let records = hooks.of("c");
let later: Vec<_> = records
.iter()
.filter(|r| matches!(r.hook, "wrap_enter" | "wrap_exit" | "uf_end"))
.collect();
assert!(later.iter().any(|r| r.hook == "uf_end"), "{records:?}");
for record in &later {
assert_eq!(
record.info, start.info,
"{} gets the start hook's info",
record.hook
);
}
assert_eq!(hooks.find("c", "uf_end").ok, Some(true));
}
/// Rule 7.3: a STARTED child with no inner record reports a replay to
/// `on_operation_start` and none to its user-function hooks and wraps.
#[tokio::test]
async fn started_child_without_inner_record_replay_flags() {
use crate::plugin::recorder::UfHooks;
let hooks = UfHooks::default();
let (ctx, _client) = uf_ctx(&hooks, vec![record_at("1", CheckpointStatus::Started)]);
let value = ctx
.run_in_child_context(|child| async move {
Ok(child.step(|_| async { Ok(3_u32) }).name("s").await?)
})
.name("c")
.await
.unwrap();
assert_eq!(value, 3);
assert!(hooks.find("c", "start").replay);
let records = hooks.of("c");
let user_function: Vec<_> = records
.iter()
.filter(|r| matches!(r.hook, "uf_start" | "wrap_enter" | "wrap_exit" | "uf_end"))
.collect();
// One start, a wrap per poll, one end.
let hook_names: Vec<_> = user_function.iter().map(|r| r.hook).collect();
let wraps = hook_names
.get(1..hook_names.len().saturating_sub(1))
.unwrap_or_default();
assert_eq!(hook_names.first(), Some(&"uf_start"), "{hook_names:?}");
assert_eq!(hook_names.last(), Some(&"uf_end"), "{hook_names:?}");
assert!(
!wraps.is_empty()
&& wraps
.chunks(2)
.all(|pair| pair == ["wrap_enter", "wrap_exit"]),
"{hook_names:?}"
);
assert!(user_function.iter().all(|r| !r.replay), "{user_function:?}");
}
/// Rule 7.9: a child's scope closes before its end hooks. At the end
/// hook the scope admits nothing and the step the body spawned and did
/// not await is aborted; the child records the body's result.
#[tokio::test]
async fn child_scope_closed_before_end_hooks() {
use crate::plugin::recorder::UfHooks;
use std::sync::atomic::{AtomicBool, Ordering};
/// Sets its flag when dropped.
struct Dropped(Arc<AtomicBool>);
impl Drop for Dropped {
fn drop(&mut self) {
self.0.store(true, Ordering::SeqCst);
}
}
let child_wire = crate::engine::compute_wire_id_public("1");
let admission: Arc<std::sync::OnceLock<Arc<crate::checkpoint_coalescer::Admission>>> =
Arc::default();
let probe_admission = Arc::clone(&admission);
let probe_wire = child_wire.clone();
let hooks = UfHooks::probing(move || {
let Some(admission) = probe_admission.get() else {
return String::new();
};
format!(
"admits={} task={}",
admission.admits(std::slice::from_ref(&probe_wire)),
admission.has_task_in(&probe_wire)
)
});
let (ctx, client) = uf_ctx(&hooks, Vec::new());
admission
.set(Arc::clone(ctx.admission()))
.expect("set once");
let dropped = Arc::new(AtomicBool::new(false));
let guard_flag = Arc::clone(&dropped);
let outcome = tokio::time::timeout(
Duration::from_secs(5),
crate::driver::test_support::drive_handler(ctx, None, move |ctx| async move {
let value = ctx
.run_in_child_context(move |child| async move {
// The step task holds the only references.
let guard = Arc::new(Dropped(guard_flag));
let pending = child
.step(move |_| async move {
let _guard = guard;
std::future::pending::<()>().await;
Ok(0_u32)
})
.name("pending")
.spawn();
drop(pending);
Ok(5_u32)
})
.name("c")
.await
.map_err(crate::driver::test_support::wire_err)?;
Ok(value.to_string())
}),
)
.await
.expect("the invocation ends within 5 s")
.expect("no panic");
assert_eq!(
outcome,
crate::driver::InvocationOutcome::Complete("5".to_owned())
);
assert_eq!(hooks.find("c", "uf_start").probe, "admits=true task=false");
assert_eq!(
hooks.find("c", "wrap_enter").probe,
"admits=true task=false"
);
assert_eq!(
hooks.find("c", "wrap_exit").probe,
"admits=false task=false"
);
assert_eq!(hooks.find("c", "uf_end").probe, "admits=false task=false");
let succeed = client
.recorded_updates()
.into_iter()
.find(|u| u.name() == Some("c") && u.action() == &OperationAction::Succeed)
.expect("the child's SUCCEED");
assert_eq!(succeed.payload(), Some("5"));
tokio::time::timeout(Duration::from_secs(5), async {
while !dropped.load(Ordering::SeqCst) {
tokio::task::yield_now().await;
}
})
.await
.expect("the step's task stopped");
assert!(
client
.recorded_updates()
.iter()
.all(|u| u.name() != Some("pending") || u.action() == &OperationAction::Start),
"the stopped step records no outcome"
);
}
/// Helper to create a test context with the given checkpoint log.
fn test_ctx(log: CheckpointLog) -> DurableContext {
DurableContext::new_root(
"arn:aws:lambda:us-east-1:123456789012:function:test".to_owned(),
lambda_runtime::Context::default(),
Arc::new(log),
)
}
/// Helper to create a checkpoint record for a succeeded child context.
fn succeeded_record(positional_id: &str, result: &str) -> (String, CheckpointRecord) {
let wire_id = crate::engine::compute_wire_id_public(positional_id);
(
wire_id.clone(),
CheckpointRecord {
id: wire_id,
status: CheckpointStatus::Succeeded,
result: Some(result.to_owned()),
error_type: None,
error_message: None,
error_data: None,
stack_trace: None,
attempt: 0,
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,
},
)
}
/// Helper to create a checkpoint record for a failed child context.
fn failed_record(
positional_id: &str,
err_type: &str,
err_msg: &str,
) -> (String, CheckpointRecord) {
let wire_id = crate::engine::compute_wire_id_public(positional_id);
(
wire_id.clone(),
CheckpointRecord {
id: wire_id,
status: CheckpointStatus::Failed,
result: None,
error_type: Some(err_type.to_owned()),
error_message: Some(err_msg.to_owned()),
error_data: None,
stack_trace: None,
attempt: 0,
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,
},
)
}
/// A fresh root context over an in-memory client, and the client.
fn live_ctx() -> (DurableContext, Arc<crate::client::InMemoryExecutionClient>) {
let client = Arc::new(crate::client::InMemoryExecutionClient::new(Vec::new()));
let ctx = DurableContext::new_root_with_client(
"arn:test".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::empty()),
Arc::clone(&client) as Arc<dyn crate::client::ExecutionClient>,
"token0".to_owned(),
);
(ctx, client)
}
fn checkout() -> crate::OperationSubType {
crate::OperationSubType::new("Checkout").expect("a valid subtype")
}
/// The actions of the updates named `name`, after asserting that each
/// of them carries the subtype `Checkout`.
fn checkout_actions(updates: &[OperationUpdate], name: &str) -> Vec<OperationAction> {
updates
.iter()
.filter(|u| u.name() == Some(name))
.map(|u| {
assert_eq!(u.sub_type(), Some("Checkout"), "{u:?}");
u.action().clone()
})
.collect()
}
/// A serdes whose `serialize` returns a plain, unmarked error.
struct PlainSerializeFailure;
impl<T: serde::de::DeserializeOwned + Send + 'static> Serdes<T> for PlainSerializeFailure {
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn serialize(
&self,
_value: T,
_context: SerdesContext,
) -> Result<String, crate::BoxError> {
Err("cannot encode".into())
}
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn deserialize(
&self,
wire: String,
_context: SerdesContext,
) -> Result<T, crate::BoxError> {
Ok(serde_json::from_str(&wire)?)
}
}
/// Every update of a succeeded child, of a child whose body fails and
/// of a child whose result fails to serialize carries the custom
/// subtype.
#[tokio::test]
async fn custom_sub_type_on_every_child_update() {
let (ctx, client) = live_ctx();
let ok: Result<u32, OperationError> = ctx
.run_in_child_context(|_child| async { Ok(1_u32) })
.name("ok")
.sub_type(checkout())
.await;
assert_eq!(ok.ok(), Some(1));
let body_fails: Result<u32, OperationError> = ctx
.run_in_child_context(|_child| async { Err("declined".into()) })
.name("body-fails")
.sub_type(checkout())
.await;
assert!(body_fails.is_err());
let serdes_fails: Result<u32, OperationError> = ctx
.run_in_child_context(|_child| async { Ok(3_u32) })
.name("serdes-fails")
.sub_type(checkout())
.serdes(PlainSerializeFailure)
.await;
assert!(serdes_fails.is_err());
let updates = client.recorded_updates();
assert_eq!(
checkout_actions(&updates, "ok"),
[OperationAction::Start, OperationAction::Succeed]
);
assert_eq!(
checkout_actions(&updates, "body-fails"),
[OperationAction::Start, OperationAction::Fail]
);
assert_eq!(
checkout_actions(&updates, "serdes-fails"),
[OperationAction::Start, OperationAction::Fail]
);
}
/// Whether `update` carries `ContextOptions` with `ReplayChildren`.
fn has_replay_children(update: &OperationUpdate) -> Option<bool> {
update
.context_options()
.and_then(aws_sdk_lambda::types::ContextOptions::replay_children)
}
/// The update of the operation named `name` with `action`.
fn named_update(
updates: &[OperationUpdate],
name: &str,
action: &OperationAction,
) -> OperationUpdate {
updates
.iter()
.find(|u| u.name() == Some(name) && u.action() == action)
.cloned()
.unwrap_or_else(|| unreachable!("no {action:?} update for {name}"))
}
/// Within the depth, a child's SUCCEED keeps its payload and carries
/// `ReplayChildren`.
#[tokio::test]
async fn preserved_child_keeps_payload_and_flag() {
let (ctx, client) = DurableContext::test_context_with_depth(1);
let value: String = ctx
.run_in_child_context(|_child| async { Ok("x".to_owned()) })
.name("c")
.await
.expect("child succeeds");
assert_eq!(value, "x");
let succeed = named_update(&client.recorded_updates(), "c", &OperationAction::Succeed);
assert_eq!(has_replay_children(&succeed), Some(true));
assert_eq!(succeed.payload(), Some("\"x\""));
}
/// Within the depth, a child's FAIL carries `ReplayChildren`; at depth 0
/// it carries no `ContextOptions`.
#[tokio::test]
async fn preserved_child_failure_has_flag() {
for (depth, flag) in [(1, Some(true)), (0, None)] {
let (ctx, client) = DurableContext::test_context_with_depth(depth);
let result: Result<u32, OperationError> = ctx
.run_in_child_context(|_child| async { Err("declined".into()) })
.name("c")
.await;
assert!(result.is_err());
let fail = named_update(&client.recorded_updates(), "c", &OperationAction::Fail);
assert_eq!(has_replay_children(&fail), flag, "depth {depth}");
}
}
/// A child inside a top-level child sits at level 2: depth 1 flags only
/// the outer record.
#[tokio::test]
async fn nested_child_is_flagged_only_within_depth() {
let (ctx, client) = DurableContext::test_context_with_depth(1);
let value: u32 = ctx
.run_in_child_context(|child| async move {
Ok(child
.run_in_child_context(|_inner| async { Ok(7_u32) })
.name("inner")
.await?)
})
.name("outer")
.await
.expect("children succeed");
assert_eq!(value, 7);
let updates = client.recorded_updates();
let outer = named_update(&updates, "outer", &OperationAction::Succeed);
let inner = named_update(&updates, "inner", &OperationAction::Succeed);
assert_eq!(has_replay_children(&outer), Some(true));
assert_eq!(has_replay_children(&inner), None);
assert_eq!(inner.payload(), Some("7"));
}
/// A child whose result fails to serialize writes its FAIL through the
/// same builder, so it carries the flag within the depth.
#[tokio::test]
async fn preserved_child_serialization_failure_has_flag() {
let (ctx, client) = DurableContext::test_context_with_depth(1);
let result: Result<u32, OperationError> = ctx
.run_in_child_context(|_child| async { Ok(3_u32) })
.name("c")
.serdes(PlainSerializeFailure)
.await;
assert!(result.is_err());
let fail = named_update(&client.recorded_updates(), "c", &OperationAction::Fail);
assert_eq!(has_replay_children(&fail), Some(true));
}
/// A result too large to checkpoint writes a `ReplayChildren` SUCCEED
/// that carries the custom subtype.
#[tokio::test]
async fn custom_sub_type_on_replay_children_succeed() {
let (ctx, client) = live_ctx();
let value: Result<String, OperationError> = ctx
.run_in_child_context(|_child| async { Ok("x".repeat(300 * 1024)) })
.name("big")
.sub_type(checkout())
.await;
assert_eq!(value.map(|v| v.len()).ok(), Some(300 * 1024));
let updates = client.recorded_updates();
let succeed = updates
.iter()
.find(|u| matches!(u.action(), OperationAction::Succeed))
.expect("the SUCCEED update is recorded");
assert_eq!(
succeed
.context_options()
.and_then(aws_sdk_lambda::types::ContextOptions::replay_children),
Some(true)
);
assert_eq!(succeed.payload(), Some(""));
assert_eq!(succeed.sub_type(), Some("Checkout"));
}
/// Runs `child` with the START write accepted and the next write
/// rejected, and returns the recorded updates.
async fn reject_second_write<F>(
ctx: DurableContext,
client: &crate::client::InMemoryExecutionClient,
child: F,
) -> Vec<OperationUpdate>
where
F: IntoFuture + Send + 'static,
F::IntoFuture: Send + 'static,
{
use crate::client::TestResponse;
client.enqueue_checkpoint_response(TestResponse::Success(vec![]));
client.enqueue_checkpoint_response(TestResponse::NonRetryableError(
"payload rejected".to_owned(),
));
let signal = Arc::clone(ctx.coordinator());
let outcome = crate::driver::test_support::outcome_of(
ctx.clone(),
Arc::clone(ctx.task_ownership()),
signal,
child,
)
.await;
assert!(
matches!(outcome, crate::driver::InvocationOutcome::Failed { .. }),
"a rejected write fails the execution: {outcome:?}"
);
client.recorded_updates()
}
/// The write the service rejects carries the custom subtype, and no
/// terminal FAIL follows it: a rejected SUCCEED, a rejected FAIL of a
/// body failure, and a rejected FAIL of a serialization failure. The
/// child's record stays at its accepted START.
#[tokio::test]
async fn custom_sub_type_on_rejected_write_and_no_terminal_fail() {
// A rejected SUCCEED.
let (ctx, client) = live_ctx();
let child = ctx
.run_in_child_context(|_child| async { Ok(1_u32) })
.name("c")
.sub_type(checkout());
let updates = reject_second_write(ctx, &client, child).await;
assert_eq!(
checkout_actions(&updates, "c"),
[OperationAction::Start, OperationAction::Succeed]
);
// A rejected FAIL of a body failure.
let (ctx, client) = live_ctx();
let child = ctx
.run_in_child_context(|_child| async { Err::<u32, crate::BoxError>("no".into()) })
.name("c")
.sub_type(checkout());
let updates = reject_second_write(ctx, &client, child).await;
assert_eq!(
checkout_actions(&updates, "c"),
[OperationAction::Start, OperationAction::Fail]
);
// A rejected FAIL of a serialization failure.
let (ctx, client) = live_ctx();
let child = ctx
.run_in_child_context(|_child| async { Ok(1_u32) })
.name("c")
.sub_type(checkout())
.serdes(PlainSerializeFailure);
let updates = reject_second_write(ctx, &client, child).await;
assert_eq!(
checkout_actions(&updates, "c"),
[OperationAction::Start, OperationAction::Fail]
);
}
/// A record with the custom subtype replays its success and its
/// failure when the child sets the same subtype.
#[tokio::test]
async fn custom_sub_type_replays_recorded_outcomes() {
let (wire_id, mut record) = succeeded_record("1", "7");
record.op_type = Some("CONTEXT".to_owned());
record.sub_type = Some("Checkout".to_owned());
let ctx = test_ctx(CheckpointLog::from_records(vec![(wire_id, record)]));
let value: Result<u32, OperationError> = ctx
.run_in_child_context(|_child| async { unreachable!("replayed") })
.sub_type(checkout())
.await;
assert_eq!(value.ok(), Some(7));
let (wire_id, mut record) = failed_record("1", "Error", "no");
record.op_type = Some("CONTEXT".to_owned());
record.sub_type = Some("Checkout".to_owned());
let ctx = test_ctx(CheckpointLog::from_records(vec![(wire_id, record)]));
let value: Result<u32, OperationError> = ctx
.run_in_child_context(|_child| async { unreachable!("replayed") })
.sub_type(checkout())
.await;
assert!(value.is_err());
}
#[tokio::test]
async fn child_live_path_runs_closure() {
// A live client: the child's checkpoint writes must succeed for
// the closure to run to completion. (Pre-#43 this test used a
// client-less context and asserted the checkpoint failure
// surfaced as Err; a rejected write now parks the future for the
// invocation driver instead of yielding.)
let client = Arc::new(crate::client::InMemoryExecutionClient::new(Vec::new()));
let ctx = DurableContext::new_root_with_client(
"arn:aws:lambda:us-east-1:123456789012:function:test".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::empty()),
client as Arc<dyn crate::client::ExecutionClient>,
"token0".to_owned(),
);
let result: Result<i32, OperationError> = ctx
.run_in_child_context(|child_ctx| async move {
// Child context has prefix "1" (first op of root); its
// inner step runs and checkpoints under that namespace.
let step_result = child_ctx.step(|_| async { Ok(42) }).await?;
Ok(step_result)
})
.await;
let value = result.unwrap();
assert_eq!(value, 42);
}
#[tokio::test]
async fn child_replay_returns_frozen_result() {
// Set up: operation "1" is a succeeded child context in the log.
let log = CheckpointLog::from_records(vec![succeeded_record("1", r#""hello""#)]);
let ctx = test_ctx(log);
let result: Result<String, OperationError> = ctx
.run_in_child_context(|_child_ctx| async move {
// This should NOT execute during replay.
unreachable!("child body should not run during replay");
})
.await;
let value = result.unwrap();
assert_eq!(value, "hello");
}
/// A serdes whose every call fails with a marked error.
struct MarkedFailure;
impl<T: Send + 'static> Serdes<T> for MarkedFailure {
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn serialize(
&self,
_value: T,
_context: SerdesContext,
) -> Result<String, crate::BoxError> {
Err(crate::serdes::RetryableSerdesError::new("injected failure").into())
}
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn deserialize(
&self,
_wire: String,
_context: SerdesContext,
) -> Result<T, crate::BoxError> {
Err(crate::serdes::RetryableSerdesError::new("injected failure").into())
}
}
#[tokio::test]
async fn child_reconstruct_marked_failure_faults_invocation() {
// A ReplayChildren child: the recorded success carries no payload,
// so the body runs again and its result takes the round trip.
let (wire_id, mut record) = succeeded_record("1", "");
record.result = None;
record.replay_children = true;
let ctx = test_ctx(CheckpointLog::from_records(vec![(wire_id, record)]));
let signal = Arc::clone(ctx.coordinator());
let child = ctx
.run_in_child_context(|_child_ctx| async move { Ok(7_i32) })
.name("rebuild")
.serdes(MarkedFailure);
crate::driver::test_support::expect_serdes_fault(
ctx.clone(),
Arc::clone(ctx.task_ownership()),
signal,
child,
"serdes serialize failed for operation \"rebuild\"",
)
.await;
}
#[tokio::test]
async fn child_replay_failure_returns_error() {
let log =
CheckpointLog::from_records(vec![failed_record("1", "ChildFnError", "step exploded")]);
let ctx = test_ctx(log);
let result: Result<String, OperationError> = ctx
.run_in_child_context(|_child_ctx| async move {
unreachable!("child body should not run during replay of failure");
})
.await;
assert!(result.is_err());
let err = result.unwrap_err();
let msg = format!("{err:#}");
assert!(
msg.contains("step exploded"),
"error should contain original message: {msg}"
);
}
#[tokio::test]
async fn nested_child_prefix_chains_two_deep() {
// Operation "1" is a child (prefix "1"), and "1-1" is its child
// (prefix "1-1"). Verify nested replay works.
let log = CheckpointLog::from_records(vec![succeeded_record("1", r#""outer""#)]);
let ctx = test_ctx(log);
// Outer child replays immediately with "outer".
let result: Result<String, OperationError> = ctx
.run_in_child_context(|_child_ctx| async move {
unreachable!("outer child should replay");
})
.await;
let value = result.unwrap();
assert_eq!(value, "outer");
}
#[tokio::test]
async fn child_fn_error_propagation() {
// Without a client, the checkpoint will fail, but the error handling
// path tests the grading logic. Use a replay-failure record to test
// the ChildFnError → ChildContextError grading.
let log = CheckpointLog::from_records(vec![failed_record(
"1",
"ChildFnError",
"child function error: operation error: step: execution failed: boom",
)]);
let ctx = test_ctx(log);
let result: Result<String, OperationError> = ctx
.run_in_child_context(|_child_ctx| async move {
unreachable!();
})
.await;
let err = result.unwrap_err();
match err.kind() {
OperationErrorKind::ChildContext(ce) => match ce.kind() {
ChildContextErrorKind::ChildFailed => {
let message = crate::error::chain_string(ce);
assert!(message.contains("boom"), "chain: {message}");
}
other => unreachable!("unexpected kind: {other:?}"),
},
other => unreachable!("unexpected kind: {other:?}"),
}
}
#[tokio::test]
async fn spawn_child_starts_eagerly() {
use tokio::sync::oneshot;
// A working in-memory client: the child's START checkpoint must
// succeed for the body to run. (Pre-#43 this test used a
// client-less context and passed vacuously: the rejected START
// write dropped the sender, which settled `rx` without the body
// ever running. A rejected write now parks the child future for
// the driver instead of yielding, so the body needs a live
// checkpoint channel to demonstrate eagerness.)
let client = Arc::new(crate::client::InMemoryExecutionClient::new(Vec::new()));
let ctx = DurableContext::new_root_with_client(
"arn:aws:lambda:us-east-1:123456789012:function:test".to_owned(),
lambda_runtime::Context::default(),
Arc::new(CheckpointLog::empty()),
client as Arc<dyn crate::client::ExecutionClient>,
"token0".to_owned(),
);
let (tx, rx) = oneshot::channel::<()>();
let handle = ctx
.run_in_child_context(move |_child_ctx| async move {
// Signal that the child body has started executing.
let _ = tx.send(());
Err("test complete".into())
})
.spawn();
// The child body should have started BEFORE we await the handle.
let signal = tokio::time::timeout(Duration::from_secs(2), rx).await;
assert_eq!(
signal.ok().and_then(Result::ok),
Some(()),
"child body should start executing before await"
);
// The handle should eventually resolve (with the child's error).
let result: Result<String, OperationError> = handle.await;
assert!(result.is_err());
}
#[tokio::test]
async fn concurrent_spawned_children_deterministic_ids() {
// Two spawned children: IDs are claimed at builder creation, so
// they are always "1" and "2" regardless of execution order.
let log = CheckpointLog::from_records(vec![
succeeded_record("1", r#""first""#),
succeeded_record("2", r#""second""#),
]);
let ctx = test_ctx(log);
// Both will replay from checkpoint (no execution needed).
let handle_a = ctx
.run_in_child_context(|_| async move {
unreachable!("should replay");
})
.spawn();
let handle_b = ctx
.run_in_child_context(|_| async move {
unreachable!("should replay");
})
.spawn();
// Await in reverse order: IDs were already claimed.
let result_b: String = handle_b.await.expect("b should succeed");
let result_a: String = handle_a.await.expect("a should succeed");
assert_eq!(result_a, "first");
assert_eq!(result_b, "second");
}
#[tokio::test]
async fn ownership_check_in_child_context() {
// Verify that the child context inherits task ownership from
// the parent: operations in the child from a foreign task fail.
// This is tested via the replay path (no client needed).
// The child's checkpoint_record lookup uses its own prefix.
let log = CheckpointLog::from_records(vec![succeeded_record("1", "42")]);
let ctx = test_ctx(log);
let result: Result<i32, OperationError> = ctx
.run_in_child_context(|_| async move {
unreachable!("should replay");
})
.await;
// Replay succeeds: ownership check passes (same task).
assert_eq!(result.expect("should succeed"), 42);
}
/// A child whose result is the 300 KiB string, named `big`, with the
/// summary `len=<length>` that counts its calls in `calls`.
async fn run_big_child(
ctx: &DurableContext,
value: String,
calls: &Arc<std::sync::atomic::AtomicUsize>,
) -> Result<String, OperationError> {
let calls = Arc::clone(calls);
ctx.run_in_child_context(move |_child| async move { Ok(value) })
.name("big")
.summary(move |s: &String| {
calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
format!("len={}", s.len())
})
.await
}
/// An oversized child records its summary as the `ReplayChildren`
/// payload, and the summary runs once.
#[tokio::test]
async fn child_summary_written_when_oversized() {
let (ctx, client) = live_ctx();
let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let value = run_big_child(&ctx, "x".repeat(300 * 1024), &calls)
.await
.expect("child succeeds");
assert_eq!(value.len(), 300 * 1024);
let succeed = named_update(&client.recorded_updates(), "big", &OperationAction::Succeed);
assert_eq!(has_replay_children(&succeed), Some(true));
assert_eq!(succeed.payload(), Some("len=307200"));
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 1);
}
/// A result that fits is recorded as before, and the summary is not
/// called.
#[tokio::test]
async fn child_summary_not_called_under_limit() {
let (ctx, client) = live_ctx();
let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let value = run_big_child(&ctx, "small".to_owned(), &calls)
.await
.expect("child succeeds");
assert_eq!(value, "small");
let succeed = named_update(&client.recorded_updates(), "big", &OperationAction::Succeed);
assert_eq!(succeed.payload(), Some("\"small\""));
assert!(succeed.context_options().is_none(), "{succeed:?}");
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 0);
}
/// An oversized child without a summary records the payload `""`.
#[tokio::test]
async fn oversized_child_without_summary_writes_empty_payload() {
let (ctx, client) = live_ctx();
let value: String = ctx
.run_in_child_context(|_child| async { Ok("x".repeat(300 * 1024)) })
.name("big")
.await
.expect("child succeeds");
assert_eq!(value.len(), 300 * 1024);
let succeed = named_update(&client.recorded_updates(), "big", &OperationAction::Succeed);
assert_eq!(has_replay_children(&succeed), Some(true));
assert_eq!(succeed.payload(), Some(""));
}
/// A serdes that encodes a `String` as JSON and appends `!` to every
/// decoded value, counting its decodes in `decodes`.
struct SuffixOnDecode {
decodes: Arc<std::sync::atomic::AtomicUsize>,
}
impl Serdes<String> for SuffixOnDecode {
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn serialize(
&self,
value: String,
_context: SerdesContext,
) -> Result<String, crate::BoxError> {
Ok(serde_json::to_string(&value)?)
}
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn deserialize(
&self,
wire: String,
_context: SerdesContext,
) -> Result<String, crate::BoxError> {
self.decodes
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
let value: String = serde_json::from_str(&wire)?;
Ok(format!("{value}!"))
}
}
/// An oversized child decodes its serialized result once, passes the
/// decoded value to the summary, and returns that same value.
#[tokio::test]
async fn oversized_child_summary_sees_decoded_value_with_one_decode() {
let (ctx, client) = live_ctx();
let decodes = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let value: String = ctx
.run_in_child_context(|_child| async { Ok("x".repeat(300 * 1024)) })
.name("big")
.serdes(SuffixOnDecode {
decodes: Arc::clone(&decodes),
})
.summary(|s: &String| format!("len={} last={:?}", s.len(), s.chars().last()))
.await
.expect("child succeeds");
assert_eq!(value.len(), 300 * 1024 + 1);
assert!(value.ends_with("x!"), "the caller gets the decoded value");
let succeed = named_update(&client.recorded_updates(), "big", &OperationAction::Succeed);
assert_eq!(has_replay_children(&succeed), Some(true));
assert_eq!(succeed.payload(), Some("len=307201 last=Some('!')"));
assert_eq!(decodes.load(std::sync::atomic::Ordering::SeqCst), 1);
}
/// A serdes that encodes JSON and fails every decode, with a marked
/// error when `marked` is set.
struct DecodeFailure {
marked: bool,
}
impl<T: serde::Serialize + Send + 'static> Serdes<T> for DecodeFailure {
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn serialize(
&self,
value: T,
_context: SerdesContext,
) -> Result<String, crate::BoxError> {
Ok(serde_json::to_string(&value)?)
}
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn deserialize(
&self,
_wire: String,
_context: SerdesContext,
) -> Result<T, crate::BoxError> {
if self.marked {
Err(crate::serdes::RetryableSerdesError::new("injected decode failure").into())
} else {
Err("injected decode failure".into())
}
}
}
/// An oversized result whose decode fails records the success with the
/// payload `""`, without calling the summary, and then returns the
/// decode error (a) or ends the invocation for a marked error (b).
#[tokio::test]
async fn oversized_child_decode_failure_records_success_then_routes() {
let child =
|ctx: &DurableContext, marked: bool, calls: &Arc<std::sync::atomic::AtomicUsize>| {
let calls = Arc::clone(calls);
ctx.run_in_child_context(|_child| async { Ok("x".repeat(300 * 1024)) })
.name("big")
.serdes(DecodeFailure { marked })
.summary(move |s: &String| {
calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
format!("len={}", s.len())
})
};
let assert_recorded = |client: &crate::client::InMemoryExecutionClient| {
let succeed =
named_update(&client.recorded_updates(), "big", &OperationAction::Succeed);
assert_eq!(has_replay_children(&succeed), Some(true));
assert_eq!(succeed.payload(), Some(""));
};
// (a) An unmarked error is the deserialize helper's error.
let (ctx, client) = live_ctx();
let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let err = child(&ctx, false, &calls)
.await
.expect_err("the decode fails");
assert!(
matches!(
err.kind(),
OperationErrorKind::ChildContext(c) if matches!(c.kind(), ChildContextErrorKind::Internal)
),
"{err:?}"
);
let chain = crate::error::chain_string(&err);
assert!(
chain.contains("deserialize result: injected decode failure"),
"{chain}"
);
assert_recorded(&client);
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 0);
// (b) A marked error ends the invocation, with the success recorded.
let (ctx, client) = live_ctx();
let calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
let signal = Arc::clone(ctx.coordinator());
crate::driver::test_support::expect_serdes_fault(
ctx.clone(),
Arc::clone(ctx.task_ownership()),
signal,
child(&ctx, true, &calls),
"serdes deserialize failed for operation \"big\"",
)
.await;
assert_recorded(&client);
assert_eq!(calls.load(std::sync::atomic::Ordering::SeqCst), 0);
}
/// A serdes whose `serialize` encodes JSON and whose `deserialize`
/// fails, with a [`RetryableSerdesError`](crate::serdes::RetryableSerdesError)
/// when `marked`.
struct DeserializeFailure {
marked: bool,
}
impl<T: serde::Serialize + Send + 'static> Serdes<T> for DeserializeFailure {
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn serialize(
&self,
value: T,
_context: SerdesContext,
) -> Result<String, crate::BoxError> {
Ok(serde_json::to_string(&value)?)
}
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn deserialize(
&self,
_wire: String,
_context: SerdesContext,
) -> Result<T, crate::BoxError> {
if self.marked {
Err(crate::serdes::RetryableSerdesError::new("injected decode failure").into())
} else {
Err("cannot decode".into())
}
}
}
/// The error a typed serdes fails with: the type `PaymentError`, the
/// message `declined` and the data `d`.
fn typed_declined() -> crate::BoxError {
Box::new(crate::TypedError::with_type("PaymentError", "declined").with_error_data("d"))
}
/// A JSON serdes whose `serialize` (when `on_serialize`) or
/// `deserialize` (otherwise) fails, unmarked, with [`typed_declined`].
struct TypedSerdesFailure {
on_serialize: bool,
}
impl<T> Serdes<T> for TypedSerdesFailure
where
T: serde::Serialize + serde::de::DeserializeOwned + Send + 'static,
{
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn serialize(
&self,
value: T,
_context: SerdesContext,
) -> Result<String, crate::BoxError> {
if self.on_serialize {
Err(typed_declined())
} else {
Ok(serde_json::to_string(&value)?)
}
}
// reason: exercises the async-fn impl form user code writes
#[expect(clippy::unused_async_trait_impl)]
async fn deserialize(
&self,
wire: String,
_context: SerdesContext,
) -> Result<T, crate::BoxError> {
if self.on_serialize {
Ok(serde_json::from_str(&wire)?)
} else {
Err(typed_declined())
}
}
}
/// The text of the source of a child context error.
fn child_source_text(err: &OperationError) -> Option<String> {
let OperationErrorKind::ChildContext(child) = err.kind() else {
unreachable!("expected a child context error, got {:?}", err.kind());
};
std::error::Error::source(child).map(ToString::to_string)
}
/// The recorded message of a typed serialize failure of a child result.
const TYPED_SERIALIZE_MESSAGE: &str =
"operation error: child_context: internal error: serialize result: declined";
/// A typed permanent serialize failure of a child result records the
/// wrapped message without the type name, under the serialization
/// type, with no data and no stack. Live and replay both give
/// `Internal` with that record.
#[tokio::test]
async fn typed_serialize_failure_records_the_wrapped_message() {
let expected = crate::error::WireError::new(
Some(crate::error::SERIALIZATION_FAILED_ERROR_TYPE),
Some(TYPED_SERIALIZE_MESSAGE),
);
let (ctx, client) = live_ctx();
let live = ctx
.run_in_child_context(|_child| async { Ok(3_u32) })
.serdes(TypedSerdesFailure { on_serialize: true })
.await
.expect_err("serialize fails");
assert!(is_child_internal(&live), "{live:?}");
assert_eq!(
child_source_text(&live).as_deref(),
Some("serialize result: declined")
);
assert_eq!(live.wire(), Some(&expected));
let updates = client.recorded_updates();
let error = updates
.iter()
.find(|u| u.action() == &OperationAction::Fail)
.and_then(OperationUpdate::error)
.expect("the child records a FAIL with an error");
assert_eq!(
error.error_type(),
Some(crate::error::SERIALIZATION_FAILED_ERROR_TYPE)
);
assert_eq!(error.error_message(), Some(TYPED_SERIALIZE_MESSAGE));
assert_eq!(error.error_data(), None);
assert!(error.stack_trace().is_empty(), "{error:?}");
let ctx = test_ctx(CheckpointLog::from_records(vec![failed_record(
"1",
crate::error::SERIALIZATION_FAILED_ERROR_TYPE,
TYPED_SERIALIZE_MESSAGE,
)]));
let replayed = ctx
.run_in_child_context(|_child| async { Ok(3_u32) })
.serdes(TypedSerdesFailure { on_serialize: true })
.await
.expect_err("the recorded failure replays");
assert!(is_child_internal(&replayed), "{replayed:?}");
assert_eq!(replayed.wire(), Some(&expected));
}
/// A typed permanent deserialize failure of a child result gives an
/// `Internal` error whose recorded message omits the type name, with
/// the child's type and no data or stack, live and on replay of the
/// recorded success.
#[tokio::test]
async fn typed_deserialize_failure_records_the_wrapped_message() {
let expected = crate::error::WireError::new(
Some("ChildContextError"),
Some("operation error: child_context: internal error: deserialize result: declined"),
);
let (ctx, client) = live_ctx();
let live = ctx
.run_in_child_context(|_child| async { Ok(3_u32) })
.serdes(TypedSerdesFailure {
on_serialize: false,
})
.await
.expect_err("deserialize fails");
assert!(is_child_internal(&live), "{live:?}");
assert_eq!(live.wire(), None);
assert_eq!(
child_source_text(&live).as_deref(),
Some("deserialize result: declined")
);
assert_eq!(crate::error::wire_error_for(&live, "Error"), expected);
assert!(
client
.recorded_updates()
.iter()
.any(|u| u.action() == &OperationAction::Succeed),
"the child records its success"
);
let ctx = test_ctx(CheckpointLog::from_records(vec![succeeded_record(
"1", "3",
)]));
let replayed = ctx
.run_in_child_context(|_child| async { Ok(3_u32) })
.serdes(TypedSerdesFailure {
on_serialize: false,
})
.await
.expect_err("the recorded success fails to decode");
assert!(is_child_internal(&replayed), "{replayed:?}");
assert_eq!(replayed.wire(), None);
assert_eq!(
child_source_text(&replayed).as_deref(),
Some("deserialize result: declined")
);
assert_eq!(crate::error::wire_error_for(&replayed, "Error"), expected);
}
/// Whether `err` is a child context error of kind `Internal`.
fn is_child_internal(err: &OperationError) -> bool {
matches!(
err.kind(),
OperationErrorKind::ChildContext(child)
if matches!(child.kind(), ChildContextErrorKind::Internal)
)
}
/// An unmarked serialize failure of a virtual child returns the serialize
/// helper's error, writes nothing, and ends the child with `Failed`.
#[tokio::test]
async fn virtual_child_unmarked_serialize_failure() {
let hooks = crate::plugin::recorder::UfHooks::default();
let (ctx, client) = uf_ctx(&hooks, Vec::new());
let err = ctx
.run_in_child_context(|_child| async { Ok(3_u32) })
.name("v")
.virtual_context()
.serdes(PlainSerializeFailure)
.await
.expect_err("serialize fails");
assert!(is_child_internal(&err), "{err:?}");
let message = format!("{err:#}");
assert!(message.contains("serialize result: "), "{message}");
assert!(message.contains("cannot encode"), "{message}");
assert_eq!(client.recorded_updates(), [] as [OperationUpdate; 0]);
let end = hooks.find("v", "end");
assert_eq!(end.status, Some(crate::OperationStatus::Failed));
assert!(end.is_virtual);
}
/// A marked serialize failure of a virtual child ends the invocation
/// and fires no end hook.
#[tokio::test]
async fn virtual_child_marked_serialize_failure_ends_invocation() {
let hooks = crate::plugin::recorder::UfHooks::default();
let (ctx, client) = uf_ctx(&hooks, Vec::new());
let child = ctx
.run_in_child_context(|_child| async { Ok(3_u32) })
.name("v")
.virtual_context()
.serdes(MarkedFailure);
crate::driver::test_support::expect_serdes_fault(
ctx.clone(),
Arc::clone(ctx.task_ownership()),
Arc::clone(ctx.coordinator()),
child,
"serdes serialize failed for operation \"v\"",
)
.await;
assert!(hooks.hooks_of("v").contains(&"start"));
assert!(!hooks.hooks_of("v").contains(&"end"));
assert_eq!(client.recorded_updates(), [] as [OperationUpdate; 0]);
}
/// An unmarked deserialize failure of a virtual child returns the
/// deserialize helper's error after the `Succeeded` end hook; a marked
/// one ends the invocation.
#[tokio::test]
async fn virtual_child_deserialize_failures() {
let hooks = crate::plugin::recorder::UfHooks::default();
let (ctx, client) = uf_ctx(&hooks, Vec::new());
let err = ctx
.run_in_child_context(|_child| async { Ok(3_u32) })
.name("v")
.virtual_context()
.serdes(DeserializeFailure { marked: false })
.await
.expect_err("deserialize fails");
assert!(is_child_internal(&err), "{err:?}");
let message = format!("{err:#}");
assert!(message.contains("deserialize result: "), "{message}");
assert!(message.contains("cannot decode"), "{message}");
assert_eq!(
hooks.find("v", "end").status,
Some(crate::OperationStatus::Succeeded)
);
assert_eq!(client.recorded_updates(), [] as [OperationUpdate; 0]);
let (ctx, _client) = uf_ctx(&crate::plugin::recorder::UfHooks::default(), Vec::new());
let child = ctx
.run_in_child_context(|_child| async { Ok(3_u32) })
.name("v")
.virtual_context()
.serdes(DeserializeFailure { marked: true });
crate::driver::test_support::expect_serdes_fault(
ctx.clone(),
Arc::clone(ctx.task_ownership()),
Arc::clone(ctx.coordinator()),
child,
"serdes deserialize failed for operation \"v\"",
)
.await;
}
/// A virtual child is not abandoned when its body returns: a step the
/// body spawned and did not await keeps running and records its
/// outcome, and the virtual child records nothing.
#[tokio::test]
async fn virtual_child_is_not_abandoned() {
let (ctx, client) = live_ctx();
let slot: Arc<std::sync::Mutex<Option<crate::DurableFuture<u32>>>> = Arc::default();
let notify = Arc::new(tokio::sync::Notify::new());
let (body_slot, body_notify, body_client) =
(Arc::clone(&slot), Arc::clone(¬ify), Arc::clone(&client));
let value = ctx
.run_in_child_context(move |child| async move {
let handle = child
.step(move |_| async move {
body_notify.notified().await;
Ok(7_u32)
})
.name("s")
.spawn();
*body_slot.lock().expect("lock") = Some(handle);
let started = |client: &crate::client::InMemoryExecutionClient| {
client
.recorded_updates()
.iter()
.any(|u| u.name() == Some("s") && u.action() == &OperationAction::Start)
};
let mut yields = 0;
while !started(&body_client) {
assert!(yields < 1_000, "the step's START is recorded");
yields += 1;
tokio::task::yield_now().await;
}
Ok(1_u32)
})
.name("v")
.virtual_context()
.await
.expect("the child succeeds");
assert_eq!(value, 1);
notify.notify_one();
let handle = slot.lock().expect("lock").take().expect("the handle");
let step = tokio::time::timeout(Duration::from_secs(1), handle)
.await
.expect("the step settles within 1 s")
.expect("the step succeeds");
assert_eq!(step, 7);
let updates = client.recorded_updates();
let actions: Vec<_> = updates
.iter()
.filter(|u| u.name() == Some("s"))
.map(|u| u.action().clone())
.collect();
assert_eq!(actions, [OperationAction::Start, OperationAction::Succeed]);
assert!(updates.iter().all(|u| u.name() != Some("v")), "{updates:?}");
}
}