infigraph-core 1.5.3

AST-powered code analysis framework — parser, graph, diff, and analysis engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
mod analysis;
pub mod bench;
pub mod bridges;
pub mod check;
pub mod cluster;
pub mod concerns;
pub mod config;
pub mod diff;
pub mod embed;
pub mod export;
pub mod extract;
pub mod graph;
pub mod lang;
pub mod learned;
pub mod manifest;
pub mod model;
pub mod multi;
pub mod patterns;
pub mod refactor;
pub mod reflection;
mod report;
pub mod resolve;
pub mod review;
pub mod routes;
pub mod scip;
pub mod search;
pub mod security;
pub mod sequence;
pub mod structured;
pub mod taint;
pub mod viz;
pub mod vuln;
pub mod watch;

use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use rayon::prelude::*;
use sha2::{Digest, Sha256};

use graph::GraphStore;
use lang::LanguageRegistry;
use model::FileExtraction;

pub(crate) fn escape_str(s: &str) -> String {
    s.replace('\\', "\\\\").replace('\'', "\\'")
}

/// The main entry point for the infigraph framework.
pub struct Infigraph {
    root: PathBuf,
    db_path: PathBuf,
    registry: LanguageRegistry,
    store: Option<GraphStore>,
}

impl Infigraph {
    /// Open a project directory. Creates `.infigraph/` if it doesn't exist.
    pub fn open(root: &Path, registry: LanguageRegistry) -> Result<Self> {
        let root = root.canonicalize().context("invalid project root")?;
        let db_path = root.join(".infigraph").join("graph");
        Ok(Self {
            root,
            db_path,
            registry,
            store: None,
        })
    }

    /// Initialize the graph store (creates DB on first run).
    pub fn init(&mut self) -> Result<()> {
        let store = GraphStore::open(&self.db_path)?;
        self.store = Some(store);
        Ok(())
    }

    /// Index all supported files in the project, building the graph.
    /// Skips files whose content hash matches the stored hash (incremental).
    pub fn index(&self) -> Result<IndexResult> {
        let store = self.store.as_ref().context("call init() first")?;

        let files = self.collect_files()?;
        let total = files.len();

        // Load existing hashes for incremental skip
        let existing_hashes = store.get_file_hashes().unwrap_or_default();

        // Parse all files in parallel; skip unchanged ones
        let done = std::sync::atomic::AtomicUsize::new(0);
        let extractions: Vec<FileExtraction> = files
            .par_iter()
            .filter_map(|path| {
                let rel_path = path
                    .strip_prefix(&self.root)
                    .ok()?
                    .to_string_lossy()
                    .replace('\\', "/");
                let source = std::fs::read(path).ok()?;
                // Skip if hash unchanged
                let hash = {
                    let mut h = Sha256::new();
                    h.update(&source);
                    format!("{:x}", h.finalize())
                };
                let n = done.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
                let pct = n * 100 / total;
                let prev_pct = (n - 1) * 100 / total;
                if (pct / 25) > (prev_pct / 25) || n == total {
                    eprintln!("Parsing: {}/{} ({}%)", n, total, pct);
                }
                if existing_hashes.get(&rel_path).map(|s| s.as_str()) == Some(hash.as_str()) {
                    return None; // unchanged
                }
                let pack = self.registry.for_file_with_content(&rel_path, &source)?;
                extract::extract_file(&rel_path, &source, pack).ok()
            })
            .collect();

        let indexed = extractions.len();

        // Write all changed files — use CSV bulk load for fresh index or large batches,
        // fall back to per-file UNWIND only for small incremental updates.
        let use_csv = !extractions.is_empty() && (existing_hashes.is_empty() || indexed > 100);
        let _write_lock = if !extractions.is_empty() {
            Some(store.write_lock()?)
        } else {
            None
        };

        if !extractions.is_empty() {
            if use_csv {
                if !existing_hashes.is_empty() {
                    let conn = store.connection()?;
                    conn.query("BEGIN TRANSACTION")
                        .context("failed to begin delete transaction")?;
                    let file_list: Vec<String> = extractions
                        .iter()
                        .map(|e| format!("'{}'", escape_str(&e.file)))
                        .collect();
                    let files_in = file_list.join(", ");
                    let _ = conn.query(&format!(
                        "MATCH (f:File)-[:DEFINES]->(s:Symbol)-[:HAS_STATEMENT]->(st:Statement) WHERE f.id IN [{}] DETACH DELETE st",
                        files_in
                    ));
                    let _ = conn.query(&format!(
                        "MATCH (s:Symbol) WHERE s.file IN [{}] DETACH DELETE s",
                        files_in
                    ));
                    let _ = conn.query(&format!(
                        "MATCH (m:Module) WHERE m.file IN [{}] DETACH DELETE m",
                        files_in
                    ));
                    let _ = conn.query(&format!(
                        "MATCH (f:File) WHERE f.id IN [{}] DETACH DELETE f",
                        files_in
                    ));
                    conn.query("COMMIT")
                        .context("failed to commit delete transaction")?;
                }
                let conn = store.connection()?;
                store.upsert_all_parquet_conn(&conn, &extractions)?;
            } else {
                let conn = store.connection()?;
                conn.query("BEGIN TRANSACTION")
                    .context("failed to begin index transaction")?;
                let file_list: Vec<String> = extractions
                    .iter()
                    .map(|e| format!("'{}'", escape_str(&e.file)))
                    .collect();
                let files_in = file_list.join(", ");
                let _ = conn.query(&format!(
                    "MATCH (f:File)-[:DEFINES]->(s:Symbol)-[:HAS_STATEMENT]->(st:Statement) WHERE f.id IN [{}] DETACH DELETE st",
                    files_in
                ));
                let _ = conn.query(&format!(
                    "MATCH (s:Symbol) WHERE s.file IN [{}] DETACH DELETE s",
                    files_in
                ));
                let _ = conn.query(&format!(
                    "MATCH (m:Module) WHERE m.file IN [{}] DETACH DELETE m",
                    files_in
                ));
                let _ = conn.query(&format!(
                    "MATCH (f:File) WHERE f.id IN [{}] DETACH DELETE f",
                    files_in
                ));
                for extraction in &extractions {
                    store.upsert_file_conn_no_delete(&conn, extraction)?;
                }
                conn.query("COMMIT")
                    .context("failed to commit index transaction")?;
                let file_paths: Vec<&str> = extractions.iter().map(|e| e.file.as_str()).collect();
                store.upsert_folders_bulk_conn(&conn, &file_paths)?;
            }
        }

        if use_csv {
            let file_paths: Vec<&str> = extractions.iter().map(|e| e.file.as_str()).collect();
            let conn = store.connection()?;
            store.upsert_folders_bulk_conn(&conn, &file_paths)?;
        }

        // resolve runs under the same write lock (creates CALLS/INHERITS edges)
        let resolve_stats = resolve::resolve_calls_incremental(store, &extractions, None)
            .unwrap_or_else(|e| {
                eprintln!("warning: call resolution failed: {e}");
                resolve::ResolveStats {
                    total_calls: 0,
                    resolved: 0,
                    unresolved: 0,
                    learned_resolved: 0,
                    inherits_resolved: 0,
                }
            });

        drop(_write_lock);

        Ok(IndexResult {
            total_files: total,
            indexed_files: indexed,
            extractions,
            resolve_stats,
        })
    }

    /// Get graph statistics.
    pub fn stats(&self) -> Result<graph::GraphStats> {
        let store = self.store.as_ref().context("call init() first")?;
        store.stats()
    }

    /// Access the underlying graph store (for direct queries).
    pub fn store(&self) -> Option<&GraphStore> {
        self.store.as_ref()
    }

    /// Access the language registry.
    pub fn registry(&self) -> &LanguageRegistry {
        &self.registry
    }

    /// Get the project root path.
    pub fn root(&self) -> &Path {
        &self.root
    }

    /// Index (or re-index) a single file by its path on disk.
    /// Path may be absolute or relative to project root.
    pub fn index_file(&self, path: &Path) -> Result<()> {
        let store = self.store.as_ref().context("call init() first")?;
        let rel = if path.is_absolute() {
            path.strip_prefix(&self.root)
                .unwrap_or(path)
                .to_string_lossy()
                .replace('\\', "/")
        } else {
            path.to_string_lossy().replace('\\', "/")
        };
        let abs = self.root.join(&rel);
        let source = std::fs::read(&abs).with_context(|| format!("read {}", abs.display()))?;
        let pack = self
            .registry
            .for_file_with_content(&rel, &source)
            .with_context(|| format!("no language for {rel}"))?;
        let extraction = extract::extract_file(&rel, &source, pack)?;
        store.upsert_file(&extraction)?;
        Ok(())
    }

    /// Index a batch of files by path, returning an IndexResult with all extractions.
    pub fn index_files(&self, paths: &[PathBuf]) -> Result<IndexResult> {
        let store = self.store.as_ref().context("call init() first")?;

        if paths.is_empty() {
            return Ok(IndexResult {
                total_files: 0,
                indexed_files: 0,
                extractions: Vec::new(),
                resolve_stats: resolve::ResolveStats {
                    total_calls: 0,
                    resolved: 0,
                    unresolved: 0,
                    learned_resolved: 0,
                    inherits_resolved: 0,
                },
            });
        }

        let extractions: Vec<FileExtraction> = paths
            .par_iter()
            .filter_map(|path| {
                let rel = if path.is_absolute() {
                    path.strip_prefix(&self.root)
                        .unwrap_or(path)
                        .to_string_lossy()
                        .replace('\\', "/")
                } else {
                    path.to_string_lossy().replace('\\', "/")
                };
                let abs = self.root.join(&rel);
                let source = std::fs::read(&abs).ok()?;
                let pack = self.registry.for_file_with_content(&rel, &source)?;
                extract::extract_file(&rel, &source, pack).ok()
            })
            .collect();

        let extractions = {
            let mut seen = std::collections::HashSet::new();
            extractions
                .into_iter()
                .filter(|e| seen.insert(e.file.clone()))
                .collect::<Vec<_>>()
        };

        let indexed = extractions.len();

        let _write_lock = if !extractions.is_empty() {
            Some(store.write_lock()?)
        } else {
            None
        };

        if !extractions.is_empty() {
            let conn = store.connection()?;
            conn.query("BEGIN TRANSACTION")
                .context("failed to begin batch delete transaction")?;
            let file_list: Vec<String> = extractions
                .iter()
                .map(|e| format!("'{}'", escape_str(&e.file)))
                .collect();
            let files_in = file_list.join(", ");
            let _ = conn.query(&format!(
                "MATCH (f:File)-[:DEFINES]->(s:Symbol)-[:HAS_STATEMENT]->(st:Statement) WHERE f.id IN [{files_in}] DETACH DELETE st"
            ));
            let _ = conn.query(&format!(
                "MATCH (s:Symbol) WHERE s.file IN [{files_in}] DETACH DELETE s"
            ));
            let _ = conn.query(&format!(
                "MATCH (m:Module) WHERE m.file IN [{files_in}] DETACH DELETE m"
            ));
            let _ = conn.query(&format!(
                "MATCH (f:File) WHERE f.id IN [{files_in}] DETACH DELETE f"
            ));
            conn.query("COMMIT")
                .context("failed to commit batch delete transaction")?;

            if indexed > 10 {
                let conn = store.connection()?;
                store.upsert_all_parquet_conn(&conn, &extractions)?;
            } else {
                let conn = store.connection()?;
                store.upsert_all_bulk(&conn, &extractions)?;
            }

            let file_paths: Vec<&str> = extractions.iter().map(|e| e.file.as_str()).collect();
            let conn = store.connection()?;
            store.upsert_folders_bulk_conn(&conn, &file_paths)?;
        }

        let resolve_stats = resolve::resolve_calls_incremental(store, &extractions, None)
            .unwrap_or_else(|e| {
                eprintln!("warning: call resolution failed: {e}");
                resolve::ResolveStats {
                    total_calls: 0,
                    resolved: 0,
                    unresolved: 0,
                    learned_resolved: 0,
                    inherits_resolved: 0,
                }
            });

        drop(_write_lock);

        Ok(IndexResult {
            total_files: paths.len(),
            indexed_files: indexed,
            extractions,
            resolve_stats,
        })
    }

    /// Detect cross-language bridges (FFI, JNI, cgo, gRPC, P/Invoke, WASM, ctypes).
    pub fn detect_bridges(&self) -> Result<bridges::BridgeScanResult> {
        bridges::detect_bridges(&self.root)
    }

    /// Remove a deleted file from the graph.
    pub fn remove_file(&self, path: &Path) -> Result<()> {
        let store = self.store.as_ref().context("call init() first")?;
        let rel = if path.is_absolute() {
            path.strip_prefix(&self.root)
                .unwrap_or(path)
                .to_string_lossy()
                .replace('\\', "/")
        } else {
            path.to_string_lossy().replace('\\', "/")
        };
        store.remove_file(&rel)
    }

    fn collect_files(&self) -> Result<Vec<PathBuf>> {
        use ignore::WalkBuilder;

        let mut files = Vec::new();
        let walker = WalkBuilder::new(&self.root)
            .hidden(true)
            .add_custom_ignore_filename(".infigraphignore")
            .git_ignore(true)
            .filter_entry(|e| {
                let name = e.file_name().to_string_lossy();
                !matches!(
                    name.as_ref(),
                    ".infigraph" | "node_modules" | "__pycache__" | ".tox"
                )
            })
            .build();

        for result in walker {
            let entry = result?;
            if entry.file_type().is_some_and(|ft| ft.is_file()) {
                let path = entry.path();
                if self.registry.for_file(&path.to_string_lossy()).is_some() {
                    files.push(path.to_path_buf());
                }
            }
        }
        Ok(files)
    }
}

pub struct IndexResult {
    pub total_files: usize,
    pub indexed_files: usize,
    pub extractions: Vec<FileExtraction>,
    pub resolve_stats: resolve::ResolveStats,
}