//#region src/validation/cycleDetection.ts
function detectDelegationCycles(agentData) {
	const graph = buildDelegationGraph(agentData);
	const cycles = [];
	const visited = /* @__PURE__ */ new Set();
	const stack = /* @__PURE__ */ new Set();
	const path = [];
	function dfs(node) {
		visited.add(node);
		stack.add(node);
		path.push(node);
		for (const neighbor of graph.get(node) || []) if (!visited.has(neighbor)) {
			if (dfs(neighbor)) return true;
		} else if (stack.has(neighbor)) {
			const cycleStart = path.indexOf(neighbor);
			cycles.push(`Circular delegation detected: ${[...path.slice(cycleStart), neighbor].join(" → ")}`);
			return true;
		}
		stack.delete(node);
		path.pop();
		return false;
	}
	for (const node of graph.keys()) if (!visited.has(node)) {
		path.length = 0;
		dfs(node);
	}
	return cycles;
}
function buildDelegationGraph(agentData) {
	const graph = /* @__PURE__ */ new Map();
	for (const [subAgentId, subAgent] of Object.entries(agentData.subAgents)) {
		const delegates = subAgent.canDelegateTo?.filter((d) => typeof d === "string");
		if (delegates?.length) graph.set(subAgentId, delegates);
	}
	return graph;
}

//#endregion
export { detectDelegationCycles };