paladin-memory 0.1.0

Memory adapters for the Paladin framework — Garrison (conversation history) and Sanctum (vector search)
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
/// Memory Extraction Service
///
/// Extracts meaningful memories from conversation history and stores them
/// in long-term memory (Sanctum) for later retrieval via RAG.
///
/// This service:
/// - Analyzes conversation history using LLM
/// - Identifies important facts, preferences, and context
/// - Generates embeddings for semantic search
/// - Detects and prevents duplicate memories
/// - Stores memories with proper metadata
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

use paladin_core::platform::container::garrison::GarrisonEntry;
use paladin_core::platform::container::prompt::{PromptItem, PromptRole, PromptType, TextPrompt};
use paladin_core::platform::container::sanctum::{MemoryBuilder, MemoryType, SanctumEntry};
use paladin_ports::output::embedding_port::EmbeddingPort;
use paladin_ports::output::llm_port::{LlmPort, LlmRequest};
use paladin_ports::output::sanctum_port::{SanctumError, SanctumFilter, SanctumPort, SanctumQuery};
use serde_json::Value;

/// Strategy for when to extract memories from conversations.
// MemoryExtractionStrategy moved to crates/paladin-memory/src/config/rag.rs (Task 6.0)
pub use crate::config::rag::MemoryExtractionStrategy;

/// Intermediate representation of extracted memory before storage.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractedMemory {
    pub content: String,
    pub memory_type: MemoryType,
    pub importance: f32,
    pub metadata: HashMap<String, String>,
}

/// Memory Extraction Service.
///
/// Coordinates LLM-based memory extraction and storage via [`SanctumPort`].
/// Depends only on port traits — contains no concrete adapter references.
pub struct MemoryExtractionService {
    llm: Arc<dyn LlmPort>,
    embedding: Arc<dyn EmbeddingPort>,
    sanctum: Arc<dyn SanctumPort>,
}

impl MemoryExtractionService {
    /// Create a new memory extraction service.
    pub fn new(
        llm: Arc<dyn LlmPort>,
        embedding: Arc<dyn EmbeddingPort>,
        sanctum: Arc<dyn SanctumPort>,
    ) -> Self {
        Self {
            llm,
            embedding,
            sanctum,
        }
    }

    /// Extract memories from conversation history.
    ///
    /// Analyzes the conversation, extracts important information, and stores it
    /// in long-term memory (Sanctum) for later retrieval.
    pub async fn extract_memories(
        &self,
        paladin_id: &str,
        conversation: &[GarrisonEntry],
    ) -> Result<Vec<SanctumEntry>, SanctumError> {
        let start = std::time::Instant::now();

        if conversation.is_empty() {
            log::debug!("No conversation history to extract memories from");
            return Ok(Vec::new());
        }

        log::info!(
            "Extracting memories for paladin={}, turns={}",
            paladin_id,
            conversation.len()
        );

        // Build extraction prompt
        let prompt = self.build_extraction_prompt(conversation);

        // Call LLM to extract memories
        let prompt_item = PromptItem::new(PromptType::Text(TextPrompt {
            content: prompt,
            role: PromptRole::User,
        }))
        .map_err(|e| SanctumError::StorageError(format!("Failed to create prompt: {}", e)))?;

        let request = LlmRequest {
            id: uuid::Uuid::new_v4(),
            model: "gpt-4".to_string(),
            prompt: prompt_item,
            attachments: Vec::new(),
            stream: false,
            metadata: HashMap::new(),
        };

        let response = match self.llm.generate(request).await {
            Ok(resp) => resp.content,
            Err(e) => {
                log::warn!(
                    "Memory extraction LLM call failed: {}, continuing without extraction",
                    e
                );
                return Ok(Vec::new());
            }
        };

        // Parse LLM response to get extracted memories
        let extracted = match self.parse_extraction_response(&response) {
            Ok(memories) => memories,
            Err(e) => {
                log::warn!("Failed to parse extraction response: {}, continuing", e);
                return Ok(Vec::new());
            }
        };

        if extracted.is_empty() {
            log::debug!("No memories extracted from conversation");
            return Ok(Vec::new());
        }

        let extracted_count = extracted.len();
        log::debug!("Extracted {} potential memories", extracted_count);

        // Convert to Memory objects and generate embeddings
        let mut memories_to_store = Vec::new();
        for ext_mem in extracted {
            // Generate embedding
            let embedding = match self.embedding.embed_text(&ext_mem.content).await {
                Ok(emb) => emb,
                Err(e) => {
                    log::warn!("Failed to generate embedding for memory: {}, skipping", e);
                    continue;
                }
            };

            // Check for duplicates
            if self
                .check_for_duplicates(paladin_id, &embedding.vector)
                .await?
            {
                log::debug!(
                    "Duplicate memory detected, skipping: {:?}",
                    &ext_mem.content[..50.min(ext_mem.content.len())]
                );
                continue;
            }

            // Convert HashMap<String, String> to HashMap<String, Value>
            let metadata_values: HashMap<String, Value> = ext_mem
                .metadata
                .into_iter()
                .map(|(k, v)| (k, Value::String(v)))
                .collect();

            // Create Memory object
            let memory = MemoryBuilder::new(paladin_id.to_string(), ext_mem.content)
                .memory_type(ext_mem.memory_type)
                .importance(ext_mem.importance)
                .metadata(metadata_values)
                .build()
                .map_err(|e| SanctumError::StorageError(e.to_string()))?;

            // Create SanctumEntry
            let entry = SanctumEntry::new(memory, embedding.vector)
                .map_err(|e| SanctumError::StorageError(e.to_string()))?;

            memories_to_store.push(entry);
        }

        // Store memories in Sanctum
        let stored = self.store_memories(paladin_id, &memories_to_store).await?;

        let duration = start.elapsed();
        let avg_importance = if !stored.is_empty() {
            stored.iter().map(|e| e.memory.importance).sum::<f32>() / stored.len() as f32
        } else {
            0.0
        };

        log::info!(
            "Memory extraction complete: paladin={}, extracted={}, stored={}, avg_importance={:.2}, duration_ms={}",
            paladin_id,
            extracted_count,
            stored.len(),
            avg_importance,
            duration.as_millis()
        );

        Ok(stored)
    }

    /// Build extraction prompt from conversation history.
    fn build_extraction_prompt(&self, conversation: &[GarrisonEntry]) -> String {
        let mut prompt = String::from(EXTRACTION_PROMPT);
        prompt.push_str("\n\nConversation:\n");

        for entry in conversation {
            prompt.push_str(&format!("{:?}: {}\n", entry.role, entry.content));
        }

        prompt.push_str("\n\nExtract important memories as JSON array:");
        prompt
    }

    /// Parse LLM extraction response into structured memories.
    fn parse_extraction_response(&self, response: &str) -> Result<Vec<ExtractedMemory>, String> {
        // Try to extract JSON from response (might be wrapped in markdown)
        let json_str = if let Some(start) = response.find('[') {
            if let Some(end) = response.rfind(']') {
                &response[start..=end]
            } else {
                response
            }
        } else {
            response
        };

        serde_json::from_str::<Vec<ExtractedMemory>>(json_str)
            .map_err(|e| format!("Failed to parse JSON: {}", e))
    }

    /// Check if a similar memory already exists (>0.95 similarity).
    async fn check_for_duplicates(
        &self,
        paladin_id: &str,
        embedding: &[f32],
    ) -> Result<bool, SanctumError> {
        let query = SanctumQuery::new(embedding.to_vec(), 1)
            .with_filter(SanctumFilter::new().paladin_id(paladin_id.to_string()))
            .with_min_score(0.95);

        let results = self.sanctum.search(query).await?;
        Ok(!results.is_empty())
    }

    /// Store memories in batch via Sanctum.
    async fn store_memories(
        &self,
        _paladin_id: &str,
        memories: &[SanctumEntry],
    ) -> Result<Vec<SanctumEntry>, SanctumError> {
        let mut stored: Vec<SanctumEntry> = Vec::new();

        for entry in memories {
            match self.sanctum.store(entry.clone()).await {
                Ok(_) => {
                    stored.push(entry.clone());
                }
                Err(e) => {
                    log::warn!("Failed to store memory: {}, continuing", e);
                }
            }
        }

        Ok(stored)
    }
}

/// LLM prompt template for memory extraction.
const EXTRACTION_PROMPT: &str = r#"You are a memory extraction assistant. Analyze the following conversation and extract important memories.

For each memory, provide:
1. content: The actual information to remember (be specific and complete)
2. memory_type: One of "Episodic", "Semantic", "Procedural"
3. importance: 0.0 to 1.0 score indicating how important this information is
4. metadata: Optional key-value pairs for additional context (as an object)

Memory Types:
- Episodic: Specific events, conversations, or experiences
- Semantic: Facts, knowledge, preferences, and general information
- Procedural: How-to instructions, procedures, and workflows

Rules:
- Extract only genuinely important information worth remembering long-term
- Be specific and include relevant details
- Avoid extracting trivial or transient information
- Combine related information into single memories when appropriate
- Use proper memory types

Return ONLY a JSON array of memories, no additional text."#;

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use paladin_core::platform::container::garrison::ConversationRole;
    use paladin_core::platform::container::sanctum::MemoryBuilder;
    use paladin_ports::output::embedding_port::{Embedding, EmbeddingError, EmbeddingPort};
    use paladin_ports::output::llm_port::{
        FinishReason, LlmError, LlmPort, LlmRequest, LlmResponse, ProviderCapabilities,
        StreamingResponse, TokenUsage,
    };
    use paladin_ports::output::sanctum_port::{
        SanctumError, SanctumFilter, SanctumPort, SanctumQuery, SanctumSearchResult,
    };
    use std::sync::Arc;

    // ── Strategy / serialization tests ────────────────────────────────────────

    #[test]
    fn test_extraction_strategy_default() {
        assert_eq!(
            MemoryExtractionStrategy::default(),
            MemoryExtractionStrategy::OnCompletion
        );
    }

    #[test]
    fn test_extraction_strategy_equality() {
        assert_eq!(
            MemoryExtractionStrategy::EveryTurn,
            MemoryExtractionStrategy::EveryTurn
        );
        assert_ne!(
            MemoryExtractionStrategy::EveryTurn,
            MemoryExtractionStrategy::OnCompletion
        );
    }

    #[test]
    fn test_extraction_strategy_threshold() {
        let strategy = MemoryExtractionStrategy::Threshold { importance: 7 };
        if let MemoryExtractionStrategy::Threshold { importance } = strategy {
            assert_eq!(importance, 7);
        } else {
            panic!("Expected Threshold variant");
        }
    }

    #[test]
    fn test_extracted_memory_serialization() {
        let mut metadata = HashMap::new();
        metadata.insert("source".to_string(), "conversation".to_string());

        let memory = ExtractedMemory {
            content: "User prefers dark mode".to_string(),
            memory_type: MemoryType::Semantic,
            importance: 0.8,
            metadata,
        };

        let json = serde_json::to_string(&memory).unwrap();
        let deserialized: ExtractedMemory = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.content, memory.content);
        assert_eq!(deserialized.importance, memory.importance);
    }

    #[test]
    fn test_extraction_strategy_serialization() {
        let strategy = MemoryExtractionStrategy::Threshold { importance: 8 };
        let json = serde_json::to_string(&strategy).unwrap();
        let deserialized: MemoryExtractionStrategy = serde_json::from_str(&json).unwrap();
        assert_eq!(strategy, deserialized);
    }

    // ── Mock helpers ──────────────────────────────────────────────────────────

    struct MockLlmPort {
        response: String,
        should_fail: bool,
    }

    #[async_trait]
    impl LlmPort for MockLlmPort {
        async fn generate(&self, request: LlmRequest) -> Result<LlmResponse, LlmError> {
            if self.should_fail {
                Err(LlmError::ProcessingError("Mock LLM failure".to_string()))
            } else {
                Ok(LlmResponse {
                    id: uuid::Uuid::new_v4(),
                    request_id: request.id,
                    model: "mock-model".to_string(),
                    content: self.response.clone(),
                    finish_reason: FinishReason::Stop,
                    usage: TokenUsage {
                        prompt_tokens: 10,
                        completion_tokens: 20,
                        total_tokens: 30,
                    },
                    created_at: chrono::Utc::now(),
                    metadata: HashMap::new(),
                    function_call: None,
                })
            }
        }

        async fn generate_stream(
            &self,
            _request: LlmRequest,
        ) -> Result<
            Box<dyn futures::Stream<Item = Result<StreamingResponse, LlmError>> + Send>,
            LlmError,
        > {
            Err(LlmError::ProcessingError(
                "Streaming not supported in mock".to_string(),
            ))
        }

        async fn validate_model(&self, _model: &str) -> Result<bool, LlmError> {
            Ok(true)
        }

        async fn get_available_models(&self) -> Result<Vec<String>, LlmError> {
            Ok(vec!["mock-model".to_string()])
        }

        fn get_provider_name(&self) -> &'static str {
            "mock-provider"
        }

        fn get_capabilities(&self) -> ProviderCapabilities {
            ProviderCapabilities::default()
        }
    }

    struct MockEmbeddingPort {
        dimension: usize,
        should_fail: bool,
    }

    #[async_trait]
    impl EmbeddingPort for MockEmbeddingPort {
        async fn embed_text(&self, _text: &str) -> Result<Embedding, EmbeddingError> {
            if self.should_fail {
                Err(EmbeddingError::NetworkError(
                    "Mock embedding failure".to_string(),
                ))
            } else {
                Ok(Embedding {
                    vector: vec![0.1; self.dimension],
                    model: "mock-embedding-model".to_string(),
                    dimension: self.dimension,
                    token_count: None,
                })
            }
        }

        async fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Embedding>, EmbeddingError> {
            if self.should_fail {
                Err(EmbeddingError::NetworkError(
                    "Mock embedding failure".to_string(),
                ))
            } else {
                Ok(texts
                    .iter()
                    .map(|_| Embedding {
                        vector: vec![0.1; self.dimension],
                        model: "mock-embedding-model".to_string(),
                        dimension: self.dimension,
                        token_count: None,
                    })
                    .collect())
            }
        }

        fn dimension(&self) -> usize {
            self.dimension
        }

        fn model_name(&self) -> &str {
            "mock-embedding-model"
        }
    }

    struct MockSanctumPort {
        stored_entries: std::sync::Mutex<Vec<SanctumEntry>>,
        should_fail_store: bool,
        duplicate_threshold: f32,
    }

    impl MockSanctumPort {
        fn new() -> Self {
            Self {
                stored_entries: std::sync::Mutex::new(Vec::new()),
                should_fail_store: false,
                // Default: threshold above 1.0 → duplicates never triggered
                duplicate_threshold: 1.1,
            }
        }

        fn with_duplicate_threshold(mut self, threshold: f32) -> Self {
            self.duplicate_threshold = threshold;
            self
        }
    }

    #[async_trait]
    impl SanctumPort for MockSanctumPort {
        async fn store(&self, entry: SanctumEntry) -> Result<(), SanctumError> {
            if self.should_fail_store {
                Err(SanctumError::StorageError(
                    "Mock storage failure".to_string(),
                ))
            } else {
                self.stored_entries.lock().unwrap().push(entry);
                Ok(())
            }
        }

        async fn store_batch(&self, entries: Vec<SanctumEntry>) -> Result<(), SanctumError> {
            for entry in entries {
                self.store(entry).await?;
            }
            Ok(())
        }

        async fn search(
            &self,
            query: SanctumQuery,
        ) -> Result<Vec<SanctumSearchResult>, SanctumError> {
            if query.min_score.unwrap_or(0.0) >= self.duplicate_threshold {
                let mock_memory = MemoryBuilder::new(
                    "test-paladin".to_string(),
                    "Existing duplicate memory".to_string(),
                )
                .memory_type(MemoryType::Semantic)
                .build()
                .unwrap();
                let mock_entry = SanctumEntry::new(mock_memory, vec![0.1; 1536]).unwrap();
                Ok(vec![SanctumSearchResult::new(mock_entry, 0.96)])
            } else {
                Ok(Vec::new())
            }
        }

        async fn delete(&self, _id: &str) -> Result<bool, SanctumError> {
            Ok(true)
        }

        async fn update(&self, _entry: SanctumEntry) -> Result<(), SanctumError> {
            Ok(())
        }

        async fn count(&self, _filter: Option<SanctumFilter>) -> Result<usize, SanctumError> {
            Ok(self.stored_entries.lock().unwrap().len())
        }
    }

    // ── Async service tests ───────────────────────────────────────────────────

    #[tokio::test]
    async fn test_successful_extraction_with_multiple_memory_types() {
        let llm_response = r#"[
            {
                "content": "User prefers dark mode in all applications",
                "memory_type": "Semantic",
                "importance": 0.8,
                "metadata": {"category": "ui"}
            },
            {
                "content": "User is learning Rust programming language",
                "memory_type": "Semantic",
                "importance": 0.9,
                "metadata": {"topic": "programming"}
            },
            {
                "content": "User wants to build a web application",
                "memory_type": "Episodic",
                "importance": 0.85,
                "metadata": {}
            }
        ]"#;

        let llm = Arc::new(MockLlmPort {
            response: llm_response.to_string(),
            should_fail: false,
        });
        let embedding = Arc::new(MockEmbeddingPort {
            dimension: 1536,
            should_fail: false,
        });
        let sanctum = Arc::new(MockSanctumPort::new());

        let service = MemoryExtractionService::new(llm, embedding, sanctum.clone());

        let conversation = vec![
            GarrisonEntry::new(ConversationRole::User, "I prefer dark mode".to_string()),
            GarrisonEntry::new(
                ConversationRole::Assistant,
                "Noted! I'll remember your preference".to_string(),
            ),
        ];

        let result = service
            .extract_memories("test-paladin", &conversation)
            .await
            .unwrap();

        assert_eq!(result.len(), 3);
        assert_eq!(sanctum.stored_entries.lock().unwrap().len(), 3);
    }

    #[tokio::test]
    async fn test_importance_scoring_correctly_assigned() {
        let llm_response = r#"[
            {
                "content": "High importance memory",
                "memory_type": "Semantic",
                "importance": 0.95,
                "metadata": {}
            },
            {
                "content": "Low importance memory",
                "memory_type": "Episodic",
                "importance": 0.3,
                "metadata": {}
            }
        ]"#;

        let llm = Arc::new(MockLlmPort {
            response: llm_response.to_string(),
            should_fail: false,
        });
        let embedding = Arc::new(MockEmbeddingPort {
            dimension: 1536,
            should_fail: false,
        });
        let sanctum = Arc::new(MockSanctumPort::new());

        let service = MemoryExtractionService::new(llm, embedding, sanctum.clone());

        let conversation = vec![GarrisonEntry::new(
            ConversationRole::User,
            "Test content".to_string(),
        )];

        let result = service
            .extract_memories("test-paladin", &conversation)
            .await
            .unwrap();

        assert_eq!(result.len(), 2);
        assert_eq!(result[0].memory.importance, 0.95);
        assert_eq!(result[1].memory.importance, 0.3);
    }

    #[tokio::test]
    async fn test_duplicate_detection_prevents_restorage() {
        let llm_response = r#"[
            {
                "content": "Duplicate memory content",
                "memory_type": "Semantic",
                "importance": 0.8,
                "metadata": {}
            }
        ]"#;

        let llm = Arc::new(MockLlmPort {
            response: llm_response.to_string(),
            should_fail: false,
        });
        let embedding = Arc::new(MockEmbeddingPort {
            dimension: 1536,
            should_fail: false,
        });
        let sanctum = Arc::new(MockSanctumPort::new().with_duplicate_threshold(0.95));

        let service = MemoryExtractionService::new(llm, embedding, sanctum.clone());

        let conversation = vec![GarrisonEntry::new(
            ConversationRole::User,
            "Duplicate content".to_string(),
        )];

        let result = service
            .extract_memories("test-paladin", &conversation)
            .await
            .unwrap();

        assert_eq!(result.len(), 0);
        assert_eq!(sanctum.stored_entries.lock().unwrap().len(), 0);
    }

    #[tokio::test]
    async fn test_llm_failure_handled_gracefully() {
        let llm = Arc::new(MockLlmPort {
            response: String::new(),
            should_fail: true,
        });
        let embedding = Arc::new(MockEmbeddingPort {
            dimension: 1536,
            should_fail: false,
        });
        let sanctum = Arc::new(MockSanctumPort::new());

        let service = MemoryExtractionService::new(llm, embedding, sanctum);

        let conversation = vec![GarrisonEntry::new(
            ConversationRole::User,
            "Test".to_string(),
        )];

        let result = service
            .extract_memories("test-paladin", &conversation)
            .await
            .unwrap();

        assert_eq!(result.len(), 0);
    }

    #[tokio::test]
    async fn test_empty_conversation_returns_empty() {
        let llm = Arc::new(MockLlmPort {
            response: String::new(),
            should_fail: false,
        });
        let embedding = Arc::new(MockEmbeddingPort {
            dimension: 1536,
            should_fail: false,
        });
        let sanctum = Arc::new(MockSanctumPort::new());

        let service = MemoryExtractionService::new(llm, embedding, sanctum);

        let result = service.extract_memories("test-paladin", &[]).await.unwrap();

        assert_eq!(result.len(), 0);
    }

    #[tokio::test]
    async fn test_malformed_json_response_handled() {
        let llm = Arc::new(MockLlmPort {
            response: "This is not valid JSON".to_string(),
            should_fail: false,
        });
        let embedding = Arc::new(MockEmbeddingPort {
            dimension: 1536,
            should_fail: false,
        });
        let sanctum = Arc::new(MockSanctumPort::new());

        let service = MemoryExtractionService::new(llm, embedding, sanctum);

        let conversation = vec![GarrisonEntry::new(
            ConversationRole::User,
            "Test".to_string(),
        )];

        let result = service
            .extract_memories("test-paladin", &conversation)
            .await
            .unwrap();

        assert_eq!(result.len(), 0);
    }

    #[tokio::test]
    async fn test_embedding_failure_skips_memory() {
        let llm_response = r#"[
            {
                "content": "Test memory",
                "memory_type": "Semantic",
                "importance": 0.8,
                "metadata": {}
            }
        ]"#;

        let llm = Arc::new(MockLlmPort {
            response: llm_response.to_string(),
            should_fail: false,
        });
        let embedding = Arc::new(MockEmbeddingPort {
            dimension: 1536,
            should_fail: true,
        });
        let sanctum = Arc::new(MockSanctumPort::new());

        let service = MemoryExtractionService::new(llm, embedding, sanctum);

        let conversation = vec![GarrisonEntry::new(
            ConversationRole::User,
            "Test".to_string(),
        )];

        let result = service
            .extract_memories("test-paladin", &conversation)
            .await
            .unwrap();

        assert_eq!(result.len(), 0);
    }
}