Skip to content

WASM Bindings

The kairos-wasm crate compiles the Kairos engine to WebAssembly for browser and Node.js environments. It exports two classes: WasmEngine for raw simulation and WasmSession (behind a feature flag) for fly-by-wire evaluation.

Terminal window
# Default build (WasmEngine only)
wasm-pack build crates/kairos-wasm --target web
# With fly-by-wire feature (adds WasmSession)
wasm-pack build crates/kairos-wasm --target web --features fly-by-wire
# For Node.js
wasm-pack build crates/kairos-wasm --target nodejs --features fly-by-wire

Output is an npm-publishable package in pkg/.

The WASM runtime operates in advisory mode:

AspectWASM (advisory)Native (authoritative)
Policy signatureSkippedVerified
License enforcementSkippedEnforced
HITL override tokensAlways rejectedCryptographically verified
Domain/expiry checksSkippedEnforced

WASM bindings are suitable for dashboards, previews, and client-side simulations. Production enforcement must use a native licensed build.

The core simulation engine — no policy evaluation, no Substrate runtime. All methods use camelCase for JavaScript convention.

import { WasmEngine } from "kairos-wasm";
const engine = new WasmEngine(
{ columns: 25, rows: 17, lambda: 0.65, gamma: 0.1 },
{ x: 0, y: 0 }
);
MethodReturnsDescription
setSeed(seed)voidSet RNG seed
getSeed()numberGet current seed
setLambda(v)voidSet λ\lambda parameter
getLambda()numberGet λ\lambda parameter
setGamma(v)voidSet γ\gamma parameter
getGamma()numberGet γ\gamma parameter
setGridSize(cols, rows)voidOverride grid dimensions
setZeroSumEnabled(enabled)voidToggle zero-sum attractor capture
getConfig()objectCurrent engine config
MethodReturnsDescription
getState()objectCurrent engine state (t, b, lastMove, marker)
getWarningSignal()objectWarning signal (active, level, kind, metrics)
setState(state)voidSet engine state
setMarker(marker)voidSet marker position
primaryAgentId()stringGet primary agent ID
MethodReturnsDescription
runTick()objectExecute single-agent tick
runTickMulti(moves)objectExecute multi-agent tick
decideNextMoveForAgent(id)MoveDirectionPolicy-decided move for one agent
decideAllMoves()objectPolicy-decided moves for all agents
MethodReturnsDescription
getAgentStates()AgentState[]All agent states
resetAgents(setups)voidReset agents from setup array
updateAgentPolicy(id, lambda, gamma)voidUpdate per-agent policy overrides
MethodReturnsDescription
getSpecialNodes()SpecialNode[]All attractors and repulsors
getSpecialNodesInRange(tMin, tMax, bMin, bMax)SpecialNode[]Nodes within bounding box
setSpecialNodes(nodes)voidReplace all special nodes
MethodReturnsDescription
computeReachabilityField(horizon)voidCompute reachability DP
getReachabilityTotal(t, b)numberReachability value at grid position
getAllowedMoves(t, b)numberAllowed moves bitmask at position
MethodReturnsDescription
startRecording(scenarioId?)voidBegin trace recording
stopRecording()object | nullEnd recording and return trace
MethodReturnsDescription
loadEventScript(events)voidLoad EDMS event script
isEdmsActive()booleanCheck if EDMS is active
MethodReturnsDescription
getDestroyedBasins()object[]Destroyed basin records
getCaptureEvents()object[]Zero-sum capture events

Requires the fly-by-wire feature. Combines the engine with Substrate policy evaluation in a stateful session.

import { WasmSession } from "kairos-wasm";
const session = new WasmSession(
{ columns: 25, rows: 17, lambda: 0.65, gamma: 0.1 },
{ x: 0, y: 0 },
artifactJson, // Calibration artifact JSON string
policyJson, // Deployment policy JSON string
null // HMAC secret (Uint8Array or null)
);
MethodReturnsDescription
evaluate(request)objectEvaluate request without advancing time
step(direction)objectAdvance primary actor by one tick
stepActor(actorId, direction)objectAdvance specific actor by one tick
stepMulti(moves)objectAdvance all actors with explicit moves
decideAllMoves()objectPolicy-decided moves for all active actors
MethodReturnsDescription
getTelemetry()objectWarning signal for primary actor
getTelemetryForActor(actorId)objectWarning signal for specific actor
getTelemetryMap()objectWarning signals for all active actors
MethodReturnsDescription
previewProjection(actorId, horizon)objectProjection preview for actor
previewProjectionForPrimary(horizon)objectProjection preview for primary actor
previewTimelineEvents(horizon)object[]Upcoming EDMS timeline events
MethodReturnsDescription
registerActor(setup)voidRegister a new actor
unregisterActor(actorId)voidRemove a non-primary actor
MethodReturnsDescription
loadScenario(scenario)voidLoad scenario into session engine
loadEventScript(events)voidLoad EDMS event script
currentTick()numberCurrent engine tick
import init, { WasmEngine } from "kairos-wasm";
await init();
const engine = new WasmEngine(
{ columns: 25, rows: 17, lambda: 0.65, gamma: 0.1 },
{ x: 0, y: 0 }
);
engine.setSeed(1207);
for (let i = 0; i < 200; i++) {
const moves = engine.decideAllMoves();
const result = engine.runTickMulti(moves);
}
const state = engine.getState();
console.log(`Position: t=${state.t}, b=${state.b}`);

All complex data crosses the WASM boundary via serde-wasm-bindgen — JavaScript objects map directly to Rust structs without JSON serialization overhead. Rust panics are caught by console_error_panic_hook and surfaced as JavaScript errors.