autumn-web 0.6.0

An opinionated, convention-over-configuration web framework for Rust
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
//! Durable dispatch for generated repository after-commit hooks.
//!
//! Generated repositories enqueue `after_*_commit` work into Postgres inside
//! the same transaction as the mutation. Any replica can later claim and run a
//! queued hook using the generated runner registered in this process.

use std::any::Any;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, OnceLock, RwLock};
// `AtomicBool`/`Ordering` back the Postgres kick-worker's pending flag, which is
// `cfg`-gated off under `sqlite` (the durable hook worker is Postgres-only).
#[cfg(not(feature = "sqlite"))]
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

use diesel::OptionalExtension as _;
use diesel_async::RunQueryDsl as _;
use diesel_async::pooled_connection::deadpool::Pool;
use futures::FutureExt as _;
// `scope_boxed` boxes the `SQLite` claim's transaction closure future for
// `scoped_immediate_transaction`.
#[cfg(feature = "sqlite")]
use scoped_futures::ScopedFutureExt as _;
use serde::Serialize;
use serde_json::Value;
use sha2::Digest as _;
// `Notify` backs the Postgres kick-worker's coalesced pending flag, and (under
// `sqlite`) the single-node poll-loop wake used in place of Postgres LISTEN/NOTIFY.
use tokio::sync::Notify;
use tokio_util::sync::CancellationToken;

use crate::{AutumnError, AutumnResult};

// The Postgres worker is typed on `PgPool`; the shared enqueue/finalize surface
// and the `SQLite` worker are typed on the runtime pool alias `RtPool` (on
// Postgres the two are the SAME type, since `RuntimeConnection = AsyncPgConnection`).
#[cfg(not(feature = "sqlite"))]
type PgPool = Pool<diesel_async::AsyncPgConnection>;
type RtPool = Pool<crate::db::RuntimeConnection>;
type HookFuture = Pin<Box<dyn Future<Output = AutumnResult<()>> + Send + 'static>>;
type HookRunner = Arc<dyn Fn(Value, Value) -> HookFuture + Send + Sync + 'static>;

/// Reserved key under which the `SQLite` durable worker embeds the originating
/// tenant into the persisted `context` JSON, so a claimed hook re-establishes
/// the SAME tenant scope it was enqueued under before executing. `MutationContext`
/// does not carry a tenant field, so tenant isolation for durable hooks would
/// otherwise be lost across the process boundary. The key is stripped before the
/// context is handed to the generated runner (which deserializes `MutationContext`).
#[cfg(feature = "sqlite")]
const TENANT_CONTEXT_KEY: &str = "__autumn_tenant";

/// Framework migration set for the durable commit-hook queue table.
///
/// Backend-forked (#1996 item 5): the Postgres DDL (`JSONB`/`TIMESTAMPTZ`/
/// `DEFAULT NOW()`) is not valid `SQLite`, so the `SQLite` build embeds a parallel
/// set (`TEXT` JSON / `TEXT` timestamps / `DEFAULT CURRENT_TIMESTAMP`) under the
/// same `20260515000000_create_repository_commit_hook_queue` version dir name,
/// keeping `__diesel_schema_migrations` bookkeeping identical across backends.
#[cfg(not(feature = "sqlite"))]
pub const REPOSITORY_COMMIT_HOOK_MIGRATIONS: diesel_migrations::EmbeddedMigrations =
    diesel_migrations::embed_migrations!("repository_commit_hook_migrations");

/// `SQLite` variant of [`REPOSITORY_COMMIT_HOOK_MIGRATIONS`]. See that item for
/// the backend-fork rationale.
#[cfg(feature = "sqlite")]
pub const REPOSITORY_COMMIT_HOOK_MIGRATIONS: diesel_migrations::EmbeddedMigrations =
    diesel_migrations::embed_migrations!("repository_commit_hook_migrations_sqlite");

const HOOK_WORKER_IDLE_SLEEP: Duration = Duration::from_millis(250);
const HOOK_STALE_CLAIM_AFTER: Duration = Duration::from_secs(60);
const HOOK_CLAIM_HEARTBEAT_INTERVAL: Duration = Duration::from_secs(15);
const HOOK_PENDING_FINALIZER_HEARTBEAT_INTERVAL: Duration = Duration::from_secs(15);
const HOOK_AFTER_HOOK_FAILURE_MARK_RETRY_SLEEP: Duration = Duration::from_millis(100);
const HOOK_AFTER_HOOK_FAILURE_MARK_MAX_ATTEMPTS: usize = 3;

// ── Backend-forked SQL (#1996 item 5) ────────────────────────────────────────
//
// Only one backend's arm is ever compiled. The Postgres SQL is byte-identical to
// the original (numbered `$n` placeholders, `NOW()`, `::JSONB` casts). The `SQLite`
// SQL uses `?` positional placeholders (bound in the SAME order as the Postgres
// binds so a single shared function body serves both), `CURRENT_TIMESTAMP` in
// place of `NOW()`, no `::JSONB` casts (the columns are already `TEXT`), and
// lowercase `excluded` in the upsert. Structurally-divergent statements (claim,
// nack retry/dead-letter, stale recovery, and bulk enqueue) are forked at the
// function level instead — see `*_claim_next` / `*_nack_*` / `*_recover_stale*`.

#[cfg(not(feature = "sqlite"))]
const HOOK_SELECT_COLS: &str = "id, handler_key, hook_name, context::TEXT AS context, \
    record::TEXT AS record, status, attempt, max_attempts, initial_backoff_ms";
#[cfg(feature = "sqlite")]
const HOOK_SELECT_COLS: &str = "id, handler_key, hook_name, context, \
    record, status, attempt, max_attempts, initial_backoff_ms";

#[cfg(not(feature = "sqlite"))]
const HOOK_ACK_SUCCESS_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET status = 'completed', finished_at = NOW(), \
         context = '{}'::JSONB, record = '{}'::JSONB, \
         claimed_by = NULL, claimed_at = NULL, last_error = NULL \
     WHERE id = $1 AND claimed_by = $2 AND status = 'running'";
#[cfg(feature = "sqlite")]
const HOOK_ACK_SUCCESS_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET status = 'completed', finished_at = CURRENT_TIMESTAMP, \
         context = '{}', record = '{}', \
         claimed_by = NULL, claimed_at = NULL, last_error = NULL \
     WHERE id = ? AND claimed_by = ? AND status = 'running'";

#[cfg(not(feature = "sqlite"))]
const HOOK_EXTEND_CLAIM_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET claimed_at = NOW() \
     WHERE id = $1 AND claimed_by = $2 AND status = 'running'";
#[cfg(feature = "sqlite")]
const HOOK_EXTEND_CLAIM_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET claimed_at = CURRENT_TIMESTAMP \
     WHERE id = ? AND claimed_by = ? AND status = 'running'";

#[cfg(not(feature = "sqlite"))]
const HOOK_ENQUEUE_INSERT_SQL: &str = "INSERT INTO autumn_repository_commit_hooks \
     (id, handler_key, hook_name, context, record, status, attempt, \
      max_attempts, initial_backoff_ms, enqueued_at, run_at) \
     VALUES ($1, $2, $3, $4::JSONB, $5::JSONB, 'enqueued', 1, 5, 1000, NOW(), NOW()) \
     ON CONFLICT (id) DO NOTHING";
#[cfg(feature = "sqlite")]
const HOOK_ENQUEUE_INSERT_SQL: &str = "INSERT INTO autumn_repository_commit_hooks \
     (id, handler_key, hook_name, context, record, status, attempt, \
      max_attempts, initial_backoff_ms, enqueued_at, run_at) \
     VALUES (?, ?, ?, ?, ?, 'enqueued', 1, 5, 1000, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) \
     ON CONFLICT (id) DO NOTHING";

#[cfg(not(feature = "sqlite"))]
const HOOK_PENDING_INSERT_SQL: &str = "INSERT INTO autumn_repository_commit_hooks \
     (id, handler_key, hook_name, context, record, status, attempt, \
       max_attempts, initial_backoff_ms, enqueued_at, run_at, claimed_by, claimed_at) \
     VALUES ($1, $2, $3, $4::JSONB, $5::JSONB, 'pending_after_hook', 1, 5, 1000, NOW(), NOW(), $6, NOW()) \
     ON CONFLICT (id) DO UPDATE \
      SET handler_key = EXCLUDED.handler_key, hook_name = EXCLUDED.hook_name, \
          context = EXCLUDED.context, record = EXCLUDED.record, \
          status = 'pending_after_hook', attempt = 1, \
          max_attempts = EXCLUDED.max_attempts, \
          initial_backoff_ms = EXCLUDED.initial_backoff_ms, \
          enqueued_at = EXCLUDED.enqueued_at, run_at = EXCLUDED.run_at, \
          claimed_by = EXCLUDED.claimed_by, claimed_at = EXCLUDED.claimed_at, \
          started_at = NULL, finished_at = NULL, last_error = NULL \
      WHERE autumn_repository_commit_hooks.status IN ('pending_after_hook', 'after_hook_failed')";
#[cfg(feature = "sqlite")]
const HOOK_PENDING_INSERT_SQL: &str = "INSERT INTO autumn_repository_commit_hooks \
     (id, handler_key, hook_name, context, record, status, attempt, \
       max_attempts, initial_backoff_ms, enqueued_at, run_at, claimed_by, claimed_at) \
     VALUES (?, ?, ?, ?, ?, 'pending_after_hook', 1, 5, 1000, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?, CURRENT_TIMESTAMP) \
     ON CONFLICT (id) DO UPDATE \
      SET handler_key = excluded.handler_key, hook_name = excluded.hook_name, \
          context = excluded.context, record = excluded.record, \
          status = 'pending_after_hook', attempt = 1, \
          max_attempts = excluded.max_attempts, \
          initial_backoff_ms = excluded.initial_backoff_ms, \
          enqueued_at = excluded.enqueued_at, run_at = excluded.run_at, \
          claimed_by = excluded.claimed_by, claimed_at = excluded.claimed_at, \
          started_at = NULL, finished_at = NULL, last_error = NULL \
      WHERE autumn_repository_commit_hooks.status IN ('pending_after_hook', 'after_hook_failed')";

#[cfg(not(feature = "sqlite"))]
const HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET context = $1::JSONB, record = $2::JSONB, status = 'after_hook_succeeded', \
          claimed_at = NOW(), last_error = NULL \
     WHERE id = $3 AND claimed_by = $4 AND status = 'pending_after_hook'";
#[cfg(feature = "sqlite")]
const HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET context = ?, record = ?, status = 'after_hook_succeeded', \
          claimed_at = CURRENT_TIMESTAMP, last_error = NULL \
     WHERE id = ? AND claimed_by = ? AND status = 'pending_after_hook'";

#[cfg(not(feature = "sqlite"))]
const HOOK_FINALIZE_AFTER_HOOK_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET status = 'enqueued', run_at = NOW(), \
          enqueued_at = COALESCE(enqueued_at, NOW()), \
          claimed_by = NULL, claimed_at = NULL, last_error = NULL \
      WHERE id = $1 AND claimed_by = $2 AND status = 'after_hook_succeeded'";
#[cfg(feature = "sqlite")]
const HOOK_FINALIZE_AFTER_HOOK_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET status = 'enqueued', run_at = CURRENT_TIMESTAMP, \
          enqueued_at = COALESCE(enqueued_at, CURRENT_TIMESTAMP), \
          claimed_by = NULL, claimed_at = NULL, last_error = NULL \
      WHERE id = ? AND claimed_by = ? AND status = 'after_hook_succeeded'";

#[cfg(not(feature = "sqlite"))]
const HOOK_DISCARD_PENDING_SQL: &str = "DELETE FROM autumn_repository_commit_hooks \
     WHERE id = $1 AND claimed_by = $2 AND status = 'pending_after_hook'";
#[cfg(feature = "sqlite")]
const HOOK_DISCARD_PENDING_SQL: &str = "DELETE FROM autumn_repository_commit_hooks \
     WHERE id = ? AND claimed_by = ? AND status = 'pending_after_hook'";

#[cfg(not(feature = "sqlite"))]
const HOOK_AFTER_HOOK_FAILED_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET status = 'after_hook_failed', \
         finished_at = NOW(), \
         context = '{}'::JSONB, record = '{}'::JSONB, \
         claimed_by = NULL, claimed_at = NULL, last_error = $1 \
      WHERE id = $2 AND claimed_by = $3 AND status = 'pending_after_hook'";
#[cfg(feature = "sqlite")]
const HOOK_AFTER_HOOK_FAILED_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET status = 'after_hook_failed', \
         finished_at = CURRENT_TIMESTAMP, \
         context = '{}', record = '{}', \
         claimed_by = NULL, claimed_at = NULL, last_error = ? \
      WHERE id = ? AND claimed_by = ? AND status = 'pending_after_hook'";

#[cfg(not(feature = "sqlite"))]
const HOOK_EXTEND_PENDING_FINALIZER_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET claimed_at = NOW() \
     WHERE id = $1 AND claimed_by = $2 AND status = 'pending_after_hook'";
#[cfg(feature = "sqlite")]
const HOOK_EXTEND_PENDING_FINALIZER_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET claimed_at = CURRENT_TIMESTAMP \
     WHERE id = ? AND claimed_by = ? AND status = 'pending_after_hook'";

#[cfg(not(feature = "sqlite"))]
const HOOK_RECOVER_STALE_RUNNING_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET status = CASE \
           WHEN attempt < max_attempts THEN 'enqueued' \
           ELSE 'failed' \
         END, \
         attempt = CASE \
           WHEN attempt < max_attempts THEN attempt + 1 \
           ELSE attempt \
         END, \
         run_at = CASE \
           WHEN attempt < max_attempts THEN NOW() \
           ELSE run_at \
         END, \
         started_at = NULL, \
         finished_at = CASE \
           WHEN attempt >= max_attempts THEN NOW() \
           ELSE NULL \
         END, \
         claimed_by = NULL, \
         claimed_at = NULL, \
         last_error = $1 \
     WHERE status = 'running' \
       AND claimed_at < NOW() - ($2::BIGINT * INTERVAL '1 millisecond')";
#[cfg(not(feature = "sqlite"))]
const HOOK_RECOVER_STALE_PENDING_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET status = CASE \
            WHEN status = 'after_hook_succeeded' THEN 'enqueued' \
            ELSE 'after_hook_failed' \
          END, \
          run_at = CASE \
            WHEN status = 'after_hook_succeeded' THEN NOW() \
            ELSE run_at \
          END, \
          enqueued_at = CASE \
            WHEN status = 'after_hook_succeeded' THEN COALESCE(enqueued_at, NOW()) \
            ELSE enqueued_at \
          END, \
          context = CASE \
            WHEN status = 'pending_after_hook' THEN '{}'::JSONB \
            ELSE context \
          END, \
          record = CASE \
            WHEN status = 'pending_after_hook' THEN '{}'::JSONB \
            ELSE record \
          END, \
          finished_at = CASE \
            WHEN status = 'pending_after_hook' THEN NOW() \
            ELSE finished_at \
          END, \
          started_at = NULL, \
          claimed_by = NULL, \
          claimed_at = NULL, \
          last_error = COALESCE(last_error, $1) \
      WHERE status IN ('pending_after_hook', 'after_hook_succeeded') \
        AND claimed_at < NOW() - ($2::BIGINT * INTERVAL '1 millisecond')";

// `SQLite` stale recovery: identical branch semantics, but the `NOW() - INTERVAL`
// arithmetic is replaced by a Rust-computed cutoff bound (bound SECOND, matching
// its textual position after the `last_error` message) — `WHERE claimed_at < ?`
// compares against a UTC `YYYY-MM-DD HH:MM:SS.fff` string. Binds: (last_error, cutoff).
#[cfg(feature = "sqlite")]
const HOOK_RECOVER_STALE_RUNNING_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET status = CASE \
           WHEN attempt < max_attempts THEN 'enqueued' \
           ELSE 'failed' \
         END, \
         attempt = CASE \
           WHEN attempt < max_attempts THEN attempt + 1 \
           ELSE attempt \
         END, \
         run_at = CASE \
           WHEN attempt < max_attempts THEN CURRENT_TIMESTAMP \
           ELSE run_at \
         END, \
         started_at = NULL, \
         finished_at = CASE \
           WHEN attempt >= max_attempts THEN CURRENT_TIMESTAMP \
           ELSE NULL \
         END, \
         claimed_by = NULL, \
         claimed_at = NULL, \
         last_error = ? \
     WHERE status = 'running' \
       AND claimed_at < ?";
#[cfg(feature = "sqlite")]
const HOOK_RECOVER_STALE_PENDING_SQL: &str = "UPDATE autumn_repository_commit_hooks \
     SET status = CASE \
            WHEN status = 'after_hook_succeeded' THEN 'enqueued' \
            ELSE 'after_hook_failed' \
          END, \
          run_at = CASE \
            WHEN status = 'after_hook_succeeded' THEN CURRENT_TIMESTAMP \
            ELSE run_at \
          END, \
          enqueued_at = CASE \
            WHEN status = 'after_hook_succeeded' THEN COALESCE(enqueued_at, CURRENT_TIMESTAMP) \
            ELSE enqueued_at \
          END, \
          context = CASE \
            WHEN status = 'pending_after_hook' THEN '{}' \
            ELSE context \
          END, \
          record = CASE \
            WHEN status = 'pending_after_hook' THEN '{}' \
            ELSE record \
          END, \
          finished_at = CASE \
            WHEN status = 'pending_after_hook' THEN CURRENT_TIMESTAMP \
            ELSE finished_at \
          END, \
          started_at = NULL, \
          claimed_by = NULL, \
          claimed_at = NULL, \
          last_error = COALESCE(last_error, ?) \
      WHERE status IN ('pending_after_hook', 'after_hook_succeeded') \
        AND claimed_at < ?";

static REPOSITORY_COMMIT_HOOK_RUNNERS: OnceLock<
    RwLock<HashMap<String, RepositoryCommitHookRegistration>>,
> = OnceLock::new();
#[cfg(not(feature = "sqlite"))]
static REPOSITORY_COMMIT_HOOK_KICKERS: OnceLock<
    RwLock<HashMap<usize, Arc<RepositoryCommitHookKickState>>>,
> = OnceLock::new();

#[cfg(not(feature = "sqlite"))]
struct RepositoryCommitHookKickState {
    notify: Notify,
    pending: AtomicBool,
    #[cfg(feature = "ws")]
    channels: OnceLock<crate::channels::Channels>,
}

#[cfg(not(feature = "sqlite"))]
impl Default for RepositoryCommitHookKickState {
    fn default() -> Self {
        Self {
            notify: Notify::new(),
            pending: AtomicBool::new(false),
            #[cfg(feature = "ws")]
            channels: OnceLock::new(),
        }
    }
}

#[cfg(not(feature = "sqlite"))]
impl RepositoryCommitHookKickState {
    fn request_kick(&self) -> bool {
        !self.pending.swap(true, Ordering::AcqRel)
    }

    fn take_pending_kick(&self) -> bool {
        self.pending.swap(false, Ordering::AcqRel)
    }
}

// `SQLite` is single-node and single-writer, so it needs no LISTEN/NOTIFY: an
// in-process `Notify` per pool wakes that pool's poll loop the moment a mutation
// commits. A missed kick is harmless — the loop also wakes on its idle sleep and
// drains, so durability never depends on the kick landing.
#[cfg(feature = "sqlite")]
static SQLITE_HOOK_KICKERS: OnceLock<RwLock<HashMap<usize, Arc<Notify>>>> = OnceLock::new();

#[cfg(feature = "sqlite")]
fn sqlite_repository_commit_hook_kick(pool: &RtPool) -> Arc<Notify> {
    let key = std::ptr::from_ref(pool.manager()).addr();
    let registry = SQLITE_HOOK_KICKERS.get_or_init(|| RwLock::new(HashMap::new()));
    {
        let registry = registry
            .read()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if let Some(notify) = registry.get(&key) {
            return notify.clone();
        }
    }
    let mut registry = registry
        .write()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    registry
        .entry(key)
        .or_insert_with(|| Arc::new(Notify::new()))
        .clone()
}

#[doc(hidden)]
#[must_use]
pub struct RepositoryCommitHookPendingHeartbeat {
    shutdown: CancellationToken,
}

impl RepositoryCommitHookPendingHeartbeat {
    const fn new(shutdown: CancellationToken) -> Self {
        Self { shutdown }
    }

    pub fn cancel(&self) {
        self.shutdown.cancel();
    }
}

impl Drop for RepositoryCommitHookPendingHeartbeat {
    fn drop(&mut self) {
        self.shutdown.cancel();
    }
}

/// Link-time descriptor emitted by generated repositories with commit hooks.
///
/// The worker replays these descriptors at startup so queued hook rows can be
/// claimed after a process restart without waiting for request traffic to touch
/// the repository type first.
#[doc(hidden)]
pub struct RepositoryCommitHookDescriptor {
    /// Registers the generated runner for one repository type.
    pub register: fn(),
}

inventory::collect!(RepositoryCommitHookDescriptor);

#[derive(Clone)]
struct RepositoryCommitHookRegistration {
    create: HookRunner,
    update: HookRunner,
    delete: HookRunner,
}

impl RepositoryCommitHookRegistration {
    fn runner(&self, hook_name: &str) -> Option<HookRunner> {
        match hook_name {
            "create" => Some(self.create.clone()),
            "update" => Some(self.update.clone()),
            "delete" => Some(self.delete.clone()),
            _ => None,
        }
    }
}

#[derive(diesel::QueryableByName, Debug, Clone)]
#[allow(dead_code)]
struct RepositoryCommitHookRow {
    #[diesel(sql_type = diesel::sql_types::Text)]
    id: String,
    #[diesel(sql_type = diesel::sql_types::Text)]
    handler_key: String,
    #[diesel(sql_type = diesel::sql_types::Text)]
    hook_name: String,
    #[diesel(sql_type = diesel::sql_types::Text)]
    context: String,
    #[diesel(sql_type = diesel::sql_types::Text)]
    record: String,
    #[diesel(sql_type = diesel::sql_types::Text)]
    status: String,
    #[diesel(sql_type = diesel::sql_types::Integer)]
    attempt: i32,
    #[diesel(sql_type = diesel::sql_types::Integer)]
    max_attempts: i32,
    #[diesel(sql_type = diesel::sql_types::BigInt)]
    initial_backoff_ms: i64,
}

/// Register generated runners for one hooked repository type.
///
/// This is called by proc-macro-generated code and is intentionally hidden
/// behind `autumn_web::__private`.
pub fn register_repository_commit_hook_runner<
    Create,
    CreateFut,
    Update,
    UpdateFut,
    Delete,
    DeleteFut,
>(
    handler_key: &'static str,
    create: Create,
    update: Update,
    delete: Delete,
) where
    Create: Fn(Value, Value) -> CreateFut + Send + Sync + 'static,
    CreateFut: Future<Output = AutumnResult<()>> + Send + 'static,
    Update: Fn(Value, Value) -> UpdateFut + Send + Sync + 'static,
    UpdateFut: Future<Output = AutumnResult<()>> + Send + 'static,
    Delete: Fn(Value, Value) -> DeleteFut + Send + Sync + 'static,
    DeleteFut: Future<Output = AutumnResult<()>> + Send + 'static,
{
    let registration = RepositoryCommitHookRegistration {
        create: Arc::new(move |ctx, record| Box::pin(create(ctx, record))),
        update: Arc::new(move |ctx, record| Box::pin(update(ctx, record))),
        delete: Arc::new(move |ctx, record| Box::pin(delete(ctx, record))),
    };

    REPOSITORY_COMMIT_HOOK_RUNNERS
        .get_or_init(|| RwLock::new(HashMap::new()))
        .write()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .insert(handler_key.to_owned(), registration);
}

fn register_inventory_repository_commit_hook_runners() {
    for descriptor in inventory::iter::<RepositoryCommitHookDescriptor> {
        (descriptor.register)();
    }
}

pub fn has_repository_commit_hook_descriptors() -> bool {
    inventory::iter::<RepositoryCommitHookDescriptor>
        .into_iter()
        .next()
        .is_some()
}

/// Insert a generated repository commit hook row using the caller's open
/// connection. The row participates in the caller's transaction.
///
/// # Errors
///
/// Returns an error when the context or record cannot be serialized, or when
/// Postgres rejects the enqueue insert.
pub async fn enqueue_repository_commit_hook_on_conn<C, R>(
    conn: &mut crate::db::RuntimeConnection,
    handler_key: &str,
    hook_name: &str,
    idempotency_key: Option<&str>,
    idempotency_discriminator: Option<&str>,
    context: &C,
    record: &R,
) -> AutumnResult<()>
where
    C: Serialize + Sync + ?Sized,
    R: Serialize + Sync + ?Sized,
{
    let (context, record) = serialize_repository_commit_hook_payloads(context, record)?;
    let id = repository_commit_hook_id(
        idempotency_key,
        idempotency_discriminator,
        handler_key,
        hook_name,
        &record,
    );

    diesel::sql_query(HOOK_ENQUEUE_INSERT_SQL)
        .bind::<diesel::sql_types::Text, _>(id)
        .bind::<diesel::sql_types::Text, _>(handler_key)
        .bind::<diesel::sql_types::Text, _>(hook_name)
        .bind::<diesel::sql_types::Text, _>(context)
        .bind::<diesel::sql_types::Text, _>(record)
        .execute(conn)
        .await
        .map(|_| ())
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook enqueue failed: {error}"
            ))
        })
}

/// Insert a generated repository commit hook row in a staged state.
///
/// The row participates in the caller's transaction but cannot be claimed by a
/// dispatcher until [`finalize_repository_commit_hook_after_hook`] promotes it
/// after the regular `after_*` hook has succeeded.
///
/// # Errors
///
/// Returns an error when the context or record cannot be serialized, or when
/// Postgres rejects the staged insert.
pub async fn enqueue_repository_commit_hook_pending_on_conn<C, R>(
    conn: &mut crate::db::RuntimeConnection,
    handler_key: &str,
    hook_name: &str,
    idempotency_key: Option<&str>,
    idempotency_discriminator: Option<&str>,
    context: &C,
    record: &R,
) -> AutumnResult<(String, String)>
where
    C: Serialize + Sync + ?Sized,
    R: Serialize + Sync + ?Sized,
{
    let (context, record) = serialize_repository_commit_hook_payloads(context, record)?;
    let id = repository_commit_hook_id(
        idempotency_key,
        idempotency_discriminator,
        handler_key,
        hook_name,
        &record,
    );
    let owner = repository_commit_hook_pending_owner_id();

    diesel::sql_query(HOOK_PENDING_INSERT_SQL)
        .bind::<diesel::sql_types::Text, _>(id.clone())
        .bind::<diesel::sql_types::Text, _>(handler_key)
        .bind::<diesel::sql_types::Text, _>(hook_name)
        .bind::<diesel::sql_types::Text, _>(context)
        .bind::<diesel::sql_types::Text, _>(record)
        .bind::<diesel::sql_types::Text, _>(owner.clone())
        .execute(conn)
        .await
        .map(|_| (id, owner))
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook staging failed: {error}"
            ))
        })
}

/// Insert multiple generated repository commit hook rows in a staged state in a single query.
///
/// # Errors
///
/// Returns an error when any context or record cannot be serialized, or when
/// Postgres rejects the staged insert.
pub async fn enqueue_repository_commit_hooks_pending_bulk_on_conn<C, R>(
    conn: &mut crate::db::RuntimeConnection,
    handler_key: &str,
    hook_name: &str,
    inputs: &[(Option<String>, Option<String>, &C, &R)],
) -> AutumnResult<Vec<(String, String)>>
where
    C: Serialize + Sync + ?Sized,
    R: Serialize + Sync + ?Sized,
{
    // Postgres stages every row in one `UNNEST`-driven statement. `SQLite` has no
    // array binds, so its arm loops the single-row staged insert on the SAME
    // caller connection (already inside the mutation transaction, so the rows
    // still commit atomically with the domain write). Both dedupe/restage
    // identically via `ON CONFLICT (id) DO UPDATE`.
    #[cfg(not(feature = "sqlite"))]
    const SQL: &str = "INSERT INTO autumn_repository_commit_hooks \
         (id, handler_key, hook_name, context, record, status, attempt, \
          max_attempts, initial_backoff_ms, enqueued_at, run_at, claimed_by, claimed_at) \
         SELECT \
             t.id, t.handler_key, t.hook_name, t.context::JSONB, t.record::JSONB, \
             'pending_after_hook', 1, 5, 1000, NOW(), NOW(), t.claimed_by, NOW() \
         FROM UNNEST($1::TEXT[], $2::TEXT[], $3::TEXT[], $4::TEXT[], $5::TEXT[], $6::TEXT[]) \
           AS t(id, handler_key, hook_name, context, record, claimed_by) \
         ON CONFLICT (id) DO UPDATE \
          SET handler_key = EXCLUDED.handler_key, hook_name = EXCLUDED.hook_name, \
              context = EXCLUDED.context, record = EXCLUDED.record, \
              status = 'pending_after_hook', attempt = 1, \
              max_attempts = EXCLUDED.max_attempts, \
              initial_backoff_ms = EXCLUDED.initial_backoff_ms, \
              enqueued_at = EXCLUDED.enqueued_at, run_at = EXCLUDED.run_at, \
              claimed_by = EXCLUDED.claimed_by, claimed_at = EXCLUDED.claimed_at, \
              started_at = NULL, finished_at = NULL, last_error = NULL \
          WHERE autumn_repository_commit_hooks.status IN ('pending_after_hook', 'after_hook_failed')";

    if inputs.is_empty() {
        return Ok(Vec::new());
    }

    let mut ids = Vec::with_capacity(inputs.len());
    let mut handler_keys = Vec::with_capacity(inputs.len());
    let mut hook_names = Vec::with_capacity(inputs.len());
    let mut contexts = Vec::with_capacity(inputs.len());
    let mut records = Vec::with_capacity(inputs.len());
    let mut owners = Vec::with_capacity(inputs.len());
    let mut results = Vec::with_capacity(inputs.len());

    let owner = repository_commit_hook_pending_owner_id();

    for &(ref idempotency_key, ref idempotency_discriminator, context, record) in inputs {
        let (context_str, record_str) = serialize_repository_commit_hook_payloads(context, record)?;
        let id = repository_commit_hook_id(
            idempotency_key.as_deref(),
            idempotency_discriminator.as_deref(),
            handler_key,
            hook_name,
            &record_str,
        );

        ids.push(id.clone());
        handler_keys.push(handler_key.to_string());
        hook_names.push(hook_name.to_string());
        contexts.push(context_str);
        records.push(record_str);
        owners.push(owner.clone());
        results.push((id, owner.clone()));
    }

    crate::backend_select! {
        pg => {{
            diesel::sql_query(SQL)
                .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(ids)
                .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(handler_keys)
                .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(hook_names)
                .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(contexts)
                .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(records)
                .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(owners)
                .execute(conn)
                .await
                .map(|_| results)
                .map_err(|error| {
                    AutumnError::internal_server_error_msg(format!(
                        "repository commit hook bulk staging failed: {error}"
                    ))
                })
        }},
        sqlite => {{
            for index in 0..ids.len() {
                diesel::sql_query(HOOK_PENDING_INSERT_SQL)
                    .bind::<diesel::sql_types::Text, _>(ids[index].clone())
                    .bind::<diesel::sql_types::Text, _>(handler_keys[index].clone())
                    .bind::<diesel::sql_types::Text, _>(hook_names[index].clone())
                    .bind::<diesel::sql_types::Text, _>(contexts[index].clone())
                    .bind::<diesel::sql_types::Text, _>(records[index].clone())
                    .bind::<diesel::sql_types::Text, _>(owners[index].clone())
                    .execute(&mut *conn)
                    .await
                    .map_err(|error| {
                        AutumnError::internal_server_error_msg(format!(
                            "repository commit hook bulk staging failed: {error}"
                        ))
                    })?;
            }
            Ok(results)
        }},
    }
}

/// Insert multiple generated repository commit hook rows directly in an enqueued state in a single query.
///
/// # Errors
///
/// Returns an error when any context or record cannot be serialized, or when
/// Postgres rejects the staged insert.
pub async fn enqueue_repository_commit_hooks_bulk_on_conn<C, R>(
    conn: &mut crate::db::RuntimeConnection,
    handler_key: &str,
    hook_name: &str,
    inputs: &[(Option<String>, Option<String>, &C, &R)],
) -> AutumnResult<()>
where
    C: Serialize + Sync + ?Sized,
    R: Serialize + Sync + ?Sized,
{
    // See `enqueue_repository_commit_hooks_pending_bulk_on_conn` for the pg/sqlite
    // fork rationale (UNNEST batch vs. per-row loop on the same txn connection).
    #[cfg(not(feature = "sqlite"))]
    const SQL: &str = "INSERT INTO autumn_repository_commit_hooks \
         (id, handler_key, hook_name, context, record, status, attempt, \
          max_attempts, initial_backoff_ms, enqueued_at, run_at) \
         SELECT \
             t.id, t.handler_key, t.hook_name, t.context::JSONB, t.record::JSONB, \
             'enqueued', 1, 5, 1000, NOW(), NOW() \
         FROM UNNEST($1::TEXT[], $2::TEXT[], $3::TEXT[], $4::TEXT[], $5::TEXT[]) \
           AS t(id, handler_key, hook_name, context, record) \
         ON CONFLICT (id) DO NOTHING";

    if inputs.is_empty() {
        return Ok(());
    }

    let mut ids = Vec::with_capacity(inputs.len());
    let mut handler_keys = Vec::with_capacity(inputs.len());
    let mut hook_names = Vec::with_capacity(inputs.len());
    let mut contexts = Vec::with_capacity(inputs.len());
    let mut records = Vec::with_capacity(inputs.len());

    for &(ref idempotency_key, ref idempotency_discriminator, context, record) in inputs {
        let (context_str, record_str) = serialize_repository_commit_hook_payloads(context, record)?;
        let id = repository_commit_hook_id(
            idempotency_key.as_deref(),
            idempotency_discriminator.as_deref(),
            handler_key,
            hook_name,
            &record_str,
        );

        ids.push(id);
        handler_keys.push(handler_key.to_string());
        hook_names.push(hook_name.to_string());
        contexts.push(context_str);
        records.push(record_str);
    }

    crate::backend_select! {
        pg => {{
            diesel::sql_query(SQL)
                .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(ids)
                .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(handler_keys)
                .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(hook_names)
                .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(contexts)
                .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(records)
                .execute(conn)
                .await
                .map(|_| ())
                .map_err(|error| {
                    AutumnError::internal_server_error_msg(format!(
                        "repository commit hook bulk enqueue failed: {error}"
                    ))
                })
        }},
        sqlite => {{
            for index in 0..ids.len() {
                diesel::sql_query(HOOK_ENQUEUE_INSERT_SQL)
                    .bind::<diesel::sql_types::Text, _>(ids[index].clone())
                    .bind::<diesel::sql_types::Text, _>(handler_keys[index].clone())
                    .bind::<diesel::sql_types::Text, _>(hook_names[index].clone())
                    .bind::<diesel::sql_types::Text, _>(contexts[index].clone())
                    .bind::<diesel::sql_types::Text, _>(records[index].clone())
                    .execute(&mut *conn)
                    .await
                    .map_err(|error| {
                        AutumnError::internal_server_error_msg(format!(
                            "repository commit hook bulk enqueue failed: {error}"
                        ))
                    })?;
            }
            Ok(())
        }},
    }
}

/// Promote a staged create/update commit hook after the regular after hook
/// succeeds, rewriting the row with the finalized mutation context.
///
/// # Errors
///
/// Returns an error when serialization fails, the database cannot be reached,
/// or the staged row is no longer present.
pub async fn finalize_repository_commit_hook_after_hook<C, R>(
    pool: &RtPool,
    hook_id: &str,
    owner: &str,
    context: &C,
    record: &R,
) -> AutumnResult<()>
where
    C: Serialize + Sync + ?Sized,
    R: Serialize + Sync + ?Sized,
{
    let (context, record) = serialize_repository_commit_hook_payloads(context, record)?;
    let mut conn = pool.get().await.map_err(|error| {
        AutumnError::internal_server_error_msg(format!("pg pool error: {error}"))
    })?;

    let rows = diesel::sql_query(HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL)
        .bind::<diesel::sql_types::Text, _>(context)
        .bind::<diesel::sql_types::Text, _>(record)
        .bind::<diesel::sql_types::Text, _>(hook_id)
        .bind::<diesel::sql_types::Text, _>(owner)
        .execute(&mut *conn)
        .await
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook after-hook success mark failed: {error}"
            ))
        })?;

    if rows == 0 {
        return missing_repository_commit_hook_finalization_result(hook_id);
    }

    let rows = diesel::sql_query(HOOK_FINALIZE_AFTER_HOOK_SQL)
        .bind::<diesel::sql_types::Text, _>(hook_id)
        .bind::<diesel::sql_types::Text, _>(owner)
        .execute(&mut *conn)
        .await
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook finalization failed: {error}"
            ))
        })?;

    if rows == 0 {
        return Err(AutumnError::internal_server_error_msg(format!(
            "repository commit hook finalization skipped marked row: {hook_id}"
        )));
    }

    Ok(())
}

fn missing_repository_commit_hook_finalization_result(hook_id: &str) -> AutumnResult<()> {
    Err(AutumnError::internal_server_error_msg(format!(
        "repository commit hook finalization skipped missing staged row: {hook_id}"
    )))
}

/// Discard a staged create/update commit hook after the regular after hook
/// fails. This preserves the previous lifecycle: after-commit work is only
/// registered after the regular after hook succeeds.
///
/// # Errors
///
/// Returns an error when the database cannot be reached or rejects the delete.
pub async fn discard_repository_commit_hook_pending(
    pool: &RtPool,
    hook_id: &str,
    owner: &str,
) -> AutumnResult<()> {
    let mut conn = pool.get().await.map_err(|error| {
        AutumnError::internal_server_error_msg(format!("pg pool error: {error}"))
    })?;

    diesel::sql_query(HOOK_DISCARD_PENDING_SQL)
        .bind::<diesel::sql_types::Text, _>(hook_id)
        .bind::<diesel::sql_types::Text, _>(owner)
        .execute(&mut *conn)
        .await
        .map(|_| ())
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook pending discard failed: {error}"
            ))
        })
}

/// Mark a staged create/update commit hook as permanently non-dispatchable
/// after the regular `after_*` hook failed or panicked.
///
/// This retries transient pool or database failures a bounded number of times
/// so callers can return the original hook error without hanging forever.
pub async fn mark_repository_commit_hook_after_hook_failed(
    pool: &RtPool,
    hook_id: &str,
    owner: &str,
    failure: impl Into<String>,
) {
    let failure = failure.into();
    for attempt in 1..=HOOK_AFTER_HOOK_FAILURE_MARK_MAX_ATTEMPTS {
        match mark_repository_commit_hook_after_hook_failed_once(pool, hook_id, owner, &failure)
            .await
        {
            Ok(true) => return,
            Ok(false) => {
                tracing::warn!(
                    hook_id = %hook_id,
                    "repository commit hook staged row was already unavailable while marking after-hook failure"
                );
                return;
            }
            Err(error) => {
                if attempt == HOOK_AFTER_HOOK_FAILURE_MARK_MAX_ATTEMPTS {
                    tracing::warn!(
                        hook_id = %hook_id,
                        error = %error,
                        attempts = HOOK_AFTER_HOOK_FAILURE_MARK_MAX_ATTEMPTS,
                        "failed to mark repository commit hook after-hook failure; giving up so the committed mutation can return"
                    );
                    return;
                }
                tracing::warn!(
                    hook_id = %hook_id,
                    error = %error,
                    attempt,
                    max_attempts = HOOK_AFTER_HOOK_FAILURE_MARK_MAX_ATTEMPTS,
                    "failed to mark repository commit hook after-hook failure; retrying"
                );
                tokio::time::sleep(HOOK_AFTER_HOOK_FAILURE_MARK_RETRY_SLEEP).await;
            }
        }
    }
}

async fn mark_repository_commit_hook_after_hook_failed_once(
    pool: &RtPool,
    hook_id: &str,
    owner: &str,
    failure: &str,
) -> AutumnResult<bool> {
    let mut conn = pool.get().await.map_err(|error| {
        AutumnError::internal_server_error_msg(format!("pg pool error: {error}"))
    })?;

    diesel::sql_query(HOOK_AFTER_HOOK_FAILED_SQL)
        .bind::<diesel::sql_types::Text, _>(failure)
        .bind::<diesel::sql_types::Text, _>(hook_id)
        .bind::<diesel::sql_types::Text, _>(owner)
        .execute(&mut *conn)
        .await
        .map(|rows| rows > 0)
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook after-hook failure mark failed: {error}"
            ))
        })
}

/// Catch panics from a regular repository `after_*` hook while preserving its
/// `AutumnResult`.
///
/// # Errors
///
/// Returns `Err` when the hook future panics. A hook that completes normally
/// still returns its own `AutumnResult` inside `Ok`.
pub async fn catch_repository_after_hook_unwind<Fut>(
    future: Fut,
) -> Result<AutumnResult<()>, Box<dyn Any + Send>>
where
    Fut: Future<Output = AutumnResult<()>> + Send,
{
    std::panic::AssertUnwindSafe(future).catch_unwind().await
}

#[doc(hidden)]
pub fn start_repository_commit_hook_pending_finalizer_heartbeat(
    pool: RtPool,
    hook_id: String,
    owner: String,
) -> RepositoryCommitHookPendingHeartbeat {
    let shutdown = CancellationToken::new();
    let heartbeat_shutdown = shutdown.child_token();
    tokio::spawn(heartbeat_repository_commit_hook_pending_finalizer(
        pool,
        hook_id,
        owner,
        heartbeat_shutdown,
    ));
    RepositoryCommitHookPendingHeartbeat::new(shutdown)
}

fn serialize_repository_commit_hook_payloads<C, R>(
    context: &C,
    record: &R,
) -> AutumnResult<(String, String)>
where
    C: Serialize + ?Sized,
    R: Serialize + ?Sized,
{
    let context = serde_json::to_string(context).map_err(|error| {
        AutumnError::internal_server_error_msg(format!(
            "serialize repository commit hook context: {error}"
        ))
    })?;
    let record = serde_json::to_string(record).map_err(|error| {
        AutumnError::internal_server_error_msg(format!(
            "serialize repository commit hook record: {error}"
        ))
    })?;
    let context = embed_originating_tenant_in_context(context)?;

    Ok((context, record))
}

/// Postgres identity: the persisted `context` is byte-identical to the serialized
/// `MutationContext`, so the Postgres worker's behaviour is unchanged. The
/// fallible signature (and lint allowances) mirror the `SQLite` arm, which can
/// genuinely fail while re-serializing the tenant-embedded context.
#[cfg(not(feature = "sqlite"))]
#[inline]
#[allow(clippy::unnecessary_wraps, clippy::missing_const_for_fn)]
fn embed_originating_tenant_in_context(context: String) -> AutumnResult<String> {
    Ok(context)
}

/// `SQLite`: capture the ambient `CURRENT_TENANT` at enqueue/finalize time (both run
/// inside the request's tenant scope) and embed it into the persisted `context`
/// JSON under [`TENANT_CONTEXT_KEY`], so the durable worker can re-establish the
/// SAME tenant before executing the hook. `MutationContext` carries no tenant
/// field, so without this a `tenant_scoped` repository touched by a durable hook
/// would lose its scope across the process boundary. A hook enqueued with no
/// ambient tenant embeds nothing (the worker then runs it with the tenant UNSET,
/// so a `tenant_scoped` repo fails closed rather than touching a default scope).
#[cfg(feature = "sqlite")]
fn embed_originating_tenant_in_context(context: String) -> AutumnResult<String> {
    let tenant = crate::tenancy::CURRENT_TENANT
        .try_with(std::clone::Clone::clone)
        .ok()
        .flatten();
    // Fail closed: only embed a genuinely non-blank tenant. A blank or
    // whitespace-only ambient tenant is never persisted, so the worker runs the
    // hook with `CURRENT_TENANT` UNSET and a `tenant_scoped` repo fails closed
    // rather than resolving to `tenant_id = ''` (or whitespace).
    let tenant = tenant.filter(|value| !value.trim().is_empty());
    let Some(tenant) = tenant else {
        return Ok(context);
    };

    let mut value: Value = serde_json::from_str(&context).map_err(|error| {
        AutumnError::internal_server_error_msg(format!(
            "embed tenant into repository commit hook context: {error}"
        ))
    })?;
    if let Value::Object(map) = &mut value {
        map.insert(TENANT_CONTEXT_KEY.to_owned(), Value::String(tenant));
    }
    serde_json::to_string(&value).map_err(|error| {
        AutumnError::internal_server_error_msg(format!(
            "reserialize repository commit hook context with tenant: {error}"
        ))
    })
}

/// `SQLite`: pull the originating tenant back out of the persisted `context` JSON
/// (and strip the reserved key so the generated runner still deserializes a clean
/// `MutationContext`). Returns the parsed context [`Value`] and the tenant to
/// re-establish (`None` when the hook was enqueued outside any tenant scope).
#[cfg(feature = "sqlite")]
fn extract_originating_tenant_from_context(context: &str) -> (Value, Option<String>) {
    // A context that does not parse is handled downstream when the runner
    // re-parses it; surface it untouched with no tenant so the hook still runs
    // (and fails closed if it touches a tenant_scoped repo).
    serde_json::from_str::<Value>(context).map_or((Value::Null, None), |mut value| {
        let tenant = match &mut value {
            Value::Object(map) => map
                .remove(TENANT_CONTEXT_KEY)
                .and_then(|value| match value {
                    // Fail closed: a blank or whitespace-only persisted tenant is
                    // NOT an established tenant. Normalize it to `None` so the drain
                    // loop leaves `CURRENT_TENANT` UNSET rather than scoping a
                    // `tenant_scoped` repo to `tenant_id = ''` (or whitespace).
                    // A non-blank tenant is preserved verbatim (never trimmed).
                    Value::String(tenant) if tenant.trim().is_empty() => None,
                    Value::String(tenant) => Some(tenant),
                    _ => None,
                }),
            _ => None,
        };
        (value, tenant)
    })
}

/// `SQLite`: a UTC `YYYY-MM-DD HH:MM:SS.fff` timestamp string. Ordered
/// lexicographically it compares correctly against `CURRENT_TIMESTAMP` defaults
/// (which share the leading second-precision prefix), and the fractional part
/// gives the worker sub-second retry/lease resolution.
#[cfg(feature = "sqlite")]
fn sqlite_timestamp(instant: chrono::DateTime<chrono::Utc>) -> String {
    instant.format("%Y-%m-%d %H:%M:%S%.3f").to_string()
}

#[cfg(all(feature = "ws", not(feature = "sqlite")))]
pub fn start_repository_commit_hook_worker(
    pool: PgPool,
    channels: Option<crate::channels::Channels>,
    shutdown: CancellationToken,
) {
    register_inventory_repository_commit_hook_runners();
    if !should_start_repository_commit_hook_worker(&registered_handler_keys()) {
        return;
    }

    if let Some(ch) = channels.clone() {
        let kick_state = repository_commit_hook_kick_state(&pool);
        let _ = kick_state.channels.set(ch);
    }

    let worker_id = repository_commit_hook_worker_id();
    tokio::spawn(async move {
        if let Some(ch) = channels {
            CURRENT_CHANNELS
                .scope(ch, async move {
                    loop {
                        tokio::select! {
                            () = shutdown.cancelled() => break,
                            () = tokio::time::sleep(HOOK_WORKER_IDLE_SLEEP) => {
                                recover_stale_repository_commit_hooks(&pool, &worker_id).await;
                                drain_ready_repository_commit_hooks(&pool, &worker_id, 32).await;
                            }
                        }
                    }
                })
                .await;
        } else {
            loop {
                tokio::select! {
                    () = shutdown.cancelled() => break,
                    () = tokio::time::sleep(HOOK_WORKER_IDLE_SLEEP) => {
                        recover_stale_repository_commit_hooks(&pool, &worker_id).await;
                        drain_ready_repository_commit_hooks(&pool, &worker_id, 32).await;
                    }
                }
            }
        }
    });
}

#[cfg(all(not(feature = "ws"), not(feature = "sqlite")))]
pub fn start_repository_commit_hook_worker(pool: PgPool, shutdown: CancellationToken) {
    register_inventory_repository_commit_hook_runners();
    if !should_start_repository_commit_hook_worker(&registered_handler_keys()) {
        return;
    }

    let worker_id = repository_commit_hook_worker_id();
    tokio::spawn(async move {
        loop {
            tokio::select! {
                () = shutdown.cancelled() => break,
                () = tokio::time::sleep(HOOK_WORKER_IDLE_SLEEP) => {
                    recover_stale_repository_commit_hooks(&pool, &worker_id).await;
                    drain_ready_repository_commit_hooks(&pool, &worker_id, 32).await;
                }
            }
        }
    });
}

// ── `SQLite` durable worker (#1996 item 5) ─────────────────────────────────────
//
// Single-node, single-writer: no `LISTEN/NOTIFY`, no `FOR UPDATE SKIP LOCKED`.
// The worker polls on the idle sleep AND wakes immediately on an in-process
// `Notify` kick, then claims one ready row at a time under a `BEGIN IMMEDIATE`
// write lock (`sqlite_claim_next_repository_commit_hook`). Graceful shutdown is
// the same `tokio::select!` on the child `CancellationToken` the Postgres worker
// uses: the loop finishes the in-flight `drain` iteration (claim → run →
// ack/nack) it is on and settles it before observing `cancelled()` on the next
// pass, so no leased row is abandoned mid-flight.
#[cfg(all(feature = "ws", feature = "sqlite"))]
pub fn start_repository_commit_hook_worker(
    pool: RtPool,
    channels: Option<crate::channels::Channels>,
    shutdown: CancellationToken,
) {
    register_inventory_repository_commit_hook_runners();
    if !should_start_repository_commit_hook_worker(&registered_handler_keys()) {
        return;
    }

    let worker_id = repository_commit_hook_worker_id();
    let kick = sqlite_repository_commit_hook_kick(&pool);
    tokio::spawn(async move {
        if let Some(ch) = channels {
            CURRENT_CHANNELS
                .scope(
                    ch,
                    sqlite_repository_commit_hook_worker_loop(pool, worker_id, kick, shutdown),
                )
                .await;
        } else {
            sqlite_repository_commit_hook_worker_loop(pool, worker_id, kick, shutdown).await;
        }
    });
}

#[cfg(all(not(feature = "ws"), feature = "sqlite"))]
pub fn start_repository_commit_hook_worker(pool: RtPool, shutdown: CancellationToken) {
    register_inventory_repository_commit_hook_runners();
    if !should_start_repository_commit_hook_worker(&registered_handler_keys()) {
        return;
    }

    let worker_id = repository_commit_hook_worker_id();
    let kick = sqlite_repository_commit_hook_kick(&pool);
    tokio::spawn(sqlite_repository_commit_hook_worker_loop(
        pool, worker_id, kick, shutdown,
    ));
}

#[cfg(feature = "sqlite")]
async fn sqlite_repository_commit_hook_worker_loop(
    pool: RtPool,
    worker_id: String,
    kick: Arc<Notify>,
    shutdown: CancellationToken,
) {
    loop {
        tokio::select! {
            biased;
            () = shutdown.cancelled() => break,
            () = kick.notified() => {
                recover_stale_repository_commit_hooks(&pool, &worker_id).await;
                sqlite_drain_ready_repository_commit_hooks(&pool, &worker_id, 32).await;
            }
            () = tokio::time::sleep(HOOK_WORKER_IDLE_SLEEP) => {
                recover_stale_repository_commit_hooks(&pool, &worker_id).await;
                sqlite_drain_ready_repository_commit_hooks(&pool, &worker_id, 32).await;
            }
        }
    }
}

/// Nudge dispatch after a mutation commits, without relying on this replica for
/// durability. Polling workers on all replicas can still claim the row later.
#[cfg(not(feature = "sqlite"))]
pub fn kick_repository_commit_hook_dispatcher(pool: &PgPool) {
    register_inventory_repository_commit_hook_runners();
    if !should_start_repository_commit_hook_worker(&registered_handler_keys()) {
        return;
    }

    let state = repository_commit_hook_kick_state(pool);
    if state.request_kick() {
        state.notify.notify_one();
    }
}

/// `SQLite` variant of the commit-hook dispatcher kick (#1996 item 5).
///
/// `SQLite` has no `LISTEN/NOTIFY`, but it is single-node, so an in-process
/// `Notify` wakes this pool's poll loop the instant a mutation commits. A missed
/// kick is harmless: the loop also wakes on its idle sleep and drains, so
/// durability never depends on the kick landing.
#[cfg(feature = "sqlite")]
pub fn kick_repository_commit_hook_dispatcher(pool: &RtPool) {
    register_inventory_repository_commit_hook_runners();
    if !should_start_repository_commit_hook_worker(&registered_handler_keys()) {
        return;
    }

    sqlite_repository_commit_hook_kick(pool).notify_one();
}

#[cfg(not(feature = "sqlite"))]
fn repository_commit_hook_kick_state(pool: &PgPool) -> Arc<RepositoryCommitHookKickState> {
    let key = repository_commit_hook_pool_key(pool);
    let registry = REPOSITORY_COMMIT_HOOK_KICKERS.get_or_init(|| RwLock::new(HashMap::new()));
    let existing = {
        let registry = registry
            .read()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        registry.get(&key).cloned()
    };
    if let Some(state) = existing {
        return state;
    }

    let mut registry = registry
        .write()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    if let Some(state) = registry.get(&key).cloned() {
        return state;
    }

    let state = Arc::new(RepositoryCommitHookKickState::default());
    spawn_repository_commit_hook_kick_worker(pool.clone(), state.clone());
    registry.insert(key, state.clone());
    state
}

#[cfg(not(feature = "sqlite"))]
fn repository_commit_hook_pool_key(pool: &PgPool) -> usize {
    std::ptr::from_ref(pool.manager()).addr()
}

#[cfg(not(feature = "sqlite"))]
fn spawn_repository_commit_hook_kick_worker(
    pool: PgPool,
    state: Arc<RepositoryCommitHookKickState>,
) {
    let worker_id = repository_commit_hook_worker_id();
    tokio::spawn(async move {
        loop {
            state.notify.notified().await;
            while state.take_pending_kick() {
                #[cfg(feature = "ws")]
                {
                    let ch_opt = state.channels.get().cloned().or_else(get_global_channels);
                    if let Some(ch) = ch_opt {
                        let pool_clone = pool.clone();
                        let worker_id_clone = worker_id.clone();
                        CURRENT_CHANNELS
                            .scope(ch, async move {
                                drain_ready_repository_commit_hooks(
                                    &pool_clone,
                                    &worker_id_clone,
                                    32,
                                )
                                .await;
                            })
                            .await;
                        continue;
                    }
                }
                drain_ready_repository_commit_hooks(&pool, &worker_id, 32).await;
            }
        }
    });
}

#[cfg(not(feature = "sqlite"))]
async fn drain_ready_repository_commit_hooks(pool: &PgPool, worker_id: &str, max_rows: usize) {
    for _ in 0..max_rows {
        let Some(row) = pg_claim_next_repository_commit_hook(pool, worker_id).await else {
            break;
        };

        let heartbeat_shutdown = CancellationToken::new();
        let heartbeat_task = tokio::spawn(heartbeat_repository_commit_hook_claim(
            pool.clone(),
            row.id.clone(),
            worker_id.to_owned(),
            heartbeat_shutdown.child_token(),
        ));
        let result = run_repository_commit_hook_row(&row).await;

        match result {
            Ok(()) => {
                if let Err(error) =
                    ack_repository_commit_hook_success(pool, &row.id, worker_id).await
                {
                    tracing::warn!(
                        hook_id = %row.id,
                        error = %error,
                        "failed to ack repository commit hook success"
                    );
                }
            }
            Err(error) => {
                let failures_total = crate::db::record_after_commit_failure();
                tracing::error!(
                    hook_id = %row.id,
                    handler_key = %row.handler_key,
                    hook_name = %row.hook_name,
                    autumn.after_commit.failures_total = failures_total,
                    "repository after_commit hook failed: {error}"
                );
                if let Err(nack_error) =
                    pg_nack_repository_commit_hook_failure(pool, &row.id, worker_id, &error, &row)
                        .await
                {
                    tracing::warn!(
                        hook_id = %row.id,
                        error = %nack_error,
                        "failed to record repository commit hook failure"
                    );
                }
            }
        }

        heartbeat_shutdown.cancel();
        if let Err(error) = heartbeat_task.await {
            tracing::warn!(
                hook_id = %row.id,
                error = %error,
                "repository commit hook heartbeat task failed"
            );
        }
    }
}

#[cfg(not(feature = "sqlite"))]
async fn run_repository_commit_hook_row(row: &RepositoryCommitHookRow) -> Result<(), String> {
    let context = serde_json::from_str::<Value>(&row.context)
        .map_err(|error| format!("decode repository hook context: {error}"))?;
    run_repository_commit_hook_row_value(row, context).await
}

/// Run a claimed hook with an already-parsed `context` [`Value`]. The `SQLite`
/// worker parses (and strips the tenant key from) the context before calling this;
/// the Postgres worker parses it in `run_repository_commit_hook_row`.
async fn run_repository_commit_hook_row_value(
    row: &RepositoryCommitHookRow,
    context: Value,
) -> Result<(), String> {
    let record = serde_json::from_str::<Value>(&row.record)
        .map_err(|error| format!("decode repository hook record: {error}"))?;
    let result = std::panic::AssertUnwindSafe(run_registered_repository_commit_hook(
        &row.handler_key,
        &row.hook_name,
        context,
        record,
    ))
    .catch_unwind()
    .await;

    match result {
        Ok(Ok(())) => Ok(()),
        Ok(Err(error)) => Err(error.to_string()),
        Err(panic) => Err(format_repository_commit_hook_panic(&*panic)),
    }
}

async fn run_registered_repository_commit_hook(
    handler_key: &str,
    hook_name: &str,
    context: Value,
    record: Value,
) -> AutumnResult<()> {
    let runner = {
        let registry = REPOSITORY_COMMIT_HOOK_RUNNERS
            .get_or_init(|| RwLock::new(HashMap::new()))
            .read()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        registry
            .get(handler_key)
            .and_then(|registration| registration.runner(hook_name))
    };

    let Some(runner) = runner else {
        return Err(AutumnError::internal_server_error_msg(format!(
            "repository commit hook runner not registered: handler_key={handler_key}, hook_name={hook_name}"
        )));
    };

    runner(context, record).await
}

async fn heartbeat_repository_commit_hook_claim(
    pool: RtPool,
    hook_id: String,
    worker_id: String,
    shutdown: CancellationToken,
) {
    loop {
        tokio::select! {
            () = shutdown.cancelled() => break,
            () = tokio::time::sleep(HOOK_CLAIM_HEARTBEAT_INTERVAL) => {
                match extend_repository_commit_hook_claim(&pool, &hook_id, &worker_id).await {
                    Ok(true) => {}
                    Ok(false) => break,
                    Err(error) => {
                        tracing::warn!(
                            hook_id = %hook_id,
                            error = %error,
                            "failed to extend repository commit hook claim"
                        );
                    }
                }
            }
        }
    }
}

async fn heartbeat_repository_commit_hook_pending_finalizer(
    pool: RtPool,
    hook_id: String,
    owner: String,
    shutdown: CancellationToken,
) {
    loop {
        tokio::select! {
            () = shutdown.cancelled() => break,
            () = tokio::time::sleep(HOOK_PENDING_FINALIZER_HEARTBEAT_INTERVAL) => {
                match extend_repository_commit_hook_pending_finalizer(&pool, &hook_id, &owner).await {
                    Ok(true) => {}
                    Ok(false) => break,
                    Err(error) => {
                        tracing::warn!(
                            hook_id = %hook_id,
                            error = %error,
                            "failed to extend repository commit hook pending finalizer lease"
                        );
                    }
                }
            }
        }
    }
}

fn registered_handler_keys() -> Vec<String> {
    REPOSITORY_COMMIT_HOOK_RUNNERS
        .get_or_init(|| RwLock::new(HashMap::new()))
        .read()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
        .keys()
        .cloned()
        .collect()
}

const fn should_start_repository_commit_hook_worker(handler_keys: &[String]) -> bool {
    !handler_keys.is_empty()
}

#[cfg(not(feature = "sqlite"))]
async fn pg_claim_next_repository_commit_hook(
    pool: &PgPool,
    worker_id: &str,
) -> Option<RepositoryCommitHookRow> {
    let handler_keys = registered_handler_keys();
    if handler_keys.is_empty() {
        return None;
    }

    let mut conn = pool.get().await.ok()?;
    let sql = format!(
        "UPDATE autumn_repository_commit_hooks \
         SET status = 'running', started_at = NOW(), claimed_by = $2, claimed_at = NOW() \
         WHERE id = ( \
           SELECT id FROM autumn_repository_commit_hooks \
           WHERE status = 'enqueued' \
             AND run_at <= NOW() \
             AND handler_key = ANY($1) \
           ORDER BY run_at ASC, enqueued_at ASC \
           LIMIT 1 \
           FOR UPDATE SKIP LOCKED \
         ) \
         RETURNING {HOOK_SELECT_COLS}"
    );

    diesel::sql_query(sql)
        .bind::<diesel::sql_types::Array<diesel::sql_types::Text>, _>(handler_keys)
        .bind::<diesel::sql_types::Text, _>(worker_id)
        .get_result::<RepositoryCommitHookRow>(&mut *conn)
        .await
        .optional()
        .unwrap_or_else(|error| {
            if is_missing_hook_table_error(&error) {
                tracing::debug!(
                    error = %error,
                    "repository commit hook queue table is not available yet"
                );
            } else {
                tracing::warn!(error = %error, "repository commit hook claim query failed");
            }
            None
        })
}

async fn ack_repository_commit_hook_success(
    pool: &RtPool,
    hook_id: &str,
    worker_id: &str,
) -> AutumnResult<bool> {
    let mut conn = pool.get().await.map_err(|error| {
        AutumnError::internal_server_error_msg(format!("commit hook pool error: {error}"))
    })?;

    diesel::sql_query(HOOK_ACK_SUCCESS_SQL)
        .bind::<diesel::sql_types::Text, _>(hook_id)
        .bind::<diesel::sql_types::Text, _>(worker_id)
        .execute(&mut *conn)
        .await
        .map(|rows| rows > 0)
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook ack failed: {error}"
            ))
        })
}

async fn extend_repository_commit_hook_claim(
    pool: &RtPool,
    hook_id: &str,
    worker_id: &str,
) -> AutumnResult<bool> {
    let mut conn = pool.get().await.map_err(|error| {
        AutumnError::internal_server_error_msg(format!("commit hook pool error: {error}"))
    })?;

    diesel::sql_query(HOOK_EXTEND_CLAIM_SQL)
        .bind::<diesel::sql_types::Text, _>(hook_id)
        .bind::<diesel::sql_types::Text, _>(worker_id)
        .execute(&mut *conn)
        .await
        .map(|rows| rows > 0)
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook claim heartbeat failed: {error}"
            ))
        })
}

async fn extend_repository_commit_hook_pending_finalizer(
    pool: &RtPool,
    hook_id: &str,
    owner: &str,
) -> AutumnResult<bool> {
    let mut conn = pool.get().await.map_err(|error| {
        AutumnError::internal_server_error_msg(format!("commit hook pool error: {error}"))
    })?;

    diesel::sql_query(HOOK_EXTEND_PENDING_FINALIZER_SQL)
        .bind::<diesel::sql_types::Text, _>(hook_id)
        .bind::<diesel::sql_types::Text, _>(owner)
        .execute(&mut *conn)
        .await
        .map(|rows| rows > 0)
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook pending finalizer heartbeat failed: {error}"
            ))
        })
}

#[cfg(not(feature = "sqlite"))]
async fn pg_nack_repository_commit_hook_failure(
    pool: &PgPool,
    hook_id: &str,
    worker_id: &str,
    error: &str,
    row: &RepositoryCommitHookRow,
) -> AutumnResult<bool> {
    let mut conn = pool.get().await.map_err(|error| {
        AutumnError::internal_server_error_msg(format!("pg pool error: {error}"))
    })?;

    if row.attempt < row.max_attempts {
        let delay_ms = retry_delay_ms(row.initial_backoff_ms, row.attempt);
        diesel::sql_query(
            "UPDATE autumn_repository_commit_hooks \
             SET status = 'enqueued', \
                 attempt = attempt + 1, \
                 run_at = NOW() + ($1::BIGINT * INTERVAL '1 millisecond'), \
                 started_at = NULL, \
                 finished_at = NULL, \
                 claimed_by = NULL, \
                 claimed_at = NULL, \
                 last_error = $2 \
             WHERE id = $3 AND claimed_by = $4 AND status = 'running'",
        )
        .bind::<diesel::sql_types::BigInt, _>(delay_ms)
        .bind::<diesel::sql_types::Text, _>(error)
        .bind::<diesel::sql_types::Text, _>(hook_id)
        .bind::<diesel::sql_types::Text, _>(worker_id)
        .execute(&mut *conn)
        .await
        .map(|rows| rows > 0)
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook retry failed: {error}"
            ))
        })
    } else {
        diesel::sql_query(
            "UPDATE autumn_repository_commit_hooks \
             SET status = 'failed', \
                 finished_at = NOW(), \
                 claimed_by = NULL, \
                 claimed_at = NULL, \
                 last_error = $1 \
             WHERE id = $2 AND claimed_by = $3 AND status = 'running'",
        )
        .bind::<diesel::sql_types::Text, _>(error)
        .bind::<diesel::sql_types::Text, _>(hook_id)
        .bind::<diesel::sql_types::Text, _>(worker_id)
        .execute(&mut *conn)
        .await
        .map(|rows| rows > 0)
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook dead-letter failed: {error}"
            ))
        })
    }
}

async fn recover_stale_repository_commit_hooks(pool: &RtPool, worker_id: &str) {
    let Ok(mut conn) = pool.get().await else {
        return;
    };

    // Postgres computes the stale cutoff inline (`NOW() - INTERVAL`); `SQLite` has
    // no interval arithmetic, so the cutoff is computed here and bound as a
    // comparable UTC timestamp string. Same recovery semantics on both.
    let running_result = crate::backend_select! {
        pg => {{
            let stale_after_ms =
                i64::try_from(HOOK_STALE_CLAIM_AFTER.as_millis()).unwrap_or(i64::MAX);
            diesel::sql_query(HOOK_RECOVER_STALE_RUNNING_SQL)
                .bind::<diesel::sql_types::Text, _>(format!("stale claim recovered by {worker_id}"))
                .bind::<diesel::sql_types::BigInt, _>(stale_after_ms)
                .execute(&mut *conn)
                .await
        }},
        sqlite => {{
            let cutoff = sqlite_timestamp(sqlite_stale_claim_cutoff());
            diesel::sql_query(HOOK_RECOVER_STALE_RUNNING_SQL)
                .bind::<diesel::sql_types::Text, _>(format!("stale claim recovered by {worker_id}"))
                .bind::<diesel::sql_types::Text, _>(cutoff)
                .execute(&mut *conn)
                .await
        }},
    };
    if let Err(error) = running_result {
        if is_missing_hook_table_error(&error) {
            tracing::debug!(
                error = %error,
                "repository commit hook queue table is not available yet"
            );
        } else {
            tracing::warn!(error = %error, "repository commit hook stale recovery failed");
        }
    }

    let pending_result = crate::backend_select! {
        pg => {{
            let stale_after_ms =
                i64::try_from(HOOK_STALE_CLAIM_AFTER.as_millis()).unwrap_or(i64::MAX);
            diesel::sql_query(HOOK_RECOVER_STALE_PENDING_SQL)
                .bind::<diesel::sql_types::Text, _>(format!(
                    "stale pending after hook recovered by {worker_id}"
                ))
                .bind::<diesel::sql_types::BigInt, _>(stale_after_ms)
                .execute(&mut *conn)
                .await
        }},
        sqlite => {{
            let cutoff = sqlite_timestamp(sqlite_stale_claim_cutoff());
            diesel::sql_query(HOOK_RECOVER_STALE_PENDING_SQL)
                .bind::<diesel::sql_types::Text, _>(format!(
                    "stale pending after hook recovered by {worker_id}"
                ))
                .bind::<diesel::sql_types::Text, _>(cutoff)
                .execute(&mut *conn)
                .await
        }},
    };
    if let Err(error) = pending_result {
        if is_missing_hook_table_error(&error) {
            tracing::debug!(
                error = %error,
                "repository commit hook queue table is not available yet"
            );
        } else {
            tracing::warn!(
                error = %error,
                "repository commit hook stale pending recovery failed"
            );
        }
    }
}

/// `SQLite`: the UTC instant before which a still-`running` (or leased-pending)
/// claim is considered abandoned by a dead worker.
#[cfg(feature = "sqlite")]
fn sqlite_stale_claim_cutoff() -> chrono::DateTime<chrono::Utc> {
    let stale_after = chrono::Duration::from_std(HOOK_STALE_CLAIM_AFTER)
        .unwrap_or_else(|_| chrono::Duration::seconds(60));
    chrono::Utc::now() - stale_after
}

// ── `SQLite` claim / drain / nack (#1996 item 5) ───────────────────────────────

/// Claim the next ready hook row under a `BEGIN IMMEDIATE` write lock. `SQLite` is
/// single-writer, so instead of `FOR UPDATE SKIP LOCKED` the worker takes the
/// write lock up front, `SELECT`s the oldest ready+registered row, flips it to
/// `running`, and returns it — no other writer can interleave between the select
/// and the update.
#[cfg(feature = "sqlite")]
async fn sqlite_claim_next_repository_commit_hook(
    pool: &RtPool,
    worker_id: &str,
) -> Option<RepositoryCommitHookRow> {
    let handler_keys = registered_handler_keys();
    if handler_keys.is_empty() {
        return None;
    }

    let mut conn = pool.get().await.ok()?;
    let now = sqlite_timestamp(chrono::Utc::now());
    // The registered handler keys are internal Rust type-path strings (no
    // user input); inline them as an escaped `IN (...)` literal list so the raw
    // `sql_query` needs a fixed bind arity (`run_at <= ?` only). Single quotes
    // are still doubled defensively.
    let handler_in_list = handler_keys
        .iter()
        .map(|key| format!("'{}'", key.replace('\'', "''")))
        .collect::<Vec<_>>()
        .join(", ");
    let select_sql = format!(
        "SELECT {HOOK_SELECT_COLS} FROM autumn_repository_commit_hooks \
         WHERE status = 'enqueued' \
           AND run_at <= ? \
           AND handler_key IN ({handler_in_list}) \
         ORDER BY run_at ASC, enqueued_at ASC \
         LIMIT 1"
    );
    let worker_id = worker_id.to_owned();

    let claim = crate::db::scoped_immediate_transaction::<
        Option<RepositoryCommitHookRow>,
        diesel::result::Error,
        _,
    >(&mut conn, move |conn| {
        async move {
            let Some(row) = diesel::sql_query(select_sql)
                .bind::<diesel::sql_types::Text, _>(now.clone())
                .get_result::<RepositoryCommitHookRow>(&mut *conn)
                .await
                .optional()?
            else {
                return Ok(None);
            };

            diesel::sql_query(
                "UPDATE autumn_repository_commit_hooks \
                 SET status = 'running', started_at = ?, claimed_by = ?, claimed_at = ? \
                 WHERE id = ? AND status = 'enqueued'",
            )
            .bind::<diesel::sql_types::Text, _>(now.clone())
            .bind::<diesel::sql_types::Text, _>(worker_id)
            .bind::<diesel::sql_types::Text, _>(now)
            .bind::<diesel::sql_types::Text, _>(row.id.clone())
            .execute(&mut *conn)
            .await?;

            Ok(Some(row))
        }
        .scope_boxed()
    })
    .await;

    match claim {
        Ok(row) => row,
        Err(error) => {
            if is_missing_hook_table_error(&error) {
                tracing::debug!(
                    error = %error,
                    "repository commit hook queue table is not available yet"
                );
            } else {
                tracing::warn!(error = %error, "repository commit hook claim query failed");
            }
            None
        }
    }
}

/// `SQLite` drain loop: claim → lease heartbeat → run (under the originating tenant
/// scope) → ack/nack, one row at a time. Mirrors the Postgres
/// `drain_ready_repository_commit_hooks` shape.
#[cfg(feature = "sqlite")]
async fn sqlite_drain_ready_repository_commit_hooks(
    pool: &RtPool,
    worker_id: &str,
    max_rows: usize,
) {
    for _ in 0..max_rows {
        let Some(row) = sqlite_claim_next_repository_commit_hook(pool, worker_id).await else {
            break;
        };

        let heartbeat_shutdown = CancellationToken::new();
        let heartbeat_task = tokio::spawn(heartbeat_repository_commit_hook_claim(
            pool.clone(),
            row.id.clone(),
            worker_id.to_owned(),
            heartbeat_shutdown.child_token(),
        ));

        // Tenant isolation (INVIOLABLE): re-establish the SAME tenant the hook was
        // enqueued under before running it, so a `tenant_scoped` repository the
        // hook touches resolves to the originating tenant — never another tenant,
        // and never a default/global scope. A hook enqueued with no tenant runs
        // with `CURRENT_TENANT` UNSET, so such a repo fails closed.
        let (context_value, tenant) = extract_originating_tenant_from_context(&row.context);
        let result = match tenant {
            Some(tenant) => {
                crate::tenancy::CURRENT_TENANT
                    .scope(
                        Some(tenant),
                        run_repository_commit_hook_row_value(&row, context_value),
                    )
                    .await
            }
            None => run_repository_commit_hook_row_value(&row, context_value).await,
        };

        match result {
            Ok(()) => {
                if let Err(error) =
                    ack_repository_commit_hook_success(pool, &row.id, worker_id).await
                {
                    tracing::warn!(
                        hook_id = %row.id,
                        error = %error,
                        "failed to ack repository commit hook success"
                    );
                }
            }
            Err(error) => {
                let failures_total = crate::db::record_after_commit_failure();
                tracing::error!(
                    hook_id = %row.id,
                    handler_key = %row.handler_key,
                    hook_name = %row.hook_name,
                    autumn.after_commit.failures_total = failures_total,
                    "repository after_commit hook failed: {error}"
                );
                if let Err(nack_error) = sqlite_nack_repository_commit_hook_failure(
                    pool, &row.id, worker_id, &error, &row,
                )
                .await
                {
                    tracing::warn!(
                        hook_id = %row.id,
                        error = %nack_error,
                        "failed to record repository commit hook failure"
                    );
                }
            }
        }

        heartbeat_shutdown.cancel();
        if let Err(error) = heartbeat_task.await {
            tracing::warn!(
                hook_id = %row.id,
                error = %error,
                "repository commit hook heartbeat task failed"
            );
        }
    }
}

/// `SQLite` retry / dead-letter. Same attempt-count and exponential-backoff
/// semantics as the Postgres `pg_nack_*`, but the `run_at` retry target is
/// computed in Rust (`SQLite` has no `NOW() + INTERVAL`) and bound as a UTC string.
#[cfg(feature = "sqlite")]
async fn sqlite_nack_repository_commit_hook_failure(
    pool: &RtPool,
    hook_id: &str,
    worker_id: &str,
    error: &str,
    row: &RepositoryCommitHookRow,
) -> AutumnResult<bool> {
    let mut conn = pool.get().await.map_err(|error| {
        AutumnError::internal_server_error_msg(format!("commit hook pool error: {error}"))
    })?;

    if row.attempt < row.max_attempts {
        let delay_ms = retry_delay_ms(row.initial_backoff_ms, row.attempt);
        let run_at = sqlite_timestamp(
            chrono::Utc::now()
                + chrono::Duration::try_milliseconds(delay_ms)
                    .unwrap_or_else(chrono::Duration::zero),
        );
        diesel::sql_query(
            "UPDATE autumn_repository_commit_hooks \
             SET status = 'enqueued', \
                 attempt = attempt + 1, \
                 run_at = ?, \
                 started_at = NULL, \
                 finished_at = NULL, \
                 claimed_by = NULL, \
                 claimed_at = NULL, \
                 last_error = ? \
             WHERE id = ? AND claimed_by = ? AND status = 'running'",
        )
        .bind::<diesel::sql_types::Text, _>(run_at)
        .bind::<diesel::sql_types::Text, _>(error)
        .bind::<diesel::sql_types::Text, _>(hook_id)
        .bind::<diesel::sql_types::Text, _>(worker_id)
        .execute(&mut *conn)
        .await
        .map(|rows| rows > 0)
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook retry failed: {error}"
            ))
        })
    } else {
        diesel::sql_query(
            "UPDATE autumn_repository_commit_hooks \
             SET status = 'failed', \
                 finished_at = CURRENT_TIMESTAMP, \
                 claimed_by = NULL, \
                 claimed_at = NULL, \
                 last_error = ? \
             WHERE id = ? AND claimed_by = ? AND status = 'running'",
        )
        .bind::<diesel::sql_types::Text, _>(error)
        .bind::<diesel::sql_types::Text, _>(hook_id)
        .bind::<diesel::sql_types::Text, _>(worker_id)
        .execute(&mut *conn)
        .await
        .map(|rows| rows > 0)
        .map_err(|error| {
            AutumnError::internal_server_error_msg(format!(
                "repository commit hook dead-letter failed: {error}"
            ))
        })
    }
}

fn is_missing_hook_table_error(error: &diesel::result::Error) -> bool {
    let message = error.to_string().to_ascii_lowercase();
    message.contains("autumn_repository_commit_hooks")
        // Postgres reports the missing table as "does not exist" / "UndefinedTable";
        // SQLite reports it as "no such table: autumn_repository_commit_hooks".
        && (message.contains("does not exist")
            || message.contains("undefinedtable")
            || message.contains("no such table"))
}

fn retry_delay_ms(initial_backoff_ms: i64, attempt: i32) -> i64 {
    let exp = u32::try_from(attempt.saturating_sub(1)).unwrap_or(0);
    initial_backoff_ms.saturating_mul(2_i64.saturating_pow(exp))
}

fn format_repository_commit_hook_panic(payload: &(dyn Any + Send)) -> String {
    let message = payload
        .downcast_ref::<&str>()
        .copied()
        .or_else(|| payload.downcast_ref::<String>().map(String::as_str));

    message.map_or_else(
        || "repository commit hook panicked".to_owned(),
        |message| format!("repository commit hook panicked: {message}"),
    )
}

fn repository_commit_hook_id(
    idempotency_key: Option<&str>,
    idempotency_discriminator: Option<&str>,
    handler_key: &str,
    hook_name: &str,
    record: &str,
) -> String {
    let Some(idempotency_key) = idempotency_key.filter(|key| !key.is_empty()) else {
        return uuid::Uuid::new_v4().to_string();
    };

    let mut hasher = sha2::Sha256::new();
    push_hook_id_component(&mut hasher, "handler", handler_key.as_bytes());
    push_hook_id_component(&mut hasher, "hook", hook_name.as_bytes());
    push_hook_id_component(&mut hasher, "idempotency", idempotency_key.as_bytes());
    if let Some(discriminator) = idempotency_discriminator {
        push_hook_id_component(&mut hasher, "mutation", discriminator.as_bytes());
    } else {
        push_hook_id_component(&mut hasher, "record", record.as_bytes());
    }
    format!("idempotent:{}", hex_lower(hasher.finalize()))
}

fn push_hook_id_component(hasher: &mut sha2::Sha256, label: &str, value: &[u8]) {
    hasher.update(label.as_bytes());
    hasher.update(b":");
    hasher.update((value.len() as u64).to_be_bytes());
    hasher.update(b":");
    hasher.update(value);
    hasher.update(b";");
}

fn hex_lower(bytes: impl AsRef<[u8]>) -> String {
    bytes.as_ref().iter().fold(
        String::with_capacity(bytes.as_ref().len() * 2),
        |mut out, byte| {
            use std::fmt::Write as _;
            let _ = write!(out, "{byte:02x}");
            out
        },
    )
}

fn repository_commit_hook_worker_id() -> String {
    format!("repository-hook-{}", uuid::Uuid::new_v4())
}

fn repository_commit_hook_pending_owner_id() -> String {
    format!("repository-hook-pending-{}", uuid::Uuid::new_v4())
}

#[cfg(feature = "ws")]
tokio::task_local! {
    pub static CURRENT_CHANNELS: crate::channels::Channels;
}

#[cfg(feature = "ws")]
static GLOBAL_CHANNELS: std::sync::RwLock<Option<crate::channels::Channels>> =
    std::sync::RwLock::new(None);

#[cfg(feature = "ws")]
pub fn set_global_channels(channels: crate::channels::Channels) {
    if let Ok(mut lock) = GLOBAL_CHANNELS.write() {
        *lock = Some(channels);
    }
}

#[cfg(feature = "ws")]
pub fn clear_global_channels() {
    if let Ok(mut lock) = GLOBAL_CHANNELS.write() {
        *lock = None;
    }
}

#[cfg(feature = "ws")]
#[must_use]
pub fn get_global_channels() -> Option<crate::channels::Channels> {
    CURRENT_CHANNELS
        .try_with(std::clone::Clone::clone)
        .ok()
        .or_else(|| GLOBAL_CHANNELS.read().ok().and_then(|lock| lock.clone()))
}

#[cfg(not(feature = "ws"))]
#[derive(Clone)]
pub struct Channels;

#[cfg(not(feature = "ws"))]
pub struct DummyBroadcast;

#[cfg(not(feature = "ws"))]
impl Channels {
    #[allow(clippy::unused_self)]
    pub const fn broadcast(&self) -> DummyBroadcast {
        DummyBroadcast
    }
}

#[cfg(not(feature = "ws"))]
impl DummyBroadcast {
    #[allow(clippy::unused_self, clippy::unnecessary_wraps)]
    pub fn publish_oob<T, S>(
        &self,
        _topic: &str,
        _id: &str,
        _swap: S,
        _fragment: &T,
    ) -> Result<(), std::convert::Infallible> {
        Ok(())
    }
}

#[cfg(not(feature = "ws"))]
#[allow(clippy::missing_const_for_fn)]
pub fn set_global_channels(_channels: Channels) {}

#[cfg(not(feature = "ws"))]
pub const fn clear_global_channels() {}

#[cfg(not(feature = "ws"))]
#[must_use]
#[allow(clippy::missing_const_for_fn)]
pub fn get_global_channels() -> Option<Channels> {
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[tokio::test]
    async fn registered_runner_executes_matching_hook() {
        let calls = Arc::new(AtomicUsize::new(0));
        let create_calls = calls.clone();
        let handler_key: &'static str = Box::leak(
            format!(
                "test::registered_runner_executes_matching_hook::{}",
                uuid::Uuid::new_v4()
            )
            .into_boxed_str(),
        );

        register_repository_commit_hook_runner(
            handler_key,
            move |_ctx, _record| {
                let create_calls = create_calls.clone();
                async move {
                    create_calls.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                }
            },
            |_ctx, _record| async { Ok(()) },
            |_ctx, _record| async { Ok(()) },
        );

        run_registered_repository_commit_hook(handler_key, "create", Value::Null, Value::Null)
            .await
            .unwrap();

        assert_eq!(calls.as_ref().load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn missing_runner_returns_recoverable_error() {
        let err = run_registered_repository_commit_hook(
            "missing-handler",
            "create",
            Value::Null,
            Value::Null,
        )
        .await
        .expect_err("missing runner should be reported");

        assert!(
            err.to_string().contains("runner not registered"),
            "unexpected error: {err}"
        );
    }

    #[cfg(not(feature = "sqlite"))]
    #[tokio::test]
    async fn after_hook_failure_marking_returns_when_pool_is_unavailable() {
        use diesel_async::AsyncPgConnection;
        use diesel_async::pooled_connection::AsyncDieselConnectionManager;

        let manager = AsyncDieselConnectionManager::<AsyncPgConnection>::new("not-a-postgres-url");
        let pool = Pool::builder(manager)
            .max_size(1)
            .runtime(deadpool::Runtime::Tokio1)
            .build()
            .expect("pool");

        let result = tokio::time::timeout(
            Duration::from_millis(750),
            mark_repository_commit_hook_after_hook_failed(&pool, "hook-id", "owner", "boom"),
        )
        .await;

        assert!(
            result.is_ok(),
            "after-hook failure marking must not block a committed mutation forever when the pool/database is down"
        );
    }

    #[test]
    fn worker_start_is_disabled_without_registered_handlers() {
        assert!(
            !should_start_repository_commit_hook_worker(&[]),
            "unhooked DB apps must not poll the hook queue"
        );
    }

    #[test]
    fn worker_start_is_enabled_with_registered_handlers() {
        assert!(
            should_start_repository_commit_hook_worker(&["handler".to_owned()]),
            "hooked DB apps should poll the hook queue"
        );
    }

    #[cfg(not(feature = "sqlite"))]
    #[test]
    fn dispatcher_kick_state_coalesces_pending_notifications() {
        let state = RepositoryCommitHookKickState::default();

        assert!(state.request_kick(), "first kick should notify the worker");
        assert!(
            !state.request_kick(),
            "repeated kicks while one is pending must coalesce"
        );
        assert!(
            state.take_pending_kick(),
            "worker should observe one wakeup"
        );
        assert!(
            !state.take_pending_kick(),
            "observed wakeup should clear pending state"
        );
        assert!(
            state.request_kick(),
            "a later kick after the worker drains should notify again"
        );
    }

    #[test]
    fn retry_delay_is_exponential() {
        assert_eq!(retry_delay_ms(100, 1), 100);
        assert_eq!(retry_delay_ms(100, 2), 200);
        assert_eq!(retry_delay_ms(100, 3), 400);
    }

    #[test]
    fn idempotent_hook_ids_are_deterministic_and_safely_delimited() {
        let record = serde_json::json!({ "id": 1, "title": "first" }).to_string();
        let first = repository_commit_hook_id(
            Some("v2:request"),
            Some("0"),
            "pkg::module::posts::Post",
            "create",
            &record,
        );
        let second = repository_commit_hook_id(
            Some("v2:request"),
            Some("0"),
            "pkg::module::posts::Post",
            "create",
            &record,
        );
        let other_hook = repository_commit_hook_id(
            Some("v2:request"),
            Some("0"),
            "pkg::module::posts::Post",
            "update",
            &record,
        );

        assert_eq!(first, second);
        assert_ne!(first, other_hook);
        assert!(first.starts_with("idempotent:"));
        assert!(!first.contains("v2:request"));
        assert!(!first.contains("pkg::module::posts::Post"));
    }

    #[test]
    fn non_idempotent_hook_ids_remain_fresh() {
        let record = serde_json::json!({ "id": 1 }).to_string();
        let first = repository_commit_hook_id(None, None, "handler", "create", &record);
        let second = repository_commit_hook_id(None, None, "handler", "create", &record);

        assert_ne!(first, second);
        assert!(uuid::Uuid::parse_str(&first).is_ok());
        assert!(uuid::Uuid::parse_str(&second).is_ok());
    }

    #[test]
    fn hook_insert_sql_ignores_duplicate_idempotent_rows() {
        assert!(
            HOOK_ENQUEUE_INSERT_SQL.contains("ON CONFLICT (id) DO NOTHING"),
            "direct delete commit hooks must dedupe duplicate idempotency rows"
        );
        assert!(
            HOOK_PENDING_INSERT_SQL.contains("ON CONFLICT (id) DO UPDATE")
                && HOOK_PENDING_INSERT_SQL.contains(
                    "WHERE autumn_repository_commit_hooks.status IN ('pending_after_hook', 'after_hook_failed')"
                ),
            "staged create/update commit hooks must dedupe successful duplicate rows while allowing a retry to reclaim unfinalized or failed staged rows"
        );
    }

    #[test]
    fn idempotent_hook_ids_distinguish_records_in_same_request() {
        let first_record = serde_json::json!({ "id": 1, "title": "first" }).to_string();
        let second_record = serde_json::json!({ "id": 2, "title": "second" }).to_string();

        let first = repository_commit_hook_id(
            Some("v2:request"),
            Some("0"),
            "pkg::module::posts::Post",
            "create",
            &first_record,
        );
        let second = repository_commit_hook_id(
            Some("v2:request"),
            Some("1"),
            "pkg::module::posts::Post",
            "create",
            &second_record,
        );

        assert_ne!(
            first, second,
            "one idempotent request can stage multiple committed records for the same hook"
        );
    }

    #[test]
    fn idempotent_hook_ids_distinguish_same_record_sequences_in_same_request() {
        let record = serde_json::json!({ "id": 1, "title": "same" }).to_string();

        let first = repository_commit_hook_id(
            Some("v2:request"),
            Some("0"),
            "pkg::module::posts::Post",
            "update",
            &record,
        );
        let second = repository_commit_hook_id(
            Some("v2:request"),
            Some("1"),
            "pkg::module::posts::Post",
            "update",
            &record,
        );
        let first_again = repository_commit_hook_id(
            Some("v2:request"),
            Some("0"),
            "pkg::module::posts::Post",
            "update",
            &record,
        );

        assert_eq!(
            first, first_again,
            "the same mutation sequence must dedupe across duplicate request attempts"
        );
        assert_ne!(
            first, second,
            "distinct mutations in one request must not collapse just because their final record serializes identically"
        );
    }

    #[test]
    fn missing_idempotent_finalization_fails_closed() {
        let err = missing_repository_commit_hook_finalization_result("idempotent:abc")
            .expect_err("missing idempotent staged rows should fail closed");

        assert!(
            err.to_string()
                .contains("finalization skipped missing staged row"),
            "unexpected error: {err}"
        );
    }

    #[cfg(not(feature = "sqlite"))]
    #[test]
    fn pending_insert_reclaims_only_unfinalized_or_failed_rows() {
        assert!(
            HOOK_PENDING_INSERT_SQL.contains("ON CONFLICT (id) DO UPDATE")
                && HOOK_PENDING_INSERT_SQL.contains("status = 'pending_after_hook'")
                && HOOK_PENDING_INSERT_SQL.contains("context = EXCLUDED.context")
                && HOOK_PENDING_INSERT_SQL.contains("record = EXCLUDED.record")
                && HOOK_PENDING_INSERT_SQL.contains("claimed_by = EXCLUDED.claimed_by")
                && HOOK_PENDING_INSERT_SQL.contains("last_error = NULL"),
            "a retried idempotent mutation must be able to restage durable hooks after an earlier unfinalized or failed regular after-hook"
        );
        assert!(
            HOOK_PENDING_INSERT_SQL.contains(
                "WHERE autumn_repository_commit_hooks.status IN ('pending_after_hook', 'after_hook_failed')"
            ),
            "restaging must reclaim unfinalized pending rows but not replace already finalized, enqueued, running, completed, or worker-failed rows"
        );
    }

    #[cfg(feature = "sqlite")]
    #[test]
    fn pending_insert_reclaims_only_unfinalized_or_failed_rows_sqlite() {
        assert!(
            HOOK_PENDING_INSERT_SQL.contains("ON CONFLICT (id) DO UPDATE")
                && HOOK_PENDING_INSERT_SQL.contains("status = 'pending_after_hook'")
                && HOOK_PENDING_INSERT_SQL.contains("context = excluded.context")
                && HOOK_PENDING_INSERT_SQL.contains("record = excluded.record")
                && HOOK_PENDING_INSERT_SQL.contains("claimed_by = excluded.claimed_by")
                && HOOK_PENDING_INSERT_SQL.contains("last_error = NULL"),
            "a retried idempotent mutation must be able to restage durable hooks after an earlier unfinalized or failed regular after-hook"
        );
        assert!(
            HOOK_PENDING_INSERT_SQL.contains(
                "WHERE autumn_repository_commit_hooks.status IN ('pending_after_hook', 'after_hook_failed')"
            ),
            "restaging must reclaim unfinalized pending rows but not replace already finalized, enqueued, running, completed, or worker-failed rows"
        );
    }

    #[test]
    fn missing_non_idempotent_finalization_remains_an_error() {
        let err = missing_repository_commit_hook_finalization_result("random-id")
            .expect_err("missing non-idempotent staged rows should still be reported");

        assert!(
            err.to_string()
                .contains("finalization skipped missing staged row"),
            "unexpected error: {err}"
        );
    }

    #[test]
    fn claim_heartbeat_runs_before_stale_recovery() {
        assert!(
            HOOK_CLAIM_HEARTBEAT_INTERVAL < HOOK_STALE_CLAIM_AFTER,
            "heartbeat interval must be shorter than stale recovery threshold"
        );
        assert!(
            HOOK_PENDING_FINALIZER_HEARTBEAT_INTERVAL < HOOK_STALE_CLAIM_AFTER,
            "pending finalizer heartbeat interval must be shorter than stale recovery threshold"
        );
    }

    #[cfg(not(feature = "sqlite"))]
    #[test]
    fn success_ack_clears_retained_payloads() {
        assert!(
            HOOK_ACK_SUCCESS_SQL.contains("context = '{}'::JSONB"),
            "success ack must clear serialized context payload"
        );
        assert!(
            HOOK_ACK_SUCCESS_SQL.contains("record = '{}'::JSONB"),
            "success ack must clear serialized record payload"
        );
    }

    #[cfg(feature = "sqlite")]
    #[test]
    fn success_ack_clears_retained_payloads_sqlite() {
        assert!(
            HOOK_ACK_SUCCESS_SQL.contains("context = '{}'"),
            "success ack must clear serialized context payload"
        );
        assert!(
            HOOK_ACK_SUCCESS_SQL.contains("record = '{}'"),
            "success ack must clear serialized record payload"
        );
        assert!(
            !HOOK_ACK_SUCCESS_SQL.contains("::JSONB") && !HOOK_ACK_SUCCESS_SQL.contains("NOW()"),
            "the SQLite ack must not carry Postgres-only JSONB casts or NOW()"
        );
    }

    #[test]
    fn stale_running_recovery_counts_against_max_attempts() {
        assert!(
            HOOK_RECOVER_STALE_RUNNING_SQL.contains("attempt < max_attempts"),
            "stale running recovery must branch on retry exhaustion"
        );
        assert!(
            HOOK_RECOVER_STALE_RUNNING_SQL.contains("attempt = CASE"),
            "stale running recovery must not requeue without updating attempt accounting"
        );
        assert!(
            HOOK_RECOVER_STALE_RUNNING_SQL.contains("attempt + 1"),
            "stale running recovery must consume the abandoned attempt"
        );
        assert!(
            HOOK_RECOVER_STALE_RUNNING_SQL.contains("ELSE 'failed'"),
            "stale running recovery must dead-letter rows already at max_attempts"
        );
        assert!(
            !HOOK_RECOVER_STALE_RUNNING_SQL.contains("SET status = 'enqueued'"),
            "stale running recovery must not unconditionally requeue exhausted rows"
        );
    }

    #[cfg(not(feature = "sqlite"))]
    #[test]
    fn staged_hooks_are_not_dispatchable_until_finalized_after_regular_hooks() {
        assert!(
            HOOK_PENDING_INSERT_SQL.contains("status, attempt")
                && HOOK_PENDING_INSERT_SQL.contains("'pending_after_hook'"),
            "create/update hooks must first be staged in a non-dispatchable lifecycle state"
        );
        assert!(
            HOOK_PENDING_INSERT_SQL.contains("claimed_by, claimed_at"),
            "staged rows must carry a finalizer lease so recovery can distinguish live after hooks from abandoned rows"
        );
        assert!(
            HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL.contains("status = 'after_hook_succeeded'")
                && HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL.contains("context = $1::JSONB")
                && HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL.contains("record = $2::JSONB"),
            "regular after-hook success must durably persist finalized hook payload before enqueue"
        );
        assert!(
            HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL
                .contains("WHERE id = $3 AND claimed_by = $4 AND status = 'pending_after_hook'"),
            "success marking must only advance the staged row it owns"
        );
        assert!(
            HOOK_FINALIZE_AFTER_HOOK_SQL.contains("status = 'enqueued'")
                && HOOK_FINALIZE_AFTER_HOOK_SQL.contains(
                    "WHERE id = $1 AND claimed_by = $2 AND status = 'after_hook_succeeded'"
                ),
            "after-hook finalization must only enqueue rows with a durable regular-hook success marker"
        );
        assert!(
            HOOK_AFTER_HOOK_FAILED_SQL.contains("status = 'after_hook_failed'")
                && HOOK_AFTER_HOOK_FAILED_SQL.contains("context = '{}'::JSONB")
                && HOOK_AFTER_HOOK_FAILED_SQL.contains("record = '{}'::JSONB")
                && HOOK_AFTER_HOOK_FAILED_SQL.contains(
                    "WHERE id = $2 AND claimed_by = $3 AND status = 'pending_after_hook'"
                ),
            "failed regular after hooks must mark only the owner-scoped staged row terminal and non-dispatchable"
        );
        assert!(
            !HOOK_AFTER_HOOK_FAILED_SQL.contains("claimed_by IS NULL")
                && !HOOK_AFTER_HOOK_FAILED_SQL.contains("'enqueued'"),
            "duplicate idempotent retries must not dead-letter already finalized hook rows"
        );
        assert!(
            HOOK_EXTEND_PENDING_FINALIZER_SQL.contains("claimed_at = NOW()")
                && HOOK_EXTEND_PENDING_FINALIZER_SQL.contains("status = 'pending_after_hook'"),
            "long-running regular after hooks must heartbeat their staged-row finalizer lease"
        );
        assert!(
            HOOK_RECOVER_STALE_PENDING_SQL
                .contains("status IN ('pending_after_hook', 'after_hook_succeeded')")
                && HOOK_RECOVER_STALE_PENDING_SQL
                    .contains("WHEN status = 'after_hook_succeeded' THEN 'enqueued'")
                && HOOK_RECOVER_STALE_PENDING_SQL.contains("ELSE 'after_hook_failed'"),
            "stale recovery must enqueue only rows with a durable regular-hook success marker"
        );
        assert!(
            HOOK_RECOVER_STALE_PENDING_SQL
                .contains("WHEN status = 'pending_after_hook' THEN '{}'::JSONB"),
            "ambiguous stale pending rows must be failed closed without retaining payloads"
        );
        assert!(
            HOOK_RECOVER_STALE_PENDING_SQL
                .contains("WHEN status = 'pending_after_hook' THEN NOW()"),
            "ambiguous stale pending rows must be marked terminal when failed closed"
        );
    }

    // `SQLite` fork of the staged-lifecycle invariants: identical status-machine
    // wording, but `CURRENT_TIMESTAMP`/`excluded`/bare-`'{}'` in place of the
    // Postgres `NOW()`/`EXCLUDED`/`::JSONB`.
    #[cfg(feature = "sqlite")]
    #[test]
    fn staged_hooks_are_not_dispatchable_until_finalized_after_regular_hooks_sqlite() {
        assert!(
            HOOK_PENDING_INSERT_SQL.contains("'pending_after_hook'")
                && HOOK_PENDING_INSERT_SQL.contains("claimed_by, claimed_at"),
            "create/update hooks must first be staged in a non-dispatchable leased state"
        );
        assert!(
            HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL.contains("status = 'after_hook_succeeded'")
                && HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL.contains("context = ?")
                && HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL.contains("record = ?")
                && HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL
                    .contains("WHERE id = ? AND claimed_by = ? AND status = 'pending_after_hook'"),
            "regular after-hook success must durably persist finalized hook payload before enqueue"
        );
        assert!(
            HOOK_FINALIZE_AFTER_HOOK_SQL.contains("status = 'enqueued'")
                && HOOK_FINALIZE_AFTER_HOOK_SQL.contains(
                    "WHERE id = ? AND claimed_by = ? AND status = 'after_hook_succeeded'"
                ),
            "after-hook finalization must only enqueue rows with a durable regular-hook success marker"
        );
        assert!(
            HOOK_AFTER_HOOK_FAILED_SQL.contains("status = 'after_hook_failed'")
                && HOOK_AFTER_HOOK_FAILED_SQL.contains("context = '{}'")
                && HOOK_AFTER_HOOK_FAILED_SQL.contains("record = '{}'")
                && HOOK_AFTER_HOOK_FAILED_SQL
                    .contains("WHERE id = ? AND claimed_by = ? AND status = 'pending_after_hook'"),
            "failed regular after hooks must mark only the owner-scoped staged row terminal and non-dispatchable"
        );
        assert!(
            HOOK_RECOVER_STALE_PENDING_SQL
                .contains("status IN ('pending_after_hook', 'after_hook_succeeded')")
                && HOOK_RECOVER_STALE_PENDING_SQL
                    .contains("WHEN status = 'after_hook_succeeded' THEN 'enqueued'")
                && HOOK_RECOVER_STALE_PENDING_SQL.contains("ELSE 'after_hook_failed'")
                && HOOK_RECOVER_STALE_PENDING_SQL
                    .contains("WHEN status = 'pending_after_hook' THEN '{}'"),
            "stale recovery must enqueue only durably-succeeded rows and fail ambiguous ones closed"
        );
        assert!(
            !HOOK_PENDING_INSERT_SQL.contains("::JSONB")
                && !HOOK_AFTER_HOOK_FAILED_SQL.contains("::JSONB")
                && !HOOK_MARK_AFTER_HOOK_SUCCEEDED_SQL.contains("::JSONB"),
            "the SQLite staged-lifecycle SQL must carry no Postgres-only JSONB casts"
        );
    }

    #[test]
    fn pending_heartbeat_guard_cancels_on_drop() {
        let guard = RepositoryCommitHookPendingHeartbeat::new(CancellationToken::new());
        let child = guard.shutdown.child_token();

        drop(guard);

        assert!(
            child.is_cancelled(),
            "dropping the pending heartbeat guard must cancel recovery-blocking heartbeats"
        );
    }

    #[cfg(not(feature = "sqlite"))]
    #[test]
    fn claim_heartbeat_is_owner_scoped() {
        assert!(
            HOOK_EXTEND_CLAIM_SQL.contains("claimed_at = NOW()"),
            "heartbeat must extend the stale-recovery lease"
        );
        assert!(
            HOOK_EXTEND_CLAIM_SQL.contains("claimed_by = $2"),
            "heartbeat must only extend this worker's claim"
        );
        assert!(
            HOOK_EXTEND_CLAIM_SQL.contains("status = 'running'"),
            "heartbeat must only touch running rows"
        );
    }

    #[cfg(feature = "sqlite")]
    #[test]
    fn claim_heartbeat_is_owner_scoped_sqlite() {
        assert!(
            HOOK_EXTEND_CLAIM_SQL.contains("claimed_at = CURRENT_TIMESTAMP"),
            "heartbeat must extend the stale-recovery lease"
        );
        assert!(
            HOOK_EXTEND_CLAIM_SQL.contains("claimed_by = ?"),
            "heartbeat must only extend this worker's claim"
        );
        assert!(
            HOOK_EXTEND_CLAIM_SQL.contains("status = 'running'"),
            "heartbeat must only touch running rows"
        );
    }

    #[test]
    fn missing_hook_table_error_is_detected_for_quiet_polling() {
        let error = diesel::result::Error::QueryBuilderError(
            std::io::Error::other("relation \"autumn_repository_commit_hooks\" does not exist")
                .into(),
        );

        assert!(is_missing_hook_table_error(&error));

        // SQLite reports the missing queue table differently.
        let sqlite_error = diesel::result::Error::QueryBuilderError(
            std::io::Error::other("no such table: autumn_repository_commit_hooks").into(),
        );

        assert!(is_missing_hook_table_error(&sqlite_error));

        // An unrelated error must not be misclassified as a missing table.
        let unrelated = diesel::result::Error::QueryBuilderError(
            std::io::Error::other("connection reset by peer").into(),
        );

        assert!(!is_missing_hook_table_error(&unrelated));
    }
}