v3/@claude-flow/guidance/docs/adrs/ADR-G025-wasm-kernel.md
Accepted
The Guidance Control Plane has security-critical hot paths (hashing, signing, secret scanning, destructive command detection) that benefit from:
JavaScript implementations using node:crypto are fast (backed by
OpenSSL C code) but are not portable across runtimes and are not
deterministic across versions. Regex scanning in JS is subject to GC
stalls under load.
Introduce a two-layer architecture:
Layer A — Rust WASM kernel (wasm-kernel/)
wasm32-unknown-unknown with SIMD128 enabled.proof (SHA-256, HMAC-SHA256, chain verification),
gates (secret scanning, destructive detection), scoring
(shard scoring and ranking).Layer B — Node host bridge (src/wasm-kernel.ts)
getKernel() returns a WasmKernel interface.batchProcess() sends multiple operations in one
WASM boundary crossing.The host calls the kernel once per event with a batch payload, not thousands of tiny calls. This amortizes the WASM boundary crossing cost.
The kernel compiles with target-feature=+simd128 via
.cargo/config.toml. This enables:
memchr and Aho-Corasick in the regex cratesha2 crateTo build without SIMD for maximum compatibility:
RUSTFLAGS="" wasm-pack build --target nodejs --release
Measured with 10,000 synthetic events (SIMD + O2):
| Benchmark | JS | WASM SIMD | Ratio |
|---|---|---|---|
| Proof chain (10k events) | 76ms | 61ms | 1.25x |
| SHA-256 individual | 505k ops/s | 910k ops/s | 1.80x |
| Secret scan (clean) | 402k scans/s | 676k scans/s | 1.68x |
| Secret scan (dirty) | 185k scans/s | 362k scans/s | 1.96x |
SIMD vs non-SIMD WASM:
| Benchmark | No SIMD (Oz) | SIMD (O2) | SIMD gain |
|---|---|---|---|
| Proof chain (10k) | 95.0ms | 60.9ms | 1.56x |
| SHA-256 individual | 506k/s | 910k/s | 1.80x |
| Secret scan (clean) | 402k/s | 676k/s | 1.68x |
| Secret scan (dirty) | 185k/s | 362k/s | 1.96x |
Positive:
Negative:
Mitigated:
wasm-pkg/| Module | Functions | Purpose |
|---|---|---|
proof | sha256_hex, hmac_sha256_hex, content_hash_sorted, verify_chain_json | Crypto primitives for ProofChain |
gates | scan_secrets, detect_destructive | Secret scanning, destructive detection |
scoring | score_shards, score_shards_json | Shard relevance scoring for Retriever |
wasm-kernel/ — Rust crate with Cargo.toml, src/{lib,proof,gates,scoring}.rswasm-kernel/.cargo/config.toml — SIMD target flagswasm-pkg/ — Built WASM package (committed, ready to use)src/wasm-kernel.ts — Node host bridge with JS fallbacktests/wasm-kernel.test.ts — 15 acceptance tests (parity + throughput)