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
upsert archived review items by id so a live copy never loses to a stale snapshot (#6853)
[._.]/ Adam Eivye21609924a3b
diff --git a/server/services/review.js b/server/services/review.jsindex dc95f13e63..ef10550770 100644--- a/server/services/review.js+++ b/server/services/review.js@@ -120,9 +120,10 @@ async function loadArchiveOrEmpty(context) {* items.json and into archive.json, at most once per `RETENTION_INTERVAL_MS`.* Writes the archive FIRST and only then returns the trimmed list for* `saveItems` to persist — so a crash between the two writes can duplicate an- * item across both files, never lose one. The dedupe-by-id below then makes- * that duplication self-heal on the very next retention pass instead of- * accumulating a second archive.json copy forever.+ * item across both files, never lose one. The archive write below upserts by+ * id with the live copy winning, so that duplication self-heals on the very+ * next retention pass — one archive entry per id, and never a stale archived+ * snapshot shadowing a live copy that was edited in the meantime.** Both the archive READ and the archive WRITE are isolated to THIS function* rather than left to throw: `saveItems` calls this on every@@ -156,20 +157,15 @@ async function applyRetention(items) {});if (!archive) return items;- const archivedIds = new Set(archive.map(i => i.id));- const fresh = toArchive.filter(i => !archivedIds.has(i.id));- if (fresh.length === 0) {- lastRetentionAt = now;- return remaining;- }-- const archived = await atomicWrite(ARCHIVE_FILE, [...archive, ...fresh]).then(() => true).catch((err) => {+ const merged = new Map(archive.map(i => [i.id, i]));+ for (const item of toArchive) merged.set(item.id, item); // live copy wins+ const archived = await atomicWrite(ARCHIVE_FILE, [...merged.values()]).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}`);+ console.log(`📦 Review items archived: ${toArchive.length}`);lastRetentionAt = now;return remaining;}diff --git a/server/services/review.test.js b/server/services/review.test.jsindex b263f9e8fb..f035ebb6c1 100644--- a/server/services/review.test.js+++ b/server/services/review.test.js@@ -585,19 +585,20 @@ describe('review service', () => {}});- it('does not write a duplicate archive entry when the same id is archived twice', async () => {+ it('upserts by id when the same item is archived twice — one archive entry, the live copy winning', async () => {vi.useFakeTimers();vi.setSystemTime(Date.now() + 1000 * 60 * 60 * 24 * 600);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 }+ { id: 'c-old', type: 'todo', title: 'edited live copy', status: 'completed', metadata: {}, createdAt: old, updatedAt: old }];// Models the crash-recovery case: a prior retention pass wrote the// archive but items.json was never rewritten, so the same eligible- // item resurfaces in items.json on this pass.- const existingArchive = [{ id: 'c-old', type: 'todo', status: 'completed', metadata: {}, createdAt: old, updatedAt: old }];+ // item resurfaces in items.json on this pass — edited since, so the+ // archived snapshot is the stale one.+ const existingArchive = [{ id: 'c-old', type: 'todo', title: 'stale snapshot', status: 'completed', metadata: {}, createdAt: old, updatedAt: old }];stat.mockResolvedValue({ mtimeMs: 821001, size: 1 });readFile.mockImplementation((path) =>@@ -605,9 +606,13 @@ describe('review service', () => {await createItem({ type: 'todo', title: 'trigger a save' });- // c-old is already archived — no second archive write for the same id.+ // One archive entry for c-old, and it is the live (newer) copy — not a+ // second entry, and not the stale archived snapshot.const archiveWrites = atomicWrite.mock.calls.filter(([path]) => String(path).endsWith('archive.json'));- expect(archiveWrites).toHaveLength(0);+ expect(archiveWrites).toHaveLength(1);+ const archivedCopies = archiveWrites[0][1].filter(i => i.id === 'c-old');+ expect(archivedCopies).toHaveLength(1);+ expect(archivedCopies[0].title).toBe('edited live copy');// 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'));