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
// See docs/operations.md#module-layout.
//! High-level operations composing storage capabilities into user-facing verbs.
//!
//! Fault-injection arming uses scoped guards only — see docs/operations.md#fault-injection-arm-migration.
use std::collections::HashMap;
use std::str::FromStr;
use chrono::Utc;
use serde::Serialize;
use uuid::Uuid;
#[cfg(test)]
use khive_score::DeterministicScore;
use khive_storage::entity::EntityTypeCounts;
use khive_storage::graph::{CommitAnnotationGuard, CommitAnnotationInsertOutcome};
#[cfg(test)]
use khive_storage::graph::{SymmetricEdgeUpdateOutcome, SymmetricEdgeUpdateRequest};
use khive_storage::note::Note;
#[cfg(test)]
use khive_storage::types::DeleteMode;
use khive_storage::types::{
DirectedNeighborHit, Direction, EdgeUpsertDisposition, EdgeUpsertRefusal, EdgeUpsertRequest,
EdgeUpsertResult, GraphPath, GuardedEdgeUpsertOutcome, LinkId, NeighborCursor, NeighborHit,
NeighborQuery, PageRequest, SqlStatement, SqlValue, TextFilter, TextQueryMode,
TextSearchRequest, TraversalRequest,
};
use khive_storage::{
Attachment, AttachmentSubstrate, Edge, EdgeRelation, Entity, EntityFilter, Event, EventFilter,
NewAttachment,
};
use khive_types::{EdgeEndpointRule, EndpointKind, EventKind, KhiveError, SubstrateKind};
use khive_db::stores::entity::entity_upsert_statement;
use khive_db::stores::graph::{
compose_graph_mutation_events, GraphMutationOutcome, GraphMutationPreconditions,
GraphMutationRequest,
};
use khive_db::stores::text::insert_document_statements;
#[cfg(test)]
mod batch_edge_tests;
struct EdgeReadWindow {
outcomes: Vec<Option<RuntimeResult<Option<Edge>>>>,
groups: Vec<(khive_types::Namespace, Vec<usize>)>,
}
/// The restore unit committed the row and its text index; only the
/// post-commit embedding rebuild failed. Name that, so the caller does not
/// read an ordinary restore failure over a record that is already live.
fn restore_reindex_failed(kind: &str, id: Uuid, error: RuntimeError) -> RuntimeError {
RuntimeError::Internal(format!(
"{kind} {id} is restored and text-indexed, but its embedding rebuild failed \
and will be retried by the next reindex: {error}"
))
}
fn merge_tombstone_restore_refused(id: Uuid, kept_id: impl std::fmt::Display) -> RuntimeError {
KhiveError::conflict(format!(
"merge_tombstone: {id} was merged into {kept_id}; a merge tombstone is not restorable, query the kept id"
))
.with_details(khive_types::Details::new_owned([
("reason", "merge_tombstone".into()),
("merged_into", kept_id.to_string()),
]))
.into()
}
/// The entity total and optional type report consumed together by `stats`.
#[derive(Debug, PartialEq, Eq)]
pub struct EntityStatsCounts {
pub entities: u64,
pub entities_by_type: Option<EntityTypeCounts>,
}
/// The live edge total and its two breakdowns consumed together by `stats`.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct EdgeStatsCounts {
pub edges: u64,
pub edges_by_relation: HashMap<String, u64>,
pub edges_by_endpoint_base: khive_storage::types::EdgeEndpointBaseCounts,
}
/// Count caller-visible live entities through one store. A supported breakdown
/// supplies its own scalar total; only an unavailable report uses legacy counting.
pub async fn entity_stats_counts(
store: &dyn khive_storage::EntityStore,
token: &NamespaceToken,
) -> RuntimeResult<EntityStatsCounts> {
let namespaces: Vec<String> = token
.visible_namespaces()
.iter()
.map(|namespace| namespace.as_str().to_owned())
.collect();
match store.count_entities_by_type(&namespaces).await? {
Some(groups) => {
let entities = groups.iter().try_fold(0_u64, |total, (_, count)| {
total.checked_add(*count).ok_or_else(|| {
RuntimeError::Internal(
"entity type counts exceed the scalar count range".into(),
)
})
})?;
Ok(EntityStatsCounts {
entities,
entities_by_type: Some(groups),
})
}
None => {
let entities = store
.count_entities(
token.namespace().as_str(),
EntityFilter {
namespaces,
..EntityFilter::default()
},
)
.await?;
Ok(EntityStatsCounts {
entities,
entities_by_type: None,
})
}
}
}
/// Inputs for a store-owned entity identity. Unlike ordinary creation, a
/// competing insert must leave the existing row untouched.
pub struct EntityClaimSpec {
pub id: Uuid,
pub kind: String,
pub entity_type: Option<String>,
pub name: String,
pub description: Option<String>,
pub properties: Option<serde_json::Value>,
pub tags: Vec<String>,
pub identity_tag: String,
}
fn live_merged_entity_refused(id: Uuid, kept_id: impl std::fmt::Display) -> RuntimeError {
KhiveError::conflict(format!(
"live_merged_entity: {id} is live but still carries merged_into {kept_id}; a row an \
earlier restore left live over its merge is not restorable, re-tombstone it or query the \
kept id"
))
.with_details(khive_types::Details::new_owned([
("reason", "live_merged_entity".into()),
("merged_into", kept_id.to_string()),
]))
.into()
}
fn restore_key_conflict(key: &str, holder: &Note) -> RuntimeError {
KhiveError::conflict(format!(
"restore_key_conflict: key {key:?} is already held by live note {}",
holder.id
))
.with_details(khive_types::Details::new_owned([
("reason", "restore_key_conflict".into()),
("key", key.to_owned()),
("existing_id", holder.id.to_string()),
]))
.into()
}
use crate::atomic_plan::{
AddEntityPlan, AffectedRowGuard, PlanStatement, PostCommitEffect, UpdatePlan,
};
use crate::atomic_runner::{run_atomic_unit, AtomicOpFailure, AtomicOpPlan, AtomicRunOutcome};
use crate::curation::{entity_fts_document, note_embedding_text_ref, note_fts_document};
use crate::error::{GuardedWriteFailure, RuntimeError, RuntimeResult};
use crate::runtime::{KhiveRuntime, NamespaceToken};
/// A non-retryable failure after a substrate mutation has committed.
/// Callers can report the committed id/result and this diagnostic together.
#[derive(Clone, Debug, Serialize)]
pub struct PostCommitDegradation {
pub stage: &'static str,
pub error: String,
}
impl PostCommitDegradation {
fn new(stage: &'static str, error: impl ToString) -> Self {
Self {
stage,
error: error.to_string(),
}
}
}
// One list declares the stages, so the variants, `ALL` and the labels cannot drift apart.
macro_rules! conditional_insert_stages {
($($stage:ident => $label:literal),+ $(,)?) => {
/// The post-commit stages a conditional note insert reports in its committed
/// `post_commit_degraded` outcome. The comm ingest decoder accepts a stage only
/// when it is the label of a member of [`ConditionalInsertStage::ALL`], and the
/// insert can record a stage only through this type, so every label the
/// runtime emits is one the decoder accepts.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ConditionalInsertStage {
$($stage,)+
}
impl ConditionalInsertStage {
/// Every stage, generated from the same list as the variants.
pub const ALL: &'static [Self] = &[$(Self::$stage,)+];
pub const fn label(self) -> &'static str {
match self {
$(Self::$stage => $label,)+
}
}
}
};
}
conditional_insert_stages! {
FtsAcquisition => "fts_acquisition",
FtsUpsert => "fts_upsert",
Embedding => "embedding",
VectorAcquisition => "vector_acquisition",
VectorInsert => "vector_insert",
}
impl ConditionalInsertStage {
pub fn from_label(label: &str) -> Option<Self> {
Self::ALL
.iter()
.copied()
.find(|stage| stage.label() == label)
}
}
fn record_conditional_insert_degradation(
degradations: &mut Vec<PostCommitDegradation>,
id: Uuid,
stage: ConditionalInsertStage,
error: impl ToString,
) {
record_post_commit_degradation(degradations, "try_create_note", id, stage.label(), error);
}
fn record_post_commit_degradation(
degradations: &mut Vec<PostCommitDegradation>,
operation: &'static str,
id: Uuid,
stage: &'static str,
error: impl ToString,
) {
let degradation = PostCommitDegradation::new(stage, error);
tracing::warn!(%operation, %id, stage, error = %degradation.error,
"substrate mutation committed with post-commit degradation");
degradations.push(degradation);
}
/// Legacy methods cannot return both their original result and the new report.
/// Give direct callers a typed, non-retryable error with the committed record
/// handle and every failed stage instead of silently dropping the report.
fn legacy_post_commit_result<T>(
operation: &'static str,
id: Uuid,
value: T,
degradations: Vec<PostCommitDegradation>,
) -> RuntimeResult<T> {
if degradations.is_empty() {
return Ok(value);
}
let failures = serde_json::Value::Array(
degradations
.iter()
.map(|failure| {
serde_json::json!({"stage": failure.stage, "error": failure.error.as_str()})
})
.collect(),
);
Err(KhiveError::internal(format!(
"{operation} committed record {id}, but post-commit work failed; do not retry the mutation; reconcile by record_id"
))
.with_details(khive_types::Details::new_owned([
("reason", "post_commit_degraded".to_string()),
("operation", operation.to_string()),
("record_id", id.to_string()),
("committed", "true".to_string()),
("retryable", "false".to_string()),
("post_commit_degradations", failures.to_string()),
]))
.into())
}
pub(crate) fn legacy_post_commit_result_with_embedding<T>(
operation: &'static str,
id: Uuid,
value: T,
embedding: crate::retrieval::EmbeddingTruncationReport,
degradations: Vec<PostCommitDegradation>,
) -> RuntimeResult<T> {
if !embedding.any_truncated() {
return legacy_post_commit_result(operation, id, value, degradations);
}
let failures = serde_json::Value::Array(
degradations
.iter()
.map(|failure| {
serde_json::json!({"stage": failure.stage, "error": failure.error.as_str()})
})
.collect(),
);
Err(KhiveError::internal(format!(
"{operation} committed record {id}, but embedding input was truncated; do not retry the mutation; reconcile by record_id"
))
.with_details(khive_types::Details::new_owned([
("reason", "embedding_input_truncated".to_string()),
("operation", operation.to_string()),
("record_id", id.to_string()),
("committed", "true".to_string()),
("retryable", "false".to_string()),
(
"embedding_truncation_report",
serde_json::json!(embedding).to_string(),
),
("post_commit_degradations", failures.to_string()),
]))
.into())
}
mod entity_bulk;
mod entity_create;
mod entity_indexes;
#[cfg(any(test, feature = "fault-injection"))]
mod fault_injection;
mod resolve_prefix_in;
mod resolve_uuid_or_prefix;
mod delete_restore;
mod edge_query;
mod graph_rules;
mod identity_reads;
mod link_ops;
mod read_helpers;
mod record_ops;
mod traversal;
use read_helpers::{
direction_sort_rank, drain_embed_join_set, normalize_symmetric_direction, note_snippet,
note_title, resolve_prefix_statement, salience_weighted_rank,
};
pub use read_helpers::{
hex_prefix_to_uuid_pattern, uuid_prefix_bounds, NoteSearchHit, NoteSearchOutcome, Resolved,
};
pub use record_ops::NoteCreateSpec;
pub use edge_query::QueryResult;
use graph_rules::{
accepted_entity_relations_description, pack_rule_allows,
static_impossible_edge_pattern_warnings,
};
pub use graph_rules::{
accepted_entity_relations_for_entities, accepted_pack_relations_for_entities,
merge_entry_metadata,
};
pub(crate) use graph_rules::{
canonical_edge_endpoint_kinds, canonical_edge_endpoints, merge_dependency_kind,
validate_edge_metadata, validate_edge_weight,
};
#[cfg(test)]
use link_ops::guarded_link_batch_failure;
#[cfg(test)]
use traversal::merge_traversal_paths_by_root;
#[cfg(any(test, feature = "fault-injection"))]
pub use fault_injection::{
arm_entity_compensation_fail_scoped, arm_fts_fail_many_partial_scoped,
arm_fts_fail_many_scoped, arm_fts_fail_scoped, arm_fts_search_fail,
arm_prefix_resolve_fail_scoped, arm_rollback_cleanup_fail, arm_vector_fail_after,
arm_vector_fail_scoped, FaultInjectionArm,
};
#[cfg(test)]
use fault_injection::{arm_fault, FaultArmSet, LINK_FAIL_AFTER};
#[cfg(any(test, feature = "fault-injection"))]
use fault_injection::{
consume_fault, ENTITY_COMPENSATION_FAIL_NS, FTS_FAIL_MANY_NS, FTS_FAIL_MANY_PARTIAL_NS,
FTS_FAIL_NS, FTS_SEARCH_FAIL_NS, PREFIX_RESOLVE_FAIL_NS, ROLLBACK_CLEANUP_FAIL_NS,
VECTOR_FAIL_AFTER, VECTOR_FAIL_NS,
};
#[cfg(any(test, feature = "fault-injection"))]
pub(crate) use fault_injection::{consume_fts_fail_fault, consume_vector_fail_fault};
pub use resolve_prefix_in::PrefixScope;
/// A by-ID edge-endpoint substrate kind, including `Edge` itself.
///
/// Unlike [`Resolved`], this carries no record data — it is used where only
/// the substrate classification is needed (coordinator locate/link parity
/// with `get`, ADR-002 rule 1: `annotates` target may be entity, note, edge,
/// or event).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EdgeEndpointKind {
Entity,
Note,
Event,
Edge,
}
impl EdgeEndpointKind {
/// Wire name carried by a link lifecycle event for this endpoint.
pub const fn name(self) -> &'static str {
match self {
Self::Entity => "entity",
Self::Note => "note",
Self::Event => "event",
Self::Edge => "edge",
}
}
}
/// Map a resolved endpoint to its `(substrate, kind, entity_type)` triple, or
/// `None` if the substrate is not a valid edge endpoint (events, edges).
///
/// `entity_type` carries the pack-owned granular subtype (`Entity::entity_type`,
/// e.g. `"theorem"`); it is `None` for notes and for entities with no subtype.
fn resolved_pair(r: Option<&Resolved>) -> Option<(&'static str, &str, Option<&str>)> {
match r? {
Resolved::Entity(e) => Some(("entity", e.kind.as_str(), e.entity_type.as_deref())),
Resolved::Note(n) => Some(("note", n.kind.as_str(), None)),
Resolved::Event(_) => None,
Resolved::PackRecord { .. } => None,
}
}
/// `true` if `spec` matches the given substrate + kind + entity_type triple.
///
/// Pure and DB-free — exposed so offline consumers (e.g. `kkernel kg
/// validate`, which parses `(substrate, kind, entity_type)` straight out of
/// NDJSON with no live record to resolve) can apply the exact same
/// `EdgeEndpointRule` matching semantics `pack_rule_allows` uses internally,
/// instead of re-deriving a parallel matcher that could drift out of sync.
pub fn endpoint_matches(
spec: &EndpointKind,
substrate: &str,
kind: &str,
entity_type: Option<&str>,
) -> bool {
match spec {
EndpointKind::EntityOfKind(k) => substrate == "entity" && *k == kind,
EndpointKind::NoteOfKind(k) => substrate == "note" && *k == kind,
EndpointKind::EntityOfType {
kind: k,
entity_type: t,
} => substrate == "entity" && *k == kind && entity_type == Some(*t),
}
}
/// Base entity endpoint allowlist — the closed set of permitted entity→entity
/// relation triples.
///
/// Each entry `(src_kind, relation, tgt_kind)` explicitly allows that combination.
/// `"*"` as `src_kind` means "any entity kind" (used by `instance_of` whose source
/// is unrestricted).
///
/// Pack rules (via `EDGE_RULES`) are additive — they cannot remove rows here.
/// Exposed via `base_entity_endpoint_rules()` for the ADR-076 certificate tests.
pub const BASE_ENTITY_ENDPOINT_RULES: &[(&str, EdgeRelation, &str)] = &[
// Structure
("concept", EdgeRelation::Contains, "concept"),
("project", EdgeRelation::Contains, "project"),
("project", EdgeRelation::Contains, "artifact"),
("org", EdgeRelation::Contains, "project"),
("org", EdgeRelation::Contains, "service"),
("concept", EdgeRelation::PartOf, "concept"),
("project", EdgeRelation::PartOf, "project"),
("project", EdgeRelation::PartOf, "org"),
("*", EdgeRelation::InstanceOf, "concept"),
("service", EdgeRelation::InstanceOf, "project"),
// ADR-002 amendment (ADR-191): web hyperlink — a document points at
// another document it links to. No qualifier inference (unlike
// depends_on); the endpoint pair is intentionally narrow (document only,
// no service/concept targets — see ADR-191 D2/F10).
("document", EdgeRelation::LinksTo, "document"),
// ADR-196 location, amended 2026-10-05: the source occupies or is manifested in the target
// without being a constituent of it; base rows for concept and org, packs narrow the rest.
("concept", EdgeRelation::LocatedIn, "concept"),
("org", EdgeRelation::LocatedIn, "concept"),
// Ownership
("person", EdgeRelation::Owns, "org"),
("org", EdgeRelation::Owns, "org"),
// Derivation
("concept", EdgeRelation::Extends, "concept"),
("concept", EdgeRelation::VariantOf, "concept"),
("artifact", EdgeRelation::VariantOf, "artifact"),
("concept", EdgeRelation::IntroducedBy, "document"),
("concept", EdgeRelation::IntroducedBy, "person"),
("artifact", EdgeRelation::IntroducedBy, "document"),
("project", EdgeRelation::IntroducedBy, "document"),
// ADR-002 amendment (ADR-167): service provenance — the document that
// introduced a service (its ADR or design record).
("service", EdgeRelation::IntroducedBy, "document"),
("document", EdgeRelation::IntroducedBy, "person"),
("document", EdgeRelation::IntroducedBy, "org"),
("concept", EdgeRelation::IntroducedBy, "org"),
// Provenance
("artifact", EdgeRelation::DerivedFrom, "dataset"),
("artifact", EdgeRelation::DerivedFrom, "document"),
("artifact", EdgeRelation::DerivedFrom, "project"),
("artifact", EdgeRelation::DerivedFrom, "artifact"),
// ADR-002 amendment 2026-07-27: publication provenance — a curated or
// filtered publication copy points at the canonical source document.
("document", EdgeRelation::DerivedFrom, "document"),
// Temporal
("document", EdgeRelation::Precedes, "document"),
("dataset", EdgeRelation::Precedes, "dataset"),
("artifact", EdgeRelation::Precedes, "artifact"),
("service", EdgeRelation::Precedes, "service"),
("project", EdgeRelation::Precedes, "project"),
// Dependency
("project", EdgeRelation::DependsOn, "project"),
("service", EdgeRelation::DependsOn, "project"),
("service", EdgeRelation::DependsOn, "service"),
("service", EdgeRelation::DependsOn, "artifact"),
("service", EdgeRelation::DependsOn, "dataset"),
("artifact", EdgeRelation::DependsOn, "project"),
("artifact", EdgeRelation::DependsOn, "service"),
("document", EdgeRelation::DependsOn, "document"),
("concept", EdgeRelation::Enables, "concept"),
("service", EdgeRelation::Enables, "concept"),
("dataset", EdgeRelation::Enables, "concept"),
// Implementation
("project", EdgeRelation::Implements, "concept"),
("service", EdgeRelation::Implements, "concept"),
// Lateral
("concept", EdgeRelation::CompetesWith, "concept"),
("project", EdgeRelation::CompetesWith, "project"),
("service", EdgeRelation::CompetesWith, "service"),
("org", EdgeRelation::CompetesWith, "org"),
("concept", EdgeRelation::ComposedWith, "concept"),
("project", EdgeRelation::ComposedWith, "project"),
// Versioning (Supersedes — Concept/Document/Artifact/Service/Dataset only)
("concept", EdgeRelation::Supersedes, "concept"),
("document", EdgeRelation::Supersedes, "document"),
("artifact", EdgeRelation::Supersedes, "artifact"),
("service", EdgeRelation::Supersedes, "service"),
("dataset", EdgeRelation::Supersedes, "dataset"),
// Epistemic (Supports/Refutes — evidence sources → Concept claim only)
("concept", EdgeRelation::Supports, "concept"),
("document", EdgeRelation::Supports, "concept"),
("dataset", EdgeRelation::Supports, "concept"),
("artifact", EdgeRelation::Supports, "concept"),
("concept", EdgeRelation::Refutes, "concept"),
("document", EdgeRelation::Refutes, "concept"),
("dataset", EdgeRelation::Refutes, "concept"),
("artifact", EdgeRelation::Refutes, "concept"),
];
/// Returns the base entity endpoint allowlist.
///
/// The returned slice is the same data that `base_entity_rule_allows` consults at
/// runtime. Exposed for the ADR-076 certificate tests in `khive-pack-kg`, which
/// must audit live rules rather than hand-copied snapshots.
pub fn base_entity_endpoint_rules() -> &'static [(&'static str, EdgeRelation, &'static str)] {
BASE_ENTITY_ENDPOINT_RULES
}
/// `true` if `(src_kind, relation, tgt_kind)` is in the base entity endpoint
/// allowlist. Pure and DB-free — exposed alongside [`base_entity_endpoint_rules`]
/// so offline consumers (e.g. `kkernel kg validate`) can apply the exact same
/// base-table membership test the live validator uses, instead of re-deriving
/// a parallel `.any()` predicate over a hand-copied allowlist.
pub fn base_entity_rule_allows(src_kind: &str, relation: EdgeRelation, tgt_kind: &str) -> bool {
BASE_ENTITY_ENDPOINT_RULES.iter().any(|(src, rel, tgt)| {
*rel == relation && (*src == "*" || *src == src_kind) && *tgt == tgt_kind
})
}
impl KhiveRuntime {
// ---- Note operations ----
/// Create and persist a note, optionally with properties and annotation targets.
///
/// After creating the note:
/// - Always indexes into FTS5 at the `notes_<namespace>` key.
/// - If an embedding model is configured, indexes into the vector store with
/// `SubstrateKind::Note`.
/// - For each UUID in `annotates`, creates an `EdgeRelation::Annotates` edge from
/// the note to that target.
///
/// A bounded embedding returns a non-retryable error carrying the committed
/// note ID and truncation report. The report-aware embedding-content variant
/// accepts `None` to retain the same default input selection.
// REASON: note creation requires kind, name, content, salience, properties, annotates,
// and namespace token — mirrors the MCP verb surface; a builder would not reduce
// caller complexity for pack handler callers.
#[allow(clippy::too_many_arguments)]
pub async fn create_note(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
salience: Option<f64>,
properties: Option<serde_json::Value>,
annotates: Vec<Uuid>,
) -> RuntimeResult<Note> {
let (note, embedding, degradations, _) = self
.create_note_inner(
token, kind, name, content, None, salience, None, properties, annotates, None,
false, false,
)
.await?;
legacy_post_commit_result_with_embedding(
"create_note",
note.id,
note,
embedding,
degradations,
)
}
/// Publish a network receipt with provenance that generic note writes
/// cannot supply. The web pack provides only the request record and the
/// annotation targets; this entry point fixes the note kind, tag, and
/// provenance before the first storage write.
pub async fn create_web_receipt_note(
&self,
token: &NamespaceToken,
summary: &str,
request: serde_json::Value,
annotates: Vec<Uuid>,
) -> RuntimeResult<Note> {
let properties = serde_json::json!({
"tags": ["web.receipt"],
"request": request,
});
let (note, _, degradations, _) = self
.create_note_inner(
token,
"observation",
None,
summary,
None,
None,
None,
Some(properties),
annotates,
None,
false,
true,
)
.await?;
legacy_post_commit_result("create_web_receipt_note", note.id, note, degradations)
}
/// Like [`Self::create_note`], but lets the caller supply a smaller text
/// to send to the vector embedder while the note's stored/FTS-indexed
/// `content` remains the full text.
///
/// `embedding_content`, when `Some`, must be non-empty and a proper
/// prefix of `content` — anything else is rejected with `InvalidInput`
/// before any write. `None` behaves exactly like [`Self::create_note`].
/// Use this when `content` may exceed an embedder's input cap (e.g. a
/// very long commit message) and only a capped head prefix should be
/// embedded, while the full text is still stored and searchable via FTS.
#[allow(clippy::too_many_arguments)]
pub async fn create_note_with_embedding_content(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
embedding_content: Option<&str>,
salience: Option<f64>,
properties: Option<serde_json::Value>,
annotates: Vec<Uuid>,
) -> RuntimeResult<Note> {
let (note, embedding, degradations, _) = self
.create_note_inner(
token,
kind,
name,
content,
embedding_content,
salience,
None,
properties,
annotates,
None,
false,
false,
)
.await?;
legacy_post_commit_result_with_embedding(
"create_note_with_embedding_content",
note.id,
note,
embedding,
degradations,
)
}
#[allow(clippy::too_many_arguments)]
pub async fn create_note_with_embedding_content_and_report(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
embedding_content: Option<&str>,
salience: Option<f64>,
properties: Option<serde_json::Value>,
annotates: Vec<Uuid>,
) -> RuntimeResult<(Note, crate::retrieval::EmbeddingTruncationReport)> {
let (note, embedding, degradations, _) = self
.create_note_inner(
token,
kind,
name,
content,
embedding_content,
salience,
None,
properties,
annotates,
None,
false,
false,
)
.await?;
legacy_post_commit_result(
"create_note_with_embedding_content_and_report",
note.id,
(note, embedding),
degradations,
)
}
/// The committed note and its non-retryable post-commit diagnostics.
#[allow(clippy::too_many_arguments)]
pub async fn create_note_with_embedding_content_and_post_commit_report(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
embedding_content: Option<&str>,
salience: Option<f64>,
properties: Option<serde_json::Value>,
annotates: Vec<Uuid>,
) -> RuntimeResult<(
Note,
crate::retrieval::EmbeddingTruncationReport,
Vec<PostCommitDegradation>,
)> {
let (note, embedding, degradations, _) = self
.create_note_inner(
token,
kind,
name,
content,
embedding_content,
salience,
None,
properties,
annotates,
None,
false,
false,
)
.await?;
Ok((note, embedding, degradations))
}
/// Like [`Self::create_note`] but also sets a non-zero decay factor on the note.
// REASON: extends create_note with an additional decay_factor parameter; same
// rationale — mirrors the MCP surface and reduces an extra builder layer.
#[allow(clippy::too_many_arguments)]
pub async fn create_note_with_decay(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
salience: Option<f64>,
decay_factor: f64,
properties: Option<serde_json::Value>,
annotates: Vec<Uuid>,
) -> RuntimeResult<Note> {
self.create_note_with_decay_for_embedding_model(
token,
kind,
name,
content,
salience,
decay_factor,
properties,
annotates,
None,
)
.await
}
/// Create a note with decay and retain embedding truncation accounting.
#[allow(clippy::too_many_arguments)]
pub async fn create_note_with_decay_and_report(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
salience: Option<f64>,
decay_factor: f64,
properties: Option<serde_json::Value>,
annotates: Vec<Uuid>,
) -> RuntimeResult<(Note, crate::retrieval::EmbeddingTruncationReport)> {
self.create_note_with_decay_for_embedding_model_and_report(
token,
kind,
name,
content,
salience,
decay_factor,
properties,
annotates,
None,
)
.await
}
/// Like [`Self::create_note_with_decay`] but targets a specific embedding model.
// REASON: adds an embedding_model parameter to the decay variant; the full parameter
// set is required for correct MCP verb routing and cannot be collapsed without
// introducing a separate config struct that would obscure call sites.
#[allow(clippy::too_many_arguments)]
pub async fn create_note_with_decay_for_embedding_model(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
salience: Option<f64>,
decay_factor: f64,
properties: Option<serde_json::Value>,
annotates: Vec<Uuid>,
embedding_model: Option<&str>,
) -> RuntimeResult<Note> {
let (note, embedding, degradations, _) = self
.create_note_inner(
token,
kind,
name,
content,
None,
salience,
Some(decay_factor),
properties,
annotates,
embedding_model,
false,
false,
)
.await?;
legacy_post_commit_result_with_embedding(
"create_note_with_decay_for_embedding_model",
note.id,
note,
embedding,
degradations,
)
}
/// Create a note with decay for a named model and retain embedding truncation accounting.
#[allow(clippy::too_many_arguments)]
pub async fn create_note_with_decay_for_embedding_model_and_report(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
salience: Option<f64>,
decay_factor: f64,
properties: Option<serde_json::Value>,
annotates: Vec<Uuid>,
embedding_model: Option<&str>,
) -> RuntimeResult<(Note, crate::retrieval::EmbeddingTruncationReport)> {
let (note, embedding, degradations, _) = self
.create_note_inner(
token,
kind,
name,
content,
None,
salience,
Some(decay_factor),
properties,
annotates,
embedding_model,
false,
false,
)
.await?;
legacy_post_commit_result(
"create_note_with_decay_for_embedding_model_and_report",
note.id,
(note, embedding),
degradations,
)
}
/// Memory-pack receipt form of the decay create. Each returned sequence
/// was read inside the transaction that inserted that model's vector and
/// ANN upsert log row; a failed or version-rejected vector writes no fence.
#[allow(clippy::too_many_arguments)]
pub async fn create_note_with_decay_for_embedding_model_with_visibility(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
salience: Option<f64>,
decay_factor: f64,
properties: Option<serde_json::Value>,
annotates: Vec<Uuid>,
embedding_model: Option<&str>,
) -> RuntimeResult<(Note, Vec<(String, u64)>)> {
let (note, _, degradations, fences) = self
.create_note_inner(
token,
kind,
name,
content,
None,
salience,
Some(decay_factor),
properties,
annotates,
embedding_model,
true,
false,
)
.await?;
legacy_post_commit_result(
"create_note_with_decay_for_embedding_model_with_visibility",
note.id,
(note, fences),
degradations,
)
}
/// Memory-pack create returning both its committed vector fences and
/// embedding-input truncation accounting. Other post-commit degradations
/// retain the same non-retryable error behavior as the receipt-only API.
#[allow(clippy::too_many_arguments)]
pub async fn create_note_with_decay_for_embedding_model_with_visibility_and_report(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
salience: Option<f64>,
decay_factor: f64,
properties: Option<serde_json::Value>,
annotates: Vec<Uuid>,
embedding_model: Option<&str>,
) -> RuntimeResult<(
Note,
Vec<(String, u64)>,
crate::retrieval::EmbeddingTruncationReport,
)> {
let (note, embedding, degradations, fences) = self
.create_note_inner(
token,
kind,
name,
content,
None,
salience,
Some(decay_factor),
properties,
annotates,
embedding_model,
true,
false,
)
.await?;
legacy_post_commit_result(
"create_note_with_decay_for_embedding_model_with_visibility_and_report",
note.id,
(note, fences, embedding),
degradations,
)
}
/// Insert a note using `INSERT OR IGNORE` semantics for atomic deduplication.
///
/// Returns `Ok(Some(note))` when the note was newly written. Returns
/// `Ok(None)` when a unique constraint (e.g. the channel-scoped `external_id`
/// partial index on comm message notes) was already satisfied by an existing row,
/// making this call a no-op. Indexing failures after an insert return a
/// typed `post_commit_degraded` error with the committed record ID and
/// `retryable=false`. Healthy models still run; the note is not rolled back.
///
/// This method is intentionally narrower than `create_note`: it skips
/// salience/decay, annotates edges, and embedding-model selection, which
/// are not needed for channel-ingest paths.
///
/// Rejects the transport-owned properties on a `message` note: the
/// `message` entry of the kind-owned property list that the note-store
/// accessor guard enforces, so both guards refuse the same keys. These
/// properties are transport-owned evidence (`comm.health` trusts the
/// quarantine and channel ones at face value), and this fast path (unlike
/// the generic `create` verb funnel) is not covered by the
/// pack-installed note-write validator. Only the trusted channel-ingest
/// path may establish them — see
/// [`Self::try_create_note_as_trusted_ingest`].
pub async fn try_create_note(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
properties: Option<serde_json::Value>,
) -> RuntimeResult<Option<Note>> {
self.try_create_note_impl(token, kind, name, content, properties, false, None, None)
.await
}
/// Like [`Self::try_create_note`] but permits the caller to establish the
/// transport-owned `message` properties that `try_create_note` refuses.
///
/// This is a deliberately named, separate entry point rather than a flag
/// on `try_create_note` so the trust decision is visible at every call
/// site: `comm.ingest` (`khive-pack-comm/src/handlers/ingest.rs`) is the sole
/// legitimate caller, because it is the only code that has just derived
/// quarantine disposition and channel provenance from the inbound
/// transport itself. The caller set is bounded by possession, not
/// documentation: the required [`crate::ChannelIngestCapability`] is
/// constructible only inside this crate and granted at pack registration
/// exclusively to channel-transport packs. Every other write path uses
/// `try_create_note`, which rejects those properties
/// unconditionally.
#[allow(clippy::too_many_arguments)]
pub async fn try_create_note_as_trusted_ingest(
&self,
_capability: &crate::pack::ChannelIngestCapability,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
properties: Option<serde_json::Value>,
expires_after: Option<std::time::Duration>,
) -> RuntimeResult<Option<Note>> {
self.try_create_note_impl(
token,
kind,
name,
content,
properties,
true,
None,
expires_after,
)
.await
}
/// Publish a trusted inbound message and its original-byte attachment in
/// one database transaction. Channel quarantine must not advertise a
/// reference in note metadata before GC can see its attachment owner.
#[allow(clippy::too_many_arguments)]
pub async fn try_create_note_as_trusted_ingest_with_attachment(
&self,
_capability: &crate::pack::ChannelIngestCapability,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
properties: Option<serde_json::Value>,
attachment: NewAttachment,
expires_after: Option<std::time::Duration>,
) -> RuntimeResult<Option<Note>> {
self.try_create_note_impl(
token,
kind,
name,
content,
properties,
true,
Some(attachment),
expires_after,
)
.await
}
#[allow(clippy::too_many_arguments)]
async fn try_create_note_impl(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
properties: Option<serde_json::Value>,
allow_transport_owned_message_properties: bool,
attachment: Option<NewAttachment>,
expires_after: Option<std::time::Duration>,
) -> RuntimeResult<Option<Note>> {
self.validate_note_kind(kind)?;
crate::secret_gate::reject_reserved_secret_gate_property(properties.as_ref())?;
crate::secret_gate::check_at(content, "note", "content")?;
if let Some(n) = name {
crate::secret_gate::check_at(n, "note", "name")?;
}
if let Some(ref p) = properties {
crate::secret_gate::check_json_at(p, "note", "properties")?;
}
if let Some(ref attachment) = attachment {
// The note and its owner row must share the main database. A
// secondary pack backend cannot atomically root the reference.
drop(self.attachments()?);
attachment.validate()?;
let blob_store = self.blob_store().ok_or_else(|| {
RuntimeError::Unconfigured(
"trusted ingest attachment requires an installed BlobStore".to_string(),
)
})?;
if !blob_store.exists(&attachment.content_ref).await? {
return Err(RuntimeError::InvalidInput(format!(
"trusted ingest attachment refers to an unpublished blob: {}",
attachment.content_ref
)));
}
}
if !allow_transport_owned_message_properties && kind == "message" {
if let Some(key) = properties
.as_ref()
.and_then(serde_json::Value::as_object)
.and_then(|supplied| {
crate::curation::kind_owned_properties("message")
.iter()
.copied()
.find(|key| supplied.contains_key(*key))
})
{
return Err(RuntimeError::InvalidInput(format!(
"`{key}` is transport-owned on a `message` note and cannot be supplied \
through `try_create_note`; only the trusted channel-ingest path may \
establish quarantine disposition and channel provenance"
)));
}
}
let ns = token.namespace().as_str();
let mut note = Note::new(ns, kind, content);
if let Some(retention) = expires_after {
let duration_us = i64::try_from(retention.as_micros()).map_err(|_| {
RuntimeError::InvalidInput(
"trusted ingest retention exceeds i64 microseconds".into(),
)
})?;
note.expires_at = Some(note.created_at.checked_add(duration_us).ok_or_else(|| {
RuntimeError::InvalidInput("trusted ingest expiry exceeds i64 microseconds".into())
})?);
}
if let Some(n) = name {
note = note.with_name(n);
}
if let Some(p) = properties {
note = note.with_properties(p);
}
// Bypasses the `notes()` accessor's PolicyEnforcingNoteStore wrapper —
// the reserved-transport-property check above already enforces the
// identical policy, conditionally allowing the trusted-ingest path,
// so this reaches storage directly rather than duplicate the check
// through a wrapper that cannot see the trust decision this function
// just made.
let inserted = if let Some(attachment) = attachment {
self.raw_notes(token)?
.try_insert_note_with_attachments(
note.clone(),
vec![Attachment::from_new(
note.id,
AttachmentSubstrate::Note,
attachment,
note.created_at,
)],
)
.await?
} else {
self.raw_notes(token)?.try_insert_note(note.clone()).await?
};
if !inserted {
return Ok(None);
}
let mut degradations = Vec::new();
match self.text_for_notes(token) {
Ok(fts) => {
if let Err(error) = fts.upsert_document(note_fts_document(¬e)).await {
record_conditional_insert_degradation(
&mut degradations,
note.id,
ConditionalInsertStage::FtsUpsert,
error,
);
}
}
Err(error) => record_conditional_insert_degradation(
&mut degradations,
note.id,
ConditionalInsertStage::FtsAcquisition,
error,
),
}
let embed_model_names = self.embedding_models_for_note_kind(kind);
for model_name in &embed_model_names {
match self
.embed_document_with_model_outcome_for_token(
token,
model_name,
note_embedding_text_ref(¬e),
)
.await
{
Ok(outcome) => {
if outcome.truncated {
tracing::warn!(
note_id = %note.id,
model = %outcome.model_name,
source_bytes = outcome.source_bytes,
embedded_bytes = outcome.embedded_bytes,
"try_create_note: embedding input truncated; full content stored unchanged"
);
}
match self.vectors_for_model(token, model_name) {
Ok(vs) => {
if let Err(error) = vs
.insert(
note.id,
SubstrateKind::Note,
ns,
"note.content",
vec![outcome.vector],
)
.await
{
record_conditional_insert_degradation(
&mut degradations,
note.id,
ConditionalInsertStage::VectorInsert,
format!("model {model_name}: {error}"),
);
}
}
Err(error) => record_conditional_insert_degradation(
&mut degradations,
note.id,
ConditionalInsertStage::VectorAcquisition,
format!("model {model_name}: {error}"),
),
}
}
Err(error) => record_conditional_insert_degradation(
&mut degradations,
note.id,
ConditionalInsertStage::Embedding,
format!("model {model_name}: {error}"),
),
}
}
legacy_post_commit_result("try_create_note", note.id, Some(note), degradations)
}
// REASON: private inner function unifies all create_note variants; it receives every
// optional parameter individually so that public variants can pass None without
// requiring callers to construct an intermediate struct.
#[allow(clippy::too_many_arguments)]
async fn create_note_inner(
&self,
token: &NamespaceToken,
kind: &str,
name: Option<&str>,
content: &str,
embedding_content: Option<&str>,
salience: Option<f64>,
decay_factor: Option<f64>,
properties: Option<serde_json::Value>,
annotates: Vec<Uuid>,
embedding_model: Option<&str>,
capture_visibility: bool,
web_receipt: bool,
) -> RuntimeResult<(
Note,
crate::retrieval::EmbeddingTruncationReport,
Vec<PostCommitDegradation>,
Vec<(String, u64)>,
)> {
self.validate_note_kind(kind)?;
// Owned identity properties are derived from the authorization token
// before anything else touches them, so every caller of this function —
// the generic `create` verb and direct Rust callers alike — stores the
// same derived values. Runs before the secret gate so the gate scans
// exactly what will be written.
let mut properties = self.derive_note_write_properties(kind, token, properties)?;
crate::secret_gate::reject_reserved_secret_gate_property(properties.as_ref())?;
if web_receipt {
let map = properties
.as_mut()
.and_then(serde_json::Value::as_object_mut)
.expect("web receipt properties are constructed as an object");
map.insert(
crate::secret_gate::RESERVED_WEB_RECEIPT_KEY.to_string(),
serde_json::Value::String(
crate::secret_gate::WEB_RECEIPT_PROVENANCE_VALUE.to_string(),
),
);
}
// Secret gate: scan content, optional name, and structured properties.
crate::secret_gate::check_at(content, "note", "content")?;
if let Some(n) = name {
crate::secret_gate::check_at(n, "note", "name")?;
}
if let Some(ref p) = properties {
crate::secret_gate::check_json_at(p, "note", "properties")?;
}
// `embedding_content` is a caller-supplied alternate vector-embedding
// input: it must be a non-empty proper prefix of `content` (never a
// superset, an unrelated string, or the full text) and passes the
// same secret gate as any other stored/embedded text. Rejected
// before any write, same as the checks above.
if let Some(ec) = embedding_content {
if ec.is_empty() {
return Err(RuntimeError::InvalidInput(
"embedding_content must not be empty".into(),
));
}
if ec.len() >= content.len() || !content.starts_with(ec) {
return Err(RuntimeError::InvalidInput(
"embedding_content must be a proper prefix of content".into(),
));
}
crate::secret_gate::check_at(ec, "note", "embedding_content")?;
}
let ns = token.namespace().as_str();
// Validate all annotates targets before any write (atomicity: all-or-nothing).
// Endpoint resolution is by-ID and namespace-agnostic.
for &target_id in &annotates {
if !self.substrate_exists_by_id(token, target_id).await? {
return Err(RuntimeError::NotFound(format!(
"create_note annotates target {target_id} not found"
)));
}
}
// Reject non-finite or out-of-range salience/decay at the runtime boundary
// rather than letting storage silently clamp them (coding-standards §508-516).
if let Some(s) = salience {
if !s.is_finite() || !(0.0..=1.0).contains(&s) {
return Err(RuntimeError::InvalidInput(format!(
"salience must be a finite value in [0.0, 1.0]; got {s}"
)));
}
}
if let Some(d) = decay_factor {
if !d.is_finite() || d < 0.0 {
return Err(RuntimeError::InvalidInput(format!(
"decay_factor must be a finite value >= 0.0; got {d}"
)));
}
}
// Resolve embedding_model BEFORE any note/FTS/vector write so unknown-model
// errors are atomic at the runtime layer, not just at one pack handler.
// Direct Rust callers (other packs, integration tests) get the same guarantee.
if let Some(model_name) = embedding_model {
self.resolve_embedding_model(Some(model_name))?;
}
let mut note = Note::new(ns, kind, content);
if let Some(s) = salience {
note = note.with_salience(s);
}
if let Some(df) = decay_factor {
note = note.with_decay(df);
}
if let Some(n) = name {
note = note.with_name(n);
}
if let Some(p) = properties {
note = note.with_properties(p);
}
let notes = if web_receipt {
self.raw_notes(token)?
} else {
self.notes(token)?
};
notes.upsert_note(note.clone()).await?;
// From here on, any error must compensate by removing the note row, its
// FTS document, and any vector entries already inserted — the same
// cleanup used by the annotates-edge block below.
// Decide which embedding models to use (before touching FTS/vectors).
let embed_model_names: Vec<String> = if let Some(m) = embedding_model {
vec![m.to_string()]
} else {
self.embedding_models_for_note_kind(kind)
};
// FTS step — compensate note row on failure.
{
// Injection: check FTS_FAIL_NS (armed by `arm_fts_fail_scoped(ns)`).
// Fires only when `ns` is in the armed set, removing it on the way
// out (one-shot, atomic check-and-remove under the mutex). No lock
// acquisition in release builds — the cfg(not) branch is a const
// false so the compiler eliminates the if-branch entirely.
#[cfg(any(test, feature = "fault-injection"))]
let fts_inject = consume_fault(&FTS_FAIL_NS, ns);
#[cfg(not(any(test, feature = "fault-injection")))]
let fts_inject = false;
let fts_result: RuntimeResult<()> = if fts_inject {
Err(RuntimeError::Internal("injected FTS failure".to_string()))
} else {
let statements =
khive_db::stores::text::delete_document_statements("fts_notes", ns, note.id)
.into_iter()
.chain(khive_db::stores::text::insert_document_statements(
"fts_notes",
¬e_fts_document(¬e),
))
.collect();
self.apply_note_index_revision(¬e, statements)
.await
.map(|_| ())
};
if let Err(e) = fts_result {
self.compensate_note_creation(¬e).await;
return Err(e);
}
}
// Vector embedding + insert step — compensate note row + FTS doc on failure.
// Multi-model vector embedding:
// - explicit embedding_model → single model (existing behaviour)
// - None → the note kind's declared model policy
// - None + no models configured → skip (text-only)
// The effective text sent to every embedder: the caller-supplied
// capped override when present, otherwise the full stored content.
// FTS indexing above always used the full `note.content` — this cap
// affects only the vector-embedding input.
let canonical_embed_text = note_embedding_text_ref(¬e);
let embed_text = embedding_content.unwrap_or(canonical_embed_text);
let mut embedding_report = crate::retrieval::EmbeddingTruncationReport::default();
let mut vector_fences = Vec::with_capacity(embed_model_names.len());
if embed_model_names.len() == 1 {
// Single-model path: preserves original sequential behaviour.
let model_name = &embed_model_names[0];
let vec_result = self
.embed_document_with_model_outcome_for_token(token, model_name, embed_text)
.await;
// Injection: check VECTOR_FAIL_NS (armed by `arm_vector_fail_scoped(ns)`) or
// VECTOR_FAIL_AFTER (armed by `arm_vector_fail_after(n)`). The former
// fires only when the armed namespace matches this note's namespace;
// callers that cannot guarantee no concurrently-running test also
// writes a note into that same namespace (e.g. a test suite whose
// fixtures share one default namespace) should prefer the latter,
// thread-local count instead — see its doc comment. Either clears
// (one-shot) once it fires. No lock/cell access in release builds —
// the cfg(not) branch is a const false eliminating the if-branch.
#[cfg(any(test, feature = "fault-injection"))]
let vec_inject = {
let ns_inject = consume_fault(&VECTOR_FAIL_NS, ns);
let count_inject = VECTOR_FAIL_AFTER.with(|cell| match cell.get() {
Some(0) => {
cell.set(None);
true
}
Some(n) => {
cell.set(Some(n - 1));
false
}
None => false,
});
ns_inject || count_inject
};
#[cfg(not(any(test, feature = "fault-injection")))]
let vec_inject = false;
let vec_result: RuntimeResult<crate::retrieval::DocumentEmbeddingOutcome> =
if vec_inject {
Err(RuntimeError::Internal(
"injected vector failure".to_string(),
))
} else {
vec_result
};
let single_model_result: RuntimeResult<()> = match vec_result {
Ok(outcome) => {
embedding_report.observe(&outcome);
if capture_visibility {
match self
.publish_note_vector_revision_with_seq(
token,
¬e,
model_name,
&outcome.vector,
)
.await
{
Ok(Some(seq)) => {
vector_fences.push((model_name.clone(), seq));
Ok(())
}
Ok(None) => Ok(()),
Err(error) => Err(error),
}
} else {
self.publish_note_vector_revision(token, ¬e, model_name, &outcome.vector)
.await
.map(|_| ())
}
}
Err(e) => Err(e),
};
if let Err(e) = single_model_result {
self.compensate_note_creation(¬e).await;
return Err(e);
}
} else if !embed_model_names.is_empty() {
// Multi-model path: embed with each model in parallel via spawned tasks,
// then insert one VectorRecord per model.
let rt_clone = self.clone();
// JoinSet tasks require owned text; an Arc keeps this to one
// content-sized allocation rather than one clone per model.
let content_owned: std::sync::Arc<str> = std::sync::Arc::from(embed_text);
let usage_ctx = crate::usage::current();
let mut join_set = tokio::task::JoinSet::new();
for (idx, model_name) in embed_model_names.iter().enumerate() {
let rt = rt_clone.clone();
let text = std::sync::Arc::clone(&content_owned);
let name = model_name.clone();
let ctx = usage_ctx.clone();
let token = (*token).clone();
join_set.spawn(crate::runtime::inherit_request_embedder_scope(async move {
let fut = rt.embed_document_with_model_outcome_for_token(
&token,
&name,
text.as_ref(),
);
let result = match ctx {
Some(ctx) => crate::usage::scope(ctx, fut).await,
None => fut.await,
};
(idx, result)
}));
}
// The first failed or panicked handle aborts and detaches its
// siblings. Embed usage is counted at dispatch, so a synchronous
// provider winding down in the background cannot change it.
let outcomes = match drain_embed_join_set(join_set, embed_model_names.len()).await {
Ok(outcomes) => outcomes,
Err(e) => {
self.compensate_note_creation(¬e).await;
return Err(e);
}
};
// TODO(P2): parallelize vector inserts
for (model_name, outcome) in embed_model_names.iter().zip(outcomes) {
embedding_report.observe(&outcome);
let insert_result = if capture_visibility {
self.publish_note_vector_revision_with_seq(
token,
¬e,
model_name,
&outcome.vector,
)
.await
.map(|seq| {
if let Some(seq) = seq {
vector_fences.push((model_name.clone(), seq));
}
})
} else {
self.publish_note_vector_revision(token, ¬e, model_name, &outcome.vector)
.await
.map(|_| ())
};
if let Err(e) = insert_result {
self.compensate_note_creation(¬e).await;
return Err(e);
}
}
}
// Create annotates edges, compensating on failure to preserve atomicity.
// Pre-validation (above) ensures all targets exist, so link failures are
// unexpected. If one occurs, purge incident edges and the note in one
// transaction; a failed purge retains the live note as their source.
let mut created_edges: Vec<Uuid> = Vec::with_capacity(annotates.len());
// In test builds, iterate with an index so the failure-injection hook can
// target a specific call. In release builds, skip the enumerate overhead.
#[cfg(test)]
let annotates_iter: Vec<(usize, Uuid)> = annotates
.iter()
.enumerate()
.map(|(i, &id)| (i, id))
.collect();
#[cfg(test)]
macro_rules! next_target {
($pair:expr) => {
$pair.1
};
}
#[cfg(not(test))]
let annotates_iter: Vec<Uuid> = annotates.to_vec();
#[cfg(not(test))]
macro_rules! next_target {
($pair:expr) => {
$pair
};
}
for pair in annotates_iter {
let target_id = next_target!(pair);
// Test-only: inject a failure on the configured call index (1-based).
#[cfg(test)]
let injected_err: Option<RuntimeError> = {
let call_idx = pair.0;
LINK_FAIL_AFTER.with(|cell| {
let n = cell.get();
if n > 0 && call_idx + 1 == n {
cell.set(0); // reset so subsequent calls are unaffected
Some(RuntimeError::Internal("injected link failure".to_string()))
} else {
None
}
})
};
#[cfg(not(test))]
let injected_err: Option<RuntimeError> = None;
let link_result = if let Some(e) = injected_err {
Err(e)
} else {
self.link(
token,
note.id,
target_id,
EdgeRelation::Annotates,
1.0,
None,
)
.await
};
match link_result {
Ok(edge) => created_edges.push(edge.id.into()),
Err(e) => {
// The graph purge, index cleanup, and note removal share
// one writer transaction. If the edge purge fails, its
// transaction rolls back, preserving a live source note
// for the surviving edges instead of orphaning them.
let edge_ids = created_edges
.iter()
.map(Uuid::to_string)
.collect::<Vec<_>>()
.join(", ");
match self.compensate_note_creation_with_edges(¬e).await {
Ok(true) => return Err(e),
Ok(false) => {
return Err(RuntimeError::Internal(format!(
"create_note: annotates link failed: {e}; note {} changed before \
compensation, retaining its incident edges [{edge_ids}]",
note.id
)));
}
Err(cleanup_error) => {
return Err(RuntimeError::Internal(format!(
"create_note: annotates link failed: {e}; compensation failed \
for note {} and retained edges [{edge_ids}]: {cleanup_error}; \
note and edges remain for reconciliation",
note.id
)));
}
}
}
}
}
// Same contract as the entity arrival event above: after compensation,
// so a rolled-back create leaves no event. This is the single funnel for
// every note create in the product, which is why the memory pack's own
// note_created emitter was removed rather than left beside it.
let created_event = khive_storage::event::Event::new(
note.namespace.clone(),
"create",
EventKind::NoteCreated,
SubstrateKind::Note,
"",
)
.with_target(note.id)
.with_payload(serde_json::json!({
"id": note.id,
"namespace": note.namespace,
"kind": note.kind,
"salience": note.salience,
}));
let event_result = match self.events(token) {
Ok(store) => store
.append_event(created_event)
.await
.map_err(RuntimeError::from),
Err(error) => Err(error),
};
let mut degradations = Vec::new();
if let Err(error) = event_result {
record_post_commit_degradation(
&mut degradations,
"create_note",
note.id,
"event_append",
error,
);
}
vector_fences.sort_by(|a, b| a.0.cmp(&b.0));
Ok((note, embedding_report, degradations, vector_fences))
}
/// Search notes using a hybrid FTS5 + vector pipeline with salience weighting.
///
/// Pipeline:
/// 1. FTS5 query against `notes_<namespace>`.
/// 2. If embedding model is configured: vector search filtered to `kind="note"`.
/// 3. RRF fusion (k=60).
/// 4. Salience-weighted rerank: `score *= (0.5 + 0.5 * note.salience)`.
/// 5. Filter soft-deleted notes, apply optional kind / tag / properties predicates.
/// Tags and properties are pushed into the per-note fetch loop BEFORE truncation
/// so that matching notes ranked beyond `limit` in the raw fusion are not silently
/// dropped.
/// 6. Truncate to `limit`.
///
/// `tags_any`: when non-empty, only notes that have at least one of these tags
/// (stored in `properties["tags"]`, case-insensitive match) are retained. The
/// check happens inside the alive-note loop, before `hits.truncate(limit)`.
///
/// `properties_filter`: when `Some`, only notes whose `properties` JSON object is
/// a superset of the given filter object are retained. Also applied before truncation.
#[allow(clippy::too_many_arguments)]
pub async fn search_notes(
&self,
token: &NamespaceToken,
query_text: &str,
query_vector: Option<Vec<f32>>,
limit: u32,
note_kind: Option<&str>,
include_superseded: bool,
tags_any: &[String],
properties_filter: Option<&serde_json::Value>,
) -> RuntimeResult<Vec<NoteSearchHit>> {
self.search_notes_with_text_mode(
token,
query_text,
query_vector,
limit,
note_kind,
include_superseded,
tags_any,
properties_filter,
TextQueryMode::Plain,
)
.await
}
/// Note search with an explicit lexical mode for the text arm.
#[allow(clippy::too_many_arguments)]
pub async fn search_notes_with_text_mode(
&self,
token: &NamespaceToken,
query_text: &str,
query_vector: Option<Vec<f32>>,
limit: u32,
note_kind: Option<&str>,
include_superseded: bool,
tags_any: &[String],
properties_filter: Option<&serde_json::Value>,
text_mode: TextQueryMode,
) -> RuntimeResult<Vec<NoteSearchHit>> {
let (hits, _vector_error) = self
.search_notes_inner(
token,
query_text,
query_vector,
limit,
note_kind,
include_superseded,
tags_any,
properties_filter,
text_mode,
false,
)
.await?;
Ok(hits)
}
/// Coordinator fan-out variant of [`Self::search_notes`]: the text arm
/// still fails loud, but a vector-arm failure after a successful text leg
/// is captured instead of discarding the text hits — mirrors
/// [`Self::hybrid_search_outcome`]'s contract for the entity substrate.
/// Reserved for `SubstrateCoordinator::fan_out_search_with_visibility`;
/// every other caller keeps the fail-loud [`Self::search_notes`] contract.
#[allow(clippy::too_many_arguments)]
pub async fn search_notes_outcome(
&self,
token: &NamespaceToken,
query_text: &str,
limit: u32,
note_kind: Option<&str>,
include_superseded: bool,
tags_any: &[String],
properties_filter: Option<&serde_json::Value>,
) -> RuntimeResult<NoteSearchOutcome> {
self.search_notes_outcome_with_text_mode(
token,
query_text,
limit,
note_kind,
include_superseded,
tags_any,
properties_filter,
TextQueryMode::Plain,
)
.await
}
/// Coordinator note-search variant with an explicit lexical mode.
#[allow(clippy::too_many_arguments)]
pub async fn search_notes_outcome_with_text_mode(
&self,
token: &NamespaceToken,
query_text: &str,
limit: u32,
note_kind: Option<&str>,
include_superseded: bool,
tags_any: &[String],
properties_filter: Option<&serde_json::Value>,
text_mode: TextQueryMode,
) -> RuntimeResult<NoteSearchOutcome> {
let (hits, vector_error) = self
.search_notes_inner(
token,
query_text,
None,
limit,
note_kind,
include_superseded,
tags_any,
properties_filter,
text_mode,
true,
)
.await?;
Ok(NoteSearchOutcome { hits, vector_error })
}
#[allow(clippy::too_many_arguments)]
async fn search_notes_inner(
&self,
token: &NamespaceToken,
query_text: &str,
query_vector: Option<Vec<f32>>,
limit: u32,
note_kind: Option<&str>,
include_superseded: bool,
tags_any: &[String],
properties_filter: Option<&serde_json::Value>,
text_mode: TextQueryMode,
tolerate_vector_error: bool,
) -> RuntimeResult<(Vec<NoteSearchHit>, Option<String>)> {
const RRF_K: usize = 60;
let candidates = limit.saturating_mul(4).max(limit);
let visible_ns: Vec<String> = token
.visible_namespaces()
.iter()
.map(|ns| ns.as_str().to_owned())
.collect();
// FTS5 over the notes index — search all visible namespaces.
//
// `sanitize_fts5_query` strips known-unsafe FTS5 metacharacters, but
// residual punctuation the sanitizer does not strip can still reach
// the FTS5 parser and error. This fails loud instead of degrading to
// vector-only fusion, so callers see the bad query instead of
// silently losing the lexical leg. Errors from any other leg (vector
// search, note hydration) still propagate normally.
//
// Injection: check FTS_SEARCH_FAIL_NS (armed by `arm_fts_search_fail(ns)`),
// exercising the propagate branch above. Fires only when the armed
// namespace is among this call's visible namespaces, then clears (one-shot).
#[cfg(any(test, feature = "fault-injection"))]
let fts_search_inject = {
let mut g = FTS_SEARCH_FAIL_NS.lock().unwrap();
match g.as_deref() {
Some(armed) if visible_ns.iter().any(|ns| ns == armed) => {
*g = None;
true
}
_ => false,
}
};
#[cfg(not(any(test, feature = "fault-injection")))]
let fts_search_inject = false;
let text_store = self.text_for_notes(token)?;
let text_fut = async {
if fts_search_inject {
return Err(khive_storage::StorageError::Timeout {
operation: "fts_search".into(),
});
}
text_store
.search(TextSearchRequest {
query: query_text.to_string(),
mode: text_mode,
filter: Some(TextFilter {
namespaces: visible_ns.clone(),
// Push the note-kind filter into the FTS query. Without it the
// text arm returns the top `candidates` rows across EVERY note
// kind in the namespace and the kind is applied post-fetch, so a
// store where short message/session rows outrank task
// descriptions under BM25 hands the caller one or two task hits
// while the store holds many more carrying the literal.
record_kinds: note_kind
.map(|kind| vec![kind.to_string()])
.unwrap_or_default(),
..TextFilter::default()
}),
top_k: candidates,
snippet_chars: 200,
})
.await
};
let text_fut = crate::stage_seam::text_stage(text_fut);
// Vector search filtered to notes; it runs with the text stage, and a text error wins.
let vector_fut = async {
if query_vector.is_some() || self.config().embedding_model.is_some() {
self.note_search_vector_search(token, query_vector, query_text, candidates)
.await
} else {
Ok(vec![])
}
};
let (text_search_result, vector_result) = tokio::join!(text_fut, vector_fut);
// FtsPasses is counted inside the store's `search()` (khive-db
// stores/text.rs), only once a real FTS5 statement is prepared —
// an empty/fully-sanitized query short-circuits there before any
// statement exists and must not count (nor does the injected-failure
// branch above, which never reaches the store at all).
let text_hits = crate::error::fts_text_leg_or_err(
text_search_result.map_err(RuntimeError::from),
"search_notes",
query_text,
)?;
let mut vector_error: Option<String> = None;
let vector_hits = match vector_result {
Ok(hits) => hits,
Err(e) if tolerate_vector_error => {
vector_error = Some(e.to_string());
Vec::new()
}
Err(e) => return Err(e),
};
// Keep the full text∪vector union through RRF — salience weighting and
// soft-delete/kind filtering happen *after* this, and the final
// `hits.truncate(limit)` is the only result-limiting cut. Truncating to
// `candidates` here would drop a high-salience note ranked just outside
// the raw RRF cutoff before salience ever applied.
let fuse_k = text_hits.len() + vector_hits.len();
let fused = self
.fuse_with_strategy(
text_hits,
vector_hits,
&crate::fusion::FusionStrategy::Rrf { k: RRF_K },
fuse_k,
)
.await?;
let candidate_ids: Vec<Uuid> = fused.iter().map(|hit| hit.entity_id).collect();
if candidate_ids.is_empty() {
return Ok((vec![], vector_error));
}
// Hydrate every candidate note with one batched read to get salience and
// apply soft-delete + (optional) kind filtering. The store chunks the read
// below its bound-parameter ceiling, so the read costs `ceil(candidates / 900)`
// statements instead of one per candidate. Notes whose `kind` doesn't
// match `note_kind` are dropped post-fetch — they're a small set
// bounded by the text∪vector union (≤ 2×candidates), so the read is cheap.
let note_store = self.notes(token)?;
let search_pool = self.backend().pool_arc();
let mailbox_view = crate::MailboxView {
actor_id: token.actor().id.clone(),
delegated: false,
};
let mut alive_notes: HashMap<Uuid, Note> = HashMap::new();
for note in note_store.get_notes_batch(&candidate_ids).await? {
search_pool.record_note_candidate_hydration_row();
if note.deleted_at.is_some() {
continue;
}
if !mailbox_view.permits_message_note(token, ¬e) {
continue;
}
if let Some(want_kind) = note_kind {
if note.kind != want_kind {
continue;
}
}
// Apply tag predicate before adding to alive set: tags on notes live
// inside `properties["tags"]` (a JSON array). This pushes the filter
// before truncation so matching notes ranked beyond `limit` in the raw
// fusion are not silently dropped.
if !tags_any.is_empty() {
let note_tags: Vec<String> = note
.properties
.as_ref()
.and_then(|p| p.get("tags"))
.and_then(serde_json::Value::as_array)
.map(|arr| {
arr.iter()
.filter_map(serde_json::Value::as_str)
.map(str::to_owned)
.collect()
})
.unwrap_or_default();
if !note_tags
.iter()
.any(|t| tags_any.iter().any(|w| t.eq_ignore_ascii_case(w)))
{
continue;
}
}
// Apply properties predicate before truncation, same reasoning as tags above.
if let Some(pf) = properties_filter {
if !crate::retrieval::properties_match(note.properties.as_ref(), pf) {
continue;
}
}
alive_notes.insert(note.id, note);
}
// Drop superseded notes unless include_superseded is true: any note targeted
// by a `supersedes` edge is obsolete and excluded from default search.
if !include_superseded && !alive_notes.is_empty() {
let graph = self.graph(token)?;
let note_ids: Vec<Uuid> = alive_notes.keys().copied().collect();
let superseded: std::collections::HashSet<Uuid> = graph
.batch_neighbors(
¬e_ids,
NeighborQuery {
direction: Direction::In,
relations: Some(vec![EdgeRelation::Supersedes]),
limit: Some(1),
min_weight: None,
},
)
.await?
.into_iter()
.map(|(note_id, _)| note_id)
.collect();
alive_notes.retain(|id, _| !superseded.contains(id));
}
// Apply salience weighting and collect final hits.
let mut hits: Vec<NoteSearchHit> = fused
.into_iter()
.filter_map(|hit| {
let note = alive_notes.get(&hit.entity_id)?;
let weighted = salience_weighted_rank(hit.score, note.salience);
Some(NoteSearchHit {
note_id: hit.entity_id,
score: weighted,
rank_score_kind: hit.rank_score_kind,
signals: hit.signals,
source: hit.source,
title: hit.title.or_else(|| note_title(note)),
snippet: hit.snippet.or_else(|| note_snippet(note)),
})
})
.collect();
hits.sort_by(|a, b| b.score.cmp(&a.score).then(a.note_id.cmp(&b.note_id)));
hits.truncate(limit as usize);
Ok((hits, vector_error))
}
/// Restore a note tombstone owned by the caller's primary namespace.
///
/// A live note holding the tombstone's `(namespace, kind, key)` refuses
/// the operation before any row changes. The same condition is repeated
/// in the guarded restore statement for the concurrent race.
pub async fn restore_note(
&self,
token: &NamespaceToken,
id: Uuid,
) -> RuntimeResult<Option<(Note, bool)>> {
let Some(note) = self.notes(token)?.get_note_including_deleted(id).await? else {
return Ok(None);
};
if note.namespace != token.namespace().as_str() {
return Ok(None);
}
if note.deleted_at.is_none() {
return Ok(Some((note, false)));
}
if let Some(key) = note.key.as_deref() {
if let Some(holder) = self
.notes(token)?
.get_live_notes_by_key(¬e.namespace, key, Some(¬e.kind))
.await?
.into_iter()
.find(|holder| holder.id != note.id)
{
return Err(restore_key_conflict(key, &holder));
}
}
let updated_at =
Utc::now()
.timestamp_micros()
.max(note.updated_at.checked_add(1).ok_or_else(|| {
RuntimeError::Internal(format!(
"note {id} updated_at is already at i64::MAX and cannot advance"
))
})?);
let mut params = vec![
SqlValue::Text("active".into()),
SqlValue::Integer(updated_at),
SqlValue::Text(id.to_string()),
SqlValue::Text(note.namespace.clone()),
SqlValue::Text(note.kind.clone()),
];
let key_clause = if let Some(key) = note.key.as_deref() {
params.push(SqlValue::Text(key.to_owned()));
format!(
" AND (key IS NULL OR NOT EXISTS (SELECT 1 FROM notes live \
WHERE live.namespace=?4 AND live.kind=?5 AND live.key=?{} \
AND live.deleted_at IS NULL AND live.id != notes.id))",
params.len()
)
} else {
String::new()
};
let mut restored = note.clone();
restored.status = "active".into();
restored.deleted_at = None;
restored.updated_at = updated_at;
restored.version = restored
.version
.checked_add(1)
.ok_or_else(|| RuntimeError::Internal(format!("note {id} version is exhausted")))?;
let mut statements = vec![PlanStatement {
statement: SqlStatement {
sql: format!(
"UPDATE notes SET status=?1, deleted_at=NULL, updated_at=?2 \
WHERE id=?3 AND namespace=?4 AND kind=?5 AND deleted_at IS NOT NULL{key_clause}"
),
params,
label: Some("note-restore".into()),
},
guard: Some(AffectedRowGuard::exactly(1)),
}];
// Text index published in the same unit as the row; see restore_entity.
for statement in
khive_db::stores::text::delete_document_statements("fts_notes", &restored.namespace, id)
.into_iter()
.chain(insert_document_statements(
"fts_notes",
¬e_fts_document(&restored),
))
{
statements.push(PlanStatement {
statement,
guard: None,
});
}
let plan = AtomicOpPlan::Update(Box::new(UpdatePlan {
graph_effects: Vec::new(),
target_id: id,
statements,
post_commit: PostCommitEffect::None,
edge_natural_key: None,
idempotent_noop: false,
entity_guard: None,
note_guard: None,
note_vector_purge: None,
note_embedding_inheritance: None,
}));
match run_atomic_unit(self.sql().as_ref(), vec![plan]).await {
Ok(AtomicRunOutcome::Committed { .. }) => {
#[cfg(any(test, feature = "fault-injection"))]
if consume_fault(&FTS_FAIL_NS, &restored.namespace) {
return Err(restore_reindex_failed(
"note",
id,
RuntimeError::Internal("injected FTS failure".to_string()),
));
}
let reindexed = self.reindex_note_with_report(token, &restored).await;
let report = reindexed.map_err(|e| restore_reindex_failed("note", id, e))?;
let degradations = report.post_commit_degradations();
legacy_post_commit_result("restore_note", id, Some((restored, true)), degradations)
}
Ok(AtomicRunOutcome::RolledBack {
failure: AtomicOpFailure::GuardFailed { .. },
..
}) => {
if let Some(key) = note.key.as_deref() {
if let Some(holder) = self
.notes(token)?
.get_live_notes_by_key(¬e.namespace, key, Some(¬e.kind))
.await?
.into_iter()
.find(|holder| holder.id != note.id)
{
return Err(restore_key_conflict(key, &holder));
}
}
Err(RuntimeError::NotFound(format!(
"note {id} is no longer a caller-owned tombstone"
)))
}
Ok(AtomicRunOutcome::RolledBack { failure, .. }) => Err(RuntimeError::Internal(
format!("note restore rolled back: {failure:?}"),
)),
Err(error) => Err(RuntimeError::Storage(error.0)),
}
}
}
/// Fully specified edge creation request — input to [`KhiveRuntime::build_edge`]
/// and [`KhiveRuntime::link_many`].
#[derive(Clone, Debug)]
pub struct LinkSpec {
pub namespace: Option<String>,
pub source_id: Uuid,
pub target_id: Uuid,
pub relation: EdgeRelation,
pub weight: f64,
pub metadata: Option<serde_json::Value>,
pub resurrect: bool,
}
/// Fully specified entity creation request — input to [`KhiveRuntime::create_many`].
///
/// `entity_type` is validated at the runtime layer by the pack-installed
/// entity-type validator. When a validator
/// is installed (e.g. by `KgPack`), unknown types are rejected with the valid
/// set listed. When no validator is installed (bare runtime without packs),
/// the value passes through — the handler layer is the primary enforcement point.
#[derive(Clone, Debug)]
pub struct EntityCreateSpec {
pub kind: String,
pub entity_type: Option<String>,
pub name: String,
pub description: Option<String>,
pub properties: Option<serde_json::Value>,
pub tags: Vec<String>,
}
// INLINE TEST JUSTIFICATION: tests here exercise private helpers (canonical_edge_endpoints,
// validate_edge_metadata, merge_dependency_kind, link-fail injection) and runtime methods
// that require pub(crate) KhiveRuntime construction. Moving them to tests/ would require
// pub-exporting those private helpers, which would widen the crate's public API surface
// undesirably. Broad behavioral tests live in tests/integration.rs.
#[cfg(test)]
#[path = "operations_tests.rs"]
mod tests;