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
feat: clean up superseded music video renders
[._.]/ Adam Eivy8e2df09d1a89
diff --git a/client/src/components/musicVideo/MusicVideoLayout.jsx b/client/src/components/musicVideo/MusicVideoLayout.jsxindex 8c2c9780f5..112b1adbc5 100644--- a/client/src/components/musicVideo/MusicVideoLayout.jsx+++ b/client/src/components/musicVideo/MusicVideoLayout.jsx@@ -1,6 +1,6 @@import { useEffect, useRef } from 'react';import {- ArrowRight, Bot, Check, Clapperboard, Film, History, Images, LayoutGrid, Music, Pencil, Play, Send, Settings, Users,+ ArrowRight, Bot, Check, Clapperboard, Film, History, Images, LayoutGrid, Music, Pencil, Play, Send, Settings, Users, Trash2,} from 'lucide-react';import CancelRenderButton from './CancelRenderButton.jsx';import TabPills from '../ui/TabPills.jsx';@@ -11,7 +11,7 @@ import { MUSIC_VIDEO_PAGES } from '../../lib/musicVideoStages.js';// One icon per step: the phone's bottom bar collapses to icons, and a repeated// glyph would be a repeated destination.const STAGE_ICONS = {- setup: Music, 'cast-sets': Users, board: LayoutGrid, produce: Clapperboard, review: Film, publish: Send, assets: Images,+ setup: Music, 'cast-sets': Users, board: LayoutGrid, produce: Clapperboard, review: Film, publish: Send, assets: Images, cleanup: Trash2,};// The page's scroll body (the route is full-width, so the page owns itsdiff --git a/client/src/components/musicVideo/MusicVideoLayout.test.jsx b/client/src/components/musicVideo/MusicVideoLayout.test.jsxindex 3acf06ea9c..9af5942eb2 100644--- a/client/src/components/musicVideo/MusicVideoLayout.test.jsx+++ b/client/src/components/musicVideo/MusicVideoLayout.test.jsx@@ -28,7 +28,7 @@ describe('music-video step navigation', () => {'4Shots' + 'Not started','5Final render' + 'Not started','6Publish' + 'Not started',- 'Assets',+ 'Assets', 'Cleanup',]);expect(steps[2]).toHaveAttribute('aria-current', 'step');fireEvent.click(steps[3]);@@ -37,7 +37,7 @@ describe('music-video step navigation', () => {expect(onStageChange).toHaveBeenCalledWith('assets');// The phone bar says each state to screen readers, since it shows only icons.expect(within(phoneBar()).getAllByRole('tab').map((tab) => tab.textContent)).toEqual([- 'Song, Done', 'Look, Done', 'Plan, Needs you', 'Shots, Not started', 'Final render, Not started', 'Publish, Not started', 'Assets',+ 'Song, Done', 'Look, Done', 'Plan, Needs you', 'Shots, Not started', 'Final render, Not started', 'Publish, Not started', 'Assets', 'Cleanup',]);// The step names what "done" means under its title, and labels the panel.expect(screen.getByRole('tabpanel', { name: 'Plan' })).toBeInTheDocument();diff --git a/client/src/components/musicVideo/stages/CleanupStage.jsx b/client/src/components/musicVideo/stages/CleanupStage.jsxnew file mode 100644index 0000000000..99a8dbf801--- /dev/null+++ b/client/src/components/musicVideo/stages/CleanupStage.jsx@@ -0,0 +1,95 @@+import { useEffect, useRef, useState } from 'react';+import { RefreshCw, Trash2 } from 'lucide-react';+import { cleanupMusicVideoRenders, getMusicVideoRenderCleanup } from '../../../services/apiMusicVideo.js';+import socket from '../../../services/socket';+import { formatBytes, formatCount, formatDateTime } from '../../../utils/formatters.js';++const ACTION = 'inline-flex min-h-[44px] items-center gap-2 rounded border border-port-border px-3 py-2 text-sm disabled:opacity-50';+const bytes = files => files.reduce((sum, file) => sum + file.bytes, 0);++export default function CleanupStage({ board }) {+ const { project } = board;+ const [inventory, setInventory] = useState(null);+ const [error, setError] = useState(null);+ const [busy, setBusy] = useState(false);+ const generation = useRef(0);+ const cleaning = useRef(false);+ const refresh = useRef(null);+ refresh.current = () => {+ const ticket = ++generation.current;+ getMusicVideoRenderCleanup(project.id, { silent: true }).then(next => {+ if (ticket === generation.current) { setInventory(next); setError(null); }+ }).catch(err => { if (ticket === generation.current) setError(err.message); });+ };+ useEffect(() => {+ setInventory(null);+ setError(null);+ refresh.current();+ const onChange = event => { if (event.projectId === project.id) refresh.current(); };+ const onConnect = () => refresh.current();+ socket.on('music-video:render-cleanup', onChange);+ socket.on('music-video:shot-render', onChange);+ socket.on('connect', onConnect);+ return () => {+ generation.current += 1;+ socket.off('music-video:render-cleanup', onChange);+ socket.off('music-video:shot-render', onChange);+ socket.off('connect', onConnect);+ };+ }, [project.id, project.renderHistoryId, project.updatedAt]);++ const clean = unusedLooks => {+ if (cleaning.current) return;+ cleaning.current = true;+ setBusy(true);+ const ticket = ++generation.current;+ cleanupMusicVideoRenders(project.id, { unusedLooks }, { silent: true }).then(next => {+ if (ticket === generation.current) { setInventory(next); setError(null); }+ }).catch(err => { if (ticket === generation.current) setError(err.message); })+ .finally(() => { cleaning.current = false; setBusy(false); });+ };+ const finals = inventory?.finals || [];+ const eligible = finals.filter(file => !file.reason);+ const stale = inventory?.segments.stale || [];+ const looks = inventory?.segments.unusedLooks || [];+ return (+ <section className="space-y-4" aria-label="Production cleanup">+ <div className="flex flex-wrap items-start justify-between gap-3">+ <div className="min-w-0 max-w-2xl">+ <h3 className="font-semibold">Production cleanup</h3>+ <p className="text-sm text-port-text-muted">Successful rerenders replace superseded final videos and shot segments. Source takes, stills, and cutting-room content stay in Assets.</p>+ <p className="mt-2 text-sm text-port-text-muted">Cleanup permanently removes derived render files. The latest final, current shot renders, and referenced composites stay. Film-look comparisons stay until you remove unused looks below.</p>+ </div>+ <button type="button" className={ACTION} disabled={busy} onClick={() => refresh.current()}><RefreshCw size={16} />Refresh</button>+ </div>+ {error && <p role="alert" className="text-sm text-port-error">{error}</p>}+ {!inventory && !error && <p role="status">Inspecting render files…</p>}+ {inventory && <>+ {inventory.segments.error && <p role="alert" className="text-sm text-port-warning">Shot cleanup unavailable: {inventory.segments.error.message}. Fix the shot plan to inspect its cache safely.</p>}+ <div className="grid grid-cols-1 gap-3 sm:grid-cols-3">+ <InventoryCard title="Superseded finals" files={eligible} noun="renders" />+ <InventoryCard title="Stale segments and previews" files={stale} />+ <InventoryCard title="Unused film-look versions" files={looks} />+ </div>+ <div className="flex flex-wrap gap-2">+ <button type="button" className={ACTION} disabled={busy || !(eligible.length + stale.length)} onClick={() => clean(false)}><Trash2 size={16} />Remove superseded renders</button>+ <button type="button" className={ACTION} disabled={busy || !looks.length} onClick={() => clean(true)}><Trash2 size={16} />Remove superseded renders and unused looks</button>+ </div>+ {busy && <p role="status" className="text-sm">Cleaning render files…</p>}+ {finals.length > 0 && <ul className="space-y-2" aria-label="Older final renders">+ {finals.map(file => <li key={file.id} className="break-words rounded border border-port-border p-3 text-sm">+ <p>{file.createdAt ? formatDateTime(file.createdAt) : 'Older final'} · {formatBytes(file.bytes)}</p>+ <p className="text-port-text-muted">{file.reason ? `Kept: ${file.reason}` : 'Superseded — safe to remove'}</p>+ </li>)}+ </ul>}+ </>}+ </section>+ );+}++function InventoryCard({ title, files, noun = 'files' }) {+ return <div className="min-w-0 rounded border border-port-border p-3">+ <h4 className="text-sm text-port-text-muted">{title}</h4>+ <p className="mt-1 font-semibold">{formatCount(files.length)} {noun} · {formatBytes(bytes(files))}</p>+ </div>;+}diff --git a/client/src/components/musicVideo/stages/CleanupStage.test.jsx b/client/src/components/musicVideo/stages/CleanupStage.test.jsxnew file mode 100644index 0000000000..67c5a68e9f--- /dev/null+++ b/client/src/components/musicVideo/stages/CleanupStage.test.jsx@@ -0,0 +1,49 @@+import { beforeEach, describe, expect, it, vi } from 'vitest';+import { fireEvent, render, screen, waitFor } from '@testing-library/react';+import CleanupStage from './CleanupStage.jsx';+import { cleanupMusicVideoRenders, getMusicVideoRenderCleanup } from '../../../services/apiMusicVideo.js';++vi.mock('../../../services/apiMusicVideo.js', () => ({ getMusicVideoRenderCleanup: vi.fn(), cleanupMusicVideoRenders: vi.fn() }));+vi.mock('../../../services/socket', () => ({ default: { on: vi.fn(), off: vi.fn() } }));+const inventory = () => ({ finals: [+ { id: 'old', bytes: 1000, reason: null }, { id: 'held', bytes: 4000, reason: 'Referenced by saved media or production records' },+], segments: { stale: [{ name: 'old-shot.mp4', bytes: 2000 }], unusedLooks: [{ name: 'old-look.mp4', bytes: 3000 }] } });+beforeEach(() => {+ vi.clearAllMocks();+ getMusicVideoRenderCleanup.mockResolvedValue(inventory());+ cleanupMusicVideoRenders.mockResolvedValue({ finals: [inventory().finals[1]], segments: { stale: [], unusedLooks: inventory().segments.unusedLooks } });+});++describe('production cleanup', () => {+ it('removes derived renders while keeping comparisons by default, then offers explicit look purging', async () => {+ render(<CleanupStage board={{ project: { id: 'example-project' } }} />);+ const cleanup = await screen.findByRole('button', { name: 'Remove superseded renders' });+ expect(screen.getByText(/Source takes, stills, and cutting-room content stay in Assets/)).toBeInTheDocument();+ expect(screen.getByText(/Kept: Referenced/)).toBeInTheDocument();+ fireEvent.click(cleanup);+ await waitFor(() => expect(cleanupMusicVideoRenders).toHaveBeenCalledWith('example-project', { unusedLooks: false }, { silent: true }));+ await waitFor(() => expect(cleanup).toBeDisabled());+ const purge = screen.getByRole('button', { name: 'Remove superseded renders and unused looks' });+ expect(purge).toBeEnabled();+ fireEvent.click(purge);+ await waitFor(() => expect(cleanupMusicVideoRenders).toHaveBeenLastCalledWith('example-project', { unusedLooks: true }, { silent: true }));+ });++ it('shows a cleanup refusal without clearing the inventory', async () => {+ cleanupMusicVideoRenders.mockRejectedValue(new Error('Wait for shot renders to finish'));+ render(<CleanupStage board={{ project: { id: 'example-project' } }} />);+ fireEvent.click(await screen.findByRole('button', { name: 'Remove superseded renders' }));+ expect(await screen.findByRole('alert')).toHaveTextContent('Wait for shot renders to finish');+ expect(screen.getByText('Superseded — safe to remove')).toBeInTheDocument();+ });++ it('drops a previous project inventory response after switching projects', async () => {+ let resolveOld;+ getMusicVideoRenderCleanup.mockReturnValueOnce(new Promise(resolve => { resolveOld = resolve; }));+ const { rerender } = render(<CleanupStage board={{ project: { id: 'old-project' } }} />);+ rerender(<CleanupStage board={{ project: { id: 'new-project' } }} />);+ await screen.findByText('Superseded — safe to remove');+ resolveOld({ finals: [{ id: 'wrong', bytes: 100, reason: 'Old project' }], segments: { stale: [], unusedLooks: [] } });+ await waitFor(() => expect(screen.queryByText('Kept: Old project')).not.toBeInTheDocument());+ });+});diff --git a/client/src/lib/musicVideoStages.js b/client/src/lib/musicVideoStages.jsindex 52c40fa904..ba8a148cbb 100644--- a/client/src/lib/musicVideoStages.js+++ b/client/src/lib/musicVideoStages.js@@ -41,6 +41,7 @@ export const MUSIC_VIDEO_STAGES = [*/export const MUSIC_VIDEO_PAGES = [{ id: 'assets', label: 'Assets', title: 'Assets' },+ { id: 'cleanup', label: 'Cleanup', title: 'Cleanup' },];// Old links name the Compose tab, which is now part of Shots; the new names work as links too.diff --git a/client/src/pages/MusicVideo.jsx b/client/src/pages/MusicVideo.jsxindex 8061a179be..145c0c6d4e 100644--- a/client/src/pages/MusicVideo.jsx+++ b/client/src/pages/MusicVideo.jsx@@ -85,6 +85,7 @@ import ProduceStage from '../components/musicVideo/stages/ProduceStage.jsx';import ReviewStage from '../components/musicVideo/stages/ReviewStage.jsx';import { PUBLISH_TARGETS } from '../components/musicVideo/PublishPostingPanel.jsx';import PublishStage from '../components/musicVideo/stages/PublishStage.jsx';+import CleanupStage from '../components/musicVideo/stages/CleanupStage.jsx';import AssetsStage from '../components/musicVideo/stages/AssetsStage.jsx';import { compositionDraft } from '../components/musicVideo/compositionDraft.js';import ContactSheetDrawer from '../components/musicVideo/ContactSheetDrawer.jsx';@@ -142,7 +143,7 @@ const approvalStageFor = (tab, project, readiness, proofActive) => (tab === 'proconst PLAYER_FIRST_STAGES = new Set(['board', 'produce', 'review']);const STAGE_VIEWS = {setup: SetupStage, 'cast-sets': CastSetsStage, board: PlanStage, produce: ProduceStage, review: ReviewStage, publish: PublishStage,- assets: AssetsStage,+ assets: AssetsStage, cleanup: CleanupStage,};diff --git a/client/src/services/README.md b/client/src/services/README.mdindex 0dac3912ba..a0788f62d6 100644--- a/client/src/services/README.md+++ b/client/src/services/README.md@@ -112,7 +112,7 @@ toasts on throw). **Custom catch ⇒ `silent: true`** — otherwise toasts fire| `apiCreativeCommission.js` | Creative Commissions (Autonomous Creation Engine — standing recurring briefs). || `apiFableLoom.js` | FableLoom branching narratives — loom/episode/scene-node/transition CRUD, deterministic graph validation, AI authoring lanes, and the bounded editorial/playthrough autopilot lifecycle. || `apiGames.js` | Game studio records, managed-app binding, reusable sprite/music bindings, deterministic asset-bundle compilation/integrity preflight, and AI feedback history. |-| `apiMusicVideo.js` | Music Video projects + scene board + audio analysis. |+| `apiMusicVideo.js` | Music Video projects + scene board + audio analysis. Includes render cleanup inventory/apply with optional unused film-look purging. || `apiContinuousVideo.js` | Continuous-video episode generation: client-side-callable lint preview of a scene draft, queue-episode submit, and the SSE progress-events URL. || `apiSprites.js` | Sprite Manager records, asset library, production-set import (#2895), reference workflow: create/generate/lock (#2896), directional walk and per-track generation/approval, animation-type definition CRUD (#3153), trim/postprocess, and per-run source-frame listing for the Loop Trimmer's re-derive (#2980), and animation render-provider readiness (#4876). || `apiShell.js` | Shell sessions over HTTP: hand a photo (plus a message) to the agent TUI running in a session. Keystrokes/output stay on the `shell:*` socket protocol. |diff --git a/client/src/services/apiMusicVideo.js b/client/src/services/apiMusicVideo.jsindex 1c62515598..5e2ce82086 100644--- a/client/src/services/apiMusicVideo.js+++ b/client/src/services/apiMusicVideo.js@@ -674,3 +674,8 @@ export const importMusicVideoDocumentShots = (id, body, options = {}) => requestexport const getMusicVideoSharingCopy = (id, options = {}) => request(`/music-video/${encodeURIComponent(id)}/sharing-copy`, options);export const prepareMusicVideoSharingCopy = (id, options = {}) => request(`/music-video/${encodeURIComponent(id)}/sharing-copy`, { method: 'POST', ...options });export const musicVideoSharingCopyDownloadUrl = (id) => `/api/music-video/${encodeURIComponent(id)}/sharing-copy/download`;++export const getMusicVideoRenderCleanup = (id, options = {}) => request(`/music-video/${encodeURIComponent(id)}/render-cleanup`, options);+export const cleanupMusicVideoRenders = (id, body = {}, options = {}) => request(`/music-video/${encodeURIComponent(id)}/render-cleanup`, {+ method: 'POST', body: JSON.stringify(body), ...options,+});diff --git a/server/lib/musicVideoValidation.js b/server/lib/musicVideoValidation.jsindex cce6cbaa8e..d49207f91e 100644--- a/server/lib/musicVideoValidation.js+++ b/server/lib/musicVideoValidation.js@@ -93,6 +93,9 @@ export const MUSIC_VIDEO_MODES = ['director', 'autonomous'];export const MUSIC_VIDEO_STATUSES = ['draft', 'analyzed', 'ready', 'rendering', 'complete', 'failed'];// Re-read a project's song style from its Suno song link.+// Explicit cleanup keeps comparison looks unless the director opts to purge them.+export const musicVideoRenderCleanupSchema = z.object({ unusedLooks: z.boolean().optional() }).strict();+// The rendered-so-far player: `light` asks for the lighter phone copy.export const musicVideoRenderPreviewSchema = z.object({ light: z.boolean().optional() }).strict();export const musicVideoSongStyleSunoSchema = z.object({ url: z.string().trim().min(1).max(2000) });diff --git a/server/lib/navManifest.js b/server/lib/navManifest.jsindex b397d2ef32..ea2a6fd678 100644--- a/server/lib/navManifest.js+++ b/server/lib/navManifest.js@@ -74,6 +74,7 @@ const RAW_NAV_COMMANDS = [{ id: 'nav.create.music-video.review', path: '/music-video/review', label: 'Music Video Final Render', section: 'Create', aliases: ['music-video-review', 'mv-review', 'music video review', 'music-video-render', 'mv-render'], keywords: ['music video', 'final render', 'render', 'review', 'export', 'drafts'] },{ id: 'nav.create.music-video.publish', path: '/music-video/publish', label: 'Music Video Publish', section: 'Create', aliases: ['music-video-publish', 'mv-publish', 'music video publish'], keywords: ['music video', 'publish', 'release', 'kit', 'social'] },{ id: 'nav.create.music-video.assets', path: '/music-video/assets', label: 'Music Video Assets', section: 'Create', aliases: ['music-video-assets', 'mv-assets', 'music video assets', 'asset-manager', 'asset manager'], keywords: ['music video', 'assets', 'stills', 'clips', 'takes', 'ingredients', 'bad', 'cleanup', 'media'] },+ { id: 'nav.create.music-video-cleanup', path: '/music-video/cleanup', label: 'Music Video Cleanup', section: 'Create', aliases: ['music-video-cleanup', 'mv-cleanup', 'music video cleanup'], keywords: ['superseded', 'renders', 'film looks', 'disk', 'cleanup'] },{ id: 'nav.create.decks', path: '/decks', label: 'Decks', section: 'Create', aliases: ['decks', 'deck', 'card-deck', 'playing-cards', 'tarot', 'tarot-deck', 'cards'], keywords: ['playing cards', 'tarot', 'card deck', 'card design', 'suits', 'arcana', 'style guide', 'batch render', 'card art'] },{ id: 'nav.create.mood-boards', path: '/mood-boards', label: 'Mood Boards', section: 'Create', aliases: ['mood-boards', 'mood-board', 'moodboard', 'mood', 'inspiration', 'references'], keywords: ['inspiration', 'reference', 'pin', 'visual', 'canvas', 'collect', 'pinboard', 'palette', 'ideas'] },// Promoted out of the Media Gen tabs to a top-level Create page (#2930).diff --git a/server/lib/socketEventContracts.js b/server/lib/socketEventContracts.jsindex e98750952a..9b174a6559 100644--- a/server/lib/socketEventContracts.js+++ b/server/lib/socketEventContracts.js@@ -96,6 +96,10 @@ export const SOCKET_EVENT_CONTRACTS = Object.freeze({direction: 'server-to-client', summary: 'ClinVar download and indexing progress for an optional client request ID.',payloadSchema: { type: 'object', required: ['message'], properties: { message: { type: 'string' }, requestId: { type: 'string', format: 'uuid' } } },},+ 'music-video:render-cleanup': {+ direction: 'server-to-client', summary: 'Derived render cleanup changed; refresh the project render inventory.',+ payloadSchema: { type: 'object', required: ['projectId'], properties: { projectId: { type: 'string' } }, additionalProperties: false },+ },'music-video:song-revision': {direction: 'server-to-client', summary: 'A song revision changed; apply the persisted project snapshot.',payloadSchema: { type: 'object', required: ['projectId', 'project'], properties: { projectId: { type: 'string' }, project: { type: 'object' } } },diff --git a/server/routes/musicVideo.js b/server/routes/musicVideo.jsindex db77ba049d..cd2ddf8628 100644--- a/server/routes/musicVideo.js+++ b/server/routes/musicVideo.js@@ -12,7 +12,7 @@ import { existsSync } from 'fs';import { unlink } from 'fs/promises';import { pipeline } from 'stream/promises';import { Router } from 'express';-import { musicVideoRenderPreviewSchema, musicVideoSongStyleSunoSchema, musicVideoProjectListQuerySchema, musicVideoFinishedOutsideSchema, musicVideoProductionDraftSchema, musicVideoProductionApprovalSchema, musicVideoShotLockSchema, musicVideoProductionProofSchema, musicVideoProductionImportSchema, musicVideoContinueShotSchema, musicVideoPoolUseSchema, musicVideoPoolMarkSchema, musicVideoPoolSearchQuerySchema, musicVideoPoolDeleteSchema, musicVideoPoolImportSchema, musicVideoLibrarySearchQuerySchema, musicVideoPoolAddSchema, musicVideoProductionFeedbackSchema, musicVideoProductionFeedbackResolutionSchema, musicVideoLyricAnchorSyncSchema, musicVideoProductionReviseSchema, musicVideoProductionRevertSchema } from '../lib/musicVideoValidation.js';+import { musicVideoRenderCleanupSchema, musicVideoRenderPreviewSchema, musicVideoSongStyleSunoSchema, musicVideoProjectListQuerySchema, musicVideoFinishedOutsideSchema, musicVideoProductionDraftSchema, musicVideoProductionApprovalSchema, musicVideoShotLockSchema, musicVideoProductionProofSchema, musicVideoProductionImportSchema, musicVideoContinueShotSchema, musicVideoPoolUseSchema, musicVideoPoolMarkSchema, musicVideoPoolSearchQuerySchema, musicVideoPoolDeleteSchema, musicVideoPoolImportSchema, musicVideoLibrarySearchQuerySchema, musicVideoPoolAddSchema, musicVideoProductionFeedbackSchema, musicVideoProductionFeedbackResolutionSchema, musicVideoLyricAnchorSyncSchema, musicVideoProductionReviseSchema, musicVideoProductionRevertSchema } from '../lib/musicVideoValidation.js';import { productionReadiness } from '../services/musicVideo/productionReview.js';import { presentMusicVideoDependencies } from '../lib/musicVideoDependencies.js';import { finishedOutsideBlocker } from '../lib/musicVideoFinishedOutside.js';@@ -724,6 +724,16 @@ router.get('/:id/render', (req, res) => {res.json({ jobId: getActiveRenderJobId(req.params.id) });});+router.get('/:id/render-cleanup', asyncHandler(async (req, res) => {+ const { getRenderCleanup } = await import('../services/musicVideo/renderCleanup.js');+ res.json(await getRenderCleanup(req.params.id));+}));+router.post('/:id/render-cleanup', asyncHandler(async (req, res) => {+ const input = validateRequest(musicVideoRenderCleanupSchema, req.body ?? {});+ const { cleanupRenders } = await import('../services/musicVideo/renderCleanup.js');+ res.json(await cleanupRenders(req.params.id, input));+}));+// Per-shot render segments (shotSegments.js): every shot's cache state, one// shot's render started or stopped, and a rendered shot's silent segment.router.get('/:id/shot-renders', asyncHandler(async (req, res) => {diff --git a/server/services/README.md b/server/services/README.mdindex df4e36173b..5d24d726c7 100644--- a/server/services/README.md+++ b/server/services/README.md@@ -16,6 +16,7 @@ image-generation paths below are useful entry points when tracing render jobs.| `databaseMaintenanceQuiescence.js` | Internal coordinator stage after producer shutdown: terminate verified running detached-writer groups, prove every registry record quiescent from process-table evidence, archive it with the operation, and refuse pending, legacy, orphaned, ambiguous or unregistered writers. || `gmailApi.js` | Six-operation Gmail v1 adapter (`createGmailApi(auth)`) over the shared OAuth client's `request()`: fixed origin, encoded IDs, URLSearchParams queries, explicit JSON bodies, capped 30s request timeout and no automatic send retries. Auth, refresh and transport stay in `googleAuth.js`. || `imageGen/prepareParams.js` | Shared image request preparation and the canonical local image model selector. Use `selectLocalImageModelFromSettings()` for model identity and `resolveLocalImageModel()` when the caller must also enforce local runtime, hardware, and edit-image requirements. |+| `musicVideo/renderCleanup.js` | Derived-render inventory and cleanup, preserving current finals, saved references, source takes and comparison looks; serializes retirement with project and history writers. || `musicVideo/renderCancellation.js` | Shared capture-abort and ffmpeg kill selection for full/excerpt renders; records explicit cancellation before signaling and classifies nonzero closes without releasing ownership. || `musicVideo/sharingCopy.js` | Source-hashed private sharing MP4s: queued 720p H.264/AAC export, strict decimal 100 MB cap, supervised cancellation, verified download and owned-file cleanup. || `mediaJobQueue/index.js` | Owns admission, lanes, durability and terminal effects; boot passes `deferCompletionRecovery` to `initMediaJobQueue()` and calls `resumeMediaJobCompletions()` after completion hooks are registered; `videoHolds.js` returns local-video cohort preparation results and owns circuit breakers, `recoverySettlement.js` names the transient recovery evidence and the maintenance-permit release decision, while `videoGen/modelSelection.js` owns model-selection policy. |diff --git a/server/services/musicVideo/render.js b/server/services/musicVideo/render.jsindex 33e3f46fc7..4aed50e903 100644--- a/server/services/musicVideo/render.js+++ b/server/services/musicVideo/render.js@@ -161,6 +161,8 @@ function findCoverageShortfalls(clips, { toleranceSec = COVERAGE_TOLERANCE_SEC }* Read-only: lets a reloaded page re-attach to the render's progress stream* without POSTing /render, which would START one when none is live (#9940).*/+export const isProjectRenderActive = (projectId) => projectRenders.has(projectId);+export function getActiveRenderJobId(projectId) {const existing = projectRenders.get(projectId);return existing && existing !== PENDING && jobs.has(existing) ? existing : null;@@ -1013,6 +1015,7 @@ async function renderSeekedMode(projectId, project, handOff, renderer, options =await writeSettled(completed);return poster;});+ await retireOldFinals(projectId, jobId);await finish(completed, { written: true });console.log(`✅ ${renderer.label[0].toUpperCase()}${renderer.label.slice(1)} music video rendered [${jobId.slice(0, 8)}]: ${filename}`);broadcastSse(job, { type: 'complete', result: { id: jobId, filename, thumbnail: thumb, path: `/data/videos/${filename}`, proof: proof ? `/data/video-thumbnails/${proof}` : null } });@@ -1094,9 +1097,10 @@ async function renderFromShotSegments(projectId, project, handOff, options) {await writeProject(settledRender('complete', { renderHistoryId: jobId, renderDependencies: captureMusicVideoEvidence(project)}), 'complete');return poster;});- finish('complete');+ await retireOldFinals(projectId, jobId);// Older takes of each shot are dropped once the new cut is published.- const pruned = await pruneSegments(plan, { startedAtMs }).catch(() => 0);+ const pruned = await pruneSegments(plan, { startedAtMs }).catch(err => { console.warn(`⚠️ Music-video segment cleanup failed: ${err.message}`); return 0; });+ finish('complete');console.log(`✅ Music video rendered [${jobId.slice(0, 8)}]: ${filename} (${all.length - cached} of ${all.length} segments rendered${pruned ? `, ${pruned} old dropped` : ''})`);broadcastSse(job, { type: 'complete', result: { id: jobId, filename, thumbnail: thumb, path: `/data/videos/${filename}` } });}).catch(async (err) => {@@ -1138,6 +1142,8 @@ async function renderAdmittedMusicVideo(projectId, options, permit) {if (typeof options?.codeDirectory === 'string' && options.codeDirectory) {throw new ServerError('Import and select the composition document before rendering.', { status: 409, code: 'MUSIC_VIDEO_UNREVIEWED_DIRECTORY' });}+ const { assertRenderCleanupIdle } = await import('./renderCleanup.js');+ assertRenderCleanupIdle(projectId);const existingJob = projectRenders.get(projectId);if (existingJob && (existingJob === PENDING || jobs.has(existingJob))) {throw new ServerError('Render already in progress for this project', {@@ -1356,6 +1362,7 @@ async function renderAdmittedMusicVideo(projectId, options, permit) {});return poster;});+ await retireOldFinals(projectId, jobId);emitRenderSettled(projectId, job);console.log(`✅ Music video rendered [${jobId.slice(0, 8)}]: ${filename}`);broadcastSse(job, { type: 'complete', result: { id: jobId, filename, thumbnail: thumb, path: `/data/videos/${filename}` } });@@ -1481,3 +1488,8 @@ function scheduleRenderResumes(resumes) {}}, RENDER_RESUME_DELAY_MS).unref?.();}++async function retireOldFinals(projectId, replacementId) {+ await import('./renderCleanup.js').then(({ cleanupSupersededFinals }) => cleanupSupersededFinals(projectId, { replacementId }))+ .catch(err => console.warn(`⚠️ Music-video superseded render cleanup failed: ${err.message}`));+}diff --git a/server/services/musicVideo/renderCleanup.js b/server/services/musicVideo/renderCleanup.jsnew file mode 100644index 0000000000..fa8353df54--- /dev/null+++ b/server/services/musicVideo/renderCleanup.js@@ -0,0 +1,104 @@+/** Retire regenerable renders without touching source takes or the cutting-room floor. */+import { unlink, stat } from 'node:fs/promises';+import { PATHS } from '../../lib/fileUtils.js';+import { safeUnder } from '../../lib/ffmpeg.js';+import { ServerError } from '../../lib/errorHandler.js';+import { withBackupAssetPublication } from '../../lib/backupSnapshotBoundary.js';+import { getProject, listProjects, withProjectMediaMutation } from './projects.js';+import { musicVideoEvents } from './events.js';+import { loadHistory, mutateVideoHistory, HISTORY_UNCHANGED } from '../videoGen/history.js';++const cleaning = new Set();+export function assertRenderCleanupIdle(projectId) {+ if (cleaning.has(projectId)) throw new ServerError('Render cleanup is in progress. Try again when it finishes.', { status: 409, code: 'RENDER_CLEANUP_ACTIVE' });+}++// Include legacy and nested references (takes, proof, kits, clone provenance).+const references = (value, ids) => typeof value === 'string' ? ids.has(value)+ : value && typeof value === 'object' && Object.values(value).some(child => references(child, ids));+const size = async path => path ? stat(path).then(info => info.size, error => {+ if (error.code === 'ENOENT') return 0;+ throw error;+}) : 0;+const remove = async path => {+ if (!path) return;+ await unlink(path).catch(error => { if (error.code !== 'ENOENT') throw error; });+};+const renderFiles = entry => [safeUnder(PATHS.videos, entry.filename),+ safeUnder(PATHS.videoThumbnails, entry.thumbnail),+ safeUnder(PATHS.videoThumbnails, entry.filename?.replace(/\.mp4$/, '-proof.png')),+ ...Array.from({ length: 9 }, (_, i) => safeUnder(PATHS.videoThumbnails, `${entry.id}-f${i + 1}.jpg`)),+].filter(Boolean);++async function finalInventory(projectId, history) {+ const project = await getProject(projectId);+ if (!project) throw new ServerError('Project not found', { status: 404, code: 'NOT_FOUND' });+ const [{ listCollections }, timeline, director] = await Promise.all([+ import('../mediaCollections.js'), import('../videoTimeline/local.js'), import('../creativeDirector/local.js'),+ ]);+ const owners = (await Promise.all([listProjects(), listCollections(), timeline.listProjects(), director.listProjects()])).flat();+ const current = history.find(entry => entry.id === project.renderHistoryId);+ const hasCurrent = current && await size(safeUnder(PATHS.videos, current.filename)) > 0;+ const finals = [];+ for (const entry of history) {+ // Only the full-render pipeline owns this tag. Source clips use musicVideo.projectId.+ if (entry.musicVideoProjectId !== projectId || !['music-video', 'music-video-code', 'music-video-document', 'music-video-eidoverse'].includes(entry.modelId) || entry.id === project.renderHistoryId) continue;+ const ids = new Set([entry.id, entry.filename]);+ const referenced = owners.some(owner => references(owner, ids))+ || history.some(other => other !== entry && references(other, ids));+ const reason = !hasCurrent ? 'No completed replacement' : referenced ? 'Referenced by saved media or production records' : null;+ finals.push({ id: entry.id, filename: entry.filename, createdAt: entry.createdAt, bytes: (await Promise.all(renderFiles(entry).map(size))).reduce((a, b) => a + b, 0), reason });+ }+ return { project, finals };+}++/** Called only after the replacement has been published. Failed writes keep old renders. */+export async function cleanupSupersededFinals(projectId, { replacementId = null } = {}) {+ return withBackupAssetPublication(() => withProjectMediaMutation(async () => {+ const deleted = [];+ await mutateVideoHistory(async history => {+ const { project, finals } = await finalInventory(projectId, history);+ if (replacementId && project.renderHistoryId !== replacementId) return HISTORY_UNCHANGED;+ const eligible = new Set(finals.filter(entry => !entry.reason).map(entry => entry.id));+ const retainedFiles = new Set(history.filter(entry => !eligible.has(entry.id)).flatMap(renderFiles));+ for (const entry of history) {+ if (!eligible.has(entry.id)) continue;+ if (!safeUnder(PATHS.videos, entry.filename)) throw new ServerError('Invalid render filename', { status: 409, code: 'INVALID_VIDEO_PATH' });+ for (const path of renderFiles(entry)) if (!retainedFiles.has(path)) await remove(path);+ deleted.push(entry.id);+ }+ return deleted.length ? history.filter(entry => !eligible.has(entry.id)) : HISTORY_UNCHANGED;+ });+ if (deleted.length) {+ const { unindexVideo } = await import('../mediaAssetIndex/index.js');+ for (const id of deleted) await unindexVideo(id);+ }+ if (deleted.length) musicVideoEvents.emit('render-cleanup', { projectId });+ return deleted.length;+ }));+}++export async function getRenderCleanup(projectId) {+ const { finals } = await finalInventory(projectId, await loadHistory());+ const { getSegmentCleanup } = await import('./shotSegments.js');+ return { finals, segments: await getSegmentCleanup(projectId) };+}++export async function withRenderCleanupReservation(projectId, work) {+ assertRenderCleanupIdle(projectId);+ cleaning.add(projectId);+ try { return await work(); } finally { cleaning.delete(projectId); }+}++export async function cleanupRenders(projectId, { unusedLooks = false } = {}) {+ return withRenderCleanupReservation(projectId, async () => {+ const { isProjectRenderActive } = await import('./render.js');+ const { cleanupShotSegments, assertSegmentCleanupIdle } = await import('./shotSegments.js');+ if (isProjectRenderActive(projectId)) throw new ServerError('Wait for the final render to finish before cleanup.', { status: 409, code: 'RENDER_IN_PROGRESS' });+ assertSegmentCleanupIdle(projectId);+ await cleanupSupersededFinals(projectId);+ await cleanupShotSegments(projectId, { unusedLooks, reserved: true });+ musicVideoEvents.emit('render-cleanup', { projectId });+ return getRenderCleanup(projectId);+ });+}diff --git a/server/services/musicVideo/renderCleanup.test.js b/server/services/musicVideo/renderCleanup.test.jsnew file mode 100644index 0000000000..2321c1fae8--- /dev/null+++ b/server/services/musicVideo/renderCleanup.test.js@@ -0,0 +1,104 @@+import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';+import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';+import { join } from 'node:path';+import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../../lib/mockPathsDataRoot.js';++const root = () => lazyTempDataRoot('mv-render-cleanup-');+vi.mock('../../lib/paths.js', async load => makePathsProxy(await load(), { dataRoot: root }));+const state = { project: null, others: [], collections: [], timelines: [], director: [], active: false };+vi.mock('./projects.js', () => ({+ getProject: vi.fn(async () => state.project), listProjects: vi.fn(async () => [state.project, ...state.others]),+ withProjectMediaMutation: async work => work({}),+}));+vi.mock('../mediaCollections.js', () => ({ listCollections: async () => state.collections }));+vi.mock('../videoTimeline/local.js', () => ({ listProjects: async () => state.timelines }));+vi.mock('../creativeDirector/local.js', () => ({ listProjects: async () => state.director }));+vi.mock('../mediaAssetIndex/index.js', () => ({ unindexVideo: vi.fn(async () => {}) }));+vi.mock('./render.js', () => ({ isProjectRenderActive: () => state.active }));+vi.mock('./shotSegments.js', () => ({+ assertSegmentCleanupIdle: vi.fn(), cleanupShotSegments: vi.fn(async () => {}),+ getSegmentCleanup: async () => ({ stale: [], unusedLooks: [] }),+}));++const { cleanupSupersededFinals, cleanupRenders, getRenderCleanup, withRenderCleanupReservation, assertRenderCleanupIdle } = await import('./renderCleanup.js');+const { PATHS } = await import('../../lib/fileUtils.js');+const { saveHistory, loadHistory } = await import('../videoGen/history.js');+const { unindexVideo } = await import('../mediaAssetIndex/index.js');+const { assertSegmentCleanupIdle } = await import('./shotSegments.js');+afterAll(cleanupTempDataRoots);+beforeEach(async () => {+ rmSync(root(), { recursive: true, force: true });+ mkdirSync(PATHS.videos, { recursive: true });+ mkdirSync(PATHS.videoThumbnails, { recursive: true });+ state.project = { id: 'example-project', renderHistoryId: 'current' };+ state.others = []; state.collections = []; state.timelines = []; state.director = []; state.active = false;+ vi.clearAllMocks();+ assertSegmentCleanupIdle.mockReset();+ await saveHistory([]);+});+const entry = (id, over = {}) => ({ id, filename: `${id}.mp4`, thumbnail: `${id}.jpg`, modelId: 'music-video', musicVideoProjectId: 'example-project', ...over });+async function seed(entries) {+ for (const item of entries) {+ writeFileSync(join(PATHS.videos, item.filename), 'video');+ writeFileSync(join(PATHS.videoThumbnails, item.thumbnail), 'poster');+ }+ await saveHistory(entries);+}++describe('derived render retention', () => {+ it('removes superseded composites, posters, proof sheets and index rows, preserving sources and referenced renders', async () => {+ const ids = ['current', 'old', 'take', 'clone', 'collection', 'timeline', 'director', 'derived'];+ await seed([...ids.map(id => entry(id)), entry('source', { modelId: 'video-model' }), entry('other', { musicVideoProjectId: 'another-project' }), entry('child', { sourceId: 'derived', musicVideoProjectId: null })]);+ state.project.scenes = [{ takes: [{ assetId: 'take' }] }];+ state.others = [{ revision: { sourceRenderId: 'clone' } }];+ state.collections = [{ items: [{ ref: 'collection' }] }];+ state.timelines = [{ clips: [{ videoHistoryId: 'timeline' }] }];+ state.director = [{ finalVideoId: 'director' }];+ writeFileSync(join(PATHS.videoThumbnails, 'old-proof.png'), 'proof');+ const inventory = await getRenderCleanup('example-project');+ expect(inventory.finals.filter(item => !item.reason).map(item => item.id)).toEqual(['old']);+ expect(await cleanupSupersededFinals('example-project', { replacementId: 'current' })).toBe(1);+ expect((await loadHistory()).map(item => item.id)).not.toContain('old');+ expect(unindexVideo).toHaveBeenCalledWith('old');+ for (const file of ['old.mp4']) expect(existsSync(join(PATHS.videos, file))).toBe(false);+ for (const file of ['old.jpg', 'old-proof.png']) expect(existsSync(join(PATHS.videoThumbnails, file))).toBe(false);+ for (const id of [...ids.filter(id => id !== 'old'), 'source', 'other']) expect(existsSync(join(PATHS.videos, `${id}.mp4`))).toBe(true);+ });++ it('keeps the previous final when its replacement failed to become the current render, or its file is missing', async () => {+ await seed([entry('current'), entry('new')]);+ expect(await cleanupSupersededFinals('example-project', { replacementId: 'new' })).toBe(0);+ expect(existsSync(join(PATHS.videos, 'new.mp4'))).toBe(true);+ state.project.renderHistoryId = 'new';+ rmSync(join(PATHS.videos, 'new.mp4'));+ expect(await cleanupSupersededFinals('example-project', { replacementId: 'new' })).toBe(0);+ expect(existsSync(join(PATHS.videos, 'current.mp4'))).toBe(true);+ });++ it('refuses cleanup while renders are active and fences new starts until cleanup settles', async () => {+ await seed([entry('current'), entry('old')]);+ state.active = true;+ await expect(cleanupRenders('example-project')).rejects.toMatchObject({ code: 'RENDER_IN_PROGRESS' });+ state.active = false;+ assertSegmentCleanupIdle.mockImplementationOnce(() => { throw Object.assign(new Error('busy'), { code: 'SHOT_RENDER_ACTIVE' }); });+ await expect(cleanupRenders('example-project')).rejects.toMatchObject({ code: 'SHOT_RENDER_ACTIVE' });+ expect(existsSync(join(PATHS.videos, 'old.mp4'))).toBe(true);+ await withRenderCleanupReservation('example-project', async () => {+ expect(() => assertRenderCleanupIdle('example-project')).toThrow(/cleanup is in progress/);+ await expect(cleanupRenders('example-project')).rejects.toMatchObject({ code: 'RENDER_CLEANUP_ACTIVE' });+ });+ expect(() => assertRenderCleanupIdle('example-project')).not.toThrow();+ });++ it('reports deletion and unreadable-history failures without claiming cleanup or deleting the current final', async () => {+ await seed([entry('current'), entry('old')]);+ rmSync(join(PATHS.videoThumbnails, 'old.jpg'));+ mkdirSync(join(PATHS.videoThumbnails, 'old.jpg'));+ await expect(cleanupSupersededFinals('example-project')).rejects.toThrow();+ expect((await loadHistory()).map(item => item.id)).toContain('old');+ expect(existsSync(join(PATHS.videos, 'current.mp4'))).toBe(true);+ writeFileSync(join(PATHS.data, 'video-history.json'), '{invalid');+ await expect(cleanupSupersededFinals('example-project')).rejects.toThrow();+ expect(existsSync(join(PATHS.videos, 'current.mp4'))).toBe(true);+ });+});diff --git a/server/services/musicVideo/shotSegments.js b/server/services/musicVideo/shotSegments.jsindex 5902df955e..decbb39801 100644--- a/server/services/musicVideo/shotSegments.js+++ b/server/services/musicVideo/shotSegments.js@@ -23,8 +23,9 @@ import { createHash, randomUUID } from 'crypto';import { existsSync } from 'fs';import { copyFile, mkdir, readdir, readFile, rename, rm, stat, writeFile } from 'fs/promises';import { availableParallelism, tmpdir, totalmem } from 'os';-import { join } from 'path';-import { PATHS, safeJSONParse } from '../../lib/fileUtils.js';+import { join, sep } from 'path';+import { PATHS, safeJSONParse, atomicWrite, readJSONFile } from '../../lib/fileUtils.js';+import { createKeyCachedQueue } from '../../lib/createKeyCachedQueue.js';import { ServerError } from '../../lib/errorHandler.js';import { AAC_ENCODE_ARGS, runFfmpegProcess, supportsSetparamsFilter } from '../../lib/ffmpeg.js';import { filterGraphViaFile } from '../../lib/ffmpegFilterGraph.js';@@ -39,6 +40,7 @@ import { applyFilmLookFinish } from './filmLookFinish.js';import { getMusicVideoOutroStats, renderStatsOutroClip } from './statsOutro.js';import { nameShots } from './productionReview.js';import { getProject } from './projects.js';+import { assertRenderCleanupIdle, withRenderCleanupReservation } from './renderCleanup.js';import { musicVideoEvents } from './events.js';import { maintenance } from '../../lib/maintenanceAdmission.js';import { trackedRenderMap } from '../renderActivity.js';@@ -46,6 +48,18 @@ import { trackedRenderMap } from '../renderActivity.js';export const SHOT_SEGMENT_DIR = 'music-video-segments';// Bump when a segment's encode changes in a way its key cannot see.export const SHOT_SEGMENT_VERSION = 1;+const segmentOwnerWrites = createKeyCachedQueue();+const segmentOwnersFile = dir => join(dir, 'segments.json');++async function rememberSegmentOwner(plan, segment) {+ if (!segment.baseKey) return;+ await segmentOwnerWrites(plan.dir, async () => {+ const owners = await readJSONFile(segmentOwnersFile(plan.dir), {}, { strict: true });+ if (owners[segment.baseKey]?.sceneId === segment.sceneId) return;+ owners[segment.baseKey] = { sceneId: segment.sceneId || null };+ await atomicWrite(segmentOwnersFile(plan.dir), owners);+ });+}// Timed text this far outside a segment is left out of its key: a lyric line// or cue edit elsewhere in the song does not re-render it.const KEY_MARGIN_SEC = 5;@@ -267,6 +281,7 @@ async function renderSegmentFiles(plan, segment, { signal, onProgress }) {throw new ServerError(`${label(plan, segment)}: film look failed: ${err.message}`, { status: err.status || 500, code: err.code || 'SHOT_LOOK_FAILED' });}}+ await rememberSegmentOwner(plan, segment);onProgress?.(1);return segment.path;}@@ -406,36 +421,83 @@ export async function stitchSegments(plan, outputPath, { signal, segments = [...}}-/**- * Remove cached files the current plan no longer uses: older takes of each- * shot, with every look rendered over them. Each listed video look over a- * current take stays, so a look can be restored without rendering- * (deleteFilmLook drops one); a look that isn't listed could never be found- * again, so its files go. The rendered-so-far previews are replaced by their- * own builder. A file written at or after `startedAtMs` (a shot rendered while- * the final render ran) was made after this plan was cut, so the plan cannot- * tell whether it is current and it stays.+/** Keep current bases and film-look comparisons; retire stale derived MP4s only.+ * A final render's time floor preserves files created after its snapshot.+ * Shot replacement uses the cache's owner journal to leave other shots alone;+ * legacy caches without that journal are handled by full/explicit cleanup.*/-export async function pruneSegments(plan, { startedAtMs = Infinity } = {}) {+async function staleSegmentFiles(plan, { startedAtMs = Infinity, unusedLooks = false, previews = false, sceneId = null } = {}) {const keep = new Set([...plan.segments, ...(plan.outro ? [plan.outro] : [])].flatMap((s) => [s.path, s.basePath]));- // Only takes that play the video's look keep its other looks; a shot's own- // look keeps just its current file, as before.- await lookWrites.get(plan.dir);- const listed = Object.values(await readFilmLooks(plan.dir)).map((entry) => entry?.look).filter(Boolean);- for (const segment of videoLookSegments(plan)) {- for (const look of listed) keep.add(segmentLookPath(plan, segment, look));- }- const names = await readdir(plan.dir).catch(() => []);- let removed = 0;+ const owners = sceneId ? await readJSONFile(segmentOwnersFile(plan.dir), {}, { strict: true }) : null;+ const names = await readdir(plan.dir).catch(error => { if (error.code === 'ENOENT') return []; throw error; });+ const stale = [];for (const name of names) {const path = join(plan.dir, name);- if (keep.has(path) || [...inFlight.keys()].some((p) => path.startsWith(p))) continue;- if (name.endsWith('.partial.mp4') || name.startsWith(LOOKS_FILE) || name.startsWith('preview-')) continue;- const info = await stat(path).catch(() => null);- if (!info || info.mtimeMs >= startedAtMs) continue;- await rm(path, { force: true }).then(() => { removed += 1; }, () => {});+ if (owners && owners[name.replace(/^outro-/, '').slice(0, 32)]?.sceneId !== sceneId) continue;+ // A shot-pinned look is also a useful comparison over this exact base.+ const comparison = !unusedLooks && [...plan.segments, ...(plan.outro ? [plan.outro] : [])]+ .some(segment => name.startsWith(`${segment.outro ? 'outro-' : ''}${segment.baseKey}-`));+ if (comparison || keep.has(path) || [...inFlight.keys()].some((p) => path.startsWith(p))) continue;+ if (!name.endsWith('.mp4') || name.endsWith('.partial.mp4') || (!previews && name.startsWith('preview-'))) continue;+ const info = await stat(path).catch(error => { if (error.code === 'ENOENT') return null; throw error; });+ if (!info || !info.isFile() || info.mtimeMs >= startedAtMs) continue;+ stale.push({ name, bytes: info.size });}- return removed;+ return stale;+}++export async function pruneSegments(plan, { reserved = false, ...options } = {}) {+ const prune = async () => {+ assertSegmentCleanupIdle(plan.project.id);+ const stale = await staleSegmentFiles(plan, options);+ for (const file of stale) await rm(join(plan.dir, file.name), { force: true });+ if (stale.length) await segmentOwnerWrites(plan.dir, async () => {+ const owners = await readJSONFile(segmentOwnersFile(plan.dir), {}, { strict: true });+ const present = new Set((await readdir(plan.dir)).filter(name => name.endsWith('.mp4')).map(name => name.replace(/^outro-/, '').slice(0, 32)));+ const kept = Object.fromEntries(Object.entries(owners).filter(([key]) => present.has(key)));+ if (Object.keys(kept).length !== Object.keys(owners).length) await atomicWrite(segmentOwnersFile(plan.dir), kept);+ });+ if (stale.length) musicVideoEvents.emit('render-cleanup', { projectId: plan.project.id });+ return stale.length;+ };+ return reserved ? prune() : withRenderCleanupReservation(plan.project.id, prune);+}++export function assertSegmentCleanupIdle(projectId) {+ const dir = shotSegmentDir(projectId);+ if ([...inFlight.keys(), ...previewBuilds.keys()].some(path => path.startsWith(`${dir}${sep}`))+ || [...shotJobs.keys()].some(key => key.startsWith(`${projectId}:`))) {+ throw new ServerError('Wait for shot renders and preview builds to finish before cleanup.', { status: 409, code: 'SHOT_RENDER_ACTIVE' });+ }+}++export async function getSegmentCleanup(projectId) {+ const { project, plan: planned } = await planForLooks(projectId);+ const plan = planned || { project, dir: shotSegmentDir(projectId), segments: [], outro: null };+ if (plan.error) return { available: true, error: plan.error, stale: [], unusedLooks: [] };+ const stale = await staleSegmentFiles(plan, { previews: true });+ const staleNames = new Set(stale.map(file => file.name));+ const variants = (await staleSegmentFiles(plan, { previews: true, unusedLooks: true })).filter(file => !staleNames.has(file.name));+ return { available: true, error: null, stale, unusedLooks: variants };+}++export async function cleanupShotSegments(projectId, { unusedLooks = false, reserved = false } = {}) {+ const { withProjectMediaMutation } = await import('./projects.js');+ const cleanup = () => withProjectMediaMutation(async () => {+ assertSegmentCleanupIdle(projectId);+ const { project, plan: planned } = await planForLooks(projectId);+ const plan = planned || { project, dir: shotSegmentDir(projectId), segments: [], outro: null };+ if (plan.error) return;+ await pruneSegments(plan, { unusedLooks, previews: true, reserved: true });+ if (unusedLooks && existsSync(plan.dir)) {+ const current = normalizeFilmLook(project.filmLook);+ const currentId = current && !isFilmLookNeutral(current) ? filmLookId(current) : null;+ await updateFilmLooks(plan.dir, looks => {+ for (const id of Object.keys(looks)) if (id !== currentId) delete looks[id];+ });+ }+ });+ return reserved ? cleanup() : withRenderCleanupReservation(projectId, cleanup);}/** Remove a project's whole segment cache (the project was deleted). */@@ -531,6 +593,7 @@ export async function startShotRender(projectId, sceneId) {}if (segments.every(segmentRendered)) return { sceneId, state: 'rendered' };if (shotJobs.has(key)) return { sceneId, state: 'rendering' };+ assertRenderCleanupIdle(projectId);const controller = new AbortController();shotJobs.set(key, { controller });handedOff = true;@@ -544,6 +607,7 @@ export async function startShotRender(projectId, sceneId) {};report(0);const started = Date.now();+ let rendered = false;Promise.resolve().then(async () => {let before = 0;for (const segment of segments) {@@ -551,6 +615,7 @@ export async function startShotRender(projectId, sceneId) {before += segment.frames;}}).then(() => {+ rendered = true;console.log(`🎬 Music-video ${nameShots(project, [sceneId])} rendered [${projectId.slice(0, 8)}] in ${((Date.now() - started) / 1000).toFixed(1)}s`);emitShotRender({ projectId, sceneId, state: 'rendered', progress: 1 });}, (err) => {@@ -558,8 +623,9 @@ export async function startShotRender(projectId, sceneId) {const log = canceled ? console.log : console.error;log(`${canceled ? '🛑' : '❌'} Music-video shot render ${canceled ? 'cancelled' : 'failed'} [${projectId.slice(0, 8)}]: ${err.message}`);emitShotRender({ projectId, sceneId, state: canceled ? 'canceled' : 'failed', error: canceled ? null : err.message });- }).finally(() => {+ }).finally(async () => {shotJobs.delete(key);+ if (rendered) await cleanupAfterShot(projectId, sceneId).catch(err => console.warn(`⚠️ Music-video shot cache cleanup failed: ${err.message}`));permit.finish();});return { sceneId, state: 'rendering' };@@ -669,6 +735,15 @@ export async function listFilmLooks(projectId) {/** Delete one cached look's files over the current takes, and forget it. */export async function deleteFilmLook(projectId, lookId) {+ return withRenderCleanupReservation(projectId, async () => {+ const { isProjectRenderActive } = await import('./render.js');+ if (isProjectRenderActive(projectId)) throw new ServerError('Wait for the final render to finish before deleting a look.', { status: 409, code: 'RENDER_IN_PROGRESS' });+ assertSegmentCleanupIdle(projectId);+ return deleteFilmLookFiles(projectId, lookId);+ });+}++async function deleteFilmLookFiles(projectId, lookId) {if (typeof lookId !== 'string' || !/^[0-9a-f]{32}$/.test(lookId)) throw new ServerError('Invalid film look id', { status: 400, code: 'VALIDATION_ERROR' });const { project, plan } = await planForLooks(projectId);if (!plan || plan.error) throw new ServerError('This video has no shot renders to delete', { status: 409, code: 'NO_SHOT_RENDERS' });@@ -721,6 +796,7 @@ export async function buildRenderPreview(projectId, { light = false } = {}) {shots, totalShots: new Set(plan.segments.map((s) => s.sceneId)).size,complete: lead.length === plan.segments.length + (plan.outro ? 1 : 0),};+ assertRenderCleanupIdle(projectId);if (existsSync(path)) return result;let build = previewBuilds.get(path);if (!build) {@@ -753,3 +829,18 @@ async function writePreview(plan, lead, path, { light }) {await rm(partial, { force: true }).catch(() => {});}}++async function cleanupAfterShot(projectId, sceneId) {+ const { isProjectRenderActive } = await import('./render.js');+ if (isProjectRenderActive(projectId)) return;+ assertSegmentCleanupIdle(projectId);+ const { withProjectMediaMutation } = await import('./projects.js');+ await withProjectMediaMutation(async () => {+ const { plan } = await planForLooks(projectId);+ if (!plan || plan.error || isProjectRenderActive(projectId)) return;+ const current = plan.segments.filter(segment => segment.sceneId === sceneId);+ if (!current.length || !current.every(segmentRendered)) return;+ assertSegmentCleanupIdle(projectId);+ await pruneSegments(plan, { sceneId });+ });+}diff --git a/server/services/musicVideo/shotSegments.test.js b/server/services/musicVideo/shotSegments.test.jsindex 3e0733ec4a..0a0d582691 100644--- a/server/services/musicVideo/shotSegments.test.js+++ b/server/services/musicVideo/shotSegments.test.js@@ -7,7 +7,7 @@import { describe, it, expect, vi, afterAll, afterEach, beforeAll } from 'vitest';import { execFileSync } from 'child_process';import { existsSync, mkdirSync, rmSync, utimesSync, writeFileSync } from 'fs';-import { join } from 'path';+import { join, basename } from 'path';import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../../lib/mockPathsDataRoot.js';const ROOT = () => lazyTempDataRoot('mv-shot-segments-test-');@@ -28,9 +28,9 @@ vi.mock('./render.js', async (importOriginal) => {// The look pass draws in a browser; here it keeps the base picture, which is// enough to follow which look file each segment lands in.vi.mock('./filmLookFinish.js', () => ({ applyFilmLookFinish: vi.fn(async () => true) }));-vi.mock('./projects.js', () => ({ getProject: vi.fn(async (id) => (id === fixture.project?.id ? fixture.project : null)) }));+vi.mock('./projects.js', () => ({ getProject: vi.fn(async (id) => (id === fixture.project?.id ? fixture.project : null)), withProjectMediaMutation: async work => work({}) }));-const { planShotSegments, renderMissingSegments, renderSegment, segmentConcurrency, stitchSegments, pruneSegments, listFilmLooks, deleteFilmLook, buildRenderPreview, segmentRendering } = await import('./shotSegments.js');+const { planShotSegments, renderMissingSegments, renderSegment, segmentConcurrency, stitchSegments, pruneSegments, listFilmLooks, deleteFilmLook, buildRenderPreview, segmentRendering, cleanupShotSegments, getSegmentCleanup, startShotRender, shotSegmentDir } = await import('./shotSegments.js');const { buildMusicVideoFfmpegArgs } = await import('./render.js');const { musicVideoEvents } = await import('./events.js');const { applyFilmLookFinish } = await import('./filmLookFinish.js');@@ -191,7 +191,7 @@ describe.skipIf(!hasFfmpeg)('shot segments', () => {expect(probe(join(planB.dir, partial.name), 'stream=nb_read_frames')).toContain('24');}, 60000);- it('deletes the look in use from a cache made before looks were listed, and drops a pinned shot\'s old look', async () => {+ it('deletes the look in use from a cache made before looks were listed, and keeps a pinned shot\'s old look for comparison', async () => {fixture.project = project({ filmLook: { preset: 'polaroid', grain: 0.4 } });const plan = await planShotSegments(fixture.project);await renderMissingSegments(plan, { concurrency: 2 });@@ -204,17 +204,47 @@ describe.skipIf(!hasFfmpeg)('shot segments', () => {expect(plan.segments.some((s) => existsSync(s.path))).toBe(false);expect(left.looks.map((l) => l.rendered)).toEqual([0]);- // A shot's own look keeps only its current file once it changes.+ // Changing only a shot's film look keeps its previous effect for comparison.const pin = (grain) => project({ scenes: project().scenes.map((sc) => (sc.sceneId === 'c' ? { ...sc, filmLook: { preset: 'polaroid', grain } } : sc)) });const before = await planShotSegments(pin(0.2));await renderMissingSegments(before, { concurrency: 2 });const after = await planShotSegments(pin(0.5));await renderMissingSegments(after, { concurrency: 2 });await pruneSegments(after);- expect(existsSync(before.segments[2].path)).toBe(false);+ expect(existsSync(before.segments[2].path)).toBe(true);expect(existsSync(after.segments[2].path)).toBe(true);}, 60000);+ it('retires only a successfully replaced shot while another edited shot still needs its replacement', async () => {+ fixture.project = project({ id: 'mv-shot-replacement' });+ const originalClips = fixture.clips;+ const before = await planShotSegments(fixture.project);+ await renderMissingSegments(before, { concurrency: 2 });+ fixture.clips = originalClips.map(clip => ['a', 'b'].includes(clip.sceneId) ? { ...clip, inSec: 0.1, outSec: 1.1 } : clip);+ const after = await planShotSegments(fixture.project);+ let listener;+ const cleaned = new Promise(resolve => {+ listener = event => {+ if (event.projectId !== fixture.project.id) return;+ musicVideoEvents.off('render-cleanup', listener);+ resolve();+ };+ musicVideoEvents.on('render-cleanup', listener);+ });+ try {+ expect(await startShotRender(fixture.project.id, 'a')).toMatchObject({ state: 'rendering' });+ await cleaned;+ expect(existsSync(before.segments[0].path)).toBe(false);+ expect(existsSync(after.segments[0].path)).toBe(true);+ expect(existsSync(before.segments[1].path)).toBe(true);+ expect(existsSync(after.segments[1].path)).toBe(false);+ expect(existsSync(before.segments[2].path)).toBe(true);+ } finally {+ musicVideoEvents.off('render-cleanup', listener);+ fixture.clips = originalClips;+ }+ });+it('tries a shot whose film look failed again before failing the render', async () => {fixture.project = project({ id: 'mv-segments-retry-test', filmLook: { preset: 'vhs', grain: 0.7 } });const plan = await planShotSegments(fixture.project);@@ -243,15 +273,22 @@ describe.skipIf(!hasFfmpeg)('shot segments', () => {await pruneSegments(planB);expect(existsSync(planA.segments[0].path)).toBe(true);- // A shot's own look was never listed, so once the shot is un-pinned its file goes.+ // A shot-pinned look also stays for comparison until explicitly purged.const pinnedScenes = project().scenes.map((sc) => (sc.sceneId === 'c' ? { ...sc, filmLook: { preset: 'polaroid', grain: 0.2 } } : sc));const pinned = await planShotSegments(project({ id, scenes: pinnedScenes, filmLook: fixture.project.filmLook }));await renderSegment(pinned, pinned.segments[2]);expect(pinned.segments[2].path).not.toBe(pinned.segments[2].basePath);const unpinned = await planShotSegments(fixture.project);await pruneSegments(unpinned);+ expect(existsSync(pinned.segments[2].path)).toBe(true);+ expect(existsSync(pinned.segments[2].basePath)).toBe(true);+ const inventory = await getSegmentCleanup(id);+ expect(inventory.unusedLooks.map(file => file.name)).toContain(basename(pinned.segments[2].path));+ await cleanupShotSegments(id, { unusedLooks: true });expect(existsSync(pinned.segments[2].path)).toBe(false);expect(existsSync(pinned.segments[2].basePath)).toBe(true);+ expect(existsSync(planB.segments[0].path)).toBe(true);+ expect(existsSync(planA.segments[0].path)).toBe(false);}, 60000);});@@ -273,6 +310,20 @@ describe.skipIf(!hasFfmpeg)('pruneSegments time floor', () => {});});+describe('cleanup after changing render styles', () => {+ it('reports and removes abandoned shot caches, preserving unfinished files', async () => {+ fixture.project = project({ id: 'mv-abandoned-cache', composition: { mode: 'document' } });+ const dir = shotSegmentDir(fixture.project.id);+ mkdirSync(dir, { recursive: true });+ writeFileSync(join(dir, 'superseded.mp4'), 'cache');+ writeFileSync(join(dir, 'unfinished.partial.mp4'), 'partial');+ expect((await getSegmentCleanup(fixture.project.id)).stale.map(file => file.name)).toEqual(['superseded.mp4']);+ await cleanupShotSegments(fixture.project.id);+ expect(existsSync(join(dir, 'superseded.mp4'))).toBe(false);+ expect(existsSync(join(dir, 'unfinished.partial.mp4'))).toBe(true);+ });+});+describe('renderSegment after a cancel', () => {const deferred = () => { let release; const promise = new Promise((r) => { release = r; }); return { promise, release }; };diff --git a/server/services/socket.js b/server/services/socket.jsindex 86a5fa2571..81ca1aa86d 100644--- a/server/services/socket.js+++ b/server/services/socket.js@@ -346,6 +346,7 @@ const SIMPLE_BRIDGES = [// The release cover art was composed, or its source image was queued or failed.{ emitter: musicVideoEvents, event: 'cover-art', channel: 'music-video:cover-art' },// A shot's cached render segment rendering, rendered, or failed (shotSegments.js).+ { emitter: musicVideoEvents, event: 'render-cleanup', channel: 'music-video:render-cleanup' },{ emitter: musicVideoEvents, event: 'shot-render', channel: 'music-video:shot-render' },];