Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(files): file_register never re-attributes a row to another page id

garrytan223d8a53bc17
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 660a7dc71e..07684f39cc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,7 +16,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`.
+- `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.
- `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 c495136093..b55cbbf8b0 100644
--- a/src/core/ops/files.ts
+++ b/src/core/ops/files.ts
@@ -287,7 +287,7 @@ const file_register: Operation = {
const existing = await ctx.engine.executeRaw<{ n: number }>(`SELECT count(*)::int AS n FROM files f WHERE f.storage_path = $1 AND NOT (${await fileReadableSql(ctx)})`, [storagePath]);
if (existing[0]!.n > 0) throw opError('page_not_found', `The file at ${storagePath} belongs to a page this caller cannot read.`, 'Nothing was recorded.');
const owned = await ctx.engine.executeRaw<{ n: number }>(`SELECT count(*)::int AS n FROM files WHERE storage_path = $1
- AND (page_slug IS DISTINCT FROM $2 OR filename <> $3 OR source_id IS DISTINCT FROM $4)`, [storagePath, pageSlug, filename, sourceId]);
+ AND (page_slug IS DISTINCT FROM $2 OR filename <> $3 OR source_id IS DISTINCT FROM $4 OR page_id IS DISTINCT FROM $5)`, [storagePath, pageSlug, filename, sourceId, pageId]);
if (owned[0]!.n > 0) throw opError('invalid_params', `${storagePath} is already registered to a different page, filename or source.`,
'A registration never moves an existing row. Register under a path no other row uses. Nothing was recorded.');
if (ctx.dryRun) return { dry_run: true, action: 'file_register', storage_path: storagePath };
@@ -308,7 +308,7 @@ const file_register: Operation = {
mime_type = EXCLUDED.mime_type,
${FILES_METADATA_MERGE_SQL}
WHERE files.page_slug IS NOT DISTINCT FROM EXCLUDED.page_slug AND files.filename = EXCLUDED.filename
- AND files.source_id IS NOT DISTINCT FROM EXCLUDED.source_id
+ AND files.source_id IS NOT DISTINCT FROM EXCLUDED.source_id AND files.page_id IS NOT DISTINCT FROM EXCLUDED.page_id
RETURNING 1`,
[pageSlug, filename, storagePath, (p.mime_type as string | undefined) ?? null, size, hash, sourceId, pageId],
[metadata]);
diff --git a/test/file-register.test.ts b/test/file-register.test.ts
index 4f9b962726..cb34baff77 100644
--- a/test/file-register.test.ts
+++ b/test/file-register.test.ts
@@ -95,6 +95,23 @@ describe('file_register', () => {
expect(await engine.executeRaw('SELECT page_slug, filename, content_hash FROM files')).toEqual([{ page_slug: null, filename: 'x.txt', content_hash: sha(old) }]);
});
+ test('refuses a registration that would re-attribute a row to another page that took the same slug, keeping the row as it was', async () => {
+ const engine = await embedded();
+ await addPage(engine, 'notes/plan');
+ const old = Buffer.from('first page bytes');
+ store('notes/plan/a.txt', old);
+ const params = { storage_path: 'notes/plan/a.txt', filename: 'a.txt', page_slug: 'notes/plan' };
+ await run('file_register', ctxFor(engine), { ...params, content_hash: sha(old), size_bytes: old.length });
+ const [first] = await engine.executeRaw<{ id: number }>("SELECT id FROM pages WHERE slug='notes/plan'");
+ await engine.updateSlug('notes/plan', 'notes/moved');
+ await importFromContent(engine, 'notes/plan', ['---', 'type: note', 'title: Replacement', '---', '# Replacement', '', 'A new page under the old slug.'].join('\n'));
+ const next = Buffer.from('second page bytes');
+ store('notes/plan/a.txt', next);
+ const err = await failure(() => run('file_register', ctxFor(engine), { ...params, content_hash: sha(next), size_bytes: next.length }));
+ expect(err?.code).toBe('invalid_params');
+ expect(await engine.executeRaw('SELECT page_id, content_hash FROM files')).toEqual([{ page_id: first!.id, content_hash: sha(old) }]);
+ });
+
test('refuses a page that does not exist and an object it cannot read back, recording nothing', async () => {
const engine = await embedded();
const bytes = Buffer.from('orphan');