Skip to content

feat(#485): page-lifecycle facts, trash roots, DB cycle guard, and snapshotBefore (wiki-v2 PR-B, fork half) - #53

Merged
PMQ9 merged 5 commits into
wiki-v2from
feat/485-lifecycle-b
Sep 23, 2026
Merged

PMQ9 merged 5 commits into
wiki-v2from
feat/485-lifecycle-b

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 22, 2026

Copy link
Copy Markdown

TL;DR

The fork half of agent page lifecycle (wiki-v2 issue 485, PR-B). It adds the facts the platform needs to decide a /v1 move or restore without ever changing who can see a restricted page, a space trash read, a DB cycle guard on pages.parent_page_id, and an opt-in pre-write snapshot so a version restore can be undone. All code is CCC-owned (service-bridge/, authz/page-write/); no upstream-owned file is touched.

Context

The engine's native move and restore paths assume a human in the UI. Traced in wiki-v2 issue 485 (ADR 0024):

  • a restore whose parent is trashed lands at the space root and drops an inherited restriction (G1);
  • a move to another space deletes page_access for the whole subtree (G2);
  • a same-space re-parent can lift an inherited restriction (F2);
  • trashed descendants are stranded across spaces (F3);
  • nothing stops a page becoming its own ancestor (G5).

The platform (not this fork) now plans every move and restore and refuses those cases with 409 restricted_page / move_cycle. To do that it needs facts only the database has: trashing reaps a page's SpiceDB edges, so the PDP cannot see restriction lineage through a trashed ancestor. This PR serves those facts and nothing else. Policy stays out of the fork. Plan, progress and tradeoffs: https://github.com/cccvu/wiki-v2/issues/485#issuecomment-5782076140

Related issues

  • Part of cccvu/wiki-v2#485. The issue closes when the super-repo PR (its gitlink bump, Closes #485) merges.
  • Builds on cccvu/docmost PR 51 (the fail-closed locked bulk check).
  • Related: cccvu/wiki-v2#493 (the native UI paths, which still declassify).

What changed

  • POST /api/service/pages/lifecycle-state (pages:read), in service-page-lifecycle.service.ts. Workspace-filtered, trashed rows included. It returns:

    • the page's restricted strict ancestors, walked through trashed ancestors;
    • its parent's space and trashed state;
    • whether any descendant is restricted, trashed or in another space.

    Given a would-be parent (targetParentPageId: a uuid, or null for the root), it also returns that target's existence/space/trashed state, its restricted self-and-ancestors, whether it is the page itself or a descendant, and the next sibling position. Every walk is cycle-safe (path array) and depth-bounded (256), and an incomplete walk reads as restricted. nextPosition mirrors PageService.nextPagePosition (COLLATE "C", live siblings only), because importing PageModule here would pull in the collaboration/lib0 graph.

  • POST /api/service/pages/trash (content:read): the ROOTS of trashed subtrees in a space, newest first.

    • Keyset on (date_trunc('milliseconds', deleted_at), id::text), limit + 1.
    • A root whose lineage carries a restriction, or whose walk is incomplete, is excluded in SQL before the limit, so pages stay full and a restricted page's title is never listed.
    • deletedBy is a display name, never a user id.
  • PageCycleGuardInstaller installs ccc_page_cycle_guard() as a BEFORE UPDATE OF parent_page_id trigger. It refuses a self or descendant parent after taking pg_advisory_xact_lock(485485, hashtext(workspace_id)), and a walk that reaches its bound (1024) is refused too. This closes the race between two concurrent moves (A under B while B moves under A), which a platform pre-check alone cannot, and it covers the engine's own UI move. Same shape as the outbox installer: remote-mode only, advisory-locked DDL, idempotent, and a remote boot that cannot install it refuses to start.

  • snapshotBefore on the CCC conditional-update route (authz/page-write/conditional-page.controller.ts).

    • Before a content replace, it saves the page's current title + content as a history version. It runs after the edit check and the parse, so a refused or malformed write saves nothing.
    • It is skipped when the write changes neither, or the newest version already holds exactly this title + content.
    • The response reports snapshot: { status: 'saved' | 'unchanged', historyId }. PageHistoryRepo is an optional appended constructor parameter.
  • Contract:

    • service-bridge.openapi.json goes to 1.3.0 (2 paths, 5 schemas). The concurrent fork PR 52 (SMTP_SECURE docmost/docmost#486) also bumps to 1.3.0; whichever merges second takes 1.4.0.
    • Provider contract rows and schema tethers are added, plus service-scope-coverage entries.

How this was tested

  • pnpm exec jest src/authz src/service-bridge src/editor-compat (the CI docmost-authz lane): 81 suites, 761 passed, 74 skipped (real-Postgres specs self-skip without a DB).
  • service-page-lifecycle.pg.spec.ts against a throwaway Postgres 18 (AUTHZ_REQUIRE_PG=1): 18/18. It covers:
    • lineage through a trashed ancestor;
    • fail-closed on a pre-existing cycle and on a dangling parent;
    • descendant facts;
    • nextPosition ordering (valid fractional keys, trashed siblings ignored);
    • trash roots-only, restricted-lineage exclusion before the limit, and the keyset;
    • the trigger refusing self/descendant parents, and re-install idempotence;
    • the concurrent A↔B race, which exactly one of the two moves wins.
  • conditional-page.controller.spec.ts (+10 cases): the snapshot is saved after the edit check and the parse and before the apply; unchanged when the newest version or the incoming write matches (title + content); saved when only the title differs; nothing on a refusal; never forwarded to PageService.update; boolean validation.
  • npx tsc --noEmit -p tsconfig.build.json: clean.
  • bash scripts/check-upstream-boundary.sh (super repo, against the committed fork HEAD): upstream boundary intact, and the brand guard passes.
  • End to end: the super PR's content-smoke run drives the new routes through the platform against this branch.

AWS / infra apply

N/A. Fork code only; it ships with the release after the super-repo gitlink bump. The trigger installs itself at boot (idempotent). Rollback: a previous image leaves the trigger in place, which is harmless: it only refuses cycles, which the engine never intended. To remove it: DROP TRIGGER ccc_page_cycle_guard ON pages; DROP FUNCTION ccc_page_cycle_guard();.

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: the native UI restore / move-to-space paths still declassify (wiki-v2 issue 493). The trash read hides every root under a restriction, including ones that could legally be restored under a live parent.

🤖 Generated with Claude Code

PMQ9 and others added 5 commits September 22, 2026 16:32
…and a DB page-cycle guard

CCC-owned (service-bridge/ prefix, zero upstream edits). The platform's /v1 move, restore and trash
routes decide from these facts; nothing here decides policy.

- POST /api/service/pages/lifecycle-state (pages:read): a page's restriction lineage through TRASHED
  ancestors (trashing reaps the SpiceDB edges, so the PDP cannot answer this), its parent's state, and
  whether any descendant is restricted / trashed / in another space; with a would-be parent, that
  target's lineage, cycle membership and next sibling position. Every walk is cycle-safe and
  depth-bounded, and an incomplete walk reads as restricted (fail closed).
- POST /api/service/pages/trash (content:read): the roots of trashed subtrees in a space, newest
  first, keyset (deletedAt ms, id), with every root under a restriction excluded IN SQL before the
  limit. deletedBy is a display name only.
- PageCycleGuardInstaller: a BEFORE UPDATE OF parent_page_id trigger that refuses a self/descendant
  parent under a per-workspace advisory xact lock (closes the concurrent A<->B race and covers the
  engine's own move path). Remote-only, advisory-locked, idempotent, fail-closed on boot.
- Contract: service-bridge.openapi.json 1.3.0 (2 paths, 5 schemas), provider contract rows, scope
  coverage. Real-Postgres spec incl. the concurrent race (18 cases).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…estore can be undone

An optional `snapshotBefore` on the CCC conditional-update route saves the page's current title +
content as a history version just before a content `replace` (after the edit check and the parse, so
a refused or malformed write saves nothing). Skipped when the write changes neither, or the newest
version already holds exactly this title + content. The response reports
`snapshot: { status: 'saved' | 'unchanged', historyId }`; the platform returns the id as the undo
handle (`previousVersionId`).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… (PR 520 review, Testing L3)

Native mode installs nothing; remote installs the guard function + BEFORE UPDATE OF parent_page_id trigger in one
advisory-locked transaction (asserting the per-workspace lock the trigger takes before walking); a not-ready
engine is retried; and a remote boot whose install never succeeds REFUSES TO START — so removing that throw
(which would silently reopen the concurrent-move race) now fails a test. Mirrors authz-outbox.installer.spec.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…B branch; service-bridge contract -> 1.4.0

Both PRs bumped service-bridge.openapi.json to 1.3.0; per the agreement with docmost#486 the second to merge takes
1.4.0 (PR 52 merged first). The provider contract spec keeps both sides' rows (ShadowUserLookup +
the docmost#485 lifecycle schemas). Fork lane 85 suites / 844 passed; real-PG lifecycle spec 18/18.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@PMQ9
PMQ9 merged commit 3d11490 into wiki-v2 Sep 23, 2026
@PMQ9
PMQ9 deleted the feat/485-lifecycle-b branch September 23, 2026 04:10
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