khive-pack-knowledge 0.16.0

Knowledge verb pack — lore corpus (atoms/domains), TF-IDF retrieval, concept registration
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
use super::*;
use khive_storage::types::{SqlRow, StorageResult};
use std::sync::{Arc, Mutex};

#[path = "rerank_blend_tests.rs"]
mod rerank_blend_tests;

#[cfg(feature = "namespace-trigram-proto")]
pub(super) type PrototypePhaseTimes = Arc<Mutex<HashMap<&'static str, (u128, u64)>>>;

#[cfg(feature = "namespace-trigram-proto")]
tokio::task_local! {
    static PROTOTYPE_PHASE_TIMES: PrototypePhaseTimes;
}

#[cfg(feature = "namespace-trigram-proto")]
pub(super) async fn with_prototype_phase_times<F: std::future::Future>(
    timings: PrototypePhaseTimes,
    future: F,
) -> F::Output {
    PROTOTYPE_PHASE_TIMES.scope(timings, future).await
}

// The ordinary path scopes atoms first; the two prototype paths still start at FTS.
fn is_namespace_existence_probe(sql: &str) -> bool {
    sql.starts_with(
        "SELECT 1 AS present FROM knowledge_atoms AS a INDEXED BY idx_knowledge_atoms_ns CROSS JOIN fts_knowledge",
    ) || sql.starts_with("SELECT 1 AS present FROM fts_")
}

#[cfg(feature = "namespace-trigram-proto")]
fn prototype_phase(sql: &str) -> &'static str {
    let prefixed = sql.contains("fts_knowledge_namespace_proto");
    if sql.starts_with("SELECT namespace_key FROM knowledge_fts_namespace_tokens") {
        "namespace_key"
    } else if sql.starts_with("SELECT count(*) AS frequency") {
        if prefixed {
            "term_frequency_prefixed"
        } else {
            "term_frequency_slot_table"
        }
    } else if sql.starts_with("SELECT rowid FROM") {
        if prefixed {
            "phase_a_rowids_prefixed"
        } else {
            "phase_a_rowids_slot_table"
        }
    } else if sql.starts_with("SELECT a.* FROM fts_") {
        if prefixed {
            "eligibility_fallback_prefixed"
        } else {
            "eligibility_fallback_slot_table"
        }
    } else if sql.starts_with("SELECT a.*, a.rowid AS rowid") {
        "phase_b_hydration"
    } else if is_namespace_existence_probe(sql) {
        if prefixed {
            "namespace_existence_prefixed"
        } else if sql.starts_with("SELECT 1 AS present FROM knowledge_atoms") {
            "namespace_existence_global"
        } else {
            "namespace_existence_slot_table"
        }
    } else if sql.starts_with("SELECT 1 AS present FROM knowledge_atoms") {
        "namespace_membership"
    } else {
        "other_lexical_read"
    }
}

#[cfg(feature = "namespace-trigram-proto")]
struct PrototypeTimingReader {
    inner: Box<dyn khive_storage::SqlReader>,
    timings: PrototypePhaseTimes,
}

#[cfg(feature = "namespace-trigram-proto")]
impl PrototypeTimingReader {
    fn record(&self, sql: &str, elapsed: std::time::Duration) {
        let mut timings = self.timings.lock().expect("prototype phase timings");
        let sample = timings.entry(prototype_phase(sql)).or_default();
        sample.0 += elapsed.as_nanos();
        sample.1 += 1;
    }
}

#[cfg(feature = "namespace-trigram-proto")]
#[async_trait::async_trait]
impl khive_storage::SqlReader for PrototypeTimingReader {
    async fn query_row(&mut self, statement: SqlStatement) -> StorageResult<Option<SqlRow>> {
        let started = std::time::Instant::now();
        let result = self.inner.query_row(statement.clone()).await;
        self.record(&statement.sql, started.elapsed());
        result
    }

    async fn query_all(&mut self, statement: SqlStatement) -> StorageResult<Vec<SqlRow>> {
        let started = std::time::Instant::now();
        let result = self.inner.query_all(statement.clone()).await;
        self.record(&statement.sql, started.elapsed());
        result
    }

    async fn query_scalar(&mut self, statement: SqlStatement) -> StorageResult<Option<SqlValue>> {
        let started = std::time::Instant::now();
        let result = self.inner.query_scalar(statement.clone()).await;
        self.record(&statement.sql, started.elapsed());
        result
    }

    async fn explain(&mut self, statement: SqlStatement) -> StorageResult<Vec<SqlRow>> {
        self.inner.explain(statement).await
    }
}

tokio::task_local! {
    static TERM_PROBES: Arc<Mutex<Vec<SqlStatement>>>;
}

struct TermRecordingReader {
    inner: Box<dyn khive_storage::SqlReader>,
    probes: Arc<Mutex<Vec<SqlStatement>>>,
}

impl TermRecordingReader {
    fn record(&self, statement: &SqlStatement) {
        if statement.sql.contains("fts_knowledge MATCH") {
            self.probes
                .lock()
                .expect("term probes")
                .push(statement.clone());
        }
    }
}

#[async_trait::async_trait]
impl khive_storage::SqlReader for TermRecordingReader {
    async fn query_row(&mut self, statement: SqlStatement) -> StorageResult<Option<SqlRow>> {
        self.record(&statement);
        self.inner.query_row(statement).await
    }

    async fn query_all(&mut self, statement: SqlStatement) -> StorageResult<Vec<SqlRow>> {
        self.record(&statement);
        self.inner.query_all(statement).await
    }

    async fn query_scalar(&mut self, statement: SqlStatement) -> StorageResult<Option<SqlValue>> {
        self.record(&statement);
        self.inner.query_scalar(statement).await
    }

    async fn explain(&mut self, statement: SqlStatement) -> StorageResult<Vec<SqlRow>> {
        self.inner.explain(statement).await
    }
}

pub(super) fn record_term_probes(
    inner: Box<dyn khive_storage::SqlReader>,
) -> Box<dyn khive_storage::SqlReader> {
    let inner: Box<dyn khive_storage::SqlReader> = match TERM_PROBES.try_with(Arc::clone) {
        Ok(probes) => Box::new(TermRecordingReader { inner, probes }),
        Err(_) => inner,
    };
    #[cfg(feature = "namespace-trigram-proto")]
    if let Ok(timings) = PROTOTYPE_PHASE_TIMES.try_with(Arc::clone) {
        return Box::new(PrototypeTimingReader { inner, timings });
    }
    inner
}

fn distinct_term_query(count: usize) -> String {
    (0..count)
        .map(|index| format!("distinctterm{index}"))
        .collect::<Vec<_>>()
        .join(" ")
}

fn probed_term(statement: &SqlStatement) -> &str {
    match &statement.params[0] {
        SqlValue::Text(term) => term,
        other => panic!("expected bound FTS term, got {other:?}"),
    }
}

#[tokio::test]
async fn distinct_term_fan_out_is_bounded_and_reports_truncation() {
    let runtime = KhiveRuntime::memory().expect("runtime");
    let token = runtime.authorize(Namespace::local()).expect("token");
    for query in [
        distinct_term_query(FTS_TERM_COUNT_LIMIT * 2),
        "alpha beta gamma".into(),
    ] {
        let terms = fts5_candidate_terms(&query);
        let probes = Arc::new(Mutex::new(Vec::new()));
        let response = TERM_PROBES
            .scope(
                probes.clone(),
                KnowledgeHandlers::search(
                    &runtime,
                    &token,
                    json!({"query": query, "rerank": false}),
                    &vamana::new_shared(),
                ),
            )
            .await
            .expect("bounded search");
        let probes = probes.lock().expect("term probes");
        assert_eq!(probes.len(), terms.len().min(FTS_TERM_COUNT_LIMIT));
        assert_eq!(
            probes.iter().map(probed_term).collect::<Vec<_>>(),
            terms
                .iter()
                .take(FTS_TERM_COUNT_LIMIT)
                .map(String::as_str)
                .collect::<Vec<_>>(),
            "admission must precede even the rarity-frequency probes"
        );
        assert_eq!(
            response["candidate_provenance"]["terms_truncated"],
            terms.len() > FTS_TERM_COUNT_LIMIT
        );
        assert_eq!(response["candidate_provenance"]["lexical"], "no_match");
    }
}

#[tokio::test]
async fn term_budget_is_shared_across_decomposed_sub_queries() {
    let runtime = KhiveRuntime::memory().expect("runtime");
    let token = runtime.authorize(Namespace::local()).expect("token");
    // Ten raw terms expand to twenty: the full query fits, but the
    // combined work of all three passes must still consume one budget.
    for count in [10, FTS_TERM_COUNT_LIMIT + 18] {
        let probes = Arc::new(Mutex::new(Vec::new()));
        let response = TERM_PROBES.scope(probes.clone(), KnowledgeHandlers::search(
                &runtime, &token,
                json!({"query": distinct_term_query(count), "decompose": true, "rerank": false}),
                &vamana::new_shared(),
            )).await.expect("decomposed bounded search");
        assert_eq!(
            probes.lock().expect("term probes").len(),
            FTS_TERM_COUNT_LIMIT
        );
        assert_eq!(response["candidate_provenance"]["terms_truncated"], true);
    }
}

#[tokio::test]
async fn term_bound_covers_staged_fallback_and_namespace_probes() {
    let runtime = KhiveRuntime::memory().expect("runtime");
    let query = distinct_term_query(FTS_TERM_COUNT_LIMIT * 2);
    let terms = fts5_candidate_terms(&query);
    let content = terms
        .iter()
        .map(|term| term.trim_matches('"'))
        .collect::<Vec<_>>()
        .join(" ");
    let sql = runtime.sql();
    let mut writer = sql.writer().await.expect("writer");
    for index in 0..4 {
        let (namespace, content) = if index < 3 {
            ("foreign", content.as_str())
        } else {
            ("local", terms.last().unwrap().trim_matches('"'))
        };
        writer.execute(SqlStatement {
                sql: "INSERT INTO knowledge_atoms (id, namespace, slug, name, content, tags, finalized, status, created_at, updated_at) VALUES (?1, ?2, ?3, ?3, ?4, '[]', 1, 'reviewed', 0, 0)".into(),
                params: vec![
                    SqlValue::Text(Uuid::new_v4().to_string()),
                    SqlValue::Text(namespace.into()),
                    SqlValue::Text(format!("term-bound-{index}")),
                    SqlValue::Text(content.into()),
                ],
                label: None,
            }).await.expect("seed bounded-stage fixture");
    }
    drop(writer);
    let budget = FtsTermBudget::new();
    let probes = Arc::new(Mutex::new(Vec::new()));
    let outcome = TERM_PROBES
        .scope(
            probes.clone(),
            with_phase_a_widen_ceiling_override(
                2,
                super::fetch_fts_candidates(
                    &runtime,
                    "local",
                    &query,
                    None,
                    &[],
                    &[],
                    5,
                    &budget,
                    LexicalStage::new(
                        LexicalPass::Full,
                        tokio::time::Instant::now(),
                        lexical_stage_budget(),
                    ),
                ),
            ),
        )
        .await
        .expect("bounded staged fetch");
    assert_eq!(outcome.state, LexicalCandidateState::NoMatch);
    assert!(outcome.atoms.is_empty() && outcome.timeout.is_none());
    assert!(budget.truncated());
    let probes = probes.lock().expect("term probes");
    let allowed: HashSet<_> = terms
        .iter()
        .take(FTS_TERM_COUNT_LIMIT)
        .map(String::as_str)
        .collect();
    assert!(probes
        .iter()
        .all(|statement| allowed.contains(probed_term(statement))));
    assert_eq!(
        probes
            .iter()
            .filter(|statement| statement.sql.starts_with("SELECT rowid"))
            .count(),
        FTS_TERM_COUNT_LIMIT
    );
    assert_eq!(
        probes
            .iter()
            .filter(|statement| statement.sql.starts_with("SELECT count(*) AS frequency"))
            .count(),
        FTS_TERM_COUNT_LIMIT
    );
    assert_eq!(
        probes
            .iter()
            .filter(|statement| statement.sql.starts_with("SELECT a.*"))
            .count(),
        FTS_TERM_COUNT_LIMIT
    );
    assert_eq!(
        probes
            .iter()
            .filter(|statement| is_namespace_existence_probe(&statement.sql))
            .count(),
        FTS_TERM_COUNT_LIMIT
    );
}

#[test]
fn lexical_candidate_state_merge_preserves_completed_and_timed_out_passes() {
    use LexicalCandidateState::*;
    for (states, expected) in [
        (vec![], NoMatch),
        (vec![NoMatch, NoMatch], NoMatch),
        (vec![NoMatch, Filtered], Filtered),
        (vec![Filtered, Matched], Matched),
        (vec![TimedOut, TimedOut], TimedOut),
        (vec![TimedOut, NoMatch], PartialTimeout),
        (vec![Matched, TimedOut], PartialTimeout),
        (vec![PartialTimeout, Matched], PartialTimeout),
    ] {
        assert_eq!(
            LexicalCandidateState::merge(&states),
            expected,
            "{states:?}"
        );
        let reversed: Vec<_> = states.into_iter().rev().collect();
        assert_eq!(LexicalCandidateState::merge(&reversed), expected);
    }
}

async fn seed_candidate_state_fixture(runtime: &KhiveRuntime) {
    let access = runtime.sql();
    let mut writer = access.writer().await.expect("writer");
    writer.execute(SqlStatement {
            sql: "INSERT INTO knowledge_atoms \
                  (id, namespace, slug, name, content, tags, finalized, status, created_at, updated_at) \
                  VALUES \
                  ('92700000-0000-0000-0000-000000000001', 'local', 'newest-unrelated', \
                   'Newest Unrelated', 'ordinary background content', '[]', 1, 'reviewed', 20, 20), \
                  ('92700000-0000-0000-0000-000000000002', 'local', 'filtered-match', \
                   'Filtered Match', 'zzfilteredzz', '[]', 0, 'draft', 10, 10)".into(),
            params: Vec::new(),
            label: None,
        }).await.expect("seed candidate state rows");
}

#[tokio::test]
async fn true_lexical_miss_does_not_return_newest_rows() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    seed_candidate_state_fixture(&runtime).await;
    let query = "zzgenuinelyabsentzz";
    let outcome = fetch_fts_candidates(&runtime, "local", query, None, &[], &[], 5)
        .await
        .expect("true lexical miss");
    assert!(
        outcome.atoms.is_empty(),
        "a true FTS miss must not return recent rows"
    );
    assert_eq!(outcome.state, LexicalCandidateState::NoMatch);
    assert!(outcome.timeout.is_none());
    let token = runtime.authorize(Namespace::local()).expect("local token");
    let response = KnowledgeHandlers::search(
        &runtime,
        &token,
        json!({"query": query, "rerank": false}),
        &vamana::new_shared(),
    )
    .await
    .expect("public search");
    assert_eq!(
        response,
        json!({
            "results": [], "total": 0,
            "candidate_provenance": {"lexical": "no_match", "fallback": "none", "terms_truncated": false},
        })
    );
    let lexical = KnowledgeHandlers::search(
        &runtime,
        &token,
        json!({"query": "ordinary", "rerank": false}),
        &vamana::new_shared(),
    )
    .await
    .expect("matching lexical control");
    assert_eq!(
        lexical["candidate_provenance"],
        json!({"lexical": "matched", "fallback": "none", "terms_truncated": false})
    );
    assert_eq!(lexical["results"][0]["slug"], "newest-unrelated");
    assert_eq!(
        lexical["results"][0]["score_provenance"],
        json!({
            "sources": ["lexical"], "embedding_rerank": false,
            "normalization": "s_over_s_plus_1", "calibrated": false,
        })
    );
}

#[tokio::test]
async fn lexical_candidate_state_distinguishes_filtered_match() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    seed_candidate_state_fixture(&runtime).await;
    let token = runtime.authorize(Namespace::local()).expect("local token");
    let ann = vamana::new_shared();
    for (include_drafts, state, total) in [(false, "filtered", 0), (true, "matched", 1)] {
        let response = KnowledgeHandlers::search(
            &runtime,
            &token,
            json!({"query": "zzfilteredzz", "rerank": false, "include_drafts": include_drafts}),
            &ann,
        )
        .await
        .expect("filtered search");
        assert_eq!(
            response["candidate_provenance"],
            json!({"lexical": state, "fallback": "none", "terms_truncated": false})
        );
        assert_eq!(response["total"], total);
        if include_drafts {
            assert_eq!(response["results"][0]["slug"], "filtered-match");
        }
    }
}

async fn fetch_fts_candidates(
    runtime: &KhiveRuntime,
    ns: &str,
    raw_query: &str,
    type_filter: Option<&str>,
    statuses: &[String],
    exclude_statuses: &[&str],
    fetch_limit: usize,
) -> Result<FtsFetchOutcome, RuntimeError> {
    super::fetch_fts_candidates(
        runtime,
        ns,
        raw_query,
        type_filter,
        statuses,
        exclude_statuses,
        fetch_limit,
        &FtsTermBudget::new(),
        LexicalStage::new(
            LexicalPass::Full,
            tokio::time::Instant::now(),
            lexical_stage_budget(),
        ),
    )
    .await
}

struct ProbeRecordingReader {
    inner: Box<dyn khive_storage::SqlReader>,
    probes: Vec<(SqlStatement, Vec<SqlRow>)>,
}

#[async_trait::async_trait]
impl khive_storage::SqlReader for ProbeRecordingReader {
    async fn query_row(&mut self, statement: SqlStatement) -> StorageResult<Option<SqlRow>> {
        self.inner.query_row(statement).await
    }

    async fn query_all(&mut self, statement: SqlStatement) -> StorageResult<Vec<SqlRow>> {
        let rows = self.inner.query_all(statement.clone()).await?;
        self.probes.push((statement, rows.clone()));
        Ok(rows)
    }

    async fn query_scalar(&mut self, statement: SqlStatement) -> StorageResult<Option<SqlValue>> {
        self.inner.query_scalar(statement).await
    }

    async fn explain(&mut self, statement: SqlStatement) -> StorageResult<Vec<SqlRow>> {
        self.inner.explain(statement).await
    }
}

#[tokio::test]
async fn compose_direct_handler_rejects_namespace_token_mismatch() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let token = runtime.authorize(Namespace::local()).expect("local token");
    let ann = vamana::new_shared();

    let err = KnowledgeHandlers::compose(
        &runtime,
        &token,
        json!({
            "namespace": "bench-arm-a",
            "query": "must reject before reading",
        }),
        &ann,
        HashMap::new(),
    )
    .await
    .expect_err("a local token must not elevate into a measurement arm");

    assert!(
        matches!(err, RuntimeError::InvalidInput(ref msg) if msg.contains("does not match authorized token namespace")),
        "unexpected error: {err:?}"
    );
}

#[test]
fn fts_candidate_terms_recalls_non_contiguous_terms() {
    assert_eq!(
        fts5_candidate_terms("alpha beta alpha and").join(" OR "),
        "\"alpha\" OR \"alphas\" OR \"beta\" OR \"betas\""
    );
    assert_eq!(
        fts5_candidate_terms("RAG").join(" OR "),
        "\"rag\" OR \"rags\""
    );
    assert_eq!(
        fts5_candidate_terms("the and").join(" OR "),
        "\"the and\"",
        "stop-only queries retain the exact-phrase fallback"
    );
}

#[tokio::test]
async fn rarity_probes_are_capped_and_sort_rare_terms_before_common_terms() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    seed_low_overlap_corpus(&runtime, 1_100, 20).await;
    let mut reader = ProbeRecordingReader {
        inner: runtime.sql().reader().await.expect("reader"),
        probes: Vec::new(),
    };
    let terms = [
        "\"term1\"",
        "\"term18\"",
        "\"missing\"",
        "\"term11\"",
        "\"synthetic\"",
    ];
    let ordered = rarest_fts_terms_first(
        &mut reader,
        terms.iter().map(|term| (*term).to_string()).collect(),
        &mut LexicalStage::new(
            LexicalPass::Full,
            tokio::time::Instant::now(),
            lexical_stage_budget(),
        ),
    )
    .await
    .expect("frequency probes");
    assert_eq!(
        ordered,
        ["\"term11\"", "\"term18\"", "\"synthetic\"", "\"term1\""]
    );
    assert_eq!(reader.probes.len(), terms.len());
    assert_eq!(
        reader
            .probes
            .iter()
            .map(|(_, rows)| rows.len())
            .collect::<Vec<_>>(),
        [1; 5],
        "each probe must materialize one aggregate row, including zero matches"
    );
    assert_eq!(
        reader
            .probes
            .iter()
            .map(|(_, rows)| row_i64(&rows[0], "frequency").expect("frequency"))
            .collect::<Vec<_>>(),
        [501, 55, 0, 55, 501],
        "rare counts stay exact and both common terms tie at the cap"
    );
    for (statement, rows) in &reader.probes {
        assert!(matches!(statement.params[1], SqlValue::Integer(501)));
        assert_eq!(rows[0].columns.len(), 1);
    }
}

#[tokio::test]
async fn fts_candidates_pin_the_rowid_prefix_boundary() {
    for (regular_count, best_is_admitted) in [
        (FTS_TERM_LIMIT * PHASE_A_OVERFETCH_FACTOR, false),
        (FTS_TERM_LIMIT - 1, true),
    ] {
        let runtime = KhiveRuntime::memory().expect("in-memory runtime");
        let access = runtime.sql();
        let mut writer = access.writer().await.expect("writer");
        writer
                .execute(SqlStatement {
                    sql: "WITH RECURSIVE entries(n) AS ( \
                              VALUES(1) UNION ALL SELECT n + 1 FROM entries WHERE n < ?1 \
                          ) \
                          INSERT INTO knowledge_atoms ( \
                              rowid, id, namespace, slug, name, content, tags, finalized, \
                              status, created_at, updated_at \
                          ) \
                          SELECT n, printf('92700000-0000-0000-0000-%012d', n), \
                              'local', printf('prefix-%06d', n), 'Prefix Document', \
                              'zzprefixzz padding padding padding padding padding padding padding', \
                              '[]', 1, 'reviewed', 0, 0 FROM entries"
                        .into(),
                    params: vec![SqlValue::Integer(regular_count as i64)],
                    label: None,
                })
                .await
                .expect("seed early matches");
        writer
            .execute(SqlStatement {
                sql: "INSERT INTO knowledge_atoms ( \
                              rowid, id, namespace, slug, name, content, tags, finalized, \
                              status, created_at, updated_at \
                          ) VALUES ( \
                              ?1, '92700000-0000-0000-0001-000000000000', 'local', \
                              'zzprefix-best', 'Prefix Document', ?2, '[]', 1, 'reviewed', 0, 0)"
                    .into(),
                params: vec![
                    SqlValue::Integer((regular_count + 1) as i64),
                    SqlValue::Text("zzprefixzz ".repeat(8)),
                ],
                label: None,
            })
            .await
            .expect("seed late repeated match");
        drop(writer);

        let mut reader = access.reader().await.expect("reader");
        let best = reader
            .query_row(SqlStatement {
                sql: "SELECT a.rowid, a.slug FROM fts_knowledge \
                          JOIN knowledge_atoms AS a ON a.rowid = fts_knowledge.rowid \
                          WHERE fts_knowledge MATCH ?1 AND a.namespace = 'local' \
                            AND a.deleted_at IS NULL \
                          ORDER BY bm25(fts_knowledge), a.slug LIMIT 1"
                    .into(),
                params: vec![SqlValue::Text("\"zzprefixzz\"".into())],
                label: None,
            })
            .await
            .expect("BM25 control query")
            .expect("BM25 match");
        assert_eq!(row_str(&best, "slug").as_deref(), Some("zzprefix-best"));
        assert_eq!(row_i64(&best, "rowid"), Some((regular_count + 1) as i64));
        drop(reader);

        let outcome = fetch_fts_candidates(
            &runtime,
            "local",
            "zzprefixzz",
            None,
            &[],
            &[],
            FTS_TERM_LIMIT,
        )
        .await
        .expect("bounded candidate fetch");
        assert!(outcome.timeout.is_none());
        assert_eq!(outcome.atoms.len(), FTS_TERM_LIMIT);
        assert_eq!(
            outcome
                .atoms
                .iter()
                .any(|atom| atom.slug == "zzprefix-best"),
            best_is_admitted,
            "BM25-best row admission must follow the rowid window; regular_count={regular_count}"
        );
    }
}

#[tokio::test]
async fn phase_a_limit_uses_index_order_without_sorting_matches() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    seed_low_overlap_corpus(&runtime, 1_000, 20).await;
    let access = runtime.sql();
    let mut reader = access.reader().await.expect("reader");
    let statement = phase_a_rowids_statement("\"term1\"", 3);
    for bounded in [statement.clone(), term_frequency_statement("\"term1\"")] {
        for (query, should_sort) in [
            (bounded.sql.clone(), false),
            (
                bounded
                    .sql
                    .replace("ORDER BY rowid", "ORDER BY bm25(fts_knowledge), rowid"),
                true,
            ),
        ] {
            let plan = reader
                .query_all(SqlStatement {
                    sql: format!("EXPLAIN QUERY PLAN {query}"),
                    params: bounded.params.clone(),
                    label: None,
                })
                .await
                .expect("query plan");
            assert!(!plan.is_empty());
            assert!(plan.iter().any(|row| {
                row_str(row, "detail")
                    .is_some_and(|detail| detail.contains("fts_knowledge VIRTUAL TABLE INDEX"))
            }));
            let sorts = plan.iter().any(|row| {
                row_str(row, "detail").is_some_and(|detail| detail.contains("TEMP B-TREE"))
            });
            assert_eq!(sorts, should_sort, "plan: {plan:?}");
        }
    }
    let rows = reader.query_all(statement).await.expect("bounded rowids");
    let ids: Vec<_> = rows
        .iter()
        .map(|row| row_i64(row, "rowid").unwrap())
        .collect();
    assert_eq!(ids.len(), 3);
    assert!(ids.windows(2).all(|pair| pair[0] < pair[1]));
}

/// Issue #2766. `rarest_fts_terms_first` fetches no candidates — it sorts
/// the terms — and before this bound it ran against the whole lexical
/// stage budget, after which the caller returned an EMPTY candidate list
/// for a stage that had not yet asked for a candidate. The probe now has
/// its own quarter-budget and its expiry falls back to the arrival order.
///
/// This arm fails on the unpatched code, where `atoms` is empty and the
/// state is `TimedOut`.
#[tokio::test(start_paused = true)]
async fn rarity_probe_expiry_keeps_the_lexical_arm() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    seed_low_overlap_corpus(&runtime, 1_000, 20).await;
    let stage_budget = std::time::Duration::from_secs(1);

    let outcome = with_lexical_stage_budget_override_ms(
        1_000,
        khive_storage::scope_request_read_deadline(
            stage_budget,
            // Past the quarter-budget (250 ms) after the first probe, and
            // short of the stage budget, so only the probe is cut off.
            with_fts_probe_deadline_advance_after_term(
                1,
                std::time::Duration::from_millis(300),
                fetch_fts_candidates(
                    &runtime,
                    "local",
                    "term1 term18",
                    None,
                    &[],
                    &[],
                    CANDIDATE_POOL,
                ),
            ),
        ),
    )
    .await
    .expect("a cut-off ordering probe must not fail the fetch");

    assert!(
        !outcome.atoms.is_empty(),
        "the ordering probe timing out must not discard the lexical arm"
    );
    assert!(
        outcome.timeout.is_some(),
        "the degradation must still be reported, not swallowed"
    );
    assert_eq!(
        outcome.state,
        LexicalCandidateState::PartialTimeout,
        "candidates in hand with a recorded timeout is partial, never clean"
    );
    let detail = outcome.timeout.expect("recorded timeout");
    assert_eq!(
        detail.phase,
        LexicalPhase::TermFrequency,
        "the reported phase must name the probe, not the fetch"
    );
}

/// The pairing that shows the fallback IS the candidate-list order and
/// that the probe is what produced the rarest-first ordering. One arm
/// cannot show both, so both run here over the same corpus and the same
/// per-term expiry, differing only in whether the probe was allowed to
/// finish.
///
/// The fallback order is NOT the caller's word order, and this arm is
/// written the way it is because the first version of it assumed that and
/// failed: `fts5_candidate_terms` ends in `expand_terms`, which sorts
/// (`scoring.rs`), so the candidate list is lexicographic whatever the
/// caller wrote. Both spellings therefore fall back to `term1` first, and
/// running both is the evidence for that rather than a repetition.
///
/// The term pair is chosen, not incidental. The corpus writes
/// `discusses topic termN` for N in 0..20, so every token matches 50 rows
/// EXCEPT `term1`, whose FTS prefix family also covers `term10`..`term19`
/// and therefore matches about 550. `term1` is the only common token
/// available, and it has to be paired with a token OUTSIDE its own prefix
/// family or the two match sets are nested and no assertion can separate
/// them. `term7` qualifies; `term18` does not, which is what an earlier
/// version of this arm got wrong.
///
/// The reading instrument is the FIRST atom. The merge loop walks the
/// per-term row vectors in order, so `atoms[0]` is the first row of the
/// first term the fetch actually queried, whatever the candidate cap does
/// to the rest.
#[tokio::test(start_paused = true)]
async fn rarity_probe_decides_the_order_and_its_expiry_falls_back_to_arrival() {
    let stage_budget = std::time::Duration::from_secs(1);

    // Anchored on the corpus's own surrounding words: a bare `term1` needle
    // is a prefix of `term18` and of `term10`, and would match rows the
    // assertion means to exclude.
    let topic = |term: &str| format!("topic {term} ");
    // Lexicographically first in the candidate list, hence first in the
    // fallback; `term7` is the rarer one, hence first when the probe runs.
    let fallback_first_term = "term1";
    for query in ["term1 term7", "term7 term1"] {
        // Probe allowed to finish: the RARE term is queried first whatever
        // the caller wrote.
        let runtime = KhiveRuntime::memory().expect("in-memory runtime");
        seed_low_overlap_corpus(&runtime, 1_000, 20).await;
        let ordered = with_lexical_stage_budget_override_ms(
            1_000,
            khive_storage::scope_request_read_deadline(
                stage_budget,
                with_fts_deadline_advance_after_term(
                    1,
                    stage_budget,
                    fetch_fts_candidates(&runtime, "local", query, None, &[], &[], CANDIDATE_POOL),
                ),
            ),
        )
        .await
        .expect("partial fetch");
        let ordered_first = ordered
            .atoms
            .first()
            .map(|atom| atom.content.clone())
            .expect("the probe-intact fetch must produce candidates");
        assert!(
            ordered_first.contains(&topic("term7")),
            "with the probe intact the rarer term is queried first whatever the \
                 caller wrote ({query}); got {} atoms, first content {ordered_first:?}",
            ordered.atoms.len()
        );

        // Probe cut off: the order is the one the caller wrote.
        let runtime = KhiveRuntime::memory().expect("in-memory runtime");
        seed_low_overlap_corpus(&runtime, 1_000, 20).await;
        let fallback = with_lexical_stage_budget_override_ms(
            1_000,
            khive_storage::scope_request_read_deadline(
                stage_budget,
                with_fts_probe_deadline_advance_after_term(
                    1,
                    std::time::Duration::from_millis(300),
                    with_fts_deadline_advance_after_term(
                        1,
                        stage_budget,
                        fetch_fts_candidates(
                            &runtime,
                            "local",
                            query,
                            None,
                            &[],
                            &[],
                            CANDIDATE_POOL,
                        ),
                    ),
                ),
            ),
        )
        .await
        .expect("partial fetch");
        assert!(
            !fallback.atoms.is_empty(),
            "the fallback must still produce candidates ({query})"
        );
        let fallback_first = fallback
            .atoms
            .first()
            .map(|atom| atom.content.clone())
            .expect("the fallback must still produce candidates");
        assert!(
            fallback_first.contains(&topic(fallback_first_term)),
            "with the probe cut off the fetch must use the candidate-list \
                 order, expected {fallback_first_term} first ({query}); got {} \
                 atoms, first content {fallback_first:?}",
            fallback.atoms.len()
        );
        // And the degradation is still reported rather than read as a miss.
        assert!(
            fallback.timeout.is_some(),
            "a cut-off probe must still be reported ({query})"
        );
    }
}

#[tokio::test(start_paused = true)]
async fn rare_term_survives_stage_expiry_independent_of_query_order() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    seed_low_overlap_corpus(&runtime, 1_000, 20).await;
    let deadline = std::time::Duration::from_secs(1);
    for query in ["term1 term18", "term18 term1"] {
        let outcome = khive_storage::scope_request_read_deadline(
            deadline,
            FTS_TEST_DEADLINE_ADVANCE.scope(
                FtsTestDeadlineAdvance {
                    after_completed_terms: 1,
                    by: deadline,
                },
                fetch_fts_candidates(&runtime, "local", query, None, &[], &[], CANDIDATE_POOL),
            ),
        )
        .await
        .expect("partial fetch");
        assert!(outcome.timeout.is_some());
        assert_eq!(outcome.state, LexicalCandidateState::PartialTimeout);
        assert_eq!(
            outcome.atoms.len(),
            50,
            "the rarer term must complete first"
        );
        assert!(outcome
            .atoms
            .iter()
            .all(|atom| atom.content.contains("term18")));
    }
}

/// Issue #1930: the old OR-joined query returned an all-or-nothing error
/// when it crossed the request deadline. The per-term fetch must instead
/// keep candidates from a completed term and report `timed_out`. Paused
/// Tokio time and the test-only per-term deadline control place expiry
/// exactly between two queries, so the assertion never depends on corpus
/// work taking longer than a machine-specific wall-clock budget.
///
/// Scope: this covers the per-term BOUNDARY only. Advancing only the
/// async clock means the post-boundary term is refused by the
/// pre-registration deadline check, never by an in-flight SQLite
/// interrupt — that path has its own wall-clock test below. The old
/// wall-clock old-query oracle (running the pre-fix OR-joined SQL over
/// this corpus against a tuned budget) is deliberately retired with the
/// machine-timing flake it depended on; the all-or-nothing behavior of a
/// single statement crossing its deadline is what the in-flight test
/// below pins.
#[tokio::test(start_paused = true)]
async fn per_term_fetch_degrades_at_controlled_deadline_boundary() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    const N: u32 = 1_000;
    const VOCAB: u32 = 20;
    seed_low_overlap_corpus(&runtime, N, VOCAB).await;

    let query = "term0 term1";
    let deadline = std::time::Duration::from_millis(650);

    let new_result = khive_storage::scope_request_read_deadline(
        deadline,
        FTS_TEST_DEADLINE_ADVANCE.scope(
            FtsTestDeadlineAdvance {
                after_completed_terms: 1,
                by: deadline,
            },
            fetch_fts_candidates(&runtime, "local", query, None, &[], &[], CANDIDATE_POOL),
        ),
    )
    .await;
    let outcome = new_result.expect(
        "the per-term fetch must return partial degradation when the controlled deadline \
             expires between term queries",
    );
    assert!(
        outcome.timeout.is_some(),
        "the controlled deadline must be observed"
    );
    assert_eq!(outcome.state, LexicalCandidateState::PartialTimeout);
    assert!(
        !outcome.atoms.is_empty(),
        "candidates from the completed term must survive degradation"
    );
    assert!(
        outcome.atoms.len() <= CANDIDATE_POOL,
        "partial pool must respect the fetch cap; got {}",
        outcome.atoms.len()
    );
}

/// Companion to the boundary test above: prove that a wall-clock
/// deadline expiring during this pack's read path surfaces the typed
/// `StorageError::Timeout` — never an untyped error — end to end through
/// the runtime's reader surface. The statement is structurally slow (a
/// 1000^3 cross join — billions of row operations on any machine), so
/// the deadline expires long before it could complete.
///
/// Scope: which arm of the deadline machinery fires is scheduling-
/// dependent — the deadline can latch at reader checkout, at read
/// registration, or mid-statement via the progress handler — and every
/// arm must yield the same typed timeout, which is exactly this test's
/// assertion. The mid-statement arm specifically (progress-handler
/// interrupt of an executing statement, proven by a probe that counts
/// progress callbacks) is deterministically covered where the mechanism
/// lives: `request_deadline_interrupts_statement_without_outer_timeout`
/// in `crates/khive-db/src/sql_bridge_tests.rs`, whose probe assertion fails
/// if SQLite work never started. This test does not re-prove the
/// in-flight arm; it pins the pack-visible contract over all arms.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn wall_clock_deadline_on_pack_read_path_surfaces_typed_timeout() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let deadline = std::time::Duration::from_millis(50);
    let result = khive_storage::scope_request_read_deadline(deadline, async {
        let sql = runtime.sql();
        let mut reader = sql.reader().await.expect("reader");
        reader
            .query_all(SqlStatement {
                sql: "WITH RECURSIVE numbers(value) AS (\
                          SELECT 1 UNION ALL SELECT value + 1 FROM numbers WHERE value < 1000\
                          ) SELECT SUM(a.value * b.value * c.value) \
                          FROM numbers AS a CROSS JOIN numbers AS b CROSS JOIN numbers AS c"
                    .into(),
                params: vec![],
                label: Some("knowledge-deadline-probe".into()),
            })
            .await
    })
    .await;
    assert!(
        matches!(result, Err(khive_storage::StorageError::Timeout { .. })),
        "a read crossing the wall-clock deadline must surface the typed \
             timeout whichever deadline arm fires; got {result:?}"
    );
}

/// Issue #1930 rework: the per-term loop used to `break` as soon as the
/// merged pool reached `fetch_limit`, checked *before* querying the next
/// term. A term that sorts first and alone has more matches than
/// `fetch_limit` then fills the pool on its own turn, so every later
/// term never gets queried at all — pool membership depended on query
/// word order. This seeds "alpha" with more rows than `fetch_limit` and
/// a disjoint, small "beta" set, then asserts beta's rows survive into
/// the returned pool. Against the pre-fix early-break loop this must
/// FAIL: alpha's own per-term query (capped at `fetch_limit`) already
/// fills `combined` to `fetch_limit` before the loop reaches "beta", so
/// "beta" is never queried and none of its rows can appear.
#[tokio::test]
async fn round_robin_merge_keeps_later_term_candidates_from_starving() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    {
        let access = runtime.sql();
        let mut writer = access.writer().await.expect("writer");
        writer
            .execute(SqlStatement {
                sql: "WITH RECURSIVE x(n) AS ( \
                              VALUES(0) UNION ALL SELECT n + 1 FROM x WHERE n < 9 \
                          ) \
                          INSERT INTO knowledge_atoms ( \
                              id, namespace, slug, name, content, tags, properties, finalized, \
                              status, source_uri, source_type, created_at, updated_at, deleted_at \
                          ) \
                          SELECT \
                              printf('90000000-0000-0000-0000-%012d', x.n), \
                              'local', printf('alpha-%02d', x.n), printf('Alpha %02d', x.n), \
                              'synthetic content about alpha only', '[]', NULL, 1, \
                              'reviewed', NULL, NULL, x.n, x.n, NULL \
                          FROM x"
                    .to_string(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed alpha rows");
        writer
            .execute(SqlStatement {
                sql: "WITH RECURSIVE x(n) AS ( \
                              VALUES(0) UNION ALL SELECT n + 1 FROM x WHERE n < 2 \
                          ) \
                          INSERT INTO knowledge_atoms ( \
                              id, namespace, slug, name, content, tags, properties, finalized, \
                              status, source_uri, source_type, created_at, updated_at, deleted_at \
                          ) \
                          SELECT \
                              printf('91000000-0000-0000-0000-%012d', x.n), \
                              'local', printf('beta-%02d', x.n), printf('Beta %02d', x.n), \
                              'synthetic content about beta only', '[]', NULL, 1, \
                              'reviewed', NULL, NULL, x.n, x.n, NULL \
                          FROM x"
                    .to_string(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed beta rows");
    }

    let fetch_limit = 5;
    let outcome =
        fetch_fts_candidates(&runtime, "local", "alpha beta", None, &[], &[], fetch_limit)
            .await
            .expect("fetch must not error");
    assert!(outcome.timeout.is_none());
    assert_eq!(outcome.state, LexicalCandidateState::Matched);
    assert_eq!(outcome.atoms.len(), fetch_limit);

    let beta_present = outcome
        .atoms
        .iter()
        .any(|atom| atom.slug.starts_with("beta-"));
    assert!(
        beta_present,
        "round-robin merge must keep the second term's candidates in the pool \
             even though the first term alone has more matches than fetch_limit; \
             got {:?}",
        outcome
            .atoms
            .iter()
            .map(|a| a.slug.as_str())
            .collect::<Vec<_>>()
    );
}

/// Issue #1930 Amendment 2: the lexical stage's own budget must not
/// cancel the rest of the request when it expires. Paused Tokio time
/// plus the existing per-term deadline control place the expiry
/// deterministically between two term queries, inside a nested
/// `scope_request_read_deadline` call mirroring exactly what
/// `search_core` does around `fetch_fts_candidates` (a narrow stage
/// scope nested inside a much longer outer one).
///
/// Before this change, `search_core` called `fetch_fts_candidates`
/// directly under whatever deadline the caller installed, so this same
/// expiry — with no separate inner scope to pop back out of — left the
/// *outer* deadline expired too, and `ensure_request_read_active`
/// called afterward, still nested in that one shared scope, returned
/// `Err`. Confirmed by running this test against the fetch called
/// directly (no stage-budget wrap) before adding the wrap: `still_active`
/// came back `Err(Timeout { .. })` — this assertion is the red-before
/// case for that call shape; the wrap makes it green.
#[tokio::test(start_paused = true)]
async fn lexical_stage_budget_expiry_does_not_cancel_the_request() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    const N: u32 = 1_000;
    const VOCAB: u32 = 20;
    seed_low_overlap_corpus(&runtime, N, VOCAB).await;

    let query = "term0 term1";
    // This test wraps `fetch_fts_candidates` in its own explicit
    // `scope_request_read_deadline`, exactly mirroring what `search_core`
    // does around it in production — it never goes through
    // `lexical_stage_budget()`'s override seam, so a literal duration is
    // enough here.
    let budget = std::time::Duration::from_millis(50);
    let outer_deadline = std::time::Duration::from_secs(60);

    let (fetch, still_active) = khive_storage::scope_request_read_deadline(outer_deadline, async {
        let fetch = khive_storage::scope_request_read_deadline(
            budget,
            FTS_TEST_DEADLINE_ADVANCE.scope(
                FtsTestDeadlineAdvance {
                    after_completed_terms: 1,
                    by: budget,
                },
                fetch_fts_candidates(&runtime, "local", query, None, &[], &[], CANDIDATE_POOL),
            ),
        )
        .await
        .expect("the fetch must not hard-error on its own stage budget");

        let still_active = khive_storage::ensure_request_read_active("test.lexical_stage_budget");
        (fetch, still_active)
    })
    .await;

    assert!(
        fetch.timeout.is_some(),
        "the lexical stage's own narrower budget must be observed"
    );
    assert!(
        !fetch.atoms.is_empty(),
        "candidates from the completed term must survive the stage degradation"
    );
    assert!(
        still_active.is_ok(),
        "the outer request deadline must still be active once the lexical \
             stage's own budget expires and its scope returns; got {still_active:?}"
    );
}

#[test]
fn lexical_stage_allowance_scales_only_with_admitted_terms_and_is_capped() {
    let base = lexical_stage_budget();
    let budget = FtsTermBudget::new();
    assert_eq!(
        lexical_stage_budget_for_terms(base, budget.available_for("zzmass")),
        base,
    );
    assert_eq!(
        lexical_stage_budget_for_terms(base, budget.available_for("zzmass zzlass")),
        std::time::Duration::from_millis(2_500),
    );
    let many = distinct_term_query(FTS_TERM_COUNT_LIMIT * 2);
    assert_eq!(
        lexical_stage_budget_for_terms(base, budget.available_for(&many)),
        std::time::Duration::from_millis(8_000),
    );
    budget.admit(fts5_candidate_terms(&many));
    assert_eq!(budget.available_for("zzmass zzlass"), 0);
    assert_eq!(lexical_stage_budget_for_terms(base, 0), base);
}

/// A second bounded term read must have time to run after the original
/// 2 s single-term allowance has elapsed. The clock moves only after the
/// first term's rows are collected, so the old fixed 2 s stage returned a
/// partial timeout here even though both local terms have cheap matches.
#[tokio::test(start_paused = true)]
async fn multi_term_search_keeps_lexical_candidates_after_single_term_budget() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let access = runtime.sql();
    let mut writer = access.writer().await.expect("writer");
    writer
            .execute(SqlStatement {
                sql: "INSERT INTO knowledge_atoms \
                      (id, namespace, slug, name, content, tags, finalized, status, created_at, updated_at) \
                      VALUES \
                      ('92700000-0000-0000-0000-000000000011', 'local', 'zzmass-row', \
                       'First term', 'zzmass content', '[]', 1, 'reviewed', 0, 0), \
                      ('92700000-0000-0000-0000-000000000012', 'local', 'zzlass-row', \
                       'Second term', 'zzlass content', '[]', 1, 'reviewed', 0, 0)"
                    .into(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed two independently matching terms");
    drop(writer);

    let token = runtime.authorize(Namespace::local()).expect("local token");
    let response = with_fts_deadline_advance_after_term(
        1,
        std::time::Duration::from_millis(2_100),
        KnowledgeHandlers::search(
            &runtime,
            &token,
            json!({"query": "zzmass zzlass", "rerank": false}),
            &vamana::new_shared(),
        ),
    )
    .await
    .expect("multi-term search");
    assert_eq!(response["candidate_provenance"]["lexical"], "matched");
    assert_eq!(response["total"], 2);
}

/// Companion pair for issue #1930 Amendment 2's phase-A overfetch/widen
/// behavior. A term whose top bm25/rowid page is entirely status-
/// ineligible must still surface its eligible rows once widening looks
/// past that page — 30 `deprecated` rows are inserted first (lower
/// rowids, so they sort first at equal bm25) and 3 `reviewed` rows
/// inserted after (higher rowids). `fetch_limit=5` with a single real
/// term gives `per_term_limit=5` and a first-round probe of `5*4=20`,
/// which the 30 deprecated rows alone fill — round one must see zero
/// eligible rows, forcing the widen arm to reach the 3 reviewed rows in
/// round two (probe 80, corpus only has 33 total so phase A returns
/// fewer than it asked for, correctly stopping further widening).
#[tokio::test]
async fn phase_b_widening_recovers_eligible_rows_behind_an_ineligible_top_page() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let access = runtime.sql();
    let mut writer = access.writer().await.expect("writer");
    writer
            .execute(SqlStatement {
                sql: "WITH RECURSIVE x(n) AS ( \
                          VALUES(0) UNION ALL SELECT n + 1 FROM x WHERE n < 29 \
                      ) \
                      INSERT INTO knowledge_atoms ( \
                          id, namespace, slug, name, content, tags, properties, finalized, \
                          status, source_uri, source_type, created_at, updated_at, deleted_at \
                      ) \
                      SELECT \
                          printf('92000000-0000-0000-0000-%012d', x.n), \
                          'local', printf('zzwidenzz-dep-%02d', x.n), printf('Widen Dep %02d', x.n), \
                          'synthetic content about zzwidenzz only', '[]', NULL, 1, \
                          'deprecated', NULL, NULL, x.n, x.n, NULL \
                      FROM x"
                    .to_string(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed deprecated widen rows");
    writer
        .execute(SqlStatement {
            sql: "WITH RECURSIVE x(n) AS ( \
                          VALUES(0) UNION ALL SELECT n + 1 FROM x WHERE n < 2 \
                      ) \
                      INSERT INTO knowledge_atoms ( \
                          id, namespace, slug, name, content, tags, properties, finalized, \
                          status, source_uri, source_type, created_at, updated_at, deleted_at \
                      ) \
                      SELECT \
                          printf('92100000-0000-0000-0000-%012d', x.n), \
                          'local', printf('zzwidenzz-ok-%02d', x.n), printf('Widen Ok %02d', x.n), \
                          'synthetic content about zzwidenzz only', '[]', NULL, 1, \
                          'reviewed', NULL, NULL, 100 + x.n, 100 + x.n, NULL \
                      FROM x"
                .to_string(),
            params: Vec::new(),
            label: None,
        })
        .await
        .expect("seed reviewed widen rows");
    drop(writer);

    let outcome = fetch_fts_candidates(&runtime, "local", "zzwidenzz", None, &[], &[], 5)
        .await
        .expect("fetch must not error");

    assert!(outcome.timeout.is_none());
    assert_eq!(
        outcome.atoms.len(),
        3,
        "only the 3 reviewed rows are eligible; widening must have looked \
             past the all-deprecated first page to find them: {:?}",
        outcome
            .atoms
            .iter()
            .map(|a| a.slug.as_str())
            .collect::<Vec<_>>()
    );
    assert!(
        outcome
            .atoms
            .iter()
            .all(|a| a.slug.starts_with("zzwidenzz-ok-")),
        "no deprecated row may survive into the eligible set: {:?}",
        outcome
            .atoms
            .iter()
            .map(|a| a.slug.as_str())
            .collect::<Vec<_>>()
    );
}

/// Control for the widening test above: a term with genuinely fewer
/// matches than `per_term_limit` (2 reviewed rows, cap 5) must return
/// exactly those 2 without fabricating more. Phase A's first probe
/// (`5*4=20`) already exceeds the corpus size for this term, so it
/// returns short on round one and widening never triggers at all — this
/// is what proves `phase_a_full` correctly detects exhaustion instead
/// of retrying up to the ceiling.
#[tokio::test]
async fn phase_b_accepts_a_genuine_shortfall_without_widening() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let access = runtime.sql();
    let mut writer = access.writer().await.expect("writer");
    writer
        .execute(SqlStatement {
            sql: "INSERT INTO knowledge_atoms ( \
                          id, namespace, slug, name, content, tags, properties, finalized, \
                          status, source_uri, source_type, created_at, updated_at, deleted_at \
                      ) VALUES \
                      ('92200000-0000-0000-0000-000000000000', 'local', 'zzsparsezz-00', \
                       'Sparse 00', 'synthetic content about zzsparsezz only', '[]', NULL, 1, \
                       'reviewed', NULL, NULL, 0, 0, NULL), \
                      ('92200000-0000-0000-0000-000000000001', 'local', 'zzsparsezz-01', \
                       'Sparse 01', 'synthetic content about zzsparsezz only', '[]', NULL, 1, \
                       'reviewed', NULL, NULL, 1, 1, NULL)"
                .to_string(),
            params: Vec::new(),
            label: None,
        })
        .await
        .expect("seed sparse rows");
    drop(writer);

    let outcome = fetch_fts_candidates(&runtime, "local", "zzsparsezz", None, &[], &[], 5)
        .await
        .expect("fetch must not error");

    assert!(outcome.timeout.is_none());
    assert_eq!(
        outcome.atoms.len(),
        2,
        "a term with only 2 genuinely eligible matches must return exactly \
             those, not fabricate more via widening: {:?}",
        outcome
            .atoms
            .iter()
            .map(|a| a.slug.as_str())
            .collect::<Vec<_>>()
    );
}

/// Phase A carries no namespace predicate (it is index-only); the
/// namespace check moves entirely to phase B's hydration query. Seed a
/// matching row in another namespace and confirm it is hydrated out —
/// never returned — even though phase A's rowid list spans both
/// namespaces.
#[tokio::test]
async fn phase_a_cross_namespace_matches_are_hydrated_out() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let access = runtime.sql();
    let mut writer = access.writer().await.expect("writer");
    writer
        .execute(SqlStatement {
            sql: "INSERT INTO knowledge_atoms ( \
                          id, namespace, slug, name, content, tags, properties, finalized, \
                          status, source_uri, source_type, created_at, updated_at, deleted_at \
                      ) VALUES \
                      ('92300000-0000-0000-0000-000000000000', 'local', 'zzcrossns-local', \
                       'Cross NS Local', 'synthetic content about zzcrossns only', '[]', NULL, 1, \
                       'reviewed', NULL, NULL, 0, 0, NULL), \
                      ('92300000-0000-0000-0000-000000000001', 'other', 'zzcrossns-other', \
                       'Cross NS Other', 'synthetic content about zzcrossns only', '[]', NULL, 1, \
                       'reviewed', NULL, NULL, 1, 1, NULL)"
                .to_string(),
            params: Vec::new(),
            label: None,
        })
        .await
        .expect("seed cross-namespace rows");
    drop(writer);

    let outcome = fetch_fts_candidates(&runtime, "local", "zzcrossns", None, &[], &[], 10)
        .await
        .expect("fetch must not error");

    assert!(outcome.timeout.is_none());
    assert_eq!(
        outcome.atoms.len(),
        1,
        "only the local-namespace row is eligible: {:?}",
        outcome
            .atoms
            .iter()
            .map(|a| (a.slug.as_str(), a.namespace.as_str()))
            .collect::<Vec<_>>()
    );
    assert_eq!(outcome.atoms[0].namespace, "local");
    assert_eq!(outcome.atoms[0].slug, "zzcrossns-local");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[ignore = "manual 200000-row lexical namespace measurement"]
async fn measure_cross_namespace_lexical_work() {
    async fn seed_namespace(
        runtime: &KhiveRuntime,
        ns: &str,
        id_prefix: &str,
        rows: i64,
        matching_rows: i64,
    ) {
        let access = runtime.sql();
        let mut writer = access.writer().await.expect("writer");
        writer
            .execute(SqlStatement {
                sql: "WITH RECURSIVE seq(n) AS ( \
                              VALUES(0) UNION ALL SELECT n + 1 FROM seq WHERE n + 1 < ?3 \
                          ) \
                          INSERT INTO knowledge_atoms ( \
                              id, namespace, slug, name, content, tags, finalized, \
                              status, created_at, updated_at \
                          ) \
                          SELECT printf('%s-%012d', ?2, n), ?1, \
                                 printf('measurement-%06d', n), 'Measurement Row', \
                                 CASE WHEN n < ?4 THEN \
                                     'namespacechannel content with ordinary padding text' \
                                 ELSE 'unrelated content with ordinary padding text' END, \
                                 '[]', 1, 'reviewed', n, n FROM seq"
                    .into(),
                params: vec![
                    SqlValue::Text(ns.into()),
                    SqlValue::Text(id_prefix.into()),
                    SqlValue::Integer(rows),
                    SqlValue::Integer(matching_rows),
                ],
                label: None,
            })
            .await
            .expect("seed namespace corpus");
    }

    for local_matches in [0, 3] {
        for foreign_present in [true, false] {
            let runtime = KhiveRuntime::memory().expect("in-memory runtime");
            if foreign_present {
                seed_namespace(
                    &runtime,
                    "tenant-a",
                    "92500000-0000-0000-0000",
                    200_000,
                    200_000,
                )
                .await;
            }
            seed_namespace(
                &runtime,
                "tenant-b",
                "92600000-0000-0000-0000",
                10,
                local_matches,
            )
            .await;

            for (mode, query) in [
                ("single", "namespacechannel"),
                ("multi", "namespacechannel absentchannel"),
            ] {
                let start = std::time::Instant::now();
                let outcome = khive_storage::scope_request_read_deadline(
                    lexical_stage_budget(),
                    fetch_fts_candidates(
                        &runtime,
                        "tenant-b",
                        query,
                        None,
                        &[],
                        &[],
                        CANDIDATE_POOL,
                    ),
                )
                .await
                .expect("bounded lexical fetch");
                let elapsed_ms = start.elapsed().as_secs_f64() * 1000.0;
                assert!(outcome
                    .atoms
                    .iter()
                    .all(|atom| atom.namespace == "tenant-b"));
                println!(
                        "LEXICAL_NAMESPACE mode={mode} foreign_present={foreign_present} local_matches={local_matches} rows={} lexical_elapsed_ms={elapsed_ms:.3} lexical_timeout={}",
                        outcome.atoms.len(),
                        outcome.timeout.is_some(),
                    );
            }
        }
    }
}

/// Foreign FTS matches must not change the public candidate state or
/// admit unrelated local rows. This retains the #2396 namespace boundary
/// after removing the recent-row fallback.
#[tokio::test]
async fn empty_result_fallback_never_leaks_a_foreign_namespace_match() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let access = runtime.sql();
    let mut writer = access.writer().await.expect("writer");
    writer
            .execute(SqlStatement {
                sql: "INSERT INTO knowledge_atoms ( \
                          id, namespace, slug, name, content, tags, properties, finalized, \
                          status, source_uri, source_type, created_at, updated_at, deleted_at \
                      ) VALUES \
                      ('92400000-0000-0000-0000-000000000000', 'local', 'unrelated-local-atom', \
                       'Unrelated Local Atom', 'generic filler text with no special term', \
                       '[]', NULL, 1, 'reviewed', NULL, NULL, 0, 0, NULL), \
                      ('92400000-0000-0000-0000-000000000001', 'tenant-b', 'foreign-term-atom', \
                       'Foreign Term Atom', 'synthetic content about zzoraclezz only', '[]', NULL, 1, \
                       'reviewed', NULL, NULL, 1, 1, NULL)"
                    .to_string(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed oracle rows");
    drop(writer);

    let foreign_match = fetch_fts_candidates(&runtime, "local", "zzoraclezz", None, &[], &[], 5)
        .await
        .expect("fetch must not error");
    let no_match = fetch_fts_candidates(&runtime, "local", "zzoracleabsentzz", None, &[], &[], 5)
        .await
        .expect("fetch must not error");

    assert!(foreign_match.timeout.is_none());
    assert!(no_match.timeout.is_none());
    assert_eq!(foreign_match.state, LexicalCandidateState::NoMatch);
    assert_eq!(foreign_match.state, no_match.state);
    let foreign_slugs: Vec<&str> = foreign_match
        .atoms
        .iter()
        .map(|a| a.slug.as_str())
        .collect();
    let absent_slugs: Vec<&str> = no_match.atoms.iter().map(|a| a.slug.as_str()).collect();
    assert_eq!(
        foreign_slugs, absent_slugs,
        "a term matching only in another namespace must produce the exact \
             same response as a term matching nowhere at all — any \
             difference is a cross-namespace existence oracle"
    );
    assert!(
        foreign_slugs.is_empty(),
        "unrelated recent rows are not candidates"
    );
}

/// A real indexed match survives, without unrelated recent rows.
#[tokio::test]
async fn empty_result_fallback_control_index_match_skips_fallback() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let access = runtime.sql();
    let mut writer = access.writer().await.expect("writer");
    writer
            .execute(SqlStatement {
                sql: "INSERT INTO knowledge_atoms ( \
                          id, namespace, slug, name, content, tags, properties, finalized, \
                          status, source_uri, source_type, created_at, updated_at, deleted_at \
                      ) VALUES \
                      ('92400000-0000-0000-0000-000000000002', 'local', 'indexed-term-atom', \
                       'Indexed Term Atom', 'synthetic content about zzindexedzz only', '[]', NULL, 1, \
                       'reviewed', NULL, NULL, 0, 0, NULL), \
                      ('92400000-0000-0000-0000-000000000003', 'local', 'unrelated-recent-atom', \
                       'Unrelated Recent Atom', 'generic filler text with no special term', '[]', NULL, 1, \
                       'reviewed', NULL, NULL, 1, 1, NULL)"
                    .to_string(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed control rows");
    drop(writer);

    let outcome = fetch_fts_candidates(&runtime, "local", "zzindexedzz", None, &[], &[], 5)
        .await
        .expect("fetch must not error");

    assert!(outcome.timeout.is_none());
    assert_eq!(
        outcome.atoms.len(),
        1,
        "only the indexed match may be returned, never the unrelated \
             fallback-only row: {:?}",
        outcome
            .atoms
            .iter()
            .map(|a| a.slug.as_str())
            .collect::<Vec<_>>()
    );
    assert_eq!(outcome.atoms[0].slug, "indexed-term-atom");
    assert_eq!(outcome.state, LexicalCandidateState::Matched);
}

#[tokio::test]
async fn filtered_candidate_state_is_stable_behind_a_foreign_ceiling_prefix() {
    let mut public_responses = Vec::new();
    for foreign_prefix in [false, true] {
        let runtime = KhiveRuntime::memory().expect("in-memory runtime");
        let access = runtime.sql();
        let mut writer = access.writer().await.expect("writer");
        if foreign_prefix {
            writer
                .execute(SqlStatement {
                    sql: "WITH RECURSIVE x(n) AS ( \
                                  VALUES(1) UNION ALL SELECT n + 1 FROM x WHERE n < 20 \
                              ) \
                              INSERT INTO knowledge_atoms ( \
                                  rowid, id, namespace, slug, name, content, tags, finalized, \
                                  status, created_at, updated_at \
                              ) \
                              SELECT x.n, printf('92710000-0000-0000-0000-%012d', x.n), \
                                  'foreign', printf('foreign-prefix-%02d', x.n), 'Foreign Match', \
                                  'zzfilteredceilingzz', '[]', 1, 'reviewed', 0, 0 \
                              FROM x"
                        .into(),
                    params: Vec::new(),
                    label: None,
                })
                .await
                .expect("seed foreign ceiling prefix");
        }
        writer
            .execute(SqlStatement {
                sql: "INSERT INTO knowledge_atoms ( \
                              rowid, id, namespace, slug, name, content, tags, finalized, \
                              status, created_at, updated_at \
                          ) VALUES ( \
                              21, '92710000-0000-0000-0000-000000000021', 'local', \
                              'local-filtered-match', 'Local Filtered Match', \
                              'zzfilteredceilingzz', '[]', 0, 'draft', 0, 0 \
                          )"
                .into(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed identical local filtered match");
        drop(writer);

        let mut reader = access.reader().await.expect("reader");
        let rows = reader
            .query_all(phase_a_rowids_statement("\"zzfilteredceilingzz\"", 20))
            .await
            .expect("inspect ceiling window");
        let rowids: Vec<_> = rows
            .iter()
            .filter_map(|row| row_i64(row, "rowid"))
            .collect();
        if foreign_prefix {
            assert_eq!(rowids, (1..=20).collect::<Vec<i64>>());
        } else {
            assert_eq!(rowids, [21]);
        }
        drop(reader);

        let token = runtime.authorize(Namespace::local()).expect("local token");
        let ann = vamana::new_shared();
        let response = with_phase_a_widen_ceiling_override(20, async {
            KnowledgeHandlers::search(
                &runtime,
                &token,
                json!({"query": "zzfilteredceilingzz", "rerank": false}),
                &ann,
            )
            .await
        })
        .await
        .expect("public filtered search");
        assert_eq!(
            response,
            json!({
                "results": [], "total": 0,
                "candidate_provenance": {"lexical": "filtered", "fallback": "none", "terms_truncated": false},
            }),
            "foreign matches must not change the same local filtered result: {foreign_prefix}"
        );
        public_responses.push(serde_json::to_vec(&response).expect("serialize public response"));
    }
    assert_eq!(public_responses[0], public_responses[1]);
}

/// Issue #2396 fix 2: when phase A's widening reaches the ceiling and the
/// eligible set is still short, more than `ceiling` ineligible top-ranked
/// rows must not be allowed to hide an eligible row further down the
/// bm25 ranking than phase A ever probed. The ceiling is overridden down
/// to 20 so the fixture can stay small: 20 `deprecated` rows (lower
/// rowids, so they sort first at equal bm25) exactly fill the first — and
/// at this override, only — probe window, so `eligible_now` is empty
/// right at the ceiling. The eligibility-scoped fallback query must then
/// recover the 2 `reviewed` rows seeded after them (higher rowids, never
/// inside the ceiling-bounded window). The ordinary (non-ceiling)
/// widening path and its shortfall control are covered by
/// `phase_b_widening_recovers_eligible_rows_behind_an_ineligible_top_page`
/// and `phase_b_accepts_a_genuine_shortfall_without_widening` above.
#[tokio::test]
async fn phase_b_ceiling_exhaustion_recovers_eligible_row_via_scoped_fallback() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let access = runtime.sql();
    let mut writer = access.writer().await.expect("writer");
    writer
            .execute(SqlStatement {
                sql: "WITH RECURSIVE x(n) AS ( \
                          VALUES(0) UNION ALL SELECT n + 1 FROM x WHERE n < 19 \
                      ) \
                      INSERT INTO knowledge_atoms ( \
                          id, namespace, slug, name, content, tags, properties, finalized, \
                          status, source_uri, source_type, created_at, updated_at, deleted_at \
                      ) \
                      SELECT \
                          printf('92500000-0000-0000-0000-%012d', x.n), \
                          'local', printf('zzceilingzz-dep-%02d', x.n), printf('Ceiling Dep %02d', x.n), \
                          'synthetic content about zzceilingzz only', '[]', NULL, 1, \
                          'deprecated', NULL, NULL, x.n, x.n, NULL \
                      FROM x"
                    .to_string(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed deprecated ceiling rows");
    writer
            .execute(SqlStatement {
                sql: "WITH RECURSIVE x(n) AS ( \
                          VALUES(0) UNION ALL SELECT n + 1 FROM x WHERE n < 1 \
                      ) \
                      INSERT INTO knowledge_atoms ( \
                          id, namespace, slug, name, content, tags, properties, finalized, \
                          status, source_uri, source_type, created_at, updated_at, deleted_at \
                      ) \
                      SELECT \
                          printf('92510000-0000-0000-0000-%012d', x.n), \
                          'local', printf('zzceilingzz-ok-%02d', x.n), printf('Ceiling Ok %02d', x.n), \
                          'synthetic content about zzceilingzz only', '[]', NULL, 1, \
                          'reviewed', NULL, NULL, 100 + x.n, 100 + x.n, NULL \
                      FROM x"
                    .to_string(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed reviewed ceiling rows");
    drop(writer);

    let outcome = with_phase_a_widen_ceiling_override(20, async {
        fetch_fts_candidates(&runtime, "local", "zzceilingzz", None, &[], &[], 5).await
    })
    .await
    .expect("fetch must not error");

    assert!(outcome.timeout.is_none());
    assert_eq!(
        outcome.atoms.len(),
        2,
        "the 2 reviewed rows sit beyond the ceiling-bounded phase-A \
             window; only the eligibility-scoped fallback query can recover \
             them: {:?}",
        outcome
            .atoms
            .iter()
            .map(|a| a.slug.as_str())
            .collect::<Vec<_>>()
    );
    assert!(
        outcome
            .atoms
            .iter()
            .all(|a| a.slug.starts_with("zzceilingzz-ok-")),
        "no deprecated row may survive into the eligible set: {:?}",
        outcome
            .atoms
            .iter()
            .map(|a| a.slug.as_str())
            .collect::<Vec<_>>()
    );
}

/// A member-token-sizing timeout returns no measurements, never a
/// placeholder zero that could be admitted as a free fold candidate.
#[tokio::test]
async fn member_token_sizes_report_timeout_instead_of_a_measured_zero() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let access = runtime.sql();
    let mut writer = access.writer().await.expect("writer");
    writer
            .execute(SqlStatement {
                sql: "INSERT INTO knowledge_atoms ( \
                          id, namespace, slug, name, content, tags, properties, finalized, \
                          status, source_uri, source_type, created_at, updated_at, deleted_at \
                      ) VALUES ( \
                          '92600000-0000-0000-0000-000000000000', 'local', 'sizing-member-atom', \
                          'Sizing Member Atom', \
                          'enough body content to price a non-zero token size for the owning domain once member sizing actually runs to completion', \
                          '[]', NULL, 1, 'reviewed', NULL, NULL, 0, 0, NULL)"
                    .to_string(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed member atom");
    writer
        .execute(SqlStatement {
            sql: "INSERT INTO knowledge_domains ( \
                          id, namespace, slug, name, description, tags, members, status, \
                          created_at, updated_at, deleted_at \
                      ) VALUES ( \
                          '92600000-0000-0000-0000-000000000001', 'local', 'sizing-domain', \
                          'Sizing Domain', NULL, '[]', '[\"sizing-member-atom\"]', 'reviewed', \
                          0, 0, NULL)"
                .to_string(),
            params: Vec::new(),
            label: None,
        })
        .await
        .expect("seed domain");
    drop(writer);

    let domain_ids = vec!["92600000-0000-0000-0000-000000000001".to_string()];

    let (degraded_sizes, timed_out) = khive_storage::scope_request_read_deadline(
        std::time::Duration::ZERO,
        load_domain_member_token_sizes(&runtime, "local", &domain_ids),
    )
    .await
    .expect("an expired read deadline must degrade, not error");
    assert!(
        timed_out,
        "an expired read deadline must be reported as unmeasured"
    );
    assert!(
        degraded_sizes.is_empty(),
        "a timed-out batch must not contain fabricated measurements"
    );

    let (healthy_sizes, healthy_timed_out) =
        load_domain_member_token_sizes(&runtime, "local", &domain_ids)
            .await
            .expect("undeadlined lookup must succeed");
    assert!(!healthy_timed_out);
    assert!(
        healthy_sizes
            .get(&domain_ids[0])
            .is_some_and(|sizing| sizing.tokens > 0 && sizing.live_members == 1),
        "control: without a deadline the lookup measures the real member \
             body cost; got {healthy_sizes:?}"
    );
}

/// Issue #2396 fix 4: the lexical-stage-budget override rides a
/// `tokio::task_local!`, so it is scoped to the task it wraps only.
/// Two concurrent tasks — one scoped to an override, one with none —
/// must observe different budgets; the prior process-global `AtomicU64`
/// override could not guarantee this; a task with no override of its own
/// could observe whatever value another concurrently running test last
/// stored.
#[tokio::test]
async fn lexical_stage_budget_override_does_not_leak_across_concurrent_tasks() {
    let with_override = tokio::spawn(with_lexical_stage_budget_override_ms(50, async {
        tokio::task::yield_now().await;
        lexical_stage_budget()
    }));
    let without_override = tokio::spawn(async {
        tokio::task::yield_now().await;
        lexical_stage_budget()
    });

    let overridden = with_override.await.expect("task must not panic");
    let baseline = without_override.await.expect("task must not panic");

    assert_eq!(overridden, std::time::Duration::from_millis(50));
    assert_eq!(
        baseline,
        std::time::Duration::from_millis(LEXICAL_STAGE_BUDGET_MS),
        "a concurrently running task with no override of its own must \
             never observe another task's override; got {baseline:?}"
    );
}

/// Issue: `MIN_TERM_LEN=3` drops every token of a query like "AI" before
/// FTS ever sees it, and the trigram tokenizer cannot match a phrase that
/// short either, so the atom was unreachable without ANN. The indexed
/// slug probe in the lexical stage must restore discoverability for both the
/// atom's exact-case name and a case-insensitive spelling, while a query
/// that matches no slug at all must still report a genuine miss.
#[tokio::test]
async fn short_exact_name_is_discoverable_without_ann() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    {
        let access = runtime.sql();
        let mut writer = access.writer().await.expect("writer");
        writer
            .execute(SqlStatement {
                sql: "INSERT INTO knowledge_atoms ( \
                              id, namespace, slug, name, content, tags, properties, finalized, \
                              status, source_uri, source_type, created_at, updated_at, deleted_at \
                          ) VALUES ( \
                              '93000000-0000-0000-0000-000000000001', 'local', \
                              'ai', 'AI', \
                              'artificial intelligence overview content for the corpus', '[]', \
                              NULL, 1, 'reviewed', NULL, NULL, 1000, 1000, NULL \
                          )"
                .to_string(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed short-name atom");
    }

    let token = runtime.authorize(Namespace::local()).expect("local token");
    let ann = vamana::new_shared();

    let exact_case = KnowledgeHandlers::search(
        &runtime,
        &token,
        json!({"query": "AI", "rerank": false}),
        &ann,
    )
    .await
    .expect("exact-case short-name search must not error");
    assert_eq!(exact_case["total"], 1);
    assert_eq!(exact_case["results"][0]["slug"], "ai");
    assert_eq!(exact_case["candidate_provenance"]["lexical"], "exact_name");
    assert_eq!(exact_case["candidate_provenance"]["fallback"], "none");
    assert_eq!(
        exact_case["results"][0]["score_provenance"]["sources"],
        json!(["lexical"])
    );
    assert!(exact_case["results"][0]["score"].as_f64().unwrap() > 0.0);

    let lower_case = KnowledgeHandlers::search(
        &runtime,
        &token,
        json!({"query": "ai", "rerank": false}),
        &ann,
    )
    .await
    .expect("lower-case short-name search must not error");
    assert_eq!(lower_case["total"], 1);
    assert_eq!(lower_case["results"][0]["slug"], "ai");
    assert_eq!(lower_case["candidate_provenance"]["lexical"], "exact_name");

    let miss = KnowledgeHandlers::search(
        &runtime,
        &token,
        json!({"query": "zz", "rerank": false}),
        &ann,
    )
    .await
    .expect("non-matching short query must not error");
    assert_eq!(miss["total"], 0);
    assert_eq!(miss["candidate_provenance"]["lexical"], "no_match");

    // A role prefix is scored, never searched: it must not hide the probe.
    let role_qualified = KnowledgeHandlers::search(
        &runtime,
        &token,
        json!({"query": "AI", "role": "researcher", "rerank": false}),
        &ann,
    )
    .await
    .expect("role-qualified short-name search must not error");
    assert_eq!(role_qualified["total"], 1);
    assert_eq!(role_qualified["results"][0]["slug"], "ai");
    assert_eq!(
        role_qualified["candidate_provenance"]["lexical"],
        "exact_name"
    );
}

/// A deadline that expires inside the exact-name probe is a lexical
/// timeout, not a miss: the caller degrades the response on `TimedOut`
/// and would otherwise reach the final active-read check and error.
#[tokio::test]
async fn exact_name_probe_reports_an_expired_deadline_as_a_timeout() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let sql = runtime.sql();
    let mut reader = sql.reader().await.expect("reader before deadline");
    let configured = lexical_stage_budget();

    let expired = khive_storage::scope_request_read_deadline(std::time::Duration::ZERO, async {
        let mut stage =
            LexicalStage::new(LexicalPass::Full, tokio::time::Instant::now(), configured);
        let outcome =
            fetch_exact_name_candidate(reader.as_mut(), "local", "AI", None, &[], &[], &mut stage)
                .await;
        let timeout = stage.timeout.expect("exact probe captures timeout details");
        assert_eq!(timeout.phase, LexicalPhase::ExactNameProbe);
        assert_eq!(timeout.effective_budget_ms, 0);
        outcome
    })
    .await;
    assert!(
        matches!(expired, Ok(ExactNameProbe::TimedOut)),
        "an expired read deadline must surface as TimedOut, never as a \
             miss; got {expired:?}"
    );

    let mut stage = LexicalStage::new(LexicalPass::Full, tokio::time::Instant::now(), configured);
    let healthy =
        fetch_exact_name_candidate(reader.as_mut(), "local", "AI", None, &[], &[], &mut stage)
            .await
            .expect("undeadlined probe must succeed");
    assert!(
        matches!(healthy, ExactNameProbe::Miss),
        "control: without a deadline an unseeded name is a plain miss; got {healthy:?}"
    );
    assert!(stage.timeout.is_none());
}

/// The indexed exact-name probe must use the unique `(namespace, slug)`
/// index, never a full-namespace scan on `name` — no such index exists.
/// Same `EXPLAIN QUERY PLAN` style as
/// `crud::get_prefix_query_plan_uses_primary_key_range_seeks`.
#[tokio::test]
async fn exact_name_probe_query_plan_uses_slug_index() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let mut reader = runtime.sql().reader().await.expect("exact-name reader");
    let rows = reader
        .explain(exact_name_statement(
            "local",
            "ai",
            Some("atom"),
            &[],
            &["draft", "deprecated"],
        ))
        .await
        .expect("explain exact-name probe");
    let details: Vec<String> = rows
        .iter()
        .filter_map(|row| match row.get("detail") {
            Some(SqlValue::Text(detail)) => Some(detail.clone()),
            _ => None,
        })
        .collect();
    assert!(
        details.iter().any(|d| d.contains("SEARCH knowledge_atoms")
            && d.contains("USING INDEX")
            && d.contains("namespace=? AND slug=?")),
        "exact-name probe must use an index seek, not a table scan: {details:?}"
    );
    assert!(
        !details.iter().any(|d| d.contains("SCAN knowledge_atoms")),
        "exact-name probe must never full-scan knowledge_atoms: {details:?}"
    );
}

#[tokio::test]
async fn short_exact_name_probe_respects_namespace_status_type_and_soft_deletion() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let sql = runtime.sql();
    let mut writer = sql.writer().await.unwrap();
    for (index, (ns, slug, name, status, tags, deleted)) in [
        ("local", "ai", "AI", "draft", "[]", SqlValue::Null),
        ("tenant-b", "ai", "AI", "reviewed", "[]", SqlValue::Null),
        (
            "local",
            "ml",
            "ML",
            "reviewed",
            "[\"type:domain\"]",
            SqlValue::Null,
        ),
        (
            "local",
            "zz",
            "ZZ",
            "reviewed",
            "[]",
            SqlValue::Integer(1000),
        ),
        (
            "local",
            "custom-name",
            "UI",
            "reviewed",
            "[]",
            SqlValue::Null,
        ),
    ]
    .into_iter()
    .enumerate()
    {
        writer.execute(SqlStatement {
                sql: "INSERT INTO knowledge_atoms (id, namespace, slug, name, content, tags, finalized, status, created_at, updated_at, deleted_at) \
                      VALUES (?1, ?2, ?3, ?4, 'unrelated overview', ?5, 1, ?6, 0, 0, ?7)".into(),
                params: vec![SqlValue::Text(format!("93000000-0000-0000-0001-{index:012}")), SqlValue::Text(ns.into()), SqlValue::Text(slug.into()), SqlValue::Text(name.into()), SqlValue::Text(tags.into()), SqlValue::Text(status.into()), deleted],
                label: None,
            }).await.unwrap();
    }
    drop(writer);
    for (ns, query, kind, statuses, expected_state, expected_count) in [
        (
            "local",
            "AI",
            Some("atom"),
            vec![],
            LexicalCandidateState::Filtered,
            0,
        ),
        (
            "local",
            "AI",
            Some("atom"),
            vec!["draft".to_string()],
            LexicalCandidateState::ExactName,
            1,
        ),
        (
            "tenant-b",
            "AI",
            Some("atom"),
            vec![],
            LexicalCandidateState::ExactName,
            1,
        ),
        (
            "tenant-c",
            "AI",
            Some("atom"),
            vec![],
            LexicalCandidateState::NoMatch,
            0,
        ),
        (
            "local",
            "ML",
            Some("atom"),
            vec![],
            LexicalCandidateState::Filtered,
            0,
        ),
        (
            "local",
            "ML",
            Some("domain"),
            vec![],
            LexicalCandidateState::ExactName,
            1,
        ),
        (
            "local",
            "ZZ",
            Some("atom"),
            vec![],
            LexicalCandidateState::NoMatch,
            0,
        ),
        (
            "local",
            "UI",
            Some("atom"),
            vec![],
            LexicalCandidateState::NoMatch,
            0,
        ),
    ] {
        let outcome = fetch_fts_candidates(
            &runtime,
            ns,
            query,
            kind,
            &statuses,
            &["draft", "deprecated"],
            5,
        )
        .await
        .unwrap();
        assert!(outcome.timeout.is_none());
        assert_eq!(
            outcome.state, expected_state,
            "{ns}/{query}/{kind:?}/{statuses:?}"
        );
        assert_eq!(outcome.atoms.len(), expected_count);
        assert!(outcome.atoms.iter().all(|atom| atom.namespace == ns));
    }
}

#[test]
fn fts_candidate_expression_recalls_non_contiguous_terms() {
    assert_eq!(
        fts5_candidate_terms("alpha beta alpha and").join(" OR "),
        "\"alpha\" OR \"alphas\" OR \"beta\" OR \"betas\""
    );
    assert_eq!(
        fts5_candidate_terms("RAG").join(" OR "),
        "\"rag\" OR \"rags\""
    );
    assert_eq!(
        fts5_candidate_terms("the and").join(" OR "),
        "\"the and\"",
        "stop-only queries retain the exact-phrase fallback"
    );
}

#[test]
fn rrf_fusion_preserves_per_hit_score_sources_and_ann_fallback() {
    let mut hybrid = vec![make_hit("shared", Some("reviewed"), 0.8)];
    let ann = vec![make_ann_hit("shared", Some("reviewed"), 0.9)];
    fuse_ann_hits(&mut hybrid, &ann, 0.0);
    assert_eq!(hybrid.len(), 1);
    assert_eq!(
        hybrid[0].provenance.to_json()["sources"],
        json!(["lexical", "ann"])
    );
    assert_eq!(candidate_fallback(&hybrid), "none");

    let mut ann_only = Vec::new();
    fuse_ann_hits(
        &mut ann_only,
        &[make_ann_hit("semantic", Some("reviewed"), 0.9)],
        0.0,
    );
    assert_eq!(ann_only.len(), 1);
    assert_eq!(ann_only[0].provenance.to_json()["sources"], json!(["ann"]));
    assert_eq!(candidate_fallback(&ann_only), "ann");
    assert_eq!(
        ann_only[0].provenance.to_json(),
        json!({
            "sources": ["ann"],
            "embedding_rerank": false,
            "normalization": "s_over_s_plus_1",
            "calibrated": false,
        })
    );

    let lexical_only = vec![make_hit("lexical", Some("reviewed"), 0.7)];
    assert_eq!(
        lexical_only[0].provenance.to_json()["sources"],
        json!(["lexical"])
    );
    assert_eq!(candidate_fallback(&lexical_only), "none");
}

#[tokio::test]
async fn embedding_rerank_provenance_is_true_when_rerank_runs() {
    let (runtime, _, fail_query) = rt_with_role_aware_recording_embedder();
    fail_query.store(false, std::sync::atomic::Ordering::SeqCst);
    {
        let access = runtime.sql();
        let mut writer = access.writer().await.expect("writer");
        writer
            .execute(SqlStatement {
                sql: "INSERT INTO knowledge_atoms ( \
                              id, namespace, slug, name, content, tags, properties, finalized, \
                              status, source_uri, source_type, created_at, updated_at, deleted_at \
                          ) VALUES ( \
                              '94000000-0000-0000-0000-000000000001', 'local', \
                              'rerank-target', 'Rerank Target', \
                              'content that the lexical stage must match for the rerank pass', \
                              '[]', NULL, 1, 'reviewed', NULL, NULL, 1000, 1000, NULL \
                          )"
                .to_string(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed rerank target atom");
    }

    let token = runtime.authorize(Namespace::local()).expect("local token");
    let ann = vamana::new_shared();
    let out = KnowledgeHandlers::search(
        &runtime,
        &token,
        json!({"query": "rerank target content", "rerank": true}),
        &ann,
    )
    .await
    .expect("rerank-enabled search must not error");

    assert_eq!(out["total"], 1);
    assert_eq!(
        out["results"][0]["score_provenance"]["embedding_rerank"], true,
        "a successful embedding rerank must record embedding_rerank: true; got {out:?}"
    );
    assert_eq!(out["rerank_provenance"]["candidates"], 1);
    assert_eq!(out["rerank_provenance"]["embedded_fallback"], 1);
}

#[tokio::test]
async fn over_limit_fallback_candidate_keeps_whole_search_reranked() {
    let (runtime, _, _) = rt_with_role_aware_recording_embedder();
    let repeated_text = "bounded fallback candidate ";
    let long_content =
        repeated_text.repeat(lattice_embed::MAX_TEXT_BYTES / repeated_text.len() + 2);
    assert!(
        atom_embed_text_fields("Bounded Fallback Long", &long_content, "[]").len()
            > lattice_embed::MAX_TEXT_BYTES
    );
    {
        let access = runtime.sql();
        let mut writer = access.writer().await.expect("writer");
        for (id, slug, name, content) in [
            (
                "94000000-0000-0000-0000-000000000011",
                "bounded-fallback-long",
                "Bounded Fallback Long",
                long_content.as_str(),
            ),
            (
                "94000000-0000-0000-0000-000000000012",
                "bounded-fallback-short",
                "Bounded Fallback Short",
                "bounded fallback candidate with short content",
            ),
        ] {
            writer
                    .execute(SqlStatement {
                        sql: "INSERT INTO knowledge_atoms ( \
                                  id, namespace, slug, name, content, tags, properties, finalized, \
                                  status, source_uri, source_type, created_at, updated_at, deleted_at \
                              ) VALUES (?, 'local', ?, ?, ?, '[]', NULL, 1, 'reviewed', \
                                        NULL, NULL, 1000, 1000, NULL)"
                            .to_string(),
                        params: vec![
                            SqlValue::Text(id.into()),
                            SqlValue::Text(slug.into()),
                            SqlValue::Text(name.into()),
                            SqlValue::Text(content.into()),
                        ],
                        label: None,
                    })
                    .await
                    .expect("seed fallback candidate without a stored vector");
        }
    }

    let token = runtime.authorize(Namespace::local()).expect("local token");
    let ann = vamana::new_shared();
    let out = KnowledgeHandlers::search(
        &runtime,
        &token,
        json!({"query": "bounded fallback candidate", "rerank": true}),
        &ann,
    )
    .await
    .expect("bounded fallback must not disable search reranking");

    assert_eq!(out["total"], 2, "both lexical candidates must be returned");
    let results = out["results"].as_array().expect("search results");
    assert_eq!(results.len(), 2);
    for result in results {
        assert_eq!(
            result["score_provenance"]["embedding_rerank"], true,
            "the long fallback must not skip reranking for either hit: {out:?}"
        );
    }
    assert_eq!(out["rerank_provenance"]["candidates"], 2);
    assert_eq!(out["rerank_provenance"]["from_stored"], 0);
    assert_eq!(out["rerank_provenance"]["embedded_fallback"], 2);
}

#[tokio::test]
async fn unsupported_vector_store_reports_fallback_provenance() {
    let (runtime, _, _) = rt_with_role_aware_recording_embedder();
    let store = crate::knowledge::a5_rerank_tests::NoReadStore;
    let mut hits = vec![make_hit(
        "94000000-0000-0000-0000-000000000001",
        Some("reviewed"),
        1.0,
    )];
    let query_vector = vec![0.25; ROLE_RECORDING_DIM];
    let provenance = rerank_search_from_store(
        &runtime,
        Some(&store),
        "local",
        &query_vector,
        &mut hits,
        0.7,
    )
    .await
    .expect("unsupported store degrades to document embedding")
    .expect("rerank ran");
    assert_eq!(provenance["stored_vector_lookup"], "unsupported");
    assert_eq!(provenance["candidates"], 1);
    assert_eq!(provenance["from_stored"], 0);
    assert_eq!(provenance["embedded_fallback"], 1);
    assert!(hits[0].provenance.embedding_rerank);
}

#[tokio::test]
async fn canonical_domain_without_mirror_vector_uses_document_fallback() {
    let (runtime, _, _) = rt_with_role_aware_recording_embedder();
    let token = runtime.authorize(Namespace::local()).expect("token");
    let store = runtime
        .vectors_for_model(&token, runtime.default_embedder_name())
        .expect("model vector store");
    let mut hits = vec![ScoredHit {
        id: "94000000-0000-0000-0000-000000000002".into(),
        slug: "canonical-only-domain".into(),
        name: "Canonical Only Domain".into(),
        content: Some("canonical description".into()),
        tags: Some("[\"domain-tag\",\"type:domain\"]".into()),
        atom_embed_text: None,
        finalized: false,
        is_domain: true,
        status: Some("reviewed".into()),
        score: 1.0,
        provenance: ScoreProvenance::lexical(),
    }];
    let query_vector = vec![0.25; ROLE_RECORDING_DIM];
    let provenance = rerank_search_from_store(
        &runtime,
        Some(store.as_ref()),
        "local",
        &query_vector,
        &mut hits,
        0.7,
    )
    .await
    .expect("missing mirror vector uses document fallback")
    .expect("rerank ran");
    assert_eq!(provenance["stored_vector_lookup"], "supported");
    assert_eq!(provenance["candidates"], 1);
    assert_eq!(provenance["from_stored"], 0);
    assert_eq!(provenance["embedded_fallback"], 1);
    assert!(hits[0].provenance.embedding_rerank);
}

#[tokio::test]
async fn spent_read_deadline_skips_document_fallback_rerank() {
    let (runtime, _, _) = rt_with_role_aware_recording_embedder();
    let store = crate::knowledge::a5_rerank_tests::NoReadStore;
    let mut hits = vec![make_hit(
        "94000000-0000-0000-0000-000000000001",
        Some("reviewed"),
        1.0,
    )];
    let query_vector = vec![0.25; ROLE_RECORDING_DIM];
    let result = khive_storage::scope_request_read_deadline(
        std::time::Duration::ZERO,
        rerank_search_from_store(
            &runtime,
            Some(&store),
            "local",
            &query_vector,
            &mut hits,
            0.7,
        ),
    )
    .await
    .expect("spent deadline degrades rather than errors");
    assert!(result.is_none());
    assert_eq!(hits[0].score, 1.0);
    assert!(!hits[0].provenance.embedding_rerank);
}

#[test]
fn exact_name_provenance_survives_merge_without_hiding_timeouts() {
    use LexicalCandidateState::{ExactName, Matched, NoMatch, PartialTimeout, TimedOut};
    for (states, expected) in [
        (vec![NoMatch, ExactName], ExactName),
        (vec![ExactName, Matched], Matched),
        (vec![ExactName, TimedOut], PartialTimeout),
        (vec![ExactName, PartialTimeout], PartialTimeout),
    ] {
        assert_eq!(LexicalCandidateState::merge(&states), expected);
    }
}

#[tokio::test]
async fn missing_ann_hydration_is_dropped_and_reported() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let mut hits = vec![ScoredHit {
        id: "00000000-0000-0000-0000-000000001763".to_string(),
        slug: String::new(),
        name: String::new(),
        content: None,
        tags: None,
        atom_embed_text: None,
        finalized: false,
        is_domain: false,
        status: None,
        score: 0.8,
        provenance: ScoreProvenance::ann(),
    }];

    let failures = hydrate_empty_hits(&runtime, "local", &mut hits).await;
    assert_eq!(failures, 1);
    assert!(hits.is_empty(), "unhydrated shells must never be returned");

    let mut response = json!({"results": [], "total": 0});
    attach_hydration_degradation(&mut response, failures);
    assert_eq!(response["degraded"]["hydration_failures"], 1);
}

#[test]
fn zero_hydration_failures_do_not_change_the_response() {
    let mut response = json!({"results": [], "total": 0});
    attach_hydration_degradation(&mut response, 0);
    assert!(response.get("degraded").is_none());
}

#[test]
fn hydration_degradation_preserves_existing_diagnostics() {
    let mut response = json!({
        "results": [],
        "total": 0,
        "degraded": {
            "reason": "ann_unavailable",
            "cache_safe": false,
        }
    });
    attach_hydration_degradation(&mut response, 7);
    assert_eq!(response["degraded"]["reason"], "ann_unavailable");
    assert_eq!(response["degraded"]["cache_safe"], false);
    assert_eq!(response["degraded"]["hydration_failures"], 7);
}

#[tokio::test]
async fn hydration_chunks_candidate_sets_above_sqlite_bind_ceiling() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    {
        let access = runtime.sql();
        let mut writer = access.writer().await.expect("writer");
        writer
            .execute(SqlStatement {
                sql: "WITH RECURSIVE x(n) AS ( \
                              VALUES(0) UNION ALL SELECT n + 1 FROM x WHERE n < 20 \
                          ), y(n) AS ( \
                              VALUES(0) UNION ALL SELECT n + 1 FROM y WHERE n < 49 \
                          ) \
                          INSERT INTO knowledge_atoms ( \
                              id, namespace, slug, name, content, tags, properties, finalized, \
                              status, source_uri, source_type, created_at, updated_at, deleted_at \
                          ) \
                          SELECT \
                              printf('70000000-0000-0000-0000-%012d', x.n * 50 + y.n), \
                              'local', printf('hydrate-%04d', x.n * 50 + y.n), \
                              printf('Hydrate %04d', x.n * 50 + y.n), 'hydration content', \
                              '[]', NULL, 1, 'reviewed', NULL, NULL, \
                              x.n * 50 + y.n, x.n * 50 + y.n, NULL \
                          FROM x CROSS JOIN y WHERE x.n * 50 + y.n < 1005"
                    .to_string(),
                params: Vec::new(),
                label: None,
            })
            .await
            .expect("seed hydration rows");
    }

    let mut hits: Vec<ScoredHit> = (0..1005)
        .map(|i| ScoredHit {
            id: format!("70000000-0000-0000-0000-{i:012}"),
            slug: String::new(),
            name: String::new(),
            content: None,
            tags: None,
            atom_embed_text: None,
            finalized: false,
            is_domain: false,
            status: None,
            score: 1.0,
            provenance: ScoreProvenance::ann(),
        })
        .collect();

    let failures = hydrate_empty_hits(&runtime, "local", &mut hits).await;
    assert_eq!(failures, 0);
    assert_eq!(hits.len(), 1005);
    assert!(hits.iter().all(|hit| hit.slug.starts_with("hydrate-")));
}

/// Pins the production plan measured on the live store (179,809-row
/// `knowledge_atoms`, no `sqlite_stat1`): with 250 literal ids and no
/// `ANALYZE`, the planner must pick the primary-key auto-index, never a
/// namespace index. A scratch, statistics-free database reproduces the
/// same wrong-index choice the live store made, because the planner's
/// default no-statistics guess (an indexed equality is ~10 rows) is what
/// drove the original defect, not data volume.
#[tokio::test]
async fn hydrate_atoms_statement_plan_uses_primary_key_not_namespace_index() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let ids: Vec<String> = (0..250)
        .map(|i| format!("aaaaaaaa-0000-0000-0000-{i:012}"))
        .collect();

    let mut reader = runtime.sql().reader().await.expect("plan reader");
    let rows = reader
        .explain(hydrate_atoms_statement("local", &ids))
        .await
        .expect("explain atom hydration statement");
    let details: Vec<String> = rows
        .iter()
        .filter_map(|row| match row.get("detail") {
            Some(SqlValue::Text(detail)) => Some(detail.clone()),
            _ => None,
        })
        .collect();

    assert!(
        details
            .iter()
            .any(|detail| detail.contains("USING INDEX sqlite_autoindex_knowledge_atoms_1")),
        "atom hydration must seek the primary key: {details:?}"
    );
    assert!(
        !details
            .iter()
            .any(|detail| detail.contains("idx_knowledge_atoms_ns")),
        "atom hydration must not fall back to a namespace index: {details:?}"
    );
}

/// Domains twin of the atoms plan-pin above — same shape, same reason
/// (`knowledge_domains` also carries a namespace index that the
/// no-statistics planner would otherwise prefer).
#[tokio::test]
async fn hydrate_domains_statement_plan_uses_primary_key_not_namespace_index() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let ids: Vec<String> = (0..250)
        .map(|i| format!("bbbbbbbb-0000-0000-0000-{i:012}"))
        .collect();

    let mut reader = runtime.sql().reader().await.expect("plan reader");
    let rows = reader
        .explain(hydrate_domains_statement("local", &ids))
        .await
        .expect("explain domain hydration statement");
    let details: Vec<String> = rows
        .iter()
        .filter_map(|row| match row.get("detail") {
            Some(SqlValue::Text(detail)) => Some(detail.clone()),
            _ => None,
        })
        .collect();

    assert!(
        details
            .iter()
            .any(|detail| { detail.contains("USING INDEX sqlite_autoindex_knowledge_domains_1") }),
        "domain hydration must seek the primary key: {details:?}"
    );
    assert!(
        !details
            .iter()
            .any(|detail| detail.contains("idx_knowledge_domains_ns")),
        "domain hydration must not fall back to a namespace index: {details:?}"
    );
}

/// Issue #2396 fix 5, same plan-pin shape as the two tests above applied
/// to the lexical phase-B hydration statement: at `HYDRATION_ID_CHUNK`
/// (900) rowids, the no-statistics planner must seek the integer primary
/// key, never fall back to `idx_knowledge_atoms_ns`.
#[tokio::test]
async fn phase_b_hydration_statement_plan_uses_primary_key_not_namespace_index() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let rowids: Vec<i64> = (1..=HYDRATION_ID_CHUNK as i64).collect();

    let mut reader = runtime.sql().reader().await.expect("plan reader");
    let rows = reader
        .explain(phase_b_hydration_statement("local", &rowids, &[], &[], ""))
        .await
        .expect("explain phase-b hydration statement");
    let details: Vec<String> = rows
        .iter()
        .filter_map(|row| match row.get("detail") {
            Some(SqlValue::Text(detail)) => Some(detail.clone()),
            _ => None,
        })
        .collect();

    assert!(
        details
            .iter()
            .any(|detail| detail.contains("USING INTEGER PRIMARY KEY")),
        "phase-b hydration must seek the rowid primary key: {details:?}"
    );
    assert!(
        !details
            .iter()
            .any(|detail| detail.contains("idx_knowledge_atoms_ns")),
        "phase-b hydration must not fall back to a namespace index: {details:?}"
    );
}

/// Functional companion to the plan-pin tests above: the primary-key-first
/// rewrite must not weaken namespace scoping. Seed atoms in two
/// namespaces, hydrate ids drawn from both against a single namespace,
/// and confirm the other namespace's row never comes back.
#[tokio::test]
async fn hydrate_atoms_statement_still_scopes_by_namespace() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let access = runtime.sql();
    let mut writer = access.writer().await.expect("writer");
    writer
        .execute(SqlStatement {
            sql: "INSERT INTO knowledge_atoms ( \
                          id, namespace, slug, name, content, tags, properties, finalized, \
                          status, source_uri, source_type, created_at, updated_at, deleted_at \
                      ) VALUES \
                      ('90000000-0000-0000-0000-000000000001', 'local', 'local-one', \
                       'Local One', 'local content', '[]', NULL, 1, 'reviewed', NULL, NULL, \
                       1, 1, NULL), \
                      ('90000000-0000-0000-0000-000000000002', 'local', 'local-two', \
                       'Local Two', 'local content', '[]', NULL, 1, 'reviewed', NULL, NULL, \
                       2, 2, NULL), \
                      ('90000000-0000-0000-0000-000000000003', 'other', 'other-one', \
                       'Other One', 'other content', '[]', NULL, 1, 'reviewed', NULL, NULL, \
                       3, 3, NULL)"
                .to_string(),
            params: Vec::new(),
            label: None,
        })
        .await
        .expect("seed cross-namespace atoms");
    drop(writer);

    let mut hits: Vec<ScoredHit> = [
        "90000000-0000-0000-0000-000000000001",
        "90000000-0000-0000-0000-000000000002",
        "90000000-0000-0000-0000-000000000003",
    ]
    .iter()
    .map(|id| ScoredHit {
        id: id.to_string(),
        slug: String::new(),
        name: String::new(),
        content: None,
        tags: None,
        atom_embed_text: None,
        finalized: false,
        is_domain: false,
        status: None,
        score: 1.0,
        provenance: ScoreProvenance::ann(),
    })
    .collect();

    let failures = hydrate_empty_hits(&runtime, "local", &mut hits).await;
    assert_eq!(
        failures, 1,
        "the other-namespace row must be reported as an unhydrated candidate"
    );
    assert_eq!(hits.len(), 2, "only the local-namespace rows may hydrate");
    assert!(
        hits.iter()
            .all(|hit| hit.id != "90000000-0000-0000-0000-000000000003"),
        "the other-namespace row must never be returned: {:?}",
        hits.iter().map(|h| &h.id).collect::<Vec<_>>()
    );
    assert!(hits.iter().any(|hit| hit.slug == "local-one"));
    assert!(hits.iter().any(|hit| hit.slug == "local-two"));
}

// ── embed-intent regression ───────────────────────────────────────────────
// Guard that the shared ANN candidate runner and the compose KG-blend gate
// use the query-intent embedding call, not the generic
// `runtime.embed(...)`. Uses include_str! so the assertion runs on the
// actual source bytes, but splits the needle to avoid matching the
// needle itself in test source.
#[test]
fn knowledge_ann_query_paths_use_query_intent_embed() {
    let src = [
        include_str!("search.rs"),
        include_str!("search/ann_search.rs"),
        include_str!("search/compose_packing.rs"),
        include_str!("search/lexical.rs"),
        include_str!("search/search_core.rs"),
        include_str!("search/hydration.rs"),
        include_str!("search/compose_helpers.rs"),
        include_str!("search/handlers.rs"),
        include_str!("search/rerank.rs"),
    ]
    .join("\n");
    // Build needle at runtime to avoid self-match in include_str.
    let generic_needle: String = [".embed(", "raw_query)"].concat();
    let generic_borrowed_needle: String = [".embed(", "&raw_query)"].concat();
    let generic_count = src
        .lines()
        // Skip lines that are part of this test body (contain "concat" or "needle").
        .filter(|l| !l.contains("concat") && !l.contains("needle"))
        .filter(|l| l.contains(&generic_needle) || l.contains(&generic_borrowed_needle))
        .count();
    assert_eq!(
        generic_count, 0,
        "ANN query paths must not call generic {generic_needle}; \
             found {generic_count} occurrence(s) — use embed_query instead"
    );

    // Positive check (#2307): the assertion above only proves the generic
    // path is *absent* — a mutation that replaced every production
    // `embed_query` call with a different method entirely (e.g.
    // `embed_document`) would still pass it, since that mutation never
    // introduces the generic-embed needle either. Count the query-intent
    // call sites directly so a silent removal (or mutation-away) of one
    // is caught: the shared ANN candidate runner and `compose`'s
    // KG-blend gate use raw_query, while search rerank can independently
    // attempt its query if the candidate runner did not.
    let query_intent_needle: String = [".embed_query(", "raw_query)"].concat();
    let query_intent_borrowed_needle: String = [".embed_query(", "&raw_query)"].concat();
    let query_intent_count = src
        .lines()
        .filter(|l| !l.contains("concat") && !l.contains("needle"))
        .filter(|l| l.contains(&query_intent_needle) || l.contains(&query_intent_borrowed_needle))
        .count();
    let rerank_query_needle: String = [".embed_query(", "query)"].concat();
    let rerank_query_count = src
        .lines()
        .filter(|l| !l.contains("concat") && !l.contains("needle"))
        .filter(|l| l.contains(&rerank_query_needle))
        .count();
    assert_eq!(
        query_intent_count, 2,
        "expected exactly 2 query-intent call sites \
             (shared ANN runner + compose KG-blend gate), found {query_intent_count}"
    );
    assert_eq!(
        rerank_query_count, 1,
        "search rerank must retain its query-intent fallback call site"
    );
}

/// #2232: once a rerank stage has successfully embedded the query, later
/// stages embed candidates only. The recording provider sees the literal
/// query exactly once across two independent candidate pools.
#[tokio::test]
async fn query_embedding_cache_reuses_query_vector_across_reranks() {
    use std::sync::{Arc, Mutex};

    use async_trait::async_trait;
    use khive_runtime::{AllowAllGate, BackendId, EmbedderProvider, RuntimeConfig};
    use lattice_embed::{EmbedError, EmbeddingModel, EmbeddingService};

    const MODEL_KEY: &str = "all-minilm-l6-v2";
    const DIM: usize = 384;

    struct RecordingService {
        texts: Arc<Mutex<Vec<String>>>,
    }

    #[async_trait]
    impl EmbeddingService for RecordingService {
        async fn embed(
            &self,
            texts: &[String],
            _model: EmbeddingModel,
        ) -> Result<Vec<Vec<f32>>, EmbedError> {
            self.texts
                .lock()
                .expect("recording lock")
                .extend(texts.iter().cloned());
            Ok(texts.iter().map(|_| vec![0.5; DIM]).collect())
        }

        fn supports_model(&self, _model: EmbeddingModel) -> bool {
            true
        }

        fn name(&self) -> &'static str {
            "query-reuse-recording-service"
        }
    }

    struct RecordingProvider {
        texts: Arc<Mutex<Vec<String>>>,
    }

    #[async_trait]
    impl EmbedderProvider for RecordingProvider {
        fn name(&self) -> &str {
            MODEL_KEY
        }

        fn dimensions(&self) -> usize {
            DIM
        }

        async fn build(&self) -> Result<Arc<dyn EmbeddingService>, khive_runtime::RuntimeError> {
            Ok(Arc::new(RecordingService {
                texts: Arc::clone(&self.texts),
            }))
        }
    }

    let texts = Arc::new(Mutex::new(Vec::new()));
    let runtime = KhiveRuntime::new(RuntimeConfig {
        wal_ceiling_bytes: 0,
        wal_ceiling_configured_bytes: 0,
        wal_ceiling_source: Default::default(),
        wal_ceiling_env_raw: None,
        web: Default::default(),
        telemetry: Default::default(),
        mounts: Vec::new(),
        brain: Default::default(),
        git_write: Default::default(),
        display_timezone: khive_runtime::config::resolve_default_display_timezone(),
        events_split: None,
        db_path: None,
        blob_hydration_bytes: khive_runtime::DEFAULT_BLOB_HYDRATION_BYTES,
        default_namespace: Namespace::local(),
        embedding_model: Some(EmbeddingModel::AllMiniLmL6V2),
        additional_embedding_models: Vec::new(),
        gate: Arc::new(AllowAllGate),
        packs: vec!["knowledge".to_string()],
        backend_id: BackendId::main(),
        brain_profile: None,
        visible_namespaces: Vec::new(),
        allowed_outbound_namespaces: Vec::new(),
        actor_id: None,
        exec: Default::default(),
        ..khive_runtime::RuntimeConfig::no_embeddings()
    })
    .expect("runtime");
    runtime.register_embedder(RecordingProvider {
        texts: Arc::clone(&texts),
    });

    let query = "one request-local query vector";
    let mut query_embedding = QueryEmbeddingCache::default();
    let first = vec![
        "first candidate".to_string(),
        "second candidate".to_string(),
    ];
    let second = vec!["third candidate".to_string()];
    assert_eq!(
        embed_cosine_scores(&runtime, query, &mut query_embedding, &first)
            .await
            .expect("first rerank")
            .expect("first scores")
            .len(),
        2
    );
    assert!(
        query_embedding.any().is_some(),
        "first rerank must fill cache"
    );
    assert!(
        query_embedding.role_specific.is_not_attempted(),
        "embed_cosine_scores must cache the batch vector as generic, \
             never role_specific — it came from embed_batch, not embed_query"
    );
    assert_eq!(
        embed_cosine_scores(&runtime, query, &mut query_embedding, &second)
            .await
            .expect("second rerank")
            .expect("second scores")
            .len(),
        1
    );

    let recorded = texts.lock().expect("recording lock");
    assert_eq!(
        recorded
            .iter()
            .filter(|text| text.as_str() == query)
            .count(),
        1,
        "the shared query must be embedded exactly once: {recorded:?}"
    );
    assert_eq!(recorded.len(), 4, "one query plus three candidates");
}

// ── #2307: failed query embeddings must not be retried within a request ──
//
// Extends the #2232 `RecordingService` pattern above with a separate,
// overridable `embed_query` (distinct from the generic `embed`) so these
// tests can fail the query embedding on demand and prove which method a
// given text actually reached.

const ROLE_RECORDING_MODEL_KEY: &str = "all-minilm-l6-v2";
const ROLE_RECORDING_DIM: usize = 384;
const ROLE_RECORDING_QUERY: &str = "graph traversal caching strategies distributed \
         knowledge retrieval systems degraded embedding providers";
// Atom content must clear the 20-word minimum; repeats the query terms
// (for lexical relevance) plus filler.
const ROLE_RECORDING_ATOM_CONTENT: &str = "graph traversal caching strategies distributed \
         knowledge retrieval systems degraded embedding providers require resilient fallback \
         behavior across production knowledge retrieval pipelines and search infrastructure";

#[derive(Debug, Default)]
struct RoleAwareRecordingCalls {
    query: Vec<String>,
    generic: Vec<String>,
    query_gate: Option<std::sync::Arc<tokio::sync::Semaphore>>,
}

struct RoleAwareRecordingService {
    calls: std::sync::Arc<std::sync::Mutex<RoleAwareRecordingCalls>>,
    fail_query: std::sync::Arc<std::sync::atomic::AtomicBool>,
}

#[async_trait::async_trait]
impl lattice_embed::EmbeddingService for RoleAwareRecordingService {
    async fn embed(
        &self,
        texts: &[String],
        _model: lattice_embed::EmbeddingModel,
    ) -> Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
        self.calls
            .lock()
            .expect("recording lock")
            .generic
            .extend(texts.iter().cloned());
        Ok(texts
            .iter()
            .map(|_| vec![0.5; ROLE_RECORDING_DIM])
            .collect())
    }

    async fn embed_query(
        &self,
        texts: &[String],
        _model: lattice_embed::EmbeddingModel,
    ) -> Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
        let gate = {
            let mut calls = self.calls.lock().expect("recording lock");
            calls.query.extend(texts.iter().cloned());
            calls.query_gate.clone()
        };
        if let Some(gate) = gate {
            gate.acquire()
                .await
                .expect("query gate remains open")
                .forget();
        }
        if self.fail_query.load(std::sync::atomic::Ordering::SeqCst) {
            return Err(lattice_embed::EmbedError::InferenceFailed(
                "forced query-embedding failure".into(),
            ));
        }
        Ok(texts
            .iter()
            .map(|_| vec![0.25; ROLE_RECORDING_DIM])
            .collect())
    }

    fn supports_model(&self, _model: lattice_embed::EmbeddingModel) -> bool {
        true
    }

    fn name(&self) -> &'static str {
        "role-aware-recording-service"
    }
}

struct RoleAwareRecordingProvider {
    calls: std::sync::Arc<std::sync::Mutex<RoleAwareRecordingCalls>>,
    fail_query: std::sync::Arc<std::sync::atomic::AtomicBool>,
}

#[async_trait::async_trait]
impl khive_runtime::EmbedderProvider for RoleAwareRecordingProvider {
    fn name(&self) -> &str {
        ROLE_RECORDING_MODEL_KEY
    }

    fn dimensions(&self) -> usize {
        ROLE_RECORDING_DIM
    }

    async fn build(
        &self,
    ) -> Result<std::sync::Arc<dyn lattice_embed::EmbeddingService>, RuntimeError> {
        Ok(std::sync::Arc::new(RoleAwareRecordingService {
            calls: std::sync::Arc::clone(&self.calls),
            fail_query: std::sync::Arc::clone(&self.fail_query),
        }))
    }
}

fn rt_with_role_aware_recording_embedder() -> (
    KhiveRuntime,
    std::sync::Arc<std::sync::Mutex<RoleAwareRecordingCalls>>,
    std::sync::Arc<std::sync::atomic::AtomicBool>,
) {
    let calls = std::sync::Arc::new(std::sync::Mutex::new(RoleAwareRecordingCalls::default()));
    let fail_query = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
    let runtime = KhiveRuntime::new(khive_runtime::RuntimeConfig {
        wal_ceiling_bytes: 0,
        wal_ceiling_configured_bytes: 0,
        wal_ceiling_source: Default::default(),
        wal_ceiling_env_raw: None,
        web: Default::default(),
        telemetry: Default::default(),
        mounts: Vec::new(),
        brain: Default::default(),
        git_write: Default::default(),
        display_timezone: khive_runtime::config::resolve_default_display_timezone(),
        events_split: None,
        db_path: None,
        blob_hydration_bytes: khive_runtime::DEFAULT_BLOB_HYDRATION_BYTES,
        default_namespace: Namespace::local(),
        embedding_model: Some(lattice_embed::EmbeddingModel::AllMiniLmL6V2),
        additional_embedding_models: Vec::new(),
        gate: std::sync::Arc::new(khive_runtime::AllowAllGate),
        packs: vec!["kg".to_string(), "knowledge".to_string()],
        backend_id: khive_runtime::BackendId::main(),
        brain_profile: None,
        visible_namespaces: Vec::new(),
        allowed_outbound_namespaces: Vec::new(),
        actor_id: None,
        exec: Default::default(),
        ..khive_runtime::RuntimeConfig::no_embeddings()
    })
    .expect("in-memory runtime");
    runtime.register_embedder(RoleAwareRecordingProvider {
        calls: std::sync::Arc::clone(&calls),
        fail_query: std::sync::Arc::clone(&fail_query),
    });
    (runtime, calls, fail_query)
}

#[tokio::test]
async fn search_and_suggest_start_lexical_reads_while_query_embedding_waits() {
    for suggest in [false, true] {
        let (runtime, calls, _) = rt_with_role_aware_recording_embedder();
        let access = runtime.sql();
        let mut writer = access.writer().await.expect("writer");
        writer
                .execute(SqlStatement {
                    sql: "INSERT INTO knowledge_atoms \
                          (id, namespace, slug, name, content, tags, finalized, status, created_at, updated_at) \
                          VALUES ('94000000-0000-0000-0000-000000000238', 'local', \
                                  'candidate-overlap', 'Candidate Overlap', \
                                  'graph traversal caching strategies distributed knowledge retrieval', \
                                  '[]', 1, 'reviewed', 0, 0)"
                        .into(),
                    params: Vec::new(),
                    label: None,
                })
                .await
                .expect("seed lexical hit");
        drop(writer);
        let gate = Arc::new(tokio::sync::Semaphore::new(0));
        calls.lock().expect("recording lock").query_gate = Some(Arc::clone(&gate));
        let token = runtime.authorize(Namespace::local()).expect("token");
        let ann = vamana::new_shared_for_role(false);
        let probes = Arc::new(Mutex::new(Vec::new()));
        let query = "graph traversal caching strategies distributed knowledge retrieval";
        let future = TERM_PROBES.scope(probes.clone(), async {
            if suggest {
                KnowledgeHandlers::suggest(&runtime, &token, json!({"query": query}), &ann).await
            } else {
                KnowledgeHandlers::search(
                    &runtime,
                    &token,
                    json!({"query": query, "rerank": false}),
                    &ann,
                )
                .await
            }
        });
        tokio::pin!(future);
        let overlapped = tokio::time::timeout(std::time::Duration::from_secs(3), async {
                loop {
                    let embedding_started = !calls.lock().expect("recording lock").query.is_empty();
                    let lexical_started = !probes.lock().expect("term probes").is_empty();
                    if embedding_started && lexical_started {
                        break;
                    }
                    tokio::select! {
                        result = &mut future => panic!("candidate stages finished before gate release: {result:?}"),
                        () = tokio::time::sleep(std::time::Duration::from_millis(1)) => {}
                    }
                }
            })
            .await;
        gate.add_permits(1);
        assert!(
            overlapped.is_ok(),
            "lexical read waited for embedding: suggest={suggest}"
        );
        let response = future
            .await
            .expect("candidate stages complete after gate release");
        if !suggest {
            assert_eq!(response["results"][0]["slug"], "candidate-overlap");
            assert_eq!(response["candidate_provenance"]["lexical"], "matched");
        }
    }
}

#[tokio::test(start_paused = true)]
async fn failed_embedding_and_timed_out_lexical_stage_return_degraded_search() {
    let (runtime, _, fail_query) = rt_with_role_aware_recording_embedder();
    fail_query.store(true, std::sync::atomic::Ordering::SeqCst);
    let token = runtime.authorize(Namespace::local()).expect("token");
    let ann = vamana::new_shared_for_role(false);
    let response = crate::knowledge::lexical_timeout::tests::with_timeout(
        vec![LexicalPhase::ReaderOpen],
        std::time::Duration::from_millis(1),
        KnowledgeHandlers::search(
            &runtime,
            &token,
            json!({"query": "graph traversal caching strategies", "rerank": false}),
            &ann,
        ),
    )
    .await
    .expect("both recoverable candidate failures remain a response");
    assert_eq!(response["total"], 0);
    assert_eq!(response["candidate_provenance"]["lexical"], "timed_out");
    assert_eq!(response["degraded"]["lexical_timeout"], true);
}

#[tokio::test(start_paused = true)]
async fn completed_ann_candidate_survives_lexical_stage_timeout() {
    let (runtime, _, _) = rt_with_role_aware_recording_embedder();
    let runtime = runtime.with_ann_fresh_tail_enabled(false);
    let atom_id = Uuid::from_u128(0x94000000000000000000000000000239);
    let access = runtime.sql();
    let mut writer = access.writer().await.expect("writer");
    writer
            .execute(SqlStatement {
                sql: "INSERT INTO knowledge_atoms \
                      (id, namespace, slug, name, content, tags, finalized, status, created_at, updated_at) \
                      VALUES (?1, 'local', 'ann-timeout-survivor', 'ANN Timeout Survivor', \
                              'dense candidate outside this lexical query', '[]', 1, 'reviewed', 0, 0)"
                    .into(),
                params: vec![SqlValue::Text(atom_id.to_string())],
                label: None,
            })
            .await
            .expect("seed ANN hydration row");
    writer
        .execute(SqlStatement {
            sql: "INSERT INTO ann_consumer_watermark \
                      (consumer, namespace, embedding_model, watermark) \
                      VALUES ('knowledge:knowledge.atom', 'local', ?1, 0)"
                .into(),
            params: vec![SqlValue::Text(ROLE_RECORDING_MODEL_KEY.into())],
            label: None,
        })
        .await
        .expect("register loaded ANN consumer");
    drop(writer);

    let ann = vamana::new_shared_for_role(false);
    let key = vamana::AnnKey::new("local", ROLE_RECORDING_MODEL_KEY);
    let bridge = vamana::AnnBridge::build(
        vec![0.25; ROLE_RECORDING_DIM],
        ROLE_RECORDING_DIM,
        vec![atom_id],
    )
    .expect("build loaded bridge");
    vamana::install_if_fresher(&ann, &key, bridge).await;
    let token = runtime.authorize(Namespace::local()).expect("token");
    let response = crate::knowledge::lexical_timeout::tests::with_timeout(
        vec![LexicalPhase::ReaderOpen],
        std::time::Duration::from_millis(1),
        KnowledgeHandlers::search(
            &runtime,
            &token,
            json!({"query": "graph traversal caching strategies", "rerank": false}),
            &ann,
        ),
    )
    .await
    .expect("lexical timeout retains completed ANN result");
    assert_eq!(response["total"], 1);
    assert_eq!(response["results"][0]["slug"], "ann-timeout-survivor");
    assert_eq!(
        response["results"][0]["score_provenance"]["sources"],
        json!(["ann"])
    );
    assert_eq!(response["candidate_provenance"]["fallback"], "ann");
    assert_eq!(response["degraded"]["lexical_timeout"], true);
}

fn build_role_recording_registry(rt: &KhiveRuntime) -> khive_runtime::VerbRegistry {
    let mut builder = khive_runtime::VerbRegistryBuilder::new();
    builder.register(khive_pack_kg::KgPack::new(rt.clone()));
    builder.register(crate::KnowledgePack::new(rt.clone()));
    let registry = builder.build().expect("registry builds");
    rt.install_edge_rules(registry.all_edge_rules());
    registry
}

/// Seeds one atom as a domain member so `compose`'s auto flow reaches the
/// Rerank phase; auto-compose skips domains with no live members before
/// the KG-blend gate these tests exercise.
async fn seed_role_recording_corpus(registry: &khive_runtime::VerbRegistry) {
    registry
        .dispatch(
            "knowledge.upsert_atoms",
            json!({
                "atoms": [{
                    "slug": "role-recording-atom",
                    "name": "Role Recording Atom",
                    "finalized": true,
                    "content": ROLE_RECORDING_ATOM_CONTENT
                }]
            }),
        )
        .await
        .expect("upsert atom");
    registry
        .dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [{
                    "slug": "role-recording-domain",
                    "name": "Role Recording Domain",
                    "description": ROLE_RECORDING_ATOM_CONTENT,
                    "members": ["role-recording-atom"]
                }]
            }),
        )
        .await
        .expect("upsert domain");
    registry
        .dispatch("knowledge.index", json!({ "rebuild_ann": false }))
        .await
        .expect("index");
}

/// (a) A failed `embed_query` inside `suggest` must not be retried by
/// `compose`'s KG-blend gate immediately afterward in the same
/// auto-compose request. Before the fix, `compose` saw
/// `role_specific: None` — indistinguishable from "never tried" — and
/// spent a second failing provider call; the query text was attempted
/// twice. Red before the fix: 2 attempts.
#[tokio::test]
async fn compose_auto_does_not_retry_role_specific_embed_after_suggest_failure() {
    let (rt, calls, fail_query) = rt_with_role_aware_recording_embedder();
    let registry = build_role_recording_registry(&rt);
    seed_role_recording_corpus(&registry).await;
    fail_query.store(true, std::sync::atomic::Ordering::SeqCst);

    let ann = vamana::new_shared();
    let token = rt.authorize(Namespace::local()).expect("authorize");
    let result = KnowledgeHandlers::compose(
        &rt,
        &token,
        json!({ "query": ROLE_RECORDING_QUERY }),
        &ann,
        HashMap::new(),
    )
    .await
    .expect("compose must not Err when the query embedding is degraded");

    let attempts = calls
        .lock()
        .expect("recording lock")
        .query
        .iter()
        .filter(|text| text.as_str() == ROLE_RECORDING_QUERY)
        .count();
    assert_eq!(
        attempts, 1,
        "a failed embed_query must not be retried later in the same request; result: {result}"
    );
    assert_eq!(
        result["data"]["count"].as_u64(),
        Some(1),
        "the atom must still reach the briefing through the degraded blend path; \
             result: {result}"
    );
}

/// (b) Control: without a prior failure, `suggest`'s successful
/// role-specific embed is the *only* attempt across the whole
/// auto-compose request — `compose`'s KG-blend gate reuses it rather than
/// embedding again. The narrower claim (a rerank stage specifically
/// reuses a cached vector across candidate batches) is covered by
/// `query_embedding_cache_reuses_query_vector_across_reranks` (#2232)
/// above; this test covers the handler-level chain instead.
#[tokio::test]
async fn compose_auto_reuses_successful_suggest_embed_without_retry() {
    let (rt, calls, _fail_query) = rt_with_role_aware_recording_embedder();
    let registry = build_role_recording_registry(&rt);
    seed_role_recording_corpus(&registry).await;

    let ann = vamana::new_shared();
    let token = rt.authorize(Namespace::local()).expect("authorize");
    let result = KnowledgeHandlers::compose(
        &rt,
        &token,
        json!({ "query": ROLE_RECORDING_QUERY }),
        &ann,
        HashMap::new(),
    )
    .await
    .expect("compose must not Err");

    let attempts = calls
        .lock()
        .expect("recording lock")
        .query
        .iter()
        .filter(|text| text.as_str() == ROLE_RECORDING_QUERY)
        .count();
    assert_eq!(
        attempts, 1,
        "a successful embed_query must be reused, not repeated; result: {result}"
    );
    assert_eq!(
        result["data"]["count"].as_u64(),
        Some(1),
        "result: {result}"
    );
}

/// (c) Control: a `compose` call that never goes through `suggest` (explicit
/// `domain_ids`, so auto-mode never runs) still embeds the query exactly
/// once for its own KG-blend gate — `NotAttempted` always authorizes the
/// one attempt a stage that never tried is entitled to.
#[tokio::test]
async fn compose_direct_call_without_suggest_still_embeds_query_once() {
    let (rt, calls, _fail_query) = rt_with_role_aware_recording_embedder();
    let registry = build_role_recording_registry(&rt);
    seed_role_recording_corpus(&registry).await;

    let ann = vamana::new_shared();
    let token = rt.authorize(Namespace::local()).expect("authorize");
    let result = KnowledgeHandlers::compose(
        &rt,
        &token,
        json!({
            "query": ROLE_RECORDING_QUERY,
            "domain_ids": ["role-recording-domain"],
        }),
        &ann,
        HashMap::new(),
    )
    .await
    .expect("compose must not Err");

    let attempts = calls
        .lock()
        .expect("recording lock")
        .query
        .iter()
        .filter(|text| text.as_str() == ROLE_RECORDING_QUERY)
        .count();
    assert_eq!(
        attempts, 1,
        "a stage that never tried must still embed once; result: {result}"
    );
    assert_eq!(
        result["data"]["count"].as_u64(),
        Some(1),
        "result: {result}"
    );
}

/// (d) #2307 item 2: `search`, `suggest`, and `compose` must each dispatch
/// the query text through `EmbeddingService::embed_query` specifically —
/// never through the generic `embed`. The source-scan guard below this
/// test only ever checked the generic path's *absence*; a mutation
/// swapping every production `embed_query` call for `embed_document`
/// still passed it (documented in the crate's fix report for #2307).
/// This behavioral check closes that gap by observing which method the
/// query text actually reaches at runtime.
#[tokio::test]
async fn search_suggest_compose_dispatch_query_through_embed_query_not_generic() {
    for verb in ["search", "suggest", "compose"] {
        let (rt, calls, _fail_query) = rt_with_role_aware_recording_embedder();
        let registry = build_role_recording_registry(&rt);
        seed_role_recording_corpus(&registry).await;
        let ann = vamana::new_shared();
        let token = rt.authorize(Namespace::local()).expect("authorize");

        match verb {
            "search" => {
                KnowledgeHandlers::search(
                    &rt,
                    &token,
                    json!({ "query": ROLE_RECORDING_QUERY }),
                    &ann,
                )
                .await
                .expect("search must not Err");
            }
            "suggest" => {
                KnowledgeHandlers::suggest(
                    &rt,
                    &token,
                    json!({ "query": ROLE_RECORDING_QUERY }),
                    &ann,
                )
                .await
                .expect("suggest must not Err");
            }
            "compose" => {
                KnowledgeHandlers::compose(
                    &rt,
                    &token,
                    json!({ "query": ROLE_RECORDING_QUERY }),
                    &ann,
                    HashMap::new(),
                )
                .await
                .expect("compose must not Err");
            }
            _ => unreachable!(),
        }

        let recorded = calls.lock().expect("recording lock");
        assert!(
            recorded
                .query
                .iter()
                .any(|text| text == ROLE_RECORDING_QUERY),
            "{verb}: query text must reach embed_query at least once; recorded={recorded:?}"
        );
        assert!(
            !recorded
                .generic
                .iter()
                .any(|text| text == ROLE_RECORDING_QUERY),
            "{verb}: query text must never reach the generic embed path; recorded={recorded:?}"
        );
    }
}

// ── filter_by_excluded_statuses ───────────────────────────────────────────

fn make_hit(id: &str, status: Option<&str>, score: f32) -> ScoredHit {
    ScoredHit {
        id: id.to_string(),
        slug: id.to_string(),
        name: id.to_string(),
        content: None,
        tags: None,
        atom_embed_text: None,
        finalized: false,
        is_domain: false,
        status: status.map(str::to_string),
        score,
        provenance: ScoreProvenance::lexical(),
    }
}

fn make_ann_hit(id: &str, status: Option<&str>, score: f32) -> ScoredHit {
    let mut hit = make_hit(id, status, score);
    hit.provenance = ScoreProvenance::ann();
    hit
}

#[test]
fn rrf_overlap_retains_both_candidate_sources() {
    let mut hits = vec![
        make_hit("shared", Some("reviewed"), 8.0),
        make_hit("lexical-only", Some("reviewed"), 4.0),
    ];
    let ann = vec![
        make_ann_hit("shared", Some("reviewed"), 0.9),
        make_ann_hit("ann-only", Some("reviewed"), 0.8),
    ];
    fuse_ann_hits(&mut hits, &ann, 0.0);

    assert_eq!(hits.len(), 3);
    assert_eq!(hits[0].id, "shared");
    assert_eq!(hits[0].score, 1.0);
    assert_eq!(
        hits[0].provenance.to_json(),
        json!({
            "sources": ["lexical", "ann"],
            "embedding_rerank": false,
            "normalization": "s_over_s_plus_1",
            "calibrated": false,
        })
    );
    let lexical = hits.iter().find(|hit| hit.id == "lexical-only").unwrap();
    let ann = hits.iter().find(|hit| hit.id == "ann-only").unwrap();
    assert_eq!(lexical.provenance, ScoreProvenance::lexical());
    assert_eq!(ann.provenance, ScoreProvenance::ann());
    assert_eq!(lexical.score, ann.score);
}

#[test]
fn decomposed_overlap_merges_provenance_without_counting_duplicates_twice() {
    let full = vec![
        make_hit("shared", Some("reviewed"), 8.0),
        make_hit("full-only", Some("reviewed"), 6.0),
    ];
    let subqueries = [
        vec![
            make_ann_hit("shared", Some("reviewed"), 100.0),
            make_hit("sub-shared", Some("reviewed"), 10.0),
            make_ann_hit("sub-shared", Some("reviewed"), 50.0),
        ],
        vec![
            make_ann_hit("shared", Some("reviewed"), 100.0),
            make_ann_hit("sub-shared", Some("reviewed"), 50.0),
            make_ann_hit("ann-only", Some("reviewed"), 2.0),
        ],
    ];
    let hits = merge_decomposed_hits(full, subqueries, 0.5, 10);

    let ids: Vec<_> = hits.iter().map(|hit| hit.id.as_str()).collect();
    assert_eq!(ids, ["shared", "full-only", "sub-shared", "ann-only"]);
    assert_eq!(hits[0].score, 12.0);
    assert_eq!(hits[1].score, 6.0);
    assert_eq!(hits[2].score, 4.5);
    assert!((hits[3].score - 0.6).abs() < 1e-6);
    for hit in [&hits[0], &hits[2]] {
        assert!(hit.provenance.lexical && hit.provenance.ann);
        assert!(!hit.provenance.embedding_rerank);
    }
    assert_eq!(hits[1].provenance, ScoreProvenance::lexical());
    assert_eq!(hits[3].provenance, ScoreProvenance::ann());
}

#[test]
fn candidate_fallback_requires_ann_without_any_lexical_source() {
    let lexical = make_hit("lexical", None, 1.0);
    let ann = make_ann_hit("ann", None, 1.0);
    let mut overlap = lexical.clone();
    overlap.provenance.merge_sources(ann.provenance);
    assert_eq!(candidate_fallback(&[]), "none");
    assert_eq!(candidate_fallback(std::slice::from_ref(&ann)), "ann");
    assert_eq!(candidate_fallback(std::slice::from_ref(&lexical)), "none");
    assert_eq!(candidate_fallback(&[lexical, ann]), "none");
    assert_eq!(candidate_fallback(&[overlap]), "none");
}

#[tokio::test]
async fn skipped_embedding_rerank_preserves_scores_and_provenance() {
    let runtime = KhiveRuntime::new(khive_runtime::RuntimeConfig {
        db_path: None,
        ..khive_runtime::RuntimeConfig::no_embeddings()
    })
    .expect("in-memory runtime without embeddings");
    assert!(runtime.config().db_path.is_none());
    let mut hits = vec![
        make_hit("lexical", None, 3.0),
        make_ann_hit("ann", None, 0.8),
    ];
    let applied = rerank_with_embeddings(
        &runtime,
        "provenance query",
        &mut QueryEmbeddingCache::default(),
        &mut hits,
        0.7,
    )
    .await
    .expect("optional rerank");
    assert!(!applied);
    assert_eq!(hits[0].score, 3.0);
    assert_eq!(hits[1].score, 0.8);
    assert_eq!(hits[0].provenance, ScoreProvenance::lexical());
    assert_eq!(hits[1].provenance, ScoreProvenance::ann());
}

#[tokio::test]
async fn search_reports_lexical_and_ann_sources_and_successful_embedding_rerank() {
    let (runtime, calls, fail_query) = rt_with_role_aware_recording_embedder();
    let registry = build_role_recording_registry(&runtime);
    seed_role_recording_corpus(&registry).await;
    fail_query.store(false, std::sync::atomic::Ordering::SeqCst);
    let token = runtime.authorize(Namespace::local()).expect("local token");
    let ann = vamana::new_shared();

    for rerank in [false, true] {
        calls.lock().expect("recording lock").generic.clear();
        let response = KnowledgeHandlers::search(
            &runtime,
            &token,
            json!({"query": ROLE_RECORDING_QUERY, "kind": "atom", "rerank": rerank}),
            &ann,
        )
        .await
        .expect("search");
        assert_eq!(response["total"], 1, "{response}");
        assert_eq!(response["results"][0]["slug"], "role-recording-atom");
        assert_eq!(
            response["results"][0]["score_provenance"],
            json!({
                "sources": ["lexical", "ann"],
                "embedding_rerank": rerank,
                "normalization": "s_over_s_plus_1",
                "calibrated": false,
            })
        );
        let recorded = calls.lock().expect("recording lock");
        if rerank {
            assert_eq!(
                response["rerank_provenance"]["stored_vector_lookup"],
                "supported"
            );
            assert_eq!(response["rerank_provenance"]["from_stored"], 1);
            assert_eq!(response["rerank_provenance"]["embedded_fallback"], 0);
            assert!(recorded.generic.is_empty());
        } else {
            assert!(response.get("rerank_provenance").is_none());
            assert!(recorded.generic.is_empty());
        }
    }
}

#[test]
fn explicit_status_is_an_exact_allowlist_for_hydrated_hits() {
    let mut hits = vec![
        make_hit("reviewed", Some("reviewed"), 0.9),
        make_hit("draft", Some("draft"), 0.8),
        make_hit("deprecated", Some("deprecated"), 0.7),
        make_hit("missing-status", None, 0.6),
    ];
    filter_hits_by_status(&mut hits, &["draft".to_string()], &[]);
    let ids: Vec<&str> = hits.iter().map(|hit| hit.id.as_str()).collect();
    assert_eq!(ids, ["draft"]);
}

#[test]
fn deprecated_multiplier_gate_uses_resolved_status_policy() {
    assert!(!deprecated_allowed_by_status_policy(
        &[],
        &["draft", "deprecated"]
    ));
    assert!(!deprecated_allowed_by_status_policy(&[], &["deprecated"]));
    assert!(deprecated_allowed_by_status_policy(&[], &["reviewed"]));
    assert!(!deprecated_allowed_by_status_policy(
        &["reviewed".to_string()],
        &[]
    ));
    assert!(deprecated_allowed_by_status_policy(
        &["deprecated".to_string()],
        &[]
    ));
}

#[test]
fn filter_excluded_statuses_removes_draft_hits() {
    let mut hits = vec![
        make_hit("reviewed-1", Some("reviewed"), 0.8),
        make_hit("draft-1", Some("draft"), 0.7),
        make_hit("reviewed-2", Some("reviewed"), 0.6),
        make_hit("draft-2", Some("draft"), 0.5),
    ];
    filter_by_excluded_statuses(&mut hits, &["draft", "deprecated"]);
    let ids: Vec<&str> = hits.iter().map(|h| h.id.as_str()).collect();
    assert_eq!(
        ids,
        ["reviewed-1", "reviewed-2"],
        "draft hits must be removed"
    );
}

#[test]
fn filter_excluded_statuses_removes_deprecated_hits() {
    let mut hits = vec![
        make_hit("reviewed-1", Some("reviewed"), 0.9),
        make_hit("deprecated-1", Some("deprecated"), 0.8),
    ];
    filter_by_excluded_statuses(&mut hits, &["draft", "deprecated"]);
    let ids: Vec<&str> = hits.iter().map(|h| h.id.as_str()).collect();
    assert_eq!(ids, ["reviewed-1"]);
}

#[test]
fn filter_excluded_statuses_empty_list_is_noop() {
    let mut hits = vec![
        make_hit("draft-1", Some("draft"), 0.9),
        make_hit("reviewed-1", Some("reviewed"), 0.8),
    ];
    filter_by_excluded_statuses(&mut hits, &[]);
    assert_eq!(hits.len(), 2, "empty exclude list must be a no-op");
}

#[test]
fn filter_excluded_statuses_null_status_treated_as_not_excluded() {
    // Hits with no status (ANN-sourced before hydration completes) must not
    // be removed by the status exclusion — they are not drafts or deprecated.
    let mut hits = vec![
        make_hit("no-status", None, 0.9),
        make_hit("draft-1", Some("draft"), 0.7),
    ];
    filter_by_excluded_statuses(&mut hits, &["draft", "deprecated"]);
    let ids: Vec<&str> = hits.iter().map(|h| h.id.as_str()).collect();
    assert_eq!(ids, ["no-status"], "null-status hit must survive exclusion");
}

#[test]
fn normalize_rrf_score_is_bounded_and_monotonic() {
    let k = RRF_K;
    let max_single = 1.0f32 / (k as f32 + 1.0);
    let scores_single = [
        max_single * 0.25,
        max_single * 0.5,
        max_single,
        max_single * 1.5,
    ];
    let normed_single: Vec<f32> = scores_single
        .iter()
        .map(|&r| normalize_rrf_score(r, 1, k))
        .collect();
    for &s in &normed_single {
        assert!((0.0..=1.0).contains(&s), "score out of range: {s}");
    }
    assert!(normed_single[0] < normed_single[1]);
    assert!(normed_single[1] < normed_single[2]);
    assert_eq!(normed_single[3], 1.0);

    let max_two = 2.0f32 / (k as f32 + 1.0);
    let scores_two = [max_two * 0.25, max_two * 0.75, max_two, max_two * 2.0];
    let normed_two: Vec<f32> = scores_two
        .iter()
        .map(|&r| normalize_rrf_score(r, 2, k))
        .collect();
    for &s in &normed_two {
        assert!((0.0..=1.0).contains(&s), "score out of range: {s}");
    }
    assert!(normed_two[0] < normed_two[1]);
    assert!(normed_two[1] < normed_two[2]);
    assert_eq!(normed_two[3], 1.0);

    let raw = [0.001f32, 0.005, 0.010, 0.015];
    let normed: Vec<f32> = raw.iter().map(|&r| normalize_rrf_score(r, 1, k)).collect();
    let raw_order: Vec<usize> = {
        let mut idx: Vec<usize> = (0..raw.len()).collect();
        idx.sort_by(|&a, &b| raw[b].partial_cmp(&raw[a]).unwrap());
        idx
    };
    let norm_order: Vec<usize> = {
        let mut idx: Vec<usize> = (0..normed.len()).collect();
        idx.sort_by(|&a, &b| normed[b].partial_cmp(&normed[a]).unwrap());
        idx
    };
    assert_eq!(
        raw_order, norm_order,
        "normalization must not invert ranking"
    );
}

#[test]
fn normalize_rrf_score_zero_source_count_returns_zero() {
    assert_eq!(normalize_rrf_score(0.5, 0, RRF_K), 0.0);
}

/// Fusion-admitted hit whose score the status multiplier squashes below
/// `min_score` must not survive the late floor. Reproduction arithmetic:
/// a single-source RRF top hit normalizes to 1.0, then `s/(s+1)` with
/// multiplier 1.0 squashes it to 0.5 — below a 0.7 floor.
#[test]
fn min_score_floor_drops_hit_squashed_below_threshold_by_status_multiplier() {
    let mut hits = vec![make_hit("atom-1", Some("reviewed"), 0.0)];
    fuse_ann_hits(&mut hits, &[], 0.7);
    assert_eq!(hits.len(), 1, "fusion stage must admit the 1.0 RRF hit");
    assert_eq!(hits[0].score, 1.0);

    apply_status_multipliers(&mut hits, false);
    assert!((hits[0].score - 0.5).abs() < 1e-6, "1.0 squashes to 0.5");

    enforce_min_score_floor(&mut hits, 0.7);
    assert!(
        hits.is_empty(),
        "0.5 post-multiplier score must not clear a 0.7 floor"
    );
}

#[test]
fn min_score_floor_keeps_hit_at_or_above_threshold_after_multiplier() {
    let mut hits = vec![make_hit("atom-1", Some("reviewed"), 0.0)];
    fuse_ann_hits(&mut hits, &[], 0.4);
    assert_eq!(hits.len(), 1, "fusion stage must admit the 1.0 RRF hit");

    apply_status_multipliers(&mut hits, false);
    let squashed = hits[0].score;

    enforce_min_score_floor(&mut hits, 0.4);
    assert_eq!(
        hits.len(),
        1,
        "0.5 post-multiplier score clears a 0.4 floor"
    );
    assert_eq!(hits[0].id, "atom-1");
    assert!(hits[0].score >= 0.4);
    assert_eq!(hits[0].score, squashed, "floor must not rewrite scores");
}

/// `min_score = 0.0` (the absent default) must return the identical set —
/// the floor cannot alter the no-threshold path.
#[test]
fn min_score_floor_zero_is_noop_on_multiplier_survivors() {
    let build = || {
        let mut hits = vec![
            make_hit("atom-1", Some("reviewed"), 0.9),
            make_hit("atom-2", Some("draft"), 0.6),
            make_hit("atom-3", Some("deprecated"), 0.8),
        ];
        apply_status_multipliers(&mut hits, false);
        hits
    };

    let before = build();
    let mut after = build();
    enforce_min_score_floor(&mut after, 0.0);

    let ids_before: Vec<&str> = before.iter().map(|h| h.id.as_str()).collect();
    let ids_after: Vec<&str> = after.iter().map(|h| h.id.as_str()).collect();
    assert_eq!(ids_after, ids_before);
    for (a, b) in after.iter().zip(before.iter()) {
        assert_eq!(a.score, b.score);
    }
}

/// Body-line metadata is best-effort: a read-deadline timeout during the
/// aggregate lookup degrades to `Ok(None)` (rendered as `body_lines:
/// null` plus a degradation flag by the handler) instead of failing an
/// already-ranked search with an `Internal` error.
#[tokio::test]
async fn body_line_counts_degrade_to_none_under_expired_read_deadline() {
    let runtime = KhiveRuntime::memory().expect("in-memory runtime");
    let atom_ids = vec!["10000000-0000-0000-0000-000000000001".to_owned()];

    let degraded = khive_storage::scope_request_read_deadline(
        std::time::Duration::ZERO,
        load_atom_body_line_counts(&runtime, "local", &atom_ids),
    )
    .await;
    assert!(
        matches!(degraded, Ok(None)),
        "an expired read deadline must degrade body-line metadata to \
             None, never error the search; got {degraded:?}"
    );

    let healthy = load_atom_body_line_counts(&runtime, "local", &atom_ids)
        .await
        .expect("undeadlined lookup must succeed");
    assert_eq!(
        healthy.and_then(|counts| counts.get(&atom_ids[0]).copied()),
        Some(0),
        "control: without a deadline the lookup returns real counts"
    );
}

#[path = "namespace_fallback_tests.rs"]
mod namespace_fallback_tests;