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: preserve atomic files when write preparation fails

Adam Eivya731417c173f
diff --git a/server/lib/aiToolkit/internal/atomicWrite.js b/server/lib/aiToolkit/internal/atomicWrite.js
index 8b38b22873..bb1b24c2c9 100644
--- a/server/lib/aiToolkit/internal/atomicWrite.js
+++ b/server/lib/aiToolkit/internal/atomicWrite.js
@@ -1,11 +1,11 @@
/**
* Atomic file write helper for the aiToolkit.
*
- * Vendored copy of server/lib/fileUtils.js#atomicWrite so the toolkit stays
+ * Vendored copy of server/lib/fileCore.js#atomicWrite so the toolkit stays
* self-contained (no imports out to sibling PortOS modules — see AGENTS.md
* "AI Toolkit" section). Keep in sync with the upstream implementation:
*
- * PARITY REQUIREMENT: every change to server/lib/fileUtils.js#atomicWrite
+ * PARITY REQUIREMENT: every change to server/lib/fileCore.js#atomicWrite
* must be mirrored here and vice-versa. The critical invariant is the
* payload-coercion line: Buffer must pass through unchanged (JSON.stringify
* on a Buffer produces `{"type":"Buffer","data":[...]}`, corrupting binary
@@ -16,7 +16,7 @@
* a partial write. Accepts a string, a Buffer, or any JSON-serializable value.
*/
-import { mkdir, writeFile, rename, unlink } from 'fs/promises';
+import { chmod, mkdir, stat, writeFile, rename, unlink } from 'fs/promises';
import { randomUUID } from 'crypto';
import { dirname } from 'path';
@@ -49,12 +49,23 @@ export async function atomicWrite(filePath, data) {
// `{"type":"Buffer","data":[...]}` which corrupts binary writes (PNG, etc.).
const payload = typeof data === 'string' || Buffer.isBuffer(data) ? data : JSON.stringify(data, null, 2);
await ensureDir(dirname(filePath));
+ // Read permissions before creating the temp so a restricted destination is
+ // never replaced with a default-mode file. Only ENOENT means a fresh create.
+ const existingMode = await stat(filePath).then((s) => s.mode & 0o777, (err) => {
+ if (err.code === 'ENOENT') return null;
+ throw err;
+ });
const tmp = `${filePath}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`;
- await writeFile(tmp, payload);
- // Persistent Windows lock after the retry window: fail the write and leave the
- // destination untouched. Never move it aside to a `.bak` (#9564, mirrors #9556).
- const err = await renameWithWindowsRetries(tmp, filePath);
- if (!err) return;
- await unlink(tmp).catch(() => {});
- throw err;
+ try {
+ // A failed write may already have produced partial bytes; cleanup owns the
+ // entire preparation, including restoring mode bits tightened by umask.
+ await writeFile(tmp, payload, existingMode !== null ? { mode: existingMode } : undefined);
+ if (existingMode !== null) await chmod(tmp, existingMode);
+ // Persistent Windows lock: fail without moving the destination aside.
+ const err = await renameWithWindowsRetries(tmp, filePath);
+ if (err) throw err;
+ } catch (err) {
+ await unlink(tmp).catch(() => {});
+ throw err;
+ }
}
diff --git a/server/lib/aiToolkit/internal/atomicWrite.test.js b/server/lib/aiToolkit/internal/atomicWrite.test.js
index 867c6b20c6..ca6db158b0 100644
--- a/server/lib/aiToolkit/internal/atomicWrite.test.js
+++ b/server/lib/aiToolkit/internal/atomicWrite.test.js
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
+import { chmodSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import * as fsPromises from 'fs/promises';
@@ -10,6 +10,9 @@ vi.mock('fs/promises', async (importOriginal) => {
return {
...actual,
rename: vi.fn((...args) => actual.rename(...args)),
+ writeFile: vi.fn((...args) => actual.writeFile(...args)),
+ stat: vi.fn((...args) => actual.stat(...args)),
+ chmod: vi.fn((...args) => actual.chmod(...args)),
};
});
@@ -24,7 +27,6 @@ let restorePlatform = () => {};
beforeEach(() => {
tmpRoot = mkdtempSync(join(tmpdir(), 'ai-toolkit-atomicwrite-'));
- restorePlatform = pinPlatform('win32');
fsPromises.rename.mockClear();
fsPromises.rename.mockImplementation((...args) => realFsPromises.rename(...args));
});
@@ -32,12 +34,15 @@ beforeEach(() => {
afterEach(() => {
restorePlatform();
restorePlatform = () => {};
- fsPromises.rename.mockReset();
- fsPromises.rename.mockImplementation((...args) => realFsPromises.rename(...args));
+ for (const name of ['rename', 'writeFile', 'stat', 'chmod']) {
+ fsPromises[name].mockReset();
+ fsPromises[name].mockImplementation((...args) => realFsPromises[name](...args));
+ }
rmSync(tmpRoot, { recursive: true, force: true });
});
describe('AI Toolkit atomicWrite Windows lock retries', () => {
+ beforeEach(() => { restorePlatform = pinPlatform('win32'); });
it('succeeds when the lock clears within the retry window', async () => {
const target = join(tmpRoot, 'transient.json');
writeFileSync(target, '{"v":1}');
@@ -61,3 +66,54 @@ describe('AI Toolkit atomicWrite Windows lock retries', () => {
expect(readdirSync(tmpRoot)).toEqual(['persistent.json']);
});
});
+
+describe('AI Toolkit atomicWrite preparation failures', () => {
+ it('removes a partially written temp file and can retry without changing the original on failure', async () => {
+ const target = join(tmpRoot, 'state.json');
+ writeFileSync(target, 'original');
+ const failure = Object.assign(new Error('synthetic disk full'), { code: 'ENOSPC' });
+ fsPromises.writeFile.mockImplementationOnce(async (path, _data, options) => {
+ await realFsPromises.writeFile(path, 'partial', options);
+ throw failure;
+ });
+
+ await expect(atomicWrite(target, 'replacement')).rejects.toBe(failure);
+ expect(readFileSync(target, 'utf8')).toBe('original');
+ expect(readdirSync(tmpRoot)).toEqual(['state.json']);
+ await atomicWrite(target, 'replacement');
+ expect(readFileSync(target, 'utf8')).toBe('replacement');
+ expect(readdirSync(tmpRoot)).toEqual(['state.json']);
+ });
+
+ it.each(['stat', 'chmod'])('preserves the original when %s fails', async (operation) => {
+ const target = join(tmpRoot, 'private.json');
+ writeFileSync(target, 'original', { mode: 0o600 });
+ const failure = Object.assign(new Error(`synthetic ${operation} failure`), { code: 'EIO' });
+ fsPromises[operation].mockRejectedValueOnce(failure);
+
+ await expect(atomicWrite(target, 'replacement')).rejects.toBe(failure);
+ expect(readFileSync(target, 'utf8')).toBe('original');
+ expect(readdirSync(tmpRoot)).toEqual(['private.json']);
+ });
+
+ it.skipIf(process.platform === 'win32')('preserves an existing owner-only mode', async () => {
+ const target = join(tmpRoot, 'private.json');
+ writeFileSync(target, 'original');
+ chmodSync(target, 0o600);
+
+ await atomicWrite(target, 'replacement');
+
+ expect(readFileSync(target, 'utf8')).toBe('replacement');
+ expect(statSync(target).mode & 0o777).toBe(0o600);
+ });
+
+ it('creates a missing file and preserves binary bytes', async () => {
+ const target = join(tmpRoot, 'new.bin');
+ const bytes = Buffer.from([0, 128, 255]);
+
+ await atomicWrite(target, bytes);
+
+ expect(readFileSync(target)).toEqual(bytes);
+ expect(readdirSync(tmpRoot)).toEqual(['new.bin']);
+ });
+});
diff --git a/server/lib/fileCore.js b/server/lib/fileCore.js
index 3d7a9e9430..962d8b8199 100644
--- a/server/lib/fileCore.js
+++ b/server/lib/fileCore.js
@@ -254,12 +254,13 @@ export async function atomicWrite(filePath, data) {
// a local user could race-read it during the window). A trailing chmod pins
// the exact mode in case the umask tightened the create below it. New files
// (stat fails with ENOENT) keep the umask default, exactly as before.
- const existingMode = await stat(filePath).then((s) => s.mode & 0o777, () => null);
+ const existingMode = await stat(filePath).then((s) => s.mode & 0o777, (err) => {
+ // An unreadable mode is not an absent file: defaulting here could replace
+ // an owner-only store with a more permissive inode.
+ if (err.code === 'ENOENT') return null;
+ throw err;
+ });
const tmp = `${filePath}.${process.pid}.${Date.now()}.${randomUUID()}.tmp`;
- await writeFile(tmp, payload, existingMode !== null ? { mode: existingMode } : undefined);
- if (existingMode !== null) {
- await chmod(tmp, existingMode).catch(() => {});
- }
const renameWithWindowsRetries = async (from, to, {
attempts = WIN_RETRY_ATTEMPTS,
delayMs = WIN_RETRY_DELAY_MS,
@@ -292,6 +293,10 @@ export async function atomicWrite(filePath, data) {
throw err;
};
try {
+ // writeFile can create a partial temp file before rejecting (e.g. ENOSPC).
+ // Own cleanup from that first write, and publish only once its mode is set.
+ await writeFile(tmp, payload, existingMode !== null ? { mode: existingMode } : undefined);
+ if (existingMode !== null) await chmod(tmp, existingMode);
await replace();
} catch (err) {
await unlink(tmp).catch(() => {});
diff --git a/server/lib/fileUtils.test.js b/server/lib/fileUtils.test.js
index 8b894887de..6a4f823919 100644
--- a/server/lib/fileUtils.test.js
+++ b/server/lib/fileUtils.test.js
@@ -23,6 +23,7 @@ vi.mock('fs/promises', async (importOriginal) => {
readFile: vi.fn((...args) => actual.readFile(...args)),
rename: vi.fn((...args) => actual.rename(...args)),
writeFile: vi.fn((...args) => actual.writeFile(...args)),
+ chmod: vi.fn((...args) => actual.chmod(...args)),
readdir: vi.fn((...args) => actual.readdir(...args)),
stat: vi.fn((...args) => actual.stat(...args)),
};
@@ -101,7 +102,7 @@ describe('importFileToDir', () => {
});
const restoreFsMocks = () => {
- for (const name of ['readFile', 'rename', 'readdir', 'mkdir', 'stat']) {
+ for (const name of ['readFile', 'rename', 'readdir', 'mkdir', 'stat', 'writeFile', 'chmod']) {
fsPromises[name].mockReset();
fsPromises[name].mockImplementation((...args) => realFsPromises[name](...args));
}
@@ -1339,6 +1340,7 @@ describe('fileUtils', () => {
});
afterEach(() => {
+ restoreFsMocks();
rmSync(tmpRoot, { recursive: true, force: true });
});
@@ -1354,6 +1356,48 @@ describe('fileUtils', () => {
expect(await readFile(target, 'utf8')).toBe('{"x":1}\n');
});
+ it('removes a partially written temp file after ENOSPC and allows a later retry', async () => {
+ const target = join(tmpRoot, 'state.json');
+ writeFileSync(target, 'original');
+ const failure = Object.assign(new Error('synthetic disk full'), { code: 'ENOSPC' });
+ fsPromises.writeFile.mockImplementationOnce(async (path, _data, options) => {
+ await realFsPromises.writeFile(path, 'partial', options);
+ throw failure;
+ });
+
+ await expect(atomicWrite(target, 'replacement')).rejects.toBe(failure);
+ expect(readFileSync(target, 'utf8')).toBe('original');
+ expect(readdirSync(tmpRoot)).toEqual(['state.json']);
+
+ await atomicWrite(target, 'replacement');
+ expect(readFileSync(target, 'utf8')).toBe('replacement');
+ expect(readdirSync(tmpRoot)).toEqual(['state.json']);
+ });
+
+ it('preserves the destination when its permissions cannot be read', async () => {
+ const target = join(tmpRoot, 'private.json');
+ writeFileSync(target, 'original', { mode: 0o600 });
+ const originalMode = (await realFsPromises.stat(target)).mode;
+ const failure = Object.assign(new Error('synthetic stat failure'), { code: 'EIO' });
+ fsPromises.stat.mockRejectedValueOnce(failure);
+
+ await expect(atomicWrite(target, 'replacement')).rejects.toBe(failure);
+ expect(readFileSync(target, 'utf8')).toBe('original');
+ expect((await realFsPromises.stat(target)).mode).toBe(originalMode);
+ expect(readdirSync(tmpRoot)).toEqual(['private.json']);
+ });
+
+ it('preserves the destination and removes the temp file when restoring its mode fails', async () => {
+ const target = join(tmpRoot, 'private.json');
+ writeFileSync(target, 'original', { mode: 0o600 });
+ const failure = Object.assign(new Error('synthetic chmod failure'), { code: 'EACCES' });
+ fsPromises.chmod.mockRejectedValueOnce(failure);
+
+ await expect(atomicWrite(target, 'replacement')).rejects.toBe(failure);
+ expect(readFileSync(target, 'utf8')).toBe('original');
+ expect(readdirSync(tmpRoot)).toEqual(['private.json']);
+ });
+
it.skipIf(process.platform === 'win32')(
'preserves the destination file\'s restrictive mode on rewrite (regression: #1837)',
async () => {