pub struct MemoryManager { /* private fields */ }Expand description
Agent memory manager.
Stores and retrieves memory entries using a pluggable storage backend. Supports embedding-based vector search via an in-memory TF-IDF index that is rebuilt on startup.
Implementations§
Source§impl MemoryManager
impl MemoryManager
Sourcepub async fn semantic_search(
&self,
query: &str,
memory_type: Option<MemoryType>,
limit: usize,
hnsw_index: &HnswMemoryIndex,
) -> Result<Vec<SemanticHit>>
pub async fn semantic_search( &self, query: &str, memory_type: Option<MemoryType>, limit: usize, hnsw_index: &HnswMemoryIndex, ) -> Result<Vec<SemanticHit>>
Semantic search using HNSW index.
Unlike search() which uses brute-force cosine similarity over the
in-memory HashMap, semantic_search() uses the HNSW approximate
nearest neighbor index for sub-linear time complexity.
Sourcepub async fn rebuild_hnsw_index(
&self,
hnsw_index: &HnswMemoryIndex,
) -> Result<usize>
pub async fn rebuild_hnsw_index( &self, hnsw_index: &HnswMemoryIndex, ) -> Result<usize>
Rebuild the HNSW index from all stored memories.
Call this at startup or after bulk operations.
Sourcepub async fn list_by_tier(
&self,
tier: MemoryTier,
limit: usize,
) -> Result<Vec<MemoryEntry>>
pub async fn list_by_tier( &self, tier: MemoryTier, limit: usize, ) -> Result<Vec<MemoryEntry>>
List memories by tier (loads all types, filters by tier field).
Sourcepub async fn get_by_id(&self, id: &str) -> Result<Option<MemoryEntry>>
pub async fn get_by_id(&self, id: &str) -> Result<Option<MemoryEntry>>
Get a memory entry by ID (searches all types).
Sourcepub async fn load_by_reference(
&self,
reference: &str,
) -> Result<Option<MemoryEntry>>
pub async fn load_by_reference( &self, reference: &str, ) -> Result<Option<MemoryEntry>>
Load a memory entry by reference string (ID or category/id).
Sourcepub async fn select_by_manifest(
&self,
query: &str,
limit: usize,
) -> Result<Vec<MemoryEntry>>
pub async fn select_by_manifest( &self, query: &str, limit: usize, ) -> Result<Vec<MemoryEntry>>
Select memories by manifest (keyword matching against content).
Sourcepub async fn build_hot_context(&self, token_budget: usize) -> Result<String>
pub async fn build_hot_context(&self, token_budget: usize) -> Result<String>
Build the Hot tier context for agent prompt injection.
Sourcepub async fn build_full_context(
&self,
_query: &str,
system_prompt: &str,
token_budget: usize,
) -> Result<String>
pub async fn build_full_context( &self, _query: &str, system_prompt: &str, token_budget: usize, ) -> Result<String>
Build full context: hot context + proactive recall blended into system prompt.
Sourcepub async fn shift_tier(
&self,
id: &str,
from: MemoryTier,
to: MemoryTier,
) -> Result<()>
pub async fn shift_tier( &self, id: &str, from: MemoryTier, to: MemoryTier, ) -> Result<()>
Shift a memory entry between tiers.
Sourcepub async fn unpin(&self, id: &str) -> Result<()>
pub async fn unpin(&self, id: &str) -> Result<()>
Unpin a memory (revert to auto-computed protection).
Sourcepub async fn set_importance(&self, id: &str, importance: f32) -> Result<()>
pub async fn set_importance(&self, id: &str, importance: f32) -> Result<()>
Set importance for a memory entry.
Sourcepub async fn recompute_all_decay(&self, multiplier: f32) -> Result<usize>
pub async fn recompute_all_decay(&self, multiplier: f32) -> Result<usize>
Recompute decay scores for all entries.
Returns the number of entries updated.
Sourcepub async fn immediate_hot_overflow(&self, hot_max: usize) -> Result<usize>
pub async fn immediate_hot_overflow(&self, hot_max: usize) -> Result<usize>
Immediate Hot overflow handling.
Source§impl MemoryManager
impl MemoryManager
Sourcepub async fn total_entries(&self) -> usize
pub async fn total_entries(&self) -> usize
Returns total entries across all memory types (from disk).
Sourcepub async fn rebuild_index(&self) -> Result<()>
pub async fn rebuild_index(&self) -> Result<()>
Rebuild the vector index from all stored memories.
Call once at startup to populate the in-memory index from persisted memory entries.
Sourcepub async fn save_index_snapshot(&self) -> Result<()>
pub async fn save_index_snapshot(&self) -> Result<()>
Save the current vector index to disk as a snapshot.
Sourcepub async fn load_index_snapshot(&self) -> Result<usize>
pub async fn load_index_snapshot(&self) -> Result<usize>
Load a previously saved vector index snapshot from disk.
Sourcepub async fn remember(&self, entry: MemoryEntry) -> Result<String>
pub async fn remember(&self, entry: MemoryEntry) -> Result<String>
Store a memory entry. Returns the entry ID.
When SQLite backend is enabled, delegates to SqliteMemoryStore.
Otherwise computes and stores the entry’s text vector in the in-memory
index for future semantic search.
Sourcepub async fn get(
&self,
id: &str,
memory_type: MemoryType,
) -> Result<Option<MemoryEntry>>
pub async fn get( &self, id: &str, memory_type: MemoryType, ) -> Result<Option<MemoryEntry>>
Retrieve a single memory by ID.
Records access for auto-protection tracking.
Sourcepub async fn forget(&self, id: &str, memory_type: MemoryType) -> Result<bool>
pub async fn forget(&self, id: &str, memory_type: MemoryType) -> Result<bool>
Delete a memory entry.
Sourcepub async fn list(
&self,
memory_type: MemoryType,
limit: usize,
) -> Result<Vec<MemoryEntry>>
pub async fn list( &self, memory_type: MemoryType, limit: usize, ) -> Result<Vec<MemoryEntry>>
List memories of a given type, most recent first.
Sourcepub async fn search(
&self,
query: &str,
memory_type: Option<MemoryType>,
limit: usize,
) -> Result<Vec<MemoryEntry>>
pub async fn search( &self, query: &str, memory_type: Option<MemoryType>, limit: usize, ) -> Result<Vec<MemoryEntry>>
Search memories by semantic similarity (vector search).
Falls back to keyword search when the vector index is empty or yields no results above the similarity threshold.
Sourcepub async fn recall(&self, query: &str) -> Result<Vec<MemoryEntry>>
pub async fn recall(&self, query: &str) -> Result<Vec<MemoryEntry>>
Recall relevant memories for a new session.
Combines recent conversation summaries, session summaries, and keyword-matched facts/episodes.
Sourcepub fn blend_into_prompt(
&self,
memories: &[MemoryEntry],
system_prompt: &str,
) -> String
pub fn blend_into_prompt( &self, memories: &[MemoryEntry], system_prompt: &str, ) -> String
Blend recalled memories into the system prompt.
Sourcepub async fn is_duplicate(&self, content: &str) -> bool
pub async fn is_duplicate(&self, content: &str) -> bool
Check if a memory entry with identical content already exists.
Uses a fast hash comparison against the in-memory vector index.
Sourcepub async fn remember_unique(
&self,
entry: MemoryEntry,
) -> Result<Option<String>>
pub async fn remember_unique( &self, entry: MemoryEntry, ) -> Result<Option<String>>
Store a memory entry only if no duplicate content exists.
Returns the entry ID if stored, or None if duplicate.
Sourcepub async fn recall_with_proactive(
&self,
query: &str,
recall_timing: &mut Option<RecallTiming>,
) -> Result<Vec<MemoryEntry>>
pub async fn recall_with_proactive( &self, query: &str, recall_timing: &mut Option<RecallTiming>, ) -> Result<Vec<MemoryEntry>>
Recall with proactive enhancement.
Extends the standard recall() with proactive memory injection
based on RecallTiming triggers.
Source§impl MemoryManager
impl MemoryManager
Sourcepub fn new(storage: Arc<dyn MemoryStorage>) -> Self
pub fn new(storage: Arc<dyn MemoryStorage>) -> Self
Create a new MemoryManager with a storage backend.
Sourcepub fn set_git_layer(&mut self, gl: Arc<dyn MemoryGit>)
pub fn set_git_layer(&mut self, gl: Arc<dyn MemoryGit>)
Attach a git layer for version-controlled saves.
Sourcepub fn set_sona_engine(&mut self, engine: Arc<SonaEngine>)
pub fn set_sona_engine(&mut self, engine: Arc<SonaEngine>)
Attach a SONA learning engine (RFC-020 Phase 2).
Sourcepub fn sona_engine(&self) -> Option<&Arc<SonaEngine>>
pub fn sona_engine(&self) -> Option<&Arc<SonaEngine>>
Get a reference to the SONA engine (if configured).
Sourcepub fn set_hnsw_index(&self, index: Arc<HnswMemoryIndex>)
pub fn set_hnsw_index(&self, index: Arc<HnswMemoryIndex>)
Attach an HNSW index for fast semantic search.
Sourcepub fn with_max_recall(self, n: usize) -> Self
pub fn with_max_recall(self, n: usize) -> Self
Set max memories returned by recall.
Sourcepub fn set_max_recall(&mut self, n: usize)
pub fn set_max_recall(&mut self, n: usize)
Set max_recall in-place.
Sourcepub fn vector_index_size(&self) -> usize
pub fn vector_index_size(&self) -> usize
Returns the number of entries in the vector index.
Sourcepub fn effective_importance(entry: &MemoryEntry) -> f32
pub fn effective_importance(entry: &MemoryEntry) -> f32
Compute effective importance of a memory entry.
Sourcepub async fn curate(&self, budget: &MemoryBudget) -> Result<CurationReport>
pub async fn curate(&self, budget: &MemoryBudget) -> Result<CurationReport>
Curate memories: identify candidates for removal based on budget.
Sourcepub fn spawn_curation_task(self: &Arc<Self>, budget: MemoryBudget)
pub fn spawn_curation_task(self: &Arc<Self>, budget: MemoryBudget)
Spawn a background curation task.