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
//! `HostedPrime` — a stateless, tenant-scoped Prime engine over a remote Core.
//!
//! This is the composition the hosted `allsource-prime` app uses instead of the
//! embedded [`Prime`](super::facade::Prime): it owns no durable store. Reads
//! resolve a tenant's warm [`GraphProjections`] from the
//! [`TenantProjectionCache`] (hydrated on demand from the remote Core's
//! `prime.*` events); writes ingest an event to the remote Core via an
//! [`HttpCore`] scoped to the tenant and then update the warm bundle so reads
//! stay current.
//!
//! Tenant identity is always an explicit argument supplied by the trusted
//! caller (the gateway), never inferred — so one `HostedPrime` serves every
//! tenant with strict isolation (each tenant's events are queried and folded
//! separately; see the cross-tenant test in [`super::tenant_cache`]).
//!
//! Deliberately a distinct type from the embedded `Prime` (not a new variant of
//! it) so the embedded local-first path and `facade.rs` are untouched. The
//! MCP/HTTP surface is adapted onto this in a later slice.
//!
//! Feature-gated behind `prime-recall`. See bead t-10f876 /
//! `docs/proposals/PRIME_STATELESS_OVER_CORE.md`.
use std::{sync::Arc, time::Duration};
use serde_json::json;
use crate::{
embedded::{EventView, IngestEvent, Query},
error::Result,
prime::{
event_store::EventStore,
http_core::HttpCore,
projection_bundle::GraphProjections,
tenant_cache::TenantProjectionCache,
types::{
Direction, EdgeId, Node, NodeId, PrimeStats, edge_entity_id, event_types,
node_entity_id,
},
},
};
/// A stateless Prime engine backed by a remote Core, serving many tenants.
pub struct HostedPrime {
base_url: String,
api_key: Option<String>,
cache: TenantProjectionCache,
/// Lazily-initialized in-process text embedder, shared across all tenants
/// (the model is stateless — only the per-tenant vectors are isolated).
/// Mirrors the embedded `Prime::embedder` OnceLock (facade.rs).
#[cfg(feature = "prime-vectors")]
embedder: std::sync::OnceLock<Arc<crate::prime::vectors::TextEmbedder>>,
}
impl HostedPrime {
/// Connect to the Core at `base_url`, keeping up to `capacity` tenants warm,
/// re-hydrating bundles older than `ttl`.
pub fn connect(
base_url: impl Into<String>,
api_key: Option<String>,
capacity: usize,
ttl: Duration,
) -> Self {
let base_url = base_url.into();
let cache = TenantProjectionCache::new(base_url.clone(), api_key.clone(), capacity, ttl);
Self {
base_url,
api_key,
cache,
#[cfg(feature = "prime-vectors")]
embedder: std::sync::OnceLock::new(),
}
}
/// An [`HttpCore`] scoped to `tenant`, for writes.
fn core_for(&self, tenant: &str) -> HttpCore {
HttpCore::new(
self.base_url.clone(),
self.api_key.clone(),
Some(tenant.to_string()),
)
}
// ── Reads ────────────────────────────────────────────────────────────
/// The tenant's warm graph-projection bundle (hydrated on demand).
pub async fn tenant_graph(&self, tenant: &str) -> Result<Arc<GraphProjections>> {
self.cache.get_or_hydrate(tenant).await
}
/// Get a node by entity_id within a tenant's graph. `None` if absent or
/// soft-deleted.
pub async fn get_node(&self, tenant: &str, entity_id: &str) -> Result<Option<Node>> {
let graph = self.cache.get_or_hydrate(tenant).await?;
Ok(graph.node_state.get_node(entity_id).filter(|n| !n.deleted))
}
// ── Writes ───────────────────────────────────────────────────────────
/// Create a node: ingest a `prime.node.created` event to the remote Core
/// (tenant-stamped) and apply it to the tenant's warm bundle so subsequent
/// reads reflect it without a re-query.
pub async fn add_node(
&self,
tenant: &str,
node_type: &str,
properties: serde_json::Value,
) -> Result<NodeId> {
let id = uuid::Uuid::new_v4().to_string();
let entity_id = node_entity_id(node_type, &id);
let payload = json!({
"id": id,
"node_type": node_type,
"properties": properties,
});
self.core_for(tenant)
.ingest(IngestEvent {
entity_id: &entity_id,
event_type: event_types::NODE_CREATED,
payload: payload.clone(),
metadata: None,
tenant_id: Some(tenant),
})
.await?;
// Update the tenant's warm bundle in place IF it is cached, so an
// in-process read reflects the write without a re-query. If the tenant
// is cold, cache.apply no-ops and the next read hydrates from Core
// (which now has this event). We deliberately do NOT hydrate-then-apply:
// that would fold the just-ingested event in twice, inflating
// count-based projections like graph_stats.
self.cache.apply(
tenant,
&self.synth_view_typed(tenant, event_types::NODE_CREATED, &entity_id, payload),
);
Ok(NodeId::new(id))
}
/// Create a directed edge: ingest a `prime.edge.created` event (tenant-stamped)
/// and apply it to the tenant's warm bundle so `neighbors` reflects it without
/// a re-query. Mirrors the embedded `Prime::add_edge_inner` payload exactly.
pub async fn add_edge(
&self,
tenant: &str,
source: &str,
target: &str,
relation: &str,
properties: Option<serde_json::Value>,
) -> Result<EdgeId> {
self.add_edge_inner(tenant, source, target, relation, None, properties)
.await
}
/// Create a weighted directed edge. See [`add_edge`](Self::add_edge).
pub async fn add_edge_weighted(
&self,
tenant: &str,
source: &str,
target: &str,
relation: &str,
weight: f64,
properties: Option<serde_json::Value>,
) -> Result<EdgeId> {
self.add_edge_inner(tenant, source, target, relation, Some(weight), properties)
.await
}
async fn add_edge_inner(
&self,
tenant: &str,
source: &str,
target: &str,
relation: &str,
weight: Option<f64>,
properties: Option<serde_json::Value>,
) -> Result<EdgeId> {
let id = uuid::Uuid::new_v4().to_string();
let entity_id = edge_entity_id(&id);
let mut payload = json!({
"id": id,
"source": source,
"target": target,
"relation": relation,
});
if let Some(w) = weight {
payload["weight"] = json!(w);
}
if let Some(props) = properties {
payload["properties"] = props;
}
self.core_for(tenant)
.ingest(IngestEvent {
entity_id: &entity_id,
event_type: event_types::EDGE_CREATED,
payload: payload.clone(),
metadata: None,
tenant_id: Some(tenant),
})
.await?;
self.cache.apply(
tenant,
&self.synth_view_typed(tenant, event_types::EDGE_CREATED, &entity_id, payload),
);
Ok(EdgeId::new(id))
}
/// Soft-delete a node: ingest a `prime.node.deleted` event (tenant-stamped)
/// and apply it to the tenant's warm bundle so `get_node` returns `None`
/// without a re-query. Mirrors the embedded `Prime::delete_node` node-delete
/// event shape (connected edges are reconciled on the next hydrate).
pub async fn delete_node(&self, tenant: &str, entity_id: &str) -> Result<()> {
self.core_for(tenant)
.ingest(IngestEvent {
entity_id,
event_type: event_types::NODE_DELETED,
payload: json!({}),
metadata: None,
tenant_id: Some(tenant),
})
.await?;
self.cache.apply(
tenant,
&self.synth_view_typed(tenant, event_types::NODE_DELETED, entity_id, json!({})),
);
Ok(())
}
/// Get 1-hop neighbors of a node, optionally filtered by relation and
/// direction. Returns full [`Node`]s with deleted nodes excluded. Mirrors
/// the embedded `Prime::neighbors` traversal over `adjacency`/`reverse_index`.
pub async fn neighbors(
&self,
tenant: &str,
entity_id: &str,
relation: Option<&str>,
direction: Direction,
) -> Result<Vec<Node>> {
let g = self.cache.get_or_hydrate(tenant).await?;
let mut peer_ids: Vec<String> = Vec::new();
let matches = |entry_relation: &str| relation.is_none_or(|r| r == entry_relation);
match direction {
Direction::Outgoing => {
for entry in g.adjacency.outgoing(entity_id) {
if matches(&entry.relation) {
peer_ids.push(entry.peer.clone());
}
}
}
Direction::Incoming => {
for entry in g.reverse_index.incoming(entity_id) {
if matches(&entry.relation) {
peer_ids.push(entry.peer.clone());
}
}
}
Direction::Both => {
let mut seen = std::collections::HashSet::new();
for entry in g.adjacency.outgoing(entity_id) {
if matches(&entry.relation) && seen.insert(entry.peer.clone()) {
peer_ids.push(entry.peer.clone());
}
}
for entry in g.reverse_index.incoming(entity_id) {
if matches(&entry.relation) && seen.insert(entry.peer.clone()) {
peer_ids.push(entry.peer.clone());
}
}
}
}
Ok(peer_ids
.iter()
.filter_map(|id| g.node_state.get_node(id).filter(|n| !n.deleted))
.collect())
}
/// All live nodes of a given type. Mirrors the embedded `Prime::nodes_by_type`
/// (reads `node_type_index` + `node_state`).
pub async fn search(&self, tenant: &str, node_type: &str) -> Result<Vec<Node>> {
let g = self.cache.get_or_hydrate(tenant).await?;
Ok(g.node_type_index
.nodes_by_type(node_type)
.iter()
.filter_map(|entity_id| g.node_state.get_node(entity_id).filter(|n| !n.deleted))
.collect())
}
/// Statistics about the tenant's graph (O(1) via the `graph_stats` projection).
pub async fn stats(&self, tenant: &str) -> Result<PrimeStats> {
let g = self.cache.get_or_hydrate(tenant).await?;
Ok(g.graph_stats.stats())
}
/// The full audit trail for an entity, read straight from the remote Core.
/// Returns raw events (no projection); empty if the entity never existed.
pub async fn history(&self, tenant: &str, entity_id: &str) -> Result<Vec<EventView>> {
self.core_for(tenant)
.query(Query::new().entity_id(entity_id))
.await
}
/// Update a node's properties (deep-merged by the projection): ingest a
/// `prime.node.updated` event (tenant-stamped) and apply it to the warm
/// bundle. Mirrors the embedded `Prime::update_node` payload — the raw update
/// `{properties}` is emitted; node_state merges on `process`. (Schema
/// validation, which the embedded path does pre-ingest, is deferred here.)
pub async fn update_node(
&self,
tenant: &str,
entity_id: &str,
properties: serde_json::Value,
) -> Result<()> {
let payload = json!({ "properties": properties });
self.core_for(tenant)
.ingest(IngestEvent {
entity_id,
event_type: event_types::NODE_UPDATED,
payload: payload.clone(),
metadata: None,
tenant_id: Some(tenant),
})
.await?;
self.cache.get_or_hydrate(tenant).await?;
self.cache.apply(
tenant,
&self.synth_view_typed(tenant, event_types::NODE_UPDATED, entity_id, payload),
);
Ok(())
}
/// Delete an edge: ingest a `prime.edge.deleted` event (tenant-stamped) and
/// apply it so `neighbors` stops returning it without a re-query. Mirrors the
/// embedded `Prime::delete_edge` shape (`edge:{id}` entity, payload `{id}`).
pub async fn delete_edge(&self, tenant: &str, edge_id: &str) -> Result<()> {
let entity_id = edge_entity_id(edge_id);
let payload = json!({ "id": edge_id });
self.core_for(tenant)
.ingest(IngestEvent {
entity_id: &entity_id,
event_type: event_types::EDGE_DELETED,
payload: payload.clone(),
metadata: None,
tenant_id: Some(tenant),
})
.await?;
self.cache.get_or_hydrate(tenant).await?;
self.cache.apply(
tenant,
&self.synth_view_typed(tenant, event_types::EDGE_DELETED, &entity_id, payload),
);
Ok(())
}
/// Shortest path between two nodes (BFS over outgoing edges, optional relation
/// filter). Mirrors the embedded `Prime::shortest_path`, but runs the BFS over
/// the tenant's warm bundle (resolved once) instead of the embedded store.
pub async fn shortest_path(
&self,
tenant: &str,
from: &str,
to: &str,
relation: Option<&str>,
) -> Result<Option<Vec<Node>>> {
use std::collections::{HashMap, VecDeque};
let g = self.cache.get_or_hydrate(tenant).await?;
let get_node = |id: &str| g.node_state.get_node(id).filter(|n| !n.deleted);
if from == to {
return Ok(get_node(from).map(|n| vec![n]));
}
let mut visited: HashMap<String, String> = HashMap::new(); // child -> parent
let mut queue: VecDeque<String> = VecDeque::new();
visited.insert(from.to_string(), String::new());
queue.push_back(from.to_string());
while let Some(current) = queue.pop_front() {
for entry in g.adjacency.outgoing(¤t) {
if relation.is_some() && relation != Some(entry.relation.as_str()) {
continue;
}
let peer = entry.peer;
if visited.contains_key(&peer) {
continue;
}
visited.insert(peer.clone(), current.clone());
if peer == to {
let mut path_ids = vec![to.to_string()];
let mut cursor = to.to_string();
while let Some(parent) = visited.get(&cursor) {
if parent.is_empty() {
break;
}
path_ids.push(parent.clone());
cursor = parent.clone();
}
path_ids.reverse();
return Ok(Some(
path_ids.iter().filter_map(|id| get_node(id)).collect(),
));
}
queue.push_back(peer);
}
}
Ok(None)
}
// ── Vectors & Recall ─────────────────────────────────────────────────
/// Store a vector embedding for a graph node: ingest a `prime.vector.stored`
/// event (tenant-stamped) and apply it to the tenant's warm bundle so recall
/// reflects it without a re-query.
///
/// `node_entity_id` is the graph entity id (e.g. `node:contact:abc`). The
/// vector is stored under `vec:{node_entity_id}` so [`recall`](Self::recall)
/// can strip the `vec:` prefix and hydrate the matched node from `node_state`.
/// Mirrors the embedded `Prime::embed_with_metadata` event shape exactly: the
/// vector lives in event metadata under `embedding`; text/dimensions/metadata
/// in the payload.
#[cfg(feature = "prime-vectors")]
pub async fn embed(
&self,
tenant: &str,
node_entity_id: &str,
vector: Vec<f32>,
metadata: Option<serde_json::Value>,
) -> Result<()> {
use crate::prime::vectors::{event_types as vec_events, vector_entity_id};
let entity_id = vector_entity_id(node_entity_id);
let dimensions = vector.len();
// Dimension guard: a store holds one dimension. Reject a mismatch before
// it corrupts the HNSW index (mirrors facade's embed_with_metadata).
let graph = self.cache.get_or_hydrate(tenant).await?;
if let Some(established) = graph.vector_index.dimension()
&& established != dimensions
{
return Err(crate::error::AllSourceError::ValidationError(format!(
"embedding dimension mismatch for `{node_entity_id}`: this tenant already \
holds {established}-dim vectors, but got a {dimensions}-dim vector. \
Mixing dimensions corrupts similarity search."
)));
}
let payload = json!({
"text": serde_json::Value::Null,
"dimensions": dimensions,
"metadata": metadata,
});
self.core_for(tenant)
.ingest(IngestEvent {
entity_id: &entity_id,
event_type: vec_events::VECTOR_STORED,
payload: payload.clone(),
metadata: Some(json!({ "embedding": vector })),
tenant_id: Some(tenant),
})
.await?;
// Fold into the warm bundle. The vector index reads the embedding from
// event metadata, so the synthesized view must carry it there too.
self.cache.apply(
tenant,
&self.synth_vector_view(tenant, &entity_id, payload, &vector),
);
Ok(())
}
/// Lazily-initialized in-process text embedder (mirrors facade `embedder()`).
/// First call downloads the model — never exercise this in tests.
#[cfg(feature = "prime-vectors")]
fn embedder(&self) -> Result<&Arc<crate::prime::vectors::TextEmbedder>> {
if let Some(e) = self.embedder.get() {
return Ok(e);
}
let new = Arc::new(
crate::prime::vectors::TextEmbedder::new()
.map_err(|e| crate::error::AllSourceError::InternalError(e.to_string()))?,
);
let _ = self.embedder.set(new);
Ok(self
.embedder
.get()
.expect("embedder was just set or already initialized"))
}
/// Embed `text` with the in-process model, then store the resulting vector
/// via [`embed`](Self::embed). Convenience for callers that only have text.
/// First use downloads the embedding model.
#[cfg(feature = "prime-vectors")]
pub async fn embed_text(&self, tenant: &str, node_entity_id: &str, text: &str) -> Result<()> {
let vector = self
.embedder()?
.embed(text)
.map_err(|e| crate::error::AllSourceError::InternalError(e.to_string()))?;
self.embed(tenant, node_entity_id, vector, None).await
}
/// Semantic recall over the tenant's warm bundle: run a vector similarity
/// search and hydrate the matched graph nodes from `node_state`.
///
/// Returns `(Node, similarity)` pairs ordered by descending similarity,
/// `similarity = 1.0 - cosine_distance` (matching facade's `vector_search`).
/// Implemented as direct `vector_index.search` + node hydration rather than
/// `RecallEngine::with_deps`: the embedded `Prime::recall` itself does not use
/// the engine — it composes `vector_search` + `get_node` + graph expansion
/// against its own projections — so the engine path would not mirror facade,
/// while this path replicates facade's vector→node hydration exactly.
#[cfg(feature = "prime-recall")]
#[cfg(feature = "prime-vectors")]
// Signature (owned `Vec<f32>` query) is fixed by the bead spec to mirror the
// facade's recall entry point; the index search borrows it.
#[allow(clippy::needless_pass_by_value)]
pub async fn recall(
&self,
tenant: &str,
query_vector: Vec<f32>,
top_k: usize,
) -> Result<Vec<(Node, f64)>> {
let g = self.cache.get_or_hydrate(tenant).await?;
let hits = g.vector_index.search(&query_vector, top_k);
let mut out = Vec::with_capacity(hits.len());
for hit in hits {
// Vector entity_id is `vec:{graph_entity_id}` — strip the prefix to
// recover the node's entity id (mirrors facade recall).
let graph_id = hit.entity_id.strip_prefix("vec:").unwrap_or(&hit.entity_id);
if let Some(node) = g.node_state.get_node(graph_id).filter(|n| !n.deleted) {
let similarity = 1.0 - f64::from(hit.distance);
out.push((node, similarity));
}
}
Ok(out)
}
/// Synthesize a local [`EventView`] for a vector-stored event, carrying the
/// embedding in metadata so the warm bundle's `VectorIndexProjection` folds
/// it (it reads the vector from `metadata.embedding`, not the payload).
#[cfg(feature = "prime-vectors")]
fn synth_vector_view(
&self,
tenant: &str,
entity_id: &str,
payload: serde_json::Value,
vector: &[f32],
) -> EventView {
EventView {
id: uuid::Uuid::new_v4(),
event_type: crate::prime::vectors::event_types::VECTOR_STORED.to_string(),
entity_id: entity_id.to_string(),
tenant_id: tenant.to_string(),
payload,
metadata: Some(json!({ "embedding": vector })),
timestamp: chrono::Utc::now(),
version: 0,
}
}
/// Delete a stored vector: ingest a `prime.vector.deleted` event
/// (tenant-stamped) and apply it so recall no longer returns it. Mirrors the
/// embedded `Prime::delete_vector` (`vec:{node_entity_id}` entity, `{}` payload).
#[cfg(feature = "prime-vectors")]
pub async fn delete_vector(&self, tenant: &str, node_entity_id: &str) -> Result<()> {
let entity_id = crate::prime::vectors::vector_entity_id(node_entity_id);
self.core_for(tenant)
.ingest(IngestEvent {
entity_id: &entity_id,
event_type: crate::prime::vectors::event_types::VECTOR_DELETED,
payload: json!({}),
metadata: None,
tenant_id: Some(tenant),
})
.await?;
self.cache.apply(
tenant,
&self.synth_view_typed(
tenant,
crate::prime::vectors::event_types::VECTOR_DELETED,
&entity_id,
json!({}),
),
);
Ok(())
}
/// Materialize the tenant's complete graph (nodes + edges + stats), mirroring
/// the embedded `Prime::full_graph`. The bundle is already single-tenant
/// (events were queried tenant-scoped), so no per-node `tenant_id` filter is
/// needed here. `node_type` filters to one type; `limit` caps nodes and sets
/// `has_more`; edges are restricted to the returned node set.
pub async fn full_graph(
&self,
tenant: &str,
node_type: Option<&str>,
limit: Option<usize>,
) -> Result<crate::prime::types::FullGraph> {
use crate::prime::types::{EntityId, FullGraph, GraphEdge, GraphNode, GraphStats};
use std::collections::{BTreeMap, HashSet};
let g = self.cache.get_or_hydrate(tenant).await?;
let mut live: Vec<Node> = g
.node_state
.all_nodes()
.into_iter()
.filter(|n| node_type.is_none_or(|nt| n.node_type == nt))
.collect();
live.sort_by(|a, b| a.id.as_str().cmp(b.id.as_str()));
let mut nodes_by_type: BTreeMap<String, usize> = BTreeMap::new();
for n in &live {
*nodes_by_type.entry(n.node_type.clone()).or_default() += 1;
}
let has_more = limit.is_some_and(|l| live.len() > l);
if let Some(l) = limit {
live.truncate(l);
}
let included: HashSet<String> = live
.iter()
.map(|n| EntityId::node(&n.node_type, n.id.as_str()).to_wire())
.collect();
let mut vector_count = 0usize;
let nodes: Vec<GraphNode> = live
.iter()
.map(|n| {
let wire = EntityId::node(&n.node_type, n.id.as_str()).to_wire();
let (has_vector, vector_dim) = self.vector_presence(&g, &wire);
if has_vector {
vector_count += 1;
}
GraphNode {
id: wire,
node_type: n.node_type.clone(),
properties: n.properties.clone(),
has_vector,
vector_dim,
created_at: n.created_at,
updated_at: n.updated_at,
}
})
.collect();
let edges: Vec<GraphEdge> = g
.adjacency
.all_edges()
.into_iter()
.filter(|(source, adj)| included.contains(source) && included.contains(&adj.peer))
.map(|(source, adj)| GraphEdge {
source,
target: adj.peer,
relation: adj.relation,
properties: None,
weight: adj.weight,
created_at: chrono::Utc::now(),
})
.collect();
let stats = GraphStats {
node_count: nodes.len(),
edge_count: edges.len(),
vector_count,
nodes_by_type,
};
Ok(FullGraph {
nodes,
edges,
stats,
has_more,
})
}
/// `(has_vector, dimension)` for a node wire-id over the warm bundle's vector
/// index. Always `(false, None)` without the `prime-vectors` feature.
#[allow(unused_variables)]
fn vector_presence(&self, g: &GraphProjections, node_wire_id: &str) -> (bool, Option<usize>) {
#[cfg(feature = "prime-vectors")]
{
use crate::application::services::projection::Projection;
let vec_entity = crate::prime::vectors::vector_entity_id(node_wire_id);
if let Some(state) = g.vector_index.get_state(&vec_entity) {
let dim = state
.get("vector")
.and_then(serde_json::Value::as_array)
.map(Vec::len)
.or_else(|| {
state
.get("dimensions")
.and_then(serde_json::Value::as_u64)
.map(|d| d as usize)
});
return (true, dim);
}
}
(false, None)
}
/// Synthesize a local [`EventView`] for cache application. The remote Core
/// assigns the authoritative id/version/timestamp; the projections only key
/// on entity_id/event_type/payload, so a local stand-in is sufficient until
/// the next hydrate reconciles from Core.
fn synth_view_typed(
&self,
tenant: &str,
event_type: &str,
entity_id: &str,
payload: serde_json::Value,
) -> EventView {
EventView {
id: uuid::Uuid::new_v4(),
event_type: event_type.to_string(),
entity_id: entity_id.to_string(),
tenant_id: tenant.to_string(),
payload,
metadata: None,
timestamp: chrono::Utc::now(),
version: 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use wiremock::{
Mock, MockServer, ResponseTemplate,
matchers::{method, path},
};
#[tokio::test]
async fn add_node_then_get_node_round_trips_through_warm_cache() {
let server = MockServer::start().await;
// Core has no prior events for this tenant…
Mock::given(method("GET"))
.and(path("/api/v1/events/query"))
.respond_with(
ResponseTemplate::new(200).set_body_json(serde_json::json!({"events": []})),
)
.mount(&server)
.await;
// …and accepts the ingest.
Mock::given(method("POST"))
.and(path("/api/v1/events"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"ok": true})))
.mount(&server)
.await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("tenant-a").await.unwrap();
let id = hosted
.add_node("tenant-a", "contact", serde_json::json!({"name": "Alice"}))
.await
.unwrap();
// The node is visible from the warm bundle even though Core's query
// returned empty — proving the write updated the cache, not just Core.
let entity_id = node_entity_id("contact", &id.0);
let node = hosted.get_node("tenant-a", &entity_id).await.unwrap();
assert!(node.is_some(), "node should be readable after add_node");
assert_eq!(node.unwrap().properties["name"], "Alice");
}
/// Mount the standard pair: GET query → empty, POST ingest → 200. Proving a
/// read after a write succeeds therefore proves the warm cache was updated,
/// not that Core re-served the event.
async fn mount_empty_core(server: &MockServer) {
Mock::given(method("GET"))
.and(path("/api/v1/events/query"))
.respond_with(
ResponseTemplate::new(200).set_body_json(serde_json::json!({"events": []})),
)
.mount(server)
.await;
Mock::given(method("POST"))
.and(path("/api/v1/events"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"ok": true})))
.mount(server)
.await;
}
#[tokio::test]
async fn add_edge_then_neighbors_returns_target() {
let server = MockServer::start().await;
mount_empty_core(&server).await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("tenant-a").await.unwrap();
let alice = hosted
.add_node("tenant-a", "contact", serde_json::json!({"name": "Alice"}))
.await
.unwrap();
let bob = hosted
.add_node("tenant-a", "contact", serde_json::json!({"name": "Bob"}))
.await
.unwrap();
let alice_eid = node_entity_id("contact", &alice.0);
let bob_eid = node_entity_id("contact", &bob.0);
hosted
.add_edge("tenant-a", &alice_eid, &bob_eid, "knows", None)
.await
.unwrap();
// Outgoing neighbors of Alice should be Bob, served entirely from the
// warm cache (Core's query is mocked empty).
let out = hosted
.neighbors("tenant-a", &alice_eid, None, Direction::Outgoing)
.await
.unwrap();
assert_eq!(out.len(), 1, "alice should have one outgoing neighbor");
assert_eq!(out[0].properties["name"], "Bob");
// And Bob's incoming neighbor is Alice.
let inc = hosted
.neighbors("tenant-a", &bob_eid, Some("knows"), Direction::Incoming)
.await
.unwrap();
assert_eq!(inc.len(), 1);
assert_eq!(inc[0].properties["name"], "Alice");
}
#[tokio::test]
async fn add_nodes_then_search_returns_all_of_type() {
let server = MockServer::start().await;
mount_empty_core(&server).await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("tenant-a").await.unwrap();
hosted
.add_node("tenant-a", "contact", serde_json::json!({"name": "Alice"}))
.await
.unwrap();
hosted
.add_node("tenant-a", "contact", serde_json::json!({"name": "Bob"}))
.await
.unwrap();
// A node of a different type must not show up in the search.
hosted
.add_node("tenant-a", "company", serde_json::json!({"name": "Acme"}))
.await
.unwrap();
let contacts = hosted.search("tenant-a", "contact").await.unwrap();
assert_eq!(contacts.len(), 2, "both contacts should be returned");
let names: std::collections::HashSet<&str> = contacts
.iter()
.map(|n| n.properties["name"].as_str().unwrap())
.collect();
assert!(names.contains("Alice"));
assert!(names.contains("Bob"));
}
#[tokio::test]
async fn stats_reflects_added_nodes_and_edges() {
let server = MockServer::start().await;
mount_empty_core(&server).await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("tenant-a").await.unwrap();
let a = hosted
.add_node("tenant-a", "contact", serde_json::json!({"name": "Alice"}))
.await
.unwrap();
let b = hosted
.add_node("tenant-a", "contact", serde_json::json!({"name": "Bob"}))
.await
.unwrap();
hosted
.add_edge(
"tenant-a",
&node_entity_id("contact", &a.0),
&node_entity_id("contact", &b.0),
"knows",
None,
)
.await
.unwrap();
let stats = hosted.stats("tenant-a").await.unwrap();
assert_eq!(stats.total_nodes, 2);
assert_eq!(stats.total_edges, 1);
}
#[tokio::test]
async fn delete_node_then_get_node_returns_none() {
let server = MockServer::start().await;
mount_empty_core(&server).await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("tenant-a").await.unwrap();
let id = hosted
.add_node("tenant-a", "contact", serde_json::json!({"name": "Alice"}))
.await
.unwrap();
let entity_id = node_entity_id("contact", &id.0);
assert!(
hosted
.get_node("tenant-a", &entity_id)
.await
.unwrap()
.is_some()
);
hosted.delete_node("tenant-a", &entity_id).await.unwrap();
// The delete folded into the warm bundle — get_node returns None even
// though Core's query is mocked empty.
let node = hosted.get_node("tenant-a", &entity_id).await.unwrap();
assert!(node.is_none(), "node should be gone after delete_node");
}
#[cfg(feature = "prime-vectors")]
#[tokio::test]
async fn embed_then_recall_returns_the_embedded_node() {
let server = MockServer::start().await;
mount_empty_core(&server).await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("tenant-a").await.unwrap();
// A real graph node so recall can hydrate it from node_state.
let id = hosted
.add_node("tenant-a", "contact", serde_json::json!({"name": "Alice"}))
.await
.unwrap();
let entity_id = node_entity_id("contact", &id.0);
// Explicit 4-dim vector — never call TextEmbedder in tests.
hosted
.embed("tenant-a", &entity_id, vec![1.0, 0.0, 0.0, 0.0], None)
.await
.unwrap();
// A near-identical query vector should return Alice from the warm bundle
// (Core's query is mocked empty — proving the vector folded into the cache).
let results = hosted
.recall("tenant-a", vec![1.0, 0.0, 0.0, 0.0], 5)
.await
.unwrap();
assert_eq!(results.len(), 1, "the embedded node should be recalled");
assert_eq!(results[0].0.properties["name"], "Alice");
assert!(
results[0].1 > 0.99,
"identical vectors should score ~1.0, got {}",
results[0].1
);
}
#[cfg(feature = "prime-vectors")]
#[tokio::test]
async fn recall_is_isolated_per_tenant() {
let server = MockServer::start().await;
mount_empty_core(&server).await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("tenant-a").await.unwrap();
// tenant-a embeds a node…
let a_id = hosted
.add_node("tenant-a", "contact", serde_json::json!({"name": "Alice"}))
.await
.unwrap();
let a_eid = node_entity_id("contact", &a_id.0);
hosted
.embed("tenant-a", &a_eid, vec![1.0, 0.0, 0.0, 0.0], None)
.await
.unwrap();
// …and tenant-b recalls with the same vector but has no embeddings.
let b_results = hosted
.recall("tenant-b", vec![1.0, 0.0, 0.0, 0.0], 5)
.await
.unwrap();
assert!(
b_results.is_empty(),
"tenant-b must not see tenant-a's embedded vector"
);
// tenant-a still sees its own.
let a_results = hosted
.recall("tenant-a", vec![1.0, 0.0, 0.0, 0.0], 5)
.await
.unwrap();
assert_eq!(a_results.len(), 1);
assert_eq!(a_results[0].0.properties["name"], "Alice");
}
#[tokio::test]
async fn get_node_absent_returns_none() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/api/v1/events/query"))
.respond_with(
ResponseTemplate::new(200).set_body_json(serde_json::json!({"events": []})),
)
.mount(&server)
.await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("t").await.unwrap();
let node = hosted
.get_node("tenant-a", "node:contact:ghost")
.await
.unwrap();
assert!(node.is_none());
}
/// A wiremock Core that has no prior events and accepts ingests — the
/// standard fixture proving warm-cache round-trips (Core query is empty, so
/// any read result came from the cache the write updated).
async fn empty_core() -> MockServer {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/api/v1/events/query"))
.respond_with(
ResponseTemplate::new(200).set_body_json(serde_json::json!({"events": []})),
)
.mount(&server)
.await;
Mock::given(method("POST"))
.and(path("/api/v1/events"))
.respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({"ok": true})))
.mount(&server)
.await;
server
}
#[tokio::test]
async fn update_node_merges_properties_in_warm_bundle() {
let server = empty_core().await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("t").await.unwrap();
let id = hosted
.add_node("t", "contact", serde_json::json!({"name": "Alice"}))
.await
.unwrap();
let eid = node_entity_id("contact", &id.0);
hosted
.update_node("t", &eid, serde_json::json!({"role": "eng"}))
.await
.unwrap();
let node = hosted.get_node("t", &eid).await.unwrap().unwrap();
assert_eq!(node.properties["name"], "Alice"); // preserved (deep merge)
assert_eq!(node.properties["role"], "eng"); // added
}
#[tokio::test]
async fn delete_edge_removes_it_from_neighbors() {
let server = empty_core().await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("t").await.unwrap();
let a = node_entity_id(
"contact",
&hosted
.add_node("t", "contact", serde_json::json!({}))
.await
.unwrap()
.0,
);
let b = node_entity_id(
"contact",
&hosted
.add_node("t", "contact", serde_json::json!({}))
.await
.unwrap()
.0,
);
let edge = hosted.add_edge("t", &a, &b, "knows", None).await.unwrap();
assert_eq!(
hosted
.neighbors("t", &a, None, Direction::Outgoing)
.await
.unwrap()
.len(),
1
);
hosted.delete_edge("t", &edge.0).await.unwrap();
assert!(
hosted
.neighbors("t", &a, None, Direction::Outgoing)
.await
.unwrap()
.is_empty()
);
}
#[tokio::test]
async fn shortest_path_finds_the_chain() {
let server = empty_core().await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("t").await.unwrap();
let na = hosted
.add_node("t", "n", serde_json::json!({}))
.await
.unwrap()
.0;
let nb = hosted
.add_node("t", "n", serde_json::json!({}))
.await
.unwrap()
.0;
let nc = hosted
.add_node("t", "n", serde_json::json!({}))
.await
.unwrap()
.0;
let (a, b, c) = (
node_entity_id("n", &na),
node_entity_id("n", &nb),
node_entity_id("n", &nc),
);
hosted.add_edge("t", &a, &b, "to", None).await.unwrap();
hosted.add_edge("t", &b, &c, "to", None).await.unwrap();
let path = hosted
.shortest_path("t", &a, &c, None)
.await
.unwrap()
.unwrap();
// Node.id is the short id; assert the BFS returned the a→b→c chain in order.
let ids: Vec<&str> = path.iter().map(|n| n.id.as_str()).collect();
assert_eq!(ids, vec![na.as_str(), nb.as_str(), nc.as_str()]);
}
#[tokio::test]
async fn full_graph_returns_tenant_nodes_and_edges() {
let server = empty_core().await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("t").await.unwrap();
let a = node_entity_id(
"contact",
&hosted
.add_node("t", "contact", serde_json::json!({}))
.await
.unwrap()
.0,
);
let b = node_entity_id(
"contact",
&hosted
.add_node("t", "contact", serde_json::json!({}))
.await
.unwrap()
.0,
);
hosted.add_edge("t", &a, &b, "knows", None).await.unwrap();
let graph = hosted.full_graph("t", None, None).await.unwrap();
assert_eq!(graph.stats.node_count, 2);
assert_eq!(graph.stats.edge_count, 1);
assert!(!graph.has_more);
}
#[cfg(feature = "prime-vectors")]
#[tokio::test]
async fn delete_vector_removes_it_from_recall() {
let server = empty_core().await;
let hosted = HostedPrime::connect(server.uri(), None, 8, Duration::from_secs(60));
// Pre-warm the tenant so post-write apply() lands on a warm bundle
// (cold writes no-op apply; reads then hydrate from Core). See bug t-d90426.
hosted.tenant_graph("t").await.unwrap();
let nid = hosted
.add_node("t", "n", serde_json::json!({}))
.await
.unwrap()
.0;
let eid = node_entity_id("n", &nid);
hosted
.embed("t", &eid, vec![1.0, 0.0, 0.0, 0.0], None)
.await
.unwrap();
assert!(
!hosted
.recall("t", vec![1.0, 0.0, 0.0, 0.0], 5)
.await
.unwrap()
.is_empty()
);
hosted.delete_vector("t", &eid).await.unwrap();
assert!(
hosted
.recall("t", vec![1.0, 0.0, 0.0, 0.0], 5)
.await
.unwrap()
.is_empty()
);
}
}