engramai 0.2.3

Neuroscience-grounded memory system for AI agents. ACT-R activation, Hebbian learning, Ebbinghaus forgetting, cognitive consolidation.
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
449
450
451
//! Link formation for multi-signal Hebbian association discovery.
//!
//! Combines signal scores, filters by threshold, enforces link budget,
//! and persists associations to the database.

use crate::association::signals::SignalComputer;
use crate::config::AssociationConfig;
use crate::storage::Storage;

/// A candidate link before persistence.
#[derive(Debug)]
struct ProtoLink {
    target_id: String,
    strength: f64,
    combined_score: f64,
    signal_source: String,
    signal_detail: String,
}

/// Orchestrates link formation for a newly stored memory.
pub struct LinkFormer<'a> {
    storage: &'a Storage,
}

impl<'a> LinkFormer<'a> {
    pub fn new(storage: &'a Storage) -> Self {
        Self { storage }
    }

    /// Discover associations for a newly stored memory.
    ///
    /// For each candidate memory ID:
    /// 1. Fetch candidate's entities, embedding, and timestamp from storage
    /// 2. Compute signal scores
    /// 3. Compute weighted combined score
    /// 4. Filter by `config.link_threshold`
    /// 5. Sort by combined score descending, take top `config.max_links_per_memory`
    /// 6. Persist each link via `storage.record_association()`
    ///
    /// Returns the number of newly created links.
    #[allow(clippy::too_many_arguments)]
    pub fn discover_associations(
        &self,
        new_memory_id: &str,
        candidates: Vec<String>,
        new_entities: &[String],
        new_embedding: Option<&[f32]>,
        new_timestamp: f64,
        config: &AssociationConfig,
        namespace: &str,
    ) -> Result<usize, rusqlite::Error> {
        let mut proto_links: Vec<ProtoLink> = Vec::new();

        for candidate_id in &candidates {
            // Fetch candidate data from storage
            let cand_entities = self.storage.get_entities_for_memory(candidate_id)?;
            let cand_embedding = self.storage.get_embedding_for_memory(candidate_id)?;
            let cand_timestamp = match self.storage.get_memory_timestamp(candidate_id)? {
                Some(ts) => ts,
                None => continue, // Memory doesn't exist, skip
            };

            // Compute all signals
            let scores = SignalComputer::compute_all(
                new_entities,
                &cand_entities,
                new_embedding,
                cand_embedding.as_deref(),
                new_timestamp,
                cand_timestamp,
            );

            // Compute weighted combined score
            let combined = scores.combined(config);

            // Filter by threshold
            if combined >= config.link_threshold {
                let signal_source = scores.signal_source(0.2).to_string();
                proto_links.push(ProtoLink {
                    target_id: candidate_id.clone(),
                    strength: config.initial_strength,
                    combined_score: combined,
                    signal_source,
                    signal_detail: scores.to_json(),
                });
            }
        }

        // Sort by combined score descending
        proto_links.sort_by(|a, b| {
            b.combined_score
                .partial_cmp(&a.combined_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Take top-K
        proto_links.truncate(config.max_links_per_memory);

        // Persist links and count new creations
        let mut created = 0;
        for link in &proto_links {
            let is_new = self.storage.record_association(
                new_memory_id,
                &link.target_id,
                link.strength,
                &link.signal_source,
                &link.signal_detail,
                namespace,
            )?;
            if is_new {
                created += 1;
            }
        }

        Ok(created)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::AssociationConfig;
    use crate::types::{MemoryLayer, MemoryRecord, MemoryType};
    use chrono::Utc;

    fn test_storage() -> Storage {
        Storage::new(":memory:").expect("in-memory storage")
    }

    fn make_record(id: &str, content: &str, created_at: chrono::DateTime<Utc>) -> MemoryRecord {
        MemoryRecord {
            id: id.to_string(),
            content: content.to_string(),
            memory_type: MemoryType::Factual,
            layer: MemoryLayer::Working,
            created_at,
            access_times: vec![created_at],
            working_strength: 1.0,
            core_strength: 0.0,
            importance: 0.5,
            pinned: false,
            consolidation_count: 0,
            last_consolidated: None,
            source: String::new(),
            contradicts: None,
            contradicted_by: None,
            superseded_by: None,
            metadata: None,
        }
    }

    fn store_memory_with_entities(
        storage: &mut Storage,
        id: &str,
        content: &str,
        entities: &[&str],
        timestamp: chrono::DateTime<Utc>,
    ) {
        let record = make_record(id, content, timestamp);
        storage.add(&record, "default").unwrap();

        // Store entities: create entity records and link them to the memory
        for entity_name in entities {
            // Use the storage's store_entities method if available,
            // or manually insert into entities + memory_entities tables
            let entity_id = format!("ent_{}", entity_name.to_lowercase().replace(' ', "_"));
            let now_ts = timestamp.timestamp() as f64;
            storage
                .connection()
                .execute(
                    "INSERT OR IGNORE INTO entities (id, name, entity_type, namespace, created_at, updated_at) \
                     VALUES (?1, ?2, 'concept', 'default', ?3, ?3)",
                    rusqlite::params![entity_id, entity_name, now_ts],
                )
                .unwrap();
            storage
                .connection()
                .execute(
                    "INSERT OR IGNORE INTO memory_entities (memory_id, entity_id, role) \
                     VALUES (?1, ?2, 'mention')",
                    rusqlite::params![id, entity_id],
                )
                .unwrap();
        }
    }

    #[allow(dead_code)]
    fn store_memory_with_embedding(
        storage: &mut Storage,
        id: &str,
        content: &str,
        embedding: &[f32],
        timestamp: chrono::DateTime<Utc>,
    ) {
        let record = make_record(id, content, timestamp);
        storage.add(&record, "default").unwrap();

        // Store embedding as BLOB
        let blob: Vec<u8> = embedding
            .iter()
            .flat_map(|f| f.to_le_bytes())
            .collect();
        let dims = embedding.len() as i64;
        storage
            .connection()
            .execute(
                "INSERT INTO memory_embeddings (memory_id, model, embedding, dimensions, created_at) \
                 VALUES (?1, 'test/model', ?2, ?3, ?4)",
                rusqlite::params![id, blob, dims, chrono::Utc::now().to_rfc3339()],
            )
            .unwrap();
    }

    #[test]
    fn test_discover_no_candidates() {
        let storage = test_storage();
        let former = LinkFormer::new(&storage);
        let config = AssociationConfig::default();

        let created = former
            .discover_associations(
                "new_mem",
                vec![],
                &["cat".to_string()],
                None,
                1700000000.0,
                &config,
                "default",
            )
            .unwrap();

        assert_eq!(created, 0, "no candidates should produce 0 links");
    }

    #[test]
    fn test_discover_below_threshold() {
        let mut storage = test_storage();
        let now = Utc::now();
        let thirty_days_ago = now - chrono::Duration::days(30);

        // Create a candidate with no entity overlap, no embedding, distant time
        let record = make_record("cand1", "totally unrelated memory", thirty_days_ago);
        storage.add(&record, "default").unwrap();

        // Create the new memory record too (for FK constraints if needed later)
        let new_record = make_record("new_mem", "a new memory", now);
        storage.add(&new_record, "default").unwrap();

        let former = LinkFormer::new(&storage);
        let mut config = AssociationConfig::default();
        config.link_threshold = 0.4;

        let created = former
            .discover_associations(
                "new_mem",
                vec!["cand1".to_string()],
                &["cat".to_string(), "dog".to_string()],
                None,
                now.timestamp() as f64,
                &config,
                "default",
            )
            .unwrap();

        assert_eq!(created, 0, "candidates below threshold should produce 0 links");
    }

    #[test]
    fn test_discover_creates_links() {
        let mut storage = test_storage();
        let now = Utc::now();

        // Create new memory
        let new_record = make_record("new_mem", "memory about cats and dogs", now);
        storage.add(&new_record, "default").unwrap();

        // Create candidate with entity overlap and same timestamp
        store_memory_with_entities(
            &mut storage,
            "cand1",
            "another memory about cats",
            &["cat", "fish"],
            now,
        );

        // New memory's entities include "cat" — overlap with cand1
        let new_entities = vec!["cat".to_string(), "dog".to_string()];

        let former = LinkFormer::new(&storage);
        let mut config = AssociationConfig::default();
        // Lower threshold to make it easier to create links
        // temporal_proximity at same time = 1.0, w_temporal = 0.2 → contributes 0.2
        // entity jaccard = 1/3 ≈ 0.333, w_entity = 0.3 → contributes 0.1
        // total ≈ 0.3, so set threshold below that
        config.link_threshold = 0.2;

        let created = former
            .discover_associations(
                "new_mem",
                vec!["cand1".to_string()],
                &new_entities,
                None,
                now.timestamp() as f64,
                &config,
                "default",
            )
            .unwrap();

        assert!(created >= 1, "should create at least 1 link, got {}", created);

        // Verify link exists in DB
        let count: i64 = storage
            .connection()
            .query_row(
                "SELECT COUNT(*) FROM hebbian_links WHERE \
                 (source_id = 'new_mem' AND target_id = 'cand1') OR \
                 (source_id = 'cand1' AND target_id = 'new_mem')",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(count, 1, "exactly one link should exist");
    }

    #[test]
    fn test_discover_respects_max_links() {
        let mut storage = test_storage();
        let now = Utc::now();

        // Create new memory
        let new_record = make_record("new_mem", "memory about animals", now);
        storage.add(&new_record, "default").unwrap();

        // Create 10 candidates, all with entity overlap
        for i in 0..10 {
            let id = format!("cand{}", i);
            store_memory_with_entities(
                &mut storage,
                &id,
                &format!("candidate {} about animals", i),
                &["animal"],
                now,
            );
        }

        let new_entities = vec!["animal".to_string()];
        let candidates: Vec<String> = (0..10).map(|i| format!("cand{}", i)).collect();

        let former = LinkFormer::new(&storage);
        let mut config = AssociationConfig::default();
        config.link_threshold = 0.1; // low threshold so all pass
        config.max_links_per_memory = 3; // but only keep top 3

        let created = former
            .discover_associations(
                "new_mem",
                candidates,
                &new_entities,
                None,
                now.timestamp() as f64,
                &config,
                "default",
            )
            .unwrap();

        assert_eq!(created, 3, "should create exactly max_links_per_memory links");

        // Verify count in DB
        let count: i64 = storage
            .connection()
            .query_row(
                "SELECT COUNT(*) FROM hebbian_links WHERE source_id = 'new_mem'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(count, 3);
    }

    #[test]
    fn test_discover_link_metadata() {
        let mut storage = test_storage();
        let now = Utc::now();

        // Create new memory
        let new_record = make_record("new_mem", "memory about cats", now);
        storage.add(&new_record, "default").unwrap();

        // Create candidate with strong entity overlap and same timestamp
        store_memory_with_entities(
            &mut storage,
            "cand1",
            "another memory about cats",
            &["cat"],
            now,
        );

        let new_entities = vec!["cat".to_string()];

        let former = LinkFormer::new(&storage);
        let mut config = AssociationConfig::default();
        config.link_threshold = 0.1;

        let created = former
            .discover_associations(
                "new_mem",
                vec!["cand1".to_string()],
                &new_entities,
                None,
                now.timestamp() as f64,
                &config,
                "default",
            )
            .unwrap();

        assert_eq!(created, 1);

        // Verify signal_source and signal_detail are stored
        let (signal_source, signal_detail): (String, String) = storage
            .connection()
            .query_row(
                "SELECT signal_source, signal_detail FROM hebbian_links \
                 WHERE source_id = 'new_mem' AND target_id = 'cand1'",
                [],
                |row| Ok((row.get(0)?, row.get(1)?)),
            )
            .unwrap();

        // signal_source should be a valid value
        assert!(
            ["entity", "embedding", "temporal", "multi"].contains(&signal_source.as_str()),
            "signal_source should be valid, got: {}",
            signal_source
        );

        // signal_detail should be valid JSON with all three fields
        let detail: serde_json::Value = serde_json::from_str(&signal_detail)
            .expect("signal_detail should be valid JSON");
        assert!(detail["entity_overlap"].is_number(), "should have entity_overlap");
        assert!(detail["embedding_cosine"].is_number(), "should have embedding_cosine");
        assert!(detail["temporal_proximity"].is_number(), "should have temporal_proximity");

        // Entity overlap should be 1.0 (identical entity set: both have only "cat")
        let entity_overlap = detail["entity_overlap"].as_f64().unwrap();
        assert!(
            (entity_overlap - 1.0).abs() < 1e-6,
            "entity_overlap should be 1.0 for identical entities, got {}",
            entity_overlap
        );
    }
}