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: stop the server crashing with EPERM when git prunes a watched refs directory

[._.]/ Adam Eivya16444e458a2
diff --git a/server/lib/ffmpegFilterGraph.js b/server/lib/ffmpegFilterGraph.js
index 6b41eaa03b..edf85bdd72 100644
--- a/server/lib/ffmpegFilterGraph.js
+++ b/server/lib/ffmpegFilterGraph.js
@@ -10,6 +10,8 @@ import { safeChildProcessOptions } from './processEnv.js';
// over 32K characters, so a filter graph longer than this goes to ffmpeg in a file.
export const FILTER_GRAPH_ARG_LIMIT = 16 * 1024;
+const FFMPEG_7_RELEASE_DATE = '2024-04-05';
+
// ffmpeg 7 reads an option's value from a file with `-/filter_complex <file>`;
// older builds only have `-filter_complex_script`, which later builds drop.
// `null` = not probed; a probe that couldn't run stays uncached.
@@ -19,9 +21,13 @@ const probeFilterGraphFileFlag = async (bin) => {
if (filterGraphFileFlag) return filterGraphFileFlag;
const out = await promisify(execFile)(bin, ['-hide_banner', '-version'], safeChildProcessOptions({}))
.then((r) => String(r.stdout || ''), () => null);
- // A git build ("ffmpeg version N-…") is newer than any release.
+ // An "N-…" git build is newer than any release, but a date-stamped one
+ // ("ffmpeg version 2021-02-13-git-…", the common Windows builds) is only as
+ // new as its date and `-/` landed with the 7.0 release.
const major = /ffmpeg version n?(\d+)\./.exec(out || '')?.[1];
- const flag = major && Number(major) < 7 ? '-filter_complex_script' : '-/filter_complex';
+ const buildDate = /ffmpeg version (\d{4}-\d{2}-\d{2})-/.exec(out || '')?.[1];
+ const predatesFileOptions = major ? Number(major) < 7 : Boolean(buildDate && buildDate < FFMPEG_7_RELEASE_DATE);
+ const flag = predatesFileOptions ? '-filter_complex_script' : '-/filter_complex';
if (out !== null) filterGraphFileFlag = flag;
return flag;
};
diff --git a/server/lib/ffmpegFilterGraph.test.js b/server/lib/ffmpegFilterGraph.test.js
new file mode 100644
index 0000000000..8e9ad5ae81
--- /dev/null
+++ b/server/lib/ffmpegFilterGraph.test.js
@@ -0,0 +1,50 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { mkdtempSync, rmSync } from 'fs';
+import { tmpdir } from 'os';
+import { join } from 'path';
+
+// The real execFile carries util.promisify.custom, so promisify() resolves to
+// `{ stdout, stderr }`; the double mirrors that.
+const mocks = vi.hoisted(() => ({ execFile: vi.fn() }));
+vi.mock('./childProcess.js', async () => {
+ const { promisify } = await import('util');
+ return { execFile: Object.assign(() => {}, { [promisify.custom]: mocks.execFile }) };
+});
+
+const LONG_GRAPH = 'x'.repeat(16 * 1024 + 1);
+
+// The probe caches its answer per process, so each case loads a fresh module.
+const flagFor = async (versionLine) => {
+ vi.resetModules();
+ mocks.execFile.mockResolvedValue({ stdout: `${versionLine}\nbuilt with gcc` });
+ const { filterGraphViaFile } = await import('./ffmpegFilterGraph.js');
+ const dir = mkdtempSync(join(tmpdir(), 'ffmpeg-graph-test-'));
+ try {
+ const { args, file } = await filterGraphViaFile('ffmpeg', ['-i', 'a.mp4', '-filter_complex', LONG_GRAPH], join(dir, 'g.txt'));
+ expect(file).toBe(join(dir, 'g.txt'));
+ return args[2];
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+};
+
+describe('filterGraphViaFile flag selection', () => {
+ beforeEach(() => mocks.execFile.mockReset());
+
+ it('keeps a short graph inline without probing ffmpeg', async () => {
+ const { filterGraphViaFile } = await import('./ffmpegFilterGraph.js');
+ const args = ['-filter_complex', 'null'];
+ expect(await filterGraphViaFile('ffmpeg', args, 'unused')).toEqual({ args, file: null });
+ expect(mocks.execFile).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ ['a release before 7', 'ffmpeg version 6.1.1 Copyright', '-filter_complex_script'],
+ ['a date-stamped Windows build older than the 7.0 release', 'ffmpeg version 2021-02-13-git-d5d6751a55-essentials_build-www.gyan.dev', '-filter_complex_script'],
+ ['a release of 7 or newer', 'ffmpeg version 7.1 Copyright', '-/filter_complex'],
+ ['a date-stamped build after the 7.0 release', 'ffmpeg version 2025-01-02-git-abc123-essentials_build-www.gyan.dev', '-/filter_complex'],
+ ['an N-numbered git build', 'ffmpeg version N-113456-gabcdef Copyright', '-/filter_complex'],
+ ])('uses the file option an ffmpeg %s understands', async (_label, versionLine, expected) => {
+ expect(await flagFor(versionLine)).toBe(expected);
+ });
+});
diff --git a/server/services/askService.test.js b/server/services/askService.test.js
index 15ebaed108..3743dddeef 100644
--- a/server/services/askService.test.js
+++ b/server/services/askService.test.js
@@ -697,7 +697,9 @@ describe('runAsk', () => {
}
const [, args] = spawn.mock.calls.at(-1);
- expect(args[0]).toBe('run');
+ // On Windows a `.cmd` shim on PATH is wrapped as `cmd.exe /c <shim> …`, so
+ // the provider's own argv starts after that two-token prefix.
+ expect(args.at(args[0] === '/c' ? 2 : 0)).toBe('run');
expect(args[args.indexOf('--model') + 1]).toBe('ollama/qwen2.5:7b');
});
diff --git a/server/services/autoUpdateScheduler.js b/server/services/autoUpdateScheduler.js
index 20aaba23d0..a13cd4ae1c 100644
--- a/server/services/autoUpdateScheduler.js
+++ b/server/services/autoUpdateScheduler.js
@@ -95,6 +95,12 @@ function invalidateStatus() {
// committed ref metadata, including worktree/common-dir layouts. Exclude index:
// git status itself can refresh it, which would make a read/notify feedback loop.
// Working-tree-only edits reconcile on activity, scheduler ticks and tab show.
+//
+// Must stay `persistent: true`: chokidar only attaches its `error` listener to
+// the native fs.watch handle on that path. A non-persistent watcher returns the
+// bare handle, and on Windows deleting a watched ref directory (git prunes
+// `refs/heads/<prefix>/` once a worktree branch is deleted) emits EPERM with no
+// listener — an uncaught exception that exits the server.
async function watchRepoChanges() {
const [{ stdout: gitDir }, { stdout: commonDir }] = await Promise.all([
execGit(['rev-parse', '--absolute-git-dir'], PATHS.root),
@@ -103,7 +109,7 @@ async function watchRepoChanges() {
const roots = [...new Set([gitDir.trim(), resolve(PATHS.root, commonDir.trim())])];
repoWatcher = watch(roots.flatMap(root => [
join(root, 'HEAD'), join(root, 'refs'), join(root, 'packed-refs'),
- ]), { ignoreInitial: true, persistent: false, ignored: /\.lock$/ });
+ ]), { ignoreInitial: true, persistent: true, ignored: /\.lock$/ });
repoWatcher.on('all', invalidateStatus);
repoWatcher.on('error', err => console.error(`❌ Auto-update repo watcher failed: ${err.message}`));
}
diff --git a/server/services/autoUpdateScheduler.test.js b/server/services/autoUpdateScheduler.test.js
index 4b7ae95229..3a8826201b 100644
--- a/server/services/autoUpdateScheduler.test.js
+++ b/server/services/autoUpdateScheduler.test.js
@@ -631,6 +631,10 @@ describe('updater status invalidation', () => {
await vi.advanceTimersByTimeAsync(100);
expect(io.emit).toHaveBeenCalledTimes(1);
expect(deps.watch.mock.calls[0][0].every(path => !path.endsWith('index'))).toBe(true);
+ // chokidar guards the native watch handle against Windows EPERM only for
+ // persistent watchers; a non-persistent one crashes the server when git
+ // prunes a watched refs/ subdirectory.
+ expect(deps.watch.mock.calls[0][1]).toMatchObject({ persistent: true });
__resetAutoUpdateSchedulerForTests();
expect(watcher.close).toHaveBeenCalledTimes(1);
});
diff --git a/server/services/musicVideo/shotSegments.test.js b/server/services/musicVideo/shotSegments.test.js
index d4ab628e4d..006bd903b7 100644
--- a/server/services/musicVideo/shotSegments.test.js
+++ b/server/services/musicVideo/shotSegments.test.js
@@ -5,7 +5,7 @@
* the segments it reaches, and a film look change keeps every base segment.
*/
import { describe, it, expect, vi, afterAll, afterEach, beforeAll } from 'vitest';
-import { execFileSync } from 'child_process';
+import { execFileSync, spawnSync } from 'child_process';
import { existsSync, mkdirSync, rmSync, utimesSync, writeFileSync } from 'fs';
import { join, basename } from 'path';
import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../../lib/mockPathsDataRoot.js';
@@ -43,7 +43,7 @@ afterEach(() => { applyFilmLookFinish.mockReset(); applyFilmLookFinish.mockResol
// Runs against the installed ffmpeg; skipped where none is on PATH (Linux CI).
const hasFfmpeg = (() => { try { execFileSync('ffmpeg', ['-version'], { stdio: 'ignore' }); execFileSync('ffprobe', ['-version'], { stdio: 'ignore' }); return true; } catch { return false; } })();
const run = (args) => execFileSync('ffmpeg', ['-hide_banner', '-loglevel', 'error', ...args]);
-const probe = (path, entries) => execFileSync('ffprobe', ['-v', 'error', '-count_frames', '-show_entries', entries, '-of', 'default=nw=1:nk=1', path]).toString().trim().split('\n');
+const probe = (path, entries) => execFileSync('ffprobe', ['-v', 'error', '-count_frames', '-show_entries', entries, '-of', 'default=nw=1:nk=1', path]).toString().trim().split(/\r?\n/);
const project = (over = {}) => ({
id: 'mv-segments-test', name: 'Example', composition: { mode: 'composed' },
@@ -112,8 +112,10 @@ describe.skipIf(!hasFfmpeg)('shot segments', () => {
run(args.filter((a, i) => a !== '-progress' && args[i - 1] !== '-progress').map((a, i, all) => (all[i - 1] === '-preset' ? 'medium' : a)));
const tags = 'setparams=range=tv:color_primaries=bt709:color_trc=bt709:colorspace=bt709';
const lavfi = `[0:v]${tags}[a];[1:v]${tags}[b];[a][b]psnr`;
- const report = execFileSync('sh', ['-c', `ffmpeg -hide_banner -i '${out}' -i '${whole}' -lavfi '${lavfi}' -f null - 2>&1 | grep -o 'average:[0-9.inf]*'`]).toString();
- const average = report.split(':')[1].trim();
+ // psnr prints its summary on stderr; read it directly rather than through a
+ // POSIX shell pipeline so the test also runs where no `sh` is on PATH.
+ const report = spawnSync('ffmpeg', ['-hide_banner', '-i', out, '-i', whole, '-lavfi', lavfi, '-f', 'null', '-'], { encoding: 'utf8' });
+ const average = /average:([0-9.inf]+)/.exec(`${report.stderr}`)?.[1];
expect(average === 'inf' || Number(average) > 35).toBe(true);
}, 60000);
diff --git a/server/services/videoGen/spawnWatch.js b/server/services/videoGen/spawnWatch.js
index c368872de1..9376b41fff 100644
--- a/server/services/videoGen/spawnWatch.js
+++ b/server/services/videoGen/spawnWatch.js
@@ -166,6 +166,11 @@ export async function spawnAndWatchVideo({
previewWatcher = fsWatch(stepwiseDir, (event) => {
if (event === 'rename' || event === 'change') void processLatestPreview();
});
+ // Windows raises EPERM here when the watched dir is deleted (job teardown);
+ // with no listener that is an uncaught exception and the server exits.
+ previewWatcher.on?.('error', (err) => {
+ console.warn(`⚠️ Video preview watcher error [${jobId.slice(0, 8)}]: ${err?.message}`);
+ });
} catch { /* preview is best-effort; the final video remains authoritative */ }
// Every file this render was asked to write, in the order the runner emits
diff --git a/server/services/videoGen/spawnWatch.test.js b/server/services/videoGen/spawnWatch.test.js
index 0ada5e8189..b5b7f64857 100644
--- a/server/services/videoGen/spawnWatch.test.js
+++ b/server/services/videoGen/spawnWatch.test.js
@@ -85,8 +85,8 @@ describe('spawnAndWatchVideo claim lifecycle', () => {
mocks.rm.mockResolvedValue();
mocks.optimize.mockResolvedValue();
mocks.thumbnail.mockResolvedValue('render-thumb.jpg');
- mocks.watch.mockReturnValue({ close: mocks.closeWatcher });
- child = Object.assign(new EventEmitter(), {
+ mocks.watch.mockReturnValue(Object.assign(new EventEmitter(), { close: mocks.closeWatcher }));
+ child =Object.assign(new EventEmitter(), {
pid: 101,
stdout: new EventEmitter(),
stderr: new EventEmitter(),
@@ -197,6 +197,18 @@ describe('spawnAndWatchVideo claim lifecycle', () => {
expect(mocks.release).not.toHaveBeenCalled();
});
+ it('survives a preview watcher error such as the Windows EPERM raised when the preview dir is removed', async () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
+ await spawnAndWatchVideo(params);
+
+ // An EventEmitter with no 'error' listener throws on emit — the uncaught
+ // exception that took the server down.
+ const error = Object.assign(new Error('EPERM: operation not permitted, watch'), { code: 'EPERM' });
+ expect(() => mocks.watch.mock.results[0].value.emit('error', error)).not.toThrow();
+ expect(warn).toHaveBeenCalledWith(expect.stringContaining('Video preview watcher error'));
+ warn.mockRestore();
+ });
+
it('awaits claim release before completing so a completion subscriber can start the next render', async () => {
const released = Promise.withResolvers();
mocks.release.mockReturnValue(released.promise);