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

2.5 KB
Raw Blame
/**
 * CoS Runner — State persistence layer
 *
 * Owns the on-disk runner-state.json: atomic writes, serialized saves, and
 * loads. Kept self-contained (only fs + the shared fileUtils helpers) so the
 * isolated `portos-cos` PM2 process stays standalone.
 */

import { join } from 'path';
import { readFile } from 'fs/promises';
import { existsSync } from 'fs';
import { atomicWrite, PATHS } from '../lib/fileUtils.js';

export const STATE_FILE = join(PATHS.cos, 'runner-state.json');

export const DEFAULT_STATE = { agents: {}, stats: { spawned: 0, completed: 0, failed: 0 } };

const freshState = () => ({ ...DEFAULT_STATE, agents: {}, stats: { ...DEFAULT_STATE.stats } });

// Serializes saves so concurrent writes can't interleave and corrupt the file.
let stateLock = Promise.resolve();

/**
 * Load runner state from disk
 */
export async function loadState() {
  if (!existsSync(STATE_FILE)) return freshState();

  const content = await readFile(STATE_FILE, 'utf-8');
  if (!content || !content.trim()) return freshState();

  return JSON.parse(content);
}

/**
 * Save runner state to disk (serialized with atomic writes to prevent corruption).
 * Delegates the temp-file-and-rename dance to the shared fileUtils.atomicWrite,
 * which also handles ensureDir, JSON stringification, and the Windows rename fallback.
 */
export function saveState(state) {
  // Swallow a prior write's rejection before chaining so one failed write can't
  // poison the lock and silently skip every subsequent save.
  stateLock = stateLock.catch(() => {}).then(() => atomicWrite(STATE_FILE, state));
  return stateLock;
}

/**
 * Queue an entire read-modify-write cycle on the serialized lock.
 * The mutator receives the current state, mutates it in-place (or returns a
 * new state), and the helper saves whatever it returns (or the mutated arg if
 * it returns undefined). Callers that previously did loadState/mutate/saveState
 * in open code are subject to read-modify-write races when two such cycles
 * interleave; using withState serializes the whole cycle end-to-end.
 *
 * @param {function(state: object): Promise<object|undefined>} mutatorFn
 */
export function withState(mutatorFn) {
  stateLock = stateLock.catch(() => {}).then(async () => {
    const state = await loadState();
    const result = await mutatorFn(state);
    const next = result !== undefined ? result : state;
    await atomicWrite(STATE_FILE, next);
  });
  return stateLock;
}

/** Await queued state writes after spawn and exit producers have drained. */
export function drainState() {
  return stateLock;
}