1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
//! Recorder: single-writer append path over `EventStore`.
use std::collections::HashMap;
use std::sync::Arc;
use aion_core::{
ActivityError, ActivityId, Event, EventEnvelope, PackageVersion, Payload, RunId,
ScheduleConfig, ScheduleId, SearchAttributeSchema, SearchAttributeValue, TimerCancelCause,
TimerId, WithTimeoutOutcome, WorkerAttribution, WorkflowError, WorkflowId,
};
use aion_store::visibility::VisibilityStore;
use crate::lifecycle::visibility::project_visibility;
use aion_store::{EventStore, OutboxRow, WriteToken};
use chrono::{DateTime, Utc};
use crate::durability::{DurabilityError, seq::SequenceHead};
/// Durable fan-out dispatch: the additive atomic events + outbox-rows append.
mod fan_out;
#[cfg(test)]
mod fan_out_tests;
/// Generation boundaries: one batch ends a run and opens its successor.
mod generation;
/// The run-scoped append guard: which generation may still append.
mod run_guard;
pub use fan_out::{FanOutCompletionResult, FanOutItem, FanOutOutcome};
pub use generation::{ContinuationTerminal, ContinuedGeneration, OpeningGeneration};
pub use run_guard::RunAdmission;
/// Identity fields recorded on a `WorkflowStarted` event.
#[derive(Clone, Debug)]
pub struct WorkflowStartRecord {
/// Logical workflow type started by the caller.
pub workflow_type: String,
/// Opaque workflow input payload.
pub input: Payload,
/// Concrete run identifier for this execution.
pub run_id: RunId,
/// Parent run that continued into this run, when applicable.
pub parent_run_id: Option<RunId>,
/// The parent WORKFLOW that spawned this run as a child (aion#77);
/// `None` for operator-started roots and continue-as-new successors.
pub parent_workflow_id: Option<WorkflowId>,
/// Package version this run was resolved against at record time.
pub package_version: PackageVersion,
}
/// Single append authority for one workflow history.
///
/// A recorder owns the workflow's tracked sequence head and computes every `expected_seq` from that
/// tracker. It never reads the store head after construction; a sequence conflict is surfaced as a
/// hard durability error because it indicates a second writer for the same workflow.
pub struct Recorder {
workflow_id: WorkflowId,
store: Arc<dyn EventStore>,
sequence: SequenceHead,
write_token: WriteToken,
visibility: Option<RecorderVisibility>,
run_id: Option<RunId>,
}
struct RecorderVisibility {
run_id: RunId,
store: Arc<dyn VisibilityStore>,
}
impl Recorder {
/// Creates a recorder for a fresh workflow history starting at sequence head `0`.
#[must_use]
pub fn new(workflow_id: WorkflowId, store: Arc<dyn EventStore>) -> Self {
Self::resume_at(workflow_id, store, 0)
}
/// Creates a recorder for a workflow whose existing history head was already derived.
#[must_use]
pub fn resume_at(workflow_id: WorkflowId, store: Arc<dyn EventStore>, head: u64) -> Self {
Self {
workflow_id,
store,
write_token: WriteToken::recorder(),
sequence: SequenceHead::from_head(head),
visibility: None,
run_id: None,
}
}
/// The sequence head this recorder has appended up to.
///
/// Exposed so a caller that appended through the loop's ONE recorder can
/// hand the resulting head to a successor recorder without a second
/// whole-history read — the read would answer a question this recorder
/// already knows the answer to, and on a workloop that read grows without
/// bound with the loop's age.
#[must_use]
pub const fn head(&self) -> u64 {
self.sequence.current()
}
/// Sets the concrete run context used for run-scoped outbox metadata.
#[must_use]
pub fn with_run_id(mut self, run_id: RunId) -> Self {
self.run_id = Some(run_id);
self
}
/// Enables visibility projection upserts after workflow-level state-changing events recorded
/// directly through this recorder.
#[must_use]
pub fn with_visibility(mut self, run_id: RunId, store: Arc<dyn VisibilityStore>) -> Self {
self.run_id = Some(run_id.clone());
self.visibility = Some(RecorderVisibility { run_id, store });
self
}
/// Point the visibility projection at `run_id` — the generation this
/// recorder now writes for, after a boundary started a successor in the
/// same history. A recorder without visibility has nothing to point.
pub(crate) fn retarget_visibility(&mut self, run_id: RunId) {
if let Some(visibility) = &mut self.visibility {
visibility.run_id = run_id;
}
}
/// Returns the workflow this recorder appends to.
#[must_use]
pub const fn workflow_id(&self) -> &WorkflowId {
&self.workflow_id
}
/// Returns the current tracked sequence head.
#[must_use]
pub const fn current_head(&self) -> u64 {
self.sequence.current()
}
/// Reconciles the tracked sequence head FORWARD to `durable_head` after an
/// acknowledgement-lost append is discovered to have landed (aion#145).
///
/// The incident shape: a store call returned an error AFTER its write
/// landed, so the append never reached `mark_append_success` and this
/// tracker sat below the durable head — every later append would mint a
/// stale sequence and die on `SequenceConflict`. Since AD-014 (aion#192)
/// the ONE append path resolves that outcome itself, under this lock,
/// before surfacing anything ([`Self::durable_append`]), so a recorder
/// should no longer be left stale by its own append. This method remains
/// for the timer bridge's already-recorded-fire branch — the second line,
/// reached when a fire is redelivered for an event that is already the
/// timer's last — which has proven UNDER THE RECORDER LOCK that the
/// active-segment history shows this recorder's own `TimerFired` as the
/// firing timer's last event (single-writer discipline: an event of that
/// exact shape can only be ours).
///
/// Forward-only, never backward: a `durable_head` at or below the tracked
/// head leaves the tracker untouched (the at-head case is the delivery
/// retry of a fully acknowledged fire, which needs no repair). This method
/// must NEVER be called in response to a `SequenceConflict` itself — a
/// genuine double-writer conflict must surface un-resynced (pinned by
/// `sequence_conflict_surfaces_without_advancing_or_retrying`); resyncing
/// there would convert the crate's double-writer alarm into silence.
pub(crate) fn reconcile_head_forward(&mut self, durable_head: u64) {
if durable_head > self.sequence.current() {
self.sequence = SequenceHead::from_head(durable_head);
}
}
/// Reads this workflow's recorded history without appending new events.
///
/// This narrow read seam lets replay contexts build a history cursor from the same store the
/// recorder writes to while preserving the recorder as the only event append authority.
///
/// # Errors
///
/// Returns [`DurabilityError::Store`] when the backing event store rejects the read.
pub async fn read_history(&self) -> Result<Vec<Event>, DurabilityError> {
self.store
.read_history(&self.workflow_id)
.await
.map_err(Into::into)
}
/// Records workflow start.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_workflow_started(
&mut self,
recorded_at: DateTime<Utc>,
start: WorkflowStartRecord,
) -> Result<(), DurabilityError> {
let WorkflowStartRecord {
workflow_type,
input,
run_id,
parent_run_id,
parent_workflow_id,
package_version,
} = start;
let recorded_run_id = run_id.clone();
self.append_with(recorded_at, |envelope| Event::WorkflowStarted {
envelope,
workflow_type,
input,
run_id,
parent_run_id,
parent_workflow_id,
package_version,
})
.await?;
self.run_id = Some(recorded_run_id);
Ok(())
}
/// Records workflow start together with its validated initial search
/// attributes in one atomic store append.
///
/// Both events land in a single batch so a crash can never leave a durable
/// `WorkflowStarted` without its initial attributes: recovery would
/// otherwise resurrect a workflow that is invisible to every visibility
/// query keyed on those attributes. Empty attribute maps record only the
/// start event.
///
/// # Errors
///
/// Returns [`DurabilityError`] when any attribute is unregistered or has a
/// type that does not match `schema`, when the event store rejects the
/// append, or when the sequence tracker cannot advance.
pub async fn record_workflow_started_with_attributes(
&mut self,
recorded_at: DateTime<Utc>,
start: WorkflowStartRecord,
attributes: HashMap<String, SearchAttributeValue>,
schema: &SearchAttributeSchema,
) -> Result<(), DurabilityError> {
if attributes.is_empty() {
return self.record_workflow_started(recorded_at, start).await;
}
for (name, value) in &attributes {
schema.validate(name, value)?;
}
let WorkflowStartRecord {
workflow_type,
input,
run_id,
parent_run_id,
parent_workflow_id,
package_version,
} = start;
let recorded_run_id = run_id.clone();
let started_envelope = self.next_envelope(recorded_at)?;
let attributes_envelope = self.envelope_after(&started_envelope, recorded_at)?;
let workflow_id = self.workflow_id.clone();
let batch = [
Event::WorkflowStarted {
envelope: started_envelope,
workflow_type,
input,
run_id,
parent_run_id,
parent_workflow_id,
package_version,
},
Event::SearchAttributesUpdated {
envelope: attributes_envelope,
workflow_id,
attributes,
},
];
self.durable_append(&batch).await?;
self.run_id = Some(recorded_run_id);
Ok(())
}
/// Records schedule creation in the schedule coordinator history.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_schedule_created(
&mut self,
recorded_at: DateTime<Utc>,
schedule_id: ScheduleId,
config: ScheduleConfig,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ScheduleCreated {
envelope,
schedule_id,
config,
})
.await
}
/// Records schedule configuration update in the schedule coordinator history.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_schedule_updated(
&mut self,
recorded_at: DateTime<Utc>,
schedule_id: ScheduleId,
config: ScheduleConfig,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ScheduleUpdated {
envelope,
schedule_id,
config,
})
.await
}
/// Records schedule pause in the schedule coordinator history.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_schedule_paused(
&mut self,
recorded_at: DateTime<Utc>,
schedule_id: ScheduleId,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::SchedulePaused {
envelope,
schedule_id,
})
.await
}
/// Records schedule resume in the schedule coordinator history.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_schedule_resumed(
&mut self,
recorded_at: DateTime<Utc>,
schedule_id: ScheduleId,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ScheduleResumed {
envelope,
schedule_id,
})
.await
}
/// Records schedule deletion in the schedule coordinator history.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_schedule_deleted(
&mut self,
recorded_at: DateTime<Utc>,
schedule_id: ScheduleId,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ScheduleDeleted {
envelope,
schedule_id,
})
.await
}
/// Records a schedule-triggered workflow execution in the schedule coordinator history.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_schedule_triggered(
&mut self,
recorded_at: DateTime<Utc>,
schedule_id: ScheduleId,
workflow_id: WorkflowId,
run_id: RunId,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ScheduleTriggered {
envelope,
schedule_id,
workflow_id,
run_id,
})
.await
}
/// Records workflow completion, then settles the workflow's live outbox
/// rows (#253).
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append. A settle failure never fails the
/// recorded terminal (see [`Self::settle_terminal_outbox_rows_nonfatal`]).
pub async fn record_workflow_completed(
&mut self,
recorded_at: DateTime<Utc>,
result: Payload,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::WorkflowCompleted {
envelope,
result,
})
.await?;
self.settle_terminal_outbox_rows_nonfatal().await;
Ok(())
}
/// Records terminal workflow failure, then settles the workflow's live
/// outbox rows (#253).
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append. A settle failure never fails the
/// recorded terminal (see [`Self::settle_terminal_outbox_rows_nonfatal`]).
pub async fn record_workflow_failed(
&mut self,
recorded_at: DateTime<Utc>,
error: WorkflowError,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::WorkflowFailed {
envelope,
error,
})
.await?;
self.settle_terminal_outbox_rows_nonfatal().await;
Ok(())
}
/// Records workflow cancellation, then settles the workflow's live outbox
/// rows (#253).
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append. A settle failure never fails the
/// recorded terminal (see [`Self::settle_terminal_outbox_rows_nonfatal`]).
pub async fn record_workflow_cancelled(
&mut self,
recorded_at: DateTime<Utc>,
reason: String,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::WorkflowCancelled {
envelope,
reason,
})
.await?;
self.settle_terminal_outbox_rows_nonfatal().await;
Ok(())
}
/// Records a workflow timeout terminal, then settles the workflow's live
/// outbox rows (#253).
///
/// The `timeout` descriptor is the closed-set kind token carried on
/// [`Event::WorkflowTimedOut`] (a declared workflow timeout uses
/// `"workflow"`); it is user-visible in `one_motion` output and replay
/// terminals. Mirrors [`Self::record_workflow_cancelled`]: a settle failure
/// never fails the recorded terminal.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the
/// sequence tracker cannot advance after a successful append.
pub async fn record_workflow_timed_out(
&mut self,
recorded_at: DateTime<Utc>,
timeout: String,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::WorkflowTimedOut {
envelope,
timeout,
})
.await?;
self.settle_terminal_outbox_rows_nonfatal().await;
Ok(())
}
/// Settles the workflow's live (`Pending`/`Claimed`) outbox rows to
/// `Cancelled` after a durably recorded workflow terminal (#253).
///
/// The Recorder is the single-writer choke point every workflow terminal
/// flows through, so this hook closes the live window in which a terminal
/// workflow's staged dispatches could still be claimed and redelivered (the
/// zombie-round incident). Deliberately NOT run for continue-as-new: the
/// workflow continues, and the old run's stragglers are already neutralized
/// by run-scoped completion threading (OBX-011).
///
/// A settle failure is loud but non-fatal: the terminal append already
/// committed (the workflow genuinely ended), and the boot sweep plus the
/// stale-claim reconciler gate repair any rows this settle missed.
async fn settle_terminal_outbox_rows_nonfatal(&self) {
match self
.store
.settle_workflow_outbox_rows_cancelled(&self.workflow_id)
.await
{
Ok(settled) if settled.is_empty() => {}
Ok(settled) => {
tracing::info!(
workflow_id = %self.workflow_id,
settled = settled.len(),
dispatch_keys = ?settled,
"settled outbox rows for terminal workflow"
);
}
Err(error) => {
tracing::error!(
workflow_id = %self.workflow_id,
%error,
"failed to settle outbox rows after recording a workflow terminal; \
the boot sweep or stale-claim reconciler will repair them"
);
}
}
}
/// Records workflow continue-as-new as a terminal event for this run.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_workflow_continued_as_new(
&mut self,
recorded_at: DateTime<Utc>,
input: Payload,
workflow_type: Option<String>,
parent_run_id: RunId,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::WorkflowContinuedAsNew {
envelope,
input,
workflow_type,
parent_run_id,
})
.await?;
self.upsert_visibility_projection_nonfatal().await;
Ok(())
}
/// Records a reopen of a failed run.
///
/// The reopen supersedes the run's prior terminal event in the status
/// projection (returning it to Running) and names the activities to
/// re-dispatch on replay. It refreshes the visibility projection so the
/// reopened workflow is listed as active again. This is the sole append
/// path for [`Event::WorkflowReopened`]; like every recorded event it lands
/// through the single-writer sequence discipline.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the
/// sequence tracker cannot advance after a successful append.
pub async fn record_workflow_reopened(
&mut self,
recorded_at: DateTime<Utc>,
run_id: RunId,
reopened: Vec<ActivityId>,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::WorkflowReopened {
envelope,
run_id,
reopened,
})
.await?;
self.upsert_visibility_projection_nonfatal().await;
Ok(())
}
/// Records an operator pause of a running run (#204).
///
/// A NON-terminal marker: it projects the run to `Paused` and refreshes the
/// visibility projection so `describe`/`list` show Paused. It is the sole
/// append path for [`Event::WorkflowPaused`] and, like every recorded event,
/// lands through the single-writer sequence discipline.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the
/// sequence tracker cannot advance after a successful append.
pub async fn record_workflow_paused(
&mut self,
recorded_at: DateTime<Utc>,
run_id: RunId,
reason: Option<String>,
operator: Option<String>,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::WorkflowPaused {
envelope,
run_id,
reason,
operator,
})
.await?;
self.upsert_visibility_projection_nonfatal().await;
Ok(())
}
/// Records an operator resume of a paused run (#204).
///
/// Supersedes the run's prior [`Event::WorkflowPaused`] under the status
/// projection (returning it to Running) and refreshes visibility. Sole append
/// path for [`Event::WorkflowResumed`].
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the
/// sequence tracker cannot advance after a successful append.
pub async fn record_workflow_resumed(
&mut self,
recorded_at: DateTime<Utc>,
run_id: RunId,
operator: Option<String>,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::WorkflowResumed {
envelope,
run_id,
operator,
})
.await?;
self.upsert_visibility_projection_nonfatal().await;
Ok(())
}
/// Records a validated search-attribute update for this workflow.
///
/// # Errors
///
/// Returns [`DurabilityError`] when any attribute is unregistered or has a type that does not
/// match `schema`, or when the event store rejects the append / sequence advance.
pub async fn record_search_attributes_updated(
&mut self,
recorded_at: DateTime<Utc>,
attributes: HashMap<String, SearchAttributeValue>,
schema: &SearchAttributeSchema,
) -> Result<(), DurabilityError> {
for (name, value) in &attributes {
schema.validate(name, value)?;
}
let workflow_id = self.workflow_id.clone();
self.append_with(recorded_at, |envelope| Event::SearchAttributesUpdated {
envelope,
workflow_id,
attributes,
})
.await?;
self.upsert_visibility_projection_nonfatal().await;
Ok(())
}
/// Records activity scheduling, stamping the `task_queue` and OPTIONAL `node` affinity the
/// activity dispatches to so reopen/recovery re-targets the **same** pool and node (NSTQ-3 /
/// NODE-3).
///
/// No SDK-level task-queue or node selection exists yet (NSTQ-4 / NODE-4), so the
/// single-schedule engine seam passes the named `"default"` task queue and `None` node — the
/// genuine current values, not a shim.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_activity_scheduled(
&mut self,
recorded_at: DateTime<Utc>,
activity_id: ActivityId,
activity_type: String,
input: Payload,
task_queue: String,
node: Option<String>,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ActivityScheduled {
envelope,
activity_id,
activity_type,
input,
task_queue,
node,
})
.await
}
/// Records that the engine DISPATCHED an activity attempt to its task queue.
///
/// Not "a worker started it": every caller invokes this BEFORE the dispatch
/// reaches the dispatcher, so no worker has been selected when the append
/// happens. See [`Event::ActivityStarted`] for the full account and for the
/// pending rename.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_activity_started(
&mut self,
recorded_at: DateTime<Utc>,
activity_id: ActivityId,
attempt: u32,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ActivityStarted {
envelope,
activity_id,
// NOI-0: the genuine one-based attempt this start belongs to, threaded from the dispatch
// seam so `ActivityStarted` and its terminal share one `(workflow, activity, attempt)`.
attempt,
})
.await
}
/// Records that recovery retained a dangling activity attempt and offered
/// that same execution identity to the worker adoption path.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the
/// sequence tracker cannot advance after a successful append.
pub async fn record_activity_adoption_offered(
&mut self,
recorded_at: DateTime<Utc>,
activity_id: ActivityId,
attempt: u32,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ActivityAdoptionOffered {
envelope,
activity_id,
attempt,
})
.await
}
/// Records successful activity completion.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_activity_completed(
&mut self,
recorded_at: DateTime<Utc>,
activity_id: ActivityId,
result: Payload,
attempt: u32,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ActivityCompleted {
envelope,
activity_id,
result,
// NOI-0: the genuine one-based attempt that produced this completion, consistent with the
// `ActivityStarted` of the same attempt.
attempt,
})
.await
}
/// Records failed activity attempt.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_activity_failed(
&mut self,
recorded_at: DateTime<Utc>,
activity_id: ActivityId,
error: ActivityError,
attempt: u32,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ActivityFailed {
envelope,
activity_id,
error,
attempt,
})
.await
}
/// Records the R5 warning that an ADVISORY activity spent its whole
/// attempt budget and failed for good.
///
/// This ACCOMPANIES the activity's honest terminal `ActivityFailed`; it
/// never replaces it. The advisory class suppresses the FAULT, never the
/// RECORD — the side channel really did fail, and history says so twice:
/// once as the activity's own failure, once as the visible warning on the
/// run (RUNTIME-OPERATIONS.md R5).
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the
/// sequence tracker cannot advance after a successful append.
pub async fn record_activity_advisory_exhausted(
&mut self,
recorded_at: DateTime<Utc>,
activity_id: ActivityId,
activity_type: String,
reason: String,
attempt: u32,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ActivityAdvisoryExhausted {
envelope,
activity_id,
activity_type,
reason,
attempt,
})
.await
}
/// Records a durable post-refusal fallback queue hop.
///
/// Retry routing calls this only through the workflow recorder held by
/// `RetryRecorderSeam`; no routing component appends this event directly.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the
/// sequence tracker cannot advance after a successful append.
pub async fn record_activity_fallback_routed(
&mut self,
recorded_at: DateTime<Utc>,
activity_id: ActivityId,
attempt: u32,
from_task_queue: String,
to_task_queue: String,
fallback_index: u32,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ActivityFallbackRouted {
envelope,
activity_id,
attempt,
from_task_queue,
to_task_queue,
fallback_index,
})
.await
}
/// Records activity cancellation.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_activity_cancelled(
&mut self,
recorded_at: DateTime<Utc>,
activity_id: ActivityId,
attempt: u32,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ActivityCancelled {
envelope,
activity_id,
// NOI-0: the genuine one-based attempt that was cancelled, consistent with the
// `ActivityStarted` of the same attempt.
attempt,
})
.await
}
/// Records activity cancellation for a fan-out ordinal and settles its outbox row.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event append fails, sequence tracking cannot advance, or
/// the outbox-aware store rejects the cancellation settlement.
pub async fn record_activity_cancelled_and_settle_outbox(
&mut self,
recorded_at: DateTime<Utc>,
ordinal: u64,
attempt: u32,
) -> Result<(), DurabilityError> {
self.record_activity_cancelled(
recorded_at,
ActivityId::from_sequence_position(ordinal),
attempt,
)
.await?;
let dispatch_key = OutboxRow::dispatch_key_for(&self.workflow_id, ordinal);
self.store
.settle_outbox_row_cancelled(&dispatch_key)
.await
.map_err(Into::into)
}
/// Records timer scheduling, returning the appended `TimerStarted`'s
/// workflow-history sequence — the arming's identity, which the caller
/// passes to `schedule_timer` so the durable row names exactly this
/// arming (`TimerEntry::armed_seq`).
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_timer_started(
&mut self,
recorded_at: DateTime<Utc>,
timer_id: TimerId,
fire_at: DateTime<Utc>,
) -> Result<u64, DurabilityError> {
let envelope = self.next_envelope(recorded_at)?;
let armed_seq = envelope.seq;
self.append_one(Event::TimerStarted {
envelope,
timer_id,
fire_at,
})
.await?;
Ok(armed_seq)
}
/// Records timer firing.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_timer_fired(
&mut self,
recorded_at: DateTime<Utc>,
timer_id: TimerId,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::TimerFired {
envelope,
timer_id,
})
.await
}
/// Records timer cancellation.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_timer_cancelled(
&mut self,
recorded_at: DateTime<Utc>,
timer_id: TimerId,
cause: TimerCancelCause,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::TimerCancelled {
envelope,
timer_id,
cause,
})
.await
}
/// Records a `with_timeout` terminal outcome.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_with_timeout_completed(
&mut self,
recorded_at: DateTime<Utc>,
timer_id: TimerId,
outcome: WithTimeoutOutcome,
result: Option<Payload>,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::WithTimeoutCompleted {
envelope,
timer_id,
outcome,
result,
})
.await
}
/// Records that a selected worker accepted one activity attempt (WA-010).
///
/// An asynchronous arrival like a signal: the server's handoff seam holds
/// no Recorder of its own, so the lease reaches this one through
/// `Engine::record_activity_lease` and serialises with every other append
/// under the same lock. `worker` is durable names only — see
/// [`WorkerAttribution`].
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_activity_leased(
&mut self,
recorded_at: DateTime<Utc>,
activity_id: ActivityId,
attempt: u32,
worker: WorkerAttribution,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ActivityLeased {
envelope,
activity_id,
attempt,
worker,
})
.await
}
/// Records signal delivery.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_signal_received(
&mut self,
recorded_at: DateTime<Utc>,
name: String,
payload: Payload,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::SignalReceived {
envelope,
name,
payload,
})
.await
}
/// Records a signal sent by this workflow.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_signal_sent(
&mut self,
recorded_at: DateTime<Utc>,
target_workflow_id: WorkflowId,
name: String,
payload: Payload,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::SignalSent {
envelope,
target_workflow_id,
name,
payload,
})
.await
}
/// Records child workflow start.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_child_workflow_started(
&mut self,
recorded_at: DateTime<Utc>,
child_workflow_id: WorkflowId,
workflow_type: String,
input: Payload,
package_version: PackageVersion,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ChildWorkflowStarted {
envelope,
child_workflow_id,
workflow_type,
input,
package_version,
})
.await
}
/// Records child workflow completion.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_child_workflow_completed(
&mut self,
recorded_at: DateTime<Utc>,
child_workflow_id: WorkflowId,
result: Payload,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ChildWorkflowCompleted {
envelope,
child_workflow_id,
result,
})
.await
}
/// Records child workflow failure.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_child_workflow_failed(
&mut self,
recorded_at: DateTime<Utc>,
child_workflow_id: WorkflowId,
error: WorkflowError,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::ChildWorkflowFailed {
envelope,
child_workflow_id,
error,
})
.await
}
/// Records an engine-side cadence fire for a workloop (workloop brief
/// R1.4/R4.3): the dead-man clock tick, appended through this one
/// Recorder exactly like any other asynchronous arrival.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_cadence_fired(
&mut self,
recorded_at: DateTime<Utc>,
window_seq: u64,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::CadenceFired {
envelope,
window_seq,
})
.await
}
/// Records a workloop iteration close (R3.1/R3.3): the iteration's routes
/// and the health samples they land against the loop's invariants.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_iteration_closed(
&mut self,
recorded_at: DateTime<Utc>,
routes: Vec<String>,
health_samples: Vec<aion_core::HealthSample>,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::IterationClosed {
envelope,
routes,
health_samples,
})
.await
}
/// Records a workloop retirement (R2.5) ATOMICALLY with its terminal: one
/// batch appends `LoopRetired { reason }` followed by
/// `WorkflowCompleted { result }`, so a crash can never leave a retirement
/// marker without its terminal (an intentional stop reading as a hang) or
/// a completed loop without its declared reason (an incident reading where
/// a decommission happened). Terminal outbox rows are settled afterwards,
/// exactly as [`Self::record_workflow_completed`] settles them.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append. A settle failure never fails the
/// recorded terminal (see [`Self::settle_terminal_outbox_rows_nonfatal`]).
pub async fn record_loop_retired(
&mut self,
recorded_at: DateTime<Utc>,
reason: String,
result: Payload,
) -> Result<(), DurabilityError> {
let retired_envelope = self.next_envelope(recorded_at)?;
let completed_envelope = self.envelope_after(&retired_envelope, recorded_at)?;
let batch = [
Event::LoopRetired {
envelope: retired_envelope,
reason,
},
Event::WorkflowCompleted {
envelope: completed_envelope,
result,
},
];
self.durable_append(&batch).await?;
// The retirement is a genuine terminal on the loop's current
// generation: its one row must read Completed with its end the moment
// the append is durable, exactly as every other terminal recorder
// projects — a retired loop that still listed as Running was a row
// only the next boot reconcile righted.
self.upsert_visibility_projection_nonfatal().await;
self.settle_terminal_outbox_rows_nonfatal().await;
Ok(())
}
/// Records a detached top-level hatch (R13.1) in the HATCHING workflow's
/// history — record-then-start, so replay re-mints the same deterministic
/// `child_workflow_id` and a crash between record and start leaves a
/// repairable record instead of an unrecorded orphan.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_workflow_hatched(
&mut self,
recorded_at: DateTime<Utc>,
child_workflow_id: WorkflowId,
key: String,
) -> Result<(), DurabilityError> {
self.append_with(recorded_at, |envelope| Event::WorkflowHatched {
envelope,
child_workflow_id,
key,
})
.await
}
/// Records the ONE alarm path (R4.2): the invariant is not confirmed
/// held, with cause as a field — never a separate alarm channel.
///
/// # Errors
///
/// Returns [`DurabilityError`] if the event store rejects the append or the sequence
/// tracker cannot advance after a successful append.
pub async fn record_invariant_unconfirmed(
&mut self,
recorded_at: DateTime<Utc>,
alarm: aion_core::InvariantAlarm,
) -> Result<(), DurabilityError> {
let aion_core::InvariantAlarm {
invariant,
cause,
window_seq,
last_confirmed_at,
consecutive_unconfirmed,
} = alarm;
self.append_with(recorded_at, |envelope| Event::InvariantUnconfirmed {
envelope,
invariant,
cause,
window_seq,
last_confirmed_at,
consecutive_unconfirmed,
})
.await
}
fn next_envelope(&self, recorded_at: DateTime<Utc>) -> Result<EventEnvelope, DurabilityError> {
let seq = self
.sequence
.next_seq()
.ok_or_else(|| DurabilityError::HistoryShape {
reason: format!(
"sequence head overflow advancing {} by 1",
self.sequence.current()
),
})?;
Ok(EventEnvelope {
seq,
recorded_at,
workflow_id: self.workflow_id.clone(),
})
}
fn envelope_after(
&self,
previous: &EventEnvelope,
recorded_at: DateTime<Utc>,
) -> Result<EventEnvelope, DurabilityError> {
let seq = previous
.seq
.checked_add(1)
.ok_or_else(|| DurabilityError::HistoryShape {
reason: format!("sequence head overflow advancing {} by 1", previous.seq),
})?;
Ok(EventEnvelope {
seq,
recorded_at,
workflow_id: self.workflow_id.clone(),
})
}
async fn append_with(
&mut self,
recorded_at: DateTime<Utc>,
build_event: impl FnOnce(EventEnvelope) -> Event,
) -> Result<(), DurabilityError> {
let envelope = self.next_envelope(recorded_at)?;
self.append_one(build_event(envelope)).await
}
async fn append_one(&mut self, event: Event) -> Result<(), DurabilityError> {
self.durable_append(std::slice::from_ref(&event)).await?;
if !reprojects_whole_row(&event) {
self.touch_visibility_nonfatal(&event).await;
}
Ok(())
}
/// THE one append: every event this recorder writes goes through here.
///
/// A store error other than [`StoreError::SequenceConflict`] is
/// INDETERMINATE — the write may have landed before the error came back
/// (aion#192: a shard-actor timeout on a `TimerStarted` that had already
/// been applied left the tracked head one below the durable head, the
/// workflow process died on the surfaced error, and every later append —
/// cancel included — conflicted until a restart rebuilt this recorder from
/// the store). So the outcome is resolved against the store BEFORE
/// anything is surfaced, by [`Self::resolve_indeterminate_append`]. A
/// `SequenceConflict` from the store itself is surfaced as-is, with no
/// read and no reconciliation: that is the double-writer alarm, and
/// resyncing on it would silence the alarm (aion#145's other side).
///
/// # Errors
///
/// Returns the store's refusal when the append demonstrably did not land
/// (a fenced [`StoreError::NotOwner`] is surfaced typed, with no read),
/// [`StoreError::SequenceConflict`] when something ELSE sits at the
/// expected sequence, and [`DurabilityError::HistoryShape`] if the tracker
/// cannot advance.
async fn durable_append(&mut self, batch: &[Event]) -> Result<(), DurabilityError> {
let expected_seq = self.sequence.current();
match self
.store
.append(self.write_token, &self.workflow_id, batch, expected_seq)
.await
{
Ok(()) => {}
Err(conflict @ aion_store::StoreError::SequenceConflict { .. }) => {
return Err(DurabilityError::Store(conflict));
}
// A fenced quorum write is a DEFINITE did-not-land — the store
// says this node is not the owner, and the typed variant is a
// retryable routing signal the completion-retry and fence paths
// match on. It never reaches the resolving read: that read from a
// just-fenced node is the one most likely to fail (and would fold
// the variant into `Backend`), and a stale replica could answer
// with a rogue and mint a spurious `SequenceConflict` out of a
// routing condition.
Err(fenced @ aion_store::StoreError::NotOwner { .. }) => {
return Err(DurabilityError::Store(fenced));
}
Err(error) => {
self.resolve_indeterminate_append(batch, expected_seq, error)
.await?;
}
}
self.sequence.mark_append_success(batch.len())
}
/// Decide what an errored append actually did, by reading the store.
///
/// Reads everything at or past `expected_seq + 1` (a range read, not the
/// whole history) and rules:
///
/// - nothing there → the append did not land; the ORIGINAL error is
/// returned unchanged and the tracked head is untouched, so the caller's
/// own retry path proceeds exactly as before;
/// - exactly this batch there (same envelopes, same payloads — `Event` is
/// compared by value, and under single-writer discipline nothing else
/// can produce these events at these sequences) → the append LANDED and
/// its acknowledgement was lost; said at WARN so it can be correlated
/// with the store's own timeout narration, and reported as success so
/// the tracker advances by the batch;
/// - anything else there → a genuine [`StoreError::SequenceConflict`],
/// surfaced un-resynced.
///
/// If the resolving read itself fails, the ORIGINAL append error is
/// returned with the read failure attached in its message; the head stays
/// untouched, because nothing was learned.
///
/// Held under the recorder's lock by construction (`&mut self`): no other
/// append of this workflow can interleave with the read.
async fn resolve_indeterminate_append(
&self,
batch: &[Event],
expected_seq: u64,
error: aion_store::StoreError,
) -> Result<(), DurabilityError> {
let Some(from_seq) = expected_seq.checked_add(1) else {
return Err(DurabilityError::HistoryShape {
reason: format!("sequence head overflow resolving an append at {expected_seq}"),
});
};
let landed = match self
.store
.read_history_from(&self.workflow_id, from_seq)
.await
{
Ok(landed) => landed,
Err(read_error) => {
return Err(DurabilityError::Store(aion_store::StoreError::Backend(
format!(
"{error}; the append's outcome could not be resolved either: {read_error}"
),
)));
}
};
if landed.is_empty() {
return Err(DurabilityError::Store(error));
}
let durable_head = landed.iter().map(Event::seq).max().unwrap_or(expected_seq);
if landed.len() == batch.len() && landed == batch {
tracing::warn!(
workflow_id = %self.workflow_id,
run_id = ?self.run_id,
landed_seq_from = from_seq,
landed_seq_to = durable_head,
store_error = %error,
"append landed but its acknowledgement was lost; resolved against the store and \
counted as recorded"
);
return Ok(());
}
Err(DurabilityError::Store(
aion_store::StoreError::SequenceConflict {
expected: expected_seq,
found: durable_head,
},
))
}
/// Projects and upserts THE workflow's row — always for the history's
/// CURRENT generation, never for the generation this recorder happens to
/// write for. The two coincide for a live recorder; they differ only for
/// a straggler (a lease or heartbeat event landing through a superseded
/// generation's recorder after the boundary replaced its row), and a
/// write may heal a row forward, never regress it to an older run.
async fn upsert_visibility_projection(&self) -> Result<(), DurabilityError> {
let Some(visibility) = &self.visibility else {
return Ok(());
};
let history = self.store.read_history(&self.workflow_id).await?;
let current = crate::lifecycle::visibility::current_run_id(&history)
.unwrap_or_else(|| visibility.run_id.clone());
let record = project_visibility(&history, ¤t).ok_or_else(|| {
DurabilityError::HistoryShape {
reason: String::from(
"workflow history has no WorkflowStarted event for visibility projection",
),
}
})?;
visibility.store.record_visibility(record).await?;
Ok(())
}
async fn upsert_visibility_projection_nonfatal(&self) {
if let Err(error) = self.upsert_visibility_projection().await {
self.warn_visibility_failure(&error, "visibility upsert failed after durable append");
}
}
/// Advances the row's `updated_at` to the event's instant after an append
/// that re-projects nothing else, and applies the event to the row's
/// outstanding leases through the SAME transition the whole-history fold
/// uses ([`aion_core::apply_lease_transition`]) — so a lease or an
/// attempt terminal moves `current_worker` on the row exactly as it would
/// move a summary built from history (WA-010 R4). A run whose row is
/// missing (its start-time upsert failed and reconciliation has not yet
/// repaired it) gets the whole row projected instead, so the touch heals
/// the gap it would otherwise widen.
async fn touch_visibility(&self, event: &Event) -> Result<(), DurabilityError> {
let Some(visibility) = &self.visibility else {
return Ok(());
};
match visibility.store.get_visibility(&self.workflow_id).await? {
// A row still carrying another generation's run is stale (the
// boundary's replace has not landed yet): touching it would
// advance the wrong generation, so project the whole row for the
// generation this recorder writes for instead.
Some(mut record) if record.run_id == visibility.run_id => {
record.updated_at = *event.recorded_at();
// The stamp is INDUCTIVE: a touch may move the row to this
// event only if the row provably folded everything before it
// (it stood at the previous seq). A projection that failed
// (tolerated, warned) leaves a gap the touch must never paper
// over — a row stamped past an unfolded lifecycle event would
// be trusted by the next boot with a status that is a lie.
// Left behind the head, the row is re-derived instead.
if record.head_seq != 0 && event.seq() == record.head_seq.saturating_add(1) {
record.head_seq = event.seq();
}
aion_core::apply_lease_transition(&mut record.outstanding_leases, event);
visibility.store.record_visibility(record).await?;
Ok(())
}
Some(_) | None => self.upsert_visibility_projection().await,
}
}
async fn touch_visibility_nonfatal(&self, event: &Event) {
if let Err(error) = self.touch_visibility(event).await {
self.warn_visibility_failure(&error, "visibility touch failed after durable append");
}
}
fn warn_visibility_failure(&self, error: &DurabilityError, what: &'static str) {
let run_id = self
.visibility
.as_ref()
.map(|visibility| &visibility.run_id);
tracing::warn!(
workflow_id = %self.workflow_id,
run_id = run_id.map(ToString::to_string).as_deref().unwrap_or("unknown"),
error = %error,
"{what}; crash-consistency window remains until reconciliation repairs visibility"
);
}
}
/// Whether `event` is one whose recording path re-projects the whole
/// visibility row afterwards (the lifecycle handlers and this Recorder's own
/// lifecycle record functions do), so the per-append `updated_at` touch would
/// only be overwritten by it.
const fn reprojects_whole_row(event: &Event) -> bool {
matches!(
event,
Event::WorkflowStarted { .. }
| Event::WorkflowCompleted { .. }
| Event::WorkflowFailed { .. }
| Event::WorkflowCancelled { .. }
| Event::WorkflowTimedOut { .. }
| Event::WorkflowContinuedAsNew { .. }
| Event::WorkflowReopened { .. }
| Event::WorkflowPaused { .. }
| Event::WorkflowResumed { .. }
| Event::SearchAttributesUpdated { .. }
)
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::future::Future;
use std::sync::Arc;
use aion_core::RunId;
use aion_core::WorkflowListRequest;
use aion_core::{
Event, Payload, SearchAttributeError, SearchAttributeSchema, SearchAttributeType,
SearchAttributeValue, TimerId,
};
use aion_store::visibility::{VisibilityPage, VisibilityRecord, VisibilityStore};
use aion_store::{
InMemoryStore, ReadableEventStore, StoreError, WritableEventStore, WriteToken,
};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde_json::json;
use super::Recorder;
use crate::durability::DurabilityError;
#[derive(Debug)]
struct FailingVisibilityStore;
#[async_trait]
impl VisibilityStore for FailingVisibilityStore {
async fn record_visibility(&self, _record: VisibilityRecord) -> Result<(), StoreError> {
Err(StoreError::Backend(String::from("visibility unavailable")))
}
async fn get_visibility(
&self,
_workflow_id: &aion_core::WorkflowId,
) -> Result<Option<VisibilityRecord>, StoreError> {
Err(StoreError::Backend(String::from("visibility unavailable")))
}
async fn remove_visibility(
&self,
_workflow_id: &aion_core::WorkflowId,
_run_id: &RunId,
) -> Result<bool, StoreError> {
Err(StoreError::Backend(String::from("visibility unavailable")))
}
async fn list_workflows(
&self,
_request: &WorkflowListRequest,
) -> Result<VisibilityPage, StoreError> {
Err(StoreError::Backend(String::from("visibility unavailable")))
}
}
fn workflow_id(value: u128) -> aion_core::WorkflowId {
aion_core::WorkflowId::new(uuid::Uuid::from_u128(value))
}
fn recorded_at(offset_seconds: i64) -> DateTime<Utc> {
DateTime::from_timestamp(1_700_000_000 + offset_seconds, 0).unwrap_or_default()
}
fn payload(label: &str) -> Result<Payload, Box<dyn std::error::Error>> {
Ok(Payload::from_json(&json!({ "label": label }))?)
}
fn workflow_started(
seq: u64,
workflow_id: &aion_core::WorkflowId,
) -> Result<Event, Box<dyn std::error::Error>> {
Ok(Event::WorkflowStarted {
envelope: aion_core::EventEnvelope {
seq,
recorded_at: recorded_at(i64::try_from(seq)?),
workflow_id: workflow_id.clone(),
},
workflow_type: String::from("checkout"),
input: payload("workflow-input")?,
run_id: aion_core::RunId::new(uuid::Uuid::from_u128(1)),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
})
}
#[tokio::test]
async fn recorder_advances_expected_sequence_between_appends()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(1);
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: aion_core::RunId::new(uuid::Uuid::from_u128(1)),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
recorder
.record_workflow_completed(recorded_at(2), payload("result")?)
.await?;
let history = store.read_history(&workflow_id).await?;
assert_eq!(history[0].seq(), 1);
assert_eq!(history[1].seq(), 2);
assert_eq!(recorder.current_head(), 2);
Ok(())
}
#[tokio::test]
async fn records_workflow_reopened_and_projects_running()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(1);
let run_id = aion_core::RunId::new(uuid::Uuid::from_u128(1));
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: run_id.clone(),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
recorder
.record_workflow_failed(
recorded_at(2),
aion_core::WorkflowError {
message: String::from("transient"),
details: None,
},
)
.await?;
recorder
.record_workflow_reopened(
recorded_at(3),
run_id,
vec![aion_core::ActivityId::from_sequence_position(2)],
)
.await?;
let history = store.read_history(&workflow_id).await?;
assert_eq!(history.len(), 3);
assert_eq!(history[2].seq(), 3);
assert_eq!(recorder.current_head(), 3);
assert!(matches!(history[2], Event::WorkflowReopened { .. }));
assert_eq!(
aion_core::status_from_events(&history),
aion_core::WorkflowStatus::Running,
"a recorded reopen returns the failed workflow to Running"
);
Ok(())
}
#[tokio::test]
async fn records_workflow_continued_as_new_terminal_event()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(2);
let parent_run_id = aion_core::RunId::new(uuid::Uuid::from_u128(20));
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
let continued_at = recorded_at(2);
let continued_input = payload("continued-input")?;
let workflow_type = Some(String::from("checkout-v2"));
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: aion_core::RunId::new(uuid::Uuid::from_u128(1)),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
recorder
.record_workflow_continued_as_new(
continued_at,
continued_input.clone(),
workflow_type.clone(),
parent_run_id.clone(),
)
.await?;
let history = store.read_history(&workflow_id).await?;
assert_eq!(history.len(), 2);
assert_eq!(history[0].seq(), 1);
assert_eq!(history[1].seq(), 2);
match &history[1] {
Event::WorkflowContinuedAsNew {
envelope,
input,
workflow_type: recorded_workflow_type,
parent_run_id: recorded_parent_run_id,
} => {
assert_eq!(envelope.recorded_at, continued_at);
assert_eq!(input, &continued_input);
assert_eq!(recorded_workflow_type, &workflow_type);
assert_eq!(recorded_parent_run_id, &parent_run_id);
}
other => return Err(format!("expected WorkflowContinuedAsNew, got {other:?}").into()),
}
assert_eq!(recorder.current_head(), 2);
Ok(())
}
#[tokio::test]
async fn records_workflow_timed_out_terminal_and_projects_timed_out()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(41);
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: aion_core::RunId::new(uuid::Uuid::from_u128(1)),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
recorder
.record_workflow_timed_out(recorded_at(2), String::from("workflow"))
.await?;
let history = store.read_history(&workflow_id).await?;
match history.as_slice() {
[
Event::WorkflowStarted { .. },
Event::WorkflowTimedOut { envelope, timeout },
] => {
assert_eq!(envelope.seq, 2);
assert_eq!(timeout, "workflow");
}
other => return Err(format!("expected started then timed out, found {other:?}").into()),
}
assert_eq!(
aion_core::status_from_events(&history),
aion_core::WorkflowStatus::TimedOut,
"a recorded WorkflowTimedOut projects to TimedOut"
);
assert_eq!(recorder.current_head(), 2);
Ok(())
}
#[tokio::test]
async fn records_validated_search_attributes_updated_event()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(7);
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
let mut schema = SearchAttributeSchema::new();
schema.register("customer_id", SearchAttributeType::String)?;
schema.register("attempt", SearchAttributeType::Int)?;
let attributes = HashMap::from([
(
String::from("customer_id"),
SearchAttributeValue::String(String::from("customer-123")),
),
(String::from("attempt"), SearchAttributeValue::Int(2)),
]);
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: aion_core::RunId::new(uuid::Uuid::from_u128(1)),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
recorder
.record_search_attributes_updated(recorded_at(2), attributes.clone(), &schema)
.await?;
let history = store.read_history(&workflow_id).await?;
match history.as_slice() {
[
Event::WorkflowStarted { .. },
Event::SearchAttributesUpdated {
envelope,
workflow_id: recorded_workflow_id,
attributes: stored_attributes,
},
] => {
assert_eq!(envelope.seq, 2);
assert_eq!(recorded_workflow_id, &workflow_id);
assert_eq!(stored_attributes, &attributes);
}
other => {
return Err(
format!("expected started then search attributes, found {other:?}").into(),
);
}
}
assert_eq!(recorder.current_head(), 2);
Ok(())
}
#[tokio::test]
async fn started_with_attributes_appends_both_events_in_one_atomic_batch()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(11);
let run_id = aion_core::RunId::new(uuid::Uuid::from_u128(1));
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
let mut schema = SearchAttributeSchema::new();
schema.register("aion.namespace", SearchAttributeType::String)?;
let attributes = HashMap::from([(
String::from("aion.namespace"),
SearchAttributeValue::String(String::from("tenant-a")),
)]);
recorder
.record_workflow_started_with_attributes(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: run_id.clone(),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
attributes.clone(),
&schema,
)
.await?;
let history = store.read_history(&workflow_id).await?;
match history.as_slice() {
[
Event::WorkflowStarted {
envelope: started_envelope,
run_id: started_run_id,
..
},
Event::SearchAttributesUpdated {
envelope: attributes_envelope,
workflow_id: recorded_workflow_id,
attributes: stored_attributes,
},
] => {
assert_eq!(started_envelope.seq, 1);
assert_eq!(started_run_id, &run_id);
assert_eq!(attributes_envelope.seq, 2);
assert_eq!(recorded_workflow_id, &workflow_id);
assert_eq!(stored_attributes, &attributes);
}
other => {
return Err(
format!("expected started then search attributes, found {other:?}").into(),
);
}
}
assert_eq!(recorder.current_head(), 2);
// The recorder must keep appending correctly after the two-event batch.
recorder
.record_workflow_completed(recorded_at(3), payload("result")?)
.await?;
let history = store.read_history(&workflow_id).await?;
assert_eq!(history.len(), 3);
assert_eq!(history[2].seq(), 3);
Ok(())
}
#[tokio::test]
async fn started_with_invalid_attributes_appends_nothing()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(12);
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
let schema = SearchAttributeSchema::new();
let attributes = HashMap::from([(
String::from("aion.namespace"),
SearchAttributeValue::String(String::from("tenant-a")),
)]);
let result = recorder
.record_workflow_started_with_attributes(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: aion_core::RunId::new(uuid::Uuid::from_u128(1)),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
attributes,
&schema,
)
.await;
assert!(matches!(
result,
Err(DurabilityError::SearchAttribute(
SearchAttributeError::UnregisteredAttribute { name }
)) if name == "aion.namespace"
));
assert!(store.read_history(&workflow_id).await?.is_empty());
assert_eq!(recorder.current_head(), 0);
Ok(())
}
#[tokio::test]
async fn started_with_empty_attributes_appends_only_the_start_event()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(13);
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
let schema = SearchAttributeSchema::new();
recorder
.record_workflow_started_with_attributes(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: aion_core::RunId::new(uuid::Uuid::from_u128(1)),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
HashMap::new(),
&schema,
)
.await?;
let history = store.read_history(&workflow_id).await?;
assert!(matches!(
history.as_slice(),
[Event::WorkflowStarted { .. }]
));
assert_eq!(recorder.current_head(), 1);
Ok(())
}
#[tokio::test]
async fn invalid_search_attributes_return_error_without_appending()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(8);
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
let mut schema = SearchAttributeSchema::new();
schema.register("attempt", SearchAttributeType::Int)?;
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: aion_core::RunId::new(uuid::Uuid::from_u128(1)),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
let attributes = HashMap::from([(
String::from("attempt"),
SearchAttributeValue::String(String::from("two")),
)]);
let error = recorder
.record_search_attributes_updated(recorded_at(2), attributes, &schema)
.await;
match error {
Err(DurabilityError::SearchAttribute(SearchAttributeError::TypeMismatch {
name,
expected,
actual,
})) => {
assert_eq!(name, "attempt");
assert_eq!(expected, SearchAttributeType::Int);
assert_eq!(actual, SearchAttributeType::String);
}
Err(other) => {
return Err(format!("expected search attribute error, got {other:?}").into());
}
Ok(()) => return Err("expected search attribute validation error".into()),
}
assert_eq!(recorder.current_head(), 1);
assert_eq!(store.read_history(&workflow_id).await?.len(), 1);
Ok(())
}
/// #214: after a workloop boundary the predecessor's row is closed and the
/// successor's row is the one running row.
#[tokio::test]
async fn a_workloop_boundary_closes_the_predecessor_row_and_opens_the_successors()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(214);
let first = aion_core::RunId::new(uuid::Uuid::from_u128(2141));
let second = aion_core::RunId::new(uuid::Uuid::from_u128(2142));
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone())
.with_visibility(first.clone(), store.clone());
let start =
|run_id: aion_core::RunId| -> Result<super::WorkflowStartRecord, Box<dyn std::error::Error>> {
Ok(super::WorkflowStartRecord {
workflow_type: String::from("disk_reaper"),
input: aion_core::Payload::from_json(&serde_json::json!({}))?,
run_id,
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
})
};
let opened = chrono::Utc::now();
recorder
.record_workflow_started(opened, start(first.clone())?)
.await?;
let closed = opened + chrono::Duration::minutes(10);
recorder
.record_workloop_iteration_boundary(
closed,
Vec::new(),
Vec::new(),
aion_core::Payload::from_json(&serde_json::json!({}))?,
first.clone(),
start(second.clone())?,
)
.await?;
// ONE ROW PER WORKFLOW: the boundary's successor upsert REPLACED
// the predecessor's row — the store holds exactly the current
// generation, Running, and no ContinuedAsNew row survives anywhere
// a list can see. The chain stays in history, under describe.
let row = store
.get_visibility(&workflow_id)
.await?
.ok_or("the workflow keeps its one row")?;
assert_eq!(row.run_id, second);
assert_eq!(row.status, aion_core::WorkflowStatus::Running);
assert_eq!(row.ended_at, None);
assert_eq!(row.started_at, closed);
Ok(())
}
/// A straggler through a SUPERSEDED generation's recorder (a lease event
/// for the predecessor landing after the boundary replaced its row) may
/// heal the workflow's row forward, never regress it: the row keeps the
/// successor's run and start.
#[tokio::test]
async fn a_straggling_touch_from_a_superseded_generation_never_regresses_the_row()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(13);
let first = aion_core::RunId::new(uuid::Uuid::from_u128(1301));
let second = aion_core::RunId::new(uuid::Uuid::from_u128(1302));
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone())
.with_visibility(first.clone(), store.clone());
let opened = recorded_at(1);
recorder
.record_workflow_started(
opened,
super::WorkflowStartRecord {
workflow_type: String::from("disk_reaper"),
input: payload("input")?,
run_id: first.clone(),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
let start = |run_id: aion_core::RunId| -> Result<_, Box<dyn std::error::Error>> {
Ok(super::WorkflowStartRecord {
workflow_type: String::from("disk_reaper"),
input: payload("input")?,
run_id,
parent_run_id: Some(first.clone()),
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
})
};
let closed = opened + chrono::Duration::minutes(10);
recorder
.record_workloop_iteration_boundary(
closed,
Vec::new(),
Vec::new(),
aion_core::Payload::from_json(&serde_json::json!({}))?,
first.clone(),
start(second.clone())?,
)
.await?;
let head = recorder.current_head();
// The straggler: a recorder still pointed at the FIRST generation
// records a lease event after the boundary.
let mut straggler = Recorder::resume_at(workflow_id.clone(), store.clone(), head)
.with_visibility(first.clone(), store.clone());
let worker = aion_core::WorkerAttribution {
identity: String::from("w-late"),
task_queue: String::from("reaper"),
node: None,
deployment: None,
instance_id: None,
transport: aion_core::WorkerTransport::Grpc,
};
straggler
.record_activity_leased(
closed + chrono::Duration::seconds(1),
aion_core::ActivityId::from_sequence_position(head + 1),
1,
worker,
)
.await?;
let row = store
.get_visibility(&workflow_id)
.await?
.ok_or("the workflow keeps its one row")?;
assert_eq!(
row.run_id, second,
"the row never regresses to the superseded generation"
);
assert_eq!(row.started_at, closed);
assert_eq!(row.status, aion_core::WorkflowStatus::Running);
Ok(())
}
#[tokio::test]
async fn recorder_visibility_updates_after_search_attributes()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(9);
let run_id = aion_core::RunId::new(uuid::Uuid::from_u128(90));
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone())
.with_visibility(run_id.clone(), store.clone());
let mut schema = SearchAttributeSchema::new();
schema.register("customer_id", SearchAttributeType::String)?;
let attributes = HashMap::from([(
String::from("customer_id"),
SearchAttributeValue::String(String::from("customer-123")),
)]);
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
// The run the projection is for IS the run that started: a row is
// projected from its own generation's window.
run_id: run_id.clone(),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
recorder
.record_search_attributes_updated(recorded_at(2), attributes.clone(), &schema)
.await?;
let row = store
.get_visibility(&workflow_id)
.await?
.ok_or("the attribute update projects the run's row")?;
assert_eq!(row.workflow_id, workflow_id);
assert_eq!(row.run_id, run_id);
assert_eq!(row.search_attributes, attributes);
assert_eq!(row.updated_at, recorded_at(2));
Ok(())
}
/// WA-010 R4: the per-append touch moves `current_worker` on the stored
/// row through the same transition the whole-history fold uses, so the
/// row a list reads and the summary a describe builds from history agree
/// after every lease and every attempt terminal — without the touch
/// re-reading history.
#[tokio::test]
async fn recorder_touch_moves_current_worker_on_the_row_like_the_fold()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(11);
let run_id = RunId::new(uuid::Uuid::from_u128(110));
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone())
.with_visibility(run_id.clone(), store.clone());
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: run_id.clone(),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
let worker = |identity: &str| aion_core::WorkerAttribution {
identity: identity.to_owned(),
task_queue: String::from("billing"),
node: None,
deployment: None,
instance_id: None,
transport: aion_core::WorkerTransport::Grpc,
};
let activity = aion_core::ActivityId::from_sequence_position(2);
recorder
.record_activity_leased(recorded_at(2), activity.clone(), 1, worker("w-a"))
.await?;
let row = store
.get_visibility(&workflow_id)
.await?
.ok_or("the start upsert projects the row")?;
assert_eq!(
row.summary().current_worker.map(|w| w.identity).as_deref(),
Some("w-a"),
"the lease touch names the worker on the row"
);
assert_eq!(row.updated_at, recorded_at(2));
// A redelivery of the same attempt: the later lease wins.
recorder
.record_activity_leased(recorded_at(3), activity.clone(), 1, worker("w-b"))
.await?;
recorder
.record_activity_completed(recorded_at(4), activity, payload("done")?, 1)
.await?;
let row = store
.get_visibility(&workflow_id)
.await?
.ok_or("the row survives every touch")?;
assert_eq!(
row.summary().current_worker,
None,
"the attempt's completion clears its lease on the row"
);
assert_eq!(row.updated_at, recorded_at(4));
// The row equals what a full projection of the same history builds.
let history = store.read_history(&workflow_id).await?;
let projected = crate::lifecycle::visibility::project_visibility(&history, &run_id)
.ok_or("a started history projects")?;
assert_eq!(
row, projected,
"touched row == projected row, leases included"
);
Ok(())
}
/// A visibility store that refuses writes while armed and delegates
/// otherwise — the crash window between a durable append and its row.
struct ArmedVisibilityStore {
inner: Arc<InMemoryStore>,
refuse: std::sync::atomic::AtomicBool,
}
#[async_trait]
impl VisibilityStore for ArmedVisibilityStore {
async fn record_visibility(&self, record: VisibilityRecord) -> Result<(), StoreError> {
if self.refuse.load(std::sync::atomic::Ordering::SeqCst) {
return Err(StoreError::Backend(String::from("visibility unavailable")));
}
self.inner.record_visibility(record).await
}
async fn get_visibility(
&self,
workflow_id: &aion_core::WorkflowId,
) -> Result<Option<VisibilityRecord>, StoreError> {
self.inner.get_visibility(workflow_id).await
}
async fn remove_visibility(
&self,
workflow_id: &aion_core::WorkflowId,
run_id: &RunId,
) -> Result<bool, StoreError> {
self.inner.remove_visibility(workflow_id, run_id).await
}
async fn list_workflows(
&self,
request: &WorkflowListRequest,
) -> Result<VisibilityPage, StoreError> {
self.inner.list_workflows(request).await
}
}
/// A recorder over an [`ArmedVisibilityStore`] with a started workflow
/// whose row was projected the way the lifecycle handlers project it
/// (`record_workflow_started` and `record_workflow_failed` append without
/// projecting; the lifecycle handlers project after them).
struct ArmedFixture {
workflow_id: aion_core::WorkflowId,
run_id: RunId,
store: Arc<InMemoryStore>,
visibility: Arc<ArmedVisibilityStore>,
recorder: Recorder,
}
impl ArmedFixture {
async fn started() -> Result<Self, Box<dyn std::error::Error>> {
let workflow_id = workflow_id(11);
let run_id = RunId::new(uuid::Uuid::from_u128(110));
let store = Arc::new(InMemoryStore::default());
let visibility = Arc::new(ArmedVisibilityStore {
inner: Arc::clone(&store),
refuse: std::sync::atomic::AtomicBool::new(false),
});
let mut recorder = Recorder::new(workflow_id.clone(), store.clone()).with_visibility(
run_id.clone(),
Arc::clone(&visibility) as Arc<dyn VisibilityStore>,
);
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: run_id.clone(),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
let fixture = Self {
workflow_id,
run_id,
store,
visibility,
recorder,
};
fixture.project().await?;
assert_eq!(
fixture.row().await?.head_seq,
1,
"the start projects a row at 1"
);
Ok(fixture)
}
/// Projects the whole row from history, as a lifecycle handler does.
async fn project(&self) -> Result<(), Box<dyn std::error::Error>> {
let history = self.store.read_history(&self.workflow_id).await?;
let row = crate::lifecycle::visibility::project_visibility(&history, &self.run_id)
.ok_or("the history projects a row")?;
self.visibility.record_visibility(row).await?;
Ok(())
}
async fn row(&self) -> Result<VisibilityRecord, Box<dyn std::error::Error>> {
Ok(self
.store
.get_visibility(&self.workflow_id)
.await?
.ok_or("the row survives")?)
}
/// A non-lifecycle append at `seq`: the per-append touch path.
async fn touch(&mut self, seq: u64) -> Result<(), Box<dyn std::error::Error>> {
self.recorder
.record_activity_scheduled(
recorded_at(i64::try_from(seq)?),
aion_core::ActivityId::from_sequence_position(seq),
format!("step_{seq}"),
payload("input")?,
String::from("default"),
None,
)
.await?;
Ok(())
}
fn refuse(&self, refuse: bool) {
self.visibility
.refuse
.store(refuse, std::sync::atomic::Ordering::SeqCst);
}
}
/// A touch right behind the row's head stamps it: the row provably
/// folded everything before that event.
#[tokio::test]
async fn a_contiguous_touch_carries_the_stamp_forward() -> Result<(), Box<dyn std::error::Error>>
{
let mut fixture = ArmedFixture::started().await?;
fixture.touch(2).await?;
let row = fixture.row().await?;
assert_eq!(
row.head_seq, 2,
"a contiguous touch carries the stamp forward"
);
assert_eq!(
aion_store::visibility::head::verdict(Some(&row), 2),
aion_store::visibility::head::RowVerdict::InFlight
);
Ok(())
}
/// The stamp is inductive: after a projection the store refused, later
/// touches must NOT carry the row to the stream head — a row stamped at
/// the head with a status that missed a lifecycle event would be trusted
/// by the next boot. Left behind, it is re-derived instead.
#[tokio::test]
async fn a_touch_never_stamps_a_row_past_a_projection_that_failed()
-> Result<(), Box<dyn std::error::Error>> {
let mut fixture = ArmedFixture::started().await?;
fixture.touch(2).await?;
assert_eq!(fixture.row().await?.head_seq, 2);
// The lifecycle event lands durably; its projection is refused.
fixture
.recorder
.record_workflow_failed(
recorded_at(3),
aion_core::WorkflowError {
message: String::from("boom"),
details: None,
},
)
.await?;
fixture.refuse(true);
assert!(
fixture.project().await.is_err(),
"the failure's projection was refused"
);
fixture.refuse(false);
// A touch follows the unfolded failure.
fixture.touch(4).await?;
let history = fixture.store.read_history(&fixture.workflow_id).await?;
assert_eq!(
history.len(),
4,
"the history holds the failure the row never folded"
);
let row = fixture.row().await?;
assert_eq!(
row.status,
aion_core::WorkflowStatus::Running,
"the row never saw the failure"
);
assert_eq!(
row.head_seq, 2,
"the touch refuses to stamp across the unfolded event: the row stays behind the head"
);
assert_eq!(
aion_store::visibility::head::verdict(Some(&row), 4),
aion_store::visibility::head::RowVerdict::Unsettled,
"a boot re-derives this row instead of trusting it"
);
Ok(())
}
#[tokio::test]
async fn visibility_upsert_failure_after_append_logs_warning_and_succeeds()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(10);
let run_id = RunId::new(uuid::Uuid::from_u128(100));
let store = Arc::new(InMemoryStore::default());
let visibility_store = Arc::new(FailingVisibilityStore);
let (logs, subscriber) = crate::log_capture::LogCapture::new()?;
let guard = tracing::subscriber::set_default(subscriber);
let mut recorder = Recorder::new(workflow_id.clone(), store.clone())
.with_visibility(run_id.clone(), visibility_store);
let mut schema = SearchAttributeSchema::new();
schema.register("customer_id", SearchAttributeType::String)?;
let attributes = HashMap::from([(
String::from("customer_id"),
SearchAttributeValue::String(String::from("customer-123")),
)]);
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: run_id.clone(),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
recorder
.record_search_attributes_updated(recorded_at(2), attributes, &schema)
.await?;
drop(guard);
let history = store.read_history(&workflow_id).await?;
assert_eq!(history.len(), 2);
assert_eq!(recorder.current_head(), 2);
// ONE warning, and everything the operator needs is in THAT one. An
// earlier revision of this test held its own `Vec<u8>` capture and
// asserted over the joined rendering of every line, which could not
// tell one complete warning from five partial ones spread across five
// events — and, holding its own capture, never installed the
// interest floor that `crate::log_capture` exists to guarantee.
let warnings = logs.at_level("WARN")?;
assert_eq!(
warnings.len(),
1,
"the failed visibility upsert must produce exactly one WARN — more than one means the \
warning was split across events and no single line carries the whole fact: \
{warnings:?}"
);
let warning = warnings
.first()
.ok_or("the visibility-upsert warning was not captured at all")?;
for needle in [
"visibility upsert failed after durable append",
"crash-consistency window",
"visibility unavailable",
workflow_id.to_string().as_str(),
run_id.to_string().as_str(),
] {
assert!(
warning.mentions(needle),
"the visibility-upsert warning did not mention `{needle}`: {warning}"
);
}
Ok(())
}
#[tokio::test]
async fn records_activity_events_in_sequence_order() -> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(6);
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
let activity_id = aion_core::ActivityId::from_sequence_position(1);
recorder
.record_activity_scheduled(
recorded_at(1),
activity_id.clone(),
String::from("charge-card"),
payload("input")?,
String::from("default"),
None,
)
.await?;
recorder
.record_activity_completed(recorded_at(2), activity_id.clone(), payload("result")?, 1)
.await?;
let history = store.read_history(&workflow_id).await?;
assert_eq!(history.len(), 2);
assert_eq!(history[0].seq(), 1);
assert_eq!(history[1].seq(), 2);
match &history[0] {
Event::ActivityScheduled {
activity_id: recorded_activity_id,
..
} => assert_eq!(recorded_activity_id, &activity_id),
other => return Err(format!("expected ActivityScheduled, got {other:?}").into()),
}
match &history[1] {
Event::ActivityCompleted {
activity_id: recorded_activity_id,
..
} => assert_eq!(recorded_activity_id, &activity_id),
other => return Err(format!("expected ActivityCompleted, got {other:?}").into()),
}
Ok(())
}
#[tokio::test]
async fn resume_at_continues_from_existing_history_head()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(3);
let store = Arc::new(InMemoryStore::default());
let seeded = [
workflow_started(1, &workflow_id)?,
workflow_started(2, &workflow_id)?,
];
store
.append(WriteToken::recorder(), &workflow_id, &seeded, 0)
.await?;
let mut recorder = Recorder::resume_at(workflow_id.clone(), store.clone(), 2);
recorder
.record_signal_received(recorded_at(3), String::from("approve"), payload("signal")?)
.await?;
let history = store.read_history(&workflow_id).await?;
assert_eq!(history.len(), 3);
assert_eq!(history[2].seq(), 3);
assert_eq!(recorder.current_head(), 3);
Ok(())
}
#[tokio::test]
async fn sequence_conflict_surfaces_without_advancing_or_retrying()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(4);
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
let rogue_event = workflow_started(1, &workflow_id)?;
store
.append(WriteToken::recorder(), &workflow_id, &[rogue_event], 0)
.await?;
let error = recorder
.record_timer_fired(recorded_at(2), TimerId::anonymous(2))
.await;
match error {
Err(DurabilityError::Store(StoreError::SequenceConflict { expected, found })) => {
assert_eq!(expected, 0);
assert_eq!(found, 1);
}
Err(other) => return Err(format!("expected sequence conflict, got {other:?}").into()),
Ok(()) => return Err("expected sequence conflict".into()),
}
assert_eq!(recorder.current_head(), 0);
let history = store.read_history(&workflow_id).await?;
assert_eq!(history.len(), 1);
assert_eq!(history[0].seq(), 1);
Ok(())
}
/// aion#145: the ack-loss repair is FORWARD-ONLY and restores the append
/// path. It exists for exactly one shape — an append this recorder issued
/// landed while its acknowledgement was lost, leaving the tracked head
/// below the durable head — and it must never move the head backward
/// (which could mask a genuine double-writer conflict by re-arming a stale
/// expectation). The sibling pin
/// `sequence_conflict_surfaces_without_advancing_or_retrying` guards the
/// other side: a conflict itself never reconciles anything.
#[tokio::test]
async fn reconcile_head_forward_is_forward_only_and_restores_the_append_path()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(42);
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::new(workflow_id.clone(), store.clone());
// Reproduce the ack-lost store state: the event the recorder minted
// landed durably, but `mark_append_success` was never reached.
let landed = workflow_started(1, &workflow_id)?;
store
.append(WriteToken::recorder(), &workflow_id, &[landed], 0)
.await?;
assert_eq!(
recorder.current_head(),
0,
"the ack loss left the tracker stale-low"
);
// At-head (here: still-stale-equal) reconciliation is a no-op.
recorder.reconcile_head_forward(0);
assert_eq!(recorder.current_head(), 0);
// Forward reconciliation adopts the durable head.
recorder.reconcile_head_forward(1);
assert_eq!(recorder.current_head(), 1);
// Never backward.
recorder.reconcile_head_forward(0);
assert_eq!(recorder.current_head(), 1);
// The append path is healthy again: the next event lands at seq 2.
recorder
.record_signal_received(recorded_at(2), String::from("next"), payload("signal")?)
.await?;
let history = store.read_history(&workflow_id).await?;
assert_eq!(history.len(), 2);
assert_eq!(history[1].seq(), 2);
assert_eq!(recorder.current_head(), 2);
Ok(())
}
#[tokio::test]
async fn sequence_overflow_returns_error_without_appending()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(5);
let store = Arc::new(InMemoryStore::default());
let mut recorder = Recorder::resume_at(workflow_id.clone(), store.clone(), u64::MAX);
let error = recorder
.record_workflow_completed(recorded_at(1), payload("result")?)
.await;
match error {
Err(DurabilityError::HistoryShape { reason }) => {
assert!(reason.contains("sequence head overflow"));
}
Err(other) => return Err(format!("expected sequence overflow, got {other:?}").into()),
Ok(()) => return Err("expected sequence overflow".into()),
}
assert_eq!(recorder.current_head(), u64::MAX);
assert!(store.read_history(&workflow_id).await?.is_empty());
Ok(())
}
/// #253 settle-at-terminal: every workflow terminal the Recorder records
/// (completed / failed / cancelled) settles the workflow's outbox rows
/// through the store seam, AND a settle failure never fails the recorded
/// terminal. The spy's `settle_workflow_outbox_rows_cancelled` returns an
/// `Err` sentinel, so each terminal here succeeding while the call is
/// recorded proves both halves at once.
#[tokio::test]
async fn workflow_terminals_settle_outbox_rows_and_settle_errors_are_nonfatal()
-> Result<(), Box<dyn std::error::Error>> {
use aion_store::testing::ShardSeamSpy;
type TerminalRecorder = fn(
&mut Recorder,
) -> std::pin::Pin<
Box<dyn Future<Output = Result<(), DurabilityError>> + Send + '_>,
>;
let terminals: [(&str, TerminalRecorder); 3] = [
("completed", |recorder| {
Box::pin(async move {
recorder
.record_workflow_completed(
recorded_at(2),
Payload::from_json(&json!({"label": "result"})).map_err(|error| {
DurabilityError::HistoryShape {
reason: error.to_string(),
}
})?,
)
.await
})
}),
("failed", |recorder| {
Box::pin(async move {
recorder
.record_workflow_failed(
recorded_at(2),
aion_core::WorkflowError {
message: String::from("boom"),
details: None,
},
)
.await
})
}),
("cancelled", |recorder| {
Box::pin(async move {
recorder
.record_workflow_cancelled(recorded_at(2), String::from("operator"))
.await
})
}),
];
for (label, record_terminal) in terminals {
let workflow_id = workflow_id(31);
let spy = Arc::new(ShardSeamSpy::new());
let store: Arc<dyn aion_store::EventStore> = Arc::clone(&spy) as _;
let mut recorder = Recorder::new(workflow_id.clone(), Arc::clone(&store));
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: aion_core::RunId::new(uuid::Uuid::from_u128(1)),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
record_terminal(&mut recorder).await.map_err(|error| {
format!("terminal `{label}` must record despite the settle Err sentinel: {error}")
})?;
let history = store.read_history(&workflow_id).await?;
assert_eq!(
history.len(),
2,
"terminal `{label}` must be durably recorded"
);
let calls = spy.calls();
assert!(
calls.contains(&format!(
"settle_workflow_outbox_rows_cancelled:{workflow_id}"
)),
"terminal `{label}` must settle the workflow's outbox rows; saw {calls:?}"
);
}
Ok(())
}
/// #253: continue-as-new is NOT a settle point — the workflow continues,
/// and the old run's stragglers are neutralized by run-scoped completion
/// threading (OBX-011), so its rows must stay live.
#[tokio::test]
async fn continue_as_new_does_not_settle_outbox_rows() -> Result<(), Box<dyn std::error::Error>>
{
use aion_store::testing::ShardSeamSpy;
let workflow_id = workflow_id(32);
let spy = Arc::new(ShardSeamSpy::new());
let store: Arc<dyn aion_store::EventStore> = Arc::clone(&spy) as _;
let mut recorder = Recorder::new(workflow_id.clone(), store);
recorder
.record_workflow_started(
recorded_at(1),
super::WorkflowStartRecord {
workflow_type: String::from("checkout"),
input: payload("input")?,
run_id: aion_core::RunId::new(uuid::Uuid::from_u128(1)),
parent_run_id: None,
parent_workflow_id: None,
package_version: aion_core::PackageVersion::new("a".repeat(64)),
},
)
.await?;
recorder
.record_workflow_continued_as_new(
recorded_at(2),
payload("continued")?,
None,
aion_core::RunId::new(uuid::Uuid::from_u128(2)),
)
.await?;
let calls = spy.calls();
assert!(
!calls
.iter()
.any(|call| call.starts_with("settle_workflow_outbox_rows_cancelled:")),
"continue-as-new must never settle the workflow's outbox rows; saw {calls:?}"
);
Ok(())
}
/// AD-014 (aion#192), the shape that bit: a COMMAND append — `TimerStarted`
/// — whose write lands while its acknowledgement is lost. The recorder
/// resolves it against the store instead of surfacing the error: the call
/// returns Ok with the armed sequence, the tracked head equals the durable
/// head, the history holds the event exactly once, the loss is said at
/// WARN with the landed range, and the NEXT append through the same
/// recorder lands at the correct sequence.
#[tokio::test]
async fn an_ack_lost_command_append_is_resolved_as_recorded_and_the_run_keeps_appending()
-> Result<(), Box<dyn std::error::Error>> {
let (capture, subscriber) = crate::log_capture::LogCapture::new()?;
let _guard = tracing::subscriber::set_default(subscriber);
let workflow_id = workflow_id(40);
let store = Arc::new(crate::store_faults::FlakyStore::new());
store
.append(
WriteToken::recorder(),
&workflow_id,
&[workflow_started(1, &workflow_id)?],
0,
)
.await?;
let mut recorder = Recorder::resume_at(
workflow_id.clone(),
Arc::clone(&store) as Arc<dyn aion_store::EventStore>,
1,
);
store.lose_ack_of_next_appends(1);
let armed = recorder
.record_timer_started(recorded_at(2), TimerId::anonymous(1), recorded_at(60))
.await?;
assert_eq!(
armed, 2,
"the command append reports the sequence it landed at"
);
assert_eq!(
recorder.current_head(),
2,
"the tracked head equals the durable head"
);
let history = store.recorded_history(&workflow_id).await?;
assert_eq!(
history.len(),
2,
"the event is in the history exactly once: {history:#?}"
);
assert!(matches!(history[1], Event::TimerStarted { .. }));
let warned = capture.at_level("WARN")?;
let resolved = warned
.iter()
.find(|event| event.mentions("acknowledgement was lost"))
.ok_or("the resolved append must be said at WARN")?;
assert_eq!(resolved.field("landed_seq_from"), Some("2"));
assert_eq!(resolved.field("landed_seq_to"), Some("2"));
assert!(
resolved
.field("store_error")
.is_some_and(|text| text.contains("injected by FlakyStore")),
"the WARN carries the store's original error: {resolved:?}"
);
recorder
.record_timer_fired(recorded_at(3), TimerId::anonymous(1))
.await?;
assert_eq!(recorder.current_head(), 3);
assert_eq!(store.recorded_history(&workflow_id).await?.len(), 3);
Ok(())
}
/// AD-014: a terminal append (`WorkflowCancelled`) under a lost
/// acknowledgement is resolved the same way — Ok, ONE terminal in the
/// history, head advanced — because the run's status is a projection of
/// exactly that event and a second one must never be minted.
#[tokio::test]
async fn an_ack_lost_terminal_append_is_resolved_with_one_terminal_in_the_history()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(41);
let store = Arc::new(crate::store_faults::FlakyStore::new());
store
.append(
WriteToken::recorder(),
&workflow_id,
&[workflow_started(1, &workflow_id)?],
0,
)
.await?;
let mut recorder = Recorder::resume_at(
workflow_id.clone(),
Arc::clone(&store) as Arc<dyn aion_store::EventStore>,
1,
);
store.lose_ack_of_next_appends(1);
recorder
.record_workflow_cancelled(recorded_at(2), String::from("operator"))
.await?;
assert_eq!(recorder.current_head(), 2);
let history = store.recorded_history(&workflow_id).await?;
let terminals = history
.iter()
.filter(|event| matches!(event, Event::WorkflowCancelled { .. }))
.count();
assert_eq!(terminals, 1, "exactly one terminal: {history:#?}");
Ok(())
}
/// AD-014: an append the store refused BEFORE writing is not a resolution
/// case — nothing is at the expected sequence, so the ORIGINAL error is
/// returned unchanged, the head is untouched, and the caller's own retry
/// then lands at the same sequence.
#[tokio::test]
async fn a_refused_append_that_did_not_land_returns_the_refusal_and_leaves_the_head()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(42);
let store = Arc::new(crate::store_faults::FlakyStore::new());
store
.append(
WriteToken::recorder(),
&workflow_id,
&[workflow_started(1, &workflow_id)?],
0,
)
.await?;
let mut recorder = Recorder::resume_at(
workflow_id.clone(),
Arc::clone(&store) as Arc<dyn aion_store::EventStore>,
1,
);
store.fail_next_appends(1);
let refused = recorder
.record_timer_started(recorded_at(2), TimerId::anonymous(1), recorded_at(60))
.await;
match refused {
Err(DurabilityError::Store(StoreError::Backend(text))) => {
assert!(text.contains("transient append failure injected by FlakyStore"));
}
other => return Err(format!("expected the store's refusal, got {other:?}").into()),
}
assert_eq!(
recorder.current_head(),
1,
"nothing landed, nothing advances"
);
assert_eq!(store.recorded_history(&workflow_id).await?.len(), 1);
let armed = recorder
.record_timer_started(recorded_at(2), TimerId::anonymous(1), recorded_at(60))
.await?;
assert_eq!(armed, 2, "the caller's retry lands at the same sequence");
Ok(())
}
/// AD-014: a fenced write (`NotOwner`) is a definite did-not-land and a
/// typed routing signal, so it is surfaced AS IS — no resolving read.
/// The rogue planted at the expected sequence is the discriminator: a
/// resolver that ran would have found it and minted a `SequenceConflict`
/// (a double-writer alarm out of a routing condition), and a resolver
/// whose read failed would have folded the variant into `Backend`.
#[tokio::test]
async fn a_fenced_append_is_surfaced_typed_without_a_resolving_read()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(45);
let store = Arc::new(crate::store_faults::FlakyStore::new());
store
.append(
WriteToken::recorder(),
&workflow_id,
&[workflow_started(1, &workflow_id)?],
0,
)
.await?;
let mut recorder = Recorder::resume_at(
workflow_id.clone(),
Arc::clone(&store) as Arc<dyn aion_store::EventStore>,
1,
);
let rogue = Event::TimerFired {
envelope: aion_core::EventEnvelope {
seq: 2,
recorded_at: recorded_at(2),
workflow_id: workflow_id.clone(),
},
timer_id: TimerId::anonymous(9),
};
store
.append(WriteToken::recorder(), &workflow_id, &[rogue], 1)
.await?;
store.fence_next_appends(1);
store.fail_next_range_reads(1);
let outcome = recorder
.record_timer_started(recorded_at(2), TimerId::anonymous(1), recorded_at(60))
.await;
match outcome {
Err(DurabilityError::Store(StoreError::NotOwner { shard })) => {
assert_eq!(shard, 7, "the fence comes back typed, shard and all");
}
other => {
return Err(format!("expected the typed NotOwner refusal, got {other:?}").into());
}
}
assert_eq!(recorder.current_head(), 1, "a fence never moves the head");
Ok(())
}
/// AD-014 keeps aion#145's other side: when the resolving read finds
/// SOMETHING ELSE at the expected sequence — a rogue writer's event, not
/// this batch — the result is a genuine `SequenceConflict` and the head
/// is NOT moved. Resyncing here would silence the double-writer alarm.
#[tokio::test]
async fn a_refused_append_over_a_rogue_event_is_a_conflict_not_a_resync()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(43);
let store = Arc::new(crate::store_faults::FlakyStore::new());
store
.append(
WriteToken::recorder(),
&workflow_id,
&[workflow_started(1, &workflow_id)?],
0,
)
.await?;
let mut recorder = Recorder::resume_at(
workflow_id.clone(),
Arc::clone(&store) as Arc<dyn aion_store::EventStore>,
1,
);
// The rogue lands at seq 2 behind this recorder's back; the recorder's
// own append is then refused before writing, so the resolving read
// sees the rogue where its batch would have been.
let rogue = Event::TimerFired {
envelope: aion_core::EventEnvelope {
seq: 2,
recorded_at: recorded_at(2),
workflow_id: workflow_id.clone(),
},
timer_id: TimerId::anonymous(9),
};
store
.append(WriteToken::recorder(), &workflow_id, &[rogue], 1)
.await?;
store.fail_next_appends(1);
let outcome = recorder
.record_timer_started(recorded_at(2), TimerId::anonymous(1), recorded_at(60))
.await;
match outcome {
Err(DurabilityError::Store(StoreError::SequenceConflict { expected, found })) => {
assert_eq!((expected, found), (1, 2));
}
other => return Err(format!("expected a sequence conflict, got {other:?}").into()),
}
assert_eq!(
recorder.current_head(),
1,
"a conflict never reconciles the head"
);
Ok(())
}
/// AD-014: when the resolving read itself fails, nothing was learned — the
/// ORIGINAL append error comes back with the read failure attached, and
/// the head is untouched.
#[tokio::test]
async fn a_failed_resolving_read_returns_the_original_error_with_the_read_attached()
-> Result<(), Box<dyn std::error::Error>> {
let workflow_id = workflow_id(44);
let store = Arc::new(crate::store_faults::FlakyStore::new());
store
.append(
WriteToken::recorder(),
&workflow_id,
&[workflow_started(1, &workflow_id)?],
0,
)
.await?;
let mut recorder = Recorder::resume_at(
workflow_id.clone(),
Arc::clone(&store) as Arc<dyn aion_store::EventStore>,
1,
);
store.lose_ack_of_next_appends(1);
store.fail_next_range_reads(1);
let outcome = recorder
.record_timer_started(recorded_at(2), TimerId::anonymous(1), recorded_at(60))
.await;
match outcome {
Err(DurabilityError::Store(StoreError::Backend(text))) => {
assert!(
text.contains("acknowledgement was lost, injected by FlakyStore"),
"{text}"
);
assert!(
text.contains("transient range read failure injected by FlakyStore"),
"{text}"
);
}
other => return Err(format!("expected the original error, got {other:?}").into()),
}
assert_eq!(
recorder.current_head(),
1,
"nothing was learned, nothing advances"
);
Ok(())
}
}