The Bus

The Hale bus provides typed, many-to-many message passing between loci without shared pointers or channel plumbing. Loci declare which topics they publish…

The Hale bus provides typed, many-to-many message passing between loci without shared pointers or channel plumbing. Loci declare which topics they publish and subscribe to inside a bus { } block, and the compiler and runtime wire delivery—from zero-overhead synchronous calls on the same thread to lock-free rings across threads or sockets across processes.

type Order { id: String; amount: Decimal; }
topic OrderPlaced { payload: Order; }
topic OrderShipped { payload: Order; }
locus Warehouse {
bus {
subscribe OrderPlaced as on_order;
publish OrderShipped;
}
fn on_order(o: Order) {
OrderShipped <- o;
}
}

Declaring Topics & Payloads

Section titled “Declaring Topics & Payloads”

A topic is a top-level declaration that pairs a channel name with a payload type:

type Order { id: String; amount: Decimal; }
topic OrderPlaced { payload: Order; }

Bare primitives (Int, String, Decimal) cannot be used directly as bus payloads and are rejected at compile time:

bus { publish "org.metrics" of type Int; } // error: `Int` cannot be carried directly on the bus

Wrap scalar values in a single-field record (type Metric { n: Int = 0; }).

A delivery is serialized value by value, and every value a record can hold crosses the wire, at any depth: scalars, enums with or without payloads, String, Bytes, nested records, bounded lists (of strings and records too), fixed arrays, tuples and std::json::Value. Only what no frame can hold is refused by hale check where the payload is first delivered or declared (of type M): a fn value, a locus handle, a view, or a type that contains itself (bound it with ids instead).

type Point { x: Int; label: String; }
type Status = enum { Ok, Moved(Point), Tagged(String, Int) };
type Snapshot {
names: bounded[String; 8]; // an i64 count, then each string
points: bounded[Point; 4]; // an i64 count, then each record
status: Status; // a u32 tag, then the variant's values
pair: (Int, String); // as a record of its elements
meta: std::json::Value; // its text
}
topic Snapshots { payload: Snapshot; }

The wire format is internal—Hale talks to Hale with it (the encodings are tabled in the spec’s Payload type section); a foreign peer goes through a binding’s codec(...). A frame that lies (a list count past its capacity or below zero, an enum tag past its last variant, a length past the frame’s end) is dropped by the transport reader and counted as dropped_decode. The raw paths keep their rule: a zero_copy binding or a layout: ring copies bytes as they are, so their payloads hold only fixed-size scalar fields. A native shm_ring binding carries struct payloads only, and a layout: ring hands its subscriber one record per call (no Drain<T> handler).

Publishing & Subscribing (bus { })

Section titled “Publishing & Subscribing (bus { })”

Every locus declares its bus inputs and outputs inside a bus { } block:

Syntax Role Description
subscribe Topic as handler; Inbound Registers fn handler(p: Payload) to receive messages sent to Topic.
publish Topic; Outbound Authorizes the locus to send messages on Topic. Publishing without this declaration is a compile error.
Topic <- value; Statement Sends value to Topic. Produces no return value.
publish "prefix.**" of type T; Wildcard outbound Authorizes publishing T to runtime-computed subject strings matching "prefix.**".

Subscriptions are declarative: the runtime registers a locus’s subscriptions when it is constructed (before birth()) and deregisters them automatically when it dissolves. Short-lived subscribers (such as per-connection loci) reuse subscription table slots as they dissolve, and any queued messages addressed to a dissolved subscriber are dropped rather than delivered to a subsequent occupant of that slot. (Note: String routing keys registered by dynamic subscribers remain interned until process exit.)

Dynamic Wildcard Subjects

Section titled “Dynamic Wildcard Subjects”

When a locus computes the target subject at runtime—such as a structured logger targeting log.<component>—declare a wildcard pattern in bus { } and send to a String expression:

type LogEvent { phase: String; }
locus Trace {
params { component: String = "root"; }
bus { publish "log.**" of type LogEvent; }
fn note(phase: String) {
let subject = "log." + self.component;
subject <- LogEvent { phase: phase };
}
}

Computed subject publishes enforce two checks at runtime (static Topic <- v sends are verified at compile time and incur zero runtime check cost):

  1. Pattern authorization: The runtime subject string must fall under a pattern declared in the locus’s bus { } block; otherwise the send raises BusPublishUnauthorized.
  2. Payload agreement: The subject must not match a subscriber expecting a different payload type (BusPayloadMismatch). hale check also warns at compile time if a subscription sits under another locus’s wildcard pattern with a mismatched payload type.

Keep wildcard patterns as narrow as possible ("log.**" rather than "**"). Static analysis uses wildcard prefixes to prove that a logger cannot publish to unrelated application topics (such as app.order) when verifying claims { } publisher counts.

Delivery Guarantees, Ordering, and Lowering

Section titled “Delivery Guarantees, Ordering, and Lowering”

In-Process vs. Bound Delivery

Section titled “In-Process vs. Bound Delivery”

A send statement OrderShipped <- o; requires no or error handler by default because the broker only accepts messages it can deliver under the topic’s contract:

FIFO Ordering

Section titled “FIFO Ordering”

All messages published by a single publisher arrive in FIFO send order across all topics on the same thread:

Prices <- p;
Orders <- o;

Every same-thread subscriber receives p before any same-thread subscriber receives o, regardless of whether the compiler lowers either send to a direct synchronous call or a queued delivery. No ordering is defined between publishers or subscribers executing on different threads.

Subscriber Registration Order & Tree Reservations

Section titled “Subscriber Registration Order & Tree Reservations”

A subscriber must be constructed before a publisher sends a message. In imperative bodies (fn main() or methods), instantiate subscribers before publishers (hale check warns on "publish before its subscriber" when both appear in the same body).

Within a single locus literal’s static tree, the runtime handles registration order automatically:

Static Devirtualization (dispatch_plan)

Section titled “Static Devirtualization (dispatch_plan)”

When a closed-world local topic has quiet handlers on the same thread, the compiler elides the bus queue and lowers Topic <- value; into a direct synchronous method call:

Lowering Flavor Conditions Mechanism
static_direct Local unbound topic, same thread, quiet handler, flat scalar payload (Int, Float, Bool, Decimal, Duration). Direct synchronous function call passing scalar fields.
static_bucket Local unbound topic, same thread, quiet handler, payload contains String, Bytes, or nested structs. Direct synchronous call with arena scratch management.
dynamic Cross-thread delivery, topic bound in bindings { }, or non-quiet handler. Full runtime bus queue / ring dispatch.

Inspect the compiler’s lowering decisions for every subject with:

Terminal window
hale model dump app.hl # see the `dispatch_plan` section

The dispatch_plan is recorded in the build identity so deterministic recordings are only replayed against builds with identical dispatch lowering.

Backpressure & Queue Bounds

Section titled “Backpressure & Queue Bounds”

By default, topic queues grow dynamically. For high-rate producers or slow consumers, you can bound capacity on the topic (publisher backpressure) or on individual subscriptions (consumer shedding):

Bound Location Syntax Behavior at Capacity
Topic declaration (Publisher contract) bounded(1024); on_full: fail; Broker rejects the send when full; every publish site must handle the failure with or raise, or discard, or or wait.
Subscribe clause (Consumer shedding) bounded(64, drop_old) Silently drops the oldest queued message to keep the newest 64 (ideal for latest-state snapshots).
Subscribe clause (Consumer shedding) bounded(64, drop_new) Silently drops newly arriving messages until queue space opens.
topic Frame {
payload: Reading;
bounded(1024);
on_full: fail;
}
subscribe Frame as on_frame bounded(64, drop_old);

A subscriber bound smaller than the topic bound sheds excess messages locally before the topic bound is reached.

The topic bound needs no keyed_by: any topic may declare bounded(N); on_full: fail;. or raise refuses with BusFull at the send site, or discard sends anyway and lets a subscriber at capacity shed the message (counted), and or wait parks the publisher until the queue drains. On a keyed topic that also declares on_unmatched: fail, a send takes or raise or or discard (or wait cannot outwait an unmatched key).

Keyed Routing (keyed_by)

Section titled “Keyed Routing (keyed_by)”

By default, a topic broadcasts every message to every subscriber. When managing per-entity or sharded subscribers (such as one locus per connection, symbol, or tenant), declare keyed_by <field> on the topic and where key == <expr> on the subscription so the runtime routes matching messages directly:

type Tick { symbol_id: Int; price: Decimal; }
topic Quote { payload: Tick; keyed_by symbol_id; }
locus Feed {
params { symbol_id: Int = 0; }
bus {
subscribe Quote as on_quote where key == self.symbol_id;
}
fn on_quote(t: Tick) {
// receives only ticks where t.symbol_id == self.symbol_id
}
}

Routing Key Rules

Section titled “Routing Key Rules”

Unmatched Key Policies (on_unmatched)

Section titled “Unmatched Key Policies (on_unmatched)”

Configure how a keyed topic handles messages whose key matches no active subscriber using on_unmatched::

Policy Syntax Behavior when No Subscriber Matches
Swallow (default) on_unmatched: swallow; Drops the message silently. Set LOTUS_BUS_LOG_UNMATCHED=1 to log unmatched drops during debugging.
Fail on_unmatched: fail; Makes publishing fallible; every send site says what a publish no subscriber matches does: or raise, or discard, or note(err) or a block or { … } (with err: BusUnmatchedKey in scope), or or fail … in a fallible fn. A subscriber whose key matches (a String key by its text), or one of the same tree still being built that will claim the message, counts as a match.
Fallback on_unmatched: fallback; Delivers unmatched messages to catch-all subscribers registered with where key == _. Requires at least one catch-all subscriber in the program.
type Tick { symbol_id: Int; price: Decimal; }
topic Quote { payload: Tick; keyed_by symbol_id; on_unmatched: fallback; }
locus Unrouted {
bus { subscribe Quote as on_stray where key == _; }
fn on_stray(t: Tick) { }
}

Next: Concurrency & Thread Placement.