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: recover generated previews after reconnect and idle expiry
Adam Eivy381009bc0a21
diff --git a/client/src/components/codeAnimation/CodeAnimationPreview.jsx b/client/src/components/codeAnimation/CodeAnimationPreview.jsxindex 8c0ffe27ee..6e5aa14431 100644--- a/client/src/components/codeAnimation/CodeAnimationPreview.jsx+++ b/client/src/components/codeAnimation/CodeAnimationPreview.jsx@@ -88,6 +88,7 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGlconst [saving, setSaving] = useState(false);const [packageDownloading, setPackageDownloading] = useState(false);const recordTimerRef = useRef(null);+ const recordingSessionRef = useRef(null);// Frame-exact export runs server-side on the media queue; its progress and// result stream from the composition job's SSE channel.const [exportJob, setExportJob] = useState(null);@@ -169,6 +170,13 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGluseEffect(() => () => clearTimeout(recordTimerRef.current), []);+ useEffect(() => {+ if (!recording || (preview.ready && preview.sessionId === recordingSessionRef.current)) return;+ clearTimeout(recordTimerRef.current);+ setRecording(false);+ toast.error('The preview stopped before recording finished. Start a new recording to try again.');+ }, [recording, preview.ready, preview.sessionId]);+useEffect(() => {const event = preview.lastEvent;if (!event || !messages) return;@@ -193,6 +201,7 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGlconst handleRecord = () => {if (!preview.ready || recording || !messages?.record) return;+ recordingSessionRef.current = preview.sessionId;setRecording(true);setRecordProgress(0);preview.command('record', { message: { type: messages.record } });@@ -302,6 +311,7 @@ export default function CodeAnimationPreview({ html, audioUrl, messages, audioGl</div><div className="flex flex-wrap items-center gap-2">+ {request && preview.status === 'idle' && <button type="button" className={BUTTON_SECONDARY} onClick={preview.resume}>Resume preview</button>}<buttontype="button"onClick={handleRecord}diff --git a/client/src/components/codeAnimation/CodeAnimationPreview.test.jsx b/client/src/components/codeAnimation/CodeAnimationPreview.test.jsxindex 2f496e1275..a62ebce58a 100644--- a/client/src/components/codeAnimation/CodeAnimationPreview.test.jsx+++ b/client/src/components/codeAnimation/CodeAnimationPreview.test.jsx@@ -5,20 +5,24 @@ import { MemoryRouter } from 'react-router';const socket = vi.hoisted(() => {const handlers = new Map();- const emit = vi.fn((event, payload) => {+ const sessions = new Map();+ const reply = (event, payload) => {if (event !== 'generated-preview:open') return;+ const sessionId = `11111111-1111-4111-8111-${String(sessions.size + 1).padStart(12, '0')}`;+ sessions.set(payload.requestId, sessionId);const opened = {requestId: payload.requestId,- sessionId: '11111111-1111-4111-8111-111111111111',+ sessionId,durationSec: 5, fps: 30, width: 1920, height: 1080,audio: payload.withAudio ? 'ready' : 'none',revision: payload.revision,};for (const fn of handlers.get('generated-preview:opened') || []) fn(opened);for (const fn of handlers.get('generated-preview:frame') || []) fn({ ...opened, generation: 0, mime: 'image/jpeg', data: btoa('frame') });- });+ };return {- emit,+ emit: vi.fn(reply),+ reply,on: vi.fn((event, cb) => {if (!handlers.has(event)) handlers.set(event, new Set());handlers.get(event).add(cb);@@ -26,6 +30,7 @@ const socket = vi.hoisted(() => {off: vi.fn((event, cb) => { handlers.get(event)?.delete(cb); }),connected: true,__handlers: handlers,+ __sessions: sessions,};});vi.mock('../../services/socket', () => ({ default: socket }));@@ -77,12 +82,81 @@ describe('CodeAnimationPreview', () => {beforeEach(() => {globalThis.URL.createObjectURL = vi.fn(() => 'blob:recorded');globalThis.URL.revokeObjectURL = vi.fn();- socket.emit.mockClear();+ socket.emit.mockReset();+ socket.emit.mockImplementation(socket.reply);socket.__handlers.clear();+ socket.__sessions.clear();+ socket.connected = true;});afterEach(() => vi.unstubAllGlobals());+ it('displays an early screencast frame without enabling recording before the session opens', async () => {+ const user = userEvent.setup();+ socket.emit.mockImplementation(() => {});+ renderPreview();+ const open = previewOpens()[0][1];+ act(() => {+ for (const fn of socket.__handlers.get('generated-preview:frame') || []) {+ fn({ requestId: open.requestId, sessionId: '11111111-1111-4111-8111-000000000001', generation: 0, mime: 'image/jpeg', data: btoa('early frame') });+ }+ });+ expect(screen.getByTitle('Code animation preview')).toBeInTheDocument();+ const record = screen.getByRole('button', { name: /record \(real-time\)/i });+ expect(record).toBeDisabled();+ await user.click(record);+ expect(socket.emit.mock.calls.filter(([event]) => event === 'generated-preview:command')).toHaveLength(0);+ act(() => socket.reply('generated-preview:open', open));+ expect(record).toBeEnabled();+ await user.click(record);+ expect(socket.emit).toHaveBeenLastCalledWith('generated-preview:command', expect.objectContaining({ requestId: open.requestId, sessionId: socket.__sessions.get(open.requestId), action: 'record' }));+ });++ it('stops an interrupted recording and reconnects without replaying its command', async () => {+ const user = userEvent.setup();+ renderPreview();+ const record = await screen.findByRole('button', { name: /record \(real-time\)/i });+ await waitFor(() => expect(record).toBeEnabled());+ const oldOpen = previewOpens()[0][1];+ await user.click(record);+ expect(screen.getByRole('button', { name: /Recording 0s/ })).toBeDisabled();+ act(() => {+ socket.connected = false;+ for (const fn of socket.__handlers.get('disconnect') || []) fn();+ });+ expect(record).toBeDisabled();+ const sent = socket.emit.mock.calls.length;+ await user.click(record);+ expect(socket.emit.mock.calls).toHaveLength(sent);+ act(() => {+ socket.connected = true;+ for (const fn of socket.__handlers.get('connect') || []) fn();+ });+ await waitFor(() => expect(record).toBeEnabled());+ expect(previewOpens()).toHaveLength(2);+ const nextOpen = previewOpens()[1][1];+ expect(nextOpen.requestId).not.toBe(oldOpen.requestId);+ expect(socket.emit.mock.calls.filter(([event]) => event === 'generated-preview:command')).toHaveLength(1);+ act(() => {+ for (const fn of socket.__handlers.get('generated-preview:recording') || []) {+ fn({ requestId: oldOpen.requestId, sessionId: socket.__sessions.get(oldOpen.requestId), index: 0, count: 1, mime: 'video/webm', chunk: btoa('x') });+ }+ });+ expect(screen.queryByLabelText('Recorded animation')).not.toBeInTheDocument();+ await user.click(record);+ expect(socket.emit).toHaveBeenLastCalledWith('generated-preview:command', expect.objectContaining({ requestId: nextOpen.requestId, action: 'record' }));+ act(() => {+ socket.connected = false;+ for (const fn of socket.__handlers.get('disconnect') || []) fn();+ socket.connected = true;+ for (const fn of socket.__handlers.get('connect') || []) fn();+ });+ expect(record).toBeEnabled();+ expect(previewOpens()).toHaveLength(3);+ expect(new Set(socket.__sessions.values()).size).toBe(3);+ expect(socket.emit.mock.calls.filter(([event]) => event === 'generated-preview:command')).toHaveLength(2);+ });+it.each([['portrait', 1080, 1920, 608, 1080],['oversized landscape', 3840, 2160, 1920, 1080],@@ -141,7 +215,7 @@ describe('CodeAnimationPreview', () => {const open = previewOpens().at(-1)[1];await act(async () => {for (const fn of socket.__handlers.get('generated-preview:event') || []) {- fn({ requestId: open.requestId, sessionId: '11111111-1111-4111-8111-111111111111', type: MESSAGES.ready, meta: { duration: 5 } });+ fn({ requestId: open.requestId, sessionId: socket.__sessions.get(open.requestId), type: MESSAGES.ready, meta: { duration: 5 } });}});expect(screen.getByText('Animation ready')).toBeInTheDocument();@@ -160,7 +234,7 @@ describe('CodeAnimationPreview', () => {await act(async () => {for (const fn of socket.__handlers.get('generated-preview:recording') || []) {- fn({ requestId: open.requestId, sessionId: '11111111-1111-4111-8111-111111111111', index: 0, count: 1, mime: 'video/webm', chunk: btoa('x') });+ fn({ requestId: open.requestId, sessionId: socket.__sessions.get(open.requestId), index: 0, count: 1, mime: 'video/webm', chunk: btoa('x') });}});expect(screen.getByLabelText('Recorded animation')).toHaveAttribute('src', 'blob:recorded');diff --git a/client/src/components/musicVideo/CodeVideoPanel.jsx b/client/src/components/musicVideo/CodeVideoPanel.jsxindex 8061473428..bf7bb7f8c2 100644--- a/client/src/components/musicVideo/CodeVideoPanel.jsx+++ b/client/src/components/musicVideo/CodeVideoPanel.jsx@@ -230,6 +230,7 @@ export default function CodeVideoPanel({ project, audioUrl, onProject, productio)}</div><div className="flex flex-wrap items-center gap-2">+ {doc && preview.status === 'idle' && <button type="button" className={buttonCls} onClick={preview.resume}>Resume preview</button>}<button type="button" className={buttonCls} onClick={togglePlay} disabled={!doc}>{playing ? 'Pause' : 'Play'}</button><button type="button" className={buttonCls} aria-pressed={looping} onClick={() => setLooping((value) => !value)}>{looping ? 'Looping section' : 'Loop section'}diff --git a/client/src/components/musicVideo/CompositionPreviewPlayer.jsx b/client/src/components/musicVideo/CompositionPreviewPlayer.jsxindex b5d7771bce..49637e9aa7 100644--- a/client/src/components/musicVideo/CompositionPreviewPlayer.jsx+++ b/client/src/components/musicVideo/CompositionPreviewPlayer.jsx@@ -124,7 +124,7 @@ export default function CompositionPreviewPlayer({ project, audioUrl, seekReques? 'Preview unavailable': preview.ready? `${t.toFixed(2)}s / ${duration.toFixed(1)}s`- : !wanted ? 'Expand to load preview' : 'Loading preview…';+ : !wanted ? 'Expand to load preview' : preview.status === 'idle' ? 'Preview paused' : 'Loading preview…';return (<div className="space-y-2" aria-label={lyrics ? 'Lyric timing playthrough' : 'Composition preview'}>{failed && <p className="text-xs text-port-error" role="alert">{preview.reason || 'Preview unavailable'}</p>}@@ -137,6 +137,7 @@ export default function CompositionPreviewPlayer({ project, audioUrl, seekReques)}</div><div className="flex flex-wrap items-center gap-2">+ {request && preview.status === 'idle' && <button type="button" className={buttonCls} onClick={preview.resume}>Resume preview</button>}<button type="button" className={buttonCls} onClick={togglePlay} disabled={!preview.ready || !audioUrl || (failed && !playing)}>{playing ? <Pause size={14} /> : <Play size={14} />} {playing ? 'Pause' : 'Play'}</button>diff --git a/client/src/components/musicVideo/CompositionPreviewPlayer.test.jsx b/client/src/components/musicVideo/CompositionPreviewPlayer.test.jsxindex 25b2eff73a..d1c6186ee2 100644--- a/client/src/components/musicVideo/CompositionPreviewPlayer.test.jsx+++ b/client/src/components/musicVideo/CompositionPreviewPlayer.test.jsx@@ -59,9 +59,120 @@ beforeEach(() => {socket.emit.mockReset();socket.emit.mockImplementation(socket.reply);socket.__handlers.clear();+ socket.connected = true;});describe('CompositionPreviewPlayer', () => {+ it('resumes an expired preview only on request and restores its controls and playhead', async () => {+ const look = { version: 1, preset: 'custom', grain: 0.4 };+ const { rerender } = render(<CompositionPreviewPlayer project={{ ...project, filmLook: look }} audioUrl='/song.mp3' />);+ await waitFor(() => expect(screen.getByRole('button', { name: 'Play' })).toBeEnabled());+ fireEvent.change(screen.getByLabelText('Scrub the composition preview'), { target: { value: '2' } });+ const oldOpen = opens()[0][1];+ const oldSessionId = socket.emit.mock.calls.find(([event]) => event === 'generated-preview:seek')[1].sessionId;+ act(() => {+ for (const fn of socket.__handlers.get('generated-preview:closed') || []) fn({ sessionId: oldSessionId, reason: 'idle' });+ });+ expect(screen.getByRole('button', { name: 'Play' })).toBeDisabled();+ expect(opens()).toHaveLength(1);+ const nextLook = { ...look, grain: 0.9 };+ rerender(<CompositionPreviewPlayer project={{ ...project, filmLook: nextLook }} audioUrl='/song.mp3' />);+ fireEvent.click(screen.getByRole('button', { name: 'Resume preview' }));+ expect(opens()).toHaveLength(2);+ const nextOpen = opens()[1][1];+ expect(nextOpen.requestId).not.toBe(oldOpen.requestId);+ expect(nextOpen.look).toEqual(nextLook);+ expect(socket.emit).toHaveBeenLastCalledWith('generated-preview:seek', expect.objectContaining({ requestId: nextOpen.requestId, t: 2 }));+ const nextSessionId = socket.emit.mock.calls.at(-1)[1].sessionId;+ act(() => {+ for (const fn of socket.__handlers.get('generated-preview:closed') || []) fn({ sessionId: oldSessionId, reason: 'idle' });+ });+ expect(screen.getByRole('button', { name: 'Play' })).toBeEnabled();+ act(() => {+ for (const fn of socket.__handlers.get('generated-preview:closed') || []) fn({ sessionId: nextSessionId, reason: 'replaced' });+ });+ expect(opens()).toHaveLength(2);+ expect(screen.getByRole('button', { name: 'Resume preview' })).toBeEnabled();+ act(() => {+ socket.connected = false;+ for (const fn of socket.__handlers.get('disconnect') || []) fn();+ socket.connected = true;+ for (const fn of socket.__handlers.get('connect') || []) fn();+ });+ expect(opens()).toHaveLength(2);+ expect(screen.getByRole('button', { name: 'Resume preview' })).toBeEnabled();+ });++ it('waits for a connection before opening and never reopens after unmount', async () => {+ socket.connected = false;+ const { unmount } = render(<CompositionPreviewPlayer project={project} audioUrl='/song.mp3' />);+ expect(opens()).toHaveLength(0);+ expect(screen.getByRole('button', { name: 'Play' })).toBeDisabled();+ act(() => {+ socket.connected = true;+ for (const fn of socket.__handlers.get('connect') || []) fn();+ for (const fn of socket.__handlers.get('connect') || []) fn();+ });+ await waitFor(() => expect(screen.getByRole('button', { name: 'Play' })).toBeEnabled());+ expect(opens()).toHaveLength(1);+ act(() => {+ socket.connected = false;+ for (const fn of socket.__handlers.get('disconnect') || []) fn();+ });+ const sent = socket.emit.mock.calls.length;+ unmount();+ act(() => {+ socket.connected = true;+ for (const fn of socket.__handlers.get('connect') || []) fn();+ });+ expect(socket.emit.mock.calls).toHaveLength(sent);+ });++ it('reopens a disconnected preview with current controls and ignores the obsolete session', async () => {+ const look = { version: 1, preset: 'custom', grain: 0.4 };+ const { rerender } = render(<CompositionPreviewPlayer project={{ ...project, filmLook: look }} audioUrl='/song.mp3' />);+ await waitFor(() => expect(screen.getByRole('button', { name: 'Play' })).toBeEnabled());+ fireEvent.change(screen.getByLabelText('Scrub the composition preview'), { target: { value: '2' } });+ const oldOpen = opens()[0][1];+ act(() => {+ for (const fn of socket.__handlers.get('generated-preview:error') || []) fn({ requestId: oldOpen.requestId, message: 'Synthetic seek failure' });+ });+ expect(screen.getByRole('button', { name: 'Play' })).toBeDisabled();+ act(() => {+ for (const fn of socket.__handlers.get('generated-preview:frame') || []) {+ fn({ requestId: oldOpen.requestId, generation: 1, mime: 'image/jpeg', data: btoa('recovered frame') });+ }+ });+ expect(screen.getByRole('button', { name: 'Play' })).toBeEnabled();+ act(() => {+ socket.connected = false;+ for (const fn of socket.__handlers.get('disconnect') || []) fn();+ });+ expect(screen.getByRole('button', { name: 'Play' })).toBeDisabled();+ expect(screen.getByLabelText('Scrub the composition preview')).toBeDisabled();+ const sent = socket.emit.mock.calls.length;+ const nextLook = { ...look, grain: 0.9 };+ rerender(<CompositionPreviewPlayer project={{ ...project, filmLook: nextLook }} audioUrl='/song.mp3' />);+ expect(socket.emit.mock.calls).toHaveLength(sent);+ socket.emit.mockImplementation(() => {});+ act(() => {+ socket.connected = true;+ for (const fn of socket.__handlers.get('connect') || []) fn();+ for (const fn of socket.__handlers.get('connect') || []) fn();+ });+ expect(opens()).toHaveLength(2);+ const nextOpen = opens()[1][1];+ expect(nextOpen.requestId).not.toBe(oldOpen.requestId);+ expect(nextOpen.look).toEqual(nextLook);+ act(() => socket.reply('generated-preview:open', oldOpen));+ expect(screen.getByRole('button', { name: 'Play' })).toBeDisabled();+ act(() => socket.reply('generated-preview:open', nextOpen));+ await waitFor(() => expect(screen.getByRole('button', { name: 'Play' })).toBeEnabled());+ expect(socket.emit).toHaveBeenLastCalledWith('generated-preview:seek', expect.objectContaining({ requestId: nextOpen.requestId, t: 2 }));+ fireEvent.change(screen.getByLabelText('Scrub the composition preview'), { target: { value: '3' } });+ expect(socket.emit).toHaveBeenLastCalledWith('generated-preview:seek', expect.objectContaining({ requestId: nextOpen.requestId, t: 3 }));+ });+it('preserves a one-frame step while pausing playback with audio present', async () => {const play = vi.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue();const pause = vi.spyOn(HTMLMediaElement.prototype, 'pause').mockImplementation(() => {});diff --git a/client/src/hooks/README.md b/client/src/hooks/README.mdindex 9a34945f42..3d29db31b8 100644--- a/client/src/hooks/README.md+++ b/client/src/hooks/README.md@@ -290,7 +290,7 @@ grep -i "what you want to do" client/src/hooks/README.md| `useEidoverseFrame` | Per-load Eidoverse handshake with origin/window/session validation and browser-only label preferences. | Embedding the managed Eidoverse renderer. || `useGallerySidecars` | Hydrates a known filename list into its image-metadata sidecars (the prompt actually sent, negative, model, seed, size), keyed on contents rather than array identity, with a `setSidecar` splice for a freshly written record. | Any page showing renders it owns the refs for (universe draft, deck cards) that must open the lightbox on the real render prompt. || `useGalleryPreviewResolver` | Builds the stable `resolveItem` callback that resolves a `?preview=` param the host's own items list does not contain, by looking the record up in the gallery and normalizing it. `usePreviewRoute` uses it as its DEFAULT. | Call it directly only to narrow the lookup — `collectionId` scopes it to one collection. |-| `useGeneratedPreview` | One OS-isolated generated-preview session. Emits `generated-preview:open` for `request` and returns inert JPEG frames, seek/command functions, and a recording blob. Film-look and title-card `controls` ride the open and later commands. A revision change reopens; a control change does not. Unsupported hosts surface `status: 'unavailable'` and never fall back to an iframe. | Music-video composition, code-video, and code-animation previews. |+| `useGeneratedPreview` | One OS-isolated generated-preview session. Emits `generated-preview:open` for `request` and returns inert JPEG frames, seek/command functions, and a recording blob. Reconnect opens a fresh session with the current controls and playhead; idle/capacity closure returns `status: 'idle'` until the viewer calls `resume()`. Film-look and title-card `controls` ride the open and later commands. A revision change reopens; a control change does not. Unsupported hosts surface `status: 'unavailable'` and never fall back to an iframe. | Music-video composition, code-video, and code-animation previews. || `useImageVariants` | Fetches the original-vs-cleaned variant set for the one open image (`GET /api/image-gen/:filename/variants`), normalized; reuses the held group across toggle clicks and aborts a superseded lookup. `null` while unfetched or failed, `[]` when the image is not indexed. | `MediaPreview` calls it for every image preview; neither a page-owned ref list nor a 60-row gallery window is a complete source of variant lineage. || `useGalleryPage` | Bounded image or mixed-media browsing with debounced server search, scopes, totals, stale-response cancellation and retryable next pages. | Gallery grids and pickers; `getGalleryImages` hydrates known references separately. || `usePagedCollection` | `usePagedCollection(fetchPage, { enabled })` — bounded cursor pages, deduplication by id, single-flight loading, abort/stale-query protection, retry, reset, and local mutation via setItems; an optional `meta` returned by `fetchPage` is exposed as the latest page's response-level data. | Growing cursor-based collections; pair with `InfiniteScrollFooter`. |diff --git a/client/src/hooks/useGeneratedPreview.js b/client/src/hooks/useGeneratedPreview.jsindex f25e24b383..0d117a5813 100644--- a/client/src/hooks/useGeneratedPreview.js+++ b/client/src/hooks/useGeneratedPreview.js@@ -5,6 +5,7 @@ import useMounted from './useMounted.js';const EMPTY = {status: 'idle',+ sessionId: null,reason: '',durationSec: 0,fps: 24,@@ -35,16 +36,20 @@ const decode = (chunk) => {export default function useGeneratedPreview({ active, request, controls = null }) {const mounted = useMounted();const pending = useRef(null);+ const openRef = useRef(null);+ const lastSeek = useRef(null);const frameUrl = useRef('');const recordingUrl = useRef('');const controlsRef = useRef(controls);controlsRef.current = controls;const [state, setState] = useState(EMPTY);const requestKey = active && request ? JSON.stringify(request) : '';+ const resume = useCallback(() => openRef.current?.(), []);const seek = useCallback((t, { playing = false } = {}) => {+ lastSeek.current = { t, playing };const current = pending.current;- if (!current?.sessionId) return;+ if (!socket.connected || !current?.sessionId) return;current.generation += 1;socket.emit('generated-preview:seek', {requestId: current.requestId,@@ -57,7 +62,7 @@ export default function useGeneratedPreview({ active, request, controls = null }const command = useCallback((action, extra = {}) => {const current = pending.current;- if (!current?.sessionId) return;+ if (!socket.connected || !current?.sessionId) return;socket.emit('generated-preview:command', {requestId: current.requestId,sessionId: current.sessionId,@@ -68,10 +73,11 @@ export default function useGeneratedPreview({ active, request, controls = null }}, []);useEffect(() => {+ lastSeek.current = null;if (!requestKey) {const current = pending.current;pending.current = null;- if (current) {+ if (current && socket.connected) {socket.emit('generated-preview:close', {requestId: current.requestId,...(current.sessionId ? { sessionId: current.sessionId } : {}),@@ -80,19 +86,45 @@ export default function useGeneratedPreview({ active, request, controls = null }if (mounted.current) setState(EMPTY);return undefined;}- const requestId = newRequestId();- const openControls = { look: controlsRef.current?.look ?? null, card: controlsRef.current?.card ?? null };- pending.current = { requestId, sessionId: null, generation: 0, controls: openControls };- if (mounted.current) setState((prev) => ({ ...prev, status: 'opening', ready: false, reason: '', lastEvent: null }));+ let requestId = null;+ const recordingChunks = new Map();+ let stoppedStatus = null;+ // Sessions belong to one socket connection. Never buffer an open/command+ // while offline: the server has already destroyed the previous session.+ const open = () => {+ if (!socket.connected || pending.current) return;+ stoppedStatus = null;+ requestId = newRequestId();+ const openControls = { look: controlsRef.current?.look ?? null, card: controlsRef.current?.card ?? null };+ pending.current = { requestId, sessionId: null, generation: 0, controls: openControls };+ if (mounted.current) setState((prev) => ({ ...prev, status: 'opening', sessionId: null, ready: false, reason: '', lastEvent: null }));+ socket.emit('generated-preview:open', {+ requestId,+ ...request,+ look: openControls.look,+ card: openControls.card,+ });+ };+ const onDisconnect = () => {+ pending.current = null;+ recordingChunks.clear();+ if (mounted.current) setState((prev) => ({ ...prev, status: 'disconnected', sessionId: null, ready: false, lastEvent: null }));+ };+ const onConnect = () => {+ if (!stoppedStatus) open();+ else if (mounted.current) setState((prev) => ({ ...prev, status: stoppedStatus, ready: false }));+ };const mine = (payload) => payload?.requestId === requestId && pending.current?.requestId === requestId;const onOpened = (payload) => {if (!mine(payload)) return;+ stoppedStatus = null;pending.current = { ...pending.current, sessionId: payload.sessionId };if (!mounted.current) return;setState((prev) => ({...prev,status: 'ready',+ sessionId: payload.sessionId,ready: true,reason: '',durationSec: Number(payload.durationSec) || 0,@@ -101,6 +133,9 @@ export default function useGeneratedPreview({ active, request, controls = null }height: Number(payload.height) || 0,audioStatus: payload.audio || 'none',}));+ // A reconnect opens at time zero; restore the current scrub/playhead+ // position using the new session, including seeks made while offline.+ if (lastSeek.current) seek(lastSeek.current.t, lastSeek.current);};const onFrame = (payload) => {if (!mine(payload) || !payload.data) return;@@ -115,10 +150,15 @@ export default function useGeneratedPreview({ active, request, controls = null }frameUrl.current = url;if (previous) URL.revokeObjectURL(previous);if (!mounted.current) return;- setState((prev) => ({ ...prev, frameUrl: url, status: 'ready', ready: true }));+ // Screencasting can start before the open acknowledgement. Show its+ // frame, but commands need the session identity established by opened.+ const opened = !!pending.current.sessionId;+ if (opened) stoppedStatus = null;+ setState((prev) => ({ ...prev, frameUrl: url, ...(opened ? { status: 'ready', ready: true } : {}) }));};const onUnavailable = (payload) => {if (!mine(payload) || !mounted.current) return;+ stoppedStatus = 'unavailable';setState((prev) => ({...prev, status: 'unavailable', ready: false, reason: payload.message || 'Isolated preview is unavailable on this host.',}));@@ -126,6 +166,7 @@ export default function useGeneratedPreview({ active, request, controls = null }const onError = (payload) => {if (payload?.requestId && payload.requestId !== requestId) return;if (pending.current?.requestId !== requestId || !mounted.current) return;+ stoppedStatus = 'error';setState((prev) => ({...prev, status: 'error', ready: false, reason: payload.message || 'The isolated preview could not start.',}));@@ -134,7 +175,6 @@ export default function useGeneratedPreview({ active, request, controls = null }if (!mine(payload) || !mounted.current) return;setState((prev) => ({ ...prev, lastEvent: payload }));};- let recordingChunks = new Map();const onRecording = (payload) => {if (!mine(payload) || typeof payload.chunk !== 'string' || !payload.count) return;const gen = payload.generation ?? 0;@@ -155,8 +195,13 @@ export default function useGeneratedPreview({ active, request, controls = null }};const onClosed = (payload) => {if (!pending.current?.sessionId || payload?.sessionId !== pending.current.sessionId) return;+ pending.current = null;+ recordingChunks.clear();+ stoppedStatus = 'idle';if (!mounted.current) return;- setState((prev) => ({ ...prev, status: 'idle', ready: false }));+ // Idle/capacity cleanup stays effective until the viewer explicitly+ // resumes. Reopening here would keep idle renderers alive indefinitely.+ setState((prev) => ({ ...prev, status: 'idle', sessionId: null, ready: false, lastEvent: null }));};socket.on('generated-preview:opened', onOpened);@@ -166,12 +211,11 @@ export default function useGeneratedPreview({ active, request, controls = null }socket.on('generated-preview:event', onEvent);socket.on('generated-preview:recording', onRecording);socket.on('generated-preview:closed', onClosed);- socket.emit('generated-preview:open', {- requestId,- ...request,- look: openControls.look ?? null,- card: openControls.card ?? null,- });+ socket.on('connect', onConnect);+ socket.on('disconnect', onDisconnect);+ openRef.current = open;+ if (socket.connected) open();+ else onDisconnect();return () => {socket.off('generated-preview:opened', onOpened);socket.off('generated-preview:frame', onFrame);@@ -180,17 +224,23 @@ export default function useGeneratedPreview({ active, request, controls = null }socket.off('generated-preview:event', onEvent);socket.off('generated-preview:recording', onRecording);socket.off('generated-preview:closed', onClosed);+ socket.off('connect', onConnect);+ socket.off('disconnect', onDisconnect);+ openRef.current = null;const current = pending.current;if (current?.requestId === requestId) {- socket.emit('generated-preview:close', {- requestId,- ...(current.sessionId ? { sessionId: current.sessionId } : {}),- });+ pending.current = null;+ if (socket.connected) {+ socket.emit('generated-preview:close', {+ requestId,+ ...(current.sessionId ? { sessionId: current.sessionId } : {}),+ });+ }}};// `request` is read for the payload; requestKey is its identity.// eslint-disable-next-line react-hooks/exhaustive-deps- }, [requestKey, mounted]);+ }, [requestKey, mounted, seek]);// Reconcile against what this session actually received. Controls can change// while its open request is in flight; readiness must not mark those edits sent.@@ -210,5 +260,5 @@ export default function useGeneratedPreview({ active, request, controls = null }if (recordingUrl.current) URL.revokeObjectURL(recordingUrl.current);}, []);- return { ...state, seek, command };+ return { ...state, seek, command, resume };}diff --git a/client/src/pages/CodeAnimation.test.jsx b/client/src/pages/CodeAnimation.test.jsxindex 5224579d0c..f165d44a57 100644--- a/client/src/pages/CodeAnimation.test.jsx+++ b/client/src/pages/CodeAnimation.test.jsx@@ -6,6 +6,7 @@ import { MemoryRouter, Route, Routes } from 'react-router';const socketHarness = vi.hoisted(() => ({ handlers: new Map(), emit: vi.fn() }));vi.mock('../services/socket', () => ({ default: {+ connected: true,emit: (event, payload) => {socketHarness.emit(event, payload);if (event !== 'generated-preview:open') return;