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: narrow the ENOENT swap probe to .bak so a first-ever write isn't mistaken for a vanished file (#4095)

[._.]/ Adam Eivy8752fa1e2f86
diff --git a/server/lib/fileUtils.js b/server/lib/fileUtils.js
index 954b1318c3..061eb8a427 100644
--- a/server/lib/fileUtils.js
+++ b/server/lib/fileUtils.js
@@ -516,13 +516,23 @@ export async function readFileTail(path, maxBytes) {
const PARSE_FAILED = Symbol('json-parse-failed');
/**
- * True when a `<filePath>.*.tmp` or `<filePath>.*.bak` sibling is on disk — the
- * signature of an `atomicWrite` temp write or backup swap that is still in
- * flight for THIS file. Non-throwing: an unreadable parent directory just means
- * "no evidence of a swap".
- *
- * Only consulted on win32, and only after a read already failed with ENOENT, so
- * the directory listing never lands on a POSIX read or on a successful read.
+ * True when a `<filePath>.*.bak` sibling is on disk — the signature of an
+ * `atomicWrite` backup swap that is mid-flight for THIS file, i.e. the only
+ * window in which the destination legitimately vanishes.
+ *
+ * Deliberately does NOT match `.tmp`: `atomicWrite` writes its temp file on
+ * EVERY write, including the first-ever create of a file that has no
+ * destination yet. Treating a `.tmp` sibling as evidence of a swap would turn a
+ * plain "not written yet" read into a retry that waits for the in-flight write
+ * and returns its brand-new contents — a different value than the read was
+ * entitled to, and a behavior change on POSIX-shaped callers. The `.bak` only
+ * exists between the swap's two renames.
+ *
+ * Non-throwing: an unreadable parent directory just means "no evidence of a
+ * swap". Only consulted on win32, and only after a read already failed with
+ * ENOENT, so the directory listing never lands on a POSIX read or a successful
+ * one. A `.bak` orphaned by a crash mid-swap costs the bounded retry budget
+ * (~50ms) on subsequent ENOENT reads of that path, never an incorrect answer.
*
* Matching is case-INSENSITIVE: NTFS/FAT are case-insensitive, so the reader's
* `filePath` casing need not match the casing the writer used to create the
@@ -538,7 +548,7 @@ async function hasSwapSibling(filePath) {
const prefix = `${basename(filePath).toLowerCase()}.`;
return entries.some((entry) => {
const name = entry.toLowerCase();
- return name.startsWith(prefix) && (name.endsWith('.tmp') || name.endsWith('.bak'));
+ return name.startsWith(prefix) && name.endsWith('.bak');
});
}
@@ -551,9 +561,10 @@ async function hasSwapSibling(filePath) {
* On win32 two transient failures are retried with a short backoff:
* - EPERM/EACCES/EBUSY — the destination is momentarily locked (AV scan, the
* writer's own open handle).
- * - ENOENT, but ONLY when `hasSwapSibling` shows a swap is in flight. A file
- * that was simply never written stays a zero-cost, silent "nothing here
- * yet": one failed read, no retry, no sleep.
+ * - ENOENT, but ONLY when `hasSwapSibling` shows a backup swap is mid-flight.
+ * A file that was simply never written — or one whose first-ever write is
+ * still in its temp stage — stays a silent "nothing here yet": one failed
+ * read, no retry, no sleep.
*
* Rejects with the last error once the attempts are exhausted, so the caller's
* existing errno handling is unchanged.
diff --git a/server/lib/fileUtils.test.js b/server/lib/fileUtils.test.js
index 2bd6a25f1a..9f1342cf69 100644
--- a/server/lib/fileUtils.test.js
+++ b/server/lib/fileUtils.test.js
@@ -66,6 +66,39 @@ import { fileURLToPath } from 'url';
const __dirname_test = dirname(fileURLToPath(import.meta.url));
+// The unmocked fs/promises, used to restore the delegating mock implementations
+// after a test installs a persistent one.
+let realFsPromises;
+beforeAll(async () => {
+ realFsPromises = await vi.importActual('fs/promises');
+});
+
+const restoreFsMocks = () => {
+ for (const name of ['readFile', 'rename', 'readdir', 'mkdir']) {
+ fsPromises[name].mockReset();
+ fsPromises[name].mockImplementation((...args) => realFsPromises[name](...args));
+ }
+};
+
+/**
+ * Run `fn` with EVERY `readFile` attempt rejected by `err`, then restore the
+ * delegating mock.
+ *
+ * A single `mockRejectedValueOnce` is not enough since #4095: on win32
+ * `readJSONFileStrict` retries a transient lock, so the first attempt would
+ * consume the queued rejection and the retry would read the real file. A test
+ * pinning "an unreadable file is not an empty one" has to keep it unreadable —
+ * which is also the contract that actually survives the retries.
+ */
+async function withUnreadableFile(err, fn) {
+ fsPromises.readFile.mockImplementation(() => Promise.reject(err));
+ try {
+ return await fn();
+ } finally {
+ restoreFsMocks();
+ }
+}
+
describe('fileUtils', () => {
describe('isValidJSON', () => {
it('should return true for valid JSON object', () => {
@@ -383,9 +416,8 @@ describe('fileUtils', () => {
it('reports EACCES as NOT ok — an unreadable file is not an empty one', async () => {
const filePath = join(testDir, 'locked.json');
await writeFile(filePath, '{"sessions": [{"id": "s1"}]}');
- fsPromises.readFile.mockRejectedValueOnce(eacces());
- const result = await readJSONFileStrict(filePath, { sessions: [] });
+ const result = await withUnreadableFile(eacces(), () => readJSONFileStrict(filePath, { sessions: [] }));
// `value` still carries the default so an ok-indifferent caller behaves as
// before; `ok: false` is what makes the failure legible.
expect(result).toEqual({ ok: false, value: { sessions: [] } });
@@ -435,8 +467,8 @@ describe('fileUtils', () => {
// Both return the same `value`; only `ok` tells them apart — which is why the
// parse sentinel can't be an in-band marker like null.
expect(await readJSONFileStrict(filePath, { sessions: [] })).toEqual({ ok: true, value: { sessions: [] } });
- fsPromises.readFile.mockRejectedValueOnce(eacces());
- expect(await readJSONFileStrict(filePath, { sessions: [] })).toEqual({ ok: false, value: { sessions: [] } });
+ expect(await withUnreadableFile(eacces(), () => readJSONFileStrict(filePath, { sessions: [] })))
+ .toEqual({ ok: false, value: { sessions: [] } });
});
it('keeps readJSONFile’s noisy-output extraction for array defaults', async () => {
@@ -509,10 +541,10 @@ describe('fileUtils', () => {
it('throws for an unreadable file instead of returning a fake empty', async () => {
const filePath = join(testDir, 'locked.json');
await writeFile(filePath, '{"sessions": []}');
- fsPromises.readFile.mockRejectedValueOnce(Object.assign(new Error('permission denied'), { code: 'EACCES' }));
+ const eacces = Object.assign(new Error('permission denied'), { code: 'EACCES' });
- await expect(readJSONFile(filePath, { sessions: [] }, { strict: true }))
- .rejects.toThrow(/Unreadable JSON file/);
+ await withUnreadableFile(eacces, () => expect(readJSONFile(filePath, { sessions: [] }, { strict: true }))
+ .rejects.toThrow(/Unreadable JSON file/));
});
it('throws for corrupt JSON', async () => {
@@ -1282,7 +1314,6 @@ describe('Windows swap-window retries (#4095)', () => {
const lockError = (code) => Object.assign(new Error(`${code}: simulated windows lock`), { code });
let tmpRoot;
- let realFs;
let restorePlatform = null;
const fakePlatform = (value) => {
@@ -1291,10 +1322,6 @@ describe('Windows swap-window retries (#4095)', () => {
restorePlatform = () => Object.defineProperty(process, 'platform', original);
};
- beforeAll(async () => {
- realFs = await vi.importActual('fs/promises');
- });
-
beforeEach(() => {
tmpRoot = mkdtempSync(join(tmpdir(), 'fileutils-winswap-'));
// Earlier describes in this file exercise the same fs mocks — start from a
@@ -1309,12 +1336,7 @@ describe('Windows swap-window retries (#4095)', () => {
restorePlatform = null;
// Drop any unconsumed `*Once` queues AND re-establish the delegating
// implementations the rest of this file relies on.
- fsPromises.readFile.mockReset();
- fsPromises.rename.mockReset();
- fsPromises.readdir.mockReset();
- fsPromises.readFile.mockImplementation((...args) => realFs.readFile(...args));
- fsPromises.rename.mockImplementation((...args) => realFs.rename(...args));
- fsPromises.readdir.mockImplementation((...args) => realFs.readdir(...args));
+ restoreFsMocks();
rmSync(tmpRoot, { recursive: true, force: true });
});
@@ -1396,13 +1418,26 @@ describe('Windows swap-window retries (#4095)', () => {
writeFileSync(target, JSON.stringify({ real: true }));
// The writer may have created the file (and therefore its swap debris)
// under different casing than the reader's path — legal on Windows.
- writeFileSync(join(tmpRoot, 'Casing.json.1234.5678.TMP'), 'partial');
+ writeFileSync(join(tmpRoot, 'Casing.json.1234.5678.BAK'), '{"real":true}');
fakePlatform('win32');
fsPromises.readFile.mockRejectedValueOnce(lockError('ENOENT'));
expect(await readJSONFileStrict(target, { fallback: true })).toEqual({ ok: true, value: { real: true } });
});
+ it('does NOT treat a .tmp sibling as a swap — a first-ever write is not a vanished file', async () => {
+ // `atomicWrite` writes a `.tmp` on EVERY write, including the first create
+ // of a file that has no destination yet. Retrying on that would make a
+ // legitimate "not written yet" read block for the in-flight write and
+ // return its brand-new contents. Only the `.bak` marks the swap window.
+ const missing = join(tmpRoot, 'first-write.json');
+ writeFileSync(`${missing}.4321.8765.tmp`, '{"brand":"new"}');
+ fakePlatform('win32');
+
+ expect(await readJSONFileStrict(missing, { fallback: true })).toEqual({ ok: true, value: { fallback: true } });
+ expect(fsPromises.readFile).toHaveBeenCalledTimes(1);
+ });
+
it('does not retry a plain missing file on win32 — no sibling, no second read', async () => {
const missing = join(tmpRoot, 'never-written.json');
fakePlatform('win32');