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
isolate archive reads and clone cached items to close two more review-store gaps (#6853)
[._.]/ Adam Eivy8cedf300851f
diff --git a/server/services/review.js b/server/services/review.jsindex 5047d929ad..1d43fcc4b1 100644--- a/server/services/review.js+++ b/server/services/review.js@@ -40,21 +40,25 @@ const ARCHIVE_ELIGIBLE_STATUSES = new Set(['completed', 'dismissed']);// re-stats after every write and seeds the cache directly from what it just// wrote, so a read immediately following a write is a cache hit, not a re-parse.//-// Every mutation path below (createItem/updateItemStatus/bulkUpdateStatus/-// updateItem/deleteItem/updateStatusByReferenceId) mutates the array/items-// returned by `loadItems()` in place and then immediately calls `saveItems`-// with that same array — never mutates without saving. That invariant is what-// makes it safe for `loadItems()` to hand back the cached array/objects-// directly instead of deep-cloning on every read: a caller either treats it as-// read-only, or mutates it and persists the result in the same call, which-// `saveItems` then adopts as the new cache. Keep that invariant true for any-// new mutation path.+// `loadItems()` hands every caller a fresh shallow clone (see `cloneItems`),+// never the cached array/objects themselves — `getItems` is a public export+// with callers outside this module's own mutate-then-save discipline, and a+// caller that decorated or edited a returned item in place would otherwise+// silently pollute the cache, later persisted by the next unrelated+// `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.let itemsCache = null; // { mtimeMs, size, items }let lastRetentionAt = 0; // 0 so the first save after boot always evaluates retention+const cloneItems = (items) => items.map(i => ({ ...i, metadata: { ...(i.metadata || {}) } }));+/*** Load all review items from file, reusing the cached parse when the file's- * mtime/size haven't changed since the last read.+ * mtime/size haven't changed since the last read. Always returns a clone —+ * see the cache comment above.*/// STRICT (#4115): `getPendingCounts()` reduces this list into the Review Hub's// total/alert/todo/briefing/cos tiles, so a swallowed unreadable read reports a@@ -73,23 +77,41 @@ async function loadItems() {}if (itemsCache && itemsCache.mtimeMs === stats.mtimeMs && itemsCache.size === stats.size) {- return itemsCache.items;+ return cloneItems(itemsCache.items);}const items = await readJSONFile(ITEMS_FILE, [], { strict: true });itemsCache = { mtimeMs: stats.mtimeMs, size: stats.size, items };- return items;+ return cloneItems(items);}/*** Load the archived (retired) items. Never cached — only read from the* history views (`getItems` for a completed/dismissed/unfiltered query,* and `createItem`'s duplicate-alert check when the live scan misses).+ * Strict: a caller that must not silently treat "unreadable" as "empty"+ * (`applyRetention`, before it trusts the file enough to write over it)+ * awaits this directly. A read-only view that can safely degrade instead+ * uses `loadArchiveOrEmpty`.*/async function loadArchive() {return readJSONFile(ARCHIVE_FILE, [], { strict: true });}+/**+ * Read-only archive access for history views: on a corrupt/unreadable+ * archive.json, log and continue without archived items rather than 500ing+ * every completed/dismissed/unfiltered review read. Never used by the write+ * path (`applyRetention`), which must not mistake "unreadable" for "empty"+ * and overwrite real archived history with just the newest batch.+ */+async function loadArchiveOrEmpty(context) {+ return loadArchive().catch((err) => {+ console.error(`⚠️ Review archive unreadable (${context}), continuing without archived items: ${err.message}`);+ return [];+ });+}+/*** Move completed/dismissed items older than `RETENTION_AGE_MS` out of* items.json and into archive.json, at most once per `RETENTION_INTERVAL_MS`.@@ -99,12 +121,14 @@ async function loadArchive() {* that duplication self-heal on the very next retention pass instead of* accumulating a second archive.json copy forever.*- * An unreadable archive.json is isolated to THIS function rather than left to- * throw: `saveItems` calls this on every create/status-flip/bulk/delete, so a- * corrupt archive.json must not block every live review write. On that- * failure this returns `items` untouched (items.json is not rewritten either)- * and leaves `lastRetentionAt` alone so the very next write retries retention- * instead of waiting out the full interval against a file nobody fixed yet.+ * Both the archive READ and the archive WRITE are isolated to THIS function+ * rather than left to throw: `saveItems` calls this on every+ * create/status-flip/bulk/delete, so a corrupt archive.json — or a failed+ * write to it (disk full, permission error) — must not block every live+ * review write. Either failure returns `items` untouched (items.json is not+ * rewritten either) and leaves `lastRetentionAt` alone, so the very next+ * write retries retention instead of waiting out the full interval against a+ * problem nobody has fixed yet.*/async function applyRetention(items) {const now = Date.now();@@ -131,10 +155,18 @@ async function applyRetention(items) {const archivedIds = new Set(archive.map(i => i.id));const fresh = toArchive.filter(i => !archivedIds.has(i.id));- if (fresh.length > 0) {- await atomicWrite(ARCHIVE_FILE, [...archive, ...fresh]);- console.log(`📦 Review items archived: ${fresh.length}`);+ if (fresh.length === 0) {+ lastRetentionAt = now;+ return remaining;}++ const archived = await atomicWrite(ARCHIVE_FILE, [...archive, ...fresh]).then(() => true).catch((err) => {+ console.error(`⚠️ Failed to write review archive, skipping retention this cycle: ${err.message}`);+ return false;+ });+ if (!archived) return items;++ console.log(`📦 Review items archived: ${fresh.length}`);lastRetentionAt = now;return remaining;}@@ -154,12 +186,19 @@ async function saveItems(items) {/*** Get all review items, sorted by type then creation date (newest first).* A completed/dismissed or unfiltered query also merges in archive.json —- * archive never holds a pending item, so a pending-only query skips it.+ * archive never holds a pending item, so a pending-only query skips it. A+ * corrupt archive.json degrades to "no archived items" rather than failing+ * the whole read (`loadArchiveOrEmpty`) — unlike the retention write path,+ * a read can safely omit history it could not trust. Deduped by id+ * (live copy wins) in case a crash left the same item in both files+ * mid-retention; see `applyRetention`'s doc comment.*/export async function getItems({ status, type } = {}) {let items = await loadItems();if (!status || ARCHIVE_ELIGIBLE_STATUSES.has(status)) {- items = items.concat(await loadArchive());+ const archived = await loadArchiveOrEmpty('getItems');+ const liveIds = new Set(items.map(i => i.id));+ items = items.concat(archived.filter(i => !liveIds.has(i.id)));}if (status) items = items.filter(i => i.status === status);if (type) items = items.filter(i => i.type === type);@@ -200,14 +239,16 @@ export async function createItem({ type, title, description = '', metadata = {}// window in practice — but check it anyway rather than silently narrowing// the dedup window the day an item crosses into archive.json. Only// consulted when the live scan misses, so the common case (no dedup match,- // or a live match) never touches the archive file.+ // or a live match) never touches the archive file. A corrupt archive.json+ // degrades to "no archived duplicate found" (`loadArchiveOrEmpty`) rather+ // than blocking item creation entirely.if (type === 'alert' && metadata?.referenceId) {const oneDayAgo = Date.now() - 24 * 60 * 60 * 1000;const isDuplicate = (i) =>i.type === 'alert' &&i.metadata?.referenceId === metadata.referenceId &&new Date(i.createdAt).getTime() > oneDayAgo;- const duplicate = items.find(isDuplicate) ?? (await loadArchive()).find(isDuplicate);+ const duplicate = items.find(isDuplicate) ?? (await loadArchiveOrEmpty('createItem duplicate check')).find(isDuplicate);if (duplicate) return duplicate;}diff --git a/server/services/review.test.js b/server/services/review.test.jsindex b611cf47d7..e085e16edc 100644--- a/server/services/review.test.js+++ b/server/services/review.test.js@@ -403,6 +403,41 @@ describe('review service', () => {const pendingView = await getItems({ status: 'pending' });expect(pendingView.map(i => i.id)).toEqual(['live-pending']);});++ it('dedupes by id when the same item is (transiently) in both files, preferring the live copy', async () => {+ stat.mockResolvedValue({ mtimeMs: 825001, size: 1 });+ // Models the crash-recovery window applyRetention's doc comment+ // describes: the same id present in both items.json and archive.json.+ const liveCopy = { id: 'dup-1', type: 'todo', status: 'completed', title: 'live version', createdAt: '2026-01-01T00:00:00Z' };+ const archiveCopy = { id: 'dup-1', type: 'todo', status: 'completed', title: 'archived version', createdAt: '2026-01-01T00:00:00Z' };+ readFile.mockImplementation((path) =>+ Promise.resolve(String(path).endsWith('archive.json') ? JSON.stringify([archiveCopy]) : JSON.stringify([liveCopy])));++ const result = await getItems({});+ expect(result).toHaveLength(1);+ expect(result[0].title).toBe('live version');+ });++ it('degrades to no archived items when archive.json is unreadable, instead of failing the whole read', async () => {+ stat.mockResolvedValue({ mtimeMs: 826001, size: 1 });+ readJSONFile.mockImplementation((path) =>+ String(path).endsWith('archive.json')+ ? Promise.reject(new Error('Unreadable JSON file: archive.json'))+ : Promise.resolve([{ id: 'live-1', type: 'todo', status: 'completed', createdAt: '2026-01-01T00:00:00Z' }]));++ try {+ const result = await getItems({ status: 'completed' });+ expect(result.map(i => i.id)).toEqual(['live-1']);+ } finally {+ readJSONFile.mockImplementation(async (path, fallback) => {+ try {+ return JSON.parse(await readFile(path));+ } catch {+ return fallback;+ }+ });+ }+ });});describe('createItem duplicate check reaches the archive', () => {@@ -551,10 +586,67 @@ describe('review service', () => {// c-old is already archived — no second archive write for the same id.const archiveWrites = atomicWrite.mock.calls.filter(([path]) => String(path).endsWith('archive.json'));expect(archiveWrites).toHaveLength(0);- // It still leaves items.json — it is stale/archived either way.+ // It is still dropped from items.json — it is archived either way, so+ // there is no reason to keep the stale live-side copy around.const itemsWrite = atomicWrite.mock.calls.find(([path]) => String(path).endsWith('items.json'));expect(itemsWrite[1].some(i => i.id === 'c-old')).toBe(false);});++ it('does not block a live write when the archive WRITE fails — retention is skipped, items.json still saves', async () => {+ vi.useFakeTimers();+ vi.setSystemTime(Date.now() + 1000 * 60 * 60 * 24 * 700);++ const now = Date.now();+ const old = new Date(now - 31 * 24 * 60 * 60 * 1000).toISOString();+ const fixture = [+ { id: 'c-old', type: 'todo', status: 'completed', metadata: {}, createdAt: old, updatedAt: old }+ ];++ stat.mockResolvedValue({ mtimeMs: 827001, size: 1 });+ readFile.mockImplementation((path) =>+ Promise.resolve(String(path).endsWith('archive.json') ? '[]' : JSON.stringify(fixture)));+ // The archive write itself fails (disk full, permission error, ...).+ atomicWrite.mockImplementationOnce(() => Promise.reject(new Error('ENOSPC: no space left on device')));++ const created = await createItem({ type: 'todo', title: 'trigger a save while archive write fails' });+ expect(created.title).toBe('trigger a save while archive write fails');++ // The archive write was attempted and failed (consuming the rejection+ // above), then the live write still landed as a second, successful+ // atomicWrite — with c-old left in items.json since retention could+ // not confirm the archive write actually succeeded.+ expect(atomicWrite).toHaveBeenCalledTimes(2);+ expect(atomicWrite.mock.calls[0][0]).toMatch(/archive\.json$/);+ expect(atomicWrite.mock.calls[1][0]).toMatch(/items\.json$/);+ expect(atomicWrite.mock.calls[1][1].some(i => i.id === 'c-old')).toBe(true);+ });+ });++ describe('read results do not leak a mutable cache reference', () => {+ it('does not let a caller mutating a getItems() result pollute a later unrelated save', async () => {+ stat.mockResolvedValue({ mtimeMs: 828001, size: 1 });+ // Persisting (not "once"): item 'a' is served identically whether a+ // later loadItems() call is a cache hit or a fresh parse, so this test+ // pins the actual property under test (no leaked mutation) rather than+ // an incidental call count.+ readFile.mockResolvedValue(JSON.stringify([+ { id: 'a', type: 'todo', status: 'pending', metadata: {} }+ ]));++ const items = await getItems({});+ // Simulates a caller decorating/editing a returned item in place —+ // must not reach the module's internal cache.+ items[0].injected = 'leaked';+ items[0].metadata.injected = 'leaked-meta';++ await createItem({ type: 'todo', title: 'unrelated new item' });++ const written = atomicWrite.mock.calls[0][1];+ const original = written.find(i => i.id === 'a');+ expect(original).toBeTruthy();+ expect(original.injected).toBeUndefined();+ expect(original.metadata.injected).toBeUndefined();+ });});describe('bulkUpdateStatus events', () => {