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.

Don't crash the server when Python can't be started

revaldianggarac4c3cf26bcb0
diff --git a/app/scripts/doctor.mjs b/app/scripts/doctor.mjs
index d726f1c..ab2b248 100644
--- a/app/scripts/doctor.mjs
+++ b/app/scripts/doctor.mjs
@@ -30,8 +30,9 @@ const PY = process.env.PYTHON || 'python';
console.log('\nReelMimic — environment check\n');
console.log('Core');
ok(`Node ${process.versions.node}`);
-const pyv = run(PY, ['--version']);
-pyv ? ok(pyv) : bad('Python 3.10+ not found', 'install Python, or set PYTHON=/path/to/python');
+// env.ts only sets PYTHON for a 3.10+ interpreter; the 'python' fallback may be older, so check the version here too
+const pyv = run(PY, ['-c', 'import sys; assert sys.version_info >= (3, 10); print("Python", sys.version.split()[0])']);
+pyv ? ok(pyv) : bad('Python 3.10+ not found', 'install Python 3.10 or newer, or set PYTHON=/path/to/python');
const ff = run('ffmpeg', ['-version']);
const ffDir = process.env.FFMPEG_DIR && existsSync(join(process.env.FFMPEG_DIR, IS_WIN ? 'ffmpeg.exe' : 'ffmpeg'));
ff || ffDir ? ok(`FFmpeg ${(ff || '').split('\n')[0].split(' ')[2] || `(FFMPEG_DIR)`}`) : bad('FFmpeg not found', 'install FFmpeg and put it on PATH, or set FFMPEG_DIR');
diff --git a/app/server/jobs.ts b/app/server/jobs.ts
index 2c4ea1d..9f32f3b 100644
--- a/app/server/jobs.ts
+++ b/app/server/jobs.ts
@@ -30,6 +30,7 @@ export const ROOT = join(import.meta.dirname, '..', '..');
export const PROJECTS = process.env.REELMIMIC_PROJECTS ? resolve(process.env.REELMIMIC_PROJECTS) : join(ROOT, 'projects'); // override: tests, a second checkout
const SCRIPTS = join(ROOT, '.claude', 'skills', 'video-clone', 'scripts');
const PY = process.env.PYTHON || 'python';
+const noPython = (e: Error) => `${PY} could not be started (${e.message}). Install Python 3.10+, or set PYTHON=/path/to/python`;
// A review passes when nothing it lists is a blocker (polish items are handed on, they never hold the line)
const passed = (rv: Review) => { const L = rv.issues || []; return L.length && L.every((x) => x.severity) ? !L.some((x) => x.severity !== 'polish') : !!rv.pass; };
const shotPassed = (s: ShotEntry) => { const L = s.issues || []; return L.length && L.every((x) => x.severity) ? !L.some((x) => x.severity !== 'polish') : !!s.pass; };
@@ -211,14 +212,17 @@ export function saveLyrics(id: string, text: string): Promise<LyricsResult> {
if (m.section?.start_s != null && song && !/clip/i.test(song)) args.push('--start', String(m.section.start_s), '--end', String(m.section.end_s));
log(id, { type: 'tool', name: 'align_lyrics.py', detail: `對時:${song}` });
return new Promise((resolve) => {
- const p = spawn(PY, args, { cwd: ROOT, env: { ...process.env, PYTHONIOENCODING: 'utf-8' } }); let o = '';
+ const p = spawn(PY, args, { cwd: ROOT, env: { ...process.env, PYTHONIOENCODING: 'utf-8' } }); let o = '', done = false;
p.stdout.on('data', (b) => { o += b; }); p.stderr.on('data', () => {});
- p.on('close', (code) => {
+ const finish = (code: number | null, err?: Error) => {
+ if (done) return; done = true;
const first = o.split('\n').find((l) => l.startsWith('lines')) || '';
- log(id, { type: code === 0 ? 'text' : 'error', text: code === 0 ? `歌詞對時完成:${first}` : '歌詞對時失敗' });
+ log(id, { type: code === 0 ? 'text' : 'error', text: code === 0 ? `歌詞對時完成:${first}` : err ? `歌詞對時失敗:${noPython(err)}` : '歌詞對時失敗' });
if (code === 0) update(id, (j) => { j.needs = (j.needs || []).filter((n) => n.kind !== 'lyrics'); });
resolve({ ok: code === 0, aligned: code === 0, report: first });
- });
+ };
+ p.on('error', (e) => finish(null, e)); // e.g. Python not found: without this listener the server crashes
+ p.on('close', (code) => finish(code));
});
}
@@ -231,11 +235,15 @@ function analyze(id: string): Promise<boolean> {
const p = spawn(PY, [join(SCRIPTS, 'analyze.py'), src, '--out', join(d, 'analysis')], { cwd: ROOT, env: { ...process.env, PYTHONIOENCODING: 'utf-8' } });
p.stdout.on('data', (b) => String(b).split('\n').filter(Boolean).forEach((l) => log(id, { type: 'text', text: l })));
p.stderr.on('data', (b) => { const s = String(b); if (!/Warning|warn\(/i.test(s)) log(id, { type: 'error', text: s.slice(0, 400) }); });
- p.on('close', (code) => {
+ let done = false;
+ const finish = (code: number | null, err?: Error) => {
+ if (done) return; done = true;
const ok = code === 0 && existsSync(join(d, 'analysis', 'report.json'));
- if (!ok) setStage(id, 'error', { failed: 'analyzing', error: 'analyze.py failed' });
+ if (!ok) setStage(id, 'error', { failed: 'analyzing', error: err ? noPython(err) : 'analyze.py failed' });
resolve(ok);
- });
+ };
+ p.on('error', (e) => finish(null, e)); // e.g. Python not found: without this listener the server crashes
+ p.on('close', (code) => finish(code));
});
}
diff --git a/app/server/python.test.ts b/app/server/python.test.ts
new file mode 100644
index 0000000..58755d1
--- /dev/null
+++ b/app/server/python.test.ts
@@ -0,0 +1,34 @@
+// When Python can't be started the job fails with a clear message instead of the server crashing (unhandled 'error').
+// Runs against a throwaway projects folder with PYTHON pointing at a command that doesn't exist. No agent is started.
+import { after, describe, test } from 'node:test';
+import assert from 'node:assert/strict';
+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+
+const TMP = mkdtempSync(join(tmpdir(), 'reelmimic-test-'));
+process.env.REELMIMIC_PROJECTS = TMP;
+process.env.PYTHON = 'reelmimic-no-such-python';
+const J = await import('./jobs.ts');
+after(() => rmSync(TMP, { recursive: true, force: true }));
+
+let n = 0;
+const newJob = () => J.createJob({ id: `py-${++n}`, title: 'test', agent: 'claude', brief: 'a brief', reference: { type: 'url', src: 'https://example.com/v' } });
+
+describe('Python missing', () => {
+ test('analysis fails the job with a "Python not found" message', async () => {
+ const { id } = newJob();
+ await J.start(id);
+ const j = J.load(id);
+ assert.equal(j.stage, 'error');
+ assert.equal(j.failed, 'analyzing');
+ assert.match(j.error || '', /reelmimic-no-such-python could not be started.*Python 3\.10\+/);
+ });
+ test('lyrics alignment reports failure', async () => {
+ const { id } = newJob();
+ writeFileSync(join(J.dirOf(id), 'inputs', 'song.mp3'), '');
+ const r = await J.saveLyrics(id, '第一句\n第二句');
+ assert.deepEqual({ ok: r.ok, aligned: r.aligned }, { ok: false, aligned: false });
+ assert.ok(J.load(id).log?.some((e) => e.type === 'error' && /could not be started/.test(e.text || '')));
+ });
+});