bobbin-ai 0.25.2

Local-first context injection engine for AI coding agents
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
use anyhow::{Context, Result};
use arrow::array::{
    Array, ArrayRef, BooleanArray, FixedSizeListArray, Float32Array, RecordBatch,
    RecordBatchIterator, StringArray, UInt32Array,
};
use arrow::datatypes::{DataType, Field, FieldRef, Schema, SchemaRef};
use futures::TryStreamExt;
use lance_index::scalar::FullTextSearchQuery;
use lancedb::query::{ExecutableQuery, QueryBase};
use lancedb::table::{CompactionOptions, Duration, OptimizeAction};
use lancedb::{connect, Connection, Table};
use std::future::Future;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;

use crate::types::{
    Chunk, ChunkEdge, ChunkEdgeType, ChunkType, Entity, EntitySearchResult, FileMetadata,
    ImportDependency, IndexStats, LanguageStats, MatchType, SearchResult,
};

/// Table name for chunk storage
const TABLE_NAME: &str = "chunks";

/// Table name for dependency storage
const DEPS_TABLE_NAME: &str = "dependencies";

/// Table name for chunk-level relationship edges
const CHUNK_EDGES_TABLE_NAME: &str = "chunk_edges";

/// Table name for knowledge graph entity embeddings
const ENTITIES_TABLE_NAME: &str = "entities";

/// Limit for queries that need all rows. LanceDB 0.17 defaults to limit=10
/// (DEFAULT_TOP_K) for all queries including plain scans, so we must set an
/// explicit large limit when scanning the full table. Using i64::MAX is safe
/// because the lance scanner casts to i64 internally.
const SCAN_ALL_LIMIT: usize = i64::MAX as usize;

/// Default embedding dimension (for backward compatibility)
const DEFAULT_EMBEDDING_DIM: i32 = 384;

const FTS_RECOVERY_DELAYS_MS: [u64; 3] = [50, 100, 200];

async fn recover_fts_query<T, Q, QF, R, RF, S, SF>(
    mut query: Q,
    mut rebuild: R,
    mut sleep: S,
) -> Result<T>
where
    Q: FnMut() -> QF,
    QF: Future<Output = Result<T>>,
    R: FnMut() -> RF,
    RF: Future<Output = Result<()>>,
    S: FnMut(u64) -> SF,
    SF: Future<Output = ()>,
{
    let first_err = match query().await {
        Ok(value) => return Ok(value),
        Err(error) => error,
    };

    for delay_ms in FTS_RECOVERY_DELAYS_MS {
        // The invalidation may be coming from another process. Waiting before
        // rebuilding is what gives that writer a chance to finish; sleeping
        // after our own failed cycle leaves the first retry just as racy as it
        // was before bounded recovery existed.
        sleep(delay_ms).await;
        if let Err(error) = rebuild().await {
            tracing::warn!(error = %error, delay_ms, "FTS index rebuild attempt failed");
            continue;
        }
        match query().await {
            Ok(value) => return Ok(value),
            Err(error) => tracing::warn!(
                error = %error,
                delay_ms,
                "FTS query still unavailable after index rebuild"
            ),
        }
    }

    Err(first_err).context("Failed to collect FTS results after bounded index recovery")
}

/// Target rows per compacted fragment.
///
/// `CompactionOptions::default()` uses 1,048,576. That default is the whole
/// bug for a store our size: compaction treats every fragment with FEWER rows
/// than this as a candidate, so on a ~152k-row table *every* fragment always
/// qualifies and the target is effectively "rewrite the entire store into one
/// fragment". Peak memory is therefore store-scale BY CONSTRUCTION — it grows
/// with the corpus and never with the amount of new work — which is why the
/// nightly reindex was OOM-killed at a 16G cap, and still at 8G after the
/// store had shrunk to 3.3G. Bounding this makes compaction rewrite a bounded
/// slice at a time instead.
const COMPACT_TARGET_ROWS_PER_FRAGMENT: usize = 65_536;

/// Scanner batch size while reading input fragments during compaction.
/// Unset, lance uses a default tuned for throughput; our rows carry full chunk
/// TEXT (the 3.3G store is ~230MB of vectors and the rest text), so in-flight
/// batches dominate compaction memory.
const COMPACT_SCAN_BATCH_SIZE: usize = 1_024;

/// Compaction options with an explicit memory bound.
///
/// Every knob here exists to keep peak RSS a function of the BATCH, not of the
/// corpus. See [`COMPACT_TARGET_ROWS_PER_FRAGMENT`] for why the library
/// defaults cannot be used on a store of this shape.
fn bounded_compaction_options() -> CompactionOptions {
    CompactionOptions {
        target_rows_per_fragment: COMPACT_TARGET_ROWS_PER_FRAGMENT,
        batch_size: Some(COMPACT_SCAN_BATCH_SIZE),
        // One compaction task at a time. The default is the compute-CPU count,
        // which multiplies peak memory by that factor; on a host shared with
        // other memory-hungry services that multiplier is what turns a large
        // compaction into an OOM of the whole unit.
        num_threads: Some(1),
        ..CompactionOptions::default()
    }
}

/// Cross-process record of the last COMPLETED maintenance sweep, written into
/// the dataset directory beside `.maintenance.lock`.
///
/// It exists because the read-path throttle it feeds was per-PROCESS, and the
/// heaviest contender for the maintenance lock is a short-lived CLI: every
/// `bobbin status` is a fresh process whose in-memory "last compacted" is
/// "never", so every one of them took the store-wide lock. See
/// [`VectorStore::compact_if_stale`].
const MAINTENANCE_STATUS_FILE: &str = ".maintenance.json";

/// How long the read path lets a compaction stay deferred. Shared by the
/// in-process throttle and the cross-process one, so a `bobbin status` storm
/// and a long-lived server obey the same budget.
const READ_PATH_COMPACT_INTERVAL_SECS: u64 = 300;

/// How often to re-try the maintenance lock while waiting for it.
const LOCK_POLL_INTERVAL: std::time::Duration = std::time::Duration::from_millis(500);

/// How a caller wants to acquire the store-wide maintenance lock.
///
/// The distinction is the whole point of this type: the two kinds of caller
/// have opposite correct behaviour, and collapsing them is what let a routine
/// `bobbin status` starve the only job that prunes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LockWait {
    /// **Opportunistic** — try once, give up immediately if another
    /// participant holds the lock. Correct for the READ path: a stats query
    /// compacts as a courtesy, and queueing behind someone else's sweep would
    /// make every request pay for it.
    NoWait,
    /// **Scheduled** — poll for the lock up to this long before giving up.
    /// Correct for the reindex/watch maintenance step, which is the ONLY thing
    /// that prunes: it must not be pre-empted by an opportunistic contender.
    UpTo(std::time::Duration),
}

/// What a maintenance sweep actually DID.
///
/// `compact`/`prune` used to return `Ok(())` whether they swept every table or
/// skipped entirely because the lock was held, so a starved nightly was
/// indistinguishable from a successful one — for months. A caller that cannot
/// tell the difference cannot report it, and nothing did.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MaintenanceOutcome {
    /// The lock was held by us and every table was swept.
    Ran,
    /// Another participant held the maintenance lock; NOTHING was reclaimed.
    SkippedLockHeld { waited: std::time::Duration },
    /// The store has no tables open — nothing to sweep.
    NoTables,
}

impl MaintenanceOutcome {
    /// True only when the sweep actually touched the store.
    pub fn ran(self) -> bool {
        matches!(self, Self::Ran)
    }

    /// True when the sweep was starved by a lock contender — the case worth
    /// reporting, because the caller's exit status will not show it.
    pub fn skipped_lock_held(self) -> bool {
        matches!(self, Self::SkippedLockHeld { .. })
    }

    /// Stable machine-readable label for JSON output and logs.
    pub fn label(self) -> &'static str {
        match self {
            Self::Ran => "ran",
            Self::SkippedLockHeld { .. } => "skipped_lock_held",
            Self::NoTables => "no_tables",
        }
    }
}

/// Wall-clock record of the last successful maintenance, read from
/// [`MAINTENANCE_STATUS_FILE`]. Unix seconds; `None` means "never, as far as
/// this store's directory knows".
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct MaintenanceStatus {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_compact_unix: Option<u64>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub last_prune_unix: Option<u64>,
}

/// Seconds since the unix epoch, or 0 if the clock is before it.
fn now_unix() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_secs())
}

/// Extract a named string column from a RecordBatch, returning a Result instead of panicking.
fn string_column<'a>(batch: &'a RecordBatch, name: &str) -> Result<&'a StringArray> {
    batch
        .column_by_name(name)
        .with_context(|| format!("missing column '{name}' in RecordBatch"))?
        .as_any()
        .downcast_ref::<StringArray>()
        .with_context(|| format!("column '{name}' is not a StringArray"))
}

/// Unified chunk storage using LanceDB (vectors + metadata + FTS + dependencies)
pub struct VectorStore {
    conn: Connection,
    table: Option<Table>,
    /// Dependency graph table
    deps_table: Option<Table>,
    /// Chunk-level relationship edges table
    chunk_edges_table: Option<Table>,
    /// Knowledge graph entity embeddings table
    entities_table: Option<Table>,
    /// Embedding dimension used by this store
    embedding_dim: i32,
    /// Whether FTS index has been created for this session
    fts_indexed: AtomicBool,
    /// Whole-corpus FTS builds THIS store asked Lance for (the global metric is
    /// `bobbin_fts_build_attempts_total`; this one is per store, for tests).
    fts_build_attempts: AtomicU64,
    /// The dataset directory, for the cross-process maintenance lock file.
    db_path: PathBuf,
    /// Baseline for the read-path compaction throttle.
    opened_at: std::time::Instant,
    /// Seconds-since-open of the last read-path compaction attempt (0 = never).
    last_compact_secs: AtomicU64,
}

/// True for Lance commit conflicts whose own error text says to retry the latest version.
fn is_commit_conflict<E: std::fmt::Display>(err: &E) -> bool {
    let msg = err.to_string().to_ascii_lowercase();
    msg.contains("commit conflict") || msg.contains("concurrent commit")
}

/// Narrow discriminator for Lance's FTS incremental-index worker panic.
/// The join boundary is stable across Lance releases; requiring both the
/// inverted-index source path and a worker panic avoids treating ordinary
/// compaction failures as rebuildable index state.
fn is_fts_compaction_panic<E: std::fmt::Display>(err: &E) -> bool {
    let msg = format!("{err:#}");
    msg.contains("scalar/inverted/builder.rs") && msg.contains("panicked")
}

/// Retry a table write that hit a commit conflict: refresh the table handle to
/// the latest version and re-run, at most twice, with a short backoff. Every
/// participant compacts/writes the same tables, so occasional conflicts are
/// expected operation, not errors — before this, the archive-chunk write
/// failed on EVERY incremental run behind a success message.
macro_rules! retry_on_conflict {
    ($table:expr, $op:expr) => {{
        let mut attempt: u32 = 0;
        loop {
            match $op.await {
                Ok(v) => break Ok(v),
                Err(e) if attempt < 2 && is_commit_conflict(&e) => {
                    attempt += 1;
                    let _ = $table.checkout_latest().await;
                    tokio::time::sleep(std::time::Duration::from_millis(100 * u64::from(attempt)))
                        .await;
                }
                Err(e) => break Err(e),
            }
        }
    }};
}

impl VectorStore {
    /// Open or create a vector store at the given path
    pub async fn open(path: &Path) -> Result<Self> {
        Self::open_with_dim(path, DEFAULT_EMBEDDING_DIM).await
    }

    /// Open or create a vector store with a specific embedding dimension
    pub async fn open_with_dim(path: &Path, embedding_dim: i32) -> Result<Self> {
        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("Failed to create directory: {}", parent.display()))?;
        }

        let path_str = path.to_str().context("non-UTF8 path for LanceDB")?;
        let conn = connect(path_str)
            .execute()
            .await
            .with_context(|| format!("Failed to open LanceDB at: {}", path.display()))?;

        // Check if table exists
        let tables = conn
            .table_names()
            .execute()
            .await
            .context("Failed to list tables")?;

        let table = if tables.contains(&TABLE_NAME.to_string()) {
            let t = conn
                .open_table(TABLE_NAME)
                .execute()
                .await
                .context("Failed to open chunks table")?;

            // Schema migration: if the on-disk schema doesn't match the
            // expected schema (e.g. a new column was added), drop the table
            // so the next insert recreates it with the correct schema.
            let table_schema = t
                .schema()
                .await
                .context("Failed to read chunks table schema")?;
            let expected = Self::build_schema(embedding_dim);
            let on_disk: Vec<&str> = table_schema
                .fields()
                .iter()
                .map(|f| f.name().as_str())
                .collect();
            let expected_fields: Vec<&str> = expected
                .fields()
                .iter()
                .map(|f| f.name().as_str())
                .collect();

            if on_disk != expected_fields {
                eprintln!("bobbin: chunks table schema changed — dropping for re-creation");
                eprintln!("  on-disk fields:  {:?}", on_disk);
                eprintln!("  expected fields: {:?}", expected_fields);
                conn.drop_table(TABLE_NAME, &[])
                    .await
                    .context("Failed to drop outdated chunks table")?;
                None
            } else {
                Some(t)
            }
        } else {
            None
        };

        let deps_table = if tables.contains(&DEPS_TABLE_NAME.to_string()) {
            let t = conn
                .open_table(DEPS_TABLE_NAME)
                .execute()
                .await
                .context("Failed to open dependencies table")?;

            // Same schema migration check for dependencies table
            let table_schema = t
                .schema()
                .await
                .context("Failed to read deps table schema")?;
            let expected = Self::deps_schema();
            let on_disk: Vec<&str> = table_schema
                .fields()
                .iter()
                .map(|f| f.name().as_str())
                .collect();
            let expected_fields: Vec<&str> = expected
                .fields()
                .iter()
                .map(|f| f.name().as_str())
                .collect();

            if on_disk != expected_fields {
                eprintln!("bobbin: deps table schema changed — dropping for re-creation");
                conn.drop_table(DEPS_TABLE_NAME, &[])
                    .await
                    .context("Failed to drop outdated deps table")?;
                None
            } else {
                Some(t)
            }
        } else {
            None
        };

        let chunk_edges_table = if tables.contains(&CHUNK_EDGES_TABLE_NAME.to_string()) {
            let t = conn
                .open_table(CHUNK_EDGES_TABLE_NAME)
                .execute()
                .await
                .context("Failed to open chunk_edges table")?;

            let table_schema = t
                .schema()
                .await
                .context("Failed to read chunk_edges table schema")?;
            let expected = Self::chunk_edges_schema();
            let on_disk: Vec<&str> = table_schema
                .fields()
                .iter()
                .map(|f| f.name().as_str())
                .collect();
            let expected_fields: Vec<&str> = expected
                .fields()
                .iter()
                .map(|f| f.name().as_str())
                .collect();

            if on_disk != expected_fields {
                eprintln!("bobbin: chunk_edges table schema changed — dropping for re-creation");
                conn.drop_table(CHUNK_EDGES_TABLE_NAME, &[])
                    .await
                    .context("Failed to drop outdated chunk_edges table")?;
                None
            } else {
                Some(t)
            }
        } else {
            None
        };

        let entities_table = if tables.contains(&ENTITIES_TABLE_NAME.to_string()) {
            let t = conn
                .open_table(ENTITIES_TABLE_NAME)
                .execute()
                .await
                .context("Failed to open entities table")?;

            let table_schema = t
                .schema()
                .await
                .context("Failed to read entities table schema")?;
            let expected = Self::entities_schema(embedding_dim);
            let on_disk: Vec<&str> = table_schema
                .fields()
                .iter()
                .map(|f| f.name().as_str())
                .collect();
            let expected_fields: Vec<&str> = expected
                .fields()
                .iter()
                .map(|f| f.name().as_str())
                .collect();

            if on_disk != expected_fields {
                eprintln!("bobbin: entities table schema changed — dropping for re-creation");
                conn.drop_table(ENTITIES_TABLE_NAME, &[])
                    .await
                    .context("Failed to drop outdated entities table")?;
                None
            } else {
                Some(t)
            }
        } else {
            None
        };

        Ok(Self {
            conn,
            table,
            deps_table,
            chunk_edges_table,
            entities_table,
            embedding_dim,
            fts_indexed: AtomicBool::new(false),
            fts_build_attempts: AtomicU64::new(0),
            db_path: path.to_path_buf(),
            opened_at: std::time::Instant::now(),
            last_compact_secs: AtomicU64::new(0),
        })
    }

    /// Get the embedding dimension of this store
    pub fn embedding_dim(&self) -> i32 {
        self.embedding_dim
    }

    /// Get the inner field for the vector FixedSizeList
    fn vector_field() -> FieldRef {
        Arc::new(Field::new("item", DataType::Float32, true))
    }

    /// Get the Arrow schema for chunk records
    fn schema(&self) -> Schema {
        Self::build_schema(self.embedding_dim)
    }

    /// Build the Arrow schema for chunk records with a given embedding dimension.
    /// Static so it can be called before `self` is fully constructed (e.g. during
    /// schema migration checks in `open_with_dim`).
    fn build_schema(embedding_dim: i32) -> Schema {
        Schema::new(vec![
            Field::new("id", DataType::Utf8, false),
            Field::new(
                "vector",
                DataType::FixedSizeList(Self::vector_field(), embedding_dim),
                false,
            ),
            Field::new("repo", DataType::Utf8, false),
            Field::new("file_path", DataType::Utf8, false),
            Field::new("file_hash", DataType::Utf8, false),
            Field::new("language", DataType::Utf8, false),
            Field::new("chunk_type", DataType::Utf8, false),
            Field::new("chunk_name", DataType::Utf8, true),
            Field::new("start_line", DataType::UInt32, false),
            Field::new("end_line", DataType::UInt32, false),
            Field::new("content", DataType::Utf8, false),
            Field::new("full_context", DataType::Utf8, true),
            Field::new("indexed_at", DataType::Utf8, false),
            Field::new("tags", DataType::Utf8, false),
        ])
    }

    /// Convert chunks and embeddings to a RecordBatch
    ///
    /// `full_contexts` contains the context-enriched text used for embedding.
    /// `None` entries mean the chunk was embedded using its content directly.
    fn to_record_batch(
        &self,
        chunks: &[Chunk],
        embeddings: &[Vec<f32>],
        full_contexts: &[Option<String>],
        repo: &str,
        file_hash: &str,
        indexed_at: &str,
    ) -> Result<RecordBatch> {
        let file_hashes: Vec<&str> = chunks.iter().map(|_| file_hash).collect();
        let indexed_ats: Vec<&str> = chunks.iter().map(|_| indexed_at).collect();
        self.to_record_batch_bulk(
            chunks,
            embeddings,
            full_contexts,
            repo,
            &file_hashes,
            &indexed_ats,
        )
    }

    fn to_record_batch_bulk(
        &self,
        chunks: &[Chunk],
        embeddings: &[Vec<f32>],
        full_contexts: &[Option<String>],
        repo: &str,
        file_hashes: &[&str],
        indexed_at_values: &[&str],
    ) -> Result<RecordBatch> {
        let schema = Arc::new(self.schema());

        let ids: Vec<&str> = chunks.iter().map(|c| c.id.as_str()).collect();
        let repos: Vec<&str> = chunks.iter().map(|_| repo).collect();
        let file_paths: Vec<&str> = chunks.iter().map(|c| c.file_path.as_str()).collect();
        let languages: Vec<&str> = chunks.iter().map(|c| c.language.as_str()).collect();
        let chunk_types: Vec<&str> = chunks
            .iter()
            .map(|c| chunk_type_to_str(&c.chunk_type))
            .collect();
        let chunk_names: Vec<Option<&str>> = chunks.iter().map(|c| c.name.as_deref()).collect();
        let start_lines: Vec<u32> = chunks.iter().map(|c| c.start_line).collect();
        let end_lines: Vec<u32> = chunks.iter().map(|c| c.end_line).collect();
        let contents: Vec<&str> = chunks.iter().map(|c| c.content.as_str()).collect();
        let full_context_refs: Vec<Option<&str>> =
            full_contexts.iter().map(|c| c.as_deref()).collect();
        let indexed_ats: Vec<&str> = indexed_at_values.to_vec();
        let tags: Vec<&str> = chunks.iter().map(|c| c.tags.as_str()).collect();

        // Flatten embeddings for FixedSizeList
        let flat_embeddings: Vec<f32> = embeddings.iter().flatten().copied().collect();
        let embedding_values: ArrayRef = Arc::new(Float32Array::from(flat_embeddings));
        let vector_array = FixedSizeListArray::try_new(
            Self::vector_field(),
            self.embedding_dim,
            embedding_values,
            None,
        )
        .context("Failed to create vector array")?;

        let columns: Vec<ArrayRef> = vec![
            Arc::new(StringArray::from(ids)),
            Arc::new(vector_array),
            Arc::new(StringArray::from(repos)),
            Arc::new(StringArray::from(file_paths)),
            Arc::new(StringArray::from(file_hashes.to_vec())),
            Arc::new(StringArray::from(languages)),
            Arc::new(StringArray::from(chunk_types)),
            Arc::new(StringArray::from(chunk_names)),
            Arc::new(UInt32Array::from(start_lines)),
            Arc::new(UInt32Array::from(end_lines)),
            Arc::new(StringArray::from(contents)),
            Arc::new(StringArray::from(full_context_refs)),
            Arc::new(StringArray::from(indexed_ats)),
            Arc::new(StringArray::from(tags)),
        ];

        RecordBatch::try_new(schema, columns).context("Failed to create record batch")
    }

    /// Create a boxed RecordBatchReader from a batch.
    ///
    /// Boxed because lancedb 0.27's `add`/`create_table` take `impl Scannable`,
    /// which is implemented for `Box<dyn RecordBatchReader + Send>` but not for
    /// a bare `RecordBatchIterator`.
    fn batch_to_reader(
        batch: RecordBatch,
        schema: SchemaRef,
    ) -> Box<dyn arrow_array::RecordBatchReader + Send> {
        Box::new(RecordBatchIterator::new(std::iter::once(Ok(batch)), schema))
    }

    /// Insert chunks with their embeddings
    ///
    /// `full_contexts` contains the context-enriched text used for embedding.
    /// `None` entries mean the chunk was embedded using its content directly.
    pub async fn insert(
        &mut self,
        chunks: &[Chunk],
        embeddings: &[Vec<f32>],
        full_contexts: &[Option<String>],
        repo: &str,
        file_hash: &str,
        indexed_at: &str,
    ) -> Result<()> {
        if chunks.is_empty() {
            return Ok(());
        }

        if chunks.len() != embeddings.len() {
            anyhow::bail!(
                "Chunks and embeddings must have same length: {} vs {}",
                chunks.len(),
                embeddings.len()
            );
        }

        if chunks.len() != full_contexts.len() {
            anyhow::bail!(
                "Chunks and full_contexts must have same length: {} vs {}",
                chunks.len(),
                full_contexts.len()
            );
        }

        let schema = Arc::new(self.schema());
        let batch = self.to_record_batch(
            chunks,
            embeddings,
            full_contexts,
            repo,
            file_hash,
            indexed_at,
        )?;

        match &self.table {
            Some(table) => {
                // Delete existing records with same IDs first (upsert behavior)
                let ids: Vec<String> = chunks.iter().map(|c| c.id.clone()).collect();
                self.delete(&ids).await?;

                // Add new records (fresh reader per attempt — the reader is
                // consumed by a failed add, and RecordBatch clones are Arc-cheap)
                retry_on_conflict!(
                    table,
                    table
                        .add(Self::batch_to_reader(batch.clone(), schema.clone()))
                        .execute()
                )
                .context("Failed to add chunks")?;
            }
            None => {
                // Create table with initial data
                let reader = Self::batch_to_reader(batch, schema);
                let table = self
                    .conn
                    .create_table(TABLE_NAME, reader)
                    .execute()
                    .await
                    .context("Failed to create chunks table")?;
                self.table = Some(table);
            }
        }

        // Invalidate FTS index since data changed
        self.fts_indexed.store(false, Ordering::Relaxed);

        Ok(())
    }

    /// Bulk insert chunks from multiple files in a single Lance write.
    ///
    /// Unlike `insert`, this accepts per-chunk file hashes to support mixed-file
    /// batches. Callers must pre-delete stale rows (e.g. via `delete_by_file`).
    pub async fn insert_bulk(
        &mut self,
        chunks: &[Chunk],
        embeddings: &[Vec<f32>],
        full_contexts: &[Option<String>],
        repo: &str,
        file_hashes: &[&str],
        indexed_at_values: &[&str],
    ) -> Result<()> {
        if chunks.is_empty() {
            return Ok(());
        }

        if chunks.len() != embeddings.len()
            || chunks.len() != full_contexts.len()
            || chunks.len() != file_hashes.len()
            || chunks.len() != indexed_at_values.len()
        {
            anyhow::bail!(
                "insert_bulk: length mismatch — chunks={}, embeddings={}, contexts={}, hashes={}, indexed_at={}",
                chunks.len(),
                embeddings.len(),
                full_contexts.len(),
                file_hashes.len(),
                indexed_at_values.len()
            );
        }

        let schema = Arc::new(self.schema());
        let batch = self.to_record_batch_bulk(
            chunks,
            embeddings,
            full_contexts,
            repo,
            file_hashes,
            indexed_at_values,
        )?;

        match &self.table {
            Some(table) => {
                retry_on_conflict!(
                    table,
                    table
                        .add(Self::batch_to_reader(batch.clone(), schema.clone()))
                        .execute()
                )
                .context("Failed to bulk-add chunks")?;
            }
            None => {
                let reader = Self::batch_to_reader(batch, schema);
                let table = self
                    .conn
                    .create_table(TABLE_NAME, reader)
                    .execute()
                    .await
                    .context("Failed to create chunks table")?;
                self.table = Some(table);
            }
        }

        self.fts_indexed.store(false, Ordering::Relaxed);
        Ok(())
    }

    /// Try to take the cross-process maintenance lock (advisory flock on a
    /// file inside the dataset directory). `None` means another bobbin
    /// participant — the server, the indexer, a CLI run — is already
    /// compacting/pruning this store, and the correct move is to SKIP:
    /// a second concurrent compaction contributes nothing but the commit
    /// conflict that interrupts the first, refragments the table, and makes
    /// the next compaction longer — the positive feedback loop that took the
    /// search server down and grew the store 2.9G -> 41G.
    /// The lock releases when the returned handle drops.
    fn try_maintenance_lock(&self) -> Option<std::fs::File> {
        use fs4::FileExt;
        let lock_path = self.db_path.join(".maintenance.lock");
        let file = std::fs::OpenOptions::new()
            .create(true)
            .truncate(false)
            .write(true)
            .open(&lock_path)
            .ok()?;
        match file.try_lock_exclusive() {
            Ok(()) => Some(file),
            Err(_) => None,
        }
    }

    /// Acquire the maintenance lock according to `wait`.
    ///
    /// `Err(waited)` means we gave up; `waited` is how long we spent trying,
    /// which is the number a caller needs in order to say something truthful
    /// about the skip.
    ///
    /// The wait is a POLL, not a blocking `flock`, for two reasons: it stays
    /// bounded (a blocking acquire behind a wedged holder is the two-day-hang
    /// failure mode this fleet already has scar tissue for), and it yields to
    /// the async runtime instead of parking a worker thread.
    async fn acquire_maintenance_lock(
        &self,
        wait: LockWait,
    ) -> std::result::Result<std::fs::File, std::time::Duration> {
        let started = std::time::Instant::now();
        let budget = match wait {
            LockWait::NoWait => std::time::Duration::ZERO,
            LockWait::UpTo(d) => d,
        };
        loop {
            if let Some(file) = self.try_maintenance_lock() {
                return Ok(file);
            }
            let waited = started.elapsed();
            if waited >= budget {
                return Err(waited);
            }
            // Never sleep past the budget — a caller that asked for 60s must
            // not be held for 60.5s.
            let remaining = budget - waited;
            tokio::time::sleep(LOCK_POLL_INTERVAL.min(remaining)).await;
        }
    }

    /// Path of the cross-process maintenance status record.
    fn maintenance_status_path(&self) -> PathBuf {
        self.db_path.join(MAINTENANCE_STATUS_FILE)
    }

    /// Last completed maintenance for this store, as recorded on disk by
    /// whichever process last swept it.
    ///
    /// This is the operator-facing signal: "time since last successful
    /// maintenance" is alertable, and unlike a caller's exit status it does not
    /// go green when the sweep was skipped. Best-effort — a missing or
    /// unreadable file reads as "never".
    pub fn maintenance_status(&self) -> MaintenanceStatus {
        std::fs::read_to_string(self.maintenance_status_path())
            .ok()
            .and_then(|s| serde_json::from_str(&s).ok())
            .unwrap_or_default()
    }

    /// Stamp a completed sweep into the status record.
    ///
    /// Only ever called while HOLDING the maintenance lock, which is what makes
    /// the read-modify-write safe across processes without a second lock.
    /// Best-effort: failing to record maintenance must not fail maintenance.
    fn record_maintenance(&self, compacted: bool, pruned: bool) {
        let mut status = self.maintenance_status();
        let now = now_unix();
        if compacted {
            status.last_compact_unix = Some(now);
        }
        if pruned {
            status.last_prune_unix = Some(now);
        }
        if let Ok(json) = serde_json::to_string(&status) {
            let _ = std::fs::write(self.maintenance_status_path(), json);
        }
    }

    /// Compact fragmented data files.
    ///
    /// LanceDB creates a new fragment for every add/delete. With per-file upserts
    /// this leads to heavy fragmentation that hurts read performance. Compaction
    /// merges small fragments into larger ones.
    ///
    /// Memory is bounded by [`bounded_compaction_options`] — with
    /// `CompactionOptions::default()` this OOM-killed the nightly reindex.
    ///
    /// Gated on the maintenance lock: at most ONE compaction runs at a time
    /// across every process sharing the store; contenders skip (Ok) rather
    /// than queue — compaction is best-effort maintenance, and an interrupted
    /// compaction is strictly worse than a deferred one.
    /// Every table this store owns, for maintenance sweeps.
    ///
    /// `compact`/`prune` used to touch ONLY `self.table`, so `dependencies`,
    /// `chunk_edges` and `entities` were never compacted and never pruned — not
    /// once, ever. Their version manifests therefore grew monotonically for the
    /// life of the store: `dependencies` was measured at 280,895 manifest
    /// versions while `chunks`, which does get pruned, sat around 4,000. Any new
    /// table MUST be added here or it silently inherits that unbounded growth.
    fn maintenance_tables(&self) -> Vec<(&'static str, &Table)> {
        [
            ("chunks", self.table.as_ref()),
            ("dependencies", self.deps_table.as_ref()),
            ("chunk_edges", self.chunk_edges_table.as_ref()),
            ("entities", self.entities_table.as_ref()),
        ]
        .into_iter()
        .filter_map(|(name, t)| t.map(|t| (name, t)))
        .collect()
    }

    pub async fn compact(&self, wait: LockWait) -> Result<MaintenanceOutcome> {
        let tables = self.maintenance_tables();
        if tables.is_empty() {
            return Ok(MaintenanceOutcome::NoTables);
        }

        let _lock = match self.acquire_maintenance_lock(wait).await {
            Ok(lock) => lock,
            Err(waited) => return Ok(MaintenanceOutcome::SkippedLockHeld { waited }),
        };

        let r = self.compact_locked(&tables).await;
        // Stamped while still holding the lock, and only for a sweep that
        // actually ran — a skip must never look like maintenance.
        self.record_maintenance(true, false);
        r.map(|()| MaintenanceOutcome::Ran)
    }

    /// The full SCHEDULED sweep — prune, then compact — under a SINGLE
    /// acquisition of the maintenance lock.
    ///
    /// Two reasons this is not just `prune().await; compact().await`:
    ///
    /// 1. **One wait, not two.** The nightly runs one `index` per repo (27 of
    ///    them here), and each waits for the lock. Charging each of prune and
    ///    compact its own full wait budget doubles the worst case for every
    ///    repo, and the second wait is nearly always redundant — a contender
    ///    that held the lock through the first will usually hold it through the
    ///    second.
    /// 2. **No steal window.** Between two separate acquisitions an
    ///    opportunistic contender can take the lock, so the run could prune and
    ///    then silently fail to compact. Holding it across both makes the
    ///    prune-before-compact ordering actually hold end to end.
    pub async fn maintain(&self, wait: LockWait) -> Result<MaintenanceOutcome> {
        let tables = self.maintenance_tables();
        if tables.is_empty() {
            return Ok(MaintenanceOutcome::NoTables);
        }

        let _lock = match self.acquire_maintenance_lock(wait).await {
            Ok(lock) => lock,
            Err(waited) => return Ok(MaintenanceOutcome::SkippedLockHeld { waited }),
        };

        // PRUNE FIRST. Prune is the cheap reclaim (it drops version manifests
        // and unreferenced fragment files without reading data into RAM);
        // compact rewrites rows and is the one that can die on memory. With
        // compact first, a compaction that fails takes the cheap reclaim down
        // with it and the next compaction is bigger — self-perpetuating.
        let pruned = self.prune_locked(&tables).await;
        let compacted = self.compact_locked(&tables).await;
        // THEN fold rows added since each index was built into it
        // (aegis-4h7zw8). Lance never adds new rows to an existing index on its
        // own: it text-scans them on every FTS query, so without this the
        // keyword leg slows with every incremental reindex. A failure here
        // leaves search exactly as it was (slow, still correct), so it is
        // reported loudly but does not fail the sweep.
        if let Err(e) = self.optimize_indices_locked(&tables).await {
            tracing::warn!(error = %e, "index optimize failed; unindexed rows stay text-scanned");
            eprintln!("bobbin: index optimize failed (search stays correct but slower): {e:#}");
        }
        self.record_maintenance(compacted.is_ok(), pruned.is_ok());
        pruned.and(compacted).map(|()| MaintenanceOutcome::Ran)
    }

    /// Compact every table. CALLER MUST HOLD the maintenance lock.
    ///
    /// One table's failure must not skip the rest — they are independent
    /// datasets and a partial sweep still reclaims.
    async fn compact_locked(&self, tables: &[(&'static str, &Table)]) -> Result<()> {
        let mut first_err = None;
        for (name, table) in tables {
            let mut r = retry_on_conflict!(
                table,
                table.optimize(OptimizeAction::Compact {
                    options: bounded_compaction_options(),
                    remap_options: None,
                })
            )
            .with_context(|| format!("Failed to compact {name} table"));

            // Lance's incremental FTS-index remap can panic while compacting
            // the chunks table. A full replacement build is the upstream-safe
            // recovery path: it discards the broken incremental generation,
            // after which the same bounded compaction can proceed. Never apply
            // this to unrelated compaction errors (I/O, schema, OOM, etc.).
            if *name == "chunks" && r.as_ref().err().is_some_and(|e| is_fts_compaction_panic(e)) {
                tracing::warn!(
                    error = %r.as_ref().expect_err("checked above"),
                    "FTS incremental compaction panicked; rebuilding the FTS index and retrying once"
                );
                if let Err(rebuild_err) = self.rebuild_fts_index().await {
                    r = Err(rebuild_err
                        .context("Failed to rebuild FTS index after incremental compaction panic"));
                } else {
                    r = retry_on_conflict!(
                        table,
                        table.optimize(OptimizeAction::Compact {
                            options: bounded_compaction_options(),
                            remap_options: None,
                        })
                    )
                    .with_context(|| "Failed to compact chunks table after FTS rebuild");
                }
            }
            if let Err(e) = r {
                first_err.get_or_insert(e);
            }
        }
        first_err.map_or(Ok(()), Err)
    }

    /// Prune every table. CALLER MUST HOLD the maintenance lock.
    async fn prune_locked(&self, tables: &[(&'static str, &Table)]) -> Result<()> {
        let mut first_err = None;
        for (name, table) in tables {
            let r = retry_on_conflict!(
                table,
                table.optimize(OptimizeAction::Prune {
                    older_than: Some(Duration::try_hours(1).expect("valid delta")),
                    delete_unverified: Some(true),
                    error_if_tagged_old_versions: None,
                })
            )
            .with_context(|| format!("Failed to prune old versions of {name} table"));
            if let Err(e) = r {
                first_err.get_or_insert(e);
            }
        }
        first_err.map_or(Ok(()), Err)
    }

    /// Remove old dataset versions to reclaim disk space.
    /// Removes versions older than 1 hour by default.
    /// Gated on the maintenance lock exactly like [`Self::compact`].
    pub async fn prune(&self, wait: LockWait) -> Result<MaintenanceOutcome> {
        let tables = self.maintenance_tables();
        if tables.is_empty() {
            return Ok(MaintenanceOutcome::NoTables);
        }

        let _lock = match self.acquire_maintenance_lock(wait).await {
            Ok(lock) => lock,
            Err(waited) => return Ok(MaintenanceOutcome::SkippedLockHeld { waited }),
        };

        let r = self.prune_locked(&tables).await;
        self.record_maintenance(false, true);
        r.map(|()| MaintenanceOutcome::Ran)
    }

    /// Read-path compaction throttle: attempt a compaction at most once per
    /// interval per process. `get_stats` is called by every search / grep /
    /// status surface, and compacting on each call meant a long compaction on
    /// a fragmented table ran inline in EVERY request while other
    /// participants interrupted it. Between attempts, a slightly
    /// stale scan beats an unavailable server.
    /// The read path is also throttled ACROSS processes, because the in-process
    /// throttle below cannot see the contender that actually mattered.
    ///
    /// `bobbin status` is a fresh process on every invocation, so its
    /// `last_compact_secs` is always the "never" sentinel and it always fell
    /// through to a lock attempt. The incremental service runs `bobbin status`
    /// on every cycle, so the store-wide maintenance lock was being taken by
    /// short-lived readers on a schedule — and the nightly, the only job that
    /// prunes, skipped in silence when it lost the race. Measured: a supervised
    /// reindex that lost the lock reclaimed nothing (dependency versions went
    /// UP, 280,899 -> 280,971); the identical command with the contender
    /// stopped took dependencies 3.2G -> 117M.
    ///
    /// Reading the shared record first means a status call that another
    /// participant already compacted for does not touch the lock at all.
    async fn compact_if_stale(&self) {
        let now_wall = now_unix();
        if let Some(last_wall) = self.maintenance_status().last_compact_unix {
            if now_wall.saturating_sub(last_wall) < READ_PATH_COMPACT_INTERVAL_SECS {
                return;
            }
        }
        // Clamped to >= 1 so a recorded value is never the "never" sentinel 0.
        let now = self.opened_at.elapsed().as_secs().max(1);
        let last = self.last_compact_secs.load(Ordering::Relaxed);
        if last != 0 && now.saturating_sub(last) < READ_PATH_COMPACT_INTERVAL_SECS {
            return;
        }
        // Record the attempt whether it ran or was skipped (lock held
        // elsewhere): either way, hammering the lock on every request is the
        // behavior this throttle exists to stop.
        self.last_compact_secs.store(now, Ordering::Relaxed);
        // NoWait is load-bearing: the read path is opportunistic and must yield
        // to scheduled maintenance, never queue in front of it.
        self.compact(LockWait::NoWait).await.ok();
    }

    /// Search for similar vectors using approximate nearest neighbor search.
    /// Optionally filter by repo name and/or an additional SQL WHERE clause.
    pub async fn search(
        &self,
        query_embedding: &[f32],
        limit: usize,
        repo: Option<&str>,
    ) -> Result<Vec<SearchResult>> {
        self.search_filtered(query_embedding, limit, repo, None)
            .await
    }

    /// Search with an additional SQL filter (e.g., "language IN ('hla', 'pensieve')").
    pub async fn search_filtered(
        &self,
        query_embedding: &[f32],
        limit: usize,
        repo: Option<&str>,
        filter: Option<&str>,
    ) -> Result<Vec<SearchResult>> {
        if limit == 0 {
            return Ok(vec![]);
        }

        let table = match &self.table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let mut query = table
            .vector_search(query_embedding.to_vec())
            .context("Failed to create vector search")?;

        let combined = Self::combine_filters(repo, filter);
        if let Some(ref f) = combined {
            query = query.only_if(f.clone());
        }

        let results = match query.limit(limit).execute().await {
            Ok(r) => r,
            Err(e) => {
                // LanceDB raises "k must be positive" when a filter yields 0 matching
                // rows in the index partition. Return empty results instead of panicking.
                let msg = e.to_string();
                if msg.contains("k must be positive") || msg.contains("must be positive") {
                    return Ok(vec![]);
                }
                return Err(e).context("Failed to execute vector search");
            }
        };

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect search results")?;

        Self::batches_to_results(&batches, MatchType::Semantic)
    }

    /// Full-text search on content and chunk_name.
    /// Optionally filter by repo name.
    pub async fn search_fts(
        &self,
        query: &str,
        limit: usize,
        repo: Option<&str>,
    ) -> Result<Vec<SearchResult>> {
        self.search_fts_filtered(query, limit, repo, None).await
    }

    /// Full-text search with an additional SQL filter.
    pub async fn search_fts_filtered(
        &self,
        query: &str,
        limit: usize,
        repo: Option<&str>,
        filter: Option<&str>,
    ) -> Result<Vec<SearchResult>> {
        if limit == 0 {
            return Ok(vec![]);
        }

        // Ensure FTS index exists (must be called before borrowing self.table)
        self.ensure_fts_index().await?;

        let table = match &self.table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let combined = Self::combine_filters(repo, filter);

        // First attempt. `full_text_search` requires a valid FTS index — if the
        // index is missing or was invalidated (e.g. by compaction/prune, see
        // #16), the query fails. Rather than surface a 500, self-heal: rebuild
        // the FTS index over current rows and retry with bounded backoff. This also fixes the
        // case where ensure_fts_index() marked the index ready after a failed
        // create, and refreshes coverage of rows added since the last build.
        let batches = recover_fts_query(
            || Self::run_fts_query(table, query, limit, combined.as_deref()),
            || self.rebuild_fts_index(),
            |delay_ms| tokio::time::sleep(std::time::Duration::from_millis(delay_ms)),
        )
        .await?;
        Self::batches_to_fts_results(&batches)
    }

    /// Execute a single FTS query and collect its result batches.
    async fn run_fts_query(
        table: &lancedb::Table,
        query: &str,
        limit: usize,
        filter: Option<&str>,
    ) -> Result<Vec<RecordBatch>> {
        let mut q = table
            .query()
            .full_text_search(FullTextSearchQuery::new(query.to_string()));
        if let Some(f) = filter {
            q = q.only_if(f.to_string());
        }
        let results = q
            .limit(limit)
            .execute()
            .await
            .context("Failed to execute FTS search")?;
        results
            .try_collect()
            .await
            .context("Failed to collect FTS results")
    }

    /// Combine repo and extra filter into a single SQL WHERE clause.
    fn combine_filters(repo: Option<&str>, filter: Option<&str>) -> Option<String> {
        let repo_clause = repo.map(|r| {
            if r.contains(',') {
                // Multi-repo: repo IN ('a', 'b', 'c')
                let escaped: Vec<String> = r
                    .split(',')
                    .map(|s| format!("'{}'", s.trim().replace('\'', "''")))
                    .collect();
                format!("repo IN ({})", escaped.join(", "))
            } else {
                format!("repo = '{}'", r.replace('\'', "''"))
            }
        });
        match (repo_clause, filter) {
            (Some(r), Some(f)) => Some(format!("{} AND ({})", r, f)),
            (Some(r), None) => Some(r),
            (None, Some(f)) => Some(f.to_string()),
            (None, None) => None,
        }
    }

    /// Convert RecordBatches to SearchResults (for vector search with _distance)
    fn batches_to_results(
        batches: &[RecordBatch],
        match_type: MatchType,
    ) -> Result<Vec<SearchResult>> {
        let mut search_results = Vec::new();

        for batch in batches {
            let ids = batch
                .column_by_name("id")
                .context("Missing id column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("id column has wrong type")?;

            let file_paths = batch
                .column_by_name("file_path")
                .context("Missing file_path column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("file_path column has wrong type")?;

            let chunk_names = batch
                .column_by_name("chunk_name")
                .context("Missing chunk_name column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("chunk_name column has wrong type")?;

            let chunk_types = batch
                .column_by_name("chunk_type")
                .context("Missing chunk_type column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("chunk_type column has wrong type")?;

            let start_lines = batch
                .column_by_name("start_line")
                .context("Missing start_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("start_line column has wrong type")?;

            let end_lines = batch
                .column_by_name("end_line")
                .context("Missing end_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("end_line column has wrong type")?;

            let contents = batch
                .column_by_name("content")
                .context("Missing content column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("content column has wrong type")?;

            let languages = batch
                .column_by_name("language")
                .context("Missing language column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("language column has wrong type")?;

            // LanceDB adds _distance column for search results
            let distances = batch
                .column_by_name("_distance")
                .context("Missing _distance column")?
                .as_any()
                .downcast_ref::<Float32Array>()
                .context("_distance column has wrong type")?;

            // indexed_at is optional (may not be present in older indices)
            let indexed_ats = batch
                .column_by_name("indexed_at")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>());

            // repo is optional (present in all current indices)
            let repos = batch
                .column_by_name("repo")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>());

            // tags is optional (may not be present in older indices)
            let tags_col = batch
                .column_by_name("tags")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>());

            for i in 0..batch.num_rows() {
                let chunk = Chunk {
                    id: ids.value(i).to_string(),
                    file_path: file_paths.value(i).to_string(),
                    chunk_type: str_to_chunk_type(chunk_types.value(i)),
                    name: if chunk_names.is_null(i) {
                        None
                    } else {
                        Some(chunk_names.value(i).to_string())
                    },
                    start_line: start_lines.value(i),
                    end_line: end_lines.value(i),
                    content: contents.value(i).to_string(),
                    language: languages.value(i).to_string(),
                    tags: tags_col
                        .map(|arr| arr.value(i).to_string())
                        .unwrap_or_default(),
                };

                let distance = distances.value(i);
                let score = 1.0 / (1.0 + distance);

                let indexed_at = indexed_ats.and_then(|arr| arr.value(i).parse::<i64>().ok());

                let repo = repos.map(|r| r.value(i).to_string());

                search_results.push(SearchResult {
                    chunk,
                    score,
                    match_type: Some(match_type),
                    indexed_at,
                    repo,
                });
            }
        }

        Ok(search_results)
    }

    /// Convert RecordBatches to SearchResults (for FTS, using _score)
    fn batches_to_fts_results(batches: &[RecordBatch]) -> Result<Vec<SearchResult>> {
        let mut search_results = Vec::new();

        for batch in batches {
            let ids = batch
                .column_by_name("id")
                .context("Missing id column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("id column has wrong type")?;

            let file_paths = batch
                .column_by_name("file_path")
                .context("Missing file_path column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("file_path column has wrong type")?;

            let chunk_names = batch
                .column_by_name("chunk_name")
                .context("Missing chunk_name column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("chunk_name column has wrong type")?;

            let chunk_types = batch
                .column_by_name("chunk_type")
                .context("Missing chunk_type column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("chunk_type column has wrong type")?;

            let start_lines = batch
                .column_by_name("start_line")
                .context("Missing start_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("start_line column has wrong type")?;

            let end_lines = batch
                .column_by_name("end_line")
                .context("Missing end_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("end_line column has wrong type")?;

            let contents = batch
                .column_by_name("content")
                .context("Missing content column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("content column has wrong type")?;

            let languages = batch
                .column_by_name("language")
                .context("Missing language column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("language column has wrong type")?;

            // FTS returns _score (BM25 relevance score)
            let scores = batch
                .column_by_name("_score")
                .and_then(|c| c.as_any().downcast_ref::<Float32Array>());

            // indexed_at is optional (may not be present in older indices)
            let indexed_ats = batch
                .column_by_name("indexed_at")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>());

            // repo is optional (present in all current indices)
            let repos = batch
                .column_by_name("repo")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>());

            // tags is optional (may not be present in older indices)
            let tags_col = batch
                .column_by_name("tags")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>());

            for i in 0..batch.num_rows() {
                let chunk = Chunk {
                    id: ids.value(i).to_string(),
                    file_path: file_paths.value(i).to_string(),
                    chunk_type: str_to_chunk_type(chunk_types.value(i)),
                    name: if chunk_names.is_null(i) {
                        None
                    } else {
                        Some(chunk_names.value(i).to_string())
                    },
                    start_line: start_lines.value(i),
                    end_line: end_lines.value(i),
                    content: contents.value(i).to_string(),
                    language: languages.value(i).to_string(),
                    tags: tags_col
                        .map(|arr| arr.value(i).to_string())
                        .unwrap_or_default(),
                };

                let score = scores.map(|s| s.value(i)).unwrap_or(1.0);

                let indexed_at = indexed_ats.and_then(|arr| arr.value(i).parse::<i64>().ok());

                let repo = repos.map(|r| r.value(i).to_string());

                search_results.push(SearchResult {
                    chunk,
                    score,
                    match_type: Some(MatchType::Keyword),
                    indexed_at,
                    repo,
                });
            }
        }

        Ok(search_results)
    }

    /// Get the stored embedding vector for a chunk by its ID
    pub async fn get_chunk_embedding(&self, chunk_id: &str) -> Result<Option<Vec<f32>>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(None),
        };

        let filter = format!("id = '{}'", chunk_id.replace('\'', "''"));

        let results = table
            .query()
            .only_if(filter)
            .select(lancedb::query::Select::Columns(vec!["vector".to_string()]))
            .limit(1)
            .execute()
            .await
            .context("Failed to query chunk embedding")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect chunk embedding")?;

        for batch in &batches {
            if batch.num_rows() == 0 {
                continue;
            }

            let vectors = batch
                .column_by_name("vector")
                .context("Missing vector column")?
                .as_any()
                .downcast_ref::<FixedSizeListArray>()
                .context("vector column has wrong type")?;

            let value_arr = vectors.value(0);
            let values = value_arr
                .as_any()
                .downcast_ref::<Float32Array>()
                .context("vector values have wrong type")?;

            return Ok(Some(values.values().to_vec()));
        }

        Ok(None)
    }

    /// Get a single chunk by its ID
    pub async fn get_chunk_by_id(&self, chunk_id: &str) -> Result<Option<Chunk>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(None),
        };

        let filter = format!("id = '{}'", chunk_id.replace('\'', "''"));

        let results = table
            .query()
            .only_if(filter)
            .execute()
            .await
            .context("Failed to query chunk by ID")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect chunk by ID")?;

        for batch in &batches {
            if batch.num_rows() == 0 {
                continue;
            }

            let ids = batch
                .column_by_name("id")
                .context("Missing id column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("id column has wrong type")?;
            let file_paths = batch
                .column_by_name("file_path")
                .context("Missing file_path column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("file_path column has wrong type")?;
            let chunk_names = batch
                .column_by_name("chunk_name")
                .context("Missing chunk_name column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("chunk_name column has wrong type")?;
            let chunk_types = batch
                .column_by_name("chunk_type")
                .context("Missing chunk_type column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("chunk_type column has wrong type")?;
            let start_lines = batch
                .column_by_name("start_line")
                .context("Missing start_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("start_line column has wrong type")?;
            let end_lines = batch
                .column_by_name("end_line")
                .context("Missing end_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("end_line column has wrong type")?;
            let contents = batch
                .column_by_name("content")
                .context("Missing content column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("content column has wrong type")?;
            let languages = batch
                .column_by_name("language")
                .context("Missing language column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("language column has wrong type")?;
            let tags_col = batch
                .column_by_name("tags")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>());

            return Ok(Some(Chunk {
                id: ids.value(0).to_string(),
                file_path: file_paths.value(0).to_string(),
                chunk_type: str_to_chunk_type(chunk_types.value(0)),
                name: if chunk_names.is_null(0) {
                    None
                } else {
                    Some(chunk_names.value(0).to_string())
                },
                start_line: start_lines.value(0),
                end_line: end_lines.value(0),
                content: contents.value(0).to_string(),
                language: languages.value(0).to_string(),
                tags: tags_col
                    .map(|arr| arr.value(0).to_string())
                    .unwrap_or_default(),
            }));
        }

        Ok(None)
    }

    /// Delete vectors by chunk IDs
    pub async fn delete(&self, chunk_ids: &[String]) -> Result<()> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(()),
        };

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

        // Batch the IN clause: a single `id IN (...)` with hundreds/thousands of
        // literals overflows LanceDB/datafusion's expression handling and fails
        // ("Failed to delete chunks"). This bit large bead batches (e.g. 632
        // beads) — insert()'s upsert delete failed, so those beads never stored.
        // Chunking keeps each filter small and reliable.
        const DELETE_BATCH: usize = 100;
        for batch in chunk_ids.chunks(DELETE_BATCH) {
            let escaped_ids: Vec<String> = batch
                .iter()
                .map(|id| format!("'{}'", id.replace('\'', "''")))
                .collect();
            let filter = format!("id IN ({})", escaped_ids.join(", "));
            retry_on_conflict!(table, table.delete(&filter)).context("Failed to delete chunks")?;
        }

        Ok(())
    }

    /// Get total chunk count
    pub async fn count(&self) -> Result<u64> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(0),
        };

        let count = table
            .count_rows(None)
            .await
            .context("Failed to count chunks")?;

        Ok(count as u64)
    }

    /// Delete all chunks for files matching the given paths.
    ///
    /// `repo = Some(..)` confines the delete to that repo's rows. Paths are
    /// repo-relative, so the same path names a different file in every indexed
    /// repo — an unscoped delete for repo A's `README.md` also removed repo
    /// B's, leaving B silently unsearchable until its next reindex
    ///. `None` keeps the old any-repo behavior for callers that
    /// genuinely cannot attribute the path.
    pub async fn delete_by_file(&self, file_paths: &[String], repo: Option<&str>) -> Result<()> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(()),
        };

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

        let repo_clause = repo
            .map(|r| format!("repo = '{}' AND ", r.replace('\'', "''")))
            .unwrap_or_default();

        // Batch the IN clause (see delete() — large IN overflows the query engine).
        const DELETE_BATCH: usize = 100;
        for batch in file_paths.chunks(DELETE_BATCH) {
            let escaped_paths: Vec<String> = batch
                .iter()
                .map(|p| format!("'{}'", p.replace('\'', "''")))
                .collect();
            let filter = format!("{}file_path IN ({})", repo_clause, escaped_paths.join(", "));
            retry_on_conflict!(table, table.delete(&filter))
                .context("Failed to delete chunks by file")?;
        }

        Ok(())
    }

    /// Delete all chunks belonging to a specific repo.
    pub async fn delete_by_repo(&self, repo: &str) -> Result<()> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(()),
        };

        let filter = format!("repo = '{}'", repo.replace('\'', "''"));
        retry_on_conflict!(table, table.delete(&filter))
            .context("Failed to delete chunks by repo")?;

        Ok(())
    }

    /// Check if a file needs reindexing by comparing hash.
    ///
    /// `repo = Some(..)` scopes the check to that repo's rows: an unscoped
    /// match can find another repo's copy of the same path holding the same
    /// content and conclude "already indexed" while THIS repo has no rows at
    /// all.
    pub async fn needs_reindex(
        &self,
        file_path: &str,
        current_hash: &str,
        repo: Option<&str>,
    ) -> Result<bool> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(true), // No data yet, needs indexing
        };

        // Query for any chunk from this file and check the file_hash
        let repo_clause = repo
            .map(|r| format!("repo = '{}' AND ", r.replace('\'', "''")))
            .unwrap_or_default();
        let filter = format!(
            "{}file_path = '{}'",
            repo_clause,
            file_path.replace('\'', "''")
        );
        let results = table
            .query()
            .only_if(filter)
            .limit(1)
            .execute()
            .await
            .context("Failed to query file hash")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect file hash results")?;

        if batches.is_empty() || batches[0].num_rows() == 0 {
            return Ok(true); // File not indexed yet
        }

        let file_hashes = batches[0]
            .column_by_name("file_hash")
            .context("Missing file_hash column")?
            .as_any()
            .downcast_ref::<StringArray>()
            .context("file_hash column has wrong type")?;

        let stored_hash = file_hashes.value(0);
        Ok(stored_hash != current_hash)
    }

    /// Get all indexed file paths, optionally filtered by repo
    pub async fn get_all_file_paths(&self, repo: Option<&str>) -> Result<Vec<String>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let mut q = table
            .query()
            .select(lancedb::query::Select::Columns(vec![
                "file_path".to_string()
            ]))
            .limit(SCAN_ALL_LIMIT);

        if let Some(repo_name) = repo {
            q = q.only_if(format!("repo = '{}'", repo_name.replace('\'', "''")));
        }

        let results = q.execute().await.context("Failed to query file paths")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect file paths")?;

        let mut paths = std::collections::HashSet::new();
        for batch in &batches {
            let file_paths = batch
                .column_by_name("file_path")
                .context("Missing file_path column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("file_path column has wrong type")?;

            for i in 0..batch.num_rows() {
                paths.insert(file_paths.value(i).to_string());
            }
        }

        Ok(paths.into_iter().collect())
    }

    /// Get all unique repo names in the index
    pub async fn get_all_repos(&self) -> Result<Vec<String>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let results = table
            .query()
            .select(lancedb::query::Select::Columns(vec!["repo".to_string()]))
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query repos")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect repos")?;

        let mut repos = std::collections::HashSet::new();
        for batch in &batches {
            let repo_col = batch
                .column_by_name("repo")
                .context("Missing repo column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("repo column has wrong type")?;

            for i in 0..batch.num_rows() {
                repos.insert(repo_col.value(i).to_string());
            }
        }

        let mut repo_list: Vec<String> = repos.into_iter().collect();
        repo_list.sort();
        Ok(repo_list)
    }

    /// Get all unique languages in the index
    pub async fn get_all_languages(&self) -> Result<Vec<String>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let results = table
            .query()
            .select(lancedb::query::Select::Columns(
                vec!["language".to_string()],
            ))
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query languages")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect languages")?;

        let mut langs = std::collections::HashSet::new();
        for batch in &batches {
            let lang_col = batch
                .column_by_name("language")
                .context("Missing language column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("language column has wrong type")?;

            for i in 0..batch.num_rows() {
                let val = lang_col.value(i);
                if !val.is_empty() {
                    langs.insert(val.to_string());
                }
            }
        }

        let mut lang_list: Vec<String> = langs.into_iter().collect();
        lang_list.sort();
        Ok(lang_list)
    }

    /// Get all unique chunk types in the index
    pub async fn get_all_chunk_types(&self) -> Result<Vec<String>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let results = table
            .query()
            .select(lancedb::query::Select::Columns(vec![
                "chunk_type".to_string()
            ]))
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query chunk types")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect chunk types")?;

        let mut types = std::collections::HashSet::new();
        for batch in &batches {
            let type_col = batch
                .column_by_name("chunk_type")
                .context("Missing chunk_type column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("chunk_type column has wrong type")?;

            for i in 0..batch.num_rows() {
                let val = type_col.value(i);
                if !val.is_empty() {
                    types.insert(val.to_string());
                }
            }
        }

        let mut type_list: Vec<String> = types.into_iter().collect();
        type_list.sort();
        Ok(type_list)
    }

    /// Get file metadata from a chunk record
    pub async fn get_file(&self, file_path: &str) -> Result<Option<FileMetadata>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(None),
        };

        let filter = format!("file_path = '{}'", file_path.replace('\'', "''"));
        let results = table
            .query()
            .only_if(filter)
            .select(lancedb::query::Select::Columns(vec![
                "file_path".to_string(),
                "language".to_string(),
                "file_hash".to_string(),
                "indexed_at".to_string(),
            ]))
            .limit(1)
            .execute()
            .await
            .context("Failed to query file metadata")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect file metadata")?;

        if batches.is_empty() || batches[0].num_rows() == 0 {
            return Ok(None);
        }

        let batch = &batches[0];
        let file_paths = string_column(batch, "file_path")?;
        let languages = string_column(batch, "language")?;
        let file_hashes = string_column(batch, "file_hash")?;
        let indexed_ats = string_column(batch, "indexed_at")?;

        let indexed_at_str = indexed_ats.value(0);
        let indexed_at = indexed_at_str.parse::<i64>().unwrap_or(0);

        Ok(Some(FileMetadata {
            path: file_paths.value(0).to_string(),
            language: Some(languages.value(0).to_string()),
            mtime: 0, // Not stored in LanceDB (derived from filesystem)
            hash: file_hashes.value(0).to_string(),
            indexed_at,
        }))
    }

    /// Get all chunks for a specific file path, ordered by start_line
    pub async fn get_chunks_for_file(
        &self,
        file_path: &str,
        repo: Option<&str>,
    ) -> Result<Vec<Chunk>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let mut filter = format!("file_path = '{}'", file_path.replace('\'', "''"));
        if let Some(repo_name) = repo {
            filter.push_str(&format!(" AND repo = '{}'", repo_name.replace('\'', "''")));
        }

        let results = table
            .query()
            .only_if(filter)
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query chunks for file")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect chunks for file")?;

        let mut chunks = Vec::new();
        for batch in &batches {
            let ids = batch
                .column_by_name("id")
                .context("Missing id column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("id column has wrong type")?;
            let file_paths = batch
                .column_by_name("file_path")
                .context("Missing file_path column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("file_path column has wrong type")?;
            let chunk_names = batch
                .column_by_name("chunk_name")
                .context("Missing chunk_name column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("chunk_name column has wrong type")?;
            let chunk_types = batch
                .column_by_name("chunk_type")
                .context("Missing chunk_type column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("chunk_type column has wrong type")?;
            let start_lines = batch
                .column_by_name("start_line")
                .context("Missing start_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("start_line column has wrong type")?;
            let end_lines = batch
                .column_by_name("end_line")
                .context("Missing end_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("end_line column has wrong type")?;
            let contents = batch
                .column_by_name("content")
                .context("Missing content column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("content column has wrong type")?;
            let languages = batch
                .column_by_name("language")
                .context("Missing language column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("language column has wrong type")?;
            let tags_col = batch
                .column_by_name("tags")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>());

            for i in 0..batch.num_rows() {
                chunks.push(Chunk {
                    id: ids.value(i).to_string(),
                    file_path: file_paths.value(i).to_string(),
                    chunk_type: str_to_chunk_type(chunk_types.value(i)),
                    name: if chunk_names.is_null(i) {
                        None
                    } else {
                        Some(chunk_names.value(i).to_string())
                    },
                    start_line: start_lines.value(i),
                    end_line: end_lines.value(i),
                    content: contents.value(i).to_string(),
                    language: languages.value(i).to_string(),
                    tags: tags_col
                        .map(|arr| arr.value(i).to_string())
                        .unwrap_or_default(),
                });
            }
        }

        chunks.sort_by_key(|c| c.start_line);
        Ok(chunks)
    }

    /// Get all chunks with their embedding vectors, optionally filtered by repo.
    /// Returns (Chunk, embedding, repo_name) tuples for bulk operations like duplicate scanning.
    pub async fn get_all_chunks_with_embeddings(
        &self,
        repo: Option<&str>,
    ) -> Result<Vec<(Chunk, Vec<f32>, String)>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let mut q = table.query().limit(SCAN_ALL_LIMIT);

        if let Some(repo_name) = repo {
            q = q.only_if(format!("repo = '{}'", repo_name.replace('\'', "''")));
        }

        let results = q
            .execute()
            .await
            .context("Failed to query all chunks with embeddings")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect chunks with embeddings")?;

        let mut items = Vec::new();
        for batch in &batches {
            let ids = string_column(batch, "id")?;
            let file_paths = string_column(batch, "file_path")?;
            let chunk_names = string_column(batch, "chunk_name")?;
            let chunk_types = string_column(batch, "chunk_type")?;
            let contents = string_column(batch, "content")?;
            let languages = string_column(batch, "language")?;
            let repos = string_column(batch, "repo")?;
            let start_lines = batch
                .column_by_name("start_line")
                .context("Missing start_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("start_line column has wrong type")?;
            let end_lines = batch
                .column_by_name("end_line")
                .context("Missing end_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("end_line column has wrong type")?;
            let vectors = batch
                .column_by_name("vector")
                .context("Missing vector column")?
                .as_any()
                .downcast_ref::<FixedSizeListArray>()
                .context("vector column has wrong type")?;
            let tags_col = batch
                .column_by_name("tags")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>());

            for i in 0..batch.num_rows() {
                let chunk = Chunk {
                    id: ids.value(i).to_string(),
                    file_path: file_paths.value(i).to_string(),
                    chunk_type: str_to_chunk_type(chunk_types.value(i)),
                    name: if chunk_names.is_null(i) {
                        None
                    } else {
                        Some(chunk_names.value(i).to_string())
                    },
                    start_line: start_lines.value(i),
                    end_line: end_lines.value(i),
                    content: contents.value(i).to_string(),
                    language: languages.value(i).to_string(),
                    tags: tags_col
                        .map(|arr| arr.value(i).to_string())
                        .unwrap_or_default(),
                };

                let value_arr = vectors.value(i);
                let values = value_arr
                    .as_any()
                    .downcast_ref::<Float32Array>()
                    .context("vector values have wrong type")?;
                let embedding = values.values().to_vec();

                let repo_name = repos.value(i).to_string();
                items.push((chunk, embedding, repo_name));
            }
        }

        Ok(items)
    }

    /// Get all chunks whose chunk_name matches the given name, optionally filtered by repo
    pub async fn get_chunks_by_name(&self, name: &str, repo: Option<&str>) -> Result<Vec<Chunk>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let mut filter = format!("chunk_name = '{}'", name.replace('\'', "''"));
        if let Some(repo_name) = repo {
            filter.push_str(&format!(" AND repo = '{}'", repo_name.replace('\'', "''")));
        }

        let results = table
            .query()
            .only_if(filter)
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query chunks by name")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect chunks by name")?;

        let mut chunks = Vec::new();
        for batch in &batches {
            let ids = batch
                .column_by_name("id")
                .context("Missing id column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("id column has wrong type")?;
            let file_paths = batch
                .column_by_name("file_path")
                .context("Missing file_path column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("file_path column has wrong type")?;
            let chunk_names = batch
                .column_by_name("chunk_name")
                .context("Missing chunk_name column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("chunk_name column has wrong type")?;
            let chunk_types = batch
                .column_by_name("chunk_type")
                .context("Missing chunk_type column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("chunk_type column has wrong type")?;
            let start_lines = batch
                .column_by_name("start_line")
                .context("Missing start_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("start_line column has wrong type")?;
            let end_lines = batch
                .column_by_name("end_line")
                .context("Missing end_line column")?
                .as_any()
                .downcast_ref::<UInt32Array>()
                .context("end_line column has wrong type")?;
            let contents = batch
                .column_by_name("content")
                .context("Missing content column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("content column has wrong type")?;
            let languages = batch
                .column_by_name("language")
                .context("Missing language column")?
                .as_any()
                .downcast_ref::<StringArray>()
                .context("language column has wrong type")?;
            let tags_col = batch
                .column_by_name("tags")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>());

            for i in 0..batch.num_rows() {
                chunks.push(Chunk {
                    id: ids.value(i).to_string(),
                    file_path: file_paths.value(i).to_string(),
                    chunk_type: str_to_chunk_type(chunk_types.value(i)),
                    name: if chunk_names.is_null(i) {
                        None
                    } else {
                        Some(chunk_names.value(i).to_string())
                    },
                    start_line: start_lines.value(i),
                    end_line: end_lines.value(i),
                    content: contents.value(i).to_string(),
                    language: languages.value(i).to_string(),
                    tags: tags_col
                        .map(|arr| arr.value(i).to_string())
                        .unwrap_or_default(),
                });
            }
        }

        Ok(chunks)
    }

    /// Get index statistics, optionally filtered by repo
    ///
    /// Compacts the table first to merge fragments. LanceDB scans on heavily
    /// fragmented tables (from per-file delete+insert cycles) can return
    /// incomplete results. Compaction is idempotent and fast when already done.
    pub async fn get_stats(&self, repo: Option<&str>) -> Result<IndexStats> {
        let table = match &self.table {
            Some(t) => t,
            None => {
                return Ok(IndexStats {
                    total_files: 0,
                    total_chunks: 0,
                    total_embeddings: 0,
                    languages: vec![],
                    last_indexed: None,
                    index_size_bytes: 0,
                });
            }
        };

        // Compact before scanning — fragmented tables return incomplete scan
        // results in LanceDB 0.17. Throttled + lock-gated: get_stats is on
        // every request path, and an unconditional compact here was the
        // runaway-contention trigger.
        self.compact_if_stale().await;

        let repo_filter = repo.map(|r| format!("repo = '{}'", r.replace('\'', "''")));

        let total_chunks = table.count_rows(repo_filter.clone()).await.unwrap_or(0) as u64;

        // Scan all rows for per-language aggregation.
        let mut q = table
            .query()
            .select(lancedb::query::Select::Columns(vec![
                "file_path".to_string(),
                "language".to_string(),
                "indexed_at".to_string(),
            ]))
            .limit(SCAN_ALL_LIMIT);

        if let Some(ref filter) = repo_filter {
            q = q.only_if(filter.clone());
        }

        let results = q.execute().await.context("Failed to query stats")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect stats")?;

        let mut file_set = std::collections::HashSet::new();
        let mut lang_files: std::collections::HashMap<String, std::collections::HashSet<String>> =
            std::collections::HashMap::new();
        let mut lang_chunks: std::collections::HashMap<String, u64> =
            std::collections::HashMap::new();
        let mut last_indexed: Option<i64> = None;
        let mut scanned_rows: u64 = 0;

        for batch in &batches {
            let file_paths = string_column(batch, "file_path")?;
            let languages = string_column(batch, "language")?;
            let indexed_ats = string_column(batch, "indexed_at")?;

            for i in 0..batch.num_rows() {
                let fp = file_paths.value(i).to_string();
                let lang = languages.value(i).to_string();
                let ts = indexed_ats.value(i).parse::<i64>().unwrap_or(0);

                file_set.insert(fp.clone());

                lang_files.entry(lang.clone()).or_default().insert(fp);
                *lang_chunks.entry(lang).or_insert(0) += 1;

                if last_indexed.is_none() || Some(ts) > last_indexed {
                    last_indexed = Some(ts);
                }
            }
            scanned_rows += batch.num_rows() as u64;
        }

        // Sanity check: scan should have found all rows. If not, the
        // per-language breakdown is inaccurate (use count_rows as truth).
        if scanned_rows < total_chunks {
            eprintln!(
                "warning: stats scan returned {scanned_rows} rows but count_rows reports \
                 {total_chunks} — per-language breakdown may be incomplete"
            );
        }

        let languages: Vec<LanguageStats> = lang_files
            .iter()
            .map(|(lang, files)| LanguageStats {
                language: lang.clone(),
                file_count: files.len() as u64,
                chunk_count: *lang_chunks.get(lang).unwrap_or(&0),
            })
            .collect();

        Ok(IndexStats {
            total_files: file_set.len() as u64,
            total_chunks,
            total_embeddings: total_chunks,
            languages,
            last_indexed,
            index_size_bytes: 0, // LanceDB doesn't expose this easily
        })
    }

    // ── Tag query methods ─────────────────────────────────────────────

    /// Get all unique tags in use with their chunk counts
    pub async fn get_tag_counts(&self) -> Result<Vec<(String, usize)>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let results = table
            .query()
            .select(lancedb::query::Select::Columns(vec!["tags".to_string()]))
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query tags")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect tag data")?;

        let mut counts: std::collections::HashMap<String, usize> = std::collections::HashMap::new();

        for batch in &batches {
            if let Some(tags_col) = batch
                .column_by_name("tags")
                .and_then(|c| c.as_any().downcast_ref::<StringArray>())
            {
                for i in 0..batch.num_rows() {
                    let tags_str = tags_col.value(i);
                    if !tags_str.is_empty() {
                        for tag in tags_str.split(',') {
                            *counts.entry(tag.to_string()).or_insert(0) += 1;
                        }
                    }
                }
            }
        }

        let mut result: Vec<(String, usize)> = counts.into_iter().collect();
        result.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
        Ok(result)
    }

    /// Get file paths that have a specific tag
    pub async fn get_files_by_tag(&self, tag: &str) -> Result<Vec<String>> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        // Use LIKE filter for comma-separated tags column
        let filter = format!(
            "tags = '{}' OR tags LIKE '{},%' OR tags LIKE '%,{}' OR tags LIKE '%,{},%'",
            tag.replace('\'', "''"),
            tag.replace('\'', "''"),
            tag.replace('\'', "''"),
            tag.replace('\'', "''"),
        );

        let results = table
            .query()
            .select(lancedb::query::Select::Columns(vec![
                "file_path".to_string()
            ]))
            .only_if(filter)
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query files by tag")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect tag results")?;

        let mut files: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
        for batch in &batches {
            let file_paths = string_column(batch, "file_path")?;
            for i in 0..batch.num_rows() {
                files.insert(file_paths.value(i).to_string());
            }
        }

        Ok(files.into_iter().collect())
    }

    /// Count tagged vs untagged chunks
    pub async fn count_tagged_chunks(&self) -> Result<(u64, u64)> {
        let table = match &self.table {
            Some(t) => t,
            None => return Ok((0, 0)),
        };

        let total = table.count_rows(None).await.unwrap_or(0) as u64;
        let untagged = table
            .count_rows(Some("tags = ''".to_string()))
            .await
            .unwrap_or(0) as u64;

        Ok((total - untagged, untagged))
    }

    // ── Dependency graph methods ──────────────────────────────────────

    /// Arrow schema for the dependencies table
    fn deps_schema() -> Schema {
        Schema::new(vec![
            Field::new("file_a", DataType::Utf8, false),
            Field::new("file_b", DataType::Utf8, false),
            Field::new("dep_type", DataType::Utf8, false),
            Field::new("import_statement", DataType::Utf8, false),
            Field::new("symbol", DataType::Utf8, true),
            Field::new("resolved", DataType::Boolean, false),
        ])
    }

    /// Convert ImportDependency slice to a RecordBatch
    fn deps_to_record_batch(deps: &[ImportDependency]) -> Result<RecordBatch> {
        let schema = Arc::new(Self::deps_schema());

        let file_as: Vec<&str> = deps.iter().map(|d| d.file_a.as_str()).collect();
        let file_bs: Vec<&str> = deps.iter().map(|d| d.file_b.as_str()).collect();
        let dep_types: Vec<&str> = deps.iter().map(|d| d.dep_type.as_str()).collect();
        let stmts: Vec<&str> = deps.iter().map(|d| d.import_statement.as_str()).collect();
        let symbols: Vec<Option<&str>> = deps.iter().map(|d| d.symbol.as_deref()).collect();
        let resolved: Vec<bool> = deps.iter().map(|d| d.resolved).collect();

        let columns: Vec<ArrayRef> = vec![
            Arc::new(StringArray::from(file_as)),
            Arc::new(StringArray::from(file_bs)),
            Arc::new(StringArray::from(dep_types)),
            Arc::new(StringArray::from(stmts)),
            Arc::new(StringArray::from(symbols)),
            Arc::new(BooleanArray::from(resolved)),
        ];

        RecordBatch::try_new(schema, columns).context("Failed to create deps record batch")
    }

    /// Insert or replace dependency edges (batch)
    pub async fn upsert_dependencies(&mut self, deps: &[ImportDependency]) -> Result<()> {
        if deps.is_empty() {
            return Ok(());
        }

        let schema = Arc::new(Self::deps_schema());
        let batch = Self::deps_to_record_batch(deps)?;

        match &self.deps_table {
            Some(table) => {
                retry_on_conflict!(
                    table,
                    table
                        .add(Self::batch_to_reader(batch.clone(), schema.clone()))
                        .execute()
                )
                .context("Failed to add dependencies")?;
            }
            None => {
                let reader = Self::batch_to_reader(batch, schema);
                let table = self
                    .conn
                    .create_table(DEPS_TABLE_NAME, reader)
                    .execute()
                    .await
                    .context("Failed to create dependencies table")?;
                self.deps_table = Some(table);
            }
        }

        Ok(())
    }

    /// Get dependencies from a file (what does this file import?)
    pub async fn get_dependencies(&self, file_path: &str) -> Result<Vec<ImportDependency>> {
        let table = match &self.deps_table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let filter = format!("file_a = '{}'", file_path.replace('\'', "''"));
        let results = table
            .query()
            .only_if(filter)
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query dependencies")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect dependencies")?;

        Self::batches_to_deps(&batches)
    }

    /// Get reverse dependencies (what files import this file?)
    pub async fn get_dependents(&self, file_path: &str) -> Result<Vec<ImportDependency>> {
        let table = match &self.deps_table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let filter = format!("file_b = '{}'", file_path.replace('\'', "''"));
        let results = table
            .query()
            .only_if(filter)
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query dependents")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect dependents")?;

        Self::batches_to_deps(&batches)
    }

    /// Clear all dependency data for a single file (for re-indexing)
    pub async fn clear_file_dependencies(&self, file_path: &str) -> Result<()> {
        let table = match &self.deps_table {
            Some(t) => t,
            None => return Ok(()),
        };

        let filter = format!("file_a = '{}'", file_path.replace('\'', "''"));
        retry_on_conflict!(table, table.delete(&filter))
            .context("Failed to clear file dependencies")?;

        Ok(())
    }

    /// Clear all dependency data
    pub async fn clear_dependencies(&self) -> Result<()> {
        let table = match &self.deps_table {
            Some(t) => t,
            None => return Ok(()),
        };

        retry_on_conflict!(table, table.delete("file_a IS NOT NULL"))
            .context("Failed to clear dependencies")?;

        Ok(())
    }

    /// Get dependency statistics: (total_edges, resolved_count)
    pub async fn get_dependency_stats(&self) -> Result<(u64, u64)> {
        let table = match &self.deps_table {
            Some(t) => t,
            None => return Ok((0, 0)),
        };

        let total = table.count_rows(None).await.unwrap_or(0) as u64;
        let resolved = table
            .count_rows(Some("resolved = true".to_string()))
            .await
            .unwrap_or(0) as u64;

        Ok((total, resolved))
    }

    // ── Chunk edges (symbol-level relationships) ──────────────────────────

    /// Arrow schema for the chunk_edges table
    fn chunk_edges_schema() -> Schema {
        Schema::new(vec![
            Field::new("source_chunk", DataType::Utf8, false),
            Field::new("target_chunk", DataType::Utf8, false),
            Field::new("source_name", DataType::Utf8, false),
            Field::new("target_name", DataType::Utf8, false),
            Field::new("edge_type", DataType::Utf8, false),
            Field::new("file_path", DataType::Utf8, false),
            // Scopes rel-path/edge lookups per repo: identical relative paths
            // in two indexed repos must not cross-contaminate neighbors.
            // (Adding this field drops any pre-existing 6-column table on
            // open — cheap for derived data; `bobbin index --force` restores
            // full edge coverage.)
            Field::new("repo", DataType::Utf8, false),
        ])
    }

    /// Convert ChunkEdge slice to a RecordBatch
    fn chunk_edges_to_record_batch(edges: &[ChunkEdge], repo: &str) -> Result<RecordBatch> {
        let schema = Arc::new(Self::chunk_edges_schema());

        let source_chunks: Vec<&str> = edges.iter().map(|e| e.source_chunk.as_str()).collect();
        let target_chunks: Vec<&str> = edges.iter().map(|e| e.target_chunk.as_str()).collect();
        let source_names: Vec<&str> = edges.iter().map(|e| e.source_name.as_str()).collect();
        let target_names: Vec<&str> = edges.iter().map(|e| e.target_name.as_str()).collect();
        let edge_types: Vec<String> = edges.iter().map(|e| e.edge_type.to_string()).collect();
        let edge_type_refs: Vec<&str> = edge_types.iter().map(|s| s.as_str()).collect();
        let file_paths: Vec<&str> = edges.iter().map(|e| e.file_path.as_str()).collect();
        let repos: Vec<&str> = edges.iter().map(|_| repo).collect();

        let columns: Vec<ArrayRef> = vec![
            Arc::new(StringArray::from(source_chunks)),
            Arc::new(StringArray::from(target_chunks)),
            Arc::new(StringArray::from(source_names)),
            Arc::new(StringArray::from(target_names)),
            Arc::new(StringArray::from(edge_type_refs)),
            Arc::new(StringArray::from(file_paths)),
            Arc::new(StringArray::from(repos)),
        ];

        RecordBatch::try_new(schema, columns).context("Failed to create chunk_edges record batch")
    }

    /// Insert chunk edges (batch)
    pub async fn upsert_chunk_edges(&mut self, edges: &[ChunkEdge], repo: &str) -> Result<()> {
        if edges.is_empty() {
            return Ok(());
        }

        let schema = Arc::new(Self::chunk_edges_schema());
        let batch = Self::chunk_edges_to_record_batch(edges, repo)?;

        match &self.chunk_edges_table {
            Some(table) => {
                retry_on_conflict!(
                    table,
                    table
                        .add(Self::batch_to_reader(batch.clone(), schema.clone()))
                        .execute()
                )
                .context("Failed to add chunk edges")?;
            }
            None => {
                let reader = Self::batch_to_reader(batch, schema);
                let table = self
                    .conn
                    .create_table(CHUNK_EDGES_TABLE_NAME, reader)
                    .execute()
                    .await
                    .context("Failed to create chunk_edges table")?;
                self.chunk_edges_table = Some(table);
            }
        }
        Ok(())
    }

    /// Get chunk edges from a file, optionally scoped to one repo
    pub async fn get_chunk_edges(
        &self,
        file_path: &str,
        repo: Option<&str>,
    ) -> Result<Vec<ChunkEdge>> {
        let table = match &self.chunk_edges_table {
            Some(t) => t,
            None => return Ok(Vec::new()),
        };

        let mut filter = format!("file_path = '{}'", file_path.replace('\'', "''"));
        if let Some(r) = repo {
            filter.push_str(&format!(" AND repo = '{}'", r.replace('\'', "''")));
        }
        let results = table
            .query()
            .only_if(filter)
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query chunk edges")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect chunk edges")?;

        Self::batches_to_chunk_edges(&batches)
    }

    /// Get all chunk edges of a specific type
    pub async fn get_chunk_edges_by_type(
        &self,
        edge_type: ChunkEdgeType,
    ) -> Result<Vec<ChunkEdge>> {
        let table = match &self.chunk_edges_table {
            Some(t) => t,
            None => return Ok(Vec::new()),
        };

        let filter = format!("edge_type = '{}'", edge_type);
        let results = table
            .query()
            .only_if(filter)
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query chunk edges by type")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect chunk edges by type")?;

        Self::batches_to_chunk_edges(&batches)
    }

    /// One-time hygiene: delete edge rows keyed by an absolute file path.
    ///
    /// Edges were historically extracted under the absolute walk path while
    /// chunks were parsed under repo-relative paths, so absolute-keyed rows
    /// can never join against the chunks table. Idempotent and cheap; called
    /// on every index run.
    pub async fn clear_absolute_path_chunk_edges(&self) -> Result<()> {
        let table = match &self.chunk_edges_table {
            Some(t) => t,
            None => return Ok(()),
        };

        retry_on_conflict!(table, table.delete("file_path LIKE '/%'"))
            .context("Failed to sweep absolute-path chunk edges")?;

        Ok(())
    }

    /// Clear all chunk edges of one type, optionally scoped to one repo.
    ///
    /// This is what makes `similar --scan --persist` idempotent: each
    /// persist run replaces the previous `similar_to` edge set (within its
    /// repo scope) instead of accumulating duplicates across runs.
    pub async fn clear_chunk_edges_by_type(
        &self,
        edge_type: ChunkEdgeType,
        repo: Option<&str>,
    ) -> Result<()> {
        let table = match &self.chunk_edges_table {
            Some(t) => t,
            None => return Ok(()),
        };

        let mut filter = format!("edge_type = '{}'", edge_type);
        if let Some(r) = repo {
            filter.push_str(&format!(" AND repo = '{}'", r.replace('\'', "''")));
        }
        retry_on_conflict!(table, table.delete(&filter))
            .context("Failed to clear chunk edges by type")?;

        Ok(())
    }

    /// Clear chunk edges for a specific file, optionally scoped to one repo
    pub async fn clear_file_chunk_edges(&self, file_path: &str, repo: Option<&str>) -> Result<()> {
        let table = match &self.chunk_edges_table {
            Some(t) => t,
            None => return Ok(()),
        };

        let mut filter = format!("file_path = '{}'", file_path.replace('\'', "''"));
        if let Some(r) = repo {
            filter.push_str(&format!(" AND repo = '{}'", r.replace('\'', "''")));
        }
        retry_on_conflict!(table, table.delete(&filter))
            .context("Failed to clear file chunk edges")?;

        Ok(())
    }

    /// Get chunk edge statistics: total edges by type
    pub async fn get_chunk_edge_stats(&self) -> Result<Vec<(String, u64)>> {
        let table = match &self.chunk_edges_table {
            Some(t) => t,
            None => return Ok(Vec::new()),
        };

        let mut stats = Vec::new();
        for edge_type in ChunkEdgeType::ALL {
            let filter = format!("edge_type = '{}'", edge_type);
            let count = table.count_rows(Some(filter)).await.unwrap_or(0) as u64;
            if count > 0 {
                stats.push((edge_type.to_string(), count));
            }
        }
        Ok(stats)
    }

    /// Get all edges where the given chunk is source or target.
    ///
    /// By-id (rather than by-file) is the read shape the MCP tool needs:
    /// callers hold a chunk ID from search results, and the query stays
    /// correct once cross-file edges exist.
    pub async fn get_edges_for_chunk(
        &self,
        chunk_id: &str,
        repo: Option<&str>,
    ) -> Result<Vec<ChunkEdge>> {
        let table = match &self.chunk_edges_table {
            Some(t) => t,
            None => return Ok(Vec::new()),
        };

        let escaped = chunk_id.replace('\'', "''");
        let mut filter = format!(
            "(source_chunk = '{}' OR target_chunk = '{}')",
            escaped, escaped
        );
        if let Some(r) = repo {
            filter.push_str(&format!(" AND repo = '{}'", r.replace('\'', "''")));
        }
        let results = table
            .query()
            .only_if(filter)
            .limit(SCAN_ALL_LIMIT)
            .execute()
            .await
            .context("Failed to query edges for chunk")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect edges for chunk")?;

        Self::batches_to_chunk_edges(&batches)
    }

    /// Convert RecordBatches to ChunkEdge structs
    fn batches_to_chunk_edges(batches: &[RecordBatch]) -> Result<Vec<ChunkEdge>> {
        let mut edges = Vec::new();

        for batch in batches {
            let source_chunks = string_column(batch, "source_chunk")?;
            let target_chunks = string_column(batch, "target_chunk")?;
            let source_names = string_column(batch, "source_name")?;
            let target_names = string_column(batch, "target_name")?;
            let edge_types = string_column(batch, "edge_type")?;
            let file_paths = string_column(batch, "file_path")?;

            for i in 0..batch.num_rows() {
                let edge_type = match edge_types.value(i) {
                    "implements" => ChunkEdgeType::Implements,
                    "impl_for" => ChunkEdgeType::ImplFor,
                    "tests" => ChunkEdgeType::Tests,
                    "extends" => ChunkEdgeType::Extends,
                    "next_chunk" => ChunkEdgeType::NextChunk,
                    "part_of" => ChunkEdgeType::PartOf,
                    "similar_to" => ChunkEdgeType::SimilarTo,
                    other => {
                        eprintln!("Unknown chunk edge type: {}", other);
                        continue;
                    }
                };
                edges.push(ChunkEdge {
                    source_chunk: source_chunks.value(i).to_string(),
                    target_chunk: target_chunks.value(i).to_string(),
                    source_name: source_names.value(i).to_string(),
                    target_name: target_names.value(i).to_string(),
                    edge_type,
                    file_path: file_paths.value(i).to_string(),
                });
            }
        }

        Ok(edges)
    }

    // ── Entity embeddings table ──────────────────────────────────────────

    /// Build the Arrow schema for the entities table.
    fn entities_schema(embedding_dim: i32) -> Schema {
        Schema::new(vec![
            Field::new("entity_iri", DataType::Utf8, false),
            Field::new("text", DataType::Utf8, false),
            Field::new(
                "vector",
                DataType::FixedSizeList(Self::vector_field(), embedding_dim),
                false,
            ),
            Field::new("entity_type", DataType::Utf8, false),
            Field::new("repo", DataType::Utf8, true),
        ])
    }

    /// Convert Entity slice + embeddings to a RecordBatch
    fn entities_to_record_batch(
        entities: &[Entity],
        embeddings: &[Vec<f32>],
        embedding_dim: i32,
    ) -> Result<RecordBatch> {
        let schema = Arc::new(Self::entities_schema(embedding_dim));

        let iris: Vec<&str> = entities.iter().map(|e| e.entity_iri.as_str()).collect();
        let texts: Vec<&str> = entities.iter().map(|e| e.text.as_str()).collect();
        let entity_types: Vec<&str> = entities.iter().map(|e| e.entity_type.as_str()).collect();
        let repos: Vec<Option<&str>> = entities.iter().map(|e| e.repo.as_deref()).collect();

        let flat_embeddings: Vec<f32> = embeddings.iter().flatten().copied().collect();
        let embedding_values: ArrayRef = Arc::new(Float32Array::from(flat_embeddings));
        let vector_array = FixedSizeListArray::try_new(
            Self::vector_field(),
            embedding_dim,
            embedding_values,
            None,
        )
        .context("Failed to create entity vector array")?;

        let columns: Vec<ArrayRef> = vec![
            Arc::new(StringArray::from(iris)),
            Arc::new(StringArray::from(texts)),
            Arc::new(vector_array),
            Arc::new(StringArray::from(entity_types)),
            Arc::new(StringArray::from(repos)),
        ];

        RecordBatch::try_new(schema, columns).context("Failed to create entities record batch")
    }

    /// Insert or update entity embeddings.
    ///
    /// Existing entities with matching IRIs are deleted first (upsert).
    pub async fn upsert_entities(
        &mut self,
        entities: &[Entity],
        embeddings: &[Vec<f32>],
    ) -> Result<()> {
        if entities.is_empty() {
            return Ok(());
        }

        if entities.len() != embeddings.len() {
            anyhow::bail!(
                "Entities and embeddings must have same length: {} vs {}",
                entities.len(),
                embeddings.len()
            );
        }

        let schema = Arc::new(Self::entities_schema(self.embedding_dim));
        let batch = Self::entities_to_record_batch(entities, embeddings, self.embedding_dim)?;

        match &self.entities_table {
            Some(table) => {
                // Delete existing entities with same IRIs (upsert)
                let iris: Vec<&str> = entities.iter().map(|e| e.entity_iri.as_str()).collect();
                self.delete_entities(&iris).await?;

                retry_on_conflict!(
                    table,
                    table
                        .add(Self::batch_to_reader(batch.clone(), schema.clone()))
                        .execute()
                )
                .context("Failed to add entities")?;
            }
            None => {
                let reader = Self::batch_to_reader(batch, schema);
                let table = self
                    .conn
                    .create_table(ENTITIES_TABLE_NAME, reader)
                    .execute()
                    .await
                    .context("Failed to create entities table")?;
                self.entities_table = Some(table);
            }
        }

        Ok(())
    }

    /// Search entities by vector similarity.
    pub async fn search_entities(
        &self,
        query_embedding: &[f32],
        limit: usize,
        repo: Option<&str>,
    ) -> Result<Vec<EntitySearchResult>> {
        if limit == 0 {
            return Ok(vec![]);
        }

        let table = match &self.entities_table {
            Some(t) => t,
            None => return Ok(vec![]),
        };

        let mut query = table
            .vector_search(query_embedding.to_vec())
            .context("Failed to create entity vector search")?;

        if let Some(repo_name) = repo {
            query = query.only_if(format!("repo = '{}'", repo_name.replace('\'', "''")));
        }

        let results = match query.limit(limit).execute().await {
            Ok(r) => r,
            Err(e) => {
                let msg = e.to_string();
                if msg.contains("k must be positive") || msg.contains("must be positive") {
                    return Ok(vec![]);
                }
                return Err(e).context("Failed to execute entity vector search");
            }
        };

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect entity search results")?;

        let mut search_results = Vec::new();
        for batch in &batches {
            let iris = string_column(batch, "entity_iri")?;
            let texts = string_column(batch, "text")?;
            let entity_types = string_column(batch, "entity_type")?;
            let repos = string_column(batch, "repo")?;
            let distances = batch
                .column_by_name("_distance")
                .context("Missing _distance column")?
                .as_any()
                .downcast_ref::<Float32Array>()
                .context("_distance column has wrong type")?;

            for i in 0..batch.num_rows() {
                let distance = distances.value(i);
                let score = 1.0 / (1.0 + distance);
                search_results.push(EntitySearchResult {
                    entity_iri: iris.value(i).to_string(),
                    text: texts.value(i).to_string(),
                    entity_type: entity_types.value(i).to_string(),
                    repo: if repos.is_null(i) {
                        None
                    } else {
                        Some(repos.value(i).to_string())
                    },
                    score,
                });
            }
        }

        Ok(search_results)
    }

    /// Get the stored embedding vector for an entity by its IRI.
    pub async fn get_entity_embedding(&self, entity_iri: &str) -> Result<Option<Vec<f32>>> {
        let table = match &self.entities_table {
            Some(t) => t,
            None => return Ok(None),
        };

        let filter = format!("entity_iri = '{}'", entity_iri.replace('\'', "''"));

        let results = table
            .query()
            .only_if(filter)
            .select(lancedb::query::Select::Columns(vec!["vector".to_string()]))
            .limit(1)
            .execute()
            .await
            .context("Failed to query entity embedding")?;

        let batches: Vec<RecordBatch> = results
            .try_collect()
            .await
            .context("Failed to collect entity embedding")?;

        for batch in &batches {
            if batch.num_rows() == 0 {
                continue;
            }

            let vectors = batch
                .column_by_name("vector")
                .context("Missing vector column")?
                .as_any()
                .downcast_ref::<FixedSizeListArray>()
                .context("vector column has wrong type")?;

            let value_arr = vectors.value(0);
            let values = value_arr
                .as_any()
                .downcast_ref::<Float32Array>()
                .context("vector values have wrong type")?;

            return Ok(Some(values.values().to_vec()));
        }

        Ok(None)
    }

    /// Delete entities by their IRIs.
    pub async fn delete_entities(&self, entity_iris: &[&str]) -> Result<()> {
        let table = match &self.entities_table {
            Some(t) => t,
            None => return Ok(()),
        };

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

        let escaped: Vec<String> = entity_iris
            .iter()
            .map(|iri| format!("'{}'", iri.replace('\'', "''")))
            .collect();
        let filter = format!("entity_iri IN ({})", escaped.join(", "));

        retry_on_conflict!(table, table.delete(&filter)).context("Failed to delete entities")?;

        Ok(())
    }

    /// Delete all entities belonging to a specific repo.
    pub async fn delete_entities_by_repo(&self, repo: &str) -> Result<()> {
        let table = match &self.entities_table {
            Some(t) => t,
            None => return Ok(()),
        };

        let filter = format!("repo = '{}'", repo.replace('\'', "''"));
        retry_on_conflict!(table, table.delete(&filter))
            .context("Failed to delete entities by repo")?;

        Ok(())
    }

    /// Get total entity count.
    pub async fn count_entities(&self) -> Result<u64> {
        let table = match &self.entities_table {
            Some(t) => t,
            None => return Ok(0),
        };

        let count = table
            .count_rows(None)
            .await
            .context("Failed to count entities")?;

        Ok(count as u64)
    }

    /// Convert RecordBatches to ImportDependency structs
    fn batches_to_deps(batches: &[RecordBatch]) -> Result<Vec<ImportDependency>> {
        let mut deps = Vec::new();

        for batch in batches {
            let file_as = string_column(batch, "file_a")?;
            let file_bs = string_column(batch, "file_b")?;
            let dep_types = string_column(batch, "dep_type")?;
            let stmts = string_column(batch, "import_statement")?;
            let symbols = string_column(batch, "symbol")?;
            let resolved_col = batch
                .column_by_name("resolved")
                .context("Missing resolved column")?
                .as_any()
                .downcast_ref::<BooleanArray>()
                .context("resolved column has wrong type")?;

            for i in 0..batch.num_rows() {
                deps.push(ImportDependency {
                    file_a: file_as.value(i).to_string(),
                    file_b: file_bs.value(i).to_string(),
                    dep_type: dep_types.value(i).to_string(),
                    import_statement: stmts.value(i).to_string(),
                    symbol: if symbols.is_null(i) {
                        None
                    } else {
                        Some(symbols.value(i).to_string())
                    },
                    resolved: resolved_col.value(i),
                });
            }
        }

        Ok(deps)
    }
}

/// Convert ChunkType to string for storage
fn chunk_type_to_str(ct: &ChunkType) -> &'static str {
    match ct {
        ChunkType::Function => "function",
        ChunkType::Method => "method",
        ChunkType::Class => "class",
        ChunkType::Struct => "struct",
        ChunkType::Enum => "enum",
        ChunkType::Interface => "interface",
        ChunkType::Module => "module",
        ChunkType::Impl => "impl",
        ChunkType::Trait => "trait",
        ChunkType::Doc => "doc",
        ChunkType::Section => "section",
        ChunkType::Table => "table",
        ChunkType::CodeBlock => "code_block",
        ChunkType::Commit => "commit",
        ChunkType::Issue => "issue",
        ChunkType::Other => "other",
    }
}

/// Parse string back to ChunkType
fn str_to_chunk_type(s: &str) -> ChunkType {
    match s {
        "function" => ChunkType::Function,
        "method" => ChunkType::Method,
        "class" => ChunkType::Class,
        "struct" => ChunkType::Struct,
        "enum" => ChunkType::Enum,
        "interface" => ChunkType::Interface,
        "module" => ChunkType::Module,
        "impl" => ChunkType::Impl,
        "trait" => ChunkType::Trait,
        "doc" => ChunkType::Doc,
        "section" => ChunkType::Section,
        "table" => ChunkType::Table,
        "code_block" => ChunkType::CodeBlock,
        "commit" => ChunkType::Commit,
        "issue" => ChunkType::Issue,
        _ => ChunkType::Other,
    }
}

#[path = "lance_fts.rs"]
mod fts;
#[cfg(test)]
use fts::should_rebuild_fts_after;

#[cfg(test)]
#[path = "lance_tests.rs"]
mod tests;