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: settle auto-review background work deterministically and stop a 9s stall staging Three.js documents

Claude7f3d8f0f8c22
diff --git a/server/routes/musicVideoAutoReview.test.js b/server/routes/musicVideoAutoReview.test.js
index b662ea551c..794ec43cc5 100644
--- a/server/routes/musicVideoAutoReview.test.js
+++ b/server/routes/musicVideoAutoReview.test.js
@@ -8,7 +8,7 @@
* sequence whose call count is the review spend).
*/
-import { describe, it, expect, vi, beforeEach, afterAll } from 'vitest';
+import { describe, it, expect, vi, beforeEach, afterEach, afterAll } from 'vitest';
import express from 'express';
import { createHash } from 'crypto';
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'fs';
@@ -131,6 +131,7 @@ const { assertRevisionOpen } = await import('../services/musicVideo/revisionServ
const { musicVideoEvents } = await import('../services/musicVideo/events.js');
const { runPromptThroughProvider } = await import('../services/promptRunner.js');
const { mediaJobEvents } = await import('../services/mediaJobQueue/index.js');
+const { __testing: autoReviewBackground } = await import('../services/musicVideo/autoReviewService.js');
const app = express();
app.use(express.json());
@@ -201,6 +202,10 @@ beforeEach(() => {
h.temporalCalls.length = 0;
vi.clearAllMocks();
});
+// A run advances in the background; let it go quiet before the next test
+// resets the shared doubles, so one test's late step can never land in the
+// next test's queue (#10467).
+afterEach(() => autoReviewBackground.settleBackground());
afterAll(cleanupTempDataRoots);
describe('opt-in automatic review/retries (#8988)', () => {
@@ -285,7 +290,7 @@ describe('opt-in automatic review/retries (#8988)', () => {
// The take the board already paid for still lands — but a stopped run
// does not advance on it: no re-render, no review.
await landTake(p.id, 's2', 'clip-2b');
- await new Promise((resolve) => setTimeout(resolve, 50));
+ await autoReviewBackground.settleBackground();
expect(h.procs).toHaveLength(1);
// Resume: the revision renders (its section already holds a take — no
diff --git a/server/services/musicVideo/autoReviewService.js b/server/services/musicVideo/autoReviewService.js
index 56a67967c3..bae968177f 100644
--- a/server/services/musicVideo/autoReviewService.js
+++ b/server/services/musicVideo/autoReviewService.js
@@ -81,6 +81,15 @@ const MAX_STEPS_PER_ADVANCE = 16;
const REVIEW_TIMEOUT_MS = 240_000;
const advancing = new Map();
+// Every fire-and-forget continuation (a background advance, an event's
+// follow-up, a pause after a failed job), so a caller can wait for the
+// service to go quiet instead of guessing with a timer.
+const background = new Set();
+function track(promise) {
+ background.add(promise);
+ promise.finally(() => background.delete(promise)).catch(() => {});
+ return promise;
+}
const short = (id) => String(id || '').slice(5, 13);
@@ -414,12 +423,12 @@ function advanceAutoReview(projectId, runId) {
// checkpoint is written; the advance continues in the background and reports
// over the `auto-review` event.
function advanceInBackground(projectId, runId) {
- advanceAutoReview(projectId, runId).catch(async (err) => {
+ track(advanceAutoReview(projectId, runId).catch(async (err) => {
console.error(`❌ Music Video auto-review ${short(runId)} step failed: ${err.message}`);
await halt(projectId, runId, { status: 'stopped', reason: `A step failed: ${err.message}`, error: err.message })
.then((out) => publish(projectId, out.project, out.run, { type: 'idle' }))
.catch(() => {});
- });
+ }));
}
/** Start a run (explicit director request with limits). Returns `{ project, run }`. */
@@ -491,9 +500,9 @@ function armJobEndListener() {
const onJobEnded = (job) => {
const tag = job?.params?.musicVideo;
if (!tag?.projectId || !tag.revisionId || !tag.sceneId) return;
- pauseOnEndedGeneration(tag, job).catch((err) => {
+ track(pauseOnEndedGeneration(tag, job).catch((err) => {
console.error(`❌ Music Video auto-review could not pause after a ${job.status} generation: ${err.message}`);
- });
+ }));
};
mediaJobEvents.on('failed', onJobEnded);
mediaJobEvents.on('canceled', onJobEnded);
@@ -519,12 +528,12 @@ async function pauseOnEndedGeneration(tag, job) {
}
function continueFromEvent(projectId, pickRun, label) {
- getProject(projectId)
+ track(getProject(projectId)
.then((project) => {
const run = project ? pickRun(project) : null;
return run ? advanceAutoReview(projectId, run.id) : null;
})
- .catch((err) => console.error(`❌ Music Video auto-review could not continue after ${label}: ${err.message}`));
+ .catch((err) => console.error(`❌ Music Video auto-review could not continue after ${label}: ${err.message}`)));
}
musicVideoEvents.on('excerpt-render', ({ projectId, excerptId } = {}) => {
@@ -545,3 +554,13 @@ const onSceneTake = ({ projectId, sceneId } = {}) => {
};
musicVideoEvents.on('scene-image', onSceneTake);
musicVideoEvents.on('scene-video', onSceneTake);
+
+/**
+ * Resolves once no background work is in flight — including work an earlier
+ * piece started while it ran (an advance that schedules another).
+ */
+async function settleBackground() {
+ while (background.size) await Promise.allSettled([...background]);
+}
+
+export const __testing = { settleBackground };
diff --git a/server/services/musicVideo/documentMediaPolicy.js b/server/services/musicVideo/documentMediaPolicy.js
index 337df97e0f..ad3da8baa7 100644
--- a/server/services/musicVideo/documentMediaPolicy.js
+++ b/server/services/musicVideo/documentMediaPolicy.js
@@ -30,8 +30,14 @@ export async function assertDocumentMediaPolicy(project, files) {
let source = bytes.toString('utf8');
if (ext === '.js' || ext === '.mjs') {
// Inspect executable text and string literals, not inert JSDoc such as Array<Image>.
+ // One pass over the (source-ordered) comments: re-slicing the whole text per
+ // comment is quadratic and took ~9s on the vendored three.core.js (#10467).
const { comments } = parse(source, { sourceType: 'unambiguous' });
- for (const comment of comments.slice().reverse()) source = source.slice(0, comment.start) + ' ' + source.slice(comment.end);
+ const kept = [];
+ let at = 0;
+ for (const comment of comments) { kept.push(source.slice(at, comment.start)); at = comment.end; }
+ kept.push(source.slice(at));
+ source = kept.join(' ');
}
if (/data\s*:\s*image\/|<(?:img|image)\b|!\[[^\]]*\]\(/i.test(source)) assertMusicVideoMedia(project, 'image', 'embedded document asset');
if (/data\s*:\s*(?:video|audio)\/|<(?:video|audio)\b/i.test(source)) assertMusicVideoMedia(project, 'video', 'embedded document asset');