Skip to main content

submilli_engine/runtime/
mod.rs

1//! Wasmtime engine configuration for Submilli.
2
3use crate::runtime::durable::RunId;
4pub mod agents;
5pub(crate) mod array_storage;
6pub mod blocking;
7pub mod call_log;
8pub mod decision;
9pub mod default_filesystem;
10pub mod disk_quota;
11pub mod durable;
12pub mod effects;
13mod effects_memory;
14pub mod embedding;
15pub mod exec;
16pub mod filesystem;
17pub mod fs;
18pub mod fuel;
19pub mod gc_singleton;
20pub mod host;
21pub mod intrinsic_types;
22pub mod json;
23pub(crate) mod json_text;
24pub mod limits;
25pub mod llm;
26pub mod mcp;
27pub mod metrics;
28pub mod number;
29pub mod prelude;
30pub mod secrets;
31pub mod security;
32pub mod session_finalizers;
33pub mod session_kv;
34pub mod skills;
35pub mod token_ledger;
36pub mod tools;
37pub mod vfs;
38pub mod watchdog;
39
40pub use agents::{
41    AGENTS_MODULE_NAME, AgentCallError, AgentInfo, AgentOutcome, AgentProvider, AgentRequest,
42    AgentUsage,
43};
44pub use call_log::{BodyCopy, CallOutcome, CallRecord, ModelUsage, PayloadRecord};
45pub use decision::{
46    CallOrigin, CallSite, CallTicket, ConsoleLine, DecisionAction, DecisionCause,
47    DecisionExplanation, DecisionLog, DecisionLogConfig, DecisionLogOutput, DecisionRecord,
48    DecisionRecorder, EntryPath, FailureReasonRecord, FailureRecord, NearMissRecord, PackageLine,
49    RecordObserver, RuleCitation, SourceLine,
50};
51pub use disk_quota::{DiskQuota, Holder, OpenFileGuard, QuotaCharge, QuotaExceeded};
52pub use embedding::{
53    DEFAULT_MAX_EMBEDDING_REQUESTS, DEFAULT_MAX_EXECUTION_EMBEDDING_TOKENS, EMBEDDING_MODULE_NAME,
54    EmbeddingBatch, EmbeddingBoundKind, EmbeddingError, EmbeddingFailureReason, EmbeddingLimitKind,
55    EmbeddingLimits, EmbeddingMalformedReason, EmbeddingModel, EmbeddingProvider,
56    EmbeddingShapeError, EmbeddingTokenBudget, Purpose, SubBatchSettlement,
57    estimate_embedding_tokens,
58};
59pub use exec::{RunResult, dispatch_main_async, instantiate_program_async};
60pub use host::{
61    INTERNAL_MODULE_NAME, NUMBER_MODULE_NAME, host_package_declarations,
62    install_async as install_runtime_async, install_async_for as install_runtime_async_for,
63    install_host_functions as install_runtime_host_functions,
64    install_host_functions_for as install_runtime_host_functions_for,
65    install_store_bound as install_runtime_store_bound, internal_host_package_declarations,
66    stdlib_package_declarations,
67};
68pub use json::JSON_MODULE_NAME;
69pub use limits::{
70    DEFAULT_MAX_STORE_BYTES, MemoryCapExceeded, MemoryExhausted, TenantLimits,
71    install_tenant_limits, is_memory_exhausted,
72};
73pub use llm::{
74    DEFAULT_MAX_ALL_EXECUTIONS_TOKENS, DEFAULT_MAX_EXECUTION_TOKENS, ExecutionTokenBudget,
75    FailureReason, LLM_MODULE_NAME, LlmCallError, LlmFailure, LlmLimitKind, LlmLimits, LlmModel,
76    LlmOutcome, LlmProvider, PromptBoundKind, SharedTokenBudget,
77};
78pub use mcp::{
79    MCP_MODULE_NAME, McpCallError, McpOutcome, McpResponse, McpTransport, install_mcp_async,
80    mcp_call_package_declaration,
81};
82pub use metrics::{HttpMetric, MetricsSink, NoopMetricsSink};
83pub use prelude::bigint::ops::BIGINT_MODULE_NAME;
84pub use prelude::temporal::shared::TEMPORAL_MODULE_NAME;
85pub use secrets::{EncryptedSecret, NoopSecretProvider, SecretKeyId, SecretProvider};
86pub use security::{AllowAllCheck, AuditDecision, CheckOutcome, SecurityCheck};
87pub use session_kv::{
88    CURSOR_KEY_ID_LEN, CursorKey, CursorKeys, InMemorySessionKv, SessionKvEntry, SessionKvError,
89    SessionKvFuture, SessionKvLimitKind, SessionKvLimits, SessionKvPage, SessionKvStore,
90    SessionKvUsage, entry_bytes, render_key, value_bytes,
91};
92pub use skills::{SKILLS_MODULE_NAME, Skill, SkillError, SkillInfo, SkillProvider};
93pub use tools::{
94    TOOLS_CAPABILITY, TOOLS_MODULE_NAME, TOOLS_PACKAGE, ToolCallError, ToolProvider,
95    install_tools_async, tools_call_package_declaration,
96};
97pub use vfs::{
98    Access, CopyDirError, MAX_MEASURED_DEPTH, MountError, MountSpec, Vfs, VfsMode, copy_host_dir,
99    copy_host_subdir, measure_dir, measure_host_dir, measure_host_dir_skipping_vanished,
100    measure_host_subdir_skipping_vanished, open_host_subdir, regular_files,
101};
102pub use watchdog::Watchdog;
103
104pub use crate::stdlib::http::{
105    AuthProxy, AuthProxyError, HttpClient, HttpError, HttpRequest, HttpResponse, NetworkPolicy,
106    NoopAuthProxy, ReqwestHttpClient,
107};
108
109use std::cell::RefCell;
110use std::io::Write;
111use std::sync::{Arc, Mutex};
112use std::time::Duration;
113
114use wasmtime::{
115    ArrayRef, AsContextMut, Config, Engine, Instance, Linker, Module, OptLevel, Rooted, Store,
116    WasmBacktraceDetails,
117};
118
119use crate::{PackageDeclaration, TypeInfoTable};
120
121/// Filesystem metadata surfaced to scripts via `submilli:fs.info()`: the
122/// active mode and the byte cap the VFS enforces, so the script — and the LLM —
123/// can branch on the sandbox shape. Never exposes the host path.
124#[derive(Debug, Clone)]
125pub struct VfsInfo {
126    pub mode: VfsMode,
127    pub size_limit: Option<u64>,
128}
129
130/// The JSON properties host-backed values show, by backing struct.
131pub(crate) type HostViews = Vec<(wasmtime::StructType, crate::stdlib::abi::HostProperties)>;
132
133pub struct StoreData {
134    #[cfg(test)]
135    pub(crate) array_growth: array_storage::GrowthStats,
136    /// After cancelling a host call, the execution owner drains this before
137    /// reusing or releasing the store. Blocking work may still be cleaning up.
138    pub blocking_work: blocking::BlockingWork,
139    pub git: Option<crate::stdlib::git::GitConfig>,
140    pub(crate) git_history: Option<Arc<crate::stdlib::git::log_cache::Cache>>,
141    pub console: Box<dyn Write + Send>,
142    pub vfs: Vfs,
143    pub filesystem: Arc<dyn filesystem::Filesystem>,
144    /// Native outcome history, installed before any guest initializer runs.
145    pub effects: Option<Arc<dyn effects::EffectHistory>>,
146    pub vfs_info: VfsInfo,
147    pub security_check: Arc<dyn SecurityCheck>,
148    pub fs_max_read_size: u64,
149    pub http_client: Arc<dyn HttpClient>,
150    pub http_max_response_size: u64,
151    /// Operator ceiling for a download, including streaming its body.
152    pub http_max_download_timeout_ms: u64,
153    pub auth_proxy: Arc<dyn AuthProxy>,
154    /// Immutable configuration resolved by the trusted session creator.
155    pub variables: Arc<std::collections::BTreeMap<String, String>>,
156    pub secret_provider: Arc<dyn SecretProvider>,
157    /// The outbound `@mcp/<server>` transport the `submilli:mcp.call` host fn
158    /// dispatches through, present when the embedder wires one. `None` in the
159    /// pure-interpreter path, where MCP calls throw "transport not configured".
160    pub mcp_transport: Option<Arc<dyn McpTransport>>,
161    /// The application tools `submilli:tools.call` (`@tools`) dispatches
162    /// through, present only when the embedder wires one. `None` makes every
163    /// call a catchable "no application tools" error.
164    pub tool_provider: Option<Arc<dyn ToolProvider>>,
165    /// Time this execution spent waiting outside the program on a host call
166    /// that must not count against its time limit (an application tool call).
167    /// An embedder's execution timeout subtracts it from the elapsed time.
168    pub clock_paused: std::time::Duration,
169    /// The model provider `submilli:llm` dispatches through, present only when
170    /// the embedder wires one. Left `None` the runtime has no model access at
171    /// all rather than silently completing against some default — the guest
172    /// surface reports that as a catchable configuration error
173    /// ([`LlmCallError::NotConfigured`]), the same rule `session_kv` follows.
174    pub llm_provider: Option<Arc<dyn LlmProvider>>,
175    /// This execution's token budget, reserving against its own ceiling and the
176    /// server-wide one together. `None` in the pure-interpreter path, where
177    /// there is no aggregate to protect and nothing to charge against; the
178    /// prompt-count and prompt-size bounds still apply there, because they
179    /// bound pathological shapes rather than spend. The embedder installs one
180    /// per execution, and dropping it is what returns the reservation.
181    pub llm_budget: Option<Arc<ExecutionTokenBudget>>,
182    /// The provider `submilli:embedding` dispatches through, present only when
183    /// the embedder wires one. `None` means the guest sees a catchable
184    /// [`EmbeddingError::NotConfigured`], never a silent default.
185    pub embedding_provider: Option<Arc<dyn EmbeddingProvider>>,
186    /// This execution's embedding budget (tokens and outbound requests). `None`
187    /// in the pure-interpreter path; dropping it returns unspent reservation.
188    pub embedding_budget: Option<Arc<EmbeddingTokenBudget>>,
189    /// Session-scoped key-value storage, present only when the embedder wires a
190    /// provider. Left `None` the store stays absent rather than silently
191    /// becoming per-execution scratch state that no later `execute` can read —
192    /// the guest surface reports that as a configuration error.
193    pub session_kv: Option<Arc<dyn SessionKvStore>>,
194    /// Durable package-owned resource cleanup supplied by the embedder.
195    pub session_finalizers: Option<Arc<dyn session_finalizers::SessionFinalizers>>,
196    pub finalizing: bool,
197    /// The keys `session.list` cursors are sealed with. `None` seals them with a
198    /// per-process key, so they stop working when the process restarts.
199    pub cursor_keys: Option<Arc<CursorKeys>>,
200    /// The harness `submilli:agents` runs sub-agents through, present only when
201    /// the embedder wires one. `None` makes every call a catchable
202    /// [`AgentCallError::NotConfigured`].
203    pub agent_provider: Option<Arc<dyn AgentProvider>>,
204    /// The harness `submilli:skills` reads skills through, present only when the
205    /// embedder wires one. `None` makes every call a catchable
206    /// [`SkillError::NotConfigured`].
207    pub skill_provider: Option<Arc<dyn SkillProvider>>,
208    /// Embedder sink for host-operation metrics (HTTP transport latencies).
209    /// Defaults to [`NoopMetricsSink`]; the server installs a Sentry-backed one.
210    pub metrics: Arc<dyn metrics::MetricsSink>,
211    pub tenant_limits: TenantLimits,
212    /// Fuel charged by host functions for their own work; the rest of the fuel
213    /// spent went to Wasm instructions. See [`fuel::charge_host_fuel`].
214    pub host_fuel: u64,
215    pub(crate) next_identity_hash: u64,
216    #[cfg(test)]
217    pub(crate) collection_index_reads: u64,
218    #[cfg(test)]
219    pub(crate) object_index_probes: u64,
220    /// Host charges not yet applied to the engine's fuel (see
221    /// [`fuel::HOST_FUEL_BATCH`]).
222    pub host_fuel_pending: u64,
223    /// The engine's fuel right after the last application of pending host
224    /// charges; `None` before the first.
225    pub host_fuel_applied_at: Option<u64>,
226    /// Test-segment labels recorded by `submilli:test.label`, in call order.
227    /// Only the test runner installs that host fn; an ordinary run leaves this
228    /// empty. The runner reads it after `main()` returns to attribute the
229    /// pass/fail outcome to the segment that was open at the time.
230    pub test_labels: RefCell<Vec<String>>,
231    /// Recovered runtime types + the prelude instance handle host functions use
232    /// to build *real* `$string`/`$Array` structs (vtable + payload) instead of
233    /// raw arrays. `None` until the prelude instantiates; set by
234    /// `install_prelude_async`. See [`crate::runtime::host::HostAbi`].
235    pub host_abi: Option<crate::runtime::host::HostAbi>,
236    /// This store's intrinsic types, built on first use; see
237    /// [`crate::runtime::intrinsic_types::intrinsic_types`].
238    pub(crate) intrinsic_types: Option<Arc<crate::runtime::intrinsic_types::IntrinsicTypes>>,
239    /// The JSON properties of each host-backed value's backing struct, built on
240    /// first use; see [`crate::stdlib::abi::json_properties`].
241    pub(crate) host_views: Option<HostViews>,
242    /// An immutable global keeps the store's undefined value rooted across calls.
243    pub(crate) undefined_value: Option<wasmtime::Global>,
244    /// The bound-receiver closure environment type, built on first use for the
245    /// same reason as [`Self::intrinsic_types`].
246    pub(crate) closure_receiver_type: Option<wasmtime::StructType>,
247    /// The call-metadata closure environment type, built on first use for the
248    /// same reason as [`Self::intrinsic_types`].
249    pub(crate) call_metadata_type: Option<wasmtime::StructType>,
250    pub(crate) iterator_functions: [Option<wasmtime::Func>; 13],
251    pub(crate) iterator_constants: [Option<wasmtime::Global>; 6],
252    /// The object each static-dispatch binding is as a value, by its string
253    /// form; see `prelude::object::static_value`.
254    pub(crate) static_values: std::collections::BTreeMap<Vec<u16>, wasmtime::Global>,
255    pub(crate) parameter_cache: prelude::arguments::ParameterCache,
256    pub(crate) regex_input: Option<prelude::regex::input::InputCache>,
257    /// Runtime type metadata keyed by package name.
258    pub type_info: std::collections::BTreeMap<String, TypeInfoTable>,
259    /// Depth of the in-flight universal-vtable walk; see
260    /// [`MAX_VTABLE_WALK_DEPTH`].
261    pub vtable_walk_depth: u32,
262    /// Hook entries in the current outer structural walk, including repeated
263    /// visits to shared children. Reset only when the outer walk finishes.
264    pub(crate) vtable_walk_nodes: u32,
265    /// Host-only result marshalling after an effect must not refuse for fuel.
266    pub(crate) settling_host_result: bool,
267    /// Denials the runtime threw, by thrown object; see [`host::ThrownDenials`].
268    pub(crate) thrown_denials: host::ThrownDenials,
269}
270
271/// The nesting the universal-vtable walk allows before it reports a runaway.
272///
273/// Pinned to `serde_json`'s own recursion limit, which is what bounds
274/// `JSON.parse`: a document the runtime is willing to parse must still be
275/// comparable and re-serializable, or `JSON.parse` would accept graphs that
276/// `JSON.stringify` then refuses. Measured headroom: a debug build on a 2 MB
277/// test-harness thread aborts between 160 and 200 levels, so the bound sits
278/// below the point where the native stack runs out.
279pub(crate) const MAX_VTABLE_WALK_DEPTH: u32 = 128;
280
281/// Bounds shared-substructure expansion independently of available fuel.
282pub(crate) const MAX_STRUCTURAL_WALK_NODES: u32 = 100_000;
283
284pub const DEFAULT_FS_MAX_READ_SIZE: u64 = 50 * 1024 * 1024;
285
286pub const DEFAULT_HTTP_MAX_RESPONSE_SIZE: u64 = 50 * 1024 * 1024;
287
288#[derive(Clone, Copy)]
289pub struct LinkedPackageModule<'a> {
290    pub module: &'a Module,
291    pub declaration: &'a PackageDeclaration,
292    pub type_info: &'a TypeInfoTable,
293}
294
295impl StoreData {
296    pub fn with_vfs(run_id: RunId, vfs: Vfs) -> Self {
297        Self::with_vfs_and_cap(run_id, vfs, DEFAULT_MAX_STORE_BYTES)
298    }
299
300    pub fn with_vfs_and_cap(run_id: RunId, vfs: Vfs, max_store_bytes: u64) -> Self {
301        let vfs_info = VfsInfo {
302            mode: vfs.mode(),
303            size_limit: None,
304        };
305        Self {
306            #[cfg(test)]
307            array_growth: array_storage::GrowthStats::default(),
308            console: Box::new(std::io::stderr()),
309            filesystem: Arc::new(default_filesystem::DefaultFilesystem::new(
310                vfs.clone(),
311                run_id,
312            )),
313            effects: None,
314            vfs,
315            vfs_info,
316            security_check: security::default_check(),
317            git: None,
318            git_history: None,
319            blocking_work: blocking::BlockingWork::default(),
320            fs_max_read_size: DEFAULT_FS_MAX_READ_SIZE,
321            http_client: crate::stdlib::http::default_http_client(),
322            http_max_response_size: DEFAULT_HTTP_MAX_RESPONSE_SIZE,
323            http_max_download_timeout_ms: 60_000,
324            auth_proxy: crate::stdlib::http::default_auth_proxy(),
325            variables: Arc::default(),
326            secret_provider: Arc::new(secrets::NoopSecretProvider),
327            mcp_transport: None,
328            tool_provider: None,
329            clock_paused: std::time::Duration::ZERO,
330            llm_provider: None,
331            llm_budget: None,
332            embedding_provider: None,
333            embedding_budget: None,
334            session_kv: None,
335            session_finalizers: None,
336            finalizing: false,
337            cursor_keys: None,
338            agent_provider: None,
339            skill_provider: None,
340            metrics: Arc::new(metrics::NoopMetricsSink),
341            tenant_limits: TenantLimits::new(max_store_bytes),
342            host_fuel: 0,
343            #[cfg(test)]
344            collection_index_reads: 0,
345            #[cfg(test)]
346            object_index_probes: 0,
347            next_identity_hash: 0,
348            host_fuel_pending: 0,
349            host_fuel_applied_at: None,
350            test_labels: RefCell::new(Vec::new()),
351            host_abi: None,
352            intrinsic_types: None,
353            host_views: None,
354            undefined_value: None,
355            closure_receiver_type: None,
356            call_metadata_type: None,
357            iterator_functions: [None; 13],
358            iterator_constants: [None; 6],
359            static_values: std::collections::BTreeMap::new(),
360            parameter_cache: Default::default(),
361            regex_input: None,
362            type_info: std::collections::BTreeMap::new(),
363            vtable_walk_depth: 0,
364            vtable_walk_nodes: 0,
365            settling_host_result: false,
366            thrown_denials: host::ThrownDenials::default(),
367        }
368    }
369
370    pub fn install_type_info(&mut self, table: TypeInfoTable) {
371        self.type_info.insert(table.package_name.clone(), table);
372    }
373
374    pub fn with_tempdir() -> std::io::Result<Self> {
375        Ok(Self::with_vfs(RunId::generate()?, Vfs::tempdir()?))
376    }
377}
378
379/// Instantiates each linked package module, running its module-level initializers.
380///
381/// Every thread polling this future must meet
382/// [`RuntimeConfig::native_stack_size`]'s caller contract.
383pub async fn install_package_modules_async(
384    linker: &mut Linker<StoreData>,
385    store: &mut Store<StoreData>,
386    packages: &[LinkedPackageModule<'_>],
387) -> wasmtime::Result<()> {
388    for package in packages {
389        let name = package.declaration.package_name.as_str();
390        if store.data().git.is_none()
391            && package
392                .module
393                .imports()
394                .any(|import| import.module() == "submilli:git")
395        {
396            wasmtime::bail!(
397                "package `{name}` imports submilli:git; configure the blueprint git block"
398            );
399        }
400        // The module's declared name *is* its principal at every gated call, so bind it to the
401        // name the package is being linked under. Without this, a prebuilt `pkg.wasm` naming
402        // itself `main` — or naming another package — would be granted that principal's
403        // permissions and, for `main`, the operator's injected credentials. The bytes are not
404        // necessarily ones this compiler produced: `submilli run` and the server both
405        // instantiate artifacts straight from the package store.
406        match package.module.name() {
407            Some(declared) if declared == name => {}
408            Some(declared) => wasmtime::bail!(
409                "package `{name}`: its module declares the name `{declared}`, which would give \
410                 it that principal's permissions. Rebuild the package."
411            ),
412            None => wasmtime::bail!(
413                "package `{name}`: its module declares no name, so its gated calls cannot be \
414                 attributed. Rebuild the package."
415            ),
416        }
417        store
418            .data_mut()
419            .install_type_info(package.type_info.clone());
420        // Instantiation runs the package's module-level initializers. They are the
421        // package's own code, executing in the package's own module, so identity read
422        // off the running frame names them without any bracketing here.
423        let instance = {
424            let outcome = linker.instantiate_async(&mut *store, package.module).await;
425            name_the_failing_initializer(&mut *store, name, outcome)?
426        };
427        linker.instance(&mut *store, name, instance)?;
428    }
429    Ok(())
430}
431
432/// Initializers run inside the Wasm start function, so anything they throw —
433/// a denial most of all — escapes instantiation as the engine's opaque
434/// `ThrownException`, whose Display is "wasm exception thrown". The thrown
435/// value is still on the store, so recover its text the way an uncaught throw
436/// from `main` is recovered and say which package it came from. Without this an
437/// operator who granted a capability under `main:` rather than the package's own
438/// block — the mistake this attribution makes easy — gets no package, no
439/// capability, and no reason.
440fn name_the_failing_initializer(
441    store: &mut Store<StoreData>,
442    package: &str,
443    outcome: wasmtime::Result<Instance>,
444) -> wasmtime::Result<Instance> {
445    let err = match outcome {
446        Ok(instance) => return Ok(instance),
447        Err(err) => err,
448    };
449    let recovered = exec::uncaught_error(store, err);
450    let Some(thrown) = recovered.downcast_ref::<crate::backtrace::ThrownError>() else {
451        return Err(recovered);
452    };
453    Err(wasmtime::Error::new(crate::backtrace::ThrownError {
454        message: format!(
455            "package `{package}` failed to initialize: {}",
456            thrown.message
457        ),
458        backtrace: thrown.backtrace.clone(),
459        denial: thrown.denial.clone(),
460    }))
461}
462
463/// Native stack bytes per byte of `max_wasm_stack`; see
464/// [`RuntimeConfig::native_stack_size`].
465const NATIVE_STACK_PER_WASM_BYTE: usize = 32;
466const MIN_NATIVE_STACK: usize = 8 * 1024 * 1024;
467
468#[derive(Clone, Debug)]
469pub struct RuntimeConfig {
470    pub fuel: u64,
471    pub max_wasm_stack: usize,
472    pub memory_reservation: u64,
473    pub memory_guard_size: u64,
474    pub memory_reservation_for_growth: u64,
475    pub max_store_bytes: u64,
476    pub timeout: Option<Duration>,
477    pub async_yield_fuel: Option<u64>,
478}
479
480impl Default for RuntimeConfig {
481    fn default() -> Self {
482        Self {
483            // Temporarily very high: large structural JSON.stringify and other
484            // per-code-unit Wasm work still burns fuel, and exhausting it mid-run
485            // is a worse failure than the loose runaway-loop bound this gives up.
486            // The real CPU cap belongs in a wall-clock timeout; revisit once the
487            // hot encoding paths are off the meter.
488            fuel: 1_000_000_000_000,
489            max_wasm_stack: 512 * 1024,
490            memory_reservation: 3 * 1024 * 1024,
491            memory_guard_size: 64 * 1024,
492            memory_reservation_for_growth: 16 * 1024 * 1024,
493            max_store_bytes: DEFAULT_MAX_STORE_BYTES,
494            timeout: None,
495            // 10K fuel ≈ ~10K wasm instructions. At the default fuel budget a
496            // CPU-bound full-budget call yields thousands of times before
497            // exhaustion — fine-grained enough for fair scheduling, coarse enough
498            // that yield overhead stays negligible.
499            async_yield_fuel: Some(10_000),
500        }
501    }
502}
503
504impl RuntimeConfig {
505    /// The native stack a thread running programs under this config needs.
506    ///
507    /// `max_wasm_stack` is an interpreter budget, but a host call that re-enters
508    /// Wasm (a callback passed to `map`, say) nests interpreter frames on the
509    /// thread's own stack. The engine charges each crossing 4 KiB of the budget;
510    /// this bounds re-entry depth, not the thread's available native stack. Debug
511    /// builds have measured about 64 KiB per crossing, so this reservation allows
512    /// 128 KiB per crossing, with an 8 MiB minimum for setup and shallow calls.
513    ///
514    /// Embedders must provide this stack on every thread that polls execution,
515    /// including after an async task migrates between workers. The runners do not
516    /// create a thread or inspect remaining stack space. Poll them near the root
517    /// of the thread, rather than underneath deep native recursion. Undersized
518    /// stacks can overflow and abort the process instead of returning a trap.
519    /// Custom host functions must also bound their own native recursion.
520    ///
521    /// Set a Tokio runtime's `thread_stack_size` for worker threads. Its
522    /// `block_on` caller runs on the calling thread, so size that thread separately
523    /// (also for a current-thread runtime or `pollster::block_on`). Compilation
524    /// separately requires [`crate::compiler_limits::COMPILER_STACK_BYTES`]; use
525    /// the larger requirement when a thread performs both operations.
526    pub fn native_stack_size(&self) -> usize {
527        self.max_wasm_stack
528            .saturating_mul(NATIVE_STACK_PER_WASM_BYTE)
529            .max(MIN_NATIVE_STACK)
530    }
531
532    pub fn engine(&self) -> wasmtime::Result<Engine> {
533        Engine::new(&self.wasmtime_config())
534    }
535
536    /// Same as [`engine`](Self::engine). `Config::async_support` is a no-op in
537    /// wasmtime 44+; this entry point exists so async embedders have a distinct
538    /// call site to evolve independently.
539    pub fn engine_async(&self) -> wasmtime::Result<Engine> {
540        Engine::new(&self.wasmtime_config())
541    }
542
543    pub fn wasmtime_config(&self) -> Config {
544        let mut config = Config::new();
545        // Winch lacks `wasm_gc` + `wasm_function_references`; Pulley is ~10× slower.
546        // Strategy::Auto → Cranelift JIT.
547        //
548        // No optimization passes: the per-request user script is compiled fresh by
549        // `Module::new` (it can't be AOT-cached — it's LLM-generated), and for
550        // short-lived scripts that compile cost dominates the run. The trade is
551        // global, though: precompiled prelude/stdlib share this engine, so they run
552        // unoptimized too. TODO: precompile stdlib + curated packages on a separate
553        // high-opt engine and `deserialize` the optimized artifacts here, so only the
554        // user script pays the unoptimized-codegen tax.
555        config.cranelift_opt_level(OptLevel::None);
556        config.consume_fuel(true);
557        config.max_wasm_stack(self.max_wasm_stack);
558
559        config.epoch_interruption(true);
560
561        config.memory_reservation(self.memory_reservation);
562        config.memory_guard_size(self.memory_guard_size);
563        config.memory_reservation_for_growth(self.memory_reservation_for_growth);
564        config.memory_may_move(true);
565        config.memory_init_cow(true);
566
567        // The engine grants this reservation without consulting the store limiter.
568        // Start at zero so every GC byte is charged to the tenant's aggregate cap,
569        // including stores whose cap differs from this engine's RuntimeConfig.
570        config.gc_heap_reservation(0);
571        config.gc_heap_guard_size(self.memory_guard_size);
572        config.gc_heap_reservation_for_growth(self.memory_reservation_for_growth);
573        config.gc_heap_may_move(true);
574
575        config.wasm_gc(true);
576        config.wasm_function_references(true);
577        config.wasm_exceptions(true);
578
579        config.wasm_backtrace_details(WasmBacktraceDetails::Enable);
580
581        config
582    }
583
584    pub fn store<T>(&self, engine: &Engine, data: T) -> wasmtime::Result<Store<T>> {
585        let mut store = Store::new(engine, data);
586        store.set_fuel(self.fuel)?;
587        // With epoch_interruption(true), an unset deadline traps immediately.
588        // Use 1 for watchdog-tripped timeouts, MAX to effectively disable.
589        let delta = if self.timeout.is_some() { 1 } else { u64::MAX };
590        store.set_epoch_deadline(delta);
591        store.epoch_deadline_trap();
592        Ok(store)
593    }
594
595    pub fn store_async<T>(&self, engine: &Engine, data: T) -> wasmtime::Result<Store<T>> {
596        let mut store = self.store(engine, data)?;
597        store.fuel_async_yield_interval(self.async_yield_fuel)?;
598        Ok(store)
599    }
600
601    /// Arm the configured timeout, failing setup if its thread cannot be created.
602    /// Keep the returned guard alive until guest execution finishes.
603    pub fn arm_timeout(&self, engine: &Engine) -> wasmtime::Result<Option<Watchdog>> {
604        self.timeout
605            .map(|duration| watchdog::arm(engine, duration))
606            .transpose()
607            .map_err(|error| {
608                wasmtime::Error::from(error).context("starting execution timeout watchdog")
609            })
610    }
611
612    /// One-shot runner: load compiled Wasm, instantiate, call `main`, return typed result
613    /// and captured console. Async like the rest of the runtime — a caller that
614    /// isn't on a runtime bridges it itself (`pollster::block_on` for
615    /// compute/`fs`-only programs, a tokio runtime when `http` is involved). For
616    /// live console streaming, build a custom `Store<StoreData>` and call
617    /// [`dispatch_main_async`] directly.
618    ///
619    /// # Native stack
620    ///
621    /// Every thread polling this future must meet [`Self::native_stack_size`]'s
622    /// caller contract. This method does not provision a stack. Compilation
623    /// separately requires [`crate::compiler_limits::COMPILER_STACK_BYTES`].
624    pub async fn run(&self, wasm_bytes: &[u8]) -> wasmtime::Result<RunResult> {
625        self.run_with_type_info(wasm_bytes, None).await
626    }
627
628    /// Runs a compiled script with its result type information and captured console.
629    ///
630    /// # Native stack
631    ///
632    /// Every thread polling this future must meet [`Self::native_stack_size`]'s
633    /// caller contract, just as for [`Self::run`]. Compiling the script separately
634    /// requires [`crate::compiler_limits::COMPILER_STACK_BYTES`].
635    pub async fn run_compiled(
636        &self,
637        compiled: &crate::compile::CompiledScript,
638    ) -> wasmtime::Result<RunResult> {
639        self.run_with_type_info(&compiled.wasm, Some(compiled.type_info.clone()))
640            .await
641    }
642
643    async fn run_with_type_info(
644        &self,
645        wasm_bytes: &[u8],
646        type_info: Option<TypeInfoTable>,
647    ) -> wasmtime::Result<RunResult> {
648        let buf = Arc::new(Mutex::new(Vec::new()));
649        let mut data =
650            StoreData::with_vfs_and_cap(RunId::generate()?, Vfs::tempdir()?, self.max_store_bytes);
651        data.console = Box::new(ConsoleSink(Arc::clone(&buf)));
652        if let Some(type_info) = type_info {
653            data.install_type_info(type_info);
654        }
655        let engine = self.engine()?;
656        let mut store = self.store_async(&engine, data)?;
657        install_tenant_limits(&mut store);
658        let module = Module::new(&engine, wasm_bytes)?;
659        let mut linker = Linker::<StoreData>::new(&engine);
660        install_runtime_async(&mut linker, &mut store).await?;
661        let _watchdog = self.arm_timeout(&engine)?;
662        let inst = instantiate_program_async(&linker, &mut store, &module).await?;
663        let value = dispatch_main_async(&mut store, &inst).await?;
664        // A poisoned ConsoleSink may contain output from an interrupted write.
665        // AGENTS.md permits this poisoned-lock panic instead of recovering partial
666        // output; it does not permit the panic that caused poisoning.
667        let captured = buf.lock().expect("console buffer lock poisoned").clone();
668        let console = String::from_utf8(captured)
669            .map_err(|e| wasmtime::Error::msg(format!("console output not utf-8: {e}")))?;
670        Ok(RunResult { value, console })
671    }
672}
673
674// A panic during writing may leave partial console output. AGENTS.md permits
675// panicking on poisoned access instead of recovering it; the panic that caused
676// poisoning is still subject to the no-panic policy.
677struct ConsoleSink(Arc<Mutex<Vec<u8>>>);
678
679impl Write for ConsoleSink {
680    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
681        self.0
682            .lock()
683            .expect("console buffer lock poisoned")
684            .write(buf)
685    }
686
687    fn flush(&mut self) -> std::io::Result<()> {
688        Ok(())
689    }
690}
691
692/// Decode a Submilli `(ref $string)` (packed UTF-16) into a Rust `String`.
693pub(crate) fn read_submilli_string(
694    mut ctx: impl AsContextMut<Data = StoreData>,
695    msg: Rooted<ArrayRef>,
696) -> wasmtime::Result<String> {
697    let units = host::read_code_units(&mut ctx, msg, "string")?;
698    fuel::charge(ctx, fuel::SCAN, units.len() as u64)?;
699    Ok(String::from_utf16_lossy(&units))
700}