Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(files): an attachment belongs to the page id it records; identical re-uploads restamp it, and purge's slug fallback covers only rows with no page id

garrytanf1393b512db3
diff --git a/src/core/facts/purge-verify.ts b/src/core/facts/purge-verify.ts
index 4ab422b6ba..0535fa55a9 100644
--- a/src/core/facts/purge-verify.ts
+++ b/src/core/facts/purge-verify.ts
@@ -594,7 +594,7 @@ export async function verifyPagePurge(engine: BrainEngine, input: { sourceId: st
await keyed('core_edit_notices', 'SELECT 1 FROM core_edit_notices WHERE source_id=$1 AND slug=$2', [src, slug]);
await keyed('page_aliases', 'SELECT 1 FROM page_aliases WHERE source_id=$1 AND slug=$2', [src, slug]);
await keyed('slug_aliases', 'SELECT 1 FROM slug_aliases WHERE source_id=$1 AND canonical_slug=$2', [src, slug]);
- await keyed('files', 'SELECT 1 FROM files WHERE page_slug=$2 AND (source_id=$1 OR source_id IS NULL)', [src, slug]);
+ await keyed('files', 'SELECT 1 FROM files WHERE page_id IS NULL AND page_slug=$2 AND (source_id=$1 OR source_id IS NULL)', [src, slug]);
await keyed('write_gate_holds', 'SELECT 1 FROM write_gate_holds WHERE source_id=$1 AND slug=$2', [src, slug]);
if (!await relationExists(engine, 'query_cache')) stores.push(missingCache('query_cache'));
else await keyed('query_cache', 'SELECT 1 FROM query_cache WHERE $1::text IS NOT NULL', [src]);
diff --git a/src/core/ops/files.ts b/src/core/ops/files.ts
index 7348128ae7..da78716a93 100644
--- a/src/core/ops/files.ts
+++ b/src/core/ops/files.ts
@@ -141,6 +141,7 @@ 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]);
return { status: 'already_exists', storage_path: storagePath };
}
}
diff --git a/src/core/persistence/page-purge.ts b/src/core/persistence/page-purge.ts
index 52ea131e5d..3f79ce2f10 100644
--- a/src/core/persistence/page-purge.ts
+++ b/src/core/persistence/page-purge.ts
@@ -330,7 +330,7 @@ export async function purgePageInTransaction(tx: BrainEngine, row: WriteRequest,
removed.persistence_requests = await lookup('redaction_read', () => redactRequestIntents(tx, requests, row.id, { skipPending: true, needles }));
const journalSweep = { scanned: prepared.sweep.scanned, ceiling: prepared.sweep.ceiling, watermark: prepared.sweep.watermark ?? null, batch: prepared.sweep.batch ?? 1,
tail: recheck.tail, pending_rechecked: recheck.pendingRechecked, complete: prepared.sweep.complete && recheck.complete };
- const files = await tx.executeRaw<{ storage_path: string }>(`DELETE FROM files WHERE (page_id=$1 OR (page_slug=$2 AND (source_id=$3 OR source_id IS NULL)))
+ const files = await tx.executeRaw<{ storage_path: string }>(`DELETE FROM files WHERE (page_id=$1 OR (page_id IS NULL AND page_slug=$2 AND (source_id=$3 OR source_id IS NULL)))
RETURNING storage_path`, [page.id, slug, sourceId]);
removed.files = files.length;
Object.assign(removed, await (await import('../generations/ledger-scrub.ts')).scrubGenerationLedger(tx, sourceId, { slug }));
diff --git a/test/page-purge.test.ts b/test/page-purge.test.ts
index c7246e4703..1d724e1cc5 100644
--- a/test/page-purge.test.ts
+++ b/test/page-purge.test.ts
@@ -125,6 +125,37 @@ describe('page purge', () => {
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' });
+ const upload = join(storageDir, '..', 'attachment.txt');
+ writeFileSync(upload, 'same bytes');
+ await call('file_upload', { path: upload, page_slug: 'notes/old' });
+ await engine.executeRaw("UPDATE pages SET slug='notes/moved' WHERE slug='notes/old' AND source_id='default'");
+ await importFromContent(engine, 'notes/old', page, { noEmbed: true, sourceId: 'default' });
+ const [replacement] = await engine.executeRaw<{ id: number }>("SELECT id FROM pages WHERE slug='notes/old' AND source_id='default'");
+ expect((await call('file_upload', { path: upload, page_slug: 'notes/old' })).status).toBe('already_exists');
+ expect(await engine.executeRaw('SELECT page_id FROM files')).toEqual([{ page_id: replacement!.id }]);
+ expect((await purgePage('notes/moved')).status).toBe('purged');
+ expect(await engine.executeRaw('SELECT page_id FROM files')).toEqual([{ page_id: replacement!.id }]);
+ expect(existsSync(join(storageDir, 'notes/old/attachment.txt'))).toBe(true);
+ });
+
+ test('purging a page that took a renamed page\'s slug keeps the attachment the renamed page still owns', async () => {
+ const page = '---\ntitle: Old\ntype: note\n---\n# Old\n\nBody.\n';
+ await importFromContent(engine, 'notes/old', page, { noEmbed: true, sourceId: 'default' });
+ const [owner] = await engine.executeRaw<{ id: number }>("SELECT id FROM pages WHERE slug='notes/old' AND source_id='default'");
+ const upload = join(storageDir, '..', 'attachment.txt');
+ writeFileSync(upload, 'owned bytes');
+ await call('file_upload', { path: upload, page_slug: 'notes/old' });
+ await engine.executeRaw("UPDATE pages SET slug='notes/moved' WHERE id=$1", [owner!.id]);
+ await importFromContent(engine, 'notes/old', page, { noEmbed: true, sourceId: 'default' });
+ const result = await purgePage('notes/old');
+ expect([result.status, result.purge.removed.files]).toEqual(['purged', 0]);
+ expect(await engine.executeRaw('SELECT page_id FROM files')).toEqual([{ page_id: owner!.id }]);
+ expect(existsSync(join(storageDir, 'notes/old/attachment.txt'))).toBe(true);
+ });
+
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');