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
fix: save the Universe Builder draft before a batch render
[._.]/ Adam Eivyf79158a0cfdf
diff --git a/client/src/components/universeBuilder/UniverseBuilderPage.jsx b/client/src/components/universeBuilder/UniverseBuilderPage.jsxindex f4122ebe57..d748eb3f81 100644--- a/client/src/components/universeBuilder/UniverseBuilderPage.jsx+++ b/client/src/components/universeBuilder/UniverseBuilderPage.jsx@@ -123,13 +123,13 @@ export default function UniverseBuilder() {defaultMode,draft,draftRef,+ flushDraftIfDirty,handleCanonChange,handleCreateNamed,handleDelete,handleSave,imageCfg,imageModels,- isDraftDirty,loading,markDraftSaved,mountedRef,@@ -172,6 +172,7 @@ export default function UniverseBuilder() {defaultMode,runs,setRuns,+ preflight: flushDraftIfDirty,});const { expanding, handleExpand, refine } = useUniverseExpand({selectedId,@@ -241,14 +242,6 @@ export default function UniverseBuilder() {// single atomic patch server-side so the universe ends up consistent or// unchanged. Renames the LLM suggests are surfaced in the toast but not// auto-applied (the user can rename manually if they want it).- // Returns true when the draft is clean or save succeeded; false (with- // handleSave's own error toast already raised) when save failed.- const flushDraftIfDirty = useCallback(async () => {- if (!isDraftDirty()) return true;- const saved = await handleSave();- return !!saved;- }, [isDraftDirty]);-const handleAutoSort = () => runUniverseAction({ref: autoSortingRef,setBusy: setAutoSorting,diff --git a/client/src/hooks/useUniverseDraft.js b/client/src/hooks/useUniverseDraft.jsindex ffcc1dc761..80a6604eec 100644--- a/client/src/hooks/useUniverseDraft.js+++ b/client/src/hooks/useUniverseDraft.js@@ -226,6 +226,18 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {return result;};+ // Preflight for any server action that reads the PERSISTED universe (the LLM+ // actions, batch render): persist a dirty draft first, so the server operates+ // on what the user is looking at rather than the last-saved snapshot. Lives+ // here — beside the `isDraftDirty` / `handleSave` pair it is derived from —+ // so every consumer shares one definition of the contract.+ // Returns true when the draft is clean or the save succeeded; false (with+ // handleSave's own error toast already raised) when the save failed.+ const flushDraftIfDirty = async () => {+ if (!isDraftDirty()) return true;+ return !!(await handleSave());+ };+const handleCreateNamed = async (rawName) => {const name = (rawName || '').trim();if (!name) {@@ -388,6 +400,7 @@ export default function useUniverseDraft({ selectedId, goToWorld }) {defaultMode,draft,draftRef,+ flushDraftIfDirty,handleCanonChange,handleCreateNamed,handleDelete,diff --git a/client/src/hooks/useUniverseRender.js b/client/src/hooks/useUniverseRender.jsindex f16f0cc731..7e5e956b6e 100644--- a/client/src/hooks/useUniverseRender.js+++ b/client/src/hooks/useUniverseRender.js@@ -18,6 +18,15 @@ export default function useUniverseRender({defaultMode,runs,setRuns,+ // Same contract as `useUniverseAction`'s `preflight`: runs after the cheap+ // guards, owns its own error toast, and a falsy return aborts the action.+ // Required here rather than optional — `/render` compiles prompts from the+ // SERVER's copy of the universe while every count/label in the scope comes+ // from the in-memory draft, so skipping the flush renders an unsaved+ // variation's stale prompt, or 400s with WORLD_BUILDER_EMPTY ("No prompts to+ // render") on one the UI is showing. A caller that forgets it should fail+ // loudly, not silently regress.+ preflight,}) {const [rendering, setRendering] = useState(false);const [renderOpts, setRenderOpts] = useLocalStoragePersisted(@@ -48,6 +57,8 @@ export default function useUniverseRender({toast.error('Configure an image-gen backend first');return;}+ // After the cheap guards so a doomed click doesn't trigger a write.+ if (!(await preflight())) return;const effectiveMode = renderOpts.mode || defaultMode || undefined;const numericOrUndefined = (value) => {if (value === '' || value == null) return undefined;diff --git a/client/src/hooks/useUniverseRender.test.jsx b/client/src/hooks/useUniverseRender.test.jsxindex 2303c2844a..aa5551e941 100644--- a/client/src/hooks/useUniverseRender.test.jsx+++ b/client/src/hooks/useUniverseRender.test.jsx@@ -38,7 +38,7 @@ beforeEach(() => {apiMocks.listWorldRuns.mockResolvedValue([{ id: 'run-2' }]);});-const renderUniverseRender = (availableBackends = [{ id: 'local' }]) => {+const renderUniverseRender = (availableBackends = [{ id: 'local' }], preflight = async () => true) => {const setRuns = vi.fn();const hook = renderHook(() => useUniverseRender({selectedId: 'u1',@@ -47,6 +47,7 @@ const renderUniverseRender = (availableBackends = [{ id: 'local' }]) => {defaultMode: 'local',runs: [],setRuns,+ preflight,}));return { ...hook, setRuns };};@@ -75,4 +76,38 @@ describe('useUniverseRender', () => {expect(apiMocks.renderWorld).not.toHaveBeenCalled();expect(toastMock.error).toHaveBeenCalledWith('Configure an image-gen backend first');});++ // /render compiles from the PERSISTED universe, so an unsaved variation the+ // UI already counts would 400 with WORLD_BUILDER_EMPTY ("No prompts to+ // render") — the preflight persists it first.+ it('runs the preflight before POSTing the render', async () => {+ const preflight = vi.fn().mockResolvedValue(true);+ const { result } = renderUniverseRender([{ id: 'local' }], preflight);+ await act(async () => {+ await result.current.handleRender({ promptMode: 'variations', selection: { heroes: 'all' } });+ });++ expect(preflight).toHaveBeenCalledTimes(1);+ expect(apiMocks.renderWorld).toHaveBeenCalledTimes(1);+ expect(preflight.mock.invocationCallOrder[0])+ .toBeLessThan(apiMocks.renderWorld.mock.invocationCallOrder[0]);+ });++ it('aborts the render when the preflight fails', async () => {+ const preflight = vi.fn().mockResolvedValue(false);+ const { result } = renderUniverseRender([{ id: 'local' }], preflight);+ await act(async () => {+ await result.current.handleRender({ promptMode: 'variations', selection: { heroes: 'all' } });+ });++ expect(apiMocks.renderWorld).not.toHaveBeenCalled();+ });++ it('skips the preflight entirely when the guards reject the render', async () => {+ const preflight = vi.fn().mockResolvedValue(true);+ const { result } = renderUniverseRender([], preflight);+ await act(async () => { await result.current.handleRender(); });++ expect(preflight).not.toHaveBeenCalled();+ });});