khive_runtime/runtime.rs
1//! KhiveRuntime — composable handle to all storage capabilities.
2//!
3//! `RuntimeConfig`, `BackendId`, `NamespaceToken`, and embedding model helpers
4//! live in `super::config` and are re-exported from here.
5
6use std::collections::{HashMap, HashSet};
7use std::future::Future;
8use std::path::PathBuf;
9use std::sync::{Arc, Mutex, OnceLock, RwLock, Weak};
10
11use khive_db::{ConnectionPool, StorageBackend};
12#[cfg(test)]
13use khive_gate::AllowAllGate;
14use khive_gate::GateRequest;
15use khive_storage::types::{SqlStatement, SqlValue};
16use khive_storage::{
17 AttachmentStore, EntityStore, Event, EventStore, GraphStore, NoteStore, SqlAccess, VectorStore,
18};
19use khive_types::{EdgeEndpointRule, EventKind, Namespace, SubstrateKind};
20use lattice_embed::{EmbeddingModel, EmbeddingService};
21
22#[cfg(test)]
23use crate::config::vec_model_key;
24use crate::config::{
25 build_embedder_registry, parse_embedding_model_alias, register_configured_embedding_models,
26 sanitize_key,
27};
28use crate::error::{RuntimeError, RuntimeResult};
29use crate::note_search_ann::NoteSearchAnnProvider;
30use crate::pack::KindHook;
31
32mod close;
33#[path = "runtime/config_access.rs"]
34mod config_access;
35pub use close::{
36 RuntimeCloseError, RuntimeCloseHostTasks, RuntimeCloseOwner, RuntimeClosePhase,
37 RuntimeCloseRoot, RuntimeCloseRootKind,
38};
39mod embedder_init;
40mod engine_binding;
41mod events_disk_policy;
42mod serving_policy;
43
44#[cfg(all(test, target_os = "macos"))]
45const IN_PROCESS_TEST_NOFILE_LIMIT: libc::rlim_t = 4096;
46#[cfg(all(test, target_os = "macos"))]
47static IN_PROCESS_TEST_NOFILE_INIT: std::sync::Once = std::sync::Once::new();
48
49#[cfg(all(test, target_os = "macos"))]
50fn ensure_in_process_test_nofile_limit() {
51 IN_PROCESS_TEST_NOFILE_INIT.call_once(|| {
52 let mut limits = libc::rlimit {
53 rlim_cur: 0,
54 rlim_max: 0,
55 };
56 // SAFETY: `limits` is writable, and only this test binary's soft
57 // limit may change; the inherited hard limit is preserved.
58 assert_eq!(unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits) }, 0);
59 assert!(
60 limits.rlim_max >= IN_PROCESS_TEST_NOFILE_LIMIT,
61 "in-process SQLite tests require a hard open-file limit of at least {IN_PROCESS_TEST_NOFILE_LIMIT}"
62 );
63 if limits.rlim_cur < IN_PROCESS_TEST_NOFILE_LIMIT {
64 limits.rlim_cur = IN_PROCESS_TEST_NOFILE_LIMIT;
65 // SAFETY: the new soft limit does not exceed the observed hard
66 // limit, which is left unchanged.
67 assert_eq!(unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &limits) }, 0);
68 }
69 });
70}
71
72tokio::task_local! {
73 static REQUEST_EMBEDDER_EXCLUSIONS: Arc<HashSet<String>>;
74}
75
76/// Run one request with daemon-only embedding models excluded from registry access.
77pub fn scope_request_embedder_exclusions<F>(
78 excluded: Vec<String>,
79 future: F,
80) -> impl Future<Output = F::Output>
81where
82 F: Future,
83{
84 REQUEST_EMBEDDER_EXCLUSIONS.scope(Arc::new(excluded.into_iter().collect()), future)
85}
86
87/// Carry the current request's embedder exclusions into a spawned task.
88pub fn inherit_request_embedder_scope<F>(future: F) -> impl Future<Output = F::Output>
89where
90 F: Future,
91{
92 let exclusions = REQUEST_EMBEDDER_EXCLUSIONS.try_with(Arc::clone).ok();
93 async move {
94 match exclusions {
95 Some(exclusions) => REQUEST_EMBEDDER_EXCLUSIONS.scope(exclusions, future).await,
96 None => future.await,
97 }
98 }
99}
100
101fn request_excludes_embedder(name: &str) -> bool {
102 let canonical = parse_embedding_model_alias(name)
103 .map(|model| model.to_string())
104 .unwrap_or_else(|| name.to_string());
105 REQUEST_EMBEDDER_EXCLUSIONS
106 .try_with(|excluded| excluded.contains(&canonical))
107 .unwrap_or(false)
108}
109
110/// Callback type for pack-installed entity-type validators.
111///
112/// Receives `(kind, entity_type)` and returns the normalised type string,
113/// or `RuntimeError::InvalidInput` if the type is not registered for that kind.
114/// When `entity_type` is `None`, the implementation must return `Ok(None)`.
115pub type EntityTypeValidatorFn =
116 Arc<dyn Fn(&str, Option<&str>) -> Result<Option<String>, RuntimeError> + Send + Sync>;
117
118/// Pack-aggregated entity-kind update hooks: `(entity kind, hook)` for every
119/// kind whose owning pack both declares it and registers a `KindHook`.
120///
121/// Named rather than written inline because the runtime stores it behind an
122/// `Arc<RwLock<..>>` and passes it across the transport boundary, so the bare
123/// form appears three times and reads as noise at each one.
124pub type EntityKindHooks = Vec<(String, Arc<dyn KindHook>)>;
125
126#[derive(Clone, Debug)]
127pub(crate) struct EntityOwnerHookLookup {
128 hooks: Arc<RwLock<EntityKindHooks>>,
129}
130
131fn find_entity_owner_hook(
132 hooks: &RwLock<EntityKindHooks>,
133 kind: &str,
134) -> Option<Arc<dyn KindHook>> {
135 hooks.read().ok().and_then(|guard| {
136 guard
137 .iter()
138 .find(|(candidate, _)| candidate == kind)
139 .map(|(_, hook)| hook.clone())
140 })
141}
142
143impl EntityOwnerHookLookup {
144 pub(crate) fn has_hook(&self, kind: &str) -> bool {
145 find_entity_owner_hook(&self.hooks, kind).is_some()
146 }
147}
148
149/// Callback type for a pack-installed note-mutation hook.
150///
151/// Invoked by `update_note` (when the note's text/embedding actually
152/// changed) and `delete_note` (soft or hard) with `(note_kind, note_id)`,
153/// after the mutation has been durably applied. Returns a boxed future so
154/// the hook can await async cache-invalidation work (e.g.
155/// `khive-pack-memory`'s ANN warm-cache generation bump) without
156/// `khive-runtime` depending on any pack crate: dependencies point the
157/// other way, so the runtime exposes an extension point and the pack
158/// installs into it, same shape as `EntityTypeValidatorFn`, just async.
159pub type NoteMutationHookFn = Arc<
160 dyn Fn(String, uuid::Uuid) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send>>
161 + Send
162 + Sync,
163>;
164
165/// Callback type for a pack-installed note-write validator.
166///
167/// The pack that owns a note kind carrying derivable identity installs one so
168/// that the identity is a function of the authorization token rather than of
169/// caller input, on every write path including direct callers that bypass the
170/// handler layer — same rationale as [`EntityTypeValidatorFn`], which exists
171/// for exactly that reason on the entity side.
172///
173/// Kinds the installing pack does not own must be returned unchanged: the slot
174/// is single-occupancy (like `note_mutation_hook`), so a validator that
175/// rewrote foreign kinds would silently govern every other pack's notes.
176pub type NoteWriteValidatorFn = Arc<
177 dyn Fn(&str, &str, Option<serde_json::Value>) -> Result<Option<serde_json::Value>, RuntimeError>
178 + Send
179 + Sync,
180>;
181
182/// Immutable identity for a non-text vector store owned by a pack consumer.
183///
184/// This does not register an [`crate::EmbedderProvider`]. It gives a pack that
185/// performs its own governed inference a narrow path to a namespace-scoped
186/// Khive vector table while keeping model-key and dimension validation at the
187/// runtime boundary.
188#[derive(Clone, Debug, Eq, PartialEq)]
189pub struct NamedVectorIdentity {
190 model_key: String,
191 model_name: String,
192 dimensions: usize,
193}
194
195struct CachedNamedVectorStores {
196 identity: NamedVectorIdentity,
197 by_namespace: HashMap<String, Arc<dyn VectorStore>>,
198}
199
200fn check_cached_named_vector_identity(
201 cached: &NamedVectorIdentity,
202 requested: &NamedVectorIdentity,
203) -> RuntimeResult<()> {
204 if cached.dimensions() != requested.dimensions() {
205 return Err(RuntimeError::InvalidInput(format!(
206 "named vector model_key {:?} is already bound to {} dimensions, expected {}",
207 requested.model_key(),
208 cached.dimensions(),
209 requested.dimensions()
210 )));
211 }
212 if cached.model_name() != requested.model_name() {
213 return Err(RuntimeError::InvalidInput(format!(
214 "named vector model_key {:?} is already bound to a different active model identity",
215 requested.model_key()
216 )));
217 }
218 Ok(())
219}
220
221impl NamedVectorIdentity {
222 const MAX_MODEL_KEY_BYTES: usize = 128;
223 const MAX_MODEL_NAME_BYTES: usize = 512;
224
225 /// Validate and construct a named vector identity.
226 pub fn new(
227 model_key: impl Into<String>,
228 model_name: impl Into<String>,
229 dimensions: usize,
230 ) -> RuntimeResult<Self> {
231 let model_key = model_key.into();
232 let model_name = model_name.into();
233 if model_key.is_empty()
234 || model_key.len() > Self::MAX_MODEL_KEY_BYTES
235 || !model_key
236 .chars()
237 .all(|c| c.is_ascii_alphanumeric() || c == '_')
238 {
239 return Err(RuntimeError::InvalidInput(format!(
240 "named vector model_key must be 1..={} bytes of ASCII alphanumeric/underscore",
241 Self::MAX_MODEL_KEY_BYTES
242 )));
243 }
244 if model_name.trim().is_empty()
245 || model_name.trim() != model_name
246 || model_name.len() > Self::MAX_MODEL_NAME_BYTES
247 {
248 return Err(RuntimeError::InvalidInput(format!(
249 "named vector model_name must be 1..={} bytes with no surrounding whitespace",
250 Self::MAX_MODEL_NAME_BYTES
251 )));
252 }
253 if !(1..=8192).contains(&dimensions) {
254 return Err(RuntimeError::InvalidInput(format!(
255 "named vector dimensions must be in 1..=8192, got {dimensions}"
256 )));
257 }
258 Ok(Self {
259 model_key,
260 model_name,
261 dimensions,
262 })
263 }
264
265 pub fn model_key(&self) -> &str {
266 &self.model_key
267 }
268
269 pub fn model_name(&self) -> &str {
270 &self.model_name
271 }
272
273 pub fn dimensions(&self) -> usize {
274 self.dimensions
275 }
276}
277
278pub use crate::config::{
279 assert_captured_db_anchor_consistent, assert_db_anchor_consistent, expand_tilde,
280 parse_pack_list, resolve_db_anchor, resolve_project_actor_id,
281 resolve_project_requires_attributed_actor, runtime_config_from_khive_config, BackendId,
282 BackendIdError, NamespaceToken, RuntimeConfig,
283};
284
285// ---- KhiveRuntime ----
286
287/// Composable runtime handle used by the MCP server.
288///
289/// Wraps a `StorageBackend` and provides namespace-scoped accessor methods
290/// Snapshot of the main runtime's embedder wiring (registry handle, default
291/// name, and the config model fields the default-resolution path reads).
292/// Carried by secondary-pack runtimes; consumed by [`KhiveRuntime::core`].
293#[derive(Clone)]
294struct CoreEmbedderState {
295 registry: Arc<std::sync::RwLock<crate::embedder_registry::EmbedderRegistry>>,
296 default_embedder_name: Arc<str>,
297 engines: Option<Vec<crate::engine_config::EngineConfig>>,
298 embedding_model: Option<EmbeddingModel>,
299 additional_embedding_models: Vec<EmbeddingModel>,
300}
301
302#[derive(Clone)]
303struct NoteKindEntry {
304 name: String,
305 embedding_policy: crate::NoteEmbeddingPolicy,
306 registered: bool,
307}
308
309/// An already-open serving backend eligible for operator diagnostics.
310/// Aliases of the same canonical database file share one entry.
311#[derive(Clone)]
312pub struct OpenedDiagnosticBackend {
313 pub backend_names: Vec<String>,
314 pub canonical_path: Option<PathBuf>,
315 pub pool: Arc<ConnectionPool>,
316}
317
318struct LateOpenedDiagnosticBackend {
319 backend_names: Vec<&'static str>,
320 pool: Weak<ConnectionPool>,
321}
322
323fn same_diagnostic_database(a: &OpenedDiagnosticBackend, b: &OpenedDiagnosticBackend) -> bool {
324 match (a.pool.canonical_path(), b.pool.canonical_path()) {
325 (Some(a), Some(b)) => a == b,
326 (None, None) => Arc::ptr_eq(&a.pool, &b.pool),
327 _ => false,
328 }
329}
330
331/// for each storage capability, plus a lazily-loaded embedder.
332#[derive(Clone)]
333pub struct KhiveRuntime {
334 pub(crate) visibility_receipts: Arc<crate::visibility_receipts::ReceiptCapability>,
335 pub(crate) visibility_cutover: Arc<crate::visibility_receipts::ReceiptCutover>,
336 core_visibility_cutover: Arc<crate::visibility_receipts::ReceiptCutover>,
337 backend: Arc<StorageBackend>,
338 /// Successful named-vector bindings and their namespace-scoped stores.
339 /// Shared by runtime clones so repeated reads do not enter the writer or
340 /// rescan the vector table after the first verified binding.
341 named_vector_stores: Arc<RwLock<HashMap<String, CachedNamedVectorStores>>>,
342 /// The main backend's cache, used when a secondary runtime creates a
343 /// `core()` handle. It must never reuse a secondary backend's store.
344 core_named_vector_stores: Option<Arc<RwLock<HashMap<String, CachedNamedVectorStores>>>>,
345 /// When `Some`, holds the main backend so that `core()` can return a
346 /// main-bound runtime handle without constructing a new connection.
347 /// `None` when this runtime is already bound to the main backend.
348 core_backend: Option<Arc<StorageBackend>>,
349 /// Observations belonging to this runtime construction and its clones.
350 config_lock_ledger: Arc<crate::config_ledger::ConfigLockLedger>,
351 /// A core ledger belongs to this runtime's exact backend-Arc binding.
352 /// Clones and old core handles retain the binding they already hold.
353 core_config_lock_ledger: Option<(
354 Arc<StorageBackend>,
355 Arc<crate::config_ledger::ConfigLockLedger>,
356 )>,
357 /// Admission identity shared by every view of this host's runtime.
358 background_domain: crate::resource_admission::BackgroundDomainHandle,
359 /// External lifetime ownership; absent only on background job views.
360 background_root: Option<crate::resource_admission::BackgroundRoot>,
361 config: RuntimeConfig,
362 pub(crate) code_map_registry: Arc<crate::CodeMapRegistry>,
363 outbound_email_policy: std::sync::Arc<crate::OutboundEmailPolicy>,
364 /// All SQLite backends declared by the host process, including those
365 /// assigned to other packs. The code pack fences these from ingest.
366 declared_backend_db_paths: Arc<[PathBuf]>,
367 /// Pools opened during serving host composition, grouped by canonical
368 /// database file.
369 diagnostic_backends: Arc<[OpenedDiagnosticBackend]>,
370 /// Pools opened later by this serving runtime and its pack handles.
371 /// Weak references keep diagnostics from extending their lifetime.
372 late_diagnostic_backends: Arc<Mutex<Vec<LateOpenedDiagnosticBackend>>>,
373 /// ADR-118 exact-leg policy, sampled once at runtime construction.
374 /// Request-time memory/knowledge serving must never re-read the process
375 /// environment because tests and embedded runtimes share one process.
376 ann_fresh_tail_enabled: bool,
377 /// Pack-extensible embedder registry.
378 ///
379 /// Shared across clones via `Arc<RwLock<_>>` so that
380 /// [`register_embedder`](Self::register_embedder) after clone is visible
381 /// to all handles. Built-in lattice models are pre-registered during
382 /// construction; packs may add more via [`PackRuntime::register_embedders`].
383 embedder_registry: Arc<std::sync::RwLock<crate::embedder_registry::EmbedderRegistry>>,
384 default_embedder_name: Arc<str>,
385 /// The MAIN runtime's embedder wiring, carried by secondary-backend
386 /// runtimes so that `core()`-routed writes embed with the main runtime's
387 /// models even when this pack's own registry is empty (`no_embed`).
388 /// `None` on the main runtime, and on secondaries wired before the boot
389 /// path calls [`with_core_embedders_from`](Self::with_core_embedders_from)
390 /// — `core()` then falls back to this runtime's own embedder state.
391 core_embedders: Option<CoreEmbedderState>,
392 /// Pack-extensible edge endpoint rules. Shared across clones
393 /// via `Arc<RwLock<_>>`; installed once by the transport after the
394 /// `VerbRegistry` is built. Empty until installed
395 edge_rules: Arc<RwLock<Vec<EdgeEndpointRule>>>,
396 /// Pack-aggregated valid entity kinds and note-kind policy entries.
397 ///
398 /// Installed by the transport layer after building the `VerbRegistry`.
399 /// When non-empty, `create_entity`, `create_note_inner`, and `import_kg`
400 /// reject kinds not in these sets. When empty (no packs loaded, e.g.
401 /// bare runtime in unit tests), kind validation is skipped — the pack
402 /// handler layer is the primary enforcement point.
403 valid_entity_kinds: Arc<RwLock<Vec<String>>>,
404 valid_note_kinds: Arc<RwLock<Vec<NoteKindEntry>>>,
405 /// Pack-installed entity-type validator.
406 ///
407 /// When `Some`, `create_many` calls this function to validate and normalise
408 /// each `(kind, entity_type)` pair before writing. When `None` (bare runtime
409 /// without packs), entity-type validation is skipped — the pack handler layer
410 /// is the primary enforcement point, same as for `valid_entity_kinds`.
411 entity_type_validator: Arc<RwLock<Option<EntityTypeValidatorFn>>>,
412 /// Pack-installed note-mutation hook.
413 ///
414 /// When `Some`, `update_note` (on text change) and `delete_note` (soft
415 /// or hard) call this after the mutation is durably applied, so a pack
416 /// that caches derived state keyed by note content (e.g. `khive-pack-memory`'s
417 /// warm ANN index) can invalidate/advance its own generation counter even
418 /// when the mutation arrived through a different pack's verb (e.g. KG's
419 /// `update`/`delete` on a `kind="memory"` note) that has no dependency on
420 /// the reacting pack. `None` when no pack installs one (bare runtime, or
421 /// no pack cares about note-mutation notifications) — the call becomes a
422 /// no-op check of an `Option`.
423 note_mutation_hook: Arc<RwLock<Option<NoteMutationHookFn>>>,
424 /// Backend-matched, pack-installed ANN source for note search. Absent on a
425 /// bare runtime or when no memory graph provider serves this backend.
426 note_search_ann_provider: Arc<RwLock<Option<Arc<dyn NoteSearchAnnProvider>>>>,
427 /// Pack-installed note-write validator.
428 ///
429 /// When `Some`, every runtime note-materialisation site that accepts
430 /// caller-supplied `properties` routes them through this function before
431 /// the `Note` is built, so a pack-owned identity property is derived from
432 /// the authorization token instead of trusted from caller input. `None`
433 /// on a bare runtime (no packs) — the properties pass through unchanged.
434 note_write_validator: Arc<RwLock<Option<NoteWriteValidatorFn>>>,
435 /// Pack-installed entity-kind update-validation hooks (issue #2943).
436 ///
437 /// Every `(entity kind, hook)` pair for which an owning pack declares
438 /// the entity kind and registers a `KindHook`, aggregated once by
439 /// `VerbRegistry::entity_kind_hooks` and installed by the transport
440 /// after the registry is built — same timing and rationale as
441 /// `entity_type_validator`: `khive-runtime` does not hold a
442 /// `VerbRegistry`, so this is the extension point that lets
443 /// `prepare_guarded_entity_update` reach a pack's `KindHook` on the
444 /// generic entity `update` path, the counterpart to
445 /// `prepare_note_update_hook` on the note side. Empty until installed
446 /// (bare runtime, or no pack registers an entity-kind hook), which
447 /// leaves the dispatch a no-op.
448 entity_kind_hooks: Arc<RwLock<EntityKindHooks>>,
449 /// Pack-owned note kinds — every note kind declared by a pack other than
450 /// the generic-CRUD pack, installed by the transport from the registry
451 /// (see `VerbRegistry::pack_owned_note_kinds`). Records of these kinds are
452 /// maintained by their owning pack's own verbs, so `update`'s `properties`
453 /// patch is refused on them at the runtime layer and their owned identity
454 /// properties survive a `merge` unchanged. Empty until installed (bare
455 /// runtime), which leaves both rules inert.
456 pack_owned_note_kinds: Arc<RwLock<Vec<String>>>,
457 /// The immutable runtime-owned store/hydration pair (ADR-160 D3).
458 ///
459 /// Boot resolves one store and constructs exactly one shared hydrator,
460 /// then installs that same `Arc` on every runtime handle. The one-shot
461 /// slot rejects replacement so pack runtimes cannot silently split the
462 /// aggregate byte budget after startup. Bare runtimes leave it unset.
463 blob_hydrator: Arc<OnceLock<Arc<crate::blob::BlobHydrator>>>,
464 /// Pack-registered custom fusion executors (ADR-012), keyed by the name
465 /// carried in `FusionStrategy::Custom { name, .. }`.
466 ///
467 /// Unlike `entity_type_validator`/`note_mutation_hook` (single-occupancy —
468 /// one pack owns the slot), multiple packs each register their own named
469 /// strategy under this shared map, so it is keyed rather than a bare
470 /// `Option`. Empty until a pack calls
471 /// [`register_fusion_strategy`](Self::register_fusion_strategy); an
472 /// unregistered `Custom` name at dispatch time is
473 /// `RuntimeError::UnknownFusionStrategy`, never a silent fallback.
474 fusion_executors: Arc<RwLock<HashMap<String, Arc<dyn crate::fusion::FusionExecutor>>>>,
475}
476
477impl KhiveRuntime {
478 /// Observe a resolved config value once for this runtime construction.
479 ///
480 /// The lazy value runs outside the ledger mutex. A name already recorded
481 /// does not evaluate its lazy value. Concurrent first observers of one name
482 /// may each evaluate their closure, so value closures must have no side effects.
483 /// Only the first to insert the name after evaluation queues an observation.
484 pub fn record_config_locked<F>(&self, key: &'static str, value: F)
485 where
486 F: FnOnce() -> String,
487 {
488 self.config_lock_ledger.record_config_locked(key, value);
489 }
490
491 #[cfg(test)]
492 pub(crate) fn install_config_lock_test_hook(
493 &self,
494 point: crate::config_ledger::TestRecordPoint,
495 hook: Arc<dyn Fn() + Send + Sync>,
496 ) {
497 self.config_lock_ledger.install_test_hook(point, hook);
498 }
499
500 /// Take this runtime's pending observations for its bound audit registry.
501 pub(crate) fn drain_config_locked(&self) -> Vec<(&'static str, String)> {
502 self.config_lock_ledger.drain_config_locked()
503 }
504
505 /// Return the code maps accepted from the configuration used to construct this runtime.
506 pub fn code_map_registry(&self) -> &crate::CodeMapRegistry {
507 self.code_map_registry.as_ref()
508 }
509
510 /// Create a new runtime with the given config.
511 ///
512 /// The config's `db_path` is used to open or create the SQLite backend.
513 /// This direct constructor is intended for fresh/current single-backend
514 /// databases and tests. Production and multi-backend hosts must use the
515 /// async khive-mcp/kkernel builders so secondary inventory and any
516 /// application-assisted V21 cutover complete before serving. The
517 /// [`from_backend`](Self::from_backend) seam is likewise only for an
518 /// already-prepared backend.
519 pub fn new(mut config: RuntimeConfig) -> RuntimeResult<Self> {
520 let wal_ceiling = config.resolve_wal_ceiling_policy(false)?;
521 let disk_guard = config.resolve_disk_guard_policy(false)?;
522 // Refuse a missing lock directory before the constructor below creates
523 // the database's parent directory.
524 let volume_lock_dir = if config.db_path.is_some() {
525 let configured = config.volume_lock_dir.clone();
526 Some(khive_db::require_volume_lock_dir(configured)?)
527 } else {
528 None
529 };
530 Self::new_with_file_backend(config, true, |path| {
531 StorageBackend::sqlite_with_max_readers_and_policies(
532 path,
533 None,
534 wal_ceiling,
535 disk_guard.expect("file-backed disk policy"),
536 volume_lock_dir.expect("file-backed volume-lock directory"),
537 )
538 })
539 }
540
541 /// Construct a fixture runtime with a small concurrent reader pool.
542 #[cfg(any(test, feature = "test-internals"))]
543 pub fn new_for_test(mut config: RuntimeConfig) -> RuntimeResult<Self> {
544 let wal_ceiling = config.resolve_wal_ceiling_policy(false)?;
545 let disk_guard = config.resolve_disk_guard_policy(false)?;
546 Self::new_with_file_backend(config, true, |path| {
547 StorageBackend::sqlite_for_test_with_policies(
548 path,
549 wal_ceiling,
550 disk_guard.expect("file-backed disk policy"),
551 )
552 })
553 }
554
555 pub(crate) fn new_with_file_backend(
556 mut config: RuntimeConfig,
557 create_parent: bool,
558 open_file: impl FnOnce(&std::path::Path) -> Result<StorageBackend, khive_db::SqliteError>,
559 ) -> RuntimeResult<Self> {
560 let background_limits = Self::validated_background_limits(&config)?;
561 config.prepare_engines()?;
562 #[cfg(unix)]
563 crate::events_split::socket_path::validate_configured_events_socket(&config)?;
564 #[cfg(all(test, target_os = "macos"))]
565 ensure_in_process_test_nofile_limit();
566 let backend = match &config.db_path {
567 Some(path) => {
568 if let Some(parent) = path.parent().filter(|_| create_parent) {
569 std::fs::create_dir_all(parent).ok();
570 }
571 open_file(path)?
572 }
573 None => {
574 if config.wal_ceiling_configured_bytes != 0 || config.wal_ceiling_bytes != 0 {
575 return Err(khive_db::SqliteError::InvalidConfig(
576 "nonzero wal_ceiling_bytes requires a file-backed SQLite backend".into(),
577 )
578 .into());
579 }
580 StorageBackend::memory()?
581 }
582 };
583 // Writable backends migrate before handlers touch the DB. A detected
584 // read-only snapshot is validated at the current schema version without
585 // attempting migration DDL.
586 let schema_version = backend.prepare_core_schema()?;
587 if schema_version < khive_db::migrations::ATTACHMENT_CUTOVER_VERSION {
588 return Err(crate::error::RuntimeError::AttachmentCutoverRequired {
589 schema_version,
590 required_version: khive_db::migrations::ATTACHMENT_CUTOVER_VERSION,
591 });
592 }
593 if !backend.is_read_only() {
594 register_configured_embedding_models(&backend, &config)?;
595 }
596 let background = Self::start_background_root(background_limits)?;
597 Ok(Self::assemble_from_backend(
598 Arc::new(backend),
599 config,
600 false,
601 background,
602 ))
603 }
604
605 /// Open a runtime for read-only inspection (no model registration, no DB creation).
606 ///
607 /// File-backed databases are opened with SQLite read-only/query-only flags
608 /// and must already be at this build's current schema version. No migrations
609 /// or configured-model registration writes are attempted. A `None` path
610 /// retains the historical ephemeral in-memory behavior for tests.
611 pub fn new_readonly(mut config: RuntimeConfig) -> RuntimeResult<Self> {
612 let wal_ceiling = config.resolve_wal_ceiling_policy(true)?;
613 Self::new_readonly_with_file_backend(config, |path| {
614 StorageBackend::sqlite_read_only_with_max_readers_and_wal_ceiling(
615 path,
616 None,
617 wal_ceiling,
618 )
619 })
620 }
621
622 /// Open an existing live database for read-only planning.
623 ///
624 /// Committed WAL frames remain visible while a writer holds the database
625 /// open. Schema compatibility is checked without migrations, configured-model
626 /// registration or application-assisted cutover writes. A file-backed path
627 /// is required.
628 pub fn new_readonly_live(mut config: RuntimeConfig) -> RuntimeResult<Self> {
629 if config.db_path.is_none() {
630 return Err(khive_db::SqliteError::InvalidConfig(
631 "live read-only planning requires a file-backed database path".into(),
632 )
633 .into());
634 }
635 let wal_ceiling = config.resolve_wal_ceiling_policy(true)?;
636 Self::new_readonly_with_file_backend(config, |path| {
637 StorageBackend::sqlite_read_only_live_with_max_readers_and_wal_ceiling(
638 path,
639 None,
640 wal_ceiling,
641 )
642 })
643 }
644
645 /// Construct a read-only fixture runtime with a small reader pool.
646 #[cfg(any(test, feature = "test-internals"))]
647 pub fn new_readonly_for_test(mut config: RuntimeConfig) -> RuntimeResult<Self> {
648 let wal_ceiling = config.resolve_wal_ceiling_policy(true)?;
649 Self::new_readonly_with_file_backend(config, |path| {
650 StorageBackend::sqlite_read_only_with_max_readers_and_wal_ceiling(
651 path,
652 Some(2),
653 wal_ceiling,
654 )
655 })
656 }
657
658 fn new_readonly_with_file_backend(
659 mut config: RuntimeConfig,
660 open_file: impl FnOnce(&std::path::Path) -> Result<StorageBackend, khive_db::SqliteError>,
661 ) -> RuntimeResult<Self> {
662 let background_limits = Self::validated_background_limits(&config)?;
663 config.prepare_engines()?;
664 #[cfg(unix)]
665 crate::events_split::socket_path::validate_configured_events_socket(&config)?;
666 #[cfg(all(test, target_os = "macos"))]
667 ensure_in_process_test_nofile_limit();
668 let backend = match &config.db_path {
669 Some(path) => open_file(path)?,
670 None => {
671 if config.wal_ceiling_configured_bytes != 0 || config.wal_ceiling_bytes != 0 {
672 return Err(khive_db::SqliteError::InvalidConfig(
673 "nonzero wal_ceiling_bytes requires a file-backed SQLite backend".into(),
674 )
675 .into());
676 }
677 StorageBackend::memory()?
678 }
679 };
680 backend.prepare_core_schema()?;
681 let background = Self::start_background_root(background_limits)?;
682 Ok(Self::assemble_from_backend(
683 Arc::new(backend),
684 config,
685 false,
686 background,
687 ))
688 }
689
690 /// Construct a runtime from an already-opened backend.
691 ///
692 /// This is a low-level, infallible assembly seam for already-prepared
693 /// multi-backend deployments. It does not migrate or require completion of
694 /// the V21 attachment cutover, and it does not read the backend: receipt
695 /// admission is checked by the first receipt operation and cached once it
696 /// succeeds. Production hosts must first run the async kkernel/khive-mcp
697 /// coordinator and must not expose a server over a pending or incomplete
698 /// backend. Prefer [`Self::from_prepared_backend`] when constructing one
699 /// fallible host runtime.
700 ///
701 /// All storage access is through the provided `backend`; the config's
702 /// ordered embedding peers are prepared before assembly.
703 ///
704 /// # Panics
705 ///
706 /// Panics if the engine configuration is invalid. Use
707 /// [`Self::try_from_backend`] to handle configuration errors at startup.
708 pub fn from_backend(backend: Arc<StorageBackend>, config: RuntimeConfig) -> Self {
709 Self::try_from_backend(backend, config)
710 .expect("from_backend requires a valid embedding engine configuration")
711 }
712
713 /// Assemble an already-opened backend after validating its engine config.
714 ///
715 /// This retains [`Self::from_backend`]'s warning-only storage-model
716 /// registration policy; use [`Self::from_prepared_backend`] when those
717 /// registration failures must also abort startup.
718 pub fn try_from_backend(
719 backend: Arc<StorageBackend>,
720 config: RuntimeConfig,
721 ) -> RuntimeResult<Self> {
722 Self::try_from_backend_with_background(backend, config, None)
723 }
724
725 /// Assemble a backend in the main runtime's existing admission domain.
726 ///
727 /// Panics on invalid configuration, matching [`Self::from_backend`].
728 pub fn from_backend_in_domain(
729 backend: Arc<StorageBackend>,
730 config: RuntimeConfig,
731 background: crate::resource_admission::BackgroundRoot,
732 ) -> Self {
733 Self::try_from_backend_in_domain(backend, config, background)
734 .expect("from_backend_in_domain requires matching valid background limits")
735 }
736
737 /// Validate limits and reuse a host domain without creating another worker.
738 pub fn try_from_backend_in_domain(
739 backend: Arc<StorageBackend>,
740 config: RuntimeConfig,
741 background: crate::resource_admission::BackgroundRoot,
742 ) -> RuntimeResult<Self> {
743 Self::try_from_backend_with_background(backend, config, Some(background))
744 }
745
746 fn try_from_backend_with_background(
747 backend: Arc<StorageBackend>,
748 mut config: RuntimeConfig,
749 background: Option<crate::resource_admission::BackgroundRoot>,
750 ) -> RuntimeResult<Self> {
751 let limits = Self::validated_background_limits(&config)?;
752 if background
753 .as_ref()
754 .is_some_and(|root| root.limits() != limits)
755 {
756 return Err(RuntimeError::InvalidInput(
757 "runtime background limits must match the host admission domain".into(),
758 ));
759 }
760 config.prepare_engines()?;
761 if !backend.is_read_only() {
762 if let Err(err) = register_configured_embedding_models(&backend, &config) {
763 tracing::warn!(error = %err, "failed to register configured embedding models");
764 }
765 }
766 let background = match background {
767 Some(root) => root,
768 None => Self::start_background_root(limits)?,
769 };
770 Ok(Self::assemble_from_backend(
771 backend, config, false, background,
772 ))
773 }
774
775 /// Construct a single-backend runtime after a host boot coordinator has
776 /// completed schema preparation and any application-assisted cutover.
777 ///
778 /// Unlike [`Self::from_backend`], configured embedding-model registration
779 /// is fallible here, preserving [`Self::new`]'s single-backend startup
780 /// semantics. This method never runs migrations itself.
781 pub fn from_prepared_backend(
782 backend: Arc<StorageBackend>,
783 mut config: RuntimeConfig,
784 ) -> RuntimeResult<Self> {
785 let background_limits = Self::validated_background_limits(&config)?;
786 config.prepare_engines()?;
787 if backend.attachment_cutover_status()?
788 != khive_db::migrations::AttachmentCutoverStatus::Complete
789 {
790 return Err(khive_db::SqliteError::InvalidData(
791 "from_prepared_backend requires a complete V21 attachment cutover".into(),
792 )
793 .into());
794 }
795 backend.validate_memory_visibility_cutover()?;
796 if !backend.is_read_only() {
797 register_configured_embedding_models(&backend, &config)?;
798 }
799 let background = Self::start_background_root(background_limits)?;
800 Ok(Self::assemble_from_backend(
801 backend, config, true, background,
802 ))
803 }
804
805 /// Validate raw host admission limits before preparing engines or opening storage.
806 /// This pure check starts no worker and performs no filesystem or boot work.
807 pub fn validated_background_limits(
808 config: &RuntimeConfig,
809 ) -> RuntimeResult<crate::resource_admission::WorkClassLimits> {
810 crate::resource_admission::WorkClassLimits::validate(
811 config.warm_concurrency,
812 config.maintenance_concurrency,
813 )
814 .map_err(|error| RuntimeError::InvalidInput(error.to_string()))
815 }
816
817 fn start_background_root(
818 limits: crate::resource_admission::WorkClassLimits,
819 ) -> RuntimeResult<crate::resource_admission::BackgroundRoot> {
820 crate::resource_admission::BackgroundRoot::start(
821 limits,
822 crate::resource_admission::CleanupPolicy::new(crate::daemon::drain_timeout()),
823 )
824 .map_err(|error| {
825 RuntimeError::Internal(format!("background runtime startup failed: {error}"))
826 })
827 }
828
829 fn assemble_from_backend(
830 backend: Arc<StorageBackend>,
831 config: RuntimeConfig,
832 cutover_validated: bool,
833 background: crate::resource_admission::BackgroundRoot,
834 ) -> Self {
835 if config.backend_id.as_str() == BackendId::MAIN {
836 backend.pool().main_pool_generation();
837 }
838 let ann_fresh_tail_enabled = crate::config::ann_fresh_tail_enabled_from_env();
839 let (registry, default_embedder_name) = build_embedder_registry(&config);
840 let runtime_db_path = backend.pool().canonical_path().map(PathBuf::from);
841 let code_map_registry = Arc::new(crate::CodeMapRegistry::load_for_runtime(
842 &config,
843 runtime_db_path,
844 ));
845 let visibility_receipts = Arc::new(
846 crate::visibility_receipts::ReceiptCapability::from_config(&config),
847 );
848 let visibility_cutover = Arc::new(crate::visibility_receipts::ReceiptCutover::new(
849 backend.clone(),
850 cutover_validated,
851 ));
852 Self {
853 visibility_receipts,
854 core_visibility_cutover: visibility_cutover.clone(),
855 visibility_cutover,
856 backend,
857 named_vector_stores: Arc::new(RwLock::new(HashMap::new())),
858 core_named_vector_stores: None,
859 core_backend: None,
860 config_lock_ledger: Arc::new(crate::config_ledger::ConfigLockLedger::default()),
861 core_config_lock_ledger: None,
862 background_domain: background.handle(),
863 background_root: Some(background),
864 config,
865 code_map_registry,
866 outbound_email_policy: Default::default(),
867 declared_backend_db_paths: Vec::new().into(),
868 diagnostic_backends: Vec::new().into(),
869 late_diagnostic_backends: Arc::new(Mutex::new(Vec::new())),
870 ann_fresh_tail_enabled,
871 embedder_registry: Arc::new(std::sync::RwLock::new(registry)),
872 default_embedder_name,
873 core_embedders: None,
874 edge_rules: Arc::new(RwLock::new(Vec::new())),
875 valid_entity_kinds: Arc::new(RwLock::new(Vec::new())),
876 valid_note_kinds: Arc::new(RwLock::new(Vec::new())),
877 entity_type_validator: Arc::new(RwLock::new(None)),
878 note_mutation_hook: Arc::new(RwLock::new(None)),
879 note_search_ann_provider: Arc::new(RwLock::new(None)),
880 note_write_validator: Arc::new(RwLock::new(None)),
881 entity_kind_hooks: Arc::new(RwLock::new(Vec::new())),
882 pack_owned_note_kinds: Arc::new(RwLock::new(Vec::new())),
883 blob_hydrator: Arc::new(OnceLock::new()),
884 fusion_executors: Arc::new(RwLock::new(HashMap::new())),
885 }
886 }
887
888 /// Wire this runtime as a secondary-backend runtime pointing at `core`.
889 ///
890 /// After this call, `self.core()` returns a handle to `core` rather than
891 /// cloning `self`. The caller (the boot path, not pack code) is responsible
892 /// for passing the correct main backend.
893 /// Binding a different core clears its named-vector cache and prior main
894 /// embedder wiring. Call [`Self::with_core_embedders_from`] with the new main
895 /// runtime after rebinding when core-routed writes require its embedders.
896 ///
897 /// Panics in debug builds if `self.config.backend_id == BackendId::MAIN`,
898 /// because the main runtime does not need a core pointer.
899 pub fn with_core_backend(mut self, core: Arc<StorageBackend>) -> Self {
900 debug_assert_ne!(
901 self.config.backend_id.as_str(),
902 BackendId::MAIN,
903 "with_core_backend must not be called on the main runtime"
904 );
905 core.pool().main_pool_generation();
906 if !self
907 .core_config_lock_ledger
908 .as_ref()
909 .is_some_and(|(bound, _)| Arc::ptr_eq(bound, &core))
910 {
911 self.core_config_lock_ledger = Some((
912 core.clone(),
913 Arc::new(crate::config_ledger::ConfigLockLedger::default()),
914 ));
915 }
916 if self.visibility_cutover.is_bound_to(&core) {
917 self.core_visibility_cutover = self.visibility_cutover.clone();
918 } else if !self.core_visibility_cutover.is_bound_to(&core) {
919 self.core_visibility_cutover = Arc::new(
920 crate::visibility_receipts::ReceiptCutover::new(core.clone(), false),
921 );
922 }
923 if self
924 .core_backend
925 .as_ref()
926 .is_some_and(|previous| !Arc::ptr_eq(previous, &core))
927 {
928 self.core_named_vector_stores = None;
929 self.core_embedders = None;
930 }
931 if self.core_named_vector_stores.is_none() {
932 self.core_named_vector_stores = Some(Arc::new(RwLock::new(HashMap::new())));
933 }
934 self.core_backend = Some(core);
935 self
936 }
937
938 /// Carry the main runtime's embedder wiring for `core()`-routed writes.
939 ///
940 /// Boot-path companion to [`with_core_backend`](Self::with_core_backend).
941 /// Without it, `core()` shares this pack runtime's own embedder registry —
942 /// which under `[packs.<name>] no_embed = true` is empty, so core-routed
943 /// concept writes would silently skip embedding on the shared graph.
944 pub fn with_core_embedders_from(mut self, main: &KhiveRuntime) -> Self {
945 debug_assert!(
946 main.core_backend.is_none(),
947 "with_core_embedders_from takes the MAIN runtime"
948 );
949 self.core_embedders = Some(CoreEmbedderState {
950 registry: main.embedder_registry.clone(),
951 default_embedder_name: main.default_embedder_name.clone(),
952 engines: main.config.engines.clone(),
953 embedding_model: main.config.embedding_model,
954 additional_embedding_models: main.config.additional_embedding_models.clone(),
955 });
956 self.core_named_vector_stores = Some(main.named_vector_stores.clone());
957 if Arc::ptr_eq(&self.backend, &main.backend) {
958 self.named_vector_stores = main.named_vector_stores.clone();
959 }
960 self
961 }
962
963 /// Share the main runtime's validated code-map registrations.
964 pub fn with_code_map_registry_from(mut self, main: &KhiveRuntime) -> Self {
965 debug_assert!(
966 main.core_backend.is_none(),
967 "with_code_map_registry_from takes the MAIN runtime"
968 );
969 self.code_map_registry = main.code_map_registry.clone();
970 self
971 }
972
973 /// Return a runtime handle bound to the main (shared-graph) backend.
974 ///
975 /// When `self` is already the main runtime (`core_backend` is `None`),
976 /// this returns a clone of `self` — no new backend reference is acquired.
977 ///
978 /// When `self` is a secondary-backend runtime (`core_backend` is `Some`),
979 /// this returns a new `KhiveRuntime` backed by the main
980 /// `Arc<StorageBackend>` and sharing all registry state (`embedder_registry`,
981 /// `edge_rules`, `valid_entity_kinds`, `valid_note_kinds`,
982 /// `entity_type_validator`, `note_mutation_hook`, `entity_kind_hooks`) with `self`.
983 /// No database I/O occurs; no embedding models are reloaded.
984 ///
985 /// Use `core()` for notes and entities that must reside in the shared graph
986 /// so that `memory.recall`, cross-pack search, and `annotates` edges work.
987 /// Use `self` (or `self.sql()`) for pack-auxiliary bulk tables.
988 ///
989 /// Handlers that call `core()` more than once per request or loop should bind
990 /// `let core = self.core();` once and reuse it, since each call clones
991 /// `RuntimeConfig` (a heap-allocated struct containing `Vec<String>` fields).
992 pub fn core(&self) -> KhiveRuntime {
993 match &self.core_backend {
994 // A main-assigned pack runtime has no core pointer, but may still
995 // carry main's embedder wiring: with `no_embed` its OWN registry
996 // is empty, and core-routed concept writes must embed regardless
997 // of which backend the pack was assigned to.
998 None => match &self.core_embedders {
999 None => self.clone(),
1000 Some(core_embedders) => {
1001 let mut core = self.clone();
1002 core.config.engines = core_embedders.engines.clone();
1003 core.config.embedding_model = core_embedders.embedding_model;
1004 core.config.additional_embedding_models =
1005 core_embedders.additional_embedding_models.clone();
1006 core.embedder_registry = core_embedders.registry.clone();
1007 core.default_embedder_name = core_embedders.default_embedder_name.clone();
1008 core.core_embedders = None;
1009 core
1010 }
1011 },
1012 Some(main_arc) => {
1013 let mut core_config = self.config.clone();
1014 core_config.backend_id = BackendId::main();
1015 // Core-routed writes embed with the MAIN runtime's wiring when
1016 // the boot path supplied it (see `with_core_embedders_from`);
1017 // both the registry handle and the config model fields must
1018 // come from main, since default-model resolution reads
1019 // `config.embedding_model` (`resolve_embedding_model`).
1020 let (embedder_registry, default_embedder_name) = match &self.core_embedders {
1021 Some(core_embedders) => {
1022 core_config.engines = core_embedders.engines.clone();
1023 core_config.embedding_model = core_embedders.embedding_model;
1024 core_config.additional_embedding_models =
1025 core_embedders.additional_embedding_models.clone();
1026 (
1027 core_embedders.registry.clone(),
1028 core_embedders.default_embedder_name.clone(),
1029 )
1030 }
1031 None => (
1032 self.embedder_registry.clone(),
1033 self.default_embedder_name.clone(),
1034 ),
1035 };
1036 KhiveRuntime {
1037 visibility_receipts: self.visibility_receipts.clone(),
1038 visibility_cutover: self.core_visibility_cutover.clone(),
1039 core_visibility_cutover: self.core_visibility_cutover.clone(),
1040 backend: main_arc.clone(),
1041 named_vector_stores: self
1042 .core_named_vector_stores
1043 .clone()
1044 .unwrap_or_else(|| Arc::new(RwLock::new(HashMap::new()))),
1045 core_named_vector_stores: None,
1046 core_backend: None,
1047 config_lock_ledger: self
1048 .core_config_lock_ledger
1049 .as_ref()
1050 .filter(|(bound, _)| Arc::ptr_eq(bound, main_arc))
1051 .map(|(_, ledger)| ledger.clone())
1052 .expect("core backend binding must retain its config-lock ledger"),
1053 core_config_lock_ledger: None,
1054 background_domain: self.background_domain.clone(),
1055 background_root: self.background_root.clone(),
1056 config: core_config,
1057 code_map_registry: self.code_map_registry.clone(),
1058 outbound_email_policy: self.outbound_email_policy.clone(),
1059 declared_backend_db_paths: self.declared_backend_db_paths.clone(),
1060 diagnostic_backends: self.diagnostic_backends.clone(),
1061 late_diagnostic_backends: self.late_diagnostic_backends.clone(),
1062 ann_fresh_tail_enabled: self.ann_fresh_tail_enabled,
1063 embedder_registry,
1064 default_embedder_name,
1065 core_embedders: None,
1066 edge_rules: self.edge_rules.clone(),
1067 valid_entity_kinds: self.valid_entity_kinds.clone(),
1068 valid_note_kinds: self.valid_note_kinds.clone(),
1069 entity_type_validator: self.entity_type_validator.clone(),
1070 note_mutation_hook: self.note_mutation_hook.clone(),
1071 note_search_ann_provider: self.note_search_ann_provider.clone(),
1072 note_write_validator: self.note_write_validator.clone(),
1073 entity_kind_hooks: self.entity_kind_hooks.clone(),
1074 pack_owned_note_kinds: self.pack_owned_note_kinds.clone(),
1075 blob_hydrator: self.blob_hydrator.clone(),
1076 fusion_executors: self.fusion_executors.clone(),
1077 }
1078 }
1079 }
1080 }
1081
1082 /// Create an in-memory runtime (for tests and ephemeral use).
1083 pub fn memory() -> RuntimeResult<Self> {
1084 Self::new(RuntimeConfig {
1085 db_path: None,
1086 packs: vec!["kg".to_string()],
1087 brain_profile: None,
1088 actor_id: None,
1089 ..RuntimeConfig::no_embeddings()
1090 })
1091 }
1092
1093 /// Return the [`BackendId`] for this runtime's backend.
1094 ///
1095 /// Used by `SubstrateCoordinator` in `kkernel`
1096 /// to identify which backend owns a given node, and to detect cross-backend merges.
1097 pub fn backend_id(&self) -> &BackendId {
1098 &self.config.backend_id
1099 }
1100
1101 /// Whether two runtime handles share one opened physical store. The host
1102 /// deduplicates same-path aliases; separately opened hard-link aliases
1103 /// compare the identity pinned when SQLite opened each file.
1104 pub fn shares_backend_storage_with(&self, other: &Self) -> bool {
1105 if Arc::ptr_eq(&self.backend, &other.backend) {
1106 return true;
1107 }
1108 #[cfg(any(unix, windows))]
1109 {
1110 matches!(
1111 (
1112 self.backend.pool().opened_file_identity_record(),
1113 other.backend.pool().opened_file_identity_record()
1114 ),
1115 (Some(left), Some(right)) if left == right
1116 )
1117 }
1118 #[cfg(not(any(unix, windows)))]
1119 {
1120 false
1121 }
1122 }
1123
1124 /// Install only pools the host actually opened. A bare runtime defaults
1125 /// to its already-open main pool without opening or creating another file.
1126 pub fn with_diagnostic_backends(mut self, backends: Arc<[OpenedDiagnosticBackend]>) -> Self {
1127 self.diagnostic_backends = backends;
1128 self
1129 }
1130
1131 /// Share late-opened diagnostics with pack runtimes from the same serving
1132 /// host. Independent runtimes retain independent observers.
1133 pub fn with_diagnostic_observer_from(mut self, main: &KhiveRuntime) -> Self {
1134 self.late_diagnostic_backends = Arc::clone(&main.late_diagnostic_backends);
1135 self
1136 }
1137
1138 fn register_late_diagnostic_pool(
1139 &self,
1140 backend_name: &'static str,
1141 pool: &Arc<ConnectionPool>,
1142 ) {
1143 let mut backends = self
1144 .late_diagnostic_backends
1145 .lock()
1146 .unwrap_or_else(std::sync::PoisonError::into_inner);
1147 backends.retain(|entry| entry.pool.strong_count() > 0);
1148 if let Some(existing) = backends.iter_mut().find(|entry| {
1149 entry
1150 .pool
1151 .upgrade()
1152 .is_some_and(|live| Arc::ptr_eq(&live, pool))
1153 }) {
1154 if !existing.backend_names.contains(&backend_name) {
1155 existing.backend_names.push(backend_name);
1156 }
1157 return;
1158 }
1159 backends.push(LateOpenedDiagnosticBackend {
1160 backend_names: vec![backend_name],
1161 pool: Arc::downgrade(pool),
1162 });
1163 }
1164
1165 fn live_late_diagnostic_backends(&self) -> Vec<OpenedDiagnosticBackend> {
1166 let mut backends = self
1167 .late_diagnostic_backends
1168 .lock()
1169 .unwrap_or_else(std::sync::PoisonError::into_inner);
1170 let mut live = Vec::new();
1171 backends.retain(|entry| {
1172 let Some(pool) = entry.pool.upgrade() else {
1173 return false;
1174 };
1175 live.push(OpenedDiagnosticBackend {
1176 backend_names: entry
1177 .backend_names
1178 .iter()
1179 .map(|name| name.to_string())
1180 .collect(),
1181 canonical_path: pool.canonical_path().map(PathBuf::from),
1182 pool,
1183 });
1184 true
1185 });
1186 live
1187 }
1188
1189 pub fn diagnostic_backends(&self) -> Arc<[OpenedDiagnosticBackend]> {
1190 let mut opened: Vec<OpenedDiagnosticBackend> = if self.diagnostic_backends.is_empty() {
1191 let main = self.core().backend.pool_arc();
1192 vec![OpenedDiagnosticBackend {
1193 backend_names: vec![BackendId::MAIN.to_string()],
1194 canonical_path: main.canonical_path().map(PathBuf::from),
1195 pool: main,
1196 }]
1197 } else {
1198 self.diagnostic_backends.iter().cloned().collect()
1199 };
1200 for late in self.live_late_diagnostic_backends() {
1201 if let Some(existing) = opened
1202 .iter_mut()
1203 .find(|existing| same_diagnostic_database(existing, &late))
1204 {
1205 for name in late.backend_names {
1206 if !existing.backend_names.contains(&name) {
1207 existing.backend_names.push(name);
1208 }
1209 }
1210 } else {
1211 opened.push(late);
1212 }
1213 }
1214 opened.into()
1215 }
1216
1217 /// Whether this runtime selects the vector arm for a hybrid search —
1218 /// true exactly when a default embedding model is configured. Single
1219 /// source of truth for the policy every fan-out and single-backend
1220 /// dispatch path uses to report `arm_participation`/`vector_selected`.
1221 pub fn vector_arm_selected(&self) -> bool {
1222 !self.default_embedder_name().is_empty()
1223 }
1224
1225 /// Return a reference to the underlying storage backend.
1226 ///
1227 /// This is an embedder/infrastructure surface (connection pools, schema
1228 /// plans, diagnostics). Stores obtained from it are NOT wrapped by the
1229 /// message-evidence policy that [`Self::notes`] enforces: an embedder
1230 /// holding the backend already holds root-equivalent access to the
1231 /// database file, so the policy boundary sits at the typed accessors
1232 /// pack code uses, not here. Pack code must not take note stores from
1233 /// this surface.
1234 pub fn backend(&self) -> &StorageBackend {
1235 &self.backend
1236 }
1237
1238 /// Whether this runtime's bound backend is explicitly or filesystem-mode
1239 /// detected read-only.
1240 pub fn is_read_only(&self) -> bool {
1241 self.backend.is_read_only()
1242 }
1243
1244 /// Return the directory containing the backend's database file, or `None`
1245 /// for an in-memory backend.
1246 pub fn backend_data_dir(&self) -> Option<std::path::PathBuf> {
1247 self.backend.data_dir()
1248 }
1249
1250 /// Root directory for this database's ANN segment tree (`<db-file>.ann/`
1251 /// beside the file), or `None` for an in-memory backend. Scoped to the
1252 /// database file itself so two databases sharing a parent directory can
1253 /// never adopt each other's segments.
1254 pub fn backend_ann_root(&self) -> Option<std::path::PathBuf> {
1255 self.backend.ann_root()
1256 }
1257
1258 /// Writer-contention, graph-edge integrity, and WAL/checkpoint diagnostics
1259 /// (ADR-091/ADR-135 operator surface): pooled writer and audit-failure
1260 /// counters, build identity, duplicate edge-ID and list-ledger counts,
1261 /// checkpoint counters, a PASSIVE checkpoint probe, WAL file size, and
1262 /// explicitly qualified WAL-pin census. Not write-free: the
1263 /// PASSIVE probe may backfill WAL frames into the database (normal
1264 /// checkpoint I/O). It never changes logical state, escalates to TRUNCATE,
1265 /// creates a missing database file, or deletes sidecar evidence — see
1266 /// `khive_db::diagnostics` for the narrowings that make those claims hold.
1267 ///
1268 /// Always targets the *main* backend via [`Self::core`], regardless of
1269 /// which backend this runtime handle is bound to, so a report never
1270 /// describes a database this handle is not the canonical owner of.
1271 pub async fn db_diagnostics(&self) -> RuntimeResult<khive_db::diagnostics::DbDiagnostics> {
1272 // No `VerbRegistry` handle is reachable from a bare `KhiveRuntime`
1273 // (the audit-batch seam is owned by whichever registry was built
1274 // over this runtime's `EventStore`, not by the runtime itself), so
1275 // the batch-health fields report unavailable with a reason here.
1276 // Callers that hold the registry — e.g. the `db_diagnostics` verb
1277 // handler — use `Self::db_diagnostics_with_audit_metrics` with
1278 // `VerbRegistry::audit_batch_metrics()` instead.
1279 self.db_diagnostics_with_audit_metrics(None).await
1280 }
1281
1282 /// As [`Self::db_diagnostics`], but with the caller supplying the
1283 /// ADR-133 audit-batch health counters from whichever `VerbRegistry`
1284 /// owns the seam over this runtime's `EventStore` (typically
1285 /// `VerbRegistry::audit_batch_metrics()`). `None` behaves identically to
1286 /// [`Self::db_diagnostics`].
1287 pub async fn db_diagnostics_with_audit_metrics(
1288 &self,
1289 runtime_audit_batch_metrics: Option<khive_db::diagnostics::RuntimeAuditBatchMetrics>,
1290 ) -> RuntimeResult<khive_db::diagnostics::DbDiagnostics> {
1291 let pool = self.core().backend.pool_arc();
1292 // Match housekeeping's compiled legacy-record fallback (ADR-091
1293 // Amendment 6), independent of checkpoint or local sweep overrides.
1294 let legacy_sweep_interval = khive_db::SessionSweepConfig::default().interval;
1295 let build_hash = crate::build_info::BUILD_INFO
1296 .is_stamped()
1297 .then_some(crate::build_info::BUILD_INFO.source_revision);
1298 let build = khive_db::diagnostics::BuildIdentity::from_env(
1299 crate::build_info::PACKAGE_VERSION,
1300 build_hash,
1301 );
1302
1303 let mut report = khive_db::diagnostics::collect_with_runtime_audit_metrics_interruptibly(
1304 pool,
1305 build,
1306 legacy_sweep_interval,
1307 crate::pack::audit_append_failure_count(),
1308 runtime_audit_batch_metrics,
1309 )
1310 .await
1311 .map_err(RuntimeError::from)?;
1312 report.writer_contention.audit_obligation_append_failures =
1313 Some(crate::pack::audit_obligation_append_failure_count());
1314 report
1315 .writer_contention
1316 .audit_obligation_append_failures_unavailable_reason = None;
1317 let (ann_routes, fallback_routes) = crate::note_search_ann::route_totals();
1318 report.note_search_ann_route_total = ann_routes;
1319 report.note_search_fallback_route_total = fallback_routes;
1320 Ok(report)
1321 }
1322
1323 /// Collect the same per-file report as the primary diagnostic surface for
1324 /// one opened backend. The process identity comes from main; invoking
1325 /// `ProcessIdentity::current` on a secondary would miscount main generations.
1326 /// `collect_size_composition = false` skips the `dbstat` walk (see
1327 /// `khive_db::diagnostics::collect_with_runtime_audit_metrics_for_process_interruptibly`).
1328 pub async fn db_diagnostics_for_opened_backend_with_audit_metrics(
1329 &self,
1330 backend: &OpenedDiagnosticBackend,
1331 runtime_audit_batch_metrics: Option<khive_db::diagnostics::RuntimeAuditBatchMetrics>,
1332 collect_size_composition: bool,
1333 ) -> RuntimeResult<khive_db::diagnostics::DbDiagnostics> {
1334 let main_pool = self.core().backend.pool_arc();
1335 let process = khive_db::diagnostics::ProcessIdentity::current(&main_pool);
1336 let build_hash = crate::build_info::BUILD_INFO
1337 .is_stamped()
1338 .then_some(crate::build_info::BUILD_INFO.source_revision);
1339 let build = khive_db::diagnostics::BuildIdentity::from_env(
1340 crate::build_info::PACKAGE_VERSION,
1341 build_hash,
1342 );
1343 let mut report =
1344 khive_db::diagnostics::collect_with_runtime_audit_metrics_for_process_interruptibly(
1345 Arc::clone(&backend.pool),
1346 build,
1347 process,
1348 khive_db::SessionSweepConfig::default().interval,
1349 crate::pack::audit_append_failure_count(),
1350 runtime_audit_batch_metrics,
1351 collect_size_composition,
1352 )
1353 .await
1354 .map_err(RuntimeError::from)?;
1355 report.writer_contention.audit_obligation_append_failures =
1356 Some(crate::pack::audit_obligation_append_failure_count());
1357 report
1358 .writer_contention
1359 .audit_obligation_append_failures_unavailable_reason = None;
1360 let (ann_routes, fallback_routes) = crate::note_search_ann::route_totals();
1361 report.note_search_ann_route_total = ann_routes;
1362 report.note_search_fallback_route_total = fallback_routes;
1363 Ok(report)
1364 }
1365
1366 // ---- Store accessors (token-scoped) ----
1367
1368 /// Get an EntityStore scoped to the token's namespace.
1369 pub fn entities(&self, token: &NamespaceToken) -> RuntimeResult<Arc<dyn EntityStore>> {
1370 Ok(self
1371 .backend
1372 .entities_for_namespace(token.namespace().as_str())?)
1373 }
1374
1375 /// Get a GraphStore scoped to the token's namespace.
1376 pub fn graph(&self, token: &NamespaceToken) -> RuntimeResult<Arc<dyn GraphStore>> {
1377 Ok(self
1378 .backend
1379 .graph_for_namespace(token.namespace().as_str())?)
1380 }
1381
1382 /// Get a NoteStore scoped to the token's namespace.
1383 ///
1384 /// Wrapped in `note_store_guard::PolicyEnforcingNoteStore`, which
1385 /// refuses any insert/upsert of a `kind = "message"` note carrying
1386 /// `quarantined` / `channel_kind` / `channel_slug` — the transport-owned
1387 /// evidence `comm.health` trusts at face value — and refuses patching
1388 /// those keys through the property-mutation seams on any note kind, so
1389 /// the guard cannot be sidestepped by inserting a clean message note and
1390 /// patching the evidence onto it afterward. Full-row writes also preserve
1391 /// existing channel-health coordinates while allowing heartbeat metadata to
1392 /// change. The trusted channel-ingest path does not go through this accessor; see
1393 /// `Self::raw_notes` and [`Self::try_create_note_as_trusted_ingest`].
1394 pub fn notes(&self, token: &NamespaceToken) -> RuntimeResult<Arc<dyn NoteStore>> {
1395 Ok(crate::note_store_guard::PolicyEnforcingNoteStore::wrap(
1396 self.raw_notes(token)?,
1397 ))
1398 }
1399
1400 /// Get the unwrapped, policy-free NoteStore scoped to the token's namespace.
1401 ///
1402 /// Bypasses `note_store_guard::PolicyEnforcingNoteStore`. Callers
1403 /// within this crate that have already enforced the reserved-transport-
1404 /// property policy themselves (namely `try_create_note_impl`, which
1405 /// applies it conditionally based on whether the caller presented a
1406 /// [`crate::pack::ChannelIngestCapability`]) use this to reach storage
1407 /// directly rather than run a redundant, less-informed check. Not exposed
1408 /// outside this crate — every other caller must use [`Self::notes`].
1409 pub(crate) fn raw_notes(&self, token: &NamespaceToken) -> RuntimeResult<Arc<dyn NoteStore>> {
1410 Ok(self
1411 .backend
1412 .notes_for_namespace(token.namespace().as_str())?)
1413 }
1414
1415 /// Return the role-keyed attachment substrate on the canonical main backend.
1416 ///
1417 /// Attachment rows are the process-shared BlobStore's sole SQL liveness
1418 /// authority. A runtime bound directly to a secondary pack backend must call
1419 /// [`Self::core`] first; accepting a secondary mutation here would create a
1420 /// reference that the main-database GC sweep cannot see or fence.
1421 pub fn attachments(&self) -> RuntimeResult<Arc<dyn AttachmentStore>> {
1422 if self.config.backend_id.as_str() != BackendId::MAIN {
1423 return Err(RuntimeError::InvalidInput(format!(
1424 "attachments are owned by the canonical main backend; runtime backend {:?} must route through KhiveRuntime::core()",
1425 self.config.backend_id.as_str()
1426 )));
1427 }
1428 Ok(self.backend.attachments()?)
1429 }
1430
1431 /// Get an EventStore scoped to the token's namespace.
1432 ///
1433 /// When the events-daemon split (ADR-170) is configured, the store routes
1434 /// by append class: the ADR-133 idempotent audit-batch lane — the
1435 /// measured bulk of event write volume — persists to the events database
1436 /// (forwarded over the events daemon socket in daemon deployments, or
1437 /// opened directly in embedded/one-shot contexts), while plain appends
1438 /// stay on this runtime's backend, keeping every raw-SQL consumer of the
1439 /// legacy `events` table (schedule provenance, kg projection guards,
1440 /// GraphQuery's substrate union) correct by construction. Reads merge
1441 /// both stores. Unconfigured runtimes (tests, in-memory) keep the legacy
1442 /// main-store behavior. Every returned store is decorated at this typed
1443 /// accessor boundary so append callers cannot override the namespace or
1444 /// actor resolved into the sealed authorization token.
1445 pub fn events(&self, token: &NamespaceToken) -> RuntimeResult<Arc<dyn EventStore>> {
1446 Ok(crate::event_store_guard::AttributedEventStore::wrap(
1447 self.raw_events_for_namespace(token.namespace().as_str())?,
1448 token,
1449 ))
1450 }
1451
1452 /// The event sidecar inherits the already-open MAIN pool's WAL policy.
1453 /// A secondary pack's config or pool does not govern this shared file.
1454 fn events_wal_ceiling_policy(&self) -> khive_db::WalCeilingPolicy {
1455 self.core_backend
1456 .as_ref()
1457 .unwrap_or(&self.backend)
1458 .pool()
1459 .config()
1460 .wal_ceiling
1461 }
1462
1463 /// Build the undecorated event store used only by the registry's trusted
1464 /// audit composer, which stamps from each resolved `GateRequest` before
1465 /// enqueueing. Pack/runtime call sites must use [`Self::events`] instead.
1466 pub(crate) fn raw_events_for_namespace(
1467 &self,
1468 namespace: &str,
1469 ) -> RuntimeResult<Arc<dyn EventStore>> {
1470 let legacy = self.backend.events_for_namespace(namespace)?;
1471 match &self.config.events_split {
1472 None => Ok(legacy),
1473 Some(split) => {
1474 // Read-only is decided before the transport question: a
1475 // read-only runtime must neither create nor schema-initialize
1476 // an events database, and it must not forward writes to the
1477 // events daemon either — a socket in the config describes the
1478 // deployment, not this process's authority. Serve merged
1479 // reads from a read-only open of the sidecar when it exists
1480 // (the storage-layer read-only binding refuses any write that
1481 // slips through), and the legacy store alone otherwise: no
1482 // sidecar on disk means no lane rows exist, so minting the
1483 // file just to read nothing from it would be a write in
1484 // disguise.
1485 if self.backend.is_read_only() {
1486 if !split.db_path.exists() {
1487 return Ok(legacy);
1488 }
1489 let lane_backend = crate::events_split::direct_backend_with_policies(
1490 &split.db_path,
1491 true,
1492 Some(self.backend.pool().config().max_readers),
1493 self.events_wal_ceiling_policy(),
1494 None,
1495 self.events_volume_lock_dir(),
1496 )?;
1497 self.register_late_diagnostic_pool("events", &lane_backend.pool_arc());
1498 let lane = lane_backend.events_for_namespace(namespace)?;
1499 return Ok(Arc::new(crate::events_split::SplitEventStore::new(
1500 legacy, lane,
1501 )));
1502 }
1503 let lane: Arc<dyn EventStore> = match &split.socket_path {
1504 #[cfg(unix)]
1505 Some(socket) => {
1506 let client = crate::events_split::client_for(socket)?;
1507 Arc::new(crate::events_split::ForwardingEventStore::new(
1508 namespace, client,
1509 ))
1510 }
1511 #[cfg(not(unix))]
1512 Some(_socket) => {
1513 return Err(RuntimeError::InvalidInput(
1514 "events-daemon socket forwarding requires a Unix platform; \
1515 configure the events split in direct mode here"
1516 .to_string(),
1517 ));
1518 }
1519 None => {
1520 let lane_backend = crate::events_split::direct_backend_with_policies(
1521 &split.db_path,
1522 false,
1523 Some(self.backend.pool().config().max_readers),
1524 self.events_wal_ceiling_policy(),
1525 Some(self.events_disk_guard_policy()?),
1526 self.events_volume_lock_dir(),
1527 )?;
1528 self.register_late_diagnostic_pool("events", &lane_backend.pool_arc());
1529 lane_backend.events_for_namespace(namespace)?
1530 }
1531 };
1532 Ok(Arc::new(crate::events_split::SplitEventStore::new(
1533 legacy, lane,
1534 )))
1535 }
1536 }
1537 }
1538
1539 /// Get the raw SQL access capability (for ad-hoc queries).
1540 pub fn sql(&self) -> Arc<dyn SqlAccess> {
1541 self.backend.sql()
1542 }
1543
1544 /// SQL access to the events-split sidecar database for read purposes,
1545 /// when the split (ADR-170) is configured and the sidecar exists on
1546 /// disk. `None` means every event row lives in the legacy `events`
1547 /// table, so a raw-SQL consumer needs no second lookup. Consumers that
1548 /// resolve an event by id or hex prefix against `self.sql()` must also
1549 /// consult this store on a miss: the audit-batch lane's rows live only
1550 /// in the sidecar.
1551 ///
1552 /// A writable runtime opens the sidecar through the ordinary writable
1553 /// binding: WAL supports one-writer-many-readers, and the read-only
1554 /// binding's frozen-snapshot guard refuses any sidecar with a live
1555 /// writer's `-shm` beside it — exactly the live-deployment case these
1556 /// reads exist for. A read-only runtime keeps the read-only open (it
1557 /// must neither create nor schema-initialize a sidecar), which serves
1558 /// genuinely frozen snapshots and refuses live ones, matching the
1559 /// read-only arm of `events()`. Never creates a sidecar as a side
1560 /// effect of a read.
1561 pub fn events_sidecar_sql_read_only(&self) -> RuntimeResult<Option<Arc<dyn SqlAccess>>> {
1562 match &self.config.events_split {
1563 None => Ok(None),
1564 Some(split) => {
1565 if !split.db_path.exists() {
1566 return Ok(None);
1567 }
1568 let backend = if self.backend.is_read_only() {
1569 crate::events_split::direct_backend_with_policies(
1570 &split.db_path,
1571 true,
1572 Some(self.backend.pool().config().max_readers),
1573 self.events_wal_ceiling_policy(),
1574 None,
1575 self.events_volume_lock_dir(),
1576 )?
1577 } else {
1578 crate::events_split::direct_backend_with_policies(
1579 &split.db_path,
1580 false,
1581 Some(self.backend.pool().config().max_readers),
1582 self.events_wal_ceiling_policy(),
1583 Some(self.events_disk_guard_policy()?),
1584 self.events_volume_lock_dir(),
1585 )?
1586 };
1587 self.register_late_diagnostic_pool("events", &backend.pool_arc());
1588 Ok(Some(backend.sql()))
1589 }
1590 }
1591 }
1592
1593 /// Get a VectorStore for the configured embedding model, scoped to the token's namespace.
1594 ///
1595 /// Returns `Unconfigured("embedding_model")` if no model is set.
1596 pub fn vectors(
1597 &self,
1598 token: &NamespaceToken,
1599 ) -> RuntimeResult<Arc<dyn khive_storage::VectorStore>> {
1600 let name = self.default_embedder_name();
1601 if name.is_empty() {
1602 return Err(RuntimeError::Unconfigured("embedding_model".into()));
1603 }
1604 self.vectors_for_model(token, name)
1605 }
1606
1607 /// Get a VectorStore for a specific named embedding model, scoped to the token's namespace.
1608 ///
1609 /// Accepts both built-in lattice model names/aliases and custom provider names
1610 /// registered via [`register_embedder`](Self::register_embedder). Lattice names
1611 /// are routed through the enum-backed path; custom provider names use the
1612 /// provider's declared `dimensions()` directly so that the vector store key
1613 /// is consistent with how vectors were written during `remember`/`recall`.
1614 pub fn vectors_for_model(
1615 &self,
1616 token: &NamespaceToken,
1617 model_name: &str,
1618 ) -> RuntimeResult<Arc<dyn khive_storage::VectorStore>> {
1619 let (model_name, dims) = self.vector_model_metadata(model_name)?;
1620 Ok(self.backend.vectors_for_namespace(
1621 &sanitize_key(&model_name),
1622 &model_name,
1623 dims,
1624 token.namespace().as_str(),
1625 )?)
1626 }
1627
1628 /// Resolve the storage identity and declared dimensions together so guarded
1629 /// SQL publication agrees with VectorStore, including built-in aliases.
1630 pub(crate) fn vector_model_metadata(&self, model_name: &str) -> RuntimeResult<(String, usize)> {
1631 if request_excludes_embedder(model_name) {
1632 return Err(crate::RuntimeError::UnknownModel(model_name.to_string()));
1633 }
1634 let registry = self
1635 .embedder_registry
1636 .read()
1637 .map_err(|_| crate::RuntimeError::Internal("embedder registry lock poisoned".into()))?;
1638 if let Some(model) = parse_embedding_model_alias(model_name) {
1639 // Only proceed via the lattice path if this model is actually in the
1640 // registry; otherwise fall through to the custom-provider path.
1641 let key = model.to_string();
1642 if registry.contains(&key) {
1643 return Ok((key, model.dimensions()));
1644 }
1645 }
1646 registry
1647 .get_provider(model_name)
1648 .map(|provider| (model_name.to_owned(), provider.dimensions()))
1649 .ok_or_else(|| crate::RuntimeError::UnknownModel(model_name.to_string()))
1650 }
1651
1652 /// Get a namespace-scoped vector store for a pack-owned immutable identity.
1653 ///
1654 /// The table key is syntactically validated by [`NamedVectorIdentity`]. This
1655 /// accessor additionally verifies the table's actual sqlite-vec dimension
1656 /// declaration and every persisted `embedding_model` value before returning
1657 /// the store, so reusing one key for incompatible descriptor geometry or
1658 /// semantics fails before a caller can replace rows.
1659 pub async fn vectors_for_named_identity(
1660 &self,
1661 token: &NamespaceToken,
1662 identity: &NamedVectorIdentity,
1663 ) -> RuntimeResult<Arc<dyn VectorStore>> {
1664 let namespace = token.namespace().as_str();
1665 {
1666 let cached = self.named_vector_stores.read().map_err(|_| {
1667 RuntimeError::Internal("named vector store cache lock poisoned".into())
1668 })?;
1669 if let Some(entry) = cached.get(identity.model_key()) {
1670 check_cached_named_vector_identity(&entry.identity, identity)?;
1671 if let Some(store) = entry.by_namespace.get(namespace) {
1672 return Ok(Arc::clone(store));
1673 }
1674 }
1675 }
1676 let store = self.backend.vectors_for_namespace(
1677 identity.model_key(),
1678 identity.model_name(),
1679 identity.dimensions(),
1680 namespace,
1681 )?;
1682
1683 let table = format!("vec_{}", identity.model_key());
1684 let mut reader = self.sql().reader().await?;
1685 let dimension_row = reader
1686 .query_row(SqlStatement {
1687 sql: "SELECT sql FROM sqlite_schema WHERE type = 'table' AND name = ?1".to_string(),
1688 params: vec![SqlValue::Text(table.clone())],
1689 label: Some("runtime_named_vector_dimension".to_string()),
1690 })
1691 .await?
1692 .ok_or_else(|| {
1693 RuntimeError::Internal(format!(
1694 "named vector table {table} has no sqlite_schema declaration"
1695 ))
1696 })?;
1697 let table_ddl = match dimension_row.get("sql") {
1698 Some(SqlValue::Text(value)) => value,
1699 other => {
1700 return Err(RuntimeError::Internal(format!(
1701 "named vector table {table} returned invalid schema metadata: {other:?}"
1702 )))
1703 }
1704 };
1705 let declared_dimensions = vector_dimensions_from_ddl(table_ddl).ok_or_else(|| {
1706 RuntimeError::Internal(format!(
1707 "named vector table {table} has no parseable embedding dimension"
1708 ))
1709 })?;
1710 if declared_dimensions != identity.dimensions() {
1711 return Err(RuntimeError::InvalidInput(format!(
1712 "named vector model_key {:?} is already bound to {declared_dimensions} dimensions, expected {}",
1713 identity.model_key(),
1714 identity.dimensions()
1715 )));
1716 }
1717
1718 let stored_models = reader
1719 .query_all(SqlStatement {
1720 sql: format!(
1721 "SELECT DISTINCT embedding_model FROM {table} ORDER BY embedding_model LIMIT 2"
1722 ),
1723 params: vec![],
1724 label: Some("runtime_named_vector_model_identity".to_string()),
1725 })
1726 .await?;
1727 for row in stored_models {
1728 let stored = match row.get("embedding_model") {
1729 Some(SqlValue::Text(value)) => value,
1730 other => {
1731 return Err(RuntimeError::Internal(format!(
1732 "named vector table {table} returned invalid model identity metadata: {other:?}"
1733 )))
1734 }
1735 };
1736 if stored != identity.model_name() {
1737 return Err(RuntimeError::InvalidInput(format!(
1738 "named vector model_key {:?} already contains model {stored:?}, cannot bind it to {:?}",
1739 identity.model_key(),
1740 identity.model_name()
1741 )));
1742 }
1743 }
1744
1745 self.backend
1746 .register_embedding_model(
1747 identity.model_key(),
1748 identity.model_name(),
1749 identity.model_key(),
1750 identity.dimensions() as u32,
1751 )
1752 .map_err(|error| {
1753 if matches!(
1754 &error,
1755 khive_db::SqliteError::Rusqlite(rusqlite::Error::SqliteFailure(code, _))
1756 | khive_db::SqliteError::Write { source: rusqlite::Error::SqliteFailure(code, _), .. }
1757 if code.code == rusqlite::ErrorCode::ConstraintViolation
1758 ) {
1759 RuntimeError::InvalidInput(format!(
1760 "named vector model_key {:?} is already bound to a different active model identity",
1761 identity.model_key()
1762 ))
1763 } else {
1764 RuntimeError::from(error)
1765 }
1766 })?;
1767
1768 let mut cached = self
1769 .named_vector_stores
1770 .write()
1771 .map_err(|_| RuntimeError::Internal("named vector store cache lock poisoned".into()))?;
1772 let entry = cached
1773 .entry(identity.model_key().to_owned())
1774 .or_insert_with(|| CachedNamedVectorStores {
1775 identity: identity.clone(),
1776 by_namespace: HashMap::new(),
1777 });
1778 check_cached_named_vector_identity(&entry.identity, identity)?;
1779 entry
1780 .by_namespace
1781 .insert(namespace.to_owned(), Arc::clone(&store));
1782 Ok(store)
1783 }
1784
1785 /// Output dimensions for a named embedding model, resolved from the
1786 /// embedder registry alone — no storage access. Mirrors
1787 /// [`vectors_for_model`](Self::vectors_for_model)'s resolution order:
1788 /// lattice aliases route through the enum when registered, otherwise the
1789 /// custom provider's declared `dimensions()`. `None` when no such model
1790 /// is registered.
1791 pub fn embedder_dimensions(&self, model_name: &str) -> Option<usize> {
1792 if request_excludes_embedder(model_name) {
1793 return None;
1794 }
1795 if let Some(model) = parse_embedding_model_alias(model_name) {
1796 let key = model.to_string();
1797 let in_registry = self
1798 .embedder_registry
1799 .read()
1800 .map(|reg| reg.contains(&key))
1801 .unwrap_or(false);
1802 if in_registry {
1803 return Some(model.dimensions());
1804 }
1805 }
1806 self.embedder_registry
1807 .read()
1808 .ok()?
1809 .get_provider(model_name)
1810 .map(|p| p.dimensions())
1811 }
1812
1813 /// Get a TextSearch index for the entity corpus (single shared table).
1814 pub fn text(
1815 &self,
1816 token: &NamespaceToken,
1817 ) -> RuntimeResult<Arc<dyn khive_storage::TextSearch>> {
1818 let _ = token;
1819 Ok(self.backend.text("entities")?)
1820 }
1821
1822 /// Get a TextSearch index for the notes corpus (single shared table).
1823 pub fn text_for_notes(
1824 &self,
1825 token: &NamespaceToken,
1826 ) -> RuntimeResult<Arc<dyn khive_storage::TextSearch>> {
1827 let _ = token;
1828 Ok(self.backend.text("notes")?)
1829 }
1830
1831 /// Mint an authorization token for the given namespace.
1832 ///
1833 /// Consults the configured [`crate::Gate`] before minting. With the default
1834 /// `AllowAllGate` this always succeeds. When a real policy-backed gate is
1835 /// installed, this method enforces it and returns `PermissionDenied` on
1836 /// denial.
1837 ///
1838 /// The returned token's read visibility set defaults to `[ns]` — identical
1839 /// to the pre-visibility-set behaviour. Use [`Self::authorize_with_visibility`]
1840 /// to mint a token that can read additional namespaces.
1841 ///
1842 /// When `actor_id` is configured in `RuntimeConfig`, the token carries that
1843 /// actor label so that `comm.inbox` filters by `to_actor`. When
1844 /// unconfigured, the token carries `ActorRef::anonymous()` and inbox falls
1845 /// back to party-line behavior.
1846 pub fn authorize(&self, ns: Namespace) -> RuntimeResult<NamespaceToken> {
1847 let actor = crate::actor_identity::resolve_actor(self.config.actor_id.as_deref());
1848 let req = GateRequest::new(
1849 actor.clone(),
1850 ns.clone(),
1851 "authorize",
1852 serde_json::Value::Null,
1853 );
1854 match self.config.gate.check(&req) {
1855 Ok(ref decision) if decision.is_allow() => {
1856 if let khive_gate::GateDecision::Allow { ref obligations } = decision {
1857 if !obligations.is_empty() {
1858 tracing::debug!(
1859 namespace = %ns.as_str(),
1860 "authorize: obligations={:?}",
1861 obligations
1862 );
1863 }
1864 }
1865 Ok(NamespaceToken::mint_authorized(ns, actor))
1866 }
1867 Ok(khive_gate::GateDecision::Deny { reason }) => {
1868 Err(crate::RuntimeError::permission_denied("authorize", reason))
1869 }
1870 Ok(_) => Err(crate::RuntimeError::permission_denied(
1871 "authorize",
1872 "gate denied",
1873 )),
1874 Err(e) => {
1875 tracing::warn!(
1876 namespace = %ns.as_str(),
1877 error = %crate::secret_gate::bounded_masked_log_text(&e.to_string()),
1878 "authorize: gate check failed (fail-closed)"
1879 );
1880 Err(crate::RuntimeError::Internal(format!(
1881 "gate error: {}",
1882 e.wire_reason()
1883 )))
1884 }
1885 }
1886 }
1887
1888 /// Mint an authorization token with an explicit read-visibility set.
1889 ///
1890 /// `primary` is the **write namespace** — all records created via the
1891 /// returned token land there. `extra_visible` lists additional namespaces
1892 /// the token may read. The primary is always included in the visible set
1893 /// regardless of `extra_visible`.
1894 ///
1895 /// Usage (lambda:leo reading both leo and khive namespaces):
1896 /// ```rust,ignore
1897 /// let tok = rt.authorize_with_visibility(
1898 /// Namespace::parse("lambda:leo").unwrap(),
1899 /// vec![Namespace::parse("lambda:khive").unwrap()],
1900 /// )?;
1901 /// ```
1902 pub fn authorize_with_visibility(
1903 &self,
1904 primary: Namespace,
1905 extra_visible: Vec<Namespace>,
1906 ) -> RuntimeResult<NamespaceToken> {
1907 let actor = crate::actor_identity::resolve_actor(self.config.actor_id.as_deref());
1908 let req = GateRequest::new(
1909 actor.clone(),
1910 primary.clone(),
1911 "authorize",
1912 serde_json::Value::Null,
1913 );
1914 match self.config.gate.check(&req) {
1915 Ok(ref decision) if decision.is_allow() => {
1916 if let khive_gate::GateDecision::Allow { ref obligations } = decision {
1917 if !obligations.is_empty() {
1918 tracing::debug!(
1919 namespace = %primary.as_str(),
1920 "authorize_with_visibility: obligations={:?}",
1921 obligations
1922 );
1923 }
1924 }
1925 // The primary check authorizes writes to `primary` only. Each
1926 // extra namespace grants read visibility, so each one takes
1927 // its own Read-classified gate check before it may enter the
1928 // minted set — a token must never carry visibility the gate
1929 // was not asked about. Any deny or gate error refuses the
1930 // whole mint, naming the offending namespace (fail-closed).
1931 for extra in &extra_visible {
1932 let extra_req = GateRequest::new(
1933 actor.clone(),
1934 extra.clone(),
1935 "authorize.visible",
1936 serde_json::Value::Null,
1937 );
1938 match self.config.gate.check(&extra_req) {
1939 Ok(ref extra_decision) if extra_decision.is_allow() => {}
1940 Ok(khive_gate::GateDecision::Deny { reason }) => {
1941 return Err(crate::RuntimeError::permission_denied(
1942 "authorize",
1943 format!(
1944 "visibility namespace {:?} denied: {reason}",
1945 extra.as_str()
1946 ),
1947 ));
1948 }
1949 Ok(_) => {
1950 return Err(crate::RuntimeError::permission_denied(
1951 "authorize",
1952 format!("visibility namespace {:?} denied by gate", extra.as_str()),
1953 ));
1954 }
1955 Err(e) => {
1956 tracing::warn!(
1957 namespace = %extra.as_str(),
1958 error = %crate::secret_gate::bounded_masked_log_text(&e.to_string()),
1959 "authorize_with_visibility: extra-namespace gate check failed (fail-closed)"
1960 );
1961 return Err(crate::RuntimeError::Internal(format!(
1962 "gate error: {}",
1963 e.wire_reason()
1964 )));
1965 }
1966 }
1967 }
1968 Ok(NamespaceToken::mint_with_visibility(
1969 primary,
1970 extra_visible,
1971 actor,
1972 ))
1973 }
1974 Ok(khive_gate::GateDecision::Deny { reason }) => {
1975 Err(crate::RuntimeError::permission_denied("authorize", reason))
1976 }
1977 Ok(_) => Err(crate::RuntimeError::permission_denied(
1978 "authorize",
1979 "gate denied",
1980 )),
1981 Err(e) => {
1982 tracing::warn!(
1983 namespace = %primary.as_str(),
1984 error = %crate::secret_gate::bounded_masked_log_text(&e.to_string()),
1985 "authorize_with_visibility: gate check failed (fail-closed)"
1986 );
1987 Err(crate::RuntimeError::Internal(format!(
1988 "gate error: {}",
1989 e.wire_reason()
1990 )))
1991 }
1992 }
1993 }
1994
1995 /// Install the pack-aggregated edge endpoint rules.
1996 ///
1997 /// Called by the transport layer after the `VerbRegistry` is built so
1998 /// that runtime-layer edge validation can consult pack rules. Idempotent:
1999 /// later calls overwrite the previous rule set.
2000 pub fn install_edge_rules(&self, rules: Vec<EdgeEndpointRule>) {
2001 if let Ok(mut guard) = self.edge_rules.write() {
2002 *guard = rules;
2003 }
2004 }
2005
2006 /// Install an already-paired blob hydrator into this runtime.
2007 ///
2008 /// Reinstalling the exact same `Arc` is idempotent. A different pair is
2009 /// rejected: replacing it would split or reset the aggregate admission
2010 /// budget while requests may still hold leases.
2011 pub fn install_blob_hydrator(
2012 &self,
2013 hydrator: Arc<crate::blob::BlobHydrator>,
2014 ) -> RuntimeResult<()> {
2015 if hydrator.budget_bytes() != self.config.blob_hydration_bytes {
2016 return Err(RuntimeError::InvalidInput(format!(
2017 "blob hydrator budget {} does not match this runtime's resolved budget {}",
2018 hydrator.budget_bytes(),
2019 self.config.blob_hydration_bytes
2020 )));
2021 }
2022 // The mode gate must sit on THIS seam, not only on `install_blob_store`:
2023 // `BlobHydrator::new` is public, so without it a caller pairs a writable
2024 // store, installs it here, and `blob_store()` hands mutating pack paths
2025 // a writable store on a runtime whose declared mode is read-only.
2026 // Boot paths installing one hydrator across handles of MIXED modes —
2027 // where the blob pack's own backend mode, not each receiving
2028 // handle's, governs mutability — go through
2029 // [`Self::install_shared_blob_hydrator`] instead.
2030 if self.is_read_only() && !hydrator.enforces_read_only() {
2031 return Err(RuntimeError::InvalidInput(
2032 "this runtime is read-only: install the raw store with install_blob_store, \
2033 which wraps it so every physical mutator refuses"
2034 .to_string(),
2035 ));
2036 }
2037 self.install_blob_hydrator_slot(hydrator)
2038 }
2039
2040 /// Install a boot-shared hydrator whose mutability is governed by the
2041 /// blob runtime's own mode, not this handle's domain-store mode.
2042 ///
2043 /// ADR-160 D3 installs one hydrator `Arc` on every runtime handle a boot
2044 /// produces, and the documented multi-backend matrix includes a writable
2045 /// blob secondary beside a read-only main: there the shared hydrator is
2046 /// legitimately writable on a read-only domain handle. The mode decision
2047 /// must therefore already be encoded in the hydrator, and it must have
2048 /// been DERIVED, not declared: only hydrators built through
2049 /// [`crate::BlobHydrator::resolve_for_governing_backend`] — whose mode
2050 /// comes from the governing backend's own access mode — are accepted
2051 /// here. A hand-paired hydrator (`BlobHydrator::new` / `for_mode`) is
2052 /// refused so a safe downstream caller cannot use this seam to put a
2053 /// writable store on a read-only runtime; such callers use
2054 /// [`Self::install_blob_hydrator`], which holds hydrator mode against
2055 /// this runtime's own.
2056 ///
2057 /// This gate is a wrong-wiring guard, not an in-process sandbox: which
2058 /// backend governs is the boot host's topology assertion, and a caller
2059 /// who deliberately selects an unrelated writable backend as governing
2060 /// is outside what any runtime seam can enforce (see the trust-model
2061 /// note on [`crate::BlobHydrator::resolve_for_governing_backend`]).
2062 pub fn install_shared_blob_hydrator(
2063 &self,
2064 hydrator: Arc<crate::blob::BlobHydrator>,
2065 ) -> RuntimeResult<()> {
2066 if !hydrator.is_governed() {
2067 return Err(RuntimeError::InvalidInput(
2068 "the shared install seam accepts only hydrators whose mode was derived from a \
2069 governing backend (BlobHydrator::resolve_for_governing_backend); use \
2070 install_blob_hydrator for a hand-paired hydrator"
2071 .to_string(),
2072 ));
2073 }
2074 if hydrator.budget_bytes() != self.config.blob_hydration_bytes {
2075 return Err(RuntimeError::InvalidInput(format!(
2076 "blob hydrator budget {} does not match this runtime's resolved budget {}",
2077 hydrator.budget_bytes(),
2078 self.config.blob_hydration_bytes
2079 )));
2080 }
2081 self.install_blob_hydrator_slot(hydrator)
2082 }
2083
2084 /// Whether `candidate` is the same pairing as the installed `current`:
2085 /// the exact `Arc`, or a distinct hydrator allocation over the same raw
2086 /// store with the same budget and mode. The latter arises when two
2087 /// concurrent first installs each construct a hydrator from one raw
2088 /// store — the `OnceLock` loser must read as idempotent, not as a
2089 /// conflicting install.
2090 fn is_same_blob_pairing(
2091 current: &Arc<crate::blob::BlobHydrator>,
2092 candidate: &Arc<crate::blob::BlobHydrator>,
2093 ) -> bool {
2094 Arc::ptr_eq(current, candidate)
2095 || (Arc::ptr_eq(¤t.raw_store(), &candidate.raw_store())
2096 && current.budget_bytes() == candidate.budget_bytes()
2097 && current.enforces_read_only() == candidate.enforces_read_only())
2098 }
2099
2100 /// One-shot slot semantics shared by both install seams: first install
2101 /// wins, an equivalent pairing is idempotent, a different pairing is
2102 /// refused (replacing it would split or reset the aggregate admission
2103 /// budget while requests may still hold leases).
2104 fn install_blob_hydrator_slot(
2105 &self,
2106 hydrator: Arc<crate::blob::BlobHydrator>,
2107 ) -> RuntimeResult<()> {
2108 if let Some(current) = self.blob_hydrator.get() {
2109 return if Self::is_same_blob_pairing(current, &hydrator) {
2110 Ok(())
2111 } else {
2112 Err(RuntimeError::InvalidInput(
2113 "a different blob hydrator is already installed".to_string(),
2114 ))
2115 };
2116 }
2117
2118 match self.blob_hydrator.set(hydrator) {
2119 Ok(()) => Ok(()),
2120 Err(candidate) => {
2121 let current = self.blob_hydrator.get().ok_or_else(|| {
2122 RuntimeError::Internal(
2123 "blob hydrator install raced without a visible winner".to_string(),
2124 )
2125 })?;
2126 if Self::is_same_blob_pairing(current, &candidate) {
2127 Ok(())
2128 } else {
2129 Err(RuntimeError::InvalidInput(
2130 "a different blob hydrator is already installed".to_string(),
2131 ))
2132 }
2133 }
2134 }
2135 }
2136
2137 /// Pair and install a store using this runtime's resolved hydration budget.
2138 ///
2139 /// Boot paths that own multiple runtimes should instead construct one
2140 /// [`crate::BlobHydrator`] and call [`Self::install_blob_hydrator`] with
2141 /// the same `Arc` on every handle.
2142 pub fn install_blob_store(
2143 &self,
2144 store: Arc<dyn khive_storage::BlobStore>,
2145 ) -> RuntimeResult<()> {
2146 if let Some(current) = self.blob_hydrator.get() {
2147 // Reinstalling the same raw store is idempotent in BOTH modes:
2148 // on a read-only runtime the installed hydrator wraps the raw
2149 // store, so identity is checked against the raw handle the
2150 // hydrator remembers, not only the (possibly wrapped) paired one.
2151 let current_store = current.store();
2152 if Arc::ptr_eq(¤t_store, &store) || Arc::ptr_eq(¤t.raw_store(), &store) {
2153 return Ok(());
2154 }
2155 }
2156 // A read-only runtime holds its mode at this seam, not only during
2157 // boot resolution: an arbitrary store installed after launch is
2158 // wrapped so every physical mutator refuses while the bounded read
2159 // surface stays available. Without this, post-boot installation is a
2160 // writable bypass of the runtime's declared mode.
2161 let hydrator = if self.is_read_only() {
2162 crate::blob::BlobHydrator::new_read_only(store, self.config.blob_hydration_bytes)?
2163 } else {
2164 crate::blob::BlobHydrator::new(store, self.config.blob_hydration_bytes)?
2165 };
2166 self.install_blob_hydrator(Arc::new(hydrator))
2167 }
2168
2169 /// Return the installed shared blob hydrator, if boot configured one.
2170 pub fn blob_hydrator(&self) -> Option<Arc<crate::blob::BlobHydrator>> {
2171 self.blob_hydrator.get().cloned()
2172 }
2173
2174 /// Return the installed `BlobStore`, if the boot path resolved and
2175 /// installed one. `None` when no `[storage.blob]` selection was ever
2176 /// installed — e.g. a bare/test runtime constructed without going
2177 /// through the `khive-mcp` boot path.
2178 pub fn blob_store(&self) -> Option<Arc<dyn khive_storage::BlobStore>> {
2179 self.blob_hydrator.get().map(|hydrator| hydrator.store())
2180 }
2181
2182 /// Return the installed `BlobStore`, or `RuntimeError::Unconfigured` with
2183 /// the operator-facing message when none is installed.
2184 ///
2185 /// Packs share this conversion so an operator sees one error wherever the
2186 /// missing `[storage.blob]` configuration is first hit.
2187 pub fn require_blob_store(&self) -> RuntimeResult<Arc<dyn khive_storage::BlobStore>> {
2188 self.blob_store().ok_or_else(Self::no_blob_store)
2189 }
2190
2191 /// Return the installed shared blob hydrator, or `RuntimeError::Unconfigured`
2192 /// with the same message as [`Self::require_blob_store`] when none is
2193 /// installed. The store is read from the hydrator, so both accessors are
2194 /// unset under the same condition.
2195 pub fn require_blob_hydrator(&self) -> RuntimeResult<Arc<crate::blob::BlobHydrator>> {
2196 self.blob_hydrator().ok_or_else(Self::no_blob_store)
2197 }
2198
2199 fn no_blob_store() -> RuntimeError {
2200 RuntimeError::Unconfigured(
2201 "no BlobStore installed on this server (configure [storage.blob] in khive.toml, or \
2202 KHIVE_BLOB_ROOT)"
2203 .to_string(),
2204 )
2205 }
2206
2207 /// Install the pack-aggregated valid entity and note kinds.
2208 ///
2209 /// Called by the transport layer after the `VerbRegistry` is built so that
2210 /// runtime-layer entity/note creation and import validate kind strings against
2211 /// the merged pack vocabulary. Idempotent: later calls overwrite previous sets.
2212 ///
2213 /// When no kinds are installed (empty lists), kind validation is skipped at
2214 /// the runtime layer. The pack handler layer remains the primary enforcement
2215 /// point; this provides defense-in-depth for direct Rust callers and import.
2216 pub fn install_kind_registry(&self, entity_kinds: Vec<String>, note_kinds: Vec<String>) {
2217 if let Ok(mut guard) = self.valid_entity_kinds.write() {
2218 *guard = entity_kinds;
2219 }
2220 if let Ok(mut guard) = self.valid_note_kinds.write() {
2221 let prior = std::mem::take(&mut *guard);
2222 *guard = note_kinds
2223 .into_iter()
2224 .map(|name| NoteKindEntry {
2225 embedding_policy: prior
2226 .iter()
2227 .find(|entry| entry.name == name)
2228 .map(|entry| entry.embedding_policy)
2229 .unwrap_or_default(),
2230 name,
2231 registered: true,
2232 })
2233 .collect();
2234 }
2235 }
2236
2237 /// Install pack-declared embedding policy on the note-kind registry.
2238 /// The transport calls this for every runtime after pack registration.
2239 pub fn install_note_embedding_policies(&self, policies: &[crate::NoteEmbeddingPolicySpec]) {
2240 if let Ok(mut guard) = self.valid_note_kinds.write() {
2241 for spec in policies {
2242 if let Some(entry) = guard.iter_mut().find(|entry| entry.name == spec.kind) {
2243 entry.embedding_policy = spec.policy;
2244 } else {
2245 guard.push(NoteKindEntry {
2246 name: spec.kind.to_owned(),
2247 embedding_policy: spec.policy,
2248 registered: false,
2249 });
2250 }
2251 }
2252 }
2253 }
2254
2255 /// Registered models selected by the installed embedding policy for a note kind.
2256 /// Unknown kinds retain the all-models default.
2257 pub fn embedding_models_for_note_kind(&self, kind: &str) -> Vec<String> {
2258 let policy = self
2259 .valid_note_kinds
2260 .read()
2261 .ok()
2262 .and_then(|guard| {
2263 guard
2264 .iter()
2265 .find(|entry| entry.name == kind)
2266 .map(|entry| entry.embedding_policy)
2267 })
2268 .unwrap_or_default();
2269 let models = self.registered_embedding_model_names();
2270 match policy {
2271 crate::NoteEmbeddingPolicy::AllModels => models,
2272 crate::NoteEmbeddingPolicy::DefaultModel => {
2273 let default = self.default_embedder_name();
2274 models
2275 .into_iter()
2276 .filter(|name| name.as_str() == default)
2277 .collect()
2278 }
2279 }
2280 }
2281
2282 /// Install the pack-owned note kinds aggregated from the pack registry.
2283 ///
2284 /// Called by the transport after the `VerbRegistry` is built, same timing
2285 /// as [`install_kind_registry`](Self::install_kind_registry).
2286 pub fn install_pack_owned_note_kinds(&self, kinds: Vec<String>) {
2287 if let Ok(mut guard) = self.pack_owned_note_kinds.write() {
2288 *guard = kinds;
2289 }
2290 }
2291
2292 /// Whether `kind` is a note kind owned by a pack (see
2293 /// [`install_pack_owned_note_kinds`](Self::install_pack_owned_note_kinds)).
2294 ///
2295 /// Always `false` before the transport installs the list — a bare runtime
2296 /// has no packs, so no kind is pack-owned there.
2297 pub fn is_pack_owned_note_kind(&self, kind: &str) -> bool {
2298 self.pack_owned_note_kinds
2299 .read()
2300 .map(|g| g.iter().any(|k| k == kind))
2301 .unwrap_or(false)
2302 }
2303
2304 /// Validate that `kind` is a pack-registered entity kind.
2305 ///
2306 /// Returns `Ok(())` when no kinds are installed (bare runtime without packs).
2307 /// Returns `InvalidInput` when kinds are installed and `kind` is not among them.
2308 pub(crate) fn validate_entity_kind(&self, kind: &str) -> crate::RuntimeResult<()> {
2309 let guard = self.valid_entity_kinds.read().map_err(|_| {
2310 crate::RuntimeError::Internal("entity kind registry lock poisoned".into())
2311 })?;
2312 if guard.is_empty() {
2313 return Ok(());
2314 }
2315 if guard.iter().any(|k| k == kind) {
2316 Ok(())
2317 } else {
2318 Err(crate::RuntimeError::InvalidInput(format!(
2319 "unknown entity kind {kind:?}; valid: {}",
2320 guard.join(", ")
2321 )))
2322 }
2323 }
2324
2325 /// Validate that `kind` is a pack-registered note kind.
2326 ///
2327 /// Returns `Ok(())` when no kinds are installed (bare runtime without packs).
2328 /// Returns `InvalidInput` when kinds are installed and `kind` is not among them.
2329 pub(crate) fn validate_note_kind(&self, kind: &str) -> crate::RuntimeResult<()> {
2330 let guard = self.valid_note_kinds.read().map_err(|_| {
2331 crate::RuntimeError::Internal("note kind registry lock poisoned".into())
2332 })?;
2333 if !guard.iter().any(|entry| entry.registered) {
2334 return Ok(());
2335 }
2336 if guard
2337 .iter()
2338 .any(|entry| entry.registered && entry.name == kind)
2339 {
2340 Ok(())
2341 } else {
2342 let valid = guard
2343 .iter()
2344 .filter(|entry| entry.registered)
2345 .map(|entry| entry.name.as_str())
2346 .collect::<Vec<_>>()
2347 .join(", ");
2348 Err(crate::RuntimeError::InvalidInput(format!(
2349 "unknown note kind {kind:?}; valid: {}",
2350 valid
2351 )))
2352 }
2353 }
2354
2355 /// Install a pack-supplied entity-type validator.
2356 ///
2357 /// Called by the `KgPack` during registration so that `create_many` can validate
2358 /// `entity_type` values at the runtime layer, closing the hole where direct Rust
2359 /// callers bypass the handler-layer `validate_entity_type` check.
2360 ///
2361 /// The callback receives `(kind, entity_type)` and returns the normalised type
2362 /// string, or `RuntimeError::InvalidInput` if the type is not registered for that
2363 /// kind. Passing `entity_type = None` must return `Ok(None)`.
2364 pub fn install_entity_type_validator(&self, f: EntityTypeValidatorFn) {
2365 if let Ok(mut guard) = self.entity_type_validator.write() {
2366 *guard = Some(f);
2367 }
2368 }
2369
2370 /// Validate and normalise `entity_type` through the pack-installed validator.
2371 ///
2372 /// Returns `Ok(entity_type)` when no validator is installed (bare runtime).
2373 /// Returns `InvalidInput` when a validator is installed and rejects the type.
2374 pub(crate) fn validate_entity_type_for_kind(
2375 &self,
2376 kind: &str,
2377 entity_type: Option<&str>,
2378 ) -> crate::RuntimeResult<Option<String>> {
2379 let guard = self.entity_type_validator.read().map_err(|_| {
2380 crate::RuntimeError::Internal("entity type validator lock poisoned".into())
2381 })?;
2382 match guard.as_ref() {
2383 None => Ok(entity_type.map(str::to_string)),
2384 Some(validate) => validate(kind, entity_type),
2385 }
2386 }
2387
2388 /// Install a pack-owned note-mutation hook.
2389 ///
2390 /// Overwrites any previously-installed hook, same single-slot semantics
2391 /// as [`install_entity_type_validator`](Self::install_entity_type_validator).
2392 /// In practice only one pack (`khive-pack-memory`) installs one today;
2393 /// if a second pack ever needs this, the slot should be widened to a
2394 /// `Vec` at that point rather than silently overwritten.
2395 pub fn install_note_mutation_hook(&self, f: NoteMutationHookFn) {
2396 if let Ok(mut guard) = self.note_mutation_hook.write() {
2397 *guard = Some(f);
2398 }
2399 }
2400
2401 /// Clone read-side backend and embedder handles without retaining this
2402 /// runtime's pack callback slots. Providers stored in one of those slots
2403 /// must not hold a clone that points back to their own installation Arc,
2404 /// including indirectly through another hook's captured runtime.
2405 pub fn detached_for_note_search_ann_provider(&self) -> Self {
2406 let mut detached = self.clone();
2407 detached.note_search_ann_provider = Arc::new(RwLock::new(None));
2408 detached.note_mutation_hook = Arc::new(RwLock::new(None));
2409 detached.entity_type_validator = Arc::new(RwLock::new(None));
2410 detached.note_write_validator = Arc::new(RwLock::new(None));
2411 detached.entity_kind_hooks = Arc::new(RwLock::new(Vec::new()));
2412 detached.fusion_executors = Arc::new(RwLock::new(HashMap::new()));
2413 detached
2414 }
2415
2416 /// Borrow the shared host domain without extending its external lifetime.
2417 pub fn background_domain(&self) -> crate::resource_admission::BackgroundDomainHandle {
2418 self.background_domain.clone()
2419 }
2420
2421 /// Retain external ownership when composing another host backend.
2422 pub fn background_root(&self) -> RuntimeResult<crate::resource_admission::BackgroundRoot> {
2423 self.background_root.clone().ok_or_else(|| {
2424 RuntimeError::Internal("a background view cannot own a new host runtime".into())
2425 })
2426 }
2427
2428 /// Close or observe this host's domain, without retaining its external owner.
2429 pub fn background_resources_control(
2430 &self,
2431 ) -> crate::resource_admission::BackgroundDomainControl {
2432 self.background_domain.control()
2433 }
2434
2435 /// Detach callbacks and external ownership before capturing a runtime in a job.
2436 pub fn background_view(&self) -> Self {
2437 let mut view = self.detached_for_note_search_ann_provider();
2438 view.background_root = None;
2439 view
2440 }
2441
2442 /// Install the memory pack's note-search provider only on its own opened
2443 /// backend. A same-named but separate store keeps the exact search route.
2444 pub fn install_note_search_ann_provider(&self, provider: Arc<dyn NoteSearchAnnProvider>) {
2445 if !provider.serves_backend(self) {
2446 return;
2447 }
2448 if let Ok(mut guard) = self.note_search_ann_provider.write() {
2449 *guard = Some(provider);
2450 }
2451 }
2452
2453 pub(crate) fn note_search_ann_provider(
2454 &self,
2455 ) -> RuntimeResult<Option<Arc<dyn NoteSearchAnnProvider>>> {
2456 self.note_search_ann_provider
2457 .read()
2458 .map(|guard| {
2459 guard
2460 .as_ref()
2461 .filter(|provider| provider.serves_backend(self))
2462 .cloned()
2463 })
2464 .map_err(|_| RuntimeError::Internal("note-search ANN provider lock poisoned".into()))
2465 }
2466
2467 /// Install the pack-aggregated entity-kind update hooks (issue #2943).
2468 ///
2469 /// Called by the transport after the `VerbRegistry` is built, same
2470 /// timing as [`install_kind_registry`](Self::install_kind_registry) —
2471 /// pass `registry.entity_kind_hooks()`. Idempotent: a later call
2472 /// replaces the set.
2473 pub fn install_entity_kind_hooks(&self, hooks: EntityKindHooks) {
2474 if let Ok(mut guard) = self.entity_kind_hooks.write() {
2475 *guard = hooks;
2476 }
2477 }
2478
2479 /// The installed `KindHook` for entity `kind`, if its owning pack
2480 /// registered one via [`install_entity_kind_hooks`](Self::install_entity_kind_hooks).
2481 ///
2482 /// `None` before the transport installs the aggregate (bare runtime) or
2483 /// when no pack registered a hook for this entity kind — the caller
2484 /// treats this the same as a hook whose `validate_entity_update`
2485 /// inherited the trait's `Ok(())` default.
2486 pub(crate) fn entity_kind_hook(&self, kind: &str) -> Option<Arc<dyn KindHook>> {
2487 find_entity_owner_hook(&self.entity_kind_hooks, kind)
2488 }
2489
2490 pub(crate) fn canonical_entity_owner_lookup(&self) -> EntityOwnerHookLookup {
2491 EntityOwnerHookLookup {
2492 hooks: Arc::clone(&self.entity_kind_hooks),
2493 }
2494 }
2495
2496 /// Install a pack-owned note-write validator.
2497 ///
2498 /// Called during pack registration (`PackRuntime::register_note_write_validator`)
2499 /// so that the covered note-write sites carrying caller-supplied
2500 /// `properties` derive the owning pack's identity properties from the
2501 /// authorization token, closing the gap where a direct Rust caller, the
2502 /// generic `create` verb, or the proposal-apply path (which dispatches no
2503 /// pack hooks) writes them unchecked. Single-slot semantics, same as
2504 /// [`install_note_mutation_hook`](Self::install_note_mutation_hook): a
2505 /// second installing pack overwrites the first, so a validator must return
2506 /// kinds it does not own unchanged.
2507 ///
2508 /// Covered sites — each calls `derive_note_write_properties`
2509 /// before the write: `create_note_inner` (`operations.rs`, the generic
2510 /// `create` verb funnel and every other public `create_note*` variant),
2511 /// `atomic_prepare::prepare_add_note` (the proposal-apply add-note path),
2512 /// and `atomic_message::create_notes_atomic_with_report` (the atomic
2513 /// multi-note writer).
2514 ///
2515 /// NOT covered by this validator: `try_create_note` (`operations.rs`).
2516 /// `try_create_note` is deliberately excluded — its only caller path is
2517 /// `comm.ingest`, where `properties.from_actor` is the external transport
2518 /// sender named by the `from` parameter, not the authenticated caller,
2519 /// and where transport-owned quarantine/channel properties are
2520 /// legitimately established. Running the generic validator there would
2521 /// stamp every inbound message as the ingesting daemon and reject the
2522 /// evidence the trusted ingest handler just derived. `try_create_note`
2523 /// instead runs its own narrower reserved-transport-property check
2524 /// inline (`operations.rs`'s `try_create_note_impl`), which allows the
2525 /// `message`-kind transport properties only when called through
2526 /// [`Self::try_create_note_as_trusted_ingest`] with a
2527 /// [`crate::pack::ChannelIngestCapability`].
2528 ///
2529 /// The `NoteStore` returned by [`notes`](Self::notes) is covered by a
2530 /// different, narrower mechanism: it is wrapped in
2531 /// `note_store_guard::PolicyEnforcingNoteStore`, which refuses
2532 /// `upsert_note` / `upsert_notes` / `try_insert_note` /
2533 /// `replace_note_if_unchanged` calls that would write a `kind = "message"`
2534 /// note carrying `quarantined` / `channel_kind` / `channel_slug`, and
2535 /// refuses `set_note_property` / `try_patch_note_property` /
2536 /// `patch_note_property_atomic` / `update_note_properties` calls that
2537 /// would patch any of those keys onto any note — unconditionally, since
2538 /// that public accessor has no way to see a trust decision. `try_create_note_impl` itself reaches storage through
2539 /// `Self::raw_notes`, the unwrapped accessor, so its own inline check
2540 /// (which can legitimately allow those properties for trusted ingest)
2541 /// is not double-enforced or contradicted by the wrapper.
2542 /// Register a pack-defined custom fusion strategy under `name` (ADR-012).
2543 ///
2544 /// Unlike `install_entity_type_validator`/`install_note_mutation_hook`,
2545 /// this slot is keyed rather than single-occupancy: multiple packs each
2546 /// register their own named strategy, and a second registration under an
2547 /// already-used `name` replaces the first. Looked up by
2548 /// `FusionStrategy::Custom { name, .. }` at the hybrid-search dispatch
2549 /// boundary in `crate::fusion`; an unregistered name fails closed with
2550 /// `RuntimeError::UnknownFusionStrategy` rather than silently falling
2551 /// back to RRF.
2552 pub fn register_fusion_strategy(
2553 &self,
2554 name: impl Into<String>,
2555 executor: Arc<dyn crate::fusion::FusionExecutor>,
2556 ) {
2557 if let Ok(mut guard) = self.fusion_executors.write() {
2558 guard.insert(name.into(), executor);
2559 }
2560 }
2561
2562 /// Resolve a registered custom fusion executor by name.
2563 ///
2564 /// Returns `RuntimeError::UnknownFusionStrategy` when no pack has
2565 /// registered `name` — callers must invoke this before any
2566 /// empty-input/zero-limit short circuit so a misconfigured name errors
2567 /// on every call, including zero-result ones.
2568 pub(crate) fn fusion_executor(
2569 &self,
2570 name: &str,
2571 ) -> RuntimeResult<Arc<dyn crate::fusion::FusionExecutor>> {
2572 let guard = self
2573 .fusion_executors
2574 .read()
2575 .map_err(|_| RuntimeError::Internal("fusion executor registry lock poisoned".into()))?;
2576 guard
2577 .get(name)
2578 .cloned()
2579 .ok_or_else(|| RuntimeError::UnknownFusionStrategy(name.to_string()))
2580 }
2581
2582 pub fn install_note_write_validator(&self, f: NoteWriteValidatorFn) {
2583 if let Ok(mut guard) = self.note_write_validator.write() {
2584 *guard = Some(f);
2585 }
2586 }
2587
2588 /// Whether a note-write validator is installed on this runtime.
2589 ///
2590 /// Exists so a transport's own tests can assert, per boot path, that the
2591 /// documented startup sequence actually filled the slot. A missing install
2592 /// fails open and silently — an empty slot passes caller-supplied
2593 /// properties straight through, which no write site can distinguish from a
2594 /// validator that approved them — so occupancy is asserted, never assumed.
2595 pub fn has_note_write_validator(&self) -> bool {
2596 self.note_write_validator
2597 .read()
2598 .map(|g| g.is_some())
2599 .unwrap_or(false)
2600 }
2601
2602 /// Run caller-supplied note `properties` through the installed note-write
2603 /// validator, returning the properties to store.
2604 ///
2605 /// Returns them unchanged when no validator is installed (bare runtime).
2606 pub(crate) fn derive_note_write_properties(
2607 &self,
2608 kind: &str,
2609 token: &NamespaceToken,
2610 properties: Option<serde_json::Value>,
2611 ) -> RuntimeResult<Option<serde_json::Value>> {
2612 let validator = self
2613 .note_write_validator
2614 .read()
2615 .map_err(|_| RuntimeError::Internal("note write validator lock poisoned".into()))?
2616 .clone();
2617 match validator {
2618 None => Ok(properties),
2619 Some(validate) => validate(kind, &token.actor().id, properties),
2620 }
2621 }
2622
2623 /// Invoke the pack-installed note-mutation hook, if any.
2624 ///
2625 /// `kind` is the note's `kind` string (e.g. `"memory"`); `id` is the
2626 /// note's UUID. No-op when no hook is installed (bare runtime, or no
2627 /// pack cares). Errors inside the hook are the hook's own concern to
2628 /// handle/log — this call site cannot propagate a failure without
2629 /// changing `update_note`/`delete_note`'s already-committed success
2630 /// return value.
2631 pub(crate) async fn fire_note_mutation_hook(&self, kind: &str, id: uuid::Uuid) {
2632 let hook = self
2633 .note_mutation_hook
2634 .read()
2635 .ok()
2636 .and_then(|guard| guard.clone());
2637 if let Some(hook) = hook {
2638 hook(kind.to_string(), id).await;
2639 }
2640 }
2641
2642 /// Snapshot of currently-installed pack edge rules.
2643 ///
2644 /// This is the same composed rule set `validate_edge_relation_endpoints`
2645 /// consults via `pack_rule_allows` when accepting/rejecting an edge. Public
2646 /// so pack-layer error-hint code (e.g. `khive-pack-kg`'s
2647 /// `valid_relations_for_entity_pair`) can derive hints from the exact
2648 /// source the validator uses, rather than maintaining a separate
2649 /// hand-authored table that can drift out of sync.
2650 pub fn pack_edge_rules(&self) -> Vec<EdgeEndpointRule> {
2651 self.edge_rules
2652 .read()
2653 .map(|g| g.clone())
2654 .unwrap_or_default()
2655 }
2656
2657 /// Borrow the installed pack edge rules for a synchronous calculation.
2658 pub(crate) fn with_pack_edge_rules<T>(&self, f: impl FnOnce(&[EdgeEndpointRule]) -> T) -> T {
2659 match self.edge_rules.read() {
2660 Ok(rules) => f(&rules),
2661 Err(_) => f(&[]),
2662 }
2663 }
2664
2665 /// List registered embedding models via `SqlAccess`, routing through the
2666 /// existing connection pool rather than opening a fresh `Connection` per call.
2667 ///
2668 /// Optionally filter by `engine_name`. Returns an empty vec when the
2669 /// `_embedding_models` table does not yet exist (e.g. no migrations have run
2670 /// or no models have been registered). All other SQL errors are propagated.
2671 /// Malformed required fields, unknown statuses and invalid non-NULL optional
2672 /// dimensions/UUIDs are warned and skipped. The legacy optional timestamps
2673 /// retain their non-integer-to-None compatibility behavior.
2674 pub async fn list_embedding_models(
2675 &self,
2676 engine_filter: Option<&str>,
2677 ) -> RuntimeResult<Vec<crate::EmbeddingModelRecord>> {
2678 use khive_storage::{SqlStatement, SqlValue};
2679
2680 let (sql_text, params) = if let Some(engine) = engine_filter {
2681 (
2682 "SELECT id, engine_name, model_id, key_version, dim, output_dim, status, \
2683 activated_at, superseded_at, superseded_by, canonical_key, created_at \
2684 FROM _embedding_models WHERE engine_name = ?1 \
2685 ORDER BY engine_name, activated_at IS NULL, activated_at"
2686 .to_string(),
2687 vec![SqlValue::Text(engine.to_string())],
2688 )
2689 } else {
2690 (
2691 "SELECT id, engine_name, model_id, key_version, dim, output_dim, status, \
2692 activated_at, superseded_at, superseded_by, canonical_key, created_at \
2693 FROM _embedding_models \
2694 ORDER BY engine_name, activated_at IS NULL, activated_at"
2695 .to_string(),
2696 vec![],
2697 )
2698 };
2699
2700 let stmt = SqlStatement {
2701 sql: sql_text,
2702 params,
2703 label: Some("list_embedding_models".into()),
2704 };
2705
2706 let mut reader = self
2707 .sql()
2708 .reader()
2709 .await
2710 .map_err(crate::RuntimeError::Storage)?;
2711
2712 let rows = match reader.query_all(stmt).await {
2713 Ok(rows) => rows,
2714 Err(e) if e.to_string().contains("no such table: _embedding_models") => {
2715 return Ok(Vec::new())
2716 }
2717 Err(e) => return Err(crate::RuntimeError::Storage(e)),
2718 };
2719
2720 Ok(rows
2721 .iter()
2722 .filter_map(crate::EmbeddingModelRecord::from_sql_row)
2723 .collect())
2724 }
2725}
2726
2727fn vector_dimensions_from_ddl(ddl: &str) -> Option<usize> {
2728 let lower = ddl.to_ascii_lowercase();
2729 let suffix = lower.split_once("embedding float[")?.1;
2730 let dimension = suffix.split_once(']')?.0;
2731 if dimension.is_empty() || !dimension.bytes().all(|byte| byte.is_ascii_digit()) {
2732 return None;
2733 }
2734 dimension.parse().ok()
2735}
2736
2737// IN-CRATE TEST JUSTIFICATION: tests here cover KhiveRuntime construction helpers
2738// (in-memory backend wiring, NamespaceToken::for_namespace) that are
2739// pub(crate)-only and cannot be called from the integration test crate.
2740#[cfg(test)]
2741#[path = "runtime_tests.rs"]
2742mod tests;
2743
2744#[cfg(test)]
2745mod background_resource_tests {
2746 use super::*;
2747
2748 fn config() -> RuntimeConfig {
2749 RuntimeConfig {
2750 db_path: None,
2751 warm_concurrency: 3,
2752 maintenance_concurrency: 2,
2753 ..RuntimeConfig::no_embeddings()
2754 }
2755 }
2756
2757 #[test]
2758 fn clones_core_and_detached_views_share_the_original_domain() {
2759 let runtime = KhiveRuntime::new(config()).expect("root runtime");
2760 let root = runtime.background_root().expect("external root");
2761 for view in [
2762 runtime.clone(),
2763 runtime.core(),
2764 runtime.detached_for_note_search_ann_provider(),
2765 ] {
2766 assert!(root.same_domain(&view.background_root().expect("retained external root")));
2767 }
2768 let other = KhiveRuntime::new(config()).expect("independent root");
2769 assert!(!root.same_domain(&other.background_root().unwrap()));
2770 }
2771
2772 #[test]
2773 fn assigned_backend_and_rebound_core_keep_the_main_domain() {
2774 let main = KhiveRuntime::new(config()).expect("main runtime");
2775 let root = main.background_root().unwrap();
2776 let secondary_config = RuntimeConfig {
2777 backend_id: BackendId::parse("secondary").expect("secondary backend id"),
2778 ..config()
2779 };
2780 let secondary = KhiveRuntime::try_from_backend_in_domain(
2781 Arc::new(StorageBackend::memory().unwrap()),
2782 secondary_config,
2783 root.clone(),
2784 )
2785 .unwrap()
2786 .with_core_backend(Arc::clone(&main.backend));
2787 assert!(root.same_domain(&secondary.background_root().unwrap()));
2788 assert!(root.same_domain(&secondary.core().background_root().unwrap()));
2789 }
2790
2791 #[test]
2792 fn background_view_cannot_keep_the_external_root_alive() {
2793 let runtime = KhiveRuntime::new(config()).expect("root runtime");
2794 let view = runtime.background_view();
2795 let control = view.background_resources_control();
2796 assert!(view.background_root().is_err());
2797 assert_eq!(view.background_domain().limits().warm(), 3);
2798 assert!(!control.is_closed());
2799 drop(runtime);
2800 assert!(
2801 control.is_closed(),
2802 "last external root closes despite the job view"
2803 );
2804 }
2805
2806 #[test]
2807 fn invalid_limits_and_injected_limit_mismatch_are_refused() {
2808 for (warm, maintenance) in [(0, 2), (3, 0), (usize::MAX, 2), (3, usize::MAX)] {
2809 let mut bad = config();
2810 bad.warm_concurrency = warm;
2811 bad.maintenance_concurrency = maintenance;
2812 assert!(matches!(
2813 KhiveRuntime::new(bad),
2814 Err(RuntimeError::InvalidInput(_))
2815 ));
2816 }
2817 let main = KhiveRuntime::new(config()).unwrap();
2818 let mut different = config();
2819 different.warm_concurrency += 1;
2820 assert!(matches!(
2821 KhiveRuntime::try_from_backend_in_domain(
2822 Arc::new(StorageBackend::memory().unwrap()),
2823 different,
2824 main.background_root().unwrap(),
2825 ),
2826 Err(RuntimeError::InvalidInput(_))
2827 ));
2828 }
2829}