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([issue-1946]): unify video-history writes onto a single serialization tail

[._.]/ Adam Eivybfe28bc2306c
diff --git a/server/services/musicVideo/render.js b/server/services/musicVideo/render.js
index 54fa1552f1..a542a735b9 100644
--- a/server/services/musicVideo/render.js
+++ b/server/services/musicVideo/render.js
@@ -30,7 +30,7 @@ import { ServerError } from '../../lib/errorHandler.js';
import { broadcastSse, attachSseClient as attachSse, closeJobAfterDelay } from '../../lib/sseUtils.js';
import { findFfmpeg, safeUnder, generateThumbnail, probeVideoDuration } from '../../lib/ffmpeg.js';
import { safeChildProcessEnv } from '../../lib/processEnv.js';
-import { loadHistory, saveHistory } from '../videoGen/local.js';
+import { loadHistory, mutateVideoHistory } from '../videoGen/local.js';
import { getTrack } from '../tracks/index.js';
import { getProject, updateProject } from './projects.js';
@@ -43,25 +43,18 @@ const projectRenders = new Map();
// can't pass the 409 check during that await window and spawn a duplicate.
const PENDING = Symbol('mv-render-pending');
-// Serialize the read-modify-write of the shared video-history file across renders.
-// The per-project mutex above deliberately lets two DIFFERENT projects render (and
-// finalize) in parallel, but each finalizer does loadHistory→unshift→saveHistory and
-// saveHistory is a bare atomicWrite (the normal video-gen pipeline only avoids this
-// race because mediaJobQueue serializes it to one GPU lane — this renderer bypasses
-// that queue). Two overlapping finalize windows would otherwise drop one project's
-// entry, leaving its "View rendered music video" deep link pointing at a 404. One
-// module-level tail collapses every history append onto a single lane.
-let historyWriteTail = Promise.resolve();
-function appendToVideoHistory(meta) {
- const run = historyWriteTail.then(async () => {
- const loaded = await loadHistory();
- const history = Array.isArray(loaded) ? loaded : [];
- history.unshift(meta);
- await saveHistory(history);
- });
- historyWriteTail = run.catch(() => {}); // keep the lane alive even if one append throws
- return run;
-}
+// Append a finalized render to the shared video-history file. The per-project
+// mutex above deliberately lets two DIFFERENT projects render (and finalize) in
+// parallel, and this renderer bypasses the mediaJobQueue GPU lane that serializes
+// the normal video-gen pipeline — so its append MUST share the ONE per-file
+// serialization tail (`mutateVideoHistory` in videoGen/history.js) with every
+// other writer of data/video-history.json (the video-gen finalizer, full-video
+// downloads, stitch/upscale, timeline saves). A private second tail here would
+// re-open the race against those other writers and drop an entry, leaving a
+// "View rendered music video" deep link pointing at a 404. mutateVideoHistory
+// keeps the lane alive if one append throws.
+const appendToVideoHistory = (meta) =>
+ mutateVideoHistory((history) => { history.unshift(meta); return history; });
export const attachRenderSseClient = (jobId, res) => attachSse(jobs, jobId, res);
diff --git a/server/services/videoGen/local.js b/server/services/videoGen/local.js
index 7ebd10bbfa..f0dc7bfe01 100644
--- a/server/services/videoGen/local.js
+++ b/server/services/videoGen/local.js
@@ -47,7 +47,7 @@ import { loadHistory, saveHistory, mutateVideoHistory } from './history.js';
// Re-export the extracted runtime + history surface so existing deep imports
// (`from '../videoGen/local.js'`) keep resolving every symbol they used to.
export * from './runtimes.js';
-export { loadHistory, saveHistory };
+export { loadHistory, saveHistory, mutateVideoHistory };
// LoRA wrapper for the notapalindrome `mlx_video` runtime. The stock
// `mlx_video.generate_av` CLI has no --lora flag, but the package ships an
diff --git a/server/services/videoTimeline/local.js b/server/services/videoTimeline/local.js
index d9cc997929..baf6ca92ea 100644
--- a/server/services/videoTimeline/local.js
+++ b/server/services/videoTimeline/local.js
@@ -23,7 +23,7 @@ import { ServerError } from '../../lib/errorHandler.js';
import { broadcastSse, attachSseClient as attachSse, closeJobAfterDelay } from '../../lib/sseUtils.js';
import { findFfmpeg, findFfprobe, safeUnder, generateThumbnail } from '../../lib/ffmpeg.js';
import { safeChildProcessEnv } from '../../lib/processEnv.js';
-import { loadHistory, saveHistory } from '../videoGen/local.js';
+import { loadHistory, mutateVideoHistory } from '../videoGen/local.js';
const PROJECTS_FILE = join(PATHS.data, 'video-projects.json');
@@ -459,10 +459,9 @@ export async function renderProject(projectId) {
createdAt: new Date().toISOString(),
timelineProjectId: projectId,
};
- const loadedHistory = await loadHistory();
- const history = Array.isArray(loadedHistory) ? loadedHistory : [];
- history.unshift(meta);
- await saveHistory(history);
+ // Serialized append through the single shared history tail so a concurrent
+ // download/render/timeline write can't clobber this entry.
+ await mutateVideoHistory((history) => { history.unshift(meta); return history; });
console.log(`✅ Timeline rendered [${jobId.slice(0, 8)}]: ${filename}`);
broadcastSse(job, { type: 'complete', result: { id: jobId, filename, thumbnail: thumb, path: `/data/videos/${filename}` } });
projectRenders.delete(projectId);