khive-pack-knowledge 0.3.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
// 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::{KhiveRuntime, PackRegistry, RuntimeError, VerbRegistry, VerbRegistryBuilder};
use serde_json::{json, Value};

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

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

struct Fixture {
    registry: VerbRegistry,
}

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");
    rt.install_edge_rules(registry.all_edge_rules());
    Fixture { registry }
}

// ── pack metadata ─────────────────────────────────────────────────────────────

#[tokio::test]
async fn pack_registers_cleanly_with_verb_registry() {
    let f = pack(rt());
    let verbs: Vec<&str> = f.registry.all_verbs().iter().map(|v| v.name).collect();
    assert!(
        verbs.contains(&"knowledge.learn"),
        "expected 'learn' verb, got: {verbs:?}"
    );
    assert!(
        verbs.contains(&"knowledge.cite"),
        "expected 'cite' verb, got: {verbs:?}"
    );
    assert!(
        verbs.contains(&"knowledge.topic"),
        "expected 'topic' verb, got: {verbs:?}"
    );
    // Corpus-tier verbs must also be registered.
    assert!(
        verbs.contains(&"knowledge.upsert_atoms"),
        "expected 'knowledge.upsert_atoms' verb, got: {verbs:?}"
    );
    assert!(
        verbs.contains(&"knowledge.search"),
        "expected 'knowledge.search' verb, got: {verbs:?}"
    );
    assert!(
        verbs.contains(&"knowledge.fold"),
        "expected 'knowledge.fold' verb, got: {verbs:?}"
    );
    assert!(
        verbs.contains(&"knowledge.suggest"),
        "expected 'knowledge.suggest' verb, got: {verbs:?}"
    );
    assert!(
        verbs.contains(&"knowledge.compose"),
        "expected 'knowledge.compose' verb, got: {verbs:?}"
    );
    // No note kinds added.
    let note_kinds: Vec<&str> = f.registry.all_note_kinds();
    assert!(
        !note_kinds.contains(&"knowledge"),
        "knowledge pack should not add note kinds"
    );
}

// ── learn verb ────────────────────────────────────────────────────────────────

#[tokio::test]
async fn learn_creates_concept_with_name_and_domain() {
    let f = pack(rt());
    let resp = f
        .dispatch(
            "knowledge.learn",
            json!({
                "name": "LoRA",
                "description": "Low-Rank Adaptation of large language models — covering concepts techniques algorithms implementations applications use cases and design patterns in detail",
                "domain": "fine-tuning",
                "tags": ["adapter"]
            }),
        )
        .await
        .expect("learn ok");

    assert_eq!(resp["kind"], "concept");
    assert_eq!(resp["name"], "LoRA");
    assert_eq!(resp["domain"], "fine-tuning");
    // Domain is promoted to tags.
    let tags = resp["tags"].as_array().expect("tags array");
    let tag_strs: Vec<&str> = tags.iter().filter_map(|v| v.as_str()).collect();
    assert!(
        tag_strs.contains(&"fine-tuning"),
        "domain not in tags: {tag_strs:?}"
    );
    assert!(tag_strs.contains(&"adapter"));
    // Response shape: short id (8 chars) + full UUID.
    let id = resp["id"].as_str().expect("id");
    let full_id = resp["full_id"].as_str().expect("full_id");
    assert_eq!(id.len(), 8, "expected 8-char short id, got: {id}");
    assert!(
        full_id.contains('-'),
        "expected UUID in full_id, got: {full_id}"
    );
}

#[tokio::test]
async fn learn_creates_concept_without_domain() {
    let f = pack(rt());
    let resp = f
        .dispatch("knowledge.learn", json!({ "name": "FlashAttention" }))
        .await
        .expect("learn ok");

    assert_eq!(resp["kind"], "concept");
    assert_eq!(resp["name"], "FlashAttention");
    assert!(resp["domain"].is_null());
}

#[tokio::test]
async fn learn_rejects_empty_name() {
    let f = pack(rt());
    let err = f
        .dispatch("knowledge.learn", json!({ "name": "   " }))
        .await
        .unwrap_err();
    let msg = err.to_string();
    assert!(msg.contains("name must not be empty"), "got: {msg}");
}

#[tokio::test]
async fn learn_rejects_missing_name_and_content() {
    let f = pack(rt());
    let err = f
        .dispatch("knowledge.learn", json!({ "domain": "attention" }))
        .await
        .unwrap_err();
    let msg = err.to_string();
    // Neither name nor content supplied — handler returns a descriptive error.
    assert!(
        msg.contains("name must not be empty"),
        "expected descriptive error, got: {msg}"
    );
}

// ── learn content-alias (issue #488) ─────────────────────────────────────────

#[tokio::test]
async fn learn_content_without_name_auto_generates_name() {
    let f = pack(rt());
    // Agent-style call: only `content` provided, no explicit `name`.
    let resp = f
        .dispatch(
            "knowledge.learn",
            json!({ "content": "Some long description about X that keeps going and going beyond sixty characters easily dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }),
        )
        .await
        .expect("learn with content only should succeed");

    assert_eq!(resp["kind"], "concept");
    let name = resp["name"].as_str().expect("name present");
    assert!(!name.is_empty(), "auto-generated name must not be empty");
    assert!(
        name.len() <= 60,
        "auto-generated name must be <= 60 chars, got: {name:?}"
    );
    // Description is populated from `content`.
    let desc = resp["description"].as_str().expect("description present");
    assert!(
        desc.contains("Some long description"),
        "description should contain content: {desc:?}"
    );
}

#[tokio::test]
async fn learn_content_alias_maps_to_description() {
    let f = pack(rt());
    // When both `name` and `content` are provided, content becomes the description.
    let resp = f
        .dispatch(
            "knowledge.learn",
            json!({
                "name": "GQA",
                "content": "Grouped-Query Attention mechanism"
            }),
        )
        .await
        .expect("learn with name + content");

    assert_eq!(resp["name"], "GQA");
    assert_eq!(resp["description"], "Grouped-Query Attention mechanism");
}

#[tokio::test]
async fn learn_short_content_uses_full_text_as_name() {
    let f = pack(rt());
    let resp = f
        .dispatch(
            "knowledge.learn",
            json!({ "content": "Speculative Decoding" }),
        )
        .await
        .expect("learn short content");

    assert_eq!(resp["name"], "Speculative Decoding");
    assert_eq!(resp["description"], "Speculative Decoding");
}

// ── cite verb ─────────────────────────────────────────────────────────────────

#[tokio::test]
async fn cite_creates_introduced_by_edge() {
    let f = pack(rt());

    // Create concept via learn.
    let concept = f
        .dispatch(
            "knowledge.learn",
            json!({ "name": "LoRA", "domain": "fine-tuning" }),
        )
        .await
        .expect("learn concept");

    // Create paper as a `document` entity (base allowlist: concept -[introduced_by]-> document).
    let paper = f
        .dispatch(
            "create",
            json!({
                "kind": "document",
                "name": "Hu et al. 2021",
                "description": "LoRA: Low-Rank Adaptation paper — covering concepts techniques algorithms implementations applications use cases and design patterns in detail — covering"
            }),
        )
        .await
        .expect("create document");

    let concept_id = concept["full_id"].as_str().unwrap();
    // `create` serialises the raw Entity — id field is the full UUID string.
    let source_id = paper["id"].as_str().unwrap();

    let resp = f
        .dispatch(
            "knowledge.cite",
            json!({
                "concept_id": concept_id,
                "source_id": source_id,
                "weight": 1.0
            }),
        )
        .await
        .expect("cite ok");

    assert_eq!(resp["relation"], "introduced_by");
    assert_eq!(resp["concept_id"], concept_id);
    assert_eq!(resp["source_id"], source_id);
    assert_eq!(resp["weight"], 1.0);
    let id = resp["id"].as_str().expect("id");
    assert_eq!(id.len(), 8, "expected 8-char edge id, got: {id}");
}

#[tokio::test]
async fn cite_rejects_unknown_id() {
    let f = pack(rt());
    let err = f
        .dispatch(
            "knowledge.cite",
            json!({
                "concept_id": "00000000-0000-0000-0000-000000000001",
                "source_id":  "00000000-0000-0000-0000-000000000002"
            }),
        )
        .await
        .unwrap_err();
    let msg = err.to_string();
    assert!(!msg.is_empty(), "expected error for unknown IDs, got empty");
}

#[tokio::test]
async fn cite_rejects_missing_concept_id() {
    let f = pack(rt());
    let err = f
        .dispatch(
            "knowledge.cite",
            json!({ "source_id": "00000000-0000-0000-0000-000000000001" }),
        )
        .await
        .unwrap_err();
    let msg = err.to_string();
    assert!(!msg.is_empty(), "expected deserialization error");
}

// ── topic verb ────────────────────────────────────────────────────────────────

#[tokio::test]
async fn topic_lists_all_concepts_without_filter() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.learn",
        json!({ "name": "GQA", "domain": "attention" }),
    )
    .await
    .expect("learn 1");
    f.dispatch(
        "knowledge.learn",
        json!({ "name": "FlashAttention", "domain": "attention" }),
    )
    .await
    .expect("learn 2");
    f.dispatch(
        "knowledge.learn",
        json!({ "name": "LoRA", "domain": "fine-tuning" }),
    )
    .await
    .expect("learn 3");

    let resp = f
        .dispatch("knowledge.topic", json!({}))
        .await
        .expect("topic ok");

    let items = resp["results"].as_array().expect("results array");
    assert_eq!(items.len(), 3, "expected 3 concepts, got: {}", items.len());
}

#[tokio::test]
async fn topic_filters_by_domain() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.learn",
        json!({ "name": "GQA", "domain": "attention" }),
    )
    .await
    .expect("learn 1");
    f.dispatch(
        "knowledge.learn",
        json!({ "name": "FlashAttention", "domain": "attention" }),
    )
    .await
    .expect("learn 2");
    f.dispatch(
        "knowledge.learn",
        json!({ "name": "LoRA", "domain": "fine-tuning" }),
    )
    .await
    .expect("learn 3");

    let resp = f
        .dispatch("knowledge.topic", json!({ "domain": "attention" }))
        .await
        .expect("topic filtered");

    let items = resp["results"].as_array().expect("results array");
    assert_eq!(
        items.len(),
        2,
        "expected 2 attention concepts, got: {}",
        items.len()
    );

    let names: Vec<&str> = items.iter().filter_map(|v| v["name"].as_str()).collect();
    assert!(names.contains(&"GQA"), "expected GQA in items: {names:?}");
    assert!(
        names.contains(&"FlashAttention"),
        "expected FlashAttention: {names:?}"
    );
}

#[tokio::test]
async fn topic_returns_empty_for_unknown_domain() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.learn",
        json!({ "name": "LoRA", "domain": "fine-tuning" }),
    )
    .await
    .expect("learn");

    let resp = f
        .dispatch("knowledge.topic", json!({ "domain": "quantum-computing" }))
        .await
        .expect("topic ok");

    let items = resp["results"].as_array().expect("results array");
    assert!(items.is_empty(), "expected 0 items for unknown domain");
}

#[tokio::test]
async fn topic_respects_limit() {
    let f = pack(rt());
    for i in 0..5 {
        f.dispatch("knowledge.learn", json!({ "name": format!("Concept{i}") }))
            .await
            .expect("learn");
    }

    let resp = f
        .dispatch("knowledge.topic", json!({ "limit": 2 }))
        .await
        .expect("topic ok");

    let items = resp["results"].as_array().expect("results array");
    assert!(
        items.len() <= 2,
        "expected <= 2 items, got: {}",
        items.len()
    );
}

// ── H1 regression: case-insensitive domain filter (ADR-047 §91) ──────────────

#[tokio::test]
async fn topic_domain_filter_is_case_insensitive_listing_path() {
    let f = pack(rt());

    // Store concept with uppercase domain "Attention".
    f.dispatch(
        "knowledge.learn",
        json!({ "name": "FlashAttention", "domain": "Attention" }),
    )
    .await
    .expect("learn with Attention");

    f.dispatch(
        "knowledge.learn",
        json!({ "name": "LoRA", "domain": "fine-tuning" }),
    )
    .await
    .expect("learn with fine-tuning");

    // Query with lowercase "attention" — must find the concept stored as "Attention".
    let resp = f
        .dispatch("knowledge.topic", json!({ "domain": "attention" }))
        .await
        .expect("topic ok");

    let items = resp["results"].as_array().expect("results array");
    let names: Vec<&str> = items.iter().filter_map(|v| v["name"].as_str()).collect();
    assert_eq!(items.len(), 1, "expected 1 match, got: {names:?}");
    assert!(
        names.contains(&"FlashAttention"),
        "expected FlashAttention in results: {names:?}"
    );
    assert_eq!(
        resp["total"].as_u64().unwrap_or(0),
        1,
        "total should be 1 on listing path"
    );
}

// ── H2 regression: search-path `total` semantics ─────────────────────────────

#[tokio::test]
async fn topic_search_path_total_is_bounded_by_candidate_window() {
    let f = pack(rt());

    // Learn 10 concepts — more than a small limit, so we can observe truncation.
    for i in 0..10 {
        f.dispatch(
            "knowledge.learn",
            json!({ "name": format!("Attention{i}"), "domain": "attention" }),
        )
        .await
        .expect("learn");
    }
    f.dispatch(
        "knowledge.learn",
        json!({ "name": "LoRA", "domain": "fine-tuning" }),
    )
    .await
    .expect("learn unrelated");

    // Search path with limit=3.  total must be <= limit*4 (12) and >= returned items.
    let resp = f
        .dispatch(
            "knowledge.topic",
            json!({ "query": "attention", "limit": 3 }),
        )
        .await
        .expect("topic search ok");

    let items = resp["results"].as_array().expect("results array");
    let total = resp["total"].as_u64().expect("total field present");

    assert!(
        items.len() <= 3,
        "items must respect limit: got {}",
        items.len()
    );
    // total is the candidate-window count, bounded by limit*4 = 12.
    assert!(
        total <= 12,
        "search-path total must be bounded by limit*4 (12), got {total}"
    );
    assert!(
        total >= items.len() as u64,
        "total must be >= returned items: total={total}, items={}",
        items.len()
    );
}

// ── upsert_atoms ──────────────────────────────────────────────────────────────

#[tokio::test]
async fn upsert_atoms_creates_new_atoms() {
    let f = pack(rt());
    let resp = f
        .dispatch(
            "knowledge.upsert_atoms",
            json!({
                "atoms": [
                    { "slug": "rag", "name": "RAG", "content": "RAG retrieves relevant passages before generating. dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity", "tags": ["retrieval", "rag"] },
                    { "slug": "lora", "name": "LoRA", "content": "Low-Rank Adaptation of LLMs — covering concepts techniques algorithms implementations applications use cases and design patterns in detail — covering", "tags": ["fine-tuning", "adapter"] },
                    { "slug": "flash-attention", "name": "FlashAttention", "content": "Memory-efficient attention — covering concepts techniques algorithms implementations applications use cases and design patterns in detail — covering concepts techniques", "tags": ["attention"] },
                ]
            }),
        )
        .await
        .expect("upsert_atoms ok");

    assert_eq!(resp["created"], 3, "expected 3 created");
    assert_eq!(resp["updated"], 0, "expected 0 updated");
    assert_eq!(resp["total"], 3);
}

#[tokio::test]
async fn upsert_atoms_updates_on_second_call() {
    let f = pack(rt());
    // First insert.
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({ "atoms": [{ "slug": "rag", "name": "RAG", "content": "original content dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
    )
    .await
    .expect("first upsert");

    // Second call with same slug — should update.
    let resp = f
        .dispatch(
            "knowledge.upsert_atoms",
            json!({ "atoms": [{ "slug": "rag", "name": "RAG updated", "content": "updated content dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
        )
        .await
        .expect("second upsert");

    assert_eq!(resp["created"], 0, "expected 0 created on second call");
    assert_eq!(resp["updated"], 1, "expected 1 updated");

    // Verify get returns the updated name.
    let got = f
        .dispatch("knowledge.get", json!({ "id": "rag" }))
        .await
        .expect("get ok");
    assert_eq!(got["name"], "RAG updated");
    assert_eq!(got["slug"], "rag");
}

#[tokio::test]
async fn upsert_atoms_rejects_empty_list() {
    let f = pack(rt());
    let err = f
        .dispatch("knowledge.upsert_atoms", json!({ "atoms": [] }))
        .await
        .unwrap_err();
    assert!(err.to_string().contains("must not be empty"), "got: {err}");
}

#[tokio::test]
async fn upsert_atoms_rejects_empty_slug() {
    let f = pack(rt());
    let err = f
        .dispatch(
            "knowledge.upsert_atoms",
            json!({ "atoms": [{ "slug": "  ", "name": "Bad", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
        )
        .await
        .unwrap_err();
    assert!(err.to_string().contains("slug"), "got: {err}");
}

// ── upsert_domains ────────────────────────────────────────────────────────────

#[tokio::test]
async fn upsert_domains_creates_and_updates() {
    let f = pack(rt());
    let resp = f
        .dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [
                    { "slug": "retrieval", "name": "Retrieval", "description": "Retrieval techniques — covering concepts techniques algorithms implementations applications use cases and design patterns in detail — covering concepts techniques", "members": ["rag", "dense-retrieval"] }
                ]
            }),
        )
        .await
        .expect("upsert_domains ok");

    assert_eq!(resp["created"], 1);
    assert_eq!(resp["updated"], 0);

    // Second call — update.
    let resp2 = f
        .dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [
                    { "slug": "retrieval", "name": "Retrieval updated", "description": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity", "members": ["rag", "dense-retrieval", "bm25"] }
                ]
            }),
        )
        .await
        .expect("second upsert_domains ok");

    assert_eq!(resp2["created"], 0);
    assert_eq!(resp2["updated"], 1);

    // get by slug returns updated name.
    let got = f
        .dispatch("knowledge.get", json!({ "id": "retrieval" }))
        .await
        .expect("get domain ok");
    assert_eq!(got["name"], "Retrieval updated");
    assert_eq!(got["kind"], "domain");
    let members = got["members"].as_array().expect("members array");
    assert_eq!(members.len(), 3);
}

#[tokio::test]
async fn upsert_domains_rejects_empty_list() {
    let f = pack(rt());
    let err = f
        .dispatch("knowledge.upsert_domains", json!({ "domains": [] }))
        .await
        .unwrap_err();
    assert!(err.to_string().contains("must not be empty"), "got: {err}");
}

// ── knowledge.get ─────────────────────────────────────────────────────────────

#[tokio::test]
async fn get_returns_atom_by_slug() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({ "atoms": [{ "slug": "lora", "name": "LoRA", "content": "Low-Rank Adaptation — covering concepts techniques algorithms implementations applications use cases and design patterns in detail — covering concepts techniques" }] }),
    )
    .await
    .expect("upsert");

    let got = f
        .dispatch("knowledge.get", json!({ "id": "lora" }))
        .await
        .expect("get ok");

    assert_eq!(got["slug"], "lora");
    assert_eq!(got["name"], "LoRA");
    assert_eq!(got["kind"], "atom");
}

#[tokio::test]
async fn get_returns_not_found_for_unknown_slug() {
    let f = pack(rt());
    let err = f
        .dispatch("knowledge.get", json!({ "id": "nonexistent-slug-xyz" }))
        .await
        .unwrap_err();
    assert!(
        err.to_string().contains("not found") || err.to_string().contains("NotFound"),
        "expected not-found error, got: {err}"
    );
}

// ── knowledge.get + include_sections ─────────────────────────────────────────

#[tokio::test]
async fn get_include_sections_false_returns_no_sections_key() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({ "atoms": [{ "slug": "s-atom", "name": "SAtom", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
    )
    .await
    .expect("upsert");
    f.dispatch(
        "knowledge.edit",
        json!({ "id": "s-atom", "sections": [{ "section_type": "overview", "content": "This section describes the overview of LoRA and its applications in fine-tuning large language models with low-rank matrix decompositions." }] }),
    )
    .await
    .expect("edit");

    let got = f
        .dispatch("knowledge.get", json!({ "id": "s-atom" }))
        .await
        .expect("get without sections");

    assert_eq!(got["kind"], "atom");
    assert!(
        got.get("sections").is_none(),
        "sections key must not be present by default"
    );
}

#[tokio::test]
async fn get_include_sections_returns_all_sections_ordered() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({ "atoms": [{ "slug": "sec-atom", "name": "SecAtom", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
    )
    .await
    .expect("upsert");

    f.dispatch(
        "knowledge.edit",
        json!({
            "id": "sec-atom",
            "sections": [
                { "section_type": "overview", "content": "This is the overview section covering the main ideas and introduction to the topic in sufficient detail for embedding purposes." },
                { "section_type": "formalism", "content": "Formal definitions go here including mathematical notation theorems proofs lemmas and corollaries that describe the system formally." },
                { "section_type": "examples", "content": "Concrete examples illustrate the concepts with worked-through scenarios code samples and practical demonstrations of usage patterns." },
            ]
        }),
    )
    .await
    .expect("edit");

    let got = f
        .dispatch(
            "knowledge.get",
            json!({ "id": "sec-atom", "include_sections": true }),
        )
        .await
        .expect("get with sections");

    assert_eq!(got["kind"], "atom");
    let sections = got["sections"].as_array().expect("sections is array");
    assert_eq!(sections.len(), 3, "expected 3 sections, got: {sections:?}");

    let types: Vec<&str> = sections
        .iter()
        .filter_map(|s| s["section_type"].as_str())
        .collect();
    assert!(types.contains(&"overview"), "missing overview: {types:?}");
    assert!(types.contains(&"formalism"), "missing formalism: {types:?}");
    assert!(types.contains(&"examples"), "missing examples: {types:?}");

    for s in sections {
        assert!(
            s["content"].as_str().is_some_and(|c| !c.is_empty()),
            "section content empty"
        );
        assert!(s["section_type"].as_str().is_some(), "section_type missing");
        assert!(s["sort_order"].as_i64().is_some(), "sort_order missing");
    }
}

#[tokio::test]
async fn get_include_sections_by_uuid() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({ "atoms": [{ "slug": "uuid-sec-atom", "name": "UuidSecAtom", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
    )
    .await
    .expect("upsert");
    f.dispatch(
        "knowledge.edit",
        json!({ "id": "uuid-sec-atom", "sections": [{ "section_type": "overview", "content": "This section describes the overview of LoRA and its applications in fine-tuning large language models with low-rank matrix decompositions." }] }),
    )
    .await
    .expect("edit");

    let by_slug = f
        .dispatch(
            "knowledge.get",
            json!({ "id": "uuid-sec-atom", "include_sections": true }),
        )
        .await
        .expect("get by slug");
    let atom_uuid = by_slug["id"].as_str().expect("id").to_owned();

    let by_uuid = f
        .dispatch(
            "knowledge.get",
            json!({ "id": atom_uuid, "include_sections": true }),
        )
        .await
        .expect("get by uuid");

    let sections = by_uuid["sections"].as_array().expect("sections array");
    assert_eq!(sections.len(), 1, "expected 1 section by UUID lookup");
}

#[tokio::test]
async fn get_include_sections_namespace_isolation() {
    let f = pack(rt());

    // ADR-007 Rev 2: all storage routes to local. Two DISTINCT slugs each get
    // their own sections; sections from one atom must not leak to the other.
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({ "atoms": [{ "slug": "iso-atom-a", "name": "NSA", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
    )
    .await
    .expect("upsert atom-a");

    f.dispatch(
        "knowledge.edit",
        json!({ "id": "iso-atom-a", "sections": [{ "section_type": "overview", "content": "This section belongs exclusively to atom A and must not be visible when fetching atom B under any circumstances." }] }),
    )
    .await
    .expect("edit atom-a");

    f.dispatch(
        "knowledge.upsert_atoms",
        json!({ "atoms": [{ "slug": "iso-atom-b", "name": "NSB", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
    )
    .await
    .expect("upsert atom-b");

    let got_b = f
        .dispatch(
            "knowledge.get",
            json!({ "id": "iso-atom-b", "include_sections": true }),
        )
        .await
        .expect("get atom-b");

    let sections_b = got_b["sections"].as_array().expect("sections array");
    assert_eq!(sections_b.len(), 0, "atom-b must not see atom-a sections");
}

// Regression: two sections sharing the same sort_order must come back in a
// stable, deterministic order (id ASC as final tie-breaker).
#[tokio::test]
async fn get_include_sections_ordering_tie_break_is_stable() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({ "atoms": [{ "slug": "tie-atom", "name": "TieAtom", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
    )
    .await
    .expect("upsert");

    // Insert two sections with the same sort_order (both default to their
    // SectionType ordinal; explicitly override to the same value to guarantee
    // the tie). Each has distinct content so both rows are inserted.
    f.dispatch(
        "knowledge.edit",
        json!({
            "id": "tie-atom",
            "sections": [
                {
                    "section_type": "overview",
                    "content": "First section content for the tie-break test covering overview of the main topic in sufficient detail for the minimum content length requirement to be satisfied.",
                    "sort_order": 5
                },
                {
                    "section_type": "formalism",
                    "content": "Second section content for the tie-break test covering formal definitions and mathematical notation in sufficient detail for the minimum content length requirement.",
                    "sort_order": 5
                },
            ]
        }),
    )
    .await
    .expect("edit");

    // Fetch twice; both calls must return the same order.
    let first = f
        .dispatch(
            "knowledge.get",
            json!({ "id": "tie-atom", "include_sections": true }),
        )
        .await
        .expect("get first");
    let second = f
        .dispatch(
            "knowledge.get",
            json!({ "id": "tie-atom", "include_sections": true }),
        )
        .await
        .expect("get second");

    let s1 = first["sections"].as_array().expect("sections first");
    let s2 = second["sections"].as_array().expect("sections second");

    assert_eq!(s1.len(), 2, "expected 2 sections on first fetch");
    assert_eq!(s2.len(), 2, "expected 2 sections on second fetch");

    // Both rows share sort_order=5; the id-ASC tie-breaker must produce the
    // same sequence across repeated queries.
    let ids_first: Vec<&str> = s1.iter().filter_map(|s| s["id"].as_str()).collect();
    let ids_second: Vec<&str> = s2.iter().filter_map(|s| s["id"].as_str()).collect();
    assert_eq!(
        ids_first, ids_second,
        "section order must be deterministic across repeated fetches (id ASC tie-breaker)"
    );

    // Pin the full ordering contract (sort_order ASC, created_at ASC, id ASC):
    // repeated-read agreement alone can pass on SQLite insertion-order luck
    // even if the id tie-breaker is removed.
    let actual: Vec<(i64, i64, &str)> = s1
        .iter()
        .map(|s| {
            (
                s["sort_order"].as_i64().expect("sort_order"),
                s["created_at"].as_i64().expect("created_at"),
                s["id"].as_str().expect("id"),
            )
        })
        .collect();
    let mut expected = actual.clone();
    expected.sort();
    assert_eq!(
        actual, expected,
        "sections must be sorted by (sort_order, created_at, id)"
    );

    // Both calls must agree on which section type comes first (also validates
    // that the order is NOT random).
    let types_first: Vec<&str> = s1
        .iter()
        .filter_map(|s| s["section_type"].as_str())
        .collect();
    let types_second: Vec<&str> = s2
        .iter()
        .filter_map(|s| s["section_type"].as_str())
        .collect();
    assert_eq!(types_first, types_second, "section_type order must match");
}

// ── knowledge.list ────────────────────────────────────────────────────────────

#[tokio::test]
async fn list_atoms_returns_all_atoms() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({
            "atoms": [
                { "slug": "a1", "name": "Alpha", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" },
                { "slug": "a2", "name": "Beta", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" },
                { "slug": "a3", "name": "Gamma", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" },
            ]
        }),
    )
    .await
    .expect("upsert");

    let resp = f
        .dispatch("knowledge.list", json!({ "type": "atom" }))
        .await
        .expect("list ok");

    let results = resp["results"].as_array().expect("results array");
    assert_eq!(results.len(), 3);
    assert_eq!(resp["total"], 3);
}

#[tokio::test]
async fn list_domains_returns_only_domains() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({ "atoms": [{ "slug": "a1", "name": "Alpha", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
    )
    .await
    .expect("upsert atom");
    f.dispatch(
        "knowledge.upsert_domains",
        json!({ "domains": [{ "slug": "d1", "name": "Domain1", "description": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
    )
    .await
    .expect("upsert domain");

    let resp = f
        .dispatch("knowledge.list", json!({ "type": "domain" }))
        .await
        .expect("list domains ok");

    let results = resp["results"].as_array().expect("results array");
    assert_eq!(results.len(), 1);
    assert_eq!(results[0]["kind"], "domain");
}

#[tokio::test]
async fn list_respects_limit_and_offset() {
    let f = pack(rt());
    for i in 0..10 {
        f.dispatch(
            "knowledge.upsert_atoms",
            json!({ "atoms": [{ "slug": format!("a{i}"), "name": format!("Atom{i}"), "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
        )
        .await
        .expect("upsert");
    }

    let page1 = f
        .dispatch("knowledge.list", json!({ "limit": 3, "offset": 0 }))
        .await
        .expect("page1 ok");
    let page2 = f
        .dispatch("knowledge.list", json!({ "limit": 3, "offset": 3 }))
        .await
        .expect("page2 ok");

    let r1 = page1["results"].as_array().expect("r1");
    let r2 = page2["results"].as_array().expect("r2");
    assert_eq!(r1.len(), 3, "page1 should have 3 items");
    assert_eq!(r2.len(), 3, "page2 should have 3 items");
    assert_eq!(page1["total"], 10);
    // IDs on page1 and page2 should not overlap.
    let ids1: std::collections::HashSet<&str> =
        r1.iter().filter_map(|v| v["id"].as_str()).collect();
    let ids2: std::collections::HashSet<&str> =
        r2.iter().filter_map(|v| v["id"].as_str()).collect();
    assert!(
        ids1.is_disjoint(&ids2),
        "page1 and page2 ids must not overlap"
    );
}

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

#[tokio::test]
async fn delete_atoms_soft_deletes_by_slug() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({ "atoms": [{ "slug": "to-delete", "name": "Will be gone", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
    )
    .await
    .expect("upsert");

    let del_resp = f
        .dispatch("knowledge.delete_atoms", json!({ "ids": ["to-delete"] }))
        .await
        .expect("delete ok");

    assert_eq!(del_resp["deleted"], 1);

    // get should now return not-found.
    let err = f
        .dispatch("knowledge.get", json!({ "id": "to-delete" }))
        .await
        .unwrap_err();
    assert!(
        err.to_string().contains("not found") || err.to_string().contains("NotFound"),
        "expected not-found after delete, got: {err}"
    );
}

#[tokio::test]
async fn delete_atoms_returns_zero_for_unknown_slug() {
    let f = pack(rt());
    let resp = f
        .dispatch(
            "knowledge.delete_atoms",
            json!({ "ids": ["does-not-exist"] }),
        )
        .await
        .expect("delete ok even for missing");
    assert_eq!(resp["deleted"], 0);
}

// ── stats ──────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn stats_reflects_current_corpus() {
    let f = pack(rt());
    // Empty corpus.
    let empty = f
        .dispatch("knowledge.stats", json!({}))
        .await
        .expect("stats ok");
    assert_eq!(empty["total_atoms"], 0);
    assert_eq!(empty["total_domains"], 0);

    // Add atoms.
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({
            "atoms": [
                { "slug": "a1", "name": "Alpha", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity", "finalized": true },
                { "slug": "a2", "name": "Beta", "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity", "finalized": false },
            ]
        }),
    )
    .await
    .expect("upsert atoms");

    f.dispatch(
        "knowledge.upsert_domains",
        json!({ "domains": [{ "slug": "d1", "name": "Domain1", "description": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }] }),
    )
    .await
    .expect("upsert domain");

    let resp = f
        .dispatch("knowledge.stats", json!({}))
        .await
        .expect("stats ok 2");
    assert_eq!(resp["total_atoms"], 2);
    assert_eq!(resp["total_domains"], 1);
    // 1 of 2 atoms is finalized → eval_coverage = 0.5.
    let cov = resp["eval_coverage"].as_f64().expect("eval_coverage f64");
    assert!(
        (cov - 0.5).abs() < 1e-6,
        "expected eval_coverage=0.5, got {cov}"
    );
}

// ── fold ──────────────────────────────────────────────────────────────────────

#[tokio::test]
async fn fold_selects_within_budget() {
    let f = pack(rt());
    let resp = f
        .dispatch(
            "knowledge.fold",
            json!({
                "candidates": [
                    { "id": "c1", "score": 0.9, "size": 100 },
                    { "id": "c2", "score": 0.8, "size": 200 },
                    { "id": "c3", "score": 0.7, "size": 150 },
                    { "id": "c4", "score": 0.6, "size": 50 },
                ],
                "budget": 300
            }),
        )
        .await
        .expect("fold ok");

    let selected = resp["selected"].as_array().expect("selected array");
    let total_size = resp["total_size"].as_u64().expect("total_size");
    assert!(
        total_size <= 300,
        "total_size {total_size} must not exceed budget 300"
    );
    assert!(!selected.is_empty(), "at least one item should be selected");
    assert_eq!(resp["budget"], 300);
}

#[tokio::test]
async fn fold_empty_candidates_returns_empty_selection() {
    let f = pack(rt());
    let resp = f
        .dispatch(
            "knowledge.fold",
            json!({ "candidates": [], "budget": 1000 }),
        )
        .await
        .expect("fold empty ok");

    let selected = resp["selected"].as_array().expect("selected array");
    assert!(selected.is_empty());
    assert_eq!(resp["total_size"], 0);
}

#[tokio::test]
async fn fold_respects_min_score_filter() {
    let f = pack(rt());
    let resp = f
        .dispatch(
            "knowledge.fold",
            json!({
                "candidates": [
                    { "id": "high", "score": 0.9, "size": 100 },
                    { "id": "low",  "score": 0.2, "size": 100 },
                ],
                "budget": 10000,
                "min_score": 0.5
            }),
        )
        .await
        .expect("fold ok");

    let selected = resp["selected"].as_array().expect("selected");
    let ids: Vec<&str> = selected.iter().filter_map(|v| v["id"].as_str()).collect();
    assert!(
        ids.contains(&"high"),
        "high-score item should be selected: {ids:?}"
    );
    assert!(
        !ids.contains(&"low"),
        "low-score item should be filtered: {ids:?}"
    );
}

// ── knowledge.search ──────────────────────────────────────────────────────────

/// Seed 10 atoms with realistic content for search tests.
async fn seed_search_corpus(f: &Fixture) {
    let atoms = json!({
        "atoms": [
            { "slug": "rag",             "name": "RAG",               "content": "Retrieval-Augmented Generation combines retrieval with generation — covering concepts techniques algorithms implementations applications use cases and design patterns in detail RAG retrieves relevant passages before generating text dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity", "tags": ["retrieval", "rag"], "finalized": true },
            { "slug": "lora",            "name": "LoRA",              "content": "Low-Rank Adaptation of large language models — covering concepts techniques algorithms implementations applications use cases and design patterns in detail", "tags": ["fine-tuning", "adapter"], "finalized": true },
            { "slug": "flash-attention", "name": "FlashAttention",    "content": "Memory-efficient attention using tiling — covering concepts techniques algorithms implementations applications use cases and design patterns in detail — covering", "tags": ["attention", "gpu"], "finalized": true },
            { "slug": "gqa",             "name": "GQA",               "content": "Grouped Query Attention reduces KV cache — covering concepts techniques algorithms implementations applications use cases and design patterns in detail", "tags": ["attention", "inference"], "finalized": true },
            { "slug": "rope",            "name": "RoPE",              "content": "Rotary Position Embedding for transformers — covering concepts techniques algorithms implementations applications use cases and design patterns in detail —", "tags": ["embedding", "position"], "finalized": true },
            { "slug": "agent",           "name": "Agent",             "content": "Autonomous agent using LLM tool calls — covering concepts techniques algorithms implementations applications use cases and design patterns in detail", "tags": ["agent", "tool-use"], "finalized": true },
            { "slug": "chain-of-thought","name": "Chain-of-Thought",  "content": "Prompting technique for step-by-step reasoning — covering concepts techniques algorithms implementations applications use cases and design patterns in detail —", "tags": ["reasoning", "prompting"], "finalized": true },
            { "slug": "speculative",     "name": "Speculative Decoding", "content": "Draft model accelerates inference via speculation — covering concepts techniques algorithms implementations applications use cases and design patterns in detail", "tags": ["inference", "draft"], "finalized": true },
            { "slug": "quantization",    "name": "Quantization",     "content": "Reduce model size by lowering numerical precision — covering concepts techniques algorithms implementations applications use cases and design patterns in", "tags": ["compression", "inference"], "finalized": true },
            { "slug": "dpo",             "name": "DPO",               "content": "Direct Preference Optimization for RLHF alignment — covering concepts techniques algorithms implementations applications use cases and design patterns in detail", "tags": ["fine-tuning", "alignment"], "finalized": true },
        ]
    });
    f.dispatch("knowledge.upsert_atoms", atoms)
        .await
        .expect("seed atoms");
}

#[tokio::test]
async fn search_basic_returns_ranked_results() {
    let f = pack(rt());
    seed_search_corpus(&f).await;

    let resp = f
        .dispatch(
            "knowledge.search",
            json!({ "query": "retrieval generation", "rerank": false }),
        )
        .await
        .expect("search ok");

    let results = resp["results"].as_array().expect("results array");
    assert!(!results.is_empty(), "expected some results");

    // RAG should rank highly for "retrieval generation".
    let first_name = results[0]["name"].as_str().unwrap_or("");
    assert_eq!(
        first_name, "RAG",
        "RAG should rank first for 'retrieval generation', got: {results:?}"
    );
}

#[tokio::test]
async fn search_exact_name_bonus_surfaces_exact_match_first() {
    let f = pack(rt());
    seed_search_corpus(&f).await;

    let resp = f
        .dispatch(
            "knowledge.search",
            json!({ "query": "LoRA", "rerank": false }),
        )
        .await
        .expect("search ok");

    let results = resp["results"].as_array().expect("results array");
    assert!(!results.is_empty(), "expected results for LoRA");
    let first_name = results[0]["name"].as_str().unwrap_or("");
    assert_eq!(
        first_name, "LoRA",
        "exact name match LoRA should rank first"
    );
}

#[tokio::test]
async fn search_query_expansion_matches_related_form() {
    let f = pack(rt());
    // "agents" expands to "agent" via plural stripping.
    seed_search_corpus(&f).await;

    let resp = f
        .dispatch(
            "knowledge.search",
            json!({ "query": "agents", "rerank": false }),
        )
        .await
        .expect("search ok");

    let results = resp["results"].as_array().expect("results array");
    // Agent atom should appear in results.
    let names: Vec<&str> = results.iter().filter_map(|v| v["name"].as_str()).collect();
    assert!(
        names.contains(&"Agent"),
        "expected Agent in search results for 'agents', got: {names:?}"
    );
}

#[tokio::test]
async fn search_weight_override_changes_ranking() {
    let f = pack(rt());
    seed_search_corpus(&f).await;

    // With very high w_tags weight, the result tagged "attention" should rank first for "attention".
    let resp = f
        .dispatch(
            "knowledge.search",
            json!({
                "query": "attention",
                "weights": { "w_tags": 50.0, "w_name": 1.0, "w_content": 0.1 },
                "rerank": false
            }),
        )
        .await
        .expect("search ok with weights");

    let results = resp["results"].as_array().expect("results array");
    assert!(!results.is_empty(), "expected results");
    // FlashAttention or GQA have tag "attention".
    let first_name = results[0]["name"].as_str().unwrap_or("");
    assert!(
        first_name == "FlashAttention" || first_name == "GQA",
        "expected attention-tagged atom first, got: {first_name}"
    );
}

#[tokio::test]
async fn search_limit_is_respected() {
    let f = pack(rt());
    seed_search_corpus(&f).await;

    let resp = f
        .dispatch(
            "knowledge.search",
            json!({ "query": "inference", "limit": 2, "rerank": false }),
        )
        .await
        .expect("search ok");

    let results = resp["results"].as_array().expect("results array");
    assert!(
        results.len() <= 2,
        "expected at most 2 results, got {}",
        results.len()
    );
}

#[tokio::test]
async fn search_empty_corpus_returns_empty_results() {
    let f = pack(rt());
    // No atoms seeded.
    let resp = f
        .dispatch(
            "knowledge.search",
            json!({ "query": "anything", "rerank": false }),
        )
        .await
        .expect("search ok on empty corpus");

    let results = resp["results"].as_array().expect("results array");
    assert!(results.is_empty(), "empty corpus should return no results");
}

#[tokio::test]
async fn search_rejects_empty_query() {
    let f = pack(rt());
    let err = f
        .dispatch("knowledge.search", json!({ "query": "  " }))
        .await
        .unwrap_err();
    assert!(
        err.to_string().contains("query must not be empty"),
        "got: {err}"
    );
}

#[tokio::test]
async fn search_type_filter_returns_only_atoms() {
    let f = pack(rt());
    seed_search_corpus(&f).await;
    f.dispatch(
        "knowledge.upsert_domains",
        json!({ "domains": [{ "slug": "attention-domain", "name": "Attention Domain", "description": "covers attention methods — covering concepts techniques algorithms implementations applications use cases and design patterns in detail — covering concepts" }] }),
    )
    .await
    .expect("upsert domain");

    let resp = f
        .dispatch(
            "knowledge.search",
            json!({ "query": "attention", "type": "atom", "rerank": false }),
        )
        .await
        .expect("search filtered ok");

    let results = resp["results"].as_array().expect("results array");
    for r in results {
        assert_eq!(
            r["kind"].as_str().unwrap_or(""),
            "atom",
            "all results should be atoms when type=atom: {r}"
        );
    }
}

#[tokio::test]
async fn search_type_domain_finds_upserted_domains() {
    let f = pack(rt());
    f.dispatch(
        "knowledge.upsert_domains",
        json!({ "domains": [
            { "slug": "retrieval-methods", "name": "Retrieval Methods", "description": "Dense and sparse retrieval techniques — covering concepts techniques algorithms implementations applications use cases and design patterns in detail —" }
        ]}),
    )
    .await
    .expect("upsert domain");

    let resp = f
        .dispatch(
            "knowledge.search",
            json!({ "query": "retrieval", "type": "domain", "rerank": false }),
        )
        .await
        .expect("search domain ok");

    let results = resp["results"].as_array().expect("results array");
    assert!(
        !results.is_empty(),
        "domain search should find the upserted domain"
    );
    assert_eq!(results[0]["kind"].as_str().unwrap_or(""), "domain");
}

// ── suggest ───────────────────────────────────────────────────────────────────

#[tokio::test]
async fn suggest_returns_domains_for_query() {
    let f = pack(rt());

    f.dispatch(
        "knowledge.upsert_domains",
        json!({
            "domains": [
                { "slug": "retrieval-methods", "name": "Retrieval Methods", "description": "sparse and dense retrieval techniques — covering concepts techniques algorithms implementations applications use cases and design patterns in detail —" },
                { "slug": "embedding-theory", "name": "Embedding Theory", "description": "vector embedding concepts — covering concepts techniques algorithms implementations applications use cases and design patterns in detail — covering concepts" },
            ]
        }),
    )
    .await
    .expect("upsert domains");

    let resp = f
        .dispatch(
            "knowledge.suggest",
            json!({ "query": "retrieval techniques for dense and sparse methods" }),
        )
        .await
        .expect("suggest ok");

    let results = resp["results"].as_array().expect("results array");
    assert!(
        !results.is_empty(),
        "suggest should return at least one domain"
    );
    let first = &results[0];
    assert!(first["id"].is_string(), "result must have id");
    assert!(first["name"].is_string(), "result must have name");
    assert!(first["score"].is_number(), "result must have score");
}

#[tokio::test]
async fn suggest_rejects_empty_query() {
    let f = pack(rt());
    let err = f
        .dispatch("knowledge.suggest", json!({ "query": "" }))
        .await
        .expect_err("empty query should fail");
    assert!(
        matches!(err, khive_runtime::RuntimeError::InvalidInput(_)),
        "expected InvalidInput, got: {err:?}"
    );
}

// ── compose ───────────────────────────────────────────────────────────────────

#[tokio::test]
async fn compose_returns_markdown_for_atoms() {
    let f = pack(rt());

    f.dispatch(
        "knowledge.upsert_atoms",
        json!({
            "atoms": [
                {
                    "slug": "rag-overview",
                    "name": "RAG Overview",
                    "content": "Retrieval-augmented generation combines retrieval with generation. dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity"
                },
                {
                    "slug": "dense-retrieval",
                    "name": "Dense Retrieval",
                    "content": "Dense retrieval uses vector embeddings to find relevant documents. dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity"
                }
            ]
        }),
    )
    .await
    .expect("upsert atoms");

    let resp = f
        .dispatch(
            "knowledge.compose",
            json!({
                "atom_ids": ["rag-overview", "dense-retrieval"],
                "query": "retrieval augmented generation"
            }),
        )
        .await
        .expect("compose ok");

    let md = resp["data"]["markdown"].as_str().expect("markdown");
    assert!(
        md.contains("Knowledge Briefing"),
        "markdown must have heading"
    );
    let atoms = resp["data"]["atoms"].as_array().expect("atoms array");
    assert_eq!(atoms.len(), 2, "expected 2 atoms in response");
    let count = resp["data"]["count"].as_u64().expect("count");
    assert_eq!(count, 2);
}

#[tokio::test]
async fn compose_returns_markdown_for_domain() {
    let f = pack(rt());

    f.dispatch(
        "knowledge.upsert_atoms",
        json!({
            "atoms": [
                { "slug": "atom-a", "name": "Atom A", "content": "content of atom a dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" }
            ]
        }),
    )
    .await
    .expect("upsert atom");

    f.dispatch(
        "knowledge.upsert_domains",
        json!({
            "domains": [
                {
                    "slug": "test-domain",
                    "name": "Test Domain", "description": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity",
                    "members": ["atom-a"]
                }
            ]
        }),
    )
    .await
    .expect("upsert domain");

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

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

    let atoms = resp["data"]["atoms"].as_array().expect("atoms");
    assert!(
        !atoms.is_empty(),
        "compose from domain should include member atoms"
    );
}

#[tokio::test]
async fn compose_rejects_missing_ids() {
    let f = pack(rt());
    let err = f
        .dispatch("knowledge.compose", json!({ "query": "test" }))
        .await
        .expect_err("compose with no ids should fail");
    assert!(
        matches!(err, khive_runtime::RuntimeError::InvalidInput(_)),
        "expected InvalidInput, got: {err:?}"
    );
}

#[tokio::test]
async fn compose_rejects_empty_query() {
    let f = pack(rt());
    let err = f
        .dispatch(
            "knowledge.compose",
            json!({ "atom_ids": ["some-atom"], "query": "" }),
        )
        .await
        .expect_err("empty query should fail");
    assert!(
        matches!(err, khive_runtime::RuntimeError::InvalidInput(_)),
        "expected InvalidInput, got: {err:?}"
    );
}

#[tokio::test]
async fn suggest_returns_empty_when_no_domains_present() {
    let f = pack(rt());
    // Empty corpus: no domains upserted. suggest should succeed with an empty results array.
    let resp = f
        .dispatch(
            "knowledge.suggest",
            json!({ "query": "anything related to general knowledge retrieval methods" }),
        )
        .await
        .expect("suggest on empty corpus must not crash");
    let results = resp["results"].as_array().expect("results array");
    assert!(
        results.is_empty(),
        "no domains in corpus → empty results, got: {results:?}"
    );
}

#[tokio::test]
async fn suggest_honors_limit_param() {
    let f = pack(rt());

    f.dispatch(
        "knowledge.upsert_domains",
        json!({
            "domains": [
                { "slug": "domain-one", "name": "Domain One", "description": "first domain about retrieval — covering concepts techniques algorithms implementations applications use cases and design patterns in detail — covering" },
                { "slug": "domain-two", "name": "Domain Two", "description": "second domain about search — covering concepts techniques algorithms implementations applications use cases and design patterns in detail — covering" },
                { "slug": "domain-three", "name": "Domain Three", "description": "third domain about indexing — covering concepts techniques algorithms implementations applications use cases and design patterns in detail — covering" },
            ]
        }),
    )
    .await
    .expect("upsert domains");

    let resp = f
        .dispatch(
            "knowledge.suggest",
            json!({ "query": "domain retrieval search indexing methods and techniques", "limit": 1 }),
        )
        .await
        .expect("suggest with limit=1");

    let results = resp["results"].as_array().expect("results array");
    // All 3 seeded domains match the FTS phrase "domain"; suggest truncates to
    // exactly `limit` via hits.truncate(limit) before returning.
    assert_eq!(
        results.len(),
        1,
        "limit=1 with 3 matching domains must return exactly 1 result, got: {}",
        results.len()
    );
}

#[tokio::test]
async fn compose_accepts_mix_of_domain_ids_and_atom_ids() {
    let f = pack(rt());

    // Atom directly referenced by atom_ids.
    f.dispatch(
        "knowledge.upsert_atoms",
        json!({
            "atoms": [
                { "slug": "direct-atom", "name": "Direct Atom", "content": "directly specified atom content dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" },
                { "slug": "member-atom", "name": "Member Atom", "content": "member atom from domain dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity" },
            ]
        }),
    )
    .await
    .expect("upsert atoms");

    // Domain whose member provides member-atom.
    f.dispatch(
        "knowledge.upsert_domains",
        json!({
            "domains": [
                { "slug": "mix-domain", "name": "Mix Domain", "description": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity", "members": ["member-atom"] }
            ]
        }),
    )
    .await
    .expect("upsert domain");

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

    let resp = f
        .dispatch(
            "knowledge.compose",
            json!({
                "domain_ids": [domain_id],
                "atom_ids": ["direct-atom"],
                "query": "content"
            }),
        )
        .await
        .expect("compose with mix of domain_ids and atom_ids");

    let atoms = resp["data"]["atoms"].as_array().expect("atoms array");
    assert_eq!(
        atoms.len(),
        2,
        "compose with 1 domain member + 1 direct atom should yield 2 atoms (deduped), got: {atoms:?}"
    );
    let count = resp["data"]["count"].as_u64().expect("count");
    assert_eq!(count, 2);
}

// ── compose slim output (explain flag) ───────────────────────────────────────

#[tokio::test]
async fn compose_default_omits_sections_and_score_annotations() {
    let f = pack(rt());

    f.dispatch(
        "knowledge.upsert_atoms",
        json!({
            "atoms": [
                {
                    "slug": "slim-atom-a",
                    "name": "Slim Atom A",
                    "content": "retrieval augmented generation dense sparse corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity"
                }
            ]
        }),
    )
    .await
    .expect("upsert atom");

    let resp = f
        .dispatch(
            "knowledge.compose",
            json!({
                "atom_ids": ["slim-atom-a"],
                "query": "retrieval augmented generation"
            }),
        )
        .await
        .expect("compose default ok");

    let data = &resp["data"];
    assert!(
        data.get("sections").is_none(),
        "sections must be absent in default mode"
    );
    assert!(
        data.get("section_count").is_none(),
        "section_count must be absent in default mode"
    );

    let md = data["markdown"].as_str().expect("markdown");
    assert!(
        !md.contains("(score:"),
        "markdown must not contain (score: in default mode"
    );
    assert!(
        !md.contains("Score:"),
        "markdown must not contain Score: in default mode"
    );

    let atoms = data["atoms"].as_array().expect("atoms array");
    assert!(!atoms.is_empty(), "atoms must be present");
    let score_val = atoms[0]["score"].as_f64().expect("score is a number");
    let rendered = format!("{}", atoms[0]["score"]);
    let decimal_len = rendered
        .find('.')
        .map(|dot| rendered.len() - dot - 1)
        .unwrap_or(0);
    assert!(
        decimal_len <= 4,
        "atom score must serialize with at most 4 decimal places, got: {rendered}"
    );
    let _ = score_val;
}

#[tokio::test]
async fn compose_explain_true_atom_path_includes_score_in_markdown() {
    // This test uses a no-embedder runtime (rt()). Without an embedder,
    // embed_query() returns None, so section_results is always empty and
    // compose falls through to the atom-path markdown branch. The sole
    // assertion here is that explain=true causes "Score:" to appear in the
    // atom-path output. The section path (sections[] + breakdown + section_count
    // + "(score:") is exercised by the embedder-backed test in fixes.rs:
    // compose_explain_sections::compose_explain_true_section_path_is_exercised.
    let f = pack(rt());

    f.dispatch(
        "knowledge.upsert_atoms",
        json!({
            "atoms": [
                {
                    "slug": "explain-atom-b",
                    "name": "Explain Atom B",
                    "content": "retrieval augmented generation combines dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity"
                }
            ]
        }),
    )
    .await
    .expect("upsert atom");

    let resp = f
        .dispatch(
            "knowledge.compose",
            json!({
                "atom_ids": ["explain-atom-b"],
                "query": "retrieval augmented generation dense sparse",
                "explain": true
            }),
        )
        .await
        .expect("compose explain ok");

    let data = &resp["data"];
    let md = data["markdown"].as_str().expect("markdown");

    // Without an embedder, sections are never emitted — the atom-path branch
    // runs and renders "Score: X.XXXX" per atom when explain=true.
    assert!(
        data.get("sections").is_none(),
        "no-embedder runtime must not emit sections key"
    );
    assert!(
        md.contains("Score:"),
        "atom-path markdown must contain 'Score:' when explain=true, got: {md}"
    );
}

// ── KPK-002: DomainInput deny_unknown_fields + domain-mirror content-word minimum ──

#[tokio::test]
async fn kpk002_domain_input_rejects_unknown_fields() {
    let f = pack(rt());
    let err = f
        .dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [{
                    "slug": "test-domain",
                    "name": "Test Domain",
                    "description": "A domain with enough words to pass the twenty word minimum content requirement for testing.",
                    "unknown_field_xyz": "should cause rejection"
                }]
            }),
        )
        .await
        .unwrap_err();
    let msg = err.to_string();
    assert!(
        msg.contains("unknown_field_xyz") || msg.contains("unknown field"),
        "unknown field must be rejected; got: {msg}"
    );
}

#[tokio::test]
async fn kpk002_domain_mirror_atom_below_word_minimum_is_rejected() {
    let f = pack(rt());
    let err = f
        .dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [{
                    "slug": "sparse-domain",
                    "name": "Sparse Domain",
                    "description": "Too short"
                }]
            }),
        )
        .await
        .unwrap_err();
    let msg = err.to_string();
    assert!(
        msg.contains("20") || msg.contains("words") || msg.contains("content"),
        "description below 20-word minimum must be rejected; got: {msg}"
    );
}

#[tokio::test]
async fn kpk002_domain_mirror_atom_empty_description_is_rejected() {
    let f = pack(rt());
    let err = f
        .dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [{
                    "slug": "empty-desc-domain",
                    "name": "Empty Desc Domain"
                }]
            }),
        )
        .await
        .unwrap_err();
    let msg = err.to_string();
    assert!(
        msg.contains("20") || msg.contains("words") || msg.contains("content"),
        "missing description must be rejected as below 20-word minimum; got: {msg}"
    );
}

#[tokio::test]
async fn kpk002_domain_with_sufficient_description_is_accepted() {
    let f = pack(rt());
    let resp = f
        .dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [{
                    "slug": "rich-domain",
                    "name": "Rich Domain",
                    "description": "This domain covers retrieval augmented generation patterns for building scalable knowledge systems with structured graph storage and semantic search capabilities for AI agents.",
                    "tags": ["rag", "retrieval"],
                    "members": []
                }]
            }),
        )
        .await
        .expect("domain with sufficient description must be accepted");
    assert_eq!(resp["created"], json!(1u64));
    assert_eq!(resp["updated"], json!(0u64));
}

// ── Secret-gate regression tests ─────────────────────────────────────────────

fn is_secret_detected(err: &RuntimeError) -> bool {
    matches!(err, RuntimeError::SecretDetected(_))
}

/// knowledge.upsert_domains with a credential-shaped slug must be rejected.
#[tokio::test]
async fn upsert_domains_blocks_secret_in_slug_insert() {
    let f = pack(rt());
    let result = f
        .dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [{
                    "slug": "ghp_FakeGitHubToken0000000000000000000", // gitleaks:allow
                    "name": "Secret Slug Domain",
                    "description": "This domain describes retrieval augmented generation patterns for building scalable AI knowledge systems with structured graph storage and semantic search capabilities.",
                }]
            }),
        )
        .await;
    assert!(
        result.as_ref().err().is_some_and(is_secret_detected),
        "upsert_domains with secret in slug must be rejected; got: {result:?}"
    );
}

/// knowledge.upsert_domains with a clean slug must succeed.
#[tokio::test]
async fn upsert_domains_clean_slug_passes() {
    let f = pack(rt());
    let result = f
        .dispatch(
            "knowledge.upsert_domains",
            json!({
                "domains": [{
                    "slug": "clean-domain-slug",
                    "name": "Clean Domain",
                    "description": "This domain covers retrieval augmented generation patterns for AI knowledge systems at scale with hybrid search and graph traversal features.",
                }]
            }),
        )
        .await;
    assert!(
        result.is_ok(),
        "upsert_domains with clean slug must succeed; got: {result:?}"
    );
}

// ── resolver e2e tests (ADR-061): registry wired via PackRegistry::register_packs ────────

/// Build a VerbRegistry the same way the production MCP server does: via
/// `PackRegistry::register_packs`. This path calls `create_resolver` and wires
/// the knowledge `PackByIdResolver` into the registry, so generic `get` /
/// `delete` / `update` can reach knowledge-private tables.
fn pack_via_registry(rt: KhiveRuntime) -> Fixture {
    let mut builder = VerbRegistryBuilder::new();
    PackRegistry::register_packs(
        &["kg".to_string(), "knowledge".to_string()],
        rt.clone(),
        &mut builder,
    )
    .expect("register_packs must succeed for kg+knowledge");
    let registry = builder.build().expect("registry build");
    rt.install_edge_rules(registry.all_edge_rules());
    Fixture { registry }
}

/// Generic `get(id=<atom-uuid>)` via the resolver returns the same wire shape
/// as `knowledge.get(id=<slug>)`: tags is a JSON array, properties is a JSON
/// object (or null), and created_at/updated_at are ISO 8601 strings.
#[tokio::test]
async fn resolver_generic_get_atom_returns_public_wire_shape() {
    let f = pack_via_registry(rt());

    // Create an atom with tags and properties.
    let upsert = f
        .dispatch(
            "knowledge.upsert_atoms",
            json!({
                "atoms": [{
                    "slug": "resolver-atom-e2e",
                    "name": "Resolver Atom E2E",
                    "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity",
                    "tags": ["test", "resolver"],
                    "properties": { "source": "test" }
                }]
            }),
        )
        .await
        .expect("upsert atom");
    assert_eq!(upsert["created"], 1);

    // Fetch the UUID from knowledge.get by slug.
    let by_slug = f
        .dispatch("knowledge.get", json!({ "id": "resolver-atom-e2e" }))
        .await
        .expect("knowledge.get by slug");
    let uuid = by_slug["id"].as_str().expect("id string");

    // Now fetch via generic get using the UUID — this exercises the resolver path.
    let by_uuid = f
        .dispatch("get", json!({ "id": uuid }))
        .await
        .expect("generic get by uuid");

    // kind must be atom.
    assert_eq!(by_uuid["kind"], "atom", "wrong kind: {by_uuid}");
    assert_eq!(by_uuid["slug"], "resolver-atom-e2e");
    assert_eq!(by_uuid["name"], "Resolver Atom E2E");

    // tags must be a JSON array, not a comma-separated string.
    let tags = by_uuid["tags"]
        .as_array()
        .expect("tags must be a JSON array");
    assert!(
        tags.iter().any(|t| t == "test"),
        "expected 'test' tag, got: {tags:?}"
    );

    // created_at and updated_at must be ISO 8601 strings, not raw microsecond integers.
    let created_at = by_uuid["created_at"]
        .as_str()
        .expect("created_at must be a string");
    assert!(
        created_at.contains('T'),
        "created_at must be ISO 8601, got: {created_at:?}"
    );
    let updated_at = by_uuid["updated_at"]
        .as_str()
        .expect("updated_at must be a string");
    assert!(
        updated_at.contains('T'),
        "updated_at must be ISO 8601, got: {updated_at:?}"
    );

    // properties must be a JSON object (or null), not a string.
    assert!(
        by_uuid["properties"].is_object() || by_uuid["properties"].is_null(),
        "properties must be object or null, got: {:?}",
        by_uuid["properties"]
    );
}

/// Generic `get(id=<domain-uuid>)` via the resolver returns the public wire
/// shape: tags and members are JSON arrays, timestamps are ISO 8601 strings.
#[tokio::test]
async fn resolver_generic_get_domain_returns_public_wire_shape() {
    let f = pack_via_registry(rt());

    f.dispatch(
        "knowledge.upsert_domains",
        json!({
            "domains": [{
                "slug": "resolver-domain-e2e",
                "name": "Resolver Domain E2E",
                "description": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity tags members",
                "members": ["rag", "dense-retrieval"],
                "tags": ["test", "resolver"]
            }]
        }),
    )
    .await
    .expect("upsert domain");

    let by_slug = f
        .dispatch("knowledge.get", json!({ "id": "resolver-domain-e2e" }))
        .await
        .expect("knowledge.get domain by slug");
    let uuid = by_slug["id"].as_str().expect("id string");

    let by_uuid = f
        .dispatch("get", json!({ "id": uuid }))
        .await
        .expect("generic get domain by uuid");

    assert_eq!(by_uuid["kind"], "domain", "wrong kind: {by_uuid}");
    assert_eq!(by_uuid["slug"], "resolver-domain-e2e");

    // tags must be a JSON array, not a raw string.
    let tags = by_uuid["tags"]
        .as_array()
        .expect("tags must be a JSON array");
    assert!(
        tags.iter().any(|t| t == "test"),
        "expected 'test' tag, got: {tags:?}"
    );

    // members must be a JSON array, not a raw string.
    let members = by_uuid["members"]
        .as_array()
        .expect("members must be a JSON array");
    assert!(
        members.iter().any(|m| m == "rag"),
        "expected 'rag' member, got: {members:?}"
    );

    // timestamps must be ISO 8601 strings.
    let created_at = by_uuid["created_at"]
        .as_str()
        .expect("created_at must be a string");
    assert!(
        created_at.contains('T'),
        "created_at must be ISO 8601, got: {created_at:?}"
    );
}

/// Generic `delete(id=<domain-uuid>)` soft-deletes; subsequent generic
/// `get` returns NotFound.
#[tokio::test]
async fn resolver_generic_soft_delete_domain() {
    let f = pack_via_registry(rt());

    f.dispatch(
        "knowledge.upsert_domains",
        json!({
            "domains": [{
                "slug": "resolver-delete-domain",
                "name": "Resolver Delete Domain",
                "description": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity",
            }]
        }),
    )
    .await
    .expect("upsert domain");

    let by_slug = f
        .dispatch("knowledge.get", json!({ "id": "resolver-delete-domain" }))
        .await
        .expect("get domain before delete");
    let uuid = by_slug["id"].as_str().expect("id string").to_string();

    // Soft-delete via generic delete.
    let del = f
        .dispatch("delete", json!({ "id": uuid }))
        .await
        .expect("generic soft delete");
    assert_eq!(del["deleted"], true, "soft delete response: {del}");

    // Generic get must now return NotFound.
    let not_found = f.dispatch("get", json!({ "id": uuid })).await;
    assert!(
        matches!(not_found, Err(RuntimeError::NotFound(_))),
        "expected NotFound after soft delete, got: {not_found:?}"
    );
}

/// Generic `delete(id=<atom-uuid>, hard=true)` hard-deletes a live atom.
#[tokio::test]
async fn resolver_generic_hard_delete_atom() {
    let f = pack_via_registry(rt());

    f.dispatch(
        "knowledge.upsert_atoms",
        json!({
            "atoms": [{
                "slug": "resolver-hard-delete-atom",
                "name": "Resolver Hard Delete Atom",
                "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity"
            }]
        }),
    )
    .await
    .expect("upsert atom");

    let by_slug = f
        .dispatch(
            "knowledge.get",
            json!({ "id": "resolver-hard-delete-atom" }),
        )
        .await
        .expect("get atom before delete");
    let uuid = by_slug["id"].as_str().expect("id string").to_string();

    // Hard-delete the live atom directly.
    let hard_del = f
        .dispatch("delete", json!({ "id": uuid, "hard": true }))
        .await
        .expect("generic hard delete");
    assert_eq!(
        hard_del["deleted"], true,
        "hard delete response: {hard_del}"
    );

    // Generic get must now return NotFound.
    let not_found = f.dispatch("get", json!({ "id": uuid })).await;
    assert!(
        matches!(not_found, Err(RuntimeError::NotFound(_))),
        "expected NotFound after hard delete, got: {not_found:?}"
    );
}

/// Generic `update(id=<atom-uuid>)` returns InvalidInput because the knowledge
/// pack defers generic update (pack-private records require pack-specific verbs).
#[tokio::test]
async fn resolver_generic_update_atom_returns_invalid_input() {
    let f = pack_via_registry(rt());

    f.dispatch(
        "knowledge.upsert_atoms",
        json!({
            "atoms": [{
                "slug": "resolver-update-atom",
                "name": "Resolver Update Atom",
                "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity"
            }]
        }),
    )
    .await
    .expect("upsert atom");

    let by_slug = f
        .dispatch("knowledge.get", json!({ "id": "resolver-update-atom" }))
        .await
        .expect("get atom");
    let uuid = by_slug["id"].as_str().expect("id string");

    let err = f
        .dispatch("update", json!({ "id": uuid, "name": "New Name" }))
        .await
        .expect_err("update on knowledge atom must return an error");
    assert!(
        matches!(err, RuntimeError::InvalidInput(_)),
        "expected InvalidInput, got: {err:?}"
    );
}

/// Hard-delete an atom that has sections via `knowledge.edit`.
///
/// Without the fix this fails with `FOREIGN KEY constraint failed` because
/// `knowledge_sections` has a FK to `knowledge_atoms(id)` without `ON DELETE
/// CASCADE`.
#[tokio::test]
async fn resolver_generic_hard_delete_atom_with_sections() {
    let f = pack_via_registry(rt());

    f.dispatch(
        "knowledge.upsert_atoms",
        json!({
            "atoms": [{
                "slug": "hard-delete-atom-with-sections",
                "name": "Hard Delete Atom With Sections",
                "content": "dense sparse retrieval corpus benchmark search latency gradient descent transformer attention vector index nearest neighbor ranking fusion pipeline embedding rerank cosine similarity"
            }]
        }),
    )
    .await
    .expect("upsert atom");

    let by_slug = f
        .dispatch(
            "knowledge.get",
            json!({ "id": "hard-delete-atom-with-sections" }),
        )
        .await
        .expect("get atom before edit");
    let uuid = by_slug["id"].as_str().expect("id string").to_string();

    // Add sections to the atom.
    f.dispatch(
        "knowledge.edit",
        json!({
            "id": "hard-delete-atom-with-sections",
            "sections": [{
                "section_type": "overview",
                "content": "This section tests that hard-delete correctly removes dependent knowledge_sections rows before deleting the parent atom to satisfy the foreign key constraint."
            }]
        }),
    )
    .await
    .expect("add section to atom");

    // Hard-delete must succeed even though sections exist.
    let hard_del = f
        .dispatch("delete", json!({ "id": uuid, "hard": true }))
        .await
        .expect("hard delete atom with sections");
    assert_eq!(
        hard_del["deleted"], true,
        "hard delete response: {hard_del}"
    );

    // Generic get must now return NotFound.
    let not_found = f.dispatch("get", json!({ "id": uuid })).await;
    assert!(
        matches!(not_found, Err(RuntimeError::NotFound(_))),
        "expected NotFound after hard delete, got: {not_found:?}"
    );
}

/// Hard-delete a domain whose mirror atom has sections via `knowledge.edit`.
///
/// Without the fix the domain row is deleted first and then the mirror atom
/// delete fails with `FOREIGN KEY constraint failed`, leaving a partial delete.
#[tokio::test]
async fn resolver_generic_hard_delete_domain_with_mirror_sections() {
    let f = pack_via_registry(rt());

    f.dispatch(
        "knowledge.upsert_domains",
        json!({
            "domains": [{
                "slug": "hard-delete-domain-with-sections",
                "name": "Hard Delete Domain With Sections",
                "description": "Domain whose mirror atom will have sections before hard-delete to verify that cascade-delete of dependent knowledge_sections rows works correctly here."
            }]
        }),
    )
    .await
    .expect("upsert domain");

    let domain = f
        .dispatch(
            "knowledge.get",
            json!({ "id": "hard-delete-domain-with-sections" }),
        )
        .await
        .expect("get domain before edit");
    let domain_uuid = domain["id"].as_str().expect("id string").to_string();

    // Add sections to the mirror atom (same UUID as the domain).
    f.dispatch(
        "knowledge.edit",
        json!({
            "id": "hard-delete-domain-with-sections",
            "sections": [{
                "section_type": "overview",
                "content": "This section tests that hard-delete of a domain removes dependent knowledge_sections rows from the mirror atom before deleting the atom and domain rows."
            }]
        }),
    )
    .await
    .expect("add section to domain mirror atom");

    // Hard-delete the domain — must succeed even though mirror atom has sections.
    let hard_del = f
        .dispatch("delete", json!({ "id": domain_uuid, "hard": true }))
        .await
        .expect("hard delete domain with mirror sections");
    assert_eq!(
        hard_del["deleted"], true,
        "hard delete response: {hard_del}"
    );

    // Domain must now be NotFound.
    let domain_not_found = f.dispatch("get", json!({ "id": domain_uuid })).await;
    assert!(
        matches!(domain_not_found, Err(RuntimeError::NotFound(_))),
        "expected NotFound for domain after hard delete, got: {domain_not_found:?}"
    );
}