Skip to main content

lean_ctx/core/graph_index/
mod.rs

1// DEPRECATED: This module is being replaced by PropertyGraph (core/property_graph/).
2// New code should use GraphProvider (core/graph_provider.rs) instead of accessing
3// ProjectIndex directly. The dashboard now resolves graphs through
4// `graph_coordinator` (PropertyGraph-first); the remaining direct consumers are
5// the build pipeline (index_orchestrator) and the extractor itself.
6// See OPT-14/15 (#696) plan for the full migration path.
7
8use std::collections::HashMap;
9use std::path::Path;
10
11use rayon::prelude::*;
12use serde::{Deserialize, Serialize};
13
14use crate::core::import_resolver;
15use crate::core::signatures;
16mod edges;
17pub(crate) use edges::*;
18#[allow(unreachable_pub)]
19#[allow(dead_code)]
20pub(crate) mod file_id;
21#[cfg(test)]
22mod tests;
23
24const INDEX_VERSION: u32 = 6;
25
26// Path-key utilities moved to `core::index_paths` (#682); re-exported so existing
27// `graph_index::…` call sites keep compiling during the migration.
28use crate::core::index_paths::normalize_absolute_path;
29pub use crate::core::index_paths::{graph_match_key, graph_relative_key, normalize_project_root};
30
31pub fn is_safe_scan_root_public(path: &str) -> bool {
32    is_safe_scan_root(path)
33}
34
35fn is_filesystem_root(path: &str) -> bool {
36    let p = Path::new(path);
37    p.parent().is_none() || (cfg!(windows) && p.parent() == Some(Path::new("")))
38}
39
40/// Returns `true` if `dir` contains a known project marker.
41///
42/// Delegates to the single TCC-guarded probe in `pathutil` (#356) so a
43/// launchd-standalone process never stats marker files under ~/Documents, and
44/// the marker set stays defined in exactly one place (`pathutil::PROJECT_MARKERS`).
45fn dir_has_project_marker(dir: &Path) -> bool {
46    crate::core::pathutil::has_project_marker(dir)
47}
48
49/// True if `p` or any ancestor strictly *below* `stop` contains a project
50/// marker. Subdirectories of a real project (e.g. `repo/rust/src`) are
51/// legitimate scan roots even though the marker lives at the repo root —
52/// refusing them produced WARN noise on every grep/ls inside ~/Documents
53/// projects (GL#438). `stop` itself is never checked, so a marker-less
54/// `~/Documents` stays refused.
55fn has_marker_in_ancestry(p: &Path, stop: &Path) -> bool {
56    let mut cur = Some(p);
57    while let Some(dir) = cur {
58        if dir == stop {
59            return false;
60        }
61        if dir_has_project_marker(dir) {
62            return true;
63        }
64        cur = dir.parent();
65    }
66    false
67}
68
69fn is_safe_scan_root(path: &str) -> bool {
70    let normalized = normalize_project_root(path);
71    let p = Path::new(&normalized);
72
73    // macOS TCC (#356): a launchd-standalone process must never stat or
74    // enumerate under ~/Documents/Desktop/Downloads. Refuse such roots before
75    // any marker probe / read_dir runs. Editor- and CLI-attached processes
76    // inherit a TCC grant and keep indexing those projects normally.
77    if !crate::core::pathutil::may_probe_path(p) {
78        return false;
79    }
80
81    if normalized == "/" || normalized == "\\" || is_filesystem_root(&normalized) {
82        tracing::warn!("[graph_index: refusing to scan filesystem root]");
83        return false;
84    }
85
86    if normalized == "." || normalized.is_empty() {
87        tracing::warn!("[graph_index: refusing to scan relative/empty root]");
88        return false;
89    }
90
91    if let Some(home) = dirs::home_dir() {
92        let home_norm = normalize_project_root(&home.to_string_lossy());
93        if normalized == home_norm {
94            use std::sync::Once;
95            static HOME_WARN: Once = Once::new();
96            HOME_WARN.call_once(|| {
97                tracing::warn!(
98                    "[graph_index: skipping — cannot index home directory {normalized}.\n  \
99                     Run from inside a project, or set LEAN_CTX_PROJECT_ROOT=/path/to/project]"
100                );
101            });
102            return false;
103        }
104        // macOS TCC: Documents/Desktop/Downloads pop a privacy prompt the moment
105        // we stat or enumerate inside them (#356). They are never valid scan roots,
106        // so refuse here before any has_marker stat or read_dir runs.
107        if crate::core::pathutil::is_tcc_sensitive_home_dir(p) {
108            tracing::warn!(
109                "[graph_index: refusing to scan {normalized} — macOS TCC-protected home dir]"
110            );
111            return false;
112        }
113        // Block common broad home subdirectories that are never valid project roots
114        let home_path = Path::new(&home_norm);
115        const BLOCKED_HOME_SUBDIRS: &[&str] = &[
116            "Desktop",
117            "Documents",
118            "Downloads",
119            "Pictures",
120            "Music",
121            "Videos",
122            "Movies",
123            "Library",
124            ".local",
125            ".cache",
126            ".config",
127            "snap",
128            "Applications",
129            // Cloud-sync roots: scanning these forces on-demand providers to
130            // hydrate (download) every placeholder file/folder (#363). iCloud's
131            // backing dir (~/Library/Mobile Documents) is already covered by
132            // "Library" above.
133            "OneDrive",
134            "Dropbox",
135            "Google Drive",
136        ];
137        for blocked in BLOCKED_HOME_SUBDIRS {
138            let blocked_path = home_path.join(blocked);
139            let is_inside_blocked = p == blocked_path || p.starts_with(&blocked_path);
140            // Markers may live in an *ancestor*: `repo/rust/src` is a legitimate
141            // scan root of the project rooted at `repo` (GL#438). Walk up to (but
142            // not past) the blocked dir itself, so `~/Documents` without any
143            // project stays refused.
144            let has_marker = has_marker_in_ancestry(p, &blocked_path);
145            if is_inside_blocked
146                && !has_marker
147                && !crate::core::pathutil::has_multi_repo_children(p)
148            {
149                tracing::warn!(
150                    "[graph_index: refusing to scan {normalized} — \
151                     inside home/{blocked} without project markers]"
152                );
153                return false;
154            }
155        }
156
157        // Block directories that are direct children of home without project markers
158        // (but allow multi-repo workspace parents like ~/code/)
159        if p.parent() == Some(home_path)
160            && !dir_has_project_marker(p)
161            && !crate::core::pathutil::has_multi_repo_children(p)
162        {
163            tracing::warn!(
164                "[graph_index: refusing to scan {normalized} — \
165                 direct child of home without project markers]"
166            );
167            return false;
168        }
169    }
170
171    let breadth_markers = [
172        ".git",
173        "Cargo.toml",
174        "package.json",
175        "go.mod",
176        "pyproject.toml",
177        "setup.py",
178        "Makefile",
179        "CMakeLists.txt",
180        "pnpm-workspace.yaml",
181        ".projectile",
182        "BUILD.bazel",
183        "go.work",
184    ];
185
186    if !breadth_markers.iter().any(|m| p.join(m).exists()) && !dir_has_dotnet_project(p) {
187        // Multi-repo workspace parent: >=2 children with project markers is always safe
188        if crate::core::pathutil::has_multi_repo_children(p) {
189            return true;
190        }
191
192        let child_count = std::fs::read_dir(p).map_or(0, |rd| {
193            rd.filter_map(Result::ok)
194                .filter(|e| e.path().is_dir())
195                .count()
196        });
197        if child_count > 50 {
198            tracing::warn!(
199                "[graph_index: {normalized} has no project markers and {child_count} subdirectories — \
200                 skipping scan to avoid indexing broad directories]"
201            );
202            return false;
203        }
204    }
205
206    true
207}
208
209/// True if the directory contains a .NET project/solution file (`*.csproj`,
210/// `*.sln`, `*.fsproj`, `*.vbproj`). Filenames vary, so we match by extension —
211/// these are strong project-root markers even when there is no `.git`.
212fn dir_has_dotnet_project(dir: &Path) -> bool {
213    std::fs::read_dir(dir).is_ok_and(|rd| {
214        rd.filter_map(Result::ok).any(|e| {
215            e.path()
216                .extension()
217                .and_then(|x| x.to_str())
218                .is_some_and(|x| {
219                    matches!(
220                        x.to_ascii_lowercase().as_str(),
221                        "csproj" | "sln" | "fsproj" | "vbproj"
222                    )
223                })
224        })
225    })
226}
227
228#[derive(Debug, Clone, Serialize, Deserialize)]
229pub struct ProjectIndex {
230    pub version: u32,
231    pub project_root: String,
232    pub last_scan: String,
233    pub files: HashMap<String, FileEntry>,
234    pub edges: Vec<IndexEdge>,
235    pub symbols: HashMap<String, SymbolEntry>,
236    /// Graph-local path interner: one owned allocation per distinct file path.
237    /// Populated during scan; used for O(1) path→id lookups in queries.
238    /// Skipped during serde — rebuilt from `files` keys on deserialization.
239    #[serde(skip)]
240    pub(crate) interner: file_id::PathInterner,
241}
242
243#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
244pub struct FileEntry {
245    pub path: String,
246    pub hash: String,
247    pub language: String,
248    pub line_count: usize,
249    pub token_count: usize,
250    pub exports: Vec<String>,
251    pub summary: String,
252}
253
254#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
255pub struct SymbolEntry {
256    pub file: String,
257    pub name: String,
258    pub kind: String,
259    pub start_line: usize,
260    pub end_line: usize,
261    pub is_exported: bool,
262}
263
264#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
265pub struct IndexEdge {
266    pub from: String,
267    pub to: String,
268    pub kind: String,
269    #[serde(default = "default_edge_weight")]
270    pub weight: f32,
271}
272
273fn default_edge_weight() -> f32 {
274    1.0
275}
276
277impl ProjectIndex {
278    pub fn new(project_root: &str) -> Self {
279        Self {
280            version: INDEX_VERSION,
281            project_root: normalize_project_root(project_root),
282            last_scan: chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(),
283            files: HashMap::new(),
284            edges: Vec::new(),
285            symbols: HashMap::new(),
286            interner: file_id::PathInterner::new(),
287        }
288    }
289
290    /// Rebuild the interner from the current `files` keys. Called after
291    /// deserialization or materialization from the property graph, where
292    /// `interner` is skipped/absent.
293    pub(crate) fn rebuild_interner(&mut self) {
294        let mut interner = file_id::PathInterner::with_capacity(self.files.len());
295        for key in self.files.keys() {
296            interner.intern(key);
297        }
298        self.interner = interner;
299    }
300
301    pub fn index_dir(project_root: &str) -> Option<std::path::PathBuf> {
302        let normalized = normalize_project_root(project_root);
303        let hash = crate::core::project_hash::hash_project_root(&normalized);
304        crate::core::data_dir::lean_ctx_data_dir()
305            .ok()
306            .map(|d| d.join("graphs").join(hash))
307    }
308
309    /// Reconstruct the index from the property graph — the sole persistence
310    /// store since #696 C4. The PG is a parity-proven lossless superset of the
311    /// former JSON index, so this is a faithful round-trip (see
312    /// `graph_provider::materialize_project_index` + its round-trip test).
313    /// `None` when the graph has not been built yet (empty file catalog).
314    pub fn load(project_root: &str) -> Option<Self> {
315        let graph = crate::core::property_graph::CodeGraph::open(project_root).ok()?;
316        if graph.file_catalog_count().unwrap_or(0) == 0 {
317            return None;
318        }
319        let provider = crate::core::graph_provider::GraphProvider::PropertyGraph(graph);
320        let mut index = provider.materialize_project_index(project_root);
321        index.rebuild_interner();
322        Some(index)
323    }
324
325    /// Persist the index by mirroring it into the property graph (the sole store
326    /// since #696 C4). Replaces the former `index.json.zst` write; the mirror
327    /// also stamps `graph.meta.json`, which the resident graph cache fingerprints
328    /// for invalidation.
329    pub fn save(&self) -> Result<(), String> {
330        crate::core::property_graph::mirror_index(&self.project_root, self)
331            .map_err(|e| e.to_string())
332    }
333
334    /// Remove all cached graph indices that are older than max_age_hours.
335    /// Called on startup/update to prevent stale data from persisting.
336    pub fn purge_stale_indices() {
337        let Ok(data_dir) = crate::core::data_dir::lean_ctx_data_dir() else {
338            return;
339        };
340        let graphs_dir = data_dir.join("graphs");
341        let Ok(entries) = std::fs::read_dir(&graphs_dir) else {
342            return;
343        };
344        let cfg = crate::core::config::Config::load();
345        let max_age_secs = cfg.archive_max_age_hours_effective() * 3600;
346
347        for entry in entries.filter_map(Result::ok) {
348            let path = entry.path();
349            if !path.is_dir() {
350                continue;
351            }
352            // #696 C4: the property graph (graph.db, stamped by graph.meta.json)
353            // is the sole store; age the directory by its last build instead of
354            // the retired JSON index.
355            let meta = path.join("graph.meta.json");
356            let db = path.join("graph.db");
357            let index_file = if meta.exists() {
358                &meta
359            } else if db.exists() {
360                &db
361            } else {
362                continue;
363            };
364
365            let is_old = index_file
366                .metadata()
367                .and_then(|m| m.modified())
368                .is_ok_and(|mtime| {
369                    mtime
370                        .elapsed()
371                        .is_ok_and(|age| age.as_secs() > max_age_secs)
372                });
373
374            if is_old {
375                tracing::info!("[graph_index: purging stale index at {}]", path.display());
376                let _ = std::fs::remove_dir_all(&path);
377            }
378        }
379    }
380
381    pub fn file_count(&self) -> usize {
382        self.files.len()
383    }
384
385    pub fn symbol_count(&self) -> usize {
386        self.symbols.len()
387    }
388
389    pub fn edge_count(&self) -> usize {
390        self.edges.len()
391    }
392
393    pub fn get_symbol(&self, key: &str) -> Option<&SymbolEntry> {
394        self.symbols.get(key)
395    }
396
397    pub fn get_reverse_deps(&self, path: &str, depth: usize) -> Vec<String> {
398        let mut result = Vec::new();
399        let mut visited = std::collections::HashSet::new();
400        let mut queue: Vec<(String, usize)> = vec![(path.to_string(), 0)];
401
402        while let Some((current, d)) = queue.pop() {
403            if d > depth || visited.contains(&current) {
404                continue;
405            }
406            visited.insert(current.clone());
407            if current != path {
408                result.push(current.clone());
409            }
410
411            for edge in &self.edges {
412                if edge.to == current && edge.kind == "import" && !visited.contains(&edge.from) {
413                    queue.push((edge.from.clone(), d + 1));
414                }
415            }
416        }
417        result
418    }
419
420    /// Forward import dependencies: files that `path` (transitively) imports.
421    /// Mirror of `get_reverse_deps` with the edge direction flipped.
422    pub fn get_forward_deps(&self, path: &str, depth: usize) -> Vec<String> {
423        let mut result = Vec::new();
424        let mut visited = std::collections::HashSet::new();
425        let mut queue: Vec<(String, usize)> = vec![(path.to_string(), 0)];
426
427        while let Some((current, d)) = queue.pop() {
428            if d > depth || visited.contains(&current) {
429                continue;
430            }
431            visited.insert(current.clone());
432            if current != path {
433                result.push(current.clone());
434            }
435
436            for edge in &self.edges {
437                if edge.from == current && edge.kind == "import" && !visited.contains(&edge.to) {
438                    queue.push((edge.to.clone(), d + 1));
439                }
440            }
441        }
442        result
443    }
444
445    pub fn get_related(&self, path: &str, depth: usize) -> Vec<String> {
446        let mut result = Vec::new();
447        let mut visited = std::collections::HashSet::new();
448        let mut queue: Vec<(String, usize)> = vec![(path.to_string(), 0)];
449
450        while let Some((current, d)) = queue.pop() {
451            if d > depth || visited.contains(&current) {
452                continue;
453            }
454            visited.insert(current.clone());
455            if current != path {
456                result.push(current.clone());
457            }
458
459            for edge in &self.edges {
460                if edge.from == current && !visited.contains(&edge.to) {
461                    queue.push((edge.to.clone(), d + 1));
462                }
463                if edge.to == current && !visited.contains(&edge.from) {
464                    queue.push((edge.from.clone(), d + 1));
465                }
466            }
467        }
468        result
469    }
470}
471
472/// Load the best available graph index, trying multiple root path variants.
473/// If no valid index exists, automatically scans the project to build one.
474/// This is the primary entry point — ensures zero-config usage.
475pub fn load_or_build(project_root: &str) -> ProjectIndex {
476    if std::env::var("LEAN_CTX_NO_INDEX").is_ok() {
477        return ProjectIndex::load(project_root).unwrap_or_else(|| ProjectIndex::new(project_root));
478    }
479
480    // Prefer stable absolute roots. Using "." as a cache key is fragile because
481    // it depends on the process cwd and can accidentally load the wrong project.
482    let root_abs = if project_root.trim().is_empty() || project_root == "." {
483        std::env::current_dir().ok().map_or_else(
484            || ".".to_string(),
485            |p| normalize_project_root(&p.to_string_lossy()),
486        )
487    } else {
488        normalize_project_root(project_root)
489    };
490
491    if !is_safe_scan_root(&root_abs) {
492        return ProjectIndex::new(&root_abs);
493    }
494
495    // Try the absolute/root-normalized path first.
496    if let Some(idx) = ProjectIndex::load(&root_abs)
497        && !idx.files.is_empty()
498    {
499        if index_looks_stale(&idx, &root_abs) {
500            tracing::warn!("[graph_index: stale index detected for {root_abs}; rebuilding]");
501            return scan(&root_abs);
502        }
503        return idx;
504    }
505
506    // CWD fallback: only use if CWD is a subdirectory of root_abs (same project)
507    if let Ok(cwd) = std::env::current_dir() {
508        let cwd_str = normalize_project_root(&cwd.to_string_lossy());
509        if cwd_str != root_abs
510            && cwd_str.starts_with(&root_abs)
511            && let Some(idx) = ProjectIndex::load(&cwd_str)
512            && !idx.files.is_empty()
513        {
514            if index_looks_stale(&idx, &cwd_str) {
515                return scan(&cwd_str);
516            }
517            return idx;
518        }
519    }
520
521    scan(&root_abs)
522}
523
524fn index_looks_stale(index: &ProjectIndex, root_abs: &str) -> bool {
525    if index.files.is_empty() {
526        return true;
527    }
528
529    // TTL check: rebuild if index is older than configured max_age_hours
530    if let Ok(scan_time) =
531        chrono::NaiveDateTime::parse_from_str(&index.last_scan, "%Y-%m-%d %H:%M:%S")
532    {
533        let cfg = crate::core::config::Config::load();
534        let effective_hours = cfg.archive_max_age_hours_effective();
535        let max_age = chrono::Duration::hours(effective_hours as i64);
536        let now = chrono::Local::now().naive_local();
537        if now.signed_duration_since(scan_time) > max_age {
538            tracing::info!(
539                "[graph_index: index is older than {}h — marking stale]",
540                effective_hours
541            );
542            return true;
543        }
544    }
545
546    // Contamination check: if index contains paths from common user directories,
547    // it was built from a too-broad root and must be rebuilt
548    const CONTAMINATION_MARKERS: &[&str] = &[
549        "Desktop/",
550        "Documents/",
551        "Downloads/",
552        "Pictures/",
553        "Music/",
554        "Videos/",
555        "Movies/",
556        "Library/",
557        ".cache/",
558        "snap/",
559    ];
560    let contaminated = index.files.keys().take(200).any(|rel| {
561        CONTAMINATION_MARKERS
562            .iter()
563            .any(|m| rel.starts_with(m) || rel.contains(&format!("/{m}")))
564    });
565    if contaminated {
566        tracing::warn!(
567            "[graph_index: index contains files from user directories (Desktop/Documents/...) — \
568             marking stale to force clean rebuild]"
569        );
570        return true;
571    }
572
573    let root_path = Path::new(root_abs);
574    // Sample up to 20 files for existence check (avoid scanning all files in large indices)
575    let sample_size = index.files.len().min(20);
576    for rel in index.files.keys().take(sample_size) {
577        let rel = rel.trim_start_matches(['/', '\\']);
578        if rel.is_empty() {
579            continue;
580        }
581        let abs = root_path.join(rel);
582        if !abs.exists() {
583            return true;
584        }
585    }
586
587    // Content-aware staleness: rescan only when source *content* actually
588    // changed. mtime is a cheap prefilter; the change is then confirmed against
589    // the stored content hash so a `touch`/checkout/format that leaves bytes
590    // unchanged never forces a needless rescan (covers edits and new files).
591    if source_content_changed_since_index(index, root_abs) {
592        tracing::info!("[graph_index: source content changed since last scan — marking stale]");
593        return true;
594    }
595
596    false
597}
598
599/// Modified time of the persisted index artifact, if one exists.
600///
601/// Since #696 C4 the property graph is the sole store, so staleness is measured
602/// against `graph.meta.json` (rewritten on every mirror) with the `graph.db`
603/// file as a fallback.
604fn index_file_mtime(root_abs: &str) -> Option<std::time::SystemTime> {
605    let dir = ProjectIndex::index_dir(root_abs)?;
606    for name in ["graph.meta.json", "graph.db"] {
607        if let Ok(meta) = std::fs::metadata(dir.join(name))
608            && let Ok(modified) = meta.modified()
609        {
610            return Some(modified);
611        }
612    }
613    None
614}
615
616/// Bounded staleness check that confirms *content* changes, not just mtimes.
617///
618/// An mtime newer than the persisted index only flags a *candidate*; the change
619/// is then confirmed by comparing the file's content hash against the stored
620/// `FileEntry.hash` (same `compute_hash` + `read_to_string` the scan uses, so
621/// the comparison is exact). This means a `touch`, `git checkout`, or formatter
622/// rewrite that leaves bytes unchanged no longer forces a needless rescan, while
623/// genuine edits and newly added files still mark the index stale.
624///
625/// Both the traversal and the number of confirming reads are capped: exceeding
626/// the read cap returns `true` (conservatively stale) instead of reading an
627/// unbounded amount. Removed files are handled by the earlier existence check.
628fn source_content_changed_since_index(index: &ProjectIndex, root_abs: &str) -> bool {
629    let Some(index_mtime) = index_file_mtime(root_abs) else {
630        // No persisted index yet — the existence/TTL checks above already decided.
631        return false;
632    };
633    // #735: excluded files must not flag the index stale (a change to CSV seed
634    // data would otherwise force needless graph rescans forever).
635    let index_filter = crate::core::index_filter::IndexFileFilter::effective();
636    let walker = ignore::WalkBuilder::new(root_abs)
637        // #1792: corpus membership follows git, not a leading dot.
638        .hidden(crate::core::walk_filter::SKIP_HIDDEN_IN_CONTENT_WALK)
639        .git_ignore(index_filter.respect_gitignore)
640        .git_global(index_filter.respect_gitignore)
641        .git_exclude(index_filter.respect_gitignore)
642        .require_git(false)
643        .max_depth(Some(20))
644        .filter_entry(crate::core::walk_filter::keep_entry)
645        .build();
646    const MAX_VISIT: usize = 50_000;
647    const MAX_CONFIRM_READS: usize = 4_000;
648    let mut visited = 0usize;
649    let mut confirm_reads = 0usize;
650    for entry in walker.filter_map(std::result::Result::ok) {
651        visited += 1;
652        if visited > MAX_VISIT {
653            break;
654        }
655        if !entry.file_type().is_some_and(|ft| ft.is_file()) {
656            continue;
657        }
658        let path = entry.path();
659        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
660        if !is_indexable_ext(ext) {
661            continue;
662        }
663        // mtime prefilter: only files touched after the index are candidates.
664        let Ok(meta) = entry.metadata() else { continue };
665        let Ok(modified) = meta.modified() else {
666            continue;
667        };
668        if modified <= index_mtime {
669            continue;
670        }
671        // Candidate: confirm against the stored content hash.
672        let rel = make_relative(&path.to_string_lossy(), root_abs);
673        if index_filter.is_excluded(&rel.replace('\\', "/")) {
674            continue;
675        }
676        let Some(file_entry) = index.files.get(&rel) else {
677            // A newly added indexable file is genuinely new content.
678            return true;
679        };
680        confirm_reads += 1;
681        if confirm_reads > MAX_CONFIRM_READS {
682            // Too many candidates to verify cheaply — assume stale.
683            return true;
684        }
685        match std::fs::read_to_string(path) {
686            // Bytes unchanged despite a newer mtime → not a real change.
687            Ok(content) if compute_hash(&content) == file_entry.hash => {}
688            // Edited content, or no longer readable as it was at scan time.
689            _ => return true,
690        }
691    }
692    false
693}
694
695/// Delete the persisted graph-index artifacts for a project so the next scan
696/// rebuilds from scratch. Backs `graph build --force`.
697///
698/// Since #696 C4 the property graph (`graph.db` + `graph.meta.json`) is the sole
699/// store; the legacy JSON names are still removed so upgrades clear stale files.
700pub fn purge_index(project_root: &str) {
701    if let Some(dir) = ProjectIndex::index_dir(project_root) {
702        for name in [
703            "graph.db",
704            "graph.db-wal",
705            "graph.db-shm",
706            "graph.meta.json",
707            "index.json.zst",
708            "index.json",
709            "call_graph.json.zst",
710        ] {
711            let _ = std::fs::remove_file(dir.join(name));
712        }
713    }
714}
715
716pub fn scan(project_root: &str) -> ProjectIndex {
717    scan_inner(project_root).0
718}
719
720pub fn scan_with_content_cache(project_root: &str) -> (ProjectIndex, HashMap<String, String>) {
721    scan_inner(project_root)
722}
723
724fn scan_inner(project_root: &str) -> (ProjectIndex, HashMap<String, String>) {
725    if std::env::var("LEAN_CTX_NO_INDEX").is_ok() {
726        tracing::info!("[graph_index: LEAN_CTX_NO_INDEX set — skipping scan]");
727        return (ProjectIndex::new(project_root), HashMap::new());
728    }
729
730    let project_root = normalize_project_root(project_root);
731
732    if !is_safe_scan_root(&project_root) {
733        tracing::debug!("[graph_index: scan aborted for unsafe root {project_root}]");
734        return (ProjectIndex::new(&project_root), HashMap::new());
735    }
736
737    let lock_name = format!(
738        "graph-idx-{}",
739        &crate::core::index_namespace::namespace_hash(Path::new(&project_root))[..8]
740    );
741    let _lock = crate::core::startup_guard::try_acquire_lock(
742        &lock_name,
743        std::time::Duration::from_millis(800),
744        std::time::Duration::from_mins(3),
745    );
746    if _lock.is_none() {
747        tracing::info!(
748            "[graph_index: another process is scanning {project_root} — returning cached or empty]"
749        );
750        return (
751            ProjectIndex::load(&project_root).unwrap_or_else(|| ProjectIndex::new(&project_root)),
752            HashMap::new(),
753        );
754    }
755
756    let existing = ProjectIndex::load(&project_root);
757    let mut index = ProjectIndex::new(&project_root);
758
759    // Borrow prior symbols instead of cloning every hash, key, and SymbolEntry up
760    // front. Reused symbols are cloned only for the current scan batch as they
761    // move into the replacement index, avoiding a third full-index copy.
762    let previous_symbols = existing
763        .as_ref()
764        .map(previous_symbols_by_file)
765        .unwrap_or_default();
766
767    let cfg = crate::core::config::Config::load();
768    // #735: shared corpus filter — same membership decision as the BM25 walk.
769    let index_filter = crate::core::index_filter::IndexFileFilter::resolve(&cfg);
770
771    let walker = ignore::WalkBuilder::new(&project_root)
772        // #1792: kept identical to the BM25 walk, as the comment above promises.
773        .hidden(crate::core::walk_filter::SKIP_HIDDEN_IN_CONTENT_WALK)
774        .git_ignore(index_filter.respect_gitignore)
775        .git_global(index_filter.respect_gitignore)
776        .git_exclude(index_filter.respect_gitignore)
777        .require_git(false)
778        .max_depth(Some(20))
779        .filter_entry(crate::core::walk_filter::keep_entry)
780        .build();
781
782    let extra_ignores: Vec<glob::Pattern> = cfg
783        .extra_ignore_patterns
784        .iter()
785        .filter_map(|p| glob::Pattern::new(p).ok())
786        .collect();
787
788    let mut scanned = 0usize;
789    let mut reused = 0usize;
790    let mut entries_visited = 0usize;
791    let mut content_cache: HashMap<String, String> = HashMap::new();
792    let mut content_cache_bytes: usize = 0;
793    const CONTENT_CACHE_MAX_BYTES: usize = 256 * 1024 * 1024; // #790: cap at 256MB
794    let max_files = if cfg.graph_index_max_files == 0 {
795        usize::MAX // unlimited
796    } else {
797        cfg.graph_index_max_files as usize
798    };
799    const MAX_ENTRIES_VISITED: usize = 500_000;
800    const MAX_FILE_SIZE_BYTES: u64 = 2 * 1024 * 1024; // 2 MB per file
801    /// Maximum phase-2 fan-out. Actual batches shrink with guardian headroom.
802    const SCAN_BATCH_FILES: usize = 500;
803    const SCAN_MIN_BATCH_FILES: usize = 1;
804    // Per-file scan: content string (~20 KB avg) + SHA-256 state +
805    // signature extraction + line/token counts. Lighter than BM25
806    // because no chunk splitting or lowered-token vectors are built.
807    const SCAN_EST_TRANSIENT_PER_FILE: u64 = 192 * 1024;
808    let scan_deadline = std::time::Instant::now() + std::time::Duration::from_mins(5);
809
810    // #934: two-phase scan. Phase 1 walks the tree sequentially (cheap; it
811    // carries the traversal caps / timeout / memory early-breaks) and collects
812    // the files to index. Phase 2 fans the expensive per-file signature
813    // extraction across a rayon pool — pure and thread-safe (the tree-sitter
814    // parser is `thread_local!`; `previous_symbols`/`existing` are read-only). Phase 3
815    // merges sequentially: `files`/`symbols` are keyed per file, so the result is
816    // identical to a sequential scan (edges are built and sorted afterwards).
817    let mut targets: Vec<(String, String, String)> = Vec::new();
818    for entry in walker.filter_map(std::result::Result::ok) {
819        entries_visited += 1;
820        if entries_visited > MAX_ENTRIES_VISITED {
821            tracing::warn!(
822                "[graph_index: walked {entries_visited} entries — aborting scan to prevent \
823                 runaway traversal. Indexed {} files so far.]",
824                targets.len()
825            );
826            break;
827        }
828        if entries_visited.is_multiple_of(5000) {
829            if std::time::Instant::now() > scan_deadline {
830                tracing::warn!(
831                    "[graph_index: scan timeout (120s) after {entries_visited} entries — \
832                     saving partial index with {} files]",
833                    targets.len()
834                );
835                break;
836            }
837            if crate::core::memory_guard::abort_requested() {
838                tracing::warn!(
839                    "[graph_index: memory pressure abort after {entries_visited} entries — \
840                     saving partial index with {} files]",
841                    targets.len()
842                );
843                break;
844            }
845            if crate::core::memory_guard::is_under_pressure() {
846                tracing::warn!(
847                    "[graph_index: memory pressure detected at {entries_visited} entries — \
848                     stopping scan with {} files]",
849                    targets.len()
850                );
851                break;
852            }
853            if let Some(ref g) = _lock {
854                g.touch();
855            }
856        }
857
858        if !entry.file_type().is_some_and(|ft| ft.is_file()) {
859            continue;
860        }
861
862        if entry.path_is_symlink() {
863            continue;
864        }
865        let file_path = normalize_absolute_path(&entry.path().to_string_lossy());
866
867        if !std::path::Path::new(&file_path).starts_with(std::path::Path::new(&project_root)) {
868            continue;
869        }
870
871        if let Ok(meta) = std::fs::symlink_metadata(&file_path) {
872            if meta.file_type().is_symlink() || !meta.is_file() {
873                continue;
874            }
875            if meta.len() > MAX_FILE_SIZE_BYTES {
876                tracing::debug!(
877                    "[graph_index: skipping {file_path} — {:.1}MB exceeds {}MB limit]",
878                    meta.len() as f64 / 1_048_576.0,
879                    MAX_FILE_SIZE_BYTES / (1024 * 1024),
880                );
881                continue;
882            }
883        }
884
885        let ext = Path::new(&file_path)
886            .extension()
887            .and_then(|e| e.to_str())
888            .unwrap_or("");
889
890        if !is_indexable_ext(ext) {
891            continue;
892        }
893
894        let rel = make_relative(&file_path, &project_root);
895        if extra_ignores.iter().any(|p| p.matches(&rel)) {
896            continue;
897        }
898        if index_filter.is_excluded(&rel.replace('\\', "/")) {
899            continue;
900        }
901
902        if max_files != usize::MAX && targets.len() >= max_files {
903            tracing::info!(
904                "[graph_index: reached configured limit of {} files. Set graph_index_max_files = 0 for unlimited.]",
905                max_files
906            );
907            break;
908        }
909
910        // Own `ext` before moving `file_path` (it borrows from it).
911        let ext = ext.to_string();
912        targets.push((file_path, rel, ext));
913    }
914
915    // #790: admission control — check if parallel fan-out fits memory headroom.
916    // Degrades to sequential (with per-file pressure breaks) on huge corpora,
917    // mirroring BM25's admission gate.
918    let target_rels: Vec<String> = targets.iter().map(|(_, r, _)| r.clone()).collect();
919    let admission = crate::core::index_admission::admit_files(
920        crate::core::index_admission::BuildKind::GraphScan,
921        std::path::Path::new(&project_root),
922        &target_rels,
923    );
924    let parallel = admission.parallel_ok && !crate::core::memory_guard::is_under_pressure();
925    let mut files_done = 0;
926    while files_done < targets.len() {
927        if crate::core::memory_guard::abort_requested() {
928            tracing::warn!(
929                "[graph_index: aborting scan after {files_done} files due to critical memory pressure]"
930            );
931            break;
932        }
933        if crate::core::memory_guard::is_under_pressure() {
934            tracing::warn!(
935                "[graph_index: stopping scan after {files_done} files due to memory pressure]"
936            );
937            break;
938        }
939        let batch_size = crate::core::memory_guard::adaptive_batch_size(
940            SCAN_MIN_BATCH_FILES,
941            SCAN_BATCH_FILES,
942            SCAN_EST_TRANSIENT_PER_FILE,
943        );
944        let batch_end = (files_done + batch_size).min(targets.len());
945        let results = process_scan_targets(
946            &targets[files_done..batch_end],
947            &previous_symbols,
948            existing.as_ref(),
949            parallel,
950        );
951        for r in results {
952            if r.reused {
953                reused += 1;
954            } else {
955                scanned += 1;
956            }
957            index.files.insert(r.rel.clone(), r.file_entry);
958            for (key, sym) in r.symbols {
959                index.symbols.insert(key, sym);
960            }
961            if content_cache_bytes < CONTENT_CACHE_MAX_BYTES {
962                content_cache_bytes += r.content.len();
963                content_cache.insert(r.rel, r.content);
964            }
965        }
966        files_done = batch_end;
967        crate::core::memory_guard::jemalloc_purge();
968    }
969
970    index.rebuild_interner();
971    // #1494: the scan_deadline covers the entire indexing pipeline, including
972    // edge-building, so a ~7k-file tree cannot spin unbounded on CPU after IO.
973    build_edges_cached(&mut index, &content_cache, scan_deadline);
974
975    if let Err(e) = index.save() {
976        tracing::warn!("could not save graph index: {e}");
977    }
978
979    tracing::debug!(
980        "[graph_index: {} files ({} scanned, {} reused), {} symbols, {} edges]",
981        index.file_count(),
982        scanned,
983        reused,
984        index.symbol_count(),
985        index.edge_count()
986    );
987
988    (index, content_cache)
989}
990
991/// Borrowed previous-scan symbols grouped by file. This index stores only
992/// references, so incremental scans do not clone the complete prior index before
993/// processing starts.
994type PreviousSymbols<'a> = HashMap<&'a str, Vec<(&'a str, &'a SymbolEntry)>>;
995
996fn previous_symbols_by_file(index: &ProjectIndex) -> PreviousSymbols<'_> {
997    let mut by_file: PreviousSymbols<'_> = HashMap::new();
998    for (key, symbol) in &index.symbols {
999        by_file
1000            .entry(symbol.file.as_str())
1001            .or_default()
1002            .push((key.as_str(), symbol));
1003    }
1004    by_file
1005}
1006
1007/// One file's contribution to the scan, produced off-thread by
1008/// [`process_scan_file`] and merged sequentially by [`scan_inner`].
1009#[derive(Debug, PartialEq)]
1010struct ScanFileResult {
1011    rel: String,
1012    file_entry: FileEntry,
1013    /// `(key, symbol)` pairs in signature order; `key` is `"<rel>::<name>"`.
1014    symbols: Vec<(String, SymbolEntry)>,
1015    content: String,
1016    reused: bool,
1017}
1018
1019/// Pure, thread-safe per-file scan work: read, hash, reuse-check against the
1020/// previous index, then extract signatures + file metadata. Returns `None` when
1021/// the file cannot be read (the sequential scan would `continue` past it).
1022fn process_scan_file(
1023    file_path: &str,
1024    rel: &str,
1025    ext: &str,
1026    previous_symbols: &PreviousSymbols<'_>,
1027    existing: Option<&ProjectIndex>,
1028) -> Option<ScanFileResult> {
1029    if crate::core::memory_guard::abort_requested() {
1030        return None;
1031    }
1032    let content = std::fs::read_to_string(file_path).ok()?;
1033    let hash = compute_hash(&content);
1034
1035    // Unchanged file with a prior entry: reuse verbatim (no re-parse). Clone
1036    // only this file's symbols into the replacement index; the complete prior
1037    // symbol table remains borrowed throughout the scan.
1038    if let Some(old_entry) = existing.and_then(|p| p.files.get(rel))
1039        && old_entry.hash == hash
1040    {
1041        let symbols = previous_symbols
1042            .get(rel)
1043            .into_iter()
1044            .flatten()
1045            .map(|(key, symbol)| ((*key).to_string(), (*symbol).clone()))
1046            .collect();
1047        return Some(ScanFileResult {
1048            rel: rel.to_string(),
1049            file_entry: old_entry.clone(),
1050            symbols,
1051            content,
1052            reused: true,
1053        });
1054    }
1055
1056    if crate::core::memory_guard::abort_requested() {
1057        return None;
1058    }
1059
1060    let sigs = signatures::extract_signatures(&content, ext);
1061    let line_count = content.lines().count();
1062    let token_count = crate::core::tokens::count_tokens(&content);
1063    let summary = extract_summary(&content);
1064
1065    let exports: Vec<String> = sigs
1066        .iter()
1067        .filter(|s| s.is_exported)
1068        .map(|s| s.name.clone())
1069        .collect();
1070
1071    let file_entry = FileEntry {
1072        path: rel.to_string(),
1073        hash,
1074        language: ext.to_string(),
1075        line_count,
1076        token_count,
1077        exports,
1078        summary,
1079    };
1080
1081    let symbols: Vec<(String, SymbolEntry)> = sigs
1082        .iter()
1083        .map(|sig| {
1084            let (start, end) = sig
1085                .start_line
1086                .zip(sig.end_line)
1087                .unwrap_or_else(|| find_symbol_range(&content, sig));
1088            let key = format!("{rel}::{}", sig.name);
1089            (
1090                key,
1091                SymbolEntry {
1092                    file: rel.to_string(),
1093                    name: sig.name.clone(),
1094                    kind: sig.kind.to_string(),
1095                    start_line: start,
1096                    end_line: end,
1097                    is_exported: sig.is_exported,
1098                },
1099            )
1100        })
1101        .collect();
1102
1103    Some(ScanFileResult {
1104        rel: rel.to_string(),
1105        file_entry,
1106        symbols,
1107        content,
1108        reused: false,
1109    })
1110}
1111
1112/// Run [`process_scan_file`] over every target. `par_iter().collect()` preserves
1113/// input order, so the [`scan_inner`] merge is order-stable whether or not the
1114/// pool is used; the sequential branch is the memory-pressure fallback and an
1115/// equivalence anchor for the determinism tests.
1116fn process_scan_targets(
1117    targets: &[(String, String, String)],
1118    previous_symbols: &PreviousSymbols<'_>,
1119    existing: Option<&ProjectIndex>,
1120    parallel: bool,
1121) -> Vec<ScanFileResult> {
1122    if parallel {
1123        targets
1124            .par_iter()
1125            .filter_map(|(file_path, rel, ext)| {
1126                process_scan_file(file_path, rel, ext, previous_symbols, existing)
1127            })
1128            .collect()
1129    } else {
1130        targets
1131            .iter()
1132            .filter_map(|(file_path, rel, ext)| {
1133                process_scan_file(file_path, rel, ext, previous_symbols, existing)
1134            })
1135            .collect()
1136    }
1137}
1138
1139fn find_symbol_range(content: &str, sig: &signatures::Signature) -> (usize, usize) {
1140    let lines: Vec<&str> = content.lines().collect();
1141    let mut start = 0;
1142
1143    for (i, line) in lines.iter().enumerate() {
1144        if line.contains(&sig.name) {
1145            let trimmed = line.trim();
1146            let is_def = trimmed.starts_with("fn ")
1147                || trimmed.starts_with("pub fn ")
1148                || trimmed.starts_with("pub(crate) fn ")
1149                || trimmed.starts_with("async fn ")
1150                || trimmed.starts_with("pub async fn ")
1151                || trimmed.starts_with("struct ")
1152                || trimmed.starts_with("pub struct ")
1153                || trimmed.starts_with("enum ")
1154                || trimmed.starts_with("pub enum ")
1155                || trimmed.starts_with("trait ")
1156                || trimmed.starts_with("pub trait ")
1157                || trimmed.starts_with("impl ")
1158                || trimmed.starts_with("class ")
1159                || trimmed.starts_with("export class ")
1160                || trimmed.starts_with("export function ")
1161                || trimmed.starts_with("export async function ")
1162                || trimmed.starts_with("function ")
1163                || trimmed.starts_with("async function ")
1164                || trimmed.starts_with("def ")
1165                || trimmed.starts_with("async def ")
1166                || trimmed.starts_with("func ")
1167                || trimmed.starts_with("interface ")
1168                || trimmed.starts_with("export interface ")
1169                || trimmed.starts_with("type ")
1170                || trimmed.starts_with("export type ")
1171                || trimmed.starts_with("const ")
1172                || trimmed.starts_with("export const ")
1173                || trimmed.starts_with("fun ")
1174                || trimmed.starts_with("private fun ")
1175                || trimmed.starts_with("public fun ")
1176                || trimmed.starts_with("internal fun ")
1177                || trimmed.starts_with("class ")
1178                || trimmed.starts_with("data class ")
1179                || trimmed.starts_with("sealed class ")
1180                || trimmed.starts_with("sealed interface ")
1181                || trimmed.starts_with("enum class ")
1182                || trimmed.starts_with("object ")
1183                || trimmed.starts_with("private object ")
1184                || trimmed.starts_with("interface ")
1185                || trimmed.starts_with("typealias ")
1186                || trimmed.starts_with("private typealias ");
1187            if is_def {
1188                start = i + 1;
1189                break;
1190            }
1191        }
1192    }
1193
1194    if start == 0 {
1195        return (1, lines.len().min(20));
1196    }
1197
1198    let base_indent = lines
1199        .get(start - 1)
1200        .map_or(0, |l| l.len() - l.trim_start().len());
1201
1202    let mut end = start;
1203    let mut brace_depth: i32 = 0;
1204    let mut found_open = false;
1205
1206    for (i, line) in lines.iter().enumerate().skip(start - 1) {
1207        for ch in line.chars() {
1208            if ch == '{' {
1209                brace_depth += 1;
1210                found_open = true;
1211            } else if ch == '}' {
1212                brace_depth -= 1;
1213            }
1214        }
1215
1216        end = i + 1;
1217
1218        if found_open && brace_depth <= 0 {
1219            break;
1220        }
1221
1222        if !found_open && i > start {
1223            let indent = line.len() - line.trim_start().len();
1224            if indent <= base_indent && !line.trim().is_empty() && i > start {
1225                end = i;
1226                break;
1227            }
1228        }
1229
1230        if end - start > 200 {
1231            break;
1232        }
1233    }
1234
1235    (start, end)
1236}
1237
1238fn extract_summary(content: &str) -> String {
1239    for line in content.lines().take(20) {
1240        let trimmed = line.trim();
1241        if trimmed.is_empty()
1242            || trimmed.starts_with("//")
1243            || trimmed.starts_with('#')
1244            || trimmed.starts_with("/*")
1245            || trimmed.starts_with('*')
1246            || trimmed.starts_with("use ")
1247            || trimmed.starts_with("import ")
1248            || trimmed.starts_with("from ")
1249            || trimmed.starts_with("require(")
1250            || trimmed.starts_with("package ")
1251        {
1252            continue;
1253        }
1254        return trimmed.chars().take(120).collect();
1255    }
1256    String::new()
1257}
1258
1259fn compute_hash(content: &str) -> String {
1260    use std::collections::hash_map::DefaultHasher;
1261    use std::hash::{Hash, Hasher};
1262
1263    let mut hasher = DefaultHasher::new();
1264    content.hash(&mut hasher);
1265    format!("{:016x}", hasher.finish())
1266}
1267
1268#[cfg(test)]
1269fn short_hash(input: &str) -> String {
1270    use std::collections::hash_map::DefaultHasher;
1271    use std::hash::{Hash, Hasher};
1272
1273    let mut hasher = DefaultHasher::new();
1274    input.hash(&mut hasher);
1275    format!("{:08x}", hasher.finish() & 0xFFFF_FFFF)
1276}
1277
1278fn make_relative(path: &str, root: &str) -> String {
1279    graph_relative_key(path, root)
1280}
1281
1282fn is_indexable_ext(ext: &str) -> bool {
1283    crate::core::language_capabilities::is_indexable_ext(ext)
1284}
1285
1286#[cfg(test)]
1287fn kotlin_package_name(content: &str) -> Option<String> {
1288    content.lines().map(str::trim).find_map(|line| {
1289        line.strip_prefix("package ")
1290            .map(|rest| rest.trim().trim_end_matches(';').to_string())
1291    })
1292}