diff --git a/backend/src/routes/tts.ts b/backend/src/routes/tts.ts index 27df38b3..2b9e6dba 100644 --- a/backend/src/routes/tts.ts +++ b/backend/src/routes/tts.ts @@ -1,12 +1,12 @@ import { Hono } from 'hono' import { z } from 'zod' import { Database } from 'bun:sqlite' -import { createHash, randomUUID } from 'crypto' -import { readFile, writeFile, readdir, stat, unlink, rename } from 'fs/promises' +import { createHash } from 'crypto' +import { readFile, readdir, stat, unlink } from 'fs/promises' import { join } from 'path' import { SettingsService } from '../services/settings' import { logger } from '../utils/logger' -import { mkdirSafe } from '../utils/fs-safe' +import { mkdirSafe, writeFileAtomic } from '../utils/fs-safe' import { getWorkspacePath } from '@opencode-manager/shared/config/env' import { normalizeToBaseUrl, @@ -124,15 +124,7 @@ async function cacheAudio(cacheKey: string, audioData: Buffer): Promise { await cleanupOldestFiles(audioData.length) } - const tempPath = join(TTS_CACHE_DIR, `${cacheKey}.${process.pid}.${randomUUID()}.tmp`) - - try { - await writeFile(tempPath, audioData) - await rename(tempPath, filePath) - } catch (error) { - await unlink(tempPath).catch(() => undefined) - throw error - } + await writeFileAtomic(filePath, audioData) } class TTSUpstreamError extends Error { diff --git a/backend/src/utils/fs-safe.ts b/backend/src/utils/fs-safe.ts index 6a591ad0..1e70c180 100644 --- a/backend/src/utils/fs-safe.ts +++ b/backend/src/utils/fs-safe.ts @@ -26,7 +26,7 @@ export function canonicalPathSync(target: string): string { } } -export async function writeFileAtomic(filePath: string, content: string, options: { mode?: number } = {}): Promise { +export async function writeFileAtomic(filePath: string, content: string | Buffer, options: { mode?: number } = {}): Promise { const dir = path.dirname(filePath) await mkdirSafe(dir) const tempPath = path.join(dir, `.${path.basename(filePath)}.ocm-tmp-${process.pid}-${Date.now()}`) diff --git a/backend/test/routes/tts.test.ts b/backend/test/routes/tts.test.ts index 8a0bcedd..54a2a501 100644 --- a/backend/test/routes/tts.test.ts +++ b/backend/test/routes/tts.test.ts @@ -3,16 +3,19 @@ import * as fs from 'fs/promises' vi.mock('fs/promises', () => ({ readFile: vi.fn(), - writeFile: vi.fn(), readdir: vi.fn(), stat: vi.fn(), unlink: vi.fn(), - rename: vi.fn(), +})) + +const { mockWriteFileAtomic } = vi.hoisted(() => ({ + mockWriteFileAtomic: vi.fn(), })) vi.mock('../../src/utils/fs-safe', () => ({ mkdirSafe: vi.fn().mockResolvedValue(undefined), mkdirSyncSafe: vi.fn(), + writeFileAtomic: mockWriteFileAtomic, })) vi.mock('bun:sqlite', () => ({ @@ -57,9 +60,6 @@ const mockUnlink = fs.unlink as any import { createTTSRoutes, cleanupExpiredCache, getCacheStats, generateCacheKey, ensureCacheDir, getCachedAudio, getCacheSize, cleanupOldestFiles, cacheAudio, clearCache } from '../../src/routes/tts' -const mockWriteFile = fs.writeFile as any -const mockRename = (fs as any).rename as any - function createTtsConfig(overrides: Record = {}) { return { enabled: true, @@ -185,8 +185,8 @@ describe('TTS Routes', () => { await expect(clearCache()).rejects.toThrow('denied') }) - it('should leave .tmp and unrelated files untouched', async () => { - mockReaddir.mockResolvedValue(['a.mp3', 'pending.tmp', 'notes.txt'] as any) + it('should leave .tmp, atomic temp and unrelated files untouched', async () => { + mockReaddir.mockResolvedValue(['a.mp3', 'pending.tmp', '.a.mp3.ocm-tmp-1-2', 'notes.txt'] as any) mockUnlink.mockResolvedValue(undefined) const cleared = await clearCache() @@ -404,8 +404,7 @@ describe('TTS route handlers', () => { it('synthesizes and caches audio on a cache miss', async () => { mockStat.mockRejectedValue(new Error('not found')) mockReaddir.mockResolvedValue([] as any) - mockWriteFile.mockResolvedValue(undefined) - mockRename.mockResolvedValue(undefined) + mockWriteFileAtomic.mockResolvedValue(undefined) const fetchMock = vi.fn().mockResolvedValue({ ok: true, arrayBuffer: async () => Buffer.from('fresh-audio'), @@ -429,21 +428,16 @@ describe('TTS route handlers', () => { signal: expect.any(AbortSignal), }), ) - expect(mockWriteFile).toHaveBeenCalledWith( - expect.stringMatching(/\.tmp$/), + expect(mockWriteFileAtomic).toHaveBeenCalledWith( + expect.stringMatching(/\.mp3$/), expect.any(Buffer), ) - expect(mockRename).toHaveBeenCalledWith( - expect.stringMatching(/\.tmp$/), - expect.stringContaining('.mp3'), - ) }) it('returns synthesized audio when the cache write fails', async () => { mockStat.mockRejectedValue(new Error('not found')) mockReaddir.mockResolvedValue([] as any) - mockWriteFile.mockRejectedValue(new Error('disk full')) - mockUnlink.mockResolvedValue(undefined) + mockWriteFileAtomic.mockRejectedValue(new Error('disk full')) const fetchMock = vi.fn().mockResolvedValue({ ok: true, arrayBuffer: async () => Buffer.from('fresh-audio'), @@ -464,8 +458,7 @@ describe('TTS route handlers', () => { it('deduplicates concurrent synthesis requests for the same text', async () => { mockStat.mockRejectedValue(new Error('not found')) mockReaddir.mockResolvedValue([] as any) - mockWriteFile.mockResolvedValue(undefined) - mockRename.mockResolvedValue(undefined) + mockWriteFileAtomic.mockResolvedValue(undefined) let resolveFetch: ((value: { ok: boolean; arrayBuffer: () => Promise }) => void) | undefined const fetchMock = vi.fn().mockImplementation( @@ -504,8 +497,7 @@ describe('TTS route handlers', () => { it('does not share an in-flight synthesis across different endpoints or API keys', async () => { mockStat.mockRejectedValue(new Error('not found')) mockReaddir.mockResolvedValue([] as any) - mockWriteFile.mockResolvedValue(undefined) - mockRename.mockResolvedValue(undefined) + mockWriteFileAtomic.mockResolvedValue(undefined) mockGetSettings.mockImplementation((userId: string) => ({ preferences: { tts: createTtsConfig( @@ -550,8 +542,7 @@ describe('TTS route handlers', () => { it('isolates the disk cache across providers for identical text', async () => { mockStat.mockRejectedValue(new Error('not found')) mockReaddir.mockResolvedValue([] as any) - mockWriteFile.mockResolvedValue(undefined) - mockRename.mockResolvedValue(undefined) + mockWriteFileAtomic.mockResolvedValue(undefined) mockGetSettings.mockImplementation((userId: string) => ({ preferences: { tts: createTtsConfig( @@ -708,7 +699,7 @@ describe('TTS route handlers', () => { }) it('clears the cache through the DELETE /cache route', async () => { - mockReaddir.mockResolvedValue(['a.mp3', 'b.mp3', 'pending.tmp'] as any) + mockReaddir.mockResolvedValue(['a.mp3', 'b.mp3', 'pending.tmp', '.a.mp3.ocm-tmp-1-2'] as any) mockUnlink.mockResolvedValue(undefined) const res = await app.request('/cache', { method: 'DELETE' }) @@ -747,66 +738,48 @@ describe('cacheAudio', () => { mockReaddir.mockResolvedValue(['big.mp3'] as any) mockStat.mockResolvedValue({ size: 200 * 1024 * 1024, mtimeMs: 1000 } as any) mockUnlink.mockResolvedValue(undefined) - mockWriteFile.mockResolvedValue(undefined) - mockRename.mockResolvedValue(undefined) + mockWriteFileAtomic.mockResolvedValue(undefined) await cacheAudio('cache-key', Buffer.from('audio')) expect(mockUnlink).toHaveBeenCalledWith(expect.stringContaining('big.mp3')) - expect(mockWriteFile).toHaveBeenCalledWith( - expect.stringMatching(/\.tmp$/), - expect.any(Buffer), - ) - expect(mockRename).toHaveBeenCalledWith( - expect.stringMatching(/\.tmp$/), + expect(mockWriteFileAtomic).toHaveBeenCalledWith( expect.stringContaining('cache-key.mp3'), + expect.any(Buffer), ) }) it('writes audio without cleanup when the cache has room', async () => { mockReaddir.mockResolvedValue(['small.mp3'] as any) mockStat.mockResolvedValue({ size: 1024, mtimeMs: 1000 } as any) - mockWriteFile.mockResolvedValue(undefined) - mockRename.mockResolvedValue(undefined) + mockWriteFileAtomic.mockResolvedValue(undefined) await cacheAudio('cache-key', Buffer.from('audio')) expect(mockUnlink).not.toHaveBeenCalled() - expect(mockWriteFile).toHaveBeenCalledWith( - expect.stringMatching(/\.tmp$/), - expect.any(Buffer), - ) - expect(mockRename).toHaveBeenCalledWith( - expect.stringMatching(/\.tmp$/), + expect(mockWriteFileAtomic).toHaveBeenCalledWith( expect.stringContaining('cache-key.mp3'), + expect.any(Buffer), ) }) - it('writes to a unique temp path and renames it to the final .mp3 path', async () => { + it('writes the audio buffer atomically to the final .mp3 path', async () => { mockReaddir.mockResolvedValue([] as any) - mockWriteFile.mockResolvedValue(undefined) - mockRename.mockResolvedValue(undefined) + mockWriteFileAtomic.mockResolvedValue(undefined) await cacheAudio('cache-key', Buffer.from('audio')) - const [writtenPath] = mockWriteFile.mock.calls[0] - const [renamedFrom, renamedTo] = mockRename.mock.calls[0] + expect(mockWriteFileAtomic).toHaveBeenCalledTimes(1) + const [writtenPath, writtenData] = mockWriteFileAtomic.mock.calls[0]! - expect(writtenPath).toContain('cache-key.') - expect(writtenPath).toMatch(/\.tmp$/) - expect(writtenPath).not.toContain('.mp3') - expect(renamedFrom).toBe(writtenPath) - expect(renamedTo).toContain('cache-key.mp3') + expect(writtenPath).toContain('cache-key.mp3') + expect(writtenData).toEqual(Buffer.from('audio')) }) - it('removes the temp file and rethrows when the rename fails', async () => { + it('propagates an atomic write failure', async () => { mockReaddir.mockResolvedValue([] as any) - mockWriteFile.mockResolvedValue(undefined) - mockRename.mockRejectedValue(new Error('rename failed')) - mockUnlink.mockResolvedValue(undefined) + mockWriteFileAtomic.mockRejectedValue(new Error('rename failed')) await expect(cacheAudio('cache-key', Buffer.from('audio'))).rejects.toThrow('rename failed') - - expect(mockUnlink).toHaveBeenCalledWith(expect.stringMatching(/\.tmp$/)) }) }) diff --git a/backend/test/utils/fs-safe.test.ts b/backend/test/utils/fs-safe.test.ts index 61b75494..ed46689d 100644 --- a/backend/test/utils/fs-safe.test.ts +++ b/backend/test/utils/fs-safe.test.ts @@ -2,7 +2,7 @@ import { mkdtemp, readdir, readFile, realpath, rm, stat, writeFile } from 'node: import { tmpdir } from 'node:os' import path from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' -import { canonicalPath, canonicalPathSync, writeFileAtomicSync } from '../../src/utils/fs-safe' +import { canonicalPath, canonicalPathSync, writeFileAtomic, writeFileAtomicSync } from '../../src/utils/fs-safe' describe('canonicalPath', () => { let workDir = '' @@ -67,3 +67,25 @@ describe('writeFileAtomicSync', () => { await expect(readdir(workDir)).resolves.toEqual(['file.txt']) }) }) + +describe('writeFileAtomic', () => { + let workDir = '' + + beforeEach(async () => { + workDir = await mkdtemp(path.join(tmpdir(), 'fs-safe-atomic-async-')) + }) + + afterEach(async () => { + await rm(workDir, { recursive: true, force: true }) + }) + + it('writes Buffer bytes unchanged and leaves no temp file behind', async () => { + const target = path.join(workDir, 'audio.mp3') + const bytes = Buffer.from([0xff, 0x00, 0xfe, 0x80]) + + await writeFileAtomic(target, bytes) + + expect(await readFile(target)).toEqual(bytes) + await expect(readdir(workDir)).resolves.toEqual(['audio.mp3']) + }) +})