Skip to content

fix(events): refilter before /nats admits a client, and resync a gap that a pull passed - #96

Merged
bketelsen merged 4 commits into
mainfrom
irulan/t8-restore-and-gap
Nov 17, 2026
Merged

bketelsen merged 4 commits into
mainfrom
irulan/t8-restore-and-gap

Conversation

@frostyard-machines

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

Copy link
Copy Markdown
Contributor

Summary

2 fixes in lodge's event broker, each with a guard test and a correction record.

  1. A start refilters before /nats admits a client. A grant change commits in the grant store and then runs its hook, which refilters the consumers. A stop between the 2 left a restored consumer with a filter wider than the grant, and /nats admitted clients a minute before the first sweep, so a client could read events outside its grant. events.Service.Run now runs Restore first: it refilters every restored subscription from the grant store, and revokes a client that it cannot read, that has no principal or no scope events (fail closed). The token check of /nats (Service.Tokens) waits for Restore, at most 5 s, and refuses a client before it.
  2. A resync for a gap that a pull passed. The resync check saw a gap only while the consumer's own position was before the stream's first message, and a pull on an open connection moves that position past the gap. The broker now records each consumer's last known stream position (at its birth, at a restore and at each check), and publishes the resync event when the stream's first message is past that record. The check is conservative: a consumer near the end of retention can get a resync event that it did not strictly need, but no gap goes unreported.

ADR-0022 (Proposed), docs/specs/lodge-http.md, docs/design/overview.md and site/content/guides/configure.md say both.

Risk tier

Risk tier: 3 — risk:high. Matched paths:

  • internal/events/** (the grant-driven event policy): the change only narrows what a client gets. Before a start has refiltered every restored consumer from the grant store, no client is admitted. A client whose OAuth client cannot be read is revoked, not admitted with its stored filter. A cancelled start admits no client at all.
  • internal/broker/** (the broker's doors and consumers): no permission, account, import or door changes. The new code is an in-memory map of consumer positions under the existing resyncMu, and a second condition that can only add a resync event. No path removes or delays an event.

Docs housekeeping

  • Cross-links unchanged and valid (make check, internal/repocheck)
  • ADR-0022 is Proposed, edited in place

Verification

  • make check green (host)
  • make race and make cross green (make ci ran verify, race and cross green; its nsl steps client-check, ui-check and shell-check could not start, because the nsl machine does not start on the host now. This PR changes nothing under client/, ui/ or shell/; CI runs them.)
  • Revert test for each fix, in a scratch worktree (git worktree add), at a0d5c6a:
    • internal/broker/broker.go and resync.go at c3fe29e, then go test ./internal/broker/ -run 'TestResyncAfterAPullPassesTheGap|TestResyncFindsTheGapOfAConsumerBornBeforeIt|TestResyncSkipsAConsumerThatKeptUp': the first 2 fail on their resync assertions.
    • Restore reduced to markReady and the wait in Tokens removed, then go test ./internal/events/ -run 'TestRestartRefiltersBeforeAdmission|TestRestartRevokesAClientItCannotRead': both fail ("the tokens check named a client before Restore"; "the broker keeps the subscriptions"). With only the loop of Restore removed, TestRestartRefiltersBeforeAdmission fails on the filters.
    • The review's fixes, each with its fix removed: TestRestoreCancelledRevokesNothing (no parent check in restoreFailure: the subscription is deleted), TestAResyncWhosePublishFailsKeepsTheRecord, TestARePutKeepsThePositionRecord, TestThePositionRecordSurvivesARestart, TestAConsumerWithNoRecordGetsTheResync, TestAConsumerThatKeptUpGetsNoResyncAfterARestart, TestTheBirthPositionIsRecorded, TestRevokeDoesNotWaitForAResyncCheck each fail.
  • Blocking review point fixed: guard added and 1 file per finding under .memory/corrections/ (2 for the findings that this PR fixes, 7 for the cross-review's blocking points below):
    • 2026-11-16-pr-96-restart-refilters-before-admission.json: TestRestartRefiltersBeforeAdmission, TestRestartRevokesAClientItCannotRead
    • 2026-11-16-pr-96-resync-after-a-pull-passes-the-gap.json: TestResyncAfterAPullPassesTheGap, TestResyncFindsTheGapOfAConsumerBornBeforeIt, TestResyncSkipsAConsumerThatKeptUp

Review record

Coder: deepseek/deepseek-flash (directed and reviewed by anthropic/claude-opus-5-5). Reviewer: openai/gpt-6.1-sol.

Round Head Result
Review a0d5c6a Not ready, 6 blocking: a cancelled restore could revoke and mark ready; the record moved before the resync publish; an update overwrote the record; records were lost at a start; a failed birth lookup left no record; position cleanup waited for the resync lock.
Re-review 1 a72a307 All 6 fixed in code. 1 blocking: the cancellation test did not reach the lookup-error path. 1 low, not blocking: the docs named only 1 case of a record's move.
Re-review 2 566329d Ready. Blocking: none. Both findings of re-review 1 fixed.

Merge-base c3fe29ec096486dc59d436dff694a77c06951ab5. Each round covered the diff since the head before it. Every changed path was reviewed; none was excluded.

What else changed for the review: the position records live in the key-value bucket lodge_positions in lodge's own (hub) account. No client or app account imports its subjects (TestThePositionBucketIsOutOfReach). A consumer with no record after a start is taken as being at position 0, so the first start of this version can send each subscription 1 resync event, if its stream has ever dropped a message.

…that a pull passed

A grant change commits in the store before its hook refilters the
consumers, so a stop between the 2 left a restored consumer with a filter
wider than the grant, and /nats admitted clients a minute before the first
sweep. The events service now runs Restore when it starts: it refilters
every restored subscription from the grant store, revokes a client it
cannot read (fail closed), and the token check of /nats waits for it.

The resync check saw a gap only while the consumer's own position was
before the stream's first message, and a pull on an open connection moves
that position past the gap. The broker now records each consumer's last
known position (at its birth, at a restore and at each check) and sends
the resync event when the stream's first message is past that record.

ADR-0022, the HTTP spec, the overview and the configure guide say both.
@github-actions github-actions Bot added level:L1 Read every line risk:high Touches a path in .github/risk-paths.json labels Nov 17, 2026
…only on a live start

A cancelled start revokes nothing and admits no client. The position record moves only after the resync event is stored, an update never raises it, the birth record comes from the create's answer, and the records live in the hub-only key-value bucket lodge_positions, so a start keeps them; a consumer with no record is at 0. Position cleanup takes its own lock, not the resync check's.
@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 17, 2026
@bketelsen
bketelsen merged commit d2cfffe into main Nov 17, 2026
13 checks passed
@bketelsen
bketelsen deleted the irulan/t8-restore-and-gap branch November 17, 2026 01:12
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