Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

A page its owner purged stays purged after a restore (#9479)

Garry Tandbb203149e5c
diff --git a/behavior-changes.d/owner_purge_tombstones_survive_9469.md b/behavior-changes.d/owner_purge_tombstones_survive_9469.md
new file mode 100644
index 0000000000..c3e1709a52
--- /dev/null
+++ b/behavior-changes.d/owner_purge_tombstones_survive_9469.md
@@ -0,0 +1,4 @@
+---
+kind: fix
+---
+A page the owner purged while a derived page kept it private now gets its purge records whichever later run removes it (an automatic expiry purge, or a run after a failed or deferred one), on repo brains and on brains without a repository, so a restore no longer brings it back. Restoring a backup now removes the same rows a live page purge removes (write requests, files, derived generation items and their effects), not only the page itself, and stops with purge_replay_incomplete, naming the purge it could not replay, instead of reporting success on a partial sweep. A new doctor warning, purge_mirror, says when purges are recorded only inside the database, so a backup taken from git alone would not carry them (#9469).
diff --git a/changelog.d/capy-purge-stays-purged-9469.md b/changelog.d/capy-purge-stays-purged-9469.md
new file mode 100644
index 0000000000..b98c6d7a01
--- /dev/null
+++ b/changelog.d/capy-purge-stays-purged-9469.md
@@ -0,0 +1,17 @@
+---
+important: security
+---
+**A page its owner purged stays purged after a restore, even when the purge waited behind a private derived page.** When an owner purges a page that a derived page depends on for its privacy, the purge waits until that derived page can be labelled private. Before this change, if an automatic purge removed the page first, or the next purge run failed or was deferred, the page was deleted without the purge records that tell a restore to delete it again, so restoring an older backup brought its text back. gbrain now remembers that the owner's purge is owed, by page id only, and whichever run finally removes the page writes those records, on repo brains and on brains without a repository. Restoring the page clears the reminder, so a page its owner brings back is never purged later by it.
+
+A restore now also removes the same rows a live page purge removes. Replaying a purge from the purge mirror used to delete the page but leave its write requests, file rows, derived generation items and their effects, which still held the purged text. Replay and the live purge now share one sweep. If replay cannot finish a sweep (a store that does not exist on the brain aside), the restore stops with `purge_replay_incomplete` and names the purge it could not replay, instead of reporting success. A new doctor warning, `purge_mirror`, says when recent purges are missing from this machine's purge mirror (for example when gbrain runs in a home directory that is removed after each run), so a restore from an older backup would not replay them. The fix is to give every writer one durable `GBRAIN_HOME`. Thanks to @oakleaf for the replay and purge mirror findings (#9469).
+
+<!-- behavior-change: owner_purge_tombstones_survive_9469 -->
+
+### Fixed
+
+- An owner purge that waited behind a private derived page writes its purge records whichever later run removes the page (an automatic expiry, or a run after a failed or deferred one), on every brain.
+- Restore-time purge replay removes write requests, file rows, derived generation items and their effects, the same rows the live purge removes, and fails with `purge_replay_incomplete` instead of reporting a partial sweep as done.
+
+### Added
+
+- Doctor check `purge_mirror`: a warning (never a failure) when purges recorded in the last 30 days have no line in this machine's purge mirror. It prints counts and the mirror's path, never a page name or text.
diff --git a/docs/architecture/key-files/commands-2-continued.md b/docs/architecture/key-files/commands-2-continued.md
index f0ea294384..2da6482109 100644
--- a/docs/architecture/key-files/commands-2-continued.md
+++ b/docs/architecture/key-files/commands-2-continued.md
@@ -41,7 +41,7 @@ Current behavior and load-bearing invariants; history belongs in Git and CHANGEL
- `src/commands/doctor/checks/rename-identity-downgrades.ts` — `rename_identity_downgrades` (#6729): warn while a downgraded rename record is open (`sync-identity-downgrades.ts`), naming up to 5 pairs with their facts and links; fix = the first pair's `gbrain repair rename-identity` apply (destructive, `ask_user`, `preview_argv`, `plan_hash`; `--close` on a managed brain), so unbound classic Postgres pairs get an actionable fix too; `gbrain restore <old-slug> --source-id <id> --force` is named as the alternative in its text only where `restore_available !== false`, and `restore_unavailable_sources` lists the rest.
-- `src/commands/doctor/checks/purge-parks.ts` — `purge_parked_pages` (#6741 C): names parked purges with their last error (only this version's parks, `isParked`); fix `gbrain pages purge-deleted --retry-parked --yes` (preview `--dry-run --json`), consent destructive, ask_user. Also `derived_origin_parked` (GST-2689, `checkDerivedOriginParked`): names each origin `persistence/derived-origin-hold.ts` parked with the page that blocks it and the reason (`details.origins`); fix the read-only `gbrain repair visibility --source <id> --slug <blocking page>` preview. An origin whose owner purge was held (`held_request_id`) also gets `finish_argv` (`gbrain delete <slug> --purge --source-id <id>`, a new request), named in the message, since the automatic purge removes it without the purge tombstones.
+- `src/commands/doctor/checks/purge-parks.ts` — `purge_parked_pages` (#6741 C): names parked purges with their last error (only this version's parks, `isParked`); fix `gbrain pages purge-deleted --retry-parked --yes` (preview `--dry-run --json`), consent destructive, ask_user. Also `derived_origin_parked` (GST-2689, `checkDerivedOriginParked`): names each origin `persistence/derived-origin-hold.ts` parked with the page that blocks it and the reason (`details.origins`); fix the read-only `gbrain repair visibility --source <id> --slug <blocking page>` preview. An origin whose owner purge was held (`held_request_id`) also gets `finish_argv` (`gbrain delete <slug> --purge --source-id <id>`, a new request), named in the message, which finishes it at once.
- `src/commands/doctor/checks/unlinked-facts.ts` — `unlinked_facts` (#5836), registered before `search_mode` and mirrored in `doctorReportRemote`: per the doctor's source scope (`--source`, else every source; the caller's grant remotely), the unlinked share of the newest 100,000 active facts per source (`idx_facts_since` LATERAL walk), the 7-day unlinked share of new facts, `no_subject` judgments and the 7-day `linked` tiers from `fact_relink_attempts`, fence-owned unlinked rows (row_num or source_markdown_slug set, conversation rows excluded), the conversation extractor's unlinked derived rows (`cli:extract-conversation-facts*`, #6764: count, origin `conversation`, and `details.conversation` naming the source with the most and its pages, most rows first, up to `CONVERSATION_FIX_SLUGS` = 50), origins and 7-day write-time inferred links by tier (`entity inferred from <tier>` context, bare or after ` — `). Warns when over 25% of at least 20 new facts are unlinked, facts judged `no_subject` and conversation rows memoized `conversation_unresolved` excluded (`new_7d.conversation_unresolved`); the fix is `gbrain facts relink --dry-run`; when conversation rows are unlinked the envelope `fix` (`details.conversation.fix`) is, for rows the free conversation relink has not tried, `gbrain facts relink --conversation --source <id> --slugs <csv>` (preview `--dry-run`, consent none, actor agent, host operator remotely; test `test/conversation-relink-6764.test.ts`) and, once only rows it memoized `conversation_unresolved` remain (a memo that a page created later re-arms unless the reason is memoized, `core/facts/conversation-memo.ts`), a bounded re-extraction of those pages not yet re-extracted on their current content (`gbrain extract-conversation-facts --source-id <id> --slugs <csv> --force --max-cost-usd <N>`, input `N` the approved USD, preview `--dry-run`, consent paid + egress, ask_user; test `test/conversation-facts-unlinked-6764.test.ts`), both verified by `gbrain doctor --only unlinked_facts --json`, because plain relink never examines derived rows; the remote report asks the brain host operator to run it. When only rows on pages already re-extracted on their current content remain, `details.conversation.fix` is `manual`: no envelope `fix`, and the message names linking by hand with `--entity` (test `test/conversation-memo-6764.test.ts`). Without `fact_relink_attempts` the relink figures are null and nothing is excluded. Non-user facts are skipped (#7270). Tests: `test/doctor-unlinked-facts.test.ts`, `test/relink-attribution-7270.test.ts`.
- `src/commands/doctor/checks/transcript-archive.ts` — `transcript_archive_freshness` (GST-2768, brain category): off is ok and reads nothing; on, it reads `archiveStatus` and `transcripts.archive.last_maintenance` and warns on a harness more than `transcripts.archive.stale_days` (2) behind its newest session day (fix `gbrain transcripts archive run`, or `backfill --from <day>` past one run window), on no maintenance pass in that long (fix `gbrain autopilot --install`, `persistent_install` consent), on held days (with codes), reader failures and pass errors; no readable sessions is info. Pinned by `test/doctor-transcript-archive-freshness.test.ts`.
diff --git a/docs/architecture/key-files/memory-purge.md b/docs/architecture/key-files/memory-purge.md
index 6c9a8f81c7..b9810bfa9c 100644
--- a/docs/architecture/key-files/memory-purge.md
+++ b/docs/architecture/key-files/memory-purge.md
@@ -7,7 +7,8 @@ User-facing contract: [expire versus purge](../../guides/memory-boundaries.md#pu
- `src/core/facts/purge.ts` — `submitPurgeFactMutation`, the handler of the owner-only `purge_fact` op (`gbrain forget <id> --purge`). Refuses every caller whose `ctx.remote !== false` with `trusted_local_only` (`purgeHostOnly`, actor user, the exact host command). `planFactPurge` (read-only) computes the scope: every fact row with the same `gbrain_fact_fingerprint` in the fact's source, visibility and subject (the fact's `entity_slug`, `'*'` with `all_subjects`) except extraction outcome rows (`outcomeMarkerSql`; the receipt's fact probes, `purgeCompletion` and the restore replay skip them too, #7831), verbatim takes (`facts.consolidated_into` plus same-fingerprint takes on the subject's page), affected pages from `discoverWithdrawalTargets`, page_versions carrying the text, terminal request intents carrying it (`findRequestsCarrying` behind the `purge:carrying_lookup` seam; its 57014 is the typed `purge_lookup_timeout`, nothing planned or purged, pinned by `test/fact-purge-lookup-timeout-6511.test.ts`), derived rows from `derivation_inputs`, the fact's P8 review rows, and a `revision` digest. A dry run returns the receipt, `confirm_token` (fingerprint's first 8 hex) and `expected_revision`; a real run requires `confirm === token` and, when given, an unchanged revision. The journaled intent is `{id, reason, all_subjects, fact_hash}`, never claim text; replay by request id returns the stored outcome. One transaction in the global lock order: source `FOR UPDATE` (serializes with the guards' `FOR SHARE`), counters for the purge and every redacted request, admission, then inside `withCoordinatedWrite`: pending-publication refusal (`purge_blocked_pending_recovery`), intent redaction, page locks, derived rows hidden before evidence goes, `fact_purges`/`take_purges` tombstones, deletes (takes, review rows, open loops by fact id, take proposals by fingerprint, facts), core-notice base text nulled, page bodies rewritten through the purge overlay, chunks deleted, versions redacted in place, legacy `query_cache` cleared, and `withdrawal-mirror`/`git`/`embedding` effects with `mode: 'purge'` and `commit_subject: gbrain: purge fact <hash8>`. Chunks are rebuilt before acknowledging; `similar_active` is computed before deletion. Pinned by `test/purge-fact.test.ts` (both engines) and `test/forget-purge-cli.test.ts`; the segmented preimage version by `test/large-pages/purge-fact-segmented-preimage.test.ts` (inline twin parity, both engines). Segmented pages (Wave B): live bodies are rewritten fence by fence through `rewriteLiveSegmentedFences` (a new generation; only the rewritten occurrences' chunks change, an unsealed page is projected after commit), and each rewritten page gets the preimage version an inline page gets: a segmented version of the revision the flip moved off, read before the flip and referencing the new generation (the body a reader of that revision sees under the purge's tombstone, as the inline preimage is read), so no generation holding the claim stays pinned; retained generations are rebuilt through `rewriteSegmentedGeneration`; the plan (and its digest) also lists open, sealed or prepared stagings whose staged texts or parts hold the claim plus the queued requests whose `body_ref` names them (`stagingsHoldingClaim`, which also reads `page_body_staging_texts`; `findRequestsHoldingStagings`): the stagings are aborted with their parts deleted, the requests cancelled with `purged_content` (a running or recovering one refuses with `purge_blocked_pending_recovery`), and the segment texts no manifest row references any more are deleted in the same transaction, never left to GC (`purgeSegmentStorage`, which the restore-time replay also runs).
- `src/core/facts/purge-verify.ts` — receipts and verification. `verifyFactPurge` runs one bounded adapter per store (row bound, per-statement `statement_timeout`, a total deadline; `__setPurgeProbeLimitsForTests`): swept stores report `deleted`, `incomplete` (still holds the claim) or `unverified` (bound hit); page-text stores report remaining text as `out_of_scope: source_prose`; every `probed_reported` inventory table is probed and listed; derived rows are `retained_inactive`; model-derived pages without edges are `derived_legacy: unverified`. On-disk: page files (`canonical_markdown`), a quarantined `<file>.tmp` carrying the claim is deleted, the facts write-failure log is reported. `gitResidualCommits` scans `git log --all -p -- <page paths>` output in-process (the pattern never enters argv; byte and time bounded) and lists commits; `OUT_OF_REACH_RESIDUALS` lead every receipt. Links, timeline entries and raw data that still carry the claim are named row by row (`link:<id> <from> -> <to>`, `timeline:<id> <slug> <date>`, `raw_data:<id> <slug> <source>`, up to 20); queued `persistence_effects` whose data, outcome or recovery carry it are reported; `belief_events` and `fact_context` get ledger reports. The vector probe returns same-model neighbours as paraphrase candidates and is never a gate. `purgeCompletion` derives `committed | complete | incomplete` from effects (failed or parked = incomplete), a fingerprint probe and the receipt, with exit codes 10/0/75. A segmented page or version is probed segment by segment (`segmentedBodyContains`, a needle split by a cut is found; row and time bounded) and reported with the page stores; `page_segment_texts` gets an exact-bytes probe (an unreferenced text holding the claim is `incomplete`, a referenced one `out_of_scope`); the page purge probes the texts its manifests referenced (`PagePurgeContext.segmentShas`).
- `src/core/belief/purge.ts` — the purge half of the belief ledger (BELIEF_LEDGER.md 6.5). Every fact or page purge bumps `sources.purge_epoch` (`bumpPurgeEpoch`; an append prepared at an older epoch with `preparedEpoch` refuses `belief_stale_preparation`) and runs three passes inside the purge transaction: `redactPurgedKeys` (the purged keys, by `claim_fp` and page lifetime, keep skeletons: value, value_hash and prior_hash null, `purged`), `redactCarryingFields` (keyset batches of 1000 reading only the carrying fields of fact, take and link events, matched in JS by `quotes` over `claimForms`, never SQL `lower()`/`strpos`: each carrying field from `CARRYING_FIELDS` that quotes a purged claim, plain or fence-escaped, becomes `REDACTION_MARKER(<purge request>)` and is listed in `redacted_fields`; a later event's prior_hash naming the old hash names the new one) with `registerLiveCarriers` (a surviving row whose field quotes the claim enters `belief_redactions` with its field hash, so its next event writes the marker until an explicit owner edit sets a different value: `publicationBeliefOptions` marks `ownerEdit` only on a local `put_page`, `edit_page`, `takes_update` or `takes_resolve` with no internal intent kind, and append still needs `OWNER_TIER_FLOOR`) and, for a fact purge, `registerVersionCarriers` (the retained versions still quoting the claim, registered under the keys the backfill gives them). A claim shorter than 8 characters quotes a field only as a word in spaced scripts (`ceo` does not match `herbaceous`; CJK and markup still match), and `appendDerivedHidden` (one `derived.hidden` per key the derivation walk hid; `readBeliefHistory` then hides that key's earlier events and every backfill event of it). A page purge's claims come from `pagePurgeClaims`: the page's facts and takes, every retained version's fence rows and every claim in its lifetimes' events, unbounded. `releaseSuppressedFields` is the owner release `trust confirm` runs (`belief_redactions.released_hash`); every release, there or in an owner-edit append, appends one `redaction.released` (`redactionReleasedDraft`: fields, purge requests and released hashes, never the text) in the publication `released_by` names. The receipt's `ledger` block reports the counts and `derived_walk`; a truncated walk withholds certification of its coverage interval (`coverage-report.ts`). Purge-verify's ledger report is a JS keyset scan too: a missed carrying field is `incomplete`, another subject's recorded value quoting the claim is `out_of_scope` with `field@key` items. Seam: `__setPurgeRedactionFaultForTests`. Probes: `test/belief-ledger-purge.test.ts` (both engines).
-- `src/core/belief/purge-mirror.ts` — the text-free purge mirror `<brain home>/.gbrain/purge-events.jsonl` (0600, fsynced): an `intent` line before the purge commits (`appendPurgeIntent`; request, source and incarnation, scope, fingerprints, content hashes, lifetimes, epoch; never claim text; an unwritable mirror refuses the purge with the typed `purge_mirror_unwritable`, with or without a ledger) and a `committed` line after (`appendPurgeCommitted`, best effort; a page purge writes it from the coordinator's `afterCommit`, so it does not depend on the op handler finishing). `restorePgliteBackup` (`gbrain backup restore --purge-mirror <absolute path>` names a mirror other than the brain home's) replays every committed purge newer than the archive inside its transaction (`replayPurgesInRestore`: tombstones, row removal (a fact-scope replay rewrites page bodies and versions through `rewritePageBodies` / `redactPageVersions`, then runs `purgeSegmentStorage` with each claim's `stagingsHoldingClaim`; a page-scope replay deletes the page through `deletePageWithSegments`, so segment texts that held the purged content go in the replay, as in the purge), the three passes, the derived rows hidden again with `derived.hidden` cause `restore_replay`, one `fact.purged`/`page.purged` with cause `restore_replay`; a page-scope replay matches pages by content hash or lifetime, never by slug alone; an archive from before the ledger, no `belief_events`, replays tombstones and row removal and reports `ledger: 'absent'`; an intent whose request already has tombstones is skipped), records each as `complete`, `partial` (no restored row held the text, or the derivation walk could not run) or `unverified` (intent without committed line, not replayed), returns `purge_replay` in the receipt and adds a reconnect warning unless the state is `none` or `complete`; an unreadable mirror is `unavailable`. `coverage.restored` carries the outcome, which `readRestorePurgeReplay` reads back for the doctor. A Postgres restore runs `gbrain beliefs replay-purges --archived-at <iso> (--purge-mirror <abs path> | --from-url-env <VAR>) [--apply]` (`src/commands/beliefs-replay-purges.ts`; dry run by default: `replayPurges` runs the replay in a transaction that rolls back), whose database source `purgeLinesFromDatabase` rebuilds the intents from every pre-restore tombstone (not bounded by `purged_at`, the purge transaction's start) and skips those the archive already holds. Seam: `__setPurgeReplayFaultForTests`. Probes: `test/belief-ledger-restore.test.ts`; segment storage after a replay: `test/large-pages/purge-replay-segments.test.ts` (both engines).
+- `src/core/belief/purge-mirror.ts` — the text-free purge mirror `<brain home>/.gbrain/purge-events.jsonl` (0600, fsynced): an `intent` line before the purge commits (`appendPurgeIntent`; request, source and incarnation, scope, fingerprints, content hashes, lifetimes, epoch; never claim text; an unwritable mirror refuses the purge with the typed `purge_mirror_unwritable`, with or without a ledger) and a `committed` line after (`appendPurgeCommitted`, best effort; a page purge writes it from the coordinator's `afterCommit`, so it does not depend on the op handler finishing). `restorePgliteBackup` (`gbrain backup restore --purge-mirror <absolute path>` names a mirror other than the brain home's) replays every committed purge newer than the archive inside its transaction (`replayPurgesInRestore`: tombstones, row removal (a fact-scope replay rewrites page bodies and versions through `rewritePageBodies` / `redactPageVersions`, then runs `purgeSegmentStorage` with each claim's `stagingsHoldingClaim`; a page-scope replay deletes the page through `deletePageWithSegments`, so segment texts that held the purged content go in the replay, as in the purge), the three passes, the derived rows hidden again with `derived.hidden` cause `restore_replay`, one `fact.purged`/`page.purged` with cause `restore_replay`; a page-scope replay matches pages by content hash or lifetime, never by slug alone; an archive from before the ledger, no `belief_events`, replays tombstones and row removal and reports `ledger: 'absent'`; an intent whose request already has tombstones is skipped), records each as `complete`, `partial` (no restored row held the text, or the derivation walk could not run) or `unverified` (intent without committed line, not replayed), returns `purge_replay` in the receipt and adds a reconnect warning unless the state is `none` or `complete`; an unreadable mirror is `unavailable`. `coverage.restored` carries the outcome, which `readRestorePurgeReplay` reads back for the doctor. A Postgres restore runs `gbrain beliefs replay-purges --archived-at <iso> (--purge-mirror <abs path> | --from-url-env <VAR>) [--apply]` (`src/commands/beliefs-replay-purges.ts`; dry run by default: `replayPurges` runs the replay in a transaction that rolls back), whose database source `purgeLinesFromDatabase` rebuilds the intents from every pre-restore tombstone (not bounded by `purged_at`, the purge transaction's start) and skips those the archive already holds. #9469: a page-scope replay runs the live purge's sweep (`page-purge.ts` `sweepPurgedPage`, then `deletePurgedPage`), with one uncapped journal census per source per replay run (`replayRun`), and stops with `purge_replay_incomplete` naming the request when a sweep cannot complete (a missing table aside). Seam: `__setPurgeReplayFaultForTests` (fault `incomplete_census`). Probes: `test/belief-ledger-restore.test.ts`; segment storage after a replay: `test/large-pages/purge-replay-segments.test.ts` (both engines).
+- `src/commands/doctor/checks/purge-mirror.ts` — `purge_mirror` (#9469): compares the request ids of the last 30 days' `page_purges`, `fact_purges` and `take_purges` (capped at 10,000, `details.capped`) with the intent lines of this process's purge mirror (`configDir()`), and warns when any is missing; never a failure, never reads the request journal, prints counts and the mirror path only (no slug, title or text). Fix: one durable `GBRAIN_HOME` for every writer ([memory boundaries](../../guides/memory-boundaries.md#purge-mirror)). Test: `test/doctor-purge-mirror.test.ts`.
- `src/core/facts/gone-page-facts.ts` — `expireGonePageFacts(exec, pages)`: in the transaction that removes a page row for good, expires (never deletes) the active facts filed on the page (ontology observations excluded: their `source_markdown_slug` is provenance) and the active facts about it that no page row holds (a soft-deleted holder keeps its fact for a restore), so they don't come back for readers once the tombstone is gone and a new page at the slug starts clean. Two updates, one per slug index. Facts have no foreign key to pages. Called by the bulk retention purge (`engine-sql/pages.ts` `purgeDeletedPages`) and the belief-ledger purge of a recorded source (`persistence/purge-deleted.ts` `expireRecordedTombstones`).
- `src/core/facts/purge-overlay.ts` — purge mode of the import overlay: `dropPurgedFenceRows` drops facts-fence rows matching `fact_purges` and takes-fence rows matching `take_purges` (the takes fence re-renders with its reservation rows plus the dropped numbers reserved, so a purged number is never reused; pinned by `test/purge-overlay-reservations.test.ts`) (subject = page slug; malformed fences left to the withdrawal diagnostics, which refuse a purged claim inside one). Called first by `preserveWithdrawnFenceRows`, so `import-file.ts` needs no change; the snapshot overlay (`page-state/snapshot.ts`) and export (`export-snapshot.ts`) carry purge tombstones as `PageWithdrawal.purged`, which `overlayWithdrawalBody` drops instead of striking.
- `src/core/facts/purge-schema.ts` — `MEMORY_PURGE_SCHEMA_SQL` (schema.sql fragment: `fact_purges`, `take_purges`, `page_purges` keyed `(source_id, content_hash)`, `derivation_inputs`, `needs_rederive`) and `MEMORY_PURGE_GUARD_SQL` (migration only): `facts_refuse_purged`, `takes_refuse_purged` and `pages_refuse_purged` raise `P0001 purged_content: …` (mapped to typed `purged_content` by `persistence/publication-failure.ts`) instead of skipping the row, whatever `expired_at` says. The pages guard has no column list so it never pins columns. Since migration v264 `facts_refuse_purged` fires on UPDATE only; the facts insert check runs once per statement in `facts/insert-guard-schema.ts`. `FACT_PURGE_LOOKUP_INDEX_SQL` (migration v231, CONCURRENTLY on Postgres) adds `fact_purges_subject_idx` (a page's own tombstones, the '*' lookup by fingerprint) and the partial `fact_purges_all_subjects_idx` (subject '*' only, for the snapshot marker count).
@@ -17,13 +18,13 @@ User-facing contract: [expire versus purge](../../guides/memory-boundaries.md#pu
- `src/core/deletion-inventory.ts` — the deletion-target registry: every table with a text, JSON, bytes, array, lexeme or vector column, classed `swept`, `retained_inactive` or `out_of_scope` (`probed_reported` with probe columns, `text_free_ledger`, `secret_material`, `no_memory_text`), plus on-disk stores and out-of-reach residuals. `test/deletion-inventory.test.ts` fails on any unlisted text-bearing column of a fresh schema and pins the AGENTS.md "never promise physical erasure" line.
- `src/core/persistence/intent-redaction.ts` — accounting-aware intent redaction: `findRequestsCarrying` (intent, outcome, error detail or message, compacted rows included; the fact purge's planning read) / `findRequestsForSlugs` (index-served by `persistence_requests_slug_intent`), the page purge's preparation-time journal census `sweepRequestsCarrying` (#6511: a keyset walk of the source's journal in `JOURNAL_SWEEP_CHUNK_ROWS` chunks, every needle in raw and JSON-escaped form against intent, outcome, error detail and message, compacted rows included, the purge's own row excluded, stopped by the claim clock between chunks and at `JOURNAL_SWEEP_MATCH_CAP`; returns the carrying ids, the ids pending at the scan, the `sequence` ceiling as text and `complete`; #6884: `sweepRequestsForGroups` runs it once for several needle groups, each row read and lowered once and every hit attributed to the groups carrying that needle, the match cap per group, and `after` starts past an earlier ceiling) with `recheckRequestsCarrying` for the publication's bounded re-check of the tail past the ceiling and of the rows that were pending, `redactRequestIntents` locks counters then request rows, refuses on a non-terminal or recovering request (unless `skipPending`), nulls `intent`/`error_message` and replaces every outcome or error-detail string carrying a needle with `"[purged]"` (`redactJson`), with compaction's terminal-byte accounting, so replay keeps returning the (redacted) stored outcome. `findRequestsHoldingStagings` / `cancelRequestsHoldingStagings`: the unclaimed requests (queued, or deferred behind a refresh: GST-1602, `test/refresh-barrier-purge-gst1602.test.ts`) whose `intent.body_ref.staging_id` names a staging a fact purge aborts are completed `cancelled` with `purged_content`; a claimed one refuses `purge_blocked_pending_recovery`.
- `src/core/persistence/purged-intent.ts` — `intentCarriesPurgedContent`: `completeWrite` (journal.ts) nulls the intent of any terminal request whose content (`fact`, `claim`, page fence rows, or the page's content hash) matches a subject-scoped purge tombstone, and of any refusal coded `purged_content`; a write refused for another reason, or committed as a no-op after the overlay dropped purged rows, keeps no copy. Only content-bearing intents are checked, and whether the source holds any tombstone is selected by completeWrite's terminal statement itself (`PURGE_PRESENCE_COLUMNS`), so the hook adds no statement to a write on a source without tombstones (#6007 budget). `purged_content` is a write error code, so receipts and the frozen verb path (wire `invalid_params`, canonical `purged_content`) keep it typed.
-- `src/core/persistence/page-purge.ts` — #6884: `preparePagePurge` takes its census from `sweepForPurge`: a purge an earlier batch sweep covered for all of its current claims scans only the tail past that sweep's ceiling (`journal_sweep.watermark`); otherwise it sweeps once for its own claims and those of the next queued purges of the source (up to `PURGE_SWEEP_BATCH_MEMBERS`, `PURGE_SWEEP_BATCH_NEEDLES` claim forms) and caches each member's result in process memory (never in the journal; `journal_sweep.batch` counts the purges one sweep covered). Rows a member's cached census saw as pending, the leader's own row included, are re-tested at publication. Tests: `test/page-purge-batch-sweep-6884.test.ts`. `purgePageInTransaction`, called by the `delete_page purge:true` apply in `page-prepare.ts`: records in `page_purges` the content hash of the page and of every retained version (up to 1000), each as imported and as purge-overlaid, so an older version is refused too; drops the decide rows, trust proposals, gate holds (by slug) and gate receipts naming the page or its rows and clears legacy `query_cache`; tombstones and deletes facts filed on the page, tombstones its takes, deletes take proposals, open loops, core notices and `files` rows, the page's `page_aliases` and the `slug_aliases` redirects whose `canonical_slug` is the page (#6619; a redirect from this slug to another page stays; counted in `removed` and checked by slug in the receipt), redacts finished intents for the slug and the carrying ids the preparation sweep found plus the publication's tail and pending re-check (#6511: `preparePagePurge` runs `readPurgeClaims` and `sweepRequestsCarrying` under the preparation budget at steps `purge_claims` / `purge_journal_sweep`, so the O(journal) substring scan is a charged `preparation_deadline` release and `preparation_stalled` at the attempt limit, never a transaction timeout; the lookups that stay inside the transaction go through `lookup`, which maps a 57014 to the terminal, non-retryable `purge_lookup_timeout` naming `gbrain repair request-indexes` only when `persistence_requests_slug_intent` is missing or INVALID; the receipt's `journal_sweep` reports `ceiling`, `scanned`, `tail`, `pending_rechecked` and `complete`), then deletes the page (FK cascades). `PURGE_RESIDUALS` names what stays out of reach. `finishPagePurge` deletes stored blobs after commit, lists any left by storage path, and attaches `receipt`, the store-by-store receipt from `verifyPagePurge` (same shape as the fact receipt: residuals first, every swept inventory store, other pages' prose naming a purged claim as `source_prose`); the page's claims used as needles live only in process memory, and a same-id replay returns the result built when the purge ran. `residuals` stays the prose string. `readPagePurgeTombstones` feeds `screenImportContent({ purgedPages })` from managed sync's freeze-time screen (`sync-screen.ts`, one read per run) and publication (`sync-prepare.ts`), which hold a matching file as `purged_content`; direct imports and `put_page` hit the pages guard instead; `listPagePurges` / `unpurgePage` (also clears the fact and take tombstones of the same request) back the owner-only ops. Pinned by `test/page-purge.test.ts` and `test/page-purge-journal-timeout-6511.test.ts` (both engines; seams `purge:*` in `fault-points.ts`). #6368: a purge of a tombstone whose file matches `sync.exclude` (discovery's globs via `syncExcludePatterns` in `sync-discovery.ts`, matched relative to the source root) owns no file: `prepareFileTarget`'s `excludeTombstone` option returns no target, the purge is database-only and the receipt says `write_through.skipped: 'sync_excluded'`; the file is neither compared nor removed (an unchanged excluded file used to be deleted, an edited one refused forever). The apply re-reads the exclusion inside the publication transaction and refuses `source_changed` if it was removed meanwhile. Live-page purges and soft deletes are unchanged. A segmented page or version is never loaded whole: its stored `content_hash` and the purge-overlaid hash streamed by `rewrittenContentHash` are tombstoned; its stagings (by slug or page id) and the segment texts its manifests referenced or its stagings recorded in `page_body_staging_texts` (LPH (h), N14-5: a text an earlier write's sweep dropped from the manifest stays unreferenced for the GC grace window, and only the page's staging still names it) that no manifest row references, plus the source's unreferenced texts holding one of the page's claims (every fact filed on it, withdrawn ones included, and every take, not the journal sweep's first 32, tested in batches of `CLAIM_FORMS_BATCH` forms; plain or as a fence cell stores it, `claimTextForms` in fence-shared.ts: a text another page's write reused outlives this page's 24-hour staging record), are deleted in the purge transaction with the page (`deletePageWithSegments`, shared with the restore-time page-scope replay; `removed.page_body_manifest` / `page_body_stagings` / `page_segment_texts`), except a text another page's live staging recorded in `page_body_staging_texts` (an unpublished create or replace still needs it; `stagingNeedsTextSql`, also applied by the 72-hour hard delete and the receipt's text probe). #7778: the page's tombstones, mirror facts and journal and segment sweeps leave out its extraction outcome rows (`outcomeMarkerSql`); a subject-'*' tombstone on the shared marker text would keep every page of the source from recording its outcome.
+- `src/core/persistence/page-purge.ts` — #6884: `preparePagePurge` takes its census from `sweepForPurge`: a purge an earlier batch sweep covered for all of its current claims scans only the tail past that sweep's ceiling (`journal_sweep.watermark`); otherwise it sweeps once for its own claims and those of the next queued purges of the source (up to `PURGE_SWEEP_BATCH_MEMBERS`, `PURGE_SWEEP_BATCH_NEEDLES` claim forms) and caches each member's result in process memory (never in the journal; `journal_sweep.batch` counts the purges one sweep covered). Rows a member's cached census saw as pending, the leader's own row included, are re-tested at publication. Tests: `test/page-purge-batch-sweep-6884.test.ts`. `purgePageInTransaction`, called by the `delete_page purge:true` apply in `page-prepare.ts`: records in `page_purges` the content hash of the page and of every retained version (up to 1000), each as imported and as purge-overlaid, so an older version is refused too; drops the decide rows, trust proposals, gate holds (by slug) and gate receipts naming the page or its rows and clears legacy `query_cache`; tombstones and deletes facts filed on the page, tombstones its takes, deletes take proposals, open loops, core notices and `files` rows, the page's `page_aliases` and the `slug_aliases` redirects whose `canonical_slug` is the page (#6619; a redirect from this slug to another page stays; counted in `removed` and checked by slug in the receipt), redacts finished intents for the slug and the carrying ids the preparation sweep found plus the publication's tail and pending re-check (#6511: `preparePagePurge` runs `readPurgeClaims` and `sweepRequestsCarrying` under the preparation budget at steps `purge_claims` / `purge_journal_sweep`, so the O(journal) substring scan is a charged `preparation_deadline` release and `preparation_stalled` at the attempt limit, never a transaction timeout; the lookups that stay inside the transaction go through `lookup`, which maps a 57014 to the terminal, non-retryable `purge_lookup_timeout` naming `gbrain repair request-indexes` only when `persistence_requests_slug_intent` is missing or INVALID; the receipt's `journal_sweep` reports `ceiling`, `scanned`, `tail`, `pending_rechecked` and `complete`), then deletes the page (FK cascades). `PURGE_RESIDUALS` names what stays out of reach. `finishPagePurge` deletes stored blobs after commit, lists any left by storage path, and attaches `receipt`, the store-by-store receipt from `verifyPagePurge` (same shape as the fact receipt: residuals first, every swept inventory store, other pages' prose naming a purged claim as `source_prose`); the page's claims used as needles live only in process memory, and a same-id replay returns the result built when the purge ran. `residuals` stays the prose string. `readPagePurgeTombstones` feeds `screenImportContent({ purgedPages })` from managed sync's freeze-time screen (`sync-screen.ts`, one read per run) and publication (`sync-prepare.ts`), which hold a matching file as `purged_content`; direct imports and `put_page` hit the pages guard instead; `listPagePurges` / `unpurgePage` (also clears the fact and take tombstones of the same request) back the owner-only ops. Pinned by `test/page-purge.test.ts` and `test/page-purge-journal-timeout-6511.test.ts` (both engines; seams `purge:*` in `fault-points.ts`). #6368: a purge of a tombstone whose file matches `sync.exclude` (discovery's globs via `syncExcludePatterns` in `sync-discovery.ts`, matched relative to the source root) owns no file: `prepareFileTarget`'s `excludeTombstone` option returns no target, the purge is database-only and the receipt says `write_through.skipped: 'sync_excluded'`; the file is neither compared nor removed (an unchanged excluded file used to be deleted, an edited one refused forever). The apply re-reads the exclusion inside the publication transaction and refuses `source_changed` if it was removed meanwhile. Live-page purges and soft deletes are unchanged. A segmented page or version is never loaded whole: its stored `content_hash` and the purge-overlaid hash streamed by `rewrittenContentHash` are tombstoned; its stagings (by slug or page id) and the segment texts its manifests referenced or its stagings recorded in `page_body_staging_texts` (LPH (h), N14-5: a text an earlier write's sweep dropped from the manifest stays unreferenced for the GC grace window, and only the page's staging still names it) that no manifest row references, plus the source's unreferenced texts holding one of the page's claims (every fact filed on it, withdrawn ones included, and every take, not the journal sweep's first 32, tested in batches of `CLAIM_FORMS_BATCH` forms; plain or as a fence cell stores it, `claimTextForms` in fence-shared.ts: a text another page's write reused outlives this page's 24-hour staging record), are deleted in the purge transaction with the page (`deletePageWithSegments`, shared with the restore-time page-scope replay; `removed.page_body_manifest` / `page_body_stagings` / `page_segment_texts`), except a text another page's live staging recorded in `page_body_staging_texts` (an unpublished create or replace still needs it; `stagingNeedsTextSql`, also applied by the 72-hour hard delete and the receipt's text probe). #7778: the page's tombstones, mirror facts and journal and segment sweeps leave out its extraction outcome rows (`outcomeMarkerSql`); a subject-'*' tombstone on the shared marker text would keep every page of the source from recording its outcome. #9469: the in-transaction sweep is `sweepPurgedPage` (its stores are `PURGE_SWEEP_STORES`) and the delete `deletePurgedPage`, both shared with the restore-time page-scope replay.
- `src/core/ops/purge.ts` — `purge_fact`, `list_page_purges`, `unpurge_page`: admin scope, `cliOnly` (every MCP route gets `trusted_local_only`). The resident owner runs them only for its verified local CLI registration (`LOCAL_CLI_OWNER_OPERATIONS` in `persistence/ipc.ts`, checked in `persistence/provider.ts`).
- `src/commands/forget-purge.ts` + `src/commands/pages-purges.ts` — the CLI fronts. `runForgetPurge` always runs the dry run, then a typed token on a terminal or `--yes` plus `--request-id` without one; `--match` lists ids, `--status` polls completion; `PURGE_FAMILY_HELP` is the purge-family table shown by `gbrain forget --help`. `gbrain pages purges list` and `gbrain pages unpurge <slug>` run the page-tombstone ops as the trusted CLI.
- Withdrawn-claim refusal on `remember` (`persistence/memory-mutations.ts`, `memory-prepare.ts`) — admission refuses a withdrawn claim (`fact_withdrawn`, `FACT_WITHDRAWN_MESSAGE`/`_SUGGESTION` in `src/core/facts/withdrawal.ts`) before a request exists, and preparation re-checks it; `writeFailureDiagnostic` allowlists that prefix, so a failed receipt and `get_write_request` name it. `replaces` accepts a target saved without an entity (links it by supersession). Test: `test/remember-withdrawn-claim.test.ts`. (R1, Cat 40 Hard.) The refusal carries an additive `withdrawn` object (`factWithdrawnRefusal` in `facts/single-prepare.ts`, from `fact_withdrawals` only: the named entity's row over `all_entities`, then the newest, omitted when the caller cannot read the forgotten rows); `withdrawalFailureDetail`/`withdrawnFromDetail` in `facts/withdrawal.ts` keep it in the failed receipt's `error_detail` for `get_write_request` and replay. Tests: `test/withdrawn-detail.test.ts`, `test/e2e/withdrawn-detail-postgres.test.ts`.
-- `src/core/persistence/purge-deleted.ts` — `purgeDeletedPagesCoordinated` backs the cycle `purge` phase, the purge job, `purge_deleted_pages` and `gbrain pages purge-deleted`. Unmanaged brains keep the engine bulk delete, after the belief-ledger expiry of recorded sources' tombstones: each is its own transaction and item (ItemRun site `purge.recorded_tombstones`), a run-scoped failure or a systemic run stops the purge, and any other failure lists the tombstone in `blocked` with its classified code, counts it in `failed` (no `error`, so the phase warns) and keeps it out of the bulk delete (`purgeDeletedPages` `excludeIds`); a content failure counts toward the same park as the managed purge, a transient one is retried next run, and parked tombstones are skipped as `purge_parked` unless `retryParked` (the unmanaged preview excludes them too). A managed brain purges each tombstone older than the window (skipping archived sources, which the source lifecycle purges) through `delete_page` with `purge: true` and the internal `retention: true` bound to its revision (retention is not an owner purge; see [belief ledger](belief-ledger.md)). Writer state is read once per run (`writerOnboardingPreflight`): a source whose writer is `recovery_required` submits nothing and its tombstones are listed in `blocked` as `writer_not_active` (counted in `inactive`). Revision races are deferred to the next run; a parked tombstone (`purge-parks.ts`) is skipped as `purge_parked` (counted in `parked`) unless `retryParked`, and any other failure is listed in `blocked` and sets `error`. `selectPurgeCandidates` is the one eligibility selector (preview, consent, `purge_deleted_pages` op, run), and its holds are `purgeHoldsSql` (archived source, rename-identity, restorable-atom and derived-retirement holds), which doctor `embedding_state` also counts with; #6884: the run submits only while the writer has outstanding-request room (`readOutstandingRoom` in `journal.ts`, at `persistence.capacity.background_pause_pct` of the cap for a background run); the rest, and the rest after a `queue_capacity` refusal, is `blocked` as `write_capacity_deferred` and counted in `capacity_deferred`, never a failure or a park. `purgePhaseStatus`/`purgePhaseNote` shape the cycle purge phase (fail on `error`; warn on `writer_not_active`, parked, capacity-deferred or an unmanaged `failed` tombstone). The engine bulk delete (`engine-sql/pages.ts` `purgeDeletedPages`) also deletes, in its transaction, the purged pages' stagings (by source and slug) and the segment texts only their manifests referenced. GST-2689: every run first calls `repair/origin-carry.ts` `carryPrivateBeforePurge` (both branches); the origins it parks are listed in `blocked` as `derived_origin_parked` and counted in `origin_parked` (the phase warns), and `derivedOriginPurgeHold` is in `purgeHoldsSql`, the recorded-tombstone scan and delete, and the engine bulk delete. Tests: `test/managed-writers-w3.test.ts`, `test/purge-deleted-parks.test.ts`, `test/purge-deleted-capacity-6884.test.ts`, `test/pathological/a49-unmanaged-purge-tombstones.test.ts`, `test/derived-origin-carry-private.test.ts`.
+- `src/core/persistence/purge-deleted.ts` — `purgeDeletedPagesCoordinated` backs the cycle `purge` phase, the purge job, `purge_deleted_pages` and `gbrain pages purge-deleted`. Unmanaged brains keep the engine bulk delete, after the belief-ledger expiry of recorded sources' tombstones: each is its own transaction and item (ItemRun site `purge.recorded_tombstones`), a run-scoped failure or a systemic run stops the purge, and any other failure lists the tombstone in `blocked` with its classified code, counts it in `failed` (no `error`, so the phase warns) and keeps it out of the bulk delete (`purgeDeletedPages` `excludeIds`); a content failure counts toward the same park as the managed purge, a transient one is retried next run, and parked tombstones are skipped as `purge_parked` unless `retryParked` (the unmanaged preview excludes them too). A managed brain purges each tombstone older than the window (skipping archived sources, which the source lifecycle purges) through `delete_page` with `purge: true` and the internal `retention: true` bound to its revision (retention is not an owner purge; see [belief ledger](belief-ledger.md)). Writer state is read once per run (`writerOnboardingPreflight`): a source whose writer is `recovery_required` submits nothing and its tombstones are listed in `blocked` as `writer_not_active` (counted in `inactive`). Revision races are deferred to the next run; a parked tombstone (`purge-parks.ts`) is skipped as `purge_parked` (counted in `parked`) unless `retryParked`, and any other failure is listed in `blocked` and sets `error`. `selectPurgeCandidates` is the one eligibility selector (preview, consent, `purge_deleted_pages` op, run), and its holds are `purgeHoldsSql` (archived source, rename-identity, restorable-atom and derived-retirement holds), which doctor `embedding_state` also counts with; #6884: the run submits only while the writer has outstanding-request room (`readOutstandingRoom` in `journal.ts`, at `persistence.capacity.background_pause_pct` of the cap for a background run); the rest, and the rest after a `queue_capacity` refusal, is `blocked` as `write_capacity_deferred` and counted in `capacity_deferred`, never a failure or a park. `purgePhaseStatus`/`purgePhaseNote` shape the cycle purge phase (fail on `error`; warn on `writer_not_active`, parked, capacity-deferred or an unmanaged `failed` tombstone). The engine bulk delete (`engine-sql/pages.ts` `purgeDeletedPages`) also deletes, in its transaction, the purged pages' stagings (by source and slug) and the segment texts only their manifests referenced. GST-2689: every run first calls `repair/origin-carry.ts` `carryPrivateBeforePurge` (both branches); the origins it parks are listed in `blocked` as `derived_origin_parked` and counted in `origin_parked` (the phase warns), and `derivedOriginPurgeHold` is in `purgeHoldsSql`, the recorded-tombstone scan and delete, and the engine bulk delete. GST-2766: a page whose owner purge was held is owed that purge (`derived-origin-hold.ts` `recordOwedPurge`, op `owed-page-purge`, keyed by page id, no slug, live only while that tombstone exists); a managed run submits it as the owner purge, and an unmanaged run keeps it out of both bulk expiries and submits it through `purgeOwedPages` as an explicit `delete_page` purge, so the purge tombstones are written whichever run removes the page. The purge transaction (`page-purge.ts` `deletePurgedPage`), `engine-sql/pages.ts` `restorePage` and the `restore_page` apply clear the record. Tests: `test/managed-writers-w3.test.ts`, `test/purge-deleted-parks.test.ts`, `test/purge-deleted-capacity-6884.test.ts`, `test/pathological/a49-unmanaged-purge-tombstones.test.ts`, `test/derived-origin-carry-private.test.ts`.
- `src/core/persistence/derived-retirement-hold.ts` — GBRA-161: the purge hold for every derived retirement. `DERIVED_RETIRED_BY` lists the `retired_by` stamps derived writers put on the pages they retire (extract-atoms, managed-reextract, stale-atoms, life-chronicle); `derivedRetirementPurgeHold(alias)` keeps a stamped page out of every purge route (engine bulk purge and its preview, the coordinated selection and delete) until `DERIVED_RETIREMENT_HOLD_DAYS` (30) after `deleted_at` (the database clock, never the `retired_at` text); `retireDerivedPages` versions, soft-deletes and stamps a batch in one attributed transaction (unmanaged extract_atoms). `assertRetirementStampUnchanged` is the page preparer's refusal (`reserved_frontmatter_key`) of a remote write that adds or changes one of `RESERVED_RETIREMENT_KEYS`. The hold length follows `generations.restore_days` (`RESTORE_DAYS_SQL`, read inside the purge statement; 30 by default) and `retireDerivedPagesInTransaction` is the in-transaction form generation adapters retire through. An explicit `gbrain delete --purge` is not held.
- `src/core/generations/ledger-scrub.ts` — GBRA-161: purge reaches the generation ledger. A page purge (`page-purge.ts`) nulls the purged item's preimages in `derived_generation_items` and every held proposal in `derived_generations` naming it, and deletes the page's fence holds (`FENCE_GENERATION_KINDS` under `page:<slug>`, whose rows the purge deletes; `test/generations-purge-fence-hold-7774.test.ts`); a fact purge (`facts/purge.ts`) nulls every preimage, proposal and held context carrying the claim. Both are `swept` (`generation_ledger`) in the deletion inventory, so the verification probes them; `gbrain repair generations` then refuses a scrubbed item with `no_preimage`.
- `src/core/persistence/purge-name-stores.ts` — #9068: `scrubPurgedNameStores(engine, sourceId, slugs)` deletes the source's `context_volunteer_events` rows (whose rationale quotes the matched name) and `mention_gazetteer_entries` rows (`name_norm -> target_slug`) for purged slugs. Every hard delete of a page calls it inside its transaction (`purgePageInTransaction`, `purgeDeletedPages`, `expireRecordedTombstones`, the restore purge replay in `belief/purge-mirror.ts`, and the engines' batch `deletePages`); removing a source deletes the source's volunteer rows (`removedSourceScrubCtes` in `source-delete.ts`) and its gazetteer rows by cascade; a soft delete keeps both. Both tables are `swept` (`page_names`) in the deletion inventory; the page receipt checks them by slug, and the fact receipt probes them (rationale, name_norm) and reports a match as `probed_reported`, because a fact purge sweeps neither.
diff --git a/docs/contracts/public-surface.json b/docs/contracts/public-surface.json
index 4b4d0a198e..2005eae934 100644
--- a/docs/contracts/public-surface.json
+++ b/docs/contracts/public-surface.json
@@ -2824,6 +2824,7 @@
"purge_blocked_pending_recovery": 1,
"purge_lookup_timeout": 1,
"purge_mirror_unwritable": 1,
+ "purge_replay_incomplete": 1,
"purged_content": 1,
"push_unconfirmed": 4,
"question_not_found": 1,
diff --git a/docs/guides/error-codes.md b/docs/guides/error-codes.md
index 9b5920cb99..4ee82cde41 100644
--- a/docs/guides/error-codes.md
+++ b/docs/guides/error-codes.md
@@ -4142,6 +4142,16 @@ More: [docs/guides/write-refusals.md#purge_lookup_timeout](../../docs/guides/wri
More: [docs/guides/write-refusals.md#purge_mirror_unwritable](../../docs/guides/write-refusals.md#purge_mirror_unwritable)
+### purge_replay_incomplete
+
+<a id="purge_replay_incomplete"></a>
+
+| Meaning | Why | Next step | Who acts · consent | Verify | Exit | Retryable |
+|---|---|---|---|---|---|---|
+| A restore stopped because the replay of a purge could not sweep every stored request that carries the purged page; that purge was not replayed and nothing was restored. | A replayed purge removes what the live purge removed, the request journal included; reporting a partial sweep as replayed would leave purged text in the restored brain. | Do not use the restored copy. Report the purge request named in the message with the output of gbrain doctor --json; restore again once it is fixed. | host_admin | `repeat the read that failed` | 1 | no |
+
+More: [docs/guides/write-refusals.md#purge_replay_incomplete](../../docs/guides/write-refusals.md#purge_replay_incomplete)
+
### purged_content
<a id="purged_content"></a>
diff --git a/docs/guides/memory-boundaries.md b/docs/guides/memory-boundaries.md
index 34faa6154a..58bb266825 100644
--- a/docs/guides/memory-boundaries.md
+++ b/docs/guides/memory-boundaries.md
@@ -574,6 +574,19 @@ the purged page's facts. A write refused because it carries purged content,
or accepted as a no-op after its purged rows were dropped, keeps no copy of
that content in its stored write request.
+An owner purge held to keep a derived page private (`derived_origin_parked`)
+stays owed until its page is removed or restored: whichever purge later removes
+the page, the automatic one included, writes the purge tombstones.
+
+<a id="purge-mirror"></a>
+A restore replays every purge newer than the backup from the purge mirror,
+`purge-events.jsonl` in gbrain's config directory (`$GBRAIN_HOME/.gbrain`,
+else `~/.gbrain`), and a replayed page purge sweeps the same stores as the
+live purge. A host that runs each gbrain writer in a home directory it removes
+after the run loses that mirror, so run every writer with `GBRAIN_HOME` set to
+one durable directory. `gbrain doctor --only purge_mirror` warns when a purge
+from the last 30 days has no line in the mirror this process uses.
+
## Verify in the actual harness
Use a generic, unique test fact: remember it, recall it, correct it, recall the
diff --git a/docs/guides/troubleshooting.md b/docs/guides/troubleshooting.md
index e67e818df5..ae44e5065c 100644
--- a/docs/guides/troubleshooting.md
+++ b/docs/guides/troubleshooting.md
@@ -75,6 +75,7 @@
| [`owner_unavailable`, reason `binding_missing`, `incarnation_changed`, `local_path_missing` or `coordination_path_missing`](#owner-unavailable) | `gbrain sources writer status --source <id> --json`; the registration is missing or outdated and re-binding or re-registering the checkout is the host administrator's decision | brain host | none | the same status read shows an active binding for this host |
| Background work (autopilot, jobs, connector syncs) refused `owner_unavailable` "Background writes to source <id> are paused: its canonical owner could not be verified" or "its worktree is recovering" | run the fix, `gbrain sources writer status --source <id> --json`, which names what the source waits on; a deleted owner marker (`.gbrain-owner.json`) is repaired by a writer self-transfer, the user's decision. Nothing was submitted and no request id was used; interactive writes are unaffected | agent; the self-transfer after the user agrees | none to inspect | the same status read shows an active binding; the next background run admits |
| Doctor warns `purge_parked_pages` | a park an older gbrain wrote is retried once by the next purge run after an upgrade and is not counted; for the rest, fix the cause each page's last error names, preview `gbrain pages purge-deleted --retry-parked --dry-run --json`, then with the user's agreement run `gbrain pages purge-deleted --retry-parked --yes` | agent, after the user agrees | `destructive` | `gbrain doctor --only purge_parked_pages --json` |
+| Doctor warns `purge_mirror` | some purges of the last 30 days have no line in the purge mirror this process uses (`<config dir>/purge-events.jsonl`), so a restore from an older backup would not replay them; gbrain ran in a home directory removed after the run, or on another machine. Run every gbrain writer with `GBRAIN_HOME` set to one durable directory | brain host operator | none | `gbrain doctor --only purge_mirror --json` |
| Doctor warns `derived_origin_parked` | a soft-deleted page is kept from the purge because a page derived from it could not be labelled private; fix the cause the reason names, or preview the blocking page's own label with the fix's `gbrain repair visibility --source <id> --slug <page>`; the next purge labels the rest and frees the page, and the purge after removes it (a consented `gbrain pages purge-deleted --yes` does both). A page whose own `gbrain delete <slug> --purge` was held is named with `gbrain delete <slug> --purge --source-id <id>`: run it again as a new request once the blocking page is labelled, so the purge tombstones are written | agent | none (the preview writes nothing) | `gbrain doctor --only derived_origin_parked --json` |
| [`gbrain repair fences` skipped a file `sync_in_progress` while a sync runs](repair.md#fence-repair-during-a-sync) | nothing: a write in flight or the running sync's frozen manifest still names that file; every other candidate repairs now and the next run picks this one up (`gbrain sync --source <id> --no-pull` finishes the sync) | agent | none | `gbrain doctor --only fence_integrity --json` |
| [A Google or GitHub item is held (`connector_held_items`)](#held-connector-items) | `gbrain sources status <id>`, fix the cause, then `gbrain sources retry-held <id>` and `gbrain sync --source <id>` | agent, after the user agrees | `egress` (fetches from the provider again) | `gbrain doctor --only connector_held_items --json` |
diff --git a/docs/guides/write-refusals.md b/docs/guides/write-refusals.md
index 88ea6cf36d..04154dee52 100644
--- a/docs/guides/write-refusals.md
+++ b/docs/guides/write-refusals.md
@@ -108,6 +108,7 @@ of making a duplicate.
| <a id="timeline_rows_would_be_removed"></a>`timeline_rows_would_be_removed` | `timeline_rows_would_be_removed` | A remote `put_page` (or a `put_pages` entry) has no Timeline section at all — no `<!-- timeline -->` sentinel and no `## Timeline` or `## History` heading — and would delete dated entries the page has, including rows `add_timeline_entry` added. `put_page` content replaces the whole page, so the omission must be explicit. Nothing was written. A section that is present but empty is a deliberate delete and is not refused; a local `gbrain put` without `--expected-revision` keeps the rows instead. | Re-read with `get_page include_content:true`, keep the Timeline section and resubmit with a new `request_id`; to remove every dated entry on purpose, resubmit the same content with `drop_timeline: true` (on put_pages, per entry). A successful write that deletes rows reports `timeline_rows_removed {count, earliest, latest, warning, fix}` (dates only); `get_versions` then `revert_version` restores them as new rows. |
| <a id="purge_lookup_timeout"></a>`purge_lookup_timeout` | `purge_lookup_timeout` | A page purge (`gbrain delete <slug> --purge`) ran one of the journal lookups that still execute inside its publication transaction (the lookup of stored intents for the slug, the re-check of requests admitted after its preparation scan or pending at that scan, the locking read of the requests to redact) and that statement hit the transaction's 5-second statement budget on a large `persistence_requests` table. The request failed terminally instead of being released as `database_contention` and claimed again: nothing of the page was removed or tombstoned, nothing in the journal was redacted, and later writes to the source move again. Do not assume the requested content was erased. The substring search for the page's claims no longer runs here: it runs at preparation in bounded keyset chunks under the preparation budget, and a sweep that outruns that budget twice ends [`preparation_stalled`](#preparation_stalled) instead. A committed purge's receipt reports `journal_sweep` (`scanned`, `ceiling`, `tail`, `pending_rechecked`, `complete`); `complete: false` means a cap was hit (the 10,000-match cap or the tail bound) and some requests were not tested. | If the receipt says the slug index `persistence_requests_slug_intent` is missing or INVALID, run `gbrain repair request-indexes --apply` (the `persistence_request_slug_intent_index` migration builds it online; the index serves the slug lookup, not the substring sweep), then submit the purge again with a new `request_id`. Otherwise submit it again with a new `request_id` (a retry with the same id returns this failure); if it times out again, report the request id with the `persistence_request_indexes` and `persistence_request_growth` entries of `gbrain doctor --json`. |
| <a id="purge_mirror_unwritable"></a>`purge_mirror_unwritable` | `purge_mirror_unwritable` | A fact or page purge appends a text-free `intent` line to the purge mirror (`<brain home>/.gbrain/purge-events.jsonl`) and fsyncs it before it commits, so a later restore of an older backup can replay the purge. The mirror could not be written (a directory in its place, a read-only home, a full disk), so the purge was refused: nothing was removed, tombstoned or redacted. Do not assume the requested content was erased. | Make the mirror a writable regular file owned by the brain's user (mode 0600), then run the purge again with a new `request_id`. |
+| <a id="purge_replay_incomplete"></a>`purge_replay_incomplete` | `purge_replay_incomplete` | A restore replays every purge newer than the backup, and each replayed page purge sweeps what the live purge swept, the stored requests that carry the page included. The replay of the named purge could not cover every such request, so the restore stopped: that purge was not replayed and nothing was restored. A restore never reports a partial sweep as replayed. | Do not use the restored copy. Report the purge request named in the message with the output of `gbrain doctor --json`, then restore again once it is fixed. |
| <a id="preparation_stalled"></a>`preparation_stalled` | `preparation_stalled` | The write's owner could not finish *preparing* it. Every write has a preparation budget (`persistence.sync_preparation_ms` for a managed sync member, `persistence.maintenance_preparation_ms` for every other managed kind, 120 s each; `remember`, `put_page` and `edit_page` keep 30 s), the owner releases the claim at the budget and tries again, and a request cut off `persistence.max_preparation_attempts` times (default 2; a process killed mid-preparation counts too) is finished `failed` with this code instead of being claimed again and holding the rest of its root. The receipt names the last recorded step and what it was waiting on (`git`, `fs`, `db`, `pool` or `unknown`); while the write was still running, `gbrain sources writer status --json` showed the same on `claim.stall` (`preparation_overdue`). The rest of the root keeps publishing. A **managed sync member** (an import, delete or rename of a file) is also **held**: the sync turns the failed receipt into a `preparation_stalled` hold and carries on, so the file is not imported until it is retried. The file is fine; this is the write owner stalling (a database or pool wait, a hung step), never a frontmatter or fence problem, so no repair applies. The hold's `message` names the request and the step; a held deletion keeps its page. A stalled hold re-screens by itself only when the gbrain version changes; a run that stalls many files in a row stops with [`preparation_systemic`](#preparation_systemic) instead of holding them all. A **foreground write** (`put_page`, `edit_page`, `remember`) or a **maintenance write** (fact-fence adoption, maintenance page writes) gets the terminal receipt only. The deadlines sit behind `persistence.preparation_deadlines` (`false` restores the old behaviour, where only `remember`, `put_page` and `edit_page` had a 30 s budget). | Run `gbrain sources writer status --source <source> --json` (read-only: the step, its wait cause, the owner process and its gbrain version), fix what it names (restart or upgrade an owner that predates deadlines, free the pool, let the lock holder finish), then for a sync hold run `gbrain sources retry-held <source>` and the same `gbrain sync` with the same options (the hold's `fix.then.then` and `retry-held` print it, for example `gbrain sync --source <source> --no-pull --no-embed`). When that sync still has a backlog to finish, it finishes it first and re-screens the scheduled files in the same invocation (the drain yields at the checkpoint and discovers again), so one run of the printed command clears the hold. A foreground write needs a new `request_id`; the next maintenance run submits its write again. Runbook: [catch-up stuck / held N files](troubleshooting.md#catch-up-stuck). |
| `file_unreadable` (sync hold) | `file_unreadable` | A managed sync held one file because it is not a readable regular file inside the source root: a symlink (to a file outside the root or inside it), a non-regular file, or a permission or loop error. Sync never reads or follows it. Until GBRA-130 the whole sync stopped at that file on every run and the rest of the source never imported; now only the file waits. A source root that is itself replaced by a symlink still refuses the whole sync. | Make the file a readable regular file in the checkout (replace the symlink with the file, fix its permissions) or exclude it with `sync.exclude`, and commit. A commit that changes it re-screens the hold on the next sync; after a permission fix run `gbrain sources retry-held <source>`, then the same sync. |
| <a id="page_write_failed"></a>`page_write_failed` (sync hold) | `page_write_failed` | A managed sync held one file because its write failed the same way three times (the same code, origin, error class and SQLSTATE in this source), with an error gbrain cannot classify: an owner exception (for example Postgres 54000 when a frontmatter tag is too long for its index), or a refusal the receipt does not explain. Until GBRA-130 every sync stopped `blocked_by_failures` at that file and the rest of the source never imported; now the file is held and the rest of the source syncs. The first two failures still stop the run, so a one-off defect never holds a file. | Read the receipt the hold names (`gbrain write-request <request_id>`, read-only). Fix the file when the error names its content (shorten the tag, split the page), or report the receipt with the gbrain version. Then `gbrain sources retry-held <source>` and the same sync. An upgrade re-tests the file on its own. A hold whose `meta.write_failure.origin` is `freeze` (GBRA-133) is different: sync could not even prepare the file (an unexpected error while reading or screening it, before any request was admitted), so it holds it on the first failure and names the error in the hold message; the next syncs re-test it on their own until it has been held 3 times, so a transient fault clears without help. When most files of one run fail that way the run stops instead (the cause is not the files). |
diff --git a/llms-full.txt b/llms-full.txt
index 738e47c1a6..c65cb41595 100644
--- a/llms-full.txt
+++ b/llms-full.txt
@@ -9270,6 +9270,7 @@ Source: https://raw.githubusercontent.com/garrytan/gbrain/master/docs/guides/tro
| [`owner_unavailable`, reason `binding_missing`, `incarnation_changed`, `local_path_missing` or `coordination_path_missing`](#owner-unavailable) | `gbrain sources writer status --source <id> --json`; the registration is missing or outdated and re-binding or re-registering the checkout is the host administrator's decision | brain host | none | the same status read shows an active binding for this host |
| Background work (autopilot, jobs, connector syncs) refused `owner_unavailable` "Background writes to source <id> are paused: its canonical owner could not be verified" or "its worktree is recovering" | run the fix, `gbrain sources writer status --source <id> --json`, which names what the source waits on; a deleted owner marker (`.gbrain-owner.json`) is repaired by a writer self-transfer, the user's decision. Nothing was submitted and no request id was used; interactive writes are unaffected | agent; the self-transfer after the user agrees | none to inspect | the same status read shows an active binding; the next background run admits |
| Doctor warns `purge_parked_pages` | a park an older gbrain wrote is retried once by the next purge run after an upgrade and is not counted; for the rest, fix the cause each page's last error names, preview `gbrain pages purge-deleted --retry-parked --dry-run --json`, then with the user's agreement run `gbrain pages purge-deleted --retry-parked --yes` | agent, after the user agrees | `destructive` | `gbrain doctor --only purge_parked_pages --json` |
+| Doctor warns `purge_mirror` | some purges of the last 30 days have no line in the purge mirror this process uses (`<config dir>/purge-events.jsonl`), so a restore from an older backup would not replay them; gbrain ran in a home directory removed after the run, or on another machine. Run every gbrain writer with `GBRAIN_HOME` set to one durable directory | brain host operator | none | `gbrain doctor --only purge_mirror --json` |
| Doctor warns `derived_origin_parked` | a soft-deleted page is kept from the purge because a page derived from it could not be labelled private; fix the cause the reason names, or preview the blocking page's own label with the fix's `gbrain repair visibility --source <id> --slug <page>`; the next purge labels the rest and frees the page, and the purge after removes it (a consented `gbrain pages purge-deleted --yes` does both). A page whose own `gbrain delete <slug> --purge` was held is named with `gbrain delete <slug> --purge --source-id <id>`: run it again as a new request once the blocking page is labelled, so the purge tombstones are written | agent | none (the preview writes nothing) | `gbrain doctor --only derived_origin_parked --json` |
| [`gbrain repair fences` skipped a file `sync_in_progress` while a sync runs](repair.md#fence-repair-during-a-sync) | nothing: a write in flight or the running sync's frozen manifest still names that file; every other candidate repairs now and the next run picks this one up (`gbrain sync --source <id> --no-pull` finishes the sync) | agent | none | `gbrain doctor --only fence_integrity --json` |
| [A Google or GitHub item is held (`connector_held_items`)](#held-connector-items) | `gbrain sources status <id>`, fix the cause, then `gbrain sources retry-held <id>` and `gbrain sync --source <id>` | agent, after the user agrees | `egress` (fetches from the provider again) | `gbrain doctor --only connector_held_items --json` |
diff --git a/src/commands/doctor/checks/purge-mirror.ts b/src/commands/doctor/checks/purge-mirror.ts
new file mode 100644
index 0000000000..6c91bf5b31
--- /dev/null
+++ b/src/commands/doctor/checks/purge-mirror.ts
@@ -0,0 +1,59 @@
+/**
+ * #9469: doctor `purge_mirror`. A restore replays every purge newer than the backup from the purge mirror, a text-free
+ * file in this process's config directory (`GBRAIN_HOME`, else the home directory). A host that runs each writer in a home
+ * of its own that it removes after the run loses the mirror, and a restore from an older backup then brings the purged
+ * text back. This check compares the purges the database's tombstones record over the last ${WINDOW_DAYS} days with the
+ * requests this mirror holds, and warns when the mirror lacks any. It reads only the three tombstone tables (bounded by
+ * the window and ${REQUEST_CAP} requests) and the mirror file, never the request journal, and it prints counts only:
+ * never a slug, title or text of a purged page. Never a failure: the purges themselves are done.
+ */
+import type { Check } from '../../doctor.ts';
+import type { BrainEngine } from '../../../core/engine.ts';
+import { configDir } from '../../../core/config.ts';
+import { purgeMirrorPath, readPurgeMirror } from '../../../core/belief/purge-mirror.ts';
+import { checkError, doctorVerify } from '../check-fix.ts';
+import { connectedEngine, type DoctorContext, type DoctorEntry } from '../context.ts';
+
+const NAME = 'purge_mirror';
+export const PURGE_MIRROR_WINDOW_DAYS = 30;
+const WINDOW_DAYS = PURGE_MIRROR_WINDOW_DAYS;
+const REQUEST_CAP = 10_000;
+const DOCS = 'docs/guides/memory-boundaries.md#purge-mirror';
+
+export async function checkPurgeMirror(engine: BrainEngine, home = configDir()): Promise<Check> {
+ let requests: string[];
+ try {
+ requests = (await engine.executeRaw<{ request: string }>(`SELECT DISTINCT request FROM (
+ SELECT request_id::text AS request FROM page_purges WHERE request_id IS NOT NULL AND purged_at > now() - make_interval(days => $1)
+ UNION SELECT request_id::text FROM fact_purges WHERE request_id IS NOT NULL AND purged_at > now() - make_interval(days => $1)
+ UNION SELECT request_id::text FROM take_purges WHERE request_id IS NOT NULL AND purged_at > now() - make_interval(days => $1)) r LIMIT $2`,
+ [WINDOW_DAYS, REQUEST_CAP])).map(row => row.request);
+ } catch (error) {
+ return checkError(NAME, 'read the recent purge tombstones', error);
+ }
+ const path = purgeMirrorPath(home);
+ if (!requests.length) return { name: NAME, status: 'ok', message: `No purge in the last ${WINDOW_DAYS} days to check against the purge mirror at ${path}.`, details: { path, recent: 0, missing: 0 } };
+ const lines = readPurgeMirror(path);
+ const mirrored = new Set((lines ?? []).filter(line => line.kind === 'intent').map(line => line.request));
+ const missing = requests.filter(request => !mirrored.has(request)).length;
+ const capped = requests.length >= REQUEST_CAP;
+ const details = { path, recent: requests.length, missing, window_days: WINDOW_DAYS, readable: lines !== null, ...(capped ? { capped: true } : {}) };
+ if (!missing) return { name: NAME, status: 'ok', message: `The purge mirror at ${path} holds all ${requests.length} purge(s) of the last ${WINDOW_DAYS} days, so a restore from an older backup replays them.`, details };
+ return {
+ name: NAME, status: 'warn', details,
+ message: `${missing} of the ${capped ? 'first ' : ''}${requests.length} purge(s) this brain recorded in the last ${WINDOW_DAYS} days have no line in the purge mirror at ${path}`
+ + `${lines === null ? ' (the mirror is missing or unreadable)' : ''}, so a restore from a backup older than them would not replay them and could bring the purged text back. `
+ + 'This happens when gbrain runs in a home directory that is removed after the run, or on another machine. Run every gbrain writer with GBRAIN_HOME set to one durable directory, so all of them append to the same mirror.',
+ fix: { argv: ['gbrain', 'doctor', '--only', NAME, '--json'], consent: [], actor: 'host_admin', requires_exclusive: false, verify: doctorVerify(NAME), docs: DOCS,
+ why: 'Read-only: shows which purge mirror this process uses and how many recent purges it lacks; the fix is setting GBRAIN_HOME on the brain host.',
+ user_message: 'Set GBRAIN_HOME to one durable directory for every gbrain process that writes to this brain, so restores can replay purges.' },
+ };
+}
+
+async function runPurgeMirror(ctx: DoctorContext): Promise<Check[]> {
+ const checks: Check[] = [];
+ checks.push(await checkPurgeMirror(connectedEngine(ctx)));
+ return checks;
+}
+
+export const purgeMirrorEntry: DoctorEntry = { name: 'purge_mirror', emits: ['purge_mirror'], run: runPurgeMirror };
diff --git a/src/commands/doctor/registry.ts b/src/commands/doctor/registry.ts
index 204f14bf1c..4cf0688f76 100644
--- a/src/commands/doctor/registry.ts
+++ b/src/commands/doctor/registry.ts
@@ -169,6 +169,7 @@ import { renameIdentityDowngradesEntry } from './checks/rename-identity-downgrad
import { unindexedFilesEntry } from './checks/unindexed-files.ts';
import { syncLandingGapsEntry } from './checks/landing-gaps.ts';
import { derivedOriginParkedEntry, purgeParkedPagesEntry } from './checks/purge-parks.ts';
+import { purgeMirrorEntry } from './checks/purge-mirror.ts';
import { derivationPurgeSweepsEntry } from './checks/derivation-purge-sweeps.ts';
import { missingRecordedFilesEntry } from './checks/missing-recorded-files.ts';
import { uncommittedSyncDeletionsEntry } from './checks/uncommitted-sync-deletions.ts';
@@ -356,6 +357,7 @@ export const DOCTOR_CHECK_REGISTRY: readonly DoctorEntry[] = [
renameIdentityDowngradesEntry,
purgeParkedPagesEntry,
derivedOriginParkedEntry,
+ purgeMirrorEntry,
derivationPurgeSweepsEntry,
missingRecordedFilesEntry,
unindexedFilesEntry,
diff --git a/src/core/belief/purge-mirror.ts b/src/core/belief/purge-mirror.ts
index 3e6dc5f030..0527080f11 100644
--- a/src/core/belief/purge-mirror.ts
+++ b/src/core/belief/purge-mirror.ts
@@ -31,12 +31,15 @@ import { COVERAGE_KEY, factKey, pageKey, slugRef } from './keys.ts';
import { appendBeliefEvents, type BeliefEventDraft } from './append.ts';
import { appendDerivedHidden, bumpPurgeEpoch, derivedBeliefTargets, pageLifetimesOf, pagePurgeClaims, redactCarryingFields, redactPurgedKeys, registerLiveCarriers } from './purge.ts';
import { derivedRowsFrom, hideDerivedRows, type DerivationRef } from '../facts/derivation-inputs.ts';
-import { writeOriginDeletedMarker } from '../persistence/sync-markers.ts';
import { outcomeMarkerSql } from '../facts/audit-sources.ts';
+import { pageChangeReasons } from '../facts/purge-reasons.ts';
+import { readFix } from '../ops/op-fix.ts';
+import { sweepRequestsForGroups, type IntentRedactionTarget } from '../persistence/intent-redaction.ts';
+import { declarePersistenceProtocol } from '../persistence/protocol.ts';
export const PURGE_MIRROR_FILE = 'purge-events.jsonl';
-export type PurgeReplayFault = 'skip_replay' | 'replay_uncommitted' | 'always_complete';
+export type PurgeReplayFault = 'skip_replay' | 'replay_uncommitted' | 'always_complete' | 'incomplete_census';
let replayFault: PurgeReplayFault | null = null;
/** Forced-probe seam (section 11): a deliberately broken replay the restore probes must catch. Null restores it. */
export function __setPurgeReplayFaultForTests(fault: PurgeReplayFault | null): void { replayFault = fault; }
@@ -105,7 +108,53 @@ export const replayState = (records: readonly ReplayRecord[]): PurgeReplayState
* Rows derived from the purged ones are hidden again and, with a ledger, get their `derived.hidden`; a walk that cannot run
* (no derivation table) or hits its bound makes the replay `partial`.
*/
-async function replayOne(tx: BrainEngine, intent: PurgeMirrorIntent, ledger: boolean): Promise<ReplayOutcome> {
+/** The restored page a page-scope intent names: by a recorded content hash or (with a ledger) a recorded lifetime, the named slug first. */
+async function replayedPage(tx: BrainEngine, intent: PurgeMirrorIntent, ledger: boolean): Promise<{ id: number; slug: string; source_path: string | null } | null> {
+ const [page] = await tx.executeRaw<{ id: number; slug: string; source_path: string | null }>(`SELECT p.id, p.slug, p.source_path FROM pages p WHERE p.source_id = $1 AND (p.content_hash = ANY($2::text[])
+ ${ledger ? 'OR p.id IN (SELECT l.page_id FROM belief_page_lifetimes l WHERE l.source_id = $1 AND l.page_lifetime = ANY($3::uuid[]))' : 'OR $3::uuid[] IS NULL'})
+ ORDER BY (p.slug = $4) DESC, p.id LIMIT 1`, [intent.source_id, intent.content_hashes ?? [], intent.lifetimes, intent.slug]);
+ return page ? { id: Number(page.id), slug: page.slug, source_path: page.source_path } : null;
+}
+
+/** What one replay run shares across its purges: the stores the restored archive has and the journal census of each page purge. */
+interface ReplayRun { present: ReadonlySet<string>; reasons: boolean; census: Map<string, { targets: IntentRedactionTarget[]; complete: boolean }> }
+
+/**
+ * #9469: one journal pass per source for the whole replay run, not one per replayed purge. Before anything is replayed,
+ * each page purge to replay is matched to its restored page and the page's claims are read; one sweep of the source's
+ * request journal then finds every request carrying any of them. Uncapped: a restore runs offline, so it has no budget.
+ */
+async function replayRun(tx: BrainEngine, intents: readonly PurgeMirrorIntent[], ledger: boolean): Promise<ReplayRun> {
+ const { PURGE_SWEEP_STORES, readPurgeClaims } = await import('../persistence/page-purge.ts');
+ const present = new Set((await tx.executeRaw<{ name: string }>('SELECT t AS name FROM unnest($1::text[]) AS t WHERE to_regclass(t) IS NOT NULL', [[...PURGE_SWEEP_STORES]])).map(r => r.name));
+ const [reasonColumn] = await tx.executeRaw<{ present: boolean }>(`SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'page_versions' AND column_name = 'change_reason') AS present`);
+ const census = new Map<string, { targets: IntentRedactionTarget[]; complete: boolean }>();
+ if (!present.has('persistence_requests')) return { present, reasons: false, census };
+ if (intents.some(intent => intent.scope === 'page') && (await tx.executeRaw<{ present: boolean }>("SELECT to_regclass('persistence_brain') IS NOT NULL AS present"))[0]?.present) await declarePersistenceProtocol(tx);
+ const groups = new Map<string, Array<{ key: string; claims: string[] }>>();
+ for (const intent of intents) {
+ if (intent.scope !== 'page' || !intent.slug) continue;
+ const page = await replayedPage(tx, intent, ledger);
+ if (!page) continue;
+ const claims = (await readPurgeClaims(tx, intent.source_id, page.slug, page.id)).needles;
+ groups.set(intent.source_id, [...groups.get(intent.source_id) ?? [], { key: intent.request, claims }]);
+ }
+ for (const [sourceId, members] of groups) {
+ for (const [key, sweep] of await sweepRequestsForGroups(tx, sourceId, members, { matchCap: Number.MAX_SAFE_INTEGER })) {
+ census.set(key, { targets: sweep.targets, complete: sweep.complete && replayFault !== 'incomplete_census' });
+ }
+ }
+ return { present, reasons: reasonColumn?.present === true, census };
+}
+
+/** A replayed page purge whose sweep could not cover every store: the restore stops and names it, never reporting a partial sweep as replayed. */
+function replayIncomplete(intent: PurgeMirrorIntent) {
+ return opError('purge_replay_incomplete', `The restore stopped: the replay of purge request ${intent.request_id} (source ${intent.source_id}) could not sweep every request that carries the purged page, so that purge was not replayed and nothing was restored.`,
+ 'A replay sweeps what the live purge swept, the request journal included; a partial sweep would leave purged text in the restored brain while reporting it replayed.',
+ { fix: readFix('Lists the restored brain\'s replay findings, read-only.', { argv: ['gbrain', 'doctor', '--json'] }) });
+}
+
+async function replayOne(tx: BrainEngine, intent: PurgeMirrorIntent, ledger: boolean, run: ReplayRun): Promise<ReplayOutcome> {
const src = intent.source_id, actor = 'restore_replay', publicationId = randomUUID();
await tx.executeRaw("SELECT set_config('gbrain.write_sources', $1, true)", [JSON.stringify([src])]);
if (ledger) await bumpPurgeEpoch(tx, src);
@@ -128,35 +177,22 @@ async function replayOne(tx: BrainEngine, intent: PurgeMirrorIntent, ledger: boo
let inputs: DerivationRef[] = [];
let removeRows = async () => {};
if (intent.scope === 'page' && intent.slug) {
- const [page] = await tx.executeRaw<{ id: number; slug: string; source_path: string | null }>(`SELECT p.id, p.slug, p.source_path FROM pages p WHERE p.source_id = $1 AND (p.content_hash = ANY($2::text[])
- ${ledger ? 'OR p.id IN (SELECT l.page_id FROM belief_page_lifetimes l WHERE l.source_id = $1 AND l.page_lifetime = ANY($3::uuid[]))' : 'OR $3::uuid[] IS NULL'})
- ORDER BY (p.slug = $4) DESC, p.id LIMIT 1`, [src, intent.content_hashes ?? [], intent.lifetimes, intent.slug]);
- if (page) {
- const restored = { id: Number(page.id), slug: page.slug, source_path: page.source_path };
+ const restored = await replayedPage(tx, intent, ledger);
+ if (restored) {
if (ledger) lifetimes = [...new Set([...lifetimes, ...await pageLifetimesOf(tx, src, restored.slug, restored.id)])];
claims = (await pagePurgeClaims(tx, { sourceId: src, slug: restored.slug, pageId: restored.id, lifetimes: ledger ? lifetimes : [] })).claims;
- const owned = await tx.executeRaw<{ t: string; id: number }>(`SELECT 'facts' AS t, id FROM facts WHERE source_id = $1 AND source_markdown_slug = $2
- UNION ALL SELECT 'takes', id FROM takes WHERE page_id = $3`, [src, restored.slug, restored.id]);
- inputs = [{ table: 'pages', id: restored.id }, ...owned.map(r => ({ table: r.t, id: Number(r.id) }))];
+ const { deletePurgedPage, readPurgeClaims, readSegmentClaims, sweepPurgedPage } = await import('../persistence/page-purge.ts');
+ const { factRows, takeRows, needles } = await readPurgeClaims(tx, src, restored.slug, restored.id);
+ const segmentClaims = await readSegmentClaims(tx, src, restored.slug, restored.id);
+ const reasons = run.reasons && run.present.has('persistence_requests') ? await pageChangeReasons(tx, src, restored.slug, restored.id) : [];
+ const factIds = factRows.map(f => Number(f.id)), takeIds = takeRows.map(t => Number(t.id));
+ inputs = [{ table: 'pages', id: restored.id }, ...factIds.map(id => ({ table: 'facts', id })), ...takeIds.map(id => ({ table: 'takes', id }))];
+ // #9469: the live purge's sweep and row removal, shared, so the replay leaves no more of the page than the purge did.
removeRows = async () => {
- await tx.executeRaw('DELETE FROM facts WHERE source_id = $1 AND source_markdown_slug = $2', [src, restored.slug]);
- await writeOriginDeletedMarker(tx, { sourceId: src, page: restored });
- const { deletePageWithSegments } = await import('../persistence/page-purge.ts');
- await deletePageWithSegments(tx, src, restored.slug, restored.id);
- const [feedback] = await tx.executeRaw<{ present: boolean }>("SELECT to_regclass('retrieval_event_pages') IS NOT NULL AS present");
- if (feedback?.present) {
- const { scrubPurgedSlugs } = await import('../feedback/store.ts');
- await scrubPurgedSlugs(tx, src, [restored.slug]);
- }
- const [names] = await tx.executeRaw<{ present: boolean }>("SELECT to_regclass('mention_gazetteer_entries') IS NOT NULL AS present");
- if (names?.present) {
- const { scrubPurgedNameStores } = await import('../persistence/purge-name-stores.ts');
- await scrubPurgedNameStores(tx, src, [restored.slug]);
- }
- const { scrubPurgedSlugHolds, scrubPurgedSlugStores } = await import('../persistence/purge-slug-stores.ts');
- await scrubPurgedSlugStores(tx, src, [restored.slug], { ifPresent: true });
- const [holds] = await tx.executeRaw<{ present: boolean }>("SELECT to_regclass('write_gate_holds') IS NOT NULL AND to_regclass('write_gate_receipts') IS NOT NULL AS present");
- if (holds?.present) await scrubPurgedSlugHolds(tx, src, [{ slug: restored.slug }]);
+ const swept = await sweepPurgedPage(tx, { sourceId: src, slug: restored.slug, pageId: restored.id, requestId: intent.request, factIds, takeIds, needles, reasons,
+ census: async () => run.census.get(intent.request) ?? { targets: [], complete: !needles.length }, timelineCap: Number.MAX_SAFE_INTEGER, present: run.present });
+ if (!swept.complete) throw replayIncomplete(intent);
+ await deletePurgedPage(tx, { sourceId: src, slug: restored.slug, pageId: restored.id, claims: segmentClaims, origin: restored, present: run.present });
};
}
} else {
@@ -243,6 +279,7 @@ export async function replayPurgesInRestore(tx: BrainEngine, input: { lines: Pur
const seen = new Set<string>();
const newer = input.lines.filter((l): l is PurgeMirrorIntent => l.kind === 'intent' && (input.allTimes || l.recorded_at > input.archivedAt)
&& sources.some(s => s.id === l.source_id && s.incarnation === l.source_incarnation));
+ const pending: Array<{ intent: PurgeMirrorIntent; replay: boolean }> = [];
for (const intent of newer) {
if (seen.has(intent.request)) continue;
seen.add(intent.request);
@@ -250,7 +287,11 @@ export async function replayPurgesInRestore(tx: BrainEngine, input: { lines: Pur
OR EXISTS (SELECT 1 FROM take_purges WHERE request_id = $1::uuid) OR EXISTS (SELECT 1 FROM page_purges WHERE request_id = $1::uuid)) AS present`, [intent.request]);
if (archived?.present === true) continue;
if (replayFault === 'skip_replay') continue;
- const outcome = committed.has(intent.request) || replayFault === 'replay_uncommitted' ? await replayOne(tx, intent, ledger) : 'unverified';
+ pending.push({ intent, replay: committed.has(intent.request) || replayFault === 'replay_uncommitted' });
+ }
+ const run = await replayRun(tx, pending.filter(p => p.replay).map(p => p.intent), ledger);
+ for (const { intent, replay } of pending) {
+ const outcome = replay ? await replayOne(tx, intent, ledger, run) : 'unverified';
replays.push({ request: intent.request, request_id: intent.request_id, scope: intent.scope, outcome });
}
}
diff --git a/src/core/doctor-categories.ts b/src/core/doctor-categories.ts
index c407804e3f..fea5410b10 100644
--- a/src/core/doctor-categories.ts
+++ b/src/core/doctor-categories.ts
@@ -224,6 +224,8 @@ export const BRAIN_CHECK_NAMES: ReadonlySet<string> = new Set([
'purge_parked_pages',
// GST-2689: origins kept from the purge because a page derived from them could not be labelled private (doctor/checks/purge-parks.ts).
'derived_origin_parked',
+ // #9469: recent purges this process's purge mirror lacks, so a restore could not replay them (doctor/checks/purge-mirror.ts).
+ 'purge_mirror',
// Wave B PR 4: fact purge sweeps of stored derivation results still pending or stalled (doctor/checks/derivation-purge-sweeps.ts).
'derivation_purge_sweeps',
'partial_embedding',
diff --git a/src/core/engine-sql/pages.ts b/src/core/engine-sql/pages.ts
index b40eb06521..aaed3c31a9 100644
--- a/src/core/engine-sql/pages.ts
+++ b/src/core/engine-sql/pages.ts
@@ -28,7 +28,7 @@ import { quarantineFilterFragment } from '../quarantine.ts';
import { rederiveHiddenSql } from '../eligibility/sql.ts';
import { validateSlug, contentHash, isBlankBody, rowToPage, rowToStalePage, isMissingSchemaObject, warnOncePerProcess } from '../utils.ts';
import { DELETE_BATCH_SIZE } from '../engine-constants.ts';
-import { derivedOriginPurgeHold } from '../persistence/derived-origin-hold.ts';
+import { derivedOriginPurgeHold, OWED_PURGE_OP } from '../persistence/derived-origin-hold.ts';
import { jsonbParam, type SqlExecutor } from './executor.ts';
import type { LegacyUnscopedRead, ScopedRead } from './brands.ts';
import type { ScopedReadRunner } from './cjk-search.ts';
@@ -312,10 +312,16 @@ export async function softDeletePages(exec: SqlExecutor, slugs: string[], opts:
export async function restorePage(exec: SqlExecutor, slug: string, opts?: { sourceId?: string }): Promise<boolean> {
const sourceId = opts?.sourceId;
const sourceCondition = sourceId ? sqlFragment`AND source_id = ${sourceId}` : sqlFragment``;
+ // GST-2766: a restored page owes no purge; its owed-purge record goes in the same statement.
const { rows } = await exec.run(sqlFragment`
- UPDATE pages SET deleted_at = NULL, updated_at = now()
- WHERE slug = ${slug} AND deleted_at IS NOT NULL ${sourceCondition}
- RETURNING slug
+ WITH restored AS (
+ UPDATE pages SET deleted_at = NULL, updated_at = now()
+ WHERE slug = ${slug} AND deleted_at IS NOT NULL ${sourceCondition}
+ RETURNING id, slug
+ ), owed AS (
+ DELETE FROM op_checkpoints WHERE op = ${OWED_PURGE_OP} AND fingerprint IN (SELECT id::text FROM restored)
+ )
+ SELECT slug FROM restored
`);
return rows.length > 0;
}
diff --git a/src/core/error-registry.ts b/src/core/error-registry.ts
index 01c2715fb0..c2117cf548 100644
--- a/src/core/error-registry.ts
+++ b/src/core/error-registry.ts
@@ -669,6 +669,11 @@ export const CODES = {
why: 'Restore replays the purges recorded in that text-free mirror; a purge that committed without its line would come back silently from an older backup.',
docs: 'docs/guides/write-refusals.md#purge_mirror_unwritable',
suggestion: 'Do not assume the requested content was erased. Make the mirror a writable regular file owned by the brain\'s user (mode 0600), then run the purge again with a new request_id.' },
+ purge_replay_incomplete: { class: 'caller', retryable: false, actor: 'host_admin',
+ summary: 'A restore stopped because the replay of a purge could not sweep every stored request that carries the purged page; that purge was not replayed and nothing was restored.',
+ why: 'A replayed purge removes what the live purge removed, the request journal included; reporting a partial sweep as replayed would leave purged text in the restored brain.',
+ docs: 'docs/guides/write-refusals.md#purge_replay_incomplete',
+ suggestion: 'Do not use the restored copy. Report the purge request named in the message with the output of gbrain doctor --json; restore again once it is fixed.' },
purged_content: { class: 'caller', summary: "The content matches a purged fact, take or page in this source, so it was not saved.", why: "A purge leaves a text-free tombstone (the claim fingerprint or the page content hash) so stale files, re-syncs, reverts and re-extraction cannot bring purged content back.", suggestion: 'Do not retry the same content. Write it in new words if it is still true, or ask the user to clear the tombstone on the brain host (gbrain pages purges list, then gbrain pages unpurge <slug> for a page).', docs: 'docs/guides/memory-boundaries.md#purge' },
question_not_found: { class: 'caller', summary: "No pinned question has this id in the caller's scope; ids are source-qualified (default:questions/<slug>).", docs: 'docs/guides/pinned-questions.md#ids-and-scope' },
question_owner_only: { class: 'host_only', summary: "Pinned questions are owner-private: this connection cannot read private pages (or is slug-fenced or delegated), so the owner runs the command on the brain host.", docs: 'docs/guides/pinned-questions.md#who-can-see-pinned-questions' },
diff --git a/src/core/op-checkpoint.ts b/src/core/op-checkpoint.ts
index e51aa55bf3..6fd9ac38c2 100644
--- a/src/core/op-checkpoint.ts
+++ b/src/core/op-checkpoint.ts
@@ -524,6 +524,7 @@ export async function purgeStaleCheckpoints(
SELECT 1 FROM pages p WHERE p.id::text=op_checkpoints.fingerprint AND p.deleted_at=(op_checkpoints.completed_keys->0->>'deleted_at')::timestamptz))
AND op<>'purge-deleted-park'
AND op<>'derived-origin-park'
+ AND op<>'owed-page-purge'
AND NOT (op='sync-identity-downgrade' AND completed_keys->0->>'disposition' IS NULL AND EXISTS (
SELECT 1 FROM pages p WHERE p.id::text=op_checkpoints.fingerprint AND p.deleted_at=(op_checkpoints.completed_keys->0->>'old_deleted_at')::timestamptz))
AND NOT (op IN ('sync-hold','sync-hold-retry','sync-import-provenance','sync-hold-summary','sync-conversions','${FENCE_CANDIDATE_OP}','${FENCE_SCAN_OP}','${FENCE_REPAIR_ATTEMPT_OP}','${FENCE_UNCOMMITTED_OP}',${SYNC_PASS_ROW_OPS.map(op => `'${op}'`).join(',')})
diff --git a/src/core/persistence/derived-origin-hold.ts b/src/core/persistence/derived-origin-hold.ts
index 0d84be4683..3e8f88c959 100644
--- a/src/core/persistence/derived-origin-hold.ts
+++ b/src/core/persistence/derived-origin-hold.ts
@@ -17,7 +17,7 @@ export const DERIVED_ORIGIN_PARKED_CODE = 'derived_origin_parked';
* `blocking_slug`: the derived page whose label write failed; null when its dependents could not be read. `held_request_id`: the owner's
* explicit purge this park held (a retention purge leaves it unset); a later park of the same page keeps it.
*/
-export interface DerivedOriginPark { source_id: string; slug: string; page_id: number; blocking_slug: string | null; code: string | null; reason: string; parked_at: string; held_request_id?: string }
+export interface DerivedOriginPark { source_id: string; slug: string; page_id: number; blocking_slug: string | null; code: string | null; reason: string; parked_at: string; held_request_id?: string; held_deleted_us?: string }
/** The command that finishes an owner purge a park held: `gbrain delete` makes a new request_id, and an explicit purge writes the purge tombstones. */
export const finishPurgeArgv = (sourceId: string, slug: string): string[] => ['gbrain', 'delete', slug, '--purge', '--source-id', sourceId];
@@ -36,7 +36,62 @@ export async function parkDerivedOrigin(engine: SqlEngine, park: Omit<DerivedOri
const row: DerivedOriginPark = { ...park, parked_at: new Date().toISOString() };
await engine.executeRaw(`INSERT INTO op_checkpoints(op,fingerprint,completed_keys) VALUES($1,$2,$3::text::jsonb)
ON CONFLICT (op,fingerprint) DO UPDATE SET updated_at=now(), completed_keys=CASE WHEN (op_checkpoints.completed_keys->0) ? 'held_request_id' AND NOT (EXCLUDED.completed_keys->0) ? 'held_request_id'
- THEN jsonb_set(EXCLUDED.completed_keys, '{0,held_request_id}', op_checkpoints.completed_keys->0->'held_request_id') ELSE EXCLUDED.completed_keys END`, [DERIVED_ORIGIN_PARK_OP, String(park.page_id), JSON.stringify([row])]);
+ THEN jsonb_build_array((EXCLUDED.completed_keys->0) || jsonb_strip_nulls(jsonb_build_object('held_request_id', op_checkpoints.completed_keys->0->'held_request_id',
+ 'held_deleted_us', op_checkpoints.completed_keys->0->'held_deleted_us'))) ELSE EXCLUDED.completed_keys END`, [DERIVED_ORIGIN_PARK_OP, String(park.page_id), JSON.stringify([row])]);
+}
+
+/**
+ * GST-2766: the record that an owner purge is owed (op `owed-page-purge`, fingerprint the page id), kept apart from the
+ * park. A park is cleared once its carry lands, often in a run that does not remove the page; this record stays until
+ * the page is removed or restored, so whichever purge removes the page writes the purge tombstones. It names no slug or
+ * text: the source, the page id, the tombstone it was owed for (`deleted_us`, as purge-parks.ts keys a tombstone) and
+ * the owner's request id. A record whose tombstone changed (restored, deleted again) applies to nothing and is pruned.
+ */
+export const OWED_PURGE_OP = 'owed-page-purge';
+export interface OwedPurge { source_id: string; page_id: number; deleted_us: string; request_id: string }
+
+const DELETED_US = (alias: string) => `floor(extract(epoch FROM ${alias}.deleted_at)*1000000)::bigint::text`;
+const OWED_TOMBSTONE = `EXISTS (SELECT 1 FROM pages p WHERE p.id::text = o.fingerprint AND p.deleted_at IS NOT NULL AND ${DELETED_US('p')} = o.completed_keys->0->>'deleted_us')`;
+
+/**
+ * Records that the owner's purge `requestId` is owed for page `pageId`'s current tombstone; a page that is not a tombstone
+ * records nothing. The page's park, when it holds that purge, is bound to the same tombstone (`held_deleted_us`), so a page
+ * restored and deleted again never inherits the old hold (releaseStaleHolds).
+ */
+export async function recordOwedPurge(engine: SqlEngine, pageId: number, requestId: string): Promise<void> {
+ await engine.executeRaw(`INSERT INTO op_checkpoints(op,fingerprint,completed_keys)
+ SELECT $1, p.id::text, jsonb_build_array(jsonb_build_object('source_id', p.source_id, 'page_id', p.id, 'deleted_us', ${DELETED_US('p')}, 'request_id', $3::text))
+ FROM pages p WHERE p.id = $2 AND p.deleted_at IS NOT NULL
+ ON CONFLICT (op,fingerprint) DO UPDATE SET updated_at=now(), completed_keys=EXCLUDED.completed_keys
+ WHERE op_checkpoints.completed_keys->0->>'deleted_us' IS DISTINCT FROM EXCLUDED.completed_keys->0->>'deleted_us'`, [OWED_PURGE_OP, pageId, requestId]);
+ await engine.executeRaw(`UPDATE op_checkpoints o SET updated_at=now(), completed_keys=jsonb_set(o.completed_keys, '{0,held_deleted_us}', to_jsonb(${DELETED_US('p')}))
+ FROM pages p WHERE o.op=$1 AND o.fingerprint=p.id::text AND p.id=$2 AND p.deleted_at IS NOT NULL AND o.completed_keys->0->>'held_request_id' = $3::text`,
+ [DERIVED_ORIGIN_PARK_OP, pageId, requestId]);
+}
+
+/** Drops the owner purge a park holds once its page's tombstone is not the one the hold was recorded for (restored, deleted again). */
+export async function releaseStaleHolds(engine: SqlEngine): Promise<void> {
+ await engine.executeRaw(`UPDATE op_checkpoints o SET updated_at=now(), completed_keys=jsonb_build_array((o.completed_keys->0) - 'held_request_id' - 'held_deleted_us')
+ FROM pages p WHERE o.op=$1 AND o.fingerprint=p.id::text AND (o.completed_keys->0) ? 'held_deleted_us'
+ AND (p.deleted_at IS NULL OR ${DELETED_US('p')} <> o.completed_keys->0->>'held_deleted_us')`, [DERIVED_ORIGIN_PARK_OP]);
+}
+
+/** Read-only: every owed purge whose tombstone still exists as recorded. */
+export async function readOwedPurges(engine: SqlEngine): Promise<OwedPurge[]> {
+ const rows = await engine.executeRaw<{ owed: OwedPurge | string }>(`SELECT o.completed_keys->0 AS owed FROM op_checkpoints o WHERE o.op=$1 AND ${OWED_TOMBSTONE}
+ ORDER BY o.fingerprint`, [OWED_PURGE_OP]);
+ return rows.map(row => typeof row.owed === 'string' ? JSON.parse(row.owed) as OwedPurge : row.owed).map(owed => ({ ...owed, page_id: Number(owed.page_id) }));
+}
+
+/** Drops the records whose tombstone was removed, restored or deleted again. */
+export async function pruneOwedPurges(engine: SqlEngine): Promise<void> {
+ await engine.executeRaw(`DELETE FROM op_checkpoints o WHERE o.op=$1 AND NOT ${OWED_TOMBSTONE}`, [OWED_PURGE_OP]);
+}
+
+/** Removes the records of these pages; the purge that removes a page calls it in its transaction. */
+export async function clearOwedPurges(engine: SqlEngine, pageIds: readonly number[]): Promise<void> {
+ if (!pageIds.length) return;
+ await engine.executeRaw('DELETE FROM op_checkpoints WHERE op=$1 AND fingerprint = ANY($2::text[])', [OWED_PURGE_OP, pageIds.map(String)]);
}
export async function clearDerivedOriginParks(engine: SqlEngine, pageIds: readonly number[]): Promise<void> {
diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.ts
index ca5c85b1ea..b2f8810e96 100644
--- a/src/core/persistence/page-prepare.ts
+++ b/src/core/persistence/page-prepare.ts
@@ -110,6 +110,7 @@ import { bindOrigin } from './origin-guard.ts';
import { assertGuestWriterAtPublication, insertGuestMark, refuseRemoteGuestDelete } from '../guests/admission.ts';
import { skipsGuestNote } from '../guests/derived.ts';
import { NOT_A_GUEST_WRITE, prepareGuestWrite, type GuestWrite } from '../guests/publication.ts';
+import { clearOwedPurges } from './derived-origin-hold.ts';
const ownerStatusFix = (sourceId: string): Action => readFix(`Shows source ${sourceId}'s canonical owner with its pending, failed and recovering requests, read-only.`,
@@ -1279,7 +1280,7 @@ async function prepareInlineOrSegmentedMutation(unbounded: BrainEngine, row: Wri
]) as [unknown, { mode: string | null } | undefined, unknown, unknown];
if (pinMode && pinned?.mode !== pinMode) throw pageRefusal('revision_conflict', 'The source slug-root mode changed during preparation.', row,
`Another write pinned source ${row.source_id}'s slug-root mode while ${row.slug} was being prepared, so this publication rolled back. Once the request is final, submit the write again; it is prepared under the pinned mode.`);
- if (row.operation === 'restore_page') await tx.restorePage(row.slug, source);
+ if (row.operation === 'restore_page') { await tx.restorePage(row.slug, source); if (snapshot) await clearOwedPurges(tx, [Number(snapshot.page.id)]); }
if (versionTags) {
for (const tag of snapshot!.tags) if (!versionTags.includes(tag)) await tx.removeTag(row.slug, tag, source);
for (const tag of versionTags) await tx.addTag(row.slug, tag, source);
diff --git a/src/core/persistence/page-purge.ts b/src/core/persistence/page-purge.ts
index 4cf2e7d0fd..db25f2c8ae 100644
--- a/src/core/persistence/page-purge.ts
+++ b/src/core/persistence/page-purge.ts
@@ -47,7 +47,7 @@ import { writeOriginDeletedMarker } from './sync-markers.ts';
import { withTrustKeep } from './context.ts';
import { clearRebuildableCache } from './rebuildable-cache.ts';
import { carryingNeedles, findRequestsForSlugs, JOURNAL_SWEEP_MATCH_CAP, recheckRequestsCarrying, redactRequestIntents, sweepRequestsCarrying, sweepRequestsForGroups,
- type IntentRedactionTarget, type JournalSweep } from './intent-redaction.ts';
+ type IntentRedactionTarget, type JournalRecheck, type JournalSweep } from './intent-redaction.ts';
import { dropPurgedFenceRows } from '../facts/purge-overlay.ts';
import { isOutcomeMarker, outcomeMarkerSql } from '../facts/audit-sources.ts';
import { derivedRowsFrom, hideDerivedRows } from '../facts/derivation-inputs.ts';
@@ -60,6 +60,7 @@ import { CLAIM_FORMS_BATCH, claimTextForms } from '../fence-shared.ts';
import type { PagePurgeContext } from '../facts/purge-verify.ts';
import { pageChangeReasons, requestsRecordingReasons, stripPageGitNotes } from '../facts/purge-reasons.ts';
import { assertHeldPageKeys } from '../page-state/guards.ts';
+import { clearOwedPurges, recordOwedPurge } from './derived-origin-hold.ts';
import { noteBelief } from '../belief/ledger.ts';
import { appendPurgeIntent } from '../belief/purge-mirror.ts';
import { scrubPurgedSlugs } from '../feedback/store.ts';
@@ -91,7 +92,7 @@ const purgeReceipts = new Map<string, { receipt: Record<string, unknown>; purge:
const PAGE_FACTS = `(source_markdown_slug=$2 OR ${extractedFromPageSql('facts', '$2')})`;
/** The page's claims (facts filed on it, its takes) that the journal sweep searches for, bounded, trimmed, deduplicated. */
-async function readPurgeClaims(engine: Pick<BrainEngine, 'executeRaw'>, sourceId: string, slug: string, pageId: number) {
+export async function readPurgeClaims(engine: Pick<BrainEngine, 'executeRaw'>, sourceId: string, slug: string, pageId: number) {
const factRows = await engine.executeRaw<{ id: number; fact: string; source: string | null }>(`SELECT id,fact,source FROM facts WHERE source_id=$1 AND ${PAGE_FACTS} ORDER BY id LIMIT 1000`, [sourceId, slug]);
const takeRows = await engine.executeRaw<{ id: number; claim: string }>('SELECT id,claim FROM takes WHERE page_id=$1 ORDER BY id LIMIT 1000', [pageId]);
const needles = [...new Set([...factRows.filter(f => !isOutcomeMarker(f)).map(f => f.fact), ...takeRows.map(t => t.claim)].map(t => t.trim()).filter(t => t.length >= 8))].slice(0, 32);
@@ -99,7 +100,7 @@ async function readPurgeClaims(engine: Pick<BrainEngine, 'executeRaw'>, sourceId
}
/** Every claim of the page (its facts, withdrawn ones included, and its takes), trimmed and deduplicated, unbounded: the segment-text sweep's needles. */
-async function readSegmentClaims(engine: Pick<BrainEngine, 'executeRaw'>, sourceId: string, slug: string, pageId: number): Promise<string[]> {
+export async function readSegmentClaims(engine: Pick<BrainEngine, 'executeRaw'>, sourceId: string, slug: string, pageId: number): Promise<string[]> {
const rows = await engine.executeRaw<{ c: string }>(`SELECT fact AS c FROM facts WHERE source_id=$1 AND ${PAGE_FACTS} AND NOT ${outcomeMarkerSql('facts')} UNION SELECT claim FROM takes WHERE page_id=$3`,
[sourceId, slug, pageId]);
return [...new Set(rows.map(r => r.c.trim()).filter(t => t.length >= 8))];
@@ -282,6 +283,114 @@ async function tombstonePage(tx: BrainEngine, sourceId: string, slug: string, pa
/** The page's add_timeline_entry receipts one purge redacts; a page with more leaves `journal_sweep.complete` false. */
const TIMELINE_RECEIPT_CAP = 10_000;
+/** The journal census a page purge redacts: the requests carrying its claims, and whether the search covered every one. */
+export interface PurgeCensus { targets: IntentRedactionTarget[]; complete: boolean }
+
+/** What the shared page sweep needs: the page, its rows and claims, and how to take the journal census. */
+export interface PurgedPageSweep {
+ sourceId: string; slug: string; pageId: number;
+ /** The purge's request row id: excluded from redaction and named as the redacting request. */
+ requestId: string;
+ factIds: number[]; takeIds: number[];
+ /** The page's claims (readPurgeClaims) and change reasons (pageChangeReasons). */
+ needles: string[]; reasons: string[];
+ /** Called once, where the census belongs in the sweep's order. */
+ census: () => Promise<PurgeCensus>;
+ /** The live purge's typed timeout wrapper around journal lookups; the restore replay passes none. */
+ lookup?: <T>(point: PurgeLookupPoint, fn: () => Promise<T>) => Promise<T>;
+ /** The most add_timeline_entry receipts redacted; more leaves the sweep incomplete. */
+ timelineCap?: number;
+ /** A restored archive older than some stores: the stores it has. Absent means every store exists (the live database). */
+ present?: ReadonlySet<string>;
+}
+
+/** The stores the shared sweep touches that an older restored archive may not have yet. */
+export const PURGE_SWEEP_STORES = ['decide_review_queue', 'decide_review_proposals', 'decide_proposals', 'trust_proposals', 'write_gate_holds', 'write_gate_receipts',
+ 'persistence_requests', 'persistence_effects', 'files', 'derived_generations', 'derived_generation_items', 'retrieval_event_pages', 'mention_gazetteer_entries', 'op_checkpoints'] as const;
+type SweptStore = typeof PURGE_SWEEP_STORES[number];
+
+/**
+ * #9469: the live-store sweep of one purged page, before its row is deleted, shared by the live page purge and the
+ * restore-time purge replay, so a replayed purge leaves no more of the page than the live one: proposals, write-gate
+ * holds and receipts naming it or its rows, the query cache, the facts filed on or extracted from it, the slug stores,
+ * the journal (the census of requests carrying its claims, its own requests, those recording its change reasons and its
+ * add_timeline_entry receipts), its Git effects' commit notes, its file records and the generation ledger. Returns the
+ * rows removed per store, the file blobs to delete after commit and whether the journal sweep covered every request.
+ */
+export async function sweepPurgedPage(tx: BrainEngine, input: PurgedPageSweep): Promise<{ removed: Record<string, number>; blobs: string[]; missingCaches: string[]; complete: boolean }> {
+ const { sourceId, slug, pageId, requestId, factIds, takeIds, needles, reasons } = input;
+ const lookup = input.lookup ?? (<T>(_point: PurgeLookupPoint, fn: () => Promise<T>) => fn());
+ const has = (store: SweptStore) => !input.present || input.present.has(store);
+ const timelineCap = input.timelineCap ?? TIMELINE_RECEIPT_CAP;
+ const count = async (sql: string, params: unknown[]) => (await tx.executeRaw(sql, params)).length;
+ const refs = factIds.map(String);
+ const removed: Record<string, number> = {};
+ if (has('decide_review_queue')) removed.decide_review_queue = await count('DELETE FROM decide_review_queue WHERE source_id=$1 AND (a_ref=ANY($2::text[]) OR b_ref=ANY($2::text[])) RETURNING 1', [sourceId, refs]);
+ if (has('decide_review_proposals')) removed.decide_review_proposals = await count('DELETE FROM decide_review_proposals WHERE source_id=$1 AND (a_ref=ANY($2::text[]) OR b_ref=ANY($2::text[])) RETURNING 1', [sourceId, refs]);
+ if (has('decide_proposals')) removed.decide_proposals = await count('DELETE FROM decide_proposals WHERE source_id=$1 AND (new_fact_id=ANY($2::bigint[]) OR old_fact_id=ANY($2::bigint[])) RETURNING 1', [sourceId, factIds]);
+ if (has('trust_proposals')) {
+ removed.trust_proposals = await count(`DELETE FROM trust_proposals WHERE source_id=$1 AND ((target_table='pages' AND target_id=$2) OR (related_table='pages' AND related_id=$2)
+ OR (target_table='facts' AND target_id=ANY($3::bigint[])) OR (related_table='facts' AND related_id=ANY($3::bigint[]))
+ OR (target_table='takes' AND target_id=ANY($4::bigint[])) OR (related_table='takes' AND related_id=ANY($4::bigint[]))) RETURNING 1`, [sourceId, pageId, factIds, takeIds]);
+ }
+ const holds = has('write_gate_holds') ? (await tx.executeRaw<{ id: string }>('DELETE FROM write_gate_holds WHERE source_id=$1 AND slug=$2 RETURNING id::text AS id', [sourceId, slug])).map(h => h.id) : [];
+ if (has('write_gate_holds')) removed.write_gate_holds = holds.length;
+ if (has('write_gate_receipts')) {
+ removed.write_gate_receipts = await count(`DELETE FROM write_gate_receipts WHERE (target_table='pages' AND target_id=$1) OR (target_table='facts' AND target_id=ANY($2::text[]))
+ OR (target_table='takes' AND target_id=ANY($3::text[])) OR (target_table='write_gate_holds' AND target_id=ANY($4::text[])) RETURNING 1`,
+ [String(pageId), refs, takeIds.map(String), holds]);
+ }
+ const missingCaches: string[] = [];
+ const cleared = await clearRebuildableCache(tx, 'query_cache');
+ if (cleared === null) missingCaches.push('query_cache');
+ removed.query_cache = cleared ?? 0;
+ removed.facts = await count(`DELETE FROM facts WHERE source_id=$1 AND ${PAGE_FACTS} RETURNING 1`, [sourceId, slug]);
+ Object.assign(removed, await scrubPurgedSlugStores(tx, sourceId, [slug], { ifPresent: !!input.present }));
+ let complete = true;
+ if (has('persistence_requests')) {
+ const census = await input.census();
+ // The page's add_timeline_entry receipts keep the entry text in their outcome (`entry` summary and source, `bullet`), compacted
+ // ones included. That text is matched whole in the page's own requests (a set lookup, linear in their strings). A page past
+ // the cap leaves the journal sweep incomplete instead of claiming every receipt.
+ const timelineRows = await lookup('slug_lookup', () => tx.executeRaw<IntentRedactionTarget & { summary: string | null; source: string | null; bullet: string | null }>(`SELECT id::text AS id,
+ principal_kind, principal_id, outcome->'entry'->>'summary' AS summary, outcome->'entry'->>'source' AS source, outcome->>'bullet' AS bullet FROM persistence_requests
+ WHERE source_id=$1 AND slug=$2 AND operation='add_timeline_entry' AND outcome ? 'entry' ORDER BY sequence LIMIT $3`, [sourceId, slug, timelineCap + 1]));
+ const timeline = timelineRows.slice(0, timelineCap);
+ const timelineText = new Set(timeline.flatMap(t => [t.summary, t.source, t.bullet]).map(text => text?.trim() ?? '').filter(Boolean));
+ const forSlug = await lookup('slug_lookup', async () => [...await findRequestsForSlugs(tx, sourceId, [slug]), ...reasons.length ? await requestsRecordingReasons(tx, sourceId, slug) : [], ...timeline]);
+ const own = new Set(forSlug.map(r => r.id));
+ const requests = [...new Map([...forSlug, ...census.targets].map(r => [r.id, r])).values()];
+ // GBRA-217: the page's reasons are needles in its own requests only, so a short reason never redacts another page's receipt.
+ removed.persistence_requests = await lookup('redaction_read', async () => await redactRequestIntents(tx, requests.filter(r => own.has(r.id)), requestId,
+ { skipPending: true, needles: [...needles, ...reasons], exact: timelineText }) + await redactRequestIntents(tx, requests.filter(r => !own.has(r.id)), requestId, { skipPending: true, needles }));
+ complete = census.complete && timelineRows.length <= timelineCap;
+ if (has('persistence_effects')) removed.persistence_effects = await stripPageGitNotes(tx, sourceId, slug);
+ }
+ const files = has('files') ? await tx.executeRaw<{ storage_path: string }>(`DELETE FROM files WHERE (page_id=$1 OR (page_id IS NULL AND page_slug=$2 AND (source_id=$3 OR source_id IS NULL)))
+ RETURNING storage_path`, [pageId, slug, sourceId]) : [];
+ if (has('files')) removed.files = files.length;
+ if (has('derived_generations') && has('derived_generation_items')) Object.assign(removed, await (await import('../generations/ledger-scrub.ts')).scrubGenerationLedger(tx, sourceId, { slug }));
+ return { removed, blobs: files.map(f => f.storage_path).filter(Boolean), missingCaches, complete };
+}
+
+/**
+ * The end of a page purge, shared by the live purge and the restore replay, after the sweep and the ledger redaction:
+ * the page row with its segment storage (deletePageWithSegments), the retrieval feedback and mention-gazetteer names
+ * for the slug, and its owed-purge record (GST-2766), in the purge's transaction, right after the origin's deletion marker (none
+ * when `origin` is null: a purge that keeps its file).
+ */
+export async function deletePurgedPage(tx: BrainEngine, input: { sourceId: string; slug: string; pageId: number; claims: readonly string[]; origin: { source_path?: string | null } | null; present?: ReadonlySet<string> }):
+ Promise<{ shas: string[]; removed: Record<string, number> }> {
+ const { sourceId, slug, pageId } = input;
+ const has = (store: SweptStore) => !input.present || input.present.has(store);
+ if (input.origin) await writeOriginDeletedMarker(tx, { sourceId, page: { id: pageId, slug, source_path: input.origin.source_path } });
+ const { shas, ...removed } = await deletePageWithSegments(tx, sourceId, slug, pageId, input.claims);
+ if (has('retrieval_event_pages')) Object.assign(removed, await scrubPurgedSlugs(tx, sourceId, [slug]));
+ if (has('mention_gazetteer_entries')) Object.assign(removed, await scrubPurgedNameStores(tx, sourceId, [slug]));
+ if (has('op_checkpoints')) await clearOwedPurges(tx, [pageId]);
+ return { shas, removed };
+}
+
export async function purgePageInTransaction(tx: BrainEngine, row: WriteRequest, snapshot: PageSnapshot, prepared: PreparedPagePurge): Promise<Record<string, unknown>> {
const page = snapshot.page, sourceId = row.source_id, slug = page.slug;
// GST-2689: a page derived from this one written after the purge's carry keeps it: soft-deleted (a live page) and parked.
@@ -289,6 +398,7 @@ export async function purgePageInTransaction(tx: BrainEngine, row: WriteRequest,
const owner = prepared.retention ? undefined : { sourceId, slug, requestId: row.request_id };
if ((await parkUncarriedInPurge(tx, [Number(page.id)], { recheckParked: true, ...(owner ? { heldRequestId: owner.requestId } : {}) })).length) {
if (!page.deleted_at) { await tx.createVersion(slug, { sourceId }); await withTrustKeep(tx, ['pages'], () => tx.softDeletePage(slug, { sourceId })); }
+ if (owner) await recordOwedPurge(tx, Number(page.id), owner.requestId);
return { status: 'soft_deleted', slug, source_id: sourceId, noop: !!page.deleted_at, ...(prepared.retention ? { retention: true } : {}), purge_held: midPurgeHold(owner) };
}
const actor = `${row.principal_kind}:${row.principal_id}`;
@@ -317,51 +427,16 @@ export async function purgePageInTransaction(tx: BrainEngine, row: WriteRequest,
const walk = derivedFrom.truncated || heldTargets.length < targets.length ? 'truncated' as const : 'complete' as const;
if (!retention) noteBelief({ kind: 'purged', table: 'pages', ids: [page.id], request: row.id, ...(walk === 'truncated' ? { derivedWalk: walk } : {}) });
const derived = await hideDerivedRows(tx, derivedRows, { sourceId, requestId: row.id, reason: 'page purge' });
- const count = async (sql: string, params: unknown[]) => (await tx.executeRaw(sql, params)).length;
- const refs = factIds.map(String);
- const removed: Record<string, number> = {
- decide_review_queue: await count('DELETE FROM decide_review_queue WHERE source_id=$1 AND (a_ref=ANY($2::text[]) OR b_ref=ANY($2::text[])) RETURNING 1', [sourceId, refs]),
- decide_review_proposals: await count('DELETE FROM decide_review_proposals WHERE source_id=$1 AND (a_ref=ANY($2::text[]) OR b_ref=ANY($2::text[])) RETURNING 1', [sourceId, refs]),
- decide_proposals: await count('DELETE FROM decide_proposals WHERE source_id=$1 AND (new_fact_id=ANY($2::bigint[]) OR old_fact_id=ANY($2::bigint[])) RETURNING 1', [sourceId, factIds]),
- trust_proposals: await count(`DELETE FROM trust_proposals WHERE source_id=$1 AND ((target_table='pages' AND target_id=$2) OR (related_table='pages' AND related_id=$2)
- OR (target_table='facts' AND target_id=ANY($3::bigint[])) OR (related_table='facts' AND related_id=ANY($3::bigint[]))
- OR (target_table='takes' AND target_id=ANY($4::bigint[])) OR (related_table='takes' AND related_id=ANY($4::bigint[]))) RETURNING 1`, [sourceId, page.id, factIds, takeIds]),
- };
- const holds = (await tx.executeRaw<{ id: string }>('DELETE FROM write_gate_holds WHERE source_id=$1 AND slug=$2 RETURNING id::text AS id', [sourceId, slug])).map(h => h.id);
- removed.write_gate_holds = holds.length;
- removed.write_gate_receipts = await count(`DELETE FROM write_gate_receipts WHERE (target_table='pages' AND target_id=$1) OR (target_table='facts' AND target_id=ANY($2::text[]))
- OR (target_table='takes' AND target_id=ANY($3::text[])) OR (target_table='write_gate_holds' AND target_id=ANY($4::text[])) RETURNING 1`,
- [String(page.id), refs, takeIds.map(String), holds]);
- const missingCaches: string[] = [];
- const cleared = await clearRebuildableCache(tx, 'query_cache');
- if (cleared === null) missingCaches.push('query_cache');
- removed.query_cache = cleared ?? 0;
- removed.facts = await count(`DELETE FROM facts WHERE source_id=$1 AND ${PAGE_FACTS} RETURNING 1`, [sourceId, slug]);
- Object.assign(removed, await scrubPurgedSlugStores(tx, sourceId, [slug]));
- // #6511 [R2]: the preparation sweep's targets, plus the tail past its ceiling and the rows pending at scan time, re-tested here.
- const recheck = await lookup('tail_recheck', () => recheckRequestsCarrying(tx, sourceId, needles, prepared.sweep,
- { exclude: row.id, beforePending: () => faultPoint('purge:pending_recheck', { requestId: row.request_id, sourceId, operation: row.operation }) }));
- // The page's add_timeline_entry receipts keep the entry text in their outcome (`entry` summary and source, `bullet`), compacted
- // ones included. That text is matched whole in the page's own requests (a set lookup, linear in their strings). A page past
- // the cap leaves the journal sweep incomplete instead of claiming every receipt.
- const timelineRows = await lookup('slug_lookup', () => tx.executeRaw<IntentRedactionTarget & { summary: string | null; source: string | null; bullet: string | null }>(`SELECT id::text AS id,
- principal_kind, principal_id, outcome->'entry'->>'summary' AS summary, outcome->'entry'->>'source' AS source, outcome->>'bullet' AS bullet FROM persistence_requests
- WHERE source_id=$1 AND slug=$2 AND operation='add_timeline_entry' AND outcome ? 'entry' ORDER BY sequence LIMIT $3`, [sourceId, slug, TIMELINE_RECEIPT_CAP + 1]));
- const timeline = timelineRows.slice(0, TIMELINE_RECEIPT_CAP);
- const timelineText = new Set(timeline.flatMap(t => [t.summary, t.source, t.bullet]).map(text => text?.trim() ?? '').filter(Boolean));
- const forSlug = await lookup('slug_lookup', async () => [...await findRequestsForSlugs(tx, sourceId, [slug]), ...reasons.length ? await requestsRecordingReasons(tx, sourceId, slug) : [], ...timeline]);
- const own = new Set(forSlug.map(r => r.id));
- const requests = [...new Map([...forSlug, ...prepared.sweep.targets, ...recheck.targets].map(r => [r.id, r])).values()];
- // GBRA-217: the page's reasons are needles in its own requests only, so a short reason never redacts another page's receipt.
- removed.persistence_requests = await lookup('redaction_read', async () => await redactRequestIntents(tx, requests.filter(r => own.has(r.id)), row.id,
- { skipPending: true, needles: [...needles, ...reasons], exact: timelineText }) + await redactRequestIntents(tx, requests.filter(r => !own.has(r.id)), row.id, { skipPending: true, needles }));
- removed.persistence_effects = await stripPageGitNotes(tx, sourceId, slug);
+ let recheck: JournalRecheck = { targets: [], tail: 0, pendingRechecked: 0, complete: true };
+ const { removed, blobs, missingCaches, complete } = await sweepPurgedPage(tx, { sourceId, slug, pageId: page.id, requestId: row.id, factIds, takeIds, needles, reasons, lookup,
+ // #6511 [R2]: the preparation sweep's targets, plus the tail past its ceiling and the rows pending at scan time, re-tested here.
+ census: async () => {
+ recheck = await lookup('tail_recheck', () => recheckRequestsCarrying(tx, sourceId, needles, prepared.sweep,
+ { exclude: row.id, beforePending: () => faultPoint('purge:pending_recheck', { requestId: row.request_id, sourceId, operation: row.operation }) }));
+ return { targets: [...prepared.sweep.targets, ...recheck.targets], complete: prepared.sweep.complete && recheck.complete };
+ } });
const journalSweep = { scanned: prepared.sweep.scanned, ceiling: prepared.sweep.ceiling, watermark: prepared.sweep.watermark ?? null, batch: prepared.sweep.batch ?? 1,
- tail: recheck.tail, pending_rechecked: recheck.pendingRechecked, complete: prepared.sweep.complete && recheck.complete && timelineRows.length <= TIMELINE_RECEIPT_CAP };
- const files = await tx.executeRaw<{ storage_path: string }>(`DELETE FROM files WHERE (page_id=$1 OR (page_id IS NULL AND page_slug=$2 AND (source_id=$3 OR source_id IS NULL)))
- RETURNING storage_path`, [page.id, slug, sourceId]);
- removed.files = files.length;
- Object.assign(removed, await (await import('../generations/ledger-scrub.ts')).scrubGenerationLedger(tx, sourceId, { slug }));
+ tail: recheck.tail, pending_rechecked: recheck.pendingRechecked, complete };
const [cascade] = await tx.executeRaw<{ chunks: number; versions: number; takes: number; timeline: number }>(`SELECT
(SELECT count(*)::int FROM content_chunks WHERE page_id=$1) AS chunks, (SELECT count(*)::int FROM page_versions WHERE page_id=$1) AS versions,
(SELECT count(*)::int FROM takes WHERE page_id=$1) AS takes, (SELECT count(*)::int FROM timeline_entries WHERE page_id=$1) AS timeline`, [page.id]);
@@ -375,11 +450,8 @@ export async function purgePageInTransaction(tx: BrainEngine, row: WriteRequest,
derived_hidden: await appendDerivedHidden(tx, { sourceId, request: row.id, publicationId: row.id, op: 'delete_page', targets: heldTargets, truncated: walk === 'truncated' }),
derived_walk: walk })) : null;
if (ledger) removed.belief_events = ledger.purged_events + ledger.redacted_fields.length;
- if (!prepared.keepOrigin) await writeOriginDeletedMarker(tx, { sourceId, page: { id: page.id, slug, source_path: page.source_path } });
- const { shas, ...segmentRows } = await deletePageWithSegments(tx, sourceId, slug, page.id, segmentClaims);
- Object.assign(removed, segmentRows);
- Object.assign(removed, await scrubPurgedSlugs(tx, sourceId, [slug]));
- Object.assign(removed, await scrubPurgedNameStores(tx, sourceId, [slug]));
+ const { shas, removed: deleted } = await deletePurgedPage(tx, { sourceId, slug, pageId: page.id, claims: segmentClaims, origin: prepared.keepOrigin ? null : { source_path: page.source_path } });
+ Object.assign(removed, deleted);
if (!retention) appendPurgeIntent({ request: row.id, request_id: row.request_id, source_id: sourceId, source_incarnation: row.source_incarnation,
scope: 'page', slug, purge_epoch: epoch, content_hashes: [...hashes], facts: mirrorFacts, takes: mirrorTakes, lifetimes });
const key = `${sourceId}:${row.request_id}`;
@@ -391,7 +463,7 @@ export async function purgePageInTransaction(tx: BrainEngine, row: WriteRequest,
...(missingCaches.length ? { missing_caches: missingCaches } : {}),
...(ledger ? { ledger: { purged_events: ledger.purged_events, redacted_fields: ledger.redacted_fields.length, live_carriers: ledger.live_carriers.length, derived_hidden: ledger.derived_hidden, derived_walk: ledger.derived_walk } } : {}),
derived: { ...derived, truncated: derivedFrom.truncated },
- blobs: files.map(f => f.storage_path).filter(Boolean) } };
+ blobs } };
}
/**
diff --git a/src/core/persistence/purge-deleted.ts b/src/core/persistence/purge-deleted.ts
index c9ee898ddc..ae57872551 100644
--- a/src/core/persistence/purge-deleted.ts
+++ b/src/core/persistence/purge-deleted.ts
@@ -15,7 +15,7 @@ import { identityDowngradePurgeHold } from '../sync-identity-downgrades.ts';
import { atomRetirementPurgeHold } from './atom-retirement-hold.ts';
import { derivedRetirementPurgeHold } from './derived-retirement-hold.ts';
import { faultPoint } from './fault-points.ts';
-import { clearDerivedOriginParks, DERIVED_ORIGIN_PARKED_CODE, derivedOriginPurgeHold, readDerivedOriginParks, type DerivedOriginPark } from './derived-origin-hold.ts';
+import { clearDerivedOriginParks, DERIVED_ORIGIN_PARKED_CODE, derivedOriginPurgeHold, pruneOwedPurges, readDerivedOriginParks, readOwedPurges, recordOwedPurge, releaseStaleHolds, type DerivedOriginPark } from './derived-origin-hold.ts';
import type { CarryForecast } from '../repair/origin-carry.ts';
import { writerOnboardingPreflight } from './onboarding.ts';
import { clearPurgePark, isParked, notePurgeSourceWriter, PURGE_PARK_LIMIT, purgeParkKey, prunePurgeParks, readPurgeParks, recordPurgeFailure, type PurgePark } from './purge-parks.ts';
@@ -365,7 +365,11 @@ export type PurgeFilter = (page: { source_id: string; slug: string }) => boolean
export async function purgeDeletedPagesCoordinated(engine: BrainEngine, olderThanHours: number, opts: { retryParked?: boolean; automatic?: boolean; only?: PurgeFilter; release?: readonly number[] } = {}): Promise<DeletedPagePurge> {
const { release = [], ...purge } = opts;
- const owed = new Set((await readDerivedOriginParks(engine)).filter(park => park.held_request_id).map(park => park.page_id));
+ // GST-2766: an owner purge a park held is recorded apart from the park, so the carry clearing the park never drops it.
+ await releaseStaleHolds(engine);
+ for (const park of await readDerivedOriginParks(engine)) if (park.held_request_id) await recordOwedPurge(engine, park.page_id, park.held_request_id);
+ await pruneOwedPurges(engine);
+ const owed = new Set((await readOwedPurges(engine)).map(record => record.page_id));
const carried = await (await import('../repair/origin-carry.ts')).carryPrivateBeforePurge(engine, olderThanHours);
const origins = originParked(carried.parks);
// GST-2689: a consented manual purge releases now the parked origins its preview counted, once their carry landed.
@@ -384,13 +388,49 @@ function originParked(parks: DerivedOriginPark[]): DeletedPagePurge['blocked'] {
+ 'the next purge tries again once the cause is fixed (gbrain doctor: derived_origin_parked).' }));
}
+/**
+ * GST-2766, a brain without managed persistence: each expired page an owner purge is owed for (held, then released), under
+ * the purge's other holds, goes through the coordinator as that explicit purge, so it writes the purge tombstones the
+ * bulk expiry does not. A failure is reported and retried by the next run.
+ */
+async function purgeOwedPages(engine: BrainEngine, config: OperationContext['config'], olderThanHours: number, ids: readonly number[], only?: PurgeFilter) {
+ const out = { slugs: [] as string[], blocked: [] as DeletedPagePurge['blocked'], failed: 0, reparked: [] as DerivedOriginPark[] };
+ if (!ids.length) return out;
+ const rows = await engine.executeRaw<{ id: number; source_id: string; slug: string }>(`SELECT p.id, p.source_id, p.slug FROM pages p WHERE p.id = ANY($1::int[])
+ AND p.deleted_at IS NOT NULL AND p.deleted_at < now() - ($2 || ' hours')::interval AND ${identityDowngradePurgeHold('p')} AND ${atomRetirementPurgeHold('p')}
+ AND ${derivedRetirementPurgeHold('p')} AND ${derivedOriginPurgeHold('p')} ORDER BY p.deleted_at, p.id`, [[...ids], String(Math.max(0, Math.floor(olderThanHours)))]);
+ for (const row of rows) {
+ if (only && !only(row)) continue;
+ try {
+ const ctx: OperationContext = { engine, config, remote: false, sourceId: row.source_id, dryRun: false, logger: { info() {}, warn() {}, error() {} } };
+ const snapshot = await engine.readPageSnapshot(row.slug, { sourceId: row.source_id, includeDeleted: true });
+ if (!snapshot?.page.deleted_at) continue;
+ const purged = await submitPageMutation(ctx, { operation: 'delete_page', waitMs: maintenancePublishWaitMs(), params: { slug: row.slug, source_id: row.source_id,
+ purge: true, expected_revision: snapshot.revision, request_id: randomUUID() } });
+ if ((purged.purge_held as { code?: unknown } | undefined)?.code === DERIVED_ORIGIN_PARKED_CODE) {
+ out.reparked.push(...(await readDerivedOriginParks(engine)).filter(park => park.page_id === Number(row.id)));
+ continue;
+ }
+ if (purged.status === 'purged') out.slugs.push(row.slug);
+ } catch (error) {
+ out.failed++;
+ out.blocked.push({ source_id: row.source_id, slug: row.slug, code: typeof (error as { code?: unknown })?.code === 'string' ? (error as { code: string }).code : null,
+ reason: `${error instanceof Error ? error.message : String(error)}; retried on the next run` });
+ }
+ }
+ return out;
+}
+
async function purgeExpired(engine: BrainEngine, olderThanHours: number, opts: { retryParked?: boolean; automatic?: boolean; only?: PurgeFilter; owed?: ReadonlySet<number> }): Promise<DeletedPagePurge> {
const { parkUncarriedInPurge } = await import('../repair/origin-carry.ts');
+ const config = loadConfig() ?? { engine: engine.kind };
if (!await managedPersistenceEnabled(engine)) {
// #7130: a brain deactivated after managed syncs keeps their tombstones' records and receipts, so the hold applies here too.
const kept = opts.automatic ? await readUnreviewedSyncTombstones(engine, olderThanHours) : [];
- const recorded = await expireRecordedTombstones(engine, olderThanHours, opts.retryParked === true, new Set(kept.map(row => row.id)), opts.only);
- const exclude = [...recorded.exclude, ...kept.map(row => row.id)];
+ // GST-2766: a page an owner purge is owed for is left out of both expiries and purged as that purge, tombstones included.
+ const owed = [...opts.owed ?? []].filter(id => !kept.some(row => row.id === id));
+ const recorded = await expireRecordedTombstones(engine, olderThanHours, opts.retryParked === true, new Set([...kept.map(row => row.id), ...owed]), opts.only);
+ const exclude = [...recorded.exclude, ...kept.map(row => row.id), ...owed];
const reparked = [...recorded.reparked];
const result = await maintenanceTransaction(engine, async tx => {
const unlisted = opts.only ? (await tx.executeRaw<{ id: number; source_id: string; slug: string }>(
@@ -404,17 +444,18 @@ async function purgeExpired(engine: BrainEngine, olderThanHours: number, opts: {
reparked.push(...await parkUncarriedInPurge(tx, expiring.map(page => Number(page.id)).filter(id => !skip.has(id))));
return tx.purgeDeletedPages(olderThanHours, excludeIds.length ? { excludeIds } : undefined);
});
+ const owner = await purgeOwedPages(engine, config, olderThanHours, owed, opts.only);
+ reparked.push(...owner.reparked);
const held = kept.filter(row => row.expired);
- return { slugs: [...recorded.slugs, ...result.slugs], count: recorded.slugs.length + result.count,
- blocked: [...originParked(reparked), ...recorded.blocked, ...held.map(row => syncDeleteHeld(row, olderThanHours))],
- deferred: 0, failed: recorded.failed, inactive: 0, parked: recorded.parked, capacity_deferred: 0, held: held.length, ...(reparked.length ? { origin_parked: reparked.length } : {}) };
+ return { slugs: [...recorded.slugs, ...result.slugs, ...owner.slugs], count: recorded.slugs.length + result.count + owner.slugs.length,
+ blocked: [...originParked(reparked), ...recorded.blocked, ...owner.blocked, ...held.map(row => syncDeleteHeld(row, olderThanHours))],
+ deferred: 0, failed: recorded.failed + owner.failed, inactive: 0, parked: recorded.parked, capacity_deferred: 0, held: held.length, ...(reparked.length ? { origin_parked: reparked.length } : {}) };
}
// W3 (#7587): an automatic run reconciles the purges an earlier run left pending before it selects or submits anything.
const deferral = await DreamWriteDeferralPass.start(engine, 'purge', opts.automatic === true);
const selection = await selectPurgeCandidates(engine, olderThanHours, opts);
for (const [sourceId, active] of Object.entries(selection.writers)) await notePurgeSourceWriter(engine, sourceId, active);
await prunePurgeParks(engine);
- const config = loadConfig() ?? { engine: engine.kind };
const slugs: string[] = [];
const blocked: DeletedPagePurge['blocked'] = [...selection.blocked];
let deferred = 0, failed = 0, capacityDeferred = 0;
diff --git a/src/core/repair/origin-carry.ts b/src/core/repair/origin-carry.ts
index 6108e7ff68..1f86027871 100644
--- a/src/core/repair/origin-carry.ts
+++ b/src/core/repair/origin-carry.ts
@@ -21,7 +21,7 @@ import { serializePageToMarkdown } from '../markdown.ts';
import { isSegmentedPage } from '../page-body/page-shape.ts';
import type { LabelEdit } from '../persistence/label-edit.ts';
import { maintenancePreflight, publishMaintenancePages } from '../persistence/prepared-maintenance.ts';
-import { clearDerivedOriginParks, DERIVED_ORIGIN_PARKED_CODE, derivedOriginPurgeHold, finishPurgeNote, parkDerivedOrigin, pruneDerivedOriginParks, readDerivedOriginParks, type DerivedOriginPark } from '../persistence/derived-origin-hold.ts';
+import { clearDerivedOriginParks, DERIVED_ORIGIN_PARKED_CODE, derivedOriginPurgeHold, finishPurgeNote, parkDerivedOrigin, pruneDerivedOriginParks, readDerivedOriginParks, recordOwedPurge, type DerivedOriginPark } from '../persistence/derived-origin-hold.ts';
import { privacyPartSql, readHonorAliases, withoutCallerAccess } from '../search/private-visibility.ts';
import { faultPoint } from '../persistence/fault-points.ts';
import { filesInCheckout, importAtRevision, writeLabelFile } from './label-write.ts';
@@ -167,6 +167,7 @@ export async function deletePageCarryingPrivate(ctx: OperationContext, p: Record
await parkDerivedOrigin(ctx.engine, { ...parked(origin, block), held_request_id: String(p.request_id) });
const result = origin.deleted ? { status: 'held', slug: origin.slug, source_id: sourceId, request_id: p.request_id }
: await submitPageMutation(ctx, { operation: 'delete_page', params: { ...p, purge: false, request_id: derivedRequestId(String(p.request_id)) } });
+ await recordOwedPurge(ctx.engine, origin.id, String(p.request_id));
return { ...result, purge_held: { code: DERIVED_ORIGIN_PARKED_CODE,
message: `Not purged: ${block.slug ?? 'a page derived from it'} is private only through ${origin.slug} and could not be labelled private (${block.code ?? 'error'}: ${block.reason}). `
+ `${origin.slug} is soft-deleted and parked: no purge removes it until that page carries its own label; gbrain doctor names it as derived_origin_parked. `
diff --git a/test/belief-ledger-restore.test.ts b/test/belief-ledger-restore.test.ts
index 3e3a2ad4a6..b64377e463 100644
--- a/test/belief-ledger-restore.test.ts
+++ b/test/belief-ledger-restore.test.ts
@@ -24,6 +24,7 @@ import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
import { __setPurgeRedactionFaultForTests } from '../src/core/belief/purge.ts';
import { recordDerivationInputs } from '../src/core/facts/derivation-inputs.ts';
import { withCoordinatedWrite } from '../src/core/persistence/context.ts';
+import { submitPageMutation } from '../src/core/persistence/page-mutations.ts';
import { TEST_WRITE_ATTRIBUTION } from './helpers/write-attribution.ts';
import { beliefBrain, beliefEngineCase, beliefPage, openFileBeliefBrain, withFileBeliefBrain, type BeliefBrain, type FileBeliefBrain } from './helpers/belief-ledger.ts';
import { testBackends } from './helpers/test-backends.ts';
@@ -294,6 +295,132 @@ describe('belief ledger restore (pglite archive)', () => {
expect(r).toEqual({ purge_replay: { state: 'complete', replays: ['complete'], ledger: 'absent' }, left: [], tombstones: 1 });
}, 300_000);
+ /** #9469: one page's text in each store a live page purge sweeps, for a replay parity probe. */
+ interface ParityPage { sourceId: string; slug: string; claim: string; timeline: string; reason: string }
+ const PURGED_REPLAY: Omit<ParityPage, 'sourceId'> = { slug: 'notes/purged-replay-example', claim: X, timeline: 'Visited the vault at the harbor office', reason: 'Reason quoting the vault plan' };
+ const KEPT_REPLAY: Omit<ParityPage, 'sourceId'> = { slug: 'notes/kept-replay-example', claim: 'Zebra fence code is 9921', timeline: 'Walked the dog by the river', reason: 'Reason about the dog walk' };
+ const REPOLESS = 'repoless-example';
+
+ /** Writes the page through the coordinator with a change reason, a fact and a timeline entry, then a file record, a core-edit notice, a generation preimage and (on a source with a checkout) its Git effect's reason trailer. */
+ async function seedParity(b: BeliefBrain, page: ParityPage): Promise<void> {
+ const ctx = { ...b.ctx, sourceId: page.sourceId };
+ const op = (operation: string, params: Record<string, unknown>) => submitPageMutation(ctx as never, { operation: operation as never, params: { request_id: randomUUID(), ...params } as never });
+ await op('put_page', { slug: page.slug, content: beliefPage({ prose: `The plan. ${page.claim}.` }), force: true, reason: page.reason });
+ await op('put_page', { slug: page.slug, content: beliefPage({ prose: `The plan. ${page.claim}.`, facts: [page.claim] }), force: true });
+ await op('add_timeline_entry', { slug: page.slug, date: '2024-01-02', summary: page.timeline, source: 'notes' });
+ const [row] = await b.engine.executeRaw<{ id: number }>('SELECT id FROM pages WHERE source_id = $1 AND slug = $2', [page.sourceId, page.slug]);
+ const [request] = await b.engine.executeRaw<{ id: string }>('SELECT id::text AS id FROM persistence_requests WHERE source_id = $1 AND slug = $2 ORDER BY sequence LIMIT 1', [page.sourceId, page.slug]);
+ await b.engine.transaction(tx => withCoordinatedWrite(tx, [page.sourceId], async () => {
+ await tx.executeRaw(`INSERT INTO files (source_id, page_slug, page_id, filename, storage_path, content_hash) VALUES ($1, $2, $3, 'plan.pdf', $4, 'sha-example')`,
+ [page.sourceId, page.slug, row!.id, `files/${page.sourceId}/${page.slug}.pdf`]);
+ await tx.executeRaw(`INSERT INTO core_edit_notices (source_id, slug, page_id, base_text, actor) VALUES ($1, $2, $3, $4, 'example')`, [page.sourceId, page.slug, row!.id, page.claim]);
+ const [gen] = await tx.executeRaw<{ id: number }>(`INSERT INTO derived_generations (source_id, kind, owner_key, state, operation_id, item_set_hash) VALUES ($1, 'example', 'page:' || $2, 'superseded', 'op-example', 'hash-example') RETURNING id`,
+ [page.sourceId, page.slug]);
+ await tx.executeRaw(`INSERT INTO derived_generation_items (generation_id, item_key, identity_hash, action, preimage) VALUES ($1, $2, 'identity-example', 'published', $3::text::jsonb)`,
+ [gen!.id, page.slug, JSON.stringify({ text: page.claim })]);
+ if (page.sourceId !== REPOLESS) {
+ await tx.executeRaw(`INSERT INTO persistence_effects (request_id, kind, data, state, source_id) VALUES ($1::uuid, 'git', $2::text::jsonb, 'committed', $3) ON CONFLICT DO NOTHING`,
+ [request!.id, JSON.stringify({ slug: page.slug, commit_line: `Update ${page.slug}`, commit_trailers: [`gbrain-reason: ${page.reason}`] }), page.sourceId]);
+ }
+ }, TEST_WRITE_ATTRIBUTION));
+ }
+
+ /** The stores of the source that still carry the page's claim, timeline entry or reason, or name its slug. */
+ async function parityStores(engine: BrainEngine, page: ParityPage): Promise<string[]> {
+ const found: string[] = [];
+ const needles = [page.claim, page.timeline, page.reason].map(n => n.toLowerCase());
+ const carries = async (store: string, sql: string) => {
+ const rows = await engine.executeRaw<{ t: string | null }>(sql, [page.sourceId, page.slug]);
+ if (rows.some(r => needles.some(n => (r.t ?? '').toLowerCase().includes(n)))) found.push(store);
+ };
+ const names = async (store: string, sql: string) => { if ((await engine.executeRaw(sql, [page.sourceId, page.slug])).length) found.push(store); };
+ await carries('persistence_requests', "SELECT intent::text || COALESCE(outcome::text, '') AS t FROM persistence_requests WHERE source_id = $1 AND ($2::text IS NOT NULL)");
+ await names('files', 'SELECT 1 FROM files WHERE source_id = $1 AND page_slug = $2');
+ await names('core_edit_notices', 'SELECT 1 FROM core_edit_notices WHERE source_id = $1 AND slug = $2');
+ await carries('derived_generation_items', 'SELECT i.preimage::text AS t FROM derived_generation_items i JOIN derived_generations g ON g.id = i.generation_id WHERE g.source_id = $1 AND i.item_key = $2');
+ await carries('persistence_effects', "SELECT data::text AS t FROM persistence_effects WHERE source_id = $1 AND kind = 'git' AND data->>'slug' = $2");
+ return found;
+ }
+
+ /** Every row of the page and of the stores keyed to it, as stored, for a byte-for-byte comparison. */
+ async function parityRows(engine: BrainEngine, page: ParityPage): Promise<Record<string, string[]>> {
+ const [row] = await engine.executeRaw<{ id: number }>('SELECT id FROM pages WHERE source_id = $1 AND slug = $2', [page.sourceId, page.slug]);
+ const read = async (sql: string, params: unknown[]) => (await engine.executeRaw<{ r: string }>(sql, params)).map(x => x.r).sort();
+ const id = row?.id ?? -1;
+ return {
+ pages: await read('SELECT row_to_json(t)::text AS r FROM pages t WHERE id = $1', [id]),
+ facts: await read('SELECT row_to_json(t)::text AS r FROM facts t WHERE source_id = $1 AND source_markdown_slug = $2', [page.sourceId, page.slug]),
+ timeline_entries: await read('SELECT row_to_json(t)::text AS r FROM timeline_entries t WHERE page_id = $1', [id]),
+ page_versions: await read('SELECT row_to_json(t)::text AS r FROM page_versions t WHERE page_id = $1', [id]),
+ files: await read('SELECT row_to_json(t)::text AS r FROM files t WHERE source_id = $1 AND page_slug = $2', [page.sourceId, page.slug]),
+ core_edit_notices: await read('SELECT row_to_json(t)::text AS r FROM core_edit_notices t WHERE source_id = $1 AND slug = $2', [page.sourceId, page.slug]),
+ derived_generation_items: await read('SELECT row_to_json(i)::text AS r FROM derived_generation_items i JOIN derived_generations g ON g.id = i.generation_id WHERE g.source_id = $1 AND i.item_key = $2', [page.sourceId, page.slug]),
+ derived_generations: await read("SELECT row_to_json(g)::text AS r FROM derived_generations g WHERE g.source_id = $1 AND g.owner_key = 'page:' || $2", [page.sourceId, page.slug]),
+ // An effect's state and attempts move as background work runs after the restore; what a purge would change is its data.
+ persistence_effects: await read("SELECT json_build_object('id', id, 'kind', kind, 'data', data)::text AS r FROM persistence_effects t WHERE source_id = $1 AND data->>'slug' = $2", [page.sourceId, page.slug]),
+ // A settled request's claim bookkeeping (execution token, terminal reservation) is released after the restore opens; a purge changes neither.
+ persistence_requests: await read("SELECT (to_jsonb(t) - 'execution_token' - 'terminal_reservation' - 'claim_expires_at')::text AS r FROM persistence_requests t WHERE source_id = $1 AND slug = $2", [page.sourceId, page.slug]),
+ };
+ }
+
+ /** Seeds a purged and a kept page in \`sourceId\`, backs up, purges the first live, restores, and reports what each brain still holds. */
+ async function parityProbe(sourceId: string) {
+ const purged = { ...PURGED_REPLAY, sourceId }, kept = { ...KEPT_REPLAY, sourceId };
+ return collect<Record<string, unknown>>(resolve => withFileBeliefBrain(SOURCE, async fb => {
+ let keptBefore: Record<string, string[]> = {}, seeded: string[] = [], effects = 0;
+ await fb.open(async b => {
+ if (sourceId === REPOLESS) await b.engine.executeRaw('INSERT INTO sources (id, name) VALUES ($1, $1)', [REPOLESS]);
+ await seedParity(b, purged);
+ await seedParity(b, kept);
+ seeded = await parityStores(b.engine, purged);
+ effects = (await b.engine.executeRaw("SELECT 1 FROM persistence_effects WHERE source_id = $1 AND kind = 'git'", [sourceId])).length;
+ keptBefore = await parityRows(b.engine, kept);
+ });
+ let live: string[] = [];
+ const restored = await backupAndRestore(fb, () => fb.open(async b => {
+ const ctx = { ...b.ctx, sourceId };
+ const snapshot = await b.engine.readPageSnapshot(purged.slug, { sourceId, includeDeleted: true });
+ expect((await deleteOp.handler(ctx as never, { slug: purged.slug, source_id: sourceId, purge: true, expected_revision: snapshot!.revision, request_id: randomUUID() }) as { status: string }).status).toBe('purged');
+ live = await parityStores(b.engine, purged);
+ }));
+ const out = { seeded, effects: effects > 0, live, replayed: await parityStores(restored.restored.engine, purged),
+ keptChanged: await parityRows(restored.restored.engine, kept).then(after => Object.keys(after).filter(k => JSON.stringify(after[k]) !== JSON.stringify(keptBefore[k]))), keptCarries: await parityStores(restored.restored.engine, kept),
+ outcomes: restored.result.purge_replay.replays.map(x => [x.scope, x.outcome]) };
+ await restored.close();
+ resolve(out);
+ }));
+ }
+
+ test('#9469: a replayed page purge leaves no more of the page than the live purge (request intents and timeline receipts, files, slug stores, the generation ledger, Git effect reasons), and a page it did not purge is byte-for-byte intact', async () => {
+ expect(await parityProbe(SOURCE)).toEqual({ seeded: ['persistence_requests', 'files', 'core_edit_notices', 'derived_generation_items', 'persistence_effects'], effects: true,
+ live: [], replayed: [], keptChanged: [], keptCarries: ['persistence_requests', 'files', 'core_edit_notices', 'derived_generation_items', 'persistence_effects'], outcomes: [['page', 'complete']] });
+ }, 300_000);
+
+ test('#9469 (repo-less source): no Git effects exist; the change reason lives only in the request journal, and the replay redacts it there as the live purge does', async () => {
+ expect(await parityProbe(REPOLESS)).toEqual({ seeded: ['persistence_requests', 'files', 'core_edit_notices', 'derived_generation_items'], effects: false,
+ live: [], replayed: [], keptChanged: [], keptCarries: ['persistence_requests', 'files', 'core_edit_notices', 'derived_generation_items'], outcomes: [['page', 'complete']] });
+ }, 300_000);
+
+ test('#9469: a replay whose journal sweep cannot cover every request stops the restore and names the purge it did not replay', async () => {
+ const r = await collect<Record<string, unknown>>(resolve => withFileBeliefBrain(SOURCE, async fb => {
+ const purged = { ...PURGED_REPLAY, sourceId: SOURCE };
+ await fb.open(b => seedParity(b, purged));
+ let requestId = '';
+ __setPurgeReplayFaultForTests('incomplete_census');
+ try {
+ await backupAndRestore(fb, () => fb.open(async b => {
+ requestId = randomUUID();
+ const snapshot = await b.engine.readPageSnapshot(purged.slug, { sourceId: SOURCE, includeDeleted: true });
+ await deleteOp.handler(b.ctx, { slug: purged.slug, purge: true, expected_revision: snapshot!.revision, request_id: requestId });
+ }));
+ resolve({ threw: null });
+ } catch (error) {
+ resolve({ threw: (error as { code?: string }).code ?? String(error), names: String((error as Error).message).includes(requestId) });
+ }
+ }));
+ expect(r).toEqual({ threw: 'purge_replay_incomplete', names: true });
+ }, 300_000);
+
test('#33: a page-scope replay never deletes an unrelated page that held the slug in the archive', async () => {
const r = await collect<Record<string, unknown>>(resolve => withFileBeliefBrain(SOURCE, async fb => {
const slug = 'notes/shared-slug-example';
diff --git a/test/derived-origin-carry-managed.test.ts b/test/derived-origin-carry-managed.test.ts
index ebe23143c3..a0fa46a53b 100644
--- a/test/derived-origin-carry-managed.test.ts
+++ b/test/derived-origin-carry-managed.test.ts
@@ -65,7 +65,7 @@ function fileText(k: string, shape: Shape): string {
};
return ['---', `title: ${k} ${shape}`, ...fm[shape], '---', '', `Harbor ledger planning notes ${canaryOf(k, shape)} for the roadmap.`, ''].join('\n');
}
-const KEYS = ['gitrm', 'purge', 'remote', 'midpurge', 'preview', 'finish', 'retain'];
+const KEYS = ['gitrm', 'purge', 'remote', 'midpurge', 'preview', 'finish', 'retain', 'owedauto', 'owedfail'];
/** A private origin, a public meeting, an event derived from the origin whose depth is the meeting, and an event whose depth is the origin. */
const EVENT_SLUGS = { origin: 'notes/event-origin', depth: 'notes/event-depth', event: 'notes/event-event', closed: 'notes/event-closed' };
const EVENT_FILES: Record<keyof typeof EVENT_SLUGS, string[]> = {
@@ -293,6 +293,50 @@ for (const backend of testBackends()) {
await expectCarried(engine, root, k, files);
}, { setup, databaseUrl }), 300_000);
+ /** An explicit purge of `k`'s origin held by a forced label failure, as the owner's `gbrain delete --purge` is when a derived page cannot be labelled. */
+ async function heldPurge(engine: BrainEngine, ctx: OperationContext, k: string): Promise<void> {
+ const failing = spyOn(preparedMaintenance, 'publishMaintenancePages').mockImplementation(async () => { throw new Error('forced label failure'); });
+ let held: Record<string, any>;
+ try {
+ held = await operationsByName.delete_page!.handler(ctx, { slug: slugOf(k, 'origin'), purge: true, request_id: randomUUID(),
+ expected_revision: (await engine.readPageSnapshot(slugOf(k, 'origin'), { sourceId: 'default', includeDeleted: true }))!.revision }) as Record<string, any>;
+ } finally { failing.mockRestore(); }
+ expect(held.purge_held?.code).toBe('derived_origin_parked');
+ }
+ const pageTombstones = async (engine: BrainEngine, slug: string) => (await engine.executeRaw<{ n: number }>(
+ "SELECT count(*)::int AS n FROM page_purges WHERE source_id = 'default' AND slug = $1", [slug]))[0]!.n;
+
+ test('a held explicit purge whose park an automatic purge releases before the page expires still writes its purge tombstones when an automatic purge removes it (GST-2766)', () => managedBrain(async ({ engine, root, ctx }) => {
+ await performManagedSync(engine, OPTS);
+ const k = 'owedauto', origin = slugOf(k, 'origin');
+ const files = await before(engine, root, k);
+ await heldPurge(engine, ctx, k);
+ const early = await purgeDeletedPagesCoordinated(engine, 24, { automatic: true });
+ expect(early.slugs).not.toContain(origin);
+ expect((await readDerivedOriginParks(engine)).map(park => park.slug)).not.toContain(origin);
+ const late = await purgeDeletedPagesCoordinated(engine, 0, { automatic: true });
+ expect(late.slugs).toContain(origin);
+ expect(await pageTombstones(engine, origin)).toBeGreaterThan(0);
+ await expectCarried(engine, root, k, files);
+ }, { setup, databaseUrl }), 300_000);
+
+ test('a held explicit purge whose consented purge-deleted fails after its carry still writes its purge tombstones on the next run (GST-2766)', () => managedBrain(async ({ engine, root, ctx }) => {
+ await performManagedSync(engine, OPTS);
+ const k = 'owedfail', origin = slugOf(k, 'origin');
+ const files = await before(engine, root, k);
+ await heldPurge(engine, ctx, k);
+ let fired = false;
+ installFaultHook(async point => { if (point === 'purge:after_carry' && !fired) { fired = true; throw new Error('forced failure after the carry'); } });
+ try { await expect(consentedPurgeDeletedPages(engine, 0, true)).rejects.toThrow('forced failure after the carry'); }
+ finally { installFaultHook(undefined); }
+ expect(fired).toBe(true);
+ expect((await page(engine, origin))?.deleted_at).not.toBeNull();
+ const out = await consentedPurgeDeletedPages(engine, 0, true) as { slugs: string[] };
+ expect(out.slugs).toContain(origin);
+ expect(await pageTombstones(engine, origin)).toBeGreaterThan(0);
+ await expectCarried(engine, root, k, files);
+ }, { setup, databaseUrl }), 300_000);
+
test('a remote delete: the stamp is a maintenance write whose batch names the delete request id, and its response names no stamped page', () => managedBrain(async ({ engine, root }) => {
await performManagedSync(engine, OPTS);
const k = 'remote', origin = slugOf(k, 'origin');
diff --git a/test/derived-origin-carry-private.test.ts b/test/derived-origin-carry-private.test.ts
index f9066d5b89..dd862910ba 100644
--- a/test/derived-origin-carry-private.test.ts
+++ b/test/derived-origin-carry-private.test.ts
@@ -122,6 +122,8 @@ for (const kind of testBackends()) {
const row = async (slug: string) => (await engine.executeRaw<{ id: number; frontmatter: Record<string, unknown> | string; trust_tier: string; deleted_at: string | null }>(
'SELECT id, frontmatter, trust_tier, deleted_at FROM pages WHERE source_id=$1 AND slug=$2', [SRC, slug]))[0];
+ /** GST-2766: the owed-purge records of a page id, as stored text. */
+ const owedRows = async (id: number) => (await engine.executeRaw<{ t: string }>("SELECT completed_keys::text AS t FROM op_checkpoints WHERE op = 'owed-page-purge' AND fingerprint = $1", [String(id)])).map(r => r.t);
const fm = async (slug: string) => { const r = await row(slug); return typeof r!.frontmatter === 'string' ? JSON.parse(r!.frontmatter) : r!.frontmatter; };
const state = async (k: string) => Object.fromEntries(await Promise.all(SHAPES.filter(s => s !== 'origin').map(async s => [s, { fm: await fm(slugOf(k, s)), tier: (await row(slugOf(k, s)))!.trust_tier }])));
const proposals = async () => Number((await engine.executeRaw<{ n: number }>('SELECT count(*)::int AS n FROM trust_proposals WHERE source_id=$1', [SRC]))[0]!.n);
@@ -526,6 +528,83 @@ for (const kind of testBackends()) {
await expectCarriedHidden(k);
}, 240_000);
+ test('a held owner purge that a later purge run removes writes its purge tombstones, even after an automatic run released its park first (GST-2766)', async () => {
+ const k = 'owed';
+ await seed(k);
+ const origin = slugOf(k, 'origin');
+ const id = Number((await row(origin))!.id);
+ await parkByFailedLabel(origin);
+ expect(await owedRows(id)).toEqual([expect.not.stringContaining(origin)]);
+ const early = await purgeDeletedPagesCoordinated(engine, 24, { automatic: true });
+ expect(early.slugs).not.toContain(origin);
+ expect((await readDerivedOriginParks(engine)).map(park => park.slug)).not.toContain(origin);
+ const late = await purgeDeletedPagesCoordinated(engine, 0, { automatic: true });
+ expect(late.slugs).toContain(origin);
+ const [{ n }] = await engine.executeRaw<{ n: number }>('SELECT count(*)::int AS n FROM page_purges WHERE source_id=$1 AND slug=$2', [SRC, origin]);
+ expect(n).toBeGreaterThan(0);
+ expect(await owedRows(id)).toEqual([]);
+ await nothingKept(origin);
+ await expectCarriedHidden(k);
+ }, 240_000);
+
+ test('a held owner purge the owner then restores owes nothing: its record goes with the restore, and a later delete of the page is purged as retention without tombstones (GST-2766)', async () => {
+ const k = 'owedrestore';
+ await seed(k);
+ const origin = slugOf(k, 'origin');
+ const id = Number((await row(origin))!.id);
+ await parkByFailedLabel(origin);
+ expect((await owedRows(id)).length).toBe(1);
+ const restored = await localCall('restore_page', { slug: origin, request_id: randomUUID(), expected_revision: await revision(origin) });
+ expect({ error: restored.isError, text: restored.text }).toMatchObject({ error: false });
+ expect((await row(origin))?.deleted_at).toBeNull();
+ expect(await owedRows(id)).toEqual([]);
+ expect((await purgeDeletedPagesCoordinated(engine, 0, { automatic: true })).slugs).not.toContain(origin);
+ expect((await row(origin))?.deleted_at).toBeNull();
+ await softDelete(origin);
+ await backdate(origin);
+ expect((await purgeDeletedPagesCoordinated(engine, 0, { automatic: true })).slugs).toContain(origin);
+ const [{ n }] = await engine.executeRaw<{ n: number }>('SELECT count(*)::int AS n FROM page_purges WHERE source_id=$1 AND slug=$2', [SRC, origin]);
+ expect(n).toBe(0);
+ expect(await owedRows(id)).toEqual([]);
+ }, 240_000);
+
+ test('a held owner purge the owner restores and deletes again before any purge run owes nothing: the hold is bound to the tombstone it held, so a later run purges the page as retention without tombstones (GST-2766)', async () => {
+ const k = 'owedredelete';
+ await seed(k);
+ const origin = slugOf(k, 'origin');
+ const id = Number((await row(origin))!.id);
+ await parkByFailedLabel(origin);
+ const restored = await localCall('restore_page', { slug: origin, request_id: randomUUID(), expected_revision: await revision(origin) });
+ expect({ error: restored.isError, text: restored.text }).toMatchObject({ error: false });
+ expect(await owedRows(id)).toEqual([]);
+ await softDelete(origin);
+ await backdate(origin);
+ const first = await purgeDeletedPagesCoordinated(engine, 0, { automatic: true });
+ const second = await purgeDeletedPagesCoordinated(engine, 0, { automatic: true });
+ expect([...first.slugs, ...second.slugs]).toContain(origin);
+ const [{ n }] = await engine.executeRaw<{ n: number }>('SELECT count(*)::int AS n FROM page_purges WHERE source_id=$1 AND slug=$2', [SRC, origin]);
+ expect(n).toBe(0);
+ expect(await owedRows(id)).toEqual([]);
+ }, 240_000);
+
+ test('a held owner purge whose page comes back through the engine restore (the large-page, guest and atoms restores share it) and is deleted again owes nothing, and the park no longer names the old request (GST-2766)', async () => {
+ const k = 'owedengine';
+ await seed(k);
+ const origin = slugOf(k, 'origin');
+ const id = Number((await row(origin))!.id);
+ await parkByFailedLabel(origin);
+ expect(await engine.restorePage(origin, { sourceId: SRC })).toBe(true);
+ await softDelete(origin);
+ await backdate(origin);
+ const first = await purgeDeletedPagesCoordinated(engine, 0, { automatic: true });
+ expect((await readDerivedOriginParks(engine)).filter(park => park.page_id === id).map(park => park.held_request_id ?? null)).not.toContainEqual(expect.any(String));
+ const second = await purgeDeletedPagesCoordinated(engine, 0, { automatic: true });
+ expect([...first.slugs, ...second.slugs]).toContain(origin);
+ const [{ n }] = await engine.executeRaw<{ n: number }>('SELECT count(*)::int AS n FROM page_purges WHERE source_id=$1 AND slug=$2', [SRC, origin]);
+ expect(n).toBe(0);
+ expect(await owedRows(id)).toEqual([]);
+ }, 240_000);
+
test('restoring a parked origin drops its park: doctor stops naming it, and the next purge run deletes the park row (GST-2128, #9347 finding 6)', async () => {
const k = 'restorepark';
await seed(k);
diff --git a/test/doctor-purge-mirror.test.ts b/test/doctor-purge-mirror.test.ts
new file mode 100644
index 0000000000..914b6a7f2a
--- /dev/null
+++ b/test/doctor-purge-mirror.test.ts
@@ -0,0 +1,65 @@
+/**
+ * #9469: doctor `purge_mirror` warns when a purge of the last 30 days has no line in the purge mirror of this process's
+ * config directory (a writer run in a throwaway home), names the mirror path and counts only, and is quiet when the
+ * mirror holds every recent purge or when the only purges are older than the window. Never a failure.
+ */
+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
+import { randomUUID } from 'node:crypto';
+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { PURGE_MIRROR_FILE } from '../src/core/belief/purge-mirror.ts';
+import { checkPurgeMirror } from '../src/commands/doctor/checks/purge-mirror.ts';
+
+const SLUG = 'notes/purged-mirror-example';
+
+describe('doctor purge_mirror', () => {
+ let engine: PGLiteEngine;
+ const root = mkdtempSync(join(tmpdir(), 'gbrain-purge-mirror-'));
+ beforeAll(async () => { engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema(); }, 120_000);
+ afterAll(async () => { await engine.disconnect(); rmSync(root, { recursive: true, force: true }); });
+
+ const home = (lines: object[]) => {
+ const dir = join(root, randomUUID().slice(0, 8));
+ mkdirSync(dir, { recursive: true });
+ writeFileSync(join(dir, 'config.json'), '{}');
+ if (lines.length) writeFileSync(join(dir, PURGE_MIRROR_FILE), lines.map(line => JSON.stringify(line)).join('\n') + '\n');
+ return dir;
+ };
+
+ test('no recent purge: ok; an old purge outside the window: ok', async () => {
+ expect((await checkPurgeMirror(engine, home([]))).status).toBe('ok');
+ await engine.executeRaw(`INSERT INTO page_purges (source_id, content_hash, slug, request_id, purged_at) VALUES ('default', 'hash-old-example', $1, $2::uuid, now() - interval '40 days')`,
+ [SLUG, randomUUID()]);
+ const check = await checkPurgeMirror(engine, home([]));
+ expect(check.status).toBe('ok');
+ expect(check.details).toMatchObject({ recent: 0, missing: 0 });
+ });
+
+ test('a recent purge the mirror lacks warns with counts and the fix, never the slug; the mirror that holds it is ok', async () => {
+ const request = randomUUID();
+ await engine.executeRaw(`INSERT INTO page_purges (source_id, content_hash, slug, request_id) VALUES ('default', 'hash-new-example', $1, $2::uuid)`, [SLUG, request]);
+ await engine.executeRaw(`INSERT INTO fact_purges (source_id, visibility, subject, fact_hash, request_id, actor, reason) VALUES ('default', 'world', $1, 'fp-example', $2::uuid, 'cli:owner-example', 'page purge')`,
+ [SLUG, request]);
+ const empty = home([]);
+ const warned = await checkPurgeMirror(engine, empty);
+ expect(warned.status).toBe('warn');
+ expect(warned.details).toMatchObject({ recent: 1, missing: 1, readable: true });
+ expect(warned.message).toContain('GBRAIN_HOME');
+ expect(warned.message).toContain(join(empty, PURGE_MIRROR_FILE));
+ expect(JSON.stringify(warned)).not.toContain(SLUG);
+ expect(JSON.stringify(warned)).not.toContain(request);
+ const held = await checkPurgeMirror(engine, home([{ kind: 'intent', request, request_id: randomUUID(), source_id: 'default', scope: 'page', recorded_at: new Date().toISOString() },
+ { kind: 'committed', request, recorded_at: new Date().toISOString() }]));
+ expect(held.status).toBe('ok');
+ expect(held.details).toMatchObject({ recent: 1, missing: 0 });
+ });
+
+ test('a home with no brain config and no mirror reads as unavailable and still only warns', async () => {
+ const gone = join(root, 'missing-home');
+ const check = await checkPurgeMirror(engine, gone);
+ expect(check.status).toBe('warn');
+ expect(check.details).toMatchObject({ missing: 1, readable: false });
+ });
+});
diff --git a/test/fixtures/goldens/doctor/content-pglite-human.json b/test/fixtures/goldens/doctor/content-pglite-human.json
index 0d4c47829b..397c71d8c9 100644
--- a/test/fixtures/goldens/doctor/content-pglite-human.json
+++ b/test/fixtures/goldens/doctor/content-pglite-human.json
@@ -273,6 +273,7 @@
" [OK] rename_identity_downgrades: No rename that lost its page identity is waiting for a disposition.",
" [OK] purge_parked_pages: No soft-deleted page is parked by the managed purge.",
" [OK] derived_origin_parked: No soft-deleted page is kept from the purge to keep a page derived from it private.",
+ " [OK] purge_mirror: No purge in the last 30 days to check against the purge mirror at <home>/.gbrain/purge-events.jsonl.",
" [OK] derivation_purge_sweeps: No fact purge has a sweep of stored derivation results pending or stalled.",
" [INFO] missing_recorded_files: Not applicable: no source is a classic checkout on this host (managed sources are checked by ghost_pages).",
" [INFO] unindexed_files: Unindexed files are not known for default (not computed yet), notes (not computed yet). The maintenance phase unindexed_files computes them on each run and continues an incomplete scan; to compute them now, run gbrain sources retry-held <id> --unindexed.",
diff --git a/test/fixtures/goldens/doctor/content-pglite-json.json b/test/fixtures/goldens/doctor/content-pglite-json.json
index d965c72f9f..308107028f 100644
--- a/test/fixtures/goldens/doctor/content-pglite-json.json
+++ b/test/fixtures/goldens/doctor/content-pglite-json.json
@@ -3952,6 +3952,18 @@
"name": "derived_origin_parked",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "missing": 0,
+ "path": "<home>/.gbrain/purge-events.jsonl",
+ "recent": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No purge in the last 30 days to check against the purge mirror at <home>/.gbrain/purge-events.jsonl.",
+ "name": "purge_mirror",
+ "status": "ok"
+ },
{
"category": "brain",
"details": {
diff --git a/test/fixtures/goldens/doctor/hot-checks-pglite-json.json b/test/fixtures/goldens/doctor/hot-checks-pglite-json.json
index 5e67792feb..c838caa36a 100644
--- a/test/fixtures/goldens/doctor/hot-checks-pglite-json.json
+++ b/test/fixtures/goldens/doctor/hot-checks-pglite-json.json
@@ -4201,6 +4201,18 @@
"name": "derived_origin_parked",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "missing": 0,
+ "path": "<home>/.gbrain/purge-events.jsonl",
+ "recent": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No purge in the last 30 days to check against the purge mirror at <home>/.gbrain/purge-events.jsonl.",
+ "name": "purge_mirror",
+ "status": "ok"
+ },
{
"category": "brain",
"details": {
@@ -9666,6 +9678,18 @@
"name": "derived_origin_parked",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "missing": 0,
+ "path": "<home>/.gbrain/purge-events.jsonl",
+ "recent": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No purge in the last 30 days to check against the purge mirror at <home>/.gbrain/purge-events.jsonl.",
+ "name": "purge_mirror",
+ "status": "ok"
+ },
{
"category": "brain",
"details": {
diff --git a/test/fixtures/goldens/doctor/hot-checks-postgres-json.json b/test/fixtures/goldens/doctor/hot-checks-postgres-json.json
index 916abd8d7d..bb7183ec9d 100644
--- a/test/fixtures/goldens/doctor/hot-checks-postgres-json.json
+++ b/test/fixtures/goldens/doctor/hot-checks-postgres-json.json
@@ -4207,6 +4207,18 @@
"name": "derived_origin_parked",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "missing": 0,
+ "path": "<home>/.gbrain/purge-events.jsonl",
+ "recent": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No purge in the last 30 days to check against the purge mirror at <home>/.gbrain/purge-events.jsonl.",
+ "name": "purge_mirror",
+ "status": "ok"
+ },
{
"category": "brain",
"details": {
@@ -9676,6 +9688,18 @@
"name": "derived_origin_parked",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "missing": 0,
+ "path": "<home>/.gbrain/purge-events.jsonl",
+ "recent": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No purge in the last 30 days to check against the purge mirror at <home>/.gbrain/purge-events.jsonl.",
+ "name": "purge_mirror",
+ "status": "ok"
+ },
{
"category": "brain",
"details": {
diff --git a/test/fixtures/goldens/doctor/json-pglite-degraded.json b/test/fixtures/goldens/doctor/json-pglite-degraded.json
index 9aee17f45f..c88f5d7629 100644
--- a/test/fixtures/goldens/doctor/json-pglite-degraded.json
+++ b/test/fixtures/goldens/doctor/json-pglite-degraded.json
@@ -3595,6 +3595,18 @@
"name": "derived_origin_parked",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "missing": 0,
+ "path": "<home>/.gbrain/purge-events.jsonl",
+ "recent": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No purge in the last 30 days to check against the purge mirror at <home>/.gbrain/purge-events.jsonl.",
+ "name": "purge_mirror",
+ "status": "ok"
+ },
{
"category": "brain",
"details": {
diff --git a/test/fixtures/goldens/doctor/json-pglite-fresh.json b/test/fixtures/goldens/doctor/json-pglite-fresh.json
index 65e2eca0a8..605cfe97ea 100644
--- a/test/fixtures/goldens/doctor/json-pglite-fresh.json
+++ b/test/fixtures/goldens/doctor/json-pglite-fresh.json
@@ -3638,6 +3638,18 @@
"name": "derived_origin_parked",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "missing": 0,
+ "path": "<home>/.gbrain/purge-events.jsonl",
+ "recent": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No purge in the last 30 days to check against the purge mirror at <home>/.gbrain/purge-events.jsonl.",
+ "name": "purge_mirror",
+ "status": "ok"
+ },
{
"category": "brain",
"details": {
diff --git a/test/fixtures/goldens/doctor/json-postgres-fresh.json b/test/fixtures/goldens/doctor/json-postgres-fresh.json
index 298450dc3b..e4c6584f2d 100644
--- a/test/fixtures/goldens/doctor/json-postgres-fresh.json
+++ b/test/fixtures/goldens/doctor/json-postgres-fresh.json
@@ -3644,6 +3644,18 @@
"name": "derived_origin_parked",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "missing": 0,
+ "path": "<home>/.gbrain/purge-events.jsonl",
+ "recent": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No purge in the last 30 days to check against the purge mirror at <home>/.gbrain/purge-events.jsonl.",
+ "name": "purge_mirror",
+ "status": "ok"
+ },
{
"category": "brain",
"details": {
diff --git a/test/fixtures/goldens/doctor/registry-build-checks.json b/test/fixtures/goldens/doctor/registry-build-checks.json
index 53e4993d0a..fbfca6004a 100644
--- a/test/fixtures/goldens/doctor/registry-build-checks.json
+++ b/test/fixtures/goldens/doctor/registry-build-checks.json
@@ -1177,6 +1177,10 @@
"category": "brain",
"name": "derived_origin_parked"
},
+ {
+ "category": "brain",
+ "name": "purge_mirror"
+ },
{
"category": "brain",
"name": "derivation_purge_sweeps"
diff --git a/test/fixtures/goldens/exports/types.json b/test/fixtures/goldens/exports/types.json
index 07196cf176..f1a8af4b71 100644
--- a/test/fixtures/goldens/exports/types.json
+++ b/test/fixtures/goldens/exports/types.json
@@ -6875,7 +6875,7 @@
{
"kind": "class",
"members": [
- "canonical?: \"unknown\" | \"timeout\" | \"keyword_candidates_incomplete\" | \"projection_pending\" | \"rate_limited\" | \"embedding_disabled\" | \"writer_lock_unavailable\" | \"writer_pool_capacity\" | \"owner_unavailable\" | \"recovery_required\" | \"queue_capacity\" | \"write_quota_exhausted\" | \"write_quota_insufficient\" | \"write_capacity_backpressure\" | \"revision_required\" | \"revision_conflict\" | \"idempotency_conflict\" | \"source_changed\" | \"write_pending\" | \"storage_error\" | \"cancelled\" | \"permission_denied\" | \"scope_denied\" | \"invalid_params\" | \"not_found\" | \"page_not_found\" | \"page_identity_changed\" | \"write_claim_lost\" | \"request_too_large\" | \"response_too_large\" | \"writer_registration_required\" | \"writer_identity_invalid\" | \"writer_not_initialized\" | \"writer_coordinator_required\" | \"fact_already_expired\" | \"source_writeback_required\" | \"unsupported_mutation_protocol\" | \"writer_upgrade_required\" | \"writer_not_quiesced\" | \"skill_bundle_required\" | \"take_row_collision\" | \"invalid_source_uri\" | \"writer_admin_locked\" | \"connector_account_changed\" | \"connector_intent_outdated\" | \"checkpoint_validation_timeout\" | \"invalid_connector_text\" | \"connector_holds_exhausted\" | \"connector_fence_below_timeline\" | \"core_budget_exceeded\" | \"core_mark_owner_only\" | \"core_remote_edit_refused\" | \"core_delete_owner_only\" | \"timeline_rows_would_be_removed\" | \"purged_content\" | \"preparation_stalled\" | \"fence_unrenderable\" | \"page_body_segmented\" | \"page_body_rows_unreadable\" | \"revision_changed\" | \"tail_changed\" | \"write_outcome_unknown\" | \"write_interrupted\" | \"connection_inherited_read_only\" | \"owner_marker_missing\" | \"embedding_failed\" | \"bucket_not_found\" | \"database_error\" | \"unknown_transport\" | \"extraction_failed\" | \"fact_not_found\" | \"provenance_required\" | \"unavailable\" | \"budget_unsatisfiable\" | \"embedding_budget_below_worst_case\" | \"absent_from_worktree\" | \"access_command_failed\" | \"access_identity_unavailable\" | \"access_member_disabled\" | \"archive_audience_unproven\" | \"archive_gateway_unreachable\" | \"archive_gateway_auth\" | \"archive_gateway_failed\" | \"archive_owner_unset\" | \"archive_redaction_failed\" | \"archive_run_busy\" | \"autopilot_no_repo\" | \"autopilot_pglite_daemon\" | \"cycle_already_running\" | \"engine_degraded\" | \"maintenance_disabled\" | \"maintenance_owned_by_serve\" | \"schedule_unsupported\" | \"schedule_install_failed\" | \"maintenance_owned_by_autopilot\" | \"on_behalf_of_invalid_audience\" | \"on_behalf_of_unknown_member\" | \"on_behalf_of_not_permitted\" | \"actor_not_attested\" | \"actor_unlinked\" | \"on_behalf_of_in_arguments\" | \"access_policy_invalid\" | \"access_policy_pending\" | \"access_policy_widening_held\" | \"access_policy_history_unreadable\" | \"access_policy_store_mismatch\" | \"access_rule_invalid\" | \"requester_scope_required\" | \"requester_scope_unknown\" | \"credential_scope_required\" | \"access_serves_roster_empty\" | \"access_serves_key_dir_in_git\" | \"repair_propose_dir_in_git\" | \"access_member_id_retired\" | \"access_labelled_only_owner\" | \"access_labelled_only_unnamed\" | \"access_member_invalid\" | \"access_denied_test_user\" | \"access_env_missing\" | \"acknowledgment_unavailable\" | \"admin_policy_enforced\" | \"admission_paused\" | \"ambient_capture_off\" | \"ambiguous_id\" | \"ambiguous_ref\" | \"ambiguous_skill\" | \"answer_not_yours\" | \"answer_pending\" | \"answer_unavailable\" | \"api_not_enabled\" | \"append_links_fence\" | \"approval_required\" | \"atom_drain_incomplete\" | \"atom_drain_no_progress\" | \"atom_drain_provider_failure\" | \"autopilot_job_owned_by_other_brain\" | \"body_staging_expired\" | \"budget_below_recent_runtime\" | \"backup_dump_failed\" | \"backup_too_large\" | \"belief_coverage_required\" | \"belief_stale_preparation\" | \"benchmark_duplicate_task_id\" | \"benchmark_empty\" | \"benchmark_exists\" | \"benchmark_judge_llm_no_rubric\" | \"benchmark_judge_qrels_no_expected\" | \"benchmark_judge_rule_no_checks\" | \"benchmark_judge_unknown_kind\" | \"benchmark_malformed\" | \"benchmark_missing_judge\" | \"benchmark_missing_task\" | \"benchmark_missing_task_id\" | \"benchmark_not_found\" | \"benchmark_rule_check_bad_arg\" | \"benchmark_rule_check_malformed\" | \"benchmark_rule_check_unknown_op\" | \"bootstrap_cleanup_pending\" | \"bootstrap_empty\" | \"bootstrap_pending_review\" | \"bridge_ownership_conflict\" | \"valid_time_undeclared\" | \"coverage_from\" | \"coverage_gap\" | \"clock_mismatch\" | \"extract_write_lost\" | \"item_failures_systemic\" | \"known_as_of_unproven\" | \"cache_quota_exceeded\" | \"calendar_unreadable\" | \"capacity_paused\" | \"catalog_capacity_exceeded\" | \"catalog_unavailable\" | \"host_upgrade_required\" | \"change_reason_rejected\" | \"changed_since_preview\" | \"chronicle_skipped\" | \"no_pricing\" | \"checkout_not_a_source\" | \"checkpoint_missing\" | \"claim_already_used\" | \"client_behind\" | \"client_json_unreadable\" | \"client_json_wrong_type\" | \"client_not_found\" | \"client_shape_invalid\" | \"code_callees_requires_symbol\" | \"code_callers_requires_symbol\" | \"code_def_requires_symbol\" | \"code_refs_requires_symbol\" | \"code_reused\" | \"colon_slug_windows_write_through\" | \"command_failed\" | \"commit_lock_busy\" | \"commit_base_moved\" | \"commit_paths_invalid\" | \"commit_paths_skipped\" | \"commit_prestaged_paths\" | \"commit_outside_source_root\" | \"concurrent_write\" | \"config_error\" | \"confirmation_required\" | \"conflicting_source_scope\" | \"connection_lost\" | \"connector_access_denied\" | \"connector_auth_required\" | \"connector_binding_missing\" | \"connector_item_too_large\" | \"connector_response_too_large\" | \"connector_shape_drift\" | \"chunkless_pages_healable\" | \"consent_required\" | \"consent_timeout\" | \"consumers_without_heartbeat\" | \"content_repair_needs_human\" | \"content_filter_owned\" | \"content_null_bytes\" | \"content_rejected\" | \"continuation_unproven\" | \"corpus_chat_unavailable\" | \"corpus_window_backoff\" | \"corpus_window_dead_lettered\" | \"convergence_read_failed\" | \"cost_cap_exceeded\" | \"cost_preview_requires_yes\" | \"cursor_incarnation_retired\" | \"cycle_failed\" | \"d_sel_too_small\" | \"delta_cursor_upgrade_required\" | \"deleted_page_chunks_overdue\" | \"demo_graph_failed\" | \"downgrade_blocked_deferred_writes\" | \"demo_import_failed\" | \"demo_verification_failed\" | \"derived_cap_exhausted\" | \"destination_not_ready\" | \"drain_holder_stalled\" | \"drain_stalled\" | \"managed_sync_not_moving\" | \"dream_breaker_tripped\" | \"dirty_tree\" | \"doctor_alert_act\" | \"doctor_alert_unknown\" | \"doctor_tier_unavailable\" | \"effect_not_failed\" | \"embed_backfill_stalled\" | \"embedding_auth_failed\" | \"embedding_configuration\" | \"embedding_identity_unqualified\" | \"embedding_model_mismatch\" | \"embedding_plane_split\" | \"embedding_state_unknown\" | \"embedding_storage_unavailable\" | \"embedding_unavailable\" | \"embedding_unconfigured\" | \"embedding_width_mismatch\" | \"embedded_core_required\" | \"enforced_member_refused\" | \"engine_embedding_mismatch\" | \"embedded_engine_required\" | \"engine_closed\" | \"engine_host_missing\" | \"engine_scope_missing\" | \"engine_scope_required\" | \"engine_settings_invalid\" | \"engine_graduated\" | \"entity_types_unavailable\" | \"eval_suite_unwired\" | \"explicit_tasks_conflict\" | \"export_limit\" | \"extractor_identity_mismatch\" | \"fact_withdrawn\" | \"fact_rows_collide\" | \"fence_row_changed\" | \"facts_absorb_write_refused\" | \"facts_backstop_skipped\" | \"facts_drain_deferred\" | \"facts_drain_job_failed\" | \"facts_payload_expired\" | \"feedback_disabled\" | \"feedback_not_authorized\" | \"fetch_failed\" | \"file_database_drift\" | \"fix_not_writable\" | \"file_drift\" | \"file_drift_mixed_selection\" | \"file_drift_state_required\" | \"file_removed_during_scan\" | \"file_unreadable\" | \"file_wins_rewrite\" | \"file_too_large\" | \"follow_approval_required\" | \"foreign_table\" | \"forget_requires_owner\" | \"reserved_frontmatter_key\" | \"generation_fenced\" | \"generation_held\" | \"generation_live_conflict\" | \"frontmatter_slug_conflict\" | \"full_resync_required\" | \"gate_failed\" | \"ghost_mass_delete_refused\" | \"notifier_stalled\" | \"moved_pages_mass_fold_refused\" | \"origin_twin_mass_merge_refused\" | \"ghost_storage_unknown\" | \"git_branch_unborn\" | \"git_durability_not_enabled\" | \"git_checkout_required\" | \"git_worktree_link_broken\" | \"hosted_repo_refused\" | \"git_detached_head\" | \"git_index_conflict\" | \"git_index_locked\" | \"git_index_stale\" | \"git_target_pending\" | \"git_operation_in_progress\" | \"git_push_unavailable\" | \"git_target_changed\" | \"git_target_ignored\" | \"git_target_unsafe\" | \"git_unavailable\" | \"git_object_missing\" | \"git_conversion_blocked\" | \"git_conversion_delta\" | \"conversion_file_in_source_root\" | \"git_conversion_incomplete\" | \"git_home_required\" | \"git_private_commit_refused\" | \"git_protocol_required\" | \"git_lfs_not_enabled\" | \"gitlink_not_followed\" | \"graduation_drain_timeout\" | \"graduation_embedding_dimension_mismatch\" | \"graduation_foreign_host_binding\" | \"graduation_in_progress\" | \"graduation_interrupted\" | \"graduation_rollback_writes_lost\" | \"graduation_source_writer_held\" | \"graduation_split_brain\" | \"graduation_target_auth_failed\" | \"graduation_target_ddl_unreachable\" | \"graduation_target_not_empty\" | \"graduation_target_unsupported\" | \"graduation_unclassified_table\" | \"graduation_unsupported_platform\" | \"graduation_verify_failed\" | \"grant_scope\" | \"graph_incomplete\" | \"host_identity_mismatch\" | \"harness_hook_duplicates\" | \"harness_hook_unowned\" | \"head_blocked\" | \"held_failed\" | \"held_out_overlaps_benchmark\" | \"held_out_required_for_bundled\" | \"independent_principal_required\" | \"insufficient_scope\" | \"integrate_conflict\" | \"internal\" | \"internal_error\" | \"interrupted\" | \"invalid_acknowledgment\" | \"invalid_client\" | \"invalid_fence\" | \"unclosed_ambiguous_tail\" | \"tail_exposure_approval\" | \"sync_in_progress\" | \"invalid_frontmatter\" | \"invalid_grant_clock_skew\" | \"invalid_grant_revoked\" | \"invalid_grant_testing_expiry\" | \"invalid_rating\" | \"invalid_receipt\" | \"invalid_receipt_transition\" | \"invalid_request\" | \"invalid_source\" | \"invalid_source_pin\" | \"invalid_token\" | \"invalid_write_wait\" | \"ipc_path_too_long\" | \"legacy_twin_mass_merge_refused\" | \"legacy_twins_storage_unknown\" | \"legacy_job_selection_invalid\" | \"legacy_jobs_active\" | \"legacy_lock_removal_refused\" | \"link_cursor_invalid\" | \"link_dates_unattributed\" | \"local_conflict\" | \"lock_busy\" | \"loops_extraction_skipped\" | \"maintenance_backpressure\" | \"maintenance_lock_busy\" | \"managed_failure_unsettled\" | \"managed_remedy_unavailable\" | \"memory_setting_write_outcome_unknown\" | \"merge_recommended\" | \"movement_unknown\" | \"managed_sync_flag_refused\" | \"managed_pull_skipped\" | \"manual_only_skipped\" | \"membership_inactive\" | \"membership_not_found\" | \"method_not_allowed\" | \"migration_lease_lost\" | \"migration_declined\" | \"migration_failed\" | \"migration_host_work_pending\" | \"migrations_pending\" | \"migrations_running\" | \"missing_source_scope\" | \"missing_source_value\" | \"models_strict_violation\" | \"no_context_ceiling\" | \"no_refresh_token\" | \"no_routing_eval\" | \"no_skill_md\" | \"no_worker\" | \"no_worker_surface\" | \"not_a_git_repo\" | \"not_converged\" | \"not_owner_host\" | \"not_connected\" | \"no_brain\" | \"overlapping_path\" | \"page_projection_conflict\" | \"page_projection_stale\" | \"label_edit_refused\" | \"page_shrink_refused\" | \"page_too_large_for_inline\" | \"parse_error\" | \"part_splits_code_point\" | \"part_too_large\" | \"frontmatter_alias_limit\" | \"parser_regression\" | \"pasted_wrong_url\" | \"payload_too_large\" | \"path_not_utf8\" | \"pattern_claims_pending\" | \"patterns_min_interval\" | \"pending_verify\" | \"pending_writes\" | \"persistence_restart_required\" | \"pool_inflight_over_budget\" | \"deep_health_probe_failed\" | \"refresh_required\" | \"serve_self_exit_suppressed\" | \"serve_unhealthy_exit\" | \"persistence_write_stall\" | \"publish_secret_found\" | \"publish_scan_unreadable\" | \"push_unconfirmed\" | \"schema_lock_blocked\" | \"postgres_tls_required\" | \"pg_connection_recovery_failed\" | \"pg_connection_stuck\" | \"pg_pool_stalled\" | \"pg_statement_build_failed\" | \"pglite_busy\" | \"pglite_vector_index_too_large\" | \"plan_output_failed\" | \"plan_stale\" | \"port_in_use\" | \"preview_changed\" | \"profile_incompatible\" | \"projection_owner_resident\" | \"knock_on_exhausted\" | \"keyword_skipped_after_timeout\" | \"proposal_closed\" | \"proposal_expired\" | \"proposals_pending_limit\" | \"publication_pending\" | \"pull_blocked_local_changes\" | \"purge_blocked_pending_recovery\" | \"purge_lookup_timeout\" | \"purge_mirror_unwritable\" | \"question_not_found\" | \"question_owner_only\" | \"rank_entities_timeout\" | \"read_budget_exhausted\" | \"reason_purge_scan_capped\" | \"reason_purge_scan_failed\" | \"reason_scan_capped\" | \"reason_scan_failed\" | \"reason_trailer_secret\" | \"receipt_conflict\" | \"receipt_fence_changed\" | \"receipt_fence_required\" | \"receipt_identity_mismatch\" | \"pat_file_unreadable\" | \"reconcile_host_required\" | \"reconcile_read_incomplete\" | \"recovery_prior_stage_unavailable\" | \"recovery_version_unsupported\" | \"redirect_uri_mismatch\" | \"ref_not_in_answer\" | \"refresh_dirty\" | \"refresh_diverged\" | \"refresh_drain_timeout\" | \"refresh_lock_timeout\" | \"refresh_no_upstream\" | \"refresh_not_managed\" | \"refresh_not_owner\" | \"pending_import_expired\" | \"refresh_barrier_expired\" | \"refresh_source_changed\" | \"refresh_sync_timeout\" | \"relay_disabled\" | \"relay_session_expired\" | \"relay_unreachable\" | \"rename_held\" | \"rename_identity_unreadable\" | \"rename_reconcile_failed\" | \"repair_preview_timeout\" | \"request_already_submitted\" | \"requirements_changed\" | \"requires_local_engine\" | \"restart_refused\" | \"restart_unverified\" | \"runtime_build_skew\" | \"reset_upstream_refused\" | \"resume_spec_mismatch\" | \"result_too_large\" | \"revision_backfill_pending\" | \"revision_unavailable\" | \"schema_owner_required\" | \"scope_missing\" | \"search_cursor_requires_keyword\" | \"search_mode_local_only\" | \"scorecard_group_by_local_only\" | \"search_offset_over_cap\" | \"segmented_mirror_writer_gate\" | \"serve_build_drift\" | \"serve_port_in_use\" | \"serve_status_only\" | \"settle_mismatch\" | \"openclaw_not_found\" | \"setup_harness_unsupported\" | \"setup_hosted_connection\" | \"setup_owner_conflict\" | \"setup_serves_several\" | \"setup_serves_unreadable\" | \"hook_session_cwd\" | \"turn_hook_not_loaded\" | \"shared_skills_unavailable\" | \"shared_skills_unsupported\" | \"skill_asset_not_found\" | \"skill_candidate_invalid\" | \"skill_not_found\" | \"skill_catalog_mismatch\" | \"skill_catalog_too_large\" | \"skill_catalog_unknown\" | \"skill_index_incompatible\" | \"skill_index_locked\" | \"skill_index_missing\" | \"skill_index_removed\" | \"skill_resolve_invalid_input\" | \"skill_resolve_no_input\" | \"skill_resolve_timeout\" | \"skill_resolver_config_invalid\" | \"skill_resolver_config_unreadable\" | \"skill_resolver_setup_incomplete\" | \"skill_resolver_plugin_not_installed\" | \"skill_resolver_host_not_local\" | \"skill_resolver_selftest_failed\" | \"skill_resolver_pin_unknown\" | \"skill_resolver_library_missing\" | \"skill_resolver_library_mismatch\" | \"skill_resolver_library_skew\" | \"skill_resolver_worker_crash\" | \"skill_resolver_worker_stuck\" | \"skill_resolver_worker_unavailable\" | \"skill_resolver_state_missing\" | \"skill_resolver_host_load_cut\" | \"skill_resolver_internal\" | \"skill_source_invalid\" | \"skill_source_unreadable\" | \"skill_retention_capacity\" | \"skill_unavailable\" | \"skillpack_backup_corrupt\" | \"skillpack_journal_not_found\" | \"skillpack_restore_conflict\" | \"skillpack_state_locked\" | \"skillpack_update_incomplete\" | \"skillpack_workspace_locked\" | \"slug_page_mismatch\" | \"slug_root_mode_mismatch\" | \"source_binding_required\" | \"source_id_taken\" | \"source_mismatch\" | \"source_not_ready\" | \"source_profile_no_backfill\" | \"source_pages_remain\" | \"source_referenced\" | \"source_retire_superseded\" | \"source_root_conflict\" | \"source_root_unreadable\" | \"sources_not_readable\" | \"split_bad_ratio\" | \"split_unparseable\" | \"stable_channel_unresolved\" | \"stable_invalid\" | \"stable_missing\" | \"stable_unreadable\" | \"stale_unavailable\" | \"state_mismatch\" | \"store_unavailable\" | \"storage_adopt_field_refused\" | \"storage_adopt_unverified\" | \"storage_card_exists\" | \"storage_manifest_missing\" | \"storage_record_invalid\" | \"storage_record_secret\" | \"storage_revision_conflict\" | \"storage_source_managed\" | \"strict_requires_managed\" | \"sync_anchor_relative\" | \"sync_not_applicable\" | \"sync_admission_restricted\" | \"sync_deadline_stalled\" | \"sync_drain_lease_held\" | \"content_receipt_stale\" | \"sync_first_run_in_progress\" | \"sync_no_successful_sync\" | \"sync_cursor_replaced\" | \"sync_pass_superseded\" | \"sync_parser_regression\" | \"tag_too_long\" | \"target_escape\" | \"timeline_comment_markup\" | \"source_writer_restricted\" | \"guest_page_immutable\" | \"guest_page_promoted\" | \"guest_attribution_missing\" | \"guest_frontmatter_refused\" | \"guest_readers_invalid\" | \"guest_promotion_owner_only\" | \"guest_mark_immutable\" | \"guest_source_database_only\" | \"readable_by_invalid\" | \"topology_change_required\" | \"transcript_secret_exposure\" | \"transcript_source_mismatch\" | \"triage_unreliable_backoff\" | \"trust_raise_refused\" | \"two_consumers_on_host\" | \"trusted_local_only\" | \"unexpected_file_bytes\" | \"unexpected_staging_bytes\" | \"unknown_command\" | \"unknown_flag\" | \"unknown_method\" | \"unknown_source\" | \"unknown_tool\" | \"unpriced_model\" | \"unmanaged_database_path\" | \"unmanaged_path\" | \"unsupported_export_data\" | \"unsupported_path\" | \"untrack_not_db_only\" | \"upload_expired\" | \"upload_not_found\" | \"upload_protected_content\" | \"upload_too_many_parts\" | \"upstream\" | \"upstream_diverged\" | \"upstream_rewritten\" | \"upstream_unknown\" | \"vector_prewarm_extension_missing\" | \"vector_prewarm_unsupported_engine\" | \"vector_prewarm_wont_fit\" | \"verification_failed\" | \"visibility_noncanonical\" | \"visibility_not_applied\" | \"wakeshift_key_invalid\" | \"wakeshift_needs_conversation\" | \"wakeshift_not_enabled\" | \"wakeshift_refused\" | \"wakeshift_shared_brain\" | \"wakeshift_unavailable\" | \"withdrawal_capacity\" | \"withdrawal_mirror_failed\" | \"withdrawal_mirror_pending\" | \"withdrawal_provenance\" | \"write_capacity_low\" | \"write_gate_rejected\" | \"write_held\" | \"worktree_dirty\" | \"worktree_refreshing\" | \"writer_admin_intent_required\" | \"writer_admin_state_changed\" | \"writer_busy\" | \"writer_identity_publish_failed\" | \"writer_manifest_mismatch\" | \"writer_manifest_rescope_required\" | \"writer_manifest_unsafe\" | \"writer_regrant_required\" | \"writer_transfer_conflict\" | \"writer_transfer_required\" | \"wrong_account_consented\" | \"schema_pack_too_large\" | \"schema_pack_emit_mismatch\" | undefined",
+ "canonical?: \"unknown\" | \"timeout\" | \"keyword_candidates_incomplete\" | \"projection_pending\" | \"rate_limited\" | \"embedding_disabled\" | \"writer_lock_unavailable\" | \"writer_pool_capacity\" | \"owner_unavailable\" | \"recovery_required\" | \"queue_capacity\" | \"write_quota_exhausted\" | \"write_quota_insufficient\" | \"write_capacity_backpressure\" | \"revision_required\" | \"revision_conflict\" | \"idempotency_conflict\" | \"source_changed\" | \"write_pending\" | \"storage_error\" | \"cancelled\" | \"permission_denied\" | \"scope_denied\" | \"invalid_params\" | \"not_found\" | \"page_not_found\" | \"page_identity_changed\" | \"write_claim_lost\" | \"request_too_large\" | \"response_too_large\" | \"writer_registration_required\" | \"writer_identity_invalid\" | \"writer_not_initialized\" | \"writer_coordinator_required\" | \"fact_already_expired\" | \"source_writeback_required\" | \"unsupported_mutation_protocol\" | \"writer_upgrade_required\" | \"writer_not_quiesced\" | \"skill_bundle_required\" | \"take_row_collision\" | \"invalid_source_uri\" | \"writer_admin_locked\" | \"connector_account_changed\" | \"connector_intent_outdated\" | \"checkpoint_validation_timeout\" | \"invalid_connector_text\" | \"connector_holds_exhausted\" | \"connector_fence_below_timeline\" | \"core_budget_exceeded\" | \"core_mark_owner_only\" | \"core_remote_edit_refused\" | \"core_delete_owner_only\" | \"timeline_rows_would_be_removed\" | \"purged_content\" | \"preparation_stalled\" | \"fence_unrenderable\" | \"page_body_segmented\" | \"page_body_rows_unreadable\" | \"revision_changed\" | \"tail_changed\" | \"write_outcome_unknown\" | \"write_interrupted\" | \"connection_inherited_read_only\" | \"owner_marker_missing\" | \"embedding_failed\" | \"bucket_not_found\" | \"database_error\" | \"unknown_transport\" | \"extraction_failed\" | \"fact_not_found\" | \"provenance_required\" | \"unavailable\" | \"budget_unsatisfiable\" | \"embedding_budget_below_worst_case\" | \"absent_from_worktree\" | \"access_command_failed\" | \"access_identity_unavailable\" | \"access_member_disabled\" | \"archive_audience_unproven\" | \"archive_gateway_unreachable\" | \"archive_gateway_auth\" | \"archive_gateway_failed\" | \"archive_owner_unset\" | \"archive_redaction_failed\" | \"archive_run_busy\" | \"autopilot_no_repo\" | \"autopilot_pglite_daemon\" | \"cycle_already_running\" | \"engine_degraded\" | \"maintenance_disabled\" | \"maintenance_owned_by_serve\" | \"schedule_unsupported\" | \"schedule_install_failed\" | \"maintenance_owned_by_autopilot\" | \"on_behalf_of_invalid_audience\" | \"on_behalf_of_unknown_member\" | \"on_behalf_of_not_permitted\" | \"actor_not_attested\" | \"actor_unlinked\" | \"on_behalf_of_in_arguments\" | \"access_policy_invalid\" | \"access_policy_pending\" | \"access_policy_widening_held\" | \"access_policy_history_unreadable\" | \"access_policy_store_mismatch\" | \"access_rule_invalid\" | \"requester_scope_required\" | \"requester_scope_unknown\" | \"credential_scope_required\" | \"access_serves_roster_empty\" | \"access_serves_key_dir_in_git\" | \"repair_propose_dir_in_git\" | \"access_member_id_retired\" | \"access_labelled_only_owner\" | \"access_labelled_only_unnamed\" | \"access_member_invalid\" | \"access_denied_test_user\" | \"access_env_missing\" | \"acknowledgment_unavailable\" | \"admin_policy_enforced\" | \"admission_paused\" | \"ambient_capture_off\" | \"ambiguous_id\" | \"ambiguous_ref\" | \"ambiguous_skill\" | \"answer_not_yours\" | \"answer_pending\" | \"answer_unavailable\" | \"api_not_enabled\" | \"append_links_fence\" | \"approval_required\" | \"atom_drain_incomplete\" | \"atom_drain_no_progress\" | \"atom_drain_provider_failure\" | \"autopilot_job_owned_by_other_brain\" | \"body_staging_expired\" | \"budget_below_recent_runtime\" | \"backup_dump_failed\" | \"backup_too_large\" | \"belief_coverage_required\" | \"belief_stale_preparation\" | \"benchmark_duplicate_task_id\" | \"benchmark_empty\" | \"benchmark_exists\" | \"benchmark_judge_llm_no_rubric\" | \"benchmark_judge_qrels_no_expected\" | \"benchmark_judge_rule_no_checks\" | \"benchmark_judge_unknown_kind\" | \"benchmark_malformed\" | \"benchmark_missing_judge\" | \"benchmark_missing_task\" | \"benchmark_missing_task_id\" | \"benchmark_not_found\" | \"benchmark_rule_check_bad_arg\" | \"benchmark_rule_check_malformed\" | \"benchmark_rule_check_unknown_op\" | \"bootstrap_cleanup_pending\" | \"bootstrap_empty\" | \"bootstrap_pending_review\" | \"bridge_ownership_conflict\" | \"valid_time_undeclared\" | \"coverage_from\" | \"coverage_gap\" | \"clock_mismatch\" | \"extract_write_lost\" | \"item_failures_systemic\" | \"known_as_of_unproven\" | \"cache_quota_exceeded\" | \"calendar_unreadable\" | \"capacity_paused\" | \"catalog_capacity_exceeded\" | \"catalog_unavailable\" | \"host_upgrade_required\" | \"change_reason_rejected\" | \"changed_since_preview\" | \"chronicle_skipped\" | \"no_pricing\" | \"checkout_not_a_source\" | \"checkpoint_missing\" | \"claim_already_used\" | \"client_behind\" | \"client_json_unreadable\" | \"client_json_wrong_type\" | \"client_not_found\" | \"client_shape_invalid\" | \"code_callees_requires_symbol\" | \"code_callers_requires_symbol\" | \"code_def_requires_symbol\" | \"code_refs_requires_symbol\" | \"code_reused\" | \"colon_slug_windows_write_through\" | \"command_failed\" | \"commit_lock_busy\" | \"commit_base_moved\" | \"commit_paths_invalid\" | \"commit_paths_skipped\" | \"commit_prestaged_paths\" | \"commit_outside_source_root\" | \"concurrent_write\" | \"config_error\" | \"confirmation_required\" | \"conflicting_source_scope\" | \"connection_lost\" | \"connector_access_denied\" | \"connector_auth_required\" | \"connector_binding_missing\" | \"connector_item_too_large\" | \"connector_response_too_large\" | \"connector_shape_drift\" | \"chunkless_pages_healable\" | \"consent_required\" | \"consent_timeout\" | \"consumers_without_heartbeat\" | \"content_repair_needs_human\" | \"content_filter_owned\" | \"content_null_bytes\" | \"content_rejected\" | \"continuation_unproven\" | \"corpus_chat_unavailable\" | \"corpus_window_backoff\" | \"corpus_window_dead_lettered\" | \"convergence_read_failed\" | \"cost_cap_exceeded\" | \"cost_preview_requires_yes\" | \"cursor_incarnation_retired\" | \"cycle_failed\" | \"d_sel_too_small\" | \"delta_cursor_upgrade_required\" | \"deleted_page_chunks_overdue\" | \"demo_graph_failed\" | \"downgrade_blocked_deferred_writes\" | \"demo_import_failed\" | \"demo_verification_failed\" | \"derived_cap_exhausted\" | \"destination_not_ready\" | \"drain_holder_stalled\" | \"drain_stalled\" | \"managed_sync_not_moving\" | \"dream_breaker_tripped\" | \"dirty_tree\" | \"doctor_alert_act\" | \"doctor_alert_unknown\" | \"doctor_tier_unavailable\" | \"effect_not_failed\" | \"embed_backfill_stalled\" | \"embedding_auth_failed\" | \"embedding_configuration\" | \"embedding_identity_unqualified\" | \"embedding_model_mismatch\" | \"embedding_plane_split\" | \"embedding_state_unknown\" | \"embedding_storage_unavailable\" | \"embedding_unavailable\" | \"embedding_unconfigured\" | \"embedding_width_mismatch\" | \"embedded_core_required\" | \"enforced_member_refused\" | \"engine_embedding_mismatch\" | \"embedded_engine_required\" | \"engine_closed\" | \"engine_host_missing\" | \"engine_scope_missing\" | \"engine_scope_required\" | \"engine_settings_invalid\" | \"engine_graduated\" | \"entity_types_unavailable\" | \"eval_suite_unwired\" | \"explicit_tasks_conflict\" | \"export_limit\" | \"extractor_identity_mismatch\" | \"fact_withdrawn\" | \"fact_rows_collide\" | \"fence_row_changed\" | \"facts_absorb_write_refused\" | \"facts_backstop_skipped\" | \"facts_drain_deferred\" | \"facts_drain_job_failed\" | \"facts_payload_expired\" | \"feedback_disabled\" | \"feedback_not_authorized\" | \"fetch_failed\" | \"file_database_drift\" | \"fix_not_writable\" | \"file_drift\" | \"file_drift_mixed_selection\" | \"file_drift_state_required\" | \"file_removed_during_scan\" | \"file_unreadable\" | \"file_wins_rewrite\" | \"file_too_large\" | \"follow_approval_required\" | \"foreign_table\" | \"forget_requires_owner\" | \"reserved_frontmatter_key\" | \"generation_fenced\" | \"generation_held\" | \"generation_live_conflict\" | \"frontmatter_slug_conflict\" | \"full_resync_required\" | \"gate_failed\" | \"ghost_mass_delete_refused\" | \"notifier_stalled\" | \"moved_pages_mass_fold_refused\" | \"origin_twin_mass_merge_refused\" | \"ghost_storage_unknown\" | \"git_branch_unborn\" | \"git_durability_not_enabled\" | \"git_checkout_required\" | \"git_worktree_link_broken\" | \"hosted_repo_refused\" | \"git_detached_head\" | \"git_index_conflict\" | \"git_index_locked\" | \"git_index_stale\" | \"git_target_pending\" | \"git_operation_in_progress\" | \"git_push_unavailable\" | \"git_target_changed\" | \"git_target_ignored\" | \"git_target_unsafe\" | \"git_unavailable\" | \"git_object_missing\" | \"git_conversion_blocked\" | \"git_conversion_delta\" | \"conversion_file_in_source_root\" | \"git_conversion_incomplete\" | \"git_home_required\" | \"git_private_commit_refused\" | \"git_protocol_required\" | \"git_lfs_not_enabled\" | \"gitlink_not_followed\" | \"graduation_drain_timeout\" | \"graduation_embedding_dimension_mismatch\" | \"graduation_foreign_host_binding\" | \"graduation_in_progress\" | \"graduation_interrupted\" | \"graduation_rollback_writes_lost\" | \"graduation_source_writer_held\" | \"graduation_split_brain\" | \"graduation_target_auth_failed\" | \"graduation_target_ddl_unreachable\" | \"graduation_target_not_empty\" | \"graduation_target_unsupported\" | \"graduation_unclassified_table\" | \"graduation_unsupported_platform\" | \"graduation_verify_failed\" | \"grant_scope\" | \"graph_incomplete\" | \"host_identity_mismatch\" | \"harness_hook_duplicates\" | \"harness_hook_unowned\" | \"head_blocked\" | \"held_failed\" | \"held_out_overlaps_benchmark\" | \"held_out_required_for_bundled\" | \"independent_principal_required\" | \"insufficient_scope\" | \"integrate_conflict\" | \"internal\" | \"internal_error\" | \"interrupted\" | \"invalid_acknowledgment\" | \"invalid_client\" | \"invalid_fence\" | \"unclosed_ambiguous_tail\" | \"tail_exposure_approval\" | \"sync_in_progress\" | \"invalid_frontmatter\" | \"invalid_grant_clock_skew\" | \"invalid_grant_revoked\" | \"invalid_grant_testing_expiry\" | \"invalid_rating\" | \"invalid_receipt\" | \"invalid_receipt_transition\" | \"invalid_request\" | \"invalid_source\" | \"invalid_source_pin\" | \"invalid_token\" | \"invalid_write_wait\" | \"ipc_path_too_long\" | \"legacy_twin_mass_merge_refused\" | \"legacy_twins_storage_unknown\" | \"legacy_job_selection_invalid\" | \"legacy_jobs_active\" | \"legacy_lock_removal_refused\" | \"link_cursor_invalid\" | \"link_dates_unattributed\" | \"local_conflict\" | \"lock_busy\" | \"loops_extraction_skipped\" | \"maintenance_backpressure\" | \"maintenance_lock_busy\" | \"managed_failure_unsettled\" | \"managed_remedy_unavailable\" | \"memory_setting_write_outcome_unknown\" | \"merge_recommended\" | \"movement_unknown\" | \"managed_sync_flag_refused\" | \"managed_pull_skipped\" | \"manual_only_skipped\" | \"membership_inactive\" | \"membership_not_found\" | \"method_not_allowed\" | \"migration_lease_lost\" | \"migration_declined\" | \"migration_failed\" | \"migration_host_work_pending\" | \"migrations_pending\" | \"migrations_running\" | \"missing_source_scope\" | \"missing_source_value\" | \"models_strict_violation\" | \"no_context_ceiling\" | \"no_refresh_token\" | \"no_routing_eval\" | \"no_skill_md\" | \"no_worker\" | \"no_worker_surface\" | \"not_a_git_repo\" | \"not_converged\" | \"not_owner_host\" | \"not_connected\" | \"no_brain\" | \"overlapping_path\" | \"page_projection_conflict\" | \"page_projection_stale\" | \"label_edit_refused\" | \"page_shrink_refused\" | \"page_too_large_for_inline\" | \"parse_error\" | \"part_splits_code_point\" | \"part_too_large\" | \"frontmatter_alias_limit\" | \"parser_regression\" | \"pasted_wrong_url\" | \"payload_too_large\" | \"path_not_utf8\" | \"pattern_claims_pending\" | \"patterns_min_interval\" | \"pending_verify\" | \"pending_writes\" | \"persistence_restart_required\" | \"pool_inflight_over_budget\" | \"deep_health_probe_failed\" | \"refresh_required\" | \"serve_self_exit_suppressed\" | \"serve_unhealthy_exit\" | \"persistence_write_stall\" | \"publish_secret_found\" | \"publish_scan_unreadable\" | \"push_unconfirmed\" | \"schema_lock_blocked\" | \"postgres_tls_required\" | \"pg_connection_recovery_failed\" | \"pg_connection_stuck\" | \"pg_pool_stalled\" | \"pg_statement_build_failed\" | \"pglite_busy\" | \"pglite_vector_index_too_large\" | \"plan_output_failed\" | \"plan_stale\" | \"port_in_use\" | \"preview_changed\" | \"profile_incompatible\" | \"projection_owner_resident\" | \"knock_on_exhausted\" | \"keyword_skipped_after_timeout\" | \"proposal_closed\" | \"proposal_expired\" | \"proposals_pending_limit\" | \"publication_pending\" | \"pull_blocked_local_changes\" | \"purge_blocked_pending_recovery\" | \"purge_lookup_timeout\" | \"purge_mirror_unwritable\" | \"purge_replay_incomplete\" | \"question_not_found\" | \"question_owner_only\" | \"rank_entities_timeout\" | \"read_budget_exhausted\" | \"reason_purge_scan_capped\" | \"reason_purge_scan_failed\" | \"reason_scan_capped\" | \"reason_scan_failed\" | \"reason_trailer_secret\" | \"receipt_conflict\" | \"receipt_fence_changed\" | \"receipt_fence_required\" | \"receipt_identity_mismatch\" | \"pat_file_unreadable\" | \"reconcile_host_required\" | \"reconcile_read_incomplete\" | \"recovery_prior_stage_unavailable\" | \"recovery_version_unsupported\" | \"redirect_uri_mismatch\" | \"ref_not_in_answer\" | \"refresh_dirty\" | \"refresh_diverged\" | \"refresh_drain_timeout\" | \"refresh_lock_timeout\" | \"refresh_no_upstream\" | \"refresh_not_managed\" | \"refresh_not_owner\" | \"pending_import_expired\" | \"refresh_barrier_expired\" | \"refresh_source_changed\" | \"refresh_sync_timeout\" | \"relay_disabled\" | \"relay_session_expired\" | \"relay_unreachable\" | \"rename_held\" | \"rename_identity_unreadable\" | \"rename_reconcile_failed\" | \"repair_preview_timeout\" | \"request_already_submitted\" | \"requirements_changed\" | \"requires_local_engine\" | \"restart_refused\" | \"restart_unverified\" | \"runtime_build_skew\" | \"reset_upstream_refused\" | \"resume_spec_mismatch\" | \"result_too_large\" | \"revision_backfill_pending\" | \"revision_unavailable\" | \"schema_owner_required\" | \"scope_missing\" | \"search_cursor_requires_keyword\" | \"search_mode_local_only\" | \"scorecard_group_by_local_only\" | \"search_offset_over_cap\" | \"segmented_mirror_writer_gate\" | \"serve_build_drift\" | \"serve_port_in_use\" | \"serve_status_only\" | \"settle_mismatch\" | \"openclaw_not_found\" | \"setup_harness_unsupported\" | \"setup_hosted_connection\" | \"setup_owner_conflict\" | \"setup_serves_several\" | \"setup_serves_unreadable\" | \"hook_session_cwd\" | \"turn_hook_not_loaded\" | \"shared_skills_unavailable\" | \"shared_skills_unsupported\" | \"skill_asset_not_found\" | \"skill_candidate_invalid\" | \"skill_not_found\" | \"skill_catalog_mismatch\" | \"skill_catalog_too_large\" | \"skill_catalog_unknown\" | \"skill_index_incompatible\" | \"skill_index_locked\" | \"skill_index_missing\" | \"skill_index_removed\" | \"skill_resolve_invalid_input\" | \"skill_resolve_no_input\" | \"skill_resolve_timeout\" | \"skill_resolver_config_invalid\" | \"skill_resolver_config_unreadable\" | \"skill_resolver_setup_incomplete\" | \"skill_resolver_plugin_not_installed\" | \"skill_resolver_host_not_local\" | \"skill_resolver_selftest_failed\" | \"skill_resolver_pin_unknown\" | \"skill_resolver_library_missing\" | \"skill_resolver_library_mismatch\" | \"skill_resolver_library_skew\" | \"skill_resolver_worker_crash\" | \"skill_resolver_worker_stuck\" | \"skill_resolver_worker_unavailable\" | \"skill_resolver_state_missing\" | \"skill_resolver_host_load_cut\" | \"skill_resolver_internal\" | \"skill_source_invalid\" | \"skill_source_unreadable\" | \"skill_retention_capacity\" | \"skill_unavailable\" | \"skillpack_backup_corrupt\" | \"skillpack_journal_not_found\" | \"skillpack_restore_conflict\" | \"skillpack_state_locked\" | \"skillpack_update_incomplete\" | \"skillpack_workspace_locked\" | \"slug_page_mismatch\" | \"slug_root_mode_mismatch\" | \"source_binding_required\" | \"source_id_taken\" | \"source_mismatch\" | \"source_not_ready\" | \"source_profile_no_backfill\" | \"source_pages_remain\" | \"source_referenced\" | \"source_retire_superseded\" | \"source_root_conflict\" | \"source_root_unreadable\" | \"sources_not_readable\" | \"split_bad_ratio\" | \"split_unparseable\" | \"stable_channel_unresolved\" | \"stable_invalid\" | \"stable_missing\" | \"stable_unreadable\" | \"stale_unavailable\" | \"state_mismatch\" | \"store_unavailable\" | \"storage_adopt_field_refused\" | \"storage_adopt_unverified\" | \"storage_card_exists\" | \"storage_manifest_missing\" | \"storage_record_invalid\" | \"storage_record_secret\" | \"storage_revision_conflict\" | \"storage_source_managed\" | \"strict_requires_managed\" | \"sync_anchor_relative\" | \"sync_not_applicable\" | \"sync_admission_restricted\" | \"sync_deadline_stalled\" | \"sync_drain_lease_held\" | \"content_receipt_stale\" | \"sync_first_run_in_progress\" | \"sync_no_successful_sync\" | \"sync_cursor_replaced\" | \"sync_pass_superseded\" | \"sync_parser_regression\" | \"tag_too_long\" | \"target_escape\" | \"timeline_comment_markup\" | \"source_writer_restricted\" | \"guest_page_immutable\" | \"guest_page_promoted\" | \"guest_attribution_missing\" | \"guest_frontmatter_refused\" | \"guest_readers_invalid\" | \"guest_promotion_owner_only\" | \"guest_mark_immutable\" | \"guest_source_database_only\" | \"readable_by_invalid\" | \"topology_change_required\" | \"transcript_secret_exposure\" | \"transcript_source_mismatch\" | \"triage_unreliable_backoff\" | \"trust_raise_refused\" | \"two_consumers_on_host\" | \"trusted_local_only\" | \"unexpected_file_bytes\" | \"unexpected_staging_bytes\" | \"unknown_command\" | \"unknown_flag\" | \"unknown_method\" | \"unknown_source\" | \"unknown_tool\" | \"unpriced_model\" | \"unmanaged_database_path\" | \"unmanaged_path\" | \"unsupported_export_data\" | \"unsupported_path\" | \"untrack_not_db_only\" | \"upload_expired\" | \"upload_not_found\" | \"upload_protected_content\" | \"upload_too_many_parts\" | \"upstream\" | \"upstream_diverged\" | \"upstream_rewritten\" | \"upstream_unknown\" | \"vector_prewarm_extension_missing\" | \"vector_prewarm_unsupported_engine\" | \"vector_prewarm_wont_fit\" | \"verification_failed\" | \"visibility_noncanonical\" | \"visibility_not_applied\" | \"wakeshift_key_invalid\" | \"wakeshift_needs_conversation\" | \"wakeshift_not_enabled\" | \"wakeshift_refused\" | \"wakeshift_shared_brain\" | \"wakeshift_unavailable\" | \"withdrawal_capacity\" | \"withdrawal_mirror_failed\" | \"withdrawal_mirror_pending\" | \"withdrawal_provenance\" | \"write_capacity_low\" | \"write_gate_rejected\" | \"write_held\" | \"worktree_dirty\" | \"worktree_refreshing\" | \"writer_admin_intent_required\" | \"writer_admin_state_changed\" | \"writer_busy\" | \"writer_identity_publish_failed\" | \"writer_manifest_mismatch\" | \"writer_manifest_rescope_required\" | \"writer_manifest_unsafe\" | \"writer_regrant_required\" | \"writer_transfer_conflict\" | \"writer_transfer_required\" | \"wrong_account_consented\" | \"schema_pack_too_large\" | \"schema_pack_emit_mismatch\" | undefined",
"canonicalCode: string",
"cause?: unknown",
"code: ErrorCode",
diff --git a/test/fixtures/goldens/sql-text/_driver.json b/test/fixtures/goldens/sql-text/_driver.json
index 9e804801f0..092fa3cc74 100644
--- a/test/fixtures/goldens/sql-text/_driver.json
+++ b/test/fixtures/goldens/sql-text/_driver.json
@@ -4050,7 +4050,7 @@
"via": "unsafe"
},
{
- "paramCount": 1,
+ "paramCount": 2,
"unsafeOptions": {
"cancelFence": false,
"prepare": true,
@@ -4070,7 +4070,7 @@
"via": "unsafe"
},
{
- "paramCount": 2,
+ "paramCount": 3,
"unsafeOptions": {
"cancelFence": false,
"prepare": true,
diff --git a/test/fixtures/goldens/sql-text/pages.json b/test/fixtures/goldens/sql-text/pages.json
index 820b3af588..c9ad0a873f 100644
--- a/test/fixtures/goldens/sql-text/pages.json
+++ b/test/fixtures/goldens/sql-text/pages.json
@@ -1373,10 +1373,11 @@
{
"lane": "tx",
"params": [
+ "string",
"string"
],
- "sql": "UPDATE pages SET deleted_at = NULL, updated_at = now() WHERE slug = $1 AND deleted_at IS NOT NULL RETURNING slug",
- "textSha256": "e657e4a81c518ed0669a4f80ce47e3114b42aaa4aa13667cdc35bb516e4d37cd"
+ "sql": "WITH restored AS ( UPDATE pages SET deleted_at = NULL, updated_at = now() WHERE slug = $1 AND deleted_at IS NOT NULL RETURNING id, slug ), owed AS ( DELETE FROM op_checkpoints WHERE op = $2 AND fingerprint IN (SELECT id::text FROM restored) ) SELECT slug FROM restored",
+ "textSha256": "0ceeb1e93750c78a5751dc0d5c8218a4527921c7f119c4ce3b4897f7a718c6ca"
},
{
"event": "commit",
@@ -1401,11 +1402,12 @@
{
"lane": "tx",
"params": [
+ "string",
"string",
"string"
],
- "sql": "UPDATE pages SET deleted_at = NULL, updated_at = now() WHERE slug = $1 AND deleted_at IS NOT NULL AND source_id = $2 RETURNING slug",
- "textSha256": "d36ab3e3d4968ce3e210fbbc3a1e9247e4d6065537b380001da3285a3dc6054b"
+ "sql": "WITH restored AS ( UPDATE pages SET deleted_at = NULL, updated_at = now() WHERE slug = $1 AND deleted_at IS NOT NULL AND source_id = $2 RETURNING id, slug ), owed AS ( DELETE FROM op_checkpoints WHERE op = $3 AND fingerprint IN (SELECT id::text FROM restored) ) SELECT slug FROM restored",
+ "textSha256": "876b5f733f74390fe821c0bc9c2251b3c904c43bc30ef0268a59756ff746cc23"
},
{
"event": "commit",
diff --git a/test/page-row-deleters-guard-6712.test.ts b/test/page-row-deleters-guard-6712.test.ts
index 0acc7ed614..e7c0f45766 100644
--- a/test/page-row-deleters-guard-6712.test.ts
+++ b/test/page-row-deleters-guard-6712.test.ts
@@ -20,8 +20,7 @@ const SEGMENTS = /(?<!function )\bdeletePageWithSegments\(/g;
* `writeOriginDeletedMarker(` within three lines.
*/
const MARKED: Record<string, number> = {
- 'src/core/persistence/page-purge.ts': 1, // delete_page --purge, manual and retention (the coordinator's purge transaction)
- 'src/core/belief/purge-mirror.ts': 1, // the restore-time purge replay
+ 'src/core/persistence/page-purge.ts': 1, // deletePurgedPage: delete_page --purge, manual and retention, and the restore-time purge replay (#9469)
};
/** Reviewed callers a managed Git source never reaches, with the number of matches each file may hold. */