Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Label derived pages private before their private origin's row is removed

garrytan6a3d31ceb071
diff --git a/docs/guides/error-codes.md b/docs/guides/error-codes.md
index 562b3e5061..8078d225f6 100644
--- a/docs/guides/error-codes.md
+++ b/docs/guides/error-codes.md
@@ -1376,7 +1376,7 @@ More: [docs/ENGINES.md#serve-deep-health](../../docs/ENGINES.md#serve-deep-healt
| Meaning | Why | Next step | Who acts · consent | Verify | Exit | Retryable |
|---|---|---|---|---|---|---|
-| Chunks of pages soft-deleted longer than the purge window (72h), which the purge would remove, are still stored. | Search never reads them; the purge lifecycle (`gbrain pages purge-deleted`, the autopilot purge phase) hard-deletes such pages and their chunks. Pages its holds keep (a derived retirement inside generations.restore_days, a restorable atom, an open rename-identity pair, an archived source) are not counted. The purge is brain-wide and irreversible, so it needs the user. | Preview with `gbrain pages purge-deleted --dry-run --json`, then run `gbrain pages purge-deleted --yes` once the user agrees. Run: gbrain pages purge-deleted --dry-run --json | agent | `gbrain doctor --only embedding_state --json` | 1 | no |
+| Chunks of pages soft-deleted longer than the purge window (72h), which the purge would remove, are still stored. | Search never reads them; the purge lifecycle (`gbrain pages purge-deleted`, the autopilot purge phase) hard-deletes such pages and their chunks. Pages its holds keep (a derived retirement inside generations.restore_days, a restorable atom, an open rename-identity pair, a parked derived-page origin, an archived source) are not counted. The purge is brain-wide and irreversible, so it needs the user. | Preview with `gbrain pages purge-deleted --dry-run --json`, then run `gbrain pages purge-deleted --yes` once the user agrees. Run: gbrain pages purge-deleted --dry-run --json | agent | `gbrain doctor --only embedding_state --json` | 1 | no |
More: [docs/guides/troubleshooting.md#embedding-state](../../docs/guides/troubleshooting.md#embedding-state)
diff --git a/scripts/check-engine-sql-dynamic.ts b/scripts/check-engine-sql-dynamic.ts
index fc6fbfb75f..8509e32b4d 100644
--- a/scripts/check-engine-sql-dynamic.ts
+++ b/scripts/check-engine-sql-dynamic.ts
@@ -94,6 +94,7 @@ const VETTED_BUILDERS: Record<string, string> = {
stagingNeedsTextSql: 'src/core/page-body/schema.ts: constant live-staging text-reference EXISTS predicate over a caller text alias; no values (Wave B purge)',
atomRetirementPurgeHold: 'src/core/persistence/atom-retirement-hold.ts: constant #7013 restorable-atom purge-hold predicate over a caller pages alias; no values',
derivedRetirementPurgeHold: 'src/core/persistence/derived-retirement-hold.ts: constant GBRA-161 derived-retirement purge-hold predicate over a caller pages alias; no values',
+ derivedOriginPurgeHold: 'src/core/persistence/derived-origin-hold.ts: constant GST-2689 parked-origin purge-hold predicate over a caller pages alias; no values',
rankingLinkSql: 'src/core/link-ranking-policy.ts: constant non-ranking link-type predicate (code-constant type names) over a caller alias',
retiredPageSql: 'src/core/entities/resolve.ts: constant retired-page frontmatter predicate over a caller alias; no values',
vectorLiteralSql: 'src/core/engine-sql/facts.ts: master\'s inlined vector literal; toPgVectorLiteral output (numbers joined by commas) + a ::vector/::halfvec constant',
diff --git a/scripts/page-body-readers-baseline.tsv b/scripts/page-body-readers-baseline.tsv
index 132526bd8d..0b5e0d4c91 100644
--- a/scripts/page-body-readers-baseline.tsv
+++ b/scripts/page-body-readers-baseline.tsv
@@ -579,6 +579,7 @@ src/core/repair/lost-fence-rows.ts plannedPage call:serializePageToMarkdown SEGW
src/core/repair/lost-fence-rows.ts plannedPage field SEGW materializedSnapshot within the single-string ceiling; over it counts as fence_unreadable (segmentedOverCeiling, checked before the read)
src/core/repair/lost-fence-rows.ts scanLostFenceRows field SEGW materializedSnapshot within the single-string ceiling; over it counts as fence_unreadable (segmentedOverCeiling, checked before the read)
src/core/repair/lost-fence-rows.ts scanLostFenceRows sql INLINE page_versions rows that hold a body copy (compiled_truth IS NOT NULL); a segmented version has none and is not read
+src/core/repair/origin-carry.ts stamp call:serializePageToMarkdown SEGW a segmented page fails before the read (segmented_page) and parks its origin
src/core/repair/orphan-children.ts PROBE_SQL sql META
src/core/repair/overwritten-edits.ts previewEdit call:serializePageToMarkdown SEGW only high-confidence edits (live page and version both inline at scan time) are previewed; a page whose revision moved since the scan is skipped before it is serialized
src/core/repair/overwritten-edits.ts versionMarkdown call:serializePageToMarkdown SEGW inline versions only: a segmented version has no lost_digest and is never previewed; the restore goes through revert_version, which refuses a segmented page or version
diff --git a/src/commands/doctor/checks/embedding-state.ts b/src/commands/doctor/checks/embedding-state.ts
index a1ef8e6767..e926fbe0a0 100644
--- a/src/commands/doctor/checks/embedding-state.ts
+++ b/src/commands/doctor/checks/embedding-state.ts
@@ -48,7 +48,7 @@ async function runEmbeddingState(ctx: DoctorContext): Promise<Check[]> {
if ((counts.quarantined ?? 0) > 0) notes.push('Quarantined pages are reviewed under quarantined_pages.');
if ((counts.keyword_only ?? 0) > 0) notes.push('Keyword-only chunks sit on embed_skip pages by design (split the page or approve a higher content_sanity.bytes_block; `gbrain pages unsearchable --state keyword_only` lists them).');
if (deleted.within_window > 0) notes.push(`${deleted.within_window} chunk(s) on pages soft-deleted within ${deleted.window_hours}h wait for the purge lifecycle.`);
- if (deleted.held > 0) notes.push(`${deleted.held} chunk(s) on pages soft-deleted more than ${deleted.window_hours}h ago are held from the purge by design (a derived retirement inside generations.restore_days, a restorable atom, an open rename-identity pair or an archived source).`);
+ if (deleted.held > 0) notes.push(`${deleted.held} chunk(s) on pages soft-deleted more than ${deleted.window_hours}h ago are held from the purge by design (a derived retirement inside generations.restore_days, a restorable atom, an open rename-identity pair, a parked derived-page origin or an archived source).`);
if (deleted.purgeable > 0 || healable > 0) {
const warnings: string[] = [];
if (deleted.purgeable > 0) warnings.push(`${deleted.purgeable} chunk(s) belong to pages soft-deleted more than ${deleted.window_hours}h ago; \`gbrain pages purge-deleted\` removes them (preview with --dry-run --json; it is brain-wide and irreversible, so ask the user first).`);
diff --git a/src/commands/doctor/checks/purge-parks.ts b/src/commands/doctor/checks/purge-parks.ts
index 4865b2fbee..cbccd81806 100644
--- a/src/commands/doctor/checks/purge-parks.ts
+++ b/src/commands/doctor/checks/purge-parks.ts
@@ -8,7 +8,8 @@
import type { Check } from '../../doctor.ts';
import type { BrainEngine } from '../../../core/engine.ts';
import { isParked, PURGE_PARK_LIMIT, readPurgeParks } from '../../../core/persistence/purge-parks.ts';
-import { checkError, doctorVerify } from '../check-fix.ts';
+import { readDerivedOriginParks } from '../../../core/persistence/derived-origin-hold.ts';
+import { agentFix, checkError, doctorVerify } from '../check-fix.ts';
import { connectedEngine, type DoctorContext, type DoctorEntry } from '../context.ts';
const NAME = 'purge_parked_pages';
@@ -42,3 +43,38 @@ async function runPurgeParkedPages(ctx: DoctorContext): Promise<Check[]> {
}
export const purgeParkedPagesEntry: DoctorEntry = { name: 'purge_parked_pages', emits: ['purge_parked_pages'], run: runPurgeParkedPages };
+
+/**
+ * GST-2689: doctor `derived_origin_parked`. A soft-deleted page that is the only privacy of a page derived from it is kept
+ * from every purge while that page could not be labelled private (persistence/derived-origin-hold.ts). Each is named
+ * with the page that blocks it and the reason; the fix previews the label on the first blocking page.
+ */
+const ORIGIN_NAME = 'derived_origin_parked';
+
+export async function checkDerivedOriginParked(engine: BrainEngine): Promise<Check> {
+ let parks;
+ try {
+ parks = await readDerivedOriginParks(engine);
+ } catch (error) {
+ return checkError(ORIGIN_NAME, 'read the parked derived-page origins', error);
+ }
+ if (!parks.length) return { name: ORIGIN_NAME, status: 'ok', message: 'No soft-deleted page is kept from the purge to keep a page derived from it private.', details: { parked: 0, origins: [] } };
+ const origins = parks.slice(0, SAMPLE).map(park => ({ source_id: park.source_id, slug: park.slug, blocking_slug: park.blocking_slug, code: park.code, reason: park.reason, parked_at: park.parked_at }));
+ const listed = origins.map(origin => `${origin.source_id}/${origin.slug} (blocked by ${origin.blocking_slug ?? 'a derived page that could not be read'}: ${origin.code ?? 'error'}: ${origin.reason})`).join(', ');
+ const first = origins.find(origin => origin.blocking_slug);
+ return {
+ name: ORIGIN_NAME, status: 'warn', details: { parked: parks.length, truncated: parks.length > origins.length, origins },
+ message: `${parks.length} soft-deleted page(s) are kept from the purge, because a page derived from each reads as private only through it and could not be labelled private: ${listed}`
+ + `${parks.length > origins.length ? ` (first ${origins.length} of ${parks.length} shown)` : ''}. Fix the cause each reason names, or give the blocking page its own visibility label; the next purge labels the rest and purges the origin.`,
+ ...(first ? { fix: agentFix(['gbrain', 'repair', 'visibility', '--source', first.source_id, '--slug', first.blocking_slug!],
+ 'Previews giving the blocking page its own visibility label; nothing is written.', ORIGIN_NAME) } : {}),
+ };
+}
+
+async function runDerivedOriginParked(ctx: DoctorContext): Promise<Check[]> {
+ const checks: Check[] = [];
+ checks.push(await checkDerivedOriginParked(connectedEngine(ctx)));
+ return checks;
+}
+
+export const derivedOriginParkedEntry: DoctorEntry = { name: 'derived_origin_parked', emits: ['derived_origin_parked'], run: runDerivedOriginParked };
diff --git a/src/commands/doctor/registry.ts b/src/commands/doctor/registry.ts
index ebdf124bcb..77277b5323 100644
--- a/src/commands/doctor/registry.ts
+++ b/src/commands/doctor/registry.ts
@@ -162,7 +162,7 @@ import { slugPathMismatchEntry } from './checks/slug-path-mismatch.ts';
import { renameIdentityDowngradesEntry } from './checks/rename-identity-downgrades.ts';
import { unindexedFilesEntry } from './checks/unindexed-files.ts';
import { syncLandingGapsEntry } from './checks/landing-gaps.ts';
-import { purgeParkedPagesEntry } from './checks/purge-parks.ts';
+import { derivedOriginParkedEntry, purgeParkedPagesEntry } from './checks/purge-parks.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';
@@ -339,6 +339,7 @@ export const DOCTOR_CHECK_REGISTRY: readonly DoctorEntry[] = [
slugPathMismatchEntry,
renameIdentityDowngradesEntry,
purgeParkedPagesEntry,
+ derivedOriginParkedEntry,
derivationPurgeSweepsEntry,
missingRecordedFilesEntry,
uncommittedSyncDeletionsEntry,
diff --git a/src/core/doctor-categories.ts b/src/core/doctor-categories.ts
index 473abd0298..55287e3361 100644
--- a/src/core/doctor-categories.ts
+++ b/src/core/doctor-categories.ts
@@ -217,6 +217,8 @@ export const BRAIN_CHECK_NAMES: ReadonlySet<string> = new Set([
'parked_effects',
// #6741: managed purges parked after repeated failures (doctor/checks/purge-parks.ts).
'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',
// 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 fb6d0ef84a..39dc6492eb 100644
--- a/src/core/engine-sql/pages.ts
+++ b/src/core/engine-sql/pages.ts
@@ -28,6 +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 { jsonbParam, type SqlExecutor } from './executor.ts';
import type { LegacyUnscopedRead, ScopedRead } from './brands.ts';
import type { ScopedReadRunner } from './cjk-search.ts';
@@ -341,7 +342,8 @@ export async function purgeDeletedPages(
AND NOT EXISTS (SELECT 1 FROM op_checkpoints dg WHERE dg.op = 'sync-identity-downgrade' AND dg.fingerprint = pages.id::text
AND dg.completed_keys->0->>'disposition' IS NULL AND pages.deleted_at = (dg.completed_keys->0->>'old_deleted_at')::timestamptz)
AND ${trustedSql(atomRetirementPurgeHold('pages'))}
- AND ${trustedSql(derivedRetirementPurgeHold('pages'))}${excluded}
+ AND ${trustedSql(derivedRetirementPurgeHold('pages'))}
+ AND ${trustedSql(derivedOriginPurgeHold('pages'))}${excluded}
ORDER BY deleted_at ASC, slug ASC
`);
const pages = rows.map((r) => ({
@@ -366,7 +368,9 @@ export async function purgeDeletedPages(
-- #7013: a restorable re-extraction retirement waits out its hold (atomRetirementPurgeHold).
AND ${trustedSql(atomRetirementPurgeHold('pages'))}
-- GBRA-161: every derived retirement waits out its hold (derivedRetirementPurgeHold).
- AND ${trustedSql(derivedRetirementPurgeHold('pages'))}${excluded}
+ AND ${trustedSql(derivedRetirementPurgeHold('pages'))}
+ -- GST-2689: an origin whose derived pages could not be labelled private stays parked (derivedOriginPurgeHold).
+ AND ${trustedSql(derivedOriginPurgeHold('pages'))}${excluded}
RETURNING slug, source_id, deleted_at::text AS deleted_at
`);
await expireGonePageFacts(tx, rows);
diff --git a/src/core/error-registry.ts b/src/core/error-registry.ts
index 6c5666ba7b..3ecc6935b7 100644
--- a/src/core/error-registry.ts
+++ b/src/core/error-registry.ts
@@ -283,7 +283,7 @@ export const CODES = {
suggestion: "Repeat the delta call with the cursor from fix (next_cursor.cursor) instead of since/since_slug, and keep passing next_cursor.cursor afterwards.",
docs: 'docs/guides/ambient-recall.md#replay-after-a-degraded-wake',
},
- deleted_page_chunks_overdue: { class: 'consent', summary: "Chunks of pages soft-deleted longer than the purge window (72h), which the purge would remove, are still stored.", why: 'Search never reads them; the purge lifecycle (`gbrain pages purge-deleted`, the autopilot purge phase) hard-deletes such pages and their chunks. Pages its holds keep (a derived retirement inside generations.restore_days, a restorable atom, an open rename-identity pair, an archived source) are not counted. The purge is brain-wide and irreversible, so it needs the user.', docs: 'docs/guides/troubleshooting.md#embedding-state', suggestion: 'Preview with `gbrain pages purge-deleted --dry-run --json`, then run `gbrain pages purge-deleted --yes` once the user agrees.', fix: { argv: ['gbrain', 'pages', 'purge-deleted', '--dry-run', '--json'], consent: [], actor: 'agent', why: 'Lists the soft-deleted pages the purge would remove; changes nothing.', requires_exclusive: false, verify: { argv: ['gbrain', 'doctor', '--only', 'embedding_state', '--json'] } } },
+ deleted_page_chunks_overdue: { class: 'consent', summary: "Chunks of pages soft-deleted longer than the purge window (72h), which the purge would remove, are still stored.", why: 'Search never reads them; the purge lifecycle (`gbrain pages purge-deleted`, the autopilot purge phase) hard-deletes such pages and their chunks. Pages its holds keep (a derived retirement inside generations.restore_days, a restorable atom, an open rename-identity pair, a parked derived-page origin, an archived source) are not counted. The purge is brain-wide and irreversible, so it needs the user.', docs: 'docs/guides/troubleshooting.md#embedding-state', suggestion: 'Preview with `gbrain pages purge-deleted --dry-run --json`, then run `gbrain pages purge-deleted --yes` once the user agrees.', fix: { argv: ['gbrain', 'pages', 'purge-deleted', '--dry-run', '--json'], consent: [], actor: 'agent', why: 'Lists the soft-deleted pages the purge would remove; changes nothing.', requires_exclusive: false, verify: { argv: ['gbrain', 'doctor', '--only', 'embedding_state', '--json'] } } },
demo_graph_failed: { class: 'server', summary: "The fictional graph did not verify." },
downgrade_blocked_deferred_writes: { class: 'consent', actor: 'host_admin', exit: 3, summary: "Writes deferred behind a worktree refresh are still waiting, and an older gbrain cannot drain them.", why: "A release before the refresh write barrier never claims a deferred write, so after a downgrade those writes wait until the next upgrade. Safety does not depend on this check; liveness does. A released write publishes only when a gbrain process runs the write consumer on the worktree's owner host; polling its receipt does not publish it.", docs: 'docs/guides/write-refusals.md#deferred-writes', suggestion: 'Turn the barrier off (gbrain config set persistence.refresh_barrier false); wait until gbrain upgrade --check-downgrade exits 0, then downgrade.' },
demo_import_failed: { class: 'server', summary: "The fictional page did not import successfully." },
diff --git a/src/core/op-checkpoint.ts b/src/core/op-checkpoint.ts
index 29bdcecce6..e51aa55bf3 100644
--- a/src/core/op-checkpoint.ts
+++ b/src/core/op-checkpoint.ts
@@ -523,6 +523,7 @@ export async function purgeStaleCheckpoints(
AND NOT (op='${SYNC_DELETE_TOMBSTONE_OP}' AND EXISTS (
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 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/operation-manifest.generated.ts b/src/core/operation-manifest.generated.ts
index e6737932ad..67e6773c03 100644
--- a/src/core/operation-manifest.generated.ts
+++ b/src/core/operation-manifest.generated.ts
@@ -548,7 +548,7 @@ export const OPERATION_MANIFEST: OperationMeta[] = [
"name": "delete_page",
"idempotent": true,
"outputRedaction": "no_stored_text",
- "description": "Soft-delete a page and remove its markdown file from the source working tree (the source local_path, or sync.repo_path when the source has none). File removal is skipped when sync.write_through is off; the committed receipt reports the persistence mode and write_through outcome. Read the page revision first and pass expected_revision; retain request_id for replay. The row is hidden from search and from get_page/list_pages, but is recoverable via restore_page within 72h, which re-creates the file. The autopilot purge phase hard-deletes after the recovery window. Pass include_deleted: true to get_page to verify the soft-delete landed. A page whose file another live page of the source records is hidden without touching that file (receipt write_through.skipped: shared_origin): the file and the surviving page remain, nothing is erased, and the warning names gbrain repair file-drift --prefer file, which imports the file's current content into the surviving page (sync holds that page file_drift rather than overwrite it). purge: true is trusted-local CLI only and removes the row, chunks, links and raw data immediately after its recorded markdown artifact is removed. Purge uses the same revision, request_id and recovery protocol, including for existing tombstones. A removal failure preserves the prior row and never reports purge success; repair the artifact and submit a new request_id. A purge of a page whose file is shared refuses and names the soft delete. A committed purge warns that git history, synced copies, exports and derived rows may retain content; rotate exposed credentials.",
+ "description": "Soft-delete a page and remove its markdown file from the source working tree (the source local_path, or sync.repo_path when the source has none). File removal is skipped when sync.write_through is off; the committed receipt reports the persistence mode and write_through outcome. Read the page revision first and pass expected_revision; retain request_id for replay. The row is hidden from search and from get_page/list_pages, but is recoverable via restore_page within 72h, which re-creates the file. The autopilot purge phase hard-deletes after the recovery window. Pass include_deleted: true to get_page to verify the soft-delete landed. A page whose file another live page of the source records is hidden without touching that file (receipt write_through.skipped: shared_origin): the file and the surviving page remain, nothing is erased, and the warning names gbrain repair file-drift --prefer file, which imports the file's current content into the surviving page (sync holds that page file_drift rather than overwrite it). purge: true is trusted-local CLI only and removes the row, chunks, links and raw data immediately after its recorded markdown artifact is removed. Purge uses the same revision, request_id and recovery protocol, including for existing tombstones. A removal failure preserves the prior row and never reports purge success; repair the artifact and submit a new request_id. A purge of a page whose file is shared refuses and names the soft delete. A committed purge warns that git history, synced copies, exports and derived rows may retain content; rotate exposed credentials. Before the row is removed, pages that are private only through this page get their own visibility: private; a purge that cannot label them soft-deletes the page instead and returns purge_held (doctor derived_origin_parked).",
"params": {
"source_id": {
"type": "string",
diff --git a/src/core/ops/pages.ts b/src/core/ops/pages.ts
index 9e9407f80e..13af547807 100644
--- a/src/core/ops/pages.ts
+++ b/src/core/ops/pages.ts
@@ -432,7 +432,7 @@ const delete_page: Operation = {
name: 'delete_page',
idempotent: true,
outputRedaction: 'no_stored_text',
- description: 'Soft-delete a page and remove its markdown file from the source working tree (the source local_path, or sync.repo_path when the source has none). File removal is skipped when sync.write_through is off; the committed receipt reports the persistence mode and write_through outcome. Read the page revision first and pass expected_revision; retain request_id for replay. The row is hidden from search and from get_page/list_pages, but is recoverable via restore_page within 72h, which re-creates the file. The autopilot purge phase hard-deletes after the recovery window. Pass include_deleted: true to get_page to verify the soft-delete landed. A page whose file another live page of the source records is hidden without touching that file (receipt write_through.skipped: shared_origin): the file and the surviving page remain, nothing is erased, and the warning names gbrain repair file-drift --prefer file, which imports the file\'s current content into the surviving page (sync holds that page file_drift rather than overwrite it). purge: true is trusted-local CLI only and removes the row, chunks, links and raw data immediately after its recorded markdown artifact is removed. Purge uses the same revision, request_id and recovery protocol, including for existing tombstones. A removal failure preserves the prior row and never reports purge success; repair the artifact and submit a new request_id. A purge of a page whose file is shared refuses and names the soft delete. A committed purge warns that git history, synced copies, exports and derived rows may retain content; rotate exposed credentials.',
+ description: 'Soft-delete a page and remove its markdown file from the source working tree (the source local_path, or sync.repo_path when the source has none). File removal is skipped when sync.write_through is off; the committed receipt reports the persistence mode and write_through outcome. Read the page revision first and pass expected_revision; retain request_id for replay. The row is hidden from search and from get_page/list_pages, but is recoverable via restore_page within 72h, which re-creates the file. The autopilot purge phase hard-deletes after the recovery window. Pass include_deleted: true to get_page to verify the soft-delete landed. A page whose file another live page of the source records is hidden without touching that file (receipt write_through.skipped: shared_origin): the file and the surviving page remain, nothing is erased, and the warning names gbrain repair file-drift --prefer file, which imports the file\'s current content into the surviving page (sync holds that page file_drift rather than overwrite it). purge: true is trusted-local CLI only and removes the row, chunks, links and raw data immediately after its recorded markdown artifact is removed. Purge uses the same revision, request_id and recovery protocol, including for existing tombstones. A removal failure preserves the prior row and never reports purge success; repair the artifact and submit a new request_id. A purge of a page whose file is shared refuses and names the soft delete. A committed purge warns that git history, synced copies, exports and derived rows may retain content; rotate exposed credentials. Before the row is removed, pages that are private only through this page get their own visibility: private; a purge that cannot label them soft-deletes the page instead and returns purge_held (doctor derived_origin_parked).',
params: {
...PAGE_MUTATION_PARAMS,
slug: { type: 'string', required: true, description: "Slug of the page to soft-delete, e.g. 'people/alice-example'." },
@@ -442,7 +442,7 @@ const delete_page: Operation = {
mutating: true,
scope: 'write',
handler: async (ctx, p) => {
- const { pageMutationSource, submitPageMutation, validateMutationSlug } = await pageMutations();
+ const { pageMutationSource, validateMutationSlug } = await pageMutations();
const sourceId = pageMutationSource(ctx, p, 'delete_page');
assertPurgeParams(p, ctx.remote);
if (ctx.dryRun) {
@@ -453,7 +453,7 @@ const delete_page: Operation = {
}
return { dry_run: true, action: p.purge === true ? 'purge_page' : 'delete_page', slug: p.slug };
}
- return (await import('../persistence/page-purge.ts')).finishPagePurge(ctx, await submitPageMutation(ctx, { operation: 'delete_page', params: p }));
+ return (await import('../repair/origin-carry.ts')).deletePageCarryingPrivate(ctx, p, sourceId);
},
cliHints: { name: 'delete', positional: ['slug'] },
};
diff --git a/src/core/persistence/derived-origin-hold.ts b/src/core/persistence/derived-origin-hold.ts
new file mode 100644
index 0000000000..4e6f67d963
--- /dev/null
+++ b/src/core/persistence/derived-origin-hold.ts
@@ -0,0 +1,47 @@
+/**
+ * GST-2689: the purge's park for a soft-deleted page that is the only privacy of a page derived from it. A page that reads
+ * as world by its own label but is hidden from remote callers because its origin is private (a declared derived_from
+ * origin, an atom's source_slug origin, a concept whose provenance atom has one) would open once the origin row is gone,
+ * so before any purge removes such a row the derived pages get `visibility: private` (repair/origin-carry.ts). When that
+ * label write fails, the origin is parked here (op `derived-origin-park`, fingerprint the page id) with the page that
+ * blocks it and the reason; every purge path skips it (derivedOriginPurgeHold) and doctor `derived_origin_parked` names
+ * it. Each purge run tries the label again and drops the park once it lands; a park whose page was restored or purged is
+ * pruned. Rows are exempt from the op_checkpoints TTL sweep. No engine-sql imports, so `engine-sql/pages.ts` may import it.
+ */
+import type { SqlEngine } from './model.ts';
+
+export const DERIVED_ORIGIN_PARK_OP = 'derived-origin-park';
+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. */
+export interface DerivedOriginPark { source_id: string; slug: string; page_id: number; blocking_slug: string | null; code: string | null; reason: string; parked_at: string }
+
+/** SQL predicate for a purge scan over `pages` aliased `alias`: the page is not a parked origin. */
+export function derivedOriginPurgeHold(alias: string): string {
+ return `NOT EXISTS (SELECT 1 FROM op_checkpoints dop WHERE dop.op = '${DERIVED_ORIGIN_PARK_OP}' AND dop.fingerprint = ${alias}.id::text)`;
+}
+
+export async function parkDerivedOrigin(engine: SqlEngine, park: Omit<DerivedOriginPark, 'parked_at'>): Promise<void> {
+ 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=EXCLUDED.completed_keys`, [DERIVED_ORIGIN_PARK_OP, String(park.page_id), JSON.stringify([row])]);
+}
+
+export async function clearDerivedOriginParks(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[])', [DERIVED_ORIGIN_PARK_OP, pageIds.map(String)]);
+}
+
+const TOMBSTONE = 'EXISTS (SELECT 1 FROM pages p WHERE p.id::text = o.fingerprint AND p.deleted_at IS NOT NULL)';
+
+/** Drops the parks of pages that were restored or purged. */
+export async function pruneDerivedOriginParks(engine: SqlEngine): Promise<void> {
+ await engine.executeRaw(`DELETE FROM op_checkpoints o WHERE o.op=$1 AND NOT ${TOMBSTONE}`, [DERIVED_ORIGIN_PARK_OP]);
+}
+
+/** Read-only: every park whose page is still a tombstone, oldest first. */
+export async function readDerivedOriginParks(engine: SqlEngine): Promise<DerivedOriginPark[]> {
+ const rows = await engine.executeRaw<{ park: DerivedOriginPark | string }>(
+ `SELECT o.completed_keys->0 AS park FROM op_checkpoints o WHERE o.op=$1 AND ${TOMBSTONE} ORDER BY o.updated_at, o.fingerprint`, [DERIVED_ORIGIN_PARK_OP]);
+ return rows.map(row => typeof row.park === 'string' ? JSON.parse(row.park) as DerivedOriginPark : row.park);
+}
diff --git a/src/core/persistence/purge-deleted.ts b/src/core/persistence/purge-deleted.ts
index c6f6bfc75e..9c4b0e3e3d 100644
--- a/src/core/persistence/purge-deleted.ts
+++ b/src/core/persistence/purge-deleted.ts
@@ -14,6 +14,7 @@ import { linkStreamSlugs } from './page-purge.ts';
import { identityDowngradePurgeHold } from '../sync-identity-downgrades.ts';
import { atomRetirementPurgeHold } from './atom-retirement-hold.ts';
import { derivedRetirementPurgeHold } from './derived-retirement-hold.ts';
+import { DERIVED_ORIGIN_PARKED_CODE, derivedOriginPurgeHold, type DerivedOriginPark } from './derived-origin-hold.ts';
import { writerOnboardingPreflight } from './onboarding.ts';
import { clearPurgePark, isParked, notePurgeSourceWriter, PURGE_PARK_LIMIT, purgeParkKey, prunePurgeParks, readPurgeParks, recordPurgeFailure, type PurgePark } from './purge-parks.ts';
import { ItemRun, type ItemFailure } from '../item-failure.ts';
@@ -55,6 +56,11 @@ export interface DeletedPagePurge {
*/
write_deferred?: number;
deferral?: DreamWriteDeferralPass;
+ /**
+ * GST-2689: expired origins kept because a page derived from them could not be labelled private before the row went
+ * (in `blocked`, code `derived_origin_parked`); doctor derived_origin_parked names each with the page that blocks it.
+ */
+ origin_parked?: number;
/** Present when `failed > 0`, so the caller cannot report success. */
error?: { class: string; code: string; message: string };
}
@@ -97,7 +103,7 @@ interface RecordedTombstone { id: number; source_id: string; slug: string; delet
async function recordedTombstones(engine: BrainEngine, hours: string): Promise<RecordedTombstone[]> {
const rows = await engine.executeRaw<{ id: number; source_id: string; slug: string; deleted_us: string }>(`SELECT p.id, p.source_id, p.slug,
floor(extract(epoch FROM p.deleted_at)*1000000)::bigint::text AS deleted_us FROM pages p
- WHERE p.deleted_at IS NOT NULL AND p.deleted_at < now() - ($1 || ' hours')::interval AND ${identityDowngradePurgeHold('p')} AND ${atomRetirementPurgeHold('p')} AND ${derivedRetirementPurgeHold('p')}
+ WHERE p.deleted_at IS NOT NULL AND p.deleted_at < now() - ($1 || ' hours')::interval AND ${identityDowngradePurgeHold('p')} AND ${atomRetirementPurgeHold('p')} AND ${derivedRetirementPurgeHold('p')} AND ${derivedOriginPurgeHold('p')}
AND EXISTS (SELECT 1 FROM belief_events e WHERE e.source_id = p.source_id AND e.subject_kind = 'coverage')
ORDER BY p.deleted_at ASC, p.source_id ASC, p.slug ASC`, [hours]);
const parks = rows.length ? await readPurgeParks(engine) : new Map<string, PurgePark>();
@@ -145,7 +151,7 @@ async function expireRecordedTombstones(engine: BrainEngine, olderThanHours: num
await tx.lockPageKeys(slugs.map(slug => ({ sourceId: row.source_id, slug })));
return withBeliefBatch(tx, { sourceId: row.source_id, slugs, publicationId, op: 'purge_deleted_pages', hardDeleteEvent: 'page.expired', report },
async () => {
- const gone = await tx.executeRaw<{ source_id: string; slug: string; deleted_at: string }>(`DELETE FROM pages WHERE id = $1 AND deleted_at IS NOT NULL AND deleted_at < now() - ($2 || ' hours')::interval AND ${identityDowngradePurgeHold('pages')} AND ${atomRetirementPurgeHold('pages')} AND ${derivedRetirementPurgeHold('pages')} RETURNING source_id, slug, deleted_at::text AS deleted_at`,
+ const gone = await tx.executeRaw<{ source_id: string; slug: string; deleted_at: string }>(`DELETE FROM pages WHERE id = $1 AND deleted_at IS NOT NULL AND deleted_at < now() - ($2 || ' hours')::interval AND ${identityDowngradePurgeHold('pages')} AND ${atomRetirementPurgeHold('pages')} AND ${derivedRetirementPurgeHold('pages')} AND ${derivedOriginPurgeHold('pages')} RETURNING source_id, slug, deleted_at::text AS deleted_at`,
[row.id, hours]);
await expireGonePageFacts(tx, gone);
if (gone.length > 0) {
@@ -211,9 +217,9 @@ export interface PurgeSelection {
writers: Record<string, boolean>;
}
-/** What keeps an expired tombstone of page `page` (source `source`) out of every managed purge: an archived source, or the rename-recovery, restorable-atom or derived-retirement hold. */
+/** What keeps an expired tombstone of page `page` (source `source`) out of every managed purge: an archived source, or the rename-recovery, restorable-atom, derived-retirement or derived-origin hold. */
export function purgeHoldsSql(page: string, source: string): string {
- return `NOT ${source}.archived AND ${identityDowngradePurgeHold(page)} AND ${atomRetirementPurgeHold(page)} AND ${derivedRetirementPurgeHold(page)}`;
+ return `NOT ${source}.archived AND ${identityDowngradePurgeHold(page)} AND ${atomRetirementPurgeHold(page)} AND ${derivedRetirementPurgeHold(page)} AND ${derivedOriginPurgeHold(page)}`;
}
export async function selectPurgeCandidates(engine: BrainEngine, olderThanHours: number, opts: { retryParked?: boolean; automatic?: boolean } = {}): Promise<PurgeSelection> {
@@ -281,7 +287,7 @@ export async function selectPurgeCandidates(engine: BrainEngine, olderThanHours:
* and so does an unmanaged recorded tombstone whose delete failed (GBRA-130 A-49: `failed` without `error`).
*/
export function purgePhaseStatus(result: DeletedPagePurge): 'ok' | 'warn' | 'fail' {
- return result.error ? 'fail' : result.inactive || result.parked || result.capacity_deferred || result.failed || result.held || result.write_deferred ? 'warn' : 'ok';
+ return result.error ? 'fail' : result.inactive || result.parked || result.capacity_deferred || result.failed || result.held || result.write_deferred || result.origin_parked ? 'warn' : 'ok';
}
/** The purge phase summary's note on the pages it did not submit (empty when there are none). */
@@ -290,7 +296,8 @@ export function purgePhaseNote(result: DeletedPagePurge): string {
result.parked ? `${result.parked} parked (gbrain doctor: purge_parked_pages)` : '',
result.capacity_deferred ? `${result.capacity_deferred} deferred to the next run (write capacity in use)` : '',
result.held ? `${result.held} deleted by a managed sync held for review (gbrain pages purge-deleted --dry-run; purge with --yes or restore each)` : '',
- result.write_deferred ? `${result.write_deferred} deferred: an accepted purge is still publishing (gbrain doctor: dream_write_deferred)` : ''].filter(Boolean);
+ result.write_deferred ? `${result.write_deferred} deferred: an accepted purge is still publishing (gbrain doctor: dream_write_deferred)` : '',
+ result.origin_parked ? `${result.origin_parked} kept: a page derived from each could not be labelled private (gbrain doctor: derived_origin_parked)` : ''].filter(Boolean);
return parts.length ? ` (${parts.join(', ')})` : '';
}
@@ -316,11 +323,26 @@ export function purgePhaseNote(result: DeletedPagePurge): string {
* #7130: `automatic` (the cycle purge phase and the `purge` job, which run without consent) keeps every tombstone a
* managed sync made unless a live page of its source has the same content (sync-delete-tombstones.ts); it is
* reported `held` (code `sync_delete_unreviewed`). The consented manual purge leaves `automatic` unset.
+ *
+ * GST-2689: every run first labels private the pages an expired origin is the only privacy of (repair/origin-carry.ts);
+ * an origin it could not carry is parked and kept by every path below (code `derived_origin_parked`).
*/
/** Limits a purge to pages the caller already showed the person (`gbrain maintain --purge --expect`). */
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 } = {}): Promise<DeletedPagePurge> {
+ const origins = originParked(await (await import('../repair/origin-carry.ts')).carryPrivateBeforePurge(engine, olderThanHours));
+ const result = await purgeExpired(engine, olderThanHours, opts);
+ return origins.length ? { ...result, blocked: [...origins, ...result.blocked], origin_parked: origins.length } : result;
+}
+
+function originParked(parks: DerivedOriginPark[]): DeletedPagePurge['blocked'] {
+ return parks.map(park => ({ source_id: park.source_id, slug: park.slug, code: DERIVED_ORIGIN_PARKED_CODE,
+ reason: `Kept: ${park.blocking_slug ?? 'a page derived from it'} is private only through this page and could not be labelled private (${park.code ?? 'error'}: ${park.reason}); `
+ + 'the next purge tries again once the cause is fixed (gbrain doctor: derived_origin_parked).' }));
+}
+
+async function purgeExpired(engine: BrainEngine, olderThanHours: number, opts: { retryParked?: boolean; automatic?: boolean; only?: PurgeFilter }): Promise<DeletedPagePurge> {
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) : [];
diff --git a/src/core/repair/origin-carry.ts b/src/core/repair/origin-carry.ts
new file mode 100644
index 0000000000..0d4e1650ba
--- /dev/null
+++ b/src/core/repair/origin-carry.ts
@@ -0,0 +1,183 @@
+/**
+ * GST-2689: a page that reads as world by its own label but is hidden from remote callers only because its origin is
+ * private (a declared derived_from origin, an atom's source_slug origin, a concept whose provenance atom has one) opens
+ * once the origin row is gone. So before an origin row is removed, every live same-source page that would gain a reader
+ * gets `visibility: private` through the label door (repair/label-write.ts): tighten-only, the trust tier kept, no lowering
+ * proposal filed, a managed file changed in that one line. The dependents are private-labels.ts dependentsSql('remove'):
+ * a page already private, naming readers, pinned or not lineage-dependent is never returned, and a life-chronicle event
+ * already fails closed, so it is skipped.
+ *
+ * Two places run it. delete_page (deletePageCarryingPrivate): a soft delete, local or remote, labels the dependents once
+ * the delete committed, as a maintenance write whose batch names the delete's request id (on a managed brain); a failure
+ * there is left to the purge. An explicit purge labels them first; when that fails it soft-deletes instead and parks the
+ * origin. Every expiry (carryPrivateBeforePurge, from purgeDeletedPagesCoordinated) labels the dependents of each expired
+ * private origin and parks the ones it could not (persistence/derived-origin-hold.ts), so no purge removes them.
+ */
+import { createHash, randomUUID } from 'node:crypto';
+import type { BrainEngine } from '../engine.ts';
+import type { OperationContext } from '../ops/contract.ts';
+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, parkDerivedOrigin, pruneDerivedOriginParks, type DerivedOriginPark } from '../persistence/derived-origin-hold.ts';
+import { privacyPartSql, readHonorAliases, withoutCallerAccess } from '../search/private-visibility.ts';
+import { filesInCheckout, importAtRevision, writeLabelFile } from './label-write.ts';
+import { dependentsSql } from './private-labels.ts';
+
+const bounded = { timeoutMs: 120_000, jitOff: true, prepare: false };
+const PREVIEW = 'gbrain repair visibility';
+const EDIT: LabelEdit = { set: { visibility: 'private' }, remove: [] };
+/** A page whose removal can open another: hidden by the privacy rules itself, or an atom a concept's provenance names. */
+const originSql = (alias: string) => `(${alias}.type = 'atom' OR NOT ${privacyPartSql(alias)})`;
+
+interface Origin { id: number; source_id: string; slug: string }
+interface Dependent { id: number; origin_id: number; source_id: string; slug: string }
+interface Failure { code: string | null; reason: string }
+export interface CarryBlock extends Failure { slug: string | null }
+
+const failureOf = (error: unknown): Failure => ({ code: typeof (error as { code?: unknown } | null)?.code === 'string' ? (error as { code: string }).code : null,
+ reason: error instanceof Error ? error.message : String(error) });
+
+async function dependentsOf(engine: BrainEngine, origins: Origin[]): Promise<Dependent[]> {
+ const rows = await engine.executeRaw<{ id: number; cleared_id: number; source_id: string; slug: string }>(
+ `SELECT d.id, d.cleared_id, p.source_id, p.slug FROM (${dependentsSql('remove')}) d JOIN pages p ON p.id = d.id WHERE d.category <> 'life_chronicle' ORDER BY d.id`,
+ [origins.map(o => o.id), origins.map(o => o.source_id), origins.map(o => o.slug), origins.map(() => '')], bounded);
+ return rows.map(row => ({ id: Number(row.id), origin_id: Number(row.cleared_id), source_id: row.source_id, slug: row.slug }));
+}
+
+/** Labels each page private through the label door; the failures by page id. */
+async function stamp(engine: BrainEngine, pages: Dependent[], batch: string): Promise<Map<number, Failure>> {
+ const failed = new Map<number, Failure>();
+ const ready: Array<{ page: Dependent; revision: string; content: string }> = [];
+ for (const page of pages) {
+ const snapshot = await engine.readPageSnapshot(page.slug, { sourceId: page.source_id });
+ if (!snapshot || Number(snapshot.page.id) !== page.id) { failed.set(page.id, { code: 'page_identity_changed', reason: `${page.slug} changed identity while it was being labelled private.` }); continue; }
+ if (isSegmentedPage(snapshot.page)) { failed.set(page.id, { code: 'segmented_page', reason: `${page.slug} is stored in segments, which a label write does not rewrite.` }); continue; }
+ ready.push({ page, revision: snapshot.revision,
+ content: serializePageToMarkdown({ ...snapshot.page, frontmatter: { ...snapshot.page.frontmatter, visibility: 'private' } }, snapshot.tags) });
+ }
+ for (const sourceId of new Set(ready.map(entry => entry.page.source_id))) {
+ const group = ready.filter(entry => entry.page.source_id === sourceId);
+ try {
+ const authority = await maintenancePreflight(engine, sourceId);
+ if (authority) {
+ const settled = await publishMaintenancePages(engine, authority, group.map(({ page, revision, content }) => ({ slug: page.slug, content, expectedRevision: revision,
+ requestId: randomUUID(), keepTrust: true as const, labelEdit: EDIT })), { batch });
+ settled.forEach((entry, index) => { if ('error' in entry) failed.set(group[index]!.page.id, failureOf(entry.error)); });
+ continue;
+ }
+ const files = await filesInCheckout(engine, group.map(entry => entry.page.id));
+ for (const { page, revision, content } of group) {
+ try {
+ const path = files.get(page.id);
+ const written = path ? await writeLabelFile(engine, page, { revision, content, edit: EDIT, embed: false, preview: PREVIEW, path })
+ : await importAtRevision(engine, page, revision, content, false, PREVIEW);
+ if (written === null) failed.set(page.id, { code: 'revision_conflict', reason: `${page.slug} changed while it was being labelled private.` });
+ else if (typeof written !== 'string') failed.set(page.id, { code: written.outcome ?? null, reason: written.reason ?? 'the label write was held' });
+ } catch (error) { failed.set(page.id, failureOf(error)); }
+ }
+ } catch (error) { for (const { page } of group) failed.set(page.id, failureOf(error)); }
+ }
+ return failed;
+}
+
+/**
+ * Labels private every page each origin is the only privacy of, as if the origin rows were gone; the origins still
+ * blocked afterwards, by origin id, with the first page that still would open and why. Runs outside any caller's view.
+ */
+export async function carryPrivate(engine: BrainEngine, origins: Origin[], batch: string): Promise<Map<number, CarryBlock>> {
+ if (!origins.length) return new Map();
+ return withoutCallerAccess(async () => {
+ await readHonorAliases(engine);
+ try {
+ const pages = [...new Map((await dependentsOf(engine, origins)).map(page => [page.id, page])).values()];
+ if (!pages.length) return new Map<number, CarryBlock>();
+ const failed = await stamp(engine, pages, batch);
+ const blocked = new Map<number, CarryBlock>();
+ for (const page of await dependentsOf(engine, origins)) {
+ if (!blocked.has(page.origin_id)) blocked.set(page.origin_id, { slug: page.slug,
+ ...(failed.get(page.id) ?? { code: null, reason: `${page.slug} still reads as world without its origin.` }) });
+ }
+ return blocked;
+ } catch (error) {
+ return new Map<number, CarryBlock>(origins.map(origin => [origin.id, { slug: null, ...failureOf(error) }]));
+ }
+ });
+}
+
+async function originOf(engine: BrainEngine, sourceId: string, slug: string): Promise<(Origin & { deleted: boolean }) | null> {
+ return withoutCallerAccess(async () => {
+ await readHonorAliases(engine);
+ const [row] = await engine.executeRaw<{ id: number; deleted: boolean }>(`SELECT p.id, p.deleted_at IS NOT NULL AS deleted FROM pages p
+ WHERE p.source_id = $1 AND p.slug = $2 AND ${originSql('p')}`, [sourceId, slug], bounded);
+ return row ? { id: Number(row.id), source_id: sourceId, slug, deleted: row.deleted === true } : null;
+ }).catch(() => null);
+}
+
+const parked = (origin: Origin, block: CarryBlock) => ({ source_id: origin.source_id, slug: origin.slug, page_id: origin.id, blocking_slug: block.slug, code: block.code, reason: block.reason });
+
+/** A request id derived from `requestId`, UUID-shaped, so a replay of the purge request replays its fallback soft delete. */
+function derivedRequestId(requestId: string): string {
+ const h = createHash('sha256').update(`derived-origin-soft-delete:${requestId}`).digest('hex');
+ return `${h.slice(0, 8)}-${h.slice(8, 12)}-5${h.slice(13, 16)}-8${h.slice(17, 20)}-${h.slice(20, 32)}`;
+}
+
+/**
+ * The delete_page handler after validation. A soft delete labels the dependents once it committed and returns the same
+ * receipt either way (it names no labelled page and counts none). A purge (trusted local) labels them first; when that
+ * fails, the page is soft-deleted instead (already a tombstone: left as it is), parked, and the receipt carries
+ * `purge_held`, so nothing that kept the derived pages hidden is removed.
+ */
+export async function deletePageCarryingPrivate(ctx: OperationContext, p: Record<string, unknown>, sourceId: string): Promise<Record<string, unknown>> {
+ const { submitPageMutation } = await import('../persistence/page-mutations.ts');
+ const { finishPagePurge } = await import('../persistence/page-purge.ts');
+ p.request_id ??= randomUUID();
+ const batch = `derived-origin-carry:${String(p.request_id)}`;
+ const origin = typeof p.slug === 'string' ? await originOf(ctx.engine, sourceId, p.slug) : null;
+ if (p.purge !== true) {
+ const result = await submitPageMutation(ctx, { operation: 'delete_page', params: p });
+ if (origin && (await originOf(ctx.engine, sourceId, origin.slug))?.deleted) await carryPrivate(ctx.engine, [origin], batch);
+ return finishPagePurge(ctx, result);
+ }
+ const block = origin ? (await carryPrivate(ctx.engine, [origin], batch)).get(origin.id) : undefined;
+ if (!origin || !block) {
+ const result = await finishPagePurge(ctx, await submitPageMutation(ctx, { operation: 'delete_page', params: p }));
+ if (origin && result.status === 'purged') await clearDerivedOriginParks(ctx.engine, [origin.id]);
+ return result;
+ }
+ 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 parkDerivedOrigin(ctx.engine, parked(origin, block));
+ 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.` } };
+}
+
+/**
+ * Before a purge: the expired soft-deleted origins get their dependents labelled private, 200 at a time; each one still
+ * blocked is parked (the purge skips it) and the rest lose any earlier park. The parks written this run.
+ */
+export async function carryPrivateBeforePurge(engine: BrainEngine, olderThanHours: number): Promise<DerivedOriginPark[]> {
+ await pruneDerivedOriginParks(engine);
+ const origins = await withoutCallerAccess(async () => {
+ await readHonorAliases(engine);
+ return engine.executeRaw<{ id: number; source_id: string; slug: string }>(`SELECT p.id, p.source_id, p.slug FROM pages p
+ WHERE p.deleted_at IS NOT NULL AND p.deleted_at < now() - ($1 || ' hours')::interval AND ${originSql('p')} ORDER BY p.id`,
+ [String(Math.max(0, Math.floor(olderThanHours)))], bounded);
+ });
+ const parks: DerivedOriginPark[] = [];
+ const batch = `derived-origin-carry:purge-${randomUUID()}`;
+ for (let i = 0; i < origins.length; i += 200) {
+ const chunk = origins.slice(i, i + 200).map(row => ({ id: Number(row.id), source_id: row.source_id, slug: row.slug }));
+ const blocked = await carryPrivate(engine, chunk, batch);
+ for (const origin of chunk) {
+ const block = blocked.get(origin.id);
+ if (!block) continue;
+ await parkDerivedOrigin(engine, parked(origin, block));
+ parks.push({ ...parked(origin, block), parked_at: new Date().toISOString() });
+ }
+ await clearDerivedOriginParks(engine, chunk.filter(origin => !blocked.has(origin.id)).map(origin => origin.id));
+ }
+ return parks;
+}
diff --git a/src/core/repair/private-labels.ts b/src/core/repair/private-labels.ts
index d66ccae113..e9c9b5dca8 100644
--- a/src/core/repair/private-labels.ts
+++ b/src/core/repair/private-labels.ts
@@ -233,8 +233,13 @@ async function planRestore(engine: BrainEngine, scope: RepairScope, selection: S
* derived_from slug without `.md`), and every tie is an equality join of those references, or of concept-provenance
* links through idx_links_to, to the cleared pages. The postimage is the pages table shadowed with the window's keys
* removed; its privacy rules read only candidate pages and the pages they name.
+ *
+ * GST-2689 `remove`: the postimage is the pages table with the cleared pages removed instead (their rows, so the links to
+ * them go too), `keys` is unused, an atom also ties to the page a rename moved its `source_slug` to (slug_aliases and
+ * slug_rename_history, read before a purge scrubs them), and a page that names readers is never returned.
*/
-export function dependentsSql(): string {
+export function dependentsSql(mode: 'clear' | 'remove' = 'clear'): string {
+ const remove = mode === 'remove';
return `WITH cleared AS MATERIALIZED (
SELECT * FROM unnest($1::integer[], $2::text[], $3::text[], $4::text[]) AS c(id, source_id, slug, keys)),
refs AS MATERIALIZED (SELECT p.id, p.source_id, p.slug, p.deleted_at, r.kind, r.ref FROM pages p
@@ -242,7 +247,11 @@ export function dependentsSql(): string {
UNION ALL SELECT 'depth', p.frontmatter->'event'->>'depth'
UNION ALL SELECT 'declared', regexp_replace(declared.slug, '\\.md$', '') FROM jsonb_array_elements_text(
CASE jsonb_typeof(p.frontmatter->'derived_from') WHEN 'array' THEN p.frontmatter->'derived_from'
- WHEN 'string' THEN jsonb_build_array(p.frontmatter->'derived_from') ELSE '[]'::jsonb END) AS declared(slug)) r
+ WHEN 'string' THEN jsonb_build_array(p.frontmatter->'derived_from') ELSE '[]'::jsonb END) AS declared(slug)${remove ? `
+ UNION ALL SELECT 'source_slug', renamed.canonical_slug FROM slug_aliases renamed WHERE renamed.source_id = p.source_id
+ AND renamed.alias_slug = p.frontmatter->>'source_slug' AND renamed.notes = 'rename'
+ UNION ALL SELECT 'source_slug', moved.new_slug FROM slug_rename_history moved WHERE moved.source_id = p.source_id
+ AND moved.old_slug = p.frontmatter->>'source_slug'` : ''}) r
WHERE p.source_id = ANY($2::text[]) AND r.ref IS NOT NULL),
hits AS MATERIALIZED (SELECT r.id, r.source_id, r.slug, r.deleted_at, r.kind, c.id AS cleared_id
FROM refs r JOIN cleared c ON c.source_id = r.source_id AND c.slug = r.ref),
@@ -256,13 +265,13 @@ export function dependentsSql(): string {
candidates AS MATERIALIZED (SELECT DISTINCT t.id, t.cleared_id FROM ties t WHERE NOT EXISTS (SELECT 1 FROM cleared x WHERE x.id = t.id)),
public_after AS MATERIALIZED (WITH pages AS NOT MATERIALIZED (SELECT pg.id, pg.source_id, pg.slug, pg.type, pg.deleted_at,
CASE WHEN c.id IS NULL THEN pg.frontmatter ELSE pg.frontmatter - string_to_array(c.keys, ',') END AS frontmatter
- FROM pages pg LEFT JOIN cleared c ON c.id = pg.id)
+ FROM pages pg LEFT JOIN cleared c ON c.id = pg.id${remove ? ' WHERE c.id IS NULL' : ''})
SELECT after_clear.id FROM (SELECT a.id, a.source_id, a.slug, a.type, a.frontmatter FROM (SELECT DISTINCT id FROM candidates) k
JOIN pages a ON a.id = k.id OFFSET 0) after_clear WHERE ${privacyPartSql('after_clear')})
SELECT cand.id, cand.cleared_id, CASE WHEN p.type = 'event' AND p.frontmatter->>'captured_via' LIKE 'life-chronicle:%' THEN 'life_chronicle'
WHEN p.type = 'atom' THEN 'atom' WHEN p.type = 'concept' AND p.frontmatter->>'synthesized_by' IS NOT NULL THEN 'concept' ELSE 'derived_from' END AS category
FROM candidates cand JOIN pages p ON p.id = cand.id
- WHERE cand.id IN (SELECT id FROM public_after) AND NOT ${privacyPartSql('p')}
+ WHERE cand.id IN (SELECT id FROM public_after) AND NOT ${privacyPartSql('p')}${remove ? " AND NOT (p.frontmatter ? 'readable_by')" : ''}
ORDER BY cand.id, cand.cleared_id`;
}
diff --git a/test/fixtures/goldens/doctor/hot-checks-pglite-json.json b/test/fixtures/goldens/doctor/hot-checks-pglite-json.json
index a1e2a02db9..83d0f2a1fb 100644
--- a/test/fixtures/goldens/doctor/hot-checks-pglite-json.json
+++ b/test/fixtures/goldens/doctor/hot-checks-pglite-json.json
@@ -3958,6 +3958,17 @@
"name": "purge_parked_pages",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "origins": [],
+ "parked": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No soft-deleted page is kept from the purge to keep a page derived from it private.",
+ "name": "derived_origin_parked",
+ "status": "ok"
+ },
{
"category": "brain",
"details": {
@@ -9296,6 +9307,17 @@
"name": "purge_parked_pages",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "origins": [],
+ "parked": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No soft-deleted page is kept from the purge to keep a page derived from it private.",
+ "name": "derived_origin_parked",
+ "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 bcbb72de8b..cb12f77087 100644
--- a/test/fixtures/goldens/doctor/hot-checks-postgres-json.json
+++ b/test/fixtures/goldens/doctor/hot-checks-postgres-json.json
@@ -3964,6 +3964,17 @@
"name": "purge_parked_pages",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "origins": [],
+ "parked": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No soft-deleted page is kept from the purge to keep a page derived from it private.",
+ "name": "derived_origin_parked",
+ "status": "ok"
+ },
{
"category": "brain",
"details": {
@@ -9306,6 +9317,17 @@
"name": "purge_parked_pages",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "origins": [],
+ "parked": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No soft-deleted page is kept from the purge to keep a page derived from it private.",
+ "name": "derived_origin_parked",
+ "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 534415ee7a..fc5b16460f 100644
--- a/test/fixtures/goldens/doctor/json-pglite-degraded.json
+++ b/test/fixtures/goldens/doctor/json-pglite-degraded.json
@@ -3409,6 +3409,17 @@
"name": "purge_parked_pages",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "origins": [],
+ "parked": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No soft-deleted page is kept from the purge to keep a page derived from it private.",
+ "name": "derived_origin_parked",
+ "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 a41c98640d..0386cb119c 100644
--- a/test/fixtures/goldens/doctor/json-pglite-fresh.json
+++ b/test/fixtures/goldens/doctor/json-pglite-fresh.json
@@ -3452,6 +3452,17 @@
"name": "purge_parked_pages",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "origins": [],
+ "parked": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No soft-deleted page is kept from the purge to keep a page derived from it private.",
+ "name": "derived_origin_parked",
+ "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 0967acc272..b083ba735d 100644
--- a/test/fixtures/goldens/doctor/json-postgres-fresh.json
+++ b/test/fixtures/goldens/doctor/json-postgres-fresh.json
@@ -3458,6 +3458,17 @@
"name": "purge_parked_pages",
"status": "ok"
},
+ {
+ "category": "brain",
+ "details": {
+ "origins": [],
+ "parked": 0
+ },
+ "elapsed_ms": "<volatile>",
+ "message": "No soft-deleted page is kept from the purge to keep a page derived from it private.",
+ "name": "derived_origin_parked",
+ "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 e3cd4c6a07..adc6530d1d 100644
--- a/test/fixtures/goldens/doctor/registry-build-checks.json
+++ b/test/fixtures/goldens/doctor/registry-build-checks.json
@@ -1133,6 +1133,10 @@
"category": "brain",
"name": "purge_parked_pages"
},
+ {
+ "category": "brain",
+ "name": "derived_origin_parked"
+ },
{
"category": "brain",
"name": "derivation_purge_sweeps"
diff --git a/test/fixtures/goldens/sql-text/pages.json b/test/fixtures/goldens/sql-text/pages.json
index 876b215307..8f5531154d 100644
--- a/test/fixtures/goldens/sql-text/pages.json
+++ b/test/fixtures/goldens/sql-text/pages.json
@@ -865,8 +865,8 @@
"params": [
"number"
],
- "sql": "DELETE FROM pages WHERE deleted_at IS NOT NULL AND deleted_at < now() - ($1 || ' hours')::interval -- #6729: the old page of an open rename-identity downgrade waits for its disposition (identityDowngradePurgeHold). AND NOT EXISTS (SELECT 1 FROM op_checkpoints dg WHERE dg.op = 'sync-identity-downgrade' AND dg.fingerprint = pages.id::text AND dg.completed_keys->0->>'disposition' IS NULL AND pages.deleted_at = (dg.completed_keys->0->>'old_deleted_at')::timestamptz) -- #7013: a restorable re-extraction retirement waits out its hold (atomRetirementPurgeHold). AND NOT (pages.type='atom' AND pages.frontmatter->>'retired_by'='managed-reextract' AND pages.deleted_at > now() - interval '30 days' AND pages.id IN (SELECT held.id FROM (WITH retired AS ( SELECT p.id, p.source_id, p.slug, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, NULLIF(p.frontmatter->>'source_slug','') AS origin_slug, COALESCE(NULLIF(p.frontmatter->>'source_slug',''), p.frontmatter->>'source_path') AS origin, regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','') AS hash, p.frontmatter->>'retired_at' AS retired_at FROM pages p WHERE p.type='atom' AND p.deleted_at IS NOT NULL AND p.frontmatter->>'retired_by'='managed-reextract' AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) ), latest AS ( SELECT DISTINCT ON (source_id, okey) source_id, okey, hash FROM retired WHERE okey IS NOT NULL ORDER BY source_id, okey, retired_at DESC NULLS LAST, id DESC ), live AS ( SELECT p.source_id, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, array_agg(DISTINCT regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','')) AS hashes FROM pages p WHERE p.type='atom' AND p.deleted_at IS NULL AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) GROUP BY 1, 2 ) SELECT r.id, r.source_id, r.slug, CASE WHEN r.origin_slug IS NOT NULL THEN 'page' ELSE 'transcript' END AS origin_kind, r.origin, o.id AS origin_page_id, r.hash, r.retired_at, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL)) AS origin_live, (lv.hashes IS NOT NULL AND NOT (lv.hashes <@ ARRAY[r.hash])) AS replaced, COALESCE(l.hash = r.hash, false) AS latest, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL) AND NOT COALESCE(lv.hashes IS NOT NULL AND NOT (lv.hashes <@ ARRAY[r.hash]), false) AND COALESCE(l.hash = r.hash, false)) AS restorable, (SELECT g.id FROM derived_generation_items i JOIN derived_generations g ON g.id=i.generation_id WHERE g.source_id=r.source_id AND g.kind='atom' AND g.state='live' AND i.item_key=r.slug AND i.action='retired' ORDER BY g.id DESC LIMIT 1) AS ledger_generation FROM retired r LEFT JOIN pages o ON o.source_id=r.source_id AND o.slug=r.origin_slug AND o.deleted_at IS NULL LEFT JOIN live lv ON lv.source_id=r.source_id AND lv.okey=r.okey LEFT JOIN latest l ON l.source_id=r.source_id AND l.okey=r.okey) held WHERE held.restorable)) -- GBRA-161: every derived retirement waits out its hold (derivedRetirementPurgeHold). AND NOT (COALESCE(pages.frontmatter->>'retired_by','') IN ('extract-atoms','managed-reextract','stale-atoms','life-chronicle','generation-restore','synthesis-resynthesis','synthesis-cleanup') AND pages.deleted_at > now() - interval '1 day' * (SELECT COALESCE(MAX(CASE WHEN c.value ~ '^[0-9]{1,3}$' THEN CASE WHEN c.value::int BETWEEN 3 AND 365 THEN c.value::int END END), 30) FROM config c WHERE c.key = 'generations.restore_days')) RETURNING slug, source_id, deleted_at::text AS deleted_at",
- "textSha256": "412d5d3181f2727b9cd12084ef7ce8cf781753671950b25bec0e05e2d6cbc8d5"
+ "sql": "DELETE FROM pages WHERE deleted_at IS NOT NULL AND deleted_at < now() - ($1 || ' hours')::interval -- #6729: the old page of an open rename-identity downgrade waits for its disposition (identityDowngradePurgeHold). AND NOT EXISTS (SELECT 1 FROM op_checkpoints dg WHERE dg.op = 'sync-identity-downgrade' AND dg.fingerprint = pages.id::text AND dg.completed_keys->0->>'disposition' IS NULL AND pages.deleted_at = (dg.completed_keys->0->>'old_deleted_at')::timestamptz) -- #7013: a restorable re-extraction retirement waits out its hold (atomRetirementPurgeHold). AND NOT (pages.type='atom' AND pages.frontmatter->>'retired_by'='managed-reextract' AND pages.deleted_at > now() - interval '30 days' AND pages.id IN (SELECT held.id FROM (WITH retired AS ( SELECT p.id, p.source_id, p.slug, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, NULLIF(p.frontmatter->>'source_slug','') AS origin_slug, COALESCE(NULLIF(p.frontmatter->>'source_slug',''), p.frontmatter->>'source_path') AS origin, regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','') AS hash, p.frontmatter->>'retired_at' AS retired_at FROM pages p WHERE p.type='atom' AND p.deleted_at IS NOT NULL AND p.frontmatter->>'retired_by'='managed-reextract' AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) ), latest AS ( SELECT DISTINCT ON (source_id, okey) source_id, okey, hash FROM retired WHERE okey IS NOT NULL ORDER BY source_id, okey, retired_at DESC NULLS LAST, id DESC ), live AS ( SELECT p.source_id, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, array_agg(DISTINCT regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','')) AS hashes FROM pages p WHERE p.type='atom' AND p.deleted_at IS NULL AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) GROUP BY 1, 2 ) SELECT r.id, r.source_id, r.slug, CASE WHEN r.origin_slug IS NOT NULL THEN 'page' ELSE 'transcript' END AS origin_kind, r.origin, o.id AS origin_page_id, r.hash, r.retired_at, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL)) AS origin_live, (lv.hashes IS NOT NULL AND NOT (lv.hashes <@ ARRAY[r.hash])) AS replaced, COALESCE(l.hash = r.hash, false) AS latest, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL) AND NOT COALESCE(lv.hashes IS NOT NULL AND NOT (lv.hashes <@ ARRAY[r.hash]), false) AND COALESCE(l.hash = r.hash, false)) AS restorable, (SELECT g.id FROM derived_generation_items i JOIN derived_generations g ON g.id=i.generation_id WHERE g.source_id=r.source_id AND g.kind='atom' AND g.state='live' AND i.item_key=r.slug AND i.action='retired' ORDER BY g.id DESC LIMIT 1) AS ledger_generation FROM retired r LEFT JOIN pages o ON o.source_id=r.source_id AND o.slug=r.origin_slug AND o.deleted_at IS NULL LEFT JOIN live lv ON lv.source_id=r.source_id AND lv.okey=r.okey LEFT JOIN latest l ON l.source_id=r.source_id AND l.okey=r.okey) held WHERE held.restorable)) -- GBRA-161: every derived retirement waits out its hold (derivedRetirementPurgeHold). AND NOT (COALESCE(pages.frontmatter->>'retired_by','') IN ('extract-atoms','managed-reextract','stale-atoms','life-chronicle','generation-restore','synthesis-resynthesis','synthesis-cleanup') AND pages.deleted_at > now() - interval '1 day' * (SELECT COALESCE(MAX(CASE WHEN c.value ~ '^[0-9]{1,3}$' THEN CASE WHEN c.value::int BETWEEN 3 AND 365 THEN c.value::int END END), 30) FROM config c WHERE c.key = 'generations.restore_days')) -- GST-2689: an origin whose derived pages could not be labelled private stays parked (derivedOriginPurgeHold). AND NOT EXISTS (SELECT 1 FROM op_checkpoints dop WHERE dop.op = 'derived-origin-park' AND dop.fingerprint = pages.id::text) RETURNING slug, source_id, deleted_at::text AS deleted_at",
+ "textSha256": "0aa3be82c72ae8fb9b6fa261c4b967abae7b29c1d1b7cb1b1a2c50a84a7105d5"
},
{
"event": "commit",
@@ -879,8 +879,8 @@
"params": [
"number"
],
- "sql": "SELECT slug, source_id, deleted_at FROM pages WHERE deleted_at IS NOT NULL AND deleted_at < now() - ($1 || ' hours')::interval AND NOT EXISTS (SELECT 1 FROM op_checkpoints dg WHERE dg.op = 'sync-identity-downgrade' AND dg.fingerprint = pages.id::text AND dg.completed_keys->0->>'disposition' IS NULL AND pages.deleted_at = (dg.completed_keys->0->>'old_deleted_at')::timestamptz) AND NOT (pages.type='atom' AND pages.frontmatter->>'retired_by'='managed-reextract' AND pages.deleted_at > now() - interval '30 days' AND pages.id IN (SELECT held.id FROM (WITH retired AS ( SELECT p.id, p.source_id, p.slug, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, NULLIF(p.frontmatter->>'source_slug','') AS origin_slug, COALESCE(NULLIF(p.frontmatter->>'source_slug',''), p.frontmatter->>'source_path') AS origin, regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','') AS hash, p.frontmatter->>'retired_at' AS retired_at FROM pages p WHERE p.type='atom' AND p.deleted_at IS NOT NULL AND p.frontmatter->>'retired_by'='managed-reextract' AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) ), latest AS ( SELECT DISTINCT ON (source_id, okey) source_id, okey, hash FROM retired WHERE okey IS NOT NULL ORDER BY source_id, okey, retired_at DESC NULLS LAST, id DESC ), live AS ( SELECT p.source_id, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, array_agg(DISTINCT regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','')) AS hashes FROM pages p WHERE p.type='atom' AND p.deleted_at IS NULL AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) GROUP BY 1, 2 ) SELECT r.id, r.source_id, r.slug, CASE WHEN r.origin_slug IS NOT NULL THEN 'page' ELSE 'transcript' END AS origin_kind, r.origin, o.id AS origin_page_id, r.hash, r.retired_at, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL)) AS origin_live, (lv.hashes IS NOT NULL AND NOT (lv.hashes <@ ARRAY[r.hash])) AS replaced, COALESCE(l.hash = r.hash, false) AS latest, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL) AND NOT COALESCE(lv.hashes IS NOT NULL AND NOT (lv.hashes <@ ARRAY[r.hash]), false) AND COALESCE(l.hash = r.hash, false)) AS restorable, (SELECT g.id FROM derived_generation_items i JOIN derived_generations g ON g.id=i.generation_id WHERE g.source_id=r.source_id AND g.kind='atom' AND g.state='live' AND i.item_key=r.slug AND i.action='retired' ORDER BY g.id DESC LIMIT 1) AS ledger_generation FROM retired r LEFT JOIN pages o ON o.source_id=r.source_id AND o.slug=r.origin_slug AND o.deleted_at IS NULL LEFT JOIN live lv ON lv.source_id=r.source_id AND lv.okey=r.okey LEFT JOIN latest l ON l.source_id=r.source_id AND l.okey=r.okey) held WHERE held.restorable)) AND NOT (COALESCE(pages.frontmatter->>'retired_by','') IN ('extract-atoms','managed-reextract','stale-atoms','life-chronicle','generation-restore','synthesis-resynthesis','synthesis-cleanup') AND pages.deleted_at > now() - interval '1 day' * (SELECT COALESCE(MAX(CASE WHEN c.value ~ '^[0-9]{1,3}$' THEN CASE WHEN c.value::int BETWEEN 3 AND 365 THEN c.value::int END END), 30) FROM config c WHERE c.key = 'generations.restore_days')) ORDER BY deleted_at ASC, slug ASC",
- "textSha256": "7ce76bf731ff75d2f4533d301ae0c8c6ca775eb648be6b1ede0c7d2242299163"
+ "sql": "SELECT slug, source_id, deleted_at FROM pages WHERE deleted_at IS NOT NULL AND deleted_at < now() - ($1 || ' hours')::interval AND NOT EXISTS (SELECT 1 FROM op_checkpoints dg WHERE dg.op = 'sync-identity-downgrade' AND dg.fingerprint = pages.id::text AND dg.completed_keys->0->>'disposition' IS NULL AND pages.deleted_at = (dg.completed_keys->0->>'old_deleted_at')::timestamptz) AND NOT (pages.type='atom' AND pages.frontmatter->>'retired_by'='managed-reextract' AND pages.deleted_at > now() - interval '30 days' AND pages.id IN (SELECT held.id FROM (WITH retired AS ( SELECT p.id, p.source_id, p.slug, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, NULLIF(p.frontmatter->>'source_slug','') AS origin_slug, COALESCE(NULLIF(p.frontmatter->>'source_slug',''), p.frontmatter->>'source_path') AS origin, regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','') AS hash, p.frontmatter->>'retired_at' AS retired_at FROM pages p WHERE p.type='atom' AND p.deleted_at IS NOT NULL AND p.frontmatter->>'retired_by'='managed-reextract' AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) ), latest AS ( SELECT DISTINCT ON (source_id, okey) source_id, okey, hash FROM retired WHERE okey IS NOT NULL ORDER BY source_id, okey, retired_at DESC NULLS LAST, id DESC ), live AS ( SELECT p.source_id, CASE WHEN NULLIF(p.frontmatter->>'source_slug','') IS NOT NULL THEN 'slug:'||(p.frontmatter->>'source_slug') WHEN NULLIF(p.frontmatter->>'source_path','') IS NOT NULL THEN 'path:'||(p.frontmatter->>'source_path') END AS okey, array_agg(DISTINCT regexp_replace(COALESCE(p.frontmatter->>'source_hash',''),'^pending:','')) AS hashes FROM pages p WHERE p.type='atom' AND p.deleted_at IS NULL AND NULLIF(p.frontmatter->>'imported_from','') IS NULL AND (true) GROUP BY 1, 2 ) SELECT r.id, r.source_id, r.slug, CASE WHEN r.origin_slug IS NOT NULL THEN 'page' ELSE 'transcript' END AS origin_kind, r.origin, o.id AS origin_page_id, r.hash, r.retired_at, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL)) AS origin_live, (lv.hashes IS NOT NULL AND NOT (lv.hashes <@ ARRAY[r.hash])) AS replaced, COALESCE(l.hash = r.hash, false) AS latest, (r.okey IS NOT NULL AND (r.origin_slug IS NULL OR o.id IS NOT NULL) AND NOT COALESCE(lv.hashes IS NOT NULL AND NOT (lv.hashes <@ ARRAY[r.hash]), false) AND COALESCE(l.hash = r.hash, false)) AS restorable, (SELECT g.id FROM derived_generation_items i JOIN derived_generations g ON g.id=i.generation_id WHERE g.source_id=r.source_id AND g.kind='atom' AND g.state='live' AND i.item_key=r.slug AND i.action='retired' ORDER BY g.id DESC LIMIT 1) AS ledger_generation FROM retired r LEFT JOIN pages o ON o.source_id=r.source_id AND o.slug=r.origin_slug AND o.deleted_at IS NULL LEFT JOIN live lv ON lv.source_id=r.source_id AND lv.okey=r.okey LEFT JOIN latest l ON l.source_id=r.source_id AND l.okey=r.okey) held WHERE held.restorable)) AND NOT (COALESCE(pages.frontmatter->>'retired_by','') IN ('extract-atoms','managed-reextract','stale-atoms','life-chronicle','generation-restore','synthesis-resynthesis','synthesis-cleanup') AND pages.deleted_at > now() - interval '1 day' * (SELECT COALESCE(MAX(CASE WHEN c.value ~ '^[0-9]{1,3}$' THEN CASE WHEN c.value::int BETWEEN 3 AND 365 THEN c.value::int END END), 30) FROM config c WHERE c.key = 'generations.restore_days')) AND NOT EXISTS (SELECT 1 FROM op_checkpoints dop WHERE dop.op = 'derived-origin-park' AND dop.fingerprint = pages.id::text) ORDER BY deleted_at ASC, slug ASC",
+ "textSha256": "ba3170990698f57c86dff9a3188d61013507b70e7cbca73291ab8b2ae79a841f"
}
]
},