fornix 0.4.0

Knowledge storage, retrieval, and graph infrastructure for cognitive systems
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
//! In-process memory vector adapter.
//!
//! Useful for testing and development. All data is lost on drop.
//! Thread-safe via `tokio::sync::RwLock` (reads are far more common
//! than writes in typical search workloads).

use std::collections::HashMap;

use async_trait::async_trait;
use tokio::sync::RwLock;

use crate::common::metadata::Metadata;
use crate::common::namespace::Namespace;
use crate::common::pagination::{Page, PageParams};
use crate::store::config::AdapterConfig;
use crate::store::health::{HealthReport, HealthStatus};
use crate::vector::{
    adapter::{ListOptions, SearchOptions, VectorAdapter},
    analysis::cosine_similarity,
    config::VectorConfig,
    error::{Error, Result},
    result::{VectorRecord, VectorResult},
};

/// A stored entry inside the memory adapter.
#[derive(Clone)]
struct Entry {
    vector: Vec<f32>,
    metadata: Metadata,
}

/// Inner state protected by the RwLock.
struct Inner {
    /// namespace → (id → entry)
    store: HashMap<String, HashMap<String, Entry>>,
}

impl Inner {
    fn new() -> Self {
        Self { store: HashMap::new() }
    }

    fn ns_key(ns: &str) -> String {
        ns.to_string()
    }

    fn namespace_entries(&self, ns: &str) -> Option<&HashMap<String, Entry>> {
        self.store.get(&Self::ns_key(ns))
    }

    fn namespace_entries_mut(&mut self, ns: &str) -> &mut HashMap<String, Entry> {
        self.store.entry(Self::ns_key(ns)).or_default()
    }
}

/// In-memory vector adapter with cosine similarity search.
pub struct MemoryVectorAdapter {
    config: VectorConfig,
    connected: bool,
    inner: RwLock<Inner>,
}

impl MemoryVectorAdapter {
    /// Create a new (disconnected) adapter.
    pub fn new(config: VectorConfig) -> Self {
        Self {
            config,
            connected: false,
            inner: RwLock::new(Inner::new()),
        }
    }

    /// Create and immediately connect an adapter.
    pub async fn connect(config: VectorConfig) -> Result<Self> {
        config
            .validate()
            .map_err(|e| Error::config(e.to_string()))?;
        Ok(Self {
            config,
            connected: true,
            inner: RwLock::new(Inner::new()),
        })
    }

    fn resolve_ns<'a>(&'a self, ns: Option<&'a Namespace>) -> &'a str {
        ns.and_then(|n| n.as_deref())
            .or_else(|| self.config.default_namespace.as_deref())
            .unwrap_or("default")
    }
}

#[async_trait]
impl VectorAdapter for MemoryVectorAdapter {
    fn name(&self) -> &'static str {
        "memory"
    }

    fn is_connected(&self) -> bool {
        self.connected
    }

    fn config(&self) -> &VectorConfig {
        &self.config
    }

    async fn upsert(
        &self,
        id: &str,
        vector: Vec<f32>,
        metadata: Option<Metadata>,
        namespace: Option<&Namespace>,
    ) -> Result<()> {
        if !self.connected {
            return Err(Error::NotConnected);
        }
        self.config
            .check_dimension(vector.len())
            .map_err(|e| Error::dimension_mismatch(
                match e {
                    crate::store::error::Error::DimensionMismatch { expected, .. } => expected,
                    _ => 0,
                },
                vector.len(),
            ))?;

        let ns = self.resolve_ns(namespace).to_string();
        let mut inner = self.inner.write().await;
        inner.namespace_entries_mut(&ns).insert(
            id.to_string(),
            Entry {
                vector,
                metadata: metadata.unwrap_or_default(),
            },
        );
        Ok(())
    }

    async fn nearest_neighbors(
        &self,
        query: &[f32],
        namespace: Option<&Namespace>,
        options: SearchOptions,
    ) -> Result<Vec<VectorResult>> {
        if !self.connected {
            return Err(Error::NotConnected);
        }
        self.config
            .check_dimension(query.len())
            .map_err(|e| match e {
                crate::store::error::Error::DimensionMismatch { expected, actual } => {
                    Error::dimension_mismatch(expected, actual)
                }
                _ => Error::operation(e.to_string()),
            })?;

        let ns = self.resolve_ns(namespace).to_string();
        let inner = self.inner.read().await;
        let filter = options.filter.unwrap_or_default();

        let mut results: Vec<VectorResult> = inner
            .namespace_entries(&ns)
            .into_iter()
            .flat_map(|entries| entries.iter())
            .filter(|(_, entry)| filter.matches(&entry.metadata))
            .filter_map(|(id, entry)| {
                let sim = cosine_similarity(query, &entry.vector).ok()?;
                if let Some(min) = options.min_similarity
                    && sim < min {
                        return None;
                    }
                Some(VectorResult::new(
                    id.clone(),
                    sim,
                    entry.metadata.clone(),
                    if options.include_vectors { Some(entry.vector.clone()) } else { None },
                ))
            })
            .collect();

        results.sort_by(|a, b| {
            b.score()
                .partial_cmp(&a.score())
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        results.truncate(options.limit);
        Ok(results)
    }

    async fn list(
        &self,
        namespace: Option<&Namespace>,
        page: PageParams,
        options: ListOptions,
    ) -> Result<Page<VectorRecord>> {
        if !self.connected {
            return Err(Error::NotConnected);
        }
        let ns = self.resolve_ns(namespace).to_string();
        let inner = self.inner.read().await;
        let filter = options.filter.unwrap_or_default();

        let mut records: Vec<(String, VectorRecord)> = inner
            .namespace_entries(&ns)
            .into_iter()
            .flat_map(|entries| entries.iter())
            .filter(|(_, entry)| filter.matches(&entry.metadata))
            .map(|(id, entry)| {
                (
                    id.clone(),
                    VectorRecord {
                        id: id.clone(),
                        metadata: entry.metadata.clone(),
                        vector: if options.include_vectors {
                            Some(entry.vector.clone())
                        } else {
                            None
                        },
                    },
                )
            })
            .collect();

        // Deterministic ordering for pagination
        records.sort_by(|(a, _), (b, _)| a.cmp(b));

        // Cursor-based pagination: cursor is the last id seen
        let start = if let Some(cursor) = &page.cursor {
            records
                .iter()
                .position(|(id, _)| id == cursor)
                .map(|i| i + 1)
                .unwrap_or(0)
        } else {
            0
        };

        let total = records.len();
        let slice: Vec<VectorRecord> = records
            .into_iter()
            .skip(start)
            .take(page.limit)
            .map(|(_, rec)| rec)
            .collect();

        let next_cursor = if start + slice.len() < total {
            slice.last().map(|rec| rec.id.clone())
        } else {
            None
        };

        Ok(match next_cursor {
            Some(cursor) => Page::with_cursor(slice, cursor, Some(total)),
            None => Page::last(slice, Some(total)),
        })
    }

    async fn delete(&self, id: &str, namespace: Option<&Namespace>) -> Result<bool> {
        if !self.connected {
            return Err(Error::NotConnected);
        }
        let ns = self.resolve_ns(namespace).to_string();
        let mut inner = self.inner.write().await;
        Ok(inner
            .namespace_entries_mut(&ns)
            .remove(id)
            .is_some())
    }

    async fn delete_namespace(&self, namespace: &Namespace) -> Result<usize> {
        if !self.connected {
            return Err(Error::NotConnected);
        }
        let ns = namespace.as_deref().unwrap_or("default");
        let mut inner = self.inner.write().await;
        Ok(inner.store.remove(ns).map(|m| m.len()).unwrap_or(0))
    }

    async fn count(&self, namespace: Option<&Namespace>) -> Result<usize> {
        if !self.connected {
            return Err(Error::NotConnected);
        }
        let inner = self.inner.read().await;
        Ok(match namespace {
            Some(ns) => inner
                .namespace_entries(ns.as_deref().unwrap_or("default"))
                .map(|m| m.len())
                .unwrap_or(0),
            None => inner.store.values().map(|m| m.len()).sum(),
        })
    }

    async fn healthcheck(&self) -> HealthReport {
        let status = if self.connected {
            HealthStatus::Healthy
        } else {
            HealthStatus::Unhealthy { reason: "not connected".to_string() }
        };
        HealthReport::begin("memory-vector").finish(status)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::metadata::Metadata;
    use crate::common::pagination::PageParams;
    use crate::vector::filter::MetadataFilter;
    use serde_json::json;

    async fn adapter_dim(dim: usize) -> MemoryVectorAdapter {
        MemoryVectorAdapter::connect(VectorConfig::with_dimension(dim)).await.unwrap()
    }

    async fn adapter() -> MemoryVectorAdapter {
        MemoryVectorAdapter::connect(VectorConfig::default()).await.unwrap()
    }

    fn ns(s: &str) -> Namespace { Namespace::named(s) }

    fn vec2(a: f32, b: f32) -> Vec<f32> { vec![a, b] }

    fn opts() -> SearchOptions { SearchOptions::default() }

    // --- lifecycle ---

    #[tokio::test]
    async fn new_is_disconnected() {
        let a = MemoryVectorAdapter::new(VectorConfig::default());
        assert!(!a.is_connected());
    }

    #[tokio::test]
    async fn connect_produces_connected_adapter() {
        let a = adapter().await;
        assert!(a.is_connected());
    }

    #[tokio::test]
    async fn operations_fail_when_disconnected() {
        let a = MemoryVectorAdapter::new(VectorConfig::default());
        let err = a.upsert("id", vec![1.0], None, None).await.unwrap_err();
        assert!(matches!(err, Error::NotConnected));
    }

    #[tokio::test]
    async fn name_is_memory() {
        assert_eq!(adapter().await.name(), "memory");
    }

    // --- upsert / count ---

    #[tokio::test]
    async fn upsert_increments_count() {
        let a = adapter().await;
        a.upsert("a", vec2(1.0, 0.0), None, None).await.unwrap();
        assert_eq!(a.count(None).await.unwrap(), 1);
    }

    #[tokio::test]
    async fn upsert_same_id_replaces() {
        let a = adapter().await;
        a.upsert("a", vec2(1.0, 0.0), None, None).await.unwrap();
        a.upsert("a", vec2(0.0, 1.0), None, None).await.unwrap();
        assert_eq!(a.count(None).await.unwrap(), 1);
    }

    #[tokio::test]
    async fn upsert_rejects_wrong_dimension() {
        let a = adapter_dim(2).await;
        let err = a.upsert("a", vec![1.0, 2.0, 3.0], None, None).await.unwrap_err();
        assert!(matches!(err, Error::DimensionMismatch { .. }));
    }

    // --- nearest_neighbors ---

    #[tokio::test]
    async fn nearest_neighbors_returns_closest_first() {
        let a = adapter().await;
        a.upsert("far",  vec2(0.0, 1.0), None, None).await.unwrap();
        a.upsert("near", vec2(1.0, 0.1), None, None).await.unwrap();

        let results = a
            .nearest_neighbors(&vec2(1.0, 0.0), None, opts())
            .await
            .unwrap();

        assert!(!results.is_empty());
        assert_eq!(results[0].id, "near");
    }

    #[tokio::test]
    async fn nearest_neighbors_respects_limit() {
        let a = adapter().await;
        for i in 0..5 {
            a.upsert(&i.to_string(), vec2(i as f32, 0.0), None, None)
                .await
                .unwrap();
        }
        let results = a
            .nearest_neighbors(&vec2(1.0, 0.0), None, opts().with_limit(2))
            .await
            .unwrap();
        assert_eq!(results.len(), 2);
    }

    #[tokio::test]
    async fn nearest_neighbors_respects_min_similarity() {
        let a = adapter().await;
        a.upsert("close",    vec2(1.0, 0.0),  None, None).await.unwrap();
        a.upsert("far",      vec2(0.0, 1.0),  None, None).await.unwrap();

        let results = a
            .nearest_neighbors(
                &vec2(1.0, 0.0),
                None,
                opts().with_min_similarity(0.9),
            )
            .await
            .unwrap();

        assert!(results.iter().all(|r| r.score() >= 0.9));
    }

    #[tokio::test]
    async fn nearest_neighbors_respects_metadata_filter() {
        let a = adapter().await;
        let mut m = Metadata::new();
        m.insert("type".to_string(), json!("doc"));
        a.upsert("doc",   vec2(1.0, 0.0), Some(m), None).await.unwrap();
        a.upsert("other", vec2(1.0, 0.0), None,    None).await.unwrap();

        let filter = MetadataFilter::new().with("type", json!("doc"));
        let results = a
            .nearest_neighbors(&vec2(1.0, 0.0), None, opts().with_filter(filter))
            .await
            .unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].id, "doc");
    }

    #[tokio::test]
    async fn nearest_neighbors_include_vectors() {
        let a = adapter().await;
        a.upsert("a", vec2(1.0, 0.0), None, None).await.unwrap();
        let results = a
            .nearest_neighbors(&vec2(1.0, 0.0), None, opts().include_vectors())
            .await
            .unwrap();
        assert!(results[0].vector.is_some());
    }

    #[tokio::test]
    async fn nearest_neighbors_without_vectors_has_none() {
        let a = adapter().await;
        a.upsert("a", vec2(1.0, 0.0), None, None).await.unwrap();
        let results = a.nearest_neighbors(&vec2(1.0, 0.0), None, opts()).await.unwrap();
        assert!(results[0].vector.is_none());
    }

    // --- namespacing ---

    #[tokio::test]
    async fn namespaces_are_isolated() {
        let a = adapter().await;
        a.upsert("id", vec2(1.0, 0.0), None, Some(&ns("ns1"))).await.unwrap();
        a.upsert("id", vec2(0.0, 1.0), None, Some(&ns("ns2"))).await.unwrap();

        assert_eq!(a.count(Some(&ns("ns1"))).await.unwrap(), 1);
        assert_eq!(a.count(Some(&ns("ns2"))).await.unwrap(), 1);
        assert_eq!(a.count(None).await.unwrap(), 2);
    }

    // --- delete ---

    #[tokio::test]
    async fn delete_existing_returns_true() {
        let a = adapter().await;
        a.upsert("a", vec2(1.0, 0.0), None, None).await.unwrap();
        assert!(a.delete("a", None).await.unwrap());
        assert_eq!(a.count(None).await.unwrap(), 0);
    }

    #[tokio::test]
    async fn delete_missing_returns_false() {
        let a = adapter().await;
        assert!(!a.delete("nope", None).await.unwrap());
    }

    // --- delete_namespace ---

    #[tokio::test]
    async fn delete_namespace_removes_all_in_namespace() {
        let a = adapter().await;
        a.upsert("a", vec2(1.0, 0.0), None, Some(&ns("x"))).await.unwrap();
        a.upsert("b", vec2(0.0, 1.0), None, Some(&ns("x"))).await.unwrap();
        a.upsert("c", vec2(1.0, 0.0), None, Some(&ns("y"))).await.unwrap();

        let removed = a.delete_namespace(&ns("x")).await.unwrap();
        assert_eq!(removed, 2);
        assert_eq!(a.count(Some(&ns("x"))).await.unwrap(), 0);
        assert_eq!(a.count(Some(&ns("y"))).await.unwrap(), 1);
    }

    // --- list / pagination ---

    #[tokio::test]
    async fn list_returns_all_records() {
        let a = adapter().await;
        for i in 0..3 {
            a.upsert(&i.to_string(), vec2(i as f32, 0.0), None, None)
                .await
                .unwrap();
        }
        let page = a.list(None, PageParams::first(100), ListOptions::default()).await.unwrap();
        assert_eq!(page.items.len(), 3);
        assert_eq!(page.total, Some(3));
    }

    #[tokio::test]
    async fn list_pagination_with_cursor() {
        let a = adapter().await;
        for i in 0..5 {
            a.upsert(&format!("rec-{:02}", i), vec2(i as f32, 0.0), None, None)
                .await
                .unwrap();
        }
        let first = a.list(None, PageParams::first(2), ListOptions::default()).await.unwrap();
        assert_eq!(first.items.len(), 2);
        assert!(first.has_next());

        let cursor = first.next_cursor.unwrap();
        let second = a.list(None, PageParams::after(cursor, 2), ListOptions::default()).await.unwrap();
        assert_eq!(second.items.len(), 2);
    }

    #[tokio::test]
    async fn list_respects_metadata_filter() {
        let a = adapter().await;
        let mut m = Metadata::new();
        m.insert("keep".to_string(), json!(true));
        a.upsert("keep",   vec2(1.0, 0.0), Some(m), None).await.unwrap();
        a.upsert("drop",   vec2(1.0, 0.0), None,    None).await.unwrap();

        let filter = MetadataFilter::new().with("keep", json!(true));
        let page = a
            .list(None, PageParams::first(100), ListOptions { filter: Some(filter), include_vectors: false })
            .await
            .unwrap();
        assert_eq!(page.items.len(), 1);
        assert_eq!(page.items[0].id, "keep");
    }

    // --- healthcheck ---

    #[tokio::test]
    async fn healthcheck_healthy_when_connected() {
        let r = adapter().await.healthcheck().await;
        assert!(r.status.is_healthy());
    }

    #[tokio::test]
    async fn healthcheck_unhealthy_when_not_connected() {
        let a = MemoryVectorAdapter::new(VectorConfig::default());
        let r = a.healthcheck().await;
        assert!(!r.status.is_usable());
    }
}