// engine/sdd-flow/checkpoints.ts — SPEC-583: Resumable state persistence
import { readFile, writeFile, mkdir } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
function getStateDir(projectPath) {
    return join(projectPath, 'planu', 'state');
}
function getStatePath(projectPath, runId) {
    return join(getStateDir(projectPath), `sdd-flow-${runId}.json`);
}
export async function saveCheckpoint(projectPath, state) {
    const dir = getStateDir(projectPath);
    if (!existsSync(dir)) {
        await mkdir(dir, { recursive: true });
    }
    const path = getStatePath(projectPath, state.runId);
    await writeFile(path, JSON.stringify(state, null, 2), 'utf-8');
}
export async function loadCheckpoint(projectPath, runId) {
    const path = getStatePath(projectPath, runId);
    if (!existsSync(path)) {
        return null;
    }
    try {
        const raw = await readFile(path, 'utf-8');
        return JSON.parse(raw);
    }
    catch {
        return null;
    }
}
export function createInitialState(runId, projectPath, controlMode, specIds) {
    const stages = ['planner', 'reviewer', 'executor', 'validator', 'merger', 'releaser'].map((name) => ({
        name,
        status: 'pending',
        inputs: null,
        outputs: null,
    }));
    return {
        runId,
        startedAt: new Date().toISOString(),
        projectPath,
        controlMode,
        specIds,
        stages,
        currentStage: 0,
    };
}
//# sourceMappingURL=checkpoints.js.map