Skip to content

fix(service-bridge): skip the upstream response envelope on every east-west handler (wiki-v2 incident 181) - #16

Merged
PMQ9 merged 3 commits into
wiki-v2from
fix/181-service-bridge-envelope
Sep 5, 2026
Merged

PMQ9 merged 3 commits into
wiki-v2from
fix/181-service-bridge-envelope

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 5, 2026 •

Copy link
Copy Markdown

TL;DR

Two fork-side defects behind wiki-v2 incident 181. (1) Every east-west handler (the 23 /api/service/* routes and POST /api/collab/force-disconnect) now carries upstream's per-handler @SkipTransform(), so the bodies on the wire are exactly the bare shapes service-bridge.openapi.json declares instead of Docmost's global { data, success, status } envelope; a metadata fitness assertion derived from ServiceBridgeModule and a wire-level proof through the real Fastify pipeline (with an undecorated negative control) make the class unshippable again. (2) Found by the first true end-to-end run of the change feed: the feed read the outbox payload as jsonb, postgres.js parses it to an object in the built image, and the application Kysely's CamelCasePlugin then camelCases its NESTED keys, so the R2 mapper (which reads the stored snake_case keys) silently dropped every live event (spaces with a null workspace, members and pages unmapped) while only the snapshot reconcile ever wrote tuples. The feed now reads payload::text; the real-Postgres spec's fixture is fixed (it had stored a double-encoded jsonb string scalar, which is why it passed) and pins the production shape.

Context

Production release v0.3.1 of wiki-v2 (the first with the Phase C platform client on the hot path) broke on 2026-09-05: the upstream TransformHttpResponseInterceptor registered in main.ts (unmodified upstream code) wraps every handler return value unless the HANDLER carries SKIP_TRANSFORM metadata (it reads the handler only, never the class). No service-bridge handler opted out, so the platform read undefined everywhere: authorization sync dead (page.events is not iterable), /v1 500, the platform running on the placeholder workspace id, and a duplicate root admin seeded under it. The fork side of the fix is this PR; the platform side (strict client, fail-closed workspace resolution, residue repair, deploy smoke) is the paired wiki-v2 PR. Full investigation, corrections to the original write-up, and the plan: cccvu/wiki-v2#181.

Related issues

  • Relates to cccvu/wiki-v2#181 (incident record; stays open until the v0.3.2 rollout is verified in prod).
  • Relates to cccvu/wiki-v2#157 (standalone boundary epic) and cccvu/wiki-v2#171 (Group D tracker).

What changed

  • @SkipTransform() (import of the upstream decorator, applied exactly as upstream does on health.controller.ts) on all 23 route handlers across the six service-bridge/*.controller.ts files and on authz/collab/collab-disconnect.controller.ts forceDisconnect. Handler level only: a class-level decorator would be a silent no-op.
  • service-bridge/service-scope-coverage.spec.ts: the controller list is now derived from ServiceBridgeModule metadata (pinned against the reviewed named set), and a new it.each asserts SKIP_TRANSFORM metadata on every route handler, so a new route or a new controller cannot ship envelope-wrapped. service-bridge.contract.spec.ts derives its list the same way. collab-disconnect.controller.spec.ts covers the sibling handler.
  • New service-bridge/service-bridge.wire.spec.ts: boots a NestFastifyApplication with setGlobalPrefix('api') and the real TransformHttpResponseInterceptor (as main.ts does), asserts three representative routes answer the bare spec bodies as application/json, and keeps an UNdecorated fixture route that must come back wrapped (negative control: the test cannot pass vacuously).
  • service-bridge/authz-change-feed.service.ts: readRawAfter selects payload::text as payload_json and parses it, so no result-key plugin can rewrite the data; the mapper is unchanged (snake_case). Doc comments corrected: the R1 note that CamelCasePlugin recurses into the payload was RIGHT for the production read path; R2 observed a string only because its fixture passed a pre-stringified JSON string to a ::jsonb parameter, which postgres.js stringifies again (a jsonb string scalar).
  • service-bridge/authz-outbox-commit-safety.pg.spec.ts: the fixture now stores a real jsonb object via the postgres.js json helper with a jsonb_typeof(payload) = 'object' guard, plus a spaces case asserting workspaceId survives the app-shaped Kysely (PostgresJSDialect + CamelCasePlugin). Against the previous feed this spec FAILS (4 of 5 cases, including the pre-existing delivered-row case); it passes now. service-bridge/authz-change-feed.service.spec.ts: a structural test pins payload::text in the feed's SQL and the spy rows answer payload_json as a string.
  • docs/integrations/authorization/service-bridge.openapi.json info.version 1.1.0 to 1.1.1 with two description sentences: the bare-body guarantee, and the pre-setup contract (until first-run setup creates a workspace row, every operation answers the upstream 404 "Workspace not found", the only response a consumer may treat as not-provisioned). README.md gets the same note. Schemas unchanged.
  • No upstream-owned file is touched (all edits under the boundary-excluded prefixes apps/server/src/service-bridge/, apps/server/src/authz/, docs/integrations/).

How this was tested

  • RED first: the fitness assertion failed on all six controllers and the collab handler (Received: undefined), and the wire spec's three route assertions failed with exactly the four envelope lines while the negative control passed.
  • GREEN: cd docmost/apps/server && pnpm exec jest src/authz src/service-bridge src/editor-compat = 39 suites, 339 passed.
  • Real-Postgres feed spec run against a Postgres 18 in an isolated local compose project (AUTHZ_TEST_PG_URL): RED with the previous feed (4 of 5 cases), GREEN now (5 of 5). The production read path was reproduced INSIDE the built fork image (docker exec ... node -e with the image's own kysely, kysely-postgres-js and postgres modules and the app's postgres() options): select payload arrives as an object with workspaceId / deletedAt, select payload::text arrives as a string whose parse keeps workspace_id / deleted_at.
  • End-to-end on the isolated stack and in the paired wiki-v2 PR's content-smoke dispatch: after a space create the live feed now projects the space, its members, page structure, restriction markers and page ACL grants and revokes within seconds (before this fix the relay advanced its cursor and wrote nothing).
  • bash scripts/check-upstream-boundary.sh from the super: "Upstream boundary intact" (exit 0).
  • pnpm exec tsc --noEmit: one pre-existing error in authz-outbox-commit-safety.pg.spec.ts (identical count on the pristine wiki-v2 tip); no new diagnostics.
  • End-to-end through the real bootstrap: the paired wiki-v2 PR's content-smoke workflow_dispatch run (linked there) boots this fork via make dev-build, runs Docmost first-run setup, and drives the platform through /v1 and the change feed.

Checklist

Scope

  • Complete and scoped to one logical concern (the east-west contract as consumed by the platform: bare bodies, and the change-feed payload keys), no unrelated drive-by changes.

Tests

  • New/changed behavior has appropriate tests (metadata fitness, wire-level proof with a negative control, collab handler assertion).
  • Existing tests pass, and any new tests pass (39 suites / 339 tests; the real-Postgres spec additionally run against Postgres 18).

Docs & contracts

  • Docs updated (service-bridge.openapi.json 1.1.1 description, README.md).
  • Contract impact called out: the wire now matches the already-declared bare schemas; no schema change.

Safety

  • Security, authorization, data integrity, failure paths, and backward compatibility considered: no guard or scope changes; fork and platform ship in one ECS task definition from one commit, so there is no mixed-version window; the platform side rejects an envelope rather than tolerating it.
  • Upstream boundary respected: no edits to Docmost-owned files; the boundary check is green.

Housekeeping

  • Related issue(s) linked above.
  • Known limitations, follow-ups, or intentionally deferred work identified below.

Known limitations / follow-ups

  • The pre-setup 404 "Workspace not found" comes from an upstream main.ts pre-handler; the platform keys its not-provisioned reason (observability only, never a fallback) on that literal. An upstream rename degrades the platform to a fail-closed error reason, which is the safe direction.
  • The fork's void writes still return { ok: true } bodies; the platform ignores them (spec unchanged).
  • Merge order (paired PRs): merge this PR first, then repoint the wiki-v2 submodule to the MERGE commit before merging the super PR.

🤖 Generated with Claude Code

PMQ9 and others added 3 commits September 5, 2026 09:12
…t-west handler (docmost#181)

Docmost's global TransformHttpResponseInterceptor (main.ts, unmodified upstream) wraps every handler
return value as { data, success, status } unless the HANDLER carries SKIP_TRANSFORM metadata. No
service-bridge handler opted out, so every /api/service/* body on the wire contradicted
service-bridge.openapi.json (bare bodies). The platform's Phase C client read those bodies raw, which
took production down on v0.3.1 (wiki-v2 incident docmost#181: authz sync dead, /v1 500, placeholder workspace).

- @SkipTransform() (the upstream per-handler decorator, as upstream uses on health/robots) on all 23
  service-bridge handlers + POST /api/collab/force-disconnect (24 east-west operations)
- fitness test: service-scope-coverage.spec.ts asserts SKIP_TRANSFORM metadata on every route handler;
  the controller list is now derived from ServiceBridgeModule so a new controller cannot escape
  (service-bridge.contract.spec.ts derives the same way); collab-disconnect.controller.spec.ts covers the
  sibling handler
- wire proof: service-bridge.wire.spec.ts boots a real NestFastifyApplication with the global prefix and
  the real interceptor, asserts bare bodies on three representative routes, and keeps an UNdecorated
  fixture route as the negative control (must come back wrapped)
- spec 1.1.1: info.description states the bare-body guarantee and the pre-setup 404 "Workspace not found"
  contract; README section added

No upstream-owned file is touched (all under boundary-excluded prefixes).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…in cannot rewrite its keys (docmost#181)

Found by the first true end-to-end run of the change feed (wiki-v2 incident 181 fix branch, content-smoke +
an isolated local stack): after a space create the relay advanced its cursor past the new outbox rows and
wrote NOTHING to SpiceDB. In the built image postgres.js parses a stored jsonb column to an OBJECT and the
application Kysely's CamelCasePlugin then camelCases its NESTED keys (workspace_id -> workspaceId), so the
R2 mapper (which reads the stored snake_case keys) saw spaces with a null workspace and unmapped members
and pages. Every live event was silently dropped; only the snapshot reconcile ever wrote tuples.

The R2 real-Postgres spec never caught it because its fixture passed a pre-stringified JSON string to a
::jsonb parameter; postgres.js infers json for that parameter and stringifies it AGAIN, so the stored
value was a jsonb STRING scalar that reads back as a string the feed then parsed. Production stores a real
object.

- authz-change-feed.service.ts: readRawAfter selects `payload::text as payload_json` and parses it; a text
  column can be touched by no result-key plugin, so the mapper sees the stored snake_case keys in every
  environment. Mapper unchanged.
- authz-outbox-commit-safety.pg.spec.ts: the fixture stores a real jsonb object (postgres.js json helper)
  with a jsonb_typeof guard, plus a spaces case asserting workspaceId survives the app-shaped Kysely
  (PostgresJSDialect + CamelCasePlugin). Fails on the previous feed; passes now.
- authz-change-feed.service.spec.ts: a structural test pins `payload::text` in the feed's SQL; the spy rows
  answer payload_json as a string. Doc comments corrected (the R1 note was right for the production path).

Fork suites: 39 suites / 339 tests; tsc clean.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…UTHZ_CHANGE_EVENT_DROPPED (review round 1, wiki-v2 incident 181)

The second incident-181 defect (a key-shape mismatch made every live
change-feed event map to null) survived a whole release because a null
mapping was silent and the cursor advanced past it. This adds the detector
the security review asked for:

- authz-change-event.ts: isExpectedSkip(row) is true only for tables the
  mapper does not know and for the one documented no-op (a page_permissions
  cascade DELETE whose page_id could not be resolved).
- authz-change-feed.service.ts: getChanges() collects every other null
  mapping and logs ONE greppable warning per poll:
  AUTHZ_CHANGE_EVENT_DROPPED count=N rows=[table:id=..:op=..]. The cursor
  still advances (the feed cannot wedge); the reconciler remains the backstop.
- spec: a known-table row without space_id warns with the table and id, an
  unknown-table row stays silent, nextCursor still advances past both.
  Verified RED (no warning) then GREEN; service-bridge + authz suites 39/39.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@PMQ9
PMQ9 merged commit 650ce5a into wiki-v2 Sep 5, 2026
@PMQ9
PMQ9 deleted the fix/181-service-bridge-envelope branch September 5, 2026 17:33
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