reflex/models.rs
1//! Core data models for Reflex
2//!
3//! These structures represent the normalized, deterministic output format
4//! that Reflex provides to AI agents and other programmatic consumers.
5
6use serde::{Deserialize, Serialize};
7use strum::{Display, EnumString};
8
9/// Represents a source code location span (line range only)
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
11pub struct Span {
12 /// Starting line number (1-indexed)
13 pub start_line: usize,
14 /// Ending line number (1-indexed)
15 pub end_line: usize,
16}
17
18impl Span {
19 pub fn new(start_line: usize, start_col: usize, end_line: usize, end_col: usize) -> Self {
20 // Ignore col parameters for backwards compatibility
21 let _ = (start_col, end_col);
22 Self {
23 start_line,
24 end_line,
25 }
26 }
27}
28
29/// Type of symbol found in code
30#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, EnumString, Display)]
31#[strum(serialize_all = "PascalCase")]
32pub enum SymbolKind {
33 Function,
34 Class,
35 Struct,
36 Enum,
37 Interface,
38 Trait,
39 Constant,
40 Variable,
41 Method,
42 Module,
43 Namespace,
44 Type,
45 Macro,
46 Property,
47 Event,
48 Import,
49 Export,
50 Attribute,
51 /// Catch-all for symbol kinds not yet explicitly supported.
52 /// This ensures no data loss when encountering new tree-sitter node types.
53 /// The string contains the original kind name from the parser.
54 #[strum(default)]
55 Unknown(String),
56}
57
58/// Programming language identifier
59#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
60#[serde(rename_all = "lowercase")]
61pub enum Language {
62 #[default]
63 Rust,
64 Python,
65 JavaScript,
66 TypeScript,
67 Vue,
68 Svelte,
69 Go,
70 Java,
71 PHP,
72 C,
73 Cpp,
74 CSharp,
75 Ruby,
76 Kotlin,
77 Swift,
78 Zig,
79 /// Plain-text tier: documentation, config and templates.
80 ///
81 /// Trigram-indexed only — no tree-sitter grammar, no symbol extraction, no
82 /// import extraction. Added because agents do not partition searches by file
83 /// type: a config key lives in the YAML, the Rust struct AND the spec paragraph,
84 /// and Reflex used to return the struct and a confident 0 for the rest.
85 ///
86 /// Serialises as `"text"` (the enum is `rename_all = "lowercase"`).
87 Text,
88 /// Lock files (`Cargo.lock`, `package-lock.json`, `*.lock`, …).
89 ///
90 /// Indexed since 2.0.0 in `[index] mode = "tracked"`, but excluded from every
91 /// search by default: 100k lines of pinned versions are noise for almost every
92 /// query and the one real question ("which lockfile pins serde 1.0.190?") is
93 /// asked with `include_locks: true` or `lang: "lock"`.
94 Lock,
95 /// Generated files judged by name: `*.pb.go`, `*_generated.*`, `*.generated.*`,
96 /// `*.min.js`, `*.min.css`, `*.map`. Indexed, excluded by default,
97 /// `include_generated: true` or `lang: "generated"` to widen.
98 Generated,
99 /// Never produced by [`Language::from_path`] since 2.0.0 (every path classifies
100 /// as code, `Text`, `Lock` or `Generated`); kept as the miss value of
101 /// [`Language::from_extension`] and as the forward-compatible sentinel for
102 /// readers of the `language` field.
103 Unknown,
104}
105
106/// Whether `file_name` is a dependency lock file.
107///
108/// Judged by full name: `package-lock.json` matches a text extension, and the
109/// name is the only thing that tells it apart from `settings.json`.
110pub fn is_lock_file(file_name: &str) -> bool {
111 if TEXT_FILENAME_EXCLUSIONS
112 .iter()
113 .any(|n| n.eq_ignore_ascii_case(file_name))
114 {
115 return true;
116 }
117 // `*-lock.json` and friends, beyond the names listed above.
118 file_name.ends_with("-lock.json") || file_name.ends_with(".lock")
119}
120
121/// Whether `file_name` looks like a generated file, judged by name only.
122///
123/// Content markers (`@generated` in the file head) are not read: the query engine
124/// derives a file's language from its path, so a content-based verdict at index
125/// time could not be honoured at query time.
126pub fn is_generated_name(file_name: &str) -> bool {
127 let lower = file_name.to_ascii_lowercase();
128 lower.ends_with(".pb.go")
129 || lower.ends_with(".min.js")
130 || lower.ends_with(".min.css")
131 || lower.ends_with(".map")
132 || lower.contains("_generated.")
133 || lower.contains(".generated.")
134}
135
136/// Extensions in the plain-text tier.
137///
138/// A fixed allowlist, not "everything unrecognised": an index that swallowed every
139/// binary blob and generated artefact in a repo would be slower and less useful.
140const TEXT_EXTENSIONS: &[&str] = &[
141 "md", "mdx", "txt", "yaml", "yml", "toml", "json", "proto", "html", "htm", "sh", "bash", "ini",
142 "cfg", "sql", "graphql", "bru",
143];
144
145/// Extensionless files in the plain-text tier, matched by exact name.
146///
147/// Agents grep these as readily as any `.md`; a `Makefile` target or a `Dockerfile`
148/// `COPY` line is a legitimate search hit. `Dockerfile.<variant>` is handled as a
149/// prefix in [`is_text_tier_file`].
150const TEXT_FILENAMES: &[&str] = &["Makefile", "makefile", "Dockerfile", "Justfile", "justfile"];
151
152/// Lock files, by exact name. See [`is_lock_file`] for the suffix rules.
153const TEXT_FILENAME_EXCLUSIONS: &[&str] = &[
154 "package-lock.json",
155 "npm-shrinkwrap.json",
156 "composer.lock",
157 "yarn.lock",
158 "pnpm-lock.yaml",
159 "Cargo.lock",
160 "poetry.lock",
161 "Gemfile.lock",
162 "go.sum",
163 "flake.lock",
164 "uv.lock",
165 "deno.lock",
166 "bun.lock",
167];
168
169/// Whether a file belongs in the ALLOWLIST text tier, judged by its full name.
170///
171/// This is the pre-2.0.0 rule, kept for `[index] mode = "allowlist"`. In the
172/// default `tracked` mode every non-binary, non-ignored file is text unless it is
173/// code, a lock file or a generated file.
174pub fn is_text_tier_file(file_name: &str) -> bool {
175 if is_lock_file(file_name) {
176 return false;
177 }
178 if TEXT_FILENAMES.contains(&file_name) || file_name.starts_with("Dockerfile.") {
179 return true;
180 }
181 match file_name.rsplit_once('.') {
182 Some((_, ext)) => TEXT_EXTENSIONS.contains(&ext.to_ascii_lowercase().as_str()),
183 None => false,
184 }
185}
186
187impl Language {
188 /// Classify a file by its path.
189 ///
190 /// This is the one classifier the indexer, watcher and query engine share, so a
191 /// file is either indexed, watched and searchable, or none of the three. Path
192 /// only: whether the file is binary is decided from its bytes by the indexer,
193 /// and a binary file is simply never in the index.
194 ///
195 /// * A lock file name wins (`Cargo.lock`, `package-lock.json`) → `Lock`.
196 /// * A generated name wins next (`x.pb.go`, `app.min.js`) → `Generated`.
197 /// * A recognised code extension → that language (`main.rs`, `app.mjs`).
198 /// * Everything else → `Text`: `README`, `OWNERS`, `foo.po`, `a.css`,
199 /// `Makefile`, `.githooks/pre-commit`. Whether such a file is INDEXED is the
200 /// `[index] mode` policy's decision (`PathPolicy::classify`).
201 pub fn from_path(path: &std::path::Path) -> Self {
202 let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
203 if is_lock_file(name) {
204 return Language::Lock;
205 }
206 if is_generated_name(name) {
207 return Language::Generated;
208 }
209 let by_ext = path
210 .extension()
211 .and_then(|e| e.to_str())
212 .map(Self::from_extension)
213 .unwrap_or(Language::Unknown);
214 match by_ext {
215 Language::Text | Language::Unknown => Language::Text,
216 code => code,
217 }
218 }
219
220 pub fn from_extension(ext: &str) -> Self {
221 match ext {
222 "rs" => Language::Rust,
223 "py" => Language::Python,
224 "js" | "mjs" | "cjs" | "jsx" => Language::JavaScript,
225 "ts" | "mts" | "cts" | "tsx" => Language::TypeScript,
226 "vue" => Language::Vue,
227 "svelte" => Language::Svelte,
228 "go" => Language::Go,
229 "java" => Language::Java,
230 "php" => Language::PHP,
231 "c" | "h" => Language::C,
232 "cpp" | "cc" | "cxx" | "hpp" | "hxx" | "C" | "H" => Language::Cpp,
233 "cs" => Language::CSharp,
234 "rb" | "rake" | "gemspec" => Language::Ruby,
235 "kt" | "kts" => Language::Kotlin,
236 "swift" => Language::Swift,
237 "zig" => Language::Zig,
238 // The text tier. Note this maps by EXTENSION only; `is_text_tier_file`
239 // additionally excludes lock files by name, and the indexer uses that.
240 ext if TEXT_EXTENSIONS.contains(&ext.to_ascii_lowercase().as_str()) => Language::Text,
241 _ => Language::Unknown,
242 }
243 }
244
245 /// Parse a language from a human-friendly name (CLI/API input)
246 ///
247 /// Accepts lowercase names and common aliases.
248 /// Returns None for unrecognized names.
249 pub fn from_name(name: &str) -> Option<Self> {
250 match name.to_lowercase().as_str() {
251 "rust" | "rs" => Some(Language::Rust),
252 "python" | "py" => Some(Language::Python),
253 "javascript" | "js" => Some(Language::JavaScript),
254 "typescript" | "ts" => Some(Language::TypeScript),
255 "vue" => Some(Language::Vue),
256 "svelte" => Some(Language::Svelte),
257 "go" => Some(Language::Go),
258 "java" => Some(Language::Java),
259 "php" => Some(Language::PHP),
260 "c" => Some(Language::C),
261 "cpp" | "c++" => Some(Language::Cpp),
262 "csharp" | "cs" | "c#" => Some(Language::CSharp),
263 "ruby" | "rb" => Some(Language::Ruby),
264 "kotlin" | "kt" => Some(Language::Kotlin),
265 "zig" => Some(Language::Zig),
266 "text" | "txt" | "plaintext" | "plain" => Some(Language::Text),
267 "lock" | "lockfile" | "lockfiles" => Some(Language::Lock),
268 "generated" | "gen" => Some(Language::Generated),
269 _ => None,
270 }
271 }
272
273 /// Human-readable list of all supported language names (for error messages)
274 pub fn supported_names_help() -> &'static str {
275 "rust (rs), python (py), javascript (js), typescript (ts), vue, svelte, \
276 go, java, php, c, cpp (c++), csharp (cs, c#), ruby (rb), kotlin (kt), zig, \
277 text (every other non-binary file: docs, config, templates, extensionless), \
278 lock (lock files, excluded by default), generated (*.pb.go, *.min.js, *.map, \
279 *_generated.*, excluded by default)"
280 }
281
282 /// Check if this language has a parser implementation
283 ///
284 /// Returns true only for languages with working Tree-sitter parsers.
285 /// This determines which files will be indexed by Reflex.
286 pub fn is_supported(&self) -> bool {
287 match self {
288 Language::Rust => true,
289 Language::TypeScript => true,
290 Language::JavaScript => true,
291 Language::Vue => true,
292 Language::Svelte => true,
293 Language::Python => true,
294 Language::Go => true,
295 Language::Java => true,
296 Language::PHP => true,
297 Language::C => true,
298 Language::Cpp => true,
299 Language::CSharp => true,
300 Language::Ruby => true,
301 Language::Kotlin => true,
302 Language::Swift => false, // Temporarily disabled - parser queries out of date with tree-sitter-swift 0.7.x grammar
303 Language::Zig => true,
304 // No tree-sitter grammar, by design.
305 Language::Text => false,
306 Language::Lock => false,
307 Language::Generated => false,
308 Language::Unknown => false,
309 }
310 }
311
312 /// Whether this is the plain-text tier.
313 pub fn is_text(&self) -> bool {
314 matches!(self, Language::Text)
315 }
316
317 /// Whether files of this language are indexed but left out of every search
318 /// unless asked for (`include_locks` / `include_generated`, or `lang`).
319 pub fn is_excluded_by_default(&self) -> bool {
320 matches!(self, Language::Lock | Language::Generated)
321 }
322
323 /// Whether this is a code language, supported or not (Swift is code without a
324 /// working grammar). Code files are assumed to be text; every other file is
325 /// sniffed for a NUL byte before it is indexed.
326 pub fn is_code(&self) -> bool {
327 !matches!(
328 self,
329 Language::Text | Language::Lock | Language::Generated | Language::Unknown
330 )
331 }
332
333 /// Whether files of this language can be in the index at all.
334 ///
335 /// Distinct from [`Self::is_supported`], which means "has a tree-sitter parser".
336 /// The text tier is indexed but never parsed, so symbol search, AST queries and
337 /// dependency analysis skip it while full-text search covers it. Whether a given
338 /// path IS indexed also depends on `[index] mode` (`PathPolicy::classify`).
339 pub fn is_indexable(&self) -> bool {
340 !matches!(self, Language::Unknown)
341 }
342}
343
344/// Which files the indexer takes, beyond the code languages.
345#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
346#[serde(rename_all = "lowercase")]
347pub enum IndexMode {
348 /// Every file not ignored by `.gitignore` / `.ignore` / `[index] exclude`,
349 /// unless a NUL byte anywhere in it says it is binary. Lock and generated
350 /// files are indexed and excluded from searches by default. This is ripgrep's
351 /// rule, and what an agent that greps expects.
352 #[default]
353 Tracked,
354 /// The pre-2.0.0 rule: code by extension plus the fixed docs/config extension
355 /// list (`is_text_tier_file`). Lock and generated files are not indexed. For
356 /// trees where the long tail of data files is not worth the index size.
357 Allowlist,
358}
359
360impl IndexMode {
361 pub fn from_name(name: &str) -> Option<Self> {
362 match name.trim().to_ascii_lowercase().as_str() {
363 "tracked" => Some(Self::Tracked),
364 "allowlist" => Some(Self::Allowlist),
365 _ => None,
366 }
367 }
368}
369
370/// Type of import/dependency
371#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
372#[serde(rename_all = "lowercase")]
373pub enum ImportType {
374 /// Internal project file
375 Internal,
376 /// External library/package
377 External,
378 /// Standard library
379 Stdlib,
380 /// Rust `mod foo;` declaration (parent→child ownership, not a usage edge)
381 #[serde(rename = "mod_decl")]
382 ModDecl,
383}
384
385/// Dependency information for API output (simplified, path-based)
386#[derive(Debug, Clone, Serialize, Deserialize)]
387pub struct DependencyInfo {
388 /// Import path as written in source (or resolved path for internal deps)
389 pub path: String,
390 /// Line number where import appears (optional)
391 #[serde(skip_serializing_if = "Option::is_none")]
392 pub line: Option<usize>,
393 /// Imported symbols (for selective imports like `from x import a, b`)
394 #[serde(skip_serializing_if = "Option::is_none")]
395 pub symbols: Option<Vec<String>>,
396}
397
398/// Full dependency record (internal representation with file IDs)
399#[derive(Debug, Clone)]
400pub struct Dependency {
401 /// Source file ID
402 pub file_id: i64,
403 /// Import path as written in source code
404 pub imported_path: String,
405 /// Resolved file ID (None if external or stdlib)
406 pub resolved_file_id: Option<i64>,
407 /// Import type classification
408 pub import_type: ImportType,
409 /// Line number where import appears
410 pub line_number: usize,
411 /// Imported symbols (for selective imports)
412 pub imported_symbols: Option<Vec<String>>,
413}
414
415/// A lightweight, stable reference to a code symbol for API responses
416///
417/// Prefer this over `(String, SymbolKind, Span)` tuples — tuples serialize as
418/// positional JSON arrays, making any field addition a breaking change.
419/// Named fields here are additive-safe: new optional fields can be added without
420/// shifting positions or bumping the version.
421#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
422pub struct SymbolRef {
423 /// Symbol name (e.g., function name, class name)
424 pub name: String,
425 /// Symbol kind (function, class, struct, etc.)
426 pub kind: SymbolKind,
427 /// Location span in source file
428 pub span: Span,
429}
430
431/// Helper function to skip serializing "Unknown" symbol kinds
432fn is_unknown_kind(kind: &SymbolKind) -> bool {
433 matches!(kind, SymbolKind::Unknown(_))
434}
435
436/// A search result representing a symbol or code location
437#[derive(Debug, Clone, Serialize, Deserialize)]
438pub struct SearchResult {
439 /// Absolute or relative path to the file
440 pub path: String,
441 /// Detected programming language (internal use only, not serialized to save tokens)
442 #[serde(skip)]
443 pub lang: Language,
444 /// Type of symbol found (only included for symbol searches, not text matches)
445 #[serde(skip_serializing_if = "is_unknown_kind")]
446 pub kind: SymbolKind,
447 /// Symbol name (e.g., function name, class name)
448 /// None for text/regex matches where symbol name cannot be accurately determined
449 #[serde(skip_serializing_if = "Option::is_none")]
450 pub symbol: Option<String>,
451 /// Location span in the source file
452 pub span: Span,
453 /// Code preview (few lines around the match)
454 pub preview: String,
455 /// File dependencies (only populated when --dependencies flag is used)
456 /// DEPRECATED: Use FileGroupedResult.dependencies instead for file-level grouping
457 #[serde(skip_serializing_if = "Option::is_none")]
458 pub dependencies: Option<Vec<DependencyInfo>>,
459}
460
461/// An individual match within a file (no path or dependencies)
462#[derive(Debug, Clone, Serialize, Deserialize)]
463pub struct MatchResult {
464 /// Type of symbol found (only included for symbol searches, not text matches)
465 #[serde(skip_serializing_if = "is_unknown_kind")]
466 pub kind: SymbolKind,
467 /// Symbol name (e.g., function name, class name)
468 #[serde(skip_serializing_if = "Option::is_none")]
469 pub symbol: Option<String>,
470 /// Location span in the source file
471 pub span: Span,
472 /// Code preview (few lines around the match)
473 pub preview: String,
474 /// Lines of code before the match (for context)
475 #[serde(skip_serializing_if = "Vec::is_empty")]
476 pub context_before: Vec<String>,
477 /// Lines of code after the match (for context)
478 #[serde(skip_serializing_if = "Vec::is_empty")]
479 pub context_after: Vec<String>,
480}
481
482/// File-level grouped results with dependencies at file level
483#[derive(Debug, Clone, Serialize, Deserialize)]
484pub struct FileGroupedResult {
485 /// Absolute or relative path to the file
486 pub path: String,
487 /// Detected programming language of this file (e.g. "rust", "python", "unknown")
488 pub language: Language,
489 /// File dependencies (only populated when --dependencies flag is used)
490 #[serde(skip_serializing_if = "Option::is_none")]
491 pub dependencies: Option<Vec<DependencyInfo>>,
492 /// Individual matches within this file
493 pub matches: Vec<MatchResult>,
494}
495
496impl SearchResult {
497 pub fn new(
498 path: String,
499 lang: Language,
500 kind: SymbolKind,
501 symbol: Option<String>,
502 span: Span,
503 scope: Option<String>,
504 preview: String,
505 ) -> Self {
506 // Ignore scope parameter for backwards compatibility
507 let _ = scope;
508 Self {
509 path,
510 lang,
511 kind,
512 symbol,
513 span,
514 preview,
515 dependencies: None,
516 }
517 }
518}
519
520/// Configuration for indexing behavior
521/// [`IndexConfig::lock_wait_secs`]: wait for `index.lock` until it is free. The
522/// lock is advisory and released when its holder exits, so a crash cannot leave
523/// a waiter blocked.
524pub const LOCK_WAIT_FOREVER: u64 = u64::MAX;
525
526#[derive(Debug, Clone, Serialize, Deserialize)]
527pub struct IndexConfig {
528 /// Languages to include (empty = all supported)
529 pub languages: Vec<Language>,
530 /// Glob patterns to include
531 pub include_patterns: Vec<String>,
532 /// Glob patterns to exclude
533 pub exclude_patterns: Vec<String>,
534 /// Follow symbolic links
535 pub follow_symlinks: bool,
536 /// Maximum file size to index (bytes)
537 pub max_file_size: usize,
538 /// Number of threads for parallel indexing (0 = auto, 80% of available cores)
539 pub parallel_threads: usize,
540 /// Threads for the background symbol pass (`rfx index-symbols-internal`).
541 /// `0` = auto: half the cores, at most 32. See [`resolve_symbol_thread_count`].
542 #[serde(default)]
543 pub symbol_threads: usize,
544 /// Query timeout in seconds (0 = no timeout)
545 pub query_timeout_secs: u64,
546 /// Maximum entries per trigram posting list (0 = unlimited).
547 /// High-frequency trigrams are truncated at this threshold to bound query latency.
548 pub max_posting_list_entries: usize,
549 /// How long `Indexer::index` waits for `.reflex/index.lock` when another
550 /// indexer holds it (seconds). 0 = fail immediately with `IndexLocked`;
551 /// [`LOCK_WAIT_FOREVER`] = wait until it is free. The `rfx index` CLI waits
552 /// 30 s; automatic updates wait forever (a query never answers from a stale
553 /// index because another run was busy).
554 #[serde(default, skip_serializing_if = "is_zero_u64")]
555 pub lock_wait_secs: u64,
556 /// Index documentation, config and template files alongside code.
557 ///
558 /// On by default. Covers md, mdx, txt, yaml, yml, toml, json, proto, html, htm,
559 /// sh, bash, ini, cfg, sql and graphql, trigram-indexed only — no symbols, no
560 /// AST, no dependency analysis. Lock files are always excluded.
561 ///
562 /// Set `[index] text_tier = false` for a repo with large generated JSON or
563 /// vendored documentation where the index growth is not worth it.
564 ///
565 /// `#[serde(default = ...)]` so a config file written before 1.7.2 still parses
566 /// and gets the new default.
567 #[serde(default = "default_true")]
568 pub text_tier: bool,
569 /// Which non-code files the text tier takes: every non-binary, non-hidden, non-ignored file
570 /// (`tracked`, the default since 2.0.0) or the fixed extension list
571 /// (`allowlist`, the pre-2.0.0 rule).
572 #[serde(default)]
573 pub mode: IndexMode,
574 /// Walk dot-directories and dotfiles too (`.githooks/pre-commit`, `.env.example`).
575 /// Off by default, like ripgrep without `--hidden`. `.git/` and `.reflex/` are
576 /// never walked.
577 #[serde(default)]
578 pub hidden: bool,
579}
580
581/// Serde default for boolean options that are on unless explicitly disabled.
582fn default_true() -> bool {
583 true
584}
585
586impl Default for IndexConfig {
587 fn default() -> Self {
588 Self {
589 languages: vec![],
590 include_patterns: vec![],
591 exclude_patterns: vec![],
592 follow_symlinks: false,
593 max_file_size: 10 * 1024 * 1024, // 10 MB
594 parallel_threads: 0, // 0 = auto (80% of available cores)
595 symbol_threads: 0, // 0 = auto (50% of available cores)
596 query_timeout_secs: 30, // 30 seconds default timeout
597 max_posting_list_entries: 500_000, // cap at 500k to bound query latency
598 text_tier: true, // docs and config are searchable by default
599 mode: IndexMode::Tracked, // ripgrep defaults: not ignored, not hidden, not binary
600 hidden: false, // dot-directories skipped, like ripgrep
601 lock_wait_secs: 0, // fail fast when another indexer runs
602 }
603 }
604}
605
606fn is_zero(v: &usize) -> bool {
607 *v == 0
608}
609fn is_zero_u64(v: &u64) -> bool {
610 *v == 0
611}
612
613/// Resolve `[performance] symbol_threads` (the background symbol pass) to a
614/// concrete thread count.
615///
616/// `REFLEX_SYMBOL_THREADS` overrides for benchmarking, then `configured` if
617/// non-zero, else half the cores (1..=32). The pass is detached from `rfx index`
618/// and runs while the user may be querying, so it takes half the machine rather
619/// than the indexer's 80%; before 2.0.0 it took 27.5%.
620pub fn resolve_symbol_thread_count(configured: usize) -> usize {
621 if let Some(n) = std::env::var("REFLEX_SYMBOL_THREADS")
622 .ok()
623 .and_then(|v| v.parse::<usize>().ok())
624 .filter(|&n| n > 0)
625 {
626 return n;
627 }
628 if configured != 0 {
629 return configured.max(1);
630 }
631 let available = std::thread::available_parallelism()
632 .map(|n| n.get())
633 .unwrap_or(4);
634 ((available as f64 * 0.5).ceil() as usize).clamp(1, 32)
635}
636
637/// Resolve `[performance] parallel_threads` to a concrete thread count.
638///
639/// `0` means automatic: 80% of the available cores, at least 1, at most
640/// `auto_cap`. A non-zero value is used as given. The indexer passes a cap of 8
641/// (write-side cache contention); query-time verification passes a higher cap.
642pub fn resolve_thread_count(configured: usize, auto_cap: usize) -> usize {
643 if configured != 0 {
644 return configured.max(1);
645 }
646 let available = std::thread::available_parallelism()
647 .map(|n| n.get())
648 .unwrap_or(4);
649 ((available as f64 * 0.8).ceil() as usize).clamp(1, auto_cap.max(1))
650}
651
652/// How a query found its candidate lines.
653///
654/// `trigram` is the normal case: the pattern's literals were looked up in the
655/// inverted index and only the lines they name were verified. `scan` means every
656/// line of every file was verified, which happens for a pattern shorter than
657/// 3 chars, a regex with no literal of 3+ chars (`\w+_id`), a non-ASCII literal
658/// under `(?i)`, or a keyword symbol query. Before 2.0.0 every `(?i)` regex
659/// scanned; since 2.0.0 its literals are looked up under all case variants.
660#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq, Eq)]
661#[serde(rename_all = "lowercase")]
662pub enum IndexPath {
663 /// Candidates came from the trigram index.
664 #[default]
665 Trigram,
666 /// Every line was verified.
667 Scan,
668}
669
670/// Per-phase wall-clock timings for one query, in microseconds.
671///
672/// Present in a [`QueryResponse`] only when the caller asked for it
673/// (`rfx query --timing`, or `REFLEX_MCP_TIMING=1` for the MCP server).
674#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
675pub struct QueryTimings {
676 /// Whether the trigram index or a full scan produced the candidates.
677 #[serde(default)]
678 pub index_path: IndexPath,
679 /// Opening (or reusing) the index handle.
680 pub open_us: u64,
681 /// Trigram lookup and posting-list intersection.
682 pub candidates_us: u64,
683 /// Verifying candidate lines against the pattern (and any enrichment).
684 pub verify_us: u64,
685 /// Time the query waited for the freshness check after the search finished.
686 /// The check runs on its own thread alongside the search, so this is usually
687 /// near zero; `status_compute_us` is what the check itself cost.
688 pub status_us: u64,
689 /// The freshness check's own duration (git spawns, or a tree walk outside git).
690 #[serde(default)]
691 pub status_compute_us: u64,
692 /// Grouping by file, context lines, dependencies.
693 pub group_us: u64,
694 /// Whole query as seen by the engine.
695 pub total_us: u64,
696 /// Automatic updates of a stale index before the answer (0: none ran).
697 /// `total_us` includes them.
698 #[serde(default, skip_serializing_if = "is_zero_u64")]
699 pub update_us: u64,
700}
701
702/// Statistics about the index
703#[derive(Debug, Clone, Serialize, Deserialize, Default)]
704pub struct IndexStats {
705 /// Total files indexed
706 pub total_files: usize,
707 /// Index size on disk (bytes)
708 pub index_size_bytes: u64,
709 /// Last update timestamp
710 pub last_updated: String,
711 /// File count breakdown by language
712 pub files_by_language: std::collections::HashMap<String, usize>,
713 /// Line count breakdown by language
714 pub lines_by_language: std::collections::HashMap<String, usize>,
715 /// New files added since last index run (0 if not an incremental run)
716 #[serde(default, skip_serializing_if = "is_zero")]
717 pub new_files: usize,
718 /// Modified files re-indexed since last run (0 if not an incremental run)
719 #[serde(default, skip_serializing_if = "is_zero")]
720 pub modified_files: usize,
721 /// Unchanged files (same hash as last run, still re-indexed due to other changes)
722 #[serde(default, skip_serializing_if = "is_zero")]
723 pub unchanged_files: usize,
724 /// Files dropped from the index because they no longer exist on disk
725 #[serde(default, skip_serializing_if = "is_zero")]
726 pub deleted_files: usize,
727 /// Files skipped because they exceeded max_file_size
728 #[serde(default, skip_serializing_if = "is_zero")]
729 pub skipped_too_large: usize,
730 /// Total bytes of files skipped due to max_file_size
731 #[serde(default, skip_serializing_if = "is_zero_u64")]
732 pub skipped_bytes_too_large: u64,
733 /// Files skipped because a NUL byte says they are binary (ripgrep's rule)
734 #[serde(default, skip_serializing_if = "is_zero")]
735 pub skipped_binary: usize,
736 /// Raw bytes of indexed source held in content.bin (0 if unknown)
737 #[serde(default, skip_serializing_if = "is_zero_u64")]
738 pub corpus_bytes: u64,
739 /// Size of trigrams.bin on disk (0 if absent)
740 #[serde(default, skip_serializing_if = "is_zero_u64")]
741 pub trigram_index_bytes: u64,
742}
743
744/// Information about an indexed file
745#[derive(Debug, Clone, Serialize, Deserialize)]
746pub struct IndexedFile {
747 /// File path
748 pub path: String,
749 /// Detected language
750 pub language: String,
751 /// Last indexed timestamp
752 pub last_indexed: String,
753}
754
755/// Index status for query responses
756#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
757#[serde(rename_all = "snake_case")]
758pub enum IndexStatus {
759 /// Index is fresh and up-to-date
760 Fresh,
761 /// Index is stale (any issue: branch not indexed, commit changed, files modified)
762 Stale,
763}
764
765/// Warning details when index is stale
766#[derive(Debug, Clone, Serialize, Deserialize)]
767pub struct IndexWarning {
768 /// Human-readable reason why index is stale
769 pub reason: String,
770 /// Command to run to fix the issue
771 pub action_required: String,
772 /// Tracked files edited since the index was built.
773 ///
774 /// BREAKING in 1.7.2: this was a `u32` count. It is now the paths themselves,
775 /// because a count told an agent something was wrong without telling it what, so
776 /// the only safe reaction was to distrust the whole result. The list is capped;
777 /// `truncated` says when.
778 #[serde(skip_serializing_if = "Option::is_none")]
779 pub files_modified: Option<Vec<String>>,
780 /// Files present on disk but absent from the index (new or untracked).
781 #[serde(skip_serializing_if = "Option::is_none")]
782 pub files_added: Option<Vec<String>>,
783 /// Files in the index but no longer on disk. These produce ghost hits.
784 #[serde(skip_serializing_if = "Option::is_none")]
785 pub files_deleted: Option<Vec<String>>,
786 /// Total changed paths, which may exceed the lengths of the lists above.
787 #[serde(skip_serializing_if = "Option::is_none")]
788 pub changed_count: Option<usize>,
789 /// Whether the lists were cut short. Set on a fresh checkout or a huge rebase.
790 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
791 pub truncated: bool,
792 /// Additional context (git branch info, etc.)
793 #[serde(skip_serializing_if = "Option::is_none")]
794 pub details: Option<IndexWarningDetails>,
795}
796
797impl IndexWarning {
798 /// A warning with only a reason and an action, no file lists.
799 pub fn new(reason: impl Into<String>, action_required: impl Into<String>) -> Self {
800 Self {
801 reason: reason.into(),
802 action_required: action_required.into(),
803 files_modified: None,
804 files_added: None,
805 files_deleted: None,
806 changed_count: None,
807 truncated: false,
808 details: None,
809 }
810 }
811
812 /// Attach git branch/commit context.
813 pub fn with_details(mut self, details: IndexWarningDetails) -> Self {
814 self.details = Some(details);
815 self
816 }
817}
818
819/// Detailed information about index staleness
820#[derive(Debug, Clone, Serialize, Deserialize)]
821pub struct IndexWarningDetails {
822 /// Current branch (if in git repo)
823 #[serde(skip_serializing_if = "Option::is_none")]
824 pub current_branch: Option<String>,
825 /// Indexed branch (if in git repo)
826 #[serde(skip_serializing_if = "Option::is_none")]
827 pub indexed_branch: Option<String>,
828 /// Current commit SHA (if in git repo)
829 #[serde(skip_serializing_if = "Option::is_none")]
830 pub current_commit: Option<String>,
831 /// Indexed commit SHA (if in git repo)
832 #[serde(skip_serializing_if = "Option::is_none")]
833 pub indexed_commit: Option<String>,
834 /// When the index was last written (Unix seconds).
835 #[serde(default, skip_serializing_if = "Option::is_none")]
836 pub indexed_at: Option<i64>,
837 /// How the working tree was compared to the index: `"git"` (candidates from
838 /// `git status`, confirmed by fingerprint) or `"walk"` (every file stat'ed).
839 #[serde(default, skip_serializing_if = "Option::is_none")]
840 pub checked_by: Option<String>,
841}
842
843/// The full answer to `check_index_status`.
844///
845/// `details` is present even when the index is fresh, so a human can see that the
846/// indexed commit differs from HEAD without that difference being called staleness:
847/// since 2.0.0 freshness is judged by file content, not by commit.
848#[derive(Debug, Clone)]
849pub struct IndexStatusReport {
850 pub status: IndexStatus,
851 pub can_trust_results: bool,
852 pub warning: Option<IndexWarning>,
853 pub details: Option<IndexWarningDetails>,
854}
855
856/// Pagination information for query results
857#[derive(Debug, Clone, Serialize, Deserialize)]
858pub struct PaginationInfo {
859 /// Total number of results before offset/limit — **`null` whenever
860 /// `total_is_exact` is false**. A list-mode search with a `limit` stops
861 /// verifying once the page is full, and the number verified by then is not a
862 /// total; reporting it as one made agents stop paginating early (2.0.0 field
863 /// test: `total: 851` for a term with 18,752 matches). Use `approx_total` for an
864 /// estimate, or a count-mode / no-limit search for the exact number.
865 #[serde(default)]
866 pub total: Option<usize>,
867 /// Number of results in this response (after offset/limit)
868 pub count: usize,
869 /// Offset used (starting position)
870 pub offset: usize,
871 /// Limit used (max results per page)
872 #[serde(skip_serializing_if = "Option::is_none")]
873 pub limit: Option<usize>,
874 /// Whether there are more results after this page
875 pub has_more: bool,
876 /// `true` when `total` counts every match. `false` when a list-mode search
877 /// stopped verifying once the page was full: `total` is then `null` and
878 /// `approx_total` carries an estimate. Count mode, no-limit searches,
879 /// symbol/AST searches and `require_exact_total` callers are always exact.
880 #[serde(default = "default_true")]
881 pub total_is_exact: bool,
882 /// Estimated total when `total_is_exact` is false. After the page filled, a
883 /// spread sample of the remaining candidate lines was verified and the
884 /// measured hit rate scaled over the rest (see
885 /// `query::ESTIMATE_SAMPLE_FILES`). Typically within ±30% on the synthetic
886 /// corpus; wider when hits cluster in a few files. Absent when no estimate was
887 /// possible (a regex with no literal, where every line is a candidate).
888 #[serde(default, skip_serializing_if = "Option::is_none")]
889 pub approx_total: Option<usize>,
890}
891
892impl PaginationInfo {
893 /// The total, only when it is a real one.
894 pub fn exact_total(&self) -> Option<usize> {
895 if self.total_is_exact {
896 self.total
897 } else {
898 None
899 }
900 }
901
902 /// The best number available for a threshold or a log line: the exact total,
903 /// else the estimate, else the end of this page. Never show it as a total.
904 pub fn best_total(&self) -> usize {
905 self.exact_total()
906 .or(self.approx_total)
907 .unwrap_or(self.offset + self.count)
908 }
909}
910
911/// Query response with results and index status
912#[derive(Debug, Clone, Serialize, Deserialize)]
913pub struct QueryResponse {
914 /// AI-optimized instruction for how to handle these results
915 /// Only present when --ai flag is used or in MCP mode
916 /// Provides guidance to AI agents on response format and next actions
917 #[serde(skip_serializing_if = "Option::is_none")]
918 pub ai_instruction: Option<String>,
919 /// Status of the index (fresh or stale)
920 pub status: IndexStatus,
921 /// Whether the results can be trusted
922 pub can_trust_results: bool,
923 /// Warning information (only present if stale)
924 #[serde(skip_serializing_if = "Option::is_none")]
925 pub warning: Option<IndexWarning>,
926 /// Pagination information
927 pub pagination: PaginationInfo,
928 /// File-grouped search results
929 /// Results are always grouped by file path, with dependencies populated when --dependencies flag is used
930 pub results: Vec<FileGroupedResult>,
931 /// For a whole-identifier search that found nothing: how many candidate lines
932 /// contain the pattern as a substring. Lets the caller explain a zero without
933 /// running a second search. Absent whenever there were results, or when the
934 /// search was not a whole-identifier one.
935 #[serde(default, skip_serializing_if = "Option::is_none")]
936 pub substring_hint_count: Option<usize>,
937 /// Things the engine did to the query that the caller should know about.
938 ///
939 /// Today this is one message at most: a whole-identifier pattern containing
940 /// brackets was escaped and run as a regex (see
941 /// `query::prepare_literal_pattern`). Every surface — CLI, MCP, HTTP — carries it,
942 /// so a rewrite is never silent on any of them.
943 #[serde(default, skip_serializing_if = "Vec::is_empty")]
944 pub warnings: Vec<String>,
945 /// For a whole-identifier search that found nothing while substring matches
946 /// exist: a ready-to-show sentence naming the count and the switch that shows
947 /// them. Absent whenever there were results, or when the search was not a
948 /// whole-identifier one.
949 #[serde(default, skip_serializing_if = "Option::is_none")]
950 pub hint: Option<String>,
951 /// Machine-readable cause behind `hint`: `hidden` (the filter names a
952 /// dot-directory), `not_indexed` (the `file` filter names a path the index does
953 /// not hold), `lock_or_generated`, `whole_identifier`. Absent when no rule
954 /// applies, so a harness can branch without parsing prose.
955 #[serde(default, skip_serializing_if = "Option::is_none")]
956 pub excluded_reason: Option<crate::query::ExcludedReason>,
957 /// For a search that found nothing: how many candidate files were lock or
958 /// generated files, which every search leaves out unless `include_locks` /
959 /// `include_generated` (or `lang`) asks for them. The `hint` says so too.
960 #[serde(default, skip_serializing_if = "Option::is_none")]
961 pub excluded_by_default: Option<usize>,
962 /// Count-only searches: the number of files with at least one match. `results`
963 /// is empty in that mode, so this is the only place the file count lives.
964 #[serde(default, skip_serializing_if = "Option::is_none")]
965 pub file_count: Option<usize>,
966 /// Per-phase timings, only when requested.
967 #[serde(default, skip_serializing_if = "Option::is_none")]
968 pub timings: Option<QueryTimings>,
969}
970
971/// Report from cache compaction operation
972#[derive(Debug, Clone, Serialize, Deserialize)]
973pub struct CompactionReport {
974 /// Always 0 since auto-update: index runs remove deleted files (kept for JSON compatibility)
975 pub files_removed: usize,
976 /// Space saved in bytes
977 pub space_saved_bytes: u64,
978 /// Duration in milliseconds
979 pub duration_ms: u64,
980}
981
982#[cfg(test)]
983mod tests {
984 use super::*;
985
986 #[test]
987 fn test_symbol_ref_json_shape() {
988 let sym = SymbolRef {
989 name: "my_function".to_string(),
990 kind: SymbolKind::Function,
991 span: Span {
992 start_line: 10,
993 end_line: 20,
994 },
995 };
996 let json = serde_json::to_value(&sym).unwrap();
997 assert_eq!(json["name"], "my_function");
998 assert_eq!(json["kind"], "Function");
999 assert_eq!(json["span"]["start_line"], 10);
1000 assert_eq!(json["span"]["end_line"], 20);
1001 assert!(json.as_array().is_none());
1002 }
1003
1004 #[test]
1005 fn test_symbol_ref_roundtrip() {
1006 let original = SymbolRef {
1007 name: "MyStruct".to_string(),
1008 kind: SymbolKind::Struct,
1009 span: Span {
1010 start_line: 1,
1011 end_line: 5,
1012 },
1013 };
1014 let json = serde_json::to_string(&original).unwrap();
1015 let decoded: SymbolRef = serde_json::from_str(&json).unwrap();
1016 assert_eq!(original, decoded);
1017 }
1018
1019 #[test]
1020 fn test_symbol_ref_exact_json() {
1021 let sym = SymbolRef {
1022 name: "Foo".to_string(),
1023 kind: SymbolKind::Class,
1024 span: Span {
1025 start_line: 3,
1026 end_line: 7,
1027 },
1028 };
1029 let json = serde_json::to_string(&sym).unwrap();
1030 assert_eq!(
1031 json,
1032 r#"{"name":"Foo","kind":"Class","span":{"start_line":3,"end_line":7}}"#
1033 );
1034 }
1035}