File size: 14,090 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 246 | """DEC-010 Track A: Runtime Dispatch, Node Parity, and Scoped Control-Flow Parity Tests."""
import ast
import io
import json
import subprocess
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from lambdas.codegen_handler import generate_code_logic
from language_core import workflow_executor
from language_core.node_schema import CANONICAL_NODE_COUNT
from language_core.executor import node_dispatch
from language_core.nodes import nodes_data, nodes_files, nodes_io, nodes_math, nodes_oop, nodes_structured_data
from language_core.workflow_executor import execute_workflow_graph
def _run_py(code: str, stdin_text: str | None = "") -> str:
ast.parse(code)
p = subprocess.run([sys.executable, "-c", code], input=stdin_text, capture_output=True, text=True, timeout=10, check=True)
return p.stdout.strip()
def test_canonical_count_and_zero_eval():
assert CANONICAL_NODE_COUNT == 47
oop_path = Path(__file__).parent.parent / "language_core" / "nodes" / "nodes_oop.py"
tree = ast.parse(oop_path.read_text(encoding="utf-8"))
eval_calls = [n for n in ast.walk(tree) if isinstance(n, ast.Call) and isinstance(n.func, ast.Name) and n.func.id == "eval"]
assert eval_calls == []
def test_dispatch_priority_hooks_and_settings(monkeypatch):
monkeypatch.setattr(workflow_executor, "_execute_variable_node", lambda d, p: {"hooked": True, "val": p.get("var_name")}, raising=False)
out_var = node_dispatch.dispatch_node_handler({}, {"var_name": "alpha"}, "core.variable", "n_var", "Var", None, {}, {"var_name": "alpha"}, {}, 5.0, [])
assert out_var == {"hooked": True, "val": "alpha"}
out_cat = node_dispatch.dispatch_node_handler({}, {"text": "alli", "operation": "upper"}, "cat_strings_collections", "n_cat", "Str", None, {}, {"text": "alli", "operation": "upper"}, {}, 5.0, [])
assert out_cat["result"] == "ALLI"
out_reg = node_dispatch.dispatch_node_handler({}, {}, "math.sqrt", "n_sqrt", "Sqrt", None, {"x": 25}, {"x": 25}, {}, 5.0, [])
assert out_reg["result"] == 5.0
try:
node_dispatch.dispatch_node_handler({}, {}, "control.for_loop", "n_for", "For", None, {"iterable": [1, 2, 3, 4]}, {"iterable": [1, 2, 3, 4]}, {"max_iterations": 2}, 5.0, [])
raise AssertionError("Expected overflow")
except Exception as exc:
assert "ERR-LOOP-OVERFLOW" in str(exc)
def test_math_domain_and_io_nontty_parity(monkeypatch):
try:
nodes_math._exec_math_sqrt({"x": -4}, {})
raise AssertionError("Expected math domain error")
except ValueError as exc:
assert "math domain error" in str(exc)
assert not str(exc).endswith("math domain error.")
monkeypatch.setattr(sys, "stdin", type("FakeTTY", (), {"isatty": staticmethod(lambda: False)})())
rt = nodes_io._exec_io_input({}, {"prompt": "Age: ", "default_value": 21, "input_type": "int"})
assert rt["value"] == 21
graph = {
"nodes": [
{"id": "n1", "node_type": "io.input", "data": {"prompt": "Enter float: ", "default_value": 3.5, "input_type": "float"}},
{"id": "n2", "node_type": "io.print", "data": {"prefix": ""}},
],
"edges": [{"source": "n1", "target": "n2", "source_handle": "value", "target_handle": "value"}],
}
cg = generate_code_logic(nodes=graph["nodes"], edges=graph["edges"], graph_name="InputNonTTY")
assert cg["success"] and _run_py(cg["python_code"]) == "3.5"
def test_safe_literal_collection_and_file_aliases(tmp_path, monkeypatch):
s_out = nodes_data._exec_data_set({}, {"initial_elements": '["a", "b"]', "operation": "add", "add_item": "c"})
assert s_out["count"] == 3 and set(s_out["result"]) == {"a", "b", "c"}
t_out = nodes_data._exec_data_tuple({}, {"items": "[10, 20, 30]"})
assert t_out["count"] == 3 and t_out["result"] == (10, 20, 30)
c_out = nodes_structured_data._exec_csv_stringify({}, {"rows": '[{"name":"Ada","score":99}]', "headers": '["name","score"]'})
assert "Ada,99" in (c_out.get("text") or c_out.get("csv_string") or c_out.get("csv") or c_out["result"])
h_res = nodes_io._exec_api_http_request({"headers": '{"Authorization": "Bearer t"}'}, {"url": "https://example.com"})
assert h_res["status_code"] == 200
http_cg = generate_code_logic(
nodes=[{"id": "h1", "node_type": "api.http_request", "data": {"url": "https://example.com", "headers": '{"Authorization": "Bearer t"}'}}],
edges=[],
graph_name="HttpHdr",
)
assert http_cg["success"] and "ast.literal_eval(" in http_cg["python_code"] and "Authorization" in http_cg["python_code"]
ast.parse(http_cg["python_code"])
monkeypatch.setenv("ALLI_SANDBOX_DIR", str(tmp_path))
w_out = nodes_files._exec_file_write({"data": "hello alli"}, {"file_path": "note.txt"})
assert w_out["success"] and (tmp_path / "note.txt").read_text() == "hello alli"
try:
nodes_files._exec_file_write({"content": "bad"}, {"file_path": "../escape.txt"})
raise AssertionError("Traversal must fail")
except Exception as exc:
assert "SEC-001" in str(exc) or "Security" in str(exc)
def test_oop_signature_method_ops_and_do_while_parity():
cls_res = nodes_oop._exec_oop_class_def({}, {"class_name": "Dog", "attributes": "name='Rex', score=0"})
inst_res = nodes_oop._exec_oop_instance({"class_in": cls_res["class_ref"]}, {"class_name": "Dog"})
assert inst_res["instance"]["name"] == "Rex"
m1 = nodes_oop._exec_oop_method_call({"instance_in": inst_res["instance"], "arg": 5}, {"operation": "set_attr", "attribute_to_modify": "score"})
assert m1["result"] == 5
nodes = [
{"id": "w1", "node_type": "control.while_loop", "data": {"initial_value": 10.0, "operator": "<", "compare_value": 5.0, "step": 1.0, "loop_mode": "do_while"}},
{"id": "p1", "node_type": "io.print", "data": {"prefix": ""}},
]
edges = [{"source": "w1", "target": "p1", "source_handle": "final_value", "target_handle": "value"}]
rt = execute_workflow_graph({"nodes": nodes, "edges": edges})
assert rt["success"] and rt["node_results"]["w1"]["outputs"]["iterations"] == 1
cg = generate_code_logic(nodes=nodes, edges=edges, graph_name="DoWhile")
assert cg["success"] and _run_py(cg["python_code"]) == rt["stdout"].strip() == "11.0"
def test_scoped_if_else_diamond_parity():
for cond_val, exp_out, skip_id in ((True, "15", "f_op"), (False, "105", "t_op")):
nodes = [
{"id": "c_cond", "node_type": "constant.bool", "data": {"value": cond_val}},
{"id": "if_1", "node_type": "control.if_else", "data": {"then_val": 5, "else_val": 5}},
{"id": "t_op", "node_type": "core.binary_op", "data": {"operator": "+", "b": 10}},
{"id": "f_op", "node_type": "core.binary_op", "data": {"operator": "+", "b": 100}},
{"id": "merge_n", "node_type": "core.binary_op", "data": {"operator": "+", "a": 0, "b": 0}},
{"id": "p_out", "node_type": "io.print", "data": {"prefix": ""}},
]
edges = [
{"source": "c_cond", "target": "if_1", "source_handle": "val", "target_handle": "condition"},
{"source": "if_1", "target": "t_op", "source_handle": "then", "target_handle": "a"},
{"source": "if_1", "target": "f_op", "source_handle": "else", "target_handle": "a"},
{"source": "t_op", "target": "merge_n", "source_handle": "result", "target_handle": "a"},
{"source": "f_op", "target": "merge_n", "source_handle": "result", "target_handle": "b"},
{"source": "merge_n", "target": "p_out", "source_handle": "result", "target_handle": "value"},
]
rt = execute_workflow_graph({"nodes": nodes, "edges": edges})
assert rt["success"] and rt["stdout"].strip() == exp_out
assert rt["node_results"][skip_id]["status"] == "skipped"
cg = generate_code_logic(nodes=nodes, edges=edges, graph_name=f"If_{cond_val}")
assert cg["success"] and _run_py(cg["python_code"]) == exp_out
def test_scoped_for_loop_with_break_parity():
nodes = [
{"id": "l1", "node_type": "control.for_loop", "data": {"iterable": "[1, 2, 3, 4]"}},
{"id": "p_item", "node_type": "io.print", "data": {"prefix": ""}},
{"id": "cmp1", "node_type": "core.compare", "data": {"operator": "==", "b": 3}},
{"id": "brk1", "node_type": "control.loop_control", "data": {"action": "break"}},
]
edges = [
{"source": "l1", "target": "p_item", "source_handle": "body", "target_handle": "value"},
{"source": "p_item", "target": "cmp1", "source_handle": "out", "target_handle": "a"},
{"source": "cmp1", "target": "brk1", "source_handle": "result", "target_handle": "condition"},
]
rt = execute_workflow_graph({"nodes": nodes, "edges": edges})
assert rt["success"]
cg = generate_code_logic(nodes=nodes, edges=edges, graph_name="LoopBreak")
assert cg["success"]
assert rt["stdout"].strip() == _run_py(cg["python_code"])
assert _run_py(cg["python_code"]).splitlines() == ["1", "2", "3"]
def test_scoped_try_except_upstream_zero_div_parity():
nodes = [
{"id": "c_num", "node_type": "constant.int", "data": {"value": 10}},
{"id": "c_zero", "node_type": "constant.int", "data": {"value": 0}},
{"id": "div_op", "node_type": "core.binary_op", "data": {"operator": "/"}},
{"id": "try_n", "node_type": "control.try_except", "data": {"default_fallback": 999}},
{"id": "p_res", "node_type": "io.print", "data": {"prefix": ""}},
]
edges = [
{"source": "c_num", "target": "div_op", "source_handle": "val", "target_handle": "a"},
{"source": "c_zero", "target": "div_op", "source_handle": "val", "target_handle": "b"},
{"source": "div_op", "target": "try_n", "source_handle": "result", "target_handle": "try_val"},
{"source": "try_n", "target": "p_res", "source_handle": "result", "target_handle": "value"},
]
rt = execute_workflow_graph({"nodes": nodes, "edges": edges})
assert rt["success"]
assert rt["node_results"]["try_n"]["outputs"]["has_error"] is True
assert rt["stdout"].strip() == "999"
cg = generate_code_logic(nodes=nodes, edges=edges, graph_name="TryUpstreamZero")
assert cg["success"] and _run_py(cg["python_code"]) == "999"
def test_scoped_while_body_and_for_continue_parity():
w_nodes = [
{"id": "w1", "node_type": "control.while_loop", "data": {"initial_value": 0, "operator": "<", "compare_value": 3, "step": 1}},
{"id": "p_iter", "node_type": "io.print", "data": {"prefix": ""}},
{"id": "p_done", "node_type": "io.print", "data": {"prefix": "done:"}},
]
w_edges = [
{"source": "w1", "target": "p_iter", "source_handle": "body", "target_handle": "exec"},
{"source": "w1", "target": "p_iter", "source_handle": "final_value", "target_handle": "value"},
{"source": "w1", "target": "p_done", "source_handle": "done", "target_handle": "exec"},
{"source": "w1", "target": "p_done", "source_handle": "final_value", "target_handle": "value"},
]
rt_w = execute_workflow_graph({"nodes": w_nodes, "edges": w_edges})
cg_w = generate_code_logic(nodes=w_nodes, edges=w_edges, graph_name="WhileBodyDone")
assert rt_w["success"] and cg_w["success"]
assert rt_w["stdout"].strip() == _run_py(cg_w["python_code"]) == "0\n1\n2\ndone: 3"
f_nodes = [
{"id": "f1", "node_type": "control.for_loop", "data": {"iterable": "[1, 2, 3]"}},
{"id": "a_cmp", "node_type": "core.compare", "data": {"operator": "==", "b": 2}},
{"id": "if_loop", "node_type": "control.if_else", "data": {"then_val": True, "else_val": False}},
{"id": "b_cont", "node_type": "control.loop_control", "data": {"action": "continue"}},
{"id": "c_print", "node_type": "io.print", "data": {"prefix": ""}},
]
f_edges = [
{"source": "f1", "target": "a_cmp", "source_handle": "body", "target_handle": "a"},
{"source": "a_cmp", "target": "if_loop", "source_handle": "result", "target_handle": "condition"},
{"source": "if_loop", "target": "b_cont", "source_handle": "then", "target_handle": "condition"},
{"source": "if_loop", "target": "c_print", "source_handle": "false", "target_handle": "exec"},
{"source": "f1", "target": "c_print", "source_handle": "body", "target_handle": "value"},
]
rt_f = execute_workflow_graph({"nodes": f_nodes, "edges": f_edges})
cg_f = generate_code_logic(nodes=f_nodes, edges=f_edges, graph_name="ForContinue")
assert rt_f["success"] and cg_f["success"]
assert rt_f["stdout"].strip() == _run_py(cg_f["python_code"]) == "1\n3"
def test_input_transform_math_alias_fallback_and_do_while_parity():
from lambdas.validate_handler import validate_workflow_protocol_logic
from language_core.input_transforms import get_effective_transform_type
assert get_effective_transform_type({"math_op": "add", "math_operand": "3"}) == "int"
nodes = [
{"id": "dw", "node_type": "control.while_loop", "data": {
"loop_variant": "do_while", "operator": "<", "compare_value": 5, "step": 1,
"_input_transforms": {"initial_value": {"fallback": "5", "math_op": "add", "math_operand": "3"}},
}},
{"id": "p", "node_type": "io.print", "data": {"prefix": "dw:"}},
]
edges = [{"source": "dw", "target": "p", "source_handle": "body", "target_handle": "exec"},
{"source": "dw", "target": "p", "source_handle": "final_value", "target_handle": "value"}]
val_res = validate_workflow_protocol_logic({"nodes": nodes, "edges": edges})
assert val_res["valid"] and not val_res.get("errors"), val_res
rt = execute_workflow_graph({"nodes": nodes, "edges": edges})
cg = generate_code_logic(nodes=nodes, edges=edges, graph_name="DoWhileTransform")
assert rt["success"] and cg["success"]
assert rt["node_results"]["dw"]["outputs"]["iteration_count"] == 1
assert rt["stdout"].strip() == _run_py(cg["python_code"]) == "dw: 8"
|