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: isolate cached store mutations until writes succeed
Adam Eivy60d1e777d7c4
diff --git a/server/lib/fileUtils.test.js b/server/lib/fileUtils.test.jsindex 0e6840939d..8b894887de 100644--- a/server/lib/fileUtils.test.js+++ b/server/lib/fileUtils.test.js@@ -1651,6 +1651,58 @@ describe('createCachedStore', () => {expect((await store.load()).items).toEqual(['a', 'b']);});+ it('keeps a rejected disk write out of cached reads and the next queued mutation', async () => {+ const file = join(dir, 'accounts.json');+ const store = createCachedStore(file, {}, { ttl: 60_000 });+ const original = { accounts: { example: { status: 'pending' } }, count: 0 };+ await store.save(structuredClone(original));+ const cached = await store.load();+ const diskError = Object.assign(new Error('synthetic replacement failure'), { code: 'EIO' });+ vi.mocked(fsPromises.rename).mockRejectedValueOnce(diskError);++ const rejected = store.mutate((data) => { delete data.accounts.example; });+ const rejection = expect(rejected).rejects.toBe(diskError);+ const next = store.mutate((data) => {+ expect(data.accounts.example).toEqual({ status: 'pending' });+ data.count += 1;+ });+ await rejection;+ await next;++ expect(cached).toEqual(original);+ const expected = { accounts: { example: { status: 'pending' } }, count: 1 };+ expect(await store.load()).toEqual(expected);+ expect(JSON.parse(await readFile(file, 'utf-8'))).toEqual(expected);+ });++ it('hides an in-flight edit and discards it if the mutation callback rejects', async () => {+ const file = join(dir, 'accounts.json');+ const store = createCachedStore(file, {}, { ttl: 60_000 });+ const original = { accounts: { example: { status: 'pending' } }, count: 0 };+ await store.save(structuredClone(original));+ let signalStarted;+ let rejectEdit;+ const started = new Promise((resolve) => { signalStarted = resolve; });+ const editing = new Promise((_resolve, reject) => { rejectEdit = reject; });+ const failed = store.mutate(async (data) => {+ data.accounts.example.status = 'uncommitted';+ signalStarted();+ await editing;+ });+ const callbackError = new Error('synthetic callback failure');+ const rejection = expect(failed).rejects.toBe(callbackError);+ await started;+ const duringEdit = await store.load();+ rejectEdit(callbackError);+ await rejection;++ expect(duringEdit).toEqual(original);+ expect(await store.load()).toEqual(original);+ expect(JSON.parse(await readFile(file, 'utf-8'))).toEqual(original);+ await store.mutate((data) => { data.count += 1; });+ expect(JSON.parse(await readFile(file, 'utf-8'))).toEqual({ ...original, count: 1 });+ });+it('invalidateCache forces a re-read from disk', async () => {const file = join(dir, 'x.json');const store = createCachedStore(file, { n: 0 });diff --git a/server/lib/jsonIo.js b/server/lib/jsonIo.jsindex 7d563c637b..3d14b535dc 100644--- a/server/lib/jsonIo.js+++ b/server/lib/jsonIo.js@@ -572,6 +572,9 @@ export async function writeJSONLines(filePath, values) {* `load → fn → persist` cycle under the same tail, so a `load` always sees the* previous cycle's committed result and one writer can't clobber the other* (AGENTS.md: "serialize writes server-side… a single tail per shared file").+ * `mutate` edits a private clone and publishes it to the cache only after the+ * atomic write succeeds. A rejected callback or write leaves committed reads+ * intact and cannot leak rejected changes into the next queued mutation.* A bare `load()` + `save()` pair does NOT get that guarantee; reach for* `mutate()` whenever the new value depends on the current one.*/@@ -615,7 +618,7 @@ export function createCachedStore(filePath, defaultValue, { ttl = 2000, context// `fn`, then persist. `fn` may mutate the loaded object in place and/or return// the value to write (returning `undefined` persists the mutated input).const mutate = (fn) => queueWrite(async () => {- const data = await load();+ const data = structuredClone(await load());const result = await fn(data);return persist(result === undefined ? data : result);});