# Workflow Guest Guide This document describes the current Wasm guest contract used by `xburner` workflow mode. ## Overview In workflow mode, the host owns: - transport - TLS - cookies - metrics - thresholds - execution scheduling The Wasm guest owns: - iteration state - request sequencing - response-driven transitions - guest-managed variables under `iter.*` and `vu.*` ## Required Exports Workflow guests must export the following functions: - `get_abi_version` - `init` - `start_iteration` - `next_request` - `handle_response` Current ABI version: - `1` ## Optional Exports Workflow guests may also export: - `setup` - `teardown` ## Export Semantics ### `get_abi_version` Signature: ```c uint32_t get_abi_version(void); ``` Return: - `1` ### `init` Signature: ```c int32_t init(uint32_t vu_id, uint32_t seed_hi, uint32_t seed_lo); ``` Called once when a workflow instance is created or recreated. Return: - `0` on success - non-zero to fail instance initialization ### `start_iteration` Signature: ```c int32_t start_iteration(uint32_t input_ptr, uint32_t input_len); ``` Called at the start of every iteration. Return: - `0` on success - non-zero to fail the iteration and recreate the instance ### `next_request` Signature: ```c uint64_t next_request(void); ``` Return value: - `0` means the iteration is done and no more requests are needed - non-zero means a packed pointer/length pair pointing to `HttpRequestActionV1` The guest is responsible for returning a valid in-memory `HttpRequestActionV1`. ### `setup` Signature: ```c int32_t setup(uint32_t input_ptr, uint32_t input_len); ``` Called once after host `setup.requests` complete and before any VU instances are created. ### `teardown` Signature: ```c int32_t teardown(void); ``` Called once after execution completes. During interrupted shutdown it is best-effort and bounded by `shutdown_timeout`. ### `handle_response` Signature: ```c int32_t handle_response(uint32_t meta_ptr, uint32_t meta_len, uint32_t body_ptr, uint32_t body_len); ``` Return values: - `0`: continue iteration - `1`: done, success - `2`: done, failed check - `3`: fatal script error ## Host Imports The host exposes the same functions in the `env`, `host`, and `xburner` modules: - `host_rand_u64` - `host_now_nanos` - `host_log` - `host_get_var` - `host_set_var` - `host_get_response_header` - `host_check_abort` ## Variable Namespaces Supported namespaces: - `setup.*` - `iter.*` - `vu.*` Rules: - `setup.*` comes from setup requests and may be extended by guest `setup` - the guest may write `setup.*` only during `setup` - during iterations the guest may write only `iter.*` and `vu.*` - `iter.*` is cleared at iteration boundaries - recreated workflow instances also clear `iter.*`, `vu.*`, response headers, and the cookie jar ## Request Model The guest does not perform HTTP directly. It emits request actions that reference host-defined request templates by `request_id`. The host-defined request templates live in `[[http.requests]]` in config. The guest may override: - body - timeout - capture body size For gRPC workflows, the returned body bytes are sent as protobuf payload bytes and `path` resolves to the RPC path from config. ## Response Model `handle_response` receives: - `HttpResponseMetaV1` for HTTP scenarios - `GrpcResponseMetaV1` for gRPC scenarios - an optional captured body slice The host sets transport flags and transport codes when a request fails before receiving a response. ## Recovery Behavior The host may recreate a workflow instance when it observes: - traps - invalid actions - invalid return values - cooperative script timeout The host may poison a worker on hard timeout, depending on backend behavior and configured limits. ## Module Cache Workflow module startup keeps two cache layers: - a filesystem validation cache under `.zig-cache/wasm-module-cache` - an in-process module-bytes cache for repeated instance creation inside one run The filesystem cache filename uses: - runtime backend - module content hash Each cached JSON record also stores: - a cache schema version - the validated ABI version and required export set When the guest bytes change, the hash changes and the previous cache entry is ignored automatically. `xburner validate` logs the observed cache outcomes as `validation_cache=hit|miss|stale` and `bytes_cache=hit|miss|reloaded`. ## Minimal Example See: - [include/xburner_workflow_guest.h](../include/xburner_workflow_guest.h) - [samples/workflow-health.c](../samples/workflow-health.c) - [samples/workflow-health.rs](../samples/workflow-health.rs) - [samples/workflow-health.go](../samples/workflow-health.go) - [samples/workflow-health.toml](../samples/workflow-health.toml) - [templates/workflow-guest.c](../templates/workflow-guest.c) - [templates/workflow-guest.rs](../templates/workflow-guest.rs) - [templates/workflow-guest.go](../templates/workflow-guest.go) - [templates/workflow-scenario.toml](../templates/workflow-scenario.toml) The generic build helper is: ```sh sh scripts/build_workflow_wasm.sh path/to/guest.c path/to/guest.wasm ``` Rust and Go helpers are also available: ```sh sh scripts/build_workflow_rust_wasm.sh path/to/guest.rs path/to/guest.wasm sh scripts/build_workflow_go_wasm.sh path/to/guest.go path/to/guest.wasm ``` Notes: - the Rust template is validated in the repository template matrix - the Go template currently targets `wasip1` and is validated with the `wasm3` backend The sample guest can be rebuilt with: ```sh sh scripts/build_workflow_sample_wasm.sh ``` To scaffold a new workflow guest plus matching scenario config: ```sh sh hacks/scaffold_workflow.sh my-flow ./my-flow ``` Language variants: ```sh sh hacks/scaffold_workflow.sh --lang c my-flow ./my-flow-c sh hacks/scaffold_workflow.sh --lang rust my-flow ./my-flow-rust sh hacks/scaffold_workflow.sh --lang go my-flow ./my-flow-go ``` The scaffold helper also supports: - `--target ` - `--path ` - `--request-name ` - `--request-id ` - `--mode ` - `--vus ` - `--rate ` - `--pre-allocated-vus ` - `--max-vus ` - `--method ` - `--body ` - `--pass-response-body ` - `--capture-body-max-bytes ` - `--duration ` - `--warmup ` - `--threshold ` - `--backend ` - `--lang ` - `--cookie-jar ` - `--cookie-jar-scope ` - `--use-tls ` - `--connect-timeout-ms ` - `--max-steps-per-iteration ` - `--header ` - `--setup-header ` - `--setup-body ` - `--setup-capture-body-max-bytes ` - `--setup-request ` ## Backend Selection Workflow backend can be selected in config: ```toml [scenario] wasm_backend = "wamr" ``` Or overridden on the CLI: ```sh xburner run --wasm-backend wasm3 path/to/scenario.toml ```