Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(persistence): read a snapshot's segmented body at its own generation (#8492)

garrytan0e046e987a51
diff --git a/src/core/page-body/ingest.ts b/src/core/page-body/ingest.ts
index c770a91b6e..43dd9c8ea7 100644
--- a/src/core/page-body/ingest.ts
+++ b/src/core/page-body/ingest.ts
@@ -482,9 +482,13 @@ export async function* readSegmentedBody(engine: BrainEngine, pageId: number, so
}
}
-/** The whole body of a segmented page as strings. Only for pages within the single-string ceiling (callers check `body_bytes`). */
-export async function materializeSegmentedBody(engine: BrainEngine, pageId: number, sourceId: string): Promise<{ compiled_truth: string; timeline: string }> {
+/**
+ * The whole body of a segmented page as strings, at `opts.generation` (default live). Only for pages within the
+ * single-string ceiling (callers check `body_bytes`).
+ */
+export async function materializeSegmentedBody(engine: BrainEngine, pageId: number, sourceId: string,
+ opts: { generation?: number } = {}): Promise<{ compiled_truth: string; timeline: string }> {
const parts: Record<BodyRegion, string[]> = { truth: [], timeline: [] };
- for await (const part of readSegmentedBody(engine, pageId, sourceId)) parts[part.region].push(utf8.decode(part.bytes));
+ for await (const part of readSegmentedBody(engine, pageId, sourceId, opts)) parts[part.region].push(utf8.decode(part.bytes));
return { compiled_truth: parts.truth.join(''), timeline: parts.timeline.join('') };
}
diff --git a/src/core/persistence/segmented-apply.ts b/src/core/persistence/segmented-apply.ts
index 23fe0bdf1e..513b29652a 100644
--- a/src/core/persistence/segmented-apply.ts
+++ b/src/core/persistence/segmented-apply.ts
@@ -70,18 +70,19 @@ export async function segmentedLivePage(engine: Pick<BrainEngine, 'executeRaw'>,
* files within the ceiling) that compare against or merge into the stored
* body. A body above the single-string ceiling is returned as is: any step
* that reads it refuses with `page_body_segmented` (its throwing getters), so
- * such a page is rewritten only by a streamed import.
+ * such a page is rewritten only by a streamed import. `generation` reads that body generation (a snapshot's) instead
+ * of the live one.
*/
-export async function materializedPage<T extends Page>(engine: BrainEngine, page: T): Promise<T>;
-export async function materializedPage<T extends Page>(engine: BrainEngine, page: T | null): Promise<T | null>;
-export async function materializedPage<T extends Page>(engine: BrainEngine, page: T | null): Promise<T | null> {
+export async function materializedPage<T extends Page>(engine: BrainEngine, page: T, generation?: number): Promise<T>;
+export async function materializedPage<T extends Page>(engine: BrainEngine, page: T | null, generation?: number): Promise<T | null>;
+export async function materializedPage<T extends Page>(engine: BrainEngine, page: T | null, generation?: number): Promise<T | null> {
if (!page || bodyStorageOf(page) !== 'segmented' || typeof Object.getOwnPropertyDescriptor(page, 'compiled_truth')?.value === 'string') return page;
const bytes = (page as { body_bytes?: number | string | null }).body_bytes
?? (await engine.executeRaw<{ body_bytes: string }>('SELECT body_bytes::text AS body_bytes FROM pages WHERE id=$1', [page.id]))[0]?.body_bytes;
if (Number(bytes ?? 0) > PAGE_MAX_BYTES) return page;
const copy: Record<string, unknown> = { body_storage: 'segmented' };
for (const key of Object.keys(page)) if (!BODY_KEYS.has(key)) copy[key] = (page as Record<string, unknown>)[key];
- Object.assign(copy, await materializeSegmentedBody(engine, Number(page.id), page.source_id));
+ Object.assign(copy, await materializeSegmentedBody(engine, Number(page.id), page.source_id, generation === undefined ? {} : { generation }));
return copy as T;
}
@@ -94,10 +95,12 @@ export function segmentedOverCeiling(page: { body_storage?: string | null; body_
* `materializedPage` over a snapshot (null passes through), with the read-back body under the snapshot's withdrawal
* ledger, as readPageSnapshot gives an inline page's body: a facts row the ledger withdraws is struck (or, purged,
* dropped), so the body a write merges into or the drift check compares a file with is the one an inline page has.
+ * The body is the snapshot's generation (#8492), never one published after it; when that generation's rows are
+ * gone the read refuses `page_body_rows_unreadable`.
*/
export async function materializedSnapshot(engine: BrainEngine, snapshot: PageSnapshot | null): Promise<PageSnapshot | null> {
if (!snapshot || bodyStorageOf(snapshot.page) !== 'segmented') return snapshot;
- const page = await materializedPage(engine, snapshot.page);
+ const page = await materializedPage(engine, snapshot.page, snapshot.bodyGeneration);
if (page === snapshot.page) return snapshot;
Object.assign(page, await overlayCanonicalBodies(engine.executeRaw.bind(engine), page.compiled_truth, page.timeline ?? '', snapshot.withdrawals,
{ sourceId: page.source_id, marker: snapshot.globalPurges }));
diff --git a/test/large-pages/snapshot-body-generation-8492.test.ts b/test/large-pages/snapshot-body-generation-8492.test.ts
new file mode 100644
index 0000000000..07f620d118
--- /dev/null
+++ b/test/large-pages/snapshot-body-generation-8492.test.ts
@@ -0,0 +1,52 @@
+/**
+ * #8492 item 1: `materializedSnapshot` reads a segmented page's body at the generation its snapshot was read with.
+ *
+ * A snapshot holds one revision's title, frontmatter and withdrawal ledger; the drift check, the managed-sync freeze
+ * screen and local-edit adoption compare a file with it read back whole. The body used to be read from the live
+ * generation, so a replacement published after the snapshot gave the old metadata the new body. When the snapshot's
+ * generation can no longer be read, the read refuses `page_body_rows_unreadable` instead of answering with the new body.
+ */
+import { afterAll, beforeAll, beforeEach, expect, test } from 'bun:test';
+import { PGLiteEngine } from '../../src/core/pglite-engine.ts';
+import { importFromContent } from '../../src/core/import-file.ts';
+import { materializedSnapshot } from '../../src/core/persistence/segmented-apply.ts';
+
+const SLUG = 'notes/alice-example';
+let engine: PGLiteEngine;
+
+/** Over the inline bound, so the page is stored in segments; `tag` marks the generation's text. */
+const file = (tag: string) => `---\ntitle: Alice Example ${tag}\ntype: concept\n---\n\n${tag} intro.\n\n${
+ Array.from({ length: 16_000 }, (_, i) => `- ${String(i).padStart(9, '0')} ${tag} acme-example journal line with enough text to fill a segment\n`).join('')}`;
+const page = async () => (await engine.executeRaw<{ id: number; body_storage: string; body_generation: number }>(
+ 'SELECT id, body_storage, body_generation FROM pages WHERE source_id=$1 AND slug=$2', ['default', SLUG]))[0]!;
+
+beforeAll(async () => {
+ engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema();
+ await engine.setConfig('pages.segmented_storage', 'on');
+}, 120_000);
+afterAll(async () => { await engine.disconnect(); });
+beforeEach(async () => { await engine.executeRaw('DELETE FROM pages'); });
+
+async function snapshotThenReplace() {
+ await importFromContent(engine, SLUG, file('genone'), { sourceId: 'default', noEmbed: true });
+ const snapshot = (await engine.readPageSnapshot(SLUG, { sourceId: 'default' }))!;
+ expect({ storage: (await page()).body_storage, generation: snapshot.bodyGeneration }).toEqual({ storage: 'segmented', generation: 1 });
+ await importFromContent(engine, SLUG, file('gentwo'), { sourceId: 'default', noEmbed: true });
+ expect(Number((await page()).body_generation)).toBe(2);
+ return snapshot;
+}
+
+test('a snapshot read back whole keeps its own generation\'s body after a replacement is published', async () => {
+ const snapshot = await snapshotThenReplace();
+ const read = (await materializedSnapshot(engine, snapshot))!;
+ expect(read.page.title).toBe('Alice Example genone');
+ expect(read.page.compiled_truth.startsWith('genone intro.')).toBe(true);
+ expect(read.page.compiled_truth).not.toContain('gentwo');
+}, 120_000);
+
+test('a snapshot whose generation is no longer readable refuses instead of reading the newer body', async () => {
+ const snapshot = await snapshotThenReplace();
+ await engine.executeRaw('DELETE FROM page_body_manifest WHERE page_id=$1 AND gen_to IS NOT NULL', [(await page()).id]);
+ const read = materializedSnapshot(engine, snapshot).then(s => s!.page.compiled_truth.slice(0, 12));
+ await expect(read).rejects.toMatchObject({ code: 'page_body_rows_unreadable' });
+}, 120_000);