rsnarsna
feat(runtime): add numeric string coercion in dispatch and non-blocking lifespan init
2ffd21b Download language_core/executor/node_dispatch.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 15.3 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/language_core/executor/node_dispatch.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/language_core/executor/node_dispatch.py
-
curl -L -o node_dispatch.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/language_core/executor/node_dispatch.py
15.3 kB
| """ | |
| ALLI Workflow Executor: Unified Node Dispatch Across Inline, Registry, Staging & Safe Builtin Handlers. | |
| """ | |
| import datetime as _dt | |
| import ipaddress | |
| import json as _json | |
| import math as _math | |
| import random as _rand | |
| import re | |
| import sys | |
| from typing import Any, Dict, List | |
| from urllib.parse import urlparse | |
| from language_core.executor.inline_handlers import ( | |
| _execute_binary_op_node, | |
| _execute_compare_node, | |
| _execute_constant_node, | |
| _execute_if_else_node, | |
| _execute_loop_node, | |
| _execute_math_sqrt_node, | |
| _execute_print_node, | |
| _execute_variable_node, | |
| ) | |
| from language_core.executor.legacy_cat_handlers import execute_legacy_cat_node | |
| from language_core.executor.single_node_io import prepare_registry_params | |
| from language_core.executor.state_and_errors import WorkflowExecutionError | |
| from language_core.node_schema import check_type_compatibility, default_registry | |
| from language_core.registry import CANONICAL_NODE_TYPES | |
| from library_inspector.node_synthesizer import global_staging_registry | |
| _CANONICAL_NODE_SET = frozenset(CANONICAL_NODE_TYPES) | |
| XFAIL_CONSTRUCTS = ( | |
| "control.for_loop", "control.try_except", "function.def", "function.call", | |
| "oop.class_def", "oop.instance", "oop.method_call", | |
| ) | |
| def _resolve_handler(name: str, default_fn: Any) -> Any: | |
| """Support test monkeypatching via language_core.workflow_executor.<handler>.""" | |
| facade = sys.modules.get("language_core.workflow_executor") | |
| return getattr(facade, name, default_fn) if facade is not None else default_fn | |
| def _validate_declared_inputs( | |
| node: Dict[str, Any], d: Dict[str, Any], schema_obj: Any, node_inputs: Dict[str, Any], node_id: str, label: str | |
| ) -> None: | |
| declared_inputs = [] | |
| if schema_obj and hasattr(schema_obj, "inputs"): | |
| declared_inputs = schema_obj.inputs | |
| elif "inputs" in node and isinstance(node["inputs"], list): | |
| declared_inputs = node["inputs"] | |
| elif "inputs" in d and isinstance(d["inputs"], list): | |
| declared_inputs = d["inputs"] | |
| for inp in declared_inputs: | |
| pid = inp.id if hasattr(inp, "id") else inp.get("id") or inp.get("name") | |
| ptype = inp.type if hasattr(inp, "type") else inp.get("type") or inp.get("type_hint", "Any") | |
| is_req = inp.required if hasattr(inp, "required") else inp.get("required", False) | |
| matched_key = pid if pid in node_inputs else None | |
| if matched_key is None and len(declared_inputs) == 1: | |
| matched_key = next((k for k in ("in", "input", "val", "value", "x") if k in node_inputs), None) | |
| if matched_key is None: | |
| continue | |
| if matched_key != pid and pid not in node_inputs: | |
| node_inputs[pid] = node_inputs[matched_key] | |
| inp_val = node_inputs[matched_key] | |
| if inp_val is None: | |
| if is_req and not check_type_compatibility(None, str(ptype)): | |
| raise TypeError(f"Type mismatch on required input port '{pid}' of node '{label}' ({node_id}): expected '{ptype}', received None.") | |
| else: | |
| if not check_type_compatibility(inp_val, str(ptype)): | |
| if str(ptype).lower() in ("float", "int", "number") and isinstance(inp_val, str): | |
| try: | |
| inp_val = float(inp_val) if str(ptype).lower() in ("float", "number") else int(float(inp_val)) | |
| except (ValueError, TypeError): | |
| raise TypeError(f"Runtime type violation on input '{pid}' of node '{label}' ({node_id}): expected '{ptype}', received '{type(inp_val).__name__}' with value {repr(inp_val)}.") | |
| else: | |
| raise TypeError(f"Runtime type violation on input '{pid}' of node '{label}' ({node_id}): expected '{ptype}', received '{type(inp_val).__name__}' with value {repr(inp_val)}.") | |
| if str(ptype).lower() == "float" and isinstance(inp_val, int) and not isinstance(inp_val, bool): | |
| inp_val = float(inp_val) | |
| node_inputs[matched_key] = inp_val | |
| if matched_key != pid: | |
| node_inputs[pid] = inp_val | |
| def _exec_allowlisted_import(node_inputs: Dict[str, Any], params: Dict[str, Any]) -> Dict[str, Any]: | |
| from library_inspector.safe_executor import exec_allowlisted_import | |
| return exec_allowlisted_import(node_inputs, params) | |
| def dispatch_node_handler( | |
| node: Dict[str, Any], | |
| d: Dict[str, Any], | |
| ntype: str, | |
| node_id: str, | |
| label: str, | |
| schema_obj: Any, | |
| node_inputs: Dict[str, Any], | |
| params: Dict[str, Any], | |
| settings_dict: Dict[str, Any], | |
| node_timeout: float, | |
| stdout_lines: List[str], | |
| ) -> Dict[str, Any]: | |
| """Validate declared inputs and dispatch node execution through allowlisted runtime handlers.""" | |
| if node.get("_is_xfail") and ntype in XFAIL_CONSTRUCTS: | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[ERR-NOT-SUPPORTED] {label} ({ntype}) is not supported yet in this engine") | |
| _validate_declared_inputs(node, d, schema_obj, node_inputs, node_id, label) | |
| if ntype.startswith("agent.") or ntype in ("agent.react", "agent.tool_call", "agent.react_agent", "agent.tools_connector"): | |
| raise WorkflowExecutionError( | |
| node_id=node_id, node_type=ntype, | |
| message=f"[ERR-SECURITY-GUARDED-AGENT] Guarded agent node '{ntype}' cannot be executed in untrusted runtime environment. Autonomous execution is restricted.", | |
| ) | |
| d_caps = d.get("capability_states") or params.get("capability_states") | |
| if d_caps and d_caps.get("runtime_supported") is False and ntype not in ("requests.get", "statistics.mean", "alli.primitive.datetime"): | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[EXEC_UNSUPPORTED] Node '{label}' ({node_id}) of type '{ntype}' is marked runtime_supported=False.") | |
| from library_inspector.preset_executors import exec_python_library_preset | |
| preset_res = exec_python_library_preset(ntype, d, node_inputs, params) | |
| if preset_res is not None: | |
| return preset_res | |
| if ntype == "core.variable": | |
| return _resolve_handler("_execute_variable_node", _execute_variable_node)(node_inputs, params) | |
| if ntype.startswith("cat_"): | |
| return execute_legacy_cat_node(ntype, node_inputs, params, stdout_lines) | |
| if default_registry.has(ntype) and (ntype in _CANONICAL_NODE_SET or not global_staging_registry.has(ntype)): | |
| if "function.call" in ntype or "func.call" in ntype: | |
| if not any(v is not None for v in (node_inputs.get("arg"), node_inputs.get("arg_1"), params.get("arg"), params.get("arg_val"), params.get("default_arg"))): | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[ERR-ARG-MISSING] Function call on node '{label}' ({node_id}) port 'arg' is missing required argument.") | |
| node_sch = default_registry.get(ntype) | |
| node_caps = getattr(node_sch, "capability_states", None) or (getattr(node_sch.verification, "capability_states", None) if getattr(node_sch, "verification", None) else None) | |
| if node_caps and node_caps.get("runtime_supported") is False: | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[EXEC_UNSUPPORTED] Node '{label}' ({node_id}) of type '{ntype}' is marked runtime_supported=False.") | |
| exec_params = prepare_registry_params(ntype, params, settings_dict) | |
| res = default_registry.execute(ntype, node_inputs, exec_params) | |
| if isinstance(res, dict) and "printed_string" in res: | |
| stdout_lines.append(str(res["printed_string"])) | |
| return res | |
| if global_staging_registry.has(ntype): | |
| staged_node = global_staging_registry.get(ntype) | |
| staged_rec = global_staging_registry.get_record(ntype) | |
| node_caps = getattr(staged_node, "capability_states", None) or (getattr(staged_node.verification, "capability_states", None) if getattr(staged_node, "verification", None) else None) | |
| if node_caps and node_caps.get("runtime_supported") is False: | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[EXEC_UNSUPPORTED] Staged node '{label}' ({node_id}) of type '{ntype}' is marked runtime_supported=False.") | |
| if staged_rec and staged_rec.is_supported is False: | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[EXEC_UNSUPPORTED] Staged node '{ntype}' has unsupported types or security warnings.") | |
| if not global_staging_registry.is_approved(ntype): | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[ERR-UNAUTHORIZED-STAGED-NODE] Staged node '{ntype}' is not authorized for production execution. Promotion/approval is required before running.") | |
| if default_registry.has(ntype): | |
| if not default_registry.has_executor(ntype) and staged_node is not None: | |
| from library_inspector.safe_executor import build_safe_staged_executor | |
| safe_exec = build_safe_staged_executor( | |
| staged_node, | |
| source_package=staged_rec.source_package if staged_rec else "", | |
| source_module=staged_rec.source_module if staged_rec else "", | |
| ) | |
| if safe_exec is not None: | |
| default_registry._executors[ntype] = safe_exec | |
| if not default_registry.has_executor(ntype): | |
| raise WorkflowExecutionError( | |
| node_id=node_id, | |
| node_type=ntype, | |
| message=f"[EXEC_UNSUPPORTED] Approved external node '{ntype}' requires Python codegen export or Docker Sandbox runtime.", | |
| ) | |
| return default_registry.execute(ntype, node_inputs, params) | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[EXEC_UNSUPPORTED] Approved staged node '{ntype}' is missing from production execution registry.") | |
| if "print" in ntype or "terminal" in ntype or "io.print" in ntype: | |
| return _resolve_handler("_execute_print_node", _execute_print_node)(node_inputs, params, stdout_lines) | |
| if "variable" in ntype or "var" in ntype: | |
| return _resolve_handler("_execute_variable_node", _execute_variable_node)(node_inputs, params) | |
| if "sqrt" in ntype or "math.sqrt" in ntype: | |
| return _resolve_handler("_execute_math_sqrt_node", _execute_math_sqrt_node)(node_inputs, params) | |
| if "binary" in ntype or "binary_op" in ntype: | |
| return _resolve_handler("_execute_binary_op_node", _execute_binary_op_node)(node_inputs, params) | |
| if "compare" in ntype or "cmp" in ntype: | |
| return _resolve_handler("_execute_compare_node", _execute_compare_node)(node_inputs, params) | |
| if "if_else" in ntype or "if_condition" in ntype or "control.if_else" in ntype: | |
| return _resolve_handler("_execute_if_else_node", _execute_if_else_node)(node_inputs, params) | |
| if "loop" in ntype or "for_loop" in ntype or "while_loop" in ntype: | |
| return _resolve_handler("_execute_loop_node", _execute_loop_node)(ntype, node_inputs, params, settings_dict, timeout_seconds=node_timeout) | |
| if "constant" in ntype or ("primitive" in ntype and ntype != "alli.primitive.datetime") or ntype in ("input", "primitive.input", "io.input") or ntype.endswith(".input"): | |
| return _resolve_handler("_execute_constant_node", _execute_constant_node)(ntype, node_inputs, params) | |
| if "file" in ntype or "sys.file" in ntype: | |
| file_path = str(node_inputs.get("file_path") or params.get("file_path") or "data.txt").strip() | |
| if ".." in file_path or file_path.startswith(("/", "\\")) or re.match(r"^[a-zA-Z]:", file_path) or "\0" in file_path: | |
| raise PermissionError(f"[ERR-SECURITY-PATH-TRAVERSAL] Sandboxed file path '{file_path}' attempts to escape workspace.") | |
| content = str(node_inputs.get("content") or params.get("content") or "sample content\n") | |
| if len(content.encode("utf-8")) > 1024 * 1024: | |
| raise ValueError("[ERR-FILE-SIZE-EXCEEDED] File write size exceeds 1MB safety quota.") | |
| return {"content": content, "result": content, "val": content, "out": content, "lines": content.splitlines(), "bytes": len(content.encode("utf-8"))} | |
| if "http" in ntype or "api.http_request" in ntype: | |
| url = str(node_inputs.get("url") or params.get("url") or "https://api.example.com").strip() | |
| parsed = urlparse(url) | |
| if parsed.scheme not in ("http", "https"): | |
| raise ValueError(f"[ERR-SECURITY-UNSUPPORTED-PROTOCOL] Protocol '{parsed.scheme}' is not permitted.") | |
| host = (parsed.hostname or "").strip("[]").lower() | |
| is_blocked = host in ("localhost", "127.0.0.1", "0.0.0.0", "::1", "169.254.169.254") or host.endswith((".internal", ".local")) | |
| if not is_blocked: | |
| try: | |
| ip_obj = ipaddress.ip_address(host) | |
| if isinstance(ip_obj, ipaddress.IPv6Address) and ip_obj.ipv4_mapped: | |
| ip_obj = ip_obj.ipv4_mapped | |
| is_blocked = ip_obj.is_private or ip_obj.is_loopback or ip_obj.is_link_local or ip_obj.is_multicast | |
| except ValueError: | |
| pass | |
| if is_blocked: | |
| raise PermissionError(f"[ERR-SECURITY-SSRF-BLOCKED] Access to restricted address '{host}' is blocked.") | |
| return {"response": {}, "result": {}, "val": {}, "status_code": 200, "is_success": True} | |
| if "query" in ntype or "db" in ntype: | |
| records = node_inputs.get("records_in") or params.get("records_in") or [] | |
| return {"records": records, "count": len(records), "first": records[0] if records else None, "result": len(records)} | |
| if ntype in ("lang.import", "module.import", "core.import", "alli.sys.import") or ntype.endswith(".import"): | |
| return _exec_allowlisted_import(node_inputs, params) | |
| from library_inspector.safe_executor import exec_python_library_preset | |
| preset_res = exec_python_library_preset(ntype, d, node_inputs, params) | |
| if preset_res is not None: | |
| return preset_res | |
| if "function.call" in ntype or "func.call" in ntype: | |
| arg_val = node_inputs.get("arg") or node_inputs.get("arg_1") or params.get("arg_val") or params.get("default_arg") | |
| if arg_val is None: | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[ERR-ARG-MISSING] Function call on node '{label}' ({node_id}) port 'arg' is missing required argument.") | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[ERR-NOT-SUPPORTED] Node '{ntype}' is not supported yet in this engine (scheduled for Step 4).") | |
| if "oop.method_call" in ntype or "method_call" in ntype or "try_except" in ntype or "try_catch" in ntype: | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[ERR-NOT-SUPPORTED] Node '{ntype}' is not supported yet in this engine (scheduled for Step 4).") | |
| if "." in ntype and not any(ntype.startswith(p) for p in ("alli.primitive.", "primitive.", "constant.", "data.")): | |
| raise WorkflowExecutionError(node_id=node_id, node_type=ntype, message=f"[ERR-UNKNOWN-NODE-TYPE] Unrecognized node type '{ntype}'. Cannot execute unregistered callable.") | |
| return _resolve_handler("_execute_constant_node", _execute_constant_node)(ntype, node_inputs, params) | |