Repository navigation
feat(broker): embed a NATS broker with JetStream for app events - #81
Merged
Merged
Conversation
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.
…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.
7 tasks done
bketelsen
approved these changes
Nov 16, 2026
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.
This was referenced Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
/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.DontListen: no TCP port, no NATS WebSocket port. A test lists the process'slistening sockets.
/run/lodge/nats.sock(grouplodge-apps, 0660) admits an app only by peer uid(
SO_PEERCRED) of an enabled app's manifest user;/natsis a WebSocket on thelodgenode's existing HTTPSlistener, with the agent kind's host,
Originand peer checks.CONNECTline: every credential out, a one-timesecret in. The server's auth callback maps the secret to the identity lodge decided.
publish with a forged reply subject (
notes.forged) stores a forged message in another app's stream; subjectpermissions cannot see a reply subject. Accounts keep each reply inside its sender's account
(
TestRepliesStayInsideTheirAccount).<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>.>.<client>-<sub>. Revoking a clientdeletes 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.
/natsrefuses every client afterINFO. The idnatsis now reserved inkit/manifest(the route/nats).Risk tier
Risk tier: 3 —
risk:highpaths, each safe as below:internal/broker/**(new, L1): the admission and permissions above; tests refuse a foreign uid, a foreign subject, aforged 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; thebroker 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):natsjoins the reserved ids; no other validation changes..github/risk-paths.json(L2): addsinternal/broker/**andbroker.goto the gateway glob.Docs housekeeping
TEMPLATE.md(ADR-0022)docs/README.mdVerification
make cigreen on the host and in nsl before the first push (verify, race, cross, client-check, ui-check, shell-check)go test ./internal/broker/ -run TestRepliesStayInsideTheirAccountfails with the accounts reverted(
Account: nilfor apps and clients): "app_notes holds 2 messages, want 0". With the app's publish permissionwidened to
>,TestAppPublishesItsOwnSubjectandTestAppOfAnotherUserIsRefusedItsSubjectsfail ("no errorcontaining Permissions Violation for Publish").
Cross-review
Coder:
deepseek/deepseek-flash(with edits byanthropic/claude-opus-5-5). Reviewer:openai/gpt-6.1-sol. Base78f5e20dafbfdf4294d14a97ecc3c27fa65aaf03.0909354CONNECT nullpanic; revoke closed connections after slow deletions; a failed revoke lost its cleanup; a subscription moved to another app orphaned a consumer529b7c3529b7c3NetConndropped the 64 KiB read limit; a WebSocket close waited for the peer's handshake. 1 low: the guide namedlodgectl statusfor broker failures7a998847a9988428d0c0928d0c0905035bbEach 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,
TestSubscribeWaitsForARevokeInProgressfails("a subscription went on while its client's revoke was deleting").