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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//! Refresh command implementation for Magellan
//!
//! Synchronizes the graph database with the current git working tree state.
//! Detects modified, deleted, and new files, then updates the database accordingly.

use anyhow::{Context, Result};
use git2::{Repository, StatusOptions};
use magellan::output::{generate_execution_id, output_json, JsonResponse, OutputFormat};
use magellan::{CodeGraph, ReconcileOutcome};
use serde::Serialize;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::time::Instant;

/// Arguments for the refresh command
#[derive(Debug, Clone)]
pub struct RefreshArgs {
    /// Path to the database file
    pub db_path: PathBuf,
    /// If true, only preview changes without applying them
    pub dry_run: bool,
    /// If true, include untracked files in the refresh
    pub include_untracked: bool,
    /// If true, only process staged changes
    pub staged: bool,
    /// If true, only process unstaged changes
    pub unstaged: bool,
    #[allow(dead_code, reason = "TODO: wire up force refresh logic")]
    /// If true, force refresh even if no changes detected
    pub force: bool,
    /// Output format (Human, Json, or Pretty)
    pub output_format: OutputFormat,
}

impl Default for RefreshArgs {
    fn default() -> Self {
        Self {
            db_path: PathBuf::from(".magellan/magellan.db"),
            dry_run: false,
            include_untracked: false,
            staged: false,
            unstaged: false,
            force: false,
            output_format: OutputFormat::Human,
        }
    }
}

/// Report of refresh operation results
#[derive(Debug, Clone, Serialize)]
pub struct RefreshReport {
    /// Files that were updated (modified and re-indexed)
    pub updated: Vec<String>,
    /// Files that were deleted from the database
    pub deleted: Vec<String>,
    /// Files that were added to the database
    pub added: Vec<String>,
    /// Number of files that were unchanged
    pub unchanged: usize,
    /// Whether this was a dry run
    pub dry_run: bool,
    /// Duration of the operation in milliseconds
    pub duration_ms: u64,
}

impl RefreshReport {
    /// Create a new empty report
    pub fn new() -> Self {
        Self {
            updated: Vec::new(),
            deleted: Vec::new(),
            added: Vec::new(),
            unchanged: 0,
            dry_run: false,
            duration_ms: 0,
        }
    }

    #[allow(dead_code, reason = "used in tests, future public API")]
    /// Total number of changes (updated + deleted + added)
    pub fn total_changes(&self) -> usize {
        self.updated.len() + self.deleted.len() + self.added.len()
    }
}

impl Default for RefreshReport {
    fn default() -> Self {
        Self::new()
    }
}

/// Response structure for JSON output
#[derive(Debug, Clone, Serialize)]
struct RefreshResponse {
    updated: Vec<String>,
    deleted: Vec<String>,
    added: Vec<String>,
    unchanged: usize,
    duration_ms: u64,
    dry_run: bool,
}

impl RefreshResponse {
    fn from_report(report: &RefreshReport, dry_run: bool) -> Self {
        Self {
            updated: report.updated.clone(),
            deleted: report.deleted.clone(),
            added: report.added.clone(),
            unchanged: report.unchanged,
            duration_ms: report.duration_ms,
            dry_run,
        }
    }
}

/// Run the refresh command
///
/// Synchronizes the graph database with the current git working tree state.
///
/// # Arguments
/// * `args` - Refresh command arguments
///
/// # Returns
/// Result containing the refresh report or an error
pub fn run_refresh(args: &RefreshArgs) -> Result<RefreshReport> {
    let start_time = Instant::now();
    let exec_id = generate_execution_id();

    // Open the git repository
    let repo = Repository::open(".")
        .context("Failed to open git repository. Are you in a git repository?")?;

    // Open the graph database
    let mut graph = CodeGraph::open(&args.db_path)?;

    // Start execution tracking
    graph.execution_log().start_execution(
        &exec_id,
        env!("CARGO_PKG_VERSION"),
        &["refresh".to_string()],
        Some("."),
        &args.db_path.to_string_lossy(),
    )?;

    // Get git status
    let git_status = get_git_status(&repo, args)?;

    // Get all files from database
    let db_files = graph.all_file_nodes()?;
    let db_file_paths: HashSet<String> = db_files.keys().cloned().collect();

    // Compute delta between git and database
    let delta = compute_delta(&git_status, &db_file_paths, args)?;

    // Apply changes if not dry-run (before moving fields from delta)
    if !args.dry_run {
        apply_changes(&mut graph, &delta)?;

        // Rebuild FTS5 index so symbol search stays synchronized
        if let Err(e) = CodeGraph::rebuild_fts5_index(&args.db_path) {
            eprintln!("  Warning: FTS5 rebuild failed: {}", e);
        }
    }

    // Build report (after apply_changes, moving fields from delta)
    let mut report = RefreshReport::new();
    report.updated = delta.to_update;
    report.deleted = delta.to_delete;
    report.added = delta.to_add;
    report.unchanged = delta.unchanged;
    report.dry_run = args.dry_run;

    // Calculate duration
    report.duration_ms = start_time.elapsed().as_millis() as u64;

    // Output results
    match args.output_format {
        OutputFormat::Json | OutputFormat::Pretty => {
            let response = RefreshResponse::from_report(&report, args.dry_run);
            let json_response = JsonResponse::new(response, &exec_id);
            output_json(&json_response, args.output_format)?;
        }
        OutputFormat::Human => {
            print_human_output(&report, args.dry_run);
        }
    }

    // Finish execution tracking
    let total_files = report.updated.len() + report.added.len();
    graph.execution_log().finish_execution(
        &exec_id,
        "success",
        None,
        total_files,
        0, // Symbol count not tracked here
        0, // Reference count not tracked here
    )?;

    Ok(report)
}

/// Git status information for refresh
#[derive(Debug, Clone)]
struct GitStatus {
    /// Modified files (staged or unstaged)
    modified: Vec<String>,
    /// Deleted files
    deleted: Vec<String>,
    /// Untracked files
    untracked: Vec<String>,
    /// Staged files (for --staged filter)
    staged: Vec<String>,
    /// Unstaged files (for --unstaged filter)
    unstaged: Vec<String>,
}

/// Delta between git and database
#[derive(Debug, Clone)]
struct FileDelta {
    /// Files to update (modified in git, exist in DB)
    to_update: Vec<String>,
    /// Files to delete (in DB but deleted in git)
    to_delete: Vec<String>,
    /// Files to add (new in git, not in DB)
    to_add: Vec<String>,
    /// Files that are unchanged
    unchanged: usize,
}

/// Get git status for the repository
fn get_git_status(repo: &Repository, args: &RefreshArgs) -> Result<GitStatus> {
    let mut status_opts = StatusOptions::new();
    status_opts
        .include_untracked(args.include_untracked)
        .renames_head_to_index(true)
        .renames_index_to_workdir(true);

    // If --staged is specified, only look at staged changes
    if args.staged {
        status_opts.include_untracked(false);
    }

    // If --unstaged is specified, exclude staged changes
    if args.unstaged {
        // StatusOptions doesn't have a direct exclude_staged, but we filter later
    }

    let statuses = repo.statuses(Some(&mut status_opts))?;

    // Get the working directory for converting relative paths to absolute
    let workdir = repo
        .workdir()
        .context("Failed to get repository working directory")?;

    let mut modified = Vec::new();
    let mut deleted = Vec::new();
    let mut untracked = Vec::new();
    let mut staged = Vec::new();
    let mut unstaged = Vec::new();

    for entry in statuses.iter() {
        let rel_path = entry.path().unwrap_or("").to_string();
        // Convert relative path to absolute path to match database
        let path = workdir.join(&rel_path).to_string_lossy().to_string();
        let status = entry.status();

        // Check if staged (in index)
        let is_staged = status.is_index_new()
            || status.is_index_modified()
            || status.is_index_deleted()
            || status.is_index_renamed()
            || status.is_index_typechange();

        // Check if unstaged (in workdir)
        let is_unstaged = status.is_wt_new()
            || status.is_wt_modified()
            || status.is_wt_deleted()
            || status.is_wt_renamed()
            || status.is_wt_typechange();

        if is_staged {
            staged.push(path.clone());
            if status.is_index_modified() || status.is_index_renamed() {
                modified.push(path.clone());
            } else if status.is_index_deleted() {
                deleted.push(path.clone());
            } else if status.is_index_new() {
                untracked.push(path.clone());
            }
        }

        if is_unstaged {
            unstaged.push(path.clone());
            if status.is_wt_modified() || status.is_wt_renamed() {
                modified.push(path.clone());
            } else if status.is_wt_deleted() {
                deleted.push(path.clone());
            } else if status.is_wt_new() {
                untracked.push(path.clone());
            }
        }

        // Handle untracked files (not in index, not in workdir modifications)
        if status.is_wt_new() && !is_staged {
            untracked.push(path);
        }
    }

    // Remove duplicates from modified
    modified.sort();
    modified.dedup();

    Ok(GitStatus {
        modified,
        deleted,
        untracked,
        staged,
        unstaged,
    })
}

/// Compute delta between git status and database files
fn compute_delta(
    git_status: &GitStatus,
    db_files: &HashSet<String>,
    args: &RefreshArgs,
) -> Result<FileDelta> {
    let mut to_update = Vec::new();
    let mut to_delete = Vec::new();
    let mut to_add = Vec::new();

    // Determine which files to consider based on flags
    let modified_files: HashSet<String> = if args.staged {
        git_status.staged.iter().cloned().collect()
    } else if args.unstaged {
        git_status.unstaged.iter().cloned().collect()
    } else {
        git_status.modified.iter().cloned().collect()
    };

    let deleted_files: HashSet<String> = git_status.deleted.iter().cloned().collect();
    let untracked_files: HashSet<String> = if args.include_untracked {
        git_status.untracked.iter().cloned().collect()
    } else {
        HashSet::new()
    };

    // Files to update: modified in git AND exist in database
    for path in &modified_files {
        if db_files.contains(path) {
            to_update.push(path.clone());
        } else if args.include_untracked && untracked_files.contains(path) {
            // New file that should be added
            to_add.push(path.clone());
        }
    }

    // Files to delete: in database but deleted in git
    for path in db_files {
        if deleted_files.contains(path) {
            to_delete.push(path.clone());
        } else if !Path::new(path).exists() {
            // File doesn't exist on filesystem (stale in DB)
            to_delete.push(path.clone());
        }
    }

    // Files to add: untracked in git and not in database
    if args.include_untracked {
        for path in &untracked_files {
            if !db_files.contains(path) && Path::new(path).exists() {
                to_add.push(path.clone());
            }
        }
    }

    // Calculate unchanged files
    let all_affected: HashSet<String> = to_update
        .iter()
        .chain(to_delete.iter())
        .chain(to_add.iter())
        .cloned()
        .collect();
    let unchanged = db_files.difference(&all_affected).count();

    // Sort for deterministic output
    to_update.sort();
    to_delete.sort();
    to_add.sort();

    Ok(FileDelta {
        to_update,
        to_delete,
        to_add,
        unchanged,
    })
}

/// Apply changes to the graph database
fn apply_changes(graph: &mut CodeGraph, delta: &FileDelta) -> Result<()> {
    // Process updates
    for path_str in &delta.to_update {
        let path = Path::new(path_str);
        match graph.reconcile_file_path(path, path_str) {
            Ok(ReconcileOutcome::Reindexed { symbols, .. }) => {
                eprintln!("  Updated: {} ({} symbols)", path_str, symbols);
            }
            Ok(ReconcileOutcome::Unchanged) => {
                eprintln!("  Unchanged: {}", path_str);
            }
            Ok(ReconcileOutcome::Deleted) => {
                eprintln!("  Deleted during update: {}", path_str);
            }
            Err(e) => {
                eprintln!("  Error updating {}: {}", path_str, e);
            }
        }
    }

    // Process deletes
    for path_str in &delta.to_delete {
        match graph.delete_file_facts(path_str) {
            Ok(result) => {
                eprintln!(
                    "  Deleted: {} ({} symbols, {} refs, {} calls)",
                    path_str,
                    result.symbols_deleted,
                    result.references_deleted,
                    result.calls_deleted
                );
            }
            Err(e) => {
                eprintln!("  Error deleting {}: {}", path_str, e);
            }
        }
    }

    // Process adds
    for path_str in &delta.to_add {
        let path = Path::new(path_str);
        match graph.reconcile_file_path(path, path_str) {
            Ok(ReconcileOutcome::Reindexed { symbols, .. }) => {
                eprintln!("  Added: {} ({} symbols)", path_str, symbols);
            }
            Ok(ReconcileOutcome::Unchanged) => {
                eprintln!("  Skipped (unchanged): {}", path_str);
            }
            Ok(ReconcileOutcome::Deleted) => {
                eprintln!("  Skipped (deleted): {}", path_str);
            }
            Err(e) => {
                eprintln!("  Error adding {}: {}", path_str, e);
            }
        }
    }

    Ok(())
}

/// Print human-readable output
fn print_human_output(report: &RefreshReport, dry_run: bool) {
    let mode = if dry_run { " (dry run)" } else { "" };
    println!("Refresh complete{}:", mode);
    println!();

    if report.updated.is_empty() && report.deleted.is_empty() && report.added.is_empty() {
        println!("  No changes detected.");
        println!("  {} files unchanged", report.unchanged);
    } else {
        if !report.updated.is_empty() {
            println!("  Updated: {} files", report.updated.len());
            for path in &report.updated {
                println!("    - {}", path);
            }
        }

        if !report.deleted.is_empty() {
            println!("  Deleted: {} files", report.deleted.len());
            for path in &report.deleted {
                println!("    - {}", path);
            }
        }

        if !report.added.is_empty() {
            println!("  Added: {} files", report.added.len());
            for path in &report.added {
                println!("    - {}", path);
            }
        }

        println!();
        println!("  {} files unchanged", report.unchanged);
    }

    println!();
    println!("Duration: {}ms", report.duration_ms);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_refresh_report_new() {
        let report = RefreshReport::new();
        assert!(report.updated.is_empty());
        assert!(report.deleted.is_empty());
        assert!(report.added.is_empty());
        assert_eq!(report.unchanged, 0);
        assert!(!report.dry_run);
        assert_eq!(report.duration_ms, 0);
    }

    #[test]
    fn test_refresh_report_total_changes() {
        let report = RefreshReport {
            updated: vec!["a.rs".to_string(), "b.rs".to_string()],
            deleted: vec!["c.rs".to_string()],
            added: vec!["d.rs".to_string()],
            unchanged: 5,
            dry_run: false,
            duration_ms: 100,
        };
        assert_eq!(report.total_changes(), 4);
    }

    #[test]
    fn test_refresh_args_default() {
        let args = RefreshArgs::default();
        assert_eq!(args.db_path, PathBuf::from(".magellan/magellan.db"));
        assert!(!args.dry_run);
        assert!(!args.include_untracked);
        assert!(!args.staged);
        assert!(!args.unstaged);
        assert!(!args.force);
        assert_eq!(args.output_format, OutputFormat::Human);
    }

    #[test]
    fn test_compute_delta_basic() {
        let git_status = GitStatus {
            modified: vec!["src/main.rs".to_string()],
            deleted: vec!["src/old.rs".to_string()],
            untracked: vec![],
            staged: vec!["src/main.rs".to_string()],
            unstaged: vec![],
        };

        let mut db_files = HashSet::new();
        db_files.insert("src/main.rs".to_string());
        db_files.insert("src/old.rs".to_string());
        // Note: src/unchanged.rs is not included because it would be detected
        // as a stale file (doesn't exist on filesystem) and deleted.
        // In a real scenario, all DB files would exist on the filesystem.

        let args = RefreshArgs::default();
        let delta = compute_delta(&git_status, &db_files, &args).unwrap();

        assert_eq!(delta.to_update, vec!["src/main.rs"]);
        assert_eq!(delta.to_delete, vec!["src/old.rs"]);
        assert!(delta.to_add.is_empty());
        assert_eq!(delta.unchanged, 0);
    }

    #[test]
    fn test_compute_delta_with_untracked() {
        let git_status = GitStatus {
            modified: vec![],
            deleted: vec![],
            untracked: vec!["src/new.rs".to_string()],
            staged: vec![],
            unstaged: vec!["src/new.rs".to_string()],
        };

        let mut db_files = HashSet::new();
        db_files.insert("src/existing.rs".to_string());

        let args = RefreshArgs {
            include_untracked: true,
            ..Default::default()
        };

        // Note: This test would need the file to actually exist for to_add to be populated
        // Since we can't create files in unit tests easily, we just verify the logic path
        let delta = compute_delta(&git_status, &db_files, &args).unwrap();

        // No files to add because src/new.rs doesn't exist in test environment
        assert!(delta.to_add.is_empty() || delta.to_add == vec!["src/new.rs"]);
    }

    #[test]
    fn test_refresh_response_from_report() {
        let report = RefreshReport {
            updated: vec!["a.rs".to_string()],
            deleted: vec!["b.rs".to_string()],
            added: vec!["c.rs".to_string()],
            unchanged: 2,
            dry_run: true,
            duration_ms: 50,
        };

        let response = RefreshResponse::from_report(&report, true);
        assert_eq!(response.updated, vec!["a.rs"]);
        assert_eq!(response.deleted, vec!["b.rs"]);
        assert_eq!(response.added, vec!["c.rs"]);
        assert_eq!(response.unchanged, 2);
        assert_eq!(response.duration_ms, 50);
        assert!(response.dry_run);
    }
}