feltdb 0.2.2

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

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum WorkloadState {
    Created,
    Ready,
    Claimed,
    Running,
    Checkpointed,
    Recovering,
    CancellationRequested,
    Succeeded,
    Failed,
    Cancelled,
    TimedOut,
    DeadLetter,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum BackoffPolicy {
    Fixed,
    Exponential,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkloadRetryPolicy {
    pub max_attempts: u32,
    pub initial_delay_ms: u64,
    pub max_delay_ms: u64,
    pub backoff: BackoffPolicy,
    #[serde(default)]
    pub retry_on: BTreeSet<String>,
}
impl Default for WorkloadRetryPolicy {
    fn default() -> Self {
        Self {
            max_attempts: 3,
            initial_delay_ms: 1000,
            max_delay_ms: 300_000,
            backoff: BackoffPolicy::Exponential,
            retry_on: [
                "timeout".into(),
                "worker_failure".into(),
                "transient_connection".into(),
            ]
            .into(),
        }
    }
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct WorkloadTimeouts {
    pub queue_ms: Option<u64>,
    pub claim_ms: Option<u64>,
    pub execution_ms: Option<u64>,
    pub heartbeat_ms: Option<u64>,
    pub overall_ms: Option<u64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkloadDependency {
    pub workload_id: String,
    #[serde(default = "succeeded_state")]
    pub required_state: WorkloadState,
}
fn succeeded_state() -> WorkloadState {
    WorkloadState::Succeeded
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkloadClaim {
    pub claim_id: String,
    pub workload_id: String,
    pub worker_id: String,
    pub lease_id: String,
    pub fencing_token: u64,
    pub capability_snapshot: BTreeSet<String>,
    pub claimed_at: i64,
    pub expires_at: i64,
    pub last_heartbeat: i64,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum AttemptStatus {
    Claimed,
    Running,
    Checkpointed,
    Succeeded,
    Failed,
    TimedOut,
    Cancelled,
    WorkerLost,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkloadAttempt {
    pub attempt_id: String,
    pub workload_id: String,
    pub worker_id: String,
    pub claim_id: String,
    pub fencing_token: u64,
    pub started_at: Option<i64>,
    pub ended_at: Option<i64>,
    pub input_hash: String,
    pub result_hash: Option<String>,
    pub error: Option<String>,
    #[serde(default)]
    pub resource_usage: BTreeMap<String, u64>,
    pub status: AttemptStatus,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkloadCheckpoint {
    pub checkpoint_id: String,
    pub workload_id: String,
    pub attempt_id: String,
    pub sequence: u64,
    pub state: Value,
    pub state_hash: String,
    pub created_at: i64,
    pub produced_by: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkloadResult {
    pub result_id: String,
    pub workload_id: String,
    pub attempt_id: String,
    pub status: String,
    pub output: Value,
    pub output_hash: String,
    #[serde(default)]
    pub artifacts: Vec<String>,
    #[serde(default)]
    pub provenance: BTreeMap<String, String>,
    pub produced_by: String,
    pub created_at: i64,
    pub schema_version: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkloadExecutionContext {
    pub workload_id: String,
    pub attempt_id: String,
    pub worker_id: String,
    pub capabilities: BTreeSet<String>,
    pub connection_scopes: BTreeSet<String>,
    pub secret_references: Vec<String>,
    pub expires_at: i64,
    pub fencing_token: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkloadEvent {
    pub sequence: u64,
    pub event_type: String,
    pub from: Option<WorkloadState>,
    pub to: WorkloadState,
    pub actor: String,
    pub at: i64,
    #[serde(default)]
    pub evidence: BTreeMap<String, String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Workload {
    pub contract_version: u32,
    pub workload_id: String,
    pub tenant_id: String,
    pub application_id: String,
    pub environment: String,
    pub definition_id: String,
    pub definition_revision: String,
    pub trigger: String,
    pub input: Value,
    pub input_hash: String,
    pub requested_by: Subject,
    #[serde(default)]
    pub session_id: Option<String>,
    pub authorization_snapshot: SignedGrant,
    pub capability_snapshot: BTreeSet<String>,
    pub connection_scopes: BTreeSet<String>,
    pub priority: i32,
    pub idempotency_key: String,
    pub created_at: i64,
    pub updated_at: i64,
    pub state: WorkloadState,
    pub execution_history: Vec<WorkloadEvent>,
    pub attempts: Vec<WorkloadAttempt>,
    pub checkpoints: Vec<WorkloadCheckpoint>,
    pub result: Option<WorkloadResult>,
    pub retry_policy: WorkloadRetryPolicy,
    pub timeouts: WorkloadTimeouts,
    pub attempt_count: u32,
    pub retry_count: u32,
    /// The instant, in unix seconds, before which this workload must not be
    /// claimed. `None` means "eligible as soon as it is `Ready`".
    ///
    /// This is the single temporal eligibility concept in the workload model.
    /// It is what retry backoff sets after a failure, what a timeout sets before
    /// re-arming, and what [`CreateWorkload::not_before`] sets at creation, so a
    /// workload that is not yet executable is always not-yet-executable for the
    /// same reason and is refused by the same check in [`WorkloadStore::claim`].
    pub next_attempt_at: Option<i64>,
    pub last_failure: Option<String>,
    pub current_claim: Option<WorkloadClaim>,
    pub fencing_token: u64,
    #[serde(default)]
    pub dependencies: Vec<WorkloadDependency>,
}

impl Workload {
    /// Has this workload's eligibility instant arrived?
    ///
    /// Eligibility is only half of claimability — the state must also be
    /// `Ready`, and [`WorkloadStore::claim`] checks both. This answers the
    /// temporal half alone so a scheduler can skip a workload that is merely
    /// early without attempting a claim it knows will be refused.
    pub fn is_eligible_at(&self, now: i64) -> bool {
        self.next_attempt_at.is_none_or(|instant| instant <= now)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateWorkload {
    pub tenant_id: String,
    pub application_id: String,
    pub environment: String,
    pub definition_id: String,
    pub definition_revision: String,
    pub trigger: String,
    pub input: Value,
    pub requested_by: Subject,
    #[serde(default)]
    pub session_id: Option<String>,
    pub authorization_snapshot: SignedGrant,
    pub capability_snapshot: BTreeSet<String>,
    #[serde(default)]
    pub connection_scopes: BTreeSet<String>,
    #[serde(default)]
    pub priority: i32,
    pub idempotency_key: String,
    #[serde(default)]
    pub retry_policy: WorkloadRetryPolicy,
    #[serde(default)]
    pub timeouts: WorkloadTimeouts,
    #[serde(default)]
    pub dependencies: Vec<WorkloadDependency>,
    /// The instant, in unix seconds, before which this workload must not be
    /// claimed. Omitted or `None` means it is claimable as soon as it is
    /// `Ready`, which is what every workload did before this field existed.
    ///
    /// A workload created with `not_before` is durable immediately and
    /// executable later: it reaches `Ready`, survives restart, and is refused to
    /// every worker until the instant arrives. It is *not* a schedule — there is
    /// no recurrence, no expression, and no component that re-arms it. Creating
    /// the next occurrence is the caller's business.
    ///
    /// A `not_before` already in the past is not an error. It simply describes a
    /// workload that is eligible now, which is the same thing as omitting it.
    #[serde(default)]
    pub not_before: Option<i64>,
    pub created_at: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct WorkloadError {
    pub code: String,
    pub message: String,
}
impl WorkloadError {
    fn new(code: &str, message: impl Into<String>) -> Self {
        Self {
            code: code.into(),
            message: message.into(),
        }
    }
}
impl std::fmt::Display for WorkloadError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.code, self.message)
    }
}
impl std::error::Error for WorkloadError {}

#[derive(Clone)]
pub struct WorkloadStore {
    db: FeltDb,
    pub workloads: BTreeMap<String, Workload>,
    idempotency: BTreeMap<String, String>,
    result_ids: BTreeMap<String, String>,
    /// Rebuildable cache of FeltDB-owned document versions. This is never
    /// serialized and never advances independently of a canonical commit.
    document_versions: BTreeMap<String, u64>,
}

#[derive(Serialize, Deserialize)]
struct LegacyWorkloadStore {
    #[serde(default)]
    workloads: BTreeMap<String, Workload>,
    #[serde(default)]
    idempotency: BTreeMap<String, String>,
    #[serde(default)]
    result_ids: BTreeMap<String, String>,
    #[serde(default)]
    event_sequence: u64,
}

impl WorkloadStore {
    pub fn load(path: impl AsRef<Path>) -> Result<Self, WorkloadError> {
        Self::from_db(FeltDb::open(path).map_err(io)?)
    }

    pub fn from_db(db: FeltDb) -> Result<Self, WorkloadError> {
        let mut workloads = BTreeMap::new();
        let mut document_versions = BTreeMap::new();
        for row in db.list_collection(DURABLE_WORK_COLLECTION).map_err(io)? {
            let version = row
                .value
                .get("__version")
                .and_then(Value::as_u64)
                // Canonical FeltDB CAS defines a pre-versioned document as
                // version 1. The next guarded mutation writes version 2, which
                // upgrades rows created before Work adopted RecordPrecondition.
                .unwrap_or(1);
            let workload: Workload = serde_json::from_value(row.value).map_err(io)?;
            document_versions.insert(workload.workload_id.clone(), version);
            workloads.insert(workload.workload_id.clone(), workload);
        }
        let idempotency = idempotency_index(&workloads, idempotency_scope);
        let mut result_ids = BTreeMap::new();
        for workload in workloads.values() {
            if let Some(result) = &workload.result {
                result_ids.insert(result.result_id.clone(), workload.workload_id.clone());
            }
        }
        Ok(Self {
            db,
            workloads,
            idempotency,
            result_ids,
            document_versions,
        })
    }

    pub fn migrate_legacy_file(db: FeltDb, path: &Path) -> Result<Self, WorkloadError> {
        let bytes = std::fs::read(path).map_err(io)?;
        let legacy: LegacyWorkloadStore = serde_json::from_slice(&bytes).map_err(io)?;
        let mut store = Self::from_db(db)?;
        for (id, workload) in legacy.workloads {
            if id != workload.workload_id {
                return Err(WorkloadError::new(
                    "WORKLOAD_MIGRATION_CONFLICT",
                    format!(
                        "legacy key {id} does not match workload identity {}",
                        workload.workload_id
                    ),
                ));
            }
            if let Some(existing) = store.workloads.get(&id) {
                if serde_json::to_value(existing).map_err(io)?
                    != serde_json::to_value(&workload).map_err(io)?
                {
                    return Err(WorkloadError::new(
                        "WORKLOAD_MIGRATION_CONFLICT",
                        format!("canonical workload {id} differs from legacy source"),
                    ));
                }
                continue;
            }
            store.workloads.insert(id.clone(), workload);
            store.persist_workload(&id, "workload_migration")?;
        }
        // The maps are verification evidence, not migrated authorities.
        let rebuilt = Self::from_db(store.db.clone())?;
        if legacy.idempotency != idempotency_index(&rebuilt.workloads, legacy_idempotency_scope)
            || legacy.result_ids != rebuilt.result_ids
            || legacy.event_sequence != derived_event_sequence(&rebuilt.workloads)
        {
            return Err(WorkloadError::new(
                "WORKLOAD_MIGRATION_CONFLICT",
                "legacy derived indexes do not match canonical work aggregates",
            ));
        }
        Ok(rebuilt)
    }

    fn persist_workload(&mut self, id: &str, action: &str) -> Result<(), WorkloadError> {
        let workload = self
            .workloads
            .get(id)
            .ok_or_else(|| WorkloadError::new("WORKLOAD_NOT_FOUND", "workload not found"))?;
        let key = work_key(workload);
        let expected_version = self.document_versions.get(id).copied();
        let transaction_id = runtime_id("work_tx");
        let aggregate_digest = hash(&serde_json::to_value(workload).map_err(io)?);
        let commit = self
            .db
            .apply_atomic_transaction_guarded(
                &transaction_id,
                None,
                None,
                &[],
                &[RecordPrecondition {
                    capability: DURABLE_WORK_COLLECTION.into(),
                    key: key.clone(),
                    require_absent: expected_version.is_none(),
                    expected_version,
                    expected_epoch: None,
                    expected_lease_id: None,
                }],
                &[AtomicMutation {
                    capability: DURABLE_WORK_COLLECTION.into(),
                    key,
                    value: Some(serde_json::to_value(workload).map_err(io)?),
                }],
                Some(serde_json::json!({
                    "kind": action,
                    "subject": workload.requested_by.id(),
                    "principal_id": workload.requested_by.id(),
                    "agent_id": (workload.requested_by.kind() == "agent").then(|| workload.requested_by.id()),
                    "session_id": workload.session_id,
                    "delegator_id": workload.authorization_snapshot.grant.issuer.id(),
                    "authorization_decision_id": workload.authorization_snapshot.grant.id,
                    "tenant": workload.tenant_id,
                    "application": workload.application_id,
                    "work_id": workload.workload_id,
                    "operation_id": transaction_id,
                    "attempt_id": workload.attempts.last().map(|attempt| attempt.attempt_id.as_str()),
                    "worker_id": workload.current_claim.as_ref().map(|claim| claim.worker_id.as_str()),
                    "result_id": workload.result.as_ref().map(|result| result.result_id.as_str()),
                    "source_kind": (action == "workload_migration").then_some("WorkloadStore"),
                    "source_digest": aggregate_digest,
                    "migration_outcome": (action == "workload_migration").then_some("imported"),
                })),
            )
            .map_err(workload_storage_error)?;
        let version = commit
            .rows
            .first()
            .and_then(|row| row.value.get("__version"))
            .and_then(Value::as_u64)
            .ok_or_else(|| io("canonical work commit has no document version"))?;
        self.document_versions.insert(id.into(), version);
        Ok(())
    }
    pub fn create(&mut self, input: CreateWorkload) -> Result<Workload, WorkloadError> {
        self.admit(input).map(|(workload, _)| workload)
    }

    /// Create a workload, or return the one this requester already created
    /// under the same key.
    ///
    /// The key is scoped to the tenant, the application **and the requester**,
    /// so one requester's key can never return, reveal or block another's
    /// workload. A repeated key is answered with the original workload only when
    /// the request is the same — definition, revision, environment, input and
    /// capability snapshot; otherwise it is refused with
    /// `IDEMPOTENCY_KEY_REUSED` and nothing is created. The requester's
    /// authorization snapshot is checked before any replay. The boolean is
    /// `true` when this call created the workload.
    pub fn admit(&mut self, input: CreateWorkload) -> Result<(Workload, bool), WorkloadError> {
        ensure_safe(&input.input)?;
        if input.idempotency_key.trim().is_empty() {
            return Err(WorkloadError::new(
                "WORKLOAD_INVALID",
                "idempotency key is required",
            ));
        }
        if input.authorization_snapshot.grant.subject != input.requested_by
            || input.authorization_snapshot.grant.tenant_id != input.tenant_id
            || input.authorization_snapshot.grant.application_id != input.application_id
        {
            return Err(WorkloadError::new(
                "WORKLOAD_UNAUTHORIZED",
                "authorization snapshot does not bind the requester",
            ));
        }
        let idempotency = idempotency_scope(
            &input.tenant_id,
            &input.application_id,
            &input.requested_by,
            &input.idempotency_key,
        );
        if let Some(id) = self.idempotency.get(&idempotency) {
            let existing = self.workloads[id].clone();
            return replay(existing, &input);
        }
        // The scoped idempotency identity is the record identity. Competing
        // admissions therefore contend on one canonical require-absent CAS
        // even when their payloads differ; no separate durable uniqueness
        // index is needed.
        let id = format!("workload_{}", &hex(&Sha256::digest(&idempotency))[..24]);
        let request = input.clone();
        let input_hash = hash(&input.input);
        let mut workload = Workload {
            contract_version: 1,
            workload_id: id.clone(),
            tenant_id: input.tenant_id,
            application_id: input.application_id,
            environment: input.environment,
            definition_id: input.definition_id,
            definition_revision: input.definition_revision,
            trigger: input.trigger,
            input: input.input,
            input_hash,
            requested_by: input.requested_by,
            session_id: input.session_id,
            authorization_snapshot: input.authorization_snapshot,
            capability_snapshot: input.capability_snapshot,
            connection_scopes: input.connection_scopes,
            priority: input.priority,
            idempotency_key: input.idempotency_key,
            created_at: input.created_at,
            updated_at: input.created_at,
            state: WorkloadState::Created,
            execution_history: vec![],
            attempts: vec![],
            checkpoints: vec![],
            result: None,
            retry_policy: input.retry_policy,
            timeouts: input.timeouts,
            attempt_count: 0,
            retry_count: 0,
            // Creation-time eligibility and retry backoff are the same thing, so
            // they are the same field. A workload with `not_before` is created
            // exactly as any other and is simply not yet claimable.
            next_attempt_at: input.not_before,
            last_failure: None,
            current_claim: None,
            fencing_token: 0,
            dependencies: input.dependencies,
        };
        // The durable history records the eligibility the workload was created
        // with, so "why was this Ready and unclaimed for an hour" is answerable
        // from the workload itself rather than from the caller's intent.
        let evidence = match workload.next_attempt_at {
            Some(instant) => [("not_before".into(), instant.to_string())].into(),
            None => BTreeMap::new(),
        };
        self.transition(
            &mut workload,
            WorkloadState::Ready,
            "rust",
            input.created_at,
            evidence,
        )?;
        let key = work_key(&workload);
        self.idempotency.insert(idempotency.clone(), id.clone());
        self.workloads.insert(id.clone(), workload.clone());
        if let Err(error) = self.persist_workload(&id, "workload.created") {
            self.workloads.remove(&id);
            self.idempotency.remove(&idempotency);
            // Another writer of the same store (a second process, or a replica)
            // admitted this identity first. Converge on what it committed.
            if error.code == "WORKLOAD_CONFLICT" {
                if let Some(existing) = self.reload_committed(&key)? {
                    self.idempotency.insert(idempotency, id);
                    return replay(existing, &request);
                }
            }
            return Err(error);
        }
        Ok((workload, true))
    }
    pub fn get(&self, id: &str) -> Result<&Workload, WorkloadError> {
        self.workloads
            .get(id)
            .ok_or_else(|| WorkloadError::new("WORKLOAD_NOT_FOUND", "workload not found"))
    }
    pub fn list(&self, tenant: &str, application: &str) -> Vec<Workload> {
        let mut values = self
            .workloads
            .values()
            .filter(|v| v.tenant_id == tenant && v.application_id == application)
            .cloned()
            .collect::<Vec<_>>();
        values.sort_by_key(|v| (std::cmp::Reverse(v.priority), v.created_at));
        values
    }
    fn edit<R>(
        &mut self,
        id: &str,
        action: impl FnOnce(&mut Self, &mut Workload) -> Result<R, WorkloadError>,
    ) -> Result<R, WorkloadError> {
        let workload = self
            .workloads
            .get(id)
            .cloned()
            .ok_or_else(|| WorkloadError::new("WORKLOAD_NOT_FOUND", "workload not found"))?;
        let mut edited = workload.clone();
        let result = action(self, &mut edited)?;
        self.workloads.insert(id.into(), edited);
        if let Err(error) = self.persist_workload(id, "workload.mutated") {
            self.workloads.insert(id.into(), workload.clone());
            // Another writer of the same store committed this workload first.
            // Nothing of this edit was committed; adopt what was, so this
            // projection does not keep acting on a version that no longer
            // exists (every later guarded commit from it would conflict).
            if error.code == "WORKLOAD_CONFLICT" {
                self.reload_committed(&work_key(&workload))?;
            }
            return Err(error);
        }
        Ok(result)
    }
    #[cfg(test)]
    pub(crate) fn db_for_tests(&self) -> FeltDb {
        self.db.clone()
    }
    /// Replace this projection's copy of a workload with the committed row.
    fn reload_committed(&mut self, key: &str) -> Result<Option<Workload>, WorkloadError> {
        let committed = self
            .db
            .query_collection(DURABLE_WORK_COLLECTION, Some(1), |row| row.key == key)
            .map_err(io)?;
        let Some(row) = committed.into_iter().next() else {
            return Ok(None);
        };
        let version = row
            .value
            .get("__version")
            .and_then(Value::as_u64)
            .unwrap_or(1);
        let workload: Workload = serde_json::from_value(row.value).map_err(io)?;
        if let Some(result) = &workload.result {
            self.result_ids
                .insert(result.result_id.clone(), workload.workload_id.clone());
        }
        self.document_versions
            .insert(workload.workload_id.clone(), version);
        self.workloads
            .insert(workload.workload_id.clone(), workload.clone());
        Ok(Some(workload))
    }
    fn transition(
        &mut self,
        w: &mut Workload,
        to: WorkloadState,
        actor: &str,
        at: i64,
        evidence: BTreeMap<String, String>,
    ) -> Result<(), WorkloadError> {
        if !legal(w.state, to) {
            return Err(WorkloadError::new(
                "WORKLOAD_TRANSITION_DENIED",
                format!("{:?} cannot transition to {:?}", w.state, to),
            ));
        }
        let from = w.state;
        w.state = to;
        w.updated_at = at;
        let sequence = w
            .execution_history
            .last()
            .map_or(1, |event| event.sequence + 1);
        w.execution_history.push(WorkloadEvent {
            sequence,
            event_type: event_name(to).into(),
            from: Some(from),
            to,
            actor: actor.into(),
            at,
            evidence,
        });
        Ok(())
    }
    pub fn claim(
        &mut self,
        id: &str,
        worker: &str,
        capabilities: &BTreeSet<String>,
        now: i64,
        lease_ms: u64,
    ) -> Result<WorkloadClaim, WorkloadError> {
        // A Ready workload that has already used its budget (an operator
        // re-arm beyond it, or a record left by unbounded recovery before it
        // was bounded) is dead-lettered here rather than claimed again.
        let exhausted = self
            .get(id)
            .is_ok_and(|w| w.state == WorkloadState::Ready && w.attempt_count >= attempt_budget(w));
        if exhausted {
            self.edit(id, |store, w| {
                let evidence = [
                    ("reason".into(), "attempt_budget_exhausted".into()),
                    ("attempts_used".into(), w.attempt_count.to_string()),
                    ("attempt_budget".into(), attempt_budget(w).to_string()),
                ]
                .into();
                store.transition(w, WorkloadState::DeadLetter, "rust.claim", now, evidence)
            })?;
            return Err(WorkloadError::new(
                "WORKLOAD_ATTEMPTS_EXHAUSTED",
                "workload has used its attempt budget",
            ));
        }
        self.edit(id, |store, w| {
            if w.state != WorkloadState::Ready {
                return Err(WorkloadError::new(
                    "WORKLOAD_NOT_READY",
                    "workload is not eligible",
                ));
            }
            // Separated from the state check because the two mean opposite
            // things to a worker: a workload in the wrong state may never become
            // claimable, while one that is merely early certainly will, at an
            // instant the error names. Collapsing them into one code left a
            // polling worker unable to tell "come back" from "give up".
            if !w.is_eligible_at(now) {
                return Err(WorkloadError::new(
                    "WORKLOAD_NOT_YET_ELIGIBLE",
                    format!(
                        "workload is not claimable until {}",
                        w.next_attempt_at.unwrap_or(now)
                    ),
                ));
            }
            if !w.capability_snapshot.is_subset(capabilities) {
                return Err(WorkloadError::new(
                    "WORKLOAD_CAPABILITY_DENIED",
                    "worker lacks required capability snapshot",
                ));
            }
            w.fencing_token += 1;
            w.attempt_count += 1;
            let seed = format!("{}:{}:{}", w.workload_id, w.fencing_token, worker);
            let claim = WorkloadClaim {
                claim_id: format!("claim_{}", &hex(&Sha256::digest(seed.as_bytes()))[..20]),
                workload_id: w.workload_id.clone(),
                worker_id: worker.into(),
                lease_id: format!("lease_{}", w.fencing_token),
                fencing_token: w.fencing_token,
                capability_snapshot: w.capability_snapshot.clone(),
                claimed_at: now,
                expires_at: now + (lease_ms / 1000) as i64,
                last_heartbeat: now,
            };
            w.attempts.push(WorkloadAttempt {
                attempt_id: format!("attempt_{}_{}", w.workload_id, w.attempt_count),
                workload_id: w.workload_id.clone(),
                worker_id: worker.into(),
                claim_id: claim.claim_id.clone(),
                fencing_token: claim.fencing_token,
                started_at: None,
                ended_at: None,
                input_hash: w.input_hash.clone(),
                result_hash: None,
                error: None,
                resource_usage: BTreeMap::new(),
                status: AttemptStatus::Claimed,
            });
            w.current_claim = Some(claim.clone());
            store.transition(
                w,
                WorkloadState::Claimed,
                worker,
                now,
                [("fencing_token".into(), claim.fencing_token.to_string())].into(),
            )?;
            Ok(claim)
        })
    }
    fn fence(
        w: &Workload,
        claim: &str,
        worker: &str,
        token: u64,
        now: i64,
    ) -> Result<(), WorkloadError> {
        let current = w
            .current_claim
            .as_ref()
            .ok_or_else(|| WorkloadError::new("WORKLOAD_CLAIM_INVALID", "no active claim"))?;
        if current.claim_id != claim
            || current.worker_id != worker
            || current.fencing_token != token
        {
            return Err(WorkloadError::new(
                "WORKLOAD_STALE_FENCE",
                "claim or fencing token is stale",
            ));
        }
        if current.expires_at <= now {
            return Err(WorkloadError::new(
                "WORKLOAD_LEASE_EXPIRED",
                "claim lease expired",
            ));
        }
        Ok(())
    }
    pub fn start(
        &mut self,
        id: &str,
        claim: &str,
        worker: &str,
        token: u64,
        now: i64,
    ) -> Result<(), WorkloadError> {
        self.edit(id, |store, w| {
            Self::fence(w, claim, worker, token, now)?;
            let attempt = w.attempts.last_mut().unwrap();
            attempt.status = AttemptStatus::Running;
            attempt.started_at = Some(now);
            store.transition(w, WorkloadState::Running, worker, now, BTreeMap::new())
        })
    }
    pub fn heartbeat(
        &mut self,
        id: &str,
        claim: &str,
        worker: &str,
        token: u64,
        now: i64,
        extend_ms: u64,
    ) -> Result<WorkloadClaim, WorkloadError> {
        self.edit(id, |_, w| {
            Self::fence(w, claim, worker, token, now)?;
            let current = w.current_claim.as_mut().unwrap();
            current.last_heartbeat = now;
            current.expires_at = now + (extend_ms / 1000) as i64;
            Ok(current.clone())
        })
    }
    pub fn execution_context(
        &self,
        id: &str,
        claim: &str,
        worker: &str,
        token: u64,
        now: i64,
        secret_references: Vec<String>,
    ) -> Result<WorkloadExecutionContext, WorkloadError> {
        let workload = self.get(id)?;
        Self::fence(workload, claim, worker, token, now)?;
        if secret_references
            .iter()
            .any(|v| !v.starts_with("secret://"))
        {
            return Err(WorkloadError::new(
                "WORKLOAD_SECRET_REJECTED",
                "execution context accepts secret references only",
            ));
        }
        let current = workload.current_claim.as_ref().unwrap();
        Ok(WorkloadExecutionContext {
            workload_id: id.into(),
            attempt_id: workload.attempts.last().unwrap().attempt_id.clone(),
            worker_id: worker.into(),
            capabilities: current.capability_snapshot.clone(),
            connection_scopes: workload.connection_scopes.clone(),
            secret_references,
            expires_at: current.expires_at,
            fencing_token: token,
        })
    }
    pub fn checkpoint(
        &mut self,
        id: &str,
        claim: &str,
        worker: &str,
        token: u64,
        state: Value,
        now: i64,
    ) -> Result<WorkloadCheckpoint, WorkloadError> {
        ensure_safe(&state)?;
        self.edit(id, |store, w| {
            Self::fence(w, claim, worker, token, now)?;
            if !matches!(
                w.state,
                WorkloadState::Running | WorkloadState::Checkpointed
            ) {
                return Err(WorkloadError::new(
                    "WORKLOAD_TRANSITION_DENIED",
                    "checkpoint requires a running workload",
                ));
            }
            let attempt = w.attempts.last_mut().unwrap();
            attempt.status = AttemptStatus::Checkpointed;
            let sequence = w.checkpoints.len() as u64 + 1;
            let checkpoint = WorkloadCheckpoint {
                checkpoint_id: format!("checkpoint_{}_{}", w.workload_id, sequence),
                workload_id: w.workload_id.clone(),
                attempt_id: attempt.attempt_id.clone(),
                sequence,
                state_hash: hash(&state),
                state,
                created_at: now,
                produced_by: worker.into(),
            };
            w.checkpoints.push(checkpoint.clone());
            if w.state == WorkloadState::Running {
                store.transition(w, WorkloadState::Checkpointed, worker, now, BTreeMap::new())?;
            }
            Ok(checkpoint)
        })
    }
    pub fn complete(
        &mut self,
        id: &str,
        claim: &str,
        worker: &str,
        token: u64,
        result_id: &str,
        output: Value,
        artifacts: Vec<String>,
        schema_version: u64,
        now: i64,
    ) -> Result<WorkloadResult, WorkloadError> {
        ensure_safe(&output)?;
        if let Some(workload_id) = self.result_ids.get(result_id) {
            if workload_id == id {
                return Ok(self.get(id)?.result.clone().unwrap());
            }
            return Err(WorkloadError::new(
                "WORKLOAD_RESULT_DUPLICATE",
                "result id belongs to another workload",
            ));
        }
        let result = self.edit(id, |store, w| {
            Self::fence(w, claim, worker, token, now)?;
            if !matches!(
                w.state,
                WorkloadState::Running | WorkloadState::Checkpointed
            ) {
                return Err(WorkloadError::new(
                    "WORKLOAD_TRANSITION_DENIED",
                    "completion requires running state",
                ));
            }
            let attempt = w.attempts.last_mut().unwrap();
            let output_hash = hash(&output);
            attempt.status = AttemptStatus::Succeeded;
            attempt.ended_at = Some(now);
            attempt.result_hash = Some(output_hash.clone());
            let result = WorkloadResult {
                result_id: result_id.into(),
                workload_id: w.workload_id.clone(),
                attempt_id: attempt.attempt_id.clone(),
                status: "succeeded".into(),
                output,
                output_hash,
                artifacts,
                provenance: [
                    ("claim_id".into(), claim.into()),
                    ("fencing_token".into(), token.to_string()),
                ]
                .into(),
                produced_by: worker.into(),
                created_at: now,
                schema_version,
            };
            w.result = Some(result.clone());
            w.current_claim = None;
            store.transition(w, WorkloadState::Succeeded, worker, now, BTreeMap::new())?;
            Ok(result)
        })?;
        self.result_ids.insert(result_id.into(), id.into());
        Ok(result)
    }
    pub fn fail(
        &mut self,
        id: &str,
        claim: &str,
        worker: &str,
        token: u64,
        kind: &str,
        error: &str,
        now: i64,
    ) -> Result<WorkloadState, WorkloadError> {
        if sensitive_text(error) {
            return Err(WorkloadError::new(
                "WORKLOAD_SECRET_REJECTED",
                "errors may not contain secret material",
            ));
        }
        self.edit(id, |store, w| {
            Self::fence(w, claim, worker, token, now)?;
            let attempt = w.attempts.last_mut().unwrap();
            attempt.status = AttemptStatus::Failed;
            attempt.ended_at = Some(now);
            attempt.error = Some(error.into());
            w.last_failure = Some(format!("{kind}: {error}"));
            w.current_claim = None;
            store.transition(
                w,
                WorkloadState::Failed,
                worker,
                now,
                [("failure_kind".into(), kind.into())].into(),
            )?;
            let retryable =
                w.retry_policy.retry_on.contains(kind) && w.attempt_count < attempt_budget(w);
            if retryable {
                w.retry_count += 1;
                let multiplier = if w.retry_policy.backoff == BackoffPolicy::Exponential {
                    2u64.saturating_pow(w.retry_count.saturating_sub(1))
                } else {
                    1
                };
                let delay = w
                    .retry_policy
                    .initial_delay_ms
                    .saturating_mul(multiplier)
                    .min(w.retry_policy.max_delay_ms);
                w.next_attempt_at = Some(now + (delay / 1000) as i64);
                store.transition(w, WorkloadState::Ready, "rust.retry", now, BTreeMap::new())?;
            } else {
                store.transition(
                    w,
                    WorkloadState::DeadLetter,
                    "rust.retry",
                    now,
                    BTreeMap::new(),
                )?;
            }
            Ok(w.state)
        })
    }
    pub fn cancel(
        &mut self,
        id: &str,
        actor: &str,
        now: i64,
    ) -> Result<WorkloadState, WorkloadError> {
        self.edit(id, |store, w| {
            match w.state {
                WorkloadState::Ready
                | WorkloadState::Created
                | WorkloadState::Failed
                | WorkloadState::DeadLetter => {
                    w.current_claim = None;
                    store.transition(w, WorkloadState::Cancelled, actor, now, BTreeMap::new())?
                }
                WorkloadState::Claimed | WorkloadState::Running | WorkloadState::Checkpointed => {
                    store.transition(
                        w,
                        WorkloadState::CancellationRequested,
                        actor,
                        now,
                        BTreeMap::new(),
                    )?
                }
                WorkloadState::CancellationRequested => {}
                _ => {
                    return Err(WorkloadError::new(
                        "WORKLOAD_TRANSITION_DENIED",
                        "terminal workload cannot be cancelled",
                    ))
                }
            }
            Ok(w.state)
        })
    }
    pub fn confirm_cancel(
        &mut self,
        id: &str,
        claim: &str,
        worker: &str,
        token: u64,
        now: i64,
    ) -> Result<(), WorkloadError> {
        self.edit(id, |store, w| {
            Self::fence(w, claim, worker, token, now)?;
            if w.state != WorkloadState::CancellationRequested {
                return Err(WorkloadError::new(
                    "WORKLOAD_TRANSITION_DENIED",
                    "cancellation was not requested",
                ));
            }
            if let Some(attempt) = w.attempts.last_mut() {
                attempt.status = AttemptStatus::Cancelled;
                attempt.ended_at = Some(now);
            }
            w.current_claim = None;
            store.transition(w, WorkloadState::Cancelled, worker, now, BTreeMap::new())
        })
    }
    pub fn timeout(
        &mut self,
        id: &str,
        actor: &str,
        now: i64,
    ) -> Result<WorkloadState, WorkloadError> {
        self.edit(id, |store, w| {
            if !matches!(
                w.state,
                WorkloadState::Ready
                    | WorkloadState::Claimed
                    | WorkloadState::Running
                    | WorkloadState::Checkpointed
            ) {
                return Err(WorkloadError::new(
                    "WORKLOAD_TRANSITION_DENIED",
                    "workload cannot time out in its current state",
                ));
            }
            if let Some(attempt) = w.attempts.last_mut() {
                attempt.status = AttemptStatus::TimedOut;
                attempt.ended_at = Some(now);
            }
            w.current_claim = None;
            store.transition(w, WorkloadState::TimedOut, actor, now, BTreeMap::new())?;
            if w.attempt_count < attempt_budget(w) && w.retry_policy.retry_on.contains("timeout")
            {
                w.retry_count += 1;
                w.next_attempt_at = Some(now + (w.retry_policy.initial_delay_ms / 1000) as i64);
                store.transition(w, WorkloadState::Ready, "rust.retry", now, BTreeMap::new())?;
            } else {
                store.transition(
                    w,
                    WorkloadState::DeadLetter,
                    "rust.timeout",
                    now,
                    BTreeMap::new(),
                )?;
            }
            Ok(w.state)
        })
    }
    pub fn retry(&mut self, id: &str, actor: &str, now: i64) -> Result<(), WorkloadError> {
        self.retry_with_reason(id, actor, now, None)
    }
    /// An operator's re-arm of failed, dead-lettered or timed-out work.
    ///
    /// Each re-arm grants one more attempt, as it always has, and is recorded
    /// in the history with its actor and reason. A workload may be re-armed at
    /// most `retry_policy.max_attempts` times, so operator retries cannot
    /// become an unbounded loop.
    pub fn retry_with_reason(
        &mut self,
        id: &str,
        actor: &str,
        now: i64,
        reason: Option<&str>,
    ) -> Result<(), WorkloadError> {
        self.edit(id, |store, w| {
            if !matches!(
                w.state,
                WorkloadState::Failed | WorkloadState::DeadLetter | WorkloadState::TimedOut
            ) {
                return Err(WorkloadError::new(
                    "WORKLOAD_TRANSITION_DENIED",
                    "workload is not retryable",
                ));
            }
            let previous = operator_retries(w);
            if previous >= w.retry_policy.max_attempts {
                return Err(WorkloadError::new(
                    "WORKLOAD_RETRY_EXHAUSTED",
                    "workload has used every operator retry its policy allows",
                ));
            }
            w.next_attempt_at = Some(now);
            w.current_claim = None;
            let mut evidence: BTreeMap<String, String> = [
                ("retry".into(), "operator".into()),
                ("operator_retry".into(), (previous + 1).to_string()),
                (
                    "attempt_budget".into(),
                    w.retry_policy
                        .max_attempts
                        .saturating_add(previous + 1)
                        .to_string(),
                ),
            ]
            .into();
            if let Some(reason) = reason.filter(|reason| !reason.trim().is_empty()) {
                evidence.insert("reason".into(), reason.chars().take(256).collect());
            }
            store.transition(w, WorkloadState::Ready, actor, now, evidence)
        })
    }
    pub fn recover_expired(&mut self, now: i64) -> Result<Vec<String>, WorkloadError> {
        let ids = self
            .workloads
            .values()
            .filter(|w| {
                w.current_claim
                    .as_ref()
                    .is_some_and(|c| c.expires_at <= now)
                    && matches!(
                        w.state,
                        WorkloadState::Claimed
                            | WorkloadState::Running
                            | WorkloadState::Checkpointed
                            | WorkloadState::CancellationRequested
                    )
            })
            .map(|w| w.workload_id.clone())
            .collect::<Vec<_>>();
        let mut recovered = Vec::with_capacity(ids.len());
        for id in ids {
            let outcome = self.edit(&id, |store, w| {
                let lost = w.current_claim.take();
                if let Some(attempt) = w.attempts.last_mut() {
                    attempt.status = AttemptStatus::WorkerLost;
                    attempt.ended_at = Some(now);
                    attempt.error = Some("worker_lost: claim lease expired".into());
                }
                let mut evidence: BTreeMap<String, String> = [
                    ("recovery_reason".into(), "lease_expired".into()),
                    ("attempts_used".into(), w.attempt_count.to_string()),
                    ("attempt_budget".into(), attempt_budget(w).to_string()),
                ]
                .into();
                if let Some(claim) = &lost {
                    evidence.insert("claim_id".into(), claim.claim_id.clone());
                    evidence.insert("worker_id".into(), claim.worker_id.clone());
                    evidence.insert("lease_expired_at".into(), claim.expires_at.to_string());
                }
                if let Some(attempt) = w.attempts.last() {
                    evidence.insert("attempt_id".into(), attempt.attempt_id.clone());
                }
                if w.state == WorkloadState::CancellationRequested {
                    store.transition(w, WorkloadState::Cancelled, "rust.recovery", now, evidence)?;
                } else {
                    store.transition(
                        w,
                        WorkloadState::Recovering,
                        "rust.recovery",
                        now,
                        evidence,
                    )?;
                    // A lost worker is the policy's `worker_failure`. Whether
                    // the lost attempt had effects is unknown to FeltDB, so a
                    // retry re-executes it: at-least-once, never exactly-once.
                    let (to, decision) = if !w.retry_policy.retry_on.contains("worker_failure") {
                        (WorkloadState::DeadLetter, "worker_failure_not_retryable")
                    } else if w.attempt_count >= attempt_budget(w) {
                        (WorkloadState::DeadLetter, "attempt_budget_exhausted")
                    } else {
                        (WorkloadState::Ready, "retry")
                    };
                    store.transition(
                        w,
                        to,
                        "rust.recovery",
                        now,
                        [("decision".into(), decision.into())].into(),
                    )?;
                }
                Ok(())
            });
            match outcome {
                Ok(()) => recovered.push(id),
                // Another writer of this store committed the workload first —
                // in a race, its own recovery. Nothing of this attempt was
                // committed and `edit` has adopted the committed workload, so
                // the attempt is recovered once, by that writer. If it still
                // needs recovery, the next pass sees it.
                Err(error) if error.code == "WORKLOAD_CONFLICT" => {}
                Err(error) => return Err(error),
            }
        }
        Ok(recovered)
    }
}

pub trait WorkScheduler {
    fn enqueue(&mut self, workload_id: &str);
    fn claim(
        &mut self,
        store: &mut WorkloadStore,
        tenant: &str,
        application: &str,
        worker: &str,
        capabilities: &BTreeSet<String>,
        now: i64,
        lease_ms: u64,
    ) -> Result<Option<WorkloadClaim>, WorkloadError>;
    fn release(&mut self, claim_id: &str);
}
#[derive(Default)]
pub struct LocalDurableScheduler {
    queue: BTreeSet<String>,
}
impl WorkScheduler for LocalDurableScheduler {
    fn enqueue(&mut self, id: &str) {
        self.queue.insert(id.into());
    }
    fn claim(
        &mut self,
        store: &mut WorkloadStore,
        tenant: &str,
        application: &str,
        worker: &str,
        capabilities: &BTreeSet<String>,
        now: i64,
        lease_ms: u64,
    ) -> Result<Option<WorkloadClaim>, WorkloadError> {
        let selected = store
            .list(tenant, application)
            .into_iter()
            .filter(|w| {
                // Eligibility is part of selection, not a reason to fail. A
                // workload that is merely early is skipped so the scheduler can
                // hand out work that is ready now; without this it would pick an
                // ineligible workload, have `store.claim` refuse it, and return
                // an error while claimable work sat behind it.
                w.state == WorkloadState::Ready
                    && w.is_eligible_at(now)
                    && w.capability_snapshot.is_subset(capabilities)
            })
            .find(|w| {
                w.dependencies.iter().all(|d| {
                    store
                        .workloads
                        .get(&d.workload_id)
                        .is_some_and(|dependency| dependency.state == d.required_state)
                })
            })
            .map(|w| w.workload_id);
        match selected {
            Some(id) => store
                .claim(&id, worker, capabilities, now, lease_ms)
                .map(Some),
            None => Ok(None),
        }
    }
    fn release(&mut self, _: &str) {}
}
fn legal(from: WorkloadState, to: WorkloadState) -> bool {
    use WorkloadState::*;
    matches!(
        (from, to),
        (Created, Ready)
            | (Ready, Claimed)
            | (Claimed, Running)
            | (Running, Checkpointed)
            | (Checkpointed, Checkpointed)
            | (Running, Succeeded)
            | (Checkpointed, Succeeded)
            | (Claimed, CancellationRequested)
            | (Running, CancellationRequested)
            | (Checkpointed, CancellationRequested)
            | (CancellationRequested, Cancelled)
            | (Ready, Cancelled)
            | (Created, Cancelled)
            | (Failed, Cancelled)
            | (DeadLetter, Cancelled)
            | (Running, Failed)
            | (Checkpointed, Failed)
            | (Claimed, Recovering)
            | (Running, Recovering)
            | (Checkpointed, Recovering)
            | (Recovering, Ready)
            | (Failed, Ready)
            | (TimedOut, Ready)
            | (DeadLetter, Ready)
            | (Failed, DeadLetter)
            | (Running, TimedOut)
            | (Claimed, TimedOut)
            | (Checkpointed, TimedOut)
            | (Ready, TimedOut)
            | (TimedOut, DeadLetter)
            | (Recovering, DeadLetter)
            | (Ready, DeadLetter)
    )
}
pub fn can_transition(from: WorkloadState, to: WorkloadState) -> bool {
    legal(from, to)
}
fn event_name(state: WorkloadState) -> &'static str {
    use WorkloadState::*;
    match state {
        Created => "workload.created",
        Ready => "workload.ready",
        Claimed => "workload.claimed",
        Running => "workload.started",
        Checkpointed => "workload.checkpointed",
        Recovering => "workload.recovered",
        CancellationRequested => "workload.cancellation_requested",
        Succeeded => "workload.completed",
        Failed => "workload.failed",
        Cancelled => "workload.cancelled",
        TimedOut => "workload.timed_out",
        DeadLetter => "workload.dead_lettered",
    }
}
fn ensure_safe(value: &Value) -> Result<(), WorkloadError> {
    fn walk(value: &Value) -> bool {
        match value {
            Value::Object(map) => map.iter().any(|(k, v)| {
                matches!(
                    k.to_ascii_lowercase().as_str(),
                    "password" | "secret" | "token" | "api_key" | "private_key"
                ) || walk(v)
            }),
            Value::Array(values) => values.iter().any(walk),
            Value::String(v) => sensitive_text(v),
            _ => false,
        }
    }
    if walk(value) {
        Err(WorkloadError::new(
            "WORKLOAD_SECRET_REJECTED",
            "plaintext secrets are forbidden in workload state",
        ))
    } else {
        Ok(())
    }
}
fn sensitive_text(v: &str) -> bool {
    v.contains("BEGIN PRIVATE KEY") || v.starts_with("sk-") || v.starts_with("fdb_live_")
}
/// The identity a caller's idempotency key is unique within: tenant,
/// application and requester. Encoded as a JSON array, so no choice of key or
/// identifier can make two different scopes coincide.
fn idempotency_scope(tenant: &str, application: &str, requester: &Subject, key: &str) -> String {
    serde_json::json!([tenant, application, requester.kind(), requester.id(), key]).to_string()
}
/// The scope before keys were bound to their requester, kept only to verify
/// legacy store files during migration.
fn legacy_idempotency_scope(
    tenant: &str,
    application: &str,
    _requester: &Subject,
    key: &str,
) -> String {
    format!("{tenant}:{application}:{key}")
}
fn idempotency_index(
    workloads: &BTreeMap<String, Workload>,
    scope: fn(&str, &str, &Subject, &str) -> String,
) -> BTreeMap<String, String> {
    workloads
        .values()
        .map(|w| {
            (
                scope(&w.tenant_id, &w.application_id, &w.requested_by, &w.idempotency_key),
                w.workload_id.clone(),
            )
        })
        .collect()
}
/// Answer a repeated key: the original workload for the same request, a
/// refusal for a different one. Never the original for a different request.
fn replay(existing: Workload, input: &CreateWorkload) -> Result<(Workload, bool), WorkloadError> {
    let same = existing.requested_by == input.requested_by
        && existing.definition_id == input.definition_id
        && existing.definition_revision == input.definition_revision
        && existing.environment == input.environment
        && existing.input_hash == hash(&input.input)
        && existing.capability_snapshot == input.capability_snapshot;
    if same {
        Ok((existing, false))
    } else {
        Err(WorkloadError::new(
            "IDEMPOTENCY_KEY_REUSED",
            "idempotency key was already used for a different request",
        ))
    }
}
/// Operator re-arms recorded in the workload's own history.
fn operator_retries(w: &Workload) -> u32 {
    w.execution_history
        .iter()
        .filter(|event| event.evidence.get("retry").map(String::as_str) == Some("operator"))
        .count() as u32
}
/// How many claims the workload may have in total: its policy's
/// `max_attempts`, plus one for each operator re-arm (which is what an operator
/// retry has always granted).
fn attempt_budget(w: &Workload) -> u32 {
    w.retry_policy
        .max_attempts
        .saturating_add(operator_retries(w))
}
fn work_key(workload: &Workload) -> String {
    format!(
        "{}/{}/{}",
        workload.tenant_id, workload.application_id, workload.workload_id
    )
}
fn derived_event_sequence(workloads: &BTreeMap<String, Workload>) -> u64 {
    workloads
        .values()
        .flat_map(|workload| workload.execution_history.iter())
        .map(|event| event.sequence)
        .max()
        .unwrap_or_default()
}
fn runtime_id(prefix: &str) -> String {
    let value: String = rand::thread_rng()
        .sample_iter(&Alphanumeric)
        .take(24)
        .map(char::from)
        .collect();
    format!("{prefix}_{value}")
}
fn io(e: impl std::fmt::Display) -> WorkloadError {
    WorkloadError::new("WORKLOAD_STORAGE_FAILURE", e.to_string())
}
fn workload_storage_error(error: FlowError) -> WorkloadError {
    match error {
        FlowError::PreconditionFailed(_) | FlowError::RevisionConflict { .. } => {
            WorkloadError::new("WORKLOAD_CONFLICT", error.to_string())
        }
        FlowError::CapabilityError(ref message) if message.starts_with("PRECONDITION_FAILED:") => {
            WorkloadError::new("WORKLOAD_CONFLICT", error.to_string())
        }
        _ => io(error),
    }
}
fn hash(value: &Value) -> String {
    hex(&Sha256::digest(
        serde_json::to_vec(value).unwrap_or_default(),
    ))
}
fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|v| format!("{v:02x}")).collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::authorization::{
        Grant, GrantConstraints, GrantSigner, GrantStatus, ResourceScope, ResourceScopeKind,
    };
    use std::{fs, path::PathBuf};
    fn create() -> CreateWorkload {
        create_as("a1")
    }
    /// The fixture request, made by agent `agent`.
    fn create_as(agent: &str) -> CreateWorkload {
        let signer = GrantSigner::new("k", vec![1; 32]).unwrap();
        let subject = Subject::Agent(agent.into());
        let grant = Grant {
            contract_version: 1,
            id: "g".into(),
            issuer: Subject::Human("h".into()),
            subject: subject.clone(),
            tenant_id: "t".into(),
            application_id: "app".into(),
            capabilities: ["workload:execute".into()].into(),
            resources: vec![ResourceScope {
                kind: ResourceScopeKind::Workload,
                uri: "flow://app/workloads/*".into(),
            }],
            constraints: GrantConstraints::default(),
            issued_at: 1,
            expires_at: 9999,
            parent_grant: None,
            revision: 1,
            status: GrantStatus::Active,
        };
        CreateWorkload {
            tenant_id: "t".into(),
            application_id: "app".into(),
            environment: "production".into(),
            definition_id: "send".into(),
            definition_revision: "r1".into(),
            trigger: "manual".into(),
            input: serde_json::json!({"id":1}),
            requested_by: subject,
            session_id: Some("session-1".into()),
            authorization_snapshot: signer.sign(grant).unwrap(),
            capability_snapshot: ["email:send".into()].into(),
            connection_scopes: BTreeSet::new(),
            priority: 0,
            idempotency_key: "one".into(),
            retry_policy: Default::default(),
            timeouts: Default::default(),
            dependencies: vec![],
            not_before: None,
            created_at: 10,
        }
    }
    fn capabilities() -> BTreeSet<String> {
        ["email:send".into()].into()
    }

    #[test]
    fn idempotency_keys_are_bound_to_their_requester_and_request() {
        let (path, mut store) = store("requester-scoped-keys");
        let (original, created) = store.admit(create_as("a1")).unwrap();
        assert!(created);

        // The same requester and request: the original, not a second workload.
        let (replayed, created) = store.admit(create_as("a1")).unwrap();
        assert!(!created);
        assert_eq!(replayed.workload_id, original.workload_id);

        // The same key for a different request is refused and creates nothing.
        let mut different = create_as("a1");
        different.input = serde_json::json!({"id": 2});
        assert_eq!(store.admit(different).unwrap_err().code, "IDEMPOTENCY_KEY_REUSED");
        let mut other_definition = create_as("a1");
        other_definition.definition_id = "other".into();
        assert_eq!(
            store.admit(other_definition).unwrap_err().code,
            "IDEMPOTENCY_KEY_REUSED"
        );

        // Another requester's identical key is its own workload: it neither
        // returns, reveals nor blocks the first requester's.
        let (theirs, created) = store.admit(create_as("a2")).unwrap();
        assert!(created);
        assert_ne!(theirs.workload_id, original.workload_id);
        assert_eq!(store.workloads.len(), 2);

        // A snapshot that does not bind the requester is refused before any
        // replay, so a key cannot be used to fetch a workload.
        let mut unbound = create_as("a1");
        unbound.requested_by = Subject::Agent("a2".into());
        assert_eq!(store.admit(unbound).unwrap_err().code, "WORKLOAD_UNAUTHORIZED");
        let _ = fs::remove_file(path);
    }

    #[test]
    fn lease_recovery_records_the_lost_worker_and_is_bounded_by_the_attempt_budget() {
        let (path, mut store) = store("bounded-recovery");
        let workload = store.create(create()).unwrap();
        let id = workload.workload_id.clone();
        for attempt in 1..=3u32 {
            let now = 100 * attempt as i64;
            let claim = store.claim(&id, "worker-a", &capabilities(), now, 10_000).unwrap();
            assert_eq!(store.recover_expired(claim.expires_at).unwrap(), vec![id.clone()]);
            let recovered = store.get(&id).unwrap();
            let lost = recovered.attempts.last().unwrap();
            assert_eq!(lost.status, AttemptStatus::WorkerLost);
            assert_eq!(lost.claim_id, claim.claim_id);
            let recovering = recovered
                .execution_history
                .iter()
                .rev()
                .find(|event| event.to == WorkloadState::Recovering)
                .unwrap();
            assert_eq!(recovering.evidence["recovery_reason"], "lease_expired");
            assert_eq!(recovering.evidence["worker_id"], "worker-a");
            assert_eq!(recovering.evidence["claim_id"], claim.claim_id);
            assert_eq!(recovering.evidence["attempts_used"], attempt.to_string());
            let decision = &recovered.execution_history.last().unwrap();
            if attempt < 3 {
                assert_eq!(recovered.state, WorkloadState::Ready);
                assert_eq!(decision.evidence["decision"], "retry");
            } else {
                assert_eq!(recovered.state, WorkloadState::DeadLetter);
                assert_eq!(decision.evidence["decision"], "attempt_budget_exhausted");
            }
        }
        assert_eq!(store.get(&id).unwrap().attempt_count, 3);
        assert_eq!(
            store.claim(&id, "worker-b", &capabilities(), 1_000, 10_000).unwrap_err().code,
            "WORKLOAD_NOT_READY"
        );
        let _ = fs::remove_file(path);
    }

    /// The recovery history of `id` as committed, read by a fresh projection.
    fn committed_workload(db: &FeltDb, id: &str) -> Workload {
        WorkloadStore::from_db(db.clone()).unwrap().get(id).unwrap().clone()
    }

    fn recoveries(workload: &Workload) -> usize {
        workload
            .execution_history
            .iter()
            .filter(|event| event.to == WorkloadState::Recovering)
            .count()
    }

    /// Two writers of one store recover the same expired workload at once.
    fn race_recovery(db: &FeltDb, now: i64) -> (WorkloadStore, WorkloadStore, Vec<Vec<String>>) {
        let stores = [
            WorkloadStore::from_db(db.clone()).unwrap(),
            WorkloadStore::from_db(db.clone()).unwrap(),
        ];
        let barrier = std::sync::Barrier::new(2);
        let results = std::thread::scope(|scope| {
            let handles = stores
                .into_iter()
                .map(|mut store| {
                    let barrier = &barrier;
                    scope.spawn(move || {
                        barrier.wait();
                        let recovered = store.recover_expired(now).unwrap();
                        (store, recovered)
                    })
                })
                .collect::<Vec<_>>();
            handles
                .into_iter()
                .map(|handle| handle.join().unwrap())
                .collect::<Vec<_>>()
        });
        let mut results = results.into_iter();
        let (first, first_recovered) = results.next().unwrap();
        let (second, second_recovered) = results.next().unwrap();
        (first, second, vec![first_recovered, second_recovered])
    }

    #[test]
    fn concurrent_recovery_commits_one_transition_and_the_loser_converges() {
        for round in 0..20 {
            let (path, mut store) = store(&format!("concurrent-recovery-{round}"));
            let db = store.db.clone();
            let id = store.create(create()).unwrap().workload_id;
            let claim = store.claim(&id, "worker-a", &capabilities(), 100, 10_000).unwrap();
            // A writer that loaded the workload before either recovery.
            let mut stale = WorkloadStore::from_db(db.clone()).unwrap();

            let (mut first, mut second, recovered) = race_recovery(&db, claim.expires_at);
            // Exactly one writer committed the recovery; the other reported none.
            let mut outcomes = recovered.clone();
            outcomes.sort();
            assert_eq!(outcomes, vec![vec![], vec![id.clone()]], "round {round}");

            let committed = committed_workload(&db, &id);
            assert_eq!(recoveries(&committed), 1, "one recovery transition");
            assert_eq!(committed.state, WorkloadState::Ready);
            assert_eq!(committed.attempt_count, 1);
            assert_eq!(committed.attempts.len(), 1);
            assert_eq!(committed.attempts[0].status, AttemptStatus::WorkerLost);
            assert!(committed.current_claim.is_none());
            let recovering = committed
                .execution_history
                .iter()
                .find(|event| event.to == WorkloadState::Recovering)
                .unwrap();
            assert_eq!(recovering.evidence["claim_id"], claim.claim_id);
            assert_eq!(recovering.evidence["attempts_used"], "1");

            // The losing writer adopted the committed workload instead of
            // keeping its own, uncommitted view.
            let expected = serde_json::to_value(&committed).unwrap();
            assert_eq!(serde_json::to_value(first.get(&id).unwrap()).unwrap(), expected);
            assert_eq!(serde_json::to_value(second.get(&id).unwrap()).unwrap(), expected);
            // And neither recovers it again, or commits anything more.
            assert!(first.recover_expired(claim.expires_at + 1).unwrap().is_empty());
            assert!(second.recover_expired(claim.expires_at + 1).unwrap().is_empty());
            assert_eq!(
                serde_json::to_value(committed_workload(&db, &id)).unwrap(),
                expected,
                "no further commit"
            );

            // The lost claimant cannot complete, even through a projection
            // that still shows its claim as current and unexpired: the guarded
            // commit refuses the stale version.
            let refused = stale
                .start(&id, &claim.claim_id, "worker-a", claim.fencing_token, claim.expires_at - 1)
                .unwrap_err();
            assert_eq!(refused.code, "WORKLOAD_CONFLICT");
            assert_eq!(stale.get(&id).unwrap().state, WorkloadState::Ready);
            let refused = stale
                .complete(
                    &id,
                    &claim.claim_id,
                    "worker-a",
                    claim.fencing_token,
                    "late-result",
                    serde_json::json!({"late": true}),
                    vec![],
                    1,
                    claim.expires_at - 1,
                )
                .unwrap_err();
            assert_eq!(refused.code, "WORKLOAD_CLAIM_INVALID");
            // Through a current projection its fence no longer matches.
            let refused = first
                .heartbeat(&id, &claim.claim_id, "worker-a", claim.fencing_token, claim.expires_at - 1, 10_000)
                .unwrap_err();
            assert_eq!(refused.code, "WORKLOAD_CLAIM_INVALID");
            assert_eq!(serde_json::to_value(committed_workload(&db, &id)).unwrap(), expected);
            drop((store, first, second, stale, db));
            let _ = fs::remove_file(path);
        }
    }

    #[test]
    fn concurrent_recovery_never_exceeds_the_attempt_budget() {
        let (path, mut store) = store("concurrent-recovery-budget");
        let db = store.db.clone();
        let id = store.create(create()).unwrap().workload_id;
        for attempt in 1..=3i64 {
            // Each attempt is claimed by an up-to-date writer and lost.
            let mut claimer = WorkloadStore::from_db(db.clone()).unwrap();
            let claim = claimer
                .claim(&id, "worker-a", &capabilities(), 100 * attempt, 10_000)
                .unwrap();
            let (_, _, recovered) = race_recovery(&db, claim.expires_at);
            assert_eq!(recovered.concat(), vec![id.clone()], "attempt {attempt}");
            let committed = committed_workload(&db, &id);
            assert_eq!(recoveries(&committed), attempt as usize);
            assert_eq!(committed.attempt_count, attempt as u32);
        }
        let committed = committed_workload(&db, &id);
        assert_eq!(committed.state, WorkloadState::DeadLetter);
        assert_eq!(
            committed.execution_history.last().unwrap().evidence["decision"],
            "attempt_budget_exhausted"
        );
        assert!(committed
            .attempts
            .iter()
            .all(|attempt| attempt.status == AttemptStatus::WorkerLost));
        let refused = WorkloadStore::from_db(db.clone())
            .unwrap()
            .claim(&id, "worker-b", &capabilities(), 10_000, 10_000)
            .unwrap_err();
        assert_eq!(refused.code, "WORKLOAD_NOT_READY");
        assert_eq!(committed_workload(&db, &id).attempt_count, 3);
        drop((store, db));
        let _ = fs::remove_file(path);
    }

    #[test]
    fn a_policy_that_does_not_retry_worker_failure_dead_letters_a_lost_attempt() {
        let (path, mut store) = store("no-worker-failure-retry");
        let mut request = create();
        request.retry_policy.retry_on = ["timeout".into()].into();
        let id = store.create(request).unwrap().workload_id;
        let claim = store.claim(&id, "worker-a", &capabilities(), 100, 10_000).unwrap();
        store.recover_expired(claim.expires_at).unwrap();
        let workload = store.get(&id).unwrap();
        assert_eq!(workload.state, WorkloadState::DeadLetter);
        assert_eq!(
            workload.execution_history.last().unwrap().evidence["decision"],
            "worker_failure_not_retryable"
        );
        let _ = fs::remove_file(path);
    }

    #[test]
    fn claiming_exhausted_ready_work_dead_letters_it_instead() {
        let (path, mut store) = store("exhausted-claim");
        let id = store.create(create()).unwrap().workload_id;
        // What unbounded recovery used to leave behind: Ready, budget spent.
        let mut legacy = store.get(&id).unwrap().clone();
        legacy.attempt_count = 7;
        store.workloads.insert(id.clone(), legacy);
        store.persist_workload(&id, "fixture.legacy").unwrap();
        let refused = store.claim(&id, "worker-a", &capabilities(), 100, 10_000).unwrap_err();
        assert_eq!(refused.code, "WORKLOAD_ATTEMPTS_EXHAUSTED");
        let workload = store.get(&id).unwrap();
        assert_eq!(workload.state, WorkloadState::DeadLetter);
        assert_eq!(
            workload.execution_history.last().unwrap().evidence["reason"],
            "attempt_budget_exhausted"
        );
        // Durable: a restart sees the same outcome.
        let reopened = WorkloadStore::from_db(store.db.clone()).unwrap();
        assert_eq!(reopened.get(&id).unwrap().state, WorkloadState::DeadLetter);
        let _ = fs::remove_file(path);
    }

    #[test]
    fn operator_retry_grants_one_attempt_is_recorded_and_is_bounded() {
        let (path, mut store) = store("operator-retry");
        let id = store.create(create()).unwrap().workload_id;
        // Claim and lose leases until the workload stops being claimable.
        fn exhaust(store: &mut WorkloadStore, id: &str, now: &mut i64) {
            loop {
                *now += 100;
                match store.claim(id, "worker-a", &capabilities(), *now, 10_000) {
                    Ok(claim) => {
                        store.recover_expired(claim.expires_at).unwrap();
                    }
                    Err(error) => {
                        assert!(
                            matches!(
                                error.code.as_str(),
                                "WORKLOAD_NOT_READY" | "WORKLOAD_ATTEMPTS_EXHAUSTED"
                            ),
                            "{error:?}"
                        );
                        return;
                    }
                }
            }
        }
        let mut now = 100;
        exhaust(&mut store, &id, &mut now);
        assert_eq!(store.get(&id).unwrap().attempt_count, 3);
        for retry in 1..=3u32 {
            now += 100;
            store
                .retry_with_reason(&id, "operator-1", now, Some("dependency restored"))
                .unwrap();
            let event = store.get(&id).unwrap().execution_history.last().unwrap().clone();
            assert_eq!(event.actor, "operator-1");
            assert_eq!(event.evidence["retry"], "operator");
            assert_eq!(event.evidence["reason"], "dependency restored");
            assert_eq!(event.evidence["operator_retry"], retry.to_string());
            exhaust(&mut store, &id, &mut now);
            assert_eq!(
                store.get(&id).unwrap().attempt_count,
                3 + retry,
                "each operator retry grants exactly one attempt"
            );
            assert_eq!(store.get(&id).unwrap().state, WorkloadState::DeadLetter);
        }
        assert_eq!(
            store.retry(&id, "operator-1", now + 100).unwrap_err().code,
            "WORKLOAD_RETRY_EXHAUSTED"
        );
        assert_eq!(store.get(&id).unwrap().state, WorkloadState::DeadLetter);
        let _ = fs::remove_file(path);
    }

    fn store(name: &str) -> (PathBuf, WorkloadStore) {
        let path =
            std::env::temp_dir().join(format!("workload-{name}-{}.json", std::process::id()));
        let store = WorkloadStore::load(&path).unwrap();
        (path, store)
    }
    #[test]
    fn legacy_migration_is_idempotent_conflict_detecting_and_source_independent() {
        let (source_db, mut source) = store("legacy-source-builder");
        let workload = source.create(create()).unwrap();
        let legacy = LegacyWorkloadStore {
            workloads: source.workloads.clone(),
            // What a pre-requester-scope release wrote.
            idempotency: idempotency_index(&source.workloads, legacy_idempotency_scope),
            result_ids: source.result_ids.clone(),
            event_sequence: derived_event_sequence(&source.workloads),
        };
        let legacy_path = source_db.with_extension("legacy-workloads.json");
        fs::write(&legacy_path, serde_json::to_vec(&legacy).unwrap()).unwrap();
        let canonical_path = source_db.with_extension("canonical.log");
        let db = FeltDb::open(&canonical_path).unwrap();
        let migrated = WorkloadStore::migrate_legacy_file(db.clone(), &legacy_path).unwrap();
        assert_eq!(
            migrated.get(&workload.workload_id).unwrap().input_hash,
            workload.input_hash
        );
        WorkloadStore::migrate_legacy_file(db.clone(), &legacy_path).unwrap();
        fs::remove_file(&legacy_path).unwrap();
        let reopened = WorkloadStore::from_db(FeltDb::open(&canonical_path).unwrap()).unwrap();
        assert_eq!(
            reopened.get(&workload.workload_id).unwrap().workload_id,
            workload.workload_id
        );

        let mut conflicting = legacy;
        conflicting
            .workloads
            .get_mut(&workload.workload_id)
            .unwrap()
            .priority += 1;
        fs::write(&legacy_path, serde_json::to_vec(&conflicting).unwrap()).unwrap();
        assert_eq!(
            WorkloadStore::migrate_legacy_file(db, &legacy_path)
                .err()
                .unwrap()
                .code,
            "WORKLOAD_MIGRATION_CONFLICT"
        );
        let _ = fs::remove_file(source_db);
        let _ = fs::remove_file(canonical_path);
        let _ = fs::remove_file(legacy_path);
    }

    #[test]
    fn canonical_history_reconstructs_work_identity_provenance() {
        let (path, mut store) = store("provenance");
        let workload = store.create(create()).unwrap();
        let transaction = store
            .db
            .committed_transactions_after(0, usize::MAX)
            .unwrap()
            .into_iter()
            .last()
            .unwrap();
        let audit = transaction.audit.unwrap();
        assert_eq!(audit["work_id"], workload.workload_id);
        assert_eq!(audit["session_id"], workload.session_id.as_deref().unwrap());
        assert_eq!(audit["agent_id"], workload.requested_by.id());
        assert_eq!(
            audit["delegator_id"],
            workload.authorization_snapshot.grant.issuer.id()
        );
        assert!(transaction.commit_revision > transaction.base_revision);
        assert_eq!(transaction.work_id.as_deref(), Some(workload.workload_id.as_str()));
        assert_eq!(transaction.agent_id.as_deref(), Some(workload.requested_by.id()));
        assert_eq!(transaction.session_id, workload.session_id);
        assert_eq!(
            transaction.delegator_id.as_deref(),
            Some(workload.authorization_snapshot.grant.issuer.id())
        );
        assert_eq!(transaction.operation_ids.len(), 1);
        assert_ne!(transaction.operation_ids[0], transaction.transaction_id);
        drop(store);
        let reopened = WorkloadStore::load(&path).unwrap();
        assert_eq!(
            reopened.get(&workload.workload_id).unwrap().workload_id,
            workload.workload_id
        );
        let _ = fs::remove_file(path);
    }

    #[test]
    fn competing_writers_use_canonical_document_cas_before_and_after_restart() {
        let (path, mut seed) = store("canonical-cas");
        let workload = seed.create(create()).unwrap();
        let id = workload.workload_id.clone();
        let db = seed.db.clone();
        let caps = ["email:send".into()].into();

        let mut winner = WorkloadStore::from_db(db.clone()).unwrap();
        let mut loser = WorkloadStore::from_db(db.clone()).unwrap();
        winner.claim(&id, "worker-a", &caps, 11, 1_000).unwrap();
        let conflict = loser.claim(&id, "worker-b", &caps, 11, 1_000).unwrap_err();
        assert_eq!(conflict.code, "WORKLOAD_CONFLICT");
        // The loser committed nothing and now projects the winner's claim.
        let projected = loser.get(&id).unwrap();
        assert_eq!(projected.state, WorkloadState::Claimed);
        assert_eq!(projected.current_claim.as_ref().unwrap().worker_id, "worker-a");

        let row = db
            .list_collection(DURABLE_WORK_COLLECTION)
            .unwrap()
            .into_iter()
            .find(|row| row.key.ends_with(&id))
            .unwrap();
        assert_eq!(row.value["__version"], 2);

        drop((seed, winner, loser, db));
        let restarted_db = FeltDb::open(&path).unwrap();
        let mut recovery = WorkloadStore::from_db(restarted_db.clone()).unwrap();
        recovery.recover_expired(13).unwrap();
        let mut restarted_winner = WorkloadStore::from_db(restarted_db.clone()).unwrap();
        let mut restarted_loser = WorkloadStore::from_db(restarted_db.clone()).unwrap();
        restarted_winner
            .claim(&id, "worker-c", &caps, 13, 10_000)
            .unwrap();
        let conflict = restarted_loser
            .claim(&id, "worker-d", &caps, 13, 10_000)
            .unwrap_err();
        assert_eq!(conflict.code, "WORKLOAD_CONFLICT");
        let projected = restarted_loser.get(&id).unwrap();
        assert_eq!(projected.state, WorkloadState::Claimed);
        assert_eq!(projected.current_claim.as_ref().unwrap().worker_id, "worker-c");
        drop((recovery, restarted_winner, restarted_loser, restarted_db));
        let _ = fs::remove_file(path);
    }

    #[test]
    fn competing_admissions_share_one_canonical_idempotency_identity() {
        let path = std::env::temp_dir().join(format!(
            "workload-idempotency-cas-{}.log",
            std::process::id()
        ));
        let db = FeltDb::open(&path).unwrap();
        let mut first = WorkloadStore::from_db(db.clone()).unwrap();
        let mut second = WorkloadStore::from_db(db.clone()).unwrap();
        let accepted = first.create(create()).unwrap();
        // A second writer of the same store loses the require-absent CAS and
        // converges on what was committed: a different request is refused as a
        // reused key, never answered with the original.
        let mut competing = create();
        competing.input = serde_json::json!({"id": 2});
        let conflict = second.create(competing).unwrap_err();
        assert_eq!(conflict.code, "IDEMPOTENCY_KEY_REUSED");
        assert_eq!(
            second.workloads.keys().collect::<Vec<_>>(),
            vec![&accepted.workload_id],
            "only the committed workload is projected"
        );
        // The same request from a third, equally stale writer converges on it.
        let mut third = WorkloadStore::from_db(db.clone()).unwrap();
        third.workloads.clear();
        third.idempotency.clear();
        third.document_versions.clear();
        let (converged, created) = third.admit(create()).unwrap();
        assert!(!created);
        assert_eq!(converged.workload_id, accepted.workload_id);

        drop((first, second, third, db));
        let restarted = WorkloadStore::from_db(FeltDb::open(&path).unwrap()).unwrap();
        assert_eq!(restarted.workloads.len(), 1);
        assert_eq!(
            restarted.idempotency[&idempotency_scope("t", "app", &accepted.requested_by, "one")],
            accepted.workload_id
        );
        let _ = fs::remove_file(path);
    }

    #[test]
    fn idempotency_scopes_cannot_be_made_to_coincide() {
        let (path, mut store) = store("scope-separators");
        let mut first = create_as("x:k1");
        first.idempotency_key = "k2".into();
        let mut second = create_as("x");
        second.idempotency_key = "k1:k2".into();
        let first = store.create(first).unwrap();
        let second = store.create(second).unwrap();
        assert_ne!(first.workload_id, second.workload_id);
        let _ = fs::remove_file(path);
    }

    #[test]
    fn concurrent_admissions_by_competing_writers_converge_or_conflict() {
        for round in 0..20 {
            let (path, seed) = store(&format!("concurrent-admission-{round}"));
            let db = seed.db.clone();
            let admit_concurrently = |requests: [CreateWorkload; 2]| {
                let barrier = std::sync::Barrier::new(2);
                std::thread::scope(|scope| {
                    requests
                        .into_iter()
                        .map(|request| {
                            let mut writer = WorkloadStore::from_db(db.clone()).unwrap();
                            let barrier = &barrier;
                            scope.spawn(move || {
                                barrier.wait();
                                writer.admit(request)
                            })
                        })
                        .collect::<Vec<_>>()
                        .into_iter()
                        .map(|handle| handle.join().unwrap())
                        .collect::<Vec<_>>()
                })
            };
            // The same request: both writers answer with one workload, and
            // exactly one of them created it.
            let same = admit_concurrently([create(), create()]);
            let (first, first_created) = same[0].as_ref().unwrap().clone();
            let (second, second_created) = same[1].as_ref().unwrap().clone();
            assert_eq!(first.workload_id, second.workload_id);
            assert!(first_created ^ second_created, "round {round}");

            // Different requests under one key: one is admitted, the other is
            // refused as a reused key and never answered with the winner.
            let mut left = create();
            left.idempotency_key = "contested".into();
            let mut right = left.clone();
            right.input = serde_json::json!({"id": 2});
            let contested = admit_concurrently([left, right]);
            let admitted = contested.iter().filter(|outcome| outcome.is_ok()).count();
            assert_eq!(admitted, 1, "round {round}");
            assert!(contested.iter().any(|outcome| outcome
                .as_ref()
                .is_err_and(|error| error.code == "IDEMPOTENCY_KEY_REUSED")));
            let (winner, _) = contested.iter().find_map(|o| o.as_ref().ok()).unwrap();
            let stored = WorkloadStore::from_db(db.clone()).unwrap();
            assert_eq!(stored.workloads.len(), 2);
            assert_eq!(stored.get(&winner.workload_id).unwrap().input, winner.input);
            drop((seed, stored, db));
            let _ = fs::remove_file(path);
        }
    }

    #[test]
    fn pre_versioned_canonical_rows_upgrade_through_the_standard_cas_contract() {
        let (path, mut store) = store("pre-versioned-upgrade");
        let workload = store.create(create()).unwrap();
        let db = store.db.clone();
        db.apply_atomic_transaction(
            "fixture:pre-versioned-work",
            None,
            &[],
            &[AtomicMutation {
                capability: DURABLE_WORK_COLLECTION.into(),
                key: work_key(&workload),
                value: Some(serde_json::to_value(&workload).unwrap()),
            }],
            None,
        )
        .unwrap();

        let mut upgraded = WorkloadStore::from_db(db.clone()).unwrap();
        upgraded
            .claim(
                &workload.workload_id,
                "worker",
                &["email:send".into()].into(),
                11,
                10_000,
            )
            .unwrap();
        let row = db
            .list_collection(DURABLE_WORK_COLLECTION)
            .unwrap()
            .into_iter()
            .find(|row| row.key.ends_with(&workload.workload_id))
            .unwrap();
        assert_eq!(row.value["__version"], 2);
        let _ = fs::remove_file(path);
    }

    #[test]
    fn derived_indexes_rebuild_exclusively_from_canonical_work() {
        let (path, mut store) = store("derived-index-rebuild");
        let workload = store.create(create()).unwrap();
        let caps = ["email:send".into()].into();
        let claim = store
            .claim(&workload.workload_id, "worker", &caps, 11, 10_000)
            .unwrap();
        store
            .start(
                &workload.workload_id,
                &claim.claim_id,
                "worker",
                claim.fencing_token,
                12,
            )
            .unwrap();
        store
            .complete(
                &workload.workload_id,
                &claim.claim_id,
                "worker",
                claim.fencing_token,
                "result-rebuilt",
                serde_json::json!({"ok": true}),
                vec![],
                1,
                13,
            )
            .unwrap();

        store.idempotency.clear();
        store.result_ids.clear();
        store.document_versions.clear();
        let rebuilt = WorkloadStore::from_db(store.db.clone()).unwrap();
        assert_eq!(
            rebuilt
                .idempotency
                .get(&idempotency_scope("t", "app", &workload.requested_by, "one")),
            Some(&workload.workload_id)
        );
        assert_eq!(
            rebuilt.result_ids.get("result-rebuilt"),
            Some(&workload.workload_id)
        );
        assert!(rebuilt.document_versions[&workload.workload_id] > 1);
        assert_eq!(
            rebuilt
                .get(&workload.workload_id)
                .unwrap()
                .execution_history
                .iter()
                .map(|event| event.sequence)
                .collect::<Vec<_>>(),
            vec![1, 2, 3, 4]
        );
        let duplicate = WorkloadStore::from_db(store.db.clone())
            .unwrap()
            .create(create())
            .unwrap();
        assert_eq!(duplicate.workload_id, workload.workload_id);
        let _ = fs::remove_file(path);
    }
    #[test]
    fn idempotent_create_and_legal_lifecycle() {
        let (path, mut store) = store("lifecycle");
        let one = store.create(create()).unwrap();
        let two = store.create(create()).unwrap();
        assert_eq!(one.workload_id, two.workload_id);
        let caps = ["email:send".into()].into();
        let claim = store
            .claim(&one.workload_id, "worker", &caps, 11, 10_000)
            .unwrap();
        store
            .start(
                &one.workload_id,
                &claim.claim_id,
                "worker",
                claim.fencing_token,
                12,
            )
            .unwrap();
        let result = store
            .complete(
                &one.workload_id,
                &claim.claim_id,
                "worker",
                claim.fencing_token,
                "result-1",
                serde_json::json!({"ok":true}),
                vec![],
                1,
                13,
            )
            .unwrap();
        assert_eq!(result.result_id, "result-1");
        assert!(store
            .start(
                &one.workload_id,
                &claim.claim_id,
                "worker",
                claim.fencing_token,
                14
            )
            .is_err());
        let recovered = WorkloadStore::load(&path).unwrap();
        assert_eq!(
            recovered.get(&one.workload_id).unwrap().state,
            WorkloadState::Succeeded
        );
        let _ = fs::remove_file(path);
    }
    #[test]
    fn stale_fence_and_lease_recovery() {
        let (path, mut store) = store("fence");
        let w = store.create(create()).unwrap();
        let caps = ["email:send".into()].into();
        let old = store.claim(&w.workload_id, "a", &caps, 11, 1000).unwrap();
        store
            .start(&w.workload_id, &old.claim_id, "a", old.fencing_token, 11)
            .unwrap();
        store.recover_expired(13).unwrap();
        let new = store.claim(&w.workload_id, "b", &caps, 13, 10_000).unwrap();
        assert!(new.fencing_token > old.fencing_token);
        assert_eq!(
            store
                .complete(
                    &w.workload_id,
                    &old.claim_id,
                    "a",
                    old.fencing_token,
                    "stale",
                    Value::Null,
                    vec![],
                    1,
                    14
                )
                .unwrap_err()
                .code,
            "WORKLOAD_STALE_FENCE"
        );
        let _ = fs::remove_file(path);
    }
    #[test]
    fn retry_dead_letter_checkpoint_and_secret_safety() {
        let (path, mut store) = store("retry");
        let mut input = create();
        input.retry_policy.max_attempts = 1;
        let w = store.create(input).unwrap();
        let caps = ["email:send".into()].into();
        let claim = store.claim(&w.workload_id, "a", &caps, 11, 10_000).unwrap();
        store
            .start(
                &w.workload_id,
                &claim.claim_id,
                "a",
                claim.fencing_token,
                12,
            )
            .unwrap();
        assert!(store
            .checkpoint(
                &w.workload_id,
                &claim.claim_id,
                "a",
                claim.fencing_token,
                serde_json::json!({"password":"bad"}),
                13
            )
            .is_err());
        assert_eq!(
            store
                .fail(
                    &w.workload_id,
                    &claim.claim_id,
                    "a",
                    claim.fencing_token,
                    "worker_failure",
                    "crashed",
                    13
                )
                .unwrap(),
            WorkloadState::DeadLetter
        );
        let _ = fs::remove_file(path);
    }
    #[test]
    fn cancellation_race_and_duplicate_completion_are_deterministic() {
        let (path, mut store) = store("cancel");
        let w = store.create(create()).unwrap();
        let caps = ["email:send".into()].into();
        let claim = store.claim(&w.workload_id, "a", &caps, 11, 10_000).unwrap();
        store
            .start(
                &w.workload_id,
                &claim.claim_id,
                "a",
                claim.fencing_token,
                12,
            )
            .unwrap();
        assert_eq!(
            store.cancel(&w.workload_id, "user", 13).unwrap(),
            WorkloadState::CancellationRequested
        );
        assert_eq!(
            store
                .complete(
                    &w.workload_id,
                    &claim.claim_id,
                    "a",
                    claim.fencing_token,
                    "r",
                    Value::Null,
                    vec![],
                    1,
                    14
                )
                .unwrap_err()
                .code,
            "WORKLOAD_TRANSITION_DENIED"
        );
        store
            .confirm_cancel(
                &w.workload_id,
                &claim.claim_id,
                "a",
                claim.fencing_token,
                14,
            )
            .unwrap();
        assert_eq!(
            store.get(&w.workload_id).unwrap().state,
            WorkloadState::Cancelled
        );
        let _ = fs::remove_file(path);
    }
    #[test]
    fn scheduler_respects_dependencies_and_tenant_application_isolation() {
        let (path, mut store) = store("dependency");
        let parent = store.create(create()).unwrap();
        let mut child = create();
        child.idempotency_key = "child".into();
        child.dependencies = vec![WorkloadDependency {
            workload_id: parent.workload_id.clone(),
            required_state: WorkloadState::Succeeded,
        }];
        let child = store.create(child).unwrap();
        assert_eq!(store.list("other", "app").len(), 0);
        let caps = ["email:send".into()].into();
        let mut scheduler = LocalDurableScheduler::default();
        let first = scheduler
            .claim(&mut store, "t", "app", "worker", &caps, 11, 10_000)
            .unwrap()
            .unwrap();
        assert_eq!(first.workload_id, parent.workload_id);
        assert_ne!(first.workload_id, child.workload_id);
        let _ = fs::remove_file(path);
    }

    // ------------------------------------------------------------------
    // Temporal work eligibility.
    //
    // The invariant under test: a workload may exist durably before it is
    // executable. Every test below drives the real store — creating, claiming,
    // reloading from disk — rather than asserting that a field or a function
    // name exists.
    // ------------------------------------------------------------------

    /// A store on a path unique to this test, cleared first so a recycled
    /// process id cannot hand a test a previous run's file.
    fn temporal_store(name: &str) -> (PathBuf, WorkloadStore) {
        let path = std::env::temp_dir().join(format!(
            "workload-temporal-{name}-{}-{}.json",
            std::process::id(),
            rand::random::<u64>()
        ));
        let _ = fs::remove_file(&path);
        (path.clone(), WorkloadStore::load(&path).unwrap())
    }

    fn scheduled(not_before: i64) -> CreateWorkload {
        let mut input = create();
        input.not_before = Some(not_before);
        input
    }

    fn caps() -> BTreeSet<String> {
        ["email:send".into()].into()
    }

    #[test]
    fn scheduled_work_is_not_claimable_before_not_before() {
        let (path, mut store) = temporal_store("before");
        let workload = store.create(scheduled(2_000)).unwrap();

        // Durable and Ready immediately: existing before it is executable is
        // the whole point.
        assert_eq!(workload.state, WorkloadState::Ready);
        assert_eq!(workload.next_attempt_at, Some(2_000));

        let refused = store
            .claim(&workload.workload_id, "worker", &caps(), 1_999, 10_000)
            .unwrap_err();
        assert_eq!(refused.code, "WORKLOAD_NOT_YET_ELIGIBLE");
        assert!(
            refused.message.contains("2000"),
            "the refusal names the instant the worker should return at: {}",
            refused.message
        );

        // Refusing a claim is not a state change. Nothing executed.
        let after = store.get(&workload.workload_id).unwrap();
        assert_eq!(after.state, WorkloadState::Ready);
        assert_eq!(after.attempt_count, 0);
        assert!(after.attempts.is_empty());
        assert!(after.current_claim.is_none());
        let _ = fs::remove_file(path);
    }

    #[test]
    fn scheduled_work_becomes_claimable_after_not_before() {
        let (path, mut store) = temporal_store("after");
        let workload = store.create(scheduled(2_000)).unwrap();

        // The boundary belongs to the eligible side: at exactly `not_before`
        // the workload is claimable, so a worker polling on the second does not
        // have to wait a further tick.
        let claim = store
            .claim(&workload.workload_id, "worker", &caps(), 2_000, 10_000)
            .expect("eligible at the instant itself");
        assert_eq!(claim.worker_id, "worker");
        assert_eq!(
            store.get(&workload.workload_id).unwrap().state,
            WorkloadState::Claimed
        );
        let _ = fs::remove_file(path);
    }

    #[test]
    fn scheduled_work_survives_restart_before_eligibility() {
        let (path, mut store) = temporal_store("restart-before");
        let id = store.create(scheduled(2_000)).unwrap().workload_id;
        drop(store);

        // Restart the authority. The workload is still here, still Ready, still
        // carrying its eligibility, and still refused.
        let mut reopened = WorkloadStore::load(&path).unwrap();
        let recovered = reopened.get(&id).unwrap();
        assert_eq!(recovered.state, WorkloadState::Ready);
        assert_eq!(recovered.next_attempt_at, Some(2_000));

        assert_eq!(
            reopened
                .claim(&id, "worker", &caps(), 1_999, 10_000)
                .unwrap_err()
                .code,
            "WORKLOAD_NOT_YET_ELIGIBLE"
        );
        let _ = fs::remove_file(path);
    }

    #[test]
    fn scheduled_work_executes_after_restart() {
        let (path, mut store) = temporal_store("restart-after");
        let id = store.create(scheduled(2_000)).unwrap().workload_id;
        drop(store);

        let mut reopened = WorkloadStore::load(&path).unwrap();
        let claim = reopened
            .claim(&id, "worker", &caps(), 2_001, 10_000)
            .expect("claimable after restart once eligible");
        reopened
            .start(&id, &claim.claim_id, "worker", claim.fencing_token, 2_002)
            .unwrap();
        reopened
            .complete(
                &id,
                &claim.claim_id,
                "worker",
                claim.fencing_token,
                "result-scheduled",
                serde_json::json!({"ok": true}),
                vec![],
                1,
                2_003,
            )
            .unwrap();

        // And the outcome is durable across a second restart.
        let again = WorkloadStore::load(&path).unwrap();
        let finished = again.get(&id).unwrap();
        assert_eq!(finished.state, WorkloadState::Succeeded);
        assert_eq!(
            finished.result.as_ref().map(|r| r.result_id.as_str()),
            Some("result-scheduled")
        );
        let _ = fs::remove_file(path);
    }

    #[test]
    fn immediate_work_behavior_is_unchanged() {
        let (path, mut store) = temporal_store("immediate");
        // `create()` sets no `not_before`, which is what every caller did
        // before this field existed.
        let workload = store.create(create()).unwrap();
        assert_eq!(workload.next_attempt_at, None);
        assert!(workload.is_eligible_at(workload.created_at));

        // Claimable at its own creation instant, with no waiting.
        store
            .claim(
                &workload.workload_id,
                "worker",
                &caps(),
                workload.created_at,
                10_000,
            )
            .expect("work with no eligibility instant is claimable immediately");
        let _ = fs::remove_file(path);
    }

    #[test]
    fn multiple_workers_claim_temporally_eligible_work_once() {
        let (path, mut store) = temporal_store("contention");
        let id = store.create(scheduled(2_000)).unwrap().workload_id;

        // Both workers are polling and both see eligibility open at the same
        // instant. Exactly one wins.
        let winner = store
            .claim(&id, "worker-a", &caps(), 2_000, 10_000)
            .unwrap();
        let loser = store
            .claim(&id, "worker-b", &caps(), 2_000, 10_000)
            .unwrap_err();
        assert_eq!(
            loser.code, "WORKLOAD_NOT_READY",
            "the second worker is refused on state, not on time: the workload is claimed"
        );

        // And the loser cannot complete work it never owned.
        assert_eq!(
            store
                .complete(
                    &id,
                    &winner.claim_id,
                    "worker-b",
                    winner.fencing_token,
                    "duplicate",
                    Value::Null,
                    vec![],
                    1,
                    2_001,
                )
                .unwrap_err()
                .code,
            "WORKLOAD_STALE_FENCE"
        );
        assert_eq!(store.get(&id).unwrap().attempt_count, 1);
        let _ = fs::remove_file(path);
    }

    #[test]
    fn retry_sets_next_eligibility_without_duplicate_execution() {
        let (path, mut store) = temporal_store("retry");
        let id = store.create(scheduled(2_000)).unwrap().workload_id;

        let first = store
            .claim(&id, "worker-a", &caps(), 2_000, 10_000)
            .unwrap();
        store
            .start(&id, &first.claim_id, "worker-a", first.fencing_token, 2_001)
            .unwrap();
        let state = store
            .fail(
                &id,
                &first.claim_id,
                "worker-a",
                first.fencing_token,
                "timeout",
                "upstream did not answer",
                2_002,
            )
            .unwrap();

        // Retry re-arms the same eligibility field creation used, so there is
        // one temporal concept rather than two competing ones.
        assert_eq!(state, WorkloadState::Ready);
        let backing_off = store.get(&id).unwrap();
        let next = backing_off.next_attempt_at.expect("retry set eligibility");
        assert!(
            next > 2_002,
            "backoff pushed eligibility into the future: {next}"
        );

        assert_eq!(
            store
                .claim(&id, "worker-b", &caps(), next - 1, 10_000)
                .unwrap_err()
                .code,
            "WORKLOAD_NOT_YET_ELIGIBLE"
        );

        let second = store.claim(&id, "worker-b", &caps(), next, 10_000).unwrap();
        assert!(second.fencing_token > first.fencing_token);

        // The first attempt cannot land a result after the retry was handed on.
        assert_eq!(
            store
                .complete(
                    &id,
                    &first.claim_id,
                    "worker-a",
                    first.fencing_token,
                    "stale",
                    Value::Null,
                    vec![],
                    1,
                    next,
                )
                .unwrap_err()
                .code,
            "WORKLOAD_STALE_FENCE"
        );
        assert_eq!(store.get(&id).unwrap().attempt_count, 2);
        let _ = fs::remove_file(path);
    }

    #[test]
    fn cancellation_before_not_before_prevents_execution() {
        let (path, mut store) = temporal_store("cancel");
        let id = store.create(scheduled(2_000)).unwrap().workload_id;

        // Cancelling work that is durable but not yet executable is the whole
        // reason to be able to see it before it runs.
        assert_eq!(
            store.cancel(&id, "operator", 1_500).unwrap(),
            WorkloadState::Cancelled
        );

        // Eligibility arriving does not resurrect it.
        assert_eq!(
            store
                .claim(&id, "worker", &caps(), 2_001, 10_000)
                .unwrap_err()
                .code,
            "WORKLOAD_NOT_READY"
        );

        let reopened = WorkloadStore::load(&path).unwrap();
        let cancelled = reopened.get(&id).unwrap();
        assert_eq!(cancelled.state, WorkloadState::Cancelled);
        assert!(cancelled.attempts.is_empty(), "nothing ever executed");
        let _ = fs::remove_file(path);
    }

    #[test]
    fn a_worker_dying_after_a_claim_returns_the_work_and_it_executes_once() {
        let (path, mut store) = temporal_store("worker-death");
        let id = store.create(create()).unwrap().workload_id;

        // Worker A claims a short lease, starts, and dies without completing.
        let dead = store.claim(&id, "worker-a", &caps(), 10, 1_000).unwrap();
        store
            .start(&id, &dead.claim_id, "worker-a", dead.fencing_token, 10)
            .unwrap();
        assert_eq!(
            store.get(&id).unwrap().state,
            WorkloadState::Running,
            "the work was in flight when its worker died"
        );

        // The lease expiring is the only thing that can notice.
        let recovered = store.recover_expired(12).unwrap();
        assert_eq!(recovered, vec![id.clone()]);
        let back = store.get(&id).unwrap();
        assert_eq!(back.state, WorkloadState::Ready, "returned to the pool");
        assert_eq!(
            back.attempts.last().map(|attempt| attempt.status),
            Some(AttemptStatus::WorkerLost),
            "and the dead attempt is recorded as lost rather than erased"
        );

        // Worker B picks it up and finishes it.
        let live = store.claim(&id, "worker-b", &caps(), 12, 10_000).unwrap();
        assert!(live.fencing_token > dead.fencing_token);
        store
            .start(&id, &live.claim_id, "worker-b", live.fencing_token, 13)
            .unwrap();
        store
            .complete(
                &id,
                &live.claim_id,
                "worker-b",
                live.fencing_token,
                "result-recovered",
                serde_json::json!({"ok": true}),
                vec![],
                1,
                14,
            )
            .unwrap();

        // Exactly one execution succeeded, and the dead worker cannot add another.
        let finished = store.get(&id).unwrap();
        assert_eq!(finished.state, WorkloadState::Succeeded);
        assert_eq!(
            finished
                .attempts
                .iter()
                .filter(|attempt| attempt.status == AttemptStatus::Succeeded)
                .count(),
            1
        );
        // The dead worker cannot add a second result. Its claim was released by
        // the recovery, and completion released the successor's, so it is
        // refused for having no claim at all rather than a stale one.
        assert_eq!(
            store
                .complete(
                    &id,
                    &dead.claim_id,
                    "worker-a",
                    dead.fencing_token,
                    "resurrected",
                    Value::Null,
                    vec![],
                    1,
                    15,
                )
                .unwrap_err()
                .code,
            "WORKLOAD_CLAIM_INVALID"
        );
        let _ = fs::remove_file(path);
    }

    #[test]
    fn a_recovered_workload_refuses_its_dead_worker_while_the_successor_still_holds_it() {
        let (path, mut store) = temporal_store("worker-death-inflight");
        let id = store.create(create()).unwrap().workload_id;

        let dead = store.claim(&id, "worker-a", &caps(), 10, 1_000).unwrap();
        store
            .start(&id, &dead.claim_id, "worker-a", dead.fencing_token, 10)
            .unwrap();
        store.recover_expired(12).unwrap();
        let live = store.claim(&id, "worker-b", &caps(), 12, 10_000).unwrap();

        // While the successor is mid-flight, the resurrected worker is refused
        // on the fence itself — the case a lease exists to decide.
        assert_eq!(
            store
                .complete(
                    &id,
                    &dead.claim_id,
                    "worker-a",
                    dead.fencing_token,
                    "resurrected",
                    Value::Null,
                    vec![],
                    1,
                    13,
                )
                .unwrap_err()
                .code,
            "WORKLOAD_STALE_FENCE"
        );
        assert_eq!(store.get(&id).unwrap().state, WorkloadState::Claimed);
        assert!(live.fencing_token > dead.fencing_token);
        let _ = fs::remove_file(path);
    }

    #[test]
    fn the_scheduler_skips_ineligible_work_rather_than_failing_on_it() {
        let (path, mut store) = temporal_store("scheduler");
        let mut early = create();
        early.idempotency_key = "early".into();
        early.not_before = Some(9_000);
        let early = store.create(early).unwrap();

        let mut ready = create();
        ready.idempotency_key = "ready".into();
        let ready = store.create(ready).unwrap();

        // The ineligible workload sorts first by id in some orderings and not
        // others, so what matters is that the scheduler never returns it and
        // never fails because of it.
        let mut scheduler = LocalDurableScheduler::default();
        let claim = scheduler
            .claim(&mut store, "t", "app", "worker", &caps(), 1_000, 10_000)
            .expect("an ineligible workload must not fail the whole pass")
            .expect("the eligible workload is still handed out");
        assert_eq!(claim.workload_id, ready.workload_id);
        assert_eq!(
            store.get(&early.workload_id).unwrap().state,
            WorkloadState::Ready
        );
        let _ = fs::remove_file(path);
    }
}