cloudillo-core 0.8.18

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

//! Scheduler subsystem. Handles async tasks, dependencies, fallbacks, repetitions, persistence..

use async_trait::async_trait;
use itertools::Itertools;
use std::{
	collections::{BTreeMap, HashMap},
	fmt::Debug,
	sync::{Arc, Mutex, RwLock},
};

use chrono::{DateTime, Utc};
use croner::Cron;
use std::str::FromStr;

use crate::prelude::*;
use cloudillo_types::{lock, meta_adapter};

pub type TaskId = u64;

pub enum TaskType {
	Periodic,
	Once,
}

/// Cron schedule wrapper using the croner crate
/// Stores the expression string for serialization
#[derive(Debug, Clone)]
pub struct CronSchedule {
	/// The original cron expression string
	expr: Box<str>,
	/// Parsed cron object
	cron: Cron,
}

impl CronSchedule {
	/// Parse a cron expression (5 fields: minute hour day month weekday)
	pub fn parse(expr: &str) -> ClResult<Self> {
		let cron = Cron::from_str(expr)
			.map_err(|e| Error::ValidationError(format!("invalid cron expression: {}", e)))?;
		Ok(Self { expr: expr.into(), cron })
	}

	/// Calculate the next execution time after the given timestamp
	///
	/// Returns an error if no next occurrence can be found (should be rare
	/// for valid expressions within reasonable time bounds).
	pub fn next_execution(&self, after: Timestamp) -> ClResult<Timestamp> {
		let dt = DateTime::<Utc>::from_timestamp(after.0, 0).unwrap_or_else(Utc::now);

		self.cron
			.find_next_occurrence(&dt, false)
			.map(|next| Timestamp(next.timestamp()))
			.map_err(|e| {
				tracing::error!("Failed to find next cron occurrence for '{}': {}", self.expr, e);
				Error::ValidationError(format!("cron next_execution failed: {}", e))
			})
	}

	/// Convert back to cron expression string
	pub fn to_cron_string(&self) -> String {
		self.expr.to_string()
	}
}

impl PartialEq for CronSchedule {
	fn eq(&self, other: &Self) -> bool {
		self.expr == other.expr
	}
}

impl Eq for CronSchedule {}

#[async_trait]
pub trait Task<S: Clone>: Send + Sync + Debug {
	fn kind() -> &'static str
	where
		Self: Sized;
	fn build(id: TaskId, context: &str) -> ClResult<Arc<dyn Task<S>>>
	where
		Self: Sized;
	fn serialize(&self) -> String;
	async fn run(&self, state: &S) -> ClResult<()>;

	fn kind_of(&self) -> &'static str;

	/// Called when the task transitions to `Failed` after exhausting retries
	/// (or on the very first failure when no retry policy is set). Lets the
	/// task perform irreversible cleanup — e.g. mark a related domain row as
	/// permanently failed — that should not happen on retryable failures.
	/// Default: no-op.
	async fn on_failed(&self, _state: &S, _attempts: u16, _last_error: &str) {}

	/// Called after a failure that **will** be retried, with the zero-based index
	/// of the attempt that just failed. Lets a task tell the user once, on the
	/// first failure, instead of once per retry. Default: no-op.
	///
	/// Mutually exclusive with [`Task::on_failed`] for any single failure: a
	/// failure either schedules a retry (this) or is terminal (that).
	async fn on_attempt_failed(&self, _state: &S, _attempt: u16, _last_error: &str) {}
}

#[derive(Debug)]
pub enum TaskStatus {
	Pending,
	Completed,
	Failed,
}

pub struct TaskData {
	id: TaskId,
	kind: Box<str>,
	status: TaskStatus,
	input: Box<str>,
	deps: Box<[TaskId]>,
	retry_data: Option<Box<str>>,
	cron_data: Option<Box<str>>,
	next_at: Option<Timestamp>,
}

#[async_trait]
pub trait TaskStore<S: Clone>: Send + Sync {
	async fn add(&self, task: &TaskMeta<S>, key: Option<&str>) -> ClResult<TaskId>;
	async fn finished(&self, id: TaskId, output: &str) -> ClResult<()>;
	async fn load(&self) -> ClResult<Vec<TaskData>>;
	async fn update_task_error(
		&self,
		task_id: TaskId,
		output: &str,
		next_at: Option<Timestamp>,
	) -> ClResult<()>;
	async fn find_by_key(&self, key: &str) -> ClResult<Option<(TaskId, TaskData)>>;
	async fn update_task(&self, id: TaskId, task: &TaskMeta<S>) -> ClResult<()>;
	async fn find_completed_deps(&self, deps: &[TaskId]) -> ClResult<Vec<TaskId>>;
}

// InMemoryTaskStore
//*******************
pub struct InMemoryTaskStore {
	last_id: Mutex<TaskId>,
}

impl InMemoryTaskStore {
	pub fn new() -> Arc<Self> {
		Arc::new(Self { last_id: Mutex::new(0) })
	}
}

#[async_trait]
impl<S: Clone> TaskStore<S> for InMemoryTaskStore {
	async fn add(&self, _task: &TaskMeta<S>, _key: Option<&str>) -> ClResult<TaskId> {
		let mut last_id = lock!(self.last_id)?;
		*last_id += 1;
		Ok(*last_id)
	}

	async fn finished(&self, _id: TaskId, _output: &str) -> ClResult<()> {
		Ok(())
	}

	async fn load(&self) -> ClResult<Vec<TaskData>> {
		Ok(vec![])
	}

	async fn update_task_error(
		&self,
		_task_id: TaskId,
		_output: &str,
		_next_at: Option<Timestamp>,
	) -> ClResult<()> {
		Ok(())
	}

	async fn find_by_key(&self, _key: &str) -> ClResult<Option<(TaskId, TaskData)>> {
		// In-memory store doesn't support persistence or keys
		Ok(None)
	}

	async fn update_task(&self, _id: TaskId, _task: &TaskMeta<S>) -> ClResult<()> {
		// In-memory store doesn't support persistence
		Ok(())
	}

	async fn find_completed_deps(&self, _deps: &[TaskId]) -> ClResult<Vec<TaskId>> {
		Ok(vec![])
	}
}

// MetaAdapterTaskStore
//**********************
pub struct MetaAdapterTaskStore {
	meta_adapter: Arc<dyn meta_adapter::MetaAdapter>,
}

impl MetaAdapterTaskStore {
	pub fn new(meta_adapter: Arc<dyn meta_adapter::MetaAdapter>) -> Arc<Self> {
		Arc::new(Self { meta_adapter })
	}
}

#[async_trait]
impl<S: Clone> TaskStore<S> for MetaAdapterTaskStore {
	async fn add(&self, task: &TaskMeta<S>, key: Option<&str>) -> ClResult<TaskId> {
		let id = self
			.meta_adapter
			.create_task(task.task.kind_of(), key, &task.task.serialize(), &task.deps)
			.await?;

		// Store cron schedule if present
		if let Some(cron) = &task.cron {
			self.meta_adapter
				.update_task(
					id,
					&meta_adapter::TaskPatch {
						cron: Patch::Value(cron.to_cron_string()),
						..Default::default()
					},
				)
				.await?;
		}

		Ok(id)
	}

	async fn finished(&self, id: TaskId, output: &str) -> ClResult<()> {
		self.meta_adapter.update_task_finished(id, output).await
	}

	async fn load(&self) -> ClResult<Vec<TaskData>> {
		let tasks = self.meta_adapter.list_tasks(meta_adapter::ListTaskOptions::default()).await?;
		let tasks = tasks
			.into_iter()
			.map(|t| TaskData {
				id: t.task_id,
				kind: t.kind,
				status: match t.status {
					'P' => TaskStatus::Pending,
					'F' => TaskStatus::Completed,
					// 'E' or unknown status = Failed
					_ => TaskStatus::Failed,
				},
				input: t.input,
				deps: t.deps,
				retry_data: t.retry,
				cron_data: t.cron,
				next_at: t.next_at,
			})
			.collect();
		Ok(tasks)
	}

	async fn update_task_error(
		&self,
		task_id: TaskId,
		output: &str,
		next_at: Option<Timestamp>,
	) -> ClResult<()> {
		self.meta_adapter.update_task_error(task_id, output, next_at).await
	}

	async fn find_by_key(&self, key: &str) -> ClResult<Option<(TaskId, TaskData)>> {
		let task_opt = self.meta_adapter.find_task_by_key(key).await?;

		match task_opt {
			Some(t) => Ok(Some((
				t.task_id,
				TaskData {
					id: t.task_id,
					kind: t.kind,
					status: match t.status {
						'P' => TaskStatus::Pending,
						'F' => TaskStatus::Completed,
						// 'E' or unknown status = Failed
						_ => TaskStatus::Failed,
					},
					input: t.input,
					deps: t.deps,
					retry_data: t.retry,
					cron_data: t.cron,
					next_at: t.next_at,
				},
			))),
			None => Ok(None),
		}
	}

	async fn update_task(&self, id: TaskId, task: &TaskMeta<S>) -> ClResult<()> {
		use cloudillo_types::types::Patch;

		// Build TaskPatch from TaskMeta
		let mut patch = meta_adapter::TaskPatch {
			input: Patch::Value(task.task.serialize()),
			next_at: match task.next_at {
				Some(ts) => Patch::Value(ts),
				None => Patch::Null,
			},
			..Default::default()
		};

		// Update deps
		if !task.deps.is_empty() {
			patch.deps = Patch::Value(task.deps.clone());
		}

		// Update retry policy
		if let Some(ref retry) = task.retry {
			let retry_str = format!(
				"{},{},{},{}",
				task.retry_count, retry.wait_min_max.0, retry.wait_min_max.1, retry.times
			);
			patch.retry = Patch::Value(retry_str);
		}

		// Update cron schedule
		if let Some(ref cron) = task.cron {
			patch.cron = Patch::Value(cron.to_cron_string());
		}

		self.meta_adapter.update_task(id, &patch).await
	}

	async fn find_completed_deps(&self, deps: &[TaskId]) -> ClResult<Vec<TaskId>> {
		self.meta_adapter.find_completed_deps(deps).await
	}
}

// Task metadata
type TaskBuilder<S> = dyn Fn(TaskId, &str) -> ClResult<Arc<dyn Task<S>>> + Send + Sync;

#[derive(Debug, Clone)]
pub struct RetryPolicy {
	wait_min_max: (u64, u64),
	times: u16,
}

impl Default for RetryPolicy {
	fn default() -> Self {
		Self { wait_min_max: (60, 3600), times: 10 }
	}
}

impl RetryPolicy {
	/// Create a new RetryPolicy with custom min/max backoff and number of retries
	pub fn new(wait_min_max: (u64, u64), times: u16) -> Self {
		Self { wait_min_max, times }
	}

	/// Calculate exponential backoff in seconds: min * (2^attempt), capped at max
	///
	/// `times` is a `u16` and `new` is public, so a policy can ask for an attempt
	/// count past the width of the shift. Saturating there rather than shifting
	/// out is what keeps this panic-free in debug builds.
	pub fn calculate_backoff(&self, attempt_count: u16) -> u64 {
		let (min, max) = self.wait_min_max;
		let backoff = 1u64
			.checked_shl(u32::from(attempt_count))
			.map_or(u64::MAX, |factor| min.saturating_mul(factor));
		backoff.min(max)
	}

	/// Check if we should continue retrying
	pub fn should_retry(&self, attempt_count: u16) -> bool {
		attempt_count < self.times
	}
}

// TaskSchedulerBuilder - Fluent API for task scheduling
//************************************************************
pub struct TaskSchedulerBuilder<'a, S: Clone> {
	scheduler: &'a Scheduler<S>,
	task: Arc<dyn Task<S>>,
	key: Option<String>,
	next_at: Option<Timestamp>,
	deps: Vec<TaskId>,
	retry: Option<RetryPolicy>,
	cron: Option<CronSchedule>,
	run_on_startup: bool,
}

impl<'a, S: Clone + Send + Sync + 'static> TaskSchedulerBuilder<'a, S> {
	/// Create a new builder for scheduling a task
	fn new(scheduler: &'a Scheduler<S>, task: Arc<dyn Task<S>>) -> Self {
		Self {
			scheduler,
			task,
			key: None,
			next_at: None,
			deps: Vec::new(),
			retry: None,
			cron: None,
			run_on_startup: false,
		}
	}

	/// Set a string key for task identification
	pub fn key(mut self, key: impl Into<String>) -> Self {
		self.key = Some(key.into());
		self
	}

	/// Schedule for a specific absolute timestamp
	pub fn schedule_at(mut self, timestamp: Timestamp) -> Self {
		self.next_at = Some(timestamp);
		self
	}

	/// Schedule after a relative delay (in seconds)
	pub fn schedule_after(mut self, seconds: i64) -> Self {
		self.next_at = Some(Timestamp::from_now(seconds));
		self
	}

	/// Add task dependencies - task waits for all of these to complete
	pub fn depend_on(mut self, deps: Vec<TaskId>) -> Self {
		self.deps = deps;
		self
	}

	/// Add a single task dependency
	pub fn depends_on(mut self, dep: TaskId) -> Self {
		self.deps.push(dep);
		self
	}

	/// Enable automatic retry with exponential backoff
	pub fn with_retry(mut self, policy: RetryPolicy) -> Self {
		self.retry = Some(policy);
		self
	}

	// ===== Cron Scheduling Methods =====

	/// Schedule task with cron expression
	/// Example: `.cron("0 9 * * *")` for 9 AM daily
	///
	/// A bad expression is logged and ignored rather than propagated — the
	/// builder is infallible by design. But ignoring it silently degrades a
	/// recurring task into a one-shot: `add_queue` sees `next_at: None`, runs the
	/// task once immediately, and the finish handler retires the row as completed,
	/// so it never comes back. Expressions that reach here from a setting must
	/// also be validated at write time (see `core_settings::cron_validator`); the
	/// `error!` is what makes an already-stored bad value visible.
	pub fn cron(mut self, expr: impl Into<String>) -> Self {
		let expr = expr.into();
		match CronSchedule::parse(&expr) {
			Ok(cron_schedule) => {
				self.next_at = cron_schedule.next_execution(Timestamp::now()).ok();
				self.cron = Some(cron_schedule);
			}
			Err(e) => error!(
				"scheduler: task '{}' has an unusable cron expression {:?} ({}); it will run \
				 once instead of recurring",
				self.task.kind_of(),
				expr,
				e
			),
		}
		self
	}

	/// Schedule task daily at specified time
	/// Example: `.daily_at(2, 30)` for 2:30 AM daily
	pub fn daily_at(mut self, hour: u8, minute: u8) -> Self {
		if hour <= 23 && minute <= 59 {
			let expr = format!("{} {} * * *", minute, hour);
			if let Ok(cron_schedule) = CronSchedule::parse(&expr) {
				// Calculate initial next_at from cron schedule
				// Use .ok() - cron was just parsed successfully, should never fail
				self.next_at = cron_schedule.next_execution(Timestamp::now()).ok();
				self.cron = Some(cron_schedule);
			}
		}
		self
	}

	/// Schedule task weekly at specified day and time
	/// Example: `.weekly_at(1, 14, 30)` for Mondays at 2:30 PM
	/// weekday: 0=Sunday, 1=Monday, ..., 6=Saturday
	pub fn weekly_at(mut self, weekday: u8, hour: u8, minute: u8) -> Self {
		if weekday <= 6 && hour <= 23 && minute <= 59 {
			let expr = format!("{} {} * * {}", minute, hour, weekday);
			if let Ok(cron_schedule) = CronSchedule::parse(&expr) {
				// Calculate initial next_at from cron schedule
				// Use .ok() - cron was just parsed successfully, should never fail
				self.next_at = cron_schedule.next_execution(Timestamp::now()).ok();
				self.cron = Some(cron_schedule);
			}
		}
		self
	}

	/// Opt-in: if this is a cron task and a scheduled run was missed
	/// while the server was down (or this is the first time the task
	/// is being registered), run it once immediately on startup before
	/// resuming the normal cron schedule.
	pub fn run_on_startup(mut self) -> Self {
		self.run_on_startup = true;
		self
	}

	/// Execute the scheduled task immediately
	pub async fn now(self) -> ClResult<TaskId> {
		self.schedule().await
	}

	/// Execute the scheduled task at a specific timestamp
	pub async fn at(mut self, ts: Timestamp) -> ClResult<TaskId> {
		self.next_at = Some(ts);
		self.schedule().await
	}

	/// Execute the scheduled task after a delay (in seconds)
	pub async fn after(mut self, seconds: i64) -> ClResult<TaskId> {
		self.next_at = Some(Timestamp::from_now(seconds));
		self.schedule().await
	}

	/// Execute the scheduled task after another task completes
	pub async fn after_task(mut self, dep: TaskId) -> ClResult<TaskId> {
		self.deps.push(dep);
		self.schedule().await
	}

	/// Execute the scheduled task with automatic retry using default policy
	pub async fn with_automatic_retry(mut self) -> ClResult<TaskId> {
		self.retry = Some(RetryPolicy::default());
		self.schedule().await
	}

	/// Execute the task with all configured options - main terminal method
	pub async fn schedule(self) -> ClResult<TaskId> {
		self.scheduler
			.schedule_task_impl(
				self.task,
				self.key.as_deref(),
				self.next_at,
				if self.deps.is_empty() { None } else { Some(self.deps) },
				self.retry,
				self.cron,
				self.run_on_startup,
			)
			.await
	}
}

#[derive(Debug, Clone)]
pub struct TaskMeta<S: Clone> {
	pub task: Arc<dyn Task<S>>,
	pub next_at: Option<Timestamp>,
	pub deps: Vec<TaskId>,
	retry_count: u16,
	pub retry: Option<RetryPolicy>,
	pub cron: Option<CronSchedule>,
	/// Set while this task is running, by a keyed re-request that arrived in
	/// flight.
	///
	/// The re-request cannot start a second body — that is what the in-place
	/// metadata update in [`Scheduler::schedule_task_impl`] prevents — and, unlike
	/// a cron task, a one-shot has no reschedule step that would pick the request
	/// up. Without this the requested run is simply dropped, so the finish handler
	/// consults it and re-queues instead of finishing the task.
	///
	/// It lives here rather than in a side table so that reading it, reading the
	/// metadata the request wrote, and closing the request window are one
	/// operation under one lock. **Invariant:** true only for an entry currently
	/// in `tasks_running`.
	rerun_requested: bool,
}

type TaskBuilderRegistry<S> = HashMap<&'static str, Box<TaskBuilder<S>>>;
type ScheduledTaskMap<S> = BTreeMap<(Timestamp, TaskId), TaskMeta<S>>;

// Scheduler
#[derive(Clone)]
pub struct Scheduler<S: Clone> {
	task_builders: Arc<RwLock<TaskBuilderRegistry<S>>>,
	store: Arc<dyn TaskStore<S>>,
	tasks_running: Arc<Mutex<HashMap<TaskId, TaskMeta<S>>>>,
	tasks_waiting: Arc<Mutex<HashMap<TaskId, TaskMeta<S>>>>,
	task_dependents: Arc<Mutex<HashMap<TaskId, Vec<TaskId>>>>,
	tasks_scheduled: Arc<Mutex<ScheduledTaskMap<S>>>,
	tx_finish: flume::Sender<TaskId>,
	rx_finish: flume::Receiver<TaskId>,
	notify_schedule: Arc<tokio::sync::Notify>,
}

impl<S: Clone + Send + Sync + 'static> Scheduler<S> {
	pub fn new(store: Arc<dyn TaskStore<S>>) -> Arc<Self> {
		let (tx_finish, rx_finish) = flume::unbounded();

		let scheduler = Self {
			task_builders: Arc::new(RwLock::new(HashMap::new())),
			store,
			tasks_running: Arc::new(Mutex::new(HashMap::new())),
			tasks_waiting: Arc::new(Mutex::new(HashMap::new())),
			task_dependents: Arc::new(Mutex::new(HashMap::new())),
			tasks_scheduled: Arc::new(Mutex::new(BTreeMap::new())),
			tx_finish,
			rx_finish,
			notify_schedule: Arc::new(tokio::sync::Notify::new()),
		};

		//scheduler.run(rx_finish)?;

		Arc::new(scheduler)
	}

	pub fn start(&self, state: S) {
		// Handle finished tasks and dependencies
		let schedule = self.clone();
		let stat = state.clone();
		let rx_finish = self.rx_finish.clone();

		tokio::spawn(async move {
			while let Ok(id) = rx_finish.recv_async().await {
				debug!("Completed task {} (notified)", id);

				// Taken out of `tasks_running` up front, then decided. Safe because by
				// the time this event arrives the task body has *already returned* —
				// removing the id cannot let a second body start beside a running one,
				// which is what the old transition bookkeeping existed to police. It is
				// also what closes the re-request window atomically: once the id is out
				// of the map, `schedule_task_impl`'s in-place update misses and the
				// request queues itself through the ordinary path, so no request can be
				// recorded that this handler will not see.
				let Some(task_meta) = schedule.take_running(id) else {
					warn!("Completed task {} not found in running queue", id);
					continue;
				};

				// Before the cron test on purpose: a cron task re-requested as a
				// one-shot runs now, and since `rerun_meta` keeps the `cron` field the
				// run after it resumes recurring.
				if task_meta.rerun_requested {
					// A keyed task re-requested while this run was in flight was only
					// updated in place — `schedule_task_impl` deliberately does not start
					// a second body next to the running one. This is where the requested
					// run happens, and `task_meta` *is* whatever that update wrote.
					info!("Task {} was re-requested while running; running again", id);
					let mut rerun_meta = task_meta;
					rerun_meta.next_at = None;
					rerun_meta.retry_count = 0;
					rerun_meta.rerun_requested = false;
					if let Err(e) = schedule.add_queue(id, rerun_meta).await {
						error!(
							"Failed to re-queue task {} after in-flight update: {} - task lost!",
							id, e
						);
					}
				} else if let Some(cron) = &task_meta.cron {
					match cron.next_execution(Timestamp::now()) {
						Ok(next_at) => {
							info!(
								"Recurring task {} completed, scheduling next execution at {}",
								id, next_at
							);
							let mut updated_meta = task_meta.clone();
							updated_meta.next_at = Some(next_at);
							// Persist the new `next_at` and keep the row `'P'`.
							if let Err(e) = schedule.store.update_task(id, &updated_meta).await {
								error!("Failed to update recurring task {} next_at: {}", id, e);
							}
							if let Err(e) = schedule.add_queue(id, updated_meta).await {
								error!(
									"Failed to reschedule recurring task {}: {} - task lost!",
									id, e
								);
							}
						}
						Err(e) => {
							error!(
								"Failed to calculate next execution for recurring task {}: {} - task will not reschedule",
								id, e
							);
							// Cannot reschedule, so finish it. Falls through to the
							// dependents release rather than `continue`-ing past it: the run
							// did complete, so its dependents are owed their release.
							if let Err(e) = schedule.store.finished(id, "").await {
								error!("Failed to mark task {} as finished: {}", id, e);
							}
						}
					}
				} else if let Err(e) = schedule.store.finished(id, "").await {
					// Removal from `tasks_running` already happened, so a failure here
					// strands nothing in memory. Recovery is the persisted row, which
					// stays `status='P'` and is re-queued by `load()` at next start.
					//
					// Known open race: a request landing after the take but during this
					// await queues itself through the ordinary path, and `mark_finished`
					// then stamps the row `'F'` underneath it — so a further re-request
					// for that key mints a *new* id and row, a crash during the rerun
					// loses it, and the rerun's own `finished` is a silent no-op. Closing
					// it needs a store-level re-open (a patch back to `status='P'`, or
					// making `finished` conditional on no successor).
					error!("Failed to mark task {} as finished: {}", id, e);
				}

				// Handle dependencies of finished task using atomic release method
				match schedule.release_dependents(id) {
					Ok(ready_to_spawn) => {
						for (dep_id, dep_task_meta) in ready_to_spawn {
							// Add to running queue before spawning
							match schedule.tasks_running.lock() {
								Ok(mut tasks_running) => {
									tasks_running.insert(dep_id, dep_task_meta.clone());
								}
								Err(poisoned) => {
									error!("Mutex poisoned: tasks_running (recovering)");
									poisoned.into_inner().insert(dep_id, dep_task_meta.clone());
								}
							}
							schedule.spawn_task(
								stat.clone(),
								dep_task_meta.task.clone(),
								dep_id,
								dep_task_meta,
							);
						}
					}
					Err(e) => {
						error!("Failed to release dependents of task {}: {}", id, e);
					}
				}
			}
		});

		// Handle scheduled tasks
		let schedule = self.clone();
		tokio::spawn(async move {
			loop {
				let is_empty = match schedule.tasks_scheduled.lock() {
					Ok(guard) => guard.is_empty(),
					Err(poisoned) => {
						error!("Mutex poisoned: tasks_scheduled (recovering)");
						poisoned.into_inner().is_empty()
					}
				};
				if is_empty {
					schedule.notify_schedule.notified().await;
				}
				let time = Timestamp::now();
				if let Some((timestamp, _id)) = loop {
					let mut tasks_scheduled = match schedule.tasks_scheduled.lock() {
						Ok(guard) => guard,
						Err(poisoned) => {
							error!("Mutex poisoned: tasks_scheduled (recovering)");
							poisoned.into_inner()
						}
					};
					if let Some((&(timestamp, id), _)) = tasks_scheduled.first_key_value() {
						let (timestamp, id) = (timestamp, id);
						if timestamp <= Timestamp::now() {
							debug!("Spawning task id {} (from schedule)", id);
							if let Some(task) = tasks_scheduled.remove(&(timestamp, id)) {
								let mut tasks_running = match schedule.tasks_running.lock() {
									Ok(guard) => guard,
									Err(poisoned) => {
										error!("Mutex poisoned: tasks_running (recovering)");
										poisoned.into_inner()
									}
								};
								tasks_running.insert(id, task.clone());
								schedule.spawn_task(state.clone(), task.task.clone(), id, task);
							} else {
								error!("Task disappeared while being removed from schedule");
								break None;
							}
						} else {
							break Some((timestamp, id));
						}
					} else {
						break None;
					}
				} {
					let diff = timestamp.0 - time.0;
					let wait =
						tokio::time::Duration::from_secs(u64::try_from(diff).unwrap_or_default());
					tokio::select! {
						() = tokio::time::sleep(wait) => (), () = schedule.notify_schedule.notified() => ()
					};
				}
			}
		});

		let schedule = self.clone();
		tokio::spawn(async move {
			// Only fatal failures reach here — `load` contains per-row ones itself.
			// A scheduler that loaded nothing must say so rather than start empty
			// and look healthy.
			if let Err(e) = schedule.load().await {
				error!("scheduler: failed to load persisted tasks: {}", e);
			}
		});
	}

	fn register_builder(
		&self,
		name: &'static str,
		builder: &'static TaskBuilder<S>,
	) -> ClResult<&Self> {
		let mut task_builders = self
			.task_builders
			.write()
			.map_err(|_| Error::Internal("task_builders RwLock poisoned".into()))?;
		task_builders.insert(name, Box::new(builder));
		Ok(self)
	}

	pub fn register<T: Task<S>>(&self) -> ClResult<&Self> {
		info!("Registering task type {}", T::kind());
		self.register_builder(T::kind(), &|id: TaskId, params: &str| T::build(id, params))?;
		Ok(self)
	}

	/// Create a builder for scheduling a task using the fluent API
	pub fn task(&self, task: Arc<dyn Task<S>>) -> TaskSchedulerBuilder<'_, S> {
		TaskSchedulerBuilder::new(self, task)
	}

	/// Internal method to schedule a task with all options
	/// This is the core implementation used by the builder pattern
	#[allow(clippy::too_many_arguments)]
	async fn schedule_task_impl(
		&self,
		task: Arc<dyn Task<S>>,
		key: Option<&str>,
		next_at: Option<Timestamp>,
		deps: Option<Vec<TaskId>>,
		retry: Option<RetryPolicy>,
		cron: Option<CronSchedule>,
		run_on_startup: bool,
	) -> ClResult<TaskId> {
		// Look up any existing task by key once; reuse for both the
		// run_on_startup decision and the dedup branch below.
		let existing = if let Some(k) = key { self.store.find_by_key(k).await? } else { None };

		// Resolve effective next_at, factoring in run_on_startup for cron tasks.
		let effective_next_at = if run_on_startup && cron.is_some() {
			match &existing {
				Some((_existing_id, existing_data)) => {
					// Task exists from a previous run. If its persisted
					// next_at has already passed (or is missing), we missed
					// a run while down — fire now. Otherwise honor the
					// persisted future schedule.
					match existing_data.next_at {
						Some(persisted) if persisted > Timestamp::now() => next_at,
						_ => Some(Timestamp::now()),
					}
				}
				None => Some(Timestamp::now()), // fresh registration → run now
			}
		} else {
			next_at
		};

		let task_meta = TaskMeta {
			task: task.clone(),
			next_at: effective_next_at,
			deps: deps.clone().unwrap_or_default(),
			retry_count: 0,
			retry,
			cron,
			rerun_requested: false,
		};

		// Check if a task with this key already exists (key-based deduplication)
		if let Some(key) = key
			&& let Some((existing_id, existing_data)) = existing
		{
			let new_serialized = task.serialize();
			let existing_serialized = existing_data.input.as_ref();
			let params_changed = new_serialized != existing_serialized;

			if params_changed {
				info!(
					"Updating recurring task '{}' (id={}) - parameters changed",
					key, existing_id
				);
				debug!("  Old params: {}", existing_serialized);
				debug!("  New params: {}", new_serialized);
			} else {
				info!(
					"Recurring task '{}' already exists with identical parameters (id={})",
					key, existing_id
				);
			}

			// The running check comes before the identical/changed split, not inside
			// the changed branch: a re-request with *identical* parameters is the
			// common shape — `IndexDocumentTask { tn_id, file_id }` serializes the
			// same way on every edit of the same file — and carries exactly as much
			// "run it again" intent as a changed one.
			//
			// A *running* task is updated in place and left alone. Going through
			// `remove_from_queues` first would take it out of `tasks_running`, the
			// very map `add_queue`'s already-running check consults, so the check
			// would miss and a second body would start alongside the one still
			// executing — for `core.db_maintenance:manual`, two concurrent VACUUMs
			// and two `tx_finish` events for one task id.
			//
			// The run in flight then reschedules itself from the new parameters —
			// but only if it is a *cron* task. A keyed one-shot takes the
			// `store.finished` branch instead, so the requested run would never
			// happen; those mark the running entry `rerun_requested` and let the
			// finish handler re-queue.
			//
			// The guard is confined to this block: it is not `Send`, so holding it
			// across the `await` below would make the whole handler non-`Send`.
			let was_running = {
				let mut running = lock!(self.tasks_running, "tasks_running")?;
				match running.get_mut(&existing_id) {
					Some(existing_meta) => {
						debug!("Task {} is running; updating metadata in place", existing_id);
						let rerun = existing_meta.rerun_requested || task_meta.cron.is_none();
						*existing_meta = task_meta.clone();
						existing_meta.rerun_requested = rerun;
						true
					}
					None => false,
				}
			};
			if was_running {
				self.store.update_task(existing_id, &task_meta).await?;
				return Ok(existing_id);
			}

			if params_changed {
				self.remove_from_queues(existing_id)?;
			}

			// Update the task in database with the current parameters, cron and
			// next_at (any of which may differ from what is stored).
			self.store.update_task(existing_id, &task_meta).await?;

			// Ensure the task is queued — it may be loaded from the DB but not yet
			// in a queue — with the updated parameters.
			self.add_queue(existing_id, task_meta).await?;

			return Ok(existing_id);
		}

		// No existing task - create new one
		let id = self.store.add(&task_meta, key).await?;
		self.add_queue(id, task_meta).await
	}

	pub async fn add(&self, task: Arc<dyn Task<S>>) -> ClResult<TaskId> {
		self.task(task).now().await
	}

	pub async fn add_queue(&self, id: TaskId, task_meta: TaskMeta<S>) -> ClResult<TaskId> {
		debug_assert!(
			!task_meta.rerun_requested,
			"a queued task must not carry a pending rerun request"
		);
		// If task is already running, update its metadata (especially for cron updates)
		// but don't add to scheduled queue (it will reschedule on completion)
		{
			let mut running = lock!(self.tasks_running, "tasks_running")?;
			if let Some(existing_meta) = running.get_mut(&id) {
				debug!(
					"Task {} is already running, updating metadata (will reschedule on completion)",
					id
				);
				// Update the running task's metadata so it has the latest cron schedule.
				// The rerun flag records a request that has not run yet, so a metadata
				// refresh must carry it across rather than drop it.
				let rerun = existing_meta.rerun_requested;
				*existing_meta = task_meta;
				existing_meta.rerun_requested = rerun;
				return Ok(id);
			}
		}

		// Remove from other queues if present (prevents duplicate entries with different timestamps)
		{
			let mut scheduled = lock!(self.tasks_scheduled, "tasks_scheduled")?;
			if let Some(key) = scheduled
				.iter()
				.find(|((_, tid), _)| *tid == id)
				.map(|((ts, tid), _)| (*ts, *tid))
			{
				scheduled.remove(&key);
				debug!("Removed existing scheduled entry for task {} before re-queueing", id);
			}
		}
		{
			let mut waiting = lock!(self.tasks_waiting, "tasks_waiting")?;
			if waiting.remove(&id).is_some() {
				debug!("Removed existing waiting entry for task {} before re-queueing", id);
			}
		}

		let deps = task_meta.deps.clone();

		// VALIDATION: Tasks with dependencies should NEVER be in tasks_scheduled
		if !deps.is_empty() && task_meta.next_at.is_some() {
			warn!(
				"Task {} has both dependencies and scheduled time - ignoring next_at, placing in waiting queue",
				id
			);
			// Force to tasks_waiting instead
			lock!(self.tasks_waiting, "tasks_waiting")?.insert(id, task_meta);
			debug!("Task {} is waiting for {:?}", id, &deps);
			for dep in &deps {
				lock!(self.task_dependents, "task_dependents")?
					.entry(*dep)
					.or_default()
					.push(id);
			}

			self.check_and_resolve_completed_deps(id, &deps).await?;
			return Ok(id);
		}

		if deps.is_empty() && task_meta.next_at.unwrap_or(Timestamp(0)) < Timestamp::now() {
			debug!("Spawning task {}", id);
			lock!(self.tasks_scheduled, "tasks_scheduled")?.insert((Timestamp(0), id), task_meta);
			self.notify_schedule.notify_one();
		} else if let Some(next_at) = task_meta.next_at {
			debug!("Scheduling task {} for {}", id, next_at);
			lock!(self.tasks_scheduled, "tasks_scheduled")?.insert((next_at, id), task_meta);
			self.notify_schedule.notify_one();
		} else {
			lock!(self.tasks_waiting, "tasks_waiting")?.insert(id, task_meta);
			debug!("Task {} is waiting for {:?}", id, &deps);
			for dep in &deps {
				lock!(self.task_dependents, "task_dependents")?
					.entry(*dep)
					.or_default()
					.push(id);
			}

			self.check_and_resolve_completed_deps(id, &deps).await?;
		}
		Ok(id)
	}

	/// After registering deps, check if any completed in the meantime.
	/// If all deps are satisfied, move the task from waiting → scheduled.
	async fn check_and_resolve_completed_deps(&self, id: TaskId, deps: &[TaskId]) -> ClResult<()> {
		let completed_deps = self.store.find_completed_deps(deps).await?;
		if completed_deps.is_empty() {
			return Ok(());
		}
		let mut waiting = lock!(self.tasks_waiting, "tasks_waiting")?;
		if let Some(task_meta) = waiting.get_mut(&id) {
			for dep in &completed_deps {
				task_meta.deps.retain(|d| *d != *dep);
			}
			if task_meta.deps.is_empty()
				&& let Some(ready_task) = waiting.remove(&id)
			{
				drop(waiting);
				let mut dependents = lock!(self.task_dependents, "task_dependents")?;
				for dep in deps {
					if let Some(dep_list) = dependents.get_mut(dep) {
						dep_list.retain(|d| *d != id);
						if dep_list.is_empty() {
							dependents.remove(dep);
						}
					}
				}
				drop(dependents);
				debug!("Task {} deps already completed, scheduling immediately", id);
				lock!(self.tasks_scheduled, "tasks_scheduled")?
					.insert((Timestamp(0), id), ready_task);
				self.notify_schedule.notify_one();
			}
		}
		Ok(())
	}

	/// Remove a task from all internal queues (waiting, scheduled, running)
	/// Returns the removed TaskMeta if found
	fn remove_from_queues(&self, task_id: TaskId) -> ClResult<Option<TaskMeta<S>>> {
		// Try tasks_waiting
		if let Some(task_meta) = lock!(self.tasks_waiting, "tasks_waiting")?.remove(&task_id) {
			debug!("Removed task {} from waiting queue for update", task_id);
			return Ok(Some(task_meta));
		}

		// Try tasks_scheduled (need to find by task_id in BTreeMap)
		{
			let mut scheduled = lock!(self.tasks_scheduled, "tasks_scheduled")?;
			if let Some(key) = scheduled
				.iter()
				.find(|((_, id), _)| *id == task_id)
				.map(|((ts, id), _)| (*ts, *id))
				&& let Some(task_meta) = scheduled.remove(&key)
			{
				debug!("Removed task {} from scheduled queue for update", task_id);
				return Ok(Some(task_meta));
			}
		}

		// Try tasks_running (should rarely happen, but handle it)
		if let Some(task_meta) = lock!(self.tasks_running, "tasks_running")?.remove(&task_id) {
			warn!("Removed task {} from running queue during update", task_id);
			return Ok(Some(task_meta));
		}

		Ok(None)
	}

	/// Release all dependent tasks of a completed task
	/// This method safely handles dependency cleanup and spawning
	fn release_dependents(
		&self,
		completed_task_id: TaskId,
	) -> ClResult<Vec<(TaskId, TaskMeta<S>)>> {
		// Get list of dependents (atomic removal to prevent re-processing)
		let dependents = {
			let mut deps_map = lock!(self.task_dependents, "task_dependents")?;
			deps_map.remove(&completed_task_id).unwrap_or_default()
		};

		if dependents.is_empty() {
			return Ok(Vec::new()); // No dependents to release
		}

		debug!("Releasing {} dependents of completed task {}", dependents.len(), completed_task_id);

		let mut ready_to_spawn = Vec::new();

		// For each dependent, check and remove dependency
		for dependent_id in dependents {
			// Try tasks_waiting first (most common case for dependent tasks)
			{
				let mut waiting = lock!(self.tasks_waiting, "tasks_waiting")?;
				if let Some(task_meta) = waiting.get_mut(&dependent_id) {
					// Remove the completed task from dependencies
					task_meta.deps.retain(|x| *x != completed_task_id);

					// If all dependencies are cleared, remove and queue for spawning
					if task_meta.deps.is_empty() {
						if let Some(task_to_spawn) = waiting.remove(&dependent_id) {
							debug!(
								"Dependent task {} ready to spawn (all dependencies cleared)",
								dependent_id
							);
							ready_to_spawn.push((dependent_id, task_to_spawn));
						}
					} else {
						debug!(
							"Task {} still has {} remaining dependencies",
							dependent_id,
							task_meta.deps.len()
						);
					}
					continue;
				}
			}

			// Try tasks_scheduled if not in waiting (shouldn't happen with validation, but be defensive)
			{
				let mut scheduled = lock!(self.tasks_scheduled, "tasks_scheduled")?;
				if let Some(scheduled_key) = scheduled
					.iter()
					.find(|((_, id), _)| *id == dependent_id)
					.map(|((ts, id), _)| (*ts, *id))
				{
					if let Some(task_meta) = scheduled.get_mut(&scheduled_key) {
						task_meta.deps.retain(|x| *x != completed_task_id);
						let remaining = task_meta.deps.len();
						if remaining == 0 {
							debug!(
								"Task {} in scheduled queue has no remaining dependencies",
								dependent_id
							);
						} else {
							debug!(
								"Task {} in scheduled queue has {} remaining dependencies",
								dependent_id, remaining
							);
						}
					}
					continue;
				}
			}

			// Task not found in any queue
			warn!(
				"Dependent task {} of completed task {} not found in any queue",
				dependent_id, completed_task_id
			);
		}

		Ok(ready_to_spawn)
	}

	/// Re-queue every pending task the store holds.
	///
	/// Per-row failures are contained: an unregistered task kind, a malformed
	/// `retry` column or a rejected `add_queue` costs exactly that one row. The
	/// store's ordering is not partitioned by kind, so propagating out of the loop
	/// would let one bad row — a task kind an older build persisted and this one
	/// does not register — discard every ACME renewal, action fanout and email
	/// task behind it.
	///
	/// Only genuinely fatal failures propagate: the store read itself, and a
	/// poisoned `task_builders` lock.
	async fn load(&self) -> ClResult<()> {
		let tasks = self.store.load().await?;
		debug!("Loaded {} tasks from store", tasks.len());
		let (mut seen, mut queued, mut skipped) = (0usize, 0usize, 0usize);
		for t in tasks {
			if !matches!(t.status, TaskStatus::Pending) {
				continue;
			}
			seen += 1;
			let (id, kind) = (t.id, t.kind.clone());
			match self.load_one(t).await {
				Ok(()) => queued += 1,
				Err(e) => {
					skipped += 1;
					error!("scheduler: skipping persisted task {} ({}): {}", id, kind, e);
				}
			}
		}
		info!("scheduler: loaded {} pending tasks, {} queued, {} skipped", seen, queued, skipped);
		Ok(())
	}

	/// Re-queue one persisted task. Every failure here is per-row — see [`Self::load`].
	async fn load_one(&self, t: TaskData) -> ClResult<()> {
		debug!("Loading task {} {}", t.id, t.kind);
		let task = {
			let builder_map = self
				.task_builders
				.read()
				.map_err(|_| Error::Internal("task_builders RwLock poisoned".into()))?;
			let builder = builder_map
				.get(t.kind.as_ref())
				.ok_or(Error::Internal(format!("task builder not registered: {}", t.kind)))?;
			builder(t.id, &t.input)?
		};
		let (retry_count, retry) = match t.retry_data {
			Some(retry_str) => {
				let (retry_count, retry_min, retry_max, retry_times) = retry_str
					.split(',')
					.collect_tuple()
					.ok_or(Error::Internal("invalid retry policy format".into()))?;
				let retry_count: u16 = retry_count
					.parse()
					.map_err(|_| Error::Internal("retry count must be u16".into()))?;
				let retry = RetryPolicy {
					wait_min_max: (
						retry_min
							.parse()
							.map_err(|_| Error::Internal("retry_min must be u64".into()))?,
						retry_max
							.parse()
							.map_err(|_| Error::Internal("retry_max must be u64".into()))?,
					),
					times: retry_times
						.parse()
						.map_err(|_| Error::Internal("retry times must be u64".into()))?,
				};
				debug!("Loaded retry policy: {:?}", retry);
				(retry_count, Some(retry))
			}
			_ => (0, None),
		};
		// Parse cron data if present. A persisted expression that no longer parses
		// is logged rather than dropped silently: the task keeps its `next_at` but
		// stops recurring, which is otherwise invisible.
		let cron = match t.cron_data.as_deref() {
			Some(cron_str) => match CronSchedule::parse(cron_str) {
				Ok(cron) => Some(cron),
				Err(e) => {
					error!(
						"scheduler: persisted task {} ({}) has an unusable cron expression \
						 {:?} ({}); it will not recur",
						t.id, t.kind, cron_str, e
					);
					None
				}
			},
			None => None,
		};

		let task_meta = TaskMeta {
			task,
			next_at: t.next_at,
			deps: t.deps.into(),
			retry_count,
			retry,
			cron,
			rerun_requested: false,
		};
		self.add_queue(t.id, task_meta).await.map(|_| ())
	}

	/// Take `id`'s entry out of `tasks_running`, metadata and rerun flag together.
	///
	/// Atomicity is the point. [`Self::schedule_task_impl`] records a rerun
	/// request by writing into this very entry, under this very lock, and only
	/// while the id is in the map — so removing the entry *is* the act of closing
	/// the request window, and whatever came with it is what the caller must
	/// honour.
	///
	/// Recovers from a poisoned lock rather than propagating: the finish handler
	/// has no error channel, and dropping the event strands the task.
	fn take_running(&self, id: TaskId) -> Option<TaskMeta<S>> {
		match self.tasks_running.lock() {
			Ok(mut running) => running.remove(&id),
			Err(poisoned) => {
				error!("Mutex poisoned: tasks_running (recovering)");
				poisoned.into_inner().remove(&id)
			}
		}
	}

	/// Drop any in-flight "re-run when this finishes" request for `id`.
	///
	/// Both terminal paths in [`Self::spawn_task`] go through here, so they cannot
	/// drift apart again. The retry path does not: its entry is already out of the
	/// map, so it clears the flag on the metadata it carries instead. Recovers
	/// from a poisoned lock rather than propagating: a stale flag left set re-runs
	/// a task that must not run again.
	fn clear_rerun_request(&self, id: TaskId) {
		let mut running = match self.tasks_running.lock() {
			Ok(guard) => guard,
			Err(poisoned) => {
				error!("Mutex poisoned: tasks_running (recovering)");
				poisoned.into_inner()
			}
		};
		if let Some(meta) = running.get_mut(&id) {
			meta.rerun_requested = false;
		}
	}

	fn spawn_task(&self, state: S, task: Arc<dyn Task<S>>, id: TaskId, task_meta: TaskMeta<S>) {
		let tx_finish = self.tx_finish.clone();
		let store = self.store.clone();
		let scheduler = self.clone();
		//let state = self.state.clone();
		tokio::spawn(async move {
			match task.run(&state).await {
				Ok(()) => {
					debug!("Task {} completed successfully", id);
					tx_finish.send(id).unwrap_or(());
				}
				Err(e) => {
					let is_retryable = e.is_retryable();
					if let Some(retry_policy) = &task_meta.retry {
						if is_retryable && retry_policy.should_retry(task_meta.retry_count) {
							let backoff = retry_policy.calculate_backoff(task_meta.retry_count);
							let next_at = Timestamp::from_now(backoff.cast_signed());

							info!(
								"Task {} failed (attempt {}/{}). Scheduling retry in {} seconds: {}",
								id,
								task_meta.retry_count + 1,
								retry_policy.times,
								backoff,
								e
							);

							// Update database with error and reschedule
							if let Err(err) =
								store.update_task_error(id, &e.to_string(), Some(next_at)).await
							{
								error!(
									"Failed to persist error for task {}: {} - retry not durable",
									id, err
								);
							}

							task.on_attempt_failed(&state, task_meta.retry_count, &e.to_string())
								.await;

							// Remove from running tasks (we're not sending finish
							// event), and take the *current* metadata out with it:
							// `task_meta` is the snapshot captured when this run was
							// spawned, and a keyed re-request may have updated the
							// task in place since. Retrying from the snapshot would
							// run the old parameters while the persisted row holds
							// the new ones. Falls back to the snapshot only if the
							// entry is already gone. The removal also recovers any
							// pending rerun request along with the metadata, and the
							// retry satisfies it — it carries the new parameters.
							let current_meta = match scheduler.tasks_running.lock() {
								Ok(mut tasks_running) => tasks_running.remove(&id),
								Err(poisoned) => {
									error!("Mutex poisoned: tasks_running (recovering)");
									poisoned.into_inner().remove(&id)
								}
							};

							// Re-queue task with incremented retry count. Counted off
							// the snapshot, not off `current_meta`: an in-place update
							// carries `retry_count: 0`, and taking that as the base
							// would hand the task a fresh retry budget on every
							// re-request.
							let mut retry_meta = current_meta.unwrap_or_else(|| task_meta.clone());
							retry_meta.retry_count = task_meta.retry_count + 1;
							retry_meta.next_at = Some(next_at);
							// The retry carries the new parameters, so an in-flight
							// re-request is already satisfied — left set, the flag would
							// run the task a second time once the retry ends. Cleared on
							// the metadata rather than through
							// `Scheduler::clear_rerun_request`: the entry is already out
							// of the map above, so a map-clearing call would be a no-op.
							retry_meta.rerun_requested = false;

							if let Err(err) = scheduler.add_queue(id, retry_meta).await {
								error!(
									"Failed to queue retry for task {}: {} - task lost!",
									id, err
								);
							}
						} else {
							// Max retries exhausted OR error is permanent
							if is_retryable {
								error!(
									"Task {} failed after {} retries: {}",
									id, task_meta.retry_count, e
								);
							} else {
								error!("Task {} failed permanently (non-retryable): {}", id, e);
							}
							// A cron task is not finished when a run fails — the finish
							// handler below reschedules it, and the row must stay `'P'`
							// or `find_by_key` and `load()` will lose it and mint a
							// duplicate on the next boot, re-firing `run_on_startup`.
							// A one-shot keeps `next_at: None` and so keeps `'E'`: a
							// fresh request there should mint a fresh row.
							let next_at = task_meta
								.cron
								.as_ref()
								.and_then(|c| c.next_execution(Timestamp::now()).ok());
							if let Err(err) =
								store.update_task_error(id, &e.to_string(), next_at).await
							{
								error!(
									"Failed to persist error for task {}: {} - retry not durable",
									id, err
								);
							}
							task.on_failed(&state, task_meta.retry_count, &e.to_string()).await;
							// Cleared for a different reason than in the retry branch
							// above, and the difference matters: `on_failed` has just
							// run its irreversible cleanup — `ActionVerifierTask` marks
							// the action `'F'`, "failed after retry exhaustion". Left
							// set, the finish handler would re-queue the task with
							// `retry_count: 0` against a row already declared
							// permanently failed, and every re-request landing during a
							// final attempt would renew the whole retry budget. A caller
							// that still wants the work done schedules a fresh task.
							scheduler.clear_rerun_request(id);
							tx_finish.send(id).unwrap_or(());
						}
					} else {
						// No retry policy - fail immediately. Terminal in the same
						// sense as the exhausted branch above, so the same reasoning
						// applies to the rerun flag.
						error!("Task {} failed: {}", id, e);
						// Same as the exhausted branch above: a cron task's row must
						// stay `'P'` with a live `next_at`, or the next boot cannot
						// find it and mints a duplicate.
						let next_at = task_meta
							.cron
							.as_ref()
							.and_then(|c| c.next_execution(Timestamp::now()).ok());
						if let Err(err) = store.update_task_error(id, &e.to_string(), next_at).await
						{
							error!(
								"Failed to persist error for task {}: {} - retry not durable",
								id, err
							);
						}
						task.on_failed(&state, 0, &e.to_string()).await;
						scheduler.clear_rerun_request(id);
						tx_finish.send(id).unwrap_or(());
					}
				}
			}
		});
	}

	/// Get health status of the scheduler
	/// Returns information about tasks in each queue and detects anomalies
	pub async fn health_check(&self) -> ClResult<SchedulerHealth> {
		let waiting_count = lock!(self.tasks_waiting, "tasks_waiting")?.len();
		let scheduled_count = lock!(self.tasks_scheduled, "tasks_scheduled")?.len();
		let running_count = lock!(self.tasks_running, "tasks_running")?.len();
		let dependents_count = lock!(self.task_dependents, "task_dependents")?.len();

		// Check for anomalies
		let mut stuck_tasks = Vec::new();
		let mut tasks_with_missing_deps = Vec::new();

		// Check tasks_waiting for tasks with no dependencies (stuck)
		{
			let waiting = lock!(self.tasks_waiting, "tasks_waiting")?;
			let _deps_map = lock!(self.task_dependents, "task_dependents")?;

			for (id, task_meta) in waiting.iter() {
				if task_meta.deps.is_empty() {
					stuck_tasks.push(*id);
					warn!("SCHEDULER HEALTH: Task {} in waiting with no dependencies", id);
				} else {
					// Check if all dependencies still exist. Warning-only, and it can
					// now fire for a dep that is merely mid-finish-handling: the finish
					// handler takes the entry out of `tasks_running` before awaiting
					// `store.finished`, so the window this probe can land in spans that
					// await instead of ending just before it.
					for dep in &task_meta.deps {
						let dep_exists = waiting.contains_key(dep)
							|| self.tasks_running.lock().ok().is_some_and(|r| r.contains_key(dep))
							|| self
								.tasks_scheduled
								.lock()
								.ok()
								.is_some_and(|s| s.iter().any(|((_, task_id), _)| task_id == dep));

						if !dep_exists {
							tasks_with_missing_deps.push((*id, *dep));
							warn!(
								"SCHEDULER HEALTH: Task {} depends on non-existent task {}",
								id, dep
							);
						}
					}
				}
			}
		}

		Ok(SchedulerHealth {
			waiting: waiting_count,
			scheduled: scheduled_count,
			running: running_count,
			dependents: dependents_count,
			stuck_tasks,
			tasks_with_missing_deps,
		})
	}
}

/// Health status of the scheduler
#[derive(Debug, Clone)]
pub struct SchedulerHealth {
	/// Number of tasks waiting for dependencies
	pub waiting: usize,
	/// Number of tasks scheduled for future execution
	pub scheduled: usize,
	/// Number of tasks currently running
	pub running: usize,
	/// Number of task entries in dependents map
	pub dependents: usize,
	/// IDs of tasks with no dependencies but still in waiting queue
	pub stuck_tasks: Vec<TaskId>,
	/// Pairs of (task_id, missing_dependency_id) where dependency doesn't exist
	pub tasks_with_missing_deps: Vec<(TaskId, TaskId)>,
}

#[cfg(test)]
mod tests {
	use super::*;
	use serde::{Deserialize, Serialize};

	type State = Arc<Mutex<Vec<u8>>>;

	#[derive(Debug, Serialize, Deserialize)]
	struct TestTask {
		num: u8,
	}

	impl TestTask {
		pub fn new(num: u8) -> Arc<Self> {
			Arc::new(Self { num })
		}
	}

	#[async_trait]
	impl Task<State> for TestTask {
		fn kind() -> &'static str {
			"test"
		}

		fn build(_id: TaskId, ctx: &str) -> ClResult<Arc<dyn Task<State>>> {
			let num: u8 = ctx
				.parse()
				.map_err(|_| Error::Internal("test task context must be u8".into()))?;
			let task = TestTask::new(num);
			Ok(task)
		}

		fn serialize(&self) -> String {
			self.num.to_string()
		}

		fn kind_of(&self) -> &'static str {
			"test"
		}

		async fn run(&self, state: &State) -> ClResult<()> {
			info!("Running task {}", self.num);
			tokio::time::sleep(std::time::Duration::from_millis(200 * u64::from(self.num))).await;
			info!("Completed task {}", self.num);
			state.lock().unwrap().push(self.num);
			Ok(())
		}
	}

	#[derive(Debug, Clone)]
	struct FailingTask {
		id: u8,
		fail_count: u8,
		attempt: Arc<Mutex<u8>>,
		/// `attempt` argument of every `on_attempt_failed` call, in order.
		retried: Arc<Mutex<Vec<u16>>>,
		/// `attempts` argument of every (terminal) `on_failed` call, in order.
		gave_up: Arc<Mutex<Vec<u16>>>,
	}

	impl FailingTask {
		pub fn new(id: u8, fail_count: u8) -> Arc<Self> {
			Arc::new(Self {
				id,
				fail_count,
				attempt: Arc::new(Mutex::new(0)),
				retried: Arc::new(Mutex::new(Vec::new())),
				gave_up: Arc::new(Mutex::new(Vec::new())),
			})
		}
	}

	#[async_trait]
	impl Task<State> for FailingTask {
		fn kind() -> &'static str {
			"failing"
		}

		fn build(_id: TaskId, ctx: &str) -> ClResult<Arc<dyn Task<State>>> {
			let parts: Vec<&str> = ctx.split(',').collect();
			if parts.len() != 2 {
				return Err(Error::Internal("failing task context must have 2 parts".into()));
			}
			let id: u8 = parts[0]
				.parse()
				.map_err(|_| Error::Internal("failing task id must be u8".into()))?;
			let fail_count: u8 = parts[1]
				.parse()
				.map_err(|_| Error::Internal("failing task fail_count must be u8".into()))?;
			Ok(FailingTask::new(id, fail_count))
		}

		fn serialize(&self) -> String {
			format!("{},{}", self.id, self.fail_count)
		}

		fn kind_of(&self) -> &'static str {
			"failing"
		}

		async fn run(&self, state: &State) -> ClResult<()> {
			let mut attempt = self.attempt.lock().unwrap();
			*attempt += 1;
			let current_attempt = *attempt;

			info!("FailingTask {} - attempt {}/{}", self.id, current_attempt, self.fail_count + 1);

			if current_attempt <= self.fail_count {
				error!("FailingTask {} failed on attempt {}", self.id, current_attempt);
				return Err(Error::ServiceUnavailable(format!("Task {} failed", self.id)));
			}

			info!("FailingTask {} succeeded on attempt {}", self.id, current_attempt);
			state.lock().unwrap().push(self.id);
			Ok(())
		}

		async fn on_attempt_failed(&self, _state: &State, attempt: u16, _last_error: &str) {
			self.retried.lock().unwrap().push(attempt);
		}

		async fn on_failed(&self, _state: &State, attempts: u16, _last_error: &str) {
			self.gave_up.lock().unwrap().push(attempts);
		}
	}

	#[test]
	fn test_calculate_backoff() {
		let policy = RetryPolicy::new((10, 43200), 50);
		assert_eq!(policy.calculate_backoff(0), 10);
		assert_eq!(policy.calculate_backoff(1), 20);
		assert_eq!(policy.calculate_backoff(4), 160);
		// Past the cap.
		assert_eq!(policy.calculate_backoff(20), 43200);

		// `times` is a `u16` and `new` is public, so an attempt count wider than
		// the shift is reachable. It must saturate to `max`, not panic.
		let wide = RetryPolicy::new((10, 43200), 200);
		assert_eq!(wide.calculate_backoff(63), 43200);
		assert_eq!(wide.calculate_backoff(64), 43200);
		assert_eq!(wide.calculate_backoff(200), 43200);
		assert_eq!(wide.calculate_backoff(u16::MAX), 43200);
	}

	#[tokio::test]
	pub async fn test_scheduler() {
		let _ = tracing_subscriber::fmt().try_init();

		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		let _task1 = TestTask::new(1);
		let task2 = TestTask::new(1);
		let task3 = TestTask::new(1);

		let task2_id = scheduler.task(task2).schedule_after(2).schedule().await.unwrap();
		let task3_id = scheduler.add(task3).await.unwrap();
		scheduler
			.task(TestTask::new(1))
			.depend_on(vec![task2_id, task3_id])
			.schedule()
			.await
			.unwrap();

		tokio::time::sleep(std::time::Duration::from_secs(4)).await;
		let task4 = TestTask::new(1);
		let task5 = TestTask::new(1);
		scheduler.task(task4).schedule_after(2).schedule().await.unwrap();
		scheduler.task(task5).schedule_after(1).schedule().await.unwrap();

		tokio::time::sleep(std::time::Duration::from_secs(3)).await;

		let st = state.lock().unwrap();
		info!("res: {}", st.len());
		let str_vec = st.iter().map(std::string::ToString::to_string).collect::<Vec<String>>();
		assert_eq!(str_vec.join(":"), "1:1:1:1:1");
	}

	#[tokio::test]
	pub async fn test_retry_with_backoff() {
		let _ = tracing_subscriber::fmt().try_init();

		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<FailingTask>().unwrap();

		// Create a task that fails twice, then succeeds
		// With retry policy: min=1s, max=3600s, max_attempts=3
		let failing_task = FailingTask::new(42, 2);
		let retried = failing_task.retried.clone();
		let gave_up = failing_task.gave_up.clone();
		let retry_policy = RetryPolicy { wait_min_max: (1, 3600), times: 3 };

		scheduler.task(failing_task).with_retry(retry_policy).schedule().await.unwrap();

		// Wait for retries: 1s (1st fail) + 1s (2nd fail) + time for success
		// First attempt: immediate fail
		// Wait 1s (min backoff)
		// Second attempt: fail
		// Wait 2s (min * 2)
		// Third attempt: success
		tokio::time::sleep(std::time::Duration::from_secs(6)).await;

		{
			let st = state.lock().unwrap();
			assert_eq!(st.len(), 1, "Task should have succeeded after retries");
			assert_eq!(st[0], 42);
		}

		// A task that fails before it succeeds must be able to tell the difference
		// between "failed, retrying" and "failed for good": `on_attempt_failed` sees
		// each retried failure with its zero-based attempt index, `on_failed` none.
		assert_eq!(
			retried.lock().unwrap().as_slice(),
			&[0, 1],
			"Both retried failures should report their zero-based attempt index"
		);
		assert!(gave_up.lock().unwrap().is_empty(), "on_failed is only for terminal failures");
	}

	// ===== Builder Pattern Tests =====

	#[tokio::test]
	pub async fn test_builder_simple_schedule() {
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Test basic builder usage: .now()
		let task = TestTask::new(1);
		let id = scheduler.task(task).now().await.unwrap();

		assert!(id > 0, "Task ID should be positive");

		tokio::time::sleep(std::time::Duration::from_millis(500)).await;

		let st = state.lock().unwrap();
		assert_eq!(st.len(), 1, "Task should have executed");
		assert_eq!(st[0], 1);
	}

	#[tokio::test]
	pub async fn test_builder_with_key() {
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Test builder with key
		let task = TestTask::new(1);
		let _id = scheduler.task(task).key("my-task-key").now().await.unwrap();

		tokio::time::sleep(std::time::Duration::from_millis(500)).await;

		let st = state.lock().unwrap();
		assert_eq!(st.len(), 1);
		assert_eq!(st[0], 1);
	}

	#[tokio::test]
	pub async fn test_builder_with_delay() {
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Test builder with .after() convenience method
		let task = TestTask::new(1);
		let _id = scheduler
			.task(task)
			.after(1)  // 1 second delay
			.await
			.unwrap();

		// Should not have executed yet
		tokio::time::sleep(std::time::Duration::from_millis(500)).await;
		{
			let st = state.lock().unwrap();
			assert_eq!(st.len(), 0, "Task should not execute yet");
		}

		// Wait for execution (1 sec delay + 200ms task sleep + buffer)
		tokio::time::sleep(std::time::Duration::from_millis(800)).await;

		{
			let st = state.lock().unwrap();
			assert_eq!(st.len(), 1, "Task should have executed");
			assert_eq!(st[0], 1);
		}
	}

	#[tokio::test]
	pub async fn test_builder_with_dependencies() {
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Create first task (sleeps 200ms)
		let task1 = TestTask::new(1);
		let id1 = scheduler.task(task1).now().await.unwrap();

		// Create second task (sleeps 400ms)
		let task2 = TestTask::new(1);
		let id2 = scheduler.task(task2).now().await.unwrap();

		// Create third task that depends on first two (sleeps 600ms)
		let task3 = TestTask::new(1);
		let _id3 = scheduler.task(task3).depend_on(vec![id1, id2]).schedule().await.unwrap();

		// Wait for all tasks: task1 200ms, task2 400ms, task3 600ms = ~1200ms
		tokio::time::sleep(std::time::Duration::from_millis(1500)).await;

		let st = state.lock().unwrap();
		// Should have all three tasks in execution order: 1 finishes first (200ms), then 2 (200ms), then 3 (200ms after both)
		let str_vec = st.iter().map(std::string::ToString::to_string).collect::<Vec<String>>();
		assert_eq!(str_vec.join(":"), "1:1:1");
	}

	#[tokio::test]
	pub async fn test_builder_with_retry() {
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<FailingTask>().unwrap();

		// Create task using builder with retry policy
		let failing_task = FailingTask::new(55, 1); // Fails once, succeeds second time
		let retry_policy = RetryPolicy { wait_min_max: (1, 3600), times: 3 };

		let _id = scheduler.task(failing_task).with_retry(retry_policy).schedule().await.unwrap();

		// Wait for retry cycle: 1 fail + 1s wait + 1 success
		tokio::time::sleep(std::time::Duration::from_secs(3)).await;

		let st = state.lock().unwrap();
		assert_eq!(st.len(), 1);
		assert_eq!(st[0], 55);
	}

	#[tokio::test]
	pub async fn test_builder_with_automatic_retry() {
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<FailingTask>().unwrap();

		// Create task using builder with automatic retry (default policy)
		let failing_task = FailingTask::new(66, 1);
		let _id = scheduler.task(failing_task).with_automatic_retry().await.unwrap();

		// Wait for retry cycle with default policy (min=60s would be too long for test)
		// but we already tested retry logic thoroughly, just verify builder integration
		tokio::time::sleep(std::time::Duration::from_millis(500)).await;

		// The important part is that this compiles and integrates correctly
		let st = state.lock().unwrap();
		// With default policy (min=60s), task shouldn't succeed in test timeframe
		// Just verify builder chaining works
		let _ = st.len(); // Verify state is accessible, but don't assert on timeout-dependent result
	}

	#[tokio::test]
	pub async fn test_builder_fluent_chaining() {
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Create first dependencies
		let dep1 = scheduler.task(TestTask::new(1)).now().await.unwrap();
		let dep2 = scheduler.task(TestTask::new(1)).now().await.unwrap();

		// Test fluent chaining with multiple methods
		let retry_policy = RetryPolicy { wait_min_max: (1, 3600), times: 3 };

		let task = TestTask::new(1);
		let _id = scheduler
			.task(task)
			.key("complex-task")
			.schedule_after(0)  // Schedule immediately
			.depend_on(vec![dep1, dep2])
			.with_retry(retry_policy)
			.schedule()
			.await
			.unwrap();

		tokio::time::sleep(std::time::Duration::from_millis(800)).await;

		let st = state.lock().unwrap();
		// Should have all tasks: 20:10 (immediate deps) then 30 (after deps)
		let str_vec = st.iter().map(std::string::ToString::to_string).collect::<Vec<String>>();
		assert_eq!(str_vec.join(":"), "1:1:1");
	}

	#[tokio::test]
	pub async fn test_builder_backward_compatibility() {
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Test that old API still works
		let _id1 = scheduler.add(TestTask::new(1)).await.unwrap();

		// Test that new builder API works
		let _id2 = scheduler.task(TestTask::new(1)).now().await.unwrap();

		tokio::time::sleep(std::time::Duration::from_millis(800)).await;

		let st = state.lock().unwrap();
		// Both old and new API should have executed
		assert_eq!(st.len(), 2);
		let str_vec = st.iter().map(std::string::ToString::to_string).collect::<Vec<String>>();
		assert_eq!(str_vec.join(":"), "1:1");
	}

	// ===== Phase 2: Integration Tests - Real-world scenarios =====

	#[tokio::test]
	pub async fn test_builder_pipeline_scenario() {
		// Simulates: Task 1 -> Task 2 (depends on 1) -> Task 3 (depends on 2)
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Stage 1: Create initial task
		let id1 = scheduler.task(TestTask::new(1)).key("stage-1").now().await.unwrap();

		// Stage 2: Create task that depends on stage 1
		let id2 = scheduler.task(TestTask::new(1)).key("stage-2").after_task(id1).await.unwrap();

		// Stage 3: Create task that depends on stage 2
		let _id3 = scheduler.task(TestTask::new(1)).key("stage-3").after_task(id2).await.unwrap();

		// Wait for pipeline: 1(200ms) + 2(200ms) + 3(200ms) = 600ms
		tokio::time::sleep(std::time::Duration::from_millis(1200)).await;

		let st = state.lock().unwrap();
		// Should execute in order: 1, 2, 3
		let str_vec = st.iter().map(std::string::ToString::to_string).collect::<Vec<String>>();
		assert_eq!(str_vec.join(":"), "1:1:1");
	}

	#[tokio::test]
	pub async fn test_builder_multi_dependency_join() {
		// Simulates: Task 1 parallel with Task 2, then Task 3 waits for both
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Parallel tasks
		let id1 = scheduler.task(TestTask::new(1)).now().await.unwrap();
		let id2 = scheduler.task(TestTask::new(1)).now().await.unwrap();

		// Join task - waits for both
		let _id3 = scheduler
			.task(TestTask::new(1))
			.depend_on(vec![id1, id2])
			.schedule()
			.await
			.unwrap();

		tokio::time::sleep(std::time::Duration::from_secs(1)).await;

		let st = state.lock().unwrap();
		// 1 and 2 execute in parallel, then 3 executes after both
		let str_vec = st.iter().map(std::string::ToString::to_string).collect::<Vec<String>>();
		assert_eq!(str_vec.join(":"), "1:1:1");
	}

	#[tokio::test]
	pub async fn test_builder_scheduled_task_with_dependencies() {
		// Simulates: Task depends on earlier task AND is scheduled for future time
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Immediate task
		let dep_id = scheduler.task(TestTask::new(1)).now().await.unwrap();

		// Task that waits for dependency AND scheduled delay
		let ts = Timestamp::from_now(1);
		let _task_id = scheduler
			.task(TestTask::new(1))
			.schedule_at(ts)
			.depend_on(vec![dep_id])
			.schedule()
			.await
			.unwrap();

		// Wait for dependency to complete but before scheduled time
		tokio::time::sleep(std::time::Duration::from_millis(300)).await;
		{
			let st = state.lock().unwrap();
			assert_eq!(st.len(), 1); // Only dependency executed
		}

		// Wait for scheduled time (1s total from initial schedule)
		tokio::time::sleep(std::time::Duration::from_millis(800)).await;

		{
			let st = state.lock().unwrap();
			let str_vec = st.iter().map(std::string::ToString::to_string).collect::<Vec<String>>();
			assert_eq!(str_vec.join(":"), "1:1");
		}
	}

	#[tokio::test]
	pub async fn test_builder_mixed_features() {
		// Simulates: Complex real-world scenario with key, scheduling, deps, and retry
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();
		scheduler.register::<FailingTask>().unwrap();

		// Create initial tasks
		let id1 = scheduler.task(TestTask::new(1)).now().await.unwrap();

		// Create complex task: scheduled + depends on id1 + has key
		let _id2 = scheduler
			.task(TestTask::new(1))
			.key("critical-task")
			.schedule_after(0)
			.depend_on(vec![id1])
			.schedule()
			.await
			.unwrap();

		// Create task with retry
		let _id3 = scheduler
			.task(FailingTask::new(1, 0))  // Fails 0 times, succeeds immediately
			.key("retryable-task")
			.with_retry(RetryPolicy {
				wait_min_max: (1, 3600),
				times: 3,
			})
			.schedule()
			.await
			.unwrap();

		// Wait for tasks: id1 (200ms) + id2 (200ms after id1) + id3 (200ms) = ~600ms
		tokio::time::sleep(std::time::Duration::from_millis(1200)).await;

		let st = state.lock().unwrap();
		// All three tasks should execute
		let str_vec = st.iter().map(std::string::ToString::to_string).collect::<Vec<String>>();
		assert_eq!(str_vec.join(":"), "1:1:1");
	}

	#[tokio::test]
	pub async fn test_builder_builder_reuse_not_possible() {
		// Verify that builder is consumed (moved) and can't be reused
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let _state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);

		let task = TestTask::new(1);
		let builder = scheduler.task(task);

		// This would not compile if uncommented (builder is moved):
		// let _id1 = builder.now().await.unwrap();
		// let _id2 = builder.now().await.unwrap();  // Error: use of moved value

		// Can only call terminal method once
		let _id = builder.now().await.unwrap();
		// builder is now consumed, can't use again

		// Test passes if it compiles (verifying move semantics)
	}

	#[tokio::test]
	pub async fn test_builder_different_task_types() {
		// Test builder works with different task implementations
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();
		scheduler.register::<FailingTask>().unwrap();

		// Mix of different task types
		let _id1 = scheduler.task(TestTask::new(1)).key("test-task").now().await.unwrap();

		let _id2 = scheduler
			.task(FailingTask::new(1, 0))  // Won't fail
			.key("failing-task")
			.now()
			.await
			.unwrap();

		let _id3 = scheduler.task(TestTask::new(1)).now().await.unwrap();

		tokio::time::sleep(std::time::Duration::from_secs(1)).await;

		let st = state.lock().unwrap();
		assert_eq!(st.len(), 3);
		let str_vec = st.iter().map(std::string::ToString::to_string).collect::<Vec<String>>();
		// All three tasks should execute
		assert_eq!(str_vec.join(":"), "1:1:1");
	}

	// ===== Phase 3: Cron Placeholder Tests =====
	// These tests verify that cron methods compile and integrate
	// Actual cron functionality will be implemented in Phase 3

	#[tokio::test]
	pub async fn test_builder_cron_placeholder_syntax() {
		// Verify cron placeholder methods compile and chain properly
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Test that cron methods compile (they're no-ops in Phase 2)
		let task = TestTask::new(1);
		let _id = scheduler
			.task(task)
			.key("cron-task")
			.cron("0 9 * * *")  // 9 AM daily
			.schedule()
			.await
			.unwrap();

		// Cron scheduling - task will execute at the next scheduled time
		// For cron "0 9 * * *", that's tomorrow at 9 AM, so task won't execute in this test
		// This test just verifies the methods compile and chain properly
		tokio::time::sleep(std::time::Duration::from_millis(500)).await;

		let st = state.lock().unwrap();
		// Task is scheduled for future (9 AM), so it won't have executed yet
		// The important thing is that the cron methods compile and integrate
		assert_eq!(st.len(), 0); // Not executed yet since scheduled for future
	}

	#[tokio::test]
	pub async fn test_builder_daily_at_placeholder() {
		// Verify daily_at placeholder compiles and integrates
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Test that daily_at placeholder compiles
		let task = TestTask::new(1);
		let _id = scheduler
			.task(task)
			.key("daily-task")
			.daily_at(14, 30)  // 2:30 PM daily
			.schedule()
			.await
			.unwrap();

		// Daily_at scheduling - task will execute at the specified time (2:30 PM daily)
		// Task is scheduled for future, so it won't execute in this test
		tokio::time::sleep(std::time::Duration::from_millis(500)).await;

		let st = state.lock().unwrap();
		// Task is scheduled for future (2:30 PM), not executed yet
		// The important thing is that daily_at compiles and integrates properly
		assert_eq!(st.len(), 0);
	}

	#[tokio::test]
	pub async fn test_builder_weekly_at_placeholder() {
		// Verify weekly_at placeholder compiles and integrates
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Test that weekly_at placeholder compiles
		let task = TestTask::new(1);
		let _id = scheduler
			.task(task)
			.key("weekly-task")
			.weekly_at(1, 9, 0)  // Monday at 9 AM
			.schedule()
			.await
			.unwrap();

		// Weekly_at scheduling - task will execute on Monday at 9 AM
		// Task is scheduled for future, so it won't execute in this test
		tokio::time::sleep(std::time::Duration::from_millis(500)).await;

		let st = state.lock().unwrap();
		// Task is scheduled for future (Monday 9 AM), not executed yet
		// The important thing is that weekly_at compiles and integrates properly
		assert_eq!(st.len(), 0);
	}

	#[tokio::test]
	pub async fn test_builder_cron_with_retry() {
		// Verify cron methods chain with retry (future combined usage)
		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Test future usage pattern: cron + retry
		let task = TestTask::new(1);
		let _id = scheduler
			.task(task)
			.key("reliable-scheduled-task")
			.daily_at(2, 0)  // 2 AM daily
			.with_retry(RetryPolicy {
				wait_min_max: (60, 3600),
				times: 5,
			})
			.schedule()
			.await
			.unwrap();

		// Verify cron+retry chain compiles properly
		// Task is scheduled for 2 AM, so won't execute in this test
		tokio::time::sleep(std::time::Duration::from_millis(500)).await;

		let st = state.lock().unwrap();
		// Task scheduled for future (2 AM), not executed yet
		// The important thing is that chaining cron + retry works
		assert_eq!(st.len(), 0);
	}

	// ===== Cron Schedule Tests =====

	#[test]
	fn test_cron_to_string() {
		// Test that to_cron_string returns the original expression
		let cron = CronSchedule::parse("*/5 * * * *").unwrap();
		assert_eq!(cron.to_cron_string(), "*/5 * * * *");
	}

	#[tokio::test]
	pub async fn test_running_task_not_double_scheduled() {
		let _ = tracing_subscriber::fmt().try_init();

		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Create a task
		let task = TestTask::new(5); // Takes 1 second (5 * 200ms)
		let task_id = scheduler.add(task.clone()).await.unwrap();

		// Wait a bit for task to start running
		tokio::time::sleep(std::time::Duration::from_millis(100)).await;

		// Verify task is in tasks_running
		{
			let running = scheduler.tasks_running.lock().unwrap();
			assert!(running.contains_key(&task_id), "Task should be in running queue");
		}

		// Try to add the same task again via add_queue
		let task_meta = TaskMeta {
			task: task.clone(),
			next_at: Some(Timestamp::now()),
			deps: vec![],
			retry_count: 0,
			retry: None,
			cron: None,
			rerun_requested: false,
		};
		let result = scheduler.add_queue(task_id, task_meta).await;

		// Should succeed but not actually add to scheduled queue
		assert!(result.is_ok(), "add_queue should succeed");

		// Verify task is NOT in tasks_scheduled (only in running)
		{
			let sched_queue = scheduler.tasks_scheduled.lock().unwrap();
			let in_scheduled = sched_queue.iter().any(|((_, id), _)| *id == task_id);
			assert!(!in_scheduled, "Task should NOT be in scheduled queue while running");
		}

		// Wait for original task to complete
		tokio::time::sleep(std::time::Duration::from_secs(2)).await;

		// Verify task completed
		let st = state.lock().unwrap();
		assert_eq!(st.len(), 1, "Only one task execution should have occurred");
		assert_eq!(st[0], 5);
	}

	#[tokio::test]
	pub async fn test_running_task_metadata_updated() {
		let _ = tracing_subscriber::fmt().try_init();

		let task_store: Arc<dyn TaskStore<State>> = InMemoryTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<TestTask>().unwrap();

		// Create a task without cron
		let task = TestTask::new(5); // Takes 1 second (5 * 200ms)
		let task_id = scheduler.add(task.clone()).await.unwrap();

		// Wait a bit for task to start running
		tokio::time::sleep(std::time::Duration::from_millis(100)).await;

		// Verify task is running and has no cron
		{
			let running = scheduler.tasks_running.lock().unwrap();
			let meta = running.get(&task_id).expect("Task should be running");
			assert!(meta.cron.is_none(), "Task should have no cron initially");
		}

		// Try to update the running task with a cron schedule
		let cron = CronSchedule::parse("*/5 * * * *").unwrap();
		let task_meta_with_cron = TaskMeta {
			task: task.clone(),
			next_at: Some(Timestamp::now()),
			deps: vec![],
			retry_count: 0,
			retry: None,
			cron: Some(cron.clone()),
			rerun_requested: false,
		};
		let result = scheduler.add_queue(task_id, task_meta_with_cron).await;

		// Should succeed
		assert!(result.is_ok(), "add_queue should succeed");

		// Verify the running task now has the cron schedule
		{
			let running = scheduler.tasks_running.lock().unwrap();
			let meta = running.get(&task_id).expect("Task should still be running");
			assert!(meta.cron.is_some(), "Task should now have cron after update");
		}

		// Wait for task to complete
		tokio::time::sleep(std::time::Duration::from_secs(2)).await;
	}

	/// A store that actually remembers keys.
	///
	/// [`InMemoryTaskStore::find_by_key`] always answers `None`, so it can never
	/// reach the keyed-dedup branch of `schedule_task_impl` — the branch the test
	/// below is about. Only `input` has to be faithful: that is what the
	/// "parameters changed" comparison reads.
	struct KeyedTaskStore {
		last_id: Mutex<TaskId>,
		by_key: Mutex<HashMap<String, TaskId>>,
		input: Mutex<HashMap<TaskId, (String, String)>>,
		/// Every `finished` call, in order. The persisted row leaves `status='P'`
		/// only through this, so a run that never reaches it is a task stuck
		/// pending forever — re-run on every process restart, dependents never
		/// released.
		finished: Mutex<Vec<TaskId>>,
		/// Every `update_task_error` call, in order. `next_at` is what decides the
		/// persisted status: `Some` keeps the row `'P'` (a cron task is not
		/// finished by a failed run), `None` stamps it `'E'`.
		errors: Mutex<Vec<(TaskId, Option<Timestamp>)>>,
		/// Hold `finished` open so a test can land a re-request *inside* that
		/// await, which is where the race lives. Separate from `update_gate`
		/// because a test that parks one still needs the other to run: the
		/// re-request it lands does its own store call on the way in.
		finished_gate: Arc<tokio::sync::Semaphore>,
		/// Same, for `update_task` — the cron reschedule's await.
		update_gate: Arc<tokio::sync::Semaphore>,
		/// Fires as `finished` is entered, before it parks.
		finished_entered: Arc<tokio::sync::Notify>,
		/// Fires as `update_task` is entered, before it parks.
		updated_entered: Arc<tokio::sync::Notify>,
	}

	impl KeyedTaskStore {
		/// Both gates wide open — the ungated store every other test uses.
		fn new() -> Arc<Self> {
			Self::with_gates(
				tokio::sync::Semaphore::MAX_PERMITS,
				tokio::sync::Semaphore::MAX_PERMITS,
			)
		}

		/// A store whose `finished` blocks until the test releases it.
		fn new_gated_finished() -> Arc<Self> {
			Self::with_gates(0, tokio::sync::Semaphore::MAX_PERMITS)
		}

		/// A store whose `update_task` blocks until the test releases it.
		fn new_gated_update() -> Arc<Self> {
			Self::with_gates(tokio::sync::Semaphore::MAX_PERMITS, 0)
		}

		fn with_gates(finished_permits: usize, update_permits: usize) -> Arc<Self> {
			Arc::new(Self {
				last_id: Mutex::new(0),
				by_key: Mutex::new(HashMap::new()),
				input: Mutex::new(HashMap::new()),
				finished: Mutex::new(Vec::new()),
				errors: Mutex::new(Vec::new()),
				finished_gate: Arc::new(tokio::sync::Semaphore::new(finished_permits)),
				update_gate: Arc::new(tokio::sync::Semaphore::new(update_permits)),
				finished_entered: Arc::new(tokio::sync::Notify::new()),
				updated_entered: Arc::new(tokio::sync::Notify::new()),
			})
		}

		fn finished_ids(&self) -> Vec<TaskId> {
			self.finished.lock().unwrap().clone()
		}

		fn error_calls(&self) -> Vec<(TaskId, Option<Timestamp>)> {
			self.errors.lock().unwrap().clone()
		}

		fn release_finished(&self, n: usize) {
			self.finished_gate.add_permits(n);
		}

		fn release_updates(&self, n: usize) {
			self.update_gate.add_permits(n);
		}

		/// Await `finished` being entered. `notify_one` stores a permit when
		/// nobody is waiting, so this cannot miss the signal. The timeout only
		/// exists so a regression fails instead of hanging.
		async fn await_finished_entered(&self) {
			tokio::time::timeout(
				std::time::Duration::from_secs(5),
				self.finished_entered.notified(),
			)
			.await
			.expect("expected `finished` to be entered");
		}

		/// Same, for `update_task`.
		async fn await_updated_entered(&self) {
			tokio::time::timeout(
				std::time::Duration::from_secs(5),
				self.updated_entered.notified(),
			)
			.await
			.expect("expected `update_task` to be entered");
		}
	}

	#[async_trait]
	impl<S: Clone> TaskStore<S> for KeyedTaskStore {
		async fn add(&self, task: &TaskMeta<S>, key: Option<&str>) -> ClResult<TaskId> {
			let id = {
				let mut last = self.last_id.lock().unwrap();
				*last += 1;
				*last
			};
			self.input
				.lock()
				.unwrap()
				.insert(id, (task.task.kind_of().to_owned(), task.task.serialize()));
			if let Some(key) = key {
				self.by_key.lock().unwrap().insert(key.to_owned(), id);
			}
			Ok(id)
		}

		async fn find_by_key(&self, key: &str) -> ClResult<Option<(TaskId, TaskData)>> {
			let Some(id) = self.by_key.lock().unwrap().get(key).copied() else { return Ok(None) };
			let Some((kind, input)) = self.input.lock().unwrap().get(&id).cloned() else {
				return Ok(None);
			};
			Ok(Some((
				id,
				TaskData {
					id,
					kind: kind.into(),
					status: TaskStatus::Pending,
					input: input.into(),
					deps: Box::from([]),
					retry_data: None,
					cron_data: None,
					next_at: None,
				},
			)))
		}

		async fn update_task(&self, id: TaskId, task: &TaskMeta<S>) -> ClResult<()> {
			self.updated_entered.notify_one();
			self.update_gate
				.acquire()
				.await
				.map(tokio::sync::SemaphorePermit::forget)
				.map_err(|_| Error::Internal("store gate closed".into()))?;
			if let Some(entry) = self.input.lock().unwrap().get_mut(&id) {
				entry.1 = task.task.serialize();
			}
			Ok(())
		}

		async fn finished(&self, id: TaskId, _output: &str) -> ClResult<()> {
			self.finished_entered.notify_one();
			self.finished_gate
				.acquire()
				.await
				.map(tokio::sync::SemaphorePermit::forget)
				.map_err(|_| Error::Internal("store gate closed".into()))?;
			self.finished.lock().unwrap().push(id);
			Ok(())
		}
		async fn load(&self) -> ClResult<Vec<TaskData>> {
			Ok(vec![])
		}
		async fn update_task_error(
			&self,
			task_id: TaskId,
			_output: &str,
			next_at: Option<Timestamp>,
		) -> ClResult<()> {
			self.errors.lock().unwrap().push((task_id, next_at));
			Ok(())
		}
		async fn find_completed_deps(&self, _deps: &[TaskId]) -> ClResult<Vec<TaskId>> {
			Ok(vec![])
		}
	}

	/// A task whose body a test drives directly: `entered` fires the moment a run
	/// starts, and the run then blocks until the test hands it a `gate` permit.
	///
	/// No clock is involved, in either direction — the tests below neither sleep
	/// on wall-clock time nor need `tokio::time::pause`, so they cannot be flaky
	/// on a loaded machine. `runs` records the parameters of every body entered,
	/// in order, which is what "the second run used the *new* parameters" means.
	#[derive(Debug)]
	struct GatedTask {
		param: u8,
		runs: Arc<Mutex<Vec<u8>>>,
		entered: Arc<tokio::sync::Notify>,
		gate: Arc<tokio::sync::Semaphore>,
		/// Return a retryable error once the gate opens.
		fail: bool,
	}

	#[async_trait]
	impl Task<State> for GatedTask {
		fn kind() -> &'static str {
			"gated"
		}
		fn kind_of(&self) -> &'static str {
			Self::kind()
		}
		fn build(_id: TaskId, _ctx: &str) -> ClResult<Arc<dyn Task<State>>> {
			Err(Error::Internal("not rebuilt in this test".into()))
		}
		fn serialize(&self) -> String {
			self.param.to_string()
		}
		async fn run(&self, _state: &State) -> ClResult<()> {
			self.runs.lock().unwrap().push(self.param);
			self.entered.notify_one();
			let _permit = self.gate.acquire().await;
			if self.fail {
				// Retryable, so the retry path rather than `on_failed` runs.
				return Err(Error::Internal("gated failure".into()));
			}
			Ok(())
		}
	}

	struct Gated {
		runs: Arc<Mutex<Vec<u8>>>,
		entered: Arc<tokio::sync::Notify>,
		gate: Arc<tokio::sync::Semaphore>,
	}

	impl Gated {
		fn new() -> Self {
			Self {
				runs: Arc::new(Mutex::new(Vec::new())),
				entered: Arc::new(tokio::sync::Notify::new()),
				gate: Arc::new(tokio::sync::Semaphore::new(0)),
			}
		}

		fn task(&self, param: u8, fail: bool) -> Arc<GatedTask> {
			Arc::new(GatedTask {
				param,
				runs: Arc::clone(&self.runs),
				entered: Arc::clone(&self.entered),
				gate: Arc::clone(&self.gate),
				fail,
			})
		}

		fn params(&self) -> Vec<u8> {
			self.runs.lock().unwrap().clone()
		}

		/// Await the next body being entered. The timeout only exists so a
		/// regression — a run that never happens — fails the test instead of
		/// hanging it; the happy path never reaches the clock.
		async fn await_run(&self) {
			tokio::time::timeout(std::time::Duration::from_secs(5), self.entered.notified())
				.await
				.expect("expected a task body to be entered");
		}
	}

	/// A keyed **one-shot** re-requested while it runs must still get its run.
	///
	/// The in-place metadata update is right — a second concurrent body would be
	/// two VACUUMs for `core.db_maintenance:manual` — but "the run in flight
	/// reschedules itself from the new parameters" only holds for a *cron* task.
	/// A one-shot takes the `store.finished` branch instead, so without the rerun
	/// flag the run the caller asked for is silently dropped.
	#[tokio::test]
	pub async fn a_running_keyed_one_shot_re_requested_in_flight_runs_again() {
		let _ = tracing_subscriber::fmt().try_init();

		let store = KeyedTaskStore::new();
		let task_store: Arc<dyn TaskStore<State>> = store.clone();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<GatedTask>().unwrap();

		let gated = Gated::new();
		let first_id = scheduler.task(gated.task(1, false)).key("test.gated").now().await.unwrap();
		gated.await_run().await;

		// Same key, new parameters, while the first body is still held open.
		let second_id = scheduler.task(gated.task(2, false)).key("test.gated").now().await.unwrap();
		assert_eq!(second_id, first_id, "the key must resolve to the running task");
		assert_eq!(gated.params(), vec![1], "no second body may start alongside the first");

		// Let the first run finish; the finish handler owes the caller a re-run.
		gated.gate.add_permits(1);
		gated.await_run().await;
		gated.gate.add_permits(1);

		assert_eq!(
			gated.params(),
			vec![1, 2],
			"the re-requested run must happen, with the new parameters"
		);

		// And it must be *accounted for*: the re-requested run has to reach
		// `finished`, or the persisted row stays `status='P'` forever — re-run on
		// every process restart, dependents never released.
		//
		// Polled rather than asserted straight away: the second body's completion
		// travels through the finish channel.
		for _ in 0..200 {
			if !store.finished_ids().is_empty() {
				break;
			}
			tokio::time::sleep(std::time::Duration::from_millis(10)).await;
		}
		assert_eq!(
			store.finished_ids(),
			vec![first_id],
			"the re-requested run must reach `finished`, or the task is stuck pending"
		);
		assert!(
			!scheduler.tasks_running.lock().unwrap().contains_key(&first_id),
			"a finished task must not stay in the running map"
		);
	}

	/// The same, for a re-request whose parameters are **identical**.
	///
	/// This is the common shape, not the exotic one: `IndexDocumentTask { tn_id,
	/// file_id }` serializes the same way for every edit of the same file, so a
	/// second edit landing while the first index run is in flight takes the
	/// identical-parameters branch. Falling through to `add_queue` there absorbs
	/// the request — its already-running arm only *copies* an existing rerun flag
	/// and never sets one — leaving the document's index stale until the weekly
	/// sweep.
	#[tokio::test]
	pub async fn a_running_keyed_one_shot_re_requested_with_identical_params_runs_again() {
		let _ = tracing_subscriber::fmt().try_init();

		let store = KeyedTaskStore::new();
		let task_store: Arc<dyn TaskStore<State>> = store.clone();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<GatedTask>().unwrap();

		let gated = Gated::new();
		let first_id = scheduler.task(gated.task(1, false)).key("test.gated").now().await.unwrap();
		gated.await_run().await;

		// Same key, *same* parameters, while the first body is still held open.
		let second_id = scheduler.task(gated.task(1, false)).key("test.gated").now().await.unwrap();
		assert_eq!(second_id, first_id, "the key must resolve to the running task");
		assert_eq!(gated.params(), vec![1], "no second body may start alongside the first");

		gated.gate.add_permits(1);
		gated.await_run().await;
		gated.gate.add_permits(1);

		assert_eq!(
			gated.params(),
			vec![1, 1],
			"an identical re-request carries the same intent as a changed one"
		);

		for _ in 0..200 {
			if !store.finished_ids().is_empty() {
				break;
			}
			tokio::time::sleep(std::time::Duration::from_millis(10)).await;
		}
		assert_eq!(
			store.finished_ids(),
			vec![first_id],
			"the re-requested run must reach `finished`, or the task is stuck pending"
		);
	}

	/// A cron task with no retry policy must keep its row Pending when a run
	/// fails.
	///
	/// `update_task_error(id, err, None)` maps to `mark_error`'s `status='E'`
	/// arm. In memory the task is fine — the finish handler sees the cron and
	/// reschedules — but the reschedule persists through `update`, which has no
	/// status clause and never restores `'P'`. From then on `find_by_key` and
	/// `load()` (both `status='P'`-filtered) cannot see the row, the next boot
	/// mints a duplicate, and `run_on_startup` fires again: a full `VACUUM` of
	/// `meta.db` on every restart.
	#[tokio::test]
	pub async fn a_failing_cron_run_keeps_the_row_pending() {
		let _ = tracing_subscriber::fmt().try_init();

		let store = KeyedTaskStore::new();
		let task_store: Arc<dyn TaskStore<State>> = store.clone();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<GatedTask>().unwrap();

		let gated = Gated::new();
		// No retry policy, so the failure is terminal for this occurrence.
		let id = scheduler
			.task(gated.task(1, true))
			.key("test.cron.failing")
			.cron("*/5 * * * *")
			.run_on_startup()
			.schedule()
			.await
			.unwrap();
		gated.await_run().await;

		// Let the body fail.
		gated.gate.add_permits(1);
		for _ in 0..200 {
			if !store.error_calls().is_empty() {
				break;
			}
			tokio::time::sleep(std::time::Duration::from_millis(10)).await;
		}

		let errors = store.error_calls();
		assert_eq!(errors.len(), 1, "the failed run must be persisted");
		assert_eq!(errors[0].0, id);
		assert!(
			errors[0].1.is_some(),
			"a cron task's row must keep a live next_at, not be stamped 'E'"
		);
		assert!(store.finished_ids().is_empty(), "a failed run does not finish the task");
	}

	/// A retry after an in-flight update must carry the **new** parameters.
	///
	/// `spawn_task` captures its `TaskMeta` when the run starts, so building
	/// `retry_meta` from that snapshot would re-run the old parameters while the
	/// persisted row already holds the new ones.
	#[tokio::test]
	pub async fn a_retry_after_an_in_flight_update_uses_the_new_parameters() {
		let _ = tracing_subscriber::fmt().try_init();

		let task_store: Arc<dyn TaskStore<State>> = KeyedTaskStore::new();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<GatedTask>().unwrap();

		let gated = Gated::new();
		// Zero backoff, so the retry is re-queued for "now" and the scheduler's
		// notify path picks it up without any timer.
		let first_id = scheduler
			.task(gated.task(1, true))
			.key("test.gated")
			.with_retry(RetryPolicy::new((0, 0), 3))
			.now()
			.await
			.unwrap();
		gated.await_run().await;

		let second_id = scheduler
			.task(gated.task(2, false))
			.key("test.gated")
			.with_retry(RetryPolicy::new((0, 0), 3))
			.now()
			.await
			.unwrap();
		assert_eq!(second_id, first_id);

		// Release the first body; it fails retryably and the retry is queued.
		gated.gate.add_permits(1);
		gated.await_run().await;
		gated.gate.add_permits(1);

		assert_eq!(
			gated.params(),
			vec![1, 2],
			"the retry must run the parameters the in-flight update wrote"
		);
	}

	/// A re-request that lands **while `store.finished` is in flight** must still
	/// run.
	///
	/// The window: were the finish handler to read the rerun flag and only then
	/// await `store.finished` with the id still in `tasks_running`, a request
	/// arriving during that await would be absorbed by `schedule_task_impl`'s
	/// in-place update and thrown away when the handler removed the entry — and
	/// the leftover flag would make the *next* run of that id re-queue itself once
	/// more. Reachable at `worker_threads = 1` precisely because `store.finished`
	/// is an await; taking the entry out *before* the await is what closes it.
	///
	/// Driven by gates, not timing: with one worker every step below is forced,
	/// because awaiting an already-ready future does not yield. The flavour is
	/// pinned rather than left to `#[tokio::test]`'s default — on a multi-thread
	/// runtime the window this test exists to cover stops existing, and the test
	/// would pass while asserting nothing.
	#[tokio::test(flavor = "current_thread")]
	pub async fn a_re_request_landing_while_the_finish_handler_marks_finished_still_runs() {
		let _ = tracing_subscriber::fmt().try_init();

		let store = KeyedTaskStore::new_gated_finished();
		let task_store: Arc<dyn TaskStore<State>> = store.clone();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<GatedTask>().unwrap();

		let gated = Gated::new();
		// `add`/`find_by_key` are ungated, but the first schedule's `update_task`
		// is not reached (no existing row), so nothing parks here.
		let id = scheduler.task(gated.task(1, false)).key("test.gated").now().await.unwrap();
		gated.await_run().await;

		// Body 1 returns; the handler wakes, sees no cron and no rerun, takes the
		// id out of `tasks_running`, then enters `store.finished` and parks.
		gated.gate.add_permits(1);
		store.await_finished_entered().await;

		// The re-request lands inside that await.
		let second = scheduler.task(gated.task(2, false)).key("test.gated").now().await.unwrap();
		assert_eq!(second, id, "the key must resolve to the same task");

		// Release `finished`. The requested run must now happen.
		store.release_finished(8);
		gated.await_run().await;
		gated.gate.add_permits(1);

		assert_eq!(
			gated.params(),
			vec![1, 2],
			"the request that landed during `finished` must still run, with the new parameters"
		);

		for _ in 0..200 {
			if store.finished_ids().len() >= 2 {
				break;
			}
			tokio::time::sleep(std::time::Duration::from_millis(10)).await;
		}
		// Not `== vec![id]`: in this interleaving the row is legitimately marked
		// finished twice — the second run queued itself through the ordinary path
		// while the first `finished` was still in flight. That residual is
		// documented at the handler's terminal branch; closing it needs a
		// store-level re-open.
		assert!(
			store.finished_ids().contains(&id),
			"the re-requested run must reach `finished`, or the row stays 'P' forever"
		);
		assert!(
			!scheduler.tasks_running.lock().unwrap().contains_key(&id),
			"a finished task must not stay in the running map"
		);
	}

	/// A cron task's reschedule must use the metadata an in-flight update wrote,
	/// not the snapshot the handler took before awaiting `store.update_task`.
	///
	/// Building `updated_meta` from a clone taken before the await discards a
	/// re-request landing during it twice over: the entry is deleted and the stale
	/// parameters re-queued. Reachable at one worker.
	#[tokio::test]
	pub async fn a_cron_reschedule_uses_the_parameters_an_in_flight_update_wrote() {
		let _ = tracing_subscriber::fmt().try_init();

		let store = KeyedTaskStore::new_gated_update();
		let task_store: Arc<dyn TaskStore<State>> = store.clone();
		let state: State = Arc::new(Mutex::new(Vec::new()));
		let scheduler = Scheduler::new(task_store);
		scheduler.start(state.clone());
		scheduler.register::<GatedTask>().unwrap();

		let gated = Gated::new();
		let id = scheduler
			.task(gated.task(1, false))
			.key("test.cron")
			.cron("*/5 * * * *")
			.run_on_startup()
			.schedule()
			.await
			.unwrap();
		gated.await_run().await;

		// Body 1 returns; the handler takes the cron branch and parks in
		// `store.update_task`.
		gated.gate.add_permits(1);
		store.await_updated_entered().await;

		// The re-request lands inside that await — and parks on the same gate on
		// its own way in, so the release has to come from beside it. `notify_one`
		// stores a permit when nobody is waiting, so this releaser cannot miss the
		// second entry however the two are interleaved.
		let releaser = {
			let store = Arc::clone(&store);
			tokio::spawn(async move {
				store.await_updated_entered().await;
				store.release_updates(8);
			})
		};
		let second = scheduler
			.task(gated.task(2, false))
			.key("test.cron")
			.cron("*/5 * * * *")
			.schedule()
			.await
			.unwrap();
		assert_eq!(second, id, "the key must resolve to the running task");
		releaser.await.unwrap();
		// The reschedule is white-box and instant — no wall-clock wait, the next
		// cron firing is minutes away.
		for _ in 0..200 {
			let queued = {
				let scheduled = scheduler.tasks_scheduled.lock().unwrap();
				scheduled.iter().any(|((_, tid), _)| *tid == id)
			};
			if queued {
				break;
			}
			tokio::time::sleep(std::time::Duration::from_millis(10)).await;
		}
		let scheduled = scheduler.tasks_scheduled.lock().unwrap();
		let (_, meta) = scheduled
			.iter()
			.find(|((_, tid), _)| *tid == id)
			.expect("cron task must be rescheduled");
		assert_eq!(meta.task.serialize(), "2", "the cron reschedule used the stale snapshot");
	}
}

// vim: ts=4