1use std::collections::{HashMap, HashSet};
27use std::path::{Path, PathBuf};
28use std::sync::{Arc, Mutex, OnceLock};
29use std::time::{Duration, Instant};
30
31use glob::Pattern;
32use ignore::WalkBuilder;
33
34use crate::tools::ctx_search::{MAX_FILE_SIZE, MAX_WALK_DEPTH, is_binary_ext, is_generated_file};
35
36const TTL: Duration = Duration::from_secs(15);
39
40const MAX_FILES: usize = 200_000;
42
43const MAX_POSTING_ENTRIES: usize = 12_000_000;
48
49const MAX_TOTAL_ENTRIES: usize = 48_000_000;
53
54const BLOOM_BITS_PER_ITEM: usize = 12;
58const BLOOM_K: usize = 7;
59const BLOOM_MIN_BITS: usize = 64;
61const BLOOM_MAX_BITS: usize = 1 << 20;
62
63fn is_word_byte(b: u8) -> bool {
65 b.is_ascii_alphanumeric() || b == b'_'
66}
67
68fn pack(b0: u8, b1: u8, b2: u8) -> u32 {
69 (u32::from(b0) << 16) | (u32::from(b1) << 8) | u32::from(b2)
70}
71
72enum Narrowing {
81 Postings(HashMap<u32, Vec<u32>>),
82 Blooms(Vec<FileBloom>),
83}
84
85struct FileBloom {
88 bits: Vec<u64>,
90}
91
92#[inline]
95fn mix64(mut x: u64) -> u64 {
96 x = x.wrapping_add(0x9E37_79B9_7F4A_7C15);
97 x = (x ^ (x >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
98 x = (x ^ (x >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
99 x ^ (x >> 31)
100}
101
102impl FileBloom {
103 fn with_capacity(distinct_trigrams: usize) -> Self {
104 let target = distinct_trigrams
105 .saturating_mul(BLOOM_BITS_PER_ITEM)
106 .next_power_of_two()
107 .clamp(BLOOM_MIN_BITS, BLOOM_MAX_BITS);
108 FileBloom {
109 bits: vec![0u64; target / 64],
110 }
111 }
112
113 #[inline]
114 fn m_bits(&self) -> usize {
115 self.bits.len() * 64
116 }
117
118 #[inline]
120 fn probes(&self, trigram: u32) -> impl Iterator<Item = usize> + '_ {
121 let m = self.m_bits();
122 let mask = m - 1; let h = mix64(u64::from(trigram));
124 let h1 = (h & 0xFFFF_FFFF) as usize;
125 let h2 = ((h >> 32) as usize) | 1; (0..BLOOM_K).map(move |i| h1.wrapping_add(i.wrapping_mul(h2)) & mask)
127 }
128
129 fn insert(&mut self, trigram: u32) {
130 for p in self.probes(trigram).collect::<Vec<_>>() {
131 self.bits[p / 64] |= 1u64 << (p % 64);
132 }
133 }
134
135 fn maybe_contains(&self, trigram: u32) -> bool {
136 self.probes(trigram)
137 .all(|p| self.bits[p / 64] & (1u64 << (p % 64)) != 0)
138 }
139}
140
141pub struct SearchIndex {
143 files: Vec<PathBuf>,
144 narrowing: Narrowing,
146 respect_gitignore: bool,
147 allow_secret_paths: bool,
148 built_at: Instant,
149}
150
151impl SearchIndex {
152 pub fn build(root: &str, respect_gitignore: bool, allow_secret_paths: bool) -> Option<Self> {
155 let root_path = Path::new(root);
156 if !root_path.exists() {
157 return None;
158 }
159 if !crate::core::graph_index::is_safe_scan_root_public(root) {
164 return None;
165 }
166
167 let walker = WalkBuilder::new(root_path)
168 .hidden(true)
169 .max_depth(Some(MAX_WALK_DEPTH))
170 .git_ignore(respect_gitignore)
171 .git_global(respect_gitignore)
172 .git_exclude(respect_gitignore)
173 .require_git(false)
174 .filter_entry(crate::core::walk_filter::keep_entry)
175 .build();
176
177 let mut files: Vec<PathBuf> = Vec::new();
178 let mut per_file_trigrams: Vec<Vec<u32>> = Vec::new();
182 let mut total_entries: usize = 0;
183 let mut scratch: HashSet<u32> = HashSet::new();
184
185 for entry in walker.filter_map(std::result::Result::ok) {
186 if entry.file_type().is_none_or(|ft| ft.is_dir()) {
187 continue;
188 }
189 if entry.file_type().is_some_and(|ft| ft.is_symlink()) {
190 continue;
191 }
192 let path = entry.path();
193 if is_binary_ext(path) || is_generated_file(path) {
194 continue;
195 }
196 if !allow_secret_paths && crate::core::io_boundary::is_secret_like(path).is_some() {
197 continue;
198 }
199 let state = match std::fs::metadata(path) {
204 Ok(meta) if !meta.file_type().is_file() => continue,
205 Ok(meta) if meta.len() > MAX_FILE_SIZE => continue,
206 Ok(meta) => crate::core::content_cache::FileState::from_metadata(&meta),
207 Err(_) => continue,
208 };
209 let content: std::sync::Arc<str> = if let Some(cached) =
215 state.and_then(|s| crate::core::content_cache::get(path, s))
216 {
217 cached
218 } else {
219 let Ok(text) = std::fs::read_to_string(path) else {
220 continue;
221 };
222 let arc: std::sync::Arc<str> = std::sync::Arc::from(text);
223 if let Some(s) = state {
224 crate::core::content_cache::insert(path, s, std::sync::Arc::clone(&arc));
225 }
226 arc
227 };
228
229 if files.len() >= MAX_FILES {
230 return None; }
232
233 scratch.clear();
234 let bytes = content.as_bytes();
235 if bytes.len() >= 3 {
236 for w in bytes.windows(3) {
237 if is_word_byte(w[0]) && is_word_byte(w[1]) && is_word_byte(w[2]) {
238 scratch.insert(pack(w[0], w[1], w[2]));
239 }
240 }
241 }
242 total_entries += scratch.len();
243 if total_entries > MAX_TOTAL_ENTRIES {
244 return None; }
246 let mut tris: Vec<u32> = scratch.iter().copied().collect();
247 tris.sort_unstable();
248 files.push(path.to_path_buf());
249 per_file_trigrams.push(tris);
250 }
251
252 let narrowing = build_narrowing(&per_file_trigrams, total_entries);
253
254 Some(Self {
255 files,
256 narrowing,
257 respect_gitignore,
258 allow_secret_paths,
259 built_at: Instant::now(),
260 })
261 }
262
263 fn is_fresh(&self) -> bool {
264 self.built_at.elapsed() < TTL
265 }
266
267 fn config_matches(&self, respect_gitignore: bool, allow_secret_paths: bool) -> bool {
268 self.respect_gitignore == respect_gitignore && self.allow_secret_paths == allow_secret_paths
269 }
270
271 pub fn candidate_paths(
281 &self,
282 pattern: &str,
283 includes: &[Pattern],
284 root: &Path,
285 ) -> CandidateSet {
286 if let Some(ids) = self.literal_candidates(pattern) {
287 let paths = ids
288 .into_iter()
289 .map(|id| self.files[id as usize].clone())
290 .filter(|p| glob_matches(p, includes, root))
291 .collect();
292 CandidateSet::Narrowed(paths)
293 } else {
294 let paths = self
295 .files
296 .iter()
297 .filter(|p| glob_matches(p, includes, root))
298 .cloned()
299 .collect();
300 CandidateSet::FullList(paths)
301 }
302 }
303
304 fn literal_candidates(&self, pattern: &str) -> Option<Vec<u32>> {
308 let bytes = pattern.as_bytes();
309 if bytes.len() < 3 || !bytes.iter().all(|&b| is_word_byte(b)) {
310 return None;
311 }
312 let mut tris: Vec<u32> = bytes.windows(3).map(|w| pack(w[0], w[1], w[2])).collect();
314 tris.sort_unstable();
315 tris.dedup();
316
317 match &self.narrowing {
318 Narrowing::Postings(trigrams) => Some(Self::postings_intersect(trigrams, &tris)),
319 Narrowing::Blooms(blooms) => Some(Self::bloom_scan(blooms, &tris)),
320 }
321 }
322
323 fn postings_intersect(trigrams: &HashMap<u32, Vec<u32>>, tris: &[u32]) -> Vec<u32> {
326 let mut lists: Vec<&Vec<u32>> = Vec::with_capacity(tris.len());
327 for &tri in tris {
328 match trigrams.get(&tri) {
329 None => return Vec::new(),
331 Some(list) => lists.push(list),
332 }
333 }
334 lists.sort_by_key(|l| l.len());
335
336 let mut acc: Vec<u32> = lists[0].clone();
337 for list in &lists[1..] {
338 acc = intersect_sorted(&acc, list);
339 if acc.is_empty() {
340 break;
341 }
342 }
343 acc
344 }
345
346 fn bloom_scan(blooms: &[FileBloom], tris: &[u32]) -> Vec<u32> {
350 let mut out = Vec::new();
351 for (fid, bloom) in blooms.iter().enumerate() {
352 if tris.iter().all(|&t| bloom.maybe_contains(t)) {
353 out.push(fid as u32);
354 }
355 }
356 out
357 }
358}
359
360fn build_narrowing(per_file: &[Vec<u32>], total_entries: usize) -> Narrowing {
362 if total_entries <= MAX_POSTING_ENTRIES {
363 let mut trigrams: HashMap<u32, Vec<u32>> = HashMap::new();
364 for (fid, tris) in per_file.iter().enumerate() {
365 for &t in tris {
366 trigrams.entry(t).or_default().push(fid as u32);
368 }
369 }
370 Narrowing::Postings(trigrams)
371 } else {
372 let blooms = per_file
373 .iter()
374 .map(|tris| {
375 let mut b = FileBloom::with_capacity(tris.len());
376 for &t in tris {
377 b.insert(t);
378 }
379 b
380 })
381 .collect();
382 Narrowing::Blooms(blooms)
383 }
384}
385
386pub enum CandidateSet {
388 Narrowed(Vec<PathBuf>),
390 FullList(Vec<PathBuf>),
392}
393
394impl CandidateSet {
395 pub fn into_paths(self) -> Vec<PathBuf> {
396 match self {
397 CandidateSet::Narrowed(p) | CandidateSet::FullList(p) => p,
398 }
399 }
400}
401
402fn glob_matches(path: &Path, includes: &[Pattern], root: &Path) -> bool {
405 if includes.is_empty() {
406 return true;
407 }
408 let rel = path.strip_prefix(root).unwrap_or(path);
409 let rel_str = rel.to_string_lossy();
410 includes.iter().any(|p| p.matches(&rel_str))
411}
412
413fn intersect_sorted(a: &[u32], b: &[u32]) -> Vec<u32> {
415 let mut out = Vec::new();
416 let (mut i, mut j) = (0, 0);
417 while i < a.len() && j < b.len() {
418 match a[i].cmp(&b[j]) {
419 std::cmp::Ordering::Less => i += 1,
420 std::cmp::Ordering::Greater => j += 1,
421 std::cmp::Ordering::Equal => {
422 out.push(a[i]);
423 i += 1;
424 j += 1;
425 }
426 }
427 }
428 out
429}
430
431struct CacheEntry {
436 index: Option<Arc<SearchIndex>>,
437 building: bool,
438}
439
440static CACHE: OnceLock<Mutex<HashMap<String, CacheEntry>>> = OnceLock::new();
441
442fn cache() -> &'static Mutex<HashMap<String, CacheEntry>> {
443 CACHE.get_or_init(|| Mutex::new(HashMap::new()))
444}
445
446fn index_disabled() -> bool {
449 std::env::var("LEAN_CTX_DISABLE_SEARCH_INDEX")
450 .is_ok_and(|v| v == "1" || v.eq_ignore_ascii_case("true"))
451}
452
453pub fn get_fresh(
457 root: &str,
458 respect_gitignore: bool,
459 allow_secret_paths: bool,
460) -> Option<Arc<SearchIndex>> {
461 if !respect_gitignore || index_disabled() {
463 return None;
464 }
465
466 let mut needs_build = false;
467 let result = {
468 let mut map = cache()
469 .lock()
470 .unwrap_or_else(std::sync::PoisonError::into_inner);
471 let entry = map.entry(root.to_string()).or_insert(CacheEntry {
472 index: None,
473 building: false,
474 });
475 match &entry.index {
476 Some(idx)
477 if idx.config_matches(respect_gitignore, allow_secret_paths) && idx.is_fresh() =>
478 {
479 Some(Arc::clone(idx))
480 }
481 Some(idx) if idx.config_matches(respect_gitignore, allow_secret_paths) => {
482 needs_build = !entry.building;
484 if needs_build {
485 entry.building = true;
486 }
487 Some(Arc::clone(idx))
488 }
489 _ => {
490 needs_build = !entry.building;
491 if needs_build {
492 entry.building = true;
493 }
494 None
495 }
496 }
497 };
498
499 if needs_build {
500 spawn_build(root.to_string(), respect_gitignore, allow_secret_paths);
501 }
502 result
503}
504
505pub fn ensure_background(root: &str, respect_gitignore: bool, allow_secret_paths: bool) {
508 if !respect_gitignore || index_disabled() {
509 return;
510 }
511 let needs_build = {
512 let mut map = cache()
513 .lock()
514 .unwrap_or_else(std::sync::PoisonError::into_inner);
515 let entry = map.entry(root.to_string()).or_insert(CacheEntry {
516 index: None,
517 building: false,
518 });
519 let fresh = entry.index.as_ref().is_some_and(|idx| {
520 idx.config_matches(respect_gitignore, allow_secret_paths) && idx.is_fresh()
521 });
522 if fresh || entry.building {
523 false
524 } else {
525 entry.building = true;
526 true
527 }
528 };
529 if needs_build {
530 spawn_build(root.to_string(), respect_gitignore, allow_secret_paths);
531 }
532}
533
534pub fn warm_blocking(root: &str, respect_gitignore: bool, allow_secret_paths: bool) -> bool {
538 if !respect_gitignore || index_disabled() {
539 return false;
540 }
541 let Some(idx) = SearchIndex::build(root, respect_gitignore, allow_secret_paths) else {
542 return false;
543 };
544 let mut map = cache()
545 .lock()
546 .unwrap_or_else(std::sync::PoisonError::into_inner);
547 map.insert(
548 root.to_string(),
549 CacheEntry {
550 index: Some(Arc::new(idx)),
551 building: false,
552 },
553 );
554 true
555}
556
557fn spawn_build(root: String, respect_gitignore: bool, allow_secret_paths: bool) {
558 std::thread::spawn(move || {
559 let built = std::panic::catch_unwind(|| {
560 SearchIndex::build(&root, respect_gitignore, allow_secret_paths)
561 })
562 .ok()
563 .flatten();
564
565 let mut map = cache()
566 .lock()
567 .unwrap_or_else(std::sync::PoisonError::into_inner);
568 if let Some(entry) = map.get_mut(&root) {
569 entry.building = false;
570 if let Some(idx) = built {
571 entry.index = Some(Arc::new(idx));
572 }
573 }
574 });
575}
576
577#[cfg(test)]
578mod tests {
579 use super::*;
580
581 fn corpus() -> tempfile::TempDir {
582 let dir = tempfile::tempdir().unwrap();
583 std::fs::write(dir.path().join("a.rs"), "fn handler() {}\nlet x = 1;\n").unwrap();
584 std::fs::write(dir.path().join("b.rs"), "fn other() {}\n// nothing here\n").unwrap();
585 std::fs::write(dir.path().join("c.txt"), "handler appears in text too\n").unwrap();
586 dir
587 }
588
589 #[test]
590 fn build_refuses_to_index_home_directory() {
591 if let Some(home) = dirs::home_dir() {
594 assert!(
595 SearchIndex::build(&home.to_string_lossy(), true, false).is_none(),
596 "search index must never auto-build over the home directory"
597 );
598 }
599 }
600
601 #[test]
602 fn narrows_to_files_containing_literal() {
603 let dir = corpus();
604 let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
605 let cands = idx.candidate_paths("handler", &[], dir.path());
606 let paths = cands.into_paths();
607 assert!(paths.iter().any(|p| p.ends_with("a.rs")));
609 assert!(paths.iter().any(|p| p.ends_with("c.txt")));
610 assert!(!paths.iter().any(|p| p.ends_with("b.rs")));
611 }
612
613 #[test]
614 fn absent_trigram_yields_empty_candidates() {
615 let dir = corpus();
616 let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
617 match idx.candidate_paths("zzzqqq", &[], dir.path()) {
618 CandidateSet::Narrowed(p) => assert!(p.is_empty()),
619 CandidateSet::FullList(_) => panic!("pure literal should narrow"),
620 }
621 }
622
623 #[test]
624 fn ext_filter_restricts_candidates() {
625 let dir = corpus();
626 let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
627 let paths = idx
628 .candidate_paths(
629 "handler",
630 &[glob::Pattern::new("*.rs").unwrap()],
631 dir.path(),
632 )
633 .into_paths();
634 assert!(paths.iter().all(|p| p.extension().unwrap() == "rs"));
635 assert!(paths.iter().any(|p| p.ends_with("a.rs")));
636 }
637
638 #[test]
639 #[cfg(unix)]
640 fn build_skips_named_pipe_without_hanging() {
641 use std::sync::mpsc;
642 use std::time::Duration;
643 let dir = corpus();
647 let fifo = dir.path().join("pipe.fifo");
648 let c = std::ffi::CString::new(fifo.to_string_lossy().as_bytes()).unwrap();
649 assert_eq!(
650 unsafe { libc::mkfifo(c.as_ptr(), 0o644) },
653 0,
654 "mkfifo failed"
655 );
656
657 let root = dir.path().to_str().unwrap().to_string();
658 let (tx, rx) = mpsc::channel();
659 std::thread::spawn(move || {
660 let built = SearchIndex::build(&root, true, false);
661 let _ = tx.send(built.map(|idx| {
662 idx.candidate_paths("handler", &[], std::path::Path::new(&root))
663 .into_paths()
664 }));
665 });
666 let paths = rx
667 .recv_timeout(Duration::from_secs(5))
668 .expect("SearchIndex::build hung on a FIFO (#336 regression)")
669 .expect("index should build");
670 assert!(paths.iter().any(|p| p.ends_with("a.rs")));
671 assert!(!paths.iter().any(|p| p.ends_with("pipe.fifo")));
672 }
673
674 #[test]
675 fn regex_query_falls_back_to_full_list() {
676 let dir = corpus();
677 let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
678 match idx.candidate_paths("fn .*\\(\\)", &[], dir.path()) {
679 CandidateSet::FullList(p) => assert!(!p.is_empty()),
680 CandidateSet::Narrowed(_) => panic!("metachar query must not narrow"),
681 }
682 }
683
684 #[test]
685 fn short_query_falls_back_to_full_list() {
686 let dir = corpus();
687 let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
688 assert!(matches!(
689 idx.candidate_paths("fn", &[], dir.path()),
690 CandidateSet::FullList(_)
691 ));
692 }
693
694 #[test]
698 fn narrowing_has_identical_recall_to_full_scan() {
699 use regex::Regex;
700 use std::collections::BTreeSet;
701
702 let dir = tempfile::tempdir().unwrap();
703 let samples = [
705 (
706 "auth/login.rs",
707 "fn authenticate(user) {}\nlet token = mint();\n",
708 ),
709 (
710 "auth/session.rs",
711 "struct Session;\n// authenticate again here\n",
712 ),
713 ("db/pool.rs", "fn connect() {}\nlet retries = 3;\n"),
714 (
715 "ui/button.tsx",
716 "export const Button = () => authenticate;\n",
717 ),
718 ("readme.md", "This project uses authenticate flows.\n"),
719 ("unrelated.rs", "fn helper() { let v = 1; }\n"),
720 ];
721 for (rel, content) in samples {
722 let p = dir.path().join(rel);
723 std::fs::create_dir_all(p.parent().unwrap()).unwrap();
724 std::fs::write(p, content).unwrap();
725 }
726 let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
727
728 let full_scan = |pat: &str| -> BTreeSet<String> {
729 let re = Regex::new(pat).unwrap();
730 let mut hits = BTreeSet::new();
731 for (rel, content) in samples {
732 for (i, line) in content.lines().enumerate() {
733 if re.is_match(line) {
734 hits.insert(format!("{rel}:{}", i + 1));
735 }
736 }
737 }
738 hits
739 };
740
741 for query in ["authenticate", "Session", "retries", "token"] {
742 let re = Regex::new(query).unwrap();
743 let candidates = idx.candidate_paths(query, &[], dir.path()).into_paths();
744 let mut narrowed = BTreeSet::new();
745 for path in &candidates {
746 let content = std::fs::read_to_string(path).unwrap();
747 let rel = path.strip_prefix(dir.path()).unwrap().to_string_lossy();
748 for (i, line) in content.lines().enumerate() {
749 if re.is_match(line) {
750 narrowed.insert(format!("{}:{}", rel.replace('\\', "/"), i + 1));
751 }
752 }
753 }
754 assert_eq!(
755 narrowed,
756 full_scan(query),
757 "recall mismatch for query {query:?}"
758 );
759 }
760 }
761
762 #[test]
763 fn intersect_sorted_basic() {
764 assert_eq!(
765 intersect_sorted(&[1, 2, 3, 5], &[2, 3, 4, 5]),
766 vec![2, 3, 5]
767 );
768 assert_eq!(intersect_sorted(&[1, 2], &[3, 4]), Vec::<u32>::new());
769 }
770
771 fn trigrams_of(s: &str) -> Vec<u32> {
774 let mut set = HashSet::new();
775 let b = s.as_bytes();
776 if b.len() >= 3 {
777 for w in b.windows(3) {
778 if is_word_byte(w[0]) && is_word_byte(w[1]) && is_word_byte(w[2]) {
779 set.insert(pack(w[0], w[1], w[2]));
780 }
781 }
782 }
783 let mut v: Vec<u32> = set.into_iter().collect();
784 v.sort_unstable();
785 v
786 }
787
788 #[test]
789 fn file_bloom_has_no_false_negatives() {
790 let tris = trigrams_of("fn authenticate(user) { let token = mint(); }");
791 let mut bloom = FileBloom::with_capacity(tris.len());
792 for &t in &tris {
793 bloom.insert(t);
794 }
795 assert!(tris.iter().all(|&t| bloom.maybe_contains(t)));
797 }
798
799 #[test]
803 fn bloom_tier_is_superset_of_postings_tier() {
804 let mut seed = 0x1234_5678_9abc_def0u64;
806 let mut rng = || {
807 seed = seed
808 .wrapping_mul(6364136223846793005)
809 .wrapping_add(1442695040888963407);
810 (seed >> 33) as u32
811 };
812 let mut per_file: Vec<Vec<u32>> = Vec::new();
813 for _ in 0..80 {
814 let n = 50 + (rng() % 250) as usize;
815 let mut s = HashSet::new();
816 for _ in 0..n {
817 s.insert(rng() & 0x00FF_FFFF);
818 }
819 let mut v: Vec<u32> = s.into_iter().collect();
820 v.sort_unstable();
821 per_file.push(v);
822 }
823 let total: usize = per_file.iter().map(Vec::len).sum();
824
825 let postings = build_narrowing(&per_file, total); let blooms = build_narrowing(&per_file, MAX_POSTING_ENTRIES + 1); let (Narrowing::Postings(pt), Narrowing::Blooms(bl)) = (&postings, &blooms) else {
828 panic!("unexpected narrowing tiers");
829 };
830
831 for f in &per_file {
834 if f.len() < 3 {
835 continue;
836 }
837 let q = vec![f[0], f[f.len() / 2], f[f.len() - 1]];
838 let exact = SearchIndex::postings_intersect(pt, &q);
839 let bloom: HashSet<u32> = SearchIndex::bloom_scan(bl, &q).into_iter().collect();
840 for id in exact {
841 assert!(
842 bloom.contains(&id),
843 "Bloom tier dropped a true match (false negative) for {q:?}"
844 );
845 }
846 }
847 }
848
849 #[test]
852 fn bloom_tier_end_to_end_recall() {
853 let samples = [
854 (
855 "auth_login.rs",
856 "fn authenticate(user) {}\nlet token = mint();\n",
857 ),
858 (
859 "auth_session.rs",
860 "struct Session;\n// authenticate again here\n",
861 ),
862 ("db_pool.rs", "fn connect() {}\nlet retries = 3;\n"),
863 (
864 "ui_button.tsx",
865 "export const Button = () => authenticate;\n",
866 ),
867 ("readme.md", "This project uses authenticate flows.\n"),
868 ("unrelated.rs", "fn helper() { let v = 1; }\n"),
869 ];
870 let files: Vec<PathBuf> = samples.iter().map(|(rel, _)| PathBuf::from(rel)).collect();
871 let per_file: Vec<Vec<u32>> = samples.iter().map(|(_, c)| trigrams_of(c)).collect();
872
873 let idx = SearchIndex {
874 files,
875 narrowing: build_narrowing(&per_file, MAX_POSTING_ENTRIES + 1),
876 respect_gitignore: true,
877 allow_secret_paths: false,
878 built_at: Instant::now(),
879 };
880 assert!(
881 matches!(idx.narrowing, Narrowing::Blooms(_)),
882 "test must exercise the Bloom tier"
883 );
884
885 for query in ["authenticate", "Session", "retries", "token"] {
886 let cands: HashSet<String> = idx
887 .candidate_paths(query, &[], std::path::Path::new(""))
888 .into_paths()
889 .iter()
890 .map(|p| p.to_string_lossy().to_string())
891 .collect();
892 for (rel, content) in samples {
893 if content.contains(query) {
894 assert!(
895 cands.contains(rel),
896 "Bloom tier dropped real match {rel} for query {query:?}"
897 );
898 }
899 }
900 }
901 }
902}