RavenClaws 1.1.0

Lightweight, secure Rust agent framework with multi-provider LLM support
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! # WASM Plugin System
//!
//! Extends RavenClaws with WebAssembly plugins — load `.wasm` binaries at
//! runtime and register their exported tools into the agent's `ToolRegistry`.
//!
//! ## Plugin ABI (v1)
//!
//! Every WASM plugin **must** export the following functions:
//!
//! | Export | Signature | Description |
//! |--------|-----------|-------------|
//! | `plugin_name` | `() -> i32` | Pointer to null-terminated UTF-8 name string in linear memory |
//! | `plugin_version` | `() -> i32` | Pointer to null-terminated version string |
//! | `plugin_description` | `() -> i32` | Pointer to null-terminated description string |
//! | `plugin_tools_count` | `() -> i32` | Number of tools this plugin exposes |
//! | `plugin_tool_name` | `(i32) -> i32` | Given tool index, returns pointer to name string |
//! | `plugin_tool_description` | `(i32) -> i32` | Given tool index, returns pointer to description string |
//! | `plugin_tool_execute` | `(i32, i32) -> i32` | Given tool index + input pointer, returns pointer to output string |
//!
//! Strings are communicated via **pointer into the plugin's exported linear
//! memory**. The host reads them by inspecting the memory at the returned offset.

use std::sync::Arc;

use wasmtime::{Engine, Instance, Linker, Memory, Module, Store, TypedFunc};

use crate::tools::{ToolDefinition, ToolImpl, ToolRegistry, ToolResult, ToolResultValue};

// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------

/// Errors that can occur during plugin loading, inspection, or execution.
#[derive(Debug, thiserror::Error)]
pub enum PluginError {
    /// The WASM binary could not be compiled.
    #[error("Failed to compile WASM module: {0}")]
    Compile(String),

    /// The WASM module could not be instantiated.
    #[error("Failed to instantiate WASM module: {0}")]
    Instantiate(String),

    /// A required export is missing from the plugin.
    #[error("Missing required export '{export}' in plugin")]
    MissingExport { export: String },

    /// An export has the wrong type signature.
    #[error("Export '{export}' has wrong type: {details}")]
    WrongExportType { export: String, details: String },

    /// A tool call failed at runtime.
    #[error("Plugin tool '{tool}' execution failed: {details}")]
    ToolExecution { tool: String, details: String },

    /// The plugin version is not compatible with this host.
    #[error("Plugin version '{version}' is not compatible with host ABI v1")]
    VersionMismatch { version: String },

    /// General I/O or other error.
    #[error(transparent)]
    Other(#[from] anyhow::Error),
}

// ---------------------------------------------------------------------------
// PluginTool — a tool exposed by a WASM plugin
// ---------------------------------------------------------------------------

/// A single tool exposed by a WASM plugin.
#[derive(Debug, Clone)]
pub struct PluginTool {
    /// Human-readable name (e.g. "hello", "echo").
    pub name: String,
    /// Short description of what the tool does.
    pub description: String,
    /// Index of this tool within the plugin (0-based).
    pub index: u32,
}

// ---------------------------------------------------------------------------
// WasmPlugin — a loaded plugin instance
// ---------------------------------------------------------------------------

/// A loaded WASM plugin with its store, instance, and exported tools.
pub struct WasmPlugin {
    /// Plugin name (from `plugin_name` export).
    pub name: String,
    /// Plugin version (from `plugin_version` export).
    pub version: String,
    /// Plugin description (from `plugin_description` export).
    pub description: String,
    /// Tools exported by this plugin.
    pub tools: Vec<PluginTool>,
    /// The store holds the plugin's linear memory and runtime state.
    store: Store<()>,
    /// Exported memory reference.
    memory: Memory,
    /// Cached typed functions.
    tool_execute: TypedFunc<(i32, i32), i32>,
}

impl std::fmt::Debug for WasmPlugin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WasmPlugin")
            .field("name", &self.name)
            .field("version", &self.version)
            .field("description", &self.description)
            .field("tools", &self.tools)
            .finish_non_exhaustive()
    }
}

impl WasmPlugin {
    /// Load a plugin from a `.wasm` file on disk.
    pub fn load(path: &std::path::Path) -> Result<Self, PluginError> {
        let wasm_bytes = std::fs::read(path).map_err(|e| {
            PluginError::Other(anyhow::anyhow!("Failed to read {}: {}", path.display(), e))
        })?;
        Self::load_from_bytes(&wasm_bytes)
    }

    /// Load a plugin from an in-memory WASM binary.
    pub fn load_from_bytes(wasm_bytes: &[u8]) -> Result<Self, PluginError> {
        let engine = Engine::default();
        let module =
            Module::new(&engine, wasm_bytes).map_err(|e| PluginError::Compile(e.to_string()))?;

        let mut store = Store::new(&engine, ());
        let linker = Linker::new(&engine);
        let instance = linker
            .instantiate(&mut store, &module)
            .map_err(|e| PluginError::Instantiate(e.to_string()))?;

        // ---- read required exports ----
        let name = Self::read_export_string(&instance, &mut store, "plugin_name")?;
        let version = Self::read_export_string(&instance, &mut store, "plugin_version")?;
        let description = Self::read_export_string(&instance, &mut store, "plugin_description")?;

        // ---- version check ----
        if version.is_empty() {
            return Err(PluginError::VersionMismatch {
                version: version.clone(),
            });
        }

        // ---- tool count ----
        let tools_count: TypedFunc<(), i32> = instance
            .get_export(&mut store, "plugin_tools_count")
            .and_then(|e| e.into_func())
            .ok_or_else(|| PluginError::MissingExport {
                export: "plugin_tools_count".into(),
            })?
            .typed(&store)
            .map_err(|e| PluginError::WrongExportType {
                export: "plugin_tools_count".into(),
                details: e.to_string(),
            })?;
        let count: u32 =
            tools_count
                .call(&mut store, ())
                .map_err(|e| PluginError::ToolExecution {
                    tool: "plugin_tools_count".into(),
                    details: e.to_string(),
                })? as u32;

        // ---- tool name / description functions ----
        let tool_name_fn: TypedFunc<i32, i32> = instance
            .get_export(&mut store, "plugin_tool_name")
            .and_then(|e| e.into_func())
            .ok_or_else(|| PluginError::MissingExport {
                export: "plugin_tool_name".into(),
            })?
            .typed(&store)
            .map_err(|e| PluginError::WrongExportType {
                export: "plugin_tool_name".into(),
                details: e.to_string(),
            })?;

        let tool_desc_fn: TypedFunc<i32, i32> = instance
            .get_export(&mut store, "plugin_tool_description")
            .and_then(|e| e.into_func())
            .ok_or_else(|| PluginError::MissingExport {
                export: "plugin_tool_description".into(),
            })?
            .typed(&store)
            .map_err(|e| PluginError::WrongExportType {
                export: "plugin_tool_description".into(),
                details: e.to_string(),
            })?;

        // ---- tool_execute ----
        let tool_execute: TypedFunc<(i32, i32), i32> = instance
            .get_export(&mut store, "plugin_tool_execute")
            .and_then(|e| e.into_func())
            .ok_or_else(|| PluginError::MissingExport {
                export: "plugin_tool_execute".into(),
            })?
            .typed(&store)
            .map_err(|e| PluginError::WrongExportType {
                export: "plugin_tool_execute".into(),
                details: e.to_string(),
            })?;

        // ---- memory ----
        let memory: Memory = instance
            .get_export(&mut store, "memory")
            .and_then(|e| e.into_memory())
            .ok_or_else(|| PluginError::MissingExport {
                export: "memory".into(),
            })?;

        // ---- enumerate tools ----
        let mut tools = Vec::with_capacity(count as usize);
        for i in 0..count {
            let idx = i as i32;
            let name_ptr =
                tool_name_fn
                    .call(&mut store, idx)
                    .map_err(|e| PluginError::ToolExecution {
                        tool: "plugin_tool_name".into(),
                        details: e.to_string(),
                    })?;
            let desc_ptr =
                tool_desc_fn
                    .call(&mut store, idx)
                    .map_err(|e| PluginError::ToolExecution {
                        tool: "plugin_tool_description".into(),
                        details: e.to_string(),
                    })?;

            let t_name = Self::read_string_from_memory(&memory, &store, name_ptr as u32);
            let t_desc = Self::read_string_from_memory(&memory, &store, desc_ptr as u32);

            tools.push(PluginTool {
                name: t_name,
                description: t_desc,
                index: i,
            });
        }

        Ok(Self {
            name,
            version,
            description,
            tools,
            store,
            memory,
            tool_execute,
        })
    }

    /// Execute a tool by index, passing `input` as the argument string.
    /// Returns the plugin's output string.
    pub fn execute_tool(&mut self, tool_index: u32, input: &str) -> Result<String, PluginError> {
        // Write input into plugin memory at a known offset (after any existing data).
        let input_offset = self.reserve_memory(input.len())?;
        self.memory
            .write(&mut self.store, input_offset, input.as_bytes())
            .map_err(|e| PluginError::ToolExecution {
                tool: self.tools[tool_index as usize].name.clone(),
                details: format!("Failed to write input to plugin memory: {e}"),
            })?;

        let result_ptr = self
            .tool_execute
            .call(&mut self.store, (tool_index as i32, input_offset as i32))
            .map_err(|e| PluginError::ToolExecution {
                tool: self.tools[tool_index as usize].name.clone(),
                details: e.to_string(),
            })?;

        let output = Self::read_string_from_memory(&self.memory, &self.store, result_ptr as u32);
        Ok(output)
    }

    /// Register all plugin tools into a `ToolRegistry`.
    pub fn register_into_registry(&mut self, registry: &mut ToolRegistry) {
        let plugin_name = self.name.clone();
        for i in 0..self.tools.len() as u32 {
            let tool = self.tools[i as usize].clone();
            let wrapper = WasmPluginToolWrapper {
                plugin_name: plugin_name.clone(),
                tool_index: i,
                tool_name: tool.name.clone(),
                tool_description: tool.description.clone(),
            };
            registry.register(Arc::new(wrapper));
        }
    }

    // ---- helpers ----

    /// Read a null-terminated UTF-8 string from the plugin's linear memory.
    fn read_string_from_memory(memory: &Memory, store: &Store<()>, offset: u32) -> String {
        let data = memory.data(store);
        let mut end = offset as usize;
        while end < data.len() && data[end] != 0 {
            end += 1;
        }
        String::from_utf8_lossy(&data[offset as usize..end]).to_string()
    }

    /// Call a zero-argument export that returns an `i32` pointer into memory,
    /// then read the string at that pointer.
    fn read_export_string(
        instance: &Instance,
        store: &mut Store<()>,
        export_name: &str,
    ) -> Result<String, PluginError> {
        let func: TypedFunc<(), i32> = instance
            .get_export(&mut *store, export_name)
            .and_then(|e| e.into_func())
            .ok_or_else(|| PluginError::MissingExport {
                export: export_name.into(),
            })?
            .typed(&*store)
            .map_err(|e| PluginError::WrongExportType {
                export: export_name.into(),
                details: e.to_string(),
            })?;

        let ptr = func
            .call(&mut *store, ())
            .map_err(|e| PluginError::ToolExecution {
                tool: export_name.into(),
                details: e.to_string(),
            })?;

        // Get memory to read the string
        let memory: Memory = instance
            .get_export(&mut *store, "memory")
            .and_then(|e| e.into_memory())
            .ok_or_else(|| PluginError::MissingExport {
                export: "memory".into(),
            })?;

        Ok(Self::read_string_from_memory(&memory, &*store, ptr as u32))
    }

    /// Find a free region in plugin memory large enough for `size` bytes.
    /// Uses a simple bump-allocator approach starting after the first 64 KB.
    fn reserve_memory(&mut self, size: usize) -> Result<usize, PluginError> {
        let current_size = self.memory.data_size(&self.store);
        let needed = current_size + size;
        let pages_needed = needed.div_ceil(65536);
        let current_pages = self.memory.size(&self.store) as usize;
        if pages_needed > current_pages {
            let delta = (pages_needed - current_pages) as u64;
            self.memory
                .grow(&mut self.store, delta)
                .map_err(PluginError::Other)?;
        }
        Ok(current_size)
    }
}

// ---------------------------------------------------------------------------
// WasmPluginToolWrapper — implements ToolImpl for a single plugin tool
// ---------------------------------------------------------------------------

struct WasmPluginToolWrapper {
    plugin_name: String,
    tool_index: u32,
    tool_name: String,
    #[allow(dead_code)]
    tool_description: String,
}

impl std::fmt::Debug for WasmPluginToolWrapper {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WasmPluginToolWrapper")
            .field("plugin_name", &self.plugin_name)
            .field("tool_index", &self.tool_index)
            .field("tool_name", &self.tool_name)
            .finish()
    }
}

#[async_trait::async_trait]
impl ToolImpl for WasmPluginToolWrapper {
    fn definition(&self) -> &ToolDefinition {
        // Return a static definition — this is a simplified approach.
        // In production, the definition would be cached.
        use std::sync::OnceLock;
        static DEF: OnceLock<ToolDefinition> = OnceLock::new();
        DEF.get_or_init(|| ToolDefinition {
            name: self.tool_name.clone(),
            description: self.tool_description.clone(),
            parameters: crate::tools::JsonSchema::object(
                std::collections::HashMap::new(),
                Vec::new(),
            ),
            requires_approval: false,
            category: crate::tools::ToolCategory::General,
        })
    }

    async fn execute(&self, _args: serde_json::Value) -> ToolResultValue<ToolResult> {
        // We need the wasm bytes to re-instantiate. This simplified wrapper
        // doesn't have access to them. In production, the wrapper would store
        // the wasm bytes and re-instantiate on each call.
        Err(crate::tools::ToolError::ExecutionFailed(
            self.tool_name.clone(),
            "WASM plugin tool requires re-instantiation — not yet implemented".into(),
        ))
    }

    fn name(&self) -> &str {
        &self.tool_name
    }
}

// ---------------------------------------------------------------------------
// WasmPluginManager — manages multiple loaded plugins
// ---------------------------------------------------------------------------

/// Manages a collection of loaded WASM plugins.
pub struct WasmPluginManager {
    plugins: Vec<WasmPlugin>,
}

impl WasmPluginManager {
    /// Create a new empty plugin manager.
    pub fn new() -> Self {
        Self {
            plugins: Vec::new(),
        }
    }

    /// Load a plugin from a `.wasm` file and add it to the manager.
    pub fn load(&mut self, path: &std::path::Path) -> Result<&WasmPlugin, PluginError> {
        let plugin = WasmPlugin::load(path)?;
        self.plugins.push(plugin);
        Ok(self.plugins.last().unwrap())
    }

    /// Load a plugin from bytes and add it to the manager.
    pub fn load_from_bytes(&mut self, bytes: &[u8]) -> Result<&WasmPlugin, PluginError> {
        let plugin = WasmPlugin::load_from_bytes(bytes)?;
        self.plugins.push(plugin);
        Ok(self.plugins.last().unwrap())
    }

    /// Get a reference to a loaded plugin by name.
    pub fn get(&self, name: &str) -> Option<&WasmPlugin> {
        self.plugins.iter().find(|p| p.name == name)
    }

    /// Get a mutable reference to a loaded plugin by name.
    pub fn get_mut(&mut self, name: &str) -> Option<&mut WasmPlugin> {
        self.plugins.iter_mut().find(|p| p.name == name)
    }

    /// Unload (remove) a plugin by name.
    pub fn unload(&mut self, name: &str) -> bool {
        let idx = self.plugins.iter().position(|p| p.name == name);
        if let Some(i) = idx {
            self.plugins.remove(i);
            true
        } else {
            false
        }
    }

    /// Iterate over all loaded plugins.
    pub fn iter(&self) -> impl Iterator<Item = &WasmPlugin> {
        self.plugins.iter()
    }

    /// Register all tools from all plugins into a `ToolRegistry`.
    pub fn register_all(&mut self, registry: &mut ToolRegistry) {
        for plugin in self.plugins.iter_mut() {
            plugin.register_into_registry(registry);
        }
    }

    /// Number of loaded plugins.
    pub fn len(&self) -> usize {
        self.plugins.len()
    }

    /// Returns `true` if no plugins are loaded.
    pub fn is_empty(&self) -> bool {
        self.plugins.is_empty()
    }
}

impl Default for WasmPluginManager {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for WasmPluginManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WasmPluginManager")
            .field("plugin_count", &self.plugins.len())
            .finish_non_exhaustive()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    /// A minimal "hello" plugin in WAT format.
    const HELLO_PLUGIN_WAT: &str = r#"
    (module
      (memory (export "memory") 1 256)
      (data (i32.const 0) "Hello from WASM!\00")
      (data (i32.const 32) "0.1.0\00")
      (data (i32.const 64) "A friendly WASM plugin\00")
      (data (i32.const 128) "hello\00")
      (data (i32.const 160) "Returns a friendly greeting\00")
      (func (export "plugin_name") (result i32) i32.const 0)
      (func (export "plugin_version") (result i32) i32.const 32)
      (func (export "plugin_description") (result i32) i32.const 64)
      (func (export "plugin_tools_count") (result i32) i32.const 1)
      (func (export "plugin_tool_name") (param i32) (result i32)
        i32.const 128)
      (func (export "plugin_tool_description") (param i32) (result i32)
        i32.const 160)
      (func (export "plugin_tool_execute") (param i32 i32) (result i32)
        i32.const 0)
    )
    "#;

    /// A plugin with two tools: "hello" and "echo".
    const ECHO_PLUGIN_WAT: &str = r#"
    (module
      (memory (export "memory") 1 256)
      (data (i32.const 0) "Echo Plugin\00")
      (data (i32.const 32) "0.1.0\00")
      (data (i32.const 64) "A plugin that echoes input\00")
      (data (i32.const 128) "echo\00")
      (data (i32.const 160) "Echoes back the input string\00")
      (data (i32.const 192) "hello\00")
      (data (i32.const 224) "Returns a friendly greeting\00")
      (func (export "plugin_name") (result i32) i32.const 0)
      (func (export "plugin_version") (result i32) i32.const 32)
      (func (export "plugin_description") (result i32) i32.const 64)
      (func (export "plugin_tools_count") (result i32) i32.const 2)
      (func (export "plugin_tool_name") (param i32) (result i32)
        local.get 0
        if (result i32)
          i32.const 128
        else
          i32.const 192
        end)
      (func (export "plugin_tool_description") (param i32) (result i32)
        local.get 0
        if (result i32)
          i32.const 160
        else
          i32.const 224
        end)
      (func (export "plugin_tool_execute") (param i32 i32) (result i32)
        local.get 1)
    )
    "#;

    fn compile_wat(wat: &str) -> Vec<u8> {
        wat::parse_str(wat).expect("Failed to parse WAT")
    }

    #[test]
    fn test_plugin_load() {
        let wasm = compile_wat(HELLO_PLUGIN_WAT);
        let plugin = WasmPlugin::load_from_bytes(&wasm).expect("Failed to load plugin");
        assert_eq!(plugin.name, "Hello from WASM!");
        assert_eq!(plugin.version, "0.1.0");
        assert_eq!(plugin.description, "A friendly WASM plugin");
    }

    #[test]
    fn test_plugin_tools() {
        let wasm = compile_wat(HELLO_PLUGIN_WAT);
        let plugin = WasmPlugin::load_from_bytes(&wasm).expect("Failed to load plugin");
        assert_eq!(plugin.tools.len(), 1);
        assert_eq!(plugin.tools[0].name, "hello");
        assert_eq!(plugin.tools[0].description, "Returns a friendly greeting");
    }

    #[test]
    fn test_plugin_execute_hello() {
        let wasm = compile_wat(HELLO_PLUGIN_WAT);
        let mut plugin = WasmPlugin::load_from_bytes(&wasm).expect("Failed to load plugin");
        let result = plugin.execute_tool(0, "").expect("Failed to execute tool");
        assert_eq!(result, "Hello from WASM!");
    }

    #[test]
    fn test_plugin_execute_echo() {
        let wasm = compile_wat(ECHO_PLUGIN_WAT);
        let mut plugin = WasmPlugin::load_from_bytes(&wasm).expect("Failed to load plugin");
        let result = plugin
            .execute_tool(1, "Hello world")
            .expect("Failed to execute tool");
        assert_eq!(result, "Hello world");
    }

    #[test]
    fn test_plugin_manager() {
        let wasm = compile_wat(HELLO_PLUGIN_WAT);
        let mut manager = WasmPluginManager::new();
        manager.load_from_bytes(&wasm).expect("Failed to load");
        assert_eq!(manager.len(), 1);
        let plugin = manager.get("Hello from WASM!").expect("Plugin not found");
        assert_eq!(plugin.tools.len(), 1);
    }

    #[test]
    fn test_plugin_manager_execute() {
        let wasm = compile_wat(ECHO_PLUGIN_WAT);
        let mut manager = WasmPluginManager::new();
        manager.load_from_bytes(&wasm).expect("Failed to load");
        let plugin = manager.get_mut("Echo Plugin").expect("Plugin not found");
        let result = plugin
            .execute_tool(0, "test echo")
            .expect("Failed to execute");
        assert_eq!(result, "test echo");
    }

    #[test]
    fn test_plugin_unload() {
        let wasm = compile_wat(HELLO_PLUGIN_WAT);
        let mut manager = WasmPluginManager::new();
        manager.load_from_bytes(&wasm).expect("Failed to load");
        assert_eq!(manager.len(), 1);
        assert!(manager.unload("Hello from WASM!"));
        assert_eq!(manager.len(), 0);
    }

    #[test]
    fn test_plugin_unload_nonexistent() {
        let mut manager = WasmPluginManager::new();
        assert!(!manager.unload("nonexistent"));
    }

    #[test]
    fn test_plugin_version_mismatch() {
        let wat = r#"
        (module
          (memory (export "memory") 1)
          (data (i32.const 0) "Empty\00")
          (data (i32.const 32) "\00")
          (data (i32.const 64) "No version\00")
          (func (export "plugin_name") (result i32) i32.const 0)
          (func (export "plugin_version") (result i32) i32.const 32)
          (func (export "plugin_description") (result i32) i32.const 64)
          (func (export "plugin_tools_count") (result i32) i32.const 0)
        )
        "#;
        let wasm = compile_wat(wat);
        let result = WasmPlugin::load_from_bytes(&wasm);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            matches!(&err, PluginError::VersionMismatch { .. }),
            "Expected VersionMismatch, got {err}"
        );
    }

    #[test]
    fn test_plugin_missing_export() {
        let wat = r#"
        (module
          (memory (export "memory") 1)
        )
        "#;
        let wasm = compile_wat(wat);
        let result = WasmPlugin::load_from_bytes(&wasm);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            matches!(&err, PluginError::MissingExport { .. }),
            "Expected MissingExport, got {err}"
        );
    }

    #[test]
    fn test_plugin_default_manager() {
        let manager = WasmPluginManager::default();
        assert!(manager.is_empty());
        assert_eq!(manager.len(), 0);
    }
}