Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(repair): extractor-facts restores only onto the previewed page id

garrytane01e9824dc64
diff --git a/src/core/repair/extractor-facts.ts b/src/core/repair/extractor-facts.ts
index 9b0be57960..22aed9e3bb 100644
--- a/src/core/repair/extractor-facts.ts
+++ b/src/core/repair/extractor-facts.ts
@@ -359,9 +359,11 @@ async function restorePageFacts(tx: BrainEngine, page: ExtractorFactsPage, manag
const ids = page.facts.map(f => f.id);
await tx.executeRaw('SELECT id FROM facts WHERE source_id=$1 AND id=ANY($2::bigint[]) FOR UPDATE', [page.source_id, ids]);
const live = new Map((await classifyExtractorFacts(tx, [page.source_id], { managed, slug: page.slug })).map(f => [f.id, f]));
- const [snapshot] = await tx.executeRaw<{ compiled_truth: string | null; max_row: number | string | null }>(
- `SELECT p.compiled_truth, (SELECT MAX(row_num) FROM facts WHERE source_id=$1 AND source_markdown_slug=$2) AS max_row
+ const [live_page] = await tx.executeRaw<{ id: number | string; compiled_truth: string | null; max_row: number | string | null }>(
+ `SELECT p.id, p.compiled_truth, (SELECT MAX(row_num) FROM facts WHERE source_id=$1 AND source_markdown_slug=$2) AS max_row
FROM pages p WHERE p.source_id=$1 AND p.slug=$2 AND p.deleted_at IS NULL`, [page.source_id, page.slug]);
+ // A page deleted and recreated at the same slug since the preview is another page.
+ const snapshot = live_page && Number(live_page.id) === page.page_id ? live_page : undefined;
const fenceRows = parseFactsFence(snapshot?.compiled_truth ?? '').facts.map(f => f.rowNum);
let next = Math.max(Number(snapshot?.max_row ?? 0), ...fenceRows, 0) + 1;
const result: RestoreResult = { restored: [], changed: [] };
diff --git a/test/repair-extractor-facts.test.ts b/test/repair-extractor-facts.test.ts
index 61085b4e99..3fc2f2cf93 100644
--- a/test/repair-extractor-facts.test.ts
+++ b/test/repair-extractor-facts.test.ts
@@ -432,5 +432,18 @@ for (const backend of testBackends()) {
expect(await activeIds(engine, [...first, ...second])).toEqual([first[0], second[0]]);
expect((await refusal(() => repair(engine, home, apply))).code).toBe('preview_changed');
}, 60_000);
+
+ test('a page deleted and recreated at the same slug since the preview restores nothing (Codex review)', async () => {
+ const sourceId = `ef-r-${randomUUID().slice(0, 8)}`;
+ await engine.executeRaw('INSERT INTO sources(id,name) VALUES($1,$1)', [sourceId]);
+ const ids = await conversation(engine, 'conversations/recreated', ['Alice sends the deck'], undefined, sourceId);
+ await engine.executeRaw('UPDATE facts SET expired_at=now(), row_num=NULL WHERE source_id=$1', [sourceId]);
+ const wide = await repair(engine, home, ['--source', sourceId, '--include-ambiguous']);
+ await engine.deletePage('conversations/recreated', { sourceId });
+ await engine.putPage('conversations/recreated', { type: 'note', title: 'Another page', compiled_truth: 'Unrelated.' }, { sourceId });
+ const applied = await repair(engine, home, ['--source', sourceId, '--include-ambiguous', '--apply', '--expect', hashOf(wide)]);
+ expect(applied.results[0].outcomes).toEqual({ changed_since_preview: 1 });
+ expect(await activeIds(engine, ids)).toEqual([]);
+ }, 60_000);
});
}