Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(files): file_upload refuses an unregistered source before writing, and a conflicting row takes the uploader's source and page id
garrytan412f77891823
diff --git a/src/core/ops/files.ts b/src/core/ops/files.tsindex 91baca0eb5..7348128ae7 100644--- a/src/core/ops/files.ts+++ b/src/core/ops/files.ts@@ -123,6 +123,13 @@ const file_upload: Operation = {const { createStorage } = await import('../storage.ts');const storage = embedded?.storage ?? await createStorage(ctx.config.storage as any);+ // #8513: the row names the caller's source and the page's id there, so purging that page finds it in any source.+ // The source is checked before any bytes are written, so a refused upload leaves the object at storagePath as it was.+ const sourceId = ctx.sourceId ?? 'default';+ if (!(await ctx.engine.executeRaw('SELECT 1 FROM sources WHERE id = $1', [sourceId])).length) throw opError('unknown_source',+ `No source "${sourceId}", so nothing was uploaded.`, 'Upload under a registered source (gbrain sources list).');+ const [page] = pageSlug ? await ctx.engine.executeRaw<{ id: number }>('SELECT id FROM pages WHERE slug = $1 AND source_id = $2', [pageSlug, sourceId]) : [];+const { sqlQueryForEngine, executeRawJsonb, FILES_METADATA_MERGE_SQL } = await import('../sql-query.ts');const sql = sqlQueryForEngine(ctx.engine);const existing = await sql`SELECT id FROM files WHERE content_hash = ${hash} AND storage_path = ${storagePath}`;@@ -146,9 +153,7 @@ const file_upload: Operation = {}try {- // #8513: the row names the caller's source and the page's id there, so purging that page finds it in any source.- const sourceId = ctx.sourceId ?? 'default';- const [page] = pageSlug ? await ctx.engine.executeRaw<{ id: number }>('SELECT id FROM pages WHERE slug = $1 AND source_id = $2', [pageSlug, sourceId]) : [];+ // The bytes at storagePath are now the caller's, so a conflicting row takes the caller's source and page id.// #4910: stamp the storage lane (doctor image_assets / files verify// classify on it) and merge metadata on conflict so legacy `{}` rows// heal on their next content change. Real object via executeRawJsonb —@@ -161,6 +166,8 @@ const file_upload: Operation = {content_hash = EXCLUDED.content_hash,size_bytes = EXCLUDED.size_bytes,mime_type = EXCLUDED.mime_type,+ source_id = EXCLUDED.source_id,+ page_id = EXCLUDED.page_id,${FILES_METADATA_MERGE_SQL}`,[pageSlug, filename, storagePath, mimeType, stat.size, hash, sourceId, page?.id ?? null],[embedded?.metadata ?? { storage: (ctx.config.storage as { backend: string }).backend }],@@ -272,7 +279,7 @@ const file_register: Operation = {mutating: true,scope: 'admin',localOnly: true,- cliOnly: { argv: ['gbrain', 'files', 'upload', '<file>', '--page', '<page_slug>'] },+ cliOnly: { argv: ['gbrain', 'files', 'verify'] },handler: async (ctx, p) => {const { storage, metadata } = await embeddedFileStore(ctx, 'file_register');const filename = p.filename as string, hash = p.content_hash as string, size = p.size_bytes as number;diff --git a/test/page-purge.test.ts b/test/page-purge.test.tsindex a1e65bc0b2..c7246e4703 100644--- a/test/page-purge.test.ts+++ b/test/page-purge.test.ts@@ -7,7 +7,7 @@*/import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test';import { randomUUID } from 'node:crypto';-import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';import { PGLiteEngine } from '../src/core/pglite-engine.ts';@@ -106,6 +106,38 @@ describe('page purge', () => {expect(existsSync(join(storageDir, 'notes/plan/attachment.txt'))).toBe(false);});+ test('a file_upload over the same path in another source records the uploader, so purging its page removes the row and its bytes', async () => {+ await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-a', 'source-a') ON CONFLICT DO NOTHING");+ const page = '---\ntitle: Plan\ntype: note\n---\n# Plan\n\nBody.\n';+ await importFromContent(engine, 'notes/plan', page, { noEmbed: true, sourceId: 'default' });+ await importFromContent(engine, 'notes/plan', page, { noEmbed: true, sourceId: 'source-a' });+ const [pageA] = await engine.executeRaw<{ id: number }>("SELECT id FROM pages WHERE slug='notes/plan' AND source_id='source-a'");+ const upload = join(storageDir, '..', 'attachment.txt');+ writeFileSync(upload, 'default bytes');+ await call('file_upload', { path: upload, page_slug: 'notes/plan' });+ writeFileSync(upload, 'source-a bytes');+ const ctx = { ...context(), sourceId: 'source-a' };+ await withEnv({ GBRAIN_HOME: home }, () => op('file_upload').handler(ctx, { path: upload, page_slug: 'notes/plan' }));+ expect(await engine.executeRaw('SELECT source_id, page_id FROM files')).toEqual([{ source_id: 'source-a', page_id: pageA!.id }]);+ const snapshot = await engine.readPageSnapshot('notes/plan', { sourceId: 'source-a', includeDeleted: true });+ const result = await withEnv({ GBRAIN_HOME: home }, () => op('delete_page').handler(ctx, { slug: 'notes/plan', purge: true, request_id: randomUUID(), expected_revision: snapshot!.revision })) as Record<string, any>;+ expect([result.status, result.purge.removed.files]).toEqual(['purged', 1]);+ expect(existsSync(join(storageDir, 'notes/plan/attachment.txt'))).toBe(false);+ });++ test('a file_upload under a source that is not registered refuses before writing, leaving the stored object and its row as they were', async () => {+ await importFromContent(engine, 'notes/plan', '---\ntitle: Plan\ntype: note\n---\n# Plan\n\nBody.\n', { noEmbed: true, sourceId: 'default' });+ const upload = join(storageDir, '..', 'attachment.txt');+ writeFileSync(upload, 'default bytes');+ await call('file_upload', { path: upload, page_slug: 'notes/plan' });+ const before = await engine.executeRaw('SELECT source_id, page_id, content_hash FROM files');+ writeFileSync(upload, 'other bytes');+ const ctx = { ...context(), sourceId: 'source-missing' };+ expect(await errorText(withEnv({ GBRAIN_HOME: home }, () => op('file_upload').handler(ctx, { path: upload, page_slug: 'notes/plan' })))).toStartWith('unknown_source: ');+ expect(readFileSync(join(storageDir, 'notes/plan/attachment.txt'), 'utf8')).toBe('default bytes');+ expect(await engine.executeRaw('SELECT source_id, page_id, content_hash FROM files')).toEqual(before);+ });+test('the tombstone refuses the same content under any slug until unpurge; edited content imports', async () => {await importFromContent(engine, SLUG, CONTENT, { noEmbed: true, sourceId: 'default' });await runExtractFacts(engine, { slugs: [SLUG] });