open-standards/wai/wai-quantum-serve/build-proxy.sh
Transaction Science e4ddbf8ff8
Some checks failed
no-vanity-metrics / Scan committed Markdown for vanity metrics (push) Successful in 3s
standards workspaces build + test / arl-rs (push) Has been cancelled
standards workspaces build + test / jcp-rs (push) Has been cancelled
standards workspaces build + test / joule-code-rs (push) Has been cancelled
standards workspaces build + test / joule-ui-rs (push) Has been cancelled
standards workspaces build + test / jouleclaw-rs (push) Has been cancelled
standards workspaces build + test / joulecontract-rs (push) Has been cancelled
standards workspaces build + test / jouledesk-rs (push) Has been cancelled
standards workspaces build + test / joulehook-rs (push) Has been cancelled
standards workspaces build + test / map-rs (push) Has been cancelled
standards workspaces build + test / mesh-rs (push) Has been cancelled
standards workspaces build + test / openpay (push) Has been cancelled
standards workspaces build + test / proof-rs (push) Has been cancelled
standards workspaces build + test / sandbox-rs (push) Has been cancelled
standards workspaces build + test / sense-rs (push) Has been cancelled
standards workspaces build + test / smart-byte-rs (push) Has been cancelled
standards workspaces build + test / aac-rs (push) Has been cancelled
standards workspaces build + test / eoc-rs (push) Has been cancelled
wai-quantum-serve: a pure wasi:http/proxy build — no host flags
The wasip2 component only ran with `wasmtime serve -S cli=y`, because Rust's std
imports `wasi:cli/environment` at startup and the bare proxy world does not
provide it. I documented that rather than claiming portability we did not have;
this closes it.

build-proxy.sh compiles a wasm32-wasip1 core module and adapts it with the
Bytecode Alliance proxy adapter. The resulting component imports only what the
proxy world defines — wasi:http/types, wasi:io/*, wasi:cli/std{in,out,err},
wasi:random — with no wasi:cli/environment, so it runs on any wasi:http host with
no host flags. Verified: `wasmtime serve` with no -S serves /health and returns
statevector_hash 25a7d930… for grover, identical to native and to the wasip2
build.

The script guards the property it exists to provide: it greps the component's own
world and fails the build if wasi:cli/environment ever reappears, so a future
dependency cannot silently reintroduce the flag requirement. The adapter is
cached per wasmtime version rather than vendored, since it is tied to a WASI
version and must match the host.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
2026-09-04 06:28:08 -04:00

45 lines
2.1 KiB
Shell
Executable file

#!/usr/bin/env bash
# Build wai-quantum-serve as a PURE `wasi:http/proxy` component.
#
# Why this exists: a plain `--target wasm32-wasip2` build imports
# `wasi:cli/environment` (Rust's std pulls it in at startup), which the bare
# proxy world does not provide — so it only runs with `wasmtime serve -S cli=y`.
# Building a wasip1 core module and adapting it with the Bytecode Alliance's
# proxy adapter produces a component whose imports all sit inside the proxy
# world, so it runs on ANY wasi:http host with no host flags.
set -euo pipefail
# Keep this in step with the wasmtime you serve with: the adapter is tied to a
# WASI version, and a mismatch shows up as an unresolved-import error.
WASMTIME_VERSION="${WASMTIME_VERSION:-v48.0.1}"
CACHE="${WASI_ADAPTER_CACHE:-$HOME/.cache/wasi-adapters}"
ADAPTER="$CACHE/wasi_snapshot_preview1.proxy.$WASMTIME_VERSION.wasm"
OUT="${1:-target/wai_quantum_serve.proxy.wasm}"
command -v wasm-tools >/dev/null || { echo "need wasm-tools (cargo install wasm-tools)" >&2; exit 1; }
rustup target list --installed | grep -qx wasm32-wasip1 || rustup target add wasm32-wasip1
if [ ! -f "$ADAPTER" ]; then
mkdir -p "$CACHE"
url="https://github.com/bytecodealliance/wasmtime/releases/download/$WASMTIME_VERSION/wasi_snapshot_preview1.proxy.wasm"
echo "fetching proxy adapter $WASMTIME_VERSION"
node -e '
const fs=require("fs");
(async()=>{const r=await fetch(process.argv[1],{redirect:"follow"});
if(!r.ok){console.error("download failed",r.status);process.exit(1);}
fs.writeFileSync(process.argv[2],Buffer.from(await r.arrayBuffer()));})();
' "$url" "$ADAPTER"
fi
cargo build --release --target wasm32-wasip1
wasm-tools component new target/wasm32-wasip1/release/wai_quantum_serve.wasm \
--adapt "wasi_snapshot_preview1=$ADAPTER" -o "$OUT"
# Fail loudly if anything outside the proxy world crept back in.
if wasm-tools component wit "$OUT" | grep -q "wasi:cli/environment"; then
echo "ERROR: component still imports wasi:cli/environment — not a proxy-world component" >&2
exit 1
fi
echo "built $OUT"
echo "run: wasmtime serve --addr 127.0.0.1:8080 $OUT # no -S flags needed"