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