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

address review (codex): route ALL video-history writers through the shared serialization tail

[._.]/ Adam Eivyee512182f7ce
diff --git a/server/services/videoGen/generateVideoHelpers.js b/server/services/videoGen/generateVideoHelpers.js
index e1415e7a21..a5d890cc01 100644
--- a/server/services/videoGen/generateVideoHelpers.js
+++ b/server/services/videoGen/generateVideoHelpers.js
@@ -150,21 +150,25 @@ export function isWatchdogSuccess({ completionWatchdogFired, signal, outputPath
* @param {string} ctx.filename
* @param {object} ctx.meta - the history-entry metadata built up-front
* @param {number} ctx.actualSeed
- * @param {() => Promise<Array>} ctx.loadHistory
- * @param {(h: Array) => Promise<void>} ctx.saveHistory
+ * @param {(mutator: (h: Array) => Array) => Promise<Array>} ctx.mutateHistory - serialized read-modify-write on the shared history file (mutateVideoHistory)
*/
-export async function finalizeGeneratedVideo({ job, jobId, outputPath, filename, meta, actualSeed, loadHistory, saveHistory }) {
+export async function finalizeGeneratedVideo({ job, jobId, outputPath, filename, meta, actualSeed, mutateHistory }) {
job.status = 'complete';
await optimizeForStreaming(outputPath);
const thumbnail = await generateThumbnail(outputPath, jobId);
- const history = await loadHistory();
+ // Serialized append through the shared history tail so a concurrent write
+ // path (a full-video download completing, another render finalizing) can't
+ // read the same stale array and clobber this record on save.
+ //
// Persist the runtime fingerprint captured from the child's startup RUNTIME:
// line (set on `job` by makeVideoGenLineHandler) so each history record
// self-documents the exact ltx/mlx/torch + chip + OS stack it rendered on.
// Absent (sentinel) when the runtime didn't emit one — e.g. the bare
// `mlx_video.generate_av` path we don't control.
- history.unshift({ ...meta, thumbnail, ...(job.runtime ? { runtime: job.runtime } : {}) });
- await saveHistory(history);
+ await mutateHistory((history) => {
+ history.unshift({ ...meta, thumbnail, ...(job.runtime ? { runtime: job.runtime } : {}) });
+ return history;
+ });
console.log(`✅ Video generated [${jobId.slice(0, 8)}]: ${filename}`);
broadcastSse(job, { type: 'complete', result: { filename, seed: actualSeed, thumbnail, path: `/data/videos/${filename}` } });
videoGenEvents.emit('completed', { generationId: jobId, filename, path: `/data/videos/${filename}`, thumbnail });
diff --git a/server/services/videoGen/generateVideoHelpers.test.js b/server/services/videoGen/generateVideoHelpers.test.js
index e56b92471e..e8c42cd810 100644
--- a/server/services/videoGen/generateVideoHelpers.test.js
+++ b/server/services/videoGen/generateVideoHelpers.test.js
@@ -119,8 +119,7 @@ describe('finalizeGeneratedVideo runtime persistence', () => {
let saved = null;
await finalizeGeneratedVideo({
...baseCtx(job),
- loadHistory: async () => [],
- saveHistory: async (h) => { saved = h; },
+ mutateHistory: async (fn) => { saved = await fn([]); return saved; },
});
expect(saved).toHaveLength(1);
expect(saved[0].runtime).toEqual(fp);
@@ -131,8 +130,7 @@ describe('finalizeGeneratedVideo runtime persistence', () => {
let saved = null;
await finalizeGeneratedVideo({
...baseCtx(job),
- loadHistory: async () => [],
- saveHistory: async (h) => { saved = h; },
+ mutateHistory: async (fn) => { saved = await fn([]); return saved; },
});
expect(saved).toHaveLength(1);
expect('runtime' in saved[0]).toBe(false);
diff --git a/server/services/videoGen/local.js b/server/services/videoGen/local.js
index 3143258410..7ebd10bbfa 100644
--- a/server/services/videoGen/local.js
+++ b/server/services/videoGen/local.js
@@ -43,7 +43,7 @@ import {
assertByovRuntimeInstalled,
invalidateByovReadyCache,
} from './runtimes.js';
-import { loadHistory, saveHistory } from './history.js';
+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';
@@ -1029,7 +1029,7 @@ export async function generateVideo({ pythonPath, prompt, negativePrompt = '', m
if (watchdogSuccess) {
console.log(`⚠️ video child force-killed after completion teardown hang — output is intact [${jobId.slice(0, 8)}]`);
}
- await finalizeGeneratedVideo({ job, jobId, outputPath, filename, meta, actualSeed, loadHistory, saveHistory });
+ await finalizeGeneratedVideo({ job, jobId, outputPath, filename, meta, actualSeed, mutateHistory: mutateVideoHistory });
}
} catch (err) {
// Finalize/teardown threw — fail the job loudly instead of crashing the
@@ -1262,12 +1262,14 @@ export async function generateChainedVideo({ chunks, jobId: outerJobId, ...rest
async function setHistoryItemsHidden(ids, hidden) {
if (!ids?.length) return;
const wanted = new Set(ids);
- const history = await loadHistory().catch(() => null);
- if (!Array.isArray(history)) return;
- for (const item of history) {
- if (wanted.has(item.id)) item.hidden = !!hidden;
- }
- await saveHistory(history).catch(() => {});
+ // Serialized through the shared history tail so a concurrent write path
+ // (a download completing, a render finalizing) can't clobber this update.
+ await mutateVideoHistory((history) => {
+ for (const item of history) {
+ if (wanted.has(item.id)) item.hidden = !!hidden;
+ }
+ return history;
+ }).catch(() => {});
}
// Extract the last frame of a video as a PNG into data/images/ — used to
@@ -1476,9 +1478,10 @@ export async function stitchVideos(videoIds, opts = {}) {
loraScales: videos[0].loraScales,
} : {}),
};
- const h = await loadHistory();
- h.unshift(stitchedMeta);
- await saveHistory(h);
+ // Serialized append against the shared history tail (re-reads the freshest
+ // list inside the mutator) so a concurrent download/render write can't drop
+ // this stitched entry.
+ await mutateVideoHistory((history) => { history.unshift(stitchedMeta); return history; });
console.log(`🎬 Stitched ${videos.length} videos → ${outFilename}`);
return stitchedMeta;
}
@@ -1541,20 +1544,25 @@ export async function upscaleHistoryItem(historyId) {
// even when the source clip was hidden.
hidden: false,
};
- const refreshedHistory = await loadHistory();
- refreshedHistory.unshift(newEntry);
- await saveHistory(refreshedHistory);
+ // Serialized append (re-reads inside the mutator) so a concurrent
+ // download/render write can't drop the upscaled entry.
+ await mutateVideoHistory((history) => { history.unshift(newEntry); return history; });
console.log(`✅ Upscaled [${newId.slice(0, 8)}]: ${newFilename} (${newEntry.width}×${newEntry.height})`);
return newEntry;
}
export async function setHistoryItemHidden(id, hidden) {
- const history = await loadHistory();
- const item = history.find((h) => h.id === id);
- if (!item) throw new ServerError('Not found', { status: 404, code: 'NOT_FOUND' });
- item.hidden = !!hidden;
- await saveHistory(history);
- return { ok: true, hidden: item.hidden };
+ let result;
+ // Serialized find-and-set through the shared tail; a 404 throw inside the
+ // mutator rejects before any save, preserving the not-found semantics.
+ await mutateVideoHistory((history) => {
+ const item = history.find((h) => h.id === id);
+ if (!item) throw new ServerError('Not found', { status: 404, code: 'NOT_FOUND' });
+ item.hidden = !!hidden;
+ result = { ok: true, hidden: item.hidden };
+ return history;
+ });
+ return result;
}
export async function deleteHistoryItem(id) {
@@ -1576,7 +1584,9 @@ export async function deleteHistoryItem(id) {
const frameFile = safeUnder(PATHS.videoThumbnails, `${id}-f${i}.jpg`);
if (frameFile) await unlink(frameFile).catch(() => {});
}
- await saveHistory(history.filter((h) => h.id !== id));
+ // Serialized removal through the shared tail (re-filters the freshest list),
+ // so a concurrent download/render append isn't dropped by this save.
+ await mutateVideoHistory((h) => h.filter((x) => x.id !== id));
console.log(`🗑️ Deleted video: ${item.filename}`);
return { ok: true };
}