scope-cli 0.9.2

Code intelligence CLI for LLM coding agents — structural navigation, dependency graphs, and semantic search without reading full source files
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
/// `scope index` — build or refresh the code index.
///
/// Walks the project's source files, parses them with tree-sitter,
/// and stores symbols and edges in the SQLite graph database.
///
/// By default, runs incrementally: only re-indexes changed files.
/// Use `--full` to force a complete rebuild.
/// Use `--watch` to keep running and re-index automatically on file changes.
use anyhow::{bail, Result};
use chrono::Utc;
use clap::Args;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::config::ProjectConfig;
use crate::core::graph::Graph;
use crate::core::indexer::Indexer;
use crate::core::searcher::Searcher;
use crate::core::watcher::{WatchLock, Watcher};
use crate::output::formatter;

/// Arguments for the `scope index` command.
#[derive(Args, Debug)]
pub struct IndexArgs {
    /// Force a full rebuild of the index (ignore incremental cache)
    #[arg(long)]
    pub full: bool,

    /// Output as JSON instead of human-readable format
    #[arg(long, short = 'j')]
    pub json: bool,

    /// Watch for file changes and re-index automatically.
    ///
    /// Runs an initial index, then watches for file changes and
    /// re-indexes incrementally when source files are modified.
    /// Runs until interrupted with Ctrl+C.
    ///
    /// With --json, emits NDJSON events to stdout:
    ///   {"event":"start",...}   — when watching begins
    ///   {"event":"reindex",...} — after each re-index
    ///   {"event":"stop",...}    — on shutdown
    #[arg(long)]
    pub watch: bool,
}

/// Run the `scope index` command.
pub fn run(args: &IndexArgs, project_root: &Path) -> Result<()> {
    let scope_dir = project_root.join(".scope");

    if !scope_dir.exists() {
        bail!("No .scope/ directory found. Run 'scope init' first.");
    }

    // Load config
    let config = ProjectConfig::load(&scope_dir)?;

    // Open graph database
    let db_path = scope_dir.join("graph.db");
    let mut graph = Graph::open(&db_path)?;

    // Create indexer
    let mut indexer = Indexer::new()?;

    // Open search index (FTS5) — optional, skip with warning if it fails
    let searcher = match Searcher::open(&db_path) {
        Ok(s) => Some(s),
        Err(e) => {
            tracing::warn!("Search index unavailable: {e}");
            None
        }
    };

    if args.watch {
        run_watch(
            args,
            &mut indexer,
            project_root,
            &config,
            &mut graph,
            searcher.as_ref(),
            &scope_dir,
            &db_path,
        )
    } else if args.full {
        run_full_index(
            args,
            &mut indexer,
            project_root,
            &config,
            &mut graph,
            searcher.as_ref(),
        )
    } else {
        run_incremental_index(
            args,
            &mut indexer,
            project_root,
            &config,
            &mut graph,
            searcher.as_ref(),
        )
    }
}

/// Run a full index rebuild.
fn run_full_index(
    args: &IndexArgs,
    indexer: &mut Indexer,
    project_root: &Path,
    config: &ProjectConfig,
    graph: &mut Graph,
    searcher: Option<&Searcher>,
) -> Result<()> {
    let stats = indexer.index_full(project_root, config, graph, searcher)?;

    if args.json {
        let output = serde_json::json!({
            "command": "index",
            "mode": "full",
            "file_count": stats.file_count,
            "symbol_count": stats.symbol_count,
            "edge_count": stats.edge_count,
            "duration_secs": stats.duration.as_secs_f64(),
            "languages": stats.language_stats.iter().map(|ls| {
                serde_json::json!({
                    "language": ls.language,
                    "file_count": ls.file_count,
                    "symbol_count": ls.symbol_count,
                })
            }).collect::<Vec<_>>(),
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        for ls in &stats.language_stats {
            eprintln!(
                "  {:<12} {} files  {} symbols",
                ls.language, ls.file_count, ls.symbol_count
            );
        }
        eprintln!(
            "Built in {:.1}s. {} symbols, {} edges.",
            stats.duration.as_secs_f64(),
            stats.symbol_count,
            stats.edge_count
        );
    }

    Ok(())
}

/// Run an incremental index (default).
fn run_incremental_index(
    args: &IndexArgs,
    indexer: &mut Indexer,
    project_root: &Path,
    config: &ProjectConfig,
    graph: &mut Graph,
    searcher: Option<&Searcher>,
) -> Result<()> {
    let stats = indexer.index_incremental(project_root, config, graph, searcher)?;

    if stats.up_to_date {
        if args.json {
            let output = serde_json::json!({
                "command": "index",
                "mode": "incremental",
                "up_to_date": true,
            });
            println!("{}", serde_json::to_string_pretty(&output)?);
        } else {
            eprintln!("Index up to date.");
        }
        return Ok(());
    }

    if args.json {
        let output = serde_json::json!({
            "command": "index",
            "mode": "incremental",
            "up_to_date": false,
            "modified": stats.modified,
            "added": stats.added,
            "deleted": stats.deleted,
            "symbol_count": stats.symbol_count,
            "edge_count": stats.edge_count,
            "duration_secs": stats.duration.as_secs_f64(),
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else {
        formatter::print_incremental_result(
            &stats.modified,
            &stats.added,
            &stats.deleted,
            stats.duration.as_secs_f64(),
        );
    }

    Ok(())
}

/// Run watch mode: initial index then watch for changes.
#[allow(clippy::too_many_arguments)]
fn run_watch(
    args: &IndexArgs,
    indexer: &mut Indexer,
    project_root: &Path,
    config: &ProjectConfig,
    graph: &mut Graph,
    searcher: Option<&Searcher>,
    scope_dir: &Path,
    db_path: &Path,
) -> Result<()> {
    // Acquire watch lock
    let lock = WatchLock::new(scope_dir);
    lock.acquire()?;

    // Set up Ctrl+C handler
    let running = Arc::new(AtomicBool::new(true));
    let running_ctrlc = running.clone();
    ctrlc::set_handler(move || {
        running_ctrlc.store(false, Ordering::SeqCst);
    })
    .map_err(|e| anyhow::anyhow!("Failed to set Ctrl+C handler: {e}"))?;

    // Run initial index
    if args.full {
        let stats = indexer.index_full(project_root, config, graph, searcher)?;
        if !args.json {
            for ls in &stats.language_stats {
                eprintln!(
                    "  {:<12} {} files  {} symbols",
                    ls.language, ls.file_count, ls.symbol_count
                );
            }
            eprintln!(
                "Initial index: {:.1}s. {} symbols, {} edges.",
                stats.duration.as_secs_f64(),
                stats.symbol_count,
                stats.edge_count
            );
        }
    } else {
        let stats = indexer.index_incremental(project_root, config, graph, searcher)?;
        if !args.json {
            if stats.up_to_date {
                eprintln!("Index up to date.");
            } else {
                let total = stats.modified.len() + stats.added.len() + stats.deleted.len();
                eprintln!(
                    "Initial index: {:.1}s. {} files changed.",
                    stats.duration.as_secs_f64(),
                    total
                );
            }
        }
    }

    // Build supported extensions list from config languages
    let supported_extensions = get_supported_extensions(config);

    // Emit start event
    let watch_start = Instant::now();
    if args.json {
        let start_event = serde_json::json!({
            "event": "start",
            "project": config.project.name,
            "languages": config.project.languages,
            "timestamp": Utc::now().to_rfc3339(),
        });
        println!("{}", serde_json::to_string(&start_event)?);
    } else {
        eprintln!("Watching for changes... (Ctrl+C to stop)");
    }

    // Create watcher
    let watcher = Watcher::new(
        project_root.to_path_buf(),
        config.index.ignore.clone(),
        supported_extensions,
        Duration::from_millis(300),
    );

    let (rx, _debouncer) = watcher.start()?;

    let mut total_reindexes: u64 = 0;
    let mut total_files_processed: u64 = 0;

    // Event loop
    while running.load(Ordering::SeqCst) {
        // Use a short timeout so we can check the running flag periodically
        match rx.recv_timeout(Duration::from_millis(200)) {
            Ok(changed_paths) => {
                let batch_start = Instant::now();
                let files_changed = changed_paths.len();

                // Re-open graph for each batch to avoid stale connections
                *graph = match Graph::open(db_path) {
                    Ok(g) => g,
                    Err(e) => {
                        eprintln!(
                            "Warning: failed to open graph for re-index: {e}. Skipping batch."
                        );
                        continue;
                    }
                };

                // Re-open searcher for each batch
                let batch_searcher = match Searcher::open(db_path) {
                    Ok(s) => Some(s),
                    Err(e) => {
                        tracing::warn!("Search index unavailable for re-index: {e}");
                        None
                    }
                };

                // Get symbol/edge counts before re-index for delta calculation
                let symbols_before = graph.symbol_count().unwrap_or(0);
                let edges_before = graph.edge_count().unwrap_or(0);

                // Run incremental index — skip batch on transient failure
                let stats = match indexer.index_incremental(
                    project_root,
                    config,
                    graph,
                    batch_searcher.as_ref(),
                ) {
                    Ok(s) => s,
                    Err(e) => {
                        eprintln!("Warning: re-index failed: {e}. Skipping batch.");
                        continue;
                    }
                };

                let duration_ms = batch_start.elapsed().as_millis() as u64;

                // Compute deltas
                let symbols_after = stats.symbol_count;
                let edges_after = stats.edge_count;
                let symbols_added = symbols_after.saturating_sub(symbols_before);
                let symbols_removed = symbols_before.saturating_sub(symbols_after);
                let edges_added = edges_after.saturating_sub(edges_before);
                let edges_removed = edges_before.saturating_sub(edges_after);

                total_reindexes += 1;
                total_files_processed += files_changed as u64;

                if args.json {
                    let reindex_event = serde_json::json!({
                        "event": "reindex",
                        "files_changed": files_changed,
                        "symbols_added": symbols_added,
                        "symbols_removed": symbols_removed,
                        "edges_added": edges_added,
                        "edges_removed": edges_removed,
                        "duration_ms": duration_ms,
                        "timestamp": Utc::now().to_rfc3339(),
                    });
                    println!("{}", serde_json::to_string(&reindex_event)?);
                } else {
                    let total_changed =
                        stats.modified.len() + stats.added.len() + stats.deleted.len();
                    if total_changed > 0 {
                        eprintln!(
                            "Re-indexed {} file{} in {}ms ({} symbols, {} edges)",
                            total_changed,
                            if total_changed == 1 { "" } else { "s" },
                            duration_ms,
                            symbols_after,
                            edges_after
                        );
                    }
                }
            }
            Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
                // No events — just loop and check running flag
                continue;
            }
            Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
                // Watcher thread died
                tracing::warn!("File watcher disconnected unexpectedly");
                break;
            }
        }
    }

    // Print shutdown summary
    let uptime_secs = watch_start.elapsed().as_secs();

    if args.json {
        let stop_event = serde_json::json!({
            "event": "stop",
            "total_reindexes": total_reindexes,
            "total_files_processed": total_files_processed,
            "uptime_seconds": uptime_secs,
            "timestamp": Utc::now().to_rfc3339(),
        });
        println!("{}", serde_json::to_string(&stop_event)?);
    } else {
        eprintln!(
            "Stopped. {} re-index{}, {} files processed, uptime {}s.",
            total_reindexes,
            if total_reindexes == 1 { "" } else { "es" },
            total_files_processed,
            uptime_secs
        );
    }

    // Lock is released by Drop
    Ok(())
}

/// Get supported file extensions from the project config languages.
fn get_supported_extensions(config: &ProjectConfig) -> Vec<String> {
    let mut extensions = Vec::new();
    for lang in &config.project.languages {
        match lang.to_lowercase().as_str() {
            "typescript" => {
                extensions.push("ts".to_string());
                extensions.push("tsx".to_string());
            }
            "csharp" | "c#" => {
                extensions.push("cs".to_string());
            }
            "python" => {
                extensions.push("py".to_string());
            }
            "go" => {
                extensions.push("go".to_string());
            }
            "java" => {
                extensions.push("java".to_string());
            }
            "rust" => {
                extensions.push("rs".to_string());
            }
            _ => {
                tracing::warn!("Unknown language for watch mode: {lang}");
            }
        }
    }
    extensions
}