khive-db 0.9.0

SQLite storage backend: entities, edges, notes, events, FTS5, sqlite-vec vectors.
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
//! FTS5-backed `TextSearch`: one virtual table per model, scores normalized to `(0.05, 1.0]`.

use std::sync::Arc;

use async_trait::async_trait;
use chrono::{DateTime, TimeZone, Utc};
use uuid::Uuid;

use khive_score::DeterministicScore;
use khive_storage::error::StorageError;
use khive_storage::types::{
    BatchWriteSummary, IndexRebuildScope, SqlStatement, SqlValue, TextDocument, TextFilter,
    TextGatherMode, TextIndexStats, TextQueryMode, TextSearchHit, TextSearchOptions,
    TextSearchRequest, TextTermStats, TextTermStatsRequest,
};
use khive_storage::usage::{UsageContext, UsageUnit};
use khive_storage::StorageCapability;
use khive_storage::TextSearch;
use khive_types::SubstrateKind;

use crate::error::SqliteError;
use crate::pool::ConnectionPool;
use crate::sql_bridge::bind_params;
use crate::writer_task::WriterTaskHandle;

/// Name of the sidecar rowid map for a given FTS table: `{namespace,
/// subject_id} -> rowid`, `WITHOUT ROWID`, `PRIMARY KEY (namespace,
/// subject_id)`.
///
/// `namespace` and `subject_id` are declared `UNINDEXED` in every FTS5 DDL
/// (trigram-tokenized production tables cannot index them without polluting
/// text queries with UUID fragments), so a point lookup on either column is a
/// full virtual-table scan of `table` — this sidecar turns it into a
/// primary-key lookup instead. `table` must already be a trusted, sanitized
/// table name.
pub fn rowid_map_table(table: &str) -> String {
    format!("{table}_rowids")
}

/// Name of [`rowid_map_table`]'s own state sidecar: a `key -> value` table
/// recording whether the map's one-time legacy backfill has completed, so
/// completion is a durable, transactionally-written fact instead of being
/// inferred from the map's row count.
pub fn rowid_map_state_table(table: &str) -> String {
    format!("{}_state", rowid_map_table(table))
}

/// Value of the `backfill` key in [`rowid_map_state_table`] once
/// `ensure_fts_rowid_map_backfilled` has completed a full reconciliation
/// pass over `table`.
pub const ROWID_MAP_BACKFILL_COMPLETE: &str = "complete";

/// DDL for [`rowid_map_table`]'s sidecar and its [`rowid_map_state_table`].
/// `IF NOT EXISTS` so every creation site (backend ensure-path, migrations,
/// test helpers) can call it unconditionally. `table` must already be a
/// trusted, sanitized table name.
pub fn rowid_map_ddl(table: &str) -> String {
    let map = rowid_map_table(table);
    let state = rowid_map_state_table(table);
    format!(
        "CREATE TABLE IF NOT EXISTS {map} (\
         namespace TEXT NOT NULL, \
         subject_id TEXT NOT NULL, \
         rowid INTEGER NOT NULL, \
         PRIMARY KEY (namespace, subject_id)\
         ) WITHOUT ROWID; \
         CREATE TABLE IF NOT EXISTS {state} (\
         key TEXT PRIMARY KEY, \
         value TEXT NOT NULL\
         ) WITHOUT ROWID"
    )
}

/// The exact `DELETE` this store's `delete_document` issues, for a given
/// FTS table (ADR-099 B3 r6 structural cut — see `entity.rs`'s sibling
/// block). Looks the target row's rowid up in [`rowid_map_table`] first (a
/// primary-key lookup) instead of scanning `table` for `namespace`/
/// `subject_id`, which are `UNINDEXED` FTS5 columns. `table` must already be
/// a trusted, sanitized table name (this mirrors `delete_document`'s own
/// pre-existing lack of a placeholder for table names — `format!` is
/// required since table identifiers cannot be bound as SQL parameters).
///
/// The trailing `AND namespace = ?1 AND subject_id = ?2` re-checks the key
/// on the (already rowid-narrowed) candidate row itself: if the map ever
/// held a stale entry — e.g. a crash between an earlier delete's FTS-row
/// removal and its own map-row removal, before FTS5 reused that rowid for a
/// different document — this statement would otherwise delete whatever
/// unrelated document now lives at that rowid instead of affecting zero
/// rows. Cheap: the `rowid IN (...)` subquery already narrows to at most one
/// candidate row via the map's primary key, so this is a single-row
/// post-filter, not a second table scan.
///
/// Callers that also need the sidecar's own row removed (a real delete, not
/// half of an upsert) must additionally run [`delete_document_map_statement`]
/// — see [`delete_document_statements`] for the paired, order-safe form.
pub fn delete_document_statement(table: &str, namespace: &str, subject_id: Uuid) -> SqlStatement {
    let map = rowid_map_table(table);
    SqlStatement {
        sql: format!(
            "DELETE FROM {table} WHERE rowid IN \
             (SELECT rowid FROM {map} WHERE namespace = ?1 AND subject_id = ?2) \
             AND namespace = ?1 AND subject_id = ?2"
        ),
        params: vec![
            SqlValue::Text(namespace.to_string()),
            SqlValue::Text(subject_id.to_string()),
        ],
        label: Some(format!("fts-delete-{table}")),
    }
}

/// Pre-map fallback: the literal delete this store issued before the rowid
/// map existed, scanning `table`'s `UNINDEXED` `namespace`/`subject_id`
/// columns directly. Used only by [`Fts5TextSearch`] instances constructed
/// in scan-fallback mode (a read-only snapshot whose FTS table predates the
/// sidecar map — see `StorageBackend::text_with_tokenizer`'s read-only
/// branch), where the map table does not exist to route through.
fn delete_document_statement_scan_fallback(
    table: &str,
    namespace: &str,
    subject_id: Uuid,
) -> SqlStatement {
    SqlStatement {
        sql: format!("DELETE FROM {table} WHERE namespace = ?1 AND subject_id = ?2"),
        params: vec![
            SqlValue::Text(namespace.to_string()),
            SqlValue::Text(subject_id.to_string()),
        ],
        label: Some(format!("fts-delete-scan-fallback-{table}")),
    }
}

/// Remove `(namespace, subject_id)`'s row from `table`'s [`rowid_map_table`].
///
/// Order matters relative to [`delete_document_statement`]: that statement's
/// subquery still needs this row, so run it first, then this one, in the same
/// transaction. Use [`delete_document_statements`] to get both in the correct
/// order.
pub fn delete_document_map_statement(
    table: &str,
    namespace: &str,
    subject_id: Uuid,
) -> SqlStatement {
    let map = rowid_map_table(table);
    SqlStatement {
        sql: format!("DELETE FROM {map} WHERE namespace = ?1 AND subject_id = ?2"),
        params: vec![
            SqlValue::Text(namespace.to_string()),
            SqlValue::Text(subject_id.to_string()),
        ],
        label: Some(format!("fts-delete-map-{table}")),
    }
}

/// The full, order-safe deletion of one FTS document: index 0 (the FTS row,
/// looked up through the map) MUST execute before index 1 (the map row
/// itself) in the same transaction/connection — index 0's subquery reads the
/// row index 1 removes. Callers that only need the FTS row gone as half of a
/// delete-then-insert upsert (the map row gets overwritten by the following
/// insert's `INSERT OR REPLACE`, so removing it first is redundant) can use
/// [`delete_document_statement`] alone.
pub fn delete_document_statements(
    table: &str,
    namespace: &str,
    subject_id: Uuid,
) -> [SqlStatement; 2] {
    [
        delete_document_statement(table, namespace, subject_id),
        delete_document_map_statement(table, namespace, subject_id),
    ]
}

/// Build the `INSERT` half of the FTS delete-then-insert upsert.
///
/// `table` must be a trusted, sanitized table name because SQL identifiers
/// cannot be bound as parameters.
///
/// This statement alone leaves [`rowid_map_table`] out of date — pair it with
/// [`insert_document_map_statement`] (or use [`insert_document_statements`]
/// for the order-safe combination) so the sidecar tracks the new rowid.
pub fn insert_document_statement(table: &str, document: &TextDocument) -> SqlStatement {
    let tags_json = tags_to_json(&document.tags);
    let metadata_json = document.metadata.as_ref().map(|v| v.to_string());
    SqlStatement {
        sql: format!(
            "INSERT INTO {table} \
             (subject_id, kind, title, body, tags, namespace, metadata, updated_at, record_kind) \
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)"
        ),
        params: vec![
            SqlValue::Text(document.subject_id.to_string()),
            SqlValue::Text(document.kind.to_string()),
            SqlValue::Text(document.title.clone().unwrap_or_default()),
            SqlValue::Text(document.body.clone()),
            SqlValue::Text(tags_json),
            SqlValue::Text(document.namespace.clone()),
            match metadata_json {
                Some(m) => SqlValue::Text(m),
                None => SqlValue::Null,
            },
            SqlValue::Integer(dt_to_micros(&document.updated_at)),
            match &document.record_kind {
                Some(kind) => SqlValue::Text(kind.clone()),
                None => SqlValue::Null,
            },
        ],
        label: Some(format!("fts-insert-{table}")),
    }
}

/// Upsert `(namespace, subject_id)`'s [`rowid_map_table`] row to point at the
/// rowid the connection's most recent successful `INSERT` produced.
///
/// Must run immediately after the FTS `INSERT` it tracks, on the same
/// connection, with nothing else written in between: `last_insert_rowid()` is
/// connection-scoped and reports whichever `INSERT` ran last. `INSERT OR
/// REPLACE` means this is safe to call whether or not a map row for this key
/// already exists (the ordinary upsert case does: the old row is overwritten
/// with the new rowid rather than requiring a prior delete).
pub fn insert_document_map_statement(
    table: &str,
    namespace: &str,
    subject_id: Uuid,
) -> SqlStatement {
    let map = rowid_map_table(table);
    SqlStatement {
        sql: format!(
            "INSERT OR REPLACE INTO {map} (namespace, subject_id, rowid) \
             VALUES (?1, ?2, last_insert_rowid())"
        ),
        params: vec![
            SqlValue::Text(namespace.to_string()),
            SqlValue::Text(subject_id.to_string()),
        ],
        label: Some(format!("fts-insert-map-{table}")),
    }
}

/// The full, order-safe insertion of one FTS document: index 0 (the FTS
/// `INSERT`) MUST execute immediately before index 1 (the rowid-map upsert)
/// on the same connection, with no other write between them — see
/// [`insert_document_map_statement`]'s adjacency contract. Every insert path
/// (create, and the insert half of an upsert) must go through this pair, or
/// the sidecar map silently falls out of sync with the FTS table it indexes.
pub fn insert_document_statements(table: &str, document: &TextDocument) -> [SqlStatement; 2] {
    [
        insert_document_statement(table, document),
        insert_document_map_statement(table, &document.namespace, document.subject_id),
    ]
}

/// Ensure the FTS5 virtual table for `table_key` (and its [`rowid_map_table`]
/// sidecar) exist.
///
/// Used in tests to set up an in-memory FTS5 table without the full `StorageBackend`.
#[cfg(test)]
pub(crate) fn ensure_fts5_schema(
    conn: &rusqlite::Connection,
    table_key: &str,
) -> Result<(), rusqlite::Error> {
    let table_name = format!("fts_{}", table_key);
    let ddl = format!(
        "CREATE VIRTUAL TABLE IF NOT EXISTS {} USING fts5(\
         subject_id UNINDEXED, \
         kind UNINDEXED, \
         title, \
         body, \
         tags UNINDEXED, \
         namespace UNINDEXED, \
         metadata UNINDEXED, \
         updated_at UNINDEXED, \
         record_kind\
         )",
        table_name
    );
    conn.execute_batch(&ddl)?;
    conn.execute_batch(&rowid_map_ddl(&table_name))
}

fn map_err(e: rusqlite::Error, op: &'static str) -> StorageError {
    StorageError::driver(StorageCapability::Text, op, e)
}

fn map_sqlite_err(e: SqliteError, op: &'static str) -> StorageError {
    StorageError::driver(StorageCapability::Text, op, e)
}

fn count_fts_pass(context: Option<&UsageContext>) {
    if let Some(context) = context {
        context.add(UsageUnit::FtsPasses, 1);
    }
}

/// A TextSearch backed by SQLite FTS5 virtual tables.
///
/// Each instance manages one table: `fts_{table_key}`. Documents are stored
/// with their metadata in UNINDEXED columns. `title`, `body`, and the optional
/// granular `record_kind` classifier are indexed; corpus filters use the
/// classifier inside MATCH and retain exact row predicates.
pub struct Fts5TextSearch {
    pool: Arc<ConnectionPool>,
    is_file_backed: bool,
    table_name: String,
    writer_task: Option<WriterTaskHandle>,
    /// Set only for a read-only snapshot whose FTS table predates the
    /// [`rowid_map_table`] sidecar (see `StorageBackend::text_with_tokenizer`'s
    /// read-only branch, which cannot create/backfill the map on a read-only
    /// connection). `get_document`/`delete_document` fall back to the
    /// pre-map `namespace`/`subject_id` scan predicates in this mode instead
    /// of joining/subquerying a map table that does not exist.
    scan_fallback: bool,
}

impl Fts5TextSearch {
    /// Create a new FTS5 text search instance.
    ///
    /// The FTS5 virtual table (and its [`rowid_map_table`] sidecar) must
    /// already exist (created by `StorageBackend::text()`).
    pub(crate) fn new(pool: Arc<ConnectionPool>, is_file_backed: bool, table_key: String) -> Self {
        Self::new_with_mode(pool, is_file_backed, table_key, false)
    }

    /// Create an instance in scan-fallback mode: the sidecar map is assumed
    /// absent, so every point lookup/delete falls back to the pre-map
    /// `namespace`/`subject_id` scan predicates. Reserved for a read-only
    /// snapshot whose FTS table predates the map (see the [`scan_fallback`]
    /// field doc and `StorageBackend::text_with_tokenizer`'s read-only
    /// branch).
    ///
    /// [`scan_fallback`]: Self::scan_fallback
    pub(crate) fn new_scan_fallback(
        pool: Arc<ConnectionPool>,
        is_file_backed: bool,
        table_key: String,
    ) -> Self {
        Self::new_with_mode(pool, is_file_backed, table_key, true)
    }

    fn new_with_mode(
        pool: Arc<ConnectionPool>,
        is_file_backed: bool,
        table_key: String,
        scan_fallback: bool,
    ) -> Self {
        let table_name = format!("fts_{}", table_key);
        // Enabled by default for file-backed pools; explicit off/degraded
        // construction remains synchronous (ADR-067 Component A, mirrors
        // entity.rs policy): a missing writer task is cached without failing
        // construction. Every write re-resolves it and applies
        // strict/compatibility policy then.
        let writer_task = pool.writer_task_handle().ok().flatten();
        Self {
            pool,
            is_file_backed,
            table_name,
            writer_task,
            scan_fallback,
        }
    }

    fn open_standalone_writer(&self) -> Result<rusqlite::Connection, StorageError> {
        self.pool
            .open_standalone_writer()
            .map_err(|e| map_sqlite_err(e, "open_fts_writer"))
    }

    /// Re-derive writer-task availability at write time instead of trusting
    /// only the field cached at construction (ADR-136 D1 gate 3 amendment).
    /// `self.writer_task` permanently caches `None` when this store was
    /// constructed outside a Tokio runtime (`writer_task_handle()` returns
    /// `Err(WriterTaskNoRuntime)`, which construction collapses via
    /// `.ok().flatten()`) — every later write, even ones running inside a
    /// runtime, would otherwise silently keep bypassing an enabled queue.
    /// The pool helper also enforces strict fail-closed routing and preserves
    /// a typed `WriterTaskNoRuntime` error when no runtime is available.
    fn current_writer_task(
        &self,
        operation: &'static str,
    ) -> Result<Option<WriterTaskHandle>, StorageError> {
        self.pool
            .writer_task_for_write(self.writer_task.as_ref(), operation)
    }

    /// Route a single-row write through the pool-wide `WriterTask` when
    /// the write queue is enabled and a handle is available. Strict mode
    /// refuses a missing handle; compatibility mode falls back to the legacy
    /// standalone-connection / pool-mutex path (ADR-067 Component A, Fork C
    /// slice 2). See `crates/khive-db/docs/api/text.md`
    /// for the per-caller routing rules (which methods bypass this via
    /// `with_writer_unmanaged` and why).
    async fn with_writer<F, R>(&self, op: &'static str, f: F) -> Result<R, StorageError>
    where
        F: FnOnce(&rusqlite::Connection) -> Result<R, rusqlite::Error> + Send + 'static,
        R: Send + 'static,
    {
        if let Some(writer_task) = self.current_writer_task(op)? {
            return writer_task
                .send_bounded(move |conn| f(conn).map_err(|e| map_err(e, op)))
                .await;
        }

        self.pool
            .record_direct_route(crate::timeout_sink::Site::DirectRouteFtsGeneralWrite);
        self.with_writer_unmanaged(op, f).await
    }

    /// Legacy standalone-connection / pool-mutex write path, bypassing the
    /// WriterTask channel unconditionally regardless of
    /// `KHIVE_WRITE_QUEUE`.
    ///
    /// Reserved for closures that manage their own transaction (a bare
    /// `BEGIN IMMEDIATE`/`COMMIT`/`ROLLBACK`) — those cannot be sent through
    /// the WriterTask channel, which already wraps every request in its own
    /// transaction. `rename_namespace` is the only caller.
    async fn with_writer_unmanaged<F, R>(&self, op: &'static str, f: F) -> Result<R, StorageError>
    where
        F: FnOnce(&rusqlite::Connection) -> Result<R, rusqlite::Error> + Send + 'static,
        R: Send + 'static,
    {
        let result = if self.is_file_backed {
            let conn = self.open_standalone_writer()?;
            tokio::task::spawn_blocking(move || f(&conn).map_err(|e| map_err(e, op)))
                .await
                .map_err(|e| StorageError::driver(StorageCapability::Text, op, e))?
        } else {
            let pool = Arc::clone(&self.pool);
            tokio::task::spawn_blocking(move || {
                let guard = pool.try_writer().map_err(|e| map_sqlite_err(e, op))?;
                f(guard.conn()).map_err(|e| map_err(e, op))
            })
            .await
            .map_err(|e| StorageError::driver(StorageCapability::Text, op, e))?
        };
        if let Err(err) = &result {
            let msg = err.to_string();
            if msg.contains("locked") || msg.contains("busy") {
                if self.is_file_backed {
                    // Only the standalone-connection path: the pool-mutex
                    // (`try_writer`) path's own timeout is already recorded
                    // at `Site::PoolAdmission` inside `ConnectionPool::writer`
                    // — emitting here too would double-count the same event.
                    crate::timeout_sink::emit_timeout(
                        &crate::timeout_sink::db_label(&self.pool),
                        crate::timeout_sink::Site::StandaloneText,
                        &msg,
                        None,
                    );
                }
                let open: Vec<String> = khive_storage::tx_registry::snapshot()
                    .into_iter()
                    .map(|(age, label)| {
                        format!(
                            "{}@{}ms",
                            label.as_deref().unwrap_or("unlabeled"),
                            age.as_millis()
                        )
                    })
                    .collect();
                tracing::warn!(
                    op,
                    open_tx_count = open.len(),
                    open_txs = %open.join(","),
                    "text write starved on the SQLite write lock; open registered \
                     transactions listed (empty means the holder issued no \
                     registered BEGIN IMMEDIATE in this process)"
                );
            }
        }
        result
    }

    async fn with_reader<F, R>(&self, op: &'static str, f: F) -> Result<R, StorageError>
    where
        F: FnOnce(&rusqlite::Connection) -> Result<R, rusqlite::Error> + Send + 'static,
        R: Send + 'static,
    {
        super::run_pooled_store_read(
            Arc::clone(&self.pool),
            StorageCapability::Text,
            op,
            move |conn| f(conn).map_err(|error| map_err(error, op)),
        )
        .await
    }
}

// -- Helper functions --

fn tags_to_json(tags: &[String]) -> String {
    serde_json::to_string(tags).unwrap_or_else(|_| "[]".to_string())
}

fn tags_from_json(s: &str) -> Vec<String> {
    serde_json::from_str(s).unwrap_or_default()
}

fn dt_to_micros(dt: &DateTime<Utc>) -> i64 {
    dt.timestamp_micros()
}

fn micros_to_dt(micros: i64) -> DateTime<Utc> {
    Utc.timestamp_micros(micros)
        .single()
        .unwrap_or_else(Utc::now)
}

/// Sanitize an FTS5 query string to prevent driver errors from special
/// chars: replace grouping/separator chars with spaces (splitting
/// punctuated identifiers into terms), strip remaining FTS5 operator
/// characters, then drop FTS5 keyword tokens (AND, OR, NOT, NEAR). See
/// `crates/khive-db/docs/api/text.md` for the exact char sets and the
/// issues (#388 and others) that grew them.
fn sanitize_fts5_query(query: &str) -> String {
    // Pass 1: replace grouping/separator chars with spaces to isolate tokens.
    // Colon, hyphen, and dot are included here (not in Pass 2) so punctuated
    // identifiers become separate terms rather than merged tokens.
    let spaced: String = query
        .chars()
        .map(|c| {
            if matches!(c, '(' | ')' | ',' | ':' | '-' | '.' | '/') {
                ' '
            } else {
                c
            }
        })
        .collect();

    // Pass 2: remove remaining FTS5 special chars and control characters.
    // Single quote (apostrophe) is included because FTS5 Plain-mode queries treat
    // it as a string-literal delimiter causing "syntax error near '''".
    // Dollar sign is included (#388) because FTS5's MATCH parser rejects it
    // unconditionally — `syntax error near "$"` — wherever it appears in the
    // expression, e.g. "$prev.id" (a common agent query for DSL docs).
    let sanitized: String = spaced
        .chars()
        .filter(|c| {
            !matches!(c, '*' | '"' | '\'' | '+' | '^' | '~' | '!' | '$' | '\0') && !c.is_control()
        })
        .collect();

    // Pass 3: filter FTS5 operator keywords.
    sanitized
        .split_whitespace()
        .filter(|t| {
            !matches!(
                t.to_ascii_uppercase().as_str(),
                "AND" | "OR" | "NOT" | "NEAR"
            )
        })
        .collect::<Vec<_>>()
        .join(" ")
}

/// Legacy (pre-#397) sanitization: hyphen and dot are stripped outright
/// (not space-split), so `khive-pack-memory` normalizes to the single merged
/// bareword `khivepackmemory`. Used only to build the merged-form
/// OR-alternative in [`sanitize_fts5_token_group`] — never as the sole
/// sanitized query — so pre-#397-indexed content stays reachable. See
/// `crates/khive-db/docs/api/text.md`.
fn sanitize_fts5_query_legacy_merged(query: &str) -> String {
    let spaced: String = query
        .chars()
        .map(|c| {
            if matches!(c, '(' | ')' | ',' | ':' | '/') {
                ' '
            } else {
                c
            }
        })
        .collect();

    let sanitized: String = spaced
        .chars()
        .filter(|c| {
            !matches!(
                c,
                '*' | '"' | '\'' | '+' | '-' | '^' | '.' | '~' | '!' | '$' | '\0'
            ) && !c.is_control()
        })
        .collect();

    sanitized
        .split_whitespace()
        .filter(|t| {
            !matches!(
                t.to_ascii_uppercase().as_str(),
                "AND" | "OR" | "NOT" | "NEAR"
            )
        })
        .collect::<Vec<_>>()
        .join(" ")
}

/// Escape a raw token for use inside a double-quoted FTS5 phrase. Embedded
/// double quotes are doubled; the trailing-`*` prefix-query trigger and
/// control characters are removed. Everything else, including punctuation,
/// passes through literally, since FTS5 phrase text is matched
/// against the column's own tokenization of that literal text (word-exact
/// under `unicode61`, substring-exact under `trigram`), not re-sanitized.
///
/// Returns `None` if nothing survives the filter.
fn sanitize_fts5_phrase_literal(token: &str) -> Option<String> {
    let mut literal = String::with_capacity(token.len());
    for c in token
        .chars()
        .filter(|c| !matches!(c, '*' | '\0') && !c.is_control())
    {
        if c == '"' {
            literal.push_str("\"\"");
        } else {
            literal.push(c);
        }
    }
    if literal.is_empty() {
        None
    } else {
        Some(literal)
    }
}

/// Below this length, FTS5's built-in `trigram` tokenizer (the production
/// default — `backend.rs::StorageBackend::text()`) produces zero tokens for
/// a bareword MATCH term. Verified against a live `tokenize='trigram'`
/// table: a term this short silently drops out of its AND clause instead of
/// constraining the match, e.g. `2026 07 10` matches any row containing
/// `2026` regardless of month/day. `sanitize_fts5_token_group` treats any
/// split segment at or below this length as trigram-unsafe.
const FTS5_TRIGRAM_MIN_SAFE_LEN: usize = 3;

/// FTS5 reserves punctuation in barewords for query syntax, including some
/// forms that change query meaning without producing an error. Keep bareword
/// alternatives to the conservative ASCII set and quote everything else.
fn is_fts5_bareword_safe(s: &str) -> bool {
    !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
}

/// Sanitize a single whitespace-isolated raw token into an FTS5
/// match-expression fragment, emitting additive OR-alternatives (split
/// AND-group, legacy-merged form, quoted literal phrase) so the result is
/// never a narrower match than any single safe form alone. Returns `None` if
/// the token sanitizes to nothing. See
/// `crates/khive-db/docs/api/text.md` for the full #397 trigram-safety
/// rationale behind which alternatives get emitted and when.
fn sanitize_fts5_token_group(token: &str) -> Option<String> {
    let split = sanitize_fts5_query(token);
    let split_terms: Vec<&str> = split.split_whitespace().collect();
    if split_terms.is_empty() {
        return None;
    }

    let all_bareword_safe = split_terms.iter().all(|t| is_fts5_bareword_safe(t));
    if split_terms.len() == 1 && is_fts5_bareword_safe(token) {
        return Some(split_terms[0].to_string());
    }

    let has_trigram_unsafe_segment = split_terms
        .iter()
        .any(|t| t.chars().count() < FTS5_TRIGRAM_MIN_SAFE_LEN);

    let mut alternatives = Vec::new();
    if all_bareword_safe && !has_trigram_unsafe_segment {
        alternatives.push(format!("({})", split_terms.join(" ")));
    }

    // An operator-bearing token (e.g. `NEAR(alpha-beta,5)`) can make the
    // legacy merge itself collapse to multiple space-separated terms rather
    // than one bareword: pass 1 of `sanitize_fts5_query_legacy_merged` spaces
    // out `(`, `)`, and `,` while pass 2 removes `-`/`.` outright, so
    // `NEAR(alpha-beta,5)` merges to `"alphabeta 5"`, not one word. Pushed
    // unguarded, that multi-term fragment carries the same trigram-unsafe
    // `5` the split-group check above exists to exclude, and under FTS5's
    // implicit-AND adjacency it silently drops, broadening the OR-alternative
    // to any row containing `alphabeta`. Apply the same trigram-safety gate
    // here whenever the merge is multi-term.
    let merged = sanitize_fts5_query_legacy_merged(token);
    let merged_terms: Vec<&str> = merged.split_whitespace().collect();
    let merged_all_bareword_safe =
        !merged_terms.is_empty() && merged_terms.iter().all(|t| is_fts5_bareword_safe(t));
    let merged_has_unsafe_segment = merged_terms.len() > 1
        && merged_terms
            .iter()
            .any(|t| t.chars().count() < FTS5_TRIGRAM_MIN_SAFE_LEN);
    // Compare against the split AND-group's own space-joined content — the
    // form it actually contributes to the expression — not the bare
    // concatenation of its terms. For an ordinary punctuated identifier the
    // concatenation always matches the merged bareword (both simply drop the
    // separators), which suppressed this alternative unconditionally and cut
    // off legacy-indexed content; a real duplicate only exists when the
    // merge itself produced the same space-separated content the split group
    // already emits (e.g. an operator token whose merge stays multi-term).
    let phrase = sanitize_fts5_phrase_literal(token);
    let duplicates_split = merged == split_terms.join(" ");
    let duplicates_phrase = phrase.as_deref() == Some(merged.as_str());
    if merged_all_bareword_safe
        && !merged.is_empty()
        && !duplicates_split
        && !duplicates_phrase
        && !merged_has_unsafe_segment
    {
        alternatives.push(merged);
    }

    if let Some(phrase) = phrase {
        alternatives.push(format!("\"{}\"", phrase));
    }

    match alternatives.len() {
        0 => None,
        1 => alternatives.into_iter().next(),
        _ => Some(format!("({})", alternatives.join(" OR "))),
    }
}

/// Join Plain-mode per-token groups into one MATCH expression.
///
/// FTS5's implicit-AND adjacency rule (a bare space between two terms)
/// applies only between two *plain* terms — verified against a live table:
/// `GQA KV cache` (all bare) matches, but `GQA AND KV AND cache` does not,
/// because FTS5's explicit `AND` treats a trigram-unsafe short bareword
/// (`KV`, 2 chars, zero trigrams) as an unsatisfiable operand, while
/// implicit adjacency instead lets it drop out harmlessly. So: use a bare
/// space between two plain (unparenthesized) groups to preserve that
/// existing leniency, and fall back to explicit `AND` only where at least
/// one side is a parenthesized OR-group (`sanitize_fts5_token_group`'s
/// output for punctuated tokens) — adjacency there without the operator is
/// a MATCH-expression syntax error (`(a OR b) c` fails; `(a OR b) AND c`
/// does not).
fn join_plain_groups(groups: &[String]) -> String {
    let mut expr = String::new();
    for (i, group) in groups.iter().enumerate() {
        if i > 0 {
            let prev_compound = groups[i - 1].starts_with('(');
            let this_compound = group.starts_with('(');
            expr.push_str(if prev_compound || this_compound {
                " AND "
            } else {
                " "
            });
        }
        expr.push_str(group);
    }
    expr
}

/// Build the FTS5 MATCH expression for a query string under a given mode.
///
/// Centralizes the AnyTerm/Plain/Phrase branching previously duplicated
/// across `search()`, `search_unranked()`, and `search_rank_within_cap()`.
///
/// AnyTerm and Plain both process the query token-by-token (split on
/// whitespace) through [`sanitize_fts5_token_group`], so a punctuated
/// identifier anywhere in the query gets its split/merged OR-alternative;
/// AnyTerm joins the per-token groups with `OR`; Plain joins them via
/// [`join_plain_groups`] (implicit-AND space where safe, explicit `AND`
/// where a group is parenthesized). Phrase mode keeps the single-string
/// literal behavior — a double-quoted FTS5 phrase cannot contain
/// `OR`/parenthesized groups.
///
/// Returns `None` when the sanitized query is empty (caller short-circuits
/// to an empty result set rather than sending an invalid MATCH expression).
fn build_match_expr(query: &str, mode: TextQueryMode) -> Option<String> {
    match mode {
        TextQueryMode::AnyTerm => {
            let groups: Vec<String> = query
                .split_whitespace()
                .filter_map(sanitize_fts5_token_group)
                .collect();
            if groups.is_empty() {
                None
            } else {
                Some(groups.join(" OR "))
            }
        }
        TextQueryMode::Plain => {
            let groups: Vec<String> = query
                .split_whitespace()
                .filter_map(sanitize_fts5_token_group)
                .collect();
            if groups.is_empty() {
                None
            } else {
                Some(join_plain_groups(&groups))
            }
        }
        TextQueryMode::Phrase => {
            sanitize_fts5_phrase_literal(query).map(|literal| format!("\"{}\"", literal))
        }
    }
}

fn quote_fts5_phrase(value: &str) -> String {
    format!("\"{}\"", value.replace('"', "\"\""))
}

/// Build an indexed classifier predicate for FTS5's MATCH expression.
///
/// The production tokenizer is trigram, so values shorter than three
/// characters have no postings. In that uncommon case the caller keeps only
/// the exact row predicate from `build_filter_clause`; correctness is
/// preserved while the indexed optimization deliberately declines.
fn record_kind_match_expr(filter: Option<&TextFilter>) -> Option<String> {
    let record_kinds = &filter?.record_kinds;
    if record_kinds.is_empty() || record_kinds.iter().any(|kind| kind.chars().count() < 3) {
        return None;
    }

    let clauses: Vec<String> = record_kinds
        .iter()
        .map(|kind| format!("record_kind : {}", quote_fts5_phrase(kind)))
        .collect();
    if clauses.len() == 1 {
        clauses.into_iter().next()
    } else {
        Some(format!("({})", clauses.join(" OR ")))
    }
}

fn build_filtered_match_expr(
    query: &str,
    mode: TextQueryMode,
    filter: Option<&TextFilter>,
) -> Option<String> {
    // Keep lexical terms confined to the two historically indexed text
    // columns. Once record_kind became indexed, an unqualified query would
    // otherwise make `memory` match every memory row solely via its classifier.
    let query = format!("{{title body}} : ({})", build_match_expr(query, mode)?);
    match record_kind_match_expr(filter) {
        Some(classifier) => Some(format!("{classifier} AND ({query})")),
        None => Some(query),
    }
}

/// Custom FTS5 rank configuration that keeps the classifier out of lexical
/// relevance while retaining the optimized hidden-`rank` ORDER BY path.
///
/// `record_kind` participates in MATCH for candidate pruning, but its weight is
/// zero so the classifier cannot become a relevance signal. Subject metadata
/// columns were already UNINDEXED; title/body retain their default unit weight.
const LEXICAL_BM25_RANK: &str = "bm25(0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0)";

/// Build a WHERE clause fragment and params for a `TextFilter`.
///
/// Returns `(clause, params)` where clause is empty if no filters are active.
/// Parameter indices start at `?{start_idx}`.
fn build_filter_clause(
    filter: &TextFilter,
    table: &str,
    start_idx: usize,
) -> (String, Vec<Box<dyn rusqlite::types::ToSql>>) {
    let mut conditions: Vec<String> = Vec::new();
    let mut params: Vec<Box<dyn rusqlite::types::ToSql>> = Vec::new();
    let mut idx = start_idx;

    if !filter.ids.is_empty() {
        let placeholders: Vec<String> = filter
            .ids
            .iter()
            .map(|_| {
                let p = format!("?{}", idx);
                idx += 1;
                p
            })
            .collect();
        conditions.push(format!(
            "{}.subject_id IN ({})",
            table,
            placeholders.join(", ")
        ));
        for id in &filter.ids {
            params.push(Box::new(id.to_string()));
        }
    }

    if !filter.kinds.is_empty() {
        let placeholders: Vec<String> = filter
            .kinds
            .iter()
            .map(|_| {
                let p = format!("?{}", idx);
                idx += 1;
                p
            })
            .collect();
        conditions.push(format!("{}.kind IN ({})", table, placeholders.join(", ")));
        for kind in &filter.kinds {
            params.push(Box::new(kind.to_string()));
        }
    }

    if !filter.record_kinds.is_empty() {
        let placeholders: Vec<String> = filter
            .record_kinds
            .iter()
            .map(|_| {
                let p = format!("?{}", idx);
                idx += 1;
                p
            })
            .collect();
        conditions.push(format!(
            "{}.record_kind IN ({})",
            table,
            placeholders.join(", ")
        ));
        for kind in &filter.record_kinds {
            params.push(Box::new(kind.clone()));
        }
    }

    if !filter.namespaces.is_empty() {
        let placeholders: Vec<String> = filter
            .namespaces
            .iter()
            .map(|_| {
                let p = format!("?{}", idx);
                idx += 1;
                p
            })
            .collect();
        conditions.push(format!(
            "{}.namespace IN ({})",
            table,
            placeholders.join(", ")
        ));
        for ns in &filter.namespaces {
            params.push(Box::new(ns.clone()));
        }
    }

    if conditions.is_empty() {
        (String::new(), params)
    } else {
        (format!(" AND {}", conditions.join(" AND ")), params)
    }
}

/// Test-only seam for [`delete_document_dml`]'s unmanaged (no-writer-task)
/// path: lets a test force the map-row delete to fail after the FTS-row
/// delete has already run, so the enclosing `BEGIN IMMEDIATE` / `ROLLBACK`
/// block's atomicity can be exercised without a real process crash
/// mid-transaction. Compiles to nothing outside `#[cfg(test)]` — production
/// code never reads this flag.
///
/// A process-wide `Mutex<HashSet<(String, Uuid)>>` rather than a
/// thread-local: the unmanaged path runs the DML closure inside
/// `tokio::task::spawn_blocking` (`with_writer_unmanaged`), on a worker
/// thread distinct from the test's own thread, so a thread-local set by the
/// test would never be observed by the closure. Each `(namespace,
/// subject_id)` target is armed/disarmed independently in the shared set —
/// `arm` inserts its own tuple and `disarm` removes only that same tuple —
/// so two tests running concurrently, each targeting a different key, cannot
/// clobber each other's armed state the way a single shared `Option` slot
/// would (one test's `disarm` would otherwise clear a target a second test
/// armed after the first but before the first's own disarm ran).
/// `delete_document_dml` only forces the failure when its own arguments are
/// a member of the set, so an unrelated delete running concurrently (e.g.
/// `test_delete_document`, which is not `#[serial]` against this seam) on a
/// key that was never armed is unaffected regardless of what else is armed
/// at the time.
#[cfg(test)]
pub(crate) mod delete_dml_test_seam {
    use std::collections::HashSet;
    use std::sync::Mutex;
    use uuid::Uuid;

    pub(crate) static TARGETS: Mutex<Option<HashSet<(String, Uuid)>>> = Mutex::new(None);

    pub(crate) fn arm(namespace: &str, subject_id: Uuid) {
        TARGETS
            .lock()
            .unwrap()
            .get_or_insert_with(HashSet::new)
            .insert((namespace.to_string(), subject_id));
    }

    pub(crate) fn disarm(namespace: &str, subject_id: Uuid) {
        if let Some(targets) = TARGETS.lock().unwrap().as_mut() {
            targets.remove(&(namespace.to_string(), subject_id));
        }
    }

    pub(crate) fn matches(namespace: &str, subject_id: Uuid) -> bool {
        TARGETS
            .lock()
            .unwrap()
            .as_ref()
            .is_some_and(|targets| targets.contains(&(namespace.to_string(), subject_id)))
    }
}

/// DML-only single-document delete shared by both the legacy (flag-off) and
/// WriterTask-routed (flag-on) `delete_document` paths.
///
/// Issues no `BEGIN` / `COMMIT` / `ROLLBACK` itself — the caller owns the
/// enclosing transaction. Both statements (the FTS row, looked up through
/// the map, then the map row itself) must commit atomically: a crash
/// between them would leave the map pointing at a rowid FTS5 may later
/// reuse for a different document.
fn delete_document_dml(
    conn: &rusqlite::Connection,
    table: &str,
    namespace: &str,
    subject_id: Uuid,
) -> Result<bool, SqliteError> {
    let [fts_statement, map_statement] = delete_document_statements(table, namespace, subject_id);

    let mut stmt = conn.prepare(&fts_statement.sql)?;
    bind_params(&mut stmt, &fts_statement.params)?;
    let deleted = stmt.raw_execute()? > 0;
    drop(stmt);

    #[cfg(test)]
    if delete_dml_test_seam::matches(namespace, subject_id) {
        return Err(SqliteError::InvalidData(
            "delete_document_dml test seam: forced failure between the FTS-row \
             delete and the map-row delete"
                .to_string(),
        ));
    }

    let mut map_stmt = conn.prepare(&map_statement.sql)?;
    bind_params(&mut map_stmt, &map_statement.params)?;
    map_stmt.raw_execute()?;

    Ok(deleted)
}

/// DML-only single-document upsert shared by both the legacy (flag-off) and
/// WriterTask-routed (flag-on) `upsert_document` paths (ADR-067 Component A).
///
/// Issues no `BEGIN` / `COMMIT` / `ROLLBACK` itself — the caller owns the
/// enclosing transaction.
fn upsert_document_dml(
    conn: &rusqlite::Connection,
    table: &str,
    document: &TextDocument,
) -> Result<(), rusqlite::Error> {
    // Delete (old rowid, via the map) -> insert (new rowid) -> map upsert
    // (new rowid). The map's own row for this key needs no separate delete:
    // `INSERT OR REPLACE` in the third statement overwrites it in place.
    let statement = delete_document_statement(table, &document.namespace, document.subject_id);
    let mut stmt = conn.prepare(&statement.sql)?;
    bind_params(&mut stmt, &statement.params)?;
    stmt.raw_execute()?;

    for statement in insert_document_statements(table, document) {
        let mut stmt = conn.prepare(&statement.sql)?;
        bind_params(&mut stmt, &statement.params)?;
        stmt.raw_execute()?;
    }
    Ok(())
}

/// DML-only batch upsert loop shared by both the legacy (flag-off) and
/// WriterTask-routed (flag-on) `upsert_documents` paths (ADR-067 Component A).
///
/// Issues no OUTER `BEGIN` / `COMMIT` / `ROLLBACK` — the caller owns the
/// enclosing transaction. The per-row named `SAVEPOINT fts_upsert_doc` is
/// preserved unchanged: it is what gives this loop its partial-success
/// semantics (one bad document does not abort the whole batch) independent
/// of which outer transaction wraps the loop.
fn batch_upsert_documents_dml(
    conn: &rusqlite::Connection,
    table: &str,
    documents: &[TextDocument],
    attempted: u64,
) -> Result<BatchWriteSummary, rusqlite::Error> {
    let map = rowid_map_table(table);
    // Delete via the map's primary key instead of a `namespace`/`subject_id`
    // scan of the (UNINDEXED on those columns) FTS table itself. The trailing
    // `AND namespace = ?1 AND subject_id = ?2` re-checks the key on the
    // rowid-narrowed candidate row: a stale map entry pointing at a rowid
    // FTS5 has since reused for a different document must delete zero rows,
    // not that unrelated document (mirrors `delete_document_statement`).
    let del_sql = format!(
        "DELETE FROM {table} WHERE rowid IN \
         (SELECT rowid FROM {map} WHERE namespace = ?1 AND subject_id = ?2) \
         AND namespace = ?1 AND subject_id = ?2"
    );
    let ins_sql = format!(
        "INSERT INTO {} \
         (subject_id, kind, title, body, tags, namespace, metadata, updated_at, record_kind) \
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
        table
    );
    // `INSERT OR REPLACE` — no separate delete of the map's own row needed;
    // see `upsert_document_dml`'s matching comment.
    let map_ins_sql = format!(
        "INSERT OR REPLACE INTO {map} (namespace, subject_id, rowid) \
         VALUES (?1, ?2, last_insert_rowid())"
    );

    let mut summary = BatchWriteSummary {
        attempted,
        ..BatchWriteSummary::default()
    };

    for (index, doc) in documents.iter().enumerate() {
        conn.execute_batch("SAVEPOINT fts_upsert_doc")?;
        let id_str = doc.subject_id.to_string();
        let namespace = &doc.namespace;
        let result = (|| {
            conn.execute(&del_sql, rusqlite::params![namespace, &id_str])?;

            let tags_json = tags_to_json(&doc.tags);
            let metadata_json: Option<String> = doc.metadata.as_ref().map(|v| v.to_string());

            conn.execute(
                &ins_sql,
                rusqlite::params![
                    &id_str,
                    &doc.kind.to_string(),
                    doc.title.as_deref().unwrap_or(""),
                    &doc.body,
                    &tags_json,
                    namespace,
                    &metadata_json,
                    dt_to_micros(&doc.updated_at),
                    &doc.record_kind,
                ],
            )?;
            conn.execute(&map_ins_sql, rusqlite::params![namespace, &id_str])?;
            Ok::<(), rusqlite::Error>(())
        })();

        match result {
            Ok(()) => {
                conn.execute_batch("RELEASE SAVEPOINT fts_upsert_doc")?;
                summary.affected = summary.affected.saturating_add(1);
            }
            Err(e) => {
                let _ = conn.execute_batch("ROLLBACK TO SAVEPOINT fts_upsert_doc");
                let _ = conn.execute_batch("RELEASE SAVEPOINT fts_upsert_doc");
                let (class, retryability) = super::classify_batch_sqlite_error(&e);
                summary.record_failure(index, Some(id_str), class, retryability, e.to_string());
            }
        }
    }

    Ok(summary)
}

#[async_trait]
impl TextSearch for Fts5TextSearch {
    async fn upsert_document(&self, document: TextDocument) -> Result<(), StorageError> {
        let table = self.table_name.clone();

        // ADR-067 Component A: when the write queue is enabled, route
        // through the pool-wide WriterTask. DML-only closure — no BEGIN
        // IMMEDIATE/COMMIT/ROLLBACK here, since the WriterTask's run loop
        // owns the transaction. `current_writer_task("fts_upsert")`
        // (ADR-136 D1 gate 3
        // amendment) re-checks past a construction-time `None` cache so a
        // handle that only became available later is still used here rather
        // than falling to `with_writer`'s BEGIN-IMMEDIATE-wrapped closure
        // below (that closure is not safe to send through the queue, which
        // already wraps its own transaction).
        if let Some(writer_task) = self.current_writer_task("fts_upsert")? {
            let table2 = table.clone();
            return writer_task
                .send_bounded(move |conn| {
                    upsert_document_dml(conn, &table2, &document)
                        .map_err(|e| map_err(e, "fts_upsert"))
                })
                .await;
        }

        // Explicitly disabled or degraded fallback path: byte-for-byte unchanged from pre-ADR-067
        // behavior — the closure owns its own BEGIN IMMEDIATE/COMMIT/ROLLBACK.
        let origin = self.pool.origin();
        self.with_writer("fts_upsert", move |conn| {
            conn.execute_batch("BEGIN IMMEDIATE")?;
            let _tx_handle = khive_storage::tx_registry::register_scoped(
                Some("text_upsert_document".to_string()),
                origin,
            );

            if let Err(e) = upsert_document_dml(conn, &table, &document) {
                let _ = conn.execute_batch("ROLLBACK");
                return Err(e);
            }

            conn.execute_batch("COMMIT")?;
            Ok(())
        })
        .await
    }

    async fn upsert_documents(
        &self,
        documents: Vec<TextDocument>,
    ) -> Result<BatchWriteSummary, StorageError> {
        let table = self.table_name.clone();
        let attempted = documents.len() as u64;

        // ADR-067 Component A: when the write queue is enabled, route
        // through the pool-wide WriterTask. DML-only closure (the per-row
        // `SAVEPOINT fts_upsert_doc` is preserved unchanged — only the OUTER
        // BEGIN IMMEDIATE/COMMIT is removed, since the WriterTask's run loop
        // owns the enclosing transaction). `current_writer_task` re-checks
        // past a construction-time `None` cache — see `upsert_document`'s
        // matching note.
        if let Some(writer_task) = self.current_writer_task("fts_upsert_batch")? {
            let table2 = table.clone();
            return writer_task
                .send_bounded(move |conn| {
                    batch_upsert_documents_dml(conn, &table2, &documents, attempted)
                        .map_err(|e| map_err(e, "fts_upsert_batch"))
                })
                .await;
        }

        // Explicitly disabled or degraded fallback path: byte-for-byte unchanged from pre-ADR-067
        // behavior — the closure owns its own BEGIN IMMEDIATE/COMMIT.
        let origin = self.pool.origin();
        self.with_writer("fts_upsert_batch", move |conn| {
            conn.execute_batch("BEGIN IMMEDIATE")?;
            let _tx_handle = khive_storage::tx_registry::register_scoped(
                Some("text_upsert_batch".to_string()),
                origin,
            );

            let summary = batch_upsert_documents_dml(conn, &table, &documents, attempted)?;

            conn.execute_batch("COMMIT")?;

            Ok(summary)
        })
        .await
    }

    async fn delete_document(
        &self,
        namespace: &str,
        subject_id: Uuid,
    ) -> Result<bool, StorageError> {
        let table = self.table_name.clone();
        let namespace = namespace.to_string();

        if self.scan_fallback {
            let statement = delete_document_statement_scan_fallback(&table, &namespace, subject_id);
            return self
                .with_writer("fts_delete_scan_fallback", move |conn| {
                    let mut stmt = conn.prepare(&statement.sql)?;
                    bind_params(&mut stmt, &statement.params)?;
                    Ok(stmt.raw_execute()? > 0)
                })
                .await;
        }

        // Route through the shared WriterTask (DML-only closure — the
        // queue's run loop owns the transaction) when available, exactly
        // like `upsert_document`'s matching branch. The fallback below wraps
        // both statements in one explicit transaction: a crash between the
        // FTS-row delete and the map-row delete must never persist only one
        // of the two, or a later insert could reuse the freed rowid while
        // the map still points at it.
        if let Some(writer_task) = self.current_writer_task("fts_delete")? {
            let table2 = table.clone();
            let namespace2 = namespace.clone();
            return writer_task
                .send_bounded(move |conn| {
                    delete_document_dml(conn, &table2, &namespace2, subject_id)
                        .map_err(|e| map_sqlite_err(e, "fts_delete"))
                })
                .await;
        }

        let origin = self.pool.origin();
        self.with_writer("fts_delete", move |conn| {
            conn.execute_batch("BEGIN IMMEDIATE")?;
            let _tx_handle = khive_storage::tx_registry::register_scoped(
                Some("text_delete_document".to_string()),
                origin,
            );

            match delete_document_dml(conn, &table, &namespace, subject_id) {
                Ok(deleted) => {
                    conn.execute_batch("COMMIT")?;
                    Ok(deleted)
                }
                Err(e) => {
                    let _ = conn.execute_batch("ROLLBACK");
                    Err(match e {
                        SqliteError::Rusqlite(inner) => inner,
                        other => rusqlite::Error::InvalidParameterName(other.to_string()),
                    })
                }
            }
        })
        .await
    }

    async fn get_document(
        &self,
        namespace: &str,
        subject_id: Uuid,
    ) -> Result<Option<TextDocument>, StorageError> {
        let namespace = namespace.to_string();
        let table = self.table_name.clone();
        let scan_fallback = self.scan_fallback;

        self.with_reader("fts_get", move |conn| {
            let sql = if scan_fallback {
                format!(
                    "SELECT subject_id, kind, title, body, tags, namespace, \
                     metadata, updated_at, record_kind \
                     FROM {table} WHERE namespace = ?1 AND subject_id = ?2"
                )
            } else {
                let map = rowid_map_table(&table);
                format!(
                    "SELECT t.subject_id, t.kind, t.title, t.body, t.tags, t.namespace, \
                     t.metadata, t.updated_at, t.record_kind \
                     FROM {table} AS t JOIN {map} AS m ON m.rowid = t.rowid \
                     WHERE m.namespace = ?1 AND m.subject_id = ?2 \
                     AND t.namespace = ?1 AND t.subject_id = ?2"
                )
            };
            let mut stmt = conn.prepare(&sql)?;
            let mut rows = stmt.query(rusqlite::params![namespace, subject_id.to_string()])?;

            match rows.next()? {
                Some(row) => {
                    let id_str: String = row.get(0)?;
                    let kind_str: String = row.get(1)?;
                    let title: String = row.get(2)?;
                    let body: String = row.get(3)?;
                    let tags_json: String = row.get(4)?;
                    let ns: String = row.get(5)?;
                    let metadata_json: Option<String> = row.get(6)?;
                    let updated_at_micros: i64 = row.get(7)?;
                    let record_kind: Option<String> = row.get(8)?;

                    let sid = Uuid::parse_str(&id_str).map_err(|e| {
                        rusqlite::Error::FromSqlConversionFailure(
                            0,
                            rusqlite::types::Type::Text,
                            Box::new(e),
                        )
                    })?;

                    let kind = kind_str.parse::<SubstrateKind>().map_err(|e| {
                        rusqlite::Error::FromSqlConversionFailure(
                            1,
                            rusqlite::types::Type::Text,
                            Box::new(e),
                        )
                    })?;

                    Ok(Some(TextDocument {
                        subject_id: sid,
                        kind,
                        record_kind,
                        title: if title.is_empty() { None } else { Some(title) },
                        body,
                        tags: tags_from_json(&tags_json),
                        namespace: ns,
                        metadata: metadata_json.and_then(|s| serde_json::from_str(&s).ok()),
                        updated_at: micros_to_dt(updated_at_micros),
                    }))
                }
                None => Ok(None),
            }
        })
        .await
    }

    async fn search(&self, request: TextSearchRequest) -> Result<Vec<TextSearchHit>, StorageError> {
        let table = self.table_name.clone();
        let usage = khive_storage::usage::current();

        let result = self
            .with_reader("fts_search", move |conn| {
                let match_expr = match build_filtered_match_expr(
                    &request.query,
                    request.mode,
                    request.filter.as_ref(),
                ) {
                    Some(expr) => expr,
                    None => return Ok(Vec::new()),
                };

                // Snippet column index 3 = body in the FTS5 schema.
                // snippet_chars == 0 is the sentinel for "no snippet" — skip the
                // snippet(...) call entirely and return NULL instead.  This avoids
                // the ~12ms BM25 snippet computation on the hot recall path where
                // snippets are unused.  Callers that need snippets (diagnostics) pass
                // snippet_chars > 0 and get the same behaviour as before.
                let snippet_expr = if request.snippet_chars == 0 {
                    "NULL AS snippet".to_string()
                } else {
                    let chars = i32::try_from(request.snippet_chars).unwrap_or(i32::MAX);
                    format!("snippet({table}, 3, '', '', '...', {chars})")
                };

                let (filter_clause, filter_params) = if let Some(ref filter) = request.filter {
                    build_filter_clause(filter, &table, 3)
                } else {
                    (String::new(), Vec::new())
                };

                let sql = format!(
                    "SELECT subject_id, rank, title, {snippet_expr} \
                     FROM {table} WHERE {table} MATCH ?1 \
                     AND rank MATCH '{LEXICAL_BM25_RANK}'{filter_clause} \
                     ORDER BY rank LIMIT ?2",
                );

                let mut stmt = conn.prepare(&sql)?;
                count_fts_pass(usage.as_ref());
                stmt.raw_bind_parameter(1, &match_expr)?;
                stmt.raw_bind_parameter(2, request.top_k as i64)?;

                for (i, param) in filter_params.iter().enumerate() {
                    param
                        .to_sql()
                        .map(|val| stmt.raw_bind_parameter(3 + i, val))
                        .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))??;
                }

                let mut hits = Vec::new();
                let mut rows = stmt.raw_query();
                let mut rank_idx = 0u32;

                while let Some(row) = rows.next()? {
                    let id_str: String = row.get(0)?;
                    let fts_rank: f64 = row.get(1)?;
                    let title: String = row.get(2)?;
                    let snippet: Option<String> = row.get(3)?;

                    let subject_id = Uuid::parse_str(&id_str).map_err(|e| {
                        rusqlite::Error::FromSqlConversionFailure(
                            0,
                            rusqlite::types::Type::Text,
                            Box::new(e),
                        )
                    })?;

                    rank_idx += 1;
                    hits.push((subject_id, fts_rank, rank_idx, title, snippet));
                }

                // Normalize scores within the result set to (0.05, 1.0].
                // Best rank (most negative) maps to 1.0, worst to 0.05.
                let min_rank = hits.iter().map(|h| h.1).fold(f64::INFINITY, f64::min);
                let max_rank = hits.iter().map(|h| h.1).fold(f64::NEG_INFINITY, f64::max);
                let range = max_rank - min_rank;

                let results = hits
                    .into_iter()
                    .map(|(subject_id, raw_rank, rank, title, snippet)| {
                        let score = if range.abs() < 1e-12 {
                            1.0
                        } else {
                            let t = (max_rank - raw_rank) / range;
                            0.05 + 0.95 * t
                        };
                        TextSearchHit {
                            subject_id,
                            score: DeterministicScore::from_f64(score),
                            rank,
                            title: if title.is_empty() { None } else { Some(title) },
                            snippet: snippet.filter(|s| !s.is_empty()),
                        }
                    })
                    .collect();

                Ok(results)
            })
            .await;

        result
    }

    async fn count(&self, filter: TextFilter) -> Result<u64, StorageError> {
        let table = self.table_name.clone();

        self.with_reader("fts_count", move |conn| {
            let indexed_classifier = record_kind_match_expr(Some(&filter));
            let filter_start = if indexed_classifier.is_some() { 2 } else { 1 };
            let (filter_clause, filter_params) = build_filter_clause(&filter, &table, filter_start);

            let sql = if indexed_classifier.is_some() {
                format!("SELECT COUNT(*) FROM {table} WHERE {table} MATCH ?1{filter_clause}")
            } else if filter_clause.is_empty() {
                format!("SELECT COUNT(*) FROM {table}")
            } else {
                let where_part = filter_clause.trim_start_matches(" AND ");
                format!("SELECT COUNT(*) FROM {table} WHERE {where_part}")
            };

            let mut stmt = conn.prepare(&sql)?;

            if let Some(classifier) = indexed_classifier {
                stmt.raw_bind_parameter(1, classifier)?;
            }

            for (i, param) in filter_params.iter().enumerate() {
                param
                    .to_sql()
                    .map(|val| stmt.raw_bind_parameter(filter_start + i, val))
                    .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))??;
            }

            let mut rows = stmt.raw_query();
            match rows.next()? {
                Some(row) => {
                    let count: i64 = row.get(0)?;
                    Ok(count as u64)
                }
                None => Ok(0),
            }
        })
        .await
    }

    async fn stats(&self) -> Result<TextIndexStats, StorageError> {
        let table = self.table_name.clone();

        self.with_reader("fts_stats", move |conn| {
            let sql = format!("SELECT COUNT(*) FROM {}", table);
            let count: i64 = conn.query_row(&sql, [], |row| row.get(0))?;

            Ok(TextIndexStats {
                document_count: count as u64,
                needs_rebuild: false,
                last_rebuild_at: None,
            })
        })
        .await
    }

    async fn search_with_options(
        &self,
        request: TextSearchRequest,
        options: TextSearchOptions,
    ) -> Result<Vec<TextSearchHit>, StorageError> {
        match options.gather_mode {
            TextGatherMode::Ranked => self.search(request).await,
            TextGatherMode::Unranked => self.search_unranked(request).await,
            TextGatherMode::RankWithinCap => {
                let gather_limit = options
                    .gather_limit
                    .unwrap_or(request.top_k)
                    .max(request.top_k);
                self.search_rank_within_cap(request, gather_limit).await
            }
        }
    }

    async fn term_stats(
        &self,
        request: TextTermStatsRequest,
    ) -> Result<Vec<TextTermStats>, StorageError> {
        let table = self.table_name.clone();

        self.with_reader("fts_term_stats", move |conn| {
            let filter = request.filter.as_ref();
            let indexed_classifier = record_kind_match_expr(filter);

            let count_filter_start = if indexed_classifier.is_some() { 2 } else { 1 };
            let (count_filter_clause, count_filter_params) = if let Some(f) = filter {
                build_filter_clause(f, &table, count_filter_start)
            } else {
                (String::new(), Vec::new())
            };

            let document_count: u64 = {
                let count_sql = if indexed_classifier.is_some() {
                    format!(
                        "SELECT COUNT(*) FROM {table} \
                         WHERE {table} MATCH ?1{count_filter_clause}"
                    )
                } else if count_filter_clause.is_empty() {
                    format!("SELECT COUNT(*) FROM {table}")
                } else {
                    let where_part = count_filter_clause.trim_start_matches(" AND ");
                    format!("SELECT COUNT(*) FROM {table} WHERE {where_part}")
                };
                let mut stmt = conn.prepare(&count_sql)?;
                if let Some(ref classifier) = indexed_classifier {
                    stmt.raw_bind_parameter(1, classifier)?;
                }
                for (i, param) in count_filter_params.iter().enumerate() {
                    param
                        .to_sql()
                        .map(|val| stmt.raw_bind_parameter(count_filter_start + i, val))
                        .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))??;
                }
                let mut rows = stmt.raw_query();
                match rows.next()? {
                    Some(row) => {
                        let c: i64 = row.get(0)?;
                        c as u64
                    }
                    None => 0,
                }
            };

            let mut results = Vec::with_capacity(request.terms.len());
            for term in &request.terms {
                // Keep document frequency aligned with the per-token search expression.
                let sanitized = sanitize_fts5_token_group(term).unwrap_or_default();
                if sanitized.is_empty() {
                    results.push(TextTermStats {
                        term: term.clone(),
                        sanitized_term: sanitized,
                        document_frequency: 0,
                        document_count,
                        inverse_document_frequency: 0.0,
                    });
                    continue;
                }

                // Per-term count: MATCH is ?1, so filter params start at ?2.
                let (term_filter_clause, term_filter_params) = if let Some(f) = filter {
                    build_filter_clause(f, &table, 2)
                } else {
                    (String::new(), Vec::new())
                };

                let text_term = format!("{{title body}} : ({sanitized})");
                let term_match = match &indexed_classifier {
                    Some(classifier) => format!("{classifier} AND ({text_term})"),
                    None => text_term,
                };
                let count_sql = format!(
                    "SELECT COUNT(*) FROM {table} WHERE {table} MATCH ?1{term_filter_clause}"
                );
                let mut stmt = conn.prepare(&count_sql)?;
                stmt.raw_bind_parameter(1, &term_match)?;
                for (i, param) in term_filter_params.iter().enumerate() {
                    param
                        .to_sql()
                        .map(|val| stmt.raw_bind_parameter(2 + i, val))
                        .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))??;
                }

                let df: u64 = {
                    let mut rows = stmt.raw_query();
                    match rows.next()? {
                        Some(row) => {
                            let c: i64 = row.get(0)?;
                            c as u64
                        }
                        None => 0,
                    }
                };

                let idf = Fts5TextSearch::bm25_idf(df, document_count);
                results.push(TextTermStats {
                    term: term.clone(),
                    sanitized_term: sanitized,
                    document_frequency: df,
                    document_count,
                    inverse_document_frequency: idf,
                });
            }

            Ok(results)
        })
        .await
    }

    async fn rebuild(&self, _scope: IndexRebuildScope) -> Result<TextIndexStats, StorageError> {
        let table = self.table_name.clone();

        self.with_writer("fts_rebuild", move |conn| {
            // FTS5 rebuild command: repopulates the internal index structures.
            let sql = format!("INSERT INTO {}({}) VALUES('rebuild')", table, table);
            conn.execute(&sql, [])?;

            let count_sql = format!("SELECT COUNT(*) FROM {}", table);
            let count: i64 = conn.query_row(&count_sql, [], |row| row.get(0))?;

            Ok(TextIndexStats {
                document_count: count as u64,
                needs_rebuild: false,
                last_rebuild_at: Some(Utc::now()),
            })
        })
        .await
    }
}

impl Fts5TextSearch {
    /// Robertson-Walker BM25 IDF: ln(((N - df + 0.5) / (df + 0.5)) + 1)
    fn bm25_idf(df: u64, document_count: u64) -> f64 {
        let n = document_count as f64;
        let f = df as f64;
        ((n - f + 0.5) / (f + 0.5) + 1.0).ln()
    }

    /// Gather candidates without BM25 ranking; return with uniform score 1.0.
    async fn search_unranked(
        &self,
        request: TextSearchRequest,
    ) -> Result<Vec<TextSearchHit>, StorageError> {
        let table = self.table_name.clone();
        let usage = khive_storage::usage::current();

        self.with_reader("fts_search_unranked", move |conn| {
            let match_expr = match build_filtered_match_expr(
                &request.query,
                request.mode,
                request.filter.as_ref(),
            ) {
                Some(expr) => expr,
                None => return Ok(Vec::new()),
            };

            let (filter_clause, filter_params) = if let Some(ref filter) = request.filter {
                build_filter_clause(filter, &table, 3)
            } else {
                (String::new(), Vec::new())
            };

            // No rank column, no ORDER BY — avoids BM25 computation entirely.
            let sql = format!(
                "SELECT subject_id, title \
                 FROM {table} WHERE {table} MATCH ?1{filter_clause} \
                 LIMIT ?2",
            );

            let mut stmt = conn.prepare(&sql)?;
            count_fts_pass(usage.as_ref());
            stmt.raw_bind_parameter(1, &match_expr)?;
            stmt.raw_bind_parameter(2, request.top_k as i64)?;

            for (i, param) in filter_params.iter().enumerate() {
                param
                    .to_sql()
                    .map(|val| stmt.raw_bind_parameter(3 + i, val))
                    .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))??;
            }

            let mut results = Vec::new();
            let mut rows = stmt.raw_query();
            let mut rank_idx = 0u32;

            while let Some(row) = rows.next()? {
                let id_str: String = row.get(0)?;
                let title: String = row.get(1)?;

                let subject_id = Uuid::parse_str(&id_str).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        0,
                        rusqlite::types::Type::Text,
                        Box::new(e),
                    )
                })?;

                rank_idx += 1;
                results.push(TextSearchHit {
                    subject_id,
                    score: DeterministicScore::from_f64(1.0),
                    rank: rank_idx,
                    title: if title.is_empty() { None } else { Some(title) },
                    snippet: None,
                });
            }

            Ok(results)
        })
        .await
    }

    /// Two-stage gather: cheap unranked LIMIT gather_limit, then BM25-rank the subset.
    async fn search_rank_within_cap(
        &self,
        request: TextSearchRequest,
        gather_limit: u32,
    ) -> Result<Vec<TextSearchHit>, StorageError> {
        let table = self.table_name.clone();
        let usage = khive_storage::usage::current();

        self.with_reader("fts_search_rank_within_cap", move |conn| {
            let match_expr = match build_filtered_match_expr(
                &request.query,
                request.mode,
                request.filter.as_ref(),
            ) {
                Some(expr) => expr,
                None => return Ok(Vec::new()),
            };

            let (filter_clause, filter_params) = if let Some(ref filter) = request.filter {
                build_filter_clause(filter, &table, 3)
            } else {
                (String::new(), Vec::new())
            };

            // Stage 1: cheap unranked gather of rowids.
            let gather_sql = format!(
                "SELECT subject_id FROM {table} WHERE {table} MATCH ?1{filter_clause} LIMIT ?2"
            );

            let mut stmt = conn.prepare(&gather_sql)?;
            count_fts_pass(usage.as_ref());
            stmt.raw_bind_parameter(1, &match_expr)?;
            stmt.raw_bind_parameter(2, gather_limit as i64)?;
            for (i, param) in filter_params.iter().enumerate() {
                param
                    .to_sql()
                    .map(|val| stmt.raw_bind_parameter(3 + i, val))
                    .map_err(|e| rusqlite::Error::ToSqlConversionFailure(Box::new(e)))??;
            }

            let mut gathered_ids: Vec<String> = Vec::new();
            let mut rows = stmt.raw_query();
            while let Some(row) = rows.next()? {
                gathered_ids.push(row.get::<_, String>(0)?);
            }

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

            // Stage 2: BM25-rank only the gathered subset via subject_id IN (...).
            let snippet_expr = if request.snippet_chars == 0 {
                "NULL AS snippet".to_string()
            } else {
                let chars = i32::try_from(request.snippet_chars).unwrap_or(i32::MAX);
                format!("snippet({table}, 3, '', '', '...', {chars})")
            };

            // Build IN clause for the gathered IDs.
            let id_placeholders: Vec<String> = gathered_ids
                .iter()
                .enumerate()
                .map(|(i, _)| format!("?{}", 3 + i))
                .collect();
            let in_clause = id_placeholders.join(", ");

            let rank_sql = format!(
                "SELECT subject_id, rank, title, {snippet_expr} \
                 FROM {table} WHERE {table} MATCH ?1 \
                 AND rank MATCH '{LEXICAL_BM25_RANK}' \
                 AND subject_id IN ({in_clause}) \
                 ORDER BY rank LIMIT ?2",
            );

            let mut stmt2 = conn.prepare(&rank_sql)?;
            count_fts_pass(usage.as_ref());
            stmt2.raw_bind_parameter(1, &match_expr)?;
            stmt2.raw_bind_parameter(2, request.top_k as i64)?;
            for (i, id_str) in gathered_ids.iter().enumerate() {
                stmt2.raw_bind_parameter(3 + i, id_str.as_str())?;
            }

            let mut hits = Vec::new();
            let mut rows2 = stmt2.raw_query();
            let mut rank_idx = 0u32;

            while let Some(row) = rows2.next()? {
                let id_str: String = row.get(0)?;
                let fts_rank: f64 = row.get(1)?;
                let title: String = row.get(2)?;
                let snippet: Option<String> = row.get(3)?;

                let subject_id = Uuid::parse_str(&id_str).map_err(|e| {
                    rusqlite::Error::FromSqlConversionFailure(
                        0,
                        rusqlite::types::Type::Text,
                        Box::new(e),
                    )
                })?;

                rank_idx += 1;
                hits.push((subject_id, fts_rank, rank_idx, title, snippet));
            }

            // Normalize scores within the ranked subset (same formula as search()).
            let min_rank = hits.iter().map(|h| h.1).fold(f64::INFINITY, f64::min);
            let max_rank = hits.iter().map(|h| h.1).fold(f64::NEG_INFINITY, f64::max);
            let range = max_rank - min_rank;

            let results = hits
                .into_iter()
                .map(|(subject_id, raw_rank, rank, title, snippet)| {
                    let score = if range.abs() < 1e-12 {
                        1.0
                    } else {
                        let t = (max_rank - raw_rank) / range;
                        0.05 + 0.95 * t
                    };
                    TextSearchHit {
                        subject_id,
                        score: DeterministicScore::from_f64(score),
                        rank,
                        title: if title.is_empty() { None } else { Some(title) },
                        snippet: snippet.filter(|s| !s.is_empty()),
                    }
                })
                .collect();

            Ok(results)
        })
        .await
    }

    /// Move all FTS5 documents from `old_namespace` to `new_namespace` in a
    /// single transaction.
    ///
    /// FTS5 virtual tables do not support updating indexed columns (`title`,
    /// `body`) via UPDATE. The correct approach is read-then-delete-then-reinsert.
    ///
    /// Callers must invoke this after any SQL-level namespace change on the
    /// backing entity table so that FTS5 keyword search stays consistent with
    /// the entity store.
    // REASON: reserved for namespace migration operations
    #[allow(dead_code)]
    pub(crate) async fn rename_namespace(
        &self,
        old_namespace: &str,
        new_namespace: &str,
    ) -> Result<u64, StorageError> {
        if old_namespace == new_namespace {
            return Ok(0);
        }
        let table = self.table_name.clone();
        let old_ns = old_namespace.to_string();
        let new_ns = new_namespace.to_string();

        // ADR-136 D1 gate 2: queue-first, DML-only closure. `run_writer_task`
        // already owns the enclosing `BEGIN IMMEDIATE`/`COMMIT`/`ROLLBACK` for
        // this request, so the SELECT enumerating rows-to-move runs INSIDE
        // that same transaction as the DELETE+INSERT that consumes it —
        // closing the TOCTOU window the pre-migration path had (its SELECT
        // ran before its own `BEGIN IMMEDIATE`, so a writer landing between
        // the two could resurrect a stale row or lose one mid-rename).
        if let Some(writer_task) = self.current_writer_task("fts_rename_namespace")? {
            let table2 = table.clone();
            return writer_task
                .send_bounded(move |conn| {
                    rename_namespace_dml(conn, &table2, &old_ns, &new_ns)
                        .map_err(|e| map_err(e, "fts_rename_namespace"))
                })
                .await;
        }

        self.pool
            .record_direct_route(crate::timeout_sink::Site::DirectRouteFtsRenameNamespace);

        let origin = self.pool.origin();
        self.with_writer_unmanaged("fts_rename_namespace", move |conn| {
            conn.execute_batch("BEGIN IMMEDIATE")?;
            let _tx_handle = khive_storage::tx_registry::register_scoped(
                Some("text_rename_namespace".to_string()),
                origin,
            );
            // The SELECT now runs inside this same `BEGIN IMMEDIATE` — same
            // TOCTOU fix as the queue path above, applied to the legacy
            // standalone-connection path too.
            match rename_namespace_dml(conn, &table, &old_ns, &new_ns) {
                Ok(moved) => {
                    conn.execute_batch("COMMIT")?;
                    Ok(moved)
                }
                Err(e) => {
                    let _ = conn.execute_batch("ROLLBACK");
                    Err(e)
                }
            }
        })
        .await
    }
}

struct FtsRenameRow {
    subject_id: String,
    kind: String,
    title: String,
    body: String,
    tags: String,
    metadata: Option<String>,
    updated_at: i64,
    record_kind: Option<String>,
}

/// Move every FTS5 document row from `old_ns` to `new_ns` in `table`.
/// DML-only — issues no `BEGIN`/`COMMIT`/`ROLLBACK` of its own, so it is safe
/// to call both inside an already-open transaction (the `WriterTask` queue
/// path) and wrapped by a caller-managed one (the legacy standalone path).
/// The `SELECT` enumerating rows-to-move and the `DELETE`+`INSERT` that
/// consumes it always run inside the SAME transaction the caller opened —
/// see `Fts5TextSearch::rename_namespace`'s TOCTOU note.
fn rename_namespace_dml(
    conn: &rusqlite::Connection,
    table: &str,
    old_ns: &str,
    new_ns: &str,
) -> Result<u64, rusqlite::Error> {
    let sel_sql = format!(
        "SELECT subject_id, kind, title, body, tags, metadata, updated_at, record_kind \
         FROM {} WHERE namespace = ?1",
        table
    );
    let rows: Vec<FtsRenameRow> = {
        let mut stmt = conn.prepare(&sel_sql)?;
        let iter = stmt.query_map(rusqlite::params![old_ns], |row| {
            Ok(FtsRenameRow {
                subject_id: row.get(0)?,
                kind: row.get(1)?,
                title: row.get(2)?,
                body: row.get(3)?,
                tags: row.get(4)?,
                metadata: row.get(5)?,
                updated_at: row.get(6)?,
                record_kind: row.get(7)?,
            })
        })?;
        iter.collect::<Result<Vec<_>, _>>()?
    };
    let moved = rows.len() as u64;
    if moved == 0 {
        return Ok(0);
    }

    let del_sql = format!("DELETE FROM {} WHERE namespace = ?1", table);
    conn.execute(&del_sql, rusqlite::params![old_ns])?;

    // The rows just deleted above are gone from `table` entirely (whole
    // namespace, not a single subject), so their map entries are pure
    // leftovers now — clear them before reinserting under `new_ns`.
    let map = rowid_map_table(table);
    let map_del_sql = format!("DELETE FROM {map} WHERE namespace = ?1");
    conn.execute(&map_del_sql, rusqlite::params![old_ns])?;

    let ins_sql = format!(
        "INSERT INTO {} \
         (subject_id, kind, title, body, tags, namespace, metadata, updated_at, record_kind) \
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
        table
    );
    let map_ins_sql = format!(
        "INSERT OR REPLACE INTO {map} (namespace, subject_id, rowid) \
         VALUES (?1, ?2, last_insert_rowid())"
    );
    for row in &rows {
        conn.execute(
            &ins_sql,
            rusqlite::params![
                row.subject_id,
                row.kind,
                row.title,
                row.body,
                row.tags,
                new_ns,
                row.metadata,
                row.updated_at,
                row.record_kind,
            ],
        )?;
        conn.execute(&map_ins_sql, rusqlite::params![new_ns, row.subject_id])?;
    }

    Ok(moved)
}

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