Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: ingest leaves a soft-deleted page at a name's natural slug deleted and restorable
garrytan2fb5f50e7a63
diff --git a/src/core/ingest/plan.ts b/src/core/ingest/plan.tsindex db9e90b58f..8455131bed 100644--- a/src/core/ingest/plan.ts+++ b/src/core/ingest/plan.ts@@ -15,6 +15,7 @@ import { resolveExcludePrivatePages, privatePagesFilterFragment } from '../searcimport { canonicalTypeOf, loadSourcePack } from '../mentions/policy.ts';import { checkFactAgainstSaved } from '../facts/backstop.ts';import { INGEST_ENTITY_CAP, isJunkName, type ExtractedEntity, type ExtractedFact } from './extract.ts';+import { INGEST_ATTEMPT_OP } from './checkpoint.ts';export type PagePlanStatus = 'existing' | 'create' | 'propose' | 'skipped';export interface PlannedPage {@@ -71,11 +72,19 @@ export function firstMention(text: string, name: string): number {}interface SlugState { exists: boolean; readable: boolean }+/**+ * A soft-deleted page still occupies its slug (it stays restorable), unless an undone ingest attempt created it:+ * re-ingesting after an undo reuses that slug.+ */async function slugState(ctx: OperationContext, sourceId: string, slug: string, excludePrivate: boolean): Promise<SlugState> {- const rows = await ctx.engine.executeRaw<{ readable: boolean }>(- 'SELECT ' + (excludePrivate ? privatePagesFilterFragment('p') : 'true') + ' AS readable FROM pages p WHERE p.source_id = $1 AND p.slug = $2 AND p.deleted_at IS NULL',- [sourceId, slug]);- return { exists: rows.length > 0, readable: rows.length > 0 && rows[0]!.readable === true };+ const rows = await ctx.engine.executeRaw<{ readable: boolean; live: boolean; undone: boolean }>(+ 'SELECT ' + (excludePrivate ? privatePagesFilterFragment('p') : 'true') + ' AS readable, p.deleted_at IS NULL AS live,'+ + " EXISTS (SELECT 1 FROM op_checkpoints c WHERE c.op = $3 AND c.fingerprint = p.frontmatter->>'ingest_attempt'"+ + " AND c.completed_keys->0->>'phase' = 'undone') AS undone FROM pages p WHERE p.source_id = $1 AND p.slug = $2",+ [sourceId, slug, INGEST_ATTEMPT_OP]);+ const row = rows[0];+ if (!row || (!row.live && row.undone)) return { exists: false, readable: false };+ return { exists: true, readable: row.live && row.readable === true };}function insideGrant(ctx: OperationContext, slug: string): boolean {diff --git a/test/e2e/ingest-postgres.test.ts b/test/e2e/ingest-postgres.test.tsindex 6de1603580..743a3eddd0 100644--- a/test/e2e/ingest-postgres.test.ts+++ b/test/e2e/ingest-postgres.test.ts@@ -64,6 +64,7 @@ d('ingest and ingest_undo (live Postgres)', () => {test('golden transcript (' + label + ')', async () => { fx = await fixture(storage); await S.goldenScenario(engine, fx); }, 90_000);test('undo through the fence (' + label + ')', async () => { fx = await fixture(storage); await S.undoScenario(engine, fx); }, 120_000);test('keyless run (' + label + ')', async () => { fx = await fixture(storage); await S.keylessScenario(engine, fx); }, 60_000);+ test('a soft-deleted page stays deleted (' + label + ')', async () => { fx = await fixture(storage); await S.deletedPageScenario(engine); }, 60_000);test('dry run (' + label + ')', async () => { fx = await fixture(storage); await S.dryRunScenario(engine); }, 60_000);test('trusted extraction (' + label + ')', async () => { fx = await fixture(storage); await S.trustedScenario(engine); }, 60_000);test('budget stop then resume (' + label + ')', async () => { fx = await fixture(storage); await S.budgetResumeScenario(engine); }, 60_000);diff --git a/test/helpers/ingest-scenarios.ts b/test/helpers/ingest-scenarios.tsindex d5562b0fe3..23624f2d35 100644--- a/test/helpers/ingest-scenarios.ts+++ b/test/helpers/ingest-scenarios.ts@@ -639,3 +639,28 @@ export async function reportReadersScenario(engine: BrainEngine): Promise<void>} finally { stub.restore(); }}++/** A soft-deleted page at a name's natural slug stays deleted and restorable: ingest files the name beside it instead of reviving it. */+export async function deletedPageScenario(engine: BrainEngine): Promise<void> {+ await seedEntities(engine);+ const original = '---\ntype: company\ntitle: ACMX\n---\n\nACMX is a page the owner deleted.\n';+ await operationsByName.put_page!.handler(reviewCtx(engine), { slug: 'companies/acmx', content: original });+ await drain(engine);+ const live = await engine.readPageSnapshot('companies/acmx', { sourceId: 'default' });+ await operationsByName.delete_page!.handler(reviewCtx(engine), { slug: 'companies/acmx', expected_revision: live!.revision });+ await drain(engine);+ resetGateway();+ configureGateway({ env: {} } as never);+ try {+ const report = await ingest(engine, { content: 'We met the team at ACMX on Tuesday to talk about pricing.' });+ expect(report.status).toBe('complete');+ const acmx = report.pages.find((p: Report) => p.name === 'ACMX');+ expect(acmx.status).toBe('proposed');+ expect(acmx.slug).not.toBe('companies/acmx');+ expect(acmx.slug.startsWith('companies/acmx-')).toBe(true);+ const deleted = await engine.readPageSnapshot('companies/acmx', { sourceId: 'default', includeDeleted: true });+ expect(deleted?.page.deleted_at).toBeTruthy();+ expect(deleted?.page.compiled_truth).toContain('ACMX is a page the owner deleted.');+ } finally { resetGateway(); }+}+diff --git a/test/ingest.serial.test.ts b/test/ingest.serial.test.tsindex 69eed3b781..525977e39d 100644--- a/test/ingest.serial.test.ts+++ b/test/ingest.serial.test.ts@@ -52,6 +52,10 @@ for (const storage of ['git', 'database'] as ReviewStorage[]) {fx = await reviewDoorFixture(engine, storage);await S.keylessScenario(engine, fx);}, 60_000);+ test('a soft-deleted page at the natural slug stays deleted and restorable; the name is filed beside it', async () => {+ fx = await reviewDoorFixture(engine, storage);+ await S.deletedPageScenario(engine);+ }, 60_000);test('dry run makes no provider call and writes nothing', async () => {fx = await reviewDoorFixture(engine, storage);await S.dryRunScenario(engine);