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: enforce video shot locks within serialized scene writes

[._.]/ Adam Eivy02e4921f660a
diff --git a/server/services/musicVideo/projects.js b/server/services/musicVideo/projects.js
index 04f8431bb2..8930a724fe 100644
--- a/server/services/musicVideo/projects.js
+++ b/server/services/musicVideo/projects.js
@@ -251,29 +251,23 @@ export async function addProjectScenes(id, sceneInputs, { source = null } = {})
}, { source });
}
-/**
- * A shot whose video is locked keeps what it shows: refuse a change to it
- * (`patch` names the fields a scene edit changes; null replaces the shot).
- */
-async function assertClipsUnlocked(id, sceneIds, patch = null) {
- const project = await getProject(id);
- if (!project?.productionReview?.clipApprovals) return;
- const { assertClipUnlocked } = await import('./productionReview.js');
- for (const sceneId of sceneIds) assertClipUnlocked(project, sceneId, patch);
-}
-
+/** Edit against the fresh record so a concurrent video lock cannot be bypassed. */
export async function updateScene(id, sceneId, patch) {
- await assertClipsUnlocked(id, [sceneId], patch);
- const result = await (await selectBackend()).updateScene(id, sceneId, patch);
- emitRecordUpdated('musicVideoProject', id);
- return result;
+ const [{ applySceneUpdate }, { assertClipUnlocked }] = await Promise.all([import('./projectsLogic.js'), import('./productionReview.js')]);
+ const { updated } = await mutateProjectRecord(id, (current) => {
+ assertClipUnlocked(current, sceneId, patch);
+ return applySceneUpdate(current, sceneId, patch);
+ });
+ return updated;
}
export async function deleteScene(id, sceneId) {
- await assertClipsUnlocked(id, [sceneId]);
- const next = await (await selectBackend()).deleteScene(id, sceneId);
- emitRecordUpdated('musicVideoProject', id);
- return next;
+ const [{ removeScene }, { assertClipUnlocked }] = await Promise.all([import('./projectsLogic.js'), import('./productionReview.js')]);
+ const { project } = await mutateProjectRecord(id, (current) => {
+ assertClipUnlocked(current, sceneId);
+ return { project: removeScene(current, sceneId) };
+ });
+ return project;
}
export async function reorderProjectScenes(id, orderedIds) {
@@ -288,9 +282,9 @@ export async function reorderProjectScenes(id, orderedIds) {
* two at `options.atSec`. A locked still stays locked on every piece. Returns `{ project, scenes }` — the persisted project and the pieces.
*/
export async function splitProjectScene(id, sceneId, options = {}) {
- await assertClipsUnlocked(id, [sceneId]);
- const [{ splitScene }, { carryShotLocks }] = await Promise.all([import('./projectsLogic.js'), import('./productionReview.js')]);
+ const [{ splitScene }, { carryShotLocks, assertClipUnlocked }] = await Promise.all([import('./projectsLogic.js'), import('./productionReview.js')]);
return mutateProjectRecord(id, (current) => {
+ assertClipUnlocked(current, sceneId);
const outcome = splitScene(current, sceneId, options);
const from = Object.fromEntries(outcome.scenes.slice(1).map((piece) => [piece.sceneId, sceneId]));
return { ...outcome, project: carryShotLocks(current, outcome.project, { from }) };
@@ -302,10 +296,14 @@ export async function splitProjectScene(id, sceneId, options = {}) {
* of the split. Returns `{ project, scene }`.
*/
export async function mergeProjectNextScene(id, sceneId, options = {}) {
- const scenes = (await getProject(id))?.scenes || [];
- await assertClipsUnlocked(id, [sceneId, scenes[scenes.findIndex(scene => scene.sceneId === sceneId) + 1]?.sceneId].filter(Boolean));
- const { mergeNextScene } = await import('./projectsLogic.js');
- return mutateProjectRecord(id, (current) => mergeNextScene(current, sceneId, options));
+ const [{ mergeNextScene }, { assertClipUnlocked }] = await Promise.all([import('./projectsLogic.js'), import('./productionReview.js')]);
+ return mutateProjectRecord(id, (current) => {
+ const scenes = current.scenes || [];
+ const index = scenes.findIndex(scene => scene.sceneId === sceneId);
+ assertClipUnlocked(current, sceneId);
+ if (index >= 0 && scenes[index + 1]) assertClipUnlocked(current, scenes[index + 1].sceneId);
+ return mergeNextScene(current, sceneId, options);
+ });
}
// ---- scene takes (#8965) ----
@@ -340,16 +338,22 @@ export async function appendTakesAcrossScenes(id, items) {
/** Explicitly select a take for its scene slot. Returns the updated scene. */
export async function selectSceneTake(id, sceneId, takeId) {
- await assertClipsUnlocked(id, [sceneId]);
- const scene = await (await selectBackend()).selectSceneTake(id, sceneId, takeId);
- emitRecordUpdated('musicVideoProject', id);
+ const [{ selectSceneTake: select }, { assertClipUnlocked }] = await Promise.all([import('./takes.js'), import('./productionReview.js')]);
+ const { scene } = await mutateProjectRecord(id, (current) => {
+ assertClipUnlocked(current, sceneId);
+ return select(current, sceneId, takeId);
+ });
return scene;
}
/** Reject/restore a take and/or set its note. Returns the updated scene. */
export async function reviewSceneTake(id, sceneId, takeId, review) {
- const scene = await (await selectBackend()).reviewSceneTake(id, sceneId, takeId, review);
- emitRecordUpdated('musicVideoProject', id);
+ const [{ reviewSceneTake: reviewTake }, { assertClipUnlocked }] = await Promise.all([import('./takes.js'), import('./productionReview.js')]);
+ const { scene } = await mutateProjectRecord(id, (current) => {
+ const outcome = reviewTake(current, sceneId, takeId, review);
+ assertClipUnlocked(current, sceneId, outcome.scene);
+ return outcome;
+ });
return scene;
}
diff --git a/server/services/musicVideo/projects.test.js b/server/services/musicVideo/projects.test.js
index b03e3fff3e..2dfd7b1551 100644
--- a/server/services/musicVideo/projects.test.js
+++ b/server/services/musicVideo/projects.test.js
@@ -329,3 +329,114 @@ describe('a locked shot video', () => {
expect((await projects.getProject(p.id)).scenes[0].videoHistoryId).toBe('clip-2');
});
});
+
+// Hold a public mutation immediately before the real backend acquires its write
+// queue, allowing another request to commit a video lock first. Both old direct
+// backend methods and the generic transactional path pass through this barrier.
+async function pauseSceneWrite(start) {
+ const backend = await import('./projectsFile.js');
+ let entered;
+ let resume;
+ const reached = new Promise(resolve => { entered = resolve; });
+ const released = new Promise(resolve => { resume = resolve; });
+ const spies = ['mutateProjectRecord', 'updateScene', 'deleteScene', 'selectSceneTake', 'reviewSceneTake'].map(name => {
+ const original = backend[name];
+ return vi.spyOn(backend, name).mockImplementationOnce(async (...args) => {
+ entered();
+ await released;
+ return original(...args);
+ });
+ });
+ const result = start();
+ // Attach immediately: rejected service calls must not become unhandled while
+ // the test is committing the intervening lock.
+ const settled = result.then(value => ({ value }), error => ({ error }));
+ await reached;
+ spies.forEach(spy => spy.mockRestore());
+ return { settled, resume };
+}
+
+describe('video lock transactions', () => {
+ // Each case crosses a distinct dispatcher mutation path. A preflight-only
+ // check lets a completed lock be overwritten after this barrier releases.
+ it.each(['edit', 'delete', 'split', 'select'])('refuses a pending %s when another request locks the video first', async (operation) => {
+ const { setShotLocks } = await import('./productionReview.js');
+ const p = await projects.createProject({ name: 'Concurrent video lock' });
+ const scene = await projects.addProjectScene(p.id, { startSec: 0, endSec: 2, prompt: 'Keep this shot' });
+ const { appended } = await projects.appendSceneTakes(p.id, scene.sceneId, [
+ { kind: 'video', assetId: 'approved-clip' }, { kind: 'video', assetId: 'alternate-clip' },
+ ]);
+ const operations = {
+ edit: () => projects.updateScene(p.id, scene.sceneId, { prompt: 'Pending edit' }),
+ delete: () => projects.deleteScene(p.id, scene.sceneId),
+ split: () => projects.splitProjectScene(p.id, scene.sceneId, { atSec: 1 }),
+ select: () => projects.selectSceneTake(p.id, scene.sceneId, appended[1].takeId),
+ };
+ const pending = await pauseSceneWrite(operations[operation]);
+ let locked;
+ try {
+ locked = (await projects.mutateProjectRecord(p.id, current => ({ project: setShotLocks(
+ setShotLocks(current, { sceneIds: [scene.sceneId], locked: true, stage: 'still' }),
+ { sceneIds: [scene.sceneId], locked: true, stage: 'video' },
+ ) }))).project;
+ } finally {
+ pending.resume();
+ }
+ expect(await pending.settled).toMatchObject({ error: { code: 'MUSIC_VIDEO_SHOT_LOCKED' } });
+ expect((await projects.getProject(p.id)).scenes).toEqual(locked.scenes);
+ await projects.mutateProjectRecord(p.id, current => ({ project: setShotLocks(current, { sceneIds: [scene.sceneId], locked: false, stage: 'video' }) }));
+ await expect(operations[operation]()).resolves.toBeDefined();
+ });
+
+ it('checks the actual merge neighbor after a concurrent reorder and lock', async () => {
+ const { setShotLocks } = await import('./productionReview.js');
+ const p = await projects.createProject({ name: 'Merge neighbor race' });
+ const { scenes: [a, b, c] } = await projects.addProjectScenes(p.id, [
+ { startSec: 0, endSec: 1, prompt: 'First' },
+ { startSec: 1, endSec: 2, prompt: 'Original next' },
+ { startSec: 2, endSec: 3, prompt: 'New next' },
+ ]);
+ await projects.updateScene(p.id, c.sceneId, { videoHistoryId: 'keep-clip' });
+ const pending = await pauseSceneWrite(() => projects.mergeProjectNextScene(p.id, a.sceneId));
+ let locked;
+ try {
+ await projects.reorderProjectScenes(p.id, [a.sceneId, c.sceneId, b.sceneId]);
+ locked = (await projects.mutateProjectRecord(p.id, current => ({ project: setShotLocks(
+ setShotLocks(current, { sceneIds: [c.sceneId], locked: true, stage: 'still' }),
+ { sceneIds: [c.sceneId], locked: true, stage: 'video' },
+ ) }))).project;
+ } finally {
+ pending.resume();
+ }
+ expect(await pending.settled).toMatchObject({ error: { code: 'MUSIC_VIDEO_SHOT_LOCKED', context: { sceneId: c.sceneId } } });
+ expect((await projects.getProject(p.id)).scenes).toEqual(locked.scenes);
+ await projects.mutateProjectRecord(p.id, current => ({ project: setShotLocks(current, { sceneIds: [c.sceneId], locked: false, stage: 'video' }) }));
+ const merged = await projects.mergeProjectNextScene(p.id, a.sceneId);
+ expect(merged.project.scenes.map(scene => scene.sceneId)).toEqual([a.sceneId, b.sceneId]);
+ expect(merged.scene.endSec).toBe(locked.scenes[1].endSec);
+ });
+
+ it('keeps a selected locked take when rejected, while allowing notes and other candidate reviews', async () => {
+ const { setShotLocks } = await import('./productionReview.js');
+ const p = await projects.createProject({ name: 'Locked take review' });
+ const scene = await projects.addProjectScene(p.id, { startSec: 0, endSec: 2, prompt: 'Keep this' });
+ const { appended } = await projects.appendSceneTakes(p.id, scene.sceneId, [
+ { kind: 'video', assetId: 'approved-clip' }, { kind: 'video', assetId: 'candidate-clip' },
+ ]);
+ await projects.mutateProjectRecord(p.id, current => ({ project: setShotLocks(
+ setShotLocks(current, { sceneIds: [scene.sceneId], locked: true, stage: 'still' }),
+ { sceneIds: [scene.sceneId], locked: true, stage: 'video' },
+ ) }));
+ await expect(projects.reviewSceneTake(p.id, scene.sceneId, appended[0].takeId, { status: 'rejected' }))
+ .rejects.toMatchObject({ code: 'MUSIC_VIDEO_SHOT_LOCKED' });
+ await projects.reviewSceneTake(p.id, scene.sceneId, appended[0].takeId, { note: 'Keep the approved take' });
+ const reviewed = await projects.reviewSceneTake(p.id, scene.sceneId, appended[1].takeId, { status: 'rejected' });
+ expect(reviewed.videoHistoryId).toBe('approved-clip');
+ expect(reviewed.takes).toEqual(expect.arrayContaining([
+ expect.objectContaining({ takeId: appended[0].takeId, status: 'candidate', note: 'Keep the approved take' }),
+ expect.objectContaining({ takeId: appended[1].takeId, status: 'rejected' }),
+ ]));
+ await projects.mutateProjectRecord(p.id, current => ({ project: setShotLocks(current, { sceneIds: [scene.sceneId], locked: false, stage: 'video' }) }));
+ expect((await projects.reviewSceneTake(p.id, scene.sceneId, appended[0].takeId, { status: 'rejected' })).videoHistoryId).toBeNull();
+ });
+});