From 5563622557ac60b6842f6c09c4c76a5ece9f6d09 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 15:27:16 +0100 Subject: 0.4.0: grep takes a regular expression, fixed=true for literals Invalid patterns explain themselves and point at fixed, rather than failing with a bare regex error the model cannot act on. --- CHANGELOG.md | 9 ++++++++ Cargo.lock | 45 +++++++++++++++++++++++++++++++++++++--- Cargo.toml | 3 ++- src/tools/fs.rs | 63 ++++++++++++++++++++++++++++++++++++++++++-------------- src/tools/mod.rs | 7 ++++--- tests/tools.rs | 48 ++++++++++++++++++++++++++++++++++++++++++ 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. file, and the tool schemas the model sees. Anything a script or a resumed session could depend on counts as API. +## 0.4.0 + +### Changed +- `grep` takes a regular expression instead of a literal string, matching what + coding agents expect from a search tool. An invalid pattern explains itself and + points at `fixed`. +- `grep` accepts `fixed: true` to match literal text, e.g. when searching for + `a+b` or `foo(`. + ## 0.3.0 ### Added diff --git a/Cargo.lock b/Cargo.lock index cc479a6..d2dc605 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,15 +10,25 @@ checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" [[package]] name = "agent686" -version = "0.3.0" +version = "0.4.0" dependencies = [ "crossterm", "libc", + "regex", "serde", "serde_json", "ureq", ] +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + [[package]] name = "base64" version = "0.23.1" @@ -136,7 +146,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e35d6e78f4cebb90e51d543db8fe41528851d6deeef6ef21ebd4bfdc1e784776" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -312,6 +322,35 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + [[package]] name = "ring" version = "0.17.14" @@ -345,7 +384,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index bc7c25d..d531c4b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "agent686" -version = "0.3.0" +version = "0.4.0" edition = "2021" description = "A small coding agent for a 32-bit Debian box" publish = false @@ -11,6 +11,7 @@ serde_json = "1" ureq = { version = "3", features = ["rustls"] } libc = "0.2" crossterm = "0.29.0" +regex = "1.13.1" [profile.release] strip = 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( } } +enum Matcher { + Literal { needle: String, ignore_case: bool }, + Regex(regex::Regex), +} + +impl Matcher { + fn is_match(&self, line: &str) -> bool { + match self { + Matcher::Literal { + needle, + ignore_case, + } => { + if *ignore_case { + line.to_lowercase().contains(needle) + } else { + line.contains(needle) + } + } + Matcher::Regex(regex) => regex.is_match(line), + } + } +} + pub fn grep(args: &Value) -> Result { let pattern = req(args, "pattern")?; if pattern.is_empty() { @@ -282,10 +305,29 @@ pub fn grep(args: &Value) -> Result { .get("ignore_case") .and_then(|v| v.as_bool()) .unwrap_or(false); - let needle = if ignore_case { - pattern.to_lowercase() + let fixed = args.get("fixed").and_then(|v| v.as_bool()).unwrap_or(false); + + let matcher = if fixed { + Matcher::Literal { + needle: if ignore_case { + pattern.to_lowercase() + } else { + pattern.clone() + }, + ignore_case, + } } else { - pattern.clone() + Matcher::Regex( + regex::RegexBuilder::new(&pattern) + .case_insensitive(ignore_case) + .build() + .map_err(|error| { + format!( + "invalid regex `{pattern}`: {error}\npass \"fixed\": true to match \ + the text literally" + ) + })?, + ) }; let root = Path::new(&base); @@ -303,7 +345,7 @@ pub fn grep(args: &Value) -> Result { if matches >= MAX_MATCHES { break; } - grep_file(&file, &needle, ignore_case, &mut matches, &mut out); + grep_file(&file, &matcher, &mut matches, &mut out); } if matches == 0 { @@ -368,7 +410,7 @@ pub fn glob(args: &Value) -> Result { Ok(out) } -fn grep_file(path: &Path, needle_lower: &str, ignore_case: bool, count: &mut usize, out: &mut String) { +fn grep_file(path: &Path, matcher: &Matcher, count: &mut usize, out: &mut String) { let meta = match fs::metadata(path) { Ok(m) => m, Err(_) => return, @@ -384,21 +426,12 @@ fn grep_file(path: &Path, needle_lower: &str, ignore_case: bool, count: &mut usi return; } let text = String::from_utf8_lossy(&bytes); - let mut needle = needle_lower.to_string(); - if !ignore_case { - needle = needle.to_string(); - } for (index, line) in text.lines().enumerate() { if *count >= MAX_MATCHES { return; } - let hit = if ignore_case { - line.to_lowercase().contains(&needle) - } else { - line.contains(&needle) - }; - if hit { + if matcher.is_match(line) { *count += 1; out.push_str(&format!( "{}:{}: {}\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 { "type": "function", "function": { "name": "grep", - "description": "Search file contents for a literal string, recursively. Skips .git, target, node_modules and similar.", + "description": "Search file contents recursively with a regular expression. Skips .git, target, node_modules and similar. Set fixed=true to match literal text instead.", "parameters": { "type": "object", "properties": { - "pattern": { "type": "string" }, + "pattern": { "type": "string", "description": "regular expression, or literal text when fixed=true" }, "path": { "type": "string", "description": "file or directory (default .)" }, - "ignore_case": { "type": "boolean" } + "ignore_case": { "type": "boolean" }, + "fixed": { "type": "boolean", "description": "treat pattern as literal text" } }, "required": ["pattern"] } 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() { assert!(text.contains(" 2\ttwo"), "{text}"); assert!(!text.contains("continue with offset"), "{text}"); } + +#[test] +fn grep_uses_regular_expressions_by_default() { + let dir = temp_dir("grep-regex"); + fs::write( + dir.join("f.rs"), + "fn main() {\n fn other() {\n}\n", + ) + .unwrap(); + + let text = tools::run("grep", &json!({ "pattern": r"fn\s+main", "path": dir })).unwrap(); + assert_eq!(text.lines().count(), 1, "{text}"); + + let text = tools::run("grep", &json!({ "pattern": r"fn\s+\w+", "path": dir })).unwrap(); + assert_eq!(text.lines().count(), 2, "{text}"); + + let text = tools::run( + "grep", + &json!({ "pattern": r"FN\s+MAIN", "path": dir, "ignore_case": true }), + ) + .unwrap(); + assert_eq!(text.lines().count(), 1, "{text}"); +} + +#[test] +fn grep_fixed_matches_literally_and_bad_patterns_explain_themselves() { + let dir = temp_dir("grep-fixed"); + fs::write(dir.join("f.txt"), "cost is a+b\nand ab\n").unwrap(); + + // As a regex, `a+b` matches the "ab" on line 2, not the literal "a+b". + let text = tools::run("grep", &json!({ "pattern": "a+b", "path": dir })).unwrap(); + assert_eq!(text.lines().count(), 1, "{text}"); + assert!(text.contains("and ab"), "{text}"); + + let text = tools::run( + "grep", + &json!({ "pattern": "a+b", "path": dir, "fixed": true }), + ) + .unwrap(); + assert_eq!(text.lines().count(), 1, "{text}"); + assert!(text.contains("cost is a+b"), "{text}"); + + let error = tools::run("grep", &json!({ "pattern": "(", "path": dir })) + .err() + .expect("invalid regex must fail"); + assert!(error.contains("invalid regex"), "{error}"); + assert!(error.contains("fixed"), "{error}"); +} -- cgit v1.2.3