Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): skip catalog attach when the target ingredient was deleted mid-render

[._.]/ Adam Eivyd672dddf435d
diff --git a/server/services/catalogImageAttachHook.js b/server/services/catalogImageAttachHook.js
index d4a0bf6622..98f94eeaf1 100644
--- a/server/services/catalogImageAttachHook.js
+++ b/server/services/catalogImageAttachHook.js
@@ -22,12 +22,19 @@
*/
import { mediaJobEvents } from './mediaJobQueue/index.js';
-import { attachMedia, setPortraitMedia, listMediaForIngredient } from './catalogDB.js';
+import { attachMedia, setPortraitMedia, listMediaForIngredient, getIngredient } from './catalogDB.js';
// Resolve the render onto the ingredient. Returns the kind attached
-// ('portrait' | 'reference'), 'duplicate' when the client already filed it, or
-// throws on a real DB error (caught by the caller).
+// ('portrait' | 'reference'), 'duplicate' when the client already filed it,
+// 'gone' when the target ingredient no longer exists, or throws on a real DB
+// error (caught by the caller).
async function attachGeneratedImage({ ingredientId, kind, filename }) {
+ // The ingredient can be deleted between enqueue and completion (a render
+ // outlives its editor by minutes). `getIngredient` filters `deleted = false`,
+ // so this skips a hard-deleted ingredient (FK would throw) AND a soft-deleted
+ // one (FK is satisfied — attaching would silently file media onto a tombstone
+ // and fan those rows to peers via sync_sequence).
+ if (!(await getIngredient(ingredientId))) return 'gone';
const existing = await listMediaForIngredient(ingredientId);
// Idempotent against the optimistic client path: it already attached this
// exact render (under any kind) — don't double-file it as a second kind.
diff --git a/server/services/catalogImageAttachHook.test.js b/server/services/catalogImageAttachHook.test.js
index b9d9928f39..ce1fe18186 100644
--- a/server/services/catalogImageAttachHook.test.js
+++ b/server/services/catalogImageAttachHook.test.js
@@ -6,7 +6,9 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
const attachMedia = vi.fn(async () => ({}));
const setPortraitMedia = vi.fn(async () => ({}));
const listMediaForIngredient = vi.fn(async () => []);
-vi.mock('./catalogDB.js', () => ({ attachMedia, setPortraitMedia, listMediaForIngredient }));
+// Defaults to "ingredient exists" — the 'gone'-path test overrides per-call.
+const getIngredient = vi.fn(async () => ({ id: 'ing', deleted: false }));
+vi.mock('./catalogDB.js', () => ({ attachMedia, setPortraitMedia, listMediaForIngredient, getIngredient }));
const { mediaJobEvents } = await import('./mediaJobQueue/index.js');
const hook = await import('./catalogImageAttachHook.js');
@@ -34,6 +36,8 @@ describe('catalogImageAttachHook', () => {
setPortraitMedia.mockClear();
listMediaForIngredient.mockReset();
listMediaForIngredient.mockResolvedValue([]);
+ getIngredient.mockReset();
+ getIngredient.mockResolvedValue({ id: 'ing', deleted: false });
});
afterEach(() => {
@@ -79,6 +83,29 @@ describe('catalogImageAttachHook', () => {
expect(attachMedia).not.toHaveBeenCalled();
});
+ it('never files a render as a second kind — filename already a reference, plus an unrelated portrait', async () => {
+ // The headline guarantee: with this filename already attached (as reference)
+ // AND a different portrait present, the auto-decision must NOT also file it
+ // as a portrait — the dedup guard wins over the hasPortrait branch.
+ listMediaForIngredient.mockResolvedValue([
+ { mediaKey: 'portrait.png', kind: 'portrait' },
+ { mediaKey: 'dup.png', kind: 'reference' },
+ ]);
+ mediaJobEvents.emit('completed', completedImageJob({ catalogAttach: { ingredientId: 'ing-8' } }, 'dup.png'));
+ await new Promise((r) => setTimeout(r, 30));
+ expect(setPortraitMedia).not.toHaveBeenCalled();
+ expect(attachMedia).not.toHaveBeenCalled();
+ });
+
+ it('skips attaching when the target ingredient was deleted before the render completed', async () => {
+ getIngredient.mockResolvedValue(null); // soft- or hard-deleted: getIngredient filters deleted=false
+ mediaJobEvents.emit('completed', completedImageJob({ catalogAttach: { ingredientId: 'ing-gone' } }, 'orphan.png'));
+ await new Promise((r) => setTimeout(r, 30));
+ expect(listMediaForIngredient).not.toHaveBeenCalled();
+ expect(setPortraitMedia).not.toHaveBeenCalled();
+ expect(attachMedia).not.toHaveBeenCalled();
+ });
+
it('ignores jobs without a catalogAttach tag', async () => {
mediaJobEvents.emit('completed', completedImageJob({ universeRun: { collectionId: 'c1' } }, 'x.png'));
mediaJobEvents.emit('completed', completedImageJob({}, 'y.png'));