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
//! Retrieval operations: local embedding generation and hybrid search with RRF fusion.
use std::collections::{HashMap, HashSet};
use lattice_embed::{EmbeddingModel, MAX_TEXT_BYTES};
use uuid::Uuid;
use crate::config::{parse_embedding_model_alias, sanitize_key};
use crate::curation::note_fts_document;
use crate::embedder_registry::with_embedding_admission;
use crate::error::{RuntimeError, RuntimeResult};
use crate::runtime::{KhiveRuntime, NamespaceToken};
use khive_retrieval::hybrid::{combine_leg_first_appearance, fuse_labelled, HitLabel};
use khive_score::DeterministicScore;
use khive_storage::types::{
PageRequest, TextFilter, TextQueryMode, TextSearchHit, TextSearchRequest, VectorRecord,
VectorSearchHit, VectorSearchRequest,
};
use khive_storage::ContentRef;
use khive_storage::EntityFilter;
use khive_types::SubstrateKind;
pub use khive_retrieval::{
HybridSearchOutcome, RankScoreKind, SearchHit, SearchSignals, SearchSource,
};
/// Bounds provider input and per-page outcome memory while amortizing model setup.
pub(crate) const EMBEDDING_BATCH_PAGE_SIZE: usize = 256;
// Fault-injection flag for backfill reader errors (test / `fault-injection` builds only).
#[cfg(any(test, feature = "fault-injection"))]
std::thread_local! {
static BACKFILL_READER_FAIL: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
/// Arm the backfill reader fault injection: the next `backfill_missing_embeddings`
/// call substitutes a `StorageError::Pool` for `sql.reader().await`'s result, then
/// resets the flag. The injected error passes through the same
/// `map_err(RuntimeError::Storage)?` path as a real reader failure, exercising the
/// fail-closed guard rather than bypassing it.
#[cfg(any(test, feature = "fault-injection"))]
pub fn arm_backfill_reader_fail() {
BACKFILL_READER_FAIL.with(|c| c.set(true));
}
/// RRF constant. Controls how strongly top ranks dominate.
///
/// The paper's k=60 over-compresses scores at KG scale (tens–thousands of
/// entities): rank 1 ≈ 0.016, rank 10 ≈ 0.014, spread ≈ 0.002. k=10 gives
/// rank 1 ≈ 0.091, rank 10 ≈ 0.050, spread ≈ 0.041 — 20× better discrimination,
/// which dedup-before-create needs at graph sizes of 50–2700 entities.
const RRF_K: usize = 10;
/// Candidates pulled per path before fusion. Higher = better recall, more work.
const CANDIDATE_MULTIPLIER: u32 = 4;
/// Advisory emitted by write verbs when only the embedding input was bounded.
pub const EMBEDDING_INPUT_TRUNCATED_WARNING: &str =
"embedding input was truncated to the embedder maximum; full content was stored unchanged";
/// Outcome for one document embedding. The exact-input fingerprint is present
/// only when the resolved provider has the runtime-audited preparation path.
#[derive(Clone, Debug)]
pub struct DocumentEmbeddingOutcome {
pub model_name: String,
pub vector: Vec<f32>,
/// Digest of the exact prepared input, or `None` for an unaudited provider.
pub prepared_text_fingerprint: Option<ContentRef>,
pub source_bytes: usize,
pub embedded_bytes: usize,
pub truncated: bool,
}
/// Aggregate truncation accounting for one logical write or reindex batch.
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct EmbeddingTruncationReport {
pub truncated: u64,
pub discarded_bytes: u64,
}
impl EmbeddingTruncationReport {
pub fn observe(&mut self, outcome: &DocumentEmbeddingOutcome) {
if outcome.truncated {
self.truncated += 1;
self.discarded_bytes +=
outcome.source_bytes.saturating_sub(outcome.embedded_bytes) as u64;
}
}
#[must_use]
pub const fn any_truncated(&self) -> bool {
self.truncated > 0
}
/// Add another batch's counters without wrapping on overflow.
pub fn merge(&mut self, other: Self) {
self.truncated = self.truncated.saturating_add(other.truncated);
self.discarded_bytes = self.discarded_bytes.saturating_add(other.discarded_bytes);
}
}
/// Maximum document bytes accepted before the embedding service adds its model prefix.
pub fn document_embedding_budget(model_name: &str) -> usize {
parse_embedding_model_alias(model_name)
.and_then(|model| model.document_instruction())
.map_or(MAX_TEXT_BYTES, |prefix| {
MAX_TEXT_BYTES.saturating_sub(prefix.len())
})
}
/// Return the longest UTF-8-safe prefix of `text` within `max_bytes` and whether it was shortened.
pub fn bounded_embedding_input(text: &str, max_bytes: usize) -> (&str, bool) {
if text.len() <= max_bytes {
return (text, false);
}
let end = text
.char_indices()
.map(|(index, _)| index)
.take_while(|index| *index <= max_bytes)
.last()
.unwrap_or(0);
(&text[..end], true)
}
fn prepared_document_fingerprint(text: &str, model: EmbeddingModel) -> ContentRef {
match model.document_instruction() {
Some(prefix) => VectorRecord::fingerprint_text(&format!("{prefix}{text}")),
None => VectorRecord::fingerprint_text(text),
}
}
impl KhiveRuntime {
fn require_default_embedder(&self) -> RuntimeResult<&str> {
let model_name = self.default_embedder_name();
if model_name.is_empty() {
return Err(RuntimeError::Unconfigured("embedding_model".into()));
}
Ok(model_name)
}
/// Generate an embedding vector for `text` using the configured default model.
///
/// First call lazily loads model weights (cold start cost). Subsequent calls reuse them.
/// Returns `Unconfigured("embedding_model")` if no model is configured.
pub async fn embed(&self, text: &str) -> RuntimeResult<Vec<f32>> {
let model_name = self.require_default_embedder()?;
self.embed_with_model(model_name, text).await
}
/// Generate an embedding vector for `text` using the named model.
///
/// Accepts both built-in lattice model names/aliases and custom provider
/// names registered via [`KhiveRuntime::register_embedder`]. For lattice
/// models the resolved `EmbeddingModel` enum is forwarded to `embed_one`
/// so the service can select the correct model variant. For custom
/// providers, `embed_one` is called with `EmbeddingModel::default()`
/// because custom services are expected to ignore the enum argument (they
/// own a single model implicitly).
///
/// Applies no instruction prefix (generic role). Use
/// [`Self::embed_document_with_model_outcome`] / [`Self::embed_query_with_model`] for
/// instruction-tuned models where the asymmetric prefix matters.
///
/// Returns `UnknownModel` if `model_name` is not in the embedder registry.
pub async fn embed_with_model(&self, model_name: &str, text: &str) -> RuntimeResult<Vec<f32>> {
let model = parse_embedding_model_alias(model_name);
let service = self.embedder(model_name).await?;
let emb_model = model.unwrap_or_default();
// Issued-at-dispatch: count before the provider await so a call that
// was handed to the provider is counted even if this task is aborted
// while parked on the await (drain_embed_join_set cancellation path).
crate::usage::count(crate::usage::UsageUnit::EmbedCalls, 1);
let out = with_embedding_admission(service.embed_one(text, emb_model)).await;
out
}
/// Embed a document/passage for indexing using the named model.
///
/// Applies `EmbeddingService::embed_passage`, which prepends the model's
/// `document_instruction()` prefix when defined (e.g. `"passage: "` for
/// multilingual-e5). For models with no document prefix (MiniLM, BGE) this
/// is identical to [`Self::embed_with_model`].
///
/// Use this for all index/store/backfill paths so that instruction-tuned
/// models produce passage-side vectors.
///
/// **Reindex caveat**: switching from an unprefixed model (or a model with no
/// `document_instruction`) to an instruction-tuned model changes the vector
/// representation. Vectors stored under the old scheme are not comparable to
/// newly prefixed vectors. Operators must trigger a full reindex
/// (`knowledge.index(rebuild_ann=true)` / `kkernel reindex`) after changing
/// the embedding model config.
///
/// Returns `UnknownModel` if `model_name` is not registered.
pub async fn embed_document_with_model_outcome(
&self,
model_name: &str,
text: &str,
) -> RuntimeResult<DocumentEmbeddingOutcome> {
self.embed_document_with_model_outcome_inner(None, model_name, text)
.await
}
pub(crate) async fn embed_document_with_model_outcome_for_token(
&self,
token: &NamespaceToken,
model_name: &str,
text: &str,
) -> RuntimeResult<DocumentEmbeddingOutcome> {
self.embed_document_with_model_outcome_inner(Some(token), model_name, text)
.await
}
async fn embed_document_with_model_outcome_inner(
&self,
token: Option<&NamespaceToken>,
model_name: &str,
text: &str,
) -> RuntimeResult<DocumentEmbeddingOutcome> {
let model = parse_embedding_model_alias(model_name);
let (service, audited_document_preparation) = self
.embedder_with_input_attestation(model_name, token)
.await?;
let emb_model = model.unwrap_or_default();
let source_bytes = text.len();
let (text, truncated) =
bounded_embedding_input(text, document_embedding_budget(model_name));
let embedded_bytes = text.len();
// Issued-at-dispatch: counted before the await — see embed_with_model.
crate::usage::count(crate::usage::UsageUnit::EmbedCalls, 1);
let embeddings =
with_embedding_admission(service.embed_passage(&[text.to_string()], emb_model)).await;
let mut vectors = embeddings?;
if vectors.len() != 1 {
return Err(RuntimeError::Internal(format!(
"embed_passage returned {} vectors for 1 input",
vectors.len()
)));
}
let out = vectors.pop().expect("checked len == 1 above");
Ok(DocumentEmbeddingOutcome {
model_name: model_name.to_owned(),
vector: out,
prepared_text_fingerprint: audited_document_preparation
.then(|| prepared_document_fingerprint(text, emb_model)),
source_bytes,
embedded_bytes,
truncated,
})
}
/// Embed a query string for retrieval using the named model.
///
/// Applies the pre-0.6 query-role behavior. E5 and Qwen models retain
/// their query instructions, while BGE and custom providers receive the
/// original unprefixed query text.
///
/// Use this for all search/recall/suggest query embedding paths so that
/// instruction-tuned models land in the correct side of their retrieval
/// space.
///
/// Returns `UnknownModel` if `model_name` is not registered.
pub async fn embed_query_with_model(
&self,
model_name: &str,
text: &str,
) -> RuntimeResult<Vec<f32>> {
self.embed_query_with_model_inner(None, model_name, text)
.await
}
pub(crate) async fn embed_query_with_model_for_token(
&self,
token: &NamespaceToken,
model_name: &str,
text: &str,
) -> RuntimeResult<Vec<f32>> {
self.embed_query_with_model_inner(Some(token), model_name, text)
.await
}
async fn embed_query_with_model_inner(
&self,
token: Option<&NamespaceToken>,
model_name: &str,
text: &str,
) -> RuntimeResult<Vec<f32>> {
let model = parse_embedding_model_alias(model_name);
let service = match token {
Some(token) => self.embedder_with_token(token, model_name).await?,
None => self.embedder(model_name).await?,
};
let texts = [text.to_string()];
let emb_model = model.unwrap_or_default();
// Issued-at-dispatch: counted before the await — see embed_with_model.
crate::usage::count(crate::usage::UsageUnit::EmbedCalls, 1);
let embeddings = match emb_model {
EmbeddingModel::BgeSmallEnV15
| EmbeddingModel::BgeBaseEnV15
| EmbeddingModel::BgeLargeEnV15 => {
with_embedding_admission(service.embed(&texts, emb_model)).await
}
_ => with_embedding_admission(service.embed_query(&texts, emb_model)).await,
};
let out = embeddings?
.into_iter()
.next()
.ok_or_else(|| RuntimeError::Internal("embed_query returned empty vec".into()))?;
Ok(out)
}
/// Embed a document for indexing using the configured default model.
///
/// Delegates to [`Self::embed_document_outcome`]. Use for entity/note
/// create and reindex paths.
///
/// Returns `Unconfigured("embedding_model")` if no model is configured.
/// Returns an error if the input is bounded; use the outcome method to
/// inspect the vector together with the truncation metadata.
pub async fn embed_document(&self, text: &str) -> RuntimeResult<Vec<f32>> {
let outcome = self.embed_document_outcome(text).await?;
if outcome.truncated {
return Err(RuntimeError::InvalidInput(format!(
"embedding input truncated from {} to {} bytes; use embed_document_outcome to inspect the bounded vector",
outcome.source_bytes, outcome.embedded_bytes
)));
}
Ok(outcome.vector)
}
/// Embed a document with the default model and retain input-bounding metadata.
pub async fn embed_document_outcome(
&self,
text: &str,
) -> RuntimeResult<DocumentEmbeddingOutcome> {
let model_name = self.require_default_embedder()?;
self.embed_document_with_model_outcome(model_name, text)
.await
}
/// Embed a query for retrieval using the configured default model.
///
/// Delegates to [`Self::embed_query_with_model`]. Use for vector search and
/// hybrid search query paths.
///
/// Returns `Unconfigured("embedding_model")` if no model is configured.
pub async fn embed_query(&self, text: &str) -> RuntimeResult<Vec<f32>> {
let model_name = self.require_default_embedder()?;
self.embed_query_with_model(model_name, text).await
}
async fn embed_query_for_token(
&self,
token: &NamespaceToken,
text: &str,
) -> RuntimeResult<Vec<f32>> {
let model_name = self.require_default_embedder()?;
self.embed_query_with_model_for_token(token, model_name, text)
.await
}
/// Generate embeddings for multiple texts in one call using the configured default model.
///
/// Delegates to the cached `EmbeddingService::embed`, so repeated texts within
/// and across calls benefit from the runtime-level LRU cache.
///
/// Returns an empty vec for empty input without hitting the embedding service.
/// Returns `Unconfigured("embedding_model")` if no model is configured.
pub async fn embed_batch(&self, texts: &[String]) -> RuntimeResult<Vec<Vec<f32>>> {
if texts.is_empty() {
return Ok(vec![]);
}
let model_name = self.require_default_embedder()?;
self.embed_batch_with_model(model_name, texts).await
}
/// Generate embeddings for multiple texts using the named model.
///
/// Accepts lattice model names/aliases and custom provider names.
/// Returns `UnknownModel` if `model_name` is not in the embedder registry.
pub async fn embed_batch_with_model(
&self,
model_name: &str,
texts: &[String],
) -> RuntimeResult<Vec<Vec<f32>>> {
if texts.is_empty() {
return Ok(vec![]);
}
let model = parse_embedding_model_alias(model_name);
let service = self.embedder(model_name).await?;
let emb_model = model.unwrap_or_default();
let out = with_embedding_admission(service.embed(texts, emb_model)).await;
crate::usage::count(crate::usage::UsageUnit::EmbedCalls, texts.len() as u64);
out
}
/// Embed a batch of documents for indexing using the named model.
///
/// Applies `EmbeddingService::embed_passage`. Use for all bulk
/// index/backfill/reindex operations to apply the passage-side prefix.
/// A mixed batch is bounded into one ordered owned batch so truncation never
/// fragments one provider batch into sequential singleton inference calls.
///
/// **Reindex caveat**: see [`Self::embed_document_with_model_outcome`] — the same
/// incomparability applies to batch-indexed vectors when switching models.
///
/// Returns `UnknownModel` if `model_name` is not registered.
/// Returns an error when any input is bounded; use the outcomes method to
/// retain the bounded vectors and per-document byte counts.
pub async fn embed_document_batch_with_model(
&self,
model_name: &str,
texts: &[String],
) -> RuntimeResult<Vec<Vec<f32>>> {
if texts.is_empty() {
return Ok(vec![]);
}
let outcomes = self
.embed_document_batch_with_model_outcomes(model_name, texts)
.await?;
let mut report = EmbeddingTruncationReport::default();
for outcome in &outcomes {
report.observe(outcome);
}
if report.any_truncated() {
return Err(RuntimeError::InvalidInput(format!(
"embedding input truncated for {} documents ({} discarded bytes); use embed_document_batch_with_model_outcomes to inspect the bounded vectors",
report.truncated, report.discarded_bytes
)));
}
Ok(outcomes.into_iter().map(|outcome| outcome.vector).collect())
}
pub async fn embed_document_batch_with_model_outcomes(
&self,
model_name: &str,
texts: &[String],
) -> RuntimeResult<Vec<DocumentEmbeddingOutcome>> {
self.embed_document_batch_with_model_outcomes_inner(None, model_name, texts)
.await
}
pub(crate) async fn embed_document_batch_with_model_outcomes_for_token(
&self,
token: &NamespaceToken,
model_name: &str,
texts: &[String],
) -> RuntimeResult<Vec<DocumentEmbeddingOutcome>> {
self.embed_document_batch_with_model_outcomes_inner(Some(token), model_name, texts)
.await
}
async fn embed_document_batch_with_model_outcomes_inner(
&self,
token: Option<&NamespaceToken>,
model_name: &str,
texts: &[String],
) -> RuntimeResult<Vec<DocumentEmbeddingOutcome>> {
if texts.is_empty() {
return Ok(vec![]);
}
let model = parse_embedding_model_alias(model_name);
let (service, audited_document_preparation) = self
.embedder_with_input_attestation(model_name, token)
.await?;
let emb_model = model.unwrap_or_default();
let budget = document_embedding_budget(model_name);
if token.is_some() {
// Atomic preparation records issued work even if its task is cancelled.
crate::usage::count(crate::usage::UsageUnit::EmbedCalls, texts.len() as u64);
}
let out = if texts.iter().all(|text| text.len() <= budget) {
with_embedding_admission(service.embed_passage(texts, emb_model)).await
} else {
let bounded_texts: Vec<String> = texts
.iter()
.map(|text| bounded_embedding_input(text, budget).0.to_owned())
.collect();
with_embedding_admission(service.embed_passage(&bounded_texts, emb_model)).await
};
if token.is_none() {
crate::usage::count(crate::usage::UsageUnit::EmbedCalls, texts.len() as u64);
}
let vectors = out?;
if vectors.len() != texts.len() {
return Err(RuntimeError::Internal(format!(
"embed_passage returned {} vectors for {} inputs",
vectors.len(),
texts.len()
)));
}
Ok(texts
.iter()
.zip(vectors)
.map(|(text, vector)| {
let (bounded, truncated) = bounded_embedding_input(text, budget);
DocumentEmbeddingOutcome {
model_name: model_name.to_owned(),
vector,
prepared_text_fingerprint: audited_document_preparation
.then(|| prepared_document_fingerprint(bounded, emb_model)),
source_bytes: text.len(),
embedded_bytes: bounded.len(),
truncated,
}
})
.collect())
}
/// Embed a batch of documents for indexing using the configured default model.
///
/// Convenience delegate to [`Self::embed_document_batch_with_model`]. Use for
/// bulk knowledge-atom and section indexing paths.
///
/// Returns `Unconfigured("embedding_model")` if no model is configured.
/// Returns an error when any input is bounded; use
/// [`Self::embed_document_batch_outcomes`] to retain the bounded vectors.
pub async fn embed_document_batch(&self, texts: &[String]) -> RuntimeResult<Vec<Vec<f32>>> {
if texts.is_empty() {
return Ok(vec![]);
}
let model_name = self.require_default_embedder()?;
self.embed_document_batch_with_model(model_name, texts)
.await
}
/// Embed documents with the default model and retain actual input-bounding metadata.
pub async fn embed_document_batch_outcomes(
&self,
texts: &[String],
) -> RuntimeResult<Vec<DocumentEmbeddingOutcome>> {
if texts.is_empty() {
return Ok(vec![]);
}
let model_name = self.require_default_embedder()?;
self.embed_document_batch_with_model_outcomes(model_name, texts)
.await
}
/// Embed a batch of queries for retrieval using the named model.
///
/// Applies the same pre-0.6 query-role compatibility behavior as
/// [`Self::embed_query_with_model`].
///
/// Returns `UnknownModel` if `model_name` is not registered.
pub async fn embed_query_batch_with_model(
&self,
model_name: &str,
texts: &[String],
) -> RuntimeResult<Vec<Vec<f32>>> {
if texts.is_empty() {
return Ok(vec![]);
}
let model = parse_embedding_model_alias(model_name);
let service = self.embedder(model_name).await?;
let emb_model = model.unwrap_or_default();
let out = match emb_model {
EmbeddingModel::BgeSmallEnV15
| EmbeddingModel::BgeBaseEnV15
| EmbeddingModel::BgeLargeEnV15 => {
with_embedding_admission(service.embed(texts, emb_model)).await
}
_ => with_embedding_admission(service.embed_query(texts, emb_model)).await,
};
crate::usage::count(crate::usage::UsageUnit::EmbedCalls, texts.len() as u64);
out
}
/// Search vectors using either a caller-provided embedding or query text.
///
/// Existing callers pass `query_embedding: Some(vec)` to avoid re-embedding.
/// Text callers pass `query_embedding: None, query_text: Some(...)` and the
/// runtime embeds internally.
pub async fn vector_search(
&self,
token: &NamespaceToken,
query_embedding: Option<Vec<f32>>,
query_text: Option<&str>,
top_k: u32,
kind: Option<SubstrateKind>,
) -> RuntimeResult<Vec<VectorSearchHit>> {
let embedding = match query_embedding {
Some(vec) => vec,
None => {
let text = query_text.ok_or_else(|| {
RuntimeError::InvalidInput(
"vector search requires query_embedding or query_text".into(),
)
})?;
if text.trim().is_empty() {
return Err(RuntimeError::InvalidInput(
"query_text must not be empty".into(),
));
}
self.embed_query_for_token(token, text).await?
}
};
let ns = token.namespace().as_str().to_owned();
let hits = self
.vectors(token)?
.search(VectorSearchRequest {
query_vectors: vec![embedding],
top_k,
namespace: Some(ns),
kind,
embedding_model: None,
filter: None,
backend_hints: None,
})
.await;
crate::usage::count(crate::usage::UsageUnit::VectorPasses, 1);
hits.map_err(RuntimeError::from)
}
/// The note-search vector leg uses the pack-owned graph when that model
/// has an installed, consumer-protected bridge. Only a missing graph for
/// this consumer takes the existing exact sqlite-vec route.
pub(crate) async fn note_search_vector_search(
&self,
token: &NamespaceToken,
query_embedding: Option<Vec<f32>>,
query_text: &str,
top_k: u32,
) -> RuntimeResult<Vec<VectorSearchHit>> {
let embedding = match query_embedding {
Some(embedding) => embedding,
None => self.embed_query_for_token(token, query_text).await?,
};
let model = self.default_embedder_name();
if !model.is_empty() {
if let Some(provider) = self.note_search_ann_provider()? {
if let Some(hits) = provider.search(token, model, &embedding, top_k).await? {
crate::note_search_ann::record_ann_route();
crate::usage::count(crate::usage::UsageUnit::VectorPasses, 1);
return Ok(hits);
}
}
}
crate::note_search_ann::record_fallback_route();
self.vector_search(
token,
Some(embedding),
None,
top_k,
Some(SubstrateKind::Note),
)
.await
}
/// Hybrid search: text (FTS5) + vector retrieval fused via Reciprocal Rank Fusion.
///
/// - Always performs text search over `query_text`.
/// - If `query_vector` is `Some`, also performs vector search and fuses both lists.
/// - If `None`, returns text-only results — no vector store needed.
/// - If `entity_kind` is `Some`, the alive-set query filters to that kind.
/// The text/vector candidate pools are unfiltered up front; the kind
/// filter applies at the alive-check stage where we already fetch each
/// candidate to confirm it isn't soft-deleted.
/// - `tags_any`: when non-empty, only entities that have at least one of these
/// tags (case-insensitive) survive the alive-set intersection. Applied BEFORE
/// truncation so matches ranked beyond `limit` in the raw fusion are not lost.
/// - `properties_filter`: when `Some`, only entities whose properties are a
/// superset of the given JSON object survive. Applied BEFORE truncation.
///
/// `limit` caps the final returned list; internally pulls `limit * 4` candidates per path.
///
/// # Cross-namespace visibility (entity search — text leg: visible set; vector leg: primary)
///
/// The two legs of entity search scope namespaces differently.
///
/// **Text leg.** Entity full-text search is one shared table (`fts_entities`)
/// with a `namespace` column. The token's whole visible set (`visible_ns`) is
/// forwarded in `TextFilter.namespaces`, which the store applies as a
/// `namespace IN (...)` predicate, so one query returns text hits from every
/// visible namespace.
///
/// **Vector leg.** The vector leg runs only when a query vector is supplied or
/// an embedding model is configured. It is an exact sqlite-vec search
/// (brute-force cosine, as in `knn`); no ANN index is involved. It issues a
/// single request scoped to the primary namespace, because a
/// `VectorSearchRequest` carries one namespace. Searching the visible set
/// would take one request per namespace and a merge of the per-namespace
/// lists; that fanout is not implemented for entities, so entity vector hits
/// come from the primary namespace only.
///
/// The fused candidates are then checked against the entity store with the
/// visible set, so an entity from an extra visible namespace is returned when
/// the text leg matched it, and is not returned on a vector-only match.
///
/// This differs from `memory.recall`, whose vector leg already searches every
/// visible namespace.
///
/// Callers can also read any visible entity directly via `get_entity` /
/// `resolve`.
#[allow(clippy::too_many_arguments)]
pub async fn hybrid_search(
&self,
token: &NamespaceToken,
query_text: &str,
query_vector: Option<Vec<f32>>,
limit: u32,
entity_kind: Option<&str>,
entity_type: Option<&str>,
tags_any: &[String],
properties_filter: Option<&serde_json::Value>,
) -> RuntimeResult<Vec<SearchHit>> {
self.hybrid_search_with_text_mode(
token,
query_text,
query_vector,
limit,
entity_kind,
entity_type,
tags_any,
properties_filter,
TextQueryMode::Plain,
)
.await
}
/// Hybrid search with an explicit lexical mode for the text arm.
#[allow(clippy::too_many_arguments)]
pub async fn hybrid_search_with_text_mode(
&self,
token: &NamespaceToken,
query_text: &str,
query_vector: Option<Vec<f32>>,
limit: u32,
entity_kind: Option<&str>,
entity_type: Option<&str>,
tags_any: &[String],
properties_filter: Option<&serde_json::Value>,
text_mode: TextQueryMode,
) -> RuntimeResult<Vec<SearchHit>> {
let (hits, _vector_error) = self
.hybrid_search_inner(
token,
query_text,
query_vector,
limit,
entity_kind,
entity_type,
tags_any,
properties_filter,
text_mode,
None,
false,
None,
)
.await?;
Ok(hits)
}
/// Hybrid search over several entity kinds that issues the vector query once.
///
/// Returns one list per entry of `entity_kinds`, in that order. Each list is what
/// [`Self::hybrid_search`] returns for that kind with the same `limit`: the text stage
/// is filtered to the kind and keeps its own `limit * 4` budget, and fusion, the kind
/// filter and the cut to `limit` run per kind. Only the vector stage is shared, because
/// it takes no entity kind: the single query is the one every per-kind search would have
/// issued. When `query_vector` is `None` and an embedding model is configured, the query
/// text is embedded once instead of once per kind. No entity-type, tag or property
/// filter is set. An empty `entity_kinds` returns no lists and runs no query.
pub async fn hybrid_search_each_kind(
&self,
token: &NamespaceToken,
query_text: &str,
query_vector: Option<Vec<f32>>,
limit: u32,
entity_kinds: &[&str],
) -> RuntimeResult<Vec<Vec<SearchHit>>> {
if entity_kinds.is_empty() {
return Ok(Vec::new());
}
let candidates = limit.saturating_mul(CANDIDATE_MULTIPLIER).max(limit);
let (vector_hits, _vector_error) = self
.hybrid_vector_stage(token, query_text, query_vector, candidates, None, false)
.await?;
let mut per_kind = Vec::with_capacity(entity_kinds.len());
for &kind in entity_kinds {
let (hits, _vector_error) = self
.hybrid_search_inner(
token,
query_text,
None,
limit,
Some(kind),
None,
&[],
None,
TextQueryMode::Plain,
None,
false,
Some(vector_hits.clone()),
)
.await?;
per_kind.push(hits);
}
Ok(per_kind)
}
/// `vector_similarity_floor` is a cosine-similarity value in `[-1.0,
/// 1.0]`, matching both the canonical vector-store score contract and the
/// scale documented on the `resolve` verb and on
/// `SEARCH_VECTOR_SIMILARITY_FLOOR`.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn hybrid_search_with_vector_similarity_floor(
&self,
token: &NamespaceToken,
query_text: &str,
query_vector: Option<Vec<f32>>,
limit: u32,
entity_kind: Option<&str>,
entity_type: Option<&str>,
tags_any: &[String],
properties_filter: Option<&serde_json::Value>,
vector_similarity_floor: f64,
) -> RuntimeResult<Vec<SearchHit>> {
let (hits, _vector_error) = self
.hybrid_search_inner(
token,
query_text,
query_vector,
limit,
entity_kind,
entity_type,
tags_any,
properties_filter,
TextQueryMode::Plain,
Some(vector_similarity_floor),
false,
None,
)
.await?;
Ok(hits)
}
/// Coordinator fan-out variant of [`Self::hybrid_search`]: the text arm
/// still fails loud (propagated through `hybrid_search_inner`'s
/// `tolerate_vector_error=false` semantics for that leg), but a
/// vector-arm failure after a successful text leg is captured instead of
/// discarding the text hits. Reserved for
/// `SubstrateCoordinator::fan_out_search_with_visibility` — every other
/// caller keeps the fail-loud [`Self::hybrid_search`] contract, so a
/// single backend's vector-store outage does not misreport that
/// backend's text arm as failed too.
#[allow(clippy::too_many_arguments)]
pub async fn hybrid_search_outcome(
&self,
token: &NamespaceToken,
query_text: &str,
limit: u32,
entity_kind: Option<&str>,
entity_type: Option<&str>,
tags_any: &[String],
properties_filter: Option<&serde_json::Value>,
) -> RuntimeResult<HybridSearchOutcome> {
self.hybrid_search_outcome_with_text_mode(
token,
query_text,
limit,
entity_kind,
entity_type,
tags_any,
properties_filter,
TextQueryMode::Plain,
)
.await
}
/// Coordinator variant with an explicit lexical mode for the text arm.
#[allow(clippy::too_many_arguments)]
pub async fn hybrid_search_outcome_with_text_mode(
&self,
token: &NamespaceToken,
query_text: &str,
limit: u32,
entity_kind: Option<&str>,
entity_type: Option<&str>,
tags_any: &[String],
properties_filter: Option<&serde_json::Value>,
text_mode: TextQueryMode,
) -> RuntimeResult<HybridSearchOutcome> {
let (hits, vector_error) = self
.hybrid_search_inner(
token,
query_text,
None,
limit,
entity_kind,
entity_type,
tags_any,
properties_filter,
text_mode,
None,
true,
None,
)
.await?;
Ok(HybridSearchOutcome { hits, vector_error })
}
/// `vector_pool`, when `Some`, supplies the vector stage's hits and the stage does not
/// run; `None` runs it. The stage takes no entity kind, so one pool serves every kind.
#[allow(clippy::too_many_arguments)]
async fn hybrid_search_inner(
&self,
token: &NamespaceToken,
query_text: &str,
query_vector: Option<Vec<f32>>,
limit: u32,
entity_kind: Option<&str>,
entity_type: Option<&str>,
tags_any: &[String],
properties_filter: Option<&serde_json::Value>,
text_mode: TextQueryMode,
vector_similarity_floor: Option<f64>,
tolerate_vector_error: bool,
vector_pool: Option<Vec<VectorSearchHit>>,
) -> RuntimeResult<(Vec<SearchHit>, Option<String>)> {
let candidates = limit.saturating_mul(CANDIDATE_MULTIPLIER).max(limit);
let visible_ns: Vec<String> = token
.visible_namespaces()
.iter()
.map(|ns| ns.as_str().to_owned())
.collect();
// sanitize_fts5_query strips known-unsafe FTS5 metacharacters up front, but if
// the lexical leg still errors at runtime on residual punctuation the sanitizer
// doesn't strip, this fails loud instead of degrading to vector-only fusion.
let text_store = self.text(token)?;
let text_fut = text_store.search(TextSearchRequest {
query: query_text.to_string(),
mode: text_mode,
filter: Some(TextFilter {
namespaces: visible_ns.clone(),
// Push the entity-kind filter into the FTS query. Without it the
// text arm returns the top `candidates` rows across EVERY entity
// kind in the namespace and the kind is applied only afterwards,
// so when one kind dominates the lexical ranking a search for a
// rarer kind gets back fewer rows than exist, or none. The
// `EntityFilter.kinds` check below stays as the backstop.
record_kinds: entity_kind
.map(|kind| vec![kind.to_string()])
.unwrap_or_default(),
..TextFilter::default()
}),
top_k: candidates,
snippet_chars: 200,
});
let text_fut = crate::stage_seam::text_stage(text_fut);
// The stages read nothing from each other, so they run together; a text error wins.
let vector_fut = async {
match vector_pool {
Some(pool) => Ok((pool, None)),
None => {
self.hybrid_vector_stage(
token,
query_text,
query_vector,
candidates,
vector_similarity_floor,
tolerate_vector_error,
)
.await
}
}
};
let (text_search_result, vector_result) = tokio::join!(text_fut, vector_fut);
// FtsPasses is counted inside the store's `search()` (khive-db
// stores/text.rs), only once a real FTS5 statement is prepared —
// an empty/fully-sanitized query short-circuits there before any
// statement exists and must not count.
let text_hits = crate::error::fts_text_leg_or_err(
text_search_result.map_err(RuntimeError::from),
"hybrid_search",
query_text,
)?;
let (vector_hits, vector_error) = vector_result?;
// Each arm fetched `candidates` independently, so their union can contain
// twice that many distinct IDs. Keep the complete fetched pool through
// ranking and the alive/kind/tag/property filters below; reusing one arm's
// cap here lets stale or filtered hits hide live candidates from the other.
let fusion_limit = text_hits.len().saturating_add(vector_hits.len());
let mut fused = rrf_fuse(text_hits, vector_hits, fusion_limit, query_text);
// tags_any has a SQL column and is pushed into query_entities; properties
// filtering has no SQL column and is applied in Rust below on the fetched records.
if !fused.is_empty() {
let candidate_ids: Vec<Uuid> = fused.iter().map(|h| h.entity_id).collect();
let alive_page = self
.entities(token)?
.query_entities(
token.namespace().as_str(),
EntityFilter {
ids: candidate_ids,
kinds: entity_kind.map(|k| vec![k.to_string()]).unwrap_or_default(),
entity_types: entity_type.map(|t| vec![t.to_string()]).unwrap_or_default(),
namespaces: visible_ns,
tags_any: tags_any.to_vec(),
..EntityFilter::default()
},
PageRequest {
offset: 0,
limit: u32::try_from(fused.len()).unwrap_or(u32::MAX),
},
)
.await?;
let mut entity_meta: HashMap<Uuid, (String, Option<String>)> = HashMap::new();
let mut alive: HashSet<Uuid> = HashSet::new();
for e in alive_page.items {
// Drop non-matching candidates here, before the alive set is built,
// so they're excluded ahead of truncation.
if let Some(pf) = properties_filter {
if !properties_match(e.properties.as_ref(), pf) {
continue;
}
}
alive.insert(e.id);
entity_meta.insert(e.id, (e.name, e.description));
}
fused.retain(|h| alive.contains(&h.entity_id));
// Enrich vector-only hits (title/snippet == None) from entity record.
for hit in &mut fused {
if let Some((name, description)) = entity_meta.get(&hit.entity_id) {
if hit.title.is_none() {
hit.title = Some(name.clone());
}
if hit.snippet.is_none() {
hit.snippet = description.clone();
}
}
}
}
fused.truncate(limit as usize);
Ok((fused, vector_error))
}
/// The vector stage of hybrid entity search: one KNN query over the entity vectors of
/// the primary namespace. It does not depend on an entity kind, which only the text
/// stage and the filter after fusion apply.
async fn hybrid_vector_stage(
&self,
token: &NamespaceToken,
query_text: &str,
query_vector: Option<Vec<f32>>,
candidates: u32,
vector_similarity_floor: Option<f64>,
tolerate_vector_error: bool,
) -> RuntimeResult<(Vec<VectorSearchHit>, Option<String>)> {
let mut vector_error: Option<String> = None;
let mut vector_hits = if query_vector.is_some() || self.config().embedding_model.is_some() {
match self
.vector_search(
token,
query_vector,
Some(query_text),
candidates,
Some(SubstrateKind::Entity),
)
.await
{
Ok(hits) => hits,
Err(e) if tolerate_vector_error => {
vector_error = Some(e.to_string());
Vec::new()
}
Err(e) => return Err(e),
}
} else {
Vec::new()
};
if let Some(cosine_floor) = vector_similarity_floor {
// Vector store scores use canonical cosine similarity (`1 - distance`),
// so the caller's raw-cosine floor is already on the comparison scale.
let score_floor = DeterministicScore::from_f64(cosine_floor);
vector_hits.retain(|hit| hit.score >= score_floor);
}
Ok((vector_hits, vector_error))
}
/// Exact KNN over the full namespace's vector store.
///
/// sqlite-vec uses brute-force cosine — results are exact, not approximate.
/// Cost is O(N · D) per query. For small-to-medium namespaces (~hundreds of
/// thousands of vectors) this is well within latency budgets.
pub async fn knn(
&self,
token: &NamespaceToken,
query_vector: Vec<f32>,
top_k: u32,
) -> RuntimeResult<Vec<VectorSearchHit>> {
let ns = token.namespace().as_str().to_owned();
Ok(self
.vectors(token)?
.search(VectorSearchRequest {
query_vectors: vec![query_vector],
top_k,
namespace: Some(ns),
kind: Some(SubstrateKind::Entity),
embedding_model: None,
filter: None,
backend_hints: None,
})
.await?)
}
/// Exact KNN restricted to a candidate set.
///
/// Useful for reranking the top-N results from `hybrid_search` (or any other
/// retrieval path) with exact cosine similarity against a query vector.
/// Returns hits sorted by similarity (highest first), truncated to `top_k`.
pub async fn rerank(
&self,
token: &NamespaceToken,
query_vector: &[f32],
candidate_ids: &[Uuid],
top_k: u32,
) -> RuntimeResult<Vec<VectorSearchHit>> {
let candidate_set: HashSet<Uuid> = candidate_ids.iter().copied().collect();
let ns = token.namespace().as_str().to_owned();
let all_hits = self
.vectors(token)?
.search(VectorSearchRequest {
query_vectors: vec![query_vector.to_vec()],
top_k: candidate_ids.len() as u32,
namespace: Some(ns),
kind: Some(SubstrateKind::Entity),
embedding_model: None,
filter: None,
backend_hints: None,
})
.await?;
let mut hits: Vec<VectorSearchHit> = all_hits
.into_iter()
.filter(|h| candidate_set.contains(&h.subject_id))
.collect();
hits.sort_by_key(|hit| std::cmp::Reverse(hit.score));
hits.truncate(top_k as usize);
Ok(hits)
}
async fn embed_backfill_page(
&self,
token: &NamespaceToken,
model_name: &str,
inputs: &[(Uuid, String)],
) -> Vec<Option<DocumentEmbeddingOutcome>> {
let texts: Vec<String> = inputs.iter().map(|(_, text)| text.clone()).collect();
match self
.embed_document_batch_with_model_outcomes_for_token(token, model_name, &texts)
.await
{
Ok(outcomes) => outcomes.into_iter().map(Some).collect(),
Err(error) => {
tracing::warn!(
model = %model_name,
error = %error,
"backfill_missing_embeddings: batch embed failed; retrying records individually"
);
let mut outcomes = Vec::with_capacity(inputs.len());
for (id, text) in inputs {
match self
.embed_document_with_model_outcome_for_token(token, model_name, text)
.await
{
Ok(outcome) => outcomes.push(Some(outcome)),
Err(error) => {
tracing::warn!(
id = %id,
model = %model_name,
error = %error,
"backfill_missing_embeddings: record embed failed"
);
outcomes.push(None);
}
}
}
outcomes
}
}
}
/// Backfill vector and FTS index entries for entities and notes that are missing them.
///
/// Intended to run once at startup as a background task (warm-up sequence steps 2–4).
/// Queries the SQL substrate for entity bodies and note contents that have no
/// corresponding entry in an eligible embedding model's vector store, then
/// embeds and inserts them. FTS entries missing for notes are also repopulated.
///
/// The operation is best-effort: individual embed/insert failures are logged and
/// skipped rather than aborting the whole backfill. If no embedding models are
/// registered, returns immediately with 0.
///
/// Returns the total number of records backfilled across all models.
pub async fn backfill_missing_embeddings(&self, token: &NamespaceToken) -> RuntimeResult<u64> {
use khive_storage::types::{SqlRow, SqlStatement, SqlValue};
let model_names = self.registered_embedding_model_names();
if model_names.is_empty() {
tracing::debug!(
"backfill_missing_embeddings: no embedding models registered, skipping"
);
return Ok(0);
}
let ns = token.namespace().as_str().to_string();
let mut total_backfilled = 0u64;
for model_name in &model_names {
let mut model_truncation = EmbeddingTruncationReport::default();
match self.vectors_for_model(token, model_name) {
Ok(_) => {}
Err(error) => {
tracing::warn!(model = %model_name, error = %error,
"backfill_missing_embeddings: vector store unavailable");
continue;
}
};
// Must match vec_model_key's naming logic.
let vec_table = format!("vec_{}", sanitize_key(model_name));
// --- Entities: embed the canonical body where no vector exists ---
// Keyset pagination advances past failed or ineligible records too;
// they must not keep the first page full forever.
const PAGE_SIZE: usize = EMBEDDING_BATCH_PAGE_SIZE;
let mut entity_total = 0usize;
let mut entity_cursor = String::new();
loop {
let entity_sql = SqlStatement {
sql: format!(
"SELECT id FROM entities \
WHERE namespace = ?1 AND deleted_at IS NULL AND id > ?3 \
AND id NOT IN (\
SELECT subject_id FROM {vec_table} \
WHERE namespace = ?1 AND embedding_model = ?2 \
) ORDER BY id LIMIT {PAGE_SIZE}"
),
params: vec![
SqlValue::Text(ns.clone()),
SqlValue::Text(model_name.clone()),
SqlValue::Text(entity_cursor.clone()),
],
label: Some("backfill_entities".into()),
};
let entity_rows: Vec<SqlRow> = {
let sql = self.sql();
let reader_result = sql.reader().await;
#[cfg(any(test, feature = "fault-injection"))]
let reader_result = if BACKFILL_READER_FAIL.with(|c| c.get()) {
BACKFILL_READER_FAIL.with(|c| c.set(false));
Err(khive_storage::StorageError::Pool {
operation: "reader".into(),
message: "injected failure".into(),
})
} else {
reader_result
};
let mut reader = reader_result.map_err(RuntimeError::Storage)?;
reader
.query_all(entity_sql)
.await
.map_err(RuntimeError::Storage)?
};
let batch_len = entity_rows.len();
entity_total += batch_len;
if batch_len == 0 {
break;
}
entity_cursor = match entity_rows.last().and_then(|row| row.columns.first()) {
Some(column) => match &column.value {
SqlValue::Text(id) => id.clone(),
_ => {
return Err(RuntimeError::Internal(
"backfill entity ID is not text".into(),
))
}
},
None => {
return Err(RuntimeError::Internal(
"backfill entity page is empty".into(),
))
}
};
let entity_store = self.entities(token)?;
let mut entities = Vec::with_capacity(batch_len);
let mut inputs = Vec::with_capacity(batch_len);
for row in &entity_rows {
let Some(SqlValue::Text(id)) = row.columns.first().map(|column| &column.value)
else {
continue;
};
let Ok(id) = id.parse::<Uuid>() else { continue };
let Some(entity) = entity_store.get_entity(id).await? else {
continue;
};
if entity.namespace != ns || entity.deleted_at.is_some() {
continue;
}
let text = crate::curation::entity_embedding_text(&entity);
if text.trim().is_empty() {
continue;
}
inputs.push((id, text));
entities.push(entity);
}
let outcomes = self.embed_backfill_page(token, model_name, &inputs).await;
for (entity, outcome) in entities.into_iter().zip(outcomes) {
let Some(outcome) = outcome else { continue };
model_truncation.observe(&outcome);
match self
.publish_entity_vector_revision(token, &entity, model_name, &outcome.vector)
.await
{
Ok(true) => total_backfilled += 1,
Ok(false) => {}
Err(error) => tracing::warn!(
id = %entity.id, model = %model_name, error = %error,
"backfill_missing_embeddings: entity vector insert failed"
),
}
}
if batch_len < PAGE_SIZE {
break;
}
}
// --- Notes: embed content where no vector entry exists ---
let text_store = self.text_for_notes(token).ok();
let note_store = self.notes(token).ok();
let mut note_total = 0usize;
// Excluded kinds remain absent from this model's vector table.
// Advancing by id prevents a full page of them from repeating.
let mut note_cursor = String::new();
loop {
// Only the id is selected here; the full Note is fetched below so
// note_fts_document gets all fields and stays parity-correct.
let note_sql = SqlStatement {
sql: format!(
"SELECT id FROM notes \
WHERE namespace = ?1 AND deleted_at IS NULL AND id > ?3 \
AND id NOT IN (\
SELECT subject_id FROM {vec_table} \
WHERE namespace = ?1 AND embedding_model = ?2 \
) ORDER BY id LIMIT {PAGE_SIZE}"
),
params: vec![
SqlValue::Text(ns.clone()),
SqlValue::Text(model_name.clone()),
SqlValue::Text(note_cursor.clone()),
],
label: Some("backfill_notes".into()),
};
let note_rows: Vec<SqlRow> = {
let sql = self.sql();
let reader_result = sql.reader().await;
#[cfg(any(test, feature = "fault-injection"))]
let reader_result = if BACKFILL_READER_FAIL.with(|c| c.get()) {
BACKFILL_READER_FAIL.with(|c| c.set(false));
Err(khive_storage::StorageError::Pool {
operation: "reader".into(),
message: "injected failure".into(),
})
} else {
reader_result
};
let mut reader = reader_result.map_err(RuntimeError::Storage)?;
reader
.query_all(note_sql)
.await
.map_err(RuntimeError::Storage)?
};
let batch_len = note_rows.len();
note_total += batch_len;
if batch_len == 0 {
break;
}
note_cursor = match note_rows.last().and_then(|row| row.columns.first()) {
Some(column) => match &column.value {
SqlValue::Text(id) => id.clone(),
_ => {
return Err(RuntimeError::Internal(
"backfill note ID is not text".into(),
))
}
},
None => {
return Err(RuntimeError::Internal("backfill note page is empty".into()))
}
};
let mut notes_for_batch = Vec::with_capacity(batch_len);
let mut inputs = Vec::with_capacity(batch_len);
for row in ¬e_rows {
let id_str = row.columns.first().and_then(|c| {
if let SqlValue::Text(s) = &c.value {
Some(s.clone())
} else {
None
}
});
let Some(id_str) = id_str else {
continue;
};
let Ok(id) = id_str.parse::<Uuid>() else {
continue;
};
let note = match ¬e_store {
Some(store) => match store.get_note(id).await {
Ok(Some(n)) => n,
_ => continue,
},
None => continue,
};
if note.content.trim().is_empty() {
continue;
}
// Repopulate FTS entry using the shared constructor (first model only
// to avoid N identical overwrites per note).
if model_names.first().map(|n| n.as_str()) == Some(model_name.as_str()) {
if let Some(ref ts) = text_store {
if let Err(e) = ts.upsert_document(note_fts_document(¬e)).await {
tracing::warn!(id = %id, error = %e,
"backfill_missing_embeddings: note FTS upsert failed");
}
}
}
if !self
.embedding_models_for_note_kind(¬e.kind)
.contains(model_name)
{
continue;
}
inputs.push((id, note.content.clone()));
notes_for_batch.push(note);
}
let outcomes = self.embed_backfill_page(token, model_name, &inputs).await;
for (note, outcome) in notes_for_batch.into_iter().zip(outcomes) {
let Some(outcome) = outcome else { continue };
model_truncation.observe(&outcome);
match self
.publish_note_vector_revision(token, ¬e, model_name, &outcome.vector)
.await
{
Ok(true) => total_backfilled += 1,
Ok(false) => {}
Err(error) => tracing::warn!(
id = %note.id, model = %model_name, error = %error,
"backfill_missing_embeddings: note vector insert failed"
),
}
}
if batch_len < PAGE_SIZE {
break;
}
}
tracing::info!(
model = %model_name,
namespace = %ns,
entities = entity_total,
notes = note_total,
truncated = model_truncation.truncated,
discarded_bytes = model_truncation.discarded_bytes,
"backfill_missing_embeddings: model pass complete"
);
}
tracing::info!(
namespace = %ns,
total_backfilled = total_backfilled,
"backfill_missing_embeddings: finished"
);
Ok(total_backfilled)
}
/// Sweep orphaned vector entries for all registered embedding models.
///
/// A vector entry is orphaned when its `subject_id` no longer exists as a
/// live row in the entity, note, or knowledge-atom tables (i.e. either the
/// row is absent or has `deleted_at IS NOT NULL`). Orphaned entries
/// accumulate after hard-deletes because the vector store and SQL
/// substrate are decoupled.
///
/// Iterates over every registered embedding model and calls
/// [`khive_storage::VectorStore::orphan_sweep`] for the token's namespace. Models whose
/// backend returns [`khive_storage::StorageError::Unsupported`] are skipped without error —
/// this preserves forward-compat when a newly registered model does not yet
/// implement sweep.
///
/// Returns the total number of vector rows deleted across all models.
pub async fn sweep_orphan_vectors(
&self,
token: &NamespaceToken,
max_delete_per_model: u32,
dry_run: bool,
) -> RuntimeResult<u64> {
use khive_storage::types::OrphanSweepConfig;
use khive_storage::StorageError;
let model_names = self.registered_embedding_model_names();
if model_names.is_empty() {
tracing::debug!("sweep_orphan_vectors: no embedding models registered, skipping");
return Ok(0);
}
let ns = token.namespace().as_str().to_string();
let mut total_deleted = 0u64;
for model_name in &model_names {
let store = match self.vectors_for_model(token, model_name) {
Ok(s) => s,
Err(e) => {
tracing::warn!(
model = %model_name,
error = %e,
"sweep_orphan_vectors: failed to get vector store, skipping model"
);
continue;
}
};
let caps = store.capabilities();
if !caps.supports_orphan_sweep {
tracing::debug!(
model = %model_name,
"sweep_orphan_vectors: backend does not support orphan sweep, skipping"
);
continue;
}
let config = OrphanSweepConfig {
subject_id_allowlist: None,
namespaces: vec![ns.clone()],
substrate_kinds: vec![],
max_delete: max_delete_per_model,
dry_run,
};
match store.orphan_sweep(&config).await {
Ok(result) => {
tracing::info!(
model = %model_name,
namespace = %ns,
scanned = result.scanned,
deleted = result.deleted,
would_delete = result.would_delete,
dry_run = dry_run,
"sweep_orphan_vectors: sweep complete"
);
total_deleted += result.deleted;
}
Err(StorageError::Unsupported { .. }) => {
tracing::debug!(
model = %model_name,
"sweep_orphan_vectors: backend returned Unsupported, skipping"
);
}
Err(e) => {
tracing::warn!(
model = %model_name,
error = %e,
"sweep_orphan_vectors: sweep failed, continuing with other models"
);
}
}
}
tracing::info!(
namespace = %ns,
total_deleted = total_deleted,
dry_run = dry_run,
"sweep_orphan_vectors: finished"
);
Ok(total_deleted)
}
}
/// Returns `true` when `properties` is a superset of all key-value pairs in `filter`.
///
/// Mirrors the semantics of `khive_pack_kg::handlers::common::props_match` so that the
/// storage-leg predicate is identical to the handler-side post-filter.
pub(crate) fn properties_match(
properties: Option<&serde_json::Value>,
filter: &serde_json::Value,
) -> bool {
let required = match filter.as_object() {
Some(obj) if !obj.is_empty() => obj,
_ => return true,
};
let actual = match properties.and_then(serde_json::Value::as_object) {
Some(obj) => obj,
None => return false,
};
required
.iter()
.all(|(k, v)| actual.get(k).is_some_and(|av| av == v))
}
/// Score bonus applied when an entity's title is an exact case-insensitive match for
/// the query. Dominates RRF scores (~0.09–0.18 range with k=10) so that an exact
/// name match always ranks above any partial or semantic match.
const EXACT_MATCH_BOOST: f64 = 0.5;
/// Fuse text + vector hits with Reciprocal Rank Fusion (k=10).
///
/// Scoring and the merge of each id's labels come from the shared labelled fusion in
/// `khive-retrieval`. Entity search keeps its own k=10 and exact-match boosting here.
/// Hits in both lists get RRF scores summed. If `query_text` exactly matches
/// (case-insensitive) the title a fused hit carries from the text hits, a bonus of
/// `EXACT_MATCH_BOOST` is added to ensure exact-name matches dominate.
/// Sort by fused score, take top-`limit`.
fn rrf_fuse(
text_hits: Vec<TextSearchHit>,
vector_hits: Vec<VectorSearchHit>,
limit: usize,
query_text: &str,
) -> Vec<SearchHit> {
let mut text_arm = Vec::new();
for (rank, hit) in text_hits.into_iter().enumerate() {
let label = HitLabel {
rank,
signals: SearchSignals {
vector_similarity: None,
keyword_score: Some(hit.score),
},
source: SearchSource::Text,
title: hit.title,
snippet: hit.snippet,
};
text_arm.push((hit.subject_id, label));
}
let mut vector_arm = Vec::new();
for (rank, hit) in vector_hits.into_iter().enumerate() {
let label = HitLabel {
rank,
signals: SearchSignals {
vector_similarity: Some(hit.score),
keyword_score: None,
},
source: SearchSource::Vector,
title: None,
snippet: None,
};
vector_arm.push((hit.subject_id, label));
}
let fused = fuse_labelled(
vec![text_arm, vector_arm],
RRF_K,
combine_leg_first_appearance,
);
// The bonus joins the full fused order, so it decides the sort and the cut below.
let query_lower = query_text.to_lowercase();
let boost = DeterministicScore::from_f64(EXACT_MATCH_BOOST);
let mut hits = Vec::with_capacity(fused.len());
for (entity_id, score, label) in fused {
let title_lower = label.title.as_deref().map(str::to_lowercase);
let exact = title_lower.as_deref() == Some(query_lower.as_str());
let score = if exact { score + boost } else { score };
hits.push(SearchHit {
entity_id,
score,
rank_score_kind: RankScoreKind::Rrf,
signals: label.signals,
source: label.source,
title: label.title,
snippet: label.snippet,
});
}
hits.sort_by(|a, b| b.score.cmp(&a.score).then(a.entity_id.cmp(&b.entity_id)));
hits.truncate(limit);
hits
}
#[cfg(test)]
mod rrf_fuse_label_tests;
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use crate::runtime::{KhiveRuntime, NamespaceToken, RuntimeConfig};
use khive_score::rrf_score;
use khive_storage::types::{TextSearchHit, VectorSearchHit};
use khive_types::namespace::Namespace;
use lattice_embed::{EmbedError, EmbeddingModel};
/// An `EmbeddingService` that always fails — used to drive a real
/// vector-arm failure (as opposed to an `Unconfigured` short-circuit)
/// through `embed_query_for_token` without loading actual model weights.
struct FailingEmbeddingService;
#[async_trait::async_trait]
impl EmbeddingService for FailingEmbeddingService {
async fn embed(
&self,
_texts: &[String],
_model: EmbeddingModel,
) -> Result<Vec<Vec<f32>>, EmbedError> {
Err(EmbedError::ModelInitialization(
"injected vector-arm failure".to_string(),
))
}
fn supports_model(&self, _model: EmbeddingModel) -> bool {
true
}
fn name(&self) -> &'static str {
"hybrid-search-test-failing-embedding"
}
}
struct FailingEmbedderProvider {
name: String,
dimensions: usize,
}
#[async_trait::async_trait]
impl EmbedderProvider for FailingEmbedderProvider {
fn name(&self) -> &str {
&self.name
}
fn dimensions(&self) -> usize {
self.dimensions
}
async fn build(&self) -> RuntimeResult<Arc<dyn EmbeddingService>> {
Ok(Arc::new(FailingEmbeddingService))
}
}
/// Swap the runtime's registered embedder for the always-failing one,
/// re-keyed under the same model name so `hybrid_search`'s vector leg
/// (which resolves the embedder by the runtime's configured model name)
/// picks it up. `EmbedderRegistry::register` overwrites by name and
/// resets the provider's build cache, so this takes effect on the next
/// embed call without needing a fresh runtime.
fn break_vector_arm(runtime: &KhiveRuntime) {
let model = EmbeddingModel::AllMiniLmL6V2;
runtime.register_embedder(FailingEmbedderProvider {
name: model.to_string(),
dimensions: model.dimensions(),
});
}
/// An `EmbeddingService` that always succeeds with a fixed vector — used
/// to exercise a genuinely healthy vector leg without loading real model
/// weights.
struct ConstantEmbeddingService {
dimensions: usize,
}
#[async_trait::async_trait]
impl EmbeddingService for ConstantEmbeddingService {
async fn embed(
&self,
texts: &[String],
_model: EmbeddingModel,
) -> Result<Vec<Vec<f32>>, EmbedError> {
Ok(texts.iter().map(|_| vec![1.0; self.dimensions]).collect())
}
fn supports_model(&self, _model: EmbeddingModel) -> bool {
true
}
fn name(&self) -> &'static str {
"hybrid-search-test-constant-embedding"
}
}
struct ConstantEmbedderProvider {
name: String,
dimensions: usize,
}
#[async_trait::async_trait]
impl EmbedderProvider for ConstantEmbedderProvider {
fn name(&self) -> &str {
&self.name
}
fn dimensions(&self) -> usize {
self.dimensions
}
async fn build(&self) -> RuntimeResult<Arc<dyn EmbeddingService>> {
Ok(Arc::new(ConstantEmbeddingService {
dimensions: self.dimensions,
}))
}
}
/// A runtime configured with a healthy (constant, non-failing) embedder —
/// the vector leg genuinely runs and succeeds.
fn runtime_with_constant_embeddings() -> KhiveRuntime {
let model = EmbeddingModel::AllMiniLmL6V2;
let runtime = KhiveRuntime::new(RuntimeConfig {
db_path: None,
embedding_model: Some(model),
packs: vec!["kg".to_string()],
..RuntimeConfig::no_embeddings()
})
.expect("in-memory runtime");
runtime.register_embedder(ConstantEmbedderProvider {
name: model.to_string(),
dimensions: model.dimensions(),
});
runtime
}
#[test]
fn bounded_embedding_input_reserves_prefix_and_preserves_utf8() {
assert_eq!(
document_embedding_budget("multilingual-e5-base"),
MAX_TEXT_BYTES - "passage: ".len()
);
let input = format!("{}\u{1f980}tail", "a".repeat(MAX_TEXT_BYTES - 1));
let (bounded, truncated) = bounded_embedding_input(&input, MAX_TEXT_BYTES);
assert!(truncated);
assert_eq!(bounded.len(), MAX_TEXT_BYTES - 1);
assert!(bounded.is_char_boundary(bounded.len()));
assert!(!bounded.contains('\u{1f980}'));
}
#[test]
fn bounded_embedding_input_leaves_normal_text_unchanged() {
let input = "normal byte-identical embedding input";
let (bounded, truncated) = bounded_embedding_input(input, MAX_TEXT_BYTES);
assert!(!truncated);
assert_eq!(bounded, input);
assert_eq!(bounded.as_ptr(), input.as_ptr());
}
fn text_hit(id: Uuid, rank: u32, title: &str) -> TextSearchHit {
TextSearchHit {
subject_id: id,
score: DeterministicScore::from_f64(1.0),
rank,
title: Some(title.to_string()),
snippet: Some("...".to_string()),
}
}
fn vector_hit(id: Uuid, rank: u32) -> VectorSearchHit {
VectorSearchHit {
subject_id: id,
score: DeterministicScore::from_f64(0.9),
rank,
}
}
#[test]
fn rrf_evidence_keeps_absence_distinct_from_measured_zero() {
let text_id = Uuid::from_u128(1);
let vector_id = Uuid::from_u128(2);
let both_id = Uuid::from_u128(3);
let quarter = DeterministicScore::from_raw(1_i64 << 30);
let half = DeterministicScore::from_raw(1_i64 << 31);
let text = vec![
TextSearchHit {
score: DeterministicScore::ZERO,
..text_hit(text_id, 1, "text")
},
TextSearchHit {
score: quarter,
..text_hit(both_id, 2, "both")
},
text_hit(both_id, 3, "duplicate"),
];
let vector = vec![
VectorSearchHit {
score: DeterministicScore::ZERO,
..vector_hit(vector_id, 1)
},
VectorSearchHit {
score: half,
..vector_hit(both_id, 2)
},
vector_hit(both_id, 3),
];
let hits = rrf_fuse(text, vector, 10, "unmatched");
assert_eq!(hits.len(), 3);
for (id, source, signals) in [
(
text_id,
SearchSource::Text,
SearchSignals {
vector_similarity: None,
keyword_score: Some(DeterministicScore::ZERO),
},
),
(
vector_id,
SearchSource::Vector,
SearchSignals {
vector_similarity: Some(DeterministicScore::ZERO),
keyword_score: None,
},
),
(
both_id,
SearchSource::Both,
SearchSignals {
vector_similarity: Some(half),
keyword_score: Some(quarter),
},
),
] {
let hit = hits.iter().find(|hit| hit.entity_id == id).unwrap();
assert_eq!(hit.rank_score_kind, RankScoreKind::Rrf);
assert_eq!(hit.source, source);
assert_eq!(hit.signals, signals);
}
}
#[test]
fn rrf_evidence_golden_preserves_true_ties_across_permutations() {
let a = Uuid::from_u128(1);
let b = Uuid::from_u128(2);
let expected = vec![
(
a,
748_365_513,
RankScoreKind::Rrf,
SearchSignals {
vector_similarity: Some(DeterministicScore::from_raw(1_i64 << 31)),
keyword_score: Some(DeterministicScore::from_raw(1_i64 << 30)),
},
),
(
b,
748_365_513,
RankScoreKind::Rrf,
SearchSignals {
vector_similarity: Some(DeterministicScore::from_raw(1_i64 << 32)),
keyword_score: Some(DeterministicScore::from_raw(3_i64 << 30)),
},
),
];
// Swapping both arms preserves each ID's rank-1 plus rank-2 total.
for (text_ids, vector_ids) in [([a, b], [b, a]), ([b, a], [a, b])] {
for _ in 0..4 {
let text = text_ids
.into_iter()
.enumerate()
.map(|(rank, id)| TextSearchHit {
score: DeterministicScore::from_raw(
if id == a { 1_i64 } else { 3_i64 } << 30,
),
..text_hit(id, rank as u32 + 1, "candidate")
})
.collect();
let vector = vector_ids
.into_iter()
.enumerate()
.map(|(rank, id)| VectorSearchHit {
score: DeterministicScore::from_raw(
if id == a { 1_i64 } else { 2_i64 } << 31,
),
..vector_hit(id, rank as u32 + 1)
})
.collect();
let hits = rrf_fuse(text, vector, 10, "unmatched");
assert_eq!(hits.len(), 2);
assert_eq!(hits[0].score, hits[1].score);
assert!(hits.iter().all(|hit| hit.source == SearchSource::Both));
let snapshot: Vec<_> = hits
.iter()
.map(|hit| {
(
hit.entity_id,
hit.score.to_raw(),
hit.rank_score_kind,
hit.signals,
)
})
.collect();
assert_eq!(snapshot, expected);
}
}
}
#[test]
fn rrf_fuse_text_only() {
let a = Uuid::new_v4();
let b = Uuid::new_v4();
let text = vec![text_hit(a, 1, "A"), text_hit(b, 2, "B")];
let hits = rrf_fuse(text, vec![], 10, "query");
assert_eq!(hits.len(), 2);
assert_eq!(hits[0].entity_id, a);
assert_eq!(hits[0].source, SearchSource::Text);
assert_eq!(hits[0].title.as_deref(), Some("A"));
}
#[test]
fn rrf_fuse_vector_only() {
let a = Uuid::new_v4();
let hits = rrf_fuse(vec![], vec![vector_hit(a, 1)], 10, "query");
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].source, SearchSource::Vector);
assert!(hits[0].title.is_none());
}
#[test]
fn rrf_fuse_marks_both_when_in_both_lists() {
let id = Uuid::new_v4();
let text = vec![text_hit(id, 1, "A")];
let vec = vec![vector_hit(id, 1)];
let hits = rrf_fuse(text, vec, 10, "query");
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].source, SearchSource::Both);
}
#[test]
fn rrf_fuse_preserves_unique_leg_scores_exactly() {
let text_only = Uuid::new_v4();
let both = Uuid::new_v4();
let vector_only = Uuid::new_v4();
let text = vec![text_hit(text_only, 1, "A"), text_hit(both, 2, "B")];
let vector = vec![vector_hit(both, 1), vector_hit(vector_only, 2)];
let hits = rrf_fuse(text, vector, 10, "query");
let score_for = |id| {
hits.iter()
.find(|hit| hit.entity_id == id)
.expect("expected fused hit")
.score
};
assert_eq!(score_for(text_only), rrf_score(1, RRF_K));
assert_eq!(score_for(both), rrf_score(2, RRF_K) + rrf_score(1, RRF_K));
assert_eq!(score_for(vector_only), rrf_score(2, RRF_K));
}
#[test]
fn rrf_fuse_counts_duplicate_once_per_leg() {
let id = Uuid::new_v4();
let text = vec![text_hit(id, 1, "A"), text_hit(id, 2, "A duplicate")];
let vector = vec![vector_hit(id, 1), vector_hit(id, 2)];
let hits = rrf_fuse(text, vector, 10, "query");
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].source, SearchSource::Both);
assert_eq!(hits[0].score, rrf_score(1, RRF_K) + rrf_score(1, RRF_K));
}
#[test]
fn rrf_fuse_respects_limit() {
let hits: Vec<TextSearchHit> = (0..20)
.map(|i| text_hit(Uuid::new_v4(), i + 1, "x"))
.collect();
let fused = rrf_fuse(hits, vec![], 5, "query");
assert_eq!(fused.len(), 5);
}
#[test]
fn rrf_fuse_orders_higher_score_first() {
// Same UUID in both lists at rank 1 → score 2/(10+1). Different UUIDs → 1/(10+1) each.
let a = Uuid::new_v4();
let b = Uuid::new_v4();
let text = vec![text_hit(a, 1, "A")];
let vec = vec![vector_hit(a, 1), vector_hit(b, 2)];
let hits = rrf_fuse(text, vec, 10, "query");
assert_eq!(hits[0].entity_id, a);
assert_eq!(hits[0].source, SearchSource::Both);
assert!(hits[0].score > hits[1].score);
}
#[test]
fn rrf_fuse_k10_score_spread_exceeds_threshold() {
// With k=10: rank 1 → 1/11 ≈ 0.0909, rank 10 → 1/20 = 0.0500.
// Spread ≈ 0.041, well above the 0.03 minimum required for reliable dedup.
let ids: Vec<Uuid> = (0..10).map(|_| Uuid::new_v4()).collect();
let text: Vec<TextSearchHit> = ids
.iter()
.enumerate()
.map(|(i, &id)| text_hit(id, (i + 1) as u32, "x"))
.collect();
let hits = rrf_fuse(text, vec![], 10, "query");
assert_eq!(hits.len(), 10);
let top_score = hits[0].score.to_f64();
let bottom_score = hits[9].score.to_f64();
let spread = top_score - bottom_score;
assert!(
spread >= 0.03,
"score spread {spread:.4} between rank 1 and rank 10 must be ≥ 0.03 (was {spread:.4})"
);
}
#[test]
fn rrf_fuse_exact_match_boost_elevates_score() {
// An entity whose title exactly matches the query should receive a score
// significantly above a non-matching entity ranked first by text search.
let exact_id = Uuid::new_v4();
let other_id = Uuid::new_v4();
// other_id ranks 1 in text, exact_id ranks 2 — but exact_id matches query.
let text = vec![
text_hit(other_id, 1, "something else"),
text_hit(exact_id, 2, "FlashAttention"),
];
let hits = rrf_fuse(text, vec![], 10, "flashattention");
assert_eq!(hits.len(), 2);
assert_eq!(
hits[0].entity_id, exact_id,
"exact match must rank first despite being rank-2 in raw text search"
);
}
// ---- embed_batch tests ----
#[test]
fn embed_batch_unconfigured_on_memory_runtime() {
// KhiveRuntime::memory() has no embedding model — embed_batch returns Unconfigured.
let rt = KhiveRuntime::memory().unwrap();
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(rt.embed_batch(&[]));
// Empty slice short-circuits before hitting the model check.
assert!(result.is_ok());
assert!(result.unwrap().is_empty());
}
#[test]
fn embed_batch_empty_input_returns_empty_vec() {
// No model needed — empty slice is handled before the embedder is touched.
let rt = KhiveRuntime::memory().unwrap();
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(rt.embed_batch(&[]));
assert_eq!(result.unwrap(), Vec::<Vec<f32>>::new());
}
#[test]
fn embed_batch_no_model_non_empty_returns_unconfigured() {
let rt = KhiveRuntime::memory().unwrap();
let texts = vec!["hello".to_string()];
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(rt.embed_batch(&texts));
match result {
Err(crate::RuntimeError::Unconfigured(s)) => assert_eq!(s, "embedding_model"),
Err(other) => panic!("expected Unconfigured, got {:?}", other),
Ok(_) => panic!("expected Err, got Ok"),
}
}
#[test]
#[ignore = "loads ~80 MB model; run with --include-ignored"]
fn embed_batch_count_matches_input() {
let config = RuntimeConfig {
db_path: None,
default_namespace: Namespace::parse("test").unwrap(),
embedding_model: Some(EmbeddingModel::AllMiniLmL6V2),
packs: vec!["kg".to_string()],
..RuntimeConfig::default()
};
let rt = KhiveRuntime::new(config).unwrap();
let texts: Vec<String> = vec!["foo".to_string(), "bar".to_string(), "baz".to_string()];
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(rt.embed_batch(&texts));
let embeddings = result.unwrap();
assert_eq!(embeddings.len(), texts.len());
}
#[test]
fn vector_search_requires_embedding_or_text() {
let rt = KhiveRuntime::memory().unwrap();
let tok = NamespaceToken::local();
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(rt.vector_search(&tok, None, None, 10, Some(SubstrateKind::Entity)));
match result {
Err(crate::RuntimeError::InvalidInput(msg)) => {
assert!(msg.contains("query_embedding or query_text"), "msg: {msg}");
}
other => panic!("expected InvalidInput, got {other:?}"),
}
}
#[test]
fn vector_search_text_without_model_returns_unconfigured() {
let rt = KhiveRuntime::memory().unwrap();
let tok = NamespaceToken::local();
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(rt.vector_search(
&tok,
None,
Some("attention"),
10,
Some(SubstrateKind::Entity),
));
match result {
Err(crate::RuntimeError::Unconfigured(s)) => assert_eq!(s, "embedding_model"),
other => panic!("expected Unconfigured, got {other:?}"),
}
}
#[test]
#[ignore = "loads ~80 MB model; run with --include-ignored"]
fn embed_batch_vectors_have_expected_dimensions() {
let model = EmbeddingModel::AllMiniLmL6V2;
let config = RuntimeConfig {
db_path: None,
default_namespace: Namespace::parse("test").unwrap(),
embedding_model: Some(model),
packs: vec!["kg".to_string()],
..RuntimeConfig::default()
};
let rt = KhiveRuntime::new(config).unwrap();
let texts = vec!["hello world".to_string()];
let result = tokio::runtime::Runtime::new()
.unwrap()
.block_on(rt.embed_batch(&texts));
let embeddings = result.unwrap();
assert_eq!(embeddings[0].len(), model.dimensions());
}
// ---- hybrid_search_outcome: vector-arm failure must not discard text hits ----
/// Baseline (red without the fix): the fail-loud `hybrid_search` must
/// still propagate a vector-arm failure as a whole-call error, even
/// though the text leg found a match — proving `break_vector_arm`
/// genuinely exercises the vector leg and that `hybrid_search_outcome`'s
/// tolerance below is a real behavioral difference, not a no-op.
#[tokio::test]
async fn hybrid_search_still_fails_loud_on_vector_arm_error() {
let rt = runtime_with_constant_embeddings();
let tok = NamespaceToken::local();
rt.create_entity(
&tok,
"concept",
None,
"FlashAttention",
Some("IO-aware exact attention using tiling"),
None,
vec![],
)
.await
.unwrap();
break_vector_arm(&rt);
let result = rt
.hybrid_search(&tok, "FlashAttention", None, 10, None, None, &[], None)
.await;
assert!(
result.is_err(),
"the fail-loud entry point must still propagate a vector-arm failure, got {result:?}"
);
}
/// Green: the coordinator-only outcome variant preserves the text hit and
/// reports the vector failure separately instead of discarding the whole
/// call.
#[tokio::test]
async fn hybrid_search_outcome_preserves_text_hits_on_vector_arm_error() {
let rt = runtime_with_constant_embeddings();
let tok = NamespaceToken::local();
rt.create_entity(
&tok,
"concept",
None,
"FlashAttention",
Some("IO-aware exact attention using tiling"),
None,
vec![],
)
.await
.unwrap();
break_vector_arm(&rt);
let outcome = rt
.hybrid_search_outcome(&tok, "FlashAttention", 10, None, None, &[], None)
.await
.expect("text leg must still succeed");
assert!(
!outcome.hits.is_empty(),
"text arm's hit must survive a vector-arm failure"
);
assert!(
outcome.hits[0]
.title
.as_deref()
.unwrap_or_default()
.contains("FlashAttention"),
"surviving hit must be the text match"
);
let vector_error = outcome
.vector_error
.expect("vector arm failure must be reported");
assert!(
vector_error.contains("injected vector-arm failure"),
"vector_error must carry the underlying cause, got {vector_error:?}"
);
}
/// A healthy vector arm still leaves `vector_error` `None` — the outcome
/// variant does not manufacture a failure where there is none.
#[tokio::test]
async fn hybrid_search_outcome_has_no_vector_error_when_vector_arm_healthy() {
let rt = runtime_with_constant_embeddings();
let tok = NamespaceToken::local();
rt.create_entity(
&tok,
"concept",
None,
"FlashAttention",
Some("IO-aware exact attention using tiling"),
None,
vec![],
)
.await
.unwrap();
let outcome = rt
.hybrid_search_outcome(&tok, "FlashAttention", 10, None, None, &[], None)
.await
.expect("hybrid search must succeed");
assert!(!outcome.hits.is_empty(), "should find the entity");
assert!(
outcome.vector_error.is_none(),
"a healthy vector arm must not report an error"
);
}
// ---- hybrid_search enrichment ----
#[tokio::test]
async fn hybrid_search_entity_hit_has_title() {
let rt = KhiveRuntime::memory().unwrap();
let tok = NamespaceToken::local();
rt.create_entity(
&tok,
"concept",
None,
"FlashAttention",
Some("IO-aware exact attention using tiling"),
None,
vec![],
)
.await
.unwrap();
let hits = rt
.hybrid_search(&tok, "FlashAttention", None, 10, None, None, &[], None)
.await
.unwrap();
assert!(!hits.is_empty(), "should find the entity");
let hit = &hits[0];
assert!(hit.title.is_some(), "title must be populated");
assert!(
hit.title.as_deref().unwrap().contains("FlashAttention"),
"title must contain entity name"
);
}
/// `hybrid_search` must not hard-fail on a query containing FTS5 metacharacters
/// like `$` (e.g. the DSL doc query `$prev.id`). `sanitize_fts5_query` (khive-db)
/// strips `$`, so this exercises the sanitizer path and takes the `Ok` arm; see
/// `hybrid_search_with_residual_fts5_char_now_sanitized` below for the character
/// class #916 closed (previously the fail-loud arm, prior to #916).
#[tokio::test]
async fn hybrid_search_with_dollar_sign_query_does_not_error() {
let rt = KhiveRuntime::memory().unwrap();
let tok = NamespaceToken::local();
rt.create_entity(
&tok,
"concept",
None,
"DSL docs",
Some("use $prev.id to chain calls"),
None,
vec![],
)
.await
.unwrap();
let result = rt
.hybrid_search(&tok, "$prev.id", None, 10, None, None, &[], None)
.await;
assert!(
result.is_ok(),
"#388 hybrid_search must not hard-fail on a '$'-bearing query, got: {:?}",
result.err()
);
}
/// #916: `@` was previously NOT stripped by `sanitize_fts5_query` and SQLite
/// FTS5's bareword parser rejected it unconditionally, so this query used to
/// reach the runtime-level fail-loud arm (`RuntimeError::InvalidInput`, #569).
/// `sanitize_fts5_token_group`'s bareword-safety gate now recognizes `@` (and
/// every other ASCII punctuation character not already handled) as unsafe for
/// an unquoted bareword position and routes it through the quoted-phrase
/// alternative instead, which FTS5 accepts literally, so the query
/// now succeeds and finds the seeded content rather than erroring.
#[tokio::test]
async fn hybrid_search_with_residual_fts5_char_now_sanitized() {
let rt = KhiveRuntime::memory().unwrap();
let tok = NamespaceToken::local();
rt.create_entity(
&tok,
"concept",
None,
"DSL docs",
Some("use foo@bar to chain calls"),
None,
vec![],
)
.await
.unwrap();
let result = rt
.hybrid_search(&tok, "foo@bar", None, 10, None, None, &[], None)
.await;
let hits = result.unwrap_or_else(|e| {
panic!("#916 hybrid_search must not fail on an '@'-bearing query, got: {e:?}")
});
assert!(
!hits.is_empty(),
"#916 '@'-bearing query must still find the seeded 'foo@bar' content via the \
quoted-phrase alternative"
);
}
/// #916 end-to-end regression, using the exact character classes from the
/// issue's live-log evidence (`#682 Stage 2`, `Min-K%Prob`, `B=128`):
/// `hybrid_search`'s FTS leg must not lose its lexical signal to a parser
/// syntax error on `#`, `%`, or `=`. Each query below must both succeed
/// and actually surface a `Text`/`Both`-sourced hit, proving the FTS leg
/// contributed, not just that the vector leg papered over a degraded
/// text leg.
#[tokio::test]
async fn hybrid_search_with_916_issue_characters_finds_text_leg_hits() {
let rt = KhiveRuntime::memory().unwrap();
let tok = NamespaceToken::local();
rt.create_entity(
&tok,
"concept",
None,
"issue tracker",
Some("tracking #682 Stage 2: MoE expert-cache prefetch work"),
None,
vec![],
)
.await
.unwrap();
rt.create_entity(
&tok,
"concept",
None,
"benchmark notes",
Some("chunkwise B=128 traffic arithmetic simdgroup_matrix DPLR"),
None,
vec![],
)
.await
.unwrap();
rt.create_entity(
&tok,
"concept",
None,
"sampling notes",
Some("evaluated with the Min-K%Prob membership inference method"),
None,
vec![],
)
.await
.unwrap();
for query in ["#682 Stage 2", "B=128", "Min-K%Prob"] {
let result = rt
.hybrid_search(&tok, query, None, 10, None, None, &[], None)
.await;
let hits = result.unwrap_or_else(|e| {
panic!("#916 hybrid_search must not fail on query {query:?}, got: {e:?}")
});
assert!(
hits.iter()
.any(|h| matches!(h.source, SearchSource::Text | SearchSource::Both)),
"#916 query {query:?} must surface a Text/Both-sourced hit \
(the FTS leg must contribute, not just the vector leg); got {hits:?}"
);
}
}
// ---- predicate pushdown before truncation ----
/// Entity-branch tag-filter regression.
///
/// Scenario: `limit=1`, tag_filter=["target-tag"]. Two entities are inserted:
/// - "decoy_alpha_beta_gamma": many query tokens → ranks 1 in FTS (dominates).
/// Does NOT have "target-tag".
/// - "alpha_beta_gamma target": fewer query tokens → ranks 2 in FTS.
/// HAS "target-tag".
///
/// Without predicate pushdown, `fused.truncate(1)` keeps only the decoy and the
/// tag-matching entity is invisible: this requires `tags_any` to be passed into
/// `query_entities`'s `EntityFilter` so the decoy is excluded before truncation.
#[tokio::test]
async fn hybrid_search_tag_filter_pushed_before_truncation() {
let rt = KhiveRuntime::memory().unwrap();
let tok = NamespaceToken::local();
// Decoy: high-ranking FTS hit (content repeats query words), no target tag.
rt.create_entity(
&tok,
"concept",
None,
"alpha beta gamma decoy alpha beta gamma",
Some("alpha beta gamma decoy description alpha beta gamma"),
None,
vec!["other-tag".to_string()],
)
.await
.unwrap();
// Target: lower-ranking FTS hit, has the tag we filter on.
let target = rt
.create_entity(
&tok,
"concept",
None,
"alpha beta gamma target",
Some("alpha beta gamma target description"),
None,
vec!["target-tag".to_string()],
)
.await
.unwrap();
// With limit=1 and tag_filter, the fix must return the target entity despite
// the decoy ranking higher. Without pushdown, the decoy occupies the single
// slot and the target is silently dropped.
let hits = rt
.hybrid_search(
&tok,
"alpha beta gamma",
None,
1,
None,
None,
&["target-tag".to_string()],
None,
)
.await
.unwrap();
assert_eq!(
hits.len(),
1,
"exactly one hit expected (the tag-matching entity)"
);
assert_eq!(
hits[0].entity_id, target.id,
"the tag-filtered entity must be returned even when ranked below limit in raw fusion"
);
}
/// Entity-branch properties-filter regression (analogous to the tag-filter test above).
///
/// Scenario: `limit=1`, properties_filter={{"domain": "target"}}. Two entities:
/// - decoy: high FTS rank, properties {{"domain": "other"}}.
/// - target: lower FTS rank, properties {{"domain": "target"}}.
///
/// Without pushdown: decoy fills the slot, target is dropped. With pushdown:
/// only the target survives the properties filter before truncation.
#[tokio::test]
async fn hybrid_search_props_filter_pushed_before_truncation() {
let rt = KhiveRuntime::memory().unwrap();
let tok = NamespaceToken::local();
rt.create_entity(
&tok,
"concept",
None,
"delta epsilon zeta decoy delta epsilon zeta",
Some("delta epsilon zeta decoy description delta epsilon zeta"),
Some(serde_json::json!({"domain": "other"})),
vec![],
)
.await
.unwrap();
let target = rt
.create_entity(
&tok,
"concept",
None,
"delta epsilon zeta target",
Some("delta epsilon zeta target description"),
Some(serde_json::json!({"domain": "target"})),
vec![],
)
.await
.unwrap();
let filter = serde_json::json!({"domain": "target"});
let hits = rt
.hybrid_search(
&tok,
"delta epsilon zeta",
None,
1,
None,
None,
&[],
Some(&filter),
)
.await
.unwrap();
assert_eq!(hits.len(), 1, "exactly one hit expected (properties match)");
assert_eq!(
hits[0].entity_id, target.id,
"the properties-filtered entity must be returned even when ranked below limit"
);
}
/// Entity-branch kind-filter regression.
///
/// Scenario: `limit=1`, so the text arm fetches 4 candidates, and
/// `entity_kind="document"`. Twelve `concept` entities repeat the query term and
/// outrank the one `document` entity that mentions it once in a long description.
///
/// When the kind is applied only after the text arm has picked its candidates,
/// the filter sees four concepts and the document is never returned. The kind
/// has to reach the text query so the candidate budget is spent on documents.
#[tokio::test]
async fn hybrid_search_entity_kind_filter_pushed_into_text_arm() {
let rt = KhiveRuntime::memory().unwrap();
let tok = NamespaceToken::local();
for i in 0..12 {
rt.create_entity(
&tok,
"concept",
None,
&format!("quillfeather decoy {i}"),
Some("quillfeather quillfeather quillfeather"),
None,
vec![],
)
.await
.unwrap();
}
let description = format!(
"A long administrative description that mentions quillfeather once. {}",
"Unrelated filing, scheduling and review words. ".repeat(20)
);
let target = rt
.create_entity(
&tok,
"document",
None,
"Archive filing report",
Some(description.as_str()),
None,
vec![],
)
.await
.unwrap();
// Premise: without a kind filter the target ranks behind more entities than
// the 4 candidates the filtered `limit=1` search requests from the text arm.
let unfiltered = rt
.hybrid_search(&tok, "quillfeather", None, 13, None, None, &[], None)
.await
.unwrap();
let target_rank = unfiltered
.iter()
.position(|hit| hit.entity_id == target.id)
.expect("the document is found without a kind filter");
assert!(
target_rank >= 4,
"the document must rank behind the candidate window, got rank {target_rank}"
);
let hits = rt
.hybrid_search(
&tok,
"quillfeather",
None,
1,
Some("document"),
None,
&[],
None,
)
.await
.unwrap();
assert_eq!(
hits.len(),
1,
"the kind-filtered search must return the matching document"
);
assert_eq!(
hits[0].entity_id, target.id,
"the returned hit must be the document, not a concept"
);
}
/// `hybrid_search_each_kind` gives each kind the list `hybrid_search` returns for it.
///
/// Scenario: twenty `concept` entities repeat the query term and outrank three
/// `document` entities that mention it once, with `limit=2`. The text arm fetches 8
/// candidates per kind, so the concepts alone overflow it and the cut to `limit`
/// binds. A text budget shared by the two kinds would leave the documents none; each
/// kind has to keep its own, so both lists equal the per-kind search.
#[tokio::test]
async fn hybrid_search_each_kind_matches_per_kind_search_when_one_kind_dominates() {
let rt = KhiveRuntime::memory().unwrap();
let tok = NamespaceToken::local();
for i in 0..20 {
rt.create_entity(
&tok,
"concept",
None,
&format!("quillfeather decoy {i}"),
Some("quillfeather quillfeather quillfeather"),
None,
vec![],
)
.await
.unwrap();
}
for i in 0..3 {
let description = format!(
"A long administrative description {i} that mentions quillfeather once. {}",
"Unrelated filing, scheduling and review words. ".repeat(20)
);
rt.create_entity(
&tok,
"document",
None,
&format!("Archive filing report {i}"),
Some(description.as_str()),
None,
vec![],
)
.await
.unwrap();
}
let kinds = ["concept", "document"];
let mut expected: Vec<Vec<Uuid>> = Vec::new();
for kind in kinds {
let hits = rt
.hybrid_search(&tok, "quillfeather", None, 2, Some(kind), None, &[], None)
.await
.unwrap();
expected.push(hits.iter().map(|hit| hit.entity_id).collect());
}
assert_eq!(
expected[0].len(),
2,
"premise: the concept list is cut to limit"
);
assert_eq!(
expected[1].len(),
2,
"premise: the document list is not starved"
);
let per_kind = rt
.hybrid_search_each_kind(&tok, "quillfeather", None, 2, &kinds)
.await
.unwrap();
let mut actual: Vec<Vec<Uuid>> = Vec::new();
for hits in &per_kind {
actual.push(hits.iter().map(|hit| hit.entity_id).collect());
}
assert_eq!(
actual, expected,
"each kind must get the ids and order its own per-kind search returns"
);
}
// ---- embed intent tests ----
struct CapturingEmbeddingService {
captured: std::sync::Arc<std::sync::Mutex<Vec<Vec<String>>>>,
}
#[async_trait::async_trait]
impl EmbeddingService for CapturingEmbeddingService {
async fn embed(
&self,
texts: &[String],
_model: EmbeddingModel,
) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
self.captured.lock().unwrap().push(texts.to_vec());
Ok(texts.iter().map(|_| vec![1.0]).collect())
}
fn supports_model(&self, _model: EmbeddingModel) -> bool {
true
}
fn name(&self) -> &'static str {
"capturing-embedding-service"
}
}
struct CapturingEmbedderProvider {
name: String,
captured: std::sync::Arc<std::sync::Mutex<Vec<Vec<String>>>>,
}
struct RewritingPassageService {
captured: std::sync::Arc<std::sync::Mutex<Vec<Vec<String>>>>,
}
#[async_trait::async_trait]
impl EmbeddingService for RewritingPassageService {
async fn embed(
&self,
texts: &[String],
_model: EmbeddingModel,
) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
self.captured.lock().unwrap().push(texts.to_vec());
Ok(texts.iter().map(|_| vec![1.0]).collect())
}
async fn embed_passage(
&self,
texts: &[String],
model: EmbeddingModel,
) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
let prepared: Vec<String> = texts.iter().map(|text| format!("custom:{text}")).collect();
self.embed(&prepared, model).await
}
fn supports_model(&self, _model: EmbeddingModel) -> bool {
true
}
fn name(&self) -> &'static str {
"rewriting-passage-service"
}
}
struct RewritingPassageProvider {
name: String,
captured: std::sync::Arc<std::sync::Mutex<Vec<Vec<String>>>>,
}
#[async_trait::async_trait]
impl EmbedderProvider for RewritingPassageProvider {
fn name(&self) -> &str {
&self.name
}
fn dimensions(&self) -> usize {
1
}
async fn build(&self) -> crate::error::RuntimeResult<std::sync::Arc<dyn EmbeddingService>> {
Ok(std::sync::Arc::new(RewritingPassageService {
captured: std::sync::Arc::clone(&self.captured),
}))
}
}
struct WrongCardinalityEmbeddingService;
#[async_trait::async_trait]
impl EmbeddingService for WrongCardinalityEmbeddingService {
async fn embed(
&self,
texts: &[String],
_model: EmbeddingModel,
) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
Ok(texts
.iter()
.take(texts.len().saturating_sub(1))
.map(|_| vec![1.0])
.collect())
}
fn supports_model(&self, _model: EmbeddingModel) -> bool {
true
}
fn name(&self) -> &'static str {
"wrong-cardinality-embedding-service"
}
}
struct WrongCardinalityEmbedderProvider;
#[async_trait::async_trait]
impl EmbedderProvider for WrongCardinalityEmbedderProvider {
fn name(&self) -> &str {
"wrong-cardinality-embedding-service"
}
fn dimensions(&self) -> usize {
1
}
async fn build(&self) -> crate::error::RuntimeResult<std::sync::Arc<dyn EmbeddingService>> {
Ok(std::sync::Arc::new(WrongCardinalityEmbeddingService))
}
}
struct SurplusCardinalityEmbeddingService;
#[async_trait::async_trait]
impl EmbeddingService for SurplusCardinalityEmbeddingService {
async fn embed(
&self,
texts: &[String],
_model: EmbeddingModel,
) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
let mut vectors: Vec<Vec<f32>> = texts.iter().map(|_| vec![1.0]).collect();
vectors.push(vec![1.0]);
Ok(vectors)
}
fn supports_model(&self, _model: EmbeddingModel) -> bool {
true
}
fn name(&self) -> &'static str {
"surplus-cardinality-embedding-service"
}
}
struct SurplusCardinalityEmbedderProvider;
#[async_trait::async_trait]
impl EmbedderProvider for SurplusCardinalityEmbedderProvider {
fn name(&self) -> &str {
"surplus-cardinality-embedding-service"
}
fn dimensions(&self) -> usize {
1
}
async fn build(&self) -> crate::error::RuntimeResult<std::sync::Arc<dyn EmbeddingService>> {
Ok(std::sync::Arc::new(SurplusCardinalityEmbeddingService))
}
}
#[async_trait::async_trait]
impl EmbedderProvider for CapturingEmbedderProvider {
fn name(&self) -> &str {
&self.name
}
fn dimensions(&self) -> usize {
1
}
async fn build(&self) -> crate::error::RuntimeResult<std::sync::Arc<dyn EmbeddingService>> {
Ok(std::sync::Arc::new(CapturingEmbeddingService {
captured: std::sync::Arc::clone(&self.captured),
}))
}
}
fn runtime_with_capturing_embedder(
model: EmbeddingModel,
) -> (
KhiveRuntime,
std::sync::Arc<std::sync::Mutex<Vec<Vec<String>>>>,
) {
let runtime = KhiveRuntime::memory().unwrap();
let captured = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
runtime.register_embedder(CapturingEmbedderProvider {
name: model.to_string(),
captured: std::sync::Arc::clone(&captured),
});
(runtime, captured)
}
#[tokio::test]
async fn bge_query_paths_pass_raw_unprefixed_text() {
const BGE_QUERY_INSTRUCTION: &str =
"Represent this sentence for searching relevant passages: ";
let single = "single raw query";
let batch = vec![
"first raw query".to_string(),
"second raw query".to_string(),
];
for model in [
EmbeddingModel::BgeSmallEnV15,
EmbeddingModel::BgeBaseEnV15,
EmbeddingModel::BgeLargeEnV15,
] {
let (runtime, captured) = runtime_with_capturing_embedder(model);
runtime
.embed_query_with_model(&model.to_string(), single)
.await
.unwrap();
runtime
.embed_query_batch_with_model(&model.to_string(), &batch)
.await
.unwrap();
let calls = captured.lock().unwrap().clone();
assert_eq!(
calls,
vec![vec![single.to_string()], batch.clone()],
"{model} must receive raw query text through single and batch paths"
);
assert!(
calls
.iter()
.flatten()
.all(|text| !text.contains(BGE_QUERY_INSTRUCTION)),
"{model} must not receive the BGE retrieval instruction"
);
}
}
#[tokio::test]
async fn e5_query_paths_apply_query_prefix() {
let model = EmbeddingModel::MultilingualE5Small;
let single = "single raw query";
let batch = vec![
"first raw query".to_string(),
"second raw query".to_string(),
];
let (runtime, captured) = runtime_with_capturing_embedder(model);
runtime
.embed_query_with_model(&model.to_string(), single)
.await
.unwrap();
runtime
.embed_query_batch_with_model(&model.to_string(), &batch)
.await
.unwrap();
assert_eq!(
captured.lock().unwrap().as_slice(),
[
vec!["query: single raw query".to_string()],
vec![
"query: first raw query".to_string(),
"query: second raw query".to_string(),
],
],
"E5 must receive its query prefix through single and batch paths"
);
}
#[tokio::test]
async fn mixed_document_batch_stays_one_ordered_provider_call() {
let model = EmbeddingModel::AllMiniLmL6V2;
let (runtime, captured) = runtime_with_capturing_embedder(model);
let texts = vec![
"first normal document".to_string(),
"x".repeat(MAX_TEXT_BYTES + 1),
"second normal document".to_string(),
];
let outcomes = runtime
.embed_document_batch_with_model_outcomes(&model.to_string(), &texts)
.await
.expect("mixed batch must embed");
assert_eq!(outcomes.len(), texts.len());
assert!(!outcomes[0].truncated);
assert_eq!(outcomes[0].source_bytes, outcomes[0].embedded_bytes);
assert!(outcomes[1].truncated);
assert_eq!(outcomes[1].source_bytes, MAX_TEXT_BYTES + 1);
assert_eq!(outcomes[1].embedded_bytes, MAX_TEXT_BYTES);
assert!(!outcomes[2].truncated);
assert_eq!(
captured.lock().unwrap().as_slice(),
[vec![
texts[0].clone(),
"x".repeat(MAX_TEXT_BYTES),
texts[2].clone(),
]]
);
}
#[tokio::test]
async fn canonical_name_override_has_no_exact_input_fingerprint() {
let model = EmbeddingModel::MultilingualE5Small;
let model_name = model.to_string();
let (runtime, captured) = runtime_with_capturing_embedder(model);
let input = "x".repeat(document_embedding_budget(&model_name) + 3);
let outcomes = runtime
.embed_document_batch_with_model_outcomes(&model_name, std::slice::from_ref(&input))
.await
.unwrap();
let calls = captured.lock().unwrap();
let actual_input = &calls[0][0];
assert!(actual_input.starts_with("passage: "));
assert_eq!(actual_input.len(), MAX_TEXT_BYTES);
assert!(outcomes[0].truncated);
assert_eq!(outcomes[0].prepared_text_fingerprint, None);
assert_eq!(
prepared_document_fingerprint(
bounded_embedding_input(&input, document_embedding_budget(&model_name)).0,
model,
),
VectorRecord::fingerprint_text(actual_input),
"the audited lattice path hashes the bounded text plus passage prefix"
);
}
#[tokio::test]
async fn ordinary_custom_document_provider_has_no_exact_input_fingerprint() {
let runtime = KhiveRuntime::memory().unwrap();
let captured = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
runtime.register_embedder(CapturingEmbedderProvider {
name: "custom-document-provider".to_string(),
captured: std::sync::Arc::clone(&captured),
});
let outcome = runtime
.embed_document_with_model_outcome("custom-document-provider", "document")
.await
.expect("custom provider must embed");
assert_eq!(outcome.prepared_text_fingerprint, None);
assert_eq!(captured.lock().unwrap().len(), 1);
}
#[tokio::test]
async fn custom_passage_override_under_builtin_name_cannot_claim_exact_input() {
let runtime = KhiveRuntime::memory().unwrap();
let captured = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
let model = EmbeddingModel::MultilingualE5Small;
let model_name = model.to_string();
runtime.register_embedder(RewritingPassageProvider {
name: model_name.clone(),
captured: std::sync::Arc::clone(&captured),
});
let outcome = runtime
.embed_document_with_model_outcome(&model_name, "document")
.await
.expect("custom override must embed");
assert_eq!(outcome.prepared_text_fingerprint, None);
assert_eq!(
captured.lock().unwrap()[0],
vec!["custom:document".to_string()]
);
assert_ne!(
prepared_document_fingerprint("document", model),
VectorRecord::fingerprint_text("custom:document")
);
}
#[tokio::test]
async fn truncated_document_batch_rejects_provider_cardinality_mismatch() {
let runtime = KhiveRuntime::memory().unwrap();
runtime.register_embedder(WrongCardinalityEmbedderProvider);
let model_name = "wrong-cardinality-embedding-service";
let texts = vec!["x".repeat(MAX_TEXT_BYTES + 1), "normal".to_string()];
let error = runtime
.embed_document_batch_with_model_outcomes(model_name, &texts)
.await
.expect_err("provider cardinality mismatch must fail the whole batch");
assert!(
error
.to_string()
.contains("embed_passage returned 1 vectors for 2 inputs"),
"unexpected error: {error}"
);
}
#[tokio::test]
async fn singleton_document_embed_rejects_zero_vectors() {
let runtime = KhiveRuntime::memory().unwrap();
runtime.register_embedder(WrongCardinalityEmbedderProvider);
let model_name = "wrong-cardinality-embedding-service";
let error = runtime
.embed_document_with_model_outcome(model_name, "single document")
.await
.expect_err("provider returning zero vectors must fail closed");
assert!(
error
.to_string()
.contains("embed_passage returned 0 vectors for 1 input"),
"unexpected error: {error}"
);
}
#[tokio::test]
async fn singleton_document_embed_rejects_surplus_vectors() {
let runtime = KhiveRuntime::memory().unwrap();
runtime.register_embedder(SurplusCardinalityEmbedderProvider);
let model_name = "surplus-cardinality-embedding-service";
let error = runtime
.embed_document_with_model_outcome(model_name, "single document")
.await
.expect_err("provider returning surplus vectors must fail closed");
assert!(
error
.to_string()
.contains("embed_passage returned 2 vectors for 1 input"),
"unexpected error: {error}"
);
}
#[test]
#[ignore = "loads ~80 MB model; run with --include-ignored"]
fn minilm_document_and_query_embed_are_identical_no_prefix_model() {
// MiniLM has no instruction prefixes; document and query paths must
// produce byte-identical vectors so that existing stored vectors remain
// comparable after this change.
let model = EmbeddingModel::AllMiniLmL6V2;
let config = RuntimeConfig {
db_path: None,
default_namespace: Namespace::parse("test").unwrap(),
embedding_model: Some(model),
packs: vec!["kg".to_string()],
..RuntimeConfig::default()
};
let rt = KhiveRuntime::new(config).unwrap();
let text = "attention is all you need".to_string();
let rt_ref = &rt;
let (doc_emb, query_emb) = tokio::runtime::Runtime::new().unwrap().block_on(async {
let d = rt_ref
.embed_document_with_model_outcome(&model.to_string(), &text)
.await
.unwrap()
.vector;
let q = rt_ref
.embed_query_with_model(&model.to_string(), &text)
.await
.unwrap();
(d, q)
});
assert_eq!(
doc_emb, query_emb,
"MiniLM has no instruction prefix: document and query embeds must be identical"
);
}
#[test]
#[ignore = "loads multilingual-e5-small (~90 MB); run with --include-ignored"]
fn e5_document_and_query_embed_differ_instruction_tuned_model() {
// multilingual-e5 prepends "passage: " for documents and "query: " for
// queries. The same raw text must produce different embeddings when the
// correct prefixes are applied, confirming the asymmetric-retrieval
// capability is now exercised.
let model = EmbeddingModel::MultilingualE5Small;
let config = RuntimeConfig {
db_path: None,
default_namespace: Namespace::parse("test").unwrap(),
embedding_model: Some(model),
packs: vec!["kg".to_string()],
..RuntimeConfig::default()
};
let rt = KhiveRuntime::new(config).unwrap();
let text = "attention is all you need".to_string();
let rt_ref = &rt;
let (doc_emb, query_emb) = tokio::runtime::Runtime::new().unwrap().block_on(async {
let d = rt_ref
.embed_document_with_model_outcome(&model.to_string(), &text)
.await
.unwrap()
.vector;
let q = rt_ref
.embed_query_with_model(&model.to_string(), &text)
.await
.unwrap();
(d, q)
});
assert_ne!(
doc_emb, query_emb,
"multilingual-e5-small uses asymmetric prefixes: document ('passage: ') \
and query ('query: ') embeds of the same text must differ"
);
}
// ---- backfill reader error must be propagated, not swallowed ----
use crate::embedder_registry::EmbedderProvider;
use lattice_embed::EmbeddingService;
/// A stub embedder that never actually loads weights — used to satisfy the
/// `registered_embedding_model_names` check inside `backfill_missing_embeddings`
/// without triggering a real model load. The test fault-injects a reader error
/// before any embedding call is made, so `embed()` is never reached.
struct StubEmbedderProvider;
#[async_trait::async_trait]
impl EmbedderProvider for StubEmbedderProvider {
fn name(&self) -> &str {
"stub-model-m07"
}
fn dimensions(&self) -> usize {
4
}
async fn build(&self) -> crate::error::RuntimeResult<std::sync::Arc<dyn EmbeddingService>> {
struct StubSvc;
#[async_trait::async_trait]
impl EmbeddingService for StubSvc {
async fn embed(
&self,
_texts: &[String],
_model: lattice_embed::EmbeddingModel,
) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
Ok(vec![])
}
fn supports_model(&self, _model: lattice_embed::EmbeddingModel) -> bool {
true
}
fn name(&self) -> &'static str {
"stub-svc-m07"
}
}
Ok(std::sync::Arc::new(StubSvc))
}
}
/// `backfill_missing_embeddings` must propagate a reader error rather than
/// treating it as "zero rows to embed" (a silent `Err(_) => vec![]` would
/// return `Ok(0)` and skip all embeddings without any signal).
///
/// The fault injection substitutes a `StorageError::Pool` for the result of
/// `sql.reader().await`, exercising the `map_err(RuntimeError::Storage)?` path;
/// falling back to `unwrap_or_default()` there would swallow the injected error.
#[tokio::test]
async fn backfill_reader_error_is_propagated_not_swallowed() {
let rt = KhiveRuntime::memory().unwrap();
rt.register_embedder(StubEmbedderProvider);
let tok = NamespaceToken::local();
// Arm the fault injection: the next backfill call will substitute a
// StorageError at the sql.reader().await boundary, then reset.
super::arm_backfill_reader_fail();
let result = rt.backfill_missing_embeddings(&tok).await;
assert!(
result.is_err(),
"backfill_missing_embeddings must propagate the reader error (got Ok instead)"
);
let err_msg = result.unwrap_err().to_string();
assert!(
err_msg.contains("injected failure"),
"error must originate from the injected reader failure, got: {err_msg}"
);
}
#[tokio::test]
async fn backfill_skips_a_full_page_of_excluded_messages_and_reaches_eligible_tail() {
use crate::{NoteEmbeddingPolicy, NoteEmbeddingPolicySpec};
use khive_storage::note::Note;
const PAGE: u128 = EMBEDDING_BATCH_PAGE_SIZE as u128;
let primary = EmbeddingModel::AllMiniLmL6V2;
let primary_name = primary.to_string();
let secondary_name = "zz-backfill-secondary";
let rt = KhiveRuntime::new(RuntimeConfig {
db_path: None,
embedding_model: Some(primary),
packs: vec![],
..RuntimeConfig::no_embeddings()
})
.unwrap();
rt.register_embedder(ConstantEmbedderProvider {
name: primary_name.clone(),
dimensions: primary.dimensions(),
});
rt.register_embedder(ConstantEmbedderProvider {
name: secondary_name.into(),
dimensions: 4,
});
rt.install_note_embedding_policies(&[NoteEmbeddingPolicySpec {
kind: "message",
policy: NoteEmbeddingPolicy::DefaultModel,
}]);
let tok = NamespaceToken::local();
let primary_store = rt.vectors_for_model(&tok, &primary_name).unwrap();
let secondary_store = rt.vectors_for_model(&tok, secondary_name).unwrap();
let notes = rt.notes(&tok).unwrap();
let mut seeded = Vec::new();
for ordinal in 1..=PAGE + 1 {
let mut message = Note::new("local", "message", "excluded from secondary");
message.id = Uuid::from_u128(ordinal);
seeded.push(message);
}
let mut ordinary = Note::new("local", "observation", "eligible after full page");
ordinary.id = Uuid::from_u128(u128::MAX);
seeded.push(ordinary.clone());
notes.upsert_notes(seeded).await.unwrap();
let backfilled = tokio::time::timeout(
std::time::Duration::from_secs(60),
rt.backfill_missing_embeddings(&tok),
)
.await
.expect("backfill must advance past a full excluded page")
.unwrap();
assert_eq!(backfilled, (PAGE + 2) as u64 + 1);
assert_eq!(primary_store.count().await.unwrap(), (PAGE + 2) as u64);
assert_eq!(secondary_store.count().await.unwrap(), 1);
let excluded_document = rt
.text_for_notes(&tok)
.unwrap()
.get_document("local", Uuid::from_u128(1))
.await
.unwrap()
.expect("first-model pass must index an excluded message");
assert_eq!(excluded_document.body, "excluded from secondary");
assert!(
rt.text_for_notes(&tok)
.unwrap()
.get_document("local", ordinary.id)
.await
.unwrap()
.is_some(),
"the first-model pass must still repopulate FTS"
);
}
const BACKFILL_BATCH_MODEL: &str = "backfill-batch-model";
struct CountingBackfillService {
calls: Arc<AtomicUsize>,
reject_poison: bool,
}
#[async_trait::async_trait]
impl EmbeddingService for CountingBackfillService {
async fn embed(
&self,
texts: &[String],
_model: EmbeddingModel,
) -> std::result::Result<Vec<Vec<f32>>, EmbedError> {
self.calls.fetch_add(1, Ordering::SeqCst);
if self.reject_poison
&& (texts.len() > 1 || texts.iter().any(|text| text.contains("poison")))
{
return Err(EmbedError::InferenceFailed("poison input".into()));
}
Ok(texts
.iter()
.map(|text| {
vec![
text.len() as f32,
text.bytes().map(u32::from).sum::<u32>() as f32,
text.as_bytes().first().copied().unwrap_or_default() as f32,
text.as_bytes().last().copied().unwrap_or_default() as f32,
]
})
.collect())
}
fn supports_model(&self, _model: EmbeddingModel) -> bool {
true
}
fn name(&self) -> &'static str {
"backfill-batch-counting-service"
}
}
struct CountingBackfillProvider {
calls: Arc<AtomicUsize>,
reject_poison: bool,
}
#[async_trait::async_trait]
impl EmbedderProvider for CountingBackfillProvider {
fn name(&self) -> &str {
BACKFILL_BATCH_MODEL
}
fn dimensions(&self) -> usize {
4
}
async fn build(&self) -> crate::error::RuntimeResult<Arc<dyn EmbeddingService>> {
Ok(Arc::new(CountingBackfillService {
calls: Arc::clone(&self.calls),
reject_poison: self.reject_poison,
}))
}
}
#[tokio::test]
async fn backfill_batches_provider_calls_and_matches_single_record_vectors() {
let token = NamespaceToken::local();
let runtime = KhiveRuntime::memory().unwrap();
let mut entity_ids = Vec::new();
for index in 0..257 {
let entity = runtime
.create_entity(
&token,
"concept",
None,
&format!("Backfill entity {index}"),
(index != 0).then_some("body"),
None,
vec![],
)
.await
.unwrap();
entity_ids.push(entity.id);
}
let mut note_ids = Vec::new();
for index in 0..3 {
let note = runtime
.create_note(
&token,
"observation",
None,
&format!("Backfill note {index}"),
None,
None,
vec![],
)
.await
.unwrap();
note_ids.push(note.id);
}
let calls = Arc::new(AtomicUsize::new(0));
runtime.register_embedder(CountingBackfillProvider {
calls: Arc::clone(&calls),
reject_poison: false,
});
let usage = crate::usage::UsageContext::new();
let backfilled =
crate::usage::scope(usage.clone(), runtime.backfill_missing_embeddings(&token))
.await
.unwrap();
assert_eq!(backfilled, 260);
assert_eq!(
calls.load(Ordering::SeqCst),
3,
"two entity pages and one note page"
);
assert_eq!(
usage.snapshot()["embed_calls"],
260,
"ADR-103 counts texts, not provider calls"
);
let singles = KhiveRuntime::memory().unwrap();
let single_calls = Arc::new(AtomicUsize::new(0));
singles.register_embedder(CountingBackfillProvider {
calls: Arc::clone(&single_calls),
reject_poison: false,
});
for id in &entity_ids {
let entity = runtime.get_entity(&token, *id).await.unwrap();
singles
.entities(&token)
.unwrap()
.upsert_entity(entity.clone())
.await
.unwrap();
singles.reindex_entity(&token, &entity).await.unwrap();
}
for id in ¬e_ids {
let note = runtime
.notes(&token)
.unwrap()
.get_note(*id)
.await
.unwrap()
.unwrap();
singles
.notes(&token)
.unwrap()
.upsert_note(note.clone())
.await
.unwrap();
singles.reindex_note(&token, ¬e).await.unwrap();
}
assert_eq!(single_calls.load(Ordering::SeqCst), 260);
let batched_entities = runtime
.vectors_for_model(&token, BACKFILL_BATCH_MODEL)
.unwrap()
.get_vectors(&entity_ids, "local", "entity.body")
.await
.unwrap();
let single_entities = singles
.vectors_for_model(&token, BACKFILL_BATCH_MODEL)
.unwrap()
.get_vectors(&entity_ids, "local", "entity.body")
.await
.unwrap();
assert_eq!(batched_entities.len(), 257);
assert_eq!(batched_entities, single_entities);
let batched_notes = runtime
.vectors_for_model(&token, BACKFILL_BATCH_MODEL)
.unwrap()
.get_vectors(¬e_ids, "local", "note.content")
.await
.unwrap();
let single_notes = singles
.vectors_for_model(&token, BACKFILL_BATCH_MODEL)
.unwrap()
.get_vectors(¬e_ids, "local", "note.content")
.await
.unwrap();
assert_eq!(batched_notes.len(), 3);
assert_eq!(batched_notes, single_notes);
}
#[tokio::test]
async fn backfill_failed_pages_retry_singly_without_duplicate_index_rows() {
use khive_storage::types::{SqlStatement, SqlValue};
let token = NamespaceToken::local();
let runtime = KhiveRuntime::memory().unwrap();
for name in ["good one", "poison", "good two"] {
runtime
.create_entity(&token, "concept", None, name, Some("body"), None, vec![])
.await
.unwrap();
runtime
.create_note(&token, "observation", None, name, None, None, vec![])
.await
.unwrap();
}
let calls = Arc::new(AtomicUsize::new(0));
runtime.register_embedder(CountingBackfillProvider {
calls: Arc::clone(&calls),
reject_poison: true,
});
let backfilled = runtime.backfill_missing_embeddings(&token).await.unwrap();
assert_eq!(backfilled, 4);
assert_eq!(
calls.load(Ordering::SeqCst),
8,
"two failed pages plus six singleton retries"
);
let mut reader = runtime.sql().reader().await.unwrap();
for (table, expected) in [("ann_write_log", 4), ("vector_provenance", 0)] {
let count = reader
.query_scalar(SqlStatement {
sql: format!("SELECT COUNT(*) FROM {table} WHERE namespace = ?1"),
params: vec![SqlValue::Text("local".into())],
label: Some("backfill-batch-no-duplicate-rows".into()),
})
.await
.unwrap();
assert!(
matches!(count, Some(SqlValue::Integer(value)) if value == expected),
"{table} must match the guarded per-record writer"
);
}
assert_eq!(
runtime.backfill_missing_embeddings(&token).await.unwrap(),
0
);
}
}