auto / language_core /input_transforms.py
rsnarsna
feat(alli-backend): Wave-1 standardization, standalone sys.env node & vault crypto, and DEC-014 security hardening
8628c98
Raw History Blame Contribute Delete
11.5 kB
"""
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))