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

fix: apply the universe pull against the latest references, not a stale snapshot

[._.]/ Adam Eivy36a1ad834917
diff --git a/client/src/components/musicVideo/VisualSpecPanel.jsx b/client/src/components/musicVideo/VisualSpecPanel.jsx
index d94033959b..b1cdff20c2 100644
--- a/client/src/components/musicVideo/VisualSpecPanel.jsx
+++ b/client/src/components/musicVideo/VisualSpecPanel.jsx
@@ -1,4 +1,4 @@
-import { useState } from 'react';
+import { useRef, useState } from 'react';
import { ImagePlus, Trash2, Plus, Palette, Sparkles } from 'lucide-react';
import useFieldDraft from '../../hooks/useFieldDraft.js';
import MoodBoardReferenceStrip from '../moodBoard/MoodBoardReferenceStrip.jsx';
@@ -43,13 +43,20 @@ export default function VisualSpecPanel({ project, onSave, onAddReference }) {
// characters/places/objects as references instead of re-picking every one
// by hand through Add reference. Idempotent: images already present are
// skipped, so a second click adds nothing new.
+ //
+ // The universe read is async, so an edit/remove/Add-reference the director
+ // makes WHILE it's in flight must not be clobbered by a stale `references`
+ // snapshot once it resolves. Read the base list through a ref that tracks
+ // every render instead of the click-time closure value.
+ const referencesRef = useRef(references);
+ referencesRef.current = references;
const universeId = project.concept?.universeId || null;
const handlePullFromUniverse = () => {
if (!universeId || pulling) return;
setPulling(true);
getUniverse(universeId, { silent: true })
.then((universe) => {
- const { next, added, skipped } = pullUniverseCanonReferences(universe, references);
+ const { next, added, skipped } = pullUniverseCanonReferences(universe, referencesRef.current);
if (added > 0) saveReferences(next);
if (added > 0) {
toast.success(`Pulled ${added} reference${added === 1 ? '' : 's'} from the universe${skipped ? ` (${skipped} skipped)` : ''}`);
diff --git a/client/src/pages/MusicVideo.test.jsx b/client/src/pages/MusicVideo.test.jsx
index eae5646181..63a1d3fb1e 100644
--- a/client/src/pages/MusicVideo.test.jsx
+++ b/client/src/pages/MusicVideo.test.jsx
@@ -1195,6 +1195,34 @@ describe('MusicVideo pull references from universe (#8978)', () => {
await waitFor(() => expect(toast.error).toHaveBeenCalledWith('Every canon image is already a reference'));
});
+ it('applies the pull against the latest references, not a stale click-time snapshot (race guard)', async () => {
+ let resolveUniverse;
+ getUniverse.mockReturnValueOnce(new Promise((resolve) => { resolveUniverse = resolve; }));
+ await openProject(UNIVERSE_PROJECT);
+
+ fireEvent.click(await screen.findByRole('button', { name: /^Pull from universe$/ }));
+ await waitFor(() => expect(getUniverse).toHaveBeenCalledTimes(1));
+
+ // While the universe fetch is still in flight, remove the existing
+ // reference — a stale click-time snapshot would resurrect it below.
+ fireEvent.click(screen.getByRole('button', { name: 'Remove reference' }));
+ await waitFor(() => expect(updateMusicVideoProject).toHaveBeenCalledWith(
+ 'mv-universe', { visualSpec: { references: [] } }, { silent: true },
+ ));
+
+ await act(async () => {
+ resolveUniverse({ characters: [{ id: 'c1', name: 'Nyra', primaryImageRef: 'nyra.png' }], places: [], objects: [] });
+ });
+
+ await waitFor(() => expect(updateMusicVideoProject).toHaveBeenLastCalledWith(
+ 'mv-universe',
+ { visualSpec: { references: [
+ { id: expect.any(String), imageId: 'nyra.png', role: 'character', label: 'Nyra', condition: false },
+ ] } },
+ { silent: true },
+ ));
+ });
+
it('does not offer Pull from universe when the project has no linked universe', async () => {
await openProject(PROJECT_NO_CLIP);
expect(screen.queryByRole('button', { name: /Pull from universe/i })).toBeNull();