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): register HF model only after a successful download + keep it selected

[._.]/ Adam Eivy384cab08b7c3
diff --git a/client/src/components/music/MusicGenPanel.jsx b/client/src/components/music/MusicGenPanel.jsx
index 5bd115853b..8482c318cd 100644
--- a/client/src/components/music/MusicGenPanel.jsx
+++ b/client/src/components/music/MusicGenPanel.jsx
@@ -51,12 +51,19 @@ export default function MusicGenPanel({ track, prompt, lyrics, onGenerated }) {
const engine = useMemo(() => engines.find((e) => e.id === engineId) || null, [engines, engineId]);
- // Reset model + duration to the engine's defaults whenever the engine changes.
+ // Keep the model selection valid for the current engine: reset to the engine
+ // default ONLY when the selected model isn't in this engine's list. Keying on
+ // engine.id (not the object) + this guard means a list refresh after installing
+ // a model (which replaces the engines array but keeps the same engine id) does
+ // 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.
useEffect(() => {
if (!engine) return;
- setModelId(engine.defaultModelId || engine.models?.[0]?.id || '');
+ const ids = (engine.models || []).map((m) => m.id);
+ if (!ids.includes(modelId)) setModelId(engine.defaultModelId || ids[0] || '');
setDurationSec((d) => (d == null ? engine.defaultDurationSec : d));
- }, [engine]);
+ }, [engine?.id, engine?.models]);
const canGenerate = !!engine?.ready && !!prompt?.trim() && !generating && !!track?.id;
diff --git a/server/routes/music.js b/server/routes/music.js
index 52e986496d..e1c2942bbe 100644
--- a/server/routes/music.js
+++ b/server/routes/music.js
@@ -23,6 +23,7 @@ import { validateRequest } from '../lib/validation.js';
import { ENGINES, DEFAULT_ENGINE_ID, getEngine, isEngineReady, generateMusic } from '../services/pipeline/musicGen.js';
import { listEngineModels, addAudioModel, removeAudioModel, isValidRepoId } from '../services/audioModels.js';
import { startHfDownloadStream } from '../lib/sseDownload.js';
+import { inspectModelCache } from '../lib/hfCache.js';
import * as tracks from '../services/tracks/index.js';
import * as albums from '../services/albums/index.js';
@@ -73,11 +74,12 @@ router.get('/models/:engine', asyncHandler(async (req, res) => {
res.json({ models: await listEngineModels(req.params.engine) });
}));
-// POST /api/music/models — register an HF repo for an engine, then STREAM its
-// download as SSE (text/event-stream). Registering first (before the bytes land)
-// is intentional: the generation sidecars auto-download a missing checkpoint on
-// first use, so even a cancelled pre-download leaves a usable (lazily-fetched)
-// model — same posture as the shipped models.
+// POST /api/music/models — STREAM the HF download as SSE (text/event-stream),
+// then register the repo only if it actually landed in the cache. Registering
+// AFTER a successful download (rather than up front) means a typo / private /
+// gated / auth-failed repo never persists into data/audio-models.json as a
+// bogus "installed" model that a later /engines list would surface and that
+// generation would fail on.
router.post('/models', asyncHandler(async (req, res) => {
const body = validateRequest(installSchema, req.body ?? {});
if (!ENGINES[body.engine]) throw new ServerError('Unknown audio engine', { status: 400, code: 'AUDIO_MODEL_UNKNOWN_ENGINE' });
@@ -88,10 +90,17 @@ router.post('/models', asyncHandler(async (req, res) => {
throw new ServerError(`${ENGINES[body.engine].name} does not support custom HuggingFace models`, { status: 400, code: 'AUDIO_MODEL_ENGINE_FIXED' });
}
if (!isValidRepoId(body.repo)) throw new ServerError('Invalid HuggingFace repo id', { status: 400, code: 'AUDIO_MODEL_INVALID_REPO' });
- await addAudioModel({ engine: body.engine, repo: body.repo, name: body.name });
// Hand the response to the shared SSE driver — it owns writeHead/end + the
- // in-flight dedupe + client-disconnect kill. The cache pre-warm is best-effort.
+ // in-flight dedupe + client-disconnect kill. It resolves after the stream ends
+ // (the HTTP response is already closed by then, so the post-stream work below
+ // is a side effect only — it can't change what the client received).
await startHfDownloadStream({ req, res, repo: body.repo });
+ // Register only if the weights are actually present now — a failed/cancelled
+ // download leaves the cache empty and we must NOT persist the model.
+ const cached = await inspectModelCache(body.repo).catch(() => ({ cached: false }));
+ if (cached.cached) {
+ await addAudioModel({ engine: body.engine, repo: body.repo, name: body.name }).catch(() => {});
+ }
}));
// DELETE /api/music/models/:engine/*id — de-register a user-added model. The id
diff --git a/server/routes/music.test.js b/server/routes/music.test.js
index a261f931e2..5f37aa2f5a 100644
--- a/server/routes/music.test.js
+++ b/server/routes/music.test.js
@@ -40,6 +40,10 @@ vi.mock('../services/audioModels.js', () => ({
const sse = vi.hoisted(() => ({ run: vi.fn(async ({ res }) => { res.writeHead(200, { 'Content-Type': 'text/event-stream' }); res.end('data: {"type":"complete"}\n\n'); }) }));
vi.mock('../lib/sseDownload.js', () => ({ startHfDownloadStream: (args) => sse.run(args) }));
+// Register-after-download gates on whether the repo landed in the cache.
+const cache = vi.hoisted(() => ({ cached: true }));
+vi.mock('../lib/hfCache.js', () => ({ inspectModelCache: vi.fn(async () => ({ cached: cache.cached })) }));
+
vi.mock('../services/albums/index.js', () => ({
getAlbum: vi.fn(async () => null),
updateAlbum: vi.fn(async (id, patch) => ({ id, ...patch })),
@@ -76,6 +80,7 @@ describe('music routes', () => {
albums.updateAlbum.mockReset().mockImplementation(async (id, patch) => ({ id, ...patch }));
sse.run.mockReset().mockImplementation(async ({ res }) => { res.writeHead(200, { 'Content-Type': 'text/event-stream' }); res.end('data: {"type":"complete"}\n\n'); });
gen.ready = true;
+ cache.cached = true;
models.list.mockResolvedValue([{ id: 'm', name: 'M', userAdded: false }]);
});
@@ -112,8 +117,17 @@ describe('music routes', () => {
models.add.mockResolvedValueOnce({ id: 'facebook/musicgen-large', repo: 'facebook/musicgen-large', name: 'musicgen-large' });
const r = await request(app).post('/api/music/models').send({ engine: 'musicgen', repo: 'facebook/musicgen-large' });
expect(r.status).toBe(200);
- expect(models.add).toHaveBeenCalledWith({ engine: 'musicgen', repo: 'facebook/musicgen-large', name: undefined });
expect(sse.run).toHaveBeenCalledWith(expect.objectContaining({ repo: 'facebook/musicgen-large' }));
+ // Registered only AFTER the download landed in the cache.
+ expect(models.add).toHaveBeenCalledWith({ engine: 'musicgen', repo: 'facebook/musicgen-large', name: undefined });
+ });
+
+ it('POST /models does NOT register the model when the download did not land', async () => {
+ cache.cached = false; // download failed/cancelled → repo not in cache
+ const r = await request(app).post('/api/music/models').send({ engine: 'musicgen', repo: 'someorg/typo-repo' });
+ expect(r.status).toBe(200); // the SSE stream still completes (with its error frames)
+ expect(sse.run).toHaveBeenCalled();
+ expect(models.add).not.toHaveBeenCalled();
});
it('POST /models rejects an unknown engine / invalid repo before downloading', async () => {