Skip to content

Show it a video you love. Get a new video in the same style. An AI crew (Claude Code or Codex) plans, builds and reviews it with you.

Address review: stopped background renders stay stopped; tighter render and review slots

許元豪accda29c42d9
diff --git a/.claude/skills/anime-cel/template/render.mjs b/.claude/skills/anime-cel/template/render.mjs
index e5c8ed3..4faa9e7 100644
--- a/.claude/skills/anime-cel/template/render.mjs
+++ b/.claude/skills/anime-cel/template/render.mjs
@@ -87,7 +87,7 @@ async function takeSlot() {
for (let i = 0; i < SLOTS; i++) {
const f = `${SLOT_DIR}/slot${i}`;
try { writeFileSync(f, String(process.pid), { flag: 'wx' }); return f; } catch {}
- try { if (!alive(+readFileSync(f, 'utf8'))) unlinkSync(f); } catch {}
+ try { if (!alive(+readFileSync(f, 'utf8'))) { const t = `${f}.stale${process.pid}`; renameSync(f, t); unlinkSync(t); } } catch {}
}
if (waited && waited % 30 === 0) console.log(`waiting for a render slot (${SLOTS} in use machine-wide)…`);
await new Promise(r => setTimeout(r, 1000));
@@ -103,9 +103,10 @@ try {
} catch {}
const slot = await takeSlot();
process.on('exit', () => { try { unlinkSync(slot); } catch {} });
-for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => process.exit(1));
+let browser;
+for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => { try { browser?.process()?.kill(); } catch {} process.exit(1); });
-const browser = await puppeteer.launch({
+browser = await puppeteer.launch({
executablePath: CHROME, headless: true, protocolTimeout: 0,
args: [...sandbox, '--allow-file-access-from-files', '--ignore-gpu-blocklist', ...gpu, '--enable-gpu-rasterization', '--window-size=1920,1080', '--disable-renderer-backgrounding', '--disable-background-timer-throttling']
});
diff --git a/.claude/skills/crayon-storybook/template/render.mjs b/.claude/skills/crayon-storybook/template/render.mjs
index 5f19c23..0b0d045 100644
--- a/.claude/skills/crayon-storybook/template/render.mjs
+++ b/.claude/skills/crayon-storybook/template/render.mjs
@@ -87,7 +87,7 @@ async function takeSlot() {
for (let i = 0; i < SLOTS; i++) {
const f = `${SLOT_DIR}/slot${i}`;
try { writeFileSync(f, String(process.pid), { flag: 'wx' }); return f; } catch {}
- try { if (!alive(+readFileSync(f, 'utf8'))) unlinkSync(f); } catch {}
+ try { if (!alive(+readFileSync(f, 'utf8'))) { const t = `${f}.stale${process.pid}`; renameSync(f, t); unlinkSync(t); } } catch {}
}
if (waited && waited % 30 === 0) console.log(`waiting for a render slot (${SLOTS} in use machine-wide)…`);
await new Promise(r => setTimeout(r, 1000));
@@ -103,9 +103,10 @@ try {
} catch {}
const slot = await takeSlot();
process.on('exit', () => { try { unlinkSync(slot); } catch {} });
-for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => process.exit(1));
+let browser;
+for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => { try { browser?.process()?.kill(); } catch {} process.exit(1); });
-const browser = await puppeteer.launch({
+browser = await puppeteer.launch({
executablePath: CHROME, headless: true, protocolTimeout: 0,
args: [...sandbox, '--allow-file-access-from-files', '--ignore-gpu-blocklist', ...gpu, '--enable-gpu-rasterization', '--window-size=1920,1080', '--disable-renderer-backgrounding', '--disable-background-timer-throttling']
});
diff --git a/.claude/skills/painted-animation/template/render.mjs b/.claude/skills/painted-animation/template/render.mjs
index e5c8ed3..4faa9e7 100644
--- a/.claude/skills/painted-animation/template/render.mjs
+++ b/.claude/skills/painted-animation/template/render.mjs
@@ -87,7 +87,7 @@ async function takeSlot() {
for (let i = 0; i < SLOTS; i++) {
const f = `${SLOT_DIR}/slot${i}`;
try { writeFileSync(f, String(process.pid), { flag: 'wx' }); return f; } catch {}
- try { if (!alive(+readFileSync(f, 'utf8'))) unlinkSync(f); } catch {}
+ try { if (!alive(+readFileSync(f, 'utf8'))) { const t = `${f}.stale${process.pid}`; renameSync(f, t); unlinkSync(t); } } catch {}
}
if (waited && waited % 30 === 0) console.log(`waiting for a render slot (${SLOTS} in use machine-wide)…`);
await new Promise(r => setTimeout(r, 1000));
@@ -103,9 +103,10 @@ try {
} catch {}
const slot = await takeSlot();
process.on('exit', () => { try { unlinkSync(slot); } catch {} });
-for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => process.exit(1));
+let browser;
+for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => { try { browser?.process()?.kill(); } catch {} process.exit(1); });
-const browser = await puppeteer.launch({
+browser = await puppeteer.launch({
executablePath: CHROME, headless: true, protocolTimeout: 0,
args: [...sandbox, '--allow-file-access-from-files', '--ignore-gpu-blocklist', ...gpu, '--enable-gpu-rasterization', '--window-size=1920,1080', '--disable-renderer-backgrounding', '--disable-background-timer-throttling']
});
diff --git a/.claude/skills/paper-cutout/template/render.mjs b/.claude/skills/paper-cutout/template/render.mjs
index e5c8ed3..4faa9e7 100644
--- a/.claude/skills/paper-cutout/template/render.mjs
+++ b/.claude/skills/paper-cutout/template/render.mjs
@@ -87,7 +87,7 @@ async function takeSlot() {
for (let i = 0; i < SLOTS; i++) {
const f = `${SLOT_DIR}/slot${i}`;
try { writeFileSync(f, String(process.pid), { flag: 'wx' }); return f; } catch {}
- try { if (!alive(+readFileSync(f, 'utf8'))) unlinkSync(f); } catch {}
+ try { if (!alive(+readFileSync(f, 'utf8'))) { const t = `${f}.stale${process.pid}`; renameSync(f, t); unlinkSync(t); } } catch {}
}
if (waited && waited % 30 === 0) console.log(`waiting for a render slot (${SLOTS} in use machine-wide)…`);
await new Promise(r => setTimeout(r, 1000));
@@ -103,9 +103,10 @@ try {
} catch {}
const slot = await takeSlot();
process.on('exit', () => { try { unlinkSync(slot); } catch {} });
-for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => process.exit(1));
+let browser;
+for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => { try { browser?.process()?.kill(); } catch {} process.exit(1); });
-const browser = await puppeteer.launch({
+browser = await puppeteer.launch({
executablePath: CHROME, headless: true, protocolTimeout: 0,
args: [...sandbox, '--allow-file-access-from-files', '--ignore-gpu-blocklist', ...gpu, '--enable-gpu-rasterization', '--window-size=1920,1080', '--disable-renderer-backgrounding', '--disable-background-timer-throttling']
});
diff --git a/.claude/skills/pixel-art/template/render.mjs b/.claude/skills/pixel-art/template/render.mjs
index e5c8ed3..4faa9e7 100644
--- a/.claude/skills/pixel-art/template/render.mjs
+++ b/.claude/skills/pixel-art/template/render.mjs
@@ -87,7 +87,7 @@ async function takeSlot() {
for (let i = 0; i < SLOTS; i++) {
const f = `${SLOT_DIR}/slot${i}`;
try { writeFileSync(f, String(process.pid), { flag: 'wx' }); return f; } catch {}
- try { if (!alive(+readFileSync(f, 'utf8'))) unlinkSync(f); } catch {}
+ try { if (!alive(+readFileSync(f, 'utf8'))) { const t = `${f}.stale${process.pid}`; renameSync(f, t); unlinkSync(t); } } catch {}
}
if (waited && waited % 30 === 0) console.log(`waiting for a render slot (${SLOTS} in use machine-wide)…`);
await new Promise(r => setTimeout(r, 1000));
@@ -103,9 +103,10 @@ try {
} catch {}
const slot = await takeSlot();
process.on('exit', () => { try { unlinkSync(slot); } catch {} });
-for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => process.exit(1));
+let browser;
+for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => { try { browser?.process()?.kill(); } catch {} process.exit(1); });
-const browser = await puppeteer.launch({
+browser = await puppeteer.launch({
executablePath: CHROME, headless: true, protocolTimeout: 0,
args: [...sandbox, '--allow-file-access-from-files', '--ignore-gpu-blocklist', ...gpu, '--enable-gpu-rasterization', '--window-size=1920,1080', '--disable-renderer-backgrounding', '--disable-background-timer-throttling']
});
diff --git a/.claude/skills/whiteboard/template/render.mjs b/.claude/skills/whiteboard/template/render.mjs
index e5c8ed3..4faa9e7 100644
--- a/.claude/skills/whiteboard/template/render.mjs
+++ b/.claude/skills/whiteboard/template/render.mjs
@@ -87,7 +87,7 @@ async function takeSlot() {
for (let i = 0; i < SLOTS; i++) {
const f = `${SLOT_DIR}/slot${i}`;
try { writeFileSync(f, String(process.pid), { flag: 'wx' }); return f; } catch {}
- try { if (!alive(+readFileSync(f, 'utf8'))) unlinkSync(f); } catch {}
+ try { if (!alive(+readFileSync(f, 'utf8'))) { const t = `${f}.stale${process.pid}`; renameSync(f, t); unlinkSync(t); } } catch {}
}
if (waited && waited % 30 === 0) console.log(`waiting for a render slot (${SLOTS} in use machine-wide)…`);
await new Promise(r => setTimeout(r, 1000));
@@ -103,9 +103,10 @@ try {
} catch {}
const slot = await takeSlot();
process.on('exit', () => { try { unlinkSync(slot); } catch {} });
-for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => process.exit(1));
+let browser;
+for (const sig of ['SIGINT', 'SIGTERM']) process.on(sig, () => { try { browser?.process()?.kill(); } catch {} process.exit(1); });
-const browser = await puppeteer.launch({
+browser = await puppeteer.launch({
executablePath: CHROME, headless: true, protocolTimeout: 0,
args: [...sandbox, '--allow-file-access-from-files', '--ignore-gpu-blocklist', ...gpu, '--enable-gpu-rasterization', '--window-size=1920,1080', '--disable-renderer-backgrounding', '--disable-background-timer-throttling']
});
diff --git a/app/server/jobs.ts b/app/server/jobs.ts
index c27d9de..efcff88 100644
--- a/app/server/jobs.ts
+++ b/app/server/jobs.ts
@@ -451,6 +451,7 @@ async function production(id: string, { fresh = false } = {}): Promise<boolean>
} else setStage(id, 'producing', { error: null, failed: null });
// 2) cast gate and 3) shot building run at the same time: shots only call the shared character definitions, so cast fixes
// flow into them automatically. Shot REVIEWS wait until the cast has passed, and re-grab fresh frames first.
+ prerenderStopped.delete(id); // a fresh or resumed production may render in the background again
const castP = castGate(id, prev);
if (!CONFIG.overlapCast && !(await castP)) return false; // characters are final before any shot is built
const prod = readJSON<Production>(join(d, 'build', 'production.json')) || {};
@@ -636,24 +637,30 @@ async function castSerial(id: string, prev: Pipeline, firstReview?: Review | nul
// re-rendering all 732 frames at assembly.
const prerenderQ = new Map<string, Promise<void>>(), prerenderProcs = new Map<string, Set<ChildProcess>>();
const PRERENDER_MAX_MS = (seconds: number) => Math.max(10 * 60e3, seconds * 24 * 6e3); // ~6 s a frame at worst, never under 10 min
-const ASSEMBLY_WAIT_MS = 10 * 60e3; // assembly waits this long for background renders, then stops them and renders what's missing itself
+const ASSEMBLY_WAIT_MS = +(process.env.PRERENDER_WAIT_MS || 10 * 60e3); // assembly waits this long for background renders, then stops them and renders what's missing itself
+// Projects whose background renders were stopped: renders already queued behind the stopped one must not start later
+// and write out/frames while assembly renders there too. Cleared when production (re)starts.
+const prerenderStopped = new Set<string>();
+function stopPrerenders(id: string) { prerenderStopped.add(id); for (const p of prerenderProcs.get(id) || []) p.kill(); prerenderQ.delete(id); }
export async function waitPrerenders(id: string, maxMs = ASSEMBLY_WAIT_MS) {
const q = prerenderQ.get(id); if (!q) return;
let timer: NodeJS.Timeout | undefined;
const late = await Promise.race([q.then(() => false), new Promise<boolean>((r) => { timer = setTimeout(() => r(true), maxMs); })]);
clearTimeout(timer);
- if (late) { log(id, { type: 'error', text: `背景渲染超過 ${Math.round(maxMs / 60e3)} 分鐘,先停掉,組裝時補渲` }); for (const p of prerenderProcs.get(id) || []) p.kill(); prerenderQ.delete(id); }
+ if (late) { log(id, { type: 'error', text: `背景渲染超過 ${Math.round(maxMs / 60e3)} 分鐘,先停掉,組裝時補渲` }); stopPrerenders(id); }
}
function prerender(id: string, c: Chunk) {
- const d = dirOf(id), pr = (readJSON<Production>(join(d, 'build', 'production.json')) || {}).prerender as { cwd?: string; cmd?: string } | undefined;
- if (!pr?.cmd) return;
+ const d = dirOf(id), pr = (readJSON<Production>(join(d, 'build', 'production.json')) || {}).prerender as { cwd?: string; cmd?: string; args?: string[] } | undefined;
+ if (!pr?.cmd && !pr?.args?.length) return;
type S = { id?: string; start_s?: number; end_s?: number };
const mine = ((readJSON<Plan>(join(d, 'plan.json'))?.shots || []) as S[]).filter((s) => s.id && c.shots.includes(s.id) && s.start_s != null && s.end_s != null);
if (!mine.length) return;
const a = Math.min(...mine.map((s) => s.start_s!)), b = Math.max(...mine.map((s) => s.end_s!));
- const [bin, ...args] = pr.cmd.replace('{start}', a.toFixed(3)).replace('{end}', b.toFixed(3)).split(/\s+/);
+ // "args": ["node", "render.mjs", …] keeps arguments with spaces intact; "cmd" is split on whitespace
+ const fill = (x: string) => x.replace('{start}', a.toFixed(3)).replace('{end}', b.toFixed(3));
+ const [bin, ...args] = pr.args?.length ? pr.args.map(fill) : fill(pr.cmd!).split(/\s+/);
const run = () => new Promise<void>((resolve) => {
- if (load(id).stage === 'error') return resolve(); // cancelled or failed meanwhile
+ if (prerenderStopped.has(id) || load(id).stage === 'error') return resolve(); // stopped, cancelled or failed meanwhile
log(id, { type: 'tool', name: 'prerender', detail: `${c.id} ${a.toFixed(2)}–${b.toFixed(2)} s: ${bin} ${args.join(' ')}` });
const t0 = Date.now(), p = spawn(bin, args, { cwd: join(d, pr.cwd || 'build'), env: process.env, stdio: 'ignore' });
const set = prerenderProcs.get(id) || new Set(); set.add(p); prerenderProcs.set(id, set);
@@ -670,9 +677,9 @@ function prerender(id: string, c: Chunk) {
const reviewQ = new Map<string, { n: number; wait: (() => void)[] }>();
async function reviewSlot<T>(id: string, fn: () => Promise<T>): Promise<T> {
const q = reviewQ.get(id) || { n: 0, wait: [] }; reviewQ.set(id, q);
- if (q.n >= CONFIG.maxReviews) await new Promise<void>((r) => q.wait.push(r));
- q.n++;
- try { return await fn(); } finally { q.n--; q.wait.shift()?.(); }
+ if (q.n >= CONFIG.maxReviews) await new Promise<void>((r) => q.wait.push(r)); // woken with the slot already counted
+ else q.n++;
+ try { return await fn(); } finally { const next = q.wait.shift(); if (next) next(); else q.n--; }
}
const sharedLocks = new Map<string, Promise<unknown>>(); // per project: one director edits shared files at a time
@@ -732,5 +739,5 @@ export async function accept(id: string) {
if (await production(id)) await finalPanel(id);
}
-export function cancel(id: string) { for (const ac of running.get(id) || []) ac.abort(); for (const p of prerenderProcs.get(id) || []) p.kill(); }
+export function cancel(id: string) { for (const ac of running.get(id) || []) ac.abort(); stopPrerenders(id); }
export async function critique(id: string) { return finalPanel(id); }
diff --git a/app/server/pipeline.test.ts b/app/server/pipeline.test.ts
index f20965e..b254c55 100644
--- a/app/server/pipeline.test.ts
+++ b/app/server/pipeline.test.ts
@@ -9,6 +9,7 @@ import type { AgentRun } from './agents/index.ts';
const TMP = mkdtempSync(join(tmpdir(), 'reelmimic-pipe-'));
process.env.REELMIMIC_PROJECTS = TMP;
+process.env.PRERENDER_WAIT_MS = '2000'; // assembly gives up on a stuck background render after 2 s here
after(() => rmSync(TMP, { recursive: true, force: true }));
// ---------- the fake agent ----------
@@ -211,6 +212,18 @@ describe('production', () => {
assert.ok(ev.indexOf(doneAt[1]) < assemble, 'assembly started before the background render finished');
});
+ test('when assembly stops waiting for a stuck background render, renders queued behind it never start', async () => {
+ S = base({ prerender: 'node -e setTimeout(()=>{},20000)' }); // every background render hangs
+ const id = newProject('plan_review');
+ writeFileSync(join(J.dirOf(id), 'plan.json'), JSON.stringify({ title: 'T', version: 1, shots: [{ id: 'S1', start_s: 0, end_s: 2 }, { id: 'S2', start_s: 2, end_s: 4 }] }));
+ await J.approve(id);
+ assert.equal(J.load(id).stage, 'done', J.load(id).error || '');
+ await new Promise((r) => setTimeout(r, 500)); // give a wrongly queued render time to show up
+ const ev = readFileSync(join(J.dirOf(id), 'logs', 'events.jsonl'), 'utf8').trim().split(/\r?\n/).map((l) => JSON.parse(l));
+ assert.equal(ev.filter((e) => e.name === 'prerender').length, 1, 'a queued background render started after assembly gave up');
+ assert.ok(ev.some((e) => /背景渲染超過/.test(e.text || '')));
+ });
+
test('shot reviews start while the builder is still working (per-shot watcher)', async () => {
S = base({ chunks: [{ id: 'C1', shots: ['S1', 'S2'] }], buildDelayMs: 4600 });
const id = newProject('plan_review');