1use std::collections::HashMap;
10use std::path::{Path, PathBuf};
11use std::sync::Arc;
12
13use md5::{Digest, Md5};
14use serde::{Deserialize, Serialize};
15
16use super::bm25_index::CodeChunk;
17use super::embedding_quant::{self, QuantizedVector};
18use super::hnsw::FlatEmbeddings;
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct EmbeddingIndex {
22 pub version: u32,
23 pub dimensions: usize,
24 #[serde(default)]
27 pub model_id: Option<String>,
28 pub entries: Vec<EmbeddingEntry>,
29 pub file_hashes: HashMap<String, String>,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct EmbeddingEntry {
34 pub file_path: String,
35 pub symbol_name: String,
36 pub start_line: usize,
37 pub end_line: usize,
38 pub quant: QuantizedVector,
40 pub content_hash: String,
41}
42
43impl EmbeddingEntry {
44 fn write_into_flat(&self, dest: &mut Vec<f32>) {
47 let q = &self.quant;
48 let scale = q.scale;
49 if scale == 0.0 {
50 dest.resize(dest.len() + q.code.len(), 0.0);
51 } else {
52 for &c in &q.code {
53 dest.push(f32::from(c) * scale);
54 }
55 }
56 }
57}
58
59#[derive(Debug, Clone, PartialEq, Eq)]
61pub enum EmbeddingBuildOutcome {
62 Ready,
64 Skipped,
66 ModelNotAvailable(String),
69 Failed,
71}
72
73impl EmbeddingBuildOutcome {
74 pub fn label(&self) -> &'static str {
75 match self {
76 Self::Ready => "ready",
77 Self::Skipped => "skipped",
78 Self::ModelNotAvailable(_) => "model-not-available",
79 Self::Failed => "failed",
80 }
81 }
82
83 pub fn reason(&self) -> Option<&str> {
84 match self {
85 Self::ModelNotAvailable(r) => Some(r.as_str()),
86 _ => None,
87 }
88 }
89}
90
91pub fn build_or_update(root: &Path, bm25: &super::bm25_index::BM25Index) -> EmbeddingBuildOutcome {
104 #[cfg(feature = "embeddings")]
105 {
106 let cfg = crate::core::config::Config::load();
109 if !cfg.search.dense_enabled {
110 tracing::info!("[embedding_index] build_or_update skipped: search.dense_enabled=false");
111 return EmbeddingBuildOutcome::Skipped;
112 }
113 let profile = crate::core::config::MemoryProfile::effective(&cfg);
114 if !profile.embeddings_enabled() {
115 tracing::info!(
116 "[embedding_index] build_or_update skipped: memory_profile disables embeddings"
117 );
118 return EmbeddingBuildOutcome::Skipped;
119 }
120
121 let root_key = root.to_string_lossy().to_string();
131 if !crate::core::embeddings::EmbeddingEngine::is_available() {
132 tracing::info!(
133 "[embedding_index] embedding model absent — downloading from HuggingFace"
134 );
135 crate::core::index_progress::report(
137 &root_key,
138 crate::core::index_progress::IndexComponent::Semantic,
139 0,
140 0,
141 );
142 if let Err(e) = crate::core::embeddings::EmbeddingEngine::ensure_downloaded() {
143 let reason = format!("embedding model auto-download from HuggingFace failed: {e}");
144 tracing::warn!("[embedding_index] build_or_update failed: {reason}");
145 return EmbeddingBuildOutcome::ModelNotAvailable(reason);
146 }
147 }
148
149 let engine = match crate::core::embeddings::shared_engine_result() {
150 Ok(engine) => engine,
151 Err(e) => {
152 let reason = format!(
153 "embedding model files found but engine failed to load: {e}. Check with `lean-ctx embeddings status`"
154 );
155 tracing::warn!("[embedding_index] build_or_update failed: {reason}");
156 return EmbeddingBuildOutcome::ModelNotAvailable(reason);
157 }
158 };
159
160 let model_name = engine.model_name();
161 let mut idx = EmbeddingIndex::load(root)
162 .unwrap_or_else(|| EmbeddingIndex::new_with_model(engine.dimensions(), model_name));
163
164 if let Some((stored, current)) = idx.model_mismatch(model_name) {
166 tracing::info!(
167 "[embedding_index] model changed: {stored} → {current}. Re-building from scratch."
168 );
169 idx = EmbeddingIndex::new_with_model(engine.dimensions(), model_name);
170 } else if idx.dimension_mismatch(engine.dimensions()) {
171 tracing::info!(
172 "[embedding_index] dimension mismatch: index={}d, engine={}d. Re-building.",
173 idx.dimensions,
174 engine.dimensions()
175 );
176 idx = EmbeddingIndex::new_with_model(engine.dimensions(), model_name);
177 }
178
179 let mut changed_files = idx.files_needing_update(&bm25.chunks);
180 changed_files.sort();
181 changed_files.dedup();
182
183 if changed_files.is_empty() {
184 tracing::info!(
185 "[embedding_index] all {} chunks up-to-date, nothing to embed",
186 bm25.chunks.len()
187 );
188 return EmbeddingBuildOutcome::Ready;
189 }
190
191 let changed_set: std::collections::HashSet<&str> =
192 changed_files.iter().map(String::as_str).collect();
193 let mut changed_indices: Vec<usize> = Vec::new();
194 let mut changed_texts: Vec<&str> = Vec::new();
195 for (i, c) in bm25.chunks.iter().enumerate() {
196 if changed_set.contains(c.file_path.as_str()) {
197 changed_indices.push(i);
198 changed_texts.push(&c.content);
199 }
200 }
201
202 let count = changed_files.len();
203 let chunk_total = changed_texts.len() as u64;
204 tracing::info!(
205 "[embedding_index] embedding {count} changed files ({total} chunks in index)",
206 total = bm25.chunks.len()
207 );
208
209 crate::core::index_progress::report(
210 &root_key,
211 crate::core::index_progress::IndexComponent::Semantic,
212 0,
213 chunk_total,
214 );
215
216 const EMBED_PROGRESS_BATCH: usize = 32;
219 let mut batch_embeddings: Vec<Vec<f32>> = Vec::with_capacity(changed_texts.len());
220 let mut embedded = 0usize;
221 for text_chunk in changed_texts.chunks(EMBED_PROGRESS_BATCH) {
222 match engine.embed_batch(text_chunk) {
223 Ok(mut v) => {
224 embedded += text_chunk.len();
225 batch_embeddings.append(&mut v);
226 crate::core::index_progress::report(
227 &root_key,
228 crate::core::index_progress::IndexComponent::Semantic,
229 embedded as u64,
230 chunk_total,
231 );
232 }
233 Err(e) => {
234 tracing::error!("[embedding_index] batch embed failed: {e}");
235 return EmbeddingBuildOutcome::Failed;
236 }
237 }
238 }
239
240 let new_embeddings: Vec<(usize, Vec<f32>)> =
241 changed_indices.into_iter().zip(batch_embeddings).collect();
242
243 idx.update(&bm25.chunks, &new_embeddings, &changed_files, None);
244
245 if let Err(e) = idx.save(root) {
246 tracing::error!("[embedding_index] save failed: {e}");
247 return EmbeddingBuildOutcome::Failed;
248 }
249
250 tracing::info!(
251 "[embedding_index] successfully persisted {count} file embeddings ({total} chunks)",
252 total = bm25.chunks.len()
253 );
254 EmbeddingBuildOutcome::Ready
255 }
256
257 #[cfg(not(feature = "embeddings"))]
258 {
259 let _ = (root, bm25);
260 EmbeddingBuildOutcome::Skipped
261 }
262}
263
264const CURRENT_VERSION: u32 = 3;
266
267impl EmbeddingIndex {
268 pub fn new(dimensions: usize) -> Self {
269 Self {
270 version: CURRENT_VERSION,
271 dimensions,
272 model_id: None,
273 entries: Vec::new(),
274 file_hashes: HashMap::new(),
275 }
276 }
277
278 pub fn new_with_model(dimensions: usize, model_id: &str) -> Self {
280 Self {
281 version: CURRENT_VERSION,
282 dimensions,
283 model_id: Some(model_id.to_string()),
284 entries: Vec::new(),
285 file_hashes: HashMap::new(),
286 }
287 }
288
289 pub fn model_mismatch<'a>(&'a self, current_model: &'a str) -> Option<(&'a str, &'a str)> {
292 match &self.model_id {
293 Some(stored) if stored != current_model => Some((stored, current_model)),
294 _ => None,
295 }
296 }
297
298 pub fn dimension_mismatch(&self, engine_dimensions: usize) -> bool {
300 self.dimensions != engine_dimensions && !self.entries.is_empty()
301 }
302
303 pub fn memory_usage_bytes(&self) -> usize {
305 let entries_size: usize = self
306 .entries
307 .iter()
308 .map(|e| {
309 e.file_path.len()
310 + e.symbol_name.len()
311 + e.content_hash.len()
312 + e.quant.code.len()
313 + 4
314 + 48
315 })
316 .sum();
317 let hashes_size: usize = self
318 .file_hashes
319 .iter()
320 .map(|(k, v)| k.len() + v.len() + 32)
321 .sum();
322 entries_size + hashes_size
323 }
324
325 pub fn unload(&mut self) {
327 let usage = self.memory_usage_bytes();
328 self.entries = Vec::new();
329 self.file_hashes = HashMap::new();
330 tracing::info!(
331 "[embeddings] unloaded index, freed ~{:.1}MB",
332 usage as f64 / 1_048_576.0
333 );
334 }
335
336 pub fn load_or_new(root: &Path, dimensions: usize) -> Self {
338 Self::load(root).unwrap_or_else(|| Self::new(dimensions))
339 }
340
341 pub fn files_needing_update(&self, chunks: &[CodeChunk]) -> Vec<String> {
346 if self.file_hashes.is_empty() {
348 let mut files: Vec<String> = chunks.iter().map(|c| c.file_path.clone()).collect();
349 files.sort();
350 files.dedup();
351 return files;
352 }
353
354 let current_hashes = compute_file_hashes(chunks);
355
356 let mut needs_update = Vec::new();
357 for (file, hash) in ¤t_hashes {
358 match self.file_hashes.get(file) {
359 Some(old_hash) if old_hash == hash => {}
360 _ => needs_update.push(file.clone()),
361 }
362 }
363
364 for file in self.file_hashes.keys() {
365 if !current_hashes.contains_key(file) {
366 needs_update.push(file.clone());
367 }
368 }
369
370 needs_update
371 }
372
373 pub fn pending_chunk_count(&self, chunks: &[CodeChunk]) -> usize {
382 let changed = self.files_needing_update(chunks);
383 if changed.is_empty() {
384 return 0;
385 }
386 let changed: std::collections::HashSet<&str> = changed.iter().map(String::as_str).collect();
387 chunks
388 .iter()
389 .filter(|c| changed.contains(c.file_path.as_str()))
390 .count()
391 }
392
393 pub fn update(
400 &mut self,
401 chunks: &[CodeChunk],
402 new_embeddings: &[(usize, Vec<f32>)],
403 changed_files: &[String],
404 precomputed_hashes: Option<HashMap<String, String>>,
405 ) {
406 self.entries
407 .retain(|e| !changed_files.contains(&e.file_path));
408
409 for file in changed_files {
410 self.file_hashes.remove(file);
411 }
412
413 let current_hashes = precomputed_hashes.unwrap_or_else(|| compute_file_hashes(chunks));
414 for file in changed_files {
415 if let Some(hash) = current_hashes.get(file) {
416 self.file_hashes.insert(file.clone(), hash.clone());
417 }
418 }
419
420 for &(chunk_idx, ref embedding) in new_embeddings {
421 if let Some(chunk) = chunks.get(chunk_idx) {
422 let content_hash = hash_content(&chunk.content);
423 self.entries.push(EmbeddingEntry {
424 file_path: chunk.file_path.clone(),
425 symbol_name: chunk.symbol_name.clone(),
426 start_line: chunk.start_line,
427 end_line: chunk.end_line,
428 quant: embedding_quant::quantize(embedding),
429 content_hash,
430 });
431 }
432 }
433 }
434
435 pub fn get_aligned_flat(&self, chunks: &[CodeChunk]) -> Option<FlatEmbeddings> {
443 let dim = self.dimensions;
444 let mut map: HashMap<(&str, usize, usize), &EmbeddingEntry> =
445 HashMap::with_capacity(self.entries.len());
446 for e in &self.entries {
447 map.insert((e.file_path.as_str(), e.start_line, e.end_line), e);
448 }
449
450 let n = chunks.len();
451 let mut data = Vec::with_capacity(n * dim);
452 for chunk in chunks {
453 let entry = map.get(&(chunk.file_path.as_str(), chunk.start_line, chunk.end_line))?;
454 entry.write_into_flat(&mut data);
455 }
456 Some(FlatEmbeddings {
457 data: Arc::from(data),
458 dim,
459 })
460 }
461
462 pub fn coverage(&self, total_chunks: usize) -> f64 {
463 if total_chunks == 0 {
464 return 0.0;
465 }
466 self.entries.len() as f64 / total_chunks as f64
467 }
468
469 pub fn save(&self, root: &Path) -> std::io::Result<()> {
470 let dir = index_dir(root);
471 std::fs::create_dir_all(&dir)?;
472 let data = postcard::to_allocvec(self).map_err(std::io::Error::other)?;
474 crate::core::atomic_fs::try_atomic_write(&dir.join("embeddings.bin"), &data, None)?;
477 Ok(())
478 }
479
480 pub fn load(root: &Path) -> Option<Self> {
481 let bin_path = index_dir(root).join("embeddings.bin");
482 let data = std::fs::read(&bin_path).ok()?;
483 match postcard::from_bytes::<Self>(&data) {
484 Ok(idx) if idx.version == CURRENT_VERSION => Some(idx),
486 Ok(idx) => {
490 tracing::warn!(
491 "[embeddings] index format v{} != current v{CURRENT_VERSION} — \
492 removing and rebuilding from scratch",
493 idx.version
494 );
495 let _ = std::fs::remove_file(&bin_path);
496 None
497 }
498 Err(_) => {
499 tracing::warn!(
500 "[embeddings] corrupt embeddings.bin — removing and will rebuild from scratch"
501 );
502 let _ = std::fs::remove_file(&bin_path);
503 None
504 }
505 }
506 }
507}
508
509fn index_dir(root: &Path) -> PathBuf {
510 crate::core::index_namespace::vectors_dir(root)
511}
512
513fn hash_content(content: &str) -> String {
514 let mut hasher = Md5::new();
515 hasher.update(content.as_bytes());
516 crate::core::agent_identity::hex_encode(&hasher.finalize())
517}
518
519fn compute_file_hashes(chunks: &[CodeChunk]) -> HashMap<String, String> {
520 let mut by_file: HashMap<&str, Vec<&CodeChunk>> = HashMap::new();
521 for chunk in chunks {
522 by_file
523 .entry(chunk.file_path.as_str())
524 .or_default()
525 .push(chunk);
526 }
527
528 let mut out: HashMap<String, String> = HashMap::with_capacity(by_file.len());
529 for (file, mut file_chunks) in by_file {
530 file_chunks.sort_by(|a, b| {
531 (a.start_line, a.end_line, a.symbol_name.as_str()).cmp(&(
532 b.start_line,
533 b.end_line,
534 b.symbol_name.as_str(),
535 ))
536 });
537
538 let mut hasher = Md5::new();
539 hasher.update(file.as_bytes());
540 for c in file_chunks {
541 hasher.update(c.start_line.to_le_bytes());
542 hasher.update(c.end_line.to_le_bytes());
543 hasher.update(c.symbol_name.as_bytes());
544 hasher.update([kind_tag(&c.kind)]);
545 hasher.update(c.content.as_bytes());
546 }
547 out.insert(
548 file.to_string(),
549 crate::core::agent_identity::hex_encode(&hasher.finalize()),
550 );
551 }
552 out
553}
554
555fn kind_tag(kind: &super::bm25_index::ChunkKind) -> u8 {
556 use super::bm25_index::ChunkKind;
557 match kind {
558 ChunkKind::Function => 1,
559 ChunkKind::Struct => 2,
560 ChunkKind::Impl => 3,
561 ChunkKind::Module => 4,
562 ChunkKind::Class => 5,
563 ChunkKind::Method => 6,
564 ChunkKind::Other => 7,
565 ChunkKind::Issue => 8,
566 ChunkKind::PullRequest => 9,
567 ChunkKind::WikiPage => 10,
568 ChunkKind::DbSchema => 11,
569 ChunkKind::ApiEndpoint => 12,
570 ChunkKind::Ticket => 13,
571 ChunkKind::ExternalOther => 14,
572 }
573}
574
575#[cfg(test)]
576pub mod tests {
577 use super::*;
578 use crate::core::bm25_index::{ChunkKind, CodeChunk};
579
580 fn make_chunk(file: &str, name: &str, content: &str, start: usize, end: usize) -> CodeChunk {
581 CodeChunk {
582 file_path: file.to_string(),
583 symbol_name: name.to_string(),
584 kind: ChunkKind::Function,
585 start_line: start,
586 end_line: end,
587 content: content.to_string(),
588 tokens: vec![name.to_string()],
589 token_count: 1,
590 }
591 }
592
593 fn dummy_embedding(dim: usize) -> Vec<f32> {
594 vec![0.1; dim]
595 }
596
597 #[test]
598 fn new_index_is_empty() {
599 let idx = EmbeddingIndex::new(384);
600 assert!(idx.entries.is_empty());
601 assert!(idx.file_hashes.is_empty());
602 assert_eq!(idx.dimensions, 384);
603 }
604
605 #[test]
606 fn files_needing_update_all_new() {
607 let idx = EmbeddingIndex::new(384);
608 let chunks = vec![
609 make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3),
610 make_chunk("b.rs", "fn_b", "fn b() {}", 1, 3),
611 ];
612 let needs = idx.files_needing_update(&chunks);
613 assert_eq!(needs.len(), 2);
614 }
615
616 #[test]
617 fn files_needing_update_unchanged() {
618 let mut idx = EmbeddingIndex::new(384);
619 let chunks = vec![make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3)];
620
621 idx.update(
622 &chunks,
623 &[(0, dummy_embedding(384))],
624 &["a.rs".to_string()],
625 None,
626 );
627
628 let needs = idx.files_needing_update(&chunks);
629 assert!(needs.is_empty(), "unchanged file should not need update");
630 }
631
632 #[test]
633 fn files_needing_update_changed_content() {
634 let mut idx = EmbeddingIndex::new(384);
635 let chunks_v1 = vec![make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3)];
636 idx.update(
637 &chunks_v1,
638 &[(0, dummy_embedding(384))],
639 &["a.rs".to_string()],
640 None,
641 );
642
643 let chunks_v2 = vec![make_chunk("a.rs", "fn_a", "fn a() { modified }", 1, 3)];
644 let needs = idx.files_needing_update(&chunks_v2);
645 assert!(
646 needs.contains(&"a.rs".to_string()),
647 "changed file should need update"
648 );
649 }
650
651 #[test]
652 fn files_needing_update_detects_change_in_later_chunk() {
653 let mut idx = EmbeddingIndex::new(3);
654 let chunks_v1 = vec![
655 make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3),
656 make_chunk("a.rs", "fn_b", "fn b() {}", 10, 12),
657 ];
658 idx.update(
659 &chunks_v1,
660 &[(0, vec![0.1, 0.1, 0.1]), (1, vec![0.2, 0.2, 0.2])],
661 &["a.rs".to_string()],
662 None,
663 );
664
665 let chunks_v2 = vec![
666 make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3),
667 make_chunk("a.rs", "fn_b", "fn b() { changed }", 10, 12),
668 ];
669 let needs = idx.files_needing_update(&chunks_v2);
670 assert!(
671 needs.contains(&"a.rs".to_string()),
672 "changing a later chunk should trigger re-embedding"
673 );
674 }
675
676 #[test]
677 fn files_needing_update_deleted_file() {
678 let mut idx = EmbeddingIndex::new(384);
679 let chunks = vec![
680 make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3),
681 make_chunk("b.rs", "fn_b", "fn b() {}", 1, 3),
682 ];
683 idx.update(
684 &chunks,
685 &[(0, dummy_embedding(384)), (1, dummy_embedding(384))],
686 &["a.rs".to_string(), "b.rs".to_string()],
687 None,
688 );
689
690 let chunks_after = vec![make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3)];
691 let needs = idx.files_needing_update(&chunks_after);
692 assert!(
693 needs.contains(&"b.rs".to_string()),
694 "deleted file should trigger update"
695 );
696 }
697
698 #[test]
699 fn pending_chunk_count_cold_start_counts_every_chunk() {
700 let idx = EmbeddingIndex::new(384);
701 let chunks = vec![
702 make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3),
703 make_chunk("a.rs", "fn_b", "fn b() {}", 10, 12),
704 make_chunk("b.rs", "fn_c", "fn c() {}", 1, 3),
705 ];
706 assert_eq!(
707 idx.pending_chunk_count(&chunks),
708 3,
709 "an empty index must report every chunk as pending (cold start)"
710 );
711 }
712
713 #[test]
714 fn pending_chunk_count_zero_when_fully_embedded() {
715 let mut idx = EmbeddingIndex::new(384);
716 let chunks = vec![
717 make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3),
718 make_chunk("b.rs", "fn_b", "fn b() {}", 1, 3),
719 ];
720 idx.update(
721 &chunks,
722 &[(0, dummy_embedding(384)), (1, dummy_embedding(384))],
723 &["a.rs".to_string(), "b.rs".to_string()],
724 None,
725 );
726 assert_eq!(
727 idx.pending_chunk_count(&chunks),
728 0,
729 "a fully-embedded index has no pending chunks (warm path stays inline)"
730 );
731 }
732
733 #[test]
734 fn pending_chunk_count_only_counts_changed_files_chunks() {
735 let mut idx = EmbeddingIndex::new(384);
736 let chunks_v1 = vec![
737 make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3),
738 make_chunk("a.rs", "fn_b", "fn b() {}", 10, 12),
739 make_chunk("b.rs", "fn_c", "fn c() {}", 1, 3),
740 ];
741 idx.update(
742 &chunks_v1,
743 &[
744 (0, dummy_embedding(384)),
745 (1, dummy_embedding(384)),
746 (2, dummy_embedding(384)),
747 ],
748 &["a.rs".to_string(), "b.rs".to_string()],
749 None,
750 );
751
752 let chunks_v2 = vec![
754 make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3),
755 make_chunk("a.rs", "fn_b", "fn b() {}", 10, 12),
756 make_chunk("b.rs", "fn_c", "fn c() { changed }", 1, 3),
757 ];
758 assert_eq!(
759 idx.pending_chunk_count(&chunks_v2),
760 1,
761 "incremental update must only count the changed file's chunks"
762 );
763 }
764
765 #[test]
766 fn update_preserves_unchanged() {
767 let mut idx = EmbeddingIndex::new(384);
768 let chunks = vec![
769 make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3),
770 make_chunk("b.rs", "fn_b", "fn b() {}", 1, 3),
771 ];
772 idx.update(
773 &chunks,
774 &[(0, dummy_embedding(384)), (1, dummy_embedding(384))],
775 &["a.rs".to_string(), "b.rs".to_string()],
776 None,
777 );
778 assert_eq!(idx.entries.len(), 2);
779
780 idx.update(&chunks, &[(0, vec![0.5; 384])], &["a.rs".to_string()], None);
781 assert_eq!(idx.entries.len(), 2);
782
783 let b_entry = idx.entries.iter().find(|e| e.file_path == "b.rs").unwrap();
784 let b_embed = b_entry.quant.dequantize();
785 assert!(
786 (b_embed[0] - 0.1).abs() < 1e-6,
787 "b.rs embedding should be preserved"
788 );
789 }
790
791 #[test]
792 fn get_aligned_flat_ok() {
793 let mut idx = EmbeddingIndex::new(2);
794 let chunks = vec![
795 make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3),
796 make_chunk("b.rs", "fn_b", "fn b() {}", 1, 3),
797 ];
798 idx.update(
799 &chunks,
800 &[(0, vec![1.0, 0.0]), (1, vec![0.0, 1.0])],
801 &["a.rs".to_string(), "b.rs".to_string()],
802 None,
803 );
804
805 let flat = idx.get_aligned_flat(&chunks).unwrap();
806 assert_eq!(flat.n_vectors(), 2);
807 assert_eq!(flat.dim, 2);
808 assert!((flat.get(0)[0] - 1.0).abs() < 1e-6);
809 assert!((flat.get(1)[1] - 1.0).abs() < 1e-6);
810 }
811
812 #[test]
813 fn get_aligned_flat_missing() {
814 let idx = EmbeddingIndex::new(384);
815 let chunks = vec![make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3)];
816 assert!(idx.get_aligned_flat(&chunks).is_none());
817 }
818
819 #[test]
820 fn coverage_calculation() {
821 let mut idx = EmbeddingIndex::new(384);
822 assert!((idx.coverage(10) - 0.0).abs() < 1e-6);
823
824 let chunks = vec![make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3)];
825 idx.update(
826 &chunks,
827 &[(0, dummy_embedding(384))],
828 &["a.rs".to_string()],
829 None,
830 );
831 assert!((idx.coverage(2) - 0.5).abs() < 1e-6);
832 assert!((idx.coverage(1) - 1.0).abs() < 1e-6);
833 }
834
835 #[test]
836 fn save_and_load_roundtrip() {
837 let _lock = crate::core::data_dir::test_env_lock();
838 let data_dir = tempfile::tempdir().unwrap();
839 crate::test_env::set_var("LEAN_CTX_DATA_DIR", data_dir.path());
840
841 let project_dir = tempfile::tempdir().unwrap();
842
843 let mut idx = EmbeddingIndex::new(3);
844 let chunks = vec![make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3)];
845 idx.update(
846 &chunks,
847 &[(0, vec![1.0, 2.0, 3.0])],
848 &["a.rs".to_string()],
849 None,
850 );
851 idx.save(project_dir.path()).unwrap();
852
853 let loaded = EmbeddingIndex::load(project_dir.path()).unwrap();
854 assert_eq!(loaded.dimensions, 3);
855 assert_eq!(loaded.entries.len(), 1);
856 let recon = loaded.entries[0].quant.dequantize();
858 assert!((recon[0] - 1.0).abs() < 0.02);
859 assert!((recon[1] - 2.0).abs() < 0.02);
860 assert!((recon[2] - 3.0).abs() < 0.02);
861
862 crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
863 }
864
865 #[test]
866 fn new_with_model_sets_model_id() {
867 let idx = EmbeddingIndex::new_with_model(768, "jina-code-v2");
868 assert_eq!(idx.model_id, Some("jina-code-v2".to_string()));
869 assert_eq!(idx.dimensions, 768);
870 }
871
872 #[test]
873 fn model_mismatch_detection() {
874 let idx = EmbeddingIndex::new_with_model(768, "all-MiniLM-L6-v2");
875 assert!(idx.model_mismatch("all-MiniLM-L6-v2").is_none());
876 assert!(idx.model_mismatch("jina-code-v2").is_some());
877
878 let (stored, current) = idx.model_mismatch("jina-code-v2").unwrap();
879 assert_eq!(stored, "all-MiniLM-L6-v2");
880 assert_eq!(current, "jina-code-v2");
881 }
882
883 #[test]
884 fn model_mismatch_none_when_no_model_id() {
885 let idx = EmbeddingIndex::new(384);
886 assert!(idx.model_mismatch("anything").is_none());
887 }
888
889 #[test]
890 fn dimension_mismatch_detection() {
891 let mut idx = EmbeddingIndex::new(384);
892 assert!(!idx.dimension_mismatch(384));
893 assert!(!idx.dimension_mismatch(768)); let chunks = vec![make_chunk("a.rs", "fn_a", "fn a() {}", 1, 3)];
896 idx.update(
897 &chunks,
898 &[(0, dummy_embedding(384))],
899 &["a.rs".to_string()],
900 None,
901 );
902 assert!(!idx.dimension_mismatch(384));
903 assert!(idx.dimension_mismatch(768));
904 }
905}