Skip to content

test(service-bridge): real-Postgres installer and retention specs (wiki-v2 issue 174 core) - #17

Merged
PMQ9 merged 3 commits into
wiki-v2from
test/174-core-pg-specs
Sep 7, 2026
Merged

PMQ9 merged 3 commits into
wiki-v2from
test/174-core-pg-specs

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 6, 2026 •

Copy link
Copy Markdown

TL;DR

Two new real-Postgres specs under apps/server/src/service-bridge/ give the R2 change feed the executed coverage it never had: the outbox installer (legacy upgrade, idempotent fresh install, and the full trigger -> outbox -> mapper chain over real base tables) and the retention sweep (gc mark advances to the max deleted (xact_id, id), strict stale-cursor gate). The only production code change is one observability line in gc() (AUTHZ_OUTBOX_GC_SWEEP, every hourly sweep) plus doc-comment corrections on the stale gate. This is the fork half of wiki-v2 issue 174's R3-precondition core (items C.1 and C.2 of the R3 evidence gate on cccvu/wiki-v2#171).

Context

Before this PR the trigger -> outbox row path had zero executed coverage (the existing pg spec hand-creates two tables and never calls install()), and gc() plus the stale 409 had never run on a real Postgres end to end. The R2 payload-key defect behind wiki-v2 incident 181 slipped through a double-encoded fixture; these specs use real jsonb objects and the production Kysely configuration (CamelCasePlugin, bigint parser) so the same class of defect fails here.

Related issues

  • Part of cccvu/wiki-v2#174 (core, re-scoped 2026-09-06). Gate item C on cccvu/wiki-v2#171. Relates to cccvu/wiki-v2#157.
  • Paired super PR: cccvu/wiki-v2 branch test/174-core (points the submodule at this branch head; repointed to the merge commit after this merges).

What changed

  • authz-outbox-installer.pg.spec.ts (new): anti-vacuity gate; LEGACY outbox upgrade (no xact_id, rows present: sentinel backfill, NOT NULL, gc table + index, backfilled rows served by the real feed); idempotent fresh install (same inventory after install() and the public bootstrap path; one trigger per table with the documented column scoping; none on users); G1 trigger chain (real install() over real users, spaces, space_members, group_users, pages, page_access, page_permissions; every INSERT/UPDATE/DELETE lands an outbox object that the real AuthzChangeFeedService maps to the expected typed event; name/title/content edits produce no row; AUTHZ_CHANGE_EVENT_DROPPED never fires; cursor ends at the head).
  • authz-outbox-retention.pg.spec.ts (new): anti-vacuity gate; gc() reclaims only rows past the window and advances the mark to the max deleted tuple monotonically (commit-reordered pair so max-by-tuple differs from max-by-id; idle sweep is a no-op); the stale gate is strict: a cursor BELOW the mark throws StaleCursorError, AT and ABOVE pass (via the public getChanges, which the controller maps to 409). Both sides are asserted so a future > to >= edit fails.
  • Each spec owns a private schema (search_path) so the pg specs can share one database.
  • service-bridge.contract.spec.ts: the provider half of the feed-schema tether (wiki-v2 issue 179). For every AuthzChangeEvent variant a Record<keyof ..., true> key map typed against the fork type is compared as a closed set to the matching components.schemas.AuthzChangeEvent.oneOf member (properties, plus required where declared); the same for ChangesResult and SnapshotResult against the changes and snapshot response schemas. A fork-side rename fails to compile (TS2561), a document-side rename fails the comparison; both proven with a temporary rename and reverted.
  • authz-change-feed.service.ts: gc() now ends EVERY hourly sweep with one greppable line, AUTHZ_OUTBOX_GC_SWEEP removed=<n> mark=<xact_id>.<id> retentionDays=<d> (mark = the gc mark the table holds after the sweep, none until something has ever been reclaimed), so a sweep that found nothing is distinguishable from a sweep that never ran (S2). The existing reclaim detail line stays; the stale check is unchanged, its doc comments now say STRICTLY below the mark (AT the mark the reclaimed row was already consumed). Covered by the feed unit spec (no-op and reclaiming sweep via the Kysely spy) and by the retention pg spec (the real sweep logs the observed mark).
  • authz-outbox-installer.pg.spec.ts: the G1 case carries an explicit 30 s jest timeout above its 15 s drain deadline, and afterAll closes both pools with a bounded end() (each independently, via allSettled) so a timed-out case cannot wedge the worker (Kysely destroy() is an unbounded postgres.end()). Header and G1 comments now say the target is a throwaway Postgres and why the lane runs in band (pg_snapshot_xmin is cluster-wide, not schema-isolated).

Verification

  • AUTHZ_TEST_PG_URL=... pnpm exec jest --runInBand 'src/service-bridge/.*\.pg\.spec\.ts$': 3 suites, 12 tests green (commit-safety 5, installer 4, retention 3).
  • Anti-vacuity: env -u AUTHZ_TEST_PG_URL AUTHZ_REQUIRE_PG=1 pnpm exec jest ...: 3 failed, 9 skipped, exit 1 (the required failure).
  • pnpm exec jest src/service-bridge src/authz: 40 suites, 333 passed, 9 skipped. ESLint clean. check-upstream-boundary.sh exit 0. tsconfig.build.json excludes spec files (0 in --listFilesOnly).
  • Mutation evidence (each applied to production code, spec run, reverted): backfill removed -> installer fails; pages trigger un-scoped -> 2 fail; payload::text -> payload (the incident 181 class) -> 2 fail; stale > -> >= -> retention fails; mark by max id -> fails; monotonic guard removed -> fails.

Notes for review

  • The pre-existing authz-outbox-commit-safety.pg.spec.ts assumes a quiet cluster (it reads the xmin-gated feed right after its insert). Under parallel jest workers a sibling's DDL transaction pins pg_snapshot_xmin and its read withholds the row (timing dependent: reproducible deterministically by holding any xid-bearing transaction open on the cluster; in band it cannot happen). The paired super PR runs the CI lane with --runInBand; a bounded-poll hardening of that sibling stays on wiki-v2 issue 174's remainder.
  • No em or en dashes; plain hyphens throughout.

🤖 Generated with Claude Code

PMQ9 and others added 3 commits September 6, 2026 10:56
… the authz outbox (docmost#174 core)

Adds two real-Postgres jest specs under service-bridge. Both self-skip without
AUTHZ_TEST_PG_URL and FAIL (not skip) under AUTHZ_REQUIRE_PG=1 without it, the
same anti-vacuity block as authz-outbox-commit-safety.pg.spec.ts.

- authz-outbox-installer.pg.spec.ts: legacy-upgrade backfill (a pre-R2 outbox
  without xact_id and with rows present: sentinel backfill, NOT NULL, the gc
  singleton and the (xact_id, id) index; the backfilled rows are then served
  by the real feed), idempotent fresh install through install() and the public
  onApplicationBootstrap path (identical inventory, one trigger per table,
  none on users, column-scoped pages/spaces triggers), and the G1 trigger
  chain: real install() over real base tables, INSERT/UPDATE/DELETE on every
  authz table, rows asserted on the table (jsonb objects via json(), never
  double-encoded) and mapped through the real AuthzChangeFeedService under the
  production Kysely config (CamelCasePlugin, bigint parser) into the typed
  events; non-structural pages/spaces edits produce no row, the docmost#181 DROPPED
  detector never fires, and the page_access cascade no-op is pinned.
- authz-outbox-retention.pg.spec.ts: gc() invoked directly (the hourly timer
  is never awaited) reclaims only rows past the retention window, advances the
  gc mark to the max deleted (xact_id, id) TUPLE (a commit-reordered pair makes
  max-by-id wrong), and stays monotonic; the stale gate is strict: a cursor
  below the mark throws StaleCursorError, at and above the mark pass.

Each spec owns a private schema (search_path) so the three pg specs can share
one database under parallel jest workers. Mutation-checked: removing the
backfill, un-scoping the pages trigger, reading payload as jsonb instead of
text, changing > to >= in the stale check, advancing the mark by max id, and
dropping the monotonic guard each turn the relevant spec red.

Refs cccvu/wiki-v2#174 and cccvu/wiki-v2#171.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
gc() now ends every hourly sweep, reclaiming or not, with one greppable line:
AUTHZ_OUTBOX_GC_SWEEP removed=<n> mark=<xact_id>.<id> retentionDays=<d>, where
mark is the gc high-water mark the table holds after the sweep (none until
something has ever been reclaimed). Previously a no-op sweep logged nothing, so
an operator could not tell a sweep that found nothing from a sweep that never
ran (S2 observability gap on wiki-v2 issue 174). The existing reclaim detail
line is kept. The stale-cursor check itself is unchanged; its doc comments now
say what the code does: a cursor STRICTLY below the mark is stale, AT the mark
the reclaimed row was already consumed and passes.

Unit spec: the line on a no-op sweep (with the current mark, and mark=none on
the zero mark) and on a reclaiming sweep, via the fake Kysely spy. Retention pg
spec: the real sweep logs the observed mark.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…installer pg spec

Contract spec: for every AuthzChangeEvent variant, a Record<keyof ..., true>
key map typed against the fork type is compared as a closed set to the matching
components.schemas.AuthzChangeEvent.oneOf member (properties, and required when
declared); the same for ChangesResult and SnapshotResult against the changes and
snapshot response schemas. A fork-side rename fails to compile, a document-side
rename fails the comparison (both proven with a temporary rename, then
reverted). This is the provider half of the wiki-v2 issue 179 tether; the
platform consumer spec pins its client types to the same document.

Installer pg spec: the G1 case gets an explicit 30 s jest timeout above its
15 s drain deadline, and afterAll closes both pools with a bounded end() via
allSettled (Kysely destroy() is an unbounded postgres.end() that a timed-out
case could wedge). Header and G1 comments now state the target is a throwaway
Postgres and why the CI lane runs in band (pg_snapshot_xmin is cluster-wide,
not schema-isolated).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@PMQ9
PMQ9 merged commit aba15fe into wiki-v2 Sep 7, 2026
@PMQ9
PMQ9 deleted the test/174-core-pg-specs branch September 7, 2026 00:20
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