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
//! WASM-extensible channel system.
//!
//! This module provides a runtime for executing WASM-based channels using a
//! Host-Managed Event Loop pattern. The host (Rust) manages infrastructure
//! (HTTP server, polling), while WASM modules define channel behavior through
//! callbacks.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────────────────────┐
//! │ Host-Managed Event Loop │
//! │ │
//! │ ┌─────────────┐ ┌──────────────┐ ┌──────────────┐ │
//! │ │ HTTP │ │ Polling │ │ Timer │ │
//! │ │ Router │ │ Scheduler │ │ Scheduler │ │
//! │ └──────┬──────┘ └──────┬───────┘ └──────┬───────┘ │
//! │ │ │ │ │
//! │ └───────────────────┴────────────────────┘ │
//! │ │ │
//! │ ▼ │
//! │ ┌─────────────────┐ │
//! │ │ Event Router │ │
//! │ └────────┬────────┘ │
//! │ │ │
//! │ ┌──────────────────┼──────────────────┐ │
//! │ ▼ ▼ ▼ │
//! │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
//! │ │ on_http_req │ │ on_poll │ │ on_respond │ WASM Exports │
//! │ └─────────────┘ └─────────────┘ └─────────────┘ │
//! │ │ │ │ │
//! │ └──────────────────┴──────────────────┘ │
//! │ │ │
//! │ ▼ │
//! │ ┌─────────────────┐ │
//! │ │ Host Imports │ │
//! │ │ emit_message │──────────▶ MessageStream │
//! │ │ http_request │ │
//! │ │ log, etc. │ │
//! │ └─────────────────┘ │
//! └─────────────────────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Key Design Decisions
//!
//! 1. **Fresh Instance Per Callback** (NEAR Pattern) - Full isolation, no shared mutable state
//! 2. **Host Manages Infrastructure** - HTTP server, polling, timing in Rust
//! 3. **WASM Defines Behavior** - Callbacks for events, message parsing, response handling
//! 4. **Reuse Tool Runtime** - Share Wasmtime engine, extend capabilities
//!
//! # Security Model
//!
//! | Threat | Mitigation |
//! |--------|------------|
//! | Path hijacking | `allowed_paths` restricts registrable endpoints |
//! | Token exposure | Injected at host boundary, WASM never sees |
//! | State pollution | Fresh instance per callback |
//! | Workspace escape | Paths prefixed with `channels/<name>/` |
//! | Message spam | Rate limiting on `emit_message` |
//! | Resource exhaustion | Fuel metering, memory limits, callback timeout |
//! | Polling abuse | Minimum 30s interval enforced |
//!
//! # Example Usage
//!
//! ```ignore
//! use ironclaw::channels::wasm::{WasmChannelLoader, WasmChannelRuntime};
//!
//! // Create runtime (can share engine with tool runtime)
//! let runtime = WasmChannelRuntime::new(config)?;
//!
//! // Load channels from directory
//! let loader = WasmChannelLoader::new(runtime);
//! let channels = loader.load_from_dir(Path::new("~/.ironclaw/channels/")).await?;
//!
//! // Add to channel manager
//! for channel in channels {
//! manager.add(Box::new(channel));
//! }
//! ```
// Core types
pub use ;
pub use ;
pub use WasmChannelError;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;