xmachines-runtime-wasi 6.0.0

WASI 0.2 component that runs XState v6 machine definitions, with guards and actions from the host
Documentation

xmachines-runtime-wasi

A WebAssembly component (WIT, the component model of WASI 0.2) that runs XState v6 machine definitions with xmachines-runtime, and the library whose export_runtime! exports that runtime from another component.

When to use it

Use the component to run machines in any host of the WebAssembly component model (Wasmtime, jco in JavaScript, or a runtime in another language) when the machine definitions arrive at run time and their guards and actions live in the host. The component is generic: it loads any MachineJSON at run time. Custom guards, actions, named delays, expressions and the evaluators of code nodes in other languages (evaluate) are imports the host implements; child machines for invoke come with the load options, any other invoke source is invoked work the host settles with a work input, and the load option hostActions leaves custom actions to the host as work too.

For one machine whose implementations are compiled in, with no host call per guard, use a typed component of xmachines-machine-wasi instead.

Building the component

rustup target add wasm32-unknown-unknown
cargo build -p xmachines-runtime-wasi --target wasm32-unknown-unknown --release
wasm-tools component new target/wasm32-unknown-unknown/release/xmachines_runtime_wasi.wasm -o xmachines_runtime_wasi.wasm
# about 1.3 MB

The module of wasm32-unknown-unknown imports nothing of the platform, since std there has no clock, randomness or stdio, and wasm-tools component new makes it a component of the world its bindings embed: it imports implementations and exports actors, and no WASI interface, so a host passes nothing else: a JavaScript host needs no WASI shim (preview2-shim), and Wasmtime no WASI context for it. A build for wasm32-wasip2 is the same component with the WASI imports of std (stdio, environment, exit, a clock and a random seed), which a host must then provide.

Debugging a panic

A panic of the runtime traps, and the call fails with no message: the component has no stderr to write it to. The trap's Wasm backtrace names the Rust functions it passed through, from the component's name section, which the release profile keeps (strip = "debuginfo", never symbols); Wasmtime prints that backtrace with the error. For the message itself, build the component for wasm32-wasip2 (cargo build -p xmachines-runtime-wasi --target wasm32-wasip2 --release, a component as it is) and run the same calls in a host that gives it WASI with stderr inherited, such as an xmachines-wasi-host Host whose WasiCtx is WasiCtx::builder().inherit_stderr().build(): std writes the panic message there before it traps.

The component is this crate exporting itself with export_runtime!, behind its default component feature.

Each release of this crate publishes the built component and its WIT package to the generic package registry of xmachines-rs, as the package runtime-wasi at the version of the release (xmachines_runtime_wasi.wasm and wit.tar.gz). For JavaScript, xmachines-js publishes them to npm as @xmachines/runtime-wasi.

Exporting the runtime from another component

Another component can export the runtime next to exports of its own: it depends on this crate with default-features = false (the component feature exports the runtime from this crate's own cdylib, which would clash) and calls export_runtime! with a type of its own. Its own wit_bindgen::generate! world and export! stay as they are; the component gets both worlds' imports and exports.

struct Runtime;
xmachines_runtime_wasi::export_runtime!(Runtime);

The component-model exports are compiled only for wasm32; a native build type-checks the crate.

Contract

The world xmachines:runtime/runtime of the WIT package xmachines:runtime@0.4.0 (wit/runtime.wit):

  • it exports actors, the workflow contract: the machine resource (load compiles a definition once, with its load options children, maxSteps, maxTimers, observeSystemEmitted and hostActions; start(options, now) and restore(snapshot) make actors of it and never compile again; each error is the contract's { "code", "message" }: WORKFLOW_INVALID_MACHINE or WORKFLOW_INVALID_INPUT of a load, WORKFLOW_MACHINE_FAILED of a start whose root fails and WORKFLOW_INVALID_INPUT of start options that do not parse or a now past 2^53 - 1, WORKFLOW_INVALID_SNAPSHOT of a restore, and no machine or actor) and the actor resource (apply a chunk of inputs, giving the step or the contract's error of a refused chunk, a rejection { "rejected": { "message", "data"? } } after which the run goes on or a fault { "code", "message" }, a time or work input that fails ending the run in error; can, XState's snapshot.can: whether the root would select a transition for the event, an empty one among them; persist, take-logs, take-system-emitted and stop);
  • it imports implementations: guard, action, delay, expr, context-expr and evaluate.

Values cross as JSON text, because WIT has no maps, recursive types or unknown, which contexts and events use freely. The JSON is specified by JSON Schemas (draft 2020-12) in wit/schemas/, each with its $id at its published place, and the WIT docs name the schema of each string: the definition envelope, load and start options, chunks, steps, work and apply errors, events, the persisted system (XState's SystemSnapshot), system emitted events, logs, effects, and what the implementations and evaluate imports receive and return. XState's own structures are left open; the schemas describe the envelope the runtime relies on. Tests hold the code to them both ways: what the components take and return validates, and what they reject the schemas reject.

The runtime never reads a clock: time is an input of a chunk, { "kind": "time", "now" }. An empty chunk, [], gives the current step: how a host reads the opening state after start or restore. Host calls happen while the actor is borrowed, so the host must not call back into the same actor from a guard or action.

Runs: the record protocol

The component also exports xmachines:workflow/runs@0.1.0 (wit/deps/workflow/workflow.wit): the stepper of the workflow contract, xmachines-workflow's Run, across the boundary. A machine loads and compiles a definition once, with the load options of actors; open(options) opens a run of it (workflow-options.schema.json: actor, time, input, checkpoint, maxTimers, maxSteps, from) and gives its opening records; a run's apply(chunk) applies one chunk with one call and gives its records as JSON text (record.schema.json), or its fault (fault.schema.json); can(event) and checkpoint() complete it. A host passes records it journals or forwards without parsing them. export_runtime! exports both interfaces.

Pins

The component model of WASI 0.2, built from wasm32-unknown-unknown, and wit-bindgen 0.63. WASI 0.3 async is not used yet; invoked work is settled by work inputs of apply.

Related crates

License

MIT