aidaemon-memory 0.28.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Retrieval scoring: the replaceable [`Scorer`] and its default,
//! [`HybridScorer`] (port of the daemon's `memory/hybrid.rs`, the fact
//! thresholds in `state/sqlite/facts.rs`, and `memory/scoring.rs`).

use std::collections::HashSet;

use chrono::{DateTime, Utc};

use crate::types::{Episode, Fact, Score, Signals};

/// Weights of the fused score. Defaults are the daemon's calibrated values
/// and sum to 1.0.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HybridWeights {
    pub semantic: f32,
    pub lexical: f32,
    /// Entity-graph match, from [`crate::MemoryStore::fact_graph_scores`].
    /// Stays 0 for a store with no graph.
    pub graph: f32,
    pub freshness: f32,
    pub confidence: f32,
    pub provenance: f32,
}

impl Default for HybridWeights {
    fn default() -> Self {
        Self {
            semantic: 0.45,
            lexical: 0.30,
            graph: 0.10,
            freshness: 0.10,
            confidence: 0.03,
            provenance: 0.02,
        }
    }
}

impl HybridWeights {
    /// Fuse independently bounded signals; inputs are clamped to 0..=1.
    pub fn fuse(&self, s: &Signals) -> f32 {
        self.semantic * s.semantic.clamp(0.0, 1.0)
            + self.lexical * s.lexical.clamp(0.0, 1.0)
            + self.graph * s.graph.clamp(0.0, 1.0)
            + self.freshness * s.freshness.clamp(0.0, 1.0)
            + self.confidence * s.confidence.clamp(0.0, 1.0)
            + self.provenance * s.provenance.clamp(0.0, 1.0)
    }
}

/// Tunables for [`HybridScorer`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ScoringConfig {
    pub weights: HybridWeights,
    /// A candidate is kept when its semantic similarity is above this...
    pub semantic_min: f32,
    /// ...or its lexical score is above this. Both 0.3 in the daemon. The
    /// freshness/confidence/provenance signals only order candidates; they
    /// never let an irrelevant one through.
    pub lexical_min: f32,
    /// Freshness falls linearly from 1 to 0 over this window (7 days).
    pub freshness_window: chrono::Duration,
    /// Provenance signal for a fact with / without a supporting excerpt.
    pub provenance_with_excerpt: f32,
    pub provenance_without_excerpt: f32,
    /// Drop candidates whose fused score is at or below this (default 0).
    pub min_score: f32,
}

impl Default for ScoringConfig {
    fn default() -> Self {
        Self {
            weights: HybridWeights::default(),
            semantic_min: 0.3,
            lexical_min: 0.3,
            freshness_window: chrono::Duration::days(7),
            provenance_with_excerpt: 1.0,
            provenance_without_excerpt: 0.3,
            min_score: 0.0,
        }
    }
}

/// Turns retrieval signals into a ranking score, or drops the candidate.
///
/// [`crate::Memory`] gathers the raw signals (cosine similarity from the
/// embedder, the store's lexical score) and asks the scorer. Replace it to
/// change ranking without touching storage; a store that ranks on its own
/// can bypass scoring entirely through [`crate::MemoryStore::search_facts`].
pub trait Scorer: Send + Sync {
    /// `semantic` is `None` when either side has no usable embedding.
    /// Return `None` to drop the fact.
    fn score_fact(
        &self,
        fact: &Fact,
        semantic: Option<f32>,
        lexical: f32,
        now: DateTime<Utc>,
    ) -> Option<Score>;

    /// [`Scorer::score_fact`] with the fact's graph relation to the query's
    /// other hits (0.0-1.0). The default ignores it, so a scorer written
    /// before this signal existed keeps ranking as it did.
    fn score_fact_with_graph(
        &self,
        fact: &Fact,
        semantic: Option<f32>,
        lexical: f32,
        _graph: f32,
        now: DateTime<Utc>,
    ) -> Option<Score> {
        self.score_fact(fact, semantic, lexical, now)
    }

    fn score_episode(
        &self,
        episode: &Episode,
        semantic: Option<f32>,
        lexical: f32,
        now: DateTime<Utc>,
    ) -> Option<Score>;
}

/// The daemon's hybrid ranking: weighted semantic + lexical + graph +
/// freshness + confidence + provenance, gated on relevance thresholds.
#[derive(Debug, Clone, Default)]
pub struct HybridScorer {
    pub config: ScoringConfig,
}

impl HybridScorer {
    pub fn new(config: ScoringConfig) -> Self {
        Self { config }
    }

    /// 1.0 when just updated, 0.0 at or beyond the freshness window.
    pub fn freshness(&self, now: DateTime<Utc>, updated_at: DateTime<Utc>) -> f32 {
        let window = self.config.freshness_window.num_seconds().max(1) as f32;
        let age = (now - updated_at).num_seconds().max(0) as f32;
        (1.0 - age / window).clamp(0.0, 1.0)
    }

    fn relevant(&self, semantic: Option<f32>, lexical: f32) -> bool {
        semantic.is_some_and(|s| s > self.config.semantic_min) || lexical > self.config.lexical_min
    }
}

impl Scorer for HybridScorer {
    fn score_fact(
        &self,
        fact: &Fact,
        semantic: Option<f32>,
        lexical: f32,
        now: DateTime<Utc>,
    ) -> Option<Score> {
        self.score_fact_with_graph(fact, semantic, lexical, 0.0, now)
    }

    /// The graph signal orders relevant facts; like freshness, it never lets
    /// an irrelevant one through.
    fn score_fact_with_graph(
        &self,
        fact: &Fact,
        semantic: Option<f32>,
        lexical: f32,
        graph: f32,
        now: DateTime<Utc>,
    ) -> Option<Score> {
        if !self.relevant(semantic, lexical) {
            return None;
        }
        let signals = Signals {
            semantic: semantic.unwrap_or(0.0).clamp(0.0, 1.0),
            lexical: lexical.clamp(0.0, 1.0),
            graph: graph.clamp(0.0, 1.0),
            freshness: self.freshness(now, fact.updated_at),
            confidence: fact.confidence.clamp(0.0, 1.0),
            provenance: if fact.source.excerpt.is_some() {
                self.config.provenance_with_excerpt
            } else {
                self.config.provenance_without_excerpt
            },
        };
        let value = self.config.weights.fuse(&signals);
        (value > self.config.min_score).then_some(Score { value, signals })
    }

    /// Episodes rank on relevance plus a small importance term, boosted by
    /// recall count. Age is deliberately absent (daemon `scoring.rs`): an
    /// old conversation must stay discoverable.
    fn score_episode(
        &self,
        episode: &Episode,
        semantic: Option<f32>,
        lexical: f32,
        _now: DateTime<Utc>,
    ) -> Option<Score> {
        if !self.relevant(semantic, lexical) {
            return None;
        }
        let signals = Signals {
            semantic: semantic.unwrap_or(0.0).clamp(0.0, 1.0),
            lexical: lexical.clamp(0.0, 1.0),
            confidence: episode.importance.clamp(0.0, 1.0),
            provenance: 1.0,
            ..Signals::default()
        };
        let value = self.config.weights.fuse(&signals) * episode_recall_boost(episode.recall_count);
        (value > self.config.min_score).then_some(Score { value, signals })
    }
}

/// Bounded reinforcement for often-recalled episodes: at most +16%.
pub fn episode_recall_boost(recall_count: u32) -> f32 {
    1.0 + ((recall_count as f32 + 1.0).ln() * 0.04).min(0.16)
}

/// Episode importance from typed session features (port of the daemon's
/// `calculate_episode_importance`). Base 0.5, clamped to 0.1..=1.0.
pub fn episode_importance(
    message_count: usize,
    has_errors: bool,
    has_decisions: bool,
    has_goals: bool,
    emotional_intensity: f32,
) -> f32 {
    let mut importance = 0.5;
    if message_count > 20 {
        importance += 0.2;
    } else if message_count > 10 {
        importance += 0.1;
    }
    if has_errors {
        importance += 0.15;
    }
    if has_decisions {
        importance += 0.1;
    }
    if has_goals {
        importance += 0.1;
    }
    let intensity = if emotional_intensity.is_finite() {
        emotional_intensity.clamp(0.0, 1.0)
    } else {
        0.0
    };
    importance += intensity * 0.2;
    importance.clamp(0.1, 1.0)
}

/// Function words that carry no retrieval signal. Used only to tokenize
/// for lexical overlap and hashing, never to decide what is remembered.
pub(crate) fn is_stopword(token: &str) -> bool {
    matches!(
        token,
        "a" | "an"
            | "and"
            | "are"
            | "as"
            | "at"
            | "be"
            | "did"
            | "do"
            | "does"
            | "for"
            | "from"
            | "how"
            | "i"
            | "in"
            | "is"
            | "it"
            | "me"
            | "my"
            | "of"
            | "on"
            | "or"
            | "please"
            | "tell"
            | "that"
            | "the"
            | "to"
            | "was"
            | "we"
            | "were"
            | "what"
            | "when"
            | "where"
            | "who"
            | "why"
            | "you"
            | "your"
    )
}

/// Lowercase alphanumeric words, stopwords removed. Splits on `_` too, so
/// `favorite_color` yields `favorite` and `color`.
pub fn tokens(text: &str) -> Vec<String> {
    text.to_lowercase()
        .split(|c: char| !c.is_alphanumeric())
        .filter(|t| !t.is_empty() && !is_stopword(t))
        .map(str::to_owned)
        .collect()
}

/// Fraction of distinct query tokens that occur as words in `text`
/// (0.0-1.0). The in-memory store's lexical signal.
pub fn term_overlap(query: &str, text: &str) -> f32 {
    let query: HashSet<String> = tokens(query).into_iter().collect();
    if query.is_empty() {
        return 0.0;
    }
    let text: HashSet<String> = tokens(text).into_iter().collect();
    query.intersection(&text).count() as f32 / query.len() as f32
}

/// Distinct words two texts share over the words they have between them
/// (0.0-1.0), stopwords removed.
pub fn text_similarity(a: &str, b: &str) -> f32 {
    let a: HashSet<String> = tokens(a).into_iter().collect();
    let b: HashSet<String> = tokens(b).into_iter().collect();
    let total = a.union(&b).count();
    if total == 0 {
        return 0.0;
    }
    a.intersection(&b).count() as f32 / total as f32
}

/// A conversation turn's importance (0.1-1.0) from structure only: its role
/// (system prompts are not memories; tool output counts less than what the
/// user says) and its length. Nothing is inferred from the words used;
/// deciding what is durable is the extraction model's job.
pub fn turn_importance(role: &str, content: Option<&str>) -> f32 {
    let base: f32 = match role {
        "system" => return 0.0,
        "tool" => 0.3,
        "assistant" => 0.5,
        "user" => 0.6,
        _ => 0.5,
    };
    let length = content.map_or(0, str::len);
    let adjust = if length > 200 {
        0.2
    } else if length < 20 {
        -0.2
    } else {
        0.0
    };
    (base + adjust).clamp(0.1, 1.0)
}

/// Fraction of distinct query words that occur as words in `text`
/// (0.0-1.0), counting every word, stopwords included. Unlike
/// [`term_overlap`], a short query such as "who is she" keeps all its words:
/// the signal for ranking when no embedder is available.
pub fn word_overlap(query: &str, text: &str) -> f32 {
    let words = |value: &str| {
        value
            .to_lowercase()
            .split(|c: char| !c.is_alphanumeric())
            .filter(|word| !word.is_empty())
            .map(str::to_owned)
            .collect::<HashSet<_>>()
    };
    let query = words(query);
    if query.is_empty() {
        return 0.0;
    }
    query.intersection(&words(text)).count() as f32 / query.len() as f32
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn turn_importance_reads_role_and_length_only() {
        assert_eq!(turn_importance("system", Some("anything")), 0.0);
        assert!(
            turn_importance("user", Some(&"a".repeat(300)))
                > turn_importance("user", Some("a medium length message"))
        );
        assert!(
            turn_importance("user", Some("ok"))
                < turn_importance("user", Some("a medium length message"))
        );
        assert!(
            turn_importance("tool", Some("output of some length"))
                < turn_importance("user", Some("output of some length"))
        );
        // Wording does not matter, only structure.
        assert_eq!(
            turn_importance("user", Some("please remember this one")),
            turn_importance("user", Some("the weather is mild today"))
        );
    }

    #[test]
    fn word_overlap_counts_every_word() {
        assert_eq!(word_overlap("who is she", "she is here"), 2.0 / 3.0);
        assert_eq!(word_overlap("", "anything"), 0.0);
        assert_eq!(word_overlap("Mia", "mia and zoe"), 1.0);
    }

    #[test]
    fn corroborated_result_beats_single_weak_signal() {
        let w = HybridWeights::default();
        let corroborated = w.fuse(&Signals {
            semantic: 0.55,
            lexical: 0.8,
            graph: 1.0,
            confidence: 1.0,
            provenance: 1.0,
            ..Default::default()
        });
        let vector_only = w.fuse(&Signals {
            semantic: 0.7,
            ..Default::default()
        });
        assert!(corroborated > vector_only);
        let max = w.fuse(&Signals {
            semantic: 2.0,
            lexical: 2.0,
            graph: 2.0,
            freshness: 2.0,
            confidence: 2.0,
            provenance: 2.0,
        });
        assert!((max - 1.0).abs() < 1e-6);
    }

    #[test]
    fn importance_and_boost_are_bounded() {
        assert_eq!(episode_importance(3, false, false, false, 0.0), 0.5);
        assert_eq!(episode_importance(30, true, true, true, 1.0), 1.0);
        assert!(episode_importance(12, true, false, false, 0.0) > 0.7);
        assert_eq!(episode_recall_boost(0), 1.0);
        assert!(episode_recall_boost(1_000_000) <= 1.16 + 1e-6);
    }

    #[test]
    fn overlap_counts_words_not_substrings() {
        assert_eq!(term_overlap("dog", "dodger stadium"), 0.0);
        assert_eq!(
            term_overlap("favorite color", "preference favorite_color: blue"),
            1.0
        );
        assert_eq!(term_overlap("the", "the"), 0.0);
    }
}