Skip to content

Commit 1f561a9

Browse files
committed
Remove case sensitivity and max results options from regex search
Eliminate `case_sensitive` and `max_results` parameters to simplify the regex search tool. Update the regex pattern description to indicate the use of `(?i)` for case-insensitivity directly within the pattern. Adjust related code logic to accommodate the removal, ensuring that the search function no longer checks or enforces maximum result limits and handles pattern validation without conditional alterations for case sensitivity. This streamlines the function and reduces unnecessary complexity.
1 parent d3211a3 commit 1f561a9

File tree

2 files changed

+7
-91
lines changed

2 files changed

+7
-91
lines changed

refact-agent/engine/src/tools/tool_regex_search.rs

Lines changed: 6 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,9 @@ async fn search_files_with_regex(
6868
gcx: Arc<ARwLock<GlobalContext>>,
6969
pattern: &str,
7070
scope: &String,
71-
case_sensitive: bool,
72-
max_results: Option<usize>,
7371
subchat_tx: Option<Arc<AMutex<tokio::sync::mpsc::UnboundedSender<Value>>>>,
7472
) -> Result<Vec<ContextFile>, String> {
75-
let regex = if case_sensitive {
76-
Regex::new(pattern).map_err(|e| format!("Invalid regex pattern: {}", e))?
77-
} else {
78-
Regex::new(&format!("(?i){}", pattern)).map_err(|e| format!("Invalid regex pattern: {}", e))?
79-
};
73+
let regex = Regex::new(pattern).map_err(|e| format!("Invalid regex pattern: {}", e))?;
8074

8175
let files_to_search = if scope == "workspace" {
8276
let workspace_files = gcx.read().await.documents_state.workspace_files.lock().unwrap().clone();
@@ -131,31 +125,18 @@ async fn search_files_with_regex(
131125

132126
let regex_arc = Arc::new(regex);
133127
let results_mutex = Arc::new(AMutex::new(Vec::new()));
134-
let max_results_reached = Arc::new(AtomicUsize::new(0));
135-
136128
let search_futures = files_to_search.into_iter().map(|file_path| {
137129
let gcx_clone = gcx.clone();
138130
let regex_clone = regex_arc.clone();
139131
let progress_clone = progress.clone();
140132
let results_mutex_clone = results_mutex.clone();
141133
let subchat_tx_clone = subchat_tx.clone();
142-
let max_results_reached_clone = max_results_reached.clone();
143134

144135
async move {
145-
// Skip if we've already reached the maximum number of results
146-
if let Some(_) = max_results {
147-
if max_results_reached_clone.load(Ordering::Relaxed) > 0 {
148-
return;
149-
}
150-
}
151-
152136
let file_results = search_single_file(gcx_clone, file_path, &regex_clone).await;
153-
154-
// Update progress counters
155137
let processed = progress_clone.processed_files.fetch_add(1, Ordering::Relaxed) + 1;
156138
let matches_found = progress_clone.total_matches.fetch_add(file_results.len(), Ordering::Relaxed) + file_results.len();
157139

158-
// Send progress update (every 10 files or when matches are found)
159140
if let Some(tx) = &subchat_tx_clone {
160141
if processed % 10 == 0 || !file_results.is_empty() {
161142
let _ = tx.lock().await.send(json!({
@@ -167,41 +148,16 @@ async fn search_files_with_regex(
167148
}
168149
}
169150

170-
// Add results to the shared results vector
171151
if !file_results.is_empty() {
172152
let mut results = results_mutex_clone.lock().await;
173153
results.extend(file_results);
174-
175-
// Check if we've reached the maximum number of results
176-
if let Some(max) = max_results {
177-
if results.len() >= max {
178-
max_results_reached_clone.store(1, Ordering::Relaxed);
179-
if let Some(tx) = &subchat_tx_clone {
180-
let _ = tx.lock().await.send(json!({
181-
"progress": format!(
182-
"Maximum result limit ({}) reached. Stopping search...",
183-
max
184-
)
185-
}));
186-
}
187-
}
188-
}
189154
}
190155
}
191156
});
192157

193158
join_all(search_futures).await;
194-
195-
// Get the final results
196159
let mut results = results_mutex.lock().await.clone();
197160

198-
// Apply result limit if specified
199-
if let Some(max) = max_results {
200-
if results.len() > max {
201-
results.truncate(max);
202-
}
203-
}
204-
205161
// Sort results by file name
206162
results.sort_by(|a, b| a.file_name.cmp(&b.file_name));
207163

@@ -240,23 +196,7 @@ impl Tool for ToolRegexSearch {
240196
None => return Err("Missing argument `scope` in the regex_search() call.".to_string())
241197
};
242198

243-
let case_sensitive = match args.get("case_sensitive") {
244-
Some(Value::Bool(b)) => *b,
245-
Some(v) => return Err(format!("argument `case_sensitive` is not a boolean: {:?}", v)),
246-
None => false,
247-
};
248-
249-
let max_results = match args.get("max_results") {
250-
Some(Value::Number(n)) => {
251-
if let Some(num) = n.as_u64() {
252-
Some(num as usize)
253-
} else {
254-
return Err("argument `max_results` must be a positive integer".to_string());
255-
}
256-
},
257-
Some(v) => return Err(format!("argument `max_results` is not a number: {:?}", v)),
258-
None => None,
259-
};
199+
260200

261201
// Get context and subchat_tx for progress updates
262202
let ccx_lock = ccx.lock().await;
@@ -269,21 +209,15 @@ impl Tool for ToolRegexSearch {
269209
"progress": format!("Starting regex search for pattern '{}' in scope '{}'...", pattern, scope)
270210
}));
271211

272-
let pattern_to_validate = if case_sensitive {
273-
pattern.clone()
274-
} else {
275-
format!("(?i){}", pattern)
276-
};
277-
if let Err(e) = Regex::new(&pattern_to_validate) {
212+
// Validate the pattern
213+
if let Err(e) = Regex::new(&pattern) {
278214
return Err(format!("Invalid regex pattern: {}. Please check your syntax.", e));
279215
}
280216

281217
let search_results = search_files_with_regex(
282218
gcx.clone(),
283219
&pattern,
284220
&scope,
285-
case_sensitive,
286-
max_results,
287221
Some(subchat_tx.clone()),
288222
).await?;
289223

@@ -296,10 +230,7 @@ impl Tool for ToolRegexSearch {
296230
"progress": format!("Search complete. Found {} matches.", search_results.len())
297231
}));
298232

299-
let mut content = format!("Regex search results for pattern '{}' (case-{}):\n\n",
300-
pattern,
301-
if case_sensitive { "sensitive" } else { "insensitive" }
302-
);
233+
let mut content = format!("Regex search results for pattern '{}':\n\n", pattern);
303234

304235
let mut file_results: HashMap<String, Vec<&ContextFile>> = HashMap::new();
305236
search_results.iter().for_each(|rec| {
@@ -310,16 +241,7 @@ impl Tool for ToolRegexSearch {
310241
let total_matches = search_results.len();
311242
let total_files = file_results.len();
312243

313-
if let Some(limit) = max_results {
314-
if total_matches >= limit {
315-
content.push_str(&format!("Found {} matches across {} files (limited to first {} matches)\n\n",
316-
total_matches, total_files, limit));
317-
} else {
318-
content.push_str(&format!("Found {} matches across {} files\n\n", total_matches, total_files));
319-
}
320-
} else {
321-
content.push_str(&format!("Found {} matches across {} files\n\n", total_matches, total_files));
322-
}
244+
content.push_str(&format!("Found {} matches across {} files\n\n", total_matches, total_files));
323245

324246
for rec in search_results.iter() {
325247
if !used_files.contains(&rec.file_name) {

refact-agent/engine/src/tools/tools_description.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,16 +465,10 @@ tools:
465465
parameters:
466466
- name: "pattern"
467467
type: "string"
468-
description: "Regular expression pattern to search for"
468+
description: "Regular expression pattern to search for. Use (?i) at the start for case-insensitive search."
469469
- name: "scope"
470470
type: "string"
471471
description: "'workspace' to search all files in workspace, 'dir/subdir/' to search in files within a directory, 'dir/file.ext' to search in a single file."
472-
- name: "case_sensitive"
473-
type: "boolean"
474-
description: "Whether the search should be case-sensitive. Defaults to false."
475-
- name: "max_results"
476-
type: "number"
477-
description: "Maximum number of results to return. If not specified, all matches will be returned."
478472
parameters_required:
479473
- "pattern"
480474
- "scope"

0 commit comments

Comments
 (0)