Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(files): an upload never takes another source's live attachment, and restamps a row only for a page in the caller's source
garrytanf39d2e53157a
diff --git a/src/core/ops/files.ts b/src/core/ops/files.tsindex da78716a93..033d7ef1ee 100644--- a/src/core/ops/files.ts+++ b/src/core/ops/files.ts@@ -130,10 +130,12 @@ const file_upload: Operation = {`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}`;- if (existing.length > 0) {+ const { executeRawJsonb, FILES_METADATA_MERGE_SQL } = await import('../sql-query.ts');+ const [owner] = await ctx.engine.executeRaw<{ source_id: string; page_id: number | null; content_hash: string }>(+ 'SELECT source_id, page_id, content_hash FROM files WHERE storage_path = $1', [storagePath]);+ // A row with a live page in another source is that source's attachment: an upload never moves it or replaces its bytes.+ const foreign = owner !== undefined && owner.page_id !== null && owner.source_id !== sourceId;+ if (owner?.content_hash === hash) {// #4302: only claim already_exists when the BACKEND really holds the// object — a DB row whose bytes vanished must re-upload, not lie.let inBackend = false;@@ -141,10 +143,12 @@ const file_upload: Operation = {inBackend = await storage.exists(storagePath);} catch { /* probe failure → treat as absent, re-upload below */ }if (inBackend) {- await ctx.engine.executeRaw('UPDATE files SET source_id = $2, page_id = $3 WHERE storage_path = $1', [storagePath, sourceId, page?.id ?? null]);+ if (page && !foreign) await ctx.engine.executeRaw('UPDATE files SET source_id = $2, page_id = $3 WHERE storage_path = $1', [storagePath, sourceId, page.id]);return { status: 'already_exists', storage_path: storagePath };}}+ if (foreign) throw opError('invalid_params', `${storagePath} holds the attachment of a page in source '${owner!.source_id}', so nothing was uploaded.`,+ 'Upload it under another filename; an upload never replaces another source\'s attachment.');try {await uploadUnchangedFile(storage, storagePath, filePath, hash, mimeType || undefined);@@ -154,7 +158,7 @@ const file_upload: Operation = {}try {- // The bytes at storagePath are now the caller's, so a conflicting row takes the caller's source and page id.+ // The bytes at storagePath are now the caller's, so a conflicting row takes the caller's source and page id when that source has the page.// #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 —@@ -167,8 +171,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,+ source_id = CASE WHEN EXCLUDED.page_id IS NULL THEN files.source_id ELSE EXCLUDED.source_id END,+ page_id = COALESCE(EXCLUDED.page_id, files.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 }],diff --git a/test/page-purge.test.ts b/test/page-purge.test.tsindex 1d724e1cc5..58d971ca1e 100644--- a/test/page-purge.test.ts+++ b/test/page-purge.test.ts@@ -106,15 +106,35 @@ 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 () => {+ test('a file_upload never takes another source\'s live attachment: different bytes are refused, identical bytes leave the row with its owner', 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 [owner] = await engine.executeRaw<{ id: number }>("SELECT id FROM pages WHERE slug='notes/plan' AND source_id='default'");const upload = join(storageDir, '..', 'attachment.txt');writeFileSync(upload, 'default bytes');await call('file_upload', { path: upload, page_slug: 'notes/plan' });+ const ctx = { ...context(), sourceId: 'source-a' };+ const uploadAsA = () => withEnv({ GBRAIN_HOME: home }, () => op('file_upload').handler(ctx, { path: upload, page_slug: 'notes/plan' })) as Promise<Record<string, any>>;+ expect((await uploadAsA()).status).toBe('already_exists');+ writeFileSync(upload, 'source-a bytes');+ expect(await errorText(uploadAsA())).toStartWith('invalid_params: ');+ expect(readFileSync(join(storageDir, 'notes/plan/attachment.txt'), 'utf8')).toBe('default bytes');+ expect(await engine.executeRaw('SELECT source_id, page_id FROM files')).toEqual([{ source_id: 'default', page_id: owner!.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', 0]);+ expect(existsSync(join(storageDir, 'notes/plan/attachment.txt'))).toBe(true);+ });++ test('an upload over a row with no page id records the uploader\'s page, so purging that page in a non-default source 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 upload = join(storageDir, '..', 'attachment.txt');+ writeFileSync(upload, 'early bytes');+ await call('file_upload', { path: upload, page_slug: 'notes/plan' });+ await importFromContent(engine, 'notes/plan', '---\ntitle: Plan\ntype: note\n---\n# Plan\n\nBody.\n', { 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'");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' }));@@ -125,6 +145,21 @@ describe('page purge', () => {expect(existsSync(join(storageDir, 'notes/plan/attachment.txt'))).toBe(false);});+ test('an identical re-upload from a source with no such page leaves the row with its page, so purging that page still removes it', async () => {+ await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-b', 'source-b') ON CONFLICT DO NOTHING");+ await importFromContent(engine, 'notes/plan', '---\ntitle: Plan\ntype: note\n---\n# Plan\n\nBody.\n', { noEmbed: true, sourceId: 'default' });+ const [owner] = await engine.executeRaw<{ id: number }>("SELECT id FROM pages WHERE slug='notes/plan' AND source_id='default'");+ const upload = join(storageDir, '..', 'attachment.txt');+ writeFileSync(upload, 'plan bytes');+ await call('file_upload', { path: upload, page_slug: 'notes/plan' });+ const ctx = { ...context(), sourceId: 'source-b' };+ expect((await withEnv({ GBRAIN_HOME: home }, () => op('file_upload').handler(ctx, { path: upload, page_slug: 'notes/plan' })) as Record<string, any>).status).toBe('already_exists');+ expect(await engine.executeRaw('SELECT source_id, page_id FROM files')).toEqual([{ source_id: 'default', page_id: owner!.id }]);+ const result = await purgePage('notes/plan');+ expect([result.status, result.purge.removed.files]).toEqual(['purged', 1]);+ expect(existsSync(join(storageDir, 'notes/plan/attachment.txt'))).toBe(false);+ });+test('re-uploading identical bytes to a page that took a renamed page\'s slug records the new page, so purging the renamed page keeps the attachment', async () => {const page = '---\ntitle: Old\ntype: note\n---\n# Old\n\nBody.\n';await importFromContent(engine, 'notes/old', page, { noEmbed: true, sourceId: 'default' });