Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(takes): a takes rebuild keeps each struck take's superseded-by link

garrytancd083b9c0e64
diff --git a/src/core/cycle/extract-takes.ts b/src/core/cycle/extract-takes.ts
index a975b89061..facbb6f6f4 100644
--- a/src/core/cycle/extract-takes.ts
+++ b/src/core/cycle/extract-takes.ts
@@ -172,22 +172,6 @@ function takesFenceRead(page: { body_storage?: BodyStorage | null; compiled_trut
return isSegmentedPage(page) ? { state: 'segmented' } : readFenceBlocks(page, 'takes');
}
-function parsedTakeToBatchInput(pageId: number, t: ParsedTake): TakeBatchInput {
- return {
- page_id: pageId,
- row_num: t.rowNum,
- claim: t.claim,
- kind: t.kind,
- holder: t.holder,
- weight: t.weight,
- since_date: t.sinceDate,
- until_date: t.untilDate,
- source: t.source,
- active: t.active,
- superseded_by: null,
- };
-}
-
const BATCH_SIZE = 100;
/**
@@ -311,7 +295,7 @@ export async function extractTakesFromFs(
result.pagesWithTakes++;
for (const t of takes) {
- buffer.push(parsedTakeToBatchInput(pageId, t));
+ buffer.push(takesPreparation.toCanonicalBatchInput(pageId, t));
if (buffer.length >= BATCH_SIZE) await flushBatch(engine, buffer, result, dryRun);
}
}
@@ -368,13 +352,13 @@ export async function extractTakesFromDb(
if (covered.has(source_id)) {
await withUnmanagedBeliefs(engine, source_id, [slug], 'takes_reextract', run => maintenanceTransaction(engine, tx => run(tx, async () => {
const takes = await reconcilePageTakes(tx, true, page, read, slug, rebuild, false, result);
- result.takesUpserted += await upsertProjectedTakes(engine, takes.map(t => parsedTakeToBatchInput(page.id, t)), result, tx);
+ result.takesUpserted += await upsertProjectedTakes(engine, takes.map(t => takesPreparation.toCanonicalBatchInput(page.id, t)), result, tx);
})));
continue;
}
const takes = await reconcilePageTakes(engine, false, page, read, slug, rebuild, dryRun, result);
for (const t of takes) {
- buffer.push(parsedTakeToBatchInput(page.id, t));
+ buffer.push(takesPreparation.toCanonicalBatchInput(page.id, t));
if (buffer.length >= BATCH_SIZE) await flushBatch(engine, buffer, result, dryRun);
}
}
diff --git a/test/takes-reconcile-superseded-pointer.test.ts b/test/takes-reconcile-superseded-pointer.test.ts
new file mode 100644
index 0000000000..255496e5f6
--- /dev/null
+++ b/test/takes-reconcile-superseded-pointer.test.ts
@@ -0,0 +1,50 @@
+/**
+ * #8390: a takes rebuild keeps the supersession pointer the fence records.
+ *
+ * Protects: `extractTakesFromDb` (plain and rebuild) stores
+ * `superseded_by = N` for an inactive fence row whose source cell says
+ * `superseded by #N`, the same row the page-write path projects.
+ * Fails when: the reconcile path builds its batch rows with
+ * `superseded_by: null`, so a rebuild wipes the pointer takes_supersede wrote.
+ * Seams: none; real PGLite.
+ */
+import { afterAll, beforeAll, expect, test } from 'bun:test';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { extractTakesFromDb } from '../src/core/cycle/extract-takes.ts';
+import { renderTakesFence } from '../src/core/takes-fence.ts';
+
+let engine: PGLiteEngine;
+let pageId: number;
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+ const page = await engine.putPage('companies/acme-example', {
+ title: 'Acme Example',
+ type: 'company',
+ compiled_truth: `# Acme Example\n\n## Takes\n\n${renderTakesFence([
+ { rowNum: 1, claim: 'Acme example ships in Q3', kind: 'bet', holder: 'world', weight: 0.5, source: 'call notes; superseded by #2', active: false },
+ { rowNum: 2, claim: 'Acme example ships in Q4', kind: 'bet', holder: 'world', weight: 0.6, active: true },
+ ])}\n`,
+ });
+ pageId = page.id;
+}, 120_000);
+
+afterAll(async () => {
+ await engine.disconnect();
+});
+
+const pointer = async (rowNum: number) => {
+ const [row] = await engine.executeRaw<{ superseded_by: number | string | null }>(
+ 'SELECT superseded_by FROM takes WHERE page_id = $1 AND row_num = $2', [pageId, rowNum]);
+ return row?.superseded_by == null ? null : Number(row.superseded_by);
+};
+
+for (const rebuild of [false, true]) {
+ test(`extractTakesFromDb (rebuild=${rebuild}) stores the fence's superseded-by pointer`, async () => {
+ await extractTakesFromDb(engine, { slugs: ['companies/acme-example'], rebuild });
+ expect(await pointer(1)).toBe(2);
+ expect(await pointer(2)).toBeNull();
+ });
+}