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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): mark the portrait guard on the optimistic-duplicate path
[._.]/ Adam Eivy90b9fcef5b4d
diff --git a/server/services/catalogImageAttachHook.js b/server/services/catalogImageAttachHook.jsindex bcb52e2148..9607bb2b5c 100644--- a/server/services/catalogImageAttachHook.js+++ b/server/services/catalogImageAttachHook.js@@ -51,7 +51,16 @@ async function attachGeneratedImage({ ingredientId, kind, filename, queuedAt })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.- if (existing.some((m) => m.mediaKey === filename)) return 'duplicate';+ const existingRow = existing.find((m) => m.mediaKey === filename);+ if (existingRow) {+ // The mounted client won the optimistic attach for this render. If it+ // landed as the PORTRAIT, still record its queuedAt in the guard — otherwise+ // a later OLDER portrait render isn't seen as stale and would clobber this+ // newer one (the guard's mark normally happens on our own setPortraitMedia+ // write below, which this duplicate path skips).+ if (existingRow.kind === 'portrait') portraitGuard.mark(ingredientId, queuedAt);+ return 'duplicate';+ }const hasPortrait = existing.some((m) => m.kind === 'portrait');// Explicit kind wins; otherwise auto: first image → portrait, later → reference// (mirrors CatalogIngredient.jsx `handleGeneratedImage`).diff --git a/server/services/catalogImageAttachHook.test.js b/server/services/catalogImageAttachHook.test.jsindex 79481a6fb0..ba0f7ddaa3 100644--- a/server/services/catalogImageAttachHook.test.js+++ b/server/services/catalogImageAttachHook.test.js@@ -197,6 +197,43 @@ describe('catalogImageAttachHook', () => {]);});+ it('marks the guard when the client already filed a newer portrait, so a later older portrait is demoted (#1791)', async () => {+ // The mounted client optimistically attached the NEWER render as the+ // portrait, so the hook sees it as a duplicate (it never runs setPortraitMedia+ // for it). The guard must still record the newer queuedAt.+ const rows = [{ mediaKey: 'new.png', kind: 'portrait' }];+ listMediaForIngredient.mockImplementation(async () => rows.map((r) => ({ ...r })));+ setPortraitMedia.mockImplementation(async (id, key) => {+ for (let i = rows.length - 1; i >= 0; i--) {+ if (rows[i].kind === 'portrait' && rows[i].mediaKey !== key) rows.splice(i, 1);+ }+ rows.push({ mediaKey: key, kind: 'portrait' });+ return {};+ });+ attachMedia.mockImplementation(async (id, key, kind) => { rows.push({ mediaKey: key, kind }); return {}; });++ // Newer render arrives at the hook but the client already filed it → duplicate.+ mediaJobEvents.emit('completed', {+ kind: 'image', params: { catalogAttach: { ingredientId: 'ing-dup', kind: 'portrait' } },+ result: { filename: 'new.png' }, queuedAt: '2026-06-29T00:00:02.000Z',+ });+ await waitFor(() => listMediaForIngredient.mock.calls.length > 0);+ // An OLDER explicit-portrait render completes afterward — it must be demoted+ // to a reference, NOT replace the newer portrait the client filed.+ mediaJobEvents.emit('completed', {+ kind: 'image', params: { catalogAttach: { ingredientId: 'ing-dup', kind: 'portrait' } },+ result: { filename: 'old.png' }, queuedAt: '2026-06-29T00:00:01.000Z',+ });+ await waitFor(() => rows.length === 2);++ expect(setPortraitMedia).not.toHaveBeenCalled(); // newer portrait never overwritten+ expect(attachMedia).toHaveBeenCalledWith('ing-dup', 'old.png', 'reference');+ expect(rows).toEqual([+ { mediaKey: 'new.png', kind: 'portrait' },+ { mediaKey: 'old.png', kind: 'reference' },+ ]);+ });+it('swallows a DB error without rejecting (best-effort)', async () => {listMediaForIngredient.mockRejectedValueOnce(new Error('db down'));// Must not throw / leave an unhandled rejection.