ferrograph 1.4.0

Graph-powered Rust code intelligence
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Configuration and CLI definition.

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

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};

/// Graph-powered Rust code intelligence.
#[derive(Debug, Parser)]
#[command(
    name = "ferrograph",
    version,
    about,
    long_version = concat!(include_str!("../assets/ascii-banner.txt"), "\n  v", env!("CARGO_PKG_VERSION"))
)]
pub struct Cli {
    /// Output JSON instead of human-readable text.
    #[arg(long, global = true)]
    pub json: bool,

    #[command(subcommand)]
    pub command: Command,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    /// Index a Rust project into the graph.
    Index {
        /// Root path of the Rust project (directory containing Cargo.toml or src/).
        #[arg(default_value = ".")]
        path: PathBuf,
        /// Output path for the graph database.
        #[arg(short, long)]
        output: Option<PathBuf>,
    },
    /// Run a Datalog or named query against the graph.
    Query {
        /// Path to the graph database (default: .ferrograph in project root).
        #[arg(short, long)]
        db: Option<PathBuf>,
        /// Query to run (Datalog script or named query).
        query: String,
    },
    /// Semantic search over the codebase.
    Search {
        /// Path to the graph database.
        #[arg(short, long)]
        db: Option<PathBuf>,
        /// Search query.
        query: String,
        /// Match case-insensitively.
        #[arg(short, long)]
        case_insensitive: bool,
    },
    /// Show index status and stats.
    Status {
        /// Path to the project or graph database.
        #[arg(default_value = ".")]
        path: PathBuf,
    },
    /// Watch for file changes and re-index.
    Watch {
        /// Root path of the Rust project.
        #[arg(default_value = ".")]
        path: PathBuf,
        /// Output path for the graph database.
        #[arg(short, long, required = true)]
        output: Option<PathBuf>,
    },
    /// Find dead (unreachable) code.
    Dead {
        /// Path to the graph database.
        #[arg(short, long)]
        db: Option<PathBuf>,
        /// Filter results by file glob pattern.
        #[arg(short, long)]
        file: Option<String>,
    },
    /// Show blast radius (transitive impact) of a node.
    Blast {
        /// Path to the graph database.
        #[arg(short, long)]
        db: Option<PathBuf>,
        /// Node ID to compute blast radius from.
        node_id: String,
    },
    /// Show callers of a node (reverse call graph).
    Callers {
        /// Path to the graph database.
        #[arg(short, long)]
        db: Option<PathBuf>,
        /// Node ID to find callers for.
        node_id: String,
        /// Maximum call depth (default: 1 = direct callers only).
        #[arg(long, default_value = "1")]
        depth: u32,
    },
    /// Show full info for a node (type, payload, edges).
    Info {
        /// Path to the graph database.
        #[arg(short, long)]
        db: Option<PathBuf>,
        /// Node ID to inspect.
        node_id: String,
    },
    /// Show module containment graph.
    Modules {
        /// Path to the graph database.
        #[arg(short, long)]
        db: Option<PathBuf>,
        /// Filter to modules under this path prefix (e.g. "./src/").
        #[arg(short, long)]
        root: Option<String>,
    },
    /// Show trait implementors.
    Traits {
        /// Path to the graph database.
        #[arg(short, long)]
        db: Option<PathBuf>,
        /// Trait name to search for.
        trait_name: String,
    },
    /// Run the MCP server over stdio (for AI agents and IDEs).
    Mcp,
}

/// Run the CLI command.
///
/// # Errors
/// Returns an error if the selected command fails (e.g. I/O or graph errors).
pub fn run(cli: Cli) -> Result<()> {
    let json = cli.json;
    match cli.command {
        Command::Index { path, output } => run_index(&path, output.as_ref()),
        Command::Query { db, query } => run_query(db.as_ref(), &query, json),
        Command::Search {
            db,
            query,
            case_insensitive,
        } => run_search(db.as_ref(), &query, case_insensitive, json),
        Command::Status { path } => run_status(&path, json),
        Command::Watch { path, output } => run_watch(&path, output.as_ref()),
        Command::Dead { db, file } => run_dead(db.as_ref(), file.as_deref(), json),
        Command::Blast { db, node_id } => run_blast(db.as_ref(), &node_id, json),
        Command::Callers { db, node_id, depth } => run_callers(db.as_ref(), &node_id, depth, json),
        Command::Info { db, node_id } => run_info(db.as_ref(), &node_id, json),
        Command::Modules { db, root } => run_modules(db.as_ref(), root.as_deref(), json),
        Command::Traits { db, trait_name } => run_traits(db.as_ref(), &trait_name, json),
        Command::Mcp => run_mcp(),
    }
}

fn run_mcp() -> Result<()> {
    let rt = tokio::runtime::Runtime::new()?;
    rt.block_on(crate::mcp::run_stdio())
        .map_err(|e| anyhow::anyhow!("{e}"))?;
    Ok(())
}

fn default_db_path() -> Option<PathBuf> {
    std::env::current_dir().ok().map(|p| p.join(".ferrograph"))
}

fn resolve_db_path(db: Option<&PathBuf>) -> Result<PathBuf> {
    db.cloned()
        .or_else(default_db_path)
        .context("No graph database path (use --db or run from a directory with .ferrograph)")
}

fn run_index(path: &Path, output: Option<&PathBuf>) -> Result<()> {
    let store = if let Some(out) = output {
        crate::graph::Store::new_persistent(out)
            .with_context(|| format!("Failed to create persistent store at {}", out.display()))?
    } else {
        crate::graph::Store::new_memory()?
    };
    let config = crate::pipeline::PipelineConfig::default();
    crate::pipeline::run_pipeline(&store, path, &config)?;
    if let Some(out) = output {
        println!("Indexed {} into {}", path.display(), out.display());
    } else {
        let nodes = store.node_count()?;
        let edges = store.edge_count()?;
        println!(
            "Indexed {} (in-memory: {nodes} nodes, {edges} edges; use --output to persist)",
            path.display()
        );
    }
    Ok(())
}

fn run_query(db: Option<&PathBuf>, query: &str, json: bool) -> Result<()> {
    let db_path = resolve_db_path(db)?;
    if !db_path.exists() {
        anyhow::bail!(
            "Graph database not found at {}. Run 'ferrograph index --output {}' first.",
            db_path.display(),
            db_path.display()
        );
    }
    let store = crate::graph::Store::new_persistent(&db_path)
        .with_context(|| format!("Failed to open graph at {}", db_path.display()))?;
    let result = crate::ops::query(&store, query).context("Query execution failed")?;
    if json {
        println!("{}", serde_json::to_string_pretty(&result)?);
    } else {
        for row in &result.rows {
            let cols: Vec<String> = row
                .iter()
                .map(|v| match v {
                    serde_json::Value::String(s) => s.clone(),
                    other => other.to_string(),
                })
                .collect();
            println!("{}", cols.join("\t"));
        }
    }
    Ok(())
}

fn run_search(db: Option<&PathBuf>, query: &str, case_insensitive: bool, json: bool) -> Result<()> {
    let db_path = resolve_db_path(db)?;
    if !db_path.exists() {
        anyhow::bail!(
            "Graph database not found at {}. Run 'ferrograph index --output {}' first.",
            db_path.display(),
            db_path.display()
        );
    }
    let store = crate::graph::Store::new_persistent(&db_path)
        .with_context(|| format!("Failed to open graph at {}", db_path.display()))?;
    let result = crate::ops::search(&store, query, case_insensitive, 10_000, 0)?;
    if json {
        println!("{}", serde_json::to_string_pretty(&result)?);
    } else {
        for item in &result.results {
            let payload_display = item.payload.as_deref().unwrap_or("");
            println!("{}\t{}\t{payload_display}", item.id, item.node_type);
        }
    }
    Ok(())
}

fn run_watch(path: &Path, output: Option<&PathBuf>) -> Result<()> {
    let out = output
        .ok_or_else(|| anyhow::anyhow!("Watch requires --output (path to graph database)"))?;
    let store = crate::graph::Store::new_persistent(out)
        .with_context(|| format!("Failed to open graph at {}", out.display()))?;
    let config = crate::pipeline::PipelineConfig::default();
    crate::watch::watch_and_reindex(&store, path, &config)
}

fn run_dead(db: Option<&PathBuf>, file: Option<&str>, json: bool) -> Result<()> {
    let db_path = resolve_db_path(db)?;
    if !db_path.exists() {
        anyhow::bail!(
            "Graph database not found at {}. Run 'ferrograph index --output {}' first.",
            db_path.display(),
            db_path.display()
        );
    }
    let store = crate::graph::Store::new_persistent(&db_path)
        .with_context(|| format!("Failed to open graph at {}", db_path.display()))?;
    let result = crate::ops::dead_code(&store, file)?;
    if json {
        println!("{}", serde_json::to_string_pretty(&result)?);
    } else {
        for node in &result.dead_nodes {
            let payload = node.payload.as_deref().unwrap_or("");
            println!("{}\t{}\t{payload}", node.id, node.node_type);
        }
        println!(
            "\n{} dead nodes found (source: {})",
            result.count, result.source
        );
        println!("\n{}", crate::ops::DEAD_CODE_CAVEAT);
    }
    Ok(())
}

fn run_blast(db: Option<&PathBuf>, node_id: &str, json: bool) -> Result<()> {
    let db_path = resolve_db_path(db)?;
    if !db_path.exists() {
        anyhow::bail!(
            "Graph database not found at {}. Run 'ferrograph index --output {}' first.",
            db_path.display(),
            db_path.display()
        );
    }
    let store = crate::graph::Store::new_persistent(&db_path)
        .with_context(|| format!("Failed to open graph at {}", db_path.display()))?;
    let result = crate::ops::blast_radius(&store, node_id)?;
    if json {
        println!("{}", serde_json::to_string_pretty(&result)?);
    } else {
        for node in &result.reachable_nodes {
            let payload = node.payload.as_deref().unwrap_or("");
            println!("{}\t{}\t{payload}", node.id, node.node_type);
        }
        println!("\n{} nodes in blast radius of {}", result.count, node_id);
    }
    Ok(())
}

fn run_callers(db: Option<&PathBuf>, node_id: &str, depth: u32, json: bool) -> Result<()> {
    let db_path = resolve_db_path(db)?;
    if !db_path.exists() {
        anyhow::bail!(
            "Graph database not found at {}. Run 'ferrograph index --output {}' first.",
            db_path.display(),
            db_path.display()
        );
    }
    let store = crate::graph::Store::new_persistent(&db_path)
        .with_context(|| format!("Failed to open graph at {}", db_path.display()))?;
    let result = crate::ops::callers(&store, node_id, depth)?;
    if json {
        println!("{}", serde_json::to_string_pretty(&result)?);
    } else {
        for node in &result.callers {
            let payload = node.payload.as_deref().unwrap_or("");
            println!("{}\t{}\t{payload}", node.id, node.node_type);
        }
        println!("\n{} callers of {}", result.count, node_id);
    }
    Ok(())
}

fn run_info(db: Option<&PathBuf>, node_id: &str, json: bool) -> Result<()> {
    let db_path = resolve_db_path(db)?;
    if !db_path.exists() {
        anyhow::bail!(
            "Graph database not found at {}. Run 'ferrograph index --output {}' first.",
            db_path.display(),
            db_path.display()
        );
    }
    let store = crate::graph::Store::new_persistent(&db_path)
        .with_context(|| format!("Failed to open graph at {}", db_path.display()))?;
    let info = crate::ops::node_info(&store, node_id)?;
    match info {
        Some(n) => {
            if json {
                println!("{}", serde_json::to_string_pretty(&n)?);
            } else {
                let payload = n.payload.as_deref().unwrap_or("");
                println!("{}\t{}\t{payload}", n.id, n.node_type);
                if !n.outgoing_edges.is_empty() {
                    println!("\n  Outgoing edges:");
                    for e in &n.outgoing_edges {
                        let p = e.payload.as_deref().unwrap_or("");
                        println!("    --[{}]--> {}\t{}\t{p}", e.edge_type, e.id, e.node_type);
                    }
                }
                if !n.incoming_edges.is_empty() {
                    println!("\n  Incoming edges:");
                    for e in &n.incoming_edges {
                        let p = e.payload.as_deref().unwrap_or("");
                        println!("    <--[{}]-- {}\t{}\t{p}", e.edge_type, e.id, e.node_type);
                    }
                }
            }
        }
        None => {
            if json {
                println!(
                    "{}",
                    serde_json::to_string_pretty(&serde_json::json!({
                        "error": "Node not found",
                        "node_id": node_id
                    }))?
                );
            } else {
                println!("Node not found: {node_id}");
            }
        }
    }
    Ok(())
}

fn run_modules(db: Option<&PathBuf>, root: Option<&str>, json: bool) -> Result<()> {
    let db_path = resolve_db_path(db)?;
    if !db_path.exists() {
        anyhow::bail!(
            "Graph database not found at {}. Run 'ferrograph index --output {}' first.",
            db_path.display(),
            db_path.display()
        );
    }
    let store = crate::graph::Store::new_persistent(&db_path)
        .with_context(|| format!("Failed to open graph at {}", db_path.display()))?;
    let result = crate::ops::module_graph(&store, root)?;
    if json {
        println!("{}", serde_json::to_string_pretty(&result)?);
    } else {
        for edge in &result.edges {
            println!(
                "{} ({}) --> {} ({})",
                edge.from_id, edge.from_type, edge.to_id, edge.to_type
            );
        }
        println!("\n{} containment edges", result.count);
    }
    Ok(())
}

fn run_traits(db: Option<&PathBuf>, trait_name: &str, json: bool) -> Result<()> {
    let db_path = resolve_db_path(db)?;
    if !db_path.exists() {
        anyhow::bail!(
            "Graph database not found at {}. Run 'ferrograph index --output {}' first.",
            db_path.display(),
            db_path.display()
        );
    }
    let store = crate::graph::Store::new_persistent(&db_path)
        .with_context(|| format!("Failed to open graph at {}", db_path.display()))?;
    let result = crate::ops::trait_implementors(&store, trait_name)?;
    if json {
        println!("{}", serde_json::to_string_pretty(&result)?);
    } else {
        for node in &result.implementors {
            let payload = node.payload.as_deref().unwrap_or("");
            println!("{}\t{}\t{payload}", node.id, node.node_type);
        }
        println!("\n{} implementors of \"{}\"", result.count, trait_name);
    }
    Ok(())
}

fn run_status(path: &Path, json: bool) -> Result<()> {
    let db_path = if path.is_dir() {
        path.join(".ferrograph")
    } else {
        path.to_path_buf()
    };
    if !db_path.exists() {
        println!(
            "No graph at {}. Run 'ferrograph index --output {}' first.",
            path.display(),
            db_path.display()
        );
        return Ok(());
    }
    let store = crate::graph::Store::new_persistent(&db_path)
        .with_context(|| format!("Failed to open graph at {}", db_path.display()))?;
    let result = crate::ops::status(&store, &db_path)?;
    if json {
        println!("{}", serde_json::to_string_pretty(&result)?);
    } else {
        println!("Graph: {}", result.db_path);
        if let Some(ts) = result.indexed_at {
            println!("  indexed_at: {ts}");
        }
        println!();
        println!("  Nodes ({} total):", result.node_count);
        for (type_name, count) in &result.nodes_by_type {
            println!("    {type_name:<20} {count:>6}");
        }
        println!();
        println!("  Edges ({} total):", result.edge_count);
        for (type_name, count) in &result.edges_by_type {
            println!("    {type_name:<20} {count:>6}");
        }
    }
    Ok(())
}