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
446
447
448
449
450
451
452
453
454
//! Scope — Code intelligence CLI for LLM coding agents.
//!
//! Builds a local code intelligence index and lets you query it efficiently.
//! Use it before editing any non-trivial code to understand structure,
//! dependencies, and blast radius.
use anyhow::{bail, Result};
use clap::{Parser, Subcommand};
use std::path::{Path, PathBuf};

mod commands;
mod config;
mod core;
mod languages;
mod output;

/// Code intelligence CLI for LLM coding agents.
///
/// Scope builds a local code intelligence index and lets you query
/// it efficiently. Use it before editing any non-trivial code to
/// understand structure, dependencies, and blast radius.
#[derive(Parser, Debug)]
#[command(
    name = "scope",
    about = "Code intelligence CLI for LLM coding agents",
    long_about = "Scope builds a local code intelligence index and lets you query \
                  it efficiently. Use it before editing any non-trivial code to \
                  understand structure, dependencies, and blast radius.",
    version,
    propagate_version = true
)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,

    /// Enable verbose logging
    #[arg(long, global = true)]
    pub verbose: bool,

    /// Query across all workspace members (requires scope-workspace.toml).
    ///
    /// When set, commands like map, refs, find, entrypoints, and status
    /// fan out to all projects in the workspace and merge results.
    /// Requires a scope-workspace.toml manifest in the current directory
    /// or a parent directory.
    #[arg(long, global = true)]
    pub workspace: bool,

    /// Target a specific workspace member by name.
    ///
    /// In workspace context, restricts queries to the named project.
    /// Use `scope workspace list` to see available member names.
    #[arg(long, global = true)]
    pub project: Option<String>,
}

/// All available subcommands.
#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Initialise Scope for this project.
    ///
    /// Creates a .scope/ directory with default configuration.
    /// Auto-detects languages from project markers (tsconfig.json, .csproj).
    /// Run this once per project before running `scope index`.
    Init(commands::init::InitArgs),

    /// Build or refresh the code index.
    ///
    /// Walks all source files, parses them with tree-sitter, and stores
    /// symbols and edges in the local SQLite graph database. First run
    /// is always a full index. Subsequent runs can be incremental.
    Index(commands::index::IndexArgs),

    /// Show structural overview of a symbol without reading full source.
    ///
    /// Returns the class/function signature, dependencies, methods with caller
    /// counts, and type information. Use this before `scope source` to understand
    /// structure first.
    ///
    /// Examples:
    ///   scope sketch PaymentService              — sketch a class
    ///   scope sketch PaymentService.processPayment  — sketch a method
    ///   scope sketch src/payments/service.ts     — sketch a whole file
    Sketch(commands::sketch::SketchArgs),

    /// Find all references to a symbol across the codebase.
    ///
    /// Returns call sites, imports, type annotations, and other references.
    /// Use before changing a function signature to find all callers.
    ///
    /// Examples:
    ///   scope refs processPayment              — all references
    ///   scope refs PaymentService --kind calls  — only call sites
    Refs(commands::refs::RefsArgs),

    /// Show all callers of a function or method.
    ///
    /// At depth 1 (default): equivalent to `scope refs <symbol> --kind calls`.
    /// At depth 2+: performs transitive impact analysis showing callers of callers.
    ///
    /// Examples:
    ///   scope callers processPayment              — direct callers only
    ///   scope callers processPayment --depth 2    — callers + callers-of-callers
    ///   scope callers processPayment --context 2  — with surrounding code (depth 1)
    Callers(commands::refs::CallersArgs),

    /// Show what a symbol depends on.
    ///
    /// Lists direct imports, calls, and type references. Use --depth 2
    /// for transitive dependencies.
    ///
    /// Examples:
    ///   scope deps PaymentService               — direct dependencies
    ///   scope deps PaymentService --depth 2     — transitive dependencies
    Deps(commands::deps::DepsArgs),

    /// Show what depends on a symbol (reverse dependencies).
    ///
    /// Critical before any refactor or deletion. Shows all symbols
    /// and files that depend on the given symbol.
    ///
    /// Examples:
    ///   scope rdeps PaymentService              — what uses this class
    ///   scope rdeps PaymentConfig --depth 2     — transitive reverse deps
    Rdeps(commands::rdeps::RdepsArgs),

    /// Analyse blast radius if a symbol changes.
    ///
    /// Performs transitive reverse dependency traversal. Shows direct
    /// callers, second-degree dependents, and affected test files.
    ///
    /// Examples:
    ///   scope impact processPayment             — who breaks if this changes
    ///   scope impact PaymentConfig              — blast radius of config change
    Impact(commands::impact::ImpactArgs),

    /// Show which symbols changed since a git ref.
    ///
    /// Cross-references `git diff --name-only` with the index to show
    /// exactly which symbols were added, modified, or deleted.
    /// Designed for code review and PR triage.
    ///
    /// Examples:
    ///   scope diff                     — changes since last commit
    ///   scope diff --ref main          — changes vs main branch
    ///   scope diff --ref HEAD~3 --json — last 3 commits, JSON output
    Diff(commands::diff::DiffArgs),

    /// Find call paths between two symbols.
    ///
    /// Shows how <start> reaches <end> through the call graph.
    /// Use this when you need to understand how data or control flows
    /// between two specific points in the codebase.
    ///
    /// Unlike `scope trace` (entry points → target), this finds paths
    /// between any two symbols.
    ///
    /// Examples:
    ///   scope flow PaymentService OrderController
    ///   scope flow processPayment handleWebhook --depth 5
    ///   scope flow "src/auth.ts::validate" "src/api.ts::respond" --json
    Flow(commands::flow::FlowArgs),

    /// Find code by intent using semantic search.
    ///
    /// Uses embeddings to find symbols by what they do, not what they
    /// are called. Returns ranked results with similarity scores.
    ///
    /// Examples:
    ///   scope find "handles authentication errors"
    ///   scope find "sends email notifications" --kind function
    Find(commands::find::FindArgs),

    /// Find structurally similar symbols.
    ///
    /// Uses embeddings to find symbols with similar structure or semantics.
    /// Useful for discovering existing implementations before writing new code.
    ///
    /// Examples:
    ///   scope similar processPayment            — find similar functions
    ///   scope similar PaymentService --kind class — find similar classes
    Similar(commands::similar::SimilarArgs),

    /// One-line summary of a symbol (~30 tokens).
    ///
    /// Returns name, kind, location, signature, caller count, and dependency
    /// count on a single line. Use when an agent just needs "what is this?"
    /// without the full sketch output.
    ///
    /// Examples:
    ///   scope summary PaymentService
    ///   scope summary Graph.find_symbol
    Summary(commands::summary::SummaryArgs),

    /// Fetch full source of a specific symbol.
    ///
    /// Returns the exact source code of the symbol. Only call this when
    /// ready to read or edit the implementation — use `scope sketch` first.
    ///
    /// Examples:
    ///   scope source processPayment
    ///   scope source PaymentService.validateCard
    Source(commands::source::SourceArgs),

    /// Trace call paths from entry points to a symbol.
    ///
    /// Shows how API endpoints, workers, and event handlers reach the
    /// target method through the call graph. Useful for understanding
    /// how a bug is triggered or what code paths exercise a function.
    ///
    /// Examples:
    ///   scope trace processPayment
    ///   scope trace SubscriptionService.processRenewal
    Trace(commands::trace::TraceArgs),

    /// List entry points — API controllers, workers, and event handlers.
    ///
    /// Shows symbols with no incoming call edges, grouped by type.
    /// These are the starting points for request flows: HTTP endpoints,
    /// background workers, event handlers, and standalone functions.
    ///
    /// Examples:
    ///   scope entrypoints
    ///   scope entrypoints --json
    Entrypoints(commands::entrypoints::EntrypointsArgs),

    /// Show a structural overview of the repository.
    ///
    /// Displays entry points, core symbols ranked by importance,
    /// architecture layers, and key statistics. Gives an LLM agent
    /// a complete mental model of the codebase in ~500-1000 tokens,
    /// replacing multiple scope sketch calls.
    ///
    /// Examples:
    ///   scope map
    ///   scope map --limit 5
    ///   scope map --json
    Map(commands::map::MapArgs),

    /// Show index status and freshness.
    ///
    /// Quick health check: is the index built? How many symbols and files?
    /// Are there stale or unindexed files?
    Status(commands::status::StatusArgs),

    /// One-command agent integration setup.
    ///
    /// Runs init + index + writes CLAUDE.md snippet + installs skill file.
    /// With --preload, bakes `scope map` into CLAUDE.md for 32% agent cost savings.
    ///
    /// Examples:
    ///   scope setup              — full setup
    ///   scope setup --preload    — setup with architecture preloading
    Setup(commands::setup::SetupArgs),

    /// Manage multi-project workspaces.
    ///
    /// A workspace groups multiple Scope projects and enables federated
    /// queries across all members. Use `scope workspace init` to discover
    /// projects and create a scope-workspace.toml manifest.
    ///
    /// Examples:
    ///   scope workspace init              — discover and create manifest
    ///   scope workspace list              — show all members and status
    ///   scope workspace list --json       — machine-readable output
    Workspace(commands::workspace::WorkspaceArgs),
}

/// The resolved execution context: single project or workspace.
pub enum Context {
    /// Standard single-project mode. CWD has a .scope/ directory (or will create one).
    SingleProject {
        /// Absolute path to the project root.
        root: PathBuf,
    },
    /// Workspace mode. A scope-workspace.toml was found.
    Workspace {
        /// Path to the scope-workspace.toml file.
        manifest_path: PathBuf,
        /// Workspace root directory (parent of the manifest).
        workspace_root: PathBuf,
        /// Parsed workspace configuration.
        config: config::workspace::WorkspaceConfig,
    },
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    // Initialise tracing
    let level = if cli.verbose {
        tracing::Level::DEBUG
    } else {
        tracing::Level::WARN
    };

    tracing_subscriber::fmt()
        .with_max_level(level)
        .with_writer(std::io::stderr)
        .init();

    // Resolve context based on flags
    let ctx = resolve_context(cli.workspace, cli.project.as_deref())?;

    match &cli.command {
        // --- Commands that SUPPORT workspace mode ---
        Commands::Status(args) => commands::status::run(args, &ctx),
        Commands::Map(args) => commands::map::run(args, &ctx),
        Commands::Refs(args) => commands::refs::run(args, &ctx),
        Commands::Find(args) => commands::find::run(args, &ctx),
        Commands::Entrypoints(args) => commands::entrypoints::run(args, &ctx),

        // --- Commands that operate on a single project only ---
        Commands::Init(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::init::run(args, root)
        }
        Commands::Index(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::index::run(args, root)
        }
        Commands::Sketch(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::sketch::run(args, root)
        }
        Commands::Callers(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::refs::run_callers(args, root)
        }
        Commands::Deps(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::deps::run(args, root)
        }
        Commands::Rdeps(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::rdeps::run(args, root)
        }
        Commands::Impact(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::impact::run(args, root)
        }
        Commands::Similar(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::similar::run(args, root)
        }
        Commands::Summary(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::summary::run(args, root)
        }
        Commands::Source(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::source::run(args, root)
        }
        Commands::Trace(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::trace::run(args, root)
        }
        Commands::Diff(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::diff::run(args, root)
        }
        Commands::Flow(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::flow::run(args, root)
        }

        Commands::Setup(args) => {
            let root = project_root_from_context(&ctx)?;
            commands::setup::run(args, root)
        }

        // --- Workspace management subcommands ---
        Commands::Workspace(args) => {
            let root = cwd()?;
            commands::workspace::run(args, &root)
        }
    }
}

/// Resolve the execution context based on CLI flags.
///
/// - No flags: returns `SingleProject` with CWD as root.
/// - `--workspace`: finds scope-workspace.toml upward from CWD.
/// - `--project <name>`: finds workspace manifest, then targets that member.
fn resolve_context(workspace_flag: bool, project_flag: Option<&str>) -> Result<Context> {
    let cwd = cwd()?;

    if let Some(project_name) = project_flag {
        // --project implies workspace context; find the manifest and resolve member
        let manifest_path = commands::workspace::find_workspace_manifest(&cwd)?;
        let workspace_root = manifest_path.parent().unwrap_or(&cwd).to_path_buf();
        let config = config::workspace::WorkspaceConfig::load(&manifest_path)?;
        config.validate(&workspace_root)?;

        // Find the named member
        let member = config
            .workspace
            .members
            .iter()
            .find(|m| config::workspace::WorkspaceConfig::resolve_member_name(m) == project_name)
            .ok_or_else(|| {
                let available: Vec<String> = config
                    .workspace
                    .members
                    .iter()
                    .map(config::workspace::WorkspaceConfig::resolve_member_name)
                    .collect();
                anyhow::anyhow!(
                    "Project '{}' not found in workspace. Available: {}",
                    project_name,
                    available.join(", ")
                )
            })?;

        let member_root = workspace_root.join(&member.path);
        return Ok(Context::SingleProject { root: member_root });
    }

    if workspace_flag {
        let manifest_path = commands::workspace::find_workspace_manifest(&cwd)?;
        let workspace_root = manifest_path.parent().unwrap_or(&cwd).to_path_buf();
        let config = config::workspace::WorkspaceConfig::load(&manifest_path)?;
        config.validate(&workspace_root)?;

        return Ok(Context::Workspace {
            manifest_path,
            workspace_root,
            config,
        });
    }

    // Default: single project with CWD
    Ok(Context::SingleProject { root: cwd })
}

/// Get the current working directory.
fn cwd() -> Result<PathBuf> {
    std::env::current_dir().map_err(|e| anyhow::anyhow!("Failed to get current directory: {e}"))
}

/// Extract a project root from the context.
///
/// For single-project mode, returns the root directly.
/// For workspace mode, errors with guidance to use `--project`.
fn project_root_from_context(ctx: &Context) -> Result<&Path> {
    match ctx {
        Context::SingleProject { root } => Ok(root),
        Context::Workspace { .. } => {
            bail!(
                "This command operates on a single project. \
                 Use --project <name> to target a workspace member."
            )
        }
    }
}