frankensearch-storage 0.2.1

FrankenSQLite-backed metadata and embedding job storage for frankensearch
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
//! FTS5 alternative lexical search adapter.
//!
//! Uses `FrankenSQLite`'s built-in FTS5 implementation as an alternative to
//! Tantivy for BM25 full-text search. Both implement the split
//! [`frankensearch_core::LexicalRead`] / [`frankensearch_core::LexicalWrite`]
//! trait from `frankensearch-core`.
//!
//! # Advantages over Tantivy
//!
//! - Zero additional binary size (FTS5 is part of `FrankenSQLite`)
//! - MVCC concurrent reads and writes
//! - Single deployment artifact (one `.db` file)
//!
//! # Content mode
//!
//! The in-memory adapter below supports `Stored` and `Contentless` modes.
//! Persisted tables are inspected from their SQLite metadata so that `Stored`,
//! external-content, and contentless layouts are never inferred from caller
//! configuration.

use std::collections::HashMap;
use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind};
use std::sync::Arc;
use std::sync::Mutex;

use asupersync::Cx;
use frankensearch_core::error::{SearchError, SearchResult};
use frankensearch_core::traits::SearchFuture;
use frankensearch_core::types::{IndexableDocument, ScoreSource, ScoredResult};
use fsqlite::{AsyncConnection, Row};
use fsqlite_ext_fts5::{Fts5Table, snippet as fts5_snippet};
use fsqlite_types::cx::Cx as FsqliteCx;
use fsqlite_types::value::SqliteValue;
use serde::{Deserialize, Serialize};
use tracing::{debug, instrument, warn};

use crate::connection::{
    Storage, fsqlite_cx, map_storage_error_at, retry_transient_storage,
    retry_transient_storage_async, unretryable_rollback_error,
};
use crate::schema::PORTER_FTS5_REBUILD_TABLE;

// ─── Constants ──────────────────────────────────────────────────────────────

/// BM25 boost applied to title field matches (mirrors Tantivy adapter).
const TITLE_BOOST: f64 = 2.0;

/// Maximum query length in characters before truncation.
const MAX_QUERY_LENGTH: usize = 10_000;

/// Default snippet window size in tokens.
const DEFAULT_SNIPPET_TOKENS: usize = 20;

/// The on-disk marker written after applying the 0.2.1 Porter rebuild.
///
/// FrankenSQLite 0.2.1 changes Porter token handling. A prior Porter index
/// must be rebuilt from its complete content source; accepting an unmarked
/// table would make terms silently unfindable.
pub const PORTER_FTS5_REBUILD_VERSION: i64 = 1;

/// Column index: content (primary search field).
const COL_CONTENT: usize = 2;
/// Column index: `metadata_json` (stored, not searched).
const COL_METADATA: usize = 3;

// ─── Configuration ──────────────────────────────────────────────────────────

/// Content storage mode for the FTS5 index.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Fts5ContentMode {
    /// FTS5 stores its own copy of the content (supports snippets).
    #[default]
    Stored,
    /// FTS5 indexes a separately governed content table.
    External,
    /// Index-only mode — no content retrieval or snippet support.
    Contentless,
}

/// Tokenizer selection for the FTS5 index.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Fts5TokenizerChoice {
    /// Unicode-aware tokenizer with optional diacritic removal.
    #[default]
    Unicode61,
    /// English Porter stemming (wraps unicode61).
    Porter,
    /// Trigram tokenizer for substring matching (slower but more flexible).
    Trigram,
}

/// Configuration for the FTS5 lexical search adapter.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Fts5AdapterConfig {
    /// Content storage mode.
    #[serde(default)]
    pub content_mode: Fts5ContentMode,
    /// Tokenizer to use.
    #[serde(default)]
    pub tokenizer: Fts5TokenizerChoice,
    /// BM25 boost for title field matches.
    #[serde(default = "default_title_boost")]
    pub title_boost: f64,
}

fn default_title_boost() -> f64 {
    TITLE_BOOST
}

impl Default for Fts5AdapterConfig {
    fn default() -> Self {
        Self {
            content_mode: Fts5ContentMode::default(),
            tokenizer: Fts5TokenizerChoice::default(),
            title_boost: TITLE_BOOST,
        }
    }
}

// ─── Row ID mapping ─────────────────────────────────────────────────────────

/// Maps between string `doc_ids` and i64 rowids required by `Fts5Table`.
#[derive(Debug, Default)]
struct RowIdMap {
    doc_to_row: HashMap<String, i64>,
    row_to_doc: HashMap<i64, String>,
    next_rowid: i64,
}

impl RowIdMap {
    fn new() -> Self {
        Self {
            doc_to_row: HashMap::new(),
            row_to_doc: HashMap::new(),
            next_rowid: 1,
        }
    }

    fn get_or_assign(&mut self, doc_id: &str) -> i64 {
        if let Some(&rowid) = self.doc_to_row.get(doc_id) {
            return rowid;
        }
        let rowid = self.next_rowid;
        self.next_rowid += 1;
        self.doc_to_row.insert(doc_id.to_owned(), rowid);
        self.row_to_doc.insert(rowid, doc_id.to_owned());
        rowid
    }

    fn get_rowid(&self, doc_id: &str) -> Option<i64> {
        self.doc_to_row.get(doc_id).copied()
    }

    fn get_doc_id(&self, rowid: i64) -> Option<&str> {
        self.row_to_doc.get(&rowid).map(String::as_str)
    }

    fn remove(&mut self, doc_id: &str) -> Option<i64> {
        if let Some(rowid) = self.doc_to_row.remove(doc_id) {
            self.row_to_doc.remove(&rowid);
            Some(rowid)
        } else {
            None
        }
    }
}

// ─── FTS5 Lexical Search ────────────────────────────────────────────────────

/// FTS5-backed implementation of the split lexical capabilities.
///
/// Uses `FrankenSQLite`'s `Fts5Table` directly for full-text indexing
/// and BM25-ranked search. Thread-safe via internal `Mutex`.
pub struct Fts5LexicalSearch {
    table: Mutex<Fts5Table>,
    rowid_map: Mutex<RowIdMap>,
    config: Fts5AdapterConfig,
}

#[allow(clippy::missing_fields_in_debug)]
impl std::fmt::Debug for Fts5LexicalSearch {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Fts5LexicalSearch")
            .field("config", &self.config)
            .finish()
    }
}

impl Fts5LexicalSearch {
    /// Create a new FTS5 lexical search instance.
    #[must_use]
    pub fn new(config: Fts5AdapterConfig) -> Self {
        let columns = vec![
            "doc_id".to_owned(),
            "title".to_owned(),
            "content".to_owned(),
            "metadata_json".to_owned(),
        ];

        let table = Fts5Table::with_columns(columns);

        Self {
            table: Mutex::new(table),
            rowid_map: Mutex::new(RowIdMap::new()),
            config,
        }
    }

    /// Create a new FTS5 lexical search instance with default configuration.
    #[must_use]
    pub fn with_defaults() -> Self {
        Self::new(Fts5AdapterConfig::default())
    }

    /// Get the adapter configuration.
    #[must_use]
    pub fn config(&self) -> &Fts5AdapterConfig {
        &self.config
    }

    /// Truncate overly long queries to prevent pathological parsing.
    fn truncate_query(query: &str) -> &str {
        if query.len() <= MAX_QUERY_LENGTH {
            return query;
        }

        let Some((end, _)) = query.char_indices().nth(MAX_QUERY_LENGTH) else {
            return query;
        };
        warn!(
            original_len_bytes = query.len(),
            max_chars = MAX_QUERY_LENGTH,
            "fts5: query truncated"
        );
        &query[..end]
    }

    /// Build column values from an `IndexableDocument`.
    fn doc_to_columns(doc: &IndexableDocument) -> Vec<String> {
        let metadata_json = if doc.metadata.is_empty() {
            String::new()
        } else {
            serde_json::to_string(&doc.metadata).unwrap_or_default()
        };

        vec![
            doc.id.clone(),
            doc.title.clone().unwrap_or_default(),
            doc.content.clone(),
            metadata_json,
        ]
    }

    /// Search with snippet generation (richer result type).
    #[allow(clippy::significant_drop_tightening)]
    pub fn search_with_snippets(&self, query: &str, limit: usize) -> SearchResult<Vec<Fts5Hit>> {
        let query = Self::truncate_query(query);
        if query.trim().is_empty() {
            return Ok(Vec::new());
        }

        let table = self.table.lock().map_err(lock_error)?;
        let rowid_map = self.rowid_map.lock().map_err(lock_error)?;

        let search_results = table
            .search(query)
            .map_err(|e| SearchError::QueryParseError {
                query: query.to_owned(),
                detail: e.to_string(),
            })?;

        let query_terms: Vec<String> = query
            .split_whitespace()
            .map(|t| t.trim_matches('"').to_lowercase())
            .collect();

        let mut hits = Vec::with_capacity(search_results.len().min(limit));
        for (rank, (rowid, score)) in search_results.into_iter().take(limit).enumerate() {
            let doc_id = rowid_map.get_doc_id(rowid).unwrap_or("").to_owned();

            // FTS5 scores are negative (lower = better). Negate for positive.
            #[allow(clippy::cast_possible_truncation)]
            let bm25_score = (-score) as f32;

            // Generate snippet from content column if available.
            let snippet = table
                .get_document(rowid)
                .and_then(|cols| cols.get(COL_CONTENT))
                .map(|content| {
                    fts5_snippet(
                        content,
                        &query_terms,
                        "<b>",
                        "</b>",
                        "...",
                        DEFAULT_SNIPPET_TOKENS,
                    )
                });

            let metadata = table
                .get_document(rowid)
                .and_then(|cols| cols.get(COL_METADATA))
                .filter(|s| !s.is_empty())
                .and_then(|s| serde_json::from_str(s).ok());

            hits.push(Fts5Hit {
                doc_id,
                bm25_score,
                rank,
                snippet,
                metadata,
            });
        }

        debug!(hits = hits.len(), query, "fts5 search completed");
        Ok(hits)
    }

    /// Delete a single document by ID.
    ///
    /// Returns `true` if the document existed and was removed.
    pub fn delete_document(&self, doc_id: &str) -> SearchResult<bool> {
        let mut table = self.table.lock().map_err(lock_error)?;
        let mut rowid_map = self.rowid_map.lock().map_err(lock_error)?;

        let Some(rowid) = rowid_map.remove(doc_id) else {
            return Ok(false);
        };
        table.delete_document(rowid);
        Ok(true)
    }

    /// Delete all indexed documents.
    pub fn clear(&self) -> SearchResult<()> {
        let mut table = self.table.lock().map_err(lock_error)?;
        let mut rowid_map = self.rowid_map.lock().map_err(lock_error)?;

        // Collect all rowids to delete.
        let rowids: Vec<i64> = rowid_map.row_to_doc.keys().copied().collect();
        for rowid in rowids {
            table.delete_document(rowid);
        }
        rowid_map.doc_to_row.clear();
        rowid_map.row_to_doc.clear();

        debug!("fts5: cleared all documents");
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct PersistedFts5Metadata {
    content_mode: Fts5ContentMode,
    tokenizer: String,
}

/// A read-only, persisted FTS5 search path.
///
/// Unlike [`Fts5LexicalSearch`], this reader executes `MATCH` against the
/// virtual table in the supplied [`Storage`] database. It revalidates the
/// table DDL and the governed rebuild marker on every open and search, so an
/// unrebuilt Porter index cannot be queried through an in-memory side path.
/// The table must expose `doc_id` and `metadata_json` columns; those are the
/// persisted storage contract required to produce `ScoredResult` values.
pub struct PersistedFts5LexicalSearch {
    storage: Arc<Storage>,
    table_name: String,
    config: Fts5AdapterConfig,
}

impl std::fmt::Debug for PersistedFts5LexicalSearch {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("PersistedFts5LexicalSearch")
            .field("table_name", &self.table_name)
            .field("config", &self.config)
            .finish_non_exhaustive()
    }
}

impl PersistedFts5LexicalSearch {
    /// Open a governed persisted Porter FTS5 table.
    ///
    /// This checks the table's actual `sqlite_master` DDL rather than trusting
    /// an application-supplied content mode or tokenizer. It also requires the
    /// committed rebuild marker before returning a searchable reader.
    pub async fn open(cx: &Cx, storage: Arc<Storage>, table_name: &str) -> SearchResult<Self> {
        let table_name = validated_fts5_identifier(table_name)?;
        let fsqlite_cx = fsqlite_cx(cx);
        let metadata =
            ensure_porter_fts5_ready(&fsqlite_cx, storage.connection(), &table_name).await?;
        Ok(Self {
            storage,
            table_name,
            config: Fts5AdapterConfig {
                content_mode: metadata.content_mode,
                tokenizer: Fts5TokenizerChoice::Porter,
                title_boost: TITLE_BOOST,
            },
        })
    }

    /// Return the verified table configuration read from persisted metadata.
    #[must_use]
    pub fn config(&self) -> &Fts5AdapterConfig {
        &self.config
    }

    /// Query the current persisted table count within a caller-owned runtime
    /// context.
    ///
    /// This is the authoritative alternative to the synchronous
    /// [`frankensearch_core::LexicalRead::doc_count`] capability, which cannot
    /// pin or await this adapter's live FrankenSQLite generation.
    ///
    /// # Errors
    ///
    /// Returns a storage or result-shape error when the current table count
    /// cannot be established.
    pub async fn current_doc_count(&self, cx: &Cx) -> SearchResult<usize> {
        let fsqlite_cx = fsqlite_cx(cx);
        retry_transient_storage_async(
            cx,
            || {
                persisted_fts5_verified_doc_count(
                    &fsqlite_cx,
                    self.storage.connection(),
                    &self.table_name,
                )
            },
            "persisted fts5 count",
        )
        .await
    }
}

impl frankensearch_core::LexicalRead for PersistedFts5LexicalSearch {
    #[instrument(skip_all, fields(table = %self.table_name, query = %query, limit = limit))]
    fn search<'a>(
        &'a self,
        cx: &'a Cx,
        query: &'a str,
        limit: usize,
    ) -> SearchFuture<'a, Vec<ScoredResult>> {
        Box::pin(async move {
            let query = Fts5LexicalSearch::truncate_query(query);
            if query.trim().is_empty() {
                return Ok(Vec::new());
            }

            let fsqlite_cx = fsqlite_cx(cx);
            let limit = i64::try_from(limit).map_err(|_| SearchError::InvalidConfig {
                field: "fts5.limit".to_owned(),
                value: limit.to_string(),
                reason: "does not fit SQLite's signed integer limit".to_owned(),
            })?;
            let params = [
                SqliteValue::Text(query.to_owned().into()),
                SqliteValue::Integer(limit),
            ];
            let sql = format!(
                "SELECT doc_id, metadata_json, bm25({0}) FROM {0} \
                         WHERE {0} MATCH ?1 ORDER BY bm25({0}), rowid LIMIT ?2;",
                self.table_name
            );
            retry_transient_storage_async(
                cx,
                || async {
                    // This is intentionally in the live data path, not only in
                    // the rebuild helper: external DDL or marker changes fail
                    // the search closed before MATCH can return a stale Porter
                    // result.
                    ensure_porter_fts5_ready(
                        &fsqlite_cx,
                        self.storage.connection(),
                        &self.table_name,
                    )
                    .await?;
                    let rows = self
                        .storage
                        .connection()
                        .query_with_params(&fsqlite_cx, &sql, &params)
                        .await
                        .map_err(|error| {
                            map_storage_error_at("persisted Porter FTS5 search", error)
                        })?;
                    rows.iter()
                        .map(decode_persisted_fts5_row)
                        .collect::<SearchResult<Vec<_>>>()
                },
                "persisted fts5 search",
            )
            .await
        })
    }

    fn doc_count(&self) -> SearchResult<usize> {
        Err(SearchError::SubsystemError {
            subsystem: "fts5",
            source: "the persisted FTS5 adapter has no synchronous pinned-generation count; \
                     call PersistedFts5LexicalSearch::current_doc_count with the caller's Cx"
                .into(),
        })
    }
}

/// Rebuild a persisted Porter FTS5 table for `FrankenSQLite` 0.3.
///
/// The table's own DDL decides its content mode. Ordinary stored and external
/// tables use FTS5's `rebuild` command; contentless tables are rejected because
/// authoritative text and original rowids must be re-ingested instead.
///
/// Rebuild and marker promotion share one synchronous worker transaction.
/// Transient `FrankenSQLite` errors retry the whole transaction after a
/// successful rollback. A failed rollback is not retried. Panic still
/// rolls back once and resumes unwinding.
pub async fn rebuild_porter_fts5_table(
    cx: &Cx,
    conn: &AsyncConnection,
    table_name: &str,
) -> SearchResult<()> {
    let table_name = validated_fts5_identifier(table_name)?;
    let fsqlite_cx = fsqlite_cx(cx);
    let metadata = read_persisted_fts5_metadata(&fsqlite_cx, conn, &table_name).await?;
    ensure_rebuildable_porter_fts5(&table_name, &metadata)?;

    match read_porter_fts5_rebuild_marker(&fsqlite_cx, conn, &table_name).await? {
        Some(PORTER_FTS5_REBUILD_VERSION) => return Ok(()),
        Some(version) if version > PORTER_FTS5_REBUILD_VERSION => {
            return Err(SearchError::InvalidConfig {
                field: "fts5.rebuild_version".to_owned(),
                value: version.to_string(),
                reason:
                    "database was rebuilt by a newer Porter FTS5 migration; refusing a downgrade"
                        .to_owned(),
            });
        }
        Some(_) | None => {}
    }

    cx.checkpoint().map_err(|error| SearchError::Cancelled {
        phase: "porter fts5 rebuild".to_owned(),
        reason: cx
            .cancel_reason()
            .map_or_else(|| error.to_string(), |reason| reason.to_string()),
    })?;
    retry_transient_storage(
        || rebuild_porter_fts5_table_once(conn, &table_name),
        "porter fts5 rebuild",
    )
}

fn rebuild_porter_fts5_table_once(conn: &AsyncConnection, table_name: &str) -> SearchResult<()> {
    conn.execute_sync("BEGIN IMMEDIATE;")
        .map_err(|error| map_storage_error_at("begin Porter FTS5 rebuild", error))?;

    let result = catch_unwind(AssertUnwindSafe(|| -> SearchResult<()> {
        let rebuild_sql = format!("INSERT INTO {table_name}({table_name}) VALUES ('rebuild');");
        conn.execute_sync(&rebuild_sql)
            .map_err(|error| map_storage_error_at("rebuild Porter FTS5 table", error))?;

        let params = [
            SqliteValue::Text(table_name.to_owned().into()),
            SqliteValue::Integer(PORTER_FTS5_REBUILD_VERSION),
        ];
        conn.execute_with_params_sync(
            &format!(
                "INSERT INTO {PORTER_FTS5_REBUILD_TABLE} (table_name, rebuild_version) \
                 VALUES (?1, ?2) \
                 ON CONFLICT(table_name) DO UPDATE SET rebuild_version = excluded.rebuild_version;"
            ),
            &params,
        )
        .map_err(|error| map_storage_error_at("write Porter FTS5 rebuild marker", error))?;
        Ok(())
    }));

    match result {
        Ok(Ok(())) => conn.commit_transaction_sync().map_err(|commit_error| {
            match conn.rollback_transaction_sync() {
                Ok(()) => map_storage_error_at("commit Porter FTS5 rebuild", commit_error),
                Err(rollback_error) => {
                    warn!(
                        error = %rollback_error,
                        "rollback failed after Porter FTS5 rebuild commit error"
                    );
                    unretryable_rollback_error(
                        &map_storage_error_at("commit Porter FTS5 rebuild", commit_error),
                        &rollback_error,
                    )
                }
            }
        }),
        Ok(Err(error)) => match conn.rollback_transaction_sync() {
            Ok(()) => Err(error),
            Err(rollback_error) => {
                warn!(
                    error = %rollback_error,
                    "rollback failed after Porter FTS5 rebuild error"
                );
                Err(unretryable_rollback_error(&error, &rollback_error))
            }
        },
        Err(payload) => {
            if let Err(rollback_error) = conn.rollback_transaction_sync() {
                warn!(
                    error = %rollback_error,
                    "rollback failed during Porter FTS5 rebuild panic recovery"
                );
            }
            resume_unwind(payload);
        }
    }
}

fn validated_fts5_identifier(table_name: &str) -> SearchResult<String> {
    let mut chars = table_name.chars();
    let valid_start = chars
        .next()
        .is_some_and(|ch| ch == '_' || ch.is_ascii_alphabetic());
    let valid_rest = chars.all(|ch| ch == '_' || ch.is_ascii_alphanumeric());
    if valid_start && valid_rest {
        Ok(table_name.to_owned())
    } else {
        Err(SearchError::InvalidConfig {
            field: "fts5.table_name".to_owned(),
            value: table_name.to_owned(),
            reason: "must be a SQLite ASCII identifier before it is interpolated into a rebuild statement".to_owned(),
        })
    }
}

async fn ensure_porter_fts5_ready(
    fsqlite_cx: &FsqliteCx,
    conn: &AsyncConnection,
    table_name: &str,
) -> SearchResult<PersistedFts5Metadata> {
    let metadata = read_persisted_fts5_metadata(fsqlite_cx, conn, table_name).await?;
    ensure_rebuildable_porter_fts5(table_name, &metadata)?;
    ensure_porter_fts5_rebuild_version(
        table_name,
        read_porter_fts5_rebuild_marker(fsqlite_cx, conn, table_name).await?,
    )?;
    Ok(metadata)
}

fn ensure_porter_fts5_rebuild_version(
    table_name: &str,
    rebuild_version: Option<i64>,
) -> SearchResult<()> {
    match rebuild_version {
        Some(PORTER_FTS5_REBUILD_VERSION) => Ok(()),
        Some(version) if version > PORTER_FTS5_REBUILD_VERSION => Err(SearchError::InvalidConfig {
            field: "fts5.rebuild_version".to_owned(),
            value: version.to_string(),
            reason: "database was rebuilt by a newer Porter FTS5 migration; refusing a downgrade".to_owned(),
        }),
        Some(version) => Err(SearchError::InvalidConfig {
            field: "fts5.rebuild_version".to_owned(),
            value: version.to_string(),
            reason: "Porter FTS5 table has an obsolete rebuild marker; rebuild must complete and commit before search".to_owned(),
        }),
        None => Err(SearchError::InvalidConfig {
            field: "fts5.rebuild_version".to_owned(),
            value: table_name.to_owned(),
            reason: "Porter FTS5 table has no committed rebuild marker; refusing potentially stale search results".to_owned(),
        }),
    }
}

async fn read_persisted_fts5_metadata(
    fsqlite_cx: &FsqliteCx,
    conn: &AsyncConnection,
    table_name: &str,
) -> SearchResult<PersistedFts5Metadata> {
    let params = [SqliteValue::Text(table_name.to_owned().into())];
    let rows = conn
        .query_with_params(
            fsqlite_cx,
            "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?1;",
            &params,
        )
        .await
        .map_err(|error| map_storage_error_at("read persisted FTS5 table metadata", error))?;

    let [row] = rows.as_slice() else {
        return Err(persisted_fts5_metadata_error(
            table_name,
            if rows.is_empty() {
                "table is absent from sqlite_master"
            } else {
                "sqlite_master returned more than one table definition"
            },
        ));
    };
    let Some(SqliteValue::Text(sql)) = row.get(0) else {
        return Err(persisted_fts5_metadata_error(
            table_name,
            "sqlite_master.sql is not text",
        ));
    };

    parse_persisted_fts5_metadata(table_name, sql.as_ref())
}

async fn read_porter_fts5_rebuild_marker(
    fsqlite_cx: &FsqliteCx,
    conn: &AsyncConnection,
    table_name: &str,
) -> SearchResult<Option<i64>> {
    let params = [SqliteValue::Text(table_name.to_owned().into())];
    let rows = conn
        .query_with_params(
            fsqlite_cx,
            &format!(
                "SELECT rebuild_version FROM {PORTER_FTS5_REBUILD_TABLE} WHERE table_name = ?1;"
            ),
            &params,
        )
        .await
        .map_err(|error| map_storage_error_at("read Porter FTS5 rebuild marker", error))?;

    let row = match rows.as_slice() {
        [] => return Ok(None),
        [row] => row,
        _ => {
            return Err(SearchError::InvalidConfig {
                field: "fts5.rebuild_version".to_owned(),
                value: table_name.to_owned(),
                reason: "governed Porter FTS5 marker table contains duplicate rows for one table"
                    .to_owned(),
            });
        }
    };
    match row.get(0) {
        Some(SqliteValue::Integer(version)) => Ok(Some(*version)),
        Some(value) => Err(SearchError::InvalidConfig {
            field: "fts5.rebuild_version".to_owned(),
            value: format!("{table_name}: {value:?}"),
            reason: "refusing to query a Porter FTS5 table whose governed rebuild marker is not an integer".to_owned(),
        }),
        None => Err(SearchError::InvalidConfig {
            field: "fts5.rebuild_version".to_owned(),
            value: format!("{table_name}: missing column"),
            reason: "refusing to query a Porter FTS5 table whose governed rebuild marker row is malformed".to_owned(),
        }),
    }
}

fn ensure_rebuildable_porter_fts5(
    table_name: &str,
    metadata: &PersistedFts5Metadata,
) -> SearchResult<()> {
    if !metadata
        .tokenizer
        .split_ascii_whitespace()
        .next()
        .is_some_and(|tokenizer| tokenizer.eq_ignore_ascii_case("porter"))
    {
        return Err(SearchError::InvalidConfig {
            field: "fts5.tokenize".to_owned(),
            value: metadata.tokenizer.clone(),
            reason: format!(
                "{table_name} is not a Porter FTS5 table according to its persisted sqlite_master definition"
            ),
        });
    }
    if metadata.content_mode == Fts5ContentMode::Contentless {
        return Err(SearchError::InvalidConfig {
            field: "fts5.content_mode".to_owned(),
            value: "contentless".to_owned(),
            reason: "Porter FTS5 rebuild requires authoritative text and original rowids; recreate the contentless table and re-ingest source documents rather than rebuilding from an index or preview".to_owned(),
        });
    }
    Ok(())
}

async fn persisted_fts5_verified_doc_count(
    fsqlite_cx: &FsqliteCx,
    conn: &AsyncConnection,
    table_name: &str,
) -> SearchResult<usize> {
    let params = [SqliteValue::Text(table_name.to_owned().into())];
    let rows = conn
        .query_with_params(
            fsqlite_cx,
            &format!(
                "SELECT schema_entry.sql, \
                        (SELECT COUNT(*) FROM {PORTER_FTS5_REBUILD_TABLE} \
                         WHERE table_name = ?1), \
                        (SELECT MIN(rebuild_version) FROM {PORTER_FTS5_REBUILD_TABLE} \
                         WHERE table_name = ?1), \
                        (SELECT COUNT(*) FROM {table_name}) \
                 FROM sqlite_master AS schema_entry \
                 WHERE schema_entry.type = 'table' AND schema_entry.name = ?1;"
            ),
            &params,
        )
        .await
        .map_err(|error| {
            map_storage_error_at("verify and count persisted Porter FTS5 documents", error)
        })?;
    let [row] = rows.as_slice() else {
        return Err(persisted_fts5_metadata_error(
            table_name,
            if rows.is_empty() {
                "table is absent from sqlite_master"
            } else {
                "verification and COUNT(*) returned more than one row"
            },
        ));
    };
    let Some(SqliteValue::Text(sql)) = row.get(0) else {
        return Err(persisted_fts5_metadata_error(
            table_name,
            "sqlite_master.sql is not text",
        ));
    };
    let metadata = parse_persisted_fts5_metadata(table_name, sql.as_ref())?;
    ensure_rebuildable_porter_fts5(table_name, &metadata)?;

    let Some(SqliteValue::Integer(marker_count)) = row.get(1) else {
        return Err(SearchError::InvalidConfig {
            field: "fts5.rebuild_version".to_owned(),
            value: table_name.to_owned(),
            reason: "governed Porter FTS5 marker cardinality is not an integer".to_owned(),
        });
    };
    match *marker_count {
        0 => ensure_porter_fts5_rebuild_version(table_name, None)?,
        1 => {}
        count => {
            return Err(SearchError::InvalidConfig {
                field: "fts5.rebuild_version".to_owned(),
                value: format!("{table_name}: {count} rows"),
                reason: "governed Porter FTS5 marker table contains duplicate rows for one table"
                    .to_owned(),
            });
        }
    }

    let rebuild_version = match row.get(2) {
        Some(SqliteValue::Integer(version)) => Some(*version),
        Some(SqliteValue::Null) => None,
        Some(value) => {
            return Err(SearchError::InvalidConfig {
                field: "fts5.rebuild_version".to_owned(),
                value: format!("{table_name}: {value:?}"),
                reason: "refusing to count a Porter FTS5 table whose governed rebuild marker is not an integer".to_owned(),
            });
        }
        None => {
            return Err(persisted_fts5_metadata_error(
                table_name,
                "verification result is missing the rebuild marker column",
            ));
        }
    };
    ensure_porter_fts5_rebuild_version(table_name, rebuild_version)?;

    let Some(SqliteValue::Integer(count)) = row.get(3) else {
        return Err(persisted_fts5_metadata_error(
            table_name,
            "COUNT(*) did not return an integer",
        ));
    };
    usize::try_from(*count).map_err(|_| {
        persisted_fts5_metadata_error(table_name, "COUNT(*) is negative or does not fit usize")
    })
}

fn decode_persisted_fts5_row(row: &Row) -> SearchResult<ScoredResult> {
    let Some(SqliteValue::Text(doc_id)) = row.get(0) else {
        return Err(persisted_fts5_result_error("doc_id must be non-NULL TEXT"));
    };
    if doc_id.is_empty() {
        return Err(persisted_fts5_result_error("doc_id must not be empty"));
    }

    let metadata = match row.get(1) {
        None => {
            return Err(persisted_fts5_result_error(
                "metadata_json column is missing",
            ));
        }
        Some(SqliteValue::Null) => None,
        Some(SqliteValue::Text(text)) if text.is_empty() => None,
        Some(SqliteValue::Text(text)) => {
            Some(
                serde_json::from_str(text).map_err(|error| SearchError::InvalidConfig {
                    field: "fts5.metadata_json".to_owned(),
                    value: error.to_string(),
                    reason: "persisted FTS5 metadata_json is not valid JSON".to_owned(),
                })?,
            )
        }
        Some(value) => {
            return Err(persisted_fts5_result_error(&format!(
                "metadata_json must be TEXT or NULL, got {value:?}"
            )));
        }
    };

    let raw_score = match row.get(2) {
        Some(SqliteValue::Float(score)) => *score,
        Some(SqliteValue::Integer(score)) => *score as f64,
        Some(value) => {
            return Err(persisted_fts5_result_error(&format!(
                "bm25 score must be REAL or INTEGER, got {value:?}"
            )));
        }
        None => return Err(persisted_fts5_result_error("bm25 score column is missing")),
    };
    let score = -raw_score;
    if !score.is_finite() || score > f64::from(f32::MAX) {
        return Err(persisted_fts5_result_error(
            "bm25 score is non-finite or does not fit f32",
        ));
    }
    #[allow(clippy::cast_possible_truncation)]
    let score = score as f32;

    Ok(ScoredResult {
        doc_id: doc_id.to_string().into(),
        score,
        source: ScoreSource::Lexical,
        index: None,
        fast_score: None,
        quality_score: None,
        lexical_score: Some(score),
        rerank_score: None,
        explanation: None,
        metadata,
    })
}

fn parse_persisted_fts5_metadata(
    table_name: &str,
    create_sql: &str,
) -> SearchResult<PersistedFts5Metadata> {
    let mut cursor = Fts5DdlCursor::new(create_sql);
    cursor.expect_keyword("CREATE", table_name)?;
    cursor.expect_keyword("VIRTUAL", table_name)?;
    cursor.expect_keyword("TABLE", table_name)?;
    if cursor.consume_keyword("IF") {
        cursor.expect_keyword("NOT", table_name)?;
        cursor.expect_keyword("EXISTS", table_name)?;
    }
    let declared_table = cursor
        .identifier()
        .ok_or_else(|| persisted_fts5_metadata_error(table_name, "missing virtual table name"))?;
    if !declared_table.eq_ignore_ascii_case(table_name) {
        return Err(persisted_fts5_metadata_error(
            table_name,
            "sqlite_master definition declares a different table name",
        ));
    }
    cursor.expect_keyword("USING", table_name)?;
    cursor.expect_keyword("FTS5", table_name)?;
    let arguments = cursor.parenthesized(table_name)?;
    cursor.finish(table_name)?;

    let mut content_mode = Fts5ContentMode::Stored;
    let mut saw_content = false;
    let mut tokenizer = None;
    for argument in split_fts5_arguments(arguments, table_name)? {
        let Some((key, value)) = split_fts5_option(argument, table_name)? else {
            continue;
        };
        if key.eq_ignore_ascii_case("content") {
            if saw_content {
                return Err(persisted_fts5_metadata_error(
                    table_name,
                    "duplicate content option",
                ));
            }
            saw_content = true;
            content_mode = if parse_fts5_option_value(value, table_name)?.is_empty() {
                Fts5ContentMode::Contentless
            } else {
                Fts5ContentMode::External
            };
        } else if key.eq_ignore_ascii_case("tokenize") {
            if tokenizer.is_some() {
                return Err(persisted_fts5_metadata_error(
                    table_name,
                    "duplicate tokenize option",
                ));
            }
            tokenizer = Some(parse_fts5_option_value(value, table_name)?);
        }
    }

    let tokenizer = tokenizer.ok_or_else(|| {
        persisted_fts5_metadata_error(table_name, "missing explicit tokenize option")
    })?;
    Ok(PersistedFts5Metadata {
        content_mode,
        tokenizer,
    })
}

fn split_fts5_arguments<'a>(arguments: &'a str, table_name: &str) -> SearchResult<Vec<&'a str>> {
    let mut items = Vec::new();
    let mut start = 0;
    let mut depth = 0_u32;
    let mut quote = None;
    let bytes = arguments.as_bytes();
    let mut index = 0;
    while index < bytes.len() {
        let byte = bytes[index];
        if let Some(delimiter) = quote {
            if byte == delimiter {
                if index + 1 < bytes.len() && bytes[index + 1] == delimiter {
                    index += 2;
                    continue;
                }
                quote = None;
            }
        } else {
            match byte {
                b'\'' | b'\"' | b'`' => quote = Some(byte),
                b'[' => quote = Some(b']'),
                b'(' => depth = depth.saturating_add(1),
                b')' => {
                    if depth == 0 {
                        return Err(persisted_fts5_metadata_error(
                            table_name,
                            "unbalanced parenthesis in FTS5 arguments",
                        ));
                    }
                    depth -= 1;
                }
                b',' if depth == 0 => {
                    items.push(arguments[start..index].trim());
                    start = index + 1;
                }
                _ => {}
            }
        }
        index += 1;
    }
    if quote.is_some() || depth != 0 {
        return Err(persisted_fts5_metadata_error(
            table_name,
            "unterminated quote or parenthesis in FTS5 arguments",
        ));
    }
    let final_item = arguments[start..].trim();
    if final_item.is_empty() {
        return Err(persisted_fts5_metadata_error(
            table_name,
            "empty FTS5 argument",
        ));
    }
    items.push(final_item);
    Ok(items)
}

fn split_fts5_option<'a>(
    argument: &'a str,
    table_name: &str,
) -> SearchResult<Option<(&'a str, &'a str)>> {
    let mut quote = None;
    let mut depth = 0_u32;
    let bytes = argument.as_bytes();
    let mut index = 0;
    while index < bytes.len() {
        let byte = bytes[index];
        if let Some(delimiter) = quote {
            if byte == delimiter {
                if index + 1 < bytes.len() && bytes[index + 1] == delimiter {
                    index += 2;
                    continue;
                }
                quote = None;
            }
        } else {
            match byte {
                b'\'' | b'\"' | b'`' => quote = Some(byte),
                b'[' => quote = Some(b']'),
                b'(' => depth = depth.saturating_add(1),
                b')' => {
                    if depth == 0 {
                        return Err(persisted_fts5_metadata_error(
                            table_name,
                            "unbalanced parenthesis in an FTS5 argument",
                        ));
                    }
                    depth -= 1;
                }
                b'=' if depth == 0 => {
                    let key = argument[..index].trim();
                    let value = argument[index + 1..].trim();
                    if key.is_empty() || value.is_empty() {
                        return Err(persisted_fts5_metadata_error(
                            table_name,
                            "FTS5 option has an empty key or value",
                        ));
                    }
                    return Ok(Some((key, value)));
                }
                _ => {}
            }
        }
        index += 1;
    }
    if quote.is_some() || depth != 0 {
        return Err(persisted_fts5_metadata_error(
            table_name,
            "unterminated quote or parenthesis in an FTS5 argument",
        ));
    }
    Ok(None)
}

fn parse_fts5_option_value(value: &str, table_name: &str) -> SearchResult<String> {
    let value = value.trim();
    let Some(quote) = value
        .as_bytes()
        .first()
        .copied()
        .filter(|quote| matches!(quote, b'\'' | b'\"' | b'`'))
    else {
        return Ok(value.to_ascii_lowercase());
    };
    if value.len() < 2 {
        return Err(persisted_fts5_metadata_error(
            table_name,
            "unterminated quoted FTS5 option value",
        ));
    }

    let mut decoded = String::new();
    let bytes = value.as_bytes();
    let mut index = 1;
    while index < bytes.len() {
        if bytes[index] == quote {
            if index + 1 < bytes.len() && bytes[index + 1] == quote {
                decoded.push(quote as char);
                index += 2;
                continue;
            }
            if !value[index + 1..].trim().is_empty() {
                return Err(persisted_fts5_metadata_error(
                    table_name,
                    "trailing text after quoted FTS5 option value",
                ));
            }
            return Ok(decoded.to_ascii_lowercase());
        }
        let Some(character) = value[index..].chars().next() else {
            break;
        };
        decoded.push(character);
        index += character.len_utf8();
    }
    Err(persisted_fts5_metadata_error(
        table_name,
        "unterminated quoted FTS5 option value",
    ))
}

struct Fts5DdlCursor<'a> {
    source: &'a str,
    index: usize,
}

impl<'a> Fts5DdlCursor<'a> {
    const fn new(source: &'a str) -> Self {
        Self { source, index: 0 }
    }

    fn skip_whitespace(&mut self) {
        while self
            .source
            .as_bytes()
            .get(self.index)
            .is_some_and(u8::is_ascii_whitespace)
        {
            self.index += 1;
        }
    }

    fn consume_keyword(&mut self, keyword: &str) -> bool {
        self.skip_whitespace();
        let remaining = &self.source[self.index..];
        let Some(candidate) = remaining.get(..keyword.len()) else {
            return false;
        };
        if !candidate.eq_ignore_ascii_case(keyword) {
            return false;
        }
        if remaining
            .as_bytes()
            .get(keyword.len())
            .is_some_and(|byte| byte.is_ascii_alphanumeric() || *byte == b'_')
        {
            return false;
        }
        self.index += keyword.len();
        true
    }

    fn expect_keyword(&mut self, keyword: &str, table_name: &str) -> SearchResult<()> {
        if self.consume_keyword(keyword) {
            Ok(())
        } else {
            Err(persisted_fts5_metadata_error(
                table_name,
                &format!("expected {keyword} in CREATE VIRTUAL TABLE definition"),
            ))
        }
    }

    fn identifier(&mut self) -> Option<String> {
        self.skip_whitespace();
        let byte = *self.source.as_bytes().get(self.index)?;
        let closing = match byte {
            b'\"' => Some(b'\"'),
            b'`' => Some(b'`'),
            b'[' => Some(b']'),
            _ => None,
        };
        if let Some(closing) = closing {
            self.index += 1;
            let start = self.index;
            while let Some(current) = self.source.as_bytes().get(self.index).copied() {
                if current == closing {
                    let value = self.source[start..self.index].to_owned();
                    self.index += 1;
                    return Some(value);
                }
                self.index += 1;
            }
            return None;
        }

        if !(byte == b'_' || byte.is_ascii_alphabetic()) {
            return None;
        }
        let start = self.index;
        self.index += 1;
        while self
            .source
            .as_bytes()
            .get(self.index)
            .is_some_and(|current| *current == b'_' || current.is_ascii_alphanumeric())
        {
            self.index += 1;
        }
        Some(self.source[start..self.index].to_owned())
    }

    fn parenthesized(&mut self, table_name: &str) -> SearchResult<&'a str> {
        self.skip_whitespace();
        if self.source.as_bytes().get(self.index) != Some(&b'(') {
            return Err(persisted_fts5_metadata_error(
                table_name,
                "expected FTS5 argument list",
            ));
        }
        self.index += 1;
        let start = self.index;
        let mut depth = 0_u32;
        let mut quote = None;
        while let Some(byte) = self.source.as_bytes().get(self.index).copied() {
            if let Some(delimiter) = quote {
                if byte == delimiter {
                    if self.source.as_bytes().get(self.index + 1) == Some(&delimiter) {
                        self.index += 2;
                        continue;
                    }
                    quote = None;
                }
            } else {
                match byte {
                    b'\'' | b'\"' | b'`' => quote = Some(byte),
                    b'[' => quote = Some(b']'),
                    b'(' => depth = depth.saturating_add(1),
                    b')' if depth == 0 => {
                        let end = self.index;
                        self.index += 1;
                        return Ok(&self.source[start..end]);
                    }
                    b')' => depth -= 1,
                    _ => {}
                }
            }
            self.index += 1;
        }
        Err(persisted_fts5_metadata_error(
            table_name,
            "unterminated FTS5 argument list",
        ))
    }

    fn finish(&mut self, table_name: &str) -> SearchResult<()> {
        self.skip_whitespace();
        if self.source.as_bytes().get(self.index) == Some(&b';') {
            self.index += 1;
            self.skip_whitespace();
        }
        if self.index == self.source.len() {
            Ok(())
        } else {
            Err(persisted_fts5_metadata_error(
                table_name,
                "unexpected trailing text in CREATE VIRTUAL TABLE definition",
            ))
        }
    }
}

fn fts5_checkpoint(cx: &Cx, phase: &'static str) -> SearchResult<()> {
    cx.checkpoint().map_err(|error| SearchError::Cancelled {
        phase: phase.to_owned(),
        reason: cx
            .cancel_reason()
            .map_or_else(|| error.to_string(), |reason| reason.to_string()),
    })
}

fn persisted_fts5_metadata_error(table_name: &str, reason: &str) -> SearchError {
    SearchError::InvalidConfig {
        field: "fts5.persisted_metadata".to_owned(),
        value: table_name.to_owned(),
        reason: reason.to_owned(),
    }
}

fn persisted_fts5_result_error(reason: &str) -> SearchError {
    SearchError::InvalidConfig {
        field: "fts5.persisted_result".to_owned(),
        value: reason.to_owned(),
        reason: "persisted FTS5 table does not meet the frankensearch result contract".to_owned(),
    }
}

// ─── Split lexical trait implementations ────────────────────────────────────

#[allow(clippy::significant_drop_tightening)]
impl frankensearch_core::LexicalRead for Fts5LexicalSearch {
    #[instrument(skip_all, fields(query = %query, limit = limit))]
    fn search<'a>(
        &'a self,
        cx: &'a Cx,
        query: &'a str,
        limit: usize,
    ) -> SearchFuture<'a, Vec<ScoredResult>> {
        Box::pin(async move {
            fts5_checkpoint(cx, "fts5.search")?;
            let query = Self::truncate_query(query);

            if query.trim().is_empty() {
                return Ok(Vec::new());
            }

            let table = self.table.lock().map_err(lock_error)?;
            let rowid_map = self.rowid_map.lock().map_err(lock_error)?;

            let search_results = table
                .search(query)
                .map_err(|e| SearchError::QueryParseError {
                    query: query.to_owned(),
                    detail: e.to_string(),
                })?;

            debug!(hits = search_results.len(), "fts5 BM25 search completed");

            let mut results = Vec::with_capacity(search_results.len().min(limit));
            for (rowid, score) in search_results.into_iter().take(limit) {
                let doc_id = rowid_map.get_doc_id(rowid).unwrap_or("").to_owned();

                // FTS5 BM25 scores are negative (lower = better).
                // Negate to produce positive scores (higher = better).
                #[allow(clippy::cast_possible_truncation)]
                let bm25_score = (-score) as f32;

                let metadata = table
                    .get_document(rowid)
                    .and_then(|cols| cols.get(COL_METADATA))
                    .filter(|s| !s.is_empty())
                    .and_then(|s| serde_json::from_str(s).ok());

                results.push(ScoredResult {
                    doc_id: doc_id.into(),
                    score: bm25_score,
                    source: ScoreSource::Lexical,
                    index: None,
                    fast_score: None,
                    quality_score: None,
                    lexical_score: Some(bm25_score),
                    rerank_score: None,
                    explanation: None,
                    metadata,
                });
            }

            Ok(results)
        })
    }

    /// FTS5 attaches full metadata during `search`, so the inherited eager
    /// `search_candidates` and its no-op hydration are exact for this backend:
    /// there is no deferred path to lose and no snapshot to pin.
    fn doc_count(&self) -> SearchResult<usize> {
        Ok(self.rowid_map.lock().map_err(lock_error)?.doc_to_row.len())
    }
}

impl frankensearch_core::LexicalWrite for Fts5LexicalSearch {
    fn index_document<'a>(
        &'a self,
        cx: &'a Cx,
        doc: &'a IndexableDocument,
    ) -> SearchFuture<'a, ()> {
        Box::pin(async move {
            fts5_checkpoint(cx, "fts5.index_document")?;
            let mut table = self.table.lock().map_err(lock_error)?;
            let mut rowid_map = self.rowid_map.lock().map_err(lock_error)?;

            // Upsert: delete existing document with same ID first.
            if let Some(old_rowid) = rowid_map.get_rowid(&doc.id) {
                table.delete_document(old_rowid);
            }

            let rowid = rowid_map.get_or_assign(&doc.id);
            let columns = Self::doc_to_columns(doc);
            table.insert_document(rowid, &columns);

            Ok(())
        })
    }

    fn index_documents<'a>(
        &'a self,
        cx: &'a Cx,
        docs: &'a [IndexableDocument],
    ) -> SearchFuture<'a, ()> {
        Box::pin(async move {
            fts5_checkpoint(cx, "fts5.index_documents")?;
            let mut table = self.table.lock().map_err(lock_error)?;
            let mut rowid_map = self.rowid_map.lock().map_err(lock_error)?;

            for doc in docs {
                fts5_checkpoint(cx, "fts5.index_documents")?;
                if let Some(old_rowid) = rowid_map.get_rowid(&doc.id) {
                    table.delete_document(old_rowid);
                }

                let rowid = rowid_map.get_or_assign(&doc.id);
                let columns = Self::doc_to_columns(doc);
                table.insert_document(rowid, &columns);
            }

            debug!(count = docs.len(), "fts5: batch indexed documents");
            Ok(())
        })
    }

    fn commit<'a>(&'a self, _cx: &'a Cx) -> SearchFuture<'a, ()> {
        // FTS5 in-memory table has no separate commit phase.
        Box::pin(async { Ok(()) })
    }
}

// ─── Hit type for snippet-aware search ──────────────────────────────────────

/// A hit from FTS5 search with optional snippet.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Fts5Hit {
    /// Document identifier.
    pub doc_id: String,
    /// BM25 relevance score (higher = better).
    pub bm25_score: f32,
    /// Position in results (0-indexed).
    pub rank: usize,
    /// Highlighted content snippet around matching terms.
    pub snippet: Option<String>,
    /// Document metadata.
    pub metadata: Option<serde_json::Value>,
}

// ─── Helpers ────────────────────────────────────────────────────────────────

fn lock_error<T>(_: T) -> SearchError {
    SearchError::SubsystemError {
        subsystem: "fts5",
        source: Box::new(std::io::Error::other("fts5 mutex poisoned")),
    }
}

// ─── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use std::future::Future;

    use super::*;
    // The split capabilities, so `adapter.search(..)` / `.index_document(..)`
    // / `.doc_count()` resolve now that the combined trait is gone.
    use frankensearch_core::{LexicalRead as _, LexicalWrite as _};

    /// Helper: run async test code with a `Cx` (asupersync, NO tokio).
    fn run_with_cx<F, Fut>(f: F)
    where
        F: FnOnce(Cx) -> Fut,
        Fut: Future<Output = ()>,
    {
        asupersync::test_utils::run_test_with_cx(f);
    }

    fn make_doc(id: &str, content: &str) -> IndexableDocument {
        IndexableDocument::new(id, content)
    }

    fn make_doc_with_title(id: &str, title: &str, content: &str) -> IndexableDocument {
        IndexableDocument::new(id, content).with_title(title)
    }

    fn make_doc_with_metadata(
        id: &str,
        content: &str,
        key: &str,
        value: &str,
    ) -> IndexableDocument {
        IndexableDocument::new(id, content).with_metadata(key, value)
    }

    // -- Construction --

    #[test]
    fn new_instance_is_empty() {
        let search = Fts5LexicalSearch::with_defaults();
        assert_eq!(search.doc_count().expect("document count"), 0);
    }

    #[test]
    fn document_count_fails_closed_when_row_authority_is_poisoned() {
        let search = Arc::new(Fts5LexicalSearch::with_defaults());
        let poisoner = Arc::clone(&search);
        std::thread::spawn(move || {
            let _guard = poisoner.rowid_map.lock().expect("lock row authority");
            panic!("poison row authority for document-count regression");
        })
        .join()
        .expect_err("poisoning worker must panic");

        assert!(matches!(
            search.doc_count(),
            Err(SearchError::SubsystemError {
                subsystem: "fts5",
                ..
            })
        ));
    }

    #[test]
    fn config_defaults_are_sane() {
        let config = Fts5AdapterConfig::default();
        assert_eq!(config.content_mode, Fts5ContentMode::Stored);
        assert_eq!(config.tokenizer, Fts5TokenizerChoice::Unicode61);
        assert!((config.title_boost - TITLE_BOOST).abs() < f64::EPSILON);
    }

    #[test]
    fn persisted_metadata_uses_ddl_for_content_mode_and_porter() {
        let stored = parse_persisted_fts5_metadata(
            "docs",
            "CREATE VIRTUAL TABLE docs USING fts5(doc_id, metadata_json, tokenize='porter unicode61');",
        )
        .expect("stored Porter definition should parse");
        assert_eq!(stored.content_mode, Fts5ContentMode::Stored);
        ensure_rebuildable_porter_fts5("docs", &stored)
            .expect("stored Porter definition should be rebuildable");

        let external = parse_persisted_fts5_metadata(
            "docs",
            "CREATE VIRTUAL TABLE docs USING fts5(doc_id, metadata_json, content='documents', tokenize='porter');",
        )
        .expect("external Porter definition should parse");
        assert_eq!(external.content_mode, Fts5ContentMode::External);
        ensure_rebuildable_porter_fts5("docs", &external)
            .expect("external Porter definition should be rebuildable");
    }

    #[test]
    fn persisted_metadata_rejects_contentless_or_non_porter_tables() {
        let contentless = parse_persisted_fts5_metadata(
            "docs",
            "CREATE VIRTUAL TABLE docs USING fts5(doc_id, metadata_json, content='', tokenize='porter');",
        )
        .expect("contentless Porter definition should parse before its policy check");
        assert_eq!(contentless.content_mode, Fts5ContentMode::Contentless);
        assert!(ensure_rebuildable_porter_fts5("docs", &contentless).is_err());

        let unicode = parse_persisted_fts5_metadata(
            "docs",
            "CREATE VIRTUAL TABLE docs USING fts5(doc_id, metadata_json, tokenize='unicode61');",
        )
        .expect("non-Porter definition should still parse");
        assert!(ensure_rebuildable_porter_fts5("docs", &unicode).is_err());
    }

    #[test]
    fn persisted_count_requires_async_authority_and_revalidates_the_marker() {
        run_with_cx(|cx| async move {
            let storage = Arc::new(
                Storage::open_unbootstrapped_in_memory_for_test()
                    .expect("in-memory storage should open"),
            );
            let fsqlite_cx = fsqlite_cx(&cx);
            storage
                .connection()
                .execute(
                    &fsqlite_cx,
                    &format!(
                        "CREATE TABLE {PORTER_FTS5_REBUILD_TABLE}(\
                         table_name TEXT, rebuild_version INTEGER NOT NULL);"
                    ),
                )
                .await
                .expect("governed rebuild marker table should be created");
            storage
                .connection()
                .execute(
                    &fsqlite_cx,
                    "CREATE VIRTUAL TABLE persisted_docs USING fts5(\
                     doc_id UNINDEXED, metadata_json UNINDEXED, content, tokenize='porter');",
                )
                .await
                .expect("persisted Porter table should be created");
            storage
                .connection()
                .execute(
                    &fsqlite_cx,
                    "INSERT INTO persisted_docs(doc_id, metadata_json, content) VALUES \
                     ('doc-1', '{}', 'first document'), \
                     ('doc-2', '{}', 'second document');",
                )
                .await
                .expect("persisted documents should be inserted");
            storage
                .connection()
                .execute(
                    &fsqlite_cx,
                    &format!(
                        "INSERT INTO {PORTER_FTS5_REBUILD_TABLE}(table_name, rebuild_version) \
                         VALUES ('persisted_docs', {PORTER_FTS5_REBUILD_VERSION});"
                    ),
                )
                .await
                .expect("governed rebuild marker should be installed");

            let reader =
                PersistedFts5LexicalSearch::open(&cx, Arc::clone(&storage), "persisted_docs")
                    .await
                    .expect("governed persisted reader should open");
            assert!(matches!(
                reader.doc_count(),
                Err(SearchError::SubsystemError {
                    subsystem: "fts5",
                    ..
                })
            ));
            assert_eq!(
                reader
                    .current_doc_count(&cx)
                    .await
                    .expect("async count should pin live persisted authority"),
                2
            );

            storage
                .connection()
                .execute(
                    &fsqlite_cx,
                    &format!(
                        "UPDATE {PORTER_FTS5_REBUILD_TABLE} SET rebuild_version = 0 \
                         WHERE table_name = 'persisted_docs';"
                    ),
                )
                .await
                .expect("marker invalidation should succeed");
            assert!(matches!(
                reader.current_doc_count(&cx).await,
                Err(SearchError::InvalidConfig { field, .. })
                    if field == "fts5.rebuild_version"
            ));

            storage
                .connection()
                .execute(
                    &fsqlite_cx,
                    &format!(
                        "UPDATE {PORTER_FTS5_REBUILD_TABLE} \
                         SET rebuild_version = {PORTER_FTS5_REBUILD_VERSION} \
                         WHERE table_name = 'persisted_docs';"
                    ),
                )
                .await
                .expect("governed marker should be restored");
            storage
                .connection()
                .execute(
                    &fsqlite_cx,
                    &format!(
                        "INSERT INTO {PORTER_FTS5_REBUILD_TABLE}(table_name, rebuild_version) \
                         VALUES ('persisted_docs', {PORTER_FTS5_REBUILD_VERSION});"
                    ),
                )
                .await
                .expect("duplicate hostile markers should be inserted");
            assert!(matches!(
                reader.current_doc_count(&cx).await,
                Err(SearchError::InvalidConfig { field, reason, .. })
                    if field == "fts5.rebuild_version" && reason.contains("duplicate")
            ));
        });
    }

    // -- Indexing --

    #[test]
    fn index_single_document() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            let doc = make_doc("doc1", "hello world of search");
            search.index_document(&cx, &doc).await.unwrap();
            assert_eq!(search.doc_count().expect("document count"), 1);
        });
    }

    #[test]
    fn index_batch_documents() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            let docs = vec![
                make_doc("a", "first document"),
                make_doc("b", "second document"),
                make_doc("c", "third document"),
            ];
            search.index_documents(&cx, &docs).await.unwrap();
            assert_eq!(search.doc_count().expect("document count"), 3);
        });
    }

    #[test]
    fn upsert_replaces_existing_document() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            let doc_v1 = make_doc("doc1", "original content");
            search.index_document(&cx, &doc_v1).await.unwrap();
            assert_eq!(search.doc_count().expect("document count"), 1);

            let doc_v2 = make_doc("doc1", "updated content completely different");
            search.index_document(&cx, &doc_v2).await.unwrap();
            assert_eq!(search.doc_count().expect("document count"), 1);

            // Search should find updated content.
            let results = search.search(&cx, "updated", 10).await.unwrap();
            assert_eq!(results.len(), 1);
            assert_eq!(results[0].doc_id, "doc1");

            // Old content should not match.
            let results = search.search(&cx, "original", 10).await.unwrap();
            assert!(results.is_empty());
        });
    }

    // -- Search --

    #[test]
    fn search_finds_matching_document() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            search
                .index_document(&cx, &make_doc("doc1", "rust programming language"))
                .await
                .unwrap();
            search
                .index_document(&cx, &make_doc("doc2", "python programming language"))
                .await
                .unwrap();

            let results = search.search(&cx, "rust", 10).await.unwrap();
            assert_eq!(results.len(), 1);
            assert_eq!(results[0].doc_id, "doc1");
            assert_eq!(results[0].source, ScoreSource::Lexical);
            assert!(results[0].lexical_score.is_some());
            assert!(results[0].score > 0.0);
        });
    }

    #[test]
    fn search_returns_results_sorted_by_relevance() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            // doc1 mentions "search" more times -> higher BM25.
            search
                .index_document(
                    &cx,
                    &make_doc("doc1", "search search search algorithms for search"),
                )
                .await
                .unwrap();
            search
                .index_document(&cx, &make_doc("doc2", "search algorithms"))
                .await
                .unwrap();

            let results = search.search(&cx, "search", 10).await.unwrap();
            assert_eq!(results.len(), 2);
            // Higher TF should produce higher BM25 score.
            assert!(results[0].score >= results[1].score);
        });
    }

    #[test]
    fn search_empty_query_returns_empty() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            search
                .index_document(&cx, &make_doc("doc1", "hello world"))
                .await
                .unwrap();

            let results = search.search(&cx, "", 10).await.unwrap();
            assert!(results.is_empty());

            let results = search.search(&cx, "   ", 10).await.unwrap();
            assert!(results.is_empty());
        });
    }

    #[test]
    fn search_no_match_returns_empty() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            search
                .index_document(&cx, &make_doc("doc1", "hello world"))
                .await
                .unwrap();

            let results = search.search(&cx, "zzzznonexistent", 10).await.unwrap();
            assert!(results.is_empty());
        });
    }

    #[test]
    fn search_respects_limit() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            for i in 0..10 {
                search
                    .index_document(
                        &cx,
                        &make_doc(&format!("doc{i}"), "common term in all docs"),
                    )
                    .await
                    .unwrap();
            }

            let results = search.search(&cx, "common", 3).await.unwrap();
            assert_eq!(results.len(), 3);
        });
    }

    #[test]
    fn search_preserves_metadata() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            let doc = make_doc_with_metadata("doc1", "searchable content", "category", "test");
            search.index_document(&cx, &doc).await.unwrap();

            let results = search.search(&cx, "searchable", 10).await.unwrap();
            assert_eq!(results.len(), 1);
            let meta = results[0].metadata.as_ref().unwrap();
            assert_eq!(meta["category"], "test");
        });
    }

    #[test]
    fn search_with_title_and_content() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            let doc = make_doc_with_title("doc1", "Important Title", "body text here");
            search.index_document(&cx, &doc).await.unwrap();

            // Should match on title.
            let results = search.search(&cx, "important", 10).await.unwrap();
            assert_eq!(results.len(), 1);
            assert_eq!(results[0].doc_id, "doc1");

            // Should match on content.
            let results = search.search(&cx, "body", 10).await.unwrap();
            assert_eq!(results.len(), 1);
        });
    }

    // -- Snippets --

    #[test]
    #[allow(clippy::significant_drop_tightening)]
    fn search_with_snippets_returns_highlighted_text() {
        let search = Fts5LexicalSearch::with_defaults();

        {
            let mut table = search.table.lock().unwrap();
            let mut rowid_map = search.rowid_map.lock().unwrap();

            let doc = make_doc("doc1", "The quick brown fox jumps over the lazy dog");
            let rowid = rowid_map.get_or_assign(&doc.id);
            let columns = Fts5LexicalSearch::doc_to_columns(&doc);
            table.insert_document(rowid, &columns);
        }

        let hits = search.search_with_snippets("fox", 10).unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].doc_id, "doc1");
        assert!(hits[0].snippet.is_some());
        let snippet = hits[0].snippet.as_ref().unwrap();
        assert!(
            snippet.contains("<b>fox</b>"),
            "snippet should highlight match: {snippet}"
        );
    }

    // -- Delete --

    #[test]
    fn delete_document_removes_it() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            search
                .index_document(&cx, &make_doc("doc1", "findable content"))
                .await
                .unwrap();
            assert_eq!(search.doc_count().expect("document count"), 1);

            let removed = search.delete_document("doc1").unwrap();
            assert!(removed);
            assert_eq!(search.doc_count().expect("document count"), 0);

            let results = search.search(&cx, "findable", 10).await.unwrap();
            assert!(results.is_empty());
        });
    }

    #[test]
    fn delete_nonexistent_returns_false() {
        let search = Fts5LexicalSearch::with_defaults();
        let removed = search.delete_document("nonexistent").unwrap();
        assert!(!removed);
    }

    // -- Clear --

    #[test]
    fn clear_removes_all_documents() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            search
                .index_document(&cx, &make_doc("doc1", "hello"))
                .await
                .unwrap();
            search
                .index_document(&cx, &make_doc("doc2", "world"))
                .await
                .unwrap();
            assert_eq!(search.doc_count().expect("document count"), 2);

            search.clear().unwrap();
            assert_eq!(search.doc_count().expect("document count"), 0);

            let results = search.search(&cx, "hello", 10).await.unwrap();
            assert!(results.is_empty());
        });
    }

    // -- Commit is no-op --

    #[test]
    fn commit_succeeds_without_error() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            search.commit(&cx).await.unwrap();
        });
    }

    // -- Edge cases --

    #[test]
    fn document_with_empty_content() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            let doc = make_doc("doc1", "");
            search.index_document(&cx, &doc).await.unwrap();
            assert_eq!(search.doc_count().expect("document count"), 1);

            let results = search.search(&cx, "anything", 10).await.unwrap();
            assert!(results.is_empty());
        });
    }

    #[test]
    fn document_with_special_characters() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            let doc = make_doc(
                "doc1",
                "error: fn<T>(x: &mut Vec<u8>) -> Result<(), Box<dyn Error>>",
            );
            search.index_document(&cx, &doc).await.unwrap();

            let results = search.search(&cx, "error", 10).await.unwrap();
            assert_eq!(results.len(), 1);
        });
    }

    #[test]
    fn batch_upsert_mixed_new_and_existing() {
        let search = Fts5LexicalSearch::with_defaults();
        run_with_cx(|cx| async move {
            search
                .index_document(&cx, &make_doc("doc1", "original"))
                .await
                .unwrap();
            assert_eq!(search.doc_count().expect("document count"), 1);

            let batch = vec![
                make_doc("doc1", "updated"),   // existing
                make_doc("doc2", "brand new"), // new
            ];
            search.index_documents(&cx, &batch).await.unwrap();
            assert_eq!(search.doc_count().expect("document count"), 2);
        });
    }

    // -- Trait object safety --

    #[test]
    fn fts5_is_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<Fts5LexicalSearch>();
    }

    // -- Config serialization --

    #[test]
    fn config_serde_roundtrip() {
        let config = Fts5AdapterConfig {
            content_mode: Fts5ContentMode::Contentless,
            tokenizer: Fts5TokenizerChoice::Porter,
            title_boost: 3.0,
        };

        let json = serde_json::to_string(&config).unwrap();
        let deserialized: Fts5AdapterConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.content_mode, Fts5ContentMode::Contentless);
        assert_eq!(deserialized.tokenizer, Fts5TokenizerChoice::Porter);
        assert!((deserialized.title_boost - 3.0).abs() < f64::EPSILON);
    }

    #[test]
    fn content_mode_default_is_stored() {
        assert_eq!(Fts5ContentMode::default(), Fts5ContentMode::Stored);
    }

    #[test]
    fn tokenizer_default_is_unicode61() {
        assert_eq!(
            Fts5TokenizerChoice::default(),
            Fts5TokenizerChoice::Unicode61
        );
    }

    // -- Query truncation --

    #[test]
    fn long_query_is_truncated() {
        let long_query = "a".repeat(MAX_QUERY_LENGTH + 100);
        let truncated = Fts5LexicalSearch::truncate_query(&long_query);
        assert_eq!(truncated.chars().count(), MAX_QUERY_LENGTH);
    }

    #[test]
    fn multibyte_query_uses_character_limit() {
        let long_query = "\u{00E9}".repeat(MAX_QUERY_LENGTH + 3);
        let truncated = Fts5LexicalSearch::truncate_query(&long_query);
        assert_eq!(truncated.chars().count(), MAX_QUERY_LENGTH);
        assert_eq!(truncated.len(), MAX_QUERY_LENGTH * '\u{00E9}'.len_utf8());
    }

    #[test]
    fn multibyte_query_within_character_limit_is_unchanged() {
        let query = "\u{00E9}".repeat(MAX_QUERY_LENGTH / 2 + 1);
        assert!(query.len() > MAX_QUERY_LENGTH);
        assert_eq!(Fts5LexicalSearch::truncate_query(&query), query);
    }

    #[test]
    fn normal_query_is_not_truncated() {
        let query = "normal search query";
        let result = Fts5LexicalSearch::truncate_query(query);
        assert_eq!(result, query);
    }

    // -- Debug impl --

    #[test]
    fn debug_format_never_waits_for_row_authority() {
        let search = Fts5LexicalSearch::with_defaults();
        let debug = format!("{search:?}");
        assert!(debug.contains("Fts5LexicalSearch"));
        assert!(debug.contains("config"));
        assert!(!debug.contains("doc_count"));
    }

    // -- Fts5Hit serde --

    #[test]
    fn fts5_hit_serde_roundtrip() {
        let hit = Fts5Hit {
            doc_id: "doc1".into(),
            bm25_score: 1.5,
            rank: 0,
            snippet: Some("hello <b>world</b>".to_owned()),
            metadata: None,
        };
        let json = serde_json::to_string(&hit).unwrap();
        let deserialized: Fts5Hit = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.doc_id, "doc1");
        assert!((deserialized.bm25_score - 1.5).abs() < f32::EPSILON);
    }

    // -- cass#301 merge/finalize scaling probe --

    /// Generate a deterministic, lexically varied document body of roughly
    /// `target_bytes`. Mixing a finite vocabulary with the doc index keeps the
    /// posting lists realistic (many shared terms, a few doc-unique terms)
    /// without any RNG, so the probe is reproducible across runs and hosts.
    fn synthetic_body(doc_index: usize, target_bytes: usize) -> String {
        const VOCAB: &[&str] = &[
            "retry",
            "backoff",
            "structured",
            "concurrency",
            "channel",
            "reserve",
            "commit",
            "cancel",
            "region",
            "scope",
            "embedder",
            "lexical",
            "semantic",
            "fusion",
            "rank",
            "vector",
            "index",
            "segment",
            "merge",
            "finalize",
            "tokenizer",
            "posting",
            "document",
            "search",
            "query",
            "score",
            "bm25",
        ];
        use std::fmt::Write as _;
        let mut body = String::with_capacity(target_bytes + 32);
        let mut counter = doc_index;
        while body.len() < target_bytes {
            let word = VOCAB[counter % VOCAB.len()];
            body.push_str(word);
            // Sprinkle a doc-unique token so each document has distinct terms.
            if counter % 11 == 0 {
                let _ = write!(body, " d{doc_index}t{counter}");
            }
            body.push(' ');
            counter = counter.wrapping_add(1).wrapping_add(doc_index);
        }
        body
    }

    /// cass#301: scaling probe for the frankensearch FTS5 lexical-index build
    /// path (`Fts5LexicalSearch::index_documents`, the exact path cass drives
    /// during `cass index --full`).
    ///
    /// Feeds an increasing number of documents simulating ~10MB -> ~40MB of
    /// indexed content and prints the wall-time of the index-build (finalize)
    /// phase plus a representative search at each size. The reported
    /// `build_ms_per_mb` (build time normalised by content size) is the
    /// diagnostic: if it is roughly flat across sizes the build is linear; if
    /// it climbs ~linearly with content size the build is O(N^2).
    ///
    /// Run with:
    /// `cargo test -p frankensearch-storage --features fts5 --release \
    ///    fts5_index_build_scaling_probe -- --ignored --nocapture`
    #[test]
    #[ignore = "cass#301 scaling probe: run explicitly with --ignored --nocapture"]
    fn fts5_index_build_scaling_probe() {
        use std::time::Instant;

        // ~50 KB per document. Content megabytes => doc_count = mb * 20.
        const DOC_BYTES: usize = 50 * 1024;
        let content_mbs: Vec<usize> = std::env::var("FTS5_PROBE_MBS")
            .ok()
            .map(|raw| {
                raw.split(',')
                    .filter_map(|s| s.trim().parse::<usize>().ok())
                    .collect()
            })
            .filter(|v: &Vec<usize>| !v.is_empty())
            .unwrap_or_else(|| vec![10, 20, 30, 40]);

        run_with_cx(|cx| async move {
            eprintln!(
                "FTS5_PROBE doc_bytes={DOC_BYTES} sizes_mb={content_mbs:?} (cass#301 build/finalize scaling)"
            );
            for &mb in &content_mbs {
                let doc_count = mb * (1024 * 1024) / DOC_BYTES;
                let docs: Vec<IndexableDocument> = (0..doc_count)
                    .map(|i| {
                        IndexableDocument::new(format!("doc-{i}"), synthetic_body(i, DOC_BYTES))
                            .with_title(format!("Document {i}"))
                    })
                    .collect();

                let search = Fts5LexicalSearch::with_defaults();

                // Index-build / finalize phase — the wedge phase in cass#301.
                let build_started = Instant::now();
                search
                    .index_documents(&cx, &docs)
                    .await
                    .expect("index_documents");
                search.commit(&cx).await.expect("commit");
                let build_elapsed = build_started.elapsed();

                // Representative query against the freshly built index.
                let search_started = Instant::now();
                let hits = search
                    .search(&cx, "structured concurrency retry", 10)
                    .await
                    .expect("search");
                let search_elapsed = search_started.elapsed();

                let build_ms = build_elapsed.as_secs_f64() * 1_000.0;
                let search_ms = search_elapsed.as_secs_f64() * 1_000.0;
                eprintln!(
                    "FTS5_PROBE content_mb={mb} docs={doc_count} build_ms={build_ms:.1} \
                     build_ms_per_mb={:.2} search_ms={search_ms:.3} hits={} doc_count={}",
                    build_ms / mb as f64,
                    hits.len(),
                    search.doc_count().expect("document count"),
                );
                assert_eq!(search.doc_count().expect("document count"), doc_count);
            }
        });
    }
}