aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--CHANGELOG.md9
-rw-r--r--Cargo.lock45
-rw-r--r--Cargo.toml3
-rw-r--r--src/tools/fs.rs63
-rw-r--r--src/tools/mod.rs7
-rw-r--r--tests/tools.rs48
6 files changed, 153 insertions, 22 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d5a2d0b..f20bc9a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,15 @@ break the interface, **PATCH** for bug fixes.
7file, and the tool schemas the model sees. Anything a script or a resumed session 7file, and the tool schemas the model sees. Anything a script or a resumed session
8could depend on counts as API. 8could depend on counts as API.
9 9
10## 0.4.0
11
12### Changed
13- `grep` takes a regular expression instead of a literal string, matching what
14 coding agents expect from a search tool. An invalid pattern explains itself and
15 points at `fixed`.
16- `grep` accepts `fixed: true` to match literal text, e.g. when searching for
17 `a+b` or `foo(`.
18
10## 0.3.0 19## 0.3.0
11 20
12### Added 21### Added
diff --git a/Cargo.lock b/Cargo.lock
index cc479a6..d2dc605 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -10,16 +10,26 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
10 10
11[[package]] 11[[package]]
12name = "agent686" 12name = "agent686"
13version = "0.3.0" 13version = "0.4.0"
14dependencies = [ 14dependencies = [
15 "crossterm", 15 "crossterm",
16 "libc", 16 "libc",
17 "regex",
17 "serde", 18 "serde",
18 "serde_json", 19 "serde_json",
19 "ureq", 20 "ureq",
20] 21]
21 22
22[[package]] 23[[package]]
24name = "aho-corasick"
25version = "1.1.5"
26source = "registry+https://github.com/rust-lang/crates.io-index"
27checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba"
28dependencies = [
29 "memchr",
30]
31
32[[package]]
23name = "base64" 33name = "base64"
24version = "0.23.1" 34version = "0.23.1"
25source = "registry+https://github.com/rust-lang/crates.io-index" 35source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -136,7 +146,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
136checksum = "e35d6e78f4cebb90e51d543db8fe41528851d6deeef6ef21ebd4bfdc1e784776" 146checksum = "e35d6e78f4cebb90e51d543db8fe41528851d6deeef6ef21ebd4bfdc1e784776"
137dependencies = [ 147dependencies = [
138 "libc", 148 "libc",
139 "windows-sys 0.52.0", 149 "windows-sys 0.61.2",
140] 150]
141 151
142[[package]] 152[[package]]
@@ -313,6 +323,35 @@ dependencies = [
313] 323]
314 324
315[[package]] 325[[package]]
326name = "regex"
327version = "1.13.1"
328source = "registry+https://github.com/rust-lang/crates.io-index"
329checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d"
330dependencies = [
331 "aho-corasick",
332 "memchr",
333 "regex-automata",
334 "regex-syntax",
335]
336
337[[package]]
338name = "regex-automata"
339version = "0.4.18"
340source = "registry+https://github.com/rust-lang/crates.io-index"
341checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2"
342dependencies = [
343 "aho-corasick",
344 "memchr",
345 "regex-syntax",
346]
347
348[[package]]
349name = "regex-syntax"
350version = "0.8.11"
351source = "registry+https://github.com/rust-lang/crates.io-index"
352checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4"
353
354[[package]]
316name = "ring" 355name = "ring"
317version = "0.17.14" 356version = "0.17.14"
318source = "registry+https://github.com/rust-lang/crates.io-index" 357source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -345,7 +384,7 @@ dependencies = [
345 "errno", 384 "errno",
346 "libc", 385 "libc",
347 "linux-raw-sys", 386 "linux-raw-sys",
348 "windows-sys 0.52.0", 387 "windows-sys 0.61.2",
349] 388]
350 389
351[[package]] 390[[package]]
diff --git a/Cargo.toml b/Cargo.toml
index bc7c25d..d531c4b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
1[package] 1[package]
2name = "agent686" 2name = "agent686"
3version = "0.3.0" 3version = "0.4.0"
4edition = "2021" 4edition = "2021"
5description = "A small coding agent for a 32-bit Debian box" 5description = "A small coding agent for a 32-bit Debian box"
6publish = false 6publish = false
@@ -11,6 +11,7 @@ serde_json = "1"
11ureq = { version = "3", features = ["rustls"] } 11ureq = { version = "3", features = ["rustls"] }
12libc = "0.2" 12libc = "0.2"
13crossterm = "0.29.0" 13crossterm = "0.29.0"
14regex = "1.13.1"
14 15
15[profile.release] 16[profile.release]
16strip = true 17strip = true
diff --git a/src/tools/fs.rs b/src/tools/fs.rs
index e543cb6..caa3992 100644
--- a/src/tools/fs.rs
+++ b/src/tools/fs.rs
@@ -268,6 +268,29 @@ fn read_line_limited<R: BufRead>(
268 } 268 }
269} 269}
270 270
271enum Matcher {
272 Literal { needle: String, ignore_case: bool },
273 Regex(regex::Regex),
274}
275
276impl Matcher {
277 fn is_match(&self, line: &str) -> bool {
278 match self {
279 Matcher::Literal {
280 needle,
281 ignore_case,
282 } => {
283 if *ignore_case {
284 line.to_lowercase().contains(needle)
285 } else {
286 line.contains(needle)
287 }
288 }
289 Matcher::Regex(regex) => regex.is_match(line),
290 }
291 }
292}
293
271pub fn grep(args: &Value) -> Result<String, String> { 294pub fn grep(args: &Value) -> Result<String, String> {
272 let pattern = req(args, "pattern")?; 295 let pattern = req(args, "pattern")?;
273 if pattern.is_empty() { 296 if pattern.is_empty() {
@@ -282,10 +305,29 @@ pub fn grep(args: &Value) -> Result<String, String> {
282 .get("ignore_case") 305 .get("ignore_case")
283 .and_then(|v| v.as_bool()) 306 .and_then(|v| v.as_bool())
284 .unwrap_or(false); 307 .unwrap_or(false);
285 let needle = if ignore_case { 308 let fixed = args.get("fixed").and_then(|v| v.as_bool()).unwrap_or(false);
286 pattern.to_lowercase() 309
310 let matcher = if fixed {
311 Matcher::Literal {
312 needle: if ignore_case {
313 pattern.to_lowercase()
314 } else {
315 pattern.clone()
316 },
317 ignore_case,
318 }
287 } else { 319 } else {
288 pattern.clone() 320 Matcher::Regex(
321 regex::RegexBuilder::new(&pattern)
322 .case_insensitive(ignore_case)
323 .build()
324 .map_err(|error| {
325 format!(
326 "invalid regex `{pattern}`: {error}\npass \"fixed\": true to match \
327 the text literally"
328 )
329 })?,
330 )
289 }; 331 };
290 332
291 let root = Path::new(&base); 333 let root = Path::new(&base);
@@ -303,7 +345,7 @@ pub fn grep(args: &Value) -> Result<String, String> {
303 if matches >= MAX_MATCHES { 345 if matches >= MAX_MATCHES {
304 break; 346 break;
305 } 347 }
306 grep_file(&file, &needle, ignore_case, &mut matches, &mut out); 348 grep_file(&file, &matcher, &mut matches, &mut out);
307 } 349 }
308 350
309 if matches == 0 { 351 if matches == 0 {
@@ -368,7 +410,7 @@ pub fn glob(args: &Value) -> Result<String, String> {
368 Ok(out) 410 Ok(out)
369} 411}
370 412
371fn grep_file(path: &Path, needle_lower: &str, ignore_case: bool, count: &mut usize, out: &mut String) { 413fn grep_file(path: &Path, matcher: &Matcher, count: &mut usize, out: &mut String) {
372 let meta = match fs::metadata(path) { 414 let meta = match fs::metadata(path) {
373 Ok(m) => m, 415 Ok(m) => m,
374 Err(_) => return, 416 Err(_) => return,
@@ -384,21 +426,12 @@ fn grep_file(path: &Path, needle_lower: &str, ignore_case: bool, count: &mut usi
384 return; 426 return;
385 } 427 }
386 let text = String::from_utf8_lossy(&bytes); 428 let text = String::from_utf8_lossy(&bytes);
387 let mut needle = needle_lower.to_string();
388 if !ignore_case {
389 needle = needle.to_string();
390 }
391 429
392 for (index, line) in text.lines().enumerate() { 430 for (index, line) in text.lines().enumerate() {
393 if *count >= MAX_MATCHES { 431 if *count >= MAX_MATCHES {
394 return; 432 return;
395 } 433 }
396 let hit = if ignore_case { 434 if matcher.is_match(line) {
397 line.to_lowercase().contains(&needle)
398 } else {
399 line.contains(&needle)
400 };
401 if hit {
402 *count += 1; 435 *count += 1;
403 out.push_str(&format!( 436 out.push_str(&format!(
404 "{}:{}: {}\n", 437 "{}:{}: {}\n",
diff --git a/src/tools/mod.rs b/src/tools/mod.rs
index 503de08..7a1a26d 100644
--- a/src/tools/mod.rs
+++ b/src/tools/mod.rs
@@ -117,13 +117,14 @@ pub fn schemas() -> Vec<Value> {
117 "type": "function", 117 "type": "function",
118 "function": { 118 "function": {
119 "name": "grep", 119 "name": "grep",
120 "description": "Search file contents for a literal string, recursively. Skips .git, target, node_modules and similar.", 120 "description": "Search file contents recursively with a regular expression. Skips .git, target, node_modules and similar. Set fixed=true to match literal text instead.",
121 "parameters": { 121 "parameters": {
122 "type": "object", 122 "type": "object",
123 "properties": { 123 "properties": {
124 "pattern": { "type": "string" }, 124 "pattern": { "type": "string", "description": "regular expression, or literal text when fixed=true" },
125 "path": { "type": "string", "description": "file or directory (default .)" }, 125 "path": { "type": "string", "description": "file or directory (default .)" },
126 "ignore_case": { "type": "boolean" } 126 "ignore_case": { "type": "boolean" },
127 "fixed": { "type": "boolean", "description": "treat pattern as literal text" }
127 }, 128 },
128 "required": ["pattern"] 129 "required": ["pattern"]
129 } 130 }
diff --git a/tests/tools.rs b/tests/tools.rs
index 3df353f..8463d8d 100644
--- a/tests/tools.rs
+++ b/tests/tools.rs
@@ -201,3 +201,51 @@ fn read_handles_a_missing_trailing_newline() {
201 assert!(text.contains(" 2\ttwo"), "{text}"); 201 assert!(text.contains(" 2\ttwo"), "{text}");
202 assert!(!text.contains("continue with offset"), "{text}"); 202 assert!(!text.contains("continue with offset"), "{text}");
203} 203}
204
205#[test]
206fn grep_uses_regular_expressions_by_default() {
207 let dir = temp_dir("grep-regex");
208 fs::write(
209 dir.join("f.rs"),
210 "fn main() {\n fn other() {\n}\n",
211 )
212 .unwrap();
213
214 let text = tools::run("grep", &json!({ "pattern": r"fn\s+main", "path": dir })).unwrap();
215 assert_eq!(text.lines().count(), 1, "{text}");
216
217 let text = tools::run("grep", &json!({ "pattern": r"fn\s+\w+", "path": dir })).unwrap();
218 assert_eq!(text.lines().count(), 2, "{text}");
219
220 let text = tools::run(
221 "grep",
222 &json!({ "pattern": r"FN\s+MAIN", "path": dir, "ignore_case": true }),
223 )
224 .unwrap();
225 assert_eq!(text.lines().count(), 1, "{text}");
226}
227
228#[test]
229fn grep_fixed_matches_literally_and_bad_patterns_explain_themselves() {
230 let dir = temp_dir("grep-fixed");
231 fs::write(dir.join("f.txt"), "cost is a+b\nand ab\n").unwrap();
232
233 // As a regex, `a+b` matches the "ab" on line 2, not the literal "a+b".
234 let text = tools::run("grep", &json!({ "pattern": "a+b", "path": dir })).unwrap();
235 assert_eq!(text.lines().count(), 1, "{text}");
236 assert!(text.contains("and ab"), "{text}");
237
238 let text = tools::run(
239 "grep",
240 &json!({ "pattern": "a+b", "path": dir, "fixed": true }),
241 )
242 .unwrap();
243 assert_eq!(text.lines().count(), 1, "{text}");
244 assert!(text.contains("cost is a+b"), "{text}");
245
246 let error = tools::run("grep", &json!({ "pattern": "(", "path": dir }))
247 .err()
248 .expect("invalid regex must fail");
249 assert!(error.contains("invalid regex"), "{error}");
250 assert!(error.contains("fixed"), "{error}");
251}