zeptoclaw 0.9.0

Ultra-lightweight personal AI assistant
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
//! Custom CLI-defined tool adapter for ZeptoClaw.
//!
//! Wraps a [`CustomToolDef`] from config and implements the [`Tool`] trait.
//! Commands execute via `sh -c` with shell-escaped parameter interpolation.

use async_trait::async_trait;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::time::Duration;
use tracing::debug;

use crate::config::CustomToolDef;
use crate::error::{Result, ZeptoError};
use crate::security::ShellSecurityConfig;

use super::{Tool, ToolCategory, ToolContext, ToolOutput};

/// Maximum output bytes to capture from custom tool stdout (50KB).
const MAX_OUTPUT_BYTES: usize = 50_000;

/// Minimum timeout in seconds for custom tool execution.
const MIN_TIMEOUT_SECS: u64 = 1;

/// Shell-escape a value by wrapping in single quotes.
/// Embedded single quotes become `'\''`.
fn shell_escape(value: &str) -> String {
    format!("'{}'", value.replace('\'', "'\\''"))
}

/// Interpolate `{{key}}` placeholders in a command template with shell-escaped values.
fn interpolate(template: &str, args: &HashMap<String, String>) -> String {
    let mut result = template.to_string();
    for (key, value) in args {
        let placeholder = format!("{{{{{}}}}}", key);
        result = result.replace(&placeholder, &shell_escape(value));
    }
    result
}

/// A tool defined as a shell command in config.
pub struct CustomTool {
    def: CustomToolDef,
    security: ShellSecurityConfig,
}

impl CustomTool {
    /// Create a new custom tool from a config definition.
    pub fn new(def: CustomToolDef) -> Self {
        Self {
            def,
            security: ShellSecurityConfig::default(),
        }
    }

    /// Create a custom tool with a specific security configuration.
    pub fn with_security(def: CustomToolDef, security: ShellSecurityConfig) -> Self {
        Self { def, security }
    }
}

#[async_trait]
impl Tool for CustomTool {
    fn name(&self) -> &str {
        &self.def.name
    }

    fn description(&self) -> &str {
        &self.def.description
    }

    fn compact_description(&self) -> &str {
        self.description()
    }

    fn category(&self) -> ToolCategory {
        ToolCategory::Shell
    }

    fn parameters(&self) -> Value {
        match &self.def.parameters {
            None => json!({
                "type": "object",
                "properties": {},
                "required": []
            }),
            Some(params) => {
                let mut properties = serde_json::Map::new();
                let mut required = Vec::new();
                for (name, type_str) in params {
                    properties.insert(name.clone(), json!({"type": type_str}));
                    required.push(json!(name));
                }
                json!({
                    "type": "object",
                    "properties": properties,
                    "required": required
                })
            }
        }
    }

    async fn execute(&self, args: Value, ctx: &ToolContext) -> Result<ToolOutput> {
        // Extract string args from JSON for interpolation
        let string_args: HashMap<String, String> = if let Some(obj) = args.as_object() {
            obj.iter()
                .map(|(k, v)| {
                    let val = match v {
                        Value::String(s) => s.clone(),
                        other => other.to_string(),
                    };
                    (k.clone(), val)
                })
                .collect()
        } else {
            HashMap::new()
        };

        // Interpolate command template
        let command = interpolate(&self.def.command, &string_args);

        // Validate against shell security blocklist (cached config, no regex recompilation)
        if let Err(e) = self.security.validate_command(&command) {
            return Err(ZeptoError::Tool(format!(
                "Command blocked by security policy: {}",
                e
            )));
        }

        debug!(tool = %self.def.name, command = %command, "Executing custom tool");

        // Determine timeout (clamp to minimum to prevent zero-duration timeouts)
        let timeout_secs = self.def.timeout_secs.unwrap_or(30).max(MIN_TIMEOUT_SECS);
        let timeout = Duration::from_secs(timeout_secs);

        // Build command
        let mut cmd = tokio::process::Command::new("sh");
        cmd.arg("-c").arg(&command);
        cmd.stdout(std::process::Stdio::piped());
        cmd.stderr(std::process::Stdio::piped());

        // Working directory: custom def > workspace > current dir
        if let Some(ref dir) = self.def.working_dir {
            cmd.current_dir(dir);
        } else if let Some(ref ws) = ctx.workspace {
            cmd.current_dir(ws);
        }

        // Environment variables
        if let Some(ref env_vars) = self.def.env {
            for (k, v) in env_vars {
                cmd.env(k, v);
            }
        }

        // Execute with timeout
        let output = match tokio::time::timeout(timeout, cmd.output()).await {
            Ok(Ok(output)) => output,
            Ok(Err(e)) => {
                return Err(ZeptoError::Tool(format!(
                    "Failed to execute command: {}",
                    e
                )));
            }
            Err(_) => {
                return Err(ZeptoError::Tool(format!(
                    "Command timed out after {}s",
                    timeout_secs
                )));
            }
        };

        if output.status.success() {
            let mut stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
            // Truncate oversized output to prevent blowing up the LLM context
            if stdout.len() > MAX_OUTPUT_BYTES {
                let mut end = MAX_OUTPUT_BYTES;
                while !stdout.is_char_boundary(end) {
                    end -= 1;
                }
                stdout.truncate(end);
                stdout.push_str("\n... (output truncated)");
            }
            Ok(ToolOutput::llm_only(if stdout.is_empty() {
                "(no output)".to_string()
            } else {
                stdout
            }))
        } else {
            let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
            let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
            Err(ZeptoError::Tool(format!(
                "Command failed (exit {}): {}",
                output.status.code().unwrap_or(-1),
                if stderr.is_empty() { &stdout } else { &stderr }
            )))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tools::types::ToolContext;

    fn test_ctx() -> ToolContext {
        ToolContext {
            workspace: Some(std::env::temp_dir().to_string_lossy().to_string()),
            channel: None,
            chat_id: None,
            is_batch: false,
        }
    }

    fn simple_def(name: &str, command: &str) -> CustomToolDef {
        CustomToolDef {
            name: name.to_string(),
            description: format!("Test tool {}", name),
            command: command.to_string(),
            parameters: None,
            working_dir: None,
            timeout_secs: None,
            env: None,
        }
    }

    // === Unit tests ===

    #[test]
    fn test_shell_escape_basic() {
        assert_eq!(shell_escape("hello"), "'hello'");
        assert_eq!(shell_escape("hello world"), "'hello world'");
    }

    #[test]
    fn test_shell_escape_injection() {
        let escaped = shell_escape("'; rm -rf / #");
        assert_eq!(escaped, "''\\''; rm -rf / #'");
        // The result when passed to sh -c is treated as a literal string
    }

    #[test]
    fn test_interpolate_basic() {
        let mut args = HashMap::new();
        args.insert("name".to_string(), "world".to_string());
        let result = interpolate("echo {{name}}", &args);
        assert_eq!(result, "echo 'world'");
    }

    #[test]
    fn test_interpolate_missing_param() {
        let args = HashMap::new();
        let result = interpolate("echo {{name}}", &args);
        // Missing params are left as-is
        assert_eq!(result, "echo {{name}}");
    }

    #[test]
    fn test_tool_name() {
        let tool = CustomTool::new(simple_def("cpu_temp", "echo 42"));
        assert_eq!(tool.name(), "cpu_temp");
    }

    #[test]
    fn test_tool_description() {
        let tool = CustomTool::new(simple_def("cpu_temp", "echo 42"));
        assert_eq!(tool.description(), "Test tool cpu_temp");
    }

    #[test]
    fn test_parameters_no_params() {
        let tool = CustomTool::new(simple_def("test", "echo"));
        let params = tool.parameters();
        assert_eq!(params["type"], "object");
        assert!(params["properties"].as_object().unwrap().is_empty());
    }

    #[test]
    fn test_parameters_with_params() {
        let mut def = simple_def("test", "echo");
        let mut params = HashMap::new();
        params.insert("pattern".to_string(), "string".to_string());
        params.insert("limit".to_string(), "integer".to_string());
        def.parameters = Some(params);
        let tool = CustomTool::new(def);
        let schema = tool.parameters();
        let props = schema["properties"].as_object().unwrap();
        assert_eq!(props.len(), 2);
        assert_eq!(props["pattern"]["type"], "string");
        assert_eq!(props["limit"]["type"], "integer");
    }

    #[test]
    fn test_custom_tool_with_security_config() {
        use crate::security::{ShellAllowlistMode, ShellSecurityConfig};

        let def = CustomToolDef {
            name: "my_tool".to_string(),
            description: "test tool".to_string(),
            command: "echo hello".to_string(),
            parameters: None,
            timeout_secs: None,
            working_dir: None,
            env: None,
        };

        let security =
            ShellSecurityConfig::new().with_allowlist(vec!["git"], ShellAllowlistMode::Strict);
        let tool = CustomTool::with_security(def, security);
        assert_eq!(tool.name(), "my_tool");
        // Verify the injected security config actually restricts commands
        assert!(tool.security.validate_command("git status").is_ok());
        assert!(tool.security.validate_command("echo hello").is_err());
    }

    #[test]
    fn test_security_config_cached() {
        let tool = CustomTool::new(simple_def("test", "echo hi"));
        // Verify security config is constructed once and stored
        assert!(tool.security.validate_command("echo hello").is_ok());
    }

    #[test]
    fn test_min_timeout_clamped() {
        let mut def = simple_def("test", "echo");
        def.timeout_secs = Some(0);
        let tool = CustomTool::new(def);
        // timeout_secs of 0 should be clamped to MIN_TIMEOUT_SECS
        assert_eq!(
            tool.def.timeout_secs.unwrap_or(30).max(MIN_TIMEOUT_SECS),
            MIN_TIMEOUT_SECS
        );
    }

    // === Async execution tests ===

    #[tokio::test]
    async fn test_execute_simple_command() {
        let tool = CustomTool::new(simple_def("test", "echo hello"));
        let result = tool.execute(json!({}), &test_ctx()).await.unwrap().for_llm;
        assert_eq!(result, "hello");
    }

    #[tokio::test]
    async fn test_execute_with_interpolation() {
        let mut def = simple_def("test", "echo {{msg}}");
        let mut params = HashMap::new();
        params.insert("msg".to_string(), "string".to_string());
        def.parameters = Some(params);
        let tool = CustomTool::new(def);
        let result = tool
            .execute(json!({"msg": "hello world"}), &test_ctx())
            .await
            .unwrap()
            .for_llm;
        assert_eq!(result, "hello world");
    }

    #[tokio::test]
    async fn test_execute_blocks_injection() {
        let mut def = simple_def("test", "echo {{msg}}");
        let mut params = HashMap::new();
        params.insert("msg".to_string(), "string".to_string());
        def.parameters = Some(params);
        let tool = CustomTool::new(def);
        // Injection attempt: $(whoami) should be treated as literal
        let result = tool
            .execute(json!({"msg": "$(whoami)"}), &test_ctx())
            .await
            .unwrap()
            .for_llm;
        assert_eq!(result, "$(whoami)");
    }

    #[tokio::test]
    async fn test_execute_failure() {
        let tool = CustomTool::new(simple_def("test", "exit 1"));
        let result = tool.execute(json!({}), &test_ctx()).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("failed") || err.contains("exit 1"),
            "Got: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_execute_timeout() {
        let mut def = simple_def("test", "sleep 10");
        def.timeout_secs = Some(1);
        let tool = CustomTool::new(def);
        let result = tool.execute(json!({}), &test_ctx()).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("timed out"));
    }

    #[tokio::test]
    async fn test_execute_with_env() {
        let mut def = simple_def("test", "echo $TEST_VAR_CUSTOM");
        let mut env = HashMap::new();
        env.insert("TEST_VAR_CUSTOM".to_string(), "custom_value".to_string());
        def.env = Some(env);
        let tool = CustomTool::new(def);
        let result = tool.execute(json!({}), &test_ctx()).await.unwrap().for_llm;
        assert_eq!(result, "custom_value");
    }

    #[tokio::test]
    async fn test_execute_with_working_dir() {
        let tool = CustomTool::new(CustomToolDef {
            name: "test".to_string(),
            description: "test".to_string(),
            command: "pwd".to_string(),
            parameters: None,
            working_dir: Some("/tmp".to_string()),
            timeout_secs: None,
            env: None,
        });
        let result = tool.execute(json!({}), &test_ctx()).await.unwrap().for_llm;
        // On macOS /tmp is a symlink to /private/tmp
        assert!(result.contains("tmp"), "Got: {}", result);
    }

    #[tokio::test]
    async fn test_execute_empty_stdout() {
        let tool = CustomTool::new(simple_def("test", "true"));
        let result = tool.execute(json!({}), &test_ctx()).await.unwrap().for_llm;
        assert_eq!(result, "(no output)");
    }

    #[tokio::test]
    async fn test_execute_shell_blocklist() {
        // The ShellSecurityConfig blocks dangerous patterns like `rm -rf /`
        let tool = CustomTool::new(simple_def("test", "rm -rf /"));
        let result = tool.execute(json!({}), &test_ctx()).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("blocked") || err.contains("security"),
            "Got: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_execute_no_params_ignores_args() {
        let tool = CustomTool::new(simple_def("test", "echo fixed"));
        // Extra args should be ignored for no-param tools
        let result = tool
            .execute(json!({"extra": "stuff"}), &test_ctx())
            .await
            .unwrap()
            .for_llm;
        assert_eq!(result, "fixed");
    }

    #[tokio::test]
    async fn test_execute_output_truncated() {
        // Generate output larger than MAX_OUTPUT_BYTES
        let repeat = MAX_OUTPUT_BYTES + 1000;
        let cmd = format!("printf '%0.s-' $(seq 1 {})", repeat);
        let tool = CustomTool::new(simple_def("test", &cmd));
        let result = tool.execute(json!({}), &test_ctx()).await.unwrap().for_llm;
        assert!(result.contains("(output truncated)"));
        // Output should be capped near MAX_OUTPUT_BYTES
        assert!(result.len() <= MAX_OUTPUT_BYTES + 100);
    }
}