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: serialize music-video file-store mutations (#8965)

[._.]/ Adam Eivya8724853b075
diff --git a/server/services/musicVideo/projectsFile.js b/server/services/musicVideo/projectsFile.js
index c57d7b6031..9443ddfac1 100644
--- a/server/services/musicVideo/projectsFile.js
+++ b/server/services/musicVideo/projectsFile.js
@@ -5,6 +5,7 @@ import { randomUUID } from 'crypto';
import { PATHS } from '../../lib/fileUtils.js';
import { ServerError } from '../../lib/errorHandler.js';
import { createProjectFileStore } from '../projectFileStore.js';
+import { createFileWriteQueue } from '../../lib/fileWriteQueue.js';
import * as logic from './projectsLogic.js';
import * as takes from './takes.js';
@@ -18,12 +19,24 @@ const store = createProjectFileStore({
});
export const {
- loadAll, saveAll, loadAllAndIndex, listProjects, getProject, getProjectsByIds,
- listProjectIds, createProject, updateProject, deleteProject, mergeProjectsFromSync,
- pruneTombstonedProjects,
+ loadAll, saveAll, loadAllAndIndex, listProjects, getProject, getProjectsByIds, listProjectIds,
} = store;
-export async function cloneProject(id, options = {}) {
+// Every mutator below is a load → modify → save of the ONE projects file, so
+// they all share a single write tail: a render-completion hook appending a take
+// can't interleave with a director's select/reject (or any other edit) and
+// persist a stale pre-image over it. The PG backend gets the same guarantee
+// from its row lock (projectsDB.js withLockedProject).
+const queueWrite = createFileWriteQueue();
+const serialized = (fn) => (...args) => queueWrite(() => fn(...args));
+
+export const createProject = serialized(store.createProject);
+export const updateProject = serialized(store.updateProject);
+export const deleteProject = serialized(store.deleteProject);
+export const mergeProjectsFromSync = serialized(store.mergeProjectsFromSync);
+export const pruneTombstonedProjects = serialized(store.pruneTombstonedProjects);
+
+async function cloneProjectUnqueued(id, options = {}) {
const all = await loadAll();
const source = all.find((project) => project.id === id && !project.deleted);
if (!source) throw new ServerError('Project not found', { status: 404, code: 'NOT_FOUND' });
@@ -37,21 +50,21 @@ export async function cloneProject(id, options = {}) {
return clone;
}
-export async function setProjectAnalysis(id, analysis) {
+async function setProjectAnalysisUnqueued(id, analysis) {
const { all, idx } = await loadAllAndIndex(id);
all[idx] = logic.setAudioAnalysis(all[idx], analysis);
await saveAll(all);
return all[idx];
}
-export async function setProjectMidiTranscription(id, midi) {
+async function setProjectMidiTranscriptionUnqueued(id, midi) {
const { all, idx } = await loadAllAndIndex(id);
all[idx] = logic.setMidiTranscription(all[idx], midi);
await saveAll(all);
return all[idx];
}
-export async function addProjectScene(id, sceneInput) {
+async function addProjectSceneUnqueued(id, sceneInput) {
const { all, idx } = await loadAllAndIndex(id);
const { project, scene } = logic.addScene(all[idx], sceneInput);
all[idx] = project;
@@ -59,7 +72,7 @@ export async function addProjectScene(id, sceneInput) {
return scene;
}
-export async function addProjectScenes(id, sceneInputs) {
+async function addProjectScenesUnqueued(id, sceneInputs) {
const { all, idx } = await loadAllAndIndex(id);
const { project, scenes } = logic.addScenes(all[idx], sceneInputs);
all[idx] = project;
@@ -67,7 +80,7 @@ export async function addProjectScenes(id, sceneInputs) {
return { project, scenes };
}
-export async function updateScene(id, sceneId, patch) {
+async function updateSceneUnqueued(id, sceneId, patch) {
const { all, idx } = await loadAllAndIndex(id);
const { project, updated } = logic.applySceneUpdate(all[idx], sceneId, patch);
all[idx] = project;
@@ -75,14 +88,14 @@ export async function updateScene(id, sceneId, patch) {
return updated;
}
-export async function deleteScene(id, sceneId) {
+async function deleteSceneUnqueued(id, sceneId) {
const { all, idx } = await loadAllAndIndex(id);
all[idx] = logic.removeScene(all[idx], sceneId);
await saveAll(all);
return all[idx];
}
-export async function reorderProjectScenes(id, orderedIds) {
+async function reorderProjectScenesUnqueued(id, orderedIds) {
const { all, idx } = await loadAllAndIndex(id);
all[idx] = logic.reorderScenes(all[idx], orderedIds);
await saveAll(all);
@@ -98,21 +111,34 @@ async function mutateProject(id, transform) {
return outcome;
}
-export async function appendSceneTakes(id, sceneId, inputs) {
+async function appendSceneTakesUnqueued(id, sceneId, inputs) {
const { scene, appended } = await mutateProject(id, (p) => takes.appendSceneTakes(p, sceneId, inputs));
return { scene, appended };
}
-export async function appendTakesAcrossScenes(id, items) {
+async function appendTakesAcrossScenesUnqueued(id, items) {
return mutateProject(id, (p) => takes.appendTakesAcrossScenes(p, items));
}
-export async function selectSceneTake(id, sceneId, takeId) {
+async function selectSceneTakeUnqueued(id, sceneId, takeId) {
const { scene } = await mutateProject(id, (p) => takes.selectSceneTake(p, sceneId, takeId));
return scene;
}
-export async function reviewSceneTake(id, sceneId, takeId, review) {
+async function reviewSceneTakeUnqueued(id, sceneId, takeId, review) {
const { scene } = await mutateProject(id, (p) => takes.reviewSceneTake(p, sceneId, takeId, review));
return scene;
}
+
+export const cloneProject = serialized(cloneProjectUnqueued);
+export const setProjectAnalysis = serialized(setProjectAnalysisUnqueued);
+export const setProjectMidiTranscription = serialized(setProjectMidiTranscriptionUnqueued);
+export const addProjectScene = serialized(addProjectSceneUnqueued);
+export const addProjectScenes = serialized(addProjectScenesUnqueued);
+export const updateScene = serialized(updateSceneUnqueued);
+export const deleteScene = serialized(deleteSceneUnqueued);
+export const reorderProjectScenes = serialized(reorderProjectScenesUnqueued);
+export const appendSceneTakes = serialized(appendSceneTakesUnqueued);
+export const appendTakesAcrossScenes = serialized(appendTakesAcrossScenesUnqueued);
+export const selectSceneTake = serialized(selectSceneTakeUnqueued);
+export const reviewSceneTake = serialized(reviewSceneTakeUnqueued);
diff --git a/server/services/musicVideoSceneHooks.test.js b/server/services/musicVideoSceneHooks.test.js
index eda2d25060..100a7d85cc 100644
--- a/server/services/musicVideoSceneHooks.test.js
+++ b/server/services/musicVideoSceneHooks.test.js
@@ -115,6 +115,24 @@ describe('music-video scene completion hooks → takes', () => {
expect(stored.takes.map((t) => t.assetId)).toEqual(['first.png', 'second.png', 'third.png']);
});
+ it('a completion racing a director selection loses neither (one write tail on the file store)', async () => {
+ mediaJobEvents.emit('completed', imageJob(project.id, scene.sceneId, 'one.png'));
+ mediaJobEvents.emit('completed', imageJob(project.id, scene.sceneId, 'two.png'));
+ await waitFor(() => emitted.length === 2);
+ const two = (await sceneOf(project.id, scene.sceneId)).takes.find((t) => t.assetId === 'two.png');
+ // Same tick: a third render lands while the director picks take two.
+ mediaJobEvents.emit('completed', imageJob(project.id, scene.sceneId, 'three.png'));
+ await Promise.all([
+ projects.selectSceneTake(project.id, scene.sceneId, two.takeId),
+ projects.updateScene(project.id, scene.sceneId, { prompt: 'edited while rendering' }),
+ ]);
+ await waitFor(() => emitted.length === 3);
+ const stored = await sceneOf(project.id, scene.sceneId);
+ expect(stored.referenceImageId).toBe('two.png');
+ expect(stored.prompt).toBe('edited while rendering');
+ expect(stored.takes.map((t) => t.assetId)).toEqual(['one.png', 'two.png', 'three.png']);
+ });
+
it('records a clip take with its source frame as a basename, filling only an empty video slot', async () => {
mediaJobEvents.emit('completed', videoJob(project.id, scene.sceneId, 'clip-1'));
mediaJobEvents.emit('completed', videoJob(project.id, scene.sceneId, 'clip-2'));