magellan 3.3.1

Deterministic codebase mapping tool for local development
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! Query command implementation
//!
//! Lists symbols in a file, optionally filtered by kind.

use anyhow::{Context, Result};
use magellan::backend_router::{BackendType, MagellanBackend};
use magellan::common::{
    detect_language_from_path, format_symbol_kind, parse_symbol_kind, resolve_path,
};
use magellan::output::rich::{SpanChecksums, SpanContext};
use magellan::output::{
    output_json, CalleeInfo, CallerInfo, JsonResponse, OutputFormat, QueryResponse, Span,
    SymbolMatch,
};
use magellan::{CodeGraph, SymbolFact};
use std::path::PathBuf;

const QUERY_EXPLAIN_TEXT: &str = r#"Query Selector Cheatsheet
--------------------------------
Selectors:
Required selectors:
  --file <path>            Absolute or root-relative path to inspect.

Optional filters:
  --kind <kind>            function|method|struct|trait|enum|mod|type_alias|union|namespace.
  --symbol <name>          Limit output to a specific symbol (case-sensitive).
  --show-extent            With --symbol, print byte + line/column ranges.

Related helpers:
  magellan refs --name <symbol> --path <file>      Show incoming/outgoing references.
  magellan find --name <symbol>                    Locate symbol across files.
  magellan find --list-glob \"test_*\"             Preview glob sets before bulk edits.

Examples:
  magellan query --db mag.db --file src/main.rs --kind function
  magellan query --db mag.db --file src/lib.rs --symbol main --show-extent
  magellan find  --db mag.db --list-glob \"handler_*\""#;

pub fn run_query(
    db_path: PathBuf,
    file_path: Option<PathBuf>,
    root: Option<PathBuf>,
    kind_str: Option<String>,
    explain: bool,
    symbol: Option<String>,
    show_extent: bool,
    output_format: OutputFormat,
    with_context: bool,
    with_callers: bool,
    with_callees: bool,
    with_semantics: bool,
    with_checksums: bool,
    context_lines: usize,
) -> Result<()> {
    // Build args for execution tracking
    let mut args = vec!["query".to_string()];
    if let Some(ref fp) = file_path {
        args.push("--file".to_string());
        args.push(fp.to_string_lossy().to_string());
    }
    if let Some(ref root_path) = root {
        args.push("--root".to_string());
        args.push(root_path.to_string_lossy().to_string());
    }
    if let Some(ref kind) = kind_str {
        args.push("--kind".to_string());
        args.push(kind.clone());
    }
    if explain {
        args.push("--explain".to_string());
    }
    if let Some(ref sym) = symbol {
        args.push("--symbol".to_string());
        args.push(sym.clone());
    }
    if show_extent {
        args.push("--show-extent".to_string());
    }

    // Check if this is a geometric database and route accordingly
    if MagellanBackend::detect_type(&db_path) == BackendType::Geometric {
        return run_query_geometric(
            &db_path,
            file_path.as_deref().and_then(|p| p.to_str()),
            symbol.as_deref(),
            explain,
            show_extent,
        );
    }

    let mut graph = CodeGraph::open(&db_path)?;
    let exec_id = magellan::output::generate_execution_id();
    let root_str = root.as_ref().map(|p| p.to_string_lossy().to_string());
    let db_path_str = db_path.to_string_lossy().to_string();

    graph.execution_log().start_execution(
        &exec_id,
        env!("CARGO_PKG_VERSION"),
        &args,
        root_str.as_deref(),
        &db_path_str,
    )?;

    if explain {
        println!("{}", QUERY_EXPLAIN_TEXT);
        let _ = graph
            .execution_log()
            .finish_execution(&exec_id, "success", None, 0, 0, 0);
        return Ok(());
    }

    if show_extent && symbol.is_none() {
        let err_msg = "--show-extent requires --symbol <name>".to_string();
        let _ = graph
            .execution_log()
            .finish_execution(&exec_id, "error", Some(&err_msg), 0, 0, 0);
        anyhow::bail!(err_msg);
    }

    // Parse kind filter if provided
    let kind_filter = match kind_str {
        Some(ref s) => match parse_symbol_kind(s) {
            Some(k) => Some(k),
            None => {
                let err_msg = format!("Unknown symbol kind: '{}'. Valid kinds: function, method, class, interface, enum, module, union, namespace, typealias", s);
                let _ = graph.execution_log().finish_execution(
                    &exec_id,
                    "error",
                    Some(&err_msg),
                    0,
                    0,
                    0,
                );
                anyhow::bail!(err_msg);
            }
        },
        None => None,
    };

    let file_path = match file_path {
        Some(fp) => fp,
        None => {
            let err_msg = "--file is required unless --explain is used".to_string();
            let _ =
                graph
                    .execution_log()
                    .finish_execution(&exec_id, "error", Some(&err_msg), 0, 0, 0);
            anyhow::bail!(err_msg);
        }
    };

    let path_str = resolve_path(&file_path, &root);

    // Handle JSON output mode - use symbol_nodes_in_file_with_ids for symbol_id propagation
    if output_format == OutputFormat::Json || output_format == OutputFormat::Pretty {
        let mut symbols_with_ids =
            magellan::graph::query::symbol_nodes_in_file_with_ids(&mut graph, &path_str)?;

        // Apply kind filter
        if let Some(ref filter_kind) = kind_filter {
            symbols_with_ids.retain(|(_, fact, _)| fact.kind == *filter_kind);
        }

        // Apply symbol name filter
        if let Some(ref symbol_name) = symbol {
            symbols_with_ids
                .retain(|(_, fact, _)| fact.name.as_deref() == Some(symbol_name.as_str()));
        }

        let symbols_with_ids: Vec<(SymbolFact, Option<String>)> = symbols_with_ids
            .into_iter()
            .map(|(_, fact, symbol_id)| (fact, symbol_id))
            .collect();

        let _ = graph
            .execution_log()
            .finish_execution(&exec_id, "success", None, 0, 0, 0);
        return output_json_mode(
            &path_str,
            symbols_with_ids,
            kind_str,
            show_extent,
            &symbol,
            &mut graph,
            &exec_id,
            output_format,
            with_context,
            with_callers,
            with_callees,
            with_semantics,
            with_checksums,
            context_lines,
        );
    }

    // Human mode - use existing flow
    let mut symbols = graph.symbols_in_file_with_kind(&path_str, kind_filter)?;

    if let Some(ref symbol_name) = symbol {
        symbols.retain(|s| s.name.as_deref() == Some(symbol_name.as_str()));
    }

    // Human mode (existing behavior)
    println!("{}:", path_str);

    if symbols.is_empty() {
        println!("  (no symbols found)");
        match symbol {
            Some(ref sym) => println!(
                "  Hint: verify the symbol name or run `magellan find --list-glob \"{}\"`.",
                sym
            ),
            None => println!("  Hint: run `magellan query --explain` for selector syntax."),
        }
        let _ = graph
            .execution_log()
            .finish_execution(&exec_id, "success", None, 0, 0, 0);
        return Ok(());
    }

    for symbol in &symbols {
        let kind_str = format_symbol_kind(&symbol.kind);
        let name = symbol.name.as_deref().unwrap_or("(unnamed)");
        println!(
            "  Line {:4}: {:12} {:<} [{}]",
            symbol.start_line, kind_str, name, symbol.kind_normalized
        );

        // Show callers if requested
        if with_callers {
            let symbol_path = symbol.file_path.to_string_lossy().to_string();
            if let Ok(callers) = graph.callers_of_symbol(&symbol_path, name) {
                if !callers.is_empty() {
                    println!("    Called from:");
                    for caller in &callers {
                        println!(
                            "      {} at {}:{}",
                            caller.caller,
                            caller.file_path.display(),
                            caller.start_line
                        );
                    }
                }
            }
        }

        // Show callees if requested
        if with_callees {
            let symbol_path = symbol.file_path.to_string_lossy().to_string();
            if let Ok(callees) = graph.calls_from_symbol(&symbol_path, name) {
                if !callees.is_empty() {
                    println!("    Calls:");
                    for callee in &callees {
                        println!("      {} at {}", callee.callee, callee.file_path.display());
                    }
                }
            }
        }
    }

    if show_extent {
        if let Some(ref symbol_name) = symbol {
            let mut extents = graph.symbol_extents(&path_str, symbol_name)?;
            if extents.is_empty() {
                println!("  (no extent info found for '{}')", symbol_name);
                let _ = graph
                    .execution_log()
                    .finish_execution(&exec_id, "success", None, 0, 0, 0);
                return Ok(());
            }
            println!();
            println!("Symbol Extents for '{}':", symbol_name);
            extents.sort_by(|(_, a), (_, b)| {
                a.start_line
                    .cmp(&b.start_line)
                    .then_with(|| a.start_col.cmp(&b.start_col))
            });
            for (node_id, fact) in extents {
                print_extent_block(node_id, &fact);
            }
        }
    }

    let _ = graph
        .execution_log()
        .finish_execution(&exec_id, "success", None, 0, 0, 0);
    Ok(())
}

/// Output query results in JSON format
fn output_json_mode(
    path_str: &str,
    mut symbols_with_ids: Vec<(SymbolFact, Option<String>)>,
    kind_str: Option<String>,
    _show_extent: bool,
    _symbol: &Option<String>,
    graph: &mut CodeGraph,
    exec_id: &str,
    output_format: OutputFormat,
    with_context: bool,
    with_callers: bool,
    with_callees: bool,
    with_semantics: bool,
    with_checksums: bool,
    context_lines: usize,
) -> Result<()> {
    // Sort deterministically: by file_path, start_line, start_col, name
    symbols_with_ids.sort_by(|(a, _), (b, _)| {
        a.file_path
            .cmp(&b.file_path)
            .then_with(|| a.start_line.cmp(&b.start_line))
            .then_with(|| a.start_col.cmp(&b.start_col))
            .then_with(|| a.name.as_deref().cmp(&b.name.as_deref()))
    });

    // Convert (SymbolFact, Option<symbol_id>) to SymbolMatch with rich span data
    let symbol_matches: Vec<SymbolMatch> = symbols_with_ids
        .into_iter()
        .map(|(s, symbol_id)| {
            let file_path = s.file_path.to_string_lossy().to_string();
            let mut span = Span::new(
                file_path.clone(),
                s.byte_start,
                s.byte_end,
                s.start_line,
                s.start_col,
                s.end_line,
                s.end_col,
            );

            // Add context if requested
            if with_context {
                if let Some(context) =
                    SpanContext::extract(&file_path, s.start_line, s.end_line, context_lines)
                {
                    span = span.with_context(context);
                }
            }

            // Add semantics if requested
            if with_semantics {
                let language = detect_language_from_path(&file_path);
                span = span.with_semantics_from(s.kind_normalized.clone(), language);
            }

            // Add checksums if requested
            if with_checksums {
                let checksums = SpanChecksums::compute(&file_path, s.byte_start, s.byte_end);
                span = span.with_checksums(checksums);
            }

            let symbol_name = s.name.clone().unwrap_or_else(|| "(unnamed)".to_string());

            // Build caller information if requested
            let callers_info = if with_callers {
                if let Ok(call_facts) = graph.callers_of_symbol(&file_path, &symbol_name) {
                    let mut callers: Vec<CallerInfo> = call_facts
                        .into_iter()
                        .map(|call| CallerInfo {
                            name: call.caller,
                            file_path: call.file_path.to_string_lossy().to_string(),
                            line: call.start_line,
                            column: call.start_col,
                        })
                        .collect();
                    // Sort deterministically
                    callers.sort_by(|a, b| {
                        a.file_path
                            .cmp(&b.file_path)
                            .then_with(|| a.line.cmp(&b.line))
                    });
                    Some(callers)
                } else {
                    None
                }
            } else {
                None
            };

            // Build callee information if requested
            let callees_info = if with_callees {
                if let Ok(call_facts) = graph.calls_from_symbol(&file_path, &symbol_name) {
                    let mut callees: Vec<CalleeInfo> = call_facts
                        .into_iter()
                        .map(|call| CalleeInfo {
                            name: call.callee,
                            file_path: call.file_path.to_string_lossy().to_string(),
                        })
                        .collect();
                    // Sort deterministically
                    callees.sort_by(|a, b| {
                        a.file_path
                            .cmp(&b.file_path)
                            .then_with(|| a.name.cmp(&b.name))
                    });
                    Some(callees)
                } else {
                    None
                }
            } else {
                None
            };

            SymbolMatch::new(
                symbol_name,
                s.kind_normalized,
                span,
                None,      // parent not tracked yet
                symbol_id, // stable symbol ID from graph
            )
            .with_callers_and_callees(callers_info, callees_info)
        })
        .collect();

    let response = QueryResponse {
        symbols: symbol_matches,
        file_path: path_str.to_string(),
        kind_filter: kind_str,
    };

    let json_response = JsonResponse::new(response, exec_id);
    output_json(&json_response, output_format)?;

    Ok(())
}

fn print_extent_block(node_id: i64, symbol: &magellan::SymbolFact) {
    let name = symbol.name.as_deref().unwrap_or("(unnamed)");
    println!("  Node ID: {}", node_id);
    println!(
        "    {} [{}] at {}",
        name,
        symbol.kind_normalized,
        symbol.file_path.to_string_lossy()
    );
    println!("    Byte Range: {}..{}", symbol.byte_start, symbol.byte_end);
    println!(
        "    Line Range: {}:{} -> {}:{}",
        symbol.start_line, symbol.start_col, symbol.end_line, symbol.end_col
    );
}

/// Query command for geometric backend databases
fn run_query_geometric(
    db_path: &std::path::Path,
    file_path: Option<&str>,
    symbol: Option<&str>,
    explain: bool,
    show_extent: bool,
) -> Result<()> {
    let backend = MagellanBackend::open(db_path)?;

    if explain {
        println!("{}", QUERY_EXPLAIN_TEXT);
        return Ok(());
    }

    if show_extent && symbol.is_none() {
        anyhow::bail!("--show-extent requires --symbol <name>");
    }

    // Get stats to show available symbols
    let stats = backend.get_stats()?;
    println!("Geometric Database Statistics:");
    println!("  Symbols: {}", stats.symbol_count);
    println!("  CFG Blocks: {}", stats.cfg_block_count);

    if let Some(sym_name) = symbol {
        // Look up specific symbol
        match backend.find_symbol_by_fqn(sym_name) {
            Ok(Some(info)) => {
                println!("\nFound symbol: {}", info.fqn);
                println!("  Name:     {}", info.name);
                println!("  Kind:     {:?}", info.kind);
                println!("  File:     {}", info.file_path);
                println!(
                    "  Location: Line {}, Column {}",
                    info.start_line, info.start_col
                );
                Ok(())
            }
            Ok(None) => {
                println!("\nSymbol '{}' not found", sym_name);
                Ok(())
            }
            Err(e) => {
                eprintln!("Error: {}", e);
                Err(e)
            }
        }?;
    } else if let Some(fp) = file_path {
        println!("\nQuerying file: {}", fp);

        // Query symbols in file using the backend
        match backend.symbols_in_file(fp) {
            Ok(symbols) => {
                if symbols.is_empty() {
                    println!("No symbols found in file");
                } else {
                    println!("Found {} symbol(s):\n", symbols.len());
                    for (i, info) in symbols.iter().enumerate() {
                        println!("  [{}] {}", i + 1, info.fqn);
                        println!("      Kind: {:?}", info.kind);
                        println!("      Lines: {}-{}", info.start_line, info.end_line);
                        println!();
                    }
                }
                Ok(())
            }
            Err(e) => Err(e).context("Error querying symbols in file"),
        }?;
    } else {
        println!("\nUse --symbol <fqn> to query specific symbols");
        println!("Use --file <path> to query by file");
    }

    Ok(())
}