Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Fix lane Y: restore_page decides its reseal before publication, so a statement-timeout retry never re-streams its file

garrytane3e351ad58f5
diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.ts
index 9509e41e2c..1d5a656602 100644
--- a/src/core/persistence/page-prepare.ts
+++ b/src/core/persistence/page-prepare.ts
@@ -495,6 +495,11 @@ async function prepareDeletePageMutation(engine: BrainEngine, row: WriteRequest,
* delete only set `deleted_at` (its body, chunks, links and projections stay), so the restore clears it without
* re-serializing or re-importing the body: version, clear, reseal (or queue the projection when chunks are missing or a
* deferred retirement left rows of retired occurrences).
+ * Whether it reseals is decided here, before publication: both probes read one index entry per live occurrence, so on
+ * a loaded host they can outlast the publication transaction's 5 s statement_timeout, and a cancelled publication is
+ * prepared and its whole file streamed again. Deciding here is exact: the transaction publishes only at the observed
+ * revision, so the page is still soft-deleted at the same body generation, and a deleted page gains no chunk rows
+ * (its projection skips it) and loses only rows its live generation does not hold.
* The canonical file is serializePageToMarkdown's bytes streamed from the segments by export's serializer into one
* buffer, never one string. The restore adds no text, so an agent-tier writer's advisory gate scan (flag only) is not
* re-run over the stored body; a step that needs the whole body refuses (`page_body_segmented`): a core page's budget
@@ -516,7 +521,10 @@ async function prepareUnreadRestore(engine: BrainEngine, row: WriteRequest, snap
const file = await prepareFileTarget(engine, row, snapshot, await unreadPageFile(engine, snapshot), undefined,
{ allowMissing: options.allowMissingFile, activePack: options.activePack, remote: row.authority.remote });
const pageId = Number(snapshot.page.id);
- let complete = true;
+ enterClaimStep(options.clock, 'seal_probe', undefined, 'db');
+ const generation = Number(snapshot.bodyGeneration), live = noop ? [] : await engine.readManifest(pageId, { generation });
+ const complete = noop || !(await occurrencesWithoutChunks(engine, pageId, generation, live)).length
+ && !await hasStaleChunks(engine, pageId, generation, live.map(row => Number(row.occurrenceId)));
const prepared: PreparedMutation = { observedRevision: snapshot.revision, noop, file, ...await pageDatabaseOnlyPublication(engine, row, file),
...(core ? { exclusiveSources: core.exclusiveSources } : {}), ...(trust ? { trust } : {}),
validate: async tx => { await core?.validate(tx); },
@@ -525,11 +533,6 @@ async function prepareUnreadRestore(engine: BrainEngine, row: WriteRequest, snap
await tx.createVersion(row.slug, source);
await tx.restorePage(row.slug, source);
const lowered = storedTier ? await recordAgentPageLowering(tx, row, { tier: storedTier, revision: snapshot.revision }) : null;
- const [page] = await tx.executeRaw<{ body_generation: number | string }>('SELECT body_generation FROM pages WHERE id=$1', [pageId]);
- const generation = Number(page!.body_generation), live = await tx.readManifest(pageId);
- // Unsealed while an occurrence lacks chunks or a deferred retirement left rows of retired occurrences: the projection retires them first.
- complete = !(await occurrencesWithoutChunks(tx, pageId, generation, live)).length
- && !await hasStaleChunks(tx, pageId, generation, live.map(row => Number(row.occurrenceId)));
if (complete) await sealSegmentedPage(tx, pageId);
if (core) await core.record(tx, (await tx.readPageSnapshot(row.slug, source))?.revision ?? null);
return { ...pageNoopAdvisories(row), status: 'restored', slug: row.slug, source_id: row.source_id, chunks: 0, noop: false,
diff --git a/test/large-pages/restore-publication-statement-bound.test.ts b/test/large-pages/restore-publication-statement-bound.test.ts
new file mode 100644
index 0000000000..b3e7d4de43
--- /dev/null
+++ b/test/large-pages/restore-publication-statement-bound.test.ts
@@ -0,0 +1,143 @@
+/**
+ * restore_page of a file-backed segmented page over the single-string ceiling writes its canonical file once, even
+ * when the publication transaction's statement bound ends a statement (Wave B, PR 2 fix lane Y).
+ *
+ * 1. Protects: the restore's publication transaction runs no read whose cost grows with the page (the probes for
+ * occurrences without chunks and for stale chunk rows, one index probe per occurrence). Those decide whether the
+ * restore seals the page, and run in preparation; the transaction only seals. The page still ends sealed, with its
+ * file streamed into its staging file exactly once.
+ * 2. Fails when: a probe runs inside the publication transaction, where `declareDurablePersistence`'s 5 s
+ * `statement_timeout` ends it on a loaded host (57014). That failure is transient: recovery requeues the request,
+ * and the owner prepares it again and streams the whole file again, once per cancelled attempt (CI stress saw a
+ * 90 MiB restore written two and four times, one cancelled stale-chunk probe per extra write). The probe here
+ * makes the first in-transaction run of either probe end as the server ends it (57014); without the fix the file
+ * is streamed twice.
+ * 3. Existing coverage: segmented-delete-restore-file.slow.test.ts checks a restore streams its file once, on an idle
+ * host only; restore-segmented-reprojects.test.ts and restore-deferred-retire.test.ts check the seal decision.
+ * 4. Forced with an engine view whose transactions fail the first probe statement; counts, never time.
+ * Runs on PGLite, and on Postgres when DATABASE_URL is set.
+ */
+import { afterAll, describe, expect, test, beforeAll } from 'bun:test';
+import { randomUUID } from 'node:crypto';
+import { execFileSync } from 'node:child_process';
+import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync, statSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { PGLiteEngine } from '../../src/core/pglite-engine.ts';
+import type { BrainEngine } from '../../src/core/engine.ts';
+import type { OperationContext } from '../../src/core/operations.ts';
+import { PAGE_MAX_BYTES } from '../../src/core/page-size-policy.ts';
+import { claimWorktree } from '../../src/core/persistence/ownership.ts';
+import { registerLocalWriter } from '../../src/core/persistence/identity.ts';
+import { importManagedFile } from '../../src/core/persistence/import-mutations.ts';
+import { submitPageMutation } from '../../src/core/persistence/page-mutations.ts';
+import { disposePersistenceConsumer } from '../../src/core/persistence/service.ts';
+import { largeFileWork } from '../../src/core/persistence/large-file.ts';
+import { isolatedPersistencePostgres } from '../helpers/persistence-postgres.ts';
+import { testBackends } from '../helpers/test-backends.ts';
+import { withEnv } from '../helpers/with-env.ts';
+import { fileSha, writeCanonicalPage } from '../helpers/large-page-file.ts';
+
+const home = mkdtempSync(join(tmpdir(), 'gbrain-restore-statement-bound-'));
+const env = { GBRAIN_HOME: home, OPENAI_API_KEY: undefined, VOYAGE_API_KEY: undefined, ANTHROPIC_API_KEY: undefined };
+const logger = { info() {}, warn() {}, error() {} };
+afterAll(() => { rmSync(home, { recursive: true, force: true }); });
+
+/** The two probes the seal decision runs (page-body/ingest.ts occurrencesWithoutChunks, projection.ts STALE_CHUNKS_SQL). */
+const SEAL_PROBES = ['FROM unnest($2::bigint[]) AS o', 'WITH RECURSIVE occ(o)'];
+/** The statement a publication transaction opens with (protocol.ts declareDurablePersistence). */
+const DECLARE_PUBLICATION = "set_config('gbrain.persistence_protocol'";
+
+/**
+ * The engine whose publication transactions end the first seal probe they run as the server ends a statement at the
+ * publication's `statement_timeout`. `probes.inPublication` counts the probes publication transactions ran.
+ */
+function statementBound(engine: BrainEngine) {
+ const probes = { inPublication: 0, cancelled: 0 };
+ const inTx = (tx: BrainEngine): BrainEngine => {
+ let publication = false;
+ return new Proxy(tx, { get(target, prop) {
+ if (prop === 'executeRaw') return (sql: string, params?: unknown[], opts?: unknown) => {
+ if (sql.includes(DECLARE_PUBLICATION)) publication = true;
+ if (publication && SEAL_PROBES.some(probe => sql.includes(probe)) && probes.inPublication++ === 0) {
+ probes.cancelled++;
+ return Promise.reject(Object.assign(new Error('canceling statement due to statement timeout'), { code: '57014' }));
+ }
+ return target.executeRaw(sql, params, opts as never);
+ };
+ if (prop === 'transaction') return (fn: (inner: BrainEngine) => Promise<unknown>, opts?: unknown) => target.transaction(inner => fn(inTx(inner)), opts as never);
+ const value = Reflect.get(target, prop, target);
+ return typeof value === 'function' ? value.bind(target) : value;
+ } });
+ };
+ const view = new Proxy(engine, { get(target, prop) {
+ if (prop === 'transaction') return (fn: (tx: BrainEngine) => Promise<unknown>, opts?: unknown) => target.transaction(tx => fn(inTx(tx)), opts as never);
+ const value = Reflect.get(target, prop, target);
+ return typeof value === 'function' ? value.bind(target) : value;
+ } });
+ return { view, probes };
+}
+
+/** A write over the reply wait is repeated with its request_id until it settles. */
+async function settled<T>(call: () => Promise<T>): Promise<T> {
+ for (;;) {
+ try { return await call(); }
+ catch (error) { if ((error as { code?: string }).code !== 'write_pending') throw error; await Bun.sleep(500); }
+ }
+}
+
+for (const kind of testBackends()) describe(`restore_page under the publication statement bound (${kind})`, () => {
+ let engine: BrainEngine;
+ let close: () => Promise<void>;
+ beforeAll(async () => withEnv(env, async () => {
+ if (kind === 'postgres') ({ engine, close } = await isolatedPersistencePostgres(process.env.DATABASE_URL!));
+ else { const lite = new PGLiteEngine(); await lite.connect({}); await lite.initSchema(); engine = lite; close = () => lite.disconnect(); }
+ }), 120_000);
+ afterAll(async () => withEnv(env, async () => { await close?.(); }));
+
+ test('a page over the ceiling restores sealed, its file streamed once, with no seal probe in the transaction', async () => withEnv(env, async () => {
+ const { view, probes } = statementBound(engine);
+ await view.setConfig('pages.segmented_storage', 'on');
+ const root = join(home, `src-${randomUUID().slice(0, 8)}`);
+ mkdirSync(join(root, 'notes'), { recursive: true });
+ execFileSync('git', ['init', '-q'], { cwd: root });
+ const sourceId = `rb-${randomUUID().slice(0, 8)}`;
+ await view.executeRaw('UPDATE persistence_brain SET enabled=false WHERE singleton=1');
+ await view.executeRaw("INSERT INTO sources(id,name,local_path,config) VALUES($1,$1,$2,'{}')", [sourceId, root]);
+ await registerLocalWriter(view, 'cli');
+ await claimWorktree(view, sourceId, root);
+ await view.executeRaw('UPDATE persistence_brain SET enabled=true WHERE singleton=1');
+ const ctx = { engine: view, sourceId, remote: false, config: { engine: view.kind, embedding_disabled: true } as never, dryRun: false, logger } as OperationContext;
+ const slug = 'notes/bound';
+ const file = join(root, `${slug}.md`);
+ const submit = async (operation: 'delete_page' | 'restore_page') => {
+ const params = { slug, expected_revision: (await view.readPageSnapshot(slug, { sourceId, includeDeleted: true }))!.revision, request_id: randomUUID() };
+ return settled(() => submitPageMutation(ctx, { operation, params }) as Promise<Record<string, unknown>>);
+ };
+ const sealed = async () => (await engine.executeRaw<{ sealed: boolean; deleted: boolean }>(`SELECT
+ text_projection_revision IS NOT DISTINCT FROM knowledge_revision AS sealed, deleted_at IS NOT NULL AS deleted
+ FROM pages WHERE source_id=$1 AND slug=$2`, [sourceId, slug]))[0];
+ try {
+ const original = writeCanonicalPage(file, PAGE_MAX_BYTES + 1024 * 1024, 301);
+ expect(await settled(() => importManagedFile(view, file, `${slug}.md`, { sourceId, noEmbed: true }))).toMatchObject({ status: 'imported' });
+ for (let polls = 0; !(await sealed()).sealed && polls < 600; polls++) await Bun.sleep(100);
+ expect(await sealed()).toEqual({ sealed: true, deleted: false });
+ expect(await submit('delete_page')).toMatchObject({ status: 'soft_deleted', noop: false });
+ expect(existsSync(file)).toBe(false);
+
+ for (const key of Object.keys(probes) as Array<keyof typeof probes>) probes[key] = 0;
+ for (const key of Object.keys(largeFileWork) as Array<keyof typeof largeFileWork>) largeFileWork[key] = 0;
+ const restored = await submit('restore_page');
+ expect(restored).toMatchObject({ status: 'restored', noop: false });
+ expect(statSync(file).size).toBe(original.size);
+ expect(fileSha(file)).toBe(original.sha);
+ expect(readdirSync(join(root, 'notes')).filter(name => name.includes('.tmp.'))).toEqual([]);
+ expect(largeFileWork.bytesWritten).toBe(original.size);
+ expect(probes).toEqual({ inPublication: 0, cancelled: 0 });
+ expect(await sealed()).toEqual({ sealed: true, deleted: false });
+ } finally {
+ await disposePersistenceConsumer(view);
+ rmSync(root, { recursive: true, force: true });
+ }
+ }), 600_000);
+});
diff --git a/test/postgres-unit-arms.txt b/test/postgres-unit-arms.txt
index a60e9d4fd4..52207795fd 100644
--- a/test/postgres-unit-arms.txt
+++ b/test/postgres-unit-arms.txt
@@ -116,6 +116,7 @@ test/large-pages/remote-get-page-privacy.test.ts
test/large-pages/remote-versions-storage-change.test.ts
test/large-pages/remote-window-privacy.test.ts
test/large-pages/restore-deferred-retire.test.ts
+test/large-pages/restore-publication-statement-bound.test.ts
test/large-pages/restore-segmented-reprojects.test.ts
test/large-pages/sealed-segmented-search.test.ts
test/large-pages/second-write-deferred-retire.test.ts