File size: 9,837 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
"""
ALLI Graph Scope Analysis & Diamond-Merge Helpers.
Shared by scoped Python codegen and scoped workflow runtime execution.
"""

import re
from collections import defaultdict, deque
from typing import Any, Callable, Dict, Iterable, List, Optional, Set, Tuple

from language_core.codegen_contracts import sanitize_identifier

TRUE_HANDLES = ("true", "then", "true_flow", "then_val")
FALSE_HANDLES = ("false", "else", "false_flow", "else_val")
BODY_HANDLES = ("body",)
POST_LOOP_HANDLES = ("done", "completed")
SAFE_SOURCE_TYPES = {"core.constant", "core.variable", "io.input", "oop.class_def", "data.list", "data.dict"}
_ASSIGN_RE = re.compile(r"^(\s*)([A-Za-z_][A-Za-z0-9_]*)\s*=(?!=)")


def norm_node_type(node: Dict[str, Any]) -> str:
    d = node.get("data") or {}
    return str(node.get("node_type") or node.get("type") or d.get("node_type") or d.get("nodeType") or "").strip().lower()


def reachable_from(roots: Iterable[str], adj: Dict[str, List[str]]) -> Set[str]:
    seen: Set[str] = set()
    q = deque(roots)
    while q:
        cur = q.popleft()
        if cur in seen:
            continue
        seen.add(cur)
        for nxt in adj.get(cur, ()):
            if nxt not in seen:
                q.append(nxt)
    return seen


def detect_scopes(node_map: Dict[str, Dict[str, Any]], edges: List[Dict[str, Any]]) -> Dict[str, Any]:
    out_edges: Dict[str, List[Tuple[str, str, str]]] = defaultdict(list)
    in_edges: Dict[str, List[Tuple[str, str, str]]] = defaultdict(list)
    adj: Dict[str, List[str]] = defaultdict(list)
    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 "")
        sh = str(e.get("source_handle") or e.get("sourceHandle") or e.get("from_port") or "").lower()
        th = str(e.get("target_handle") or e.get("targetHandle") or e.get("to_port") or "").lower()
        if s in node_map and t in node_map:
            out_edges[s].append((sh, t, th))
            in_edges[t].append((s, sh, th))
            adj[s].append(t)

    if_scopes, loop_scopes, try_scopes = {}, {}, {}
    for nid, node in node_map.items():
        rt = norm_node_type(node)
        if rt in ("control.if_else", "if_else"):
            tr = [t for sh, t, _ in out_edges[nid] if sh in TRUE_HANDLES]
            fr = [t for sh, t, _ in out_edges[nid] if sh in FALSE_HANDLES]
            if tr or fr:
                r_t, r_f = reachable_from(tr, adj), reachable_from(fr, adj)
                merge = r_t & r_f
                if (r_t - merge) or (r_f - merge):
                    if_scopes[nid] = {"only_true": r_t - merge, "only_false": r_f - merge, "merge": merge}
        elif rt in ("control.for_loop", "for_loop", "control.while_loop", "while_loop"):
            br = [t for sh, t, _ in out_edges[nid] if sh in BODY_HANDLES]
            if br:
                dr = [t for sh, t, _ in out_edges[nid] if sh in POST_LOOP_HANDLES]
                post = reachable_from(dr, adj)
                body = reachable_from(br, adj) - post - {nid}
                if body:
                    loop_scopes[nid] = {"kind": "for" if "for" in rt else "while", "body": body}
        elif rt in ("control.try_except", "try_except"):
            inc = in_edges[nid]
            roots = {s for s, _, th in inc if th in ("try_val", "in", "value") or (not th and len(inc) == 1)}
            tset: Set[str] = set()
            q = deque(roots)
            while q:
                u = q.popleft()
                ut = norm_node_type(node_map[u]) if u in node_map else ""
                if not ut or u in tset or u == nid or ut in SAFE_SOURCE_TYPES or ut.startswith(("constant.", "trigger.")):
                    continue
                if all(tgt in tset or tgt == nid for _, tgt, _ in out_edges[u]):
                    tset.add(u)
                    for ps, _, _ in in_edges[u]:
                        q.append(ps)
            if tset:
                try_scopes[nid] = {"try_nodes": tset}
    return {"if": if_scopes, "loop": loop_scopes, "try": try_scopes, "in_edges": in_edges}


def scope_children(nid: str, scopes: Dict[str, Any]) -> Set[str]:
    if nid in scopes["if"]:
        return scopes["if"][nid]["only_true"] | scopes["if"][nid]["only_false"]
    if nid in scopes["loop"]:
        return scopes["loop"][nid]["body"]
    if nid in scopes["try"]:
        return scopes["try"][nid]["try_nodes"]
    return set()


def preinit_block_vars(cg: Any, header_idx: int, block_indent: str, skip_vars: Set[str]) -> None:
    new_vars: List[str] = []
    seen = set(skip_vars)
    for line in cg.code_body_lines[:header_idx]:
        m = _ASSIGN_RE.match(line)
        if m and m.group(1) == block_indent:
            seen.add(m.group(2))
    child_indent = block_indent + "    "
    for line in cg.code_body_lines[header_idx:]:
        m = _ASSIGN_RE.match(line)
        if m and m.group(1).startswith(child_indent) and m.group(2) not in seen and m.group(2) != "return":
            seen.add(m.group(2))
            new_vars.append(m.group(2))
    if not new_vars:
        return
    init_lines = [f"{block_indent}{v} = None" for v in new_vars]
    cg.code_body_lines[header_idx:header_idx] = init_lines
    shift = len(init_lines)
    for sm in cg.source_map:
        if sm["line_start"] > header_idx:
            sm["line_start"] += shift
            sm["line_end"] += shift


def build_merge_override(nid: str, cg: Any, inc_edges: List[Dict[str, Any]], completed_ifs: List[Tuple[str, str, Set[str], Set[str]]], indent: str, node: Optional[Dict[str, Any]] = None) -> Optional[List[Dict[str, Any]]]:
    ovm, grouped = cg.output_var_map, defaultdict(list)
    for e in inc_edges:
        th = str(e.get("target_handle") or e.get("targetHandle") or e.get("to_port") or "_default").lower()
        grouped[th].append(e)
    rewritten: Optional[List[Dict[str, Any]]] = None
    safe_m = sanitize_identifier(nid.replace("-", "_"), "m")
    np = node_params(node) if node else {}

    def _expr(ed: Dict[str, Any]) -> str:
        sid = str(ed.get("source") or ed.get("from_node"))
        sh = str(ed.get("source_handle") or ed.get("sourceHandle") or ed.get("from_port") or "")
        return ovm.get(f"{sid}:{sh}") or ovm.get(f"{sid}:result") or ovm.get(f"{sid}:out") or ovm.get(sid) or "None"

    for if_id, branch_var, only_t, only_f in completed_ifs:
        def _is_t(e: Dict[str, Any]) -> bool:
            s, sh = str(e.get("source") or e.get("from_node")), str(e.get("source_handle") or e.get("sourceHandle") or "").lower()
            return s in only_t or (s == if_id and sh in TRUE_HANDLES)
        def _is_f(e: Dict[str, Any]) -> bool:
            s, sh = str(e.get("source") or e.get("from_node")), str(e.get("source_handle") or e.get("sourceHandle") or "").lower()
            return s in only_f or (s == if_id and sh in FALSE_HANDLES)

        for th, elist in grouped.items():
            t_e = next((e for e in elist if _is_t(e)), None)
            f_e = next((e for e in elist if _is_f(e)), None)
            if not (t_e or f_e):
                continue
            if t_e and f_e:
                cond_rhs = f"({_expr(t_e)} if {branch_var} else {_expr(f_e)})"
            else:
                fb = repr(np[th]) if th in np and np[th] is not None else ("0" if norm_node_type(node or {}) in ("core.binary_op", "primitive.binary_op") else "None")
                cond_rhs = f"({_expr(t_e)} if {branch_var} else {fb})" if t_e else f"({fb} if {branch_var} else {_expr(f_e)})"
            mvar = f"_merge_{safe_m}_{sanitize_identifier(th, 'v')}"
            cg.code_body_lines.append(f"{indent}{mvar} = {cond_rhs}")
            ovm[f"_phi_{nid}:{th}"] = mvar
            if rewritten is None:
                rewritten = list(inc_edges)
            rewritten = [e for e in rewritten if e not in ([t_e] if t_e else []) and e not in ([f_e] if f_e else [])]
            rewritten.append({"source": f"_phi_{nid}", "source_handle": th, "target": nid, "target_handle": "" if th == "_default" else th})
    return rewritten


CMP_OPS: Dict[str, Callable[[Any, Any], bool]] = {
    "<": lambda a, b: a < b, "<=": lambda a, b: a <= b,
    ">": lambda a, b: a > b, ">=": lambda a, b: a >= b,
    "==": lambda a, b: a == b, "!=": lambda a, b: a != b,
}


def node_params(node: Dict[str, Any]) -> Dict[str, Any]:
    d = node.get("data") or {}
    return {**(d if isinstance(d, dict) else {}), **(node.get("parameters") or {}), **(d.get("parameters") or {})}


def match_catch_type(catch_type: str, err_msg: str) -> bool:
    ct, low = (catch_type or "Exception").strip(), err_msg.lower()
    if ct in ("Exception", "BaseException", "") or ct in err_msg or ct.lower() in low:
        return True
    if ct == "ZeroDivisionError" and any(k in low for k in ("zero", "math-e001", "division")):
        return True
    return ct == "ValueError" and any(k in low for k in ("domain", "math-e002", "value"))


def reset_sub_nodes(nodes: Set[str], store: Dict[str, Dict[str, Any]], skipped: Set[str], failed: Set[str]) -> None:
    for n in nodes:
        store.pop(n, None)
        skipped.discard(n)
        failed.discard(n)


def mark_skipped(nodes: Set[str], node_map: Dict[str, Any], skipped: Set[str], results: Dict[str, Dict[str, Any]], reason: str = "INFO-BRANCH-SKIPPED", skip_msg: Optional[str] = None) -> None:
    msg = skip_msg or f"Skipped ({reason})."
    for nid in nodes:
        skipped.add(nid)
        entry = results.setdefault(nid, {"node_id": nid, "node_type": norm_node_type(node_map.get(nid, {})), "outputs": {}, "duration_ms": 0.0})
        entry["status"] = "skipped"
        entry["exec_state"] = "skipped"
        if not entry.get("skip_reason"):
            entry["skip_reason"] = msg
        if not entry.get("error_semantics"):
            entry["error_semantics"] = {"code": reason, "severity": "info", "message": msg}