Parents & Children

Hale organizes loci into a strict parent-child ownership hierarchy where every child's memory region is nested inside its parent's arena. Parents can hold…

Hale organizes loci into a strict parent-child ownership hierarchy where every child’s memory region is nested inside its parent’s arena. Parents can hold static children in params or dynamically admit children at runtime via accept(c: Child), inspect child state through contract { expose; consume; }, and automatically reclaim per-connection children upon completion via release(c: Child).

type Player { name: String; }
locus GameSession {
params { host: Player; tick: Int = 0; }
}
locus Room {
accept(g: GameSession) {
// runs after g's params are initialized, before g.birth()
}
fn on_join(p: Player) {
GameSession { host: p }; // attaches to Room via accept()
}
}

Child Ownership Modes

Section titled “Child Ownership Modes”
Child Kind Declaration on Parent Lifetime & Arena Reclamation Typical Use Case
Static child params { db: Pool = Pool { }; } Constructed with parent; lives until replaced (self.db = Pool { }) or parent dissolves. Fixed subsystem components, connection pools, co-located counters.
Dynamic resident accept(c: Worker) { } (no release) Constructed dynamically; stays alive after c.run() returns until c calls terminate; or parent dissolves. Long-lived dynamic entities (rooms, actors, subscribed shards).
Dynamic flow accept(c: Conn) { } + release(c: Conn) { } c.run() is its lifetime; when c.run() returns, the runtime immediately drains, calls release(c), dissolves c, and frees its arena. Per-connection TCP/WebSocket handlers, short-lived request tasks.

Accepting Dynamic Children (accept)

Section titled “Accepting Dynamic Children (accept)”

A locus declares that it can parent dynamic instances of Child by implementing accept(c: Child):

  1. When GameSession { host: p }; is evaluated inside Room (or a descendant of Room), the runtime allocates the child’s arena inside Room’s arena and initializes g’s params.
  2. The runtime invokes Room.accept(g) before g.birth() runs. accept can inspect g’s initialized params and update parent state, but cannot reject the child.
  3. The runtime executes g.birth() and g.run().

self.children and Single-Clause Rule

Section titled “self.children and Single-Clause Rule”

Polymorphic Acceptance via an Interface

Section titled “Polymorphic Acceptance via an Interface”

To parent multiple child locus types under a single supervisor, declare accept(j: Interface) (and optionally release(j: Interface)) using an interface:

interface Job { fn weight() -> Int; }
locus Small { params { w: Int = 1; } fn weight() -> Int { return self.w; } }
locus Big { params { w: Int = 9; } fn weight() -> Int { return self.w * 2; } }
locus Pool {
params { load: Int = 0; }
accept(j: Job) { self.load = self.load + j.weight(); }
fn fill() {
Small { w: 3 }; // accepted via Job
Big { w: 5 }; // accepted via Job
}
}

Ancestor Bubbling

Section titled “Ancestor Bubbling”

When a locus instantiates a child whose type the immediate enclosing locus does not accept, ownership bubbles up the locus tower to the nearest ancestor that accepts it:

locus Ship { params { hull: Int = 0; } }
locus World {
accept(s: Ship) { } // top-level owner of all Ship instances
}
locus Fleet {
fn spawn() {
Ship { hull: 100 }; // Fleet has no accept(s: Ship), so Ship bubbles up to World
}
}

Bubbling Rules

Section titled “Bubbling Rules”

Bus Subscribers Created in Handlers Require an Owner

Section titled “Bus Subscribers Created in Handlers Require an Owner”

Because a bus handler returns after processing each message, an unowned locus instantiated inside a bus handler dissolves immediately when the handler exits—meaning any bus subscriptions on that locus would never fire for future messages. hale check rejects unowned bus subscribers instantiated inside handlers:

type Ping { n: Int = 0; }
topic Tick { payload: Ping; }
locus Watcher {
bus { subscribe Tick as on_tick; }
fn on_tick(t: Ping) { }
}
locus Hub {
bus { subscribe Tick as on_msg; }
fn on_msg(p: Ping) {
Watcher { }; // error: bus subscriber instantiated without an accepting owner in handler
}
}
main locus App {
params { h: Hub = Hub { }; }
run() { }
}

To fix this error, declare accept(w: Watcher) on Hub or on an ancestor of Hub that encloses Hub on every construction path:

Parent-Child Contracts (contract { expose; consume; })

Section titled “Parent-Child Contracts (contract { expose; consume; })”

A child locus declares which parameters its parent may inspect in a contract { expose ...; } block, and the parent declares which fields it reads in contract { consume ...; }:

type SessionState = enum { Lobby, Playing, Over };
locus GameSession {
params { tick: Int = 0; state: SessionState = SessionState::Lobby; }
contract {
expose tick: Int;
expose state: SessionState;
}
}
locus Room {
contract { consume tick: Int; }
accept(g: GameSession) {
if g.tick > 1000 { /* ... */ }
}
}

hale check verifies that the parent declares an accept clause and that every field listed in consume is listed in the accepted child’s expose block with the exact same type. (Note: contract checks the declared expose/consume pairs; use @sealed on a locus when you want the compiler to forbid all external field access.)

Flow Is Vertical Only

Section titled “Flow Is Vertical Only”

In Hale’s ownership hierarchy, references flow strictly vertically—from parent to child and child to parent—never sideways between siblings. When sibling loci need to coordinate, they route through their shared parent or publish over the bus.

Because child memory arenas are sub-regions of their parent’s arena and no lateral pointers exist across siblings, tearing down a parent locus safely tears down every accepted child, deregisters all child bus subscriptions, and frees the entire memory subtree wholesale without garbage collection.

Flow Children vs. Resident Children (release and terminate;)

Section titled “Flow Children vs. Resident Children (release and terminate;)”

By default, an accepted child is a resident: when its run() returns, the child stays alive in the parent’s arena (for example, to continue handling bus messages) until the parent dissolves.

For a long-lived server that accepts a new child per client connection, keeping children as residents until the server exits would accumulate memory indefinitely. Declaring release(c: Conn) on the parent marks Conn as a flow:

locus Conn {
params { conn_fd: Int = -1; }
run() {
let stream = std::io::tcp::Stream { conn_fd: self.conn_fd, owns_fd: false };
while true {
let chunk = stream.recv(4096) or "";
if len(chunk) == 0 { return; } // client closed connection -> run() returns
}
}
}
locus Server {
accept(c: Conn) { }
release(c: Conn) { } // marks Conn as a flow child
}

Flow vs. Resident Semantics

Section titled “Flow vs. Resident Semantics”

Next: Supervision & Structural Failure.