1pub mod filter;
7pub mod open_index;
8pub mod result;
9pub mod zero_hint;
10
11pub use filter::{LiteralPattern, QueryFilter, prepare_literal_pattern, substring_hint_text};
12pub use zero_hint::{ExcludedReason, ZeroHint};
13
14use anyhow::{Context, Result};
15use regex::Regex;
16
17use crate::cache::CacheManager;
18use crate::models::{
19 IndexPath, IndexStatus, IndexStatusReport, IndexWarning, Language, QueryResponse, SearchResult,
20 Span, SymbolKind,
21};
22use crate::output;
23use crate::parsers::ParserFactory;
24use std::sync::Arc;
25use std::sync::atomic::{AtomicUsize, Ordering};
26
27use open_index::OpenIndex;
28
29pub fn invalidate_caches(workspace_root: &std::path::Path) {
33 open_index::invalidate(&workspace_root.join(crate::cache::CACHE_DIR));
34 status_cache::invalidate(workspace_root);
35}
36
37#[derive(Debug, Clone, PartialEq, Eq)]
40pub enum UpdatePlan {
41 Fresh,
43 Paths(Vec<std::path::PathBuf>),
45 Full(Vec<std::path::PathBuf>),
48 Foreign(String),
50 Unknown(String),
52}
53
54pub fn update_plan(cache: &CacheManager) -> Result<UpdatePlan> {
57 Ok(status_cache::update_plan(cache)?.0)
58}
59
60pub struct PlannedUpdate {
62 pub plan: UpdatePlan,
63 snapshot: Arc<status_cache::Snapshot>,
64}
65
66pub fn plan_update(cache: &CacheManager) -> Result<PlannedUpdate> {
68 let (plan, snapshot) = status_cache::update_plan(cache)?;
69 Ok(PlannedUpdate { plan, snapshot })
70}
71
72pub fn settle_update(cache: &CacheManager, planned: &PlannedUpdate) -> bool {
77 match &planned.plan {
78 UpdatePlan::Paths(paths) => status_cache::settle(cache, &planned.snapshot, paths),
79 _ => false,
80 }
81}
82
83struct Internal {
86 results: Vec<SearchResult>,
87 total_count: usize,
91 total_is_exact: bool,
93 approx_total: Option<usize>,
95 substring_only: Option<usize>,
98 excluded_by_default: usize,
101 counted: Option<(usize, usize)>,
104 candidates_us: u64,
106 index_path: IndexPath,
108 warnings: Vec<String>,
110}
111
112#[derive(Debug, Clone)]
114struct CandidateStats {
115 candidates_us: u64,
116 substring_only: Option<usize>,
117 excluded_by_default: usize,
119 counted: Option<(usize, usize)>,
121 exhausted: bool,
123 approx_total: Option<usize>,
125 index_path: IndexPath,
127 warnings: Vec<String>,
130}
131
132impl Default for CandidateStats {
133 fn default() -> Self {
134 Self {
135 candidates_us: 0,
136 substring_only: None,
137 excluded_by_default: 0,
138 counted: None,
139 exhausted: true,
140 approx_total: None,
141 index_path: IndexPath::Trigram,
142 warnings: Vec::new(),
143 }
144 }
145}
146
147enum LineSet {
149 Only(Vec<u32>),
151 All,
153}
154
155struct FileFilter {
158 language: Option<Language>,
159 include: Option<globset::GlobSet>,
160 exclude: Option<globset::GlobSet>,
161 file_pattern: Option<String>,
162 exclude_text: bool,
163 include_locks: bool,
164 include_generated: bool,
165}
166
167impl FileFilter {
168 fn from_filter(filter: &QueryFilter) -> Self {
169 Self {
170 language: filter.language,
171 include: result::build_glob_set(&filter.glob_patterns, "glob"),
172 exclude: result::build_glob_set(&filter.exclude_patterns, "exclude"),
173 file_pattern: filter.file_pattern.clone(),
174 exclude_text: filter.exclude_text,
175 include_locks: filter.include_locks,
176 include_generated: filter.include_generated,
177 }
178 }
179
180 fn excluded_by_default(&self, lang: Language) -> bool {
182 filter::excluded_by_default(
183 lang,
184 self.language,
185 self.include_locks,
186 self.include_generated,
187 )
188 }
189
190 fn path_admits(&self, path: &str) -> bool {
192 if let Some(set) = &self.include
193 && !set.is_match(path)
194 {
195 return false;
196 }
197 if let Some(set) = &self.exclude
198 && set.is_match(path)
199 {
200 return false;
201 }
202 if let Some(needle) = &self.file_pattern
203 && !path.contains(needle.as_str())
204 {
205 return false;
206 }
207 true
208 }
209
210 fn accept(&self, path: &str, lang: Language) -> bool {
211 filter::tier_admits(
212 lang,
213 self.language,
214 self.exclude_text,
215 self.include_locks,
216 self.include_generated,
217 ) && self.path_admits(path)
218 }
219}
220
221struct VerifyOutcome {
223 results: Vec<SearchResult>,
225 exhausted: bool,
227 estimated_total: Option<usize>,
231 substring_only: usize,
234 excluded_by_default: usize,
237 counted: Option<(usize, usize)>,
239}
240
241enum LineMatcher {
246 WordBoundary(Regex, String),
249 Contains(String),
252 Regex(Regex, &'static str),
255}
256
257impl LineMatcher {
258 fn new(pattern: &str, filter: &QueryFilter) -> Result<Self> {
259 if filter.use_regex {
260 let label = if filter.ignore_case && filter.rewritten_from.is_some() {
261 "text_match"
262 } else {
263 "regex_match"
264 };
265 return match Regex::new(pattern) {
266 Ok(re) => Ok(Self::Regex(re, label)),
267 Err(e) => {
268 log::error!("Invalid regex pattern '{}': {}", pattern, e);
269 anyhow::bail!("Invalid regex pattern '{}': {}", pattern, e);
270 }
271 };
272 }
273 if filter.use_contains {
274 return Ok(Self::Contains(pattern.to_string()));
275 }
276 match Regex::new(&format!(r"\b{}\b", regex::escape(pattern))) {
277 Ok(re) => Ok(Self::WordBoundary(re, pattern.to_string())),
278 Err(_) => {
279 log::debug!(
280 "Word boundary regex failed for pattern '{}', falling back to substring",
281 pattern
282 );
283 Ok(Self::Contains(pattern.to_string()))
284 }
285 }
286 }
287
288 fn is_word_boundary(&self) -> bool {
289 matches!(self, Self::WordBoundary(..))
290 }
291
292 #[inline]
293 fn is_match(&self, line: &str) -> bool {
294 match self {
295 Self::WordBoundary(re, _) | Self::Regex(re, _) => re.is_match(line),
296 Self::Contains(p) => line.contains(p.as_str()),
297 }
298 }
299
300 #[inline]
302 fn find(&self, line: &str) -> Option<usize> {
303 match self {
304 Self::WordBoundary(re, _) | Self::Regex(re, _) => re.find(line).map(|m| m.start()),
305 Self::Contains(p) => line.find(p.as_str()),
306 }
307 }
308
309 fn literal(&self) -> Option<&str> {
311 match self {
312 Self::WordBoundary(_, p) | Self::Contains(p) => Some(p.as_str()),
313 Self::Regex(..) => None,
314 }
315 }
316
317 fn kind_label(&self) -> &'static str {
319 match self {
320 Self::Regex(_, label) => label,
321 _ => "text_match",
322 }
323 }
324}
325
326pub const ESTIMATE_SAMPLE_FILES: usize = 32;
338pub const ESTIMATE_FINISH_LINES: usize = 128;
341pub const ESTIMATE_PER_FILE_LINES: usize = 16;
344
345fn verify_files_streaming(
346 open: &OpenIndex,
347 files: Vec<(u32, LineSet)>,
348 matcher: &LineMatcher,
349 file_filter: &FileFilter,
350 paths_only: bool,
351 budget: Option<usize>,
352 count_only: bool,
353) -> VerifyOutcome {
354 use rayon::prelude::*;
355
356 let content = &open.snapshot;
357
358 let mut excluded_by_default = 0usize;
361 let mut accepted: Vec<(u32, String, Language, LineSet)> = files
362 .into_iter()
363 .filter_map(|(file_id, lines)| {
364 let path = content.get_file_path(file_id)?;
365 let lang = Language::from_path(path);
366 let path_str = path.to_string_lossy().into_owned();
367 if file_filter.accept(&path_str, lang) {
368 Some((file_id, path_str, lang, lines))
369 } else {
370 if file_filter.path_admits(&path_str) && file_filter.excluded_by_default(lang) {
374 excluded_by_default += 1;
375 }
376 None
377 }
378 })
379 .collect();
380 accepted.sort_by(|a, b| a.1.cmp(&b.1));
381
382 let count_substring_only = matcher.is_word_boundary();
383 let substring_only = AtomicUsize::new(0);
384 let kind_label = matcher.kind_label();
385
386 let verify_one = |file_id: u32, path: &str, lang: Language, lines: &LineSet| {
387 let Ok(text) = content.get_file_content(file_id) else {
388 return Vec::new();
389 };
390 let all_lines: Vec<&str> = text.lines().collect();
391 let mut out = Vec::new();
392
393 let mut check = |line_no: usize, line: &str| -> bool {
394 let Some(offset) = matcher.find(line) else {
395 if count_substring_only
396 && let Some(p) = matcher.literal()
397 && line.contains(p)
398 {
399 substring_only.fetch_add(1, Ordering::Relaxed);
400 }
401 return false;
402 };
403 out.push(SearchResult {
404 path: path.to_string(),
405 lang,
406 kind: SymbolKind::Unknown(kind_label.to_string()),
407 symbol: None,
408 span: Span {
409 start_line: line_no,
410 end_line: line_no,
411 },
412 preview: crate::parsers::preview::line_preview(line, offset),
416 dependencies: None,
417 });
418 true
419 };
420
421 match lines {
422 LineSet::Only(nos) => {
423 let mut last = 0u32;
424 for &line_no in nos {
425 if line_no == last {
426 continue;
427 }
428 last = line_no;
429 let idx = line_no as usize;
430 if idx == 0 || idx > all_lines.len() {
431 log::debug!(
432 "Line {} out of bounds (file has {} lines)",
433 line_no,
434 all_lines.len()
435 );
436 continue;
437 }
438 if check(idx, all_lines[idx - 1]) && paths_only {
439 break;
440 }
441 }
442 }
443 LineSet::All => {
444 for (i, line) in all_lines.iter().enumerate() {
445 if check(i + 1, line) && paths_only {
446 break;
447 }
448 }
449 }
450 }
451 out
452 };
453
454 if count_only && budget.is_none() {
459 let count_one = |file_id: u32, lines: &LineSet| -> usize {
460 let Ok(text) = content.get_file_content(file_id) else {
461 return 0;
462 };
463 let mut hits = 0usize;
464 let mut check = |line: &str| {
465 if matcher.find(line).is_some() {
466 hits += 1;
467 true
468 } else {
469 if count_substring_only
470 && let Some(p) = matcher.literal()
471 && line.contains(p)
472 {
473 substring_only.fetch_add(1, Ordering::Relaxed);
474 }
475 false
476 }
477 };
478 match lines {
479 LineSet::Only(nos) => {
480 let all_lines: Vec<&str> = text.lines().collect();
481 let mut last = 0u32;
482 for &line_no in nos {
483 if line_no == last {
484 continue;
485 }
486 last = line_no;
487 let idx = line_no as usize;
488 if idx == 0 || idx > all_lines.len() {
489 continue;
490 }
491 if check(all_lines[idx - 1]) && paths_only {
492 break;
493 }
494 }
495 }
496 LineSet::All => {
497 for line in text.lines() {
498 if check(line) && paths_only {
499 break;
500 }
501 }
502 }
503 }
504 hits
505 };
506 let (lines, files_hit) = open.pool().install(|| {
507 accepted
508 .par_iter()
509 .map(|(file_id, _, _, lines)| {
510 let n = count_one(*file_id, lines);
511 (n, usize::from(n > 0))
512 })
513 .reduce(|| (0, 0), |a, b| (a.0 + b.0, a.1 + b.1))
514 });
515 return VerifyOutcome {
516 results: Vec::new(),
517 exhausted: true,
518 estimated_total: None,
519 substring_only: substring_only.into_inner(),
520 excluded_by_default,
521 counted: Some((if paths_only { files_hit } else { lines }, files_hit)),
522 };
523 }
524
525 let mut results: Vec<SearchResult> = Vec::new();
526 let mut next = 0usize;
527 let mut chunk = if budget.is_some() {
531 16usize
532 } else {
533 accepted.len().max(1)
534 };
535 let mut exhausted = true;
536
537 while next < accepted.len() {
538 if let Some(b) = budget
539 && results.len() >= b
540 {
541 exhausted = false;
542 break;
543 }
544 let end = (next + chunk).min(accepted.len());
545 let slice = &accepted[next..end];
546 let round: Vec<Vec<SearchResult>> = open.pool().install(|| {
547 slice
548 .par_iter()
549 .map(|(file_id, path, lang, lines)| verify_one(*file_id, path, *lang, lines))
550 .collect()
551 });
552 for r in round {
553 results.extend(r);
554 }
555 next = end;
556 chunk = (chunk * 2).min(1024);
557 }
558
559 let mut estimated_total = None;
563 if !exhausted {
564 let remaining = &accepted[next..];
565 let unit = |lines: &LineSet| -> Option<usize> {
568 match lines {
569 _ if paths_only => Some(1),
570 LineSet::Only(v) => Some(v.len()),
571 LineSet::All => None,
572 }
573 };
574 let remaining_units = remaining
575 .iter()
576 .try_fold(0usize, |acc, (_, _, _, l)| unit(l).map(|u| acc + u));
577
578 let finish = remaining.len() <= ESTIMATE_SAMPLE_FILES
579 || remaining_units.is_some_and(|u| u <= ESTIMATE_FINISH_LINES);
580 if finish {
581 let round: Vec<Vec<SearchResult>> = open.pool().install(|| {
582 remaining
583 .par_iter()
584 .map(|(file_id, path, lang, lines)| verify_one(*file_id, path, *lang, lines))
585 .collect()
586 });
587 for r in round {
588 results.extend(r);
589 }
590 exhausted = true;
591 } else if let Some(units) = remaining_units {
592 let stride = remaining.len() / ESTIMATE_SAMPLE_FILES;
593 let picks: Vec<&(u32, String, Language, LineSet)> = (0..ESTIMATE_SAMPLE_FILES)
594 .map(|i| &remaining[i * stride])
595 .collect();
596 let (hits, sampled_units) = open.pool().install(|| {
597 picks
598 .par_iter()
599 .map(|(file_id, path, lang, lines)| {
600 let trimmed = match lines {
601 LineSet::Only(v) if !paths_only => {
602 LineSet::Only(v[..v.len().min(ESTIMATE_PER_FILE_LINES)].to_vec())
603 }
604 LineSet::Only(v) => LineSet::Only(v.clone()),
605 LineSet::All => LineSet::All,
606 };
607 let u = unit(&trimmed).unwrap_or(0);
608 (verify_one(*file_id, path, *lang, &trimmed).len(), u)
611 })
612 .reduce(|| (0usize, 0usize), |a, b| (a.0 + b.0, a.1 + b.1))
613 });
614 let rate = hits as f64 / sampled_units.max(1) as f64;
615 estimated_total = Some(results.len() + (rate * units as f64).round() as usize);
616 }
617 }
618
619 VerifyOutcome {
620 results,
621 exhausted,
622 estimated_total,
623 substring_only: substring_only.into_inner(),
624 excluded_by_default,
625 counted: None,
626 }
627}
628
629pub struct QueryEngine {
631 cache: CacheManager,
632 open: std::sync::Mutex<Option<Arc<OpenIndex>>>,
636 update: Option<crate::auto_update::UpdateOptions>,
639}
640
641impl QueryEngine {
642 pub fn new(cache: CacheManager) -> Self {
644 Self {
645 cache,
646 open: std::sync::Mutex::new(None),
647 update: None,
648 }
649 }
650
651 pub fn with_update(mut self, opts: crate::auto_update::UpdateOptions) -> Self {
656 self.update = Some(opts);
657 self
658 }
659
660 fn run_update(&self) -> Result<Option<crate::auto_update::Updated>> {
663 let Some(opts) = self.update else {
664 return Ok(None);
665 };
666 self.reset_open();
667 let updated = crate::auto_update::update_if_stale(&self.cache, &opts)?;
668 if let crate::auto_update::Updated::Skipped(reason) = &updated {
669 log::warn!("Index not updated: {reason}");
670 }
671 Ok(Some(updated))
672 }
673
674 fn update_first(&self, filter: &QueryFilter) -> Result<()> {
677 match self.run_update()? {
678 Some(crate::auto_update::Updated::Skipped(reason)) if !filter.suppress_output => {
679 output::warn(&format!("⚠️ Index not updated: {reason}"));
680 Ok(())
681 }
682 Some(_) => Ok(()),
683 None => {
684 if !self.cache.exists() {
685 return Err(crate::errors::ReflexError::IndexNotFound.into());
686 }
687 self.check_index_freshness(filter)
688 }
689 }
690 }
691
692 fn open_index(&self) -> Result<Arc<OpenIndex>> {
697 let mut slot = self.open.lock().unwrap_or_else(|e| e.into_inner());
698 if let Some(open) = slot.as_ref() {
699 return Ok(Arc::clone(open));
700 }
701 let open = open_index::get_or_open(&self.cache)?;
702 *slot = Some(Arc::clone(&open));
703 Ok(open)
704 }
705
706 fn reset_open(&self) {
711 *self.open.lock().unwrap_or_else(|e| e.into_inner()) = None;
712 }
713
714 fn load_dependencies(&self, results: &mut [SearchResult], include_deps: bool) -> Result<()> {
717 if !include_deps || results.is_empty() {
718 return Ok(());
719 }
720
721 log::debug!("Loading dependencies for {} results", results.len());
722
723 let workspace_root = self
727 .cache
728 .path()
729 .parent()
730 .ok_or_else(|| anyhow::anyhow!("Cache path has no parent"))?;
731 let cache_for_deps = CacheManager::new(workspace_root);
732 let dep_index = crate::dependency::DependencyIndex::new(cache_for_deps);
733
734 for result in results {
736 let normalized_path = result.path.strip_prefix("./").unwrap_or(&result.path);
738
739 match self.cache.get_file_id(normalized_path) {
741 Ok(Some(file_id)) => {
742 log::debug!("Found file_id={} for path={}", file_id, result.path);
743 match dep_index.get_dependencies_info(file_id) {
745 Ok(dep_infos) => {
746 log::debug!(
747 "Loaded {} dependencies for file_id={}",
748 dep_infos.len(),
749 file_id
750 );
751 if !dep_infos.is_empty() {
752 result.dependencies = Some(dep_infos);
753 }
754 }
755 Err(e) => {
756 log::warn!("Failed to get dependencies for file_id={}: {}", file_id, e);
757 }
758 }
759 }
760 Ok(None) => {
761 log::warn!("No file_id found for path: {}", result.path);
762 }
763 Err(e) => {
764 log::warn!("Failed to get file_id for path {}: {}", result.path, e);
765 }
766 }
767 }
768
769 Ok(())
770 }
771
772 fn group_and_load_dependencies(
775 &self,
776 results: Vec<SearchResult>,
777 include_deps: bool,
778 context_lines: usize,
779 ) -> Result<Vec<crate::models::FileGroupedResult>> {
780 use crate::models::{FileGroupedResult, MatchResult};
781 use std::collections::HashMap;
782
783 if results.is_empty() {
784 return Ok(Vec::new());
785 }
786
787 let mut grouped: HashMap<String, Vec<SearchResult>> = HashMap::new();
789 for result in results {
790 grouped.entry(result.path.clone()).or_default().push(result);
791 }
792
793 let dep_index = if include_deps {
795 let workspace_root = self
796 .cache
797 .path()
798 .parent()
799 .ok_or_else(|| anyhow::anyhow!("Cache path has no parent"))?;
800 let cache_for_deps = CacheManager::new(workspace_root);
801 Some(crate::dependency::DependencyIndex::new(cache_for_deps))
802 } else {
803 None
804 };
805
806 let open_opt = self.open_index().ok();
808 let content_reader_opt = open_opt.as_deref().map(|o| &o.snapshot);
809
810 let mut file_results: Vec<FileGroupedResult> = grouped
812 .into_iter()
813 .map(|(path, file_matches)| {
814 let language = file_matches.first().map(|r| r.lang).unwrap_or_default();
816
817 let dependencies = if let Some(dep_idx) = &dep_index {
819 let normalized_path = path.strip_prefix("./").unwrap_or(&path);
820 match self.cache.get_file_id(normalized_path) {
821 Ok(Some(file_id)) => match dep_idx.get_dependencies_info(file_id) {
822 Ok(dep_infos) if !dep_infos.is_empty() => {
823 log::debug!(
824 "Loaded {} dependencies for file: {}",
825 dep_infos.len(),
826 path
827 );
828 Some(dep_infos)
829 }
830 Ok(_) => None,
831 Err(e) => {
832 log::warn!("Failed to get dependencies for {}: {}", path, e);
833 None
834 }
835 },
836 Ok(None) => {
837 log::warn!("No file_id found for path: {}", path);
838 None
839 }
840 Err(e) => {
841 log::warn!("Failed to get file_id for path {}: {}", path, e);
842 None
843 }
844 }
845 } else {
846 None
847 };
848
849 let file_id_for_context = open_opt.as_deref().and_then(|o| o.file_id_for(&path));
851 log::debug!(
852 "Context extraction: file={}, file_id={:?}, content_reader={}",
853 path,
854 file_id_for_context,
855 content_reader_opt.is_some()
856 );
857
858 let matches: Vec<MatchResult> = file_matches
860 .into_iter()
861 .map(|r| {
862 let (context_before, context_after) = if context_lines > 0 {
864 if let (Some(reader), Some(fid)) =
865 (&content_reader_opt, file_id_for_context)
866 {
867 let result = reader
868 .get_context_by_line(fid, r.span.start_line, context_lines)
869 .unwrap_or_else(|e| {
870 log::warn!(
871 "Failed to extract context for {}:{}: {}",
872 path,
873 r.span.start_line,
874 e
875 );
876 (vec![], vec![])
877 });
878 log::debug!(
879 "Extracted context for {}:{} - before: {}, after: {}",
880 path,
881 r.span.start_line,
882 result.0.len(),
883 result.1.len()
884 );
885 result
886 } else {
887 if content_reader_opt.is_none() {
888 log::debug!(
889 "No ContentReader available for context extraction"
890 );
891 }
892 if file_id_for_context.is_none() {
893 log::debug!("No file_id found for {}", path);
894 }
895 (vec![], vec![])
896 }
897 } else {
898 (vec![], vec![])
899 };
900
901 MatchResult {
902 kind: r.kind,
903 symbol: r.symbol,
904 span: r.span,
905 preview: r.preview,
906 context_before,
907 context_after,
908 }
909 })
910 .collect();
911
912 FileGroupedResult {
913 path,
914 language,
915 dependencies,
916 matches,
917 }
918 })
919 .collect();
920
921 file_results.sort_by(|a, b| a.path.cmp(&b.path));
923
924 Ok(file_results)
925 }
926
927 pub fn search_with_metadata(
932 &self,
933 pattern: &str,
934 filter: QueryFilter,
935 ) -> Result<QueryResponse> {
936 if self.update.is_none() {
937 return self.search_with_metadata_once(pattern, filter);
938 }
939 let started = std::time::Instant::now();
940 let mut update_time = std::time::Duration::ZERO;
941 let mut notes = Vec::new();
942 let mut update = |notes: &mut Vec<String>| -> Result<bool> {
943 let t = std::time::Instant::now();
944 let updated = self.run_update()?;
945 update_time += t.elapsed();
946 Ok(match updated {
947 Some(crate::auto_update::Updated::Skipped(reason)) => {
948 notes.push(format!("Index not updated: {reason}"));
949 false
950 }
951 Some(u) => u.wrote(),
952 None => false,
953 })
954 };
955 if !self.cache.exists() {
956 update(&mut notes)?;
957 }
958 let mut response = self.search_with_metadata_once(pattern, filter.clone())?;
962 for _ in 0..2 {
963 if response.status != IndexStatus::Stale || !update(&mut notes)? {
964 break;
965 }
966 response = self.search_with_metadata_once(pattern, filter.clone())?;
967 }
968 response.warnings.extend(notes);
969 if let Some(t) = response.timings.as_mut() {
970 t.update_us = update_time.as_micros() as u64;
971 t.total_us = started.elapsed().as_micros() as u64;
972 }
973 Ok(response)
974 }
975
976 fn search_with_metadata_once(
977 &self,
978 pattern: &str,
979 filter: QueryFilter,
980 ) -> Result<QueryResponse> {
981 log::info!(
982 "Executing query with metadata: pattern='{}', filter={:?}",
983 pattern,
984 filter
985 );
986
987 let started = std::time::Instant::now();
988
989 if !self.cache.exists() {
991 return Err(crate::errors::ReflexError::IndexNotFound.into());
992 }
993
994 self.open_index()?;
999 let open_us = started.elapsed().as_micros() as u64;
1000
1001 let prepared = prepare_literal_pattern(pattern, &filter);
1005 let regex_warning = filter::regex_syntax_warning(pattern, &filter);
1006 let mut filter = filter;
1007 if prepared.use_regex && !filter.use_regex {
1008 log::debug!(
1009 "Pattern {:?} rewritten to regex {:?}",
1010 pattern,
1011 prepared.effective
1012 );
1013 filter.rewritten_from = Some(pattern.to_string());
1014 }
1015 filter.use_regex = prepared.use_regex;
1016
1017 let status_started = std::time::Instant::now();
1023 let (searched, search_done, snapshot) = std::thread::scope(|s| {
1024 let status = s.spawn(|| {
1025 let snapshot = status_cache::snapshot(&self.cache);
1026 (snapshot, status_started.elapsed())
1027 });
1028 let searched = self.search_internal(&prepared.effective, filter.clone());
1029 let search_done = started.elapsed();
1031 let snapshot = status
1032 .join()
1033 .unwrap_or_else(|panic| std::panic::resume_unwind(panic));
1034 (searched, search_done, snapshot)
1035 });
1036 let Internal {
1037 results,
1038 total_count: total,
1039 total_is_exact,
1040 approx_total,
1041 substring_only,
1042 excluded_by_default,
1043 counted,
1044 candidates_us,
1045 index_path,
1046 warnings: engine_warnings,
1047 } = searched?;
1048 let (snapshot, status_compute) = snapshot;
1049 let snapshot = snapshot?;
1050
1051 let scope: Vec<String> = results.iter().map(|r| r.path.clone()).collect();
1055 let report = Self::report_from(&snapshot, Some(&scope));
1056 let (status, can_trust_results, warning) =
1057 (report.status, report.can_trust_results, report.warning);
1058 let status_done = started.elapsed();
1059
1060 use crate::models::PaginationInfo;
1062 let pagination = PaginationInfo {
1063 total: total_is_exact.then_some(total),
1065 count: results.len(),
1066 offset: filter.offset.unwrap_or(0),
1067 limit: filter.limit,
1068 has_more: !total_is_exact || total > filter.offset.unwrap_or(0) + results.len(),
1069 total_is_exact,
1070 approx_total,
1071 };
1072
1073 let grouped_results = self.group_and_load_dependencies(
1076 results,
1077 filter.include_dependencies,
1078 filter.context_lines,
1079 )?;
1080
1081 let total_elapsed = started.elapsed();
1082 let timings = crate::models::QueryTimings {
1083 index_path,
1084 open_us,
1085 candidates_us,
1086 verify_us: (search_done.as_micros() as u64).saturating_sub(open_us + candidates_us),
1087 status_us: (status_done - search_done).as_micros() as u64,
1088 status_compute_us: status_compute.as_micros() as u64,
1089 group_us: (total_elapsed - status_done).as_micros() as u64,
1090 total_us: total_elapsed.as_micros() as u64,
1091 update_us: 0,
1092 };
1093 log::debug!("Query timings for '{}': {:?}", pattern, timings);
1094
1095 let substring_hint_count = if total == 0 { substring_only } else { None };
1099 let excluded_by_default =
1100 (total == 0 && excluded_by_default > 0).then_some(excluded_by_default);
1101 let zero = if total == 0 {
1102 let config = self.cache.load_index_config().unwrap_or_default();
1103 self.open_index().ok().and_then(|open| {
1104 zero_hint::explain_zero(
1105 &self.cache.workspace_root(),
1106 &config,
1107 &open,
1108 &filter,
1109 pattern,
1110 substring_only,
1111 excluded_by_default.unwrap_or(0),
1112 prepared.warning.is_some(),
1113 )
1114 })
1115 } else {
1116 None
1117 };
1118 let (hint, excluded_reason) = match zero {
1119 Some(z) => (Some(z.text), Some(z.reason)),
1120 None => (None, None),
1121 };
1122
1123 Ok(QueryResponse {
1124 ai_instruction: None, status,
1126 can_trust_results,
1127 warning,
1128 pagination,
1129 results: grouped_results,
1130 substring_hint_count,
1131 warnings: prepared
1132 .warning
1133 .into_iter()
1134 .chain(regex_warning)
1135 .chain(filter::no_symbol_parser_warning(&filter))
1136 .chain(engine_warnings)
1137 .collect(),
1138 hint,
1139 excluded_reason,
1140 excluded_by_default,
1141 file_count: counted.map(|(_, files)| files),
1142 timings: filter.collect_timings.then_some(timings),
1143 })
1144 }
1145
1146 pub fn search(&self, pattern: &str, filter: QueryFilter) -> Result<Vec<SearchResult>> {
1151 log::info!(
1152 "Executing query: pattern='{}', filter={:?}",
1153 pattern,
1154 filter
1155 );
1156
1157 self.update_first(&filter)?;
1159
1160 self.open_index()?;
1162
1163 let prepared = prepare_literal_pattern(pattern, &filter);
1166 let mut filter = filter;
1167 if prepared.use_regex && !filter.use_regex {
1168 filter.rewritten_from = Some(pattern.to_string());
1169 }
1170 filter.use_regex = prepared.use_regex;
1171
1172 let mut results = self
1174 .search_internal(&prepared.effective, filter.clone())?
1175 .results;
1176
1177 self.load_dependencies(&mut results, filter.include_dependencies)?;
1179
1180 Ok(results)
1181 }
1182
1183 fn search_internal(&self, pattern: &str, filter: QueryFilter) -> Result<Internal> {
1186 use std::time::{Duration, Instant};
1187
1188 let start_time = Instant::now();
1190 let timeout = if filter.timeout_secs > 0 {
1191 Some(Duration::from_secs(filter.timeout_secs))
1192 } else {
1193 None
1194 };
1195
1196 let is_keyword_query = if filter.symbols_mode || filter.kind.is_some() {
1210 pattern.is_empty() || ParserFactory::get_all_keywords().contains(&pattern)
1211 } else {
1212 false
1213 };
1214
1215 let mut filter = filter.clone(); if is_keyword_query
1220 && filter.kind.is_none()
1221 && let Some(inferred_kind) = Self::keyword_to_kind(pattern)
1222 {
1223 log::info!(
1224 "Keyword '{}' mapped to kind {:?} (auto-inferred)",
1225 pattern,
1226 inferred_kind
1227 );
1228 filter.kind = Some(inferred_kind);
1229 }
1230
1231 let guarded_literal = filter.rewritten_from.as_deref();
1245 if !filter.force && (!filter.use_regex || guarded_literal.is_some()) && !is_keyword_query {
1246 let total_files = self.open_index()?.file_count();
1249 let pattern = guarded_literal.unwrap_or(pattern);
1250 let pattern_len = pattern.chars().count();
1251
1252 let large_index_threshold = filter.test_large_index_threshold.unwrap_or(20_000);
1257 let short_pattern_threshold = filter.test_short_pattern_threshold.unwrap_or(4);
1258
1259 if total_files > large_index_threshold && pattern_len < short_pattern_threshold {
1260 anyhow::bail!(
1261 "Query too broad - would be expensive to execute on this large index\n\
1262 \n\
1263 This index contains {} files, and pattern '{}' ({} characters) is too short for efficient searching.\n\
1264 On large codebases, short patterns can take 10-30+ seconds to complete.\n\
1265 \n\
1266 This query could:\n\
1267 • Hang for an extended period before returning results\n\
1268 • Return thousands of results\n\
1269 • Flood LLM context windows with excessive data\n\
1270 • Fail entirely\n\
1271 \n\
1272 Suggestions to narrow the query:\n\
1273 • Use a longer, more specific pattern (4+ characters recommended for large indexes)\n\
1274 • Add a language filter: --lang <language>\n\
1275 • Add a file filter: --glob <pattern> or --file <path>\n\
1276 • Use --force to bypass this check if you really need all results\n\
1277 \n\
1278 To force execution anyway:\n\
1279 rfx query \"{}\" --force",
1280 total_files,
1281 pattern,
1282 pattern_len,
1283 pattern
1284 );
1285 }
1286 }
1287
1288 let budget = if filter.symbols_mode
1294 || filter.kind.is_some()
1295 || filter.use_ast
1296 || is_keyword_query
1297 || filter.require_exact_total
1298 {
1299 None
1300 } else {
1301 filter
1302 .limit
1303 .map(|limit| filter.offset.unwrap_or(0).saturating_add(limit))
1304 };
1305
1306 let mut substring_only = None;
1308 let mut excluded_by_default = 0usize;
1309 let mut counted = None;
1310 let mut candidates_us = 0u64;
1311 let mut total_is_exact = true;
1312 let mut approx_total = None;
1313 let index_path;
1314 let engine_warnings;
1315 let mut results = if is_keyword_query {
1316 index_path = IndexPath::Scan;
1318 engine_warnings = Vec::new();
1319 if let Some(lang) = filter.language {
1322 log::info!(
1323 "Keyword query detected for '{}' - scanning all {:?} files (bypassing trigram search)",
1324 pattern,
1325 lang
1326 );
1327 } else {
1328 log::info!(
1329 "Keyword query detected for '{}' - scanning all files (bypassing trigram search)",
1330 pattern
1331 );
1332 }
1333 self.get_all_language_files(&filter)?
1334 } else {
1335 let (candidates, stats) = if filter.use_regex {
1336 self.get_regex_candidates(pattern, &filter, timeout.as_ref(), &start_time, budget)?
1338 } else {
1339 self.get_trigram_candidates(pattern, &filter, budget)?
1341 };
1342 substring_only = stats.substring_only;
1343 excluded_by_default = stats.excluded_by_default;
1344 counted = stats.counted;
1345 candidates_us = stats.candidates_us;
1346 total_is_exact = stats.exhausted;
1347 approx_total = (!stats.exhausted).then_some(stats.approx_total).flatten();
1348 index_path = stats.index_path;
1349 engine_warnings = stats.warnings;
1350 candidates
1351 };
1352
1353 if !is_keyword_query && let Some(lang) = filter.language {
1359 let before_count = results.len();
1360 results.retain(|r| r.lang == lang);
1361 log::debug!(
1362 "Language filter ({:?}): reduced {} candidates to {} candidates",
1363 lang,
1364 before_count,
1365 results.len()
1366 );
1367 }
1368
1369 if !filter.glob_patterns.is_empty() || !filter.exclude_patterns.is_empty() {
1373 let include_matcher = result::build_glob_set(&filter.glob_patterns, "glob");
1375
1376 let exclude_matcher = result::build_glob_set(&filter.exclude_patterns, "exclude");
1378
1379 let before_count = results.len();
1381 results.retain(|r| {
1382 let included = if let Some(ref matcher) = include_matcher {
1384 matcher.is_match(&r.path)
1385 } else {
1386 true };
1388
1389 let excluded = if let Some(ref matcher) = exclude_matcher {
1391 matcher.is_match(&r.path)
1392 } else {
1393 false };
1395
1396 included && !excluded
1397 });
1398 log::debug!(
1399 "Glob filter: reduced {} candidates to {} candidates",
1400 before_count,
1401 results.len()
1402 );
1403 }
1404
1405 if let Some(timeout_duration) = timeout
1407 && start_time.elapsed() > timeout_duration
1408 {
1409 anyhow::bail!(
1410 "Query timeout exceeded ({} seconds).\n\
1411 \n\
1412 The query took too long to complete. Try one of these approaches:\n\
1413 • Use a more specific search pattern (longer patterns = faster search)\n\
1414 • Add a language filter with --lang to narrow the search space\n\
1415 • Add a file filter with --file to search specific directories\n\
1416 • Increase the timeout with --timeout <seconds>\n\
1417 \n\
1418 Example: rfx query \"{}\" --lang rust --timeout 60",
1419 filter.timeout_secs,
1420 pattern
1421 );
1422 }
1423
1424 if !filter.force {
1427 let candidate_count = results.len();
1428 let pattern_len = pattern.chars().count();
1429
1430 let is_short_pattern = pattern_len < 3 && !filter.use_regex && !is_keyword_query;
1433
1434 let is_broad_ast =
1437 filter.use_ast && filter.glob_patterns.is_empty() && candidate_count >= 100;
1438
1439 let threshold = if filter.use_ast && filter.glob_patterns.is_empty() {
1446 100 } else if filter.use_ast {
1448 10_000 } else if is_keyword_query {
1450 20_000 } else {
1452 50_000 };
1454
1455 let has_many_candidates = candidate_count > threshold
1456 && (filter.symbols_mode || filter.kind.is_some() || filter.use_ast);
1457
1458 if is_short_pattern || has_many_candidates || is_broad_ast {
1459 let reason = if is_short_pattern {
1460 format!(
1461 "Pattern '{}' is too short ({} characters). Short patterns bypass trigram optimization and require scanning many files.",
1462 pattern, pattern_len
1463 )
1464 } else if is_broad_ast {
1465 format!(
1466 "AST query without --glob restriction will scan the entire codebase ({} files). AST queries are SLOW (500ms-10s+).",
1467 candidate_count
1468 )
1469 } else if is_keyword_query {
1470 format!(
1471 "Keyword query '{}' matched {} files. This query scans all files of the target language, which will take significant time and produce excessive results.",
1472 pattern, candidate_count
1473 )
1474 } else {
1475 format!(
1476 "Query matched {} files. Parsing this many files with --symbols or --kind will take significant time and produce excessive results.",
1477 candidate_count
1478 )
1479 };
1480
1481 let suggestions = if is_short_pattern {
1482 vec![
1483 "• Use a longer, more specific pattern (3+ characters recommended)",
1484 "• Add a language filter: --lang <language>",
1485 "• Add a file path filter: --file <path> or --glob <pattern>",
1486 "• Use --force to bypass this check if you really need all results",
1487 ]
1488 } else if is_broad_ast {
1489 vec![
1490 "• Add --glob to restrict AST query to specific files: --glob 'src/**/*.rs'",
1491 "• Use --symbols instead (10-100x faster in 95% of cases)",
1492 "• Use --force to bypass this check if you need a full codebase scan",
1493 ]
1494 } else if is_keyword_query {
1495 vec![
1496 "• Add a language filter to reduce files scanned: --lang <language>",
1497 "• Add glob patterns to search specific directories: --glob 'src/**/*.rs'",
1498 "• Add --kind to filter to specific symbol types: --kind function",
1499 "• Use a more specific pattern instead of a keyword",
1500 "• Use --force to bypass this check if you need all results",
1501 ]
1502 } else {
1503 vec![
1504 "• Add a language filter to reduce candidate set: --lang <language>",
1505 "• Add glob patterns to search specific directories: --glob 'src/**/*.rs'",
1506 "• Use a more specific search pattern",
1507 "• Use --force to bypass this check if you need all results",
1508 ]
1509 };
1510
1511 let mut cmd_flags = String::new();
1513 if filter.symbols_mode {
1514 cmd_flags.push_str("--symbols ");
1515 }
1516 if let Some(ref lang) = filter.language {
1517 cmd_flags.push_str(&format!("--lang {:?} ", lang));
1518 }
1519 if let Some(ref kind) = filter.kind {
1520 cmd_flags.push_str(&format!("--kind {:?} ", kind));
1521 }
1522 if filter.use_ast {
1523 cmd_flags.push_str("--ast ");
1524 }
1525
1526 anyhow::bail!(
1527 "Query too broad - would be expensive to execute\n\
1528 \n\
1529 {}\n\
1530 \n\
1531 This query could:\n\
1532 • Hang for an extended period before returning results\n\
1533 • Return thousands of results\n\
1534 • Flood LLM context windows with excessive data\n\
1535 • Fail entirely\n\
1536 \n\
1537 Suggestions to narrow the query:\n\
1538 {}\n\
1539 \n\
1540 To force execution anyway:\n\
1541 rfx query \"{}\" --force {}",
1542 reason,
1543 suggestions.join("\n "),
1544 pattern,
1545 cmd_flags
1546 );
1547 }
1548 }
1549
1550 if filter.symbols_mode || filter.kind.is_some() || filter.use_ast {
1553 results.sort_by(|a, b| {
1554 a.path
1555 .cmp(&b.path)
1556 .then_with(|| a.span.start_line.cmp(&b.span.start_line))
1557 });
1558
1559 let candidate_count = results.len();
1561 if candidate_count > 1000 && !filter.suppress_output {
1562 output::warn(&format!(
1563 "Pattern '{}' matched {} files - parsing may take some time. Consider using --file, --glob, or a more specific pattern to narrow the search.",
1564 pattern, candidate_count
1565 ));
1566 } else if candidate_count > 100 {
1567 log::info!(
1568 "Parsing {} candidate files for symbol extraction",
1569 candidate_count
1570 );
1571 }
1572 }
1573
1574 if filter.use_ast {
1576 results = self.enrich_with_ast(results, pattern, filter.language)?;
1578 } else if filter.symbols_mode || filter.kind.is_some() {
1579 results = self.enrich_with_symbols(results, pattern, &filter, is_keyword_query)?;
1581 }
1582
1583 if filter.symbols_mode || filter.kind.is_some() {
1592 let mut seen = std::collections::HashSet::<(String, usize, Option<String>)>::new();
1593 results.retain(|r| seen.insert((r.path.clone(), r.span.start_line, r.symbol.clone())));
1594 }
1595
1596 if let Some(ref kind) = filter.kind {
1599 results.retain(|r| {
1600 if matches!(kind, SymbolKind::Function) {
1601 matches!(r.kind, SymbolKind::Function | SymbolKind::Method)
1603 } else {
1604 r.kind == *kind
1605 }
1606 });
1607 }
1608
1609 if let Some(ref file_pattern) = filter.file_pattern {
1611 results.retain(|r| r.path.contains(file_pattern));
1612 }
1613
1614 if filter.exact && filter.symbols_mode {
1616 results.retain(|r| r.symbol.as_deref() == Some(pattern));
1617 }
1618
1619 if filter.expand {
1622 if let Ok(open) = self.open_index() {
1624 let content_reader = &open.snapshot;
1625 for result in &mut results {
1626 if result.span.start_line < result.span.end_line {
1628 if let Some(file_id) = open.file_id_for(&result.path) {
1630 if let Ok(content) = content_reader.get_file_content(file_id) {
1632 let lines: Vec<&str> = content.lines().collect();
1633 let start_idx = result.span.start_line.saturating_sub(1);
1634 let end_idx = result.span.end_line.min(lines.len());
1635
1636 if start_idx < end_idx {
1637 let full_body = lines[start_idx..end_idx].join("\n");
1638 result.preview =
1642 crate::parsers::preview::expand_preview(&full_body);
1643 }
1644 }
1645 }
1646 }
1647 }
1648 }
1649 }
1650
1651 if filter.paths_only {
1653 use std::collections::HashSet;
1654 let mut seen_paths = HashSet::new();
1655 results.retain(|r| seen_paths.insert(r.path.clone()));
1656 }
1657
1658 results.retain(|r| {
1662 filter::tier_admits(
1663 r.lang,
1664 filter.language,
1665 filter.exclude_text,
1666 filter.include_locks,
1667 filter.include_generated,
1668 )
1669 });
1670
1671 results.sort_by(|a, b| {
1673 a.path
1674 .cmp(&b.path)
1675 .then_with(|| a.span.start_line.cmp(&b.span.start_line))
1676 });
1677
1678 let total_count = counted.map(|(lines, _)| lines).unwrap_or(results.len());
1682
1683 if let Some(offset) = filter.offset {
1685 if offset < results.len() {
1686 results = results.into_iter().skip(offset).collect();
1687 } else {
1688 results.clear();
1690 }
1691 }
1692
1693 if let Some(limit) = filter.limit {
1695 results.truncate(limit);
1696 }
1697
1698 log::info!(
1699 "Query returned {} results (total before pagination: {})",
1700 results.len(),
1701 total_count
1702 );
1703
1704 Ok(Internal {
1705 results,
1706 total_count,
1707 total_is_exact,
1708 approx_total,
1709 substring_only,
1710 excluded_by_default,
1711 counted,
1712 candidates_us,
1713 index_path,
1714 warnings: engine_warnings,
1715 })
1716 }
1717
1718 pub fn find_symbol(&self, name: &str) -> Result<Vec<SearchResult>> {
1720 let filter = QueryFilter {
1721 symbols_mode: true,
1722 ..Default::default()
1723 };
1724 self.search(name, filter)
1725 }
1726
1727 pub fn search_ast(&self, pattern: &str, lang: Option<Language>) -> Result<Vec<SearchResult>> {
1729 let filter = QueryFilter {
1730 language: lang,
1731 use_ast: true,
1732 ..Default::default()
1733 };
1734
1735 self.search(pattern, filter)
1736 }
1737
1738 pub fn search_ast_all_files(
1759 &self,
1760 ast_pattern: &str,
1761 filter: QueryFilter,
1762 ) -> Result<Vec<SearchResult>> {
1763 log::info!(
1764 "Executing AST query on all files: pattern='{}', filter={:?}",
1765 ast_pattern,
1766 filter
1767 );
1768
1769 let lang = filter.language.ok_or_else(|| anyhow::anyhow!(
1771 "Language must be specified for AST pattern matching. Use --lang to specify the language.\n\
1772 \n\
1773 Example: rfx query \"(function_definition) @fn\" --ast --lang python"
1774 ))?;
1775
1776 self.update_first(&filter)?;
1778
1779 let open = self.open_index()?;
1781 let content_reader = &open.snapshot;
1782
1783 let include_matcher = result::build_glob_set(&filter.glob_patterns, "glob");
1785
1786 let exclude_matcher = result::build_glob_set(&filter.exclude_patterns, "exclude");
1787
1788 let mut candidates: Vec<SearchResult> = Vec::new();
1790
1791 for file_id in content_reader.live_ids() {
1792 let file_path = match content_reader.get_file_path(file_id) {
1793 Some(p) => p,
1794 None => continue,
1795 };
1796
1797 let detected_lang = Language::from_path(file_path);
1799
1800 if detected_lang != lang {
1802 continue;
1803 }
1804
1805 let file_path_str = file_path.to_string_lossy().to_string();
1806
1807 let included = include_matcher
1809 .as_ref()
1810 .is_none_or(|m| m.is_match(&file_path_str));
1811 let excluded = exclude_matcher
1812 .as_ref()
1813 .is_some_and(|m| m.is_match(&file_path_str));
1814
1815 if !included || excluded {
1816 continue;
1817 }
1818
1819 candidates.push(SearchResult {
1821 path: file_path_str,
1822 lang: detected_lang,
1823 span: Span {
1824 start_line: 1,
1825 end_line: 1,
1826 },
1827 symbol: None,
1828 kind: SymbolKind::Unknown("ast_query".to_string()),
1829 preview: String::new(),
1830 dependencies: None,
1831 });
1832 }
1833
1834 log::info!(
1835 "AST query scanning {} files for language {:?}",
1836 candidates.len(),
1837 lang
1838 );
1839
1840 if !filter.force && filter.glob_patterns.is_empty() && candidates.len() >= 100 {
1843 anyhow::bail!(
1844 "Query too broad - would be expensive to execute\n\
1845 \n\
1846 AST query without --glob restriction will scan the ENTIRE codebase ({} files). AST queries are SLOW (500ms-10s+).\n\
1847 \n\
1848 This query could:\n\
1849 • Hang for an extended period before returning results\n\
1850 • Return thousands of results\n\
1851 • Flood LLM context windows with excessive data\n\
1852 • Fail entirely\n\
1853 \n\
1854 Suggestions to narrow the query:\n\
1855 • Add --glob to restrict AST query to specific files: --glob 'src/**/*.rs'\n\
1856 • Use --symbols instead (10-100x faster in 95% of cases)\n\
1857 • Use --force to bypass this check if you need a full codebase scan\n\
1858 \n\
1859 To force execution anyway:\n\
1860 rfx query \"{}\" --force --ast --lang {:?}",
1861 candidates.len(),
1862 ast_pattern,
1863 lang
1864 );
1865 }
1866
1867 if candidates.is_empty() {
1868 if !filter.suppress_output {
1869 output::warn(&format!(
1870 "No files found for language {:?}. Check your language filter or glob patterns.",
1871 lang
1872 ));
1873 }
1874 return Ok(Vec::new());
1875 }
1876
1877 let mut results = self.enrich_with_ast(candidates, ast_pattern, filter.language)?;
1880
1881 log::debug!("AST query found {} matches before filtering", results.len());
1882
1883 if let Some(ref kind) = filter.kind {
1887 results.retain(|r| {
1888 if matches!(kind, SymbolKind::Function) {
1889 matches!(r.kind, SymbolKind::Function | SymbolKind::Method)
1890 } else {
1891 r.kind == *kind
1892 }
1893 });
1894 }
1895
1896 if filter.expand
1900 && let Ok(open) = self.open_index()
1901 {
1902 let content_reader = &open.snapshot;
1903 {
1904 for result in &mut results {
1905 if result.span.start_line < result.span.end_line
1906 && let Some(file_id) = open.file_id_for(&result.path)
1907 && let Ok(content) = content_reader.get_file_content(file_id)
1908 {
1909 let lines: Vec<&str> = content.lines().collect();
1910 let start_idx = result.span.start_line.saturating_sub(1);
1911 let end_idx = result.span.end_line.min(lines.len());
1912
1913 if start_idx < end_idx {
1914 let full_body = lines[start_idx..end_idx].join("\n");
1915 result.preview = crate::parsers::preview::expand_preview(&full_body);
1917 }
1918 }
1919 }
1920 }
1921 }
1922
1923 if filter.paths_only {
1925 use std::collections::HashSet;
1926 let mut seen_paths = HashSet::new();
1927 results.retain(|r| seen_paths.insert(r.path.clone()));
1928 }
1929
1930 results.retain(|r| {
1934 filter::tier_admits(
1935 r.lang,
1936 filter.language,
1937 filter.exclude_text,
1938 filter.include_locks,
1939 filter.include_generated,
1940 )
1941 });
1942
1943 results.sort_by(|a, b| {
1945 a.path
1946 .cmp(&b.path)
1947 .then_with(|| a.span.start_line.cmp(&b.span.start_line))
1948 });
1949
1950 if let Some(offset) = filter.offset {
1952 if offset < results.len() {
1953 results = results.into_iter().skip(offset).collect();
1954 } else {
1955 results.clear();
1956 }
1957 }
1958
1959 if let Some(limit) = filter.limit {
1961 results.truncate(limit);
1962 }
1963
1964 log::info!("AST query returned {} results", results.len());
1965
1966 self.load_dependencies(&mut results, filter.include_dependencies)?;
1968
1969 Ok(results)
1970 }
1971
1972 pub fn search_ast_with_text_filter(
1984 &self,
1985 text_pattern: &str,
1986 ast_pattern: &str,
1987 filter: QueryFilter,
1988 ) -> Result<Vec<SearchResult>> {
1989 log::info!(
1990 "Executing AST query with text filter: text='{}', ast='{}', filter={:?}",
1991 text_pattern,
1992 ast_pattern,
1993 filter
1994 );
1995
1996 self.update_first(&filter)?;
1998
1999 use std::time::{Duration, Instant};
2001 let start_time = Instant::now();
2002 let timeout = if filter.timeout_secs > 0 {
2003 Some(Duration::from_secs(filter.timeout_secs))
2004 } else {
2005 None
2006 };
2007
2008 let candidates = if filter.use_regex {
2010 self.get_regex_candidates(text_pattern, &filter, timeout.as_ref(), &start_time, None)?
2011 .0
2012 } else {
2013 self.get_trigram_candidates(text_pattern, &filter, None)?.0
2014 };
2015
2016 log::debug!("Phase 1 found {} candidate locations", candidates.len());
2017
2018 let mut results = self.enrich_with_ast(candidates, ast_pattern, filter.language)?;
2020
2021 log::debug!("Phase 2 AST matching found {} results", results.len());
2022
2023 if let Some(lang) = filter.language {
2025 results.retain(|r| r.lang == lang);
2026 }
2027
2028 if let Some(ref kind) = filter.kind {
2029 results.retain(|r| {
2030 if matches!(kind, SymbolKind::Function) {
2031 matches!(r.kind, SymbolKind::Function | SymbolKind::Method)
2032 } else {
2033 r.kind == *kind
2034 }
2035 });
2036 }
2037
2038 if let Some(ref file_pattern) = filter.file_pattern {
2039 results.retain(|r| r.path.contains(file_pattern));
2040 }
2041
2042 if !filter.glob_patterns.is_empty() || !filter.exclude_patterns.is_empty() {
2044 let include_matcher = result::build_glob_set(&filter.glob_patterns, "glob");
2045
2046 let exclude_matcher = result::build_glob_set(&filter.exclude_patterns, "exclude");
2047
2048 results.retain(|r| {
2049 let included = include_matcher.as_ref().is_none_or(|m| m.is_match(&r.path));
2050 let excluded = exclude_matcher
2051 .as_ref()
2052 .is_some_and(|m| m.is_match(&r.path));
2053 included && !excluded
2054 });
2055 }
2056
2057 if filter.exact && filter.symbols_mode {
2058 results.retain(|r| r.symbol.as_deref() == Some(text_pattern));
2059 }
2060
2061 if filter.expand
2063 && let Ok(open) = self.open_index()
2064 {
2065 let content_reader = &open.snapshot;
2066 {
2067 for result in &mut results {
2068 if result.span.start_line < result.span.end_line
2069 && let Some(file_id) = open.file_id_for(&result.path)
2070 && let Ok(content) = content_reader.get_file_content(file_id)
2071 {
2072 let lines: Vec<&str> = content.lines().collect();
2073 let start_idx = result.span.start_line.saturating_sub(1);
2074 let end_idx = result.span.end_line.min(lines.len());
2075
2076 if start_idx < end_idx {
2077 let full_body = lines[start_idx..end_idx].join("\n");
2078 result.preview = crate::parsers::preview::expand_preview(&full_body);
2080 }
2081 }
2082 }
2083 }
2084 }
2085
2086 results.retain(|r| {
2090 filter::tier_admits(
2091 r.lang,
2092 filter.language,
2093 filter.exclude_text,
2094 filter.include_locks,
2095 filter.include_generated,
2096 )
2097 });
2098
2099 results.sort_by(|a, b| {
2101 a.path
2102 .cmp(&b.path)
2103 .then_with(|| a.span.start_line.cmp(&b.span.start_line))
2104 });
2105
2106 if let Some(offset) = filter.offset {
2108 if offset < results.len() {
2109 results = results.into_iter().skip(offset).collect();
2110 } else {
2111 results.clear();
2112 }
2113 }
2114
2115 if let Some(limit) = filter.limit {
2117 results.truncate(limit);
2118 }
2119
2120 log::info!("AST query returned {} results", results.len());
2121
2122 Ok(results)
2123 }
2124
2125 pub fn list_by_kind(&self, kind: SymbolKind) -> Result<Vec<SearchResult>> {
2127 let filter = QueryFilter {
2128 kind: Some(kind),
2129 symbols_mode: true,
2130 ..Default::default()
2131 };
2132
2133 self.search("*", filter)
2134 }
2135
2136 fn enrich_with_symbols(
2157 &self,
2158 candidates: Vec<SearchResult>,
2159 pattern: &str,
2160 filter: &QueryFilter,
2161 keyword_query: bool,
2162 ) -> Result<Vec<SearchResult>> {
2163 use rayon::prelude::*;
2164 use std::collections::{HashMap, HashSet};
2165
2166 let open = self.open_index()?;
2168 let content_reader = &open.snapshot;
2169
2170 let mut files_by_path: HashMap<String, Vec<SearchResult>> = HashMap::new();
2172 let mut skipped_unsupported = 0;
2173
2174 for candidate in candidates {
2175 if !candidate.lang.is_supported() {
2177 skipped_unsupported += 1;
2178 continue;
2179 }
2180
2181 files_by_path
2182 .entry(candidate.path.clone())
2183 .or_default()
2184 .push(candidate);
2185 }
2186
2187 let total_files = files_by_path.len();
2188 log::debug!(
2189 "Processing {} candidate files for symbol enrichment (skipped {} unsupported language files)",
2190 total_files,
2191 skipped_unsupported
2192 );
2193
2194 if total_files > 1000 && !filter.suppress_output {
2196 output::warn(&format!(
2197 "Pattern '{}' matched {} files. This may take some time to parse. Consider using a more specific pattern or adding --lang/--file filters to narrow the search.",
2198 pattern, total_files
2199 ));
2200 }
2201
2202 let pool = open.pool();
2204
2205 let files_to_skip: HashSet<String> = if keyword_query || filter.use_regex {
2218 HashSet::new()
2219 } else {
2220 pool.install(|| {
2221 files_by_path
2222 .par_iter()
2223 .filter_map(|(file_path, cands)| {
2224 let lang = Language::from_path(std::path::Path::new(file_path));
2225 let line_filter = crate::line_filter::get_filter(lang)?;
2226 let file_id = open.file_id_for(file_path)?;
2227 let content = content_reader.get_file_content(file_id).ok()?;
2228 let all_lines: Vec<&str> = content.lines().collect();
2229
2230 let mut wanted: Vec<usize> =
2231 cands.iter().map(|c| c.span.start_line).collect();
2232 wanted.sort_unstable();
2233 wanted.dedup();
2234
2235 let mut saw_occurrence = false;
2236 for line_no in wanted {
2237 let Some(line) = line_no.checked_sub(1).and_then(|i| all_lines.get(i))
2238 else {
2239 continue;
2240 };
2241 let mut search_start = 0;
2242 while let Some(pos) = line[search_start..].find(pattern) {
2243 saw_occurrence = true;
2244 let at = search_start + pos;
2245 if !line_filter.is_in_comment_or_string(line, at) {
2246 return None;
2248 }
2249 search_start = at + pattern.len();
2250 }
2251 }
2252 saw_occurrence.then(|| {
2254 log::debug!(
2255 "Pre-filter: Skipping {} (all matches in comments/strings)",
2256 file_path
2257 );
2258 file_path.clone()
2259 })
2260 })
2261 .collect()
2262 })
2263 };
2264
2265 let files_to_process: Vec<String> = files_by_path
2266 .keys()
2267 .filter(|p| !files_to_skip.contains(p.as_str()))
2268 .cloned()
2269 .collect();
2270
2271 log::debug!(
2272 "Pre-filter: Skipped {} files where all matches are in comments/strings (parsing {} files)",
2273 files_to_skip.len(),
2274 files_to_process.len()
2275 );
2276
2277 let mut conn = open.meta_conn()?;
2281 let rows = crate::cache::CacheManager::file_rows_on(&conn, &files_to_process)
2282 .context("Failed to load file hashes")?;
2283 log::debug!("Loaded {} file rows for symbol cache lookups", rows.len());
2284
2285 let file_lookup_tuples: Vec<(i64, String, String)> = files_to_process
2286 .iter()
2287 .filter_map(|path| {
2288 let (id, hash) = rows.get(path)?;
2289 Some((*id, hash.clone(), path.clone()))
2290 })
2291 .collect();
2292
2293 let batch_results = crate::symbol_cache::SymbolCache::batch_get_with_kind_on(
2294 &conn,
2295 &file_lookup_tuples,
2296 filter.kind.clone(),
2297 )
2298 .context("Failed to batch read symbol cache")?;
2299
2300 let id_to_path: HashMap<i64, &str> = rows
2301 .iter()
2302 .map(|(path, (id, _))| (*id, path.as_str()))
2303 .collect();
2304
2305 let mut cached_symbols: HashMap<String, Vec<SearchResult>> = HashMap::new();
2306 for (file_id, symbols) in batch_results {
2307 if let Some(file_path) = id_to_path.get(&file_id) {
2308 cached_symbols.insert(file_path.to_string(), symbols);
2309 }
2310 }
2311
2312 let files_needing_parse: Vec<String> = files_to_process
2314 .iter()
2315 .filter(|p| !cached_symbols.contains_key(p.as_str()))
2316 .cloned()
2317 .collect();
2318
2319 log::debug!(
2320 "Symbol cache: {} hits, {} need parsing",
2321 cached_symbols.len(),
2322 files_needing_parse.len()
2323 );
2324
2325 type ParsedFile = (Vec<SearchResult>, Option<(i64, String)>);
2330 let parsed: Vec<ParsedFile> = pool.install(|| {
2331 files_needing_parse
2332 .par_iter()
2333 .map(|file_path| {
2334 let Some(file_id) = open.file_id_for(file_path) else {
2335 log::warn!("Could not find file_id for path: {}", file_path);
2336 return (Vec::new(), None);
2337 };
2338 let content = match content_reader.get_file_content(file_id) {
2339 Ok(c) => c,
2340 Err(e) => {
2341 log::warn!("Failed to read file {}: {}", file_path, e);
2342 return (Vec::new(), None);
2343 }
2344 };
2345 let lang = Language::from_path(std::path::Path::new(file_path));
2346 let symbols = match ParserFactory::parse(file_path, content, lang) {
2347 Ok(symbols) => {
2348 log::debug!("Parsed {} symbols from {}", symbols.len(), file_path);
2349 symbols
2350 }
2351 Err(e) => {
2352 log::debug!("Failed to parse {}: {}", file_path, e);
2353 Vec::new()
2354 }
2355 };
2356 let key = rows.get(file_path).map(|(id, hash)| (*id, hash.clone()));
2357 (symbols, key)
2358 })
2359 .collect()
2360 });
2361
2362 let mut parsed_symbols: Vec<SearchResult> = Vec::new();
2363 let mut to_cache: Vec<(i64, String, Vec<SearchResult>)> = Vec::new();
2364 for (symbols, key) in parsed {
2365 if let Some((id, hash)) = key {
2366 to_cache.push((id, hash, symbols.clone()));
2367 }
2368 parsed_symbols.extend(symbols);
2369 }
2370 if !open.meta_matches_snapshot(&conn) {
2374 log::debug!(
2375 "Index is mid-publish; not caching {} parses",
2376 to_cache.len()
2377 );
2378 } else if let Err(e) =
2379 crate::symbol_cache::SymbolCache::batch_set_by_id_on(&mut conn, &to_cache)
2380 {
2381 log::debug!(
2382 "Failed to cache symbols for {} files: {}",
2383 to_cache.len(),
2384 e
2385 );
2386 }
2387 drop(conn);
2388
2389 let mut all_symbols: Vec<SearchResult> = Vec::new();
2391
2392 for symbols in cached_symbols.values() {
2394 all_symbols.extend_from_slice(symbols);
2395 }
2396
2397 all_symbols.extend(parsed_symbols);
2399
2400 let is_keyword_query = {
2408 let lang_to_check = if let Some(lang) = filter.language {
2410 vec![lang]
2413 } else {
2414 let mut langs: Vec<Language> =
2418 all_symbols.iter().map(|s| s.lang).collect::<Vec<_>>();
2419 langs.sort_by(|a, b| format!("{:?}", a).cmp(&format!("{:?}", b))); langs.dedup(); langs
2422 };
2423
2424 lang_to_check
2426 .iter()
2427 .any(|lang| ParserFactory::get_keywords(*lang).contains(&pattern))
2428 };
2429
2430 let filtered: Vec<SearchResult> = if is_keyword_query {
2433 log::info!(
2434 "Pattern '{}' is a language keyword - listing all symbols (kind filtering will be applied in Phase 3)",
2435 pattern
2436 );
2437 all_symbols
2438 } else if filter.use_regex {
2439 use std::collections::{HashMap, HashSet};
2445 let mut candidate_lines: HashMap<String, HashSet<usize>> = HashMap::new();
2446 for candidate in &files_by_path {
2447 for cand in candidate.1 {
2448 candidate_lines
2449 .entry(candidate.0.clone())
2450 .or_default()
2451 .insert(cand.span.start_line);
2452 }
2453 }
2454
2455 all_symbols
2457 .into_iter()
2458 .filter(|sym| {
2459 if let Some(lines) = candidate_lines.get(&sym.path) {
2460 for line in sym.span.start_line..=sym.span.end_line {
2462 if lines.contains(&line) {
2463 return true;
2464 }
2465 }
2466 }
2467 false
2468 })
2469 .collect()
2470 } else if filter.use_contains {
2471 all_symbols
2473 .into_iter()
2474 .filter(|sym| sym.symbol.as_deref().is_some_and(|s| s.contains(pattern)))
2475 .collect()
2476 } else {
2477 all_symbols
2479 .into_iter()
2480 .filter(|sym| sym.symbol.as_deref() == Some(pattern))
2481 .collect()
2482 };
2483
2484 log::info!(
2485 "Symbol enrichment found {} matches for pattern '{}'",
2486 filtered.len(),
2487 pattern
2488 );
2489
2490 Ok(filtered)
2491 }
2492
2493 fn enrich_with_ast(
2512 &self,
2513 candidates: Vec<SearchResult>,
2514 ast_pattern: &str,
2515 language: Option<Language>,
2516 ) -> Result<Vec<SearchResult>> {
2517 let lang = language.ok_or_else(|| anyhow::anyhow!(
2519 "Language must be specified for AST pattern matching. Use --lang to specify the language."
2520 ))?;
2521
2522 let open = self.open_index()?;
2524 let content_reader = &open.snapshot;
2525
2526 use std::collections::HashMap;
2528 let mut file_contents: HashMap<String, String> = HashMap::new();
2529
2530 for candidate in &candidates {
2531 if file_contents.contains_key(&candidate.path) {
2532 continue;
2533 }
2534
2535 let file_id = match open.file_id_for(&candidate.path) {
2537 Some(id) => id,
2538 None => {
2539 log::warn!("Could not find file_id for path: {}", candidate.path);
2540 continue;
2541 }
2542 };
2543
2544 let content = match content_reader.get_file_content(file_id) {
2546 Ok(c) => c,
2547 Err(e) => {
2548 log::warn!("Failed to read file {}: {}", candidate.path, e);
2549 continue;
2550 }
2551 };
2552
2553 file_contents.insert(candidate.path.clone(), content.to_string());
2554 }
2555
2556 log::debug!(
2557 "Executing AST query on {} candidate files with language {:?}",
2558 file_contents.len(),
2559 lang
2560 );
2561
2562 let results =
2564 crate::ast_query::execute_ast_query(candidates, ast_pattern, lang, &file_contents)?;
2565
2566 log::info!(
2567 "AST query found {} matches for pattern '{}'",
2568 results.len(),
2569 ast_pattern
2570 );
2571
2572 Ok(results)
2573 }
2574
2575 fn keyword_to_kind(keyword: &str) -> Option<SymbolKind> {
2583 filter::keyword_to_kind(keyword)
2584 }
2585
2586 fn get_all_language_files(&self, filter: &QueryFilter) -> Result<Vec<SearchResult>> {
2594 let open = self.open_index()?;
2599 let content_reader = &open.snapshot;
2600
2601 let include_matcher = result::build_glob_set(&filter.glob_patterns, "glob");
2603
2604 let exclude_matcher = result::build_glob_set(&filter.exclude_patterns, "exclude");
2605
2606 let mut candidates: Vec<SearchResult> = Vec::new();
2608
2609 for file_id in content_reader.live_ids() {
2610 let file_path = match content_reader.get_file_path(file_id) {
2611 Some(p) => p,
2612 None => continue,
2613 };
2614
2615 let detected_lang = Language::from_path(file_path);
2617
2618 if let Some(lang) = filter.language
2620 && detected_lang != lang
2621 {
2622 continue;
2623 }
2624
2625 let file_path_str = file_path.to_string_lossy().to_string();
2626
2627 let included = include_matcher
2629 .as_ref()
2630 .is_none_or(|m| m.is_match(&file_path_str));
2631 let excluded = exclude_matcher
2632 .as_ref()
2633 .is_some_and(|m| m.is_match(&file_path_str));
2634
2635 if !included || excluded {
2636 continue;
2637 }
2638
2639 if let Some(ref file_pattern) = filter.file_pattern
2641 && !file_path_str.contains(file_pattern)
2642 {
2643 continue;
2644 }
2645
2646 candidates.push(SearchResult {
2649 path: file_path_str,
2650 lang: detected_lang,
2651 span: Span {
2652 start_line: 1,
2653 end_line: 1,
2654 },
2655 symbol: None,
2656 kind: SymbolKind::Unknown("keyword_query".to_string()),
2657 preview: String::new(),
2658 dependencies: None,
2659 });
2660 }
2661
2662 if let Some(lang) = filter.language {
2663 log::info!(
2664 "Keyword query will scan {} {:?} files for symbol extraction",
2665 candidates.len(),
2666 lang
2667 );
2668 } else {
2669 log::info!(
2670 "Keyword query will scan {} files (all languages) for symbol extraction",
2671 candidates.len()
2672 );
2673 }
2674
2675 Ok(candidates)
2676 }
2677
2678 fn get_trigram_candidates(
2684 &self,
2685 pattern: &str,
2686 filter: &QueryFilter,
2687 budget: Option<usize>,
2688 ) -> Result<(Vec<SearchResult>, CandidateStats)> {
2689 let open = self.open_index()?;
2690 let trigram_index = &open.snapshot;
2691
2692 if pattern.chars().count() < 3 {
2696 log::info!(
2697 "Pattern '{}' is shorter than 3 chars — trigram index cannot be used, \
2698 falling back to linear scan",
2699 pattern
2700 );
2701 let results = self.linear_scan_candidates(pattern, filter, &open)?;
2702 let stats = CandidateStats {
2703 index_path: IndexPath::Scan,
2704 ..CandidateStats::default()
2705 };
2706 return Ok((results, stats));
2707 }
2708
2709 let candidates_started = std::time::Instant::now();
2711 let candidates = trigram_index.search_candidates(pattern);
2712 let candidates_us = candidates_started.elapsed().as_micros() as u64;
2713 log::debug!(
2714 "Found {} candidate locations from trigram search in {} us",
2715 candidates.len(),
2716 candidates_us
2717 );
2718
2719 let mut files: Vec<(u32, LineSet)> = Vec::new();
2722 for loc in candidates {
2723 match files.last_mut() {
2724 Some((id, LineSet::Only(lines))) if *id == loc.file_id => lines.push(loc.line_no),
2725 _ => files.push((loc.file_id, LineSet::Only(vec![loc.line_no]))),
2726 }
2727 }
2728 log::debug!("Scanning {} files with trigram matches", files.len());
2729
2730 let matcher = LineMatcher::new(pattern, filter)?;
2732 let file_filter = FileFilter::from_filter(filter);
2733
2734 let outcome = verify_files_streaming(
2735 &open,
2736 files,
2737 &matcher,
2738 &file_filter,
2739 filter.paths_only,
2740 budget,
2741 filter.count_only_line_search(),
2742 );
2743
2744 let stats = CandidateStats {
2745 candidates_us,
2746 substring_only: matcher.is_word_boundary().then_some(outcome.substring_only),
2747 excluded_by_default: outcome.excluded_by_default,
2748 counted: outcome.counted,
2749 exhausted: outcome.exhausted,
2750 approx_total: outcome.estimated_total,
2751 index_path: IndexPath::Trigram,
2752 warnings: Vec::new(),
2753 };
2754 Ok((outcome.results, stats))
2755 }
2756
2757 fn linear_scan_candidates(
2764 &self,
2765 pattern: &str,
2766 filter: &QueryFilter,
2767 open: &OpenIndex,
2768 ) -> Result<Vec<SearchResult>> {
2769 use rayon::prelude::*;
2770
2771 let content_reader = &open.snapshot;
2772 let pattern_owned = pattern.to_string();
2773 let matcher = LineMatcher::new(pattern, filter)?;
2774
2775 let results: Vec<SearchResult> = open.pool().install(|| {
2776 content_reader
2777 .live_ids()
2778 .collect::<Vec<_>>()
2779 .par_iter()
2780 .flat_map(|&file_id| {
2781 let file_path = match content_reader.get_file_path(file_id) {
2782 Some(p) => p.to_path_buf(),
2783 None => return Vec::new(),
2784 };
2785 let content = match content_reader.get_file_content(file_id) {
2786 Ok(c) => c,
2787 Err(_) => return Vec::new(),
2788 };
2789
2790 let file_path_str = file_path.to_string_lossy().to_string();
2791 let lang = Language::from_path(&file_path);
2792
2793 let mut seen_lines = std::collections::HashSet::new();
2794 let mut file_results = Vec::new();
2795
2796 for (line_idx, line) in content.lines().enumerate() {
2797 let line_no = line_idx + 1;
2798 if seen_lines.contains(&line_no) {
2799 continue;
2800 }
2801
2802 if !matcher.is_match(line) {
2803 continue;
2804 }
2805
2806 seen_lines.insert(line_no);
2807 file_results.push(SearchResult {
2808 path: file_path_str.clone(),
2809 lang,
2810 kind: SymbolKind::Unknown("text_match".to_string()),
2811 symbol: None,
2812 span: Span {
2813 start_line: line_no,
2814 end_line: line_no,
2815 },
2816 preview: crate::parsers::preview::line_preview(
2820 line,
2821 line.find(pattern_owned.as_str()).unwrap_or(0),
2822 ),
2823 dependencies: None,
2824 });
2825 }
2826
2827 file_results
2828 })
2829 .collect()
2830 });
2831
2832 log::info!(
2833 "Linear scan (short pattern '{}') found {} results across {} files",
2834 pattern,
2835 results.len(),
2836 content_reader.live_file_count()
2837 );
2838 Ok(results)
2839 }
2840
2841 fn get_regex_candidates(
2862 &self,
2863 pattern: &str,
2864 filter: &QueryFilter,
2865 timeout: Option<&std::time::Duration>,
2866 start_time: &std::time::Instant,
2867 budget: Option<usize>,
2868 ) -> Result<(Vec<SearchResult>, CandidateStats)> {
2869 let matcher = LineMatcher::new(pattern, filter)
2871 .with_context(|| format!("Invalid regex pattern: {}", pattern))?;
2872
2873 if let Some(timeout_duration) = timeout
2875 && start_time.elapsed() > *timeout_duration
2876 {
2877 anyhow::bail!(
2878 "Query timeout exceeded ({} seconds) during regex compilation",
2879 timeout_duration.as_secs()
2880 );
2881 }
2882
2883 use crate::regex_trigrams::extract_literals;
2886 let literals = extract_literals(pattern);
2887 let unfoldable = literals
2888 .iter()
2889 .any(|l| l.case_insensitive && !l.text.is_ascii());
2890
2891 let open = self.open_index()?;
2892 let candidates_started = std::time::Instant::now();
2893
2894 let mut warnings = Vec::new();
2895 let index_path;
2896 let files: Vec<(u32, LineSet)> = if literals.is_empty() || unfoldable {
2897 index_path = IndexPath::Scan;
2899 let short_literal = filter
2902 .rewritten_from
2903 .as_deref()
2904 .is_some_and(|p| p.chars().count() < 3);
2905 if !short_literal {
2906 let text = if unfoldable {
2907 format!(
2908 "Regex pattern '{}' has a non-ASCII case-insensitive literal, which the trigram index cannot fold; falling back to full content scan. This may be slow on large codebases.",
2909 pattern
2910 )
2911 } else {
2912 format!(
2913 "Regex pattern '{}' has no literals (≥3 chars), falling back to full content scan. This may be slow on large codebases. Consider using patterns with literal text.",
2914 pattern
2915 )
2916 };
2917 if !filter.suppress_output {
2918 output::warn(&text);
2919 }
2920 warnings.push(text);
2921 }
2922 open.snapshot
2923 .live_ids()
2924 .map(|id| (id, LineSet::All))
2925 .collect()
2926 } else {
2927 index_path = IndexPath::Trigram;
2928 log::debug!(
2929 "Using {} literals to narrow regex search candidates",
2930 literals.len()
2931 );
2932
2933 let mut locations: Vec<crate::trigram::FileLocation> = Vec::new();
2938 let mut sources = 0usize;
2939 let mut need_exotic = false;
2942 for literal in &literals {
2943 let found = if literal.case_insensitive {
2944 if literal
2945 .text
2946 .bytes()
2947 .any(|b| matches!(b.to_ascii_lowercase(), b'k' | b's'))
2948 {
2949 need_exotic = true;
2950 }
2951 open.snapshot
2952 .search_candidates_fold(literal.text.as_bytes())
2953 } else {
2954 open.snapshot.search_candidates(&literal.text)
2955 };
2956 log::debug!(
2957 "Literal '{}' (ci={}) found on {} candidate lines",
2958 literal.text,
2959 literal.case_insensitive,
2960 found.len()
2961 );
2962 locations.extend(found);
2963 sources += 1;
2964 }
2965 if need_exotic {
2966 locations.extend(open.snapshot.exotic_fold_lines());
2967 sources += 1;
2968 }
2969 if sources > 1 {
2970 locations.sort_unstable();
2971 locations.dedup();
2972 }
2973 let mut files: Vec<(u32, LineSet)> = Vec::new();
2975 for loc in locations {
2976 match files.last_mut() {
2977 Some((id, LineSet::Only(lines))) if *id == loc.file_id => {
2978 lines.push(loc.line_no)
2979 }
2980 _ => files.push((loc.file_id, LineSet::Only(vec![loc.line_no]))),
2981 }
2982 }
2983 files
2984 };
2985 let candidates_us = candidates_started.elapsed().as_micros() as u64;
2986 log::debug!(
2987 "Regex candidates: {} files in {} us",
2988 files.len(),
2989 candidates_us
2990 );
2991
2992 let file_filter = FileFilter::from_filter(filter);
2993 let outcome = verify_files_streaming(
2994 &open,
2995 files,
2996 &matcher,
2997 &file_filter,
2998 filter.paths_only,
2999 budget,
3000 filter.count_only_line_search(),
3001 );
3002
3003 log::info!(
3004 "Regex search found {} matches for pattern '{}'",
3005 outcome.results.len(),
3006 pattern
3007 );
3008 let stats = CandidateStats {
3009 candidates_us,
3010 substring_only: None,
3011 excluded_by_default: outcome.excluded_by_default,
3012 counted: outcome.counted,
3013 exhausted: outcome.exhausted,
3014 approx_total: outcome.estimated_total,
3015 index_path,
3016 warnings,
3017 };
3018 Ok((outcome.results, stats))
3019 }
3020
3021 pub fn get_index_status(&self) -> Result<(IndexStatus, bool, Option<IndexWarning>)> {
3026 self.index_status_for(None)
3027 }
3028
3029 pub fn fresh_index_status(&self) -> Result<(IndexStatus, bool, Option<IndexWarning>)> {
3034 let r = self.fresh_index_report()?;
3035 Ok((r.status, r.can_trust_results, r.warning))
3036 }
3037
3038 pub fn fresh_index_report(&self) -> Result<IndexStatusReport> {
3041 status_cache::invalidate(&self.cache.workspace_root());
3042 self.index_report_for(None)
3043 }
3044
3045 pub fn index_status_for(
3057 &self,
3058 scope: Option<&[String]>,
3059 ) -> Result<(IndexStatus, bool, Option<IndexWarning>)> {
3060 let r = self.index_report_for(scope)?;
3061 Ok((r.status, r.can_trust_results, r.warning))
3062 }
3063
3064 pub fn index_report_for(&self, scope: Option<&[String]>) -> Result<IndexStatusReport> {
3066 let snapshot = status_cache::snapshot(&self.cache)?;
3069 Ok(Self::report_from(&snapshot, scope))
3070 }
3071
3072 fn report_from(
3077 snapshot: &status_cache::Snapshot,
3078 scope: Option<&[String]>,
3079 ) -> IndexStatusReport {
3080 if let Some((status, can_trust_results, warning)) = snapshot.decided.clone() {
3081 return IndexStatusReport {
3082 status,
3083 can_trust_results,
3084 warning,
3085 details: snapshot.details.clone(),
3086 };
3087 }
3088 let changes = &snapshot.changes;
3089 let details = snapshot.details.clone();
3090
3091 if changes.is_empty() {
3092 return IndexStatusReport {
3093 status: IndexStatus::Fresh,
3094 can_trust_results: true,
3095 warning: None,
3096 details,
3097 };
3098 }
3099
3100 let hits_results = match scope {
3103 None | Some([]) => false,
3104 Some(paths) => changes.any(|changed| {
3105 paths
3106 .iter()
3107 .any(|p| p == changed || p.ends_with(changed) || changed.ends_with(p.as_str()))
3108 }),
3109 };
3110
3111 let reason = {
3112 let mut parts = Vec::new();
3113 if changes.modified_count > 0 {
3114 parts.push(format!("{} modified", changes.modified_count));
3115 }
3116 if changes.added_count > 0 {
3117 parts.push(format!("{} added", changes.added_count));
3118 }
3119 if changes.deleted_count > 0 {
3120 parts.push(format!("{} deleted", changes.deleted_count));
3121 }
3122 let impact = if hits_results {
3123 " — including a file these results came from"
3124 } else if changes.deleted_count > 0 {
3125 " — deleted files still produce hits at their old lines"
3126 } else {
3127 " — these results may not reflect them"
3128 };
3129 format!(
3130 "Files changed since the index was built ({}){}",
3131 parts.join(", "),
3132 impact
3133 )
3134 };
3135
3136 let some_if_any = |v: Vec<String>| if v.is_empty() { None } else { Some(v) };
3137 let warning = IndexWarning {
3138 reason,
3139 action_required: "index_project".to_string(),
3140 files_modified: some_if_any(changes.modified.clone()),
3141 files_added: some_if_any(changes.added.clone()),
3142 files_deleted: some_if_any(changes.deleted.clone()),
3143 changed_count: Some(
3144 changes.modified_count + changes.added_count + changes.deleted_count,
3145 ),
3146 truncated: changes.truncated,
3147 details: details.clone(),
3148 };
3149
3150 IndexStatusReport {
3154 status: IndexStatus::Stale,
3155 can_trust_results: false,
3156 warning: Some(warning),
3157 details,
3158 }
3159 }
3160
3161 fn check_index_freshness(&self, filter: &QueryFilter) -> Result<()> {
3168 let root = self.cache.workspace_root();
3169
3170 if crate::git::is_git_repo(&root) {
3172 if !crate::git::is_git_available() {
3173 static WARNED: std::sync::OnceLock<()> = std::sync::OnceLock::new();
3174 if !filter.suppress_output {
3175 WARNED.get_or_init(|| {
3176 output::warn("⚠️ git binary not found in PATH; index freshness checks disabled for this session.");
3177 });
3178 }
3179 return Ok(());
3180 }
3181 if let Ok(current_branch) = crate::git::get_current_branch(&root) {
3182 if !self.cache.branch_exists(¤t_branch).unwrap_or(false) {
3184 if !filter.suppress_output {
3185 output::warn(&format!(
3186 "⚠️ WARNING: Index not found for branch '{}'. Run 'rfx index' to index this branch.",
3187 current_branch
3188 ));
3189 }
3190 return Ok(());
3191 }
3192
3193 if let (Ok(current_commit), Ok(branch_info)) = (
3195 crate::git::get_current_commit(&root),
3196 self.cache.get_branch_info(¤t_branch),
3197 ) {
3198 if branch_info.commit_sha != current_commit {
3199 if !filter.suppress_output {
3200 output::warn(&format!(
3201 "⚠️ WARNING: Index may be stale (commit changed: {} → {}). Consider running 'rfx index'.",
3202 &branch_info.commit_sha[..7],
3203 ¤t_commit[..7]
3204 ));
3205 }
3206 return Ok(());
3207 }
3208
3209 if let Ok(branch_files) = self.cache.get_branch_files(¤t_branch) {
3212 let mut checked = 0;
3213 let mut changed = 0;
3214 const SAMPLE_SIZE: usize = 10;
3215
3216 for (path, _indexed_hash) in branch_files.iter().take(SAMPLE_SIZE) {
3217 checked += 1;
3218 let file_path = std::path::Path::new(path);
3219
3220 if let Ok(metadata) = std::fs::metadata(file_path)
3222 && let Ok(modified) = metadata.modified()
3223 {
3224 let indexed_time = branch_info.last_indexed;
3225 let file_time = modified
3226 .duration_since(std::time::UNIX_EPOCH)
3227 .unwrap_or_default()
3228 .as_secs()
3229 as i64;
3230
3231 if file_time > indexed_time {
3233 changed += 1;
3238 }
3239 }
3240 }
3241
3242 if changed > 0 && !filter.suppress_output {
3243 output::warn(&format!(
3244 "⚠️ WARNING: {} of {} sampled files changed since indexing. Consider running 'rfx index'.",
3245 changed, checked
3246 ));
3247 }
3248 }
3249 }
3250 }
3251 }
3252
3253 Ok(())
3254 }
3255}
3256
3257#[allow(clippy::too_many_arguments)]
3262pub fn generate_ai_instruction(
3263 result_count: usize,
3264 total_count: usize,
3265 has_more: bool,
3266 symbols_mode: bool,
3267 paths_only: bool,
3268 use_ast: bool,
3269 use_regex: bool,
3270 language_filter: bool,
3271 glob_filter: bool,
3272 exact_mode: bool,
3273) -> Option<String> {
3274 if result_count == 0 {
3276 return Some(
3277 "No results found. Consider these alternatives: 1) Check pattern spelling, 2) Remove --kind or --lang filters to broaden search, 3) Try partial match or related term, 4) Use a regex search (regex:true, or --regex on the CLI) for special characters or complex patterns."
3278 .to_string()
3279 );
3280 }
3281
3282 if total_count >= 500 {
3284 return Some(format!(
3285 "Query too broad: {} results found. STOP. Do not list results. Refine search automatically by adding filters: kind parameter (Function/Struct/Class), lang parameter (rust/python/etc), or glob parameter (['src/**/*.rs']). Call search_code again with appropriate filters.",
3286 total_count
3287 ));
3288 }
3289
3290 if has_more {
3298 return Some(format!(
3299 "Showing {} of {} results — {} more available. This is a partial answer. To finish a find-all task, call again with offset={} (raise limit up to 500 to get the rest in one call), or use mode=\"count\" first if you only need the total.",
3300 result_count,
3301 total_count,
3302 total_count.saturating_sub(result_count),
3303 result_count
3304 ));
3305 }
3306
3307 if result_count == 1 && symbols_mode {
3309 return Some(
3310 "Found 1 precise result. Respond concisely: '[symbol] at [path]:[line]'.".to_string(),
3311 );
3312 }
3313
3314 if (2..=10).contains(&result_count) && symbols_mode {
3316 return Some(format!(
3317 "Found {} precise results (definitions only, not usages). List locations concisely: '[symbol] at [path]:[line]' for each result.",
3318 result_count
3319 ));
3320 }
3321
3322 if (101..500).contains(&total_count) {
3324 return Some(format!(
3325 "Found {} results - this is broad. Suggest refining search with: kind parameter (Function/Struct/Class/etc), lang parameter (rust/python/etc), or glob parameter to narrow file scope.",
3326 total_count
3327 ));
3328 }
3329
3330 if result_count >= 100 && !symbols_mode {
3332 return Some(format!(
3333 "Found {} results in full-text search mode (includes definitions AND all usages). Consider using symbols=true parameter to filter to definitions only. This typically reduces results by 80-90%.",
3334 result_count
3335 ));
3336 }
3337
3338 if paths_only {
3340 return Some(format!(
3341 "Found {} unique files (paths-only mode - no code content included). Next step: Use Read tool on specific files that look relevant based on their paths.",
3342 result_count
3343 ));
3344 }
3345
3346 if use_ast {
3348 return Some(format!(
3349 "Found {} results using AST pattern matching. These are structure-based matches using Tree-sitter patterns, not text search.",
3350 result_count
3351 ));
3352 }
3353
3354 if use_regex && result_count >= 100 {
3356 return Some(format!(
3357 "Found {} results using regex pattern matching. Regex matches are expansive. Consider using exact text search or symbols mode for more precise results.",
3358 result_count
3359 ));
3360 }
3361
3362 if language_filter && result_count <= 5 {
3364 return Some(format!(
3365 "Found {} results with language filter active. Results are limited to this language only. Remove lang parameter if you want to search all languages.",
3366 result_count
3367 ));
3368 }
3369
3370 if glob_filter && result_count <= 10 {
3372 return Some(format!(
3373 "Found {} results with glob filter active. Results are limited to matching paths. Remove glob parameter to search entire codebase.",
3374 result_count
3375 ));
3376 }
3377
3378 if exact_mode && result_count <= 5 {
3380 return Some(format!(
3381 "Found {} results in exact match mode. Only exact symbol name matches are included. Remove exact parameter to allow substring matching.",
3382 result_count
3383 ));
3384 }
3385
3386 None
3388}
3389
3390#[cfg(test)]
3391mod tests {
3392 use super::*;
3393 use crate::indexer::Indexer;
3394 use crate::models::IndexConfig;
3395 use std::fs;
3396 use tempfile::TempDir;
3397
3398 #[test]
3401 fn test_query_engine_creation() {
3402 let temp = TempDir::new().unwrap();
3403 let cache = CacheManager::new(temp.path());
3404 let engine = QueryEngine::new(cache);
3405
3406 assert!(engine.cache.path().ends_with(".reflex"));
3407 }
3408
3409 #[test]
3410 fn test_filter_modes() {
3411 let filter_fulltext = QueryFilter::default();
3413 assert!(!filter_fulltext.symbols_mode);
3414
3415 let filter_symbols = QueryFilter {
3416 symbols_mode: true,
3417 ..Default::default()
3418 };
3419 assert!(filter_symbols.symbols_mode);
3420
3421 let filter_with_kind = QueryFilter {
3423 kind: Some(SymbolKind::Function),
3424 symbols_mode: true,
3425 ..Default::default()
3426 };
3427 assert!(filter_with_kind.symbols_mode);
3428 }
3429
3430 #[test]
3433 fn test_fulltext_search() {
3434 let temp = TempDir::new().unwrap();
3435 let project = temp.path().join("project");
3436 fs::create_dir(&project).unwrap();
3437
3438 fs::write(
3440 project.join("main.rs"),
3441 "fn main() {\n println!(\"hello\");\n}",
3442 )
3443 .unwrap();
3444 fs::write(project.join("lib.rs"), "pub fn hello() {}").unwrap();
3445
3446 let cache = CacheManager::new(&project);
3448 let indexer = Indexer::new(cache, IndexConfig::default());
3449 indexer.index(&project, false).unwrap();
3450
3451 let cache = CacheManager::new(&project);
3453 let engine = QueryEngine::new(cache);
3454 let filter = QueryFilter::default(); let results = engine.search("hello", filter).unwrap();
3456
3457 assert!(results.len() >= 2);
3459 assert!(results.iter().any(|r| r.path.contains("main.rs")));
3460 assert!(results.iter().any(|r| r.path.contains("lib.rs")));
3461 }
3462
3463 #[test]
3464 fn test_symbol_search() {
3465 let temp = TempDir::new().unwrap();
3466 let project = temp.path().join("project");
3467 fs::create_dir(&project).unwrap();
3468
3469 fs::write(
3471 project.join("main.rs"),
3472 "fn greet() {}\nfn main() {\n greet();\n}",
3473 )
3474 .unwrap();
3475
3476 let cache = CacheManager::new(&project);
3478 let indexer = Indexer::new(cache, IndexConfig::default());
3479 indexer.index(&project, false).unwrap();
3480
3481 let cache = CacheManager::new(&project);
3482
3483 let engine = QueryEngine::new(cache);
3485 let filter = QueryFilter {
3486 symbols_mode: true,
3487 ..Default::default()
3488 };
3489 let results = engine.search("greet", filter).unwrap();
3490
3491 assert!(!results.is_empty());
3493 assert!(results.iter().any(|r| r.kind == SymbolKind::Function));
3494 }
3495
3496 #[test]
3497 fn test_regex_search() {
3498 let temp = TempDir::new().unwrap();
3499 let project = temp.path().join("project");
3500 fs::create_dir(&project).unwrap();
3501
3502 fs::write(
3503 project.join("main.rs"),
3504 "fn test1() {}\nfn test2() {}\nfn other() {}",
3505 )
3506 .unwrap();
3507
3508 let cache = CacheManager::new(&project);
3509 let indexer = Indexer::new(cache, IndexConfig::default());
3510 indexer.index(&project, false).unwrap();
3511
3512 let cache = CacheManager::new(&project);
3513
3514 let engine = QueryEngine::new(cache);
3515 let filter = QueryFilter {
3516 use_regex: true,
3517 ..Default::default()
3518 };
3519 let results = engine.search(r"fn test\d", filter).unwrap();
3520
3521 assert_eq!(results.len(), 2);
3523 assert!(results.iter().all(|r| r.preview.contains("test")));
3524 }
3525
3526 #[test]
3529 fn test_language_filter() {
3530 let temp = TempDir::new().unwrap();
3531 let project = temp.path().join("project");
3532 fs::create_dir(&project).unwrap();
3533
3534 fs::write(project.join("main.rs"), "fn main() {}").unwrap();
3535 fs::write(project.join("main.js"), "function main() {}").unwrap();
3536
3537 let cache = CacheManager::new(&project);
3538 let indexer = Indexer::new(cache, IndexConfig::default());
3539 indexer.index(&project, false).unwrap();
3540
3541 let cache = CacheManager::new(&project);
3542
3543 let engine = QueryEngine::new(cache);
3544
3545 let filter = QueryFilter {
3547 language: Some(Language::Rust),
3548 ..Default::default()
3549 };
3550 let results = engine.search("main", filter).unwrap();
3551
3552 assert!(results.iter().all(|r| r.lang == Language::Rust));
3553 assert!(results.iter().all(|r| r.path.ends_with(".rs")));
3554 }
3555
3556 #[test]
3557 fn test_kind_filter() {
3558 let temp = TempDir::new().unwrap();
3559 let project = temp.path().join("project");
3560 fs::create_dir(&project).unwrap();
3561
3562 fs::write(
3563 project.join("main.rs"),
3564 "struct Point {}\nfn main() {}\nimpl Point { fn new() {} }",
3565 )
3566 .unwrap();
3567
3568 let cache = CacheManager::new(&project);
3569 let indexer = Indexer::new(cache, IndexConfig::default());
3570 indexer.index(&project, false).unwrap();
3571
3572 let cache = CacheManager::new(&project);
3573
3574 let engine = QueryEngine::new(cache);
3575
3576 let filter = QueryFilter {
3578 symbols_mode: true,
3579 kind: Some(SymbolKind::Function),
3580 use_contains: true, ..Default::default()
3582 };
3583 let results = engine.search("mai", filter).unwrap();
3585
3586 assert!(!results.is_empty(), "Should find at least one result");
3588 assert!(
3589 results.iter().any(|r| r.symbol.as_deref() == Some("main")),
3590 "Should find 'main' function"
3591 );
3592 }
3593
3594 #[test]
3595 fn test_file_pattern_filter() {
3596 let temp = TempDir::new().unwrap();
3597 let project = temp.path().join("project");
3598 fs::create_dir_all(project.join("src")).unwrap();
3599 fs::create_dir_all(project.join("tests")).unwrap();
3600
3601 fs::write(project.join("src/lib.rs"), "fn foo() {}").unwrap();
3602 fs::write(project.join("tests/test.rs"), "fn foo() {}").unwrap();
3603
3604 let cache = CacheManager::new(&project);
3605 let indexer = Indexer::new(cache, IndexConfig::default());
3606 indexer.index(&project, false).unwrap();
3607
3608 let cache = CacheManager::new(&project);
3609
3610 let engine = QueryEngine::new(cache);
3611
3612 let filter = QueryFilter {
3614 file_pattern: Some("src/".to_string()),
3615 ..Default::default()
3616 };
3617 let results = engine.search("foo", filter).unwrap();
3618
3619 assert!(results.iter().all(|r| r.path.contains("src/")));
3620 assert!(!results.iter().any(|r| r.path.contains("tests/")));
3621 }
3622
3623 #[test]
3624 fn test_limit_filter() {
3625 let temp = TempDir::new().unwrap();
3626 let project = temp.path().join("project");
3627 fs::create_dir(&project).unwrap();
3628
3629 let content = (0..20)
3631 .map(|i| format!("fn test{}() {{}}", i))
3632 .collect::<Vec<_>>()
3633 .join("\n");
3634 fs::write(project.join("main.rs"), content).unwrap();
3635
3636 let cache = CacheManager::new(&project);
3637 let indexer = Indexer::new(cache, IndexConfig::default());
3638 indexer.index(&project, false).unwrap();
3639
3640 let cache = CacheManager::new(&project);
3641
3642 let engine = QueryEngine::new(cache);
3643
3644 let filter = QueryFilter {
3646 limit: Some(5),
3647 use_contains: true, ..Default::default()
3649 };
3650 let results = engine.search("test", filter).unwrap();
3651
3652 assert_eq!(results.len(), 5);
3653 }
3654
3655 #[test]
3656 fn test_exact_match_filter() {
3657 let temp = TempDir::new().unwrap();
3658 let project = temp.path().join("project");
3659 fs::create_dir(&project).unwrap();
3660
3661 fs::write(
3662 project.join("main.rs"),
3663 "fn test() {}\nfn test_helper() {}\nfn other_test() {}",
3664 )
3665 .unwrap();
3666
3667 let cache = CacheManager::new(&project);
3668 let indexer = Indexer::new(cache, IndexConfig::default());
3669 indexer.index(&project, false).unwrap();
3670
3671 let cache = CacheManager::new(&project);
3672
3673 let engine = QueryEngine::new(cache);
3674
3675 let filter = QueryFilter {
3677 symbols_mode: true,
3678 exact: true,
3679 ..Default::default()
3680 };
3681 let results = engine.search("test", filter).unwrap();
3682
3683 assert_eq!(results.len(), 1);
3685 assert_eq!(results[0].symbol.as_deref(), Some("test"));
3686 }
3687
3688 #[test]
3691 fn test_expand_mode() {
3692 let temp = TempDir::new().unwrap();
3693 let project = temp.path().join("project");
3694 fs::create_dir(&project).unwrap();
3695
3696 fs::write(
3697 project.join("main.rs"),
3698 "fn greet() {\n println!(\"Hello\");\n println!(\"World\");\n}",
3699 )
3700 .unwrap();
3701
3702 let cache = CacheManager::new(&project);
3703 let indexer = Indexer::new(cache, IndexConfig::default());
3704 indexer.index(&project, false).unwrap();
3705
3706 let cache = CacheManager::new(&project);
3707
3708 let engine = QueryEngine::new(cache);
3709
3710 let filter = QueryFilter {
3712 symbols_mode: true,
3713 expand: true,
3714 ..Default::default()
3715 };
3716 let results = engine.search("greet", filter).unwrap();
3717
3718 assert!(!results.is_empty());
3720 let result = &results[0];
3721 assert!(result.preview.contains("println"));
3722 }
3723
3724 #[test]
3727 fn test_search_empty_index() {
3728 let temp = TempDir::new().unwrap();
3729 let project = temp.path().join("project");
3730 fs::create_dir(&project).unwrap();
3731
3732 let cache = CacheManager::new(&project);
3733 let indexer = Indexer::new(cache, IndexConfig::default());
3734 indexer.index(&project, false).unwrap();
3735
3736 let cache = CacheManager::new(&project);
3737
3738 let engine = QueryEngine::new(cache);
3739 let filter = QueryFilter::default();
3740 let results = engine.search("nonexistent", filter).unwrap();
3741
3742 assert_eq!(results.len(), 0);
3743 }
3744
3745 #[test]
3746 fn test_search_no_index() {
3747 let temp = TempDir::new().unwrap();
3748 let project = temp.path().join("project");
3749 fs::create_dir(&project).unwrap();
3750
3751 let cache = CacheManager::new(&project);
3752 let engine = QueryEngine::new(cache);
3753 let filter = QueryFilter::default();
3754
3755 assert!(engine.search("test", filter).is_err());
3757 }
3758
3759 #[test]
3760 fn test_search_special_characters() {
3761 let temp = TempDir::new().unwrap();
3762 let project = temp.path().join("project");
3763 fs::create_dir(&project).unwrap();
3764
3765 fs::write(project.join("main.rs"), "let x = 42;\nlet y = x + 1;").unwrap();
3766
3767 let cache = CacheManager::new(&project);
3768 let indexer = Indexer::new(cache, IndexConfig::default());
3769 indexer.index(&project, false).unwrap();
3770
3771 let cache = CacheManager::new(&project);
3772
3773 let engine = QueryEngine::new(cache);
3774 let filter = QueryFilter::default();
3775
3776 let results = engine.search("x + ", filter).unwrap();
3778 assert!(!results.is_empty());
3779 }
3780
3781 #[test]
3782 fn test_search_unicode() {
3783 let temp = TempDir::new().unwrap();
3784 let project = temp.path().join("project");
3785 fs::create_dir(&project).unwrap();
3786
3787 fs::write(project.join("main.rs"), "// 你好世界\nfn main() {}").unwrap();
3788
3789 let cache = CacheManager::new(&project);
3790 let indexer = Indexer::new(cache, IndexConfig::default());
3791 indexer.index(&project, false).unwrap();
3792
3793 let cache = CacheManager::new(&project);
3794
3795 let engine = QueryEngine::new(cache);
3796 let filter = QueryFilter {
3797 use_contains: true, force: true, ..Default::default()
3800 };
3801
3802 let results = engine.search("你好", filter).unwrap();
3804 assert!(!results.is_empty());
3805 }
3806
3807 #[test]
3808 fn test_case_sensitive_search() {
3809 let temp = TempDir::new().unwrap();
3810 let project = temp.path().join("project");
3811 fs::create_dir(&project).unwrap();
3812
3813 fs::write(project.join("main.rs"), "fn Test() {}\nfn test() {}").unwrap();
3814
3815 let cache = CacheManager::new(&project);
3816 let indexer = Indexer::new(cache, IndexConfig::default());
3817 indexer.index(&project, false).unwrap();
3818
3819 let cache = CacheManager::new(&project);
3820
3821 let engine = QueryEngine::new(cache);
3822 let filter = QueryFilter::default();
3823
3824 let results = engine.search("Test", filter).unwrap();
3826 assert!(results.iter().any(|r| r.preview.contains("Test()")));
3827 }
3828
3829 #[test]
3832 fn test_results_sorted_deterministically() {
3833 let temp = TempDir::new().unwrap();
3834 let project = temp.path().join("project");
3835 fs::create_dir(&project).unwrap();
3836
3837 fs::write(project.join("a.rs"), "fn test() {}").unwrap();
3838 fs::write(project.join("z.rs"), "fn test() {}").unwrap();
3839 fs::write(project.join("m.rs"), "fn test() {}\nfn test2() {}").unwrap();
3840
3841 let cache = CacheManager::new(&project);
3842 let indexer = Indexer::new(cache, IndexConfig::default());
3843 indexer.index(&project, false).unwrap();
3844
3845 let cache = CacheManager::new(&project);
3846
3847 let engine = QueryEngine::new(cache);
3848 let filter = QueryFilter::default();
3849
3850 let results1 = engine.search("test", filter.clone()).unwrap();
3852 let results2 = engine.search("test", filter.clone()).unwrap();
3853 let results3 = engine.search("test", filter).unwrap();
3854
3855 assert_eq!(results1.len(), results2.len());
3857 assert_eq!(results1.len(), results3.len());
3858
3859 for i in 0..results1.len() {
3860 assert_eq!(results1[i].path, results2[i].path);
3861 assert_eq!(results1[i].path, results3[i].path);
3862 assert_eq!(results1[i].span.start_line, results2[i].span.start_line);
3863 assert_eq!(results1[i].span.start_line, results3[i].span.start_line);
3864 }
3865
3866 for i in 0..results1.len().saturating_sub(1) {
3868 let curr = &results1[i];
3869 let next = &results1[i + 1];
3870 assert!(
3871 curr.path < next.path
3872 || (curr.path == next.path && curr.span.start_line <= next.span.start_line)
3873 );
3874 }
3875 }
3876
3877 #[test]
3880 fn test_multiple_filters_combined() {
3881 let temp = TempDir::new().unwrap();
3882 let project = temp.path().join("project");
3883 fs::create_dir_all(project.join("src")).unwrap();
3884
3885 fs::write(project.join("src/main.rs"), "fn test() {}\nstruct Test {}").unwrap();
3886 fs::write(project.join("src/lib.rs"), "fn test() {}").unwrap();
3887 fs::write(project.join("test.js"), "function test() {}").unwrap();
3888
3889 let cache = CacheManager::new(&project);
3890 let indexer = Indexer::new(cache, IndexConfig::default());
3891 indexer.index(&project, false).unwrap();
3892
3893 let cache = CacheManager::new(&project);
3894
3895 let engine = QueryEngine::new(cache);
3896
3897 let filter = QueryFilter {
3899 language: Some(Language::Rust),
3900 kind: Some(SymbolKind::Function),
3901 file_pattern: Some("src/main".to_string()),
3902 symbols_mode: true,
3903 ..Default::default()
3904 };
3905 let results = engine.search("test", filter).unwrap();
3906
3907 assert_eq!(results.len(), 1);
3909 assert!(results[0].path.contains("src/main.rs"));
3910 assert_eq!(results[0].kind, SymbolKind::Function);
3911 }
3912
3913 #[test]
3916 fn test_find_symbol_helper() {
3917 let temp = TempDir::new().unwrap();
3918 let project = temp.path().join("project");
3919 fs::create_dir(&project).unwrap();
3920
3921 fs::write(project.join("main.rs"), "fn greet() {}").unwrap();
3922
3923 let cache = CacheManager::new(&project);
3924 let indexer = Indexer::new(cache, IndexConfig::default());
3925 indexer.index(&project, false).unwrap();
3926
3927 let cache = CacheManager::new(&project);
3928
3929 let engine = QueryEngine::new(cache);
3930 let results = engine.find_symbol("greet").unwrap();
3931
3932 assert!(!results.is_empty());
3933 assert_eq!(results[0].kind, SymbolKind::Function);
3934 }
3935
3936 #[test]
3937 fn test_list_by_kind_helper() {
3938 let temp = TempDir::new().unwrap();
3939 let project = temp.path().join("project");
3940 fs::create_dir(&project).unwrap();
3941
3942 fs::write(
3943 project.join("main.rs"),
3944 "struct Point {}\nfn test() {}\nstruct Line {}",
3945 )
3946 .unwrap();
3947
3948 let cache = CacheManager::new(&project);
3949 let indexer = Indexer::new(cache, IndexConfig::default());
3950 indexer.index(&project, false).unwrap();
3951
3952 let cache = CacheManager::new(&project);
3953
3954 let engine = QueryEngine::new(cache);
3955
3956 let filter = QueryFilter {
3958 kind: Some(SymbolKind::Struct),
3959 symbols_mode: true,
3960 use_contains: true, ..Default::default()
3962 };
3963 let results = engine.search("oin", filter).unwrap();
3964
3965 assert!(!results.is_empty(), "Should find at least Point struct");
3967 assert!(results.iter().all(|r| r.kind == SymbolKind::Struct));
3968 assert!(results.iter().any(|r| r.symbol.as_deref() == Some("Point")));
3969 }
3970
3971 #[test]
3974 fn test_search_with_metadata() {
3975 let temp = TempDir::new().unwrap();
3976 let project = temp.path().join("project");
3977 fs::create_dir(&project).unwrap();
3978
3979 fs::write(project.join("main.rs"), "fn test() {}").unwrap();
3980
3981 let cache = CacheManager::new(&project);
3982 let indexer = Indexer::new(cache, IndexConfig::default());
3983 indexer.index(&project, false).unwrap();
3984
3985 let cache = CacheManager::new(&project);
3986
3987 let engine = QueryEngine::new(cache);
3988 let filter = QueryFilter::default();
3989 let response = engine.search_with_metadata("test", filter).unwrap();
3990
3991 assert!(!response.results.is_empty());
3993 }
3995
3996 #[test]
3999 fn test_search_across_languages() {
4000 let temp = TempDir::new().unwrap();
4001 let project = temp.path().join("project");
4002 fs::create_dir(&project).unwrap();
4003
4004 fs::write(project.join("main.rs"), "fn greet() {}").unwrap();
4005 fs::write(project.join("main.ts"), "function greet() {}").unwrap();
4006 fs::write(project.join("main.py"), "def greet(): pass").unwrap();
4007
4008 let cache = CacheManager::new(&project);
4009 let indexer = Indexer::new(cache, IndexConfig::default());
4010 indexer.index(&project, false).unwrap();
4011
4012 let cache = CacheManager::new(&project);
4013
4014 let engine = QueryEngine::new(cache);
4015 let filter = QueryFilter::default();
4016 let results = engine.search("greet", filter).unwrap();
4017
4018 assert!(results.len() >= 3);
4020 assert!(results.iter().any(|r| r.lang == Language::Rust));
4021 assert!(results.iter().any(|r| r.lang == Language::TypeScript));
4022 assert!(results.iter().any(|r| r.lang == Language::Python));
4023 }
4024}
4025
4026mod status_cache {
4042 use crate::cache::{CacheManager, FileFingerprint};
4043 use crate::git::WorktreeChanges;
4044 use crate::indexer::{Indexer, PathPolicy, looks_binary};
4045 use crate::models::{IndexConfig, IndexStatus, IndexWarning, IndexWarningDetails, Language};
4046 use anyhow::Result;
4047 use std::collections::{HashMap, HashSet};
4048 use std::path::{Path, PathBuf};
4049 use std::sync::{Arc, Mutex, OnceLock};
4050 use std::time::{Duration, Instant};
4051
4052 const DEFAULT_TTL_MS: u64 = 1_000;
4053
4054 pub type Verdict = (IndexStatus, bool, Option<IndexWarning>);
4055
4056 pub struct Snapshot {
4059 pub decided: Option<Verdict>,
4062 pub details: Option<IndexWarningDetails>,
4064 pub changes: WorktreeChanges,
4066 pub unreadable: bool,
4069 pub foreign: bool,
4071 pub rules_changed: bool,
4076 pub checked_at: Instant,
4079 }
4080
4081 impl Snapshot {
4082 fn unreadable() -> Self {
4083 Self {
4084 decided: Some((IndexStatus::Fresh, true, None)),
4085 details: None,
4086 changes: WorktreeChanges::default(),
4087 unreadable: true,
4088 foreign: false,
4089 rules_changed: false,
4090 checked_at: Instant::now(),
4091 }
4092 }
4093 }
4094
4095 struct Entry {
4096 computed_at: Instant,
4097 snapshot: Arc<Snapshot>,
4098 }
4099
4100 fn ttl() -> Duration {
4101 static TTL: OnceLock<Duration> = OnceLock::new();
4102 *TTL.get_or_init(|| {
4103 let ms = std::env::var("REFLEX_FRESHNESS_TTL_MS")
4104 .ok()
4105 .and_then(|v| v.trim().parse::<u64>().ok())
4106 .unwrap_or(DEFAULT_TTL_MS);
4107 Duration::from_millis(ms)
4108 })
4109 }
4110
4111 static EPOCH: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
4114
4115 fn store() -> &'static Mutex<HashMap<PathBuf, Entry>> {
4116 static STORE: OnceLock<Mutex<HashMap<PathBuf, Entry>>> = OnceLock::new();
4117 STORE.get_or_init(|| Mutex::new(HashMap::new()))
4118 }
4119
4120 fn key(root: &Path) -> PathBuf {
4121 root.canonicalize().unwrap_or_else(|_| root.to_path_buf())
4122 }
4123
4124 fn indexable_with(path: &str, policy: &crate::indexer::PathPolicy) -> bool {
4127 crate::indexer::Indexer::is_indexable_path_with(Path::new(path), Some(policy))
4128 }
4129
4130 pub fn snapshot(cache: &CacheManager) -> Result<Arc<Snapshot>> {
4132 let root = cache.workspace_root();
4133 let key = key(&root);
4134 let ttl = ttl();
4135
4136 if !ttl.is_zero()
4137 && let Ok(map) = store().lock()
4138 && let Some(entry) = map.get(&key)
4139 && entry.computed_at.elapsed() < ttl
4140 {
4141 return Ok(Arc::clone(&entry.snapshot));
4142 }
4143
4144 let epoch = EPOCH.load(std::sync::atomic::Ordering::Acquire);
4145 let snapshot = Arc::new(compute(cache, &root)?);
4146
4147 if !ttl.is_zero()
4148 && let Ok(mut map) = store().lock()
4149 && EPOCH.load(std::sync::atomic::Ordering::Acquire) == epoch
4150 {
4151 map.insert(
4152 key,
4153 Entry {
4154 computed_at: snapshot.checked_at,
4155 snapshot: Arc::clone(&snapshot),
4156 },
4157 );
4158 }
4159
4160 Ok(snapshot)
4161 }
4162
4163 enum Probe {
4165 Git(HashSet<String>),
4168 Walk,
4170 }
4171
4172 fn compute(cache: &CacheManager, root: &Path) -> Result<Snapshot> {
4182 let is_git = crate::git::is_git_repo(root) && crate::git::is_git_available();
4183 let current_branch = if is_git {
4184 crate::git::read_head_branch(root).or_else(|| crate::git::get_current_branch(root).ok())
4185 } else {
4186 None
4187 };
4188
4189 let reads = match cache.status_reads(current_branch.as_deref()) {
4191 Ok(r) => r,
4192 Err(e) => {
4193 log::debug!("Could not read index status from meta.db: {}", e);
4194 return Ok(Snapshot::unreadable());
4195 }
4196 };
4197
4198 if !reads.schema_ok {
4203 let foreign = reads
4204 .owner
4205 .as_ref()
4206 .is_some_and(|(v, _)| v != env!("CARGO_PKG_VERSION"));
4207 let reason = match reads.owner {
4208 Some((v, sha)) if v != env!("CARGO_PKG_VERSION") => {
4209 let sha = sha
4210 .map(|s| format!(" (sha {})", &s[..s.len().min(7)]))
4211 .unwrap_or_default();
4212 format!(
4213 "This .reflex/ was written by reflex {}{}; this binary is reflex {}. \
4214 Results come from a cache format this version does not fully \
4215 understand.",
4216 v,
4217 sha,
4218 env!("CARGO_PKG_VERSION")
4219 )
4220 }
4221 _ => "The index was built with a different cache format and needs \
4224 rebuilding. Results may be incomplete until then."
4225 .to_string(),
4226 };
4227
4228 let warning = IndexWarning::new(reason, "index_project");
4229 return Ok(Snapshot {
4230 decided: Some((IndexStatus::Stale, false, Some(warning))),
4231 details: None,
4232 changes: WorktreeChanges::default(),
4233 unreadable: false,
4234 foreign,
4235 rules_changed: false,
4236 checked_at: Instant::now(),
4237 });
4238 }
4239
4240 let config = cache.load_index_config().unwrap_or_else(|e| {
4241 log::debug!("Using default index config for freshness: {}", e);
4242 IndexConfig::default()
4243 });
4244 let policy = PathPolicy::from_config(root, &config);
4245
4246 let info = reads.branch_info.as_ref();
4247 let mut details = IndexWarningDetails {
4248 current_branch: current_branch.clone(),
4249 indexed_branch: info.map(|i| i.branch.clone()),
4250 current_commit: None,
4251 indexed_commit: info.map(|i| i.commit_sha.clone()),
4252 indexed_at: info.map(|i| i.last_indexed),
4253 checked_by: None,
4254 };
4255
4256 let probe = if current_branch.is_some() {
4257 let current_commit = crate::git::get_current_commit(root).ok();
4258 details.current_commit = current_commit.clone();
4259 let indexed_commit = info.map(|i| i.commit_sha.as_str());
4260 match git_candidates(
4261 root,
4262 &reads.dirty_at_index,
4263 indexed_commit,
4264 current_commit.as_deref(),
4265 ) {
4266 Ok(set) => Probe::Git(set),
4267 Err(e) => {
4268 log::debug!("git could not name changed paths ({}); walking the tree", e);
4269 Probe::Walk
4270 }
4271 }
4272 } else {
4273 Probe::Walk
4274 };
4275
4276 let rule_record: Option<std::collections::BTreeMap<String, String>> = reads
4277 .rule_files
4278 .as_deref()
4279 .and_then(|r| serde_json::from_str(r).ok());
4280 let mut rules_changed: Vec<String> = Vec::new();
4281
4282 let mut changes = match probe {
4283 Probe::Git(candidates) => {
4284 let keys: Vec<&str> = candidates.iter().map(String::as_str).collect();
4285 let fingerprints = cache.fingerprints_for(&keys)?;
4286 let mut out = WorktreeChanges::default();
4287 let mut added = Vec::new();
4288 for path in &candidates {
4289 if let Some(record) = &rule_record
4290 && crate::indexer::is_rule_file(path)
4291 && !crate::indexer::FIXED_RULE_FILES.contains(&path.as_str())
4292 && record.get(path).map(String::as_str)
4293 != Some(crate::indexer::rule_file_hash(root, path).as_str())
4294 {
4295 rules_changed.push(path.clone());
4296 continue;
4297 }
4298 classify_one(
4299 root,
4300 &config,
4301 &policy,
4302 path,
4303 fingerprints.get(path),
4304 &mut out,
4305 &mut added,
4306 );
4307 }
4308 if !added.is_empty() {
4312 let walked = Indexer::walked_among(root, &config, &policy, &added);
4313 for path in added.iter().filter(|p| walked.contains(*p)) {
4314 out.push_added(path);
4315 }
4316 }
4317 details.checked_by = Some("git".to_string());
4318 out
4319 }
4320 Probe::Walk => {
4321 let fingerprints = fingerprints_all(cache)?;
4322 details.checked_by = Some("walk".to_string());
4323 walk_changes(root, &config, &policy, &fingerprints)
4324 }
4325 };
4326 if let Some(record) = &rule_record {
4327 for rel in crate::indexer::FIXED_RULE_FILES {
4328 let now = crate::indexer::rule_file_hash(root, rel);
4329 if record.get(rel).map(String::as_str).unwrap_or("") != now {
4330 rules_changed.push(rel.to_string());
4331 }
4332 }
4333 }
4334 for rel in &rules_changed {
4335 changes.push_modified(rel);
4336 }
4337 changes.sort();
4338
4339 Ok(Snapshot {
4340 decided: None,
4341 details: Some(details),
4342 changes,
4343 unreadable: false,
4344 foreign: false,
4345 rules_changed: !rules_changed.is_empty(),
4346 checked_at: Instant::now(),
4347 })
4348 }
4349
4350 fn git_candidates(
4362 root: &Path,
4363 dirty_at_index: &[String],
4364 indexed_commit: Option<&str>,
4365 current_commit: Option<&str>,
4366 ) -> Result<HashSet<String>> {
4367 let mut set = crate::git::changed_paths(root)?;
4368 set.extend(dirty_at_index.iter().cloned());
4369 match (indexed_commit, current_commit) {
4370 (Some(indexed), Some(current)) if indexed != current => {
4371 if indexed == "unknown" {
4372 anyhow::bail!("the index does not record a commit");
4373 }
4374 set.extend(crate::git::diff_names(root, indexed, current)?);
4375 }
4376 (None, _) | (_, None) => anyhow::bail!("no commit to diff against"),
4377 _ => {}
4378 }
4379 Ok(set)
4380 }
4381
4382 fn fingerprints_all(cache: &CacheManager) -> Result<Arc<HashMap<String, FileFingerprint>>> {
4385 match super::open_index::get_or_open(cache) {
4386 Ok(open) => open.fingerprints(cache),
4387 Err(_) => Ok(Arc::new(cache.load_fingerprints()?)),
4388 }
4389 }
4390
4391 fn content_matches(full: &Path, md: &std::fs::Metadata, fp: &FileFingerprint) -> bool {
4397 if fp.stat_matches(md) {
4398 return true;
4399 }
4400 match std::fs::read(full) {
4401 Ok(bytes) => blake3::hash(&bytes).to_hex().as_str() == fp.hash,
4402 Err(_) => false,
4403 }
4404 }
4405
4406 fn classify_one(
4408 root: &Path,
4409 config: &IndexConfig,
4410 policy: &PathPolicy,
4411 path: &str,
4412 fp: Option<&FileFingerprint>,
4413 out: &mut WorktreeChanges,
4414 added: &mut Vec<String>,
4415 ) {
4416 if !indexable_with(path, policy) {
4417 return;
4418 }
4419 let full = root.join(path);
4420 let md = if config.follow_symlinks {
4421 std::fs::metadata(&full)
4422 } else {
4423 std::fs::symlink_metadata(&full)
4424 };
4425 match (md, fp) {
4426 (Err(_), Some(_)) => out.push_deleted(path),
4427 (Err(_), None) => {}
4428 (Ok(md), _) if !md.is_file() => {
4430 if fp.is_some() {
4431 out.push_deleted(path);
4432 }
4433 }
4434 (Ok(md), _) if md.len() > config.max_file_size as u64 => {
4435 if fp.is_some() {
4438 out.push_modified(path);
4439 }
4440 }
4441 (Ok(_), None) => {
4444 if !Language::from_path(Path::new(path)).is_code() && looks_binary(&full) {
4445 return;
4446 }
4447 added.push(path.to_string());
4448 }
4449 (Ok(md), Some(fp)) => {
4450 if !content_matches(&full, &md, fp) {
4451 out.push_modified(path);
4452 }
4453 }
4454 }
4455 }
4456
4457 fn walk_changes(
4463 root: &Path,
4464 config: &IndexConfig,
4465 policy: &PathPolicy,
4466 fingerprints: &HashMap<String, FileFingerprint>,
4467 ) -> WorktreeChanges {
4468 let threads = std::thread::available_parallelism()
4469 .map(|n| n.get())
4470 .unwrap_or(1)
4471 .min(8);
4472 let mut builder = Indexer::walk_builder(root, config, policy);
4473 builder.threads(threads);
4474
4475 let seen: Mutex<Vec<(String, std::fs::Metadata)>> = Mutex::new(Vec::new());
4476 let max_size = config.max_file_size as u64;
4477 builder.build_parallel().run(|| {
4478 let seen = &seen;
4479 Box::new(move |entry| {
4480 let Ok(entry) = entry else {
4481 return ignore::WalkState::Continue;
4482 };
4483 if !entry.file_type().map(|t| t.is_file()).unwrap_or(false) {
4484 return ignore::WalkState::Continue;
4485 }
4486 let path = entry.path();
4487 if policy.classify(path).is_none() {
4488 return ignore::WalkState::Continue;
4489 }
4490 let Ok(md) = entry.metadata() else {
4491 return ignore::WalkState::Continue;
4492 };
4493 if md.len() > max_size {
4494 return ignore::WalkState::Continue;
4495 }
4496 let rel = path
4497 .strip_prefix(root)
4498 .unwrap_or(path)
4499 .to_string_lossy()
4500 .replace('\\', "/");
4501 if let Ok(mut v) = seen.lock() {
4502 v.push((rel, md));
4503 }
4504 ignore::WalkState::Continue
4505 })
4506 });
4507
4508 let seen = seen.into_inner().unwrap_or_default();
4509 let mut out = WorktreeChanges::default();
4510 let mut present: HashSet<&str> = HashSet::with_capacity(seen.len());
4511 for (rel, md) in &seen {
4512 present.insert(rel.as_str());
4513 match fingerprints.get(rel) {
4514 None => {
4515 let full = root.join(rel);
4516 if !Language::from_path(&full).is_code() && looks_binary(&full) {
4517 continue;
4518 }
4519 out.push_added(rel);
4520 }
4521 Some(fp) => {
4522 if !content_matches(&root.join(rel), md, fp) {
4523 out.push_modified(rel);
4524 }
4525 }
4526 }
4527 }
4528 for path in fingerprints.keys() {
4529 if !present.contains(path.as_str()) {
4530 out.push_deleted(path);
4531 }
4532 }
4533 out
4534 }
4535
4536 pub fn update_plan(cache: &CacheManager) -> Result<(super::UpdatePlan, Arc<Snapshot>)> {
4538 let snap = snapshot(cache)?;
4539 Ok((plan_of(&snap), snap))
4540 }
4541
4542 fn plan_of(snap: &Snapshot) -> super::UpdatePlan {
4543 use super::UpdatePlan;
4544 if snap.unreadable {
4545 return UpdatePlan::Unknown(
4546 "the index metadata (meta.db) could not be read".to_string(),
4547 );
4548 }
4549 if let Some((status, _, warning)) = &snap.decided {
4550 if *status == IndexStatus::Fresh {
4551 return UpdatePlan::Fresh;
4552 }
4553 let reason = warning
4554 .as_ref()
4555 .map(|w| w.reason.clone())
4556 .unwrap_or_default();
4557 return if snap.foreign {
4558 UpdatePlan::Foreign(reason)
4559 } else {
4560 UpdatePlan::Full(Vec::new())
4561 };
4562 }
4563 let c = &snap.changes;
4564 if c.is_empty() {
4565 return UpdatePlan::Fresh;
4566 }
4567 let mut paths: Vec<PathBuf> = c
4568 .modified
4569 .iter()
4570 .chain(&c.added)
4571 .chain(&c.deleted)
4572 .map(PathBuf::from)
4573 .collect();
4574 paths.sort();
4575 paths.dedup();
4576 if snap.rules_changed || c.truncated {
4577 UpdatePlan::Full(paths)
4578 } else {
4579 UpdatePlan::Paths(paths)
4580 }
4581 }
4582
4583 pub fn settle(cache: &CacheManager, prior: &Snapshot, paths: &[PathBuf]) -> bool {
4590 let ttl = ttl();
4591 if ttl.is_zero()
4592 || prior.checked_at.elapsed() >= ttl
4593 || prior.decided.is_some()
4594 || prior.rules_changed
4595 || prior.changes.truncated
4596 {
4597 return false;
4598 }
4599 let root = cache.workspace_root();
4600 let Ok(config) = cache.load_index_config() else {
4601 return false;
4602 };
4603 let policy = PathPolicy::from_config(&root, &config);
4604 let rels: Vec<String> = paths
4605 .iter()
4606 .map(|p| p.to_string_lossy().replace('\\', "/"))
4607 .collect();
4608 let keys: Vec<&str> = rels.iter().map(String::as_str).collect();
4609 let Ok(fingerprints) = cache.fingerprints_for(&keys) else {
4610 return false;
4611 };
4612 let mut still = WorktreeChanges::default();
4613 let mut added = Vec::new();
4614 for rel in &rels {
4615 classify_one(
4616 &root,
4617 &config,
4618 &policy,
4619 rel,
4620 fingerprints.get(rel),
4621 &mut still,
4622 &mut added,
4623 );
4624 }
4625 if !still.is_empty() || !added.is_empty() {
4626 return false;
4627 }
4628 let settled = Arc::new(Snapshot {
4629 decided: None,
4630 details: prior.details.clone(),
4631 changes: WorktreeChanges::default(),
4632 unreadable: false,
4633 foreign: false,
4634 rules_changed: false,
4635 checked_at: prior.checked_at,
4636 });
4637 match store().lock() {
4638 Ok(mut map) => {
4639 map.insert(
4640 key(&root),
4641 Entry {
4642 computed_at: prior.checked_at,
4643 snapshot: settled,
4644 },
4645 );
4646 true
4647 }
4648 Err(_) => false,
4649 }
4650 }
4651
4652 pub fn invalidate(root: &Path) {
4658 let key = key(root);
4659 if let Ok(mut map) = store().lock() {
4660 EPOCH.fetch_add(1, std::sync::atomic::Ordering::AcqRel);
4661 map.remove(&key);
4662 }
4663 }
4664}