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

fix(cos): atomic state writes and full mutex coverage for state.json

Adam Eivy2bacd5d6ef62
diff --git a/.changelog/v0.17.x.md b/.changelog/v0.17.x.md
index 5466db798a..aa4d699043 100644
--- a/.changelog/v0.17.x.md
+++ b/.changelog/v0.17.x.md
@@ -40,3 +40,16 @@ JIRA integration improvements and token lifecycle management.
### Replace raw JSON.parse with safeJSONParse in 5 services
- `platformAccounts.js`, `agentPersonalities.js`, `cosEvolution.js`, `missions.js`, `agentDrafts.js`
- Corrupted JSON files now gracefully fall back to defaults instead of crashing the process
+
+### Atomic state writes and full mutex coverage for CoS state
+- `saveState()` now writes to a temp file then renames (atomic on POSIX), preventing half-written JSON on crash/interrupt
+- `start()`, `stop()`, `pause()`, `resume()` now use `withStateLock` — previously these 4 lifecycle functions wrote state without the mutex, racing with 18+ other locked write paths
+- Root cause fix for the recurring `state.json` corruption that produced 500+ `.corrupted` backup files
+
+### Interactive memory graph visualization
+- **Canvas-based force-directed graph** renders all memory nodes with Coulomb repulsion + Hooke spring attraction
+- **Colored by type** (fact/learning/observation/decision/preference/context) with radius scaled by importance
+- **Interactions**: hover tooltip, click to select + highlight connections, drag to reposition nodes, scroll to zoom, drag empty space to pan
+- **Detail panel** below canvas shows selected node summary, type, importance, and clickable list of connected nodes
+- **Legend overlay** with type colors and edge style (solid=linked, dashed=similar)
+- **URL-driven view routing** — memory tab views (`list`, `timeline`, `graph`) are now deep-linkable via `?view=graph` query param
diff --git a/server/services/cos.js b/server/services/cos.js
index 5bb05b4a0c..c0b89b4e81 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -5,7 +5,7 @@
* spawns sub-agents, and orchestrates task completion.
*/
-import { readFile, writeFile, mkdir, readdir, rm, stat } from 'fs/promises';
+import { readFile, writeFile, rename, mkdir, readdir, rm, stat } from 'fs/promises';
import { existsSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
@@ -277,7 +277,9 @@ async function loadState() {
*/
async function saveState(state) {
await ensureDirectories();
- await writeFile(STATE_FILE, JSON.stringify(state, null, 2));
+ const tmpFile = `${STATE_FILE}.tmp`;
+ await writeFile(tmpFile, JSON.stringify(state, null, 2));
+ await rename(tmpFile, STATE_FILE);
}
/**
@@ -344,9 +346,12 @@ export async function start() {
emitLog('info', 'Starting Chief of Staff daemon...');
- const state = await loadState();
- state.running = true;
- await saveState(state);
+ const state = await withStateLock(async () => {
+ const s = await loadState();
+ s.running = true;
+ await saveState(s);
+ return s;
+ });
daemonRunning = true;
@@ -409,9 +414,11 @@ export async function stop() {
cancelEvent('cos-evaluation');
cancelEvent('cos-health-check');
- const state = await loadState();
- state.running = false;
- await saveState(state);
+ await withStateLock(async () => {
+ const state = await loadState();
+ state.running = false;
+ await saveState(state);
+ });
daemonRunning = false;
cosEvents.emit('status', { running: false });
@@ -423,46 +430,51 @@ export async function stop() {
* Daemon stays running but skips evaluations
*/
export async function pause(reason = null) {
- const state = await loadState();
+ return withStateLock(async () => {
+ const state = await loadState();
- if (state.paused) {
- return { success: false, error: 'Already paused' };
- }
+ if (state.paused) {
+ return { success: false, error: 'Already paused' };
+ }
- state.paused = true;
- state.pausedAt = new Date().toISOString();
- state.pauseReason = reason;
- await saveState(state);
+ state.paused = true;
+ state.pausedAt = new Date().toISOString();
+ state.pauseReason = reason;
+ await saveState(state);
- emitLog('info', `CoS paused${reason ? `: ${reason}` : ''}`);
- cosEvents.emit('status:paused', { paused: true, pausedAt: state.pausedAt, reason });
- return { success: true, pausedAt: state.pausedAt };
+ emitLog('info', `CoS paused${reason ? `: ${reason}` : ''}`);
+ cosEvents.emit('status:paused', { paused: true, pausedAt: state.pausedAt, reason });
+ return { success: true, pausedAt: state.pausedAt };
+ });
}
/**
* Resume the CoS daemon from pause
*/
export async function resume() {
- const state = await loadState();
+ const result = await withStateLock(async () => {
+ const state = await loadState();
- if (!state.paused) {
- return { success: false, error: 'Not paused' };
- }
+ if (!state.paused) {
+ return { success: false, error: 'Not paused' };
+ }
- state.paused = false;
- state.pausedAt = null;
- state.pauseReason = null;
- await saveState(state);
+ state.paused = false;
+ state.pausedAt = null;
+ state.pauseReason = null;
+ await saveState(state);
- emitLog('info', 'CoS resumed');
- cosEvents.emit('status:resumed', { paused: false });
+ emitLog('info', 'CoS resumed');
+ cosEvents.emit('status:resumed', { paused: false });
+ return { success: true };
+ });
- // Trigger immediate evaluation on resume
- if (daemonRunning) {
+ // Trigger immediate evaluation on resume (outside lock to avoid holding it)
+ if (result.success && daemonRunning) {
setTimeout(() => evaluateTasks(), 500);
}
- return { success: true };
+ return result;
}
/**