tinyharness-lib 0.5.6

Core library for TinyHarness: LLM providers, tools, sessions and context management
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
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use tokio::io::AsyncReadExt;

/// A shell command with template substitution and timeout.
///
/// The `command` field is a template string with `{var}` placeholders.
/// At execution time, the placeholders are replaced with values from a
/// `HashMap<String, String>`. The command is run via `sh -c` (or `cmd /C`
/// on Windows) with the substituted variables also available as `TH_*`
/// environment variables.
///
/// This type backs the custom-tools system: each custom tool wraps a
/// `ShellCommand` template executed with the tool's arguments substituted.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShellCommand {
    /// Command template, e.g. `"docker run --rm {image} sh -c \"$TH_COMMAND\""`.
    pub command: String,
    /// Timeout in seconds. Default: 30.
    #[serde(default = "default_timeout")]
    pub timeout_secs: u64,
    /// Working directory for the command. If `None`, inherits the current
    /// directory of the TinyHarness process.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cwd: Option<String>,
}

fn default_timeout() -> u64 {
    30
}

impl ShellCommand {
    /// Shell-escape a value for safe inline substitution into an unquoted
    /// position in a shell command.
    ///
    /// Wraps the value in single quotes and escapes any single quotes within
    /// it by replacing `'` with `'\''`. This prevents shell metacharacters
    /// in the value (e.g. from LLM responses) from breaking the command.
    fn shell_escape(value: &str) -> String {
        // Replace ' with '\'' and wrap in single quotes.
        // e.g.  I'll do it  →  'I'\''ll do it'
        let escaped = value.replace('\'', "'\\''");
        format!("'{}'", escaped)
    }

    /// Escape a value for safe substitution inside a double-quoted string in
    /// a shell command.
    ///
    /// Inside double quotes, the special characters are `"`, `\`, `$`, and
    /// backtick. These are backslash-escaped to prevent expansion or early
    /// quote termination.
    fn escape_for_double_quote(value: &str) -> String {
        value
            .replace('\\', "\\\\")
            .replace('"', "\\\"")
            .replace('$', "\\$")
            .replace('`', "\\`")
    }

    /// Render the command template by substituting `{var}` placeholders.
    ///
    /// The renderer is **quote-context-aware**: it tracks whether each
    /// `{var}` placeholder appears inside single quotes, double quotes, or
    /// unquoted in the template, and applies the appropriate escaping:
    ///
    /// | Context | Strategy |
    /// |---------|----------|
    /// | Unquoted | Wrap in single quotes (`shell_escape`) |
    /// | Inside `"..."` | Escape `"`, `\`, `$`, `` ` `` for double-quote context |
    /// | Inside `'...'` | Close the quote, insert `shell_escape`d value, reopen |
    ///
    /// This prevents broken quoting when the template already wraps a
    /// placeholder in quotes, e.g. `echo "{user_input}"` or `echo '{tool}'`.
    ///
    /// Missing variables (not present in `vars`) are left as-is — the
    /// `{placeholder}` appears literally in the rendered command.
    pub fn render(&self, vars: &HashMap<String, String>) -> String {
        let chars: Vec<char> = self.command.chars().collect();
        let mut result = String::new();
        let mut i = 0;
        let mut in_single = false;
        let mut in_double = false;

        while i < chars.len() {
            let c = chars[i];

            if c == '\'' && !in_double {
                in_single = !in_single;
                result.push(c);
                i += 1;
            } else if c == '"' && !in_single {
                in_double = !in_double;
                result.push(c);
                i += 1;
            } else if c == '{' {
                // Try to find a matching `}` and look up the var name.
                if let Some(end) = chars[i + 1..].iter().position(|&ch| ch == '}') {
                    let var_name: String = chars[i + 1..i + 1 + end].iter().collect();
                    if let Some(value) = vars.get(&var_name) {
                        if in_double {
                            // Inside double quotes: escape special chars
                            // for double-quote context.
                            result.push_str(&Self::escape_for_double_quote(value));
                        } else if in_single {
                            // Inside single quotes: close the quote, insert
                            // the shell-escaped value, then reopen.
                            // e.g. 'It is {var}' with value=it's →
                            //   'It is '\''it'\''s''
                            result.push('\'');
                            result.push_str(&Self::shell_escape(value));
                            result.push('\'');
                        } else {
                            // Unquoted: wrap in single quotes.
                            result.push_str(&Self::shell_escape(value));
                        }
                        i += end + 2;
                        continue;
                    }
                }
                // Not a known var or no closing `}` — push literally.
                result.push(c);
                i += 1;
            } else {
                result.push(c);
                i += 1;
            }
        }

        result
    }

    /// The `TH_*` / raw environment variables for these tool arguments.
    fn environment(&self, vars: &HashMap<String, String>) -> Vec<(String, String)> {
        let mut out = Vec::with_capacity(vars.len() * 2);
        for (key, value) in vars {
            out.push((
                format!("TH_{}", key.to_uppercase().replace([' ', '-'], "_")),
                value.clone(),
            ));
        }
        // Also the raw key, for templates that use `$name` instead of `$TH_NAME`.
        for (key, value) in vars {
            out.push((key.clone(), value.clone()));
        }
        out
    }

    /// Execute the command asynchronously.
    ///
    /// Returns `Ok(stdout)` on success (exit code 0), or `Err(message)` on
    /// failure (non-zero exit, timeout, or spawn error).
    ///
    /// The `vars` are used for both `{var}` template substitution and as
    /// `TH_*` environment variables.
    pub async fn execute(&self, vars: &HashMap<String, String>) -> Result<String, String> {
        self.execute_with_sandbox(vars, None).await
    }

    /// [`ShellCommand::execute`] routed through an OS-level process sandbox.
    ///
    /// Custom tools are arbitrary shell templates, so they get the same
    /// containment as the `run` tool: with a sandbox configured, the command
    /// runs inside it.
    pub async fn execute_with_sandbox(
        &self,
        vars: &HashMap<String, String>,
        sandbox: Option<&crate::sandbox::Sandbox>,
    ) -> Result<String, String> {
        let rendered = self.render(vars);
        let env = self.environment(vars);

        let mut cmd = match (sandbox, cfg!(target_os = "windows")) {
            (Some(sb), _) => {
                // bwrap clears the environment inside the sandbox, so the
                // tool's variables travel as explicit --setenv arguments
                // rather than as inherited env vars.
                let (program, sb_args) = sb.argv_with_env(
                    &rendered,
                    self.cwd.as_deref().map(std::path::Path::new),
                    &env,
                );
                let mut c = tokio::process::Command::new(program);
                c.args(sb_args);
                c
            }
            (None, true) => {
                let mut c = tokio::process::Command::new("cmd");
                c.arg("/C").arg(&rendered);
                c
            }
            (None, false) => {
                let mut c = tokio::process::Command::new("sh");
                c.arg("-c").arg(&rendered);
                c
            }
        };

        // Set environment variables. Outside the sandbox these are inherited
        // normally; inside it they are redundant but harmless, and keeping the
        // call makes both paths behave identically for firejail (which, unlike
        // bwrap, does not clear the environment).
        for (key, value) in &env {
            cmd.env(key, value);
        }

        // The CWD is set on the outer process for both paths: it is a real
        // host directory, and bwrap binds the workspace at the same absolute
        // path, so it resolves inside the sandbox too. (firejail has no
        // --chdir and relies on this inheritance.)
        if let Some(ref cwd) = self.cwd {
            cmd.current_dir(cwd);
        }

        #[cfg(unix)]
        cmd.process_group(0);

        let mut child = cmd
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .map_err(|e| format!("Failed to spawn command: {}", e))?;

        let timeout = tokio::time::Duration::from_secs(self.timeout_secs);
        let wait_result = tokio::time::timeout(timeout, child.wait()).await;

        match wait_result {
            Ok(Ok(status)) => {
                let stdout = read_pipe(child.stdout.take()).await;
                let stderr = read_pipe(child.stderr.take()).await;

                if status.success() {
                    Ok(stdout.trim().to_string())
                } else {
                    let mut msg =
                        format!("Command exited with code {}", status.code().unwrap_or(-1));
                    if !stderr.is_empty() {
                        msg.push_str(&format!("\n{}", stderr.trim()));
                    }
                    if !stdout.is_empty() {
                        msg.push_str(&format!("\n{}", stdout.trim()));
                    }
                    Err(msg)
                }
            }
            Ok(Err(e)) => {
                crate::tools::run::kill_child(child).await;
                Err(format!("Failed to wait for command: {}", e))
            }
            Err(_) => {
                crate::tools::run::kill_child(child).await;
                Err(format!(
                    "Command timed out after {}s: {}",
                    self.timeout_secs, rendered
                ))
            }
        }
    }
}

/// Read a piped stdout/stderr to a string, truncating at 10 KB.
async fn read_pipe<T: tokio::io::AsyncRead + Unpin>(mut pipe: Option<T>) -> String {
    if let Some(ref mut r) = pipe.as_mut() {
        let mut buf = String::new();
        let _ = r.read_to_string(&mut buf).await;
        buf
    } else {
        String::new()
    }
}

/// Execute a shell command for a custom tool, substituting tool arguments.
///
/// The tool arguments are passed as a `HashMap<String, String>` (same as
/// built-in tools).
pub async fn execute_shell_tool(command: &ShellCommand, args: &HashMap<String, String>) -> String {
    execute_shell_tool_with_sandbox(command, args, None).await
}

/// [`execute_shell_tool`] routed through an OS-level process sandbox.
pub async fn execute_shell_tool_with_sandbox(
    command: &ShellCommand,
    args: &HashMap<String, String>,
    sandbox: Option<&crate::sandbox::Sandbox>,
) -> String {
    match command.execute_with_sandbox(args, sandbox).await {
        Ok(stdout) => {
            if stdout.is_empty() {
                "(no output)".to_string()
            } else {
                stdout
            }
        }
        Err(e) => format!("Error: {}", e),
    }
}

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

    #[test]
    fn test_render_basic_substitution() {
        let cmd = ShellCommand {
            command: "echo {name}".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("name".to_string(), "world".to_string());
        // Values are shell-escaped (wrapped in single quotes)
        assert_eq!(cmd.render(&vars), "echo 'world'");
    }

    #[test]
    fn test_render_multiple_vars() {
        let cmd = ShellCommand {
            command: "docker run --rm {image} sh -c {command}".to_string(),
            timeout_secs: 30,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("image".to_string(), "ubuntu:latest".to_string());
        vars.insert("command".to_string(), "ls -la".to_string());
        // Each value is individually shell-escaped
        assert_eq!(
            cmd.render(&vars),
            "docker run --rm 'ubuntu:latest' sh -c 'ls -la'"
        );
    }

    #[test]
    fn test_render_shell_escapes_single_quotes() {
        let cmd = ShellCommand {
            command: "echo {text}".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("text".to_string(), "I'll do it".to_string());
        // Single quotes in the value are escaped: ' -> '\''
        assert_eq!(cmd.render(&vars), "echo 'I'\\''ll do it'");
    }

    #[test]
    fn test_render_shell_escapes_metacharacters() {
        let cmd = ShellCommand {
            command: "echo -n {response} | wc -c".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert(
            "response".to_string(),
            "I'll find all files in `./src` and run $(whoami)".to_string(),
        );
        let rendered = cmd.render(&vars);
        // The value is safely quoted — metacharacters are inert
        assert_eq!(
            rendered,
            "echo -n 'I'\\''ll find all files in `./src` and run $(whoami)' | wc -c"
        );
    }

    #[test]
    fn test_render_inside_double_quotes() {
        let cmd = ShellCommand {
            command: "echo \"{text}\"".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("text".to_string(), "He said \"hi\" and $HOME".to_string());
        // Inside double quotes: escape ", \, $, ` for double-quote context
        let rendered = cmd.render(&vars);
        assert_eq!(rendered, "echo \"He said \\\"hi\\\" and \\$HOME\"");
    }

    #[test]
    fn test_render_inside_double_quotes_backtick() {
        let cmd = ShellCommand {
            command: "echo \"{text}\"".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("text".to_string(), "run `whoami`".to_string());
        let rendered = cmd.render(&vars);
        assert_eq!(rendered, "echo \"run \\`whoami\\`\"");
    }

    #[test]
    fn test_render_inside_double_quotes_simple() {
        let cmd = ShellCommand {
            command: "echo \"{name}\"".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("name".to_string(), "world".to_string());
        // Simple value inside double quotes — no extra quoting needed
        let rendered = cmd.render(&vars);
        assert_eq!(rendered, "echo \"world\"");
    }

    #[test]
    fn test_render_inside_single_quotes() {
        let cmd = ShellCommand {
            command: "echo '{text}'".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("text".to_string(), "it's here".to_string());
        let rendered = cmd.render(&vars);
        assert_eq!(rendered, "echo '''it'\\''s here'''");
    }

    #[test]
    fn test_render_inside_single_quotes_simple() {
        let cmd = ShellCommand {
            command: "echo '{name}'".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("name".to_string(), "world".to_string());
        let rendered = cmd.render(&vars);
        assert_eq!(rendered, "echo '''world'''");
    }

    #[test]
    fn test_render_mixed_quotes_multiple_vars() {
        let cmd = ShellCommand {
            command: "echo \"{greeting}\" '{name}'".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("greeting".to_string(), "hello $USER".to_string());
        vars.insert("name".to_string(), "world".to_string());
        let rendered = cmd.render(&vars);
        assert_eq!(rendered, "echo \"hello \\$USER\" '''world'''");
    }

    #[test]
    fn test_render_nested_brace_not_var() {
        let cmd = ShellCommand {
            command: "echo {not_a_var}".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let vars = HashMap::new();
        // Unknown var stays as literal {not_a_var}
        assert_eq!(cmd.render(&vars), "echo {not_a_var}");
    }

    #[test]
    fn test_render_unclosed_brace() {
        let cmd = ShellCommand {
            command: "echo {name".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("name".to_string(), "world".to_string());
        // No closing } — push { literally
        assert_eq!(cmd.render(&vars), "echo {name");
    }

    #[test]
    fn test_render_missing_var_stays_as_placeholder() {
        let cmd = ShellCommand {
            command: "echo {missing}".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let vars = HashMap::new();
        // Missing vars are left as-is (not replaced with empty string)
        assert_eq!(cmd.render(&vars), "echo {missing}");
    }

    #[test]
    fn test_render_no_vars() {
        let cmd = ShellCommand {
            command: "git status".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let vars = HashMap::new();
        assert_eq!(cmd.render(&vars), "git status");
    }

    #[test]
    fn test_default_timeout() {
        assert_eq!(default_timeout(), 30);
    }

    #[test]
    fn test_shell_command_serde() {
        let json = r#"{"command": "echo hi", "timeout_secs": 10}"#;
        let cmd: ShellCommand = serde_json::from_str(json).unwrap();
        assert_eq!(cmd.command, "echo hi");
        assert_eq!(cmd.timeout_secs, 10);
        assert!(cmd.cwd.is_none());
    }

    #[test]
    fn test_shell_command_serde_default_timeout() {
        let json = r#"{"command": "echo hi"}"#;
        let cmd: ShellCommand = serde_json::from_str(json).unwrap();
        assert_eq!(cmd.timeout_secs, 30);
    }

    #[test]
    fn test_shell_command_serde_with_cwd() {
        let json = r#"{"command": "ls", "timeout_secs": 5, "cwd": "/tmp"}"#;
        let cmd: ShellCommand = serde_json::from_str(json).unwrap();
        assert_eq!(cmd.cwd.as_deref(), Some("/tmp"));
    }

    #[tokio::test]
    async fn test_execute_success() {
        let cmd = ShellCommand {
            command: "echo hello".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let vars = HashMap::new();
        let result = cmd.execute(&vars).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "hello");
    }

    #[tokio::test]
    async fn test_execute_with_substitution() {
        // On Windows, cmd /C doesn't strip single quotes the way sh does,
        // so shell-escaped values appear literally in the output.
        let expected = if cfg!(target_os = "windows") {
            "'world'"
        } else {
            "world"
        };
        let cmd = ShellCommand {
            command: "echo {name}".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("name".to_string(), "world".to_string());
        let result = cmd.execute(&vars).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), expected);
    }

    #[tokio::test]
    async fn test_execute_failure() {
        let cmd = ShellCommand {
            command: "exit 1".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let vars = HashMap::new();
        let result = cmd.execute(&vars).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("exited with code 1"));
    }

    #[tokio::test]
    async fn test_execute_timeout() {
        // Use a long-running command that works on both platforms.
        let cmd = ShellCommand {
            command: (if cfg!(target_os = "windows") {
                "ping -n 100 127.0.0.1 > NUL"
            } else {
                "sleep 100"
            })
            .to_string(),
            timeout_secs: 1,
            cwd: None,
        };
        let vars = HashMap::new();
        let result = cmd.execute(&vars).await;
        assert!(result.is_err());
        assert!(
            result.unwrap_err().contains("timed out"),
            "expected 'timed out' in error"
        );
    }

    #[tokio::test]
    async fn test_execute_env_var_available() {
        let cmd = ShellCommand {
            command: (if cfg!(target_os = "windows") {
                "echo %TH_NAME%"
            } else {
                "echo $TH_NAME"
            })
            .to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("name".to_string(), "from_env".to_string());
        let result = cmd.execute(&vars).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "from_env");
    }

    #[tokio::test]
    async fn test_execute_cwd() {
        let tmp = std::env::temp_dir();
        let cmd = ShellCommand {
            command: (if cfg!(target_os = "windows") {
                "cd"
            } else {
                "pwd"
            })
            .to_string(),
            timeout_secs: 5,
            cwd: Some(tmp.to_string_lossy().to_string()),
        };
        let vars = HashMap::new();
        let result = cmd.execute(&vars).await;
        assert!(result.is_ok());
        let expected = std::fs::canonicalize(&tmp)
            .unwrap_or_else(|_| tmp.clone())
            .to_string_lossy()
            .to_string();
        let actual = result.unwrap();
        if cfg!(target_os = "windows") {
            let actual_canon = std::fs::canonicalize(&actual)
                .or_else(|_| std::path::Path::new(&actual).canonicalize())
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or(actual);
            let expected_canon = std::fs::canonicalize(&expected)
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or(expected);
            assert_eq!(actual_canon.to_lowercase(), expected_canon.to_lowercase());
        } else {
            assert_eq!(actual, expected);
        }
    }

    #[tokio::test]
    async fn test_execute_shell_tool_success() {
        let expected = if cfg!(target_os = "windows") {
            "hello 'world'"
        } else {
            "hello world"
        };
        let cmd = ShellCommand {
            command: "echo hello {name}".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut args = HashMap::new();
        args.insert("name".to_string(), "world".to_string());
        let result = execute_shell_tool(&cmd, &args).await;
        assert_eq!(result, expected);
    }

    #[tokio::test]
    async fn test_execute_shell_tool_error() {
        let cmd = ShellCommand {
            command: "exit 42".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let args = HashMap::new();
        let result = execute_shell_tool(&cmd, &args).await;
        assert!(result.starts_with("Error:"));
    }

    #[tokio::test]
    async fn test_execute_shell_tool_empty_output() {
        // `exit 0` succeeds and prints nothing on both sh and cmd. A POSIX
        // `true` would not be recognised by cmd on Windows and would exit 1,
        // turning the "no output" case into an error case.
        let cmd = ShellCommand {
            command: "exit 0".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let args = HashMap::new();
        let result = execute_shell_tool(&cmd, &args).await;
        assert_eq!(result, "(no output)");
    }

    #[tokio::test]
    async fn test_execute_double_quoted_var_with_special_chars() {
        let cmd = ShellCommand {
            command: "echo \"{text}\"".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("text".to_string(), "He said \"hi\" and $FOO".to_string());
        let result = cmd.execute(&vars).await;
        assert!(result.is_ok());
        if cfg!(target_os = "windows") {
            // cmd /C echoes backslash escapes and $ literally; the exact
            // output is platform-specific, so only check the command succeeds.
        } else {
            assert_eq!(result.unwrap(), "He said \"hi\" and $FOO");
        }
    }

    #[tokio::test]
    async fn test_execute_single_quoted_var_with_single_quote() {
        let cmd = ShellCommand {
            command: "echo '{text}'".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("text".to_string(), "it's here".to_string());
        let result = cmd.execute(&vars).await;
        assert!(result.is_ok());
        if cfg!(target_os = "windows") {
            // cmd /C treats single quotes as literal characters.
        } else {
            assert_eq!(result.unwrap(), "it's here");
        }
    }

    #[tokio::test]
    async fn test_execute_unquoted_var_with_single_quote() {
        let cmd = ShellCommand {
            command: "echo {text}".to_string(),
            timeout_secs: 5,
            cwd: None,
        };
        let mut vars = HashMap::new();
        vars.insert("text".to_string(), "it's here".to_string());
        let result = cmd.execute(&vars).await;
        assert!(result.is_ok());
        if cfg!(target_os = "windows") {
            // cmd /C treats single quotes as literal characters.
        } else {
            assert_eq!(result.unwrap(), "it's here");
        }
    }
}