Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Keep derived pages private when their origin is purged by a mixed-case slug, alongside a deleted derived page, or past 200 expired origins

garrytan99f0f3072e0c
diff --git a/src/core/repair/origin-carry.ts b/src/core/repair/origin-carry.ts
index 717adc488d..171ffdc072 100644
--- a/src/core/repair/origin-carry.ts
+++ b/src/core/repair/origin-carry.ts
@@ -32,7 +32,7 @@ const EDIT: LabelEdit = { set: { visibility: 'private' }, remove: [] };
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 Dependent { id: number; origin_id: number; source_id: string; slug: string; deleted: boolean }
interface Failure { code: string | null; reason: string }
export interface CarryBlock extends Failure { slug: string | null }
@@ -40,10 +40,10 @@ const failureOf = (error: unknown): Failure => ({ code: typeof (error as { code?
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`,
+ const rows = await engine.executeRaw<{ id: number; cleared_id: number; source_id: string; slug: string; deleted: boolean }>(
+ `SELECT d.id, d.cleared_id, p.source_id, p.slug, p.deleted_at IS NOT NULL AS deleted 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 }));
+ return rows.map(row => ({ id: Number(row.id), origin_id: Number(row.cleared_id), source_id: row.source_id, slug: row.slug, deleted: row.deleted === true }));
}
/** Labels each page private through the label door; the failures by page id. */
@@ -62,9 +62,12 @@ async function stamp(engine: BrainEngine, pages: Dependent[], batch: string): Pr
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)); });
+ for (let i = 0; i < group.length; i += 200) {
+ const part = group.slice(i, i + 200);
+ const settled = await publishMaintenancePages(engine, authority, part.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(part[index]!.page.id, failureOf(entry.error)); });
+ }
continue;
}
const files = await filesInCheckout(engine, group.map(entry => entry.page.id));
@@ -84,7 +87,8 @@ async function stamp(engine: BrainEngine, pages: Dependent[], batch: string): Pr
/**
* 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.
+ * blocked afterwards, by origin id, with the first page that still would open and why. A soft-deleted dependent is not
+ * labelled: it blocks its origin until it is purged or restored. 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();
@@ -93,7 +97,9 @@ export async function carryPrivate(engine: BrainEngine, origins: Origin[], batch
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 failed = await stamp(engine, pages.filter(page => !page.deleted), batch);
+ for (const page of pages) if (page.deleted) failed.set(page.id, { code: 'dependent_soft_deleted',
+ reason: `${page.slug} is soft-deleted and private only through this page; kept until ${page.slug} is purged or restored.` });
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,
@@ -134,7 +140,7 @@ export async function deletePageCarryingPrivate(ctx: OperationContext, p: Record
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;
+ const origin = typeof p.slug === 'string' ? await originOf(ctx.engine, sourceId, p.slug.toLowerCase()) : 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);
@@ -155,8 +161,9 @@ export async function deletePageCarryingPrivate(ctx: OperationContext, p: Record
}
/**
- * 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.
+ * Before a purge: the expired soft-deleted origins get their dependents labelled private, judged against every expired
+ * origin at once (a page private through two of them opens only when both go); 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);
@@ -168,16 +175,14 @@ export async function carryPrivateBeforePurge(engine: BrainEngine, olderThanHour
});
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));
+ const expired = origins.map(row => ({ id: Number(row.id), source_id: row.source_id, slug: row.slug }));
+ const blocked = await carryPrivate(engine, expired, batch);
+ for (const origin of expired) {
+ 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, expired.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 2802f18902..b4de44fa36 100644
--- a/src/core/repair/private-labels.ts
+++ b/src/core/repair/private-labels.ts
@@ -237,10 +237,12 @@ async function planRestore(engine: BrainEngine, scope: RepairScope, selection: S
* 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). A page that names readers is judged by the same privacy rules,
- * since readable_by never relaxes them: it is returned when the removal would open it to its listed readers.
+ * since readable_by never relaxes them: it is returned when the removal would open it to its listed readers. A soft-deleted
+ * page is a candidate too, since a restore brings it back with the origin row gone.
*/
export function dependentsSql(mode: 'clear' | 'remove' = 'clear'): string {
const remove = mode === 'remove';
+ const live = (sql: string) => remove ? '' : sql;
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
@@ -259,10 +261,10 @@ export function dependentsSql(mode: 'clear' | 'remove' = 'clear'): string {
provenance AS MATERIALIZED (SELECT l.from_page_id, x.cleared_id, x.source_id
FROM (SELECT c.id, c.id AS cleared_id, c.source_id FROM cleared c UNION ALL SELECT h.id, h.cleared_id, h.source_id FROM hits h WHERE h.kind = 'source_slug') x
JOIN links l ON l.to_page_id = x.id WHERE l.link_source = 'concept-provenance' AND l.link_type = 'synthesized_from'),
- ties AS (SELECT h.id, h.cleared_id FROM hits h WHERE h.deleted_at IS NULL
+ ties AS (SELECT h.id, h.cleared_id FROM hits h${live(' WHERE h.deleted_at IS NULL')}
UNION ALL SELECT r.id, h.cleared_id FROM hits h JOIN refs r ON r.source_id = h.source_id AND r.kind = 'depth' AND r.ref = h.slug
- WHERE h.kind = 'declared' AND r.deleted_at IS NULL
- UNION ALL SELECT p.id, t.cleared_id FROM provenance t JOIN pages p ON p.id = t.from_page_id AND p.source_id = t.source_id AND p.deleted_at IS NULL),
+ WHERE h.kind = 'declared'${live(' AND r.deleted_at IS NULL')}
+ UNION ALL SELECT p.id, t.cleared_id FROM provenance t JOIN pages p ON p.id = t.from_page_id AND p.source_id = t.source_id${live(' AND p.deleted_at IS NULL')}),
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
diff --git a/test/derived-origin-carry-private.test.ts b/test/derived-origin-carry-private.test.ts
index d3fe696980..9faf57efb6 100644
--- a/test/derived-origin-carry-private.test.ts
+++ b/test/derived-origin-carry-private.test.ts
@@ -242,6 +242,59 @@ for (const kind of testBackends()) {
await expectCarried(k, base.before, base.state, base.proposals);
}, 240_000);
+ test('a purge sent with a mixed-case slug stamps the derived pages first, as the lowercase slug does', async () => {
+ const k = 'mixedcase';
+ await seed(k);
+ const base = await baseline(k);
+ const origin = slugOf(k, 'origin');
+ const out = await localCall('delete_page', { slug: 'MixedCase/Origin', purge: true, request_id: randomUUID(), expected_revision: await revision(origin) });
+ expect(out.isError ? out.text : '').toBe('');
+ expect(JSON.parse(out.text).status).toBe('purged');
+ await nothingKept(origin);
+ await expectCarried(k, base.before, base.state, base.proposals);
+ }, 240_000);
+
+ test('a derived page soft-deleted when its origin expires parks the origin; restored, it is stamped before the origin goes', async () => {
+ const k = 'tomb';
+ await seed(k);
+ const origin = slugOf(k, 'origin'), digest = slugOf(k, 'digest');
+ await softDelete(origin);
+ await backdate(origin);
+ await softDelete(digest);
+ const held = await purgeDeletedPagesCoordinated(engine, 72, { automatic: true });
+ expect(held.slugs).not.toContain(origin);
+ expect(held.blocked.find(b => b.slug === origin)?.code).toBe('derived_origin_parked');
+ expect((await row(origin))?.deleted_at).not.toBeNull();
+ const doctor = await checkDerivedOriginParked(engine);
+ expect((doctor.details as { origins: Array<{ slug: string; blocking_slug: string }> }).origins)
+ .toContainEqual(expect.objectContaining({ slug: origin, blocking_slug: digest }));
+ const restored = await localCall('restore_page', { slug: digest, request_id: randomUUID(), expected_revision: await revision(digest) });
+ expect(restored.isError ? restored.text : '').toBe('');
+ const result = await purgeDeletedPagesCoordinated(engine, 72, { automatic: true });
+ expect(result.slugs).toContain(origin);
+ await nothingKept(origin);
+ expect((await fm(digest)).visibility).toBe('private');
+ __resetPrivateVisibilityCacheForTests();
+ for (const who of ['unbound', 'alice'] as const) expect({ who, seen: await seen(who, k, 'digest') }).toEqual({ who, seen: none });
+ expect({ owner: await seen('owner', k, 'digest') }).toEqual({ owner: every });
+ }, 240_000);
+
+ test('a page private through two expired origins more than 200 apart is stamped before both rows go', async () => {
+ const k = 'wide', first = 'wide/first', last = 'wide/last', page = 'wide/joined';
+ const privatePage = (slug: string) => importFromContent(engine, slug, ['---', `title: ${slug}`, 'type: note', 'visibility: private', '---', `# ${slug}`, '', 'Private origin.'].join('\n'), { sourceId: SRC, noEmbed: true });
+ expect((await privatePage(first)).status).toBe('imported');
+ for (let i = 0; i < 199; i++) expect((await privatePage(`wide/filler-${i}`)).status).toBe('imported');
+ expect((await privatePage(last)).status).toBe('imported');
+ expect((await importFromContent(engine, page, ['---', 'title: joined', 'type: note', `derived_from: [${first}.md, ${last}.md]`, '---', '# joined', '',
+ `Harbor ledger planning notes ${canaryOf(k, 'digest')} for the roadmap.`].join('\n'), { sourceId: SRC, noEmbed: true })).status).toBe('imported');
+ await engine.executeRaw("UPDATE pages SET deleted_at = now() - interval '100 hours' WHERE source_id=$1 AND slug LIKE 'wide/%' AND slug <> $2", [SRC, page]);
+ const result = await purgeDeletedPagesCoordinated(engine, 72, { automatic: true });
+ expect(result.slugs).toEqual(expect.arrayContaining([first, last]));
+ expect((await fm(page)).visibility).toBe('private');
+ __resetPrivateVisibilityCacheForTests();
+ for (const who of ['unbound', 'alice'] as const) expect({ who, get_page: (await call(who, 'get_page', { slug: page })).text.includes(canaryOf(k, 'digest')) }).toEqual({ who, get_page: false });
+ }, 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 purges', async () => {
const k = 'park';
await seed(k);