Skip to content

feat(broker): embed a NATS broker with JetStream for app events - #81

Merged
bketelsen merged 5 commits into
mainfrom
irulan/broker-foundation
Nov 16, 2026
Merged

bketelsen merged 5 commits into
mainfrom
irulan/broker-foundation

Conversation

@frostyard-machines

@frostyard-machines frostyard-machines commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

lodge embeds a NATS server with JetStream as its event broker, as the foundation of app events
(ADR-0022, Proposed). It proves the topology before any
app publishes:

  • Off by default. /etc/lodge/config.json "broker": {"enabled": true} starts it, read at start. The store is
    /var/lib/lodge/jetstream. A broken member or a failed start leaves it off with a log line; the rest of lodge serves.
  • No listener. The server runs with DontListen: no TCP port, no NATS WebSocket port. A test lists the process's
    listening sockets.
  • 2 doors, both lodge's. /run/lodge/nats.sock (group lodge-apps, 0660) admits an app only by peer uid
    (SO_PEERCRED) of an enabled app's manifest user; /nats is a WebSocket on the lodge node's existing HTTPS
    listener, with the agent kind's host, Origin and peer checks.
  • lodge reads 1 protocol line. It rewrites each connection's CONNECT line: every credential out, a one-time
    secret in. The server's auth callback maps the secret to the identity lodge decided.
  • An account per app and per client. A test of the embedded server showed that, in 1 account, a pull request or a
    publish with a forged reply subject (notes.forged) stores a forged message in another app's stream; subject
    permissions cannot see a reply subject. Accounts keep each reply inside its sender's account
    (TestRepliesStayInsideTheirAccount).
  • Permissions. An app publishes only <id>.>. A client publishes only its own consumers'
    $JS.API.CONSUMER.MSG.NEXT.<stream>.<consumer> and $JS.ACK.<stream>.<consumer>.>, and subscribes only
    _INBOX.<client>.>.
  • Consumers. lodge creates 1 durable pull consumer per client and subscription, <client>-<sub>. Revoking a client
    deletes its consumers and closes its connections (measured well under 1 s). A restart keeps the streams and the
    consumer positions, and rebuilds lodge's index from consumer metadata.
  • lodge issues no event token yet, so /nats refuses every client after INFO. The id nats is now reserved in
    kit/manifest (the route /nats).

Risk tier

Risk tier: 3 — risk:high paths, each safe as below:

  • internal/broker/** (new, L1): the admission and permissions above; tests refuse a foreign uid, a foreign subject, a
    forged reply, another client's consumer, the JetStream API, a publish by a client, and a bad token.
  • internal/gateway/{gateway,broker}.go (L1): 1 new exact route /nats, placed before the shell page case; the
    broker is nil when off (404). Existing routes are unchanged.
  • cmd/lodge/main.go (L1): the broker starts only when enabled; the socket group follows the inference socket's rule.
  • kit/manifest/** (L1): nats joins the reserved ids; no other validation changes.
  • .github/risk-paths.json (L2): adds internal/broker/** and broker.go to the gateway glob.

Docs housekeeping

  • New docs started from their category's TEMPLATE.md (ADR-0022)
  • Every new doc indexed in docs/README.md
  • Cross-links added in both directions (ADR ↔ design ↔ spec)
  • New significant decision recorded as an ADR first, in this PR
  • New or edited ADRs are fact-only: context, decision and consequences

Verification

  • make ci green on the host and in nsl before the first push (verify, race, cross, client-check, ui-check, shell-check)
  • Revert test: go test ./internal/broker/ -run TestRepliesStayInsideTheirAccount fails with the accounts reverted
    (Account: nil for apps and clients): "app_notes holds 2 messages, want 0". With the app's publish permission
    widened to >, TestAppPublishesItsOwnSubject and TestAppOfAnotherUserIsRefusedItsSubjects fail ("no error
    containing Permissions Violation for Publish").

Cross-review

Coder: deepseek/deepseek-flash (with edits by anthropic/claude-opus-5-5). Reviewer: openai/gpt-6.1-sol. Base
78f5e20dafbfdf4294d14a97ecc3c27fa65aaf03.

Round Head Result Fixed in
Initial, batch 1 (production code) 0909354 4 blocking: CONNECT null panic; revoke closed connections after slow deletions; a failed revoke lost its cleanup; a subscription moved to another app orphaned a consumer 529b7c3
Initial, batch 2 (tests, docs) + re-review 1 529b7c3 4 blocking: Subscribe went on after a failed cleanup; concurrent Subscribe calls bypassed the one-app check; NetConn dropped the 64 KiB read limit; a WebSocket close waited for the peer's handshake. 1 low: the guide named lodgectl status for broker failures 7a99884
Re-review 2 7a99884 1 blocking: Revoke waited for the lifecycle lock with no deadline 28d0c09
Re-review 3 28d0c09 1 blocking: a Subscribe that started during a revoke's deletion could be deleted by it 05035bb

Each blocking fix has a guard test that fails without it, and a record in .memory/corrections/2026-11-15-pr-81-*.json.
The review budget (1 review and 3 re-reviews) was spent; 1 extra re-review was authorized. Re-review 4 of 28d0c09..05035bb: Ready, no findings, Blocking: none.
Revert result for the last fix: with the in-progress check removed, TestSubscribeWaitsForARevokeInProgress fails
("a subscription went on while its client's revoke was deleting").

lodge runs a NATS server with JetStream in its own process, off unless
the broker member of /etc/lodge/config.json enables it. It opens no
listener: apps connect on /run/lodge/nats.sock, admitted by peer uid,
and outside clients on /nats, a WebSocket on the lodge node. lodge
rewrites each CONNECT line with a one-time secret, and every app and
client is its own account, so a reply subject cannot reach another
app's stream. lodge creates 1 durable pull consumer per client and
subscription, and a revoke deletes them and closes the connections.

ADR-0022 records the topology; the app id nats is reserved for the
route.
@github-actions github-actions Bot added level:L1 Read every line risk:high Touches a path in .github/risk-paths.json labels Nov 16, 2026
…leanup

A CONNECT line whose options are no JSON object is refused before
admission. Revoke closes the client's connections at once, deletes its
consumers concurrently within 1 s, and keeps the ones it could not
delete for the next revoke. A subscription stays on 1 app.
A subscription waits for a cleanup that a failed revoke kept, and a
client's subscribe, unsubscribe and revoke run 1 at a time. /nats keeps
its 64 KiB message cap after NetConn, and a revoke ends a WebSocket
without a close handshake.
Revoke takes no lifecycle lock and spends 1 budget of 1 s from its
entry. An epoch per client lets a subscription that a revoke overtook
delete the consumer it made, and fail.
A revoke counts itself while it deletes the client's consumers, and a
subscription of that client fails until it ends, so the revoke cannot
delete the consumer the subscription makes.
@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 16, 2026
@bketelsen
bketelsen merged commit c3b136d into main Nov 16, 2026
14 checks passed
@bketelsen
bketelsen deleted the irulan/broker-foundation branch November 16, 2026 07:21
frostyard-machines added a commit that referenced this pull request Nov 16, 2026
Takes the embedded NATS broker (#81). Both reserve an app ID: the list
is now app, device, lodge, mcp, nats and oauth. The gateway serves /nats
and the OAuth routes, and the daemon passes both the broker and the
OAuth server to it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L1 Read every line ready-for-human Reviewed, CI green: ready for Brian to review and merge risk:high Touches a path in .github/risk-paths.json

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants