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
keep review-store reads swap-safe and seed the cache from a clone (#6853)
[._.]/ Adam Eivy353b17b13793
diff --git a/server/services/review.js b/server/services/review.jsindex 1d43fcc4b1..dc95f13e63 100644--- a/server/services/review.js+++ b/server/services/review.js@@ -48,8 +48,9 @@ const ARCHIVE_ELIGIBLE_STATUSES = new Set(['completed', 'dismissed']);// `saveItems` call. The internal mutation paths below (createItem/// updateItemStatus/bulkUpdateStatus/updateItem/deleteItem/// updateStatusByReferenceId) mutate the clone `loadItems()` gave them and-// then immediately call `saveItems` with it — `saveItems` adopts whatever-// array it is given as the new cache, so this costs nothing extra there.+// then immediately call `saveItems` with it — `saveItems` seeds the cache+// from a clone of what it wrote, so the objects those paths return to their+// callers are theirs to mutate too.let itemsCache = null; // { mtimeMs, size, items }let lastRetentionAt = 0; // 0 so the first save after boot always evaluates retention@@ -71,17 +72,19 @@ async function loadItems() {throw err;});- if (!stats) {- itemsCache = null;- return [];- }-- if (itemsCache && itemsCache.mtimeMs === stats.mtimeMs && itemsCache.size === stats.size) {+ if (stats && itemsCache && itemsCache.mtimeMs === stats.mtimeMs && itemsCache.size === stats.size) {return cloneItems(itemsCache.items);}+ // A stat ENOENT is NOT collapsed to `[]` here: on win32 an `atomicWrite`+ // swap in flight (temp file renamed over the target) reports ENOENT for a+ // moment too, and only readJSONFile's swap-aware retry can tell that apart+ // from a genuinely absent file (which it returns `[]` for). Short-circuiting+ // would hand a concurrent read-modify-write (a cosEvents `task:ready` burst)+ // an empty list to save over every stored item. Nothing is cached under an+ // unknown identity — the next read re-stats and re-parses.const items = await readJSONFile(ITEMS_FILE, [], { strict: true });- itemsCache = { mtimeMs: stats.mtimeMs, size: stats.size, items };+ itemsCache = stats ? { mtimeMs: stats.mtimeMs, size: stats.size, items } : null;return cloneItems(items);}@@ -180,7 +183,10 @@ async function saveItems(items) {const retained = await applyRetention(items);await atomicWrite(ITEMS_FILE, retained);const stats = await stat(ITEMS_FILE).catch(() => null);- itemsCache = stats ? { mtimeMs: stats.mtimeMs, size: stats.size, items: retained } : null;+ // Seed from a clone, not the caller's array: the mutation paths return the+ // objects they just saved to their callers, and the cache must never share+ // a reference with anything outside this module (see the cache comment).+ itemsCache = stats ? { mtimeMs: stats.mtimeMs, size: stats.size, items: cloneItems(retained) } : null;}/**diff --git a/server/services/review.test.js b/server/services/review.test.jsindex e085e16edc..b263f9e8fb 100644--- a/server/services/review.test.js+++ b/server/services/review.test.js@@ -352,10 +352,13 @@ describe('review service', () => {it('treats a missing items.json as an empty, cache-cleared list', async () => {const enoent = Object.assign(new Error('ENOENT'), { code: 'ENOENT' });stat.mockRejectedValueOnce(enoent);+ readFile.mockRejectedValueOnce(enoent);const whileMissing = await getPendingCounts();expect(whileMissing.total).toBe(0);- expect(readFile).not.toHaveBeenCalled();+ // The read still goes through the swap-aware reader (which returns [] for+ // a genuinely absent file) rather than short-circuiting on the stat alone.+ expect(readJSONFile).toHaveBeenCalledTimes(1);// The file reappears — the cleared cache must not keep reporting the// pre-deletion (or pre-existence) state.@@ -365,6 +368,25 @@ describe('review service', () => {expect(afterRecreate.total).toBe(1);});+ it('does not collapse a stat ENOENT to an empty list while the file is still readable (win32 atomicWrite swap window)', async () => {+ const enoent = Object.assign(new Error('ENOENT'), { code: 'ENOENT' });+ stat.mockRejectedValueOnce(enoent);+ readFile.mockResolvedValueOnce(JSON.stringify([{ id: 'a', type: 'todo', status: 'pending' }]));++ const midSwap = await getPendingCounts();+ expect(midSwap.total).toBe(1);++ // Nothing was cached under an unknown file identity — the next read re-parses.+ stat.mockResolvedValueOnce({ mtimeMs: 813101, size: 1 });+ readFile.mockResolvedValueOnce(JSON.stringify([+ { id: 'a', type: 'todo', status: 'pending' },+ { id: 'b', type: 'todo', status: 'pending' }+ ]));+ const afterSwap = await getPendingCounts();+ expect(afterSwap.total).toBe(2);+ expect(readFile).toHaveBeenCalledTimes(2);+ });+it('reflects a write in the very next read without a second parse', async () => {stat.mockResolvedValueOnce({ mtimeMs: 814001, size: 1 }); // loadItems() inside createItemreadFile.mockResolvedValueOnce('[]');@@ -647,6 +669,18 @@ describe('review service', () => {expect(original.injected).toBeUndefined();expect(original.metadata.injected).toBeUndefined();});++ it('does not let a caller mutating a createItem() result pollute the cache seeded by that save', async () => {+ stat.mockResolvedValue({ mtimeMs: 829001, size: 1 });+ readFile.mockResolvedValue('[]');++ const created = await createItem({ type: 'todo', title: 'Original title' });+ created.title = 'mutated after return';++ // Same file identity → cache hit: the cached copy must not carry the mutation.+ const [cached] = await getItems({ status: 'pending' });+ expect(cached.title).toBe('Original title');+ });});describe('bulkUpdateStatus events', () => {