Skip to main content

reference_query/cli/
mod.rs

1//! Command-line surface. Search is the default action: `rq <query>`.
2
3use std::collections::HashSet;
4use std::io::{IsTerminal, Write};
5use std::path::PathBuf;
6use std::process::ExitCode;
7use std::time::Duration;
8
9use clap::{CommandFactory, Parser};
10use clap_complete::Shell;
11
12use crate::core::now_unix;
13use crate::store::Store;
14
15/// Search is the default action (`rq <query>`). Operations are flags rather
16/// than subcommands so no word is reserved — `rq index`, `rq status`, and
17/// `rq record` all search for those symbols. This also matches the rg/fd feel.
18#[derive(Parser)]
19#[command(
20    name = "rq",
21    version,
22    about = "Ranked definition lookup — the one place a symbol is defined, first.",
23    long_about = "rq finds where a symbol is defined and ranks the one you most \
24likely meant to the top — not every match.\n\n\
25Search is the default action; operations are flags, not subcommands, so every \
26word (including \"index\", \"status\", \"record\") stays searchable. Ranking favors \
27your current repo, recently-active files, and the files your branch changes. Run \
28`rq <query> --explain` to see the score behind each result.",
29    after_help = "EXAMPLES:\n  \
30rq thing                  search for a definition named or like \"thing\"\n  \
31rq wibble --explain       same, plus the score behind each result\n  \
32rq thing --json           machine-readable results (for editors/agents)\n  \
33rq thing --no-wait        answer now from the committed index; don't block on a rebuild\n  \
34rq thing --wait 2s        ...or wait up to a bounded time for the index to warm\n  \
35rq thing app/web          restrict to a directory (rg-style)\n  \
36rq perform -k method      restrict to a symbol kind (c/mod/m/f/s/e/t)\n  \
37rq class Widget           a leading kind keyword is shorthand for -k\n  \
38rq --symbols FILE         outline a file's definitions, in line order\n  \
39rq thing -x rust          restrict to a language (ruby/rust/go/python/ts/js)\n  \
40rq 'Foo::Bar'             qualify by scope — the surest way past an ambiguous name\n  \
41rq 'Foo#bar'              ...and by owner, for a method\n  \
42rq save --anchor w.rb:9   rank as if asked from line 9 of w.rb\n  \
43rq Foo.new                the constructor (initialize, __init__, ...)\n  \
44rq 'refund*proc'          wildcards: * (any run), ? (one char) — quote them\n  \
45rq -o thing               open the best match in your editor\n  \
46rq --index                index the current repository\n  \
47rq --status               show indexing coverage\n  \
48rq --usage                show how rq has been called (by caller and flags)\n  \
49rq --drop                 remove this repo's index (opposite of --index)\n\n\
50SHORT FLAGS (easy to misread):\n  \
51-j = --json (not jobs; --jobs is long-only)   -l = --limit (not lang)   -x = --lang\n\n\
52The index is a SQLite file at $RQ_DB, an absolute path (default \
53~/.local/share/rq/rq.db); it warms \
54automatically on the first search in a git repo. On a large, cold repo a search \
55keeps indexing until it can answer rather than reporting a premature \"no \
56matches\" (an interactive run shows progress and stops on Ctrl-C).\n\n\
57EXIT CODES:\n  \
580   matched\n  \
591   no match\n  \
602   no match yet: the index is still warming, or indexing was interrupted — ask again\n  \
6164  usage error: a bad flag, value or query — fix the command\n  \
6266  a file the command names doesn't exist\n  \
6369  no editor, browser or git remote to hand off to\n  \
6470  internal error (a bug)\n  \
6574  the index can't be opened, read or written"
66)]
67struct Cli {
68    /// Search query. With --drop, the repo path/identity to drop.
69    //
70    // `Other` keeps shells from offering filenames here: a search query isn't a
71    // path. The path-valued operations (--index, --symbols) carry their own
72    // value with a path hint instead, so completion is scoped to them.
73    #[arg(value_name = "TARGET", value_hint = clap::ValueHint::Other)]
74    target: Option<String>,
75
76    /// Directories to restrict results to (rg-style; same as repeated --path).
77    #[arg(value_name = "PATH")]
78    dirs: Vec<String>,
79
80    /// Show the score breakdown for each result.
81    #[arg(short = 'e', long)]
82    explain: bool,
83
84    /// Answer immediately from the committed index — never block waiting on a
85    /// background (re)index. For agents/scripts: a query issued mid-rebuild
86    /// returns at once (a miss reports `warming`, exit 2, so a caller can retry)
87    /// instead of blocking up to the wait budget. Shorthand for `--wait 0`;
88    /// leftover warming still detaches to a background child.
89    #[arg(long = "no-wait")]
90    no_wait: bool,
91
92    /// How long a query may wait for the index to warm before answering with
93    /// whatever's committed: a duration like `50ms`, `2s`, `1m`, or a bare number
94    /// of seconds. `0` doesn't wait at all (same as `--no-wait`). Overrides
95    /// `RQ_WAIT_BUDGET_MS` for this call (default 1 minute).
96    #[arg(long, value_name = "DUR", value_parser = parse_wait, conflicts_with = "no_wait")]
97    wait: Option<Duration>,
98
99    /// Open the best match in your editor.
100    /// On a terminal with several matches, prompts to choose. Launcher: `RQ_OPEN`
101    /// (a template with `{file}`/`{line}`/`{}` = path:line; with none of them,
102    /// path:line is appended), else VS Code (`code`), else `$VISUAL`/`$EDITOR`,
103    /// else prints the resolved path:line.
104    #[arg(short = 'o', long, conflicts_with_all = ["index", "status", "json", "ndjson"])]
105    open: bool,
106
107    /// Like --open, but in the browser: the match on its git host (GitHub-style
108    /// `blob/<sha>/<file>#L<line>` URL), pinned to the newest pushed commit in
109    /// HEAD's history so the link resolves and stays accurate. Launcher: `$BROWSER`, else `open`/`xdg-open`, else
110    /// prints the URL.
111    #[arg(short = 'w', long, conflicts_with_all = ["open", "index", "status", "json", "ndjson"])]
112    web: bool,
113
114    /// Print the definition's source, not just its location — but only when the
115    /// top match is confident; otherwise falls back to the ranked list. Pipe to a
116    /// pager (`rq --show foo | less`). JSON adds a `body` field.
117    #[arg(long, conflicts_with_all = ["open", "web", "index", "status", "symbols", "drop"])]
118    show: bool,
119
120    /// Emit results as a JSON array (for editors and scripts).
121    #[arg(short = 'j', long)]
122    json: bool,
123
124    /// Emit results as newline-delimited JSON, one object per line.
125    #[arg(short = 'J', long, conflicts_with = "json")]
126    ndjson: bool,
127
128    /// Restrict results to files under this repo-relative directory (repeatable).
129    #[arg(short = 'p', long, value_name = "DIR")]
130    path: Vec<String>,
131
132    /// Maximum number of results to show; `0` shows every match.
133    #[arg(short = 'l', long, value_name = "N", default_value_t = DEFAULT_LIMIT)]
134    limit: usize,
135
136    /// Restrict to symbol kinds: class, module, method, function, struct, enum,
137    /// trait, constant (shortcuts: c, mod, m, f, s, e, t, const; `interface` =
138    /// trait, `type` = struct). Repeatable or comma-separated.
139    #[arg(short = 'k', long, value_name = "KIND", value_delimiter = ',')]
140    kind: Vec<String>,
141
142    /// Restrict to languages: ruby, rust, go, python, typescript, javascript.
143    /// Prefix-matched, so `r` means ruby+rust and `p` means python; aliases rb,
144    /// rs, golang, ts, tsx, js, jsx. Repeatable or comma-separated.
145    #[arg(short = 'x', long = "lang", value_name = "LANG", value_delimiter = ',')]
146    lang: Vec<String>,
147
148    /// Search every indexed repository, not just the current one. By default a
149    /// search inside a repo returns only that repo's definitions.
150    #[arg(short = 'a', long = "all-repos")]
151    all_repos: bool,
152
153    /// The position the query is asked from — an editor's cursor, or the file
154    /// an agent is reading. Ranks definitions in the scopes enclosing that line,
155    /// then those in the same file and nearby directories, higher. Context,
156    /// never a filter. FILE is relative to the current directory; COL is
157    /// accepted and ignored.
158    #[arg(long, value_name = "FILE:LINE[:COL]", value_parser = parse_anchor, conflicts_with_all = ["index", "status", "usage", "symbols", "drop", "warm"])]
159    anchor: Option<AnchorSpec>,
160
161    /// Index a repository (PATH, or the current directory).
162    #[arg(long, value_name = "PATH", num_args = 0..=1, value_hint = clap::ValueHint::AnyPath, conflicts_with = "status")]
163    index: Option<Option<String>>,
164
165    /// Show indexing coverage per known repository.
166    #[arg(long, conflicts_with = "index")]
167    status: bool,
168
169    /// Show how rq has been used: searches per day, by caller and flags.
170    #[arg(long, conflicts_with_all = ["index", "status"])]
171    usage: bool,
172
173    /// List the symbols defined in FILE, in line order — a structural outline,
174    /// not a ranked search. Honors -k/-x to filter by kind/language.
175    #[arg(long, value_name = "FILE", value_hint = clap::ValueHint::FilePath, conflicts_with_all = ["index", "status", "drop", "open", "web"])]
176    symbols: Option<String>,
177
178    /// Drop a repository's index — the opposite of --index. Removes its symbols,
179    /// files, and coverage. TARGET is the repo's path (or the
180    /// current repo); a known identity string (as shown by --status) also works.
181    #[arg(long, conflicts_with_all = ["index", "status", "open", "web"])]
182    drop: bool,
183
184    /// Finish warming a repository's index in the background — the target a
185    /// search re-execs after printing results, detached, so the shell never
186    /// waits on it. Single-flighted per repo; safe to run by hand.
187    #[arg(long, hide = true, value_name = "PATH", num_args = 0..=1, value_hint = clap::ValueHint::AnyPath, conflicts_with_all = ["index", "status", "drop", "symbols", "open", "web", "show"])]
188    warm: Option<Option<String>>,
189
190    /// Print a shell completion script (bash, zsh, fish, elvish, powershell).
191    #[arg(long, value_name = "SHELL")]
192    completions: Option<Shell>,
193
194    /// Trace what rq decides (root, coverage, warming, reconcile) to stderr —
195    /// for debugging. `RQ_LOG=1` does the same for an installed binary.
196    #[arg(short = 'v', long)]
197    verbose: bool,
198
199    /// Report where a search spent its time, phase by phase, to stderr — as
200    /// JSON alongside --json, so a baseline can be stored and diffed.
201    /// `RQ_PROFILE=1` does the same for an installed binary.
202    #[arg(long)]
203    profile: bool,
204
205    /// Parse worker threads the background indexer uses (0 = auto). (`-j` is
206    /// taken by `--json`, so this is `--jobs` only.) `RQ_JOBS` works too.
207    #[arg(long, value_name = "N", default_value_t = 0)]
208    jobs: usize,
209}
210
211/// Parse arguments and dispatch. Returns the process exit code.
212pub fn run() -> ExitCode {
213    let cli = match Cli::try_parse() {
214        Ok(cli) => cli,
215        Err(err) => return clap_failure(err),
216    };
217    crate::trace::enable_from(cli.verbose);
218    crate::profile::enable_from(cli.profile);
219    crate::index::set_parse_jobs(cli.jobs);
220    let json_out = output_format(&cli) != Output::Text;
221    let code = dispatch(cli);
222    crate::profile::emit(json_out);
223    code
224}
225
226fn dispatch(cli: Cli) -> ExitCode {
227    if let Some(shell) = cli.completions {
228        clap_complete::generate(shell, &mut Cli::command(), "rq", &mut std::io::stdout());
229        return ExitCode::SUCCESS;
230    }
231    // A relative path would resolve against each caller's cwd, silently
232    // splitting the one shared index into a database per directory.
233    if let Some(db) = std::env::var_os("RQ_DB").filter(|p| std::path::Path::new(p).is_relative()) {
234        return fail(
235            output_format(&cli),
236            Failure::Usage,
237            format_args!(
238                "rq: RQ_DB must be an absolute path, not {:?} (e.g. RQ_DB=\"$PWD/{}\")",
239                db,
240                db.to_string_lossy()
241            ),
242        );
243    }
244    if let Some(path) = &cli.index {
245        // index PATH (else cwd); with --path, seed only those subtrees
246        let out = output_format(&cli);
247        return cmd_index(path.as_deref().map(PathBuf::from), &cli.path, out);
248    }
249    if let Some(path) = &cli.warm {
250        return cmd_warm(path.as_deref());
251    }
252    if cli.status {
253        return cmd_status(output_format(&cli));
254    }
255    if cli.usage {
256        return cmd_usage(output_format(&cli));
257    }
258    if cli.drop {
259        let out = output_format(&cli);
260        return cmd_drop(cli.target, out);
261    }
262    let out = output_format(&cli);
263    if cli.target.as_deref().is_some_and(|t| t.trim().is_empty()) {
264        return fail(out, Failure::Usage, format_args!("rq: empty query"));
265    }
266    // Reject an unknown --kind/--lang rather than filtering everything away: a
267    // typo used to come back as `no_match`, exit 1 — the one code a script is
268    // meant to trust as "this symbol does not exist".
269    let mut kinds: Vec<String> = Vec::new();
270    for k in &cli.kind {
271        match canonical_kind(k) {
272            Some(c) => kinds.push(c.to_string()),
273            None => {
274                return fail(
275                    out,
276                    Failure::Usage,
277                    format_args!(
278                        "rq: unknown --kind {k:?} (class, module, method, function, struct, enum, trait, constant)"
279                    ),
280                );
281            }
282        }
283    }
284    // a language token can expand to several tags (`r` → ruby + rust)
285    let mut langs: Vec<String> = Vec::new();
286    for x in &cli.lang {
287        let matched = canonical_langs(x);
288        if matched.is_empty() {
289            return fail(
290                out,
291                Failure::Usage,
292                format_args!(
293                    "rq: unknown --lang {x:?} ({})",
294                    crate::lang::languages().join(", ")
295                ),
296            );
297        }
298        langs.extend(matched);
299    }
300    if let Some(file) = &cli.symbols {
301        return cmd_symbols(file, &kinds, &langs, out);
302    }
303    // path filters: trailing positionals (rg-style) plus any --path flags
304    let mut paths = cli.path.clone();
305    match cli.target {
306        Some(target) => {
307            // A leading kind keyword (`rq class Foo`) is shorthand for `-k`; skip
308            // it when the user gave an explicit `-k`, so the two never conflict.
309            let query = if cli.kind.is_empty() {
310                let (kw, query, dirs) = split_kind_keyword(target, cli.dirs.clone());
311                if let Some(k) = kw {
312                    kinds.push(k.to_string());
313                }
314                paths.extend(dirs);
315                query
316            } else {
317                paths.extend(cli.dirs.clone());
318                target
319            };
320            let mut session = match Session::open(out) {
321                Ok(s) => s,
322                Err(code) => return code,
323            };
324            session.anchor = cli.anchor.as_ref().map(|a| session.anchor_at(a));
325            cmd_search(
326                &mut session,
327                &SearchArgs {
328                    query: &query,
329                    explain: cli.explain,
330                    out,
331                    paths: &paths,
332                    kinds: &kinds,
333                    langs: &langs,
334                    want: requested_limit(cli.limit),
335                    no_wait: cli.no_wait,
336                    wait: cli.wait,
337                    open: cli.open,
338                    web: cli.web,
339                    all_repos: cli.all_repos,
340                    show: cli.show,
341                    batch: false,
342                    anchored: cli.anchor.is_some(),
343                },
344            )
345        }
346        // No query, but a pipe on stdin: each line is one, all sharing this
347        // run's store, repo resolution and warm.
348        None if !std::io::stdin().is_terminal() => cmd_batch(&cli, out, &paths, &kinds, &langs),
349        // bare `rq` (or just flags like --explain with no query): show help
350        None => {
351            let _ = Cli::command().print_long_help();
352            ExitCode::SUCCESS
353        }
354    }
355}
356
357/// How results are rendered.
358#[derive(Clone, Copy, PartialEq)]
359enum Output {
360    Text,
361    Json,
362    Ndjson,
363}
364
365fn output_format(cli: &Cli) -> Output {
366    if cli.ndjson {
367        Output::Ndjson
368    } else if cli.json {
369        Output::Json
370    } else {
371        Output::Text
372    }
373}
374
375/// Results shown when `--limit` isn't given.
376const DEFAULT_LIMIT: usize = 10;
377
378/// Minimum headroom to rank before a `--path` filter (so filtered-in results
379/// aren't lost to the cutoff).
380const PATH_HEADROOM: usize = 200;
381
382/// `--limit 0` means unlimited: every ranked hit, bounded only by how many
383/// candidates recall returned.
384fn requested_limit(limit: usize) -> usize {
385    if limit == 0 { usize::MAX } else { limit }
386}
387
388/// Count one search for `--usage`. Observability only: nothing reads it back
389/// into ranking.
390fn record_usage(
391    store: &Store,
392    args: &SearchArgs,
393    status: &str,
394    coverage: Option<&str>,
395    live: bool,
396) {
397    let _ = store.record_search(&crate::store::SearchRecord {
398        source: &crate::origin::detect(),
399        flags: &flag_summary(args),
400        status,
401        coverage: coverage.unwrap_or("none"),
402        live,
403    });
404}
405
406/// The call's flags as a canonical, comma-joined string, for usage counts.
407/// A fixed vocabulary in a fixed order, so the same call always produces the
408/// same string and the counter table stays small — values are never included,
409/// only which knobs were reached for.
410fn flag_summary(args: &SearchArgs) -> String {
411    let mut on: Vec<&str> = Vec::new();
412    match args.out {
413        Output::Json => on.push("json"),
414        Output::Ndjson => on.push("ndjson"),
415        Output::Text => {}
416    }
417    for (present, name) in [
418        (args.explain, "explain"),
419        (args.show, "show"),
420        (args.open, "open"),
421        (args.web, "web"),
422        (args.all_repos, "all-repos"),
423        (args.no_wait, "no-wait"),
424        (args.batch, "batch"),
425        (args.anchored, "anchor"),
426        (!args.paths.is_empty(), "path"),
427        (!args.kinds.is_empty(), "kind"),
428        (!args.langs.is_empty(), "lang"),
429        (args.want != DEFAULT_LIMIT, "limit"),
430    ] {
431        if present {
432            on.push(name);
433        }
434    }
435    on.join(",")
436}
437
438/// How often the search re-checks the index while a cold repo warms on the
439/// background thread. Each poll runs a full read query against the DB the
440/// indexer is actively writing, so polling too fast steals CPU and read-lock
441/// churn from the warm; 100 ms keeps that pressure low while staying
442/// imperceptible (an early answer or completion appears within a frame, and the
443/// progress line only redraws every `PROGRESS_REDRAW` anyway).
444const POLL_INTERVAL: Duration = Duration::from_millis(100);
445
446/// How long a cold-repo query may wait silently before we tell the user we're
447/// indexing — short enough to explain the pause, long enough that a repo which
448/// indexes quickly never flashes a message.
449const HEADS_UP_DELAY: Duration = Duration::from_millis(500);
450
451/// Minimum gap between progress-line redraws once the heads-up is showing — keeps
452/// the line from flickering (and the count query off the hot path) while still
453/// feeling live.
454const PROGRESS_REDRAW: Duration = Duration::from_millis(120);
455
456/// Everything `rq <query>` needs, bundled from the parsed CLI flags.
457struct SearchArgs<'a> {
458    query: &'a str,
459    explain: bool,
460    out: Output,
461    paths: &'a [String],
462    kinds: &'a [String],
463    langs: &'a [String],
464    /// Number of results to show (`--limit`).
465    want: usize,
466    /// Answer from the committed index without blocking on a (re)index (`--no-wait`).
467    no_wait: bool,
468    /// Cap on how long to wait for the index to warm (`--wait`); `None` = the
469    /// default/`RQ_WAIT_BUDGET_MS` budget.
470    wait: Option<Duration>,
471    open: bool,
472    web: bool,
473    all_repos: bool,
474    /// One of several queries sharing a run, so each row says which query it
475    /// answers — a single stream serving many questions is otherwise
476    /// unattributable.
477    batch: bool,
478    show: bool,
479    /// Asked from a position (`--anchor`); the anchor itself lives on the session.
480    anchored: bool,
481}
482
483/// Default action: search the index and print ranked results.
484/// Everything a search needs that doesn't depend on the query: the open store,
485/// the repo it's rooted in, the branch's changed files, and who that repo is.
486///
487/// Split out because it's the expensive half — opening the store, resolving the
488/// root, reading branch files, resolving identity — and none of it varies per
489/// query. One search builds one and drops it; a caller answering many can build
490/// it once. Deliberately *not* holding the warm decision: that one is entangled
491/// with the query (the indexer path-prioritises toward it) and belongs to a
492/// single search.
493struct Session {
494    store: Store,
495    cwd: Option<PathBuf>,
496    cwd_is_git: bool,
497    root: Option<PathBuf>,
498    active_paths: Vec<String>,
499    branch_refresh: Option<BranchRefresh>,
500    identity: Option<String>,
501    coverage: Option<String>,
502    /// Where the queries are asked from (`--anchor`), resolved once.
503    anchor: Option<crate::search::Anchor>,
504}
505
506impl Session {
507    /// Resolve the search context, or the exit code to fail with.
508    fn open(out: Output) -> std::result::Result<Session, ExitCode> {
509        let open_span = crate::profile::span("store open");
510        let store = match open_store() {
511            Ok(s) => s,
512            Err(e) => {
513                return Err(fail(
514                    out,
515                    Failure::Database,
516                    format_args!("rq: cannot open database: {e}"),
517                ));
518            }
519        };
520        drop(open_span);
521        let git_span = crate::profile::span("setup: git root");
522        let cwd = std::env::current_dir().ok();
523        let cwd_is_git = cwd.as_deref().is_some_and(crate::index::is_git_repo);
524
525        // Index relative to the repo ROOT, not wherever the search happens to run.
526        // Paths and the stored checkout root must be repo-root-relative and stable, or
527        // a search from a subdirectory would re-key the same repo under subdir-relative
528        // paths — and the deletion reconcile / staleness revalidation would then forget
529        // everything indexed from the root. Outside git, the root is just the cwd.
530        let root = cwd
531            .as_deref()
532            .map(|c| crate::index::repo_root(c).unwrap_or_else(|| c.to_path_buf()));
533        drop(git_span);
534
535        // Files you're changing on this feature branch (and their directory
536        // neighbors): the branch ranking boost, and the warm pass's priority set.
537        let mut branch_span = crate::profile::span("setup: branch files");
538        let (active_paths, branch_refresh, cached_cost) = match &root {
539            Some(c) if cwd_is_git => cached_branch_files(&store, c),
540            _ => (Vec::new(), None, None),
541        };
542        branch_span.note(|| {
543            let how = if branch_refresh.is_some() {
544                "cached, refreshing alongside"
545            } else {
546                "cached"
547            };
548            // The window is derived, not constant — say which one is in force,
549            // or a slow repo's backoff looks like rq ignoring stale state.
550            let ttl = branch_files_ttl(cached_cost);
551            format!("{} changed, {how} ({ttl}s window)", active_paths.len())
552        });
553        drop(branch_span);
554
555        // Resolve identity from the repo root, cache-first: looked up by checkout root
556        // (no `git remote` fork), falling back to git only the first time we see a
557        // repo. Computed even for non-git dirs so an explicitly `--index`ed one is
558        // still recognized as the current repo below.
559        let mut identity_span = crate::profile::span("setup: identity");
560        let identity = root.as_deref().map(|c| resolve_identity(&store, c));
561        let coverage = identity
562            .as_deref()
563            .and_then(|id| store.coverage_status(id).ok())
564            .flatten();
565        identity_span.note(|| coverage.as_deref().unwrap_or("unknown").to_string());
566        drop(identity_span);
567        Ok(Session {
568            store,
569            cwd,
570            cwd_is_git,
571            root,
572            active_paths,
573            branch_refresh,
574            identity,
575            coverage,
576            anchor: None,
577        })
578    }
579
580    /// Resolve `--anchor` against this session: the repo its file sits in (the
581    /// one we're in, when it's under our root), its path there, and what
582    /// encloses its line — read live if the index doesn't hold this version.
583    fn anchor_at(&self, spec: &AnchorSpec) -> crate::search::Anchor {
584        let _span = crate::profile::span("setup: anchor");
585        let here = self.cwd.clone().unwrap_or_else(|| PathBuf::from("."));
586        let abs = here.join(&spec.file);
587        let abs = abs.canonicalize().unwrap_or(abs);
588        let root = self
589            .root
590            .clone()
591            .filter(|r| abs.starts_with(r))
592            .or_else(|| crate::index::repo_root(&abs))
593            .or_else(|| abs.parent().map(PathBuf::from))
594            .unwrap_or(here);
595        let identity = resolve_identity(&self.store, &root);
596        let rel = abs
597            .strip_prefix(&root)
598            .map_or_else(|_| spec.file.to_string_lossy(), |r| r.to_string_lossy())
599            .into_owned();
600        let repo_id = self.store.repository_id(&identity).ok().flatten();
601        let defs = crate::index::current_definitions(&self.store, repo_id, &identity, &root, &rel);
602        crate::search::Anchor::new(identity, rel, spec.line, &defs)
603    }
604}
605
606/// A parsed `--anchor FILE:LINE[:COL]`.
607#[derive(Debug, Clone, PartialEq)]
608struct AnchorSpec {
609    file: PathBuf,
610    line: i64,
611}
612
613/// Parse `FILE:LINE` or `FILE:LINE:COL` (1-based), splitting from the right so
614/// a path holding a colon still parses.
615fn parse_anchor(s: &str) -> Result<AnchorSpec, String> {
616    let num = |t: &str| t.parse::<i64>().ok().filter(|n| *n > 0);
617    let bad = || format!("expected FILE:LINE[:COL], got {s:?}");
618    let (rest, last) = s.rsplit_once(':').ok_or_else(bad)?;
619    let last = num(last).ok_or_else(bad)?;
620    // `FILE:LINE:COL` when what precedes the last number is itself a number
621    let (file, line) = rest
622        .rsplit_once(':')
623        .and_then(|(file, line)| num(line).map(|line| (file, line)))
624        .unwrap_or((rest, last));
625    if file.is_empty() {
626        return Err(bad());
627    }
628    Ok(AnchorSpec {
629        file: PathBuf::from(file),
630        line,
631    })
632}
633
634/// Answer a stream of queries, one per line on stdin, in a single run.
635///
636/// Everything a query doesn't vary — the store, the repo, its identity, the
637/// branch's changed files — is resolved once and reused, which on a large repo
638/// is most of what a single lookup costs. Agents and scripts do runs of
639/// lookups; this is that shape.
640///
641/// A cold repo warms **once, up front, until complete** rather than answering
642/// each line from whatever happens to be indexed. Block-until-*answered*
643/// doesn't generalise to queries we haven't read yet — you can't prioritise
644/// toward them — so block-until-*complete* is the batch-shaped equivalent, and
645/// it keeps this file's own rule that correctness beats the first query's
646/// latency. `--no-wait` opts out, exactly as it does for one query, and any
647/// line the index can't yet answer says so with `status: "warming"`.
648fn cmd_batch(
649    cli: &Cli,
650    out: Output,
651    paths: &[String],
652    kinds: &[String],
653    langs: &[String],
654) -> ExitCode {
655    if out == Output::Json {
656        return fail(
657            out,
658            Failure::Usage,
659            format_args!(
660                "rq: --json can't frame a stream of queries — use --ndjson (-J), \
661             where each line carries the query it answers"
662            ),
663        );
664    }
665    if cli.open || cli.web || cli.show {
666        return fail(
667            out,
668            Failure::Usage,
669            format_args!(
670                "rq: --open, --web and --show act on a single result, not a stream of queries"
671            ),
672        );
673    }
674
675    use std::io::BufRead;
676    let queries: Vec<String> = std::io::stdin()
677        .lock()
678        .lines()
679        .map_while(std::result::Result::ok)
680        .map(|l| l.trim().to_string())
681        .filter(|l| !l.is_empty())
682        .collect();
683    // Nothing on stdin isn't a batch — it's a bare invocation that happens to
684    // run without a terminal (a script, a test harness, stdin from /dev/null).
685    // Treat it the way `rq` with no arguments is always treated.
686    if queries.is_empty() {
687        let _ = Cli::command().print_long_help();
688        return ExitCode::SUCCESS;
689    }
690
691    let mut session = match Session::open(out) {
692        Ok(s) => s,
693        Err(code) => return code,
694    };
695    session.anchor = cli.anchor.as_ref().map(|a| session.anchor_at(a));
696
697    // Warm to completion before answering anything, so a cold repo doesn't
698    // return a page of misses that only mean "not indexed yet".
699    if !cli.no_wait
700        && session.coverage.as_deref() != Some("complete")
701        && let Some(root) = session.root.clone()
702    {
703        {
704            let budget = cli.wait.unwrap_or_else(wait_budget);
705            crate::trace!(
706                "batch: warming {} queries' worth of index first",
707                queries.len()
708            );
709            let active = session.active_paths.clone();
710            let _ = crate::index::index_budgeted(&mut session.store, &root, &active, budget, None);
711            session.coverage = session
712                .identity
713                .as_deref()
714                .and_then(|id| session.store.coverage_status(id).ok())
715                .flatten();
716        }
717    }
718
719    let mut worst = ExitCode::SUCCESS;
720    let mut any_hit = false;
721    for query in &queries {
722        let code = cmd_search(
723            &mut session,
724            &SearchArgs {
725                query,
726                explain: cli.explain,
727                out,
728                paths,
729                kinds,
730                langs,
731                want: requested_limit(cli.limit),
732                // The warm happened above, once. Per-query warming would undo
733                // the point of batching, and block-until-answered is meaningless
734                // when the queries were all read up front.
735                no_wait: true,
736                wait: cli.wait,
737                open: false,
738                web: false,
739                all_repos: cli.all_repos,
740                show: false,
741                batch: true,
742                anchored: cli.anchor.is_some(),
743            },
744        );
745        if code == ExitCode::SUCCESS {
746            any_hit = true;
747        } else {
748            worst = code;
749        }
750    }
751    // The batch ran; per-line `status` carries each query's outcome. Only a
752    // wholly fruitless batch reports failure, mirroring one query's contract.
753    if any_hit { ExitCode::SUCCESS } else { worst }
754}
755
756fn cmd_search(session: &mut Session, args: &SearchArgs) -> ExitCode {
757    let &SearchArgs {
758        query,
759        out,
760        want,
761        no_wait,
762        wait,
763        open,
764        web,
765        all_repos,
766        show,
767        ..
768    } = args;
769    // `--wait DUR` overrides the wait budget for this call; `--wait 0` (or
770    // `--no-wait`) means don't block or warm in-process at all.
771    let wait_budget = wait.unwrap_or_else(wait_budget);
772    let no_wait = no_wait || wait_budget.is_zero();
773    // post-filters (--path, --kind, --lang) need headroom before the cutoff so a
774    // filtered-in result isn't lost to the top-N truncation
775    let limit = if args.paths.is_empty() && args.kinds.is_empty() && args.langs.is_empty() {
776        want
777    } else {
778        want.saturating_mul(20).max(PATH_HEADROOM)
779    };
780    let _timer = crate::trace::Timer::start("search done");
781    let t_setup = std::time::Instant::now();
782    // Brackets the warm decision as well as the session, so it outlives both.
783    let setup_span = crate::profile::span("setup");
784    // Borrowed field-by-field so the body reads the same as when it owned them,
785    // while the session itself outlives this call and can answer again.
786    let Session {
787        store,
788        cwd,
789        cwd_is_git,
790        root,
791        active_paths,
792        branch_refresh,
793        identity,
794        coverage,
795        anchor,
796    } = session;
797    let cwd_is_git = *cwd_is_git;
798
799    // Opportunistic indexing (Layer 5), time-bounded so the first query in a
800    // large repo never blocks on a full walk. We may warm a git work tree (safe
801    // to auto-discover) *or* any dir we already track — one earns tracking by
802    // being explicitly `--index`ed, which opts a non-git dir in. We never warm
803    // an unknown non-git dir (don't walk a random directory). A subtree index
804    // (`--index --path …`) is a seed, not a fence: coverage stays `warming`, so
805    // warming continues over the rest of the repo from here.
806    let known = coverage.is_some();
807    let warming_ok = cwd_is_git || known;
808    if crate::trace::enabled() {
809        crate::trace!(
810            "query {query:?}: root={} identity={} coverage={} warming_ok={warming_ok} active={}",
811            root.as_deref().map_or("?".into(), crate::trace::abbrev),
812            identity.as_deref().unwrap_or("none"),
813            coverage.as_deref().unwrap_or("none"),
814            active_paths.len(),
815        );
816    }
817    let repo_span = crate::profile::span("setup: repo state");
818    let repo_id = |store: &Store| {
819        identity
820            .as_deref()
821            .and_then(|id| store.repository_id(id).ok().flatten())
822    };
823    let mut current = repo_id(store);
824    // Default: scope results to the current repo so a search never leaks
825    // another repo's definitions. `--all-repos` searches everything.
826    let scope = |current| Scope::new(all_repos, warming_ok, current);
827    let ctx = crate::search::Context {
828        active: crate::search::ActiveFiles::new(active_paths.clone()),
829        anchor: anchor.clone(),
830    };
831
832    drop(repo_span);
833    let warm_span = crate::profile::span("setup: warm decision");
834
835    // Warm the index on a background thread (its own connection — WAL lets it
836    // write while we read) whenever there's work: a not-yet-complete repo, or a
837    // complete one changed since it was indexed. The search below reads whatever
838    // it has committed so far. With detach on (the default), this in-process
839    // warm only serves *this* answer — leftover work goes to a detached child
840    // after results print, so the shell never waits on it.
841    let warm_budget = if warm_detach_enabled() {
842        answer_warm_budget()
843    } else {
844        answer_warm_budget() + deferred_warm_budget()
845    };
846    let was_warming = coverage.as_deref() != Some("complete");
847
848    // On a complete repo the only question left is whether the worktree moved
849    // since it was indexed — and answering it forks `git status`, which on a
850    // large worktree is most of a query's cost. It decides nothing this answer
851    // depends on: with `was_warming` false, `block` and `polling` below are
852    // false too, the search reads the committed index, and `revalidate_top`
853    // guarantees the freshness of what we print. So start it alongside the
854    // search and collect it in `settle_warm` once results are out.
855    //
856    // A still-warming repo never ran this check at all — the `||` short-circuit
857    // saw to that — so its path here is unchanged.
858    let indexed_head = (!was_warming)
859        .then(|| current.and_then(|id| store.indexed_head(id).ok().flatten()))
860        .flatten();
861    // Whether the worktree moved is a property of the repo, not of the query,
862    // so a batch asks once (up front) instead of forking `git status` per line.
863    let staleness = (!was_warming && warming_ok && !args.batch)
864        .then(|| root.clone())
865        .flatten()
866        .map(|c| {
867            if warm_detach_enabled() {
868                Staleness::Deferred(c, indexed_head)
869            } else {
870                Staleness::Running(std::thread::spawn(move || {
871                    worktree_edits(&c, indexed_head.as_deref())
872                }))
873            }
874        });
875    // Only a repo that's still warming warms *before* the answer now; a
876    // complete-but-edited one is reindexed by `settle_warm` afterwards.
877    let want_warm = warming_ok && was_warming && root.is_some();
878
879    // Block-until-answered on a cold/partial repo. A bounded warm exists so a
880    // query never hangs, but on a *huge, cold* repo it can expire before the
881    // symbol is indexed — turning a real hit into a false "no matches". Since
882    // correctness beats the first query's latency (and once warm the repo answers
883    // fast), we keep indexing until the answer appears or the repo is fully
884    // indexed — for humans *and* programs alike. Small/medium repos finish inside
885    // the normal budget and are unaffected; only a genuinely large cold repo
886    // waits, and only once.
887    // `--no-wait`: a scripted/agent caller that would rather answer from the
888    // committed index right now than block up to the wait budget while a
889    // background rebuild rewrites the index. It suppresses the block-until-answered
890    // escalation *and* the in-process warm (no lock contention, no join) — leftover
891    // warming still detaches below, so the index keeps improving for next time.
892    let block = want_warm && was_warming && !no_wait;
893    // A human at a plain-text terminal also gets a live progress heads-up and a
894    // graceful Ctrl-C; piped/`--json` callers (agents, scripts) block silently and
895    // are bounded by a wait budget instead, since there's nothing to draw to and
896    // no one to interrupt.
897    let progress_ui = block && show_progress(out, stderr_interactive());
898    let indexer_budget = if block { wait_budget } else { warm_budget };
899    if progress_ui {
900        install_interrupt_handler();
901    }
902
903    // `warm_done` lets the poll stop the instant the indexer finishes — so a miss
904    // on a small repo returns as soon as it's indexed, not at the deadline.
905    let warm_done = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
906    let indexer = (want_warm && root.is_some() && !no_wait).then(|| {
907        crate::trace!(
908            "background warm ({indexer_budget:?}, block={block}, progress_ui={progress_ui}, {} jobs)",
909            crate::index::parse_jobs()
910        );
911        let root = root.clone().expect("checked");
912        let active = active_paths.clone();
913        let q = query.to_string();
914        let warm_done = std::sync::Arc::clone(&warm_done);
915        std::thread::spawn(move || {
916            if let Ok(mut idx) = open_store() {
917                // path-prioritize toward the query so the relevant file indexes first
918                let _ = if block {
919                    // the abort flag (`INTERRUPTED`) lets a Ctrl-C, a wait timeout,
920                    // or an early answer stop the pass without losing committed work
921                    crate::index::index_budgeted_cancellable(
922                        &mut idx,
923                        &root,
924                        &active,
925                        indexer_budget,
926                        Some(&q),
927                        &INTERRUPTED,
928                    )
929                } else {
930                    crate::index::index_budgeted(&mut idx, &root, &active, indexer_budget, Some(&q))
931                };
932            }
933            warm_done.store(true, std::sync::atomic::Ordering::Relaxed);
934        })
935    });
936
937    // Poll while a cold/partial repo warms. Don't print the first hit off a sparse
938    // index — a fuzzy or path match can be wrong once more is indexed. Hold until a
939    // *high-confidence* (exact or prefix name) match appears; otherwise keep
940    // building until the index is complete (a "no matches" is then trustworthy), a
941    // wait deadline passes, or — interactively — Ctrl-C. A human sees a progress
942    // line once the pause is noticeable.
943    crate::trace!(
944        "setup (open + repo detect + warm decision): {} ms",
945        t_setup.elapsed().as_millis()
946    );
947    let poll_start = std::time::Instant::now();
948    // Deadline: an interactive block waits unbounded (Ctrl-C escapes); a
949    // programmatic block waits out the wait budget; a non-block (complete repo)
950    // keeps the original fast answer budget.
951    let deadline = if progress_ui {
952        None
953    } else if block {
954        Some(poll_start + wait_budget)
955    } else {
956        Some(poll_start + answer_warm_budget())
957    };
958    drop(warm_span);
959    let polling = indexer.is_some() && was_warming;
960    // Everything before the first search: resolving the repo root, checking
961    // coverage, deciding whether to warm. It runs on every query, so it counts
962    // toward the first-answer budget even though no searching happened yet.
963    drop(setup_span);
964    let mut query_span = crate::profile::span("query");
965    let label = repo_label(root.as_deref());
966    let mut drew_progress = false;
967    let mut last_draw = poll_start;
968    // Rank one deeper than asked: confidence is a comparison against the
969    // runner-up, so normalizing over the returned window made `-l 1` read 1.0
970    // every time — and that reading is what gates `--show`.
971    let rank_limit = limit.max(2);
972    let mut total;
973    let mut hits = loop {
974        // the warm registers a repo it's indexing for the first time
975        if current.is_none() {
976            current = repo_id(store);
977        }
978        match scope(current).search(store, query, current, &ctx, rank_limit) {
979            Ok(m) => {
980                total = m.total;
981                let h = m.hits;
982                let confident = h.first().is_some_and(|hit| {
983                    hit.features
984                        .iter()
985                        .any(|f| matches!(f.name, "exact" | "prefix"))
986                });
987                let warm_finished = warm_done.load(std::sync::atomic::Ordering::Relaxed);
988                let stopped = INTERRUPTED.load(std::sync::atomic::Ordering::Relaxed);
989                let timed_out = deadline.is_some_and(|d| std::time::Instant::now() >= d);
990                if !polling || confident || warm_finished || stopped || timed_out {
991                    break h;
992                }
993                if progress_ui
994                    && poll_start.elapsed() >= HEADS_UP_DELAY
995                    && last_draw.elapsed() >= PROGRESS_REDRAW
996                {
997                    draw_progress(store, identity.as_deref(), &label);
998                    drew_progress = true;
999                    last_draw = std::time::Instant::now();
1000                }
1001            }
1002            Err(e) => {
1003                if let Some(h) = indexer {
1004                    let _ = h.join();
1005                }
1006                return fail(out, Failure::Database, format_args!("rq: {e}"));
1007            }
1008        }
1009        std::thread::sleep(POLL_INTERVAL);
1010    };
1011    query_span.note(|| {
1012        if polling {
1013            "polled a warming index".to_string()
1014        } else {
1015            String::new()
1016        }
1017    });
1018    drop(query_span);
1019    if drew_progress {
1020        clear_progress();
1021    }
1022    // Captured before we self-cancel below, so it reflects only a *user's* Ctrl-C.
1023    let interrupted = INTERRUPTED.load(std::sync::atomic::Ordering::Relaxed);
1024
1025    let here = identity
1026        .as_deref()
1027        .zip(root.as_deref())
1028        .map(|(identity, root)| Here { identity, root });
1029    // Staleness: revalidate the files behind the top hits; re-rank once if changed.
1030    if !hits.is_empty()
1031        && revalidate_top(store, &hits, here)
1032        && let Ok(m) = scope(current).search(store, query, current, &ctx, rank_limit)
1033    {
1034        total = m.total;
1035        hits = m.hits;
1036    }
1037
1038    // Untracked non-git dir — nothing persisted, no warmer running — so scan it
1039    // live in-memory (substring, then fuzzy) and blend with whatever the index
1040    // gave. The only non-persisting scan left.
1041    let mut live_scan = None;
1042    if !hits.iter().any(strong)
1043        && indexer.is_none()
1044        && coverage.is_none()
1045        && let Some(root) = &root
1046    {
1047        let (tail, cost) = live_fallback(root, query, rank_limit, &ctx);
1048        hits = crate::search::merge(hits, tail, rank_limit);
1049        total = total.max(hits.len());
1050        live_scan = Some(cost);
1051    }
1052
1053    apply_gates(query, &mut hits);
1054    apply_post_filters(args, cwd.as_deref(), root.as_deref(), &mut hits);
1055    // A filtered search reports what survived the filter — that's the set the
1056    // caller asked about — counted before any cut. The runner-up stays for
1057    // confidence; `--limit` applies once that's assigned.
1058    if !args.paths.is_empty() || !args.kinds.is_empty() || !args.langs.is_empty() {
1059        total = hits.len();
1060        hits.truncate(want.max(2));
1061    }
1062
1063    if hits.is_empty() {
1064        // Stop a still-running block so the join is prompt, then settle coverage.
1065        if block {
1066            INTERRUPTED.store(true, std::sync::atomic::Ordering::Relaxed);
1067        }
1068        if let Some(h) = indexer {
1069            let _ = h.join();
1070        }
1071        // A miss against a *complete* index is definitive (the symbol isn't
1072        // there); against a still-warming one it's only "not yet". Distinguish
1073        // them so a caller — agent or script — isn't misled into thinking the
1074        // symbol is absent when the index simply hasn't reached it. `--no-wait`
1075        // returns without blocking, so its miss is judged the same way — an
1076        // incomplete index yields `warming` (exit 2, "retry"), not a false absence.
1077        let mut incomplete = (block || no_wait)
1078            && identity
1079                .as_deref()
1080                .and_then(|id| store.coverage_status(id).ok().flatten())
1081                .as_deref()
1082                != Some("complete");
1083        // a "not yet" miss leaves work behind — reindex an edited worktree and
1084        // let a detached child keep warming, so the retry lands on a better
1085        // index. This is the path a just-added symbol takes, so it has to do
1086        // the same settling the render path does.
1087        // A worktree that has moved since we indexed it makes this miss
1088        // provisional, not definitive: the symbol may be in an edit the
1089        // detached warm hasn't caught up with. Say "warming" (exit 2, retry)
1090        // rather than "no match" (exit 1, absent) — a just-added symbol is
1091        // exactly this case, and a confident no is the wrong answer to it.
1092        incomplete |= settle_warm(
1093            store,
1094            staleness,
1095            false,
1096            was_warming,
1097            warming_ok,
1098            root.as_deref(),
1099            active_paths,
1100            query,
1101            warm_budget,
1102            no_wait,
1103            identity.as_deref(),
1104        );
1105        // A named scope that matched nothing is a different miss from a name
1106        // that doesn't exist: re-run on the bare leaf to tell them apart, and
1107        // say where the name actually lives. Only on the miss path, so a normal
1108        // search never pays for it.
1109        let elsewhere = match scope(current) {
1110            Scope::Nothing => None,
1111            Scope::All => crate::search::scope_miss_owner(store, query, current, None, &ctx),
1112            Scope::Repo(id) => {
1113                crate::search::scope_miss_owner(store, query, current, Some(id), &ctx)
1114            }
1115        };
1116        if let (Some(cost), Some(root)) = (&live_scan, root.as_deref()) {
1117            note_live_scan(root, cost, false);
1118        }
1119        let code = no_match_code(out, query, interrupted, incomplete, elsewhere.as_deref());
1120        // Counted after the answer, and only here: whether this was a
1121        // definitive miss or a not-ready one is only known on this path, and
1122        // counting them as one number overstates how often rq truly finds nothing.
1123        let _span = crate::profile::span("after: record usage");
1124        record_usage(
1125            store,
1126            args,
1127            if incomplete { "warming" } else { "miss" },
1128            coverage.as_deref(),
1129            false,
1130        );
1131        return code;
1132    }
1133
1134    // Live if any result the caller sees came from the scan; `hits` is
1135    // already in its final order, so the first `want` are the ones shown.
1136    let live = hits
1137        .iter()
1138        .take(want)
1139        .any(|h| h.source == crate::search::Source::Live);
1140    if let (Some(cost), Some(root)) = (&live_scan, root.as_deref()) {
1141        note_live_scan(root, cost, live);
1142    }
1143
1144    // A process's first write can stall for milliseconds on a busy machine
1145    // (DECISIONS D13), so the ranked list counts itself once it has printed.
1146    // --show/--open/--web leave by their own exits (--open `exec`s), so they
1147    // count here; a --show that falls through to the list was already counted.
1148    let counted_early = show || open || web;
1149    if counted_early {
1150        let _span = crate::profile::span("record usage");
1151        record_usage(store, args, "hit", coverage.as_deref(), live);
1152    }
1153
1154    // Confidence first, while the runner-up is still in hand, then cut to the
1155    // window the caller asked for — `--show`'s gate reads this, so measuring it
1156    // over an already-truncated list made `-l 1` unconditionally confident.
1157    attach_confidence(&mut hits);
1158    hits.truncate(want);
1159    let total = total.max(hits.len());
1160    for hit in &mut hits {
1161        hit.total = total;
1162        if args.explain {
1163            hit.explain = Some(
1164                hit.features
1165                    .iter()
1166                    .map(|f| (f.name.to_string(), f.reported()))
1167                    .collect(),
1168            );
1169        }
1170    }
1171
1172    // Attach each result's definition line (e.g. `def perform(refund)`) — shown
1173    // in text output and carried in JSON. Cheap: only the displayed results.
1174    let _signatures_span = crate::profile::span("signatures");
1175    for hit in &mut hits {
1176        let root = hit_root(store, &hit.repo_identity, &hit.file, here);
1177        hit.signature = root
1178            .as_deref()
1179            .and_then(|r| read_signature(&r.join(&hit.file), hit.line));
1180        hit.root = root.map(|r| r.to_string_lossy().into_owned());
1181    }
1182    drop(_signatures_span);
1183
1184    // --show: print the top hit's full source when confident; otherwise fall
1185    // through to the normal ranked list (rq won't dump a body it isn't sure of).
1186    if show && let Some(code) = show_top_definition(&mut hits, query, out) {
1187        return code;
1188    }
1189
1190    // --open/--web: pick the best match (prompting on a TTY with several) and
1191    // hand off to the editor or browser.
1192    // Returns before the normal print / warm-join — opening should be snappy,
1193    // and a launcher `exec`s.
1194    if open || web {
1195        return finish_open(store, &hits, current, root.as_deref(), web);
1196    }
1197
1198    if let Some(code) = render_hits(args, &hits) {
1199        return code;
1200    }
1201
1202    // The budget's number. `total` adds the bookkeeping below, which runs
1203    // after results are out but still before the process exits.
1204    crate::profile::mark("first answer");
1205
1206    // Before the warm child is spawned below, whose own writes it would
1207    // otherwise queue behind.
1208    if !counted_early {
1209        let _span = crate::profile::span("after: record usage");
1210        record_usage(store, args, "hit", coverage.as_deref(), live);
1211    }
1212
1213    // Collect the refresh started back at setup. It ran alongside the search
1214    // rather than after it, so by now it has usually finished — and it only
1215    // ever feeds the *next* query, never this one's ranking, so waiting on it
1216    // can't reorder what was just printed.
1217    // Taken, not borrowed: the refresh is one-shot, and a session answering
1218    // several queries must not re-store a result it already consumed.
1219    if let Some(refresh) = branch_refresh.take() {
1220        let _span = crate::profile::span("after: branch refresh");
1221        refresh.store(store);
1222    }
1223
1224    // Results are out; stop the in-process warm (it persists as it goes, so a
1225    // cut pass keeps everything parsed) and join it — then hand whatever's left
1226    // to a detached child, which finishes coverage with a budget no foreground
1227    // query could afford. The shell only ever waits on the answer.
1228    if block {
1229        INTERRUPTED.store(true, std::sync::atomic::Ordering::Relaxed);
1230    }
1231    if let Some(h) = indexer {
1232        let _ = h.join();
1233    }
1234    let _ = settle_warm(
1235        store,
1236        staleness,
1237        true,
1238        was_warming,
1239        warming_ok,
1240        root.as_deref(),
1241        active_paths,
1242        query,
1243        warm_budget,
1244        no_wait,
1245        identity.as_deref(),
1246    );
1247
1248    ExitCode::SUCCESS
1249}
1250
1251/// Re-exec a detached warm child when this query's warming didn't finish the
1252/// job. No-op when detach is off, nothing was warming, or coverage completed.
1253fn maybe_detach_warm(
1254    store: &Store,
1255    want_warm: bool,
1256    changed: bool,
1257    root: Option<&std::path::Path>,
1258    identity: Option<&str>,
1259) {
1260    if !warm_detach_enabled() || !want_warm {
1261        return;
1262    }
1263    let (Some(root), Some(id)) = (root, identity) else {
1264        return;
1265    };
1266    // Coverage measures breadth, not freshness — an edit never demotes it. So
1267    // "complete" alone isn't done; it's done only if the worktree also hasn't
1268    // moved since we indexed it.
1269    if !changed && store.coverage_status(id).ok().flatten().as_deref() == Some("complete") {
1270        return; // the in-process pass finished the job
1271    }
1272    spawn_detached_warm(root);
1273}
1274
1275/// Spawn `rq --warm <root>` fully detached: null stdio and its own process
1276/// group, so it survives this process and a later Ctrl-C in the terminal
1277/// can't reach it. The child nices itself and is single-flighted per repo.
1278fn spawn_detached_warm(root: &std::path::Path) {
1279    use std::os::unix::process::CommandExt;
1280    let Ok(exe) = std::env::current_exe() else {
1281        return;
1282    };
1283    let mut cmd = std::process::Command::new(exe);
1284    cmd.arg("--warm")
1285        .arg(root)
1286        .stdin(std::process::Stdio::null())
1287        .stdout(std::process::Stdio::null())
1288        .stderr(std::process::Stdio::null())
1289        .process_group(0);
1290    match cmd.spawn() {
1291        Ok(child) => crate::trace!(
1292            "background warm (detached): pid {} for {}",
1293            child.id(),
1294            crate::trace::abbrev(root)
1295        ),
1296        Err(e) => crate::trace!("detached warm failed to spawn: {e}"),
1297    }
1298}
1299
1300/// How long a warm lock is trusted without a liveness hit — past this, a
1301/// stamp is a crashed warmer's leftover and a new child takes over.
1302const WARM_LOCK_TTL_SECS: i64 = 600;
1303
1304/// How long a warm child's "nothing moved" verdict spares later hits the spawn,
1305/// while git's own state still matches it. Only an unstaged edit to a tracked
1306/// file can hide inside this window (it touches nothing in `.git`); a miss still
1307/// checks inline and a top hit's file is revalidated on read, so what's left is
1308/// a changed file that isn't among the hits, picked up by the first hit after.
1309fn warm_recheck_window() -> Duration {
1310    env_budget("RQ_WARM_RECHECK_MS", 10_000)
1311}
1312
1313/// Whether a warm child found this worktree unchanged recently enough, with
1314/// git's state untouched since, that spawning another would find nothing.
1315fn recently_verified(
1316    store: &Store,
1317    identity: Option<&str>,
1318    root: &std::path::Path,
1319    indexed_head: Option<&str>,
1320) -> bool {
1321    let _span = crate::profile::span("after: warm recently verified?");
1322    let (Some(id), Some(stamp)) = (
1323        identity,
1324        indexed_head.and_then(|h| crate::index::git_state_stamp(root, h)),
1325    ) else {
1326        return false;
1327    };
1328    let Ok(Some((seen, at))) = store.warm_verified(id) else {
1329        return false;
1330    };
1331    let age = now_unix().saturating_sub(at);
1332    seen == stamp && (0..warm_recheck_window().as_secs() as i64).contains(&age)
1333}
1334
1335/// `rq --warm [PATH]`: the detached child a search re-execs after printing —
1336/// finishes warming the repo's index in the background. Niced so it stays out
1337/// of the foreground's way; single-flighted per repo so a burst of queries
1338/// runs at most one warmer. Safe (and boring) to run by hand.
1339fn cmd_warm(path: Option<&str>) -> ExitCode {
1340    // Stay out of the way: drop scheduling priority, and throttle disk I/O on
1341    // macOS. Best-effort — a failure just means a less-polite warm.
1342    #[cfg(target_os = "macos")]
1343    unsafe extern "C" {
1344        // <sys/resource.h>; not in the libc crate. Args below:
1345        // IOPOL_TYPE_DISK=0, IOPOL_SCOPE_PROCESS=0, IOPOL_THROTTLE=3.
1346        fn setiopolicy_np(
1347            iotype: libc::c_int,
1348            scope: libc::c_int,
1349            policy: libc::c_int,
1350        ) -> libc::c_int;
1351    }
1352    unsafe {
1353        libc::nice(10);
1354        #[cfg(target_os = "macos")]
1355        setiopolicy_np(0, 0, 3);
1356    }
1357    let mut store = match open_store() {
1358        Ok(s) => s,
1359        Err(_) => return ExitCode::from(Failure::Database.exit_code()),
1360    };
1361    let start = path
1362        .map(PathBuf::from)
1363        .or_else(|| std::env::current_dir().ok())
1364        .unwrap_or_else(|| PathBuf::from("."));
1365    let root = crate::index::repo_root(&start).unwrap_or(start);
1366    let identity = resolve_identity(&store, &root);
1367
1368    // Single-flight: if another live rq is already warming this repo, bow out.
1369    // A dead pid or a stale stamp is a crashed warmer — take over. A claim that
1370    // can't be made at all means another writer is busy; the next search retries.
1371    let held = |pid: u32, ts: i64| {
1372        let alive = unsafe { libc::kill(pid as libc::pid_t, 0) } == 0;
1373        alive && now_unix() - ts < WARM_LOCK_TTL_SECS
1374    };
1375    if !store
1376        .claim_warm_lock(&identity, std::process::id(), held)
1377        .unwrap_or(false)
1378    {
1379        return ExitCode::SUCCESS;
1380    }
1381
1382    // A search on a complete repo hands us the staleness check rather than
1383    // wait on `git status` itself, so most runs end here: nothing moved.
1384    if store.coverage_status(&identity).ok().flatten().as_deref() == Some("complete") {
1385        let head = store
1386            .repository_id(&identity)
1387            .ok()
1388            .flatten()
1389            .and_then(|id| store.indexed_head(id).ok().flatten());
1390        // The window runs from before `git status`, so an edit made during it
1391        // still falls inside. The stamp is read after: status may rewrite
1392        // `.git/index` itself, and a HEAD that moved meanwhile yields none.
1393        let checked_at = now_unix();
1394        let edits = worktree_edits(&root, head.as_deref());
1395        if !changed_since_index(&store, Some(&identity), Some(&root), edits) {
1396            crate::trace!("warm: unchanged since indexed, nothing to do");
1397            // but a database from before the name index has none yet
1398            if let Some(id) = store.repository_id(&identity).ok().flatten() {
1399                let _ = store.maintain_name_index(id);
1400            }
1401            if let Some(stamp) = head
1402                .as_deref()
1403                .and_then(|h| crate::index::git_state_stamp(&root, h))
1404            {
1405                let _ = store.set_warm_verified(&identity, &stamp, checked_at);
1406            }
1407            let _ = store.clear_warm_lock(&identity);
1408            return ExitCode::SUCCESS;
1409        }
1410    }
1411
1412    // Sweep until coverage completes, the budget runs out, or a pass stops
1413    // making progress (each pass converges — mtime-skips what's done).
1414    let deadline = std::time::Instant::now() + warm_bg_budget();
1415    let active = crate::index::branch_changed_files(&root);
1416    loop {
1417        let remaining = deadline.saturating_duration_since(std::time::Instant::now());
1418        if remaining.is_zero() {
1419            break;
1420        }
1421        let stats = match crate::index::index_budgeted(&mut store, &root, &active, remaining, None)
1422        {
1423            Ok(s) => s,
1424            Err(_) => break,
1425        };
1426        if store.coverage_status(&identity).ok().flatten().as_deref() == Some("complete")
1427            || stats.files_indexed == 0
1428        {
1429            break;
1430        }
1431    }
1432    let _ = store.clear_warm_lock(&identity);
1433    ExitCode::SUCCESS
1434}
1435
1436/// Live in-memory scan of an untracked (non-git, never-indexed) dir: substring
1437/// pre-filtered first, then the unfiltered fuzzy retry. Persists nothing.
1438fn live_fallback(
1439    root: &std::path::Path,
1440    query: &str,
1441    limit: usize,
1442    ctx: &crate::search::Context,
1443) -> (Vec<crate::search::Hit>, LiveCost) {
1444    let start = std::time::Instant::now();
1445    let deadline = start + live_fallback_budget();
1446    let mut files = 0;
1447    let mut scan = |prefilter| {
1448        let mut span = crate::profile::span(if prefilter {
1449            "live scan: prefiltered"
1450        } else {
1451            "live scan: unfiltered"
1452        });
1453        let found = crate::search::live_search(
1454            root,
1455            query,
1456            limit,
1457            &HashSet::new(),
1458            Some(deadline),
1459            prefilter,
1460            ctx,
1461        );
1462        span.note(|| format!("{} files", found.files));
1463        files += found.files;
1464        found.hits
1465    };
1466    let mut hits = scan(true);
1467    if hits.is_empty() {
1468        hits = scan(false);
1469    }
1470    let cost = LiveCost {
1471        files,
1472        elapsed: start.elapsed(),
1473    };
1474    (hits, cost)
1475}
1476
1477/// What a live scan cost, for its `-v` note.
1478struct LiveCost {
1479    files: usize,
1480    elapsed: Duration,
1481}
1482
1483/// Under `-v`, say that a live scan ran and whether the answer came from it —
1484/// otherwise invisible, since a live result prints like an indexed one.
1485fn note_live_scan(root: &std::path::Path, cost: &LiveCost, answered: bool) {
1486    if !crate::trace::enabled() {
1487        return;
1488    }
1489    let budget = live_fallback_budget();
1490    let files = match cost.files {
1491        1 => "1 file".to_string(),
1492        n => format!("{n} files"),
1493    };
1494    let what = if answered {
1495        "answered from a live scan of"
1496    } else {
1497        "no answer from a live scan of"
1498    };
1499    let cut = if cost.elapsed >= budget {
1500        " (stopped at the budget)"
1501    } else {
1502        ""
1503    };
1504    crate::trace!(
1505        "{what} {}: {files} in {} ms, budget {} ms{cut}",
1506        crate::trace::abbrev(root),
1507        cost.elapsed.as_millis(),
1508        budget.as_millis(),
1509    );
1510}
1511
1512/// A high-confidence name match: exact or prefix (not fuzzy/path-only).
1513fn strong(h: &crate::search::Hit) -> bool {
1514    h.features
1515        .iter()
1516        .any(|f| matches!(f.name, "exact" | "prefix"))
1517}
1518
1519/// The result-quality gates, in order:
1520/// - relevance: when the query lands a real name match (exact or prefix), drop
1521///   the scattered fuzzy / path-only near-matches — they're noise next to a
1522///   solid hit, and rq favors fewer, better results. A purely-fuzzy query (no
1523///   exact/prefix anywhere) keeps its matches.
1524/// - scope: a qualified query (`Foo::Bar#baz`) that lands inside the named
1525///   scope keeps only the in-scope results; if none match, the others stay
1526///   (the definition may live elsewhere).
1527fn apply_gates(query: &str, hits: &mut Vec<crate::search::Hit>) {
1528    if hits.iter().any(strong) {
1529        hits.retain(strong);
1530    }
1531    crate::search::apply_scope_gate(query, hits);
1532}
1533
1534/// Post-filters: keep only results under a `--path` dir, of a `--kind`, and/or
1535/// in a `--lang`.
1536fn apply_post_filters(
1537    args: &SearchArgs,
1538    cwd: Option<&std::path::Path>,
1539    root: Option<&std::path::Path>,
1540    hits: &mut Vec<crate::search::Hit>,
1541) {
1542    if !args.paths.is_empty() {
1543        // --path values may be absolute or cwd-relative; stored files are
1544        // repo-root-relative, so normalize before prefix-matching or an
1545        // absolute path would silently filter everything out.
1546        let here = cwd.map_or_else(|| PathBuf::from("."), PathBuf::from);
1547        let base = root.map_or_else(|| here.clone(), PathBuf::from);
1548        let norm: Vec<String> = args
1549            .paths
1550            .iter()
1551            .map(|p| repo_relative(&base, &here, p))
1552            .collect();
1553        hits.retain(|h| under_any(&h.file, &norm));
1554    }
1555    if !args.kinds.is_empty() {
1556        hits.retain(|h| args.kinds.iter().any(|k| k == &h.kind));
1557    }
1558    if !args.langs.is_empty() {
1559        hits.retain(|h| args.langs.iter().any(|l| l == &h.language));
1560    }
1561}
1562
1563/// Report a miss and pick its exit code. Structured callers get a reason, not
1564/// a bare `[]`/empty: `warming` (retry — index incomplete), `interrupted` (a
1565/// stopped block), or `no_match` (definitive). Text keeps its human message.
1566/// Exit 2 = indeterminate (index incomplete), 1 = a definitive miss — both
1567/// non-zero, so `rq … && …` still reads as "found something".
1568fn no_match_code(
1569    out: Output,
1570    query: &str,
1571    interrupted: bool,
1572    incomplete: bool,
1573    // Where the unqualified name *does* live, when a scope was named and
1574    // nothing in it matched. "Not in that scope" and "no such name" are
1575    // different answers and the second is the less useful one.
1576    elsewhere: Option<&str>,
1577) -> ExitCode {
1578    let status = if interrupted {
1579        "interrupted"
1580    } else if incomplete {
1581        "warming"
1582    } else if elsewhere.is_some() {
1583        "scope_not_found"
1584    } else {
1585        "no_match"
1586    };
1587    match out {
1588        Output::Json | Output::Ndjson => {
1589            let mut obj = serde_json::json!({ "status": status, "query": query });
1590            if let Some(found_in) = elsewhere {
1591                obj["found_in"] = serde_json::json!(found_in);
1592            }
1593            let _ = emit_json(out, &obj); // the exit code below carries the miss
1594        }
1595        Output::Text if interrupted => {
1596            eprintln!("rq: indexing interrupted — run again to finish")
1597        }
1598        Output::Text if incomplete => eprintln!(
1599            "rq: still indexing — no match for {query:?} yet (run again, or `rq --index` to finish)"
1600        ),
1601        Output::Text if elsewhere.is_some() => eprintln!(
1602            "rq: nothing matching {query:?} — that name is defined under {}",
1603            elsewhere.unwrap_or_default()
1604        ),
1605        Output::Text => eprintln!("no matches for {query:?}"),
1606    }
1607    if incomplete {
1608        ExitCode::from(2)
1609    } else {
1610        ExitCode::FAILURE
1611    }
1612}
1613
1614/// Normalized confidence per hit: match quality scaled by dominance over the
1615/// other results (needs the whole ranked set). "Best other" is the top score —
1616/// or the runner-up, for the top hit itself.
1617fn attach_confidence(hits: &mut [crate::search::Hit]) {
1618    let (top, second) = hits.iter().fold((None::<f64>, None::<f64>), |(t, s), h| {
1619        if t.is_none_or(|t| h.score > t) {
1620            (Some(h.score), t)
1621        } else if s.is_none_or(|s| h.score > s) {
1622            (t, Some(h.score))
1623        } else {
1624            (t, s)
1625        }
1626    });
1627    for hit in hits.iter_mut() {
1628        let best_other = if Some(hit.score) == top { second } else { top };
1629        hit.confidence = crate::search::confidence(
1630            hit.score,
1631            crate::search::match_quality(&hit.features),
1632            best_other,
1633        );
1634    }
1635}
1636
1637/// Print the ranked results (JSON array, NDJSON lines, or highlighted text).
1638/// `Some(exit)` on a serialization failure, `None` on success.
1639fn render_hits(args: &SearchArgs, hits: &[crate::search::Hit]) -> Option<ExitCode> {
1640    // Time to the first printed result, not to the last: rq streams, and the
1641    // sub-50 ms budget is about the first answer. A change that speeds the
1642    // total while delaying this one is a regression.
1643    let render_span = crate::profile::span("render");
1644    if args.batch {
1645        // One stream, many questions: tag each row with the query it answers,
1646        // the same way `no_match_code` already tags a miss.
1647        #[derive(serde::Serialize)]
1648        struct Tagged<'a> {
1649            query: &'a str,
1650            #[serde(flatten)]
1651            hit: &'a crate::search::Hit,
1652        }
1653        let rows: Vec<Tagged> = hits
1654            .iter()
1655            .map(|hit| Tagged {
1656                query: args.query,
1657                hit,
1658            })
1659            .collect();
1660        if let Some(code) = emit_rows(args.out, &rows) {
1661            return Some(code);
1662        }
1663    } else if let Some(code) = emit_rows(args.out, hits) {
1664        return Some(code);
1665    }
1666    if args.out != Output::Text {
1667        return None;
1668    }
1669    drop(render_span);
1670    let color = match_color();
1671    let c = color.as_deref();
1672    let query = args.query;
1673    if args.show {
1674        // fell through from --show: no single confident match to print
1675        let total = hits.first().map_or(hits.len(), |h| h.total);
1676        eprintln!(
1677            "rq: no single confident match for {query:?} — {} of {total} candidates below; narrow the query to --show one",
1678            hits.len()
1679        );
1680    }
1681    for hit in hits {
1682        // highlight the chars the query matched — in the name, the
1683        // filename, and the definition line (great for fuzzy matches)
1684        let name = hl(&hit.name, query, c);
1685        let qualified = match &hit.parent {
1686            Some(p) => format!("{name} · {p}"),
1687            None => name,
1688        };
1689        println!(
1690            "{}:{}  {} {}",
1691            hl_path(&hit.file, query, c),
1692            hit.line,
1693            hit.kind,
1694            qualified
1695        );
1696        if let Some(sig) = &hit.signature {
1697            println!("    {}", hl(sig, query, c));
1698        }
1699        if args.explain {
1700            let parts: Vec<String> = hit
1701                .features
1702                .iter()
1703                .map(|f| format!("{} {}", f.name, f.reported()))
1704                .collect();
1705            println!(
1706                "    confidence {:.2} · score {:.0} = {}",
1707                hit.confidence,
1708                hit.score,
1709                parts.join(" + ")
1710            );
1711        }
1712    }
1713    None
1714}
1715
1716/// Pick a hit for `--open`: the top match, unless we're on an interactive
1717/// terminal with several — then print a short numbered menu and read a choice
1718/// (empty = the top match). `None` means abort (EOF or unparseable input).
1719fn choose_hit(hits: &[crate::search::Hit]) -> Option<&crate::search::Hit> {
1720    use std::io::{IsTerminal, Write};
1721    if hits.len() == 1 || !std::io::stdin().is_terminal() || !std::io::stderr().is_terminal() {
1722        return hits.first();
1723    }
1724    let mut err = std::io::stderr();
1725    let _ = writeln!(err, "rq: {} matches — pick one (enter = 1):", hits.len());
1726    for (i, h) in hits.iter().enumerate() {
1727        let _ = writeln!(
1728            err,
1729            "  {}. {}:{}  {} {}",
1730            i + 1,
1731            h.file,
1732            h.line,
1733            h.kind,
1734            h.name
1735        );
1736    }
1737    let _ = write!(err, "rq> ");
1738    let _ = err.flush();
1739    let mut line = String::new();
1740    if std::io::stdin().read_line(&mut line).unwrap_or(0) == 0 {
1741        return None; // Ctrl-D
1742    }
1743    parse_choice(&line, hits.len()).and_then(|i| hits.get(i))
1744}
1745
1746/// Resolve a menu reply to a 0-based index: blank → 0 (the top match), `N` → N-1
1747/// when in range, anything else → `None` (abort). Pure, so it's unit-tested.
1748fn parse_choice(input: &str, n: usize) -> Option<usize> {
1749    let s = input.trim();
1750    if s.is_empty() {
1751        return Some(0);
1752    }
1753    let i = s.parse::<usize>().ok()?.checked_sub(1)?;
1754    (i < n).then_some(i)
1755}
1756
1757/// `--open`/`--web`: choose a hit, then hand off to the editor or browser. The launcher `exec`s (replacing this
1758/// process), so the shell waits on it — not on rq's background warm.
1759fn finish_open(
1760    store: &Store,
1761    hits: &[crate::search::Hit],
1762    current: Option<i64>,
1763    root: Option<&std::path::Path>,
1764    web: bool,
1765) -> ExitCode {
1766    let Some(hit) = choose_hit(hits) else {
1767        return ExitCode::SUCCESS; // aborted at the prompt
1768    };
1769
1770    if web {
1771        return open_web(store, hit, current, root);
1772    }
1773
1774    // Results are relative to their own checkout, which under `--all-repos`
1775    // needn't be the one we're in; the bare path wouldn't open from a subdir.
1776    let target = match hit.root.as_deref().map(std::path::Path::new).or(root) {
1777        Some(r) => r.join(&hit.file),
1778        None => PathBuf::from(&hit.file),
1779    };
1780    launch_editor(&target, hit.line)
1781}
1782
1783/// The repos a search may answer from.
1784#[derive(Clone, Copy)]
1785enum Scope {
1786    /// `--all-repos`, or a directory that isn't a repo rq tracks.
1787    All,
1788    Repo(i64),
1789    /// A repo the index hasn't registered yet — its first query, before the
1790    /// warm has written anything. Nothing is in scope, not every repo.
1791    Nothing,
1792}
1793
1794impl Scope {
1795    fn new(all_repos: bool, in_repo: bool, current: Option<i64>) -> Scope {
1796        match current {
1797            _ if all_repos => Scope::All,
1798            Some(id) => Scope::Repo(id),
1799            None if in_repo => Scope::Nothing,
1800            None => Scope::All,
1801        }
1802    }
1803
1804    fn search(
1805        self,
1806        store: &Store,
1807        query: &str,
1808        current: Option<i64>,
1809        ctx: &crate::search::Context,
1810        limit: usize,
1811    ) -> crate::store::Result<crate::search::Matches> {
1812        let only_repo = match self {
1813            Scope::All => None,
1814            Scope::Repo(id) => Some(id),
1815            Scope::Nothing => {
1816                return Ok(crate::search::Matches {
1817                    hits: Vec::new(),
1818                    total: 0,
1819                });
1820            }
1821        };
1822        crate::search::search(store, query, current, only_repo, ctx, limit)
1823    }
1824}
1825
1826/// Launch the editor on `file:line`, resolving the command in order: `RQ_OPEN`
1827/// template → VS Code (`code`) → `$VISUAL`/`$EDITOR` → print the location. The
1828/// chosen command replaces this process via `exec`.
1829fn launch_editor(file: &std::path::Path, line: i64) -> ExitCode {
1830    use std::os::unix::process::CommandExt;
1831    let loc = format!("{}:{}", file.display(), line);
1832    match open_command(file, line, &loc) {
1833        Some((prog, args)) => {
1834            // exec replaces this process, so the run's profile goes out now
1835            crate::profile::emit(false);
1836            // exec returns only on failure
1837            let err = std::process::Command::new(&prog).args(&args).exec();
1838            fail(
1839                Output::Text,
1840                Failure::Launch,
1841                format_args!("rq --open: cannot run {prog}: {err}"),
1842            )
1843        }
1844        None => {
1845            println!("{loc}");
1846            ExitCode::SUCCESS
1847        }
1848    }
1849}
1850
1851/// Resolve the editor command + args. `None` → no launcher configured (the
1852/// caller prints the location). `RQ_OPEN` is split on whitespace (no shell) with
1853/// `{file}` / `{line}` / `{}` (= `path:line`) substituted per token; a template
1854/// with none of them gets `path:line` as its last argument, so `RQ_OPEN=subl`
1855/// opens the match rather than a bare editor.
1856fn open_command(file: &std::path::Path, line: i64, loc: &str) -> Option<(String, Vec<String>)> {
1857    let fstr = file.to_string_lossy().into_owned();
1858
1859    if let Some(t) = std::env::var_os("RQ_OPEN") {
1860        let t = t.to_string_lossy();
1861        let placeholder = ["{file}", "{line}", "{}"].iter().any(|p| t.contains(p));
1862        let mut parts = t.split_whitespace().map(|p| {
1863            p.replace("{file}", &fstr)
1864                .replace("{line}", &line.to_string())
1865                .replace("{}", loc)
1866        });
1867        if let Some(prog) = parts.next() {
1868            let mut args: Vec<String> = parts.collect();
1869            if !placeholder {
1870                args.push(loc.to_string());
1871            }
1872            return Some((prog, args));
1873        }
1874    }
1875
1876    if on_path("code") {
1877        return Some(("code".into(), vec!["--goto".into(), loc.into()]));
1878    }
1879
1880    if let Some(ed) = std::env::var_os("VISUAL").or_else(|| std::env::var_os("EDITOR")) {
1881        let ed = ed.to_string_lossy().into_owned();
1882        let l = ed.to_ascii_lowercase();
1883        // line-aware launch for the common terminal editors; others just get the file
1884        if ["vim", "nvim", "vi", "nano", "emacs", "kak", "micro"]
1885            .iter()
1886            .any(|e| l.contains(e))
1887        {
1888            return Some((ed, vec![format!("+{line}"), fstr]));
1889        }
1890        return Some((ed, vec![fstr]));
1891    }
1892
1893    None
1894}
1895
1896/// `--web`: open `hit` on its git host. Pinned to the newest pushed sha in HEAD's
1897/// history when the hit is in the repo we're standing in — an unpushed sha would
1898/// 404. Another repo's checkout state is unknown, so its link follows the host's
1899/// default branch instead.
1900fn open_web(
1901    store: &Store,
1902    hit: &crate::search::Hit,
1903    current: Option<i64>,
1904    root: Option<&std::path::Path>,
1905) -> ExitCode {
1906    if hit.repo_identity.starts_with("local:") {
1907        return fail(
1908            Output::Text,
1909            Failure::NoRemote,
1910            format_args!(
1911                "rq --web: {}:{} has no git remote to link to ({}) — open it \
1912                 locally with -o, or add one with `git remote add origin <url>`",
1913                hit.file, hit.line, hit.repo_identity
1914            ),
1915        );
1916    }
1917    let here =
1918        current.is_some() && store.repository_id(&hit.repo_identity).ok().flatten() == current;
1919    let rev = root
1920        .filter(|_| here)
1921        .and_then(crate::index::pushed_head)
1922        .unwrap_or_else(|| "HEAD".into());
1923    let url = web_url(&hit.repo_identity, &rev, &hit.file, hit.line);
1924
1925    use std::os::unix::process::CommandExt;
1926    let browser = std::env::var("BROWSER")
1927        .ok()
1928        .filter(|b| !b.is_empty())
1929        .or_else(|| {
1930            ["open", "xdg-open"]
1931                .into_iter()
1932                .find(|p| on_path(p))
1933                .map(str::to_string)
1934        });
1935    match browser {
1936        Some(prog) => {
1937            // exec replaces this process, so the run's profile goes out now
1938            crate::profile::emit(false);
1939            // exec returns only on failure
1940            let err = std::process::Command::new(&prog).arg(&url).exec();
1941            fail(
1942                Output::Text,
1943                Failure::Launch,
1944                format_args!("rq --web: cannot run {prog}: {err}"),
1945            )
1946        }
1947        None => {
1948            println!("{url}");
1949            ExitCode::SUCCESS
1950        }
1951    }
1952}
1953
1954/// A GitHub-style permalink: `https://<host/org/repo>/blob/<rev>/<file>#L<line>`.
1955/// GitLab redirects the same shape, so it isn't GitHub-only.
1956fn web_url(identity: &str, rev: &str, file: &str, line: i64) -> String {
1957    let path: String = file
1958        .bytes()
1959        .map(|b| match b {
1960            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' | b'/' => {
1961                (b as char).to_string()
1962            }
1963            _ => format!("%{b:02X}"),
1964        })
1965        .collect();
1966    format!("https://{identity}/blob/{rev}/{path}#L{line}")
1967}
1968
1969/// Whether `prog` resolves on `PATH` (a regular file; symlinks followed).
1970fn on_path(prog: &str) -> bool {
1971    std::env::var_os("PATH")
1972        .is_some_and(|paths| std::env::split_paths(&paths).any(|dir| dir.join(prog).is_file()))
1973}
1974
1975/// How long a branch-file list is served before it's refreshed. A commit or a
1976/// checkout is caught by the stamp; a bare working-tree edit touches neither
1977/// `.git/HEAD` nor `.git/index`, so only elapsed time catches that — short
1978/// enough that a burst of searches shares one computation and the edit you just
1979/// made is reflected on the next search.
1980const BRANCH_FILES_TTL_SECS: i64 = 15;
1981
1982/// Longest a branch-file list may be served for, however slow it is to rebuild.
1983/// The window only governs noticing an *unstaged* edit — every git operation
1984/// invalidates by stamp regardless — so five minutes is already generous.
1985const BRANCH_FILES_TTL_MAX_SECS: i64 = 300;
1986
1987/// How many times its own rebuild cost a list may be served for — so the
1988/// refresh never eats more than about 1% of the time between searches. The
1989/// floor binds below ~150 ms, which is where every small repo sits.
1990///
1991/// The refresh forks two `git diff`s over the whole worktree, which is cheap on
1992/// a small repo and very much not on a large one — it was measured at 700 ms on
1993/// a 90k-file monorepo, against a 40-115 ms query it runs *alongside* and
1994/// competes with for disk. A fixed 15-second window then re-paid that every
1995/// fifteen seconds of active searching. Scaling the window by the measured cost
1996/// leaves small repos exactly where they were and backs off only where the
1997/// evidence says it's needed.
1998const BRANCH_FILES_WINDOW_MULTIPLE: i64 = 100;
1999
2000/// How long a cached branch-file list stays good, given what it cost to build.
2001fn branch_files_ttl(cost_ms: Option<u64>) -> i64 {
2002    // `as i64` would wrap a large cost to a negative and quietly hand it the
2003    // floor — the opposite of what an expensive rebuild has earned.
2004    let earned = cost_ms.map_or(0, |ms| {
2005        i64::try_from(ms)
2006            .unwrap_or(i64::MAX)
2007            .saturating_mul(BRANCH_FILES_WINDOW_MULTIPLE)
2008            / 1000
2009    });
2010    earned.clamp(BRANCH_FILES_TTL_SECS, BRANCH_FILES_TTL_MAX_SECS)
2011}
2012
2013/// A branch-file recomputation running alongside the search. The git work
2014/// happens on the thread; the store write waits for the main thread, since a
2015/// SQLite connection isn't shared.
2016struct BranchRefresh {
2017    /// Yields the file list and what it cost to build, which sets how long
2018    /// the result stays good.
2019    handle: std::thread::JoinHandle<(Vec<String>, u64)>,
2020    identity: String,
2021    stamp: String,
2022}
2023
2024impl BranchRefresh {
2025    /// Wait for the recomputation and store it for the next query.
2026    fn store(self, store: &Store) {
2027        let Ok((files, cost_ms)) = self.handle.join() else {
2028            return;
2029        };
2030        let _ = store.branch_files_set(&self.identity, &self.stamp, now_unix(), cost_ms, &files);
2031    }
2032}
2033
2034/// The branch-changed file list, served from the store when it's still good.
2035/// Returns the list, plus a recomputation to collect after results print when
2036/// the stored one has aged out.
2037///
2038/// The list feeds a *ranking boost*, so serving a slightly old one costs a
2039/// little ranking quality, while recomputing it first would cost every search
2040/// the git diff behind it — which is O(tracked files). So the stored list is
2041/// served immediately and the refresh runs concurrently with the search rather
2042/// than after it, which usually hides its cost entirely. It feeds only the next
2043/// query, so nothing about this run's ranking depends on how the race lands.
2044///
2045/// The first search in a repo has nothing to serve and computes inline; that's
2046/// once per repo, like the first index.
2047fn cached_branch_files(
2048    store: &Store,
2049    root: &std::path::Path,
2050) -> (Vec<String>, Option<BranchRefresh>, Option<u64>) {
2051    let identity = resolve_identity(store, root);
2052    let stamp = crate::index::branch_files_stamp(root);
2053    let cached = store.branch_files_get(&identity).ok().flatten();
2054    let now = now_unix();
2055
2056    if let (Some(hit), Some(stamp)) = (&cached, &stamp) {
2057        if &hit.stamp == stamp && now.saturating_sub(hit.written_at) < branch_files_ttl(hit.cost_ms)
2058        {
2059            return (hit.files.clone(), None, hit.cost_ms);
2060        }
2061        let owned_root = root.to_path_buf();
2062        let refresh = BranchRefresh {
2063            handle: std::thread::spawn(move || {
2064                let t = std::time::Instant::now();
2065                let files = crate::index::branch_changed_files(&owned_root);
2066                (files, t.elapsed().as_millis() as u64)
2067            }),
2068            identity,
2069            stamp: stamp.clone(),
2070        };
2071        return (hit.files.clone(), Some(refresh), hit.cost_ms);
2072    }
2073
2074    // Nothing cached (or nowhere to cache it, e.g. a worktree): compute inline.
2075    let t = std::time::Instant::now();
2076    let files = crate::index::branch_changed_files(root);
2077    let cost_ms = t.elapsed().as_millis() as u64;
2078    if let Some(stamp) = stamp {
2079        let _ = store.branch_files_set(&identity, &stamp, now, cost_ms, &files);
2080    }
2081    (files, None, Some(cost_ms))
2082}
2083
2084/// Whether the worktree has moved since it was indexed — a different HEAD, or
2085/// uncommitted edits. Split out from the store read so this half can run on its
2086/// own thread: `dirty_files` forks `git status`, which on a large worktree costs
2087/// more than the search it was gating (measured: 12.6ms of a 16.8ms query on a
2088/// 6k-file repo, against 0.1ms on a 54-file one).
2089///
2090/// `None` means HEAD moved — or we never recorded one, so there's nothing to
2091/// compare against — and everything counts as changed. Otherwise the dirty
2092/// files, for [`changed_since_index`] to check against the index.
2093fn worktree_edits(cwd: &std::path::Path, indexed_head: Option<&str>) -> Option<Vec<String>> {
2094    let head = indexed_head?;
2095    let _span = crate::profile::span("git: worktree changed?");
2096    (crate::index::head_state(cwd).as_deref() == Some(head)).then(|| crate::index::dirty_files(cwd))
2097}
2098
2099/// Whether the worktree holds anything the index doesn't yet reflect, given
2100/// what [`worktree_edits`] found.
2101fn changed_since_index(
2102    store: &Store,
2103    identity: Option<&str>,
2104    root: Option<&std::path::Path>,
2105    edits: Option<Vec<String>>,
2106) -> bool {
2107    let (Some(dirty), Some(root)) = (edits, root) else {
2108        return true;
2109    };
2110    match identity.and_then(|id| store.repository_id(id).ok().flatten()) {
2111        Some(repo_id) => crate::index::has_unindexed_changes(store, repo_id, root, &dirty),
2112        None => !dirty.is_empty(),
2113    }
2114}
2115
2116/// The "has the worktree moved since it was indexed?" check on a complete
2117/// repo. It forks `git status`, which grows with the worktree (~12 ms on
2118/// rails, ~27 ms on a 14k-file repo) and decides nothing a hit depends on.
2119enum Staleness {
2120    /// Running alongside the search, collected after the answer — the
2121    /// no-detach mode, where the reindex it may trigger runs in-process.
2122    Running(std::thread::JoinHandle<Option<Vec<String>>>),
2123    /// Not started: a hit hands it to the detached warm child, so the process
2124    /// exits without waiting on git; a miss, whose exit code depends on it,
2125    /// runs it inline. Holds the root and the HEAD the index reflects.
2126    Deferred(PathBuf, Option<String>),
2127}
2128
2129/// Settle warming once the answer is out: resolve the staleness check,
2130/// reindex if the worktree moved, and hand any remainder to a detached child.
2131///
2132/// Called from *both* exits. The miss path matters as much as the render one —
2133/// a symbol added a moment ago is precisely a miss, and reindexing before we
2134/// exit is what makes the immediate retry hit. `hit` says which exit this is.
2135#[allow(clippy::too_many_arguments)]
2136fn settle_warm(
2137    store: &Store,
2138    staleness: Option<Staleness>,
2139    hit: bool,
2140    was_warming: bool,
2141    warming_ok: bool,
2142    root: Option<&std::path::Path>,
2143    active: &[String],
2144    query: &str,
2145    budget: Duration,
2146    no_wait: bool,
2147    identity: Option<&str>,
2148) -> bool {
2149    let changed = match staleness {
2150        None => false,
2151        // The answer is out and didn't depend on this: the warm child asks git
2152        // and reindexes only if something moved (see `cmd_warm`).
2153        Some(Staleness::Deferred(r, head)) if hit => {
2154            if recently_verified(store, identity, &r, head.as_deref()) {
2155                crate::trace!("warm: verified unchanged within the recheck window, not spawning");
2156            } else {
2157                spawn_detached_warm(&r);
2158            }
2159            return false;
2160        }
2161        Some(Staleness::Deferred(r, head)) => {
2162            let _span = crate::profile::span("after: staleness check");
2163            changed_since_index(store, identity, root, worktree_edits(&r, head.as_deref()))
2164        }
2165        Some(Staleness::Running(h)) => {
2166            // the check ran alongside the search; this is only what's left of it
2167            let mut span = crate::profile::span("after: staleness wait");
2168            // A panicked check counts as changed: warming needlessly costs a
2169            // little time, skipping it wrongly serves a stale index.
2170            let changed = h.join().map_or(true, |edits| {
2171                changed_since_index(store, identity, root, edits)
2172            });
2173            span.note(|| if changed { "changed" } else { "unchanged" }.to_string());
2174            changed
2175        }
2176    };
2177    // Reindexing an edited worktree means sweeping every file to find the few
2178    // that moved — ~32ms on a 3000-file repo, and it was paid on *every* query
2179    // for as long as anything stayed uncommitted, which is exactly while you're
2180    // working. The shell shouldn't wait for that: hand it to the detached
2181    // child, which is what "the shell never waits on it" already promises
2182    // everywhere else.
2183    //
2184    // With detach off (the harness pins it so no child races a test's cleanup)
2185    // there's nobody to hand it to, so do it here as before.
2186    if changed
2187        && !no_wait
2188        && !warm_detach_enabled()
2189        && let Some(r) = root
2190        && let Ok(mut idx) = open_store()
2191    {
2192        crate::trace!("background warm (deferred, {budget:?}): worktree changed since index");
2193        let _ = crate::index::index_budgeted(&mut idx, r, active, budget, Some(query));
2194    }
2195    maybe_detach_warm(
2196        store,
2197        warming_ok && (was_warming || changed),
2198        changed,
2199        root,
2200        identity,
2201    );
2202    // Report only that work was *deferred*, which is what makes a miss
2203    // provisional. When the reindex ran inline just above (detach off), the
2204    // index is as current as we can make it and a miss is definitive.
2205    changed && warm_detach_enabled()
2206}
2207
2208/// Inline warm budget on the search path. A *cap*, not a fixed delay:
2209/// `index_budgeted` returns the moment a full sweep finishes, so small/medium
2210/// repos index completely and pay only their real cost. The cap only bites a
2211/// genuinely huge, never-indexed repo — where a bigger budget buys a much better
2212/// first answer (a tiny budget can return nothing, since a git repo has no
2213/// live-scan fallback). 500 ms is a one-time cold-cache cost, trivial next to
2214/// scanning a large tree from scratch; the deferred pass and later queries fill
2215/// in the rest.
2216fn answer_warm_budget() -> Duration {
2217    env_budget("RQ_ANSWER_BUDGET_MS", 500)
2218}
2219
2220/// Deferred warm budget, spent after results are printed: larger, to make real
2221/// progress on coverage per query while keeping each invocation snappy.
2222fn deferred_warm_budget() -> Duration {
2223    env_budget("RQ_DEFERRED_BUDGET_MS", 250)
2224}
2225
2226/// Bound for the git-repo live-scan fallback (index empty, still warming): enough
2227/// to surface a result the warm hasn't reached, without an unbounded walk.
2228fn live_fallback_budget() -> Duration {
2229    env_budget("RQ_FALLBACK_BUDGET_MS", 250)
2230}
2231
2232/// Budget for the *detached* warm child — generous, because nothing waits on
2233/// it: the shell got its results and the child runs niced in the background.
2234fn warm_bg_budget() -> Duration {
2235    env_budget("RQ_WARM_BUDGET_MS", 20_000)
2236}
2237
2238/// Whether a search hands leftover warming to a detached child (default) or
2239/// finishes it in-process before exiting (`RQ_WARM_DETACH=0` — used by the
2240/// test harness for hermetic runs, and handy for debugging).
2241fn warm_detach_enabled() -> bool {
2242    std::env::var("RQ_WARM_DETACH").map_or(true, |v| v != "0")
2243}
2244
2245/// How long a query may block indexing a cold repo before giving up with an
2246/// honest "still indexing" rather than a false miss. A generous backstop, not the
2247/// real cost: `index_budgeted` returns the moment the sweep completes, so any
2248/// normal repo finishes well under it, and an interactive run isn't bounded by it
2249/// at all (Ctrl-C escapes). It mainly bounds a programmatic caller on a
2250/// pathologically huge repo — where the partial index still persists for the next
2251/// query. `RQ_WAIT_BUDGET_MS=0` makes a programmatic caller non-blocking again —
2252/// it answers immediately from whatever's already indexed.
2253fn wait_budget() -> Duration {
2254    env_budget("RQ_WAIT_BUDGET_MS", 60_000)
2255}
2256
2257/// Parse a `--wait` value into a duration: `<n>ms`, `<n>s`, `<n>m`, or a bare
2258/// `<n>` (seconds). Fractions are allowed (`1.5s`); `0` (any unit) means "don't
2259/// wait". A `clap` value parser, so an invalid duration is rejected at parse
2260/// time with a usage error.
2261fn parse_wait(s: &str) -> std::result::Result<Duration, String> {
2262    let s = s.trim();
2263    let bad = || format!("invalid duration {s:?} — use e.g. 50ms, 2s, 1m, or 0");
2264    // check "ms" before "s" so the "s" arm doesn't swallow it
2265    let (num, unit_ms) = if let Some(n) = s.strip_suffix("ms") {
2266        (n, 1.0)
2267    } else if let Some(n) = s.strip_suffix('s') {
2268        (n, 1_000.0)
2269    } else if let Some(n) = s.strip_suffix('m') {
2270        (n, 60_000.0)
2271    } else {
2272        // a bare number is seconds
2273        (s, 1_000.0)
2274    };
2275    let val: f64 = num.trim().parse().map_err(|_| bad())?;
2276    if !val.is_finite() || val < 0.0 {
2277        return Err(bad());
2278    }
2279    Ok(Duration::from_millis((val * unit_ms).round() as u64))
2280}
2281
2282/// Set by the SIGINT handler during an interactive cold-start escalation. The
2283/// poll loop and the running index pass watch it, so Ctrl-C stops the wait
2284/// promptly and prints the best partial results instead of killing the process.
2285static INTERRUPTED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
2286
2287extern "C" fn on_sigint(_: libc::c_int) {
2288    // Async-signal-safe: a lone relaxed atomic store — no allocation, no locks.
2289    INTERRUPTED.store(true, std::sync::atomic::Ordering::Relaxed);
2290}
2291
2292/// Install the SIGINT handler once. Scoped to the escalation path: a normal fast
2293/// query keeps the default behavior (Ctrl-C kills it outright).
2294fn install_interrupt_handler() {
2295    static ONCE: std::sync::Once = std::sync::Once::new();
2296    ONCE.call_once(|| unsafe {
2297        let mut action: libc::sigaction = std::mem::zeroed();
2298        action.sa_sigaction = on_sigint as *const () as usize;
2299        libc::sigemptyset(&mut action.sa_mask);
2300        libc::sigaction(libc::SIGINT, &action, std::ptr::null_mut());
2301    });
2302}
2303
2304/// Is a human watching stderr? True for a real terminal; `RQ_ASSUME_INTERACTIVE`
2305/// forces it on so the progress/Ctrl-C path is exercisable under test (where
2306/// stderr is a pipe), mirroring the `RQ_*_BUDGET_MS` testing knobs.
2307fn stderr_interactive() -> bool {
2308    std::io::stderr().is_terminal() || std::env::var_os("RQ_ASSUME_INTERACTIVE").is_some()
2309}
2310
2311/// Whether to show the live "indexing…" progress heads-up and handle Ctrl-C
2312/// gracefully while a cold repo blocks — a human watching a plain-text terminal.
2313/// Piped / `--json` / `--ndjson` callers block silently instead (no line to draw,
2314/// no one to interrupt); the *decision to block* is the same for both.
2315fn show_progress(out: Output, interactive: bool) -> bool {
2316    interactive && matches!(out, Output::Text)
2317}
2318
2319/// A short, friendly name for the repo being indexed — its directory name, for
2320/// the progress line.
2321fn repo_label(root: Option<&std::path::Path>) -> String {
2322    root.and_then(|r| r.file_name())
2323        .map(|n| n.to_string_lossy().into_owned())
2324        .unwrap_or_else(|| "repo".into())
2325}
2326
2327/// Redraw the in-place "indexing…" progress line on stderr (kept off stdout so
2328/// piped/`--json` output stays clean). The file count comes from the index the
2329/// background pass is filling, so it climbs as warming proceeds.
2330fn draw_progress(store: &Store, identity: Option<&str>, label: &str) {
2331    let files = identity
2332        .and_then(|id| store.repository_id(id).ok().flatten())
2333        .and_then(|rid| store.repo_totals(rid).ok())
2334        .map_or(0, |(f, _)| f);
2335    eprint!("\r\x1b[Krq: indexing {label}… {files} files");
2336    let _ = std::io::stderr().flush();
2337}
2338
2339/// Erase the progress line so results print to a clean terminal.
2340fn clear_progress() {
2341    eprint!("\r\x1b[K");
2342    let _ = std::io::stderr().flush();
2343}
2344
2345/// Read a budget (milliseconds) from an env var, else the default. The env knobs
2346/// exist mainly for testing — a tiny budget reproduces large-repo warming
2347/// behavior on a small repo.
2348fn env_budget(var: &str, default_ms: u64) -> Duration {
2349    let ms = std::env::var(var)
2350        .ok()
2351        .and_then(|v| v.parse().ok())
2352        .unwrap_or(default_ms);
2353    Duration::from_millis(ms)
2354}
2355
2356/// The checkout a command runs in: its repo identity and root.
2357#[derive(Clone, Copy)]
2358struct Here<'a> {
2359    identity: &'a str,
2360    root: &'a std::path::Path,
2361}
2362
2363/// Candidate on-disk roots that may hold a hit's file, most-current first: the
2364/// checkout you're in, when the hit is from its repo — another clone of the
2365/// same remote shares the rows but not necessarily the content — then every
2366/// recorded checkout root, newest first (a moved repo keeps its old row, and
2367/// reading from that path fails). Callers read from the first that has the file.
2368fn hit_file_roots(store: &Store, repo_identity: &str, here: Option<Here>) -> Vec<PathBuf> {
2369    let mut roots: Vec<PathBuf> = here
2370        .filter(|h| h.identity == repo_identity)
2371        .map(|h| h.root.to_path_buf())
2372        .into_iter()
2373        .collect();
2374    let recorded = store
2375        .repository_id(repo_identity)
2376        .ok()
2377        .flatten()
2378        .map(|id| store.checkout_roots(id).unwrap_or_default())
2379        .unwrap_or_default();
2380    for root in recorded.into_iter().map(PathBuf::from) {
2381        if !roots.contains(&root) {
2382            roots.push(root);
2383        }
2384    }
2385    roots
2386}
2387
2388/// The checkout root a hit's `file` is relative to: the first candidate (see
2389/// [`hit_file_roots`]) that has the file on disk, else the likeliest one — the
2390/// file may be gone, but the root still says where it was relative to.
2391fn hit_root(store: &Store, repo_identity: &str, file: &str, here: Option<Here>) -> Option<PathBuf> {
2392    let mut roots = hit_file_roots(store, repo_identity, here);
2393    let at = roots
2394        .iter()
2395        .position(|r| r.join(file).is_file())
2396        .unwrap_or(0);
2397    (at < roots.len()).then(|| roots.swap_remove(at))
2398}
2399
2400/// The definition's source line (trimmed) at `line` of `path`. Best-effort.
2401fn read_signature(path: &std::path::Path, line: i64) -> Option<String> {
2402    let src = std::fs::read_to_string(path).ok()?;
2403    signature_in(&src.lines().collect::<Vec<_>>(), line)
2404}
2405
2406/// Confidence at or above which `--show` prints a body instead of a list. Exact
2407/// (1.0) and a unique prefix (0.9) clear it; a fuzzy or tied match does not — so
2408/// `--show` never prints a definition it isn't sure about.
2409const SHOW_CONFIDENCE: f64 = 0.85;
2410
2411/// `--show`: if the top hit is confident, read and print its full source span
2412/// and return the exit code; otherwise return `None` to fall through to the
2413/// ranked list. Emits a single object in JSON/NDJSON (with a `body` field).
2414fn show_top_definition(
2415    hits: &mut [crate::search::Hit],
2416    query: &str,
2417    out: Output,
2418) -> Option<ExitCode> {
2419    let top = hits.first()?;
2420    if top.confidence < SHOW_CONFIDENCE {
2421        return None; // ambiguous / weak — let the caller list candidates
2422    }
2423    let end = top.end_line.unwrap_or(top.line);
2424    let body = top.root.as_deref().and_then(|root| {
2425        let src = std::fs::read_to_string(std::path::Path::new(root).join(&top.file)).ok()?;
2426        span_in(&src, top.line, end)
2427    });
2428    hits[0].body = body;
2429    let top = &hits[0];
2430    let code = match out {
2431        Output::Json | Output::Ndjson => {
2432            // fail loudly on a serialize error, like every other JSON path
2433            emit_json(out, top)
2434        }
2435        Output::Text => {
2436            let color = match_color();
2437            let c = color.as_deref();
2438            let name = hl(&top.name, query, c);
2439            let qualified = match &top.parent {
2440                Some(p) => format!("{name} · {p}"),
2441                None => name,
2442            };
2443            println!(
2444                "{}:{}  {} {}",
2445                hl_path(&top.file, query, c),
2446                top.line,
2447                top.kind,
2448                qualified
2449            );
2450            match (&top.body, &top.signature) {
2451                (Some(body), _) => println!("{body}"),
2452                // end_line unknown (pre-v4 row) → at least the definition line
2453                (None, Some(sig)) => println!("{sig}"),
2454                (None, None) => {}
2455            }
2456            ExitCode::SUCCESS
2457        }
2458    };
2459
2460    Some(code)
2461}
2462
2463/// Lines `start..=end` (1-based, inclusive) of already-read `content`, joined —
2464/// clamped to the file's bounds. `None` if `start` is past the end.
2465fn span_in(content: &str, start: i64, end: i64) -> Option<String> {
2466    let s = usize::try_from(start).ok()?.checked_sub(1)?;
2467    let lines: Vec<&str> = content.lines().collect();
2468    if s >= lines.len() {
2469        return None;
2470    }
2471    let e = usize::try_from(end).ok()?.clamp(s + 1, lines.len());
2472    Some(lines[s..e].join("\n"))
2473}
2474
2475/// The trimmed source line `line` (1-based) of a file already split into
2476/// `lines`, if non-empty — a symbol's definition line. Takes the split rather
2477/// than the text so `--symbols` splits once: re-scanning from the top per
2478/// symbol is quadratic in a large file.
2479fn signature_in(lines: &[&str], line: i64) -> Option<String> {
2480    let idx = usize::try_from(line).ok()?.checked_sub(1)?;
2481    let l = lines.get(idx)?.trim();
2482    (!l.is_empty()).then(|| l.to_string())
2483}
2484
2485/// One symbol in `rq --symbols` output. Same field names as a search hit
2486/// (`repo`, `signature`) for agent consistency, but no score/features — an
2487/// outline is structural, not ranked.
2488#[derive(serde::Serialize)]
2489struct SymbolOut {
2490    name: String,
2491    kind: String,
2492    language: String,
2493    file: String,
2494    /// Absolute checkout root `file` is relative to, as on a search hit.
2495    root: String,
2496    line: i64,
2497    #[serde(skip_serializing_if = "Option::is_none")]
2498    end_line: Option<i64>,
2499    #[serde(skip_serializing_if = "Option::is_none")]
2500    parent: Option<String>,
2501    #[serde(skip_serializing_if = "Option::is_none")]
2502    visibility: Option<String>,
2503    repo: String,
2504    #[serde(skip_serializing_if = "Option::is_none")]
2505    signature: Option<String>,
2506}
2507
2508/// `rq --symbols <file>`: list a file's symbols in line order — a structural
2509/// outline, not a ranked search. Warms the file's repo if it's cold/incomplete or
2510/// changed (same gate as search), then reads straight from the index. Honors
2511/// --kind/--lang filters and --json/--ndjson.
2512fn cmd_symbols(file_arg: &str, kinds: &[String], langs: &[String], out: Output) -> ExitCode {
2513    let open_span = crate::profile::span("store open");
2514    let mut store = match open_store() {
2515        Ok(s) => s,
2516        Err(e) => {
2517            return fail(
2518                out,
2519                Failure::Database,
2520                format_args!("rq: cannot open database: {e}"),
2521            );
2522        }
2523    };
2524    drop(open_span);
2525    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
2526    let root = crate::index::repo_root(&cwd).unwrap_or_else(|| cwd.clone());
2527    let rel = repo_relative(&root, &cwd, file_arg);
2528
2529    let identity = resolve_identity(&store, &root);
2530    let coverage = store.coverage_status(&identity).ok().flatten();
2531    let warming_ok = crate::index::is_git_repo(&root) || coverage.is_some();
2532    let current = store.repository_id(&identity).ok().flatten();
2533    let path = root.join(&rel);
2534    if !path.is_file() {
2535        return fail(
2536            out,
2537            Failure::NotFound,
2538            format_args!("rq --symbols: no such file: {file_arg}"),
2539        );
2540    }
2541    let indexable = path
2542        .extension()
2543        .and_then(|e| e.to_str())
2544        .is_some_and(|e| crate::lang::plugin_for_extension(e).is_some());
2545    match current {
2546        // An outline depends on this one file, so on a complete index freshness
2547        // is just re-extracting it if it moved — no `git status` over the whole
2548        // worktree, and a new untracked file is picked up too.
2549        Some(repo_id) if coverage.as_deref() == Some("complete") => {
2550            if indexable {
2551                let _span = crate::profile::span("symbols: refresh");
2552                let _ = crate::index::refresh_file(&mut store, repo_id, &root, &rel);
2553            }
2554        }
2555        // Not fully indexed yet: warm synchronously — there's no answer to get
2556        // out of the way of here — with this file as an active one, so it's
2557        // indexed first whatever the budget.
2558        _ if warming_ok => {
2559            let budget = answer_warm_budget() + deferred_warm_budget();
2560            let _span = crate::profile::span("symbols: warm");
2561            let active = [rel.clone()];
2562            let _ = crate::index::index_budgeted(&mut store, &root, &active, budget, None);
2563        }
2564        _ => {}
2565    }
2566
2567    let Some(repo_id) = store.repository_id(&identity).ok().flatten() else {
2568        return emit_symbols(out, &[]); // unknown / un-indexed repo → nothing
2569    };
2570    let mut query_span = crate::profile::span("symbols: query");
2571    let mut rows = match store.symbols_in_file(repo_id, &rel) {
2572        Ok(r) => r,
2573        Err(e) => return fail(out, Failure::Database, format_args!("rq: {e}")),
2574    };
2575    query_span.note(|| format!("{} rows", rows.len()));
2576    drop(query_span);
2577    if !kinds.is_empty() {
2578        rows.retain(|r| kinds.iter().any(|k| k == &r.kind));
2579    }
2580    if !langs.is_empty() {
2581        rows.retain(|r| langs.iter().any(|l| l == &r.language));
2582    }
2583
2584    // Read the source once for signatures (every row is the same file), from
2585    // the checkout we're in — it's the one the outline was refreshed from.
2586    let signatures_span = crate::profile::span("symbols: signatures");
2587    let content = std::fs::read_to_string(&path).ok();
2588    let lines: Vec<&str> = content
2589        .as_deref()
2590        .map_or_else(Vec::new, |c| c.lines().collect());
2591    let root_str = root.to_string_lossy().into_owned();
2592    let syms: Vec<SymbolOut> = rows
2593        .into_iter()
2594        .map(|r| SymbolOut {
2595            signature: signature_in(&lines, r.line),
2596            name: r.name,
2597            kind: r.kind,
2598            language: r.language,
2599            file: r.file,
2600            root: root_str.clone(),
2601            line: r.line,
2602            end_line: r.end_line,
2603            parent: r.parent,
2604            visibility: r.visibility,
2605            repo: r.repo_identity,
2606        })
2607        .collect();
2608    drop(signatures_span);
2609    let _span = crate::profile::span("render");
2610    emit_symbols(out, &syms)
2611}
2612
2613/// Render the outline. Exit 0 if any symbols, non-zero if none — rq's exit-code
2614/// convention, matching how search reports an empty result per format.
2615fn emit_symbols(out: Output, syms: &[SymbolOut]) -> ExitCode {
2616    if syms.is_empty() {
2617        match out {
2618            Output::Json | Output::Ndjson => {
2619                let obj = serde_json::json!({ "status": "no_match" });
2620                let _ = emit_json(out, &obj); // exit code below carries the miss
2621            }
2622            Output::Text => eprintln!("no symbols"),
2623        }
2624        return ExitCode::FAILURE;
2625    }
2626    if let Some(code) = emit_rows(out, syms) {
2627        return code;
2628    }
2629    match out {
2630        Output::Json | Output::Ndjson => {}
2631        Output::Text => {
2632            for s in syms {
2633                let qualified = match &s.parent {
2634                    Some(p) => format!("{} · {p}", s.name),
2635                    None => s.name.clone(),
2636                };
2637                println!("{}:{}  {} {}", s.file, s.line, s.kind, qualified);
2638                if let Some(sig) = &s.signature {
2639                    println!("    {sig}");
2640                }
2641            }
2642        }
2643    }
2644    ExitCode::SUCCESS
2645}
2646
2647/// A leading positional that names a symbol kind — the shorthand behind
2648/// `rq class Foo` and `rq method zoom`. Only the full, unambiguous keyword forms
2649/// count (never the single-letter `-k` shortcuts, which are far likelier to be a
2650/// real query). Returns the canonical kind, so it filters exactly like `--kind`.
2651fn keyword_kind(token: &str) -> Option<&'static str> {
2652    match token.to_ascii_lowercase().as_str() {
2653        "class" => Some("class"),
2654        "module" => Some("module"),
2655        "method" => Some("method"),
2656        "function" | "fn" => Some("function"),
2657        "struct" | "type" => Some("struct"),
2658        "enum" => Some("enum"),
2659        "trait" | "interface" => Some("trait"),
2660        "constant" | "const" => Some("constant"),
2661        _ => None,
2662    }
2663}
2664
2665/// Peel a leading kind keyword off the query, so `rq class Foo` (or the quoted
2666/// `rq 'class Foo'`) means `-k class` + query `Foo`. The keyword must be followed
2667/// by a real query token — a bare `rq class` stays a search for a symbol literally
2668/// named `class`. Returns `(kind, query, trailing_path_dirs)`; the trailing dirs
2669/// are the rg-style positionals left after the query is consumed.
2670fn split_kind_keyword(
2671    target: String,
2672    dirs: Vec<String>,
2673) -> (Option<&'static str>, String, Vec<String>) {
2674    // Quoted form: the whole thing is one arg (`"class Foo"`), so peel the first
2675    // whitespace-separated word and keep the remainder as the query.
2676    if let Some((head, rest)) = target.split_once(char::is_whitespace) {
2677        let rest = rest.trim();
2678        if let Some(k) = keyword_kind(head)
2679            && !rest.is_empty()
2680        {
2681            return (Some(k), rest.to_string(), dirs);
2682        }
2683    } else if let Some(k) = keyword_kind(&target)
2684        && let Some((query, extra)) = dirs.split_first()
2685    {
2686        // Unquoted form: `rq class Foo` — the next positional is the query.
2687        return (Some(k), query.clone(), extra.to_vec());
2688    }
2689    (None, target, dirs)
2690}
2691
2692/// Normalize a `--kind` value (name or shortcut) to a canonical symbol kind.
2693/// Unknown values pass through lowercased (so they simply match nothing).
2694fn canonical_kind(s: &str) -> Option<&'static str> {
2695    Some(match s.to_ascii_lowercase().as_str() {
2696        "c" | "class" => "class",
2697        "m" | "method" => "method",
2698        "f" | "fn" | "func" | "function" => "function",
2699        "mod" | "module" => "module",
2700        "s" | "struct" | "type" => "struct",
2701        "e" | "enum" => "enum",
2702        "t" | "trait" | "interface" => "trait",
2703        "const" | "constant" => "constant",
2704        _ => return None,
2705    })
2706}
2707
2708/// Expand a `--lang` value to the language tag(s) it selects: a **prefix** of any
2709/// known language name (so `r` → ruby+rust, `p`/`py` → python, `g` → go,
2710/// `t` → typescript, `j` → javascript), plus a few non-prefix aliases
2711/// (`rb`→ruby, `rs`→rust, `golang`→go, `ts`/`tsx`→typescript,
2712/// `js`/`jsx`→javascript). An unknown value passes through lowercased so it
2713/// simply matches nothing.
2714fn canonical_langs(s: &str) -> Vec<String> {
2715    let t = s.to_ascii_lowercase();
2716    let alias = match t.as_str() {
2717        "rb" => Some("ruby"),
2718        "rs" => Some("rust"),
2719        "golang" => Some("go"),
2720        "ts" | "tsx" => Some("typescript"),
2721        "js" | "jsx" => Some("javascript"),
2722        _ => None,
2723    };
2724    let matched: Vec<String> = crate::lang::languages()
2725        .into_iter()
2726        .filter(|lang| alias == Some(*lang) || lang.starts_with(&t))
2727        .map(str::to_string)
2728        .collect();
2729    matched
2730}
2731
2732/// The ANSI SGR code for highlighting matches, or `None` to disable color.
2733/// Off unless stdout is a terminal; honors `NO_COLOR`; takes the match style
2734/// from `GREP_COLORS` (`mt`/`ms`) when set, else grep's default bold red.
2735fn match_color() -> Option<String> {
2736    if std::env::var_os("NO_COLOR").is_some() || !std::io::stdout().is_terminal() {
2737        return None;
2738    }
2739    let style = std::env::var("GREP_COLORS").ok().and_then(|gc| {
2740        gc.split(':').find_map(|e| {
2741            e.strip_prefix("mt=")
2742                .or_else(|| e.strip_prefix("ms="))
2743                .filter(|v| !v.is_empty())
2744                .map(str::to_string)
2745        })
2746    });
2747    Some(style.unwrap_or_else(|| "1;31".to_string()))
2748}
2749
2750/// Highlight the chars of `text` that `query` matched (no-op when `color` is
2751/// `None`, e.g. piped output).
2752fn hl(text: &str, query: &str, color: Option<&str>) -> String {
2753    match color {
2754        Some(c) => highlight(text, &crate::search::match_positions(query, text), c),
2755        None => text.to_string(),
2756    }
2757}
2758
2759/// Like [`hl`], but only over a path's filename — so matched chars light up in
2760/// `payrolls_controller.rb`, not scattered across the directory parts.
2761fn hl_path(path: &str, query: &str, color: Option<&str>) -> String {
2762    let Some(c) = color else {
2763        return path.to_string();
2764    };
2765    let base_byte = path.rfind('/').map(|b| b + 1).unwrap_or(0);
2766    let base_start = path[..base_byte].chars().count();
2767    // align on the filename *stem* (drop the extension), the same string the
2768    // scorer matched — so the query can't straggle into `.rb` instead of lighting
2769    // up the logical name (`employees_controller`)
2770    let stem = crate::search::path_stem(path);
2771    let positions: Vec<usize> = crate::search::match_positions(query, stem)
2772        .into_iter()
2773        .map(|p| p + base_start)
2774        .collect();
2775    highlight(path, &positions, c)
2776}
2777
2778/// Wrap the matched character positions of `text` in an ANSI color run.
2779/// Consecutive matched chars share one escape sequence.
2780fn highlight(text: &str, positions: &[usize], color: &str) -> String {
2781    if positions.is_empty() {
2782        return text.to_string();
2783    }
2784    let matched: std::collections::HashSet<usize> = positions.iter().copied().collect();
2785    let mut out = String::new();
2786    let mut on = false;
2787    for (i, c) in text.chars().enumerate() {
2788        match (matched.contains(&i), on) {
2789            (true, false) => {
2790                out.push_str("\x1b[");
2791                out.push_str(color);
2792                out.push('m');
2793                on = true;
2794            }
2795            (false, true) => {
2796                out.push_str("\x1b[0m");
2797                on = false;
2798            }
2799            _ => {}
2800        }
2801        out.push(c);
2802    }
2803    if on {
2804        out.push_str("\x1b[0m");
2805    }
2806    out
2807}
2808
2809/// Whether a repo-relative `file` sits under one of the `--path` directories
2810/// (prefix match on a path boundary). `app/services` matches
2811/// `app/services/refund.rb` but not `app/services_old/x.rb`.
2812fn under_any(file: &str, paths: &[String]) -> bool {
2813    paths.iter().any(|p| {
2814        let p = p.trim_start_matches("./").trim_end_matches('/');
2815        p.is_empty() || file == p || file.starts_with(&format!("{p}/"))
2816    })
2817}
2818
2819/// Resolve a possibly-absolute or cwd-relative path to a repo-relative one.
2820fn repo_relative(root: &std::path::Path, cwd: &std::path::Path, file: &str) -> String {
2821    let p = std::path::Path::new(file);
2822    let abs = if p.is_absolute() {
2823        p.to_path_buf()
2824    } else {
2825        cwd.join(p)
2826    };
2827    let abs = abs.canonicalize().unwrap_or(abs);
2828    abs.strip_prefix(root)
2829        .map(|r| r.to_string_lossy().into_owned())
2830        .unwrap_or_else(|_| file.to_string())
2831}
2832
2833/// Revalidate the files behind the top hits against disk — read from the same
2834/// checkout their signatures will be (see [`hit_file_roots`]) — refreshing any
2835/// that changed. Returns true if anything changed (so the caller re-runs the
2836/// search).
2837fn revalidate_top(store: &mut Store, hits: &[crate::search::Hit], here: Option<Here>) -> bool {
2838    use std::collections::HashSet;
2839    let mut seen = HashSet::new();
2840    let mut changed = false;
2841    for hit in hits {
2842        if !seen.insert((hit.repo_identity.as_str(), hit.file.as_str())) {
2843            continue;
2844        }
2845        let Some(repo_id) = store.repository_id(&hit.repo_identity).ok().flatten() else {
2846            continue;
2847        };
2848        let roots = hit_file_roots(store, &hit.repo_identity, here);
2849        let Some(root) = roots.iter().find(|r| r.join(&hit.file).is_file()) else {
2850            continue;
2851        };
2852        if let Ok(crate::index::Refresh::Updated) =
2853            crate::index::refresh_file(store, repo_id, root, &hit.file)
2854        {
2855            changed = true;
2856        }
2857    }
2858    changed
2859}
2860
2861/// The repository's normalized identity for `cwd`, cache-first: look it up by
2862/// the canonical cwd (the checkout root indexing records), so a known repo (git
2863/// or explicitly `--index`ed) costs no `git` fork. On a cache miss, a non-git
2864/// dir resolves to its `local:` path directly (still no fork); only a git work
2865/// tree we haven't seen yet pays a `git remote` call.
2866fn resolve_identity(store: &Store, cwd: &std::path::Path) -> String {
2867    if let Ok(canon) = cwd.canonicalize() {
2868        if let Ok(Some(identity)) = store.identity_for_root(&canon.to_string_lossy()) {
2869            return identity;
2870        }
2871        if crate::index::repo_root(cwd).is_none() {
2872            return crate::core::RepoIdentity::local(&canon.to_string_lossy()).to_string();
2873        }
2874    }
2875    crate::index::detect_identity(cwd).to_string()
2876}
2877
2878fn cmd_index(path: Option<PathBuf>, subdirs: &[String], out: Output) -> ExitCode {
2879    let explicit = path.is_some();
2880    let target = path.unwrap_or_else(|| PathBuf::from("."));
2881    // Normalize to the repo root: the index is repo-root-relative, so indexing
2882    // from a subdirectory must still key off the root (a subdir-relative index
2883    // would mismatch a later search and get reconciled away). `--path` scopes a
2884    // subset; outside git the target is used as-is.
2885    let root = crate::index::repo_root(&target).unwrap_or_else(|| target.clone());
2886    // An explicit TARGET *inside* the repo scopes the index to that subtree — the
2887    // user pointed at a subdir, not the whole repo, and shouldn't pay to walk
2888    // everything. Folded in alongside any `--path` subdirs. (A bare `rq --index`
2889    // with no target still walks the whole repo.)
2890    let mut subdirs = subdirs.to_vec();
2891    if explicit
2892        && let (Ok(t), Ok(r)) = (target.canonicalize(), root.canonicalize())
2893        && t != r
2894        && let Ok(rel) = t.strip_prefix(&r)
2895        && !rel.as_os_str().is_empty()
2896    {
2897        subdirs.push(rel.to_string_lossy().into_owned());
2898    }
2899    let open_span = crate::profile::span("store open");
2900    let mut store = match open_store() {
2901        Ok(s) => s,
2902        Err(e) => {
2903            return fail(
2904                out,
2905                Failure::Database,
2906                format_args!("rq: cannot open database: {e}"),
2907            );
2908        }
2909    };
2910    drop(open_span);
2911    let indexed = crate::index::index_under(&mut store, &root, &subdirs);
2912    // After the index, which has just recorded this checkout's identity — so
2913    // this is a cache hit rather than a second `git remote` fork.
2914    let identity = resolve_identity(&store, &root);
2915    match indexed {
2916        Ok(stats) => {
2917            let subtree = !subdirs.is_empty();
2918            // distinguish this run's incremental work from the index totals
2919            let totals = store
2920                .repository_id(&identity)
2921                .ok()
2922                .flatten()
2923                .and_then(|id| store.repo_totals(id).ok());
2924            match out {
2925                Output::Json | Output::Ndjson => {
2926                    let (files, symbols) = match totals {
2927                        Some((f, s)) => (Some(f), Some(s)),
2928                        None => (None, None),
2929                    };
2930                    return emit_json(
2931                        out,
2932                        &serde_json::json!({
2933                            "repo": identity,
2934                            "scope": if subtree { "subtree" } else { "full" },
2935                            "files_added": stats.files_indexed,
2936                            "symbols_added": stats.symbols,
2937                            "files": files,
2938                            "symbols": symbols,
2939                        }),
2940                    );
2941                }
2942                Output::Text => {
2943                    let scope = if subtree { " (subtree seed)" } else { "" };
2944                    match totals {
2945                        Some((files, symbols)) => println!(
2946                            "{} file(s)/{} symbol(s) added this run; index{scope} now {files} files, {symbols} symbols",
2947                            stats.files_indexed, stats.symbols
2948                        ),
2949                        None => println!(
2950                            "{} file(s)/{} symbol(s) added this run{scope}",
2951                            stats.files_indexed, stats.symbols
2952                        ),
2953                    }
2954                }
2955            }
2956            ExitCode::SUCCESS
2957        }
2958        Err(e) => fail(out, Failure::Index, format_args!("rq --index: {e}")),
2959    }
2960}
2961
2962fn cmd_drop(target: Option<String>, out: Output) -> ExitCode {
2963    let mut store = match open_store() {
2964        Ok(s) => s,
2965        Err(e) => {
2966            return fail(
2967                out,
2968                Failure::Database,
2969                format_args!("rq: cannot open database: {e}"),
2970            );
2971        }
2972    };
2973
2974    // Resolve the repo to drop: TARGET as a path (→ repo root → identity, like
2975    // --index), falling back to TARGET as a literal identity string — so cruft
2976    // shown by --status can be dropped by name even if the checkout is gone.
2977    let path = PathBuf::from(target.clone().unwrap_or_else(|| ".".to_string()));
2978    let root = crate::index::repo_root(&path).unwrap_or(path);
2979    let from_path = crate::index::detect_identity(&root).to_string();
2980    let resolved = match store.repository_id(&from_path) {
2981        Ok(Some(id)) => Some((from_path.clone(), id)),
2982        Ok(None) => target.as_deref().and_then(|s| {
2983            store
2984                .repository_id(s)
2985                .ok()
2986                .flatten()
2987                .map(|id| (s.to_string(), id))
2988        }),
2989        Err(e) => return fail(out, Failure::Database, format_args!("rq --drop: {e}")),
2990    };
2991
2992    let Some((identity, repo_id)) = resolved else {
2993        // nothing to drop — idempotent. `dropped: false` lets a script tell.
2994        return match out {
2995            Output::Text => {
2996                println!("not indexed: {from_path}");
2997                ExitCode::SUCCESS
2998            }
2999            _ => emit_json(
3000                out,
3001                &serde_json::json!({"repo": from_path, "files": 0, "symbols": 0, "dropped": false}),
3002            ),
3003        };
3004    };
3005
3006    let (files, symbols) = store.repo_totals(repo_id).unwrap_or((0, 0));
3007    match store.drop_repository(repo_id) {
3008        Ok(()) => match out {
3009            Output::Text => {
3010                println!("dropped {identity} ({files} file(s), {symbols} symbol(s))");
3011                ExitCode::SUCCESS
3012            }
3013            _ => emit_json(
3014                out,
3015                &serde_json::json!({"repo": identity, "files": files, "symbols": symbols, "dropped": true}),
3016            ),
3017        },
3018        Err(e) => fail(out, Failure::Database, format_args!("rq --drop: {e}")),
3019    }
3020}
3021
3022/// Print a single value as JSON: `--json` pretty, `--ndjson` compact one-liner.
3023/// Used by the single-object operations (`--index`, `--drop`) and the
3024/// no-match status objects; [`emit_rows`] is the multi-row twin.
3025fn emit_json<T: serde::Serialize>(out: Output, value: &T) -> ExitCode {
3026    let rendered = if out == Output::Json {
3027        serde_json::to_string_pretty(value)
3028    } else {
3029        serde_json::to_string(value)
3030    };
3031    match rendered {
3032        Ok(s) => {
3033            println!("{s}");
3034            ExitCode::SUCCESS
3035        }
3036        Err(e) => fail(out, Failure::Internal, format_args!("rq: {e}")),
3037    }
3038}
3039
3040/// Print a row set as structured output: `--json` one pretty array, `--ndjson`
3041/// one compact object per line. Returns `Some(exit)` on a serialization
3042/// failure, `None` on success (Text output is the caller's business).
3043fn emit_rows<T: serde::Serialize>(out: Output, rows: &[T]) -> Option<ExitCode> {
3044    match out {
3045        Output::Json => match serde_json::to_string_pretty(rows) {
3046            Ok(s) => println!("{s}"),
3047            Err(e) => return Some(fail(out, Failure::Internal, format_args!("rq: {e}"))),
3048        },
3049        Output::Ndjson => {
3050            for r in rows {
3051                match serde_json::to_string(r) {
3052                    Ok(line) => println!("{line}"),
3053                    Err(e) => return Some(fail(out, Failure::Internal, format_args!("rq: {e}"))),
3054                }
3055            }
3056        }
3057        Output::Text => {}
3058    }
3059    None
3060}
3061
3062fn cmd_status(out: Output) -> ExitCode {
3063    let store = match open_store() {
3064        Ok(s) => s,
3065        Err(e) => {
3066            return fail(
3067                out,
3068                Failure::Database,
3069                format_args!("rq: cannot open database: {e}"),
3070            );
3071        }
3072    };
3073    let rows = match store.coverage_overview() {
3074        Ok(rows) => rows,
3075        Err(e) => return fail(out, Failure::Database, format_args!("rq --status: {e}")),
3076    };
3077    if let Some(code) = emit_rows(out, &rows) {
3078        return code;
3079    }
3080    match out {
3081        Output::Json | Output::Ndjson => {}
3082        Output::Text if rows.is_empty() => {
3083            println!("no repositories indexed yet (try `rq --index`)");
3084        }
3085        Output::Text => {
3086            for r in &rows {
3087                println!(
3088                    "{:<10} {:>6} files  {:>7} symbols  {}",
3089                    r.status, r.files, r.symbols, r.identity
3090                );
3091            }
3092        }
3093    }
3094    ExitCode::SUCCESS
3095}
3096
3097/// `--usage`: how rq has actually been called, by day, caller, and flag set.
3098/// Reads `usage_daily`, which outlives the pruned raw event log.
3099fn cmd_usage(out: Output) -> ExitCode {
3100    let store = match open_store() {
3101        Ok(s) => s,
3102        Err(e) => {
3103            return fail(
3104                out,
3105                Failure::Database,
3106                format_args!("rq: cannot open database: {e}"),
3107            );
3108        }
3109    };
3110    let rows = match store.usage_overview() {
3111        Ok(rows) => rows,
3112        Err(e) => return fail(out, Failure::Database, format_args!("rq --usage: {e}")),
3113    };
3114    if let Some(code) = emit_rows(out, &rows) {
3115        return code;
3116    }
3117    match out {
3118        Output::Json | Output::Ndjson => {}
3119        Output::Text if rows.is_empty() => {
3120            println!("no usage recorded yet");
3121        }
3122        Output::Text => {
3123            // Columns of bare numbers need naming; `--status` gets away without
3124            // a header because its columns carry their own units.
3125            println!(
3126                "{:<10}  {:<16} {:>6} {:>7} {:>8}  flags",
3127                "day", "caller", "found", "missed", "warming"
3128            );
3129            for r in &rows {
3130                let flags = if r.flags.is_empty() { "-" } else { &r.flags };
3131                println!(
3132                    "{:<10}  {:<16} {:>6} {:>7} {:>8}  {}",
3133                    r.day,
3134                    r.source,
3135                    r.searches - r.misses - r.warming,
3136                    r.misses,
3137                    r.warming,
3138                    flags
3139                );
3140            }
3141            let searches: i64 = rows.iter().map(|r| r.searches).sum();
3142            let misses: i64 = rows.iter().map(|r| r.misses).sum();
3143            let warming: i64 = rows.iter().map(|r| r.warming).sum();
3144            let complete: i64 = rows.iter().map(|r| r.on_complete).sum();
3145            let live: i64 = rows.iter().map(|r| r.live).sum();
3146            let plural = if searches == 1 { "search" } else { "searches" };
3147            // rare, and only outside a repo: named when it happened at all
3148            let live = if live > 0 {
3149                format!(" · {live} from a live scan")
3150            } else {
3151                String::new()
3152            };
3153            // Counts, not a percentage: these totals are often small enough
3154            // that a percentage would read as more evidence than there is.
3155            println!(
3156                "{searches} {plural} · {misses} missed · {warming} asked too early · {complete} on a complete index{live}"
3157            );
3158        }
3159    }
3160    // Nothing recorded is the "nothing happened" case, like an empty --status.
3161    if rows.is_empty() {
3162        return ExitCode::from(1);
3163    }
3164    ExitCode::SUCCESS
3165}
3166
3167/// Open the rq database, honoring `RQ_DB` and creating parent dirs.
3168fn open_store() -> Result<Store, Box<dyn std::error::Error>> {
3169    let path = db_path()?;
3170    if let Some(parent) = path.parent() {
3171        std::fs::create_dir_all(parent)?;
3172    }
3173    Ok(Store::open(&path)?)
3174}
3175
3176/// Resolve the database path: `$RQ_DB`, else `$HOME/.local/share/rq/rq.db`.
3177fn db_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
3178    if let Ok(p) = std::env::var("RQ_DB") {
3179        return Ok(PathBuf::from(p));
3180    }
3181    let home = std::env::var("HOME")?;
3182    Ok(PathBuf::from(home).join(".local/share/rq/rq.db"))
3183}
3184
3185/// What kind of thing went wrong: the stable `kind` of a structured error.
3186#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3187enum Failure {
3188    /// The command line asks for something rq can't do.
3189    Usage,
3190    /// The index can't be opened or read.
3191    Database,
3192    /// A file the command names doesn't exist.
3193    NotFound,
3194    /// `--web` on a repo with no git host to link to.
3195    NoRemote,
3196    /// The editor or browser couldn't be started.
3197    Launch,
3198    /// `--index` failed part-way.
3199    Index,
3200    /// rq couldn't render its own output.
3201    Internal,
3202}
3203
3204impl Failure {
3205    fn as_str(self) -> &'static str {
3206        match self {
3207            Failure::Usage => "usage",
3208            Failure::Database => "database",
3209            Failure::NotFound => "not_found",
3210            Failure::NoRemote => "no_remote",
3211            Failure::Launch => "launch",
3212            Failure::Index => "index",
3213            Failure::Internal => "internal",
3214        }
3215    }
3216
3217    /// The process exit code, from sysexits(3) so no error shares a code with
3218    /// a verdict (0 hit, 1 miss, 2 warming). Coarser than `kind`: one code per
3219    /// thing the caller does about it.
3220    fn exit_code(self) -> u8 {
3221        match self {
3222            Failure::Usage => 64,                      // EX_USAGE: fix the command
3223            Failure::NotFound => 66,                   // EX_NOINPUT: fix the path
3224            Failure::NoRemote | Failure::Launch => 69, // EX_UNAVAILABLE
3225            Failure::Internal => 70,                   // EX_SOFTWARE: a bug
3226            Failure::Database | Failure::Index => 74,  // EX_IOERR
3227        }
3228    }
3229}
3230
3231/// Report an error and return its exit code. The message always goes to
3232/// stderr; a structured caller also gets it as one JSON object on stdout.
3233fn fail(out: Output, kind: Failure, args: std::fmt::Arguments) -> ExitCode {
3234    let message = args.to_string();
3235    eprintln!("{message}");
3236    emit_error(out, kind, &message);
3237    ExitCode::from(kind.exit_code())
3238}
3239
3240/// The structured half of an error: `{"error", "kind", "code"}` on stdout,
3241/// nothing for text. `code` is the exit code the process leaves with.
3242fn emit_error(out: Output, kind: Failure, message: &str) {
3243    let obj = serde_json::json!({
3244        "error": message,
3245        "kind": kind.as_str(),
3246        "code": kind.exit_code(),
3247    });
3248    // Printed directly: `emit_json` reports its own failures through here.
3249    let rendered = match out {
3250        Output::Text => return,
3251        Output::Json => serde_json::to_string_pretty(&obj),
3252        Output::Ndjson => serde_json::to_string(&obj),
3253    };
3254    if let Ok(s) = rendered {
3255        println!("{s}");
3256    }
3257}
3258
3259/// A command line clap rejected. It fails before rq knows its output mode, so
3260/// the structured flags are read off argv directly: a caller that asked for
3261/// JSON gets its usage error as JSON too. Not `err.exit()`: clap exits 2,
3262/// which rq reserves for warming.
3263fn clap_failure(err: clap::Error) -> ExitCode {
3264    // help and --version aren't errors
3265    if !err.use_stderr() {
3266        err.exit();
3267    }
3268    let _ = err.print();
3269    let out = requested_output(std::env::args_os().skip(1));
3270    let text = err.to_string();
3271    emit_error(out, Failure::Usage, text.lines().next().unwrap_or(""));
3272    ExitCode::from(Failure::Usage.exit_code())
3273}
3274
3275/// The output mode argv asks for, without a full parse: `--json`/`--ndjson`,
3276/// or `-j`/`-J` alone or in a cluster of short flags (`-ej`). A cluster ends at
3277/// the first flag that takes a value, since the rest is that value (`-xj` is
3278/// `--lang j`). Nothing after `--` is a flag.
3279fn requested_output(args: impl IntoIterator<Item = std::ffi::OsString>) -> Output {
3280    let cmd = Cli::command();
3281    let takes_value = |c: char| {
3282        cmd.get_arguments()
3283            .any(|a| a.get_short() == Some(c) && a.get_action().takes_values())
3284    };
3285    let (mut json, mut ndjson) = (false, false);
3286    for arg in args {
3287        let arg = arg.to_string_lossy();
3288        match arg.as_ref() {
3289            "--" => break,
3290            "--json" => json = true,
3291            "--ndjson" => ndjson = true,
3292            a if a.starts_with('-') && !a.starts_with("--") => {
3293                for c in a.chars().skip(1) {
3294                    match c {
3295                        'j' => json = true,
3296                        'J' => ndjson = true,
3297                        c if takes_value(c) => break,
3298                        _ => {}
3299                    }
3300                }
3301            }
3302            _ => {}
3303        }
3304    }
3305    // the same precedence `output_format` gives a parsed command line
3306    if ndjson {
3307        Output::Ndjson
3308    } else if json {
3309        Output::Json
3310    } else {
3311        Output::Text
3312    }
3313}
3314
3315#[cfg(test)]
3316mod tests {
3317    use super::*;
3318
3319    #[test]
3320    fn parses_an_anchor_from_the_right() {
3321        let ok = |file: &str, line| {
3322            Ok(AnchorSpec {
3323                file: PathBuf::from(file),
3324                line,
3325            })
3326        };
3327        assert_eq!(parse_anchor("app/w.rb:42"), ok("app/w.rb", 42));
3328        assert_eq!(parse_anchor("app/w.rb:42:7"), ok("app/w.rb", 42));
3329        assert_eq!(parse_anchor("C:odd/w.rb:3"), ok("C:odd/w.rb", 3));
3330        for bad in ["app/w.rb", "app/w.rb:", "app/w.rb:0", ":12"] {
3331            assert!(parse_anchor(bad).is_err(), "{bad}");
3332        }
3333    }
3334
3335    #[test]
3336    fn the_output_mode_is_read_off_argv_before_clap_parses_it() {
3337        let mode = |args: &[&str]| requested_output(args.iter().map(std::ffi::OsString::from));
3338        for (args, want) in [
3339            (&["x", "--json"][..], Output::Json),
3340            (&["x", "--ndjson"], Output::Ndjson),
3341            (&["x", "-ej"], Output::Json),
3342            (&["-Je", "x"], Output::Ndjson),
3343            (&["x", "--json", "-J"], Output::Ndjson),
3344            (&["x"], Output::Text),
3345            // the rest of a cluster after a value-taking flag is its value
3346            (&["x", "-xj"], Output::Text),
3347            (&["x", "-l5j"], Output::Text),
3348            // and after `--` nothing is a flag
3349            (&["--", "-j"], Output::Text),
3350            (&["x", "--jobs", "2"], Output::Text),
3351        ] {
3352            assert!(mode(args) == want, "{args:?}");
3353        }
3354    }
3355
3356    #[test]
3357    fn open_menu_choice_parsing() {
3358        // blank reply takes the top match; a valid number maps to its index
3359        assert_eq!(parse_choice("\n", 5), Some(0));
3360        assert_eq!(parse_choice("  ", 5), Some(0));
3361        assert_eq!(parse_choice("3", 5), Some(2));
3362        assert_eq!(parse_choice("5", 5), Some(4));
3363        // out of range, zero, or non-numeric aborts
3364        assert_eq!(parse_choice("6", 5), None);
3365        assert_eq!(parse_choice("0", 5), None);
3366        assert_eq!(parse_choice("q", 5), None);
3367    }
3368
3369    #[test]
3370    fn web_url_shape() {
3371        assert_eq!(
3372            web_url("github.com/org/repo", "abc123", "src/a b#.rs", 42),
3373            "https://github.com/org/repo/blob/abc123/src/a%20b%23.rs#L42"
3374        );
3375    }
3376
3377    #[test]
3378    fn wait_duration_parsing() {
3379        use std::time::Duration;
3380        // units: ms / s / m, and a bare number is seconds
3381        assert_eq!(parse_wait("50ms"), Ok(Duration::from_millis(50)));
3382        assert_eq!(parse_wait("2s"), Ok(Duration::from_secs(2)));
3383        assert_eq!(parse_wait("1m"), Ok(Duration::from_secs(60)));
3384        assert_eq!(parse_wait("250"), Ok(Duration::from_secs(250)));
3385        // fractions and zero
3386        assert_eq!(parse_wait("1.5s"), Ok(Duration::from_millis(1500)));
3387        assert_eq!(parse_wait("0"), Ok(Duration::ZERO));
3388        assert!(parse_wait("0s").unwrap().is_zero());
3389        // surrounding whitespace is tolerated
3390        assert_eq!(parse_wait(" 2s "), Ok(Duration::from_secs(2)));
3391        // garbage, empty, and negatives are rejected (a usage error at parse time)
3392        assert!(parse_wait("2x").is_err());
3393        assert!(parse_wait("").is_err());
3394        assert!(parse_wait("s").is_err());
3395        assert!(parse_wait("-1s").is_err());
3396    }
3397
3398    #[test]
3399    fn leading_kind_keyword_becomes_a_kind_filter() {
3400        let d = |s: &[&str]| s.iter().map(|x| x.to_string()).collect::<Vec<_>>();
3401        // unquoted: `rq class Widget` — keyword + next positional is the query
3402        assert_eq!(
3403            split_kind_keyword("class".into(), d(&["Widget"])),
3404            (Some("class"), "Widget".into(), vec![])
3405        );
3406        // quoted: `rq 'method zoom'` — one arg, peel the first word
3407        assert_eq!(
3408            split_kind_keyword("method zoom".into(), vec![]),
3409            (Some("method"), "zoom".into(), vec![])
3410        );
3411        // `fn` is an alias for function; composes with a qualifier tail
3412        assert_eq!(
3413            split_kind_keyword("fn".into(), d(&["Foo::run"])),
3414            (Some("function"), "Foo::run".into(), vec![])
3415        );
3416        // extra positionals after the query stay as rg-style path dirs
3417        assert_eq!(
3418            split_kind_keyword("struct".into(), d(&["Gadget", "src"])),
3419            (Some("struct"), "Gadget".into(), d(&["src"]))
3420        );
3421    }
3422
3423    #[test]
3424    fn a_bare_or_non_keyword_query_is_left_alone() {
3425        let d = |s: &[&str]| s.iter().map(|x| x.to_string()).collect::<Vec<_>>();
3426        // a keyword with no following query token is a search for that literal name
3427        assert_eq!(
3428            split_kind_keyword("class".into(), vec![]),
3429            (None, "class".into(), vec![])
3430        );
3431        // an ordinary query is untouched, trailing dirs preserved
3432        assert_eq!(
3433            split_kind_keyword("Widget".into(), d(&["app"])),
3434            (None, "Widget".into(), d(&["app"]))
3435        );
3436        // single-letter `-k` shortcuts are NOT keywords here (too query-like)
3437        assert_eq!(
3438            split_kind_keyword("c".into(), d(&["Foo"])),
3439            (None, "c".into(), d(&["Foo"]))
3440        );
3441    }
3442
3443    #[test]
3444    fn the_branch_window_scales_with_what_the_refresh_costs() {
3445        // a cheap refresh keeps the default window exactly — small repos see no
3446        // change in behaviour at all
3447        assert_eq!(branch_files_ttl(Some(5)), BRANCH_FILES_TTL_SECS);
3448        assert_eq!(branch_files_ttl(Some(150)), BRANCH_FILES_TTL_SECS);
3449        // an expensive one earns a proportionally longer window: the refresh
3450        // runs alongside the query and competes with it for disk, so a 700ms
3451        // rebuild every 15s costs more than the searches it decorates
3452        assert_eq!(branch_files_ttl(Some(700)), 70);
3453        assert_eq!(branch_files_ttl(Some(2_000)), 200);
3454        // never indefinite — the window is the only thing that notices an
3455        // unstaged edit, since every git operation invalidates by stamp
3456        assert_eq!(branch_files_ttl(Some(60_000)), BRANCH_FILES_TTL_MAX_SECS);
3457        assert_eq!(branch_files_ttl(Some(u64::MAX)), BRANCH_FILES_TTL_MAX_SECS);
3458        // an entry written before the cost was recorded falls back to default
3459        assert_eq!(branch_files_ttl(None), BRANCH_FILES_TTL_SECS);
3460    }
3461
3462    #[test]
3463    fn a_language_selects_by_prefix_or_alias() {
3464        // a prefix can name more than one language
3465        assert_eq!(canonical_langs("r"), ["ruby", "rust"]);
3466        assert_eq!(canonical_langs("t"), ["typescript"]);
3467        // the names people actually type aren't prefixes of the tag
3468        assert_eq!(canonical_langs("ts"), ["typescript"]);
3469        assert_eq!(canonical_langs("jsx"), ["javascript"]);
3470        assert_eq!(canonical_langs("rb"), ["ruby"]);
3471        // an unknown value matches nothing, so the caller can reject it rather
3472        // than silently filtering every result away
3473        assert!(canonical_langs("COBOL").is_empty());
3474    }
3475
3476    #[test]
3477    fn a_kind_normalizes_language_specific_spellings() {
3478        assert_eq!(canonical_kind("f"), Some("function"));
3479        // TypeScript's spellings land on the shared model's kinds
3480        assert_eq!(canonical_kind("interface"), Some("trait"));
3481        assert_eq!(canonical_kind("type"), Some("struct"));
3482        assert_eq!(canonical_kind("const"), Some("constant"));
3483        assert_eq!(canonical_kind("banana"), None);
3484        // …and work as the leading-keyword shorthand too
3485        let d = |s: &[&str]| s.iter().map(|x| x.to_string()).collect::<Vec<_>>();
3486        assert_eq!(
3487            split_kind_keyword("interface".into(), d(&["Renderer"])),
3488            (Some("trait"), "Renderer".into(), vec![])
3489        );
3490    }
3491
3492    #[test]
3493    fn highlight_wraps_matched_runs() {
3494        assert_eq!(
3495            highlight("FooThing", &[0, 1, 2], "1;31"),
3496            "\u{1b}[1;31mFoo\u{1b}[0mThing"
3497        );
3498        // scattered matches get separate runs
3499        assert_eq!(
3500            highlight("FooThing", &[0, 3], "1"),
3501            "\u{1b}[1mF\u{1b}[0moo\u{1b}[1mT\u{1b}[0mhing"
3502        );
3503        // nothing matched → unchanged
3504        assert_eq!(highlight("FooThing", &[], "1;31"), "FooThing");
3505    }
3506
3507    #[test]
3508    fn progress_ui_only_for_an_interactive_text_terminal() {
3509        // a person at a terminal, plain text → live progress + graceful Ctrl-C
3510        assert!(show_progress(Output::Text, true));
3511
3512        // machine-readable output blocks silently (no progress line to corrupt it)
3513        assert!(!show_progress(Output::Json, true));
3514        assert!(!show_progress(Output::Ndjson, true));
3515
3516        // not a terminal (a script/agent/pipe) — block, but without the UI
3517        assert!(!show_progress(Output::Text, false));
3518    }
3519
3520    #[test]
3521    fn signature_in_reads_one_based_trimmed_nonblank_lines() {
3522        let lines = ["class Widget", "", "  def go"];
3523        assert_eq!(signature_in(&lines, 1).as_deref(), Some("class Widget"));
3524        assert_eq!(signature_in(&lines, 3).as_deref(), Some("def go"));
3525        assert_eq!(signature_in(&lines, 2), None, "blank line");
3526        assert_eq!(signature_in(&lines, 0), None, "lines are 1-based");
3527        assert_eq!(signature_in(&lines, 4), None, "past the end");
3528    }
3529
3530    #[test]
3531    fn repo_label_uses_the_directory_name() {
3532        assert_eq!(
3533            repo_label(Some(std::path::Path::new("/src/widgets"))),
3534            "widgets"
3535        );
3536        assert_eq!(repo_label(None), "repo");
3537    }
3538
3539    #[test]
3540    fn hl_path_highlights_the_stem_not_the_extension() {
3541        // matching `employeescontroller`, the highlight covers the logical name in
3542        // the stem and never straggles into `.rb`
3543        let out = hl_path(
3544            "app/employees_controller.rb",
3545            "employeescontroller",
3546            Some("1;31"),
3547        );
3548        assert!(
3549            out.starts_with("app/\u{1b}[1;31memployees"),
3550            "stem highlighted: {out:?}"
3551        );
3552        assert!(
3553            out.ends_with("controller\u{1b}[0m.rb"),
3554            "`.rb` left un-highlighted: {out:?}"
3555        );
3556    }
3557}