| Crates.io | mielin-wasm |
| lib.rs | mielin-wasm |
| version | 0.1.0-rc.1 |
| created_at | 2026-01-18 02:31:18.36794+00 |
| updated_at | 2026-01-18 02:31:18.36794+00 |
| description | WebAssembly sandboxing and execution runtime for agent cells using Wasmtime |
| homepage | |
| repository | https://github.com/cool-japan/mielin |
| max_upload_size | |
| id | 2051626 |
| size | 861,546 |
WebAssembly Runtime Integration
Wasmtime-based runtime for executing agent code with capability-based security.
Add to your Cargo.toml:
[dependencies]
mielin-wasm = { path = "../mielin-wasm" }
use mielin_wasm::executor::WasmExecutor;
use mielin_cells::Agent;
// Create executor
let executor = WasmExecutor::new()?;
// Create agent with WASM binary
let agent = Agent::new(wasm_binary);
// Execute agent
let result = executor.execute(&agent)?;
println!("Exit code: {}", result.exit_code);
use mielin_wasm::executor::WasmExecutor;
let executor = WasmExecutor::new()?;
// Validate WASM module
match executor.validate(&wasm_bytes) {
Ok(_) => println!("Module is valid"),
Err(e) => eprintln!("Invalid module: {}", e),
}
use mielin_wasm::sandbox::{Sandbox, Capability};
let mut sandbox = Sandbox::new();
// Grant specific capabilities
sandbox.grant(Capability::Network);
sandbox.grant(Capability::FileSystem);
// Check permissions
if sandbox.has_capability(&Capability::Camera) {
// Allow camera access
} else {
// Deny camera access
}
Main execution engine:
pub struct WasmExecutor {
engine: Engine,
}
impl WasmExecutor {
pub fn new() -> Result<Self, WasmError>;
pub fn with_config(config: Config) -> Result<Self, WasmError>;
pub fn compile_module(&self, wasm_bytes: &[u8]) -> Result<Module, WasmError>;
pub fn execute(&self, agent: &Agent) -> Result<ExecutionResult, WasmError>;
pub fn suspend(&self, agent: &Agent) -> Result<Vec<u8>, WasmError>;
pub fn validate(&self, wasm_bytes: &[u8]) -> Result<(), WasmError>;
}
Capability-based security:
pub struct Sandbox {
allowed_capabilities: HashSet<Capability>,
}
pub enum Capability {
FileSystem,
Network,
Camera,
Gpio,
}
impl Sandbox {
pub fn new() -> Self;
pub fn grant(&mut self, cap: Capability);
pub fn has_capability(&self, cap: &Capability) -> bool;
}
Execution outcome:
pub struct ExecutionResult {
pub exit_code: i32,
pub memory_snapshot: Vec<u8>,
}
use mielin_wasm::executor::WasmExecutor;
use mielin_cells::Agent;
// WAT format (WebAssembly Text)
let wat = r#"
(module
(func (export "_start")
;; Agent code here
)
)
"#;
let wasm = wat::parse_str(wat)?;
let agent = Agent::new(wasm);
let executor = WasmExecutor::new()?;
let result = executor.execute(&agent)?;
assert_eq!(result.exit_code, 0);
use mielin_wasm::executor::WasmExecutor;
let executor = WasmExecutor::new()?;
// Execute agent
executor.execute(&agent)?;
// Suspend and capture memory
let memory_snapshot = executor.suspend(&agent)?;
// Use snapshot for migration
println!("Captured {} bytes of memory", memory_snapshot.len());
use mielin_wasm::executor::WasmExecutor;
use wasmtime::Config;
let mut config = Config::new();
config.wasm_multi_memory(true);
config.wasm_multi_value(true);
config.consume_fuel(true); // Enable fuel metering
let executor = WasmExecutor::with_config(config)?;
use mielin_wasm::sandbox::{Sandbox, Capability};
// Create restricted sandbox
let mut sandbox = Sandbox::new();
// Grant only network access
sandbox.grant(Capability::Network);
// Before allowing operation, check capability
fn allow_camera_access(sandbox: &Sandbox) -> bool {
sandbox.has_capability(&Capability::Camera)
}
assert!(!allow_camera_access(&sandbox)); // Denied
| 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 |
The capability model follows the principle of least privilege:
pub enum WasmError {
CompilationFailed(String),
ExecutionFailed(String),
InvalidModule(String),
}
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 |
cargo test -p mielin-wasm
Tests include:
Current limitations:
Limit agent execution time:
let mut config = Config::new();
config.consume_fuel(true);
let executor = WasmExecutor::with_config(config)?;
// Set fuel limit before execution
Enable multiple linear memories:
let mut config = Config::new();
config.wasm_multi_memory(true);
let executor = WasmExecutor::with_config(config)?;
validate() before executionMIT OR Apache-2.0