mielin-wasm
WebAssembly Runtime Integration
Wasmtime-based runtime for executing agent code with capability-based security.
Features
- Wasmtime Integration: Production-grade WebAssembly runtime
- Module Compilation: Ahead-of-time and just-in-time compilation
- Capability Security: Fine-grained permission system
- Memory Snapshots: Capture agent state for migration
- Module Validation: Verify WASM modules before execution
Usage
Add to your Cargo.toml:
[]
= { = "../mielin-wasm" }
Basic Execution
use WasmExecutor;
use Agent;
// Create executor
let executor = new?;
// Create agent with WASM binary
let agent = new;
// Execute agent
let result = executor.execute?;
println!;
Module Validation
use WasmExecutor;
let executor = new?;
// Validate WASM module
match executor.validate
Capability-Based Security
use ;
let mut sandbox = new;
// Grant specific capabilities
sandbox.grant;
sandbox.grant;
// Check permissions
if sandbox.has_capability else
Components
WasmExecutor
Main execution engine:
Sandbox
Capability-based security:
ExecutionResult
Execution outcome:
Examples
Execute Simple Module
use WasmExecutor;
use Agent;
// WAT format (WebAssembly Text)
let wat = r#"
(module
(func (export "_start")
;; Agent code here
)
)
"#;
let wasm = parse_str?;
let agent = new;
let executor = new?;
let result = executor.execute?;
assert_eq!;
Capture Memory Snapshot
use WasmExecutor;
let executor = new?;
// Execute agent
executor.execute?;
// Suspend and capture memory
let memory_snapshot = executor.suspend?;
// Use snapshot for migration
println!;
Custom Configuration
use WasmExecutor;
use Config;
let mut config = new;
config.wasm_multi_memory;
config.wasm_multi_value;
config.consume_fuel; // Enable fuel metering
let executor = with_config?;
Sandbox Example
use ;
// Create restricted sandbox
let mut sandbox = new;
// Grant only network access
sandbox.grant;
// Before allowing operation, check capability
assert!; // Denied
Capabilities
Available Capabilities
| Capability | Description | Use Case |
|---|---|---|
FileSystem |
Read/write files | Data persistence |
Network |
Network I/O | Communication |
Camera |
Camera access | Image capture |
Gpio |
GPIO pins | Hardware control |
Capability Model
The capability model follows the principle of least privilege:
- No capabilities by default: Agents start with zero permissions
- Explicit grants: Each capability must be explicitly granted
- Revocable: Capabilities can be revoked at runtime (future)
- Auditable: Capability checks are logged (future)
Error Handling
Performance
Execution benchmarks:
| Operation | Time | Notes |
|---|---|---|
| Module compilation | ~10-50 ms | Depends on module size |
| Module validation | ~1-5 ms | Fast safety checks |
| Instantiation | ~100 μs | Create instance |
| Simple function call | ~10 ns | Native-like speed |
| Memory snapshot | ~1 μs/KB | Linear in memory size |
Testing
Tests include:
- Executor creation
- Module compilation
- Module validation
- Capability granting and checking
- Execution with various WASM modules
Limitations
Current limitations:
- No async support (future enhancement)
- Limited host function imports
- No WASI support yet
- Sandbox is basic (more capabilities needed)
Advanced Usage
Fuel Metering
Limit agent execution time:
let mut config = new;
config.consume_fuel;
let executor = with_config?;
// Set fuel limit before execution
Multi-Memory Support
Enable multiple linear memories:
let mut config = new;
config.wasm_multi_memory;
let executor = with_config?;
Future Enhancements
- Async/await support for agents
- WASI (WebAssembly System Interface)
- More comprehensive host functions
- Resource limits (CPU, memory)
- Profiling and debugging support
- Hot-reload for agent code
- Shared memory between agents
Security Considerations
- Always validate modules: Use
validate()before execution - Sandbox all agents: Never grant unnecessary capabilities
- Limit resources: Implement fuel metering for production
- Audit capability usage: Log all capability checks
- Isolate agents: Each agent in separate instance
Best Practices
- Compile once, execute many: Cache compiled modules
- Minimize memory: Keep agent state small
- Use snapshots: Capture state only when needed
- Validate early: Check modules before deployment
- Grant minimal capabilities: Follow least privilege
License
MIT OR Apache-2.0