agent-works 0.13.0

Batteries-included Agent toolbox built on agent-base
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
//! Plugin wasm form: loading and execution via the wasmtime Component Model.
//!
//! Criteria (`notes/tool-socket-design.md`, "Plugin package mechanism"):
//!
//! - **WIT contract** `phi:tool@0.1.0` `executor.invoke(tool, args)` — both the host
//!   and author-side bindings consume `wit/phi-tool.wit` (`wit/tool-guest-world.wit` is
//!   just build glue);
//! - **Capability tightening only honors pinned registry values** ([`ResolvedCapability`]),
//!   not the plugin manifest: fs only preopens pinned paths (undeclared = no preopen =
//!   cannot read anything); net only allows pinned hosts ([`HostFilterHooks`] filters
//!   per request, anything not on the whitelist is denied); raw sockets are denied by
//!   default (no `inherit_network`, all wasi:sockets operations return
//!   PermissionDenied); when net is undeclared, `wasi:http` is not even linked
//!   (cannot be called);
//! - **Timeout/suspension kills the tool, not the host**: epoch interruption cuts it
//!   off ([`Trap::Interrupt`] → [`WasmError::Timeout`]), guest panic/trap
//!   → [`WasmError::Trap`] — the host keeps serving other tools (the second call in
//!   tests verifies liveness);
//! - **Compile at load time**: a corrupt .wasm errors at the moment of
//!   [`WasmRuntime::compile`], and the loader disables just that tool instead of
//!   skipping the whole package.
//!
//! secret rule: the wasm side only sees resolved literals (`${ENV:VAR}` is resolved
//! by the product layer before connection/environment injection); the host never
//! resolves or persists any reference.

use std::path::Path;
use std::sync::Arc;
use std::time::Duration;

use agent_base::{AgentError, AgentResult, Content, Tool, ToolContext, ToolExposure, ToolMetadata};
use async_trait::async_trait;
use serde_json::Value;
use wasmtime::component::{Component, Linker};
use wasmtime::{Config, Engine, Store, Trap};
use wasmtime_wasi::p2::pipe::MemoryOutputPipe;
use wasmtime_wasi::{FsPerms, ResourceTable, WasiCtx, WasiCtxBuilder, WasiCtxView, WasiView};
use wasmtime_wasi_http::p2::add_only_http_to_linker_async;
use wasmtime_wasi_http::{
    Error as HttpError, RequestOptions, WasiBody, WasiHttpCtx, WasiHttpCtxView, WasiHttpHooks,
    WasiHttpView,
};

use super::manifest::ResolvedCapability;
use crate::script_tool::{ParamSpec, parameters_envelope};
use std::collections::BTreeMap;

/// epoch tick granularity (timeout quantization unit).
const EPOCH_TICK_MS: u64 = 10;

/// How many epoch ticker threads have been started in this process.
///
/// Starts at exactly one when the shared engine is built; the counter exists
/// so a regression (a ticker per runtime) is observable rather than merely
/// suspected.
fn epoch_ticker_spawns_counter() -> &'static std::sync::atomic::AtomicUsize {
    static SPAWNS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
    &SPAWNS
}

#[cfg(test)]
fn epoch_ticker_spawns() -> usize {
    epoch_ticker_spawns_counter().load(std::sync::atomic::Ordering::SeqCst)
}
/// The process-wide wasmtime engine and its single epoch ticker.
///
/// Two independent reasons this is shared instead of built per runtime:
///
/// 1. **macOS trap-handler safety.** wasmtime's mach-port trap handler stores
///    its port in a process-global `static mut`. Creating a second Engine
///    overwrites that port while the first handler thread is still waiting on
///    it; the stale thread then calls `abort()` and takes the whole process
///    down with SIGABRT. One engine means one handler thread, so the port is
///    written once.
/// 2. **One epoch ticker.** The ticker thread runs forever, so a per-runtime
///    engine would leak one thread per runtime.
///
/// The engine is created lazily and never dropped (it is held in a
/// `OnceLock` for the life of the process).
fn shared_engine() -> Result<Engine, WasmError> {
    static ENGINE: std::sync::OnceLock<Result<Engine, String>> = std::sync::OnceLock::new();

    let result = ENGINE.get_or_init(|| {
        let mut config = Config::new();
        config.wasm_component_model(true);
        config.epoch_interruption(true);
        // On macOS, use the signals-based trap handler instead of mach ports:
        // mach ports do not survive `fork()`, and this process forks children.
        // Disabling them also removes the static-mut port race described above.
        if cfg!(target_os = "macos") {
            config.macos_use_mach_ports(false);
        }
        let engine = Engine::new(&config).map_err(|e| e.to_string())?;

        // The heartbeat behind the timeout criterion: bump the epoch every
        // tick; callers convert the timeout into a deadline. Started once,
        // next to the engine it serves.
        let ticker = engine.clone();
        std::thread::Builder::new()
            .name("plugin-wasm-epoch".into())
            .spawn(move || {
                loop {
                    std::thread::sleep(Duration::from_millis(EPOCH_TICK_MS));
                    ticker.increment_epoch();
                }
            })
            .map_err(|e| e.to_string())?;
        epoch_ticker_spawns_counter().fetch_add(1, std::sync::atomic::Ordering::SeqCst);

        Ok(engine)
    });

    match result {
        Ok(engine) => Ok(engine.clone()),
        Err(err) => Err(WasmError::Host(err.clone())),
    }
}

wasmtime::component::bindgen!({
    path: "wit",
    world: "tool-guest",
    exports: { default: async },
});

/// Load/execution errors for the wasm form. All of them hang only on the tool side
/// (call or load), never on the host — a host panic is not covered here, that is a bug.
#[derive(Debug)]
pub enum WasmError {
    /// Failed to read the component file.
    Io(std::io::Error),
    /// Component compilation failed (corrupt / not a component / version mismatch)
    /// → disable the tool at load time.
    Compile(String),
    /// epoch interruption cut the call off (timed out / stuck).
    Timeout { timeout: Duration },
    /// guest trap / panic.
    Trap(String),
    /// the guest explicitly returned an error (`result::err`).
    Guest(String),
    /// capability denied (e.g. net outside the whitelist).
    Denied(String),
    /// host linking/instantiation failed.
    Host(String),
}

impl std::fmt::Display for WasmError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io(e) => write!(f, "wasm io: {e}"),
            Self::Compile(e) => write!(f, "wasm compile failed: {e}"),
            Self::Timeout { timeout } => {
                write!(f, "wasm call timed out after {}ms", timeout.as_millis())
            }
            Self::Trap(e) => write!(f, "wasm trapped: {e}"),
            Self::Guest(e) => write!(f, "wasm tool error: {e}"),
            Self::Denied(e) => write!(f, "wasm capability denied: {e}"),
            Self::Host(e) => write!(f, "wasm host error: {e}"),
        }
    }
}

impl std::error::Error for WasmError {}

/// Input for one wasm call. `capabilities` MUST be pinned registry values
/// (enforcement does not honor the plugin's own manifest).
pub struct WasmCall<'a> {
    /// the `tool` parameter of `executor.invoke` (the `name` of the wasm entry in the manifest).
    pub tool: &'a str,
    /// validated JSON argument text (the host MUST validate against the parameters schema first).
    pub args_json: &'a str,
    /// pinned capability whitelist values.
    pub capabilities: &'a [ResolvedCapability],
    /// hard timeout.
    pub timeout: Duration,
}

/// wasm runtime: one engine + one epoch tick thread (shared within the process).
pub struct WasmRuntime {
    engine: Engine,
}

impl WasmRuntime {
    /// Build a runtime (component model + async + epoch interruption).
    ///
    /// The engine is a process-wide singleton: wasmtime's macOS mach-port
    /// trap handler keeps its port in a `static mut`, so building a second
    /// Engine overwrites the port the first handler thread is still using —
    /// that thread then aborts the process (SIGABRT). Sharing one engine
    /// also keeps exactly one epoch ticker thread alive, which is what the
    /// struct doc already promised.
    pub fn new() -> Result<Self, WasmError> {
        Ok(Self {
            engine: shared_engine()?,
        })
    }

    /// Compile one component at load time (failure = the error surface that disables
    /// this tool, not skipping the whole package).
    pub fn compile(&self, module_path: &Path) -> Result<Component, WasmError> {
        Component::from_file(&self.engine, module_path)
            .map_err(|e| WasmError::Compile(e.to_string()))
    }

    /// Execute one call. Timeout/trap/capability denial only hangs this call.
    pub async fn invoke(
        &self,
        component: &Component,
        call: &WasmCall<'_>,
    ) -> Result<String, WasmError> {
        let state = HostState::new(call.capabilities)?;
        let mut linker: Linker<HostState> = Linker::new(&self.engine);
        wasmtime_wasi::p2::add_to_linker_async(&mut linker)
            .map_err(|e| WasmError::Host(e.to_string()))?;
        // net capability: undeclared = don't even link wasi:http (the guest cannot call it).
        if call
            .capabilities
            .iter()
            .any(|c| matches!(c, ResolvedCapability::NetHttp { .. }))
        {
            add_only_http_to_linker_async(&mut linker)
                .map_err(|e| WasmError::Host(e.to_string()))?;
        }

        let mut store = Store::new(&self.engine, state);
        let ticks = (call.timeout.as_millis() as u64 / EPOCH_TICK_MS).max(1);
        store.set_epoch_deadline(ticks);

        let bindings = ToolGuest::instantiate_async(&mut store, component, &linker)
            .await
            .map_err(classify_host_error)?;

        // When the exported interface comes from package `phi:tool`, the accessor name
        // carries the package-name prefix (bindgen naming).
        let result = bindings
            .phi_tool_executor()
            .call_invoke(&mut store, call.tool, call.args_json)
            .await;

        match result {
            Ok(Ok(json)) => Ok(json),
            Ok(Err(message)) => Err(WasmError::Guest(message)),
            Err(e) => Err(classify_trap(e, call.timeout)),
        }
    }
}

/// Find the [`Trap`] in the error (including the root cause — the component layer may
/// wrap a trap with context).
fn as_trap(e: &wasmtime::Error) -> Option<Trap> {
    e.downcast_ref::<Trap>()
        .copied()
        .or_else(|| e.root_cause().downcast_ref::<Trap>().copied())
}

/// Call-time error classification: epoch interruption = timeout, any other trap = the tool hangs.
fn classify_trap(e: wasmtime::Error, timeout: Duration) -> WasmError {
    match as_trap(&e) {
        Some(Trap::Interrupt) => WasmError::Timeout { timeout },
        Some(trap) => WasmError::Trap(trap.to_string()),
        None => WasmError::Host(e.to_string()),
    }
}

/// Instantiation-time errors (missing import / linker mismatch) likewise hang only the tool side.
fn classify_host_error(e: wasmtime::Error) -> WasmError {
    match as_trap(&e) {
        Some(trap) => WasmError::Trap(trap.to_string()),
        None => WasmError::Host(e.to_string()),
    }
}

// ── Host state and capability tightening ────────────────────────────────────

struct HostState {
    wasi: WasiCtx,
    table: ResourceTable,
    http: WasiHttpCtx,
    hooks: HostFilterHooks,
}

impl HostState {
    /// Assemble from pinned capabilities: fs only preopens pinned paths; the net whitelist
    /// goes into hooks; sockets are denied by default (no inherit_network).
    fn new(capabilities: &[ResolvedCapability]) -> Result<Self, WasmError> {
        let mut builder = WasiCtxBuilder::new();
        // The guest's println/panic goes to in-memory pipes — fixtures don't pollute test
        // output; a plugin tool's output goes through the invoke return value, not stdout.
        builder.stdout(MemoryOutputPipe::new(64 * 1024));
        builder.stderr(MemoryOutputPipe::new(16 * 1024));

        for cap in capabilities {
            if let ResolvedCapability::FsRead { paths } = cap {
                for path in paths {
                    // guest name = pinned absolute path: this is the name authorized in the model/manifest.
                    builder
                        .preopened_dir(path, path.as_str(), FsPerms::ReadOnly)
                        .map_err(|e| WasmError::Host(format!("preopen {path}: {e}")))?;
                }
            }
        }

        let mut allowed_hosts = Vec::new();
        for cap in capabilities {
            if let ResolvedCapability::NetHttp { hosts } = cap {
                allowed_hosts.extend(hosts.iter().cloned());
            }
        }

        Ok(Self {
            wasi: builder.build(),
            table: ResourceTable::new(),
            http: WasiHttpCtx::new(),
            hooks: HostFilterHooks { allowed_hosts },
        })
    }
}

impl WasiView for HostState {
    fn ctx(&mut self) -> WasiCtxView<'_> {
        WasiCtxView {
            ctx: &mut self.wasi,
            table: &mut self.table,
        }
    }
}

impl WasiHttpView for HostState {
    fn http(&mut self) -> WasiHttpCtxView<'_> {
        WasiHttpCtxView {
            hooks: &mut self.hooks,
            table: &mut self.table,
            ctx: &mut self.http,
        }
    }
}

/// net:http capability tightening: filter each request against the pinned host
/// whitelist; not on the whitelist = denied (fail-closed; an empty list denies everything).
struct HostFilterHooks {
    allowed_hosts: Vec<String>,
}

impl WasiHttpHooks for HostFilterHooks {
    fn send_request(
        &mut self,
        request: http::Request<WasiBody>,
        options: Option<RequestOptions>,
        fut: Box<dyn std::future::Future<Output = Result<(), HttpError>> + Send>,
    ) -> Box<
        dyn std::future::Future<
                Output = Result<
                    (
                        http::Response<WasiBody>,
                        Box<dyn std::future::Future<Output = Result<(), HttpError>> + Send>,
                    ),
                    HttpError,
                >,
            > + Send,
    > {
        let Some(host) = request.uri().host().map(str::to_ascii_lowercase) else {
            return Box::new(std::future::ready(Err(HttpError::InternalError(Some(
                "plugin net:http: request has no host".to_string(),
            )))));
        };
        if !host_allowed(&self.allowed_hosts, &host) {
            tracing::warn!(target: "plugin", host = %host, "net:http denied by capability whitelist");
            return Box::new(std::future::ready(Err(HttpError::HttpRequestDenied)));
        }
        // Allowed → go through the default implementation (real outbound).
        DefaultHooks.send_request(request, options, fut)
    }
}

/// Delegate to the default outbound implementation (the default `send_request`
/// method body is reused as-is).
struct DefaultHooks;

impl WasiHttpHooks for DefaultHooks {}

/// Host whitelist criterion (already normalized to lowercase / whitespace-stripped at
/// install time; an empty list denies everything).
///
/// Only compares the host itself (without port); exact match, no suffix wildcarding —
/// wildcards are a future product-layer thing; the secure default is tight first.
pub fn host_allowed(allowed_hosts: &[String], host: &str) -> bool {
    let host = host.trim().to_ascii_lowercase();
    !host.is_empty() && allowed_hosts.contains(&host)
}

// ── Tool table adapter (`plugin.{plugin}.{tool}`) ───────────────────────────

/// One row of the wasm tool table: in-process component + pinned capabilities + hard timeout.
///
/// Naming follows the same law as [`crate::script_tool::ScriptTool::with_key`]
/// (`Box::leak` follows the existing `McpToolAdapter` precedent). The execution surface
/// hangs tools, not the host: timeout/trap/capability denial all come back for this row
/// as [`AgentError`], while the component and runtime keep serving other tools.
pub struct WasmTool {
    name: &'static str,
    description: &'static str,
    schema: Value,
    runtime: Arc<WasmRuntime>,
    component: Component,
    /// the `tool` parameter of `executor.invoke` (the `name` of the wasm entry in the manifest).
    tool: String,
    /// pinned capability whitelist values (from the registry, not the plugin manifest).
    capabilities: Vec<ResolvedCapability>,
    timeout: Duration,
}

impl WasmTool {
    /// Build one tool table entry. `identity_key` = `plugin.{plugin}.{tool}`.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        identity_key: &str,
        description: &str,
        parameters: &BTreeMap<String, ParamSpec>,
        runtime: Arc<WasmRuntime>,
        component: Component,
        tool: &str,
        capabilities: Vec<ResolvedCapability>,
        timeout: Duration,
    ) -> Self {
        Self {
            name: Box::leak(identity_key.to_string().into_boxed_str()),
            description: Box::leak(description.to_string().into_boxed_str()),
            schema: parameters_envelope(parameters),
            runtime,
            component,
            tool: tool.to_string(),
            capabilities,
            timeout,
        }
    }

    async fn invoke(&self, args_json: &str) -> Result<String, WasmError> {
        self.runtime
            .invoke(
                &self.component,
                &WasmCall {
                    tool: &self.tool,
                    args_json,
                    capabilities: &self.capabilities,
                    timeout: self.timeout,
                },
            )
            .await
    }
}

fn wasm_error_to_agent(name: &str, e: WasmError) -> AgentError {
    match e {
        WasmError::Timeout { .. } => AgentError::ToolTimeout,
        other => AgentError::ToolExecution {
            name: name.to_string(),
            source: Box::new(AgentError::Internal(other.to_string())),
        },
    }
}

#[async_trait]
impl Tool for WasmTool {
    fn name(&self) -> &'static str {
        self.name
    }

    fn description(&self) -> &'static str {
        self.description
    }

    fn schema(&self) -> Value {
        self.schema.clone()
    }

    async fn call(&self, args: &Value, _ctx: &ToolContext) -> AgentResult<Vec<Content>> {
        let json = self
            .invoke(&args.to_string())
            .await
            .map_err(|e| wasm_error_to_agent(self.name, e))?;
        Ok(vec![Content::text(json)])
    }

    fn timeout_ms(&self) -> Option<u64> {
        Some(self.timeout.as_millis() as u64)
    }

    fn metadata(&self) -> ToolMetadata {
        ToolMetadata {
            name: self.name.to_string(),
            description: self.description.to_string(),
            origin: "plugin-wasm".to_string(),
            version: "unknown".to_string(),
            requirements: vec![],
        }
    }

    fn exposure(&self) -> ToolExposure {
        ToolExposure::Direct
    }
}

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

    fn fixture() -> std::path::PathBuf {
        std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/dispatcher.wasm")
    }

    fn runtime() -> WasmRuntime {
        WasmRuntime::new().expect("runtime")
    }

    async fn echo(rt: &WasmRuntime, component: &Component) -> String {
        rt.invoke(
            component,
            &WasmCall {
                tool: "echo",
                args_json: "{\"hello\":\"wasm\"}",
                capabilities: &[],
                timeout: Duration::from_secs(5),
            },
        )
        .await
        .expect("echo")
    }

    // ── Engine sharing (regression: macOS SIGABRT) ───────────────────────

    /// Repeated runtime construction must share one engine and one epoch
    /// ticker thread.
    ///
    /// wasmtime's macOS trap handler keeps its mach port in a process-global
    /// `static mut`: building a second Engine overwrites the port while the
    /// first handler thread still waits on it, and that thread aborts the
    /// process (SIGABRT). One shared engine means one handler thread, one
    /// port write, and one epoch ticker.
    #[test]
    fn repeated_runtimes_share_one_engine_and_one_ticker() {
        let first = WasmRuntime::new().expect("runtime");
        let second = WasmRuntime::new().expect("runtime");
        let third = WasmRuntime::new().expect("runtime");

        // Same engine instance across every runtime.
        assert!(
            Engine::same(&first.engine, &second.engine),
            "runtimes must share one wasmtime engine"
        );
        assert!(
            Engine::same(&second.engine, &third.engine),
            "runtimes must share one wasmtime engine"
        );

        // Building many runtimes must not start extra epoch tickers.
        let before = epoch_ticker_spawns();
        let _many: Vec<WasmRuntime> = (0..16)
            .map(|_| WasmRuntime::new().expect("runtime"))
            .collect();
        assert_eq!(
            before,
            epoch_ticker_spawns(),
            "constructing runtimes must not spawn extra epoch ticker threads"
        );
    }

    // ── Whitelist criterion (pure function, fast) ───────────────────────

    #[test]
    fn host_allowlist_is_fail_closed_exact_match() {
        let allow = vec!["api.example.com".to_string()];
        assert!(host_allowed(&allow, "api.example.com"));
        assert!(host_allowed(&allow, "API.Example.com"), "case normalized");
        assert!(!host_allowed(&allow, "evil.com"));
        assert!(!host_allowed(&allow, "example.com"), "no suffix wildcards");
        assert!(
            !host_allowed(&allow, "api.example.com.evil"),
            "no suffix wildcards"
        );
        assert!(
            !host_allowed(&[], "api.example.com"),
            "empty list denies all"
        );
        assert!(!host_allowed(&allow, ""), "empty host denied");
    }

    // ── net whitelist hook (the send_request surface) ────────────────────

    fn empty_body() -> WasiBody {
        use http_body_util::BodyExt;
        http_body_util::Empty::<bytes::Bytes>::new()
            .map_err(|never: std::convert::Infallible| match never {})
            .boxed_unsync()
    }

    fn ready_fut() -> Box<dyn std::future::Future<Output = Result<(), HttpError>> + Send> {
        Box::new(std::future::ready(Ok(())))
    }

    /// send_request returns `Box<dyn Future>` (the target is not Unpin, so it cannot be
    /// awaited directly): use `Box::into_pin` to get `Pin<Box<_>>` and await that. The err
    /// goes through Option (the Ok side is not Debug).
    async fn deny_reason(
        hooks: &mut HostFilterHooks,
        request: http::Request<WasiBody>,
    ) -> HttpError {
        Box::into_pin(hooks.send_request(request, None, ready_fut()))
            .await
            .err()
            .expect("expected rejection")
    }

    #[tokio::test]
    async fn net_whitelist_denies_unlisted_host_at_send_request() {
        let mut hooks = HostFilterHooks {
            allowed_hosts: vec!["api.example.com".to_string()],
        };
        let request = http::Request::builder()
            .uri("https://evil.example.net/x")
            .body(empty_body())
            .unwrap();
        let err = deny_reason(&mut hooks, request).await;
        assert!(
            matches!(err, HttpError::HttpRequestDenied),
            "deny must be HttpRequestDenied: {err:?}"
        );

        // An empty whitelist denies everything
        let mut hooks = HostFilterHooks {
            allowed_hosts: Vec::new(),
        };
        let request = http::Request::builder()
            .uri("https://api.example.com/x")
            .body(empty_body())
            .unwrap();
        let err = deny_reason(&mut hooks, request).await;
        assert!(matches!(err, HttpError::HttpRequestDenied), "{err:?}");

        // A request without a host is denied too
        let request = http::Request::builder()
            .uri("/relative-only")
            .body(empty_body())
            .unwrap();
        let err = deny_reason(&mut hooks, request).await;
        assert!(matches!(err, HttpError::InternalError(_)), "{err:?}");
    }

    #[tokio::test]
    async fn net_whitelist_passes_allowed_host_downstream() {
        // Allowed goes through the default outbound implementation: start a local server,
        // put 127.0.0.1 on the whitelist, and let the loopback go all the way through.
        let server = wiremock::MockServer::start().await;
        wiremock::Mock::given(wiremock::matchers::path("/ok"))
            .respond_with(wiremock::ResponseTemplate::new(200).set_body_string("fine"))
            .mount(&server)
            .await;

        let mut hooks = HostFilterHooks {
            allowed_hosts: vec!["127.0.0.1".to_string()],
        };
        let request = http::Request::builder()
            .uri(format!("http://127.0.0.1:{}/ok", server.address().port()))
            .body(empty_body())
            .unwrap();
        let (response, _io) = Box::into_pin(hooks.send_request(request, None, ready_fut()))
            .await
            .unwrap_or_else(|e| panic!("allowed host must reach the wire: {e:?}"));
        assert_eq!(response.status(), 200);
    }

    // ── Execution surface (real component; hangs tools, not the host) ────

    #[tokio::test]
    async fn echo_roundtrips_json() {
        let rt = runtime();
        let component = rt.compile(&fixture()).expect("compile fixture");
        assert_eq!(echo(&rt, &component).await, "{\"hello\":\"wasm\"}");
    }

    #[tokio::test]
    async fn fs_without_preopen_reads_nothing() {
        // Undeclared fs:read → no preopen → cannot read (tool errors, host lives).
        let rt = runtime();
        let component = rt.compile(&fixture()).expect("compile fixture");
        let err = rt
            .invoke(
                &component,
                &WasmCall {
                    tool: "read_file",
                    args_json: "/etc/passwd",
                    capabilities: &[],
                    timeout: Duration::from_secs(5),
                },
            )
            .await
            .expect_err("no preopen = no reads");
        assert!(matches!(err, WasmError::Guest(_)), "{err}");
        // The host keeps serving
        assert_eq!(echo(&rt, &component).await, "{\"hello\":\"wasm\"}");
    }

    #[tokio::test]
    async fn fs_preopen_grants_only_pinned_path() {
        let rt = runtime();
        let component = rt.compile(&fixture()).expect("compile fixture");

        let dir = tempfile::tempdir().unwrap();
        let pinned = dir.path().join("data");
        std::fs::create_dir(&pinned).unwrap();
        let file = pinned.join("ok.txt");
        std::fs::write(&file, "from-preopen").unwrap();

        let outside = dir.path().join("outside.txt");
        std::fs::write(&outside, "secret").unwrap();

        let caps = vec![ResolvedCapability::FsRead {
            paths: vec![pinned.to_string_lossy().into_owned()],
        }];
        let read = |path: std::path::PathBuf| {
            let caps = &caps;
            let rt = &rt;
            let component = &component;
            async move {
                let args = path.to_string_lossy().into_owned();
                rt.invoke(
                    component,
                    &WasmCall {
                        tool: "read_file",
                        args_json: &args,
                        capabilities: caps,
                        timeout: Duration::from_secs(5),
                    },
                )
                .await
            }
        };

        // Readable inside the pinned directory
        assert_eq!(read(file).await.expect("preopen read"), "from-preopen");
        // Cannot read outside the directory (the preopen boundary is the capability boundary)
        let err = read(outside).await.expect_err("outside preopen");
        assert!(matches!(err, WasmError::Guest(_)), "{err}");
    }

    #[tokio::test]
    async fn timeout_kills_call_not_host() {
        let rt = runtime();
        let component = rt.compile(&fixture()).expect("compile fixture");
        let err = rt
            .invoke(
                &component,
                &WasmCall {
                    tool: "spin",
                    args_json: "{}",
                    capabilities: &[],
                    timeout: Duration::from_millis(200),
                },
            )
            .await
            .expect_err("spin must time out");
        assert!(matches!(err, WasmError::Timeout { .. }), "{err}");
        // The tool hangs, not the host: the next call still works
        assert_eq!(echo(&rt, &component).await, "{\"hello\":\"wasm\"}");
    }

    #[tokio::test]
    async fn panic_traps_call_not_host() {
        let rt = runtime();
        let component = rt.compile(&fixture()).expect("compile fixture");
        let err = rt
            .invoke(
                &component,
                &WasmCall {
                    tool: "boom",
                    args_json: "{}",
                    capabilities: &[],
                    timeout: Duration::from_secs(5),
                },
            )
            .await
            .expect_err("boom must trap");
        assert!(matches!(err, WasmError::Trap(_)), "{err}");
        assert_eq!(echo(&rt, &component).await, "{\"hello\":\"wasm\"}");
    }

    #[tokio::test]
    async fn corrupt_wasm_fails_at_compile() {
        // Compile failure at load time → the error surface that disables this tool
        // (the loader does not skip the whole package).
        let rt = runtime();
        let dir = tempfile::tempdir().unwrap();
        let bogus = dir.path().join("bogus.wasm");
        std::fs::write(&bogus, b"not a wasm component").unwrap();
        let err = rt.compile(&bogus).expect_err("garbage must not compile");
        assert!(matches!(err, WasmError::Compile(_)), "{err}");
    }
}