sqry-core 6.0.12

Core library for sqry - semantic code search engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Fallback search engine combining semantic (AST) and text (ripgrep) search
//!
//! This module implements intelligent query execution that automatically falls back
//! to text search when semantic search returns insufficient results.
//!
//! Note: This is distinct from the embedding-based hybrid search,
//! which combines vectors + AST/graph. This module handles AST → ripgrep fallback.

use super::classifier::{QueryClassifier, QueryType};
use super::{Match as TextMatch, SearchConfig, SearchMode, Searcher as TextSearcher};
use crate::query::QueryExecutor;
use crate::query::results::QueryResults;
use anyhow::{Context, Error, Result, anyhow};
use log::error;
use std::path::Path;

/// Configuration for fallback search behavior
#[derive(Debug, Clone)]
pub struct FallbackConfig {
    /// Enable automatic fallback to text search (default: true)
    pub fallback_enabled: bool,

    /// Minimum semantic results before fallback (default: 1)
    /// If semantic search returns fewer than this, fallback to text
    pub min_semantic_results: usize,

    /// Context lines for text search results (default: 2)
    pub text_context_lines: usize,

    /// Maximum text search results (default: 1000)
    pub max_text_results: usize,

    /// Show which search mode was used (default: true)
    pub show_search_mode: bool,
}

impl Default for FallbackConfig {
    fn default() -> Self {
        Self {
            fallback_enabled: true,
            min_semantic_results: 1,
            text_context_lines: 2,
            max_text_results: 1000,
            show_search_mode: true,
        }
    }
}

impl FallbackConfig {
    /// Load configuration from environment variables
    ///
    /// Supported environment variables:
    /// - `SQRY_FALLBACK_ENABLED`: Enable/disable fallback (true/false)
    /// - `SQRY_MIN_SEMANTIC_RESULTS`: Minimum semantic results threshold
    /// - `SQRY_TEXT_CONTEXT_LINES`: Context lines for text results
    /// - `SQRY_MAX_TEXT_RESULTS`: Maximum text results
    /// - `SQRY_SHOW_SEARCH_MODE`: Show search mode (true/false)
    #[must_use]
    pub fn from_env() -> Self {
        let mut config = Self::default();

        if let Ok(val) = std::env::var("SQRY_FALLBACK_ENABLED") {
            config.fallback_enabled = val.parse().unwrap_or(true);
        }

        if let Ok(val) = std::env::var("SQRY_MIN_SEMANTIC_RESULTS") {
            config.min_semantic_results = val.parse().unwrap_or(1);
        }

        if let Ok(val) = std::env::var("SQRY_TEXT_CONTEXT_LINES") {
            config.text_context_lines = val.parse().unwrap_or(2);
        }

        if let Ok(val) = std::env::var("SQRY_MAX_TEXT_RESULTS") {
            config.max_text_results = val.parse().unwrap_or(1000);
        }

        if let Ok(val) = std::env::var("SQRY_SHOW_SEARCH_MODE") {
            config.show_search_mode = val.parse().unwrap_or(true);
        }

        config
    }
}

/// Search results from hybrid engine
#[derive(Debug)]
pub enum SearchResults {
    /// Semantic (CodeGraph-based) results
    Semantic {
        /// Results from semantic search
        results: QueryResults,
        /// Which search mode was used
        mode: SearchModeUsed,
    },

    /// Pure text (regex-based) results
    Text {
        /// Text matches found by ripgrep search
        matches: Vec<TextMatch>,
        /// Which search mode was used
        mode: SearchModeUsed,
    },
}

/// Which search mode was actually used
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchModeUsed {
    /// Semantic search only
    SemanticOnly,

    /// Text search only
    TextOnly,

    /// Semantic succeeded (no fallback needed)
    SemanticSucceeded,

    /// Semantic returned empty, fell back to text
    SemanticFallbackToText,

    /// Both semantic and text (combined)
    Combined,
}

impl SearchResults {
    /// Get the total number of results
    #[must_use]
    pub fn len(&self) -> usize {
        match self {
            SearchResults::Semantic { results, .. } => results.len(),
            SearchResults::Text { matches, .. } => matches.len(),
        }
    }

    /// Check if results are empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the search mode that was used
    #[must_use]
    pub fn mode(&self) -> SearchModeUsed {
        match self {
            SearchResults::Semantic { mode, .. } | SearchResults::Text { mode, .. } => *mode,
        }
    }
}

/// Fallback search engine combining semantic and text search
pub struct FallbackSearchEngine {
    /// Semantic query executor (AST-based)
    query_executor: QueryExecutor,

    /// Text searcher (ripgrep-based)
    text_searcher: Option<TextSearcher>,

    /// Reason text searcher initialization failed (if unavailable)
    text_search_error: Option<String>,

    /// Configuration
    config: FallbackConfig,
}

impl FallbackSearchEngine {
    fn from_parts(
        query_executor: QueryExecutor,
        text_searcher: Option<TextSearcher>,
        text_search_error: Option<String>,
        config: FallbackConfig,
    ) -> Self {
        Self {
            query_executor,
            text_searcher,
            text_search_error,
            config,
        }
    }

    fn without_text_search(
        query_executor: QueryExecutor,
        config: FallbackConfig,
        error: &Error,
    ) -> Self {
        Self::from_parts(query_executor, None, Some(format!("{error:#}")), config)
    }

    fn text_searcher(&self) -> Result<&TextSearcher> {
        self.text_searcher.as_ref().ok_or_else(|| {
            let reason = self
                .text_search_error
                .as_deref()
                .unwrap_or("text searcher initialization failed");
            anyhow!("Text search is unavailable ({reason})")
        })
    }

    /// Create a new fallback search engine with default configuration
    ///
    /// # Errors
    ///
    /// Returns [`anyhow::Error`] if the underlying [`FallbackSearchEngine::with_config`] call
    /// fails to construct a text searcher.
    pub fn new() -> Result<Self> {
        Self::with_config(FallbackConfig::default())
    }

    /// Create a fallback search engine with custom configuration
    ///
    /// **Note:** This creates a `QueryExecutor` without plugins, so metadata field queries
    /// like `async:true` and `visibility:public` will fail with "unknown field" errors.
    /// For metadata field support, use [`with_config_and_executor`](Self::with_config_and_executor)
    /// with a plugin-enabled executor.
    ///
    /// # Errors
    ///
    /// Returns [`anyhow::Error`] if either the `QueryExecutor` or the text searcher cannot
    /// be initialised.
    pub fn with_config(config: FallbackConfig) -> Result<Self> {
        Self::with_config_and_executor(config, QueryExecutor::new())
    }

    /// Create fallback search engine with custom query executor (with plugins)
    ///
    /// This constructor allows passing a `QueryExecutor` that has plugin fields registered,
    /// enabling metadata queries like `async:true` and `visibility:public` in hybrid mode.
    ///
    /// # Arguments
    /// * `config` - Hybrid search configuration
    /// * `query_executor` - Pre-configured `QueryExecutor` with plugin fields
    ///
    /// # Example
    /// ```no_run
    /// use sqry_core::search::fallback::{FallbackSearchEngine, FallbackConfig};
    /// use sqry_core::query::QueryExecutor;
    /// use sqry_core::plugin::PluginManager;
    ///
    /// // Create plugin manager and register built-in plugins
    /// let mut plugin_manager = PluginManager::new();
    /// // Register plugins as needed (e.g., in CLI):
    /// // plugin_manager.register_builtin(Box::new(sqry_lang_rust::RustPlugin::default()));
    /// // plugin_manager.register_builtin(Box::new(sqry_lang_python::PythonPlugin::default()));
    /// // ... register other plugins
    ///
    /// let query_executor = QueryExecutor::with_plugin_manager(plugin_manager);
    /// let config = FallbackConfig::default();
    /// let engine = FallbackSearchEngine::with_config_and_executor(config, query_executor);
    /// ```
    /// # Errors
    ///
    /// Returns [`anyhow::Error`] when the ripgrep-based text searcher fails to initialise.
    pub fn with_config_and_executor(
        config: FallbackConfig,
        query_executor: QueryExecutor,
    ) -> Result<Self> {
        let text_searcher =
            TextSearcher::new().context("Failed to create text searcher for hybrid engine")?;

        Ok(Self::from_parts(
            query_executor,
            Some(text_searcher),
            None,
            config,
        ))
    }

    /// Search with automatic mode detection and fallback
    ///
    /// # Arguments
    /// * `query` - The search query
    /// * `path` - The path to search in
    ///
    /// # Returns
    /// `SearchResults` with the mode used and matched symbols/text
    ///
    /// # Example
    /// ```no_run
    /// use sqry_core::search::fallback::FallbackSearchEngine;
    /// use std::path::Path;
    ///
    /// let mut engine = FallbackSearchEngine::new().unwrap();
    /// // Search for functions in current directory
    /// let results = engine.search("kind:function", Path::new("."));
    /// ```
    /// # Errors
    ///
    /// Returns [`anyhow::Error`] if either semantic or text search fails and no fallback
    /// mode can recover (for example, when both engines encounter I/O errors).
    pub fn search(&mut self, query: &str, path: &Path) -> Result<SearchResults> {
        // Step 1: Classify query
        let query_type = QueryClassifier::classify(query);

        if self.config.show_search_mode {
            match query_type {
                QueryType::Semantic => log::debug!("[Semantic search mode]"),
                QueryType::Text => log::debug!("[Text search mode]"),
                QueryType::Hybrid => log::debug!("[Hybrid mode: trying semantic first...]"),
            }
        }

        // Step 2: Execute based on classification
        match query_type {
            QueryType::Semantic => self.search_semantic_only(query, path),
            QueryType::Text => self.search_text_only(query, path),
            QueryType::Hybrid => self.search_hybrid(query, path),
        }
    }

    /// Force semantic search only (no fallback)
    ///
    /// # Errors
    ///
    /// Returns [`anyhow::Error`] when the semantic query executor fails to parse or execute
    /// the request (invalid syntax, missing graph, or predicate errors).
    pub fn search_semantic_only(&mut self, query: &str, path: &Path) -> Result<SearchResults> {
        // Execute query using CodeGraph
        let results = self.query_executor.execute_on_graph(query, path)?;

        Ok(SearchResults::Semantic {
            results,
            mode: SearchModeUsed::SemanticOnly,
        })
    }

    /// Force text search only (no semantic attempt)
    ///
    /// # Errors
    ///
    /// Returns [`anyhow::Error`] when text search is disabled/unavailable or when ripgrep
    /// returns an error while scanning the requested paths.
    pub fn search_text_only(&mut self, query: &str, path: &Path) -> Result<SearchResults> {
        let config = SearchConfig {
            mode: SearchMode::Regex,
            case_insensitive: false,
            include_hidden: false,
            follow_symlinks: false,
            max_depth: None,
            file_types: Vec::new(),
            exclude_patterns: Vec::new(),
            before_context: self.config.text_context_lines,
            after_context: self.config.text_context_lines,
        };

        let searcher = self
            .text_searcher()
            .context("Text search unavailable in hybrid engine")?;

        let matches = searcher
            .search(query, &[path], &config)
            .context("Text search failed")?;

        // Limit results
        let matches = matches
            .into_iter()
            .take(self.config.max_text_results)
            .collect();

        Ok(SearchResults::Text {
            matches,
            mode: SearchModeUsed::TextOnly,
        })
    }

    /// Hybrid search: try semantic first, fallback to text if needed
    fn search_hybrid(&mut self, query: &str, path: &Path) -> Result<SearchResults> {
        // Try semantic search first using CodeGraph
        let semantic_result = self.query_executor.execute_on_graph(query, path);

        match semantic_result {
            Ok(results) if results.len() >= self.config.min_semantic_results => {
                // Semantic search succeeded with sufficient results
                if self.config.show_search_mode {
                    log::debug!("[Semantic search: {} results]", results.len());
                }

                Ok(SearchResults::Semantic {
                    results,
                    mode: SearchModeUsed::SemanticSucceeded,
                })
            }

            Ok(results) if self.config.fallback_enabled => {
                // Semantic returned too few results - fallback to text
                if self.config.show_search_mode {
                    log::debug!(
                        "[Semantic search: {} results (below threshold {})]",
                        results.len(),
                        self.config.min_semantic_results
                    );
                    log::debug!("[Falling back to text search...]");
                }

                let config = SearchConfig {
                    mode: SearchMode::Regex,
                    case_insensitive: false,
                    include_hidden: false,
                    follow_symlinks: false,
                    max_depth: None,
                    file_types: Vec::new(),
                    exclude_patterns: Vec::new(),
                    before_context: self.config.text_context_lines,
                    after_context: self.config.text_context_lines,
                };

                let searcher = self
                    .text_searcher()
                    .context("Text search unavailable during fallback")?;
                let matches = searcher
                    .search(query, &[path], &config)
                    .context("Text search failed during fallback")?;

                let matches = matches
                    .into_iter()
                    .take(self.config.max_text_results)
                    .collect::<Vec<_>>();

                if self.config.show_search_mode {
                    log::debug!("[Text search: {} results]", matches.len());
                }

                Ok(SearchResults::Text {
                    matches,
                    mode: SearchModeUsed::SemanticFallbackToText,
                })
            }

            Ok(results) => {
                // Fallback disabled, return semantic results as-is
                Ok(SearchResults::Semantic {
                    results,
                    mode: SearchModeUsed::SemanticOnly,
                })
            }

            Err(e) if self.config.fallback_enabled => {
                // Semantic search failed - fallback to text
                if self.config.show_search_mode {
                    log::debug!("[Semantic search failed: {e}]");
                    log::debug!("[Falling back to text search...]");
                }

                let config = SearchConfig {
                    mode: SearchMode::Regex,
                    case_insensitive: false,
                    include_hidden: false,
                    follow_symlinks: false,
                    max_depth: None,
                    file_types: Vec::new(),
                    exclude_patterns: Vec::new(),
                    before_context: self.config.text_context_lines,
                    after_context: self.config.text_context_lines,
                };

                let searcher = self
                    .text_searcher()
                    .context("Text search unavailable during fallback")?;
                let matches = searcher
                    .search(query, &[path], &config)
                    .context("Text search failed during fallback")?;

                let matches = matches
                    .into_iter()
                    .take(self.config.max_text_results)
                    .collect();

                Ok(SearchResults::Text {
                    matches,
                    mode: SearchModeUsed::SemanticFallbackToText,
                })
            }

            Err(e) => {
                // Fallback disabled and semantic failed - return error
                Err(e)
            }
        }
    }
}

impl Default for FallbackSearchEngine {
    fn default() -> Self {
        let config = FallbackConfig::default();
        let query_executor = QueryExecutor::new();
        match TextSearcher::new() {
            Ok(searcher) => Self::from_parts(query_executor, Some(searcher), None, config),
            Err(err) => {
                error!(
                    "FallbackSearchEngine default initialization failed; text search disabled: {err:#}"
                );
                Self::without_text_search(query_executor, config, &err)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    fn setup_test_dir() -> TempDir {
        let dir = TempDir::new().unwrap();
        let test_file = dir.path().join("test.rs");
        fs::write(
            &test_file,
            r#"
pub fn foo() {
    // TODO: add more functionality
    println!("hello");
}

// bar intentionally simplified for fixture use
fn bar() {
    // Stubbed for test fixture coverage only
}
"#,
        )
        .unwrap();

        dir
    }

    #[test]
    fn test_hybrid_config_default() {
        let config = FallbackConfig::default();
        assert!(config.fallback_enabled);
        assert_eq!(config.min_semantic_results, 1);
        assert_eq!(config.text_context_lines, 2);
        assert_eq!(config.max_text_results, 1000);
    }

    #[test]
    fn test_hybrid_config_from_env() {
        unsafe {
            std::env::set_var("SQRY_FALLBACK_ENABLED", "false");
            std::env::set_var("SQRY_MIN_SEMANTIC_RESULTS", "5");
            std::env::set_var("SQRY_TEXT_CONTEXT_LINES", "3");
        }

        let config = FallbackConfig::from_env();
        assert!(!config.fallback_enabled);
        assert_eq!(config.min_semantic_results, 5);
        assert_eq!(config.text_context_lines, 3);

        // Cleanup
        unsafe {
            std::env::remove_var("SQRY_FALLBACK_ENABLED");
            std::env::remove_var("SQRY_MIN_SEMANTIC_RESULTS");
            std::env::remove_var("SQRY_TEXT_CONTEXT_LINES");
        }
    }

    #[test]
    fn test_search_text_only() {
        let dir = setup_test_dir();
        let mut engine = FallbackSearchEngine::new().unwrap();

        let results = engine.search_text_only("TODO", dir.path()).unwrap();

        match results {
            SearchResults::Text { matches, mode } => {
                assert!(!matches.is_empty());
                assert_eq!(mode, SearchModeUsed::TextOnly);
            }
            _ => panic!("Expected Text results"),
        }
    }

    #[test]
    fn test_search_results_len() {
        let dir = setup_test_dir();
        let mut engine = FallbackSearchEngine::new().unwrap();

        let results = engine.search_text_only("TODO", dir.path()).unwrap();
        assert!(!results.is_empty());
    }

    #[test]
    fn test_hybrid_fallback_disabled() {
        let dir = setup_test_dir();
        let config = FallbackConfig {
            fallback_enabled: false,
            ..Default::default()
        };

        let mut engine = FallbackSearchEngine::with_config(config).unwrap();

        // This should fail with fallback disabled (no semantic match for "TODO")
        let result = engine.search_hybrid("nonexistent", dir.path());

        // Should return empty semantic results (not fallback to text)
        if let Ok(SearchResults::Semantic { results, .. }) = result {
            assert_eq!(results.len(), 0);
        }
    }
}