agent-air-runtime 0.7.0

Core runtime for agent-air - LLM orchestration, tools, and permissions (no TUI dependencies)
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
//! Glob tool implementation for fast file pattern matching.
//!
//! This tool allows the LLM to find files matching glob patterns.
//! It integrates with the PermissionRegistry to require user approval
//! before searching directories.

use std::collections::HashMap;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;
use std::time::SystemTime;

use globset::{Glob, GlobMatcher};
use walkdir::WalkDir;

use super::types::{
    DisplayConfig, DisplayResult, Executable, ResultContentType, ToolContext, ToolType,
};
use crate::permissions::{GrantTarget, PermissionLevel, PermissionRegistry, PermissionRequest};

/// Glob tool name constant.
pub const GLOB_TOOL_NAME: &str = "glob";

/// Glob tool description constant.
pub const GLOB_TOOL_DESCRIPTION: &str = r#"Fast file pattern matching tool that works with any codebase size.

Usage:
- Supports glob patterns like "**/*.js" or "src/**/*.ts"
- Returns matching file paths sorted by modification time (most recent first)
- Use this tool when you need to find files by name patterns
- The path parameter specifies the directory to search in (defaults to current directory)

Pattern Syntax:
- `*` matches any sequence of characters except path separators
- `**` matches any sequence of characters including path separators (recursive)
- `?` matches any single character except path separators
- `[abc]` matches any character in the brackets
- `[!abc]` matches any character not in the brackets
- `{a,b}` matches either pattern a or pattern b

Examples:
- "*.rs" - all Rust files in the search directory
- "**/*.rs" - all Rust files recursively
- "src/**/*.{ts,tsx}" - all TypeScript files under src/
- "**/test_*.py" - all Python test files recursively"#;

/// Glob tool JSON schema constant.
pub const GLOB_TOOL_SCHEMA: &str = r#"{
    "type": "object",
    "properties": {
        "pattern": {
            "type": "string",
            "description": "The glob pattern to match files against"
        },
        "path": {
            "type": "string",
            "description": "The directory to search in. Defaults to current working directory."
        },
        "limit": {
            "type": "integer",
            "description": "Maximum number of results to return. Defaults to 1000."
        },
        "include_hidden": {
            "type": "boolean",
            "description": "Include hidden files (starting with dot). Defaults to false."
        }
    },
    "required": ["pattern"]
}"#;

/// Tool that finds files matching glob patterns with permission checks.
pub struct GlobTool {
    /// Reference to the permission registry for requesting read permissions.
    permission_registry: Arc<PermissionRegistry>,
    /// Default search path if none provided.
    default_path: Option<PathBuf>,
}

impl GlobTool {
    /// Create a new GlobTool with the given permission registry.
    ///
    /// # Arguments
    /// * `permission_registry` - The registry used to request and cache permissions.
    pub fn new(permission_registry: Arc<PermissionRegistry>) -> Self {
        Self {
            permission_registry,
            default_path: None,
        }
    }

    /// Create a new GlobTool with a default search path.
    ///
    /// # Arguments
    /// * `permission_registry` - The registry used to request and cache permissions.
    /// * `default_path` - The default directory to search if no path is provided.
    pub fn with_default_path(
        permission_registry: Arc<PermissionRegistry>,
        default_path: PathBuf,
    ) -> Self {
        Self {
            permission_registry,
            default_path: Some(default_path),
        }
    }

    /// Builds a permission request for searching files in a directory.
    fn build_permission_request(tool_use_id: &str, path: &str, pattern: &str) -> PermissionRequest {
        let reason = format!("Search for '{}' pattern", pattern);
        PermissionRequest::new(
            tool_use_id,
            GrantTarget::path(path, true), // recursive for glob
            PermissionLevel::Read,
            format!("Glob search in: {}", path),
        )
        .with_reason(reason)
        .with_tool(GLOB_TOOL_NAME)
    }

    /// Get file modification time for sorting.
    fn get_mtime(path: &Path) -> SystemTime {
        path.metadata()
            .and_then(|m| m.modified())
            .unwrap_or(SystemTime::UNIX_EPOCH)
    }
}

impl Executable for GlobTool {
    fn name(&self) -> &str {
        GLOB_TOOL_NAME
    }

    fn description(&self) -> &str {
        GLOB_TOOL_DESCRIPTION
    }

    fn input_schema(&self) -> &str {
        GLOB_TOOL_SCHEMA
    }

    fn tool_type(&self) -> ToolType {
        ToolType::FileRead
    }

    fn execute(
        &self,
        context: ToolContext,
        input: HashMap<String, serde_json::Value>,
    ) -> Pin<Box<dyn Future<Output = Result<String, String>> + Send>> {
        let permission_registry = self.permission_registry.clone();
        let default_path = self.default_path.clone();

        Box::pin(async move {
            // ─────────────────────────────────────────────────────────────
            // Step 1: Extract and validate parameters
            // ─────────────────────────────────────────────────────────────
            let pattern = input
                .get("pattern")
                .and_then(|v| v.as_str())
                .ok_or_else(|| "Missing required 'pattern' parameter".to_string())?;

            let search_path = input
                .get("path")
                .and_then(|v| v.as_str())
                .map(PathBuf::from)
                .or(default_path)
                .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));

            let search_path_str = search_path.to_string_lossy().to_string();

            let limit = input
                .get("limit")
                .and_then(|v| v.as_i64())
                .map(|v| v.max(1) as usize)
                .unwrap_or(1000);

            let include_hidden = input
                .get("include_hidden")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);

            // Validate search path exists
            if !search_path.exists() {
                return Err(format!("Search path does not exist: {}", search_path_str));
            }

            if !search_path.is_dir() {
                return Err(format!(
                    "Search path is not a directory: {}",
                    search_path_str
                ));
            }

            // ─────────────────────────────────────────────────────────────
            // Step 2: Request permission if not pre-approved by batch executor
            // ─────────────────────────────────────────────────────────────
            if !context.permissions_pre_approved {
                let permission_request =
                    Self::build_permission_request(&context.tool_use_id, &search_path_str, pattern);

                let response_rx = permission_registry
                    .request_permission(
                        context.session_id,
                        permission_request,
                        context.turn_id.clone(),
                    )
                    .await
                    .map_err(|e| format!("Failed to request permission: {}", e))?;

                let response = response_rx
                    .await
                    .map_err(|_| "Permission request was cancelled".to_string())?;

                if !response.granted {
                    let reason = response
                        .message
                        .unwrap_or_else(|| "Permission denied by user".to_string());
                    return Err(format!(
                        "Permission denied to search '{}': {}",
                        search_path_str, reason
                    ));
                }
            }

            // ─────────────────────────────────────────────────────────────
            // Step 3: Compile glob pattern
            // ─────────────────────────────────────────────────────────────
            let glob_matcher: GlobMatcher = Glob::new(pattern)
                .map_err(|e| format!("Invalid glob pattern '{}': {}", pattern, e))?
                .compile_matcher();

            // ─────────────────────────────────────────────────────────────
            // Step 8: Walk directory and collect matches
            // ─────────────────────────────────────────────────────────────
            let search_path_for_filter = search_path.clone();
            let mut matches: Vec<PathBuf> = WalkDir::new(&search_path)
                .follow_links(false)
                .into_iter()
                .filter_entry(move |e| {
                    // Skip hidden directories unless include_hidden is true
                    // But always allow the root search path
                    let is_root = e.path() == search_path_for_filter;
                    is_root || include_hidden || !e.file_name().to_string_lossy().starts_with('.')
                })
                .filter_map(|e| e.ok())
                .filter(|e| e.file_type().is_file())
                .filter(|e| {
                    let path = e.path();
                    let relative = path.strip_prefix(&search_path).unwrap_or(path);
                    glob_matcher.is_match(relative)
                })
                .map(|e| e.path().to_path_buf())
                .collect();

            // ─────────────────────────────────────────────────────────────
            // Step 9: Sort by modification time (most recent first)
            // ─────────────────────────────────────────────────────────────
            matches.sort_by(|a, b| Self::get_mtime(b).cmp(&Self::get_mtime(a)));

            // ─────────────────────────────────────────────────────────────
            // Step 10: Apply limit and format output
            // ─────────────────────────────────────────────────────────────
            let total_matches = matches.len();

            if matches.is_empty() {
                return Ok(format!(
                    "No files found matching pattern '{}' in '{}'",
                    pattern, search_path_str
                ));
            }

            matches.truncate(limit);

            let mut output = String::new();
            for path in &matches {
                output.push_str(&path.display().to_string());
                output.push('\n');
            }

            if total_matches > limit {
                output.push_str(&format!(
                    "\n... and {} more files (showing {}/{})",
                    total_matches - limit,
                    limit,
                    total_matches
                ));
            }

            Ok(output.trim_end().to_string())
        })
    }

    fn display_config(&self) -> DisplayConfig {
        DisplayConfig {
            display_name: "Glob".to_string(),
            display_title: Box::new(|input| {
                input
                    .get("pattern")
                    .and_then(|v| v.as_str())
                    .unwrap_or("*")
                    .to_string()
            }),
            display_content: Box::new(|_input, result| {
                let lines: Vec<&str> = result.lines().take(20).collect();
                let total_lines = result.lines().count();

                DisplayResult {
                    content: lines.join("\n"),
                    content_type: ResultContentType::PlainText,
                    is_truncated: total_lines > 20,
                    full_length: total_lines,
                }
            }),
        }
    }

    fn compact_summary(&self, input: &HashMap<String, serde_json::Value>, result: &str) -> String {
        let pattern = input.get("pattern").and_then(|v| v.as_str()).unwrap_or("*");

        let file_count = result
            .lines()
            .filter(|line| {
                !line.starts_with("...") && !line.starts_with("No files") && !line.is_empty()
            })
            .count();

        format!("[Glob: {} ({} files)]", pattern, file_count)
    }

    fn required_permissions(
        &self,
        context: &ToolContext,
        input: &HashMap<String, serde_json::Value>,
    ) -> Option<Vec<PermissionRequest>> {
        // Extract pattern - required parameter
        let pattern = input.get("pattern").and_then(|v| v.as_str())?;

        // Extract path or use default_path, mirroring execute() logic
        let search_path = input
            .get("path")
            .and_then(|v| v.as_str())
            .map(PathBuf::from)
            .or_else(|| self.default_path.clone())
            .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));

        let search_path_str = search_path.to_string_lossy().to_string();

        // Build permission request using the helper method
        let permission_request =
            Self::build_permission_request(&context.tool_use_id, &search_path_str, pattern);

        Some(vec![permission_request])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::controller::types::ControllerEvent;
    use crate::permissions::{PermissionLevel, PermissionPanelResponse};
    use std::fs;
    use tempfile::TempDir;
    use tokio::sync::mpsc;

    /// Helper to create a permission registry for testing.
    fn create_test_registry() -> (Arc<PermissionRegistry>, mpsc::Receiver<ControllerEvent>) {
        let (tx, rx) = mpsc::channel(16);
        let registry = Arc::new(PermissionRegistry::new(tx));
        (registry, rx)
    }

    fn grant_once() -> PermissionPanelResponse {
        PermissionPanelResponse {
            granted: true,
            grant: None,
            message: None,
        }
    }

    fn deny(reason: &str) -> PermissionPanelResponse {
        PermissionPanelResponse {
            granted: false,
            grant: None,
            message: Some(reason.to_string()),
        }
    }

    fn setup_test_dir() -> TempDir {
        let temp = TempDir::new().unwrap();

        // Create test structure
        fs::create_dir_all(temp.path().join("src")).unwrap();
        fs::create_dir_all(temp.path().join("tests")).unwrap();
        fs::create_dir_all(temp.path().join(".hidden_dir")).unwrap();
        fs::write(temp.path().join("src/main.rs"), "fn main() {}").unwrap();
        fs::write(temp.path().join("src/lib.rs"), "pub mod lib;").unwrap();
        fs::write(temp.path().join("tests/test.rs"), "#[test]").unwrap();
        fs::write(temp.path().join("README.md"), "# README").unwrap();
        fs::write(temp.path().join(".hidden"), "hidden file").unwrap();
        fs::write(temp.path().join(".hidden_dir/secret.txt"), "secret").unwrap();

        temp
    }

    #[tokio::test]
    async fn test_simple_pattern_with_permission() {
        let temp = setup_test_dir();
        let (registry, mut event_rx) = create_test_registry();
        let tool = GlobTool::with_default_path(registry.clone(), temp.path().to_path_buf());

        let mut input = HashMap::new();
        input.insert(
            "pattern".to_string(),
            serde_json::Value::String("*.md".to_string()),
        );

        let context = ToolContext {
            session_id: 1,
            tool_use_id: "test-glob-1".to_string(),
            turn_id: None,
            permissions_pre_approved: false,
        };

        // Grant permission in background
        let registry_clone = registry.clone();
        tokio::spawn(async move {
            if let Some(ControllerEvent::PermissionRequired { tool_use_id, .. }) =
                event_rx.recv().await
            {
                registry_clone
                    .respond_to_request(&tool_use_id, grant_once())
                    .await
                    .unwrap();
            }
        });

        let result = tool.execute(context, input).await;
        assert!(result.is_ok());
        assert!(result.unwrap().contains("README.md"));
    }

    #[tokio::test]
    async fn test_recursive_pattern() {
        let temp = setup_test_dir();
        let (registry, mut event_rx) = create_test_registry();
        let tool = GlobTool::with_default_path(registry.clone(), temp.path().to_path_buf());

        let mut input = HashMap::new();
        input.insert(
            "pattern".to_string(),
            serde_json::Value::String("**/*.rs".to_string()),
        );

        let context = ToolContext {
            session_id: 1,
            tool_use_id: "test-glob-recursive".to_string(),
            turn_id: None,
            permissions_pre_approved: false,
        };

        let registry_clone = registry.clone();
        tokio::spawn(async move {
            if let Some(ControllerEvent::PermissionRequired { tool_use_id, .. }) =
                event_rx.recv().await
            {
                registry_clone
                    .respond_to_request(&tool_use_id, grant_once())
                    .await
                    .unwrap();
            }
        });

        let result = tool.execute(context, input).await;
        assert!(result.is_ok());
        let output = result.unwrap();
        assert!(output.contains("main.rs"));
        assert!(output.contains("lib.rs"));
        assert!(output.contains("test.rs"));
    }

    #[tokio::test]
    async fn test_hidden_files_excluded() {
        let temp = setup_test_dir();
        let (registry, mut event_rx) = create_test_registry();
        let tool = GlobTool::with_default_path(registry.clone(), temp.path().to_path_buf());

        let mut input = HashMap::new();
        input.insert(
            "pattern".to_string(),
            serde_json::Value::String("**/*".to_string()),
        );

        let context = ToolContext {
            session_id: 1,
            tool_use_id: "test-glob-hidden".to_string(),
            turn_id: None,
            permissions_pre_approved: false,
        };

        let registry_clone = registry.clone();
        tokio::spawn(async move {
            if let Some(ControllerEvent::PermissionRequired { tool_use_id, .. }) =
                event_rx.recv().await
            {
                registry_clone
                    .respond_to_request(&tool_use_id, grant_once())
                    .await
                    .unwrap();
            }
        });

        let result = tool.execute(context, input).await;
        assert!(result.is_ok());
        let output = result.unwrap();
        // Should not contain hidden files
        assert!(!output.contains(".hidden"));
        assert!(!output.contains("secret.txt"));
    }

    #[tokio::test]
    async fn test_hidden_files_included() {
        let temp = setup_test_dir();
        let (registry, mut event_rx) = create_test_registry();
        let tool = GlobTool::with_default_path(registry.clone(), temp.path().to_path_buf());

        let mut input = HashMap::new();
        input.insert(
            "pattern".to_string(),
            serde_json::Value::String("**/*".to_string()),
        );
        input.insert("include_hidden".to_string(), serde_json::Value::Bool(true));

        let context = ToolContext {
            session_id: 1,
            tool_use_id: "test-glob-hidden-incl".to_string(),
            turn_id: None,
            permissions_pre_approved: false,
        };

        let registry_clone = registry.clone();
        tokio::spawn(async move {
            if let Some(ControllerEvent::PermissionRequired { tool_use_id, .. }) =
                event_rx.recv().await
            {
                registry_clone
                    .respond_to_request(&tool_use_id, grant_once())
                    .await
                    .unwrap();
            }
        });

        let result = tool.execute(context, input).await;
        assert!(result.is_ok());
        let output = result.unwrap();
        // Should contain hidden files when include_hidden is true
        assert!(output.contains(".hidden") || output.contains("secret.txt"));
    }

    #[tokio::test]
    async fn test_permission_denied() {
        let temp = setup_test_dir();
        let (registry, mut event_rx) = create_test_registry();
        let tool = GlobTool::with_default_path(registry.clone(), temp.path().to_path_buf());

        let mut input = HashMap::new();
        input.insert(
            "pattern".to_string(),
            serde_json::Value::String("*.rs".to_string()),
        );

        let context = ToolContext {
            session_id: 1,
            tool_use_id: "test-glob-denied".to_string(),
            turn_id: None,
            permissions_pre_approved: false,
        };

        let registry_clone = registry.clone();
        tokio::spawn(async move {
            if let Some(ControllerEvent::PermissionRequired { tool_use_id, .. }) =
                event_rx.recv().await
            {
                registry_clone
                    .respond_to_request(&tool_use_id, deny("Access denied"))
                    .await
                    .unwrap();
            }
        });

        let result = tool.execute(context, input).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Permission denied"));
    }

    #[tokio::test]
    async fn test_limit() {
        let temp = setup_test_dir();
        let (registry, mut event_rx) = create_test_registry();
        let tool = GlobTool::with_default_path(registry.clone(), temp.path().to_path_buf());

        let mut input = HashMap::new();
        input.insert(
            "pattern".to_string(),
            serde_json::Value::String("**/*".to_string()),
        );
        input.insert("limit".to_string(), serde_json::Value::Number(2.into()));

        let context = ToolContext {
            session_id: 1,
            tool_use_id: "test-glob-limit".to_string(),
            turn_id: None,
            permissions_pre_approved: false,
        };

        let registry_clone = registry.clone();
        tokio::spawn(async move {
            if let Some(ControllerEvent::PermissionRequired { tool_use_id, .. }) =
                event_rx.recv().await
            {
                registry_clone
                    .respond_to_request(&tool_use_id, grant_once())
                    .await
                    .unwrap();
            }
        });

        let result = tool.execute(context, input).await;
        assert!(result.is_ok());
        let output = result.unwrap();
        // Should indicate more files available
        assert!(output.contains("... and"));
    }

    #[tokio::test]
    async fn test_no_matches() {
        let temp = setup_test_dir();
        let (registry, mut event_rx) = create_test_registry();
        let tool = GlobTool::with_default_path(registry.clone(), temp.path().to_path_buf());

        let mut input = HashMap::new();
        input.insert(
            "pattern".to_string(),
            serde_json::Value::String("*.nonexistent".to_string()),
        );

        let context = ToolContext {
            session_id: 1,
            tool_use_id: "test-glob-nomatch".to_string(),
            turn_id: None,
            permissions_pre_approved: false,
        };

        let registry_clone = registry.clone();
        tokio::spawn(async move {
            if let Some(ControllerEvent::PermissionRequired { tool_use_id, .. }) =
                event_rx.recv().await
            {
                registry_clone
                    .respond_to_request(&tool_use_id, grant_once())
                    .await
                    .unwrap();
            }
        });

        let result = tool.execute(context, input).await;
        assert!(result.is_ok());
        assert!(result.unwrap().contains("No files found"));
    }

    #[tokio::test]
    async fn test_invalid_pattern() {
        let temp = setup_test_dir();
        let (registry, mut event_rx) = create_test_registry();
        let tool = GlobTool::with_default_path(registry.clone(), temp.path().to_path_buf());

        let mut input = HashMap::new();
        // Invalid glob pattern (unclosed bracket)
        input.insert(
            "pattern".to_string(),
            serde_json::Value::String("[invalid".to_string()),
        );

        let context = ToolContext {
            session_id: 1,
            tool_use_id: "test-glob-invalid".to_string(),
            turn_id: None,
            permissions_pre_approved: false,
        };

        let registry_clone = registry.clone();
        tokio::spawn(async move {
            if let Some(ControllerEvent::PermissionRequired { tool_use_id, .. }) =
                event_rx.recv().await
            {
                registry_clone
                    .respond_to_request(&tool_use_id, grant_once())
                    .await
                    .unwrap();
            }
        });

        let result = tool.execute(context, input).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Invalid glob pattern"));
    }

    #[tokio::test]
    async fn test_missing_pattern() {
        let (registry, _event_rx) = create_test_registry();
        let tool = GlobTool::new(registry);

        let input = HashMap::new();

        let context = ToolContext {
            session_id: 1,
            tool_use_id: "test".to_string(),
            turn_id: None,
            permissions_pre_approved: false,
        };

        let result = tool.execute(context, input).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Missing required 'pattern'"));
    }

    #[tokio::test]
    async fn test_nonexistent_path() {
        let (registry, _event_rx) = create_test_registry();
        let tool = GlobTool::new(registry);

        let mut input = HashMap::new();
        input.insert(
            "pattern".to_string(),
            serde_json::Value::String("*.rs".to_string()),
        );
        input.insert(
            "path".to_string(),
            serde_json::Value::String("/nonexistent/path".to_string()),
        );

        let context = ToolContext {
            session_id: 1,
            tool_use_id: "test".to_string(),
            turn_id: None,
            permissions_pre_approved: false,
        };

        let result = tool.execute(context, input).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("does not exist"));
    }

    #[test]
    fn test_compact_summary() {
        let (registry, _event_rx) = create_test_registry();
        let tool = GlobTool::new(registry);

        let mut input = HashMap::new();
        input.insert(
            "pattern".to_string(),
            serde_json::Value::String("**/*.rs".to_string()),
        );

        let result = "/path/main.rs\n/path/lib.rs\n/path/test.rs";
        let summary = tool.compact_summary(&input, result);
        assert_eq!(summary, "[Glob: **/*.rs (3 files)]");
    }

    #[test]
    fn test_compact_summary_no_matches() {
        let (registry, _event_rx) = create_test_registry();
        let tool = GlobTool::new(registry);

        let mut input = HashMap::new();
        input.insert(
            "pattern".to_string(),
            serde_json::Value::String("*.xyz".to_string()),
        );

        let result = "No files found matching pattern '*.xyz' in '/path'";
        let summary = tool.compact_summary(&input, result);
        assert_eq!(summary, "[Glob: *.xyz (0 files)]");
    }

    #[test]
    fn test_build_permission_request() {
        let request = GlobTool::build_permission_request("tool-123", "/path/to/src", "**/*.rs");

        assert_eq!(request.description, "Glob search in: /path/to/src");
        assert_eq!(
            request.reason,
            Some("Search for '**/*.rs' pattern".to_string())
        );
        assert_eq!(request.target, GrantTarget::path("/path/to/src", true));
        assert_eq!(request.required_level, PermissionLevel::Read);
    }
}