Repository navigation
feat(service-bridge): Group D R2 commit-safe change feed (issue #171) - #15
Merged
Merged
Conversation
…st#171) Make the authz change feed the commit-safe, gap-free source the platform's R2 cutover consumes. Fork side of the paired R2 PR. Feed: - Gate the read by `xact_id < pg_snapshot_xmin(pg_current_snapshot())` and order/cursor by `(xact_id, id)` (an opaque token), so a lower bigserial that commits AFTER the consumer passed a higher one is never skipped (the classic transactional-outbox commit-ordering skip = a lost revocation = P0 fail-open). `xact_id` is the non-wrapping 64-bit xid8, a permanent total order. The event `seq` becomes diagnostic-only. - Parse the jsonb payload off the wire: postgres.js returns jsonb as a raw snake_case JSON string (it does not parse json/jsonb, and CamelCasePlugin only recurses into objects), so the R1 mapper's camelCase-object assumption never held on the real driver. The feed JSON.parses the payload and the mapper reads the stored snake_case keys. This latent R1 bug (the feed would have delivered ZERO events in prod) was unexercised because R1 is expand-only; the new real-PG test catches it. - Stale-cursor detection via an `authz_outbox_gc` high-water mark (max (xact_id,id) ever GC'd), so age-based retention can never silently skip an un-consumed event; a cursor at/below it returns 409 -> platform rebaselines. - getChanges returns `oldestPendingAgeMs` (fork-provided) for the platform's oldest-unconsumed-age alarm; head is the opaque safe frontier. Snapshot: the phase-0 response carries `baseline` = "<pg_snapshot_xmin>.0", the change cursor the platform checkpoints to after a successful reconcile (never the feed head), so a transaction in-flight at snapshot start is replayed by the live drain, not skipped. Installer: add the `xact_id xid8` column (DEFAULT pg_current_xact_id(), which also covers the legacy platform trigger during rollout) + the (xact_id, id) index + the gc high-water table, behind a fail-fast PG 13+ version invariant (the change feed needs xid8 / pg_current_xact_id / pg_snapshot_xmin). Tests: a REQUIRED real-Postgres two-connection spec (authz-outbox-commit-safety.pg.spec.ts) reproduces the actual commit-order race + the rollback case on a live engine (self-skips without AUTHZ_TEST_PG_URL); offline feed/snapshot/installer/event specs updated for the opaque cursor, snake_case payload, version invariant, and baseline. OpenAPI + README updated; redocly clean; boundary intact; fork server typecheck-builds. Co-Authored-By: Claude Opus 4.8 <[email protected]>
- oldestPendingAgeMs: an index-ordered `(xact_id, id) limit 1` head-of-line seek instead of a `min(created_at)` aggregate scan, so a bulk burst drained one-per-poll is O(M log n) not O(M^2) (perf review L3). Head-of-line age is also the truer "drainer stuck" signal. - add an anti-vacuity guard to the real-PG commit-safety spec: when the CI lane declares it REQUIRES Postgres (AUTHZ_REQUIRE_PG=1) but AUTHZ_TEST_PG_URL is unset, fail loudly instead of self-skipping to a false green (testing review L3 — a self-skipping required gate could silently lose all coverage). Co-Authored-By: Claude Opus 4.8 <[email protected]>
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.
Fork half of the paired Group D R2 (issue cccvu/wiki-v2#171). Makes the authz change feed the commit-safe, gap-free source the platform's R2 cutover consumes. All under the boundary-excluded
apps/server/src/service-bridge/+docs/integrations/(zero upstream edits).What changed
Commit-safe watermark (closes the R1 P4 gap). The R1 feed read
id > cursoron the outbox bigserial, which is at-least-once with a transactional-outbox commit-ordering skip: a lower bigserial that commits after the consumer passed a higher one is never re-read (a lost revocation = P0 fail-open). R2 fixes it entirely fork-side:xact_id xid8column (defaultpg_current_xact_id(), so both this trigger and the legacy platform trigger populate it during rollout);xact_id < pg_snapshot_xmin(pg_current_snapshot())and order/cursor by the opaque(xact_id, id)token, so a row is served only once its inserting txn (and every older one) is fully settled.xact_idis the non-wrapping 64-bitxid8, a permanent total order.Latent R1 bug fixed (found by the new real-PG test). postgres.js returns a
jsonbcolumn as a raw snake_case string (it does not parse json/jsonb, and CamelCasePlugin only recurses into objects) — so the R1 mapper's camelCase-object assumption never held on the real driver, and the feed would have delivered zero events in prod. The change feed is the only consumer and was unexercised in R1, so this shipped uncaught. The feed now JSON-parses the payload and the mapper reads the stored snake_case keys.Precise stale detection. An
authz_outbox_gchigh-water table records the max(xact_id, id)ever removed by retention; a cursor at/below it returns409 {stale, head}so the platform rebaselines rather than silently skipping a GC'd gap (amin(created_at)/min(id)heuristic could miss a late-committing higher-xid row GC'd early).Snapshot baseline + observability. The snapshot phase-0 response carries
baseline = "<pg_snapshot_xmin>.0", the change cursor the platform checkpoints to after a successful reconcile (never the feed head), so a txn in-flight at snapshot start is replayed by the live drain, not skipped.getChangesreturnsoldestPendingAgeMsfor the platform's oldest-unconsumed-age alarm;head/nextCursorare opaque tokens; the eventseqis now diagnostic-only.How this was tested
authz-outbox-commit-safety.pg.spec.ts, self-skips withoutAUTHZ_TEST_PG_URL): reproduces the ACTUAL commit-order race (a low-xid/late-bigserial txn racing a high-xid/early-bigserial txn that commits first) + the rollback case on a livepostgres:18— the consumer withholds the early row until the older txn settles, then delivers both in(xact_id,id)order with NO skip. Wired into the super repo'sdocmost-authz-pgCI job.pnpm exec jest src/service-bridge src/authzgreen.nest build(typecheck) + redocly lint (service-bridge.openapi.json) +check-upstream-boundary.shclean.Merge order
Paired with cccvu/wiki-v2#178. Merge THIS fork PR first, then repoint the super's
docmostsubmodule to this PR's merge commit before merging the super PR.🤖 Generated with Claude Code