Skip to main content

MemoryStore

Trait MemoryStore 

Source
pub trait MemoryStore: Send + Sync {
Show 21 methods // Required methods fn upsert_fact<'life0, 'async_trait>( &'life0 self, fact: NewFact, embedding: Option<StoredEmbedding>, now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<UpsertOutcome>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn supersede_fact<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, scope: &'life1 str, category: &'life2 str, key: &'life3 str, now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait; fn active_facts<'life0, 'life1, 'async_trait>( &'life0 self, scope: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<FactRecord>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn fact_history<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, scope: &'life1 str, category: &'life2 str, key: &'life3 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<Fact>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait; fn record_fact_recalls<'life0, 'life1, 'async_trait>( &'life0 self, ids: &'life1 [String], now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn insert_episode<'life0, 'async_trait>( &'life0 self, episode: NewEpisode, embedding: Option<StoredEmbedding>, now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<Episode>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; fn episodes<'life0, 'life1, 'async_trait>( &'life0 self, scope: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<EpisodeRecord>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn record_episode_recalls<'life0, 'life1, 'async_trait>( &'life0 self, ids: &'life1 [String], now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait; fn stats<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<MemoryStats>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait; // Provided methods fn upsert_fact_with_access<'life0, 'life1, 'async_trait>( &'life0 self, fact: NewFact, embedding: Option<StoredEmbedding>, _access: &'life1 FactAccess, now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<UpsertOutcome>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn fact_access<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _scope: &'life1 str, _ids: &'life2 [String], ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, FactAccess>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn fact_lexical_scores<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, scope: &'life1 str, query: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, f32>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn fact_graph_scores<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _scope: &'life1 str, _seed_ids: &'life2 [String], ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, f32>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn search_facts<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _scope: &'life1 str, _query: &'life2 str, _query_embedding: Option<&'life3 StoredEmbedding>, _limit: usize, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<ScoredFact>>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait { ... } fn insert_episode_with_access<'life0, 'life1, 'async_trait>( &'life0 self, episode: NewEpisode, embedding: Option<StoredEmbedding>, _access: &'life1 FactAccess, now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<Episode>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn episode_access<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _scope: &'life1 str, _ids: &'life2 [String], ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, FactAccess>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn episode_lexical_scores<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, scope: &'life1 str, query: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, f32>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn search_episodes<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _scope: &'life1 str, _query: &'life2 str, _query_embedding: Option<&'life3 StoredEmbedding>, _limit: usize, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<ScoredEpisode>>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait { ... } fn decay<'life0, 'life1, 'async_trait>( &'life0 self, _now: DateTime<Utc>, _config: &'life1 RetentionConfig, ) -> Pin<Box<dyn Future<Output = Result<DecayReport>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn stale_embeddings<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _model: &'life1 str, _kind: RecordKind, _after_id: Option<&'life2 str>, _limit: usize, ) -> Pin<Box<dyn Future<Output = Result<Vec<StaleEmbedding>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait { ... } fn replace_embedding<'life0, 'life1, 'async_trait>( &'life0 self, _stale: &'life1 StaleEmbedding, _embedding: StoredEmbedding, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... }
}
Expand description

Persistence for facts and episodes. This is the extension point for bringing your own backend (pgvector, Qdrant, a document store, …). crate::Memory holds an Arc<dyn MemoryStore>; the trait is object-safe.

Contract:

  • Facts are keyed by (scope, category, key) (already normalized when they arrive) and at most one is active per key.
  • upsert_fact with a different value marks the active row superseded (sets superseded_at) and inserts a new one; with the same value (see crate::same_value) it refreshes updated_at, keeps the higher confidence and returns UpsertOutcome::Reinforced.
  • Superseded facts are never returned by active_facts or searches, but remain in fact_history until retention deletes them.
  • Scopes are hard boundaries: never return another scope’s rows.

Lexical scoring and searching have defaults, so a minimal store only stores rows. A store with its own ranking (vector DB, hybrid search engine) overrides MemoryStore::search_facts / MemoryStore::search_episodes and the crate::Scorer is bypassed.

Near-duplicate merging needs nothing from a store: crate::Memory finds the duplicate and writes through MemoryStore::upsert_fact_with_access on the existing key, which keeps history by the rules above.

Access levels are optional too. A store that keeps the default MemoryStore::fact_access cannot say who may see a record, so crate::Memory shows its records only to the owner in a direct conversation and never in a shared channel.

Required Methods§

Source

fn upsert_fact<'life0, 'async_trait>( &'life0 self, fact: NewFact, embedding: Option<StoredEmbedding>, now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<UpsertOutcome>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Insert, reinforce or supersede (see the trait docs). embedding describes the new value’s search text, when one was computed.

Source

fn supersede_fact<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, scope: &'life1 str, category: &'life2 str, key: &'life3 str, now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Mark the active fact for this key superseded. false if none.

Source

fn active_facts<'life0, 'life1, 'async_trait>( &'life0 self, scope: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<FactRecord>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Every active fact in scope, with embeddings.

Source

fn fact_history<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, scope: &'life1 str, category: &'life2 str, key: &'life3 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<Fact>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

All versions of one key, oldest first, superseded ones included.

Source

fn record_fact_recalls<'life0, 'life1, 'async_trait>( &'life0 self, ids: &'life1 [String], now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Bump recall_count and set last_recalled_at for these facts.

Source

fn insert_episode<'life0, 'async_trait>( &'life0 self, episode: NewEpisode, embedding: Option<StoredEmbedding>, now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<Episode>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source

fn episodes<'life0, 'life1, 'async_trait>( &'life0 self, scope: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<EpisodeRecord>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Every episode in scope, with embeddings.

Source

fn record_episode_recalls<'life0, 'life1, 'async_trait>( &'life0 self, ids: &'life1 [String], now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Source

fn stats<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<MemoryStats>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Provided Methods§

Source

fn upsert_fact_with_access<'life0, 'life1, 'async_trait>( &'life0 self, fact: NewFact, embedding: Option<StoredEmbedding>, _access: &'life1 FactAccess, now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<UpsertOutcome>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

MemoryStore::upsert_fact that records who may see the fact, atomically with the write. A reinforced fact keeps the narrowest of its access and access (FactAccess::narrowest); a new version takes access. The default ignores access, which leaves the fact unclassified and so private.

Source

fn fact_access<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _scope: &'life1 str, _ids: &'life2 [String], ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, FactAccess>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Who may see these facts, by id, for ids in scope the store knows. A fact absent from the map is unclassified and treated as private.

Source

fn fact_lexical_scores<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, scope: &'life1 str, query: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, f32>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Lexical relevance (0.0-1.0) of active facts to query, by fact id. Facts absent from the map score 0. The default is word overlap over Fact::search_text.

Source

fn fact_graph_scores<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _scope: &'life1 str, _seed_ids: &'life2 [String], ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, f32>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

How related each active fact in scope is to the seed_ids (facts that matched the query), 0.0-1.0 by fact id. The default has no graph: an empty map leaves the graph signal at 0 and ranking as it was. A store that keeps no edges can use related_fact_scores.

Source

fn search_facts<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _scope: &'life1 str, _query: &'life2 str, _query_embedding: Option<&'life3 StoredEmbedding>, _limit: usize, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<ScoredFact>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Native ranked search. Return Some to bypass the scorer: the result is used as is (already filtered, ordered, at most limit). The default None lets crate::Memory score candidates itself.

Source

fn insert_episode_with_access<'life0, 'life1, 'async_trait>( &'life0 self, episode: NewEpisode, embedding: Option<StoredEmbedding>, _access: &'life1 FactAccess, now: DateTime<Utc>, ) -> Pin<Box<dyn Future<Output = Result<Episode>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

MemoryStore::insert_episode that records who may see the episode. The default ignores access, leaving the episode private.

Source

fn episode_access<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _scope: &'life1 str, _ids: &'life2 [String], ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, FactAccess>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Who may see these episodes; see MemoryStore::fact_access.

Source

fn episode_lexical_scores<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, scope: &'life1 str, query: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<HashMap<String, f32>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Lexical relevance of episodes; default word overlap.

Source

fn search_episodes<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, _scope: &'life1 str, _query: &'life2 str, _query_embedding: Option<&'life3 StoredEmbedding>, _limit: usize, ) -> Pin<Box<dyn Future<Output = Result<Option<Vec<ScoredEpisode>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Native ranked episode search; see MemoryStore::search_facts.

Source

fn decay<'life0, 'life1, 'async_trait>( &'life0 self, _now: DateTime<Utc>, _config: &'life1 RetentionConfig, ) -> Pin<Box<dyn Future<Output = Result<DecayReport>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Apply recall decay and retention (see RetentionConfig). The default does nothing, for stores whose backend handles expiry.

Source

fn stale_embeddings<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, _model: &'life1 str, _kind: RecordKind, _after_id: Option<&'life2 str>, _limit: usize, ) -> Pin<Box<dyn Future<Output = Result<Vec<StaleEmbedding>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Up to limit records of kind with no vector or a vector from a model other than model, in ascending id order after after_id. Superseded facts are excluded: they are never compared. The default finds none, so such a store keeps lexical recall for old rows.

Source

fn replace_embedding<'life0, 'life1, 'async_trait>( &'life0 self, _stale: &'life1 StaleEmbedding, _embedding: StoredEmbedding, ) -> Pin<Box<dyn Future<Output = Result<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Replace a stale record’s vector, only if it still has the model stale reported; false otherwise. Changes nothing else about the record: updated_at feeds ranking and this is not a new statement.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§