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: prevent stale cached store reads from reverting writes (#12063)
Adam Eivyb1b450f69d9b
diff --git a/server/lib/fileUtils.test.js b/server/lib/fileUtils.test.jsindex 4b21616e08..616b9f2751 100644--- a/server/lib/fileUtils.test.js+++ b/server/lib/fileUtils.test.js@@ -1773,6 +1773,71 @@ describe('createCachedStore', () => {expect(JSON.parse(await readFile(file, 'utf-8')).n).toBe(N);});+ it('does not let an in-flight stale read replace a persisted mutation', async () => {+ const file = join(dir, 'feed-state.json');+ const store = createCachedStore(file, { items: [] }, { ttl: 60_000 });+ await store.save({ items: [] });+ store.invalidateCache();++ let releaseRead;+ let signalReadStarted;+ const heldRead = new Promise((resolve) => { releaseRead = resolve; });+ const readStarted = new Promise((resolve) => { signalReadStarted = resolve; });+ fsPromises.readFile.mockImplementationOnce(async (...args) => {+ const bytes = await realFsPromises.readFile(...args);+ signalReadStarted();+ await heldRead;+ return bytes;+ });++ const staleRead = store.load();+ await readStarted;+ try {+ await store.mutate((data) => { data.items.push('first'); });+ releaseRead();+ await expect(staleRead).resolves.toEqual({ items: ['first'] });++ await store.mutate((data) => { data.items.push('second'); });+ expect(JSON.parse(await readFile(file, 'utf-8'))).toEqual({ items: ['first', 'second'] });+ } finally {+ releaseRead();+ restoreFsMocks();+ }+ });++ it('re-reads after an in-flight stale read races a persist and cache invalidation', async () => {+ const file = join(dir, 'invalidated-state.json');+ const store = createCachedStore(file, { items: [] }, { ttl: 60_000 });+ await store.save({ items: [] });+ store.invalidateCache();++ let releaseRead;+ let signalReadStarted;+ const heldRead = new Promise((resolve) => { releaseRead = resolve; });+ const readStarted = new Promise((resolve) => { signalReadStarted = resolve; });+ fsPromises.readFile.mockImplementationOnce(async (...args) => {+ const bytes = await realFsPromises.readFile(...args);+ signalReadStarted();+ await heldRead;+ return bytes;+ });++ const staleRead = store.load();+ await readStarted;+ try {+ await store.mutate((data) => { data.items.push('first'); });+ store.invalidateCache();+ releaseRead();+ await expect(staleRead).resolves.toEqual({ items: ['first'] });++ await store.mutate((data) => { data.items.push('second'); });+ expect(JSON.parse(await readFile(file, 'utf-8'))).toEqual({ items: ['first', 'second'] });+ } finally {+ releaseRead();+ restoreFsMocks();+ }+ });+it('mutate that returns undefined persists the mutated input', async () => {const store = createCachedStore(join(dir, 'x.json'), { items: [] });await store.mutate((data) => { data.items.push('a'); });diff --git a/server/lib/jsonIo.js b/server/lib/jsonIo.jsindex 3d14b535dc..51a7234550 100644--- a/server/lib/jsonIo.js+++ b/server/lib/jsonIo.js@@ -581,6 +581,7 @@ export async function writeJSONLines(filePath, values) {export function createCachedStore(filePath, defaultValue, { ttl = 2000, context = '' } = {}) {let cache = null;let cacheTimestamp = 0;+ let writeSeq = 0;const dir = dirname(filePath);const queueWrite = createFileWriteQueue();// Safe clone for plain JSON defaults (structuredClone requires Node 17+)@@ -589,6 +590,7 @@ export function createCachedStore(filePath, defaultValue, { ttl = 2000, contextconst load = async () => {const now = Date.now();if (cache && (now - cacheTimestamp) < ttl) return cache;+ const readSeq = writeSeq;await ensureDir(dir);// Strict (#4115): `mutate` is load → fn → persist over this value, so a// present-but-unreadable file must reject here rather than read as the@@ -598,6 +600,10 @@ export function createCachedStore(filePath, defaultValue, { ttl = 2000, contextif (!ok) {throw new Error(`Unreadable JSON file: ${filePath}${context ? ` (${context})` : ''}`);}+ // A serialized write may have completed while this disk read was in+ // flight. In that case the bytes we just read can predate the persisted+ // state, so keep and return the newer cache instead of replacing it.+ if (readSeq !== writeSeq) return cache === null ? load() : cache;cache = value === null ? cloneDefault() : value;cacheTimestamp = now;return cache;@@ -607,6 +613,7 @@ export function createCachedStore(filePath, defaultValue, { ttl = 2000, context// it via the serialized `save`/`mutate`, never directly.const persist = async (data) => {await atomicWrite(filePath, data);+ writeSeq += 1;cache = data;cacheTimestamp = Date.now();return data;