""" ALLI Workflow Executor: Compiled Single-Path Graph Execution Engine. Synthesizes standalone Python via codegen_handler and executes via RuntimeRunner for full parity across local_subprocess and docker_sandbox modes. """ import time from typing import Any, Dict, List, Optional, Set from language_core.runtime_events import ( EVENT_RUN_COMPLETED, EVENT_RUN_FAILED, GLOBAL_EVENT_STORE, ) from runtime_environment.active_runs import GLOBAL_ACTIVE_RUNS from runtime_environment.runner import DockerSandboxRunner, RuntimeRunner def _slice_upstream_subgraph(graph: Dict[str, Any], target_node_id: str) -> Dict[str, Any]: """Slice graph to include target_node_id and all upstream dependency nodes/edges.""" raw_nodes = graph.get("nodes") or [] raw_edges = graph.get("edges") if graph.get("edges") is not None else (graph.get("connections") or []) upstream: Set[str] = {str(target_node_id)} changed = True while changed: changed = False for e in raw_edges: src = str(e.get("source") or e.get("from_node") or "") tgt = str(e.get("target") or e.get("to_node") or "") if tgt in upstream and src and src not in upstream: upstream.add(src) changed = True return { **graph, "nodes": [n for n in raw_nodes if str(n.get("id")) in upstream], "edges": [ e for e in raw_edges if str(e.get("source") or e.get("from_node")) in upstream and str(e.get("target") or e.get("to_node")) in upstream ], } def execute_compiled_workflow_graph( graph: Dict[str, Any], timeout_seconds: float, mode: str, run_id: str, graph_revision: str, created_ts: str, start_total_time: float, target_node_id: Optional[str] = None, owner_user_id: Optional[str] = None, workspace_env: Optional[Dict[str, str]] = None, ) -> Dict[str, Any]: """Compile graph to Python and run via RuntimeRunner (local_subprocess or docker_sandbox).""" from language_core.executor.run_prep import scrub_result_secrets from lambdas.codegen_handler import generate_code_logic norm_mode = mode.strip().lower() if norm_mode in ("docker", "docker_sandbox"): runner_mode = "docker" mode_label = "docker_sandbox" elif norm_mode in ("local_subprocess", "local_compiled"): runner_mode = "local" mode_label = "local_subprocess" else: runner_mode = "auto" mode_label = "docker_sandbox" if DockerSandboxRunner.is_docker_available() else "local_subprocess" exec_graph = _slice_upstream_subgraph(graph, target_node_id) if target_node_id else graph codegen_res = generate_code_logic(graph=exec_graph, validate=True) execution_order: List[str] = list(codegen_res.get("execution_order") or []) if not codegen_res.get("success") or not codegen_res.get("python_code"): duration_ms = round((time.perf_counter() - start_total_time) * 1000, 2) diags = codegen_res.get("diagnostics") or [] err_msgs = [d.get("message", "Codegen/validation error") for d in diags if d.get("severity") == "error"] err_msg = "; ".join(err_msgs[:3]) or "Failed to synthesize executable Python from workflow graph." GLOBAL_EVENT_STORE.record_event( run_id=run_id, graph_revision=graph_revision, event_type=EVENT_RUN_FAILED, status="failed", code="CODEGEN-FAILED", message=err_msg, payload={"diagnostics": diags}, owner_user_id=owner_user_id, ) return { "run_id": run_id, "graph_revision": graph_revision, "cursor": GLOBAL_EVENT_STORE._seq_counters.get(run_id, 0), "success": False, "status": "failed", "mode": mode_label, "duration_ms": duration_ms, "total_duration_ms": duration_ms, "execution_order": execution_order, "execution_plan": None, "node_results": {}, "validation_errors": diags, "diagnostics": diags, "final_outputs": {}, "logs": [f"[ERROR] {err_msg}"], "stdout": "", "stderr": err_msg, "exit_code": 1, "error": err_msg, "python_code": "", } python_code = codegen_res["python_code"] runner = RuntimeRunner(default_mode=runner_mode, timeout_sec=timeout_seconds, run_id=run_id, workspace_env=workspace_env) GLOBAL_ACTIVE_RUNS.register(run_id) try: exec_res = runner.run(python_code, mode=runner_mode) except RuntimeError as exc: duration_ms = round((time.perf_counter() - start_total_time) * 1000, 2) err_str = str(exc) err_code = "ERR-SANDBOX-UNAVAILABLE" if "ERR-SANDBOX-UNAVAILABLE" in err_str else "ERR-SANDBOX-FAILED" GLOBAL_EVENT_STORE.record_event( run_id=run_id, graph_revision=graph_revision, event_type=EVENT_RUN_FAILED, status="failed", severity="error", code=err_code, message=err_str, owner_user_id=owner_user_id, ) return { "run_id": run_id, "graph_revision": graph_revision, "cursor": GLOBAL_EVENT_STORE._seq_counters.get(run_id, 0), "success": False, "status": "failed", "mode": mode_label, "code": err_code, "duration_ms": duration_ms, "total_duration_ms": duration_ms, "execution_order": execution_order, "execution_plan": None, "node_results": {}, "final_outputs": {}, "logs": [f"[ERROR] {err_str}"], "stdout": "", "stderr": err_str, "exit_code": 1, "error": err_str, "python_code": python_code, } finally: GLOBAL_ACTIVE_RUNS.unregister(run_id) total_ms = round((time.perf_counter() - start_total_time) * 1000, 2) per_node_ms = round(exec_res.duration_ms / max(len(execution_order), 1), 2) node_map = {str(n.get("id")): n for n in (exec_graph.get("nodes") or []) if n.get("id")} node_results: Dict[str, Any] = {} status_str = "success" if exec_res.success else "failed" for nid in execution_order: n = node_map.get(nid, {}) d = n.get("data") or {} ntype = str(n.get("node_type") or n.get("type") or d.get("node_type") or "").lower() lbl = d.get("label") or n.get("label") or ntype or nid outs = {"stdout": exec_res.stdout} if (exec_res.success and ("print" in ntype or "output" in ntype)) else {} node_results[nid] = { "run_id": run_id, "node_id": nid, "node_type": ntype, "label": lbl, "status": status_str, "config_state": "valid", "exec_state": status_str, "freshness": "current" if exec_res.success else "invalidated", "inputs": {}, "outputs": outs, "output_ports": {}, "duration_ms": per_node_ms, "retries_attempted": 0, "error": None if exec_res.success else (exec_res.stderr or "Execution failed"), "error_semantics": None, "skip_reason": None, "timestamp": created_ts, } GLOBAL_EVENT_STORE.record_event( run_id=run_id, graph_revision=graph_revision, event_type=EVENT_RUN_COMPLETED if exec_res.success else EVENT_RUN_FAILED, status=status_str, message=f"Compiled workflow ({exec_res.mode}) exited {exec_res.exit_code} in {round(exec_res.duration_ms, 2)}ms.", payload={"mode": exec_res.mode, "exit_code": exec_res.exit_code, "duration_ms": round(exec_res.duration_ms, 2)}, owner_user_id=owner_user_id, ) return scrub_result_secrets({ "run_id": run_id, "graph_revision": graph_revision, "cursor": GLOBAL_EVENT_STORE._seq_counters.get(run_id, 0), "success": exec_res.success, "status": status_str, "mode": exec_res.mode, "duration_ms": round(exec_res.duration_ms, 2), "total_duration_ms": total_ms, "execution_order": execution_order, "execution_plan": None, "node_results": node_results, "final_outputs": {"stdout": exec_res.stdout} if exec_res.success else {}, "logs": [f"[{exec_res.mode}] Exit {exec_res.exit_code} in {round(exec_res.duration_ms, 2)}ms"], "stdout": exec_res.stdout[:100000], "stderr": exec_res.stderr[:100000], "exit_code": exec_res.exit_code, "error": None if exec_res.success else (exec_res.stderr or "Compiled workflow execution failed."), "python_code": python_code, })