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:
vector_index- Vector similarity search indexid_mapping- Memory ID ↔ Vector ID mappingconsolidation_events- Introspection event buffer
Rules:
- Never acquire a higher-numbered lock while holding a lower-numbered lock
- For read operations, prefer
read()overwrite()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
impl RetrievalEngine
Sourcepub fn new(
storage: Arc<MemoryStorage>,
embedder: Arc<MiniLMEmbedder>,
) -> Result<Self>
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
Sourcepub fn with_event_buffer(
storage: Arc<MemoryStorage>,
embedder: Arc<MiniLMEmbedder>,
consolidation_events: Option<Arc<RwLock<ConsolidationEventBuffer>>>,
) -> Result<Self>
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.
Sourcepub fn set_consolidation_events(
&mut self,
events: Arc<RwLock<ConsolidationEventBuffer>>,
)
pub fn set_consolidation_events( &mut self, events: Arc<RwLock<ConsolidationEventBuffer>>, )
Set the consolidation event buffer (for late binding after construction)
Sourcepub fn save(&self) -> Result<()>
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.
Sourcepub fn get_indexed_memory_ids(&self) -> HashSet<MemoryId>
pub fn get_indexed_memory_ids(&self) -> HashSet<MemoryId>
Get set of all indexed memory IDs (for integrity checking)
Sourcepub fn index_memory(&self, memory: &Memory) -> Result<()>
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.
Sourcepub fn reindex_memory(&self, memory: &Memory) -> Result<()>
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)
Sourcepub fn remove_memory(&self, memory_id: &MemoryId) -> bool
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.
Sourcepub fn search_ids(
&self,
query: &Query,
limit: usize,
) -> Result<Vec<(MemoryId, f32)>>
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
Sourcepub fn get_from_storage(&self, id: &MemoryId) -> Result<Memory>
pub fn get_from_storage(&self, id: &MemoryId) -> Result<Memory>
Get memory from storage by ID
Sourcepub fn search_by_embedding(
&self,
embedding: &[f32],
limit: usize,
exclude_id: Option<&MemoryId>,
) -> Result<Vec<(MemoryId, f32)>>
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
Sourcepub fn search(&self, query: &Query, limit: usize) -> Result<Vec<SharedMemory>>
pub fn search(&self, query: &Query, limit: usize) -> Result<Vec<SharedMemory>>
Search for memories using multiple retrieval modes (zero-copy with Arc)
Sourcepub fn matches_filters(&self, memory: &Memory, query: &Query) -> bool
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.
Sourcepub fn rebuild_index(&self) -> Result<()>
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
Sourcepub fn add_to_graph(&self, _memory: &Memory)
👎Deprecated: Use GraphMemory at API layer instead
pub fn add_to_graph(&self, _memory: &Memory)
Use GraphMemory at API layer instead
Add memory to knowledge graph - DEPRECATED Use GraphMemory at the API layer instead
Sourcepub fn record_coactivation(&self, _memory_ids: &[MemoryId])
👎Deprecated: Use GraphMemory.record_memory_coactivation() at API layer instead
pub fn record_coactivation(&self, _memory_ids: &[MemoryId])
Use GraphMemory.record_memory_coactivation() at API layer instead
Record co-activation of memories - DEPRECATED Use GraphMemory.record_memory_coactivation() at API layer instead
Sourcepub fn graph_maintenance(&self)
👎Deprecated: Use GraphMemory.apply_decay() at API layer instead
pub fn graph_maintenance(&self)
Use GraphMemory.apply_decay() at API layer instead
Perform graph maintenance - DEPRECATED Use GraphMemory.apply_decay() at API layer instead
Sourcepub fn graph_stats(&self) -> MemoryGraphStats
👎Deprecated: Use GraphMemory.get_stats() at API layer instead
pub fn graph_stats(&self) -> MemoryGraphStats
Use GraphMemory.get_stats() at API layer instead
Get memory graph statistics - DEPRECATED Use GraphMemory.get_stats() at API layer instead
Sourcepub fn auto_rebuild_index_if_needed(&self) -> Result<bool>
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.
Sourcepub fn index_health(&self) -> IndexHealth
pub fn index_health(&self) -> IndexHealth
Get vector index degradation info
Source§impl RetrievalEngine
impl RetrievalEngine
Sourcepub fn search_tracked(
&self,
query: &Query,
limit: usize,
) -> Result<TrackedRetrieval>
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.
Sourcepub fn reinforce_recall(
&self,
memory_ids: &[MemoryId],
outcome: RetrievalOutcome,
) -> Result<ReinforcementStats>
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.
Sourcepub fn reinforce_tracked(
&self,
tracked: &TrackedRetrieval,
outcome: RetrievalOutcome,
) -> Result<ReinforcementStats>
pub fn reinforce_tracked( &self, tracked: &TrackedRetrieval, outcome: RetrievalOutcome, ) -> Result<ReinforcementStats>
Reinforce using a tracked retrieval (convenience wrapper)
Sourcepub fn reinforce_batch(
&self,
feedbacks: &[RetrievalFeedback],
retrieval_memories: &HashMap<String, Vec<MemoryId>>,
) -> Result<Vec<ReinforcementStats>>
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§
impl Freeze for RetrievalEngine
impl !RefUnwindSafe for RetrievalEngine
impl Send for RetrievalEngine
impl Sync for RetrievalEngine
impl Unpin for RetrievalEngine
impl UnsafeUnpin for RetrievalEngine
impl !UnwindSafe for RetrievalEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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