Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(files): file_upload records the source and page id of its row (#8513)

garrytan8722cd7766b3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 07684f39cc..b05fbf5d85 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@ identifiers and attribution are available in the pre-removal Git revision
### Itemized changes
- `src/core/ops/files.ts`: `file_register` accepts only the upload layout path (`<page_slug>/<filename>`, or `unsorted/<first 8 hash chars>-<filename>`), refuses `page_not_found` for a page that does not exist or that the caller's read policy hides (member access and privacy, the private-pages hatch included), and records nothing unless the stored object matches. Rows record their store's backend and prefix, and the page and source they belong to, so purging that page finds them. Registering the same path again updates its row, and a registration that would move a row to another page, filename or source is refused `invalid_params`. The page is compared by id as well as slug, so a page that takes a renamed page's slug cannot take over the renamed page's files.
+- `file_upload` records the caller's source and the page's id in that source on each files row it writes, on every engine, so `delete_page --purge` of a page in a source other than `default` removes its attachments and their stored objects (#8513). Rows written before this release keep `source_id = 'default'` and no page id; nothing rewrites them.
- `src/core/storage/s3.ts`: `file_url` on an s3 store with an endpoint builds the URL from the endpoint the SDK resolves, instead of throwing.
- A store that does not answer is the new `store_unavailable` code, never a file reported missing and never a row recorded.
- On an engine opened from settings, `delete_page` purge deletes a page's files from `settings.storage` too. A file it can't find there, or a store that doesn't answer, is reported in `blobs_remaining`, never as deleted; files an embedded engine's `file_upload` wrote before this release live in the process store, so delete those remaining paths by hand. Rows an embedded engine's `file_upload` wrote before this release point at the process store, so `file_url` and `file_verify` look for them in `settings.storage`.
diff --git a/src/core/ops/files.ts b/src/core/ops/files.ts
index b55cbbf8b0..4223cd87f0 100644
--- a/src/core/ops/files.ts
+++ b/src/core/ops/files.ts
@@ -146,20 +146,23 @@ 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]) : [];
// #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 —
// never a JSON string into ::jsonb (#2339).
await executeRawJsonb(
ctx.engine,
- `INSERT INTO files (page_slug, filename, storage_path, mime_type, size_bytes, content_hash, metadata)
- VALUES ($1, $2, $3, $4, $5, $6, $7::jsonb)
+ `INSERT INTO files (page_slug, filename, storage_path, mime_type, size_bytes, content_hash, source_id, page_id, metadata)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9::jsonb)
ON CONFLICT (storage_path) DO UPDATE SET
content_hash = EXCLUDED.content_hash,
size_bytes = EXCLUDED.size_bytes,
mime_type = EXCLUDED.mime_type,
${FILES_METADATA_MERGE_SQL}`,
- [pageSlug, filename, storagePath, mimeType, stat.size, hash],
+ [pageSlug, filename, storagePath, mimeType, stat.size, hash, sourceId, page?.id ?? null],
[embedded?.metadata ?? { storage: (ctx.config.storage as { backend: string }).backend }],
);
} catch (dbErr) {
diff --git a/test/page-purge.test.ts b/test/page-purge.test.ts
index a92d3dc01d..a1e65bc0b2 100644
--- a/test/page-purge.test.ts
+++ b/test/page-purge.test.ts
@@ -89,6 +89,23 @@ describe('page purge', () => {
expect(await engine.executeRaw(`SELECT 1 FROM fact_purges WHERE reason='page purge'`)).toHaveLength(1);
}, 60_000);
+ test('a file_upload to a page in a non-default source records that page, so purging it removes the row and the stored object (#8513)', async () => {
+ await engine.executeRaw("INSERT INTO sources (id, name) VALUES ('source-a', 'source-a') ON CONFLICT DO NOTHING");
+ await importFromContent(engine, 'notes/plan', '---\ntitle: Plan\ntype: note\n---\n# Plan\n\nBody.\n', { noEmbed: true, sourceId: 'source-a' });
+ const [page] = 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, 'attachment 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: page!.id }]);
+ expect(existsSync(join(storageDir, 'notes/plan/attachment.txt'))).toBe(true);
+ 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, result.purge.blobs_remaining]).toEqual(['purged', 1, []]);
+ expect(await engine.executeRaw('SELECT 1 FROM files')).toEqual([]);
+ expect(existsSync(join(storageDir, 'notes/plan/attachment.txt'))).toBe(false);
+ });
+
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] });