shodh-memory 0.2.0

Persistent cognitive memory for AI agents and robots — Hebbian learning, knowledge graph, spatial recall. Zenoh/ROS2 native. Single binary, runs offline.
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
//! Comprehensive Test Suite for Adaptive Memory Systems
//!
//! Tests the three core adaptive memory mechanisms:
//! - Outcome Feedback System (Hebbian learning through task outcomes)
//! - Semantic Consolidation (episodic → semantic fact extraction)
//! - Anticipatory Prefetch (context-aware cache warming)
//! - NER integration for entity extraction
//!
//! These tests verify that the memory system can learn, adapt, and improve over time.

use chrono::{Duration, Timelike, Utc};
use tempfile::TempDir;
use uuid::Uuid;

use shodh_memory::embeddings::ner::{NerConfig, NeuralNer};
use shodh_memory::memory::{
    compression::{ConsolidationResult, FactType, SemanticConsolidator, SemanticFact},
    retrieval::{
        AnticipatoryPrefetch, PrefetchContext, PrefetchReason, PrefetchResult, ReinforcementStats,
        RetrievalOutcome,
    },
    types::{
        CodeContext, ContextId, ConversationContext, DocumentContext, EnvironmentContext,
        Experience, ExperienceType, ProjectContext, Query, RetrievalMode, RichContext,
        SemanticContext, TemporalContext, UserContext,
    },
    Memory, MemoryConfig, MemoryId, MemorySystem,
};

/// Create fallback NER instance for testing
fn setup_fallback_ner() -> NeuralNer {
    let config = NerConfig::default();
    NeuralNer::new_fallback(config)
}

/// Create experience with NER-extracted entities
fn create_experience_with_ner(
    content: &str,
    exp_type: ExperienceType,
    ner: &NeuralNer,
) -> Experience {
    let entities = ner.extract(content).unwrap_or_default();
    let entity_names: Vec<String> = entities.iter().map(|e| e.text.clone()).collect();
    Experience {
        experience_type: exp_type,
        content: content.to_string(),
        entities: entity_names,
        ..Default::default()
    }
}

// ============================================================================
// TEST INFRASTRUCTURE
// ============================================================================

fn create_test_system() -> (MemorySystem, TempDir) {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let config = MemoryConfig {
        storage_path: temp_dir.path().to_path_buf(),
        working_memory_size: 100,
        session_memory_size_mb: 50,
        max_heap_per_user_mb: 500,
        auto_compress: false,
        compression_age_days: 7,
        importance_threshold: 0.3,
    };
    let system = MemorySystem::new(config, None).expect("Failed to create memory system");
    (system, temp_dir)
}

fn create_experience(content: &str, exp_type: ExperienceType, entities: Vec<&str>) -> Experience {
    Experience {
        experience_type: exp_type,
        content: content.to_string(),
        entities: entities.into_iter().map(|s| s.to_string()).collect(),
        ..Default::default()
    }
}

fn create_learning_experience(content: &str, entities: Vec<&str>) -> Experience {
    create_experience(content, ExperienceType::Learning, entities)
}

fn create_decision_experience(content: &str, entities: Vec<&str>) -> Experience {
    create_experience(content, ExperienceType::Decision, entities)
}

fn create_error_experience(content: &str, entities: Vec<&str>) -> Experience {
    create_experience(content, ExperienceType::Error, entities)
}

fn create_conversation_experience(content: &str, entities: Vec<&str>) -> Experience {
    create_experience(content, ExperienceType::Conversation, entities)
}

fn create_memory(
    content: &str,
    exp_type: ExperienceType,
    entities: Vec<&str>,
    importance: f32,
) -> Memory {
    let exp = create_experience(content, exp_type, entities);
    Memory::new(
        MemoryId(Uuid::new_v4()),
        exp,
        importance,
        None,
        None,
        None,
        None,
    )
}

fn create_rich_context(project_id: Option<String>, code_ctx: Option<CodeContext>) -> RichContext {
    let now = Utc::now();
    RichContext {
        id: ContextId(Uuid::new_v4()),
        conversation: ConversationContext::default(),
        user: UserContext::default(),
        project: ProjectContext {
            project_id,
            ..Default::default()
        },
        temporal: TemporalContext::default(),
        semantic: SemanticContext::default(),
        code: code_ctx.unwrap_or_default(),
        document: DocumentContext::default(),
        environment: EnvironmentContext::default(),
        emotional: Default::default(),
        source: Default::default(),
        episode: Default::default(),
        parent: None,
        embeddings: None,
        decay_rate: 0.1,
        created_at: now,
        updated_at: now,
    }
}

// ============================================================================
// OUTCOME FEEDBACK SYSTEM TESTS
// ============================================================================

#[test]
fn test_retrieval_outcome_enum_values() {
    let helpful = RetrievalOutcome::Helpful;
    let misleading = RetrievalOutcome::Misleading;
    let neutral = RetrievalOutcome::Neutral;

    assert!(helpful != misleading);
    assert!(helpful != neutral);
    assert!(misleading != neutral);
}

#[test]
fn test_retrieval_outcome_default() {
    let default = RetrievalOutcome::default();
    assert_eq!(default, RetrievalOutcome::Neutral);
}

#[test]
fn test_tracked_retrieval_creation() {
    let (mut system, _temp_dir) = create_test_system();

    let exp1 = create_learning_experience(
        "Rust ownership prevents memory leaks",
        vec!["rust", "ownership"],
    );
    let exp2 = create_learning_experience(
        "The borrow checker enforces safety",
        vec!["rust", "borrow_checker"],
    );

    system.remember(exp1, None).expect("Failed to record");
    system.remember(exp2, None).expect("Failed to record");

    let query = Query {
        query_text: Some("rust memory safety".to_string()),
        max_results: 5,
        ..Default::default()
    };

    let tracked = system
        .recall_tracked(&query)
        .expect("Failed to retrieve tracked");

    assert!(!tracked.retrieval_id.is_empty());
    assert!(!tracked.memories.is_empty());
    assert!(tracked.query_fingerprint != 0);
}

#[test]
fn test_tracked_retrieval_memory_ids() {
    let (mut system, _temp_dir) = create_test_system();

    let exp = create_learning_experience("Test content", vec!["test"]);
    let id = system.remember(exp, None).expect("Failed to record");

    let query = Query {
        query_text: Some("test content".to_string()),
        max_results: 5,
        ..Default::default()
    };

    let tracked = system.recall_tracked(&query).expect("Failed");
    let ids = tracked.memory_ids();

    assert!(ids.iter().any(|i| i == &id));
}

#[test]
fn test_reinforce_helpful_outcome() {
    let (mut system, _temp_dir) = create_test_system();

    let exp1 =
        create_learning_experience("API endpoint for user authentication", vec!["api", "auth"]);
    let exp2 = create_learning_experience("JWT token validation process", vec!["jwt", "auth"]);
    let exp3 = create_learning_experience("OAuth2 flow implementation", vec!["oauth", "auth"]);

    let id1 = system.remember(exp1, None).expect("Failed");
    let id2 = system.remember(exp2, None).expect("Failed");
    let id3 = system.remember(exp3, None).expect("Failed");

    let stats = system
        .reinforce_recall(
            &[id1.clone(), id2.clone(), id3.clone()],
            RetrievalOutcome::Helpful,
        )
        .expect("Failed to reinforce");

    assert_eq!(stats.memories_processed, 3);
    assert_eq!(stats.importance_boosts, 3);
    assert_eq!(stats.importance_decays, 0);
    assert_eq!(stats.associations_strengthened, 3);
    assert_eq!(stats.outcome, RetrievalOutcome::Helpful);
}

#[test]
fn test_reinforce_misleading_outcome() {
    let (mut system, _temp_dir) = create_test_system();

    let exp = create_learning_experience("Misleading information", vec!["test"]);
    let id = system.remember(exp, None).expect("Failed");

    let initial_importance = {
        let query = Query {
            query_text: Some("misleading".to_string()),
            max_results: 1,
            ..Default::default()
        };
        let results = system.recall(&query).expect("Failed");
        results[0].importance()
    };

    // Apply multiple misleading reinforcements to ensure measurable decay
    for _ in 0..5 {
        let stats = system
            .reinforce_recall(&[id.clone()], RetrievalOutcome::Misleading)
            .expect("Failed");
        assert_eq!(stats.memories_processed, 1);
        assert_eq!(stats.importance_decays, 1);
        assert_eq!(stats.importance_boosts, 0);
        assert_eq!(stats.associations_strengthened, 0);
    }

    let query = Query {
        query_text: Some("misleading".to_string()),
        max_results: 1,
        ..Default::default()
    };
    let results = system.recall(&query).expect("Failed");
    let final_importance = results[0].importance();
    // After 5 decays of 10% each: 0.9^5 = 0.59 of original
    assert!(
        final_importance < initial_importance * 0.7,
        "importance should decay: {} < {} * 0.7 = {}",
        final_importance,
        initial_importance,
        initial_importance * 0.7
    );
}

#[test]
fn test_reinforce_neutral_outcome() {
    let (mut system, _temp_dir) = create_test_system();

    let exp1 = create_learning_experience("First neutral memory", vec!["neutral"]);
    let exp2 = create_learning_experience("Second neutral memory", vec!["neutral"]);

    let id1 = system.remember(exp1, None).expect("Failed");
    let id2 = system.remember(exp2, None).expect("Failed");

    let stats = system
        .reinforce_recall(&[id1, id2], RetrievalOutcome::Neutral)
        .expect("Failed");

    assert_eq!(stats.memories_processed, 2);
    assert_eq!(stats.associations_strengthened, 1);
    assert_eq!(stats.importance_boosts, 0);
    assert_eq!(stats.importance_decays, 0);
}

#[test]
fn test_reinforce_empty_list() {
    let (system, _temp_dir) = create_test_system();

    let stats = system
        .reinforce_recall(&[], RetrievalOutcome::Helpful)
        .expect("Failed");

    assert_eq!(stats.memories_processed, 0);
    assert_eq!(stats.associations_strengthened, 0);
}

#[test]
fn test_reinforce_tracked_convenience() {
    let (mut system, _temp_dir) = create_test_system();

    let exp = create_learning_experience("Tracked memory test", vec!["tracked"]);
    system.remember(exp, None).expect("Failed");

    let query = Query {
        query_text: Some("tracked memory".to_string()),
        max_results: 5,
        ..Default::default()
    };

    let tracked = system.recall_tracked(&query).expect("Failed");
    let stats = system
        .reinforce_recall_tracked(&tracked, RetrievalOutcome::Helpful)
        .expect("Failed");

    assert!(stats.memories_processed > 0);
}

#[test]
fn test_importance_boost_cumulative() {
    let (mut system, _temp_dir) = create_test_system();

    let exp = create_learning_experience("Memory that gets boosted multiple times", vec!["boost"]);
    let id = system.remember(exp, None).expect("Failed");

    // Verify the boost mechanism reports correct stats
    let mut total_boosts = 0;
    for _ in 0..10 {
        let stats = system
            .reinforce_recall(&[id.clone()], RetrievalOutcome::Helpful)
            .expect("Failed");
        assert_eq!(stats.importance_boosts, 1);
        total_boosts += stats.importance_boosts;
    }

    // Verify all 10 boosts were applied (stats counted correctly)
    assert_eq!(total_boosts, 10);
}

#[test]
fn test_importance_decay_floor() {
    let (mut system, _temp_dir) = create_test_system();

    let exp = create_learning_experience("Memory that gets decayed to minimum", vec!["decay"]);
    let id = system.remember(exp, None).expect("Failed");

    // Verify decay mechanism reports correctly
    let mut total_decays = 0;
    for _ in 0..20 {
        let stats = system
            .reinforce_recall(&[id.clone()], RetrievalOutcome::Misleading)
            .expect("Failed");
        assert_eq!(stats.importance_decays, 1);
        total_decays += 1;
    }

    // Verify all 20 decays were processed
    assert_eq!(total_decays, 20);
}

#[test]
fn test_coactivation_strengthens_graph() {
    let (mut system, _temp_dir) = create_test_system();

    let exp1 = create_learning_experience("Database connection pooling", vec!["database"]);
    let exp2 = create_learning_experience("Query optimization techniques", vec!["database"]);
    let exp3 = create_learning_experience("Index design patterns", vec!["database"]);

    let id1 = system.remember(exp1, None).expect("Failed");
    let id2 = system.remember(exp2, None).expect("Failed");
    let id3 = system.remember(exp3, None).expect("Failed");

    let initial_stats = system.graph_stats();

    for _ in 0..5 {
        system
            .reinforce_recall(
                &[id1.clone(), id2.clone(), id3.clone()],
                RetrievalOutcome::Helpful,
            )
            .expect("Failed");
    }

    let final_stats = system.graph_stats();
    assert!(final_stats.edge_count >= initial_stats.edge_count);
}

// ============================================================================
// SEMANTIC CONSOLIDATION TESTS
// ============================================================================

#[test]
fn test_semantic_consolidator_creation() {
    let _consolidator = SemanticConsolidator::new();
}

#[test]
fn test_semantic_consolidator_with_thresholds() {
    let _consolidator = SemanticConsolidator::with_thresholds(3, 14);
}

#[test]
fn test_consolidate_empty_memories() {
    let consolidator = SemanticConsolidator::new();
    let result = consolidator.consolidate(&[]);

    assert_eq!(result.memories_processed, 0);
    assert_eq!(result.facts_extracted, 0);
}

#[test]
fn test_consolidate_young_memories() {
    let consolidator = SemanticConsolidator::new();

    let memories: Vec<Memory> = vec![
        create_memory(
            "Young memory 1",
            ExperienceType::Learning,
            vec!["test"],
            0.5,
        ),
        create_memory(
            "Young memory 2",
            ExperienceType::Learning,
            vec!["test"],
            0.5,
        ),
    ];

    let result = consolidator.consolidate(&memories);
    assert_eq!(result.memories_processed, 2);
    assert_eq!(result.facts_extracted, 0);
}

#[test]
fn test_fact_type_classification() {
    let pref_pattern = "preference: use tabs over spaces";
    let fact_type = classify_pattern(pref_pattern);
    assert_eq!(fact_type, FactType::Preference);

    let cap_pattern = "the system can handle 1000 requests per second";
    let fact_type = classify_pattern(cap_pattern);
    assert_eq!(fact_type, FactType::Capability);

    let rel_pattern = "authentication relates to user sessions";
    let fact_type = classify_pattern(rel_pattern);
    assert_eq!(fact_type, FactType::Relationship);

    let proc_pattern = "to deploy the application run npm build";
    let fact_type = classify_pattern(proc_pattern);
    assert_eq!(fact_type, FactType::Procedure);

    let def_pattern = "a mutex is a synchronization primitive";
    let fact_type = classify_pattern(def_pattern);
    assert_eq!(fact_type, FactType::Definition);
}

fn classify_pattern(pattern: &str) -> FactType {
    let lower = pattern.to_lowercase();

    if lower.starts_with("preference:") || lower.contains("prefer") || lower.contains("like") {
        FactType::Preference
    } else if lower.contains("can ") || lower.contains("able to") || lower.contains("supports") {
        FactType::Capability
    } else if lower.contains("relates to")
        || lower.contains("depends on")
        || lower.contains("connects")
    {
        FactType::Relationship
    } else if lower.contains("to ")
        || lower.contains("run ")
        || lower.contains("execute")
        || lower.contains("deploy")
    {
        FactType::Procedure
    } else if lower.contains(" is ") || lower.contains(" are ") || lower.contains("means") {
        FactType::Definition
    } else {
        FactType::Pattern
    }
}

#[test]
fn test_semantic_fact_structure() {
    let fact = SemanticFact {
        id: "fact_001".to_string(),
        fact: "Users prefer dark mode".to_string(),
        confidence: 0.85,
        support_count: 3,
        source_memories: vec![MemoryId(Uuid::new_v4()), MemoryId(Uuid::new_v4())],
        related_entities: vec!["user".to_string(), "dark_mode".to_string()],
        created_at: Utc::now(),
        last_reinforced: Utc::now(),
        fact_type: FactType::Preference,
    };

    assert_eq!(fact.id, "fact_001");
    assert_eq!(fact.confidence, 0.85);
    assert_eq!(fact.support_count, 3);
    assert_eq!(fact.source_memories.len(), 2);
    assert_eq!(fact.fact_type, FactType::Preference);
}

#[test]
fn test_reinforce_fact_increases_confidence() {
    let consolidator = SemanticConsolidator::new();

    let mut fact = SemanticFact {
        id: "test_fact".to_string(),
        fact: "Test fact".to_string(),
        confidence: 0.5,
        support_count: 2,
        source_memories: vec![],
        related_entities: vec![],
        created_at: Utc::now(),
        last_reinforced: Utc::now() - Duration::days(1),
        fact_type: FactType::Pattern,
    };

    let initial_confidence = fact.confidence;
    let initial_support = fact.support_count;

    let memory = create_memory(
        "Supporting evidence",
        ExperienceType::Learning,
        vec!["test"],
        0.5,
    );
    consolidator.reinforce_fact(&mut fact, &memory);

    assert!(fact.confidence > initial_confidence);
    assert_eq!(fact.support_count, initial_support + 1);
}

#[test]
fn test_reinforce_fact_adds_source() {
    let consolidator = SemanticConsolidator::new();

    let mut fact = SemanticFact {
        id: "test_fact".to_string(),
        fact: "Test".to_string(),
        confidence: 0.5,
        support_count: 1,
        source_memories: vec![],
        related_entities: vec![],
        created_at: Utc::now(),
        last_reinforced: Utc::now(),
        fact_type: FactType::Pattern,
    };

    let memory = create_memory("Evidence", ExperienceType::Learning, vec!["test"], 0.5);

    assert!(fact.source_memories.is_empty());
    consolidator.reinforce_fact(&mut fact, &memory);
    assert_eq!(fact.source_memories.len(), 1);
}

#[test]
fn test_should_decay_fact_old_unreinforced() {
    let consolidator = SemanticConsolidator::new();

    let old_fact = SemanticFact {
        id: "old_fact".to_string(),
        fact: "Old unused fact".to_string(),
        // Keep confidence at the deletion threshold so modest additional decay
        // from long inactivity deterministically crosses the cutoff.
        confidence: 0.1,
        support_count: 1,
        source_memories: vec![],
        related_entities: vec![],
        created_at: Utc::now() - Duration::days(365),
        last_reinforced: Utc::now() - Duration::days(100),
        fact_type: FactType::Pattern,
    };

    assert!(consolidator.should_decay_fact(&old_fact));
}

#[test]
fn test_should_not_decay_high_confidence_fact() {
    let consolidator = SemanticConsolidator::new();

    let strong_fact = SemanticFact {
        id: "strong_fact".to_string(),
        fact: "Well-established fact".to_string(),
        confidence: 0.95,
        support_count: 10,
        source_memories: vec![],
        related_entities: vec![],
        created_at: Utc::now() - Duration::days(30),
        last_reinforced: Utc::now() - Duration::days(30),
        fact_type: FactType::Definition,
    };

    assert!(!consolidator.should_decay_fact(&strong_fact));
}

#[test]
fn test_fact_type_default() {
    let default = FactType::default();
    assert_eq!(default, FactType::Pattern);
}

#[test]
fn test_consolidation_result_structure() {
    let result = ConsolidationResult {
        memories_processed: 10,
        facts_extracted: 3,
        facts_reinforced: 5,
        new_fact_ids: vec!["f1".to_string(), "f2".to_string(), "f3".to_string()],
        new_facts: Vec::new(),
    };

    assert_eq!(result.memories_processed, 10);
    assert_eq!(result.facts_extracted, 3);
    assert_eq!(result.facts_reinforced, 5);
    assert_eq!(result.new_fact_ids.len(), 3);
}

// ============================================================================
// ANTICIPATORY PREFETCH TESTS
// ============================================================================

#[test]
fn test_prefetch_context_default() {
    let ctx = PrefetchContext::default();

    assert!(ctx.project_id.is_none());
    assert!(ctx.current_file.is_none());
    assert!(ctx.recent_entities.is_empty());
    assert!(ctx.hour_of_day.is_none());
    assert!(ctx.day_of_week.is_none());
    assert!(ctx.task_type.is_none());
}

#[test]
fn test_prefetch_context_from_current_time() {
    let ctx = PrefetchContext::from_current_time();

    assert!(ctx.hour_of_day.is_some());
    assert!(ctx.day_of_week.is_some());

    let hour = ctx.hour_of_day.unwrap();
    assert!(hour <= 23);

    let day = ctx.day_of_week.unwrap();
    assert!(day <= 6);
}

#[test]
fn test_anticipatory_prefetch_creation() {
    let _prefetch = AnticipatoryPrefetch::new();
}

#[test]
fn test_anticipatory_prefetch_with_limit() {
    let _prefetch = AnticipatoryPrefetch::with_limit(50);
}

#[test]
fn test_generate_prefetch_query_project() {
    let prefetch = AnticipatoryPrefetch::new();

    let ctx = PrefetchContext {
        project_id: Some("auth-service".to_string()),
        ..Default::default()
    };

    let query = prefetch.generate_prefetch_query(&ctx);
    assert!(query.is_some());

    let q = query.unwrap();
    assert!(q.query_text.is_some());
    assert!(q.query_text.as_ref().unwrap().contains("auth-service"));
}

#[test]
fn test_generate_prefetch_query_entities() {
    let prefetch = AnticipatoryPrefetch::new();

    let ctx = PrefetchContext {
        recent_entities: vec!["User".to_string(), "Session".to_string()],
        ..Default::default()
    };

    let query = prefetch.generate_prefetch_query(&ctx);
    assert!(query.is_some());

    let q = query.unwrap();
    assert!(q.query_text.is_some());
    let text = q.query_text.as_ref().unwrap();
    assert!(text.contains("User") || text.contains("Session"));
}

#[test]
fn test_generate_prefetch_query_file() {
    let prefetch = AnticipatoryPrefetch::new();

    let ctx = PrefetchContext {
        current_file: Some("/src/auth/login.rs".to_string()),
        ..Default::default()
    };

    let query = prefetch.generate_prefetch_query(&ctx);
    assert!(query.is_some());

    let q = query.unwrap();
    assert!(q.query_text.is_some());
    assert!(q.query_text.as_ref().unwrap().contains("login.rs"));
}

#[test]
fn test_generate_prefetch_query_temporal() {
    let prefetch = AnticipatoryPrefetch::new();

    let ctx = PrefetchContext {
        hour_of_day: Some(14),
        day_of_week: Some(1),
        ..Default::default()
    };

    let query = prefetch.generate_prefetch_query(&ctx);
    assert!(query.is_some());

    let q = query.unwrap();
    assert!(q.time_range.is_some());
    assert_eq!(q.retrieval_mode, RetrievalMode::Temporal);
}

#[test]
fn test_generate_prefetch_query_empty_context() {
    let prefetch = AnticipatoryPrefetch::new();
    let ctx = PrefetchContext::default();

    let query = prefetch.generate_prefetch_query(&ctx);
    assert!(query.is_none());
}

#[test]
fn test_prefetch_priority_order() {
    let prefetch = AnticipatoryPrefetch::new();

    let ctx = PrefetchContext {
        project_id: Some("priority-project".to_string()),
        recent_entities: vec!["Entity1".to_string()],
        current_file: Some("file.rs".to_string()),
        hour_of_day: Some(10),
        day_of_week: Some(2),
        ..Default::default()
    };

    let query = prefetch.generate_prefetch_query(&ctx);
    assert!(query.is_some());

    let q = query.unwrap();
    assert!(q.query_text.as_ref().unwrap().contains("priority-project"));
}

#[test]
fn test_relevance_score_project_match() {
    let prefetch = AnticipatoryPrefetch::new();

    let mut exp = create_learning_experience("Auth implementation", vec!["auth"]);
    exp.context = Some(create_rich_context(Some("auth-project".to_string()), None));

    let memory = Memory::new(MemoryId(Uuid::new_v4()), exp, 0.7, None, None, None, None);

    let ctx = PrefetchContext {
        project_id: Some("auth-project".to_string()),
        ..Default::default()
    };

    let score = prefetch.relevance_score(&memory, &ctx);
    assert!(score >= 0.4);
}

#[test]
fn test_relevance_score_entity_overlap() {
    let prefetch = AnticipatoryPrefetch::new();

    let exp = create_learning_experience("User authentication", vec!["User", "Auth"]);
    let memory = Memory::new(MemoryId(Uuid::new_v4()), exp, 0.7, None, None, None, None);

    let ctx = PrefetchContext {
        recent_entities: vec!["User".to_string(), "Session".to_string()],
        ..Default::default()
    };

    let score = prefetch.relevance_score(&memory, &ctx);
    assert!(score > 0.0);
}

#[test]
fn test_relevance_score_file_mention() {
    let prefetch = AnticipatoryPrefetch::new();

    let exp = create_learning_experience(
        "The login.rs file handles user authentication",
        vec!["login", "auth"],
    );
    let memory = Memory::new(MemoryId(Uuid::new_v4()), exp, 0.7, None, None, None, None);

    let ctx = PrefetchContext {
        current_file: Some("login.rs".to_string()),
        ..Default::default()
    };

    let score = prefetch.relevance_score(&memory, &ctx);
    assert!(score > 0.0);
}

#[test]
fn test_relevance_score_recency_boost() {
    let prefetch = AnticipatoryPrefetch::new();

    let exp = create_learning_experience("Recent memory", vec!["recent"]);
    let memory = Memory::new(MemoryId(Uuid::new_v4()), exp, 0.7, None, None, None, None);

    let ctx = PrefetchContext::default();
    let score = prefetch.relevance_score(&memory, &ctx);
    assert!(score >= 0.1);
}

#[test]
fn test_relevance_score_maximum() {
    let prefetch = AnticipatoryPrefetch::new();

    let mut exp =
        create_learning_experience("The login.rs file is important", vec!["User", "Auth"]);

    let code_ctx = CodeContext {
        current_file: Some("login.rs".to_string()),
        related_files: vec!["login.rs".to_string()],
        ..Default::default()
    };

    exp.context = Some(create_rich_context(
        Some("test-project".to_string()),
        Some(code_ctx),
    ));

    let memory = Memory::new(MemoryId(Uuid::new_v4()), exp, 0.7, None, None, None, None);

    let ctx = PrefetchContext {
        project_id: Some("test-project".to_string()),
        recent_entities: vec!["User".to_string(), "Auth".to_string()],
        current_file: Some("login.rs".to_string()),
        hour_of_day: Some(Utc::now().hour()),
        ..Default::default()
    };

    let score = prefetch.relevance_score(&memory, &ctx);
    assert!(score <= 1.0);
}

#[test]
fn test_prefetch_result_structure() {
    let result = PrefetchResult {
        prefetched_ids: vec![MemoryId(Uuid::new_v4()), MemoryId(Uuid::new_v4())],
        reason: PrefetchReason::Project("test".to_string()),
        cache_hits: 1,
        fetches: 1,
    };

    assert_eq!(result.prefetched_ids.len(), 2);
    assert_eq!(result.cache_hits, 1);
    assert_eq!(result.fetches, 1);
}

#[test]
fn test_prefetch_reason_variants() {
    let _project = PrefetchReason::Project("test".to_string());
    let _files = PrefetchReason::RelatedFiles;
    let _entities = PrefetchReason::SharedEntities;
    let _temporal = PrefetchReason::TemporalPattern;
    let _assoc = PrefetchReason::AssociatedMemories;
    let _query = PrefetchReason::QueryPrediction;
    let _mixed = PrefetchReason::Mixed;
}

#[test]
fn test_prefetch_reason_default() {
    let default = PrefetchReason::default();
    assert!(matches!(default, PrefetchReason::Mixed));
}

// ============================================================================
// INTEGRATION TESTS - Combining All Systems
// ============================================================================

#[test]
fn test_adaptive_memory_workflow() {
    let (mut system, _temp_dir) = create_test_system();

    let experiences = vec![
        create_learning_experience(
            "Rust ownership model prevents memory bugs",
            vec!["rust", "ownership"],
        ),
        create_learning_experience(
            "The borrow checker validates references at compile time",
            vec!["rust", "borrow_checker"],
        ),
        create_decision_experience(
            "Decided to use async/await for concurrency",
            vec!["rust", "async"],
        ),
        create_error_experience(
            "Error: lifetime mismatch in function return",
            vec!["rust", "lifetime"],
        ),
    ];

    let mut ids = Vec::new();
    for exp in experiences {
        ids.push(system.remember(exp, None).expect("Failed to record"));
    }

    let query = Query {
        query_text: Some("rust memory safety borrow".to_string()),
        max_results: 5,
        ..Default::default()
    };

    let tracked = system.recall_tracked(&query).expect("Failed");
    assert!(!tracked.memories.is_empty());

    let stats = system
        .reinforce_recall_tracked(&tracked, RetrievalOutcome::Helpful)
        .expect("Failed");
    assert!(stats.memories_processed > 0);

    let graph_stats = system.graph_stats();
    assert!(graph_stats.node_count > 0 || graph_stats.edge_count >= 0);

    let prefetch = AnticipatoryPrefetch::new();
    let ctx = PrefetchContext {
        recent_entities: vec!["rust".to_string()],
        ..Default::default()
    };

    let prefetch_query = prefetch.generate_prefetch_query(&ctx);
    assert!(prefetch_query.is_some());
}

#[test]
fn test_graph_maintenance() {
    let (mut system, _temp_dir) = create_test_system();

    let exp1 = create_learning_experience("First memory", vec!["test"]);
    let exp2 = create_learning_experience("Second memory", vec!["test"]);

    let id1 = system.remember(exp1, None).expect("Failed");
    let id2 = system.remember(exp2, None).expect("Failed");

    system
        .reinforce_recall(&[id1, id2], RetrievalOutcome::Helpful)
        .expect("Failed");

    system.graph_maintenance();

    let stats = system.graph_stats();
    assert!(stats.node_count >= 0);
}

#[test]
fn test_reinforcement_stats_structure() {
    let stats = ReinforcementStats {
        memories_processed: 5,
        associations_strengthened: 10,
        importance_boosts: 3,
        importance_decays: 2,
        outcome: RetrievalOutcome::Helpful,
        persist_failures: 0,
    };

    assert_eq!(stats.memories_processed, 5);
    assert_eq!(stats.associations_strengthened, 10);
    assert_eq!(stats.importance_boosts, 3);
    assert_eq!(stats.importance_decays, 2);
    assert_eq!(stats.outcome, RetrievalOutcome::Helpful);
    assert_eq!(stats.persist_failures, 0);
}

// ============================================================================
// STRESS TESTS
// ============================================================================

#[test]
fn test_high_volume_reinforcement() {
    let (mut system, _temp_dir) = create_test_system();

    let mut ids = Vec::new();
    for i in 0..50 {
        let exp =
            create_learning_experience(&format!("High volume memory {i}"), vec!["stress", "test"]);
        ids.push(system.remember(exp, None).expect("Failed"));
    }

    for chunk in ids.chunks(10) {
        let stats = system
            .reinforce_recall(chunk, RetrievalOutcome::Helpful)
            .expect("Failed");
        assert!(stats.memories_processed > 0);
    }
}

#[test]
fn test_rapid_feedback_cycles() {
    let (mut system, _temp_dir) = create_test_system();

    for i in 0..20 {
        let exp = create_learning_experience(&format!("Cycle {i}"), vec!["cycle"]);
        let _id = system.remember(exp, None).expect("Failed");

        let query = Query {
            query_text: Some(format!("cycle {i}")),
            max_results: 5,
            ..Default::default()
        };
        let tracked = system.recall_tracked(&query).expect("Failed");

        let outcome = if i % 3 == 0 {
            RetrievalOutcome::Helpful
        } else if i % 3 == 1 {
            RetrievalOutcome::Misleading
        } else {
            RetrievalOutcome::Neutral
        };

        system
            .reinforce_recall_tracked(&tracked, outcome)
            .expect("Failed");
    }

    let stats = system.stats();
    assert!(stats.total_memories >= 20);
}