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 (codex): keep the style-reference snapshot fresh across adds, not just removes

[._.]/ Adam Eivy3e270d2afd9b
diff --git a/client/src/hooks/useUniverseDraft.js b/client/src/hooks/useUniverseDraft.js
index f42aa76c89..63773be747 100644
--- a/client/src/hooks/useUniverseDraft.js
+++ b/client/src/hooks/useUniverseDraft.js
@@ -90,13 +90,16 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {
const savedDraftSnapshotRef = useRef(universeDraftSnapshot(createEmptyUniverseDraft()));
const savedStyleSnapshotRef = useRef(ensureInfluences(createEmptyUniverseDraft().influences));
const pendingCanonAdditionsRef = useRef(emptyPendingCanon());
- // Serializes removeStyleReference calls and tracks the array across them —
- // draftRef only syncs after a React effect, which can still be one render
- // behind when two removals fire before either PATCH resolves; both would
- // otherwise derive their replacement array from the same stale list and the
- // second request's wholesale-replace PATCH would silently restore the item
- // the first request just removed. Reset whenever the selected universe
- // changes (see the selectedId-load effect below).
+ // Shared authoritative snapshot of styleReferences that both
+ // persistStyleReference (add) and removeStyleReference (remove) read from
+ // and write back to — draftRef only syncs after a React effect, which can
+ // still be one render behind when two mutations fire before either PATCH
+ // resolves; both would otherwise derive their replacement array from the
+ // same stale list and the slower request's wholesale-replace PATCH would
+ // silently undo the other's change. removeStyleReference additionally
+ // serializes through styleReferenceQueueRef so back-to-back removals never
+ // overlap. Reset whenever the selected universe changes (see the
+ // selectedId-load effect below).
const styleReferenceQueueRef = useRef(Promise.resolve());
const styleReferenceSnapshotRef = useRef(null);
@@ -309,14 +312,17 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {
styleNotes: current.styleNotes || '',
influences: ensureInfluences(current.influences),
};
- if ((current.styleReferences || []).length >= WORLD_STYLE_REFERENCES_MAX) {
+ // Same authoritative snapshot removeStyleReference maintains — reading
+ // draftRef here instead would miss a removal still settling and could
+ // resurrect the item it just removed once this add's wholesale-replace
+ // PATCH lands.
+ const baseStyleReferences = styleReferenceSnapshotRef.current
+ ?? (Array.isArray(current.styleReferences) ? current.styleReferences : []);
+ if (baseStyleReferences.length >= WORLD_STYLE_REFERENCES_MAX) {
toast.error(`A universe can hold up to ${WORLD_STYLE_REFERENCES_MAX} art references`);
return false;
}
- const styleReferences = [
- ...(Array.isArray(current.styleReferences) ? current.styleReferences : []),
- reference,
- ];
+ const styleReferences = [...baseStyleReferences, reference];
const patch = {
styleReferences,
...(adopt ? {
@@ -329,6 +335,11 @@ 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
diff --git a/client/src/hooks/useUniverseDraft.test.jsx b/client/src/hooks/useUniverseDraft.test.jsx
index 08c9683abb..000cfb6dc3 100644
--- a/client/src/hooks/useUniverseDraft.test.jsx
+++ b/client/src/hooks/useUniverseDraft.test.jsx
@@ -185,4 +185,29 @@ describe('useUniverseDraft', () => {
expect(apiMocks.updateUniverse.mock.calls[1][1]).toEqual({ styleReferences: [] });
expect(result.current.draft.styleReferences).toEqual([]);
});
+
+ it('keeps a reference added between two removals (remove A, add C, remove B)', async () => {
+ const refA = { id: 'style-ref-a', title: 'Ref A', prompt: 'moody', imageRefs: ['a.png'] };
+ const refB = { id: 'style-ref-b', title: 'Ref B', prompt: 'bright', imageRefs: ['b.png'] };
+ const refC = { id: 'style-ref-c', title: 'Ref C', prompt: 'sunlit', imageRefs: ['c.png'] };
+ apiMocks.getUniverse.mockResolvedValueOnce({ ...universe, styleReferences: [refA, refB] });
+ const { result } = renderDraft();
+ await waitFor(() => expect(result.current.draft.styleReferences).toEqual([refA, refB]));
+
+ await act(async () => { await result.current.removeStyleReference(refA.id); });
+ expect(apiMocks.updateUniverse.mock.calls.at(-1)[1]).toEqual({ styleReferences: [refB] });
+
+ await act(async () => {
+ await result.current.persistStyleReference({ reference: refC, adopt: false });
+ });
+ // persistStyleReference must build its PATCH from the post-removal
+ // snapshot (refB), not a stale pre-removal draft read.
+ expect(apiMocks.updateUniverse.mock.calls.at(-1)[1]).toEqual({ styleReferences: [refB, refC] });
+
+ await act(async () => { await result.current.removeStyleReference(refB.id); });
+ // Regression: removeStyleReference's snapshot must have picked up refC
+ // from the add above, or this PATCH would silently drop refC too.
+ expect(apiMocks.updateUniverse.mock.calls.at(-1)[1]).toEqual({ styleReferences: [refC] });
+ expect(result.current.draft.styleReferences).toEqual([refC]);
+ });
});