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>, Error>
pub async fn semantic_search( &self, query: &str, memory_type: Option<MemoryType>, limit: usize, hnsw_index: &HnswMemoryIndex, ) -> Result<Vec<SemanticHit>, Error>
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, Error>
pub async fn rebuild_hnsw_index( &self, hnsw_index: &HnswMemoryIndex, ) -> Result<usize, Error>
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>, Error>
pub async fn list_by_tier( &self, tier: MemoryTier, limit: usize, ) -> Result<Vec<MemoryEntry>, Error>
List memories by tier (loads all types, filters by tier field).
Sourcepub async fn get_by_id(&self, id: &str) -> Result<Option<MemoryEntry>, Error>
pub async fn get_by_id(&self, id: &str) -> Result<Option<MemoryEntry>, Error>
Get a memory entry by ID (searches all types).
Sourcepub async fn load_by_reference(
&self,
reference: &str,
) -> Result<Option<MemoryEntry>, Error>
pub async fn load_by_reference( &self, reference: &str, ) -> Result<Option<MemoryEntry>, Error>
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>, Error>
pub async fn select_by_manifest( &self, query: &str, limit: usize, ) -> Result<Vec<MemoryEntry>, Error>
Select memories by manifest (keyword matching against content).
Sourcepub async fn build_hot_context(
&self,
token_budget: usize,
) -> Result<String, Error>
pub async fn build_hot_context( &self, token_budget: usize, ) -> Result<String, Error>
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, Error>
pub async fn build_full_context( &self, _query: &str, system_prompt: &str, token_budget: usize, ) -> Result<String, Error>
Build full context: hot context + proactive recall blended into system prompt.
Sourcepub async fn shift_tier(
&self,
id: &str,
from: MemoryTier,
to: MemoryTier,
) -> Result<(), Error>
pub async fn shift_tier( &self, id: &str, from: MemoryTier, to: MemoryTier, ) -> Result<(), Error>
Shift a memory entry between tiers.
Sourcepub async fn pin(&self, id: &str) -> Result<(), Error>
pub async fn pin(&self, id: &str) -> Result<(), Error>
Pin a memory (set permanent protection).
Sourcepub async fn unpin(&self, id: &str) -> Result<(), Error>
pub async fn unpin(&self, id: &str) -> Result<(), Error>
Unpin a memory (revert to auto-computed protection).
Sourcepub async fn set_importance(
&self,
id: &str,
importance: f32,
) -> Result<(), Error>
pub async fn set_importance( &self, id: &str, importance: f32, ) -> Result<(), Error>
Set importance for a memory entry.
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<(), Error>
pub async fn rebuild_index(&self) -> Result<(), Error>
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<(), Error>
pub async fn save_index_snapshot(&self) -> Result<(), Error>
Save the current vector index to disk as a snapshot.
Sourcepub async fn load_index_snapshot(&self) -> Result<usize, Error>
pub async fn load_index_snapshot(&self) -> Result<usize, Error>
Load a previously saved vector index snapshot from disk.
Sourcepub async fn remember(&self, entry: MemoryEntry) -> Result<String, Error>
pub async fn remember(&self, entry: MemoryEntry) -> Result<String, Error>
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>, Error>
pub async fn get( &self, id: &str, memory_type: MemoryType, ) -> Result<Option<MemoryEntry>, Error>
Retrieve a single memory by ID.
Records access for auto-protection tracking.
Sourcepub async fn forget(
&self,
id: &str,
memory_type: MemoryType,
) -> Result<bool, Error>
pub async fn forget( &self, id: &str, memory_type: MemoryType, ) -> Result<bool, Error>
Delete a memory entry.
Sourcepub async fn list(
&self,
memory_type: MemoryType,
limit: usize,
) -> Result<Vec<MemoryEntry>, Error>
pub async fn list( &self, memory_type: MemoryType, limit: usize, ) -> Result<Vec<MemoryEntry>, Error>
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>, Error>
pub async fn search( &self, query: &str, memory_type: Option<MemoryType>, limit: usize, ) -> Result<Vec<MemoryEntry>, Error>
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>, Error>
pub async fn recall(&self, query: &str) -> Result<Vec<MemoryEntry>, Error>
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 recall_with_rerank(
&self,
query: &str,
) -> Result<Vec<MemoryEntry>, Error>
pub async fn recall_with_rerank( &self, query: &str, ) -> Result<Vec<MemoryEntry>, Error>
Recall with Flash Attention re-ranking (Phase 6).
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>, Error>
pub async fn remember_unique( &self, entry: MemoryEntry, ) -> Result<Option<String>, Error>
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>, Error>
pub async fn recall_with_proactive( &self, query: &str, recall_timing: &mut Option<RecallTiming>, ) -> Result<Vec<MemoryEntry>, Error>
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>) -> MemoryManager
pub fn new(storage: Arc<dyn MemoryStorage>) -> MemoryManager
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_sqlite_store(&mut self, store: Arc<SqliteMemoryStore>)
pub fn set_sqlite_store(&mut self, store: Arc<SqliteMemoryStore>)
Attach a SQLite-backed memory store (RFC-012).
Sourcepub fn sqlite_store(&self) -> &Option<Arc<SqliteMemoryStore>>
pub fn sqlite_store(&self) -> &Option<Arc<SqliteMemoryStore>>
Get a reference to the SQLite store (if configured).
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) -> MemoryManager
pub fn with_max_recall(self, n: usize) -> MemoryManager
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, Error>
pub async fn curate( &self, budget: &MemoryBudget, ) -> Result<CurationReport, Error>
Curate memories: identify candidates for removal based on budget.
Sourcepub fn spawn_curation_task(self: &Arc<MemoryManager>, budget: MemoryBudget)
pub fn spawn_curation_task(self: &Arc<MemoryManager>, budget: MemoryBudget)
Spawn a background curation task.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for MemoryManager
impl !RefUnwindSafe for MemoryManager
impl !UnwindSafe for MemoryManager
impl Send for MemoryManager
impl Sync for MemoryManager
impl Unpin for MemoryManager
impl UnsafeUnpin for MemoryManager
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
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 moreimpl<T> MaybeSendSync for T
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.