// orchestrator/topologies.ts — Topology strategy implementations (SPEC-079 AC-02)
// Each topology defines planExecution() returning a DAG of execution nodes.
/**
 * Plan execution graph for mesh topology.
 * All agents work in parallel — no dependencies between tasks.
 * Results are reconciled at the end.
 */
export function planMeshExecution(tasks, agents) {
    const agentIds = new Set(agents.map((a) => a.agentId));
    const nodes = tasks
        .filter((t) => agentIds.has(t.agentId))
        .map((t) => ({
        taskId: t.taskId,
        agentId: t.agentId,
        dependencies: [],
        phase: 0,
    }));
    return { nodes, phases: nodes.length > 0 ? 1 : 0, topology: 'mesh' };
}
/**
 * Plan execution graph for hierarchical topology.
 * First agent is coordinator (phase 0), remaining are workers (phase 1).
 * Workers depend on the coordinator task completing first.
 */
export function planHierarchicalExecution(tasks, agents) {
    if (tasks.length === 0 || agents.length === 0) {
        return { nodes: [], phases: 0, topology: 'hierarchical' };
    }
    const coordinatorTask = tasks[0];
    const workerTasks = tasks.slice(1);
    const nodes = [];
    if (coordinatorTask) {
        nodes.push({
            taskId: coordinatorTask.taskId,
            agentId: coordinatorTask.agentId,
            dependencies: [],
            phase: 0,
        });
    }
    const coordId = coordinatorTask?.taskId ?? '';
    for (const t of workerTasks) {
        nodes.push({
            taskId: t.taskId,
            agentId: t.agentId,
            dependencies: [coordId],
            phase: 1,
        });
    }
    return { nodes, phases: workerTasks.length > 0 ? 2 : 1, topology: 'hierarchical' };
}
/**
 * Plan execution graph for pipeline topology.
 * Tasks execute sequentially — each depends on the previous one.
 */
export function planPipelineExecution(tasks, _agents) {
    const nodes = tasks.map((t, i) => ({
        taskId: t.taskId,
        agentId: t.agentId,
        dependencies: i > 0 ? [tasks[i - 1]?.taskId ?? ''] : [],
        phase: i,
    }));
    return { nodes, phases: tasks.length, topology: 'pipeline' };
}
/**
 * Plan execution graph for star topology.
 * First agent is the hub (phase 0), all others are spokes (phase 1).
 * Spokes run in parallel but each depends on the hub.
 */
export function planStarExecution(tasks, agents) {
    // Star is structurally similar to hierarchical but semantically different.
    // Hub coordinates, spokes report only to hub (not to each other).
    return planHierarchicalExecution(tasks, agents);
}
/** Topology planner registry. */
const TOPOLOGY_PLANNERS = {
    mesh: planMeshExecution,
    hierarchical: planHierarchicalExecution,
    pipeline: planPipelineExecution,
    star: planStarExecution,
};
/**
 * Plan execution for the given topology type.
 * Delegates to the appropriate topology strategy.
 */
export function planExecution(topology, tasks, agents) {
    const planner = TOPOLOGY_PLANNERS[topology];
    return planner(tasks, agents);
}
//# sourceMappingURL=topologies.js.map