Skip to main content

lean_ctx/core/
search_index.rs

1//! Resident line-search index for `ctx_search` (Phase 1 of the efficiency epic).
2//!
3//! Historically `ctx_search` walked the filesystem, read every file, and ran a
4//! regex on every line on *every* call — `O(files × lines)`. That is the
5//! 40–200 ms latency floor this module eliminates.
6//!
7//! This module keeps a RAM-resident trigram index (`trigram → file ids`) so the
8//! common case (an identifier / literal query) collapses to: intersect a few
9//! posting lists in memory → read & regex-verify only the handful of candidate
10//! files. The index never decides matches itself; it only *narrows the file
11//! set*, then `ctx_search` verifies candidates with the exact same regex loop,
12//! so the returned `file:line` hits are byte-identical to the walk path.
13//!
14//! Design notes:
15//! - The index is built with the *same* walk config and file filters as
16//!   `ctx_search` (see [`crate::tools::ctx_search`]) so the searchable universe
17//!   is identical — that is what guarantees recall parity.
18//! - Only `[A-Za-z0-9_]` trigrams are indexed. Lookups only ever use trigrams
19//!   from pure-identifier queries, so this is both sufficient and memory-bounded.
20//! - Narrowing is applied *only* for pure `[A-Za-z0-9_]` literal queries (the
21//!   dominant agent case). Any query containing a regex metacharacter falls
22//!   back to scanning the cached file list (still skips the directory walk).
23//! - Freshness uses a short TTL with background rebuild, mirroring
24//!   [`crate::core::bm25_cache`]. A real fs watcher is Phase 5.
25
26use 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
36/// Freshness window before a background rebuild is triggered. Matches the
37/// bounded-staleness model already used by the BM25 cache.
38const TTL: Duration = Duration::from_secs(15);
39
40/// Upper bound on indexed files; larger trees fall back to the walk path.
41const MAX_FILES: usize = 200_000;
42
43/// Posting-entry budget (`file_id` occurrences across all trigrams). Up to this
44/// many entries we keep exact inverted posting lists (fastest, sparse lookups).
45/// Beyond it we switch to the per-file Bloom tier instead of giving up — see
46/// [`Narrowing`]. ~4 bytes each → ~48 MB before the switch.
47const MAX_POSTING_ENTRIES: usize = 12_000_000;
48
49/// Hard ceiling on total trigram entries collected during a build. Past this we
50/// abandon indexing (walk fallback) to avoid pathological memory use even with
51/// the compact Bloom tier.
52const MAX_TOTAL_ENTRIES: usize = 48_000_000;
53
54/// Bloom tuning: bits per distinct trigram and number of hash probes. ~12 bits
55/// with k=7 keeps the false-positive rate well under 1% — and a false positive
56/// only costs one extra regex-verified file read (never a missed match).
57const BLOOM_BITS_PER_ITEM: usize = 12;
58const BLOOM_K: usize = 7;
59/// Per-file Bloom size clamp (in bits): 64 bits min, 1 Mi bits (128 KiB) max.
60const BLOOM_MIN_BITS: usize = 64;
61const BLOOM_MAX_BITS: usize = 1 << 20;
62
63/// A trigram is indexable only if all three bytes are `[A-Za-z0-9_]`.
64fn 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
72/// How candidate files are narrowed for a literal query. Two tiers, chosen by
73/// corpus size, both providing a *superset* of true matches (zero false
74/// negatives) which `ctx_search` then regex-verifies:
75/// - `Postings`: exact inverted lists `trigram → sorted file ids`. Fast, sparse
76///   lookups; used while total entries fit [`MAX_POSTING_ENTRIES`].
77/// - `Blooms`: one compact per-file Bloom filter of the file's trigrams. ~3×
78///   smaller than postings, so monorepos that would otherwise blow the posting
79///   budget still get index-narrowing instead of a full directory walk.
80enum Narrowing {
81    Postings(HashMap<u32, Vec<u32>>),
82    Blooms(Vec<FileBloom>),
83}
84
85/// A per-file Bloom filter over the file's word-trigrams. No false negatives:
86/// if any probed bit for a trigram is unset, the file provably lacks it.
87struct FileBloom {
88    /// Bit storage; the filter width `m = bits.len() * 64` is a power of two.
89    bits: Vec<u64>,
90}
91
92/// 64-bit avalanche mix (splitmix64 finalizer) — spreads a packed trigram into
93/// a well-distributed hash for double-probing.
94#[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    /// Double hashing: `p_i = h1 + i·h2 (mod m)` with `m` a power of two.
119    #[inline]
120    fn probes(&self, trigram: u32) -> impl Iterator<Item = usize> + '_ {
121        let m = self.m_bits();
122        let mask = m - 1; // m is a power of two
123        let h = mix64(u64::from(trigram));
124        let h1 = (h & 0xFFFF_FFFF) as usize;
125        let h2 = ((h >> 32) as usize) | 1; // odd step → full-period probing
126        (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
141/// RAM-resident trigram index over one project root.
142pub struct SearchIndex {
143    files: Vec<PathBuf>,
144    /// Candidate-narrowing structure (exact postings or compact per-file Bloom).
145    narrowing: Narrowing,
146    respect_gitignore: bool,
147    allow_secret_paths: bool,
148    built_at: Instant,
149}
150
151impl SearchIndex {
152    /// Build the index by walking `root` with the exact same config and filters
153    /// as `ctx_search`, so the searchable file universe is identical.
154    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        // Never auto-index a broad/unsafe root (HOME, filesystem root, a dir with
160        // dozens of unrelated subtrees). This mirrors the graph/BM25 guard and
161        // stops a background build from walking the whole home directory — which
162        // on Windows would hydrate OneDrive placeholders (#363).
163        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        // Per-file sorted, deduped trigrams. Same memory as the posting lists
179        // would be, but grouped by file so we can materialise *either* tier
180        // afterwards without a second pass over the corpus.
181        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            // Only index regular files within the size budget. A FIFO/socket/
200            // device node would block the `read_to_string` below forever (#336),
201            // hanging the background build and starving the fast path. `metadata`
202            // (stat) never opens the file, so it is safe on special files.
203            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            // Read the corpus exactly once (issue #148): reuse a fresh cached
210            // copy if a prior `ctx_search`/build already read this file, else
211            // read it now and publish it so the upcoming `ctx_search` verify
212            // pass is an in-memory hit instead of a second disk read. Mirrors
213            // ctx_search: a non-UTF-8 file is never searchable, so it is skipped.
214            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; // too large even for the Bloom tier — use the walk
231            }
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; // memory guard — fall back to walk
245            }
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    /// Candidate files for `pattern`, filtered by the `include` glob (matched
272    /// against each file's path relative to `root`). `None` means "no safe
273    /// narrowing possible" — the caller should scan the full file list.
274    ///
275    /// Narrowing is applied only for pure `[A-Za-z0-9_]` literals of length ≥ 3.
276    /// For such a literal every match contains it on a single line, hence the
277    /// file contains all of its consecutive trigrams: intersecting their
278    /// posting lists yields a *superset* of matching files (zero false
279    /// negatives), which the caller then regex-verifies.
280    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    /// Returns candidate file ids for a pure-literal query, or `None` if the
305    /// query is not a trigram-narrowable pure `[A-Za-z0-9_]` literal. Both tiers
306    /// return a *superset* of true matches (zero false negatives).
307    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        // Distinct trigrams of the literal.
313        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    /// Exact-tier: intersect the posting lists of every required trigram
324    /// (smallest first for a cheap intersection).
325    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                // A required trigram is absent → provably no match anywhere.
330                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    /// Bloom-tier: a file is a candidate iff its Bloom filter may contain every
347    /// required trigram. No false negatives (an unset probe bit ⇒ the trigram is
348    /// provably absent), so the result is still a superset of true matches.
349    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
360/// Materialise the appropriate narrowing tier for a freshly walked corpus.
361fn 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                // file ids are appended in ascending order ⇒ lists stay sorted.
367                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
386/// Result of [`SearchIndex::candidate_paths`].
387pub enum CandidateSet {
388    /// Trigram-narrowed candidate files (a superset of real matches).
389    Narrowed(Vec<PathBuf>),
390    /// No safe narrowing — the full cached file list (still skips the walk).
391    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
402/// True when `path` matches *any* of the `includes` globs (relative to `root`),
403/// or when there is no filter (`includes` empty).
404fn 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
413/// Intersection of two ascending, deduped `u32` slices.
414fn 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
431// ---------------------------------------------------------------------------
432// Resident cache (one index per project root) with background (re)build.
433// ---------------------------------------------------------------------------
434
435struct 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
446/// Escape hatch: `LEAN_CTX_DISABLE_SEARCH_INDEX=1` forces the walk path
447/// everywhere (debugging / A-B measurement / opt-out).
448fn 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
453/// Returns a fresh resident index for `root` if one is available for the given
454/// config, otherwise spawns a background (re)build and returns `None` so the
455/// caller uses the walk fallback for this call.
456pub fn get_fresh(
457    root: &str,
458    respect_gitignore: bool,
459    allow_secret_paths: bool,
460) -> Option<Arc<SearchIndex>> {
461    // Privileged "ignore gitignore" scans are rare and bypass the index.
462    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                // Stale but usable: serve it and refresh in the background.
483                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
505/// Ensure a resident index for `root` is built (or building) in the background.
506/// Safe to call repeatedly; deduped via the per-root `building` flag.
507pub 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
534/// Build the index synchronously and install it in the resident cache.
535/// Returns `true` on success. Useful for CLI prewarm and benchmarks that need a
536/// guaranteed-warm index. Respects the disable env var.
537pub 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
557/// Per-repo lock name serializing the resident search-index build across
558/// processes, mirroring the `graph-idx` / `bm25-idx` locks in
559/// [`crate::core::index_orchestrator`]. Distinct `search-` prefix so the three
560/// indexers never serialize against one another.
561fn search_index_lock_name(root: &str) -> String {
562    format!(
563        "search-idx-{}",
564        &crate::core::index_namespace::namespace_hash(Path::new(root))[..8]
565    )
566}
567
568/// Outcome of a guarded background build: did this process do the walk, or did
569/// it yield to another process already building the same root?
570#[derive(Debug, Clone, Copy, PartialEq, Eq)]
571enum BuildOutcome {
572    Built,
573    Deferred,
574}
575
576/// Build the resident index under a cross-process herd guard (#460).
577///
578/// The trigram index is RAM-resident (not shareable on disk), so on lock
579/// contention we *defer* the proactive pre-warm instead of running a second
580/// simultaneous file walk: a boot wave of N sessions on one repo then triggers
581/// ~1 walk at a time, not N. Deferring is safe — `ctx_search` still works via
582/// its walk fallback, and the per-process `building` flag is cleared so the next
583/// `ensure_background` nudge (every search, post-TTL) retries once the holder
584/// releases. The short 200 ms wait keeps the common single-session path
585/// latency-free.
586fn build_guarded(root: &str, respect_gitignore: bool, allow_secret_paths: bool) -> BuildOutcome {
587    let lock = crate::core::startup_guard::try_acquire_lock(
588        &search_index_lock_name(root),
589        Duration::from_millis(200),
590        Duration::from_mins(3),
591    );
592    if lock.is_none() {
593        // Another process owns the build. Clear the in-flight flag so a later
594        // nudge retries rather than leaving `building` stuck true forever.
595        let mut map = cache()
596            .lock()
597            .unwrap_or_else(std::sync::PoisonError::into_inner);
598        if let Some(entry) = map.get_mut(root) {
599            entry.building = false;
600        }
601        return BuildOutcome::Deferred;
602    }
603
604    let built = std::panic::catch_unwind(|| {
605        SearchIndex::build(root, respect_gitignore, allow_secret_paths)
606    })
607    .ok()
608    .flatten();
609
610    let mut map = cache()
611        .lock()
612        .unwrap_or_else(std::sync::PoisonError::into_inner);
613    if let Some(entry) = map.get_mut(root) {
614        entry.building = false;
615        if let Some(idx) = built {
616            entry.index = Some(Arc::new(idx));
617        }
618    }
619    // `lock` is held until here so the cross-process guard spans the whole walk.
620    drop(lock);
621    BuildOutcome::Built
622}
623
624fn spawn_build(root: String, respect_gitignore: bool, allow_secret_paths: bool) {
625    std::thread::spawn(move || {
626        let _ = build_guarded(&root, respect_gitignore, allow_secret_paths);
627    });
628}
629
630#[cfg(test)]
631mod tests {
632    use super::*;
633
634    fn corpus() -> tempfile::TempDir {
635        let dir = tempfile::tempdir().unwrap();
636        std::fs::write(dir.path().join("a.rs"), "fn handler() {}\nlet x = 1;\n").unwrap();
637        std::fs::write(dir.path().join("b.rs"), "fn other() {}\n// nothing here\n").unwrap();
638        std::fs::write(dir.path().join("c.txt"), "handler appears in text too\n").unwrap();
639        dir
640    }
641
642    #[test]
643    fn build_refuses_to_index_home_directory() {
644        // Auto-indexing HOME would walk the entire home tree and, on Windows,
645        // hydrate every OneDrive placeholder (#363). The build must bail out.
646        if let Some(home) = dirs::home_dir() {
647            assert!(
648                SearchIndex::build(&home.to_string_lossy(), true, false).is_none(),
649                "search index must never auto-build over the home directory"
650            );
651        }
652    }
653
654    #[test]
655    fn narrows_to_files_containing_literal() {
656        let dir = corpus();
657        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
658        let cands = idx.candidate_paths("handler", &[], dir.path());
659        let paths = cands.into_paths();
660        // a.rs and c.txt contain "handler"; b.rs must be excluded.
661        assert!(paths.iter().any(|p| p.ends_with("a.rs")));
662        assert!(paths.iter().any(|p| p.ends_with("c.txt")));
663        assert!(!paths.iter().any(|p| p.ends_with("b.rs")));
664    }
665
666    #[test]
667    fn absent_trigram_yields_empty_candidates() {
668        let dir = corpus();
669        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
670        match idx.candidate_paths("zzzqqq", &[], dir.path()) {
671            CandidateSet::Narrowed(p) => assert!(p.is_empty()),
672            CandidateSet::FullList(_) => panic!("pure literal should narrow"),
673        }
674    }
675
676    #[test]
677    fn ext_filter_restricts_candidates() {
678        let dir = corpus();
679        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
680        let paths = idx
681            .candidate_paths(
682                "handler",
683                &[glob::Pattern::new("*.rs").unwrap()],
684                dir.path(),
685            )
686            .into_paths();
687        assert!(paths.iter().all(|p| p.extension().unwrap() == "rs"));
688        assert!(paths.iter().any(|p| p.ends_with("a.rs")));
689    }
690
691    #[test]
692    #[cfg(unix)]
693    fn build_skips_named_pipe_without_hanging() {
694        use std::sync::mpsc;
695        use std::time::Duration;
696        // #336: the background index build read every file, so a FIFO in the
697        // corpus blocked the build thread forever. It must be skipped while the
698        // regular files are still indexed, and the build must return.
699        let dir = corpus();
700        let fifo = dir.path().join("pipe.fifo");
701        let c = std::ffi::CString::new(fifo.to_string_lossy().as_bytes()).unwrap();
702        assert_eq!(
703            // SAFETY: `c` is a live CString providing a valid NUL-terminated
704            // path pointer for the duration of the call.
705            unsafe { libc::mkfifo(c.as_ptr(), 0o644) },
706            0,
707            "mkfifo failed"
708        );
709
710        let root = dir.path().to_str().unwrap().to_string();
711        let (tx, rx) = mpsc::channel();
712        std::thread::spawn(move || {
713            let built = SearchIndex::build(&root, true, false);
714            let _ = tx.send(built.map(|idx| {
715                idx.candidate_paths("handler", &[], std::path::Path::new(&root))
716                    .into_paths()
717            }));
718        });
719        let paths = rx
720            .recv_timeout(Duration::from_secs(5))
721            .expect("SearchIndex::build hung on a FIFO (#336 regression)")
722            .expect("index should build");
723        assert!(paths.iter().any(|p| p.ends_with("a.rs")));
724        assert!(!paths.iter().any(|p| p.ends_with("pipe.fifo")));
725    }
726
727    #[test]
728    fn regex_query_falls_back_to_full_list() {
729        let dir = corpus();
730        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
731        match idx.candidate_paths("fn .*\\(\\)", &[], dir.path()) {
732            CandidateSet::FullList(p) => assert!(!p.is_empty()),
733            CandidateSet::Narrowed(_) => panic!("metachar query must not narrow"),
734        }
735    }
736
737    #[test]
738    fn short_query_falls_back_to_full_list() {
739        let dir = corpus();
740        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
741        assert!(matches!(
742            idx.candidate_paths("fn", &[], dir.path()),
743            CandidateSet::FullList(_)
744        ));
745    }
746
747    /// The core correctness claim: trigram narrowing never drops a real match.
748    /// For each literal query, the set of `file:line` hits found by scanning only
749    /// the narrowed candidates must equal the set found by scanning every file.
750    #[test]
751    fn narrowing_has_identical_recall_to_full_scan() {
752        use regex::Regex;
753        use std::collections::BTreeSet;
754
755        let dir = tempfile::tempdir().unwrap();
756        // A spread of files; some contain the query tokens, most do not.
757        let samples = [
758            (
759                "auth/login.rs",
760                "fn authenticate(user) {}\nlet token = mint();\n",
761            ),
762            (
763                "auth/session.rs",
764                "struct Session;\n// authenticate again here\n",
765            ),
766            ("db/pool.rs", "fn connect() {}\nlet retries = 3;\n"),
767            (
768                "ui/button.tsx",
769                "export const Button = () => authenticate;\n",
770            ),
771            ("readme.md", "This project uses authenticate flows.\n"),
772            ("unrelated.rs", "fn helper() { let v = 1; }\n"),
773        ];
774        for (rel, content) in samples {
775            let p = dir.path().join(rel);
776            std::fs::create_dir_all(p.parent().unwrap()).unwrap();
777            std::fs::write(p, content).unwrap();
778        }
779        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
780
781        let full_scan = |pat: &str| -> BTreeSet<String> {
782            let re = Regex::new(pat).unwrap();
783            let mut hits = BTreeSet::new();
784            for (rel, content) in samples {
785                for (i, line) in content.lines().enumerate() {
786                    if re.is_match(line) {
787                        hits.insert(format!("{rel}:{}", i + 1));
788                    }
789                }
790            }
791            hits
792        };
793
794        for query in ["authenticate", "Session", "retries", "token"] {
795            let re = Regex::new(query).unwrap();
796            let candidates = idx.candidate_paths(query, &[], dir.path()).into_paths();
797            let mut narrowed = BTreeSet::new();
798            for path in &candidates {
799                let content = std::fs::read_to_string(path).unwrap();
800                let rel = path.strip_prefix(dir.path()).unwrap().to_string_lossy();
801                for (i, line) in content.lines().enumerate() {
802                    if re.is_match(line) {
803                        narrowed.insert(format!("{}:{}", rel.replace('\\', "/"), i + 1));
804                    }
805                }
806            }
807            assert_eq!(
808                narrowed,
809                full_scan(query),
810                "recall mismatch for query {query:?}"
811            );
812        }
813    }
814
815    #[test]
816    fn intersect_sorted_basic() {
817        assert_eq!(
818            intersect_sorted(&[1, 2, 3, 5], &[2, 3, 4, 5]),
819            vec![2, 3, 5]
820        );
821        assert_eq!(intersect_sorted(&[1, 2], &[3, 4]), Vec::<u32>::new());
822    }
823
824    // ── Bloom tier ────────────────────────────────────────────────────────
825
826    fn trigrams_of(s: &str) -> Vec<u32> {
827        let mut set = HashSet::new();
828        let b = s.as_bytes();
829        if b.len() >= 3 {
830            for w in b.windows(3) {
831                if is_word_byte(w[0]) && is_word_byte(w[1]) && is_word_byte(w[2]) {
832                    set.insert(pack(w[0], w[1], w[2]));
833                }
834            }
835        }
836        let mut v: Vec<u32> = set.into_iter().collect();
837        v.sort_unstable();
838        v
839    }
840
841    #[test]
842    fn file_bloom_has_no_false_negatives() {
843        let tris = trigrams_of("fn authenticate(user) { let token = mint(); }");
844        let mut bloom = FileBloom::with_capacity(tris.len());
845        for &t in &tris {
846            bloom.insert(t);
847        }
848        // Every inserted trigram must be reported present (Bloom guarantee).
849        assert!(tris.iter().all(|&t| bloom.maybe_contains(t)));
850    }
851
852    /// Parity fuzz: the Bloom tier must return a *superset* of the exact posting
853    /// tier for every query (zero false negatives). False positives are allowed
854    /// (and verified away downstream), so we assert containment, not equality.
855    #[test]
856    fn bloom_tier_is_superset_of_postings_tier() {
857        // Deterministic synthetic corpus (LCG → reproducible).
858        let mut seed = 0x1234_5678_9abc_def0u64;
859        let mut rng = || {
860            seed = seed
861                .wrapping_mul(6364136223846793005)
862                .wrapping_add(1442695040888963407);
863            (seed >> 33) as u32
864        };
865        let mut per_file: Vec<Vec<u32>> = Vec::new();
866        for _ in 0..80 {
867            let n = 50 + (rng() % 250) as usize;
868            let mut s = HashSet::new();
869            for _ in 0..n {
870                s.insert(rng() & 0x00FF_FFFF);
871            }
872            let mut v: Vec<u32> = s.into_iter().collect();
873            v.sort_unstable();
874            per_file.push(v);
875        }
876        let total: usize = per_file.iter().map(Vec::len).sum();
877
878        let postings = build_narrowing(&per_file, total); // ≤ cap → postings
879        let blooms = build_narrowing(&per_file, MAX_POSTING_ENTRIES + 1); // forced bloom
880        let (Narrowing::Postings(pt), Narrowing::Blooms(bl)) = (&postings, &blooms) else {
881            panic!("unexpected narrowing tiers");
882        };
883
884        // Queries drawn from real file trigrams (these MUST be found by both),
885        // plus a few that are unlikely to exist anywhere.
886        for f in &per_file {
887            if f.len() < 3 {
888                continue;
889            }
890            let q = vec![f[0], f[f.len() / 2], f[f.len() - 1]];
891            let exact = SearchIndex::postings_intersect(pt, &q);
892            let bloom: HashSet<u32> = SearchIndex::bloom_scan(bl, &q).into_iter().collect();
893            for id in exact {
894                assert!(
895                    bloom.contains(&id),
896                    "Bloom tier dropped a true match (false negative) for {q:?}"
897                );
898            }
899        }
900    }
901
902    /// End-to-end: an index forced onto the Bloom tier must still surface every
903    /// file that actually contains the literal (recall parity with a full scan).
904    #[test]
905    fn bloom_tier_end_to_end_recall() {
906        let samples = [
907            (
908                "auth_login.rs",
909                "fn authenticate(user) {}\nlet token = mint();\n",
910            ),
911            (
912                "auth_session.rs",
913                "struct Session;\n// authenticate again here\n",
914            ),
915            ("db_pool.rs", "fn connect() {}\nlet retries = 3;\n"),
916            (
917                "ui_button.tsx",
918                "export const Button = () => authenticate;\n",
919            ),
920            ("readme.md", "This project uses authenticate flows.\n"),
921            ("unrelated.rs", "fn helper() { let v = 1; }\n"),
922        ];
923        let files: Vec<PathBuf> = samples.iter().map(|(rel, _)| PathBuf::from(rel)).collect();
924        let per_file: Vec<Vec<u32>> = samples.iter().map(|(_, c)| trigrams_of(c)).collect();
925
926        let idx = SearchIndex {
927            files,
928            narrowing: build_narrowing(&per_file, MAX_POSTING_ENTRIES + 1),
929            respect_gitignore: true,
930            allow_secret_paths: false,
931            built_at: Instant::now(),
932        };
933        assert!(
934            matches!(idx.narrowing, Narrowing::Blooms(_)),
935            "test must exercise the Bloom tier"
936        );
937
938        for query in ["authenticate", "Session", "retries", "token"] {
939            let cands: HashSet<String> = idx
940                .candidate_paths(query, &[], std::path::Path::new(""))
941                .into_paths()
942                .iter()
943                .map(|p| p.to_string_lossy().to_string())
944                .collect();
945            for (rel, content) in samples {
946                if content.contains(query) {
947                    assert!(
948                        cands.contains(rel),
949                        "Bloom tier dropped real match {rel} for query {query:?}"
950                    );
951                }
952            }
953        }
954    }
955
956    /// A scoped override of `LEAN_CTX_DATA_DIR`, restored on drop, so the
957    /// cross-process lock files land in an isolated temp dir during tests.
958    struct DataDirGuard {
959        prev: Option<String>,
960    }
961    impl DataDirGuard {
962        fn set(path: &std::path::Path) -> Self {
963            let prev = std::env::var("LEAN_CTX_DATA_DIR").ok();
964            crate::test_env::set_var("LEAN_CTX_DATA_DIR", path);
965            Self { prev }
966        }
967    }
968    impl Drop for DataDirGuard {
969        fn drop(&mut self) {
970            match self.prev.as_deref() {
971                Some(v) => crate::test_env::set_var("LEAN_CTX_DATA_DIR", v),
972                None => crate::test_env::remove_var("LEAN_CTX_DATA_DIR"),
973            }
974        }
975    }
976
977    #[test]
978    fn search_index_lock_name_is_per_repo_and_distinct() {
979        let a = search_index_lock_name("/tmp/repo-a");
980        let b = search_index_lock_name("/tmp/repo-b");
981        assert!(a.starts_with("search-idx-"), "unexpected lock name: {a}");
982        assert_ne!(a, b, "lock name must be per-repo");
983        assert_eq!(a, search_index_lock_name("/tmp/repo-a"), "stable per repo");
984        // Must not collide with the graph/bm25 locks for the same repo, or the
985        // three indexers would needlessly serialize against one another.
986        let h = &crate::core::index_namespace::namespace_hash(Path::new("/tmp/repo-a"))[..8];
987        assert_ne!(
988            a,
989            format!("graph-idx-{h}"),
990            "must not collide with graph lock"
991        );
992        assert_ne!(
993            a,
994            format!("bm25-idx-{h}"),
995            "must not collide with bm25 lock"
996        );
997    }
998
999    #[test]
1000    fn build_guarded_builds_when_uncontended() {
1001        let _env = crate::core::data_dir::test_env_lock();
1002        let data = tempfile::tempdir().unwrap();
1003        let _guard = DataDirGuard::set(data.path());
1004
1005        let dir = corpus();
1006        let root = dir.path().to_string_lossy().to_string();
1007        // Seed the in-flight flag the way `ensure_background` does before spawn.
1008        {
1009            let mut map = cache()
1010                .lock()
1011                .unwrap_or_else(std::sync::PoisonError::into_inner);
1012            map.insert(
1013                root.clone(),
1014                CacheEntry {
1015                    index: None,
1016                    building: true,
1017                },
1018            );
1019        }
1020        assert_eq!(
1021            build_guarded(&root, true, false),
1022            BuildOutcome::Built,
1023            "an uncontended root must build"
1024        );
1025        let map = cache()
1026            .lock()
1027            .unwrap_or_else(std::sync::PoisonError::into_inner);
1028        let entry = map.get(&root).expect("entry present");
1029        assert!(!entry.building, "building flag must clear after build");
1030        assert!(entry.index.is_some(), "index must be installed after build");
1031    }
1032
1033    #[test]
1034    fn build_guarded_defers_when_another_process_holds_the_lock() {
1035        let _env = crate::core::data_dir::test_env_lock();
1036        let data = tempfile::tempdir().unwrap();
1037        let _guard = DataDirGuard::set(data.path());
1038
1039        let dir = corpus();
1040        let root = dir.path().to_string_lossy().to_string();
1041        // Pre-hold the cross-process lock with *this* (alive) PID and a fresh
1042        // mtime, so neither the dead-owner nor the staleness reclaim can take it
1043        // — exactly the "another session is already building" state from #460.
1044        let lock_path = data
1045            .path()
1046            .join(format!(".{}.lock", search_index_lock_name(&root)));
1047        std::fs::write(&lock_path, format!("{}\n", std::process::id())).unwrap();
1048
1049        {
1050            let mut map = cache()
1051                .lock()
1052                .unwrap_or_else(std::sync::PoisonError::into_inner);
1053            map.insert(
1054                root.clone(),
1055                CacheEntry {
1056                    index: None,
1057                    building: true,
1058                },
1059            );
1060        }
1061        assert_eq!(
1062            build_guarded(&root, true, false),
1063            BuildOutcome::Deferred,
1064            "a contended root must defer the proactive pre-warm"
1065        );
1066        let map = cache()
1067            .lock()
1068            .unwrap_or_else(std::sync::PoisonError::into_inner);
1069        let entry = map.get(&root).expect("entry present");
1070        assert!(
1071            !entry.building,
1072            "deferred build must clear the in-flight flag so a later nudge retries"
1073        );
1074        assert!(
1075            entry.index.is_none(),
1076            "deferred build must not run a second walk / install an index"
1077        );
1078    }
1079}