velesdb-core 2.0.0

High-performance vector database engine written in Rust
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
//! Tests for AgentMemory (EPIC-010/US-001, US-002, US-003, US-004)

use super::*;
use crate::Database;
use std::sync::Arc;
use tempfile::tempdir;

// ============================================================================
// US-001: Basic API tests
// ============================================================================

/// Test: AgentMemory can be created from a Database
#[test]
fn test_agent_memory_new() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    let memory = AgentMemory::new(Arc::clone(&db));
    assert!(memory.is_ok(), "AgentMemory::new should succeed");
}

/// Test: AgentMemory provides access to SemanticMemory
#[test]
fn test_agent_memory_semantic_access() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::new(Arc::clone(&db)).unwrap();

    let semantic = memory.semantic();
    assert!(semantic.collection_name().starts_with("_semantic"));
}

/// Test: AgentMemory provides access to EpisodicMemory
#[test]
fn test_agent_memory_episodic_access() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::new(Arc::clone(&db)).unwrap();

    let episodic = memory.episodic();
    assert!(episodic.collection_name().starts_with("_episodic"));
}

/// Test: AgentMemory provides access to ProceduralMemory
#[test]
fn test_agent_memory_procedural_access() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::new(Arc::clone(&db)).unwrap();

    let procedural = memory.procedural();
    assert!(procedural.collection_name().starts_with("_procedural"));
}

/// Test: Multiple AgentMemory instances share the same collections
#[test]
fn test_agent_memory_shared_collections() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    let memory1 = AgentMemory::new(Arc::clone(&db)).unwrap();
    let memory2 = AgentMemory::new(Arc::clone(&db)).unwrap();

    assert_eq!(
        memory1.semantic().collection_name(),
        memory2.semantic().collection_name()
    );
}

// ============================================================================
// US-002: SemanticMemory tests
// ============================================================================

/// Test: SemanticMemory can store and query facts
#[test]
fn test_semantic_store_and_query() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    // Store a fact
    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    memory
        .semantic()
        .store(1, "The sky is blue", &embedding)
        .unwrap();

    // Query should find it
    let results = memory.semantic().query(&embedding, 1).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].0, 1); // ID
    assert!(results[0].2.contains("blue")); // Content
}

/// Test: SemanticMemory dimension validation
#[test]
fn test_semantic_dimension_mismatch() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    // Wrong dimension should fail
    let bad_embedding = vec![1.0, 0.0]; // Only 2 dims
    let result = memory.semantic().store(1, "test", &bad_embedding);
    assert!(result.is_err());
}

/// Test: AgentMemory rejects mismatched dimension when collection exists (PR #93 bug fix)
#[test]
fn test_dimension_mismatch_on_existing_collection() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    // Create memory with dimension 4
    let memory1 = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();
    assert_eq!(memory1.semantic().dimension(), 4);

    // Store something to ensure collection is created
    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    memory1.semantic().store(1, "test", &embedding).unwrap();

    // Try to create memory with different dimension - should fail
    let result = AgentMemory::with_dimension(Arc::clone(&db), 8);
    assert!(result.is_err());

    // Creating with same dimension should succeed
    let memory2 = AgentMemory::with_dimension(Arc::clone(&db), 4);
    assert!(memory2.is_ok());
}

// ============================================================================
// US-003: EpisodicMemory tests
// ============================================================================

/// Test: EpisodicMemory can record and retrieve events
#[test]
fn test_episodic_record_and_recent() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    // Record events with timestamps
    memory.episodic().record(1, "Event A", 1000, None).unwrap();
    memory.episodic().record(2, "Event B", 2000, None).unwrap();
    memory.episodic().record(3, "Event C", 3000, None).unwrap();

    // Get recent events (should be ordered by timestamp desc)
    let events = memory.episodic().recent(2, None).unwrap();
    assert_eq!(events.len(), 2);
    assert_eq!(events[0].0, 3); // Most recent first (Event C)
    assert_eq!(events[1].0, 2); // Then Event B
}

/// Test: EpisodicMemory similarity search
#[test]
fn test_episodic_recall_similar() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    // Record events with embeddings
    let emb1 = vec![1.0, 0.0, 0.0, 0.0];
    let emb2 = vec![0.0, 1.0, 0.0, 0.0];
    memory
        .episodic()
        .record(1, "Similar to query", 1000, Some(&emb1))
        .unwrap();
    memory
        .episodic()
        .record(2, "Different event", 2000, Some(&emb2))
        .unwrap();

    // Query with similar embedding
    let results = memory.episodic().recall_similar(&emb1, 2).unwrap();
    assert!(!results.is_empty());
    assert_eq!(results[0].0, 1); // Most similar should be first
}

// ============================================================================
// US-004: ProceduralMemory tests
// ============================================================================

/// Test: ProceduralMemory can learn and recall procedures
#[test]
fn test_procedural_learn_and_recall() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    // Learn a procedure
    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    let steps = vec!["Step 1".to_string(), "Step 2".to_string()];
    memory
        .procedural()
        .learn(1, "Test Procedure", &steps, Some(&embedding), 0.8)
        .unwrap();

    // Recall should find it
    let results = memory.procedural().recall(&embedding, 1, 0.0).unwrap();
    assert_eq!(results.len(), 1);
    assert_eq!(results[0].id, 1);
    assert_eq!(results[0].name, "Test Procedure");
    assert_eq!(results[0].steps.len(), 2);
    assert!((results[0].confidence - 0.8).abs() < 0.01);
}

/// Test: ProceduralMemory reinforcement
#[test]
fn test_procedural_reinforce() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    // Learn a procedure with initial confidence
    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    let steps = vec!["Step 1".to_string()];
    memory
        .procedural()
        .learn(1, "Reinforce Test", &steps, Some(&embedding), 0.5)
        .unwrap();

    // Reinforce positively
    memory.procedural().reinforce(1, true).unwrap();

    // Check confidence increased
    let results = memory.procedural().recall(&embedding, 1, 0.0).unwrap();
    assert!((results[0].confidence - 0.6).abs() < 0.01); // 0.5 + 0.1 = 0.6
}

/// Test: ProceduralMemory min_confidence filter
#[test]
fn test_procedural_min_confidence_filter() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    let steps = vec!["Step".to_string()];

    // Learn procedure with low confidence
    memory
        .procedural()
        .learn(1, "Low Confidence", &steps, Some(&embedding), 0.3)
        .unwrap();

    // Query with high min_confidence should return empty
    let results = memory.procedural().recall(&embedding, 1, 0.5).unwrap();
    assert!(results.is_empty());

    // Query with low min_confidence should return the procedure
    let results = memory.procedural().recall(&embedding, 1, 0.2).unwrap();
    assert_eq!(results.len(), 1);
}

// ============================================================================
// US-005: Eviction, TTL, Snapshots, Consolidation
// ============================================================================

/// Test: with_eviction_config sets the eviction configuration
#[test]
fn test_with_eviction_config() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    let config = EvictionConfig {
        consolidation_age_threshold: 3600,
        min_confidence_threshold: 0.5,
        max_entries_per_cycle: 100,
    };

    // Builder pattern must not fail
    let memory = AgentMemory::new(Arc::clone(&db))
        .unwrap()
        .with_eviction_config(config);

    // Verify the memory is usable after configuration
    memory.semantic().store(1, "fact", &vec![0.0; 384]).unwrap();
    assert!(memory.auto_expire().is_ok());
}

/// Test: with_snapshots enables the snapshot manager
#[test]
fn test_with_snapshots() {
    let dir = tempdir().unwrap();
    let snap_dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    let memory = AgentMemory::new(Arc::clone(&db))
        .unwrap()
        .with_snapshots(snap_dir.path().to_str().unwrap(), 5);

    // Snapshot operations should now succeed (manager is configured)
    let version = memory.snapshot().unwrap();
    assert_eq!(version, 1);
}

/// Test: set_semantic_ttl / set_episodic_ttl / set_procedural_ttl register TTLs
#[test]
fn test_set_ttl_functions() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    let embedding = vec![1.0, 0.0, 0.0, 0.0];

    memory.semantic().store(1, "sem fact", &embedding).unwrap();
    memory
        .episodic()
        .record(2, "epi event", 1000, Some(&embedding))
        .unwrap();
    let steps = vec!["s1".to_string()];
    memory
        .procedural()
        .learn(3, "proc", &steps, Some(&embedding), 0.9)
        .unwrap();

    // Setting TTLs should not panic
    memory.set_semantic_ttl(1, 3600);
    memory.set_episodic_ttl(2, 7200);
    memory.set_procedural_ttl(3, 1800);

    // Non-expired entries should still be queryable
    let results = memory.semantic().query(&embedding, 1).unwrap();
    assert_eq!(results.len(), 1);
}

/// Test: auto_expire removes entries whose TTL has elapsed
#[test]
fn test_auto_expire_removes_expired() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    memory
        .semantic()
        .store(1, "will expire", &embedding)
        .unwrap();

    // Set a TTL of 0 seconds — expires immediately
    memory.set_semantic_ttl(1, 0);

    let result = memory.auto_expire().unwrap();
    // #1041: each expired key is namespaced by subsystem, so only the semantic
    // counter increments — episodic/procedural are not touched by a semantic TTL.
    assert_eq!(result.semantic_expired, 1);
    assert_eq!(result.episodic_expired, 0);
    assert_eq!(result.procedural_expired, 0);

    // Verify the entry is actually gone from semantic memory
    let results = memory.semantic().query(&embedding, 1).unwrap();
    assert!(results.is_empty());
}

/// Test: auto_expire returns zero counts when nothing is expired
#[test]
fn test_auto_expire_nothing_expired() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    memory.semantic().store(1, "keep me", &embedding).unwrap();

    // TTL far in the future — should not expire
    memory.set_semantic_ttl(1, 999_999);

    let result = memory.auto_expire().unwrap();
    assert_eq!(result.semantic_expired, 0);
    assert_eq!(result.episodic_expired, 0);
    assert_eq!(result.procedural_expired, 0);
    assert_eq!(result.episodic_consolidated, 0);
}

/// Test: evict_low_confidence_procedures removes low-confidence entries
#[test]
fn test_evict_low_confidence_procedures() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    let steps = vec!["step".to_string()];

    // Low confidence — should be evicted
    memory
        .procedural()
        .learn(1, "weak", &steps, Some(&embedding), 0.1)
        .unwrap();
    // High confidence — should survive
    memory
        .procedural()
        .learn(2, "strong", &steps, Some(&embedding), 0.9)
        .unwrap();

    let evicted = memory.evict_low_confidence_procedures(0.5).unwrap();
    assert_eq!(evicted, 1);

    // Only the high-confidence procedure should remain
    let remaining = memory.procedural().list_all().unwrap();
    assert_eq!(remaining.len(), 1);
    assert_eq!(remaining[0].id, 2);
}

/// Test: evict_low_confidence_procedures returns 0 when all above threshold
#[test]
fn test_evict_low_confidence_none_evicted() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    let steps = vec!["step".to_string()];

    memory
        .procedural()
        .learn(1, "good", &steps, Some(&embedding), 0.8)
        .unwrap();
    memory
        .procedural()
        .learn(2, "also good", &steps, Some(&embedding), 0.7)
        .unwrap();

    let evicted = memory.evict_low_confidence_procedures(0.5).unwrap();
    assert_eq!(evicted, 0);
}

/// Test: snapshot and load_latest_snapshot round-trip
#[test]
fn test_snapshot_round_trip() {
    let dir = tempdir().unwrap();
    let snap_dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4)
        .unwrap()
        .with_snapshots(snap_dir.path().to_str().unwrap(), 5);

    // Store data across all subsystems
    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    memory.semantic().store(1, "fact one", &embedding).unwrap();
    memory
        .episodic()
        .record(2, "event one", 1000, Some(&embedding))
        .unwrap();
    let steps = vec!["step1".to_string()];
    memory
        .procedural()
        .learn(3, "proc one", &steps, Some(&embedding), 0.8)
        .unwrap();

    // Create snapshot
    let version = memory.snapshot().unwrap();
    assert!(version >= 1);

    // Load it back
    let loaded_version = memory.load_latest_snapshot().unwrap();
    assert_eq!(loaded_version, version);
}

/// Test: snapshot without manager returns an error
#[test]
fn test_snapshot_without_manager_errors() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::new(Arc::clone(&db)).unwrap();

    // No snapshot manager configured — should fail
    assert!(memory.snapshot().is_err());
    assert!(memory.load_latest_snapshot().is_err());
    assert!(memory.list_snapshot_versions().is_err());
}

/// Test: list_snapshot_versions returns created versions
#[test]
fn test_list_snapshot_versions() {
    let dir = tempdir().unwrap();
    let snap_dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4)
        .unwrap()
        .with_snapshots(snap_dir.path().to_str().unwrap(), 10);

    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    memory.semantic().store(1, "fact", &embedding).unwrap();

    // Create two snapshots
    let v1 = memory.snapshot().unwrap();
    let v2 = memory.snapshot().unwrap();

    let versions = memory.list_snapshot_versions().unwrap();
    assert!(versions.contains(&v1));
    assert!(versions.contains(&v2));
    assert_eq!(versions.len(), 2);
}

/// Test: consolidate_old_episodes migrates old events to semantic memory
#[test]
fn test_consolidate_old_episodes_via_auto_expire() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    // Use a very small consolidation threshold (1 second) so past events qualify
    let config = EvictionConfig {
        consolidation_age_threshold: 1,
        min_confidence_threshold: 0.1,
        max_entries_per_cycle: 1000,
    };
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4)
        .unwrap()
        .with_eviction_config(config);

    // Record an episode with a timestamp far in the past
    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    memory
        .episodic()
        .record(1, "ancient event", 100, Some(&embedding))
        .unwrap();

    // auto_expire should consolidate the old episode into semantic memory
    let result = memory.auto_expire().unwrap();
    assert_eq!(result.episodic_consolidated, 1);

    // The event should now be findable in semantic memory
    let sem_results = memory.semantic().query(&embedding, 1).unwrap();
    assert_eq!(sem_results.len(), 1);
    assert!(sem_results[0].2.contains("ancient event"));
}

// ============================================================================
// #1041..#1043: P0 data-loss / deadlock regression coverage
// ============================================================================

/// #1041: a TTL on semantic id=5 must not expire a live episodic id=5.
/// `auto_expire` after expiry deletes only the semantic row; the episodic
/// event survives and the counters are exact.
#[test]
fn test_auto_expire_no_cross_subsystem_expiry() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    // Disable consolidation so this test isolates TTL cross-expiry only.
    let config = EvictionConfig {
        consolidation_age_threshold: 0,
        ..EvictionConfig::default()
    };
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4)
        .unwrap()
        .with_eviction_config(config);

    let embedding = vec![1.0, 0.0, 0.0, 0.0];

    // Same numeric id (5) in two subsystems.
    memory
        .semantic()
        .store(5, "semantic fact", &embedding)
        .unwrap();
    memory
        .episodic()
        .record(5, "live episodic event", 1_000, Some(&embedding))
        .unwrap();

    // Expire ONLY the semantic id 5.
    memory.set_semantic_ttl(5, 0);

    let result = memory.auto_expire().unwrap();
    assert_eq!(result.semantic_expired, 1, "semantic id 5 must expire");
    assert_eq!(
        result.episodic_expired, 0,
        "episodic id 5 must NOT be counted as expired"
    );
    assert_eq!(result.procedural_expired, 0);

    // The episodic event must SURVIVE.
    let survivor = memory.episodic().get_with_embedding(5).unwrap();
    assert!(
        survivor.is_some(),
        "episodic id 5 must survive a semantic-only TTL expiry"
    );
    assert_eq!(survivor.unwrap().0, "live episodic event");

    // The semantic fact is gone.
    assert!(memory.semantic().get(5).unwrap().is_none());
}

/// #1042: consolidation must not clobber an existing semantic fact that shares
/// the consolidated episode's numeric id. The original fact stays intact and
/// the consolidated event lands under a fresh semantic id.
#[test]
fn test_consolidation_preserves_existing_semantic_fact() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    let config = EvictionConfig {
        consolidation_age_threshold: 1,
        min_confidence_threshold: 0.1,
        max_entries_per_cycle: 1000,
    };
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4)
        .unwrap()
        .with_eviction_config(config);

    let sem_emb = vec![1.0, 0.0, 0.0, 0.0];
    let epi_emb = vec![0.0, 1.0, 0.0, 0.0];

    // Pre-existing semantic fact at id=1.
    memory
        .semantic()
        .store(1, "irreplaceable fact", &sem_emb)
        .unwrap();
    // Old episodic event ALSO at id=1 — will be consolidated.
    memory
        .episodic()
        .record(1, "old episode", 100, Some(&epi_emb))
        .unwrap();

    let result = memory.auto_expire().unwrap();
    assert_eq!(result.episodic_consolidated, 1);

    // The original semantic fact at id=1 must still be intact (NOT overwritten).
    let original = memory.semantic().get(1).unwrap();
    assert!(original.is_some(), "original semantic fact must survive");
    assert_eq!(original.unwrap().0, "irreplaceable fact");

    // The consolidated episode is now retrievable in semantic memory under a
    // fresh id.
    let found = memory.semantic().query(&epi_emb, 5).unwrap();
    assert!(
        found.iter().any(|r| r.2.contains("old episode")),
        "consolidated episode must be stored under a fresh semantic id"
    );
}

/// #1042: exceeding the per-cycle consolidation cap surfaces a "truncated"
/// signal so the caller knows to run `auto_expire` again.
#[test]
fn test_consolidation_truncation_signal() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    let config = EvictionConfig {
        consolidation_age_threshold: 1,
        min_confidence_threshold: 0.1,
        max_entries_per_cycle: 2, // tiny cap to force truncation
    };
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4)
        .unwrap()
        .with_eviction_config(config);

    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    // Record more old episodes than the cap.
    for id in 1u64..=5 {
        let ts = 100 + i64::try_from(id).unwrap();
        memory
            .episodic()
            .record(id, "ancient", ts, Some(&embedding))
            .unwrap();
    }

    let result = memory.auto_expire().unwrap();
    assert_eq!(
        result.episodic_consolidated, 2,
        "cap limits this cycle to 2"
    );
    assert!(
        result.consolidation_truncated,
        "truncated signal must be set when more old episodes remain"
    );

    // A second pass drains more and (with 3 left, still > cap=2) stays truncated.
    let result2 = memory.auto_expire().unwrap();
    assert_eq!(result2.episodic_consolidated, 2);
    assert!(result2.consolidation_truncated);

    // Final pass: 1 left, fits under the cap, no longer truncated.
    let result3 = memory.auto_expire().unwrap();
    assert_eq!(result3.episodic_consolidated, 1);
    assert!(!result3.consolidation_truncated);
}

/// #1043: snapshot round-trip must restore DATA and TTL state, not just the
/// version number. Restore into a fresh AgentMemory and assert both.
#[test]
fn test_snapshot_round_trip_restores_data_and_ttl() {
    let dir = tempdir().unwrap();
    let snap_dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4)
        .unwrap()
        .with_snapshots(snap_dir.path().to_str().unwrap(), 5);

    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    memory
        .semantic()
        .store(1, "persisted fact", &embedding)
        .unwrap();
    memory
        .episodic()
        .record(2, "persisted event", 1_000, Some(&embedding))
        .unwrap();
    // A long-lived TTL on the semantic fact so it survives but is tracked.
    memory.set_semantic_ttl(1, 9_999);

    let version = memory.snapshot().unwrap();

    // Restore into a SEPARATE database/memory to prove the snapshot carries
    // state, not just the in-memory handle.
    let dir2 = tempdir().unwrap();
    let db2 = Arc::new(Database::open(dir2.path()).unwrap());
    let memory2 = AgentMemory::with_dimension(Arc::clone(&db2), 4)
        .unwrap()
        .with_snapshots(snap_dir.path().to_str().unwrap(), 5);

    let loaded = memory2.load_snapshot_version(version);
    assert!(loaded.is_ok());

    // DATA restored.
    let fact = memory2.semantic().get(1).unwrap();
    assert_eq!(fact.unwrap().0, "persisted fact");
    let event = memory2.episodic().get_with_embedding(2).unwrap();
    assert_eq!(event.unwrap().0, "persisted event");

    // TTL state restored under the SEMANTIC namespace (still live, not expired).
    let after = memory2.auto_expire().unwrap();
    assert_eq!(
        after.semantic_expired, 0,
        "restored TTL is far-future, fact must not expire"
    );
    assert!(
        memory2.semantic().get(1).unwrap().is_some(),
        "fact with restored future TTL must survive auto_expire"
    );
}

/// #1045: `auto_expire` now populates `procedural_evicted` using the configured
/// `min_confidence_threshold` (previously a dead field on both ends).
#[test]
fn test_auto_expire_populates_procedural_evicted() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());

    let config = EvictionConfig {
        consolidation_age_threshold: 0, // isolate eviction
        min_confidence_threshold: 0.5,
        max_entries_per_cycle: 1000,
    };
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4)
        .unwrap()
        .with_eviction_config(config);

    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    let steps = vec!["s".to_string()];
    // Below threshold -> evicted.
    memory
        .procedural()
        .learn(1, "weak", &steps, Some(&embedding), 0.2)
        .unwrap();
    // Above threshold -> kept.
    memory
        .procedural()
        .learn(2, "strong", &steps, Some(&embedding), 0.9)
        .unwrap();

    let result = memory.auto_expire().unwrap();
    assert_eq!(
        result.procedural_evicted, 1,
        "one low-confidence procedure evicted"
    );

    let remaining = memory.procedural().list_all().unwrap();
    assert_eq!(remaining.len(), 1);
    assert_eq!(remaining[0].id, 2);
}

/// Reinforcing a procedure by an id obtained from `recall` must update that
/// same procedure (no id drift between recall and reinforce).
#[test]
fn test_reinforce_by_recalled_id_updates_same_procedure() {
    let dir = tempdir().unwrap();
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    let embedding = vec![1.0, 0.0, 0.0, 0.0];
    let steps = vec!["step".to_string()];
    memory
        .procedural()
        .learn(99, "recall me", &steps, Some(&embedding), 0.5)
        .unwrap();

    // Obtain the id via recall, then reinforce by that id.
    let recalled = memory.procedural().recall(&embedding, 1, 0.0).unwrap();
    assert_eq!(recalled.len(), 1);
    let id = recalled[0].id;
    assert_eq!(id, 99);

    memory.procedural().reinforce(id, true).unwrap();

    let after = memory.procedural().recall(&embedding, 1, 0.0).unwrap();
    assert_eq!(after[0].id, 99);
    assert!(
        (after[0].confidence - 0.6).abs() < 0.01,
        "reinforce(recalled_id) must raise the same procedure's confidence"
    );
}

// ============================================================================
// TTL persistence across restart (durable expires_at in payload)
// ============================================================================

/// A TTL'd entry must remain mortal after dropping and reopening a DB-backed
/// `AgentMemory` on the same path — no snapshot ritual required. Entries
/// stored without TTL must survive the reopen untouched.
#[test]
fn test_ttl_survives_reopen_all_kinds() {
    let dir = tempdir().unwrap();
    let embedding = vec![1.0, 0.0, 0.0, 0.0];

    {
        let db = Arc::new(Database::open(dir.path()).unwrap());
        let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();
        memory
            .semantic()
            .store_with_ttl(1, "ephemeral fact", &embedding, 1)
            .unwrap();
        memory
            .semantic()
            .store(2, "durable fact", &embedding)
            .unwrap();
        memory
            .episodic()
            .record_with_ttl(10, "ephemeral event", 1_700_000_000, Some(&embedding), 1)
            .unwrap();
        memory
            .procedural()
            .learn_with_ttl(
                20,
                "ephemeral proc",
                &["step".to_string()],
                Some(&embedding),
                0.9,
                1,
            )
            .unwrap();
    }

    // Let the 1-second TTLs lapse, then reopen on the same path.
    std::thread::sleep(std::time::Duration::from_millis(1200));
    let db = Arc::new(Database::open(dir.path()).unwrap());
    let memory = AgentMemory::with_dimension(Arc::clone(&db), 4).unwrap();

    assert!(
        memory.semantic().get(1).unwrap().is_none(),
        "TTL'd semantic fact must stay expired after reopen"
    );
    let results = memory.semantic().query(&embedding, 10).unwrap();
    assert!(
        !results.iter().any(|r| r.0 == 1),
        "expired semantic fact must be filtered from query after reopen"
    );
    assert!(
        results.iter().any(|r| r.0 == 2),
        "fact stored without TTL must survive reopen"
    );

    let recent = memory.episodic().recent(10, None).unwrap();
    assert!(
        !recent.iter().any(|e| e.0 == 10),
        "TTL'd episodic event must stay expired after reopen"
    );

    let procs = memory.procedural().list_all().unwrap();
    assert!(
        !procs.iter().any(|p| p.id == 20),
        "TTL'd procedure must stay expired after reopen"
    );

    // The rebuilt TTL map must also drive physical deletion on auto_expire.
    let stats = memory.auto_expire().unwrap();
    assert_eq!(stats.semantic_expired, 1, "expired semantic fact deleted");
    assert_eq!(stats.episodic_expired, 1, "expired episodic event deleted");
    assert_eq!(stats.procedural_expired, 1, "expired procedure deleted");
}