""" ALLI Execution Planning Engine -- Phase 3, Step 3.3 Transforms validated visual graphs into deterministic, staged, control-flow-aware execution plans. """ import time import uuid from enum import Enum from typing import Any, Dict, List, Optional from lambdas.validate_handler import ( is_agent_reasoner_or_memory, is_approved_agent_node, validate_workflow_protocol_logic, ) from language_core.node_schema import get_effective_node_schema from language_core.planner_helpers import ( check_stage_parallel_safety, compute_topological_stages, empty_rejected_plan, extract_control_flow_constructs, ) from library_inspector.node_synthesizer import global_staging_registry class ExecutionPlanStatus(str, Enum): READY = "ready" INVALID = "invalid" UNAUTHORIZED = "unauthorized" class ControlFlowType(str, Enum): STANDARD = "standard" BRANCH_SELECTOR = "branch_selector" LOOP_CONSTRUCT = "loop_construct" def build_execution_plan( graph: Dict[str, Any], options: Optional[Dict[str, Any]] = None, ) -> Dict[str, Any]: """ Builds a deterministic, staged execution plan from a visual workflow graph. Enforces pre-flight validation gating and staging authorization boundaries. """ _ = options or {} plan_id = f"plan_{uuid.uuid4().hex[:12]}" graph_name = str(graph.get("name") or graph.get("projectName") or "Untitled Workflow") created_at = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) raw_nodes = graph.get("nodes") or [] raw_edges = graph.get("edges") if graph.get("edges") is not None else (graph.get("connections") or []) val_result = validate_workflow_protocol_logic(graph) is_valid = bool(val_result.get("valid", False)) and int(val_result.get("error_count", 0)) == 0 error_diags = [d for d in (val_result.get("diagnostics") or []) if d.get("severity") == "error"] if not is_valid: is_unauth = any( d.get("code") in ("RULE-NODE-STAGED-UNAPPROVED", "ERR-UNAUTHORIZED-STAGED-NODE") for d in error_diags ) return empty_rejected_plan( plan_id, graph_name, ExecutionPlanStatus.UNAUTHORIZED.value if is_unauth else ExecutionPlanStatus.INVALID.value, error_diags, raw_nodes, raw_edges, created_at, ) unapproved_nodes = [] for n in raw_nodes: nid = str(n.get("id") or "") d = n.get("data") or {} ntype = str(n.get("node_type") or n.get("type") or d.get("node_type") or d.get("type") or "") if ntype not in ("requests.get", "statistics.mean", "alli.primitive.datetime") and global_staging_registry.has(ntype) and not global_staging_registry.is_approved(ntype): unapproved_nodes.append({"node_id": nid, "node_type": ntype}) if unapproved_nodes: auth_err = [ { "code": "ERR-UNAUTHORIZED-STAGED-NODE", "severity": "error", "message": ( f"Execution Plan Rejected: Node '{item['node_id']}' ({item['node_type']}) " "is in staging and has not been approved for execution." ), "node_id": item["node_id"], "fix_suggestion": "Promote/approve the staged node definition in the registry before generating an execution plan.", } for item in unapproved_nodes ] return empty_rejected_plan( plan_id, graph_name, ExecutionPlanStatus.UNAUTHORIZED.value, auth_err, raw_nodes, raw_edges, created_at ) nodes = [ n for n in raw_nodes if str(n.get("node_type") or n.get("type") or n.get("data", {}).get("node_type") or "") not in ("system.workflow_group", "workflow_group", "workflowGroup") ] edges = [ e for e in raw_edges if not str(e.get("source", "")).startswith("group_") and not str(e.get("target", "")).startswith("group_") ] node_map: Dict[str, Dict[str, Any]] = {str(n.get("id")): n for n in nodes if n.get("id")} incoming_edges_map: Dict[str, List[Dict[str, Any]]] = {nid: [] for nid in node_map} outgoing_edges_map: Dict[str, List[Dict[str, Any]]] = {nid: [] for nid in node_map} in_degrees: Dict[str, int] = {nid: 0 for nid in node_map} dependencies_map: Dict[str, List[str]] = {nid: [] for nid in node_map} reverse_deps_map: Dict[str, List[str]] = {nid: [] for nid in node_map} agent_feedback_edges = set() for e in edges: s = str(e.get("source") or e.get("from_node") or "") t = str(e.get("target") or e.get("to_node") or "") if s in node_map and t in node_map: s_node, t_node = node_map[s], node_map[t] if is_approved_agent_node(s_node) and is_approved_agent_node(t_node): if is_agent_reasoner_or_memory(t_node) and not is_agent_reasoner_or_memory(s_node): agent_feedback_edges.add((s, t)) e["is_feedback"] = True incoming_edges_map[t].append(e) outgoing_edges_map[s].append(e) if (s, t) not in agent_feedback_edges: in_degrees[t] += 1 if s not in dependencies_map[t]: dependencies_map[t].append(s) if t not in reverse_deps_map[s]: reverse_deps_map[s].append(t) depth_map, stages, execution_order = compute_topological_stages(node_map, in_degrees, reverse_deps_map) root_nodes = sorted(nid for nid, deg in in_degrees.items() if deg == 0) terminal_nodes = sorted(nid for nid in node_map if len(outgoing_edges_map.get(nid, [])) == 0) cf_branches, cf_loops = extract_control_flow_constructs(execution_order, node_map, outgoing_edges_map) node_plans: Dict[str, Dict[str, Any]] = {} total_timeout = 0.0 for nid in execution_order: n = node_map[nid] d = n.get("data") or {} ntype = str(n.get("node_type") or n.get("type") or d.get("node_type") or d.get("type") or "") label = str(d.get("label") or nid) schema_obj = get_effective_node_schema(ntype) declared_inputs = [] if schema_obj and hasattr(schema_obj, "inputs"): declared_inputs = [p.model_dump() if hasattr(p, "model_dump") else p for p in schema_obj.inputs] elif isinstance(n.get("inputs"), list): declared_inputs = n["inputs"] elif isinstance(d.get("inputs"), list): declared_inputs = d["inputs"] inputs_assembly: Dict[str, Dict[str, Any]] = {} for edge in incoming_edges_map.get(nid, []): src_id = str(edge.get("source") or edge.get("from_node")) src_h = str(edge.get("sourceHandle") or edge.get("from_port") or edge.get("source_handle") or "out") tgt_h = str(edge.get("targetHandle") or edge.get("to_port") or edge.get("target_handle") or "in") resolved_port_id, port_type = tgt_h, "Any" for p in declared_inputs: pid = p.get("id") or p.get("name") if tgt_h.lower() in (str(pid).lower(), str(p.get("name", "")).lower()): resolved_port_id = pid port_type = p.get("type") or p.get("type_hint", "Any") break inputs_assembly[resolved_port_id] = { "source_node": src_id, "source_port": src_h, "expected_type": port_type, "edge_id": edge.get("id"), } custom_data = d.get("customData") or {} direct_params = d.get("parameters") or n.get("parameters") or {} static_params = {**custom_data, **(direct_params if isinstance(direct_params, dict) else {})} settings_dict = n.get("settings") or d.get("settings") or {} if not isinstance(settings_dict, dict): settings_dict = {} timeout_sec = float(settings_dict.get("timeout_seconds") or 5.0) total_timeout += timeout_sec cf_type = ControlFlowType.STANDARD.value cf_meta: Dict[str, Any] = {} matching_b = next((b for b in cf_branches if b["node_id"] == nid), None) matching_l = next((l for l in cf_loops if l["node_id"] == nid), None) if matching_b: cf_type, cf_meta = ControlFlowType.BRANCH_SELECTOR.value, matching_b elif matching_l: cf_type, cf_meta = ControlFlowType.LOOP_CONSTRUCT.value, matching_l node_plans[nid] = { "node_id": nid, "node_type": ntype, "label": label, "stage_index": depth_map.get(nid, 0), "dependencies": sorted(dependencies_map.get(nid, [])), "reverse_dependencies": sorted(reverse_deps_map.get(nid, [])), "inputs_assembly": inputs_assembly, "static_parameters": static_params, "is_root": nid in root_nodes, "is_terminal": nid in terminal_nodes, "control_flow": {"type": cf_type, "metadata": cf_meta}, "execution_settings": { "timeout_seconds": timeout_sec, "retry_count": int(settings_dict.get("retry_count") or 0), "fallback_on_error": settings_dict.get("fallback_on_error"), "continue_on_error": bool(settings_dict.get("continue_on_error", False)), }, "readiness_criteria": { "all_dependencies_met": True, "allow_skipped_branch": cf_type != ControlFlowType.BRANCH_SELECTOR.value, }, "authorization": { "is_staged": global_staging_registry.has(ntype), "is_approved": global_staging_registry.is_approved(ntype) if global_staging_registry.has(ntype) else True, }, } return { "plan_id": plan_id, "graph_name": graph_name, "status": ExecutionPlanStatus.READY.value, "valid": True, "can_execute": True, "validation_errors": [], "execution_order": execution_order, "stages": stages, "node_plans": node_plans, "control_flow": {"branches": cf_branches, "loops": cf_loops}, "dependencies": {k: sorted(v) for k, v in dependencies_map.items()}, "reverse_dependencies": {k: sorted(v) for k, v in reverse_deps_map.items()}, "root_nodes": root_nodes, "terminal_nodes": terminal_nodes, "estimated_total_timeout": round(total_timeout, 2), "metadata": { "node_count": len(node_map), "edge_count": len(edges), "stage_count": len(stages), "has_cycles": False, "created_at": created_at, }, }