engram-core 0.16.0

AI Memory Infrastructure - Persistent memory for AI agents with semantic search
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! LongMemEval benchmark — 5-dimension memory evaluation
//!
//! Evaluates memory quality across five dimensions:
//! 1. Information Retention — can we retrieve stored facts?
//! 2. Temporal Reasoning — can we answer time-based questions?
//! 3. Knowledge Update — does updating a fact reflect correctly?
//! 4. Multi-Hop Reasoning — can we chain facts across memories?
//! 5. Contradiction Detection — do we surface conflicting facts?

use std::collections::HashMap;
use std::time::Instant;

use super::{Benchmark, BenchmarkResult};
use crate::storage::queries::{create_memory, update_memory};
use crate::storage::Storage;
use crate::types::{CreateMemoryInput, MemoryType, StorageConfig, StorageMode, UpdateMemoryInput};

/// LongMemEval benchmark with configurable dimension weights
pub struct LongMemEvalBenchmark {
    /// Weights per dimension (will be normalized if they don't sum to 1.0)
    pub dimension_weights: HashMap<String, f64>,
}

impl Default for LongMemEvalBenchmark {
    fn default() -> Self {
        let mut weights = HashMap::new();
        weights.insert("information_retention".to_string(), 0.25);
        weights.insert("temporal_reasoning".to_string(), 0.20);
        weights.insert("knowledge_update".to_string(), 0.20);
        weights.insert("multi_hop".to_string(), 0.20);
        weights.insert("contradiction_detection".to_string(), 0.15);
        Self {
            dimension_weights: weights,
        }
    }
}

/// A single test case for keyword-retrieval dimensions
struct TestCase {
    setup_memories: Vec<String>,
    query_keyword: String,
    expected_content_substring: String,
}

impl LongMemEvalBenchmark {
    /// Evaluate information retention: store facts and retrieve them by keyword
    fn eval_information_retention(&self, storage: &Storage) -> f64 {
        let cases = vec![
            TestCase {
                setup_memories: vec![
                    "Alice is a software engineer at TechCorp".to_string(),
                    "Alice has 5 years of experience in Rust".to_string(),
                ],
                query_keyword: "Alice".to_string(),
                expected_content_substring: "engineer".to_string(),
            },
            TestCase {
                setup_memories: vec![
                    "The Eiffel Tower is located in Paris, France".to_string(),
                    "The Eiffel Tower was built in 1889".to_string(),
                ],
                query_keyword: "Eiffel".to_string(),
                expected_content_substring: "Paris".to_string(),
            },
            TestCase {
                setup_memories: vec!["Project Alpha deadline is Q3 2026".to_string()],
                query_keyword: "Alpha".to_string(),
                expected_content_substring: "Q3 2026".to_string(),
            },
        ];

        self.run_cases(storage, &cases)
    }

    /// Evaluate temporal reasoning: tag memories with dates and retrieve by time context
    fn eval_temporal_reasoning(&self, storage: &Storage) -> f64 {
        let cases = vec![
            TestCase {
                setup_memories: vec![
                    "Meeting on 2026-01-10: discussed Q1 roadmap".to_string(),
                    "Meeting on 2026-02-15: reviewed Q2 budget".to_string(),
                ],
                query_keyword: "2026-01".to_string(),
                expected_content_substring: "Q1 roadmap".to_string(),
            },
            TestCase {
                setup_memories: vec![
                    "Sprint 42 started on 2026-03-01".to_string(),
                    "Sprint 42 ended on 2026-03-14 with 12 story points".to_string(),
                ],
                query_keyword: "Sprint 42".to_string(),
                expected_content_substring: "story points".to_string(),
            },
        ];

        self.run_cases(storage, &cases)
    }

    /// Evaluate knowledge update: create a fact, update it, verify new value is retrievable
    fn eval_knowledge_update(&self, storage: &Storage) -> f64 {
        let mut correct = 0usize;
        let total = 3usize;

        // Test 1: Update memory content and verify retrieval
        let mem = storage
            .with_connection(|conn| {
                create_memory(
                    conn,
                    &CreateMemoryInput {
                        content: "Budget for Q1 is $50,000".to_string(),
                        memory_type: MemoryType::Note,
                        workspace: Some("longmemeval-bench".to_string()),
                        ..Default::default()
                    },
                )
            })
            .unwrap();

        let update = UpdateMemoryInput {
            content: Some("Budget for Q1 is $75,000 (revised)".to_string()),
            memory_type: None,
            tags: None,
            metadata: None,
            importance: None,
            scope: None,
            ttl_seconds: None,
            event_time: None,
            trigger_pattern: None,
        };
        let _ = storage.with_connection(|conn| update_memory(conn, mem.id, &update));

        let updated_content: Option<String> = storage
            .with_connection(|conn| {
                conn.query_row(
                    "SELECT content FROM memories WHERE id = ?1",
                    [mem.id],
                    |row| row.get(0),
                )
                .map_err(crate::error::EngramError::Database)
            })
            .ok();

        if let Some(c) = updated_content {
            if c.contains("$75,000") {
                correct += 1;
            }
        }

        // Test 2: Update service config timeout
        let tag_mem = storage
            .with_connection(|conn| {
                create_memory(
                    conn,
                    &CreateMemoryInput {
                        content: "Service config: timeout=30s".to_string(),
                        memory_type: MemoryType::Note,
                        workspace: Some("longmemeval-bench".to_string()),
                        ..Default::default()
                    },
                )
            })
            .unwrap();

        let update2 = UpdateMemoryInput {
            content: Some("Service config: timeout=60s (doubled for reliability)".to_string()),
            memory_type: None,
            tags: None,
            metadata: None,
            importance: None,
            scope: None,
            ttl_seconds: None,
            event_time: None,
            trigger_pattern: None,
        };
        let _ = storage.with_connection(|conn| update_memory(conn, tag_mem.id, &update2));

        let updated2: Option<String> = storage
            .with_connection(|conn| {
                conn.query_row(
                    "SELECT content FROM memories WHERE id = ?1",
                    [tag_mem.id],
                    |row| row.get(0),
                )
                .map_err(crate::error::EngramError::Database)
            })
            .ok();

        if let Some(c) = updated2 {
            if c.contains("timeout=60s") {
                correct += 1;
            }
        }

        // Test 3: Verify original value is replaced (not duplicated)
        let count: i64 = storage
            .with_connection(|conn| {
                conn.query_row(
                    "SELECT COUNT(*) FROM memories WHERE content LIKE '%timeout=30s%' AND id = ?1",
                    [tag_mem.id],
                    |row| row.get(0),
                )
                .map_err(crate::error::EngramError::Database)
            })
            .unwrap_or(1);

        if count == 0 {
            correct += 1;
        }

        correct as f64 / total as f64
    }

    /// Evaluate multi-hop: store chained facts, verify both are retrievable via linking keyword
    fn eval_multi_hop(&self, storage: &Storage) -> f64 {
        let cases = vec![
            TestCase {
                setup_memories: vec![
                    "Node A connects to Node B via link L1".to_string(),
                    "Node B connects to Node C via link L2".to_string(),
                ],
                query_keyword: "Node B".to_string(),
                expected_content_substring: "connects".to_string(),
            },
            TestCase {
                setup_memories: vec![
                    "Company Acme acquired Startup X in 2024".to_string(),
                    "Startup X built the Zephyr product".to_string(),
                    "Zephyr product has 50,000 active users".to_string(),
                ],
                query_keyword: "Zephyr".to_string(),
                expected_content_substring: "users".to_string(),
            },
        ];

        self.run_cases(storage, &cases)
    }

    /// Evaluate contradiction detection: store conflicting facts, verify both appear
    fn eval_contradiction_detection(&self, storage: &Storage) -> f64 {
        let mut correct = 0usize;
        let total = 2usize;

        let pairs = [
            (
                "Server capacity is 100 concurrent users (from 2025-01 report)",
                "Server capacity is 500 concurrent users (from 2026-01 report)",
                "concurrent users",
            ),
            (
                "API rate limit is 100 req/min per client",
                "API rate limit is 1000 req/min per client (updated)",
                "rate limit",
            ),
        ];

        for (fact_a, fact_b, keyword) in &pairs {
            let _ = storage.with_connection(|conn| {
                create_memory(
                    conn,
                    &CreateMemoryInput {
                        content: fact_a.to_string(),
                        memory_type: MemoryType::Note,
                        workspace: Some("longmemeval-bench".to_string()),
                        ..Default::default()
                    },
                )
            });
            let _ = storage.with_connection(|conn| {
                create_memory(
                    conn,
                    &CreateMemoryInput {
                        content: fact_b.to_string(),
                        memory_type: MemoryType::Note,
                        workspace: Some("longmemeval-bench".to_string()),
                        ..Default::default()
                    },
                )
            });

            let count: i64 = storage
                .with_connection(|conn| {
                    conn.query_row(
                        "SELECT COUNT(*) FROM memories WHERE content LIKE ?1",
                        [format!("%{}%", keyword)],
                        |row| row.get(0),
                    )
                    .map_err(crate::error::EngramError::Database)
                })
                .unwrap_or(0);

            if count >= 2 {
                correct += 1;
            }
        }

        correct as f64 / total as f64
    }

    /// Helper: store memories for each case and check if expected content is retrievable
    fn run_cases(&self, storage: &Storage, cases: &[TestCase]) -> f64 {
        if cases.is_empty() {
            return 1.0;
        }

        let mut correct = 0usize;

        for case in cases {
            for content in &case.setup_memories {
                let _ = storage.with_connection(|conn| {
                    create_memory(
                        conn,
                        &CreateMemoryInput {
                            content: content.clone(),
                            memory_type: MemoryType::Note,
                            workspace: Some("longmemeval-bench".to_string()),
                            ..Default::default()
                        },
                    )
                });
            }

            let retrieved: Option<String> = storage
                .with_connection(|conn| {
                    conn.query_row(
                        "SELECT content FROM memories WHERE content LIKE ?1 LIMIT 1",
                        [format!("%{}%", case.query_keyword)],
                        |row| row.get(0),
                    )
                    .map_err(crate::error::EngramError::Database)
                })
                .ok();

            if let Some(content) = retrieved {
                if content.contains(&case.expected_content_substring) {
                    correct += 1;
                }
            }
        }

        correct as f64 / cases.len() as f64
    }

    /// Compute weighted score across dimensions
    fn weighted_score(&self, scores: &HashMap<String, f64>) -> f64 {
        let total_weight: f64 = self.dimension_weights.values().sum();
        if total_weight == 0.0 {
            return 0.0;
        }

        self.dimension_weights
            .iter()
            .filter_map(|(dim, &weight)| scores.get(dim).map(|&score| score * weight))
            .sum::<f64>()
            / total_weight
    }
}

impl Benchmark for LongMemEvalBenchmark {
    fn name(&self) -> &str {
        "longmemeval"
    }

    fn description(&self) -> &str {
        "5-dimension memory evaluation benchmark: information retention, temporal reasoning, \
         knowledge update, multi-hop reasoning, and contradiction detection."
    }

    fn run(&self, db_path: &str) -> Result<BenchmarkResult, Box<dyn std::error::Error>> {
        let start = Instant::now();

        let storage = if db_path == ":memory:" {
            Storage::open_in_memory()?
        } else {
            let bench_path = format!("{}.longmemeval_bench.db", db_path);
            Storage::open(StorageConfig {
                db_path: bench_path,
                storage_mode: StorageMode::Local,
                cloud_uri: None,
                encrypt_cloud: false,
                confidence_half_life_days: 30.0,
                auto_sync: false,
                sync_debounce_ms: 5000,
            })?
        };

        let retention = self.eval_information_retention(&storage);
        let temporal = self.eval_temporal_reasoning(&storage);
        let knowledge_update = self.eval_knowledge_update(&storage);
        let multi_hop = self.eval_multi_hop(&storage);
        let contradiction = self.eval_contradiction_detection(&storage);

        let mut dimension_scores = HashMap::new();
        dimension_scores.insert("information_retention".to_string(), retention);
        dimension_scores.insert("temporal_reasoning".to_string(), temporal);
        dimension_scores.insert("knowledge_update".to_string(), knowledge_update);
        dimension_scores.insert("multi_hop".to_string(), multi_hop);
        dimension_scores.insert("contradiction_detection".to_string(), contradiction);

        let weighted = self.weighted_score(&dimension_scores);

        let duration_ms = start.elapsed().as_millis() as u64;

        let mut metrics = dimension_scores;
        metrics.insert("weighted_score".to_string(), weighted);

        // Clean up temporary file
        if db_path != ":memory:" {
            let bench_path = format!("{}.longmemeval_bench.db", db_path);
            drop(storage);
            let _ = std::fs::remove_file(&bench_path);
            let _ = std::fs::remove_file(format!("{}-wal", bench_path));
            let _ = std::fs::remove_file(format!("{}-shm", bench_path));
        }

        Ok(BenchmarkResult {
            name: self.name().to_string(),
            metrics,
            duration_ms,
            timestamp: chrono::Utc::now().to_rfc3339(),
        })
    }
}

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

    #[test]
    fn test_longmemeval_runs() {
        let bench = LongMemEvalBenchmark::default();
        let result = bench.run(":memory:").expect("benchmark should succeed");
        assert_eq!(result.name, "longmemeval");
    }

    #[test]
    fn test_all_dimensions_present() {
        let bench = LongMemEvalBenchmark::default();
        let result = bench.run(":memory:").expect("benchmark should succeed");

        let expected_dims = [
            "information_retention",
            "temporal_reasoning",
            "knowledge_update",
            "multi_hop",
            "contradiction_detection",
            "weighted_score",
        ];
        for dim in &expected_dims {
            assert!(
                result.metrics.contains_key(*dim),
                "missing dimension: {}",
                dim
            );
        }
    }

    #[test]
    fn test_scores_in_range() {
        let bench = LongMemEvalBenchmark::default();
        let result = bench.run(":memory:").expect("benchmark should succeed");

        for (key, value) in &result.metrics {
            assert!(
                (0.0..=1.0).contains(value),
                "metric '{}' = {} out of range [0,1]",
                key,
                value
            );
        }
    }

    #[test]
    fn test_weighted_score_with_custom_weights() {
        let mut weights = HashMap::new();
        weights.insert("information_retention".to_string(), 1.0);
        weights.insert("temporal_reasoning".to_string(), 0.0);
        weights.insert("knowledge_update".to_string(), 0.0);
        weights.insert("multi_hop".to_string(), 0.0);
        weights.insert("contradiction_detection".to_string(), 0.0);

        let bench = LongMemEvalBenchmark {
            dimension_weights: weights,
        };
        let result = bench.run(":memory:").expect("benchmark should succeed");
        let retention = result.metrics["information_retention"];
        let weighted = result.metrics["weighted_score"];
        assert!(
            (weighted - retention).abs() < 1e-9,
            "weighted={} retention={}",
            weighted,
            retention
        );
    }
}