Skip to content

ariadne: WebAssembly guests natively on wasmtime (G1, first part) - #1093

Merged
transfix merged 3 commits into
masterfrom
feat/wasm-guests
Nov 9, 2026
Merged

transfix merged 3 commits into
masterfrom
feat/wasm-guests

Conversation

@transfix

@transfix transfix commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Stacked on #1091 (base feat/script-engine). Once #1091 merges, this branch is rebased onto master and retargeted there, and CI runs then. Its recipe CI is cancelled here so the queue isn't held up twice.

G1, first part: WebAssembly guests natively on wasmtime, as a ScriptEngine on #1091's interface (docs/roadmap/ARIADNE_SCRIPTING_HOSTS.md §8.2–8.7; the user-facing reference is ARIADNE.md §9.6).

requires: [wasm]
wasm:
  module: scorer.wasm            # or a .wat beside the document
  export: [score_route, {name: rank, as: rank-routes}]
  hooks: {on_tick: tick, on: {replan: replan}, watch: {route: route_changed}}
  imports: [state.read, call]
  limits: {memory_mb: 32}

What's in it

  • src/ariadne_wasm/
    • guest_runtime.h: §8.3's runtime interface. It compiles a module, instantiates it with the imports, starts an export call, and polls it a slice of fuel at a time, pausing in an import that waits.
    • wasmtime_runtime.cpp: the wasmtime backend. Each slice is one wasmtime_call_future_poll on the guest's own stack, under wasmtime_context_fuel_async_yield_interval. A waiting import is an async host function whose continuation reports when the host has answered. The store's limiter caps memory.
    • value_cbor.{h,cpp}: DSL values as CBOR. A closure, a native function or a host object doesn't cross (host objects are §8.4.1's handles, later).
    • wasm_engine.{h,cpp}: the engine.
      • The wasm: block, and an instance per document, rebuilt at a reload. _initialize runs at instantiation.
      • The guest ABI (§8.4, as built): an export takes (ptr, len) of its CBOR argument list and returns ptr << 32 | len of its result. The guest exports memory and ari_alloc.
      • The imports, all in module ari: call(name, args) -> i64, reply(dst), yield() and fail(text).
  • Exports are functions of the document through the new ScriptLane::export_activation. A call is an activation the caller parks on:
    • It is an async function whose async_work.start begins the activation, which settles the call when it ends. So no pool thread is used, and the same path will work in single-threaded wasm.
    • The parked time counts against the caller's caps.
    • -async returns a future.
    • A failure re-raises with its own class: ActivationOutcome now keeps a failed step's exception.
    • Under it is AppRuntime::register_async_work_function.
  • Limits.
    • A slice's fuel is charged as DSL steps (ariadne.limits.wasm_gas_per_step, 1000), so the action caps bound a guest.
    • The guests share the wasm frame budget.
    • An instance runs one activation at a time; the others wait a frame at a time.
    • A guest calling its own export is refused, since it would wait on itself.
    • imports: narrows the call import by group (state.read, state.write, msg, net, call).
  • WasmError joins the built-in classes beside PythonError (18 now; two tests pinned the count, and the docs are updated). The wasm token, and the loader's warning for a wasm: block on a host without guests, are added too.
  • Hosts.
    • arix and ariglx carry the engine where wasmtime's C API is found. -DARI_WITH_WASM / -DARIGL_WITH_WASM make it required.
    • On Windows, with wasmtime.dll's import library, the DLL is copied beside the host and installed with it.
    • CI's package-linux installs the cvcpkg wasmtime package, a prebuilt download, so ariadne_wasm_test runs there.
  • Not yet (§8.3 and the G1 row list it):
    • in-program sliced calls, and calls from init: (which can't park, so they raise RuntimeError today);
    • a pump budget that counts slice steps;
    • the lock manager in place of the frame-at-a-time wait for an instance;
    • key, pointer, signal and message hooks;
    • MemoryError at a refused memory.grow;
    • the dedicated state imports, and handles;
    • ari_init, and a host option for state_exec.policy;
    • wasmtime in the arix / ariglx cvcpkg recipes.

Tests

Local runs, Windows, MSVC 14.44, static Release core tree plus arix, with wasmtime 45.0.0+cvc.1 from cvcpkg:

  • ariadne_wasm_test, 11/11 (×5 repeats), with a WAT guest:
    • the CBOR round trip and its refusals;
    • exports and an -async future, and _initialize;
    • calls through the lane under the chroot, and imports: narrowing;
    • fuel as steps: it finishes with no cap, and max_steps_exceeded stops it at 5000;
    • a yield; traps and fail as WasmError; a refused re-entry;
    • one activation at a time; an on_tick hook;
    • the block's load errors.
  • ariadne_script_engine_test 19/19 (the new export_activation case: park, -async, the class kept, init: refused). ariadne_requires_test 20/20.
  • Core suites: state_exec_exceptions_test 25/25, state_exec_data_steps_test 54/54, state_exec_codec_test 73/73, ariadne_runtime_test 262/262.
  • ctests: arix_wasm_block (arix test with 5 cases) and ari_wasm_needs_arix pass.
  • ariglx: its edited main.cpp passes a syntax-only compile against the GL tree with CVC_ARIADNE_WASM on. ariglx itself wasn't built here, and the GL CI jobs don't install wasmtime yet.
  • clang-format 18.1.8 is clean on the changed lines.

The seam G1 (WebAssembly guests on wasmtime) and L1 (Lua) plug into beside
Python (docs/roadmap/ARIADNE_SCRIPTING_HOSTS.md 3.1, 8.3-8.7).

- inc/cvc/ariadne/script_engine.h: ScriptEngine (language, has_module,
  load_block, unload), registered once per process by language
  (register_script_engine), which makes its block a key the loader keeps
  and its tokens hold; set_script_engine goes. ScriptBlock is what a
  block hands its engine: the block, the document's path, dir and prefix.
- ScriptLane, one per runtime and engine (AppRuntime::use_script_engine):
  the document's function registry, activations, and hooks (an on: event
  as an action, every frame and a changed value as a resident, one in
  flight).
- Activations run like sliced tasks: a process of the document's program
  lane (Runtime::start_process: its owner tag, chroot, channel policy,
  lock manager, functions: and ariadne.limits.* caps, which an activation
  may lower and never raise), a DSL loop around (script-step) that steps
  a ScriptActivation one slice at a time. A slice charges DSL steps, so
  the step cap is the gas cap; it may finish, yield the frame, await a
  future, or call any function the lane holds, which the loop applies in
  the process (narrowed by may_call). A language's activations share
  ariadne.limits.<language>_frame_budget (0.008 s) of each frame
  (exec_frame).
- Python's engine is a ScriptEngine: it answers the tokens and runs the
  python: block in load_block, and attach() uses it instead of a load
  hook. What a document sees is unchanged.
- Tests: ariadne_script_engine_test (a stub engine through all of it);
  the AriadneRequires token test through a stub Python engine.
A document's wasm: block runs a compiled plugin in arix and ariglx, as a
ScriptEngine on P1b's interface (docs/roadmap/ARIADNE_SCRIPTING_HOSTS.md
8.2-8.7; ARIADNE.md 9.6).

- src/ariadne_wasm: the runtime interface (guest_runtime.h) and its
  wasmtime backend, where each slice is one wasmtime_call_future_poll
  under a fuel yield interval and an import that waits is an async host
  function; a CBOR codec for DSL values; and the engine: the wasm:
  block (module, export, hooks, imports, limits, priority), an instance
  per document, _initialize, and the guest ABI (values as CBOR in the
  guest's memory, ari_alloc, the call, reply, yield and fail imports).
- ScriptLane::export_activation: a function whose calls are activations
  the caller parks on (an async function whose work starts itself, so no
  pool thread, and the wait counts against the caller's caps), with
  -async futures. AppRuntime::register_async_work_function under it.
  ActivationOutcome keeps a failed step's exception, so it re-raises
  with its own class.
- Fuel is charged as DSL steps (ariadne.limits.wasm_gas_per_step), under
  the action caps and the wasm frame budget; one activation per
  instance; a guest calling its own export is refused; imports:
  narrows its calls (state.read, state.write, msg, net, call).
- WasmError joins the built-in classes (18), the wasm token and the
  loader's warning for a wasm: block on a host without guests.
- arix and ariglx carry the engine where wasmtime's C API is found
  (-DARI_WITH_WASM / -DARIGL_WITH_WASM require it); CI's package-linux
  installs the cvcpkg wasmtime package.
- Tests: ariadne_wasm_test (a WAT guest), the export_activation stub
  case, and the arix_wasm_block / ari_wasm_needs_arix ctests.
Base automatically changed from feat/script-engine to master November 9, 2026 09:05
@transfix
transfix enabled auto-merge (squash) November 9, 2026 09:11
@transfix
transfix merged commit 600861c into master Nov 9, 2026
14 of 30 checks passed
@transfix
transfix deleted the feat/wasm-guests branch November 9, 2026 10:14
transfix added a commit that referenced this pull request Nov 9, 2026
master's tree is feat/wasm-guests' (ec7eef2), which this branch already holds.
transfix added a commit that referenced this pull request Nov 9, 2026
Master's WebAssembly engine (#1093) adds async work functions
(AppRuntime::register_async_work_function), which a script lane's
export_activation uses: a program's call parks while an activation runs
and finishes it. Fitted to Stage 1b:

- AppRuntime: a seventh provider binds them, host_only, on the calling
  program's own host (*ictx.host), after the host's async functions as
  master's single provider bound them; their table is locked like the
  others and swapped out in ~AppRuntime.
- export_activation finished its calls on app.exec_async_host(). A call
  made on an isolated document's group host and finished through group
  0's host forgets it by a slot of the wrong host's list (it may drop
  another live call of group 0's). New async_host::starting() names the
  host whose call() runs a work's start on this thread; the export
  finishes through that host's finisher, which posts nothing once the
  host has shut down.
- start_process loses the assert_outside_pump the last merge added: an
  exported call or an on: hook starts its activation from the calling
  step, on the host thread inside a pump of group 0 or a host group.

Tests: ScriptEngineTest.AnIsolatedDocumentsExportedCallFinishesOnItsGroup
and AsyncCalls.AStartSeesTheHostItWasMadeOn. The wrong-host finish is
not visible through a public API, so the first passes on the plain
merge too; it guards the path. Docs: STATE_EXEC_THREADED.md (the
seventh provider, exported calls), ARIADNE.md.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant