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};
 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 settings::SettingsStore;
 831            SettingsStore::update_global(cx, |store, cx| {
 832                store.update_user_settings(cx, |settings| {
 833                    settings.project.worktree.file_scan_exclusions = Some(vec![
 834                        "**/.secretdir".to_string(),
 835                        "**/.mymetadata".to_string(),
 836                    ]);
 837                    settings.project.worktree.private_files = Some(
 838                        vec![
 839                            "**/.mysecrets".to_string(),
 840                            "**/*.privatekey".to_string(),
 841                            "**/*.mysensitive".to_string(),
 842                        ]
 843                        .into(),
 844                    );
 845                });
 846            });
 847        });
 848
 849        let project = Project::test(fs.clone(), [path!("/project_root").as_ref()], cx).await;
 850
 851        // Searching for files outside the project worktree should return no results
 852        let result = run_grep_tool(
 853            GrepToolInput {
 854                regex: "outside_function".to_string(),
 855                include_pattern: None,
 856                offset: 0,
 857                case_sensitive: false,
 858            },
 859            project.clone(),
 860            cx,
 861        )
 862        .await;
 863        let paths = extract_paths_from_results(&result);
 864        assert!(
 865            paths.is_empty(),
 866            "grep_tool should not find files outside the project worktree"
 867        );
 868
 869        // Searching within the project should succeed
 870        let result = run_grep_tool(
 871            GrepToolInput {
 872                regex: "main".to_string(),
 873                include_pattern: None,
 874                offset: 0,
 875                case_sensitive: false,
 876            },
 877            project.clone(),
 878            cx,
 879        )
 880        .await;
 881        let paths = extract_paths_from_results(&result);
 882        assert!(
 883            paths.iter().any(|p| p.contains("allowed_file.rs")),
 884            "grep_tool should be able to search files inside worktrees"
 885        );
 886
 887        // Searching files that match file_scan_exclusions should return no results
 888        let result = run_grep_tool(
 889            GrepToolInput {
 890                regex: "special_configuration".to_string(),
 891                include_pattern: None,
 892                offset: 0,
 893                case_sensitive: false,
 894            },
 895            project.clone(),
 896            cx,
 897        )
 898        .await;
 899        let paths = extract_paths_from_results(&result);
 900        assert!(
 901            paths.is_empty(),
 902            "grep_tool should not search files in .secretdir (file_scan_exclusions)"
 903        );
 904
 905        let result = run_grep_tool(
 906            GrepToolInput {
 907                regex: "custom_metadata".to_string(),
 908                include_pattern: None,
 909                offset: 0,
 910                case_sensitive: false,
 911            },
 912            project.clone(),
 913            cx,
 914        )
 915        .await;
 916        let paths = extract_paths_from_results(&result);
 917        assert!(
 918            paths.is_empty(),
 919            "grep_tool should not search .mymetadata files (file_scan_exclusions)"
 920        );
 921
 922        // Searching private files should return no results
 923        let result = run_grep_tool(
 924            GrepToolInput {
 925                regex: "SECRET_KEY".to_string(),
 926                include_pattern: None,
 927                offset: 0,
 928                case_sensitive: false,
 929            },
 930            project.clone(),
 931            cx,
 932        )
 933        .await;
 934        let paths = extract_paths_from_results(&result);
 935        assert!(
 936            paths.is_empty(),
 937            "grep_tool should not search .mysecrets (private_files)"
 938        );
 939
 940        let result = run_grep_tool(
 941            GrepToolInput {
 942                regex: "private_key_content".to_string(),
 943                include_pattern: None,
 944                offset: 0,
 945                case_sensitive: false,
 946            },
 947            project.clone(),
 948            cx,
 949        )
 950        .await;
 951        let paths = extract_paths_from_results(&result);
 952
 953        assert!(
 954            paths.is_empty(),
 955            "grep_tool should not search .privatekey files (private_files)"
 956        );
 957
 958        let result = run_grep_tool(
 959            GrepToolInput {
 960                regex: "sensitive_data".to_string(),
 961                include_pattern: None,
 962                offset: 0,
 963                case_sensitive: false,
 964            },
 965            project.clone(),
 966            cx,
 967        )
 968        .await;
 969        let paths = extract_paths_from_results(&result);
 970        assert!(
 971            paths.is_empty(),
 972            "grep_tool should not search .mysensitive files (private_files)"
 973        );
 974
 975        // Searching a normal file should still work, even with private_files configured
 976        let result = run_grep_tool(
 977            GrepToolInput {
 978                regex: "normal_file_content".to_string(),
 979                include_pattern: None,
 980                offset: 0,
 981                case_sensitive: false,
 982            },
 983            project.clone(),
 984            cx,
 985        )
 986        .await;
 987        let paths = extract_paths_from_results(&result);
 988        assert!(
 989            paths.iter().any(|p| p.contains("normal_file.rs")),
 990            "Should be able to search normal files"
 991        );
 992
 993        // Path traversal attempts with .. in include_pattern should not escape project
 994        let result = run_grep_tool(
 995            GrepToolInput {
 996                regex: "outside_function".to_string(),
 997                include_pattern: Some("../outside_project/**/*.rs".to_string()),
 998                offset: 0,
 999                case_sensitive: false,
1000            },
1001            project.clone(),
1002            cx,
1003        )
1004        .await;
1005        let paths = extract_paths_from_results(&result);
1006        assert!(
1007            paths.is_empty(),
1008            "grep_tool should not allow escaping project boundaries with relative paths"
1009        );
1010    }
1011
1012    #[gpui::test]
1013    async fn test_grep_with_multiple_worktree_settings(cx: &mut TestAppContext) {
1014        init_test(cx);
1015
1016        let fs = FakeFs::new(cx.executor());
1017
1018        // Create first worktree with its own private files
1019        fs.insert_tree(
1020            path!("/worktree1"),
1021            json!({
1022                ".zed": {
1023                    "settings.json": r#"{
1024                        "file_scan_exclusions": ["**/fixture.*"],
1025                        "private_files": ["**/secret.rs"]
1026                    }"#
1027                },
1028                "src": {
1029                    "main.rs": "fn main() { let secret_key = \"hidden\"; }",
1030                    "secret.rs": "const API_KEY: &str = \"secret_value\";",
1031                    "utils.rs": "pub fn get_config() -> String { \"config\".to_string() }"
1032                },
1033                "tests": {
1034                    "test.rs": "fn test_secret() { assert!(true); }",
1035                    "fixture.sql": "SELECT * FROM secret_table;"
1036                }
1037            }),
1038        )
1039        .await;
1040
1041        // Create second worktree with different private files
1042        fs.insert_tree(
1043            path!("/worktree2"),
1044            json!({
1045                ".zed": {
1046                    "settings.json": r#"{
1047                        "file_scan_exclusions": ["**/internal.*"],
1048                        "private_files": ["**/private.js", "**/data.json"]
1049                    }"#
1050                },
1051                "lib": {
1052                    "public.js": "export function getSecret() { return 'public'; }",
1053                    "private.js": "const SECRET_KEY = \"private_value\";",
1054                    "data.json": "{\"secret_data\": \"hidden\"}"
1055                },
1056                "docs": {
1057                    "README.md": "# Documentation with secret info",
1058                    "internal.md": "Internal secret documentation"
1059                }
1060            }),
1061        )
1062        .await;
1063
1064        // Set global settings
1065        cx.update(|cx| {
1066            SettingsStore::update_global(cx, |store, cx| {
1067                store.update_user_settings(cx, |settings| {
1068                    settings.project.worktree.file_scan_exclusions =
1069                        Some(vec!["**/.git".to_string(), "**/node_modules".to_string()]);
1070                    settings.project.worktree.private_files =
1071                        Some(vec!["**/.env".to_string()].into());
1072                });
1073            });
1074        });
1075
1076        let project = Project::test(
1077            fs.clone(),
1078            [path!("/worktree1").as_ref(), path!("/worktree2").as_ref()],
1079            cx,
1080        )
1081        .await;
1082
1083        // Wait for worktrees to be fully scanned
1084        cx.executor().run_until_parked();
1085
1086        // Search for "secret" - should exclude files based on worktree-specific settings
1087        let result = run_grep_tool(
1088            GrepToolInput {
1089                regex: "secret".to_string(),
1090                include_pattern: None,
1091                offset: 0,
1092                case_sensitive: false,
1093            },
1094            project.clone(),
1095            cx,
1096        )
1097        .await;
1098        let paths = extract_paths_from_results(&result);
1099
1100        // Should find matches in non-private files
1101        assert!(
1102            paths.iter().any(|p| p.contains("main.rs")),
1103            "Should find 'secret' in worktree1/src/main.rs"
1104        );
1105        assert!(
1106            paths.iter().any(|p| p.contains("test.rs")),
1107            "Should find 'secret' in worktree1/tests/test.rs"
1108        );
1109        assert!(
1110            paths.iter().any(|p| p.contains("public.js")),
1111            "Should find 'secret' in worktree2/lib/public.js"
1112        );
1113        assert!(
1114            paths.iter().any(|p| p.contains("README.md")),
1115            "Should find 'secret' in worktree2/docs/README.md"
1116        );
1117
1118        // Should NOT find matches in private/excluded files based on worktree settings
1119        assert!(
1120            !paths.iter().any(|p| p.contains("secret.rs")),
1121            "Should not search in worktree1/src/secret.rs (local private_files)"
1122        );
1123        assert!(
1124            !paths.iter().any(|p| p.contains("fixture.sql")),
1125            "Should not search in worktree1/tests/fixture.sql (local file_scan_exclusions)"
1126        );
1127        assert!(
1128            !paths.iter().any(|p| p.contains("private.js")),
1129            "Should not search in worktree2/lib/private.js (local private_files)"
1130        );
1131        assert!(
1132            !paths.iter().any(|p| p.contains("data.json")),
1133            "Should not search in worktree2/lib/data.json (local private_files)"
1134        );
1135        assert!(
1136            !paths.iter().any(|p| p.contains("internal.md")),
1137            "Should not search in worktree2/docs/internal.md (local file_scan_exclusions)"
1138        );
1139
1140        // Test with `include_pattern` specific to one worktree
1141        let result = run_grep_tool(
1142            GrepToolInput {
1143                regex: "secret".to_string(),
1144                include_pattern: Some("worktree1/**/*.rs".to_string()),
1145                offset: 0,
1146                case_sensitive: false,
1147            },
1148            project.clone(),
1149            cx,
1150        )
1151        .await;
1152
1153        let paths = extract_paths_from_results(&result);
1154
1155        // Should only find matches in worktree1 *.rs files (excluding private ones)
1156        assert!(
1157            paths.iter().any(|p| p.contains("main.rs")),
1158            "Should find match in worktree1/src/main.rs"
1159        );
1160        assert!(
1161            paths.iter().any(|p| p.contains("test.rs")),
1162            "Should find match in worktree1/tests/test.rs"
1163        );
1164        assert!(
1165            !paths.iter().any(|p| p.contains("secret.rs")),
1166            "Should not find match in excluded worktree1/src/secret.rs"
1167        );
1168        assert!(
1169            paths.iter().all(|p| !p.contains("worktree2")),
1170            "Should not find any matches in worktree2"
1171        );
1172    }
1173
1174    // Helper function to extract file paths from grep results
1175    fn extract_paths_from_results(results: &str) -> Vec<String> {
1176        results
1177            .lines()
1178            .filter(|line| line.starts_with("## Matches in "))
1179            .map(|line| {
1180                line.strip_prefix("## Matches in ")
1181                    .unwrap()
1182                    .trim()
1183                    .to_string()
1184            })
1185            .collect()
1186    }
1187}