import { findCoveringWorkspaceLease } from "../workspace-state/enforcement.js";
const allowedTransitions = {
    planned: ["ready", "blocked", "cancelled"],
    ready: ["executing", "blocked", "cancelled"],
    executing: ["verifying", "rework", "blocked", "cancelled"],
    verifying: ["accepted", "rework", "blocked", "cancelled"],
    rework: ["ready", "executing", "blocked", "cancelled"],
    blocked: ["ready", "cancelled"],
    accepted: [],
    cancelled: [],
};
export function assertSubplanTransitionAllowed(from, to) {
    if (from === to)
        return;
    if (!allowedTransitions[from]?.includes(to)) {
        throw new Error(`invalid subplan transition: ${from} -> ${to}`);
    }
}
export function assertSubplanExecutionReady(subplan, activeLeases) {
    if (subplan.write_paths.length === 0)
        return;
    if (subplan.workspace_strategy === "worktree")
        return;
    if (!subplan.owner_member) {
        throw new Error(`subplan ${subplan.subplan_id} cannot execute write work without owner_member`);
    }
    if (subplan.lease_ids.length === 0) {
        throw new Error(`subplan ${subplan.subplan_id} cannot execute write work without bound workspace lease_ids`);
    }
    const missing = subplan.write_paths.filter((writePath) => !findCoveringWorkspaceLease({
        path: writePath,
        access: "write",
        leases: activeLeases,
        owner: subplan.owner_member,
        leaseIds: subplan.lease_ids,
    }));
    if (missing.length > 0) {
        throw new Error(`subplan ${subplan.subplan_id} cannot execute until active write leases cover: ${missing.join(", ")}`);
    }
}
export function nextStatusForVerdict(verdict) {
    if (verdict === "accepted")
        return "accepted";
    if (verdict === "rework")
        return "rework";
    return "blocked";
}
//# sourceMappingURL=state-machine.js.map