khive-db 0.10.0

SQLite storage backend: entities, edges, notes, events, FTS5, sqlite-vec vectors.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
use super::*;
use crate::pool::PoolConfig;
use crate::writer_task::WriterTaskHandle;
use rusqlite::hooks::{AuthAction, Authorization};
use serial_test::serial;
use std::sync::atomic::{AtomicBool, AtomicUsize};
use tracing::field::{Field, Visit};

static LIVE_CHECKPOINT_BUSY_HANDLER_ENTERED: AtomicBool = AtomicBool::new(false);

fn hold_checkpoint_lock_for_live_busy_probe(_attempt: i32) -> bool {
    LIVE_CHECKPOINT_BUSY_HANDLER_ENTERED.store(true, Ordering::SeqCst);
    std::thread::sleep(Duration::from_millis(250));
    false
}

fn pr3409_active_interval(pool: &ConnectionPool) -> u64 {
    let key = checkpoint_db_key(pool);
    checkpoint_runs()
        .lock()
        .unwrap()
        .get(&key)
        .expect("an owner is active")
        .checkpoint_interval_ms
}

include!("checkpoint/environment_tests.rs");

include!("checkpoint_owner_interval_tests.rs");

#[test]
fn verify_3409_backward_wall_clock_step_bypasses_busy_gap_budget() {
    let mut entry = None;
    let start = Instant::now();
    advance_checkpoint_run_at(&mut entry, Some((0, 20, 7)), 100_000, start, 10);
    advance_checkpoint_run_at(
        &mut entry,
        Some((1, -1, -1)),
        100_005,
        start + Duration::from_millis(5),
        10,
    );
    advance_checkpoint_run_at(
        &mut entry,
        Some((0, 21, 7)),
        90_000,
        start + Duration::from_millis(50),
        10,
    );
    let observed = entry.expect("an entry exists after the third sample");
    assert_eq!(
        observed.run.first_observed_at_unix_ms, 90_000,
        "a backward wall-clock step must not let the busy-gap budget check \
             silently treat a large elapsed gap as zero and continue the stale run"
    );
}

#[test]
fn checkpoint_run_snapshot_uses_monotonic_age_with_future_epoch() {
    let dir = tempfile::tempdir().unwrap();
    let pool = file_pool(&dir.path().join("monotonic_pin_age.db"));
    let _guard = CheckpointRunTaskGuard::start(&pool, Duration::from_millis(10));
    let key = checkpoint_db_key(&pool);
    let observed_at = Instant::now() - Duration::from_secs(2);
    {
        let mut runs = checkpoint_runs().lock().unwrap();
        let state = runs.get_mut(&key).expect("active checkpoint task");
        advance_checkpoint_run_at(
            &mut state.entry,
            Some((0, 20, 7)),
            u64::MAX,
            observed_at,
            10,
        );
    }
    let (status, age) = checkpoint_run_snapshot(&pool);
    assert_eq!(
        status,
        CheckpointRunStatus::Observed(CheckpointRun {
            frame: 7,
            first_observed_at_unix_ms: u64::MAX,
        })
    );
    assert!(age.expect("observed run has an age") >= Duration::from_secs(2));
}

#[test]
fn checkpoint_run_guard_captures_the_configured_interval_for_busy_spans() {
    let dir = tempfile::tempdir().unwrap();
    let pool = file_pool(&dir.path().join("busy_span_interval.db"));
    let key = checkpoint_db_key(&pool);
    let guard = CheckpointRunTaskGuard::start(&pool, Duration::from_millis(10));
    let interval_ms = checkpoint_runs()
        .lock()
        .unwrap()
        .get(&key)
        .expect("active task has run state")
        .checkpoint_interval_ms;
    assert_eq!(interval_ms, 10);
    drop(guard);
    assert!(!checkpoint_runs().lock().unwrap().contains_key(&key));
}

#[test]
fn checkpoint_run_ends_on_full_backfill_before_a_later_pin() {
    let mut entry = None;
    advance_checkpoint_run(&mut entry, Some((0, 10, 7)), 100, 500);
    advance_checkpoint_run(&mut entry, Some((0, 12, 7)), 200, 500);
    assert_eq!(
        entry.map(|value| value.run),
        Some(CheckpointRun {
            frame: 7,
            first_observed_at_unix_ms: 100,
        })
    );

    advance_checkpoint_run(&mut entry, Some((0, 12, 12)), 300, 500);
    assert_eq!(entry, None, "a full backfill ends the prior run");

    advance_checkpoint_run(&mut entry, Some((0, 18, 15)), 400, 500);
    assert_eq!(
        entry.map(|value| value.run),
        Some(CheckpointRun {
            frame: 15,
            first_observed_at_unix_ms: 400,
        })
    );
}

#[test]
fn checkpoint_run_restarts_when_log_frame_count_decreases() {
    let mut entry = None;
    advance_checkpoint_run(&mut entry, Some((0, 20, 7)), 100, 500);
    advance_checkpoint_run(&mut entry, Some((0, 22, 7)), 125, 500);
    advance_checkpoint_run(&mut entry, Some((1, -1, -1)), 150, 500);
    advance_checkpoint_run(&mut entry, Some((0, 21, 7)), 200, 500);

    assert_eq!(
        entry.map(|value| value.run),
        Some(CheckpointRun {
            frame: 7,
            first_observed_at_unix_ms: 200,
        }),
        "a lower log count breaks the sequence and starts a new run"
    );
}

#[test]
fn checkpoint_run_ends_on_error_negative_or_unpinned_results() {
    for result in [Some((0, -1, -1)), Some((0, 20, 20)), None] {
        let mut entry = None;
        advance_checkpoint_run(&mut entry, Some((0, 20, 7)), 100, 500);
        advance_checkpoint_run(&mut entry, result, 200, 500);
        assert_eq!(entry, None, "result {result:?} must end the run");
    }
}

#[test]
fn checkpoint_run_keeps_held_pin_across_busy_rows_without_using_their_columns() {
    let mut entry = None;
    advance_checkpoint_run(&mut entry, Some((0, 20, 7)), 100, 500);
    let first = entry.expect("qualifying row begins a run");
    advance_checkpoint_run(&mut entry, Some((1, -1, -1)), 500, 500);
    advance_checkpoint_run(&mut entry, Some((1, 100, 99)), 700, 500);
    assert_eq!(entry.expect("busy is neutral").run, first.run);

    advance_checkpoint_run(&mut entry, Some((0, 21, 7)), 900, 500);
    let resumed = entry.expect("the next informative row continues the run");
    assert_eq!(resumed.run, first.run);
    assert!(!resumed.busy_since_last_informative);
    assert_eq!(resumed.last_log_frames, 21);
}

#[test]
fn checkpoint_run_survives_frequent_busy_results_for_a_one_second_pin() {
    let mut entry = None;
    advance_checkpoint_run(&mut entry, Some((0, 20, 7)), 100, 20);
    for step in 1..=70 {
        let at = 100 + step * 16;
        advance_checkpoint_run(&mut entry, Some((1, -1, -1)), at - 8, 20);
        advance_checkpoint_run(&mut entry, Some((0, 20 + step as i64, 7)), at, 20);
    }
    let run = entry
        .expect("the held pin remains observable through busy rows")
        .run;
    assert_eq!(run.frame, 7);
    assert_eq!(run.first_observed_at_unix_ms, 100);
}

#[test]
fn checkpoint_run_restarts_only_after_busy_span_exceeds_two_configured_intervals() {
    let mut entry = None;
    advance_checkpoint_run(&mut entry, Some((0, 20, 7)), 100, 10);
    advance_checkpoint_run(&mut entry, Some((1, -1, -1)), 105, 10);
    advance_checkpoint_run(&mut entry, Some((0, 21, 7)), 120, 10);
    assert_eq!(
        entry
            .expect("busy span at the bound preserves the run")
            .run
            .first_observed_at_unix_ms,
        100
    );

    advance_checkpoint_run(&mut entry, Some((1, -1, -1)), 125, 10);
    advance_checkpoint_run(&mut entry, Some((0, 22, 7)), 141, 10);
    assert_eq!(
        entry
            .expect("busy span past the bound starts a new run")
            .run
            .first_observed_at_unix_ms,
        141
    );
}

#[test]
fn checkpoint_run_long_nonbusy_interval_does_not_trigger_busy_span_bound() {
    let mut entry = None;
    advance_checkpoint_run(&mut entry, Some((0, 20, 7)), 100, 500);
    advance_checkpoint_run(&mut entry, Some((0, 21, 7)), 1_101, 500);
    assert_eq!(
        entry
            .expect("the busy bound only applies after a busy row")
            .run
            .first_observed_at_unix_ms,
        100
    );
}

#[test]
#[serial(
    checkpoint_skip_metrics,
    khive_walpin_census_budget_env,
    checkpoint_live_busy
)]
fn live_concurrent_busy_checkpoints_preserve_a_reader_pin_run() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_CENSUS_BUDGET_MS", "10");
    }) {
        return;
    }

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("live_concurrent_busy_pin.db");
    let pool = file_pool(&path);
    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE t (value INTEGER); INSERT INTO t VALUES (0)")
            .unwrap();
        assert_eq!(query_truncate_observation(writer.conn()).unwrap().busy, 0);
        writer
            .conn()
            .execute("INSERT INTO t VALUES (1)", [])
            .unwrap();
    }

    // The reader sees a WAL frame before another writer advances the log.
    // Holding a snapshot of an already-backfilled database would not pin it.
    let reader = rusqlite::Connection::open(&path).unwrap();
    reader.execute_batch("BEGIN DEFERRED").unwrap();
    let visible_rows: i64 = reader
        .query_row("SELECT COUNT(*) FROM t", [], |row| row.get(0))
        .unwrap();
    assert_eq!(visible_rows, 2);
    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute("INSERT INTO t VALUES (2)", [])
            .unwrap();
    }

    // Use real SQLite PASSIVE rows with the same tracker and diagnostic
    // path as the periodic checkpoint task. The 500 ms configured interval
    // gives a one-second neutral-busy bound; observations below are faster.
    let _run_owner = CheckpointRunTaskGuard::start(&pool, Duration::from_millis(500));
    let observer = rusqlite::Connection::open(&path).unwrap();
    let first = query_checkpoint_observation(&observer).unwrap();
    assert_eq!(first.busy, 0);
    assert!(first.checkpointed_frames > 0);
    assert!(first.log_frames > first.checkpointed_frames);
    let CheckpointRunStatus::Observed(first_run) = record_checkpoint_run_result(
        &pool,
        Some((first.busy, first.log_frames, first.checkpointed_frames)),
    ) else {
        panic!("the held reader must start a checkpoint run");
    };

    // SQLite holds the CKPT lock while a TRUNCATE waits on this reader's
    // read mark. Its bounded busy handler signals when PASSIVE probes can
    // collide with that lock and return actual busy rows (often -1/-1).
    LIVE_CHECKPOINT_BUSY_HANDLER_ENTERED.store(false, Ordering::SeqCst);
    let contender_path = path.clone();
    let contender = std::thread::spawn(move || {
        let conn = rusqlite::Connection::open(contender_path).unwrap();
        conn.busy_handler(Some(hold_checkpoint_lock_for_live_busy_probe))
            .unwrap();
        query_truncate_observation(&conn).unwrap()
    });
    let lock_deadline = Instant::now() + Duration::from_secs(2);
    while !LIVE_CHECKPOINT_BUSY_HANDLER_ENTERED.load(Ordering::SeqCst)
        && Instant::now() < lock_deadline
    {
        std::thread::sleep(Duration::from_millis(1));
    }
    assert!(
        LIVE_CHECKPOINT_BUSY_HANDLER_ENTERED.load(Ordering::SeqCst),
        "TRUNCATE must wait on the held reader while holding the CKPT lock"
    );

    let mut busy_rows = 0;
    for _ in 0..10 {
        let sample = query_checkpoint_observation(&observer).unwrap();
        let status = record_checkpoint_run_result(
            &pool,
            Some((sample.busy, sample.log_frames, sample.checkpointed_frames)),
        );
        if sample.busy != 0 {
            busy_rows += 1;
            assert_eq!(
                status,
                CheckpointRunStatus::Observed(first_run),
                "a real busy PASSIVE row must leave the first run intact"
            );
        }
        std::thread::sleep(Duration::from_millis(10));
    }
    let contender_result = contender.join().expect("TRUNCATE contender must finish");
    assert_ne!(contender_result.busy, 0, "the held reader blocks TRUNCATE");
    assert!(
        busy_rows > 0,
        "the test must observe real concurrent busy rows"
    );

    let resumed = query_checkpoint_observation(&observer).unwrap();
    assert_eq!(resumed.busy, 0);
    assert_eq!(resumed.checkpointed_frames, first_run.frame);
    assert!(resumed.log_frames >= first.log_frames);
    assert_eq!(
        record_checkpoint_run_result(
            &pool,
            Some((
                resumed.busy,
                resumed.log_frames,
                resumed.checkpointed_frames
            )),
        ),
        CheckpointRunStatus::Observed(first_run),
        "the first informative row after a bounded busy span resumes the run"
    );

    let aging_started = Instant::now();
    while aging_started.elapsed() < Duration::from_millis(1_050) {
        {
            let writer = pool.writer().unwrap();
            writer
                .conn()
                .execute("INSERT INTO t VALUES (3)", [])
                .unwrap();
        }
        let sample = query_checkpoint_observation(&observer).unwrap();
        assert_eq!(sample.busy, 0);
        assert_eq!(sample.checkpointed_frames, first_run.frame);
        assert!(sample.log_frames > sample.checkpointed_frames);
        assert_eq!(
            record_checkpoint_run_result(
                &pool,
                Some((sample.busy, sample.log_frames, sample.checkpointed_frames)),
            ),
            CheckpointRunStatus::Observed(first_run)
        );
        std::thread::sleep(Duration::from_millis(10));
    }

    let aged = crate::diagnostics::collect(
        &pool,
        crate::diagnostics::BuildIdentity::from_env("test", None),
        Duration::from_secs(30),
    );
    let probe = aged
        .checkpoint_probe
        .as_ref()
        .expect("diagnostic probe row");
    assert_eq!(probe.busy, 0);
    assert_eq!(
        aged.checkpoint_pin.oldest_pinned_frame,
        Some(first_run.frame)
    );
    assert_eq!(aged.checkpoint_pin.oldest_pinned_frame_run, Some(first_run));
    assert_eq!(
        aged.checkpoint_pin.pin_depth,
        Some(probe.log_frames - first_run.frame)
    );

    reader.execute_batch("ROLLBACK").unwrap();
    let drained = crate::diagnostics::collect(
        &pool,
        crate::diagnostics::BuildIdentity::from_env("test", None),
        Duration::from_secs(30),
    );
    assert_eq!(drained.checkpoint_pin.oldest_pinned_frame, None);
    assert_eq!(drained.checkpoint_pin.pin_depth, None);
}

/// The bundled SQLite reuses the slot at the greatest read mark when all
/// frame-carrying slots are occupied. An upgrade that changes this must
/// fail here before the oldest-frame report relies on that behavior.
#[test]
fn wal_read_mark_slots_keep_the_fourth_frame_for_a_later_reader() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("read_mark_slots.db");
    let pool = file_pool(&path);
    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE t (value INTEGER)")
            .unwrap();
        query_truncate_observation(writer.conn()).unwrap();
    }

    let observer = rusqlite::Connection::open(&path).unwrap();
    let mut readers = Vec::new();
    let mut previous_log = 0;
    for value in 0..4 {
        {
            let writer = pool.writer().unwrap();
            writer
                .conn()
                .execute("INSERT INTO t VALUES (?1)", [value])
                .unwrap();
        }
        // Take the snapshot before the checkpoint. If PASSIVE first copies
        // every frame, this reader can use slot 0 (the main database),
        // allowing the next write to restart the WAL at frame 1.
        let reader = rusqlite::Connection::open(&path).unwrap();
        reader.execute_batch("BEGIN DEFERRED").unwrap();
        let rows: i64 = reader
            .query_row("SELECT COUNT(*) FROM t", [], |row| row.get::<_, i64>(0))
            .unwrap();
        assert_eq!(rows, i64::from(value) + 1);
        readers.push(reader);

        let observation = query_checkpoint_observation(&observer).unwrap();
        assert!(observation.log_frames > previous_log);
        previous_log = observation.log_frames;
    }
    let pinned_frame = previous_log;

    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute("INSERT INTO t VALUES (4)", [])
            .unwrap();
    }
    let fifth_reader = rusqlite::Connection::open(&path).unwrap();
    fifth_reader.execute_batch("BEGIN DEFERRED").unwrap();
    let rows: i64 = fifth_reader
        .query_row("SELECT COUNT(*) FROM t", [], |row| row.get::<_, i64>(0))
        .unwrap();
    assert_eq!(rows, 5, "the fifth snapshot sees the newly committed frame");
    drop(readers);

    let observation = query_checkpoint_observation(&observer).unwrap();
    assert_eq!(observation.busy, 0);
    assert_eq!(observation.checkpointed_frames, pinned_frame);
    assert!(observation.log_frames > pinned_frame);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[serial(checkpoint_skip_metrics, khive_walpin_census_budget_env)]
async fn db_diagnostics_reports_a_reader_pin_after_one_second_and_clears_after_backfill() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_CENSUS_BUDGET_MS", "10");
    }) {
        return;
    }

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("diagnostic_pin_run.db");
    let pool = file_pool(&path);
    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE t (value INTEGER); INSERT INTO t VALUES (0)")
            .unwrap();
    }

    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let task_pool = Arc::clone(&pool);
    let task = tokio::spawn(run_checkpoint_task(
        task_pool,
        CheckpointConfig {
            interval: Duration::from_millis(2_500),
            ..Default::default()
        },
        None,
        shutdown_rx,
        true,
    ));
    tokio::time::timeout(Duration::from_secs(4), async {
        while routine_wal_observation(&pool).is_none() {
            tokio::task::yield_now().await;
        }
    })
    .await
    .expect("the checkpoint task must record its initial sample");

    // A reader opened immediately after a full checkpoint may use WAL
    // slot 0 and cannot pin the next frame. Give it an uncheckpointed
    // frame to read before the writer appends another one.
    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute("INSERT INTO t VALUES (1)", [])
            .unwrap();
    }
    let mut reader = ReaderProcess::spawn(&path);
    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute("INSERT INTO t VALUES (2)", [])
            .unwrap();
    }

    let first = crate::diagnostics::collect(
        &pool,
        crate::diagnostics::BuildIdentity::from_env("test", None),
        Duration::from_secs(30),
    );
    let first_probe = first.checkpoint_probe.as_ref().expect("probe row");
    assert!(first_probe.checkpointed_frames > 0);
    assert_eq!(
        first.checkpoint_pin.backfill_ceiling,
        Some(first_probe.checkpointed_frames)
    );
    assert!(first_probe.log_frames > first_probe.checkpointed_frames);
    assert_eq!(first.checkpoint_pin.oldest_pinned_frame, None);
    assert_eq!(first.checkpoint_pin.pin_depth, None);
    assert!(first
        .checkpoint_pin
        .oldest_pinned_frame_unavailable_reason
        .as_deref()
        .is_some_and(|reason| reason.contains("less than one second")));

    tokio::time::sleep(Duration::from_millis(1_010)).await;
    let aged = crate::diagnostics::collect(
        &pool,
        crate::diagnostics::BuildIdentity::from_env("test", None),
        Duration::from_secs(30),
    );
    let aged_probe = aged.checkpoint_probe.as_ref().expect("probe row");
    assert_eq!(
        aged.checkpoint_pin.backfill_ceiling,
        Some(aged_probe.checkpointed_frames)
    );
    assert_eq!(
        aged.checkpoint_pin.oldest_pinned_frame,
        Some(aged_probe.checkpointed_frames)
    );
    assert_eq!(
        aged.checkpoint_pin.pin_depth,
        Some(aged_probe.log_frames - aged_probe.checkpointed_frames)
    );
    let first_run = aged
        .checkpoint_pin
        .oldest_pinned_frame_run
        .expect("matching run accompanies the reported frame");
    assert_eq!(first_run.frame, aged_probe.checkpointed_frames);

    reader.release();
    let previous_tick = routine_wal_observation(&pool)
        .expect("the checkpoint task has an initial sample")
        .observed_at_unix_ms;
    tokio::time::timeout(Duration::from_secs(4), async {
        loop {
            if routine_wal_observation(&pool)
                .is_some_and(|sample| sample.observed_at_unix_ms > previous_tick)
            {
                break;
            }
            tokio::task::yield_now().await;
        }
    })
    .await
    .expect("a later checkpoint tick must fully backfill after the reader ends");
    let drained = crate::diagnostics::collect(
        &pool,
        crate::diagnostics::BuildIdentity::from_env("test", None),
        Duration::from_secs(30),
    );
    assert_eq!(drained.checkpoint_pin.backfill_ceiling, None);
    assert!(drained
        .checkpoint_pin
        .backfill_ceiling_unavailable_reason
        .is_some());
    assert_eq!(drained.checkpoint_pin.oldest_pinned_frame, None);
    assert_eq!(drained.checkpoint_pin.pin_depth, None);
    assert_eq!(drained.checkpoint_pin.oldest_pinned_frame_run, None);

    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute_batch("INSERT INTO t VALUES (3); INSERT INTO t VALUES (4)")
            .unwrap();
    }
    let second_reader = rusqlite::Connection::open(&path).unwrap();
    second_reader.execute_batch("BEGIN DEFERRED").unwrap();
    second_reader
        .query_row("SELECT COUNT(*) FROM t", [], |row| row.get::<_, i64>(0))
        .unwrap();
    let second_pin_started_at_unix_ms = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_millis() as u64;
    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute("INSERT INTO t VALUES (5)", [])
            .unwrap();
    }
    let second_first = crate::diagnostics::collect(
        &pool,
        crate::diagnostics::BuildIdentity::from_env("test", None),
        Duration::from_secs(30),
    );
    let second_first_probe = second_first.checkpoint_probe.as_ref().expect("probe row");
    assert!(second_first_probe.checkpointed_frames > 0);
    assert!(second_first_probe.log_frames > second_first_probe.checkpointed_frames);
    assert_eq!(second_first.checkpoint_pin.oldest_pinned_frame, None);
    tokio::time::sleep(Duration::from_millis(1_010)).await;
    let second_aged = crate::diagnostics::collect(
        &pool,
        crate::diagnostics::BuildIdentity::from_env("test", None),
        Duration::from_secs(30),
    );
    let second_aged_probe = second_aged.checkpoint_probe.as_ref().expect("probe row");
    let second_run = second_aged
        .checkpoint_pin
        .oldest_pinned_frame_run
        .expect("the second pin must begin a distinct observed run");
    assert_eq!(
        second_aged.checkpoint_pin.oldest_pinned_frame,
        Some(second_aged_probe.checkpointed_frames)
    );
    // A full backfill ended the earlier run above. WAL frame numbers may
    // restart, so distinctness is established by the new observation time.
    assert!(second_run.first_observed_at_unix_ms > first_run.first_observed_at_unix_ms);
    assert!(
        second_run.first_observed_at_unix_ms >= second_pin_started_at_unix_ms,
        "the new run begins after the second reader starts"
    );
    second_reader.execute_batch("ROLLBACK").unwrap();

    shutdown_tx.send(()).unwrap();
    tokio::time::timeout(Duration::from_secs(2), task)
        .await
        .expect("checkpoint task shutdown must finish")
        .expect("checkpoint task must not panic");
}

include!("checkpoint/churn_tests.rs");

#[test]
fn db_diagnostics_reports_short_reader_backfill_ceiling_without_a_pin() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("diagnostic_short_reader_gap.db");
    let pool = file_pool(&path);
    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE t (value INTEGER); INSERT INTO t VALUES (1)")
            .unwrap();
    }

    // The reader snapshots an uncheckpointed frame. A later commit must
    // remain beyond that snapshot when the diagnostic PASSIVE probe runs.
    // No checkpoint task races this fixture or can age the gap into a pin.
    let reader = rusqlite::Connection::open(&path).unwrap();
    reader.execute_batch("BEGIN DEFERRED").unwrap();
    let visible_rows: i64 = reader
        .query_row("SELECT COUNT(*) FROM t", [], |row| row.get(0))
        .unwrap();
    assert_eq!(visible_rows, 1);
    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute("INSERT INTO t VALUES (2)", [])
            .unwrap();
    }

    let report = crate::diagnostics::collect(
        &pool,
        crate::diagnostics::BuildIdentity::from_env("test", None),
        Duration::from_secs(30),
    );
    let probe = report.checkpoint_probe.as_ref().expect("probe row");
    assert_eq!(probe.busy, 0);
    assert!(probe.checkpointed_frames > 0);
    assert!(probe.log_frames > probe.checkpointed_frames);
    assert_eq!(
        report.checkpoint_pin.backfill_ceiling,
        Some(probe.checkpointed_frames)
    );
    assert_eq!(report.checkpoint_pin.oldest_pinned_frame, None);
    assert_eq!(report.checkpoint_pin.pin_depth, None);

    reader.execute_batch("ROLLBACK").unwrap();
    let drained = crate::diagnostics::collect(
        &pool,
        crate::diagnostics::BuildIdentity::from_env("test", None),
        Duration::from_secs(30),
    );
    assert_eq!(drained.checkpoint_pin.backfill_ceiling, None);
}

#[derive(Clone, Debug, Default)]
struct CapturedEvent {
    message: Option<String>,
    open_tx_count: Option<u64>,
    oldest_tx_age_secs: Option<String>,
    elapsed_us: Option<u64>,
    busy: Option<i64>,
    backfill_gap_frames: Option<i64>,
    legacy_wal_pin_depth: Option<i64>,
    oldest_tx_label: Option<String>,
    tx_label: Option<String>,
    census_only: Option<String>,
}

#[derive(Default)]
struct CapturedEventVisitor(CapturedEvent);

impl Visit for CapturedEventVisitor {
    fn record_u64(&mut self, field: &Field, value: u64) {
        match field.name() {
            "open_tx_count" => self.0.open_tx_count = Some(value),
            "elapsed_us" => self.0.elapsed_us = Some(value),
            _ => {}
        }
    }

    fn record_i64(&mut self, field: &Field, value: i64) {
        match field.name() {
            "busy" => self.0.busy = Some(value),
            "backfill_gap_frames" => self.0.backfill_gap_frames = Some(value),
            "wal_pin_depth" => self.0.legacy_wal_pin_depth = Some(value),
            _ => {}
        }
    }

    fn record_str(&mut self, field: &Field, value: &str) {
        match field.name() {
            "message" => self.0.message = Some(value.to_string()),
            "oldest_tx_label" => self.0.oldest_tx_label = Some(value.to_string()),
            "tx_label" => self.0.tx_label = Some(value.to_string()),
            _ => {}
        }
    }

    fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
        let formatted = format!("{value:?}");
        let cleaned = formatted
            .trim_start_matches('"')
            .trim_end_matches('"')
            .to_string();
        match field.name() {
            "message" => self.0.message = Some(cleaned),
            "oldest_tx_label" => self.0.oldest_tx_label = Some(cleaned),
            "tx_label" => self.0.tx_label = Some(cleaned),
            "census_only" => self.0.census_only = Some(cleaned),
            "oldest_tx_age_secs" => self.0.oldest_tx_age_secs = Some(cleaned),
            _ => {}
        }
    }
}

/// Minimal `tracing::Subscriber` that captures events into a thread-local
/// vec, installed as the thread-local default for the duration of one
/// test closure via `tracing::subscriber::with_default`. Mirrors the
/// capture subscriber in `khive-runtime/src/pack.rs`'s gate-dispatch tests.
struct CaptureSubscriber {
    events: std::sync::Arc<std::sync::Mutex<Vec<CapturedEvent>>>,
}

impl tracing::Subscriber for CaptureSubscriber {
    fn enabled(&self, _: &tracing::Metadata<'_>) -> bool {
        true
    }
    fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id {
        tracing::span::Id::from_u64(1)
    }
    fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {}
    fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {}
    fn event(&self, event: &tracing::Event<'_>) {
        let mut visitor = CapturedEventVisitor::default();
        event.record(&mut visitor);
        self.events.lock().unwrap().push(visitor.0);
    }
    fn enter(&self, _: &tracing::span::Id) {}
    fn exit(&self, _: &tracing::span::Id) {}
}

/// Builds two `OldestSpan`s that differ ONLY in age, off one real
/// registration so the id and origin are the ones the tick would carry.
/// The ages straddle the threshold passed to the function under test.
fn spans_straddling(
    threshold: Duration,
) -> (
    khive_storage::tx_registry::OldestSpan,
    khive_storage::tx_registry::OldestSpan,
) {
    let _handle = khive_storage::tx_registry::register(Some("writer_task_tx".to_string()));
    let (id, _age, _label) = khive_storage::tx_registry::oldest().expect("a registration is open");
    let base = khive_storage::tx_registry::OldestSpan {
        id,
        age: Duration::ZERO,
        label: Some("writer_task_tx".to_string()),
        origin: khive_storage::tx_registry::TxOrigin::Unscoped,
    };
    let aged = khive_storage::tx_registry::OldestSpan {
        age: threshold + Duration::from_secs(1),
        ..base.clone()
    };
    let young = khive_storage::tx_registry::OldestSpan {
        // The capture that produced this fix: 5.8 ms, a writer.
        age: Duration::from_micros(5_849),
        ..base
    };
    (aged, young)
}

fn capture<F: FnOnce()>(f: F) -> Vec<CapturedEvent> {
    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };
    tracing::subscriber::with_default(subscriber, f);
    let events = buffer.lock().unwrap();
    events.clone()
}

#[test]
fn truncate_no_progress_warn_reports_nonempty_registry_snapshot_facts() {
    let snapshot = [
        (Duration::from_secs(2), Some("younger-entry".to_string())),
        (Duration::from_secs(7), Some("older-entry".to_string())),
    ];
    let events = capture(|| log_truncate_no_progress_warn(6003, 6003, &snapshot));
    assert_eq!(events.len(), 3, "one summary and both captured entries");
    let summary = &events[0];
    assert_eq!(summary.open_tx_count, Some(2));
    assert_eq!(summary.oldest_tx_age_secs.as_deref(), Some("Some(7.0)"));
    let message = summary.message.as_deref().expect("summary message");
    assert_eq!(
        message,
        "WAL TRUNCATE attempt made no progress; open transactions observed in this process's registry"
    );
    assert!(!message.contains("pinning"));
    assert!(!message.contains("long-lived reader"));
    assert_eq!(events[1].tx_label.as_deref(), Some("younger-entry"));
    assert_eq!(events[2].tx_label.as_deref(), Some("older-entry"));
    assert!(events[1..].iter().all(|event| {
        event.message.as_deref() == Some("WAL high-water: open transaction registry entry")
    }));
}

#[test]
fn truncate_no_progress_warn_reports_empty_process_registry_without_pin_claim() {
    let events = capture(|| log_truncate_no_progress_warn(6003, 6003, &[]));
    assert_eq!(
        events.len(),
        1,
        "an empty snapshot has no entries to enumerate"
    );
    let summary = &events[0];
    assert_eq!(summary.open_tx_count, Some(0));
    assert_eq!(summary.oldest_tx_age_secs.as_deref(), Some("None"));
    let message = summary.message.as_deref().expect("summary message");
    assert_eq!(
        message,
        "WAL TRUNCATE attempt made no progress; no open transaction in this process's registry"
    );
    assert!(!message.contains("pinning"));
    assert!(!message.contains("long-lived reader"));
    let nonempty = capture(|| {
        log_truncate_no_progress_warn(6003, 6003, &[(Duration::ZERO, None)]);
    });
    assert_ne!(summary.message, nonempty[0].message);
    assert_eq!(nonempty[0].open_tx_count, Some(1));
    assert_eq!(nonempty[0].oldest_tx_age_secs.as_deref(), Some("Some(0.0)"));
}

/// An entry at or past the threshold: the WARN names it, its age and its
/// label. This is the branch the old unconditional text was right about.
#[test]
#[serial(tx_registry)]
fn high_water_warn_names_an_aged_registered_transaction_without_asserting_a_pin() {
    let threshold = Duration::from_secs(30);
    let (aged, _young) = spans_straddling(threshold);

    let events = capture(|| log_wal_high_water_warn(6003, 6000, Some(&aged), threshold));

    let message = events
        .iter()
        .find_map(|e| e.message.clone())
        .expect("one WARN is emitted");
    assert!(
        message.contains("in-process registered transaction is older")
            && message.contains("may hold a snapshot")
            && !message.contains("is pinning a snapshot"),
        "an aged entry is evidence of age, not proof of a pin: {message:?}"
    );
    assert_eq!(
        events.iter().find_map(|e| e.oldest_tx_label.clone()),
        Some("writer_task_tx".to_string()),
        "the named entry is the one handed in"
    );
}

/// A young in-process entry does not rule out a reader in another process.
#[test]
#[serial(tx_registry)]
fn high_water_warn_keeps_external_reader_and_write_rate_hypotheses_when_young() {
    let threshold = Duration::from_secs(30);
    let (aged, young) = spans_straddling(threshold);

    let young_message = capture(|| log_wal_high_water_warn(6003, 6000, Some(&young), threshold))
        .iter()
        .find_map(|e| e.message.clone())
        .expect("one WARN is emitted");
    let aged_message = capture(|| log_wal_high_water_warn(6003, 6000, Some(&aged), threshold))
        .iter()
        .find_map(|e| e.message.clone())
        .expect("one WARN is emitted");

    // One fixture cannot demonstrate a branch: the two must differ.
    assert_ne!(
        young_message, aged_message,
        "the two registry states must produce different text"
    );
    assert!(
        young_message.contains("no in-process transaction older than the age threshold")
            && young_message.contains("a reader in another process may hold the snapshot")
            && young_message.contains("writes may outpace PASSIVE checkpoints"),
        "got {young_message:?}"
    );
    assert!(
        !young_message.contains("is pinning a snapshot"),
        "the young branch must not assert a pin, got {young_message:?}"
    );
    assert!(
        !young_message.contains("long-lived reader"),
        "the young branch must not name a reader, got {young_message:?}"
    );
}

/// An empty local registry still cannot exclude an external reader.
#[test]
#[serial(tx_registry)]
fn high_water_warn_with_an_empty_registry_keeps_external_reader_visible() {
    let events = capture(|| log_wal_high_water_warn(6003, 6000, None, Duration::from_secs(30)));
    let message = events
        .iter()
        .find_map(|e| e.message.clone())
        .expect("one WARN is emitted");
    assert!(
        message.contains("no in-process transaction older than the age threshold")
            && message.contains("a reader in another process may hold the snapshot")
            && message.contains("writes may outpace PASSIVE checkpoints"),
        "got {message:?}"
    );
    assert_eq!(
        events.iter().find_map(|e| e.oldest_tx_label.clone()),
        Some("<none>".to_string()),
        "an absent entry is labelled as absent, never as unlabeled"
    );
}

/// `log_tx_registry_oldest_debug` names the oldest open registry entry.
/// See crates/khive-db/docs/api/checkpoint.md#log_tx_registry_oldest_debug_reports_oldest_open_entry
#[test]
#[serial(tx_registry)]
fn log_tx_registry_oldest_debug_reports_oldest_open_entry() {
    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };

    let _handle = khive_storage::tx_registry::register(Some("checkpoint_tick_test".to_string()));

    let oldest = khive_storage::tx_registry::oldest().map(|(id, age, label)| {
        khive_storage::tx_registry::OldestSpan {
            id,
            age,
            label,
            origin: khive_storage::tx_registry::TxOrigin::Unscoped,
        }
    });
    let expected_label = oldest
        .as_ref()
        .and_then(|s| s.label.clone())
        .unwrap_or_else(|| "<unlabeled>".to_string());

    tracing::subscriber::with_default(subscriber, || {
        log_tx_registry_oldest_debug(100, oldest.as_ref());
    });

    let events = buffer.lock().unwrap();
    assert!(
        events.iter().any(|e| {
            e.message.as_deref()
                == Some("WAL checkpoint tick: oldest open transaction registry entry")
                && e.oldest_tx_label.as_deref() == Some(expected_label.as_str())
        }),
        "expected a log line naming the open registry entry's label, got: {events:?}"
    );
}

/// ADR-091 Plank 0: the oldest-entry WARN and the
/// high-water snapshot-enumeration WARN are gated by `crossing_warn` at
/// the call site (mirroring the WAL-threshold WARNs), so driving two
/// consecutive above-threshold ticks through that same gate must produce
/// exactly one of each — never a repeat on the second tick.
#[test]
#[serial(tx_registry)]
fn registry_warns_fire_on_crossing_and_do_not_repeat() {
    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };

    let _handle =
        khive_storage::tx_registry::register(Some("registry_warn_crossing_test".to_string()));
    let oldest = khive_storage::tx_registry::oldest().map(|(id, age, label)| {
        khive_storage::tx_registry::OldestSpan {
            id,
            age,
            label,
            origin: khive_storage::tx_registry::TxOrigin::Unscoped,
        }
    });

    let mut was_above_warn = false;
    let mut was_above_high_water = false;

    tracing::subscriber::with_default(subscriber, || {
        // Tick 1: below→above crossing for both bands — both WARNs fire.
        if crossing_warn(true, &mut was_above_warn) {
            log_tx_registry_oldest_warn(6000, oldest.as_ref());
        }
        if crossing_warn(true, &mut was_above_high_water) {
            log_tx_registry_snapshot_warn(6000);
        }

        // Tick 2: still above both thresholds — neither must repeat.
        if crossing_warn(true, &mut was_above_warn) {
            log_tx_registry_oldest_warn(6000, oldest.as_ref());
        }
        if crossing_warn(true, &mut was_above_high_water) {
            log_tx_registry_snapshot_warn(6000);
        }
    });

    let events = buffer.lock().unwrap();

    // `tracing::subscriber::with_default` scopes capture to THIS thread for
    // the duration of the closure, so `events` contains only the two
    // `log_tx_registry_oldest_warn` calls made above — no concurrent test's
    // log calls land in this buffer. This lets the crossing/no-repeat
    // assertion match on message text alone: unlike the "names MY label"
    // assertion in the sibling test above, WHICH label `oldest()` reports
    // is irrelevant here (a concurrent write path elsewhere in the binary
    // may transiently be the registry's genuine oldest entry) — only the
    // fire-once-per-crossing COUNT is under test.
    let oldest_warn_count = events
        .iter()
        .filter(|e| {
            e.message.as_deref()
                == Some("WAL checkpoint tick: oldest open transaction registry entry")
        })
        .count();
    assert_eq!(
        oldest_warn_count, 1,
        "oldest-entry WARN must fire exactly once across two above-threshold ticks, got: {events:?}"
    );

    let snapshot_warn_count = events
        .iter()
        .filter(|e| {
            e.message.as_deref() == Some("WAL high-water: open transaction registry entry")
                && e.tx_label.as_deref() == Some("registry_warn_crossing_test")
        })
        .count();
    assert_eq!(
        snapshot_warn_count, 1,
        "high-water snapshot WARN must fire exactly once across two above-threshold ticks, got: {events:?}"
    );
}

/// ADR-091 Plank 1: `log_tx_age_emission` emits the correct message text
/// and carries the entry's label, for both the `Warn` and `Stale` rungs.
#[test]
fn log_tx_age_emission_carries_label_for_both_rungs() {
    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };

    tracing::subscriber::with_default(subscriber, || {
        log_tx_age_emission(&TxAgeEmission {
            rung: TxAgeRung::Warn,
            age: Duration::from_secs(45),
            label: Some("plank1_warn_test".to_string()),
        });
        log_tx_age_emission(&TxAgeEmission {
            rung: TxAgeRung::Stale,
            age: Duration::from_secs(150),
            label: Some("plank1_stale_test".to_string()),
        });
    });

    let events = buffer.lock().unwrap();
    assert!(
        events.iter().any(|e| {
            e.message.as_deref()
                == Some("ADR-091 Plank 1: open transaction registry entry exceeded soft-cap age")
                && e.tx_label.as_deref() == Some("plank1_warn_test")
        }),
        "expected a Warn-rung log line naming the entry, got: {events:?}"
    );
    assert!(
        events.iter().any(|e| {
            e.message.as_deref().is_some_and(|m| {
                m.starts_with(
                    "ADR-091 Plank 1: open transaction registry entry exceeded the cooperative",
                )
            }) && e.tx_label.as_deref() == Some("plank1_stale_test")
        }),
        "expected a Stale-rung log line naming the entry, got: {events:?}"
    );
}

pub(super) fn file_pool(path: &std::path::Path) -> Arc<ConnectionPool> {
    let cfg = PoolConfig {
        path: Some(path.to_path_buf()),
        ..PoolConfig::for_test()
    };
    Arc::new(ConnectionPool::new(cfg).expect("pool open"))
}

async fn writer_task_wal_autocheckpoint_pages(handle: &WriterTaskHandle) -> u32 {
    handle
        .send_top_level(|conn| {
            conn.pragma_query_value(None, "wal_autocheckpoint", |row| row.get::<_, u32>(0))
                .map_err(|error| khive_storage::error::StorageError::Pool {
                    operation: "test_wal_autocheckpoint".into(),
                    message: error.to_string(),
                })
        })
        .await
        .expect("query writer-task connection pragma")
}

/// Test helper: open the same dedicated standalone connection
/// `run_checkpoint_task` opens in production, for tests that drive
/// `checkpoint_once` directly.
pub(super) fn checkpoint_conn(pool: &ConnectionPool) -> rusqlite::Connection {
    pool.open_standalone_writer()
        .expect("open dedicated checkpoint connection")
}

struct TruncateReportHookGuard;

impl Drop for TruncateReportHookGuard {
    fn drop(&mut self) {
        truncate_report_test_sync::uninstall();
    }
}

#[cfg(unix)]
struct WalpinAttributionHookGuard;

#[cfg(unix)]
impl Drop for WalpinAttributionHookGuard {
    fn drop(&mut self) {
        walpin_attribution_test_sync::uninstall();
    }
}

#[tokio::test(flavor = "current_thread")]
#[cfg(unix)]
#[serial(khive_walpin_sidecar_env)]
async fn diagnostic_legacy_forecast_matches_housekeeping_with_distinct_cadences() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let root = tempfile::tempdir().unwrap();
    let pool = file_pool(&root.path().join("forecast.db"));
    let path = pool.canonical_path().unwrap();
    let checkpoint_interval = Duration::from_millis(500);
    let session_interval = SessionSweepConfig::default().interval;
    assert_eq!(session_interval, Duration::from_secs(5));
    let state = TruncateState::default();
    assert_eq!(state.legacy_walpin_fallback_interval, session_interval);
    let sidecar = WalpinSidecarState::new(Some(path), true, "daemon", checkpoint_interval)
        .expect("enabled fixture sidecar");
    crate::walpin::ensure_sidecar_dir(&sidecar.dir).unwrap();
    let mut paths = Vec::new();
    for (pid, age) in [(2_000_000_001, 5), (2_000_000_002, 40)] {
        assert!(!crate::walpin::is_process_alive(pid));
        let temp = sidecar.dir.join(format!(".{pid}.beacon.tmp"));
        std::fs::write(
            &temp,
            serde_json::to_vec(&serde_json::json!({
                "pid": pid, "process_role": "session", "started_at": 1
            }))
            .unwrap(),
        )
        .unwrap();
        std::fs::File::options()
            .write(true)
            .open(&temp)
            .unwrap()
            .set_modified(std::time::SystemTime::now() - Duration::from_secs(age))
            .unwrap();
        paths.push(temp);
    }
    let fast = crate::walpin::inspect_live(&sidecar.dir, checkpoint_interval).unwrap();
    assert_eq!(
        fast.cleanup_would_reap, 2,
        "control must distinguish the cadences"
    );
    let forecast = crate::diagnostics::wal_pin_attribution(path, session_interval);
    assert_eq!(forecast.sidecar_listing_truncated, Some(false));
    assert_eq!(forecast.sidecar_entries_cleanup_would_reap, Some(1));
    assert!(
        paths.iter().all(|path| path.exists()),
        "inspection retains evidence"
    );

    let cleanup = sidecar
        .reap_dead_entries_bounded(state.legacy_walpin_fallback_interval)
        .await
        .expect("housekeeping report");
    assert_eq!(
        Some(cleanup.orphan_temps_reaped),
        forecast.sidecar_entries_cleanup_would_reap
    );
    assert!(paths[0].exists(), "the temp inside the 15s window remains");
    assert!(!paths[1].exists(), "the trusted older temp is reaped");
}

#[test]
#[cfg(unix)]
fn walpin_full_scan_cadence_refreshes_first_then_reuses_until_boundary() {
    let cadence = Duration::from_secs(30);
    let started_at = Instant::now();
    let mut state = TruncateState::with_walpin_full_scan_cadence(cadence);

    assert!(matches!(
        state.plan_walpin_attribution_at(started_at),
        WalpinFullScanPlan::Refresh { .. }
    ));
    state.cache_walpin_attribution(
        crate::walpin::WalpinReport::default(),
        Ok(crate::walpin::CensusResult::default()),
        started_at,
    );

    assert!(matches!(
        state.plan_walpin_attribution_at(started_at + cadence - Duration::from_nanos(1)),
        WalpinFullScanPlan::Cached(_)
    ));
    assert!(matches!(
        state.plan_walpin_attribution_at(started_at + cadence),
        WalpinFullScanPlan::Refresh { .. }
    ));
}

#[test]
#[cfg(unix)]
fn walpin_full_scan_failure_retries_only_after_cadence() {
    let cadence = Duration::from_secs(30);
    let started_at = Instant::now();
    let mut state = TruncateState::with_walpin_full_scan_cadence(cadence);

    assert!(matches!(
        state.plan_walpin_attribution_at(started_at),
        WalpinFullScanPlan::Refresh { .. }
    ));
    // No cache update models either an enumeration error or a panicked
    // blocking worker. The attempt itself still owns the cadence slot.
    assert!(matches!(
        state.plan_walpin_attribution_at(started_at + cadence - Duration::from_nanos(1)),
        WalpinFullScanPlan::Suppressed
    ));
    assert!(matches!(
        state.plan_walpin_attribution_at(started_at + cadence),
        WalpinFullScanPlan::Refresh { .. }
    ));
}

#[test]
#[cfg(unix)]
fn cached_walpin_report_is_diagnostic_only_even_when_fully_attributed() {
    let report = crate::walpin::WalpinReport::default();
    assert!(
        report.fully_attributed(),
        "the fixture must otherwise license the sharp conclusion"
    );
    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };

    tracing::subscriber::with_default(subscriber, || {
        log_walpin_sidecar_report(
            &report,
            Ok(crate::walpin::CensusResult::default()),
            WalpinReportFreshness::Cached {
                age: Duration::from_secs(1),
            },
        );
    });

    let events = buffer.lock().unwrap();
    assert!(
        events.iter().any(|event| {
            event.message.as_deref()
                == Some(
                    "cached WAL-pin attribution is diagnostic-only; fully-attributed \
                         conclusion is not licensed",
                )
        }),
        "cached attribution must declare its fail-closed status: {events:?}"
    );
    assert!(
        !events.iter().any(|event| {
            event.message.as_deref().is_some_and(|message| {
                message.starts_with("ADR-091 Amendment 2 Plank B: every live PID is reporting")
            })
        }),
        "cached attribution must never authorize the fully-attributed conclusion: {events:?}"
    );
}

#[tokio::test(flavor = "current_thread")]
#[cfg(unix)]
#[serial(checkpoint_skip_metrics, khive_walpin_sidecar_env)]
async fn progressing_truncate_releases_full_scan_reservation_to_housekeeping() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("walpin-progress-reservation.db");
    let pool = file_pool(&path);
    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE t (x INTEGER); INSERT INTO t VALUES (1);")
            .unwrap();
    }
    let conn = checkpoint_conn(&pool);
    let mut state = TruncateState::default();
    let config = CheckpointConfig {
        truncate_high_water_pages: 0,
        truncate_min_interval: Duration::ZERO,
        ..CheckpointConfig::default()
    };

    assert!(
        maybe_truncate(&pool, &conn, &config, u64::MAX, &mut state).is_none(),
        "a progressing TRUNCATE must not schedule no-progress attribution"
    );
    assert!(
        state.housekeeping_due(),
        "unused pre-TRUNCATE reservation must be restored before housekeeping"
    );
    let sidecar = WalpinSidecarState::new(pool.canonical_path(), true, "daemon", config.interval)
        .expect("file-backed test sidecar");
    assert!(
        run_walpin_housekeeping_if_due(&sidecar, &mut state, DEFAULT_SESSION_SWEEP_INTERVAL,).await,
        "the production housekeeping arm must consume one full scan"
    );
    assert!(state.walpin_cached_attribution.is_some());
}

#[tokio::test(flavor = "current_thread")]
#[cfg(unix)]
#[serial(checkpoint_skip_metrics, khive_walpin_sidecar_env)]
async fn erroring_truncate_releases_full_scan_reservation_to_housekeeping() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("walpin-error-reservation.db");
    let pool = file_pool(&path);
    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE t (x INTEGER); INSERT INTO t VALUES (1);")
            .unwrap();
    }
    let conn = checkpoint_conn(&pool);
    conn.authorizer(Some(
        |context: rusqlite::hooks::AuthContext<'_>| match context.action {
            AuthAction::Pragma { pragma_name, .. }
                if pragma_name.eq_ignore_ascii_case("wal_checkpoint") =>
            {
                Authorization::Deny
            }
            _ => Authorization::Allow,
        },
    ))
    .unwrap();
    let mut state = TruncateState::default();
    let config = CheckpointConfig {
        truncate_high_water_pages: 0,
        truncate_min_interval: Duration::ZERO,
        ..CheckpointConfig::default()
    };

    assert!(
        maybe_truncate(&pool, &conn, &config, u64::MAX, &mut state).is_none(),
        "an erroring TRUNCATE must not schedule no-progress attribution"
    );
    conn.authorizer(None::<fn(rusqlite::hooks::AuthContext<'_>) -> Authorization>)
        .unwrap();
    assert!(
        state.housekeeping_due(),
        "failed TRUNCATE must restore its unused full-scan reservation"
    );
    let sidecar = WalpinSidecarState::new(pool.canonical_path(), true, "daemon", config.interval)
        .expect("file-backed test sidecar");
    assert!(
        run_walpin_housekeeping_if_due(&sidecar, &mut state, DEFAULT_SESSION_SWEEP_INTERVAL,).await,
        "the production housekeeping arm must consume one full scan"
    );
    assert!(state.walpin_cached_attribution.is_some());
}

struct ReaderProcess {
    child: std::process::Child,
    _stdout: std::io::BufReader<std::process::ChildStdout>,
}

impl ReaderProcess {
    fn spawn(db_path: &std::path::Path) -> Self {
        use std::io::BufRead;
        use std::process::Stdio;

        let mut child = std::process::Command::new(
            std::env::current_exe().expect("resolve current test executable"),
        )
        .args([
            "--exact",
            "checkpoint::tests::walpin_transient_reader_process_helper",
            "--nocapture",
        ])
        .env("KHIVE_CHECKPOINT_READER_HELPER_PATH", db_path)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .expect("spawn transient WAL reader helper");

        let stdout = child.stdout.take().expect("capture helper stdout");
        let mut reader = std::io::BufReader::new(stdout);
        let mut line = String::new();
        loop {
            line.clear();
            let bytes = reader
                .read_line(&mut line)
                .expect("read transient reader readiness signal");
            assert!(bytes > 0, "reader helper exited before readiness signal");
            if line.contains("KHIVE_CHECKPOINT_READER_READY") {
                break;
            }
        }
        Self {
            child,
            _stdout: reader,
        }
    }

    fn pid(&self) -> u32 {
        self.child.id()
    }

    fn release(&mut self) {
        use std::io::Write;

        let mut stdin = self.child.stdin.take().expect("helper stdin is available");
        stdin
            .write_all(b"release\n")
            .expect("release transient reader");
        drop(stdin);
        let status = self.child.wait().expect("wait for transient reader helper");
        assert!(status.success(), "transient reader helper failed: {status}");
    }
}

impl Drop for ReaderProcess {
    fn drop(&mut self) {
        if self.child.try_wait().ok().flatten().is_none() {
            let _ = self.child.kill();
            let _ = self.child.wait();
        }
    }
}

#[test]
fn walpin_transient_reader_process_helper() {
    use std::io::Write;

    let Some(path) = std::env::var_os("KHIVE_CHECKPOINT_READER_HELPER_PATH") else {
        return;
    };
    let conn = rusqlite::Connection::open(path).expect("helper opens database");
    conn.execute_batch("BEGIN DEFERRED; SELECT * FROM t;")
        .expect("helper pins a read snapshot");
    println!("KHIVE_CHECKPOINT_READER_READY");
    std::io::stdout().flush().expect("flush readiness signal");
    let mut release = String::new();
    std::io::stdin()
        .read_line(&mut release)
        .expect("wait for release signal");
    conn.execute_batch("COMMIT")
        .expect("helper releases read snapshot");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[cfg(all(unix, any(target_os = "linux", target_os = "macos")))]
#[serial(checkpoint_skip_metrics, khive_walpin_census_budget_env)]
async fn db_diagnostics_keeps_a_holder_started_after_the_run_observation() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("post_observation_holder.db");
    let pool = file_pool(&path);
    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE t (value INTEGER); INSERT INTO t VALUES (0)")
            .unwrap();
    }

    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let task = tokio::spawn(run_checkpoint_task(
        Arc::clone(&pool),
        CheckpointConfig {
            interval: Duration::from_secs(60),
            ..Default::default()
        },
        None,
        shutdown_rx,
        true,
    ));
    tokio::time::timeout(Duration::from_secs(2), async {
        while routine_wal_observation(&pool).is_none() {
            tokio::task::yield_now().await;
        }
    })
    .await
    .expect("the checkpoint task must record its initial sample");

    let earlier_reader = rusqlite::Connection::open(&path).unwrap();
    earlier_reader.execute_batch("BEGIN DEFERRED").unwrap();
    earlier_reader
        .query_row("SELECT COUNT(*) FROM t", [], |row| row.get::<_, i64>(0))
        .unwrap();
    {
        let writer = pool.writer().unwrap();
        writer
            .conn()
            .execute("INSERT INTO t VALUES (1)", [])
            .unwrap();
    }

    let first = crate::diagnostics::collect(
        &pool,
        crate::diagnostics::BuildIdentity::from_env("test", None),
        Duration::from_secs(30),
    );
    let first_probe = first.checkpoint_probe.as_ref().expect("probe row");
    assert!(first_probe.log_frames > first_probe.checkpointed_frames);
    let run = checkpoint_run_status(&pool);
    let first_observed_at_unix_ms = match run {
        CheckpointRunStatus::Observed(run) => run.first_observed_at_unix_ms,
        other => panic!("diagnostic probe must establish a checkpoint run: {other:?}"),
    };

    let mut later_reader = ReaderProcess::spawn(&path);
    let later_pid = later_reader.pid();
    let report = crate::diagnostics::collect(
        &pool,
        crate::diagnostics::BuildIdentity::from_env("test", None),
        Duration::from_secs(30),
    );
    let later_start = report
        .wal_pin
        .census_process_start_times
        .iter()
        .find(|process| process.pid == later_pid)
        .expect("a post-observation holder must remain in the census");
    assert_eq!(report.wal_pin.reporting_process_is_holder, Some(true));
    assert!(
        report.wal_pin.census_holder_pids.contains(&later_pid),
        "the later process remains a confirmed holder"
    );
    assert_eq!(
        later_start.process_start_time_secs,
        crate::walpin::process_start_time_secs(later_pid)
    );
    assert!(later_start.process_start_time_secs.is_some());
    assert_eq!(later_start.process_start_time_unavailable_reason, None);
    #[cfg(target_os = "linux")]
    assert_eq!(report.wal_pin.start_time_resolution_secs, Some(2));
    #[cfg(target_os = "macos")]
    assert_eq!(report.wal_pin.start_time_resolution_secs, Some(1));
    assert!(first_observed_at_unix_ms > 0);
    assert!(!report
        .wal_pin
        .census_process_start_times
        .iter()
        .any(|process| process.pid == later_pid && process.process_start_time_secs.is_none()));

    later_reader.release();
    earlier_reader.execute_batch("ROLLBACK").unwrap();
    shutdown_tx.send(()).unwrap();
    tokio::time::timeout(Duration::from_secs(2), task)
        .await
        .expect("checkpoint task shutdown must finish")
        .expect("checkpoint task must not panic");
}

#[tokio::test(flavor = "current_thread")]
#[cfg(unix)]
#[serial(
    checkpoint_skip_metrics,
    khive_walpin_sidecar_env,
    walpin_attribution_async,
    walpin_report_seam
)]
async fn no_progress_report_keeps_holder_released_after_truncate_timeout() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("transient-reader.db");
    let pool = file_pool(&path);
    {
        let writer = pool.try_writer().expect("writer");
        writer
            .conn()
            .execute_batch("CREATE TABLE t (x INTEGER); INSERT INTO t VALUES (1);")
            .expect("seed WAL before reader snapshot");
    }

    let mut reader = ReaderProcess::spawn(&path);
    let reader_pid = reader.pid();
    {
        let writer = pool.try_writer().expect("writer");
        writer
            .conn()
            .execute_batch("INSERT INTO t VALUES (2);")
            .expect("append WAL behind reader snapshot");
    }

    let canonical_path = pool
        .canonical_path()
        .expect("file-backed pool has canonical path")
        .to_path_buf();
    let (reached_rx, proceed_tx) = truncate_report_test_sync::install(canonical_path.clone());
    let _hook_guard = TruncateReportHookGuard;
    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let checkpoint_pool = Arc::clone(&pool);
    let dedicated_conn = checkpoint_conn(&checkpoint_pool);
    let checkpoint_events = Arc::clone(&buffer);
    let checkpoint = std::thread::spawn(move || {
        let subscriber = CaptureSubscriber {
            events: checkpoint_events,
        };
        let _subscriber_guard = tracing::subscriber::set_default(subscriber);
        let mut state = TruncateState::default();
        let result = checkpoint_once_core(
            &checkpoint_pool,
            &dedicated_conn,
            &CheckpointConfig {
                truncate_high_water_pages: 0,
                truncate_min_interval: Duration::ZERO,
                truncate_busy_timeout: Duration::from_millis(50),
                ..CheckpointConfig::default()
            },
            &mut state,
        );
        (result, state)
    });

    reached_rx
        .recv_timeout(Duration::from_secs(5))
        .expect("TRUNCATE must report no progress while the reader is pinned");
    reader.release();
    let post_attempt_census =
        crate::walpin::census_holders(&canonical_path).expect("post-attempt holder census");
    assert!(
        !post_attempt_census.holders.contains(&reader_pid),
        "released reader PID must be absent from a post-attempt census"
    );
    proceed_tx
        .send(())
        .expect("allow no-progress reporting to continue");
    let (checkpoint_result, mut state) = checkpoint.join().expect("checkpoint thread");
    let outcome = checkpoint_result.expect("checkpoint succeeds");
    assert!(
        outcome.sidecar_attribution.is_some(),
        "the synchronous checkpoint result must carry a separate attribution request"
    );
    assert!(
        !state.sidecar_attribution_attempted_this_tick,
        "capturing a request is not the same as attempting its directory enumeration"
    );

    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };
    let _subscriber_guard = tracing::subscriber::set_default(subscriber);
    let attribution_attempted =
        complete_walpin_attribution(outcome.sidecar_attribution, &mut state)
            .await
            .expect("deferred attribution succeeds");
    assert!(
        attribution_attempted,
        "a no-progress attribution pass must suppress the redundant healthy-housekeeping \
             pass for the same tick"
    );

    let events = buffer.lock().expect("captured events");
    let summaries: Vec<_> = events
        .iter()
        .filter(|event| {
            event.message.as_deref().is_some_and(|message| {
                message.starts_with("WAL TRUNCATE attempt made no progress;")
            })
        })
        .collect();
    assert_eq!(
        summaries.len(),
        1,
        "the real no-progress path emits one summary"
    );
    let summary = summaries[0];
    assert!(summary.open_tx_count.is_some());
    assert!(summary.oldest_tx_age_secs.is_some());
    let message = summary.message.as_deref().expect("summary message");
    assert!(message.contains("in this process's registry"));
    assert!(!message.contains("pinning"));
    assert!(!message.contains("long-lived reader"));
    assert!(
        events.iter().any(|event| {
            event
                .census_only
                .as_deref()
                .is_some_and(|pids| pids.contains(&reader_pid.to_string()))
        }),
        "the no-progress report must retain PID {reader_pid} from the pre-attempt census: {events:?}"
    );
}

/// The 512-entry attribution walk must run on Tokio's blocking pool and
/// the async owner must await it before the report is consumed. A paused
/// real enumeration proves all three facts without a timing sleep: its
/// thread differs from the current-thread runtime, the completion future
/// remains pending, and the report-use counter stays zero until release.
#[tokio::test(flavor = "current_thread")]
#[cfg(unix)]
#[serial(walpin_attribution_async)]
async fn no_progress_attribution_is_off_runtime_and_awaited_before_report_use() {
    use std::sync::atomic::Ordering;

    let dir = tempfile::tempdir().expect("tempdir");
    let sidecar_dir = dir.path().join("checkpoint.db.walpin");
    let (reached_rx, proceed_tx, report_counter) =
        walpin_attribution_test_sync::install_pause(sidecar_dir.clone());
    let _hook_guard = WalpinAttributionHookGuard;

    let runtime_thread = std::thread::current().id();
    let mut state = TruncateState::default();
    let request = Some(WalpinAttributionRequest::Fresh {
        dir: sidecar_dir,
        census: Ok(crate::walpin::CensusResult::default()),
        legacy_fallback_interval: DEFAULT_SESSION_SWEEP_INTERVAL,
        previous_last_attempt: None,
    });
    let completion = tokio::spawn(async move {
        let result = complete_walpin_attribution(request, &mut state).await;
        (result, state)
    });

    let blocking_thread = reached_rx
        .await
        .expect("spawn_blocking attribution reached test seam");
    assert_ne!(
        blocking_thread, runtime_thread,
        "sidecar enumeration must not execute on the current-thread Tokio runtime worker"
    );
    assert!(
        !completion.is_finished(),
        "the async attribution owner must await the still-paused blocking enumeration"
    );
    assert_eq!(
        report_counter.load(Ordering::SeqCst),
        0,
        "the attribution report must not be consumed before enumeration completes"
    );

    proceed_tx
        .send(())
        .expect("release blocking attribution enumeration");
    let (result, state) = completion.await.expect("attribution task joins");
    assert_eq!(result, Ok(true));
    assert_eq!(
        report_counter.load(Ordering::SeqCst),
        1,
        "the completed enumeration must feed exactly one report use"
    );
    assert!(state.sidecar_attribution_attempted_this_tick);
    assert!(
        !state.housekeeping_due(),
        "completed attribution must suppress same-tick housekeeping"
    );
}

/// A panicked blocking worker is not flattened into success. The tick is
/// still marked attempted because the worker may have partially walked
/// the directory before failing, so starting housekeeping afterward
/// would violate the one-pass bound.
#[tokio::test(flavor = "current_thread")]
#[cfg(unix)]
#[serial(walpin_attribution_async)]
async fn no_progress_attribution_join_failure_is_honest_and_suppresses_retry() {
    let dir = tempfile::tempdir().expect("tempdir");
    let sidecar_dir = dir.path().join("checkpoint.db.walpin");
    walpin_attribution_test_sync::install_panic(sidecar_dir.clone());
    let _hook_guard = WalpinAttributionHookGuard;

    let mut state = TruncateState::default();
    let request = Some(WalpinAttributionRequest::Fresh {
        dir: sidecar_dir,
        census: Ok(crate::walpin::CensusResult::default()),
        legacy_fallback_interval: DEFAULT_SESSION_SWEEP_INTERVAL,
        previous_last_attempt: None,
    });

    let error = complete_walpin_attribution(request, &mut state)
        .await
        .expect_err("injected worker panic must surface as failure");
    assert!(
        matches!(error, WalpinAttributionFailure::Worker(_)),
        "join failure must retain its worker classification: {error:?}"
    );
    assert!(state.sidecar_attribution_attempted_this_tick);
    assert!(
        !state.housekeeping_due(),
        "an indeterminate partial pass must not authorize a second scan"
    );
}

/// Structural guard for the accepted ADR split: the synchronous SQLite
/// checkpoint core only schedules attribution, the sole direct
/// `enumerate_live` call is nested in an awaited `spawn_blocking`, and the
/// task completes it before either housekeeping or lifecycle outcome use.
#[test]
#[cfg(unix)]
#[serial(checkpoint_skip_metrics)]
fn async_checkpoint_source_keeps_enumeration_behind_awaited_spawn_blocking() {
    fn section<'a>(source: &'a str, start: &str, end: &str) -> &'a str {
        source
            .split_once(start)
            .unwrap_or_else(|| panic!("missing source marker {start:?}"))
            .1
            .split_once(end)
            .unwrap_or_else(|| panic!("missing source marker {end:?}"))
            .0
    }

    let source = include_str!("checkpoint.rs");
    let checkpoint_core = section(
        source,
        "fn checkpoint_once_core(",
        "/// Evaluate and, if due, attempt a TRUNCATE escalation",
    );
    let truncate_core = section(
        source,
        "fn maybe_truncate(",
        "#[cfg(test)]\nmod truncate_report_test_sync",
    );
    let report_logger = section(
        source,
        "fn log_walpin_sidecar_report(",
        "/// ADR-091 Amendment 2 Plank C",
    );
    for (name, body) in [
        ("checkpoint_once_core", checkpoint_core),
        ("maybe_truncate", truncate_core),
        ("log_walpin_sidecar_report", report_logger),
    ] {
        assert!(
            !body.contains("enumerate_live("),
            "{name} must not perform direct sidecar enumeration"
        );
    }

    let async_completion = section(
        source,
        "async fn complete_walpin_attribution(",
        "/// When a TRUNCATE attempt makes no progress",
    );
    let spawn = async_completion
        .find("tokio::task::spawn_blocking")
        .expect("completion must spawn blocking work");
    let enumerate = async_completion
        .find("crate::walpin::enumerate_live")
        .expect("blocking closure must perform the attribution enumeration");
    let awaited = async_completion[enumerate..]
        .find(".await")
        .map(|offset| enumerate + offset)
        .expect("blocking worker must be awaited");
    assert!(spawn < enumerate && enumerate < awaited);

    let task = section(
        source,
        "pub async fn run_checkpoint_task(",
        "/// Whether a `CheckpointOutcomeRecorded` transition should be enqueued",
    );
    let checkpoint = task
        .find("run_checkpoint_core_off_worker(")
        .expect("checkpoint core call");
    let completion = task
        .find("complete_walpin_attribution(")
        .expect("awaited attribution completion");
    let housekeeping = task
        .find("run_walpin_housekeeping_if_due(")
        .expect("fallback housekeeping");
    let outcome = task
        .find("observe_checkpoint_pressure_tick(")
        .expect("lifecycle outcome use");
    assert!(
        checkpoint < completion && completion < housekeeping && housekeeping < outcome,
        "tick ordering must be checkpoint -> awaited attribution -> housekeeping decision -> outcome"
    );
    let housekeeping_helper = section(
        source,
        "async fn run_walpin_housekeeping_if_due(",
        "fn now_epoch_secs()",
    );
    assert!(
        housekeeping_helper.contains("reap_dead_entries_bounded(legacy_fallback_interval)"),
        "the ordered housekeeping arm must retain the bounded full scan"
    );

    // The outcome decision itself moved into the extracted per-tick
    // helper; the emit gate must still be consulted there, so the
    // ordering assertion above remains transitively about the same
    // lifecycle decision it always pinned.
    let pressure_tick = section(
        source,
        "fn observe_checkpoint_pressure_tick(",
        "/// ADR-091 Plank 0",
    );
    assert!(
        pressure_tick.contains("checkpoint_outcome_should_emit"),
        "extracted pressure tick helper must gate on the lifecycle emit decision"
    );
}

/// `run_fts_maintenance_off_worker` moves the same `run_if_due` call onto
/// a blocking thread; driven on a genuinely multi-threaded runtime
/// against a fragmented fixture, it must reach the same step outcome as
/// calling `run_if_due` directly on an identically-seeded fixture.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fts_maintenance_off_worker_matches_the_direct_call() {
    fn fragmented_fixture() -> (tempfile::TempDir, rusqlite::Connection) {
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("fts-off-worker.db");
        let conn = rusqlite::Connection::open(&path).expect("open sqlite");
        conn.execute_batch(
            "CREATE VIRTUAL TABLE fts_entities USING fts5(namespace UNINDEXED, subject_id UNINDEXED, title, body, tokenize='trigram');
                 CREATE VIRTUAL TABLE fts_notes USING fts5(namespace UNINDEXED, subject_id UNINDEXED, title, body, tokenize='trigram');
                 INSERT INTO fts_entities(fts_entities, rank) VALUES('automerge', 0);
                 INSERT INTO fts_notes(fts_notes, rank) VALUES('automerge', 0);",
        )
        .expect("create FTS fixtures");
        // Round-robin starts at table index 0 (`fts_entities`); fragment
        // that one so the very first due call has real merge work to do.
        for index in 0..80 {
            let body = format!(
                "segment fixture {index} keeps enough repeated production recall text to span pages {}",
                "memory query corpus ".repeat(40)
            );
            conn.execute(
                "INSERT INTO fts_entities(namespace, subject_id, title, body) VALUES(?1, ?2, ?3, ?4)",
                rusqlite::params![
                    "local",
                    format!("id-{index}"),
                    format!("title {index}"),
                    body
                ],
            )
            .expect("one autocommit FTS write");
        }
        (dir, conn)
    }

    let config = crate::fts_maintenance::FtsMaintenanceConfig {
        enabled: true,
        interval: Duration::ZERO,
        merge_pages: 8,
        minimum_segments: 2,
    };

    let (_direct_dir, direct_conn) = fragmented_fixture();
    let mut direct_state = crate::fts_maintenance::FtsMaintenanceState::new(Instant::now());
    let direct_step = crate::fts_maintenance::run_if_due(
        &direct_conn,
        &config,
        &mut direct_state,
        Instant::now(),
    )
    .expect("direct maintenance step")
    .expect("fragmented fixture has a due step");

    let (_wrapped_dir, wrapped_conn) = fragmented_fixture();
    let wrapped_state = crate::fts_maintenance::FtsMaintenanceState::new(Instant::now());
    let (_conn, _state, wrapped_result) =
        run_fts_maintenance_off_worker(wrapped_conn, config, wrapped_state, Instant::now())
            .await
            .expect("maintenance step did not panic");
    let wrapped_step = wrapped_result
        .expect("wrapped maintenance step")
        .expect("fragmented fixture has a due step");

    assert_eq!(
        direct_step, wrapped_step,
        "the spawn_blocking wrapper must produce the same step outcome as calling \
             run_if_due directly"
    );
    assert_eq!(direct_step.table, "fts_entities");
}

// `checkpoint_once` -> `query_wal_pages` writes the process-wide
// `LAST_WAL_PAGES` gauge and resets `CHECKPOINT_CONSECUTIVE_SKIPS`
// (see the reset-discipline comment on `reset_checkpoint_metrics_for_tests`
// above) — this must join the `checkpoint_skip_metrics` group so it can
// never interleave with a test asserting on those same gauges.
#[test]
#[serial(checkpoint_skip_metrics)]
fn checkpoint_once_succeeds_on_file_backed_pool() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("wal_test.db");
    let pool = file_pool(&path);

    // Create a table so the DB is not completely empty.
    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE IF NOT EXISTS t (x INTEGER);")
            .unwrap();
        writer
            .conn()
            .execute_batch("INSERT INTO t VALUES (1);")
            .unwrap();
    }

    let conn = checkpoint_conn(&pool);
    checkpoint_once(
        &pool,
        &conn,
        &CheckpointConfig::default(),
        &mut TruncateState::default(),
    )
    .expect("checkpoint_once must succeed against a healthy dedicated connection");
}

/// In-memory pools have no on-disk file to open a second, dedicated
/// standalone connection against — this is exactly the precondition that
/// makes `CheckpointConnection::ensure_open` return `None` and
/// `run_checkpoint_task` report the tick `Skipped`, so `checkpoint_once`
/// is never even called for one.
#[test]
fn open_standalone_writer_fails_on_in_memory_pool() {
    let cfg = PoolConfig {
        path: None,
        ..PoolConfig::default()
    };
    let pool = Arc::new(ConnectionPool::new(cfg).expect("in-memory pool"));
    assert!(
        pool.open_standalone_writer().is_err(),
        "an in-memory pool must not be able to open a dedicated checkpoint connection"
    );
}

/// `CheckpointConnection::ensure_open` must open its dedicated connection
/// through the untracked standalone boundary, both on the initial open
/// and on a reopen after the connection is dropped — the checkpoint task
/// is exempt infrastructure, not a request-traffic writer acquisition
/// (ADR-136 D1 gate 5).
#[test]
fn ensure_open_does_not_move_writer_acquisition_counters() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("checkpoint_ensure_open.db");
    let pool = file_pool(&path);

    let before_first_open = pool.writer_acquisition_snapshot();
    let mut checkpoint_conn = CheckpointConnection::new();
    checkpoint_conn
        .ensure_open(&pool)
        .expect("dedicated checkpoint connection must open against a file-backed pool");
    assert_eq!(
        pool.writer_acquisition_snapshot(),
        before_first_open,
        "the checkpoint connection's initial open must not count as a writer acquisition"
    );

    checkpoint_conn.conn = None;
    let before_reopen = pool.writer_acquisition_snapshot();
    checkpoint_conn
        .ensure_open(&pool)
        .expect("dedicated checkpoint connection must reopen after invalidation");
    assert_eq!(
        pool.writer_acquisition_snapshot(),
        before_reopen,
        "reopening the checkpoint connection must not count as a writer acquisition either"
    );
}

#[test]
fn checkpoint_connection_disables_wal_autocheckpoint_on_open_and_reopen() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("checkpoint_autocheckpoint.db");
    let pool = file_pool(&path);
    let mut checkpoint_conn = CheckpointConnection::new();

    let initial: u32 = checkpoint_conn
        .ensure_open(&pool)
        .expect("dedicated checkpoint connection must open")
        .pragma_query_value(None, "wal_autocheckpoint", |row| row.get(0))
        .expect("read initial autocheckpoint setting");
    assert_eq!(initial, 0);

    checkpoint_conn.conn = None;
    let reopened: u32 = checkpoint_conn
        .ensure_open(&pool)
        .expect("dedicated checkpoint connection must reopen")
        .pragma_query_value(None, "wal_autocheckpoint", |row| row.get(0))
        .expect("read reopened autocheckpoint setting");
    assert_eq!(reopened, 0);
}

#[tokio::test(flavor = "current_thread")]
#[serial(checkpoint_skip_metrics)]
async fn failed_checkpoint_claim_keeps_existing_writer_task_on_fallback() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("failed_claim_writer_task.db");
    let pool = Arc::new(
        ConnectionPool::new(PoolConfig {
            path: Some(path),
            checkout_timeout: Duration::from_millis(1),
            write_queue_enabled: Some(true),
            ..PoolConfig::for_test()
        })
        .expect("pool open"),
    );
    let writer_task = pool
        .writer_task_handle()
        .expect("writer-task resolution")
        .expect("writer task enabled");
    assert_eq!(
        writer_task_wal_autocheckpoint_pages(&writer_task).await,
        crate::pool::FALLBACK_WAL_AUTOCHECKPOINT_PAGES
    );

    let (held_tx, held_rx) = tokio::sync::oneshot::channel();
    let (release_tx, release_rx) = std::sync::mpsc::channel();
    let holder_pool = Arc::clone(&pool);
    let holder = tokio::task::spawn_blocking(move || {
        let _held_writer = holder_pool.try_checkpoint_nowait().expect("claim writer");
        held_tx.send(()).expect("signal held pooled writer");
        release_rx.recv().expect("release held pooled writer");
    });
    held_rx.await.expect("pooled writer holder started");
    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    drop(shutdown_tx);
    run_checkpoint_task(
        Arc::clone(&pool),
        CheckpointConfig {
            interval: Duration::from_secs(60),
            ..CheckpointConfig::default()
        },
        None,
        shutdown_rx,
        true,
    )
    .await;

    assert_eq!(pool.writer_acquisition_snapshot().timeouts, 1);
    assert_eq!(
        writer_task_wal_autocheckpoint_pages(&writer_task).await,
        crate::pool::FALLBACK_WAL_AUTOCHECKPOINT_PAGES,
        "failed pooled-writer claim must not partially propagate ownership"
    );
    release_tx.send(()).expect("release pooled writer");
    holder.await.expect("pooled writer holder joined");
}

#[tokio::test]
#[serial(checkpoint_skip_metrics)]
async fn checkpoint_task_exits_on_shutdown_signal() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("wal_task_shutdown.db");
    let pool = file_pool(&path);

    // Use a very short interval so the task ticks quickly in the test.
    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        ..Default::default()
    };

    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(pool, cfg, None, shutdown_rx, true));

    shutdown_tx.send(()).expect("send shutdown signal");

    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should exit within 1s")
        .expect("checkpoint task panicked");
}

#[cfg(unix)]
#[tokio::test]
#[serial(checkpoint_skip_metrics, khive_walpin_sidecar_env)]
async fn healthy_checkpoint_tick_reaps_a_dead_walpin_beacon_without_truncate() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("healthy_sidecar_reap.db");
    let pool = file_pool(&path);
    let sidecar_dir =
        crate::walpin::sidecar_dir_for(pool.canonical_path().expect("file-backed pool"));

    let dead_pid = 2_000_000_000;
    let dead_beacon = crate::walpin::WalpinBeacon {
        pid: dead_pid,
        process_role: "session".to_string(),
        started_at: 1,
        sweep_interval_ms: 5_000,
    };
    crate::walpin::write_beacon(&sidecar_dir, &dead_beacon)
        .expect("seed a crashed process's orphan beacon");
    let dead_beacon_path = crate::walpin::beacon_path(&sidecar_dir, dead_pid);
    assert!(dead_beacon_path.exists(), "orphan fixture must exist");

    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        warn_pages: u64::MAX,
        high_water_pages: u64::MAX,
        truncate_high_water_pages: u64::MAX,
        ..CheckpointConfig::default()
    };
    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(pool, cfg, None, shutdown_rx, true));

    let reaped = wait_for(Duration::from_secs(2), || !dead_beacon_path.exists()).await;
    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should exit within 1s")
        .expect("checkpoint task panicked");

    assert!(
        reaped,
        "the ordinary healthy tick must reap positively dead sidecar residue independently of \
             TRUNCATE diagnostics"
    );
}

/// Regression #774: exits via watch-signal even with a live event_store
/// pool clone (rules out a strong-count-based exit condition). See
/// crates/khive-db/docs/api/checkpoint.md#checkpoint_task_exits_via_shutdown_signal_with_live_event_store_pool_clone
#[tokio::test]
#[serial(checkpoint_skip_metrics)]
async fn checkpoint_task_exits_via_shutdown_signal_with_live_event_store_pool_clone() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("wal_task_event_store.db");
    let pool = file_pool(&path);

    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        ..Default::default()
    };

    let event_store: Arc<dyn khive_storage::EventStore> =
        Arc::new(crate::stores::event::SqlEventStore::new_scoped(
            Arc::clone(&pool),
            true,
            "local".to_string(),
        ));
    // A second, independent sibling clone of `pool` outlives this test
    // function's own binding — mirrors `StorageBackend` retaining
    // `self.pool` alongside the `SqlEventStore` it hands to the
    // checkpoint task in production.
    let sibling_pool_clone = Arc::clone(&pool);

    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(
        pool,
        cfg,
        Some(CheckpointLifecycleOwner::new(event_store, "local")),
        shutdown_rx,
        true,
    ));

    // Confirm strong_count is well above 1 — the old check would spin
    // forever here — before proving the new signal-based exit works
    // regardless.
    assert!(
        Arc::strong_count(&sibling_pool_clone) > 1,
        "test setup must reproduce the multi-owner shape the bug depends on"
    );

    shutdown_tx.send(()).expect("send shutdown signal");

    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect(
            "checkpoint task should exit within 1s via the watch signal, \
                 even with a live sibling Arc<ConnectionPool> clone held by \
                 the event store",
        )
        .expect("checkpoint task panicked");
}

/// Regression: a high-water tick must NOT block behind an active read
/// transaction (isomorphism guarantee — fails if `checkpoint_once`
/// regresses to TRUNCATE). See
/// crates/khive-db/docs/api/checkpoint.md#checkpoint_high_water_does_not_block_behind_reader
#[test]
#[serial(checkpoint_skip_metrics)]
fn checkpoint_high_water_does_not_block_behind_reader() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("high_water_test.db");

    // busy_timeout = 2000ms: a TRUNCATE regression blocks ~2s, while the
    // assertion below uses the midpoint of that gap instead of a noise-floor
    // threshold. The checkpoint config carries the same 2000ms default.
    let pool = Arc::new(
        ConnectionPool::new(PoolConfig {
            path: Some(path.clone()),
            busy_timeout: Duration::from_millis(2000),
            ..PoolConfig::for_test()
        })
        .expect("pool open"),
    );

    // Write data so the WAL has frames to checkpoint.
    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE IF NOT EXISTS t (x INTEGER); INSERT INTO t VALUES (1);")
            .unwrap();
    }

    // Open a reader and start a real read transaction so it holds a WAL
    // snapshot. An idle connection (no BEGIN) does NOT pin frames and would
    // not cause TRUNCATE to wait — the transaction is required for isomorphism.
    let reader = pool.reader().expect("reader");
    reader
        .conn()
        .execute_batch("BEGIN DEFERRED; SELECT * FROM t;")
        .expect("begin read tx");

    // Write another row AFTER the snapshot is established. These new WAL
    // frames are now pinned by the open reader snapshot — TRUNCATE cannot
    // reclaim them without waiting; PASSIVE skips them and returns immediately.
    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("INSERT INTO t VALUES (2);")
            .unwrap();
    }

    let checkpoint_config = CheckpointConfig::default();
    let conn = checkpoint_conn(&pool);
    let start = std::time::Instant::now();
    checkpoint_once(
        &pool,
        &conn,
        &checkpoint_config,
        &mut TruncateState::default(),
    )
    .expect("checkpoint_once must succeed against a healthy dedicated connection");
    let elapsed = start.elapsed();

    // Commit and release the read snapshot only after checkpoint_once returns.
    reader.conn().execute_batch("COMMIT;").ok();
    drop(reader);

    // PASSIVE returns without waiting for the reader snapshot. A TRUNCATE
    // regression would block for the configured busy timeout (2000ms), so
    // use the midpoint of that gap rather than a noise-floor threshold.
    let max_elapsed = checkpoint_config.truncate_busy_timeout / 2;
    assert!(
        elapsed < max_elapsed,
        "checkpoint_once with active reader snapshot took {:?}; expected <{:?} \
             (PASSIVE must not block on readers; a TRUNCATE regression would block \
             for the configured {:?})",
        elapsed,
        max_elapsed,
        checkpoint_config.truncate_busy_timeout
    );
}

/// Regression: a Skipped tick must NOT reset `was_above_high_water`. See
/// crates/khive-db/docs/api/checkpoint.md#skipped_tick_does_not_reset_high_water_crossing_state
#[test]
fn skipped_tick_does_not_reset_high_water_crossing_state() {
    let mut was_above = false;

    // First observed tick: above threshold — fires WARN, sets was_above=true.
    assert!(
        crossing_warn(true, &mut was_above),
        "should fire on first crossing"
    );
    assert!(was_above);

    // Simulate several skipped ticks: crossing state must remain true.
    // (In the task, Skipped causes `continue` so crossing_warn is never called.)
    // We verify by calling crossing_warn with the SAME above=true value, which
    // is what Observed(high_count) would produce — but a Skipped tick skips
    // the call entirely, so was_above stays as-is. Test the invariant directly:
    // if we leave was_above unchanged (no call at all), was_above remains true.
    assert!(was_above, "was_above must stay true across skipped ticks");

    // Another observed tick still above threshold — must NOT re-fire.
    let fired = crossing_warn(true, &mut was_above);
    assert!(!fired, "WARN must not re-fire while still above threshold");

    // Observed tick below threshold — resets was_above.
    let fired = crossing_warn(false, &mut was_above);
    assert!(!fired);
    assert!(!was_above);

    // Next observed tick above threshold — fires again (legitimate new crossing).
    let fired = crossing_warn(true, &mut was_above);
    assert!(fired, "WARN must fire again on a new below→above crossing");
}

/// Regression: warn_pages WARN fires once on crossing, not every tick.
///
/// Before the fix, the WARN was emitted inside `checkpoint_once` on every tick
/// while WAL sat in the warn band — log spam under sustained moderate pressure.
/// With the fix, `crossing_warn` gates the WARN on the first in-band tick only;
/// subsequent ticks while still in the band return false.
#[test]
fn warn_pages_fires_once_on_crossing_not_every_tick() {
    let mut was_above_warn = false;

    // Simulate three consecutive ticks with WAL in the warn band.
    let fired_1 = crossing_warn(true, &mut was_above_warn);
    let fired_2 = crossing_warn(true, &mut was_above_warn);
    let fired_3 = crossing_warn(true, &mut was_above_warn);

    assert!(fired_1, "WARN must fire on the first in-band tick");
    assert!(
        !fired_2,
        "WARN must not fire on the second consecutive in-band tick"
    );
    assert!(
        !fired_3,
        "WARN must not fire on the third consecutive in-band tick"
    );

    // Drop below warn band — resets state.
    crossing_warn(false, &mut was_above_warn);
    assert!(!was_above_warn);

    // Re-enter warn band — fires again.
    let fired_reentry = crossing_warn(true, &mut was_above_warn);
    assert!(
        fired_reentry,
        "WARN must fire again on re-entry into warn band"
    );
}

// ADR-091 Plank 2: TRUNCATE escalation state machine tests.

/// Trigger threshold: once `wal_pages` (as observed by `checkpoint_once`) is
/// at/above `truncate_high_water_pages` and no prior attempt has run, the
/// escalation fires and stamps `last_attempt`.
#[test]
#[serial(tx_registry, checkpoint_skip_metrics)]
fn truncate_attempts_when_high_water_crossed_with_no_prior_attempt() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("truncate_trigger.db");
    let pool = file_pool(&path);

    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE IF NOT EXISTS t (x INTEGER); INSERT INTO t VALUES (1);")
            .unwrap();
    }

    let config = CheckpointConfig {
        // Force the escalation to arm regardless of the tiny WAL this test
        // actually produces — isolates the trigger-threshold behavior from
        // needing to stuff 20,000 real WAL pages.
        truncate_high_water_pages: 0,
        truncate_min_interval: Duration::from_secs(300),
        ..CheckpointConfig::default()
    };
    let mut state = TruncateState::default();

    assert!(
        state.last_attempt.is_none(),
        "precondition: no attempt has run yet"
    );

    let conn = checkpoint_conn(&pool);
    checkpoint_once(&pool, &conn, &config, &mut state)
        .expect("checkpoint_once must succeed against a healthy dedicated connection");
    assert!(
        state.last_attempt.is_some(),
        "an attempt must be stamped once the high-water threshold is crossed"
    );
}

/// Below-threshold skip: `wal_pages < truncate_high_water_pages` must never
/// stamp `last_attempt` — only an actual attempt advances it.
#[test]
#[serial(tx_registry, checkpoint_skip_metrics)]
fn truncate_does_not_attempt_below_high_water() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("truncate_below_threshold.db");
    let pool = file_pool(&path);

    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE IF NOT EXISTS t (x INTEGER); INSERT INTO t VALUES (1);")
            .unwrap();
    }

    // Effectively unreachable threshold for this test's tiny WAL.
    let config = CheckpointConfig {
        truncate_high_water_pages: u64::MAX,
        ..CheckpointConfig::default()
    };
    let mut state = TruncateState::default();

    let conn = checkpoint_conn(&pool);
    checkpoint_once(&pool, &conn, &config, &mut state)
        .expect("checkpoint_once must succeed against a healthy dedicated connection");

    assert!(
        state.last_attempt.is_none(),
        "a below-threshold tick must never stamp last_attempt"
    );
}

/// Min-interval skip: once an attempt has run, a subsequent tick that is
/// still above threshold but within `truncate_min_interval` must skip
/// without re-stamping `last_attempt` (the timestamp must not move).
#[test]
#[serial(tx_registry, checkpoint_skip_metrics)]
fn truncate_min_interval_skip_does_not_restamp_last_attempt() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("truncate_min_interval.db");
    let pool = file_pool(&path);

    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE IF NOT EXISTS t (x INTEGER); INSERT INTO t VALUES (1);")
            .unwrap();
    }

    let config = CheckpointConfig {
        truncate_high_water_pages: 0,
        truncate_min_interval: Duration::from_secs(300),
        ..CheckpointConfig::default()
    };
    let mut state = TruncateState::default();
    let conn = checkpoint_conn(&pool);

    checkpoint_once(&pool, &conn, &config, &mut state)
        .expect("checkpoint_once must succeed against a healthy dedicated connection");
    let first_attempt = state.last_attempt.expect("first tick must attempt");

    // Second tick, immediately after, on the SAME dedicated connection
    // (mirroring how `run_checkpoint_task` reuses one connection across
    // ticks): still above threshold, but the min-interval has clearly
    // not elapsed — must skip and leave last_attempt exactly as it was.
    checkpoint_once(&pool, &conn, &config, &mut state)
        .expect("checkpoint_once must succeed against a healthy dedicated connection");
    let second_attempt = state.last_attempt.expect("attempt timestamp must persist");

    assert_eq!(
        first_attempt, second_attempt,
        "a tick within truncate_min_interval must not re-stamp last_attempt"
    );
}

/// The fix this module exists to prove: holding the POOL's writer mutex
/// (via `pool.try_writer()`, exactly like a concurrent write in
/// progress) must NOT cause `checkpoint_once` to skip — PASSIVE (and, if
/// armed, TRUNCATE) run on the task's own dedicated connection, which
/// never contends with the pool writer at all. Before the dedicated-
/// connection fix, this same setup made `checkpoint_once` return
/// `Skipped` via `try_writer_nowait()`. See also the standalone
/// integration reproducer in `tests/checkpoint_dedicated_connection.rs`,
/// which demonstrates the converse: a fat WAL held busy by
/// `checkpoint_once` no longer blocks a concurrent `pool.writer()`
/// admission either.
#[test]
#[serial(tx_registry, checkpoint_skip_metrics)]
fn checkpoint_once_proceeds_and_can_attempt_truncate_while_pool_writer_held() {
    reset_checkpoint_metrics_for_tests();

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("truncate_busy_skip.db");
    let pool = file_pool(&path);

    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE IF NOT EXISTS t (x INTEGER); INSERT INTO t VALUES (1);")
            .unwrap();
    }

    let conn = checkpoint_conn(&pool);

    // Hold the POOL's writer mutex for the duration of the checkpoint_once
    // call, acquired BEFORE the call so the dedicated connection cannot
    // possibly race a still-free writer.
    let _held = pool.try_writer().unwrap();

    let config = CheckpointConfig {
        truncate_high_water_pages: 0,
        ..CheckpointConfig::default()
    };
    let mut state = TruncateState::default();

    checkpoint_once(&pool, &conn, &config, &mut state).expect(
        "checkpoint_once must observe normally on its own dedicated connection even \
             while a concurrent caller holds the pool's writer mutex",
    );

    assert!(
        state.last_attempt.is_some(),
        "a threshold-armed tick must still evaluate (and attempt) TRUNCATE even while \
             the pool writer is held — the dedicated connection is unaffected by it"
    );
    assert_eq!(
        checkpoint_skipped_ticks(),
        0,
        "a busy pool writer must no longer count as a skipped checkpoint tick"
    );
    assert_eq!(
        checkpoint_consecutive_skips(),
        0,
        "a busy pool writer must not bump the consecutive-skip run length"
    );
}

/// Regression guard for #845 (a recurrence of the #828 shared-statics
/// race): every test in this module that calls `checkpoint_once`,
/// `checkpoint_once_core`, or `run_checkpoint_task` — all funnel through
/// `query_wal_pages`, which
/// writes the process-wide `LAST_WAL_PAGES` / `CHECKPOINT_*` atomics —
/// must be tagged with a `#[serial(...)]` group that includes
/// `checkpoint_skip_metrics`. Before #828, six such call sites carried no
/// serial tag at all: cargo's default test thread pool ran them
/// concurrently with `busy_writer_skips_both_passive_and_truncate`, and an
/// untagged tick's `query_wal_pages` call clobbered the gauges between
/// this test's warmup tick and its skip assertion (`left: Some(0), right:
/// Some(3)` on CI — the two ticks never actually raced against each
/// other, a third test's tick did). This scans the module's own source so
/// a future test that calls either function without the tag fails this
/// assertion instead of flaking on a loaded CI runner.
#[test]
#[serial(checkpoint_skip_metrics)]
fn all_checkpoint_metrics_callers_are_serial_tagged() {
    let sources = [
        include_str!("checkpoint.rs"),
        include_str!("checkpoint_tests.rs"),
        include_str!("checkpoint/churn_tests.rs"),
        include_str!("checkpoint_owner_interval_tests.rs"),
    ]
    .join("\n");
    let lines: Vec<&str> = sources.lines().collect();

    let attr_starts: Vec<usize> = lines
        .iter()
        .enumerate()
        .filter(|(_, l)| {
            let t = l.trim();
            t == "#[test]" || t.starts_with("#[tokio::test")
        })
        .map(|(i, _)| i)
        .collect();

    let mut offenders = Vec::new();

    for (idx, &start) in attr_starts.iter().enumerate() {
        let end = attr_starts.get(idx + 1).copied().unwrap_or(lines.len());
        let span = &lines[start..end];

        let touches_shared_metrics = span.iter().any(|l| {
            l.contains("checkpoint_once(")
                || l.contains("checkpoint_once_core(")
                || l.contains("run_checkpoint_task(")
        });
        if !touches_shared_metrics {
            continue;
        }

        // Rustfmt splits long multi-key attributes across lines, so scan
        // the whole attribute instead of requiring the group on `#[serial(`.
        let mut in_serial_attr = false;
        let has_group_tag = span.iter().any(|line| {
            let trimmed = line.trim();
            if !in_serial_attr {
                in_serial_attr = trimmed.starts_with("#[serial(");
            }
            if !in_serial_attr {
                return false;
            }

            let has_group = trimmed
                .split(|ch: char| !ch.is_ascii_alphanumeric() && ch != '_')
                .any(|token| token == "checkpoint_skip_metrics");
            if trimmed.ends_with(")]") {
                in_serial_attr = false;
            }
            has_group
        });

        if !has_group_tag {
            let name = span
                .iter()
                .find_map(|l| {
                    let t = l.trim_start();
                    let t = t.strip_prefix("pub(crate) ").unwrap_or(t);
                    let t = t.strip_prefix("pub ").unwrap_or(t);
                    let t = t.strip_prefix("async ").unwrap_or(t);
                    t.strip_prefix("fn ")
                        .map(|rest| rest.split(['(', '<']).next().unwrap_or("").trim())
                })
                .unwrap_or("<unknown test>");
            offenders.push(name.to_string());
        }
    }

    assert!(
        offenders.is_empty(),
        "these tests call checkpoint_once/checkpoint_once_core/run_checkpoint_task (which write the \
             process-wide LAST_WAL_PAGES/CHECKPOINT_* atomics via query_wal_pages) but \
             are not tagged #[serial(checkpoint_skip_metrics)] (or a group including it); \
             an untagged caller running concurrently on cargo's default test thread pool \
             can clobber those atomics mid-assertion in another test (the #828/#845 race): \
             {offenders:?}"
    );
}

/// Observation branch: a checkpoint tick that is actually observed
/// (dedicated connection available) must close out a prior skip streak,
/// resetting the consecutive-skip counter to 0 without touching the
/// lifetime total. Drives `note_checkpoint_skipped()` directly for the
/// skipped ticks — exactly what `run_checkpoint_task` calls when
/// `CheckpointConnection::ensure_open` returns `None` — rather than
/// through pool-writer contention, which (since the dedicated-connection
/// fix) no longer produces a skipped tick at all.
#[test]
#[serial(tx_registry, checkpoint_skip_metrics)]
fn observed_tick_resets_consecutive_skips_but_not_lifetime_total() {
    reset_checkpoint_metrics_for_tests();

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("skip_then_observe.db");
    let pool = file_pool(&path);

    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE IF NOT EXISTS t (x INTEGER); INSERT INTO t VALUES (1);")
            .unwrap();
    }

    // Two consecutive skipped ticks (dedicated connection unavailable).
    note_checkpoint_skipped();
    note_checkpoint_skipped();
    assert_eq!(checkpoint_skipped_ticks(), 2);
    assert_eq!(checkpoint_consecutive_skips(), 2);

    // Now the dedicated connection is available: an observed tick must
    // reset the streak.
    let conn = checkpoint_conn(&pool);
    let mut state = TruncateState::default();
    checkpoint_once(&pool, &conn, &CheckpointConfig::default(), &mut state)
        .expect("checkpoint_once must succeed against a healthy dedicated connection");

    assert_eq!(
        checkpoint_skipped_ticks(),
        2,
        "an observed tick must not change the lifetime skipped-tick total"
    );
    assert_eq!(
        checkpoint_consecutive_skips(),
        0,
        "an observed tick must reset the consecutive-skip run length"
    );
}

/// Edge-triggered escalation WARN: `note_truncate_outcome` fires exactly
/// once, on the third consecutive attempt that fails to clear
/// `warn_pages`, and does not repeat on a fourth consecutive failure. A
/// single attempt that clears `warn_pages` resets the counter.
#[test]
#[serial(checkpoint_skip_metrics)]
fn note_truncate_outcome_warns_once_at_third_consecutive_failure() {
    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };

    let config = CheckpointConfig {
        warn_pages: 2000,
        ..CheckpointConfig::default()
    };
    let mut state = TruncateState::default();

    tracing::subscriber::with_default(subscriber, || {
        // Three consecutive attempts that fail to clear warn_pages.
        note_truncate_outcome(&config, Some(5000), &mut state);
        note_truncate_outcome(&config, Some(5000), &mut state);
        note_truncate_outcome(&config, Some(5000), &mut state);
        // A fourth consecutive failure must not re-fire the escalation.
        note_truncate_outcome(&config, Some(5000), &mut state);
    });

    assert_eq!(state.consecutive_failures, 4);

    let events = buffer.lock().unwrap();
    let escalation_count = events
        .iter()
        .filter(|e| {
            e.message.as_deref()
                == Some("WAL TRUNCATE has failed to clear WAL pressure for 3 consecutive attempts")
        })
        .count();
    assert_eq!(
        escalation_count, 1,
        "escalation WARN must fire exactly once at the 3rd consecutive failure, got: {events:?}"
    );

    // A clearing attempt resets the counter.
    note_truncate_outcome(&config, Some(100), &mut state);
    assert_eq!(
        state.consecutive_failures, 0,
        "an attempt that clears warn_pages must reset the consecutive-failure counter"
    );
}

// ADR-091 #617: graduated severity ladder state-machine tests.

fn severity_test_config() -> CheckpointConfig {
    CheckpointConfig {
        warn_pages: 100,
        warn_sustained_cycles: 3,
        ..CheckpointConfig::default()
    }
}

/// INFO rung: a below→above crossing emits exactly one INFO and no WARN
/// (default `warn_sustained_cycles = 3`, only one above-warn tick here).
#[test]
fn severity_ladder_info_on_first_crossing_no_warn() {
    let config = severity_test_config();
    let mut state = CheckpointSeverityState::default();

    let below = state.observe_wal_pages(10, &config);
    assert!(below.is_empty(), "below-warn tick must emit nothing");

    let above = state.observe_wal_pages(150, &config);
    assert_eq!(
        above,
        vec![CheckpointSeverityEmission {
            rung: CheckpointSeverityRung::Info,
            wal_pages: 150,
            threshold_pages: 100,
            consecutive_cycles: 1,
        }],
        "first below->above crossing must emit exactly one INFO and no WARN"
    );
}

/// WARN rung: `warn_sustained_cycles` (3) consecutive above-warn ticks
/// emit WARN exactly on the third tick, not before and not repeated after.
#[test]
fn severity_ladder_warn_on_third_consecutive_cycle() {
    let config = severity_test_config();
    let mut state = CheckpointSeverityState::default();

    let tick1 = state.observe_wal_pages(150, &config);
    assert_eq!(tick1.len(), 1);
    assert_eq!(tick1[0].rung, CheckpointSeverityRung::Info);

    let tick2 = state.observe_wal_pages(150, &config);
    assert!(
        tick2.is_empty(),
        "second consecutive above-warn tick must emit nothing yet"
    );

    let tick3 = state.observe_wal_pages(150, &config);
    assert_eq!(
        tick3,
        vec![CheckpointSeverityEmission {
            rung: CheckpointSeverityRung::Warn,
            wal_pages: 150,
            threshold_pages: 100,
            consecutive_cycles: 3,
        }],
        "WARN must fire exactly on the third consecutive above-warn tick"
    );

    let tick4 = state.observe_wal_pages(150, &config);
    assert!(
        tick4.is_empty(),
        "WARN must not repeat on a fourth consecutive above-warn tick"
    );
}

/// Re-arm: after a WARN episode drains below warn_pages, a fresh episode
/// of `warn_sustained_cycles` above-warn ticks must WARN again.
#[test]
fn severity_ladder_rearms_warn_after_drain() {
    let config = severity_test_config();
    let mut state = CheckpointSeverityState::default();

    // First episode reaches WARN.
    for _ in 0..3 {
        state.observe_wal_pages(150, &config);
    }
    assert!(state.warn_emitted_for_episode);

    // Drain below warn_pages: resets the episode.
    let drain = state.observe_wal_pages(10, &config);
    assert!(drain.is_empty(), "a draining tick must emit nothing");

    // Second episode: INFO on first tick, no WARN until the third again.
    let reentry = state.observe_wal_pages(150, &config);
    assert_eq!(reentry.len(), 1);
    assert_eq!(reentry[0].rung, CheckpointSeverityRung::Info);

    let mid = state.observe_wal_pages(150, &config);
    assert!(mid.is_empty());

    let second_warn = state.observe_wal_pages(150, &config);
    assert_eq!(
        second_warn,
        vec![CheckpointSeverityEmission {
            rung: CheckpointSeverityRung::Warn,
            wal_pages: 150,
            threshold_pages: 100,
            consecutive_cycles: 3,
        }],
        "a fresh elevation episode after a drain must WARN again"
    );
}

/// False-positive guard: three isolated single-tick crossings, each
/// followed by a drain, must never reach WARN — only INFO fires each time.
#[test]
fn severity_ladder_isolated_crossings_never_warn() {
    let config = severity_test_config();
    let mut state = CheckpointSeverityState::default();

    for _ in 0..3 {
        let crossing = state.observe_wal_pages(150, &config);
        assert_eq!(
            crossing.len(),
            1,
            "each isolated crossing must emit exactly one INFO"
        );
        assert_eq!(crossing[0].rung, CheckpointSeverityRung::Info);

        let drain = state.observe_wal_pages(10, &config);
        assert!(drain.is_empty(), "the drain tick must emit nothing");
    }

    assert!(
        !state.warn_emitted_for_episode,
        "isolated single-tick crossings must never accumulate into a WARN"
    );
}

/// ALARM rung: the existing TRUNCATE-attempt gate is the ADR-091 ALARM
/// tier. `observe_wal_pages` never produces it; this test documents and
/// locks in that boundary so a future change can't silently reroute
/// ALARM through the INFO/WARN ladder.
#[test]
fn severity_ladder_never_emits_alarm() {
    let config = CheckpointConfig {
        warn_pages: 100,
        warn_sustained_cycles: 1,
        ..CheckpointConfig::default()
    };
    let mut state = CheckpointSeverityState::default();

    for wal_pages in [150, 200, 250, u64::MAX] {
        let emissions = state.observe_wal_pages(wal_pages, &config);
        assert!(
            emissions
                .iter()
                .all(|e| e.rung != CheckpointSeverityRung::Alarm),
            "observe_wal_pages must never emit the ALARM rung, got: {emissions:?}"
        );
    }
}

// ADR-091 Plank 1: `TxAgeSweepState` background-sweep state-machine tests.
// Pure unit tests mirroring the severity-ladder tests above — no I/O.

fn tx_age_test_config() -> CheckpointConfig {
    CheckpointConfig {
        tx_warn_secs: Duration::from_secs(30),
        tx_max_age_secs: Duration::from_secs(120),
        ..CheckpointConfig::default()
    }
}

/// Synthetic identity for `TxAgeSweepState::observe`'s pure unit tests
/// below, which exercise identity-change detection without paying for a
/// real `tx_registry::register` call. `TxId`'s wrapped value is public
/// exactly to support this (see its doc comment in `khive-storage`).
fn tx_id(n: u64) -> khive_storage::tx_registry::TxId {
    khive_storage::tx_registry::TxId(n)
}

/// No open entry: nothing fires, and any prior latch state clears.
#[test]
fn tx_age_sweep_empty_registry_emits_nothing() {
    let config = tx_age_test_config();
    let mut state = TxAgeSweepState::default();

    let emissions = state.observe(None, config.tx_warn_secs, config.tx_max_age_secs);
    assert!(emissions.is_empty(), "no open entry must emit nothing");
}

/// A fresh entry (age below both thresholds) emits nothing.
#[test]
fn tx_age_sweep_fresh_entry_emits_nothing() {
    let config = tx_age_test_config();
    let mut state = TxAgeSweepState::default();

    let emissions = state.observe(
        Some((
            tx_id(1),
            Duration::from_secs(5),
            Some("fresh_span".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert!(emissions.is_empty(), "a fresh entry must emit nothing");
}

/// Below→above crossing of `tx_warn_secs` fires exactly one `Warn`
/// emission carrying the entry's label; it must not repeat on a second
/// tick that is still above `tx_warn_secs` but below `tx_max_age_secs`.
#[test]
fn tx_age_sweep_warn_fires_once_on_crossing() {
    let config = tx_age_test_config();
    let mut state = TxAgeSweepState::default();

    let tick1 = state.observe(
        Some((
            tx_id(1),
            Duration::from_secs(45),
            Some("stale_span".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert_eq!(
        tick1,
        vec![TxAgeEmission {
            rung: TxAgeRung::Warn,
            age: Duration::from_secs(45),
            label: Some("stale_span".to_string()),
        }],
        "crossing tx_warn_secs must emit exactly one Warn"
    );

    let tick2 = state.observe(
        Some((
            tx_id(1),
            Duration::from_secs(50),
            Some("stale_span".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert!(
        tick2.is_empty(),
        "Warn must not repeat while the entry stays in the warn band"
    );
}

/// Crossing `tx_max_age_secs` fires `Stale`; a further tick still above
/// the cap must not repeat it.
#[test]
fn tx_age_sweep_stale_fires_once_on_crossing() {
    let config = tx_age_test_config();
    let mut state = TxAgeSweepState::default();

    // Drive through the warn crossing first, matching real elapsed-time
    // progression (an entry ages through the warn band before the max).
    state.observe(
        Some((
            tx_id(1),
            Duration::from_secs(45),
            Some("stuck_writer_task_tx".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );

    let tick = state.observe(
        Some((
            tx_id(1),
            Duration::from_secs(130),
            Some("stuck_writer_task_tx".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert_eq!(
        tick,
        vec![TxAgeEmission {
            rung: TxAgeRung::Stale,
            age: Duration::from_secs(130),
            label: Some("stuck_writer_task_tx".to_string()),
        }],
        "crossing tx_max_age_secs must emit exactly one Stale"
    );

    let tick_repeat = state.observe(
        Some((
            tx_id(1),
            Duration::from_secs(200),
            Some("stuck_writer_task_tx".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert!(
        tick_repeat.is_empty(),
        "Stale must not repeat while the entry stays above tx_max_age_secs"
    );
}

/// An entry already stale the first time the sweep observes it (e.g.
/// right after process start with a pre-existing registry entry) crosses
/// both rungs on the same tick.
#[test]
fn tx_age_sweep_already_stale_entry_emits_both_rungs_same_tick() {
    let config = tx_age_test_config();
    let mut state = TxAgeSweepState::default();

    let tick = state.observe(
        Some((
            tx_id(1),
            Duration::from_secs(300),
            Some("ancient_tx".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert_eq!(
        tick,
        vec![
            TxAgeEmission {
                rung: TxAgeRung::Warn,
                age: Duration::from_secs(300),
                label: Some("ancient_tx".to_string()),
            },
            TxAgeEmission {
                rung: TxAgeRung::Stale,
                age: Duration::from_secs(300),
                label: Some("ancient_tx".to_string()),
            },
        ],
        "an already-stale entry must cross both rungs on its first observed tick"
    );
}

/// Re-arm: once the stale entry closes (registry reports a fresher
/// oldest entry, or none at all), a future stale span must fire again.
#[test]
fn tx_age_sweep_rearms_after_entry_clears() {
    let config = tx_age_test_config();
    let mut state = TxAgeSweepState::default();

    state.observe(
        Some((
            tx_id(1),
            Duration::from_secs(150),
            Some("first_span".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );

    // The stale span closed; nothing is open now.
    let cleared = state.observe(None, config.tx_warn_secs, config.tx_max_age_secs);
    assert!(cleared.is_empty(), "a clearing tick must emit nothing");

    // A fresh entry (unrelated span) is now oldest — still below threshold.
    let fresh = state.observe(
        Some((
            tx_id(2),
            Duration::from_secs(2),
            Some("second_span".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert!(fresh.is_empty(), "a fresh oldest entry must emit nothing");

    // That second span goes stale in turn — must WARN again (re-armed).
    let rewarn = state.observe(
        Some((
            tx_id(2),
            Duration::from_secs(35),
            Some("second_span".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert_eq!(
        rewarn,
        vec![TxAgeEmission {
            rung: TxAgeRung::Warn,
            age: Duration::from_secs(35),
            label: Some("second_span".to_string()),
        }],
        "a fresh stale episode after a clear must Warn again"
    );
}

/// Fix: an already-stale entry replacing a stale one on the next tick,
/// with no intervening clear, must still emit both rungs. See
/// crates/khive-db/docs/api/checkpoint.md#tx_age_sweep_stale_replacement_without_intervening_clear_still_names_new_entry
#[test]
fn tx_age_sweep_stale_replacement_without_intervening_clear_still_names_new_entry() {
    let config = tx_age_test_config();
    let mut state = TxAgeSweepState::default();

    let tick_a = state.observe(
        Some((
            tx_id(1),
            Duration::from_secs(300),
            Some("stale_entry_a".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert_eq!(
        tick_a.len(),
        2,
        "entry A must cross both rungs on its first observed tick, got: {tick_a:?}"
    );

    // B replaces A as the oldest entry on the VERY NEXT tick — already
    // stale itself, with no intervening None/below-threshold tick.
    let tick_b = state.observe(
        Some((
            tx_id(2),
            Duration::from_secs(400),
            Some("stale_entry_b".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert_eq!(
        tick_b,
        vec![
            TxAgeEmission {
                rung: TxAgeRung::Warn,
                age: Duration::from_secs(400),
                label: Some("stale_entry_b".to_string()),
            },
            TxAgeEmission {
                rung: TxAgeRung::Stale,
                age: Duration::from_secs(400),
                label: Some("stale_entry_b".to_string()),
            },
        ],
        "a same-tick identity change to an already-stale successor must re-emit both \
             rungs naming the NEW entry, got: {tick_b:?}"
    );
}

/// Closes the loop from env var to actual emitted rung. See
/// crates/khive-db/docs/api/checkpoint.md#tx_age_sweep_uses_configured_thresholds_not_hardcoded_defaults
#[test]
fn tx_age_sweep_uses_configured_thresholds_not_hardcoded_defaults() {
    let config = CheckpointConfig {
        tx_warn_secs: Duration::from_millis(1),
        tx_max_age_secs: Duration::from_millis(2),
        ..CheckpointConfig::default()
    };
    let mut state = TxAgeSweepState::default();

    let tick = state.observe(
        Some((
            tx_id(1),
            Duration::from_millis(5),
            Some("fast_cap_span".to_string()),
        )),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert_eq!(
        tick.len(),
        2,
        "a millisecond-scale cap must cross both rungs immediately, got: {tick:?}"
    );
}

/// Integration-level regression for the incident this ADR fixes. See
/// crates/khive-db/docs/api/checkpoint.md#tx_age_sweep_names_long_lived_reader_pinning_wal_past_high_water
#[test]
#[serial(tx_registry, checkpoint_skip_metrics)]
fn tx_age_sweep_names_long_lived_reader_pinning_wal_past_high_water() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("tx_age_sweep_reader_pin.db");
    let pool = file_pool(&path);

    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE IF NOT EXISTS t (x INTEGER); INSERT INTO t VALUES (1);")
            .unwrap();
    }

    // Open a real read transaction so it holds a WAL snapshot (same
    // isomorphism as `checkpoint_high_water_does_not_block_behind_reader`),
    // AND register it in tx_registry — the telemetry a real long-lived
    // reader call site (e.g. `graph_traverse_read`) is expected to carry.
    let reader = pool.reader().expect("reader");
    reader
        .conn()
        .execute_batch("BEGIN DEFERRED; SELECT * FROM t;")
        .expect("begin read tx");
    let _tx_handle =
        khive_storage::tx_registry::register(Some("tx_age_sweep_reader_pin_test".to_string()));

    // Drive writes past high_water_pages while the reader snapshot pins
    // the WAL tail — PASSIVE cannot reclaim these frames.
    let config = CheckpointConfig {
        high_water_pages: 1,
        tx_warn_secs: Duration::from_millis(1),
        tx_max_age_secs: Duration::from_millis(1),
        ..CheckpointConfig::default()
    };
    {
        let writer = pool.try_writer().unwrap();
        for i in 0..50 {
            writer
                .conn()
                .execute_batch(&format!("INSERT INTO t VALUES ({i});"))
                .unwrap();
        }
    }

    let conn = checkpoint_conn(&pool);
    let wal_pages = checkpoint_once(&pool, &conn, &config, &mut TruncateState::default())
        .expect("checkpoint_once must succeed against a healthy dedicated connection");
    assert!(
        wal_pages >= config.high_water_pages,
        "test setup must actually drive wal_pages ({wal_pages}) past high_water_pages \
             ({}) for this regression to mean anything",
        config.high_water_pages
    );

    // The Plank 1 sweep, given the SAME registry state, must name the
    // pinning reader at the Stale rung. The handle's age must exceed the
    // 1ms `tx_max_age_secs` cap deterministically: the inserts plus one
    // PASSIVE checkpoint above can complete in under a millisecond on a
    // warm page cache, so sleep past the cap instead of assuming
    // the elapsed work already crossed it.
    std::thread::sleep(Duration::from_millis(5));
    // `tx_registry` is a process-wide singleton shared by every test in
    // this binary (cargo runs `#[test]`s in parallel threads of the same
    // process): `#[serial(tx_registry)]` only excludes other tests that
    // carry the same key, not every production write path elsewhere in
    // the crate (e.g. `graph_upsert_edges`) that also calls `register()`
    // as ordinary telemetry. If one of those happens to still be open and
    // was registered before this test's own handle, raw `oldest()` would
    // return THAT entry instead of the fixture's reader — see #926. Look
    // up this test's own entry by its known label instead of trusting
    // global `oldest()`, so the assertion is immune to that noise.
    let our_entry = khive_storage::tx_registry::snapshot()
        .into_iter()
        .find(|(_, label)| label.as_deref() == Some("tx_age_sweep_reader_pin_test"))
        .expect("this test's own tx_registry entry must still be open");
    let mut tx_age_state = TxAgeSweepState::default();
    let emissions = tx_age_state.observe(
        Some((tx_id(1), our_entry.0, our_entry.1)),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert!(
        emissions.iter().any(|e| e.rung == TxAgeRung::Stale
            && e.label.as_deref() == Some("tx_age_sweep_reader_pin_test")),
        "expected a Stale emission naming the pinning reader, got: {emissions:?}"
    );

    reader.conn().execute_batch("COMMIT;").ok();
    drop(reader);
    drop(_tx_handle);
}

/// Regression #926: reproduces the exact tx_registry race directly. See
/// crates/khive-db/docs/api/checkpoint.md#tx_age_sweep_own_entry_survives_concurrent_older_registration
#[test]
#[serial(tx_registry, checkpoint_skip_metrics)]
fn tx_age_sweep_own_entry_survives_concurrent_older_registration() {
    let _decoy = khive_storage::tx_registry::register(Some("decoy_unrelated_span".to_string()));
    std::thread::sleep(Duration::from_millis(2));
    let _own = khive_storage::tx_registry::register(Some("this_test_own_span".to_string()));
    std::thread::sleep(Duration::from_millis(5));

    // Confirm the race condition is actually reproduced: an entry older
    // than this test's own span must currently lead the process-wide
    // registry. Another concurrently running test may have registered an
    // entry before the decoy, so do not assume the decoy is globally
    // oldest; the required invariant is only that our span is not.
    let global_oldest = khive_storage::tx_registry::oldest().expect("registry not empty");
    assert_ne!(
        global_oldest.2.as_deref(),
        Some("this_test_own_span"),
        "test setup must reproduce the race: an older, unrelated entry must be \
             the current global oldest, got: {global_oldest:?}"
    );

    let our_entry = khive_storage::tx_registry::snapshot()
        .into_iter()
        .find(|(_, label)| label.as_deref() == Some("this_test_own_span"))
        .expect("this test's own tx_registry entry must still be open");

    let config = CheckpointConfig {
        tx_warn_secs: Duration::from_millis(1),
        tx_max_age_secs: Duration::from_millis(1),
        ..CheckpointConfig::default()
    };
    let mut state = TxAgeSweepState::default();
    let emissions = state.observe(
        Some((tx_id(2), our_entry.0, our_entry.1)),
        config.tx_warn_secs,
        config.tx_max_age_secs,
    );
    assert!(
        emissions
            .iter()
            .any(|e| e.rung == TxAgeRung::Stale
                && e.label.as_deref() == Some("this_test_own_span")),
        "expected a Stale emission naming this test's own span despite an older, \
             unrelated concurrent registration, got: {emissions:?}"
    );
}

// ADR-094: `CheckpointOutcomeRecorded` lifecycle event tests.

#[derive(Clone, Copy)]
enum FakeAppendBehavior {
    Record,
    Fail,
}

struct FakeEventStore {
    events: std::sync::Mutex<Vec<khive_storage::Event>>,
    append_attempts: std::sync::atomic::AtomicUsize,
    append_behavior: FakeAppendBehavior,
}

impl Default for FakeEventStore {
    fn default() -> Self {
        Self {
            events: std::sync::Mutex::new(Vec::new()),
            append_attempts: std::sync::atomic::AtomicUsize::new(0),
            append_behavior: FakeAppendBehavior::Record,
        }
    }
}

impl FakeEventStore {
    fn failing() -> Self {
        Self {
            append_behavior: FakeAppendBehavior::Fail,
            ..Self::default()
        }
    }
}

#[async_trait::async_trait]
impl khive_storage::EventStore for FakeEventStore {
    async fn append_event(&self, event: khive_storage::Event) -> khive_storage::StorageResult<()> {
        self.append_attempts
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        match self.append_behavior {
            FakeAppendBehavior::Record => {
                self.events.lock().unwrap().push(event);
                Ok(())
            }
            FakeAppendBehavior::Fail => Err(khive_storage::StorageError::Internal(
                "synthetic checkpoint lifecycle append failure".to_string(),
            )),
        }
    }

    async fn append_events(
        &self,
        events: Vec<khive_storage::Event>,
    ) -> khive_storage::StorageResult<khive_storage::BatchWriteSummary> {
        let count = events.len() as u64;
        self.events.lock().unwrap().extend(events);
        Ok(khive_storage::BatchWriteSummary {
            attempted: count,
            affected: count,
            ..khive_storage::BatchWriteSummary::default()
        })
    }

    async fn get_event(
        &self,
        id: uuid::Uuid,
    ) -> khive_storage::StorageResult<Option<khive_storage::Event>> {
        Ok(self
            .events
            .lock()
            .unwrap()
            .iter()
            .find(|e| e.id == id)
            .cloned())
    }

    async fn query_events(
        &self,
        _filter: khive_storage::EventFilter,
        _page: khive_storage::PageRequest,
    ) -> khive_storage::StorageResult<khive_storage::Page<khive_storage::Event>> {
        unimplemented!("not exercised by the checkpoint lifecycle-event tests")
    }

    async fn count_events(
        &self,
        _filter: khive_storage::EventFilter,
    ) -> khive_storage::StorageResult<u64> {
        Ok(self.events.lock().unwrap().len() as u64)
    }
}

/// Pure decision-table coverage for every input combination
/// `checkpoint_outcome_should_emit` can see: elevation, sustained
/// pressure, recovery, and repeated healthy observations.
#[test]
fn checkpoint_outcome_should_emit_covers_all_transitions() {
    assert!(
        checkpoint_outcome_should_emit(true, false),
        "first elevated tick must emit"
    );
    assert!(
        !checkpoint_outcome_should_emit(true, true),
        "sustained elevated ticks must aggregate in memory instead of writing the WAL"
    );
    assert!(
        checkpoint_outcome_should_emit(false, true),
        "the single drain row (elevated -> healthy) must emit"
    );
    assert!(
        !checkpoint_outcome_should_emit(false, false),
        "an ordinary below-warn tick must not emit"
    );
}

/// Regression #1838: under a persistent WAL pin, lifecycle persistence
/// must scale with pressure-state transitions, not checkpoint attempts.
#[test]
fn persistent_pressure_lifecycle_rows_are_o_state_transitions() {
    let observations = [true; 128].into_iter().chain([false]).chain([false; 128]);
    let mut was_elevated = false;
    let writes = observations
        .filter(|above_warn| {
            let emit = checkpoint_outcome_should_emit(*above_warn, was_elevated);
            if emit {
                was_elevated = *above_warn;
            }
            emit
        })
        .count();

    assert_eq!(
        writes, 2,
        "one elevation row plus one recovery summary must cover any number of attempts"
    );
}

#[test]
fn checkpoint_pressure_episode_retains_recovery_summary() {
    let mut episode = CheckpointPressureEpisode::start(2_500);
    episode.observe(2_300);
    episode.observe(8_100);
    episode.observe(4_000);

    assert_eq!(episode.elevated_ticks, 4);
    assert_eq!(episode.peak_wal_pages, 8_100);
}

/// Drives [`observe_checkpoint_pressure_tick`] through a fixed sequence
/// of `(above_warn, wal_pages)` ticks, faking `try_emit` per call index
/// (0-based across the whole sequence) via `fail_on`. Returns every
/// payload that was reported as successfully delivered, in delivery
/// order.
fn drive_pressure_ticks(
    config: &CheckpointConfig,
    ticks: &[(bool, u64)],
    mut fail_on: impl FnMut(usize) -> bool,
) -> Vec<khive_storage::CheckpointOutcomeRecordedPayload> {
    let mut event_elevation_open = false;
    let mut pressure_episode: Option<CheckpointPressureEpisode> = None;
    let mut pending_recovery: Option<khive_storage::CheckpointOutcomeRecordedPayload> = None;
    let mut delivered = Vec::new();
    let mut call_index = 0usize;
    for &(above_warn, wal_pages) in ticks {
        observe_checkpoint_pressure_tick(
            above_warn,
            wal_pages,
            false,
            false,
            config,
            &mut event_elevation_open,
            &mut pressure_episode,
            &mut pending_recovery,
            |payload| {
                let idx = call_index;
                call_index += 1;
                if fail_on(idx) {
                    false
                } else {
                    delivered.push(payload);
                    true
                }
            },
        );
    }
    delivered
}

#[test]
fn busy_row_does_not_close_or_rearm_an_observed_pressure_episode() {
    let config = CheckpointConfig {
        warn_pages: 10,
        high_water_pages: 15,
        warn_sustained_cycles: 2,
        ..CheckpointConfig::default()
    };
    let rows = [
        RawCheckpointObservation {
            busy: 0,
            log_frames: 20,
            checkpointed_frames: 5,
        },
        RawCheckpointObservation {
            busy: 1,
            log_frames: -1,
            checkpointed_frames: -1,
        },
        RawCheckpointObservation {
            busy: 0,
            log_frames: 20,
            checkpointed_frames: 5,
        },
        RawCheckpointObservation {
            busy: 0,
            log_frames: 5,
            checkpointed_frames: 5,
        },
    ];
    let ticks: Vec<(bool, u64)> = rows
        .iter()
        .filter_map(|row| observed_wal_pages(*row).ok())
        .map(|pages| (pages >= config.warn_pages, pages))
        .collect();
    assert_eq!(ticks, vec![(true, 20), (true, 20), (false, 5)]);

    let elevated = drive_pressure_ticks(&config, &ticks[..2], |_| false);
    assert_eq!(
        elevated.len(),
        1,
        "busy must not emit a recovery or new elevation"
    );
    assert!(elevated[0].above_warn);
    let complete = drive_pressure_ticks(&config, &ticks, |_| false);
    assert_eq!(complete.len(), 2, "one elevation and one real recovery");
    assert!(!complete[1].above_warn);
    assert_eq!(complete[1].episode_elevated_ticks, Some(2));

    let mut severity = CheckpointSeverityState::default();
    let mut was_above_high_water = false;
    let mut high_water_warnings = 0;
    let mut rungs = Vec::new();
    for row in rows {
        if let Ok(pages) = observed_wal_pages(row) {
            high_water_warnings += usize::from(crossing_warn(
                pages >= config.high_water_pages,
                &mut was_above_high_water,
            ));
            rungs.extend(
                severity
                    .observe_wal_pages(pages, &config)
                    .into_iter()
                    .map(|e| e.rung),
            );
        }
    }
    assert_eq!(high_water_warnings, 1);
    assert_eq!(
        rungs,
        vec![CheckpointSeverityRung::Info, CheckpointSeverityRung::Warn]
    );
    assert!(
        !was_above_high_water,
        "only the measured recovery rearms high-water"
    );
    assert_eq!(
        observed_wal_pages(RawCheckpointObservation {
            busy: 1,
            log_frames: 100,
            checkpointed_frames: 99,
        }),
        Err(CheckpointUnavailableReason::Busy),
        "a populated busy row is neutral too"
    );
    assert_eq!(
        observed_wal_pages(RawCheckpointObservation {
            busy: 0,
            log_frames: -1,
            checkpointed_frames: -1,
        }),
        Ok(0),
        "a nonbusy row with no WAL is a measured zero"
    );
    assert_eq!(
        observed_wal_pages(RawCheckpointObservation {
            busy: 0,
            log_frames: 20,
            checkpointed_frames: -1,
        }),
        Err(CheckpointUnavailableReason::InconsistentFrames),
        "a malformed frame pair must not be clamped into a measurement"
    );
    assert_eq!(
        observed_wal_pages(RawCheckpointObservation {
            busy: 0,
            log_frames: 5,
            checkpointed_frames: 6,
        }),
        Err(CheckpointUnavailableReason::InconsistentFrames),
        "a checkpointed count above the log count is inconsistent"
    );
}

#[test]
#[serial(checkpoint_skip_metrics)]
fn inconsistent_nonbusy_row_is_not_reported_as_sqlite_busy() {
    reset_checkpoint_metrics_for_tests();
    let dir = tempfile::tempdir().unwrap();
    let pool = file_pool(&dir.path().join("inconsistent_checkpoint_row.db"));
    let conn = checkpoint_conn(&pool);
    let row = RawCheckpointObservation {
        busy: 0,
        log_frames: 20,
        checkpointed_frames: -1,
    };
    assert_eq!(
        observed_wal_pages(row),
        Err(CheckpointUnavailableReason::InconsistentFrames)
    );
    test_arm_passive_row(&pool, row);

    let mut result = None;
    let events = capture(|| {
        result = Some(checkpoint_once(
            &pool,
            &conn,
            &CheckpointConfig::default(),
            &mut TruncateState::default(),
        ));
    });
    let error = result
        .expect("checkpoint_once ran")
        .expect_err("an inconsistent nonbusy row cannot be measured");
    let rusqlite::Error::SqliteFailure(code, Some(message)) = error else {
        panic!("expected an explicit SQLite error for inconsistent frames");
    };
    assert_eq!(code.extended_code, rusqlite::ffi::SQLITE_ERROR);
    assert!(message.contains("inconsistent frame pair"));
    assert!(!message.contains("busy"));
    assert!(events.iter().any(|event| {
        event.message.as_deref()
            == Some("WAL PASSIVE checkpoint returned an inconsistent frame pair; frame observation unavailable")
            && event.busy == Some(0)
    }));
    assert_eq!(checkpoint_timing(&pool).busy_ticks, 0);
    assert!(test_take_passive_row(&pool).is_none());
}

#[tokio::test]
#[serial(checkpoint_skip_metrics)]
async fn periodic_task_skips_a_passive_busy_row_after_opening_its_connection() {
    reset_checkpoint_metrics_for_tests();
    let dir = tempfile::tempdir().unwrap();
    let pool = file_pool(&dir.path().join("periodic_busy_row.db"));
    {
        let writer = pool.writer().expect("seed WAL database");
        writer
            .conn()
            .execute_batch("CREATE TABLE t (v INTEGER); INSERT INTO t VALUES (1)")
            .expect("seed row");
    }
    // The real periodic task owns its dedicated connection. Inject only
    // its first SQLite-shaped PASSIVE return row, not a missing connection
    // or a hand-built CheckpointTick, so the production mapping to
    // `Skipped` and the skip counter are both exercised.
    test_arm_passive_row(
        &pool,
        RawCheckpointObservation {
            busy: 1,
            log_frames: 10,
            checkpointed_frames: 5,
        },
    );
    let skipped_before = checkpoint_skipped_ticks();
    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let task = tokio::spawn(run_checkpoint_task(
        Arc::clone(&pool),
        CheckpointConfig {
            interval: Duration::from_millis(10),
            truncate_high_water_pages: u64::MAX,
            ..CheckpointConfig::default()
        },
        None,
        shutdown_rx,
        false,
    ));
    let deadline = tokio::time::Instant::now() + Duration::from_secs(5);
    while checkpoint_timing(&pool).busy_ticks == 0 || checkpoint_skipped_ticks() == skipped_before {
        assert!(
            tokio::time::Instant::now() < deadline,
            "periodic task did not classify the busy PASSIVE row as a skipped tick"
        );
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    shutdown_tx.send(()).expect("stop checkpoint task");
    tokio::time::timeout(Duration::from_secs(2), task)
        .await
        .expect("checkpoint task stops")
        .expect("checkpoint task does not panic");
    assert!(test_take_passive_row(&pool).is_none());
    assert!(checkpoint_timing(&pool).busy_ticks >= 1);
    assert!(checkpoint_skipped_ticks() > skipped_before);
}

/// #1857 regression: a dropped recovery handoff must not fold the next,
/// separate, pressure incident into the closed episode's aggregate — and
/// the undelivered recovery is a BARRIER, so episode 2's opening must not
/// be delivered ahead of episode 1's recovery (ADR-094: consumers assert
/// on the ordered event history).
///
/// Sequence: episode 1 opens and sustains for 3 ticks, then its recovery
/// row is dropped (call index 1) and two retries are also dropped (call
/// indices 2 and 3) — the second of them on the tick where episode 2
/// begins, so the barrier defers episode 2's opening. Once the worker
/// frees up, episode 1's delayed recovery delivers first, then episode
/// 2's opening (reflecting its state at emission time), then episode 2's
/// recovery.
#[test]
#[serial(checkpoint_skip_metrics)]
fn dropped_recovery_handoff_does_not_merge_pressure_episodes() {
    let config = CheckpointConfig {
        warn_pages: 1_000,
        ..CheckpointConfig::default()
    };
    let ticks = [
        (true, 1_500), // call 0: episode 1 opens — delivered
        (true, 1_800), // sustained, no emit attempt
        (true, 2_000), // sustained, no emit attempt
        (false, 500),  // call 1: episode 1 recovery — DROPPED
        (false, 400),  // call 2: retry episode 1 recovery — DROPPED
        (true, 3_000), // call 3: retry — DROPPED; barrier defers episode 2's opening
        (true, 3_500), // call 4: retry — delivered; call 5: episode 2 opens — delivered
        (false, 300),  // call 6: episode 2 recovery — delivered
    ];

    let delivered = drive_pressure_ticks(&config, &ticks, |idx| matches!(idx, 1..=3));

    assert_eq!(
        delivered.len(),
        4,
        "expected episode-1 open, episode-1 delayed recovery, episode-2 open, \
             episode-2 recovery: {delivered:?}"
    );

    let ep1_open = &delivered[0];
    assert!(ep1_open.above_warn);
    assert_eq!(ep1_open.episode_elevated_ticks, Some(1));
    assert_eq!(ep1_open.episode_peak_wal_pages, Some(1_500));

    let ep1_recovery = &delivered[1];
    assert!(
        !ep1_recovery.above_warn,
        "episode 1's recovery must be delivered BEFORE episode 2's opening; \
             an opening in this slot means the barrier failed: {delivered:?}"
    );
    assert_eq!(
        ep1_recovery.episode_elevated_ticks,
        Some(3),
        "episode 1's delayed recovery must report only its own 3 elevated ticks, \
             not ticks absorbed from episode 2"
    );
    assert_eq!(ep1_recovery.episode_peak_wal_pages, Some(2_000));

    let ep2_open = &delivered[2];
    assert!(ep2_open.above_warn);
    assert_eq!(
        ep2_open.episode_elevated_ticks,
        Some(2),
        "episode 2 opens fresh (never continuing episode 1's count), deferred one \
             tick by the barrier, so its opening reports 2 elevated ticks"
    );
    assert_eq!(ep2_open.episode_peak_wal_pages, Some(3_500));

    let ep2_recovery = &delivered[3];
    assert!(!ep2_recovery.above_warn);
    assert_eq!(
        ep2_recovery.episode_elevated_ticks,
        Some(2),
        "episode 2's recovery must report only its own 2 elevated ticks"
    );
    assert_eq!(ep2_recovery.episode_peak_wal_pages, Some(3_500));
}

/// Degenerate barrier arm: an episode whose entire lifetime falls inside
/// the window where an earlier recovery is still undelivered is discarded
/// rather than reported out of order — from any consumer's view it never
/// opened, so no stale opening or recovery for it may surface after the
/// queue frees. The loss itself is counted and logged at the discard
/// site; this test pins the delivered-history shape.
#[test]
#[serial(checkpoint_skip_metrics)]
fn episode_elapsed_entirely_behind_barrier_is_discarded_not_reordered() {
    let config = CheckpointConfig {
        warn_pages: 1_000,
        ..CheckpointConfig::default()
    };
    let ticks = [
        (true, 1_500), // call 0: episode 1 opens — delivered
        (false, 500),  // call 1: episode 1 recovery — DROPPED
        (true, 9_000), // call 2: retry — DROPPED; barrier defers episode 2's opening
        (false, 400),  // call 3: retry — DROPPED; episode 2 discarded behind barrier
        (false, 300),  // call 4: retry — delivered
        (true, 2_500), // call 5: episode 3 opens — delivered
        (false, 200),  // call 6: episode 3 recovery — delivered
    ];

    let delivered = drive_pressure_ticks(&config, &ticks, |idx| matches!(idx, 1..=3));

    let peaks: Vec<_> = delivered
        .iter()
        .map(|payload| (payload.above_warn, payload.episode_peak_wal_pages))
        .collect();
    assert_eq!(
        peaks,
        vec![
            (true, Some(1_500)),  // episode 1 open
            (false, Some(1_500)), // episode 1 delayed recovery
            (true, Some(2_500)),  // episode 3 open — episode 2 (peak 9_000) never surfaces
            (false, Some(2_500)), // episode 3 recovery
        ],
        "an episode elapsed entirely behind the barrier must not surface late or \
             out of order: {delivered:?}"
    );
}

/// ASCII-simple control for the regression above: the identical tick
/// sequence with no queue drops must report the same two episodes
/// separately (and promptly), confirming the merge in the drop case is
/// caused by the drop, not by the tick sequence itself.
#[test]
fn no_dropped_handoff_reports_two_separate_episodes() {
    let config = CheckpointConfig {
        warn_pages: 1_000,
        ..CheckpointConfig::default()
    };
    let ticks = [
        (true, 1_500),
        (true, 1_800),
        (true, 2_000),
        (false, 500),
        (false, 400),
        (true, 3_000),
        (true, 3_500),
        (false, 300),
    ];

    let delivered = drive_pressure_ticks(&config, &ticks, |_idx| false);

    assert_eq!(delivered.len(), 4, "{delivered:?}");
    assert_eq!(
        (
            delivered[0].above_warn,
            delivered[0].episode_elevated_ticks,
            delivered[0].episode_peak_wal_pages
        ),
        (true, Some(1), Some(1_500)),
        "episode 1 open"
    );
    assert_eq!(
        (
            delivered[1].above_warn,
            delivered[1].episode_elevated_ticks,
            delivered[1].episode_peak_wal_pages
        ),
        (false, Some(3), Some(2_000)),
        "episode 1 recovery"
    );
    assert_eq!(
        (
            delivered[2].above_warn,
            delivered[2].episode_elevated_ticks,
            delivered[2].episode_peak_wal_pages
        ),
        (true, Some(1), Some(3_000)),
        "episode 2 open"
    );
    assert_eq!(
        (
            delivered[3].above_warn,
            delivered[3].episode_elevated_ticks,
            delivered[3].episode_peak_wal_pages
        ),
        (false, Some(2), Some(3_500)),
        "episode 2 recovery"
    );
}

#[test]
#[serial(checkpoint_skip_metrics)]
fn pressure_diagnostics_count_observations_and_transitions_separately() {
    reset_checkpoint_metrics_for_tests();

    note_checkpoint_pressure_observation(true, false);
    note_checkpoint_pressure_observation(true, true);
    note_checkpoint_pressure_observation(true, true);
    note_checkpoint_pressure_observation(false, true);
    note_checkpoint_pressure_observation(false, false);

    assert_eq!(checkpoint_pressure_elevated_ticks(), 3);
    assert_eq!(checkpoint_pressure_episodes_started(), 1);
    assert_eq!(checkpoint_pressure_episodes_recovered(), 1);
    assert_eq!(checkpoint_lifecycle_append_attempts(), 0);
}

#[tokio::test]
#[serial(checkpoint_skip_metrics)]
async fn checkpoint_task_emits_one_opening_for_persistent_pressure() {
    reset_checkpoint_metrics_for_tests();
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("outcome_emit.db");
    let pool = file_pool(&path);

    // warn_pages: 0 means any observed WAL page count (even 0) is
    // "elevated" for the duration this config is active.
    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        warn_pages: 0,
        ..CheckpointConfig::default()
    };
    let store = Arc::new(FakeEventStore::default());
    let store_dyn: Arc<dyn khive_storage::EventStore> = store.clone();

    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(
        pool,
        cfg,
        Some(CheckpointLifecycleOwner::new(store_dyn, "local")),
        shutdown_rx,
        true,
    ));

    let progressed = wait_for(Duration::from_secs(10), || {
        checkpoint_pressure_elevated_ticks() >= 10
    })
    .await;
    let emitted = wait_for(Duration::from_secs(10), || {
        !store.events.lock().unwrap().is_empty()
    })
    .await;
    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should exit within 1s")
        .expect("checkpoint task panicked");

    let events = store.events.lock().unwrap();
    assert!(
        progressed,
        "the simulated persistent-pressure episode must span at least ten checkpoint ticks"
    );
    assert!(
        emitted,
        "an always-elevated config must append one CheckpointOutcomeRecorded event \
             within the poll deadline"
    );
    assert_eq!(
        checkpoint_lifecycle_append_attempts(),
        1,
        "primary-store lifecycle writes must stay O(state transitions), not O(attempts)"
    );
    assert_eq!(events.len(), 1);
    assert_eq!(events[0].payload_schema_version, 2);
    assert_eq!(events[0].payload["episode_elevated_ticks"], 1);
    assert_eq!(
        events[0].payload["episode_peak_wal_pages"],
        events[0].payload["wal_pages"]
    );
    assert!(
        events
            .iter()
            .all(|e| e.kind == khive_types::EventKind::CheckpointOutcomeRecorded),
        "every appended event must be CheckpointOutcomeRecorded, got: {events:?}"
    );
    assert!(
        events.iter().all(|e| e.namespace == "local"),
        "events must be stamped with the namespace passed to run_checkpoint_task"
    );
}

/// Regression #1434/#1838: a lifecycle append may wait five seconds for
/// its sink writer, while checkpoint observations must continue without
/// enqueueing one new row per elevated tick.
#[tokio::test]
#[serial(checkpoint_skip_metrics)]
async fn checkpoint_cycles_and_task_shutdown_do_not_wait_for_a_contended_lifecycle_writer() {
    reset_checkpoint_metrics_for_tests();
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("outcome_contended_sink.db");
    let checkpoint_pool = file_pool(&path);

    let event_pool = Arc::new(
        ConnectionPool::new(PoolConfig {
            path: None,
            checkout_timeout: Duration::from_secs(5),
            write_queue_enabled: Some(false),
            ..PoolConfig::default()
        })
        .expect("event pool"),
    );
    {
        let writer = event_pool.try_writer().expect("initialize event schema");
        crate::stores::event::ensure_events_schema(writer.conn()).expect("initialize event schema");
    }
    let event_store: Arc<dyn khive_storage::EventStore> = Arc::new(
        crate::stores::event::SqlEventStore::new_scoped(Arc::clone(&event_pool), false, "local"),
    );
    let held_event_writer = event_pool
        .try_writer()
        .expect("hold the event-store writer");

    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        warn_pages: 0,
        ..CheckpointConfig::default()
    };
    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(
        checkpoint_pool,
        cfg,
        Some(CheckpointLifecycleOwner::new(event_store, "local")),
        shutdown_rx,
        true,
    ));

    let progressed = wait_for(Duration::from_secs(2), || {
        checkpoint_pressure_elevated_ticks() >= 10
    })
    .await;
    assert!(
        progressed,
        "checkpoint observations must continue while the lifecycle append is contended"
    );
    assert_eq!(checkpoint_lifecycle_append_attempts(), 1);
    assert_eq!(checkpoint_lifecycle_enqueue_drops(), 0);

    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect(
            "the run_checkpoint_task handle must not wait for the event store's \
                 five-second writer checkout",
        )
        .expect("checkpoint task panicked");

    // The bound above is deliberately checkpoint-task-local. Aborting the
    // lifecycle worker cannot cancel the `spawn_blocking` checkout already
    // admitted by `SqlEventStore`; release its fixture contention only
    // after the `run_checkpoint_task` handle has returned.
    drop(held_event_writer);
}

/// A sink error is observable without turning a sustained pressure
/// episode into a retrying primary-store write loop.
#[tokio::test]
#[serial(checkpoint_skip_metrics)]
async fn checkpoint_task_continues_after_lifecycle_append_failure() {
    reset_checkpoint_metrics_for_tests();
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("outcome_failing_sink.db");
    let pool = file_pool(&path);
    let store = Arc::new(FakeEventStore::failing());
    let store_dyn: Arc<dyn khive_storage::EventStore> = store.clone();

    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };
    let _tracing_guard = tracing::subscriber::set_default(subscriber);

    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        warn_pages: 0,
        ..CheckpointConfig::default()
    };
    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(
        pool,
        cfg,
        Some(CheckpointLifecycleOwner::new(store_dyn, "local")),
        shutdown_rx,
        true,
    ));

    let progressed = wait_for(Duration::from_secs(2), || {
        checkpoint_pressure_elevated_ticks() >= 10
    })
    .await;
    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should remain responsive after sink failure")
        .expect("checkpoint task panicked");

    assert!(
        progressed,
        "a failed append must not terminate or stall the checkpoint task"
    );
    assert_eq!(
        store
            .append_attempts
            .load(std::sync::atomic::Ordering::Relaxed),
        1,
        "a persistent pressure state must not retry one primary-store append per tick"
    );
    assert_eq!(checkpoint_lifecycle_append_attempts(), 1);
    assert_eq!(checkpoint_lifecycle_append_failures(), 1);
    let captured = buffer.lock().unwrap().clone();
    assert!(
        captured
            .iter()
            .any(|event| event.message.as_deref()
                == Some("checkpoint lifecycle event append failed")),
        "lifecycle append failures must remain observable; got: {:?}",
        captured
    );
}

#[tokio::test]
#[serial(checkpoint_skip_metrics)]
async fn secondary_checkpoint_task_with_lifecycle_ownership_emits_outcome_events() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("secondary_outcome.db");
    let pool = file_pool(&path);
    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        warn_pages: 0,
        ..CheckpointConfig::default()
    };
    let store = Arc::new(FakeEventStore::default());
    let store_dyn: Arc<dyn khive_storage::EventStore> = store.clone();

    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(
        pool,
        cfg,
        Some(CheckpointLifecycleOwner::new(store_dyn, "local")),
        shutdown_rx,
        false,
    ));

    // Poll for the first emitted event instead of a fixed sleep (same
    // slowdown-flake class as the stale-sweep test above).
    let emitted = wait_for(Duration::from_secs(10), || {
        !store.events.lock().unwrap().is_empty()
    })
    .await;
    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should exit within 1s")
        .expect("checkpoint task panicked");

    assert!(
        emitted,
        "a designated secondary lifecycle owner must append outcome events within the poll \
             deadline"
    );
}

#[tokio::test]
#[serial(checkpoint_skip_metrics)]
async fn checkpoint_task_emits_nothing_while_healthy() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("outcome_no_emit.db");
    let pool = file_pool(&path);

    // An unreachable warn_pages threshold for this test's tiny WAL: every
    // tick stays below warn, so no event should ever be appended.
    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        warn_pages: u64::MAX,
        ..CheckpointConfig::default()
    };
    let store = Arc::new(FakeEventStore::default());
    let store_dyn: Arc<dyn khive_storage::EventStore> = store.clone();

    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(
        pool,
        cfg,
        Some(CheckpointLifecycleOwner::new(store_dyn, "local")),
        shutdown_rx,
        true,
    ));

    tokio::time::sleep(Duration::from_millis(60)).await;
    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should exit within 1s")
        .expect("checkpoint task panicked");

    assert!(
        store.events.lock().unwrap().is_empty(),
        "a config that never crosses warn_pages must never append a lifecycle event"
    );
}

#[tokio::test]
#[serial(checkpoint_skip_metrics)]
async fn checkpoint_task_with_no_event_store_does_not_panic() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("outcome_none_store.db");
    let pool = file_pool(&path);

    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        warn_pages: 0,
        ..CheckpointConfig::default()
    };

    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(pool, cfg, None, shutdown_rx, true));

    tokio::time::sleep(Duration::from_millis(40)).await;
    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should exit within 1s")
        .expect("checkpoint task panicked");
}

// Fix: task-level regressions
// that actually spawn `run_checkpoint_task` and capture its `tracing`
// output, so the wiring at the `tx_age_state.observe(...)` call site
// itself is under test — the pure `TxAgeSweepState` unit tests above
// stay green even if that call site is deleted; these do not. All three
// share `#[serial(tx_registry, checkpoint_skip_metrics)]`: `tx_registry`
// because they read the process-wide registry singleton (see the
// `log_tx_registry_oldest_debug_reports_oldest_open_entry` doc comment
// above for why other tests in this same binary can transiently touch
// it too), `checkpoint_skip_metrics` because they spawn the real task
// that updates the module's skip-tracking atomics.

/// (1) A stale labeled entry with a healthy WAL: the spawned task itself
/// must sweep and escalate it to `Stale`, with WAL-pressure thresholds
/// set unreachably high so only the age sweep — never the WAL-pressure
/// ladder — could be responsible for the captured emission.
#[tokio::test]
#[serial(tx_registry, checkpoint_skip_metrics)]
async fn checkpoint_task_sweeps_stale_registry_entry_while_wal_is_healthy() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("tx_age_sweep_task_healthy_wal.db");
    let pool = file_pool(&path);

    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };
    let _tracing_guard = tracing::subscriber::set_default(subscriber);

    let _tx_handle = khive_storage::tx_registry::register(Some(
        "checkpoint_task_healthy_wal_sweep_test".to_string(),
    ));

    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        warn_pages: u64::MAX,
        high_water_pages: u64::MAX,
        truncate_high_water_pages: u64::MAX,
        tx_warn_secs: Duration::from_millis(1),
        tx_max_age_secs: Duration::from_millis(1),
        ..CheckpointConfig::default()
    };

    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(pool, cfg, None, shutdown_rx, true));

    // Poll for the sweep instead of a fixed sleep: a fixed wall-clock
    // budget assumes the spawned task completes its tick (registry scan
    // + walpin sidecar heartbeat) within that window, which widens and
    // flakes under slowdown (coverage instrumentation, contended CI
    // runners) — see `wait_for`'s doc comment for the same reasoning
    // applied to the sibling walpin tests.
    let swept = wait_for(Duration::from_secs(10), || {
        buffer.lock().unwrap().iter().any(|e| {
            e.tx_label.as_deref() == Some("checkpoint_task_healthy_wal_sweep_test")
                && e.message
                    .as_deref()
                    .is_some_and(|m| m.contains("stale-op cap"))
        })
    })
    .await;

    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should exit within 1s")
        .expect("checkpoint task panicked");

    drop(_tx_handle);

    let events = buffer.lock().unwrap();
    assert!(
        swept,
        "expected the spawned task to sweep and escalate the stale registry entry \
             to Stale on its own within the poll deadline, got: {events:?}"
    );
}

/// (2) An empty registry must never produce a Plank 1 age emission from
/// the real spawned task, mirroring the pure
/// `tx_age_sweep_empty_registry_emits_nothing` unit test above.
#[tokio::test]
#[serial(tx_registry, checkpoint_skip_metrics)]
async fn checkpoint_task_emits_no_age_alert_for_an_empty_registry() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("tx_age_sweep_task_empty_registry.db");
    let pool = file_pool(&path);

    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };
    let _tracing_guard = tracing::subscriber::set_default(subscriber);

    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        tx_warn_secs: Duration::from_millis(1),
        tx_max_age_secs: Duration::from_millis(1),
        ..CheckpointConfig::default()
    };

    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(pool, cfg, None, shutdown_rx, true));

    tokio::time::sleep(Duration::from_millis(40)).await;
    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should exit within 1s")
        .expect("checkpoint task panicked");

    let events = buffer.lock().unwrap();
    assert!(
        events.iter().all(|e| e
            .message
            .as_deref()
            .is_none_or(|m| !m.contains("ADR-091 Plank 1"))),
        "an empty registry must never produce a Plank 1 age emission, got: {events:?}"
    );
}

/// (3) High-finding regression: a Skipped tick must NOT silence the age
/// sweep. Since the dedicated-connection fix, holding the pool's writer
/// mutex no longer produces a Skipped tick at all (see
/// `checkpoint_once_proceeds_and_can_attempt_truncate_while_pool_writer_held`),
/// so this drives Skipped the way it now actually happens in production:
/// a read-only pool, on which `ConnectionPool::open_standalone_writer`
/// always fails, so `CheckpointConnection::ensure_open` can never open a
/// dedicated connection and every tick reports `Skipped`. Asserts the age
/// alert still fires across several such ticks alongside a stale
/// registered entry. Before the original fix (#845 predecessor), the
/// sweep call sat after the `Skipped` early-continue and never ran here;
/// this regression must keep holding under the new skip mechanism too.
#[tokio::test]
#[serial(tx_registry, checkpoint_skip_metrics)]
async fn checkpoint_task_sweeps_stale_entry_even_when_dedicated_connection_is_unavailable_every_tick(
) {
    reset_checkpoint_metrics_for_tests();

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("tx_age_sweep_task_conn_unavailable.db");
    {
        // Seed the schema with an ordinary read-write pool, then drop it
        // (releasing its connections) before reopening the same file
        // read-only below.
        let seed_pool = file_pool(&path);
        let writer = seed_pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE IF NOT EXISTS t (x INTEGER);")
            .unwrap();
    }

    #[cfg(unix)]
    {
        khive_storage::test_support::freeze_snapshot_sidecars(&path);
    }

    let pool = Arc::new(
        ConnectionPool::new(PoolConfig {
            path: Some(path.clone()),
            read_only: true,
            ..PoolConfig::for_test()
        })
        .expect("read-only pool open"),
    );
    assert!(
        pool.open_standalone_writer().is_err(),
        "test precondition: a read-only pool must never be able to open a dedicated \
             checkpoint connection"
    );

    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };
    let _tracing_guard = tracing::subscriber::set_default(subscriber);

    let _tx_handle = khive_storage::tx_registry::register(Some(
        "checkpoint_task_conn_unavailable_sweep_test".to_string(),
    ));

    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        tx_warn_secs: Duration::from_millis(1),
        tx_max_age_secs: Duration::from_millis(1),
        ..CheckpointConfig::default()
    };

    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(
        Arc::clone(&pool),
        cfg,
        None,
        shutdown_rx,
        true,
    ));

    // Wait until the task has actually recorded a Skipped tick rather
    // than sleeping a fixed real-time budget: each tick also does
    // registry queries and sidecar filesystem writes, so under
    // instrumented (coverage) or loaded runners a fixed sleep races the
    // first completed tick. Bounded, fail-loud.
    let deadline = tokio::time::Instant::now() + Duration::from_secs(10);
    while checkpoint_skipped_ticks() == 0 {
        assert!(
            tokio::time::Instant::now() < deadline,
            "test setup must actually drive at least one Skipped tick for this \
                 regression to mean anything (none within 10s)"
        );
        tokio::time::sleep(Duration::from_millis(10)).await;
    }

    loop {
        let events = buffer.lock().unwrap().clone();
        if events.iter().any(|e| {
            e.tx_label.as_deref() == Some("checkpoint_task_conn_unavailable_sweep_test")
                && e.message
                    .as_deref()
                    .is_some_and(|m| m.contains("stale-op cap"))
        }) {
            break;
        }
        assert!(
            tokio::time::Instant::now() < deadline,
            "expected the age sweep to fire even though every tick's dedicated \
                 connection was unavailable within 10s, got: {events:?}"
        );
        tokio::time::sleep(Duration::from_millis(10)).await;
    }

    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should exit within 1s")
        .expect("checkpoint task panicked");
    drop(_tx_handle);
}

// ── ADR-091 Amendment 2: Plank A (session sweep), Plank B (walpin
// sidecar), Plank C (backfill-gap probe) ─────────────────────────────

#[tokio::test]
async fn session_sweep_task_exits_on_shutdown_signal() {
    let cfg = SessionSweepConfig {
        interval: Duration::from_millis(10),
        ..SessionSweepConfig::default()
    };
    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_session_sweep_task(Vec::new(), cfg, shutdown_rx));

    shutdown_tx.send(()).expect("send shutdown signal");

    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("session sweep task should exit within 1s")
        .expect("session sweep task panicked");
}

/// Bounded condition poll for filesystem effects of the async sweep
/// task — fixed sleeps flake under parallel test load because sidecar
/// writes fsync.
pub(super) async fn wait_for(deadline: Duration, mut cond: impl FnMut() -> bool) -> bool {
    let start = std::time::Instant::now();
    while start.elapsed() < deadline {
        if cond() {
            return true;
        }
        tokio::time::sleep(Duration::from_millis(5)).await;
    }
    cond()
}

#[tokio::test]
#[serial(khive_walpin_sidecar_env)]
async fn walpin_observe_drops_beacon_when_heartbeat_write_fails() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("observe_gate.db");
    let sidecar_dir = crate::walpin::sidecar_dir_for(&db_path);

    let mut state = WalpinSidecarState::new(
        Some(db_path.as_path()),
        true,
        "session",
        Duration::from_millis(500),
    )
    .expect("sidecar enabled for a file-backed path");
    let pid = std::process::id();
    state.register_beacon().await;
    let beacon_path = sidecar_dir.join(format!("{pid}.beacon"));
    let before = std::fs::metadata(&beacon_path)
        .expect("register_beacon must create the beacon file")
        .modified()
        .unwrap();

    // Force the heartbeat write to fail without touching directory
    // permissions (which would confound with the dir-mode validation):
    // occupy the exclusive-create temp name with a directory, so the
    // tolerant unlink and the O_EXCL create both fail.
    let obstruction = sidecar_dir.join(format!(".{pid}.json.tmp"));
    std::fs::create_dir(&obstruction).unwrap();

    tokio::time::sleep(Duration::from_millis(20)).await;
    let over_threshold = Some(khive_storage::tx_registry::OldestSpan {
        id: khive_storage::tx_registry::TxId(1),
        age: Duration::from_secs(60),
        label: None,
        origin: khive_storage::tx_registry::TxOrigin::Unscoped,
    });
    state
        .observe(over_threshold.clone(), Duration::from_secs(30))
        .await;

    assert!(
        !sidecar_dir.join(format!("{pid}.json")).exists(),
        "heartbeat write must have failed"
    );
    // Skipping the refresh alone would leave `before` fresh inside the
    // three-tick window; the fail-closed contract removes the beacon.
    assert!(
        !beacon_path.exists(),
        "a failed heartbeat write must remove the beacon — a still-fresh \
             beacon with no heartbeat would classify registered-silent \
             (before-mtime {before:?})"
    );

    // Recovery: clear the obstruction; the next over-threshold tick
    // writes the heartbeat and re-registers the beacon.
    std::fs::remove_dir(&obstruction).unwrap();
    state.observe(over_threshold, Duration::from_secs(30)).await;
    assert!(
        sidecar_dir.join(format!("{pid}.json")).exists(),
        "heartbeat must land once the write path recovers"
    );
    assert!(
        beacon_path.exists(),
        "beacon must re-register on the first healthy tick after removal"
    );
}

#[tokio::test]
#[serial(khive_walpin_sidecar_env)]
async fn walpin_observe_touches_mtime_without_rewriting_body_when_content_unchanged() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("observe_touch.db");
    let sidecar_dir = crate::walpin::sidecar_dir_for(&db_path);

    let mut state = WalpinSidecarState::new(
        Some(db_path.as_path()),
        true,
        "session",
        Duration::from_millis(500),
    )
    .expect("sidecar enabled for a file-backed path");
    let pid = std::process::id();
    let heartbeat_path = sidecar_dir.join(format!("{pid}.json"));
    let span = khive_storage::tx_registry::OldestSpan {
        id: khive_storage::tx_registry::TxId(1),
        age: Duration::from_secs(60),
        label: None,
        origin: khive_storage::tx_registry::TxOrigin::Unscoped,
    };

    state
        .observe(Some(span.clone()), Duration::from_secs(30))
        .await;
    let body_after_create = std::fs::read(&heartbeat_path).expect("heartbeat written");

    // Backdate the mtime so the touch is unambiguous: if `observe`
    // rewrote the body instead of touching it, the write would also
    // reset the mtime, making this assertion pass for the wrong reason —
    // the body-byte comparison below is what actually distinguishes
    // touch from rewrite.
    let backdated = std::time::SystemTime::now() - Duration::from_secs(120);
    // `set_modified` needs write access to the handle on Windows (a
    // read-only open succeeds but is refused by `set_modified` with
    // `PermissionDenied`); Unix accepts a read-only handle for this.
    std::fs::OpenOptions::new()
        .write(true)
        .open(&heartbeat_path)
        .unwrap()
        .set_modified(backdated)
        .unwrap();

    state.observe(Some(span), Duration::from_secs(30)).await;

    let body_after_second_observe =
        std::fs::read(&heartbeat_path).expect("heartbeat still present");
    assert_eq!(
        body_after_create, body_after_second_observe,
        "unchanged oldest-span identity/label/attribution/cadence must touch mtime, \
             not rewrite the body"
    );
    let mtime_after = std::fs::metadata(&heartbeat_path)
        .unwrap()
        .modified()
        .unwrap();
    assert!(
        mtime_after > backdated,
        "the touch must advance mtime past the backdated value"
    );
}

#[tokio::test]
#[serial(khive_walpin_sidecar_env)]
async fn walpin_observe_recreates_heartbeat_after_it_is_deleted_while_span_still_live() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("observe_recreate.db");
    let sidecar_dir = crate::walpin::sidecar_dir_for(&db_path);

    let mut state = WalpinSidecarState::new(
        Some(db_path.as_path()),
        true,
        "session",
        Duration::from_millis(500),
    )
    .expect("sidecar enabled for a file-backed path");
    let pid = std::process::id();
    let heartbeat_path = sidecar_dir.join(format!("{pid}.json"));
    let span = khive_storage::tx_registry::OldestSpan {
        id: khive_storage::tx_registry::TxId(1),
        age: Duration::from_secs(60),
        label: None,
        origin: khive_storage::tx_registry::TxOrigin::Unscoped,
    };

    state
        .observe(Some(span.clone()), Duration::from_secs(30))
        .await;
    assert!(heartbeat_path.exists(), "heartbeat written on first tick");

    // Simulate enumeration deleting a slow writer's heartbeat while its
    // span is still live: the next tick still sees unchanged content
    // (same span, same label, same attribution, same cadence) so it
    // takes the touch path — which must detect the missing target and
    // fall through to a full recreate rather than silently no-op.
    std::fs::remove_file(&heartbeat_path).unwrap();
    assert!(!heartbeat_path.exists());

    state.observe(Some(span), Duration::from_secs(30)).await;

    assert!(
        heartbeat_path.exists(),
        "a touch failure against a deleted heartbeat must recreate it via a full write"
    );
    let recreated: crate::walpin::WalpinHeartbeat =
        serde_json::from_slice(&std::fs::read(&heartbeat_path).unwrap()).unwrap();
    assert_eq!(recreated.pid, pid);
    assert_eq!(recreated.oldest_tx_age_secs, 60.0);
}

#[tokio::test]
#[serial(tx_registry, khive_walpin_sidecar_env)]
async fn session_sweep_task_writes_and_clears_walpin_heartbeat() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let dir = tempfile::tempdir().unwrap();
    let db_path = dir.path().join("session_sweep.db");
    let pool = file_pool(&db_path);
    let sidecar_dir =
        crate::walpin::sidecar_dir_for(pool.canonical_path().expect("file-backed pool"));

    let cfg = SessionSweepConfig {
        interval: Duration::from_millis(10),
        tx_warn_secs: Duration::from_millis(20),
        tx_max_age_secs: Duration::from_millis(500),
    };
    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_session_sweep_task(
        vec![SweepBackend {
            pool: Arc::clone(&pool),
            is_main: true,
        }],
        cfg,
        shutdown_rx,
    ));

    // No open span yet: a quiet process must write no *heartbeat*, but
    // it DOES register its one-time beacon at startup (ADR-091
    // Amendment 2 sidecar-health attribution) — the sidecar dir is not
    // empty, only heartbeat-free. Poll-wait rather than a fixed sleep:
    // the first tick fsyncs the beacon, and under parallel test load
    // that write can take longer than any small fixed window.
    let pid = std::process::id();
    let beacon = crate::walpin::beacon_path(&sidecar_dir, pid);
    let beacon_registered = wait_for(Duration::from_secs(2), || beacon.exists()).await;
    assert!(
        beacon_registered,
        "a quiet process must still register its one-time beacon"
    );
    assert!(
        !sidecar_dir.join(format!("{pid}.json")).exists(),
        "a quiet process must not write a walpin heartbeat"
    );

    let tx_handle =
        khive_storage::tx_registry::register(Some("session_sweep_walpin_test".to_string()));
    let heartbeat_path = sidecar_dir.join(format!("{pid}.json"));
    assert!(
        wait_for(Duration::from_secs(2), || heartbeat_path.exists()).await,
        "expected a walpin heartbeat once the span crossed tx_warn_secs"
    );
    let body = std::fs::read_to_string(&heartbeat_path).unwrap();
    let hb: crate::walpin::WalpinHeartbeat = serde_json::from_str(&body).unwrap();
    assert_eq!(hb.pid, pid);
    assert_eq!(hb.process_role, "session");
    assert_eq!(
        hb.oldest_tx_label.as_deref(),
        Some("session_sweep_walpin_test")
    );
    assert_eq!(
        hb.attribution_basis.as_deref(),
        Some("fallback"),
        "an Unscoped span observed only through the main view's fallback \
             must carry attribution_basis=\"fallback\", never \"origin\""
    );

    drop(tx_handle);
    assert!(
        wait_for(Duration::from_secs(2), || !heartbeat_path.exists()).await,
        "heartbeat must be removed once the stale span clears"
    );

    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("session sweep task should exit within 1s")
        .expect("session sweep task panicked");
}

/// ADR-091 Amendment 3 fan-out: two file-backed pools in one process,
/// each its own `SweepBackend`. A span scoped to the SECONDARY pool's
/// own origin must produce a heartbeat only in the secondary's sidecar
/// — never the main backend's — and, because a `Secondary` filter never
/// falls back to `Unscoped`, its heartbeat carries the evidence-backed
/// `attribution_basis="origin"`. Uses the `graph_traverse_read` label
/// (`stores/graph.rs`'s `traverse`) — the design note's own example of
/// "the most WAL-pin-relevant span in the store" — as the registered
/// span's label, so this doubles as coverage that a traversal read span
/// surfaces correctly in a secondary backend's filtered view.
#[tokio::test]
#[serial(tx_registry, khive_walpin_sidecar_env)]
async fn session_sweep_fan_out_scopes_secondary_span_to_secondary_sidecar_only() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let main_dir = tempfile::tempdir().unwrap();
    let secondary_dir = tempfile::tempdir().unwrap();
    let main_pool = file_pool(&main_dir.path().join("main.db"));
    let secondary_pool = file_pool(&secondary_dir.path().join("secondary.db"));
    let main_sidecar =
        crate::walpin::sidecar_dir_for(main_pool.canonical_path().expect("file-backed"));
    let secondary_sidecar =
        crate::walpin::sidecar_dir_for(secondary_pool.canonical_path().expect("file-backed"));

    let cfg = SessionSweepConfig {
        interval: Duration::from_millis(10),
        tx_warn_secs: Duration::from_millis(20),
        tx_max_age_secs: Duration::from_millis(500),
    };
    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_session_sweep_task(
        vec![
            SweepBackend {
                pool: Arc::clone(&main_pool),
                is_main: true,
            },
            SweepBackend {
                pool: Arc::clone(&secondary_pool),
                is_main: false,
            },
        ],
        cfg,
        shutdown_rx,
    ));

    let pid = std::process::id();
    let secondary_heartbeat = secondary_sidecar.join(format!("{pid}.json"));
    let main_heartbeat = main_sidecar.join(format!("{pid}.json"));

    let tx_handle = khive_storage::tx_registry::register_scoped(
        Some("graph_traverse_read".to_string()),
        secondary_pool.origin(),
    );
    assert!(
        wait_for(Duration::from_secs(2), || secondary_heartbeat.exists()).await,
        "expected a walpin heartbeat in the secondary backend's own sidecar"
    );
    assert!(
        !main_heartbeat.exists(),
        "a span scoped to the secondary backend's origin must never produce \
             a heartbeat in the main backend's sidecar"
    );

    let body = std::fs::read_to_string(&secondary_heartbeat).unwrap();
    let hb: crate::walpin::WalpinHeartbeat = serde_json::from_str(&body).unwrap();
    assert_eq!(hb.oldest_tx_label.as_deref(), Some("graph_traverse_read"));
    assert_eq!(
        hb.attribution_basis.as_deref(),
        Some("origin"),
        "a Secondary-view winner is always Database-origin-backed — never fallback"
    );

    drop(tx_handle);
    assert!(
        wait_for(Duration::from_secs(2), || !secondary_heartbeat.exists()).await,
        "secondary heartbeat must be removed once its span clears"
    );
    assert!(
        !main_heartbeat.exists(),
        "the main sidecar must have stayed untouched for the whole tick sequence"
    );

    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("session sweep task should exit within 1s")
        .expect("session sweep task panicked");
}

/// ADR-091 Amendment 3: a `run_checkpoint_task` instance for backend A
/// (`is_main: false`, a `Secondary` filter scoped to A's own identity)
/// must never observe a span registered against a DIFFERENT backend's
/// `Database` origin, nor an `Unscoped` span — a `Secondary` filter never
/// falls back to `Unscoped` (that fallback is the main view's alone).
/// Drives the real task for several ticks and asserts neither the
/// captured `tracing` emissions nor backend A's own sidecar ever name
/// either span.
#[tokio::test]
#[serial(tx_registry, checkpoint_skip_metrics, khive_walpin_sidecar_env)]
async fn checkpoint_task_ignores_span_registered_against_other_backend_origin_and_unscoped() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let dir_a = tempfile::tempdir().unwrap();
    let dir_b = tempfile::tempdir().unwrap();
    let pool_a = file_pool(&dir_a.path().join("backend_a.db"));
    // Only used to mint a real, distinct `DbIdentity` for backend B — no
    // checkpoint task is spawned for it.
    let pool_b = file_pool(&dir_b.path().join("backend_b.db"));
    let sidecar_a = crate::walpin::sidecar_dir_for(pool_a.canonical_path().expect("file-backed"));

    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };
    let _tracing_guard = tracing::subscriber::set_default(subscriber);

    let _b_origin_handle = khive_storage::tx_registry::register_scoped(
        Some("b_origin_span_ignored_by_a".to_string()),
        pool_b.origin(),
    );
    let _unscoped_handle = khive_storage::tx_registry::register(Some(
        "unscoped_span_ignored_by_secondary".to_string(),
    ));

    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        tx_warn_secs: Duration::from_millis(1),
        tx_max_age_secs: Duration::from_millis(1),
        ..CheckpointConfig::default()
    };
    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let handle = tokio::spawn(run_checkpoint_task(
        pool_a,
        cfg,
        None,
        shutdown_rx,
        false, // is_main: backend A is a secondary backend here
    ));

    // No positive condition to poll for — this asserts an absence over a
    // bounded run of several ticks, mirroring
    // `checkpoint_task_emits_no_age_alert_for_an_empty_registry`'s same
    // fixed-window shape (there is nothing to wait-until for a negative).
    tokio::time::sleep(Duration::from_millis(60)).await;
    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should exit within 1s")
        .expect("checkpoint task panicked");

    let events = buffer.lock().unwrap();
    assert!(
        events.iter().all(|e| {
            e.tx_label.as_deref() != Some("b_origin_span_ignored_by_a")
                && e.tx_label.as_deref() != Some("unscoped_span_ignored_by_secondary")
        }),
        "backend A's Secondary filter must never emit an age alert naming a span \
             registered against a different backend's origin or an Unscoped span, got: \
             {events:?}"
    );
    assert!(
        !sidecar_a
            .join(format!("{}.json", std::process::id()))
            .exists(),
        "backend A's own sidecar must never gain a heartbeat from a span it does not own"
    );
}

/// ADR-091 Amendment 3: a secondary backend's own `run_checkpoint_task`
/// must detect a stall on its OWN backend (never main-only ownership) —
/// both the Plank 1 age-sweep emission and the sidecar heartbeat, with
/// `attribution_basis="origin"` (a `Secondary` filter winner is always
/// `Database`-origin-backed, never the `Unscoped` fallback) and a
/// nonzero reflected age.
#[tokio::test]
#[serial(tx_registry, checkpoint_skip_metrics, khive_walpin_sidecar_env)]
async fn checkpoint_task_detects_and_enumerates_secondary_backend_stall() {
    if crate::test_process::run_in_child(|command| {
        command.env("KHIVE_WALPIN_SIDECAR", "1");
    }) {
        return;
    }

    let dir = tempfile::tempdir().unwrap();
    let pool = file_pool(&dir.path().join("secondary_stall.db"));
    let sidecar_dir = crate::walpin::sidecar_dir_for(pool.canonical_path().expect("file-backed"));

    let buffer = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let subscriber = CaptureSubscriber {
        events: std::sync::Arc::clone(&buffer),
    };
    let _tracing_guard = tracing::subscriber::set_default(subscriber);

    let tx_handle = khive_storage::tx_registry::register_scoped(
        Some("secondary_stall_test".to_string()),
        pool.origin(),
    );

    let cfg = CheckpointConfig {
        interval: Duration::from_millis(10),
        tx_warn_secs: Duration::from_millis(5),
        tx_max_age_secs: Duration::from_millis(500),
        ..CheckpointConfig::default()
    };
    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let pid = std::process::id();
    let heartbeat_path = sidecar_dir.join(format!("{pid}.json"));
    let handle = tokio::spawn(run_checkpoint_task(
        pool,
        cfg,
        None,
        shutdown_rx,
        false, // is_main: this is a secondary backend's own checkpoint task
    ));

    assert!(
        wait_for(Duration::from_secs(2), || heartbeat_path.exists()).await,
        "expected a walpin heartbeat once the secondary backend's own span crossed \
             tx_warn_secs"
    );
    let body = std::fs::read_to_string(&heartbeat_path).unwrap();
    let hb: crate::walpin::WalpinHeartbeat = serde_json::from_str(&body).unwrap();
    assert_eq!(hb.oldest_tx_label.as_deref(), Some("secondary_stall_test"));
    assert_eq!(
        hb.attribution_basis.as_deref(),
        Some("origin"),
        "a Secondary-view winner is always Database-origin-backed — never fallback"
    );
    assert!(
        hb.oldest_tx_age_secs > 0.0,
        "the heartbeat must reflect a nonzero stale age for the secondary backend's own \
             span, got {hb:?}"
    );

    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(1), handle)
        .await
        .expect("checkpoint task should exit within 1s")
        .expect("checkpoint task panicked");

    drop(tx_handle);

    let events = buffer.lock().unwrap();
    assert!(
        events.iter().any(|e| {
            e.tx_label.as_deref() == Some("secondary_stall_test")
                && e.message
                    .as_deref()
                    .is_some_and(|m| m.contains("ADR-091 Plank 1"))
        }),
        "expected the secondary backend's own checkpoint task to emit a Plank 1 age alert \
             for its own stalled span, got: {events:?}"
    );
}

#[test]
fn backfill_gap_arithmetic_against_real_connection() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("backfill_gap.db");
    let pool = file_pool(&path);
    let writer = pool.try_writer().expect("acquire writer");
    let conn = writer.conn();

    conn.execute_batch("CREATE TABLE t (v INTEGER)").unwrap();
    conn.execute_batch("INSERT INTO t (v) VALUES (1)").unwrap();

    let observation =
        query_backfill_gap(conn).expect("PRAGMA wal_checkpoint(PASSIVE) must succeed");
    // Nothing pins the WAL open in this test (no concurrent reader), so a
    // PASSIVE checkpoint fully drains what it just wrote: the one-row
    // backfill gap (log - checkpointed) is zero.
    assert!(
        observation.log_frames >= observation.checkpointed_frames,
        "checkpointed frames cannot exceed log frames"
    );
    assert_eq!(
        observation.log_frames - observation.checkpointed_frames,
        0,
        "an unpinned WAL must fully checkpoint under PASSIVE"
    );
}

#[test]
fn truncate_no_progress_probe_logs_backfill_gap_without_a_pin_depth_field() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("backfill_gap_log.db");
    let pool = file_pool(&path);
    let writer = pool.try_writer().expect("acquire writer");
    writer
        .conn()
        .execute_batch("CREATE TABLE t (v INTEGER); INSERT INTO t VALUES (1)")
        .unwrap();

    let events = capture(|| log_backfill_gap(&pool, writer.conn()));
    let event = events
        .iter()
        .find(|event| {
            event
                .message
                .as_deref()
                .is_some_and(|message| message.contains("WAL backfill gap"))
        })
        .expect("a successful PASSIVE probe logs its backfill gap");
    assert!(event.backfill_gap_frames.is_some());
    assert_eq!(event.legacy_wal_pin_depth, None);
}

#[test]
fn backfill_gap_arithmetic_on_in_memory_pool_errors_cleanly() {
    // In-memory databases report `log = -1` (no WAL); the pragma read
    // itself does not panic and the caller (`log_backfill_gap`) treats
    // any error as a logged warning, never a crash.
    let cfg = PoolConfig {
        path: None,
        ..PoolConfig::default()
    };
    let pool = ConnectionPool::new(cfg).expect("in-memory pool");
    let writer = pool.try_writer().expect("acquire writer");
    // Either an explicit error or a nonsensical negative `log` value is
    // acceptable here — the requirement is just "does not panic".
    let _ = query_backfill_gap(writer.conn());
}

/// #1849: a canonical filesystem identity is an OS path, not a display
/// label. Distinct non-UTF-8 Unix paths can render to the same lossy
/// string and must still occupy distinct backend telemetry slots.
#[cfg(unix)]
#[test]
fn routine_wal_backend_key_preserves_non_utf8_path_bytes() {
    use std::ffi::OsString;
    use std::os::unix::ffi::OsStringExt;

    let path_a = PathBuf::from(OsString::from_vec(b"/tmp/khive-wal-\x80.db".to_vec()));
    let path_b = PathBuf::from(OsString::from_vec(b"/tmp/khive-wal-\x81.db".to_vec()));
    assert_eq!(
        path_a.display().to_string(),
        path_b.display().to_string(),
        "fixture must reproduce the lossy display-label collision"
    );
    assert_ne!(
        checkpoint_db_key_from_path(Some(&path_a)),
        checkpoint_db_key_from_path(Some(&path_b)),
        "backend keys must retain the canonical path's exact OS bytes"
    );
}

/// #1849: the periodic checkpoint's own PASSIVE row is the monitoring
/// sample. One tick must not issue the old no-arg probe followed by a
/// second PASSIVE, and the stored sample must distinguish logical
/// backlog from the physical sidecar high-water mark.
#[test]
#[serial(checkpoint_skip_metrics)]
fn routine_checkpoint_records_one_pass_logical_and_physical_wal_sample() {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("routine_wal_sample.db");
    let pool = file_pool(&path);

    {
        let writer = pool.try_writer().expect("writer");
        writer
            .conn()
            .execute_batch(
                "PRAGMA wal_autocheckpoint=0; \
                     CREATE TABLE t (id INTEGER PRIMARY KEY, payload TEXT); \
                     INSERT INTO t VALUES (0, 'seed');",
            )
            .unwrap();
    }

    let reader =
        rusqlite::Connection::open_with_flags(&path, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY)
            .unwrap();
    reader.execute_batch("BEGIN").unwrap();
    let _: i64 = reader
        .query_row("SELECT COUNT(*) FROM t", [], |row| row.get(0))
        .unwrap();

    {
        let writer = pool.try_writer().expect("writer");
        writer.conn().execute_batch("BEGIN IMMEDIATE").unwrap();
        for id in 1..=256_i64 {
            writer
                .conn()
                .execute("INSERT INTO t VALUES (?1, printf('%.*c', 2048, 'x'))", [id])
                .unwrap();
        }
        writer.conn().execute_batch("COMMIT").unwrap();
    }

    let checkpoint_conn = pool.open_standalone_writer().unwrap();
    let pragma_calls = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let pragma_calls_from_hook = Arc::clone(&pragma_calls);
    checkpoint_conn
        .authorizer(Some(move |context: rusqlite::hooks::AuthContext<'_>| {
            if matches!(
                context.action,
                AuthAction::Pragma { pragma_name, .. }
                    if pragma_name.eq_ignore_ascii_case("wal_checkpoint")
            ) {
                pragma_calls_from_hook.fetch_add(1, Ordering::SeqCst);
            }
            Authorization::Allow
        }))
        .unwrap();

    checkpoint_once(
        &pool,
        &checkpoint_conn,
        &CheckpointConfig::default(),
        &mut TruncateState::default(),
    )
    .unwrap();
    checkpoint_conn
        .authorizer(None::<fn(rusqlite::hooks::AuthContext<'_>) -> Authorization>)
        .unwrap();

    assert_eq!(
        pragma_calls.load(Ordering::SeqCst),
        1,
        "one routine tick must issue exactly one PASSIVE checkpoint"
    );
    let pinned = routine_wal_observation(&pool).expect("routine sample");
    assert_eq!(
        pinned.busy, 0,
        "a pinned reader is not checkpoint-lock contention"
    );
    let first_timing = checkpoint_timing(&pool);
    assert_eq!(first_timing.ticks, 1);
    assert_eq!(
        first_timing.busy_ticks, 0,
        "pending frames must not count as busy"
    );
    assert!(pinned.log_frames > 0, "the test must create WAL frames");
    assert!(
        pinned.pending_frames > 0,
        "the old reader must leave a logical backlog: {pinned:?}"
    );
    assert_eq!(
        pinned.pending_frames,
        pinned.log_frames.saturating_sub(pinned.checkpointed_frames)
    );
    assert!(
        pinned.physical_wal_bytes.is_some_and(|bytes| bytes > 0),
        "the physical sidecar high-water must be reported separately: {pinned:?}"
    );

    reader.execute_batch("COMMIT").unwrap();
    checkpoint_once(
        &pool,
        &checkpoint_conn,
        &CheckpointConfig::default(),
        &mut TruncateState::default(),
    )
    .unwrap();
    let drained = routine_wal_observation(&pool).expect("drained routine sample");
    let drained_timing = checkpoint_timing(&pool);
    assert_eq!(drained_timing.ticks, first_timing.ticks + 1);
    assert!(drained_timing.elapsed_us_sum >= first_timing.elapsed_us_sum);
    assert!(drained_timing.elapsed_us_max >= first_timing.elapsed_us_max);
    assert_eq!(drained_timing.busy_ticks, 0);
    assert_eq!(drained.pending_frames, 0, "unpinned PASSIVE must drain");
    assert!(
        drained.physical_wal_bytes.is_some_and(|bytes| bytes > 0),
        "PASSIVE may reuse rather than shrink the physical WAL; the two gauges must remain \
             independently visible: {drained:?}"
    );
}

#[test]
#[serial(checkpoint_skip_metrics)]
fn routine_checkpoint_timing_records_real_call_and_debug_fields() {
    let dir = tempfile::tempdir().unwrap();
    let pool = file_pool(&dir.path().join("timed_tick.db"));
    let conn = checkpoint_conn(&pool);
    conn.execute_batch("CREATE TABLE t (x INTEGER); INSERT INTO t VALUES (1);")
        .unwrap();
    let (entered_tx, entered_rx) = std::sync::mpsc::sync_channel(0);
    let (release_tx, release_rx) = std::sync::mpsc::sync_channel(0);
    let release_rx = Mutex::new(release_rx);
    conn.authorizer(Some(move |context: rusqlite::hooks::AuthContext<'_>| {
        if matches!(context.action, AuthAction::Pragma { pragma_name, .. }
            if pragma_name.eq_ignore_ascii_case("wal_checkpoint"))
        {
            entered_tx.send(()).unwrap();
            release_rx
                .lock()
                .unwrap()
                .recv_timeout(Duration::from_secs(5))
                .unwrap();
        }
        Authorization::Allow
    }))
    .unwrap();
    let tick_pool = Arc::clone(&pool);
    let tick = std::thread::spawn(move || {
        let mut result = None;
        let events = capture(|| {
            result = Some(checkpoint_once(
                &tick_pool,
                &conn,
                &CheckpointConfig {
                    truncate_high_water_pages: u64::MAX,
                    ..CheckpointConfig::default()
                },
                &mut TruncateState::default(),
            ));
        });
        (result.unwrap(), events)
    });
    entered_rx.recv_timeout(Duration::from_secs(5)).unwrap();
    let during = checkpoint_timing(&pool);
    release_tx.send(()).unwrap();
    let (result, events) = tick.join().unwrap();
    result.unwrap();
    assert_eq!(
        during.ticks, 0,
        "in-flight call must not publish partial counters"
    );
    let timing = checkpoint_timing(&pool);
    assert_eq!(
        timing.ticks, 1,
        "one actual PASSIVE call must advance the count"
    );
    assert!(
        timing.elapsed_us_sum > 0,
        "channel-held checkpoint call must record elapsed time"
    );
    assert_eq!(timing.elapsed_us_max, timing.elapsed_us_sum);
    assert_eq!(timing.busy_ticks, 0);
    assert_eq!(timing.error_ticks, 0);
    let issued: Vec<_> = events
        .iter()
        .filter(|event| event.message.as_deref() == Some("WAL checkpoint issued"))
        .collect();
    assert_eq!(issued.len(), 1);
    assert_eq!(issued[0].elapsed_us, Some(timing.elapsed_us_sum));
    assert_eq!(issued[0].busy, Some(0));
}

#[test]
#[serial(checkpoint_skip_metrics)]
fn routine_checkpoint_timing_counts_sqlite_busy_from_competing_checkpoint() {
    struct BusyGate {
        entered: std::sync::mpsc::SyncSender<()>,
        release: std::sync::mpsc::Receiver<()>,
    }
    static BUSY_GATE: Mutex<Option<BusyGate>> = Mutex::new(None);
    fn hold_checkpoint_lock(_attempt: i32) -> bool {
        let gate = BUSY_GATE
            .lock()
            .unwrap()
            .take()
            .expect("armed busy handler");
        gate.entered.send(()).unwrap();
        gate.release.recv_timeout(Duration::from_secs(5)).unwrap();
        false
    }

    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("busy_checkpoint.db");
    let pool = file_pool(&path);
    let conn = checkpoint_conn(&pool);
    conn.execute_batch(
        "PRAGMA wal_autocheckpoint=0; CREATE TABLE t (x INTEGER); INSERT INTO t VALUES (1);",
    )
    .unwrap();
    let reader =
        rusqlite::Connection::open_with_flags(&path, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY)
            .unwrap();
    reader.execute_batch("BEGIN").unwrap();
    let _: i64 = reader
        .query_row("SELECT COUNT(*) FROM t", [], |row| row.get(0))
        .unwrap();
    conn.execute_batch("INSERT INTO t VALUES (2);").unwrap();
    let config = CheckpointConfig {
        truncate_high_water_pages: u64::MAX,
        ..CheckpointConfig::default()
    };
    let first_pages = checkpoint_once(&pool, &conn, &config, &mut TruncateState::default())
        .expect("initial PASSIVE observation");
    assert!(first_pages > 0);
    let first_sample = routine_wal_observation(&pool).expect("initial routine sample");

    let (entered_tx, entered_rx) = std::sync::mpsc::sync_channel(0);
    let (release_tx, release_rx) = std::sync::mpsc::sync_channel(0);
    *BUSY_GATE.lock().unwrap() = Some(BusyGate {
        entered: entered_tx,
        release: release_rx,
    });
    let competing = std::thread::spawn(move || {
        let checkpoint = rusqlite::Connection::open(path).unwrap();
        checkpoint.busy_handler(Some(hold_checkpoint_lock)).unwrap();
        checkpoint.query_row("PRAGMA wal_checkpoint(FULL)", [], |row| {
            row.get::<_, i64>(0)
        })
    });
    entered_rx
        .recv_timeout(Duration::from_secs(5))
        .expect("FULL checkpoint holds CKPT lock while waiting on reader");
    let mut result = None;
    let events = capture(|| {
        result = Some(checkpoint_once(
            &pool,
            &conn,
            &config,
            &mut TruncateState::default(),
        ));
        log_backfill_gap(&pool, &conn);
    });
    release_tx.send(()).unwrap();
    let competing_busy = competing.join().unwrap().unwrap();
    reader.execute_batch("COMMIT").unwrap();
    assert!(matches!(
        result.unwrap(),
        Err(rusqlite::Error::SqliteFailure(code, _))
            if code.code == rusqlite::ErrorCode::DatabaseBusy
    ));
    assert_eq!(competing_busy, 1);
    assert_eq!(
        routine_wal_observation(&pool),
        Some(first_sample),
        "a real busy row must preserve the last valid routine sample"
    );
    assert_eq!(last_observed_wal_pages(), Some(first_pages));
    let timing = checkpoint_timing(&pool);
    assert_eq!(timing.ticks, 2);
    assert_eq!(
        timing.busy_ticks, 1,
        "SQLite busy result must increment busy ticks"
    );
    assert_eq!(timing.error_ticks, 0);
    let issued = events
        .iter()
        .find(|event| {
            event.message.as_deref()
                == Some("WAL PASSIVE checkpoint returned a busy row; frame observation unavailable")
        })
        .expect("busy tick record");
    assert_eq!(issued.busy, Some(1), "tick record must retain SQLite busy");
    assert!(issued.elapsed_us.is_some());
    let gap = events
        .iter()
        .find(|event| {
            event.message.as_deref()
                == Some("ADR-091 Plank C: WAL backfill gap unavailable after TRUNCATE")
        })
        .expect("busy probe must report an unavailable backfill gap");
    assert_eq!(gap.backfill_gap_frames, None);
}

#[test]
#[serial(checkpoint_skip_metrics)]
fn routine_checkpoint_timing_counts_errors_and_excludes_post_truncate_probes() {
    let dir = tempfile::tempdir().unwrap();
    let pool = file_pool(&dir.path().join("failed_tick.db"));
    let conn = checkpoint_conn(&pool);
    conn.authorizer(Some(|context: rusqlite::hooks::AuthContext<'_>| {
        if matches!(context.action, AuthAction::Pragma { pragma_name, .. }
            if pragma_name.eq_ignore_ascii_case("wal_checkpoint"))
        {
            Authorization::Deny
        } else {
            Authorization::Allow
        }
    }))
    .unwrap();
    let result = checkpoint_once(
        &pool,
        &conn,
        &CheckpointConfig::default(),
        &mut TruncateState::default(),
    );
    conn.authorizer(None::<fn(rusqlite::hooks::AuthContext<'_>) -> Authorization>)
        .unwrap();
    assert!(result.is_err());
    let timing = checkpoint_timing(&pool);
    assert_eq!(timing.ticks, 1);
    assert_eq!(timing.error_ticks, 1);
    assert_eq!(timing.busy_ticks, 0);
    query_wal_pages(&pool, &conn);
    assert_eq!(
        checkpoint_timing(&pool),
        timing,
        "post-TRUNCATE observation is not a routine tick"
    );
}

#[test]
#[serial(checkpoint_skip_metrics)]
fn failed_post_truncate_measurement_preserves_last_sample_and_failure_streak() {
    let dir = tempfile::tempdir().unwrap();
    let pool = file_pool(&dir.path().join("unmeasured_truncate.db"));
    let conn = checkpoint_conn(&pool);
    let previous_pages = LAST_WAL_PAGES.swap(73, Ordering::Relaxed);
    let previous_attempts = TRUNCATE_ATTEMPTS.load(Ordering::Relaxed);
    let previous_failures = TRUNCATE_CONSECUTIVE_FAILURES.load(Ordering::Relaxed);
    let checkpoint_calls = Arc::new(AtomicUsize::new(0));
    let authorizer_calls = Arc::clone(&checkpoint_calls);
    conn.authorizer(Some(move |context: rusqlite::hooks::AuthContext<'_>| {
        if matches!(context.action, AuthAction::Pragma { pragma_name, .. }
            if pragma_name.eq_ignore_ascii_case("wal_checkpoint"))
        {
            // Allow TRUNCATE, then fail exactly its post-attempt PASSIVE
            // re-measurement. The later backfill-gap probe may still run.
            if authorizer_calls.fetch_add(1, Ordering::SeqCst) == 1 {
                Authorization::Deny
            } else {
                Authorization::Allow
            }
        } else {
            Authorization::Allow
        }
    }))
    .unwrap();
    let config = CheckpointConfig {
        warn_pages: 10,
        truncate_high_water_pages: 0,
        truncate_min_interval: Duration::ZERO,
        ..CheckpointConfig::default()
    };
    let mut state = TruncateState {
        consecutive_failures: 2,
        ..TruncateState::default()
    };
    let events = capture(|| {
        let _ = maybe_truncate(&pool, &conn, &config, 73, &mut state);
    });
    conn.authorizer(None::<fn(rusqlite::hooks::AuthContext<'_>) -> Authorization>)
        .unwrap();
    assert!(checkpoint_calls.load(Ordering::SeqCst) >= 2);
    assert!(state.last_attempt.is_some());
    assert_eq!(truncate_attempts(), previous_attempts + 1);
    assert_eq!(last_observed_wal_pages(), Some(73));
    assert_eq!(state.consecutive_failures, 2);
    assert_eq!(truncate_consecutive_failures(), 2);
    assert!(events.iter().any(|event| event.message.as_deref()
        == Some("WAL TRUNCATE progress unmeasured; checking possible holders in this process and others")));
    assert!(truncate_needs_attribution(73, None));
    assert!(truncate_needs_attribution(73, Some(73)));
    assert!(!truncate_needs_attribution(73, Some(0)));

    let events = capture(|| note_truncate_outcome(&config, Some(20), &mut state));
    assert_eq!(state.consecutive_failures, 3);
    assert_eq!(truncate_consecutive_failures(), 3);
    assert_eq!(
        events
            .iter()
            .filter(|event| event.message.as_deref()
                == Some("WAL TRUNCATE has failed to clear WAL pressure for 3 consecutive attempts"))
            .count(),
        1,
        "unmeasured attempt must preserve the streak for the next measured failure"
    );
    LAST_WAL_PAGES.store(previous_pages, Ordering::Relaxed);
    TRUNCATE_ATTEMPTS.store(previous_attempts, Ordering::Relaxed);
    TRUNCATE_CONSECUTIVE_FAILURES.store(previous_failures, Ordering::Relaxed);
}

#[test]
fn checkpoint_timing_accumulates_per_store_and_saturates() {
    let dir = tempfile::tempdir().unwrap();
    let a = file_pool(&dir.path().join("a.db"));
    let b = file_pool(&dir.path().join("b.db"));
    record_checkpoint_timing(&a, 17, Some(0));
    record_checkpoint_timing(&a, 31, Some(1));
    record_checkpoint_timing(&a, 7, None);
    record_checkpoint_timing(&b, 3, Some(0));
    assert_eq!(
        checkpoint_timing(&a),
        CheckpointTiming {
            ticks: 3,
            elapsed_us_sum: 55,
            elapsed_us_max: 31,
            busy_ticks: 1,
            error_ticks: 1,
        }
    );
    assert_eq!(
        checkpoint_timing(&b),
        CheckpointTiming {
            ticks: 1,
            elapsed_us_sum: 3,
            elapsed_us_max: 3,
            busy_ticks: 0,
            error_ticks: 0,
        }
    );
    checkpoint_timings().lock().unwrap().insert(
        checkpoint_db_key(&a),
        CheckpointTiming {
            ticks: u64::MAX,
            elapsed_us_sum: u64::MAX,
            elapsed_us_max: 31,
            busy_ticks: u64::MAX,
            error_ticks: u64::MAX,
        },
    );
    record_checkpoint_timing(&a, 1, Some(1));
    assert_eq!(
        checkpoint_timing(&a).elapsed_us_sum,
        u64::MAX,
        "elapsed sum must saturate on the first overflowing addition"
    );
    record_checkpoint_timing(&a, u64::MAX, None);
    assert_eq!(
        checkpoint_timing(&a),
        CheckpointTiming {
            ticks: u64::MAX,
            elapsed_us_sum: u64::MAX,
            elapsed_us_max: u64::MAX,
            busy_ticks: u64::MAX,
            error_ticks: u64::MAX,
        }
    );
}