aidaemon-memory 0.25.0

Swappable long-term memory for aidaemon agents: the MemoryStore traits plus SQLite and embedding implementations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use std::collections::{HashMap, HashSet};
use std::sync::Mutex;

use async_trait::async_trait;
use chrono::{DateTime, Utc};

use super::{MemoryStore, related_fact_scores};
use crate::error::{MemoryError, Result};
use crate::types::{
    DecayReport, Episode, EpisodeRecord, Fact, FactAccess, FactRecord, MemoryStats, NewEpisode,
    NewFact, RecordKind, RetentionConfig, StaleEmbedding, StoredEmbedding, UpsertOutcome, new_id,
    normalize_category, normalize_key, same_value,
};

#[derive(Default)]
struct Inner {
    facts: Vec<FactRecord>,
    episodes: Vec<EpisodeRecord>,
    /// By record id. Rows without an entry are private.
    fact_access: HashMap<String, FactAccess>,
    episode_access: HashMap<String, FactAccess>,
}

/// A process-local [`MemoryStore`]. Same semantics as the SQLite store,
/// nothing persisted. Lexical scores use word overlap.
#[derive(Default)]
pub struct InMemoryMemoryStore {
    inner: Mutex<Inner>,
}

impl InMemoryMemoryStore {
    pub fn new() -> Self {
        Self::default()
    }

    fn lock(&self) -> Result<std::sync::MutexGuard<'_, Inner>> {
        self.inner
            .lock()
            .map_err(|_| MemoryError::Store("in-memory store lock poisoned".into()))
    }
}

/// Decay a recall count by a tenth, at least one (daemon `decay_memories`).
pub(crate) fn decayed_fact_recalls(count: u32) -> u32 {
    count.saturating_sub((count / 10).max(1))
}

#[async_trait]
impl MemoryStore for InMemoryMemoryStore {
    async fn upsert_fact(
        &self,
        fact: NewFact,
        embedding: Option<StoredEmbedding>,
        now: DateTime<Utc>,
    ) -> Result<UpsertOutcome> {
        self.upsert_fact_with_access(fact, embedding, &FactAccess::default(), now)
            .await
    }

    async fn upsert_fact_with_access(
        &self,
        fact: NewFact,
        embedding: Option<StoredEmbedding>,
        access: &FactAccess,
        now: DateTime<Utc>,
    ) -> Result<UpsertOutcome> {
        let mut inner = self.lock()?;
        let Inner {
            facts, fact_access, ..
        } = &mut *inner;
        let active = facts.iter_mut().find(|r| {
            r.fact.is_active()
                && r.fact.scope == fact.scope
                && r.fact.category == fact.category
                && r.fact.key == fact.key
        });
        let mut previous = None;
        if let Some(record) = active {
            if same_value(&record.fact.value, &fact.value) {
                record.fact.updated_at = now;
                record.fact.confidence = record.fact.confidence.max(fact.confidence);
                if record.fact.source.excerpt.is_none() {
                    record.fact.source.excerpt = fact.source.excerpt;
                }
                if embedding.is_some() {
                    record.embedding = embedding;
                }
                let id = record.fact.id.clone();
                let narrowed = fact_access
                    .get(&id)
                    .cloned()
                    .unwrap_or_default()
                    .narrowest(access);
                fact_access.insert(id.clone(), narrowed);
                return Ok(UpsertOutcome::Reinforced { id });
            }
            record.fact.superseded_at = Some(now);
            previous = Some((record.fact.id.clone(), record.fact.value.clone()));
        }
        let id = new_id();
        fact_access.insert(id.clone(), access.clone());
        facts.push(FactRecord {
            fact: Fact {
                id: id.clone(),
                scope: fact.scope,
                category: fact.category,
                key: fact.key,
                value: fact.value,
                confidence: fact.confidence,
                source: fact.source,
                created_at: now,
                updated_at: now,
                superseded_at: None,
                recall_count: 0,
                last_recalled_at: None,
            },
            embedding,
        });
        Ok(match previous {
            None => UpsertOutcome::Inserted { id },
            Some((previous_id, previous_value)) => UpsertOutcome::Superseded {
                id,
                previous_id,
                previous_value,
            },
        })
    }

    async fn fact_access(
        &self,
        scope: &str,
        ids: &[String],
    ) -> Result<HashMap<String, FactAccess>> {
        let inner = self.lock()?;
        let wanted: HashSet<&String> = ids.iter().collect();
        Ok(inner
            .facts
            .iter()
            .filter(|r| r.fact.scope == scope && wanted.contains(&r.fact.id))
            .map(|r| {
                let access = inner.fact_access.get(&r.fact.id).cloned();
                (r.fact.id.clone(), access.unwrap_or_default())
            })
            .collect())
    }

    async fn supersede_fact(
        &self,
        scope: &str,
        category: &str,
        key: &str,
        now: DateTime<Utc>,
    ) -> Result<bool> {
        let (category, key) = (normalize_category(category), normalize_key(key));
        let mut inner = self.lock()?;
        let mut changed = false;
        for r in inner.facts.iter_mut().filter(|r| {
            r.fact.is_active()
                && r.fact.scope == scope
                && r.fact.category == category
                && r.fact.key == key
        }) {
            r.fact.superseded_at = Some(now);
            changed = true;
        }
        Ok(changed)
    }

    async fn active_facts(&self, scope: &str) -> Result<Vec<FactRecord>> {
        Ok(self
            .lock()?
            .facts
            .iter()
            .filter(|r| r.fact.is_active() && r.fact.scope == scope)
            .cloned()
            .collect())
    }

    async fn fact_graph_scores(
        &self,
        scope: &str,
        seed_ids: &[String],
    ) -> Result<HashMap<String, f32>> {
        let active: Vec<Fact> = self
            .active_facts(scope)
            .await?
            .into_iter()
            .map(|r| r.fact)
            .collect();
        Ok(related_fact_scores(&active, seed_ids))
    }

    async fn fact_history(&self, scope: &str, category: &str, key: &str) -> Result<Vec<Fact>> {
        let (category, key) = (normalize_category(category), normalize_key(key));
        Ok(self
            .lock()?
            .facts
            .iter()
            .filter(|r| r.fact.scope == scope && r.fact.category == category && r.fact.key == key)
            .map(|r| r.fact.clone())
            .collect())
    }

    async fn record_fact_recalls(&self, ids: &[String], now: DateTime<Utc>) -> Result<()> {
        let ids: HashSet<&String> = ids.iter().collect();
        for r in self
            .lock()?
            .facts
            .iter_mut()
            .filter(|r| ids.contains(&r.fact.id))
        {
            r.fact.recall_count += 1;
            r.fact.last_recalled_at = Some(now);
        }
        Ok(())
    }

    async fn insert_episode(
        &self,
        episode: NewEpisode,
        embedding: Option<StoredEmbedding>,
        now: DateTime<Utc>,
    ) -> Result<Episode> {
        self.insert_episode_with_access(episode, embedding, &FactAccess::default(), now)
            .await
    }

    async fn insert_episode_with_access(
        &self,
        episode: NewEpisode,
        embedding: Option<StoredEmbedding>,
        access: &FactAccess,
        now: DateTime<Utc>,
    ) -> Result<Episode> {
        let episode = Episode {
            id: new_id(),
            scope: episode.scope,
            session_id: episode.session_id,
            summary: episode.summary,
            topics: episode.topics,
            importance: episode.importance,
            created_at: now,
            recall_count: 0,
            last_recalled_at: None,
        };
        let mut inner = self.lock()?;
        inner
            .episode_access
            .insert(episode.id.clone(), access.clone());
        inner.episodes.push(EpisodeRecord {
            episode: episode.clone(),
            embedding,
        });
        Ok(episode)
    }

    async fn episode_access(
        &self,
        scope: &str,
        ids: &[String],
    ) -> Result<HashMap<String, FactAccess>> {
        let inner = self.lock()?;
        let wanted: HashSet<&String> = ids.iter().collect();
        Ok(inner
            .episodes
            .iter()
            .filter(|r| r.episode.scope == scope && wanted.contains(&r.episode.id))
            .map(|r| {
                let access = inner.episode_access.get(&r.episode.id).cloned();
                (r.episode.id.clone(), access.unwrap_or_default())
            })
            .collect())
    }

    async fn episodes(&self, scope: &str) -> Result<Vec<EpisodeRecord>> {
        Ok(self
            .lock()?
            .episodes
            .iter()
            .filter(|r| r.episode.scope == scope)
            .cloned()
            .collect())
    }

    async fn record_episode_recalls(&self, ids: &[String], now: DateTime<Utc>) -> Result<()> {
        let ids: HashSet<&String> = ids.iter().collect();
        for r in self
            .lock()?
            .episodes
            .iter_mut()
            .filter(|r| ids.contains(&r.episode.id))
        {
            r.episode.recall_count += 1;
            r.episode.last_recalled_at = Some(now);
        }
        Ok(())
    }

    async fn decay(&self, now: DateTime<Utc>, config: &RetentionConfig) -> Result<DecayReport> {
        let mut report = DecayReport::default();
        let mut inner = self.lock()?;
        if let Some(window) = config.superseded_retention {
            let cutoff = now - window;
            let before = inner.facts.len();
            inner
                .facts
                .retain(|r| r.fact.superseded_at.is_none_or(|at| at >= cutoff));
            report.superseded_deleted = (before - inner.facts.len()) as u64;
            let kept: HashSet<String> = inner.facts.iter().map(|r| r.fact.id.clone()).collect();
            inner.fact_access.retain(|id, _| kept.contains(id));
        }
        if let Some(window) = config.episode_retention {
            let cutoff = now - window;
            let before = inner.episodes.len();
            inner
                .episodes
                .retain(|r| r.episode.recall_count > 0 || r.episode.created_at >= cutoff);
            report.episodes_deleted = (before - inner.episodes.len()) as u64;
            let kept: HashSet<String> = inner
                .episodes
                .iter()
                .map(|r| r.episode.id.clone())
                .collect();
            inner.episode_access.retain(|id, _| kept.contains(id));
        }
        if let Some(window) = config.fact_recall_decay_after {
            let cutoff = now - window;
            for r in inner.facts.iter_mut().filter(|r| {
                r.fact.is_active()
                    && r.fact.recall_count > 0
                    && r.fact.last_recalled_at.is_none_or(|at| at < cutoff)
            }) {
                r.fact.recall_count = decayed_fact_recalls(r.fact.recall_count);
                report.facts_decayed += 1;
            }
        }
        if let Some(window) = config.episode_recall_decay_after {
            let cutoff = now - window;
            for r in inner.episodes.iter_mut().filter(|r| {
                r.episode.recall_count > 0
                    && r.episode.last_recalled_at.is_none_or(|at| at < cutoff)
            }) {
                r.episode.recall_count -= 1;
                report.episodes_decayed += 1;
            }
        }
        Ok(report)
    }

    async fn stale_embeddings(
        &self,
        model: &str,
        kind: RecordKind,
        after_id: Option<&str>,
        limit: usize,
    ) -> Result<Vec<StaleEmbedding>> {
        let inner = self.lock()?;
        let describe = |id: &str, text: String, embedding: &Option<StoredEmbedding>| {
            let current = embedding.as_ref().map(|e| e.model.clone());
            let stale = current.as_deref() != Some(model) && after_id.is_none_or(|a| id > a);
            stale.then(|| StaleEmbedding {
                kind,
                id: id.to_string(),
                text,
                model: current,
            })
        };
        let mut rows: Vec<StaleEmbedding> = match kind {
            RecordKind::Fact => inner
                .facts
                .iter()
                .filter(|r| r.fact.is_active())
                .filter_map(|r| describe(&r.fact.id, r.fact.search_text(), &r.embedding))
                .collect(),
            RecordKind::Episode => inner
                .episodes
                .iter()
                .filter_map(|r| describe(&r.episode.id, r.episode.search_text(), &r.embedding))
                .collect(),
        };
        rows.sort_by(|a, b| a.id.cmp(&b.id));
        rows.truncate(limit);
        Ok(rows)
    }

    async fn replace_embedding(
        &self,
        stale: &StaleEmbedding,
        embedding: StoredEmbedding,
    ) -> Result<bool> {
        let mut inner = self.lock()?;
        let slot = match stale.kind {
            RecordKind::Fact => inner
                .facts
                .iter_mut()
                .find(|r| r.fact.id == stale.id && r.fact.is_active())
                .map(|r| &mut r.embedding),
            RecordKind::Episode => inner
                .episodes
                .iter_mut()
                .find(|r| r.episode.id == stale.id)
                .map(|r| &mut r.embedding),
        };
        let Some(slot) = slot else {
            return Ok(false);
        };
        if slot.as_ref().map(|e| e.model.as_str()) != stale.model.as_deref() {
            return Ok(false);
        }
        *slot = Some(embedding);
        Ok(true)
    }

    async fn stats(&self) -> Result<MemoryStats> {
        let inner = self.lock()?;
        let mut scopes: HashSet<&str> = inner.facts.iter().map(|r| r.fact.scope.as_str()).collect();
        scopes.extend(inner.episodes.iter().map(|r| r.episode.scope.as_str()));
        let active = inner.facts.iter().filter(|r| r.fact.is_active()).count() as u64;
        Ok(MemoryStats {
            active_facts: active,
            superseded_facts: inner.facts.len() as u64 - active,
            episodes: inner.episodes.len() as u64,
            scopes: scopes.len() as u64,
        })
    }
}