diff --git a/src/index.ts b/src/index.ts index 7d18a058..54355a97 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ * All operations are conversation-scoped: requests carry a sessionId and the * session's authoritative cwd comes from the session store. */ +import { randomUUID } from 'node:crypto' import { mkdir, open, readFile, rename, rm, stat, writeFile } from 'node:fs/promises' import { basename, dirname, extname, isAbsolute, join } from 'node:path' import type { IncomingMessage } from 'node:http' @@ -307,10 +308,14 @@ function buildApi( const { cwd } = await cwdOf(payload) const path = await ensureWorkspaceWritePath(cwd, requireString(payload, 'path'), fenceEnabledOf(getSettings)) const content = requireString(payload, 'content') - const tmp = `${path}.dsh-sidebar-tmp-${process.pid}` + // Per-request temp name (same pattern as writeWorkspaceUpload): a + // pid-suffixed name is shared by every concurrent save to the same + // path, letting two writers interleave into one temp file — and the + // failure-path rm could delete the other writer's in-flight temp. + const tmp = join(dirname(path), `.${basename(path)}.dsh-sidebar-tmp-${randomUUID()}.tmp`) try { await mkdir(dirname(path), { recursive: true }) - await writeFile(tmp, content, 'utf8') + await writeFile(tmp, content, { encoding: 'utf8', flag: 'wx' }) await rename(tmp, path) } catch (error) { await rm(tmp, { force: true }).catch(() => {}) diff --git a/tests/smoke.spec.ts b/tests/smoke.spec.ts index c8389472..9822c11c 100644 --- a/tests/smoke.spec.ts +++ b/tests/smoke.spec.ts @@ -533,6 +533,31 @@ describe('session cwd resolution over the API route', () => { } }) + it('concurrent fs.write calls to the same path do not corrupt each other', async () => { + const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-fs-concurrent-')) + const workspace = join(root, 'workspace') + mkdirSync(workspace) + try { + const route = mount({ sessions: { get: () => ({ header: { cwd: workspace } }) } }) + const target = join(workspace, 'notes.txt') + const draftA = 'A'.repeat(200000) + const draftB = 'B'.repeat(200000) + // Two editors of the same file ("open to the side" mints a second tab + // for one path) saving within the temp→rename window. Guards the + // contract: every concurrent save succeeds and the published file is + // one complete draft (never byte-mixed, never a failed rename). + const results = await Promise.allSettled([ + invoke(route, 'fs.write', { sessionId: 'concurrent', path: target, content: draftA }), + invoke(route, 'fs.write', { sessionId: 'concurrent', path: target, content: draftB }), + ]) + for (const result of results) expect(result.status).toBe('fulfilled') + const written = readFileSync(target, 'utf8') + expect([draftA, draftB]).toContain(written) + } finally { + rmSync(root, { recursive: true, force: true }) + } + }) + it('rejects media and HTML reads through a workspace symlink', async () => { if (!canCreateSymlink) return const root = mkdtempSync(join(tmpdir(), 'dsh-sidebar-route-symlink-security-'))