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
//! Multi-Database Context for Cross-Project Queries
//!
//! Provides a unified query interface over multiple project databases.
//! Each database is treated as an isolated project; cross-project edges
//! are runtime unions, not stored relationships.

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

use anyhow::Result;

use crate::context::{
    affected_analysis, get_file_context, get_project_summary, get_symbol_detail,
    get_symbol_detail_recursive, impact_analysis, list_symbols, FileContext, ListQuery,
    PaginatedResult, ProjectSummary, SymbolDetail, SymbolListItem, SymbolRelation,
};
use crate::graph::CodeGraph;
use crate::output::{ProjectCalleeInfo, ProjectCallerInfo, ProjectSymbolMatch, Span};

/// A single graph connection with its project name
struct ProjectGraph {
    name: String,
    graph: CodeGraph,
}

/// Multi-database context for querying across project boundaries
pub struct MultiDbContext {
    projects: Vec<ProjectGraph>,
}

impl MultiDbContext {
    /// Open multiple databases from explicit paths
    pub fn from_paths(paths: &[PathBuf]) -> Result<Self> {
        let mut projects = Vec::with_capacity(paths.len());
        for path in paths {
            let name = project_name_from_path(path);
            match CodeGraph::open(path) {
                Ok(graph) => projects.push(ProjectGraph { name, graph }),
                Err(e) => eprintln!("Warning: skipping {}: {}", path.display(), e),
            }
        }
        Ok(Self { projects })
    }

    /// Discover databases in a directory
    pub fn from_directory(dir: &Path) -> Result<Self> {
        let mut paths = Vec::new();
        for entry in std::fs::read_dir(dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_some_and(|e| e == "db") {
                paths.push(path);
            }
        }
        paths.sort();
        Self::from_paths(&paths)
    }

    /// Number of successfully opened projects
    pub fn project_count(&self) -> usize {
        self.projects.len()
    }

    /// Project names in order
    pub fn project_names(&self) -> Vec<String> {
        self.projects.iter().map(|p| p.name.clone()).collect()
    }

    /// Get summaries for all projects
    pub fn summaries(&mut self) -> Vec<(String, ProjectSummary)> {
        let mut results = Vec::with_capacity(self.projects.len());
        for project in &mut self.projects {
            match get_project_summary(&mut project.graph) {
                Ok(summary) => results.push((project.name.clone(), summary)),
                Err(e) => eprintln!("Warning: summary failed for {}: {}", project.name, e),
            }
        }
        results
    }

    /// Search for a symbol by name across all projects
    pub fn search_symbol(
        &mut self,
        name: &str,
        file: Option<&str>,
        depth: Option<usize>,
        include_callers: bool,
        include_callees: bool,
    ) -> Vec<ProjectSymbolMatch> {
        let mut results = Vec::new();
        let mut seen: HashSet<String> = HashSet::new();

        for project in &mut self.projects {
            let detail = match depth {
                Some(d) if d > 1 => get_symbol_detail_recursive(&mut project.graph, name, file, d),
                _ => get_symbol_detail(&mut project.graph, name, file),
            };

            let detail = match detail {
                Ok(d) => d,
                Err(_) => continue,
            };

            let key = format!("{}:{}:{}", project.name, detail.file, detail.line);
            if !seen.insert(key) {
                continue;
            }

            let span = Span {
                span_id: crate::output::Span::generate_id(
                    &detail.file,
                    detail.byte_start,
                    detail.byte_end,
                ),
                file_path: detail.file.clone(),
                byte_start: detail.byte_start,
                byte_end: detail.byte_end,
                start_line: detail.line,
                start_col: detail.start_col,
                end_line: detail.end_line,
                end_col: detail.end_col,
                context: None,
                semantics: None,
                relationships: None,
                checksums: None,
            };

            let callers = if include_callers {
                Some(
                    detail
                        .callers
                        .iter()
                        .map(|c| ProjectCallerInfo {
                            project: project.name.clone(),
                            name: c.name.clone(),
                            file_path: c.file.clone(),
                            line: c.line,
                            column: 0,
                            depth: c.depth,
                        })
                        .collect(),
                )
            } else {
                None
            };

            let callees = if include_callees {
                Some(
                    detail
                        .callees
                        .iter()
                        .map(|c| ProjectCalleeInfo {
                            project: project.name.clone(),
                            name: c.name.clone(),
                            file_path: c.file.clone(),
                            line: c.line as u32,
                            depth: c.depth,
                        })
                        .collect(),
                )
            } else {
                None
            };

            results.push(ProjectSymbolMatch {
                project: project.name.clone(),
                match_id: format!("{}::{}#{}", project.name, name, detail.line),
                span,
                name: name.to_string(),
                kind: detail.kind,
                parent: None,
                symbol_id: Some(format!("{}::{}#{}", project.name, name, detail.line)),
                callers,
                callees,
                source: None,
            });
        }

        results
    }

    /// List symbols across all projects with pagination
    pub fn list_symbols(&mut self, query: &ListQuery) -> Vec<(String, SymbolListItem)> {
        let mut all_items = Vec::new();
        let mut seen: HashSet<String> = HashSet::new();

        for project in &mut self.projects {
            let result = match list_symbols(&mut project.graph, query) {
                Ok(r) => r,
                Err(_) => continue,
            };

            for item in result.items {
                let key = format!("{}:{}:{}:{}", project.name, item.name, item.file, item.line);
                if seen.insert(key) {
                    all_items.push((project.name.clone(), item));
                }
            }
        }

        all_items
    }

    /// Impact analysis: all symbols that transitively call the target
    pub fn impact(
        &mut self,
        name: &str,
        file: Option<&str>,
        max_depth: usize,
    ) -> Vec<(String, SymbolRelation)> {
        let mut all_impacted = Vec::new();
        let mut seen: HashSet<String> = HashSet::new();

        for project in &mut self.projects {
            let impacted = match impact_analysis(&mut project.graph, name, file, max_depth) {
                Ok(i) => i,
                Err(_) => continue,
            };

            for relation in impacted {
                let key = format!("{}:{}:{}", project.name, relation.name, relation.file);
                if seen.insert(key) {
                    all_impacted.push((project.name.clone(), relation));
                }
            }
        }

        all_impacted
    }

    /// Affected analysis: all symbols that the target transitively calls
    pub fn affected(
        &mut self,
        name: &str,
        file: Option<&str>,
        max_depth: usize,
    ) -> Vec<(String, SymbolRelation)> {
        let mut all_affected = Vec::new();
        let mut seen: HashSet<String> = HashSet::new();

        for project in &mut self.projects {
            let affected = match affected_analysis(&mut project.graph, name, file, max_depth) {
                Ok(a) => a,
                Err(_) => continue,
            };

            for relation in affected {
                let key = format!("{}:{}:{}", project.name, relation.name, relation.file);
                if seen.insert(key) {
                    all_affected.push((project.name.clone(), relation));
                }
            }
        }

        all_affected
    }

    /// File context across all projects
    pub fn file_context(&mut self, file_path: &str) -> Vec<(String, FileContext)> {
        let mut results = Vec::new();
        for project in &mut self.projects {
            match get_file_context(&mut project.graph, file_path) {
                Ok(ctx) => results.push((project.name.clone(), ctx)),
                Err(_) => continue,
            }
        }
        results
    }
}

/// Extract project name from a database file path
fn project_name_from_path(path: &Path) -> String {
    path.file_stem()
        .map(|s| s.to_string_lossy().to_string())
        .unwrap_or_else(|| "unknown".to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    /// Create a test DB with some indexed symbols and return its path
    fn create_test_db(dir: &std::path::Path, name: &str) -> PathBuf {
        let db_path = dir.join(format!("{}.db", name));
        let mut graph = CodeGraph::open(&db_path).unwrap();

        let source = r#"fn greet() { hello() }
fn hello() { println!("hi") }
struct AppConfig { name: String }
impl AppConfig { fn new() -> Self { Self { name: String::new() } } }"#;

        graph.index_file("src/lib.rs", source.as_bytes()).unwrap();
        db_path
    }

    /// Create a test DB with a different symbol set
    fn create_test_db_b(dir: &std::path::Path, name: &str) -> PathBuf {
        let db_path = dir.join(format!("{}.db", name));
        let mut graph = CodeGraph::open(&db_path).unwrap();

        let source = r#"fn process(data: &str) { parse(data) }
fn parse(data: &str) { data.len() }
fn greet() { println!("hello from project_b") }"#;

        graph.index_file("src/main.rs", source.as_bytes()).unwrap();
        db_path
    }

    // ── from_paths ──

    #[test]
    fn test_from_paths_opens_multiple_dbs() {
        let dir = tempdir().unwrap();
        let p1 = create_test_db(dir.path(), "project_a");
        let p2 = create_test_db_b(dir.path(), "project_b");

        let ctx = MultiDbContext::from_paths(&[p1, p2]).unwrap();
        assert_eq!(ctx.project_count(), 2);
        assert_eq!(ctx.project_names(), vec!["project_a", "project_b"]);
    }

    #[test]
    fn test_from_paths_skips_bad_db() {
        let dir = tempdir().unwrap();
        let good = create_test_db(dir.path(), "good_project");
        let bad = dir.path().join("not_a_db.txt");
        fs::write(&bad, "not a database").unwrap();

        let ctx = MultiDbContext::from_paths(&[good, bad]).unwrap();
        assert_eq!(ctx.project_count(), 1);
        assert_eq!(ctx.project_names(), vec!["good_project"]);
    }

    #[test]
    fn test_from_paths_empty_input() {
        let ctx = MultiDbContext::from_paths(&[]).unwrap();
        assert_eq!(ctx.project_count(), 0);
    }

    #[test]
    fn test_from_paths_all_bad() {
        let dir = tempdir().unwrap();
        let bad1 = dir.path().join("bad1.txt");
        let bad2 = dir.path().join("bad2.txt");
        fs::write(&bad1, "garbage").unwrap();
        fs::write(&bad2, "also garbage").unwrap();

        let ctx = MultiDbContext::from_paths(&[bad1, bad2]).unwrap();
        assert_eq!(ctx.project_count(), 0);
    }

    // ── from_directory ──

    #[test]
    fn test_from_directory_discovers_dbs() {
        let dir = tempdir().unwrap();
        create_test_db(dir.path(), "alpha");
        create_test_db_b(dir.path(), "beta");
        // Non-.db file should be ignored
        fs::write(dir.path().join("README.md"), "not a db").unwrap();

        let ctx = MultiDbContext::from_directory(dir.path()).unwrap();
        assert_eq!(ctx.project_count(), 2);
        // Sorted alphabetically: alpha < beta
        assert_eq!(ctx.project_names(), vec!["alpha", "beta"]);
    }

    #[test]
    fn test_from_directory_empty_directory() {
        let dir = tempdir().unwrap();
        let ctx = MultiDbContext::from_directory(dir.path()).unwrap();
        assert_eq!(ctx.project_count(), 0);
    }

    #[test]
    fn test_from_directory_missing_directory() {
        let result = MultiDbContext::from_directory(Path::new("/tmp/nonexistent_dir_hermes_test"));
        assert!(result.is_err());
    }

    // ── search_symbol ──

    #[test]
    fn test_search_symbol_cross_project() {
        let dir = tempdir().unwrap();
        let p1 = create_test_db(dir.path(), "proj_a");
        let p2 = create_test_db_b(dir.path(), "proj_b");

        let mut ctx = MultiDbContext::from_paths(&[p1, p2]).unwrap();
        // "greet" exists in both project_a and project_b
        let results = ctx.search_symbol("greet", None, None, false, false);
        assert_eq!(results.len(), 2);
        let projects: Vec<&str> = results.iter().map(|r| r.project.as_str()).collect();
        assert!(projects.contains(&"proj_a"));
        assert!(projects.contains(&"proj_b"));
    }

    #[test]
    fn test_search_symbol_single_project() {
        let dir = tempdir().unwrap();
        let p1 = create_test_db(dir.path(), "proj_a");
        let p2 = create_test_db_b(dir.path(), "proj_b");

        let mut ctx = MultiDbContext::from_paths(&[p1, p2]).unwrap();
        // "process" only exists in project_b
        let results = ctx.search_symbol("process", None, None, false, false);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].project, "proj_b");
    }

    #[test]
    fn test_search_symbol_nonexistent() {
        let dir = tempdir().unwrap();
        let p1 = create_test_db(dir.path(), "proj_a");

        let mut ctx = MultiDbContext::from_paths(&[p1]).unwrap();
        let results = ctx.search_symbol("nonexistent_function_xyz", None, None, false, false);
        assert!(results.is_empty());
    }

    // ── list_symbols ──

    #[test]
    fn test_list_symbols_deduplication() {
        let dir = tempdir().unwrap();
        let p1 = create_test_db(dir.path(), "proj_a");
        let p2 = create_test_db_b(dir.path(), "proj_b");

        let mut ctx = MultiDbContext::from_paths(&[p1, p2]).unwrap();
        let query = ListQuery {
            kind: Some("fn".to_string()),
            file_pattern: None,
            page: Some(1),
            page_size: Some(100),
            cursor: None,
        };
        let items = ctx.list_symbols(&query);

        // "greet" appears in both projects but should appear twice
        // (different projects, different files — dedup key includes project+file+line)
        let greets: Vec<&str> = items
            .iter()
            .filter(|(_, i)| i.name == "greet")
            .map(|(p, _)| p.as_str())
            .collect();
        assert_eq!(greets.len(), 2);
    }

    // ── impact ──

    #[test]
    fn test_impact_stays_within_project() {
        let dir = tempdir().unwrap();
        let p1 = create_test_db(dir.path(), "proj_a");
        let p2 = create_test_db_b(dir.path(), "proj_b");

        let mut ctx = MultiDbContext::from_paths(&[p1, p2]).unwrap();
        // "hello" is only called by "greet" in proj_a
        let impacted = ctx.impact("hello", None, 3);
        // All results should be from proj_a only
        for (project, _) in &impacted {
            assert_eq!(project, "proj_a");
        }
    }

    // ── affected ──

    #[test]
    fn test_affected_stays_within_project() {
        let dir = tempdir().unwrap();
        let p1 = create_test_db(dir.path(), "proj_a");
        let p2 = create_test_db_b(dir.path(), "proj_b");

        let mut ctx = MultiDbContext::from_paths(&[p1, p2]).unwrap();
        // "greet" in proj_a calls "hello" — affected should only return proj_a symbols
        let affected = ctx.affected("greet", None, 3);
        for (project, _) in &affected {
            assert_eq!(project, "proj_a");
        }
    }

    // ── summaries ──

    #[test]
    fn test_summaries_returns_all_projects() {
        let dir = tempdir().unwrap();
        let p1 = create_test_db(dir.path(), "alpha");
        let p2 = create_test_db_b(dir.path(), "beta");

        let mut ctx = MultiDbContext::from_paths(&[p1, p2]).unwrap();
        let summaries = ctx.summaries();
        assert_eq!(summaries.len(), 2);
        let names: Vec<&str> = summaries.iter().map(|(n, _)| n.as_str()).collect();
        assert!(names.contains(&"alpha"));
        assert!(names.contains(&"beta"));
    }

    // ── project_name_from_path ──

    #[test]
    fn test_project_name_extraction() {
        assert_eq!(
            project_name_from_path(Path::new("/some/path/magellan.db")),
            "magellan"
        );
        assert_eq!(project_name_from_path(Path::new("splice.db")), "splice");
        // No extension but has a stem — returns stem
        assert_eq!(project_name_from_path(Path::new("/noext")), "noext");
    }
}