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: write CoS task files atomically so a crash can't truncate the task list
Adam Eivyd6cb9df0799e
diff --git a/server/services/cosTaskStore.js b/server/services/cosTaskStore.jsindex b1b625581d..45c58c441b 100644--- a/server/services/cosTaskStore.js+++ b/server/services/cosTaskStore.js@@ -17,7 +17,8 @@ import { getSkipReason } from './cosTaskClaim.js';import { DEVELOPMENT_ACTIVE_STATUSES, developmentWorkIdentity, sameDevelopmentWork } from '../lib/developmentWorkIdentity.js';import { reviewerModelsFromDefaults } from '../lib/reviewerConfig.js';-import { readFile, writeFile, stat } from 'fs/promises';+import { readFile, stat } from 'fs/promises';+import { atomicWrite } from '../lib/fileUtils.js';import { existsSync } from 'fs';import { join } from 'path';import { parseTasksMarkdown, groupTasksByStatus, getAutoApprovedTasks, getAwaitingApprovalTasks, generateTasksMarkdown, hasKnownPrefix, toRepresentableTask, PRIORITY_VALUES } from '../lib/taskParser.js';@@ -220,7 +221,11 @@ async function findTaskInFile(filePath, taskId) {*/async function writeTaskFile(filePath, markdown) {parsedTaskCache.delete(filePath);- await writeFile(filePath, markdown);+ // Temp-file + rename, never an in-place truncate: TASKS.md is the user's whole+ // task list, and a plain writeFile killed mid-write (PM2 restart, crash, full+ // disk) leaves it empty or half-written — which parses as "no tasks" and is+ // then persisted by the next write.+ await atomicWrite(filePath, markdown);}/** Test hook: forget every cached parse. */diff --git a/server/services/cosTaskStore.test.js b/server/services/cosTaskStore.test.jsindex 7721b9e9dc..991f5743e4 100644--- a/server/services/cosTaskStore.test.js+++ b/server/services/cosTaskStore.test.js@@ -28,6 +28,8 @@ const mock = vi.hoisted(() => ({mtimes: new Map(),// Counts real parses so the cache tests can prove a hit did no work.parseCalls: 0,+ // When set, atomicWrite rejects with it (a write that dies before the rename).+ atomicFailure: null,state: null,events: [],// Controls the mocked codeReview.js for resolveTaskChallengeWithRecheck (#2471).@@ -48,15 +50,26 @@ vi.mock('fs', async (importOriginal) => {};});+// Task files are written through atomicWrite (temp file + rename). The mock+// applies the same in-memory write the fs/promises writeFile mock used to, and+// `mock.atomicFailure` lets a test simulate the write dying before the rename.+vi.mock('../lib/fileUtils.js', async (importOriginal) => {+ const actual = await importOriginal();+ return {+ ...actual,+ atomicWrite: vi.fn(async (p, content) => {+ if (mock.atomicFailure) throw mock.atomicFailure;+ mock.files.set(p, content);+ mock.mtimes.set(p, (mock.mtimes.get(p) || 0) + 1000);+ })+ };+});+vi.mock('fs/promises', () => ({readFile: vi.fn(async (p) => {if (!mock.files.has(p)) throw new Error(`ENOENT: ${p}`);return mock.files.get(p);}),- writeFile: vi.fn(async (p, content) => {- mock.files.set(p, content);- mock.mtimes.set(p, (mock.mtimes.get(p) || 0) + 1000);- }),// The parsed-task cache stamps on mtimeMs + size, so the mock must model both.stat: vi.fn(async (p) => {if (!mock.files.has(p)) throw new Error(`ENOENT: ${p}`);@@ -142,6 +155,7 @@ const baseState = () => ({});beforeEach(() => {+ mock.atomicFailure = null;mock.files = new Map();mock.mtimes = new Map();mock.parseCalls = 0;@@ -632,6 +646,14 @@ describe('cosTaskStore.addTask', () => {expect(mock.events.some(e => e.name === 'tasks:changed' && e.payload.action === 'added' && e.payload.type === 'user')).toBe(true);});+ it('keeps the existing task list intact and surfaces the error when the file write fails', async () => {+ await addTask({ description: 'survivor', id: 'task-survivor' }, 'user');+ mock.atomicFailure = Object.assign(new Error('ENOSPC: no space left on device'), { code: 'ENOSPC' });+ await expect(addTask({ description: 'lost write', id: 'task-lost' }, 'user')).rejects.toThrow('ENOSPC');+ mock.atomicFailure = null;+ expect((await getUserTasks()).tasks.map(t => t.id)).toEqual(['task-survivor']);+ });+it('persists the no-change success marker for an autonomous audit', async () => {const created = await addTask({description: 'Audit shipped catalogs',@@ -2011,12 +2033,19 @@ describe('parsed-task cache — no bypass (source guards, #3497)', () => {expect(bypasses, `direct readFile call(s): ${bypasses.join(' | ')}`).toEqual([]);});+ it('writeTaskFile persists through atomicWrite, never an in-place fs writeFile', () => {+ // An in-place truncate killed mid-write leaves TASKS.md empty/half-written,+ // which parses as "no tasks" and is persisted by the next write.+ expect(STORE_SRC).toMatch(/import \{ atomicWrite \} from '\.\.\/lib\/fileUtils\.js'/);+ expect(STORE_SRC).not.toMatch(/import \{[^}]*\bwriteFile\b[^}]*\} from 'fs\/promises'/);+ });+it('every task-file write goes through writeTaskFile', () => {// A write calling writeFile directly leaves the previous parse cached, and// mtime granularity is too coarse to be relied on to catch it — the next// read would serve pre-write tasks.- const bypasses = outsideAccessors.filter(line => /\bwriteFile\(/.test(line));- expect(bypasses, `direct writeFile call(s): ${bypasses.join(' | ')}`).toEqual([]);+ const bypasses = outsideAccessors.filter(line => /\b(writeFile|atomicWrite)\(/.test(line));+ expect(bypasses, `direct writeFile/atomicWrite call(s): ${bypasses.join(' | ')}`).toEqual([]);});});@@ -2068,7 +2097,7 @@ describe('cosTaskStore.mergePeerTasks', () => {it('is a no-op (no write, no event) when the peer payload changes nothing', async () => {await addTask({ description: 'same', priority: 'MEDIUM', id: 'task-same' }, 'user');mock.events = [];- const writeSpy = (await import('fs/promises')).writeFile;+ const writeSpy = (await import('../lib/fileUtils.js')).atomicWrite;writeSpy.mockClear();const remote = [{ id: 'task-same', taskType: 'user', status: 'pending', priority: 'MEDIUM', description: 'same', metadata: {} }];const res = await mergePeerTasks('user', remote, { now: NOW });