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 is a function of *corpus state*, not the clock: the build records
24//!   a cheap, order-independent signature over the eligible files' `(path,
25//!   mtime, size)`, and every [`get_fresh`] re-derives it via a stat-only walk
26//!   that shares the build's exact filter path. The resident index is served
27//!   only when the signature matches live disk — so an edit, *even through a
28//!   tool lean-ctx never observes* (native editors, `git checkout`), is
29//!   reflected on the very next search instead of lingering for a TTL window.
30//!   A push-based fs-watcher (zero per-lookup cost) is a possible future
31//!   optimization on top of this correctness gate.
32
33use std::collections::{HashMap, HashSet};
34use std::path::{Path, PathBuf};
35use std::sync::{Arc, Mutex, OnceLock};
36use std::time::{Duration, Instant};
37
38use glob::Pattern;
39use ignore::WalkBuilder;
40
41use crate::tools::ctx_search::{MAX_FILE_SIZE, MAX_WALK_DEPTH, is_binary_ext, is_generated_file};
42
43/// Upper bound on indexed files; larger trees fall back to the walk path.
44const MAX_FILES: usize = 200_000;
45
46/// Posting-entry budget (`file_id` occurrences across all trigrams). Up to this
47/// many entries we keep exact inverted posting lists (fastest, sparse lookups).
48/// Beyond it we switch to the per-file Bloom tier instead of giving up — see
49/// [`Narrowing`]. ~4 bytes each → ~48 MB before the switch.
50const MAX_POSTING_ENTRIES: usize = 12_000_000;
51
52/// Hard ceiling on total trigram entries collected during a build. Past this we
53/// abandon indexing (walk fallback) to avoid pathological memory use even with
54/// the compact Bloom tier.
55const MAX_TOTAL_ENTRIES: usize = 48_000_000;
56
57/// Bloom tuning: bits per distinct trigram and number of hash probes. ~12 bits
58/// with k=7 keeps the false-positive rate well under 1% — and a false positive
59/// only costs one extra regex-verified file read (never a missed match).
60const BLOOM_BITS_PER_ITEM: usize = 12;
61const BLOOM_K: usize = 7;
62/// Per-file Bloom size clamp (in bits): 64 bits min, 1 Mi bits (128 KiB) max.
63const BLOOM_MIN_BITS: usize = 64;
64const BLOOM_MAX_BITS: usize = 1 << 20;
65
66/// A trigram is indexable only if all three bytes are `[A-Za-z0-9_]`.
67fn is_word_byte(b: u8) -> bool {
68    b.is_ascii_alphanumeric() || b == b'_'
69}
70
71fn pack(b0: u8, b1: u8, b2: u8) -> u32 {
72    (u32::from(b0) << 16) | (u32::from(b1) << 8) | u32::from(b2)
73}
74
75/// How candidate files are narrowed for a literal query. Two tiers, chosen by
76/// corpus size, both providing a *superset* of true matches (zero false
77/// negatives) which `ctx_search` then regex-verifies:
78/// - `Postings`: exact inverted lists `trigram → sorted file ids`. Fast, sparse
79///   lookups; used while total entries fit [`MAX_POSTING_ENTRIES`].
80/// - `Blooms`: one compact per-file Bloom filter of the file's trigrams. ~3×
81///   smaller than postings, so monorepos that would otherwise blow the posting
82///   budget still get index-narrowing instead of a full directory walk.
83enum Narrowing {
84    Postings(HashMap<u32, Vec<u32>>),
85    Blooms(Vec<FileBloom>),
86}
87
88/// A per-file Bloom filter over the file's word-trigrams. No false negatives:
89/// if any probed bit for a trigram is unset, the file provably lacks it.
90struct FileBloom {
91    /// Bit storage; the filter width `m = bits.len() * 64` is a power of two.
92    bits: Vec<u64>,
93}
94
95/// 64-bit avalanche mix (splitmix64 finalizer) — spreads a packed trigram into
96/// a well-distributed hash for double-probing.
97#[inline]
98fn mix64(mut x: u64) -> u64 {
99    x = x.wrapping_add(0x9E37_79B9_7F4A_7C15);
100    x = (x ^ (x >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
101    x = (x ^ (x >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
102    x ^ (x >> 31)
103}
104
105impl FileBloom {
106    fn with_capacity(distinct_trigrams: usize) -> Self {
107        let target = distinct_trigrams
108            .saturating_mul(BLOOM_BITS_PER_ITEM)
109            .next_power_of_two()
110            .clamp(BLOOM_MIN_BITS, BLOOM_MAX_BITS);
111        FileBloom {
112            bits: vec![0u64; target / 64],
113        }
114    }
115
116    #[inline]
117    fn m_bits(&self) -> usize {
118        self.bits.len() * 64
119    }
120
121    /// Double hashing: `p_i = h1 + i·h2 (mod m)` with `m` a power of two.
122    #[inline]
123    fn probes(&self, trigram: u32) -> impl Iterator<Item = usize> + '_ {
124        let m = self.m_bits();
125        let mask = m - 1; // m is a power of two
126        let h = mix64(u64::from(trigram));
127        let h1 = (h & 0xFFFF_FFFF) as usize;
128        let h2 = ((h >> 32) as usize) | 1; // odd step → full-period probing
129        (0..BLOOM_K).map(move |i| h1.wrapping_add(i.wrapping_mul(h2)) & mask)
130    }
131
132    fn insert(&mut self, trigram: u32) {
133        for p in self.probes(trigram).collect::<Vec<_>>() {
134            self.bits[p / 64] |= 1u64 << (p % 64);
135        }
136    }
137
138    fn maybe_contains(&self, trigram: u32) -> bool {
139        self.probes(trigram)
140            .all(|p| self.bits[p / 64] & (1u64 << (p % 64)) != 0)
141    }
142}
143
144/// RAM-resident trigram index over one project root.
145pub struct SearchIndex {
146    files: Vec<PathBuf>,
147    /// Candidate-narrowing structure (exact postings or compact per-file Bloom).
148    narrowing: Narrowing,
149    respect_gitignore: bool,
150    allow_secret_paths: bool,
151    /// Signature of the on-disk corpus this index was built from — the freshness
152    /// truth checked on every [`get_fresh`] (see [`corpus_signature`]).
153    signature: u64,
154}
155
156impl SearchIndex {
157    /// Build the index by walking `root` with the exact same config and filters
158    /// as `ctx_search`, so the searchable file universe is identical.
159    pub fn build(root: &str, respect_gitignore: bool, allow_secret_paths: bool) -> Option<Self> {
160        let mut files: Vec<PathBuf> = Vec::new();
161        // Per-file sorted, deduped trigrams. Same memory as the posting lists
162        // would be, but grouped by file so we can materialise *either* tier
163        // afterwards without a second pass over the corpus.
164        let mut per_file_trigrams: Vec<Vec<u32>> = Vec::new();
165        let mut total_entries: usize = 0;
166        let mut scratch: HashSet<u32> = HashSet::new();
167        // Corpus-signature accumulators. The running sum is order-independent
168        // (commutative `wrapping_add`) and the file count is folded in at the end
169        // so a change that happens to cancel under addition is still detected.
170        // Folded over the *eligible-by-stat* universe — before the read below —
171        // so the stat-only re-walk in [`corpus_signature`] reproduces it exactly,
172        // including files that turn out to be non-UTF-8 (counted, never indexed).
173        let mut sig_sum: u64 = 0;
174        let mut file_count: usize = 0;
175        let mut aborted = false;
176
177        walk_index_corpus(
178            root,
179            respect_gitignore,
180            allow_secret_paths,
181            |path, state| {
182                sig_sum = sig_sum.wrapping_add(file_sig(path, state));
183                file_count += 1;
184
185                // Read the corpus exactly once (issue #148): reuse a fresh cached
186                // copy if a prior `ctx_search`/build already read this file, else
187                // read it now and publish it so the upcoming `ctx_search` verify
188                // pass is an in-memory hit instead of a second disk read. Mirrors
189                // ctx_search: a non-UTF-8 file is never searchable, so it is skipped
190                // for trigrams (but already folded into the signature above).
191                let content: std::sync::Arc<str> = if let Some(cached) =
192                    state.and_then(|s| crate::core::content_cache::get(path, s))
193                {
194                    cached
195                } else {
196                    let Ok(text) = std::fs::read_to_string(path) else {
197                        return true;
198                    };
199                    let arc: std::sync::Arc<str> = std::sync::Arc::from(text);
200                    if let Some(s) = state {
201                        crate::core::content_cache::insert(path, s, std::sync::Arc::clone(&arc));
202                    }
203                    arc
204                };
205
206                if files.len() >= MAX_FILES {
207                    aborted = true; // too large even for the Bloom tier — use the walk
208                    return false;
209                }
210
211                scratch.clear();
212                let bytes = content.as_bytes();
213                if bytes.len() >= 3 {
214                    for w in bytes.windows(3) {
215                        if is_word_byte(w[0]) && is_word_byte(w[1]) && is_word_byte(w[2]) {
216                            scratch.insert(pack(w[0], w[1], w[2]));
217                        }
218                    }
219                }
220                total_entries += scratch.len();
221                if total_entries > MAX_TOTAL_ENTRIES {
222                    aborted = true; // memory guard — fall back to walk
223                    return false;
224                }
225                let mut tris: Vec<u32> = scratch.iter().copied().collect();
226                tris.sort_unstable();
227                files.push(path.to_path_buf());
228                per_file_trigrams.push(tris);
229                true
230            },
231        )?;
232
233        if aborted {
234            return None;
235        }
236
237        let narrowing = build_narrowing(&per_file_trigrams, total_entries);
238
239        Some(Self {
240            files,
241            narrowing,
242            respect_gitignore,
243            allow_secret_paths,
244            signature: finalize_sig(sig_sum, file_count),
245        })
246    }
247
248    fn config_matches(&self, respect_gitignore: bool, allow_secret_paths: bool) -> bool {
249        self.respect_gitignore == respect_gitignore && self.allow_secret_paths == allow_secret_paths
250    }
251
252    /// Candidate files for `pattern`, filtered by the `include` glob (matched
253    /// against each file's path relative to `root`). `None` means "no safe
254    /// narrowing possible" — the caller should scan the full file list.
255    ///
256    /// Narrowing is applied only for pure `[A-Za-z0-9_]` literals of length ≥ 3.
257    /// For such a literal every match contains it on a single line, hence the
258    /// file contains all of its consecutive trigrams: intersecting their
259    /// posting lists yields a *superset* of matching files (zero false
260    /// negatives), which the caller then regex-verifies.
261    pub fn candidate_paths(
262        &self,
263        pattern: &str,
264        includes: &[Pattern],
265        root: &Path,
266    ) -> CandidateSet {
267        if let Some(ids) = self.literal_candidates(pattern) {
268            let paths = ids
269                .into_iter()
270                .map(|id| self.files[id as usize].clone())
271                .filter(|p| glob_matches(p, includes, root))
272                .collect();
273            CandidateSet::Narrowed(paths)
274        } else {
275            let paths = self
276                .files
277                .iter()
278                .filter(|p| glob_matches(p, includes, root))
279                .cloned()
280                .collect();
281            CandidateSet::FullList(paths)
282        }
283    }
284
285    /// Returns candidate file ids for a pure-literal query, or `None` if the
286    /// query is not a trigram-narrowable pure `[A-Za-z0-9_]` literal. Both tiers
287    /// return a *superset* of true matches (zero false negatives).
288    fn literal_candidates(&self, pattern: &str) -> Option<Vec<u32>> {
289        let bytes = pattern.as_bytes();
290        if bytes.len() < 3 || !bytes.iter().all(|&b| is_word_byte(b)) {
291            return None;
292        }
293        // Distinct trigrams of the literal.
294        let mut tris: Vec<u32> = bytes.windows(3).map(|w| pack(w[0], w[1], w[2])).collect();
295        tris.sort_unstable();
296        tris.dedup();
297
298        match &self.narrowing {
299            Narrowing::Postings(trigrams) => Some(Self::postings_intersect(trigrams, &tris)),
300            Narrowing::Blooms(blooms) => Some(Self::bloom_scan(blooms, &tris)),
301        }
302    }
303
304    /// Exact-tier: intersect the posting lists of every required trigram
305    /// (smallest first for a cheap intersection).
306    fn postings_intersect(trigrams: &HashMap<u32, Vec<u32>>, tris: &[u32]) -> Vec<u32> {
307        let mut lists: Vec<&Vec<u32>> = Vec::with_capacity(tris.len());
308        for &tri in tris {
309            match trigrams.get(&tri) {
310                // A required trigram is absent → provably no match anywhere.
311                None => return Vec::new(),
312                Some(list) => lists.push(list),
313            }
314        }
315        lists.sort_by_key(|l| l.len());
316
317        let mut acc: Vec<u32> = lists[0].clone();
318        for list in &lists[1..] {
319            acc = intersect_sorted(&acc, list);
320            if acc.is_empty() {
321                break;
322            }
323        }
324        acc
325    }
326
327    /// Bloom-tier: a file is a candidate iff its Bloom filter may contain every
328    /// required trigram. No false negatives (an unset probe bit ⇒ the trigram is
329    /// provably absent), so the result is still a superset of true matches.
330    fn bloom_scan(blooms: &[FileBloom], tris: &[u32]) -> Vec<u32> {
331        let mut out = Vec::new();
332        for (fid, bloom) in blooms.iter().enumerate() {
333            if tris.iter().all(|&t| bloom.maybe_contains(t)) {
334                out.push(fid as u32);
335            }
336        }
337        out
338    }
339}
340
341/// Materialise the appropriate narrowing tier for a freshly walked corpus.
342fn build_narrowing(per_file: &[Vec<u32>], total_entries: usize) -> Narrowing {
343    if total_entries <= MAX_POSTING_ENTRIES {
344        let mut trigrams: HashMap<u32, Vec<u32>> = HashMap::new();
345        for (fid, tris) in per_file.iter().enumerate() {
346            for &t in tris {
347                // file ids are appended in ascending order ⇒ lists stay sorted.
348                trigrams.entry(t).or_default().push(fid as u32);
349            }
350        }
351        Narrowing::Postings(trigrams)
352    } else {
353        let blooms = per_file
354            .iter()
355            .map(|tris| {
356                let mut b = FileBloom::with_capacity(tris.len());
357                for &t in tris {
358                    b.insert(t);
359                }
360                b
361            })
362            .collect();
363        Narrowing::Blooms(blooms)
364    }
365}
366
367/// Result of [`SearchIndex::candidate_paths`].
368pub enum CandidateSet {
369    /// Trigram-narrowed candidate files (a superset of real matches).
370    Narrowed(Vec<PathBuf>),
371    /// No safe narrowing — the full cached file list (still skips the walk).
372    FullList(Vec<PathBuf>),
373}
374
375impl CandidateSet {
376    pub fn into_paths(self) -> Vec<PathBuf> {
377        match self {
378            CandidateSet::Narrowed(p) | CandidateSet::FullList(p) => p,
379        }
380    }
381}
382
383/// True when `path` matches *any* of the `includes` globs (relative to `root`),
384/// or when there is no filter (`includes` empty).
385fn glob_matches(path: &Path, includes: &[Pattern], root: &Path) -> bool {
386    if includes.is_empty() {
387        return true;
388    }
389    let rel = path.strip_prefix(root).unwrap_or(path);
390    let rel_str = rel.to_string_lossy();
391    includes.iter().any(|p| p.matches(&rel_str))
392}
393
394/// Intersection of two ascending, deduped `u32` slices.
395fn intersect_sorted(a: &[u32], b: &[u32]) -> Vec<u32> {
396    let mut out = Vec::new();
397    let (mut i, mut j) = (0, 0);
398    while i < a.len() && j < b.len() {
399        match a[i].cmp(&b[j]) {
400            std::cmp::Ordering::Less => i += 1,
401            std::cmp::Ordering::Greater => j += 1,
402            std::cmp::Ordering::Equal => {
403                out.push(a[i]);
404                i += 1;
405                j += 1;
406            }
407        }
408    }
409    out
410}
411
412// ---------------------------------------------------------------------------
413// Corpus freshness: the build and its freshness check share one traversal so
414// the signature is computed over an identical file universe (parity is what
415// makes the equality check trustworthy).
416// ---------------------------------------------------------------------------
417
418/// Walk `root` with the *exact* `ctx_search` file universe — same gitignore
419/// semantics, depth limit, and binary/generated/secret/size/regular-file guards
420/// — invoking `visit(path, state)` for every eligible regular file with its
421/// `(mtime, size)` identity (`None` only when the platform cannot report mtime).
422/// `visit` returns `false` to stop early. Returns `None` for a missing or unsafe
423/// scan root (HOME, fs root, …), the same guard that makes the caller fall back
424/// to a direct walk. This traversal reads nothing: it is the shared spine of
425/// both [`SearchIndex::build`] and [`corpus_signature`].
426fn walk_index_corpus<F>(
427    root: &str,
428    respect_gitignore: bool,
429    allow_secret_paths: bool,
430    mut visit: F,
431) -> Option<()>
432where
433    F: FnMut(&Path, Option<crate::core::content_cache::FileState>) -> bool,
434{
435    let root_path = Path::new(root);
436    if !root_path.exists() {
437        return None;
438    }
439    // Never auto-index a broad/unsafe root (HOME, filesystem root, a dir with
440    // dozens of unrelated subtrees). Mirrors the graph/BM25 guard and stops a
441    // walk of the whole home directory — which on Windows would hydrate OneDrive
442    // placeholders (#363).
443    if !crate::core::graph_index::is_safe_scan_root_public(root) {
444        return None;
445    }
446
447    let walker = WalkBuilder::new(root_path)
448        .hidden(true)
449        .max_depth(Some(MAX_WALK_DEPTH))
450        .git_ignore(respect_gitignore)
451        .git_global(respect_gitignore)
452        .git_exclude(respect_gitignore)
453        .require_git(false)
454        .filter_entry(crate::core::walk_filter::keep_entry)
455        .build();
456
457    for entry in walker.filter_map(std::result::Result::ok) {
458        if entry.file_type().is_none_or(|ft| ft.is_dir()) {
459            continue;
460        }
461        if entry.file_type().is_some_and(|ft| ft.is_symlink()) {
462            continue;
463        }
464        let path = entry.path();
465        if is_binary_ext(path) || is_generated_file(path) {
466            continue;
467        }
468        if !allow_secret_paths && crate::core::io_boundary::is_secret_like(path).is_some() {
469            continue;
470        }
471        // `metadata` (stat) never opens the file, so it cannot block on a
472        // FIFO/socket/device node (#336); those are filtered out here.
473        let state = match std::fs::metadata(path) {
474            Ok(meta) if !meta.file_type().is_file() => continue,
475            Ok(meta) if meta.len() > MAX_FILE_SIZE => continue,
476            Ok(meta) => crate::core::content_cache::FileState::from_metadata(&meta),
477            Err(_) => continue,
478        };
479        if !visit(path, state) {
480            break;
481        }
482    }
483    Some(())
484}
485
486/// Stable per-file contribution to the corpus signature: folds the path with its
487/// `(mtime, size)` identity. Order-independent (callers combine with
488/// `wrapping_add`), so traversal order never affects the result. A file with no
489/// resolvable mtime contributes its path only — add/rename/delete are still
490/// detected, while an in-place edit on such an exotic filesystem is the same
491/// blind spot the `(mtime, size)` content cache already documents.
492fn file_sig(path: &Path, state: Option<crate::core::content_cache::FileState>) -> u64 {
493    let mut h: u64 = 0xcbf2_9ce4_8422_2325; // FNV-1a offset basis
494    for &b in path.as_os_str().as_encoded_bytes() {
495        h ^= u64::from(b);
496        h = h.wrapping_mul(0x0000_0100_0000_01b3); // FNV-1a prime
497    }
498    if let Some(st) = state {
499        h ^= mix64(st.mtime_ms).rotate_left(1);
500        h ^= mix64(st.size_bytes).rotate_left(33);
501    }
502    mix64(h)
503}
504
505/// Fold the order-independent per-file sum together with the file count into the
506/// final signature, so adding and removing files whose hashes cancel under
507/// addition still changes the result.
508fn finalize_sig(sum: u64, count: usize) -> u64 {
509    sum ^ mix64(count as u64).rotate_left(32)
510}
511
512/// Cheap, stat-only signature of the *current* on-disk corpus for `root`.
513/// Compared against [`SearchIndex::signature`] in [`get_fresh`] to detect any
514/// change — content edits (including via tools lean-ctx never observes),
515/// additions, renames and deletions — so a stale candidate set can never
516/// silently drop a real match. `None` mirrors a non-indexable root (the caller
517/// then walks directly).
518fn corpus_signature(root: &str, respect_gitignore: bool, allow_secret_paths: bool) -> Option<u64> {
519    let mut sum: u64 = 0;
520    let mut count: usize = 0;
521    walk_index_corpus(
522        root,
523        respect_gitignore,
524        allow_secret_paths,
525        |path, state| {
526            sum = sum.wrapping_add(file_sig(path, state));
527            count += 1;
528            true
529        },
530    )?;
531    Some(finalize_sig(sum, count))
532}
533
534// ---------------------------------------------------------------------------
535// Resident cache (one index per project root) with background (re)build.
536// ---------------------------------------------------------------------------
537
538struct CacheEntry {
539    index: Option<Arc<SearchIndex>>,
540    building: bool,
541    /// When this root's resident index was last confirmed current against disk.
542    /// Gates the optional coalesce window in [`get_fresh`]; `None` forces a fresh
543    /// verification on the next lookup.
544    last_verified: Option<Instant>,
545}
546
547impl CacheEntry {
548    fn empty() -> Self {
549        Self {
550            index: None,
551            building: false,
552            last_verified: None,
553        }
554    }
555}
556
557static CACHE: OnceLock<Mutex<HashMap<String, CacheEntry>>> = OnceLock::new();
558
559fn cache() -> &'static Mutex<HashMap<String, CacheEntry>> {
560    CACHE.get_or_init(|| Mutex::new(HashMap::new()))
561}
562
563/// Escape hatch: `LEAN_CTX_DISABLE_SEARCH_INDEX=1` forces the walk path
564/// everywhere (debugging / A-B measurement / opt-out).
565fn index_disabled() -> bool {
566    std::env::var("LEAN_CTX_DISABLE_SEARCH_INDEX")
567        .is_ok_and(|v| v == "1" || v.eq_ignore_ascii_case("true"))
568}
569
570/// Optional coalescing window for the freshness check. Default `0` (disabled):
571/// every [`get_fresh`] re-verifies the corpus signature against disk, so a fresh
572/// edit is never missed. On very large indexed trees, set
573/// `LEAN_CTX_SEARCH_INDEX_COALESCE_MS` to trade a bounded staleness window for
574/// fewer stat-walks under bursty search load.
575fn coalesce_window() -> Option<Duration> {
576    let ms = std::env::var("LEAN_CTX_SEARCH_INDEX_COALESCE_MS")
577        .ok()
578        .and_then(|v| v.trim().parse::<u64>().ok())
579        .unwrap_or(0);
580    (ms > 0).then(|| Duration::from_millis(ms))
581}
582
583/// Returns a fresh resident index for `root` if one is available for the given
584/// config, otherwise spawns a background (re)build and returns `None` so the
585/// caller uses the walk fallback for this call.
586pub fn get_fresh(
587    root: &str,
588    respect_gitignore: bool,
589    allow_secret_paths: bool,
590) -> Option<Arc<SearchIndex>> {
591    // Privileged "ignore gitignore" scans are rare and bypass the index.
592    if !respect_gitignore || index_disabled() {
593        return None;
594    }
595
596    // Phase 1 (locked, O(1)): grab the resident index for this config, if any,
597    // together with when it was last verified against disk.
598    let (candidate, last_verified) = {
599        let map = cache()
600            .lock()
601            .unwrap_or_else(std::sync::PoisonError::into_inner);
602        match map.get(root) {
603            Some(entry) => match &entry.index {
604                Some(idx) if idx.config_matches(respect_gitignore, allow_secret_paths) => {
605                    (Some(Arc::clone(idx)), entry.last_verified)
606                }
607                _ => (None, None),
608            },
609            None => (None, None),
610        }
611    };
612
613    let Some(idx) = candidate else {
614        // No usable index yet → build in the background, walk this call.
615        request_build(root, respect_gitignore, allow_secret_paths);
616        return None;
617    };
618
619    // Coalesce (opt-in, default off): inside the window trust the recent
620    // verification and skip the stat-walk — keeps bursty search load O(1).
621    if let Some(window) = coalesce_window()
622        && last_verified.is_some_and(|t| t.elapsed() < window)
623    {
624        return Some(idx);
625    }
626
627    // Phase 2 (unlocked): verify the live corpus signature. Deliberately held
628    // *outside* the cache mutex so a multi-millisecond stat-walk never serializes
629    // other roots or concurrent searches.
630    match corpus_signature(root, respect_gitignore, allow_secret_paths) {
631        Some(sig) if sig == idx.signature => {
632            mark_verified(root);
633            Some(idx)
634        }
635        _ => {
636            // Corpus changed (or root no longer indexable): walk accurately now
637            // and rebuild the index in the background for the next call.
638            request_build(root, respect_gitignore, allow_secret_paths);
639            None
640        }
641    }
642}
643
644/// Record that `root`'s resident index was just confirmed current, extending its
645/// coalesce window.
646fn mark_verified(root: &str) {
647    let mut map = cache()
648        .lock()
649        .unwrap_or_else(std::sync::PoisonError::into_inner);
650    if let Some(entry) = map.get_mut(root) {
651        entry.last_verified = Some(Instant::now());
652    }
653}
654
655/// Spawn a background (re)build for `root` unless one is already in flight.
656fn request_build(root: &str, respect_gitignore: bool, allow_secret_paths: bool) {
657    let needs_build = {
658        let mut map = cache()
659            .lock()
660            .unwrap_or_else(std::sync::PoisonError::into_inner);
661        let entry = map
662            .entry(root.to_string())
663            .or_insert_with(CacheEntry::empty);
664        if entry.building {
665            false
666        } else {
667            entry.building = true;
668            true
669        }
670    };
671    if needs_build {
672        spawn_build(root.to_string(), respect_gitignore, allow_secret_paths);
673    }
674}
675
676/// Ensure a resident index for `root` is built (or building) in the background.
677/// Safe to call repeatedly; deduped via the per-root `building` flag.
678pub fn ensure_background(root: &str, respect_gitignore: bool, allow_secret_paths: bool) {
679    if !respect_gitignore || index_disabled() {
680        return;
681    }
682    // Prewarm only when there is no usable index yet — staleness of an existing
683    // index is corrected on demand by `get_fresh`'s signature gate, so there is
684    // nothing to proactively refresh here.
685    let has_index = {
686        let map = cache()
687            .lock()
688            .unwrap_or_else(std::sync::PoisonError::into_inner);
689        map.get(root).is_some_and(|entry| {
690            entry
691                .index
692                .as_ref()
693                .is_some_and(|idx| idx.config_matches(respect_gitignore, allow_secret_paths))
694        })
695    };
696    if !has_index {
697        request_build(root, respect_gitignore, allow_secret_paths);
698    }
699}
700
701/// Build the index synchronously and install it in the resident cache.
702/// Returns `true` on success. Useful for CLI prewarm and benchmarks that need a
703/// guaranteed-warm index. Respects the disable env var.
704pub fn warm_blocking(root: &str, respect_gitignore: bool, allow_secret_paths: bool) -> bool {
705    if !respect_gitignore || index_disabled() {
706        return false;
707    }
708    let Some(idx) = SearchIndex::build(root, respect_gitignore, allow_secret_paths) else {
709        return false;
710    };
711    let mut map = cache()
712        .lock()
713        .unwrap_or_else(std::sync::PoisonError::into_inner);
714    map.insert(
715        root.to_string(),
716        CacheEntry {
717            index: Some(Arc::new(idx)),
718            building: false,
719            last_verified: Some(Instant::now()),
720        },
721    );
722    true
723}
724
725/// Per-repo lock name serializing the resident search-index build across
726/// processes, mirroring the `graph-idx` / `bm25-idx` locks in
727/// [`crate::core::index_orchestrator`]. Distinct `search-` prefix so the three
728/// indexers never serialize against one another.
729fn search_index_lock_name(root: &str) -> String {
730    format!(
731        "search-idx-{}",
732        &crate::core::index_namespace::namespace_hash(Path::new(root))[..8]
733    )
734}
735
736/// Outcome of a guarded background build: did this process do the walk, or did
737/// it yield to another process already building the same root?
738#[derive(Debug, Clone, Copy, PartialEq, Eq)]
739enum BuildOutcome {
740    Built,
741    Deferred,
742}
743
744/// Build the resident index under a cross-process herd guard (#460).
745///
746/// The trigram index is RAM-resident (not shareable on disk), so on lock
747/// contention we *defer* the proactive pre-warm instead of running a second
748/// simultaneous file walk: a boot wave of N sessions on one repo then triggers
749/// ~1 walk at a time, not N. Deferring is safe — `ctx_search` still works via
750/// its walk fallback, and the per-process `building` flag is cleared so the next
751/// `get_fresh` / `ensure_background` nudge retries once the holder releases. The
752/// short 200 ms wait keeps the common single-session path latency-free.
753fn build_guarded(root: &str, respect_gitignore: bool, allow_secret_paths: bool) -> BuildOutcome {
754    let lock = crate::core::startup_guard::try_acquire_lock(
755        &search_index_lock_name(root),
756        Duration::from_millis(200),
757        Duration::from_mins(3),
758    );
759    if lock.is_none() {
760        // Another process owns the build. Clear the in-flight flag so a later
761        // nudge retries rather than leaving `building` stuck true forever.
762        let mut map = cache()
763            .lock()
764            .unwrap_or_else(std::sync::PoisonError::into_inner);
765        if let Some(entry) = map.get_mut(root) {
766            entry.building = false;
767        }
768        return BuildOutcome::Deferred;
769    }
770
771    let built = std::panic::catch_unwind(|| {
772        SearchIndex::build(root, respect_gitignore, allow_secret_paths)
773    })
774    .ok()
775    .flatten();
776
777    let mut map = cache()
778        .lock()
779        .unwrap_or_else(std::sync::PoisonError::into_inner);
780    if let Some(entry) = map.get_mut(root) {
781        entry.building = false;
782        if let Some(idx) = built {
783            entry.index = Some(Arc::new(idx));
784            // A just-built index is current with the disk it walked.
785            entry.last_verified = Some(Instant::now());
786        }
787    }
788    // `lock` is held until here so the cross-process guard spans the whole walk.
789    drop(lock);
790    BuildOutcome::Built
791}
792
793fn spawn_build(root: String, respect_gitignore: bool, allow_secret_paths: bool) {
794    std::thread::spawn(move || {
795        let _ = build_guarded(&root, respect_gitignore, allow_secret_paths);
796    });
797}
798
799#[cfg(test)]
800mod tests {
801    use super::*;
802
803    fn corpus() -> tempfile::TempDir {
804        let dir = tempfile::tempdir().unwrap();
805        std::fs::write(dir.path().join("a.rs"), "fn handler() {}\nlet x = 1;\n").unwrap();
806        std::fs::write(dir.path().join("b.rs"), "fn other() {}\n// nothing here\n").unwrap();
807        std::fs::write(dir.path().join("c.txt"), "handler appears in text too\n").unwrap();
808        dir
809    }
810
811    #[test]
812    fn build_refuses_to_index_home_directory() {
813        // Auto-indexing HOME would walk the entire home tree and, on Windows,
814        // hydrate every OneDrive placeholder (#363). The build must bail out.
815        if let Some(home) = dirs::home_dir() {
816            assert!(
817                SearchIndex::build(&home.to_string_lossy(), true, false).is_none(),
818                "search index must never auto-build over the home directory"
819            );
820        }
821    }
822
823    #[test]
824    fn narrows_to_files_containing_literal() {
825        let dir = corpus();
826        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
827        let cands = idx.candidate_paths("handler", &[], dir.path());
828        let paths = cands.into_paths();
829        // a.rs and c.txt contain "handler"; b.rs must be excluded.
830        assert!(paths.iter().any(|p| p.ends_with("a.rs")));
831        assert!(paths.iter().any(|p| p.ends_with("c.txt")));
832        assert!(!paths.iter().any(|p| p.ends_with("b.rs")));
833    }
834
835    #[test]
836    fn absent_trigram_yields_empty_candidates() {
837        let dir = corpus();
838        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
839        match idx.candidate_paths("zzzqqq", &[], dir.path()) {
840            CandidateSet::Narrowed(p) => assert!(p.is_empty()),
841            CandidateSet::FullList(_) => panic!("pure literal should narrow"),
842        }
843    }
844
845    #[test]
846    fn ext_filter_restricts_candidates() {
847        let dir = corpus();
848        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
849        let paths = idx
850            .candidate_paths(
851                "handler",
852                &[glob::Pattern::new("*.rs").unwrap()],
853                dir.path(),
854            )
855            .into_paths();
856        assert!(paths.iter().all(|p| p.extension().unwrap() == "rs"));
857        assert!(paths.iter().any(|p| p.ends_with("a.rs")));
858    }
859
860    #[test]
861    #[cfg(unix)]
862    fn build_skips_named_pipe_without_hanging() {
863        use std::sync::mpsc;
864        use std::time::Duration;
865        // #336: the background index build read every file, so a FIFO in the
866        // corpus blocked the build thread forever. It must be skipped while the
867        // regular files are still indexed, and the build must return.
868        let dir = corpus();
869        let fifo = dir.path().join("pipe.fifo");
870        let c = std::ffi::CString::new(fifo.to_string_lossy().as_bytes()).unwrap();
871        assert_eq!(
872            // SAFETY: `c` is a live CString providing a valid NUL-terminated
873            // path pointer for the duration of the call.
874            unsafe { libc::mkfifo(c.as_ptr(), 0o644) },
875            0,
876            "mkfifo failed"
877        );
878
879        let root = dir.path().to_str().unwrap().to_string();
880        let (tx, rx) = mpsc::channel();
881        std::thread::spawn(move || {
882            let built = SearchIndex::build(&root, true, false);
883            let _ = tx.send(built.map(|idx| {
884                idx.candidate_paths("handler", &[], std::path::Path::new(&root))
885                    .into_paths()
886            }));
887        });
888        let paths = rx
889            .recv_timeout(Duration::from_secs(5))
890            .expect("SearchIndex::build hung on a FIFO (#336 regression)")
891            .expect("index should build");
892        assert!(paths.iter().any(|p| p.ends_with("a.rs")));
893        assert!(!paths.iter().any(|p| p.ends_with("pipe.fifo")));
894    }
895
896    #[test]
897    fn regex_query_falls_back_to_full_list() {
898        let dir = corpus();
899        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
900        match idx.candidate_paths("fn .*\\(\\)", &[], dir.path()) {
901            CandidateSet::FullList(p) => assert!(!p.is_empty()),
902            CandidateSet::Narrowed(_) => panic!("metachar query must not narrow"),
903        }
904    }
905
906    #[test]
907    fn short_query_falls_back_to_full_list() {
908        let dir = corpus();
909        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
910        assert!(matches!(
911            idx.candidate_paths("fn", &[], dir.path()),
912            CandidateSet::FullList(_)
913        ));
914    }
915
916    /// The core correctness claim: trigram narrowing never drops a real match.
917    /// For each literal query, the set of `file:line` hits found by scanning only
918    /// the narrowed candidates must equal the set found by scanning every file.
919    #[test]
920    fn narrowing_has_identical_recall_to_full_scan() {
921        use regex::Regex;
922        use std::collections::BTreeSet;
923
924        let dir = tempfile::tempdir().unwrap();
925        // A spread of files; some contain the query tokens, most do not.
926        let samples = [
927            (
928                "auth/login.rs",
929                "fn authenticate(user) {}\nlet token = mint();\n",
930            ),
931            (
932                "auth/session.rs",
933                "struct Session;\n// authenticate again here\n",
934            ),
935            ("db/pool.rs", "fn connect() {}\nlet retries = 3;\n"),
936            (
937                "ui/button.tsx",
938                "export const Button = () => authenticate;\n",
939            ),
940            ("readme.md", "This project uses authenticate flows.\n"),
941            ("unrelated.rs", "fn helper() { let v = 1; }\n"),
942        ];
943        for (rel, content) in samples {
944            let p = dir.path().join(rel);
945            std::fs::create_dir_all(p.parent().unwrap()).unwrap();
946            std::fs::write(p, content).unwrap();
947        }
948        let idx = SearchIndex::build(dir.path().to_str().unwrap(), true, false).unwrap();
949
950        let full_scan = |pat: &str| -> BTreeSet<String> {
951            let re = Regex::new(pat).unwrap();
952            let mut hits = BTreeSet::new();
953            for (rel, content) in samples {
954                for (i, line) in content.lines().enumerate() {
955                    if re.is_match(line) {
956                        hits.insert(format!("{rel}:{}", i + 1));
957                    }
958                }
959            }
960            hits
961        };
962
963        for query in ["authenticate", "Session", "retries", "token"] {
964            let re = Regex::new(query).unwrap();
965            let candidates = idx.candidate_paths(query, &[], dir.path()).into_paths();
966            let mut narrowed = BTreeSet::new();
967            for path in &candidates {
968                let content = std::fs::read_to_string(path).unwrap();
969                let rel = path.strip_prefix(dir.path()).unwrap().to_string_lossy();
970                for (i, line) in content.lines().enumerate() {
971                    if re.is_match(line) {
972                        narrowed.insert(format!("{}:{}", rel.replace('\\', "/"), i + 1));
973                    }
974                }
975            }
976            assert_eq!(
977                narrowed,
978                full_scan(query),
979                "recall mismatch for query {query:?}"
980            );
981        }
982    }
983
984    #[test]
985    fn intersect_sorted_basic() {
986        assert_eq!(
987            intersect_sorted(&[1, 2, 3, 5], &[2, 3, 4, 5]),
988            vec![2, 3, 5]
989        );
990        assert_eq!(intersect_sorted(&[1, 2], &[3, 4]), Vec::<u32>::new());
991    }
992
993    // ── Bloom tier ────────────────────────────────────────────────────────
994
995    fn trigrams_of(s: &str) -> Vec<u32> {
996        let mut set = HashSet::new();
997        let b = s.as_bytes();
998        if b.len() >= 3 {
999            for w in b.windows(3) {
1000                if is_word_byte(w[0]) && is_word_byte(w[1]) && is_word_byte(w[2]) {
1001                    set.insert(pack(w[0], w[1], w[2]));
1002                }
1003            }
1004        }
1005        let mut v: Vec<u32> = set.into_iter().collect();
1006        v.sort_unstable();
1007        v
1008    }
1009
1010    #[test]
1011    fn file_bloom_has_no_false_negatives() {
1012        let tris = trigrams_of("fn authenticate(user) { let token = mint(); }");
1013        let mut bloom = FileBloom::with_capacity(tris.len());
1014        for &t in &tris {
1015            bloom.insert(t);
1016        }
1017        // Every inserted trigram must be reported present (Bloom guarantee).
1018        assert!(tris.iter().all(|&t| bloom.maybe_contains(t)));
1019    }
1020
1021    /// Parity fuzz: the Bloom tier must return a *superset* of the exact posting
1022    /// tier for every query (zero false negatives). False positives are allowed
1023    /// (and verified away downstream), so we assert containment, not equality.
1024    #[test]
1025    fn bloom_tier_is_superset_of_postings_tier() {
1026        // Deterministic synthetic corpus (LCG → reproducible).
1027        let mut seed = 0x1234_5678_9abc_def0u64;
1028        let mut rng = || {
1029            seed = seed
1030                .wrapping_mul(6364136223846793005)
1031                .wrapping_add(1442695040888963407);
1032            (seed >> 33) as u32
1033        };
1034        let mut per_file: Vec<Vec<u32>> = Vec::new();
1035        for _ in 0..80 {
1036            let n = 50 + (rng() % 250) as usize;
1037            let mut s = HashSet::new();
1038            for _ in 0..n {
1039                s.insert(rng() & 0x00FF_FFFF);
1040            }
1041            let mut v: Vec<u32> = s.into_iter().collect();
1042            v.sort_unstable();
1043            per_file.push(v);
1044        }
1045        let total: usize = per_file.iter().map(Vec::len).sum();
1046
1047        let postings = build_narrowing(&per_file, total); // ≤ cap → postings
1048        let blooms = build_narrowing(&per_file, MAX_POSTING_ENTRIES + 1); // forced bloom
1049        let (Narrowing::Postings(pt), Narrowing::Blooms(bl)) = (&postings, &blooms) else {
1050            panic!("unexpected narrowing tiers");
1051        };
1052
1053        // Queries drawn from real file trigrams (these MUST be found by both),
1054        // plus a few that are unlikely to exist anywhere.
1055        for f in &per_file {
1056            if f.len() < 3 {
1057                continue;
1058            }
1059            let q = vec![f[0], f[f.len() / 2], f[f.len() - 1]];
1060            let exact = SearchIndex::postings_intersect(pt, &q);
1061            let bloom: HashSet<u32> = SearchIndex::bloom_scan(bl, &q).into_iter().collect();
1062            for id in exact {
1063                assert!(
1064                    bloom.contains(&id),
1065                    "Bloom tier dropped a true match (false negative) for {q:?}"
1066                );
1067            }
1068        }
1069    }
1070
1071    /// End-to-end: an index forced onto the Bloom tier must still surface every
1072    /// file that actually contains the literal (recall parity with a full scan).
1073    #[test]
1074    fn bloom_tier_end_to_end_recall() {
1075        let samples = [
1076            (
1077                "auth_login.rs",
1078                "fn authenticate(user) {}\nlet token = mint();\n",
1079            ),
1080            (
1081                "auth_session.rs",
1082                "struct Session;\n// authenticate again here\n",
1083            ),
1084            ("db_pool.rs", "fn connect() {}\nlet retries = 3;\n"),
1085            (
1086                "ui_button.tsx",
1087                "export const Button = () => authenticate;\n",
1088            ),
1089            ("readme.md", "This project uses authenticate flows.\n"),
1090            ("unrelated.rs", "fn helper() { let v = 1; }\n"),
1091        ];
1092        let files: Vec<PathBuf> = samples.iter().map(|(rel, _)| PathBuf::from(rel)).collect();
1093        let per_file: Vec<Vec<u32>> = samples.iter().map(|(_, c)| trigrams_of(c)).collect();
1094
1095        let idx = SearchIndex {
1096            files,
1097            narrowing: build_narrowing(&per_file, MAX_POSTING_ENTRIES + 1),
1098            respect_gitignore: true,
1099            allow_secret_paths: false,
1100            signature: 0, // freshness is not exercised by candidate_paths
1101        };
1102        assert!(
1103            matches!(idx.narrowing, Narrowing::Blooms(_)),
1104            "test must exercise the Bloom tier"
1105        );
1106
1107        for query in ["authenticate", "Session", "retries", "token"] {
1108            let cands: HashSet<String> = idx
1109                .candidate_paths(query, &[], std::path::Path::new(""))
1110                .into_paths()
1111                .iter()
1112                .map(|p| p.to_string_lossy().to_string())
1113                .collect();
1114            for (rel, content) in samples {
1115                if content.contains(query) {
1116                    assert!(
1117                        cands.contains(rel),
1118                        "Bloom tier dropped real match {rel} for query {query:?}"
1119                    );
1120                }
1121            }
1122        }
1123    }
1124
1125    /// A scoped override of `LEAN_CTX_DATA_DIR`, restored on drop, so the
1126    /// cross-process lock files land in an isolated temp dir during tests.
1127    struct DataDirGuard {
1128        prev: Option<String>,
1129    }
1130    impl DataDirGuard {
1131        fn set(path: &std::path::Path) -> Self {
1132            let prev = std::env::var("LEAN_CTX_DATA_DIR").ok();
1133            crate::test_env::set_var("LEAN_CTX_DATA_DIR", path);
1134            Self { prev }
1135        }
1136    }
1137    impl Drop for DataDirGuard {
1138        fn drop(&mut self) {
1139            match self.prev.as_deref() {
1140                Some(v) => crate::test_env::set_var("LEAN_CTX_DATA_DIR", v),
1141                None => crate::test_env::remove_var("LEAN_CTX_DATA_DIR"),
1142            }
1143        }
1144    }
1145
1146    #[test]
1147    fn search_index_lock_name_is_per_repo_and_distinct() {
1148        let a = search_index_lock_name("/tmp/repo-a");
1149        let b = search_index_lock_name("/tmp/repo-b");
1150        assert!(a.starts_with("search-idx-"), "unexpected lock name: {a}");
1151        assert_ne!(a, b, "lock name must be per-repo");
1152        assert_eq!(a, search_index_lock_name("/tmp/repo-a"), "stable per repo");
1153        // Must not collide with the graph/bm25 locks for the same repo, or the
1154        // three indexers would needlessly serialize against one another.
1155        let h = &crate::core::index_namespace::namespace_hash(Path::new("/tmp/repo-a"))[..8];
1156        assert_ne!(
1157            a,
1158            format!("graph-idx-{h}"),
1159            "must not collide with graph lock"
1160        );
1161        assert_ne!(
1162            a,
1163            format!("bm25-idx-{h}"),
1164            "must not collide with bm25 lock"
1165        );
1166    }
1167
1168    #[test]
1169    fn build_guarded_builds_when_uncontended() {
1170        let _env = crate::core::data_dir::test_env_lock();
1171        let data = tempfile::tempdir().unwrap();
1172        let _guard = DataDirGuard::set(data.path());
1173
1174        let dir = corpus();
1175        let root = dir.path().to_string_lossy().to_string();
1176        // Seed the in-flight flag the way `ensure_background` does before spawn.
1177        {
1178            let mut map = cache()
1179                .lock()
1180                .unwrap_or_else(std::sync::PoisonError::into_inner);
1181            map.insert(
1182                root.clone(),
1183                CacheEntry {
1184                    index: None,
1185                    building: true,
1186                    last_verified: None,
1187                },
1188            );
1189        }
1190        assert_eq!(
1191            build_guarded(&root, true, false),
1192            BuildOutcome::Built,
1193            "an uncontended root must build"
1194        );
1195        let map = cache()
1196            .lock()
1197            .unwrap_or_else(std::sync::PoisonError::into_inner);
1198        let entry = map.get(&root).expect("entry present");
1199        assert!(!entry.building, "building flag must clear after build");
1200        assert!(entry.index.is_some(), "index must be installed after build");
1201    }
1202
1203    #[test]
1204    fn build_guarded_defers_when_another_process_holds_the_lock() {
1205        let _env = crate::core::data_dir::test_env_lock();
1206        let data = tempfile::tempdir().unwrap();
1207        let _guard = DataDirGuard::set(data.path());
1208
1209        let dir = corpus();
1210        let root = dir.path().to_string_lossy().to_string();
1211        // Pre-hold the cross-process lock with *this* (alive) PID and a fresh
1212        // mtime, so neither the dead-owner nor the staleness reclaim can take it
1213        // — exactly the "another session is already building" state from #460.
1214        let lock_path = data
1215            .path()
1216            .join(format!(".{}.lock", search_index_lock_name(&root)));
1217        std::fs::write(&lock_path, format!("{}\n", std::process::id())).unwrap();
1218
1219        {
1220            let mut map = cache()
1221                .lock()
1222                .unwrap_or_else(std::sync::PoisonError::into_inner);
1223            map.insert(
1224                root.clone(),
1225                CacheEntry {
1226                    index: None,
1227                    building: true,
1228                    last_verified: None,
1229                },
1230            );
1231        }
1232        assert_eq!(
1233            build_guarded(&root, true, false),
1234            BuildOutcome::Deferred,
1235            "a contended root must defer the proactive pre-warm"
1236        );
1237        let map = cache()
1238            .lock()
1239            .unwrap_or_else(std::sync::PoisonError::into_inner);
1240        let entry = map.get(&root).expect("entry present");
1241        assert!(
1242            !entry.building,
1243            "deferred build must clear the in-flight flag so a later nudge retries"
1244        );
1245        assert!(
1246            entry.index.is_none(),
1247            "deferred build must not run a second walk / install an index"
1248        );
1249    }
1250
1251    #[test]
1252    fn corpus_signature_matches_a_freshly_built_index() {
1253        let dir = corpus();
1254        let root = dir.path().to_string_lossy().to_string();
1255        let idx = SearchIndex::build(&root, true, false).expect("index builds");
1256        let sig = corpus_signature(&root, true, false).expect("signature computes");
1257        assert_eq!(
1258            idx.signature, sig,
1259            "a freshly built index must match the live corpus signature, or it \
1260             would be treated as permanently stale and never served"
1261        );
1262        // Re-derivation is deterministic for an unchanged corpus.
1263        assert_eq!(sig, corpus_signature(&root, true, false).unwrap());
1264    }
1265
1266    #[test]
1267    fn corpus_signature_changes_on_edit_add_and_delete() {
1268        let dir = corpus();
1269        let root = dir.path().to_string_lossy().to_string();
1270        let base = corpus_signature(&root, true, false).unwrap();
1271
1272        std::fs::write(
1273            dir.path().join("a.rs"),
1274            "fn handler() {}\nlet x = 1;\nlet y = 2;\n",
1275        )
1276        .unwrap();
1277        let after_edit = corpus_signature(&root, true, false).unwrap();
1278        assert_ne!(
1279            base, after_edit,
1280            "an in-place edit must change the signature"
1281        );
1282
1283        std::fs::write(dir.path().join("d.rs"), "fn fresh() {}\n").unwrap();
1284        let after_add = corpus_signature(&root, true, false).unwrap();
1285        assert_ne!(
1286            after_edit, after_add,
1287            "adding a file must change the signature"
1288        );
1289
1290        std::fs::remove_file(dir.path().join("b.rs")).unwrap();
1291        let after_delete = corpus_signature(&root, true, false).unwrap();
1292        assert_ne!(
1293            after_add, after_delete,
1294            "deleting a file must change the signature"
1295        );
1296    }
1297
1298    #[test]
1299    fn get_fresh_serves_unchanged_then_refuses_after_edit() {
1300        let _env = crate::core::data_dir::test_env_lock();
1301        let data = tempfile::tempdir().unwrap();
1302        let _guard = DataDirGuard::set(data.path());
1303        crate::test_env::remove_var("LEAN_CTX_DISABLE_SEARCH_INDEX");
1304        crate::test_env::remove_var("LEAN_CTX_SEARCH_INDEX_COALESCE_MS");
1305
1306        let dir = corpus();
1307        let root = dir.path().to_string_lossy().to_string();
1308        assert!(warm_blocking(&root, true, false), "index warms");
1309
1310        // Unchanged corpus → the resident index is served.
1311        assert!(
1312            get_fresh(&root, true, false).is_some(),
1313            "an unchanged corpus must serve the resident index"
1314        );
1315
1316        // Native edit (size changes) → the now-stale index is refused so the
1317        // caller walks the live corpus instead of trusting outdated trigrams.
1318        std::fs::write(
1319            dir.path().join("a.rs"),
1320            "fn handler() {}\nlet x = 1;\nlet z = 9;\n",
1321        )
1322        .unwrap();
1323        assert!(
1324            get_fresh(&root, true, false).is_none(),
1325            "an edited corpus must refuse the stale resident index (#624)"
1326        );
1327    }
1328}