Skip to main content

submilli_engine/runtime/
mod.rs

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