Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Hold an explicit purge when the origin lookup fails instead of treating the page as no origin
garrytaned609336e805
diff --git a/src/core/persistence/page-purge.ts b/src/core/persistence/page-purge.tsindex 03af4856d9..af72b8f7ab 100644--- a/src/core/persistence/page-purge.ts+++ b/src/core/persistence/page-purge.ts@@ -282,7 +282,7 @@ export async function purgePageInTransaction(tx: BrainEngine, row: WriteRequest,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.const { MID_PURGE_HOLD, parkUncarriedInPurge } = await import('../repair/origin-carry.ts');- if ((await parkUncarriedInPurge(tx, [Number(page.id)])).length) {+ if ((await parkUncarriedInPurge(tx, [Number(page.id)], { recheckParked: true })).length) {if (!page.deleted_at) { await tx.createVersion(slug, { sourceId }); await withTrustKeep(tx, ['pages'], () => tx.softDeletePage(slug, { sourceId })); }return { status: 'soft_deleted', slug, source_id: sourceId, noop: !!page.deleted_at, ...(prepared.retention ? { retention: true } : {}), purge_held: MID_PURGE_HOLD };}diff --git a/src/core/repair/origin-carry.ts b/src/core/repair/origin-carry.tsindex b4110ece3e..394f9bdbd5 100644--- a/src/core/repair/origin-carry.ts+++ b/src/core/repair/origin-carry.ts@@ -10,7 +10,7 @@* 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+ * origin, and when the origin lookup itself fails it soft-deletes and keeps any park (purge_held derived_origin_unchecked). 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';@@ -119,7 +119,7 @@ async function originOf(engine: BrainEngine, sourceId: string, slug: string): Prconst [row] = await engine.executeRaw<{ id: number; deleted: boolean }>(`SELECT p.id, p.deleted_at IS NOT NULL AS deleted FROM pages pWHERE 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 });@@ -141,12 +141,19 @@ export async function deletePageCarryingPrivate(ctx: OperationContext, p: Recordconst { 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.toLowerCase()) : null;+ const slug = typeof p.slug === 'string' ? p.slug.toLowerCase() : null;if (p.purge !== true) {+ const origin = slug ? await originOf(ctx.engine, sourceId, slug).catch(() => null) : null;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).catch(() => undefined);+ if (origin && (await originOf(ctx.engine, sourceId, origin.slug).catch(() => null))?.deleted) await carryPrivate(ctx.engine, [origin], batch).catch(() => undefined);return finishPagePurge(ctx, result);}+ let origin: (Origin & { deleted: boolean }) | null;+ try { origin = slug ? await originOf(ctx.engine, sourceId, slug) : null; }+ catch {+ const held = await submitPageMutation(ctx, { operation: 'delete_page', params: { ...p, purge: false, request_id: derivedRequestId(String(p.request_id)) } });+ return { ...held, purge_held: ORIGIN_UNCHECKED_HOLD };+ }const block = origin ? (await carryPrivate(ctx.engine, [origin], batch)).get(origin.id) : undefined;if (!origin || !block) {if (origin) await clearDerivedOriginParks(ctx.engine, [origin.id]);@@ -202,23 +209,28 @@ export async function retryParkedOrigins(engine: BrainEngine, olderThanHours: nuawait clearDerivedOriginParks(engine, (await carryPrivateBeforePurge(engine, olderThanHours)).unparked.map(origin => origin.id));}+/** The receipt note of an explicit purge held because its origin lookup failed; it names no page and counts none. */+export const ORIGIN_UNCHECKED_HOLD = { code: 'derived_origin_unchecked',+ message: 'Not purged: whether a page derived from this one would open without it could not be checked, so this page is kept soft-deleted (a parked page stays parked); purge it again once that read works, and the automatic purge checks it before it removes it.' };+/** The receipt note of a purge the in-transaction recheck held; it names no page and counts none. */export const MID_PURGE_HOLD = { code: DERIVED_ORIGIN_PARKED_CODE,message: 'Not purged: a page derived from this one was written during the purge and is not labelled private yet, so this page is kept soft-deleted and parked; the next purge labels it and frees this page, and the one after removes it (gbrain doctor: derived_origin_parked).' };/**- * Inside a purge's delete transaction, after the carry: the origins among `ids` (not already parked) that a page would+ * Inside a purge's delete transaction, after the carry: the origins among `ids` (the bulk paths skip parked ones; the+ * single-page purge passes `recheckParked`, so a parked origin whose carry was skipped is held too) that a page would* still open without, which means a dependent was written after the carry labelled the rest. The label door cannot write* inside this transaction (a managed label is its own publication, a Git label its own commit), so each such origin is* parked here, in the same transaction, and the caller skips its row delete; the next purge labels the page and the one* after removes the origin. The parks written, by origin.*/-export async function parkUncarriedInPurge(tx: BrainEngine, ids: readonly number[]): Promise<DerivedOriginPark[]> {+export async function parkUncarriedInPurge(tx: BrainEngine, ids: readonly number[], opts: { recheckParked?: boolean } = {}): Promise<DerivedOriginPark[]> {if (!ids.length) return [];return withoutCallerAccess(async () => {await readHonorAliases(tx);const origins = (await tx.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 ${originSql('p')} AND ${derivedOriginPurgeHold('p')} ORDER BY p.id`, [[...ids]], bounded))+ WHERE p.id = ANY($1::int[]) AND ${originSql('p')}${opts.recheckParked ? '' : ` AND ${derivedOriginPurgeHold('p')}`} ORDER BY p.id`, [[...ids]], bounded)).map(row => ({ id: Number(row.id), source_id: row.source_id, slug: row.slug }));if (!origins.length) return [];const first = new Map<number, Dependent>();diff --git a/test/derived-origin-carry-private.test.ts b/test/derived-origin-carry-private.test.tsindex b867c0462a..47a3af6f38 100644--- a/test/derived-origin-carry-private.test.ts+++ b/test/derived-origin-carry-private.test.ts@@ -36,6 +36,7 @@ import { bindCredential } from '../src/core/access/members.ts';import { consentedPurgeDeletedPages, purgeDeletedPagesCoordinated, selectPurgeCandidates } from '../src/core/persistence/purge-deleted.ts';import { checkDerivedOriginParked } from '../src/commands/doctor/checks/purge-parks.ts';import { installFaultHook } from '../src/core/persistence/fault-points.ts';+import { submitPageMutation } from '../src/core/persistence/page-mutations.ts';import * as labelWrite from '../src/core/repair/label-write.ts';import * as privateVisibility from '../src/core/search/private-visibility.ts';import type { AuthInfo } from '../src/core/ops/contract.ts';@@ -376,6 +377,23 @@ for (const kind of testBackends()) {for (const who of ['unbound', 'alice'] as const) expect({ who, read: (await call(who, 'get_page', { slug: late })).text.includes(`${canaryOf(k, 'plain')}late`) }).toEqual({ who, read: false });}+ /** Parks the origin the way a failed label does: an explicit purge whose label write fails soft-deletes and parks it. */+ async function parkByFailedLabel(origin: string): Promise<void> {+ const failing = spyOn(labelWrite, 'importAtRevision').mockImplementation(async () => { throw new Error('forced label failure'); });+ try {+ const first = JSON.parse((await localCall('delete_page', { slug: origin, purge: true, request_id: randomUUID(), expected_revision: await revision(origin) })).text);+ expect(first.purge_held?.code).toBe('derived_origin_parked');+ } finally { failing.mockRestore(); }+ expect((await row(origin))?.deleted_at).not.toBeNull();+ expect((await checkDerivedOriginParked(engine)).status).toBe('warn');+ }+ async function expectCarriedHidden(k: string): Promise<void> {+ __resetPrivateVisibilityCacheForTests();+ for (const who of ['unbound', 'alice'] as const) for (const shape of CARRIED) {+ expect({ who, shape, read: (await call(who, 'get_page', { slug: slugOf(k, shape) })).text.includes(canaryOf(k, shape)) }).toEqual({ who, shape, read: false });+ }+ }+test('an explicit purge rechecks inside its transaction: a derived page written after the carry keeps the origin soft-deleted and parked', async () => {const k = 'midsingle';await seed(k);@@ -462,6 +480,52 @@ for (const kind of testBackends()) {for (const shape of CARRIED) expect({ shape, visibility: (await fm(slugOf(k, shape))).visibility }).toEqual({ shape, visibility: 'private' });}, 240_000);+ test('an explicit purge whose origin lookup fails is held: a parked origin is not purged and the pages derived from it stay hidden', async () => {+ const k = 'lookupfail';+ await seed(k);+ const origin = slugOf(k, 'origin');+ await parkByFailedLabel(origin);+ const original = privateVisibility.readHonorAliases;+ let thrown = 0;+ const failing = spyOn(privateVisibility, 'readHonorAliases').mockImplementation(async (...args: Parameters<typeof original>) => {+ if (new Error().stack?.includes('originOf')) { thrown++; throw new Error('forced origin lookup failure'); }+ return original(...args);+ });+ let out: { isError: boolean; text: string };+ try { out = await localCall('delete_page', { slug: origin, purge: true, request_id: randomUUID(), expected_revision: await revision(origin) }); }+ finally { failing.mockRestore(); }+ expect(out.isError ? out.text : '').toBe('');+ const receipt = JSON.parse(out.text);+ expect(receipt.status).not.toBe('purged');+ expect(receipt.purge_held?.code).toBe('derived_origin_unchecked');+ expect(thrown).toBeGreaterThan(0);+ expect((await row(origin))?.deleted_at).not.toBeNull();+ expect((await checkDerivedOriginParked(engine)).status).toBe('warn');+ await expectCarriedHidden(k);+ const retried = JSON.parse((await localCall('delete_page', { slug: origin, purge: true, request_id: randomUUID(), expected_revision: await revision(origin) })).text);+ expect(retried.status).toBe('purged');+ await nothingKept(origin);+ await expectCarriedHidden(k);+ }, 240_000);++ test('the single-page purge transaction rechecks a parked origin: submitted without the carry, it is held and its derived pages stay hidden', async () => {+ const k = 'parkedtx';+ await seed(k);+ const origin = slugOf(k, 'origin');+ await parkByFailedLabel(origin);+ const ctx = { engine, config: { engine: engine.kind }, remote: false, sourceId: SRC, dryRun: false, logger: { info() {}, warn() {}, error() {} } } as never;+ const receipt = await withEnv({ GBRAIN_HOME: home }, async () => submitPageMutation(ctx, { operation: 'delete_page',+ params: { slug: origin, source_id: SRC, purge: true, request_id: randomUUID(), expected_revision: await revision(origin) } }));+ expect(receipt.status).not.toBe('purged');+ expect((receipt.purge_held as { code?: string } | undefined)?.code).toBe('derived_origin_parked');+ expect((await row(origin))?.deleted_at).not.toBeNull();+ await expectCarriedHidden(k);+ const retried = JSON.parse((await localCall('delete_page', { slug: origin, purge: true, request_id: randomUUID(), expected_revision: await revision(origin) })).text);+ expect(retried.status).toBe('purged');+ await nothingKept(origin);+ await expectCarriedHidden(k);+ }, 240_000);+test('a stamp that fails parks the origin: never purged, named by doctor with the page that blocks it, the derived page stays hidden; fixed, the next purge stamps and unparks and the one after purges', async () => {const k = 'park';await seed(k);