tapirus 1.2.1

Embedded multi-model database with SQL, vector search and local AI memory
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//! Native Embedded AI Agent Memory Subsystem for TapirusDB.
//!
//! Provides a unified memory engine combining:
//! - **Episodic & Semantic Storage**: Chronological observations and facts.
//! - **Hybrid Retrieval**: Dense vector similarity plus BM25 lexical keyword search.
//! - **Temporal Recency Decay**: Time-decay scoring ($e^{-\lambda \Delta t}$) with customizable half-life.
//! - **Graph Memory Association**: Associative links connecting related memories and entities.
//! - Configurable admission limits and indexed retrieval for native/edge workloads.

pub mod bm25;
pub mod embedder;

pub use bm25::{Bm25Index, Bm25Params, reciprocal_rank_fusion};
pub use embedder::{
    DeterministicHashEmbedder, EmbeddingEngine, EmbeddingProvider, HttpEmbeddingEngine,
};

use crate::error::{Error, Result};

pub mod api;
pub(crate) mod binary;
mod config;
mod indexed;
pub use api::*;
pub use config::*;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// An individual memory unit recorded by an AI agent
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MemoryEntry {
    /// Unique 64-bit Memory ID
    pub id: u64,
    /// Memory content (text observation, user dialogue, learned fact, or summary)
    pub content: String,
    /// High-dimensional dense embedding representation (optional)
    pub vector: Option<Vec<f32>>,
    /// Timestamp when this memory occurred (Unix epoch seconds)
    pub timestamp: u64,
    /// Timestamp when this memory was last retrieved/accessed
    pub last_accessed: u64,
    /// Memory importance / intrinsic priority score in $[0.0, 1.0]$ (default: 0.5)
    pub importance: f32,
    /// Access counter tracking how often this memory has been recalled
    pub access_count: u32,
    /// Semantic tags or categorical labels
    pub tags: Vec<String>,
    /// Associated memory IDs connected in the knowledge graph
    pub associations: Vec<u64>,
    /// Multi-agent namespace isolation (e.g. "agent_alpha", "crew_finance")
    #[serde(default)]
    pub namespace: Option<String>,
    /// Session or thread conversation identifier (e.g. "chat_123")
    #[serde(default)]
    pub session_id: Option<String>,
    /// Application document identifier, unique within namespace and session.
    #[serde(default)]
    pub external_id: Option<String>,
    /// Application's source revision, independent of the database revision.
    #[serde(default)]
    pub source_version: Option<String>,
    /// Stable embedding model identifier; absent denotes the legacy vector space.
    #[serde(default)]
    pub embedding_model: Option<String>,
    /// Absolute Unix expiry time; expired sources are excluded from retrieval.
    #[serde(default)]
    pub expires_at: Option<u64>,
    /// Monotonic document revision for conditional updates.
    #[serde(default)]
    pub revision: u64,
    /// Last source update time in Unix seconds.
    #[serde(default)]
    pub updated_at: u64,
    /// SHA-256 of the original UTF-8 content.
    #[serde(default)]
    pub content_hash: String,
}

/// Parameters and weights controlling multi-modal memory recall
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryRecallFilter {
    /// Weight assigned to dense vector semantic similarity (default: 0.45)
    pub vector_weight: f32,
    /// Weight assigned to BM25 lexical keyword matching (default: 0.35)
    pub bm25_weight: f32,
    /// Weight assigned to temporal recency decay (default: 0.10)
    pub recency_weight: f32,
    /// Weight assigned to intrinsic memory importance (default: 0.10)
    pub importance_weight: f32,
    /// Half-life in seconds for exponential temporal decay (default: 30 days = 2,592,000s)
    pub half_life_seconds: u64,
    /// Optional tag filters (must contain at least one of these tags if specified)
    pub required_tags: Vec<String>,
    /// Minimum combined score cutoff $[0.0, 1.0]$
    pub min_score: f32,
    /// Graph expansion hops to automatically include connected associative memories (default: 0)
    pub graph_expansion_hops: usize,
    /// Multi-agent namespace scope filter
    #[serde(default)]
    pub namespace: Option<String>,
    /// Session conversation scope filter
    #[serde(default)]
    pub session_id: Option<String>,
}

impl Default for MemoryRecallFilter {
    fn default() -> Self {
        Self {
            vector_weight: 0.45,
            bm25_weight: 0.35,
            recency_weight: 0.10,
            importance_weight: 0.10,
            half_life_seconds: 30 * 86400, // 30 days
            required_tags: Vec::new(),
            min_score: 0.0,
            graph_expansion_hops: 0,
            namespace: None,
            session_id: None,
        }
    }
}

impl MemoryRecallFilter {
    /// Configure custom weights for vector, bm25, recency, and importance
    pub fn with_weights(mut self, vector: f32, bm25: f32, recency: f32, importance: f32) -> Self {
        self.vector_weight = vector;
        self.bm25_weight = bm25;
        self.recency_weight = recency;
        self.importance_weight = importance;
        self
    }

    /// Set half-life duration in seconds for temporal decay
    pub fn with_half_life(mut self, seconds: u64) -> Self {
        self.half_life_seconds = seconds;
        self
    }

    /// Set required tags
    pub fn with_tags(mut self, tags: &[&str]) -> Self {
        self.required_tags = tags.iter().map(|t| t.to_string()).collect();
        self
    }

    /// Set graph expansion hops for associative recall
    pub fn with_graph_hops(mut self, hops: usize) -> Self {
        self.graph_expansion_hops = hops;
        self
    }
}

/// A recalled memory result containing the entry and its constituent scores
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryRecallResult {
    /// The retrieved memory entry
    pub entry: MemoryEntry,
    /// Total combined hybrid score
    pub combined_score: f32,
    /// Semantic vector similarity component $[0.0, 1.0]$
    pub semantic_score: f32,
    /// Raw BM25 lexical keyword score (not a probability or bounded to one).
    pub lexical_score: f32,
    /// Temporal recency decay score $[0.0, 1.0]$
    pub recency_score: f32,
    /// Whether this result was retrieved via graph association expansion
    pub is_associative: bool,
}

/// The embedded AI Agent Memory Engine
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryEngine {
    entries: HashMap<u64, MemoryEntry>,
    bm25: Bm25Index,
    next_id: u64,
    #[serde(skip)]
    runtime: indexed::MemoryRuntime,
}

impl Default for MemoryEngine {
    fn default() -> Self {
        Self::new()
    }
}

impl MemoryEngine {
    /// Create a new empty MemoryEngine
    pub fn new() -> Self {
        Self {
            entries: HashMap::new(),
            bm25: Bm25Index::new(),
            next_id: 1,
            runtime: indexed::MemoryRuntime::default(),
        }
    }

    /// Store a new memory into the engine with optional multi-agent namespace and session scope
    #[allow(
        clippy::too_many_arguments,
        reason = "Preserve the public scoped-memory API"
    )]
    pub fn remember_scoped(
        &mut self,
        content: &str,
        vector: Option<&[f32]>,
        importance: f32,
        tags: &[&str],
        now_ts: u64,
        namespace: Option<&str>,
        session_id: Option<&str>,
    ) -> u64 {
        let id = self.next_id;
        self.next_id += 1;

        let entry = MemoryEntry {
            id,
            content: content.to_string(),
            vector: vector.map(|v| v.to_vec()),
            timestamp: now_ts,
            last_accessed: now_ts,
            importance: importance.clamp(0.0, 1.0),
            access_count: 0,
            tags: tags.iter().map(|s| s.to_string()).collect(),
            associations: Vec::new(),
            namespace: namespace.map(|s| s.to_string()),
            session_id: session_id.map(|s| s.to_string()),
            external_id: None,
            source_version: None,
            embedding_model: None,
            expires_at: None,
            revision: 1,
            updated_at: now_ts,
            content_hash: indexed::content_hash(content),
        };

        // Index in BM25
        self.bm25.index_document(id, content);
        self.entries.insert(id, entry);
        self.runtime.changed(id, self.entries.get(&id));

        id
    }

    /// Restore a persisted memory entry from disk storage
    pub fn restore_entry(&mut self, mut entry: MemoryEntry) -> Result<()> {
        if entry.id == 0 || entry.id == u64::MAX {
            return Err(Error::Corrupted("Invalid persisted memory ID".into()));
        }
        if entry.content_hash.is_empty() {
            entry.content_hash = indexed::content_hash(&entry.content);
        }
        if entry.revision == 0 {
            entry.revision = 1;
        }
        if entry.updated_at == 0 {
            entry.updated_at = entry.timestamp;
        }
        self.validate_entry(&entry)?;
        if entry.id >= self.next_id {
            self.next_id = entry.id + 1;
        }
        self.bm25.index_document(entry.id, &entry.content);
        self.runtime.changed(entry.id, Some(&entry));
        self.entries.insert(entry.id, entry);
        Ok(())
    }

    /// Store a new memory into the engine (default namespace)
    pub fn remember(
        &mut self,
        content: &str,
        vector: Option<&[f32]>,
        importance: f32,
        tags: &[&str],
        now_ts: u64,
    ) -> u64 {
        self.remember_scoped(content, vector, importance, tags, now_ts, None, None)
    }

    /// Associate two memories together with a bidirectional link
    pub fn associate(&mut self, id_a: u64, id_b: u64) -> Result<()> {
        if !self.entries.contains_key(&id_a) {
            return Err(Error::Corrupted(format!("Memory #{id_a} not found")));
        }
        if !self.entries.contains_key(&id_b) {
            return Err(Error::Corrupted(format!("Memory #{id_b} not found")));
        }

        if let Some(entry_a) = self.entries.get_mut(&id_a)
            && !entry_a.associations.contains(&id_b)
        {
            entry_a.associations.push(id_b);
        }
        if let Some(entry_b) = self.entries.get_mut(&id_b)
            && !entry_b.associations.contains(&id_a)
        {
            entry_b.associations.push(id_a);
        }

        Ok(())
    }

    /// Retrieve a single memory by ID and increment its access count
    pub fn get_memory_mut(&mut self, id: u64, now_ts: u64) -> Option<&mut MemoryEntry> {
        if let Some(entry) = self.entries.get_mut(&id) {
            if entry.expires_at.is_some_and(|expiry| now_ts >= expiry) {
                return None;
            }
            entry.last_accessed = now_ts;
            entry.access_count += 1;
            Some(entry)
        } else {
            None
        }
    }

    /// Retrieve a single memory by ID without mutating access metrics
    pub fn get_memory(&self, id: u64) -> Option<&MemoryEntry> {
        self.entries.get(&id)
    }

    /// Return the total count of stored memories
    pub fn memory_count(&self) -> usize {
        self.entries.len()
    }

    /// Recall relevant memories using hybrid Vector + BM25 + Temporal Decay + Graph expansion
    pub fn recall(
        &mut self,
        query_text: Option<&str>,
        query_vector: Option<&[f32]>,
        limit: usize,
        filter: &MemoryRecallFilter,
        now_ts: u64,
    ) -> Vec<MemoryRecallResult> {
        if self.entries.is_empty() || limit == 0 {
            return Vec::new();
        }

        // 1. Evaluate BM25 scores if query text is present
        let bm25_scores = if let Some(q_text) = query_text {
            let matches = self.bm25.search(q_text, self.entries.len());
            matches.into_iter().collect::<HashMap<u64, f32>>()
        } else {
            HashMap::new()
        };

        let lambda = if filter.half_life_seconds > 0 {
            2.0f32.ln() / (filter.half_life_seconds as f32)
        } else {
            0.0
        };

        let mut scored_candidates: Vec<(u64, f32, f32, f32, f32, bool)> =
            Vec::with_capacity(self.entries.len());

        // 2. Score candidate memories
        for entry in self.entries.values() {
            if entry.expires_at.is_some_and(|expiry| now_ts >= expiry) {
                continue;
            }
            // Check namespace filter
            if let Some(ref req_ns) = filter.namespace {
                match &entry.namespace {
                    Some(ns) if ns.eq_ignore_ascii_case(req_ns) => {}
                    _ => continue,
                }
            }

            // Check session filter
            if let Some(ref req_sess) = filter.session_id {
                match &entry.session_id {
                    Some(sess) if sess == req_sess => {}
                    _ => continue,
                }
            }

            // Check required tags filter
            if !filter.required_tags.is_empty() {
                let matches_tag = filter
                    .required_tags
                    .iter()
                    .any(|req_tag| entry.tags.iter().any(|t| t.eq_ignore_ascii_case(req_tag)));
                if !matches_tag {
                    continue;
                }
            }

            // A. Semantic Vector Score
            let semantic_score = match (query_vector, &entry.vector) {
                (Some(q_vec), Some(e_vec)) => cosine_similarity(q_vec, e_vec).max(0.0),
                _ => 0.0,
            };

            // B. Lexical BM25 Score
            let lexical_score = *bm25_scores.get(&entry.id).unwrap_or(&0.0);

            // C. Temporal Recency Decay Score: e^(-lambda * dt)
            let dt = now_ts.saturating_sub(entry.timestamp) as f32;
            let recency_score = (-lambda * dt).exp().clamp(0.0, 1.0);

            // D. Combined Weighted Score
            let combined_score = filter.vector_weight * semantic_score
                + filter.bm25_weight * lexical_score
                + filter.recency_weight * recency_score
                + filter.importance_weight * entry.importance;

            let has_query = query_text.is_some() || query_vector.is_some();
            let matches_query = if has_query {
                (query_text.is_some() && lexical_score > 0.0)
                    || (query_vector.is_some() && semantic_score > 0.0)
            } else {
                true
            };

            if matches_query && combined_score >= filter.min_score {
                scored_candidates.push((
                    entry.id,
                    combined_score,
                    semantic_score,
                    lexical_score,
                    recency_score,
                    false, // is_associative
                ));
            }
        }

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

        // 3. Graph Associative Expansion
        if filter.graph_expansion_hops > 0 && !scored_candidates.is_empty() {
            let mut existing_ids: HashSet<u64> = scored_candidates.iter().map(|c| c.0).collect();
            let mut associative_candidates = Vec::new();

            // Take the top candidate memories and expand their associations
            let base_count = scored_candidates.len().min(limit);
            for &(parent_id, parent_score, ..) in scored_candidates.iter().take(base_count) {
                if let Some(parent_entry) = self.entries.get(&parent_id) {
                    for &assoc_id in &parent_entry.associations {
                        if !existing_ids.contains(&assoc_id)
                            && let Some(assoc_entry) = self.entries.get(&assoc_id)
                        {
                            if assoc_entry
                                .expires_at
                                .is_some_and(|expiry| now_ts >= expiry)
                            {
                                continue;
                            }
                            if let Some(ref req_ns) = filter.namespace {
                                match &assoc_entry.namespace {
                                    Some(ns) if ns.eq_ignore_ascii_case(req_ns) => {}
                                    _ => continue,
                                }
                            }
                            if let Some(ref req_sess) = filter.session_id {
                                match &assoc_entry.session_id {
                                    Some(sess) if sess == req_sess => {}
                                    _ => continue,
                                }
                            }

                            existing_ids.insert(assoc_id);
                            let assoc_score = parent_score * 0.85;
                            associative_candidates.push((
                                assoc_id,
                                assoc_score,
                                0.0,
                                0.0,
                                0.0,
                                true,
                            ));
                        }
                    }
                }
            }

            scored_candidates.extend(associative_candidates);
            scored_candidates
                .sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        }

        scored_candidates.truncate(limit);

        // Hydrate and update access metrics only for the winning top-K memories
        let mut final_results = Vec::with_capacity(scored_candidates.len());
        for (id, combined_score, semantic_score, lexical_score, recency_score, is_associative) in
            scored_candidates
        {
            if let Some(entry) = self.entries.get_mut(&id) {
                entry.last_accessed = now_ts;
                entry.access_count = entry.access_count.saturating_add(1);
                final_results.push(MemoryRecallResult {
                    entry: entry.clone(),
                    combined_score,
                    semantic_score,
                    lexical_score,
                    recency_score,
                    is_associative,
                });
            }
        }

        final_results
    }

    /// Recall memories using Reciprocal Rank Fusion (RRF) combining BM25 lexical and Vector semantic ranks
    pub fn recall_rrf(
        &mut self,
        query_text: &str,
        query_vector: Option<&[f32]>,
        limit: usize,
        rrf_k: f32,
        now_ts: u64,
    ) -> Vec<MemoryRecallResult> {
        self.recall_rrf_scoped(query_text, query_vector, limit, rrf_k, now_ts, None)
    }

    pub(crate) fn recall_rrf_scoped(
        &mut self,
        query_text: &str,
        query_vector: Option<&[f32]>,
        limit: usize,
        rrf_k: f32,
        now_ts: u64,
        scope: Option<&MemoryAccessScope>,
    ) -> Vec<MemoryRecallResult> {
        // 1. BM25 search
        let bm25_hits = self.bm25.search(query_text, self.entries.len());
        let bm25_ids: Vec<u64> = bm25_hits
            .iter()
            .filter_map(|(id, _)| {
                let entry = self.entries.get(id)?;
                if scope.is_some_and(|s| !s.matches_entry(entry))
                    || entry.expires_at.is_some_and(|expiry| now_ts >= expiry)
                {
                    return None;
                }
                Some(*id)
            })
            .collect();

        // 2. Vector search (if vector is provided)
        let mut vector_hits: Vec<(u64, f32)> = Vec::new();
        if let Some(q_vec) = query_vector {
            for (id, entry) in &self.entries {
                if scope.is_some_and(|s| !s.matches_entry(entry))
                    || entry.expires_at.is_some_and(|expiry| now_ts >= expiry)
                {
                    continue;
                }
                if let Some(e_vec) = &entry.vector
                    && e_vec.len() == q_vec.len()
                {
                    let dot: f32 = q_vec.iter().zip(e_vec.iter()).map(|(a, b)| a * b).sum();
                    let norm_q: f32 = q_vec.iter().map(|x| x * x).sum::<f32>().sqrt();
                    let norm_e: f32 = e_vec.iter().map(|x| x * x).sum::<f32>().sqrt();
                    let sim = if norm_q > 1e-9 && norm_e > 1e-9 {
                        dot / (norm_q * norm_e)
                    } else {
                        0.0
                    };
                    vector_hits.push((*id, sim));
                }
            }
            vector_hits.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        }
        let vector_ids: Vec<u64> = vector_hits.iter().map(|(id, _)| *id).collect();

        // 3. Fused using RRF
        let mut ranked_lists: Vec<(&[u64], f32)> = Vec::new();
        if !bm25_ids.is_empty() {
            ranked_lists.push((&bm25_ids, 1.0));
        }
        if !vector_ids.is_empty() {
            ranked_lists.push((&vector_ids, 1.0));
        }

        let fused = bm25::reciprocal_rank_fusion(&ranked_lists, rrf_k, limit);

        let mut results = Vec::new();
        for (id, fused_score) in fused {
            if let Some(entry) = self.entries.get_mut(&id) {
                entry.last_accessed = now_ts;
                entry.access_count += 1;
                results.push(MemoryRecallResult {
                    entry: entry.clone(),
                    combined_score: fused_score,
                    semantic_score: 0.0,
                    lexical_score: 0.0,
                    recency_score: 0.0,
                    is_associative: false,
                });
            }
        }
        results
    }

    /// Prune decayed memories and return the list of deleted IDs
    pub fn prune_decayed_ids_at(
        &mut self,
        decay_threshold: f32,
        half_life_seconds: u64,
        now_ts: u64,
    ) -> Vec<u64> {
        self.prune_decayed_ids_at_scoped(decay_threshold, half_life_seconds, now_ts, None)
    }

    pub(crate) fn prune_decayed_ids_at_scoped(
        &mut self,
        decay_threshold: f32,
        half_life_seconds: u64,
        now_ts: u64,
        scope: Option<&MemoryAccessScope>,
    ) -> Vec<u64> {
        let lambda = if half_life_seconds > 0 {
            2.0f32.ln() / (half_life_seconds as f32)
        } else {
            0.0
        };

        let mut to_remove: Vec<u64> = Vec::new();
        for (&id, entry) in &self.entries {
            if scope.is_some_and(|s| !s.matches_entry(entry)) {
                continue;
            }
            let dt = now_ts.saturating_sub(entry.timestamp) as f32;
            let recency = (-lambda * dt).exp();
            let retention_score = 0.6 * entry.importance + 0.4 * recency;
            if retention_score < decay_threshold {
                to_remove.push(id);
            }
        }

        for &id in &to_remove {
            self.entries.remove(&id);
            self.bm25.remove_document(id);
            self.runtime.changed(id, None);
        }

        // Clean up associations in remaining entries
        for entry in self.entries.values_mut() {
            entry.associations.retain(|id| !to_remove.contains(id));
        }

        to_remove
    }

    /// Prune memories whose retention score falls below `decay_threshold`
    pub fn prune_decayed_at(
        &mut self,
        decay_threshold: f32,
        half_life_seconds: u64,
        now_ts: u64,
    ) -> usize {
        self.prune_decayed_ids_at(decay_threshold, half_life_seconds, now_ts)
            .len()
    }

    /// Prune memories whose retention score falls below `decay_threshold` (alias for prune_decayed_at)
    pub fn prune(&mut self, decay_threshold: f32, half_life_seconds: u64, now_ts: u64) -> usize {
        self.prune_decayed_at(decay_threshold, half_life_seconds, now_ts)
    }

    /// Explicitly remove and forget a memory by ID, purging it from BM25 and associative links
    pub fn forget(&mut self, id: u64) -> bool {
        if self.entries.remove(&id).is_some() {
            self.bm25.remove_document(id);
            self.runtime.changed(id, None);
            for entry in self.entries.values_mut() {
                entry.associations.retain(|&assoc_id| assoc_id != id);
            }
            true
        } else {
            false
        }
    }

    /// Get reference to a memory entry by ID
    pub fn get(&self, id: u64) -> Option<&MemoryEntry> {
        self.entries.get(&id)
    }

    /// Return all memory entries
    pub fn all_entries(&self) -> &HashMap<u64, MemoryEntry> {
        &self.entries
    }
}

/// Compute cosine similarity between two normalized or unnormalized float vectors in Safe Rust
pub fn cosine_similarity(u: &[f32], v: &[f32]) -> f32 {
    if u.len() != v.len() || u.is_empty() {
        return 0.0;
    }

    let mut dot = 0.0f64;
    let mut norm_u = 0.0f64;
    let mut norm_v = 0.0f64;

    for i in 0..u.len() {
        dot += f64::from(u[i]) * f64::from(v[i]);
        norm_u += f64::from(u[i]).powi(2);
        norm_v += f64::from(v[i]).powi(2);
    }

    let denom = norm_u.sqrt() * norm_v.sqrt();
    if denom > 1e-8 {
        (dot / denom).clamp(-1.0, 1.0) as f32
    } else {
        0.0
    }
}

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

    #[test]
    fn test_memory_remember_and_hybrid_recall() {
        let mut engine = MemoryEngine::new();
        let now = 1_000_000;

        let vec1 = vec![1.0, 0.0, 0.0];
        let vec2 = vec![0.0, 1.0, 0.0];

        let id1 = engine.remember(
            "User prefers replies in Bahasa Melayu and lives in Kuala Lumpur",
            Some(&vec1),
            0.9,
            &["preference", "language"],
            now,
        );

        let id2 = engine.remember(
            "User is building a high performance database engine called TapirusDB",
            Some(&vec2),
            0.8,
            &["project", "systems"],
            now,
        );

        assert_eq!(id1, 1);
        assert_eq!(id2, 2);

        // A. BM25 keyword query
        let filter = MemoryRecallFilter::default();
        let results = engine.recall(Some("Bahasa Melayu"), None, 5, &filter, now);
        assert!(!results.is_empty());
        assert_eq!(results[0].entry.id, id1);

        // B. Vector semantic query
        let results_vec = engine.recall(None, Some(&vec2), 5, &filter, now);
        assert!(!results_vec.is_empty());
        assert_eq!(results_vec[0].entry.id, id2);
    }

    #[test]
    fn test_temporal_recency_decay() {
        let mut engine = MemoryEngine::new();
        let t0 = 1_000_000;
        let half_life = 86400; // 1 day half life

        // Store old memory (7 days ago) with medium importance
        let _id_old = engine.remember(
            "Saw an interesting article on databases",
            None,
            0.5,
            &[],
            t0,
        );

        // Store new memory (just now) with same importance
        let t_now = t0 + 7 * 86400;
        let id_new = engine.remember(
            "Saw an interesting article on databases",
            None,
            0.5,
            &[],
            t_now,
        );

        let filter = MemoryRecallFilter::default()
            .with_weights(0.0, 0.4, 0.6, 0.0) // Give high weight to recency
            .with_half_life(half_life);

        let results = engine.recall(Some("article databases"), None, 2, &filter, t_now);
        assert_eq!(results.len(), 2);
        // The newer memory must rank higher due to temporal decay
        assert_eq!(results[0].entry.id, id_new);
        assert!(results[0].combined_score > results[1].combined_score);
    }

    #[test]
    fn test_graph_associative_recall() {
        let mut engine = MemoryEngine::new();
        let now = 1_000_000;

        let id1 = engine.remember(
            "Alex Chen is the architect of TapirusDB",
            None,
            0.9,
            &[],
            now,
        );
        let id2 = engine.remember(
            "TapirusDB uses ChaCha20-Poly1305 AEAD page encryption",
            None,
            0.8,
            &[],
            now,
        );

        engine.associate(id1, id2).expect("Associate memories");

        // Query only mentions "Alex Chen"
        let filter = MemoryRecallFilter::default().with_graph_hops(1);
        let results = engine.recall(Some("Alex Chen"), None, 5, &filter, now);

        // Should retrieve id1 directly, and id2 via associative graph link!
        assert!(results.len() >= 2);
        assert_eq!(results[0].entry.id, id1);
        assert_eq!(results[1].entry.id, id2);
        assert!(results[1].is_associative);
    }
}