Repository navigation
feat(events): grant-driven event subscriptions, resync and the broker on by default - #91
Merged
Merged
Conversation
… on by default A client with an OAuth token of scope events subscribes on /gateway/events by topic prefix. lodge gives its consumer the effective read prefixes: the principal's grant read through the app's manifest events, narrowed to the prefix, as hearth's events.ReadFilters derives them. A grant change refilters the consumer in place, a revoke ends delivery, and a consumer that retention overtook gets 1 org.frostyard.lodge.resync event. The broker is on unless the configuration turns it off (ADR-0022).
… races A subscription made while its grant changed refilters itself (a generation per principal and app); a refilter that fails stops the consumer's delivery and revokes its client, except in a sweep that lodge stops; a consumer's resync check and publish run 1 at a time; a list answers the consumer's installed filters; a bad name in DELETE is 400.
… closed on a lookup A new consumer starts with its resync subject alone and is registered; then lodge reads the grant inside the client's lock and gives it its filters, so a grant change after that read finds it. A client lookup that fails in a grant hook revokes the client, and a failed refilter that a revoke overtook deletes the consumer it wrote.
…ation revokes nothing Only a new subscription starts with its resync subject alone: a registered one keeps its filters, so a pull between the 2 steps cannot move its consumer past events its grant reads. An activation whose subscription is already gone answers 404 and leaves the client's other subscriptions.
Broker.Activate installs a new subscription's filters and, when that fails, removes it under the same lock, with a budget of its own: the cleanup can no longer remove a subscription that another request put in its place.
SubscribeWith takes an Activate callback: a new subscription is made with its resync subject alone, registered, activated and, on a failure, removed, all before the client's lifecycle lock is released, so no other request of the client can replace it in between. The activation checks that the OAuth client is still there with scope events, so a client revoked after the token check keeps no subscription (404).
Only a client that OAuth does not find is a revoked one; another lookup error during a subscription's activation goes back as it is. An earlier correction record is restored as it was, and a new one names the tests that replace the one it names.
bketelsen
approved these changes
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
Grant-driven event subscriptions on lodge's embedded broker (ADR-0022, amended in place while Proposed; roadmap Phase 9).
/gateway/events(new route, kind gateway): a client with an OAuth access token of scopeeventslists, puts and deletes its subscriptions.PUT /gateway/events/<sub>takes{"topic": "<prefix>"}; lodge makes 1 durable pull consumer whose filters are the effective read prefixes: the principal's grant read through the app's manifestevents(as hearth'sevents.ReadFiltersderives them), narrowed to the requested prefix, plus the consumer's own resync subject. A token withouteventsis 403; a topic the grant gives nothing under is 403./natsnow admits a client by its OAuth token of scopeevents(internal/events.Service.Tokens). Its broker name iso+ base32 of its client ID. A connection closes when its token expires, and each minute lodge checks open connections' tokens again.broker.Refilter): undelivered events outside a narrowed grant are filtered out; a grant that gives nothing leaves the resync subject only. Grants are read inside the client's lifecycle lock, so a subscribe and a grant change of 1 client never cross. A grant that gives more than 64 filters fails closed.lodge.<id>.>. A consumer whose position is before its stream's first message gets 1org.frostyard.lodge.resyncCloudEvent, checked when its client connects (before itsCONNECTreaches the server) and each minute.broker.enableddefaults to true;{"enabled": false}turns it off.packaging/config.example.jsonfollows.internal/appsreads the hearth manifest'sevents;internal/oauthgainsServer.Client.specs/lodge-http.md(Event subscriptions),specs/oauth.md,specs/app-contract.md,design/overview.md, roadmap Phase 9,site/content/guides/configure.md, AGENTS.md map.Risk tier
Risk tier: 3 —
risk:highpaths (each matched, and why the change is safe):internal/broker/**(L1, auth): admission now takes OAuth tokens of scopeeventsonly; accounts and subject permissions are unchanged (a client still reaches only its own consumers' NEXT and ACK). New: resync publishing by lodge's hub client onlodge.<id>.>, which no app may publish (lodgeis a reserved app ID); refilter keeps permissions and fails closed.internal/events/**(L1, new, added to the list in this PR): the filter computation; cross-checked against hearth'sevents.ReadFiltersininternal/hearthtest/events_test.go.internal/gateway/{gateway,events}.go(L1): the new route runs the MCP gateway's checks in the same order (Host, no Origin, method, peer, bearer, scope, no upgrade); a test fails each.internal/oauth/grants.go(L1): adds a read-onlyClientlookup of a live client.internal/apps/apps.go(L1): reads and validates the manifest's events; a bad event disables only that app.cmd/lodge/main.go,cmd/lodge/build_test.go(L1): wiring of the service, its hooks and its sweep; the development build test now expects the broker on by default..github/risk-paths.json(L2): addsinternal/events/**andinternal/gateway/events.go.Verification
make checkgreen (host).make raceandmake crossgreen (run as part ofmake ci).make client-check,ui-check,shell-check: not run locally (the nsl VM is not reachable from this session); this PR changes nothing underclient/,ui/orshell/, and CI runs them.make mcp-e2e: not run (nsl)./gateway/mcpandinternal/mcpgware unchanged; the new route has its own tests.internal/events/acceptance_test.go):TestSubscribeByTopic,TestOneFolderOfMany(100 events in 10 folders, a grant on 1 folder receives exactly its 10),TestNarrowing,TestRefilterAfterAnEmptyGrant,TestRevoke,TestStreamLimits,TestResync,TestSweepRevokesAGoneClient,TestTooManyFilters; route checks ininternal/gateway/events_test.go.Cross-review
Coder:
deepseek/deepseek-flash(Go), docs byanthropic/claude-opus-5-5. Reviewer:openai/gpt-6.1-sol. Base422dd8b.1e6f7456ab694f6ab694fstopDeliverycould recreate a revoked consumerb4caabd(activate-after-register replaces the generation design)b4caabdcb92cf6cb92cf69f74d8f(Broker.Activate: activate and clean up under 1 lifecycle lock, cleanup with its own budget)9f74d8fSubscribeWithandActivate, so a delete and a re-create of the same name in that gap lets a failed activation remove the replacement;Activatehas no registration identity1498766(SubscribeWithregisters, activates and cleans up under 1 lifecycle lock; the activation checks the OAuth client again)1498766promoted_towas changed. 1 low: a test can hang on its failure path896a882(liveClientwrapsErrNotFoundonly foroauth.ErrNotFound; the record restored, a new record names the replacement tests; the test puts the value back)1498766..896a882diff)896a882Each blocking point has a guard test and a record in
.memory/corrections/2026-11-16-pr-91-*.json.Revert tests (scratch worktree from the PR head, fix files reverted, tests kept;
go test -count=1 -run '<names>' ./internal/events/ ./internal/broker/):6ab694f:TestSubscribeSeesAGrantChange,TestAFailedRefilterRevokesItsClient,TestListAnswersTheConsumersFilters,TestUnsubscribeRefusesABadName,TestResyncPublishesOneEventForConcurrentChecks,TestRefilterStopsDeliveryWhenTheGrantFailsfail;TestAStoppedSweepRevokesNoClientfails with only itsctx.Err()check removed.b4caabd:TestANewSubscriptionDeliversNothingBeforeItsGrantIsRead(withInactiveforced false),TestAClientLookupFailureRevokesItsClient,TestAFailedRefilterAfterARevokeLeavesNoConsumerfail.cb92cf6:TestARePutKeepsTheBacklog,TestAnUnsubscribeDuringActivationKeepsTheOthersfail.9f74d8f:TestAFailedActivationKeepsAReplacement(service reverted to 2 steps),TestActivateCleansUpAfterItsBudget(cleanup on the spent context) fail.TestActivateCleansUpUnderOneLockchecks the outcome under concurrency; it does not tell the 2-step design apart.Revert tests of the last 2 rounds (scratch worktree, fix reverted, tests kept):
1498766:TestNoGapBetweenRegistrationAndActivationfails with the lock released and taken again around the activation point;TestARevokedClientGetsNoSubscriptionfails with the activation's client check removed.896a882:TestAClientLookupFailureDuringActivationIs503fails with every lookup error wrapped asErrNotFound.Status: Ready at
896a882(re-review 6).