oxirag 0.1.1

A four-layer RAG engine with SMT-based logic verification and knowledge graph support
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
//! Embedding cache with LRU eviction.
//!
//! This module provides a caching wrapper for embedding providers that stores
//! computed embeddings in an LRU cache to avoid redundant computations.

use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::sync::RwLock;
use std::sync::atomic::{AtomicU64, Ordering};

use async_trait::async_trait;

use crate::error::EmbeddingError;
use crate::layer1_echo::traits::EmbeddingProvider;

/// Configuration for the embedding cache.
#[derive(Debug, Clone)]
pub struct EmbeddingCacheConfig {
    /// Maximum number of entries in the cache.
    pub max_entries: usize,
    /// Whether to track cache statistics.
    pub track_stats: bool,
}

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

impl EmbeddingCacheConfig {
    /// Create a new cache configuration.
    #[must_use]
    pub fn new(max_entries: usize) -> Self {
        Self {
            max_entries,
            track_stats: true,
        }
    }

    /// Set whether to track cache statistics.
    #[must_use]
    pub fn with_stats_tracking(mut self, track: bool) -> Self {
        self.track_stats = track;
        self
    }
}

/// Statistics for the embedding cache.
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    /// Number of cache hits.
    pub hits: u64,
    /// Number of cache misses.
    pub misses: u64,
    /// Number of evictions.
    pub evictions: u64,
    /// Current number of entries.
    pub entries: usize,
}

impl CacheStats {
    /// Calculate the hit rate as a percentage.
    #[must_use]
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            #[allow(clippy::cast_precision_loss)]
            {
                (self.hits as f64 / total as f64) * 100.0
            }
        }
    }
}

/// An entry in the LRU cache.
struct CacheEntry {
    /// The cached embedding.
    embedding: Vec<f32>,
    /// The last access time (used for LRU ordering).
    last_access: u64,
}

/// A caching wrapper for embedding providers with LRU eviction.
///
/// This wrapper caches embeddings based on the input text hash to avoid
/// redundant embedding computations. When the cache is full, the least
/// recently used entries are evicted.
pub struct CachedEmbeddingProvider<P: EmbeddingProvider> {
    /// The underlying embedding provider.
    provider: P,
    /// The cache configuration.
    config: EmbeddingCacheConfig,
    /// The cache storage (text hash -> embedding).
    cache: RwLock<HashMap<u64, CacheEntry>>,
    /// Monotonically increasing counter for LRU ordering.
    access_counter: AtomicU64,
    /// Cache statistics.
    hits: AtomicU64,
    misses: AtomicU64,
    evictions: AtomicU64,
}

impl<P: EmbeddingProvider> CachedEmbeddingProvider<P> {
    /// Create a new cached embedding provider.
    #[must_use]
    pub fn new(provider: P, config: EmbeddingCacheConfig) -> Self {
        Self {
            provider,
            config,
            cache: RwLock::new(HashMap::new()),
            access_counter: AtomicU64::new(0),
            hits: AtomicU64::new(0),
            misses: AtomicU64::new(0),
            evictions: AtomicU64::new(0),
        }
    }

    /// Create a cached provider with default configuration.
    #[must_use]
    pub fn with_defaults(provider: P) -> Self {
        Self::new(provider, EmbeddingCacheConfig::default())
    }

    /// Get the underlying provider.
    #[must_use]
    pub fn provider(&self) -> &P {
        &self.provider
    }

    /// Get cache statistics.
    ///
    /// # Panics
    ///
    /// Panics if the cache lock is poisoned.
    #[must_use]
    pub fn stats(&self) -> CacheStats {
        let cache = self.cache.read().expect("cache lock poisoned");
        CacheStats {
            hits: self.hits.load(Ordering::Relaxed),
            misses: self.misses.load(Ordering::Relaxed),
            evictions: self.evictions.load(Ordering::Relaxed),
            entries: cache.len(),
        }
    }

    /// Clear the cache.
    ///
    /// # Panics
    ///
    /// Panics if the cache lock is poisoned.
    pub fn clear_cache(&self) {
        let mut cache = self.cache.write().expect("cache lock poisoned");
        cache.clear();
    }

    /// Compute a hash for the given text.
    fn hash_text(text: &str) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        let mut hasher = DefaultHasher::new();
        text.hash(&mut hasher);
        hasher.finish()
    }

    /// Try to get an embedding from the cache.
    fn get_cached(&self, text_hash: u64) -> Option<Vec<f32>> {
        let mut cache = self.cache.write().expect("cache lock poisoned");
        if let Some(entry) = cache.get_mut(&text_hash) {
            // Update last access time
            entry.last_access = self.access_counter.fetch_add(1, Ordering::Relaxed);
            if self.config.track_stats {
                self.hits.fetch_add(1, Ordering::Relaxed);
            }
            Some(entry.embedding.clone())
        } else {
            if self.config.track_stats {
                self.misses.fetch_add(1, Ordering::Relaxed);
            }
            None
        }
    }

    /// Insert an embedding into the cache, evicting LRU entries if necessary.
    fn insert_cached(&self, text_hash: u64, embedding: Vec<f32>) {
        let mut cache = self.cache.write().expect("cache lock poisoned");

        // Evict if necessary
        while cache.len() >= self.config.max_entries {
            // Find the least recently used entry
            let lru_key = cache
                .iter()
                .min_by_key(|(_, entry)| entry.last_access)
                .map(|(k, _)| *k);

            if let Some(key) = lru_key {
                cache.remove(&key);
                if self.config.track_stats {
                    self.evictions.fetch_add(1, Ordering::Relaxed);
                }
            } else {
                break;
            }
        }

        let access_time = self.access_counter.fetch_add(1, Ordering::Relaxed);
        cache.insert(
            text_hash,
            CacheEntry {
                embedding,
                last_access: access_time,
            },
        );
    }
}

#[async_trait]
impl<P: EmbeddingProvider> EmbeddingProvider for CachedEmbeddingProvider<P> {
    async fn embed(&self, text: &str) -> Result<Vec<f32>, EmbeddingError> {
        let text_hash = Self::hash_text(text);

        // Check cache first
        if let Some(embedding) = self.get_cached(text_hash) {
            return Ok(embedding);
        }

        // Cache miss - compute embedding
        let embedding = self.provider.embed(text).await?;

        // Store in cache
        self.insert_cached(text_hash, embedding.clone());

        Ok(embedding)
    }

    async fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>, EmbeddingError> {
        if texts.is_empty() {
            return Err(EmbeddingError::EmptyInput);
        }

        // Separate cached and uncached texts
        let mut results: Vec<Option<Vec<f32>>> = vec![None; texts.len()];
        let mut uncached_indices: Vec<usize> = Vec::new();
        let mut uncached_texts: Vec<&str> = Vec::new();

        for (i, text) in texts.iter().enumerate() {
            let text_hash = Self::hash_text(text);
            if let Some(embedding) = self.get_cached(text_hash) {
                results[i] = Some(embedding);
            } else {
                uncached_indices.push(i);
                uncached_texts.push(text);
            }
        }

        // If all cached, return immediately
        if uncached_texts.is_empty() {
            return Ok(results.into_iter().flatten().collect());
        }

        // Compute embeddings for uncached texts
        let new_embeddings = self.provider.embed_batch(&uncached_texts).await?;

        // Store new embeddings in cache and results
        for (i, embedding) in uncached_indices.into_iter().zip(new_embeddings) {
            let text_hash = Self::hash_text(texts[i]);
            self.insert_cached(text_hash, embedding.clone());
            results[i] = Some(embedding);
        }

        Ok(results.into_iter().flatten().collect())
    }

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

    fn model_id(&self) -> &str {
        self.provider.model_id()
    }
}

#[cfg(disabled)]
mod tests {
    use super::*;
    use crate::layer1_echo::embedding::MockEmbeddingProvider;

    #[tokio::test]
    async fn test_cache_hit() {
        let provider = MockEmbeddingProvider::new(64);
        let cached = CachedEmbeddingProvider::with_defaults(provider);

        // First call - cache miss
        let emb1 = cached.embed("test text").await.unwrap();
        assert_eq!(cached.stats().hits, 0);
        assert_eq!(cached.stats().misses, 1);

        // Second call - cache hit
        let emb2 = cached.embed("test text").await.unwrap();
        assert_eq!(cached.stats().hits, 1);
        assert_eq!(cached.stats().misses, 1);

        // Embeddings should be identical
        assert_eq!(emb1, emb2);
    }

    #[tokio::test]
    async fn test_cache_different_texts() {
        let provider = MockEmbeddingProvider::new(64);
        let cached = CachedEmbeddingProvider::with_defaults(provider);

        cached.embed("text 1").await.unwrap();
        cached.embed("text 2").await.unwrap();

        assert_eq!(cached.stats().misses, 2);
        assert_eq!(cached.stats().entries, 2);
    }

    #[tokio::test]
    async fn test_cache_lru_eviction() {
        let provider = MockEmbeddingProvider::new(32);
        let config = EmbeddingCacheConfig::new(3);
        let cached = CachedEmbeddingProvider::new(provider, config);

        // Fill the cache
        cached.embed("text 1").await.unwrap();
        cached.embed("text 2").await.unwrap();
        cached.embed("text 3").await.unwrap();
        assert_eq!(cached.stats().entries, 3);
        assert_eq!(cached.stats().evictions, 0);

        // Access text 1 to make it recently used
        cached.embed("text 1").await.unwrap();

        // Add a new entry - should evict text 2 (LRU)
        cached.embed("text 4").await.unwrap();
        assert_eq!(cached.stats().entries, 3);
        assert_eq!(cached.stats().evictions, 1);

        // text 1 should still be cached (was recently accessed)
        let stats_before = cached.stats();
        cached.embed("text 1").await.unwrap();
        assert_eq!(cached.stats().hits, stats_before.hits + 1);
    }

    #[tokio::test]
    async fn test_cache_batch_partial_hit() {
        let provider = MockEmbeddingProvider::new(32);
        let cached = CachedEmbeddingProvider::with_defaults(provider);

        // Pre-populate cache with some texts
        cached.embed("cached text").await.unwrap();

        // Batch with mixed cached and uncached
        let texts = vec!["cached text", "new text 1", "new text 2"];
        let embeddings = cached.embed_batch(&texts).await.unwrap();

        assert_eq!(embeddings.len(), 3);
        // 1 hit for "cached text", 2 misses for new texts + 1 initial miss
        assert_eq!(cached.stats().hits, 1);
        assert_eq!(cached.stats().misses, 3); // 1 initial + 2 from batch
    }

    #[tokio::test]
    async fn test_cache_batch_all_cached() {
        let provider = MockEmbeddingProvider::new(32);
        let cached = CachedEmbeddingProvider::with_defaults(provider);

        // Pre-populate cache
        cached.embed("text 1").await.unwrap();
        cached.embed("text 2").await.unwrap();

        let stats_before = cached.stats();

        // All texts are cached
        let embeddings = cached.embed_batch(&["text 1", "text 2"]).await.unwrap();
        assert_eq!(embeddings.len(), 2);

        // Should have 2 more hits, no additional misses
        assert_eq!(cached.stats().hits, stats_before.hits + 2);
        assert_eq!(cached.stats().misses, stats_before.misses);
    }

    #[tokio::test]
    async fn test_cache_clear() {
        let provider = MockEmbeddingProvider::new(32);
        let cached = CachedEmbeddingProvider::with_defaults(provider);

        cached.embed("text 1").await.unwrap();
        cached.embed("text 2").await.unwrap();
        assert_eq!(cached.stats().entries, 2);

        cached.clear_cache();
        assert_eq!(cached.stats().entries, 0);

        // After clear, should be cache miss
        cached.embed("text 1").await.unwrap();
        assert_eq!(cached.stats().misses, 3); // 2 initial + 1 after clear
    }

    #[tokio::test]
    async fn test_cache_stats_hit_rate() {
        let stats = CacheStats {
            hits: 75,
            misses: 25,
            evictions: 0,
            entries: 100,
        };

        assert!((stats.hit_rate() - 75.0).abs() < 0.001);
    }

    #[tokio::test]
    async fn test_cache_stats_hit_rate_zero() {
        let stats = CacheStats::default();
        assert!((stats.hit_rate() - 0.0).abs() < 0.001);
    }

    #[tokio::test]
    async fn test_cache_config_builder() {
        let config = EmbeddingCacheConfig::new(5000).with_stats_tracking(false);

        assert_eq!(config.max_entries, 5000);
        assert!(!config.track_stats);
    }

    // Property-based tests
    #[cfg(disabled)]
    mod proptest_tests {
        use super::*;
        use proptest::prelude::*;

        proptest! {
            /// Cache size never exceeds max_entries

            fn cache_size_never_exceeds_max(
                max_entries in 1usize..50,
                num_insertions in 1usize..100
            ) {
                tokio_test::block_on(async {
                    let provider = MockEmbeddingProvider::new(32);
                    let config = EmbeddingCacheConfig::new(max_entries);
                    let cached = CachedEmbeddingProvider::new(provider, config);

                    // Insert many different texts
                    for i in 0..num_insertions {
                        let text = format!("text_{}", i);
                        let _ = cached.embed(&text).await;
                    }

                    let stats = cached.stats();
                    prop_assert!(stats.entries <= max_entries,
                        "Cache size {} exceeds max_entries {}", stats.entries, max_entries);
                    Ok(())
                });
            }

            /// Cache hits return identical values
            #[test]
            fn cache_hits_return_same_value(
                text in "[a-z]{5,20}",
                num_accesses in 2usize..10
            ) {
                tokio_test::block_on(async {
                    let provider = MockEmbeddingProvider::new(64);
                    let cached = CachedEmbeddingProvider::with_defaults(provider);

                    // First access
                    let first = cached.embed(&text).await.ok();
                    prop_assert!(first.is_some(), "First embed should succeed");

                    // Subsequent accesses should return same value
                    for _ in 1..num_accesses {
                        let current = cached.embed(&text).await.ok();
                        prop_assert_eq!(first.clone(), current,
                            "Cache hit should return identical value");
                    }

                    // Verify we got cache hits
                    let stats = cached.stats();
                    prop_assert_eq!(stats.hits as usize, num_accesses - 1,
                        "Should have {} cache hits, got {}", num_accesses - 1, stats.hits);
                    Ok(())
                });
            }

            /// Different texts produce different cache entries
            #[test]
            fn different_texts_different_entries(
                texts in prop::collection::hash_set("[a-z]{3,10}", 2..20)
            ) {
                tokio_test::block_on(async {
                    let provider = MockEmbeddingProvider::new(32);
                    let cached = CachedEmbeddingProvider::with_defaults(provider);

                    let text_vec: Vec<_> = texts.iter().collect();
                    for text in &text_vec {
                        let _ = cached.embed(text).await;
                    }

                    let stats = cached.stats();
                    prop_assert!(stats.entries <= text_vec.len(),
                        "Cache entries {} should not exceed unique texts {}",
                        stats.entries, text_vec.len());
                    Ok(())
                });
            }

            /// LRU evicts least recently used entries
            #[test]
            fn lru_evicts_least_recently_used(
                max_entries in 3usize..10
            ) {
                tokio_test::block_on(async {
                    let provider = MockEmbeddingProvider::new(32);
                    let config = EmbeddingCacheConfig::new(max_entries);
                    let cached = CachedEmbeddingProvider::new(provider, config);

                    // Fill cache
                    for i in 0..max_entries {
                        let _ = cached.embed(&format!("text_{}", i)).await;
                    }

                    // Access the first entry to make it recently used
                    let _ = cached.embed("text_0").await;

                    // Add one more entry, should evict something other than text_0
                    let _ = cached.embed("new_text").await;

                    let stats = cached.stats();
                    prop_assert!(stats.evictions >= 1,
                        "Should have at least 1 eviction, got {}", stats.evictions);
                    prop_assert_eq!(stats.entries, max_entries,
                        "Cache should maintain max_entries size");

                    // text_0 should still be cached (it was recently accessed)
                    let hits_before = cached.stats().hits;
                    let _ = cached.embed("text_0").await;
                    let hits_after = cached.stats().hits;
                    prop_assert!(hits_after > hits_before,
                        "text_0 should still be cached (recently accessed)");
                    Ok(())
                });
            }

            /// Hit rate is calculated correctly
            #[test]
            fn hit_rate_calculation_correct(
                num_unique in 1usize..20,
                repeats in 1usize..5
            ) {
                tokio_test::block_on(async {
                    let provider = MockEmbeddingProvider::new(32);
                    let cached = CachedEmbeddingProvider::with_defaults(provider);

                    // Insert unique texts
                    for i in 0..num_unique {
                        let _ = cached.embed(&format!("text_{}", i)).await;
                    }

                    // Repeat accesses
                    for _ in 0..repeats {
                        for i in 0..num_unique {
                            let _ = cached.embed(&format!("text_{}", i)).await;
                        }
                    }

                    let stats = cached.stats();
                    let expected_hits = num_unique * repeats;
                    let expected_misses = num_unique;

                    prop_assert_eq!(stats.hits as usize, expected_hits,
                        "Expected {} hits, got {}", expected_hits, stats.hits);
                    prop_assert_eq!(stats.misses as usize, expected_misses,
                        "Expected {} misses, got {}", expected_misses, stats.misses);

                    #[allow(clippy::cast_precision_loss)]
                    let expected_rate = (expected_hits as f64 / (expected_hits + expected_misses) as f64) * 100.0;
                    prop_assert!((stats.hit_rate() - expected_rate).abs() < 0.1,
                        "Hit rate should be ~{:.2}%, got {:.2}%", expected_rate, stats.hit_rate());
                    Ok(())
                });
            }

            /// Batch embed maintains cache consistency
            #[test]
            fn batch_embed_cache_consistency(
                texts in prop::collection::vec("[a-z]{3,10}", 1..15)
            ) {
                tokio_test::block_on(async {
                    let provider = MockEmbeddingProvider::new(32);
                    let cached = CachedEmbeddingProvider::with_defaults(provider);

                    // Embed individually first
                    let mut individual_results = Vec::new();
                    for text in &texts {
                        individual_results.push(cached.embed(text).await.ok());
                    }

                    // Clear cache
                    cached.clear_cache();

                    // Embed as batch
                    let text_refs: Vec<&str> = texts.iter().map(|s| s.as_str()).collect();
                    let batch_results = cached.embed_batch(&text_refs).await.ok();

                    // Results should be consistent
                    if let Some(batch) = batch_results {
                        prop_assert_eq!(batch.len(), texts.len(),
                            "Batch should return same number of embeddings");

                        // Verify cached
                        for (i, text) in texts.iter().enumerate() {
                            let cached_result = cached.embed(text).await.ok();
                            prop_assert_eq!(Some(batch[i].clone()), cached_result,
                                "Cached embedding should match batch result at index {}", i);
                        }
                    }
                    Ok(())
                });
            }
        }
    }

    #[tokio::test]
    async fn test_cache_dimension_passthrough() {
        let provider = MockEmbeddingProvider::new(256);
        let cached = CachedEmbeddingProvider::with_defaults(provider);

        assert_eq!(cached.dimension(), 256);
    }

    #[tokio::test]
    async fn test_cache_model_id_passthrough() {
        let provider = MockEmbeddingProvider::new(64).with_model_id("test-model");
        let cached = CachedEmbeddingProvider::with_defaults(provider);

        assert_eq!(cached.model_id(), "test-model");
    }

    #[tokio::test]
    async fn test_cache_empty_batch() {
        let provider = MockEmbeddingProvider::new(32);
        let cached = CachedEmbeddingProvider::with_defaults(provider);

        let result = cached.embed_batch(&[]).await;
        assert!(matches!(result, Err(EmbeddingError::EmptyInput)));
    }
}