vera2-core 0.12.16-aristoddle.1

Core engine for Vera: parsing, indexing, retrieval, storage, and embedding
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
//! Vector storage and exact similarity search.
//!
//! SQLite vec0 remains the durable vector store and rollback path. The default
//! search mode also maintains a flat, little-endian `f32` sidecar in the index
//! directory and scans it with SimSIMD. Set `VERA_VECTOR_SCAN=vec0` to select
//! the SQLite KNN path for comparison or rollback.

use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap};
use std::fs::{self, File, OpenOptions};
use std::io::{Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::{Mutex, atomic::AtomicU64};
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{Context, Result};
use memmap2::Mmap;
use rusqlite::{Connection, OptionalExtension, ffi::sqlite3_auto_extension, params};
use serde::{Deserialize, Serialize};
use simsimd::SpatialSimilarity;
use sqlite_vec::sqlite3_vec_init;
use zerocopy::IntoBytes;

const PREFIX_RANGE_SQL: &str =
    "SELECT rowid FROM chunk_id_map WHERE chunk_id >= ?1 AND chunk_id < ?2";
const PREFIX_LOWER_BOUND_SQL: &str = "SELECT rowid FROM chunk_id_map WHERE chunk_id >= ?1";
const FLAT_FILE_NAME: &str = "vectors.f32";
const TOMBSTONE_FILE_NAME: &str = "vectors.tombs";
const MANIFEST_FILE_NAME: &str = "vectors.manifest";
const FLAT_MANIFEST_VERSION: u32 = 2;
const VECTOR_SCAN_ENV: &str = "VERA_VECTOR_SCAN";
const DATABASE_ID_KEY: &str = "database_id";

static TEMP_FILE_COUNTER: AtomicU64 = AtomicU64::new(0);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum VectorScanMode {
    Flat,
    Vec0,
}

impl VectorScanMode {
    fn from_env() -> Self {
        match std::env::var(VECTOR_SCAN_ENV) {
            Ok(value) if value.eq_ignore_ascii_case("vec0") => Self::Vec0,
            Ok(value) if value.eq_ignore_ascii_case("flat") || value.trim().is_empty() => {
                Self::Flat
            }
            Ok(value) => {
                tracing::warn!(
                    value = %value,
                    default = "flat",
                    "unknown vector scan mode; using flat scan"
                );
                Self::Flat
            }
            Err(_) => Self::Flat,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct FlatManifest {
    version: u32,
    database_id: String,
    dim: usize,
    max_rowid: i64,
    generation: u64,
    tombstone_count: u64,
    flat_bytes: u64,
}

enum FlatData {
    Empty,
    Memory(Vec<f32>),
    Mmap(Mmap),
}

impl FlatData {
    fn as_slice(&self) -> &[f32] {
        match self {
            Self::Empty => &[],
            Self::Memory(values) => values,
            Self::Mmap(mmap) => {
                // The flat file is written as little-endian f32 and only mapped
                // directly on little-endian targets, which is the format used
                // by Vera's supported native builds.
                debug_assert_eq!(mmap.len() % std::mem::size_of::<f32>(), 0);
                // SAFETY: the mmap starts at a page-aligned address and its
                // length is validated as a multiple of four. Updates preserve
                // existing row offsets, and callers reload after an append or
                // atomic sidecar replacement changes the mapped file size.
                unsafe {
                    std::slice::from_raw_parts(
                        mmap.as_ptr().cast::<f32>(),
                        mmap.len() / std::mem::size_of::<f32>(),
                    )
                }
            }
        }
    }

    fn to_vec(&self) -> Vec<f32> {
        self.as_slice().to_vec()
    }
}

struct FlatSnapshot {
    manifest: FlatManifest,
    data: FlatData,
    tombstones: Vec<u8>,
    manifest_mtime: Option<SystemTime>,
}

struct DiskFlatStorage {
    dim: usize,
    flat_path: PathBuf,
    tombstone_path: PathBuf,
    manifest_path: PathBuf,
    snapshot: FlatSnapshot,
}

struct FlatPaths<'a> {
    flat: &'a Path,
    tombstone: &'a Path,
    manifest: &'a Path,
}

struct MemoryFlatStorage {
    dim: usize,
    snapshot: FlatSnapshot,
}

enum FlatStorage {
    Disk(DiskFlatStorage),
    Memory(MemoryFlatStorage),
}

#[derive(Debug, Clone, Copy)]
struct DistanceCandidate {
    rowid: i64,
    distance: f64,
}

impl PartialEq for DistanceCandidate {
    fn eq(&self, other: &Self) -> bool {
        self.distance.total_cmp(&other.distance) == Ordering::Equal && self.rowid == other.rowid
    }
}

impl Eq for DistanceCandidate {}

impl PartialOrd for DistanceCandidate {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for DistanceCandidate {
    fn cmp(&self, other: &Self) -> Ordering {
        self.distance
            .total_cmp(&other.distance)
            .then_with(|| self.rowid.cmp(&other.rowid))
    }
}

impl FlatSnapshot {
    fn empty(dim: usize, generation: u64) -> Self {
        Self {
            manifest: FlatManifest {
                version: FLAT_MANIFEST_VERSION,
                database_id: String::new(),
                dim,
                max_rowid: 0,
                generation,
                tombstone_count: 0,
                flat_bytes: 0,
            },
            data: FlatData::Empty,
            tombstones: Vec::new(),
            manifest_mtime: None,
        }
    }
}

impl FlatStorage {
    fn open_disk(conn: &Connection, dim: usize, sidecar_dir: &Path) -> Result<Self> {
        let storage = DiskFlatStorage {
            dim,
            flat_path: sidecar_dir.join(FLAT_FILE_NAME),
            tombstone_path: sidecar_dir.join(TOMBSTONE_FILE_NAME),
            manifest_path: sidecar_dir.join(MANIFEST_FILE_NAME),
            snapshot: FlatSnapshot::empty(dim, 0),
        };
        let mut storage = Self::Disk(storage);
        storage.reload_disk(conn)?;
        Ok(storage)
    }

    fn refresh(&mut self, conn: &Connection) -> Result<()> {
        let Self::Disk(storage) = self else {
            return Ok(());
        };
        let generation = current_generation(conn)?;
        let database_id = current_database_id(conn)?;
        let mtime = manifest_mtime(&storage.manifest_path);
        if storage.snapshot.manifest.database_id != database_id
            || storage.snapshot.manifest.generation != generation
            || storage.snapshot.manifest_mtime != mtime
        {
            self.reload_disk(conn)?;
        }
        Ok(())
    }

    fn reload_disk(&mut self, conn: &Connection) -> Result<()> {
        let Self::Disk(storage) = self else {
            return Ok(());
        };
        let snapshot = load_or_rebuild_disk_snapshot(
            conn,
            storage.dim,
            FlatPaths {
                flat: &storage.flat_path,
                tombstone: &storage.tombstone_path,
                manifest: &storage.manifest_path,
            },
        )?;
        storage.snapshot = snapshot;
        Ok(())
    }

    fn apply_update(
        &mut self,
        conn: &Connection,
        inserts: &[(i64, &[f32])],
        tombstone_rowids: &[i64],
        generation: u64,
    ) -> Result<()> {
        match self {
            Self::Memory(storage) => storage.apply_update(inserts, tombstone_rowids, generation),
            Self::Disk(storage) => {
                apply_disk_update(conn, storage, inserts, tombstone_rowids, generation)?;
                self.reload_disk(conn)
            }
        }
    }

    fn search(
        &mut self,
        conn: &Connection,
        query: &[f32],
        limit: usize,
    ) -> Result<Vec<DistanceCandidate>> {
        self.refresh(conn)?;
        let snapshot = match self {
            Self::Disk(storage) => &storage.snapshot,
            Self::Memory(storage) => &storage.snapshot,
        };
        scan_snapshot(snapshot, query, limit)
    }
}

impl MemoryFlatStorage {
    fn apply_update(
        &mut self,
        inserts: &[(i64, &[f32])],
        tombstone_rowids: &[i64],
        generation: u64,
    ) -> Result<()> {
        let max_rowid = inserts
            .iter()
            .map(|(rowid, _)| *rowid)
            .chain(tombstone_rowids.iter().copied())
            .chain(std::iter::once(self.snapshot.manifest.max_rowid))
            .max()
            .unwrap_or(0);
        if max_rowid < 0 {
            anyhow::bail!("negative vector rowid")
        }
        let max_rowid = max_rowid as usize;
        let values_len = max_rowid
            .checked_mul(self.dim)
            .context("flat vector storage size overflow")?;
        let mut values = self.snapshot.data.to_vec();
        values.resize(values_len, 0.0);
        let mut tombstones = self.snapshot.tombstones.clone();
        resize_tombstones(
            &mut tombstones,
            self.snapshot.manifest.max_rowid,
            max_rowid as i64,
        )?;

        for rowid in tombstone_rowids {
            set_tombstone(&mut tombstones, *rowid, true)?;
        }
        for (rowid, vector) in inserts {
            if vector.len() != self.dim {
                anyhow::bail!(
                    "vector dimension mismatch: expected {}, got {}",
                    self.dim,
                    vector.len()
                );
            }
            let rowid = usize::try_from(*rowid).context("vector rowid out of range")?;
            if rowid == 0 {
                anyhow::bail!("vector rowid must be positive")
            }
            let start = (rowid - 1)
                .checked_mul(self.dim)
                .context("flat vector storage offset overflow")?;
            let end = start + self.dim;
            values[start..end].copy_from_slice(vector);
            set_tombstone(&mut tombstones, rowid as i64, false)?;
        }

        let tombstone_count = count_tombstones(&tombstones, max_rowid as i64);
        self.snapshot = FlatSnapshot {
            manifest: FlatManifest {
                version: FLAT_MANIFEST_VERSION,
                database_id: String::new(),
                dim: self.dim,
                max_rowid: max_rowid as i64,
                generation,
                tombstone_count,
                flat_bytes: (values_len * std::mem::size_of::<f32>()) as u64,
            },
            data: if values.is_empty() {
                FlatData::Empty
            } else {
                FlatData::Memory(values)
            },
            tombstones,
            manifest_mtime: None,
        };
        Ok(())
    }
}

fn scan_snapshot(
    snapshot: &FlatSnapshot,
    query: &[f32],
    limit: usize,
) -> Result<Vec<DistanceCandidate>> {
    let max_k = limit.min(MAX_KNN_K);
    if max_k == 0 {
        return Ok(Vec::new());
    }
    let dim = snapshot.manifest.dim;
    if dim == 0 || query.len() != dim {
        anyhow::bail!(
            "flat vector snapshot dimension mismatch: expected {}, got {}",
            dim,
            query.len()
        );
    }
    let values = snapshot.data.as_slice();
    let mut heap = BinaryHeap::with_capacity(max_k);
    for (index, vector) in values.chunks_exact(dim).enumerate() {
        let rowid = index as i64 + 1;
        if is_tombstoned(&snapshot.tombstones, rowid) {
            continue;
        }
        let distance = f32::euclidean(query, vector)
            .context("SimSIMD returned no distance for equal vector dimensions")?;
        let candidate = DistanceCandidate { rowid, distance };
        if heap.len() < max_k {
            heap.push(candidate);
        } else if candidate
            < *heap
                .peek()
                .context("flat top-k heap is unexpectedly empty")?
        {
            heap.pop();
            heap.push(candidate);
        }
    }

    let mut results: Vec<_> = heap.into_vec();
    results.sort();
    Ok(results)
}

fn bitmap_len(max_rowid: i64) -> usize {
    if max_rowid <= 0 {
        0
    } else {
        (max_rowid as usize).div_ceil(8)
    }
}

fn set_tombstone(bitmap: &mut [u8], rowid: i64, deleted: bool) -> Result<()> {
    if rowid <= 0 {
        anyhow::bail!("vector rowid must be positive")
    }
    let index = rowid as usize - 1;
    let byte = index / 8;
    let bit = 1u8 << (index % 8);
    let slot = bitmap
        .get_mut(byte)
        .context("vector rowid exceeds tombstone bitmap")?;
    if deleted {
        *slot |= bit;
    } else {
        *slot &= !bit;
    }
    Ok(())
}

fn is_tombstoned(bitmap: &[u8], rowid: i64) -> bool {
    if rowid <= 0 {
        return true;
    }
    let index = rowid as usize - 1;
    bitmap
        .get(index / 8)
        .is_some_and(|byte| byte & (1u8 << (index % 8)) != 0)
}

fn count_tombstones(bitmap: &[u8], max_rowid: i64) -> u64 {
    if max_rowid <= 0 {
        return 0;
    }
    let max_rowid = max_rowid as usize;
    let full_bytes = max_rowid / 8;
    let mut count: u64 = bitmap
        .get(..full_bytes)
        .unwrap_or_default()
        .iter()
        .map(|byte| u64::from(byte.count_ones()))
        .sum();
    if let Some(remainder) = max_rowid.checked_rem(8)
        && remainder != 0
        && let Some(byte) = bitmap.get(full_bytes)
    {
        count += u64::from((byte & ((1u8 << remainder) - 1)).count_ones());
    }
    count
}

fn resize_tombstones(bitmap: &mut Vec<u8>, old_max_rowid: i64, new_max_rowid: i64) -> Result<()> {
    if old_max_rowid < 0 || new_max_rowid < 0 || new_max_rowid < old_max_rowid {
        anyhow::bail!("invalid vector rowid range for tombstone bitmap")
    }
    bitmap.resize(bitmap_len(new_max_rowid), 0);
    for rowid in old_max_rowid + 1..=new_max_rowid {
        set_tombstone(bitmap, rowid, true)?;
    }
    clear_tombstone_tail(bitmap, new_max_rowid);
    Ok(())
}

fn clear_tombstone_tail(bitmap: &mut [u8], max_rowid: i64) {
    if max_rowid <= 0 {
        return;
    }
    let remainder = (max_rowid as usize) % 8;
    if remainder != 0
        && let Some(last) = bitmap.last_mut()
    {
        *last &= (1u8 << remainder) - 1;
    }
}

fn tombstones_are_consistent(bitmap: &[u8], max_rowid: i64) -> bool {
    if bitmap.len() != bitmap_len(max_rowid) {
        return false;
    }
    if max_rowid <= 0 {
        return bitmap.is_empty();
    }
    let remainder = (max_rowid as usize) % 8;
    remainder == 0
        || bitmap
            .last()
            .is_some_and(|last| *last & !((1u8 << remainder) - 1) == 0)
}

fn manifest_mtime(path: &Path) -> Option<SystemTime> {
    fs::metadata(path)
        .and_then(|metadata| metadata.modified())
        .ok()
}

fn current_generation(conn: &Connection) -> Result<u64> {
    let value: String = conn
        .query_row(
            "SELECT value FROM vector_store_meta WHERE key = 'generation'",
            [],
            |row| row.get(0),
        )
        .context("failed to read vector store generation")?;
    value
        .parse::<u64>()
        .with_context(|| format!("invalid vector store generation: {value}"))
}

fn current_database_id(conn: &Connection) -> Result<String> {
    conn.query_row(
        "SELECT value FROM vector_store_meta WHERE key = ?1",
        params![DATABASE_ID_KEY],
        |row| row.get(0),
    )
    .context("failed to read vector store database identity")
}

fn ensure_database_id(conn: &Connection) -> Result<()> {
    let existing: Option<String> = conn
        .query_row(
            "SELECT value FROM vector_store_meta WHERE key = ?1",
            params![DATABASE_ID_KEY],
            |row| row.get(0),
        )
        .optional()
        .context("failed to inspect vector store database identity")?;
    if existing.is_some_and(|value| !value.is_empty()) {
        return Ok(());
    }

    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_nanos();
    let sequence = TEMP_FILE_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let database_id = format!("{timestamp:032x}-{sequence:016x}");
    conn.execute(
        "INSERT OR IGNORE INTO vector_store_meta (key, value) VALUES (?1, ?2)",
        params![DATABASE_ID_KEY, database_id],
    )
    .context("failed to initialize vector store database identity")?;
    Ok(())
}

fn bump_generation(tx: &rusqlite::Transaction<'_>) -> Result<u64> {
    let current: String = tx
        .query_row(
            "SELECT value FROM vector_store_meta WHERE key = 'generation'",
            [],
            |row| row.get(0),
        )
        .context("failed to read vector store generation")?;
    let next = current
        .parse::<u64>()
        .with_context(|| format!("invalid vector store generation: {current}"))?
        .checked_add(1)
        .context("vector store generation overflow")?;
    tx.execute(
        "UPDATE vector_store_meta SET value = ?1 WHERE key = 'generation'",
        params![next.to_string()],
    )
    .context("failed to update vector store generation")?;
    Ok(next)
}

fn current_max_rowid(conn: &Connection) -> Result<i64> {
    conn.query_row(
        "SELECT COALESCE(
            (SELECT seq FROM sqlite_sequence WHERE name = 'chunk_id_map'),
            (SELECT MAX(rowid) FROM chunk_id_map),
            0
        )",
        [],
        |row| row.get(0),
    )
    .context("failed to read maximum vector rowid")
}

fn expected_flat_bytes(dim: usize, max_rowid: i64) -> Result<u64> {
    let max_rowid = u64::try_from(max_rowid).context("negative maximum vector rowid")?;
    let dim = u64::try_from(dim).context("vector dimension does not fit in u64")?;
    max_rowid
        .checked_mul(dim)
        .and_then(|value| value.checked_mul(std::mem::size_of::<f32>() as u64))
        .context("flat vector storage size overflow")
}

fn load_or_rebuild_disk_snapshot(
    conn: &Connection,
    dim: usize,
    paths: FlatPaths<'_>,
) -> Result<FlatSnapshot> {
    let generation = current_generation(conn)?;
    let database_id = current_database_id(conn)?;
    let max_rowid = current_max_rowid(conn)?;
    if let Some(snapshot) = load_valid_disk_snapshot(
        dim,
        &database_id,
        generation,
        max_rowid,
        paths.flat,
        paths.tombstone,
        paths.manifest,
    )? {
        return Ok(snapshot);
    }

    tracing::debug!(
        flat = %paths.flat.display(),
        "flat vector sidecar missing or inconsistent; rebuilding from vec0"
    );
    rebuild_disk_snapshot(conn, dim, &database_id, max_rowid, generation, paths)
}

fn load_valid_disk_snapshot(
    dim: usize,
    database_id: &str,
    generation: u64,
    max_rowid: i64,
    flat_path: &Path,
    tombstone_path: &Path,
    manifest_path: &Path,
) -> Result<Option<FlatSnapshot>> {
    let manifest_bytes = match fs::read(manifest_path) {
        Ok(bytes) => bytes,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            tracing::debug!(error = %error, "failed to read flat vector manifest");
            return Ok(None);
        }
    };
    let manifest: FlatManifest = match serde_json::from_slice(&manifest_bytes) {
        Ok(manifest) => manifest,
        Err(error) => {
            tracing::debug!(error = %error, "failed to parse flat vector manifest");
            return Ok(None);
        }
    };
    let expected_bytes = expected_flat_bytes(dim, max_rowid)?;
    let flat_size = match fs::metadata(flat_path) {
        Ok(metadata) => metadata.len(),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            tracing::debug!(error = %error, "failed to stat flat vector file");
            return Ok(None);
        }
    };
    let tombstone_bytes = match fs::read(tombstone_path) {
        Ok(bytes) => bytes,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            tracing::debug!(error = %error, "failed to read flat vector tombstones");
            return Ok(None);
        }
    };
    let expected_tombstones = bitmap_len(max_rowid);
    if manifest.version != FLAT_MANIFEST_VERSION
        || manifest.database_id != database_id
        || manifest.dim != dim
        || manifest.max_rowid != max_rowid
        || manifest.generation != generation
        || manifest.flat_bytes != expected_bytes
        || flat_size != expected_bytes
        || tombstone_bytes.len() != expected_tombstones
        || !tombstones_are_consistent(&tombstone_bytes, max_rowid)
        || manifest.tombstone_count != count_tombstones(&tombstone_bytes, max_rowid)
    {
        return Ok(None);
    }

    let data = if expected_bytes == 0 {
        FlatData::Empty
    } else {
        let file = File::open(flat_path).context("failed to open flat vector file")?;
        // SAFETY: the file is validated against the manifest before mapping and
        // is replaced atomically by writers, so this mapping's inode remains
        // stable for its lifetime.
        FlatData::Mmap(unsafe { Mmap::map(&file) }.context("failed to mmap flat vector file")?)
    };
    Ok(Some(FlatSnapshot {
        manifest,
        data,
        tombstones: tombstone_bytes,
        manifest_mtime: manifest_mtime(manifest_path),
    }))
}

fn rebuild_disk_snapshot(
    conn: &Connection,
    dim: usize,
    database_id: &str,
    max_rowid: i64,
    generation: u64,
    paths: FlatPaths<'_>,
) -> Result<FlatSnapshot> {
    let max_rowid_usize =
        usize::try_from(max_rowid).context("maximum vector rowid out of range")?;
    let values_len = max_rowid_usize
        .checked_mul(dim)
        .context("flat vector storage size overflow")?;
    let mut values = vec![0.0f32; values_len];
    let mut tombstones = vec![u8::MAX; bitmap_len(max_rowid)];
    clear_tombstone_tail(&mut tombstones, max_rowid);
    let mut stmt = conn
        .prepare("SELECT rowid, embedding FROM vec_chunks ORDER BY rowid")
        .context("failed to prepare flat vector rebuild")?;
    let rows = stmt
        .query_map([], |row| {
            Ok((row.get::<_, i64>(0)?, row.get::<_, Vec<u8>>(1)?))
        })
        .context("failed to query vectors for flat rebuild")?;
    for row in rows {
        let (rowid, bytes) = row.context("failed to read vector for flat rebuild")?;
        let rowid_usize = usize::try_from(rowid).context("vector rowid out of range")?;
        if rowid_usize == 0 || rowid_usize > max_rowid_usize {
            anyhow::bail!("vector rowid {rowid} exceeds the SQLite rowid space")
        }
        let vector = decode_f32_vector(&bytes, dim)
            .with_context(|| format!("failed to decode vector rowid {rowid}"))?;
        let start = (rowid_usize - 1)
            .checked_mul(dim)
            .context("flat vector storage offset overflow")?;
        values[start..start + dim].copy_from_slice(&vector);
        set_tombstone(&mut tombstones, rowid, false)?;
    }
    let manifest = FlatManifest {
        version: FLAT_MANIFEST_VERSION,
        database_id: database_id.to_string(),
        dim,
        max_rowid,
        generation,
        tombstone_count: count_tombstones(&tombstones, max_rowid),
        flat_bytes: expected_flat_bytes(dim, max_rowid)?,
    };
    publish_disk_files(
        paths.flat,
        paths.tombstone,
        paths.manifest,
        &values,
        &tombstones,
        &manifest,
    )?;
    load_valid_disk_snapshot(
        dim,
        database_id,
        generation,
        max_rowid,
        paths.flat,
        paths.tombstone,
        paths.manifest,
    )?
    .context("flat vector rebuild did not produce a valid snapshot")
}

fn decode_f32_vector(bytes: &[u8], dim: usize) -> Result<Vec<f32>> {
    let expected = dim
        .checked_mul(std::mem::size_of::<f32>())
        .context("vector byte size overflow")?;
    if bytes.len() != expected {
        anyhow::bail!(
            "vector byte length mismatch: expected {}, got {}",
            expected,
            bytes.len()
        );
    }
    Ok(bytes
        .as_chunks::<{ std::mem::size_of::<f32>() }>()
        .0
        .iter()
        .map(|bytes| f32::from_le_bytes(*bytes))
        .collect())
}

fn apply_disk_update(
    conn: &Connection,
    storage: &DiskFlatStorage,
    inserts: &[(i64, &[f32])],
    tombstone_rowids: &[i64],
    generation: u64,
) -> Result<()> {
    let max_rowid = current_max_rowid(conn)?;
    let max_rowid_usize =
        usize::try_from(max_rowid).context("maximum vector rowid out of range")?;
    let previous_max_rowid = storage.snapshot.manifest.max_rowid;
    if max_rowid < previous_max_rowid {
        anyhow::bail!(
            "flat vector maximum rowid moved backwards: previous {}, current {}",
            previous_max_rowid,
            max_rowid
        );
    }
    let expected_bytes = expected_flat_bytes(storage.dim, max_rowid)?;
    let previous_bytes = expected_flat_bytes(storage.dim, previous_max_rowid)?;
    let actual_bytes = fs::metadata(&storage.flat_path)
        .with_context(|| {
            format!(
                "failed to stat flat vector file: {}",
                storage.flat_path.display()
            )
        })?
        .len();
    if actual_bytes != previous_bytes {
        anyhow::bail!(
            "flat vector file size mismatch before incremental update: expected {}, got {}",
            previous_bytes,
            actual_bytes
        );
    }

    let mut writes = Vec::with_capacity(inserts.len());
    for (rowid, vector) in inserts {
        if vector.len() != storage.dim {
            anyhow::bail!(
                "vector dimension mismatch: expected {}, got {}",
                storage.dim,
                vector.len()
            );
        }
        let rowid_usize = usize::try_from(*rowid).context("vector rowid out of range")?;
        if rowid_usize == 0 || rowid_usize > max_rowid_usize {
            anyhow::bail!("vector rowid {rowid} exceeds the SQLite rowid space")
        }
        writes.push((vector_byte_offset(*rowid, storage.dim)?, *vector));
    }

    let mut flat_file = OpenOptions::new()
        .write(true)
        .open(&storage.flat_path)
        .with_context(|| {
            format!(
                "failed to open flat vector file for update: {}",
                storage.flat_path.display()
            )
        })?;
    for (offset, vector) in writes {
        write_vector_at(&mut flat_file, offset, vector)?;
    }
    flat_file.set_len(expected_bytes).with_context(|| {
        format!(
            "failed to size flat vector file: {}",
            storage.flat_path.display()
        )
    })?;
    flat_file.sync_all().with_context(|| {
        format!(
            "failed to sync flat vector file: {}",
            storage.flat_path.display()
        )
    })?;

    let mut tombstones = storage.snapshot.tombstones.clone();
    resize_tombstones(
        &mut tombstones,
        storage.snapshot.manifest.max_rowid,
        max_rowid,
    )?;
    for rowid in tombstone_rowids {
        set_tombstone(&mut tombstones, *rowid, true)?;
    }
    for (rowid, _) in inserts {
        set_tombstone(&mut tombstones, *rowid, false)?;
    }
    let manifest = FlatManifest {
        version: FLAT_MANIFEST_VERSION,
        database_id: current_database_id(conn)?,
        dim: storage.dim,
        max_rowid,
        generation,
        tombstone_count: count_tombstones(&tombstones, max_rowid),
        flat_bytes: expected_bytes,
    };
    let tombstone_tmp = write_temp_file(&storage.tombstone_path, &tombstones)?;
    let manifest_bytes = serde_json::to_vec(&manifest).context("failed to encode flat manifest")?;
    let manifest_tmp = match write_temp_file(&storage.manifest_path, &manifest_bytes) {
        Ok(path) => path,
        Err(error) => {
            let _ = fs::remove_file(&tombstone_tmp);
            return Err(error);
        }
    };

    if let Err(error) = fs::rename(&tombstone_tmp, &storage.tombstone_path) {
        let _ = fs::remove_file(&tombstone_tmp);
        let _ = fs::remove_file(&manifest_tmp);
        return Err(error).context("failed to publish vector tombstones");
    }
    if let Err(error) = fs::rename(&manifest_tmp, &storage.manifest_path) {
        let _ = fs::remove_file(&manifest_tmp);
        return Err(error).with_context(|| {
            format!(
                "failed to publish vector manifest: {}",
                storage.manifest_path.display()
            )
        });
    }
    Ok(())
}

fn vector_byte_offset(rowid: i64, dim: usize) -> Result<u64> {
    let rowid = u64::try_from(rowid)
        .context("vector rowid out of range")?
        .checked_sub(1)
        .context("vector rowid must be positive")?;
    rowid
        .checked_mul(u64::try_from(dim).context("vector dimension does not fit in u64")?)
        .and_then(|offset| offset.checked_mul(std::mem::size_of::<f32>() as u64))
        .context("flat vector storage offset overflow")
}

fn write_vector_at(file: &mut File, offset: u64, vector: &[f32]) -> Result<()> {
    let mut bytes = Vec::with_capacity(std::mem::size_of_val(vector));
    for value in vector {
        bytes.extend_from_slice(&value.to_le_bytes());
    }
    file.seek(SeekFrom::Start(offset))
        .with_context(|| format!("failed to seek flat vector file to byte {offset}"))?;
    file.write_all(&bytes)
        .with_context(|| format!("failed to write flat vector row at byte {offset}"))
}

fn publish_disk_files(
    flat_path: &Path,
    tombstone_path: &Path,
    manifest_path: &Path,
    values: &[f32],
    tombstones: &[u8],
    manifest: &FlatManifest,
) -> Result<()> {
    let mut flat_bytes = Vec::with_capacity(std::mem::size_of_val(values));
    for value in values {
        flat_bytes.extend_from_slice(&value.to_le_bytes());
    }
    let manifest_bytes = serde_json::to_vec(manifest).context("failed to encode flat manifest")?;
    let flat_tmp = write_temp_file(flat_path, &flat_bytes)?;
    let tombstone_tmp = match write_temp_file(tombstone_path, tombstones) {
        Ok(path) => path,
        Err(error) => {
            let _ = fs::remove_file(&flat_tmp);
            return Err(error);
        }
    };
    let manifest_tmp = match write_temp_file(manifest_path, &manifest_bytes) {
        Ok(path) => path,
        Err(error) => {
            let _ = fs::remove_file(&flat_tmp);
            let _ = fs::remove_file(&tombstone_tmp);
            return Err(error);
        }
    };

    if let Err(error) = fs::rename(&flat_tmp, flat_path) {
        let _ = fs::remove_file(&flat_tmp);
        let _ = fs::remove_file(&tombstone_tmp);
        let _ = fs::remove_file(&manifest_tmp);
        return Err(error).context("failed to publish flat vector file");
    }
    if let Err(error) = fs::rename(&tombstone_tmp, tombstone_path) {
        let _ = fs::remove_file(&tombstone_tmp);
        let _ = fs::remove_file(&manifest_tmp);
        return Err(error).context("failed to publish vector tombstones");
    }
    fs::rename(&manifest_tmp, manifest_path).with_context(|| {
        format!(
            "failed to publish vector manifest: {}",
            manifest_path.display()
        )
    })
}

fn write_temp_file(path: &Path, bytes: &[u8]) -> Result<PathBuf> {
    let counter = TEMP_FILE_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let temp_path = path.with_extension(format!(
        "{}-{}.tmp",
        path.extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or("sidecar"),
        counter
    ));
    let mut file = OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&temp_path)
        .with_context(|| {
            format!(
                "failed to create sidecar temp file: {}",
                temp_path.display()
            )
        })?;
    file.write_all(bytes)
        .with_context(|| format!("failed to write sidecar temp file: {}", temp_path.display()))?;
    file.sync_all()
        .with_context(|| format!("failed to sync sidecar temp file: {}", temp_path.display()))?;
    Ok(temp_path)
}

/// Dual-backed vector store for embedding search.
///
/// SQLite vec0 remains the durable source used for writes and sidecar
/// recovery. The default search path scans the exact flat SIMD sidecar; set
/// `VERA_VECTOR_SCAN=vec0` to select sqlite-vec at open time.
pub struct VectorStore {
    conn: Connection,
    dim: usize,
    scan_mode: VectorScanMode,
    flat: Mutex<FlatStorage>,
}

/// Maximum `k` sqlite-vec accepts in a KNN query. Requesting more is a hard
/// error from the extension, not a soft limit.
///
/// Public so callers can size their candidate pools against the real ceiling
/// instead of scaling past it and relying on [`VectorStore::search`] to clamp.
pub const MAX_KNN_K: usize = 4096;

/// A single vector search result: chunk ID and distance score.
#[derive(Debug, Clone)]
pub struct VectorSearchResult {
    /// The chunk ID (rowid in the vec table, mapped to chunk string ID).
    pub chunk_id: String,
    /// Distance from the query vector (lower is closer).
    pub distance: f64,
}

impl VectorStore {
    /// Open (or create) a vector store at the given path.
    ///
    /// The `dim` parameter specifies the vector dimensionality.
    pub fn open(db_path: &Path, dim: usize) -> Result<Self> {
        Self::open_at_mode(db_path, dim, VectorScanMode::from_env())
    }

    fn open_at_mode(db_path: &Path, dim: usize, scan_mode: VectorScanMode) -> Result<Self> {
        register_sqlite_vec();
        let conn = Connection::open(db_path)
            .with_context(|| format!("failed to open vector db: {}", db_path.display()))?;
        let sidecar_dir = db_path.parent().filter(|path| !path.as_os_str().is_empty());
        let sidecar_dir = sidecar_dir.unwrap_or_else(|| Path::new("."));
        Self::from_connection(conn, dim, scan_mode, Some(sidecar_dir))
    }

    /// Create an in-memory vector store (useful for testing).
    pub fn open_in_memory(dim: usize) -> Result<Self> {
        register_sqlite_vec();
        let conn = Connection::open_in_memory().context("failed to open in-memory vector db")?;
        Self::from_connection(conn, dim, VectorScanMode::from_env(), None)
    }

    fn from_connection(
        conn: Connection,
        dim: usize,
        scan_mode: VectorScanMode,
        sidecar_dir: Option<&Path>,
    ) -> Result<Self> {
        let store = Self {
            conn,
            dim,
            scan_mode,
            flat: Mutex::new(FlatStorage::Memory(MemoryFlatStorage {
                dim,
                snapshot: FlatSnapshot::empty(dim, 0),
            })),
        };
        store.init_schema()?;
        if let Some(sidecar_dir) = sidecar_dir {
            let flat = FlatStorage::open_disk(&store.conn, dim, sidecar_dir)?;
            *store
                .flat
                .lock()
                .map_err(|_| anyhow::anyhow!("flat vector storage lock poisoned"))? = flat;
        }
        Ok(store)
    }

    #[cfg(test)]
    fn open_in_memory_with_mode(dim: usize, scan_mode: VectorScanMode) -> Result<Self> {
        register_sqlite_vec();
        let conn = Connection::open_in_memory().context("failed to open in-memory vector db")?;
        Self::from_connection(conn, dim, scan_mode, None)
    }

    #[cfg(test)]
    fn open_disk_with_mode(db_path: &Path, dim: usize, scan_mode: VectorScanMode) -> Result<Self> {
        Self::open_at_mode(db_path, dim, scan_mode)
    }

    #[cfg(test)]
    fn rowids_for_chunk_ids(&self, ids: &[&str]) -> Vec<i64> {
        ids.iter()
            .map(|id| {
                self.conn
                    .query_row(
                        "SELECT rowid FROM chunk_id_map WHERE chunk_id = ?1",
                        params![id],
                        |row| row.get(0),
                    )
                    .unwrap()
            })
            .collect()
    }

    /// Initialize the vector table schema.
    fn init_schema(&self) -> Result<()> {
        self.conn
            .execute_batch("PRAGMA journal_mode=WAL; PRAGMA synchronous=NORMAL;")
            .context("failed to set vector db pragmas")?;

        // Mapping from string chunk IDs to integer rowids.
        self.conn
            .execute_batch(
                "CREATE TABLE IF NOT EXISTS chunk_id_map (
                    rowid INTEGER PRIMARY KEY AUTOINCREMENT,
                    chunk_id TEXT NOT NULL UNIQUE
                );
                -- `chunk_id` is UNIQUE, which already builds
                -- `sqlite_autoindex_chunk_id_map_1` over the same column. The
                -- explicit index duplicated it exactly and only added a second
                -- B-tree to maintain on every vector insert. Dropped here so
                -- existing databases shed it on next open; the autoindex
                -- serves both the equality lookup and the prefix range scan.
                DROP INDEX IF EXISTS idx_chunk_id_map;",
            )
            .context("failed to create chunk_id_map table")?;

        self.conn
            .execute_batch(
                "CREATE TABLE IF NOT EXISTS vector_store_meta (
                    key TEXT PRIMARY KEY,
                    value TEXT NOT NULL
                );
                INSERT OR IGNORE INTO vector_store_meta (key, value)
                    VALUES ('generation', '0');",
            )
            .context("failed to create vector store metadata")?;
        ensure_database_id(&self.conn)?;

        // sqlite-vec virtual table for vector storage.
        self.conn
            .execute_batch(&format!(
                "CREATE VIRTUAL TABLE IF NOT EXISTS vec_chunks
                 USING vec0(embedding float[{}])",
                self.dim
            ))
            .context("failed to create vec_chunks virtual table")?;

        Ok(())
    }

    /// Insert a single vector for a chunk.
    ///
    /// Uses INSERT OR IGNORE + SELECT to get a stable rowid, avoiding the
    /// AUTOINCREMENT orphan problem where INSERT OR REPLACE allocates a
    /// new rowid and orphans old vectors in the vec_chunks virtual table.
    /// For re-inserts (same chunk_id), deletes the old vector first since
    /// the vec0 virtual table does not support INSERT OR REPLACE.
    pub fn insert(&self, chunk_id: &str, vector: &[f32]) -> Result<()> {
        if vector.len() != self.dim {
            anyhow::bail!(
                "vector dimension mismatch: expected {}, got {}",
                self.dim,
                vector.len()
            );
        }

        self.refresh_flat()?;

        let tx = rusqlite::Transaction::new_unchecked(
            &self.conn,
            rusqlite::TransactionBehavior::Immediate,
        )
        .context("failed to begin vector insert transaction")?;
        // Use INSERT OR IGNORE to preserve existing rowid if already present.
        tx.execute(
            "INSERT OR IGNORE INTO chunk_id_map (chunk_id) VALUES (?1)",
            params![chunk_id],
        )
        .context("failed to insert chunk id mapping")?;

        let rowid: i64 = tx
            .query_row(
                "SELECT rowid FROM chunk_id_map WHERE chunk_id = ?1",
                params![chunk_id],
                |row| row.get(0),
            )
            .context("failed to get rowid for chunk")?;

        // Delete any existing vector for this rowid before inserting.
        // vec0 virtual tables do not support INSERT OR REPLACE. Deleting an
        // absent rowid is Ok with 0 rows, so only genuine failures (I/O,
        // virtual-table malfunction) reach this error path; swallowing them
        // would persist a mapping row the count and KNN cannot agree on.
        tx.execute("DELETE FROM vec_chunks WHERE rowid = ?1", params![rowid])
            .context("failed to delete stale vector")?;

        tx.execute(
            "INSERT INTO vec_chunks (rowid, embedding) VALUES (?1, ?2)",
            params![rowid, vector.as_bytes()],
        )
        .context("failed to insert vector")?;

        let generation = bump_generation(&tx)?;
        tx.commit().context("failed to commit vector insert")?;
        self.update_flat(&[(rowid, vector)], &[], generation)?;
        Ok(())
    }

    /// Insert a batch of vectors.
    ///
    /// Uses INSERT OR IGNORE to preserve stable rowids, avoiding the
    /// AUTOINCREMENT orphan problem. For re-inserts, deletes old vectors
    /// first since the vec0 virtual table doesn't support upsert.
    pub fn insert_batch(&self, items: &[(&str, &[f32])]) -> Result<()> {
        if items.is_empty() {
            return Ok(());
        }
        self.refresh_flat()?;
        let tx = self
            .conn
            .unchecked_transaction()
            .context("failed to begin vector insert transaction")?;
        let mut flat_inserts = Vec::with_capacity(items.len());
        {
            let mut id_stmt = self
                .conn
                .prepare_cached("INSERT OR IGNORE INTO chunk_id_map (chunk_id) VALUES (?1)")
                .context("failed to prepare id insert")?;

            let mut rowid_stmt = self
                .conn
                .prepare_cached("SELECT rowid FROM chunk_id_map WHERE chunk_id = ?1")
                .context("failed to prepare rowid query")?;

            let mut del_vec_stmt = self
                .conn
                .prepare_cached("DELETE FROM vec_chunks WHERE rowid = ?1")
                .context("failed to prepare vector delete")?;

            let mut vec_stmt = self
                .conn
                .prepare_cached("INSERT INTO vec_chunks (rowid, embedding) VALUES (?1, ?2)")
                .context("failed to prepare vector insert")?;

            for (chunk_id, vector) in items {
                if vector.len() != self.dim {
                    anyhow::bail!(
                        "vector dimension mismatch for {}: expected {}, got {}",
                        chunk_id,
                        self.dim,
                        vector.len()
                    );
                }

                id_stmt
                    .execute(params![chunk_id])
                    .context("failed to insert chunk id")?;

                let rowid: i64 = rowid_stmt
                    .query_row(params![chunk_id], |row| row.get(0))
                    .context("failed to get rowid")?;

                // Same as in insert(): deleting an absent rowid is Ok with 0
                // rows, so only genuine failures reach the error path.
                del_vec_stmt
                    .execute(params![rowid])
                    .context("failed to delete stale vector")?;

                vec_stmt
                    .execute(params![rowid, vector.as_bytes()])
                    .context("failed to insert vector")?;
                flat_inserts.push((rowid, *vector));
            }
        }
        let generation = bump_generation(&tx)?;
        tx.commit().context("failed to commit vector batch")?;
        self.update_flat(&flat_inserts, &[], generation)?;
        Ok(())
    }

    /// Insert a batch of vectors whose chunk ids are all new to this store.
    ///
    /// Full builds write into a freshly created staging store, where the
    /// per-row `INSERT OR IGNORE` + rowid lookup + stale-vector delete that
    /// [`VectorStore::insert_batch`] performs can never match anything. This
    /// path halves the per-row statement count for that case. A pre-existing
    /// chunk id violates the PRIMARY KEY and fails loudly, so callers cannot
    /// silently corrupt an existing mapping.
    pub fn insert_batch_fresh(&self, items: &[(&str, &[f32])]) -> Result<()> {
        if items.is_empty() {
            return Ok(());
        }
        self.refresh_flat()?;
        let tx = self
            .conn
            .unchecked_transaction()
            .context("failed to begin vector insert transaction")?;
        let mut flat_inserts = Vec::with_capacity(items.len());
        {
            let mut id_stmt = self
                .conn
                .prepare_cached("INSERT INTO chunk_id_map (chunk_id) VALUES (?1)")
                .context("failed to prepare id insert")?;

            let mut vec_stmt = self
                .conn
                .prepare_cached("INSERT INTO vec_chunks (rowid, embedding) VALUES (?1, ?2)")
                .context("failed to prepare vector insert")?;

            for (chunk_id, vector) in items {
                if vector.len() != self.dim {
                    anyhow::bail!(
                        "vector dimension mismatch for {}: expected {}, got {}",
                        chunk_id,
                        self.dim,
                        vector.len()
                    );
                }

                id_stmt
                    .execute(params![chunk_id])
                    .context("failed to insert chunk id")?;
                let rowid = self.conn.last_insert_rowid();

                vec_stmt
                    .execute(params![rowid, vector.as_bytes()])
                    .context("failed to insert vector")?;
                flat_inserts.push((rowid, *vector));
            }
        }
        let generation = bump_generation(&tx)?;
        tx.commit().context("failed to commit vector batch")?;
        self.update_flat(&flat_inserts, &[], generation)?;
        Ok(())
    }

    /// Find the nearest neighbors to a query vector.
    ///
    /// Returns up to `limit` results sorted by ascending distance.
    pub fn search(&self, query: &[f32], limit: usize) -> Result<Vec<VectorSearchResult>> {
        if query.len() != self.dim {
            anyhow::bail!(
                "query vector dimension mismatch: expected {}, got {}",
                self.dim,
                query.len()
            );
        }

        if limit > MAX_KNN_K {
            tracing::warn!(
                requested = limit,
                clamped = MAX_KNN_K,
                "clamping vector search limit to the KNN cap; the extra candidates are not fetched"
            );
        }

        if self.scan_mode == VectorScanMode::Flat {
            let mut flat = self
                .flat
                .lock()
                .map_err(|_| anyhow::anyhow!("flat vector storage lock poisoned"))?;
            let hits = flat.search(&self.conn, query, limit)?;
            let rowids: Vec<i64> = hits.iter().map(|hit| hit.rowid).collect();
            let chunk_ids = self.chunk_ids_for_rowids(&rowids)?;
            return hits
                .iter()
                .map(|hit| {
                    let chunk_id = chunk_ids
                        .get(&hit.rowid)
                        .with_context(|| format!("failed to map rowid {} to chunk_id", hit.rowid))?
                        .clone();
                    Ok(VectorSearchResult {
                        chunk_id,
                        distance: hit.distance,
                    })
                })
                .collect();
        }

        // sqlite-vec reads this LIMIT as the KNN `k` and rejects anything above
        // MAX_KNN_K with "k value in knn query too large". Callers scale the
        // candidate pool from the query type and result limit, which can exceed
        // it on natural-language queries, and the whole vector arm would then be
        // dropped in favour of BM25-only results. Ask for as many as the backend
        // allows instead.
        // Warn rather than debug: this is a silent quality reduction, and the
        // reason #38 was hard to diagnose was a swallowed signal. Vera's own
        // retrieval path now bounds the pool before it gets here, so reaching
        // this branch means an external caller asked for more than the backend
        // can give.
        let limit = limit.min(MAX_KNN_K);

        // `prepare`, not `prepare_cached`: `limit` is interpolated into the
        // text, so every distinct limit is a distinct cache key. Caching these
        // never hits and evicts the statements that do have stable text, since
        // rusqlite's cache is a 16-entry LRU shared by the whole connection.
        let mut stmt = self
            .conn
            .prepare(&format!(
                "SELECT v.rowid, v.distance
                 FROM vec_chunks v
                 WHERE v.embedding MATCH ?1
                 ORDER BY v.distance
                 LIMIT {limit}"
            ))
            .context("failed to prepare vector search")?;

        let hits: Vec<(i64, f64)> = stmt
            .query_map([query.as_bytes()], |row| {
                Ok((row.get::<_, i64>(0)?, row.get::<_, f64>(1)?))
            })
            .context("failed to execute vector search")?
            .collect::<std::result::Result<Vec<_>, _>>()
            .context("failed to read vector result")?;

        // Resolve the rowids in one query instead of one per hit. The KNN
        // query above is deliberately left alone: joining `chunk_id_map` into
        // it would risk losing the vec0 KNN optimization.
        let rowids: Vec<i64> = hits.iter().map(|(rowid, _)| *rowid).collect();
        let chunk_ids = self.chunk_ids_for_rowids(&rowids)?;

        hits.iter()
            .map(|(rowid, distance)| {
                let chunk_id = chunk_ids
                    .get(rowid)
                    .with_context(|| format!("failed to map rowid {rowid} to chunk_id"))?
                    .clone();
                Ok(VectorSearchResult {
                    chunk_id,
                    distance: *distance,
                })
            })
            .collect()
    }

    /// Resolve many rowids to their chunk ids in a single query.
    fn chunk_ids_for_rowids(&self, rowids: &[i64]) -> Result<HashMap<i64, String>> {
        if rowids.is_empty() {
            return Ok(HashMap::new());
        }

        let placeholders = std::iter::repeat_n("?", rowids.len())
            .collect::<Vec<_>>()
            .join(",");
        // Text varies with the number of ids, so plain `prepare` here too.
        let mut stmt = self
            .conn
            .prepare(&format!(
                "SELECT rowid, chunk_id FROM chunk_id_map WHERE rowid IN ({placeholders})"
            ))
            .context("failed to prepare chunk id lookup")?;

        let mapped = stmt
            .query_map(rusqlite::params_from_iter(rowids.iter()), |row| {
                Ok((row.get::<_, i64>(0)?, row.get::<_, String>(1)?))
            })
            .context("failed to query chunk ids")?
            .collect::<std::result::Result<HashMap<_, _>, _>>()
            .context("failed to collect chunk ids")?;
        Ok(mapped)
    }

    /// Count vectors actually stored, and therefore searchable.
    ///
    /// `chunk_id_map` historically backed this number, but a mapping row can
    /// exist without a backing vector in databases written before single
    /// inserts became transactional. KNN resolves every hit through
    /// `vec_chunks`, so that is what an honest count reports; orphan mappings
    /// neither inflate it nor surface in searches, and they disappear when
    /// their file is next re-indexed.
    pub fn count(&self) -> Result<u64> {
        let count: i64 = self
            .conn
            .query_row("SELECT COUNT(*) FROM vec_chunks", [], |row| row.get(0))
            .context("failed to count vectors")?;
        Ok(count as u64)
    }

    /// Delete a vector by chunk ID.
    pub fn delete(&self, chunk_id: &str) -> Result<bool> {
        let rowid: Option<i64> = self
            .conn
            .query_row(
                "SELECT rowid FROM chunk_id_map WHERE chunk_id = ?1",
                params![chunk_id],
                |row| row.get(0),
            )
            .optional()
            .context("failed to look up chunk for deletion")?;

        if let Some(rowid) = rowid {
            self.refresh_flat()?;
            let tx = rusqlite::Transaction::new_unchecked(
                &self.conn,
                rusqlite::TransactionBehavior::Immediate,
            )
            .context("failed to begin vector delete transaction")?;
            tx.execute("DELETE FROM vec_chunks WHERE rowid = ?1", params![rowid])
                .context("failed to delete vector")?;
            tx.execute("DELETE FROM chunk_id_map WHERE rowid = ?1", params![rowid])
                .context("failed to delete chunk id mapping")?;
            let generation = bump_generation(&tx)?;
            tx.commit().context("failed to commit vector delete")?;
            self.update_flat(&[], &[rowid], generation)?;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Delete all vectors whose chunk_id starts with the given prefix.
    ///
    /// This is used for incremental indexing: when a file is re-indexed, all
    /// old chunks for that file (whose IDs start with "filepath:") are removed.
    pub fn delete_by_file_prefix(&self, prefix: &str) -> Result<u64> {
        self.delete_by_file_prefix_after_scan(prefix, || {})
    }

    fn delete_by_file_prefix_after_scan<F>(&self, prefix: &str, after_scan: F) -> Result<u64>
    where
        F: FnOnce(),
    {
        self.refresh_flat()?;

        // A half-open range rather than `LIKE ?1 ESCAPE '\'`. The ESCAPE
        // clause disqualifies SQLite's LIKE-prefix optimization, so the LIKE
        // form scans `chunk_id_map` in full where a range seeks the index. It
        // also removes the need to escape `%` and `_`, since a range has no
        // wildcards to confuse.
        // One transaction for the whole file instead of two commits per row,
        // opened *before* the scan so a concurrent writer on the same database
        // cannot insert a matching row between finding the rowids and deleting
        // them — that row would survive while this reported success.
        //
        // IMMEDIATE rather than the default DEFERRED because this transaction
        // reads and then writes: a deferred transaction starts as a reader and
        // can fail the upgrade with SQLITE_BUSY_SNAPSHOT, which `busy_timeout`
        // does not retry.
        let tx = rusqlite::Transaction::new_unchecked(
            &self.conn,
            rusqlite::TransactionBehavior::Immediate,
        )
        .context("failed to begin prefix delete transaction")?;

        let rows = rowids_with_prefix(&tx, prefix)?;
        after_scan();
        let count = rows.len() as u64;
        if count == 0 {
            return Ok(count);
        }

        {
            let mut delete_vector = tx
                .prepare_cached("DELETE FROM vec_chunks WHERE rowid = ?1")
                .context("failed to prepare vector delete")?;
            let mut delete_mapping = tx
                .prepare_cached("DELETE FROM chunk_id_map WHERE rowid = ?1")
                .context("failed to prepare chunk id delete")?;
            for rowid in &rows {
                delete_vector
                    .execute(params![rowid])
                    .context("failed to delete vector by prefix")?;
                delete_mapping
                    .execute(params![rowid])
                    .context("failed to delete chunk id by prefix")?;
            }
        }
        let generation = bump_generation(&tx)?;
        tx.commit().context("failed to commit prefix delete")?;
        self.update_flat(&[], &rows, generation)?;

        Ok(count)
    }

    /// Clear all vectors from the store.
    pub fn clear(&self) -> Result<()> {
        self.refresh_flat()?;
        let tx = rusqlite::Transaction::new_unchecked(
            &self.conn,
            rusqlite::TransactionBehavior::Immediate,
        )
        .context("failed to begin vector clear transaction")?;
        tx.execute_batch("DELETE FROM vec_chunks; DELETE FROM chunk_id_map;")
            .context("failed to clear vector store")?;
        let generation = bump_generation(&tx)?;
        let max_rowid = current_max_rowid(&self.conn)?;
        let tombstones: Vec<i64> = (1..=max_rowid).collect();
        tx.commit().context("failed to commit vector clear")?;
        self.update_flat(&[], &tombstones, generation)?;
        Ok(())
    }

    /// Get the configured vector dimensionality.
    pub fn dim(&self) -> usize {
        self.dim
    }

    fn update_flat(
        &self,
        inserts: &[(i64, &[f32])],
        tombstone_rowids: &[i64],
        generation: u64,
    ) -> Result<()> {
        let mut flat = self
            .flat
            .lock()
            .map_err(|_| anyhow::anyhow!("flat vector storage lock poisoned"))?;
        if let FlatStorage::Disk(storage) = &*flat {
            let current_generation = current_generation(&self.conn)?;
            let current_database_id = current_database_id(&self.conn)?;
            let generation_changed = current_generation != generation;
            let database_changed = storage.snapshot.manifest.database_id != current_database_id;
            let snapshot_is_stale =
                generation.checked_sub(1) != Some(storage.snapshot.manifest.generation);
            if generation_changed || database_changed || snapshot_is_stale {
                tracing::debug!(
                    expected_generation = generation,
                    "vector store changed while publishing flat sidecar; reloading"
                );
                flat.reload_disk(&self.conn)?;
                return Ok(());
            }
        }
        flat.apply_update(&self.conn, inserts, tombstone_rowids, generation)
    }

    fn refresh_flat(&self) -> Result<()> {
        let mut flat = self
            .flat
            .lock()
            .map_err(|_| anyhow::anyhow!("flat vector storage lock poisoned"))?;
        flat.refresh(&self.conn)
    }
}

/// Rowids of every `chunk_id` starting with `prefix`, via a range scan.
///
/// Takes the connection rather than `&self` so the caller can pass an open
/// transaction, keeping the scan atomic with the deletes that follow it.
fn rowids_with_prefix(conn: &Connection, prefix: &str) -> Result<Vec<i64>> {
    // The two predicates stay distinct because `prefix_upper_bound` returns
    // None when nothing can sort above the prefix, and `>= prefix` is then
    // already exact. Only the execution and collection are shared; merging the
    // SQL would cost the index range plan, which is the point of all this.
    let upper = prefix_upper_bound(prefix);
    let (sql, args): (&str, Vec<&dyn rusqlite::ToSql>) = match &upper {
        Some(upper) => (PREFIX_RANGE_SQL, vec![&prefix, upper]),
        None => (PREFIX_LOWER_BOUND_SQL, vec![&prefix]),
    };

    let mut stmt = conn
        .prepare_cached(sql)
        .context("failed to prepare prefix delete query")?;
    let rows = stmt
        .query_map(args.as_slice(), |row| row.get::<_, i64>(0))
        .context("failed to query chunks by prefix")?
        .collect::<std::result::Result<Vec<_>, _>>()
        .context("failed to collect prefix results")?;
    Ok(rows)
}

/// Smallest string that sorts strictly above every string starting with
/// `prefix`, giving the exclusive upper bound of a prefix range scan.
///
/// SQLite's default `BINARY` collation compares TEXT bytewise, and UTF-8 byte
/// order matches code point order, so incrementing the final character is
/// sufficient. Two cases need care: the successor of a code point may land in
/// the surrogate range, which is not a valid `char`, so it is skipped; and a
/// trailing `char::MAX` has no successor at all, so it is dropped and the
/// character before it is incremented instead.
///
/// Returns `None` when no upper bound exists (an empty prefix, or one made
/// entirely of `char::MAX`), in which case `chunk_id >= prefix` is already
/// exact and needs no upper bound.
fn prefix_upper_bound(prefix: &str) -> Option<String> {
    let mut chars: Vec<char> = prefix.chars().collect();
    while let Some(last) = chars.pop() {
        if let Some(next) = next_char(last) {
            let mut bound: String = chars.into_iter().collect();
            bound.push(next);
            return Some(bound);
        }
    }
    None
}

/// Next valid `char` above `c`, skipping the UTF-16 surrogate range.
fn next_char(c: char) -> Option<char> {
    let mut code = c as u32 + 1;
    while code <= char::MAX as u32 {
        if let Some(next) = char::from_u32(code) {
            return Some(next);
        }
        code += 1;
    }
    None
}

/// Register the sqlite-vec extension globally (idempotent).
fn register_sqlite_vec() {
    use std::sync::Once;
    static INIT: Once = Once::new();
    INIT.call_once(|| {
        unsafe {
            // sqlite-vec requires registering via auto_extension with a transmute
            // from the C-style init function pointer to the sqlite3 extension type.
            #[allow(clippy::missing_transmute_annotations)]
            let func = std::mem::transmute(sqlite3_vec_init as *const ());
            sqlite3_auto_extension(Some(func));
        }
    });
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(unix)]
    use std::os::unix::fs::MetadataExt;

    fn random_vector(dim: usize, seed: u64) -> Vec<f32> {
        // Simple deterministic pseudo-random for testing.
        let mut v = Vec::with_capacity(dim);
        let mut s = seed;
        for _ in 0..dim {
            s = s.wrapping_mul(6364136223846793005).wrapping_add(1);
            v.push(((s >> 33) as f32) / (u32::MAX as f32));
        }
        v
    }

    #[test]
    fn insert_and_count() {
        let store = VectorStore::open_in_memory(4).unwrap();
        store.insert("chunk1", &[1.0, 2.0, 3.0, 4.0]).unwrap();
        store.insert("chunk2", &[5.0, 6.0, 7.0, 8.0]).unwrap();
        assert_eq!(store.count().unwrap(), 2);
    }

    #[test]
    fn dimension_mismatch_rejected() {
        let store = VectorStore::open_in_memory(4).unwrap();
        let result = store.insert("chunk1", &[1.0, 2.0, 3.0]);
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("dimension mismatch")
        );
    }

    #[test]
    fn nearest_neighbor_self_query() {
        let store = VectorStore::open_in_memory(4).unwrap();
        let v1 = vec![1.0, 0.0, 0.0, 0.0];
        let v2 = vec![0.0, 1.0, 0.0, 0.0];
        let v3 = vec![0.0, 0.0, 1.0, 0.0];

        store.insert("c1", &v1).unwrap();
        store.insert("c2", &v2).unwrap();
        store.insert("c3", &v3).unwrap();

        // Query with v1 should return c1 as the closest.
        let results = store.search(&v1, 3).unwrap();
        assert!(!results.is_empty());
        assert_eq!(results[0].chunk_id, "c1");
        assert!(results[0].distance < 0.001); // Self-match should be ~0 distance.
    }

    #[test]
    fn search_pairs_each_chunk_id_with_its_own_distance() {
        // The rowid -> chunk_id mapping is now one batched query returning a
        // HashMap, so the results have to be re-projected back into the KNN
        // distance order. Insert so that rowid order (insertion order) is the
        // reverse of distance order, then assert the pairing — not just that
        // distances ascend, which holds for any implementation that emits one
        // result per hit in hit order.
        // Distances must be strictly increasing, or the KNN order between two
        // equidistant vectors is unspecified and the assertion below would be
        // decided by tie-breaking rather than by distance.
        //   near 0.0   mid ~0.894   far ~1.414   (L2 from the query)
        // Inserted in reverse of that order, so rowid order is not distance
        // order and a SQL-ordered result cannot pass by accident.
        let store = VectorStore::open_in_memory(4).unwrap();
        store.insert("far", &[0.0, 0.0, 1.0, 0.0]).unwrap();
        store.insert("mid", &[0.6, 0.8, 0.0, 0.0]).unwrap();
        store.insert("near", &[1.0, 0.0, 0.0, 0.0]).unwrap();

        let results = store.search(&[1.0, 0.0, 0.0, 0.0], 3).unwrap();
        assert_eq!(results.len(), 3);

        let order: Vec<&str> = results.iter().map(|r| r.chunk_id.as_str()).collect();
        assert_eq!(
            order,
            vec!["near", "mid", "far"],
            "results must come back in distance order, not rowid order"
        );
        assert!(
            results[0].distance < 0.001,
            "the self-match must keep its own near-zero distance, got {}",
            results[0].distance
        );
        for pair in results.windows(2) {
            assert!(
                pair[0].distance < pair[1].distance,
                "distances must strictly ascend, or the order is a tie-break: {:?}",
                results.iter().map(|r| r.distance).collect::<Vec<_>>()
            );
        }
    }

    #[test]
    fn chunk_id_batch_lookup_handles_4096_bound_parameters() {
        let store = VectorStore::open_in_memory(4).unwrap();
        for index in 1..=4096 {
            store
                .conn
                .execute(
                    "INSERT INTO chunk_id_map (rowid, chunk_id) VALUES (?1, ?2)",
                    params![index, format!("chunk-{index}")],
                )
                .unwrap();
        }

        let rowids: Vec<i64> = (1..=4096).collect();
        let mapped = store.chunk_ids_for_rowids(&rowids).unwrap();

        assert_eq!(mapped.len(), 4096);
        assert_eq!(mapped.get(&1).map(String::as_str), Some("chunk-1"));
        assert_eq!(mapped.get(&4096).map(String::as_str), Some("chunk-4096"));
    }

    #[test]
    fn search_reports_missing_chunk_id_mapping() {
        let store = VectorStore::open_in_memory(4).unwrap();
        store.insert("missing", &[1.0, 0.0, 0.0, 0.0]).unwrap();
        store
            .conn
            .execute(
                "DELETE FROM chunk_id_map WHERE chunk_id = ?1",
                params!["missing"],
            )
            .unwrap();

        let error = store.search(&[1.0, 0.0, 0.0, 0.0], 1).unwrap_err();
        assert!(
            error.to_string().contains("failed to map rowid"),
            "{error:#}"
        );
    }

    #[test]
    fn search_clamps_limit_above_sqlite_vec_knn_cap() {
        let store = VectorStore::open_in_memory(4).unwrap();
        for i in 0..10 {
            let v = vec![i as f32, 0.0, 0.0, 0.0];
            store.insert(&format!("c{i}"), &v).unwrap();
        }

        // Without clamping, sqlite-vec fails the query outright with
        // "k value in knn query too large".
        let results = store
            .search(&[5.0, 0.0, 0.0, 0.0], MAX_KNN_K + 1)
            .expect("oversized k must be clamped, not rejected");
        assert_eq!(results.len(), 10);
    }

    #[test]
    fn search_respects_limit() {
        let store = VectorStore::open_in_memory(4).unwrap();
        for i in 0..10 {
            let v = vec![i as f32, 0.0, 0.0, 0.0];
            store.insert(&format!("c{i}"), &v).unwrap();
        }

        let results = store.search(&[5.0, 0.0, 0.0, 0.0], 3).unwrap();
        assert_eq!(results.len(), 3);
    }

    #[test]
    fn delete_vector() {
        let store = VectorStore::open_in_memory(4).unwrap();
        store.insert("c1", &[1.0, 2.0, 3.0, 4.0]).unwrap();
        store.insert("c2", &[5.0, 6.0, 7.0, 8.0]).unwrap();
        assert_eq!(store.count().unwrap(), 2);

        assert!(store.delete("c1").unwrap());
        assert_eq!(store.count().unwrap(), 1);

        // Deleting non-existent returns false.
        assert!(!store.delete("nonexistent").unwrap());
    }

    #[test]
    fn clear_vectors() {
        let store = VectorStore::open_in_memory(4).unwrap();
        store.insert("c1", &[1.0, 2.0, 3.0, 4.0]).unwrap();
        store.insert("c2", &[5.0, 6.0, 7.0, 8.0]).unwrap();

        store.clear().unwrap();
        assert_eq!(store.count().unwrap(), 0);
    }

    #[test]
    fn batch_insert() {
        let store = VectorStore::open_in_memory(4).unwrap();
        let items: Vec<(&str, &[f32])> = vec![
            ("c1", &[1.0, 0.0, 0.0, 0.0]),
            ("c2", &[0.0, 1.0, 0.0, 0.0]),
            ("c3", &[0.0, 0.0, 1.0, 0.0]),
        ];
        store.insert_batch(&items).unwrap();
        assert_eq!(store.count().unwrap(), 3);
    }

    #[test]
    fn insert_batch_fresh_inserts_and_rejects_duplicates() {
        let store = VectorStore::open_in_memory(4).unwrap();
        let items: Vec<(&str, &[f32])> = vec![
            ("c1", &[1.0, 0.0, 0.0, 0.0]),
            ("c2", &[0.0, 1.0, 0.0, 0.0]),
            ("c3", &[0.0, 0.0, 1.0, 0.0]),
        ];
        store.insert_batch_fresh(&items).unwrap();
        assert_eq!(store.count().unwrap(), 3);

        let results = store.search(&[1.0, 0.0, 0.0, 0.0], 1).unwrap();
        assert_eq!(results[0].chunk_id, "c1");
        assert!(results[0].distance < 0.001);

        // A pre-existing chunk id must fail loudly, not silently remap.
        let dup: Vec<(&str, &[f32])> = vec![("c1", &[0.0, 0.0, 1.0, 0.0])];
        assert!(store.insert_batch_fresh(&dup).is_err());
        assert_eq!(store.count().unwrap(), 3);
    }

    #[test]
    fn higher_dim_vectors_work() {
        // Validate with 4096-dim (Qwen3 production dimensionality).
        let dim = 4096;
        let store = VectorStore::open_in_memory(dim).unwrap();

        let v1 = random_vector(dim, 42);
        let v2 = random_vector(dim, 123);
        let v3 = random_vector(dim, 456);

        store.insert("c1", &v1).unwrap();
        store.insert("c2", &v2).unwrap();
        store.insert("c3", &v3).unwrap();

        assert_eq!(store.count().unwrap(), 3);

        // Self-query should find the same vector.
        let results = store.search(&v1, 1).unwrap();
        assert_eq!(results[0].chunk_id, "c1");
    }

    #[test]
    fn query_dimension_mismatch_rejected() {
        let store = VectorStore::open_in_memory(4).unwrap();
        store.insert("c1", &[1.0, 2.0, 3.0, 4.0]).unwrap();

        let result = store.search(&[1.0, 2.0], 1);
        assert!(result.is_err());
    }

    #[test]
    fn insert_same_chunk_preserves_rowid_no_orphans() {
        // Verify that re-inserting a chunk_id updates the vector in-place
        // without creating orphaned rows (the AUTOINCREMENT fix).
        let store = VectorStore::open_in_memory(4).unwrap();

        // Insert initial vector.
        store.insert("c1", &[1.0, 0.0, 0.0, 0.0]).unwrap();
        assert_eq!(store.count().unwrap(), 1);

        // Re-insert same chunk_id with a different vector.
        store.insert("c1", &[0.0, 1.0, 0.0, 0.0]).unwrap();
        assert_eq!(
            store.count().unwrap(),
            1,
            "count should still be 1 after re-insert"
        );

        // Search should find the updated vector, not the old one.
        let results = store.search(&[0.0, 1.0, 0.0, 0.0], 1).unwrap();
        assert_eq!(results[0].chunk_id, "c1");
        assert!(results[0].distance < 0.001, "should match updated vector");

        // Old vector should not be a close match.
        let results_old = store.search(&[1.0, 0.0, 0.0, 0.0], 2).unwrap();
        // The only result should be c1, and it should have nonzero distance
        // from the old vector since we updated it.
        assert_eq!(results_old.len(), 1);
        assert!(
            results_old[0].distance > 0.5,
            "old vector should not match closely"
        );
    }

    #[test]
    fn batch_insert_same_chunk_no_orphans() {
        let store = VectorStore::open_in_memory(4).unwrap();

        // Insert initial batch.
        let items: Vec<(&str, &[f32])> =
            vec![("c1", &[1.0, 0.0, 0.0, 0.0]), ("c2", &[0.0, 1.0, 0.0, 0.0])];
        store.insert_batch(&items).unwrap();
        assert_eq!(store.count().unwrap(), 2);

        // Re-insert c1 with updated vector.
        let items2: Vec<(&str, &[f32])> = vec![("c1", &[0.0, 0.0, 1.0, 0.0])];
        store.insert_batch(&items2).unwrap();
        assert_eq!(store.count().unwrap(), 2, "count should still be 2");

        // Verify c1 has the updated vector.
        let results = store.search(&[0.0, 0.0, 1.0, 0.0], 1).unwrap();
        assert_eq!(results[0].chunk_id, "c1");
        assert!(results[0].distance < 0.001);
    }

    #[test]
    fn orphan_mapping_row_does_not_inflate_count() {
        let store = VectorStore::open_in_memory(4).unwrap();
        store.insert("real", &[1.0, 0.0, 0.0, 0.0]).unwrap();

        // Simulate a database written before single inserts became
        // transactional: a mapping row whose vector write never landed.
        store
            .conn
            .execute(
                "INSERT INTO chunk_id_map (chunk_id) VALUES (?1)",
                params!["ghost"],
            )
            .unwrap();

        assert_eq!(
            store.count().unwrap(),
            1,
            "count must report stored vectors, not mapping rows"
        );
        let results = store.search(&[1.0, 0.0, 0.0, 0.0], 5).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].chunk_id, "real");
    }

    #[test]
    fn batch_failure_rolls_back_earlier_items() {
        let store = VectorStore::open_in_memory(4).unwrap();
        let items: Vec<(&str, &[f32])> = vec![
            ("good-1", &[1.0, 0.0, 0.0, 0.0]),
            ("bad-dim", &[1.0, 0.0]), // dimension mismatch aborts mid-batch
        ];
        assert!(store.insert_batch(&items).is_err());

        // The earlier item's mapping row and vector are both gone: the
        // transaction rolls back when commit never runs.
        assert_eq!(store.count().unwrap(), 0);
        assert!(store.search(&[1.0, 0.0, 0.0, 0.0], 5).unwrap().is_empty());
    }

    #[test]
    fn delete_by_file_prefix() {
        let store = VectorStore::open_in_memory(4).unwrap();
        let items: Vec<(&str, &[f32])> = vec![
            ("src/main.rs:0", &[1.0, 0.0, 0.0, 0.0]),
            ("src/main.rs:1", &[0.0, 1.0, 0.0, 0.0]),
            ("src/lib.rs:0", &[0.0, 0.0, 1.0, 0.0]),
        ];
        store.insert_batch(&items).unwrap();
        assert_eq!(store.count().unwrap(), 3);

        // Delete all vectors for src/main.rs.
        let deleted = store.delete_by_file_prefix("src/main.rs:").unwrap();
        assert_eq!(deleted, 2);
        assert_eq!(store.count().unwrap(), 1);

        // Remaining vector should be src/lib.rs:0.
        let results = store.search(&[0.0, 0.0, 1.0, 0.0], 1).unwrap();
        assert_eq!(results[0].chunk_id, "src/lib.rs:0");
    }

    #[test]
    fn delete_by_file_prefix_does_not_treat_wildcards_as_patterns() {
        // `_` matches any character and `%` any sequence in LIKE. A range
        // predicate has no wildcards, but the paths that would have been
        // mis-matched are exactly the ones worth pinning.
        let store = VectorStore::open_in_memory(4).unwrap();
        let items: Vec<(&str, &[f32])> = vec![
            ("src/a_b.rs:0", &[1.0, 0.0, 0.0, 0.0]),
            ("src/axb.rs:0", &[0.0, 1.0, 0.0, 0.0]),
            ("src/100%.rs:0", &[0.0, 0.0, 1.0, 0.0]),
            ("src/100pct.rs:0", &[0.0, 0.0, 0.0, 1.0]),
        ];
        store.insert_batch(&items).unwrap();

        assert_eq!(store.delete_by_file_prefix("src/a_b.rs:").unwrap(), 1);
        assert_eq!(store.delete_by_file_prefix("src/100%.rs:").unwrap(), 1);
        assert_eq!(store.count().unwrap(), 2);

        // The literal-looking siblings survive.
        let remaining = store.search(&[0.0, 1.0, 0.0, 0.0], 4).unwrap();
        let ids: Vec<&str> = remaining.iter().map(|r| r.chunk_id.as_str()).collect();
        assert!(ids.contains(&"src/axb.rs:0"), "{ids:?}");
        assert!(ids.contains(&"src/100pct.rs:0"), "{ids:?}");
    }

    #[test]
    fn delete_by_file_prefix_stops_at_the_prefix_boundary() {
        // The upper bound must exclude the next sibling but include every
        // descendant of the prefix, however long.
        let store = VectorStore::open_in_memory(4).unwrap();
        let items: Vec<(&str, &[f32])> = vec![
            ("src/app.rs:0", &[1.0, 0.0, 0.0, 0.0]),
            ("src/app.rs:10", &[0.0, 1.0, 0.0, 0.0]),
            ("src/app2.rs:0", &[0.0, 0.0, 1.0, 0.0]),
            ("src/apq.rs:0", &[0.0, 0.0, 0.0, 1.0]),
        ];
        store.insert_batch(&items).unwrap();

        assert_eq!(store.delete_by_file_prefix("src/app.rs:").unwrap(), 2);
        assert_eq!(store.count().unwrap(), 2);
        let remaining = store.search(&[0.0, 0.0, 1.0, 0.0], 4).unwrap();
        let ids: Vec<&str> = remaining.iter().map(|r| r.chunk_id.as_str()).collect();
        assert!(ids.contains(&"src/app2.rs:0"), "{ids:?}");
        assert!(ids.contains(&"src/apq.rs:0"), "{ids:?}");
    }

    #[test]
    fn delete_by_file_prefix_handles_non_ascii_paths() {
        let store = VectorStore::open_in_memory(4).unwrap();
        let items: Vec<(&str, &[f32])> = vec![
            ("src/café.rs:0", &[1.0, 0.0, 0.0, 0.0]),
            ("src/cafz.rs:0", &[0.0, 1.0, 0.0, 0.0]),
        ];
        store.insert_batch(&items).unwrap();

        assert_eq!(store.delete_by_file_prefix("src/café.rs:").unwrap(), 1);
        assert_eq!(store.count().unwrap(), 1);
        let remaining = store.search(&[0.0, 1.0, 0.0, 0.0], 1).unwrap();
        assert_eq!(remaining[0].chunk_id, "src/cafz.rs:0");
    }

    #[test]
    fn prefix_upper_bound_covers_the_awkward_code_points() {
        assert_eq!(prefix_upper_bound("src/a"), Some("src/b".to_string()));
        assert_eq!(prefix_upper_bound("a:"), Some("a;".to_string()));

        // Successor lands in the surrogate range and must be skipped.
        let below_surrogates = char::from_u32(0xD7FF).unwrap();
        let bound = prefix_upper_bound(&below_surrogates.to_string()).unwrap();
        assert_eq!(bound.chars().next(), char::from_u32(0xE000));

        // A trailing char::MAX has no successor, so the previous character is
        // incremented and the max is dropped.
        let trailing_max = format!("a{}", char::MAX);
        assert_eq!(prefix_upper_bound(&trailing_max), Some("b".to_string()));

        // Nothing sorts above these.
        assert_eq!(prefix_upper_bound(""), None);
        assert_eq!(prefix_upper_bound(&char::MAX.to_string()), None);
    }

    #[test]
    fn prefix_delete_releases_transaction_for_subsequent_writes() {
        // A second connection can write after the prefix delete commits, which
        // confirms that the transaction was committed and released.
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("vectors.db");
        let store = VectorStore::open(&path, 4).unwrap();
        store
            .insert_batch(&[("src/a.rs:0", &[1.0, 0.0, 0.0, 0.0][..])])
            .unwrap();

        let other = VectorStore::open(&path, 4).unwrap();

        assert_eq!(store.delete_by_file_prefix("src/a.rs:").unwrap(), 1);
        assert_eq!(store.count().unwrap(), 0);

        // The second connection still works afterwards: the transaction was
        // committed and released, not left open.
        other
            .insert_batch(&[("src/a.rs:1", &[0.0, 1.0, 0.0, 0.0][..])])
            .unwrap();
        assert_eq!(other.count().unwrap(), 1);
        assert_eq!(other.delete_by_file_prefix("src/a.rs:").unwrap(), 1);
    }

    #[test]
    fn prefix_delete_takes_the_write_lock_before_it_scans() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("vectors.db");
        let store = VectorStore::open(&path, 4).unwrap();
        store
            .insert_batch(&[("src/a.rs:0", &[1.0, 0.0, 0.0, 0.0][..])])
            .unwrap();

        let competitor = VectorStore::open(&path, 4).unwrap();
        competitor
            .conn
            .execute_batch("PRAGMA busy_timeout=0")
            .unwrap();

        let deleted = store
            .delete_by_file_prefix_after_scan("src/a.rs:", || {
                let err = competitor
                    .insert_batch(&[("src/a.rs:1", &[0.0, 1.0, 0.0, 0.0][..])])
                    .expect_err("the prefix transaction must already hold the write lock");
                assert!(
                    err.chain().any(|cause| matches!(
                        cause.downcast_ref::<rusqlite::Error>(),
                        Some(rusqlite::Error::SqliteFailure(failure, _))
                            if matches!(
                                failure.code,
                                rusqlite::ErrorCode::DatabaseBusy
                                    | rusqlite::ErrorCode::DatabaseLocked
                            )
                    )),
                    "competing write should fail because the database is locked: {err:#}"
                );
            })
            .unwrap();

        assert_eq!(deleted, 1);
        assert_eq!(store.count().unwrap(), 0);
        competitor
            .insert_batch(&[("src/a.rs:1", &[0.0, 1.0, 0.0, 0.0][..])])
            .unwrap();
        assert_eq!(competitor.count().unwrap(), 1);
    }

    #[test]
    fn prefix_range_seeks_the_index_instead_of_scanning() {
        // The whole point of the range form: `LIKE ... ESCAPE` cannot use the
        // index, so a plan check is what actually guards this.
        let store = VectorStore::open_in_memory(4).unwrap();
        let mut stmt = store
            .conn
            .prepare(&format!(
                "EXPLAIN QUERY PLAN\n                 {PREFIX_RANGE_SQL}"
            ))
            .unwrap();
        let plan: Vec<String> = stmt
            .query_map(params!["a", "b"], |row| row.get::<_, String>(3))
            .unwrap()
            .map(|r| r.unwrap())
            .collect();
        let plan = plan.join(" | ");
        assert!(
            plan.contains("SEARCH")
                && (plan.contains("USING COVERING INDEX") || plan.contains("USING INDEX")),
            "prefix range must search using an index: {plan}"
        );
    }

    fn assert_search_results_match(
        left: &[VectorSearchResult],
        right: &[VectorSearchResult],
        tolerance: f64,
    ) {
        assert_eq!(left.len(), right.len());
        for (left, right) in left.iter().zip(right) {
            assert_eq!(left.chunk_id, right.chunk_id);
            assert!(
                (left.distance - right.distance).abs() <= tolerance,
                "distance mismatch: left={}, right={}",
                left.distance,
                right.distance
            );
        }
        for pair in left.windows(2) {
            assert!(pair[0].distance <= pair[1].distance);
        }
    }

    fn parity_fixture() -> Vec<(&'static str, &'static [f32])> {
        vec![
            ("a", &[1.0, 0.0, 0.0, 0.0]),
            ("b", &[0.0, 1.0, 0.0, 0.0]),
            ("c", &[-1.0, 0.0, 0.0, 0.0]),
            ("d", &[0.0, 0.0, 1.0, 0.0]),
            ("e", &[0.5, 0.2, 0.1, 0.0]),
        ]
    }

    #[test]
    fn flat_scan_matches_vec0_ids_and_distances() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("vectors.db");
        let flat = VectorStore::open_disk_with_mode(&path, 4, VectorScanMode::Flat).unwrap();
        flat.insert_batch(&parity_fixture()).unwrap();
        let vec0 = VectorStore::open_disk_with_mode(&path, 4, VectorScanMode::Vec0).unwrap();

        let flat_results = flat.search(&[0.9, 0.2, 0.1, 0.0], 5).unwrap();
        let vec0_results = vec0.search(&[0.9, 0.2, 0.1, 0.0], 5).unwrap();
        assert_search_results_match(&flat_results, &vec0_results, 1e-5);
    }

    #[test]
    fn flat_incremental_update_preserves_prefix_and_appends_at_row_offset() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("vectors.db");
        let store = VectorStore::open_disk_with_mode(&path, 4, VectorScanMode::Flat).unwrap();
        store.insert_batch(&parity_fixture()).unwrap();

        let flat_path = dir.path().join(FLAT_FILE_NAME);
        let before = fs::read(&flat_path).unwrap();
        #[cfg(unix)]
        let before_inode = fs::metadata(&flat_path).unwrap().ino();

        let updated = [0.2, 0.3, 0.4, 0.5];
        let appended = [1.5, -0.5, 0.25, 2.0];
        store
            .insert_batch(&[("c", &updated[..]), ("new", &appended[..])])
            .unwrap();

        let after = fs::read(&flat_path).unwrap();
        let changed_offset = vector_byte_offset(3, 4).unwrap() as usize;
        assert_eq!(
            &before[..changed_offset],
            &after[..changed_offset],
            "rows before the first changed rowid must not be rewritten"
        );

        let new_rowid = store.rowids_for_chunk_ids(&["new"])[0];
        let appended_offset = vector_byte_offset(new_rowid, 4).unwrap() as usize;
        let expected_appended: Vec<u8> = appended
            .iter()
            .flat_map(|value| value.to_le_bytes())
            .collect();
        assert_eq!(&after[appended_offset..], expected_appended.as_slice());
        assert_eq!(
            after.len(),
            expected_flat_bytes(4, new_rowid).unwrap() as usize
        );

        #[cfg(unix)]
        assert_eq!(
            before_inode,
            fs::metadata(&flat_path).unwrap().ino(),
            "incremental updates must retain the flat file inode"
        );
    }

    #[test]
    fn flat_incremental_mixed_updates_match_vec0() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("vectors.db");
        let flat = VectorStore::open_disk_with_mode(&path, 4, VectorScanMode::Flat).unwrap();
        flat.insert_batch(&parity_fixture()).unwrap();
        assert!(flat.delete("b").unwrap());
        assert_eq!(flat.delete_by_file_prefix("d").unwrap(), 1);

        let updated = [0.2, 0.3, 0.4, 0.5];
        let appended = [1.5, -0.5, 0.25, 2.0];
        flat.insert_batch(&[("c", &updated[..]), ("new", &appended[..])])
            .unwrap();

        let vec0 = VectorStore::open_disk_with_mode(&path, 4, VectorScanMode::Vec0).unwrap();
        for query in [[0.9, 0.2, 0.1, 0.0], [0.0, 0.0, 1.0, 0.0]] {
            let flat_results = flat.search(&query, 5).unwrap();
            let vec0_results = vec0.search(&query, 5).unwrap();
            assert_search_results_match(&flat_results, &vec0_results, 1e-5);
        }
    }

    #[test]
    fn flat_scan_excludes_tombstoned_rows() {
        let store = VectorStore::open_in_memory_with_mode(4, VectorScanMode::Flat).unwrap();
        store
            .insert_batch(&[
                ("near", &[1.0, 0.0, 0.0, 0.0][..]),
                ("far", &[0.0, 1.0, 0.0, 0.0][..]),
            ])
            .unwrap();
        assert!(store.delete("near").unwrap());

        let results = store.search(&[1.0, 0.0, 0.0, 0.0], 2).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].chunk_id, "far");
    }

    #[test]
    fn flat_delete_then_reinsert_same_chunk_id_uses_new_row() {
        let store = VectorStore::open_in_memory_with_mode(4, VectorScanMode::Flat).unwrap();
        store.insert("chunk", &[1.0, 0.0, 0.0, 0.0]).unwrap();
        let old_rowid = store.rowids_for_chunk_ids(&["chunk"])[0];
        assert!(store.delete("chunk").unwrap());
        store.insert("chunk", &[0.0, 1.0, 0.0, 0.0]).unwrap();
        let new_rowid = store.rowids_for_chunk_ids(&["chunk"])[0];

        assert!(new_rowid > old_rowid);
        let results = store.search(&[0.0, 1.0, 0.0, 0.0], 1).unwrap();
        assert_eq!(results[0].chunk_id, "chunk");
        assert!(results[0].distance < 1e-5);
    }

    #[test]
    fn flat_prefix_delete_then_reinsert_batch_restores_rows() {
        let store = VectorStore::open_in_memory_with_mode(4, VectorScanMode::Flat).unwrap();
        store
            .insert_batch(&[
                ("src/a.rs:0", &[1.0, 0.0, 0.0, 0.0][..]),
                ("src/a.rs:1", &[0.0, 1.0, 0.0, 0.0][..]),
                ("src/b.rs:0", &[0.0, 0.0, 1.0, 0.0][..]),
            ])
            .unwrap();
        assert_eq!(store.delete_by_file_prefix("src/a.rs:").unwrap(), 2);
        store
            .insert_batch(&[
                ("src/a.rs:2", &[1.0, 1.0, 0.0, 0.0][..]),
                ("src/a.rs:3", &[1.0, 0.0, 1.0, 0.0][..]),
            ])
            .unwrap();

        let results = store.search(&[1.0, 1.0, 0.0, 0.0], 5).unwrap();
        let ids: Vec<_> = results
            .iter()
            .map(|result| result.chunk_id.as_str())
            .collect();
        assert_eq!(ids.len(), 3);
        assert!(ids.contains(&"src/a.rs:2"));
        assert!(ids.contains(&"src/a.rs:3"));
        assert!(ids.contains(&"src/b.rs:0"));
        assert!(!ids.contains(&"src/a.rs:0"));
        assert!(!ids.contains(&"src/a.rs:1"));
    }

    #[test]
    fn flat_manifest_mismatch_rebuilds_from_vec0() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("vectors.db");
        let store = VectorStore::open_disk_with_mode(&path, 4, VectorScanMode::Flat).unwrap();
        store.insert_batch(&parity_fixture()).unwrap();
        drop(store);

        fs::write(dir.path().join(MANIFEST_FILE_NAME), b"not-json").unwrap();
        let reopened = VectorStore::open_disk_with_mode(&path, 4, VectorScanMode::Flat).unwrap();
        let results = reopened.search(&[0.9, 0.2, 0.1, 0.0], 5).unwrap();
        assert_eq!(results[0].chunk_id, "a");
        assert!(
            results
                .windows(2)
                .all(|pair| pair[0].distance <= pair[1].distance)
        );
        assert!(dir.path().join(FLAT_FILE_NAME).metadata().unwrap().len() > 0);
    }

    #[test]
    fn flat_and_vec0_in_memory_results_match() {
        let flat = VectorStore::open_in_memory_with_mode(4, VectorScanMode::Flat).unwrap();
        let vec0 = VectorStore::open_in_memory_with_mode(4, VectorScanMode::Vec0).unwrap();
        let fixture = parity_fixture();
        flat.insert_batch(&fixture).unwrap();
        vec0.insert_batch(&fixture).unwrap();

        let flat_results = flat.search(&[0.9, 0.2, 0.1, 0.0], 5).unwrap();
        let vec0_results = vec0.search(&[0.9, 0.2, 0.1, 0.0], 5).unwrap();
        assert_search_results_match(&flat_results, &vec0_results, 1e-5);
    }

    #[test]
    fn flat_scan_clamps_limit_above_knn_cap() {
        let store = VectorStore::open_in_memory_with_mode(4, VectorScanMode::Flat).unwrap();
        for index in 0..10 {
            store
                .insert(&format!("chunk-{index}"), &[index as f32, 0.0, 0.0, 0.0])
                .unwrap();
        }

        let results = store.search(&[5.0, 0.0, 0.0, 0.0], MAX_KNN_K + 1).unwrap();
        assert_eq!(results.len(), 10);
    }
}