// engine/checkpoint/policy-engine.ts — Checkpoint policy evaluation (SPEC-411)
import { readJson, writeJson, projectDataDir, hashProjectPath } from '../../storage/base-store.js';
// ---------------------------------------------------------------------------
// Built-in presets
// ---------------------------------------------------------------------------
export const POLICY_PRESETS = {
    strict: {
        name: 'strict',
        description: 'All major transitions require explicit approval',
        policies: [
            {
                transition: 'draft->review',
                requiredRole: 'developer',
                description: 'Self-review before submitting',
            },
            {
                transition: 'review->approved',
                requiredRole: 'tech_lead',
                description: 'Tech lead must approve before work starts',
            },
            {
                transition: 'approved->implementing',
                requiredRole: 'product_owner',
                description: 'PO confirms priority before devs start',
            },
            {
                transition: 'implementing->done',
                requiredRole: 'qa',
                description: 'QA must verify before marking done',
            },
        ],
    },
    balanced: {
        name: 'balanced',
        description: 'Only critical transitions need approval',
        policies: [
            {
                transition: 'review->approved',
                requiredRole: 'tech_lead',
                description: 'Tech lead approves specs',
            },
            {
                transition: 'implementing->done',
                requiredRole: 'qa',
                description: 'QA verifies completion',
            },
        ],
    },
    relaxed: {
        name: 'relaxed',
        description: 'Only final completion needs approval',
        policies: [
            {
                transition: 'implementing->done',
                requiredRole: 'tech_lead',
                description: 'Lead confirms done',
            },
        ],
    },
};
// ---------------------------------------------------------------------------
// Policy evaluation
// ---------------------------------------------------------------------------
/**
 * Returns the matching policy if the given transition requires a checkpoint,
 * or null if the transition is free to proceed.
 */
export function requiresCheckpoint(config, fromStatus, toStatus) {
    const transition = `${fromStatus}->${toStatus}`;
    return config.policies.find((p) => p.transition === transition) ?? null;
}
// ---------------------------------------------------------------------------
// Config persistence
// ---------------------------------------------------------------------------
function configFilePath(projectId) {
    return `${projectDataDir(projectId)}/checkpoint-config.json`;
}
const EMPTY_CONFIG = { policies: [] };
export async function loadCheckpointConfig(projectPath) {
    const projectId = hashProjectPath(projectPath);
    return readJson(configFilePath(projectId), EMPTY_CONFIG);
}
export async function saveCheckpointConfig(projectPath, config) {
    const projectId = hashProjectPath(projectPath);
    await writeJson(configFilePath(projectId), config);
}
//# sourceMappingURL=policy-engine.js.map