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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: remove sprite trim scratch frames when encoding throws (#12967) (#13084)
[._.]/4a2beb8fd8aa
diff --git a/server/services/sprites/walkTrims.js b/server/services/sprites/walkTrims.jsindex 2511ddb134..e5890472b9 100644--- a/server/services/sprites/walkTrims.js+++ b/server/services/sprites/walkTrims.js@@ -48,24 +48,29 @@ async function encodeTrimGif(frames, fps, destAbs) {const ffmpeg = await findFfmpeg();if (!ffmpeg) throw new ServerError('ffmpeg not found — install ffmpeg to render trim GIFs', { status: 503, code: 'FFMPEG_MISSING' });const scratch = await mkdtemp(join(tmpdir(), 'portos-sprite-trim-'));- for (let i = 0; i < frames.length; i++) {- await writeFileGuarded(- join(scratch, `frame-${String(i).padStart(3, '0')}.png`),- await encodePng(frames[i]),- );+ let result;+ // The scratch frames must not outlive a throw in the write loop or ffmpeg.+ try {+ for (let i = 0; i < frames.length; i++) {+ await writeFileGuarded(+ join(scratch, `frame-${String(i).padStart(3, '0')}.png`),+ await encodePng(frames[i]),+ );+ }+ result = await runFfmpegProcess({+ bin: ffmpeg,+ args: [+ '-y', '-framerate', String(fps),+ '-i', join(scratch, 'frame-%03d.png'),+ '-filter_complex', '[0:v]split[a][b];[a]palettegen=reserve_transparent=1[p];[b][p]paletteuse=alpha_threshold=128',+ '-loop', '0',+ destAbs,+ ],+ timeoutMs: FFMPEG_SCAN_TIMEOUT_MS,+ });+ } finally {+ await rmGuarded(scratch, { recursive: true, force: true }).catch(() => {});}- const result = await runFfmpegProcess({- bin: ffmpeg,- args: [- '-y', '-framerate', String(fps),- '-i', join(scratch, 'frame-%03d.png'),- '-filter_complex', '[0:v]split[a][b];[a]palettegen=reserve_transparent=1[p];[b][p]paletteuse=alpha_threshold=128',- '-loop', '0',- destAbs,- ],- timeoutMs: FFMPEG_SCAN_TIMEOUT_MS,- });- await rmGuarded(scratch, { recursive: true, force: true }).catch(() => {});if (!result.ok) throw new ServerError(`GIF encode failed: ${result.reason}`, { status: 500, code: 'GIF_ENCODE_FAILED' });}diff --git a/server/services/sprites/walkTrims.test.js b/server/services/sprites/walkTrims.test.jsindex 4922b3e873..b7a8bd60e2 100644--- a/server/services/sprites/walkTrims.test.js+++ b/server/services/sprites/walkTrims.test.js@@ -6,7 +6,7 @@*/import { describe, it, expect, vi, beforeEach, afterAll } from 'vitest';-import { mkdtempSync, rmSync } from 'fs';+import { existsSync, mkdtempSync, rmSync } from 'fs';import { tmpdir } from 'os';import { dirname, join } from 'path';import sharp from 'sharp';@@ -15,6 +15,7 @@ import { writeFile, mkdir, readFile, readdir } from 'fs/promises';const TEST_ROOT = mkdtempSync(join(tmpdir(), 'sprite-trim-test-'));let beforeAssetWrite = async () => {};+let beforeScratchFrameWrite = async () => {};let bypassAdmission = false;vi.mock('../../lib/backupSnapshotBoundary.js', async importOriginal => {const actual = await importOriginal();@@ -29,6 +30,9 @@ vi.mock('../../lib/fileUtils.js', async (importOriginal) => {return { ...actual, atomicWrite: async (path, ...args) => {await beforeAssetWrite(path);return actual.atomicWrite(path, ...args);+ }, writeFileGuarded: async (path, ...args) => {+ await beforeScratchFrameWrite(path);+ return actual.writeFileGuarded(path, ...args);} };});@@ -173,7 +177,7 @@ async function characterWithImportedRun(id, { manifest = 'valid', anchorPaths =}beforeEach(() => {- beforeAssetWrite = async () => {}; bypassAdmission = false;+ beforeAssetWrite = async () => {}; beforeScratchFrameWrite = async () => {}; bypassAdmission = false;runFfmpegProcess.mockClear();rmSync(join(TEST_ROOT, 'sprite-records.json'), { force: true });});@@ -376,3 +380,30 @@ describe('trim backup publication', () => {} finally { release(); }});});++describe('saveLoopTrim scratch cleanup', () => {+ // encodeTrimGif's scratch dir is a tmpdir mkdtemp; capture it from the frame writes.+ it('removes the scratch frame dir when a frame write throws mid-loop', async () => {+ const id = await characterWithRun(newId());+ const dirs = new Set();+ beforeScratchFrameWrite = async path => {+ if (!/frame-\d+\.png$/.test(path)) return;+ dirs.add(dirname(path));+ if (path.endsWith('frame-001.png')) throw new Error('ENOSPC: simulated disk full');+ };+ await expect(saveLoopTrim(id, { runId: RUN_ID, enabledColumns: [0, 1, 2] }))+ .rejects.toThrow('ENOSPC');+ expect(dirs.size).toBe(1);+ for (const dir of dirs) expect(existsSync(dir)).toBe(false);+ });+ it('removes the scratch frame dir and keeps GIF_ENCODE_FAILED when ffmpeg reports failure', async () => {+ const id = await characterWithRun(newId());+ const dirs = new Set();+ beforeScratchFrameWrite = async path => { if (/frame-\d+\.png$/.test(path)) dirs.add(dirname(path)); };+ runFfmpegProcess.mockResolvedValueOnce({ ok: false, reason: 'boom' });+ await expect(saveLoopTrim(id, { runId: RUN_ID, enabledColumns: [0, 1] }))+ .rejects.toMatchObject({ code: 'GIF_ENCODE_FAILED' });+ expect(dirs.size).toBe(1);+ for (const dir of dirs) expect(existsSync(dir)).toBe(false);+ });+});