rsnarsna commited on
Commit ·
c9366ea
1
Parent(s): 355ed30
test(phase5): add adversarial regression suite for step 5.5 cycle & preflight invariants
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398"""ALLI Phase 5 Step 5.5 — Adversarial Review & Invariant Verification SuiteTests all 9 adversarial invariants against the live backend implementation:1. Pure procedural cycles & disconnected cycles -> DAG-001, can_execute: False2. Approved agent reasoning cycles -> Permitted within approved structures3. Hybrid agent/procedural cycles -> Rejected with DAG-0014. Invalid loop-header feedback -> Consistently produces CTRL-0015. Fail-closed execution preflight contracts6. Direct graph execution & step execution preflight enforcement7. Planner & executor preflight rejection before starting subprocesses or side effects8. Diagnostic codes and execution eligibility consistency9. Legitimate procedural, loop, and agent workflows remain fully functional"""
import pytestfrom lambdas.validate_handler import validate_workflow_protocol_logic, validate_connection_logicfrom language_core.topology import TopologyGraph, Node, InvalidConnectionError, CycleDetectedErrorfrom language_core.types import TYPE_ANYfrom language_core.execution_planner import build_execution_planfrom language_core.workflow_executor import execute_workflow_graph, execute_workflow_step
# ==============================================================================# INVARIANT 1: Pure Procedural Cycles & Disconnected Cycles Rejected with DAG-001# ==============================================================================
def test_inv1_pure_procedural_cycles_rejected_with_dag001(): # 2-node cycle g_2node = { "nodes": [ {"id": "a", "node_type": "math.add", "data": {"label": "Add A"}}, {"id": "b", "node_type": "math.add", "data": {"label": "Add B"}}, ], "edges": [ {"id": "e1", "source": "a", "target": "b"}, {"id": "e2", "source": "b", "target": "a"}, ], } res2 = validate_workflow_protocol_logic(g_2node) assert not res2["valid"] assert not res2["can_execute"] assert any(d["code"] == "DAG-001" for d in res2["diagnostics"])
# 4-node long cycle g_4node = { "nodes": [ {"id": "n1", "node_type": "math.add", "data": {"label": "Node 1"}}, {"id": "n2", "node_type": "math.multiply", "data": {"label": "Node 2"}}, {"id": "n3", "node_type": "math.subtract", "data": {"label": "Node 3"}}, {"id": "n4", "node_type": "math.divide", "data": {"label": "Node 4"}}, ], "edges": [ {"id": "e1", "source": "n1", "target": "n2"}, {"id": "e2", "source": "n2", "target": "n3"}, {"id": "e3", "source": "n3", "target": "n4"}, {"id": "e4", "source": "n4", "target": "n1"}, ], } res4 = validate_workflow_protocol_logic(g_4node) assert not res4["valid"] assert not res4["can_execute"] dag_diags = [d for d in res4["diagnostics"] if d["code"] == "DAG-001"] assert len(dag_diags) > 0 assert "Node 1" in dag_diags[0]["message"]
def test_inv1_disconnected_procedural_cycles_rejected(): # Linear component (v1 -> p1) + Disconnected cycle (c1 <-> c2) g_disc = { "nodes": [ {"id": "v1", "node_type": "constant.int", "data": {"label": "Var", "customData": {"var_name": "x", "value": 10}}}, {"id": "p1", "node_type": "core.print", "data": {"label": "Print"}}, {"id": "c1", "node_type": "math.multiply", "data": {"label": "Cycle 1"}}, {"id": "c2", "node_type": "math.multiply", "data": {"label": "Cycle 2"}}, ], "edges": [ {"id": "e_valid", "source": "v1", "source_handle": "val", "target": "p1", "target_handle": "value"}, {"id": "e_c1", "source": "c1", "target": "c2"}, {"id": "e_c2", "source": "c2", "target": "c1"}, ], } res = validate_workflow_protocol_logic(g_disc) assert not res["valid"] assert not res["can_execute"] assert any(d["code"] == "DAG-001" for d in res["diagnostics"])
def test_inv1_self_loop_procedural_node(): # Direct self connection on ordinary node g_self = { "nodes": [ {"id": "m1", "node_type": "math.add", "data": {"label": "Add Self"}}, ], "edges": [ {"id": "e_self", "source": "m1", "target": "m1"}, ], } nodes_map = {"m1": g_self["nodes"][0]} conn_res = validate_connection_logic( connection={"source": "m1", "target": "m1", "from_node": "m1", "to_node": "m1"}, nodes_map=nodes_map, ) assert not conn_res["valid"] assert conn_res["code"] == "DAG-001"
# ==============================================================================# INVARIANT 2: Approved Agent Reasoning Cycles Permitted ONLY Within Approved Structures# ==============================================================================
def test_inv2_approved_agent_reasoning_cycle_permitted(): agent_graph = { "name": "Approved Agent Graph", "nodes": [ {"id": "prompt_in", "node_type": "io.input", "data": {"label": "Prompt", "customData": {"value": "What is 2+2?"}}}, {"id": "agent_llm", "node_type": "alli.agent.llm", "data": {"label": "LLM", "category": "Agent Logic", "customData": {"model": "gpt-4o"}}}, { "id": "agent_tool", "node_type": "alli.agent.tool", "data": { "label": "Calculator", "category": "Agent Tools", "customData": {"tool_name": "calc"}, "spec": { "inputs": [{"id": "query", "name": "query", "type": "str", "required": False}], "outputs": [{"id": "result", "name": "result", "type": "str"}], }, }, }, ], "edges": [ {"id": "e_in", "source": "prompt_in", "source_handle": "output", "target": "agent_llm", "target_handle": "prompt"}, {"id": "e_fwd", "source": "agent_llm", "source_handle": "response", "target": "agent_tool", "target_handle": "query"}, {"id": "e_back", "source": "agent_tool", "source_handle": "result", "target": "agent_llm", "target_handle": "messages"}, ], } res = validate_workflow_protocol_logic(agent_graph) assert not any(d["code"] in ("DAG-001", "CTRL-001") for d in res["diagnostics"])
plan = build_execution_plan(agent_graph) assert plan["valid"] is True assert plan["can_execute"] is True assert plan["execution_order"] == ["prompt_in", "agent_llm", "agent_tool"]
def test_inv2_tool_to_tool_cycle_without_reasoner_rejected(): # Two tools cycling with each other without any LLM, reasoner, or memory two_tools_graph = { "name": "Two Tools Cycle", "nodes": [ {"id": "prompt_in", "node_type": "io.input", "data": {"label": "Prompt", "customData": {"value": "test"}}}, { "id": "tool_1", "node_type": "alli.agent.tool", "data": {"label": "Tool 1", "category": "Agent Tools", "spec": {"inputs": [{"id": "in", "name": "in", "required": False}]}}, }, { "id": "tool_2", "node_type": "alli.agent.tool", "data": {"label": "Tool 2", "category": "Agent Tools", "spec": {"inputs": [{"id": "in", "name": "in", "required": False}]}}, }, ], "edges": [ {"id": "e0", "source": "prompt_in", "target": "tool_1"}, {"id": "e1", "source": "tool_1", "target": "tool_2"}, {"id": "e2", "source": "tool_2", "target": "tool_1"}, ], } res = validate_workflow_protocol_logic(two_tools_graph) assert not res["valid"] assert not res["can_execute"] assert any(d["code"] == "DAG-001" for d in res["diagnostics"])
# ==============================================================================# INVARIANT 3: Hybrid Agent/Procedural Cycles Rejected# ==============================================================================
def test_inv3_hybrid_agent_procedural_cycle_rejected(): # LLM Reasoner -> math.add -> LLM Reasoner hybrid_graph = { "name": "Hybrid Cycle", "nodes": [ {"id": "agent_llm", "node_type": "alli.agent.llm", "data": {"label": "LLM Reasoner", "category": "Agent Logic"}}, {"id": "math_add", "node_type": "math.add", "data": {"label": "Add Node", "category": "Math"}}, ], "edges": [ {"id": "e1", "source": "agent_llm", "target": "math_add"}, {"id": "e2", "source": "math_add", "target": "agent_llm"}, ], } res = validate_workflow_protocol_logic(hybrid_graph) assert not res["valid"] assert not res["can_execute"] dag_diags = [d for d in res["diagnostics"] if d["code"] == "DAG-001"] assert len(dag_diags) > 0 assert "Dependency Cycle Detected" in dag_diags[0]["message"]
def test_inv3_multi_node_hybrid_cycle_rejected(): # LLM -> Tool -> Math -> LLM multi_hybrid = { "nodes": [ {"id": "llm", "node_type": "alli.agent.llm", "data": {"label": "LLM", "category": "Agent Logic"}}, {"id": "tool", "node_type": "alli.agent.tool", "data": {"label": "Tool", "category": "Agent Tools"}}, {"id": "proc", "node_type": "core.transform", "data": {"label": "Procedural Transform"}}, ], "edges": [ {"id": "e1", "source": "llm", "target": "tool"}, {"id": "e2", "source": "tool", "target": "proc"}, {"id": "e3", "source": "proc", "target": "llm"}, ], } res = validate_workflow_protocol_logic(multi_hybrid) assert not res["valid"] assert not res["can_execute"] assert any(d["code"] == "DAG-001" for d in res["diagnostics"])
# ==============================================================================# INVARIANT 4: Invalid Loop-Header Feedback Consistently Produces CTRL-001# ==============================================================================
def test_inv4_for_loop_header_feedback_ctrl001(): # For loop header feedback wire for_graph = { "nodes": [ {"id": "for_hdr", "node_type": "control.for_loop", "data": {"label": "ForLoop Header", "customData": {"range_count": 5}}}, {"id": "body_step", "node_type": "core.print", "data": {"label": "Print Body"}}, ], "edges": [ {"id": "e_fwd", "source": "for_hdr", "source_handle": "item", "target": "body_step", "target_handle": "value"}, {"id": "e_back", "source": "body_step", "source_handle": "out", "target": "for_hdr", "target_handle": "iterable"}, ], } res = validate_workflow_protocol_logic(for_graph) assert not res["valid"] assert not res["can_execute"] ctrl_diags = [d for d in res["diagnostics"] if d["code"] == "CTRL-001"] assert len(ctrl_diags) > 0 assert ctrl_diags[0]["node_id"] == "for_hdr" assert ctrl_diags[0]["port_id"] == "iterable" assert ctrl_diags[0]["edge_id"] == "e_back"
def test_inv4_while_loop_header_feedback_ctrl001(): # While loop header feedback wire while_graph = { "nodes": [ {"id": "while_hdr", "node_type": "control.while_loop", "data": {"label": "While Header", "customData": {"condition": True}}}, {"id": "step_1", "node_type": "math.add", "data": {"label": "Step 1"}}, ], "edges": [ {"id": "e1", "source": "while_hdr", "target": "step_1"}, {"id": "e2", "source": "step_1", "target": "while_hdr"}, ], } res = validate_workflow_protocol_logic(while_graph) assert not res["valid"] assert not res["can_execute"] ctrl_diags = [d for d in res["diagnostics"] if d["code"] == "CTRL-001"] assert len(ctrl_diags) > 0 assert ctrl_diags[0]["node_id"] == "while_hdr"
def test_inv4_topology_graph_raises_ctrl001_on_loop(): top = TopologyGraph() n_for = Node("for_node", label="For Loop", node_type="control.for_loop") n_for.add_input("iterable", TYPE_ANY) n_for.add_output("item", TYPE_ANY)
n_step = Node("step_node", label="Print Node", node_type="core.print") n_step.add_input("value", TYPE_ANY) n_step.add_output("out", TYPE_ANY)
top.add_node(n_for) top.add_node(n_step) top.connect("for_node", "item", "step_node", "value")
# Connecting back to loop header must raise InvalidConnectionError with code CTRL-001 with pytest.raises(InvalidConnectionError) as exc_info: top.connect("step_node", "out", "for_node", "iterable") assert exc_info.value.code == "CTRL-001"
# ==============================================================================# INVARIANT 6: Direct Backend Graph Execution & Step Execution Enforce Preflight# ==============================================================================
def test_inv6_direct_backend_graph_execution_enforces_preflight(): invalid_graph = { "nodes": [ {"id": "x", "node_type": "math.add", "data": {"label": "X"}}, {"id": "y", "node_type": "math.add", "data": {"label": "Y"}}, ], "edges": [ {"id": "e1", "source": "x", "target": "y"}, {"id": "e2", "source": "y", "target": "x"}, ], } res = execute_workflow_graph(invalid_graph) assert res["success"] is False assert res["status"] == "failed" assert res["exit_code"] == 1 assert "Workflow validation failed" in res["error"] assert len(res["final_outputs"]) == 0
def test_inv6_direct_backend_step_execution_enforces_preflight(): invalid_graph = { "nodes": [ {"id": "x", "node_type": "math.add", "data": {"label": "X"}}, {"id": "y", "node_type": "math.add", "data": {"label": "Y"}}, ], "edges": [ {"id": "e1", "source": "x", "target": "y"}, {"id": "e2", "source": "y", "target": "x"}, ], } res = execute_workflow_step(invalid_graph, target_node_id="x") assert res["success"] is False assert res["status"] == "failed" assert res["exit_code"] == 1 assert "Workflow step execution blocked" in res["logs"][0]
# ==============================================================================# INVARIANT 7: Planner Rejects Invalid Graphs Before Subprocesses / Side Effects# ==============================================================================
def test_inv7_planner_preflight_blocks_staging_and_execution(): cyclic_graph = { "nodes": [ {"id": "a", "node_type": "math.multiply", "data": {"label": "A"}}, {"id": "b", "node_type": "math.multiply", "data": {"label": "B"}}, ], "edges": [ {"id": "e1", "source": "a", "target": "b"}, {"id": "e2", "source": "b", "target": "a"}, ], } plan = build_execution_plan(cyclic_graph) assert plan["valid"] is False assert plan["can_execute"] is False assert plan["execution_order"] == [] assert plan["stages"] == [] assert len(plan["validation_errors"]) > 0
# ==============================================================================# INVARIANT 8: Diagnostic Codes Consistency# ==============================================================================
def test_inv8_diagnostic_codes_and_fields_consistent(): graph_cycle = { "nodes": [ {"id": "node_a", "node_type": "math.add", "data": {"label": "Add A"}}, {"id": "node_b", "node_type": "math.add", "data": {"label": "Add B"}}, ], "edges": [ {"id": "e1", "source": "node_a", "target": "node_b"}, {"id": "e2", "source": "node_b", "target": "node_a"}, ], } res = validate_workflow_protocol_logic(graph_cycle) diag = next(d for d in res["diagnostics"] if d["code"] == "DAG-001") assert diag["severity"] == "error" assert diag["category"] == "Connection Validation" assert diag["node_id"] in ("node_a", "node_b") assert "fix_suggestion" in diag or "fix" in diag
# ==============================================================================# INVARIANT 9: Legitimate Procedural, Loop, and Agent Workflows Remain Functional# ==============================================================================
def test_inv9_legitimate_procedural_workflow_executes_successfully(): valid_proc = { "name": "Valid Procedural Pipeline", "nodes": [ {"id": "v1", "node_type": "constant.int", "data": {"label": "Const", "customData": {"var_name": "val", "value": 49}}}, {"id": "m1", "node_type": "math.sqrt", "data": {"label": "Sqrt"}}, {"id": "p1", "node_type": "core.print", "data": {"label": "Print"}}, ], "edges": [ {"id": "e1", "source": "v1", "source_handle": "val", "target": "m1", "target_handle": "x"}, {"id": "e2", "source": "m1", "source_handle": "result", "target": "p1", "target_handle": "value"}, ], } val = validate_workflow_protocol_logic(valid_proc) assert val["valid"] is True, f"Valid workflow rejected: {val['diagnostics']}" assert val["can_execute"] is True
exec_res = execute_workflow_graph(valid_proc) assert exec_res["success"] is True assert exec_res["exit_code"] == 0 assert exec_res["node_results"]["m1"]["outputs"]["result"] == 7.0 assert "7.0" in exec_res["stdout"]tests/test_adversarial_step5_5.pyAdded+398