paladin-memory 0.5.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
//! In-memory implementation of SanctumPort for development and testing
//!
//! This adapter provides a fast, thread-safe in-memory storage for semantic memories
//! with cosine similarity search. It's ideal for:
//! - Development and testing
//! - Small-scale deployments
//! - Prototyping
//!
//! Features:
//! - Thread-safe concurrent access with RwLock
//! - LRU eviction when capacity is reached
//! - Cosine similarity-based semantic search
//! - Metadata filtering (paladin_id, memory_type, importance, timestamps)
//!
//! Performance characteristics:
//! - Storage: O(1)
//! - Search: O(n) brute-force comparison (suitable for < 10K vectors)
//! - Memory overhead: All vectors kept in RAM

use async_trait::async_trait;
use paladin_core::platform::container::sanctum::SanctumEntry;
use paladin_ports::output::sanctum_port::{
    SanctumError, SanctumFilter, SanctumPort, SanctumQuery, SanctumSearchResult,
};
use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, RwLock};
use uuid::Uuid;

/// Configuration for [`InMemorySanctum`].
///
/// Controls the maximum number of entries held in memory before LRU eviction
/// removes the least-recently-used entries to make room for new ones.
///
/// # Example
/// ```no_run
/// use paladin_memory::sanctum::{InMemorySanctum, InMemorySanctumConfig};
///
/// let config = InMemorySanctumConfig { max_entries: 5_000 };
/// let sanctum = InMemorySanctum::with_config(config);
/// ```
#[derive(Debug, Clone)]
pub struct InMemorySanctumConfig {
    /// Maximum number of entries to store.
    ///
    /// When this limit is reached the oldest (least-recently-used) entry is
    /// evicted before inserting the new one, keeping memory use bounded.
    pub max_entries: usize,
}

impl Default for InMemorySanctumConfig {
    fn default() -> Self {
        Self {
            max_entries: 10_000,
        }
    }
}

/// In-memory implementation of [`SanctumPort`].
///
/// Thread-safe storage with LRU eviction and brute-force cosine similarity
/// search. Suitable for development, testing, and small-scale production
/// deployments with fewer than ~10 000 vectors.
///
/// For larger workloads prefer `QdrantSanctumAdapter` (requires the `qdrant`
/// feature).
///
/// # Example
/// ```no_run
/// use paladin_memory::sanctum::InMemorySanctum;
///
/// let sanctum = InMemorySanctum::new(1_000);
/// ```
pub struct InMemorySanctum {
    /// Entry storage keyed by memory ID (Uuid)
    storage: Arc<RwLock<HashMap<Uuid, SanctumEntry>>>,

    /// LRU tracking: oldest entries at the front
    lru_queue: Arc<RwLock<VecDeque<Uuid>>>,

    /// Configuration
    config: InMemorySanctumConfig,
}

impl InMemorySanctum {
    /// Create a new `InMemorySanctum` with the given capacity.
    ///
    /// # Arguments
    /// * `max_entries` - Maximum number of entries before LRU eviction.
    pub fn new(max_entries: usize) -> Self {
        Self {
            storage: Arc::new(RwLock::new(HashMap::new())),
            lru_queue: Arc::new(RwLock::new(VecDeque::new())),
            config: InMemorySanctumConfig { max_entries },
        }
    }

    /// Create a new `InMemorySanctum` with custom configuration.
    pub fn with_config(config: InMemorySanctumConfig) -> Self {
        Self {
            storage: Arc::new(RwLock::new(HashMap::new())),
            lru_queue: Arc::new(RwLock::new(VecDeque::new())),
            config,
        }
    }

    /// Calculate cosine similarity between two vectors.
    ///
    /// Returns a score between -1.0 and 1.0, where 1.0 means identical direction.
    fn cosine_similarity(a: &[f32], b: &[f32]) -> Result<f32, SanctumError> {
        if a.len() != b.len() {
            return Err(SanctumError::InvalidDimension(format!(
                "Vector dimensions don't match: {} vs {}",
                a.len(),
                b.len()
            )));
        }

        if a.is_empty() {
            return Err(SanctumError::InvalidDimension(
                "Cannot calculate similarity of empty vectors".to_string(),
            ));
        }

        // Dot product
        let dot_product: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();

        // Magnitudes
        let mag_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
        let mag_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();

        if mag_a == 0.0 || mag_b == 0.0 {
            return Ok(0.0); // Treat zero vectors as orthogonal
        }

        Ok(dot_product / (mag_a * mag_b))
    }

    /// Check if an entry matches the given filter.
    fn matches_filter(entry: &SanctumEntry, filter: &SanctumFilter) -> bool {
        // Filter by paladin_id
        if let Some(ref paladin_id) = filter.paladin_id
            && &entry.memory.paladin_id != paladin_id
        {
            return false;
        }

        // Filter by memory_type
        if let Some(memory_type) = filter.memory_type
            && entry.memory.memory_type != memory_type
        {
            return false;
        }

        // Filter by created_after
        if let Some(created_after) = filter.created_after
            && entry.memory.created_at < created_after
        {
            return false;
        }

        // Filter by created_before
        if let Some(created_before) = filter.created_before
            && entry.memory.created_at > created_before
        {
            return false;
        }

        // Filter by min_importance
        if let Some(min_importance) = filter.min_importance
            && entry.memory.importance < min_importance
        {
            return false;
        }

        // Filter by metadata (exact match for now)
        for (key, value) in &filter.metadata_filters {
            match entry.memory.metadata.get(key) {
                Some(entry_value) if entry_value == value => {}
                _ => return false,
            }
        }

        true
    }

    /// Perform LRU eviction if capacity is exceeded.
    fn evict_if_needed(&self) {
        let storage = self
            .storage
            .read()
            .expect("Failed to acquire read lock on storage");

        if storage.len() >= self.config.max_entries {
            drop(storage); // Release read lock before acquiring write lock

            let mut lru = self
                .lru_queue
                .write()
                .expect("Failed to acquire write lock on LRU queue");
            let mut storage = self
                .storage
                .write()
                .expect("Failed to acquire write lock on storage");

            // Remove oldest entry
            if let Some(oldest_id) = lru.pop_front() {
                storage.remove(&oldest_id);
            }
        }
    }

    /// Update LRU queue when accessing an entry.
    fn touch_entry(&self, id: &Uuid) {
        let mut lru = self
            .lru_queue
            .write()
            .expect("Failed to acquire write lock on LRU queue");

        // Remove from current position
        if let Some(pos) = lru.iter().position(|x| x == id) {
            lru.remove(pos);
        }

        // Add to back (most recently used)
        lru.push_back(*id);
    }
}

#[async_trait]
impl SanctumPort for InMemorySanctum {
    /// Store a single entry in memory.
    async fn store(&self, entry: SanctumEntry) -> Result<(), SanctumError> {
        // Check for capacity and evict if needed
        self.evict_if_needed();

        let id = entry.memory.id;

        let mut storage = self
            .storage
            .write()
            .expect("Failed to acquire write lock on storage");

        storage.insert(id, entry);
        drop(storage); // Release write lock

        // Update LRU queue
        self.touch_entry(&id);

        Ok(())
    }

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

    /// Perform semantic search with cosine similarity.
    async fn search(&self, query: SanctumQuery) -> Result<Vec<SanctumSearchResult>, SanctumError> {
        let storage = self
            .storage
            .read()
            .expect("Failed to acquire read lock on storage");

        let mut results = Vec::new();

        // Calculate similarity for all entries that match the filter
        for entry in storage.values() {
            // Apply filter if provided
            if let Some(ref filter) = query.filter
                && !Self::matches_filter(entry, filter)
            {
                continue;
            }

            // Calculate cosine similarity
            let score = Self::cosine_similarity(&query.embedding, &entry.embedding)?;

            // Apply min_score filter
            if let Some(min_score) = query.min_score
                && score < min_score
            {
                continue;
            }

            results.push(SanctumSearchResult {
                entry: entry.clone(),
                score,
            });
        }

        // Sort by score descending (highest similarity first)
        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        // Apply top_k limit
        results.truncate(query.top_k);

        Ok(results)
    }

    /// Delete an entry by ID.
    async fn delete(&self, id: &str) -> Result<bool, SanctumError> {
        // Parse the id string to Uuid
        let uuid = Uuid::parse_str(id)
            .map_err(|e| SanctumError::NotFound(format!("Invalid UUID format: {}", e)))?;

        let mut storage = self
            .storage
            .write()
            .expect("Failed to acquire write lock on storage");

        let removed = storage.remove(&uuid).is_some();

        if removed {
            drop(storage); // Release write lock

            // Remove from LRU queue
            let mut lru = self
                .lru_queue
                .write()
                .expect("Failed to acquire write lock on LRU queue");

            if let Some(pos) = lru.iter().position(|x| x == &uuid) {
                lru.remove(pos);
            }
        }

        Ok(removed)
    }

    /// Update an existing entry.
    async fn update(&self, entry: SanctumEntry) -> Result<(), SanctumError> {
        let id = entry.memory.id;

        let mut storage = self
            .storage
            .write()
            .expect("Failed to acquire write lock on storage");

        // Check if entry exists
        if !storage.contains_key(&id) {
            return Err(SanctumError::NotFound(format!("Entry not found: {}", id)));
        }

        storage.insert(id, entry);
        drop(storage); // Release write lock

        // Update LRU queue (treat update as access)
        self.touch_entry(&id);

        Ok(())
    }

    /// Count entries matching optional filter.
    async fn count(&self, filter: Option<SanctumFilter>) -> Result<usize, SanctumError> {
        let storage = self
            .storage
            .read()
            .expect("Failed to acquire read lock on storage");

        if let Some(filter) = filter {
            Ok(storage
                .values()
                .filter(|entry| Self::matches_filter(entry, &filter))
                .count())
        } else {
            Ok(storage.len())
        }
    }
}

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

    #[test]
    fn test_cosine_similarity_identical() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        let similarity = InMemorySanctum::cosine_similarity(&a, &b).unwrap();
        assert!((similarity - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_cosine_similarity_orthogonal() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![0.0, 1.0, 0.0];
        let similarity = InMemorySanctum::cosine_similarity(&a, &b).unwrap();
        assert!((similarity - 0.0).abs() < 1e-6);
    }

    #[test]
    fn test_cosine_similarity_opposite() {
        let a = vec![1.0, 0.0, 0.0];
        let b = vec![-1.0, 0.0, 0.0];
        let similarity = InMemorySanctum::cosine_similarity(&a, &b).unwrap();
        assert!((similarity - (-1.0)).abs() < 1e-6);
    }

    #[test]
    fn test_cosine_similarity_dimension_mismatch() {
        let a = vec![1.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        let result = InMemorySanctum::cosine_similarity(&a, &b);
        assert!(result.is_err());
    }

    #[test]
    fn test_cosine_similarity_empty() {
        let a: Vec<f32> = vec![];
        let b: Vec<f32> = vec![];
        let result = InMemorySanctum::cosine_similarity(&a, &b);
        assert!(result.is_err());
    }

    #[test]
    fn test_cosine_similarity_zero_vector() {
        let a = vec![0.0, 0.0, 0.0];
        let b = vec![1.0, 0.0, 0.0];
        let similarity = InMemorySanctum::cosine_similarity(&a, &b).unwrap();
        assert_eq!(similarity, 0.0);
    }
}