The API Surface
Hale programs declare their external RPC and streaming interfaces as API surfaces (api blocks and @rpc annotations). From a surface declaration, the compiler generates strict request/response codecs, computes cryptographic contract digests, serves self-describing endpoints over multiple transports (unix::Rpc, http::Rpc, http::Rest, mcp::Rpc, grpc::Rpc, ws::Hub), and produces typed clients and OpenAPI/Protobuf specifications.
role trader;
unit cent;type Money = quantity Int in cent;type OrderId = distinct Int;
type OrderRequest { symbol: String; qty: Int; limit: Money; }type OrderResponse { order: OrderId; notional: Money; }type CancelRequest { order: OrderId; }type CancelResponse { order: OrderId; was_open: Bool; }type OrderError { code: String; reason: String; }
api Public { rpc Orders::place; rpc Orders::cancel requires: [trader];}
locus Orders { params { next: Int = 41; open: Int = 0; } closure position_limit { captures: open; epoch inline; }
fn place(req: OrderRequest) -> OrderResponse fallible(ClosureViolation) { if req.qty > 10000 { violate position_limit; } let id = OrderId(self.next); self.next = self.next + 1; self.open = self.open + 1; return OrderResponse { order: id, notional: req.limit * req.qty }; }
fn cancel(req: CancelRequest, ctx: std::api::Context) -> CancelResponse fallible(OrderError) { let n = Int(req.order); if n < 41 || n >= self.next { fail OrderError { code: "unknown_order", reason: "no order " + to_string(n) }; } return CancelResponse { order: req.order, was_open: true }; }}
fn main() { }For a step-by-step tutorial, see Build an API. For guidance on designing wire types and evolving schemas safely, see Designing API Messages.
Ecosystem Overview
Section titled “Ecosystem Overview”| Area | Construct / Tool | Purpose |
|---|---|---|
| Declaring Surfaces | api <Name> { rpc Locus::fn; }@rpc / @rpc(requires: [...]) |
Declares a table of callable operations, their required roles, and optional HTTP REST routes. |
| Serving Exposures | api::serve(Surface, Transport, ...) |
Binds a surface to a listener (unix::Rpc, http::Rpc, http::Rest, mcp::Rpc, grpc::Rpc, ws::Hub, or std::api::test::Rpc). |
| Streaming Hubs | ws::Hub, udp::Hubbindings { Topic: self.hub ... } |
Streams internal bus topics to external WebSocket or UDP subscribers with role checks and bounded queues. |
| Auth & Context | role, std::api::Context,BearerSource, RoleSource, StaticRoles |
Authenticates callers (Unix kernel credentials or bearer tokens), checks role grants, and passes caller metadata to handlers. |
| CLI Inspection & Driving | hale check --api, hale describe,hale call, hale watch, hale admin, hale mcp |
Inspects static inventories, queries live descriptions, calls endpoints, tails streams, serves a web admin UI, or bridges to MCP. |
| Spec & Client Codegen | hale api exporthale api client --lang hale|ts|js |
Exports OpenAPI, JSON Schema, MCP, and .proto specs, or generates zero-dependency Hale, TypeScript, and JavaScript clients. |
| Consuming External APIs | hale api import, api::client,VaultPrincipals, hale api drift |
Imports external OpenAPI (JSON/YAML) or MCP specs into typed api::Client handles tracked by the client(Surface) effect class. |
Surfaces
Section titled “Surfaces”Declaring api Blocks and @rpc Methods
Section titled “Declaring api Blocks and @rpc Methods”An api block names a surface and lists its operations as rpc rows. Each row references a locus method and optionally specifies role requirements (requires: [...]) or HTTP routes (at "METHOD /path").
- Handler Signature: Each handler takes one request parameter and may accept a trailing
ctx: std::api::Context(orctx: std::api::ServedContext), which is supplied by the transport and omitted from the wire request schema. - Return Value & Errors: The return type is the wire response. If the handler is
fallible(E):- When
Eis a user record or enum (likeOrderErrorabove), failures are returned to the caller as a structuredHandlerErrormatchingE’s schema. - When the handler may
violatean inline closure, it must declarefallible(ClosureViolation). If a violation occurs, the owner’son_failurepolicy runs first and the caller receives an opaqueServerErrorcontaining no internal violation state.
- When
Instead of writing a separate api block, you can annotate locus methods with @rpc to add them to the seed’s default surface (named after the seed):
locus Orders { @rpc fn place(req: OrderRequest) -> OrderResponse fallible(ClosureViolation) { … } @rpc(requires: [trader]) fn cancel(req: CancelRequest) -> CancelResponse fallible(OrderError) { … }}Both forms compile to the same internal surface representation. A single handler method can appear in multiple api surfaces (for example, to expose different role requirements or versioned surfaces over the same implementation).
Surface-Level Default Roles & Cross-Seed Roles
Section titled “Surface-Level Default Roles & Cross-Seed Roles”When most rows in a surface share the same role requirement, declare requires: on the api header. Rows without an explicit requires: inherit the surface default; a row with requires: [] overrides the default to allow any authenticated caller:
api Admin requires: [operator] { rpc Ledger::rebalance; // requires operator rpc Orders::cancel; // requires operator rpc Orders::status requires: []; // requires no role (any authenticated caller)}This produces the exact same surface table and contract digest as spelling requires: [operator] on each row.
Roles form a single program-wide vocabulary across seeds. A role declared in an imported library is referenced via its import alias (requires: [lib::reader], @rpc(requires: [lib::writer]), or role boss includes lib::writer;) and appears in descriptions and refusals under its base name (reader, writer).
Compile-Time Surface Validation
Section titled “Compile-Time Surface Validation”hale check validates every api row at compile time and rejects:
- A row naming a nonexistent locus or method (with a did-you-mean suggestion).
- A handler taking zero or more than one request parameter, or a
Drain<T>batch handler. - A method listed more than once in the same surface.
- An undeclared role name in
requires:(with a did-you-mean suggestion). - A request, response, or error type containing a field the wire codec cannot serialize (such as
Bytes, an unbounded[T]slice, or a locus). - A void-returning handler that may
violatea closure without declaringfallible(ClosureViolation):
rpc `Orders::plcae`: `Orders` declares no fn `plcae`; did you mean `place`?rpc `Orders::cancel` requires `tradr`, which no `role` declares; did you mean `trader`?rpc `Ledger::dump`: its response `Export` has a field `raw: Bytes`, which the JSON codec does not carryrpc `Orders::flush` may violate (`violate stale`) and returns nothing: an rpc handler that may violate is `fallible(ClosureViolation)`, so its caller receives the server error instead of a resultContract Digests (Surface and Member Digests)
Section titled “Contract Digests (Surface and Member Digests)”The compiler hashes every surface into a 64-bit FNV-1a contract digest (fnv1a64:<hex>):
- Surface Digest: Computed over all rows in member order, folding each member’s name, the recursive shape hashes of its request, response, and error types (
spec/model.md§ The shape of a type), its error classes, its HTTP route (if any), and its required roles. It is unaffected by the surface’s name, where it is served, or build flags. - Member Digest: Computed over a single
rpcrow in isolation. Generated clients send the target member’s digest on every call, and the server admits any request matching either the surface’s current digest or that member’s digest.
Because admission checks member digests, adding a new rpc row to a surface changes the surface digest without invalidating clients compiled against existing members. Modifying an existing member’s request/response types, error classes, route, or roles changes its member digest, causing outdated clients to be rejected with digest_mismatch.
Documenting a Surface (/// Doc Comments)
Section titled “Documenting a Surface (/// Doc Comments)”A /// comment directly above a declaration is its doc comment, and every document generated from a surface carries it: the description, the OpenAPI, JSON Schema, MCP and .proto forms, and the TypeScript, JavaScript and Hale clients. Document the surface, its rows, the handlers and the types they name:
role clerk;
/// A request to place an order.type PlaceOrder { /// The instrument's ticker symbol. symbol: String; /// How many units to buy. qty: Int;}
/// Why an order was not placed.type OrderError = enum { /// No instrument has the symbol. @not_found UnknownSymbol, Rejected(String),};
type Receipt { order: Int; }
locus Desk { /// Place an order (the handler's doc; a row's own doc replaces it). fn place( /// The order to place. o: PlaceOrder ) -> Receipt fallible(OrderError) { return Receipt { order: o.qty }; }}
/// The order desk's public surface.api Orders { /// Place an order. /// It is filled at the limit or better. rpc Desk::place requires: [clerk];}- Rows. A row’s doc is the one above its
rpcline, or its handler’s when it has none. In OpenAPI the first line is the operation’ssummaryand the rest itsdescription; an MCP tool’s description starts with it, which is what an agent reads when it picks a tool. - Types. A record’s, a field’s, an enum’s and a variant’s docs travel with the type into every row that uses it, as JSON Schema
descriptionkeys (a variant’s underx-hale-variant-descriptions),.protocomments, JSDoc and Hale doc comments. A field with no doc of its own takes the doc of the scalar or enum it is declared with. - The request. A doc on the handler’s request parameter describes the request in that row, in place of its type’s.
- Prose is not shape. Docs move no digest, so rewording one refuses no client.
hale api export --checkstill reports a committed bundle whose docs changed, since its files differ.
A /// comment that documents nothing (above a statement, a lifecycle hook or an import, at the end of a file, or after code on its own line) is a warning, not an error. Write // for a comment that is not a doc. hale fmt keeps every doc comment where it is.
Inspecting the Static API Inventory (hale check --api)
Section titled “Inspecting the Static API Inventory (hale check --api)”Pass --api to hale check to emit the program’s complete API inventory as JSON (all surfaces, digests, exposures, and JSON Schemas) without running the program:
$ hale check --api desk.hl{ "inventory": 1, "app": "Desk", "surfaces": [ { "name": "Public", "digest": "fnv1a64:a8930d6e7998e986", "members": [ … ] } ], …}You can also simulate the filtered description a specific caller will receive on a named exposure, or export specification formats for a surface:
# Preview exposure description filtered by caller roleshale check --api desk.hl --exposure public --caller bobhale check --api desk.hl --exposure public --caller alice --holds trader
# Export surface specifications to stdouthale check --api desk.hl --surface Public --openapihale check --api desk.hl --surface Public --json-schemahale check --api desk.hl --surface Public --mcphale check --api desk.hl --surface Public --protoRequest/Response Codec & Wire Shapes
Section titled “Request/Response Codec & Wire Shapes”Every type reachable from an rpc row is encoded and decoded strictly. Unknown fields in incoming JSON objects are ignored, while wrong field types, out-of-bounds lists, and missing required fields are rejected with malformed before the handler is invoked.
| Hale Type / Pattern | JSON Wire Format | Protobuf (.proto) Format |
Refusal Conditions (malformed) |
|---|---|---|---|
Int, Float, Bool, String |
Standard JSON primitives | optional int64, double, bool, string |
wrong_type, missing_field (if no default) |
Decimal<S> |
JSON number at scale S, every digit kept (12.34; schema {"type": "number", "x-hale-type": "Decimal<2>"}); a string holding a number ("12.34") reads the same; tagged `json:"price,string"` it is written as that string (schema "type": "string") |
optional string (its digits; // Hale: Decimal<2>) |
A nonzero digit past S, or a string holding no JSON number (wrong_type), never a truncation |
U256, I256 |
JSON string of its decimal digits ("1000000000000000000"), or 0x hex tagged `json:",hex"`; reads either, or an integer number that fits (schema {"type": "string", "x-hale-type": "U256"}) |
optional string (its digits; // Hale: U256) |
A fraction, an exponent, a value out of range, or other text (wrong_type) |
Int tagged `json:",hex"` |
0x hex in a JSON string ("0x4dc"), a negative value as its number; reads hex text or a number (schema {"type": "string", "x-hale-type": "Int"}) |
optional int64 |
No 0x, no digits, a sign, a space, past Int’s range (wrong_type) |
distinct T / quantity Int in u |
Underlying scalar value (Int count for quantities) |
Underlying scalar (// Hale: Type, q(u)) |
wrong_type |
Time |
UTC RFC 3339 string ending in Z ("2026-10-12T03:04:05.123456789Z") |
optional int64 (nanoseconds) |
Non-UTC offset, missing Z, or past year 2262 (wrong_type) |
Duration |
Integer nanoseconds (1500000000 = 1.5s) |
optional int64 (nanoseconds) |
wrong_type |
bounded[T; N] |
JSON array of 0..=N elements (optional on input; defaults to []) |
repeated T |
Length > N (too_long) |
[T; N] |
Fixed JSON array of exactly N elements |
repeated T |
Length > N (too_long), length < N (too_short) |
bounded[T; N] indexed_by k |
JSON object keyed by T.k ({"db": {"status": "up"}}) |
map<string, T> |
Length > N (too_long), repeated key (duplicate_key), a map that is no object or a member that is no record, such as an array (wrong_type) |
bounded[T; N] indexed_by k -> v |
JSON object mapping T.k to T.v ({"db": "ok"}) |
map<string, V> |
Length > N (too_long), repeated key (duplicate_key) |
std::json::Value |
Arbitrary well-formed JSON (<= 64 levels deep, <= 64 KiB) | optional string |
Invalid JSON, depth > 64, or size > 64 KiB |
| Any field with a default | null is the key omitted: the declared default, whatever its shape (a literal, an enum variant, a nested record’s declared default); a field with no default is missing_field |
Declared as-is | A std::json::Value field alone holds null as its text |
Optional field (x?: T, or x + has_x: Bool) |
Optional field; omitted or null sets has_x = false |
Both fields declared as-is | wrong_type if non-null value fails to decode |
Clear triple (x?: T clearable, or x + has_x + clear_x) |
Omitted -> neither; null -> clear_x = true; value -> has_x = true |
All three fields declared as-is | wrong_type if non-null value fails to decode |
enum { A, B(T), C(T, U) } |
"A", {"B": val}, {"C": [v1, v2]} |
enum (unit-only) or message with oneof |
Unknown variant, wrong payload shape/arity (wrong_type), missing_field |
Type set (type U = A | B;) |
Tagged object by type name ({"B": {...}}) |
message with oneof |
Unknown variant or wrong inner shape (wrong_type) |
Lists and Pagination
Section titled “Lists and Pagination”Lists must be fields of a record and have a static bound (bounded[T; N] or fixed [T; N]). Unbounded [T] slices, nested lists, and top-level bare list requests/responses are rejected at compile time.
type Item { id: Int; title: String; }type Page { items: bounded[Item; 50]; more: Bool; }
locus Shelf { params { items: bounded[Item; 500]; } fn list(q: ListQuery) -> Page { let mut page = Page { more: false }; for item in self.items { push(page.items, item) or { page.more = true; break; }; } return page; }}Keyed Maps (indexed_by)
Section titled “Keyed Maps (indexed_by)”A bounded[T; N] list of structs containing a String key field can be serialized on the wire as a JSON object using indexed_by <key_field>:
type Check { name: String; status: String; }type Health { checks: bounded[Check; 16] indexed_by name; }On the wire, checks is encoded as {"db": {"status": "up"}, "cache": {"status": "down"}}—each element’s name field becomes its property key and is omitted from the inner object, then restored in order when decoded.
When the element struct has only two fields (the String key and a scalar, Time, or unit-enum value), use indexed_by <key> -> <value> to serialize the values directly without a wrapper object:
type Check { name: String; status: String; }type Health { checks: bounded[Check; 16] indexed_by name -> status; }// Wire format: {"db": "ok", "nats": "unavailable"}In both Hale and generated TypeScript clients, an indexed_by field remains a list of Check records (Check[] in TypeScript), with the client converting to and from the wire map automatically.
Free-Form JSON (std::json::Value)
Section titled “Free-Form JSON (std::json::Value)”Use std::json::Value for opaque JSON subtrees that your program passes through or inspects dynamically:
type Seat { name: String; config: std::json::Value; }std::json::Value preserves the exact bytes sent by the caller (including key ordering, whitespace, and trailing decimal zeros like 2.50). It must be valid JSON at most 64 levels deep and 64 KiB long. Inside Hale, read .text using std::json cursor functions (std::json::find_field_raw(seat.config.text, "theme")), and construct new values using std::json::Value::parse(text) or ... or std::json::Value::null() (direct struct literals and mutating .text are rejected by the compiler).
Optional Fields (x?: T and clearable)
Section titled “Optional Fields (x?: T and clearable)”Hale has no Option<T> wrapper type: optionality is a property of a record field. An optional wire field is a field with a companion boolean flag, which x?: T declares for you:
- Default Value (
limit: Int = 50;): Use when omitting a field has the same meaning as passing a default value. - Optional Field (
text?: String;): Use when you must distinguish an omitted/null field from a default value (or distinguish an empty list[]from an omitted list). It declarestextand a real flaghas_text: Boolafter it, the presence pair. Whentextis omitted ornull,has_textisfalse. On output,textis only written whenhas_textistrue, andhas_textitself never appears on the wire. Writingtextin the program setshas_text. - PATCH Clear Field (
note?: String clearable;): Use for PATCH endpoints that distinguish “leave unchanged” (omitted -> both flagsfalse), “clear to null” ("note": null->clear_note = true, encoded asnullon output), and “set to value” ("note": "hi"->has_note = true). It declares the presence triplenote,has_note,clear_note. Marked"x-hale-null": "clear"in the schema and typednote?: string | nullin TypeScript.
The flags can also be spelled by hand (text: String = ""; has_text: Bool = false;), as earlier programs do: the description, the .proto and the contract digest are byte for byte the same either way, so converting a hand-written pair to x? changes no shape hash. Only the mark makes a write of the field set its flag.
Enums and Type Sets
Section titled “Enums and Type Sets”Enums serialize as a string for unit variants ("Open"), a single-key object for single-value variants ({"Renamed": "Q3 plan"}), and a single-key object holding an array for multi-value variants ({"Moved": [3, 7]}):
type Status = enum { Open, Closed };type Change = enum { Renamed(String), Moved(Int, Int), Archived };Type sets (type Inbound = NewOrder | Cancel;) serialize as enums keyed by each member’s type name ({"Cancel": {"id": 1}}).
Wire names and the catch-all
Section titled “Wire names and the catch-all”A wire you do not own often spells its values in lowercase, with dashes, or adds values over time. Tag a variant with json: to give it a wire name, and mark one variant other to catch the strings no variant names:
type Side = enum { Buy `json:"buy"`, Sell `json:"sell"` };type Event = enum { Fill(Int) `json:"fill"`, Cancel `json:"cancel"` };type Venue = enum { Spot `json:"spot"`, Margin `json:"margin"`, Other(String) `json:",other"` };type Kind = enum { Limit `json:"limit"`, Market `json:"market"`, Unknown `json:"unknown,other"` };Side::Sellcrosses as"sell", and"Sell"is nowwrong_typelike any unknown name.Event::Fill(3)is{"fill": 3}: a variant with values uses its wire name as the object key.Other(String)is a lossless catch-all:"dark"decodes toVenue::Other("dark"), which writes"dark"back. It writes whatever it holds, soVenue::Other("spot")writes"spot", which reads back asVenue::Spot.Unknownis a lossy catch-all:"iceberg"decodes toKind::Unknown, which writes its own wire name,"unknown".- Only strings are caught: an object with an unknown key is still
wrong_type. - The check refuses two variants with one wire name, a second catch-all, a catch-all holding anything but one
String, and a name on a lossless catch-all. A set’s member takes no tag. - When a whole enum follows one convention, mark the declaration instead of tagging each variant:
@wire_case(snake) type Status = enum { InProgress, OnHold, Done `json:"finished"` };crosses as"in_progress","on_hold"and"finished". The cases aresnake,screaming_snake,kebabandlower; a variant’s own tag wins, and everything below treats the spelling as a tag. Venue::name(v)andVenue::parse(s)(Records & Data) read the same wire names and the same catch-all, so a name you log or store is the name the codec writes.
The description lists the wire names ("enum": ["spot", "margin"]) and adds x-hale-variants (each variant’s wire name) and x-hale-other (the catch-all). The tags are part of the contract digest. The TypeScript client types Venue as "spot" | "margin" | (string & {}), and the Hale client declares the same tags. Protobuf ignores the renames and keeps Venue_Spot-style identifiers; a lossy catch-all also takes an enum number no variant has. A lossless catch-all makes the enum one with payloads, so the codecs cannot yet carry a list of it, and it cannot be a map value or a route parameter. The bus carries such an enum as any other, by its variant’s tag and values, so the renames do not reach it.
Error Classes
Section titled “Error Classes”When a handler’s error type in fallible(E) is an enum, its variants can carry an error class annotation to select the transport status code:
type OrderError = enum { @not_found Missing, @conflict Taken(String), Closed };| Error Class Annotation | HTTP Status Code | gRPC Status Code | Hale-Error-Class Header |
|---|---|---|---|
| (unannotated) | 422 Unprocessable Content |
FAILED_PRECONDITION |
(omitted) |
@not_found |
404 Not Found |
NOT_FOUND |
not_found |
@conflict |
409 Conflict |
ABORTED |
conflict |
@invalid |
422 Unprocessable Content |
INVALID_ARGUMENT |
invalid |
@forbidden |
403 Forbidden |
PERMISSION_DENIED |
forbidden |
@unavailable |
503 Service Unavailable |
UNAVAILABLE |
unavailable |
Because HTTP status codes 403, 409, and 503 can also be returned by transport-level refusals, classified handler errors include the Hale-Error-Class HTTP header (and "class" field on Unix sockets). Error classes are recorded in the schema (x-hale-error-classes), passed to custom std::api::Rpc transports via std::api::Outcome::HandlerError(body, class), and folded into the contract digest.
Direct Codec Invocation (T::api_decode and T::api_encode)
Section titled “Direct Codec Invocation (T::api_decode and T::api_encode)”You can call the compiler-generated wire codec directly on any API-eligible record or enum T, even outside api::serve:
let order = PlaceOrder::api_decode(body) or { return std::http::Response { status: 400, body: err.kind + ": " + err.field };};let reply = PlaceReply::api_encode(desk.place(order));T::api_decode applies the exact same validation rules as a served exposure (wrong_type, missing_field, too_long, too_short, duplicate_key), reporting the offending field name in err.field (or "payload" when decoding a top-level enum).
Positional Records (@positional)
Section titled “Positional Records (@positional)”Another program’s API may send a table’s rows as arrays rather than objects: candles as [[1688666400, 2990.0, 3010.0, 2995.0, 3005.0, 100.5], ...], book levels as [["3000.40", "0.5", 1], ...]. A record marked @positional crosses JSON as an array of its fields in declaration order:
@positional type Level { px: Decimal<2>; qty: Decimal<4>; orders: Int = 1; }type Depth { bids: bounded[Level; 50]; asks: bounded[Level; 50]; }
let depth = Depth::api_decode(body) or raise;let text = Depth::api_encode(depth); // {"bids":[[3000.4,0.5,1],...],"asks":[...]}Element i is read as field i would be, as strictly, so a Decimal from a string sits beside an Int from a number. Elements past the last field are skipped (a row that gains a column keeps decoding). A row that ends before a field leaves it absent: its default, an empty bounded list, or missing_field naming Level.qty; every error inside a row names its field <Type>.<field>, since a row has no keys. All fields are elements (a has_x flag is a plain Bool element), and the encoder writes them all in order. Protobuf carries the record as any other.
A positional record is the shape of a wire someone else defined, so a served surface does not carry one: a row whose request, response or error reaches it is refused by the check, because the description, the generated clients and MCP tools name a record’s fields as keys. Serve a keyed record and convert at the edge.
Serving Exposures (api::serve)
Section titled “Serving Exposures (api::serve)”An exposure (api::Exposure) binds an api surface to a transport listener inside a locus’s params block:
params { orders: Orders = Orders { }; fixture: std::api::test::Rpc = std::api::test::Rpc { }; public: api::Exposure = api::serve(Public, self.fixture, as: "public", receivers: { Orders: self.orders }, bound: 64, on_full: refuse);}Lifecycle, Receivers, and Execution Order
Section titled “Lifecycle, Receivers, and Execution Order”- Declaration & Birth Order:
api::servemust be the default initializer of a locusparamsfield (callingapi::serveinside a method body is a compile error). Declare receiver loci before theapi::Exposureparam so they are born before the exposure starts accepting calls. - Receiver Binding (
receivers:): Maps each locus type referenced in the surface to a locus instance held by the enclosing locus. When the holder has exactly one instance of a receiver type,receivers:can be omitted; if two instances of the same type exist, omittingreceivers:is a compile error. One exposure dispatches to at most 100 receiver types; a surface whose rows name more is refused atreceivers:, and is served as two surfaces. - Imported Surfaces: To serve a surface declared in an imported library seed, pass
lib::SurfaceNametoapi::serve(lib::Svc, ...). The live description reports its base name (Svc). - Lifecycle Controls:
self.public.wait()(orstd::api::run_until_stopped(self.public)) parks until the exposure stops or the process begins draining. Ifmain locusomitsrun(), the compiler generates this wait automatically.self.public.stop()stops accepting new requests, refuses queued requests asshutting_down, waits for in-flight handlers to finish, and closes the listener.
- Admission Order: Every incoming request is checked in a fixed sequence before touching receiver state:
- Caller authentication (
unauthenticated) - Surface or member digest match (
digest_mismatch) - Member existence (
malformed) - Role authorization (
unauthorized) - Payload decoding (
malformed) - Exposure queue capacity (
fullwhenboundis reached underon_full: refuse)
- Caller authentication (
- Per-Receiver Serialization: Transports read and write connections concurrently, but an exposure dispatches at most one call at a time to each receiver instance in FIFO order. Calls targeting different receiver instances run concurrently. If a receiver is draining, restarting, or replaced, calls to that receiver return
unavailablewhile other receivers on the exposure continue serving. - The
http::Restfallback is not a receiver: its requests are never queued or dispatched one at a time. Each connection calls the fallback handler on its own coroutine, asstd::http::Serverdoes, so long streams through the fallback run concurrently (see thefallback:rules below).
Listener Placement and Child Server Loci
Section titled “Listener Placement and Child Server Loci”Socket transport listeners (unix::Rpc, http::Rpc, http::Rest, mcp::Rpc, grpc::Rpc) run their accept and connection loops on an async_io coroutine pool:
- Direct Placement on
main locus: Add a placement row naming the exposure (public: cooperative(pool = io) where async_io;). The listener and its connections run onio, while the exposure state remains withmain locus. Unplaced exposures share a default per-transportasync_iopool. - Encapsulated Child Server Loci: You can group receivers, auth sources, and an
api::Exposureinside a child server locus and place that child frommain locus:
locus AdminServer { params { cfg: LiveConfig = LiveConfig { }; tokens: Tokens = Tokens { }; api: api::Exposure = api::serve(AdminApi, http::Rpc { bind: "127.0.0.1:8080", codec: json, principals: self.tokens }, as: "web", receivers: { LiveConfig: self.cfg }, bound: 64, on_full: refuse); }}
main locus App { params { admin: AdminServer = AdminServer { }; } placement { admin: cooperative(pool = io) where async_io; }}When admin is placed on an async_io pool, the server locus, its receivers, and its transport listener all run on io. A server locus holding an exposure may only be instantiated once (instantiating it twice or with replicas is rejected).
RPC Transports
Section titled “RPC Transports”1. Unix Domain Sockets (unix::Rpc)
Section titled “1. Unix Domain Sockets (unix::Rpc)”unix::Rpc serves newline-delimited JSON over a local Unix stream socket and authenticates callers using kernel peer credentials (uid, gid, pid).
main locus Desk { params { admin_roles: Grants = Grants { operator: "uid:1000" }; orders: Orders = Orders { }; ledger: Ledger = Ledger { }; admin: api::Exposure = api::serve(Admin, unix::Rpc { path: "/run/desk/admin.sock", roles: self.admin_roles }, as: "admin", bound: 16, on_full: refuse); } run() { self.admin.wait(); self.admin.stop(); }}- Parameters:
path: String(passing""leaves the exposure unbound without a socket) androles: RoleSource. - Wire Protocol: Clients send one JSON object per line and read one JSON object per reply:
{"call": "Orders::cancel", "payload": {"order": 41}, "id": "c-1", "digest": "fnv1a64:40381db6685c9f75"}{"request_id": 1, "id": "c-1", "ok": true, "value": {"order": 41, "was_open": true}, "caller": {"mode": "unix", "name": "uid:1000", "uid": 1000, "gid": 1000, "pid": 4242}}{"describe": true}Sending {"describe": true} returns the caller-filtered JSON description document.
2. HTTP RPC (http::Rpc)
Section titled “2. HTTP RPC (http::Rpc)”http::Rpc serves JSON RPC calls over HTTP/1.1 using bearer token authentication:
locus Tokens { fn principal(token: String) -> std::api::Principal { if token == "t-alice" { return std::api::Principal { mode: "bearer", name: "alice" }; } return std::api::Principal { mode: "bearer", name: "" }; } fn refused() -> String { return "no such token"; }}
main locus Desk { params { bearer: Tokens = Tokens { }; public_roles: Grants = Grants { trader: "alice" }; partner_roles: Grants = Grants { trader: "carol" }; orders: Orders = Orders { }; partner_orders: Orders = Orders { next: 9001 }; public: api::Exposure = api::serve(Public, http::Rpc { bind: "127.0.0.1:8080", codec: json, principals: self.bearer, roles: self.public_roles }, as: "public", receivers: { Orders: self.orders }, bound: 64, on_full: refuse); partner: api::Exposure = api::serve(Public, http::Rpc { bind: "127.0.0.1:8081", codec: json, principals: self.bearer, roles: self.partner_roles }, as: "partner", receivers: { Orders: self.partner_orders }, bound: 64, on_full: refuse); } placement { public: cooperative(pool = io) where async_io; } run() { self.public.wait(); self.public.stop(); self.partner.stop(); }}- Parameters:
bind: "host:port"("127.0.0.1:0"binds an ephemeral kernel port and reports the assigned port in/.description),codec: json,principals: BearerSource,roles: RoleSource, and optionalcors: "origin1, origin2"(or"*"). - Endpoints & Headers:
POST /call/<member>withAuthorization: Bearer <token>,Content-Type: application/json, and optionalHale-Surface-Digest: <digest>.GET /.descriptionreturns the caller’s filtered description document.
POST /call/Orders::place HTTP/1.1Authorization: Bearer t-aliceContent-Type: application/jsonHale-Surface-Digest: fnv1a64:a8930d6e7998e986
{"symbol": "ACME", "qty": 10, "limit": 12500}
HTTP/1.1 200 OKContent-Type: application/json
{"order":41,"notional":125000}- HTTP Status Mapping:
200 OK: Handler returned a result.422(or@not_found404,@conflict409,@forbidden403,@unavailable503withHale-Error-Class): Handler returned a declaredfallible(E)error.400(malformed),401(unauthenticated),403(unauthorized),409(digest_mismatch),429(full),503(unavailable/shutting_down): Exposure refused the call; body is{"refusal": {"kind": "...", "reason": "..."}}.500 Internal Server Error: Handler triggered aClosureViolation; body is{"refusal": {"kind": "server"}}.
- Browser CORS (
cors:): Settingcors: "https://app.example.com, http://localhost:5173"(or"*") answers browserOPTIONSpreflight requests from listed origins with204 No Content(Access-Control-Allow-Origin,Access-Control-Allow-Methods: POST, GET, OPTIONS,Access-Control-Allow-Headers: Authorization, Content-Type, Hale-Surface-Digest,Access-Control-Max-Age: 600) and attachesAccess-Control-Allow-Originto all responses (including refusals) for listed origins. Unlisted origins receive403on preflight and no CORS headers.
3. HTTP REST Routing (http::Rest and fallback)
Section titled “3. HTTP REST Routing (http::Rest and fallback)”When external callers or load balancers expect custom HTTP methods and URL paths (GET /healthz, GET /v1/items/{id}, DELETE /v1/items/{id}), annotate surface rows with at "<METHOD> <path>" and optional status: <code>, then serve the surface using http::Rest:
type Health { status: String; }type ItemKey { id: Int; }
api Gateway { rpc Probe::live at "GET /healthz"; rpc Items::get at "GET /v1/items/{id}"; rpc Items::remove at "DELETE /v1/items/{id}" status: 204; rpc Items::create at "POST /v1/items" status: 201;}
locus Svc { params { bearer: Tokens = Tokens { }; grants: Grants = Grants { }; web: api::Exposure = api::serve(Gateway, http::Rest { bind: "0.0.0.0:8080", codec: json, principals: self.bearer, roles: self.grants, anonymous: "public" }, as: "web", receivers: { Probe: self.probe, Items: self.items }, bound: 64, on_full: refuse); }}- Request Assembly:
http::Restpopulates the handler’s request struct from three sources simultaneously:- Path segment parameters (
{id}populates fieldid, parsed as the field’s declared type). - URL query parameters (
?limit=3populates scalar fieldlimit). - JSON request body fields.
- Path segment parameters (
- Routing & HTTP Semantics:
status:sets the HTTP success status (200by default;204omits the response body).- Unmatched paths return
404; matched paths called with an unsupported method return405 Method Not Allowedwith anAllowheader;HEADexecutesGETand omits the body;OPTIONSandcors:are handled per route. - Rows without
at "..."remain reachable atPOST /call/<member>. - Static segments take precedence over
{param}segments (GET /items/openalongsideGET /items/{id}is valid), whereas ambiguous overlapping patterns (GET /p/{a}/xvsGET /p/y/{b}) are rejected at compile time. - Routes are folded into both the surface and member contract digests.
- Cross-Origin Writes: a row refuses a body that is not
Content-Type: application/jsonwith415(what a cross-site form posts,text/plainor form-encoded, with no preflight), and a write with no body (aDELETE) from anOriginthatcors:does not list with403. Your own clients send the header and noOrigin; a page the exposure serves itself sends anOriginnaming the request’sHost(http://orhttps://) and passes without being listed; a page of yours on another origin is listed incors:. The fallback is not guarded: it guards itself. - Body Cap (
max_request_bytes:): a body is at most 1 MiB unless the transport says more;max_request_bytes: 12582912raises it, up to 1 GiB, asstd::http::Server’s does. A request declaring more is413before its body is read. The cap is the listener’s, so the fallback has it too. - Unauthenticated Probes (
anonymous:): Settinganonymous: "public"assigns principal"public"when a request omits theAuthorizationheader (requests with an invalid token still receive401). Protected rows (requires: [operator]) still refuse"public"unless the role source grants"public"that role.
Custom Non-RPC Endpoints (fallback:)
Section titled “Custom Non-RPC Endpoints (fallback:)”To serve raw HTTP endpoints (such as Server-Sent Events, raw streaming proxies, or custom WebSocket upgrades) on the same port and origin as http::Rest, pass a locus implementing std::http::Handler (fn handle(req: std::http::Request) -> std::http::Response) as fallback::
locus DataPlane { fn handle(req: std::http::Request) -> std::http::Response { if req.path == "/v1/events" { let w = std::http::SseWriter { conn_fd: req.conn_fd }; w.event(std::http::SseEvent { event: "ready", data: "{}" }) or discard; return w.end(); } return std::http::Response { status: 404, body: "no such path" }; }}
main locus Svc { params { bearer: Tokens = Tokens { }; data_plane: DataPlane = DataPlane { }; web: api::Exposure = api::serve(Gateway, http::Rest { bind: "0.0.0.0:8080", codec: json, principals: self.bearer, fallback: self.data_plane }, as: "web", receivers: { Probe: self.probe, Items: self.items }, bound: 64, on_full: refuse); } placement { web: cooperative(pool = edge) where async_io; data_plane: cooperative(pool = edge) where async_io; }}Rules for fallback:
- Unmatched paths only: Any path claimed by a surface route stays owned by
http::Restfor all HTTP methods (e.g.,POST /healthzon aGET /healthzroute returns405, never reachingfallback). - Independent authentication:
fallbackbypasses the exposure’sprincipals,anonymous,roles, andcorschecks; it receives the rawstd::http::Requestand performs its own header inspection. - Same pool placement:
fallbackexecutes directly on the listener’s connection coroutines and must be placed on the exact sameasync_iopool as the exposure (edgeabove). - Field reference only: When used inside a child server locus,
fallback:must reference a param field (self.front) orself, never a method call (self.make()). - Concurrent, like
std::http::Server: the fallback is not a receiver, so the one-call-per-receiver rule does not apply. Each connection callshandleon a coroutine of its own and calls interleave wherever one parks (asleep, a socket read, an outbound request, an event sent to a slow client): fifty one-second requests, or fifty Server-Sent Event streams, finish in about a second, while the rows keep their per-receiver order. Calls never run in parallel (one pool, one thread), but a value the handler read from its ownparamsbefore a park can be stale after it, so keep per-request state in locals. The bound is the listener’s: 256 connections served at once and as many waiting, shared with the rows. The exposure’sbound:counts only calls queued for rows, so a stream that lasts minutes never takes one of its slots.
4. Model Context Protocol Server (mcp::Rpc)
Section titled “4. Model Context Protocol Server (mcp::Rpc)”mcp::Rpc exposes a surface directly as an MCP server over HTTP (POST /mcp speaking JSON-RPC 2.0):
public: api::Exposure = api::serve(Public, mcp::Rpc { bind: "127.0.0.1:8090", principals: self.bearer, roles: self.public_roles }, as: "public", receivers: { Orders: self.orders }, bound: 64, on_full: refuse);tools/listreturns the caller’s authorized rows as MCP tools, replacing::with__(Orders__place, escaping identifier underscores where necessary).tools/callvalidates arguments and invokes the handler:
POST /mcp HTTP/1.1Authorization: Bearer t-alice
{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"Orders__place","arguments":{"symbol":"ACME","qty":10,"limit":12500}}}
{"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","text":"{\"order\":41,\"notional\":125000}"}],"structuredContent":{"order":41,"notional":125000},"isError":false}}- JSON-RPC Error Codes:
-32001(HandlerError),-32602(malformedpayload),-32005(unauthorizedrole),-32603(ServerError/ closure violation). Unauthenticated requests return HTTP401.
5. gRPC over HTTP/2 (grpc::Rpc)
Section titled “5. gRPC over HTTP/2 (grpc::Rpc)”grpc::Rpc serves unary RPCs over cleartext HTTP/2 (powered by built-in nghttp2 on the async_io pool), accepting both Protobuf (application/grpc or application/grpc+proto) and JSON (application/grpc+json) framing on the same listener:
public: api::Exposure = api::serve(Public, grpc::Rpc { bind: "127.0.0.1:8070", codec: json, principals: self.bearer, roles: self.public_roles }, as: "public", receivers: { Orders: self.orders }, bound: 64, on_full: refuse);- Path & Metadata: Calls use
:path /<Surface>/<Locus>__<method>(or/<Surface>/<Locus>.<method>),authorization: Bearer <token>, and optionalhale-surface-digest: <digest>.
:method POST :path /Public/Orders__place content-type application/grpc+jsonauthorization Bearer t-alice hale-surface-digest fnv1a64:a8930d6e7998e986message {"symbol": "ACME", "qty": 10, "limit": 12500}
:status 200 content-type application/grpc+jsonmessage {"order":41,"notional":125000}trailers grpc-status 0- Generated
.proto& Server Reflection:hale api export(orhale check --api --surface Public --proto) generates a proto3 definition with deterministic field numbers matching declaration order andoptionalscalars so missing fields are distinguished from zero values:
service Public { // rpc Orders::place // requires no role // a violation is the server error (ClosureViolation): status 13, no handler error rpc Orders__place(OrderRequest) returns (OrderResponse);}
message OrderRequest { optional string symbol = 1; optional int64 qty = 2; optional int64 limit = 3; // Hale: Money, q(cent)}- If two fields in a Hale struct collide under proto3’s case-insensitive camelCase rules (
a_bandaB, oraand_a), the compiler rejects the struct unless one field is renamed with a`json:"..."`tag. If a record shares the surface’s name (api Todowithtype Todo), its proto message is namedTodoMessage. - Every
grpc::Rpclistener serves gRPC Server Reflection (grpc.reflection.v1) andhale.api.Description/Describe(authenticated by theauthorizationmetadata), allowing tools likegrpcurlto discover schemas without local.protofiles. - gRPC Status & Error Details:
HandlerError: ReturnsFAILED_PRECONDITION(or the@not_found/@conflict/@invalid/@forbidden/@unavailablegRPC status) with agoogle.rpc.Statuspayload ingrpc-status-details-binpacking the error message (or JSON) inAny.Refusal: ReturnsINVALID_ARGUMENT,FAILED_PRECONDITION(digest mismatch),UNAUTHENTICATED,PERMISSION_DENIED,RESOURCE_EXHAUSTED, orUNAVAILABLEwithHaleRefusalingrpc-status-details-bin.ServerError: ReturnsINTERNAL.- Calling
public.stop()drains in-flight calls, sendsGOAWAYon all HTTP/2 connections, and closes the listener. Callingstd::io::tcp::stop("127.0.0.1:8070")closes the listener while allowing active connections to continue.
Streaming Hubs (ws::Hub and udp::Hub)
Section titled “Streaming Hubs (ws::Hub and udp::Hub)”To stream internal bus topics to external subscribers, declare a ws::Hub (WebSocket) or udp::Hub (UDP datagrams) in params and bind topics to it in a bindings { } block:
main locus Desk { params { bearer: Tokens = Tokens { }; hub_roles: Grants = Grants { operator: "dave" }; hub: ws::Hub = ws::Hub { bind: "127.0.0.1:9000", principals: self.bearer, roles: self.hub_roles, as: "fills" }; } bindings { Fills: self.hub requires: [operator], bound: 64, on_full: drop_old; }}The hub is built wherever its literal is written in the main locus’s params, with or without a bindings { } row or a surface served over it (api::serve(Public, self.hub, …)): a hub with no stream row listens and carries the rpcs served over it. A ws::Hub literal in any other locus is refused, naming the main locus as its place.
Hub Protocol and Queue Semantics
Section titled “Hub Protocol and Queue Semantics”- Configuration: A hub param must keep its default constructor literal (overriding
Desk { hub: ... }is rejected). Published events must fit within the 65,536-byte bus frame limit (including header). - Imported Main Loci: A seed whose
main locusholds a hub can be imported (an application’s tests import the application); the imported hub never runs. Constructing that imported main locus (lib::Desk { }) is a compile error, as anapi::servethere is: only the entry program’s main locus binds streams. - Queue Shedding (
on_full): Stream bindings requireon_full: drop_oldoron_full: drop_new(refuseis not valid for streams because slow subscribers shed events rather than blocking publishers). Every event offered to a subscription incrementsseq, so any gap in receivedseqnumbers indicates the exact number of shed frames. - WebSocket Frame Protocol: Clients authenticate during the HTTP upgrade (
Authorization: Bearer <token>or?access_token=<token>) and send a subscribe frame:
{"type": "subscribe", "topic": "Fills"}{"type": "subscribed", "topic": "Fills"}{"type": "event", "topic": "Fills", "seq": 1, "payload": {"order": 1, "qty": 1, "price": 10}}{"type": "refusal", "topic": "Fills", "refusal": {"kind": "unauthorized", "reason": "Fills requires operator", "requires": ["operator"]}}- Live Credential Expiry & Role Revocation: If a subscriber’s credential expires, or if the role source announces a policy revision (by publishing
std::api::Revisionto"__api.roles.revision"or callingself.hub.announce(n)) under which the subscriber no longer holds the required role, the hub immediately sends{"type": "unauthorized", "topic": "Fills", "reason": "expired"}(or"revoked") and drops all queued frames for that subscription. - Inspection & Shutdown: Call
self.hub.subscribers(),self.hub.connections(), andself.hub.events()to inspect hub metrics. Callingself.hub.stop()flushes queued events and sends{"type": "closed", "reason": "shutting_down"}. - Multiplexing RPC over
ws::Hub: You can also pass aws::Hubas the transport inapi::serve(Public, self.hub, as: "desk", ...). Clients can then send{"type": "call", "id": "c1", "call": "Orders::place", "payload": {...}}frames and receive{"type": "reply", "id": "c1", "ok": true, "value": ...}frames interleaved with stream events on the same WebSocket connection. - UDP Streams (
udp::Hub): Carries streams only. Subscribers send a UDP datagram with a client-chosenidand bearer token; lost datagrams appear as gaps inseq.
Mounting a Hub on an http::Rest Listener (hubs:)
Section titled “Mounting a Hub on an http::Rest Listener (hubs:)”A ws::Hub does not need a port of its own. Leave out its bind: and list it in the hubs: of an http::Rest exposure, at a path. The exposure may be the main locus’s own, or held by a locus below it (see below). The REST surface, the hub, and any fallback: then share one address and one origin:
main locus Desk { params { bearer: Tokens = Tokens { }; hub_roles: Grants = Grants { operator: "dave" }; hub: ws::Hub = ws::Hub { principals: self.bearer, roles: self.hub_roles, as: "fills" }; web: api::Exposure = api::serve(Gateway, http::Rest { bind: "0.0.0.0:8080", codec: json, principals: self.bearer, roles: self.hub_roles, hubs: [("/admin/v1/hub", self.hub)] }, as: "web", bound: 64, on_full: refuse); } bindings { Fills: self.hub requires: [operator], bound: 64, on_full: drop_old; } placement { web: cooperative(pool = edge) where async_io; }}- Routing: only a WebSocket upgrade (
Upgrade: websocket) at the mounted path goes to the hub. Any other request at that path, and every other path, is routed by the REST table as usual (a row, thefallback, or404). - The hub keeps its own gate: the hub’s
principals,roles, and each stream’srequires:,bound:andon_full:judge the connection exactly as on a listener of its own. The credential is stillAuthorization: Beareror?access_token=on the upgrade. The REST exposure’s bearer and roles judge only its rows. - No second listener: the hub opens no socket. Its connections run on the REST listener’s
async_iopool. At most 256 are open at once, and an upgrade beyond that gets503. A handed-over connection waits for its subscriber for as long as the subscriber is quiet; the 5-second read limit of a REST request does not apply to it.self.hub.stop()closes them withclosedas before. - Description: the REST exposure’s
GET /.descriptionlists the hub’s streams beside its members, filtered by the caller’s roles, andhale check --api --exposure webshows the same. The document does not state the mount path yet. - Rules: the hub is a
ws::Hubparam of themain locuswith at least one stream bound to it, mounted once and with nobind:. The path is letters, digits,-._~and/, with no trailing/and no{param}. It is not/.description, and no row of the surface may serve it by any method. Audp::Hubcannot be mounted.
A hub mounted on an exposure held below the main locus
Section titled “A hub mounted on an exposure held below the main locus”When the http::Rest exposure lives in a sub-locus, for example so that the exposure, its receivers and its store share one async_io pool, the hub still belongs to the main locus. The sub-locus declares a ws::Hub param with no default, and the main locus passes its hub down:
locus Front { params { hub: ws::Hub; bearer: Tokens = Tokens { }; grants: Grants = Grants { operator: "dave" }; web: api::Exposure = api::serve(Gateway, http::Rest { bind: "0.0.0.0:8080", codec: json, principals: self.bearer, roles: self.grants, hubs: [("/admin/v1/hub", self.hub)] }, as: "web", bound: 64, on_full: refuse); }}
main locus Desk { params { bearer: Tokens = Tokens { }; hub_roles: Grants = Grants { operator: "dave" }; hub: ws::Hub = ws::Hub { principals: self.bearer, roles: self.hub_roles, as: "fills" }; front: Front = Front { hub: self.hub }; } bindings { Fills: self.hub requires: [operator], bound: 64, on_full: drop_old; } placement { front: cooperative(pool = edge) where async_io; }}- The hub’s literal, its
principalsandroles, and its stream bindings stay in themain locus. Only the connections move: the REST listener runs where themain locusplacesfront(hereedge), and the hub connections it takes over run there too. - Every construction between the
main locusand the holder passes the hub on withself.<param>. The checker refuses a mount whose param is not passed themain locus’s hub, and one whose param builds a hub of its own. - The hub can be mounted by only one exposure in the program, whichever locus holds it.
Authentication, Authorization & Refusals
Section titled “Authentication, Authorization & Refusals”Refusal Kinds
Section titled “Refusal Kinds”A transport refusal rejects a request before the handler runs:
{"request_id": 4, "id": 4, "ok": false, "refusal": {"kind": "malformed", "reason": "wrong_type: qty"}}| Refusal Kind | HTTP Status | gRPC Status | Cause |
|---|---|---|---|
malformed |
400 |
INVALID_ARGUMENT |
Invalid JSON/frame, unknown member, missing required field (missing_field), wrong field type (wrong_type), list bound violation (too_long / too_short), or duplicate map key (duplicate_key). |
unauthenticated |
401 |
UNAUTHENTICATED |
Missing or unrecognized credential (bearer source returned empty name, or kernel peer credentials unavailable). |
unauthorized |
403 |
PERMISSION_DENIED |
Authenticated caller does not hold a role listed in the row’s requires: (includes "requires": [...]). |
digest_mismatch |
409 |
FAILED_PRECONDITION |
Client’s digest matches neither the surface digest nor the member’s digest (includes "served": "<digest>"). |
full |
429 |
RESOURCE_EXHAUSTED |
Exposure has reached its bound of in-flight requests under on_full: refuse. |
unavailable |
503 |
UNAVAILABLE |
Target receiver is draining, restarting, or replaced. |
shutting_down |
503 |
UNAVAILABLE |
Exposure is stopping (stop() called); queued request did not run. |
Caller Context (std::api::Context)
Section titled “Caller Context (std::api::Context)”Any RPC handler can inspect the caller and transport by declaring a second parameter of type std::api::Context (or std::api::ServedContext to also read the exposure name and receiver generation):
type RefundRequest { order_id: Int; amount: Int; }type RefundResponse { ok: Bool; by: String; }
locus Billing { fn refund(req: RefundRequest, ctx: std::api::Context) -> RefundResponse { // ctx.caller is the authenticated Principal; // ctx.via is "unix", "http", "ws", "mcp", or "local" for direct in-process calls. return RefundResponse { ok: true, by: ctx.caller.name }; }}
fn main() { }std::api::Context provides:
ctx.caller:std::api::Principal(mode,name,uid,gid,pid).ctx.request_id: MonotonicIntassigned by the exposure.ctx.via: Transport identifier ("unix","http","ws","mcp", or"local"when invoked directly inside Hale).ctx.role: The first role from the row’srequires:held by the caller (""if the row requires no roles).
Role Sources (std::api::RoleSource and StaticRoles)
Section titled “Role Sources (std::api::RoleSource and StaticRoles)”Role requirements (requires: [refund_support]) are declared on surface rows, while role hierarchy (role owner includes refund_support;) is declared at top level:
role refund_support;role auditor;role owner includes refund_support;
api Billing { rpc Billing::refund requires: [refund_support]; rpc Billing::ledger requires: [auditor];}Who holds a role at runtime is determined by the roles: locus passed to the transport, which must satisfy std::api::RoleSource (fn holds(p: std::api::Principal, r: String) -> Bool; the exposure walks includes automatically):
locus RecordRoles { // satisfies std::api::RoleSource params { root: String = "."; } fn holds(p: std::api::Principal, r: String) -> Bool { return r == "owner" && p.name == "uid:1000"; }}
main locus Head { params { roles: RecordRoles = RecordRoles { }; local: api::Exposure = api::serve(Billing, unix::Rpc { path: "/run/head.sock", roles: self.roles }, as: "head", bound: 64, on_full: refuse); } run() { self.local.wait(); self.local.stop(); }}
fn main() { Head { }; }For static or environment-driven deployments, use std::api::StaticRoles, which reads a semicolon-separated grant table (role=member,member;...) from its table parameter or the LOTUS_API_ROLES environment variable:
StaticRoles Member Pattern |
Matches |
|---|---|
uid:<n> |
Unix socket peer with user ID <n>. |
gid:<n> |
Unix socket peer with primary or supplementary group ID <n>. |
user:<name> |
Unix socket peer with account <name> (resolved once at startup). |
group:<name> |
Unix socket peer in group <name> (resolved once at startup). |
bearer:<name> |
HTTP, MCP, or Hub caller whose BearerSource returned principal <name>. |
* |
Any authenticated caller on any transport. |
Unix account patterns (uid:, gid:, user:, group:) never match bearer callers, and bearer: patterns never match Unix socket peers.
CLI Driving & Client Code Generation
Section titled “CLI Driving & Client Code Generation”Driving Live Exposures from the Terminal
Section titled “Driving Live Exposures from the Terminal”Five built-in CLI commands interact with any live exposure by reading its self-description ({"describe": true} on Unix sockets, GET /.description on HTTP and WebSocket hubs):
# 1. Inspect caller-visible descriptionhale describe /run/desk/admin.sockhale describe http://127.0.0.1:8080 --bearer t-alice
# 2. Invoke RPC members (flags per field, or raw JSON)hale call /run/desk/admin.sock Orders::cancel '{"order": 41}'hale call http://127.0.0.1:8080 Orders::place --symbol ACME --qty 10 --limit 12500 --bearer t-aliceecho '{"book": "desk"}' | hale api call unix:/run/desk/admin.sock Ledger::rebalance --json -
# 3. Call HTTP REST routes directlyhale api call http://127.0.0.1:8080 'GET /v1/items/42'
# 4. Subscribe to WebSocket hub streamshale watch ws://127.0.0.1:9000 Fills --token t-dave
# 5. Launch loopback web dashboard or MCP stdio bridgehale admin /run/desk/admin.sockclaude mcp add desk -- hale mcp --app http://127.0.0.1:8080 --token t-alice- Typed Flags vs.
--json:hale callvalidates--<field>flags against the exposure’s JSON Schema before sending (exiting with code5on client-side schema mismatch). Passing--json '{...}'(or--json -for stdin) sends raw JSON directly to the server. - Reserved Flags:
hale callreserves--json,--bearer,--digest,--raw, and--request-id(defaults tohale-<pid>). If a request payload has a field matching one of those five names, pass it via--json(a field namedidcan be passed normally as--id 5). - Exit Codes:
0(Resulton stdout),1(HandlerErroron stderr),2(Refusalon stderr),3(ServerErroron stderr),4(Lost/ connection failure),5(client-side refusal before sending). Passing--rawprints the full wire envelope to stdout while preserving the exit code.
Exporting Specification Bundles (hale api export)
Section titled “Exporting Specification Bundles (hale api export)”hale api export writes all specification artifacts for a surface to a directory deterministically:
hale api export --surface Public --out api/public desk.hlhale api export --surface Public --check api/public desk.hl # exits 1 on drift in CIGenerated files in api/public/:
Public.description.json: Full surface description, schemas, exposures, and hubs.Public.openapi.json: OpenAPI 3.1 specification, with one relative server ("url": "/"), since where the exposure listens is a deployment fact; a linter’sno-empty-serverspasses, and a client points it at the exposure’s address.Public.json-schema.json: Standalone JSON Schema bundle.Public.mcp.json: MCP tool definitions.Public.proto: Protobuf v3 service and message definitions forgrpc::Rpc.DIGEST: The surface contract digest and compiler version.
Generating Typed Clients (hale api client)
Section titled “Generating Typed Clients (hale api client)”Generate standalone, zero-dependency client modules for Hale, TypeScript, or JavaScript (ES module with JSDoc types):
hale api client --surface Public --lang hale --out client/desk.hl desk.hlhale api client --surface Public --lang ts --out client/desk.ts desk.hlhale api client --surface Public --lang ts --check client/desk.ts desk.hl # exits 1 on drifthale api client --surface Public --lang js --out web/desk.js desk.hl| Target | Supported Transports | RPC Return Shape | Stream Shape |
|---|---|---|---|
Hale (--lang hale) |
unix:PATH, http://, ws:// |
<Member>Outcome enum: Result(T), HandlerError(E), Refusal(R), ServerError, Lost(String) |
Subscription locus (start(), next() -> Event(seq, payload) | Expired | Revoked | Closed) |
TypeScript (--lang ts)JavaScript ( --lang js) |
http://, ws:// |
Tagged union: result, handler_error, refusal, server_error (throws TransportError if connection drops mid-call) |
Async iterable (for await (const ev of ...)) |
Every generated client embeds the per-member digest on each call and exposes the surface digest as a constant.
Consuming External APIs
Section titled “Consuming External APIs”Importing OpenAPI and MCP Specifications (hale api import)
Section titled “Importing OpenAPI and MCP Specifications (hale api import)”Use hale api import to generate a typed Hale api declaration from an external OpenAPI 3.x document (JSON or YAML, local file or URL) or a live MCP server:
hale api import https://shelf.example/openapi.json --as Shelf --out shelf.hlhale api import ./vendor/shelf.json --as Shelfhale api import ./vendor/shelf.yaml --as Shelfhale api import http://127.0.0.1:8931/mcp --as ToolsThis generates a source file declaring the external surface and its wire types:
// Generated by `hale api import` from openapi ../shelf.json (OpenAPI 3.0.3: Shelf 1.0.0).
api Shelf from openapi "../shelf.json" { rpc create_item(ShelfNewItem) -> ShelfItem wire: "POST /items"; rpc get_item(ShelfGetItemRequest) -> ShelfItem wire: "GET /items/{itemId}"; // response: std::json::Value, an array with no `maxItems`, and a record holds a bounded list only rpc list_items() -> std::json::Value wire: "GET /items";}
type ShelfGetItemRequest { item_id: Int `json:"itemId"`;}
type ShelfItem { id: Int; name: String; state?: ShelfItemState;}
type ShelfItemState = enum { open, held, gone };
type ShelfNewItem { name: String;}- Mapping Rules: Operations map to snake_case
rpcrows withwire: "METHOD /path?query_param". Non-required properties generate optional fields (name?: T;, thehas_<field>presence pair); non-identifier JSON keys use`json:"..."`tags; a string enum whose values are not all identifiers becomes PascalCase variants tagged with their values (StopLimit `json:"stop-limit"`); schemas that cannot map to fixed Hale structs (oneOf, unbounded arrays withoutmaxItems, free-form objects, or schemaless MCP outputs) fall back tostd::json::Valuewith an explanatory comment. - Supported YAML Subset: The built-in YAML reader follows the YAML 1.2 Core Schema (so
onandyesare strings, not booleans) and supports block/flow mappings and sequences, plain/quoted scalars,|and>block scalars, comments, and a single---document marker. Anchors/aliases (&,*), merge keys (<<), tags (!), complex keys (?), duplicate keys, and multi-document streams are rejected with a line-numbered diagnostic.
Calling External Surfaces (api::client and VaultPrincipals)
Section titled “Calling External Surfaces (api::client and VaultPrincipals)”Bind an imported api surface (or a Hale api surface served by another process) to an api::Client param using http::Client or mcp::Client:
locus Desk { params { creds: std::api::VaultPrincipals = std::api::VaultPrincipals { slots: "editor=shelf-editor, *=shelf-reader" }; shelf: api::Client = api::client(Shelf, http::Client { base: "https://shelf.example", principals: self.creds }); } fn label(id: Int) -> String { return match self.shelf.get_item(ShelfGetItemRequest { item_id: id }) { ShelfGetItemOutcome::Result(item) -> item.name, ShelfGetItemOutcome::HandlerError(f) -> "failed: " + f.class, ShelfGetItemOutcome::Refusal(r) -> "refused: " + r.kind, ShelfGetItemOutcome::ServerError -> "server error", ShelfGetItemOutcome::Lost(why) -> "lost: " + why, }; }}- Outcome Matching: Each row becomes a method returning
<Surface><Row>Outcome. HTTP404maps toHandlerErrorwithclass: "not_found",401/403/429map toRefusal, and5xxmaps toServerError. When calling a Hale-authored surface overapi::client,HandlerErrorcarries the server’s typed error enum directly. - Secret Vault Credentials (
std::api::VaultPrincipals): Maps surface roles to secret vault slots ("editor=shelf-editor, *=shelf-reader"), fetching the bearer token from the vault on each outbound request so raw tokens never appear in source or locus state. - Effect Class (
client(Surface)): Outbound calls carry theclient(<Surface>)effect class, allowing architectural claims to restrict external API calls:
claims { desks_stay_local: forbid reaches(desks, effects(client(Shelf))); // Use `effects(client(*))` to forbid calls to any consumed API surface.}- Exporting Role-Scoped Agent Tools: Run
hale api export --surface Shelf --tools-for editorto export MCP tool definitions (Shelf.create_item,Shelf.get_item, …) filtered to the operationseditoris permitted to invoke.
Detecting Upstream API Drift (hale api drift)
Section titled “Detecting Upstream API Drift (hale api drift)”Run hale api drift in CI or a watcher to compare the upstream OpenAPI/MCP specification against your committed api rows by structural shape:
$ hale api drift .drift Shelf added archive_itemdrift Shelf removed create_itemdrift Shelf changed get_item response$ echo $?1- Exits
0if all rows match (Shelf: in step with ../shelf.json (4 rows)),1if any row wasadded,removed, orchanged(request,response, orwire), or2if the upstream document cannot be read. - Pass
hale api drift --documentto output the upstream specification as canonical, key-sorted JSON regardless of whether the source was YAML or JSON.
Current Limitations
Section titled “Current Limitations”- Unsupported Wire Field Types:
Bytes, unbounded slices, and locus types cannot appear in wire records. - Batch Handlers:
Drain<T>batch handlers cannot be exposed asrpcrows. - Non-Object MCP Requests: An
rpcrow whose request type is a top-level enum rather than a struct has no MCP tool projection. - Hub Live Descriptions: When a
ws::Hubalso serves anapisurface viaapi::serve, its liveGET /.descriptionlists its stream bindings; the RPC surface members remain in the statichale check --apiinventory. - Bearer Group Claims:
StaticRolesmatches bearer principals by individual name (bearer:<name>) or*. To authorize bearer tokens by OIDC group claims, implement a customstd::api::RoleSource.