File size: 11,452 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
"""
ALLI Shared AST-Safe Input Transforms (_input_transforms)
Single Source of Truth for runtime execution, Python codegen, and pre-flight validation.
Supports: fallback, cast_to (int|float|str|bool|json), math_op (+|-|*|/|//|%|**), container (list|dict|tuple|set).
Strictly ZERO eval().
"""

import ast
import json
import math
import operator
from typing import Any, Callable, Dict, Iterable, Optional, Tuple

from language_core.codegen_contracts import format_literal
from language_core.core_nodes_generated import CORE_PORT_ALIASES

ALLOWED_CASTS = {"int", "float", "str", "bool", "json"}
ALLOWED_CONTAINERS = {"list", "dict", "tuple", "set"}
_MATH_OP_ALIASES = {"add": "+", "sub": "-", "mul": "*", "div": "/", "mod": "%", "pow": "**", "floordiv": "//"}
ALLOWED_MATH_OPS: Dict[str, Callable[[Any, Any], Any]] = {
    "+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv,
    "//": operator.floordiv, "%": operator.mod, "**": operator.pow,
}


def normalize_math_op(raw_op: Any) -> str:
    if raw_op is None:
        return ""
    s = str(raw_op).strip()
    return "" if not s or s.lower() == "none" else _MATH_OP_ALIASES.get(s.lower(), s)


def _coerce_fallback(raw: Any) -> Any:
    if not isinstance(raw, str):
        return raw
    ok, parsed = parse_safe_literal_or_json(raw.strip())
    return parsed if ok else raw


class TransformedExpr(str):
    """Marker str subclass preventing double application in nested resolve_input calls."""


def parse_safe_literal_or_json(raw: Any) -> Tuple[bool, Any]:
    """Safely parses a stringified JSON or Python literal without eval()."""
    if not isinstance(raw, str) or not raw.strip():
        return False, raw
    s = raw.strip()
    try:
        return True, json.loads(s)
    except Exception:
        pass
    try:
        return True, ast.literal_eval(s)
    except Exception:
        return False, raw


def _parse_math_operand(raw: Any) -> Any:
    if raw is None or raw == "":
        return 0
    if isinstance(raw, bool):
        return int(raw)
    if isinstance(raw, str):
        s = raw.strip()
        ok, parsed = parse_safe_literal_or_json(s)
        if ok and isinstance(parsed, (int, float)) and not isinstance(parsed, bool):
            val: Any = parsed
        else:
            try:
                f = float(s)
                val = int(s) if s.lstrip("+-").isdigit() else f
            except ValueError as exc:
                raise ValueError(f"Invalid numeric math_operand '{raw}'.") from exc
    elif isinstance(raw, (int, float)):
        val = raw
    else:
        raise ValueError(f"Unsupported math_operand type: {type(raw).__name__}")
    if math.isnan(float(val)) or math.isinf(float(val)):
        raise ValueError("Non-finite math_operand is not allowed in _input_transforms.")
    return val


def get_node_input_transforms(
    node: Optional[Dict[str, Any]] = None, data: Optional[Dict[str, Any]] = None, params: Optional[Dict[str, Any]] = None
) -> Dict[str, Dict[str, Any]]:
    """Extracts merged _input_transforms mapping from node, data, and parameters."""
    merged: Dict[str, Dict[str, Any]] = {}
    for src in (node, data, params):
        if isinstance(src, dict) and isinstance(src.get("_input_transforms"), dict):
            merged.update({str(k): dict(v) for k, v in src["_input_transforms"].items() if isinstance(v, dict)})
    return merged


def find_transform_for_port(
    transforms: Dict[str, Dict[str, Any]], port_ids: Iterable[str], ntype: str = ""
) -> Optional[Dict[str, Any]]:
    """Finds transform config matching any candidate port ID or canonical port alias."""
    if not transforms:
        return None
    lowered = {str(k).lower(): v for k, v in transforms.items()}
    in_aliases = CORE_PORT_ALIASES.get(ntype.lower(), {}).get("inputs", {})
    for pid in port_ids:
        key = str(pid)
        if key in transforms:
            return transforms[key]
        kl = key.lower()
        if kl in lowered:
            return lowered[kl]
        canon = in_aliases.get(kl)
        if canon and canon.lower() in lowered:
            return lowered[canon.lower()]
    return None


def apply_runtime_input_transform(val: Any, cfg: Optional[Dict[str, Any]]) -> Any:
    """Applies fallback, cast_to, math_op, and container transform identically to generated Python code."""
    if not cfg or not isinstance(cfg, dict):
        return val
    if val is None and cfg.get("fallback") not in (None, ""):
        val = _coerce_fallback(cfg["fallback"])
    cast_to = str(cfg.get("cast_to") or "").strip().lower()
    if cast_to == "none":
        cast_to = ""
    if cast_to in ALLOWED_CASTS and val is not None:
        if cast_to == "int":
            val = int(float(val.strip())) if isinstance(val, str) and "." in val else int(val.strip() if isinstance(val, str) else val)
        elif cast_to == "float":
            val = float(val)
        elif cast_to == "str":
            val = str(val)
        elif cast_to == "bool":
            val = val.strip().lower() not in ("", "false", "0", "no", "none", "null") if isinstance(val, str) else bool(val)
        elif cast_to == "json" and isinstance(val, str):
            ok, parsed = parse_safe_literal_or_json(val)
            if not ok:
                raise ValueError(f"Invalid JSON input for _input_transforms cast_to='json': {val!r}")
            val = parsed

    math_op = normalize_math_op(cfg.get("math_op"))
    if math_op:
        if math_op not in ALLOWED_MATH_OPS:
            raise ValueError(f"Unsupported math_op '{math_op}' in _input_transforms.")
        operand = _parse_math_operand(cfg.get("math_operand", 0))
        if math_op in ("/", "//", "%") and operand == 0:
            raise ZeroDivisionError(f"Division by zero in _input_transforms ('{math_op} 0').")
        val = ALLOWED_MATH_OPS[math_op](val, operand)

    container = str(cfg.get("container") or "").strip().lower()
    if container in ALLOWED_CONTAINERS and container != "none":
        if isinstance(val, str) and val.strip().startswith(("[", "(", "{")):
            ok, parsed = parse_safe_literal_or_json(val)
            val = parsed if ok else val
        if container == "dict":
            pairs = isinstance(val, (list, tuple)) and all(isinstance(x, (list, tuple)) and len(x) == 2 for x in val)
            val = dict(val) if isinstance(val, dict) or pairs else {"value": val}
        else:
            ctor = {"list": list, "tuple": tuple, "set": set}[container]
            val = ctor(val) if isinstance(val, (list, tuple, set)) else ctor([val])
    return val


def apply_codegen_input_transform(expr: str, cfg: Optional[Dict[str, Any]], cg: Any) -> str:
    """Emits AST-safe Python expression matching apply_runtime_input_transform."""
    if not cfg or not isinstance(cfg, dict) or isinstance(expr, TransformedExpr):
        return expr
    res = expr
    if cfg.get("fallback") not in (None, ""):
        fb_lit = format_literal(_coerce_fallback(cfg["fallback"]))
        res = fb_lit if res == "None" else f"({fb_lit} if {res} is None else {res})"

    cast_to = str(cfg.get("cast_to") or "").strip().lower()
    if cast_to in ALLOWED_CASTS and cast_to != "none":
        if cast_to == "json":
            cg.discovered_modules.add("json")
            res = f"(json.loads({res}) if isinstance({res}, str) else {res})"
        else:
            res = {
                "int": f"int(float({res}) if isinstance({res}, str) and '.' in {res} else {res})",
                "float": f"float({res})",
                "str": f"str({res})",
                "bool": f"({res}.strip().lower() not in ('', 'false', '0', 'no', 'none', 'null') if isinstance({res}, str) else bool({res}))",
            }[cast_to]

    math_op = normalize_math_op(cfg.get("math_op"))
    if math_op in ALLOWED_MATH_OPS:
        operand = _parse_math_operand(cfg.get("math_operand", 0))
        res = f"({res} {math_op} {format_literal(operand)})"

    container = str(cfg.get("container") or "").strip().lower()
    if container in ALLOWED_CONTAINERS and container != "none":
        cg.discovered_modules.add("ast")
        s_chk = f"isinstance({res}, str) and {res}.strip().startswith(('[', '(', '{{'))"
        i_chk = f"isinstance({res}, (list, tuple, set))"
        if container == "dict":
            res = f"(ast.literal_eval({res}) if isinstance({res}, str) and {res}.strip().startswith('{{') else (dict({res}) if isinstance({res}, dict) else {{'value': {res}}}))"
        else:
            wrap = {"list": f"[{res}]", "tuple": f"({res},)", "set": f"{{{res}}}"}[container]
            res = f"({container}(ast.literal_eval({res})) if {s_chk} else ({container}({res}) if {i_chk} else {wrap}))"
    return TransformedExpr(res)


def get_effective_transform_type(cfg: Optional[Dict[str, Any]]) -> Optional[str]:
    """Returns effective output type produced by an _input_transforms configuration."""
    if not cfg or not isinstance(cfg, dict):
        return None
    container = str(cfg.get("container") or "").strip().lower()
    if container in ALLOWED_CONTAINERS and container != "none":
        return container
    cast_to = str(cfg.get("cast_to") or "").strip().lower()
    math_op = normalize_math_op(cfg.get("math_op"))
    if math_op in ALLOWED_MATH_OPS:
        try:
            op_val = _parse_math_operand(cfg.get("math_operand", 0))
        except ValueError:
            op_val = 0
        return "float" if cast_to == "float" or math_op == "/" or isinstance(op_val, float) else "int"
    if cast_to in ("int", "float", "str", "bool"):
        return cast_to
    return "any" if cast_to == "json" else None


def validate_input_transforms_config(transforms: Any, label: str, nid: str, add_diag: Any) -> None:
    """Validates _input_transforms declarations during pre-flight validation."""
    if not isinstance(transforms, dict):
        return
    for pid, cfg in transforms.items():
        if not isinstance(cfg, dict):
            continue
        cast_to = str(cfg.get("cast_to") or "").strip().lower()
        if cast_to and cast_to != "none" and cast_to not in ALLOWED_CASTS:
            add_diag("error", "Node Validation", "VAL-PARAM-CONSTRAINT", f"Invalid cast_to '{cast_to}' on port '{pid}' of node '{label}' ({nid}).", 2, node_id=nid, port_id=str(pid))
        container = str(cfg.get("container") or "").strip().lower()
        if container and container != "none" and container not in ALLOWED_CONTAINERS:
            add_diag("error", "Node Validation", "VAL-PARAM-CONSTRAINT", f"Invalid container '{container}' on port '{pid}' of node '{label}' ({nid}).", 2, node_id=nid, port_id=str(pid))
        math_op = normalize_math_op(cfg.get("math_op"))
        if math_op and math_op not in ALLOWED_MATH_OPS:
            add_diag("error", "Expression Validation", "VAL-PARAM-CONSTRAINT", f"Unsupported math_op '{math_op}' on port '{pid}' of node '{label}' ({nid}).", 4, node_id=nid, port_id=str(pid))
        elif math_op:
            try:
                if math_op in ("/", "//", "%") and _parse_math_operand(cfg.get("math_operand", 0)) == 0:
                    add_diag("error", "Expression Validation", "MATH-E001", f"ZeroDivisionError in _input_transforms for port '{pid}' on node '{label}' ({nid}).", 4, node_id=nid, port_id=str(pid))
            except ValueError as err:
                add_diag("error", "Expression Validation", "VAL-PARAM-CONSTRAINT", f"{err} on port '{pid}' of node '{label}' ({nid}).", 4, node_id=nid, port_id=str(pid))