selfware 0.2.2

Your personal AI workshop — software you own, software that lasts
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
use super::Tool;
use anyhow::{Context, Result};
use async_trait::async_trait;
use once_cell::sync::Lazy;
use regex::{Regex, RegexBuilder};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Mutex;
use tracing::instrument;
use walkdir::WalkDir;

/// Maximum number of compiled regex patterns to cache.
const REGEX_CACHE_MAX: usize = 64;

/// Maximum length of a user-supplied regex pattern (bytes).
/// Prevents denial-of-service via extremely large or complex patterns.
const MAX_PATTERN_LENGTH: usize = 1_000;

/// Maximum compiled regex size (bytes). Limits memory consumed by
/// pathological patterns that expand into large automata.
const MAX_REGEX_SIZE: usize = 1 << 20; // 1 MB

/// Global cache of compiled regex patterns, keyed by their source string.
/// When the cache exceeds [`REGEX_CACHE_MAX`] entries, it is cleared entirely
/// (simple but bounded eviction strategy).
static REGEX_CACHE: Lazy<Mutex<HashMap<String, Regex>>> = Lazy::new(|| Mutex::new(HashMap::new()));

/// Return a cached `Regex` for `pattern`, compiling and caching it on first use.
///
/// Applies size limits to prevent ReDoS attacks from pathological patterns.
fn cached_regex(pattern: &str) -> Result<Regex> {
    if pattern.len() > MAX_PATTERN_LENGTH {
        anyhow::bail!(
            "Regex pattern too long ({} bytes, max {})",
            pattern.len(),
            MAX_PATTERN_LENGTH
        );
    }

    let mut cache = REGEX_CACHE
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner());

    if let Some(re) = cache.get(pattern) {
        return Ok(re.clone());
    }

    let re = RegexBuilder::new(pattern)
        .size_limit(MAX_REGEX_SIZE)
        .build()
        .context("Invalid or too-complex regex pattern")?;

    // Evict all entries when the cache is full to stay bounded.
    if cache.len() >= REGEX_CACHE_MAX {
        cache.clear();
    }

    cache.insert(pattern.to_owned(), re.clone());
    Ok(re)
}

/// Searches file contents for regex patterns, returning matching lines with context.
pub struct GrepSearch;
/// Finds files by glob pattern (e.g. `**/*.rs`), returning paths with metadata.
pub struct GlobFind;
/// Finds Rust symbol definitions (functions, structs, enums, traits, etc.) by name.
pub struct SymbolSearch;

/// A single match result from grep search
#[derive(Debug, Serialize, Deserialize)]
struct GrepMatch {
    file: String,
    line: u32,
    column: u32,
    content: String,
    context_before: Vec<String>,
    context_after: Vec<String>,
}

/// Result of a glob find operation
#[derive(Debug, Serialize, Deserialize)]
struct FileInfo {
    path: String,
    size: u64,
    modified: Option<String>,
}

/// A symbol found in code
#[derive(Debug, Serialize, Deserialize)]
struct Symbol {
    name: String,
    symbol_type: String,
    file: String,
    line: u32,
    signature: String,
}

#[async_trait]
impl Tool for GrepSearch {
    fn name(&self) -> &str {
        "grep_search"
    }

    fn description(&self) -> &str {
        "Search for regex patterns in files. Returns matching lines with context. Use for finding code patterns, error messages, or specific text."
    }

    fn schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "required": ["pattern", "path"],
            "properties": {
                "pattern": {
                    "type": "string",
                    "description": "Regex pattern to search for"
                },
                "path": {
                    "type": "string",
                    "description": "File or directory to search in"
                },
                "recursive": {
                    "type": "boolean",
                    "default": true,
                    "description": "Search directories recursively"
                },
                "case_insensitive": {
                    "type": "boolean",
                    "default": false,
                    "description": "Ignore case when matching"
                },
                "context_lines": {
                    "type": "integer",
                    "default": 2,
                    "description": "Lines of context before and after match"
                },
                "max_matches": {
                    "type": "integer",
                    "default": 100,
                    "description": "Maximum matches to return"
                },
                "offset": {
                    "type": "integer",
                    "default": 0,
                    "description": "Number of matches to skip (for pagination)"
                },
                "include": {
                    "type": "string",
                    "description": "Only search files matching this glob pattern (e.g., *.rs)"
                },
                "exclude": {
                    "type": "string",
                    "description": "Exclude files matching this glob pattern"
                }
            }
        })
    }

    #[instrument(level = "info", skip(self, args), fields(tool_name = self.name()))]
    async fn execute(&self, args: Value) -> Result<Value> {
        let result = tokio::task::spawn_blocking(move || -> Result<Value> {
            let pattern_str = args
                .get("pattern")
                .and_then(|v| v.as_str())
                .context("Missing required parameter: pattern")?;

            let path_str = args
                .get("path")
                .and_then(|v| v.as_str())
                .context("Missing required parameter: path")?;

            let recursive = args
                .get("recursive")
                .and_then(|v| v.as_bool())
                .unwrap_or(true);
            let case_insensitive = args
                .get("case_insensitive")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);
            let context_lines = args
                .get("context_lines")
                .and_then(|v| v.as_u64())
                .unwrap_or(2) as usize;
            let max_matches = args
                .get("max_matches")
                .and_then(|v| v.as_u64())
                .unwrap_or(100) as usize;
            let skip_offset = args.get("offset").and_then(|v| v.as_u64()).unwrap_or(0) as usize;
            let include_pattern = args.get("include").and_then(|v| v.as_str());
            let exclude_pattern = args.get("exclude").and_then(|v| v.as_str());

            // Build regex (uses a bounded cache to avoid recompilation)
            let full_pattern = if case_insensitive {
                format!("(?i){}", pattern_str)
            } else {
                pattern_str.to_string()
            };
            let regex = cached_regex(&full_pattern)?;

            // Build include/exclude globs
            let include_glob = include_pattern
                .map(glob::Pattern::new)
                .transpose()
                .context("Invalid include pattern")?;
            let exclude_glob = exclude_pattern
                .map(glob::Pattern::new)
                .transpose()
                .context("Invalid exclude pattern")?;

            let path = Path::new(path_str);
            let mut matches = Vec::new();
            let mut total_matches = 0;

            // Collect files to search
            let files: Vec<_> = if path.is_file() {
                vec![path.to_path_buf()]
            } else {
                let walker = if recursive {
                    WalkDir::new(path)
                } else {
                    WalkDir::new(path).max_depth(1)
                };

                walker
                    .into_iter()
                    .filter_map(|e| e.ok())
                    .filter(|e| e.file_type().is_file())
                    .filter(|e| {
                        let file_name = e.file_name().to_string_lossy();
                        // Skip hidden files and common binary/build directories
                        if file_name.starts_with('.') {
                            return false;
                        }
                        let path_str = e.path().to_string_lossy();
                        if path_str.contains("/target/")
                            || path_str.contains("/.git/")
                            || path_str.contains("/node_modules/")
                        {
                            return false;
                        }
                        // Apply include/exclude patterns
                        if let Some(ref glob) = include_glob {
                            if !glob.matches(&file_name) {
                                return false;
                            }
                        }
                        if let Some(ref glob) = exclude_glob {
                            if glob.matches(&file_name) {
                                return false;
                            }
                        }
                        true
                    })
                    .map(|e| e.path().to_path_buf())
                    .collect()
            };

            // Search files
            for file_path in files {
                if matches.len() >= max_matches {
                    break;
                }

                let content = match std::fs::read_to_string(&file_path) {
                    Ok(c) => c,
                    Err(_) => continue, // Skip binary/unreadable files
                };

                let lines: Vec<&str> = content.lines().collect();

                for (line_num, line) in lines.iter().enumerate() {
                    if matches.len() >= max_matches {
                        break;
                    }

                    if let Some(m) = regex.find(line) {
                        total_matches += 1;

                        // Skip matches before the offset
                        if total_matches <= skip_offset {
                            continue;
                        }

                        // Get context
                        let start = line_num.saturating_sub(context_lines);
                        let end = (line_num + context_lines + 1).min(lines.len());

                        let context_before: Vec<String> = lines[start..line_num]
                            .iter()
                            .map(|s| s.to_string())
                            .collect();

                        let context_after: Vec<String> = if line_num + 1 < lines.len() {
                            lines[(line_num + 1)..end]
                                .iter()
                                .map(|s| s.to_string())
                                .collect()
                        } else {
                            vec![]
                        };

                        matches.push(GrepMatch {
                            file: file_path.to_string_lossy().to_string(),
                            line: (line_num + 1) as u32,
                            column: (m.start() + 1) as u32,
                            content: line.to_string(),
                            context_before,
                            context_after,
                        });
                    }
                }
            }

            let truncated = matches.len() >= max_matches;
            let has_more = truncated || (total_matches > skip_offset + matches.len());

            Ok(serde_json::json!({
                "matches": matches,
                "count": matches.len(),
                "total_matches": total_matches,
                "truncated": truncated,
                "pagination": {
                    "offset": skip_offset,
                    "limit": max_matches,
                    "total_matches": total_matches,
                    "has_more": has_more
                }
            }))
        })
        .await??;
        Ok(result)
    }
}

#[async_trait]
impl Tool for GlobFind {
    fn name(&self) -> &str {
        "glob_find"
    }

    fn description(&self) -> &str {
        "Find files by glob pattern (e.g., *.rs, src/**/*.ts). Returns file paths with metadata. Use to locate files before reading or editing."
    }

    fn schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "required": ["pattern"],
            "properties": {
                "pattern": {
                    "type": "string",
                    "description": "Glob pattern (e.g., *.rs, src/**/*.ts, **/*_test.go)"
                },
                "path": {
                    "type": "string",
                    "default": ".",
                    "description": "Base directory to search from"
                },
                "max_results": {
                    "type": "integer",
                    "default": 100,
                    "description": "Maximum results to return"
                }
            }
        })
    }

    #[instrument(level = "info", skip(self, args), fields(tool_name = self.name()))]
    async fn execute(&self, args: Value) -> Result<Value> {
        let result = tokio::task::spawn_blocking(move || -> Result<Value> {
            let pattern_str = args
                .get("pattern")
                .and_then(|v| v.as_str())
                .context("Missing required parameter: pattern")?;

            let base_path = args.get("path").and_then(|v| v.as_str()).unwrap_or(".");

            let max_results = args
                .get("max_results")
                .and_then(|v| v.as_u64())
                .unwrap_or(100) as usize;

            // Combine base path with pattern
            let full_pattern = if pattern_str.starts_with('/') || pattern_str.starts_with("./") {
                pattern_str.to_string()
            } else {
                format!("{}/{}", base_path, pattern_str)
            };

            let glob_pattern = glob::Pattern::new(&full_pattern).context("Invalid glob pattern")?;

            let mut files = Vec::new();

            // Walk directory and match against pattern
            for entry in WalkDir::new(base_path)
                .into_iter()
                .filter_map(|e| e.ok())
                .filter(|e| e.file_type().is_file())
            {
                if files.len() >= max_results {
                    break;
                }

                let path = entry.path();
                let path_str = path.to_string_lossy();

                // Skip common directories
                if path_str.contains("/.git/")
                    || path_str.contains("/target/")
                    || path_str.contains("/node_modules/")
                {
                    continue;
                }

                if glob_pattern.matches(&path_str) {
                    let metadata = std::fs::metadata(path).ok();
                    let modified = metadata.as_ref().and_then(|m| {
                        m.modified().ok().map(|t| {
                            let datetime: chrono::DateTime<chrono::Utc> = t.into();
                            datetime.to_rfc3339()
                        })
                    });

                    files.push(FileInfo {
                        path: path_str.to_string(),
                        size: metadata.map(|m| m.len()).unwrap_or(0),
                        modified,
                    });
                }
            }

            let truncated = files.len() >= max_results;

            Ok(serde_json::json!({
                "files": files,
                "count": files.len(),
                "truncated": truncated
            }))
        })
        .await??;
        Ok(result)
    }
}

#[async_trait]
impl Tool for SymbolSearch {
    fn name(&self) -> &str {
        "symbol_search"
    }

    fn description(&self) -> &str {
        "Find function, struct, enum, trait, or impl definitions in Rust code. Use to locate code symbols for navigation or understanding."
    }

    fn schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "required": ["name"],
            "properties": {
                "name": {
                    "type": "string",
                    "description": "Symbol name or pattern to search for"
                },
                "path": {
                    "type": "string",
                    "default": ".",
                    "description": "Directory to search in"
                },
                "symbol_type": {
                    "type": "string",
                    "enum": ["function", "struct", "enum", "trait", "impl", "const", "type", "mod", "all"],
                    "default": "all",
                    "description": "Type of symbol to search for"
                },
                "max_results": {
                    "type": "integer",
                    "default": 50,
                    "description": "Maximum results to return"
                }
            }
        })
    }

    #[instrument(level = "info", skip(self, args), fields(tool_name = self.name()))]
    async fn execute(&self, args: Value) -> Result<Value> {
        let result = tokio::task::spawn_blocking(move || -> Result<Value> {
            let name_pattern = args
                .get("name")
                .and_then(|v| v.as_str())
                .context("Missing required parameter: name")?;

            let base_path = args.get("path").and_then(|v| v.as_str()).unwrap_or(".");

            let symbol_type = args
                .get("symbol_type")
                .and_then(|v| v.as_str())
                .unwrap_or("all");

            let max_results = args
                .get("max_results")
                .and_then(|v| v.as_u64())
                .unwrap_or(50) as usize;

            // Build patterns for different symbol types
            let patterns = build_symbol_patterns(symbol_type, name_pattern)?;

            let mut symbols = Vec::new();

            // Walk Rust files
            for entry in WalkDir::new(base_path)
                .into_iter()
                .filter_map(|e| e.ok())
                .filter(|e| {
                    e.file_type().is_file()
                        && e.path().extension().map(|ext| ext == "rs").unwrap_or(false)
                })
            {
                if symbols.len() >= max_results {
                    break;
                }

                let path = entry.path();
                let path_str = path.to_string_lossy();

                // Skip target directory
                if path_str.contains("/target/") {
                    continue;
                }

                let content = match std::fs::read_to_string(path) {
                    Ok(c) => c,
                    Err(_) => continue,
                };

                for (regex, sym_type) in &patterns {
                    if symbols.len() >= max_results {
                        break;
                    }

                    for (line_num, line) in content.lines().enumerate() {
                        if symbols.len() >= max_results {
                            break;
                        }

                        if let Some(caps) = regex.captures(line) {
                            let symbol_name = caps.get(1).map(|m| m.as_str()).unwrap_or("");

                            // Verify the name matches our pattern
                            if !symbol_name
                                .to_lowercase()
                                .contains(&name_pattern.to_lowercase())
                            {
                                continue;
                            }

                            symbols.push(Symbol {
                                name: symbol_name.to_string(),
                                symbol_type: sym_type.to_string(),
                                file: path_str.to_string(),
                                line: (line_num + 1) as u32,
                                signature: line.trim().to_string(),
                            });
                        }
                    }
                }
            }

            Ok(serde_json::json!({
                "symbols": symbols,
                "count": symbols.len()
            }))
        })
        .await??;
        Ok(result)
    }
}

/// Pre-compiled symbol regexes (compiled once, reused forever).
struct SymbolRegexes {
    fn_pattern: Regex,
    struct_pattern: Regex,
    enum_pattern: Regex,
    trait_pattern: Regex,
    impl_pattern: Regex,
    const_pattern: Regex,
    type_pattern: Regex,
    mod_pattern: Regex,
}

static SYMBOL_REGEXES: Lazy<SymbolRegexes> = Lazy::new(|| SymbolRegexes {
    fn_pattern: Regex::new(r"(?:pub(?:\s*\([^)]*\))?\s+)?(?:async\s+)?fn\s+(\w+)").unwrap(),
    struct_pattern: Regex::new(r"(?:pub(?:\s*\([^)]*\))?\s+)?struct\s+(\w+)").unwrap(),
    enum_pattern: Regex::new(r"(?:pub(?:\s*\([^)]*\))?\s+)?enum\s+(\w+)").unwrap(),
    trait_pattern: Regex::new(r"(?:pub(?:\s*\([^)]*\))?\s+)?trait\s+(\w+)").unwrap(),
    impl_pattern: Regex::new(r"impl(?:<[^>]*>)?\s+(?:(\w+)|(?:\w+\s+for\s+(\w+)))").unwrap(),
    const_pattern: Regex::new(r"(?:pub(?:\s*\([^)]*\))?\s+)?const\s+(\w+)").unwrap(),
    type_pattern: Regex::new(r"(?:pub(?:\s*\([^)]*\))?\s+)?type\s+(\w+)").unwrap(),
    mod_pattern: Regex::new(r"(?:pub(?:\s*\([^)]*\))?\s+)?mod\s+(\w+)").unwrap(),
});

/// Build regex patterns for different Rust symbol types.
/// The underlying regexes are compiled once via `Lazy` statics.
fn build_symbol_patterns(
    symbol_type: &str,
    _name_pattern: &str,
) -> Result<Vec<(&'static Regex, &'static str)>> {
    let sr = &*SYMBOL_REGEXES;
    let mut patterns = Vec::new();

    match symbol_type {
        "function" => patterns.push((&sr.fn_pattern, "function")),
        "struct" => patterns.push((&sr.struct_pattern, "struct")),
        "enum" => patterns.push((&sr.enum_pattern, "enum")),
        "trait" => patterns.push((&sr.trait_pattern, "trait")),
        "impl" => patterns.push((&sr.impl_pattern, "impl")),
        "const" => patterns.push((&sr.const_pattern, "const")),
        "type" => patterns.push((&sr.type_pattern, "type")),
        "mod" => patterns.push((&sr.mod_pattern, "mod")),
        _ => {
            patterns.push((&sr.fn_pattern, "function"));
            patterns.push((&sr.struct_pattern, "struct"));
            patterns.push((&sr.enum_pattern, "enum"));
            patterns.push((&sr.trait_pattern, "trait"));
            patterns.push((&sr.impl_pattern, "impl"));
            patterns.push((&sr.const_pattern, "const"));
            patterns.push((&sr.type_pattern, "type"));
            patterns.push((&sr.mod_pattern, "mod"));
        }
    }

    Ok(patterns)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_grep_search_basic() {
        let tool = GrepSearch;
        assert_eq!(tool.name(), "grep_search");
        assert!(tool.description().contains("Search"));
    }

    #[tokio::test]
    async fn test_grep_search_schema() {
        let tool = GrepSearch;
        let schema = tool.schema();
        assert_eq!(schema["type"], "object");
        assert!(schema["required"]
            .as_array()
            .unwrap()
            .contains(&"pattern".into()));
        assert!(schema["required"]
            .as_array()
            .unwrap()
            .contains(&"path".into()));
    }

    #[tokio::test]
    async fn test_grep_search_file() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.rs");
        fs::write(&file_path, "fn hello() {\n    println!(\"Hello\");\n}\n").unwrap();

        let tool = GrepSearch;
        let result = tool
            .execute(serde_json::json!({
                "pattern": "hello",
                "path": file_path.to_str().unwrap()
            }))
            .await
            .unwrap();

        assert!(result["count"].as_u64().unwrap() >= 1);
        let matches = result["matches"].as_array().unwrap();
        assert!(!matches.is_empty());
        assert_eq!(matches[0]["line"], 1);
    }

    #[tokio::test]
    async fn test_grep_search_case_insensitive() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.rs");
        fs::write(&file_path, "fn HELLO() {}\nfn Hello() {}\nfn hello() {}\n").unwrap();

        let tool = GrepSearch;
        let result = tool
            .execute(serde_json::json!({
                "pattern": "hello",
                "path": file_path.to_str().unwrap(),
                "case_insensitive": true
            }))
            .await
            .unwrap();

        assert_eq!(result["count"].as_u64().unwrap(), 3);
    }

    #[tokio::test]
    async fn test_grep_search_with_context() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.rs");
        fs::write(&file_path, "line1\nline2\nMATCH\nline4\nline5\n").unwrap();

        let tool = GrepSearch;
        let result = tool
            .execute(serde_json::json!({
                "pattern": "MATCH",
                "path": file_path.to_str().unwrap(),
                "context_lines": 2
            }))
            .await
            .unwrap();

        let matches = result["matches"].as_array().unwrap();
        assert_eq!(matches.len(), 1);
        let m = &matches[0];
        assert_eq!(m["context_before"].as_array().unwrap().len(), 2);
        assert_eq!(m["context_after"].as_array().unwrap().len(), 2);
    }

    #[tokio::test]
    async fn test_grep_search_max_matches() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.rs");
        let content = (0..50)
            .map(|i| format!("test line {}", i))
            .collect::<Vec<_>>()
            .join("\n");
        fs::write(&file_path, content).unwrap();

        let tool = GrepSearch;
        let result = tool
            .execute(serde_json::json!({
                "pattern": "test",
                "path": file_path.to_str().unwrap(),
                "max_matches": 5
            }))
            .await
            .unwrap();

        assert_eq!(result["count"].as_u64().unwrap(), 5);
        assert!(result["truncated"].as_bool().unwrap());
    }

    #[tokio::test]
    async fn test_glob_find_basic() {
        let tool = GlobFind;
        assert_eq!(tool.name(), "glob_find");
        assert!(tool.description().contains("Find files"));
    }

    #[tokio::test]
    async fn test_glob_find_schema() {
        let tool = GlobFind;
        let schema = tool.schema();
        assert_eq!(schema["type"], "object");
        assert!(schema["required"]
            .as_array()
            .unwrap()
            .contains(&"pattern".into()));
    }

    #[tokio::test]
    async fn test_glob_find_files() {
        let dir = tempdir().unwrap();
        fs::write(dir.path().join("test1.rs"), "").unwrap();
        fs::write(dir.path().join("test2.rs"), "").unwrap();
        fs::write(dir.path().join("test.txt"), "").unwrap();

        let tool = GlobFind;
        let result = tool
            .execute(serde_json::json!({
                "pattern": "**/*.rs",
                "path": dir.path().to_str().unwrap()
            }))
            .await
            .unwrap();

        assert_eq!(result["count"].as_u64().unwrap(), 2);
    }

    #[tokio::test]
    async fn test_symbol_search_basic() {
        let tool = SymbolSearch;
        assert_eq!(tool.name(), "symbol_search");
        assert!(tool.description().contains("function"));
    }

    #[tokio::test]
    async fn test_symbol_search_schema() {
        let tool = SymbolSearch;
        let schema = tool.schema();
        assert_eq!(schema["type"], "object");
        assert!(schema["required"]
            .as_array()
            .unwrap()
            .contains(&"name".into()));
    }

    #[tokio::test]
    async fn test_symbol_search_function() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.rs");
        fs::write(&file_path, "pub fn my_function() {}\nfn another_fn() {}").unwrap();

        let tool = SymbolSearch;
        let result = tool
            .execute(serde_json::json!({
                "name": "function",
                "path": dir.path().to_str().unwrap(),
                "symbol_type": "function"
            }))
            .await
            .unwrap();

        let symbols = result["symbols"].as_array().unwrap();
        assert!(!symbols.is_empty());
        assert!(symbols
            .iter()
            .any(|s| s["name"].as_str().unwrap() == "my_function"));
    }

    #[tokio::test]
    async fn test_symbol_search_struct() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.rs");
        fs::write(&file_path, "pub struct MyStruct {\n    field: i32,\n}\n").unwrap();

        let tool = SymbolSearch;
        let result = tool
            .execute(serde_json::json!({
                "name": "Struct",
                "path": dir.path().to_str().unwrap(),
                "symbol_type": "struct"
            }))
            .await
            .unwrap();

        let symbols = result["symbols"].as_array().unwrap();
        assert!(symbols
            .iter()
            .any(|s| s["name"].as_str().unwrap() == "MyStruct"));
    }

    #[tokio::test]
    async fn test_symbol_search_all_types() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.rs");
        fs::write(
            &file_path,
            r#"
            pub struct TestStruct {}
            pub enum TestEnum {}
            pub trait TestTrait {}
            pub fn test_function() {}
            impl TestStruct {}
        "#,
        )
        .unwrap();

        let tool = SymbolSearch;
        let result = tool
            .execute(serde_json::json!({
                "name": "Test",
                "path": dir.path().to_str().unwrap(),
                "symbol_type": "all"
            }))
            .await
            .unwrap();

        let symbols = result["symbols"].as_array().unwrap();
        // Should find struct, enum, trait, function
        assert!(symbols.len() >= 4);
    }
}