beleth 0.2.0-rc.1

Autonomous agent framework - The King commands legions
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
//! Long-term memory system with persistent file storage.
//!
//! Inspired by Leviathan/Persona's `LongTermMemory` system that provides:
//! - File-based persistent storage
//! - Memory types (session summary, project learning, decisions, etc.)
//! - Full-text search capability
//! - Importance levels
//! - Automatic tagging and metadata

use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};

use serde::{Deserialize, Serialize};

use infernum_core::Result;

// ============================================================================
// Memory Types and Importance
// ============================================================================

/// Types of long-term memory entries.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MemoryType {
    /// Summary of a multi-turn conversation session.
    SessionSummary,
    /// Knowledge learned across sessions.
    ProjectLearning,
    /// Important decisions made.
    Decision,
    /// Project context and background.
    Context,
    /// Error patterns and their solutions.
    ErrorPattern,
    /// Performance optimizations discovered.
    Optimization,
    /// User preferences and behaviors.
    UserPreference,
    /// Code patterns and conventions.
    CodePattern,
}

impl MemoryType {
    /// Returns the directory name for this memory type.
    #[must_use]
    pub fn dir_name(&self) -> &'static str {
        match self {
            Self::SessionSummary => "sessions",
            Self::ProjectLearning => "learnings",
            Self::Decision => "decisions",
            Self::Context => "context",
            Self::ErrorPattern => "errors",
            Self::Optimization => "optimizations",
            Self::UserPreference => "preferences",
            Self::CodePattern => "patterns",
        }
    }
}

/// Importance level of a memory entry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum ImportanceLevel {
    /// Low importance, may be pruned first.
    Low,
    /// Medium importance, normal retention.
    Medium,
    /// High importance, retained longer.
    High,
    /// Critical importance, never pruned automatically.
    Critical,
}

impl Default for ImportanceLevel {
    fn default() -> Self {
        Self::Medium
    }
}

// ============================================================================
// Memory Entry
// ============================================================================

/// A single memory entry.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryEntry {
    /// Unique identifier.
    pub id: String,
    /// Type of memory.
    pub memory_type: MemoryType,
    /// The memory content.
    pub content: String,
    /// Summary of the content.
    pub summary: Option<String>,
    /// Importance level.
    pub importance: ImportanceLevel,
    /// Tags for categorization.
    pub tags: Vec<String>,
    /// Additional metadata.
    pub metadata: HashMap<String, String>,
    /// Creation timestamp (Unix epoch seconds).
    pub created_at: u64,
    /// Last access timestamp.
    pub last_accessed: u64,
    /// Number of times this memory was accessed.
    pub access_count: u64,
    /// Related memory IDs.
    pub related_ids: Vec<String>,
}

impl MemoryEntry {
    /// Creates a new memory entry.
    pub fn new(memory_type: MemoryType, content: impl Into<String>) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);

        Self {
            id: generate_id(),
            memory_type,
            content: content.into(),
            summary: None,
            importance: ImportanceLevel::default(),
            tags: Vec::new(),
            metadata: HashMap::new(),
            created_at: now,
            last_accessed: now,
            access_count: 0,
            related_ids: Vec::new(),
        }
    }

    /// Sets the summary.
    #[must_use]
    pub fn with_summary(mut self, summary: impl Into<String>) -> Self {
        self.summary = Some(summary.into());
        self
    }

    /// Sets the importance level.
    #[must_use]
    pub fn with_importance(mut self, importance: ImportanceLevel) -> Self {
        self.importance = importance;
        self
    }

    /// Adds a tag.
    #[must_use]
    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Adds tags.
    #[must_use]
    pub fn with_tags(mut self, tags: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.tags.extend(tags.into_iter().map(Into::into));
        self
    }

    /// Adds metadata.
    #[must_use]
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Records an access to this memory.
    pub fn record_access(&mut self) {
        self.access_count += 1;
        self.last_accessed = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
    }

    /// Checks if content matches the search query.
    pub fn matches(&self, query: &str) -> bool {
        let query_lower = query.to_lowercase();
        self.content.to_lowercase().contains(&query_lower)
            || self
                .summary
                .as_ref()
                .map_or(false, |s| s.to_lowercase().contains(&query_lower))
            || self
                .tags
                .iter()
                .any(|t| t.to_lowercase().contains(&query_lower))
    }
}

// ============================================================================
// Memory Statistics
// ============================================================================

/// Statistics about the memory store.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryStats {
    /// Total number of entries.
    pub total_entries: usize,
    /// Entries by type.
    pub by_type: HashMap<String, usize>,
    /// Entries by importance.
    pub by_importance: HashMap<String, usize>,
    /// Total size in bytes.
    pub total_size_bytes: u64,
    /// Oldest entry timestamp.
    pub oldest_entry: Option<u64>,
    /// Newest entry timestamp.
    pub newest_entry: Option<u64>,
}

// ============================================================================
// Long-Term Memory Store
// ============================================================================

/// Long-term memory store with file-based persistence.
pub struct LongTermMemory {
    /// Base directory for storage.
    base_dir: PathBuf,
    /// In-memory cache of entries.
    cache: HashMap<String, MemoryEntry>,
    /// Index by memory type.
    type_index: HashMap<MemoryType, Vec<String>>,
    /// Index by tag.
    tag_index: HashMap<String, Vec<String>>,
    /// ID to memory type index for O(1) lookups.
    id_type_index: HashMap<String, MemoryType>,
    /// Maximum cache size.
    max_cache_size: usize,
}

impl LongTermMemory {
    /// Creates a new long-term memory store.
    pub fn new(base_dir: impl Into<PathBuf>) -> Result<Self> {
        let base_dir = base_dir.into();

        // Create directory structure
        if !base_dir.exists() {
            fs::create_dir_all(&base_dir).map_err(|e| {
                infernum_core::Error::internal(format!("Failed to create memory dir: {}", e))
            })?;
        }

        // Create subdirectories for each memory type
        for memory_type in [
            MemoryType::SessionSummary,
            MemoryType::ProjectLearning,
            MemoryType::Decision,
            MemoryType::Context,
            MemoryType::ErrorPattern,
            MemoryType::Optimization,
            MemoryType::UserPreference,
            MemoryType::CodePattern,
        ] {
            let type_dir = base_dir.join(memory_type.dir_name());
            if !type_dir.exists() {
                fs::create_dir_all(&type_dir).map_err(|e| {
                    infernum_core::Error::internal(format!("Failed to create type dir: {}", e))
                })?;
            }
        }

        let mut memory = Self {
            base_dir,
            cache: HashMap::new(),
            type_index: HashMap::new(),
            tag_index: HashMap::new(),
            id_type_index: HashMap::new(),
            max_cache_size: 1000,
        };

        // Load existing entries
        memory.load_all()?;

        Ok(memory)
    }

    /// Creates with a custom cache size.
    #[must_use]
    pub fn with_cache_size(mut self, size: usize) -> Self {
        self.max_cache_size = size;
        self
    }

    /// Stores a memory entry.
    pub fn store(&mut self, entry: MemoryEntry) -> Result<String> {
        let id = entry.id.clone();
        let memory_type = entry.memory_type;
        let tags = entry.tags.clone();

        // Write to disk
        self.write_entry(&entry)?;

        // Update indices
        self.type_index
            .entry(memory_type)
            .or_default()
            .push(id.clone());

        // Update ID-to-type index for O(1) lookups
        self.id_type_index.insert(id.clone(), memory_type);

        for tag in &tags {
            self.tag_index
                .entry(tag.clone())
                .or_default()
                .push(id.clone());
        }

        // Add to cache
        self.cache.insert(id.clone(), entry);

        // Prune cache if needed
        self.prune_cache();

        Ok(id)
    }

    /// Retrieves a memory entry by ID.
    pub fn get(&mut self, id: &str) -> Option<&MemoryEntry> {
        // Try cache first
        if let Some(entry) = self.cache.get_mut(id) {
            entry.record_access();
            return self.cache.get(id);
        }

        // Load from disk if not in cache
        if let Ok(entry) = self.load_entry(id) {
            self.cache.insert(id.to_string(), entry);
            return self.cache.get(id);
        }

        None
    }

    /// Searches for memories matching a query.
    pub fn search(&self, query: &str) -> Vec<&MemoryEntry> {
        self.cache.values().filter(|e| e.matches(query)).collect()
    }

    /// Gets memories by type.
    pub fn get_by_type(&self, memory_type: MemoryType) -> Vec<&MemoryEntry> {
        self.type_index
            .get(&memory_type)
            .map(|ids| ids.iter().filter_map(|id| self.cache.get(id)).collect())
            .unwrap_or_default()
    }

    /// Gets memories by tag.
    pub fn get_by_tag(&self, tag: &str) -> Vec<&MemoryEntry> {
        self.tag_index
            .get(tag)
            .map(|ids| ids.iter().filter_map(|id| self.cache.get(id)).collect())
            .unwrap_or_default()
    }

    /// Gets the most important memories.
    pub fn get_important(
        &self,
        min_importance: ImportanceLevel,
        limit: usize,
    ) -> Vec<&MemoryEntry> {
        let mut entries: Vec<_> = self
            .cache
            .values()
            .filter(|e| e.importance >= min_importance)
            .collect();

        entries.sort_by(|a, b| b.importance.cmp(&a.importance));
        entries.truncate(limit);
        entries
    }

    /// Gets recently accessed memories.
    pub fn get_recent(&self, limit: usize) -> Vec<&MemoryEntry> {
        let mut entries: Vec<_> = self.cache.values().collect();
        entries.sort_by(|a, b| b.last_accessed.cmp(&a.last_accessed));
        entries.truncate(limit);
        entries
    }

    /// Deletes a memory entry.
    pub fn delete(&mut self, id: &str) -> Result<bool> {
        if let Some(entry) = self.cache.remove(id) {
            // Remove from type index
            if let Some(ids) = self.type_index.get_mut(&entry.memory_type) {
                ids.retain(|i| i != id);
            }

            // Remove from ID-to-type index
            self.id_type_index.remove(id);

            // Remove from tag indices
            for tag in &entry.tags {
                if let Some(ids) = self.tag_index.get_mut(tag) {
                    ids.retain(|i| i != id);
                }
            }

            // Delete from disk
            let path = self.entry_path(&entry);
            if path.exists() {
                fs::remove_file(&path).map_err(|e| {
                    infernum_core::Error::internal(format!("Failed to delete: {}", e))
                })?;
            }

            return Ok(true);
        }
        Ok(false)
    }

    /// Returns statistics about the memory store.
    pub fn stats(&self) -> MemoryStats {
        let mut by_type: HashMap<String, usize> = HashMap::new();
        let mut by_importance: HashMap<String, usize> = HashMap::new();
        let mut oldest: Option<u64> = None;
        let mut newest: Option<u64> = None;
        let mut total_size = 0u64;

        for entry in self.cache.values() {
            *by_type
                .entry(format!("{:?}", entry.memory_type))
                .or_default() += 1;
            *by_importance
                .entry(format!("{:?}", entry.importance))
                .or_default() += 1;

            oldest = Some(oldest.map_or(entry.created_at, |o| o.min(entry.created_at)));
            newest = Some(newest.map_or(entry.created_at, |o| o.max(entry.created_at)));
            total_size += entry.content.len() as u64;
        }

        MemoryStats {
            total_entries: self.cache.len(),
            by_type,
            by_importance,
            total_size_bytes: total_size,
            oldest_entry: oldest,
            newest_entry: newest,
        }
    }

    /// Prunes low-importance entries if cache is too large.
    fn prune_cache(&mut self) {
        if self.cache.len() <= self.max_cache_size {
            return;
        }

        // Sort by importance (ascending) and last access (ascending)
        let mut entries: Vec<_> = self.cache.keys().cloned().collect();
        entries.sort_by(|a, b| {
            let entry_a = self.cache.get(a);
            let entry_b = self.cache.get(b);
            match (entry_a, entry_b) {
                (Some(a), Some(b)) => a
                    .importance
                    .cmp(&b.importance)
                    .then(a.last_accessed.cmp(&b.last_accessed)),
                _ => std::cmp::Ordering::Equal,
            }
        });

        // Remove lowest priority entries
        let to_remove = self.cache.len() - self.max_cache_size;
        for id in entries.into_iter().take(to_remove) {
            // Don't remove critical entries
            if let Some(entry) = self.cache.get(&id) {
                if entry.importance != ImportanceLevel::Critical {
                    self.cache.remove(&id);
                }
            }
        }
    }

    fn entry_path(&self, entry: &MemoryEntry) -> PathBuf {
        self.base_dir
            .join(entry.memory_type.dir_name())
            .join(format!("{}.json", entry.id))
    }

    fn write_entry(&self, entry: &MemoryEntry) -> Result<()> {
        let path = self.entry_path(entry);
        let json = serde_json::to_string_pretty(entry)
            .map_err(|e| infernum_core::Error::internal(format!("Failed to serialize: {}", e)))?;
        fs::write(&path, json)
            .map_err(|e| infernum_core::Error::internal(format!("Failed to write: {}", e)))?;
        Ok(())
    }

    fn load_entry(&self, id: &str) -> Result<MemoryEntry> {
        // Use ID-to-type index for O(1) lookup if available
        if let Some(memory_type) = self.id_type_index.get(id) {
            let path = self
                .base_dir
                .join(memory_type.dir_name())
                .join(format!("{}.json", id));
            if path.exists() {
                let content = fs::read_to_string(&path).map_err(|e| {
                    infernum_core::Error::internal(format!("Failed to read: {}", e))
                })?;
                let entry: MemoryEntry = serde_json::from_str(&content).map_err(|e| {
                    infernum_core::Error::internal(format!("Failed to parse: {}", e))
                })?;
                return Ok(entry);
            }
        }

        // Fallback: search in all type directories (for entries not yet indexed)
        for memory_type in [
            MemoryType::SessionSummary,
            MemoryType::ProjectLearning,
            MemoryType::Decision,
            MemoryType::Context,
            MemoryType::ErrorPattern,
            MemoryType::Optimization,
            MemoryType::UserPreference,
            MemoryType::CodePattern,
        ] {
            let path = self
                .base_dir
                .join(memory_type.dir_name())
                .join(format!("{}.json", id));
            if path.exists() {
                let content = fs::read_to_string(&path).map_err(|e| {
                    infernum_core::Error::internal(format!("Failed to read: {}", e))
                })?;
                let entry: MemoryEntry = serde_json::from_str(&content).map_err(|e| {
                    infernum_core::Error::internal(format!("Failed to parse: {}", e))
                })?;
                return Ok(entry);
            }
        }
        Err(infernum_core::Error::internal(format!(
            "Entry not found: {}",
            id
        )))
    }

    fn load_all(&mut self) -> Result<()> {
        for memory_type in [
            MemoryType::SessionSummary,
            MemoryType::ProjectLearning,
            MemoryType::Decision,
            MemoryType::Context,
            MemoryType::ErrorPattern,
            MemoryType::Optimization,
            MemoryType::UserPreference,
            MemoryType::CodePattern,
        ] {
            let type_dir = self.base_dir.join(memory_type.dir_name());
            if let Ok(entries) = fs::read_dir(&type_dir) {
                for entry in entries.flatten() {
                    if entry.path().extension().map_or(false, |e| e == "json") {
                        if let Ok(content) = fs::read_to_string(entry.path()) {
                            if let Ok(mem_entry) = serde_json::from_str::<MemoryEntry>(&content) {
                                let id = mem_entry.id.clone();
                                let tags = mem_entry.tags.clone();

                                self.type_index
                                    .entry(memory_type)
                                    .or_default()
                                    .push(id.clone());

                                // Populate ID-to-type index for O(1) lookups
                                self.id_type_index.insert(id.clone(), memory_type);

                                for tag in &tags {
                                    self.tag_index
                                        .entry(tag.clone())
                                        .or_default()
                                        .push(id.clone());
                                }

                                self.cache.insert(id, mem_entry);
                            }
                        }
                    }
                }
            }
        }
        Ok(())
    }
}

/// Generates a unique ID.
fn generate_id() -> String {
    use std::sync::atomic::{AtomicU64, Ordering};
    static COUNTER: AtomicU64 = AtomicU64::new(0);

    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis())
        .unwrap_or(0);
    let counter = COUNTER.fetch_add(1, Ordering::Relaxed);

    format!("{:x}-{:04x}", timestamp, counter)
}

// ============================================================================
// Tests
// ============================================================================

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

    fn create_test_memory() -> (LongTermMemory, TempDir) {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let memory = LongTermMemory::new(temp_dir.path()).expect("Failed to create memory");
        (memory, temp_dir)
    }

    #[test]
    fn test_memory_entry_creation() {
        let entry = MemoryEntry::new(MemoryType::SessionSummary, "Test content");
        assert_eq!(entry.memory_type, MemoryType::SessionSummary);
        assert_eq!(entry.content, "Test content");
        assert_eq!(entry.importance, ImportanceLevel::Medium);
        assert!(entry.tags.is_empty());
    }

    #[test]
    fn test_memory_entry_builder() {
        let entry = MemoryEntry::new(MemoryType::Decision, "Important decision")
            .with_importance(ImportanceLevel::High)
            .with_tag("architecture")
            .with_tag("database")
            .with_summary("Summary of decision")
            .with_metadata("author", "test");

        assert_eq!(entry.importance, ImportanceLevel::High);
        assert_eq!(entry.tags.len(), 2);
        assert!(entry.tags.contains(&"architecture".to_string()));
        assert_eq!(entry.summary, Some("Summary of decision".to_string()));
        assert_eq!(entry.metadata.get("author"), Some(&"test".to_string()));
    }

    #[test]
    fn test_memory_entry_matches() {
        let entry = MemoryEntry::new(MemoryType::Context, "Rust programming language")
            .with_tag("rust")
            .with_summary("About Rust");

        assert!(entry.matches("rust"));
        assert!(entry.matches("RUST")); // case insensitive
        assert!(entry.matches("programming"));
        assert!(!entry.matches("python"));
    }

    #[test]
    fn test_long_term_memory_store_and_retrieve() {
        let (mut memory, _temp) = create_test_memory();

        let entry = MemoryEntry::new(MemoryType::ProjectLearning, "Learning about Rust")
            .with_importance(ImportanceLevel::High)
            .with_tag("rust");

        let id = memory.store(entry).expect("Failed to store");
        let retrieved = memory.get(&id).expect("Failed to retrieve");

        assert_eq!(retrieved.content, "Learning about Rust");
        assert_eq!(retrieved.importance, ImportanceLevel::High);
    }

    #[test]
    fn test_long_term_memory_search() {
        let (mut memory, _temp) = create_test_memory();

        memory
            .store(MemoryEntry::new(
                MemoryType::Context,
                "Rust is a systems language",
            ))
            .ok();
        memory
            .store(MemoryEntry::new(
                MemoryType::Context,
                "Python is interpreted",
            ))
            .ok();
        memory
            .store(MemoryEntry::new(
                MemoryType::Context,
                "Rust has zero-cost abstractions",
            ))
            .ok();

        let results = memory.search("rust");
        assert_eq!(results.len(), 2);
    }

    #[test]
    fn test_long_term_memory_get_by_type() {
        let (mut memory, _temp) = create_test_memory();

        memory
            .store(MemoryEntry::new(MemoryType::Decision, "Decision 1"))
            .ok();
        memory
            .store(MemoryEntry::new(MemoryType::Decision, "Decision 2"))
            .ok();
        memory
            .store(MemoryEntry::new(MemoryType::Context, "Context 1"))
            .ok();

        let decisions = memory.get_by_type(MemoryType::Decision);
        assert_eq!(decisions.len(), 2);
    }

    #[test]
    fn test_long_term_memory_get_by_tag() {
        let (mut memory, _temp) = create_test_memory();

        memory
            .store(MemoryEntry::new(MemoryType::Context, "Content 1").with_tag("important"))
            .ok();
        memory
            .store(MemoryEntry::new(MemoryType::Context, "Content 2").with_tag("important"))
            .ok();
        memory
            .store(MemoryEntry::new(MemoryType::Context, "Content 3").with_tag("other"))
            .ok();

        let important = memory.get_by_tag("important");
        assert_eq!(important.len(), 2);
    }

    #[test]
    fn test_long_term_memory_get_important() {
        let (mut memory, _temp) = create_test_memory();

        memory
            .store(
                MemoryEntry::new(MemoryType::Context, "Low").with_importance(ImportanceLevel::Low),
            )
            .ok();
        memory
            .store(
                MemoryEntry::new(MemoryType::Context, "High")
                    .with_importance(ImportanceLevel::High),
            )
            .ok();
        memory
            .store(
                MemoryEntry::new(MemoryType::Context, "Critical")
                    .with_importance(ImportanceLevel::Critical),
            )
            .ok();

        let important = memory.get_important(ImportanceLevel::High, 10);
        assert_eq!(important.len(), 2);
    }

    #[test]
    fn test_long_term_memory_delete() {
        let (mut memory, _temp) = create_test_memory();

        let id = memory
            .store(MemoryEntry::new(MemoryType::Context, "To delete"))
            .expect("Failed to store");

        assert!(memory.get(&id).is_some());
        assert!(memory.delete(&id).expect("Failed to delete"));
        assert!(memory.get(&id).is_none());
    }

    #[test]
    fn test_long_term_memory_stats() {
        let (mut memory, _temp) = create_test_memory();

        memory
            .store(
                MemoryEntry::new(MemoryType::Decision, "D1").with_importance(ImportanceLevel::High),
            )
            .ok();
        memory
            .store(
                MemoryEntry::new(MemoryType::Decision, "D2").with_importance(ImportanceLevel::Low),
            )
            .ok();
        memory
            .store(MemoryEntry::new(MemoryType::Context, "C1"))
            .ok();

        let stats = memory.stats();
        assert_eq!(stats.total_entries, 3);
        assert_eq!(stats.by_type.get("Decision"), Some(&2));
        assert_eq!(stats.by_type.get("Context"), Some(&1));
    }

    #[test]
    fn test_memory_type_dir_name() {
        assert_eq!(MemoryType::SessionSummary.dir_name(), "sessions");
        assert_eq!(MemoryType::ProjectLearning.dir_name(), "learnings");
        assert_eq!(MemoryType::Decision.dir_name(), "decisions");
    }

    #[test]
    fn test_importance_ordering() {
        assert!(ImportanceLevel::Critical > ImportanceLevel::High);
        assert!(ImportanceLevel::High > ImportanceLevel::Medium);
        assert!(ImportanceLevel::Medium > ImportanceLevel::Low);
    }
}