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
refactor([issue-1798]): extract useSceneRenderLifecycle to dedup the Music Video board's image/video socket effects
[._.]/ Adam Eivy51fd4250786a
diff --git a/client/src/hooks/README.md b/client/src/hooks/README.mdindex 704edbf940..1adb77d13d 100644--- a/client/src/hooks/README.md+++ b/client/src/hooks/README.md@@ -41,6 +41,7 @@ grep -i "what you want to do" client/src/hooks/README.md| `useImageGenProgress` | Live diffusion progress for an image-gen call. | Showing per-call image-gen progress. || `useImporterProgress` | Live analyze-phase stage checklist via the `importer:progress` socket (runId-filtered); exports `stageStatusIcon` for the status→icon lookup. | Importer analyze progress UI only. || `useMediaJobProgress` | Live progress for a single `mediaJobQueue` job. | Subscribing to a known media-job id. |+| `useSceneRenderLifecycle` | One Music Video board lane's async-render lifecycle: owns the per-scene spinner state (`genScenes`), job-id→scene correlation, orphan-terminal reconcile, and the durable-attach + `*-gen:completed/failed/canceled` socket subscription (with running-cancel toast disambiguation). Returns `{ genScenes, startScene, clearScene, trackJob }`. | A board lane that kicks media-job renders per scene and clears spinners from terminal events (reference frame, i2v clip). One call per lane. || `useSlotInFlight` | Whether a render slot (`{ jobId, filename }`) has an in-flight render; returns `{ inFlight, setStatus }`. A present `filename` (or 5s grace on a stale/expired job) clears `inFlight` so the render button never sticks "loading". | A render button that should disable + spin only while its own slot is actually rendering (comic pages, covers). Wire `setStatus` to the slot's `MediaJobThumb` `onStatus`. || `useMediaJobSse` | Imperative per-job `/{kind}-gen/:id/events` SSE; `attach()` returns a Promise that settles on the terminal frame. | POST-then-attach media render flows that await completion (ImageGen/VideoGen). || `useOpenClawStream` | OpenClaw SSE chat stream. | OpenClaw file-browser chat surface only. |diff --git a/client/src/hooks/index.js b/client/src/hooks/index.jsindex c15f7d4056..8f4cacc1e1 100644--- a/client/src/hooks/index.js+++ b/client/src/hooks/index.js@@ -30,6 +30,7 @@ export { default as useKeyboardShortcuts } from './useKeyboardShortcuts.js';export * from './useKeyboardShortcuts.js';export { default as useMediaJobProgress } from './useMediaJobProgress.js';export * from './useMediaJobSse.js';+export { default as useSceneRenderLifecycle } from './useSceneRenderLifecycle.js';export { default as useMoltworldWs } from './useMoltworldWs.js';export { default as useMounted } from './useMounted.js';export { default as usePendingListRows } from './usePendingListRows.js';diff --git a/client/src/hooks/useSceneRenderLifecycle.js b/client/src/hooks/useSceneRenderLifecycle.jsnew file mode 100644index 0000000000..e2deb482a5--- /dev/null+++ b/client/src/hooks/useSceneRenderLifecycle.js@@ -0,0 +1,183 @@+import { useCallback, useEffect, useRef, useState } from 'react';+import socket from '../services/socket';+import { getMediaJob } from '../services/apiMediaJobs';+import toast from '../components/ui/Toast';++/**+ * Per-scene async-render lifecycle for a Music Video board lane (reference+ * frame OR i2v clip — #1798). One call owns everything one lane needs to drive+ * its "Rendering…" spinners from media-job socket events:+ *+ * - `genScenes` — sceneId → true while that scene's render is in flight (the+ * spinner source).+ * - a pending-jobs map (jobId → sceneId) so a queued render's terminal event+ * clears the RIGHT scene's spinner.+ * - an orphan-terminals map (jobId → failed) for the terminal event that+ * raced ahead of the kickoff's `trackJob` registration (the HTTP response+ * and the WebSocket terminal event arrive on separate channels, so a+ * fast-failing queued job's event can land first).+ * - the socket subscription: a durable `attachEvent` that folds the finished+ * asset onto the matching scene without a refetch (does NOT touch the+ * spinner — an older render's attach can arrive while a newer one is still+ * in flight, so the spinner is owned solely by the job-id-correlated+ * terminal events), plus the `completedEvent` / `failedEvent` /+ * `canceledEvent` terminal events that clear the spinner.+ *+ * The image and video lanes were near-verbatim copies of this before the+ * extraction; the server-side image/video duplication was already unified in+ * #1791 behind `createMediaJobImageHook`, so this is the client-side analog.+ *+ * Config:+ * - `attachEvent` — durable broadcast that lands the finished asset+ * (e.g. `music-video:scene-image`). Handler receives the+ * raw event payload and calls `apply(data)`.+ * - `completedEvent` — job terminal success (e.g. `image-gen:completed`).+ * - `failedEvent` — job terminal failure (e.g. `image-gen:failed`).+ * - `canceledEvent` — optional job cancel (e.g. `image-gen:canceled`). A+ * queued-cancel emits only this (no `failedEvent`), so+ * without it the spinner would stick.+ * - `apply(data)` — fold the finished asset onto the matching scene+ * (functional setProjects update); called on `attachEvent`.+ * - `failMessage` — the toast string for a confirmed render failure.+ *+ * Returns `{ genScenes, startScene, clearScene, trackJob }`:+ * - `startScene(sceneId)` — light the spinner before the kickoff request.+ * - `clearScene(sceneId)` — drop the spinner (sync-lane finish, or a kickoff+ * that returns no trackable job id).+ * - `trackJob(jobId, sceneId)` — register a queued render so its terminal+ * event clears the right scene's spinner; reconciles an already-arrived+ * orphan terminal inline (clears the spinner, toasts on failure) instead of+ * registering a job that's already done.+ */+export default function useSceneRenderLifecycle({+ attachEvent,+ completedEvent,+ failedEvent,+ canceledEvent,+ apply,+ failMessage,+}) {+ const [genScenes, setGenScenes] = useState({});+ // jobId → sceneId for renders this lane is awaiting.+ const pendingRef = useRef(new Map());+ // jobId → failed(bool) for terminal events that beat their kickoff's+ // trackJob registration. Capped so unrelated jobs across the app can't grow+ // it unbounded; the kickoff reconciles its own entry on arrival.+ const orphanRef = useRef(new Map());+ // Latest `apply` / `failMessage` without re-subscribing the socket every+ // render — the effect keys on the (static) event names and reads the mutable+ // callbacks through this ref, mirroring the original `[]`-deps effects.+ const cfgRef = useRef({ apply, failMessage });+ cfgRef.current = { apply, failMessage };++ const startScene = useCallback(+ (sceneId) => setGenScenes((prev) => ({ ...prev, [sceneId]: true })),+ [],+ );+ const clearScene = useCallback(+ (sceneId) => setGenScenes((prev) => {+ const next = { ...prev };+ delete next[sceneId];+ return next;+ }),+ [],+ );++ // The orphan-reconcile helper used by the kickoff `.then`: if the terminal+ // event already raced ahead, settle it now; otherwise register the pending job.+ const trackJob = useCallback((jobId, sceneId) => {+ if (orphanRef.current.has(jobId)) {+ const failed = orphanRef.current.get(jobId);+ orphanRef.current.delete(jobId);+ clearScene(sceneId);+ if (failed) toast.error(cfgRef.current.failMessage);+ return;+ }+ pendingRef.current.set(jobId, sceneId);+ }, [clearScene]);++ useEffect(() => {+ const onAttach = (data) => cfgRef.current.apply(data);+ // jobId → pending error-toast timer (running-cancel deferral; see onFailed).+ const failTimers = new Map();+ const settle = (data, failed) => {+ const jobId = data?.generationId || data?.jobId;+ if (!jobId) return;+ const sceneId = pendingRef.current.get(jobId);+ if (!sceneId) {+ // Not yet correlated (the kickoff `trackJob` hasn't run, or it's an+ // unrelated job). Stash it so a slightly-late registration can+ // reconcile; cap so other pages' renders can't grow this unbounded.+ const orphans = orphanRef.current;+ orphans.set(jobId, !!failed);+ if (orphans.size > 64) orphans.delete(orphans.keys().next().value);+ return;+ }+ pendingRef.current.delete(jobId);+ clearScene(sceneId);+ if (failed) toast.error(cfgRef.current.failMessage);+ };+ const onCompleted = (data) => settle(data, false);+ // Deferred failure toast for an owned render. A render canceled WHILE RUNNING+ // reaches us as `failedEvent` (SIGTERM) just before `canceledEvent` — and+ // before the queue flips the job to 'canceled' — so neither the failed event+ // nor an immediate status fetch can tell a cancel from a real failure+ // (#1791/#1796). `canceledEvent` cancels this timer in the common case; if+ // the timer fires first it re-polls the job and only toasts on a CONFIRMED+ // terminal failure — a still-'running'/'queued' status means the cancel (or+ // the failure transition) hasn't landed yet, so it re-polls a bounded number+ // of times rather than toasting prematurely (the spinner is already cleared,+ // so giving up silently never strands the UI).+ const armFailToast = (jobId, attempt = 0) => {+ failTimers.set(jobId, setTimeout(() => {+ failTimers.delete(jobId);+ getMediaJob(jobId)+ .then((job) => {+ const status = job?.status;+ if (status === 'canceled') return; // user cancel — never a failure toast+ if (status === 'failed' || status === 'error') { toast.error(cfgRef.current.failMessage); return; }+ if (attempt < 2) armFailToast(jobId, attempt + 1); // non-terminal: wait, don't toast yet+ })+ .catch(() => toast.error(cfgRef.current.failMessage));+ }, 800));+ };+ const onFailed = (data) => {+ const jobId = data?.generationId || data?.jobId;+ if (!jobId) return;+ // Only THIS lane's renders surface a failure toast. An OWNED job clears the+ // spinner silently (settle with failed=false) and defers the toast above so+ // a running-cancel can retract it. A not-yet-owned job is stashed as an+ // orphan WITH the failure bit so a fast-fail that raced ahead of its own+ // kickoff registration is toasted by the kickoff reconciliation; an+ // unrelated job is simply capped/evicted from the orphan map unseen.+ const owned = pendingRef.current.has(jobId);+ settle(data, !owned);+ if (owned && !failTimers.has(jobId)) armFailToast(jobId);+ };+ // Queued-cancel emits no `failedEvent`; running-cancel emits failed then this.+ // Either way clear the spinner and cancel any pending failure toast.+ const onCanceled = (data) => {+ const jobId = data?.generationId || data?.jobId;+ if (jobId) {+ const t = failTimers.get(jobId);+ if (t) { clearTimeout(t); failTimers.delete(jobId); }+ }+ settle(data, false);+ };++ socket.on(attachEvent, onAttach);+ socket.on(completedEvent, onCompleted);+ socket.on(failedEvent, onFailed);+ if (canceledEvent) socket.on(canceledEvent, onCanceled);+ return () => {+ socket.off(attachEvent, onAttach);+ socket.off(completedEvent, onCompleted);+ socket.off(failedEvent, onFailed);+ if (canceledEvent) socket.off(canceledEvent, onCanceled);+ for (const t of failTimers.values()) clearTimeout(t);+ failTimers.clear();+ };+ }, [attachEvent, completedEvent, failedEvent, canceledEvent, clearScene]);++ return { genScenes, startScene, clearScene, trackJob };+}diff --git a/client/src/hooks/useSceneRenderLifecycle.test.jsx b/client/src/hooks/useSceneRenderLifecycle.test.jsxnew file mode 100644index 0000000000..0dcb8c68f7--- /dev/null+++ b/client/src/hooks/useSceneRenderLifecycle.test.jsx@@ -0,0 +1,160 @@+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';+import { renderHook, act, cleanup } from '@testing-library/react';++// Drive the socket handlers the hook registers with `on()`.+const handlers = new Map();+vi.mock('../services/socket', () => ({+ default: {+ on: (event, fn) => { handlers.set(event, fn); },+ off: (event, fn) => { if (handlers.get(event) === fn) handlers.delete(event); },+ emit: () => {},+ },+}));++const getMediaJob = vi.fn(async () => ({ status: 'failed' }));+vi.mock('../services/apiMediaJobs', () => ({ getMediaJob: (...a) => getMediaJob(...a) }));++const toastError = vi.fn();+vi.mock('../components/ui/Toast', () => ({ default: { error: (...a) => toastError(...a), success: () => {} } }));++const useSceneRenderLifecycle = (await import('./useSceneRenderLifecycle.js')).default;++const fire = (event, payload) => act(() => { handlers.get(event)?.(payload); });++const IMAGE_CFG = {+ attachEvent: 'music-video:scene-image',+ completedEvent: 'image-gen:completed',+ failedEvent: 'image-gen:failed',+ canceledEvent: 'image-gen:canceled',+ failMessage: 'Frame render failed',+};++const renderLane = (overrides = {}) => {+ const apply = vi.fn();+ const view = renderHook(() => useSceneRenderLifecycle({ ...IMAGE_CFG, apply, ...overrides }));+ return { ...view, apply };+};++describe('useSceneRenderLifecycle', () => {+ beforeEach(() => { handlers.clear(); getMediaJob.mockReset(); toastError.mockReset(); });+ afterEach(cleanup);++ it('subscribes to the configured lane events and unsubscribes on unmount', () => {+ const { unmount } = renderLane();+ expect(handlers.has('music-video:scene-image')).toBe(true);+ expect(handlers.has('image-gen:completed')).toBe(true);+ expect(handlers.has('image-gen:failed')).toBe(true);+ expect(handlers.has('image-gen:canceled')).toBe(true);+ unmount();+ expect(handlers.size).toBe(0);+ });++ it('does not subscribe to a canceled event when canceledEvent is omitted (e.g. a no-cancel lane)', () => {+ renderLane({ canceledEvent: undefined });+ expect(handlers.has('image-gen:canceled')).toBe(false);+ expect(handlers.has('image-gen:completed')).toBe(true);+ });++ it('clears the right scene spinner when a tracked job completes', () => {+ const { result } = renderLane();+ act(() => { result.current.startScene('s1'); result.current.trackJob('job-1', 's1'); });+ expect(result.current.genScenes.s1).toBe(true);+ fire('image-gen:completed', { generationId: 'job-1' });+ expect(result.current.genScenes.s1).toBeUndefined();+ expect(toastError).not.toHaveBeenCalled();+ });++ it('attach event folds the asset via apply without touching the spinner', () => {+ const { result, apply } = renderLane();+ act(() => { result.current.startScene('s1'); result.current.trackJob('job-1', 's1'); });+ fire('music-video:scene-image', { projectId: 'p1', sceneId: 's1', referenceImageId: 'img.png' });+ expect(apply).toHaveBeenCalledWith({ projectId: 'p1', sceneId: 's1', referenceImageId: 'img.png' });+ // attach does NOT clear the spinner — only the terminal event does.+ expect(result.current.genScenes.s1).toBe(true);+ });++ it('ignores a terminal event for a job this lane never tracked (orphan stash, no spinner change)', () => {+ const { result } = renderLane();+ act(() => result.current.startScene('s1'));+ fire('image-gen:completed', { generationId: 'unrelated' });+ expect(result.current.genScenes.s1).toBe(true);+ });++ it('trackJob reconciles a completed orphan that raced ahead of the kickoff', () => {+ const { result } = renderLane();+ act(() => result.current.startScene('s1'));+ // Terminal event lands BEFORE trackJob registers the job id.+ fire('image-gen:completed', { generationId: 'job-1' });+ expect(result.current.genScenes.s1).toBe(true); // still spinning — not yet correlated+ act(() => result.current.trackJob('job-1', 's1'));+ expect(result.current.genScenes.s1).toBeUndefined(); // reconciled → spinner cleared+ expect(toastError).not.toHaveBeenCalled();+ });++ it('trackJob reconciles a FAILED orphan and toasts the failure', () => {+ const { result } = renderLane();+ act(() => result.current.startScene('s1'));+ fire('image-gen:failed', { generationId: 'job-1' });+ act(() => result.current.trackJob('job-1', 's1'));+ expect(result.current.genScenes.s1).toBeUndefined();+ expect(toastError).toHaveBeenCalledWith('Frame render failed');+ });++ it('queued-cancel (canceled with no prior failed) clears the spinner silently', () => {+ const { result } = renderLane();+ act(() => { result.current.startScene('s1'); result.current.trackJob('job-1', 's1'); });+ fire('image-gen:canceled', { generationId: 'job-1' });+ expect(result.current.genScenes.s1).toBeUndefined();+ expect(toastError).not.toHaveBeenCalled();+ });++ it('an owned failure clears the spinner immediately and toasts only after the re-poll confirms it', async () => {+ vi.useFakeTimers();+ getMediaJob.mockResolvedValue({ status: 'failed' });+ const { result } = renderLane();+ act(() => { result.current.startScene('s1'); result.current.trackJob('job-1', 's1'); });+ fire('image-gen:failed', { generationId: 'job-1' });+ // Spinner clears at once; toast is deferred pending the re-poll.+ expect(result.current.genScenes.s1).toBeUndefined();+ expect(toastError).not.toHaveBeenCalled();+ await act(async () => { await vi.runAllTimersAsync(); });+ expect(getMediaJob).toHaveBeenCalledWith('job-1');+ expect(toastError).toHaveBeenCalledWith('Frame render failed');+ vi.useRealTimers();+ });++ it('a running-cancel (failed then canceled) cancels the deferred toast — no failure toast', async () => {+ vi.useFakeTimers();+ getMediaJob.mockResolvedValue({ status: 'canceled' });+ const { result } = renderLane();+ act(() => { result.current.startScene('s1'); result.current.trackJob('job-1', 's1'); });+ fire('image-gen:failed', { generationId: 'job-1' });+ // The canceled event arrives before the 800ms timer fires and cancels it.+ fire('image-gen:canceled', { generationId: 'job-1' });+ await act(async () => { await vi.runAllTimersAsync(); });+ expect(toastError).not.toHaveBeenCalled();+ vi.useRealTimers();+ });++ it('a deferred toast whose re-poll resolves to canceled does not fire (running-cancel disambiguation)', async () => {+ vi.useFakeTimers();+ getMediaJob.mockResolvedValue({ status: 'canceled' });+ const { result } = renderLane();+ act(() => { result.current.startScene('s1'); result.current.trackJob('job-1', 's1'); });+ fire('image-gen:failed', { generationId: 'job-1' });+ // Timer fires before the canceled event; the re-poll sees 'canceled' → stays silent.+ await act(async () => { await vi.runAllTimersAsync(); });+ expect(getMediaJob).toHaveBeenCalledWith('job-1');+ expect(toastError).not.toHaveBeenCalled();+ vi.useRealTimers();+ });++ it('reacts only to its configured events — a different lane\'s terminal event is ignored', () => {+ const { result } = renderLane();+ act(() => { result.current.startScene('s1'); result.current.trackJob('job-1', 's1'); });+ // The video lane's terminal event has no handler on this (image) lane, so the+ // image spinner is untouched even on a same job-id collision.+ fire('video-gen:completed', { generationId: 'job-1' });+ expect(result.current.genScenes.s1).toBe(true);+ });+});diff --git a/client/src/pages/MusicVideo.jsx b/client/src/pages/MusicVideo.jsxindex bc08d74068..0de5d61488 100644--- a/client/src/pages/MusicVideo.jsx+++ b/client/src/pages/MusicVideo.jsx@@ -1,4 +1,4 @@-import { useEffect, useRef, useState, useCallback } from 'react';+import { useEffect, useState, useCallback } from 'react';import { Plus, Film, Trash2, Music, Activity, ArrowUp, ArrowDown, Image as ImageIcon, Video } from 'lucide-react';import toast from '../components/ui/Toast';import PageHeader from '../components/PageHeader';@@ -15,8 +15,7 @@ import {import { generateImage } from '../services/apiSystem.js';import { generateVideo } from '../services/apiImageVideo.js';import { listTracks } from '../services/apiTracks.js';-import { getMediaJob } from '../services/apiMediaJobs.js';-import socket from '../services/socket.js';+import useSceneRenderLifecycle from '../hooks/useSceneRenderLifecycle.js';import { formatDurationSec } from '../utils/formatters.js';const MODES = ['director', 'autonomous'];@@ -40,34 +39,8 @@ export default function MusicVideo() {const [loading, setLoading] = useState(true);const [analyzing, setAnalyzing] = useState(false);const [form, setForm] = useState({ name: '', mode: 'director', trackId: '' });- // Per-scene reference-frame generation: sceneId → true while a render is in flight.- const [genScenes, setGenScenes] = useState({});- // Async-render correlation: in-flight media-job id → sceneId, so a queued- // render's terminal image-gen:completed / image-gen:failed event clears the- // right scene's spinner. The success image lands separately via the durable- // music-video:scene-image event, but the SPINNER is cleared on job lifecycle- // so a render whose attach hook 404s (scene deleted mid-render) or that fails- // outright doesn't leave the button stuck on "Rendering…" forever.- const pendingJobsRef = useRef(new Map());- // Terminal events that arrived BEFORE the kickoff .then registered the job id.- // The HTTP response and the WebSocket terminal event race on separate channels,- // so a fast-failing queued job's event can land first; without this, that event- // finds no pending entry, gets dropped, and the spinner sticks forever. Capped- // (jobId → failed) so the terminal events of unrelated image-gen jobs across the- // app can't grow it unbounded; the kickoff reconciles its own entry on arrival.- const orphanTerminalsRef = useRef(new Map());- // Per-scene i2v video generation (#1760 Phase 1): sceneId → true while a scene- // clip is rendering. Video renders always ride the media-job queue (no- // synchronous lane), so the spinner is cleared by the job-id-correlated- // video-gen:completed/failed events and the clip lands durably via- // music-video:scene-video — the direct analog of the reference-frame refs above.- const [genVideoScenes, setGenVideoScenes] = useState({});- const pendingVideoJobsRef = useRef(new Map());- const orphanVideoTerminalsRef = useRef(new Map());-const selected = projects.find((p) => p.id === selectedId) || null;const replaceProject = (next) => setProjects((prev) => prev.map((p) => (p.id === next.id ? next : p)));- const clearGen = (sceneId) => setGenScenes((prev) => { const next = { ...prev }; delete next[sceneId]; return next; });// Merge ONLY a scene's referenceImageId via a functional update so a render// that resolves after the user edited the board can't clobber those edits with// a stale project snapshot. Shared by the socket handler and the synchronous@@ -76,7 +49,6 @@ export default function MusicVideo() {setProjects((prev) => prev.map((p) => (p.id === projectId? { ...p, scenes: (p.scenes || []).map((s) => (s.sceneId === sceneId ? { ...s, referenceImageId } : s)) }: p)));- const clearGenVideo = (sceneId) => setGenVideoScenes((prev) => { const next = { ...prev }; delete next[sceneId]; return next; });// Merge ONLY a scene's videoHistoryId via a functional update (same stale-// snapshot guard as applyReferenceImage above).const applySceneVideo = (projectId, sceneId, videoHistoryId) =>@@ -84,175 +56,31 @@ export default function MusicVideo() {? { ...p, scenes: (p.scenes || []).map((s) => (s.sceneId === sceneId ? { ...s, videoHistoryId } : s)) }: p)));- // Socket lifecycle for async (local/Codex) reference-frame renders (#1760- // Phase 1b). Three events, all broadcast to every client:- // - music-video:scene-image — durable attach filed by musicVideoSceneImageHook;- // fold the new referenceImageId onto the matching scene without a refetch,- // even for a project that isn't selected (a render that finished after- // navigating away still lands). It does NOT touch the spinner: an older- // render's scene-image can arrive while a newer one is still in flight, so- // the spinner is owned solely by the job-id-correlated terminal events below.- // - image-gen:completed / image-gen:failed — the job's terminal events;- // clear the spinner by correlating generationId → sceneId. Failure toasts.- useEffect(() => {- const onSceneImage = ({ projectId, sceneId, referenceImageId }) => {- applyReferenceImage(projectId, sceneId, referenceImageId);- };- // jobId → pending error-toast timer (running-cancel deferral; see onFailed).- const failTimers = new Map();- const settle = (data, failed) => {- const jobId = data?.generationId || data?.jobId;- if (!jobId) return;- const sceneId = pendingJobsRef.current.get(jobId);- if (!sceneId) {- // Not yet correlated (the kickoff .then hasn't registered it, or it's an- // unrelated image-gen job). Stash it so a slightly-late registration can- // reconcile; cap so other pages' renders can't grow this unbounded.- const orphans = orphanTerminalsRef.current;- orphans.set(jobId, !!failed);- if (orphans.size > 64) orphans.delete(orphans.keys().next().value);- return;- }- pendingJobsRef.current.delete(jobId);- clearGen(sceneId);- if (failed) toast.error('Frame render failed');- };- const onCompleted = (data) => settle(data, false);- // Deferred failure toast for an owned render. A render canceled WHILE RUNNING- // reaches us as image-gen:failed (SIGTERM) just before image-gen:canceled —- // and before the queue flips the job to 'canceled' — so neither the failed- // event nor an immediate status fetch can tell a cancel from a real failure- // (#1791/#1796). image-gen:canceled cancels this timer in the common case;- // if the timer fires first it re-polls the job and only toasts on a CONFIRMED- // terminal failure — a still-'running'/'queued' status means the cancel (or- // the failure transition) hasn't landed yet, so it re-polls a bounded number- // of times rather than toasting prematurely (the spinner is already cleared,- // so giving up silently never strands the UI).- const armFailToast = (jobId, attempt = 0) => {- failTimers.set(jobId, setTimeout(() => {- failTimers.delete(jobId);- getMediaJob(jobId)- .then((job) => {- const status = job?.status;- if (status === 'canceled') return; // user cancel — never a failure toast- if (status === 'failed' || status === 'error') { toast.error('Frame render failed'); return; }- if (attempt < 2) armFailToast(jobId, attempt + 1); // non-terminal: wait, don't toast yet- })- .catch(() => toast.error('Frame render failed'));- }, 800));- };- const onFailed = (data) => {- const jobId = data?.generationId || data?.jobId;- if (!jobId) return;- // Only THIS page's renders surface a failure toast. An OWNED job clears the- // spinner silently (settle with failed=false) and defers the toast above so- // a running-cancel can retract it. A not-yet-owned job is stashed as an- // orphan WITH the failure bit so a fast-fail that raced ahead of its own- // kickoff registration is toasted by the kickoff .then reconciliation; an- // unrelated image-gen job is simply capped/evicted from the orphan map- // unseen (never reconciled → never toasts here).- const owned = pendingJobsRef.current.has(jobId);- settle(data, !owned);- if (owned && !failTimers.has(jobId)) armFailToast(jobId);- };- // Queued-cancel emits no *:failed; running-cancel emits failed then this.- // Either way clear the spinner and cancel any pending failure toast.- const onCanceled = (data) => {- const jobId = data?.generationId || data?.jobId;- if (jobId) {- const t = failTimers.get(jobId);- if (t) { clearTimeout(t); failTimers.delete(jobId); }- }- settle(data, false);- };- socket.on('music-video:scene-image', onSceneImage);- socket.on('image-gen:completed', onCompleted);- socket.on('image-gen:failed', onFailed);- socket.on('image-gen:canceled', onCanceled);- return () => {- socket.off('music-video:scene-image', onSceneImage);- socket.off('image-gen:completed', onCompleted);- socket.off('image-gen:failed', onFailed);- socket.off('image-gen:canceled', onCanceled);- for (const t of failTimers.values()) clearTimeout(t);- failTimers.clear();- };- }, []);-- // Socket lifecycle for async i2v scene-clip renders (#1760 Phase 1). Mirrors- // the reference-frame effect above, for video:- // - music-video:scene-video — durable attach filed by musicVideoSceneVideoHook;- // fold videoHistoryId onto the matching scene without a refetch, even for a- // project that isn't selected. Does NOT touch the spinner.- // - video-gen:completed / video-gen:failed / video-gen:canceled — the job's- // terminal events; clear the spinner by correlating generationId → sceneId.- // A queued-cancel emits only :canceled (no :failed), so without that- // handler the spinner would stick; a running-cancel emits :failed then- // :canceled, so the deferred toast re-polls the job and stays silent when- // it resolved to 'canceled' (same disambiguation the image lane uses).- useEffect(() => {- const onSceneVideo = ({ projectId, sceneId, videoHistoryId }) => {- applySceneVideo(projectId, sceneId, videoHistoryId);- };- const failTimers = new Map();- const settle = (data, failed) => {- const jobId = data?.generationId || data?.jobId;- if (!jobId) return;- const sceneId = pendingVideoJobsRef.current.get(jobId);- if (!sceneId) {- const orphans = orphanVideoTerminalsRef.current;- orphans.set(jobId, !!failed);- if (orphans.size > 64) orphans.delete(orphans.keys().next().value);- return;- }- pendingVideoJobsRef.current.delete(jobId);- clearGenVideo(sceneId);- if (failed) toast.error('Scene video render failed');- };- const onCompleted = (data) => settle(data, false);- const armFailToast = (jobId, attempt = 0) => {- failTimers.set(jobId, setTimeout(() => {- failTimers.delete(jobId);- getMediaJob(jobId)- .then((job) => {- const status = job?.status;- if (status === 'canceled') return; // user cancel — never a failure toast- if (status === 'failed' || status === 'error') { toast.error('Scene video render failed'); return; }- if (attempt < 2) armFailToast(jobId, attempt + 1); // non-terminal: wait, don't toast yet- })- .catch(() => toast.error('Scene video render failed'));- }, 800));- };- const onFailed = (data) => {- const jobId = data?.generationId || data?.jobId;- if (!jobId) return;- const owned = pendingVideoJobsRef.current.has(jobId);- settle(data, !owned);- if (owned && !failTimers.has(jobId)) armFailToast(jobId);- };- // Queued-cancel emits no *:failed; running-cancel emits failed then this.- // Either way clear the spinner and cancel any pending failure toast.- const onCanceled = (data) => {- const jobId = data?.generationId || data?.jobId;- if (jobId) {- const t = failTimers.get(jobId);- if (t) { clearTimeout(t); failTimers.delete(jobId); }- }- settle(data, false);- };- socket.on('music-video:scene-video', onSceneVideo);- socket.on('video-gen:completed', onCompleted);- socket.on('video-gen:failed', onFailed);- socket.on('video-gen:canceled', onCanceled);- return () => {- socket.off('music-video:scene-video', onSceneVideo);- socket.off('video-gen:completed', onCompleted);- socket.off('video-gen:failed', onFailed);- socket.off('video-gen:canceled', onCanceled);- for (const t of failTimers.values()) clearTimeout(t);- failTimers.clear();- };- }, []);+ // Per-scene async-render lifecycle for each lane (#1798). One hook call owns a+ // lane's spinner state, job-id correlation, orphan-terminal reconcile, and+ // socket subscription — the client-side analog of the server's #1791+ // image/video hook unification. The reference-frame lane attaches the finished+ // still durably via music-video:scene-image; the i2v lane attaches the clip via+ // music-video:scene-video. Both ride the media-job queue, so the spinner is+ // cleared by the job-id-correlated *-gen:completed/failed/canceled events.+ const frameLane = useSceneRenderLifecycle({+ attachEvent: 'music-video:scene-image',+ completedEvent: 'image-gen:completed',+ failedEvent: 'image-gen:failed',+ canceledEvent: 'image-gen:canceled',+ apply: ({ projectId, sceneId, referenceImageId }) => applyReferenceImage(projectId, sceneId, referenceImageId),+ failMessage: 'Frame render failed',+ });+ const videoLane = useSceneRenderLifecycle({+ attachEvent: 'music-video:scene-video',+ completedEvent: 'video-gen:completed',+ failedEvent: 'video-gen:failed',+ canceledEvent: 'video-gen:canceled',+ apply: ({ projectId, sceneId, videoHistoryId }) => applySceneVideo(projectId, sceneId, videoHistoryId),+ failMessage: 'Scene video render failed',+ });+ const genScenes = frameLane.genScenes;+ const genVideoScenes = videoLane.genScenes;useEffect(() => {listMusicVideoProjects()@@ -343,25 +171,17 @@ export default function MusicVideo() {const handleGenerateFrame = (scene) => {const prompt = buildFramePrompt(scene);if (!prompt) { toast.error('Add a frame prompt or shot prompt first'); return; }- setGenScenes((prev) => ({ ...prev, [scene.sceneId]: true }));+ frameLane.startScene(scene.sceneId);generateImage({ prompt, musicVideo: { projectId: selected.id, sceneId: scene.sceneId } }, { silent: true }).then((res) => {const stillRunning = res?.status === 'queued' || res?.status === 'running';if (stillRunning) {// async lane: correlate the job so its terminal event clears the spinner- // (and the durable scene-image event lands the generated frame).+ // (and the durable scene-image event lands the generated frame). trackJob+ // reconciles a terminal event that raced ahead of this .then (fast fail).const jobId = res?.jobId || res?.generationId;- if (!jobId) { clearGen(scene.sceneId); return; } // no id to track → don't strand the button- // The terminal event may have raced ahead of this .then (fast fail) —- // if so, reconcile it now instead of registering a job that's already done.- if (orphanTerminalsRef.current.has(jobId)) {- const failed = orphanTerminalsRef.current.get(jobId);- orphanTerminalsRef.current.delete(jobId);- clearGen(scene.sceneId);- if (failed) toast.error('Frame render failed');- return;- }- pendingJobsRef.current.set(jobId, scene.sceneId);+ if (!jobId) { frameLane.clearScene(scene.sceneId); return; } // no id to track → don't strand the button+ frameLane.trackJob(jobId, scene.sceneId);return;}const filename = res?.filename;@@ -370,11 +190,11 @@ export default function MusicVideo() {updateMusicVideoScene(selected.id, scene.sceneId, { referenceImageId: filename }, { silent: true }).catch((err) => toast.error(err?.message || 'Failed to attach frame'));}- clearGen(scene.sceneId);+ frameLane.clearScene(scene.sceneId);}).catch((err) => {toast.error(err?.message || 'Frame generation failed');- clearGen(scene.sceneId);+ frameLane.clearScene(scene.sceneId);});};@@ -397,7 +217,7 @@ export default function MusicVideo() {if (!scene.referenceImageId) { toast.error('Generate a reference frame first'); return; }const prompt = buildShotPrompt(scene);if (!prompt) { toast.error('Add a shot prompt first'); return; }- setGenVideoScenes((prev) => ({ ...prev, [scene.sceneId]: true }));+ videoLane.startScene(scene.sceneId);generateVideo({prompt,mode: 'image',@@ -406,20 +226,13 @@ export default function MusicVideo() {}).then((res) => {const jobId = res?.jobId || res?.generationId;- if (!jobId) { clearGenVideo(scene.sceneId); return; } // no id to track → don't strand the button- // The terminal event may have raced ahead of this .then — reconcile it now.- if (orphanVideoTerminalsRef.current.has(jobId)) {- const failed = orphanVideoTerminalsRef.current.get(jobId);- orphanVideoTerminalsRef.current.delete(jobId);- clearGenVideo(scene.sceneId);- if (failed) toast.error('Scene video render failed');- return;- }- pendingVideoJobsRef.current.set(jobId, scene.sceneId);+ if (!jobId) { videoLane.clearScene(scene.sceneId); return; } // no id to track → don't strand the button+ // trackJob reconciles a terminal event that raced ahead of this .then.+ videoLane.trackJob(jobId, scene.sceneId);}).catch((err) => {toast.error(err?.message || 'Scene video generation failed');- clearGenVideo(scene.sceneId);+ videoLane.clearScene(scene.sceneId);});};