khive-pack-knowledge 0.17.0

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

//! Integration tests for the knowledge pack against an in-memory runtime.

use khive_pack_kg::KgPack;
use khive_pack_knowledge::KnowledgePack;
use khive_runtime::{
    Gate, GateDecision, GateError, GateRequest, KhiveRuntime, PackRegistry, RequestIdentity,
    RuntimeError, VerbRegistry, VerbRegistryBuilder,
};
use khive_storage::{SqlStatement, SqlValue};
use serde_json::{json, Value};
use std::sync::{Arc, Mutex};

// ── test fixture ──────────────────────────────────────────────────────────────

fn rt() -> KhiveRuntime {
    KhiveRuntime::memory().expect("memory runtime")
}

struct Fixture {
    registry: VerbRegistry,
}

#[derive(Debug, Default)]
struct NestedProfileIdentityGate {
    requests: Mutex<Vec<(String, String, String)>>,
}

impl Gate for NestedProfileIdentityGate {
    fn check(&self, req: &GateRequest) -> Result<GateDecision, GateError> {
        self.requests.lock().expect("gate request lock").push((
            req.verb.clone(),
            req.actor.id.clone(),
            req.namespace.as_str().to_string(),
        ));
        if matches!(req.verb.as_str(), "brain.resolve" | "brain.profile")
            && req.actor.id != "requester"
        {
            return Ok(GateDecision::deny(
                "nested profile reads require the per-request principal",
            ));
        }
        Ok(GateDecision::allow())
    }
}

impl Fixture {
    async fn dispatch(&self, verb: &str, args: Value) -> Result<Value, RuntimeError> {
        self.registry.dispatch(verb, args).await
    }
}

fn pack(rt: KhiveRuntime) -> Fixture {
    let mut builder = VerbRegistryBuilder::new();
    builder.register(KgPack::new(rt.clone()));
    builder.register(KnowledgePack::new(rt.clone()));
    let registry = builder.build().expect("registry builds");
    registry.apply_schema_plans(rt.backend());
    rt.install_edge_rules(registry.all_edge_rules());
    Fixture { registry }
}

include!("integration_tests/learn_and_registration_tests.rs");

include!("integration_tests/cite_tests.rs");

include!("integration_tests/topic_listing_tests.rs");

include!("integration_tests/topic_query_tests.rs");

include!("integration_tests/atom_upsert_tests.rs");

include!("integration_tests/domain_upsert_tests.rs");

include!("integration_tests/record_get_tests.rs");

include!("integration_tests/prefix_and_namespace_get_tests.rs");

include!("integration_tests/section_get_tests.rs");

include!("integration_tests/list_projection_tests.rs");

include!("integration_tests/list_walk_tests.rs");

/// #1671: the batch re-embed sweep in `knowledge.index` pages the full atom
/// table with `created_at ASC, id ASC`. A recording embedder captures the
/// texts in the order the pages deliver them; with every atom forced onto
/// one shared `created_at` (so the `id` tiebreak is load-bearing), the
/// recorded sequence must equal every atom exactly once in `id ASC` order —
/// no duplicates, no misses, and the exact documented order.
#[tokio::test]
async fn index_reembed_paging_sweep_covers_equal_created_at_in_order() {
    use async_trait::async_trait;
    use khive_runtime::{AllowAllGate, BackendId, EmbedderProvider, RuntimeConfig};
    use khive_types::Namespace;
    use lattice_embed::{EmbedError, EmbeddingModel, EmbeddingService};

    const MODEL_KEY: &str = "all-minilm-l6-v2";
    const DIM: usize = 384;

    /// Records every text it is asked to embed, in call order, and returns a
    /// fixed vector per text so the vector write succeeds.
    struct RecordingEmbedService {
        recorded: Arc<Mutex<Vec<String>>>,
    }

    #[async_trait]
    impl EmbeddingService for RecordingEmbedService {
        async fn embed(
            &self,
            texts: &[String],
            _model: EmbeddingModel,
        ) -> std::result::Result<Vec<Vec<f32>>, EmbedError> {
            self.recorded
                .lock()
                .expect("recorded lock")
                .extend(texts.iter().cloned());
            Ok(texts.iter().map(|_| vec![0.5f32; DIM]).collect())
        }

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

        fn name(&self) -> &'static str {
            "recording-embed-service"
        }
    }

    struct RecordingEmbedProvider {
        recorded: Arc<Mutex<Vec<String>>>,
    }

    #[async_trait]
    impl EmbedderProvider for RecordingEmbedProvider {
        fn name(&self) -> &str {
            MODEL_KEY
        }

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

        async fn build(
            &self,
        ) -> std::result::Result<Arc<dyn EmbeddingService>, khive_runtime::RuntimeError> {
            Ok(Arc::new(RecordingEmbedService {
                recorded: Arc::clone(&self.recorded),
            }))
        }
    }

    let recorded = Arc::new(Mutex::new(Vec::<String>::new()));
    let rt = KhiveRuntime::new(RuntimeConfig {
        wal_ceiling_bytes: 0,
        wal_ceiling_configured_bytes: 0,
        wal_ceiling_source: Default::default(),
        wal_ceiling_env_raw: None,
        web: Default::default(),
        telemetry: Default::default(),
        mounts: Vec::new(),
        brain: Default::default(),
        git_write: Default::default(),
        display_timezone: khive_runtime::config::resolve_default_display_timezone(),
        events_split: None,
        db_path: None,
        blob_hydration_bytes: khive_runtime::DEFAULT_BLOB_HYDRATION_BYTES,
        default_namespace: Namespace::local(),
        embedding_model: Some(EmbeddingModel::AllMiniLmL6V2),
        additional_embedding_models: vec![],
        gate: Arc::new(AllowAllGate),
        packs: vec!["kg".to_string(), "knowledge".to_string()],
        backend_id: BackendId::main(),
        brain_profile: None,
        visible_namespaces: vec![],
        allowed_outbound_namespaces: vec![],
        actor_id: None,
        exec: Default::default(),
        ..khive_runtime::RuntimeConfig::no_embeddings()
    })
    .expect("runtime");
    rt.register_embedder(RecordingEmbedProvider {
        recorded: Arc::clone(&recorded),
    });
    let f = pack(rt.clone());

    let content = "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity";
    for n in 0..31 {
        f.dispatch(
            "knowledge.upsert_atoms",
            json!({ "atoms": [{ "slug": format!("reembed-{n:03}"), "name": format!("ReEmbed {n:03}"), "content": content }] }),
        )
        .await
        .expect("upsert atom");
    }

    // Force every atom onto one shared `created_at` so the paging sweep
    // exercises the tiebreak path; otherwise unique microsecond timestamps
    // would let the test pass even without the `id` tiebreak.
    let shared_created_at = 1_750_000_000_000_000_i64;
    {
        let sql = rt.sql();
        let mut writer = sql.writer().await.expect("sql writer must open");
        writer
            .execute(SqlStatement {
                sql: "UPDATE knowledge_atoms SET created_at = ?1".into(),
                params: vec![SqlValue::Integer(shared_created_at)],
                label: None,
            })
            .await
            .expect("force shared created_at");
    }
    // One shared `created_at` reduces the documented `created_at ASC,
    // id ASC` page order to `id ASC`; read (name, id) straight from the
    // table and sort by id in Rust rather than re-deriving with SQL. The
    // writer guard above is dropped before this reader opens.
    let expected_names: Vec<String> = {
        let sql = rt.sql();
        let mut reader = sql.reader().await.expect("sql reader must open");
        let rows = reader
            .query_all(SqlStatement {
                sql: "SELECT id, name FROM knowledge_atoms WHERE deleted_at IS NULL".into(),
                params: vec![],
                label: None,
            })
            .await
            .expect("read atom rows");
        let mut pairs: Vec<(String, String)> = rows
            .iter()
            .map(|row| {
                let id = match row.get("id") {
                    Some(SqlValue::Text(s)) => s.clone(),
                    other => panic!("unexpected id {other:?}"),
                };
                let name = match row.get("name") {
                    Some(SqlValue::Text(s)) => s.clone(),
                    other => panic!("unexpected name {other:?}"),
                };
                (id, name)
            })
            .collect();
        pairs.sort_unstable_by(|a, b| a.0.cmp(&b.0));
        pairs.into_iter().map(|(_, name)| name).collect()
    };
    assert_eq!(expected_names.len(), 31, "all atoms must be seeded");

    let result = f
        .dispatch("knowledge.index", json!({ "batch_size": 7 }))
        .await
        .expect("index ok");
    assert_eq!(
        result["indexed"].as_u64(),
        Some(31),
        "every atom must be indexed by the default engine: {result:?}"
    );

    // `atom_embed_text` puts the atom name first, so the first line of each
    // recorded text is the atom name; the concatenated pages must deliver
    // every atom exactly once in the documented page order.
    let recorded_names: Vec<String> = recorded
        .lock()
        .expect("recorded lock")
        .iter()
        .map(|text| {
            text.lines()
                .next()
                .expect("embed text carries the atom name")
                .to_string()
        })
        .collect();
    assert_eq!(
        recorded_names, expected_names,
        "re-embed paging sweep must embed every atom exactly once in \
         created_at ASC, id ASC order"
    );

    // The recording provider is pack-supplied, even though it replaces a
    // built-in model name. Its input preparation is not attested, so live
    // vectors must exist without a claimed exact-input fingerprint.
    let live_vectors = {
        let sql = rt.sql();
        let mut reader = sql.reader().await.expect("provenance reader");
        reader
            .query_all(SqlStatement {
                sql: "SELECT a.name, p.text_fingerprint \
                      FROM knowledge_atoms AS a \
                      JOIN vec_all_minilm_l6_v2 AS v \
                        ON v.subject_id = a.id AND v.namespace = a.namespace \
                       AND v.field = 'knowledge.atom' \
                      LEFT JOIN vector_provenance AS p \
                        ON p.model_key = 'all_minilm_l6_v2' \
                       AND p.subject_id = v.subject_id AND p.namespace = v.namespace \
                      WHERE a.deleted_at IS NULL"
                    .into(),
                params: vec![],
                label: None,
            })
            .await
            .expect("read live vectors and optional provenance")
    };
    let mut indexed_names = std::collections::HashSet::new();
    for row in live_vectors {
        let name = match row.get("name") {
            Some(SqlValue::Text(value)) => value.clone(),
            other => panic!("unexpected atom name {other:?}"),
        };
        assert!(
            indexed_names.insert(name.clone()),
            "each atom must have exactly one live vector: {name}"
        );
        assert!(
            matches!(row.get("text_fingerprint"), Some(SqlValue::Null)),
            "custom provider must not claim exact-input provenance for {name}"
        );
    }
    assert_eq!(indexed_names.len(), expected_names.len());
    assert!(
        expected_names
            .iter()
            .all(|name| indexed_names.contains(name)),
        "all recorded atoms must retain a live vector"
    );
}

// ── delete_atoms ──────────────────────────────────────────────────────────────

#[tokio::test]
async fn knowledge_index_persists_audited_bounded_prefixed_fingerprint() {
    use async_trait::async_trait;
    use khive_runtime::{AllowAllGate, BackendId, EmbedderProvider, RuntimeConfig};
    use khive_types::Namespace;
    use lattice_embed::{EmbedError, EmbeddingModel, EmbeddingService};

    const MODEL: EmbeddingModel = EmbeddingModel::MultilingualE5Small;
    const PREFIX: &str = "passage: ";

    struct RecordingService(Arc<Mutex<Vec<String>>>);
    #[async_trait]
    impl EmbeddingService for RecordingService {
        async fn embed(
            &self,
            texts: &[String],
            _model: EmbeddingModel,
        ) -> Result<Vec<Vec<f32>>, EmbedError> {
            self.0
                .lock()
                .expect("recorded input lock")
                .extend_from_slice(texts);
            Ok(texts
                .iter()
                .map(|_| vec![0.25; MODEL.dimensions()])
                .collect())
        }

        fn supports_model(&self, model: EmbeddingModel) -> bool {
            model == MODEL
        }

        fn name(&self) -> &'static str {
            "knowledge-audited-test-service"
        }
    }
    struct RecordingProvider(Arc<Mutex<Vec<String>>>);
    #[async_trait]
    impl EmbedderProvider for RecordingProvider {
        fn name(&self) -> &str {
            "multilingual-e5-small"
        }

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

        async fn build(&self) -> Result<Arc<dyn EmbeddingService>, RuntimeError> {
            Ok(Arc::new(RecordingService(Arc::clone(&self.0))))
        }
    }

    let recorded = Arc::new(Mutex::new(Vec::<String>::new()));
    let rt = KhiveRuntime::new(RuntimeConfig {
        wal_ceiling_bytes: 0,
        wal_ceiling_configured_bytes: 0,
        wal_ceiling_source: Default::default(),
        wal_ceiling_env_raw: None,
        web: Default::default(),
        telemetry: Default::default(),
        mounts: Vec::new(),
        brain: Default::default(),
        git_write: Default::default(),
        display_timezone: khive_runtime::config::resolve_default_display_timezone(),
        events_split: None,
        db_path: None,
        blob_hydration_bytes: khive_runtime::DEFAULT_BLOB_HYDRATION_BYTES,
        default_namespace: Namespace::local(),
        embedding_model: Some(MODEL),
        additional_embedding_models: vec![],
        gate: Arc::new(AllowAllGate),
        packs: vec!["kg".to_string(), "knowledge".to_string()],
        backend_id: BackendId::main(),
        brain_profile: None,
        visible_namespaces: vec![],
        allowed_outbound_namespaces: vec![],
        actor_id: None,
        exec: Default::default(),
        ..khive_runtime::RuntimeConfig::no_embeddings()
    })
    .expect("runtime");
    rt.register_test_audited_embedder(MODEL, RecordingProvider(Arc::clone(&recorded)));
    let f = pack(rt.clone());
    let name = "Audited E5 atom";
    let content = format!("{}海", "海 ".repeat(11_999));
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({"atoms": [{"slug": "audited-e5-atom", "name": name, "content": content.clone()}]}),
    )
    .await
    .expect("upsert atom");
    let indexed = f
        .dispatch("knowledge.index", json!({"ids": ["audited-e5-atom"]}))
        .await
        .expect("index atom");
    assert_eq!(indexed["indexed"].as_u64(), Some(1), "{indexed:?}");

    let source = format!("{name}\n\n{content}");
    let budget = lattice_embed::MAX_TEXT_BYTES - PREFIX.len();
    let end = source
        .char_indices()
        .map(|(index, _)| index)
        .take_while(|index| *index <= budget)
        .last()
        .expect("nonempty input");
    let prepared = format!("{PREFIX}{}", &source[..end]);
    assert_eq!(
        recorded.lock().unwrap().as_slice(),
        std::slice::from_ref(&prepared)
    );
    assert!(prepared.len() <= lattice_embed::MAX_TEXT_BYTES);
    assert!(
        source.len() > budget,
        "fixture must exercise UTF-8 truncation"
    );
    let expected = blake3::hash(prepared.as_bytes()).to_hex().to_string();

    async fn stored_fingerprint(rt: &KhiveRuntime) -> String {
        let sql = rt.sql();
        let mut reader = sql.reader().await.expect("provenance reader");
        let row = reader
            .query_row(SqlStatement {
                sql: "SELECT p.text_fingerprint FROM vector_provenance AS p \
                      JOIN vec_multilingual_e5_small AS v \
                        ON p.subject_id = v.subject_id AND p.namespace = v.namespace \
                      JOIN knowledge_atoms AS a \
                        ON a.id = v.subject_id AND a.namespace = v.namespace \
                      WHERE p.model_key = 'multilingual_e5_small' \
                        AND v.field = 'knowledge.atom' AND a.slug = 'audited-e5-atom'"
                    .into(),
                params: vec![],
                label: None,
            })
            .await
            .expect("read stored fingerprint")
            .expect("attributed atom vector");
        match row.get("text_fingerprint") {
            Some(SqlValue::Text(value)) => value.clone(),
            other => panic!("expected exact-input fingerprint, got {other:?}"),
        }
    }
    assert_eq!(stored_fingerprint(&rt).await, expected);

    f.dispatch("knowledge.index", json!({"ids": ["audited-e5-atom"]}))
        .await
        .expect("repeat same-source index");
    assert_eq!(stored_fingerprint(&rt).await, expected);
    assert_eq!(
        recorded.lock().unwrap().as_slice(),
        &[prepared.clone(), prepared.clone()]
    );

    let changed_content = content.replacen('海', "河", 1);
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({"atoms": [{"slug": "audited-e5-atom", "name": name, "content": changed_content}]}),
    )
    .await
    .expect("change bounded source byte");
    f.dispatch("knowledge.index", json!({"ids": ["audited-e5-atom"]}))
        .await
        .expect("index changed atom");
    let changed_prepared = prepared.replacen('海', "河", 1);
    assert_eq!(recorded.lock().unwrap()[2], changed_prepared);
    let changed = blake3::hash(changed_prepared.as_bytes())
        .to_hex()
        .to_string();
    assert_ne!(changed, expected);
    assert_eq!(stored_fingerprint(&rt).await, changed);
}

// ── delete_atoms ──────────────────────────────────────────────────────────────

// `knowledge.index` deliberately does not accept a FTS rebuild — rebuilding
// both global FTS indexes is a whole-database operation with no per-caller
// cost admission on the ordinary verb, and the namespace-scoped
// `reindex_knowledge` options do not offer it either. The only entry point is
// the operator call `khive_pack_knowledge::rebuild_knowledge_fts_indexes`
// (what `kkernel reindex --rebuild-fts` uses), which takes no namespace
// token, not the MCP verb dispatch this fixture drives for every other test
// in this file.
#[tokio::test]
async fn index_rebuild_fts_repairs_atom_and_section_external_content_drift() {
    let runtime = rt();
    let atom_id = "22730000-0000-4000-8000-000000000001";

    let mut writer = runtime.sql().writer().await.expect("knowledge writer");
    writer
        .execute_batch(vec![
            SqlStatement {
                sql: "INSERT INTO knowledge_atoms \
                      (id, namespace, slug, name, content, created_at, updated_at) \
                      VALUES (?1, 'local', 'fts-repair-atom', 'FTS Repair Atom', \
                              'repairable lexical atom document', 1, 1)"
                    .into(),
                params: vec![SqlValue::Text(atom_id.into())],
                label: Some("test.knowledge_fts_repair.atom".into()),
            },
            SqlStatement {
                sql: "INSERT INTO knowledge_sections \
                      (id, atom_id, namespace, section_type, heading, content, content_hash, \
                       created_at, updated_at) \
                      VALUES ('22730000-0000-4000-8000-000000000002', ?1, 'local', \
                              'overview', 'Repairable Overview', \
                              'repairable lexical section document', 'repair-hash', 1, 1)"
                    .into(),
                params: vec![SqlValue::Text(atom_id.into())],
                label: Some("test.knowledge_fts_repair.section".into()),
            },
            SqlStatement {
                sql: "INSERT INTO fts_knowledge \
                      (fts_knowledge, rowid, id, namespace, slug, name, content) \
                      SELECT 'delete', rowid, id, namespace, slug, name, content \
                      FROM knowledge_atoms WHERE id = ?1"
                    .into(),
                params: vec![SqlValue::Text(atom_id.into())],
                label: Some("test.knowledge_fts_repair.desync_atom".into()),
            },
            SqlStatement {
                sql: "INSERT INTO fts_sections \
                      (fts_sections, rowid, id, namespace, atom_id, section_type, heading, content) \
                      SELECT 'delete', rowid, id, namespace, atom_id, section_type, heading, content \
                      FROM knowledge_sections WHERE atom_id = ?1"
                    .into(),
                params: vec![SqlValue::Text(atom_id.into())],
                label: Some("test.knowledge_fts_repair.desync_section".into()),
            },
        ])
        .await
        .expect("seed and deliberately desynchronize both knowledge FTS indexes");
    drop(writer);

    let result = khive_pack_knowledge::rebuild_knowledge_fts_indexes(&runtime)
        .await
        .expect("knowledge FTS repair must succeed");
    assert_eq!(result["integrity_ok"], true, "repair result: {result}");
    let indexes: Vec<&str> = result["indexes"]
        .as_array()
        .expect("indexes array")
        .iter()
        .map(|v| v.as_str().expect("index name"))
        .collect();
    assert_eq!(
        indexes,
        vec!["fts_knowledge", "fts_sections"],
        "repair result: {result}"
    );
    assert!(
        result.get("atoms_indexed").is_none(),
        "the rebuild is FTS-only maintenance and reports no embedding work: {result}"
    );

    let mut reader = runtime.sql().reader().await.expect("knowledge reader");
    for (label, sql) in [
        (
            "atom",
            "SELECT count(*) AS n FROM fts_knowledge \
             WHERE fts_knowledge MATCH 'repairable'",
        ),
        (
            "section",
            "SELECT count(*) AS n FROM fts_sections \
             WHERE fts_sections MATCH 'repairable'",
        ),
    ] {
        let row = reader
            .query_row(SqlStatement {
                sql: sql.into(),
                params: vec![],
                label: Some(format!("test.knowledge_fts_repair.verify_{label}")),
            })
            .await
            .expect("query repaired FTS index")
            .expect("count row");
        assert!(
            matches!(row.get("n"), Some(SqlValue::Integer(1))),
            "{label} FTS document must be restored; row={row:?}"
        );
    }
}

include!("integration_tests/atom_delete_tests.rs");

include!("integration_tests/stats_and_fold_tests.rs");

include!("integration_tests/search_tests.rs");

include!("integration_tests/suggest_tests.rs");

include!("integration_tests/compose_atom_and_identity_tests.rs");

include!("integration_tests/compose_domain_and_prefix_tests.rs");

include!("integration_tests/compose_input_and_output_tests.rs");

include!("integration_tests/creation_and_secret_admission_tests.rs");

include!("integration_tests/deleted_slug_tests.rs");

include!("integration_tests/record_resolver_tests.rs");

// ── ADR-051 Amendment 1: KG entity blending in knowledge.compose ─────────────
//
// A fake `EmbedderProvider` whose vector is a function of a marker substring
// ("zzzquantumfoo") rather than real semantics: texts containing the marker
// embed to [1.0, 0.0], everything else to [0.0, 1.0]. This makes the
// KG-concept-outranks-lore-atom scenario (the ADR-051 Amendment 1 dogfood
// motivation) deterministic without a real embedding model — the query and
// the seeded concept share the marker, atoms/domains never do.
mod kg_blend {
    use super::*;
    use async_trait::async_trait;
    use khive_runtime::{AllowAllGate, BackendId, EmbedderProvider, RuntimeConfig};
    use khive_types::Namespace;
    use lattice_embed::{EmbedError, EmbeddingModel, EmbeddingService};
    use std::collections::HashSet;
    use std::sync::{Arc, Mutex};
    use tokio::sync::{oneshot, Notify};
    use tokio::time::{timeout, Duration};

    const MARKER: &str = "zzzquantumfoo";
    const MODEL_KEY: &str = "all-minilm-l6-v2";
    const DIM: usize = 384;
    const RACE_WAIT: Duration = Duration::from_secs(30);

    struct MarkerEmbedService {
        recorded: Option<Arc<Mutex<Vec<String>>>>,
        pause: Option<Arc<EmbedPause>>,
    }

    struct EmbedPause {
        entered: Mutex<Option<oneshot::Sender<()>>>,
        release: Notify,
    }

    #[async_trait]
    impl EmbeddingService for MarkerEmbedService {
        async fn embed(
            &self,
            texts: &[String],
            _model: EmbeddingModel,
        ) -> std::result::Result<Vec<Vec<f32>>, EmbedError> {
            if let Some(pause) = &self.pause {
                if texts
                    .iter()
                    .any(|text| text.contains("\nPaused heading\n\n"))
                {
                    let entered = pause.entered.lock().expect("pause lock").take();
                    if let Some(entered) = entered {
                        let _ = entered.send(());
                        pause.release.notified().await;
                    }
                }
            }
            if let Some(recorded) = &self.recorded {
                recorded
                    .lock()
                    .expect("recording lock")
                    .extend(texts.iter().cloned());
            }
            Ok(texts
                .iter()
                .map(|t| {
                    let mut v = vec![0.0f32; DIM];
                    if t.contains(MARKER) {
                        v[0] = 1.0;
                    } else {
                        v[1] = 1.0;
                    }
                    v
                })
                .collect())
        }

        async fn embed_query(
            &self,
            texts: &[String],
            model: EmbeddingModel,
        ) -> std::result::Result<Vec<Vec<f32>>, EmbedError> {
            self.embed(texts, model).await
        }

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

        fn name(&self) -> &'static str {
            "marker-embed-service"
        }
    }

    struct MarkerEmbedProvider {
        recorded: Option<Arc<Mutex<Vec<String>>>>,
        pause: Option<Arc<EmbedPause>>,
    }

    #[async_trait]
    impl EmbedderProvider for MarkerEmbedProvider {
        fn name(&self) -> &str {
            MODEL_KEY
        }

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

        async fn build(
            &self,
        ) -> std::result::Result<Arc<dyn EmbeddingService>, khive_runtime::RuntimeError> {
            Ok(Arc::new(MarkerEmbedService {
                recorded: self.recorded.as_ref().map(Arc::clone),
                pause: self.pause.as_ref().map(Arc::clone),
            }))
        }
    }

    fn rt_with_marker_embedder() -> KhiveRuntime {
        rt_with_marker_embedder_recording(None)
    }

    fn rt_with_marker_embedder_recording(
        recorded: Option<Arc<Mutex<Vec<String>>>>,
    ) -> KhiveRuntime {
        rt_with_marker_embedder_options(recorded, None)
    }

    fn rt_with_marker_embedder_options(
        recorded: Option<Arc<Mutex<Vec<String>>>>,
        pause: Option<Arc<EmbedPause>>,
    ) -> KhiveRuntime {
        let rt = KhiveRuntime::new(RuntimeConfig {
            wal_ceiling_bytes: 0,
            wal_ceiling_configured_bytes: 0,
            wal_ceiling_source: Default::default(),
            wal_ceiling_env_raw: None,
            web: Default::default(),
            telemetry: Default::default(),
            mounts: Vec::new(),
            brain: Default::default(),
            git_write: Default::default(),
            display_timezone: khive_runtime::config::resolve_default_display_timezone(),
            events_split: None,
            db_path: None,
            blob_hydration_bytes: khive_runtime::DEFAULT_BLOB_HYDRATION_BYTES,
            default_namespace: Namespace::local(),
            embedding_model: Some(EmbeddingModel::AllMiniLmL6V2),
            additional_embedding_models: vec![],
            gate: Arc::new(AllowAllGate),
            packs: vec!["kg".to_string(), "knowledge".to_string()],
            backend_id: BackendId::main(),
            brain_profile: None,
            visible_namespaces: vec![],
            allowed_outbound_namespaces: vec![],
            actor_id: None,
            exec: Default::default(),
            ..khive_runtime::RuntimeConfig::no_embeddings()
        })
        .expect("runtime");
        rt.register_embedder(MarkerEmbedProvider { recorded, pause });
        rt
    }

    const QUERY: &str = "zzzquantumfoo kv cache paging decode attention retrieval \
        augmented generation dense sparse benchmark corpus";

    const OVERLAP_CONTENT: &str = "kv cache paging decode attention retrieval augmented \
        generation dense sparse benchmark corpus latency gradient descent transformer vector \
        index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity";

    async fn section_embedding_head(rt: &KhiveRuntime, section_id: &str) -> [f32; 2] {
        let mut reader = rt.sql().reader().await.expect("section embedding reader");
        let row = reader
            .query_row(SqlStatement {
                sql: "SELECT embedding FROM knowledge_sections WHERE id = ?1".into(),
                params: vec![SqlValue::Text(section_id.to_string())],
                label: None,
            })
            .await
            .expect("section embedding query")
            .expect("section row");
        let Some(SqlValue::Blob(bytes)) = row.get("embedding") else {
            panic!("section must have an inline embedding: {row:?}");
        };
        assert!(bytes.len() >= 8, "section embedding has two dimensions");
        [
            f32::from_le_bytes(bytes[0..4].try_into().expect("first dimension")),
            f32::from_le_bytes(bytes[4..8].try_into().expect("second dimension")),
        ]
    }

    async fn section_embedding_is_null(rt: &KhiveRuntime, section_id: &str) -> bool {
        let mut reader = rt.sql().reader().await.expect("section embedding reader");
        let row = reader
            .query_row(SqlStatement {
                sql: "SELECT embedding FROM knowledge_sections WHERE id = ?1".into(),
                params: vec![SqlValue::Text(section_id.to_string())],
                label: None,
            })
            .await
            .expect("section embedding query")
            .expect("section row");
        matches!(row.get("embedding"), Some(SqlValue::Null))
    }

    #[tokio::test]
    async fn edit_same_body_changed_heading_refreshes_section_embedding() {
        let rt = rt_with_marker_embedder();
        let f = pack(rt.clone());
        f.dispatch(
            "knowledge.upsert_atoms",
            json!({ "atoms": [{
                "slug": "heading-vector-refresh",
                "name": "Heading Vector Refresh",
                "content": OVERLAP_CONTENT
            }] }),
        )
        .await
        .expect("upsert atom");

        let body = "The section body stays byte identical across edits so its content hash and section identity remain unchanged while the heading changes the embedding input.";
        let first = f
            .dispatch(
                "knowledge.edit",
                json!({ "id": "heading-vector-refresh", "sections": [{
                    "section_type": "overview", "heading": "Plain heading", "content": body
                }] }),
            )
            .await
            .expect("create section with plain heading");
        let section_id = first["sections"][0]["id"]
            .as_str()
            .expect("section id")
            .to_string();
        assert_eq!(section_embedding_head(&rt, &section_id).await, [0.0, 1.0]);

        let second = f
            .dispatch(
                "knowledge.edit",
                json!({ "id": "heading-vector-refresh", "sections": [{
                    "section_type": "overview", "heading": format!("{MARKER} heading"), "content": body
                }] }),
            )
            .await
            .expect("change heading without changing section body");
        assert_eq!(second["sections"][0]["id"], section_id);
        assert_eq!(section_embedding_head(&rt, &section_id).await, [1.0, 0.0]);
    }

    #[tokio::test]
    async fn atom_rename_clears_section_embedding_until_the_next_edit() {
        let recorded = Arc::new(Mutex::new(Vec::new()));
        let rt = rt_with_marker_embedder_recording(Some(Arc::clone(&recorded)));
        let f = pack(rt.clone());
        let atom = json!({
            "slug": "renamed-section-vector",
            "name": "Plain atom name",
            "content": OVERLAP_CONTENT,
        });
        f.dispatch("knowledge.upsert_atoms", json!({"atoms": [atom]}))
            .await
            .expect("create atom");
        let section = f
            .dispatch(
                "knowledge.edit",
                json!({"id": "renamed-section-vector", "sections": [{
                    "section_type": "overview",
                    "heading": "Plain heading",
                    "content": "This section keeps its heading and body while its atom is renamed, so only the atom breadcrumb should change in the embedding input."
                }]}),
            )
            .await
            .expect("create section");
        let section_id = section["sections"][0]["id"].as_str().expect("section id");
        assert_eq!(section_embedding_head(&rt, section_id).await, [0.0, 1.0]);

        let before = recorded.lock().expect("recording lock").len();
        f.dispatch(
            "knowledge.upsert_atoms",
            json!({"atoms": [{
                "slug": "renamed-section-vector",
                "name": "Plain atom name",
                "content": OVERLAP_CONTENT,
            }]}),
        )
        .await
        .expect("same-name upsert");
        assert_eq!(recorded.lock().expect("recording lock").len(), before);
        assert_eq!(section_embedding_head(&rt, section_id).await, [0.0, 1.0]);

        f.dispatch(
            "knowledge.upsert_atoms",
            json!({"atoms": [{
                "slug": "renamed-section-vector",
                "name": format!("{MARKER} atom name"),
                "content": OVERLAP_CONTENT,
            }]}),
        )
        .await
        .expect("rename atom");
        assert_eq!(recorded.lock().expect("recording lock").len(), before);
        assert!(section_embedding_is_null(&rt, section_id).await);

        f.dispatch(
            "knowledge.edit",
            json!({"id": "renamed-section-vector", "sections": [{
                "section_type": "overview",
                "heading": "Plain heading",
                "content": "This section keeps its heading and body while its atom is renamed, so only the atom breadcrumb should change in the embedding input."
            }]}),
        )
        .await
        .expect("re-embed the renamed section");
        assert_eq!(section_embedding_head(&rt, section_id).await, [1.0, 0.0]);
    }

    #[tokio::test]
    async fn domain_mirror_rename_clears_section_embedding_until_the_next_edit() {
        let recorded = Arc::new(Mutex::new(Vec::new()));
        let rt = rt_with_marker_embedder_recording(Some(Arc::clone(&recorded)));
        let f = pack(rt.clone());
        let body = "A domain mirror can own a section, and its name is part of that section's embedding input even when its heading and content remain unchanged.";
        f.dispatch(
            "knowledge.upsert_domains",
            json!({"domains": [{
                "slug": "renamed-domain-mirror",
                "name": "Plain domain name",
                "description": OVERLAP_CONTENT
            }]}),
        )
        .await
        .expect("create domain");
        let section = f
            .dispatch(
                "knowledge.edit",
                json!({"id": "renamed-domain-mirror", "sections": [{
                    "section_type": "overview",
                    "heading": "Plain heading",
                    "content": body
                }]}),
            )
            .await
            .expect("create section on domain mirror");
        let section_id = section["sections"][0]["id"].as_str().expect("section id");
        assert_eq!(section_embedding_head(&rt, section_id).await, [0.0, 1.0]);

        let before = recorded.lock().expect("recording lock").len();
        f.dispatch(
            "knowledge.upsert_domains",
            json!({"domains": [{
                "slug": "renamed-domain-mirror",
                "name": format!("{MARKER} domain name"),
                "description": OVERLAP_CONTENT
            }]}),
        )
        .await
        .expect("rename domain");
        assert_eq!(recorded.lock().expect("recording lock").len(), before);
        assert!(section_embedding_is_null(&rt, section_id).await);

        f.dispatch(
            "knowledge.edit",
            json!({"id": "renamed-domain-mirror", "sections": [{
                "section_type": "overview",
                "heading": "Plain heading",
                "content": body
            }]}),
        )
        .await
        .expect("refresh domain mirror section");
        assert_eq!(section_embedding_head(&rt, section_id).await, [1.0, 0.0]);
    }

    #[tokio::test]
    async fn late_section_embedding_cannot_overwrite_newer_heading_vector() {
        let (entered, waiting) = oneshot::channel();
        let pause = Arc::new(EmbedPause {
            entered: Mutex::new(Some(entered)),
            release: Notify::new(),
        });
        let rt = rt_with_marker_embedder_options(None, Some(Arc::clone(&pause)));
        let f = Arc::new(pack(rt.clone()));
        f.dispatch(
            "knowledge.upsert_atoms",
            json!({"atoms": [{
                "slug": "late-section-embed",
                "name": "Plain atom name",
                "content": OVERLAP_CONTENT,
            }]}),
        )
        .await
        .expect("create atom");
        let body = "This body stays the same across both concurrent edits so both writes address the same section, while the headings produce different deterministic vectors.";
        let original = f
            .dispatch(
                "knowledge.edit",
                json!({"id": "late-section-embed", "sections": [{
                    "section_type": "overview", "heading": "Original heading", "content": body,
                }]}),
            )
            .await
            .expect("create section");
        let section_id = original["sections"][0]["id"]
            .as_str()
            .expect("section id")
            .to_string();
        assert_eq!(section_embedding_head(&rt, &section_id).await, [0.0, 1.0]);

        let first = Arc::clone(&f);
        let earlier = tokio::spawn(async move {
            first
                .dispatch(
                    "knowledge.edit",
                    json!({"id": "late-section-embed", "sections": [{
                        "section_type": "overview", "heading": "Paused heading", "content": body,
                    }]}),
                )
                .await
        });
        timeout(RACE_WAIT, waiting)
            .await
            .expect("first embedding did not pause")
            .expect("first embedding did not signal");

        let newer = timeout(
            RACE_WAIT,
            f.dispatch("knowledge.edit", json!({"id": "late-section-embed", "sections": [{
                "section_type": "overview", "heading": format!("{MARKER} new heading"), "content": body,
            }]})),
        )
            .await
            .expect("newer edit blocked behind paused embed")
            .expect("newer edit completes before old embedding");
        assert_eq!(newer["sections"][0]["id"], section_id);
        assert_eq!(section_embedding_head(&rt, &section_id).await, [1.0, 0.0]);

        pause.release.notify_one();
        earlier
            .await
            .expect("earlier edit task")
            .expect("earlier edit");
        assert_eq!(section_embedding_head(&rt, &section_id).await, [1.0, 0.0]);
    }

    #[tokio::test]
    async fn late_reindex_after_atom_rename_is_superseded_not_failed() {
        let pause = Arc::new(EmbedPause {
            entered: Mutex::new(None),
            release: Notify::new(),
        });
        let rt = rt_with_marker_embedder_options(None, Some(Arc::clone(&pause)));
        let f = pack(rt.clone());
        f.dispatch(
            "knowledge.upsert_atoms",
            json!({"atoms": [{
                "slug": "rename-during-reindex",
                "name": "Plain atom name",
                "content": OVERLAP_CONTENT,
            }]}),
        )
        .await
        .expect("create atom");
        let section = f
            .dispatch("knowledge.edit", json!({"id": "rename-during-reindex", "sections": [{
                "section_type": "overview",
                "heading": "Paused heading",
                "content": "This section remains byte identical through the rename and reindex race, so its vector must encode the current atom name after the late reindex finishes."
            }]}))
            .await
            .expect("create section");
        let section_id = section["sections"][0]["id"].as_str().expect("section id");
        assert_eq!(section_embedding_head(&rt, section_id).await, [0.0, 1.0]);

        let (entered, waiting) = oneshot::channel();
        *pause.entered.lock().expect("pause lock") = Some(entered);
        let reindex_runtime = rt.clone();
        let reindex = tokio::spawn(async move {
            let token = reindex_runtime
                .authorize(Namespace::local())
                .expect("authorize reindex");
            khive_pack_knowledge::reindex_knowledge(
                &reindex_runtime,
                &token,
                khive_pack_knowledge::KnowledgeReindexOptions {
                    atoms: false,
                    sections: true,
                    drop_existing: true,
                    rebuild_ann: false,
                    batch_size: None,
                },
                None,
                None,
            )
            .await
        });
        timeout(RACE_WAIT, waiting)
            .await
            .expect("reindex embedding did not pause")
            .expect("reindex embedding did not signal");

        timeout(
            RACE_WAIT,
            f.dispatch(
                "knowledge.upsert_atoms",
                json!({"atoms": [{
                    "slug": "rename-during-reindex",
                    "name": format!("{MARKER} atom name"),
                    "content": OVERLAP_CONTENT,
                }]}),
            ),
        )
        .await
        .expect("rename blocked behind paused embed")
        .expect("rename atom");
        assert!(section_embedding_is_null(&rt, section_id).await);

        pause.release.notify_one();
        let result = reindex
            .await
            .expect("reindex task")
            .expect("reindex result");
        assert_eq!(result["sections_superseded"], 1);
        assert_eq!(result["sections_indexed"], 0);
        assert_eq!(result["sections_failed"], 0);
        assert!(section_embedding_is_null(&rt, section_id).await);

        f.dispatch(
            "knowledge.edit",
            json!({"id": "rename-during-reindex", "sections": [{
                "section_type": "overview",
                "heading": "Paused heading",
                "content": "This section remains byte identical through the rename and reindex race, so its vector must encode the current atom name after the late reindex finishes."
            }]}),
        )
        .await
        .expect("refresh renamed section");
        assert_eq!(section_embedding_head(&rt, section_id).await, [1.0, 0.0]);
    }

    async fn seed_domain_and_atom(f: &Fixture) -> String {
        f.dispatch(
            "knowledge.upsert_atoms",
            json!({
                "atoms": [
                    { "slug": "kg-blend-atom", "name": "KG Blend Atom", "content": OVERLAP_CONTENT, "finalized": true }
                ]
            }),
        )
        .await
        .expect("upsert atom");

        f.dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [
                    {
                        "slug": "kg-blend-domain",
                        "name": "KG Blend Domain",
                        "description": OVERLAP_CONTENT,
                        "members": ["kg-blend-atom"]
                    }
                ]
            }),
        )
        .await
        .expect("upsert domain");

        let domain_resp = f
            .dispatch("knowledge.get", json!({ "id": "kg-blend-domain" }))
            .await
            .expect("get domain");
        domain_resp["id"].as_str().expect("domain id").to_string()
    }

    async fn seed_kg_concept(f: &Fixture) -> String {
        let resp = f
            .dispatch(
                "create",
                json!({
                    "kind": "concept",
                    "name": "ZipCache",
                    "description": format!(
                        "{MARKER} paged KV cache quantization technique for decode-time attention"
                    ),
                }),
            )
            .await
            .expect("create kg concept");
        resp["id"].as_str().expect("entity id").to_string()
    }

    /// Test 1: AUTO compose with a query whose top KG concept is seeded blends
    /// the entity into `data.entities` and the "Knowledge graph" markdown
    /// section, while atoms remain present.
    #[tokio::test]
    async fn auto_compose_blends_seeded_kg_concept() {
        let f = pack(rt_with_marker_embedder());
        seed_domain_and_atom(&f).await;
        let concept_id = seed_kg_concept(&f).await;

        let resp = f
            .dispatch("knowledge.compose", json!({ "query": QUERY }))
            .await
            .expect("auto compose ok");

        let data = &resp["data"];
        let atoms = data["atoms"].as_array().expect("atoms array");
        assert!(!atoms.is_empty(), "atoms must still be present");

        let entities = data["entities"]
            .as_array()
            .expect("entities array must be present when a KG concept blends");
        assert!(
            entities.iter().any(|e| e["id"] == concept_id),
            "expected seeded concept {concept_id} in entities, got: {entities:?}"
        );

        let md = data["markdown"].as_str().expect("markdown");
        assert!(
            md.contains("Knowledge graph"),
            "markdown must contain the Knowledge graph section, got: {md}"
        );
        assert!(
            md.contains("ZipCache"),
            "markdown must render the blended concept's name, got: {md}"
        );
    }

    /// #2232: auto-compose crosses suggest ANN/rerank, atom rerank, and the
    /// KG blend search. Every stage must reuse the first successful query
    /// vector instead of independently embedding the same request text.
    #[tokio::test]
    async fn auto_compose_embeds_request_query_exactly_once() {
        let recorded = Arc::new(Mutex::new(Vec::<String>::new()));
        let f = pack(rt_with_marker_embedder_recording(Some(Arc::clone(
            &recorded,
        ))));
        seed_domain_and_atom(&f).await;
        seed_kg_concept(&f).await;
        recorded.lock().expect("recording lock").clear();

        let response = f
            .dispatch("knowledge.compose", json!({ "query": QUERY }))
            .await
            .expect("auto compose ok");
        assert!(
            response["data"]["count"].as_u64().unwrap_or_default() > 0,
            "fixture must exercise a non-empty compose path: {response:?}"
        );

        let recorded = recorded.lock().expect("recording lock");
        assert_eq!(
            recorded
                .iter()
                .filter(|text| text.as_str() == QUERY)
                .count(),
            1,
            "one request must embed its query exactly once: {recorded:?}"
        );
    }

    /// #1505 direct-call exactness: even when the authorized token's primary
    /// namespace matches the explicit arm, a broader visible set must be
    /// narrowed before KG blending. Otherwise a local-only entity leaks into
    /// an arm briefing while corpus rows remain correctly arm-scoped.
    #[tokio::test]
    async fn direct_compose_narrows_matching_broad_token_before_kg_blend() {
        use khive_runtime::PackRuntime;

        let rt = rt_with_marker_embedder();
        let mut builder = VerbRegistryBuilder::new();
        builder.register(KgPack::new(rt.clone()));
        let registry = builder.build().expect("kg registry");
        let local_entity = registry
            .dispatch(
                "create",
                json!({
                    "kind": "concept",
                    "name": "LocalOnlyZipCache",
                    "description": format!(
                        "{MARKER} local-only paged KV cache quantization technique"
                    ),
                }),
            )
            .await
            .expect("create local-only KG entity")["id"]
            .as_str()
            .expect("entity id")
            .to_string();

        let arm_ns = Namespace::parse("bench-arm-a").expect("arm namespace");
        let arm_entity = registry
            .dispatch(
                "create",
                json!({
                    "namespace": arm_ns.as_str(),
                    "kind": "concept",
                    "name": "ArmOnlyZipCache",
                    "description": format!(
                        "{MARKER} arm-only paged KV cache quantization technique"
                    ),
                }),
            )
            .await
            .expect("create arm-only KG entity")["id"]
            .as_str()
            .expect("arm entity id")
            .to_string();
        let broad_arm_token = rt
            .authorize_with_visibility(arm_ns.clone(), vec![Namespace::local()])
            .expect("arm token with local visibility");
        let knowledge = KnowledgePack::new(rt.clone());
        knowledge
            .dispatch(
                "knowledge.upsert_atoms",
                json!({
                    "atoms": [{
                        "slug": "arm-kg-blend-atom",
                        "name": "Arm KG Blend Atom",
                        "content": format!("{MARKER} {OVERLAP_CONTENT}"),
                        "finalized": true,
                    }]
                }),
                &registry,
                &broad_arm_token,
            )
            .await
            .expect("upsert arm atom");
        knowledge
            .dispatch(
                "knowledge.upsert_domains",
                json!({
                    "domains": [{
                        "slug": "arm-kg-blend-domain",
                        "name": "Arm KG Blend Domain",
                        "description": OVERLAP_CONTENT,
                        "members": ["arm-kg-blend-atom"],
                    }]
                }),
                &registry,
                &broad_arm_token,
            )
            .await
            .expect("upsert arm domain");

        let resp = knowledge
            .dispatch(
                "knowledge.compose",
                json!({
                    "namespace": arm_ns.as_str(),
                    "domain_ids": ["arm-kg-blend-domain"],
                    "query": QUERY,
                }),
                &registry,
                &broad_arm_token,
            )
            .await
            .expect("direct exact-arm compose");
        let entities = resp["data"]["entities"]
            .as_array()
            .cloned()
            .unwrap_or_default();
        assert!(
            entities.iter().any(|entity| entity["id"] == arm_entity),
            "the arm KG candidate must prove the blend leg ran: {entities:?}"
        );
        assert!(
            entities.iter().all(|entity| entity["id"] != local_entity),
            "local-only KG entity leaked through a matching but broad direct-call token: {entities:?}"
        );
    }

    /// Test 2: `blend_kg=false` behaves exactly as compose did before this
    /// feature — no `entities` field, no "Knowledge graph" markdown section —
    /// even when a matching KG concept exists.
    #[tokio::test]
    async fn blend_kg_false_omits_entities() {
        let f = pack(rt_with_marker_embedder());
        seed_domain_and_atom(&f).await;
        seed_kg_concept(&f).await;

        let resp = f
            .dispatch(
                "knowledge.compose",
                json!({ "query": QUERY, "blend_kg": false }),
            )
            .await
            .expect("compose with blend_kg=false ok");

        let data = &resp["data"];
        assert!(
            data.get("entities").is_none(),
            "entities must be absent when blend_kg=false, got: {data}"
        );
        let md = data["markdown"].as_str().expect("markdown");
        assert!(
            !md.contains("Knowledge graph"),
            "markdown must not contain a Knowledge graph section when blend_kg=false, got: {md}"
        );
        let atoms = data["atoms"].as_array().expect("atoms array");
        assert!(!atoms.is_empty(), "atoms must still be present");
    }

    /// #2307 fix 2: an explicit (`domain_ids`) compose call has no ANN stage
    /// to prime the query embedding cache, so it only pays for a dedicated
    /// role-specific `embed_query` round trip when something downstream
    /// actually requires that role — the KG blend. With `blend_kg: false`
    /// nothing in the request needs a role-specific vector, so the rerank
    /// stage folds the query into its single candidate batch: exactly one
    /// `embed()` round trip for the whole request, matching the pre-#2232
    /// combined-batch cost. Before this fix the fallback embed_query call
    /// ran unconditionally regardless of `blend_kg`, always costing a
    /// second round trip even when its result had no consumer.
    #[tokio::test]
    async fn explicit_compose_without_kg_blend_embeds_in_single_round_trip() {
        use async_trait::async_trait;
        use khive_runtime::{AllowAllGate, BackendId, EmbedderProvider, RuntimeConfig};
        use lattice_embed::{EmbedError, EmbeddingModel, EmbeddingService};

        const DIM: usize = 384;
        const MODEL_KEY: &str = "all-minilm-l6-v2";

        struct CountingEmbedService {
            calls: Arc<Mutex<usize>>,
        }

        #[async_trait]
        impl EmbeddingService for CountingEmbedService {
            async fn embed(
                &self,
                texts: &[String],
                _model: EmbeddingModel,
            ) -> std::result::Result<Vec<Vec<f32>>, EmbedError> {
                *self.calls.lock().expect("calls lock") += 1;
                Ok(texts.iter().map(|_| vec![0.1f32; DIM]).collect())
            }

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

            fn name(&self) -> &'static str {
                "round-trip-counting-embed-service"
            }
        }

        struct CountingEmbedProvider {
            calls: Arc<Mutex<usize>>,
        }

        #[async_trait]
        impl EmbedderProvider for CountingEmbedProvider {
            fn name(&self) -> &str {
                MODEL_KEY
            }

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

            async fn build(
                &self,
            ) -> std::result::Result<Arc<dyn EmbeddingService>, khive_runtime::RuntimeError>
            {
                Ok(Arc::new(CountingEmbedService {
                    calls: Arc::clone(&self.calls),
                }))
            }
        }

        let calls = Arc::new(Mutex::new(0usize));
        let rt = KhiveRuntime::new(RuntimeConfig {
            wal_ceiling_bytes: 0,
            wal_ceiling_configured_bytes: 0,
            wal_ceiling_source: Default::default(),
            wal_ceiling_env_raw: None,
            web: Default::default(),
            telemetry: Default::default(),
            mounts: Vec::new(),
            brain: Default::default(),
            git_write: Default::default(),
            display_timezone: khive_runtime::config::resolve_default_display_timezone(),
            events_split: None,
            db_path: None,
            blob_hydration_bytes: khive_runtime::DEFAULT_BLOB_HYDRATION_BYTES,
            default_namespace: Namespace::local(),
            embedding_model: Some(EmbeddingModel::AllMiniLmL6V2),
            additional_embedding_models: vec![],
            gate: Arc::new(AllowAllGate),
            packs: vec!["kg".to_string(), "knowledge".to_string()],
            backend_id: BackendId::main(),
            brain_profile: None,
            visible_namespaces: vec![],
            allowed_outbound_namespaces: vec![],
            actor_id: None,
            exec: Default::default(),
            ..khive_runtime::RuntimeConfig::no_embeddings()
        })
        .expect("runtime");
        rt.register_embedder(CountingEmbedProvider {
            calls: Arc::clone(&calls),
        });
        let f = pack(rt);
        let domain_id = seed_domain_and_atom(&f).await;
        *calls.lock().expect("calls lock") = 0;

        let resp = f
            .dispatch(
                "knowledge.compose",
                json!({ "domain_ids": [domain_id], "query": QUERY, "blend_kg": false }),
            )
            .await
            .expect("explicit compose with blend_kg=false ok");
        assert!(
            resp["data"]["count"].as_u64().unwrap_or_default() > 0,
            "fixture must exercise a non-empty compose path: {resp:?}"
        );

        assert_eq!(
            *calls.lock().expect("calls lock"),
            1,
            "blend_kg=false must embed the query and its candidates in one combined \
             round trip, not a dedicated role-specific call plus a separate batch"
        );
    }

    /// Test 3: `atom_ids`-only calls never blend, regardless of `blend_kg`
    /// (which defaults to true) — the caller pinned exact atoms.
    #[tokio::test]
    async fn atom_ids_only_never_blends() {
        let f = pack(rt_with_marker_embedder());
        f.dispatch(
            "knowledge.upsert_atoms",
            json!({
                "atoms": [
                    { "slug": "kg-blend-atom", "name": "KG Blend Atom", "content": OVERLAP_CONTENT }
                ]
            }),
        )
        .await
        .expect("upsert atom");
        seed_kg_concept(&f).await;

        let resp = f
            .dispatch(
                "knowledge.compose",
                json!({ "atom_ids": ["kg-blend-atom"], "query": QUERY }),
            )
            .await
            .expect("atom_ids-only compose ok");

        let data = &resp["data"];
        assert!(
            data.get("entities").is_none(),
            "atom_ids-only compose must never blend KG entities, got: {data}"
        );
        let md = data["markdown"].as_str().expect("markdown");
        assert!(
            !md.contains("Knowledge graph"),
            "atom_ids-only markdown must not contain a Knowledge graph section, got: {md}"
        );
    }

    /// Test 4: blended entities respect `max_tokens` trimming — a budget too
    /// tight to fit any entity after the atom/section body omits the
    /// "Knowledge graph" section entirely, while the atom body itself
    /// survives untouched.
    #[tokio::test]
    async fn blended_entities_respect_max_tokens_budget() {
        let f = pack(rt_with_marker_embedder());

        // A padded atom whose fully rendered briefing consumes nearly all
        // of the 2000-byte floor budget, leaving too little for a KG heading
        // and entity line. The atom must survive either way.
        let filler = "x".repeat(1510);
        let big_content = format!("{OVERLAP_CONTENT} {filler}");
        f.dispatch(
            "knowledge.upsert_atoms",
            json!({
                "atoms": [
                    { "slug": "kg-blend-atom", "name": "KG Blend Atom", "content": big_content, "finalized": true }
                ]
            }),
        )
        .await
        .expect("upsert atom");
        f.dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [
                    {
                        "slug": "kg-blend-domain",
                        "name": "KG Blend Domain",
                        "description": OVERLAP_CONTENT,
                        "members": ["kg-blend-atom"]
                    }
                ]
            }),
        )
        .await
        .expect("upsert domain");
        seed_kg_concept(&f).await;

        // Generous budget: entity fits.
        let generous = f
            .dispatch(
                "knowledge.compose",
                json!({ "query": QUERY, "max_tokens": 8000 }),
            )
            .await
            .expect("generous-budget compose ok");
        let generous_atoms = generous["data"]["atoms"].as_array().expect("atoms array");
        assert!(!generous_atoms.is_empty(), "atoms must be present");
        assert!(
            generous["data"].get("entities").is_some(),
            "entity must fit under a generous budget, got: {}",
            generous["data"]
        );

        // Minimum-clamp budget: the atom body alone consumes it, leaving no
        // room for the entity section — atoms must still survive.
        let tight = f
            .dispatch(
                "knowledge.compose",
                json!({ "query": QUERY, "max_tokens": 500 }),
            )
            .await
            .expect("tight-budget compose ok");
        let tight_atoms = tight["data"]["atoms"].as_array().expect("atoms array");
        assert!(!tight_atoms.is_empty(), "atoms must survive a tight budget");
        assert!(
            tight["data"]["markdown"].as_str().expect("markdown").len() <= 2_000,
            "the whole rendered briefing, including the KG tail, must fit"
        );
        assert!(
            tight["data"].get("entities").is_none(),
            "entity must be trimmed out under a tight budget, got: {}",
            tight["data"]
        );
    }

    async fn seed_kg_document(f: &Fixture) -> String {
        let resp = f
            .dispatch(
                "create",
                json!({
                    "kind": "document",
                    "name": "ADR-051",
                    "description": format!(
                        "{MARKER} section-embeddings hybrid compose architecture decision record"
                    ),
                }),
            )
            .await
            .expect("create kg document");
        resp["id"].as_str().expect("entity id").to_string()
    }

    /// Test 5: explicit `domain_ids` (non-AUTO) calls blend exactly like AUTO
    /// calls do — the Amendment 1 decision covers both, not just AUTO.
    #[tokio::test]
    async fn explicit_domain_ids_compose_blends_seeded_kg_concept() {
        let f = pack(rt_with_marker_embedder());
        let domain_id = seed_domain_and_atom(&f).await;
        let concept_id = seed_kg_concept(&f).await;

        let resp = f
            .dispatch(
                "knowledge.compose",
                json!({ "domain_ids": [domain_id], "query": QUERY }),
            )
            .await
            .expect("explicit domain_ids compose ok");

        let data = &resp["data"];
        let atoms = data["atoms"].as_array().expect("atoms array");
        assert!(!atoms.is_empty(), "atoms must still be present");
        let entities = data["entities"]
            .as_array()
            .expect("entities array must be present for explicit domain_ids blend");
        assert!(
            entities.iter().any(|e| e["id"] == concept_id),
            "expected seeded concept {concept_id} in entities, got: {entities:?}"
        );
    }

    /// Test 6: a `document`-kind KG entity (not just `concept`) blends.
    #[tokio::test]
    async fn auto_compose_blends_seeded_kg_document() {
        let f = pack(rt_with_marker_embedder());
        seed_domain_and_atom(&f).await;
        let document_id = seed_kg_document(&f).await;

        let resp = f
            .dispatch("knowledge.compose", json!({ "query": QUERY }))
            .await
            .expect("auto compose ok");

        let data = &resp["data"];
        let entities = data["entities"]
            .as_array()
            .expect("entities array must be present when a KG document blends");
        assert!(
            entities
                .iter()
                .any(|e| e["id"] == document_id && e["kind"] == "document"),
            "expected seeded document {document_id} in entities, got: {entities:?}"
        );
    }

    /// Test 7: the blended candidate pool is capped at `KG_BLEND_CAP` (5) and
    /// deduplicated — seeding more matching concept/document entities than
    /// the cap never yields duplicate or over-cap results.
    #[tokio::test]
    async fn blend_candidates_are_capped_and_deduped_across_kinds() {
        let f = pack(rt_with_marker_embedder());
        seed_domain_and_atom(&f).await;

        for i in 0..4 {
            f.dispatch(
                "create",
                json!({
                    "kind": "concept",
                    "name": format!("Concept{i}"),
                    "description": format!("{MARKER} paged KV cache technique variant {i}"),
                }),
            )
            .await
            .expect("create kg concept");
        }
        for i in 0..4 {
            f.dispatch(
                "create",
                json!({
                    "kind": "document",
                    "name": format!("Document{i}"),
                    "description": format!("{MARKER} decode-time attention paper variant {i}"),
                }),
            )
            .await
            .expect("create kg document");
        }

        let resp = f
            .dispatch("knowledge.compose", json!({ "query": QUERY }))
            .await
            .expect("auto compose ok");

        let entities = resp["data"]["entities"]
            .as_array()
            .expect("entities array must be present — 8 matching entities were seeded");
        assert!(
            entities.len() <= 5,
            "blended entities must be capped at KG_BLEND_CAP=5, got {}: {entities:?}",
            entities.len()
        );
        let ids: HashSet<&str> = entities.iter().filter_map(|e| e["id"].as_str()).collect();
        assert_eq!(
            ids.len(),
            entities.len(),
            "blended entities must be deduplicated by id, got: {entities:?}"
        );
    }

    /// Runs an explicit-domain compose with a dispatch-accounting context armed and
    /// returns the response with the counters that dispatch recorded. Explicit domains
    /// skip the suggest phase, so the KG blend is the only vector search in the call.
    async fn compose_counting_usage(f: &Fixture, domain_id: &str) -> (Value, Value) {
        let usage = khive_runtime::usage::UsageContext::new();
        let resp = khive_runtime::usage::scope(
            usage.clone(),
            f.dispatch(
                "knowledge.compose",
                json!({ "domain_ids": [domain_id], "query": QUERY }),
            ),
        )
        .await
        .expect("explicit domain_ids compose ok");
        (resp, usage.snapshot())
    }

    /// The blend finds `concept` and `document` entities with one vector query shared
    /// by both kinds, so the vector store is queried once for the call rather than once
    /// per kind, and both kinds still come back.
    #[tokio::test]
    async fn kg_blend_searches_both_kinds_in_one_vector_pass() {
        let f = pack(rt_with_marker_embedder());
        let domain_id = seed_domain_and_atom(&f).await;
        let concept_id = seed_kg_concept(&f).await;
        let document_id = seed_kg_document(&f).await;

        let (resp, usage) = compose_counting_usage(&f, &domain_id).await;

        assert_eq!(
            usage["vector_passes"], 1,
            "one blend search over both kinds must run one vector pass, got: {usage}"
        );
        let entities = resp["data"]["entities"]
            .as_array()
            .expect("entities array must be present when both kinds blend");
        let ids: HashSet<&str> = entities.iter().filter_map(|e| e["id"].as_str()).collect();
        assert_eq!(
            ids,
            HashSet::from([concept_id.as_str(), document_id.as_str()]),
            "both seeded kinds must be blended, got: {entities:?}"
        );
    }

    /// With only one blend kind present, the shared vector query returns that kind's
    /// entity and the call still runs one vector pass.
    #[tokio::test]
    async fn kg_blend_with_one_kind_present_returns_it_in_one_vector_pass() {
        for kind in ["concept", "document"] {
            let f = pack(rt_with_marker_embedder());
            let domain_id = seed_domain_and_atom(&f).await;
            let seeded = if kind == "concept" {
                seed_kg_concept(&f).await
            } else {
                seed_kg_document(&f).await
            };

            let (resp, usage) = compose_counting_usage(&f, &domain_id).await;

            assert_eq!(
                usage["vector_passes"], 1,
                "a {kind}-only blend must run one vector pass, got: {usage}"
            );
            let entities = resp["data"]["entities"]
                .as_array()
                .expect("entities array must be present when one kind blends");
            let ids: Vec<&str> = entities.iter().filter_map(|e| e["id"].as_str()).collect();
            assert_eq!(
                ids,
                [seeded.as_str()],
                "a {kind}-only blend must return exactly the seeded entity, got: {entities:?}"
            );
        }
    }

    /// The shared vector query leaves each kind with the list a per-kind search returns,
    /// also when one kind has more matches than its budget.
    ///
    /// Scenario: with a limit of 2, six concepts match both the text query and the vector,
    /// so the concept list is cut to its budget. Two documents match only by vector. The
    /// oracle is the per-kind `hybrid_search` call the blend made before it shared the
    /// vector query.
    #[tokio::test]
    async fn kg_blend_each_kind_matches_the_per_kind_search_when_a_kind_is_over_budget() {
        const LIMIT: u32 = 2;
        let rt = rt_with_marker_embedder();
        let f = pack(rt.clone());
        for i in 0..6 {
            f.dispatch(
                "create",
                json!({
                    "kind": "concept",
                    "name": format!("Concept{i}"),
                    "description": format!("{MARKER} paged cache variant {i}"),
                }),
            )
            .await
            .expect("create kg concept");
        }
        for i in 0..2 {
            f.dispatch(
                "create",
                json!({
                    "kind": "document",
                    "name": format!("Document{i}"),
                    "description": format!("{MARKER} decode attention paper {i}"),
                }),
            )
            .await
            .expect("create kg document");
        }
        let token = rt.authorize(Namespace::local()).expect("authorize");
        let mut marker_vector = vec![0.0f32; DIM];
        marker_vector[0] = 1.0;
        let kinds = ["concept", "document"];

        let mut expected: Vec<Vec<String>> = Vec::new();
        for kind in kinds {
            let hits = rt
                .hybrid_search(
                    &token,
                    "paged",
                    Some(marker_vector.clone()),
                    LIMIT,
                    Some(kind),
                    None,
                    &[],
                    None,
                )
                .await
                .expect("per-kind search");
            expected.push(hits.iter().map(|hit| hit.entity_id.to_string()).collect());
        }
        assert_eq!(
            expected[0].len(),
            LIMIT as usize,
            "premise: the concept list is cut to its budget"
        );
        assert_eq!(
            expected[1].len(),
            2,
            "premise: the document list is not empty"
        );

        let per_kind = rt
            .hybrid_search_each_kind(&token, "paged", Some(marker_vector), LIMIT, &kinds)
            .await
            .expect("shared-vector search");
        let mut actual: Vec<Vec<String>> = Vec::new();
        for hits in &per_kind {
            actual.push(hits.iter().map(|hit| hit.entity_id.to_string()).collect());
        }
        assert_eq!(
            actual, expected,
            "each kind must get the ids and order its own per-kind search returns"
        );
    }

    /// Test 8: zero-atom edge case (ADR-051 Amendment 1 Decision) — when the
    /// final compose body ends up with zero atoms (everything trimmed by
    /// `max_tokens`), the entity inclusion floor is undefined, so no
    /// entities blend even though the leftover budget would easily fit one.
    #[tokio::test]
    async fn zero_atoms_in_final_body_blends_no_entities_even_with_budget_room() {
        let f = pack(rt_with_marker_embedder());

        // Oversized atom: alone it exceeds the max_tokens=500 (2000-char)
        // budget, so it is excluded from the body entirely — body_used stays
        // 0, leaving the *entire* budget as "remaining" for the entity trim.
        let filler = "x".repeat(2500);
        let big_content = format!("{OVERLAP_CONTENT} {filler}");
        f.dispatch(
            "knowledge.upsert_atoms",
            json!({
                "atoms": [
                    { "slug": "kg-blend-atom", "name": "KG Blend Atom", "content": big_content, "finalized": true }
                ]
            }),
        )
        .await
        .expect("upsert atom");
        f.dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [
                    {
                        "slug": "kg-blend-domain",
                        "name": "KG Blend Domain",
                        "description": OVERLAP_CONTENT,
                        "members": ["kg-blend-atom"]
                    }
                ]
            }),
        )
        .await
        .expect("upsert domain");
        seed_kg_concept(&f).await;

        let resp = f
            .dispatch(
                "knowledge.compose",
                json!({ "query": QUERY, "max_tokens": 500 }),
            )
            .await
            .expect("compose ok");

        let data = &resp["data"];
        assert!(
            data.get("entities").is_none(),
            "zero-atom final body must blend no entities even with room in the budget, got: {data}"
        );
    }

    // ── degrade-not-abort ───────────────────────────────────────────────────

    /// Embedder that fails exactly the single-text `embed_query` calls the KG
    /// blend path's `hybrid_search` → `vector_search` makes for the literal
    /// compose query text, while behaving like `MarkerEmbedService` for every
    /// other call (batched atom rerank, entity setup during seeding). This
    /// isolates the failure to the blend boundary without breaking fixture
    /// setup or the atom-scoring path, which only ever calls `embed` with
    /// batches of 2+ texts (query + candidates).
    struct FailingBlendEmbedService;

    const CANCEL_QUERY: &str = "zzzquantumfoo cancellation boundary kv cache paging decode \
        attention retrieval augmented generation dense sparse benchmark corpus";

    static CANCEL_ON_BLEND_FAILURE: std::sync::Mutex<Option<tokio::sync::watch::Sender<bool>>> =
        std::sync::Mutex::new(None);

    #[async_trait]
    impl EmbeddingService for FailingBlendEmbedService {
        async fn embed(
            &self,
            texts: &[String],
            _model: EmbeddingModel,
        ) -> std::result::Result<Vec<Vec<f32>>, EmbedError> {
            if texts.len() == 1 && (texts[0] == QUERY || texts[0] == CANCEL_QUERY) {
                if texts[0] == CANCEL_QUERY {
                    if let Some(cancel) = CANCEL_ON_BLEND_FAILURE
                        .lock()
                        .unwrap_or_else(std::sync::PoisonError::into_inner)
                        .take()
                    {
                        let _ = cancel.send(true);
                    }
                }
                return Err(EmbedError::Internal(
                    "simulated KG blend-path embed failure".into(),
                ));
            }
            Ok(texts
                .iter()
                .map(|t| {
                    let mut v = vec![0.0f32; DIM];
                    if t.contains(MARKER) {
                        v[0] = 1.0;
                    } else {
                        v[1] = 1.0;
                    }
                    v
                })
                .collect())
        }

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

        fn name(&self) -> &'static str {
            "failing-blend-embed-service"
        }
    }

    struct FailingBlendEmbedProvider;

    #[async_trait]
    impl EmbedderProvider for FailingBlendEmbedProvider {
        fn name(&self) -> &str {
            MODEL_KEY
        }

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

        async fn build(
            &self,
        ) -> std::result::Result<Arc<dyn EmbeddingService>, khive_runtime::RuntimeError> {
            Ok(Arc::new(FailingBlendEmbedService))
        }
    }

    fn rt_with_failing_blend_embedder() -> KhiveRuntime {
        let rt = KhiveRuntime::new(RuntimeConfig {
            wal_ceiling_bytes: 0,
            wal_ceiling_configured_bytes: 0,
            wal_ceiling_source: Default::default(),
            wal_ceiling_env_raw: None,
            web: Default::default(),
            telemetry: Default::default(),
            mounts: Vec::new(),
            brain: Default::default(),
            git_write: Default::default(),
            display_timezone: khive_runtime::config::resolve_default_display_timezone(),
            events_split: None,
            db_path: None,
            blob_hydration_bytes: khive_runtime::DEFAULT_BLOB_HYDRATION_BYTES,
            default_namespace: Namespace::local(),
            embedding_model: Some(EmbeddingModel::AllMiniLmL6V2),
            additional_embedding_models: vec![],
            gate: Arc::new(AllowAllGate),
            packs: vec!["kg".to_string(), "knowledge".to_string()],
            backend_id: BackendId::main(),
            brain_profile: None,
            visible_namespaces: vec![],
            allowed_outbound_namespaces: vec![],
            actor_id: None,
            exec: Default::default(),
            ..khive_runtime::RuntimeConfig::no_embeddings()
        })
        .expect("runtime");
        rt.register_embedder(FailingBlendEmbedProvider);
        rt
    }

    /// Test 9 (High finding regression): a KG-blend-path embed failure
    /// (`hybrid_search` → `vector_search` → `embed_query` erroring) must not
    /// abort `knowledge.compose` — it must degrade to the already-finalized
    /// atom-only response. Uses explicit `domain_ids` (not AUTO) so the
    /// `knowledge.suggest` phase never calls `embed_query` on `QUERY` itself,
    /// keeping the induced failure isolated to the KG blend call.
    #[tokio::test]
    async fn kg_blend_failure_degrades_to_atom_only_response() {
        let f = pack(rt_with_failing_blend_embedder());
        let domain_id = seed_domain_and_atom(&f).await;
        seed_kg_concept(&f).await;

        let resp = f
            .dispatch(
                "knowledge.compose",
                json!({ "domain_ids": [domain_id], "query": QUERY }),
            )
            .await
            .expect("compose must succeed atom-only despite KG blend failure");

        let data = &resp["data"];
        let atoms = data["atoms"].as_array().expect("atoms array");
        assert!(
            !atoms.is_empty(),
            "atom-only body must survive a KG blend failure, got: {data}"
        );
        assert!(
            data.get("entities").is_none(),
            "entities must be absent when the KG blend fails, got: {data}"
        );
        let md = data["markdown"].as_str().expect("markdown");
        assert!(
            !md.contains("Knowledge graph"),
            "markdown must not contain a Knowledge graph section when the blend fails, got: {md}"
        );
    }

    #[tokio::test]
    async fn cancellation_during_kg_blend_failure_never_degrades_to_success() {
        let f = pack(rt_with_failing_blend_embedder());
        let domain_id = seed_domain_and_atom(&f).await;
        seed_kg_concept(&f).await;
        let (cancel_tx, cancel_rx) = tokio::sync::watch::channel(false);
        *CANCEL_ON_BLEND_FAILURE
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(cancel_tx);

        let result = khive_storage::scope_request_read_cancellation(
            cancel_rx,
            f.dispatch(
                "knowledge.compose",
                json!({ "domain_ids": [domain_id], "query": CANCEL_QUERY }),
            ),
        )
        .await;
        assert!(
            matches!(
                result,
                Err(khive_runtime::RuntimeError::Storage(
                    khive_storage::StorageError::Timeout { .. }
                ))
            ),
            "request cancellation at the KG-blend catch must propagate, got {result:?}"
        );
    }
}

include!("integration_tests/private_merge_admission_tests.rs");

include!("integration_tests/merge_error_and_success_tests.rs");

include!("integration_tests/resolver_error_tests.rs");

include!("integration_tests/properties_only_payload_tests.rs");

include!("integration_tests/properties_only_validation_tests.rs");

include!("integration_tests/properties_only_batch_tests.rs");

include!("support/project_origin.rs");

#[path = "integration/refusal_events.rs"]
mod refusal_events;

#[path = "../src/knowledge/cursor_query.rs"]
mod cursor_query;

#[tokio::test]
async fn cursor_indexes_cover_order_and_walk_terminates() {
    use khive_db::{migrations::MIGRATIONS, StorageBackend};
    use khive_runtime::RuntimeConfig;
    use std::collections::HashSet;

    for migrated in [false, true] {
        let backend = Arc::new(StorageBackend::memory().unwrap());
        if migrated {
            backend
                .pool()
                .writer()
                .unwrap()
                .autocommit(|conn| {
                    conn.execute_batch(include_str!(
                        "../../khive-db/sql/schema-migrations-table.sql"
                    ))
                    .unwrap();
                    // Rebuild the actual parent migration prefix, without the new indexes.
                    for migration in MIGRATIONS.iter().filter(|m| m.version <= 38) {
                        conn.execute_batch(migration.up).unwrap();
                        conn.execute(
                    "INSERT INTO _schema_migrations (version, name, applied_at) VALUES (?1, ?2, 0)",
                    rusqlite::params![migration.version, migration.name],
                )
                .unwrap();
                    }
                    Ok(())
                })
                .unwrap();
        } else {
            backend.prepare_core_schema().unwrap();
        }
        {
            backend
                .pool()
                .writer()
                .unwrap()
                .transaction(|conn| {
                    for (kind, table) in [(0, "knowledge_atoms"), (1, "knowledge_domains")] {
                        for (ns_number, namespace) in [(0, "local"), (1, "foreign")] {
                            for n in (1..=1020).rev() {
                                // 1000 live rows, ten tombstones, ten atom-only domain mirrors.
                                if kind == 1 && n > 1010 {
                                    continue;
                                }
                                let id = format!("{kind:08x}-{ns_number:04x}-4000-8000-{n:012x}");
                                let tags = if n > 1010 { "[\"type:domain\"]" } else { "[]" };
                                conn.execute(
                                    &format!(
                                        "INSERT INTO {table} \
                                (id, namespace, slug, name, tags, created_at, \
                                updated_at, deleted_at) \
                                VALUES (?1, ?2, ?3, 'cursor row', ?4, ?5, ?5, ?6)"
                                    ),
                                    rusqlite::params![
                                        id,
                                        namespace,
                                        format!("row-{n}"),
                                        tags,
                                        n / 25,
                                        if (1001..=1010).contains(&n) {
                                            Some(1)
                                        } else {
                                            None
                                        }
                                    ],
                                )
                                .unwrap();
                            }
                        }
                    }
                    Ok(())
                })
                .unwrap();
        }
        if migrated {
            let writer = backend.pool().writer().unwrap();
            let count: i64 = writer
                .autocommit(|conn| {
                    Ok(conn.query_row(
                    "SELECT count(*) FROM sqlite_schema WHERE name LIKE 'idx_knowledge_%_cursor'",
                    [],
                    |row| row.get(0),
                )?)
                })
                .unwrap();
            assert_eq!(count, 0, "historical fixture must lack cursor indexes");
            backend.prepare_core_schema().unwrap();
        }
        let runtime = KhiveRuntime::from_backend(backend, RuntimeConfig::no_embeddings());
        let fixture = pack(runtime.clone());
        for (kind, list_type, table) in [
            (0, "atom", "knowledge_atoms"),
            (1, "domain", "knowledge_domains"),
        ] {
            let index = format!("idx_{table}_cursor");
            {
                let writer = runtime.backend().pool().writer().unwrap();
                writer
                    .autocommit(|conn| {
                        let columns: Vec<String> = conn
                            .prepare(&format!("PRAGMA index_info('{index}')"))
                            .unwrap()
                            .query_map([], |r| r.get(2))
                            .unwrap()
                            .collect::<Result<_, _>>()
                            .unwrap();
                        assert_eq!(
                            columns,
                            ["namespace", "created_at", "id"],
                            "cursor index must cover tie-breaker"
                        );
                        for subsequent in [false, true] {
                            let clause = if kind == 0 {
                                " AND (status IS NULL OR status != 'deprecated')"
                            } else {
                                ""
                            };
                            let sql =
                                cursor_query::cursor_query(table, "id, slug", subsequent, clause);
                            let mut params = vec![rusqlite::types::Value::Text("local".into())];
                            if subsequent {
                                params.push(rusqlite::types::Value::Integer(0));
                                params.push(rusqlite::types::Value::Text(format!(
                                    "{kind:08x}-0000-4000-8000-000000000001"
                                )));
                            }
                            params.push(rusqlite::types::Value::Integer(74));
                            let plan: Vec<String> = conn
                                .prepare(&format!("EXPLAIN QUERY PLAN {sql}"))
                                .unwrap()
                                .query_map(rusqlite::params_from_iter(params), |r| r.get(3))
                                .unwrap()
                                .collect::<Result<_, _>>()
                                .unwrap();
                            eprintln!(
                                "migrated={migrated} subsequent={subsequent} {table}: {plan:?}"
                            );
                            assert!(
                                plan.iter().any(|p| p.contains(&index)),
                                "cursor plan must use compound index: {plan:?}"
                            );
                            assert!(
                                !plan.iter().any(|p| p.contains("TEMP B-TREE")),
                                "cursor ordering must not sort: {plan:?}"
                            );
                        }
                        Ok(())
                    })
                    .unwrap();
            }
            for (ns_number, namespace) in [(0, "local"), (1, "foreign")] {
                let expected: HashSet<String> = (1..=1000)
                    .map(|n| format!("{kind:08x}-{ns_number:04x}-4000-8000-{n:012x}"))
                    .collect();
                let mut seen = HashSet::new();
                let mut after = String::new();
                let mut terminated = false;
                for _ in 0..20 {
                    let page = fixture
                        .dispatch(
                            "knowledge.list",
                            json!({
                                "type": list_type, "namespace": namespace, "after": after,
                                "limit": 73, "fields": ["id", "slug"],
                            }),
                        )
                        .await
                        .unwrap();
                    for row in page["results"].as_array().unwrap() {
                        assert!(
                            seen.insert(row["id"].as_str().unwrap().to_string()),
                            "cursor walk must not repeat rows"
                        );
                    }
                    if page["next_after"].is_null() {
                        terminated = true;
                        break;
                    }
                    after = page["next_after"].as_str().unwrap().to_string();
                }
                assert!(
                    terminated,
                    "cursor walk must terminate with null next_after"
                );
                assert_eq!(
                    seen, expected,
                    "cursor walk must return exact live namespace set"
                );
            }
        }
    }
}

include!("integration_tests/cite_weight_tests.rs");