mnemefusion-core 0.1.4

Unified memory engine for AI applications - Core library
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! Temporal index for time-based memory queries
//!
//! This module provides efficient time-range queries using redb's native B-tree ordering.
//! Memories are indexed by their timestamp, allowing fast range queries and "recent N" lookups.

use crate::{
    storage::StorageEngine,
    types::{MemoryId, Timestamp},
    Result,
};
use redb::ReadableTable;
use std::sync::Arc;

/// Result from a temporal query
#[derive(Debug, Clone)]
pub struct TemporalResult {
    /// The memory ID
    pub id: MemoryId,
    /// The timestamp of the memory
    pub timestamp: Timestamp,
}

/// Temporal index for time-based queries
///
/// Uses redb's native B-tree ordering for efficient range queries.
/// The TEMPORAL_INDEX table maps timestamp (u64) → memory_id (bytes).
pub struct TemporalIndex {
    storage: Arc<StorageEngine>,
}

impl TemporalIndex {
    /// Create a new temporal index
    ///
    /// # Arguments
    ///
    /// * `storage` - The storage engine containing the TEMPORAL_INDEX table
    pub fn new(storage: Arc<StorageEngine>) -> Self {
        Self { storage }
    }

    /// Query memories within a time range
    ///
    /// Returns memories whose timestamps fall within [start, end] (inclusive),
    /// sorted by timestamp in descending order (newest first).
    ///
    /// # Arguments
    ///
    /// * `start` - Start of the time range (inclusive)
    /// * `end` - End of the time range (inclusive)
    /// * `limit` - Maximum number of results to return
    ///
    /// # Returns
    ///
    /// A vector of TemporalResult, sorted newest first
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use mnemefusion_core::{index::TemporalIndex, Timestamp};
    /// # use std::sync::Arc;
    /// # let storage = Arc::new(mnemefusion_core::storage::StorageEngine::open("test.mfdb").unwrap());
    /// let temporal = TemporalIndex::new(storage);
    /// let now = Timestamp::now();
    /// let week_ago = now.subtract_days(7);
    ///
    /// let results = temporal.range_query(week_ago, now, 100).unwrap();
    /// println!("Found {} memories from the past week", results.len());
    /// ```
    pub fn range_query(
        &self,
        start: Timestamp,
        end: Timestamp,
        limit: usize,
    ) -> Result<Vec<TemporalResult>> {
        let read_txn = self.storage.db().begin_read()?;
        let table = read_txn.open_table(crate::storage::engine::TEMPORAL_INDEX)?;

        // Get range in ascending order, then reverse
        let range = table.range(start.as_micros()..=end.as_micros())?;

        let mut results = Vec::new();
        for entry in range {
            let (ts_key, id_bytes) = entry?;
            let timestamp = Timestamp::from_micros(ts_key.value());
            let id = MemoryId::from_bytes(id_bytes.value())?;

            results.push(TemporalResult { id, timestamp });

            if results.len() >= limit {
                break;
            }
        }

        // Reverse to get newest first
        results.reverse();

        Ok(results)
    }

    /// Get the N most recent memories
    ///
    /// Returns the N most recent memories, sorted by timestamp descending (newest first).
    ///
    /// # Arguments
    ///
    /// * `n` - Number of recent memories to retrieve
    ///
    /// # Returns
    ///
    /// A vector of TemporalResult, sorted newest first
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use mnemefusion_core::index::TemporalIndex;
    /// # use std::sync::Arc;
    /// # let storage = Arc::new(mnemefusion_core::storage::StorageEngine::open("test.mfdb").unwrap());
    /// let temporal = TemporalIndex::new(storage);
    ///
    /// let recent = temporal.recent(10).unwrap();
    /// println!("10 most recent memories:");
    /// for result in recent {
    ///     println!("  {} at {:?}", result.id, result.timestamp);
    /// }
    /// ```
    pub fn recent(&self, n: usize) -> Result<Vec<TemporalResult>> {
        let read_txn = self.storage.db().begin_read()?;
        let table = read_txn.open_table(crate::storage::engine::TEMPORAL_INDEX)?;

        // Use reverse iterator to get newest first
        let mut results = Vec::with_capacity(n);
        let iter = table.iter()?.rev();

        for entry in iter.take(n) {
            let (ts_key, id_bytes) = entry?;
            let timestamp = Timestamp::from_micros(ts_key.value());
            let id = MemoryId::from_bytes(id_bytes.value())?;

            results.push(TemporalResult { id, timestamp });
        }

        Ok(results)
    }

    /// Get the count of memories within a time range
    ///
    /// Efficiently counts memories without loading full data.
    ///
    /// # Arguments
    ///
    /// * `start` - Start of the time range (inclusive)
    /// * `end` - End of the time range (inclusive)
    pub fn count_range(&self, start: Timestamp, end: Timestamp) -> Result<usize> {
        let read_txn = self.storage.db().begin_read()?;
        let table = read_txn.open_table(crate::storage::engine::TEMPORAL_INDEX)?;

        let range = table.range(start.as_micros()..=end.as_micros())?;
        let count = range.count();

        Ok(count)
    }

    /// Add a memory to the temporal index
    ///
    /// This is called automatically when a memory is added to the database.
    ///
    /// # Arguments
    ///
    /// * `id` - The memory ID to index
    /// * `timestamp` - The timestamp of the memory
    pub fn add(&self, id: &MemoryId, timestamp: Timestamp) -> Result<()> {
        let write_txn = self.storage.db().begin_write()?;
        {
            let mut table = write_txn.open_table(crate::storage::engine::TEMPORAL_INDEX)?;
            table.insert(timestamp.as_micros(), id.as_bytes().as_slice())?;
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Remove a memory from the temporal index
    ///
    /// This is called automatically when a memory is deleted from the database.
    /// Since we don't have the timestamp during deletion, we need to scan for it.
    ///
    /// # Arguments
    ///
    /// * `id` - The memory ID to remove
    pub fn remove(&self, id: &MemoryId) -> Result<()> {
        let write_txn = self.storage.db().begin_write()?;
        {
            // First, find the timestamp(s) associated with this memory
            let timestamps_to_remove: Vec<u64> = {
                let table = write_txn.open_table(crate::storage::engine::TEMPORAL_INDEX)?;
                let mut timestamps = Vec::new();

                for entry in table.iter()? {
                    let (ts_key, id_bytes) = entry?;
                    if let Ok(entry_id) = MemoryId::from_bytes(id_bytes.value()) {
                        if &entry_id == id {
                            timestamps.push(ts_key.value());
                        }
                    }
                }

                timestamps
            };

            // Now remove all found entries
            let mut table = write_txn.open_table(crate::storage::engine::TEMPORAL_INDEX)?;
            for ts in timestamps_to_remove {
                table.remove(ts)?;
            }
        }
        write_txn.commit()?;
        Ok(())
    }

    /// Search memories by temporal content matching
    ///
    /// Unlike timestamp-based queries (recent, range_query), this method matches
    /// temporal expressions in the query to temporal expressions in memory content.
    /// For example, "yesterday" in the query matches memories that mention "yesterday".
    ///
    /// # Arguments
    ///
    /// * `query_text` - The query text to extract temporal expressions from
    /// * `limit` - Maximum number of results to return
    ///
    /// # Returns
    ///
    /// A vector of (MemoryId, score) tuples, sorted by score descending
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use mnemefusion_core::index::TemporalIndex;
    /// # use std::sync::Arc;
    /// # let storage = Arc::new(mnemefusion_core::storage::StorageEngine::open("test.mfdb").unwrap());
    /// let temporal = TemporalIndex::new(storage);
    ///
    /// // Query mentions "yesterday" - will match memories with "yesterday" in content
    /// let results = temporal.search_temporal_content("What happened yesterday?", 10).unwrap();
    /// for (id, score) in results {
    ///     println!("Memory {} has temporal overlap score: {}", id, score);
    /// }
    /// ```
    pub fn search_temporal_content(
        &self,
        query_text: &str,
        limit: usize,
    ) -> Result<Vec<(MemoryId, f32)>> {
        use crate::ingest::get_temporal_extractor;

        // Extract temporal expressions from query
        let extractor = get_temporal_extractor();
        let query_expressions = extractor.extract(query_text);

        // If query has no temporal expressions, return empty
        if query_expressions.is_empty() {
            return Ok(vec![]);
        }

        // Get all memories and score them
        let mut scored_results = Vec::new();

        // Get all memories from storage (TODO: optimize this with an index)
        let read_txn = self.storage.db().begin_read()?;
        let table = read_txn.open_table(crate::storage::engine::TEMPORAL_INDEX)?;

        for entry in table.iter()? {
            let (_ts_key, id_bytes) = entry?;
            let memory_id = MemoryId::from_bytes(id_bytes.value())?;

            // Get memory to access metadata
            if let Some(memory) = self.storage.get_memory(&memory_id)? {
                // Get temporal expressions from metadata
                if let Some(temporal_expr_json) = memory.get_metadata("temporal_expressions") {
                    // Parse JSON array of temporal expression strings
                    if let Ok(expr_strings) =
                        serde_json::from_str::<Vec<String>>(temporal_expr_json)
                    {
                        if !expr_strings.is_empty() {
                            // Convert strings back to TemporalExpression objects
                            // For scoring, we just need the text, so create simple Relative expressions
                            let memory_expressions: Vec<_> = expr_strings
                                .iter()
                                .map(|s| crate::ingest::TemporalExpression::Relative(s.clone()))
                                .collect();

                            // Calculate overlap score
                            let score = extractor
                                .calculate_overlap(&query_expressions, &memory_expressions);

                            if score > 0.0 {
                                scored_results.push((memory_id, score));
                            }
                        }
                    }
                }
            }
        }

        // Sort by score descending
        scored_results.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        // Take top-K
        scored_results.truncate(limit);

        Ok(scored_results)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{storage::StorageEngine, types::Memory};
    use tempfile::tempdir;

    fn create_test_index() -> (TemporalIndex, Arc<StorageEngine>, tempfile::TempDir) {
        let dir = tempdir().unwrap();
        let path = dir.path().join("test.mfdb");
        let storage = Arc::new(StorageEngine::open(&path).unwrap());
        let temporal = TemporalIndex::new(Arc::clone(&storage));

        (temporal, storage, dir)
    }

    #[test]
    fn test_recent_with_no_memories() {
        let (temporal, _storage, _dir) = create_test_index();

        let results = temporal.recent(10).unwrap();
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_recent_with_multiple_memories() {
        let (temporal, storage, _dir) = create_test_index();

        let now = Timestamp::now();

        // Add memories with different timestamps
        let mem1 =
            Memory::new_with_timestamp("oldest".to_string(), vec![0.1; 384], now.subtract_days(3));
        let mem2 =
            Memory::new_with_timestamp("middle".to_string(), vec![0.2; 384], now.subtract_days(2));
        let mem3 =
            Memory::new_with_timestamp("newest".to_string(), vec![0.3; 384], now.subtract_days(1));

        storage.store_memory(&mem1).unwrap();
        storage.store_memory(&mem2).unwrap();
        storage.store_memory(&mem3).unwrap();

        // Get 2 most recent
        let results = temporal.recent(2).unwrap();
        assert_eq!(results.len(), 2);

        // Should be newest first
        assert_eq!(results[0].timestamp, mem3.created_at);
        assert_eq!(results[1].timestamp, mem2.created_at);
    }

    #[test]
    fn test_recent_with_limit_larger_than_count() {
        let (temporal, storage, _dir) = create_test_index();

        let mem = Memory::new("test".to_string(), vec![0.1; 384]);
        storage.store_memory(&mem).unwrap();

        let results = temporal.recent(100).unwrap();
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_range_query_within_range() {
        let (temporal, storage, _dir) = create_test_index();

        let now = Timestamp::now();
        let start = now.subtract_days(7);
        let end = now;

        // Add memory within range
        let mem1 =
            Memory::new_with_timestamp("within".to_string(), vec![0.1; 384], now.subtract_days(3));
        // Add memory outside range (too old)
        let mem2 = Memory::new_with_timestamp(
            "too old".to_string(),
            vec![0.2; 384],
            now.subtract_days(10),
        );

        storage.store_memory(&mem1).unwrap();
        storage.store_memory(&mem2).unwrap();

        let results = temporal.range_query(start, end, 100).unwrap();

        // Should only get mem1
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, mem1.id);
    }

    #[test]
    fn test_range_query_empty_range() {
        let (temporal, storage, _dir) = create_test_index();

        let now = Timestamp::now();
        let mem = Memory::new("test".to_string(), vec![0.1; 384]);
        storage.store_memory(&mem).unwrap();

        // Query range in the future
        let future_start = now.add_days(1);
        let future_end = now.add_days(2);

        let results = temporal.range_query(future_start, future_end, 100).unwrap();
        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_range_query_with_limit() {
        let (temporal, storage, _dir) = create_test_index();

        let now = Timestamp::now();

        // Add 5 memories
        for i in 0..5 {
            let mem = Memory::new_with_timestamp(
                format!("memory {}", i),
                vec![0.1; 384],
                now.subtract_days(i as u64),
            );
            storage.store_memory(&mem).unwrap();
        }

        // Query all but limit to 3
        let results = temporal.range_query(now.subtract_days(10), now, 3).unwrap();
        assert_eq!(results.len(), 3);

        // Should be newest first
        assert!(results[0].timestamp > results[1].timestamp);
        assert!(results[1].timestamp > results[2].timestamp);
    }

    #[test]
    fn test_count_range() {
        let (temporal, storage, _dir) = create_test_index();

        let now = Timestamp::now();

        // Add memories
        for i in 0..10 {
            let mem = Memory::new_with_timestamp(
                format!("memory {}", i),
                vec![0.1; 384],
                now.subtract_days(i as u64),
            );
            storage.store_memory(&mem).unwrap();
        }

        // Count last 5 days
        let count = temporal.count_range(now.subtract_days(5), now).unwrap();
        assert_eq!(count, 6); // Days 0,1,2,3,4,5 = 6 days
    }

    #[test]
    fn test_range_query_ordering() {
        let (temporal, storage, _dir) = create_test_index();

        let now = Timestamp::now();

        // Add memories in random order
        let times = vec![5, 2, 8, 1, 9, 3];
        for (i, days_ago) in times.iter().enumerate() {
            let mem = Memory::new_with_timestamp(
                format!("memory {}", i),
                vec![0.1; 384],
                now.subtract_days(*days_ago),
            );
            storage.store_memory(&mem).unwrap();
        }

        let results = temporal
            .range_query(now.subtract_days(10), now, 100)
            .unwrap();

        // Verify ordering: newest first
        for i in 0..results.len() - 1 {
            assert!(
                results[i].timestamp >= results[i + 1].timestamp,
                "Results should be sorted newest first"
            );
        }
    }

    // Temporal content scoring tests
    #[test]
    fn test_search_temporal_content_basic() {
        let (temporal, storage, _dir) = create_test_index();

        // Add memory with temporal expression in content
        let mut mem1 = Memory::new(
            "We had a meeting yesterday about the project".to_string(),
            vec![0.1; 384],
        );
        mem1.set_metadata(
            "temporal_expressions".to_string(),
            r#"["yesterday"]"#.to_string(),
        );
        storage.store_memory(&mem1).unwrap();

        // Add memory with different temporal expression
        let mut mem2 = Memory::new(
            "The event is scheduled for next week".to_string(),
            vec![0.2; 384],
        );
        mem2.set_metadata(
            "temporal_expressions".to_string(),
            r#"["next week"]"#.to_string(),
        );
        storage.store_memory(&mem2).unwrap();

        // Add memory with no temporal expressions
        let mem3 = Memory::new(
            "Machine learning is fascinating".to_string(),
            vec![0.3; 384],
        );
        storage.store_memory(&mem3).unwrap();

        // Query with "yesterday"
        let results = temporal
            .search_temporal_content("What happened yesterday?", 10)
            .unwrap();

        // Should find mem1 with "yesterday"
        assert!(
            !results.is_empty(),
            "Should find memories with matching temporal expressions"
        );
        assert_eq!(results[0].0, mem1.id, "Should rank mem1 (yesterday) first");
        assert!(results[0].1 > 0.0, "Should have positive score");
    }

    #[test]
    fn test_search_temporal_content_no_query_expressions() {
        let (temporal, storage, _dir) = create_test_index();

        // Add memory with temporal expression
        let mut mem1 = Memory::new("We met yesterday".to_string(), vec![0.1; 384]);
        mem1.set_metadata(
            "temporal_expressions".to_string(),
            r#"["yesterday"]"#.to_string(),
        );
        storage.store_memory(&mem1).unwrap();

        // Query with no temporal expressions
        let results = temporal
            .search_temporal_content("Tell me about machine learning", 10)
            .unwrap();

        // Should return empty (no temporal expressions in query)
        assert_eq!(
            results.len(),
            0,
            "Should return empty when query has no temporal expressions"
        );
    }

    #[test]
    fn test_search_temporal_content_multiple_matches() {
        let (temporal, storage, _dir) = create_test_index();

        // Add memories with "yesterday"
        let mut mem1 = Memory::new(
            "I saw Alice yesterday at the park".to_string(),
            vec![0.1; 384],
        );
        mem1.set_metadata(
            "temporal_expressions".to_string(),
            r#"["yesterday"]"#.to_string(),
        );
        storage.store_memory(&mem1).unwrap();

        let mut mem2 = Memory::new(
            "Yesterday was a great day for tennis".to_string(),
            vec![0.2; 384],
        );
        mem2.set_metadata(
            "temporal_expressions".to_string(),
            r#"["yesterday"]"#.to_string(),
        );
        storage.store_memory(&mem2).unwrap();

        let mut mem3 = Memory::new("The meeting was last week".to_string(), vec![0.3; 384]);
        mem3.set_metadata(
            "temporal_expressions".to_string(),
            r#"["last week"]"#.to_string(),
        );
        storage.store_memory(&mem3).unwrap();

        // Query with "yesterday"
        let results = temporal
            .search_temporal_content("What did I do yesterday?", 10)
            .unwrap();

        // Should find both mem1 and mem2 (both have "yesterday")
        assert!(
            results.len() >= 2,
            "Should find multiple memories with matching temporal expression"
        );

        let result_ids: Vec<_> = results.iter().map(|(id, _)| id).collect();
        assert!(
            result_ids.contains(&&mem1.id),
            "Should include mem1 (yesterday)"
        );
        assert!(
            result_ids.contains(&&mem2.id),
            "Should include mem2 (yesterday)"
        );
    }

    #[test]
    fn test_search_temporal_content_scoring() {
        let (temporal, storage, _dir) = create_test_index();

        // Add memory with multiple temporal expressions matching query
        let mut mem1 = Memory::new(
            "Yesterday morning we discussed the project".to_string(),
            vec![0.1; 384],
        );
        mem1.set_metadata(
            "temporal_expressions".to_string(),
            r#"["yesterday", "morning"]"#.to_string(),
        );
        storage.store_memory(&mem1).unwrap();

        // Add memory with only one matching expression
        let mut mem2 = Memory::new("I went to the park yesterday".to_string(), vec![0.2; 384]);
        mem2.set_metadata(
            "temporal_expressions".to_string(),
            r#"["yesterday"]"#.to_string(),
        );
        storage.store_memory(&mem2).unwrap();

        // Query with multiple temporal expressions
        let results = temporal
            .search_temporal_content("What happened yesterday morning?", 10)
            .unwrap();

        assert!(results.len() >= 2, "Should find both memories");

        // mem1 should score higher (has both "yesterday" and "morning")
        // Note: The actual scoring depends on TemporalExtractor.calculate_overlap()
        // which may normalize scores, so we just verify both are found
        let result_ids: Vec<_> = results.iter().map(|(id, _)| id).collect();
        assert!(
            result_ids.contains(&&mem1.id),
            "Should include mem1 (yesterday morning)"
        );
        assert!(
            result_ids.contains(&&mem2.id),
            "Should include mem2 (yesterday)"
        );
    }
}