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
//! Native content-search backend for `grep_file` and `code_search`.
//!
//! This module owns the search execution and the result shapes. The actual
//! scanning lives in [`vtcode_commons::content_search`], which walks the tree
//! with the `ignore` crate and matches lines with the `regex` crate.
//!
//! Two result shapes exist:
//!
//! - [`LiteralSearchOutcome`]: typed candidates used by `code_search`.
//! - [`GrepSearchResult`]: search JSON events used by `grep_file`.
//!
//! The JSON event shape is kept because `grep_file` results are consumed by
//! the transcript renderer and the file-palette path, which index into the
//! `data.path.text` and `type` fields. Producing the same shape natively keeps
//! those consumers unchanged.
use std::num::NonZeroUsize;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::time::Duration;
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use vtcode_commons::content_search::{CaseSensitivity, LineMatch, SearchRequest};
use vtcode_commons::exclusions::DEFAULT_IGNORE_GLOBS;
use super::code_language::CodeLanguage;
/// Maximum number of search results to return - AGENTS.md requires max 5 results
pub(crate) const MAX_SEARCH_RESULTS: NonZeroUsize = NonZeroUsize::new(5).expect("5 is non-zero");
/// Default timeout for blocking grep invocations.
pub(crate) const DEFAULT_SEARCH_TIMEOUT: Duration = Duration::from_secs(5);
/// A literal search hit, in the shape `code_search` consumes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct LiteralSearchCandidate {
/// Path of the file the hit was found in.
pub path: PathBuf,
/// One-based line number of the hit.
pub line: usize,
/// One-based column of the hit start.
pub column: usize,
/// Byte offset of the hit start within the file.
pub byte_start: usize,
/// Byte offset of the hit end within the file.
pub byte_end: usize,
/// The matched substring.
pub matched_text: String,
/// The full text of the matching line.
pub snippet: String,
/// Whether the hit covers a whole identifier.
///
/// A hit inside a longer identifier (`Widget` within `WidgetExtra`) is not
/// word-bounded, so it cannot be a usage of the queried symbol.
pub word_bounded: bool,
}
/// The outcome of a bounded literal search.
#[derive(Debug, Clone, Default)]
pub(crate) struct LiteralSearchOutcome {
/// The hits that were found, bounded by the caller's cap.
pub candidates: Vec<LiteralSearchCandidate>,
/// Whether the cap was reached before the search finished.
pub truncated: bool,
}
/// Splits a `|`-separated query into its literal terms.
///
/// An empty query yields no terms. A query without `|` yields exactly one
/// term. Otherwise the query is split on `|`, each part is trimmed, and empty
/// parts are dropped so the alternation is always well-formed.
pub(crate) fn split_alternation(query: &str) -> Vec<String> {
let trimmed = query.trim();
if trimmed.is_empty() {
return Vec::new();
}
if !trimmed.contains('|') {
return vec![trimmed.to_string()];
}
trimmed
.split('|')
.map(str::trim)
.filter(|term| !term.is_empty())
.map(str::to_string)
.collect()
}
/// Builds a search request for a literal-or-alternation query.
///
/// A single-term query is matched verbatim, so regex metacharacters in the
/// query keep no special meaning. A multi-term query becomes a regex
/// alternation of the escaped terms, so `tokio|async-std|runtime` matches any
/// of the three.
fn literal_request(query: &str, search_path: &Path, languages: &[CodeLanguage]) -> SearchRequest {
let terms = split_alternation(query);
let mut request = if terms.len() >= 2 {
let alternation = terms.iter().map(|term| regex::escape(term)).collect::<Vec<_>>().join("|");
SearchRequest::regex(alternation, search_path.to_path_buf())
} else {
SearchRequest::literal(terms.first().cloned().unwrap_or_default(), search_path.to_path_buf())
};
request = request.with_case(CaseSensitivity::Smart);
for language in languages {
for glob in language.path_globs() {
request = request.with_language_glob(*glob);
}
}
request
}
/// Runs a bounded literal search over `search_path`.
///
/// The search stops once `candidate_cap` candidates have been collected, so a
/// broad query over a large tree does not scan the whole repository.
pub(crate) async fn search_literal_bounded(
query: &str,
search_path: &Path,
languages: &[CodeLanguage],
candidate_cap: usize,
) -> Result<LiteralSearchOutcome> {
if !search_path.exists() {
anyhow::bail!("literal search failed: {} does not exist", search_path.display());
}
let request = literal_request(query, search_path, languages).with_max_results(candidate_cap);
let matches = vtcode_commons::content_search::search(&request)
.with_context(|| format!("literal search failed for pattern '{query}'"))?;
let truncated = matches.len() >= candidate_cap;
let candidates = matches.into_iter().map(|line_match| literal_candidate(&line_match)).collect();
Ok(LiteralSearchOutcome { candidates, truncated })
}
/// Converts a native line match into the literal-search candidate shape.
fn literal_candidate(line_match: &LineMatch) -> LiteralSearchCandidate {
LiteralSearchCandidate {
path: line_match.path.clone(),
line: line_match.line,
column: line_match.column,
// The offsets are file-absolute, so they can be compared with the
// declaration ranges the scanner records for the same file.
byte_start: line_match.byte_start,
byte_end: line_match.byte_end,
matched_text: line_match.matched_text.clone(),
snippet: line_match.line_text.clone(),
word_bounded: line_match.word_bounded,
}
}
/// Input parameters for a content search.
///
/// Every option here is read by the engine. Options the engine cannot honour
/// are deliberately absent rather than accepted and ignored, so a caller
/// cannot be misled into believing an unsupported filter was applied.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct GrepSearchInput {
/// The pattern to search for.
pub pattern: String,
/// The path to search.
pub path: String,
/// Whether matching is case-sensitive. `None` means smart-case.
pub case_sensitive: Option<bool>,
/// Whether the pattern is a literal string rather than a regular expression.
pub literal: Option<bool>,
/// Restrict the search to files matching this glob.
pub glob_pattern: Option<String>,
/// Whether to include hidden files.
pub include_hidden: Option<bool>,
/// Maximum number of results to return.
pub max_results: Option<usize>,
/// Whether to respect ignore files.
pub respect_ignore_files: Option<bool>,
/// Maximum file size to search, in bytes.
pub max_file_size: Option<usize>,
/// Whether to report only the files that contain a match.
pub files_with_matches: Option<bool>,
/// Optional truncation threshold (bytes).
pub max_result_bytes: Option<usize>,
/// Optional timeout for blocking grep.
pub timeout: Option<Duration>,
/// Additional ignore globs.
pub extra_ignore_globs: Option<Vec<String>>,
}
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,
include_hidden: None,
max_results: None,
respect_ignore_files: None,
max_file_size: None,
files_with_matches: 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,
include_hidden: Some(false),
max_results: Some(MAX_SEARCH_RESULTS.get()),
respect_ignore_files: Some(true),
max_file_size: None,
files_with_matches: Some(false),
max_result_bytes: None,
timeout: None,
extra_ignore_globs: None,
}
}
}
/// The result of a content search, as JSON events.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GrepSearchResult {
/// The query that produced this result.
pub query: String,
/// The matches, as JSON events.
pub matches: Vec<Value>,
/// Whether the result was truncated.
pub truncated: bool,
/// Total number of matches found before truncation.
#[serde(default)]
pub total_matches: Option<usize>,
}
/// Runs a content search and returns the JSON events.
///
/// The event stream keeps the `begin` / `match` / `end` shape the transcript
/// renderer and the file-palette path already read.
pub(crate) async fn run_search_backend(
input: &GrepSearchInput,
cancel_flag: Option<&Arc<AtomicBool>>,
) -> Result<Vec<Value>> {
let search_path = PathBuf::from(&input.path);
if !search_path.exists() {
anyhow::bail!("failed to search for pattern '{}': path does not exist", input.pattern);
}
let max_results = input.max_results.unwrap_or(MAX_SEARCH_RESULTS.get());
let mut request = if input.literal.unwrap_or(false) {
SearchRequest::literal(input.pattern.clone(), search_path.clone())
} else {
SearchRequest::regex(input.pattern.clone(), search_path.clone())
};
request = match input.case_sensitive {
Some(true) => request.with_case(CaseSensitivity::Sensitive),
Some(false) => request.with_case(CaseSensitivity::Insensitive),
None => request.with_case(CaseSensitivity::Smart),
};
request = request.with_max_results(max_results);
request = request.with_max_per_file(max_results);
if let Some(max_file_size) = input.max_file_size {
request = request.with_max_file_size(max_file_size as u64);
}
if input.include_hidden.unwrap_or(false) {
request = request.with_hidden();
}
if input.files_with_matches.unwrap_or(false) {
request = request.with_files_with_matches();
}
if let Some(glob) = &input.glob_pattern {
request = request.with_include_glob(glob.clone());
}
for glob in DEFAULT_IGNORE_GLOBS {
request = request.with_exclude_glob(*glob);
}
if let Some(extra) = &input.extra_ignore_globs {
for glob in extra {
request = request.with_exclude_glob(glob);
}
}
if !input.respect_ignore_files.unwrap_or(true) {
request = request.without_ignore_files();
}
// The flag is cloned so the blocking closure owns it: the scan runs on a
// blocking thread, and the caller's reference does not outlive the await.
let cancel_flag = cancel_flag.cloned();
let matches = tokio::task::spawn_blocking(move || {
if cancel_flag.is_some_and(|flag| flag.load(std::sync::atomic::Ordering::Relaxed)) {
anyhow::bail!("content search cancelled");
}
vtcode_commons::content_search::search(&request).context("failed to search for pattern")
})
.await
.context("content search worker failed")??;
Ok(matches.into_iter().flat_map(json_events_for_match).collect())
}
/// Renders one native match as the JSON event sequence.
///
/// The shape matches `rg --json`: the submatch offsets are relative to the
/// start of the line, so they index into `lines.text`, and `absolute_offset`
/// marks where that line starts in the file.
fn json_events_for_match(line_match: LineMatch) -> Vec<Value> {
let path_text = line_match.path.to_string_lossy().into_owned();
// The offsets are relative to the line, so the file offset is reduced by
// the start of the line the match was found on.
let line_relative_start = line_match.byte_start.saturating_sub(line_match.line_start);
let line_relative_end = line_match.byte_end.saturating_sub(line_match.line_start);
let mut events = vec![json!({
"type": "begin",
"data": {"path": {"text": path_text}}
})];
events.push(json!({
"type": "match",
"data": {
"path": {"text": path_text},
"lines": {"text": line_match.line_text},
"line_number": line_match.line,
"absolute_offset": line_match.line_start,
"submatches": [{
"start": line_relative_start,
"end": line_relative_end,
"match": {"text": line_match.matched_text}
}]
}
}));
events.push(json!({
"type": "end",
"data": {"path": {"text": path_text}}
}));
events
}
#[cfg(test)]
mod tests;