Skip to main content

RetrievalEngine

Struct RetrievalEngine 

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

Multi-modal retrieval engine with production vector search

§Lock Ordering (SHO-72)

To prevent deadlocks, locks MUST be acquired in this order:

  1. vector_index - Vector similarity search index
  2. id_mapping - Memory ID ↔ Vector ID mapping
  3. consolidation_events - Introspection event buffer

Rules:

  • Never acquire a higher-numbered lock while holding a lower-numbered lock
  • For read operations, prefer read() over write() when possible
  • Release locks as soon as possible (don’t hold during I/O)

Note: Memory graph (Hebbian learning) has been consolidated into GraphMemory which is managed at the API layer (MultiUserMemoryManager.graph_memories)

Implementations§

Source§

impl RetrievalEngine

Source

pub fn new( storage: Arc<MemoryStorage>, embedder: Arc<MiniLMEmbedder>, ) -> Result<Self>

Create new retrieval engine with shared embedder (CRITICAL: embedder loaded only once)

ATOMIC ARCHITECTURE: RocksDB is the ONLY source of truth.

  • Vector mappings are stored atomically with memories in RocksDB
  • Vamana index is rebuilt from RocksDB on startup (pure in-memory cache)
  • No more file-based IdMapping = no more orphaned memories
Source

pub fn with_event_buffer( storage: Arc<MemoryStorage>, embedder: Arc<MiniLMEmbedder>, consolidation_events: Option<Arc<RwLock<ConsolidationEventBuffer>>>, ) -> Result<Self>

Create retrieval engine with event buffer for consolidation introspection

The event buffer is used to record Hebbian learning events:

  • Edge formation (new associations)
  • Edge strengthening (co-activation)
  • Edge potentiation (LTP)
  • Edge pruning (decay below threshold)

ATOMIC STARTUP: Rebuilds Vamana from RocksDB mappings for crash safety.

Source

pub fn set_consolidation_events( &mut self, events: Arc<RwLock<ConsolidationEventBuffer>>, )

Set the consolidation event buffer (for late binding after construction)

Source

pub fn save(&self) -> Result<()>

Save Vamana index to disk for instant startup

HYBRID ARCHITECTURE:

  • RocksDB: Source of truth for memories and ID mappings
  • .vamana file: Persisted graph for instant startup (skip rebuild)

On next startup, if .vamana exists and is valid, we load it directly. Otherwise, we fall back to rebuilding from RocksDB.

Source

pub fn len(&self) -> usize

Get number of vectors in the index

Source

pub fn is_empty(&self) -> bool

Check if index is empty

Source

pub fn get_indexed_memory_ids(&self) -> HashSet<MemoryId>

Get set of all indexed memory IDs (for integrity checking)

Source

pub fn index_memory(&self, memory: &Memory) -> Result<()>

Add memory to vector index with atomic RocksDB storage

ATOMIC ARCHITECTURE: This method stores the vector mapping atomically in RocksDB alongside the memory data, ensuring no orphaned memories.

For long content, this chunks the text and creates multiple embeddings to ensure ALL content is searchable, not just the first 256 tokens.

Source

pub fn reindex_memory(&self, memory: &Memory) -> Result<()>

Re-index an existing memory with updated embeddings

Used when memory content is updated via upsert() to ensure the vector index reflects the new content.

Strategy: Remove old vector and add new one (Vamana doesn’t support update-in-place)

Source

pub fn remove_memory(&self, memory_id: &MemoryId) -> bool

Remove a memory from the vector index

ATOMIC ARCHITECTURE: Removes the vector mapping from RocksDB atomically. The in-memory Vamana index is updated immediately, and the RocksDB mapping is deleted to ensure consistency on restart.

Returns true if the memory was found and removed, false if not indexed.

Source

pub fn search_ids( &self, query: &Query, limit: usize, ) -> Result<Vec<(MemoryId, f32)>>

Search for memory IDs only (for cache-aware retrieval)

With chunked embeddings, multiple vectors can map to the same memory. This function deduplicates by MemoryId, keeping the highest-scoring chunk.

Returns (MemoryId, similarity_score) pairs

Source

pub fn get_from_storage(&self, id: &MemoryId) -> Result<Memory>

Get memory from storage by ID

Source

pub fn search_by_embedding( &self, embedding: &[f32], limit: usize, exclude_id: Option<&MemoryId>, ) -> Result<Vec<(MemoryId, f32)>>

Search for similar memories by embedding directly (SHO-106)

Used for interference detection to find memories similar to a new memory. Optionally excludes a specific memory ID from results.

With chunked embeddings, multiple vectors can map to the same memory. This function deduplicates by MemoryId, keeping the highest-scoring chunk.

Returns (MemoryId, similarity_score) pairs

Source

pub fn search(&self, query: &Query, limit: usize) -> Result<Vec<SharedMemory>>

Search for memories using multiple retrieval modes (zero-copy with Arc)

Source

pub fn matches_filters(&self, memory: &Memory, query: &Query) -> bool

Check if memory matches query filters

Delegates to Query::matches() which is the SINGLE source of truth for all filter logic. This ensures consistent filtering across all memory tiers and retrieval modes.

Source

pub fn rebuild_index(&self) -> Result<()>

Build vector index from existing memories (resumable on failure)

Uses incremental indexing so partial progress is preserved:

  • Skips memories already in the index
  • On failure, next rebuild/repair continues from where it left off
  • Logs progress every 1000 memories for monitoring
Source

pub fn add_to_graph(&self, _memory: &Memory)

👎Deprecated:

Use GraphMemory at API layer instead

Add memory to knowledge graph - DEPRECATED Use GraphMemory at the API layer instead

Source

pub fn record_coactivation(&self, _memory_ids: &[MemoryId])

👎Deprecated:

Use GraphMemory.record_memory_coactivation() at API layer instead

Record co-activation of memories - DEPRECATED Use GraphMemory.record_memory_coactivation() at API layer instead

Source

pub fn graph_maintenance(&self)

👎Deprecated:

Use GraphMemory.apply_decay() at API layer instead

Perform graph maintenance - DEPRECATED Use GraphMemory.apply_decay() at API layer instead

Source

pub fn graph_stats(&self) -> MemoryGraphStats

👎Deprecated:

Use GraphMemory.get_stats() at API layer instead

Get memory graph statistics - DEPRECATED Use GraphMemory.get_stats() at API layer instead

Source

pub fn auto_rebuild_index_if_needed(&self) -> Result<bool>

Check if vector index needs rebuild and rebuild if necessary

Returns true if rebuild was performed.

IMPORTANT: We perform a full rebuild from RocksDB rather than using Vamana’s internal auto_rebuild_if_needed(). The internal rebuild extracts live vectors and assigns new sequential IDs (0, 1, 2, …), but does NOT update the RetrievalEngine’s id_mapping. This would silently corrupt all search results after compaction — searches would return wrong memories because old vector_id→memory_id mappings no longer match the new vector IDs.

By rebuilding from RocksDB (the single source of truth), both the vector index and the id_mapping are rebuilt atomically.

Source

pub fn index_health(&self) -> IndexHealth

Get vector index degradation info

Source§

impl RetrievalEngine

Source

pub fn search_tracked( &self, query: &Query, limit: usize, ) -> Result<TrackedRetrieval>

Search with tracking for later feedback

Use this when you want to provide feedback on retrieval quality. Returns a TrackedRetrieval that can be used with reinforce_recall.

Source

pub fn reinforce_recall( &self, memory_ids: &[MemoryId], outcome: RetrievalOutcome, ) -> Result<ReinforcementStats>

Reinforce memories based on task outcome (core feedback loop)

This is THE key method that closes the Hebbian loop:

  • If outcome is Helpful: strengthen associations, boost importance
  • If outcome is Misleading: weaken associations, reduce importance
  • If outcome is Neutral: just record access (mild reinforcement)

Call this after a task completes to indicate which memories helped.

Source

pub fn reinforce_tracked( &self, tracked: &TrackedRetrieval, outcome: RetrievalOutcome, ) -> Result<ReinforcementStats>

Reinforce using a tracked retrieval (convenience wrapper)

Source

pub fn reinforce_batch( &self, feedbacks: &[RetrievalFeedback], retrieval_memories: &HashMap<String, Vec<MemoryId>>, ) -> Result<Vec<ReinforcementStats>>

Batch reinforce multiple retrievals (for async feedback processing)

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Sync + Send>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<T> Fruit for T
where T: Send + Downcast,