File size: 10,697 Bytes
e942c7e
 
 
 
 
 
 
 
8628c98
 
 
 
 
 
 
 
 
 
 
 
 
 
e942c7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8628c98
e942c7e
 
 
 
 
d893281
e942c7e
 
 
8628c98
e942c7e
 
8628c98
 
 
 
 
 
 
 
 
 
 
 
 
e942c7e
 
 
 
 
 
ee7ac62
8628c98
e942c7e
 
 
 
 
 
8628c98
 
 
 
e942c7e
 
 
 
 
8628c98
 
 
e942c7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
355ed30
8628c98
e942c7e
 
 
 
8628c98
355ed30
 
 
 
e942c7e
 
355ed30
 
 
 
 
 
e942c7e
8628c98
 
 
 
e942c7e
 
 
 
 
 
 
 
8628c98
e942c7e
 
 
 
8628c98
e942c7e
8628c98
e942c7e
 
 
 
 
 
 
8628c98
e942c7e
 
 
 
 
 
 
8628c98
 
e942c7e
 
 
 
 
 
 
 
8628c98
e942c7e
 
 
 
8628c98
 
 
 
 
 
e942c7e
 
 
 
 
8628c98
e942c7e
 
 
 
 
 
8628c98
e942c7e
 
8628c98
 
 
e942c7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8628c98
e942c7e
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"""
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,
        },
    }