Skip to main content

symforge/live_index/
search.rs

1use std::collections::{HashMap, HashSet};
2use std::path::{Path, PathBuf};
3
4use globset::{GlobBuilder, GlobMatcher};
5
6use crate::domain::{FileClass, FileClassification, LanguageId, SymbolKind};
7use crate::live_index::LiveIndex;
8use crate::live_index::query::{SearchFilesHit, SearchFilesTier};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
11pub enum SymbolMatchTier {
12    Exact = 0,
13    Prefix = 1,
14    Substring = 2,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct SymbolSearchHit {
19    pub tier: SymbolMatchTier,
20    pub name: String,
21    pub path: String,
22    pub kind: String,
23    pub line: u32,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct SymbolSearchResult {
28    pub file_count: usize,
29    pub hits: Vec<SymbolSearchHit>,
30    pub overflow_count: usize,
31}
32
33#[derive(Debug, Clone, PartialEq, Eq, Default)]
34pub enum PathScope {
35    #[default]
36    Any,
37    Exact(String),
38    Prefix(String),
39}
40
41impl PathScope {
42    pub const fn any() -> Self {
43        Self::Any
44    }
45
46    pub fn exact(path: impl Into<String>) -> Self {
47        Self::Exact(path.into())
48    }
49
50    pub fn prefix(path_prefix: impl Into<String>) -> Self {
51        Self::Prefix(path_prefix.into())
52    }
53
54    pub fn matches(&self, path: &str) -> bool {
55        match self {
56            Self::Any => true,
57            Self::Exact(exact_path) => path == exact_path,
58            Self::Prefix(path_prefix) => {
59                let normalized_prefix = path_prefix.trim_end_matches('/');
60                normalized_prefix.is_empty()
61                    || path == normalized_prefix
62                    || path
63                        .strip_prefix(normalized_prefix)
64                        .is_some_and(|suffix| suffix.starts_with('/'))
65            }
66        }
67    }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
71pub enum SearchScope {
72    All,
73    #[default]
74    Code,
75    Text,
76    Binary,
77}
78
79impl SearchScope {
80    pub const fn allows(self, classification: &FileClassification) -> bool {
81        match self {
82            Self::All => true,
83            Self::Code => matches!(classification.class, FileClass::Code),
84            Self::Text => matches!(classification.class, FileClass::Text),
85            Self::Binary => matches!(classification.class, FileClass::Binary),
86        }
87    }
88}
89
90#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91pub struct ResultLimit(usize);
92
93impl ResultLimit {
94    pub const fn new(limit: usize) -> Self {
95        Self(limit)
96    }
97
98    pub const fn symbol_search_default() -> Self {
99        Self(50)
100    }
101
102    pub const fn get(self) -> usize {
103        self.0
104    }
105}
106
107impl Default for ResultLimit {
108    fn default() -> Self {
109        Self::symbol_search_default()
110    }
111}
112
113#[derive(Debug, Clone, PartialEq, Eq)]
114pub struct ContentContext {
115    pub start_line: Option<u32>,
116    pub end_line: Option<u32>,
117    pub around_line: Option<u32>,
118    pub around_match: Option<String>,
119    pub match_occurrence: Option<u32>,
120    pub around_symbol: Option<String>,
121    pub symbol_line: Option<u32>,
122    pub context_lines: Option<u32>,
123    pub chunk_index: Option<u32>,
124    pub max_lines: Option<u32>,
125    pub show_line_numbers: bool,
126    pub header: bool,
127    /// The mode used for content selection (e.g. "lines", "symbol", "match", "chunk").
128    pub mode_name: Option<String>,
129    /// Whether the mode was explicitly specified by the caller or inferred from flags.
130    pub mode_explicit: bool,
131}
132
133impl ContentContext {
134    pub fn line_range(start_line: Option<u32>, end_line: Option<u32>) -> Self {
135        Self::line_range_with_format(start_line, end_line, false, false)
136    }
137
138    pub fn line_range_with_format(
139        start_line: Option<u32>,
140        end_line: Option<u32>,
141        show_line_numbers: bool,
142        header: bool,
143    ) -> Self {
144        Self {
145            start_line,
146            end_line,
147            around_line: None,
148            around_match: None,
149            match_occurrence: None,
150            around_symbol: None,
151            symbol_line: None,
152            context_lines: None,
153            chunk_index: None,
154            max_lines: None,
155            show_line_numbers,
156            header,
157            mode_name: None,
158            mode_explicit: false,
159        }
160    }
161
162    pub fn around_line(
163        around_line: u32,
164        context_lines: Option<u32>,
165        show_line_numbers: bool,
166        header: bool,
167    ) -> Self {
168        Self {
169            start_line: None,
170            end_line: None,
171            around_line: Some(around_line),
172            around_match: None,
173            match_occurrence: None,
174            around_symbol: None,
175            symbol_line: None,
176            context_lines,
177            chunk_index: None,
178            max_lines: None,
179            show_line_numbers,
180            header,
181            mode_name: None,
182            mode_explicit: false,
183        }
184    }
185
186    pub fn around_match(
187        around_match: impl Into<String>,
188        context_lines: Option<u32>,
189        show_line_numbers: bool,
190        header: bool,
191    ) -> Self {
192        Self::around_match_occurrence(around_match, None, context_lines, show_line_numbers, header)
193    }
194
195    pub fn around_match_occurrence(
196        around_match: impl Into<String>,
197        match_occurrence: Option<u32>,
198        context_lines: Option<u32>,
199        show_line_numbers: bool,
200        header: bool,
201    ) -> Self {
202        Self {
203            start_line: None,
204            end_line: None,
205            around_line: None,
206            around_match: Some(around_match.into()),
207            match_occurrence,
208            around_symbol: None,
209            symbol_line: None,
210            context_lines,
211            chunk_index: None,
212            max_lines: None,
213            show_line_numbers,
214            header,
215            mode_name: None,
216            mode_explicit: false,
217        }
218    }
219
220    pub fn around_symbol(
221        around_symbol: impl Into<String>,
222        symbol_line: Option<u32>,
223        context_lines: Option<u32>,
224    ) -> Self {
225        Self::around_symbol_with_max_lines(
226            around_symbol,
227            symbol_line,
228            context_lines,
229            None,
230            false,
231            false,
232        )
233    }
234
235    pub fn around_symbol_with_max_lines(
236        around_symbol: impl Into<String>,
237        symbol_line: Option<u32>,
238        context_lines: Option<u32>,
239        max_lines: Option<u32>,
240        show_line_numbers: bool,
241        header: bool,
242    ) -> Self {
243        Self {
244            start_line: None,
245            end_line: None,
246            around_line: None,
247            around_match: None,
248            match_occurrence: None,
249            around_symbol: Some(around_symbol.into()),
250            symbol_line,
251            context_lines,
252            chunk_index: None,
253            max_lines,
254            show_line_numbers,
255            header,
256            mode_name: None,
257            mode_explicit: false,
258        }
259    }
260
261    pub fn chunk(chunk_index: u32, max_lines: u32) -> Self {
262        Self {
263            start_line: None,
264            end_line: None,
265            around_line: None,
266            around_match: None,
267            match_occurrence: None,
268            around_symbol: None,
269            symbol_line: None,
270            context_lines: None,
271            chunk_index: Some(chunk_index),
272            max_lines: Some(max_lines),
273            show_line_numbers: false,
274            header: false,
275            mode_name: None,
276            mode_explicit: false,
277        }
278    }
279}
280
281/// Semantic noise classification for files matched by gitignore or path heuristics.
282///
283/// This is **suppressive** — files are down-ranked/tagged in explore and repo_map,
284/// but remain visible to search_text, search_symbols, and get_file_context.
285#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
286pub enum NoiseClass {
287    /// Not noise — normal project source.
288    #[default]
289    None,
290    /// Third-party / vendored dependency (vendor/, node_modules/, etc.).
291    Vendor,
292    /// Machine-generated output (.lock, .min.js, /dist/, etc.).
293    Generated,
294    /// Matched by .gitignore but not classified as vendor or generated.
295    Ignored,
296}
297
298impl NoiseClass {
299    /// Human-readable tag for file tree rendering (empty string for None).
300    pub fn tag(self) -> &'static str {
301        match self {
302            NoiseClass::None => "",
303            NoiseClass::Vendor => "[vendor]",
304            NoiseClass::Generated => "[generated]",
305            NoiseClass::Ignored => "[ignored]",
306        }
307    }
308}
309
310#[derive(Debug, Clone, Copy, PartialEq, Eq)]
311pub struct NoisePolicy {
312    pub include_generated: bool,
313    pub include_tests: bool,
314    pub include_vendor: bool,
315    pub include_ignored: bool,
316}
317
318impl NoisePolicy {
319    pub const fn permissive() -> Self {
320        Self {
321            include_generated: true,
322            include_tests: true,
323            include_vendor: true,
324            include_ignored: true,
325        }
326    }
327
328    pub const fn hide_classified_noise() -> Self {
329        Self {
330            include_generated: false,
331            include_tests: false,
332            include_vendor: false,
333            include_ignored: false,
334        }
335    }
336
337    pub const fn allows(self, classification: &FileClassification) -> bool {
338        (self.include_generated || !classification.is_generated)
339            && (self.include_tests || !classification.is_test)
340            && (self.include_vendor || !classification.is_vendor)
341    }
342
343    /// Classify a file path into a `NoiseClass` using heuristic rules first,
344    /// then falling back to gitignore patterns.
345    ///
346    /// Heuristic priority:
347    /// 1. Vendor directories: `vendor/`, `node_modules/`, `third_party/`, etc.
348    /// 2. Generated artifacts: `.lock`, `.min.js`, `.min.css`, `/dist/`, `/generated/`, etc.
349    /// 3. Gitignore catch-all: matched by .gitignore but not heuristic → `Ignored`
350    pub fn classify_path(
351        path: &str,
352        gitignore: Option<&ignore::gitignore::Gitignore>,
353    ) -> NoiseClass {
354        let lower = path.replace('\\', "/").to_ascii_lowercase();
355        let segments: Vec<&str> = lower.split('/').filter(|s| !s.is_empty()).collect();
356        let basename = segments.last().copied().unwrap_or("");
357
358        // 1. Vendor heuristic. Shares VENDOR_PATH_SEGMENTS with
359        // FileClassification::for_code_path so search_symbols/search_text agree
360        // with explore/repo_map/search_files on what counts as vendored
361        // (SF-STRESS-011).
362        let is_vendor = segments
363            .iter()
364            .any(|s| crate::domain::index::VENDOR_PATH_SEGMENTS.contains(s));
365        if is_vendor {
366            return NoiseClass::Vendor;
367        }
368
369        // 2. Generated heuristic. Directory roots share GENERATED_PATH_SEGMENTS
370        // with FileClassification; the basename suffixes below additionally cover
371        // lockfiles and source maps that are not directory-keyed.
372        let is_generated = segments
373            .iter()
374            .any(|s| crate::domain::index::GENERATED_PATH_SEGMENTS.contains(s))
375            || basename.ends_with(".lock")
376            || basename.contains("-lock.")
377            || basename.ends_with(".min.js")
378            || basename.ends_with(".min.css")
379            || basename.ends_with(".map")
380            || basename.contains(".generated.")
381            || basename.contains(".gen.")
382            || basename.ends_with(".g.dart")
383            || basename.ends_with(".pb.go")
384            || basename.ends_with(".designer.cs");
385        if is_generated {
386            return NoiseClass::Generated;
387        }
388
389        // 3. Gitignore catch-all
390        // The `ignore` crate asserts that paths are relative (!path.has_root()).
391        // Guard against absolute paths reaching here (e.g., from watcher events
392        // or concurrent agents passing unsanitized paths).
393        if let Some(gi) = gitignore {
394            let p = std::path::Path::new(path);
395            if !p.has_root() {
396                let matched = gi.matched_path_or_any_parents(path, false);
397                if matched.is_ignore() {
398                    return NoiseClass::Ignored;
399                }
400            }
401        }
402
403        NoiseClass::None
404    }
405
406    /// Whether a file with the given noise class should be hidden in suppressive views.
407    pub fn should_hide(self, class: NoiseClass) -> bool {
408        match class {
409            NoiseClass::None => false,
410            NoiseClass::Vendor => !self.include_vendor,
411            NoiseClass::Generated => !self.include_generated,
412            NoiseClass::Ignored => !self.include_ignored,
413        }
414    }
415}
416
417impl Default for NoisePolicy {
418    fn default() -> Self {
419        Self::permissive()
420    }
421}
422
423#[derive(Debug, Clone, PartialEq, Eq, Default)]
424pub struct SymbolSearchOptions {
425    pub path_scope: PathScope,
426    pub search_scope: SearchScope,
427    pub result_limit: ResultLimit,
428    pub noise_policy: NoisePolicy,
429    pub include_personal_tooling: bool,
430    pub language_filter: Option<LanguageId>,
431}
432
433impl SymbolSearchOptions {
434    pub fn for_current_code_search(result_limit: usize) -> Self {
435        Self {
436            path_scope: PathScope::any(),
437            search_scope: SearchScope::Code,
438            result_limit: ResultLimit::new(result_limit),
439            noise_policy: NoisePolicy {
440                include_generated: false,
441                include_tests: false,
442                include_vendor: true,
443                include_ignored: false,
444            },
445            include_personal_tooling: true,
446            language_filter: None,
447        }
448    }
449}
450
451#[derive(Debug, Clone, PartialEq)]
452pub struct TextSearchOptions {
453    pub path_scope: PathScope,
454    pub search_scope: SearchScope,
455    pub noise_policy: NoisePolicy,
456    pub include_personal_tooling: bool,
457    pub language_filter: Option<LanguageId>,
458    pub total_limit: usize,
459    pub max_per_file: usize,
460    pub glob: Option<String>,
461    pub exclude_glob: Option<String>,
462    pub context: Option<usize>,
463    pub case_sensitive: Option<bool>,
464    pub whole_word: bool,
465    pub ranked: bool,
466    /// Optional pre-computed churn scores keyed by relative file path.
467    /// When `Some`, the ranking code uses these instead of the default `0.0`.
468    /// Populated by the protocol handler from `GitTemporalIndex` when available.
469    pub churn_scores: Option<HashMap<String, f32>>,
470}
471
472impl Default for TextSearchOptions {
473    fn default() -> Self {
474        Self {
475            path_scope: PathScope::default(),
476            search_scope: SearchScope::default(),
477            noise_policy: NoisePolicy::default(),
478            include_personal_tooling: true,
479            language_filter: None,
480            total_limit: 50,
481            max_per_file: 5,
482            glob: None,
483            exclude_glob: None,
484            context: None,
485            case_sensitive: None,
486            whole_word: false,
487            ranked: false,
488            churn_scores: None,
489        }
490    }
491}
492
493impl TextSearchOptions {
494    pub fn for_current_code_search() -> Self {
495        Self {
496            path_scope: PathScope::any(),
497            search_scope: SearchScope::Code,
498            noise_policy: NoisePolicy {
499                include_generated: false,
500                include_tests: false,
501                include_vendor: true,
502                include_ignored: false,
503            },
504            include_personal_tooling: true,
505            language_filter: None,
506            total_limit: 50,
507            max_per_file: 5,
508            glob: None,
509            exclude_glob: None,
510            context: None,
511            case_sensitive: None,
512            whole_word: false,
513            ranked: false,
514            churn_scores: None,
515        }
516    }
517}
518
519#[derive(Debug, Clone, PartialEq, Eq)]
520pub struct FileContentOptions {
521    pub path_scope: PathScope,
522    pub content_context: ContentContext,
523}
524
525impl FileContentOptions {
526    pub fn for_explicit_path_read(
527        path: impl Into<String>,
528        start_line: Option<u32>,
529        end_line: Option<u32>,
530    ) -> Self {
531        Self::for_explicit_path_read_with_format(path, start_line, end_line, false, false)
532    }
533
534    pub fn for_explicit_path_read_with_format(
535        path: impl Into<String>,
536        start_line: Option<u32>,
537        end_line: Option<u32>,
538        show_line_numbers: bool,
539        header: bool,
540    ) -> Self {
541        Self {
542            path_scope: PathScope::exact(path),
543            content_context: ContentContext::line_range_with_format(
544                start_line,
545                end_line,
546                show_line_numbers,
547                header,
548            ),
549        }
550    }
551
552    pub fn for_explicit_path_read_around_line(
553        path: impl Into<String>,
554        around_line: u32,
555        context_lines: Option<u32>,
556        show_line_numbers: bool,
557        header: bool,
558    ) -> Self {
559        Self {
560            path_scope: PathScope::exact(path),
561            content_context: ContentContext::around_line(
562                around_line,
563                context_lines,
564                show_line_numbers,
565                header,
566            ),
567        }
568    }
569
570    pub fn for_explicit_path_read_around_match(
571        path: impl Into<String>,
572        around_match: impl Into<String>,
573        match_occurrence: Option<u32>,
574        context_lines: Option<u32>,
575        show_line_numbers: bool,
576        header: bool,
577    ) -> Self {
578        Self {
579            path_scope: PathScope::exact(path),
580            content_context: ContentContext::around_match_occurrence(
581                around_match,
582                match_occurrence,
583                context_lines,
584                show_line_numbers,
585                header,
586            ),
587        }
588    }
589
590    pub fn for_explicit_path_read_chunk(
591        path: impl Into<String>,
592        chunk_index: u32,
593        max_lines: u32,
594    ) -> Self {
595        Self {
596            path_scope: PathScope::exact(path),
597            content_context: ContentContext::chunk(chunk_index, max_lines),
598        }
599    }
600
601    pub fn for_explicit_path_read_around_symbol(
602        path: impl Into<String>,
603        around_symbol: impl Into<String>,
604        symbol_line: Option<u32>,
605        context_lines: Option<u32>,
606        max_lines: Option<u32>,
607        show_line_numbers: bool,
608        header: bool,
609    ) -> Self {
610        Self {
611            path_scope: PathScope::exact(path),
612            content_context: ContentContext::around_symbol_with_max_lines(
613                around_symbol,
614                symbol_line,
615                context_lines,
616                max_lines,
617                show_line_numbers,
618                header,
619            ),
620        }
621    }
622}
623
624#[derive(Debug, Clone, PartialEq, Eq)]
625pub struct EnclosingMatchSymbol {
626    pub name: String,
627    pub kind: String,
628    pub line_range: (u32, u32),
629}
630
631#[derive(Debug, Clone, PartialEq, Eq)]
632pub struct TextLineMatch {
633    pub line_number: usize,
634    pub line: String,
635    pub enclosing_symbol: Option<EnclosingMatchSymbol>,
636}
637
638#[derive(Debug, Clone, PartialEq, Eq)]
639pub struct TextRenderedLine {
640    pub line_number: usize,
641    pub line: String,
642    pub is_match: bool,
643}
644
645#[derive(Debug, Clone, PartialEq, Eq)]
646pub enum TextDisplayLine {
647    Separator,
648    Line(TextRenderedLine),
649}
650
651#[derive(Debug, Clone, PartialEq, Eq)]
652pub struct CallerEntry {
653    pub file: String,
654    pub symbol: String,
655    pub line: u32,
656}
657
658#[derive(Debug, Clone, PartialEq, Eq)]
659pub struct TextFileMatches {
660    pub path: String,
661    pub matches: Vec<TextLineMatch>,
662    pub rendered_lines: Option<Vec<TextDisplayLine>>,
663    pub callers: Option<Vec<CallerEntry>>,
664}
665
666#[derive(Debug, Clone, PartialEq, Eq)]
667pub struct TextSearchResult {
668    pub label: String,
669    pub total_matches: usize,
670    pub files: Vec<TextFileMatches>,
671    /// Matches that were found but suppressed by noise policy (e.g., inside test modules).
672    pub suppressed_by_noise: usize,
673    pub overflow_count: usize,
674}
675
676pub const SUPPRESSED_TEXT_MATCH_DISPLAY_CAP: usize = 100;
677
678/// Hard per-line character cap applied at capture time to every source line
679/// stored for text-search output. A single committed minified line can be
680/// hundreds of kilobytes; emitting it verbatim detonates the tool result and
681/// can blow an agent's entire context budget (SF-STRESS-008). Capping at
682/// capture time means every downstream renderer (default/symbol/usage modes,
683/// context windows) inherits the bound for free.
684pub const MAX_DISPLAY_LINE_CHARS: usize = 2000;
685
686/// The default per-file visible-match cap shared by `for_current_code_search`
687/// (both the symbol and text option presets). The overlay post-filter
688/// (`live_index::view`) reuses this so an upserted dirty file is bounded
689/// identically to a base file.
690pub(super) const DEFAULT_MAX_PER_FILE: usize = 5;
691
692/// Truncate a source line to a bounded, char-boundary-safe head excerpt with an
693/// honest marker when it exceeds [`MAX_DISPLAY_LINE_CHARS`].
694///
695/// Slicing is done over `char_indices` so multibyte UTF-8 (e.g. U+2028 in
696/// minified bundles) never causes a byte-boundary panic. Lines at or under the
697/// cap are returned unchanged. The marker reports the number of characters
698/// omitted so the result stays honest about what was elided.
699///
700/// `pub(super)` so the overlay post-filter (`live_index::view`) applies the
701/// SAME per-line cap as the base search instead of emitting raw lines.
702pub(super) fn truncate_display_line(line: &str) -> String {
703    // Fast path: most lines are short. `len()` (bytes) is a cheap upper bound on
704    // char count, so a line whose byte length fits the cap cannot exceed it in
705    // chars and needs no scan.
706    if line.len() <= MAX_DISPLAY_LINE_CHARS {
707        return line.to_string();
708    }
709
710    let total_chars = line.chars().count();
711    if total_chars <= MAX_DISPLAY_LINE_CHARS {
712        return line.to_string();
713    }
714
715    // Take a char-boundary-safe head excerpt of the first MAX_DISPLAY_LINE_CHARS
716    // characters. nth(n) yields the byte offset of the (n+1)-th char start.
717    let cut_byte = line
718        .char_indices()
719        .nth(MAX_DISPLAY_LINE_CHARS)
720        .map(|(byte_idx, _)| byte_idx)
721        .unwrap_or(line.len());
722    let omitted = total_chars - MAX_DISPLAY_LINE_CHARS;
723    format!(
724        "{}... [line truncated, {omitted} chars omitted]",
725        &line[..cut_byte]
726    )
727}
728
729#[derive(Debug, Clone, PartialEq, Eq)]
730pub enum TextSearchError {
731    EmptyRegexQuery,
732    EmptyQueryOrTerms,
733    InvalidRegex {
734        pattern: String,
735        error: String,
736    },
737    InvalidGlob {
738        field: &'static str,
739        pattern: String,
740        error: String,
741    },
742    UnsupportedWholeWordRegex,
743    InvalidStructuralPattern {
744        pattern: String,
745        error: String,
746    },
747    UnsupportedStructuralLanguage {
748        pattern: String,
749        sample_error: String,
750    },
751}
752
753struct CompiledTextGlobFilters {
754    include: Option<GlobMatcher>,
755    exclude: Option<GlobMatcher>,
756}
757
758impl CompiledTextGlobFilters {
759    fn matches(&self, path: &str) -> bool {
760        self.include
761            .as_ref()
762            .is_none_or(|include| include.is_match(path))
763            && self
764                .exclude
765                .as_ref()
766                .is_none_or(|exclude| !exclude.is_match(path))
767    }
768}
769
770struct ScoredSymbolMatch {
771    tier: SymbolMatchTier,
772    tiebreak: u32,
773    name: String,
774    path: String,
775    kind: String,
776    line: u32,
777}
778
779/// Returns true when `filter` matches the given `kind`, accepting both the
780/// Display string (e.g. `"let"` for `Variable`, `"fn"` for `Function`) and
781/// common semantic aliases (e.g. `"variable"`, `"function"`, `"method"`).
782fn kind_filter_matches(filter: &str, kind: &crate::domain::SymbolKind) -> bool {
783    if kind.to_string().eq_ignore_ascii_case(filter) {
784        return true;
785    }
786    // Semantic aliases: users naturally write "variable", "function", "method"
787    // even though the display strings are "let", "fn", "fn".
788    matches!(
789        (filter.to_ascii_lowercase().as_str(), kind),
790        ("variable", crate::domain::SymbolKind::Variable)
791            | ("function", crate::domain::SymbolKind::Function)
792            | ("method", crate::domain::SymbolKind::Method)
793            | ("module", crate::domain::SymbolKind::Module)
794            | ("constant", crate::domain::SymbolKind::Constant)
795    )
796}
797
798pub fn search_symbols(
799    index: &LiveIndex,
800    query: &str,
801    kind_filter: Option<&str>,
802    result_limit: usize,
803) -> SymbolSearchResult {
804    let options = SymbolSearchOptions::for_current_code_search(result_limit);
805    search_symbols_with_options(index, query, kind_filter, &options)
806}
807
808pub fn search_symbols_with_options(
809    index: &LiveIndex,
810    query: &str,
811    kind_filter: Option<&str>,
812    options: &SymbolSearchOptions,
813) -> SymbolSearchResult {
814    let query_lower = query.to_lowercase();
815    let mut matches: Vec<ScoredSymbolMatch> = Vec::new();
816
817    let mut paths: Vec<&String> = index.all_files().map(|(path, _)| path).collect();
818    paths.sort();
819
820    for path in paths {
821        let file = index
822            .get_file(path)
823            .expect("path from all_files must exist");
824        if !options.path_scope.matches(path)
825            || !options.search_scope.allows(&file.classification)
826            || !options.noise_policy.allows(&file.classification)
827            || (!options.include_personal_tooling
828                && crate::live_index::query::is_personal_tooling_path(path))
829            || options
830                .language_filter
831                .as_ref()
832                .is_some_and(|language| &file.language != language)
833        {
834            continue;
835        }
836        // Precompute test module byte ranges so we can skip symbols inside
837        // inline `mod tests` blocks even when the file itself is not a test file.
838        let test_module_ranges: Vec<(u32, u32)> =
839            if !options.noise_policy.include_tests && !file.classification.is_test {
840                file.symbols
841                    .iter()
842                    .filter(|s| {
843                        s.kind == crate::domain::SymbolKind::Module
844                            && matches!(s.name.as_str(), "tests" | "test")
845                    })
846                    .map(|s| s.byte_range)
847                    .collect()
848            } else {
849                vec![]
850            };
851
852        for sym in &file.symbols {
853            // Skip symbols inside inline test modules (e.g. `mod tests { struct T; }`)
854            if !test_module_ranges.is_empty()
855                && test_module_ranges
856                    .iter()
857                    .any(|&(start, end)| sym.byte_range.0 >= start && sym.byte_range.1 <= end)
858            {
859                continue;
860            }
861
862            if let Some(filter) = kind_filter
863                && !filter.eq_ignore_ascii_case("all")
864                && !kind_filter_matches(filter, &sym.kind)
865            {
866                continue;
867            }
868
869            let name_lower = sym.name.to_lowercase();
870            if !name_lower.contains(&query_lower) {
871                continue;
872            }
873
874            let (tier, tiebreak) = if name_lower == query_lower {
875                (SymbolMatchTier::Exact, 0u32)
876            } else if name_lower.starts_with(&query_lower) {
877                (SymbolMatchTier::Prefix, sym.name.len() as u32)
878            } else {
879                let pos = name_lower.find(&query_lower).unwrap_or(0) as u32;
880                (SymbolMatchTier::Substring, pos)
881            };
882
883            matches.push(ScoredSymbolMatch {
884                tier,
885                tiebreak,
886                name: sym.name.clone(),
887                path: path.clone(),
888                kind: sym.kind.to_string(),
889                line: sym.line_range.0 + 1,
890            });
891        }
892    }
893
894    matches.sort_by(|a, b| {
895        a.tier
896            .cmp(&b.tier)
897            .then(a.tiebreak.cmp(&b.tiebreak))
898            .then(a.name.cmp(&b.name))
899    });
900
901    let total_matches = matches.len();
902    let overflow_count = total_matches.saturating_sub(options.result_limit.get());
903    let hits: Vec<SymbolSearchHit> = matches
904        .into_iter()
905        .take(options.result_limit.get())
906        .map(|m| SymbolSearchHit {
907            tier: m.tier,
908            name: m.name,
909            path: m.path,
910            kind: m.kind,
911            line: m.line,
912        })
913        .collect();
914
915    let file_count = hits
916        .iter()
917        .map(|h| h.path.as_str())
918        .collect::<std::collections::HashSet<_>>()
919        .len();
920
921    SymbolSearchResult {
922        file_count,
923        hits,
924        overflow_count,
925    }
926}
927
928pub fn search_text(
929    index: &LiveIndex,
930    query: Option<&str>,
931    terms: Option<&[String]>,
932    regex: bool,
933) -> Result<TextSearchResult, TextSearchError> {
934    search_text_with_options(
935        index,
936        query,
937        terms,
938        regex,
939        &TextSearchOptions::for_current_code_search(),
940    )
941}
942
943pub fn search_text_with_options(
944    index: &LiveIndex,
945    query: Option<&str>,
946    terms: Option<&[String]>,
947    regex: bool,
948    options: &TextSearchOptions,
949) -> Result<TextSearchResult, TextSearchError> {
950    let compiled_globs = compile_text_glob_filters(options)?;
951    let case_sensitive = options.case_sensitive.unwrap_or(regex);
952    let normalized_terms: Vec<String> = match terms {
953        Some(raw_terms) if !raw_terms.is_empty() => raw_terms
954            .iter()
955            .map(|term| term.trim())
956            .filter(|term| !term.is_empty())
957            .map(ToOwned::to_owned)
958            .collect(),
959        _ => query
960            .map(str::trim)
961            .filter(|text| !text.is_empty())
962            .map(|text| vec![text.to_string()])
963            .unwrap_or_default(),
964    };
965
966    if regex {
967        if options.whole_word {
968            return Err(TextSearchError::UnsupportedWholeWordRegex);
969        }
970        let pattern = query
971            .map(str::trim)
972            .filter(|text| !text.is_empty())
973            .unwrap_or("");
974        if pattern.is_empty() {
975            return Err(TextSearchError::EmptyRegexQuery);
976        }
977
978        let regex = match regex::RegexBuilder::new(pattern)
979            .case_insensitive(!case_sensitive)
980            .build()
981        {
982            Ok(regex) => regex,
983            Err(error) => {
984                return Err(TextSearchError::InvalidRegex {
985                    pattern: pattern.to_string(),
986                    error: error.to_string(),
987                });
988            }
989        };
990
991        let mut candidate_paths = Vec::new();
992        let mut suppressed_candidate_paths = Vec::new();
993        for (path, file) in index.all_files() {
994            if !file_matches_text_base_scope(path, file, options, &compiled_globs) {
995                continue;
996            }
997            if file_hidden_by_search_policy(path, file, options) {
998                suppressed_candidate_paths.push(path.clone());
999            } else {
1000                candidate_paths.push(path.clone());
1001            }
1002        }
1003        let label = format!("regex '{pattern}'");
1004        let mut result = collect_text_matches(
1005            index,
1006            candidate_paths,
1007            |line| regex.is_match(line),
1008            label.clone(),
1009            options,
1010        );
1011        result.suppressed_by_noise =
1012            result
1013                .suppressed_by_noise
1014                .saturating_add(count_suppressed_text_matches(
1015                    index,
1016                    suppressed_candidate_paths,
1017                    |line| regex.is_match(line),
1018                    label,
1019                    options,
1020                ));
1021        return Ok(result);
1022    }
1023
1024    if normalized_terms.is_empty() {
1025        return Err(TextSearchError::EmptyQueryOrTerms);
1026    }
1027
1028    let mut candidate_paths = HashSet::new();
1029    let mut suppressed_candidate_paths = HashSet::new();
1030    for term in &normalized_terms {
1031        for path in index.trigram_index.search(term.as_bytes(), &index.files) {
1032            let Some(file) = index.get_file(&path) else {
1033                continue;
1034            };
1035            if !file_matches_text_base_scope(&path, file, options, &compiled_globs) {
1036                continue;
1037            }
1038            if file_hidden_by_search_policy(&path, file, options) {
1039                suppressed_candidate_paths.insert(path);
1040            } else {
1041                candidate_paths.insert(path);
1042            }
1043        }
1044    }
1045
1046    let lowered_terms = (!case_sensitive).then(|| {
1047        normalized_terms
1048            .iter()
1049            .map(|term| term.to_lowercase())
1050            .collect::<Vec<_>>()
1051    });
1052    let whole_word_matcher = options
1053        .whole_word
1054        .then(|| compile_literal_whole_word_matcher(&normalized_terms, case_sensitive));
1055
1056    // Aho-Corasick automaton for multi-term OR searches: matches all patterns in
1057    // a single pass over each line, eliminating per-line .to_lowercase() allocation
1058    // for case-insensitive searches.  Only used when: >1 term, not whole-word mode,
1059    // and all terms are ASCII (aho-corasick only does ASCII case folding).
1060    let all_ascii = normalized_terms.iter().all(|t| t.is_ascii());
1061    let multi_term_ac =
1062        (normalized_terms.len() > 1 && !options.whole_word && all_ascii).then(|| {
1063            aho_corasick::AhoCorasick::builder()
1064                .ascii_case_insensitive(!case_sensitive)
1065                .build(&normalized_terms)
1066                .expect("literal terms always build")
1067        });
1068
1069    let label = if normalized_terms.len() == 1 {
1070        format!("'{}'", normalized_terms[0])
1071    } else {
1072        format!("terms [{}]", normalized_terms.join(", "))
1073    };
1074
1075    // When multiple OR terms are provided, scale up the total_limit so each term
1076    // gets a fair share of results.  Without this, high-frequency terms dominate
1077    // the ranked file list and exhaust the limit before rarer terms appear.
1078    let effective_options;
1079    let opts = if normalized_terms.len() > 1 {
1080        effective_options = TextSearchOptions {
1081            total_limit: options.total_limit.saturating_mul(normalized_terms.len()),
1082            max_per_file: options.max_per_file,
1083            case_sensitive: options.case_sensitive,
1084            whole_word: options.whole_word,
1085            path_scope: options.path_scope.clone(),
1086            language_filter: options.language_filter.clone(),
1087            glob: options.glob.clone(),
1088            exclude_glob: options.exclude_glob.clone(),
1089            search_scope: options.search_scope,
1090            noise_policy: options.noise_policy,
1091            include_personal_tooling: options.include_personal_tooling,
1092            context: options.context,
1093            ranked: options.ranked,
1094            churn_scores: options.churn_scores.clone(),
1095        };
1096        &effective_options
1097    } else {
1098        options
1099    };
1100
1101    let line_matches = |line: &str| -> bool {
1102        if let Some(matcher) = whole_word_matcher.as_ref() {
1103            return matcher.is_match(line);
1104        }
1105
1106        if let Some(ac) = multi_term_ac.as_ref() {
1107            return ac.is_match(line);
1108        }
1109
1110        // Single-term path (or non-ASCII multi-term fallback)
1111        if case_sensitive {
1112            normalized_terms.iter().any(|term| line.contains(term))
1113        } else {
1114            let lowered = line.to_lowercase();
1115            lowered_terms
1116                .as_ref()
1117                .expect("lowered terms should exist for case-insensitive search")
1118                .iter()
1119                .any(|term| lowered.contains(term))
1120        }
1121    };
1122
1123    let mut result = collect_text_matches(
1124        index,
1125        candidate_paths.into_iter().collect(),
1126        |line| line_matches(line),
1127        label.clone(),
1128        opts,
1129    );
1130    result.suppressed_by_noise =
1131        result
1132            .suppressed_by_noise
1133            .saturating_add(count_suppressed_text_matches(
1134                index,
1135                suppressed_candidate_paths.into_iter().collect(),
1136                |line| line_matches(line),
1137                label,
1138                opts,
1139            ));
1140    Ok(result)
1141}
1142
1143fn count_suppressed_text_matches<F>(
1144    index: &LiveIndex,
1145    candidate_paths: Vec<String>,
1146    mut is_match: F,
1147    _label: String,
1148    _options: &TextSearchOptions,
1149) -> usize
1150where
1151    F: FnMut(&str) -> bool,
1152{
1153    if candidate_paths.is_empty() {
1154        return 0;
1155    }
1156
1157    let mut suppressed = 0usize;
1158    for path in candidate_paths {
1159        let Some(file) = index.get_file(&path) else {
1160            continue;
1161        };
1162        let content_str = String::from_utf8_lossy(&file.content);
1163
1164        for line in content_str.lines() {
1165            let line = line.trim_end_matches('\r');
1166            if !is_match(line) {
1167                continue;
1168            }
1169            suppressed = suppressed.saturating_add(1);
1170            if suppressed > SUPPRESSED_TEXT_MATCH_DISPLAY_CAP {
1171                return SUPPRESSED_TEXT_MATCH_DISPLAY_CAP + 1;
1172            }
1173        }
1174    }
1175    suppressed
1176}
1177
1178fn file_matches_text_base_scope(
1179    path: &str,
1180    file: &crate::live_index::IndexedFile,
1181    options: &TextSearchOptions,
1182    glob_filters: &CompiledTextGlobFilters,
1183) -> bool {
1184    options.path_scope.matches(path)
1185        && glob_filters.matches(path)
1186        && options.search_scope.allows(&file.classification)
1187        && options
1188            .language_filter
1189            .as_ref()
1190            .is_none_or(|language| &file.language == language)
1191}
1192
1193fn file_hidden_by_search_policy(
1194    path: &str,
1195    file: &crate::live_index::IndexedFile,
1196    options: &TextSearchOptions,
1197) -> bool {
1198    !options.noise_policy.allows(&file.classification)
1199        || (!options.include_personal_tooling
1200            && crate::live_index::query::is_personal_tooling_path(path))
1201}
1202
1203fn file_matches_text_options(
1204    path: &str,
1205    file: &crate::live_index::IndexedFile,
1206    options: &TextSearchOptions,
1207    glob_filters: &CompiledTextGlobFilters,
1208) -> bool {
1209    file_matches_text_base_scope(path, file, options, glob_filters)
1210        && !file_hidden_by_search_policy(path, file, options)
1211}
1212
1213/// File-level scope+noise gate for the default code-search preset
1214/// (`TextSearchOptions::for_current_code_search`), expressed without any
1215/// caller-supplied glob/path scope (the overlay post-filter has none).
1216///
1217/// Returns `true` when a file with `path` and `classification` would survive
1218/// the base's file-level filtering for `search_text`/`find_references`:
1219/// `SearchScope::Code` (code class only) + the default `NoisePolicy`
1220/// (generated and test files hidden; vendor kept) + personal-tooling exclusion.
1221/// It is single-sourced through [`SearchScope::allows`],
1222/// [`NoisePolicy::allows`], and [`is_personal_tooling_path`] so the overlay
1223/// scan cannot diverge from the base classifier.
1224///
1225/// `pub(super)` for the overlay post-filter in `live_index::view`; the base
1226/// path keeps using `file_matches_text_base_scope` + `file_hidden_by_search_policy`
1227/// (which additionally honor caller glob/path/language scope).
1228pub(super) fn current_code_search_keeps_file(
1229    path: &str,
1230    classification: &FileClassification,
1231) -> bool {
1232    let options = TextSearchOptions::for_current_code_search();
1233    options.search_scope.allows(classification)
1234        && options.noise_policy.allows(classification)
1235        && (options.include_personal_tooling
1236            || !crate::live_index::query::is_personal_tooling_path(path))
1237}
1238
1239/// Structural (AST-pattern) search across indexed files.
1240///
1241/// Uses ast-grep to match a tree-sitter AST pattern against source files.
1242/// Returns results in the same `TextSearchResult` format as text search so
1243/// the existing rendering pipeline can display them unchanged.
1244pub fn search_structural(
1245    index: &LiveIndex,
1246    pattern: &str,
1247    options: &TextSearchOptions,
1248) -> Result<TextSearchResult, TextSearchError> {
1249    search_structural_with_compiler(
1250        index,
1251        pattern,
1252        options,
1253        crate::parsing::ast_grep::compile_structural_pattern,
1254    )
1255}
1256
1257fn search_structural_with_compiler(
1258    index: &LiveIndex,
1259    pattern: &str,
1260    options: &TextSearchOptions,
1261    mut compile_pattern: impl FnMut(
1262        &str,
1263        &LanguageId,
1264        bool,
1265    ) -> Result<
1266        crate::parsing::ast_grep::CompiledStructuralPattern,
1267        String,
1268    >,
1269) -> Result<TextSearchResult, TextSearchError> {
1270    let compiled_globs = compile_text_glob_filters(options)?;
1271
1272    let mut files: Vec<TextFileMatches> = Vec::new();
1273    let mut total_matches = 0usize;
1274    let mut suppressed_by_noise = 0usize;
1275    // Cache key includes the TSX flavor because `.tsx` and `.ts` share the
1276    // TypeScript LanguageId but compile against different tree-sitter grammars.
1277    let mut compiled_patterns: HashMap<
1278        (LanguageId, bool, String),
1279        Result<crate::parsing::ast_grep::CompiledStructuralPattern, String>,
1280    > = HashMap::new();
1281    // Track whether at least one candidate successfully compiled the
1282    // pattern. If every candidate errored, we must distinguish:
1283    //   * at least one candidate rejected the pattern as syntactically
1284    //     invalid — propagate as InvalidStructuralPattern (pattern bug).
1285    //   * every candidate was in a language ast-grep does not support —
1286    //     propagate as UnsupportedStructuralLanguage (index/filter bug).
1287    // Conflating the two was the original Unit 5 bug; split buckets fix it.
1288    let mut any_parse_succeeded = false;
1289    let mut first_syntax_error: Option<String> = None;
1290    let mut first_unsupported_error: Option<String> = None;
1291
1292    // Collect candidate files filtered by options.
1293    let mut candidates: Vec<(String, crate::domain::index::LanguageId)> = index
1294        .all_files()
1295        .filter(|(path, file)| file_matches_text_options(path, file, options, &compiled_globs))
1296        .map(|(path, file)| (path.clone(), file.language.clone()))
1297        .collect();
1298    candidates.sort_by(|a, b| a.0.cmp(&b.0));
1299
1300    for (path, lang) in &candidates {
1301        if total_matches >= options.total_limit {
1302            break;
1303        }
1304
1305        let file = match index.get_file(path) {
1306            Some(f) => f,
1307            None => continue,
1308        };
1309
1310        let content_str = String::from_utf8_lossy(&file.content);
1311
1312        let is_tsx = LanguageId::is_tsx_path(path);
1313        let compiled = compiled_patterns
1314            .entry((lang.clone(), is_tsx, pattern.to_string()))
1315            .or_insert_with(|| compile_pattern(pattern, lang, is_tsx));
1316        let structural_matches = match compiled {
1317            Ok(compiled_pattern) => {
1318                any_parse_succeeded = true;
1319                crate::parsing::ast_grep::structural_search_with_compiled(
1320                    &content_str,
1321                    compiled_pattern,
1322                )
1323            }
1324            Err(e) => {
1325                // compile_structural_pattern returns two distinguishable error
1326                // shapes: "structural search not supported for …" for
1327                // config languages, and "invalid structural pattern: …"
1328                // for ast-grep Pattern::try_new failures.
1329                let e = e.clone();
1330                if e.starts_with("invalid structural pattern") {
1331                    if first_syntax_error.is_none() {
1332                        first_syntax_error = Some(e);
1333                    }
1334                } else if first_unsupported_error.is_none() {
1335                    first_unsupported_error = Some(e);
1336                }
1337                continue;
1338            }
1339        };
1340
1341        if structural_matches.is_empty() {
1342            continue;
1343        }
1344
1345        // Pre-compute Rust test module line ranges for noise filtering.
1346        let test_ranges: Vec<(u32, u32)> = if !options.noise_policy.include_tests
1347            && *lang == crate::domain::index::LanguageId::Rust
1348        {
1349            compute_test_ranges(file)
1350        } else {
1351            Vec::new()
1352        };
1353
1354        let remaining = options.total_limit.saturating_sub(total_matches);
1355        let per_file_limit = options.max_per_file.min(remaining);
1356
1357        let mut matches: Vec<TextLineMatch> = Vec::new();
1358        for sm in &structural_matches {
1359            let line_idx = sm.start_line;
1360
1361            // Skip matches inside Rust #[cfg(test)] modules.
1362            if !test_ranges.is_empty() {
1363                let line_num = line_idx as u32;
1364                if test_ranges
1365                    .iter()
1366                    .any(|&(start, end)| line_num >= start && line_num <= end)
1367                {
1368                    suppressed_by_noise += 1;
1369                    continue;
1370                }
1371            }
1372
1373            // Build display line: first line of matched text + captures summary.
1374            let first_line = sm.text.lines().next().unwrap_or(&sm.text);
1375            let display = if sm.captures.is_empty() {
1376                first_line.to_string()
1377            } else {
1378                let caps: Vec<String> = sm
1379                    .captures
1380                    .iter()
1381                    .map(|(name, val)| {
1382                        let short = if val.len() > 40 {
1383                            format!("{}...", &val[..37])
1384                        } else {
1385                            val.clone()
1386                        };
1387                        format!("${name}={short}")
1388                    })
1389                    .collect();
1390                format!("{first_line}  // {}", caps.join(", "))
1391            };
1392
1393            matches.push(TextLineMatch {
1394                line_number: line_idx + 1,
1395                line: display,
1396                enclosing_symbol: file
1397                    .symbols
1398                    .iter()
1399                    .filter(|s| {
1400                        s.line_range.0 <= (line_idx as u32) && s.line_range.1 >= (line_idx as u32)
1401                    })
1402                    .max_by_key(|s| s.depth)
1403                    .map(|s| EnclosingMatchSymbol {
1404                        name: s.name.clone(),
1405                        kind: s.kind.to_string(),
1406                        line_range: s.line_range,
1407                    }),
1408            });
1409            if matches.len() >= per_file_limit {
1410                break;
1411            }
1412        }
1413
1414        if !matches.is_empty() {
1415            let rendered_lines = options
1416                .context
1417                .map(|ctx| build_context_rendered_lines(&content_str, &matches, ctx));
1418            total_matches += matches.len();
1419            files.push(TextFileMatches {
1420                path: path.clone(),
1421                matches,
1422                rendered_lines,
1423                callers: None,
1424            });
1425        }
1426    }
1427
1428    // If no candidate compiled the pattern, surface the right error kind.
1429    // Syntax errors win over "unsupported language" because a syntax error
1430    // means the USER has a bug in their pattern; the language-unsupported
1431    // bucket only matters when every candidate was in a config language.
1432    if !any_parse_succeeded {
1433        if let Some(error) = first_syntax_error {
1434            return Err(TextSearchError::InvalidStructuralPattern {
1435                pattern: pattern.to_string(),
1436                error,
1437            });
1438        }
1439        if let Some(sample_error) = first_unsupported_error {
1440            return Err(TextSearchError::UnsupportedStructuralLanguage {
1441                pattern: pattern.to_string(),
1442                sample_error,
1443            });
1444        }
1445    }
1446
1447    Ok(TextSearchResult {
1448        label: format!("structural '{pattern}'"),
1449        total_matches,
1450        files,
1451        suppressed_by_noise,
1452        overflow_count: 0,
1453    })
1454}
1455
1456fn compile_text_glob_filters(
1457    options: &TextSearchOptions,
1458) -> Result<CompiledTextGlobFilters, TextSearchError> {
1459    Ok(CompiledTextGlobFilters {
1460        include: compile_text_glob("glob", options.glob.as_deref())?,
1461        exclude: compile_text_glob("exclude_glob", options.exclude_glob.as_deref())?,
1462    })
1463}
1464
1465fn compile_text_glob(
1466    field: &'static str,
1467    pattern: Option<&str>,
1468) -> Result<Option<GlobMatcher>, TextSearchError> {
1469    let Some(pattern) = pattern.filter(|pattern| !pattern.is_empty()) else {
1470        return Ok(None);
1471    };
1472
1473    let glob = GlobBuilder::new(pattern)
1474        .literal_separator(true)
1475        .build()
1476        .map_err(|error| TextSearchError::InvalidGlob {
1477            field,
1478            pattern: pattern.to_string(),
1479            error: error.to_string(),
1480        })?;
1481
1482    Ok(Some(glob.compile_matcher()))
1483}
1484
1485fn compile_literal_whole_word_matcher(terms: &[String], case_sensitive: bool) -> regex::Regex {
1486    let pattern = format!(
1487        r"\b(?:{})\b",
1488        terms
1489            .iter()
1490            .map(|term| regex::escape(term))
1491            .collect::<Vec<_>>()
1492            .join("|")
1493    );
1494
1495    regex::RegexBuilder::new(&pattern)
1496        .case_insensitive(!case_sensitive)
1497        .build()
1498        .expect("escaped literal whole-word matcher should compile")
1499}
1500
1501/// Returns a 0.0–1.0 priority score based on symbol kind.
1502pub(crate) fn symbol_kind_priority(kind: &str) -> f32 {
1503    match kind {
1504        "function" | "method" | "async_function" | "generator" => 1.0,
1505        "class" | "struct" | "enum" | "interface" | "trait" | "union" => 0.8,
1506        "impl" | "implementation" => 0.7,
1507        "module" | "namespace" => 0.5,
1508        "constant" | "const" => 0.4,
1509        "variable" | "type" | "type_alias" => 0.3,
1510        "key" | "section" | "property" | "field" => 0.2,
1511        _ => 0.1,
1512    }
1513}
1514
1515/// Compute a semantic importance score for a file's search results.
1516/// Combines match count, caller connectivity, and symbol kind.
1517fn compute_importance_score(
1518    file_matches: &TextFileMatches,
1519    match_count_max: usize,
1520    reverse_index: &HashMap<String, Vec<super::store::ReferenceLocation>>,
1521    churn_score: f32,
1522) -> f32 {
1523    let match_count_norm = if match_count_max > 0 {
1524        file_matches.matches.len() as f32 / match_count_max as f32
1525    } else {
1526        0.0
1527    };
1528
1529    // Find max caller count among enclosing symbols
1530    let max_callers = file_matches
1531        .matches
1532        .iter()
1533        .filter_map(|m| m.enclosing_symbol.as_ref())
1534        .map(|sym| {
1535            reverse_index
1536                .get(&sym.name)
1537                .map(|refs| refs.len())
1538                .unwrap_or(0)
1539        })
1540        .max()
1541        .unwrap_or(0);
1542    // Calibrated for medium repos; high-caller utilities saturate at 1.0.
1543    const CALLER_COUNT_NORMALIZER: f32 = 20.0;
1544    let caller_norm = (max_callers as f32 / CALLER_COUNT_NORMALIZER).min(1.0);
1545
1546    // Find max kind priority among enclosing symbols
1547    let max_kind = file_matches
1548        .matches
1549        .iter()
1550        .filter_map(|m| m.enclosing_symbol.as_ref())
1551        .map(|sym| symbol_kind_priority(&sym.kind))
1552        .fold(0.0f32, f32::max);
1553
1554    // Composite score
1555    0.30 * match_count_norm + 0.40 * caller_norm + 0.15 * churn_score + 0.15 * max_kind
1556}
1557
1558/// `pub(super)` so the overlay post-filter (`live_index::view`) suppresses
1559/// matches inside Rust `#[cfg(test)]`/`mod tests` blocks exactly as the base
1560/// `collect_text_matches` does for an upserted dirty file.
1561pub(super) fn compute_test_ranges(file: &crate::live_index::IndexedFile) -> Vec<(u32, u32)> {
1562    file.symbols
1563        .iter()
1564        .filter(|s| s.name == "tests" && s.kind == SymbolKind::Module)
1565        .map(|s| s.line_range)
1566        .collect()
1567}
1568
1569fn attach_text_match_context(
1570    index: &LiveIndex,
1571    options: &TextSearchOptions,
1572    file_matches: &mut TextFileMatches,
1573) {
1574    let Some(context) = options.context else {
1575        return;
1576    };
1577    let Some(file) = index.get_file(&file_matches.path) else {
1578        return;
1579    };
1580    let content_str = String::from_utf8_lossy(&file.content);
1581    file_matches.rendered_lines = Some(build_context_rendered_lines(
1582        &content_str,
1583        &file_matches.matches,
1584        context,
1585    ));
1586}
1587
1588fn collect_text_matches<F>(
1589    index: &LiveIndex,
1590    candidate_paths: Vec<String>,
1591    mut is_match: F,
1592    label: String,
1593    options: &TextSearchOptions,
1594) -> TextSearchResult
1595where
1596    F: FnMut(&str) -> bool,
1597{
1598    struct PathMatchBucket {
1599        path: String,
1600        visible_count: usize,
1601        matches: Vec<TextLineMatch>,
1602    }
1603
1604    // Single scan per file: count every visible hit for overflow reporting while
1605    // retaining only the bounded match lines needed for output/ranking.
1606    let mut buckets: Vec<PathMatchBucket> = Vec::new();
1607    let mut suppressed_by_noise: usize = 0;
1608    let mut all_visible_matches: usize = 0;
1609    for path in candidate_paths {
1610        let file = match index.get_file(&path) {
1611            Some(file) => file,
1612            None => continue,
1613        };
1614        let content_str = String::from_utf8_lossy(&file.content);
1615
1616        // Pre-compute Rust test module line ranges for noise filtering.
1617        let test_ranges: Vec<(u32, u32)> =
1618            if !options.noise_policy.include_tests && file.language == LanguageId::Rust {
1619                compute_test_ranges(file)
1620            } else {
1621                Vec::new()
1622            };
1623
1624        let mut visible_count = 0usize;
1625        let mut suppressed = 0usize;
1626        let mut matches: Vec<TextLineMatch> = Vec::new();
1627        for (line_idx, line) in content_str.lines().enumerate() {
1628            let line = line.trim_end_matches('\r');
1629            if !is_match(line) {
1630                continue;
1631            }
1632            // Skip matches inside Rust #[cfg(test)] modules.
1633            if !test_ranges.is_empty() {
1634                let line_num = line_idx as u32;
1635                if test_ranges
1636                    .iter()
1637                    .any(|&(start, end)| line_num >= start && line_num <= end)
1638                {
1639                    suppressed += 1;
1640                    continue;
1641                }
1642            }
1643            visible_count += 1;
1644            if matches.len() < options.max_per_file {
1645                matches.push(TextLineMatch {
1646                    line_number: line_idx + 1,
1647                    line: truncate_display_line(line),
1648                    enclosing_symbol: file
1649                        .symbols
1650                        .iter()
1651                        .filter(|s| {
1652                            s.line_range.0 <= (line_idx as u32)
1653                                && s.line_range.1 >= (line_idx as u32)
1654                        })
1655                        .max_by_key(|s| s.depth)
1656                        .map(|s| EnclosingMatchSymbol {
1657                            name: s.name.clone(),
1658                            kind: s.kind.to_string(),
1659                            line_range: s.line_range,
1660                        }),
1661                });
1662            }
1663        }
1664        suppressed_by_noise += suppressed;
1665        all_visible_matches += visible_count;
1666        if visible_count > 0 {
1667            buckets.push(PathMatchBucket {
1668                path,
1669                visible_count,
1670                matches,
1671            });
1672        }
1673    }
1674
1675    // Sort by match count descending, alphabetical tiebreak.
1676    buckets.sort_by(|a, b| {
1677        b.visible_count
1678            .cmp(&a.visible_count)
1679            .then_with(|| a.path.cmp(&b.path))
1680    });
1681
1682    let mut files: Vec<TextFileMatches> = Vec::new();
1683    let mut total_matches = 0usize;
1684
1685    // When ranked, collect from all files (up to a safety cap) so the ranker
1686    // can reorder across the full set.  The total_limit is applied *after*
1687    // ranking to trim the final output.  Without this, high-match-count files
1688    // exhaust the budget before diverse but important files are visited.
1689    const RANKED_FILE_CAP: usize = 500;
1690
1691    for (file_idx, mut bucket) in buckets.into_iter().enumerate() {
1692        if options.ranked {
1693            if file_idx >= RANKED_FILE_CAP {
1694                break;
1695            }
1696        } else if total_matches >= options.total_limit {
1697            break;
1698        }
1699
1700        let per_file_limit = if options.ranked {
1701            options.max_per_file
1702        } else {
1703            let remaining_total = options.total_limit.saturating_sub(total_matches);
1704            options.max_per_file.min(remaining_total)
1705        };
1706
1707        if per_file_limit == 0 {
1708            break;
1709        }
1710
1711        if bucket.matches.len() > per_file_limit {
1712            bucket.matches.truncate(per_file_limit);
1713        }
1714
1715        if !bucket.matches.is_empty() {
1716            total_matches += bucket.matches.len();
1717            let mut file_matches = TextFileMatches {
1718                path: bucket.path,
1719                matches: bucket.matches,
1720                rendered_lines: None,
1721                callers: None,
1722            };
1723            if !options.ranked {
1724                attach_text_match_context(index, options, &mut file_matches);
1725            }
1726            files.push(file_matches);
1727        }
1728    }
1729
1730    // Apply semantic re-ranking when requested.
1731    if options.ranked {
1732        let match_count_max = files.iter().map(|f| f.matches.len()).max().unwrap_or(1);
1733        let churn = options.churn_scores.as_ref();
1734        files.sort_by(|a, b| {
1735            let churn_a = churn.and_then(|m| m.get(&a.path).copied()).unwrap_or(0.0);
1736            let churn_b = churn.and_then(|m| m.get(&b.path).copied()).unwrap_or(0.0);
1737            let score_a =
1738                compute_importance_score(a, match_count_max, &index.reverse_index, churn_a);
1739            let score_b =
1740                compute_importance_score(b, match_count_max, &index.reverse_index, churn_b);
1741            score_b
1742                .partial_cmp(&score_a)
1743                .unwrap_or(std::cmp::Ordering::Equal)
1744        });
1745
1746        // Truncate to total_limit *after* ranking so the most important files
1747        // survive regardless of their raw match count.
1748        let mut remaining = options.total_limit;
1749        files.retain_mut(|f| {
1750            if remaining == 0 {
1751                return false;
1752            }
1753            if f.matches.len() > remaining {
1754                f.matches.truncate(remaining);
1755            }
1756            remaining -= f.matches.len();
1757            true
1758        });
1759        total_matches = files.iter().map(|f| f.matches.len()).sum();
1760        for file_matches in &mut files {
1761            attach_text_match_context(index, options, file_matches);
1762        }
1763    }
1764
1765    TextSearchResult {
1766        label,
1767        total_matches,
1768        files,
1769        suppressed_by_noise,
1770        overflow_count: all_visible_matches.saturating_sub(total_matches),
1771    }
1772}
1773
1774fn build_context_rendered_lines(
1775    content: &str,
1776    matches: &[TextLineMatch],
1777    context: usize,
1778) -> Vec<TextDisplayLine> {
1779    if matches.is_empty() {
1780        return Vec::new();
1781    }
1782
1783    let lines: Vec<&str> = content
1784        .lines()
1785        .map(|line| line.trim_end_matches('\r'))
1786        .collect();
1787    if lines.is_empty() {
1788        return Vec::new();
1789    }
1790
1791    let mut windows: Vec<(usize, usize)> = Vec::new();
1792    for line_match in matches {
1793        let start = line_match.line_number.saturating_sub(context).max(1);
1794        let end = (line_match.line_number + context).min(lines.len());
1795
1796        if let Some((_, last_end)) = windows.last_mut()
1797            && start <= *last_end + 1
1798        {
1799            *last_end = (*last_end).max(end);
1800            continue;
1801        }
1802        windows.push((start, end));
1803    }
1804
1805    let match_lines: HashSet<usize> = matches
1806        .iter()
1807        .map(|line_match| line_match.line_number)
1808        .collect();
1809    let mut rendered: Vec<TextDisplayLine> = Vec::new();
1810
1811    for (window_idx, (start, end)) in windows.into_iter().enumerate() {
1812        if window_idx > 0 {
1813            rendered.push(TextDisplayLine::Separator);
1814        }
1815        for line_number in start..=end {
1816            rendered.push(TextDisplayLine::Line(TextRenderedLine {
1817                line_number,
1818                line: truncate_display_line(lines[line_number - 1]),
1819                is_match: match_lines.contains(&line_number),
1820            }));
1821        }
1822    }
1823
1824    rendered
1825}
1826
1827// ── File-search frecency fusion ────────────────────────────────────────────
1828//
1829// Opt-in rerank used by `search_files` when `rank_by="frecency"` is set and
1830// call-time frecency history is available. Default callers keep the existing
1831// tier-based comparator in `query::capture_search_files_view`.
1832//
1833// Contract: `combined = 0.6 * path_match + 0.3 * co_change + 0.1 * frecency_norm`
1834// with frecency normalized against the max raw score in the candidate set so
1835// every contribution sits on the same `[0, 1]` scale. Anchors the fusion
1836// weights from the spec's Implementation Notes §"Decay + fusion starting
1837// parameters".
1838
1839/// Path-match signal contribution derived from the candidate's tier. Strong
1840/// (exact or suffix) paths earn the full `1.0`; basenames a solid `0.6`;
1841/// loose-component and prefix hits `0.3`. Co-change hits don't participate
1842/// in path-match (they flow through the co-change signal instead).
1843pub fn tier_path_match_score(tier: SearchFilesTier) -> f64 {
1844    match tier {
1845        SearchFilesTier::StrongPath => 1.0,
1846        SearchFilesTier::Basename => 0.6,
1847        SearchFilesTier::LoosePath => 0.3,
1848        SearchFilesTier::CoChange => 0.0,
1849        // Tier-2 metadata-only paths earn no path-match credit; the hard rank
1850        // floor in `reorder_hits_by_frecency_fusion` keeps them below Tier-1.
1851        SearchFilesTier::MetadataOnly => 0.0,
1852    }
1853}
1854
1855/// Per-hit fused score. Returned alongside the re-ranked hits so callers
1856/// rendering `SYMFORGE_DEBUG_RANKING=1` can show the breakdown without
1857/// re-deriving anything.
1858#[derive(Debug, Clone, Copy, PartialEq)]
1859pub struct FrecencyFusionBreakdown {
1860    pub path_match: f64,
1861    pub co_change: f64,
1862    pub frecency_normalized: f64,
1863    pub combined: f64,
1864}
1865
1866/// Re-rank a candidate set by the frecency fusion policy. The returned
1867/// vector preserves one-to-one correspondence with the input `hits`: each
1868/// hit keeps its position in the result vec, and the parallel
1869/// `breakdowns` vec reports the per-hit scores for call-time or default-on
1870/// ranking diagnostics. Callers can then sort or filter however they need;
1871/// the `reorder_by_combined` helper is the
1872/// common case.
1873pub fn score_hits_by_frecency_fusion(
1874    hits: &[SearchFilesHit],
1875    frecency_scores: &HashMap<PathBuf, f64>,
1876) -> Vec<FrecencyFusionBreakdown> {
1877    let max_frecency = frecency_scores.values().copied().fold(0.0_f64, f64::max);
1878    hits.iter()
1879        .map(|hit| {
1880            let path_match = tier_path_match_score(hit.tier);
1881            let co_change = hit.coupling_score.map(f64::from).unwrap_or(0.0);
1882            let raw = frecency_scores
1883                .get(Path::new(hit.path.as_str()))
1884                .copied()
1885                .unwrap_or(0.0);
1886            let frecency_normalized = if max_frecency > 0.0 {
1887                (raw / max_frecency).clamp(0.0, 1.0)
1888            } else {
1889                0.0
1890            };
1891            let combined = 0.6 * path_match + 0.3 * co_change + 0.1 * frecency_normalized;
1892            FrecencyFusionBreakdown {
1893                path_match,
1894                co_change,
1895                frecency_normalized,
1896                combined,
1897            }
1898        })
1899        .collect()
1900}
1901
1902/// Re-sort `hits` by descending combined score using the provided
1903/// `breakdowns` (must be parallel to `hits`). Ties break stably by path so
1904/// ordering is deterministic across runs.
1905pub fn reorder_hits_by_frecency_fusion(
1906    mut hits: Vec<SearchFilesHit>,
1907    breakdowns: &[FrecencyFusionBreakdown],
1908) -> Vec<SearchFilesHit> {
1909    debug_assert_eq!(hits.len(), breakdowns.len());
1910    let mut indexed: Vec<(usize, SearchFilesHit)> = hits.drain(..).enumerate().collect();
1911    indexed.sort_by(|a, b| {
1912        // Rank floor: Tier-2 metadata-only hits never outrank a Tier-1 hit, even
1913        // if frecency would score them higher. `false` (Tier-1) precedes `true`.
1914        let a_meta = a.1.tier.is_metadata_only();
1915        let b_meta = b.1.tier.is_metadata_only();
1916        if a_meta != b_meta {
1917            return a_meta.cmp(&b_meta);
1918        }
1919        let sa = breakdowns[a.0].combined;
1920        let sb = breakdowns[b.0].combined;
1921        sb.partial_cmp(&sa)
1922            .unwrap_or(std::cmp::Ordering::Equal)
1923            .then_with(|| a.1.path.cmp(&b.1.path))
1924    });
1925    indexed.into_iter().map(|(_, hit)| hit).collect()
1926}
1927
1928#[cfg(test)]
1929mod tests {
1930    use std::collections::HashMap;
1931    use std::time::{Duration, Instant, SystemTime};
1932
1933    use super::*;
1934    use crate::domain::{LanguageId, SymbolKind, SymbolRecord};
1935    use crate::live_index::store::{CircuitBreakerState, IndexedFile, ParseStatus};
1936    use crate::live_index::trigram::TrigramIndex;
1937
1938    fn make_symbol(name: &str, kind: SymbolKind, line: u32) -> SymbolRecord {
1939        let byte_range = (0, 0);
1940        SymbolRecord {
1941            name: name.to_string(),
1942            kind,
1943            depth: 0,
1944            sort_order: 0,
1945            byte_range,
1946            item_byte_range: Some(byte_range),
1947            line_range: (line, line),
1948            doc_byte_range: None,
1949        }
1950    }
1951
1952    fn make_file_with_classification(
1953        path: &str,
1954        content: &str,
1955        symbols: Vec<SymbolRecord>,
1956        classification: crate::domain::FileClassification,
1957    ) -> (String, IndexedFile) {
1958        (
1959            path.to_string(),
1960            IndexedFile {
1961                relative_path: path.to_string(),
1962                language: LanguageId::Rust,
1963                classification,
1964                content: content.as_bytes().to_vec(),
1965                symbols,
1966                parse_status: ParseStatus::Parsed,
1967                parse_diagnostic: None,
1968                byte_len: content.len() as u64,
1969                content_hash: "hash".to_string(),
1970                references: Vec::new(),
1971                alias_map: HashMap::new(),
1972                mtime_secs: 0,
1973            },
1974        )
1975    }
1976
1977    fn make_file(path: &str, content: &str, symbols: Vec<SymbolRecord>) -> (String, IndexedFile) {
1978        make_file_with_classification(
1979            path,
1980            content,
1981            symbols,
1982            crate::domain::FileClassification::for_code_path(path),
1983        )
1984    }
1985
1986    fn make_index(files: Vec<(String, IndexedFile)>) -> LiveIndex {
1987        let file_map: HashMap<String, std::sync::Arc<IndexedFile>> = files
1988            .into_iter()
1989            .map(|(path, file)| (path, std::sync::Arc::new(file)))
1990            .collect();
1991        let trigram_index = TrigramIndex::build_from_files(&file_map);
1992        let mut index = LiveIndex {
1993            files: file_map,
1994            loaded_at: Instant::now(),
1995            loaded_at_system: SystemTime::now(),
1996            load_duration: Duration::ZERO,
1997            cb_state: CircuitBreakerState::new(0.20),
1998            is_empty: false,
1999            load_source: crate::live_index::store::IndexLoadSource::FreshLoad,
2000            snapshot_verify_state: crate::live_index::store::SnapshotVerifyState::NotNeeded,
2001            reverse_index: HashMap::new(),
2002            files_by_basename: HashMap::new(),
2003            files_by_dir_component: HashMap::new(),
2004            trigram_index,
2005            gitignore: None,
2006            skipped_files: Vec::new(),
2007            coupling_store: None,
2008            local_empty_reason: std::sync::Arc::new(parking_lot::RwLock::new(None)),
2009            indexed_root: None,
2010        };
2011        index.rebuild_path_indices();
2012        index
2013    }
2014
2015    #[test]
2016    fn test_search_module_symbol_search_respects_tiers_and_limit() {
2017        let index = make_index(vec![
2018            make_file(
2019                "src/a.rs",
2020                "",
2021                vec![
2022                    make_symbol("job", SymbolKind::Function, 1),
2023                    make_symbol("jobQueue", SymbolKind::Function, 2),
2024                    make_symbol("enqueueJob", SymbolKind::Function, 3),
2025                ],
2026            ),
2027            make_file(
2028                "src/b.rs",
2029                "",
2030                vec![make_symbol("jobber", SymbolKind::Method, 4)],
2031            ),
2032        ]);
2033
2034        let result = search_symbols(&index, "job", None, 3);
2035
2036        assert_eq!(result.file_count, 2);
2037        assert_eq!(result.hits.len(), 3);
2038        assert_eq!(result.hits[0].tier, SymbolMatchTier::Exact);
2039        assert_eq!(result.hits[0].name, "job");
2040        assert_eq!(result.hits[1].tier, SymbolMatchTier::Prefix);
2041        assert_eq!(result.hits[1].name, "jobber");
2042        assert_eq!(result.hits[2].tier, SymbolMatchTier::Prefix);
2043        assert_eq!(result.hits[2].name, "jobQueue");
2044        assert_eq!(result.overflow_count, 1);
2045    }
2046
2047    #[test]
2048    fn test_search_module_symbol_search_kind_filter_allows_all_keyword() {
2049        let index = make_index(vec![make_file(
2050            "src/a.rs",
2051            "",
2052            vec![
2053                make_symbol("job", SymbolKind::Function, 1),
2054                make_symbol("job", SymbolKind::Method, 2),
2055            ],
2056        )]);
2057
2058        let result = search_symbols(&index, "job", Some("all"), 50);
2059
2060        assert_eq!(result.hits.len(), 2);
2061    }
2062
2063    #[test]
2064    fn test_kind_filter_matches_semantic_aliases() {
2065        use crate::domain::SymbolKind;
2066        // Display strings match exactly
2067        assert!(kind_filter_matches("let", &SymbolKind::Variable));
2068        assert!(kind_filter_matches("fn", &SymbolKind::Function));
2069        assert!(kind_filter_matches("fn", &SymbolKind::Method));
2070        assert!(kind_filter_matches("mod", &SymbolKind::Module));
2071        assert!(kind_filter_matches("const", &SymbolKind::Constant));
2072        // Semantic aliases also match
2073        assert!(kind_filter_matches("variable", &SymbolKind::Variable));
2074        assert!(kind_filter_matches("Variable", &SymbolKind::Variable));
2075        assert!(kind_filter_matches("function", &SymbolKind::Function));
2076        assert!(kind_filter_matches("method", &SymbolKind::Method));
2077        assert!(kind_filter_matches("module", &SymbolKind::Module));
2078        assert!(kind_filter_matches("constant", &SymbolKind::Constant));
2079        // Non-matching pairs
2080        assert!(!kind_filter_matches("variable", &SymbolKind::Function));
2081        assert!(!kind_filter_matches("function", &SymbolKind::Variable));
2082        assert!(!kind_filter_matches("let", &SymbolKind::Constant));
2083    }
2084
2085    #[test]
2086    fn test_search_symbols_kind_variable_alias_finds_scss_variables() {
2087        // Regression: search_symbols(kind="variable") must find Variable-kind symbols.
2088        // Previously, SymbolKind::Variable.to_string() == "let", so kind="variable" matched nothing.
2089        let index = make_index(vec![make_file(
2090            "styles/tokens.scss",
2091            "",
2092            vec![
2093                make_symbol("$primary-color", SymbolKind::Variable, 1),
2094                make_symbol("$gap", SymbolKind::Variable, 2),
2095                make_symbol("flex-center", SymbolKind::Function, 3),
2096            ],
2097        )]);
2098
2099        let result = search_symbols(&index, "", Some("variable"), 50);
2100        let names: Vec<&str> = result.hits.iter().map(|h| h.name.as_str()).collect();
2101        assert!(
2102            names.contains(&"$primary-color"),
2103            "kind='variable' should find $primary-color, got: {:?}",
2104            names
2105        );
2106        assert!(
2107            names.contains(&"$gap"),
2108            "kind='variable' should find $gap, got: {:?}",
2109            names
2110        );
2111        assert!(
2112            !names.contains(&"flex-center"),
2113            "kind='variable' should NOT find mixin, got: {:?}",
2114            names
2115        );
2116    }
2117
2118    #[test]
2119    fn test_search_module_text_search_terms_are_trimmed_and_grouped() {
2120        let index = make_index(vec![
2121            make_file("src/a.rs", "TODO one\nother\nFIXME two\n", Vec::new()),
2122            make_file("src/b.rs", "todo lower\n", Vec::new()),
2123        ]);
2124        let terms = vec![" TODO ".to_string(), "".to_string(), "FIXME".to_string()];
2125
2126        let result = search_text(&index, None, Some(&terms), false).expect("search should work");
2127
2128        assert_eq!(result.label, "terms [TODO, FIXME]");
2129        assert_eq!(result.total_matches, 3);
2130        assert_eq!(result.files.len(), 2);
2131        assert_eq!(result.files[0].path, "src/a.rs");
2132        assert_eq!(result.files[0].matches[0].line_number, 1);
2133        assert_eq!(result.files[1].path, "src/b.rs");
2134        assert_eq!(result.overflow_count, 0);
2135    }
2136
2137    #[test]
2138    fn test_search_module_text_search_reports_overflow_when_limit_truncates() {
2139        let index = make_index(vec![make_file(
2140            "src/a.rs",
2141            "needle one\nneedle two\nneedle three\n",
2142            Vec::new(),
2143        )]);
2144        let options = TextSearchOptions {
2145            total_limit: 2,
2146            max_per_file: 2,
2147            ..TextSearchOptions::default()
2148        };
2149
2150        let result = search_text_with_options(&index, Some("needle"), None, false, &options)
2151            .expect("search");
2152
2153        assert_eq!(result.total_matches, 2);
2154        assert_eq!(result.overflow_count, 1);
2155    }
2156
2157    // ── SF-STRESS-008: per-line truncation guards ──────────────────────────
2158    //
2159    // A single committed minified line (hundreds of KB) must never be emitted
2160    // verbatim; the snippet renderer would otherwise detonate the tool result
2161    // (~123k tokens for one 490KB line) and consume an agent's whole context.
2162
2163    #[test]
2164    fn test_truncate_display_line_caps_overlong_line_with_honest_marker() {
2165        let long = "a".repeat(500_000);
2166        let out = truncate_display_line(&long);
2167        // Head excerpt is exactly the cap; marker accounts for the rest.
2168        assert!(
2169            out.starts_with(&"a".repeat(MAX_DISPLAY_LINE_CHARS)),
2170            "excerpt must retain the first MAX_DISPLAY_LINE_CHARS chars"
2171        );
2172        assert!(
2173            out.contains("[line truncated,"),
2174            "must carry an honest truncation marker, got len {}",
2175            out.len()
2176        );
2177        let expected_omitted = 500_000 - MAX_DISPLAY_LINE_CHARS;
2178        assert!(
2179            out.contains(&format!("{expected_omitted} chars omitted")),
2180            "marker must report the exact omitted-char count"
2181        );
2182        // Output is the excerpt plus a short fixed marker, nowhere near 500KB.
2183        assert!(
2184            out.len() < MAX_DISPLAY_LINE_CHARS + 64,
2185            "truncated line must stay close to the cap, got {} bytes",
2186            out.len()
2187        );
2188    }
2189
2190    #[test]
2191    fn test_truncate_display_line_is_char_boundary_safe_on_multibyte() {
2192        // Mix of multibyte chars (U+2028 line separator, emoji, CJK) repeated
2193        // past the cap. Byte slicing here would panic; char_indices must not.
2194        let unit = "a\u{2028}\u{1F600}\u{4E2D}"; // 1 + 3 + 4 + 3 = 11 bytes, 4 chars
2195        let long = unit.repeat(MAX_DISPLAY_LINE_CHARS); // 4 * cap chars >> cap
2196        let out = truncate_display_line(&long); // must not panic
2197        assert!(out.contains("[line truncated,"));
2198        // The excerpt prefix must itself be valid UTF-8 (guaranteed by &str),
2199        // and contain exactly MAX_DISPLAY_LINE_CHARS chars before the marker.
2200        let head = out.split("... [line truncated,").next().unwrap();
2201        assert_eq!(
2202            head.chars().count(),
2203            MAX_DISPLAY_LINE_CHARS,
2204            "excerpt must hold exactly the cap in chars"
2205        );
2206    }
2207
2208    #[test]
2209    fn test_truncate_display_line_passes_short_lines_through_unchanged() {
2210        let short = "let x = 42; // needle here";
2211        assert_eq!(truncate_display_line(short), short);
2212        // A line exactly at the cap is untouched.
2213        let at_cap = "x".repeat(MAX_DISPLAY_LINE_CHARS);
2214        assert_eq!(truncate_display_line(&at_cap), at_cap);
2215    }
2216
2217    #[test]
2218    fn test_search_text_bounds_a_500kb_single_line_file() {
2219        // Reproduce the corpus detonation: one ~500KB minified line holding the
2220        // needle. Pre-fix this emits the line verbatim (~490KB / ~123k tokens);
2221        // post-fix the stored match line is bounded and carries the marker.
2222        let mut line = "x".repeat(250_000);
2223        line.push_str("needle");
2224        line.push_str(&"y".repeat(250_000));
2225        let content = format!("{line}\n");
2226        let index = make_index(vec![make_file("dist/scripts.js", &content, Vec::new())]);
2227
2228        let result = search_text_with_options(
2229            &index,
2230            Some("needle"),
2231            None,
2232            false,
2233            &TextSearchOptions::default(),
2234        )
2235        .expect("search should work");
2236
2237        assert_eq!(result.total_matches, 1, "the needle line must still match");
2238        let stored = &result.files[0].matches[0].line;
2239        assert!(
2240            stored.chars().count() <= MAX_DISPLAY_LINE_CHARS + 64,
2241            "stored match line must be bounded, got {} chars",
2242            stored.chars().count()
2243        );
2244        assert!(
2245            stored.contains("[line truncated,"),
2246            "bounded match line must carry the truncation marker"
2247        );
2248
2249        // The renderer emits the stored `line` verbatim, so bounding total
2250        // stored-line bytes bounds the whole tool result. Pre-fix this sum was
2251        // ~500KB for this single match; post-fix it is a few KB. With a default
2252        // max_tokens budget of, say, 2000 tokens (~8KB), the result can no
2253        // longer be blown by one line.
2254        let total_line_bytes: usize = result
2255            .files
2256            .iter()
2257            .flat_map(|f| f.matches.iter())
2258            .map(|m| m.line.len())
2259            .sum();
2260        assert!(
2261            total_line_bytes < 8_000,
2262            "total stored match-line bytes must stay well under an 8KB (~2k token) budget, got {total_line_bytes}"
2263        );
2264    }
2265
2266    #[test]
2267    fn test_search_text_context_window_truncates_overlong_neighbor_lines() {
2268        // Neighbors of a minified line are themselves minified; the context
2269        // renderer must cap them too, not just the match line.
2270        let huge_neighbor = "z".repeat(400_000);
2271        let content = format!("{huge_neighbor}\nneedle here\n{huge_neighbor}\n");
2272        let index = make_index(vec![make_file("dist/bundle.js", &content, Vec::new())]);
2273        let options = TextSearchOptions {
2274            context: Some(1),
2275            ..TextSearchOptions::default()
2276        };
2277
2278        let result = search_text_with_options(&index, Some("needle"), None, false, &options)
2279            .expect("search should work");
2280
2281        let rendered = result.files[0]
2282            .rendered_lines
2283            .as_ref()
2284            .expect("context mode materializes rendered lines");
2285        for display in rendered {
2286            if let TextDisplayLine::Line(rl) = display {
2287                assert!(
2288                    rl.line.chars().count() <= MAX_DISPLAY_LINE_CHARS + 64,
2289                    "every context line (match or neighbor) must be bounded, got {} chars on line {}",
2290                    rl.line.chars().count(),
2291                    rl.line_number
2292                );
2293            }
2294        }
2295    }
2296
2297    #[test]
2298    fn test_search_module_text_search_empty_regex_query_errors() {
2299        let index = make_index(vec![make_file("src/a.rs", "content", Vec::new())]);
2300
2301        let result = search_text(&index, Some(" "), None, true);
2302
2303        assert_eq!(result, Err(TextSearchError::EmptyRegexQuery));
2304    }
2305
2306    #[test]
2307    fn test_search_module_symbol_search_with_options_respects_path_scope_and_noise_policy() {
2308        let index = make_index(vec![
2309            make_file(
2310                "src/job.rs",
2311                "",
2312                vec![make_symbol("job", SymbolKind::Function, 1)],
2313            ),
2314            make_file(
2315                "tests/generated/job_test.rs",
2316                "",
2317                vec![make_symbol("jobNoise", SymbolKind::Function, 2)],
2318            ),
2319        ]);
2320        let options = SymbolSearchOptions {
2321            path_scope: PathScope::prefix("src/"),
2322            noise_policy: NoisePolicy::hide_classified_noise(),
2323            ..Default::default()
2324        };
2325
2326        let result = search_symbols_with_options(&index, "job", None, &options);
2327
2328        assert_eq!(result.file_count, 1);
2329        assert_eq!(result.hits.len(), 1);
2330        assert_eq!(result.hits[0].path, "src/job.rs");
2331        assert_eq!(result.hits[0].name, "job");
2332    }
2333
2334    #[test]
2335    fn test_search_module_symbol_search_hides_generated_and_test_noise_by_default() {
2336        let index = make_index(vec![
2337            make_file(
2338                "src/job.rs",
2339                "",
2340                vec![make_symbol("Job", SymbolKind::Class, 1)],
2341            ),
2342            make_file(
2343                "src/generated/job_generated.rs",
2344                "",
2345                vec![make_symbol("JobGenerated", SymbolKind::Class, 2)],
2346            ),
2347            make_file(
2348                "tests/job_test.rs",
2349                "",
2350                vec![make_symbol("JobTest", SymbolKind::Class, 3)],
2351            ),
2352        ]);
2353
2354        let result = search_symbols(&index, "job", None, 50);
2355
2356        assert_eq!(result.file_count, 1);
2357        assert_eq!(result.hits.len(), 1);
2358        assert_eq!(result.hits[0].path, "src/job.rs");
2359        assert_eq!(result.hits[0].name, "Job");
2360    }
2361
2362    #[test]
2363    fn test_search_module_symbol_search_with_options_respects_path_language_and_limit() {
2364        let rust_model = make_file(
2365            "src/models/job.rs",
2366            "",
2367            vec![
2368                make_symbol("Job", SymbolKind::Class, 1),
2369                make_symbol("JobRunner", SymbolKind::Function, 2),
2370            ],
2371        );
2372        let mut ts_ui = make_file(
2373            "src/ui/job.ts",
2374            "",
2375            vec![
2376                make_symbol("JobCard", SymbolKind::Class, 3),
2377                make_symbol("JobList", SymbolKind::Class, 4),
2378            ],
2379        );
2380        ts_ui.1.language = LanguageId::TypeScript;
2381        let noise = make_file(
2382            "tests/job_test.rs",
2383            "",
2384            vec![make_symbol("JobTest", SymbolKind::Function, 5)],
2385        );
2386        let index = make_index(vec![rust_model, ts_ui, noise]);
2387        let options = SymbolSearchOptions {
2388            path_scope: PathScope::prefix("src/ui"),
2389            search_scope: SearchScope::Code,
2390            result_limit: ResultLimit::new(1),
2391            noise_policy: NoisePolicy::permissive(),
2392            include_personal_tooling: true,
2393            language_filter: Some(LanguageId::TypeScript),
2394        };
2395
2396        let result = search_symbols_with_options(&index, "job", Some("class"), &options);
2397
2398        assert_eq!(result.file_count, 1);
2399        assert_eq!(result.hits.len(), 1);
2400        assert_eq!(result.hits[0].path, "src/ui/job.ts");
2401        assert_eq!(result.hits[0].name, "JobCard");
2402        assert_eq!(result.hits[0].kind, "class");
2403    }
2404
2405    #[test]
2406    fn test_search_module_text_search_with_options_respects_scope_and_path() {
2407        let mut text_classification =
2408            crate::domain::FileClassification::for_code_path("docs/readme.md");
2409        text_classification.class = FileClass::Text;
2410        let index = make_index(vec![
2411            make_file_with_classification(
2412                "docs/readme.md",
2413                "needle in docs\n",
2414                Vec::new(),
2415                text_classification,
2416            ),
2417            make_file("src/lib.rs", "needle in code\n", Vec::new()),
2418        ]);
2419        let options = TextSearchOptions {
2420            path_scope: PathScope::prefix("docs/"),
2421            search_scope: SearchScope::Text,
2422            ..Default::default()
2423        };
2424
2425        let result = search_text_with_options(&index, Some("needle"), None, false, &options)
2426            .expect("search should work");
2427
2428        assert_eq!(result.total_matches, 1);
2429        assert_eq!(result.files.len(), 1);
2430        assert_eq!(result.files[0].path, "docs/readme.md");
2431        assert_eq!(result.files[0].matches[0].line, "needle in docs");
2432    }
2433
2434    #[test]
2435    fn test_path_scope_prefix_requires_directory_boundary() {
2436        assert!(PathScope::prefix("src").matches("src/lib.rs"));
2437        assert!(PathScope::prefix("src").matches("src"));
2438        assert!(!PathScope::prefix("src").matches("srcgen/lib.rs"));
2439    }
2440
2441    #[test]
2442    fn test_current_code_symbol_search_options_are_explicit() {
2443        let options = SymbolSearchOptions::for_current_code_search(17);
2444
2445        assert_eq!(options.path_scope, PathScope::Any);
2446        assert_eq!(options.search_scope, SearchScope::Code);
2447        assert_eq!(options.result_limit, ResultLimit::new(17));
2448        assert_eq!(
2449            options.noise_policy,
2450            NoisePolicy {
2451                include_generated: false,
2452                include_tests: false,
2453                include_vendor: true,
2454                include_ignored: false,
2455            }
2456        );
2457        assert_eq!(options.language_filter, None);
2458    }
2459
2460    #[test]
2461    fn test_current_code_text_search_options_are_explicit() {
2462        let options = TextSearchOptions::for_current_code_search();
2463
2464        assert_eq!(options.path_scope, PathScope::Any);
2465        assert_eq!(options.search_scope, SearchScope::Code);
2466        assert_eq!(
2467            options.noise_policy,
2468            NoisePolicy {
2469                include_generated: false,
2470                include_tests: false,
2471                include_vendor: true,
2472                include_ignored: false,
2473            }
2474        );
2475        assert_eq!(options.total_limit, 50);
2476        assert_eq!(options.max_per_file, 5);
2477        assert_eq!(options.language_filter, None);
2478        assert_eq!(options.glob, None);
2479        assert_eq!(options.exclude_glob, None);
2480        assert_eq!(options.context, None);
2481        assert_eq!(options.case_sensitive, None);
2482        assert!(!options.whole_word);
2483    }
2484
2485    #[test]
2486    fn test_search_module_text_search_with_options_respects_language_noise_and_caps() {
2487        let (path_a, mut file_a) = make_file(
2488            "src/app.ts",
2489            "needle one\nneedle two\nneedle three\n",
2490            Vec::new(),
2491        );
2492        file_a.language = LanguageId::TypeScript;
2493
2494        let (path_b, mut file_b) =
2495            make_file("src/lib.ts", "needle four\nneedle five\n", Vec::new());
2496        file_b.language = LanguageId::TypeScript;
2497
2498        let (path_c, file_c) = make_file(
2499            "tests/generated/noisy.ts",
2500            "needle hidden\nneedle hidden two\n",
2501            Vec::new(),
2502        );
2503
2504        let (path_d, file_d) =
2505            make_file("src/lib.rs", "needle rust\nneedle rust two\n", Vec::new());
2506
2507        let index = make_index(vec![
2508            (path_a, file_a),
2509            (path_b, file_b),
2510            (path_c, file_c),
2511            (path_d, file_d),
2512        ]);
2513        let options = TextSearchOptions {
2514            path_scope: PathScope::prefix("src"),
2515            search_scope: SearchScope::Code,
2516            noise_policy: NoisePolicy {
2517                include_generated: false,
2518                include_tests: false,
2519                include_vendor: true,
2520                include_ignored: false,
2521            },
2522            include_personal_tooling: true,
2523            language_filter: Some(LanguageId::TypeScript),
2524            total_limit: 3,
2525            max_per_file: 2,
2526            glob: None,
2527            exclude_glob: None,
2528            context: None,
2529            case_sensitive: None,
2530            whole_word: false,
2531            ranked: false,
2532            churn_scores: None,
2533        };
2534
2535        let result = search_text_with_options(&index, Some("needle"), None, false, &options)
2536            .expect("search should work");
2537
2538        assert_eq!(result.total_matches, 3);
2539        assert_eq!(result.files.len(), 2);
2540        assert_eq!(result.files[0].path, "src/app.ts");
2541        assert_eq!(result.files[0].matches.len(), 2);
2542        assert_eq!(result.files[1].path, "src/lib.ts");
2543        assert_eq!(result.files[1].matches.len(), 1);
2544    }
2545
2546    #[test]
2547    fn test_search_module_text_search_filters_obsidian_but_keeps_wiki_markdown() {
2548        let index = make_index(vec![
2549            make_file("wiki/notes.md", "needle visible\n", Vec::new()),
2550            make_file(
2551                "wiki/.obsidian/workspace.json",
2552                "needle hidden workspace\n",
2553                Vec::new(),
2554            ),
2555            make_file(
2556                ".obsidian/plugins/dataview/styles.css",
2557                "needle hidden style\n",
2558                Vec::new(),
2559            ),
2560        ]);
2561        let options = TextSearchOptions {
2562            include_personal_tooling: false,
2563            total_limit: 10,
2564            max_per_file: 5,
2565            ..TextSearchOptions::default()
2566        };
2567
2568        let result = search_text_with_options(&index, Some("needle"), None, false, &options)
2569            .expect("search should work");
2570
2571        assert_eq!(result.total_matches, 1);
2572        assert_eq!(result.files.len(), 1);
2573        assert_eq!(result.files[0].path, "wiki/notes.md");
2574        assert_eq!(result.suppressed_by_noise, 2);
2575    }
2576
2577    #[test]
2578    fn test_search_module_text_search_with_options_respects_glob_filters() {
2579        let index = make_index(vec![
2580            make_file("src/app.ts", "needle app\n", Vec::new()),
2581            make_file("src/app.spec.ts", "needle spec\n", Vec::new()),
2582            make_file("src/nested/feature.ts", "needle nested\n", Vec::new()),
2583            make_file("src/lib.rs", "needle rust\n", Vec::new()),
2584        ]);
2585        let options = TextSearchOptions {
2586            path_scope: PathScope::prefix("src"),
2587            search_scope: SearchScope::Code,
2588            noise_policy: NoisePolicy {
2589                include_generated: false,
2590                include_tests: false,
2591                include_vendor: true,
2592                include_ignored: false,
2593            },
2594            include_personal_tooling: true,
2595            language_filter: None,
2596            total_limit: 10,
2597            max_per_file: 5,
2598            glob: Some("src/**/*.ts".to_string()),
2599            exclude_glob: Some("**/*.spec.ts".to_string()),
2600            context: None,
2601            case_sensitive: None,
2602            whole_word: false,
2603            ranked: false,
2604            churn_scores: None,
2605        };
2606
2607        let result = search_text_with_options(&index, Some("needle"), None, false, &options)
2608            .expect("search should work");
2609
2610        assert_eq!(result.total_matches, 2);
2611        assert_eq!(result.files.len(), 2);
2612        assert_eq!(result.files[0].path, "src/app.ts");
2613        assert_eq!(result.files[1].path, "src/nested/feature.ts");
2614    }
2615
2616    #[test]
2617    fn test_search_module_text_search_invalid_glob_returns_error() {
2618        let index = make_index(vec![make_file("src/app.ts", "needle app\n", Vec::new())]);
2619        let options = TextSearchOptions {
2620            glob: Some("[".to_string()),
2621            ..TextSearchOptions::for_current_code_search()
2622        };
2623
2624        let result = search_text_with_options(&index, Some("needle"), None, false, &options);
2625
2626        assert!(matches!(result, Err(TextSearchError::InvalidGlob { .. })));
2627    }
2628
2629    #[test]
2630    fn test_search_module_text_search_with_context_merges_windows_and_marks_matches() {
2631        let index = make_index(vec![make_file(
2632            "src/lib.rs",
2633            "line 1\nline 2\nneedle 3\nline 4\nneedle 5\nline 6\nline 7\nline 8\nneedle 9\nline 10\n",
2634            Vec::new(),
2635        )]);
2636        let options = TextSearchOptions {
2637            context: Some(1),
2638            ..TextSearchOptions::for_current_code_search()
2639        };
2640
2641        let result = search_text_with_options(&index, Some("needle"), None, false, &options)
2642            .expect("search should work");
2643
2644        assert_eq!(result.total_matches, 3);
2645        assert_eq!(result.files.len(), 1);
2646        assert_eq!(result.files[0].matches.len(), 3);
2647
2648        let rendered = result.files[0]
2649            .rendered_lines
2650            .as_ref()
2651            .expect("context mode should materialize rendered lines");
2652
2653        assert_eq!(rendered.len(), 9, "expected merged windows plus separator");
2654        assert!(matches!(
2655            rendered[0],
2656            TextDisplayLine::Line(TextRenderedLine {
2657                line_number: 2,
2658                is_match: false,
2659                ..
2660            })
2661        ));
2662        assert!(matches!(
2663            rendered[1],
2664            TextDisplayLine::Line(TextRenderedLine {
2665                line_number: 3,
2666                is_match: true,
2667                ..
2668            })
2669        ));
2670        assert!(matches!(rendered[5], TextDisplayLine::Separator));
2671        assert!(matches!(
2672            rendered[7],
2673            TextDisplayLine::Line(TextRenderedLine {
2674                line_number: 9,
2675                is_match: true,
2676                ..
2677            })
2678        ));
2679    }
2680
2681    #[test]
2682    fn test_search_module_text_search_with_options_respects_case_sensitive_literal_matching() {
2683        let index = make_index(vec![make_file(
2684            "src/lib.rs",
2685            "Needle exact\nneedle lower\n",
2686            Vec::new(),
2687        )]);
2688
2689        let sensitive = search_text_with_options(
2690            &index,
2691            Some("Needle"),
2692            None,
2693            false,
2694            &TextSearchOptions {
2695                case_sensitive: Some(true),
2696                ..TextSearchOptions::for_current_code_search()
2697            },
2698        )
2699        .expect("case-sensitive search should work");
2700
2701        assert_eq!(sensitive.total_matches, 1);
2702        assert_eq!(sensitive.files[0].matches[0].line_number, 1);
2703
2704        let insensitive = search_text_with_options(
2705            &index,
2706            Some("Needle"),
2707            None,
2708            false,
2709            &TextSearchOptions::for_current_code_search(),
2710        )
2711        .expect("default literal search should work");
2712
2713        assert_eq!(insensitive.total_matches, 2);
2714    }
2715
2716    #[test]
2717    fn test_search_module_text_search_with_options_respects_whole_word_boundaries() {
2718        let index = make_index(vec![make_file(
2719            "src/lib.rs",
2720            "needle\nneedle_case\nprefixneedle\nneedle suffix\n",
2721            Vec::new(),
2722        )]);
2723        let result = search_text_with_options(
2724            &index,
2725            Some("needle"),
2726            None,
2727            false,
2728            &TextSearchOptions {
2729                whole_word: true,
2730                ..TextSearchOptions::for_current_code_search()
2731            },
2732        )
2733        .expect("whole-word search should work");
2734
2735        assert_eq!(result.total_matches, 2);
2736        assert_eq!(result.files[0].matches[0].line_number, 1);
2737        assert_eq!(result.files[0].matches[1].line_number, 4);
2738    }
2739
2740    #[test]
2741    fn test_search_module_text_search_with_options_respects_whole_word_terms() {
2742        let index = make_index(vec![make_file(
2743            "src/lib.rs",
2744            "TODO\nTODO_NOTE\nFIXME!\n",
2745            Vec::new(),
2746        )]);
2747        let terms = vec!["todo".to_string(), "fixme".to_string()];
2748        let result = search_text_with_options(
2749            &index,
2750            None,
2751            Some(&terms),
2752            false,
2753            &TextSearchOptions {
2754                whole_word: true,
2755                ..TextSearchOptions::for_current_code_search()
2756            },
2757        )
2758        .expect("whole-word multi-term search should work");
2759
2760        assert_eq!(result.total_matches, 2);
2761        assert_eq!(result.files[0].matches[0].line, "TODO");
2762        assert_eq!(result.files[0].matches[1].line, "FIXME!");
2763    }
2764
2765    #[test]
2766    fn test_search_module_text_search_regex_can_opt_into_case_insensitive_matching() {
2767        let index = make_index(vec![make_file("src/lib.rs", "needle lower\n", Vec::new())]);
2768        let result = search_text_with_options(
2769            &index,
2770            Some("Needle"),
2771            None,
2772            true,
2773            &TextSearchOptions {
2774                case_sensitive: Some(false),
2775                ..TextSearchOptions::for_current_code_search()
2776            },
2777        )
2778        .expect("case-insensitive regex search should work");
2779
2780        assert_eq!(result.total_matches, 1);
2781        assert_eq!(result.files[0].matches[0].line, "needle lower");
2782    }
2783
2784    #[test]
2785    fn test_search_module_text_search_rejects_regex_whole_word_combination() {
2786        let index = make_index(vec![make_file("src/lib.rs", "needle lower\n", Vec::new())]);
2787        let result = search_text_with_options(
2788            &index,
2789            Some("needle"),
2790            None,
2791            true,
2792            &TextSearchOptions {
2793                whole_word: true,
2794                ..TextSearchOptions::for_current_code_search()
2795            },
2796        );
2797
2798        assert!(matches!(
2799            result,
2800            Err(TextSearchError::UnsupportedWholeWordRegex)
2801        ));
2802    }
2803
2804    #[test]
2805    fn test_explicit_path_read_options_are_exact() {
2806        let options = FileContentOptions::for_explicit_path_read("src/lib.rs", Some(2), Some(4));
2807
2808        assert_eq!(
2809            options.path_scope,
2810            PathScope::Exact("src/lib.rs".to_string())
2811        );
2812        assert_eq!(
2813            options.content_context,
2814            ContentContext::line_range(Some(2), Some(4))
2815        );
2816    }
2817
2818    #[test]
2819    fn test_explicit_path_read_options_preserve_format_flags() {
2820        let options = FileContentOptions::for_explicit_path_read_with_format(
2821            "src/lib.rs",
2822            Some(2),
2823            Some(4),
2824            true,
2825            true,
2826        );
2827
2828        assert_eq!(
2829            options.path_scope,
2830            PathScope::Exact("src/lib.rs".to_string())
2831        );
2832        assert_eq!(
2833            options.content_context,
2834            ContentContext::line_range_with_format(Some(2), Some(4), true, true)
2835        );
2836    }
2837
2838    #[test]
2839    fn test_explicit_path_read_around_line_options_are_exact() {
2840        let options = FileContentOptions::for_explicit_path_read_around_line(
2841            "src/lib.rs",
2842            3,
2843            Some(1),
2844            false,
2845            false,
2846        );
2847
2848        assert_eq!(
2849            options.path_scope,
2850            PathScope::Exact("src/lib.rs".to_string())
2851        );
2852        assert_eq!(
2853            options.content_context,
2854            ContentContext::around_line(3, Some(1), false, false)
2855        );
2856    }
2857
2858    #[test]
2859    fn test_explicit_path_read_around_match_options_are_exact() {
2860        let options = FileContentOptions::for_explicit_path_read_around_match(
2861            "src/lib.rs",
2862            "needle",
2863            None,
2864            Some(1),
2865            false,
2866            false,
2867        );
2868
2869        assert_eq!(
2870            options.path_scope,
2871            PathScope::Exact("src/lib.rs".to_string())
2872        );
2873        assert_eq!(
2874            options.content_context,
2875            ContentContext::around_match("needle", Some(1), false, false)
2876        );
2877    }
2878
2879    #[test]
2880    fn test_explicit_path_read_chunk_options_are_exact() {
2881        let options = FileContentOptions::for_explicit_path_read_chunk("src/lib.rs", 2, 2);
2882
2883        assert_eq!(
2884            options.path_scope,
2885            PathScope::Exact("src/lib.rs".to_string())
2886        );
2887        assert_eq!(options.content_context, ContentContext::chunk(2, 2));
2888    }
2889
2890    #[test]
2891    fn test_explicit_path_read_around_symbol_options_are_exact() {
2892        let options = FileContentOptions::for_explicit_path_read_around_symbol(
2893            "src/lib.rs",
2894            "connect",
2895            Some(3),
2896            Some(1),
2897            None,
2898            false,
2899            false,
2900        );
2901
2902        assert_eq!(
2903            options.path_scope,
2904            PathScope::Exact("src/lib.rs".to_string())
2905        );
2906        assert_eq!(
2907            options.content_context,
2908            ContentContext::around_symbol("connect", Some(3), Some(1))
2909        );
2910    }
2911
2912    // ── NoiseClass + NoisePolicy tests ──────────────────────────────────
2913
2914    #[test]
2915    fn test_noise_policy_classifies_vendor_paths() {
2916        assert_eq!(
2917            NoisePolicy::classify_path("vendor/github.com/pkg/errors/errors.go", None),
2918            NoiseClass::Vendor
2919        );
2920        assert_eq!(
2921            NoisePolicy::classify_path("node_modules/lodash/index.js", None),
2922            NoiseClass::Vendor
2923        );
2924        assert_eq!(
2925            NoisePolicy::classify_path("third_party/protobuf/src/lib.rs", None),
2926            NoiseClass::Vendor
2927        );
2928        assert_eq!(
2929            NoisePolicy::classify_path(".venv/lib/site-packages/flask/app.py", None),
2930            NoiseClass::Vendor
2931        );
2932        assert_eq!(
2933            NoisePolicy::classify_path("bower_components/jquery/jquery.js", None),
2934            NoiseClass::Vendor
2935        );
2936    }
2937
2938    #[test]
2939    fn test_noise_policy_classifies_generated_paths() {
2940        assert_eq!(
2941            NoisePolicy::classify_path("Cargo.lock", None),
2942            NoiseClass::Generated
2943        );
2944        assert_eq!(
2945            NoisePolicy::classify_path("package-lock.json", None),
2946            NoiseClass::Generated
2947        );
2948        assert_eq!(
2949            NoisePolicy::classify_path("assets/app.min.js", None),
2950            NoiseClass::Generated
2951        );
2952        assert_eq!(
2953            NoisePolicy::classify_path("styles/theme.min.css", None),
2954            NoiseClass::Generated
2955        );
2956        assert_eq!(
2957            NoisePolicy::classify_path("dist/bundle.js", None),
2958            NoiseClass::Generated
2959        );
2960        assert_eq!(
2961            NoisePolicy::classify_path("src/generated/api.rs", None),
2962            NoiseClass::Generated
2963        );
2964        assert_eq!(
2965            NoisePolicy::classify_path("proto/service.pb.go", None),
2966            NoiseClass::Generated
2967        );
2968        assert_eq!(
2969            NoisePolicy::classify_path("lib/model.g.dart", None),
2970            NoiseClass::Generated
2971        );
2972        assert_eq!(
2973            NoisePolicy::classify_path("Forms/Form1.Designer.cs", None),
2974            NoiseClass::Generated
2975        );
2976    }
2977
2978    /// SF-STRESS-011: the broadened vendor/generated/test segment sets must be
2979    /// recognized, and `NoiseClass::classify_path` must AGREE with
2980    /// `FileClassification::for_code_path` on the same paths (they share the
2981    /// segment-set source of truth) so search and explore no longer disagree.
2982    #[test]
2983    fn test_noise_classifiers_agree_on_corpus_paths() {
2984        use crate::domain::FileClassification;
2985
2986        // deps/ vendored dependency (redis, node) — previously an undocumented gap.
2987        assert_eq!(
2988            NoisePolicy::classify_path("deps/hiredis/sds.c", None),
2989            NoiseClass::Vendor
2990        );
2991        assert!(FileClassification::for_code_path("deps/hiredis/sds.c").is_vendor);
2992
2993        // dist/ build output (laravel) — now shared by both classifiers.
2994        assert_eq!(
2995            NoisePolicy::classify_path("dist/css/app.css", None),
2996            NoiseClass::Generated
2997        );
2998        assert!(FileClassification::for_code_path("dist/css/app.css").is_generated);
2999
3000        // .min.css / .map generated assets (mojo bootstrap.css, source maps).
3001        assert_eq!(
3002            NoisePolicy::classify_path("public/bootstrap.min.css", None),
3003            NoiseClass::Generated
3004        );
3005        assert!(FileClassification::for_code_path("public/bootstrap.min.css").is_generated);
3006        assert!(FileClassification::for_code_path("dist/app.js.map").is_generated);
3007
3008        // test_data / __snapshots__ fixtures (rust-analyzer) — FileClassification
3009        // is the test-aware classifier; classify_path does not model tests, so we
3010        // only assert FileClassification here.
3011        assert!(FileClassification::for_code_path("crates/parser/test_data/err/0001.rs").is_test);
3012        assert!(
3013            FileClassification::for_code_path("src/__snapshots__/Button.test.tsx.snap").is_test
3014        );
3015    }
3016
3017    #[test]
3018    fn test_noise_policy_classifies_normal_paths() {
3019        assert_eq!(
3020            NoisePolicy::classify_path("src/main.rs", None),
3021            NoiseClass::None
3022        );
3023        assert_eq!(
3024            NoisePolicy::classify_path("lib/utils.ts", None),
3025            NoiseClass::None
3026        );
3027    }
3028
3029    #[test]
3030    fn test_noise_policy_should_hide_respects_flags() {
3031        let restrictive = NoisePolicy::hide_classified_noise();
3032        assert!(!restrictive.should_hide(NoiseClass::None));
3033        assert!(restrictive.should_hide(NoiseClass::Vendor));
3034        assert!(restrictive.should_hide(NoiseClass::Generated));
3035        assert!(restrictive.should_hide(NoiseClass::Ignored));
3036
3037        let permissive = NoisePolicy::permissive();
3038        assert!(!permissive.should_hide(NoiseClass::None));
3039        assert!(!permissive.should_hide(NoiseClass::Vendor));
3040        assert!(!permissive.should_hide(NoiseClass::Generated));
3041        assert!(!permissive.should_hide(NoiseClass::Ignored));
3042
3043        // Custom: include vendor but not generated
3044        let custom = NoisePolicy {
3045            include_vendor: true,
3046            include_generated: false,
3047            include_tests: true,
3048            include_ignored: false,
3049        };
3050        assert!(!custom.should_hide(NoiseClass::None));
3051        assert!(!custom.should_hide(NoiseClass::Vendor));
3052        assert!(custom.should_hide(NoiseClass::Generated));
3053        assert!(custom.should_hide(NoiseClass::Ignored)); // Ignored follows include_ignored
3054    }
3055
3056    #[test]
3057    fn test_noise_class_tag() {
3058        assert_eq!(NoiseClass::None.tag(), "");
3059        assert_eq!(NoiseClass::Vendor.tag(), "[vendor]");
3060        assert_eq!(NoiseClass::Generated.tag(), "[generated]");
3061        assert_eq!(NoiseClass::Ignored.tag(), "[ignored]");
3062    }
3063
3064    #[test]
3065    fn test_gitignore_vendor_classified() {
3066        use ignore::gitignore::GitignoreBuilder;
3067
3068        let tmp = tempfile::tempdir().unwrap();
3069        let mut builder = GitignoreBuilder::new(tmp.path());
3070        builder.add_line(None, "build/").unwrap();
3071        builder.add_line(None, "*.log").unwrap();
3072        let gi = builder.build().unwrap();
3073
3074        // "build/output.js" matches gitignore but not vendor/generated heuristic → Ignored
3075        assert_eq!(
3076            NoisePolicy::classify_path("build/output.js", Some(&gi)),
3077            NoiseClass::Ignored
3078        );
3079        // "debug.log" matches gitignore → Ignored
3080        assert_eq!(
3081            NoisePolicy::classify_path("debug.log", Some(&gi)),
3082            NoiseClass::Ignored
3083        );
3084        // "src/main.rs" does NOT match gitignore → None
3085        assert_eq!(
3086            NoisePolicy::classify_path("src/main.rs", Some(&gi)),
3087            NoiseClass::None
3088        );
3089        // Vendor heuristic takes priority over gitignore
3090        assert_eq!(
3091            NoisePolicy::classify_path("node_modules/pkg/index.js", Some(&gi)),
3092            NoiseClass::Vendor
3093        );
3094    }
3095
3096    #[test]
3097    fn test_gitignore_negation_exempts_file() {
3098        use ignore::gitignore::GitignoreBuilder;
3099
3100        let tmp = tempfile::tempdir().unwrap();
3101        let mut builder = GitignoreBuilder::new(tmp.path());
3102        builder.add_line(None, "build/").unwrap();
3103        builder.add_line(None, "!build/important.js").unwrap();
3104        let gi = builder.build().unwrap();
3105
3106        // Negated file should NOT be classified as noise
3107        assert_eq!(
3108            NoisePolicy::classify_path("build/important.js", Some(&gi)),
3109            NoiseClass::None
3110        );
3111        // Non-negated file in same dir should still be noise
3112        assert_eq!(
3113            NoisePolicy::classify_path("build/other.js", Some(&gi)),
3114            NoiseClass::Ignored
3115        );
3116    }
3117
3118    #[test]
3119    fn test_symbol_kind_priority() {
3120        assert_eq!(symbol_kind_priority("function"), 1.0);
3121        assert_eq!(symbol_kind_priority("method"), 1.0);
3122        assert_eq!(symbol_kind_priority("async_function"), 1.0);
3123        assert_eq!(symbol_kind_priority("struct"), 0.8);
3124        assert_eq!(symbol_kind_priority("class"), 0.8);
3125        assert_eq!(symbol_kind_priority("enum"), 0.8);
3126        assert_eq!(symbol_kind_priority("trait"), 0.8);
3127        assert_eq!(symbol_kind_priority("impl"), 0.7);
3128        assert_eq!(symbol_kind_priority("module"), 0.5);
3129        assert_eq!(symbol_kind_priority("constant"), 0.4);
3130        assert_eq!(symbol_kind_priority("variable"), 0.3);
3131        assert_eq!(symbol_kind_priority("key"), 0.2);
3132        assert!(symbol_kind_priority("unknown") > 0.0);
3133        assert_eq!(symbol_kind_priority("unknown"), 0.1);
3134    }
3135
3136    #[test]
3137    fn test_ranked_reorders_by_importance() {
3138        use crate::live_index::store::ReferenceLocation;
3139
3140        // Create two TextFileMatches: one with many callers, one with few.
3141        let file_a = TextFileMatches {
3142            path: "a.rs".to_string(),
3143            matches: vec![TextLineMatch {
3144                line_number: 10,
3145                line: "let x = 1;".to_string(),
3146                enclosing_symbol: Some(EnclosingMatchSymbol {
3147                    name: "dead_code_fn".to_string(),
3148                    kind: "function".to_string(),
3149                    line_range: (1, 20),
3150                }),
3151            }],
3152            rendered_lines: None,
3153            callers: None,
3154        };
3155
3156        let file_b = TextFileMatches {
3157            path: "b.rs".to_string(),
3158            matches: vec![TextLineMatch {
3159                line_number: 5,
3160                line: "let y = 2;".to_string(),
3161                enclosing_symbol: Some(EnclosingMatchSymbol {
3162                    name: "popular_fn".to_string(),
3163                    kind: "function".to_string(),
3164                    line_range: (1, 10),
3165                }),
3166            }],
3167            rendered_lines: None,
3168            callers: None,
3169        };
3170
3171        // Build a reverse index where popular_fn has 20 callers, dead_code_fn has 0.
3172        let mut reverse_index: HashMap<String, Vec<ReferenceLocation>> = HashMap::new();
3173        let refs: Vec<ReferenceLocation> = (0..20)
3174            .map(|i| ReferenceLocation {
3175                file_path: format!("caller_{}.rs", i),
3176                reference_idx: 0,
3177            })
3178            .collect();
3179        reverse_index.insert("popular_fn".to_string(), refs);
3180        // dead_code_fn has no entries in reverse_index.
3181
3182        let match_count_max = 1;
3183
3184        let score_a = compute_importance_score(&file_a, match_count_max, &reverse_index, 0.0);
3185        let score_b = compute_importance_score(&file_b, match_count_max, &reverse_index, 0.0);
3186
3187        // file_b (popular_fn) should score higher than file_a (dead_code_fn)
3188        assert!(
3189            score_b > score_a,
3190            "Expected popular_fn score ({}) > dead_code_fn score ({})",
3191            score_b,
3192            score_a,
3193        );
3194    }
3195
3196    #[test]
3197    fn test_compute_importance_score_with_churn() {
3198        use crate::live_index::store::ReferenceLocation;
3199
3200        let file = TextFileMatches {
3201            path: "c.rs".to_string(),
3202            matches: vec![TextLineMatch {
3203                line_number: 1,
3204                line: "test".to_string(),
3205                enclosing_symbol: Some(EnclosingMatchSymbol {
3206                    name: "some_fn".to_string(),
3207                    kind: "function".to_string(),
3208                    line_range: (1, 5),
3209                }),
3210            }],
3211            rendered_lines: None,
3212            callers: None,
3213        };
3214
3215        let reverse_index: HashMap<String, Vec<ReferenceLocation>> = HashMap::new();
3216
3217        let score_no_churn = compute_importance_score(&file, 1, &reverse_index, 0.0);
3218        let score_high_churn = compute_importance_score(&file, 1, &reverse_index, 1.0);
3219
3220        // Higher churn should increase the score.
3221        assert!(
3222            score_high_churn > score_no_churn,
3223            "Expected high churn score ({}) > no churn score ({})",
3224            score_high_churn,
3225            score_no_churn,
3226        );
3227    }
3228
3229    // ── Frecency fusion ────────────────────────────────────────────────────
3230
3231    fn make_file_hit(path: &str, tier: SearchFilesTier) -> SearchFilesHit {
3232        SearchFilesHit {
3233            tier,
3234            path: path.to_string(),
3235            coupling_score: None,
3236            shared_commits: None,
3237            metadata_reason: None,
3238        }
3239    }
3240
3241    #[test]
3242    fn tier_path_match_score_maps_tiers_to_expected_weights() {
3243        assert_eq!(tier_path_match_score(SearchFilesTier::StrongPath), 1.0);
3244        assert_eq!(tier_path_match_score(SearchFilesTier::Basename), 0.6);
3245        assert_eq!(tier_path_match_score(SearchFilesTier::LoosePath), 0.3);
3246        assert_eq!(tier_path_match_score(SearchFilesTier::CoChange), 0.0);
3247    }
3248
3249    #[test]
3250    fn score_hits_by_frecency_fusion_returns_zero_frecency_when_store_empty() {
3251        let hits = vec![
3252            make_file_hit("src/a.rs", SearchFilesTier::StrongPath),
3253            make_file_hit("src/b.rs", SearchFilesTier::Basename),
3254        ];
3255        let scores: HashMap<PathBuf, f64> = HashMap::new();
3256        let breakdowns = score_hits_by_frecency_fusion(&hits, &scores);
3257        assert_eq!(breakdowns.len(), 2);
3258        for b in &breakdowns {
3259            assert_eq!(b.frecency_normalized, 0.0);
3260        }
3261        // combined = 0.6 * path_match (co_change and frecency both 0)
3262        assert!((breakdowns[0].combined - 0.6).abs() < f64::EPSILON);
3263        assert!((breakdowns[1].combined - 0.36).abs() < f64::EPSILON);
3264    }
3265
3266    #[test]
3267    fn score_hits_by_frecency_fusion_normalizes_against_max_in_set() {
3268        let hits = vec![
3269            make_file_hit("src/peak.rs", SearchFilesTier::LoosePath),
3270            make_file_hit("src/mid.rs", SearchFilesTier::LoosePath),
3271            make_file_hit("src/cold.rs", SearchFilesTier::LoosePath),
3272        ];
3273        let mut scores = HashMap::new();
3274        scores.insert(PathBuf::from("src/peak.rs"), 10.0);
3275        scores.insert(PathBuf::from("src/mid.rs"), 2.5);
3276        // "src/cold.rs" intentionally missing — treated as 0.
3277        let breakdowns = score_hits_by_frecency_fusion(&hits, &scores);
3278        assert!((breakdowns[0].frecency_normalized - 1.0).abs() < f64::EPSILON);
3279        assert!((breakdowns[1].frecency_normalized - 0.25).abs() < f64::EPSILON);
3280        assert_eq!(breakdowns[2].frecency_normalized, 0.0);
3281    }
3282
3283    #[test]
3284    fn score_hits_by_frecency_fusion_applies_weights_060_030_010() {
3285        // Single candidate with a full-score path_match, nonzero co_change,
3286        // and the only frecency row (→ normalized == 1.0). Final combined
3287        // must equal 0.6 + 0.3 * 0.5 + 0.1 = 0.85.
3288        let mut hit = make_file_hit("src/star.rs", SearchFilesTier::StrongPath);
3289        hit.coupling_score = Some(0.5);
3290        let hits = vec![hit];
3291        let mut scores = HashMap::new();
3292        scores.insert(PathBuf::from("src/star.rs"), 1.0);
3293        let breakdowns = score_hits_by_frecency_fusion(&hits, &scores);
3294        let b = breakdowns[0];
3295        assert!((b.path_match - 1.0).abs() < f64::EPSILON);
3296        assert!((b.co_change - 0.5).abs() < f64::EPSILON);
3297        assert!((b.frecency_normalized - 1.0).abs() < f64::EPSILON);
3298        assert!((b.combined - (0.6 + 0.15 + 0.1)).abs() < f64::EPSILON);
3299    }
3300
3301    #[test]
3302    fn reorder_hits_by_frecency_fusion_sorts_by_combined_descending() {
3303        let hits = vec![
3304            make_file_hit("src/c.rs", SearchFilesTier::LoosePath),
3305            make_file_hit("src/a.rs", SearchFilesTier::LoosePath),
3306            make_file_hit("src/b.rs", SearchFilesTier::LoosePath),
3307        ];
3308        // All three tied on path_match (LoosePath → 0.3). Give each a
3309        // different raw frecency so the normalized contribution breaks
3310        // ties and re-orders the set.
3311        let mut scores = HashMap::new();
3312        scores.insert(PathBuf::from("src/c.rs"), 1.0);
3313        scores.insert(PathBuf::from("src/a.rs"), 10.0);
3314        scores.insert(PathBuf::from("src/b.rs"), 5.0);
3315        let breakdowns = score_hits_by_frecency_fusion(&hits, &scores);
3316        let reordered = reorder_hits_by_frecency_fusion(hits, &breakdowns);
3317        assert_eq!(reordered[0].path, "src/a.rs");
3318        assert_eq!(reordered[1].path, "src/b.rs");
3319        assert_eq!(reordered[2].path, "src/c.rs");
3320    }
3321
3322    #[test]
3323    fn reorder_hits_by_frecency_fusion_breaks_combined_ties_by_path() {
3324        // Two hits with identical tier and no frecency → identical combined.
3325        // Tiebreak must be stable-alphabetical by path for determinism.
3326        let hits = vec![
3327            make_file_hit("src/z.rs", SearchFilesTier::Basename),
3328            make_file_hit("src/a.rs", SearchFilesTier::Basename),
3329        ];
3330        let scores: HashMap<PathBuf, f64> = HashMap::new();
3331        let breakdowns = score_hits_by_frecency_fusion(&hits, &scores);
3332        let reordered = reorder_hits_by_frecency_fusion(hits, &breakdowns);
3333        assert_eq!(reordered[0].path, "src/a.rs");
3334        assert_eq!(reordered[1].path, "src/z.rs");
3335    }
3336
3337    #[test]
3338    fn recent_single_bump_outranks_old_ten_bumps_via_fusion_math() {
3339        // Spec fixture: "File touched 5 min ago outranks file touched 6
3340        // months ago with 10× hits". Pinned here at the fusion-math layer
3341        // (no DB involved) so the math stays correct even before the
3342        // end-to-end integration test is wired.
3343        //
3344        // Decay uses 7-day half-life. 5 minutes is negligible decay
3345        // → score_b ≈ 1.0. 6 months ≈ 26 half-lives → score_a ≈ 10 * 2^-26
3346        // ≈ 1.5e-7. After normalizing against max (== score_b), file_a's
3347        // contribution is essentially 0 and file_b's is 1.0.
3348        let hits = vec![
3349            make_file_hit("src/file_a.rs", SearchFilesTier::LoosePath),
3350            make_file_hit("src/file_b.rs", SearchFilesTier::LoosePath),
3351        ];
3352        let mut scores = HashMap::new();
3353        scores.insert(PathBuf::from("src/file_a.rs"), 1.5e-7);
3354        scores.insert(PathBuf::from("src/file_b.rs"), 1.0);
3355        let breakdowns = score_hits_by_frecency_fusion(&hits, &scores);
3356        let reordered = reorder_hits_by_frecency_fusion(hits, &breakdowns);
3357        assert_eq!(
3358            reordered[0].path, "src/file_b.rs",
3359            "recent single bump must outrank old 10x bumps"
3360        );
3361    }
3362
3363    /// Regression: Unit 5 originally bundled "every file was an
3364    /// unsupported config language" with "every file rejected the pattern
3365    /// syntax." The TOML-only case below proves the former path: we must
3366    /// get `UnsupportedStructuralLanguage`, not a generic "invalid pattern"
3367    /// error that would mislead the caller into thinking their pattern is
3368    /// broken.
3369    #[test]
3370    fn search_structural_surfaces_unsupported_language_when_only_config_files_indexed() {
3371        let toml_content = "[package]\nname = \"thing\"\n";
3372        let file = IndexedFile {
3373            relative_path: "Cargo.toml".to_string(),
3374            language: LanguageId::Toml,
3375            classification: crate::domain::FileClassification::for_code_path("Cargo.toml"),
3376            content: toml_content.as_bytes().to_vec(),
3377            symbols: Vec::new(),
3378            parse_status: ParseStatus::Parsed,
3379            parse_diagnostic: None,
3380            byte_len: toml_content.len() as u64,
3381            content_hash: "hash".to_string(),
3382            references: Vec::new(),
3383            alias_map: HashMap::new(),
3384            mtime_secs: 0,
3385        };
3386        let index = make_index(vec![("Cargo.toml".to_string(), file)]);
3387
3388        let options = TextSearchOptions::default();
3389        let pattern = "fn $NAME($$$) { $$$ }";
3390        let result = search_structural(&index, pattern, &options);
3391
3392        match result {
3393            Err(TextSearchError::UnsupportedStructuralLanguage {
3394                pattern: err_pattern,
3395                sample_error,
3396            }) => {
3397                assert_eq!(err_pattern, pattern);
3398                assert!(
3399                    sample_error.contains("not supported"),
3400                    "sample error must quote the underlying language rejection; got: {sample_error}"
3401                );
3402            }
3403            Err(TextSearchError::InvalidStructuralPattern { .. }) => panic!(
3404                "regression: TOML-only index must not masquerade as an invalid-pattern error"
3405            ),
3406            Err(other) => panic!("expected UnsupportedStructuralLanguage, got {other:?}"),
3407            Ok(result) => panic!(
3408                "expected error propagation, got Ok with {} match(es) in {} file(s)",
3409                result.total_matches,
3410                result.files.len()
3411            ),
3412        }
3413    }
3414
3415    /// Regression: with a mixed index (a supported language plus a config
3416    /// language), a pattern that `Pattern::try_new` rejects as syntactically
3417    /// invalid must surface as `InvalidStructuralPattern`, not masked behind
3418    /// the config-language error that would otherwise populate
3419    /// `first_unsupported_error`. Syntax errors win.
3420    #[test]
3421    fn search_structural_prefers_syntax_error_over_unsupported_language() {
3422        // Rust file — supported by ast-grep.
3423        let rust_content = "fn main() { println!(\"hi\"); }\n";
3424        let rust_file = IndexedFile {
3425            relative_path: "src/a.rs".to_string(),
3426            language: LanguageId::Rust,
3427            classification: crate::domain::FileClassification::for_code_path("src/a.rs"),
3428            content: rust_content.as_bytes().to_vec(),
3429            symbols: Vec::new(),
3430            parse_status: ParseStatus::Parsed,
3431            parse_diagnostic: None,
3432            byte_len: rust_content.len() as u64,
3433            content_hash: "hash".to_string(),
3434            references: Vec::new(),
3435            alias_map: HashMap::new(),
3436            mtime_secs: 0,
3437        };
3438        // TOML file — sorts before src/a.rs, so without syntax-wins logic
3439        // its "not supported" error would populate first_*_error first.
3440        let toml_content = "[package]\nname = \"x\"\n";
3441        let toml_file = IndexedFile {
3442            relative_path: "Cargo.toml".to_string(),
3443            language: LanguageId::Toml,
3444            classification: crate::domain::FileClassification::for_code_path("Cargo.toml"),
3445            content: toml_content.as_bytes().to_vec(),
3446            symbols: Vec::new(),
3447            parse_status: ParseStatus::Parsed,
3448            parse_diagnostic: None,
3449            byte_len: toml_content.len() as u64,
3450            content_hash: "hash".to_string(),
3451            references: Vec::new(),
3452            alias_map: HashMap::new(),
3453            mtime_secs: 0,
3454        };
3455        let index = make_index(vec![
3456            ("Cargo.toml".to_string(), toml_file),
3457            ("src/a.rs".to_string(), rust_file),
3458        ]);
3459
3460        // Empty pattern is the most portable "definitely invalid" input we
3461        // can pass to ast-grep without depending on grammar-specific quirks.
3462        let options = TextSearchOptions::default();
3463        let bad_pattern = "";
3464        let result = search_structural(&index, bad_pattern, &options);
3465
3466        match result {
3467            Err(TextSearchError::InvalidStructuralPattern { .. }) => {
3468                // Correct — syntax error propagated despite the TOML file
3469                // contributing an earlier "unsupported" error.
3470            }
3471            Err(TextSearchError::UnsupportedStructuralLanguage { .. }) => {
3472                // If ast-grep happens to accept the empty pattern against
3473                // Rust and returns zero matches, we'd hit Ok below, not
3474                // this branch — so reaching here would mean we regressed to
3475                // the old conflation bug.
3476                panic!(
3477                    "regression: syntax-error pattern must not be masked by the TOML \
3478                     file's unsupported-language error"
3479                );
3480            }
3481            Err(other) => panic!("unexpected error variant: {other:?}"),
3482            Ok(_) => {
3483                // ast-grep accepted the empty pattern; skip silently. The
3484                // intent of this test only holds when Pattern::try_new
3485                // rejects the chosen "bad_pattern". If ast-grep is more
3486                // lenient than assumed, this test simply does not exercise
3487                // the precedence path — the TOML-only test still proves
3488                // UnsupportedStructuralLanguage is wired.
3489            }
3490        }
3491    }
3492
3493    #[test]
3494    fn search_structural_zero_match_result_surfaces_structural_label() {
3495        // Valid pattern, but nothing matches in the indexed content.
3496        // Result should be Ok with empty files, and label must identify it
3497        // as structural so the renderer can give a structural-aware hint.
3498        let (path, file) = make_file(
3499            "src/a.rs",
3500            "fn main() { println!(\"hi\"); }\n",
3501            vec![make_symbol("main", SymbolKind::Function, 1)],
3502        );
3503        let index = make_index(vec![(path, file)]);
3504
3505        let options = TextSearchOptions::default();
3506        // Rust-specific pattern that won't match the `fn main` body above.
3507        let result = search_structural(&index, "struct $NAME { $$$ }", &options)
3508            .expect("valid pattern must not error");
3509        assert_eq!(result.total_matches, 0);
3510        assert!(result.files.is_empty());
3511        assert!(
3512            result.label.starts_with("structural "),
3513            "label must start with `structural ` so the renderer can detect structural searches; got: {}",
3514            result.label
3515        );
3516    }
3517
3518    #[test]
3519    fn search_structural_reuses_compiled_pattern_per_language_per_request() {
3520        let pattern = "fn $NAME($$$) { $$$ }";
3521        let index = make_index(vec![
3522            make_file(
3523                "src/a.rs",
3524                "fn alpha() {}\n",
3525                vec![make_symbol("alpha", SymbolKind::Function, 1)],
3526            ),
3527            make_file(
3528                "src/b.rs",
3529                "fn beta() {}\n",
3530                vec![make_symbol("beta", SymbolKind::Function, 1)],
3531            ),
3532            make_file(
3533                "src/c.rs",
3534                "fn gamma() {}\n",
3535                vec![make_symbol("gamma", SymbolKind::Function, 1)],
3536            ),
3537        ]);
3538
3539        let mut compile_calls_by_language: HashMap<LanguageId, usize> = HashMap::new();
3540        let result = search_structural_with_compiler(
3541            &index,
3542            pattern,
3543            &TextSearchOptions::default(),
3544            |pattern, lang, is_tsx| {
3545                *compile_calls_by_language.entry(lang.clone()).or_default() += 1;
3546                crate::parsing::ast_grep::compile_structural_pattern(pattern, lang, is_tsx)
3547            },
3548        )
3549        .expect("valid structural pattern should search all candidate files");
3550
3551        assert_eq!(result.total_matches, 3);
3552        assert_eq!(compile_calls_by_language.get(&LanguageId::Rust), Some(&1));
3553    }
3554}