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
//! CoordinatorService trait seam — dependency inversion for ADR-029 Phase 2.
//!
//! `khive-mcp` defines the contract; `kkernel` provides the concrete implementation.
//! This avoids a crate-cycle: kkernel depends on khive-mcp, so khive-mcp cannot
//! depend on kkernel. The trait is the stable boundary.
use std::fmt;
use async_trait::async_trait;
use uuid::Uuid;
use khive_runtime::Namespace;
use khive_runtime::{BackendId, NoteSearchHit, SearchHit};
use khive_storage::{Edge, EdgeRelation};
/// Result of a cross-backend link operation.
pub struct CoordLinkResult {
/// The edge that was written (on the source backend).
pub edge: Edge,
/// True when source and target are on different backends.
pub cross_backend: bool,
/// The target backend id when `cross_backend` is true.
pub target_backend_id: Option<BackendId>,
}
/// Error variants the coordinator can produce.
pub enum CoordError {
/// The given UUID was not found on any registered backend.
UnknownNode { id: Uuid },
/// The proposed edge violates ADR-002 endpoint rules.
EdgeRuleViolation(String),
/// A backend operation failed.
Backend(String),
}
impl fmt::Display for CoordError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CoordError::UnknownNode { id } => write!(f, "node {id} not found on any backend"),
CoordError::EdgeRuleViolation(msg) => write!(f, "edge rule violation: {msg}"),
CoordError::Backend(msg) => write!(f, "backend error: {msg}"),
}
}
}
impl fmt::Debug for CoordError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl From<CoordError> for khive_runtime::RuntimeError {
fn from(e: CoordError) -> Self {
match e {
CoordError::UnknownNode { id } => {
khive_runtime::RuntimeError::NotFound(format!("node {id} not found on any backend"))
}
CoordError::EdgeRuleViolation(msg) => khive_runtime::RuntimeError::InvalidInput(msg),
CoordError::Backend(msg) => khive_runtime::RuntimeError::Internal(msg),
}
}
}
/// Per-backend contribution to a fan-out search.
pub struct BackendSearchResult {
pub backend_id: BackendId,
pub entity_hits: Vec<SearchHit>,
pub note_hits: Vec<NoteSearchHit>,
/// Populated when this backend errored during the fan-out.
pub error: Option<String>,
}
/// Merged fan-out search result.
pub struct CoordSearchResult {
/// RRF-merged entity hits across all backends.
pub entity_hits: Vec<SearchHit>,
/// RRF-merged note hits across all backends.
pub note_hits: Vec<NoteSearchHit>,
/// Per-backend detail (for diagnostics).
pub per_backend: Vec<BackendSearchResult>,
/// True when at least one backend errored (results may be incomplete).
pub partial: bool,
/// Kind string for each entity hit, keyed by entity UUID.
/// Populated by the coordinator after the RRF merge. Missing entries mean
/// the kind could not be resolved (e.g. the owning backend errored).
pub entity_kinds: std::collections::HashMap<uuid::Uuid, String>,
/// Kind string for each note hit, keyed by note UUID.
/// Populated by the coordinator after the RRF merge.
pub note_kinds: std::collections::HashMap<uuid::Uuid, String>,
}
/// Cross-backend coordinator seam visible to `khive-mcp`.
///
/// Implemented by `kkernel::coordinator::SubstrateCoordinatorService`.
/// `khive-mcp` holds an `Option<Arc<dyn CoordinatorService>>` and calls through
/// when in multi-backend mode; single-backend servers hold `None` and dispatch
/// through the `VerbRegistry` unchanged (zero-change invariant).
#[async_trait]
pub trait CoordinatorService: Send + Sync {
/// Resolve the owning backend for a UUID.
///
/// Namespace-agnostic per ADR-007 Rev 3: presence of the record in a backend
/// is sufficient — the record's stored namespace is not compared to the caller.
async fn locate(&self, id: Uuid) -> Option<BackendId>;
/// Prewarm the locator cache after a successful create so the first
/// `locate()` for the new record is a cache hit rather than a backend scan.
fn record_created(&self, id: Uuid, backend_id: BackendId);
/// The primary backend id (used to prewarm after create).
fn primary_backend_id(&self) -> Option<BackendId>;
/// Cross-backend link (D3). Locates both endpoints, validates the relation,
/// and writes the edge on the source backend with `target_backend` stamped
/// when the endpoints are on different backends.
async fn link(
&self,
namespace: &Namespace,
source_id: Uuid,
target_id: Uuid,
relation: EdgeRelation,
weight: f64,
metadata: Option<serde_json::Value>,
) -> Result<CoordLinkResult, CoordError>;
/// Fan-out search across all registered backends (D4).
///
/// `kind` controls which substrate to search:
/// - `"entity"` or any granular entity kind → entity fan-out via `hybrid_search`
/// - `"note"` or any granular note kind → note fan-out via `search_notes`
///
/// `kind_filter` is the granular kind to pass as a storage-level filter
/// (`entity_kind` for entity substrate, `note_kind` for note substrate).
/// Pass `None` for substrate-level (`kind="entity"` or `kind="note"`) searches.
///
/// `props_filter` and `tags` are entity-substrate filters forwarded to each
/// backend's `hybrid_search`. When either is active the per-backend candidate
/// window is widened so that sparse matches ranked below the bare `limit` are
/// not cut off before filtering (mirrors the single-backend handler).
/// Both are ignored for note-substrate searches.
///
/// Granular kinds that cannot be resolved to a substrate fall through to the
/// registry (single-backend path); the coordinator does not silently drop results.
#[allow(clippy::too_many_arguments)]
async fn fan_out_search(
&self,
kind: &str,
query: &str,
namespace: &Namespace,
limit: u32,
kind_filter: Option<&str>,
props_filter: Option<&serde_json::Value>,
tags: &[String],
) -> CoordSearchResult;
/// True when only one backend is registered (zero-change invariant check).
fn is_single_backend(&self) -> bool;
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use std::sync::Arc;
/// Minimal mock for server-routing tests (T6 in the test plan).
pub struct MockCoordinator {
pub link_called: std::sync::atomic::AtomicBool,
pub search_called: std::sync::atomic::AtomicBool,
pub single_backend: bool,
}
impl MockCoordinator {
pub fn multi_backend() -> Arc<Self> {
Arc::new(Self {
link_called: std::sync::atomic::AtomicBool::new(false),
search_called: std::sync::atomic::AtomicBool::new(false),
single_backend: false,
})
}
pub fn single_backend_instance() -> Arc<Self> {
Arc::new(Self {
link_called: std::sync::atomic::AtomicBool::new(false),
search_called: std::sync::atomic::AtomicBool::new(false),
single_backend: true,
})
}
}
#[async_trait]
impl CoordinatorService for MockCoordinator {
async fn locate(&self, _id: Uuid) -> Option<BackendId> {
Some(BackendId::main())
}
fn record_created(&self, _id: Uuid, _backend_id: BackendId) {}
fn primary_backend_id(&self) -> Option<BackendId> {
Some(BackendId::main())
}
async fn link(
&self,
_namespace: &Namespace,
_source_id: Uuid,
_target_id: Uuid,
_relation: EdgeRelation,
_weight: f64,
_metadata: Option<serde_json::Value>,
) -> Result<CoordLinkResult, CoordError> {
self.link_called
.store(true, std::sync::atomic::Ordering::SeqCst);
Err(CoordError::UnknownNode { id: Uuid::new_v4() })
}
async fn fan_out_search(
&self,
_kind: &str,
_query: &str,
_namespace: &Namespace,
_limit: u32,
_kind_filter: Option<&str>,
_props_filter: Option<&serde_json::Value>,
_tags: &[String],
) -> CoordSearchResult {
self.search_called
.store(true, std::sync::atomic::Ordering::SeqCst);
CoordSearchResult {
entity_hits: vec![],
note_hits: vec![],
per_backend: vec![],
partial: false,
entity_kinds: std::collections::HashMap::new(),
note_kinds: std::collections::HashMap::new(),
}
}
fn is_single_backend(&self) -> bool {
self.single_backend
}
}
// ── T6: server-level coordinator routing ─────────────────────────────────
use crate::server::KhiveMcpServer;
use crate::tools::request::RequestParams;
use khive_runtime::{KhiveRuntime, Namespace as RuntimeNamespace, RuntimeConfig};
fn make_registry() -> (khive_runtime::VerbRegistry, khive_runtime::KhiveRuntime) {
let config = RuntimeConfig {
db_path: None,
default_namespace: RuntimeNamespace::parse("local").unwrap(),
embedding_model: None,
additional_embedding_models: vec![],
packs: vec!["kg".to_string()],
..RuntimeConfig::default()
};
let runtime = KhiveRuntime::new(config).expect("in-memory runtime");
let gate = runtime.config().gate.clone();
let default_ns = runtime.config().default_namespace.clone();
let actor_id = runtime.config().actor_id.clone();
let mut builder = khive_runtime::VerbRegistryBuilder::new();
builder.with_gate(gate);
builder.with_default_namespace(default_ns.as_str());
builder.with_actor_id(actor_id);
khive_runtime::PackRegistry::register_packs(
&["kg".to_string()],
runtime.clone(),
&mut builder,
)
.expect("register kg");
let registry = builder.build().expect("build registry");
runtime.install_edge_rules(registry.all_edge_rules());
(registry, runtime)
}
/// T6a: A multi-backend server MUST route `link` through the coordinator.
///
/// This test must FAIL before BLOCKER-1 is wired (coordinator never called)
/// and PASS after wiring.
#[tokio::test]
async fn t6a_multi_backend_server_routes_link_through_coordinator() {
let (registry, _runtime) = make_registry();
let coord = MockCoordinator::multi_backend();
let server = KhiveMcpServer::from_registry_with_meta(registry, "local", "test-cfg")
.with_coordinator(Arc::clone(&coord) as Arc<dyn CoordinatorService>);
let src_id = Uuid::new_v4();
let tgt_id = Uuid::new_v4();
let ops = format!(
r#"link(source_id="{}", target_id="{}", relation="implements")"#,
src_id, tgt_id
);
let _result = server
.dispatch_request_local(RequestParams {
ops,
presentation: None,
presentation_per_op: None,
save_to: None,
format: None,
format_per_op: None,
})
.await;
assert!(
coord
.link_called
.load(std::sync::atomic::Ordering::SeqCst),
"T6a: coordinator.link must be called when a link op is dispatched through a multi-backend server"
);
}
/// T6b: A multi-backend server MUST route `search` through the coordinator.
///
/// This test must FAIL before BLOCKER-1 is wired and PASS after wiring.
#[tokio::test]
async fn t6b_multi_backend_server_routes_search_through_coordinator() {
let (registry, _runtime) = make_registry();
let coord = MockCoordinator::multi_backend();
let server = KhiveMcpServer::from_registry_with_meta(registry, "local", "test-cfg")
.with_coordinator(Arc::clone(&coord) as Arc<dyn CoordinatorService>);
let _result = server
.dispatch_request_local(RequestParams {
ops: r#"search(kind="entity", query="anything")"#.to_string(),
presentation: None,
presentation_per_op: None,
save_to: None,
format: None,
format_per_op: None,
})
.await;
assert!(
coord
.search_called
.load(std::sync::atomic::Ordering::SeqCst),
"T6b: coordinator.fan_out_search must be called when a search op is dispatched through a multi-backend server"
);
}
/// T6d: A multi-backend search with a malformed `tags` value must return a
/// per-op error (`ok: false`) rather than silently returning unfiltered results.
///
/// Single-backend rejects malformed tags via `SearchParams` deserialisation
/// (RuntimeError::InvalidInput → `ok: false`). Multi-backend must match that
/// contract: the server must reject before reaching the coordinator, not silently
/// collapse the filter to an empty Vec.
///
/// This test FAILS against the old `filter_map(as_str)` code (which would call
/// the coordinator with an empty tags Vec and return `ok: true, result: []`),
/// and PASSES after the strict `serde_json::from_value::<Vec<String>>` fix.
#[tokio::test]
async fn t6d_malformed_tags_return_per_op_error_in_multi_backend() {
let (registry, _runtime) = make_registry();
let coord = MockCoordinator::multi_backend();
let server = KhiveMcpServer::from_registry_with_meta(registry, "local", "test-cfg")
.with_coordinator(Arc::clone(&coord) as Arc<dyn CoordinatorService>);
// Pass a non-string entry in the tags array; the strict parser must reject this.
let raw = server
.dispatch_request_local(RequestParams {
ops: r#"search(kind="entity", query="anything", tags=[42])"#.to_string(),
presentation: None,
presentation_per_op: None,
save_to: None,
format: None,
format_per_op: None,
})
.await
.expect("T6d: dispatch must not return an MCP-level error");
let result_val: serde_json::Value =
serde_json::from_str(&raw).expect("T6d: response must be valid JSON");
let first = result_val
.get("results")
.and_then(|r| r.as_array())
.and_then(|a| a.first())
.expect("T6d: results array must be non-empty");
assert_eq!(
first.get("ok").and_then(serde_json::Value::as_bool),
Some(false),
"T6d: malformed tags must produce ok=false; got {:?}",
first
);
// The coordinator must NOT have been called — rejection happens before dispatch.
assert!(
!coord
.search_called
.load(std::sync::atomic::Ordering::SeqCst),
"T6d: coordinator must not be reached when tags validation fails"
);
}
/// T6c: A single-backend server must NOT route through the coordinator.
///
/// When the coordinator reports `is_single_backend() == true`, the zero-change
/// invariant requires the registry path (unchanged from pre-coordinator code).
#[tokio::test]
async fn t6c_single_backend_server_bypasses_coordinator() {
let (registry, runtime) = make_registry();
let coord = MockCoordinator::single_backend_instance();
let server = KhiveMcpServer::from_registry_with_meta(registry, "local", "test-cfg")
.with_coordinator(Arc::clone(&coord) as Arc<dyn CoordinatorService>);
// Create a real entity so the search op succeeds via registry.
let ns = RuntimeNamespace::local();
let token = runtime.authorize(ns).expect("authorize");
let entity = runtime
.create_entity(&token, "concept", None, "T6cEntity", None, None, vec![])
.await
.expect("create entity");
let _ = entity;
let _result = server
.dispatch_request_local(RequestParams {
ops: r#"search(kind="entity", query="T6cEntity")"#.to_string(),
presentation: None,
presentation_per_op: None,
save_to: None,
format: None,
format_per_op: None,
})
.await;
assert!(
!coord
.search_called
.load(std::sync::atomic::Ordering::SeqCst),
"T6c: coordinator.fan_out_search must NOT be called for a single-backend server"
);
assert!(
!coord.link_called.load(std::sync::atomic::Ordering::SeqCst),
"T6c: coordinator.link must NOT be called for a single-backend server"
);
}
}