cognis-rag 0.3.2

RAG primitives for Cognis: embeddings, vector stores (in-memory, FAISS, Chroma, Qdrant, Pinecone, Weaviate), retrievers, text splitters, document loaders, and incremental indexing pipelines.
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
//! Content-deduplicating [`VectorStore`] decorator.
//!
//! [`DedupVectorStore`] wraps any [`VectorStore`] and silently drops
//! documents whose normalised content fingerprint has already been seen.
//! Use it whenever the same fact may arrive multiple times (agent loops,
//! repeated indexing runs, multi-source pipelines) and you want the index
//! to stay clean without extra bookkeeping in application code.
//!
//! The default fingerprint normalises text (lowercase + collapsed
//! whitespace) then applies FNV-1a 128-bit — the same algorithm used by
//! [`crate::record_manager::fingerprint`]. Supply a custom function via
//! [`DedupVectorStore::with_fingerprint`] when your dedup key is not
//! derived from raw text (e.g. a document ID or a composite hash).

use std::collections::HashMap;
use std::collections::HashSet;

use async_trait::async_trait;

use cognis_core::Result;

use super::{Filter, SearchResult, VectorStore};

// ---------------------------------------------------------------------------
// Default fingerprint
// ---------------------------------------------------------------------------

/// Normalise `text` (lowercase + collapse whitespace) then fingerprint with
/// FNV-1a 128-bit. Two pieces of text that differ only in case or spacing
/// produce the same fingerprint and are treated as duplicates.
///
/// The algorithm is the same as [`crate::record_manager::fingerprint`] but
/// applied to the normalised form so that case / whitespace variants
/// collapse. Exposed as a public function so callers can pre-compute
/// fingerprints when seeding an existing store (see
/// [`DedupVectorStore::with_seen`]).
pub fn normalized_fingerprint(text: &str) -> String {
    // Normalise: lowercase + single-space whitespace runs.
    let normalised = text
        .split_whitespace()
        .map(|w| w.to_lowercase())
        .collect::<Vec<_>>()
        .join(" ");

    // FNV-1a 128-bit (published constants, public domain).
    const OFFSET: u128 = 0x6c62272e07bb014262b821756295c58d;
    const PRIME: u128 = 0x0000000001000000000000000000013b;
    let mut h: u128 = OFFSET;
    for b in normalised.as_bytes() {
        h ^= u128::from(*b);
        h = h.wrapping_mul(PRIME);
    }
    format!("{h:032x}")
}

// ---------------------------------------------------------------------------
// DedupVectorStore
// ---------------------------------------------------------------------------

/// A [`VectorStore`] decorator that silently skips documents whose
/// normalised content fingerprint is already in the seen-set.
///
/// # Type parameters
///
/// * `S` — the inner [`VectorStore`] implementation.
/// * `F` — the fingerprint function `fn(&str) -> String`. Defaults to
///   [`normalized_fingerprint`] (lowercase + whitespace-collapse + FNV-1a).
///   Provide a custom function via [`DedupVectorStore::with_fingerprint`]
///   when you need a different dedup key (e.g. document ID, composite hash).
///
/// # Persistence across restarts
///
/// The seen-set lives in memory and is cleared on process restart. To
/// survive restarts, query your storage for existing content fingerprints
/// on startup and pass them to [`DedupVectorStore::with_seen`]. The
/// [`normalized_fingerprint`] function is public so you can pre-compute
/// hashes from stored records.
///
/// ```rust,ignore
/// let hashes = db.query_all("SELECT content_hash FROM facts").await?;
/// let store = DedupVectorStore::with_seen(inner, hashes);
/// ```
pub struct DedupVectorStore<S, F = fn(&str) -> String>
where
    S: VectorStore,
    F: Fn(&str) -> String + Send + Sync,
{
    inner: S,
    fingerprint_fn: F,
    seen: HashSet<String>,
}

// --- constructors for the default fingerprint ---

impl<S: VectorStore> DedupVectorStore<S, fn(&str) -> String> {
    /// Wrap `inner` with an empty seen-set and the default
    /// [`normalized_fingerprint`] function.
    pub fn new(inner: S) -> Self {
        Self {
            inner,
            fingerprint_fn: normalized_fingerprint,
            seen: HashSet::new(),
        }
    }

    /// Wrap `inner` and pre-populate the seen-set from `seen` fingerprints.
    ///
    /// Use this when re-starting a process and you want to restore the
    /// dedup state from previously persisted fingerprints.
    pub fn with_seen(inner: S, seen: impl IntoIterator<Item = String>) -> Self {
        Self {
            inner,
            fingerprint_fn: normalized_fingerprint,
            seen: seen.into_iter().collect(),
        }
    }
}

// --- constructors with a custom fingerprint function ---

impl<S, F> DedupVectorStore<S, F>
where
    S: VectorStore,
    F: Fn(&str) -> String + Send + Sync,
{
    /// Wrap `inner` with a custom fingerprint function and an empty seen-set.
    ///
    /// The function receives the raw document text and returns a string key.
    /// Documents whose key is already in the seen-set are skipped.
    pub fn with_fingerprint(inner: S, f: F) -> Self {
        Self {
            inner,
            fingerprint_fn: f,
            seen: HashSet::new(),
        }
    }

    /// Wrap `inner` with a custom fingerprint function and a pre-populated
    /// seen-set.
    pub fn with_fingerprint_and_seen(
        inner: S,
        f: F,
        seen: impl IntoIterator<Item = String>,
    ) -> Self {
        Self {
            inner,
            fingerprint_fn: f,
            seen: seen.into_iter().collect(),
        }
    }

    // --- inspection ---

    /// Whether `text` is already recorded in the seen-set (using the
    /// configured fingerprint function).
    pub fn contains(&self, text: &str) -> bool {
        self.seen.contains(&(self.fingerprint_fn)(text))
    }

    /// Read-only access to the inner store.
    pub fn inner(&self) -> &S {
        &self.inner
    }

    /// Mutable access to the inner store — e.g. to call store-specific
    /// methods not on the [`VectorStore`] trait.
    pub fn inner_mut(&mut self) -> &mut S {
        &mut self.inner
    }

    /// Iterate over all fingerprints currently held in the seen-set.
    /// Useful when persisting state to storage before shutdown.
    pub fn seen_fingerprints(&self) -> impl Iterator<Item = &str> {
        self.seen.iter().map(|s| s.as_str())
    }

    /// Number of unique fingerprints recorded (≥ documents in the inner
    /// store when duplicates were skipped).
    pub fn seen_count(&self) -> usize {
        self.seen.len()
    }
}

// ---------------------------------------------------------------------------
// VectorStore impl
// ---------------------------------------------------------------------------

#[async_trait]
impl<S, F> VectorStore for DedupVectorStore<S, F>
where
    S: VectorStore + Send + Sync,
    F: Fn(&str) -> String + Send + Sync,
{
    /// Filter out texts whose fingerprint is already seen, then delegate
    /// the remainder to the inner store.
    ///
    /// Skipped documents are represented as `"dedup:skipped:{fingerprint}"`
    /// in the returned ID list so that callers whose code expects
    /// `ids.len() == texts.len()` still holds.
    async fn add_texts(
        &mut self,
        texts: Vec<String>,
        metadata: Option<Vec<HashMap<String, serde_json::Value>>>,
    ) -> Result<Vec<String>> {
        if texts.is_empty() {
            return Ok(Vec::new());
        }

        let mut pass_texts: Vec<String> = Vec::new();
        let mut pass_meta: Vec<HashMap<String, serde_json::Value>> = Vec::new();
        // Tracks final position: None = sent to inner, Some(id) = skipped.
        let mut slots: Vec<Option<String>> = Vec::with_capacity(texts.len());

        for (i, text) in texts.iter().enumerate() {
            let fp = (self.fingerprint_fn)(text);
            if self.seen.contains(&fp) {
                slots.push(Some(format!("dedup:skipped:{fp}")));
            } else {
                self.seen.insert(fp);
                pass_texts.push(text.clone());
                if let Some(m) = &metadata {
                    pass_meta.push(m[i].clone());
                }
                slots.push(None);
            }
        }

        let real_meta = if metadata.is_some() && !pass_meta.is_empty() {
            Some(pass_meta)
        } else {
            None
        };

        let mut inner_ids = if !pass_texts.is_empty() {
            self.inner.add_texts(pass_texts, real_meta).await?
        } else {
            Vec::new()
        };

        // Reconstruct output in original order.
        let mut inner_iter = inner_ids.drain(..);
        let ids = slots
            .into_iter()
            .map(|slot| match slot {
                Some(skipped_id) => skipped_id,
                None => inner_iter.next().unwrap_or_default(),
            })
            .collect();
        Ok(ids)
    }

    /// Filter out pre-embedded vectors whose text fingerprint is already
    /// seen, then delegate the remainder to the inner store.
    async fn add_vectors(
        &mut self,
        vectors: Vec<Vec<f32>>,
        texts: Vec<String>,
        metadata: Option<Vec<HashMap<String, serde_json::Value>>>,
    ) -> Result<Vec<String>> {
        if texts.is_empty() {
            return Ok(Vec::new());
        }

        let mut pass_vecs: Vec<Vec<f32>> = Vec::new();
        let mut pass_texts: Vec<String> = Vec::new();
        let mut pass_meta: Vec<HashMap<String, serde_json::Value>> = Vec::new();
        let mut slots: Vec<Option<String>> = Vec::with_capacity(texts.len());

        for (i, (text, vec)) in texts.iter().zip(vectors.iter()).enumerate() {
            let fp = (self.fingerprint_fn)(text);
            if self.seen.contains(&fp) {
                slots.push(Some(format!("dedup:skipped:{fp}")));
            } else {
                self.seen.insert(fp);
                pass_texts.push(text.clone());
                pass_vecs.push(vec.clone());
                if let Some(m) = &metadata {
                    pass_meta.push(m[i].clone());
                }
                slots.push(None);
            }
        }

        let real_meta = if metadata.is_some() && !pass_meta.is_empty() {
            Some(pass_meta)
        } else {
            None
        };

        let mut inner_ids = if !pass_texts.is_empty() {
            self.inner
                .add_vectors(pass_vecs, pass_texts, real_meta)
                .await?
        } else {
            Vec::new()
        };

        let mut inner_iter = inner_ids.drain(..);
        let ids = slots
            .into_iter()
            .map(|slot| match slot {
                Some(skipped_id) => skipped_id,
                None => inner_iter.next().unwrap_or_default(),
            })
            .collect();
        Ok(ids)
    }

    async fn similarity_search(&self, query: &str, k: usize) -> Result<Vec<SearchResult>> {
        self.inner.similarity_search(query, k).await
    }

    async fn similarity_search_by_vector(
        &self,
        query_vector: Vec<f32>,
        k: usize,
    ) -> Result<Vec<SearchResult>> {
        self.inner
            .similarity_search_by_vector(query_vector, k)
            .await
    }

    async fn similarity_search_with_filter(
        &self,
        query: &str,
        k: usize,
        filter: &Filter,
    ) -> Result<Vec<SearchResult>> {
        self.inner
            .similarity_search_with_filter(query, k, filter)
            .await
    }

    async fn delete(&mut self, ids: Vec<String>) -> Result<()> {
        self.inner.delete(ids).await
    }

    fn len(&self) -> usize {
        self.inner.len()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::embeddings::FakeEmbeddings;
    use crate::vectorstore::InMemoryVectorStore;
    use std::sync::Arc;

    fn inner() -> InMemoryVectorStore {
        InMemoryVectorStore::new(Arc::new(FakeEmbeddings::new(8)))
    }

    // --- add_texts ---

    #[tokio::test]
    async fn skips_duplicate_on_second_add() {
        let mut store = DedupVectorStore::new(inner());
        store
            .add_texts(vec!["the workspace uses Go".into()], None)
            .await
            .unwrap();
        store
            .add_texts(vec!["the workspace uses Go".into()], None)
            .await
            .unwrap();
        assert_eq!(store.len(), 1);
    }

    #[tokio::test]
    async fn case_and_whitespace_normalisation_deduplicates() {
        let mut store = DedupVectorStore::new(inner());
        store
            .add_texts(vec!["The workspace uses Go.".into()], None)
            .await
            .unwrap();
        store
            .add_texts(vec!["  THE  WORKSPACE   USES  GO.  ".into()], None)
            .await
            .unwrap();
        assert_eq!(store.len(), 1);
    }

    #[tokio::test]
    async fn distinct_content_both_stored() {
        let mut store = DedupVectorStore::new(inner());
        store.add_texts(vec!["Fact A.".into()], None).await.unwrap();
        store.add_texts(vec!["Fact B.".into()], None).await.unwrap();
        assert_eq!(store.len(), 2);
    }

    #[tokio::test]
    async fn batch_add_with_mixed_duplicates() {
        let mut store = DedupVectorStore::new(inner());
        let ids1 = store
            .add_texts(vec!["unique one".into(), "unique two".into()], None)
            .await
            .unwrap();
        assert_eq!(ids1.len(), 2);
        assert!(!ids1[0].starts_with("dedup:skipped:"));
        assert!(!ids1[1].starts_with("dedup:skipped:"));

        let ids2 = store
            .add_texts(
                vec![
                    "unique one".into(),
                    "unique three".into(),
                    "unique two".into(),
                ],
                None,
            )
            .await
            .unwrap();
        assert_eq!(ids2.len(), 3);
        // First and third are duplicates.
        assert!(ids2[0].starts_with("dedup:skipped:"));
        assert!(
            !ids2[1].starts_with("dedup:skipped:"),
            "unique three should pass through"
        );
        assert!(ids2[2].starts_with("dedup:skipped:"));
        assert_eq!(store.len(), 3);
    }

    #[tokio::test]
    async fn with_seen_skips_pre_populated_fingerprints() {
        let fp = normalized_fingerprint("already known fact");
        let mut store = DedupVectorStore::with_seen(inner(), [fp]);
        store
            .add_texts(vec!["already known fact".into()], None)
            .await
            .unwrap();
        assert_eq!(store.len(), 0);
    }

    #[tokio::test]
    async fn read_operations_pass_through() {
        let mut store = DedupVectorStore::new(inner());
        store
            .add_texts(vec!["searchable fact".into()], None)
            .await
            .unwrap();
        let results = store.similarity_search("fact", 5).await.unwrap();
        assert!(!results.is_empty());
    }

    #[tokio::test]
    async fn delete_passes_through() {
        let mut store = DedupVectorStore::new(inner());
        let ids = store
            .add_texts(vec!["deletable".into()], None)
            .await
            .unwrap();
        assert_eq!(store.len(), 1);
        store.delete(ids).await.unwrap();
        assert_eq!(store.len(), 0);
    }

    #[tokio::test]
    async fn seen_count_tracks_unique_fingerprints() {
        let mut store = DedupVectorStore::new(inner());
        store
            .add_texts(vec!["a".into(), "b".into()], None)
            .await
            .unwrap();
        store.add_texts(vec!["a".into()], None).await.unwrap(); // duplicate
        assert_eq!(store.seen_count(), 2);
    }

    #[tokio::test]
    async fn contains_reflects_seen_set() {
        let mut store = DedupVectorStore::new(inner());
        assert!(!store.contains("new fact"));
        store
            .add_texts(vec!["new fact".into()], None)
            .await
            .unwrap();
        assert!(store.contains("new fact"));
        // Case variant also matches.
        assert!(store.contains("NEW FACT"));
    }

    // --- custom fingerprint ---

    #[tokio::test]
    async fn custom_fingerprint_uses_provided_function() {
        // Fingerprint only on the first word — any two texts with the same
        // first word are considered duplicates.
        let mut store = DedupVectorStore::with_fingerprint(inner(), |text: &str| {
            text.split_whitespace()
                .next()
                .unwrap_or("")
                .to_lowercase()
                .to_string()
        });
        store
            .add_texts(vec!["rust is great".into()], None)
            .await
            .unwrap();
        store
            .add_texts(vec!["rust is also fast".into()], None)
            .await
            .unwrap();
        // Both start with "rust" → second is a duplicate.
        assert_eq!(store.len(), 1);
    }

    // --- add_vectors ---

    #[tokio::test]
    async fn add_vectors_deduplicates() {
        let mut store = DedupVectorStore::new(inner());
        let vec = vec![0.1_f32; 8];
        store
            .add_vectors(vec![vec.clone()], vec!["vec fact".into()], None)
            .await
            .unwrap();
        store
            .add_vectors(vec![vec.clone()], vec!["vec fact".into()], None)
            .await
            .unwrap();
        assert_eq!(store.len(), 1);
    }

    // --- normalized_fingerprint properties ---

    #[test]
    fn fingerprint_is_deterministic() {
        assert_eq!(
            normalized_fingerprint("hello world"),
            normalized_fingerprint("hello world")
        );
    }

    #[test]
    fn fingerprint_is_case_insensitive() {
        assert_eq!(
            normalized_fingerprint("Hello World"),
            normalized_fingerprint("hello world")
        );
    }

    #[test]
    fn fingerprint_collapses_whitespace() {
        assert_eq!(
            normalized_fingerprint("hello   world"),
            normalized_fingerprint("hello world")
        );
    }

    #[test]
    fn fingerprint_distinguishes_different_content() {
        assert_ne!(
            normalized_fingerprint("hello world"),
            normalized_fingerprint("goodbye world")
        );
    }
}