codemem-engine 0.19.0

Domain logic engine for Codemem: indexing, hooks, watching, scoring, recall, 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
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
use crate::CodememEngine;
use codemem_core::{Edge, MemoryNode, MemoryType, RelationshipType};
use std::collections::HashMap;

fn make_memory(id: &str, content: &str) -> MemoryNode {
    make_memory_with_opts(id, content, MemoryType::Context, None, &[], 0.7, 0.9)
}

fn make_memory_with_opts(
    id: &str,
    content: &str,
    memory_type: MemoryType,
    namespace: Option<&str>,
    tags: &[&str],
    importance: f64,
    confidence: f64,
) -> MemoryNode {
    let mut m = MemoryNode::test_default(content);
    m.id = id.to_string();
    m.memory_type = memory_type;
    m.importance = importance;
    m.confidence = confidence;
    m.tags = tags.iter().map(|s| s.to_string()).collect();
    m.namespace = namespace.map(String::from);
    m
}

// ── Basic recall ────────────────────────────────────────────────────

#[test]
fn recall_returns_matching_memories() {
    let engine = CodememEngine::for_testing();
    engine
        .persist_memory(&make_memory("r1", "Rust ownership and borrowing rules"))
        .unwrap();
    engine
        .persist_memory(&make_memory("r2", "Python list comprehensions"))
        .unwrap();

    let results = engine
        .recall(&crate::recall::RecallQuery::new("ownership borrowing", 5))
        .unwrap();
    assert!(!results.is_empty(), "should find at least one result");
    assert_eq!(results[0].memory.id, "r1");
}

#[test]
fn recall_respects_k_limit() {
    let engine = CodememEngine::for_testing();
    for i in 0..10 {
        engine
            .persist_memory(&make_memory(
                &format!("k{i}"),
                &format!("memory about testing topic number {i}"),
            ))
            .unwrap();
    }

    let results = engine
        .recall(&crate::recall::RecallQuery::new("testing topic", 3))
        .unwrap();
    assert!(results.len() <= 3, "should return at most k=3 results");
}

#[test]
fn recall_sorted_by_score_descending() {
    let engine = CodememEngine::for_testing();
    engine
        .persist_memory(&make_memory("s1", "alpha beta gamma"))
        .unwrap();
    engine
        .persist_memory(&make_memory("s2", "alpha beta"))
        .unwrap();

    let results = engine
        .recall(&crate::recall::RecallQuery::new("alpha beta gamma", 5))
        .unwrap();
    if results.len() >= 2 {
        assert!(
            results[0].score >= results[1].score,
            "results should be sorted by score descending"
        );
    }
}

// ── Memory type filter ──────────────────────────────────────────────

#[test]
fn recall_filters_by_memory_type() {
    let engine = CodememEngine::for_testing();
    engine
        .persist_memory(&make_memory_with_opts(
            "t1",
            "decision about database schema",
            MemoryType::Decision,
            None,
            &[],
            0.7,
            0.9,
        ))
        .unwrap();
    engine
        .persist_memory(&make_memory_with_opts(
            "t2",
            "context about database schema",
            MemoryType::Context,
            None,
            &[],
            0.7,
            0.9,
        ))
        .unwrap();

    let results = engine
        .recall(&crate::recall::RecallQuery {
            memory_type_filter: Some(MemoryType::Decision),
            ..crate::recall::RecallQuery::new("database schema", 5)
        })
        .unwrap();
    for r in &results {
        assert_eq!(
            r.memory.memory_type,
            MemoryType::Decision,
            "should only return Decision type"
        );
    }
}

// ── Namespace filter ────────────────────────────────────────────────

#[test]
fn recall_filters_by_namespace() {
    let engine = CodememEngine::for_testing();
    engine
        .persist_memory(&make_memory_with_opts(
            "n1",
            "project alpha architecture notes",
            MemoryType::Context,
            Some("alpha"),
            &[],
            0.7,
            0.9,
        ))
        .unwrap();
    engine
        .persist_memory(&make_memory_with_opts(
            "n2",
            "project beta architecture notes",
            MemoryType::Context,
            Some("beta"),
            &[],
            0.7,
            0.9,
        ))
        .unwrap();

    let results = engine
        .recall(&crate::recall::RecallQuery {
            namespace_filter: Some("alpha"),
            ..crate::recall::RecallQuery::new("architecture notes", 5)
        })
        .unwrap();
    for r in &results {
        assert_eq!(
            r.memory.namespace.as_deref(),
            Some("alpha"),
            "should only return alpha namespace"
        );
    }
}

// ── Tag exclusion ───────────────────────────────────────────────────

#[test]
fn recall_excludes_tags() {
    let engine = CodememEngine::for_testing();
    engine
        .persist_memory(&make_memory_with_opts(
            "ex1",
            "temporary draft notes about architecture",
            MemoryType::Context,
            None,
            &["draft"],
            0.7,
            0.9,
        ))
        .unwrap();
    engine
        .persist_memory(&make_memory_with_opts(
            "ex2",
            "finalized notes about architecture",
            MemoryType::Context,
            None,
            &["final"],
            0.7,
            0.9,
        ))
        .unwrap();

    let exclude = vec!["draft".to_string()];
    let results = engine
        .recall(&crate::recall::RecallQuery {
            exclude_tags: &exclude,
            ..crate::recall::RecallQuery::new("architecture", 5)
        })
        .unwrap();
    for r in &results {
        assert!(
            !r.memory.tags.contains(&"draft".to_string()),
            "should not contain excluded tag"
        );
    }
}

// ── Importance threshold ────────────────────────────────────────────

#[test]
fn recall_filters_by_min_importance() {
    let engine = CodememEngine::for_testing();
    engine
        .persist_memory(&make_memory_with_opts(
            "imp1",
            "low importance note about testing",
            MemoryType::Context,
            None,
            &[],
            0.1,
            0.9,
        ))
        .unwrap();
    engine
        .persist_memory(&make_memory_with_opts(
            "imp2",
            "high importance note about testing",
            MemoryType::Context,
            None,
            &[],
            0.9,
            0.9,
        ))
        .unwrap();

    let results = engine
        .recall(&crate::recall::RecallQuery {
            min_importance: Some(0.5),
            ..crate::recall::RecallQuery::new("testing", 5)
        })
        .unwrap();
    for r in &results {
        assert!(
            r.memory.importance >= 0.5,
            "should only return memories with importance >= 0.5, got {}",
            r.memory.importance
        );
    }
}

// ── Confidence threshold ────────────────────────────────────────────

#[test]
fn recall_filters_by_min_confidence() {
    let engine = CodememEngine::for_testing();
    engine
        .persist_memory(&make_memory_with_opts(
            "conf1",
            "uncertain note about deployment",
            MemoryType::Context,
            None,
            &[],
            0.7,
            0.2,
        ))
        .unwrap();
    engine
        .persist_memory(&make_memory_with_opts(
            "conf2",
            "confident note about deployment",
            MemoryType::Context,
            None,
            &[],
            0.7,
            0.95,
        ))
        .unwrap();

    let results = engine
        .recall(&crate::recall::RecallQuery {
            min_confidence: Some(0.8),
            ..crate::recall::RecallQuery::new("deployment", 5)
        })
        .unwrap();
    for r in &results {
        assert!(
            r.memory.confidence >= 0.8,
            "should only return memories with confidence >= 0.8, got {}",
            r.memory.confidence
        );
    }
}

// ── Empty recall ────────────────────────────────────────────────────

#[test]
fn recall_scores_low_for_no_token_overlap() {
    let engine = CodememEngine::for_testing();
    // Store a memory with specific content
    engine
        .persist_memory(&make_memory("no1", "cats dogs pets animals"))
        .unwrap();
    // Store a matching memory
    engine
        .persist_memory(&make_memory(
            "match1",
            "quantum chromodynamics gluon plasma",
        ))
        .unwrap();

    let results = engine
        .recall(&crate::recall::RecallQuery::new(
            "quantum_chromodynamics_gluon_plasma",
            5,
        ))
        .unwrap();
    // BM25 fallback may still return results due to importance/confidence/recency
    // scoring components, but the matching memory should score higher
    if results.len() >= 2 {
        let match_score = results
            .iter()
            .find(|r| r.memory.id == "match1")
            .map(|r| r.score);
        let no_match_score = results
            .iter()
            .find(|r| r.memory.id == "no1")
            .map(|r| r.score);
        if let (Some(ms), Some(nms)) = (match_score, no_match_score) {
            assert!(ms >= nms, "matching memory should score >= non-matching");
        }
    }
}

// ── Entity expansion in default recall ──────────────────────────────

#[test]
fn recall_finds_entity_connected_memories() {
    let engine = CodememEngine::for_testing();

    // Create a memory that is semantically unrelated to "AuthService"
    let m = make_memory("entity-m1", "database connection pool tuning parameters");
    engine.persist_memory(&m).unwrap();

    // Create a code entity node in the graph and link the memory to it
    let now = chrono::Utc::now();
    {
        let mut graph = engine.lock_graph().unwrap();
        let code_node = codemem_core::GraphNode {
            id: "sym:AuthService".to_string(),
            kind: codemem_core::NodeKind::Class,
            label: "AuthService".to_string(),
            payload: std::collections::HashMap::new(),
            centrality: 0.0,
            memory_id: None,
            namespace: None,
            valid_from: None,
            valid_to: None,
        };
        graph.add_node(code_node).unwrap();

        let edge = Edge {
            id: "sym:AuthService-RELATES_TO-entity-m1".to_string(),
            src: "sym:AuthService".to_string(),
            dst: "entity-m1".to_string(),
            relationship: RelationshipType::RelatesTo,
            weight: 0.5,
            properties: HashMap::new(),
            created_at: now,
            valid_from: None,
            valid_to: None,
        };
        graph.add_edge(edge).unwrap();
    }

    // Query mentions the entity name — should find the connected memory
    // even though "AuthService" has zero token overlap with "database connection pool"
    let results = engine
        .recall(&crate::recall::RecallQuery::new("AuthService", 10))
        .unwrap();

    let found = results.iter().any(|r| r.memory.id == "entity-m1");
    assert!(
        found,
        "entity expansion should surface memory connected to AuthService node; got: {:?}",
        results.iter().map(|r| &r.memory.id).collect::<Vec<_>>()
    );
}

#[test]
fn resolve_entity_memories_skips_expired_edges() {
    let engine = CodememEngine::for_testing();

    let m = make_memory("entity-exp1", "expired edge memory content");
    engine.persist_memory(&m).unwrap();

    let now = chrono::Utc::now();
    let past = now - chrono::Duration::hours(1);
    {
        let mut graph = engine.lock_graph().unwrap();
        let code_node = codemem_core::GraphNode {
            id: "sym:ExpiredService".to_string(),
            kind: codemem_core::NodeKind::Class,
            label: "ExpiredService".to_string(),
            payload: std::collections::HashMap::new(),
            centrality: 0.0,
            memory_id: None,
            namespace: None,
            valid_from: None,
            valid_to: None,
        };
        graph.add_node(code_node).unwrap();

        // Create an expired edge (valid_to in the past)
        let edge = Edge {
            id: "sym:ExpiredService-RELATES_TO-entity-exp1".to_string(),
            src: "sym:ExpiredService".to_string(),
            dst: "entity-exp1".to_string(),
            relationship: RelationshipType::RelatesTo,
            weight: 0.5,
            properties: HashMap::new(),
            created_at: now,
            valid_from: None,
            valid_to: Some(past),
        };
        graph.add_edge(edge).unwrap();
    }

    // Directly test resolve_entity_memories — expired edge should be skipped
    let graph = engine.lock_graph().unwrap();
    let entity_ids = engine.resolve_entity_memories("ExpiredService", &**graph, now);
    assert!(
        !entity_ids.contains("entity-exp1"),
        "resolve_entity_memories should not return memories connected via expired edges"
    );
}

// ── Temporal edge filtering in expansion ─────────────────────────────

#[test]
fn recall_expansion_excludes_expired_edge_memories() {
    let engine = CodememEngine::for_testing();
    let now = chrono::Utc::now();
    let past = now - chrono::Duration::hours(1);

    // Create seed memory and a second memory connected by an expired edge
    let m1 = make_memory("temp-exp-seed", "temporal test seed about architecture");
    let m2 = make_memory("temp-exp-target", "expired edge target about architecture");
    engine.persist_memory(&m1).unwrap();
    engine.persist_memory(&m2).unwrap();

    // Create an expired edge between them
    {
        let mut graph = engine.lock_graph().unwrap();
        let edge = Edge {
            id: "temp-expired-edge".to_string(),
            src: "temp-exp-seed".to_string(),
            dst: "temp-exp-target".to_string(),
            relationship: RelationshipType::LeadsTo,
            weight: 0.8,
            properties: HashMap::new(),
            created_at: now,
            valid_from: None,
            valid_to: Some(past), // expired
        };
        let _ = graph.add_edge(edge);
    }

    // recall_with_expansion filters expired edges during BFS expansion
    let results = engine
        .recall_with_expansion("temporal test architecture", 10, 2, None)
        .unwrap();

    // The target memory may still appear via direct BM25 scoring (both contain "architecture"),
    // but it should NOT appear with expansion_path referencing the expired edge.
    // The key assertion: if target is found, it must be via "direct" scoring, not expansion.
    for r in &results {
        if r.result.memory.id == "temp-exp-target" {
            assert_eq!(
                r.expansion_path, "direct",
                "expired edge should not be used for expansion; got path: {}",
                r.expansion_path
            );
        }
    }
}

#[test]
fn recall_expansion_includes_active_edge_memories() {
    let engine = CodememEngine::for_testing();
    let now = chrono::Utc::now();
    let future = now + chrono::Duration::hours(24);

    let m1 = make_memory("temp-act-seed", "active edge seed about modules");
    let m2 = make_memory(
        "temp-act-target",
        "active edge target totally unrelated xyzzy",
    );
    engine.persist_memory(&m1).unwrap();
    engine.persist_memory(&m2).unwrap();

    // Create an active edge (valid_to in the future)
    {
        let mut graph = engine.lock_graph().unwrap();
        let edge = Edge {
            id: "temp-active-edge".to_string(),
            src: "temp-act-seed".to_string(),
            dst: "temp-act-target".to_string(),
            relationship: RelationshipType::LeadsTo,
            weight: 0.8,
            properties: HashMap::new(),
            created_at: now,
            valid_from: Some(now - chrono::Duration::hours(1)),
            valid_to: Some(future),
        };
        let _ = graph.add_edge(edge);
    }

    let results = engine
        .recall_with_expansion("active edge seed modules", 10, 2, None)
        .unwrap();

    let found = results
        .iter()
        .any(|r| r.result.memory.id == "temp-act-target");
    assert!(
        found,
        "memory connected via active (valid_to > now) edge should be reachable via expansion"
    );
}

#[test]
fn recall_expansion_excludes_future_valid_from_edges() {
    let engine = CodememEngine::for_testing();
    let now = chrono::Utc::now();
    let future = now + chrono::Duration::hours(24);

    let m1 = make_memory("temp-fut-seed", "future edge seed about patterns");
    let m2 = make_memory(
        "temp-fut-target",
        "future edge target completely unrelated abcdef",
    );
    engine.persist_memory(&m1).unwrap();
    engine.persist_memory(&m2).unwrap();

    // Create edge with future valid_from (not yet active)
    {
        let mut graph = engine.lock_graph().unwrap();
        let edge = Edge {
            id: "temp-future-edge".to_string(),
            src: "temp-fut-seed".to_string(),
            dst: "temp-fut-target".to_string(),
            relationship: RelationshipType::LeadsTo,
            weight: 0.8,
            properties: HashMap::new(),
            created_at: now,
            valid_from: Some(future), // not yet active
            valid_to: None,
        };
        let _ = graph.add_edge(edge);
    }

    let results = engine
        .recall_with_expansion("future edge seed patterns", 10, 2, None)
        .unwrap();

    // Target should not appear via expansion (edge not yet active)
    for r in &results {
        if r.result.memory.id == "temp-fut-target" {
            assert_eq!(
                r.expansion_path, "direct",
                "future valid_from edge should not be used for expansion; got path: {}",
                r.expansion_path
            );
        }
    }
}

// ── Recall with expansion ───────────────────────────────────────────

#[test]
fn recall_with_expansion_finds_graph_connected_memories() {
    let engine = CodememEngine::for_testing();

    // Create two memories
    let m1 = make_memory("exp1", "primary architecture decision about modules");
    let m2 = make_memory("exp2", "secondary implementation detail about modules");
    engine.persist_memory(&m1).unwrap();
    engine.persist_memory(&m2).unwrap();

    // Link them in the graph
    let now = chrono::Utc::now();
    {
        let mut graph = engine.lock_graph().unwrap();
        let edge = Edge {
            id: "exp1-exp2".to_string(),
            src: "exp1".to_string(),
            dst: "exp2".to_string(),
            relationship: RelationshipType::LeadsTo,
            weight: 0.8,
            properties: HashMap::new(),
            created_at: now,
            valid_from: None,
            valid_to: None,
        };
        let _ = graph.add_edge(edge);
    }

    let results = engine
        .recall_with_expansion("architecture decision", 10, 2, None)
        .unwrap();
    assert!(!results.is_empty(), "should find memories via expansion");
}

#[test]
fn recall_with_expansion_namespace_filter() {
    let engine = CodememEngine::for_testing();

    let m1 = make_memory_with_opts(
        "ens1",
        "expansion test alpha namespace",
        MemoryType::Context,
        Some("alpha"),
        &[],
        0.7,
        0.9,
    );
    let m2 = make_memory_with_opts(
        "ens2",
        "expansion test beta namespace",
        MemoryType::Context,
        Some("beta"),
        &[],
        0.7,
        0.9,
    );
    engine.persist_memory(&m1).unwrap();
    engine.persist_memory(&m2).unwrap();

    let results = engine
        .recall_with_expansion("expansion test", 10, 1, Some("alpha"))
        .unwrap();
    for r in &results {
        assert_eq!(
            r.result.memory.namespace.as_deref(),
            Some("alpha"),
            "expansion should respect namespace filter"
        );
    }
}

// ── Namespace stats ─────────────────────────────────────────────────

#[test]
fn namespace_stats_empty_namespace() {
    let engine = CodememEngine::for_testing();
    let stats = engine.namespace_stats("nonexistent").unwrap();
    assert_eq!(stats.count, 0);
    assert_eq!(stats.avg_importance, 0.0);
    assert_eq!(stats.avg_confidence, 0.0);
    assert!(stats.type_distribution.is_empty());
    assert!(stats.oldest.is_none());
    assert!(stats.newest.is_none());
}

#[test]
fn namespace_stats_computes_correctly() {
    let engine = CodememEngine::for_testing();

    engine
        .persist_memory(&make_memory_with_opts(
            "ns1",
            "first memory in stats ns",
            MemoryType::Decision,
            Some("stats-ns"),
            &["tag-a"],
            0.6,
            0.8,
        ))
        .unwrap();
    engine
        .persist_memory(&make_memory_with_opts(
            "ns2",
            "second memory in stats ns",
            MemoryType::Context,
            Some("stats-ns"),
            &["tag-a", "tag-b"],
            0.8,
            1.0,
        ))
        .unwrap();

    let stats = engine.namespace_stats("stats-ns").unwrap();
    assert_eq!(stats.count, 2);
    assert!((stats.avg_importance - 0.7).abs() < 0.01);
    assert!((stats.avg_confidence - 0.9).abs() < 0.01);
    assert_eq!(stats.type_distribution.get("decision"), Some(&1));
    assert_eq!(stats.type_distribution.get("context"), Some(&1));
    assert_eq!(stats.tag_frequency.get("tag-a"), Some(&2));
    assert_eq!(stats.tag_frequency.get("tag-b"), Some(&1));
    assert!(stats.oldest.is_some());
    assert!(stats.newest.is_some());
}

// ── Delete namespace ────────────────────────────────────────────────

#[test]
fn delete_namespace_removes_all_memories() {
    let engine = CodememEngine::for_testing();

    for i in 0..3 {
        engine
            .persist_memory(&make_memory_with_opts(
                &format!("del-{i}"),
                &format!("memory {i} to delete"),
                MemoryType::Context,
                Some("to-delete"),
                &[],
                0.7,
                0.9,
            ))
            .unwrap();
    }
    // One in a different namespace
    engine
        .persist_memory(&make_memory_with_opts(
            "keep-1",
            "memory to keep",
            MemoryType::Context,
            Some("keep-ns"),
            &[],
            0.7,
            0.9,
        ))
        .unwrap();

    let deleted = engine.delete_namespace("to-delete").unwrap();
    assert_eq!(deleted, 3, "should delete exactly 3 memories");

    // Verify they're gone from storage
    for i in 0..3 {
        let m = engine.storage.get_memory(&format!("del-{i}")).unwrap();
        assert!(m.is_none(), "deleted memory should be gone");
    }

    // Verify the other namespace is intact
    let kept = engine.storage.get_memory("keep-1").unwrap();
    assert!(kept.is_some(), "memory in other namespace should remain");
}

#[test]
fn delete_namespace_returns_zero_for_empty() {
    let engine = CodememEngine::for_testing();
    let deleted = engine.delete_namespace("nonexistent-ns").unwrap();
    assert_eq!(deleted, 0);
}

// ── is_edge_active ──────────────────────────────────────────────────

#[test]
fn is_edge_active_no_bounds() {
    let now = chrono::Utc::now();
    let edge = Edge {
        id: "e1".to_string(),
        src: "a".to_string(),
        dst: "b".to_string(),
        relationship: RelationshipType::RelatesTo,
        weight: 1.0,
        properties: HashMap::new(),
        created_at: now,
        valid_from: None,
        valid_to: None,
    };
    assert!(
        crate::recall::is_edge_active(&edge, now),
        "edge with no bounds should be active"
    );
}

#[test]
fn is_edge_active_expired() {
    let now = chrono::Utc::now();
    let past = now - chrono::Duration::hours(1);
    let edge = Edge {
        id: "e2".to_string(),
        src: "a".to_string(),
        dst: "b".to_string(),
        relationship: RelationshipType::RelatesTo,
        weight: 1.0,
        properties: HashMap::new(),
        created_at: now,
        valid_from: None,
        valid_to: Some(past),
    };
    assert!(
        !crate::recall::is_edge_active(&edge, now),
        "edge with valid_to in the past should be inactive"
    );
}

#[test]
fn is_edge_active_future_start() {
    let now = chrono::Utc::now();
    let future = now + chrono::Duration::hours(1);
    let edge = Edge {
        id: "e3".to_string(),
        src: "a".to_string(),
        dst: "b".to_string(),
        relationship: RelationshipType::RelatesTo,
        weight: 1.0,
        properties: HashMap::new(),
        created_at: now,
        valid_from: Some(future),
        valid_to: None,
    };
    assert!(
        !crate::recall::is_edge_active(&edge, now),
        "edge with valid_from in the future should be inactive"
    );
}

#[test]
fn is_edge_active_within_window() {
    let now = chrono::Utc::now();
    let past = now - chrono::Duration::hours(1);
    let future = now + chrono::Duration::hours(1);
    let edge = Edge {
        id: "e4".to_string(),
        src: "a".to_string(),
        dst: "b".to_string(),
        relationship: RelationshipType::RelatesTo,
        weight: 1.0,
        properties: HashMap::new(),
        created_at: now,
        valid_from: Some(past),
        valid_to: Some(future),
    };
    assert!(
        crate::recall::is_edge_active(&edge, now),
        "edge within valid window should be active"
    );
}