Skip to content

feat(service-bridge): Group D R2 commit-safe change feed (issue #171) - #15

Merged
PMQ9 merged 2 commits into
wiki-v2from
feat/standalone-group-d-r2
Sep 4, 2026
Merged

PMQ9 merged 2 commits into
wiki-v2from
feat/standalone-group-d-r2

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 4, 2026 •

Copy link
Copy Markdown

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 > cursor on 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:

  • add an xact_id xid8 column (default pg_current_xact_id(), so both this trigger and the legacy platform trigger populate it during rollout);
  • gate the feed by 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_id is the non-wrapping 64-bit xid8, a permanent total order.
  • behind a fail-fast PG 13+ invariant (the installer refuses to boot in remote on an older engine).

Latent R1 bug fixed (found by the new real-PG test). postgres.js returns a jsonb column 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_gc high-water table records the max (xact_id, id) ever removed by retention; a cursor at/below it returns 409 {stale, head} so the platform rebaselines rather than silently skipping a GC'd gap (a min(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. getChanges returns oldestPendingAgeMs for the platform's oldest-unconsumed-age alarm; head/nextCursor are opaque tokens; the event seq is now diagnostic-only.

How this was tested

  • REQUIRED real-Postgres two-connection concurrency proof (authz-outbox-commit-safety.pg.spec.ts, self-skips without AUTHZ_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 live postgres: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's docmost-authz-pg CI job.
  • Offline feed/snapshot/installer/event specs updated for the opaque cursor, xmin gate, snake_case payload, version invariant, gc-watermark stale, and baseline: pnpm exec jest src/service-bridge src/authz green.
  • nest build (typecheck) + redocly lint (service-bridge.openapi.json) + check-upstream-boundary.sh clean.

Merge order

Paired with cccvu/wiki-v2#178. Merge THIS fork PR first, then repoint the super's docmost submodule to this PR's merge commit before merging the super PR.

🤖 Generated with Claude Code

PMQ9 and others added 2 commits September 4, 2026 16:14
…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]>
@PMQ9
PMQ9 merged commit c5c4cd6 into wiki-v2 Sep 4, 2026
@PMQ9
PMQ9 deleted the feat/standalone-group-d-r2 branch September 4, 2026 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant