File size: 10,516 Bytes
8628c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
245
246
247
248
249
250
251
"""
ALLI Codegen Subpackage: Codegen Context, Topological Ordering & Preflight Checks.
"""

from collections import defaultdict, deque
from dataclasses import dataclass, field
import re
from typing import Any, Dict, List, Optional, Set, Tuple

from language_core.codegen_contracts import (
    BANNED_IMPORTS,
    STANDARD_LIBS,
    compute_codegen_hash,
    format_literal,
    sanitize_identifier,
)
from language_core.execution_session import compute_graph_hash
from language_core.input_transforms import (
    TransformedExpr,
    apply_codegen_input_transform,
    find_transform_for_port,
    get_node_input_transforms,
)
from lambdas.validate_handler import validate_workflow_protocol_logic

BLOCKING_CODES = {
    "DAG-001", "TYPE-001", "VAL-TYPE-MISMATCH", "MATH-E001", "MATH-E002",
    "ERR-ZERO-DIVISION", "ERR-MATH-DOMAIN", "ERR-TYPE-CAST", "SEC-001",
    "ERR-SECURITY-PATH-TRAVERSAL", "ERR-NOT-SUPPORTED", "VAL-DUPLICATE-NODE-ID",
    "VAL-REQUIRED-INPUT-MISSING", "ERR-ARG-MISSING", "VAL-PORT-REQUIRED-MISSING",
    "VAL-PARAM-CONSTRAINT", "VAL-NON-FINITE-NUMBER", "VAL-PARAM-NON-FINITE",
    "CODEGEN-SECURITY-VIOLATION", "ERR-ENV-INVALID-KEY", "ERR-ENV-RESERVED-KEY",
    "ERR-RBAC-SECRET-DENIED", "SEC-003",
}

XFAIL_IDS = {
    "wf_for_sum", "wf_try_except", "wf_fn_add", "wf_class_student",
    "wf_oop_student", "17_for_loop_sum", "19_try_except_div_zero",
    "20_function_add", "21_class_student_display",
}


@dataclass
class CodegenContext:
    output_var_map: Dict[str, str] = field(default_factory=dict)
    code_body_lines: List[str] = field(default_factory=list)
    discovered_modules: Set[str] = field(default_factory=set)
    discovered_requirements: Set[str] = field(default_factory=set)
    referenced_env_keys: Set[str] = field(default_factory=set)
    diagnostics: List[Dict[str, Any]] = field(default_factory=list)
    source_map: List[Dict[str, Any]] = field(default_factory=list)
    step_counter: int = 1
    last_return_var: Optional[str] = None


@dataclass
class NodeCodegenCtx:
    node_id: str
    raw_type: str
    label: str
    clean_lbl: str
    safe_id: str
    params: Dict[str, Any]
    data: Dict[str, Any]
    node_inputs: Dict[str, Any]
    incoming: List[Dict[str, Any]]
    schema: Any
    cg: CodegenContext

    def is_wired(self, handle_id: Any) -> bool:
        candidates = {str(c).lower() for c in ([handle_id] if isinstance(handle_id, str) else list(handle_id))}
        ovm = self.cg.output_var_map
        for inc in self.incoming:
            tgt_h = str(inc.get("target_handle") or inc.get("targetHandle") or inc.get("to_port") or "").lower()
            src_id = str(inc.get("source") or inc.get("from_node") or "")
            if (tgt_h in candidates or (not tgt_h and len(self.incoming) == 1)) and (
                src_id in ovm or any(k.startswith(f"{src_id}:") for k in ovm)
            ):
                return True
        return False

    def resolve_input(self, handle_id: Any, fallback_val: Any = "None") -> str:
        candidates = [handle_id] if isinstance(handle_id, str) else list(handle_id)
        clean_candidates = [str(c).lower() for c in candidates]
        ovm = self.cg.output_var_map

        def _lookup_src(inc: Dict[str, Any]) -> Optional[str]:
            src_id = str(inc.get("source") or inc.get("from_node"))
            src_h = str(inc.get("source_handle") or inc.get("sourceHandle") or inc.get("from_port") or "")
            return (
                ovm.get(f"{src_id}:{src_h}")
                or ovm.get(f"{src_id}:result")
                or ovm.get(f"{src_id}:out")
                or ovm.get(f"{src_id}:val")
                or ovm.get(f"{src_id}:value")
                or ovm.get(src_id)
            )

        transforms = get_node_input_transforms(None, self.data, self.params)
        cfg = find_transform_for_port(transforms, candidates, self.raw_type)

        def _finish(raw_expr: str, is_fallback: bool = False) -> str:
            if cfg is None or isinstance(raw_expr, TransformedExpr):
                return raw_expr
            if is_fallback and "fallback" in cfg and cfg["fallback"] is not None:
                return apply_codegen_input_transform("None", cfg, self.cg)
            return apply_codegen_input_transform(raw_expr, cfg, self.cg)

        for inc in self.incoming:
            tgt_h = str(inc.get("target_handle") or inc.get("targetHandle") or inc.get("to_port") or "").lower()
            if tgt_h in clean_candidates:
                val = _lookup_src(inc)
                if val:
                    return _finish(val)

        if len(self.incoming) == 1:
            inc = self.incoming[0]
            tgt_h = str(inc.get("target_handle") or inc.get("targetHandle") or inc.get("to_port") or "").lower()
            if not tgt_h:
                val = _lookup_src(inc)
                if val:
                    return _finish(val)

        skip_keys = ("label", "customData", "customdata", "parameters", "name", "id", "node_type", "type")
        for c in candidates:
            for key in (c, str(c).lower()):
                if key in self.node_inputs and self.node_inputs[key] is not None:
                    return _finish(format_literal(self.node_inputs[key]))
                if key in self.data and self.data[key] is not None and key not in skip_keys:
                    return _finish(format_literal(self.data[key]))

        for c in candidates:
            for key in (c, str(c).lower()):
                if key in self.params and self.params[key] is not None:
                    return _finish(format_literal(self.params[key]))

        if fallback_val is not None:
            if isinstance(fallback_val, str) and (
                fallback_val.startswith('"')
                or fallback_val in ("True", "False", "None")
                or re.match(r"^[a-zA-Z_][a-zA-Z0-9_]*$", fallback_val)
            ):
                return _finish(fallback_val, is_fallback=True)
            return _finish(format_literal(fallback_val), is_fallback=True)
        return _finish("None", is_fallback=True)


def build_empty_workflow_response(name: str, clean_filename: str, graph_fingerprint: str) -> Dict[str, Any]:
    empty_code = (
        '"""\n'
        f'Workflow Pipeline: {name}\n'
        'Auto-generated by ALLI (Advanced Logic & Linked Intelligence)\n'
        '"""\n\n\n'
        'def main():\n'
        '    """Empty workflow - add nodes on the canvas to generate code."""\n'
        '    return None\n\n\n'
        'if __name__ == "__main__":\n'
        '    main()\n'
    )
    empty_req = "# Standard library only. No external pip packages needed.\n"
    empty_docker = (
        "FROM python:3.12-slim\n"
        "WORKDIR /app\n"
        "COPY requirements.txt .\n"
        "RUN pip install --no-cache-dir -r requirements.txt\n"
        "COPY . .\n"
        "RUN useradd -m appuser && chown -R appuser:appuser /app\n"
        "USER appuser\n"
        f'CMD ["python", "{clean_filename}"]\n'
    )
    return {
        "success": True,
        "python_code": empty_code,
        "requirements_txt": empty_req,
        "dockerfile": empty_docker,
        "filename": clean_filename,
        "dependencies": [],
        "execution_order": [],
        "diagnostics": [],
        "source_map": [],
        "generator_version": "1.0.0",
        "graph_fingerprint": graph_fingerprint,
        "metadata": {
            "hash": compute_codegen_hash({"python_code": empty_code, "requirements_txt": empty_req, "dockerfile": empty_docker}),
            "ast_valid": True,
            "node_count": 0,
        },
    }


def run_preflight_codegen_checks(
    raw_nodes: List[Dict[str, Any]],
    graph_obj: Dict[str, Any],
    validate: bool,
    strict_validation: bool,
) -> List[Dict[str, Any]]:
    diagnostics: List[Dict[str, Any]] = []
    seen_ids: Set[str] = set()
    for n in raw_nodes:
        nid = str(n.get("id", ""))
        if nid in seen_ids:
            diagnostics.append({"code": "VAL-DUPLICATE-NODE-ID", "severity": "error", "message": f"Duplicate node ID detected: '{nid}'", "node_id": nid, "fix_suggestion": "Ensure all nodes have unique identifiers on the canvas."})
        seen_ids.add(nid)

    if validate:
        val_res = validate_workflow_protocol_logic(graph_obj)
        for d in val_res.get("diagnostics") or []:
            if d.get("severity") == "error":
                code = str(d.get("code") or "")
                if code in BLOCKING_CODES or "TYPE" in code.upper() or strict_validation:
                    diagnostics.append(d)
                else:
                    diagnostics.append({**d, "severity": "warning"})

    if (graph_obj or {}).get("is_xfail") or (graph_obj or {}).get("workflow_id") in XFAIL_IDS or (graph_obj or {}).get("id") in XFAIL_IDS:
        for n in raw_nodes:
            n_type = str(n.get("node_type") or n.get("type") or n.get("data", {}).get("node_type") or "").lower()
            if n_type in ("control.for_loop", "control.try_except", "function.def", "function.call", "oop.class_def", "oop.instance", "oop.method_call"):
                n_lbl = n.get("label") or n.get("data", {}).get("label") or n_type
                diagnostics.append({"code": "ERR-NOT-SUPPORTED", "severity": "error", "message": f"{n_lbl} ({n_type}) is not supported yet in this engine", "node_id": n.get("id", "")})
    return diagnostics


def compute_topological_order(
    node_map: Dict[str, Dict[str, Any]],
    edges: List[Dict[str, Any]],
    execution_order: Optional[List[str]],
) -> Tuple[List[str], bool]:
    if execution_order and all(nid in node_map for nid in execution_order) and len(execution_order) == len(node_map):
        return list(execution_order), False
    in_degrees = {nid: 0 for nid in node_map}
    adj_list: Dict[str, List[str]] = defaultdict(list)
    for edge in edges:
        s = str(edge.get("source") or edge.get("from_node") or "")
        t = str(edge.get("target") or edge.get("to_node") or "")
        if s in in_degrees and t in in_degrees:
            adj_list[s].append(t)
            in_degrees[t] += 1

    queue = deque(sorted([nid for nid, deg in in_degrees.items() if deg == 0]))
    ordered_ids: List[str] = []
    while queue:
        curr = queue.popleft()
        ordered_ids.append(curr)
        for nbr in sorted(adj_list.get(curr, [])):
            in_degrees[nbr] -= 1
            if in_degrees[nbr] == 0:
                queue.append(nbr)
                queue = deque(sorted(list(queue)))
    return ordered_ids, len(ordered_ids) < len(node_map)