codescout 0.14.0

High-performance coding agent toolkit MCP server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! `grep` tool and related format helpers.

use anyhow::Result;
use serde_json::{json, Value};

use super::format::format_overflow;
use super::{optional_u64_param, OutputForm, RecoverableError, Tool, ToolContext};

// ── grep ───────────────────────────────────────────────────────

pub struct Grep;

#[async_trait::async_trait]
impl Tool for Grep {
    fn name(&self) -> &str {
        "grep"
    }

    fn description(&self) -> &str {
        "Regex search across files. Returns matching lines with location. Pass context_lines for surrounding code."
    }

    fn input_schema(&self) -> Value {
        json!({
            "type": "object",
            "required": ["pattern"],
            "properties": {
                "pattern": { "type": "string", "description": "Regex pattern" },
                "path": { "type": "string", "description": "File or directory (default: project root)" },
                "limit": { "type": "integer", "default": 50, "description": "Max matching lines" },
                "context_lines": { "type": "integer", "default": 0, "description": "Context lines before/after each match (max 20). Adjacent matches merge." }
            }
        })
    }

    async fn call(&self, input: Value, ctx: &ToolContext) -> Result<Value> {
        let pattern = super::require_str_param_or(&input, "pattern", &["query", "regex"])?;
        let raw_path = strip_buffer_ref_quotes(input["path"].as_str().unwrap_or("."));

        // Buffer ref (@tool_*, @cmd_*, @file_*): search the cached content
        // instead of treating the ref as a filesystem path.
        if raw_path.starts_with('@') {
            let mut input = input.clone();
            input["path"] = serde_json::json!(raw_path);
            return grep_in_buffer(&input, ctx).await;
        }

        let project_root = ctx.agent.project_root().await;
        let security = ctx.agent.security_config().await;
        let search_path = crate::util::path_security::validate_read_path(
            raw_path,
            project_root.as_deref(),
            &security,
        )?;
        let max = optional_u64_param(&input, "limit").unwrap_or(50) as usize;
        let context_lines = optional_u64_param(&input, "context_lines")
            .unwrap_or(0)
            .min(20) as usize;
        let (re, is_literal_fallback) = match regex::RegexBuilder::new(pattern)
            .size_limit(1 << 20)
            .dfa_size_limit(1 << 20)
            .build()
        {
            Ok(re) => (re, false),
            Err(e) => {
                if super::is_regex_like(pattern) {
                    // User intended regex but it's broken — keep the error
                    return Err(RecoverableError::with_hint(
                        format!("invalid regex: {e}"),
                        "patterns are full regex syntax — escape metacharacters like \\( \\. \\[ for literals",
                    )
                    .into());
                }
                // Plain text with metacharacters — search literally
                let escaped = regex::escape(pattern);
                let re = regex::RegexBuilder::new(&escaped)
                    .size_limit(1 << 20)
                    .dfa_size_limit(1 << 20)
                    .build()
                    .map_err(|e2| {
                        RecoverableError::with_hint(
                            format!("invalid pattern even after escaping: {e2}"),
                            format!("original error: {e}"),
                        )
                    })?;
                (re, true)
            }
        };
        let mut matches: Vec<Value> = vec![];
        let mut total_match_count = 0usize;
        let mut hit_cap = false;

        let walker = ignore::WalkBuilder::new(&search_path)
            .hidden(true)
            .git_ignore(true)
            .build();
        'outer: for entry in walker.flatten() {
            if !entry.file_type().map(|t| t.is_file()).unwrap_or(false) {
                continue;
            }
            let Ok(text) = std::fs::read_to_string(entry.path()) else {
                continue;
            };

            if context_lines == 0 {
                // Original behaviour: one entry per matching line
                for (i, line) in text.lines().enumerate() {
                    if re.is_match(line) {
                        total_match_count += 1;
                        matches.push(json!({
                            "file": entry.path().display().to_string(),
                            "line": i + 1,
                            "content": line
                        }));
                        if matches.len() >= max {
                            hit_cap = true;
                            break 'outer;
                        }
                    }
                }
            } else {
                // Context mode: merge overlapping windows into blocks
                let file_lines: Vec<&str> = text.lines().collect();
                let n = file_lines.len();
                // (block_start_idx, match_indices, block_end_idx) — all 0-indexed
                let mut current: Option<(usize, Vec<usize>, usize)> = None;

                for (i, line) in file_lines.iter().enumerate() {
                    if !re.is_match(line) {
                        continue;
                    }
                    total_match_count += 1;
                    let ctx_start = i.saturating_sub(context_lines);
                    let ctx_end = (i + context_lines).min(n.saturating_sub(1));

                    match current.take() {
                        None => {
                            current = Some((ctx_start, vec![i], ctx_end));
                        }
                        Some((blk_start, mut blk_matches, blk_end)) => {
                            if ctx_start <= blk_end + 1 {
                                // Overlapping or adjacent: extend block, append match
                                blk_matches.push(i);
                                current = Some((blk_start, blk_matches, ctx_end.max(blk_end)));
                            } else {
                                // Non-overlapping: emit finished block, start new one
                                let content = file_lines[blk_start..=blk_end].join("\n");
                                let match_lines: Vec<u64> =
                                    blk_matches.iter().map(|&m| (m + 1) as u64).collect();
                                matches.push(json!({
                                    "file": entry.path().display().to_string(),
                                    "match_lines": match_lines,
                                    "start_line": blk_start + 1,
                                    "content": content,
                                }));
                                current = Some((ctx_start, vec![i], ctx_end));
                            }
                        }
                    }

                    if total_match_count >= max {
                        hit_cap = true;
                        break;
                    }
                }

                // Emit the last in-flight block
                if let Some((blk_start, blk_matches, blk_end)) = current {
                    let content = file_lines[blk_start..=blk_end].join("\n");
                    let match_lines: Vec<u64> =
                        blk_matches.iter().map(|&m| (m + 1) as u64).collect();
                    matches.push(json!({
                        "file": entry.path().display().to_string(),
                        "match_lines": match_lines,
                        "start_line": blk_start + 1,
                        "content": content,
                    }));
                }

                if total_match_count >= max {
                    hit_cap = true;
                    break 'outer;
                }
            }
        }

        // In context mode, matches contains merged blocks — fewer than total_match_count
        // (which counts individual matching lines). Report blocks so `total` == `matches.len()`.
        let shown_count = if context_lines > 0 {
            matches.len()
        } else {
            total_match_count
        };

        // Build grouped output (simple mode) or keep flat (context mode).
        let mut result = if context_lines == 0 {
            use crate::tools::file_group::{cap_grouped, group_by_file, groups_to_json};
            let budget = max;
            let (visible, total, files) = cap_grouped(matches, budget);
            let truncated = hit_cap || total > visible.len();
            let groups = group_by_file(&visible);
            let file_groups = groups_to_json(&groups);
            let mut r = json!({
                "file_groups": file_groups,
                "total": total,
                "files": files,
            });
            if truncated {
                r["overflow"] = json!({
                    "shown": visible.len(),
                    "total": total,
                    "hint": "Many matches. Narrow the pattern or use a more specific path.",
                });
            }
            r
        } else {
            // Context mode: keep flat matches[], preserve legacy shape for format_grep
            let mut r =
                json!({ "matches": matches, "total": shown_count, "context_lines": context_lines });
            if hit_cap {
                r["overflow"] = json!({
                    "shown": shown_count,
                    "hint": format!(
                        "Showing first {} matches (cap hit). Narrow with a more specific pattern or path=<file>.",
                        shown_count
                    )
                });
            }
            r
        };

        if is_literal_fallback {
            result["mode"] = json!("literal_fallback");
            result["reason"] = json!("pattern was not valid regex — searched as literal text");
        }
        if crate::util::path_security::is_identifier_pattern(pattern) {
            let name = pattern.split('|').next().unwrap_or(pattern);
            result["suggestion"] = json!(format!(
                "Pattern looks like a symbol name. Consider: \
                 symbols(name='{name}') for declarations, \
                 references(symbol='{name}') for direct callers, \
                 call_graph(symbol='{name}', direction='callers') for transitive blast radius."
            ));
        }
        Ok(result)
    }

    fn format_compact(&self, result: &Value) -> Option<String> {
        Some(format_grep(result))
    }

    fn output_form(&self) -> OutputForm {
        OutputForm::Text
    }
}

// ── format helpers ──────────────────────────────────────────────────────

pub(super) fn format_grep(val: &Value) -> String {
    let total = val["total"].as_u64().unwrap_or(0) as usize;

    if total == 0 {
        return "0 matches".to_string();
    }

    let mut out = String::new();

    if val.get("mode").and_then(|m| m.as_str()) == Some("literal_fallback") {
        out.push_str("[literal fallback] ");
    }

    // Dispatch: file_groups[] → simple mode (new shape).
    //           matches[]    → context mode (legacy shape with start_line items).
    if let Some(groups) = val["file_groups"].as_array() {
        let files = val["files"].as_u64().unwrap_or(0) as usize;
        format_search_simple_mode(&mut out, groups, total, files);
    } else if let Some(flat) = val["matches"].as_array() {
        let match_word = if total == 1 { "match" } else { "matches" };
        out.push_str(&format!("{total} {match_word}\n"));
        format_search_context_mode(&mut out, flat);
    }

    if let Some(overflow) = val.get("overflow").filter(|o| o.is_object()) {
        out.push('\n');
        out.push_str(&format_overflow(overflow));
    }
    out
}

fn format_search_simple_mode(out: &mut String, file_groups: &[Value], total: usize, files: usize) {
    use crate::tools::file_group::{groups_from_json, render_grouped};

    let groups = groups_from_json(file_groups);
    let noun = if total == 1 { "match" } else { "matches" };

    let render_item = |item: &Value| -> String {
        let line = item["line"].as_u64().unwrap_or(0);
        let content = item["content"].as_str().unwrap_or("").trim();
        format!("  {line:>5}: {content}")
    };

    out.push_str(&render_grouped(&groups, total, files, noun, render_item));
}

fn format_search_context_mode(out: &mut String, matches: &[Value]) {
    use std::collections::HashMap;

    // Precompute per-file match totals so the header can show `file (N)`,
    // matching the simple-mode format for at-a-glance density.
    let mut per_file_total: HashMap<&str, u64> = HashMap::new();
    for m in matches {
        let file = m["file"].as_str().unwrap_or("?");
        let n = m["match_lines"]
            .as_array()
            .map(|a| a.len() as u64)
            .unwrap_or(0);
        *per_file_total.entry(file).or_insert(0) += n;
    }

    let mut current_file: Option<&str> = None;

    for m in matches {
        let file = m["file"].as_str().unwrap_or("?");
        let start_line = m["start_line"].as_u64().unwrap_or(1);
        let content = m["content"].as_str().unwrap_or("");
        let match_lines: std::collections::HashSet<u64> = m["match_lines"]
            .as_array()
            .map(|a| a.iter().filter_map(|v| v.as_u64()).collect())
            .unwrap_or_default();

        let same_file = current_file == Some(file);
        if !same_file {
            let count = per_file_total.get(file).copied().unwrap_or(0);
            out.push_str("\n  ");
            out.push_str(file);
            out.push_str(&format!(" ({count})\n"));
            current_file = Some(file);
        } else {
            // Separator between non-overlapping blocks in the same file —
            // ripgrep uses `--` for the same purpose.
            out.push_str("  --\n");
        }

        for (i, line) in content.lines().enumerate() {
            let line_num = start_line + i as u64;
            // Ripgrep convention: `N:` for match line, `N-` for context.
            let sep = if match_lines.contains(&line_num) {
                ':'
            } else {
                '-'
            };
            out.push_str(&format!("  {line_num:>5}{sep} {line}\n"));
        }
    }

    if out.ends_with('\n') {
        out.pop();
    }
}

/// Build a search regex with literal-fallback for plain-text patterns.
fn build_grep_regex(pattern: &str) -> Result<(regex::Regex, bool)> {
    match regex::RegexBuilder::new(pattern)
        .size_limit(1 << 20)
        .dfa_size_limit(1 << 20)
        .build()
    {
        Ok(re) => Ok((re, false)),
        Err(e) => {
            if super::is_regex_like(pattern) {
                return Err(RecoverableError::with_hint(
                    format!("invalid regex: {e}"),
                    "patterns are full regex syntax — escape metacharacters like \\( \\. \\[ for literals",
                )
                .into());
            }
            let escaped = regex::escape(pattern);
            let re = regex::RegexBuilder::new(&escaped)
                .size_limit(1 << 20)
                .dfa_size_limit(1 << 20)
                .build()
                .map_err(|e2| {
                    RecoverableError::with_hint(
                        format!("invalid pattern even after escaping: {e2}"),
                        format!("original error: {e}"),
                    )
                })?;
            Ok((re, true))
        }
    }
}

/// Grep against a buffer ref (`@tool_*`, `@cmd_*`, `@file_*`).
///
/// `@tool_*` content is JSON; it is pretty-printed before search so
/// identifier-shaped strings sit on dedicated lines and become matchable.
async fn grep_in_buffer(input: &Value, ctx: &ToolContext) -> Result<Value> {
    let pattern = super::require_str_param_or(input, "pattern", &["query", "regex"])?;
    let raw_path = input["path"].as_str().unwrap_or_default();
    let max = optional_u64_param(input, "limit").unwrap_or(50) as usize;
    let context_lines = optional_u64_param(input, "context_lines")
        .unwrap_or(0)
        .min(20) as usize;

    let raw = ctx
        .output_buffer
        .get(raw_path)
        .ok_or_else(|| {
            RecoverableError::with_hint(
                format!("buffer reference not found: '{raw_path}'"),
                "Buffer refs expire when the session resets. Re-run the command to get a fresh ref.",
            )
        })?
        .stdout;

    let text = if raw_path.starts_with("@tool_") {
        serde_json::from_str::<serde_json::Value>(&raw)
            .ok()
            .and_then(|v| serde_json::to_string_pretty(&v).ok())
            .unwrap_or(raw)
    } else {
        raw
    };

    let (re, is_literal_fallback) = build_grep_regex(pattern)?;

    let mut matches: Vec<Value> = vec![];
    let mut total_match_count = 0usize;
    let mut hit_cap = false;

    if context_lines == 0 {
        for (i, line) in text.lines().enumerate() {
            if re.is_match(line) {
                total_match_count += 1;
                matches.push(json!({
                    "file": raw_path,
                    "line": i + 1,
                    "content": line,
                }));
                if matches.len() >= max {
                    hit_cap = true;
                    break;
                }
            }
        }
    } else {
        let file_lines: Vec<&str> = text.lines().collect();
        let n = file_lines.len();
        let mut current: Option<(usize, Vec<usize>, usize)> = None;

        for (i, line) in file_lines.iter().enumerate() {
            if !re.is_match(line) {
                continue;
            }
            total_match_count += 1;
            let ctx_start = i.saturating_sub(context_lines);
            let ctx_end = (i + context_lines).min(n.saturating_sub(1));

            match current.take() {
                None => current = Some((ctx_start, vec![i], ctx_end)),
                Some((blk_start, mut blk_matches, blk_end)) => {
                    if ctx_start <= blk_end + 1 {
                        blk_matches.push(i);
                        current = Some((blk_start, blk_matches, ctx_end.max(blk_end)));
                    } else {
                        let content = file_lines[blk_start..=blk_end].join("\n");
                        let match_lines: Vec<u64> =
                            blk_matches.iter().map(|&m| (m + 1) as u64).collect();
                        matches.push(json!({
                            "file": raw_path,
                            "match_lines": match_lines,
                            "start_line": blk_start + 1,
                            "content": content,
                        }));
                        current = Some((ctx_start, vec![i], ctx_end));
                    }
                }
            }

            if total_match_count >= max {
                hit_cap = true;
                break;
            }
        }

        if let Some((blk_start, blk_matches, blk_end)) = current {
            let content = file_lines[blk_start..=blk_end].join("\n");
            let match_lines: Vec<u64> = blk_matches.iter().map(|&m| (m + 1) as u64).collect();
            matches.push(json!({
                "file": raw_path,
                "match_lines": match_lines,
                "start_line": blk_start + 1,
                "content": content,
            }));
        }
    }

    let shown_count = if context_lines > 0 {
        matches.len()
    } else {
        total_match_count
    };

    let mut result = if context_lines == 0 {
        use crate::tools::file_group::{cap_grouped, group_by_file, groups_to_json};
        let (visible, total, files) = cap_grouped(matches, max);
        let truncated = hit_cap || total > visible.len();
        let groups = group_by_file(&visible);
        let file_groups = groups_to_json(&groups);
        let mut r = json!({
            "file_groups": file_groups,
            "total": total,
            "files": files,
        });
        if truncated {
            r["overflow"] = json!({
                "shown": visible.len(),
                "total": total,
                "hint": "Many matches. Narrow the pattern.",
            });
        }
        r
    } else {
        let mut r = json!({
            "matches": matches,
            "total": shown_count,
            "context_lines": context_lines,
        });
        if hit_cap {
            r["overflow"] = json!({
                "shown": shown_count,
                "hint": format!(
                    "Showing first {shown_count} matches (cap hit). Narrow the pattern."
                ),
            });
        }
        r
    };

    if is_literal_fallback {
        result["mode"] = json!("literal_fallback");
        result["reason"] = json!("pattern was not valid regex — searched as literal text");
    }
    if crate::util::path_security::is_identifier_pattern(pattern) {
        let name = pattern.split('|').next().unwrap_or(pattern);
        result["suggestion"] = json!(format!(
            "Pattern looks like a symbol name. Consider: \
             symbols(name='{name}') for declarations, \
             references(symbol='{name}') for direct callers."
        ));
    }
    Ok(result)
}

/// Strip surrounding quotes/backticks from @ref paths the same way read_file
/// does. Lets buffer-ref greps survive LLM quoting habits.
fn strip_buffer_ref_quotes(path: &str) -> &str {
    for q in ['"', '\'', '`'] {
        if let Some(inner) = path.strip_prefix(q).and_then(|s| s.strip_suffix(q)) {
            if inner.starts_with("@file_")
                || inner.starts_with("@cmd_")
                || inner.starts_with("@tool_")
                || inner.starts_with("@ack_")
            {
                return inner;
            }
        }
    }
    path
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::agent::Agent;
    use crate::lsp::LspManager;
    use crate::tools::ToolContext;
    use tempfile::tempdir;

    async fn test_ctx() -> ToolContext {
        ToolContext {
            agent: Agent::new(None).await.unwrap(),
            lsp: LspManager::new_arc(),
            output_buffer: std::sync::Arc::new(crate::tools::output_buffer::OutputBuffer::new(20)),
            progress: None,
            peer: None,
            section_coverage: std::sync::Arc::new(std::sync::Mutex::new(
                crate::tools::section_coverage::SectionCoverage::new(),
            )),
            guide_hints_emitted: std::sync::Arc::new(parking_lot::Mutex::new(Default::default())),
        }
    }

    #[tokio::test]
    async fn grep_returns_grouped_shape_simple_mode() {
        use serde_json::json;
        let dir = tempdir().unwrap();
        std::fs::write(dir.path().join("a.rs"), "fn foo() {}\nfn foo_bar() {}\n").unwrap();
        std::fs::write(dir.path().join("b.rs"), "fn foo_baz() {}\n").unwrap();

        let ctx = test_ctx().await;
        let tool = Grep;
        let result = tool
            .call(
                json!({ "pattern": "foo", "path": dir.path().to_str().unwrap() }),
                &ctx,
            )
            .await
            .unwrap();

        let groups = result["file_groups"].as_array().unwrap();
        assert!(!groups.is_empty(), "file_groups must be non-empty");
        for group in groups {
            assert!(group.get("file").is_some(), "group must have file");
            let items = group["items"].as_array().unwrap();
            for item in items {
                assert!(
                    item.get("file").is_none(),
                    "per-item file should be stripped, got: {item}"
                );
                assert!(item.get("line").is_some(), "item must have line");
                assert!(item.get("content").is_some(), "item must have content");
            }
        }
        assert!(
            result["total"].as_u64().unwrap() >= 3,
            "total must be >= 3, got {}",
            result["total"]
        );
        assert!(
            result["files"].as_u64().unwrap() >= 2,
            "files must be >= 2, got {}",
            result["files"]
        );
    }

    #[tokio::test]
    async fn grep_call_content_returns_ripgrep_style_text_not_json() {
        // Regression: small grep results used to serialize as pretty JSON via the
        // default Tool::call_content path. Now Grep declares OutputForm::Text, so
        // even sub-threshold results come through as the compact ripgrep-style
        // form ("file\n  N: content"), saving ~60% tokens on bulk locator output.
        use serde_json::json;
        let dir = tempdir().unwrap();
        std::fs::write(dir.path().join("a.rs"), "fn foo() {}\nfn foo_bar() {}\n").unwrap();

        let ctx = test_ctx().await;
        let tool = Grep;
        let content = tool
            .call_content(
                json!({ "pattern": "foo", "path": dir.path().to_str().unwrap() }),
                &ctx,
            )
            .await
            .unwrap();

        assert_eq!(content.len(), 1, "expected exactly 1 content block");
        let text = content[0].as_text().map(|t| t.text.as_str()).unwrap_or("");
        assert!(
            !text.trim_start().starts_with('{'),
            "small grep output must NOT be JSON, got: {text}"
        );
        assert!(
            text.contains("a.rs"),
            "text must reference matched file, got: {text}"
        );
        assert!(
            text.contains(": fn foo"),
            "text must use ripgrep-style `N: content` lines, got: {text}"
        );
    }

    #[tokio::test]
    async fn grep_buffer_ref_matches_content_in_tool_buffer() {
        // Probe bug 2026-05-09-grep-buffer-false-negatives.
        // Seed an @tool_* buffer with a known identifier, then assert grep finds it.
        use serde_json::json;
        let ctx = test_ctx().await;
        let raw = r#"{"symbols":[{"name":"foo_bar_baz","kind":"fn"}]}"#;
        let buf_id = ctx.output_buffer.store_tool("symbols", raw.to_string());

        let tool = Grep;
        let result = tool
            .call(json!({ "pattern": "foo_bar_baz", "path": buf_id }), &ctx)
            .await
            .unwrap();

        let total = result.get("total").and_then(|v| v.as_u64()).unwrap_or(0);
        assert!(
            total > 0,
            "grep should find 'foo_bar_baz' in @tool_* buffer content, got total={total}: {result}"
        );
    }
}