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: stop MusicGenPanel reverting a model picked before the engine defaults settle (#4348)
[._.]/ Adam Eivy0e1e2f1e2bed
diff --git a/client/src/components/music/MusicGenPanel.jsx b/client/src/components/music/MusicGenPanel.jsxindex db8a3e7682..56553d6973 100644--- a/client/src/components/music/MusicGenPanel.jsx+++ b/client/src/components/music/MusicGenPanel.jsx@@ -289,10 +289,17 @@ export default function MusicGenPanel({ track, title = '', artistId = '', artist// NOT clobber a freshly-selected model — it stays selected because it's now in// the list. Switching to a different engine still resets (the old model isn't// in the new engine's list). Duration seeds once.+ // Functional updater, not a read of `modelId`: this effect is keyed on the+ // ENGINE, so the `modelId` its closure captured is whatever the render that+ // queued it saw. React drains passive effects on its own scheduler task, so a+ // pick made after that commit but before the drain would be read as absent+ // and silently reset to the engine default — reverting a selection the user+ // had already made and quietly re-enabling every gate hanging off it. Reading+ // the value at flush time cannot go stale. (#4348 / PR #4819 CI failure.)useEffect(() => {if (!engine) return;const ids = (engine.models || []).map((m) => m.id);- if (!ids.includes(modelId)) setModelId(engine.defaultModelId || ids[0] || '');+ setModelId((current) => (ids.includes(current) ? current : (engine.defaultModelId || ids[0] || '')));setDurationSec((d) => (d == null ? engine.defaultDurationSec : d));}, [engine?.id, engine?.models]);@@ -511,8 +518,13 @@ export default function MusicGenPanel({ track, title = '', artistId = '', artist</label><label className="block"><span className="block text-[11px] uppercase tracking-wider text-gray-500 mb-1">Model</span>+ {/* The EFFECTIVE id, not the raw state. `modelId` is '' until the+ engine-defaults effect drains, and React leaves a select alone+ when no option matches its value — so the raw state would let+ the control display one snapshot while every readiness gate+ below reads another, for as long as that gap lasts. */}<select- value={modelId}+ value={selectedModelId}onChange={(e) => setModelId(e.target.value)}className="w-full px-2 py-1.5 bg-port-bg border border-port-border rounded text-white text-sm">diff --git a/client/src/components/music/MusicGenPanel.test.jsx b/client/src/components/music/MusicGenPanel.test.jsxindex cdeb046fdc..1d8bd455f9 100644--- a/client/src/components/music/MusicGenPanel.test.jsx+++ b/client/src/components/music/MusicGenPanel.test.jsx@@ -1,6 +1,6 @@import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';import { StrictMode } from 'react';-import { fireEvent, render, screen, waitFor } from '@testing-library/react';+import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';import MusicGenPanel from './MusicGenPanel';import * as api from '../../services/api';import toast from '../ui/Toast';@@ -807,6 +807,48 @@ describe('MusicGenPanel', () => {));});+ // Switching engine and immediately picking one of the NEW engine's snapshots+ // is the closest this harness gets to the interleaving that failed on CI+ // (#4819): the engine-defaults effect is queued holding the OLD engine's+ // model id, which is absent from the new list, so a drain that ran after the+ // pick would reset it. `act()` drains between the two discrete events, so+ // this passes either way — it is here for the end-to-end behaviour (the pick+ // survives an engine switch and every selected-snapshot gate follows it), NOT+ // as the guard for the stale-closure reset. That guard has to read the source,+ // and lives in musicGenModelSelection.guard.test.js.+ it('follows a model picked right after an engine switch', async () => {+ const alpha = engine({+ id: 'alpha', name: 'Alpha', ready: true, customModels: false,+ models: [{ id: 'alpha-1', name: 'Alpha One' }],+ defaultModelId: 'alpha-1',+ });+ const beta = engine({+ id: 'beta', name: 'Beta', ready: true, runtimeReady: true, customModels: false,+ fixedModelInstall: true, modelReady: true,+ modelReadyById: { 'beta-1': true, 'beta-2': false },+ modelSizeGbById: { 'beta-1': 14, 'beta-2': 29 },+ models: [{ id: 'beta-1', name: 'Beta One' }, { id: 'beta-2', name: 'Beta Two' }],+ defaultModelId: 'beta-1',+ });+ api.listMusicEngines.mockResolvedValue({ defaultEngine: 'alpha', engines: [alpha, beta] });++ render(<MusicGenPanel prompt="cinematic score" lyrics="" />);+ const engineSelect = await screen.findByRole('combobox', { name: /engine/i });++ await act(async () => {+ // Commit 1: engine becomes Beta while modelId is still 'alpha-1', queuing+ // the defaults effect with that stale id.+ fireEvent.change(engineSelect, { target: { value: 'beta' } });+ // Commit 2: the user picks Beta's un-installed snapshot before the drain.+ fireEvent.change(screen.getByRole('combobox', { name: /model/i }), { target: { value: 'beta-2' } });+ });++ expect(screen.getByRole('combobox', { name: /model/i })).toHaveValue('beta-2');+ expect(screen.getByText(/selected model weights are not installed yet/i)).toBeInTheDocument();+ expect(screen.getByRole('button', { name: /download model weights \(~29 GB\)/i })).toBeInTheDocument();+ expect(screen.getByRole('button', { name: /generate track/i })).toBeDisabled();+ });+it('shows honest elapsed GPU feedback while MiniMax generation is running', async () => {api.listMusicEngines.mockResolvedValue({defaultEngine: 'minimax-music3',diff --git a/client/src/components/music/musicGenModelSelection.guard.test.js b/client/src/components/music/musicGenModelSelection.guard.test.jsnew file mode 100644index 0000000000..b27ea89f18--- /dev/null+++ b/client/src/components/music/musicGenModelSelection.guard.test.js@@ -0,0 +1,66 @@+// @vitest-environment node++/**+ * Guard: MusicGenPanel's model selection must never disagree with itself.+ *+ * Two shapes, one bug class, both of which shipped red on PR #4819's CI and+ * neither of which a rendering test in this harness can reproduce — `act()`+ * drains React's passive effects between discrete events, which is exactly the+ * drain a loaded CI runner does NOT do at that moment.+ *+ * ## 1. The engine-defaults effect must reset through a functional updater+ *+ * The broken shape reads `modelId` out of the closure:+ *+ * useEffect(() => {+ * const ids = (engine.models || []).map((m) => m.id);+ * if (!ids.includes(modelId)) setModelId(engine.defaultModelId || ids[0] || '');+ * }, [engine?.id, engine?.models]); // ← modelId is NOT a dep+ *+ * The effect is keyed on the ENGINE, so the `modelId` it reads is whatever the+ * render that queued it captured. React drains passive effects on its own+ * scheduler task, separate from the DOM mutation a `findBy*` query resolves on,+ * so a user pick landing in that window is invisible to the queued effect: it+ * reads the pick as absent and resets to the engine default. The selection+ * silently reverts, and every gate hanging off it — the "selected model weights+ * are not installed yet" warning, the sized download button, the disabled+ * Generate — goes quiet as if the un-installed snapshot had never been picked.+ *+ * Reading the value at flush time (`setModelId((current) => …)`) cannot go+ * stale. Adding `modelId` to the dep array does NOT fix it: the already-queued+ * effect still runs with its own closure before the re-keyed one does.+ *+ * ## 2. The Model <select> must be bound to the EFFECTIVE model id+ *+ * `modelId` is `''` until that effect drains, and React leaves a `<select>`+ * alone when no option matches its value — so the control keeps displaying its+ * first option while the state says "nothing selected". Bind the raw state and+ * the control shows one snapshot while every readiness gate below it reads+ * another (`selectedModelId`, which falls back through `defaultModelId`), for as+ * long as that gap lasts. That divergence is what made the CI failure look+ * impossible: the assertion that the select "has value 8-bit" passed while the+ * component held no selection at all.+ */++import { describe, it, expect } from 'vitest';+import { readFileSync } from 'node:fs';++const source = readFileSync(new URL('./MusicGenPanel.jsx', import.meta.url), 'utf8');++describe('MusicGenPanel model selection', () => {+ it('resets the model through a functional updater, never a closure read', () => {+ // Scoped to the engine-defaults effect. Other call sites legitimately+ // name the default (removing the model that is currently selected); only+ // THIS one runs from a queued closure that can be older than a user pick.+ const start = source.indexOf('if (!engine) return;');+ const effect = source.slice(start, source.indexOf('}, [engine?.id, engine?.models]);', start));+ expect(effect).not.toMatch(/if \(!ids\.includes\(modelId\)\)/);+ expect(effect).toMatch(/setModelId\(\(current\) => \(ids\.includes\(current\)/);+ });++ it('binds the Model select to the effective id, not the raw state', () => {+ const modelSelect = source.slice(source.indexOf('>Model</span>'));+ expect(modelSelect).toMatch(/<select\s+value=\{selectedModelId\}/);+ expect(modelSelect.slice(0, modelSelect.indexOf('</select>'))).not.toMatch(/value=\{modelId\}/);+ });+});diff --git a/client/src/hooks/useFederatedMediaTarget.js b/client/src/hooks/useFederatedMediaTarget.jsindex 1df8acc0e8..15c3b8038c 100644--- a/client/src/hooks/useFederatedMediaTarget.js+++ b/client/src/hooks/useFederatedMediaTarget.js@@ -79,20 +79,26 @@ export function useFederatedMediaTarget(kind) {const [peerId, setPeerId] = useState('');const [modelKey, setModelKey] = useState('');+ // Only the opted-in peers are stored, and only when there is at least one:+ // on an install with no media provider (the common case) the mount fetch then+ // produces NO state update at all, so adding this hook to a form costs it no+ // extra commit. Filtering here rather than in a `useMemo` below is what makes+ // that possible — the raw list is never state, so its identity cannot churn.useEffect(() => {getInstances({ silent: true })- .then((data) => { if (Array.isArray(data?.peers)) setAllPeers(data.peers); })+ .then((data) => {+ if (!Array.isArray(data?.peers)) return;+ const providers = data.peers.filter((candidate) => candidate?.mediaProvider?.enabled === true);+ setAllPeers((current) => (providers.length === 0 && current.length === 0 ? current : providers));+ }).catch(() => {});}, []);- // Opting a peer in is a local config fact that survives a failed probe, so- // the list is filtered on that alone. A peer whose capacity has gone stale- // stays listed (carrying its own suffix) rather than vanishing from the- // dropdown with no explanation of where it went.- const peers = useMemo(- () => allPeers.filter((candidate) => candidate?.mediaProvider?.enabled === true),- [allPeers],- );+ // Opting a peer in is a local config fact that survives a failed probe, which+ // is why the stored list is filtered on that alone (above). A peer whose+ // capacity has gone stale stays listed — carrying its own suffix — rather+ // than vanishing from the dropdown with no explanation of where it went.+ const peers = allPeers;const peer = useMemo(() => peers.find((candidate) => candidate.id === peerId) || null, [peers, peerId]);const models = useMemo(() => federatedMediaModelsForPeer(peer, kind), [peer, kind]);// Fall back to the first option for the same reason a native <select> does: