tldr-core 0.1.2

Core analysis engine for TLDR code analysis tool
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
//! Semantic search index combining embedder, chunker, similarity, and cache
//!
//! This module provides the `SemanticIndex` struct, which is the main entry point
//! for semantic code search. It combines:
//!
//! - **Embedder**: Generates dense embeddings using fastembed-rs
//! - **Chunker**: Extracts code chunks via tree-sitter
//! - **Similarity**: Cosine similarity and top-K search
//! - **Cache**: Optional embedding persistence
//!
//! # P0 Mitigations (from phased-plan.yaml)
//!
//! - Hard limit at MAX_INDEX_SIZE (100K chunks) to prevent memory exhaustion
//! - Memory estimate before building to fail fast on large codebases
//! - Report parse failures during index build
//!
//! # Example
//!
//! ```rust,ignore
//! use std::path::Path;
//! use tldr_core::semantic::{SemanticIndex, SearchOptions, BuildOptions};
//!
//! // Build an index from a project directory
//! let index = SemanticIndex::build(
//!     Path::new("src/"),
//!     BuildOptions::default(),
//!     None, // No cache
//! )?;
//!
//! // Search for semantically related code
//! let report = index.search("parse configuration file", &SearchOptions::default())?;
//!
//! for result in report.results {
//!     println!("{}: {} (score: {:.2})",
//!         result.file_path.display(),
//!         result.function_name.unwrap_or_default(),
//!         result.score
//!     );
//! }
//! ```

use std::path::Path;
use std::time::Instant;

use crate::semantic::cache::EmbeddingCache;
use crate::semantic::chunker::chunk_code;
use crate::semantic::embedder::Embedder;
use crate::semantic::similarity::top_k_similar;
use crate::semantic::types::{
    CacheConfig, ChunkGranularity, ChunkOptions, EmbeddedChunk, EmbeddingModel,
    SemanticSearchReport, SemanticSearchResult, SimilarityReport,
};
use crate::{TldrError, TldrResult};

// =============================================================================
// Constants (P0 Mitigations)
// =============================================================================

/// Maximum number of chunks allowed in index (P0 mitigation)
///
/// Prevents memory exhaustion on large codebases. For larger projects,
/// users should filter by language or directory.
pub const MAX_INDEX_SIZE: usize = 100_000;

/// Estimated memory per chunk in bytes
///
/// Calculation: 768 dims * 4 bytes per f32 + ~500 bytes metadata
const BYTES_PER_CHUNK: usize = 768 * 4 + 500;

/// Maximum memory usage in bytes (500MB)
const MAX_MEMORY_BYTES: usize = 500 * 1024 * 1024;

// =============================================================================
// Build Options
// =============================================================================

/// Options for building a semantic index
///
/// Controls how the index is constructed, including model selection,
/// chunking granularity, and caching behavior.
#[derive(Debug, Clone)]
pub struct BuildOptions {
    /// Embedding model to use
    pub model: EmbeddingModel,

    /// Chunking granularity (file or function level)
    pub granularity: ChunkGranularity,

    /// Languages to process (None = auto-detect all)
    pub languages: Option<Vec<String>>,

    /// Show progress during index building
    pub show_progress: bool,

    /// Use embedding cache
    pub use_cache: bool,
}

impl Default for BuildOptions {
    fn default() -> Self {
        Self {
            model: EmbeddingModel::default(),
            granularity: ChunkGranularity::Function,
            languages: None,
            show_progress: true,
            use_cache: true,
        }
    }
}

// =============================================================================
// Search Options (re-exported from types but with local defaults)
// =============================================================================

/// Options for semantic search operations
///
/// Controls how search results are filtered and ranked.
#[derive(Debug, Clone)]
pub struct SearchOptions {
    /// Maximum number of results to return
    pub top_k: usize,

    /// Minimum similarity threshold (0.0 to 1.0)
    pub threshold: f64,

    /// Include code snippet in results
    pub include_snippet: bool,

    /// Maximum lines in snippet
    pub snippet_lines: usize,
}

impl Default for SearchOptions {
    fn default() -> Self {
        Self {
            top_k: 10,
            threshold: 0.5,
            include_snippet: true,
            snippet_lines: 5,
        }
    }
}

// =============================================================================
// Semantic Index
// =============================================================================

/// In-memory semantic index for fast similarity search
///
/// The index holds embedded code chunks and supports natural language
/// queries and code similarity searches.
///
/// # Memory Usage
///
/// Memory usage is approximately `chunks * (dimensions * 4 + 500)` bytes.
/// For 10K functions with 768-dim embeddings: ~30MB.
///
/// # Thread Safety
///
/// `SemanticIndex` is `Send` but not `Sync` due to the internal embedder.
/// Create one index per thread for concurrent searches.
pub struct SemanticIndex {
    /// All embedded chunks in the index
    chunks: Vec<EmbeddedChunk>,

    /// Embedding model used for all embeddings
    model: EmbeddingModel,

    /// Embedder for query embedding (lazily initialized, reused for searches)
    /// None if index was built entirely from cache
    embedder: Option<Embedder>,
}

impl SemanticIndex {
    /// Build a semantic index from a directory
    ///
    /// Extracts code chunks, generates embeddings, and builds a searchable index.
    ///
    /// # Arguments
    ///
    /// * `root` - Project root directory to index
    /// * `options` - Build options (model, granularity, etc.)
    /// * `cache_config` - Optional cache configuration for embedding persistence
    ///
    /// # Returns
    ///
    /// * `Ok(SemanticIndex)` - Built index ready for search
    /// * `Err(TldrError::IndexTooLarge)` - If chunk count exceeds MAX_INDEX_SIZE
    /// * `Err(TldrError::MemoryLimitExceeded)` - If estimated memory exceeds limit
    ///
    /// # P0 Mitigations
    ///
    /// - Hard limit at 100K chunks
    /// - Memory estimate before building
    /// - Reports parse failures (not silent)
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let index = SemanticIndex::build(
    ///     Path::new("src/"),
    ///     BuildOptions::default(),
    ///     None,
    /// )?;
    /// ```
    pub fn build<P: AsRef<Path>>(
        root: P,
        options: BuildOptions,
        cache_config: Option<CacheConfig>,
    ) -> TldrResult<Self> {
        let start = Instant::now();
        let root = root.as_ref();

        // Initialize cache if configured
        let mut cache = if options.use_cache {
            cache_config.map(EmbeddingCache::open).transpose()?
        } else {
            None
        };

        // Convert languages from strings if provided
        let chunk_languages = options.languages.as_ref().map(|langs| {
            langs
                .iter()
                .filter_map(|s| crate::Language::from_extension(s))
                .collect()
        });

        // Chunk the codebase
        let chunk_opts = ChunkOptions {
            granularity: options.granularity,
            languages: chunk_languages,
            ..Default::default()
        };

        let chunk_result = chunk_code(root, &chunk_opts)?;

        // P0: Check index size limit
        if chunk_result.chunks.len() > MAX_INDEX_SIZE {
            return Err(TldrError::IndexTooLarge {
                count: chunk_result.chunks.len(),
                max: MAX_INDEX_SIZE,
            });
        }

        // P0: Memory estimate
        let estimated_memory = chunk_result.chunks.len() * BYTES_PER_CHUNK;
        if estimated_memory > MAX_MEMORY_BYTES {
            return Err(TldrError::MemoryLimitExceeded {
                estimated_mb: estimated_memory / (1024 * 1024),
                max_mb: MAX_MEMORY_BYTES / (1024 * 1024),
            });
        }

        // Progress reporting
        if options.show_progress && !chunk_result.chunks.is_empty() {
            eprintln!("Building index for {} chunks...", chunk_result.chunks.len());
        }

        // Report skipped files (P0: not silent)
        if !chunk_result.skipped.is_empty() && options.show_progress {
            eprintln!(
                "Skipped {} files (parse errors or unsupported)",
                chunk_result.skipped.len()
            );
        }

        // Phase 1: Separate cached vs uncached chunks
        let mut embedded_chunks: Vec<EmbeddedChunk> = Vec::with_capacity(chunk_result.chunks.len());
        let mut uncached_indices: Vec<usize> = Vec::new();

        for (i, chunk) in chunk_result.chunks.iter().enumerate() {
            let cached_embedding = if let Some(ref mut c) = cache {
                c.get(chunk, options.model)
            } else {
                None
            };

            match cached_embedding {
                Some(e) => {
                    embedded_chunks.push(EmbeddedChunk {
                        chunk: chunk.clone(),
                        embedding: e,
                    });
                }
                None => {
                    // Placeholder - will be filled by batch embed
                    embedded_chunks.push(EmbeddedChunk {
                        chunk: chunk.clone(),
                        embedding: Vec::new(),
                    });
                    uncached_indices.push(i);
                }
            }
        }

        // Phase 2: Lazy initialize embedder and batch embed uncached chunks
        // Only load the ONNX model if there are cache misses
        let embedder = if !uncached_indices.is_empty() {
            if options.show_progress {
                eprintln!(
                    "Batch embedding {} uncached chunks...",
                    uncached_indices.len()
                );
            }

            // Initialize embedder (loads 110MB ONNX model)
            let mut embedder = Embedder::new(options.model)?;

            let texts: Vec<&str> = uncached_indices
                .iter()
                .map(|&i| chunk_result.chunks[i].content.as_str())
                .collect();
            let embeddings = embedder.embed_batch(texts, options.show_progress)?;

            for (idx, embedding) in uncached_indices.iter().zip(embeddings) {
                // Store in cache
                if let Some(ref mut c) = cache {
                    c.put(&chunk_result.chunks[*idx], embedding.clone(), options.model);
                }
                embedded_chunks[*idx].embedding = embedding;
            }

            Some(embedder)
        } else {
            // All chunks were cached - skip ONNX model load entirely
            // Embedder will be lazily created later if search() is called
            if options.show_progress {
                eprintln!("All chunks cached - skipping embedder initialization");
            }
            None
        };

        // Flush cache
        if let Some(ref mut c) = cache {
            c.flush()?;
        }

        if options.show_progress {
            eprintln!("Index built in {:?}", start.elapsed());
        }

        Ok(Self {
            chunks: embedded_chunks,
            model: options.model,
            embedder,
        })
    }

    /// Search the index with a natural language query
    ///
    /// Embeds the query and finds the most similar code chunks.
    ///
    /// # Arguments
    ///
    /// * `query` - Natural language search query
    /// * `options` - Search options (top_k, threshold, etc.)
    ///
    /// # Returns
    ///
    /// * `Ok(SemanticSearchReport)` - Search results with scores and metadata
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let report = index.search("parse configuration file", &SearchOptions::default())?;
    /// for result in report.results {
    ///     println!("{}: {} ({:.2})",
    ///         result.file_path.display(),
    ///         result.function_name.unwrap_or_default(),
    ///         result.score
    ///     );
    /// }
    /// ```
    pub fn search(
        &mut self,
        query: &str,
        options: &SearchOptions,
    ) -> TldrResult<SemanticSearchReport> {
        let start = Instant::now();

        // Lazy initialize embedder if not already loaded
        if self.embedder.is_none() {
            self.embedder = Some(Embedder::new(self.model)?);
        }

        // Embed query
        let query_embedding = self.embedder.as_mut().unwrap().embed_text(query)?;

        // Build candidates for top_k_similar
        let candidates: Vec<(usize, &[f32])> = self
            .chunks
            .iter()
            .enumerate()
            .map(|(i, c)| (i, c.embedding.as_slice()))
            .collect();

        // Find similar chunks
        let similar = top_k_similar(
            &query_embedding,
            &candidates,
            options.top_k,
            options.threshold,
        );

        // Build results
        let results: Vec<SemanticSearchResult> = similar
            .into_iter()
            .map(|(idx, score)| {
                let chunk = &self.chunks[idx].chunk;
                let snippet = if options.include_snippet {
                    make_snippet(&chunk.content, options.snippet_lines)
                } else {
                    String::new()
                };
                SemanticSearchResult {
                    file_path: chunk.file_path.clone(),
                    function_name: chunk.function_name.clone(),
                    class_name: chunk.class_name.clone(),
                    score,
                    line_start: chunk.line_start,
                    line_end: chunk.line_end,
                    snippet,
                }
            })
            .collect();

        let matches_above_threshold = results.len();

        Ok(SemanticSearchReport {
            query: query.to_string(),
            model: self.model,
            results,
            total_chunks: self.chunks.len(),
            matches_above_threshold,
            latency_ms: start.elapsed().as_millis() as u64,
            cache_hit: false, // Query embeddings are not cached
        })
    }

    /// Find chunks similar to a given file/function
    ///
    /// Looks up a chunk in the index and finds similar code elsewhere.
    ///
    /// # Arguments
    ///
    /// * `file_path` - Path to the source file
    /// * `function_name` - Optional function name (None for file-level match)
    /// * `options` - Search options
    ///
    /// # Returns
    ///
    /// * `Ok(SimilarityReport)` - Similar chunks with scores
    /// * `Err(TldrError::ChunkNotFound)` - If the specified chunk is not in the index
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let report = index.find_similar("src/config.rs", Some("parse_config"), &SearchOptions::default())?;
    /// for similar in report.similar {
    ///     println!("{}: {} ({:.2})",
    ///         similar.file_path.display(),
    ///         similar.function_name.unwrap_or_default(),
    ///         similar.score
    ///     );
    /// }
    /// ```
    pub fn find_similar(
        &self,
        file_path: &str,
        function_name: Option<&str>,
        options: &SearchOptions,
    ) -> TldrResult<SimilarityReport> {
        // Find the query chunk
        let query_chunk = self
            .chunks
            .iter()
            .find(|c| {
                c.chunk.file_path.to_string_lossy() == file_path
                    && (function_name.is_none()
                        || c.chunk.function_name.as_deref() == function_name)
            })
            .ok_or_else(|| TldrError::ChunkNotFound {
                file: file_path.to_string(),
                function: function_name.map(String::from),
            })?;

        // Build candidates (excluding self)
        let candidates: Vec<(usize, &[f32])> = self
            .chunks
            .iter()
            .enumerate()
            .filter(|(_, c)| {
                c.chunk.file_path.to_string_lossy() != file_path
                    || c.chunk.function_name != query_chunk.chunk.function_name
            })
            .map(|(i, c)| (i, c.embedding.as_slice()))
            .collect();

        // Find similar
        let similar = top_k_similar(
            &query_chunk.embedding,
            &candidates,
            options.top_k,
            options.threshold,
        );

        // Build results
        let results: Vec<SemanticSearchResult> = similar
            .into_iter()
            .map(|(idx, score)| {
                let chunk = &self.chunks[idx].chunk;
                let snippet = if options.include_snippet {
                    make_snippet(&chunk.content, options.snippet_lines)
                } else {
                    String::new()
                };
                SemanticSearchResult {
                    file_path: chunk.file_path.clone(),
                    function_name: chunk.function_name.clone(),
                    class_name: chunk.class_name.clone(),
                    score,
                    line_start: chunk.line_start,
                    line_end: chunk.line_end,
                    snippet,
                }
            })
            .collect();

        Ok(SimilarityReport {
            source: query_chunk.chunk.clone(),
            model: self.model,
            similar: results,
            total_compared: candidates.len(),
            exclude_self: true,
        })
    }

    /// Get a specific chunk by file and function name
    ///
    /// # Arguments
    ///
    /// * `file_path` - Path to the source file
    /// * `function_name` - Optional function name
    ///
    /// # Returns
    ///
    /// The embedded chunk if found, None otherwise.
    pub fn get_chunk(
        &self,
        file_path: &str,
        function_name: Option<&str>,
    ) -> Option<&EmbeddedChunk> {
        self.chunks.iter().find(|c| {
            c.chunk.file_path.to_string_lossy() == file_path
                && (function_name.is_none() || c.chunk.function_name.as_deref() == function_name)
        })
    }

    /// Get the number of chunks in the index
    pub fn len(&self) -> usize {
        self.chunks.len()
    }

    /// Check if the index is empty
    pub fn is_empty(&self) -> bool {
        self.chunks.is_empty()
    }

    /// Get all chunks in the index
    pub fn chunks(&self) -> &[EmbeddedChunk] {
        &self.chunks
    }

    /// Get the embedding model used by this index
    pub fn model(&self) -> EmbeddingModel {
        self.model
    }
}

// =============================================================================
// Helper Functions
// =============================================================================

/// Create a snippet from code content
///
/// Takes the first N lines of the content for display purposes.
fn make_snippet(content: &str, max_lines: usize) -> String {
    content
        .lines()
        .take(max_lines)
        .collect::<Vec<_>>()
        .join("\n")
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod index_tests {
    use super::*;

    // =========================================================================
    // SearchOptions tests
    // =========================================================================

    #[test]
    fn search_options_default_values() {
        // GIVEN: Default search options
        let options = SearchOptions::default();

        // THEN: Should have sensible defaults
        assert_eq!(options.top_k, 10);
        assert!((options.threshold - 0.5).abs() < 1e-6);
        assert!(options.include_snippet);
        assert_eq!(options.snippet_lines, 5);
    }

    // =========================================================================
    // BuildOptions tests
    // =========================================================================

    #[test]
    fn build_options_default_values() {
        // GIVEN: Default build options
        let options = BuildOptions::default();

        // THEN: Should have sensible defaults
        assert_eq!(options.model, EmbeddingModel::ArcticM);
        assert_eq!(options.granularity, ChunkGranularity::Function);
        assert!(options.languages.is_none());
        assert!(options.show_progress);
        assert!(options.use_cache);
    }

    // =========================================================================
    // make_snippet tests
    // =========================================================================

    #[test]
    fn make_snippet_limits_lines() {
        // GIVEN: Multi-line content
        let content = "line1\nline2\nline3\nline4\nline5\nline6";

        // WHEN: We create a snippet with max 3 lines
        let snippet = make_snippet(content, 3);

        // THEN: Should have only 3 lines
        assert_eq!(snippet, "line1\nline2\nline3");
    }

    #[test]
    fn make_snippet_handles_short_content() {
        // GIVEN: Content with fewer lines than limit
        let content = "line1\nline2";

        // WHEN: We create a snippet with max 5 lines
        let snippet = make_snippet(content, 5);

        // THEN: Should have all lines
        assert_eq!(snippet, "line1\nline2");
    }

    #[test]
    fn make_snippet_handles_empty_content() {
        // GIVEN: Empty content
        let content = "";

        // WHEN: We create a snippet
        let snippet = make_snippet(content, 5);

        // THEN: Should be empty
        assert_eq!(snippet, "");
    }

    // =========================================================================
    // Integration tests (require model download, marked #[ignore])
    // =========================================================================

    #[test]
    #[ignore = "Requires model download"]
    fn semantic_index_build_from_directory() {
        // GIVEN: A test directory with code files
        let temp_dir = tempfile::tempdir().unwrap();
        let test_file = temp_dir.path().join("test.py");
        std::fs::write(&test_file, "def foo():\n    pass\n").unwrap();

        // WHEN: We build an index
        let options = BuildOptions {
            show_progress: false,
            use_cache: false,
            ..Default::default()
        };
        let index = SemanticIndex::build(temp_dir.path(), options, None).unwrap();

        // THEN: Index should contain chunks
        assert!(!index.is_empty());
    }

    #[test]
    #[ignore = "Requires model download"]
    fn semantic_index_search_returns_ranked_results() {
        // GIVEN: An index with some code
        let temp_dir = tempfile::tempdir().unwrap();
        std::fs::write(
            temp_dir.path().join("config.py"),
            "def parse_config():\n    pass\n",
        )
        .unwrap();
        std::fs::write(
            temp_dir.path().join("loader.py"),
            "def load_data():\n    pass\n",
        )
        .unwrap();

        let options = BuildOptions {
            show_progress: false,
            use_cache: false,
            ..Default::default()
        };
        let mut index = SemanticIndex::build(temp_dir.path(), options, None).unwrap();

        // WHEN: We search for "parse configuration"
        let search_opts = SearchOptions::default();
        let report = index.search("parse configuration", &search_opts).unwrap();

        // THEN: Results should be ranked by score
        if report.results.len() >= 2 {
            assert!(report.results[0].score >= report.results[1].score);
        }
    }

    #[test]
    #[ignore = "Requires model download"]
    fn semantic_index_search_respects_top_k() {
        // GIVEN: An index with multiple chunks
        let temp_dir = tempfile::tempdir().unwrap();
        for i in 0..5 {
            std::fs::write(
                temp_dir.path().join(format!("file{}.py", i)),
                format!("def func{}():\n    pass\n", i),
            )
            .unwrap();
        }

        let options = BuildOptions {
            show_progress: false,
            use_cache: false,
            ..Default::default()
        };
        let mut index = SemanticIndex::build(temp_dir.path(), options, None).unwrap();

        // WHEN: We search with top_k = 2
        let search_opts = SearchOptions {
            top_k: 2,
            threshold: 0.0, // Accept all
            ..Default::default()
        };
        let report = index.search("function", &search_opts).unwrap();

        // THEN: Should return at most 2 results
        assert!(report.results.len() <= 2);
    }

    #[test]
    #[ignore = "Requires model download"]
    fn semantic_index_search_respects_threshold() {
        // GIVEN: An index
        let temp_dir = tempfile::tempdir().unwrap();
        std::fs::write(temp_dir.path().join("test.py"), "def foo():\n    pass\n").unwrap();

        let options = BuildOptions {
            show_progress: false,
            use_cache: false,
            ..Default::default()
        };
        let mut index = SemanticIndex::build(temp_dir.path(), options, None).unwrap();

        // WHEN: We search with a very high threshold
        let search_opts = SearchOptions {
            top_k: 10,
            threshold: 0.99, // Very high
            ..Default::default()
        };
        let report = index
            .search("completely unrelated query", &search_opts)
            .unwrap();

        // THEN: May return no results due to threshold
        // (We can't assert empty because embeddings might still be similar)
        assert!(report.results.iter().all(|r| r.score >= 0.99));
    }

    #[test]
    fn semantic_index_empty_returns_no_results() {
        // This test doesn't need the model since we're testing empty behavior
        // We can't easily create an empty index without the model, so skip
    }

    #[test]
    #[ignore = "Requires model download"]
    fn semantic_index_len_returns_chunk_count() {
        // GIVEN: An index with known number of files
        let temp_dir = tempfile::tempdir().unwrap();
        std::fs::write(temp_dir.path().join("a.py"), "def a():\n    pass\n").unwrap();
        std::fs::write(temp_dir.path().join("b.py"), "def b():\n    pass\n").unwrap();

        let options = BuildOptions {
            show_progress: false,
            use_cache: false,
            ..Default::default()
        };
        let index = SemanticIndex::build(temp_dir.path(), options, None).unwrap();

        // THEN: len() should return chunk count
        assert!(index.len() >= 2); // At least 2 functions
    }

    #[test]
    #[ignore = "Requires model download"]
    fn semantic_index_build_uses_batch_embedding() {
        // GIVEN: A directory with multiple code files (tests batch path)
        let temp_dir = tempfile::tempdir().unwrap();
        for i in 0..10 {
            std::fs::write(
                temp_dir.path().join(format!("mod{}.py", i)),
                format!("def func_{}(x):\n    return x + {}\n", i, i),
            )
            .unwrap();
        }

        // WHEN: We build an index (should use batch embedding internally)
        let options = BuildOptions {
            show_progress: false,
            use_cache: false,
            ..Default::default()
        };
        let index = SemanticIndex::build(temp_dir.path(), options, None).unwrap();

        // THEN: All chunks should have embeddings with correct dimensions
        assert!(
            index.len() >= 10,
            "Expected at least 10 chunks, got {}",
            index.len()
        );
        for chunk in index.chunks() {
            assert_eq!(
                chunk.embedding.len(),
                768,
                "Each chunk should have 768-dim embedding"
            );
            // Verify embedding is normalized
            let norm: f32 = chunk.embedding.iter().map(|x| x * x).sum::<f32>().sqrt();
            assert!(
                (norm - 1.0).abs() < 1e-4,
                "Embedding should be normalized, got norm={}",
                norm
            );
        }
    }

    #[test]
    #[ignore = "Requires model download"]
    fn semantic_index_build_batch_matches_sequential() {
        // GIVEN: Same files, build with batch (the new default) and verify
        // results are consistent (same chunks produce same search rankings)
        let temp_dir = tempfile::tempdir().unwrap();
        std::fs::write(
            temp_dir.path().join("parser.py"),
            "def parse_config(path):\n    with open(path) as f:\n        return f.read()\n",
        )
        .unwrap();
        std::fs::write(
            temp_dir.path().join("loader.py"),
            "def load_data(file):\n    return read(file)\n",
        )
        .unwrap();
        std::fs::write(
            temp_dir.path().join("math.py"),
            "def add_numbers(a, b):\n    return a + b\n",
        )
        .unwrap();

        let options = BuildOptions {
            show_progress: false,
            use_cache: false,
            ..Default::default()
        };
        let mut index = SemanticIndex::build(temp_dir.path(), options, None).unwrap();

        // WHEN: We search for "parse configuration"
        let search_opts = SearchOptions {
            top_k: 3,
            threshold: 0.0,
            ..Default::default()
        };
        let report = index.search("parse configuration", &search_opts).unwrap();

        // THEN: parse_config should rank higher than add_numbers
        assert!(!report.results.is_empty(), "Should have results");
        // The parser function should score higher for "parse configuration"
        let parser_result = report
            .results
            .iter()
            .find(|r| r.function_name.as_deref() == Some("parse_config"));
        let math_result = report
            .results
            .iter()
            .find(|r| r.function_name.as_deref() == Some("add_numbers"));
        if let (Some(p), Some(m)) = (parser_result, math_result) {
            assert!(
                p.score > m.score,
                "parse_config ({}) should score higher than add_numbers ({}) for 'parse configuration'",
                p.score,
                m.score
            );
        }
    }

    #[test]
    #[ignore = "Requires model download"]
    fn semantic_index_find_similar() {
        // GIVEN: An index with similar functions
        let temp_dir = tempfile::tempdir().unwrap();
        std::fs::write(
            temp_dir.path().join("config.py"),
            "def parse_config(path):\n    return read(path)\n",
        )
        .unwrap();
        std::fs::write(
            temp_dir.path().join("settings.py"),
            "def load_settings(file):\n    return read(file)\n",
        )
        .unwrap();
        std::fs::write(
            temp_dir.path().join("unrelated.py"),
            "def calculate_sum(a, b):\n    return a + b\n",
        )
        .unwrap();

        let options = BuildOptions {
            show_progress: false,
            use_cache: false,
            ..Default::default()
        };
        let index = SemanticIndex::build(temp_dir.path(), options, None).unwrap();

        // WHEN: We find similar to parse_config
        let search_opts = SearchOptions {
            top_k: 5,
            threshold: 0.0,
            ..Default::default()
        };
        let report = index
            .find_similar("config.py", Some("parse_config"), &search_opts)
            .unwrap();

        // THEN: Should exclude self and find similar code
        assert!(report.exclude_self);
        assert!(!report.similar.iter().any(|r| {
            r.file_path.to_string_lossy() == "config.py"
                && r.function_name.as_deref() == Some("parse_config")
        }));
    }
}