wasm-hub.md raw

The wasm domain hub: one protocol, two planes

Status: protocol fixed and implemented (round 386). The hub lives in jsruntime/supervisor.mjs; the host supplies start, stop, log, fault and exited, and the protocol runs under node --test with fake ports. The step order that got it there is at the bottom.

Why there is a hub at all

A wasm domain is a Worker. A Worker started by a Worker does not begin to run while its creator is parked in Atomics.wait - the startup task needs the creator's event loop - and a domain that spawns and then waits for the child is exactly that shape. The page is the only realm that is never parked in wasm, so the page starts every domain. That is not a convenience: it is the reason wasm spawn can work at all.

The page is therefore already a switchboard. One protocol carries everything a domain says to the page; the data plane does not go through the page.

The three planes

planepathwhat crosseswho reads it
controldomain -> hub (port)spawn, exit, panic, rebind, heartbeatthe hub, by id
logdomain -> hub (port)stdout/stderr textthe hub, then the page's console
datadomain -> domain (SharedArrayBuffer)message bytesthe two domains only

The first two share a port and an id scheme; they are still separate planes in the hub (separate handlers, and logs have their own queue). Keeping them apart is what lets a log be printed when its id is stale without that tolerance leaking into control.

Logs star to the page. A domain posts its log to the hub; it is never forwarded up the tree. A parent that forwards its child's logs is a parent parked in a receive, which cannot pump anything, so a blocked parent would silence its children - the same defect shape as a blocking select arm. The page can pump on a frame callback and is never in Atomics.wait, so it is the one place a log queue may sit.

Data stays direct. A spawn channel is a SharedArrayBuffer ring created by the spawning domain. The hub forwards the handles (and the SABs) into the child's boot message and never reads or writes a byte of the ring.

The log queue is bounded, and a drop is reported. A domain that prints without a reader (a trace loop, a busy worker) must not grow the page without limit, so the hub keeps at most queueMax entries (4096 by default). A log line that arrives with the queue full is dropped, counted on that domain, and the count is flushed when the domain exits as one stderr line: [hub] N log lines dropped for <id>. The summary itself bypasses the queue if it is still full - a summary of lost diagnostics is the one line that may not be lost, or "no output" would be ambiguous again. The hub's stats().dropped and stats().unattributedDropped carry the same numbers.

A channel that already sits on a boundary keeps its ring when it crosses another spawn, on both targets: wasm's runtime.SpawnChannelHandle reuses the existing SAB handle, and native's spawnDomain reuses the channel's existing ring pair instead of creating a new one. Creating a second ring for the same channel detaches it from the holder that passed it on - the manager-hands-a-worker's-pipe-on case - and the two ends then wait on different rings.

Native's direction is decided per channel, by the child-domain token in the channel (ch.childDomain), not by a per-domain flag. Both domains hold the same channel struct (they are threads), and a domain can be a child of one spawn and the parent of another: a single "am I a child" flag picks the wrong ring for one of the two, which is exactly how a spawned domain that spawns and then receives used to hang.

Addressing

never reused. The root takes 1; its first child takes 2 and its lineage is 1.2; that child's first child takes 3 and is 1.2.3. The LAST component is the domain id itself - the same number the native runtime's ch.childDomain carries - so a log line, a hub node and a channel's role all name one number, and a debugging session never has to map one id scheme onto another.

parent. A domain cannot choose or change its own id.

the hub.

chans, wasm or fnIdx; those fields are payload and are handed to the child's boot verbatim.

Control messages (domain -> hub)

Every control message carries kind and id. parent is carried on spawn (the sender's own parent, for the record) and set by the hub on the child.

kindfieldshub action
spawnid, parent, wasm, fnIdx, args, chans, breakerallocate child id id.N; call the host's start; register the child under id; the payload goes into the boot untouched
exitidthe domain returned normally: stop its Worker (a root's Worker is kept - the page drives it through the relay), adopt its children to its parent (a rebind to each), count it
panicid, textreport a fault; the page stops every domain (native parity: a panic aborts the process)
rebindparenthub -> domain only: your parent id changed (the intermediate above you exited cleanly). A rebind in the other direction is a protocol error.
heartbeatidrecord lastSeen, nothing else

exit and panic are different on purpose. A clean exit is not a failure: an intermediate that returns leaves a live subtree behind, and the hub re-parents it rather than killing it. A panic is the process dying, so the tree goes with it.

Log messages (domain -> hub)

{kind:'log', id, fd, text} where fd is 1 (stdout) or 2 (stderr). The hub queues them and drain() hands them to the page in arrival order, per domain.

Missing or mismatched id

This is where the next defects will live, so the table is part of the protocol:

caseactioncounter
spawn from an unknown idrefuse: do not start a domain that the tree cannot place or reap. Reply {kind:'refused', id, reason:'unknown-sender'} to the sender's port.refusedSpawn
spawn from a known id but on the wrong portrefuse, same reply, reason:'port-mismatch'portMismatch
log from an unknown idprint it attributed to ? - a log must never be dropped for a stale lineage, or the evidence for the next defect disappearsunattributedLog
exit / panic from an unknown iddrop (the node is already gone); countunknownExit / unknownPanic
heartbeat from an unknown iddrop; countunknownHeartbeat
rebind addressed to the hubdrop; countunknownRebind
any control field with a non-string iddrop; countcounter per case

A refused spawn is loud at the sender: the domain that asked sees refused and reports it as an error. A hub that starts the domain anyway would create a Worker no exit can match and no stop can find - an unkillable orphan, which is worse than a refusal.

Heartbeat is advisory, never a reaper

A domain parked in Atomics.wait cannot send a heartbeat: its thread is inside the wasm call and its event loop is not running. A liveness timer that reaped silent domains would therefore kill exactly the domains that are waiting for a message - the defect this whole design exists to avoid. Heartbeats may inform a diagnostic or a human; they must not decide liveness.

Reaping

with rebind; they keep logging to the page and exit cleanly. This is the invariant test: A spawns B, A exits, B keeps logging and exits cleanly.

resolves the loader and the page keeps the root's Worker for callbacks, so the hub adopts the root's children at the page (0) and they keep running. The app stops them the way it always could: it holds their breakers. A page teardown (stopAll) is what ends a tree.

native process.

lattice will use once reaping is scoped per subtree.

Divergence to keep visible: native needs no broker

Native spawn is a fork: the child is a process, the kernel owns the tree, and nothing has to route anything. Wasm spawn requires the page's supervisor for the lifetime of the domain - not just at spawn. So:

(__spawn_entry present) that is booted without a supervisor fails at boot with a named message, not at the first spawn();

host), and sw-host.mjs - which runs a module in a service worker with no page - is the one path that does not, so a spawn-capable module there is refused at init.

Step order (from the design discussion)

  1. This document.
  2. Extract the hub into jsruntime/supervisor.mjs with a real log queue and a

node-side test that feeds spawn/log/exit sequences with no browser.

  1. Browser fixtures: the orphan invariant, and a parent parked in a receive

while its child spawns (the shape that broke last time).

  1. The lattice: hubs per arena, subtree survival policy, and fan-out - deferred

until 1-3 are real.