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): stop a stale cross-universe style-reference save from corrupting the newly selected universe
[._.]/ Adam Eivy28a19a788606
diff --git a/client/src/hooks/useUniverseDraft.js b/client/src/hooks/useUniverseDraft.jsindex 63773be747..5e692d672c 100644--- a/client/src/hooks/useUniverseDraft.js+++ b/client/src/hooks/useUniverseDraft.js@@ -102,6 +102,14 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {// selectedId-load effect below).const styleReferenceQueueRef = useRef(Promise.resolve());const styleReferenceSnapshotRef = useRef(null);+ // Bumped every time the selection changes (see the selectedId-load effect+ // below). A pending add/remove for universe A can resolve AFTER the user+ // has already switched to universe B — without this, A's stale response+ // would repopulate styleReferenceSnapshotRef and get sent as B's next+ // wholesale styleReferences PATCH, silently replacing B's real references.+ // Each mutator captures the generation before its await and only applies+ // its result if the generation is still current on resolve.+ const universeSelectionGenerationRef = useRef(0);useEffect(() => () => { mountedRef.current = false; }, []);useEffect(() => { draftRef.current = draft; }, [draft]);@@ -161,9 +169,13 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {clearPendingCanonAdditions();// A new selection means any in-flight removeStyleReference chain belongs// to the PREVIOUS universe — reset so the next removal seeds its snapshot- // from the freshly-hydrated draft instead of a stale/foreign one.+ // from the freshly-hydrated draft instead of a stale/foreign one. Bumping+ // the generation also stops a still-pending add/remove for the OLD+ // universe from applying its result once it resolves (see+ // universeSelectionGenerationRef above).styleReferenceQueueRef.current = Promise.resolve();styleReferenceSnapshotRef.current = null;+ universeSelectionGenerationRef.current += 1;if (!selectedId) {const empty = createEmptyUniverseDraft();setDraft(empty);@@ -307,6 +319,7 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {const persistStyleReference = useCallback(async ({ reference, proposed, adopt }) => {if (!selectedId || !reference) return false;+ const generation = universeSelectionGenerationRef.current;const current = draftRef.current || draft;const capturedStyle = {styleNotes: current.styleNotes || '',@@ -335,25 +348,33 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {return null;});if (!updated) return false;- // Keep the shared snapshot current so a later removeStyleReference call- // builds its replacement array from a list that includes this addition —- // otherwise a stale post-removal snapshot would silently drop it (codex- // review finding).- styleReferenceSnapshotRef.current = updated.styleReferences || [];- if (adopt) markStyleGuidanceSaved(updated);- setDraft((latest) => {- const styleUnchangedDuringSave = latest.styleNotes === capturedStyle.styleNotes- && sameJsonShape(ensureInfluences(latest.influences), capturedStyle.influences);- return {- ...latest,- styleReferences: updated.styleReferences || [],- updatedAt: updated.updatedAt,- ...(adopt && styleUnchangedDuringSave ? {- styleNotes: updated.styleNotes || '',- influences: ensureInfluences(updated.influences),- } : {}),- };- });+ // The user may have switched to a different universe while this PATCH was+ // in flight. `updated` is still a legitimate save FOR THIS UNIVERSE, so+ // the worlds list (setWorlds, below) always reflects it — but applying it+ // to the shared snapshot/open draft now would poison the NEWLY selected+ // universe's state with THIS universe's references (codex review+ // finding), so those two are gated on the selection being unchanged.+ if (universeSelectionGenerationRef.current === generation) {+ // Keep the shared snapshot current so a later removeStyleReference call+ // builds its replacement array from a list that includes this addition+ // — otherwise a stale post-removal snapshot would silently drop it+ // (codex review finding).+ styleReferenceSnapshotRef.current = updated.styleReferences || [];+ if (adopt) markStyleGuidanceSaved(updated);+ setDraft((latest) => {+ const styleUnchangedDuringSave = latest.styleNotes === capturedStyle.styleNotes+ && sameJsonShape(ensureInfluences(latest.influences), capturedStyle.influences);+ return {+ ...latest,+ styleReferences: updated.styleReferences || [],+ updatedAt: updated.updatedAt,+ ...(adopt && styleUnchangedDuringSave ? {+ styleNotes: updated.styleNotes || '',+ influences: ensureInfluences(updated.influences),+ } : {}),+ };+ });+ }setWorlds((previous) => upsertByIdPrepend(previous, updated));toast.success(adopt ? 'Art reference added and style guide updated' : 'Art reference added');return true;@@ -361,26 +382,44 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {const removeStyleReference = useCallback((referenceId) => {if (!selectedId) return Promise.resolve(false);+ const generation = universeSelectionGenerationRef.current;+ const targetId = selectedId;// Chain onto the queue so a second removal clicked before the first PATCH// resolves derives its replacement array from the FIRST removal's actual// result (tracked in styleReferenceSnapshotRef), not from draftRef — which// only syncs one React effect later and would still show both items present.const task = styleReferenceQueueRef.current.then(async () => {+ // This task can be dequeued well after the user switched away from+ // targetId (a prior queued removal for the SAME universe can still be+ // pending across the switch). At that point styleReferenceSnapshotRef/+ // draftRef hold the NEWLY selected universe's data, not targetId's — so+ // computing `base` here would derive a replacement array from the wrong+ // universe entirely and send it TO targetId, corrupting it (codex+ // review finding: the queue reset alone doesn't stop an+ // already-in-flight chained task from running). Abort rather than risk+ // that; the user has moved on from this removal's universe anyway.+ if (universeSelectionGenerationRef.current !== generation) return false;const base = styleReferenceSnapshotRef.current?? (draftRef.current || draft).styleReferences?? [];const styleReferences = base.filter((item) => item.id !== referenceId);- const updated = await updateUniverse(selectedId, { styleReferences }, { silent: true }).catch((error) => {+ const updated = await updateUniverse(targetId, { styleReferences }, { silent: true }).catch((error) => {toast.error(`Reference removal failed: ${error.message}`);return null;});if (!updated) return false;- styleReferenceSnapshotRef.current = updated.styleReferences || [];- setDraft((latest) => ({- ...latest,- styleReferences: updated.styleReferences || [],- updatedAt: updated.updatedAt,- }));+ // Same cross-selection guard as persistStyleReference: the worlds list+ // always reflects this (correct) save, but the shared snapshot/open+ // draft must not be overwritten with a DIFFERENT, now-selected+ // universe's state (codex review finding).+ if (universeSelectionGenerationRef.current === generation) {+ styleReferenceSnapshotRef.current = updated.styleReferences || [];+ setDraft((latest) => ({+ ...latest,+ styleReferences: updated.styleReferences || [],+ updatedAt: updated.updatedAt,+ }));+ }setWorlds((previous) => upsertByIdPrepend(previous, updated));toast.success('Art reference removed');return true;diff --git a/client/src/hooks/useUniverseDraft.test.jsx b/client/src/hooks/useUniverseDraft.test.jsxindex 000cfb6dc3..99c2972638 100644--- a/client/src/hooks/useUniverseDraft.test.jsx+++ b/client/src/hooks/useUniverseDraft.test.jsx@@ -210,4 +210,51 @@ describe('useUniverseDraft', () => {expect(apiMocks.updateUniverse.mock.calls.at(-1)[1]).toEqual({ styleReferences: [refC] });expect(result.current.draft.styleReferences).toEqual([refC]);});++ it('does not let a stale in-flight save for one universe overwrite a different, now-selected universe (codex review finding)', async () => {+ const universeTwo = {+ ...universe,+ id: 'u2',+ name: 'Second Universe',+ styleReferences: [{ id: 'style-ref-b2', title: 'Ref B2', prompt: 'b2', imageRefs: ['b2.png'] }],+ };+ apiMocks.getUniverse.mockImplementation(async (id) => (id === 'u2' ? universeTwo : universe));++ const goToWorld = vi.fn();+ const { result, rerender } = renderHook(+ ({ selectedId }) => useUniverseDraft({ selectedId, goToWorld }),+ { initialProps: { selectedId: 'u1' } },+ );+ await waitFor(() => expect(result.current.draft.id).toBe('u1'));++ // Start a save for u1 but hold its PATCH response open — simulates the+ // user switching universes before this request resolves. Not wrapped in+ // act(): nothing synchronous before its first await touches React state.+ let resolveU1Save;+ apiMocks.updateUniverse.mockImplementationOnce(() => new Promise((resolve) => { resolveU1Save = resolve; }));+ const referenceForU1 = { id: 'style-ref-stale', title: 'Stale', prompt: 'stale', imageRefs: ['stale.png'] };+ const staleSave = result.current.persistStyleReference({ reference: referenceForU1, adopt: false });++ // Switch to u2 while u1's save is still pending.+ await act(async () => { rerender({ selectedId: 'u2' }); });+ await waitFor(() => expect(result.current.draft.id).toBe('u2'));+ expect(result.current.draft.styleReferences).toEqual(universeTwo.styleReferences);++ // Now let u1's stale save resolve.+ await act(async () => {+ resolveU1Save({ ...universe, styleReferences: [referenceForU1] });+ await staleSave;+ });++ // u2's displayed draft must be untouched by u1's stale, now-resolved response.+ expect(result.current.draft.id).toBe('u2');+ expect(result.current.draft.styleReferences).toEqual(universeTwo.styleReferences);++ // A subsequent removal on u2 must build its PATCH from u2's OWN+ // references, not from u1's stale snapshot.+ await act(async () => {+ await result.current.removeStyleReference(universeTwo.styleReferences[0].id);+ });+ expect(apiMocks.updateUniverse.mock.calls.at(-1)).toEqual(['u2', { styleReferences: [] }, { silent: true }]);+ });});