Skip to content

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

isolate archive.json corruption from the live review write path (#6853)

[._.]/ Adam Eivybad47ad36653
diff --git a/server/services/review.js b/server/services/review.js
index daffef4014..5047d929ad 100644
--- a/server/services/review.js
+++ b/server/services/review.js
@@ -95,13 +95,20 @@ async function loadArchive() {
* 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 (harmless, self-heals on the next read) but can
- * never lose one.
+ * 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.
+ *
+ * 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.
*/
async function applyRetention(items) {
const now = Date.now();
if (now - lastRetentionAt < RETENTION_INTERVAL_MS) return items;
- lastRetentionAt = now;
const cutoff = now - RETENTION_AGE_MS;
const remaining = [];
@@ -111,11 +118,24 @@ async function applyRetention(items) {
(eligible ? toArchive : remaining).push(item);
}
- if (toArchive.length === 0) return items;
+ if (toArchive.length === 0) {
+ lastRetentionAt = now;
+ return items;
+ }
- const archive = await loadArchive();
- await atomicWrite(ARCHIVE_FILE, [...archive, ...toArchive]);
- console.log(`📦 Review items archived: ${toArchive.length}`);
+ const archive = await loadArchive().catch((err) => {
+ console.error(`⚠️ Review archive unreadable, skipping retention this cycle: ${err.message}`);
+ return null;
+ });
+ 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) {
+ await atomicWrite(ARCHIVE_FILE, [...archive, ...fresh]);
+ console.log(`📦 Review items archived: ${fresh.length}`);
+ }
+ lastRetentionAt = now;
return remaining;
}
diff --git a/server/services/review.test.js b/server/services/review.test.js
index 700ae7d2c1..b611cf47d7 100644
--- a/server/services/review.test.js
+++ b/server/services/review.test.js
@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { readFile, readdir, stat } from 'fs/promises';
-import { atomicWrite } from '../lib/fileUtils.js';
+import { atomicWrite, readJSONFile } from '../lib/fileUtils.js';
vi.mock('fs/promises', () => ({
readFile: vi.fn(),
@@ -482,6 +482,79 @@ describe('review service', () => {
expect(counts.total).toBe(2); // p1 + the new item createItem just added
expect(readFile).toHaveBeenCalledTimes(2); // items.json once + archive.json once — no third parse
});
+
+ it('does not block a live write when archive.json is unreadable — retention is skipped, items.json still saves', async () => {
+ vi.useFakeTimers();
+ vi.setSystemTime(Date.now() + 1000 * 60 * 60 * 24 * 500);
+
+ 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 }
+ ];
+
+ // Drive readJSONFile directly, keyed on path rather than call count/
+ // order (items.json always resolves, archive.json always rejects) —
+ // simulates a corrupt archive.json, since the real
+ // readJSONFile(..., { strict: true }) throws on unparseable content
+ // rather than degrading to the [] fallback.
+ readJSONFile.mockImplementation((path) =>
+ String(path).endsWith('archive.json')
+ ? Promise.reject(new Error('Unreadable JSON file: archive.json'))
+ : Promise.resolve(fixture));
+
+ try {
+ const created = await createItem({ type: 'todo', title: 'trigger a save while archive is corrupt' });
+ expect(created.title).toBe('trigger a save while archive is corrupt');
+
+ // The live write was NOT blocked by the corrupt archive...
+ expect(atomicWrite).toHaveBeenCalledTimes(1);
+ expect(atomicWrite.mock.calls[0][0]).toMatch(/items\.json$/);
+ // ...and c-old was left in items.json rather than silently dropped,
+ // since retention could not safely move it anywhere.
+ const written = atomicWrite.mock.calls[0][1];
+ expect(written.some(i => i.id === 'c-old')).toBe(true);
+ } finally {
+ // This test overrides readJSONFile itself (not just readFile), so
+ // restore the shared default — later tests in this file rely on it
+ // delegating to the mocked readFile the normal way.
+ readJSONFile.mockImplementation(async (path, fallback) => {
+ try {
+ return JSON.parse(await readFile(path));
+ } catch {
+ return fallback;
+ }
+ });
+ }
+ });
+
+ it('does not write a duplicate archive entry when the same id is archived twice', 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 }
+ ];
+ // 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 }];
+
+ stat.mockResolvedValue({ mtimeMs: 821001, size: 1 });
+ readFile.mockImplementation((path) =>
+ Promise.resolve(String(path).endsWith('archive.json') ? JSON.stringify(existingArchive) : JSON.stringify(fixture)));
+
+ await createItem({ type: 'todo', title: 'trigger a save' });
+
+ // 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.
+ const itemsWrite = atomicWrite.mock.calls.find(([path]) => String(path).endsWith('items.json'));
+ expect(itemsWrite[1].some(i => i.id === 'c-old')).toBe(false);
+ });
});
describe('bulkUpdateStatus events', () => {