khive-runtime 0.13.0

Composable Service API: entity/note CRUD, graph traversal, hybrid search, curation.
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
use super::*;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;

use crate::runtime::{KhiveRuntime, NamespaceToken, RuntimeConfig};
use khive_score::rrf_score;
use khive_storage::types::{TextSearchHit, VectorSearchHit};
use khive_types::namespace::Namespace;
use lattice_embed::{EmbedError, EmbeddingModel};

/// Arm a provider that changes from healthy to failing after setup so a
/// test can exercise an error on a provider already serving requests.
fn break_vector_arm(fail: &AtomicBool) {
    fail.store(true, Ordering::SeqCst);
}

/// An `EmbeddingService` that always succeeds with a fixed vector — used
/// to exercise a genuinely healthy vector leg without loading real model
/// weights.
struct ConstantEmbeddingService {
    dimensions: usize,
    fail: Arc<AtomicBool>,
}

#[async_trait::async_trait]
impl EmbeddingService for ConstantEmbeddingService {
    async fn embed(
        &self,
        texts: &[String],
        _model: EmbeddingModel,
    ) -> Result<Vec<Vec<f32>>, EmbedError> {
        if self.fail.load(Ordering::SeqCst) {
            return Err(EmbedError::ModelInitialization(
                "injected vector-arm failure".to_string(),
            ));
        }
        Ok(texts.iter().map(|_| vec![1.0; self.dimensions]).collect())
    }

    fn supports_model(&self, _model: EmbeddingModel) -> bool {
        true
    }

    fn name(&self) -> &'static str {
        "hybrid-search-test-constant-embedding"
    }
}

struct ConstantEmbedderProvider {
    name: String,
    dimensions: usize,
    fail: Arc<AtomicBool>,
}

#[async_trait::async_trait]
impl EmbedderProvider for ConstantEmbedderProvider {
    fn name(&self) -> &str {
        &self.name
    }

    fn dimensions(&self) -> usize {
        self.dimensions
    }

    async fn build(&self) -> RuntimeResult<Arc<dyn EmbeddingService>> {
        Ok(Arc::new(ConstantEmbeddingService {
            dimensions: self.dimensions,
            fail: Arc::clone(&self.fail),
        }))
    }
}

/// A runtime configured with a healthy (constant, non-failing) embedder —
/// the vector leg genuinely runs and succeeds.
fn runtime_with_constant_embeddings() -> KhiveRuntime {
    runtime_with_toggleable_embeddings().0
}

fn runtime_with_toggleable_embeddings() -> (KhiveRuntime, Arc<AtomicBool>) {
    let model = EmbeddingModel::AllMiniLmL6V2;
    let runtime = KhiveRuntime::new(RuntimeConfig {
        db_path: None,
        embedding_model: Some(model),
        packs: vec!["kg".to_string()],
        ..RuntimeConfig::no_embeddings()
    })
    .expect("in-memory runtime");
    let fail = Arc::new(AtomicBool::new(false));
    runtime.register_embedder(ConstantEmbedderProvider {
        name: model.to_string(),
        dimensions: model.dimensions(),
        fail: Arc::clone(&fail),
    });
    (runtime, fail)
}

#[test]
fn bounded_embedding_input_reserves_prefix_and_preserves_utf8() {
    assert_eq!(
        document_embedding_budget("multilingual-e5-base"),
        MAX_TEXT_BYTES - "passage: ".len()
    );

    let input = format!("{}\u{1f980}tail", "a".repeat(MAX_TEXT_BYTES - 1));
    let (bounded, truncated) = bounded_embedding_input(&input, MAX_TEXT_BYTES);
    assert!(truncated);
    assert_eq!(bounded.len(), MAX_TEXT_BYTES - 1);
    assert!(bounded.is_char_boundary(bounded.len()));
    assert!(!bounded.contains('\u{1f980}'));
}

#[test]
fn bounded_embedding_input_leaves_normal_text_unchanged() {
    let input = "normal byte-identical embedding input";
    let (bounded, truncated) = bounded_embedding_input(input, MAX_TEXT_BYTES);
    assert!(!truncated);
    assert_eq!(bounded, input);
    assert_eq!(bounded.as_ptr(), input.as_ptr());
}

fn text_hit(id: Uuid, rank: u32, title: &str) -> TextSearchHit {
    TextSearchHit {
        subject_id: id,
        score: DeterministicScore::from_f64(1.0),
        rank,
        title: Some(title.to_string()),
        snippet: Some("...".to_string()),
    }
}

fn vector_hit(id: Uuid, rank: u32) -> VectorSearchHit {
    VectorSearchHit {
        subject_id: id,
        score: DeterministicScore::from_f64(0.9),
        rank,
    }
}

#[test]
fn rrf_evidence_keeps_absence_distinct_from_measured_zero() {
    let text_id = Uuid::from_u128(1);
    let vector_id = Uuid::from_u128(2);
    let both_id = Uuid::from_u128(3);
    let quarter = DeterministicScore::from_raw(1_i64 << 30);
    let half = DeterministicScore::from_raw(1_i64 << 31);
    let text = vec![
        TextSearchHit {
            score: DeterministicScore::ZERO,
            ..text_hit(text_id, 1, "text")
        },
        TextSearchHit {
            score: quarter,
            ..text_hit(both_id, 2, "both")
        },
        text_hit(both_id, 3, "duplicate"),
    ];
    let vector = vec![
        VectorSearchHit {
            score: DeterministicScore::ZERO,
            ..vector_hit(vector_id, 1)
        },
        VectorSearchHit {
            score: half,
            ..vector_hit(both_id, 2)
        },
        vector_hit(both_id, 3),
    ];
    let hits = rrf_fuse(text, vector, 10, "unmatched");
    assert_eq!(hits.len(), 3);
    for (id, source, signals) in [
        (
            text_id,
            SearchSource::Text,
            SearchSignals {
                vector_similarity: None,
                keyword_score: Some(DeterministicScore::ZERO),
            },
        ),
        (
            vector_id,
            SearchSource::Vector,
            SearchSignals {
                vector_similarity: Some(DeterministicScore::ZERO),
                keyword_score: None,
            },
        ),
        (
            both_id,
            SearchSource::Both,
            SearchSignals {
                vector_similarity: Some(half),
                keyword_score: Some(quarter),
            },
        ),
    ] {
        let hit = hits.iter().find(|hit| hit.entity_id == id).unwrap();
        assert_eq!(hit.rank_score_kind, RankScoreKind::Rrf);
        assert_eq!(hit.source, source);
        assert_eq!(hit.signals, signals);
    }
}

#[test]
fn rrf_evidence_golden_preserves_true_ties_across_permutations() {
    let a = Uuid::from_u128(1);
    let b = Uuid::from_u128(2);
    let expected = vec![
        (
            a,
            748_365_513,
            RankScoreKind::Rrf,
            SearchSignals {
                vector_similarity: Some(DeterministicScore::from_raw(1_i64 << 31)),
                keyword_score: Some(DeterministicScore::from_raw(1_i64 << 30)),
            },
        ),
        (
            b,
            748_365_513,
            RankScoreKind::Rrf,
            SearchSignals {
                vector_similarity: Some(DeterministicScore::from_raw(1_i64 << 32)),
                keyword_score: Some(DeterministicScore::from_raw(3_i64 << 30)),
            },
        ),
    ];
    // Swapping both arms preserves each ID's rank-1 plus rank-2 total.
    for (text_ids, vector_ids) in [([a, b], [b, a]), ([b, a], [a, b])] {
        for _ in 0..4 {
            let text = text_ids
                .into_iter()
                .enumerate()
                .map(|(rank, id)| TextSearchHit {
                    score: DeterministicScore::from_raw(if id == a { 1_i64 } else { 3_i64 } << 30),
                    ..text_hit(id, rank as u32 + 1, "candidate")
                })
                .collect();
            let vector = vector_ids
                .into_iter()
                .enumerate()
                .map(|(rank, id)| VectorSearchHit {
                    score: DeterministicScore::from_raw(if id == a { 1_i64 } else { 2_i64 } << 31),
                    ..vector_hit(id, rank as u32 + 1)
                })
                .collect();
            let hits = rrf_fuse(text, vector, 10, "unmatched");
            assert_eq!(hits.len(), 2);
            assert_eq!(hits[0].score, hits[1].score);
            assert!(hits.iter().all(|hit| hit.source == SearchSource::Both));
            let snapshot: Vec<_> = hits
                .iter()
                .map(|hit| {
                    (
                        hit.entity_id,
                        hit.score.to_raw(),
                        hit.rank_score_kind,
                        hit.signals,
                    )
                })
                .collect();
            assert_eq!(snapshot, expected);
        }
    }
}

#[test]
fn rrf_fuse_text_only() {
    let a = Uuid::new_v4();
    let b = Uuid::new_v4();
    let text = vec![text_hit(a, 1, "A"), text_hit(b, 2, "B")];
    let hits = rrf_fuse(text, vec![], 10, "query");
    assert_eq!(hits.len(), 2);
    assert_eq!(hits[0].entity_id, a);
    assert_eq!(hits[0].source, SearchSource::Text);
    assert_eq!(hits[0].title.as_deref(), Some("A"));
}

#[test]
fn rrf_fuse_vector_only() {
    let a = Uuid::new_v4();
    let hits = rrf_fuse(vec![], vec![vector_hit(a, 1)], 10, "query");
    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].source, SearchSource::Vector);
    assert!(hits[0].title.is_none());
}

#[test]
fn rrf_fuse_marks_both_when_in_both_lists() {
    let id = Uuid::new_v4();
    let text = vec![text_hit(id, 1, "A")];
    let vec = vec![vector_hit(id, 1)];
    let hits = rrf_fuse(text, vec, 10, "query");
    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].source, SearchSource::Both);
}

#[test]
fn rrf_fuse_preserves_unique_leg_scores_exactly() {
    let text_only = Uuid::new_v4();
    let both = Uuid::new_v4();
    let vector_only = Uuid::new_v4();
    let text = vec![text_hit(text_only, 1, "A"), text_hit(both, 2, "B")];
    let vector = vec![vector_hit(both, 1), vector_hit(vector_only, 2)];

    let hits = rrf_fuse(text, vector, 10, "query");
    let score_for = |id| {
        hits.iter()
            .find(|hit| hit.entity_id == id)
            .expect("expected fused hit")
            .score
    };

    assert_eq!(score_for(text_only), rrf_score(1, RRF_K));
    assert_eq!(score_for(both), rrf_score(2, RRF_K) + rrf_score(1, RRF_K));
    assert_eq!(score_for(vector_only), rrf_score(2, RRF_K));
}

#[test]
fn rrf_fuse_counts_duplicate_once_per_leg() {
    let id = Uuid::new_v4();
    let text = vec![text_hit(id, 1, "A"), text_hit(id, 2, "A duplicate")];
    let vector = vec![vector_hit(id, 1), vector_hit(id, 2)];

    let hits = rrf_fuse(text, vector, 10, "query");

    assert_eq!(hits.len(), 1);
    assert_eq!(hits[0].source, SearchSource::Both);
    assert_eq!(hits[0].score, rrf_score(1, RRF_K) + rrf_score(1, RRF_K));
}

#[test]
fn rrf_fuse_respects_limit() {
    let hits: Vec<TextSearchHit> = (0..20)
        .map(|i| text_hit(Uuid::new_v4(), i + 1, "x"))
        .collect();
    let fused = rrf_fuse(hits, vec![], 5, "query");
    assert_eq!(fused.len(), 5);
}

#[test]
fn rrf_fuse_orders_higher_score_first() {
    // Same UUID in both lists at rank 1 → score 2/(10+1). Different UUIDs → 1/(10+1) each.
    let a = Uuid::new_v4();
    let b = Uuid::new_v4();
    let text = vec![text_hit(a, 1, "A")];
    let vec = vec![vector_hit(a, 1), vector_hit(b, 2)];
    let hits = rrf_fuse(text, vec, 10, "query");
    assert_eq!(hits[0].entity_id, a);
    assert_eq!(hits[0].source, SearchSource::Both);
    assert!(hits[0].score > hits[1].score);
}

#[test]
fn rrf_fuse_k10_score_spread_exceeds_threshold() {
    // With k=10: rank 1 → 1/11 ≈ 0.0909, rank 10 → 1/20 = 0.0500.
    // Spread ≈ 0.041, well above the 0.03 minimum required for reliable dedup.
    let ids: Vec<Uuid> = (0..10).map(|_| Uuid::new_v4()).collect();
    let text: Vec<TextSearchHit> = ids
        .iter()
        .enumerate()
        .map(|(i, &id)| text_hit(id, (i + 1) as u32, "x"))
        .collect();
    let hits = rrf_fuse(text, vec![], 10, "query");
    assert_eq!(hits.len(), 10);
    let top_score = hits[0].score.to_f64();
    let bottom_score = hits[9].score.to_f64();
    let spread = top_score - bottom_score;
    assert!(
        spread >= 0.03,
        "score spread {spread:.4} between rank 1 and rank 10 must be ≥ 0.03 (was {spread:.4})"
    );
}

#[test]
fn rrf_fuse_exact_match_boost_elevates_score() {
    // An entity whose title exactly matches the query should receive a score
    // significantly above a non-matching entity ranked first by text search.
    let exact_id = Uuid::new_v4();
    let other_id = Uuid::new_v4();
    // other_id ranks 1 in text, exact_id ranks 2 — but exact_id matches query.
    let text = vec![
        text_hit(other_id, 1, "something else"),
        text_hit(exact_id, 2, "FlashAttention"),
    ];
    let hits = rrf_fuse(text, vec![], 10, "flashattention");
    assert_eq!(hits.len(), 2);
    assert_eq!(
        hits[0].entity_id, exact_id,
        "exact match must rank first despite being rank-2 in raw text search"
    );
}

// ---- embed_batch tests ----

#[test]
fn embed_batch_unconfigured_on_memory_runtime() {
    // KhiveRuntime::memory() has no embedding model — embed_batch returns Unconfigured.
    let rt = KhiveRuntime::memory().unwrap();
    let result = tokio::runtime::Runtime::new()
        .unwrap()
        .block_on(rt.embed_batch(&[]));
    // Empty slice short-circuits before hitting the model check.
    assert!(result.is_ok());
    assert!(result.unwrap().is_empty());
}

#[test]
fn embed_batch_empty_input_returns_empty_vec() {
    // No model needed — empty slice is handled before the embedder is touched.
    let rt = KhiveRuntime::memory().unwrap();
    let result = tokio::runtime::Runtime::new()
        .unwrap()
        .block_on(rt.embed_batch(&[]));
    assert_eq!(result.unwrap(), Vec::<Vec<f32>>::new());
}

#[test]
fn embed_batch_no_model_non_empty_returns_unconfigured() {
    let rt = KhiveRuntime::memory().unwrap();
    let texts = vec!["hello".to_string()];
    let result = tokio::runtime::Runtime::new()
        .unwrap()
        .block_on(rt.embed_batch(&texts));
    match result {
        Err(crate::RuntimeError::Unconfigured(s)) => assert_eq!(s, "embedding_model"),
        Err(other) => panic!("expected Unconfigured, got {:?}", other),
        Ok(_) => panic!("expected Err, got Ok"),
    }
}

#[test]
#[ignore = "loads ~80 MB model; run with --include-ignored"]
fn embed_batch_count_matches_input() {
    let config = RuntimeConfig {
        db_path: None,
        default_namespace: Namespace::parse("test").unwrap(),
        embedding_model: Some(EmbeddingModel::AllMiniLmL6V2),
        packs: vec!["kg".to_string()],
        ..RuntimeConfig::default()
    };
    let rt = KhiveRuntime::new(config).unwrap();
    let texts: Vec<String> = vec!["foo".to_string(), "bar".to_string(), "baz".to_string()];
    let result = tokio::runtime::Runtime::new()
        .unwrap()
        .block_on(rt.embed_batch(&texts));
    let embeddings = result.unwrap();
    assert_eq!(embeddings.len(), texts.len());
}

#[test]
fn vector_search_requires_embedding_or_text() {
    let rt = KhiveRuntime::memory().unwrap();
    let tok = NamespaceToken::local();
    let result = tokio::runtime::Runtime::new()
        .unwrap()
        .block_on(rt.vector_search(&tok, None, None, 10, Some(SubstrateKind::Entity)));
    match result {
        Err(crate::RuntimeError::InvalidInput(msg)) => {
            assert!(msg.contains("query_embedding or query_text"), "msg: {msg}");
        }
        other => panic!("expected InvalidInput, got {other:?}"),
    }
}

#[test]
fn vector_search_text_without_model_returns_unconfigured() {
    let rt = KhiveRuntime::memory().unwrap();
    let tok = NamespaceToken::local();
    let result = tokio::runtime::Runtime::new()
        .unwrap()
        .block_on(rt.vector_search(
            &tok,
            None,
            Some("attention"),
            10,
            Some(SubstrateKind::Entity),
        ));
    match result {
        Err(crate::RuntimeError::Unconfigured(s)) => assert_eq!(s, "embedding_model"),
        other => panic!("expected Unconfigured, got {other:?}"),
    }
}

#[test]
#[ignore = "loads ~80 MB model; run with --include-ignored"]
fn embed_batch_vectors_have_expected_dimensions() {
    let model = EmbeddingModel::AllMiniLmL6V2;
    let config = RuntimeConfig {
        db_path: None,
        default_namespace: Namespace::parse("test").unwrap(),
        embedding_model: Some(model),
        packs: vec!["kg".to_string()],
        ..RuntimeConfig::default()
    };
    let rt = KhiveRuntime::new(config).unwrap();
    let texts = vec!["hello world".to_string()];
    let result = tokio::runtime::Runtime::new()
        .unwrap()
        .block_on(rt.embed_batch(&texts));
    let embeddings = result.unwrap();
    assert_eq!(embeddings[0].len(), model.dimensions());
}

// ---- hybrid_search_outcome: vector-arm failure must not discard text hits ----

/// Baseline (red without the fix): the fail-loud `hybrid_search` must
/// still propagate a vector-arm failure as a whole-call error, even
/// though the text leg found a match — proving `break_vector_arm`
/// genuinely exercises the vector leg and that `hybrid_search_outcome`'s
/// tolerance below is a real behavioral difference, not a no-op.
#[tokio::test]
async fn hybrid_search_still_fails_loud_on_vector_arm_error() {
    let (rt, fail) = runtime_with_toggleable_embeddings();
    let tok = NamespaceToken::local();
    rt.create_entity(
        &tok,
        "concept",
        None,
        "FlashAttention",
        Some("IO-aware exact attention using tiling"),
        None,
        vec![],
    )
    .await
    .unwrap();
    break_vector_arm(&fail);

    let result = rt
        .hybrid_search(&tok, "FlashAttention", None, 10, None, None, &[], None)
        .await;

    assert!(
        result.is_err(),
        "the fail-loud entry point must still propagate a vector-arm failure, got {result:?}"
    );
}

/// Green: the coordinator-only outcome variant preserves the text hit and
/// reports the vector failure separately instead of discarding the whole
/// call.
#[tokio::test]
async fn hybrid_search_outcome_preserves_text_hits_on_vector_arm_error() {
    let (rt, fail) = runtime_with_toggleable_embeddings();
    let tok = NamespaceToken::local();
    rt.create_entity(
        &tok,
        "concept",
        None,
        "FlashAttention",
        Some("IO-aware exact attention using tiling"),
        None,
        vec![],
    )
    .await
    .unwrap();
    break_vector_arm(&fail);

    let outcome = rt
        .hybrid_search_outcome(&tok, "FlashAttention", 10, None, None, &[], None)
        .await
        .expect("text leg must still succeed");

    assert!(
        !outcome.hits.is_empty(),
        "text arm's hit must survive a vector-arm failure"
    );
    assert!(
        outcome.hits[0]
            .title
            .as_deref()
            .unwrap_or_default()
            .contains("FlashAttention"),
        "surviving hit must be the text match"
    );
    let vector_error = outcome
        .vector_error
        .expect("vector arm failure must be reported");
    assert!(
        vector_error.contains("injected vector-arm failure"),
        "vector_error must carry the underlying cause, got {vector_error:?}"
    );
}

/// A healthy vector arm still leaves `vector_error` `None` — the outcome
/// variant does not manufacture a failure where there is none.
#[tokio::test]
async fn hybrid_search_outcome_has_no_vector_error_when_vector_arm_healthy() {
    let rt = runtime_with_constant_embeddings();
    let tok = NamespaceToken::local();
    rt.create_entity(
        &tok,
        "concept",
        None,
        "FlashAttention",
        Some("IO-aware exact attention using tiling"),
        None,
        vec![],
    )
    .await
    .unwrap();

    let outcome = rt
        .hybrid_search_outcome(&tok, "FlashAttention", 10, None, None, &[], None)
        .await
        .expect("hybrid search must succeed");

    assert!(!outcome.hits.is_empty(), "should find the entity");
    assert!(
        outcome.vector_error.is_none(),
        "a healthy vector arm must not report an error"
    );
}

// ---- hybrid_search enrichment ----

#[tokio::test]
async fn hybrid_search_entity_hit_has_title() {
    let rt = KhiveRuntime::memory().unwrap();
    let tok = NamespaceToken::local();
    rt.create_entity(
        &tok,
        "concept",
        None,
        "FlashAttention",
        Some("IO-aware exact attention using tiling"),
        None,
        vec![],
    )
    .await
    .unwrap();

    let hits = rt
        .hybrid_search(&tok, "FlashAttention", None, 10, None, None, &[], None)
        .await
        .unwrap();

    assert!(!hits.is_empty(), "should find the entity");
    let hit = &hits[0];
    assert!(hit.title.is_some(), "title must be populated");
    assert!(
        hit.title.as_deref().unwrap().contains("FlashAttention"),
        "title must contain entity name"
    );
}

/// `hybrid_search` must not hard-fail on a query containing FTS5 metacharacters
/// like `$` (e.g. the DSL doc query `$prev.id`). `sanitize_fts5_query` (khive-db)
/// strips `$`, so this exercises the sanitizer path and takes the `Ok` arm; see
/// `hybrid_search_with_residual_fts5_char_now_sanitized` below for the character
/// class #916 closed (previously the fail-loud arm, prior to #916).
#[tokio::test]
async fn hybrid_search_with_dollar_sign_query_does_not_error() {
    let rt = KhiveRuntime::memory().unwrap();
    let tok = NamespaceToken::local();
    rt.create_entity(
        &tok,
        "concept",
        None,
        "DSL docs",
        Some("use $prev.id to chain calls"),
        None,
        vec![],
    )
    .await
    .unwrap();

    let result = rt
        .hybrid_search(&tok, "$prev.id", None, 10, None, None, &[], None)
        .await;

    assert!(
        result.is_ok(),
        "#388 hybrid_search must not hard-fail on a '$'-bearing query, got: {:?}",
        result.err()
    );
}

/// #916: `@` was previously NOT stripped by `sanitize_fts5_query` and SQLite
/// FTS5's bareword parser rejected it unconditionally, so this query used to
/// reach the runtime-level fail-loud arm (`RuntimeError::InvalidInput`, #569).
/// `sanitize_fts5_token_group`'s bareword-safety gate now recognizes `@` (and
/// every other ASCII punctuation character not already handled) as unsafe for
/// an unquoted bareword position and routes it through the quoted-phrase
/// alternative instead, which FTS5 accepts literally, so the query
/// now succeeds and finds the seeded content rather than erroring.
#[tokio::test]
async fn hybrid_search_with_residual_fts5_char_now_sanitized() {
    let rt = KhiveRuntime::memory().unwrap();
    let tok = NamespaceToken::local();
    rt.create_entity(
        &tok,
        "concept",
        None,
        "DSL docs",
        Some("use foo@bar to chain calls"),
        None,
        vec![],
    )
    .await
    .unwrap();

    let result = rt
        .hybrid_search(&tok, "foo@bar", None, 10, None, None, &[], None)
        .await;

    let hits = result.unwrap_or_else(|e| {
        panic!("#916 hybrid_search must not fail on an '@'-bearing query, got: {e:?}")
    });
    assert!(
        !hits.is_empty(),
        "#916 '@'-bearing query must still find the seeded 'foo@bar' content via the \
             quoted-phrase alternative"
    );
}

/// #916 end-to-end regression, using the exact character classes from the
/// issue's live-log evidence (`#682 Stage 2`, `Min-K%Prob`, `B=128`):
/// `hybrid_search`'s FTS leg must not lose its lexical signal to a parser
/// syntax error on `#`, `%`, or `=`. Each query below must both succeed
/// and actually surface a `Text`/`Both`-sourced hit, proving the FTS leg
/// contributed, not just that the vector leg papered over a degraded
/// text leg.
#[tokio::test]
async fn hybrid_search_with_916_issue_characters_finds_text_leg_hits() {
    let rt = KhiveRuntime::memory().unwrap();
    let tok = NamespaceToken::local();

    rt.create_entity(
        &tok,
        "concept",
        None,
        "issue tracker",
        Some("tracking #682 Stage 2: MoE expert-cache prefetch work"),
        None,
        vec![],
    )
    .await
    .unwrap();
    rt.create_entity(
        &tok,
        "concept",
        None,
        "benchmark notes",
        Some("chunkwise B=128 traffic arithmetic simdgroup_matrix DPLR"),
        None,
        vec![],
    )
    .await
    .unwrap();
    rt.create_entity(
        &tok,
        "concept",
        None,
        "sampling notes",
        Some("evaluated with the Min-K%Prob membership inference method"),
        None,
        vec![],
    )
    .await
    .unwrap();

    for query in ["#682 Stage 2", "B=128", "Min-K%Prob"] {
        let result = rt
            .hybrid_search(&tok, query, None, 10, None, None, &[], None)
            .await;
        let hits = result.unwrap_or_else(|e| {
            panic!("#916 hybrid_search must not fail on query {query:?}, got: {e:?}")
        });
        assert!(
            hits.iter()
                .any(|h| matches!(h.source, SearchSource::Text | SearchSource::Both)),
            "#916 query {query:?} must surface a Text/Both-sourced hit \
                 (the FTS leg must contribute, not just the vector leg); got {hits:?}"
        );
    }
}

// ---- predicate pushdown before truncation ----

/// Entity-branch tag-filter regression.
///
/// Scenario: `limit=1`, tag_filter=["target-tag"]. Two entities are inserted:
///   - "decoy_alpha_beta_gamma": many query tokens → ranks 1 in FTS (dominates).
///     Does NOT have "target-tag".
///   - "alpha_beta_gamma target": fewer query tokens → ranks 2 in FTS.
///     HAS "target-tag".
///
/// Without predicate pushdown, `fused.truncate(1)` keeps only the decoy and the
/// tag-matching entity is invisible: this requires `tags_any` to be passed into
/// `query_entities`'s `EntityFilter` so the decoy is excluded before truncation.
#[tokio::test]
async fn hybrid_search_tag_filter_pushed_before_truncation() {
    let rt = KhiveRuntime::memory().unwrap();
    let tok = NamespaceToken::local();

    // Decoy: high-ranking FTS hit (content repeats query words), no target tag.
    rt.create_entity(
        &tok,
        "concept",
        None,
        "alpha beta gamma decoy alpha beta gamma",
        Some("alpha beta gamma decoy description alpha beta gamma"),
        None,
        vec!["other-tag".to_string()],
    )
    .await
    .unwrap();

    // Target: lower-ranking FTS hit, has the tag we filter on.
    let target = rt
        .create_entity(
            &tok,
            "concept",
            None,
            "alpha beta gamma target",
            Some("alpha beta gamma target description"),
            None,
            vec!["target-tag".to_string()],
        )
        .await
        .unwrap();

    // With limit=1 and tag_filter, the fix must return the target entity despite
    // the decoy ranking higher. Without pushdown, the decoy occupies the single
    // slot and the target is silently dropped.
    let hits = rt
        .hybrid_search(
            &tok,
            "alpha beta gamma",
            None,
            1,
            None,
            None,
            &["target-tag".to_string()],
            None,
        )
        .await
        .unwrap();

    assert_eq!(
        hits.len(),
        1,
        "exactly one hit expected (the tag-matching entity)"
    );
    assert_eq!(
        hits[0].entity_id, target.id,
        "the tag-filtered entity must be returned even when ranked below limit in raw fusion"
    );
}

/// Entity-branch properties-filter regression (analogous to the tag-filter test above).
///
/// Scenario: `limit=1`, properties_filter={{"domain": "target"}}. Two entities:
///   - decoy: high FTS rank, properties {{"domain": "other"}}.
///   - target: lower FTS rank, properties {{"domain": "target"}}.
///
/// Without pushdown: decoy fills the slot, target is dropped. With pushdown:
/// only the target survives the properties filter before truncation.
#[tokio::test]
async fn hybrid_search_props_filter_pushed_before_truncation() {
    let rt = KhiveRuntime::memory().unwrap();
    let tok = NamespaceToken::local();

    rt.create_entity(
        &tok,
        "concept",
        None,
        "delta epsilon zeta decoy delta epsilon zeta",
        Some("delta epsilon zeta decoy description delta epsilon zeta"),
        Some(serde_json::json!({"domain": "other"})),
        vec![],
    )
    .await
    .unwrap();

    let target = rt
        .create_entity(
            &tok,
            "concept",
            None,
            "delta epsilon zeta target",
            Some("delta epsilon zeta target description"),
            Some(serde_json::json!({"domain": "target"})),
            vec![],
        )
        .await
        .unwrap();

    let filter = serde_json::json!({"domain": "target"});
    let hits = rt
        .hybrid_search(
            &tok,
            "delta epsilon zeta",
            None,
            1,
            None,
            None,
            &[],
            Some(&filter),
        )
        .await
        .unwrap();

    assert_eq!(hits.len(), 1, "exactly one hit expected (properties match)");
    assert_eq!(
        hits[0].entity_id, target.id,
        "the properties-filtered entity must be returned even when ranked below limit"
    );
}

/// Entity-branch kind-filter regression.
///
/// Scenario: `limit=1`, so the text arm fetches 4 candidates, and
/// `entity_kind="document"`. Twelve `concept` entities repeat the query term and
/// outrank the one `document` entity that mentions it once in a long description.
///
/// When the kind is applied only after the text arm has picked its candidates,
/// the filter sees four concepts and the document is never returned. The kind
/// has to reach the text query so the candidate budget is spent on documents.
#[tokio::test]
async fn hybrid_search_entity_kind_filter_pushed_into_text_arm() {
    let rt = KhiveRuntime::memory().unwrap();
    let tok = NamespaceToken::local();

    for i in 0..12 {
        rt.create_entity(
            &tok,
            "concept",
            None,
            &format!("quillfeather decoy {i}"),
            Some("quillfeather quillfeather quillfeather"),
            None,
            vec![],
        )
        .await
        .unwrap();
    }

    let description = format!(
        "A long administrative description that mentions quillfeather once. {}",
        "Unrelated filing, scheduling and review words. ".repeat(20)
    );
    let target = rt
        .create_entity(
            &tok,
            "document",
            None,
            "Archive filing report",
            Some(description.as_str()),
            None,
            vec![],
        )
        .await
        .unwrap();

    // Premise: without a kind filter the target ranks behind more entities than
    // the 4 candidates the filtered `limit=1` search requests from the text arm.
    let unfiltered = rt
        .hybrid_search(&tok, "quillfeather", None, 13, None, None, &[], None)
        .await
        .unwrap();
    let target_rank = unfiltered
        .iter()
        .position(|hit| hit.entity_id == target.id)
        .expect("the document is found without a kind filter");
    assert!(
        target_rank >= 4,
        "the document must rank behind the candidate window, got rank {target_rank}"
    );

    let hits = rt
        .hybrid_search(
            &tok,
            "quillfeather",
            None,
            1,
            Some("document"),
            None,
            &[],
            None,
        )
        .await
        .unwrap();

    assert_eq!(
        hits.len(),
        1,
        "the kind-filtered search must return the matching document"
    );
    assert_eq!(
        hits[0].entity_id, target.id,
        "the returned hit must be the document, not a concept"
    );
}

/// `hybrid_search_each_kind` gives each kind the list `hybrid_search` returns for it.
///
/// Scenario: twenty `concept` entities repeat the query term and outrank three
/// `document` entities that mention it once, with `limit=2`. The text arm fetches 8
/// candidates per kind, so the concepts alone overflow it and the cut to `limit`
/// binds. A text budget shared by the two kinds would leave the documents none; each
/// kind has to keep its own, so both lists equal the per-kind search.
#[tokio::test]
async fn hybrid_search_each_kind_matches_per_kind_search_when_one_kind_dominates() {
    let rt = KhiveRuntime::memory().unwrap();
    let tok = NamespaceToken::local();

    for i in 0..20 {
        rt.create_entity(
            &tok,
            "concept",
            None,
            &format!("quillfeather decoy {i}"),
            Some("quillfeather quillfeather quillfeather"),
            None,
            vec![],
        )
        .await
        .unwrap();
    }
    for i in 0..3 {
        let description = format!(
            "A long administrative description {i} that mentions quillfeather once. {}",
            "Unrelated filing, scheduling and review words. ".repeat(20)
        );
        rt.create_entity(
            &tok,
            "document",
            None,
            &format!("Archive filing report {i}"),
            Some(description.as_str()),
            None,
            vec![],
        )
        .await
        .unwrap();
    }

    let kinds = ["concept", "document"];
    let mut expected: Vec<Vec<Uuid>> = Vec::new();
    for kind in kinds {
        let hits = rt
            .hybrid_search(&tok, "quillfeather", None, 2, Some(kind), None, &[], None)
            .await
            .unwrap();
        expected.push(hits.iter().map(|hit| hit.entity_id).collect());
    }
    assert_eq!(
        expected[0].len(),
        2,
        "premise: the concept list is cut to limit"
    );
    assert_eq!(
        expected[1].len(),
        2,
        "premise: the document list is not starved"
    );

    let per_kind = rt
        .hybrid_search_each_kind(&tok, "quillfeather", None, 2, &kinds)
        .await
        .unwrap();
    let mut actual: Vec<Vec<Uuid>> = Vec::new();
    for hits in &per_kind {
        actual.push(hits.iter().map(|hit| hit.entity_id).collect());
    }
    assert_eq!(
        actual, expected,
        "each kind must get the ids and order its own per-kind search returns"
    );
}

// ---- embed intent tests ----

struct CapturingEmbeddingService {
    captured: std::sync::Arc<std::sync::Mutex<Vec<Vec<String>>>>,
}

#[async_trait::async_trait]
impl EmbeddingService for CapturingEmbeddingService {
    async fn embed(
        &self,
        texts: &[String],
        _model: EmbeddingModel,
    ) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
        self.captured.lock().unwrap().push(texts.to_vec());
        Ok(texts.iter().map(|_| vec![1.0]).collect())
    }

    fn supports_model(&self, _model: EmbeddingModel) -> bool {
        true
    }

    fn name(&self) -> &'static str {
        "capturing-embedding-service"
    }
}

struct CapturingEmbedderProvider {
    name: String,
    captured: std::sync::Arc<std::sync::Mutex<Vec<Vec<String>>>>,
}

struct RewritingPassageService {
    captured: std::sync::Arc<std::sync::Mutex<Vec<Vec<String>>>>,
}

#[async_trait::async_trait]
impl EmbeddingService for RewritingPassageService {
    async fn embed(
        &self,
        texts: &[String],
        _model: EmbeddingModel,
    ) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
        self.captured.lock().unwrap().push(texts.to_vec());
        Ok(texts.iter().map(|_| vec![1.0]).collect())
    }

    async fn embed_passage(
        &self,
        texts: &[String],
        model: EmbeddingModel,
    ) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
        let prepared: Vec<String> = texts.iter().map(|text| format!("custom:{text}")).collect();
        self.embed(&prepared, model).await
    }

    fn supports_model(&self, _model: EmbeddingModel) -> bool {
        true
    }

    fn name(&self) -> &'static str {
        "rewriting-passage-service"
    }
}

struct RewritingPassageProvider {
    name: String,
    captured: std::sync::Arc<std::sync::Mutex<Vec<Vec<String>>>>,
}

#[async_trait::async_trait]
impl EmbedderProvider for RewritingPassageProvider {
    fn name(&self) -> &str {
        &self.name
    }

    fn dimensions(&self) -> usize {
        1
    }

    async fn build(&self) -> crate::error::RuntimeResult<std::sync::Arc<dyn EmbeddingService>> {
        Ok(std::sync::Arc::new(RewritingPassageService {
            captured: std::sync::Arc::clone(&self.captured),
        }))
    }
}

struct WrongCardinalityEmbeddingService;

#[async_trait::async_trait]
impl EmbeddingService for WrongCardinalityEmbeddingService {
    async fn embed(
        &self,
        texts: &[String],
        _model: EmbeddingModel,
    ) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
        Ok(texts
            .iter()
            .take(texts.len().saturating_sub(1))
            .map(|_| vec![1.0])
            .collect())
    }

    fn supports_model(&self, _model: EmbeddingModel) -> bool {
        true
    }

    fn name(&self) -> &'static str {
        "wrong-cardinality-embedding-service"
    }
}

struct WrongCardinalityEmbedderProvider;

#[async_trait::async_trait]
impl EmbedderProvider for WrongCardinalityEmbedderProvider {
    fn name(&self) -> &str {
        "wrong-cardinality-embedding-service"
    }

    fn dimensions(&self) -> usize {
        1
    }

    async fn build(&self) -> crate::error::RuntimeResult<std::sync::Arc<dyn EmbeddingService>> {
        Ok(std::sync::Arc::new(WrongCardinalityEmbeddingService))
    }
}

struct SurplusCardinalityEmbeddingService;

#[async_trait::async_trait]
impl EmbeddingService for SurplusCardinalityEmbeddingService {
    async fn embed(
        &self,
        texts: &[String],
        _model: EmbeddingModel,
    ) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
        let mut vectors: Vec<Vec<f32>> = texts.iter().map(|_| vec![1.0]).collect();
        vectors.push(vec![1.0]);
        Ok(vectors)
    }

    fn supports_model(&self, _model: EmbeddingModel) -> bool {
        true
    }

    fn name(&self) -> &'static str {
        "surplus-cardinality-embedding-service"
    }
}

struct SurplusCardinalityEmbedderProvider;

#[async_trait::async_trait]
impl EmbedderProvider for SurplusCardinalityEmbedderProvider {
    fn name(&self) -> &str {
        "surplus-cardinality-embedding-service"
    }

    fn dimensions(&self) -> usize {
        1
    }

    async fn build(&self) -> crate::error::RuntimeResult<std::sync::Arc<dyn EmbeddingService>> {
        Ok(std::sync::Arc::new(SurplusCardinalityEmbeddingService))
    }
}

#[async_trait::async_trait]
impl EmbedderProvider for CapturingEmbedderProvider {
    fn name(&self) -> &str {
        &self.name
    }

    fn dimensions(&self) -> usize {
        1
    }

    async fn build(&self) -> crate::error::RuntimeResult<std::sync::Arc<dyn EmbeddingService>> {
        Ok(std::sync::Arc::new(CapturingEmbeddingService {
            captured: std::sync::Arc::clone(&self.captured),
        }))
    }
}

fn runtime_with_capturing_embedder(
    model: EmbeddingModel,
) -> (
    KhiveRuntime,
    std::sync::Arc<std::sync::Mutex<Vec<Vec<String>>>>,
) {
    let runtime = KhiveRuntime::memory().unwrap();
    let captured = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    runtime.register_embedder(CapturingEmbedderProvider {
        name: model.to_string(),
        captured: std::sync::Arc::clone(&captured),
    });
    (runtime, captured)
}

#[tokio::test]
async fn bge_query_paths_pass_raw_unprefixed_text() {
    const BGE_QUERY_INSTRUCTION: &str = "Represent this sentence for searching relevant passages: ";
    let single = "single raw query";
    let batch = vec![
        "first raw query".to_string(),
        "second raw query".to_string(),
    ];

    for model in [
        EmbeddingModel::BgeSmallEnV15,
        EmbeddingModel::BgeBaseEnV15,
        EmbeddingModel::BgeLargeEnV15,
    ] {
        let (runtime, captured) = runtime_with_capturing_embedder(model);
        runtime
            .embed_query_with_model(&model.to_string(), single)
            .await
            .unwrap();
        runtime
            .embed_query_batch_with_model(&model.to_string(), &batch)
            .await
            .unwrap();

        let calls = captured.lock().unwrap().clone();
        assert_eq!(
            calls,
            vec![vec![single.to_string()], batch.clone()],
            "{model} must receive raw query text through single and batch paths"
        );
        assert!(
            calls
                .iter()
                .flatten()
                .all(|text| !text.contains(BGE_QUERY_INSTRUCTION)),
            "{model} must not receive the BGE retrieval instruction"
        );
    }
}

#[tokio::test]
async fn e5_query_paths_apply_query_prefix() {
    let model = EmbeddingModel::MultilingualE5Small;
    let single = "single raw query";
    let batch = vec![
        "first raw query".to_string(),
        "second raw query".to_string(),
    ];
    let (runtime, captured) = runtime_with_capturing_embedder(model);

    runtime
        .embed_query_with_model(&model.to_string(), single)
        .await
        .unwrap();
    runtime
        .embed_query_batch_with_model(&model.to_string(), &batch)
        .await
        .unwrap();

    assert_eq!(
        captured.lock().unwrap().as_slice(),
        [
            vec!["query: single raw query".to_string()],
            vec![
                "query: first raw query".to_string(),
                "query: second raw query".to_string(),
            ],
        ],
        "E5 must receive its query prefix through single and batch paths"
    );
}

#[tokio::test]
async fn mixed_document_batch_stays_one_ordered_provider_call() {
    let model = EmbeddingModel::AllMiniLmL6V2;
    let (runtime, captured) = runtime_with_capturing_embedder(model);
    let texts = vec![
        "first normal document".to_string(),
        "x".repeat(MAX_TEXT_BYTES + 1),
        "second normal document".to_string(),
    ];

    let outcomes = runtime
        .embed_document_batch_with_model_outcomes(&model.to_string(), &texts)
        .await
        .expect("mixed batch must embed");
    assert_eq!(outcomes.len(), texts.len());
    assert!(!outcomes[0].truncated);
    assert_eq!(outcomes[0].source_bytes, outcomes[0].embedded_bytes);
    assert!(outcomes[1].truncated);
    assert_eq!(outcomes[1].source_bytes, MAX_TEXT_BYTES + 1);
    assert_eq!(outcomes[1].embedded_bytes, MAX_TEXT_BYTES);
    assert!(!outcomes[2].truncated);
    assert_eq!(
        captured.lock().unwrap().as_slice(),
        [vec![
            texts[0].clone(),
            "x".repeat(MAX_TEXT_BYTES),
            texts[2].clone(),
        ]]
    );
}

#[tokio::test]
async fn canonical_name_override_has_no_exact_input_fingerprint() {
    let model = EmbeddingModel::MultilingualE5Small;
    let model_name = model.to_string();
    let (runtime, captured) = runtime_with_capturing_embedder(model);
    let input = "x".repeat(document_embedding_budget(&model_name) + 3);
    let outcomes = runtime
        .embed_document_batch_with_model_outcomes(&model_name, std::slice::from_ref(&input))
        .await
        .unwrap();
    let calls = captured.lock().unwrap();
    let actual_input = &calls[0][0];
    assert!(actual_input.starts_with("passage: "));
    assert_eq!(actual_input.len(), MAX_TEXT_BYTES);
    assert!(outcomes[0].truncated);
    assert_eq!(outcomes[0].prepared_text_fingerprint, None);
    assert_eq!(
        prepared_document_fingerprint(
            bounded_embedding_input(&input, document_embedding_budget(&model_name)).0,
            model,
        ),
        VectorRecord::fingerprint_text(actual_input),
        "the audited lattice path hashes the bounded text plus passage prefix"
    );
}

#[tokio::test]
async fn ordinary_custom_document_provider_has_no_exact_input_fingerprint() {
    let runtime = KhiveRuntime::memory().unwrap();
    let captured = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    runtime.register_embedder(CapturingEmbedderProvider {
        name: "custom-document-provider".to_string(),
        captured: std::sync::Arc::clone(&captured),
    });

    let outcome = runtime
        .embed_document_with_model_outcome("custom-document-provider", "document")
        .await
        .expect("custom provider must embed");
    assert_eq!(outcome.prepared_text_fingerprint, None);
    assert_eq!(captured.lock().unwrap().len(), 1);
}

#[tokio::test]
async fn custom_passage_override_under_builtin_name_cannot_claim_exact_input() {
    let runtime = KhiveRuntime::memory().unwrap();
    let captured = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let model = EmbeddingModel::MultilingualE5Small;
    let model_name = model.to_string();
    runtime.register_embedder(RewritingPassageProvider {
        name: model_name.clone(),
        captured: std::sync::Arc::clone(&captured),
    });

    let outcome = runtime
        .embed_document_with_model_outcome(&model_name, "document")
        .await
        .expect("custom override must embed");
    assert_eq!(outcome.prepared_text_fingerprint, None);
    assert_eq!(
        captured.lock().unwrap()[0],
        vec!["custom:document".to_string()]
    );
    assert_ne!(
        prepared_document_fingerprint("document", model),
        VectorRecord::fingerprint_text("custom:document")
    );
}

#[tokio::test]
async fn truncated_document_batch_rejects_provider_cardinality_mismatch() {
    let runtime = KhiveRuntime::memory().unwrap();
    runtime.register_embedder(WrongCardinalityEmbedderProvider);
    let model_name = "wrong-cardinality-embedding-service";
    let texts = vec!["x".repeat(MAX_TEXT_BYTES + 1), "normal".to_string()];

    let error = runtime
        .embed_document_batch_with_model_outcomes(model_name, &texts)
        .await
        .expect_err("provider cardinality mismatch must fail the whole batch");

    assert!(
        error
            .to_string()
            .contains("embed_passage returned 1 vectors for 2 inputs"),
        "unexpected error: {error}"
    );
}

#[tokio::test]
async fn singleton_document_embed_rejects_zero_vectors() {
    let runtime = KhiveRuntime::memory().unwrap();
    runtime.register_embedder(WrongCardinalityEmbedderProvider);
    let model_name = "wrong-cardinality-embedding-service";

    let error = runtime
        .embed_document_with_model_outcome(model_name, "single document")
        .await
        .expect_err("provider returning zero vectors must fail closed");

    assert!(
        error
            .to_string()
            .contains("embed_passage returned 0 vectors for 1 input"),
        "unexpected error: {error}"
    );
}

#[tokio::test]
async fn singleton_document_embed_rejects_surplus_vectors() {
    let runtime = KhiveRuntime::memory().unwrap();
    runtime.register_embedder(SurplusCardinalityEmbedderProvider);
    let model_name = "surplus-cardinality-embedding-service";

    let error = runtime
        .embed_document_with_model_outcome(model_name, "single document")
        .await
        .expect_err("provider returning surplus vectors must fail closed");

    assert!(
        error
            .to_string()
            .contains("embed_passage returned 2 vectors for 1 input"),
        "unexpected error: {error}"
    );
}

#[test]
#[ignore = "loads ~80 MB model; run with --include-ignored"]
fn minilm_document_and_query_embed_are_identical_no_prefix_model() {
    // MiniLM has no instruction prefixes; document and query paths must
    // produce byte-identical vectors so that existing stored vectors remain
    // comparable after this change.
    let model = EmbeddingModel::AllMiniLmL6V2;
    let config = RuntimeConfig {
        db_path: None,
        default_namespace: Namespace::parse("test").unwrap(),
        embedding_model: Some(model),
        packs: vec!["kg".to_string()],
        ..RuntimeConfig::default()
    };
    let rt = KhiveRuntime::new(config).unwrap();
    let text = "attention is all you need".to_string();
    let rt_ref = &rt;
    let (doc_emb, query_emb) = tokio::runtime::Runtime::new().unwrap().block_on(async {
        let d = rt_ref
            .embed_document_with_model_outcome(&model.to_string(), &text)
            .await
            .unwrap()
            .vector;
        let q = rt_ref
            .embed_query_with_model(&model.to_string(), &text)
            .await
            .unwrap();
        (d, q)
    });
    assert_eq!(
        doc_emb, query_emb,
        "MiniLM has no instruction prefix: document and query embeds must be identical"
    );
}

#[test]
#[ignore = "loads multilingual-e5-small (~90 MB); run with --include-ignored"]
fn e5_document_and_query_embed_differ_instruction_tuned_model() {
    // multilingual-e5 prepends "passage: " for documents and "query: " for
    // queries. The same raw text must produce different embeddings when the
    // correct prefixes are applied, confirming the asymmetric-retrieval
    // capability is now exercised.
    let model = EmbeddingModel::MultilingualE5Small;
    let config = RuntimeConfig {
        db_path: None,
        default_namespace: Namespace::parse("test").unwrap(),
        embedding_model: Some(model),
        packs: vec!["kg".to_string()],
        ..RuntimeConfig::default()
    };
    let rt = KhiveRuntime::new(config).unwrap();
    let text = "attention is all you need".to_string();
    let rt_ref = &rt;
    let (doc_emb, query_emb) = tokio::runtime::Runtime::new().unwrap().block_on(async {
        let d = rt_ref
            .embed_document_with_model_outcome(&model.to_string(), &text)
            .await
            .unwrap()
            .vector;
        let q = rt_ref
            .embed_query_with_model(&model.to_string(), &text)
            .await
            .unwrap();
        (d, q)
    });
    assert_ne!(
        doc_emb, query_emb,
        "multilingual-e5-small uses asymmetric prefixes: document ('passage: ') \
             and query ('query: ') embeds of the same text must differ"
    );
}

// ---- backfill reader error must be propagated, not swallowed ----

use crate::embedder_registry::EmbedderProvider;
use lattice_embed::EmbeddingService;

/// A stub embedder that never actually loads weights — used to satisfy the
/// `registered_embedding_model_names` check inside `backfill_missing_embeddings`
/// without triggering a real model load. The test fault-injects a reader error
/// before any embedding call is made, so `embed()` is never reached.
struct StubEmbedderProvider;

#[async_trait::async_trait]
impl EmbedderProvider for StubEmbedderProvider {
    fn name(&self) -> &str {
        "stub-model-m07"
    }

    fn dimensions(&self) -> usize {
        4
    }

    async fn build(&self) -> crate::error::RuntimeResult<std::sync::Arc<dyn EmbeddingService>> {
        struct StubSvc;
        #[async_trait::async_trait]
        impl EmbeddingService for StubSvc {
            async fn embed(
                &self,
                _texts: &[String],
                _model: lattice_embed::EmbeddingModel,
            ) -> std::result::Result<Vec<Vec<f32>>, lattice_embed::EmbedError> {
                Ok(vec![])
            }

            fn supports_model(&self, _model: lattice_embed::EmbeddingModel) -> bool {
                true
            }

            fn name(&self) -> &'static str {
                "stub-svc-m07"
            }
        }
        Ok(std::sync::Arc::new(StubSvc))
    }
}

/// `backfill_missing_embeddings` must propagate a reader error rather than
/// treating it as "zero rows to embed" (a silent `Err(_) => vec![]` would
/// return `Ok(0)` and skip all embeddings without any signal).
///
/// The fault injection substitutes a `StorageError::Pool` for the result of
/// `sql.reader().await`, exercising the `map_err(RuntimeError::Storage)?` path;
/// falling back to `unwrap_or_default()` there would swallow the injected error.
#[tokio::test]
async fn backfill_reader_error_is_propagated_not_swallowed() {
    let rt = KhiveRuntime::memory().unwrap();
    rt.register_embedder(StubEmbedderProvider);
    let tok = NamespaceToken::local();

    // Arm the fault injection: the next backfill call will substitute a
    // StorageError at the sql.reader().await boundary, then reset.
    super::arm_backfill_reader_fail();

    let result = rt.backfill_missing_embeddings(&tok).await;
    assert!(
        result.is_err(),
        "backfill_missing_embeddings must propagate the reader error (got Ok instead)"
    );
    let err_msg = result.unwrap_err().to_string();
    assert!(
        err_msg.contains("injected failure"),
        "error must originate from the injected reader failure, got: {err_msg}"
    );
}

#[tokio::test]
async fn backfill_skips_a_full_page_of_excluded_messages_and_reaches_eligible_tail() {
    use crate::{NoteEmbeddingPolicy, NoteEmbeddingPolicySpec};
    use khive_storage::note::Note;

    const PAGE: u128 = EMBEDDING_BATCH_PAGE_SIZE as u128;
    let primary = EmbeddingModel::AllMiniLmL6V2;
    let primary_name = primary.to_string();
    let secondary_name = "zz-backfill-secondary";
    let rt = KhiveRuntime::new(RuntimeConfig {
        db_path: None,
        embedding_model: Some(primary),
        packs: vec![],
        ..RuntimeConfig::no_embeddings()
    })
    .unwrap();
    rt.register_embedder(ConstantEmbedderProvider {
        name: primary_name.clone(),
        dimensions: primary.dimensions(),
        fail: Arc::new(AtomicBool::new(false)),
    });
    rt.register_embedder(ConstantEmbedderProvider {
        name: secondary_name.into(),
        dimensions: 4,
        fail: Arc::new(AtomicBool::new(false)),
    });
    rt.install_note_embedding_policies(&[NoteEmbeddingPolicySpec {
        kind: "message",
        policy: NoteEmbeddingPolicy::DefaultModel,
    }]);
    let tok = NamespaceToken::local();
    let primary_store = rt.vectors_for_model(&tok, &primary_name).unwrap();
    let secondary_store = rt.vectors_for_model(&tok, secondary_name).unwrap();
    let notes = rt.notes(&tok).unwrap();
    let mut seeded = Vec::new();
    for ordinal in 1..=PAGE + 1 {
        let mut message = Note::new("local", "message", "excluded from secondary");
        message.id = Uuid::from_u128(ordinal);
        seeded.push(message);
    }
    let mut ordinary = Note::new("local", "observation", "eligible after full page");
    ordinary.id = Uuid::from_u128(u128::MAX);
    seeded.push(ordinary.clone());
    notes.upsert_notes(seeded).await.unwrap();

    let backfilled = tokio::time::timeout(
        std::time::Duration::from_secs(60),
        rt.backfill_missing_embeddings(&tok),
    )
    .await
    .expect("backfill must advance past a full excluded page")
    .unwrap();
    assert_eq!(backfilled, (PAGE + 2) as u64 + 1);
    assert_eq!(primary_store.count().await.unwrap(), (PAGE + 2) as u64);
    assert_eq!(secondary_store.count().await.unwrap(), 1);
    let excluded_document = rt
        .text_for_notes(&tok)
        .unwrap()
        .get_document("local", Uuid::from_u128(1))
        .await
        .unwrap()
        .expect("first-model pass must index an excluded message");
    assert_eq!(excluded_document.body, "excluded from secondary");
    assert!(
        rt.text_for_notes(&tok)
            .unwrap()
            .get_document("local", ordinary.id)
            .await
            .unwrap()
            .is_some(),
        "the first-model pass must still repopulate FTS"
    );
}

const BACKFILL_BATCH_MODEL: &str = "backfill-batch-model";

struct CountingBackfillService {
    calls: Arc<AtomicUsize>,
    reject_poison: bool,
}

#[async_trait::async_trait]
impl EmbeddingService for CountingBackfillService {
    async fn embed(
        &self,
        texts: &[String],
        _model: EmbeddingModel,
    ) -> std::result::Result<Vec<Vec<f32>>, EmbedError> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        if self.reject_poison
            && (texts.len() > 1 || texts.iter().any(|text| text.contains("poison")))
        {
            return Err(EmbedError::InferenceFailed("poison input".into()));
        }
        Ok(texts
            .iter()
            .map(|text| {
                vec![
                    text.len() as f32,
                    text.bytes().map(u32::from).sum::<u32>() as f32,
                    text.as_bytes().first().copied().unwrap_or_default() as f32,
                    text.as_bytes().last().copied().unwrap_or_default() as f32,
                ]
            })
            .collect())
    }

    fn supports_model(&self, _model: EmbeddingModel) -> bool {
        true
    }

    fn name(&self) -> &'static str {
        "backfill-batch-counting-service"
    }
}

struct CountingBackfillProvider {
    calls: Arc<AtomicUsize>,
    reject_poison: bool,
}

#[async_trait::async_trait]
impl EmbedderProvider for CountingBackfillProvider {
    fn name(&self) -> &str {
        BACKFILL_BATCH_MODEL
    }

    fn dimensions(&self) -> usize {
        4
    }

    async fn build(&self) -> crate::error::RuntimeResult<Arc<dyn EmbeddingService>> {
        Ok(Arc::new(CountingBackfillService {
            calls: Arc::clone(&self.calls),
            reject_poison: self.reject_poison,
        }))
    }
}

#[tokio::test]
async fn backfill_batches_provider_calls_and_matches_single_record_vectors() {
    let token = NamespaceToken::local();
    let runtime = KhiveRuntime::memory().unwrap();
    let mut entity_ids = Vec::new();
    for index in 0..257 {
        let entity = runtime
            .create_entity(
                &token,
                "concept",
                None,
                &format!("Backfill entity {index}"),
                (index != 0).then_some("body"),
                None,
                vec![],
            )
            .await
            .unwrap();
        entity_ids.push(entity.id);
    }
    let mut note_ids = Vec::new();
    for index in 0..3 {
        let note = runtime
            .create_note(
                &token,
                "observation",
                None,
                &format!("Backfill note {index}"),
                None,
                None,
                vec![],
            )
            .await
            .unwrap();
        note_ids.push(note.id);
    }
    let calls = Arc::new(AtomicUsize::new(0));
    runtime.register_embedder(CountingBackfillProvider {
        calls: Arc::clone(&calls),
        reject_poison: false,
    });
    let usage = crate::usage::UsageContext::new();
    let backfilled =
        crate::usage::scope(usage.clone(), runtime.backfill_missing_embeddings(&token))
            .await
            .unwrap();
    assert_eq!(backfilled, 260);
    assert_eq!(
        calls.load(Ordering::SeqCst),
        3,
        "two entity pages and one note page"
    );
    assert_eq!(
        usage.snapshot()["embed_calls"],
        260,
        "ADR-103 counts texts, not provider calls"
    );

    let singles = KhiveRuntime::memory().unwrap();
    let single_calls = Arc::new(AtomicUsize::new(0));
    singles.register_embedder(CountingBackfillProvider {
        calls: Arc::clone(&single_calls),
        reject_poison: false,
    });
    for id in &entity_ids {
        let entity = runtime.get_entity(&token, *id).await.unwrap();
        singles
            .entities(&token)
            .unwrap()
            .upsert_entity(entity.clone())
            .await
            .unwrap();
        singles.reindex_entity(&token, &entity).await.unwrap();
    }
    for id in &note_ids {
        let note = runtime
            .notes(&token)
            .unwrap()
            .get_note(*id)
            .await
            .unwrap()
            .unwrap();
        singles
            .notes(&token)
            .unwrap()
            .upsert_note(note.clone())
            .await
            .unwrap();
        singles.reindex_note(&token, &note).await.unwrap();
    }
    assert_eq!(single_calls.load(Ordering::SeqCst), 260);
    let batched_entities = runtime
        .vectors_for_model(&token, BACKFILL_BATCH_MODEL)
        .unwrap()
        .get_vectors(&entity_ids, "local", "entity.body")
        .await
        .unwrap();
    let single_entities = singles
        .vectors_for_model(&token, BACKFILL_BATCH_MODEL)
        .unwrap()
        .get_vectors(&entity_ids, "local", "entity.body")
        .await
        .unwrap();
    assert_eq!(batched_entities.len(), 257);
    assert_eq!(batched_entities, single_entities);
    let batched_notes = runtime
        .vectors_for_model(&token, BACKFILL_BATCH_MODEL)
        .unwrap()
        .get_vectors(&note_ids, "local", "note.content")
        .await
        .unwrap();
    let single_notes = singles
        .vectors_for_model(&token, BACKFILL_BATCH_MODEL)
        .unwrap()
        .get_vectors(&note_ids, "local", "note.content")
        .await
        .unwrap();
    assert_eq!(batched_notes.len(), 3);
    assert_eq!(batched_notes, single_notes);
}

#[tokio::test]
async fn backfill_failed_pages_retry_singly_without_duplicate_index_rows() {
    use khive_storage::types::{SqlStatement, SqlValue};

    let token = NamespaceToken::local();
    let runtime = KhiveRuntime::memory().unwrap();
    for name in ["good one", "poison", "good two"] {
        runtime
            .create_entity(&token, "concept", None, name, Some("body"), None, vec![])
            .await
            .unwrap();
        runtime
            .create_note(&token, "observation", None, name, None, None, vec![])
            .await
            .unwrap();
    }
    let calls = Arc::new(AtomicUsize::new(0));
    runtime.register_embedder(CountingBackfillProvider {
        calls: Arc::clone(&calls),
        reject_poison: true,
    });
    let backfilled = runtime.backfill_missing_embeddings(&token).await.unwrap();
    assert_eq!(backfilled, 4);
    assert_eq!(
        calls.load(Ordering::SeqCst),
        8,
        "two failed pages plus six singleton retries"
    );
    let mut reader = runtime.sql().reader().await.unwrap();
    for (table, expected) in [("ann_write_log", 4), ("vector_provenance", 0)] {
        let count = reader
            .query_scalar(SqlStatement {
                sql: format!("SELECT COUNT(*) FROM {table} WHERE namespace = ?1"),
                params: vec![SqlValue::Text("local".into())],
                label: Some("backfill-batch-no-duplicate-rows".into()),
            })
            .await
            .unwrap();
        assert!(
            matches!(count, Some(SqlValue::Integer(value)) if value == expected),
            "{table} must match the guarded per-record writer"
        );
    }
    assert_eq!(
        runtime.backfill_missing_embeddings(&token).await.unwrap(),
        0
    );
}