vtcode-core 0.136.3

Core library for VT Code - a Rust-based terminal coding agent
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
//! Helper that owns the debounce/cancellation logic for `grep_file` operations.
//!
//! This module manages the orchestration of ripgrep searches, implementing
//! debounce and cancellation logic to ensure responsive and efficient searches.
//!
//! It works as follows:
//! 1. First query starts a debounce timer.
//! 2. While the timer is pending, the latest query from the user is stored.
//! 3. When the timer fires, it is cleared, and a search is done for the most
//!    recent query.
//! 4. If there is an in-flight search that is not a prefix of the latest thing
//!    the user typed, it is cancelled.

use super::file_search_bridge::{self, FileSearchConfig};
use super::grep_cache::GrepSearchCache;
use crate::cache::estimate_json_size;
use anyhow::{Context, Result};
use serde_json::{self, Value};
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::OnceLock;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::thread;
use std::time::Duration;
use tokio::io::{AsyncBufRead, AsyncBufReadExt, BufReader};
use tokio::process::Command as TokioCommand;
use tokio::task::spawn_blocking;
use tracing::warn;

/// Maximum number of search results to return - AGENTS.md requires max 5 results
const MAX_SEARCH_RESULTS: NonZeroUsize = NonZeroUsize::new(5).expect("5 is non-zero");

/// Optimal number of threads for searching, calculated based on CPU count
static OPTIMAL_SEARCH_THREADS: OnceLock<NonZeroUsize> = OnceLock::new();

/// Calculate optimal number of search threads based on available CPU cores
/// Uses 75% of cores, clamped between 2 and 8 threads
fn optimal_search_threads() -> NonZeroUsize {
    *OPTIMAL_SEARCH_THREADS.get_or_init(|| {
        let cpu_count = num_cpus::get();
        // Use 75% of cores for better parallelism, min 2, max 8
        let threads = (cpu_count * 3 / 4).clamp(2, 8);
        NonZeroUsize::new(threads).unwrap_or(NonZeroUsize::new(2).expect("2 is non-zero"))
    })
}

/// Maximum bytes to keep in a single grep response before truncation.
const DEFAULT_MAX_RESULT_BYTES: usize = 32 * 1024;

/// Default timeout for blocking grep invocations.
const DEFAULT_SEARCH_TIMEOUT: Duration = Duration::from_secs(5);

use vtcode_commons::exclusions::DEFAULT_IGNORE_GLOBS;

/// How long to wait after a keystroke before firing the first search when none
/// is currently running. Keeps early queries more meaningful.
const SEARCH_DEBOUNCE: Duration = Duration::from_millis(150);

/// Poll interval when waiting for an active search to complete
const ACTIVE_SEARCH_COMPLETE_POLL_INTERVAL: Duration = Duration::from_millis(20);

use serde::{Deserialize, Serialize};

use crate::tools::ast_grep_language::AstGrepLanguage;

pub(crate) const CODE_SEARCH_STREAM_BYTE_CAP: usize = 1024 * 1024;

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct LiteralSearchCandidate {
    pub path: PathBuf,
    pub line: usize,
    pub column: usize,
    pub byte_start: usize,
    pub byte_end: usize,
    pub matched_text: String,
    pub snippet: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct LiteralSearchOutcome {
    pub candidates: Vec<LiteralSearchCandidate>,
    pub truncated: bool,
}

async fn kill_and_reap_literal_child(child: &mut tokio::process::Child) {
    let _ = child.start_kill();
    let _ = child.wait().await;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BoundedRecordRead {
    Record,
    Eof,
    Exhausted,
}

async fn read_bounded_record<R: AsyncBufRead + Unpin>(
    reader: &mut R,
    record: &mut Vec<u8>,
    bytes_read: &mut usize,
    byte_cap: usize,
) -> std::io::Result<BoundedRecordRead> {
    loop {
        let available = reader.fill_buf().await?;
        if available.is_empty() {
            return Ok(if record.is_empty() {
                BoundedRecordRead::Eof
            } else {
                BoundedRecordRead::Record
            });
        }
        if *bytes_read >= byte_cap {
            return Ok(BoundedRecordRead::Exhausted);
        }

        let remaining = byte_cap - *bytes_read;
        let bounded = &available[..available.len().min(remaining)];
        let consumed = bounded
            .iter()
            .position(|byte| *byte == b'\n')
            .map_or(bounded.len(), |index| index + 1);
        let record_complete = bounded.get(consumed.saturating_sub(1)) == Some(&b'\n');
        record.extend_from_slice(&bounded[..consumed]);
        reader.consume(consumed);
        *bytes_read += consumed;
        if record_complete {
            return Ok(BoundedRecordRead::Record);
        }
    }
}

/// Run a fixed-string smart-case ripgrep stream with request-scoped bounds.
pub(crate) async fn search_literal_bounded(
    query: &str,
    search_path: &Path,
    languages: &[AstGrepLanguage],
    candidate_cap: usize,
) -> Result<LiteralSearchOutcome> {
    let mut command = TokioCommand::new("rg");
    command
        .arg("--json")
        .arg("--fixed-strings")
        .arg("--smart-case")
        .arg("--sort=path")
        .arg("--hidden")
        .arg("--no-messages")
        .stdout(Stdio::piped())
        .stderr(Stdio::null());
    for pattern in DEFAULT_IGNORE_GLOBS {
        command.arg("--glob").arg(format!("!{pattern}"));
    }
    for language in languages {
        for glob in language.path_globs() {
            command.arg("--iglob").arg(glob);
        }
    }
    command.arg(query).arg(search_path);

    let mut child = command
        .spawn()
        .with_context(|| format!("failed to execute ripgrep for literal query '{query}'"))?;
    let Some(stdout) = child.stdout.take() else {
        kill_and_reap_literal_child(&mut child).await;
        anyhow::bail!("failed to capture ripgrep output");
    };
    let mut reader = BufReader::new(stdout);
    let mut candidates = Vec::with_capacity(candidate_cap);
    let mut bytes_read = 0usize;
    let mut truncated = false;
    let mut line_buf = Vec::with_capacity(CODE_SEARCH_STREAM_BYTE_CAP);

    loop {
        line_buf.clear();
        let read =
            match read_bounded_record(&mut reader, &mut line_buf, &mut bytes_read, CODE_SEARCH_STREAM_BYTE_CAP).await {
                Ok(read) => read,
                Err(error) => {
                    drop(reader);
                    kill_and_reap_literal_child(&mut child).await;
                    return Err(error).context("failed to read ripgrep JSON stream");
                }
            };
        match read {
            BoundedRecordRead::Record => {}
            BoundedRecordRead::Eof => break,
            BoundedRecordRead::Exhausted => {
                truncated = true;
                break;
            }
        }
        let event = match serde_json::from_slice::<Value>(&line_buf) {
            Ok(event) => event,
            Err(error) => {
                drop(reader);
                kill_and_reap_literal_child(&mut child).await;
                return Err(error).context("failed to parse ripgrep JSON stream record");
            }
        };
        if event.get("type").and_then(Value::as_str) != Some("match") {
            continue;
        }
        let Some(data) = event.get("data") else {
            continue;
        };
        let Some(path) = data.get("path").and_then(|path| path.get("text")).and_then(Value::as_str) else {
            continue;
        };
        let Some(snippet) = data.get("lines").and_then(|lines| lines.get("text")).and_then(Value::as_str) else {
            continue;
        };
        let line = data
            .get("line_number")
            .and_then(Value::as_u64)
            .and_then(|line| usize::try_from(line).ok())
            .unwrap_or(1);
        let absolute_offset = data
            .get("absolute_offset")
            .and_then(Value::as_u64)
            .and_then(|offset| usize::try_from(offset).ok())
            .unwrap_or(0);
        let Some(submatches) = data.get("submatches").and_then(Value::as_array) else {
            continue;
        };
        for submatch in submatches {
            let Some(start) = submatch
                .get("start")
                .and_then(Value::as_u64)
                .and_then(|offset| usize::try_from(offset).ok())
            else {
                continue;
            };
            let Some(end) = submatch
                .get("end")
                .and_then(Value::as_u64)
                .and_then(|offset| usize::try_from(offset).ok())
            else {
                continue;
            };
            let Some(matched_text) = submatch
                .get("match")
                .and_then(|value| value.get("text"))
                .and_then(Value::as_str)
            else {
                continue;
            };
            candidates.push(LiteralSearchCandidate {
                path: PathBuf::from(path),
                line,
                column: start.saturating_add(1),
                byte_start: absolute_offset.saturating_add(start),
                byte_end: absolute_offset.saturating_add(end),
                matched_text: matched_text.to_string(),
                snippet: snippet.to_string(),
            });
            if candidates.len() >= candidate_cap {
                truncated = true;
                break;
            }
        }
        if truncated {
            break;
        }
    }

    drop(reader);
    if truncated {
        let _ = child.start_kill();
    }
    let status = child.wait().await.context("failed to reap ripgrep process")?;
    if !truncated && !matches!(status.code(), Some(0) | Some(1)) {
        anyhow::bail!("ripgrep literal search failed");
    }

    Ok(LiteralSearchOutcome { candidates, truncated })
}

/// Input parameters for ripgrep search
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GrepSearchInput {
    pub pattern: String,
    pub path: String,
    pub case_sensitive: Option<bool>,
    pub literal: Option<bool>,
    pub glob_pattern: Option<String>,
    pub context_lines: Option<usize>,
    pub include_hidden: Option<bool>,
    pub max_results: Option<usize>,
    pub respect_ignore_files: Option<bool>, // Whether to respect .gitignore, .ignore files
    pub max_file_size: Option<usize>,       // Maximum file size to search (in bytes)
    pub search_hidden: Option<bool>,        // Whether to search hidden files/directories
    pub search_binary: Option<bool>,        // Whether to search binary files
    pub files_with_matches: Option<bool>,   // Only print filenames with matches
    pub type_pattern: Option<String>,       // Search files of a specific type (e.g., "rust", "python")
    pub invert_match: Option<bool>,         // Invert the matching
    pub word_boundaries: Option<bool>,      // Match only word boundaries (regexp \b)
    pub line_number: Option<bool>,          // Show line numbers
    pub column: Option<bool>,               // Show column numbers
    pub only_matching: Option<bool>,        // Show only matching parts
    pub trim: Option<bool>,                 // Trim whitespace from matches
    pub max_result_bytes: Option<usize>,    // Optional truncation threshold (bytes)
    pub timeout: Option<Duration>,          // Optional timeout for blocking grep
    pub extra_ignore_globs: Option<Vec<String>>, // Additional ignore globs
}

impl GrepSearchInput {
    /// Create a new search input with pattern and path, using sensible defaults
    #[inline]
    pub fn new(pattern: String, path: String) -> Self {
        Self {
            pattern,
            path,
            case_sensitive: None,
            literal: None,
            glob_pattern: None,
            context_lines: None,
            include_hidden: None,
            max_results: None,
            respect_ignore_files: None,
            max_file_size: None,
            search_hidden: None,
            search_binary: None,
            files_with_matches: None,
            type_pattern: None,
            invert_match: None,
            word_boundaries: None,
            line_number: None,
            column: None,
            only_matching: None,
            trim: None,
            max_result_bytes: None,
            timeout: None,
            extra_ignore_globs: None,
        }
    }

    /// Create a search input with common defaults for internal grep searches
    #[inline]
    pub fn with_defaults(pattern: String, path: String) -> Self {
        Self {
            pattern,
            path,
            case_sensitive: Some(true),
            literal: Some(false),
            glob_pattern: None,
            context_lines: None,
            include_hidden: Some(false),
            max_results: Some(MAX_SEARCH_RESULTS.get()),
            respect_ignore_files: Some(true),
            max_file_size: None,
            search_hidden: Some(false),
            search_binary: Some(false),
            files_with_matches: Some(false),
            type_pattern: None,
            invert_match: Some(false),
            word_boundaries: Some(false),
            line_number: Some(true),
            column: Some(false),
            only_matching: Some(false),
            trim: Some(false),
            max_result_bytes: Some(DEFAULT_MAX_RESULT_BYTES),
            timeout: Some(DEFAULT_SEARCH_TIMEOUT),
            extra_ignore_globs: None,
        }
    }
}

/// Result of a ripgrep search
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GrepSearchResult {
    pub query: String,
    pub matches: Vec<Value>,
    pub truncated: bool,
    /// Total number of "match" type entries found before truncation.
    /// When `truncated` is true, this tells the agent how many matches exist
    /// vs how many are returned in `matches`.
    #[serde(default)]
    pub total_matches: Option<usize>,
}

/// State machine for grep_file orchestration.
pub struct GrepSearchManager {
    /// Unified state guarded by one mutex.
    state: Arc<Mutex<SearchState>>,

    search_dir: PathBuf,

    /// LRU cache for search results to avoid redundant searches
    cache: Arc<GrepSearchCache>,
}

struct SearchState {
    /// Latest query typed by user (updated every keystroke).
    latest_query: String,

    /// true if a search is currently scheduled.
    is_search_scheduled: bool,

    /// If there is an active search, this will be the query being searched.
    active_search: Option<ActiveSearch>,
    last_result: Option<GrepSearchResult>,
}

struct ActiveSearch {
    query: String,
    cancellation_token: Arc<AtomicBool>,
}

impl GrepSearchManager {
    pub fn new(search_dir: PathBuf) -> Self {
        Self {
            state: Arc::new(Mutex::new(SearchState {
                latest_query: String::new(),
                is_search_scheduled: false,
                active_search: None,
                last_result: None,
            })),
            search_dir,
            cache: Arc::new(GrepSearchCache::new(100)), // Cache up to 100 recent searches
        }
    }

    fn cached_result(cache: &GrepSearchCache, input: &GrepSearchInput) -> Option<GrepSearchResult> {
        cache.get(input).map(|cached| GrepSearchResult {
            query: cached.query.clone(),
            matches: cached.matches.clone(),
            truncated: cached.truncated,
            total_matches: cached.total_matches,
        })
    }

    /// Call whenever the user edits the search query.
    pub fn on_user_query(&self, query: &str) {
        {
            let mut st = match self.state.lock() {
                Ok(state) => state,
                Err(err) => {
                    warn!("grep search state lock poisoned while handling query update: {err}");
                    return;
                }
            };
            if query != st.latest_query {
                st.latest_query.clear();
                st.latest_query.push_str(query);
            } else {
                return;
            }

            // If there is an in-flight search that is definitely obsolete,
            // cancel it now.
            if let Some(active_search) = &st.active_search
                && !query.starts_with(&active_search.query)
            {
                active_search.cancellation_token.store(true, Ordering::Relaxed);
                st.active_search = None;
            }

            // Schedule a search to run after debounce.
            if !st.is_search_scheduled {
                st.is_search_scheduled = true;
            } else {
                return;
            }
        }

        // If we are here, we set `st.is_search_scheduled = true` before
        // dropping the lock. This means we are the only thread that can spawn a
        // debounce timer.
        let state = self.state.clone();
        let search_dir = self.search_dir.clone();
        let cache = self.cache.clone();
        // Run debounce and search spawn on a blocking thread to avoid
        // blocking the async runtime or reader threads.
        spawn_blocking(move || {
            // Always do a minimum debounce, but then poll until the
            // `active_search` is cleared.
            thread::sleep(SEARCH_DEBOUNCE);
            loop {
                let active_is_none = match state.lock() {
                    Ok(st) => st.active_search.is_none(),
                    Err(err) => {
                        warn!("grep search state lock poisoned while waiting for active search: {err}");
                        return;
                    }
                };
                if active_is_none {
                    break;
                }
                thread::sleep(ACTIVE_SEARCH_COMPLETE_POLL_INTERVAL);
            }

            // The debounce timer has expired, so start a search using the
            // latest query.
            let cancellation_token = Arc::new(AtomicBool::new(false));
            let token = cancellation_token.clone();
            let query = {
                let mut st = match state.lock() {
                    Ok(state) => state,
                    Err(err) => {
                        warn!("grep search state lock poisoned while preparing debounced search: {err}");
                        return;
                    }
                };
                let query = st.latest_query.clone();
                st.is_search_scheduled = false;
                st.active_search = Some(ActiveSearch { query: query.clone(), cancellation_token: token });
                query
            };

            GrepSearchManager::spawn_grep_file(query, search_dir, cancellation_token, state, Some(cache));
        });
    }

    /// Retrieve the last successful search result
    pub fn last_result(&self) -> Option<GrepSearchResult> {
        match self.state.lock() {
            Ok(st) => st.last_result.clone(),
            Err(err) => {
                warn!("grep search state lock poisoned while reading last result: {err}");
                None
            }
        }
    }

    fn execute_with_backends(input: &GrepSearchInput) -> Result<(Vec<Value>, bool, usize)> {
        Self::run_ripgrep_backend(input)
    }

    fn run_ripgrep_backend(input: &GrepSearchInput) -> Result<(Vec<Value>, bool, usize)> {
        use std::process::Command;

        let mut cmd = Command::new("rg");
        cmd.arg("-j").arg(optimal_search_threads().get().to_string());

        // Add support for respecting ignore files (default is to respect them)
        if !input.respect_ignore_files.unwrap_or(true) {
            cmd.arg("--no-ignore");
        }

        // Add support for searching hidden files (default is not to search hidden)
        if input.search_hidden.unwrap_or(false) {
            cmd.arg("--hidden");
        }

        // Add support for searching binary files
        if input.search_binary.unwrap_or(false) {
            cmd.arg("--binary");
        }

        // Add support for files with matches only
        if input.files_with_matches.unwrap_or(false) {
            cmd.arg("--files-with-matches");
        }

        // Add support for file type filtering
        if let Some(type_pattern) = &input.type_pattern {
            cmd.arg("--type").arg(type_pattern);
        }

        // Add support for max file size
        if let Some(max_file_size) = input.max_file_size {
            cmd.arg("--max-filesize").arg(format!("{max_file_size}B"));
        }

        // Case sensitivity: pick exactly one flag from a single match so
        // ripgrep never sees conflicting `--ignore-case` + `--smart-case`.
        // Previously the cascade could append both when `case_sensitive`
        // defaulted to None but a higher-level wrapper set it to false.
        match input.case_sensitive {
            Some(true) => {
                cmd.arg("--case-sensitive");
            }
            Some(false) => {
                cmd.arg("--ignore-case");
            }
            None => {
                // Default to smart case when the caller didn't specify.
                cmd.arg("--smart-case");
            }
        }

        // Invert match
        if input.invert_match.unwrap_or(false) {
            cmd.arg("--invert-match");
        }

        // Word boundaries
        if input.word_boundaries.unwrap_or(false) {
            cmd.arg("--word-regexp");
        }

        // Line numbers
        if input.line_number.unwrap_or(true) {
            // Default to true to maintain context
            cmd.arg("--line-number");
        } else {
            cmd.arg("--no-line-number");
        }

        // Column numbers
        if input.column.unwrap_or(false) {
            cmd.arg("--column");
        }

        // Only matching parts
        if input.only_matching.unwrap_or(false) {
            cmd.arg("--only-matching");
        }

        // Trim whitespace (handled by not adding the --no-unicode flag, which is default)
        if input.trim.unwrap_or(false) {
            // This is handled in post-processing, not as a flag
        }

        if let Some(literal) = input.literal
            && literal
        {
            cmd.arg("--fixed-strings");
        }

        if let Some(glob_pattern) = &input.glob_pattern {
            cmd.arg("--glob").arg(glob_pattern);
        }

        if input.respect_ignore_files.unwrap_or(true) {
            for pattern in DEFAULT_IGNORE_GLOBS {
                cmd.arg("--glob").arg(format!("!{pattern}"));
            }
            if let Some(extra) = &input.extra_ignore_globs {
                for pattern in extra {
                    cmd.arg("--glob").arg(format!("!{pattern}"));
                }
            }
        }

        if let Some(context_lines) = input.context_lines {
            cmd.arg("--context").arg(context_lines.to_string());
        }

        let max_results = input.max_results.unwrap_or(MAX_SEARCH_RESULTS.get());
        cmd.arg("--max-count").arg(max_results.to_string());

        // Use JSON output format for structured results
        cmd.arg("--json");

        cmd.arg(&input.pattern);
        cmd.arg(&input.path);

        let output = cmd
            .output()
            .with_context(|| format!("failed to execute ripgrep for pattern '{}'", input.pattern))?;

        let output_str = String::from_utf8_lossy(&output.stdout);
        let matches: Vec<Value> = output_str
            .lines()
            .filter_map(|line| serde_json::from_str::<Value>(line).ok())
            .collect();

        Ok(Self::finalize_matches(matches, input))
    }

    fn finalize_matches(mut matches: Vec<Value>, input: &GrepSearchInput) -> (Vec<Value>, bool, usize) {
        let mut truncated = false;
        let max_results = input.max_results.unwrap_or(MAX_SEARCH_RESULTS.get());

        if max_results == 0 {
            return (Vec::new(), !matches.is_empty(), 0);
        }

        // Count total "match" type entries before any truncation.
        let total_match_count = matches
            .iter()
            .filter(|e| e.get("type").and_then(Value::as_str) == Some("match"))
            .count();

        // Count only "match" type entries (not "context", "begin", "end") so that
        // context lines don't crowd out actual matches from the result set.
        let mut match_count = 0usize;
        let mut cut_index = matches.len();
        for (i, entry) in matches.iter().enumerate() {
            let is_match = entry.get("type").and_then(Value::as_str).is_some_and(|t| t == "match");
            if is_match {
                match_count += 1;
                if match_count >= max_results {
                    // Keep everything up to and including this match, plus any
                    // trailing context lines that belong to it.
                    cut_index = i + 1;
                    // Advance past trailing context lines for this match.
                    for rest in matches.iter().skip(i + 1) {
                        let tp = rest.get("type").and_then(Value::as_str);
                        if tp == Some("context") {
                            cut_index += 1;
                        } else {
                            break;
                        }
                    }
                    break;
                }
            }
        }
        // Check if there are more match-type entries beyond our cut point.
        if matches[cut_index..]
            .iter()
            .any(|e| e.get("type").and_then(Value::as_str) == Some("match"))
        {
            truncated = true;
        }
        if cut_index < matches.len() {
            matches.truncate(cut_index);
        }

        if let Some(limit) = input.max_result_bytes {
            let mut total = 0usize;
            let mut kept_count = 0;
            for entry in &matches {
                let entry_bytes = estimate_json_size(entry) as usize;
                if total + entry_bytes > limit {
                    truncated = true;
                    break;
                }
                total += entry_bytes;
                kept_count += 1;
            }
            matches.truncate(kept_count);
        }

        (matches, truncated, total_match_count)
    }

    fn spawn_grep_file(
        query: String,
        search_dir: PathBuf,
        cancellation_token: Arc<AtomicBool>,
        search_state: Arc<Mutex<SearchState>>,
        cache: Option<Arc<GrepSearchCache>>,
    ) {
        // Spawn grep worker on a blocking thread — searching and ripgrep are blocking.
        spawn_blocking(move || {
            // Check if cancelled before starting
            if cancellation_token.load(Ordering::Relaxed) {
                // Reset the active search state
                {
                    let mut st = match search_state.lock() {
                        Ok(state) => state,
                        Err(err) => {
                            warn!("grep search state lock poisoned while cancelling search: {err}");
                            return;
                        }
                    };
                    if let Some(active_search) = &st.active_search
                        && Arc::ptr_eq(&active_search.cancellation_token, &cancellation_token)
                    {
                        st.active_search = None;
                    }
                }
                return;
            }

            let input = GrepSearchInput::with_defaults(query.clone(), search_dir.to_string_lossy().into_owned());

            // Check cache first if available
            if let Some(ref cache) = cache
                && let Some(cached_result) = Self::cached_result(cache, &input)
            {
                let mut st = match search_state.lock() {
                    Ok(state) => state,
                    Err(err) => {
                        warn!("grep search state lock poisoned while loading cached result: {err}");
                        return;
                    }
                };
                st.last_result = Some(cached_result);
                return;
            }

            let search_result = GrepSearchManager::execute_with_backends(&input);

            let is_cancelled = cancellation_token.load(Ordering::Relaxed);
            if !is_cancelled
                && let Ok((matches, truncated, total_match_count)) = search_result
                && !matches.is_empty()
            {
                let result = GrepSearchResult {
                    query,
                    matches,
                    truncated,
                    total_matches: if truncated { Some(total_match_count) } else { None },
                };

                // Cache the result if cache is available
                if let Some(ref cache) = cache
                    && GrepSearchCache::should_cache(&result)
                {
                    cache.put(&input, result.clone());
                }

                let mut st = match search_state.lock() {
                    Ok(state) => state,
                    Err(err) => {
                        warn!("grep search state lock poisoned while storing search result: {err}");
                        return;
                    }
                };
                st.last_result = Some(result);
            }

            // Reset the active search state
            {
                let mut st = match search_state.lock() {
                    Ok(state) => state,
                    Err(err) => {
                        warn!("grep search state lock poisoned while clearing active search: {err}");
                        return;
                    }
                };
                if let Some(active_search) = &st.active_search
                    && Arc::ptr_eq(&active_search.cancellation_token, &cancellation_token)
                {
                    st.active_search = None;
                }
            }
        });
    }

    /// Perform an actual ripgrep search with the given input parameters
    pub async fn perform_search(&self, input: GrepSearchInput) -> Result<GrepSearchResult> {
        // Check cache first
        if let Some(cached_result) = Self::cached_result(&self.cache, &input) {
            return Ok(cached_result);
        }

        let query = input.pattern.clone();
        let input_clone = input.clone();

        let timeout = input.timeout.unwrap_or(DEFAULT_SEARCH_TIMEOUT);
        // Spawn the blocking search on its own handle so we can cancel it
        // on timeout. The previous implementation used `tokio::time::timeout`
        // which returns `Err(Elapsed)` but leaves the blocking task running
        // — repeated timeouts would leak threads holding the state lock.
        let mut join = spawn_blocking(move || GrepSearchManager::execute_with_backends(&input_clone));
        let outcome = tokio::time::timeout(timeout, &mut join).await;
        let (matches, truncated, total_match_count) = match outcome {
            Ok(Ok(Ok(result))) => result,
            Ok(Ok(Err(worker_err))) => {
                return Err(worker_err.context("ripgrep search worker failed"));
            }
            Ok(Err(join_err)) => {
                return Err(anyhow::Error::new(join_err).context("ripgrep search worker panicked"));
            }
            Err(_elapsed) => {
                // Abort the blocking task so it doesn't keep running after
                // we time out. The runtime cancels the spawn_blocking task
                // and frees its slot in the blocking pool.
                join.abort();
                let _ = join.await;
                return Err(anyhow::anyhow!(
                    "ripgrep search timed out after {}s; the worker has been cancelled",
                    timeout.as_secs()
                ));
            }
        };

        let result = GrepSearchResult {
            query,
            matches,
            truncated,
            total_matches: if truncated { Some(total_match_count) } else { None },
        };

        // Cache the result if it's worth caching (non-empty, successful)
        if GrepSearchCache::should_cache(&result) {
            self.cache.put(&input, result.clone());
        }

        Ok(result)
    }

    /// Perform file enumeration using the optimized file search bridge
    ///
    /// This method uses vtcode-indexer::file_search for parallel, fuzzy file discovery.
    /// It's optimized for:
    /// - Listing files in large directories
    /// - Fuzzy filename matching
    /// - Respecting .gitignore and .ignore files
    /// - Parallel directory traversal
    ///
    /// # Arguments
    ///
    /// * `pattern` - Fuzzy search pattern for filenames (e.g., "main", "test.rs")
    /// * `max_results` - Maximum number of files to return
    /// * `cancel_flag` - Optional cancellation token for early termination
    ///
    /// # Returns
    ///
    /// A vector of file paths matching the pattern, sorted by match quality
    pub fn enumerate_files_with_pattern(
        &self,
        pattern: String,
        max_results: usize,
        cancel_flag: Option<Arc<AtomicBool>>,
    ) -> Result<Vec<String>> {
        let config = FileSearchConfig::new(pattern, self.search_dir.clone())
            .with_limit(max_results)
            .respect_gitignore(true);

        let results = file_search_bridge::search_files(config, cancel_flag)?;

        Ok(file_search_bridge::file_matches_only(results.matches)
            .into_iter()
            .map(|m| m.path)
            .collect())
    }

    /// List all files in the search directory using the file search bridge
    ///
    /// This is useful for operations that need to enumerate all discoverable files
    /// without a specific pattern match.
    ///
    /// # Arguments
    ///
    /// * `max_results` - Maximum number of files to return
    /// * `exclude_patterns` - Patterns to exclude from results (glob-style)
    ///
    /// # Returns
    ///
    /// A vector of file paths
    pub fn list_all_files(&self, max_results: usize, exclude_patterns: Vec<String>) -> Result<Vec<String>> {
        let mut config = FileSearchConfig::new("".to_string(), self.search_dir.clone())
            .with_limit(max_results)
            .respect_gitignore(true);

        for pattern in exclude_patterns {
            config = config.exclude(pattern);
        }

        let results = file_search_bridge::search_files(config, None)?;

        Ok(file_search_bridge::file_matches_only(results.matches)
            .into_iter()
            .map(|m| m.path)
            .collect())
    }
}

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

    #[tokio::test]
    async fn code_search_literal_stream_reaps_at_candidate_cap() {
        let workspace = TempDir::new().expect("workspace");
        std::fs::write(workspace.path().join("matches.txt"), "Widget\nWidget\nWidget\n").expect("fixture");

        let outcome = search_literal_bounded("Widget", workspace.path(), &[], 2)
            .await
            .expect("bounded literal search");

        assert_eq!(outcome.candidates.len(), 2);
        assert!(outcome.truncated);
    }

    #[tokio::test]
    async fn code_search_literal_candidate_cap_selects_stable_path_prefix() {
        let workspace = TempDir::new().expect("workspace");
        for name in ["z.txt", "a.txt", "m.txt"] {
            std::fs::write(workspace.path().join(name), "Widget\nWidget\n").expect("fixture");
        }

        let first = search_literal_bounded("Widget", workspace.path(), &[], 2)
            .await
            .expect("first bounded search");
        let second = search_literal_bounded("Widget", workspace.path(), &[], 2)
            .await
            .expect("second bounded search");

        assert_eq!(first, second);
        assert!(first.truncated);
        assert!(
            first.candidates.iter().all(|candidate| candidate.path.ends_with("a.txt")),
            "sorted cap prefix should come from a.txt: {first:?}"
        );
    }

    #[tokio::test]
    async fn code_search_literal_stream_reaps_at_byte_cap() {
        let workspace = TempDir::new().expect("workspace");
        std::fs::write(
            workspace.path().join("large.txt"),
            format!("Widget{}\n", "x".repeat(CODE_SEARCH_STREAM_BYTE_CAP)),
        )
        .expect("fixture");

        let outcome = search_literal_bounded("Widget", workspace.path(), &[], 20)
            .await
            .expect("byte-bounded literal search");

        assert!(outcome.candidates.is_empty());
        assert!(outcome.truncated);
    }

    #[tokio::test]
    async fn code_search_bounded_record_distinguishes_exact_eof_from_exhaustion() {
        let mut exact_reader = BufReader::new(&b"{}\n"[..]);
        let mut exact_record = Vec::with_capacity(3);
        let mut exact_bytes_read = 0;
        assert_eq!(
            read_bounded_record(&mut exact_reader, &mut exact_record, &mut exact_bytes_read, 3,)
                .await
                .expect("exact record"),
            BoundedRecordRead::Record
        );
        exact_record.clear();
        assert_eq!(
            read_bounded_record(&mut exact_reader, &mut exact_record, &mut exact_bytes_read, 3,)
                .await
                .expect("exact EOF probe"),
            BoundedRecordRead::Eof
        );

        let mut oversized_reader = BufReader::new(&b"xxxx"[..]);
        let mut oversized_record = Vec::with_capacity(3);
        let mut oversized_bytes_read = 0;
        assert_eq!(
            read_bounded_record(&mut oversized_reader, &mut oversized_record, &mut oversized_bytes_read, 3,)
                .await
                .expect("oversized record"),
            BoundedRecordRead::Exhausted
        );
        assert_eq!(oversized_record.len(), 3);
    }

    #[tokio::test]
    async fn code_search_literal_language_prefilters_are_case_insensitive() {
        let workspace = TempDir::new().expect("workspace");
        std::fs::write(workspace.path().join("UPPER.RS"), "Widget\n").expect("Rust fixture");
        std::fs::write(workspace.path().join("DOCKERFILE"), "Widget\n").expect("Dockerfile fixture");

        let rust = search_literal_bounded("Widget", workspace.path(), &[AstGrepLanguage::Rust], 20)
            .await
            .expect("uppercase Rust extension");
        assert!(rust.candidates.iter().any(|candidate| {
            candidate.path.ends_with("UPPER.RS")
                && AstGrepLanguage::from_path(&candidate.path) == Some(AstGrepLanguage::Rust)
        }));

        let dockerfile = search_literal_bounded("Widget", workspace.path(), &[AstGrepLanguage::Dockerfile], 20)
            .await
            .expect("uppercase Dockerfile name");
        assert!(dockerfile.candidates.iter().any(|candidate| {
            candidate.path.ends_with("DOCKERFILE")
                && AstGrepLanguage::from_path(&candidate.path) == Some(AstGrepLanguage::Dockerfile)
        }));
    }

    #[test]
    fn finalize_matches_respects_max_bytes() {
        let mut input = GrepSearchInput::with_defaults("pat".into(), ".".into());
        input.max_result_bytes = Some(100);
        input.max_results = Some(5);

        let matches = vec![json!({"text": "12345"}), json!({"text": "6789"})];

        let (kept, truncated, _total) = GrepSearchManager::finalize_matches(matches, &input);
        assert!(!truncated);
        assert_eq!(kept.len(), 2);

        // Test with smaller limit that truncates
        input.max_result_bytes = Some(20);
        let matches = vec![json!({"text": "12345"}), json!({"text": "6789"})];
        let (kept, truncated, _total) = GrepSearchManager::finalize_matches(matches, &input);
        assert!(truncated);
        assert_eq!(kept.len(), 1); // Only first match fits in 20 bytes
    }

    #[test]
    fn finalize_matches_counts_only_match_type_entries() {
        let mut input = GrepSearchInput::with_defaults("pat".into(), ".".into());
        input.max_results = Some(2);

        // Simulate ripgrep JSON output: begin, context, match, context, end
        let matches = vec![
            json!({"type": "begin", "data": {"path": {"text": "Cargo.lock"}}}),
            json!({"type": "context", "data": {"line_number": 538, "lines": {"text": "ctx1"}}}),
            json!({"type": "context", "data": {"line_number": 539, "lines": {"text": "ctx2"}}}),
            json!({"type": "match", "data": {"line_number": 553, "lines": {"text": "match1"}}}),
            json!({"type": "context", "data": {"line_number": 554, "lines": {"text": "ctx3"}}}),
            json!({"type": "context", "data": {"line_number": 555, "lines": {"text": "ctx4"}}}),
            json!({"type": "context", "data": {"line_number": 560, "lines": {"text": "ctx5"}}}),
            json!({"type": "match", "data": {"line_number": 563, "lines": {"text": "match2"}}}),
            json!({"type": "context", "data": {"line_number": 564, "lines": {"text": "ctx6"}}}),
            json!({"type": "end", "data": {"path": {"text": "Cargo.lock"}}}),
        ];

        let (kept, truncated, total) = GrepSearchManager::finalize_matches(matches, &input);
        // Should keep all entries up through the second match's trailing context.
        // match_count reaches 2 at index 7, then trailing context at index 8 -> cut_index = 9.
        assert!(!truncated);
        assert_eq!(kept.len(), 9);
        assert_eq!(kept[3]["type"], "match");
        assert_eq!(kept[7]["type"], "match");
        assert_eq!(total, 2);
    }

    #[test]
    fn finalize_matches_truncates_when_more_match_types_than_limit() {
        let mut input = GrepSearchInput::with_defaults("pat".into(), ".".into());
        input.max_results = Some(1);

        let matches = vec![
            json!({"type": "begin", "data": {"path": {"text": "f.txt"}}}),
            json!({"type": "match", "data": {"line_number": 1, "lines": {"text": "m1"}}}),
            json!({"type": "context", "data": {"line_number": 2, "lines": {"text": "c1"}}}),
            json!({"type": "match", "data": {"line_number": 10, "lines": {"text": "m2"}}}),
            json!({"type": "context", "data": {"line_number": 11, "lines": {"text": "c2"}}}),
        ];

        let (kept, truncated, total) = GrepSearchManager::finalize_matches(matches, &input);
        assert!(truncated);
        // Keeps: begin + match1 + context after match1 = 3 entries
        assert_eq!(kept.len(), 3);
        assert_eq!(kept[1]["type"], "match");
        assert_eq!(kept[2]["type"], "context");
        assert_eq!(total, 2); // 2 match-type entries in the raw input
    }

    #[test]
    fn test_grep_search_manager_creation() {
        let manager = GrepSearchManager::new(PathBuf::from("."));
        assert_eq!(manager.search_dir, PathBuf::from("."));
    }

    #[test]
    fn test_grep_search_input_new() {
        let input = GrepSearchInput::new("pattern".to_string(), "/path/to/search".to_string());
        assert_eq!(input.pattern, "pattern");
        assert_eq!(input.path, "/path/to/search");
        assert!(input.case_sensitive.is_none());
    }

    #[test]
    fn test_grep_search_input_with_defaults() {
        let input = GrepSearchInput::with_defaults("pattern".to_string(), "/path".to_string());
        assert_eq!(input.pattern, "pattern");
        assert_eq!(input.path, "/path");
        assert_eq!(input.case_sensitive, Some(true));
        assert_eq!(input.include_hidden, Some(false));
        assert_eq!(input.max_results, Some(MAX_SEARCH_RESULTS.get()));
    }
}