Skip to main content

pixie_pi/tools/
grep.rs

1//! `grep` tool — search file contents (gitignore-aware, in-process).
2
3use std::collections::HashSet;
4use std::path::{Path, PathBuf};
5
6use async_trait::async_trait;
7use serde::Deserialize;
8use serde_json::{json, Value};
9use tokio_util::sync::CancellationToken;
10
11use crate::agent::tool::{AgentTool, ToolResult};
12use crate::tools::truncate::{
13    format_size, truncate_head, truncate_line, DEFAULT_MAX_BYTES, GREP_MAX_LINE_LENGTH,
14};
15use crate::tools::util::{display_path, resolve_to_cwd};
16
17const DEFAULT_MATCH_LIMIT: usize = 100;
18
19pub struct GrepTool {
20    pub cwd: PathBuf,
21}
22
23#[derive(Debug, Deserialize)]
24struct GrepInput {
25    pattern: String,
26    #[serde(default)]
27    path: Option<String>,
28    #[serde(default)]
29    glob: Option<String>,
30    #[serde(default)]
31    ignore_case: Option<bool>,
32    #[serde(default)]
33    literal: Option<bool>,
34    #[serde(default)]
35    context: Option<usize>,
36    #[serde(default)]
37    limit: Option<usize>,
38}
39
40#[async_trait]
41impl AgentTool for GrepTool {
42    fn name(&self) -> &str {
43        "grep"
44    }
45    fn description(&self) -> &str {
46        "Search file contents for a pattern. Returns matching lines with file paths and line numbers. Respects .gitignore. Output is truncated to 100 matches or 50KB (whichever is hit first). Long lines are truncated to 500 chars."
47    }
48    fn input_schema(&self) -> Value {
49        json!({
50            "type": "object",
51            "properties": {
52                "pattern": { "type": "string", "description": "Search pattern (regex or literal string)" },
53                "path": { "type": "string", "description": "Directory or file to search (default: current directory)" },
54                "glob": { "type": "string", "description": "Filter files by glob pattern, e.g. '*.ts' or '**/*.spec.ts'" },
55                "ignoreCase": { "type": "boolean", "description": "Case-insensitive search (default: false)" },
56                "literal": { "type": "boolean", "description": "Treat pattern as a literal string instead of regex (default: false)" },
57                "context": { "type": "number", "description": "Number of lines to show before and after each match (default: 0)" },
58                "limit": { "type": "number", "description": "Maximum number of matches to return (default: 100)" }
59            },
60            "required": ["pattern"]
61        })
62    }
63
64    async fn execute(&self, args: Value, _cancel: CancellationToken) -> anyhow::Result<ToolResult> {
65        let input: GrepInput = serde_json::from_value(args)?;
66        let effective_limit = input.limit.unwrap_or(DEFAULT_MATCH_LIMIT).max(1);
67        let context = input.context.unwrap_or(0);
68
69        let pattern = if input.literal.unwrap_or(false) {
70            regex::escape(&input.pattern)
71        } else {
72            input.pattern.clone()
73        };
74        let re = regex::RegexBuilder::new(&pattern)
75            .case_insensitive(input.ignore_case.unwrap_or(false))
76            .build()
77            .map_err(|e| anyhow::anyhow!("Invalid regex pattern: {e}"))?;
78
79        let search_path = resolve_to_cwd(input.path.as_deref().unwrap_or("."), &self.cwd);
80        if !search_path.exists() {
81            anyhow::bail!("Path not found: {}", display_path(input.path.as_deref().unwrap_or("."), &self.cwd));
82        }
83        let is_dir = search_path.is_dir();
84
85        let glob_matcher = match input.glob.as_deref() {
86            Some(g) => Some(
87                globset::Glob::new(g)
88                    .map_err(|e| anyhow::anyhow!("Invalid glob: {e}"))?
89                    .compile_matcher(),
90            ),
91            None => None,
92        };
93
94        let files = collect_files(&search_path);
95        let mut out_lines: Vec<String> = Vec::new();
96        let mut match_count = 0usize;
97        let mut match_limit_reached = false;
98        let mut lines_truncated = false;
99
100        'outer: for file in files {
101            if let Some(gm) = &glob_matcher {
102                let rel = file.strip_prefix(&search_path).unwrap_or(&file).to_string_lossy().to_string();
103                let base = file
104                    .file_name()
105                    .map(|n| n.to_string_lossy().to_string())
106                    .unwrap_or_default();
107                if !gm.is_match(&rel) && !gm.is_match(&base) {
108                    continue;
109                }
110            }
111            let Ok(text) = std::fs::read_to_string(&file) else {
112                continue;
113            };
114            let normalized = text.replace("\r\n", "\n").replace('\r', "\n");
115            let lines: Vec<&str> = normalized.split('\n').collect();
116            let display = display_for(&file, &search_path, is_dir);
117
118            // Collect this file's matching line indices first (honoring the
119            // global match limit) so context lines are emitted only once even
120            // when matches are close together. The previous per-match loop
121            // re-emitted overlapping context lines — and even re-emitted a
122            // shared match line — whenever two matches sat within `2*context`
123            // lines of each other.
124            let mut match_indices: Vec<usize> = Vec::new();
125            for (i, line) in lines.iter().enumerate() {
126                if re.is_match(line) {
127                    match_count += 1;
128                    match_indices.push(i);
129                    if match_count > effective_limit {
130                        match_limit_reached = true;
131                        break;
132                    }
133                }
134            }
135
136            // We probe one match past the limit so "exactly N matches" is
137            // distinguishable from "more than N" (the limit notice must not fire
138            // when nothing was actually held back). That extra match lands in
139            // `match_indices`; if it pushed us over the global limit, drop it —
140            // and any surplus in this file — so exactly `effective_limit` matches
141            // are emitted total. `match_count - match_indices.len()` is the number
142            // of matches already emitted from earlier files.
143            if match_limit_reached {
144                let emitted_before = match_count - match_indices.len();
145                match_indices.truncate(effective_limit.saturating_sub(emitted_before));
146            }
147
148            if context == 0 {
149                for &i in &match_indices {
150                    let (truncated, was) = truncate_line(lines[i], None);
151                    if was {
152                        lines_truncated = true;
153                    }
154                    out_lines.push(format!("{}:{}: {}", display, i + 1, truncated));
155                }
156            } else if !match_indices.is_empty() {
157                let is_match: HashSet<usize> = match_indices.iter().copied().collect();
158                let n = lines.len();
159                // Merge the [i-context, i+context] windows around each match
160                // into maximal disjoint runs (overlapping or adjacent windows
161                // coalesce), mirroring `grep -C`. Emit each covered line once —
162                // matches with ':', context with '-' — with a "--" separator
163                // between disjoint groups.
164                let mut groups: Vec<(usize, usize)> = Vec::new();
165                for &i in &match_indices {
166                    let start = i.saturating_sub(context);
167                    let end = (i + context).min(n.saturating_sub(1));
168                    match groups.last_mut() {
169                        Some((_, prev_end)) if start <= *prev_end + 1 => {
170                            *prev_end = (*prev_end).max(end);
171                        }
172                        _ => groups.push((start, end)),
173                    }
174                }
175                for (gi, (start, end)) in groups.iter().enumerate() {
176                    if gi > 0 {
177                        out_lines.push("--".to_string());
178                    }
179                    for (offset, line) in lines[*start..=*end].iter().enumerate() {
180                        let c = start + offset;
181                        let (truncated, was) = truncate_line(line, None);
182                        if was {
183                            lines_truncated = true;
184                        }
185                        if is_match.contains(&c) {
186                            out_lines.push(format!("{}:{}: {}", display, c + 1, truncated));
187                        } else {
188                            out_lines.push(format!("{}-{}- {}", display, c + 1, truncated));
189                        }
190                    }
191                }
192            }
193
194            if match_limit_reached {
195                break 'outer;
196            }
197        }
198
199        if match_count == 0 {
200            return Ok(ToolResult::text("No matches found"));
201        }
202
203        let raw = out_lines.join("\n");
204        let trunc = truncate_head(&raw, None, None);
205        let mut output = trunc.content;
206        let mut notices: Vec<String> = Vec::new();
207        if match_limit_reached {
208            notices.push(format!(
209                "{effective_limit} matches limit reached. Use limit={} for more, or refine pattern",
210                effective_limit * 2
211            ));
212        }
213        if trunc.truncated {
214            notices.push(format!("{} limit reached", format_size(DEFAULT_MAX_BYTES)));
215        }
216        if lines_truncated {
217            notices.push(format!(
218                "Some lines truncated to {GREP_MAX_LINE_LENGTH} chars. Use read tool to see full lines"
219            ));
220        }
221        if !notices.is_empty() {
222            output.push_str(&format!("\n\n[{}]", notices.join(". ")));
223        }
224
225        Ok(ToolResult::text(output))
226    }
227}
228
229/// Walk `root` (gitignore-aware, including hidden files) and return file paths.
230fn collect_files(root: &Path) -> Vec<PathBuf> {
231    let mut files = Vec::new();
232    if root.is_file() {
233        files.push(root.to_path_buf());
234        return files;
235    }
236    let walker = ignore::WalkBuilder::new(root)
237        .hidden(false)
238        .git_ignore(true)
239        .git_global(true)
240        .parents(true)
241        .build();
242    for entry in walker.flatten() {
243        if entry.file_type().map(|t| t.is_file()).unwrap_or(false) {
244            files.push(entry.into_path());
245        }
246    }
247    files
248}
249
250fn display_for(file: &Path, search_root: &Path, is_dir: bool) -> String {
251    if is_dir {
252        if let Ok(rel) = file.strip_prefix(search_root) {
253            let s = rel.to_string_lossy().replace('\\', "/");
254            if s.is_empty() {
255                return file
256                    .file_name()
257                    .map(|n| n.to_string_lossy().to_string())
258                    .unwrap_or_default();
259            }
260            return s;
261        }
262    }
263    file.file_name()
264        .map(|n| n.to_string_lossy().to_string())
265        .unwrap_or_default()
266}
267
268#[cfg(test)]
269mod tests {
270    use super::*;
271    use crate::agent::tool::AgentTool;
272    use tokio_util::sync::CancellationToken;
273
274    fn scratch_dir(label: &str) -> PathBuf {
275        let dir = std::env::temp_dir()
276            .join(format!("pi-grep-test-{label}-{}", uuid::Uuid::new_v4()));
277        std::fs::create_dir_all(&dir).unwrap();
278        dir
279    }
280
281    async fn run_grep(cwd: PathBuf, args: serde_json::Value) -> String {
282        let tool = GrepTool { cwd };
283        let res = tool.execute(args, CancellationToken::new()).await.unwrap();
284        match res.content.into_iter().next() {
285            Some(crate::ai::types::ToolResultContent::Text { text }) => text,
286            _ => panic!("expected text tool result"),
287        }
288    }
289
290    #[tokio::test]
291    async fn context_merges_overlapping_windows_no_duplicates() {
292        // Two matches on adjacent lines; with context=1 their windows overlap.
293        // The previous per-match loop emitted each covered line twice (a match
294        // line re-appeared as a "-" context line for its neighbor). The merged
295        // output must list every line exactly once.
296        let dir = scratch_dir("adjacent");
297        std::fs::write(dir.join("f.txt"), "a\nb\nMATCH\nMATCH\nc\nd\n").unwrap();
298
299        let out = run_grep(
300            dir.clone(),
301            serde_json::json!({ "pattern": "MATCH", "path": dir.to_string_lossy(), "context": 1 }),
302        )
303        .await;
304
305        let body: Vec<&str> = out.lines().filter(|l| !l.is_empty()).collect();
306        assert_eq!(
307            body,
308            vec![
309                "f.txt-2- b",
310                "f.txt:3: MATCH",
311                "f.txt:4: MATCH",
312                "f.txt-5- c",
313            ],
314            "adjacent matches should merge into one group with no duplicates"
315        );
316        // Coalesced groups carry no separator.
317        assert!(!out.contains("\n--\n"));
318
319        let _ = std::fs::remove_dir_all(&dir);
320    }
321
322    #[tokio::test]
323    async fn context_separates_disjoint_groups() {
324        // Two matches far apart produce two disjoint groups joined by "--",
325        // each line emitted once.
326        let dir = scratch_dir("disjoint");
327        std::fs::write(dir.join("f.txt"), "MATCH\nx\nx\nx\nx\nx\nx\nx\nMATCH\n").unwrap();
328
329        let out = run_grep(
330            dir.clone(),
331            serde_json::json!({ "pattern": "MATCH", "path": dir.to_string_lossy(), "context": 1 }),
332        )
333        .await;
334
335        assert!(
336            out.contains("\n--\n"),
337            "disjoint groups should be separated by --\n{out}"
338        );
339        // Each match line appears exactly once.
340        assert_eq!(out.matches(":1:").count(), 1);
341        assert_eq!(out.matches(":9:").count(), 1);
342
343        let _ = std::fs::remove_dir_all(&dir);
344    }
345
346    #[tokio::test]
347    async fn limit_not_reported_when_matches_equal_limit_exactly() {
348        // Exactly `limit` matches exist → all are shown, and the "limit reached"
349        // notice must NOT fire (nothing was held back).
350        let dir = scratch_dir("at-limit");
351        std::fs::write(dir.join("f.txt"), "MATCH\nMATCH\nMATCH\n").unwrap();
352
353        let out = run_grep(
354            dir.clone(),
355            serde_json::json!({ "pattern": "MATCH", "path": dir.to_string_lossy(), "limit": 3 }),
356        )
357        .await;
358
359        assert_eq!(out.matches("f.txt:").count(), 3, "all 3 matches shown");
360        assert!(
361            !out.contains("limit reached"),
362            "no false-positive limit notice\n{out}"
363        );
364
365        let _ = std::fs::remove_dir_all(&dir);
366    }
367
368    #[tokio::test]
369    async fn limit_reported_and_capped_when_matches_exceed_limit() {
370        // One more match than the limit → notice fires and exactly `limit`
371        // matches are emitted (no off-by-one in the straddling file).
372        let dir = scratch_dir("over-limit");
373        std::fs::write(dir.join("f.txt"), "MATCH\nMATCH\nMATCH\nMATCH\n").unwrap();
374
375        let out = run_grep(
376            dir.clone(),
377            serde_json::json!({ "pattern": "MATCH", "path": dir.to_string_lossy(), "limit": 3 }),
378        )
379        .await;
380
381        assert_eq!(out.matches("f.txt:").count(), 3, "exactly 3 matches shown");
382        assert!(
383            out.contains("limit reached"),
384            "truncation notice present\n{out}"
385        );
386
387        let _ = std::fs::remove_dir_all(&dir);
388    }
389
390    #[tokio::test]
391    async fn limit_straddling_two_files_emits_exactly_limit() {
392        // The limit is crossed inside the second file. Exactly `limit` matches
393        // must be emitted across both files — this is the case the per-file
394        // truncation math is really for.
395        let dir = scratch_dir("straddle");
396        std::fs::write(dir.join("a.txt"), "MATCH\nMATCH\n").unwrap();
397        std::fs::write(dir.join("b.txt"), "MATCH\nMATCH\n").unwrap();
398
399        let out = run_grep(
400            dir.clone(),
401            serde_json::json!({ "pattern": "MATCH", "path": dir.to_string_lossy(), "limit": 3 }),
402        )
403        .await;
404
405        assert_eq!(
406            out.matches(".txt:").count(),
407            3,
408            "exactly 3 matches across both files\n{out}"
409        );
410        assert!(
411            out.contains("limit reached"),
412            "truncation notice present\n{out}"
413        );
414
415        let _ = std::fs::remove_dir_all(&dir);
416    }
417}