claude-code-acp-rs 0.1.22

Use Claude Code from any ACP client - A Rust implementation of Claude Code ACP Agent
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
//! Bash tool implementation
//!
//! Executes shell commands with security protections.
//! Supports both direct process execution and Client-side PTY via Terminal API.

use async_trait::async_trait;
use sacp::schema::ToolCallStatus;
use serde::Deserialize;
use serde_json::json;
use std::process::Stdio;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
use tokio::time::timeout;
use uuid::Uuid;

use super::base::{Tool, ToolKind};
use crate::mcp::registry::{ToolContext, ToolResult};
use crate::session::{BackgroundTerminal, ChildHandle, TerminalExitStatus, WrappedChild};
use crate::terminal::TerminalClient;

// Process group management
use process_wrap::tokio::*;

/// Maximum output size in characters
const MAX_OUTPUT_SIZE: usize = 30_000;

/// Shell operators that indicate command chaining (security risk)
///
/// These operators allow chaining multiple commands, which could be used
/// for command injection attacks. Commands containing these operators
/// should be handled with extra care in permission rules.
const SHELL_OPERATORS: &[&str] = &["&&", "||", ";", "|", "$(", "`", "\n"];

/// Check if a command string contains shell operators
///
/// This is used to prevent command injection when matching prefix-based
/// permission rules. For example, if a rule allows `npm run:*`, we must
/// ensure that `npm run build && rm -rf /` doesn't match by detecting
/// the `&&` operator in the remainder after the prefix.
///
/// # Examples
///
/// ```
/// use claude_code_acp::mcp::tools::contains_shell_operator;
///
/// assert!(contains_shell_operator("ls && rm -rf /"));
/// assert!(contains_shell_operator("cat file | grep secret"));
/// assert!(contains_shell_operator("$(whoami)"));
/// assert!(!contains_shell_operator("npm run build"));
/// ```
pub fn contains_shell_operator(command: &str) -> bool {
    SHELL_OPERATORS.iter().any(|op| command.contains(op))
}

/// Bash tool for executing shell commands
#[derive(Debug, Default)]
pub struct BashTool;

/// Bash tool input parameters
#[derive(Debug, Deserialize)]
struct BashInput {
    /// The command to execute
    command: String,
    /// Optional description of what the command does
    #[serde(default)]
    description: Option<String>,
    /// Optional timeout in milliseconds
    #[serde(default)]
    timeout: Option<u64>,
    /// Run command in background (returns immediately with shell ID)
    #[serde(default)]
    run_in_background: Option<bool>,
}

impl BashTool {
    /// Create a new Bash tool instance
    pub fn new() -> Self {
        Self
    }

    /// Safely truncate string to maximum size, respecting UTF-8 character boundaries
    fn safe_truncate(s: &mut String, max_len: usize) {
        if s.len() > max_len {
            // Handle edge case: max_len is 0
            if max_len == 0 {
                s.clear();
                s.push_str("... (output truncated)");
                return;
            }

            // Find valid UTF-8 boundary
            let mut truncate_at = max_len;
            while truncate_at > 0 && !s.is_char_boundary(truncate_at) {
                truncate_at -= 1;
            }
            s.truncate(truncate_at);
            s.push_str("\n... (output truncated)");
        }
    }

    /// Check permission before executing the tool
    ///
    /// Note: Permission checking is now handled at the SDK level.
    /// The SDK's `mcp_message` handler calls `can_use_tool` callback before executing MCP tools.
    /// This method is kept for potential future tool-specific permission logic.
    fn check_permission(
        &self,
        _input: &serde_json::Value,
        _context: &ToolContext,
    ) -> Option<ToolResult> {
        // Permission check is handled by SDK's can_use_tool callback
        None
    }
}

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

    fn description(&self) -> &str {
        "Execute a shell command. Commands are run in a bash shell with the session's working directory. Use for git, npm, build tools, and other terminal operations."
    }

    fn input_schema(&self) -> serde_json::Value {
        json!({
            "type": "object",
            "required": ["command"],
            "properties": {
                "command": {
                    "type": "string",
                    "description": "The shell command to execute"
                },
                "description": {
                    "type": "string",
                    "description": "A short description of what this command does"
                },
                "timeout": {
                    "type": "integer",
                    "description": "Timeout in milliseconds (max 600000, default 120000)"
                },
                "run_in_background": {
                    "type": "boolean",
                    "description": "Run command in background. Returns immediately with a shell ID that can be used with BashOutput to retrieve output."
                }
            }
        })
    }

    fn kind(&self) -> ToolKind {
        ToolKind::Execute
    }

    fn requires_permission(&self) -> bool {
        true // Command execution requires permission
    }

    async fn execute(&self, input: serde_json::Value, context: &ToolContext) -> ToolResult {
        // Check permission before executing
        if let Some(result) = self.check_permission(&input, context) {
            return result;
        }

        // Parse input
        let params: BashInput = match serde_json::from_value(input) {
            Ok(p) => p,
            Err(e) => return ToolResult::error(format!("Invalid input: {}", e)),
        };

        // Prefer Terminal API when available (Client-side PTY)
        if let Some(terminal_client) = context.terminal_client() {
            if params.run_in_background.unwrap_or(false) {
                return self
                    .execute_terminal_background(&params, terminal_client, context)
                    .await;
            }
            return self
                .execute_terminal_foreground(&params, terminal_client, context)
                .await;
        }

        // Fall back to direct process execution
        if params.run_in_background.unwrap_or(false) {
            return self.execute_background(&params, context);
        }

        self.execute_foreground(&params, context).await
    }
}

impl BashTool {
    /// Execute command in foreground (blocking)
    async fn execute_foreground(&self, params: &BashInput, context: &ToolContext) -> ToolResult {
        let cmd_start = Instant::now();

        // Use timeout as specified by user, without limiting maximum
        let timeout_ms = params.timeout;

        // Stage 1: Build the command
        let build_start = Instant::now();
        let mut cmd = Command::new("bash");
        cmd.arg("-c")
            .arg(&params.command)
            .current_dir(&context.cwd)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        let build_duration = build_start.elapsed();

        tracing::debug!(
            command = %params.command,
            build_duration_ms = build_duration.as_millis(),
            timeout_ms = ?timeout_ms,
            "Bash command built"
        );

        // Stage 2: Execute with or without timeout
        let exec_start = Instant::now();
        let output = if let Some(ms) = timeout_ms {
            // User specified timeout - apply it
            let timeout_duration = Duration::from_millis(ms);
            match timeout(timeout_duration, cmd.output()).await {
                Ok(Ok(output)) => output,
                Ok(Err(e)) => {
                    let exec_duration = exec_start.elapsed();
                    tracing::error!(
                        command = %params.command,
                        exec_duration_ms = exec_duration.as_millis(),
                        error = %e,
                        "Bash command execution failed"
                    );
                    return ToolResult::error(format!("Failed to execute command: {}", e));
                }
                Err(_) => {
                    let exec_duration = exec_start.elapsed();
                    tracing::warn!(
                        command = %params.command,
                        exec_duration_ms = exec_duration.as_millis(),
                        timeout_ms = ms,
                        "Bash command timed out"
                    );
                    return ToolResult::error(format!("Command timed out after {}ms", ms));
                }
            }
        } else {
            // No timeout specified - execute without timeout limit
            match cmd.output().await {
                Ok(output) => output,
                Err(e) => {
                    let exec_duration = exec_start.elapsed();
                    tracing::error!(
                        command = %params.command,
                        exec_duration_ms = exec_duration.as_millis(),
                        error = %e,
                        "Bash command execution failed"
                    );
                    return ToolResult::error(format!("Failed to execute command: {}", e));
                }
            }
        };
        let exec_duration = exec_start.elapsed();

        // Stage 3: Process output
        let process_start = Instant::now();
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);

        let mut result_text = String::new();

        // Add stdout
        if !stdout.is_empty() {
            result_text.push_str(&stdout);
        }

        // Add stderr (prefixed if there's also stdout)
        if !stderr.is_empty() {
            if !result_text.is_empty() {
                result_text.push_str("\n--- stderr ---\n");
            }
            result_text.push_str(&stderr);
        }

        // Truncate if too long (using UTF-8 safe truncation)
        let was_truncated = result_text.len() > MAX_OUTPUT_SIZE;
        Self::safe_truncate(&mut result_text, MAX_OUTPUT_SIZE);

        // Handle empty output
        if result_text.is_empty() {
            result_text = "(no output)".to_string();
        }

        let process_duration = process_start.elapsed();
        let total_elapsed = cmd_start.elapsed();

        let exit_code = output.status.code().unwrap_or(-1);
        let success = output.status.success();

        // Log execution summary
        tracing::info!(
            command = %params.command,
            exit_code = exit_code,
            success = success,
            build_duration_ms = build_duration.as_millis(),
            exec_duration_ms = exec_duration.as_millis(),
            process_duration_ms = process_duration.as_millis(),
            total_elapsed_ms = total_elapsed.as_millis(),
            output_size_bytes = result_text.len(),
            was_truncated = was_truncated,
            "Bash command execution summary"
        );

        if success {
            ToolResult::success(result_text).with_metadata(json!({
                "exit_code": exit_code,
                "truncated": was_truncated,
                "description": params.description,
                "total_elapsed_ms": total_elapsed.as_millis(),
                "exec_duration_ms": exec_duration.as_millis()
            }))
        } else {
            ToolResult::error(format!(
                "Command failed with exit code {}\n{}",
                exit_code, result_text
            ))
            .with_metadata(json!({
                "exit_code": exit_code,
                "truncated": was_truncated,
                "total_elapsed_ms": total_elapsed.as_millis(),
                "exec_duration_ms": exec_duration.as_millis()
            }))
        }
    }

    /// Execute command in background (non-blocking)
    fn execute_background(&self, params: &BashInput, context: &ToolContext) -> ToolResult {
        // Get background process manager
        let manager = match context.background_processes() {
            Some(m) => m.clone(),
            None => {
                return ToolResult::error("Background process manager not available");
            }
        };

        // Build the command with process-wrap for process group support
        let mut cmd = CommandWrap::with_new("bash", |c| {
            c.arg("-c")
                .arg(&params.command)
                .current_dir(&context.cwd)
                .stdout(Stdio::piped())
                .stderr(Stdio::piped());
        });

        // Add platform-specific wrapper for process group management
        #[cfg(unix)]
        cmd.wrap(ProcessGroup::leader());

        #[cfg(windows)]
        cmd.wrap(JobObject::new());

        // Spawn the process
        let mut wrapped_child = match cmd.spawn() {
            Ok(c) => c,
            Err(e) => return ToolResult::error(format!("Failed to spawn command: {}", e)),
        };

        // Extract stdout and stderr BEFORE wrapping (ChildWrapper doesn't expose them)
        let stdout = wrapped_child.stdout().take();
        let stderr = wrapped_child.stderr().take();

        // Generate shell ID
        let shell_id = format!("shell-{}", Uuid::new_v4().simple());

        // Create wrapped child handle (stdout/stderr not stored in handle)
        let child_handle = ChildHandle::Wrapped {
            child: Arc::new(tokio::sync::Mutex::new(WrappedChild::new(wrapped_child))),
        };

        // Create background terminal
        let terminal = BackgroundTerminal::new_running(child_handle);

        // Get reference to output buffer for the read task
        let output_buffer = match &terminal {
            BackgroundTerminal::Running { output_buffer, .. } => output_buffer.clone(),
            BackgroundTerminal::Finished { .. } => unreachable!(),
        };

        // Register with manager
        let shell_id_clone = shell_id.clone();
        manager.register(shell_id.clone(), terminal);

        // Spawn task to read output
        let manager_clone = manager.clone();
        let description = params.description.clone();
        tokio::spawn(async move {
            let mut combined_output = String::new();

            // Read stdout
            if let Some(stdout) = stdout {
                let reader = BufReader::new(stdout);
                let mut lines = reader.lines();
                while let Ok(Some(line)) = lines.next_line().await {
                    combined_output.push_str(&line);
                    combined_output.push('\n');
                    let mut buffer = output_buffer.lock().await;
                    buffer.push_str(&line);
                    buffer.push('\n');
                }
            }

            // Read stderr
            if let Some(stderr) = stderr {
                let reader = BufReader::new(stderr);
                let mut lines = reader.lines();
                while let Ok(Some(line)) = lines.next_line().await {
                    if !combined_output.is_empty() && !combined_output.ends_with('\n') {
                        combined_output.push('\n');
                    }
                    combined_output.push_str(&line);
                    combined_output.push('\n');
                    let mut buffer = output_buffer.lock().await;
                    buffer.push_str(&line);
                    buffer.push('\n');
                }
            }

            // Wait for process to finish and update terminal state
            // Use get() instead of get_mut() because BackgroundTerminal contains ChildHandle
            // We only need a shared reference to clone the ChildHandle
            if let Some(terminal_ref) = manager_clone.get(&shell_id_clone) {
                if let BackgroundTerminal::Running { child, .. } = &*terminal_ref {
                    // Clone the ChildHandle to hold it across await points
                    let mut child_handle = child.clone();
                    drop(terminal_ref); // Release DashMap read lock before await

                    // ChildHandle::wait() handles locking internally
                    if let Ok(status) = child_handle.wait().await {
                        let exit_code = status.code().unwrap_or(-1);
                        manager_clone
                            .finish_terminal(&shell_id_clone, TerminalExitStatus::Exited(exit_code))
                            .await;
                    } else {
                        manager_clone
                            .finish_terminal(&shell_id_clone, TerminalExitStatus::Aborted)
                            .await;
                    }
                }
            }
        });

        // Return immediately with shell ID
        ToolResult::success(format!(
            "Command started in background.\n\nShell ID: {}\n\nUse BashOutput to check status and retrieve output.",
            shell_id
        )).with_metadata(json!({
            "shell_id": shell_id,
            "status": "running",
            "description": description
        }))
    }

    /// Execute command using Terminal API in foreground (blocking)
    ///
    /// Uses Client-side PTY for execution, which provides better terminal
    /// emulation and real-time output streaming.
    async fn execute_terminal_foreground(
        &self,
        params: &BashInput,
        terminal_client: &Arc<TerminalClient>,
        context: &ToolContext,
    ) -> ToolResult {
        // Use timeout as specified by user, without limiting maximum
        let timeout_ms = params.timeout;

        // Create terminal with bash -c command
        let terminal_id = match terminal_client
            .create(
                "bash",
                vec!["-c".to_string(), params.command.clone()],
                Some(context.cwd.clone()),
                Some(MAX_OUTPUT_SIZE as u64),
            )
            .await
        {
            Ok(id) => id,
            Err(e) => return ToolResult::error(format!("Failed to create terminal: {}", e)),
        };

        // Send ToolCallUpdate with terminal_id immediately
        // This allows the client (e.g., Zed) to start showing terminal output
        if let Err(e) = context.send_terminal_update(
            terminal_id.0.as_ref(),
            ToolCallStatus::InProgress,
            params.description.as_deref(),
        ) {
            tracing::debug!("Failed to send terminal update: {}", e);
            // Continue even if notification fails - tool should still work
        }

        // Wait for command to exit with optional timeout
        let exit_result = if let Some(ms) = timeout_ms {
            // User specified timeout - apply it
            let timeout_duration = Duration::from_millis(ms);
            timeout(
                timeout_duration,
                terminal_client.wait_for_exit(terminal_id.clone()),
            )
            .await
        } else {
            // No timeout - wait indefinitely
            Ok(terminal_client.wait_for_exit(terminal_id.clone()).await)
        };

        // Get output regardless of exit status
        let output = match terminal_client.output(terminal_id.clone()).await {
            Ok(resp) => resp.output,
            Err(e) => format!("(failed to get output: {})", e),
        };

        // Release terminal (ignore result - best effort)
        drop(terminal_client.release(terminal_id).await);

        // Process result
        match exit_result {
            Ok(Ok(exit_response)) => {
                let exit_status = exit_response.exit_status;
                // exit_code is Option<u32>, convert to i32 for compatibility
                #[allow(clippy::cast_possible_wrap)]
                let exit_code = exit_status.exit_code.map(|c| c as i32).unwrap_or(-1);

                // Apply UTF-8 safe truncation if needed
                let mut result_text = if output.is_empty() {
                    "(no output)".to_string()
                } else {
                    output
                };

                let was_truncated = result_text.len() > MAX_OUTPUT_SIZE;
                Self::safe_truncate(&mut result_text, MAX_OUTPUT_SIZE);

                if exit_code == 0 {
                    ToolResult::success(result_text).with_metadata(json!({
                        "exit_code": exit_code,
                        "truncated": was_truncated,
                        "description": params.description,
                        "terminal_api": true
                    }))
                } else {
                    ToolResult::error(format!(
                        "Command failed with exit code {}\n{}",
                        exit_code, result_text
                    ))
                    .with_metadata(json!({
                        "exit_code": exit_code,
                        "truncated": was_truncated,
                        "terminal_api": true
                    }))
                }
            }
            Ok(Err(e)) => ToolResult::error(format!("Terminal execution failed: {}", e)),
            Err(_) => {
                // Timeout occurred - timeout_ms must be Some in this branch
                let ms = timeout_ms.unwrap_or(0);
                ToolResult::error(format!("Command timed out after {}ms\n{}", ms, output))
            }
        }
    }

    /// Execute command using Terminal API in background (non-blocking)
    ///
    /// Creates a terminal via Client-side PTY and returns immediately.
    /// The terminal_id serves as the shell_id for later queries.
    async fn execute_terminal_background(
        &self,
        params: &BashInput,
        terminal_client: &Arc<TerminalClient>,
        context: &ToolContext,
    ) -> ToolResult {
        // Create terminal with bash -c command
        let terminal_id = match terminal_client
            .create(
                "bash",
                vec!["-c".to_string(), params.command.clone()],
                Some(context.cwd.clone()),
                None, // No output limit for background
            )
            .await
        {
            Ok(id) => id,
            Err(e) => return ToolResult::error(format!("Failed to create terminal: {}", e)),
        };

        // Use terminal_id as shell_id (prefixed for clarity)
        let shell_id = format!("term-{}", terminal_id.0.as_ref());

        // Send ToolCallUpdate with terminal_id immediately
        // This allows the client (e.g., Zed) to start showing terminal output
        if let Err(e) = context.send_terminal_update(
            terminal_id.0.as_ref(),
            ToolCallStatus::InProgress,
            params.description.as_deref(),
        ) {
            tracing::debug!("Failed to send terminal update: {}", e);
            // Continue even if notification fails - tool should still work
        }

        // Return immediately with shell ID
        ToolResult::success(format!(
            "Command started in background via Terminal API.\n\nShell ID: {}\n\nUse BashOutput to check status and retrieve output.",
            shell_id
        )).with_metadata(json!({
            "shell_id": shell_id,
            "terminal_id": terminal_id.0.as_ref(),
            "status": "running",
            "description": params.description,
            "terminal_api": true
        }))
    }
}

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

    #[tokio::test]
    async fn test_bash_echo() {
        let temp_dir = TempDir::new().unwrap();
        let tool = BashTool::new();
        let context = ToolContext::new("test", temp_dir.path());

        let result = tool
            .execute(
                json!({
                    "command": "echo 'Hello, World!'"
                }),
                &context,
            )
            .await;

        assert!(!result.is_error);
        assert!(result.content.contains("Hello, World!"));
    }

    #[tokio::test]
    async fn test_bash_with_cwd() {
        let temp_dir = TempDir::new().unwrap();
        let tool = BashTool::new();
        let context = ToolContext::new("test", temp_dir.path());

        let result = tool
            .execute(
                json!({
                    "command": "pwd"
                }),
                &context,
            )
            .await;

        assert!(!result.is_error);
        assert!(result.content.contains(temp_dir.path().to_str().unwrap()));
    }

    #[tokio::test]
    async fn test_bash_failure() {
        let temp_dir = TempDir::new().unwrap();
        let tool = BashTool::new();
        let context = ToolContext::new("test", temp_dir.path());

        let result = tool
            .execute(
                json!({
                    "command": "exit 1"
                }),
                &context,
            )
            .await;

        assert!(result.is_error);
        assert!(result.content.contains("exit code 1"));
    }

    #[tokio::test]
    async fn test_bash_stderr() {
        let temp_dir = TempDir::new().unwrap();
        let tool = BashTool::new();
        let context = ToolContext::new("test", temp_dir.path());

        let result = tool
            .execute(
                json!({
                    "command": "echo 'error message' >&2"
                }),
                &context,
            )
            .await;

        assert!(!result.is_error);
        assert!(result.content.contains("error message"));
    }

    #[tokio::test]
    async fn test_bash_timeout() {
        let temp_dir = TempDir::new().unwrap();
        let tool = BashTool::new();
        let context = ToolContext::new("test", temp_dir.path());

        let result = tool
            .execute(
                json!({
                    "command": "sleep 10",
                    "timeout": 100
                }),
                &context,
            )
            .await;

        assert!(result.is_error);
        assert!(result.content.contains("timed out"));
    }

    #[test]
    fn test_bash_tool_properties() {
        let tool = BashTool::new();
        assert_eq!(tool.name(), "Bash");
        assert_eq!(tool.kind(), ToolKind::Execute);
        assert!(tool.requires_permission());
    }

    #[test]
    fn test_shell_operator_detection() {
        // Commands with shell operators (should be detected)
        assert!(contains_shell_operator("ls && rm -rf /"));
        assert!(contains_shell_operator("cat file || echo fail"));
        assert!(contains_shell_operator("echo a; echo b"));
        assert!(contains_shell_operator("cat file | grep secret"));
        assert!(contains_shell_operator("echo $(whoami)"));
        assert!(contains_shell_operator("echo `whoami`"));
        assert!(contains_shell_operator("echo a\necho b"));

        // Safe commands (should not be detected)
        assert!(!contains_shell_operator("npm run build"));
        assert!(!contains_shell_operator("git status"));
        assert!(!contains_shell_operator("cargo test --release"));
        assert!(!contains_shell_operator("ls -la /tmp"));
        assert!(!contains_shell_operator("echo 'hello world'"));
    }

    #[test]
    fn test_shell_operator_prefix_matching() {
        // Simulate prefix matching scenario
        let prefix = "npm run ";
        let command = "npm run build && malicious";

        // After prefix match, check remainder for operators
        let remainder = &command[prefix.len()..];
        assert!(contains_shell_operator(remainder));

        // Safe case
        let safe_command = "npm run build --watch";
        let safe_remainder = &safe_command[prefix.len()..];
        assert!(!contains_shell_operator(safe_remainder));
    }
}