Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(ingest): read the bound snapshot before authorizing; a page created mid-write is a retryable conflict (GST-2112 outside voice)
garrytan94f6deb9146b
diff --git a/src/core/import-file.ts b/src/core/import-file.tsindex ea09d43cf7..6f714ced7a 100644--- a/src/core/import-file.ts+++ b/src/core/import-file.ts@@ -443,7 +443,7 @@ export async function importFromContent(// unscoped-check/scoped-write bug class).const existingSnapshot = !opts.prepare ? null : await materializedSnapshot(engine, opts.coordinated && opts.existingSnapshot !== undefined ? opts.existingSnapshot : await engine.readPageSnapshot(slug, { sourceId: sourceId ?? 'default', includeDeleted: true }));const existing = opts.prepare ? existingSnapshot?.page ?? null : await materializedPage(engine, await engine.getPage(slug, { sourceId: sourceId ?? 'default', includeDeleted: true }));- if (opts.expectedBaseRevision !== undefined) assertPageRevision(existing ? { revision: existing.knowledge_revision! } : null, { expectedRevision: opts.expectedBaseRevision ?? undefined });+ if (opts.expectedBaseRevision !== undefined) assertPageRevision(existing ? { revision: existing.knowledge_revision! } : null, { expectedRevision: opts.expectedBaseRevision });if (existing) stabilizeSafetyAssessments(parsed.frontmatter, existing.frontmatter);const collisionOwner = collidingSlugOwner(existing, slug, opts.sourceRoot, opts.sourcePath);diff --git a/src/core/minions/handlers/ingest-capture.ts b/src/core/minions/handlers/ingest-capture.tsindex 9a6d22c72f..4c58cbdc13 100644--- a/src/core/minions/handlers/ingest-capture.ts+++ b/src/core/minions/handlers/ingest-capture.ts@@ -117,12 +117,13 @@ export async function assertWebhookWriterMayReplace(engine: BrainEngine, writer:assertScopePageWriter({ remote: true }, target, 'ingest_capture');const authority = { version: 1, principal: webhookPrincipal(writer), remote: true, sourceId, sourceIncarnation: '',scopes: [], operations: null, slugPrefixes: null, excludePrivate: await excludesPrivateWrites(engine, true) } as WriteAuthority;+ // GST-2112: the snapshot is read before the visibility check, so the revision returned (which binds the write) is+ // never older than the state the writer was authorized against.+ const stored = content === undefined ? undefined : await engine.readPageSnapshot(target, { sourceId, includeDeleted: true });await authorizePageVisibility(engine, authority, target);if (content === undefined) return;- // GST-2112: 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.- // The returned revision binds the write to the frontmatter checked here.- const stored = await engine.readPageSnapshot(target, { sourceId, includeDeleted: true });+ // 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 } });return stored?.revision ?? null;}diff --git a/test/ingest-capture-keeps-readers.test.ts b/test/ingest-capture-keeps-readers.test.tsindex 91bcf6ca93..ced4705396 100644--- a/test/ingest-capture-keeps-readers.test.ts+++ b/test/ingest-capture-keeps-readers.test.ts@@ -35,6 +35,8 @@ const SHARED = 'notes/shared-plan';const LATER = 'notes/restricted-later';const OPEN = 'notes/team-open';const RACED = 'notes/restricted-mid-write';+const NARROWED = 'notes/narrowed-before-snapshot';+const CREATED = 'notes/created-mid-write';let engine: PGLiteEngine;let server: Server;@@ -55,6 +57,7 @@ beforeAll(async () => {await importFromContent(engine, LATER, page('Restricted later'), { sourceId: SRC, noEmbed: true } as never);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);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']);@@ -149,6 +152,47 @@ describe('a webhook capture keeps a page\'s stored readers (GST-2112)', () => {expect((await stored(RACED))?.compiled_truth).toContain('Original body.');});+ test('readers narrowed away from the writer while the check reads the page: the write refuses, the page keeps its text', async () => {+ let narrowed = false;+ const racing = new Proxy(engine, { get(target, key) {+ if (key === 'readPageSnapshot') return async (...args: Parameters<PGLiteEngine['readPageSnapshot']>) => {+ if (!narrowed && args[0] === NARROWED) {+ narrowed = true;+ await importFromContent(target, NARROWED, page('Narrowed', 'readable_by: [alice-example]\n'), { sourceId: SRC, noEmbed: true } as never);+ }+ return target.readPageSnapshot(...args);+ };+ const value = Reflect.get(target, key) as unknown;+ return typeof value === 'function' ? value.bind(target) : value;+ } });+ await expect(makeIngestCaptureHandler(racing)(bobJob(NARROWED, page('Narrowed', 'readable_by: [bob-example]\n', 'Bob after losing access.'))))+ .rejects.toMatchObject({ code: 'page_not_found' });+ expect(narrowed).toBe(true);+ expect(await readers(NARROWED)).toEqual(['alice-example']);+ expect((await stored(NARROWED))?.compiled_truth).toContain('Original body.');+ });++ test('a page created between the check and the write: the write refuses revision_conflict, a retryable race', async () => {+ let checked = false;+ let raced = false;+ const racing = new Proxy(engine, { get(target, key) {+ if (key === 'readPageSnapshot') return async (...args: Parameters<PGLiteEngine['readPageSnapshot']>) => { checked = true; return target.readPageSnapshot(...args); };+ if (key === 'getPage') return async (...args: Parameters<PGLiteEngine['getPage']>) => {+ if (checked && !raced && args[0] === CREATED) {+ raced = true;+ await importFromContent(target, CREATED, page('Created mid write', READERS), { sourceId: SRC, noEmbed: true } as never);+ }+ return target.getPage(...args);+ };+ const value = Reflect.get(target, key) as unknown;+ return typeof value === 'function' ? value.bind(target) : value;+ } });+ await expect(makeIngestCaptureHandler(racing)(bobJob(CREATED, page('Created mid write', '', 'Checked while absent.')))).rejects.toMatchObject({ code: 'revision_conflict' });+ expect(raced).toBe(true);+ expect(await readers(CREATED)).toEqual(['alice-example', 'bob-example']);+ expect((await stored(CREATED))?.compiled_truth).toContain('Original body.');+ });+test('a body whose frontmatter names another language is held by the importer, not answered 500', async () => {const res = await ingest(OPEN, '---js\n{ title: "Team open" }\n---\n# Team open\n');expect(res.status).toBe(202);