allsource-core 0.29.0

High-performance event store core built in Rust
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
//! VectorIndex projection — maintains an HNSW index over stored embeddings.
//!
//! Uses `instant-distance` for pure-Rust HNSW (ADR-015). Since the crate is
//! batch-only (no incremental insert), vectors are accumulated in a `DashMap`
//! and the HNSW index is rebuilt after mutations by one builder at a time.
//! Only a search with no index yet waits for it; the rest keep using the last
//! build and brute-force whatever was written since.
//!
//! ## Key design decisions (ADR-015)
//!
//! - **Generation counter** instead of boolean dirty flag — avoids TOCTOU race
//!   where concurrent mutations during rebuild cause missed vectors.
//! - **No separate `deleted` DashMap** — deletions remove directly from `vectors`,
//!   eliminating snapshot/restore asymmetry and `len()` underflow bugs.

use dashmap::DashMap;
use instant_distance::{Builder, HnswMap, Point, Search};
use parking_lot::{Mutex, RwLock};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
    mem::size_of,
    sync::{
        Arc,
        atomic::{AtomicBool, AtomicU64, Ordering},
    },
    time::Instant,
};

use crate::{
    application::services::projection::Projection, domain::entities::Event, error::Result,
    infrastructure::memory::ledger::Tally, prime::projections::charge,
};

use super::types::event_types as vec_events;

/// A point in the HNSW index — wraps a `Vec<f32>`.
#[derive(Clone, Debug)]
struct VecPoint(Vec<f32>);

impl Point for VecPoint {
    fn distance(&self, other: &Self) -> f32 {
        cosine_distance(&self.0, &other.0)
    }
}

/// Cosine distance = 1.0 - cosine_similarity
fn cosine_distance(a: &[f32], b: &[f32]) -> f32 {
    let dot: f32 = a.iter().zip(b.iter()).map(|(x, y)| x * y).sum();
    let norm_a: f32 = a.iter().map(|x| x * x).sum::<f32>().sqrt();
    let norm_b: f32 = b.iter().map(|x| x * x).sum::<f32>().sqrt();
    let denom = norm_a * norm_b;
    if denom < f32::EPSILON {
        1.0
    } else {
        1.0 - (dot / denom)
    }
}

/// Serializable entry for snapshot/restore.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct VectorRecord {
    entity_id: String,
    vector: Vec<f32>,
    text: Option<String>,
    metadata: Option<Value>,
    /// Generation that made this record current; above `built_generation` means
    /// the last build does not reflect it.
    #[serde(skip)]
    written_at: u64,
}

/// Configuration for the HNSW index.
#[derive(Debug, Clone)]
pub struct VectorIndexConfig {
    pub ef_construction: usize,
    pub ef_search: usize,
}

impl Default for VectorIndexConfig {
    fn default() -> Self {
        Self {
            ef_construction: 100,
            ef_search: 100,
        }
    }
}

/// Result from a vector search.
#[derive(Debug, Clone)]
pub struct SearchHit {
    pub entity_id: String,
    pub distance: f32,
    pub text: Option<String>,
    pub metadata: Option<Value>,
}

/// HNSW-indexed vector projection with generation-based staleness tracking.
///
/// Vectors are stored in a `DashMap` as events arrive. The HNSW index is
/// rebuilt lazily when a search is performed and the generation has advanced.
pub struct VectorIndexProjection {
    name: String,
    /// entity_id -> VectorRecord (live vectors only — deletions remove entries)
    vectors: Arc<DashMap<String, VectorRecord>>,
    /// The built HNSW index (None when empty or invalidated)
    index: Arc<RwLock<Option<HnswMap<VecPoint, String>>>>,
    /// Mutation generation counter — incremented on each insert/delete.
    generation: Arc<AtomicU64>,
    /// Generation at which the index was last built.
    built_generation: Arc<AtomicU64>,
    /// Held for the whole of a build, so at most one runs.
    build_lock: Arc<Mutex<()>>,
    /// A background rebuild is queued or running.
    rebuilding: Arc<AtomicBool>,
    /// Completed builds.
    builds: Arc<AtomicU64>,
    config: VectorIndexConfig,
    /// Shared with the background handle, so one projection holds one charge.
    charge: Arc<Tally>,
}

fn record_bytes(record: &VectorRecord) -> u64 {
    charge::text(&record.entity_id)
        + (record.vector.len() * size_of::<f32>()) as u64
        + charge::opt_text(record.text.as_deref())
        + record.metadata.as_ref().map_or(0, charge::json)
}

/// Clears `rebuilding` even if the build panics, so later rebuilds still start.
struct RebuildingFlag(Arc<AtomicBool>);

impl Drop for RebuildingFlag {
    fn drop(&mut self) {
        self.0.store(false, Ordering::Release);
    }
}

impl VectorIndexProjection {
    pub fn new(name: impl Into<String>) -> Self {
        Self::with_config(name, VectorIndexConfig::default())
    }

    pub fn with_config(name: impl Into<String>, config: VectorIndexConfig) -> Self {
        Self {
            name: name.into(),
            vectors: Arc::new(DashMap::new()),
            index: Arc::new(RwLock::new(None)),
            generation: Arc::new(AtomicU64::new(0)),
            built_generation: Arc::new(AtomicU64::new(0)),
            build_lock: Arc::new(Mutex::new(())),
            rebuilding: Arc::new(AtomicBool::new(false)),
            builds: Arc::new(AtomicU64::new(0)),
            config,
            charge: Arc::new(charge::tally()),
        }
    }

    fn store(&self, record: VectorRecord) {
        let owned = record_bytes(&record);
        match self.vectors.insert(record.entity_id.clone(), record) {
            Some(old) => self.charge.adjust(record_bytes(&old), owned),
            None => self
                .charge
                .add(charge::entry::<String, VectorRecord>(owned)),
        }
    }

    /// Number of live vectors.
    pub fn len(&self) -> usize {
        self.vectors.len()
    }

    /// Whether there are no live vectors.
    pub fn is_empty(&self) -> bool {
        self.vectors.is_empty()
    }

    /// Dimension of the vectors held by this index, if any.
    ///
    /// All live vectors in a given data-dir share one dimension (the embedding
    /// model's output size). Returns `None` when the index is empty. Used to
    /// reject a mismatched embedder before it corrupts similarity search — an
    /// HNSW built over mixed-dimension points produces meaningless distances.
    pub fn dimension(&self) -> Option<usize> {
        self.vectors.iter().next().map(|e| e.value().vector.len())
    }

    /// Search for the `top_k` nearest neighbors to `query`.
    pub fn search(&self, query: &[f32], top_k: usize) -> Vec<SearchHit> {
        self.ensure_index();

        // Read before the index, which a build publishes first: a record newer
        // than this counter is then always scanned, even if the index has it.
        let built_gen = self.built_generation.load(Ordering::Acquire);
        let mut hits: Vec<SearchHit> = Vec::new();

        if let Some(hnsw) = self.index.read().as_ref() {
            let query_point = VecPoint(query.to_vec());
            let mut search = Search::default();
            hits.extend(
                hnsw.search(&query_point, &mut search)
                    .filter_map(|item| {
                        let record = self.vectors.get(item.value)?;
                        (record.written_at <= built_gen).then(|| SearchHit {
                            entity_id: item.value.clone(),
                            distance: item.distance,
                            text: record.text.clone(),
                            metadata: record.metadata.clone(),
                        })
                    })
                    .take(top_k),
            );
        }

        if self.generation.load(Ordering::Acquire) != built_gen {
            hits.extend(self.scan(query, |record| record.written_at > built_gen));
            Self::keep_nearest(&mut hits, top_k);
        }
        hits
    }

    /// Exact search over vectors whose entity id starts with `entity_prefix`.
    ///
    /// Never touches the HNSW, so a small typed subset neither waits for a
    /// rebuild nor loses its top-k to vectors of other types.
    pub fn search_prefix(
        &self,
        query: &[f32],
        top_k: usize,
        entity_prefix: &str,
    ) -> Vec<SearchHit> {
        let mut hits = self.scan(query, |record| record.entity_id.starts_with(entity_prefix));
        Self::keep_nearest(&mut hits, top_k);
        hits
    }

    fn scan(&self, query: &[f32], keep: impl Fn(&VectorRecord) -> bool) -> Vec<SearchHit> {
        self.vectors
            .iter()
            .filter(|entry| keep(entry.value()))
            .map(|entry| {
                let record = entry.value();
                SearchHit {
                    entity_id: record.entity_id.clone(),
                    distance: cosine_distance(query, &record.vector),
                    text: record.text.clone(),
                    metadata: record.metadata.clone(),
                }
            })
            .collect()
    }

    fn keep_nearest(hits: &mut Vec<SearchHit>, top_k: usize) {
        hits.sort_by(|a, b| a.distance.total_cmp(&b.distance));
        hits.truncate(top_k);
    }

    /// Bring the index up to date, or start that happening.
    fn ensure_index(&self) {
        if self.is_current() {
            return;
        }
        if self.index.read().is_some() {
            self.spawn_rebuild();
            return;
        }

        let _build = self.build_lock.lock();
        if self.index.read().is_none() && !self.is_current() {
            self.rebuild();
        }
    }

    fn is_current(&self) -> bool {
        self.generation.load(Ordering::Acquire) == self.built_generation.load(Ordering::Acquire)
    }

    fn spawn_rebuild(&self) {
        if self.rebuilding.swap(true, Ordering::AcqRel) {
            return;
        }
        let flag = RebuildingFlag(Arc::clone(&self.rebuilding));
        let worker = self.background_handle();
        let spawned = std::thread::Builder::new()
            .name("prime-vector-rebuild".to_string())
            .spawn(move || {
                let _flag = flag;
                let _build = worker.build_lock.lock();
                worker.rebuild();
            });
        if let Err(error) = spawned {
            tracing::warn!(%error, index = %self.name, "could not start vector index rebuild");
        }
    }

    fn background_handle(&self) -> Self {
        Self {
            name: self.name.clone(),
            vectors: Arc::clone(&self.vectors),
            index: Arc::clone(&self.index),
            generation: Arc::clone(&self.generation),
            built_generation: Arc::clone(&self.built_generation),
            build_lock: Arc::clone(&self.build_lock),
            rebuilding: Arc::clone(&self.rebuilding),
            builds: Arc::clone(&self.builds),
            config: self.config.clone(),
            charge: Arc::clone(&self.charge),
        }
    }

    /// Caller holds `build_lock`.
    fn rebuild(&self) {
        // Read before collecting: a write landing mid-collect leaves
        // `built_generation` behind `generation`, so it is picked up again.
        let target = self.generation.load(Ordering::Acquire);

        let mut points = Vec::new();
        let mut values = Vec::new();
        for entry in self.vectors.iter() {
            points.push(VecPoint(entry.value().vector.clone()));
            values.push(entry.key().clone());
        }
        let vector_count = points.len();

        let started = Instant::now();
        if points.is_empty() {
            *self.index.write() = None;
        } else {
            let hnsw = Builder::default()
                .ef_construction(self.config.ef_construction)
                .build(points, values);
            *self.index.write() = Some(hnsw);
        }
        self.built_generation.store(target, Ordering::Release);
        self.builds.fetch_add(1, Ordering::Relaxed);

        tracing::info!(
            index = %self.name,
            vector_count,
            build_ms = started.elapsed().as_millis() as u64,
            "vector index built"
        );
    }
}

impl Projection for VectorIndexProjection {
    fn name(&self) -> &str {
        &self.name
    }

    fn process(&self, event: &Event) -> Result<()> {
        let event_type = event.event_type_str();
        let entity_id = event.entity_id_str().to_string();
        let payload = &event.payload;

        match event_type {
            vec_events::VECTOR_STORED => {
                let vector: Vec<f32> = event
                    .metadata
                    .as_ref()
                    .and_then(|m| m.get("embedding"))
                    .and_then(|v| serde_json::from_value(v.clone()).ok())
                    .unwrap_or_default();

                let text = payload
                    .get("text")
                    .and_then(|v| v.as_str())
                    .map(String::from);

                let metadata = payload.get("metadata").cloned();
                let written_at = self.generation.load(Ordering::Acquire) + 1;

                self.store(VectorRecord {
                    entity_id,
                    vector,
                    text,
                    metadata,
                    written_at,
                });
                self.generation.fetch_add(1, Ordering::Release);
            }
            vec_events::VECTOR_DELETED => {
                if let Some((_, record)) = self.vectors.remove(&entity_id) {
                    self.charge
                        .sub(charge::entry::<String, VectorRecord>(record_bytes(&record)));
                }
                self.generation.fetch_add(1, Ordering::Release);
            }
            _ => {}
        }

        Ok(())
    }

    fn get_state(&self, entity_id: &str) -> Option<Value> {
        self.vectors
            .get(entity_id)
            .map(|r| serde_json::to_value(r.value()).unwrap_or(Value::Null))
    }

    fn clear(&self) {
        self.vectors.clear();
        self.charge.take();
        *self.index.write() = None;
        self.generation.fetch_add(1, Ordering::Release);
    }

    fn snapshot(&self) -> Option<Value> {
        let records: Vec<VectorRecord> = self.vectors.iter().map(|e| e.value().clone()).collect();
        serde_json::to_value(records).ok()
    }

    fn restore(&self, snapshot: &Value) -> Result<()> {
        let records: Vec<VectorRecord> = serde_json::from_value(snapshot.clone())
            .map_err(|e| crate::error::AllSourceError::StorageError(e.to_string()))?;
        self.vectors.clear();
        self.charge.take();
        for record in records {
            self.store(record);
        }
        // Restored records carry no generation, so the old build cannot serve them.
        *self.index.write() = None;
        self.generation.fetch_add(1, Ordering::Release);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    use uuid::Uuid;

    fn make_vector_event(entity_id: &str, vector: &[f32], text: Option<&str>) -> Event {
        Event::reconstruct_from_strings(
            Uuid::new_v4(),
            vec_events::VECTOR_STORED.to_string(),
            entity_id.to_string(),
            "default".to_string(),
            serde_json::json!({
                "text": text,
                "dimensions": vector.len(),
            }),
            Utc::now(),
            Some(serde_json::json!({ "embedding": vector })),
            1,
        )
    }

    fn make_delete_event(entity_id: &str) -> Event {
        Event::reconstruct_from_strings(
            Uuid::new_v4(),
            vec_events::VECTOR_DELETED.to_string(),
            entity_id.to_string(),
            "default".to_string(),
            serde_json::json!({}),
            Utc::now(),
            None,
            1,
        )
    }

    #[test]
    fn test_insert_100_vectors() {
        let proj = VectorIndexProjection::new("vec_idx");

        for i in 0..100 {
            let vector: Vec<f32> = (0..8).map(|j| (i * 8 + j) as f32).collect();
            let event =
                make_vector_event(&format!("vec:doc-{i}"), &vector, Some(&format!("doc {i}")));
            proj.process(&event).unwrap();
        }

        assert_eq!(proj.len(), 100);
    }

    #[test]
    fn test_dimension_reports_stored_vector_size() {
        let proj = VectorIndexProjection::new("vec_idx");
        assert_eq!(proj.dimension(), None, "empty index has no dimension");

        proj.process(&make_vector_event(
            "vec:a",
            &[1.0, 2.0, 3.0, 4.0],
            Some("x"),
        ))
        .unwrap();
        assert_eq!(proj.dimension(), Some(4));
    }

    #[test]
    fn test_search_returns_results() {
        let proj = VectorIndexProjection::new("vec_idx");

        proj.process(&make_vector_event(
            "vec:a",
            &[1.0, 0.0, 0.0, 0.0],
            Some("close"),
        ))
        .unwrap();
        proj.process(&make_vector_event(
            "vec:b",
            &[0.7, 0.7, 0.0, 0.0],
            Some("medium"),
        ))
        .unwrap();
        proj.process(&make_vector_event(
            "vec:c",
            &[0.0, 0.0, 0.0, 1.0],
            Some("far"),
        ))
        .unwrap();

        let hits = proj.search(&[1.0, 0.0, 0.0, 0.0], 3);

        assert_eq!(hits.len(), 3);
        assert_eq!(hits[0].entity_id, "vec:a");
        assert!(hits[0].distance < 0.01);
        assert_eq!(hits[2].entity_id, "vec:c");
    }

    #[test]
    fn test_delete_excludes_from_search() {
        let proj = VectorIndexProjection::new("vec_idx");

        proj.process(&make_vector_event("vec:a", &[1.0, 0.0], Some("a")))
            .unwrap();
        proj.process(&make_vector_event("vec:b", &[0.9, 0.1], Some("b")))
            .unwrap();

        proj.process(&make_delete_event("vec:a")).unwrap();

        let hits = proj.search(&[1.0, 0.0], 10);
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].entity_id, "vec:b");
    }

    #[test]
    fn test_snapshot_restore_roundtrip() {
        let proj = VectorIndexProjection::new("vec_idx");

        proj.process(&make_vector_event("vec:x", &[1.0, 0.0, 0.0], Some("x")))
            .unwrap();
        proj.process(&make_vector_event("vec:y", &[0.0, 1.0, 0.0], Some("y")))
            .unwrap();

        let snap = proj.snapshot().unwrap();
        proj.clear();
        assert_eq!(proj.len(), 0);

        proj.restore(&snap).unwrap();
        assert_eq!(proj.len(), 2);

        let hits = proj.search(&[1.0, 0.0, 0.0], 2);
        assert_eq!(hits.len(), 2);
        assert_eq!(hits[0].entity_id, "vec:x");
    }

    #[test]
    fn test_snapshot_restore_after_delete_is_clean() {
        // Regression: old code had deleted vectors reappearing after restore
        let proj = VectorIndexProjection::new("vec_idx");

        proj.process(&make_vector_event("vec:a", &[1.0, 0.0], Some("a")))
            .unwrap();
        proj.process(&make_vector_event("vec:b", &[0.0, 1.0], Some("b")))
            .unwrap();
        proj.process(&make_delete_event("vec:a")).unwrap();

        let snap = proj.snapshot().unwrap();
        proj.clear();
        proj.restore(&snap).unwrap();

        assert_eq!(proj.len(), 1);
        assert!(proj.get_state("vec:a").is_none());
        assert!(proj.get_state("vec:b").is_some());
    }

    #[test]
    fn test_search_empty_index() {
        let proj = VectorIndexProjection::new("vec_idx");
        let hits = proj.search(&[1.0, 0.0], 10);
        assert!(hits.is_empty());
    }

    #[test]
    fn test_delete_nonexistent_is_noop() {
        let proj = VectorIndexProjection::new("vec_idx");
        proj.process(&make_delete_event("vec:ghost")).unwrap();
        assert_eq!(proj.len(), 0);
    }

    fn wait_until_current(proj: &VectorIndexProjection) {
        for _ in 0..500 {
            if proj.is_current() && !proj.rebuilding.load(Ordering::Acquire) {
                return;
            }
            std::thread::sleep(std::time::Duration::from_millis(10));
        }
        panic!("vector index rebuild did not finish");
    }

    #[test]
    fn a_write_after_a_build_is_found_by_the_very_next_search() {
        let proj = VectorIndexProjection::new("vec_idx");
        proj.process(&make_vector_event("vec:old", &[0.0, 1.0], Some("old")))
            .unwrap();
        proj.search(&[1.0, 0.0], 1);

        proj.process(&make_vector_event("vec:new", &[1.0, 0.0], Some("new")))
            .unwrap();

        let hits = proj.search(&[1.0, 0.0], 1);
        assert_eq!(hits[0].entity_id, "vec:new");
    }

    #[test]
    fn a_rewritten_vector_is_returned_once_at_its_new_position() {
        let proj = VectorIndexProjection::new("vec_idx");
        proj.process(&make_vector_event("vec:a", &[0.0, 1.0], Some("before")))
            .unwrap();
        proj.search(&[1.0, 0.0], 1);

        proj.process(&make_vector_event("vec:a", &[1.0, 0.0], Some("after")))
            .unwrap();

        let hits = proj.search(&[1.0, 0.0], 10);
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].text.as_deref(), Some("after"));
        assert!(hits[0].distance < 0.01);
    }

    #[test]
    fn a_delete_after_a_build_is_not_served_from_the_old_build() {
        let proj = VectorIndexProjection::new("vec_idx");
        proj.process(&make_vector_event("vec:a", &[1.0, 0.0], Some("a")))
            .unwrap();
        proj.process(&make_vector_event("vec:b", &[0.0, 1.0], Some("b")))
            .unwrap();
        proj.search(&[1.0, 0.0], 2);

        proj.process(&make_delete_event("vec:a")).unwrap();

        let hits = proj.search(&[1.0, 0.0], 2);
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].entity_id, "vec:b");
    }

    #[test]
    fn concurrent_searches_share_one_build() {
        let proj = Arc::new(VectorIndexProjection::new("vec_idx"));
        for i in 0..200 {
            let vector = vec![i as f32, 1.0, (200 - i) as f32];
            proj.process(&make_vector_event(&format!("vec:{i}"), &vector, None))
                .unwrap();
        }

        let search_from_many_threads = |proj: &Arc<VectorIndexProjection>| {
            let handles: Vec<_> = (0..8)
                .map(|_| {
                    let proj = Arc::clone(proj);
                    std::thread::spawn(move || proj.search(&[1.0, 1.0, 1.0], 5))
                })
                .collect();
            for handle in handles {
                assert_eq!(handle.join().unwrap().len(), 5);
            }
        };

        search_from_many_threads(&proj);
        assert_eq!(proj.builds.load(Ordering::Relaxed), 1);

        proj.process(&make_vector_event("vec:late", &[1.0, 1.0, 1.0], None))
            .unwrap();
        search_from_many_threads(&proj);
        wait_until_current(&proj);
        assert_eq!(proj.builds.load(Ordering::Relaxed), 2);
    }

    #[test]
    fn prefix_search_finds_a_small_subset_behind_closer_vectors_without_a_build() {
        let proj = VectorIndexProjection::new("vec_idx");
        for i in 0..50 {
            proj.process(&make_vector_event(
                &format!("vec:output:{i}"),
                &[1.0, 0.0],
                None,
            ))
            .unwrap();
        }
        proj.process(&make_vector_event("vec:tool:far", &[0.0, 1.0], None))
            .unwrap();
        proj.process(&make_vector_event("vec:tool:near", &[0.5, 0.5], None))
            .unwrap();

        let hits = proj.search_prefix(&[1.0, 0.0], 5, "vec:tool:");

        let ids: Vec<_> = hits.iter().map(|hit| hit.entity_id.as_str()).collect();
        assert_eq!(ids, ["vec:tool:near", "vec:tool:far"]);
        assert_eq!(proj.builds.load(Ordering::Relaxed), 0);
    }

    #[test]
    fn test_zero_vector_search() {
        let proj = VectorIndexProjection::new("vec_idx");
        proj.process(&make_vector_event("vec:a", &[1.0, 0.0], Some("a")))
            .unwrap();
        proj.process(&make_vector_event("vec:zero", &[0.0, 0.0], Some("zero")))
            .unwrap();

        // Searching with zero vector should return results (distance = 1.0 for all)
        let hits = proj.search(&[0.0, 0.0], 10);
        assert_eq!(hits.len(), 2);
    }
}