Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: preserve active render consumers during cleanup

[._.]/ Adam Eivy1f8df255a885
diff --git a/client/src/components/musicVideo/stages/CleanupStage.jsx b/client/src/components/musicVideo/stages/CleanupStage.jsx
index 67a9640ac4..23cb473542 100644
--- a/client/src/components/musicVideo/stages/CleanupStage.jsx
+++ b/client/src/components/musicVideo/stages/CleanupStage.jsx
@@ -1,4 +1,5 @@
import { useEffect, useRef, useState } from 'react';
+import CollapsibleListItem from '../../ui/CollapsibleListItem.jsx';
import { RefreshCw, Trash2 } from 'lucide-react';
import { cleanupMusicVideoRenders, getMusicVideoRenderCleanup } from '../../../services/apiMusicVideo.js';
import socket from '../../../services/socket';
@@ -86,12 +87,7 @@ export default function CleanupStage({ board }) {
<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}` : file.pending ? 'Cleanup incomplete — retry to remove remaining files' : 'Superseded — safe to remove'}</p>
- </li>)}
- </ul>}
+ <CleanupFinalList key={project.id} files={finals} />
</>}
</section>
);
@@ -103,3 +99,25 @@ function InventoryCard({ title, files, noun = 'files' }) {
<p className="mt-1 font-semibold">{formatCount(files.length)} {noun} · {formatBytes(bytes(files))}</p>
</div>;
}
+
+function CleanupFinalList({ files }) {
+ const [rows, setRows] = useState(() => files.map(file => ({ file, removing: false })));
+ useEffect(() => {
+ setRows(previous => {
+ const latest = new Map(files.map(file => [file.id, file]));
+ const known = new Set(previous.map(row => row.file.id));
+ return [...previous.map(row => latest.has(row.file.id)
+ ? { file: latest.get(row.file.id), removing: false } : { ...row, removing: true }),
+ ...files.filter(file => !known.has(file.id)).map(file => ({ file, removing: false }))];
+ });
+ }, [files]);
+ return <div role="list" aria-label="Older final renders" tabIndex={-1}>
+ {rows.map(({ file, removing }) => <CollapsibleListItem key={file.id} removing={removing}
+ onExited={() => setRows(previous => previous.filter(row => row.file.id !== file.id || !row.removing))}>
+ <article role="listitem" 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}` : file.pending ? 'Cleanup incomplete — retry to remove remaining files' : 'Superseded — safe to remove'}</p>
+ </article>
+ </CollapsibleListItem>)}
+ </div>;
+}
diff --git a/client/src/components/musicVideo/stages/CleanupStage.test.jsx b/client/src/components/musicVideo/stages/CleanupStage.test.jsx
index dd3d767855..03b62d9ec2 100644
--- a/client/src/components/musicVideo/stages/CleanupStage.test.jsx
+++ b/client/src/components/musicVideo/stages/CleanupStage.test.jsx
@@ -24,6 +24,8 @@ describe('production cleanup', () => {
fireEvent.click(cleanup);
await waitFor(() => expect(cleanupMusicVideoRenders).toHaveBeenCalledWith('example-project', { unusedLooks: false }, { silent: true }));
await waitFor(() => expect(cleanup).toBeDisabled());
+ await waitFor(() => expect(screen.queryByText('Superseded — safe to remove')).not.toBeInTheDocument());
+ expect(screen.getByRole('list', { name: 'Older final renders' })).toBeInTheDocument();
const purge = screen.getByRole('button', { name: 'Remove superseded renders and unused looks' });
expect(purge).toBeEnabled();
fireEvent.click(purge);
diff --git a/server/services/README.md b/server/services/README.md
index 5d24d726c7..9641e315dd 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/renderCleanupActivity.js` | Synchronous per-project admission between render retirement, shot renders, previews and publishing-kit preparation. |
| `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. |
diff --git a/server/services/musicVideo/publishKit.js b/server/services/musicVideo/publishKit.js
index 5622eaf6b3..0e8d4f0cf7 100644
--- a/server/services/musicVideo/publishKit.js
+++ b/server/services/musicVideo/publishKit.js
@@ -1,3 +1,4 @@
+import { assertRenderCleanupIdle } from './renderCleanupActivity.js';
/**
* Music Video — publishing kit (#9281).
*
@@ -49,6 +50,8 @@ const kitError = (status, code, message, context) => new ServerError(message, {
* reattach. Only once its SSE job exists: during the prerequisite lookup the
* reservation holds the slot but there is nothing to attach to yet.
*/
+export const isPublishKitBuildActive = (projectId) => projectBuilds.has(projectId);
+
export const getActivePublishKitBuild = (projectId) => {
const jobId = projectBuilds.get(projectId);
return jobId && jobs.get(jobId)?.status === 'running' ? { jobId, status: 'running' } : null;
@@ -97,6 +100,7 @@ export async function releaseKitFiles(filenames, keep = []) {
* record carries the result.
*/
export async function startPublishKitBuild(projectId) {
+ assertRenderCleanupIdle(projectId);
if (projectBuilds.has(projectId)) throw kitError(409, 'PUBLISH_KIT_BUILD_IN_PROGRESS', 'A publishing kit build is already running for this project', getActivePublishKitBuild(projectId) || undefined);
const jobId = `mvpk-${randomUUID()}`;
// Reserve synchronously: final-render lookup and ffmpeg probing can overlap
diff --git a/server/services/musicVideo/publishKit.test.js b/server/services/musicVideo/publishKit.test.js
index af44011cd7..fcd9296393 100644
--- a/server/services/musicVideo/publishKit.test.js
+++ b/server/services/musicVideo/publishKit.test.js
@@ -97,6 +97,16 @@ describe('publishing kit build (#9281)', () => {
await expect(kit.startPublishKitBuild(id)).rejects.toMatchObject({ status: 409, code: 'NO_FINAL_RENDER' });
});
+ it('refuses a new build during render retirement and remains usable after cleanup releases', async () => {
+ const { id } = await projects.createProject({ name: 'Example Cleanup' });
+ const { withRenderCleanupReservation } = await import('./renderCleanupActivity.js');
+ await withRenderCleanupReservation(id, async () => {
+ await expect(kit.startPublishKitBuild(id)).rejects.toMatchObject({ code: 'RENDER_CLEANUP_ACTIVE' });
+ expect(kit.isPublishKitBuildActive(id)).toBe(false);
+ });
+ await expect(kit.startPublishKitBuild(id)).rejects.toMatchObject({ code: 'NO_FINAL_RENDER' });
+ });
+
it('reserves a build before async prerequisites and releases the reservation after setup fails', async () => {
const { id } = await projects.createProject({ name: 'Example Concurrent Build' });
await mkdir(PATHS.videos, { recursive: true });
@@ -114,8 +124,10 @@ describe('publishing kit build (#9281)', () => {
releasePrerequisite(null);
// No SSE job exists yet, so nothing is advertised as attachable.
expect(kit.getActivePublishKitBuild(id)).toBeNull();
+ expect(kit.isPublishKitBuildActive(id)).toBe(true);
await expect(overlap).rejects.toMatchObject({ status: 409, code: 'PUBLISH_KIT_BUILD_IN_PROGRESS' });
expect(await firstFailure).toMatchObject({ code: 'FFMPEG_MISSING' });
+ expect(kit.isPublishKitBuildActive(id)).toBe(false);
expect(probe).toHaveBeenCalledTimes(1);
// A failed setup must leave the project available for the next attempt.
await expect(kit.startPublishKitBuild(id)).rejects.toMatchObject({ code: 'FFMPEG_MISSING' });
diff --git a/server/services/musicVideo/render.js b/server/services/musicVideo/render.js
index eb4fbe54f5..4e70510bef 100644
--- a/server/services/musicVideo/render.js
+++ b/server/services/musicVideo/render.js
@@ -1142,7 +1142,7 @@ 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');
+ const { assertRenderCleanupIdle } = await import('./renderCleanupActivity.js');
assertRenderCleanupIdle(projectId);
const existingJob = projectRenders.get(projectId);
if (existingJob && (existingJob === PENDING || jobs.has(existingJob))) {
diff --git a/server/services/musicVideo/renderCleanup.js b/server/services/musicVideo/renderCleanup.js
index ffb6f9bc9a..ef02456560 100644
--- a/server/services/musicVideo/renderCleanup.js
+++ b/server/services/musicVideo/renderCleanup.js
@@ -7,6 +7,8 @@ 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 { withRenderCleanupReservation } from './renderCleanupActivity.js';
+export { assertRenderCleanupIdle, withRenderCleanupReservation } from './renderCleanupActivity.js';
import { musicVideoEvents } from './events.js';
import { loadHistory, mutateVideoHistory, HISTORY_UNCHANGED } from '../videoGen/history.js';
@@ -25,11 +27,6 @@ const savePending = pending => pending.size
? atomicWrite(JOURNAL, { version: 1, entries: [...pending.values()] }) : remove(JOURNAL);
const retirementEntries = (history, pending) => [...new Map([...pending, ...history].map(entry => [entry.id, entry])).values()];
-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) => {
if (typeof value === 'string') {
@@ -56,15 +53,18 @@ const renderFiles = entry => [safeUnder(PATHS.videos, entry.filename),
async function finalInventory(projectId, history, pending = []) {
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([
+ const [{ listCollections }, timeline, director, { isPublishKitBuildActive }, { listJobs }] = await Promise.all([
import('../mediaCollections.js'), import('../videoTimeline/local.js'), import('../creativeDirector/local.js'),
+ import('./publishKit.js'), import('../mediaJobQueue/index.js'),
]);
// Completed run receipts describe history, rather than a live media dependency.
const projects = (await listProjects()).map(({ productionRuns, autonomousRun, ...record }) => ({
...record, productionRuns: productionRuns?.filter(run => run.status !== 'completed'),
autonomousRun: autonomousRun?.status === 'completed' ? null : autonomousRun,
}));
- const owners = [projects, ...(await Promise.all([listCollections(), timeline.listProjects(), director.listProjects()]))].flat();
+ const owners = [projects, listJobs().filter(job => ['queued', 'running'].includes(job.status)),
+ ...(await Promise.all([listCollections(), timeline.listProjects(), director.listProjects()]))].flat();
+ const kitActive = isPublishKitBuildActive(projectId);
const current = history.find(entry => entry.id === project.renderHistoryId);
const hasCurrent = current && await size(safeUnder(PATHS.videos, current.filename)) > 0;
const finals = [];
@@ -80,16 +80,16 @@ async function finalInventory(projectId, history, pending = []) {
}
const referenced = owners.some(owner => references(owner, ids))
|| history.some(other => other.id !== entry.id && references(other, ids));
- const reason = !hasCurrent ? 'No completed replacement' : referenced ? 'Referenced by saved media or production records' : null;
+ const reason = !hasCurrent ? 'No completed replacement' : kitActive ? 'In use by a publishing-kit build' : referenced ? 'Referenced by saved media or production records' : null;
finals.push({ id: entry.id, pending: pendingIds.has(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 } = {}) {
+export async function cleanupSupersededFinals(projectId, { replacementId = null, reserved = false } = {}) {
// One writer owns the shared journal across history commits and file retirement.
- return cleanupWrites(() => withBackupAssetPublication(() => withProjectMediaMutation(async () => {
+ const retire = () => cleanupWrites(() => withBackupAssetPublication(() => withProjectMediaMutation(async () => {
const pending = new Map((await readPending()).map(entry => [entry.id, entry]));
let retiring = [];
let retainedFiles;
@@ -125,6 +125,7 @@ export async function cleanupSupersededFinals(projectId, { replacementId = null
return removed;
})().finally(() => { if (retiring.length) musicVideoEvents.emit('render-cleanup', { projectId }); });
})));
+ return reserved ? retire() : withRenderCleanupReservation(projectId, retire);
}
export async function getRenderCleanup(projectId) {
@@ -134,19 +135,13 @@ export async function getRenderCleanup(projectId) {
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 cleanupSupersededFinals(projectId, { reserved: true });
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.js
index d352f489d6..d1fec06a2e 100644
--- a/server/services/musicVideo/renderCleanup.test.js
+++ b/server/services/musicVideo/renderCleanup.test.js
@@ -5,7 +5,7 @@ import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../../li
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, failHistory: false };
+const state = { project: null, others: [], collections: [], timelines: [], director: [], active: false, failHistory: false, kitActive: false, jobs: [] };
vi.mock('../../lib/fileUtils.js', async load => {
const actual = await load();
return { ...actual, atomicWrite: async (path, data) => {
@@ -13,6 +13,8 @@ vi.mock('../../lib/fileUtils.js', async load => {
return actual.atomicWrite(path, data);
} };
});
+vi.mock('./publishKit.js', () => ({ isPublishKitBuildActive: () => state.kitActive }));
+vi.mock('../mediaJobQueue/index.js', () => ({ listJobs: () => state.jobs }));
vi.mock('./projects.js', () => ({
getProject: vi.fn(async () => state.project), listProjects: vi.fn(async () => [state.project, ...state.others]),
withProjectMediaMutation: async work => work({}),
@@ -38,7 +40,7 @@ beforeEach(async () => {
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; state.failHistory = false;
+ state.others = []; state.collections = []; state.timelines = []; state.director = []; state.active = false; state.failHistory = false; state.kitActive = false; state.jobs = [];
vi.clearAllMocks();
assertSegmentCleanupIdle.mockReset();
await saveHistory([]);
@@ -88,6 +90,19 @@ describe('derived render retention', () => {
expect(existsSync(join(PATHS.videos, 'unused.mp4'))).toBe(false);
});
+ it('retains finals consumed by active publishing-kit builds and queued media jobs', async () => {
+ await seed([entry('current'), entry('old')]);
+ state.kitActive = true;
+ expect(await cleanupSupersededFinals('example-project', { replacementId: 'current' })).toBe(0);
+ expect((await getRenderCleanup('example-project')).finals[0].reason).toMatch(/publishing-kit/);
+ state.kitActive = false;
+ state.jobs = [{ status: 'queued', params: { sourceId: 'old' } }];
+ expect(await cleanupSupersededFinals('example-project')).toBe(0);
+ expect(existsSync(join(PATHS.videos, 'old.mp4'))).toBe(true);
+ state.jobs = [];
+ expect(await cleanupSupersededFinals('example-project')).toBe(1);
+ });
+
it('keeps history and bytes retryable when index removal fails, then settles them on retry', async () => {
await seed([entry('current'), entry('old')]);
unindexVideo.mockRejectedValueOnce(new Error('Index temporarily unavailable'));
diff --git a/server/services/musicVideo/renderCleanupActivity.js b/server/services/musicVideo/renderCleanupActivity.js
new file mode 100644
index 0000000000..41caa75f32
--- /dev/null
+++ b/server/services/musicVideo/renderCleanupActivity.js
@@ -0,0 +1,14 @@
+/** Synchronous admission shared by derived-render cleanup and its consumers. */
+import { ServerError } from '../../lib/errorHandler.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' });
+}
+
+export async function withRenderCleanupReservation(projectId, work) {
+ assertRenderCleanupIdle(projectId);
+ cleaning.add(projectId);
+ try { return await work(); } finally { cleaning.delete(projectId); }
+}
+
diff --git a/server/services/musicVideo/shotSegments.js b/server/services/musicVideo/shotSegments.js
index e832801375..a0ad705e21 100644
--- a/server/services/musicVideo/shotSegments.js
+++ b/server/services/musicVideo/shotSegments.js
@@ -40,7 +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 { assertRenderCleanupIdle, withRenderCleanupReservation } from './renderCleanupActivity.js';
import { musicVideoEvents } from './events.js';
import { maintenance } from '../../lib/maintenanceAdmission.js';
import { trackedRenderMap } from '../renderActivity.js';