Skip to main content

KhiveRuntime

Struct KhiveRuntime 

Source
pub struct KhiveRuntime { /* private fields */ }
Expand description

Composable runtime handle used by the MCP server.

Wraps a StorageBackend and provides namespace-scoped accessor methods for each storage capability, plus a lazily-loaded embedder.

Implementations§

Source§

impl KhiveRuntime

Source

pub async fn update_entity( &self, namespace: Option<&str>, id: Uuid, patch: EntityPatch, ) -> RuntimeResult<Entity>

Patch-style entity update.

Only fields set to Some(_) are changed. Re-indexes FTS5 (and vectors if configured) when name or description changes; skips re-indexing for property/tag-only patches.

Returns RuntimeError::NotFound if the entity does not exist or belongs to a different namespace. This enforces ADR-007 namespace isolation at the runtime layer.

Source

pub async fn merge_entity( &self, namespace: Option<&str>, into_id: Uuid, from_id: Uuid, strategy: MergeStrategy, ) -> RuntimeResult<MergeSummary>

Merge from_id into into_id.

All edges incident to from_id are rewired to into_id. Self-loops that would result from the rewire are dropped. Properties and tags are merged per strategy. from_id is hard-deleted and removed from indexes. Returns a summary.

Atomic: all SQL (entity reads/writes, edge rewires, FTS updates, vec-index delete) runs on a single pool connection inside one BEGIN IMMEDIATE transaction via merge_entity_sql. If embedding vectors are configured, the vector re-insert for into_id is performed after the transaction (requires async embedding computation).

Source§

impl KhiveRuntime

Source

pub async fn hybrid_search_with_strategy( &self, namespace: Option<&str>, query_text: &str, query_vector: Option<Vec<f32>>, strategy: FusionStrategy, limit: u32, ) -> RuntimeResult<Vec<SearchHit>>

Hybrid search with a caller-supplied fusion strategy.

Source§

impl KhiveRuntime

Source

pub async fn bfs_traverse( &self, namespace: Option<&str>, start: Uuid, options: TraversalOptions, ) -> RuntimeResult<Vec<PathNode>>

BFS traversal from start, returning nodes in level order.

The first element is always the start node (via_edge = None, depth = 0). Nodes already visited are skipped so the result set is deduplicated.

Source

pub async fn shortest_path( &self, namespace: Option<&str>, from: Uuid, to: Uuid, max_depth: usize, ) -> RuntimeResult<Option<Vec<PathNode>>>

Bidirectional BFS shortest path from from to to.

Returns Some(path) where path[0] is from and path.last() is to, or None if no path exists within max_depth hops. For from == to returns Some with a single-node path immediately.

Source§

impl KhiveRuntime

Source

pub async fn create_entity( &self, namespace: Option<&str>, kind: &str, name: &str, description: Option<&str>, properties: Option<Value>, tags: Vec<String>, ) -> RuntimeResult<Entity>

Create and persist a new entity.

Source

pub async fn get_entity( &self, namespace: Option<&str>, id: Uuid, ) -> RuntimeResult<Option<Entity>>

Retrieve an entity by ID.

Returns None if the entity does not exist or belongs to a different namespace. This enforces ADR-007 namespace isolation at the runtime layer.

Source

pub async fn list_entities( &self, namespace: Option<&str>, kind: Option<&str>, limit: u32, offset: u32, ) -> RuntimeResult<Vec<Entity>>

List entities in a namespace, optionally filtered by kind.

Source

pub async fn list_events( &self, namespace: Option<&str>, filter: EventFilter, limit: u32, offset: u32, ) -> RuntimeResult<Page<Event>>

List events in a namespace, optionally filtered.

Create a directed edge between two substrates.

Enforces the ADR-002/ADR-019/ADR-024 three-case relation contract via validate_edge_relation_endpoints. See that method for the full contract.

A record that exists but belongs to a different namespace is treated as not found (fail-closed; no cross-namespace existence leak).

Source

pub async fn neighbors( &self, namespace: Option<&str>, node_id: Uuid, direction: Direction, limit: Option<u32>, relations: Option<Vec<EdgeRelation>>, ) -> RuntimeResult<Vec<NeighborHit>>

Get immediate neighbors of a node, optionally filtered by relation type.

Pass relations: Some(vec![EdgeRelation::Annotates]) to retrieve only annotation edges, enabling cross-substrate navigation as described in ADR-024.

Source

pub async fn neighbors_with_query( &self, namespace: Option<&str>, node_id: Uuid, query: NeighborQuery, ) -> RuntimeResult<Vec<NeighborHit>>

Get neighbors with full query control (includes min_weight).

Source

pub async fn traverse( &self, namespace: Option<&str>, request: TraversalRequest, ) -> RuntimeResult<Vec<GraphPath>>

Traverse the graph from a set of root nodes.

Source

pub async fn create_note( &self, namespace: Option<&str>, kind: &str, name: Option<&str>, content: &str, salience: f64, properties: Option<Value>, annotates: Vec<Uuid>, ) -> RuntimeResult<Note>

Create and persist a note, optionally with properties and annotation targets.

After creating the note:

  • Always indexes into FTS5 at the notes_<namespace> key.
  • If an embedding model is configured, indexes into the vector store with SubstrateKind::Note.
  • For each UUID in annotates, creates an EdgeRelation::Annotates edge from the note to that target.
Source

pub async fn create_note_with_decay( &self, namespace: Option<&str>, kind: &str, name: Option<&str>, content: &str, salience: f64, decay_factor: f64, properties: Option<Value>, annotates: Vec<Uuid>, ) -> RuntimeResult<Note>

Like [create_note] but also sets a non-zero decay factor on the note.

Source

pub async fn list_notes( &self, namespace: Option<&str>, kind: Option<&str>, limit: u32, offset: u32, ) -> RuntimeResult<Vec<Note>>

List notes, optionally filtered by kind.

Source

pub async fn search_notes( &self, namespace: Option<&str>, query_text: &str, query_vector: Option<Vec<f32>>, limit: u32, note_kind: Option<&str>, ) -> RuntimeResult<Vec<NoteSearchHit>>

Search notes using a hybrid FTS5 + vector pipeline with salience weighting.

Pipeline (per ADR-024):

  1. FTS5 query against notes_<namespace>.
  2. If embedding model is configured: vector search filtered to kind="note".
  3. RRF fusion (k=60).
  4. Salience-weighted rerank: score *= (0.5 + 0.5 * note.salience).
  5. Filter soft-deleted notes (deleted_at IS NOT NULL).
  6. Truncate to limit.
Source

pub async fn resolve_prefix( &self, namespace: Option<&str>, prefix: &str, ) -> RuntimeResult<Option<Uuid>>

Resolve a short UUID prefix (8+ hex chars) to a full UUID.

Searches entities, notes, and edges tables for a UUID starting with the given prefix, scoped to the caller’s namespace. Returns Ok(Some(uuid)) if exactly one match is found, Ok(None) if no matches, or an error if ambiguous (multiple matches).

Source

pub async fn resolve( &self, namespace: Option<&str>, id: Uuid, ) -> RuntimeResult<Option<Resolved>>

Resolve a UUID to its substrate kind by trying entity, then note, then event stores.

Returns None if the UUID is not found in any substrate. Cost: at most 3 store lookups per call (cheap for v0.1).

Source

pub async fn delete_note( &self, namespace: Option<&str>, id: Uuid, hard: bool, ) -> RuntimeResult<bool>

Delete a note by ID, enforcing namespace isolation.

On hard delete, cascades to remove all incident edges (both inbound and outbound) and cleans up FTS and vector indexes, preventing dangling references for annotates edges that target this note (ADR-002, ADR-024). Soft delete also cleans FTS and vector indexes; edges are left in place.

Returns false without deleting if the note does not exist or belongs to a different namespace (ADR-007 namespace isolation).

Source§

impl KhiveRuntime

Source

pub async fn query( &self, namespace: Option<&str>, query: &str, ) -> RuntimeResult<Vec<SqlRow>>

Execute a GQL or SPARQL query string, returning raw SQL rows.

The query is compiled to SQL with the namespace scope applied. GQL syntax: MATCH (a:concept)-[e:extends]->(b) RETURN a, b LIMIT 10 SPARQL syntax: SELECT ?a WHERE { ?a :kind "concept" . }

Source

pub async fn query_with_metadata( &self, namespace: Option<&str>, query: &str, ) -> RuntimeResult<QueryResult>

Execute a GQL/SPARQL query, returning rows and any validation warnings.

Source

pub async fn delete_entity( &self, namespace: Option<&str>, id: Uuid, hard: bool, ) -> RuntimeResult<bool>

Delete an entity by ID (soft delete by default).

On hard delete, cascades to remove all incident edges (both inbound and outbound) to prevent dangling references. Soft delete also cleans FTS and vector indexes; edges are left in place.

Returns false without deleting if the entity exists but belongs to a different namespace (ADR-007 namespace isolation).

Source

pub async fn count_entities( &self, namespace: Option<&str>, kind: Option<&str>, ) -> RuntimeResult<u64>

Count entities in a namespace, optionally filtered.

Source

pub async fn get_edge( &self, namespace: Option<&str>, edge_id: Uuid, ) -> RuntimeResult<Option<Edge>>

Fetch a single edge by id. Returns None if the edge does not exist.

Source

pub async fn list_edges( &self, namespace: Option<&str>, filter: EdgeListFilter, limit: u32, ) -> RuntimeResult<Vec<Edge>>

List edges matching filter. limit is capped at 1000; defaults to 100.

Source

pub async fn update_edge( &self, namespace: Option<&str>, edge_id: Uuid, relation: Option<EdgeRelation>, weight: Option<f64>, ) -> RuntimeResult<Edge>

Patch-style edge update. Only Some(_) fields are applied.

When relation is Some(new_rel), validates that the edge’s existing endpoints are legal for new_rel before persisting. Weight-only updates (relation = None) skip validation. Returns InvalidInput if the new relation would violate the ADR-002/ADR-019/ADR-024 three-case contract; the edge is NOT mutated on error.

Source

pub async fn delete_edge( &self, namespace: Option<&str>, edge_id: Uuid, ) -> RuntimeResult<bool>

Hard-delete an edge by id.

Cascades to remove any annotates edges whose target is the deleted edge (ADR-002: annotates is note → anything; deleting an edge target leaves annotation edges dangling if not cleaned up). Returns true if the primary edge was removed.

If edge_id does not refer to an edge (e.g. the caller passes an entity or note UUID by mistake), this method returns Ok(false) immediately with no side effects — it does not cascade inbound edges of the non-edge record.

Source

pub async fn count_edges( &self, namespace: Option<&str>, filter: EdgeListFilter, ) -> RuntimeResult<u64>

Count edges matching filter.

Source§

impl KhiveRuntime

Source

pub async fn export_kg( &self, namespace: Option<&str>, ) -> RuntimeResult<KgArchive>

Export all entities and edges in a namespace to a portable JSON archive.

Edge collection: all entity IDs in the namespace are gathered first; query_edges is then called with those IDs as source_ids. This captures every edge whose source entity belongs to the namespace.

Source

pub async fn export_kg_json( &self, namespace: Option<&str>, ) -> RuntimeResult<String>

Export to a JSON string (convenience wrapper around export_kg).

Source

pub async fn import_kg( &self, archive: &KgArchive, target_namespace: Option<&str>, ) -> RuntimeResult<ImportSummary>

Import an archive into target_namespace.

If target_namespace is None, the archive’s own namespace is used.

  • Entities: upserted by ID; existing records are overwritten.
  • Edges: upserted; existing records are overwritten.
  • Validation: format != "khive-kg" or unsupported version → InvalidInput. Invalid edge relations are caught at JSON deserialization time.
Source

pub async fn import_kg_json( &self, json: &str, target_namespace: Option<&str>, ) -> RuntimeResult<ImportSummary>

Import from a JSON string (convenience wrapper around import_kg).

Source§

impl KhiveRuntime

Source

pub async fn embed(&self, text: &str) -> RuntimeResult<Vec<f32>>

Generate an embedding vector for text using the configured local model.

First call lazily loads model weights (cold start cost). Subsequent calls reuse them. Returns Unconfigured("embedding_model") if no model is configured.

Source

pub async fn embed_batch( &self, texts: &[String], ) -> RuntimeResult<Vec<Vec<f32>>>

Generate embeddings for multiple texts in one call.

Delegates to the cached EmbeddingService::embed, so repeated texts within and across calls benefit from the runtime-level LRU cache.

Returns an empty vec for empty input without hitting the embedding service. Returns Unconfigured("embedding_model") if no model is configured.

Search vectors using either a caller-provided embedding or query text.

Existing callers pass query_embedding: Some(vec) to avoid re-embedding. Text callers pass query_embedding: None, query_text: Some(...) and the runtime embeds internally.

Hybrid search: text (FTS5) + vector retrieval fused via Reciprocal Rank Fusion.

  • Always performs text search over query_text.
  • If query_vector is Some, also performs vector search and fuses both lists.
  • If None, returns text-only results — no vector store needed.
  • If entity_kind is Some, the alive-set query filters to that kind. The text/vector candidate pools are unfiltered up front; the kind filter applies at the alive-check stage where we already fetch each candidate to confirm it isn’t soft-deleted.

limit caps the final returned list; internally pulls limit * 4 candidates per path. The fused candidate set is kept untruncated until after the alive + kind filter so that right-kind hits ranked below limit in the raw fusion still surface when higher-ranked candidates are wrong-kind or soft-deleted.

Source

pub async fn knn( &self, namespace: Option<&str>, query_vector: Vec<f32>, top_k: u32, ) -> RuntimeResult<Vec<VectorSearchHit>>

Exact KNN over the full namespace’s vector store.

sqlite-vec uses brute-force cosine — results are exact, not approximate. Cost is O(N · D) per query. For small-to-medium namespaces (~hundreds of thousands of vectors) this is well within latency budgets.

Source

pub async fn rerank( &self, namespace: Option<&str>, query_vector: &[f32], candidate_ids: &[Uuid], top_k: u32, ) -> RuntimeResult<Vec<VectorSearchHit>>

Exact KNN restricted to a candidate set.

Useful for reranking the top-N results from hybrid_search (or any other retrieval path) with exact cosine similarity against a query vector. Returns hits sorted by similarity (highest first), truncated to top_k.

Source§

impl KhiveRuntime

Source

pub fn new(config: RuntimeConfig) -> RuntimeResult<Self>

Create a new runtime with the given config.

Source

pub fn memory() -> RuntimeResult<Self>

Create an in-memory runtime (for tests and ephemeral use).

Source

pub fn config(&self) -> &RuntimeConfig

Return a reference to the runtime config.

Source

pub fn backend(&self) -> &StorageBackend

Return a reference to the underlying storage backend.

Source

pub fn ns<'a>(&'a self, namespace: Option<&'a str>) -> &'a str

Resolve namespace: use provided value or fall back to default_namespace.

Source

pub fn entities( &self, namespace: Option<&str>, ) -> RuntimeResult<Arc<dyn EntityStore>>

Get an EntityStore scoped to the given namespace (or default).

Source

pub fn graph( &self, namespace: Option<&str>, ) -> RuntimeResult<Arc<dyn GraphStore>>

Get a GraphStore scoped to the given namespace (or default).

Source

pub fn notes( &self, namespace: Option<&str>, ) -> RuntimeResult<Arc<dyn NoteStore>>

Get a NoteStore scoped to the given namespace (or default).

Source

pub fn events( &self, namespace: Option<&str>, ) -> RuntimeResult<Arc<dyn EventStore>>

Get an EventStore scoped to the given namespace (or default).

Source

pub fn sql(&self) -> Arc<dyn SqlAccess>

Get the raw SQL access capability (for ad-hoc queries).

Source

pub fn vectors( &self, namespace: Option<&str>, ) -> RuntimeResult<Arc<dyn VectorStore>>

Get a VectorStore for the configured embedding model, scoped to the namespace.

Returns Unconfigured("embedding_model") if no model is set.

Source

pub fn text( &self, namespace: Option<&str>, ) -> RuntimeResult<Arc<dyn TextSearch>>

Get a TextSearch index for the namespace’s entity corpus.

Source

pub fn text_for_notes( &self, namespace: Option<&str>, ) -> RuntimeResult<Arc<dyn TextSearch>>

Get a TextSearch index for the namespace’s notes corpus.

Source

pub fn install_edge_rules(&self, rules: Vec<EdgeEndpointRule>)

Install the pack-aggregated edge endpoint rules (ADR-031).

Called by the transport layer after the VerbRegistry is built so that runtime-layer edge validation (in validate_edge_relation_endpoints) can consult pack rules in addition to the ADR-002 base contract. Idempotent: later calls overwrite the previous rule set.

Source

pub async fn embedder(&self) -> RuntimeResult<Arc<dyn EmbeddingService>>

Get the lazily-initialized embedding service.

Returns a CachedEmbeddingService wrapping a NativeEmbeddingService. First call loads the model (cold start cost); subsequent calls are cheap and benefit from LRU caching of repeated inputs.

Returns Unconfigured("embedding_model") if no model is set.

Trait Implementations§

Source§

impl Clone for KhiveRuntime

Source§

fn clone(&self) -> KhiveRuntime

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more