File size: 18,164 Bytes
8628c98 ee7ac62 8628c98 ee7ac62 8628c98 ee7ac62 8628c98 ee7ac62 8628c98 ee7ac62 8628c98 ee7ac62 8628c98 ee7ac62 8628c98 ee7ac62 8628c98 ee7ac62 | 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 Validation Subpackage: Per-Node Schema, Required Ports & Parameter Validation.
"""
from typing import Any, Callable, Dict, List, Set
from language_core.core_nodes_generated import CORE_PARAM_ALIASES, CORE_PORT_ALIASES
from language_core.input_transforms import find_transform_for_port, get_node_input_transforms, validate_input_transforms_config
from language_core.node_schema import default_registry, get_effective_node_schema
from language_core.parameter_validator import (
is_non_finite_number,
validate_parameter_value,
validate_python_identifier,
)
from library_inspector.node_synthesizer import global_staging_registry
from lambdas.validation.security_checks import validate_node_security_and_deps
from lambdas.validation.types_and_expr import is_configured_port_value, validate_expression_logic
KNOWN_PREFIXES = (
"cat_", "cat.", "fundamentals.", "alli.primitive.", "alli.func.", "alli.oop.", "alli.sys.", "alli.agent.", "alli.lang.",
"primitive.", "func.", "oop.", "sys.", "agent.", "constant.", "control.", "io.", "math.", "core.", "operator.", "workflow.", "trigger.", "api.", "file.", "json.", "csv.", "data.", "lang.", "module.", "db.",
)
KNOWN_EXACT_TYPES = {
"variable", "constant", "binary_op", "operator", "math", "input", "output", "display.output", "print", "print_output", "if_else", "condition", "control.branch", "control.if_else", "for_loop", "while_loop", "loop", "function", "function.def", "function.call", "class", "class_def", "try_except", "try_catch", "start", "workflow_group", "system.workflow_group", "group", "webhook", "schedule", "manual_trigger", "text_prompt", "file_input", "http_request", "response_json", "api_response", "lang.import", "module.import", "core.import", "core.scope", "db.query", "sys.file", "numpy.array", "pandas.dataframe", "requests.get", "statistics.mean", "alli.sys.import", "alli.sys.env", "alli.primitive.datetime",
}
def resolve_inputs_for_node(n: Dict[str, Any]) -> List[Dict[str, Any]]:
d = n.get("data", {})
ntype = str(n.get("node_type") or n.get("type") or d.get("node_type") or "")
reg_schema = get_effective_node_schema(ntype)
if reg_schema:
return [
{"id": p.id, "name": p.name, "type": p.type, "required": p.required, "default_value": p.default_value}
for p in reg_schema.inputs
]
spec = d.get("spec", {})
inputs = spec.get("inputs") or d.get("inputs") or n.get("inputs")
if inputs and isinstance(inputs, list):
return inputs
t, lbl = ntype.lower(), str(d.get("label") or "").lower()
if "constant" in t or t.startswith("constant.") or t in ("input", "constant"):
return []
if "math.sqrt" in t or "sqrt" in lbl:
return [{"id": "x", "name": "x", "type": "float", "required": True}]
if "binary" in t or "calc" in lbl or "add" in lbl or "operator" in t:
return [{"id": "a", "name": "a", "type": "any", "required": True}, {"id": "b", "name": "b", "type": "any", "required": True}]
if "if" in t or ("condition" in lbl and "true" not in lbl and "false" not in lbl):
return [{"id": "condition", "name": "condition", "type": "bool", "required": True}]
if "print" in t or "display" in t or "io.print" in t:
return [{"id": "value", "name": "value", "type": "any", "required": True}]
if "for_loop" in t:
return [{"id": "iterable", "name": "iterable", "type": "any", "required": True}]
if "while_loop" in t:
return [{"id": "condition", "name": "condition", "type": "bool", "required": True}]
return []
def is_known_node_type(ntype: str, graph_deps: Set[str], std_libs: Set[str]) -> bool:
t = ntype.lower().strip()
if not t:
return False
if default_registry.get(t) or any(t.startswith(p) for p in KNOWN_PREFIXES) or t in KNOWN_EXACT_TYPES or t in graph_deps or t in std_libs:
return True
if "." in t:
mod = t.split(".")[0].lower()
return mod in std_libs or mod in ("alli", "system", "primitive", "constant", "control", "io", "math", "custom", "display", "core", "operator", "lang", "module", "data", "agent", "db", "sys") or mod in graph_deps
return False
def _validate_schema_and_staging(nid: str, ntype: str, label: str, n: Dict[str, Any], d: Dict[str, Any], graph_deps: Set[str], std_libs: Set[str], add_diag: Callable[..., None]) -> None:
node_schema_obj = get_effective_node_schema(ntype)
staged_rec = global_staging_registry.get_record(ntype) if global_staging_registry.has(ntype) else None
node_caps = None
if node_schema_obj:
node_caps = getattr(node_schema_obj, "capability_states", None) or getattr(getattr(node_schema_obj, "verification", None), "capability_states", None)
if hasattr(node_caps, "model_dump"):
node_caps = node_caps.model_dump()
if not node_caps:
node_caps = (
d.get("capability_states")
or n.get("capability_states")
or (n.get("parameters") or {}).get("capability_states")
or (d.get("verification") or {}).get("capability_states")
or (staged_rec.capability_states if staged_rec else None)
)
if default_registry.has(ntype) and default_registry.has_executor(ntype):
return
if node_caps and node_caps.get("runtime_supported") is False and ntype not in ("requests.get", "statistics.mean", "alli.primitive.datetime"):
add_diag("error", "Execution Validation", "EXEC_UNSUPPORTED", f"[VAL-UNSUPPORTED-RUNTIME] Execution Unsupported: Node '{label}' ({nid}) of type '{ntype}' is marked runtime_supported=False and cannot be executed in the runtime engine.", 8, node_id=nid, fix="Use a runtime-supported node implementation or export python code for external execution.")
elif staged_rec and staged_rec.is_supported is False and ntype not in ("requests.get", "statistics.mean", "alli.primitive.datetime"):
add_diag("error", "Execution Validation", "EXEC_UNSUPPORTED", f"[VAL-UNSUPPORTED-RUNTIME] Execution Unsupported: Staged node '{label}' ({nid}) of type '{ntype}' contains unsupported types or security violations.", 8, node_id=nid, fix="Ensure all port types are supported and security constraints are satisfied.")
elif global_staging_registry.has(ntype) and not global_staging_registry.is_approved(ntype) and not default_registry.has(ntype) and ntype != "requests.get":
add_diag("error", "Execution Validation", "RULE-NODE-STAGED-UNAPPROVED", f"Unapproved Staged Node: Node '{label}' ({nid}) uses introspected staged schema '{ntype}' which has not been authorized for production execution.", 8, node_id=nid, fix="An authorized administrator must approve this staged node before workflow execution.")
elif global_staging_registry.has(ntype) and global_staging_registry.is_approved(ntype) and not default_registry.has(ntype):
if ntype not in ("requests.get", "statistics.mean", "alli.primitive.datetime"):
add_diag("error", "Execution Validation", "EXEC_UNSUPPORTED", f"[VAL-UNSUPPORTED-RUNTIME] Execution Handler Missing: Approved staged node '{label}' ({nid}) of type '{ntype}' has no registered execution handler in runtime registry.", 8, node_id=nid, fix="Register an executable callable handler for this staged node in the execution registry.")
elif not is_known_node_type(ntype, graph_deps, std_libs):
add_diag("error", "Node Validation", "VAL-UNKNOWN-NODE-TYPE", f"Unregistered Node Type: Node '{label}' ({nid}) specifies unknown type '{ntype}' with no registered execution handler.", 2, node_id=nid, fix="Select a valid standard node schema from the library palette or register its definition.")
def _validate_required_inputs(nid: str, ntype: str, label: str, n: Dict[str, Any], node_incoming: List[Dict[str, Any]], combined_params: Dict[str, Any], add_diag: Callable[..., None]) -> None:
input_defs = resolve_inputs_for_node(n)
data_port_aliases = {"in", "input", "value", "val", "data", "text", "x", "collection", "items", "args"}
single_req = len(input_defs) == 1 or len([i for i in input_defs if i.get("required")]) == 1
node_aliases = CORE_PORT_ALIASES.get(ntype.lower(), {}).get("inputs", {})
for inp in input_defs:
if not inp.get("required", False):
continue
port_id = inp.get("id") or inp.get("name")
port_name = inp.get("name") or port_id
pid_low, pname_low = str(port_id).lower(), str(port_name).lower()
is_data_port = pid_low in data_port_aliases or pname_low in data_port_aliases
def _wire_matches(e: Dict[str, Any]) -> bool:
tgt = str(e.get("targetHandle") or e.get("to_port") or e.get("target_handle") or "").strip().lower()
if tgt in (pid_low, pname_low):
return True
canon = node_aliases.get(tgt)
if canon and canon.lower() in (pid_low, pname_low):
return True
if (not tgt or (is_data_port and tgt in data_port_aliases)) and single_req:
return True
if pid_low == "value" and tgt in ("in", "input", "val", "data"):
return True
return pid_low == "in" and tgt in ("value", "input", "val", "data")
has_wire = any(_wire_matches(e) for e in node_incoming)
port_type = str(inp.get("type") or inp.get("type_hint") or "any")
alias_keys = [ak for ak, av in node_aliases.items() if str(av).lower() in (pid_low, pname_low)]
param_aliases = CORE_PARAM_ALIASES.get(ntype.lower(), {})
alias_keys.extend(ak for ak, av in param_aliases.items() if str(av).lower() in (pid_low, pname_low))
check_keys = [port_id, port_name, f"param_{port_id}", f"param_{port_name}", *alias_keys]
has_val = any(k in combined_params and is_configured_port_value(combined_params[k], expected_type=port_type) for k in check_keys)
if not has_val:
t_cfg = find_transform_for_port(get_node_input_transforms(n, n.get("data"), combined_params), [port_id, port_name], ntype)
if t_cfg and t_cfg.get("fallback") is not None:
has_val = True
if not has_val and inp.get("default_value") is not None:
has_val = is_configured_port_value(inp["default_value"], expected_type=port_type)
if not has_wire and not has_val:
add_diag("error", "Data Validation", "VAL-REQUIRED-INPUT-MISSING", f"Required Input Missing: Port '{port_id}' on node '{label}' ({nid}) is required but unconnected and has no configured value.", 6, node_id=nid, port_id=port_id, fix=f"Connect an output wire to port '{port_id}' or provide a parameter value in the configuration drawer.")
def validate_all_nodes(
nodes: List[Dict[str, Any]],
incoming_map: Dict[str, List[Dict[str, Any]]],
graph_deps: Set[str],
std_libs: Set[str],
add_diag: Callable[..., None],
) -> None:
"""Validate every node's schema, required inputs, parameter constraints, expressions, and security."""
for n in nodes:
nid = n.get("id")
d = n.get("data", {}) if isinstance(n.get("data"), dict) else {}
custom = d.get("customData") if isinstance(d.get("customData"), dict) else {}
top_params = n.get("parameters") if isinstance(n.get("parameters"), dict) else (n.get("params") if isinstance(n.get("params"), dict) else {})
params = d.get("parameters") if isinstance(d.get("parameters"), dict) else (d.get("params") if isinstance(d.get("params"), dict) else {})
values = d.get("values") if isinstance(d.get("values"), dict) else {}
inputs_dict = n.get("inputs") if isinstance(n.get("inputs"), dict) else (d.get("inputs") if isinstance(d.get("inputs"), dict) else {})
combined = {**d, **custom, **values, **inputs_dict, **params, **top_params}
ntype = str(n.get("node_type") or n.get("type") or d.get("node_type") or d.get("nodeType") or "")
label = str(d.get("label") or ntype or "Node")
_validate_schema_and_staging(nid, ntype, label, n, d, graph_deps, std_libs, add_diag)
_validate_required_inputs(nid, ntype, label, n, incoming_map.get(nid, []), combined, add_diag)
if "binary" in ntype.lower() or "calc" in label.lower() or "operator" in ntype.lower():
if (combined.get("operator") or "+") in ("/", "//", "%") and combined.get("b") in (0, "0"):
add_diag("error", "Expression Validation", "MATH-E001", f"ZeroDivisionError: Division or modulo by literal zero in node '{label}' ({nid}).", 4, node_id=nid, port_id="b", fix="Operand B cannot be zero for division ('/') or modulo ('%').")
if "sqrt" in ntype.lower() or "sqrt" in label.lower():
x_val = combined.get("x")
if x_val is not None and x_val != "":
try:
if float(x_val) < 0:
add_diag("error", "Expression Validation", "MATH-E002", f"Math Domain Error: Node '{label}' ({nid}) receives negative literal {x_val} for square root.", 4, node_id=nid, port_id="x", fix="Square root requires a non-negative number (x >= 0).")
except ValueError:
pass
if ntype in ("core.type_cast", "type_cast", "custom.type_cast") or "type_cast" in str(ntype).lower():
from language_core.executor.wave1_handlers import normalize_cast_target, strict_cast_value
raw_ttype = combined.get("target_type")
if raw_ttype not in (None, ""):
try:
clean_ttype = normalize_cast_target(raw_ttype)
except ValueError as ve:
add_diag("error", "Data Validation", "VAL-PARAM-CONSTRAINT", str(ve), 6, node_id=nid, port_id="target_type", fix="Choose one of int, float, str, bool, list, dict, json.")
clean_ttype = None
if clean_ttype and combined.get("value") not in (None, ""):
try:
strict_cast_value(combined["value"], clean_ttype)
except ValueError:
add_diag("error", "Data Validation", "ERR-TYPE-CAST", f"Type Cast Error: Cannot cast literal '{combined['value']}' to {clean_ttype} on node '{label}' ({nid}).", 6, node_id=nid, port_id="value", fix=f"Provide a valid {clean_ttype} literal.")
if ntype in ("sys.env_get", "sys.env_set", "alli.sys.env", "sys.env"):
from language_core.workspace_env_contracts import validate_env_key
raw_env_key = combined.get("key")
if raw_env_key not in (None, ""):
ok_env, code_env, err_env = validate_env_key(raw_env_key)
if not ok_env:
add_diag("error", "Security", code_env, err_env, 6, node_id=nid, port_id="key", fix="Use a non-reserved UPPER_SNAKE_CASE environment key (e.g. APP_MODE).")
if ntype in ("function.call", "func.call"):
has_wire = any(str(e.get("target_handle") or e.get("targetHandle") or "").lower() in ("arg", "arg_1", "args", "value") for e in incoming_map.get(nid, []))
if not has_wire and combined.get("arg") in (None, ""):
add_diag("error", "Data Validation", "ERR-ARG-MISSING", f"Function call on node '{label}' ({nid}) port 'arg' is missing required argument.", 6, node_id=nid, port_id="arg", fix="Connect an argument to port 'arg' or configure it in the parameters.")
for pk, pv in combined.items():
found_nf, tok_nf = is_non_finite_number(pv)
if found_nf:
add_diag("error", "Node Validation", "VAL-PARAM-NON-FINITE", f"Non-Finite Number Error: Parameter '{pk}' on node '{label}' ({nid}) contains non-finite numeric value '{tok_nf}' (NaN or Infinity). Only finite numeric values are permitted.", 2, node_id=nid, port_id=pk, fix=f"Replace '{tok_nf}' with a valid finite number.")
validated_param_ids: Set[str] = set()
node_schema = get_effective_node_schema(ntype)
if node_schema and node_schema.parameters:
for param in node_schema.parameters:
validated_param_ids.add(param.id)
pval = combined[param.id] if param.id in combined else (custom[param.id] if param.id in custom else (combined[f"param_{param.id}"] if f"param_{param.id}" in combined else param.default))
p_valid, p_code, p_msg, p_fix = validate_parameter_value(param, pval, node_label=label, node_id=nid)
if not p_valid:
add_diag("error", "Node Validation", p_code or "VAL-PARAM-CONSTRAINT", p_msg or f"Parameter '{param.name}' violates declared schema constraint.", 2, node_id=nid, port_id=param.id, fix=p_fix)
is_var_or_const = "variable" in ntype.lower() or "constant" in ntype.lower()
for id_field in ("var_name", "variable_name", "const_name", "func_name", "fn_name", "function_name", "class_name", "loop_var", "iter_var", "target_var"):
if id_field in combined and (id_field not in validated_param_ids or (is_var_or_const and id_field == "const_name")):
if id_field in ("var_name", "variable_name", "const_name") and not is_var_or_const:
continue
if combined[id_field] is not None:
ok, code, msg, fix = validate_python_identifier(combined[id_field], field_id=id_field)
if not ok:
add_diag("error", "Node Validation", code or "COMP-E101", f"{msg} on node '{label}' ({nid}).", 2, node_id=nid, port_id=id_field, fix=fix)
if "condition" in combined or "if" in ntype.lower():
cond = str(combined.get("condition") or "True")
expr_res = validate_expression_logic(cond)
if not expr_res.get("valid"):
add_diag("error", "Expression Validation", "COMP-E102", f"Syntax error in condition expression '{cond}': {expr_res.get('error')}", 4, node_id=nid, port_id="condition", fix="Check for balanced parentheses, quotes, and valid Python syntax.")
validate_input_transforms_config(combined.get("_input_transforms"), label, nid, add_diag)
validate_node_security_and_deps(nid, ntype, label, combined, graph_deps, std_libs, add_diag)
|