Skip to content

fix(#493,#545): page-state read + database guards that keep restrictions (Release A) - #57

Merged
PMQ9 merged 2 commits into
wiki-v2from
fix/493-545-a
Sep 23, 2026
Merged

PMQ9 merged 2 commits into
wiki-v2from
fix/493-545-a

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 23, 2026

Copy link
Copy Markdown

TL;DR

This is the fork half of Release A of the page lifecycle projection fix for cccvu/wiki-v2 issues 493 and 545 (ADR 0027 there). It adds:

  • a read of the pages' current authorization structure, which the platform's page projector will project level-triggered;
  • database guards, so that only an explicit unrestrict or a purge can take a restriction away.

Context

  • Issue 493: the engine's own write paths remove restrictions in the database. movePageToSpace deletes the moved pages' page_access rows and re-roots the page. A restore whose parent is trashed detaches the page to the space root, dropping an inherited restriction. A concurrent restrict can land with a stale page_access.space_id, which a later space delete cascades away.

  • Issue 545: the platform projects page events edge-triggered. It applies them in (xact_id, id) order, which is not commit order, across three SpiceDB transactions each. A partially projected ancestor chain can therefore read locked=false while a page's DB lineage is restricted.

  • The fix, in two releases:

    • a per-page lineage_restricted marker in the SpiceDB schema;
    • a single-writer page projector that re-reads current state (that is the platform's Release B);
    • DB guards (this PR).

    ADR 0027 in cccvu/wiki-v2 has the design and a minimality proof: why the simpler "keep ancestry + atomic/add-before-remove" direction cannot satisfy the invariants.

Related issues

  • Refs cccvu/wiki-v2 issues 493 and 545: Release A. The paired super PR does not close them; Release B does.
  • Follow-up: cccvu/wiki-v2 issue 556 (native restore half-applies when the guard refuses its detach — fails closed).

What changed

All changes are CCC-owned, under apps/server/src/service-bridge/ and docs/integrations/. Zero upstream-owned file edits.

  • POST /api/service/authz/pages/state (page-authz-state.service.ts, DTO, route on AuthzChangeController). New scope pages:authz:read, with its own 6000/min window (the platform reads it per page event).
    • Modes: {pageIds≤500}, {subtreeRootId, limit, after?} (descendants, keyset), or {limit, after?} (every page, trashed included). A mix of modes is a 400.
    • Row: {pageId, exists, spaceId, parentPageId, restricted, lineageRestricted, lineageComplete}. lineageRestricted is exactly the fix: uuid7 for commentId docmost/docmost#524 rule, lineageRestricted(readPageLineage(includeSelf)).
    • One SQL statement per response, so no row is torn across a concurrent move.
    • Walks go through trashed and cross-space rows and stay in the workspace. An unfinished walk reads as restricted.
  • PageRestrictionGuardInstaller (remote only, fail-closed boot, like the fix: close space selection popover onClickOutside docmost/docmost#485 cycle guard):
    • ccc_page_restriction_guard (BEFORE UPDATE OF space_id, parent_page_id ON pages):
      • g1 refuses a space change for a page that is restricted or sits under a restricted page (walked from the OLD parent);
      • g2 refuses a parent change that takes an unrestricted page out from under its last restricted ancestor. An unfinished walk counts as restricted before the move and as unrestricted after it.
    • ccc_page_access_guard (BEFORE INSERT OR UPDATE ON page_access): g0 sets space_id from the page.
    • All of them take the cycle guard's per-workspace advisory lock unconditionally before reading, so a restrict and a move serialize.
    • Installing is idempotent: no DDL runs when the version stamp matches and both triggers are enabled; otherwise it uses lock_timeout 3s and retries.
  • PageGuardConflictInterceptor is an APP_INTERCEPTOR registered by ServiceBridgeModule. It maps exactly the guards' 23514 refusals, plus the cycle guard's (a 500 until now), to 409 {message, code} with a fixed message. It is inner to the Docmost vs. Outline docmost/docmost#467 access audit, so the audit records 409.
  • ServiceAttachmentService.resolvePage: an attachment of a trashed or missing page is a 404. After Release B a trashed page keeps its PDP decision.
  • LIFECYCLE_MAX_DEPTH 256 → 1024, equal to the cycle guard's bound and pinned by a spec.
  • Contract 1.7.0 (additive): new op + schemas, pages:authz:read in the scope list, and the guard 409 documented. README updated.

How this was tested

  • Real Postgres 18 pg lane (AUTHZ_REQUIRE_PG=1 … jest --runInBand 'src/(service-bridge|authz)/.*\.pg\.spec\.ts$'): 15 suites, 175/175.
    • page-authz-state.pg.spec.ts:
      • walks through trashed and cross-space rows;
      • cycle, dangling and foreign-workspace parents fail closed;
      • the 1025- and 1026-row chains around the 1024 bound;
      • the marker equals lineageRestricted(readPageLineage(includeSelf)) on a mixed forest;
      • keyset and subtree paging cover every page exactly once;
      • torn-read stress under concurrent re-parenting.
    • page-restriction-guard.pg.spec.ts drives the real upstream PageService.movePageToSpace / movePage and PageRepo.restorePage:
      • refusals roll back, outbox rows included;
      • legitimate moves pass;
      • two-connection races (restrict ∥ move-to-space in both orders; an ancestor restricted while the child moves) serialize on the lock;
      • installer idempotence and a disabled trigger re-established;
      • the REAL driver error maps to the 409.
  • Unit: jest src/authz src/service-bridge: 92 suites, 985 passed. page-guard-conflict.interceptor.spec.ts boots the app topology with the real Docmost vs. Outline docmost/docmost#467 audit interceptor; the audit records 409, and goes red with the order swapped.
  • Typecheck: no new tsc errors (the 19 pre-existing ones are unchanged); eslint is clean on the new files.
  • End to end: the full /v1 content smoke passes on a locally built stack from these branches (ALL CHECKS PASSED), including the new 10e leg. A native /api/pages/move-to-space of a restricted page returns 409 {code: ccc_page_restricted_space_move} and the page does not move. The fork log shows page restriction guard installed.

AWS / infra apply

N/A. The guards are installed by the fork at boot, like the cycle guard and outbox. No Terraform, no AWS.

Checklist

Scope

  • Complete and scoped to one logical concern — no unrelated drive-by changes (or follow-ups noted below).

Tests

  • New/changed behavior has appropriate tests — unit / integration / e2e / security / regression as applicable (or N/A, with why).
  • Existing tests pass, and any new tests pass.

Docs & contracts

  • Docs added/updated where needed (README / docs/ / an ADR), or N/A.
  • Any API/contract, config, migration, permission, or deployment impact is called out in this PR, or N/A.
  • Any AWS/infra apply is described precisely, scoped (no mass-apply), and captured in a reviewable one-time script — see "AWS / infra apply" above, or N/A.

Safety (this is a security-first, deny-by-default platform)

  • Security, authorization, data integrity, failure paths, and backward compatibility considered, or N/A.
  • Upstream boundary respected — no undocumented edits to Docmost-owned files (see UPSTREAM_MODIFICATIONS.md); proprietary/authz logic stays out-of-process.

Housekeeping

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

Known limitations / follow-ups

  • Behaviour change (intended). A native move-to-space of a restricted page or section, and a move that strips an inherited restriction, now return 409. Restrict the page itself, or lift the restriction first.
  • Half-restore. Upstream restorePage is two autocommit statements. When the guard refuses its detach, the un-trash has already committed: the page stays under its restricted, trashed parent (fails closed) but is live. This is cccvu/wiki-v2 issue 556; the pg spec pins the behaviour.
  • Guards outlive rollback. They persist across any rollback, because no image drops them. Under a pre-A image a refusal is a 500; removal is an operator step documented in cccvu/wiki-v2 docs/runbooks/cd.md.
  • Merge order: this PR first. Then the super PR's gitlink is repointed to this PR's merge commit.

🤖 Generated with Claude Code

PMQ9 and others added 2 commits September 23, 2026 14:50
…keep restrictions (Release A)

Release A of ADR 0027 (wiki-v2): the fork side of the page lifecycle projection fix.

- POST /api/service/authz/pages/state (pages:authz:read, own 6000/min window,
  contract 1.7.0): the pages' CURRENT placement, own restriction and lineage
  (the docmost#524 rule, includeSelf) by id / subtree / keyset. ONE SQL statement per
  response, so a row never mixes two committed trees; walks go through trashed
  and cross-space rows, stay in the workspace, and an unfinished walk reads as
  restricted.
- PageRestrictionGuardInstaller (remote only): ccc_page_restriction_guard on
  pages (g1: no space change for a restricted page or section; g2: no parent
  change that strips an unrestricted page's last restricted ancestor) and
  ccc_page_access_guard on page_access (g0: space_id from the page). All take
  the cycle guard's per-workspace lock first, so restrict and move serialize.
  Idempotent: no DDL when current; lock_timeout 3s when it must change.
- PageGuardConflictInterceptor (APP_INTERCEPTOR via ServiceBridgeModule): the
  guards' 23514 refusals (and the cycle guard's, a 500 until now) become
  409 {message, code}; inner to the docmost#467 access audit, which records the 409.
- ServiceAttachmentService.resolvePage: an attachment of a trashed or missing
  page is a 404 (trash will keep its PDP decision in Release B).
- LIFECYCLE_MAX_DEPTH 256 -> 1024, the cycle guard's bound.

Real-Postgres specs drive the REAL upstream movePageToSpace/movePage/
restorePage, two-connection restrict/move races, the marker == docmost#524 rule
cross-check and a torn-read stress. Zero upstream-owned file edits.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… hot-scope rate windows, installer boot spec

- g1 now runs on EVERY row an UPDATE sets pages.space_id on, changed or not (new trigger
  ccc_page_space_guard; g2 stays on real parent changes). Upstream movePageToSpace deletes every
  moved row's page_access, including a child already in the target space (stranded there by an
  earlier move that skipped it while trashed, then restored and restricted) — native-only
  declassification the old OLD<>NEW gate let through. pg spec drives it through the real paths;
  red with the old gate.
- Per-scope rate-limit table: pages:read (the /v1 live-page resolve, every page-content request)
  gets its own 6000/min window beside pages:authz:read; every other scope stays on the shared 600.
- Installer: page_access before pages (the outbox installer's lock order; they boot concurrently),
  accurate lock_timeout docstring; new unit spec for the fail-closed boot (red if the final throw
  becomes a return), retry, no-DDL-when-current and lock_timeout-before-DDL.
- pages/state rejects an explicit null (it would widen subtreeRootId:null to a full scan).
- Lock test fails fast under lock_timeout instead of hanging; uses CYCLE_LOCK_CLASS.
- README: a refused restore-detach does not roll back the un-trash (issue 556).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@PMQ9
PMQ9 merged commit 3921a8b into wiki-v2 Sep 23, 2026
@PMQ9
PMQ9 deleted the fix/493-545-a branch September 23, 2026 22:13
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