grep_tool.rs

   1use crate::{AgentTool, ToolCallEventStream};
   2use agent_client_protocol as acp;
   3use anyhow::{Result, anyhow};
   4use futures::StreamExt;
   5use gpui::{App, Entity, SharedString, Task};
   6use language::{OffsetRangeExt, ParseStatus, Point};
   7use project::{
   8    Project, WorktreeSettings,
   9    search::{SearchQuery, SearchResult},
  10};
  11use schemars::JsonSchema;
  12use serde::{Deserialize, Serialize};
  13use settings::Settings;
  14use std::{cmp, fmt::Write, sync::Arc};
  15use util::RangeExt;
  16use util::markdown::MarkdownInlineCode;
  17use util::paths::PathMatcher;
  18
  19/// Searches the contents of files in the project with a regular expression
  20///
  21/// - Prefer this tool to path search when searching for symbols in the project, because you won't need to guess what path it's in.
  22/// - Supports full regex syntax (eg. "log.*Error", "function\\s+\\w+", etc.)
  23/// - Pass an `include_pattern` if you know how to narrow your search on the files system
  24/// - Never use this tool to search for paths. Only search file contents with this tool.
  25/// - Use this tool when you need to find files containing specific patterns
  26/// - Results are paginated with 20 matches per page. Use the optional 'offset' parameter to request subsequent pages.
  27/// - DO NOT use HTML entities solely to escape characters in the tool parameters.
  28#[derive(Debug, Serialize, Deserialize, JsonSchema)]
  29pub struct GrepToolInput {
  30    /// A regex pattern to search for in the entire project. Note that the regex will be parsed by the Rust `regex` crate.
  31    ///
  32    /// Do NOT specify a path here! This will only be matched against the code **content**.
  33    pub regex: String,
  34    /// A glob pattern for the paths of files to include in the search.
  35    /// Supports standard glob patterns like "**/*.rs" or "frontend/src/**/*.ts".
  36    /// If omitted, all files in the project will be searched.
  37    ///
  38    /// The glob pattern is matched against the full path including the project root directory.
  39    ///
  40    /// <example>
  41    /// If the project has the following root directories:
  42    ///
  43    /// - /a/b/backend
  44    /// - /c/d/frontend
  45    ///
  46    /// Use "backend/**/*.rs" to search only Rust files in the backend root directory.
  47    /// Use "frontend/src/**/*.ts" to search TypeScript files only in the frontend root directory (sub-directory "src").
  48    /// Use "**/*.rs" to search Rust files across all root directories.
  49    /// </example>
  50    pub include_pattern: Option<String>,
  51    /// Optional starting position for paginated results (0-based).
  52    /// When not provided, starts from the beginning.
  53    #[serde(default)]
  54    pub offset: u32,
  55    /// Whether the regex is case-sensitive. Defaults to false (case-insensitive).
  56    #[serde(default)]
  57    pub case_sensitive: bool,
  58}
  59
  60impl GrepToolInput {
  61    /// Which page of search results this is.
  62    pub fn page(&self) -> u32 {
  63        1 + (self.offset / RESULTS_PER_PAGE)
  64    }
  65}
  66
  67const RESULTS_PER_PAGE: u32 = 20;
  68
  69pub struct GrepTool {
  70    project: Entity<Project>,
  71}
  72
  73impl GrepTool {
  74    pub fn new(project: Entity<Project>) -> Self {
  75        Self { project }
  76    }
  77}
  78
  79impl AgentTool for GrepTool {
  80    type Input = GrepToolInput;
  81    type Output = String;
  82
  83    fn name() -> &'static str {
  84        "grep"
  85    }
  86
  87    fn kind() -> acp::ToolKind {
  88        acp::ToolKind::Search
  89    }
  90
  91    fn initial_title(
  92        &self,
  93        input: Result<Self::Input, serde_json::Value>,
  94        _cx: &mut App,
  95    ) -> SharedString {
  96        match input {
  97            Ok(input) => {
  98                let page = input.page();
  99                let regex_str = MarkdownInlineCode(&input.regex);
 100                let case_info = if input.case_sensitive {
 101                    " (case-sensitive)"
 102                } else {
 103                    ""
 104                };
 105
 106                if page > 1 {
 107                    format!("Get page {page} of search results for regex {regex_str}{case_info}")
 108                } else {
 109                    format!("Search files for regex {regex_str}{case_info}")
 110                }
 111            }
 112            Err(_) => "Search with regex".into(),
 113        }
 114        .into()
 115    }
 116
 117    fn run(
 118        self: Arc<Self>,
 119        input: Self::Input,
 120        _event_stream: ToolCallEventStream,
 121        cx: &mut App,
 122    ) -> Task<Result<Self::Output>> {
 123        const CONTEXT_LINES: u32 = 2;
 124        const MAX_ANCESTOR_LINES: u32 = 10;
 125
 126        let path_style = self.project.read(cx).path_style(cx);
 127
 128        let include_matcher = match PathMatcher::new(
 129            input
 130                .include_pattern
 131                .as_ref()
 132                .into_iter()
 133                .collect::<Vec<_>>(),
 134            path_style,
 135        ) {
 136            Ok(matcher) => matcher,
 137            Err(error) => {
 138                return Task::ready(Err(anyhow!("invalid include glob pattern: {error}")));
 139            }
 140        };
 141
 142        // Exclude global file_scan_exclusions and private_files settings
 143        let exclude_matcher = {
 144            let global_settings = WorktreeSettings::get_global(cx);
 145            let exclude_patterns = global_settings
 146                .file_scan_exclusions
 147                .sources()
 148                .chain(global_settings.private_files.sources());
 149
 150            match PathMatcher::new(exclude_patterns, path_style) {
 151                Ok(matcher) => matcher,
 152                Err(error) => {
 153                    return Task::ready(Err(anyhow!("invalid exclude pattern: {error}")));
 154                }
 155            }
 156        };
 157
 158        let query = match SearchQuery::regex(
 159            &input.regex,
 160            false,
 161            input.case_sensitive,
 162            false,
 163            false,
 164            include_matcher,
 165            exclude_matcher,
 166            true, // Always match file include pattern against *full project paths* that start with a project root.
 167            None,
 168        ) {
 169            Ok(query) => query,
 170            Err(error) => return Task::ready(Err(error)),
 171        };
 172
 173        let results = self
 174            .project
 175            .update(cx, |project, cx| project.search(query, cx));
 176
 177        let project = self.project.downgrade();
 178        cx.spawn(async move |cx|  {
 179            futures::pin_mut!(results);
 180
 181            let mut output = String::new();
 182            let mut skips_remaining = input.offset;
 183            let mut matches_found = 0;
 184            let mut has_more_matches = false;
 185
 186            'outer: while let Some(SearchResult::Buffer { buffer, ranges }) = results.next().await {
 187                if ranges.is_empty() {
 188                    continue;
 189                }
 190
 191                let Ok((Some(path), mut parse_status)) = buffer.read_with(cx, |buffer, cx| {
 192                    (buffer.file().map(|file| file.full_path(cx)), buffer.parse_status())
 193                }) else {
 194                    continue;
 195                };
 196
 197                // Check if this file should be excluded based on its worktree settings
 198                if let Ok(Some(project_path)) = project.read_with(cx, |project, cx| {
 199                    project.find_project_path(&path, cx)
 200                })
 201                    && cx.update(|cx| {
 202                        let worktree_settings = WorktreeSettings::get(Some((&project_path).into()), cx);
 203                        worktree_settings.is_path_excluded(&project_path.path)
 204                            || worktree_settings.is_path_private(&project_path.path)
 205                    }).unwrap_or(false) {
 206                        continue;
 207                    }
 208
 209                while *parse_status.borrow() != ParseStatus::Idle {
 210                    parse_status.changed().await?;
 211                }
 212
 213                let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot())?;
 214
 215                let mut ranges = ranges
 216                    .into_iter()
 217                    .map(|range| {
 218                        let matched = range.to_point(&snapshot);
 219                        let matched_end_line_len = snapshot.line_len(matched.end.row);
 220                        let full_lines = Point::new(matched.start.row, 0)..Point::new(matched.end.row, matched_end_line_len);
 221                        let symbols = snapshot.symbols_containing(matched.start, None);
 222
 223                        if let Some(ancestor_node) = snapshot.syntax_ancestor(full_lines.clone()) {
 224                            let full_ancestor_range = ancestor_node.byte_range().to_point(&snapshot);
 225                            let end_row = full_ancestor_range.end.row.min(full_ancestor_range.start.row + MAX_ANCESTOR_LINES);
 226                            let end_col = snapshot.line_len(end_row);
 227                            let capped_ancestor_range = Point::new(full_ancestor_range.start.row, 0)..Point::new(end_row, end_col);
 228
 229                            if capped_ancestor_range.contains_inclusive(&full_lines) {
 230                                return (capped_ancestor_range, Some(full_ancestor_range), symbols)
 231                            }
 232                        }
 233
 234                        let mut matched = matched;
 235                        matched.start.column = 0;
 236                        matched.start.row =
 237                            matched.start.row.saturating_sub(CONTEXT_LINES);
 238                        matched.end.row = cmp::min(
 239                            snapshot.max_point().row,
 240                            matched.end.row + CONTEXT_LINES,
 241                        );
 242                        matched.end.column = snapshot.line_len(matched.end.row);
 243
 244                        (matched, None, symbols)
 245                    })
 246                    .peekable();
 247
 248                let mut file_header_written = false;
 249
 250                while let Some((mut range, ancestor_range, parent_symbols)) = ranges.next(){
 251                    if skips_remaining > 0 {
 252                        skips_remaining -= 1;
 253                        continue;
 254                    }
 255
 256                    // We'd already found a full page of matches, and we just found one more.
 257                    if matches_found >= RESULTS_PER_PAGE {
 258                        has_more_matches = true;
 259                        break 'outer;
 260                    }
 261
 262                    while let Some((next_range, _, _)) = ranges.peek() {
 263                        if range.end.row >= next_range.start.row {
 264                            range.end = next_range.end;
 265                            ranges.next();
 266                        } else {
 267                            break;
 268                        }
 269                    }
 270
 271                    if !file_header_written {
 272                        writeln!(output, "\n## Matches in {}", path.display())?;
 273                        file_header_written = true;
 274                    }
 275
 276                    let end_row = range.end.row;
 277                    output.push_str("\n### ");
 278
 279                    for symbol in parent_symbols {
 280                        write!(output, "{} › ", symbol.text)?;
 281                    }
 282
 283                    if range.start.row == end_row {
 284                        writeln!(output, "L{}", range.start.row + 1)?;
 285                    } else {
 286                        writeln!(output, "L{}-{}", range.start.row + 1, end_row + 1)?;
 287                    }
 288
 289                    output.push_str("```\n");
 290                    output.extend(snapshot.text_for_range(range));
 291                    output.push_str("\n```\n");
 292
 293                    if let Some(ancestor_range) = ancestor_range
 294                        && end_row < ancestor_range.end.row {
 295                            let remaining_lines = ancestor_range.end.row - end_row;
 296                            writeln!(output, "\n{} lines remaining in ancestor node. Read the file to see all.", remaining_lines)?;
 297                        }
 298
 299                    matches_found += 1;
 300                }
 301            }
 302
 303            if matches_found == 0 {
 304                Ok("No matches found".into())
 305            } else if has_more_matches {
 306                Ok(format!(
 307                    "Showing matches {}-{} (there were more matches found; use offset: {} to see next page):\n{output}",
 308                    input.offset + 1,
 309                    input.offset + matches_found,
 310                    input.offset + RESULTS_PER_PAGE,
 311                ))
 312            } else {
 313                Ok(format!("Found {matches_found} matches:\n{output}"))
 314            }
 315        })
 316    }
 317}
 318
 319#[cfg(test)]
 320mod tests {
 321    use crate::ToolCallEventStream;
 322
 323    use super::*;
 324    use gpui::{TestAppContext, UpdateGlobal};
 325    use project::{FakeFs, Project};
 326    use serde_json::json;
 327    use settings::SettingsStore;
 328    use unindent::Unindent;
 329    use util::path;
 330
 331    #[gpui::test]
 332    async fn test_grep_tool_with_include_pattern(cx: &mut TestAppContext) {
 333        init_test(cx);
 334        cx.executor().allow_parking();
 335
 336        let fs = FakeFs::new(cx.executor());
 337        fs.insert_tree(
 338            path!("/root"),
 339            serde_json::json!({
 340                "src": {
 341                    "main.rs": "fn main() {\n    println!(\"Hello, world!\");\n}",
 342                    "utils": {
 343                        "helper.rs": "fn helper() {\n    println!(\"I'm a helper!\");\n}",
 344                    },
 345                },
 346                "tests": {
 347                    "test_main.rs": "fn test_main() {\n    assert!(true);\n}",
 348                }
 349            }),
 350        )
 351        .await;
 352
 353        let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
 354
 355        // Test with include pattern for Rust files inside the root of the project
 356        let input = GrepToolInput {
 357            regex: "println".to_string(),
 358            include_pattern: Some("root/**/*.rs".to_string()),
 359            offset: 0,
 360            case_sensitive: false,
 361        };
 362
 363        let result = run_grep_tool(input, project.clone(), cx).await;
 364        assert!(result.contains("main.rs"), "Should find matches in main.rs");
 365        assert!(
 366            result.contains("helper.rs"),
 367            "Should find matches in helper.rs"
 368        );
 369        assert!(
 370            !result.contains("test_main.rs"),
 371            "Should not include test_main.rs even though it's a .rs file (because it doesn't have the pattern)"
 372        );
 373
 374        // Test with include pattern for src directory only
 375        let input = GrepToolInput {
 376            regex: "fn".to_string(),
 377            include_pattern: Some("root/**/src/**".to_string()),
 378            offset: 0,
 379            case_sensitive: false,
 380        };
 381
 382        let result = run_grep_tool(input, project.clone(), cx).await;
 383        assert!(
 384            result.contains("main.rs"),
 385            "Should find matches in src/main.rs"
 386        );
 387        assert!(
 388            result.contains("helper.rs"),
 389            "Should find matches in src/utils/helper.rs"
 390        );
 391        assert!(
 392            !result.contains("test_main.rs"),
 393            "Should not include test_main.rs as it's not in src directory"
 394        );
 395
 396        // Test with empty include pattern (should default to all files)
 397        let input = GrepToolInput {
 398            regex: "fn".to_string(),
 399            include_pattern: None,
 400            offset: 0,
 401            case_sensitive: false,
 402        };
 403
 404        let result = run_grep_tool(input, project.clone(), cx).await;
 405        assert!(result.contains("main.rs"), "Should find matches in main.rs");
 406        assert!(
 407            result.contains("helper.rs"),
 408            "Should find matches in helper.rs"
 409        );
 410        assert!(
 411            result.contains("test_main.rs"),
 412            "Should include test_main.rs"
 413        );
 414    }
 415
 416    #[gpui::test]
 417    async fn test_grep_tool_with_case_sensitivity(cx: &mut TestAppContext) {
 418        init_test(cx);
 419        cx.executor().allow_parking();
 420
 421        let fs = FakeFs::new(cx.executor());
 422        fs.insert_tree(
 423            path!("/root"),
 424            serde_json::json!({
 425                "case_test.txt": "This file has UPPERCASE and lowercase text.\nUPPERCASE patterns should match only with case_sensitive: true",
 426            }),
 427        )
 428        .await;
 429
 430        let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
 431
 432        // Test case-insensitive search (default)
 433        let input = GrepToolInput {
 434            regex: "uppercase".to_string(),
 435            include_pattern: Some("**/*.txt".to_string()),
 436            offset: 0,
 437            case_sensitive: false,
 438        };
 439
 440        let result = run_grep_tool(input, project.clone(), cx).await;
 441        assert!(
 442            result.contains("UPPERCASE"),
 443            "Case-insensitive search should match uppercase"
 444        );
 445
 446        // Test case-sensitive search
 447        let input = GrepToolInput {
 448            regex: "uppercase".to_string(),
 449            include_pattern: Some("**/*.txt".to_string()),
 450            offset: 0,
 451            case_sensitive: true,
 452        };
 453
 454        let result = run_grep_tool(input, project.clone(), cx).await;
 455        assert!(
 456            !result.contains("UPPERCASE"),
 457            "Case-sensitive search should not match uppercase"
 458        );
 459
 460        // Test case-sensitive search
 461        let input = GrepToolInput {
 462            regex: "LOWERCASE".to_string(),
 463            include_pattern: Some("**/*.txt".to_string()),
 464            offset: 0,
 465            case_sensitive: true,
 466        };
 467
 468        let result = run_grep_tool(input, project.clone(), cx).await;
 469
 470        assert!(
 471            !result.contains("lowercase"),
 472            "Case-sensitive search should match lowercase"
 473        );
 474
 475        // Test case-sensitive search for lowercase pattern
 476        let input = GrepToolInput {
 477            regex: "lowercase".to_string(),
 478            include_pattern: Some("**/*.txt".to_string()),
 479            offset: 0,
 480            case_sensitive: true,
 481        };
 482
 483        let result = run_grep_tool(input, project.clone(), cx).await;
 484        assert!(
 485            result.contains("lowercase"),
 486            "Case-sensitive search should match lowercase text"
 487        );
 488    }
 489
 490    /// Helper function to set up a syntax test environment
 491    async fn setup_syntax_test(cx: &mut TestAppContext) -> Entity<Project> {
 492        use unindent::Unindent;
 493        init_test(cx);
 494        cx.executor().allow_parking();
 495
 496        let fs = FakeFs::new(cx.executor());
 497
 498        // Create test file with syntax structures
 499        fs.insert_tree(
 500            path!("/root"),
 501            serde_json::json!({
 502                "test_syntax.rs": r#"
 503                    fn top_level_function() {
 504                        println!("This is at the top level");
 505                    }
 506
 507                    mod feature_module {
 508                        pub mod nested_module {
 509                            pub fn nested_function(
 510                                first_arg: String,
 511                                second_arg: i32,
 512                            ) {
 513                                println!("Function in nested module");
 514                                println!("{first_arg}");
 515                                println!("{second_arg}");
 516                            }
 517                        }
 518                    }
 519
 520                    struct MyStruct {
 521                        field1: String,
 522                        field2: i32,
 523                    }
 524
 525                    impl MyStruct {
 526                        fn method_with_block() {
 527                            let condition = true;
 528                            if condition {
 529                                println!("Inside if block");
 530                            }
 531                        }
 532
 533                        fn long_function() {
 534                            println!("Line 1");
 535                            println!("Line 2");
 536                            println!("Line 3");
 537                            println!("Line 4");
 538                            println!("Line 5");
 539                            println!("Line 6");
 540                            println!("Line 7");
 541                            println!("Line 8");
 542                            println!("Line 9");
 543                            println!("Line 10");
 544                            println!("Line 11");
 545                            println!("Line 12");
 546                        }
 547                    }
 548
 549                    trait Processor {
 550                        fn process(&self, input: &str) -> String;
 551                    }
 552
 553                    impl Processor for MyStruct {
 554                        fn process(&self, input: &str) -> String {
 555                            format!("Processed: {}", input)
 556                        }
 557                    }
 558                "#.unindent().trim(),
 559            }),
 560        )
 561        .await;
 562
 563        let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
 564
 565        project.update(cx, |project, _cx| {
 566            project.languages().add(language::rust_lang())
 567        });
 568
 569        project
 570    }
 571
 572    #[gpui::test]
 573    async fn test_grep_top_level_function(cx: &mut TestAppContext) {
 574        let project = setup_syntax_test(cx).await;
 575
 576        // Test: Line at the top level of the file
 577        let input = GrepToolInput {
 578            regex: "This is at the top level".to_string(),
 579            include_pattern: Some("**/*.rs".to_string()),
 580            offset: 0,
 581            case_sensitive: false,
 582        };
 583
 584        let result = run_grep_tool(input, project.clone(), cx).await;
 585        let expected = r#"
 586            Found 1 matches:
 587
 588            ## Matches in root/test_syntax.rs
 589
 590            ### fn top_level_function › L1-3
 591            ```
 592            fn top_level_function() {
 593                println!("This is at the top level");
 594            }
 595            ```
 596            "#
 597        .unindent();
 598        assert_eq!(result, expected);
 599    }
 600
 601    #[gpui::test]
 602    async fn test_grep_function_body(cx: &mut TestAppContext) {
 603        let project = setup_syntax_test(cx).await;
 604
 605        // Test: Line inside a function body
 606        let input = GrepToolInput {
 607            regex: "Function in nested module".to_string(),
 608            include_pattern: Some("**/*.rs".to_string()),
 609            offset: 0,
 610            case_sensitive: false,
 611        };
 612
 613        let result = run_grep_tool(input, project.clone(), cx).await;
 614        let expected = r#"
 615            Found 1 matches:
 616
 617            ## Matches in root/test_syntax.rs
 618
 619            ### mod feature_module › pub mod nested_module › pub fn nested_function › L10-14
 620            ```
 621                    ) {
 622                        println!("Function in nested module");
 623                        println!("{first_arg}");
 624                        println!("{second_arg}");
 625                    }
 626            ```
 627            "#
 628        .unindent();
 629        assert_eq!(result, expected);
 630    }
 631
 632    #[gpui::test]
 633    async fn test_grep_function_args_and_body(cx: &mut TestAppContext) {
 634        let project = setup_syntax_test(cx).await;
 635
 636        // Test: Line with a function argument
 637        let input = GrepToolInput {
 638            regex: "second_arg".to_string(),
 639            include_pattern: Some("**/*.rs".to_string()),
 640            offset: 0,
 641            case_sensitive: false,
 642        };
 643
 644        let result = run_grep_tool(input, project.clone(), cx).await;
 645        let expected = r#"
 646            Found 1 matches:
 647
 648            ## Matches in root/test_syntax.rs
 649
 650            ### mod feature_module › pub mod nested_module › pub fn nested_function › L7-14
 651            ```
 652                    pub fn nested_function(
 653                        first_arg: String,
 654                        second_arg: i32,
 655                    ) {
 656                        println!("Function in nested module");
 657                        println!("{first_arg}");
 658                        println!("{second_arg}");
 659                    }
 660            ```
 661            "#
 662        .unindent();
 663        assert_eq!(result, expected);
 664    }
 665
 666    #[gpui::test]
 667    async fn test_grep_if_block(cx: &mut TestAppContext) {
 668        use unindent::Unindent;
 669        let project = setup_syntax_test(cx).await;
 670
 671        // Test: Line inside an if block
 672        let input = GrepToolInput {
 673            regex: "Inside if block".to_string(),
 674            include_pattern: Some("**/*.rs".to_string()),
 675            offset: 0,
 676            case_sensitive: false,
 677        };
 678
 679        let result = run_grep_tool(input, project.clone(), cx).await;
 680        let expected = r#"
 681            Found 1 matches:
 682
 683            ## Matches in root/test_syntax.rs
 684
 685            ### impl MyStruct › fn method_with_block › L26-28
 686            ```
 687                    if condition {
 688                        println!("Inside if block");
 689                    }
 690            ```
 691            "#
 692        .unindent();
 693        assert_eq!(result, expected);
 694    }
 695
 696    #[gpui::test]
 697    async fn test_grep_long_function_top(cx: &mut TestAppContext) {
 698        use unindent::Unindent;
 699        let project = setup_syntax_test(cx).await;
 700
 701        // Test: Line in the middle of a long function - should show message about remaining lines
 702        let input = GrepToolInput {
 703            regex: "Line 5".to_string(),
 704            include_pattern: Some("**/*.rs".to_string()),
 705            offset: 0,
 706            case_sensitive: false,
 707        };
 708
 709        let result = run_grep_tool(input, project.clone(), cx).await;
 710        let expected = r#"
 711            Found 1 matches:
 712
 713            ## Matches in root/test_syntax.rs
 714
 715            ### impl MyStruct › fn long_function › L31-41
 716            ```
 717                fn long_function() {
 718                    println!("Line 1");
 719                    println!("Line 2");
 720                    println!("Line 3");
 721                    println!("Line 4");
 722                    println!("Line 5");
 723                    println!("Line 6");
 724                    println!("Line 7");
 725                    println!("Line 8");
 726                    println!("Line 9");
 727                    println!("Line 10");
 728            ```
 729
 730            3 lines remaining in ancestor node. Read the file to see all.
 731            "#
 732        .unindent();
 733        assert_eq!(result, expected);
 734    }
 735
 736    #[gpui::test]
 737    async fn test_grep_long_function_bottom(cx: &mut TestAppContext) {
 738        use unindent::Unindent;
 739        let project = setup_syntax_test(cx).await;
 740
 741        // Test: Line in the long function
 742        let input = GrepToolInput {
 743            regex: "Line 12".to_string(),
 744            include_pattern: Some("**/*.rs".to_string()),
 745            offset: 0,
 746            case_sensitive: false,
 747        };
 748
 749        let result = run_grep_tool(input, project.clone(), cx).await;
 750        let expected = r#"
 751            Found 1 matches:
 752
 753            ## Matches in root/test_syntax.rs
 754
 755            ### impl MyStruct › fn long_function › L41-45
 756            ```
 757                    println!("Line 10");
 758                    println!("Line 11");
 759                    println!("Line 12");
 760                }
 761            }
 762            ```
 763            "#
 764        .unindent();
 765        assert_eq!(result, expected);
 766    }
 767
 768    async fn run_grep_tool(
 769        input: GrepToolInput,
 770        project: Entity<Project>,
 771        cx: &mut TestAppContext,
 772    ) -> String {
 773        let tool = Arc::new(GrepTool { project });
 774        let task = cx.update(|cx| tool.run(input, ToolCallEventStream::test().0, cx));
 775
 776        match task.await {
 777            Ok(result) => {
 778                if cfg!(windows) {
 779                    result.replace("root\\", "root/")
 780                } else {
 781                    result
 782                }
 783            }
 784            Err(e) => panic!("Failed to run grep tool: {}", e),
 785        }
 786    }
 787
 788    fn init_test(cx: &mut TestAppContext) {
 789        cx.update(|cx| {
 790            let settings_store = SettingsStore::test(cx);
 791            cx.set_global(settings_store);
 792        });
 793    }
 794
 795    #[gpui::test]
 796    async fn test_grep_security_boundaries(cx: &mut TestAppContext) {
 797        init_test(cx);
 798
 799        let fs = FakeFs::new(cx.executor());
 800
 801        fs.insert_tree(
 802            path!("/"),
 803            json!({
 804                "project_root": {
 805                    "allowed_file.rs": "fn main() { println!(\"This file is in the project\"); }",
 806                    ".mysecrets": "SECRET_KEY=abc123\nfn secret() { /* private */ }",
 807                    ".secretdir": {
 808                        "config": "fn special_configuration() { /* excluded */ }"
 809                    },
 810                    ".mymetadata": "fn custom_metadata() { /* excluded */ }",
 811                    "subdir": {
 812                        "normal_file.rs": "fn normal_file_content() { /* Normal */ }",
 813                        "special.privatekey": "fn private_key_content() { /* private */ }",
 814                        "data.mysensitive": "fn sensitive_data() { /* private */ }"
 815                    }
 816                },
 817                "outside_project": {
 818                    "sensitive_file.rs": "fn outside_function() { /* This file is outside the project */ }"
 819                }
 820            }),
 821        )
 822        .await;
 823
 824        cx.update(|cx| {
 825            use gpui::UpdateGlobal;
 826            use settings::SettingsStore;
 827            SettingsStore::update_global(cx, |store, cx| {
 828                store.update_user_settings(cx, |settings| {
 829                    settings.project.worktree.file_scan_exclusions = Some(vec![
 830                        "**/.secretdir".to_string(),
 831                        "**/.mymetadata".to_string(),
 832                    ]);
 833                    settings.project.worktree.private_files = Some(
 834                        vec![
 835                            "**/.mysecrets".to_string(),
 836                            "**/*.privatekey".to_string(),
 837                            "**/*.mysensitive".to_string(),
 838                        ]
 839                        .into(),
 840                    );
 841                });
 842            });
 843        });
 844
 845        let project = Project::test(fs.clone(), [path!("/project_root").as_ref()], cx).await;
 846
 847        // Searching for files outside the project worktree should return no results
 848        let result = run_grep_tool(
 849            GrepToolInput {
 850                regex: "outside_function".to_string(),
 851                include_pattern: None,
 852                offset: 0,
 853                case_sensitive: false,
 854            },
 855            project.clone(),
 856            cx,
 857        )
 858        .await;
 859        let paths = extract_paths_from_results(&result);
 860        assert!(
 861            paths.is_empty(),
 862            "grep_tool should not find files outside the project worktree"
 863        );
 864
 865        // Searching within the project should succeed
 866        let result = run_grep_tool(
 867            GrepToolInput {
 868                regex: "main".to_string(),
 869                include_pattern: None,
 870                offset: 0,
 871                case_sensitive: false,
 872            },
 873            project.clone(),
 874            cx,
 875        )
 876        .await;
 877        let paths = extract_paths_from_results(&result);
 878        assert!(
 879            paths.iter().any(|p| p.contains("allowed_file.rs")),
 880            "grep_tool should be able to search files inside worktrees"
 881        );
 882
 883        // Searching files that match file_scan_exclusions should return no results
 884        let result = run_grep_tool(
 885            GrepToolInput {
 886                regex: "special_configuration".to_string(),
 887                include_pattern: None,
 888                offset: 0,
 889                case_sensitive: false,
 890            },
 891            project.clone(),
 892            cx,
 893        )
 894        .await;
 895        let paths = extract_paths_from_results(&result);
 896        assert!(
 897            paths.is_empty(),
 898            "grep_tool should not search files in .secretdir (file_scan_exclusions)"
 899        );
 900
 901        let result = run_grep_tool(
 902            GrepToolInput {
 903                regex: "custom_metadata".to_string(),
 904                include_pattern: None,
 905                offset: 0,
 906                case_sensitive: false,
 907            },
 908            project.clone(),
 909            cx,
 910        )
 911        .await;
 912        let paths = extract_paths_from_results(&result);
 913        assert!(
 914            paths.is_empty(),
 915            "grep_tool should not search .mymetadata files (file_scan_exclusions)"
 916        );
 917
 918        // Searching private files should return no results
 919        let result = run_grep_tool(
 920            GrepToolInput {
 921                regex: "SECRET_KEY".to_string(),
 922                include_pattern: None,
 923                offset: 0,
 924                case_sensitive: false,
 925            },
 926            project.clone(),
 927            cx,
 928        )
 929        .await;
 930        let paths = extract_paths_from_results(&result);
 931        assert!(
 932            paths.is_empty(),
 933            "grep_tool should not search .mysecrets (private_files)"
 934        );
 935
 936        let result = run_grep_tool(
 937            GrepToolInput {
 938                regex: "private_key_content".to_string(),
 939                include_pattern: None,
 940                offset: 0,
 941                case_sensitive: false,
 942            },
 943            project.clone(),
 944            cx,
 945        )
 946        .await;
 947        let paths = extract_paths_from_results(&result);
 948
 949        assert!(
 950            paths.is_empty(),
 951            "grep_tool should not search .privatekey files (private_files)"
 952        );
 953
 954        let result = run_grep_tool(
 955            GrepToolInput {
 956                regex: "sensitive_data".to_string(),
 957                include_pattern: None,
 958                offset: 0,
 959                case_sensitive: false,
 960            },
 961            project.clone(),
 962            cx,
 963        )
 964        .await;
 965        let paths = extract_paths_from_results(&result);
 966        assert!(
 967            paths.is_empty(),
 968            "grep_tool should not search .mysensitive files (private_files)"
 969        );
 970
 971        // Searching a normal file should still work, even with private_files configured
 972        let result = run_grep_tool(
 973            GrepToolInput {
 974                regex: "normal_file_content".to_string(),
 975                include_pattern: None,
 976                offset: 0,
 977                case_sensitive: false,
 978            },
 979            project.clone(),
 980            cx,
 981        )
 982        .await;
 983        let paths = extract_paths_from_results(&result);
 984        assert!(
 985            paths.iter().any(|p| p.contains("normal_file.rs")),
 986            "Should be able to search normal files"
 987        );
 988
 989        // Path traversal attempts with .. in include_pattern should not escape project
 990        let result = run_grep_tool(
 991            GrepToolInput {
 992                regex: "outside_function".to_string(),
 993                include_pattern: Some("../outside_project/**/*.rs".to_string()),
 994                offset: 0,
 995                case_sensitive: false,
 996            },
 997            project.clone(),
 998            cx,
 999        )
1000        .await;
1001        let paths = extract_paths_from_results(&result);
1002        assert!(
1003            paths.is_empty(),
1004            "grep_tool should not allow escaping project boundaries with relative paths"
1005        );
1006    }
1007
1008    #[gpui::test]
1009    async fn test_grep_with_multiple_worktree_settings(cx: &mut TestAppContext) {
1010        init_test(cx);
1011
1012        let fs = FakeFs::new(cx.executor());
1013
1014        // Create first worktree with its own private files
1015        fs.insert_tree(
1016            path!("/worktree1"),
1017            json!({
1018                ".zed": {
1019                    "settings.json": r#"{
1020                        "file_scan_exclusions": ["**/fixture.*"],
1021                        "private_files": ["**/secret.rs"]
1022                    }"#
1023                },
1024                "src": {
1025                    "main.rs": "fn main() { let secret_key = \"hidden\"; }",
1026                    "secret.rs": "const API_KEY: &str = \"secret_value\";",
1027                    "utils.rs": "pub fn get_config() -> String { \"config\".to_string() }"
1028                },
1029                "tests": {
1030                    "test.rs": "fn test_secret() { assert!(true); }",
1031                    "fixture.sql": "SELECT * FROM secret_table;"
1032                }
1033            }),
1034        )
1035        .await;
1036
1037        // Create second worktree with different private files
1038        fs.insert_tree(
1039            path!("/worktree2"),
1040            json!({
1041                ".zed": {
1042                    "settings.json": r#"{
1043                        "file_scan_exclusions": ["**/internal.*"],
1044                        "private_files": ["**/private.js", "**/data.json"]
1045                    }"#
1046                },
1047                "lib": {
1048                    "public.js": "export function getSecret() { return 'public'; }",
1049                    "private.js": "const SECRET_KEY = \"private_value\";",
1050                    "data.json": "{\"secret_data\": \"hidden\"}"
1051                },
1052                "docs": {
1053                    "README.md": "# Documentation with secret info",
1054                    "internal.md": "Internal secret documentation"
1055                }
1056            }),
1057        )
1058        .await;
1059
1060        // Set global settings
1061        cx.update(|cx| {
1062            SettingsStore::update_global(cx, |store, cx| {
1063                store.update_user_settings(cx, |settings| {
1064                    settings.project.worktree.file_scan_exclusions =
1065                        Some(vec!["**/.git".to_string(), "**/node_modules".to_string()]);
1066                    settings.project.worktree.private_files =
1067                        Some(vec!["**/.env".to_string()].into());
1068                });
1069            });
1070        });
1071
1072        let project = Project::test(
1073            fs.clone(),
1074            [path!("/worktree1").as_ref(), path!("/worktree2").as_ref()],
1075            cx,
1076        )
1077        .await;
1078
1079        // Wait for worktrees to be fully scanned
1080        cx.executor().run_until_parked();
1081
1082        // Search for "secret" - should exclude files based on worktree-specific settings
1083        let result = run_grep_tool(
1084            GrepToolInput {
1085                regex: "secret".to_string(),
1086                include_pattern: None,
1087                offset: 0,
1088                case_sensitive: false,
1089            },
1090            project.clone(),
1091            cx,
1092        )
1093        .await;
1094        let paths = extract_paths_from_results(&result);
1095
1096        // Should find matches in non-private files
1097        assert!(
1098            paths.iter().any(|p| p.contains("main.rs")),
1099            "Should find 'secret' in worktree1/src/main.rs"
1100        );
1101        assert!(
1102            paths.iter().any(|p| p.contains("test.rs")),
1103            "Should find 'secret' in worktree1/tests/test.rs"
1104        );
1105        assert!(
1106            paths.iter().any(|p| p.contains("public.js")),
1107            "Should find 'secret' in worktree2/lib/public.js"
1108        );
1109        assert!(
1110            paths.iter().any(|p| p.contains("README.md")),
1111            "Should find 'secret' in worktree2/docs/README.md"
1112        );
1113
1114        // Should NOT find matches in private/excluded files based on worktree settings
1115        assert!(
1116            !paths.iter().any(|p| p.contains("secret.rs")),
1117            "Should not search in worktree1/src/secret.rs (local private_files)"
1118        );
1119        assert!(
1120            !paths.iter().any(|p| p.contains("fixture.sql")),
1121            "Should not search in worktree1/tests/fixture.sql (local file_scan_exclusions)"
1122        );
1123        assert!(
1124            !paths.iter().any(|p| p.contains("private.js")),
1125            "Should not search in worktree2/lib/private.js (local private_files)"
1126        );
1127        assert!(
1128            !paths.iter().any(|p| p.contains("data.json")),
1129            "Should not search in worktree2/lib/data.json (local private_files)"
1130        );
1131        assert!(
1132            !paths.iter().any(|p| p.contains("internal.md")),
1133            "Should not search in worktree2/docs/internal.md (local file_scan_exclusions)"
1134        );
1135
1136        // Test with `include_pattern` specific to one worktree
1137        let result = run_grep_tool(
1138            GrepToolInput {
1139                regex: "secret".to_string(),
1140                include_pattern: Some("worktree1/**/*.rs".to_string()),
1141                offset: 0,
1142                case_sensitive: false,
1143            },
1144            project.clone(),
1145            cx,
1146        )
1147        .await;
1148
1149        let paths = extract_paths_from_results(&result);
1150
1151        // Should only find matches in worktree1 *.rs files (excluding private ones)
1152        assert!(
1153            paths.iter().any(|p| p.contains("main.rs")),
1154            "Should find match in worktree1/src/main.rs"
1155        );
1156        assert!(
1157            paths.iter().any(|p| p.contains("test.rs")),
1158            "Should find match in worktree1/tests/test.rs"
1159        );
1160        assert!(
1161            !paths.iter().any(|p| p.contains("secret.rs")),
1162            "Should not find match in excluded worktree1/src/secret.rs"
1163        );
1164        assert!(
1165            paths.iter().all(|p| !p.contains("worktree2")),
1166            "Should not find any matches in worktree2"
1167        );
1168    }
1169
1170    // Helper function to extract file paths from grep results
1171    fn extract_paths_from_results(results: &str) -> Vec<String> {
1172        results
1173            .lines()
1174            .filter(|line| line.starts_with("## Matches in "))
1175            .map(|line| {
1176                line.strip_prefix("## Matches in ")
1177                    .unwrap()
1178                    .trim()
1179                    .to_string()
1180            })
1181            .collect()
1182    }
1183}