infigraph-core 1.5.0

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
mod bridge;
pub mod combined;
mod cross_service;
pub mod grpc;

pub use bridge::*;
pub use cross_service::*;

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

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

use crate::graph::GraphQuery;
use crate::lang::LanguageRegistry;
use crate::Infigraph;

/// Global registry stored at ~/.infigraph/registry.json
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Registry {
    pub repos: HashMap<String, RepoEntry>,
    pub groups: HashMap<String, Group>,
}

/// A registered repository.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RepoEntry {
    pub name: String,
    pub path: PathBuf,
    pub languages: Vec<String>,
    pub symbol_count: u64,
    pub module_count: u64,
}

/// A group of repositories (e.g., microservice architecture).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Group {
    pub name: String,
    pub repos: Vec<String>,
    pub contracts: Vec<Contract>,
}

/// A contract extracted from a service (HTTP route, gRPC endpoint, etc.).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Contract {
    pub kind: ContractKind,
    pub service: String,
    pub method: String,
    pub path: String,
    pub symbol_id: String,
    pub file: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ContractKind {
    HttpRoute,
    GrpcService,
    EventPublish,
    EventSubscribe,
}

impl Registry {
    /// Load registry from ~/.infigraph/registry.json
    pub fn load() -> Result<Self> {
        let path = registry_path()?;
        if !path.exists() {
            return Ok(Self::default());
        }
        let data = std::fs::read_to_string(&path)?;
        let registry: Registry = serde_json::from_str(&data)?;
        Ok(registry)
    }

    /// Save registry to disk.
    pub fn save(&self) -> Result<()> {
        let path = registry_path()?;
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let data = serde_json::to_string_pretty(self)?;
        std::fs::write(&path, data)?;
        Ok(())
    }

    /// Register a repository after indexing.
    pub fn register_repo(&mut self, name: &str, path: &Path, prism: &Infigraph) -> Result<()> {
        let stats = prism.stats()?;
        let langs: Vec<String> = prism
            .registry()
            .languages()
            .map(|p| p.name.clone())
            .collect();

        let abs_path = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());

        self.repos.insert(
            name.to_string(),
            RepoEntry {
                name: name.to_string(),
                path: abs_path,
                languages: langs,
                symbol_count: stats.symbols,
                module_count: stats.modules,
            },
        );
        self.save()
    }

    /// Create a new group.
    pub fn create_group(&mut self, name: &str) -> Result<()> {
        if self.groups.contains_key(name) {
            anyhow::bail!("group '{}' already exists", name);
        }
        self.groups.insert(
            name.to_string(),
            Group {
                name: name.to_string(),
                repos: Vec::new(),
                contracts: Vec::new(),
            },
        );
        self.save()
    }

    /// Add a repo to a group.
    pub fn group_add(&mut self, group_name: &str, repo_name: &str) -> Result<()> {
        let group = self
            .groups
            .get_mut(group_name)
            .context(format!("group '{}' not found", group_name))?;
        if !self.repos.contains_key(repo_name) {
            anyhow::bail!("repo '{}' not registered. Run index first.", repo_name);
        }
        if !group.repos.contains(&repo_name.to_string()) {
            group.repos.push(repo_name.to_string());
        }
        self.save()
    }

    /// Remove a repo from a group.
    pub fn group_remove(&mut self, group_name: &str, repo_name: &str) -> Result<()> {
        let group = self
            .groups
            .get_mut(group_name)
            .context(format!("group '{}' not found", group_name))?;
        group.repos.retain(|r| r != repo_name);
        self.save()
    }

    /// Search across all repos in a group.
    pub fn group_query(
        &self,
        group_name: &str,
        cypher: &str,
        build_registry: impl Fn() -> Result<LanguageRegistry>,
    ) -> Result<Vec<(String, Vec<Vec<String>>)>> {
        let group = self
            .groups
            .get(group_name)
            .context(format!("group '{}' not found", group_name))?;

        let mut results = Vec::new();
        for repo_name in &group.repos {
            let entry = self
                .repos
                .get(repo_name)
                .context(format!("repo '{}' not in registry", repo_name))?;

            let registry = build_registry()?;
            let mut prism = Infigraph::open(&entry.path, registry)?;
            prism.init()?;

            let store = prism.store().context("graph not initialized")?;
            let conn = store.connection()?;
            let gq = GraphQuery::new(&conn);

            match gq.raw_query(cypher) {
                Ok(rows) => {
                    if !rows.is_empty() {
                        results.push((repo_name.clone(), rows));
                    }
                }
                Err(e) => {
                    eprintln!("warning: query failed for repo '{}': {}", repo_name, e);
                }
            }
        }
        Ok(results)
    }
}

/// Extract HTTP route contracts from a project's graph.
///
/// Sources (in priority order):
/// 1. Route symbols (kind='Route') — from call-expression routing (Express, Gin, etc.)
/// 2. Decorated functions — docstring contains route decorator (@app.route, #[get], etc.)
/// 3. Heuristic detect_routes fallback
pub fn extract_contracts(prism: &Infigraph, service_name: &str) -> Result<Vec<Contract>> {
    let store = prism.store().context("graph not initialized")?;
    let conn = store.connection()?;
    let gq = GraphQuery::new(&conn);

    let mut contracts = Vec::new();
    let mut seen_paths: std::collections::HashSet<String> = std::collections::HashSet::new();

    // 1. Route symbols (call-expression routes: Express, Gin, Django, etc.)
    let route_rows = gq.raw_query(
        "MATCH (s:Symbol) WHERE s.kind = 'Route' RETURN s.id, s.name, s.kind, s.file, s.docstring",
    )?;
    for row in &route_rows {
        let (method, path) = parse_route_name(&row[1]);
        let key = format!("{} {}", method, path);
        if seen_paths.insert(key) {
            contracts.push(Contract {
                kind: ContractKind::HttpRoute,
                service: service_name.to_string(),
                method,
                path,
                symbol_id: row[0].clone(),
                file: row[3].clone(),
            });
        }
    }

    // 2. Decorated functions with route info in docstring
    let decorated_rows = gq.raw_query(
        "MATCH (s:Symbol) WHERE s.kind IN ['Function', 'Method'] AND s.docstring IS NOT NULL AND (s.docstring CONTAINS '@app.route' OR s.docstring CONTAINS '@app.get' OR s.docstring CONTAINS '@app.post' OR s.docstring CONTAINS '#[get' OR s.docstring CONTAINS '#[post' OR s.docstring CONTAINS '@GetMapping' OR s.docstring CONTAINS '@PostMapping' OR s.docstring CONTAINS '@RequestMapping' OR s.docstring CONTAINS 'MapGet' OR s.docstring CONTAINS 'MapPost') RETURN s.id, s.name, s.kind, s.file, s.docstring",
    )?;
    for row in &decorated_rows {
        let doc = row.get(4).map(|s| s.as_str()).unwrap_or("");
        let (method, path) = parse_route_from_docstring(doc);
        if !path.is_empty() {
            let key = format!("{} {}", method, path);
            if seen_paths.insert(key) {
                contracts.push(Contract {
                    kind: ContractKind::HttpRoute,
                    service: service_name.to_string(),
                    method,
                    path,
                    symbol_id: row[0].clone(),
                    file: row[3].clone(),
                });
            }
        }
    }

    Ok(contracts)
}

/// Parse "GET /api/users" or "MAPGET /api/users" into (method, path).
fn parse_route_name(name: &str) -> (String, String) {
    let parts: Vec<&str> = name.splitn(2, ' ').collect();
    if parts.len() == 2 {
        let method = parts[0].trim().to_uppercase();
        // Normalize MapGet -> GET, MapPost -> POST, etc.
        let method = if method.starts_with("MAP") {
            method.trim_start_matches("MAP").to_string()
        } else {
            method
        };
        (method, parts[1].trim().to_string())
    } else {
        ("UNKNOWN".to_string(), name.to_string())
    }
}

/// Extract method and path from decorator docstrings.
fn parse_route_from_docstring(doc: &str) -> (String, String) {
    // @app.route("/api/users", methods=["GET"])
    // @app.get("/api/users")
    // #[get("/api/payments")]
    // @GetMapping("/api/users")
    let doc_lower = doc.to_lowercase();

    // Extract path from quotes
    let path = doc
        .split('"')
        .chain(doc.split('\''))
        .find(|s| s.starts_with('/'))
        .unwrap_or("")
        .to_string();

    // Extract method
    let method = if doc_lower.contains("methods") {
        // methods=["GET", "POST"] — take first
        if doc_lower.contains("\"get\"") || doc_lower.contains("'get'") {
            "GET"
        } else if doc_lower.contains("\"post\"") || doc_lower.contains("'post'") {
            "POST"
        } else if doc_lower.contains("\"put\"") || doc_lower.contains("'put'") {
            "PUT"
        } else if doc_lower.contains("\"delete\"") || doc_lower.contains("'delete'") {
            "DELETE"
        } else if doc_lower.contains("\"patch\"") || doc_lower.contains("'patch'") {
            "PATCH"
        } else {
            "UNKNOWN"
        }
    } else if doc_lower.contains("@app.get")
        || doc_lower.contains("#[get")
        || doc_lower.contains("getmapping")
        || doc_lower.contains("mapget")
    {
        "GET"
    } else if doc_lower.contains("@app.post")
        || doc_lower.contains("#[post")
        || doc_lower.contains("postmapping")
        || doc_lower.contains("mappost")
    {
        "POST"
    } else if doc_lower.contains("@app.put")
        || doc_lower.contains("#[put")
        || doc_lower.contains("putmapping")
        || doc_lower.contains("mapput")
    {
        "PUT"
    } else if doc_lower.contains("@app.delete")
        || doc_lower.contains("#[delete")
        || doc_lower.contains("deletemapping")
        || doc_lower.contains("mapdelete")
    {
        "DELETE"
    } else if doc_lower.contains("@app.patch")
        || doc_lower.contains("#[patch")
        || doc_lower.contains("patchmapping")
        || doc_lower.contains("mappatch")
    {
        "PATCH"
    } else {
        "UNKNOWN"
    };

    (method.to_string(), path)
}

/// Sync contracts for all repos in a group.
pub fn sync_group_contracts(
    registry: &mut Registry,
    group_name: &str,
    build_registry: impl Fn() -> Result<LanguageRegistry>,
) -> Result<usize> {
    let group = registry
        .groups
        .get(group_name)
        .context(format!("group '{}' not found", group_name))?
        .clone();

    let mut all_contracts = Vec::new();

    for repo_name in &group.repos {
        let entry = registry
            .repos
            .get(repo_name)
            .context(format!("repo '{}' not in registry", repo_name))?
            .clone();

        let lang_registry = build_registry()?;
        let mut prism = Infigraph::open(&entry.path, lang_registry)?;
        prism.init()?;

        let contracts = extract_contracts(&prism, repo_name)?;
        all_contracts.extend(contracts);
    }

    let count = all_contracts.len();
    let group = registry
        .groups
        .get_mut(group_name)
        .context("group not found")?;
    group.contracts = all_contracts;
    registry.save()?;

    Ok(count)
}

/// Index all repos in a group. Returns Vec of (repo_name, indexed_files, total_files).
pub fn index_group(
    registry: &mut Registry,
    group_name: &str,
    full: bool,
    build_registry: impl Fn() -> Result<LanguageRegistry>,
) -> Result<Vec<(String, usize, usize)>> {
    let group = registry
        .groups
        .get(group_name)
        .context(format!("group '{}' not found", group_name))?
        .clone();

    let mut results = Vec::new();

    for repo_name in &group.repos {
        let entry = registry
            .repos
            .get(repo_name)
            .context(format!("repo '{}' not in registry", repo_name))?
            .clone();

        if full {
            let tg_dir = entry.path.join(".infigraph");
            if tg_dir.exists() {
                std::fs::remove_dir_all(&tg_dir)?;
            }
        }

        let lang_registry = build_registry()?;
        let mut prism = Infigraph::open(&entry.path, lang_registry)?;
        prism.init()?;
        let result = prism.index()?;
        results.push((repo_name.clone(), result.indexed_files, result.total_files));

        registry.register_repo(repo_name, &entry.path, &prism)?;
    }

    Ok(results)
}

fn registry_path() -> Result<PathBuf> {
    let home = dirs_next::home_dir().context("cannot determine home directory")?;
    Ok(home.join(".infigraph").join("registry.json"))
}