ironclaw 0.22.0

Secure personal AI assistant that protects your data and expands its capabilities on the fly
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
//! Shared tool execution pipeline.
//!
//! Provides a single implementation of the validate → timeout → execute → serialize
//! pipeline used by all agentic loop consumers (chat, job, container) and the
//! scheduler's subtask execution.

use crate::context::JobContext;
use crate::error::Error;
use crate::llm::ChatMessage;
use crate::safety::SafetyLayer;
use crate::tools::{ToolRegistry, prepare_tool_params, redact_params};

/// Execute a tool with safety checks: lookup → validate → timeout → execute → serialize.
///
/// This is the single canonical implementation of tool execution. All consumers
/// (chat dispatcher, job worker, container runtime, scheduler subtasks) use this
/// function instead of maintaining their own copies.
pub async fn execute_tool_with_safety(
    tools: &ToolRegistry,
    safety: &SafetyLayer,
    tool_name: &str,
    params: serde_json::Value,
    job_ctx: &JobContext,
) -> Result<String, Error> {
    if tool_name.is_empty() {
        return Err(crate::error::ToolError::NotFound {
            name: tool_name.to_string(),
        }
        .into());
    }
    let tool = tools
        .get(tool_name)
        .await
        .ok_or_else(|| crate::error::ToolError::NotFound {
            name: tool_name.to_string(),
        })?;

    let normalized_params = prepare_tool_params(tool.as_ref(), &params);

    // Validate tool parameters
    let validation = safety.validator().validate_tool_params(&normalized_params);
    if !validation.is_valid {
        let details = validation
            .errors
            .iter()
            .map(|e| format!("{}: {}", e.field, e.message))
            .collect::<Vec<_>>()
            .join("; ");
        return Err(crate::error::ToolError::InvalidParameters {
            name: tool_name.to_string(),
            reason: format!("Invalid tool parameters: {}", details),
        }
        .into());
    }

    let safe_params = redact_params(&normalized_params, tool.sensitive_params());
    tracing::debug!(
        tool = %tool_name,
        params = %safe_params,
        "Tool call started"
    );

    // Execute with per-tool timeout
    let timeout = tool.execution_timeout();
    let start = std::time::Instant::now();
    let result = tokio::time::timeout(timeout, tool.execute(normalized_params, job_ctx)).await;
    let elapsed = start.elapsed();

    match &result {
        Ok(Ok(output)) => {
            let result_size = serde_json::to_string(&output.result)
                .map(|s| s.len())
                .unwrap_or(0);
            tracing::debug!(
                tool = %tool_name,
                elapsed_ms = elapsed.as_millis() as u64,
                result_size_bytes = result_size,
                "Tool call succeeded"
            );
        }
        Ok(Err(e)) => {
            tracing::debug!(
                tool = %tool_name,
                elapsed_ms = elapsed.as_millis() as u64,
                error = %e,
                "Tool call failed"
            );
        }
        Err(_) => {
            tracing::debug!(
                tool = %tool_name,
                elapsed_ms = elapsed.as_millis() as u64,
                timeout_secs = timeout.as_secs(),
                "Tool call timed out"
            );
        }
    }

    let result = result
        .map_err(|_| crate::error::ToolError::Timeout {
            name: tool_name.to_string(),
            timeout,
        })?
        .map_err(|e| crate::error::ToolError::ExecutionFailed {
            name: tool_name.to_string(),
            reason: e.to_string(),
        })?;

    serde_json::to_string_pretty(&result.result).map_err(|e| {
        crate::error::ToolError::ExecutionFailed {
            name: tool_name.to_string(),
            reason: format!("Failed to serialize result: {}", e),
        }
        .into()
    })
}

/// Process a tool result into a `ChatMessage::tool_result` with safety sanitization.
///
/// On success: sanitize → wrap → ChatMessage::tool_result.
/// On error: format error → ChatMessage::tool_result.
///
/// Returns the content string and the ChatMessage.
pub fn process_tool_result(
    safety: &SafetyLayer,
    tool_name: &str,
    tool_call_id: &str,
    result: &Result<String, impl std::fmt::Display>,
) -> (String, ChatMessage) {
    let content = match result {
        Ok(output) => {
            let sanitized = safety.sanitize_tool_output(tool_name, output);
            safety.wrap_for_llm(tool_name, &sanitized.content)
        }
        Err(e) => format!("Error: {}", e),
    };
    let message = ChatMessage::tool_result(tool_call_id, tool_name, content.clone());
    (content, message)
}

/// Execute a tool with safety checks, returning a string error (for container runtime).
///
/// This is a thin wrapper around `execute_tool_with_safety` that converts
/// `Error` to `String` for the container runtime's simpler error model.
pub async fn execute_tool_simple(
    tools: &ToolRegistry,
    safety: &SafetyLayer,
    tool_name: &str,
    params: serde_json::Value,
    job_ctx: &JobContext,
) -> Result<String, String> {
    execute_tool_with_safety(tools, safety, tool_name, params, job_ctx)
        .await
        .map_err(|e| e.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tools::tool::{Tool, ToolError, ToolOutput};
    use std::sync::Arc;
    use std::time::Duration;

    struct EchoTool;

    #[async_trait::async_trait]
    impl Tool for EchoTool {
        fn name(&self) -> &str {
            "echo"
        }
        fn description(&self) -> &str {
            "Echoes input"
        }
        fn parameters_schema(&self) -> serde_json::Value {
            serde_json::json!({"type": "object", "properties": {}})
        }
        async fn execute(
            &self,
            params: serde_json::Value,
            _ctx: &JobContext,
        ) -> Result<ToolOutput, ToolError> {
            Ok(ToolOutput::success(params, Duration::default()))
        }
        fn requires_sanitization(&self) -> bool {
            false
        }
    }

    struct FailTool;

    #[async_trait::async_trait]
    impl Tool for FailTool {
        fn name(&self) -> &str {
            "fail_tool"
        }
        fn description(&self) -> &str {
            "Always fails"
        }
        fn parameters_schema(&self) -> serde_json::Value {
            serde_json::json!({"type": "object", "properties": {}})
        }
        async fn execute(
            &self,
            _: serde_json::Value,
            _: &JobContext,
        ) -> Result<ToolOutput, ToolError> {
            Err(ToolError::ExecutionFailed(
                "intentional failure".to_string(),
            ))
        }
        fn requires_sanitization(&self) -> bool {
            false
        }
    }

    struct SlowTool;

    #[async_trait::async_trait]
    impl Tool for SlowTool {
        fn name(&self) -> &str {
            "slow_tool"
        }
        fn description(&self) -> &str {
            "Sleeps forever"
        }
        fn parameters_schema(&self) -> serde_json::Value {
            serde_json::json!({"type": "object", "properties": {}})
        }
        async fn execute(
            &self,
            _: serde_json::Value,
            _: &JobContext,
        ) -> Result<ToolOutput, ToolError> {
            tokio::time::sleep(Duration::from_secs(60)).await;
            unreachable!()
        }
        fn execution_timeout(&self) -> Duration {
            Duration::from_millis(50)
        }
        fn requires_sanitization(&self) -> bool {
            false
        }
    }

    struct ArrayEchoTool;

    #[async_trait::async_trait]
    impl Tool for ArrayEchoTool {
        fn name(&self) -> &str {
            "array_echo"
        }
        fn description(&self) -> &str {
            "Echoes normalized params"
        }
        fn parameters_schema(&self) -> serde_json::Value {
            serde_json::json!({
                "type": "object",
                "properties": {
                    "values": {
                        "type": "array",
                        "items": { "type": "integer" }
                    }
                }
            })
        }
        async fn execute(
            &self,
            params: serde_json::Value,
            _ctx: &JobContext,
        ) -> Result<ToolOutput, ToolError> {
            Ok(ToolOutput::success(params, Duration::default()))
        }
        fn requires_sanitization(&self) -> bool {
            false
        }
    }

    fn test_safety() -> SafetyLayer {
        SafetyLayer::new(&crate::config::SafetyConfig {
            max_output_length: 100_000,
            injection_check_enabled: false,
        })
    }

    fn test_job_ctx() -> JobContext {
        JobContext::default()
    }

    async fn registry_with(tools: Vec<Arc<dyn Tool>>) -> ToolRegistry {
        let registry = ToolRegistry::new();
        for tool in tools {
            registry.register(tool).await;
        }
        registry
    }

    #[tokio::test]
    async fn test_execute_empty_tool_name_returns_not_found() {
        // Regression: execute_tool_with_safety must reject empty tool names
        // gracefully via ToolError::NotFound (not a panic).
        let registry = registry_with(vec![]).await;
        let safety = test_safety();

        let result = execute_tool_with_safety(
            &registry,
            &safety,
            "",
            serde_json::json!({}),
            &test_job_ctx(),
        )
        .await;

        assert!(
            matches!(
                result,
                Err(crate::error::Error::Tool(
                    crate::error::ToolError::NotFound { .. }
                ))
            ),
            "Empty tool name should return ToolError::NotFound, got: {result:?}"
        );
    }

    #[tokio::test]
    async fn test_execute_success() {
        let registry = registry_with(vec![Arc::new(EchoTool)]).await;
        let safety = test_safety();
        let params = serde_json::json!({"message": "hello"});

        let result =
            execute_tool_with_safety(&registry, &safety, "echo", params, &test_job_ctx()).await;

        assert!(result.is_ok(), "Echo tool should succeed");
        let output = result.unwrap();
        assert!(
            output.contains("hello"),
            "Output should contain the echoed input"
        );
    }

    #[tokio::test]
    async fn test_execute_missing_tool() {
        let registry = registry_with(vec![]).await;
        let safety = test_safety();

        let result = execute_tool_with_safety(
            &registry,
            &safety,
            "nonexistent",
            serde_json::json!({}),
            &test_job_ctx(),
        )
        .await;

        assert!(result.is_err(), "Missing tool should return error");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("nonexistent") || err.contains("not found"),
            "Error should mention the tool: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_execute_tool_failure() {
        let registry = registry_with(vec![Arc::new(FailTool)]).await;
        let safety = test_safety();

        let result = execute_tool_with_safety(
            &registry,
            &safety,
            "fail_tool",
            serde_json::json!({}),
            &test_job_ctx(),
        )
        .await;

        assert!(result.is_err(), "FailTool should return error");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("intentional failure"),
            "Error should contain the failure reason: {}",
            err
        );
    }

    #[tokio::test]
    async fn test_execute_tool_timeout() {
        let registry = registry_with(vec![Arc::new(SlowTool)]).await;
        let safety = test_safety();

        let start = std::time::Instant::now();
        let result = execute_tool_with_safety(
            &registry,
            &safety,
            "slow_tool",
            serde_json::json!({}),
            &test_job_ctx(),
        )
        .await;
        let elapsed = start.elapsed();

        assert!(result.is_err(), "SlowTool should timeout");
        let err = result.unwrap_err().to_string();
        assert!(
            err.to_lowercase().contains("timeout") || err.to_lowercase().contains("timed out"),
            "Error should mention timeout: {}",
            err
        );
        assert!(
            elapsed < Duration::from_secs(1),
            "Should timeout quickly, not wait 60s"
        );
    }

    #[tokio::test]
    async fn test_execute_normalizes_stringified_array_params() {
        let registry = registry_with(vec![Arc::new(ArrayEchoTool)]).await;
        let safety = test_safety();

        let result = execute_tool_with_safety(
            &registry,
            &safety,
            "array_echo",
            serde_json::json!({"values": "[\"1\", \"2\", 3]"}),
            &test_job_ctx(),
        )
        .await
        .expect("array_echo should succeed"); // safety: test-only assertion

        let output: serde_json::Value =
            serde_json::from_str(&result).expect("tool result should be valid JSON"); // safety: test-only assertion
        assert_eq!(output["values"], serde_json::json!([1, 2, 3])); // safety: test-only assertion
    }

    #[test]
    fn test_process_tool_result_success() {
        let safety = test_safety();
        let result: Result<String, String> = Ok("tool output data".to_string());

        let (content, message) = process_tool_result(&safety, "echo", "call_1", &result);

        assert!(
            content.contains("tool_output"),
            "Content should be XML-wrapped: {}",
            content
        );
        assert!(
            content.contains("tool output data"),
            "Content should contain the output: {}",
            content
        );
        assert_eq!(message.role, crate::llm::Role::Tool);
        assert_eq!(message.name.as_deref(), Some("echo"));
    }

    #[test]
    fn test_process_tool_result_error() {
        let safety = test_safety();
        let result: Result<String, String> = Err("something went wrong".to_string());

        let (content, message) = process_tool_result(&safety, "echo", "call_1", &result);

        assert!(
            content.contains("Error:"),
            "Error content should start with 'Error:': {}",
            content
        );
        assert!(
            content.contains("something went wrong"),
            "Error content should contain the message: {}",
            content
        );
        assert_eq!(message.role, crate::llm::Role::Tool);
    }
}