/** * State management — tracks sequence numbers and run phases in DELTAS/state.json. * The state file is a recoverable cache; source of truth is the DELTAS directory. */ import { readFile, writeFile } from 'fs/promises'; import { join } from 'path'; const STATE_FILE = 'state.json'; export async function readState(deltasDir) { const path = join(deltasDir, STATE_FILE); try { const raw = await readFile(path, 'utf8'); return JSON.parse(raw); } catch { // Missing or unreadable — reconstruct from directory scan return reconstructState(deltasDir); } } export async function writeState(deltasDir, state) { const path = join(deltasDir, STATE_FILE); await writeFile(path, JSON.stringify(state, null, 2) + '\n', 'utf8'); } async function reconstructState(deltasDir) { // TODO: scan DELTAS for highest committed sequence number // For now, start fresh return { next_seq: 1, last_complete: 0, phase: 'idle' }; } export const PHASES = { IDLE: 'idle', CLEARING_PEND: 'clearing_pend', RSYNC_LOCAL: 'rsync_local', RSYNC_REMOTE: 'rsync_remote', GENERATING: 'generating_delta', COMMITTING: 'committing', PROMOTING: 'promoting_prev', };