Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Webhook capture refuses a page the revision backfill has not reached

garrytan8367f19c3b56
diff --git a/src/commands/serve-http-webhooks.ts b/src/commands/serve-http-webhooks.ts
index 1c98ebe0d4..cd63ca9f89 100644
--- a/src/commands/serve-http-webhooks.ts
+++ b/src/commands/serve-http-webhooks.ts
@@ -34,6 +34,7 @@ import { assertWebhookWriterMayReplace } from '../core/minions/handlers/ingest-c
import { defaultSourceMemberWriter } from '../core/persistence/page-visibility.ts';
import type { WriteAuthority } from '../core/persistence/model.ts';
import { validateSlug } from '../core/utils.ts';
+import { RevisionBackfillPendingError } from '../core/page-state/types.ts';
/**
* v0.46: normalize the per-event GitHub webhook payload shape into
@@ -563,8 +564,8 @@ async function handleIngest(
try {
await assertWebhookWriterMayReplace(engine, writer, writeSourceId, callerSlug, content);
} catch (err) {
- if (!(err instanceof OperationError)) throw err;
- res.status(err.code === 'page_not_found' ? 404 : 403).json({ error: err.code, message: err.message });
+ if (!(err instanceof OperationError) && !(err instanceof RevisionBackfillPendingError)) throw err;
+ res.status(err.code === 'page_not_found' ? 404 : err.code === 'revision_backfill_pending' ? 409 : 403).json({ error: err.code, message: err.message });
return;
}
}
diff --git a/src/core/minions/handlers/ingest-capture.ts b/src/core/minions/handlers/ingest-capture.ts
index 44a530decd..c6610f6c03 100644
--- a/src/core/minions/handlers/ingest-capture.ts
+++ b/src/core/minions/handlers/ingest-capture.ts
@@ -48,6 +48,7 @@ import { authorizePageVisibility, defaultSourceMemberWriter, excludesPrivateWrit
import type { Principal, WriteAuthority } from '../../persistence/model.ts';
import { assertMemberPageWriter, assertScopePageWriter } from '../../access/member-facts.ts';
import { validateSlug } from '../../utils.ts';
+import { REVISION_BACKFILL_PENDING, RevisionBackfillPendingError } from '../../page-state/types.ts';
import { appliedCaptureRequestPrefix, readAppliedCapture, recordAppliedCapture, saveAppliedCapture } from '../../ingestion/applied-captures.ts';
export interface IngestCaptureResult {
@@ -131,6 +132,8 @@ export async function assertWebhookWriterMayReplace(engine: BrainEngine, writer:
// The capture writes through a trusted local path, so apply the remote put_page readers rule here: a body never
// drops readable_by from a page that names readers, and the readers it names keep the writer.
await remoteReadersChecked(engine, authority, stored?.page.frontmatter, { parsedPage: { frontmatter: parseMarkdown(content, target + '.md', { validate: true }).frontmatter } });
+ // A page the revision backfill has not reached has no revision to bind the write to: refused, kept in the job row for `gbrain jobs retry`.
+ if (stored?.revision === REVISION_BACKFILL_PENDING) throw new RevisionBackfillPendingError(null);
return stored?.revision ?? null;
}
diff --git a/test/ingest-capture-keeps-readers.test.ts b/test/ingest-capture-keeps-readers.test.ts
index 16f694416a..71f9ce78a0 100644
--- a/test/ingest-capture-keeps-readers.test.ts
+++ b/test/ingest-capture-keeps-readers.test.ts
@@ -42,6 +42,7 @@ const OPEN = 'notes/team-open';
const RACED = 'notes/restricted-mid-write';
const NARROWED = 'notes/narrowed-before-snapshot';
const CREATED = 'notes/created-mid-write';
+const BACKFILL = 'notes/revision-backfill-pending';
let engine: PGLiteEngine;
let server: Server;
@@ -63,6 +64,7 @@ beforeAll(async () => {
await importFromContent(engine, OPEN, page('Team open'), { sourceId: SRC, noEmbed: true } as never);
await importFromContent(engine, RACED, page('Restricted mid write'), { sourceId: SRC, noEmbed: true } as never);
await importFromContent(engine, NARROWED, page('Narrowed', READERS), { sourceId: SRC, noEmbed: true } as never);
+ await importFromContent(engine, BACKFILL, page('Backfill pending', READERS), { sourceId: SRC, noEmbed: true } as never);
for (const id of ['alice-example', 'bob-example']) await engine.executeRaw('INSERT INTO access_members (id) VALUES ($1)', [id]);
const bob = await mintLegacyToken(engine, { name: 'bob', scopes: ['read', 'write'], takesHolders: ['world'], sourceGrant: [SRC] });
await engine.executeRaw('UPDATE access_tokens SET member_id = $2 WHERE id = $1::uuid', [bob.id, 'bob-example']);
@@ -198,6 +200,29 @@ describe('a webhook capture keeps a page\'s stored readers (GST-2112)', () => {
expect((await stored(CREATED))?.compiled_truth).toContain('Original body.');
});
+ test('a page the revision backfill has not reached: the route answers 409 and queues nothing, the job refuses revision_backfill_pending (not a bare TypeError); the page keeps its text', async () => {
+ const revisions = async (sql: string) => {
+ await engine.executeRaw('ALTER TABLE pages DISABLE TRIGGER USER');
+ try { await engine.executeRaw(sql, [BACKFILL, SRC]); } finally { await engine.executeRaw('ALTER TABLE pages ENABLE TRIGGER USER'); }
+ };
+ await engine.executeRaw('ALTER TABLE pages ALTER COLUMN knowledge_revision DROP NOT NULL');
+ await revisions('UPDATE pages SET knowledge_revision = NULL WHERE slug = $1 AND source_id = $2');
+ try {
+ expect((await engine.readPageSnapshot(BACKFILL, { sourceId: SRC }))?.revision).toBe('backfill_pending');
+ const before = await queuedJobs();
+ const res = await ingest(BACKFILL, page('Backfill pending', READERS, 'Posted before the backfill.'));
+ expect(res.status).toBe(409);
+ expect((await res.json() as { error: string }).error).toBe('revision_backfill_pending');
+ expect(await queuedJobs()).toBe(before);
+ await expect(makeIngestCaptureHandler(engine)(bobJob(BACKFILL, page('Backfill pending', READERS, 'Captured before the backfill.')))).rejects.toMatchObject({ code: 'revision_backfill_pending' });
+ expect(await readers(BACKFILL)).toEqual(['alice-example', 'bob-example']);
+ expect((await stored(BACKFILL))?.compiled_truth).toContain('Original body.');
+ } finally {
+ await revisions('UPDATE pages SET knowledge_revision = gen_random_uuid() WHERE slug = $1 AND source_id = $2 AND knowledge_revision IS NULL');
+ await engine.executeRaw('ALTER TABLE pages ALTER COLUMN knowledge_revision SET NOT NULL');
+ }
+ });
+
test('a capture queued for a member-bound credential writing to default is refused at write time (GST-2051): nothing is created', async () => {
const carol = await mintLegacyToken(engine, { name: 'carol-default', scopes: ['read', 'write'], takesHolders: ['world'], sourceGrant: ['default'] });
await engine.executeRaw('UPDATE access_tokens SET member_id = $2 WHERE id = $1::uuid', [carol.id, 'alice-example']);