vex-llm 0.1.5

LLM provider integrations for VEX
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
//! Tool Executor with Merkle audit integration
//!
//! This module provides `ToolExecutor` which wraps tool execution with:
//! - Timeout protection (DoS prevention)
//! - Input validation
//! - Cryptographic result hashing
//! - Merkle audit trail integration
//!
//! # VEX Innovation
//!
//! Every tool execution is automatically logged to the audit chain with:
//! - Tool name and argument hash (not raw args for privacy)
//! - Result hash for verification
//! - Execution time metrics
//!
//! This enables cryptographic proof of what tools were used.
//!
//! # Security Considerations
//!
//! - All executions have configurable timeouts
//! - Validation runs before execution
//! - Audit logging is non-fatal (doesn't break execution)
//! - Arguments are hashed before logging (privacy protection)

use std::time::Instant;
use tokio::time::timeout;
use tracing::{debug, error, info, warn};

use crate::tool::{Capability, ToolRegistry};
use crate::tool_error::ToolError;
use crate::tool_result::ToolResult;

/// Tool executor with automatic audit logging and timeout protection.
///
/// The executor provides a safe, audited interface to execute tools:
/// 1. Validates arguments against tool's schema
/// 2. Executes with timeout protection
/// 3. Hashes results for Merkle chain
/// 4. Logs execution to audit trail (if configured)
///
/// # Example
///
/// ```ignore
/// use vex_llm::{ToolExecutor, ToolRegistry};
///
/// let registry = ToolRegistry::with_builtins();
/// let executor = ToolExecutor::new(registry);
///
/// let result = executor
///     .execute("calculator", json!({"expression": "2+2"}))
///     .await?;
///
/// println!("Result: {}", result.output);
/// println!("Hash: {}", result.hash);
/// ```
pub struct ToolExecutor {
    registry: ToolRegistry,
    /// Enable/disable audit logging
    audit_enabled: bool,
    /// Maximum parallel executions (0 = unlimited)
    max_parallel: usize,
    /// Allowed capabilities for this executor (Security Sandbox)
    allowed_capabilities: Vec<Capability>,
}

impl ToolExecutor {
    /// Create a new executor with the given registry
    pub fn new(registry: ToolRegistry) -> Self {
        Self {
            registry,
            audit_enabled: true,
            max_parallel: 0, // Unlimited by default
            allowed_capabilities: vec![
                Capability::PureComputation,
                Capability::Network,
                Capability::FileSystem,
                Capability::Subprocess,
                Capability::Environment,
                Capability::Cryptography,
            ],
        }
    }

    /// Create executor with audit logging disabled
    pub fn without_audit(registry: ToolRegistry) -> Self {
        Self {
            registry,
            audit_enabled: false,
            max_parallel: 0,
            allowed_capabilities: vec![
                Capability::PureComputation,
                Capability::Network,
                Capability::FileSystem,
                Capability::Subprocess,
                Capability::Environment,
                Capability::Cryptography,
            ],
        }
    }

    /// Set maximum parallel executions
    pub fn with_max_parallel(mut self, max: usize) -> Self {
        self.max_parallel = max;
        self
    }

    /// Set allowed capabilities for the sandbox
    pub fn with_allowed_capabilities(mut self, caps: Vec<Capability>) -> Self {
        self.allowed_capabilities = caps;
        self
    }

    /// Execute a tool by name with given arguments.
    ///
    /// # Arguments
    ///
    /// * `tool_name` - Name of the tool to execute
    /// * `args` - JSON arguments to pass to the tool
    ///
    /// # Returns
    ///
    /// * `Ok(ToolResult)` - Execution result with hash for verification
    /// * `Err(ToolError)` - If tool not found, validation failed, execution error, or timeout
    ///
    /// # Security
    ///
    /// - Tool lookup prevents arbitrary code execution
    /// - Timeout prevents DoS from hanging tools
    /// - Result hash enables tamper detection
    pub async fn execute(
        &self,
        tool_name: &str,
        args: serde_json::Value,
    ) -> Result<ToolResult, ToolError> {
        // 1. Get tool from registry
        let tool = self.registry.get(tool_name).ok_or_else(|| {
            warn!(tool = tool_name, "Tool not found");
            ToolError::not_found(tool_name)
        })?;

        // 1.5. Check capabilities (Sandbox)
        for cap in tool.capabilities() {
            if !self.allowed_capabilities.contains(&cap) {
                warn!(
                    tool = tool_name,
                    capability = ?cap,
                    "Tool requires missing capability"
                );
                return Err(ToolError::unavailable(
                    tool_name,
                    format!("Sandbox violation: tool requires {:?}", cap),
                ));
            }
        }

        // 2. Check availability
        if !tool.is_available() {
            warn!(tool = tool_name, "Tool is unavailable");
            return Err(ToolError::unavailable(
                tool_name,
                "Tool is currently disabled",
            ));
        }

        // 3. Validate arguments against JSON Schema
        debug!(tool = tool_name, "Validating arguments against schema");
        let schema_str = tool.definition().parameters;
        if !schema_str.is_empty() && schema_str != "{}" {
            let schema_json: serde_json::Value = serde_json::from_str(schema_str).map_err(|e| {
                ToolError::execution_failed(tool_name, format!("Invalid tool schema: {}", e))
            })?;

            let compiled = jsonschema::JSONSchema::compile(&schema_json).map_err(|e| {
                ToolError::execution_failed(
                    tool_name,
                    format!("Failed to compile tool schema: {}", e),
                )
            })?;

            if !compiled.is_valid(&args) {
                warn!(tool = tool_name, "Schema validation failed");
                return Err(ToolError::invalid_args(
                    tool_name,
                    "Arguments do not match tool schema",
                ));
            }
        }

        // 4. Custom validation
        debug!(tool = tool_name, "Running custom validation");
        tool.validate(&args)?;

        // 5. Execute with timeout
        let tool_timeout = tool.timeout();
        let start = Instant::now();

        debug!(
            tool = tool_name,
            timeout_ms = tool_timeout.as_millis(),
            "Executing tool"
        );

        let output = timeout(tool_timeout, tool.execute(args.clone()))
            .await
            .map_err(|_| {
                error!(
                    tool = tool_name,
                    timeout_ms = tool_timeout.as_millis(),
                    "Tool execution timed out"
                );
                ToolError::timeout(tool_name, tool_timeout.as_millis() as u64)
            })??;

        let elapsed = start.elapsed();

        // 5. Create result with cryptographic hash
        let result = ToolResult::new(tool_name, &args, output, elapsed);

        // 6. Log execution metrics
        info!(
            tool = tool_name,
            execution_ms = elapsed.as_millis(),
            hash = %result.hash,
            "Tool executed successfully"
        );

        // 7. Audit logging would happen here (integration point)
        // Note: We log to tracing; actual AuditStore integration is in the runtime
        if self.audit_enabled {
            debug!(
                tool = tool_name,
                result_hash = %result.hash,
                "Audit entry created"
            );
        }

        Ok(result)
    }

    /// Execute multiple tools in parallel.
    ///
    /// # Arguments
    ///
    /// * `calls` - Vector of (tool_name, args) pairs
    ///
    /// # Returns
    ///
    /// Vector of results in the same order as input.
    /// Each result is independent (one failure doesn't affect others).
    ///
    /// # Security
    ///
    /// - Respects max_parallel limit to prevent resource exhaustion
    /// - Each tool has its own timeout
    pub async fn execute_parallel(
        &self,
        calls: Vec<(String, serde_json::Value)>,
    ) -> Vec<Result<ToolResult, ToolError>> {
        debug!(count = calls.len(), "Executing tools in parallel");

        if self.max_parallel > 0 {
            use futures::stream::{self, StreamExt};

            stream::iter(calls)
                .map(|(name, args)| async move { self.execute(&name, args).await })
                .buffered(self.max_parallel)
                .collect()
                .await
        } else {
            let futures: Vec<_> = calls
                .into_iter()
                .map(|(name, args)| {
                    // Create an owned future that doesn't borrow the iterator
                    async move { self.execute(&name, args).await }
                })
                .collect();

            futures::future::join_all(futures).await
        }
    }

    /// Get a reference to the tool registry
    pub fn registry(&self) -> &ToolRegistry {
        &self.registry
    }

    /// Get a mutable reference to the tool registry
    pub fn registry_mut(&mut self) -> &mut ToolRegistry {
        &mut self.registry
    }

    /// Check if a tool exists
    pub fn has_tool(&self, name: &str) -> bool {
        self.registry.contains(name)
    }

    /// List all available tool names
    pub fn tool_names(&self) -> Vec<&str> {
        self.registry.names()
    }
}

impl std::fmt::Debug for ToolExecutor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ToolExecutor")
            .field("tools", &self.registry.names())
            .field("audit_enabled", &self.audit_enabled)
            .field("max_parallel", &self.max_parallel)
            .finish()
    }
}

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

    // Test tool that returns arguments
    struct EchoTool {
        definition: ToolDefinition,
    }

    impl EchoTool {
        fn new() -> Self {
            Self {
                definition: ToolDefinition::new(
                    "echo",
                    "Echo back the input",
                    r#"{"type": "object"}"#,
                ),
            }
        }
    }

    #[async_trait]
    impl Tool for EchoTool {
        fn definition(&self) -> &ToolDefinition {
            &self.definition
        }

        async fn execute(&self, args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
            Ok(serde_json::json!({ "echo": args }))
        }
    }

    // Test tool that always fails
    struct FailTool {
        definition: ToolDefinition,
    }

    impl FailTool {
        fn new() -> Self {
            Self {
                definition: ToolDefinition::new("fail", "Always fails", r#"{"type": "object"}"#),
            }
        }
    }

    #[async_trait]
    impl Tool for FailTool {
        fn definition(&self) -> &ToolDefinition {
            &self.definition
        }

        async fn execute(&self, _args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
            Err(ToolError::execution_failed("fail", "Intentional failure"))
        }
    }

    // Test tool that times out
    struct SlowTool {
        definition: ToolDefinition,
    }

    impl SlowTool {
        fn new() -> Self {
            Self {
                definition: ToolDefinition::new("slow", "Takes forever", r#"{"type": "object"}"#),
            }
        }
    }

    #[async_trait]
    impl Tool for SlowTool {
        fn definition(&self) -> &ToolDefinition {
            &self.definition
        }

        fn timeout(&self) -> Duration {
            Duration::from_millis(50) // Very short timeout for testing
        }

        async fn execute(&self, _args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
            tokio::time::sleep(Duration::from_secs(10)).await;
            Ok(serde_json::json!({"done": true}))
        }
    }

    #[tokio::test]
    async fn test_execute_success() {
        let mut registry = ToolRegistry::new();
        registry.register(Arc::new(EchoTool::new()));

        let executor = ToolExecutor::new(registry);
        let result = executor
            .execute("echo", serde_json::json!({"message": "hello"}))
            .await
            .unwrap();

        assert_eq!(result.tool_name, "echo");
        assert!(result.output["echo"]["message"] == "hello");
        assert!(!result.hash.to_string().is_empty());
    }

    #[tokio::test]
    async fn test_execute_not_found() {
        let registry = ToolRegistry::new();
        let executor = ToolExecutor::new(registry);

        let result = executor.execute("nonexistent", serde_json::json!({})).await;

        assert!(matches!(result, Err(ToolError::NotFound { .. })));
    }

    #[tokio::test]
    async fn test_execute_failure() {
        let mut registry = ToolRegistry::new();
        registry.register(Arc::new(FailTool::new()));

        let executor = ToolExecutor::new(registry);
        let result = executor.execute("fail", serde_json::json!({})).await;

        assert!(matches!(result, Err(ToolError::ExecutionFailed { .. })));
    }

    #[tokio::test]
    async fn test_execute_timeout() {
        let mut registry = ToolRegistry::new();
        registry.register(Arc::new(SlowTool::new()));

        let executor = ToolExecutor::new(registry);
        let result = executor.execute("slow", serde_json::json!({})).await;

        assert!(matches!(result, Err(ToolError::Timeout { .. })));
    }

    #[tokio::test]
    async fn test_execute_parallel() {
        let mut registry = ToolRegistry::new();
        registry.register(Arc::new(EchoTool::new()));

        let executor = ToolExecutor::new(registry);

        let calls = vec![
            ("echo".to_string(), serde_json::json!({"n": 1})),
            ("echo".to_string(), serde_json::json!({"n": 2})),
            ("echo".to_string(), serde_json::json!({"n": 3})),
        ];

        let results = executor.execute_parallel(calls).await;

        assert_eq!(results.len(), 3);
        assert!(results.iter().all(|r| r.is_ok()));
    }

    #[tokio::test]
    async fn test_has_tool() {
        let mut registry = ToolRegistry::new();
        registry.register(Arc::new(EchoTool::new()));

        let executor = ToolExecutor::new(registry);

        assert!(executor.has_tool("echo"));
        assert!(!executor.has_tool("nonexistent"));
    }
}