rsnarsna
feat(alli-backend): Wave-1 standardization, standalone sys.env node & vault crypto, and DEC-014 security hardening
8628c98 Download lambdas/run_handler.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 6.01 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/lambdas/run_handler.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/lambdas/run_handler.py
-
curl -L -o run_handler.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/lambdas/run_handler.py
6.01 kB
| """ | |
| ALLI Lambda Function 1: Code Execution Sandbox (alli-run) | |
| Air-gapped execution of user Python code & workflow graphs with strict timeouts, isolation, and credential scrubbing. | |
| """ | |
| import json | |
| from typing import Any, Dict, Optional | |
| from runtime_environment.runner import RuntimeRunner | |
| runner = RuntimeRunner(default_mode="auto", timeout_sec=5.0) | |
| def execute_code_logic( | |
| code: str, | |
| timeout_seconds: float = 5.0, | |
| mode: str = "local", | |
| workspace_env: Optional[Dict[str, str]] = None, | |
| ) -> Dict[str, Any]: | |
| """Pure business logic for sandboxed execution, shared by FastAPI and AWS Lambda.""" | |
| if not code or not code.strip(): | |
| return { | |
| "success": False, | |
| "stdout": "", | |
| "stderr": "No code provided to execute.", | |
| "exit_code": 1, | |
| "duration_ms": 0.0, | |
| } | |
| norm_mode = str(mode or "local").strip().lower() | |
| err_mode = "docker_sandbox" if norm_mode in ("docker", "docker_sandbox") else "local_subprocess" | |
| try: | |
| custom_runner = RuntimeRunner(default_mode=norm_mode, timeout_sec=timeout_seconds, workspace_env=workspace_env) | |
| res = custom_runner.run(code, mode=norm_mode) | |
| return { | |
| "success": res.success, | |
| "stdout": res.stdout[:100000], | |
| "stderr": res.stderr[:100000], | |
| "exit_code": res.exit_code, | |
| "duration_ms": res.duration_ms, | |
| "mode": res.mode, | |
| } | |
| except Exception as e: | |
| err_str = str(e) | |
| return { | |
| "success": False, | |
| "stdout": "", | |
| "stderr": err_str if "ERR-SANDBOX-UNAVAILABLE" in err_str else f"Runtime sandbox error: {err_str}", | |
| "error": err_str, | |
| "code": "ERR-SANDBOX-UNAVAILABLE" if "ERR-SANDBOX-UNAVAILABLE" in err_str else "ERR-RUNTIME", | |
| "exit_code": 1, | |
| "duration_ms": 0.0, | |
| "mode": err_mode, | |
| } | |
| def execute_workflow_logic( | |
| graph: Dict[str, Any], | |
| timeout_seconds: float = 5.0, | |
| owner_user_id: Optional[str] = None, | |
| target_node_id: Optional[str] = None, | |
| mode: str = "in_process", | |
| org_id: Optional[str] = None, | |
| role: Optional[str] = None, | |
| workspace_env: Optional[Dict[str, str]] = None, | |
| cached_outputs: Optional[Dict[str, Dict[str, Any]]] = None, | |
| pinned_inputs: Optional[Dict[str, Dict[str, Any]]] = None, | |
| single_node_only: bool = False, | |
| ) -> Dict[str, Any]: | |
| """Pure business logic for connected graph execution with multi-mode parity.""" | |
| from language_core.workflow_executor import execute_workflow_graph | |
| from language_core.workflow_migration import migrate_workflow | |
| if graph and isinstance(graph, dict): | |
| graph = migrate_workflow(graph) | |
| return execute_workflow_graph( | |
| graph, | |
| timeout_seconds=timeout_seconds, | |
| owner_user_id=owner_user_id, | |
| target_node_id=target_node_id, | |
| mode=mode, | |
| org_id=org_id, | |
| role=role, | |
| workspace_env=workspace_env, | |
| cached_outputs=cached_outputs, | |
| pinned_inputs=pinned_inputs, | |
| single_node_only=single_node_only, | |
| ) | |
| def execute_workflow_step_logic( | |
| graph: Dict[str, Any], | |
| target_node_id: Optional[str] = None, | |
| previous_outputs: Optional[Dict[str, Dict[str, Any]]] = None, | |
| previous_results: Optional[Dict[str, Dict[str, Any]]] = None, | |
| timeout_seconds: float = 5.0, | |
| run_id: Optional[str] = None, | |
| owner_user_id: Optional[str] = None, | |
| org_id: Optional[str] = None, | |
| role: Optional[str] = None, | |
| workspace_env: Optional[Dict[str, str]] = None, | |
| ) -> Dict[str, Any]: | |
| """Pure business logic for step-by-step interactive workflow execution (Step 3.4 & Step 6.4).""" | |
| from language_core.workflow_executor import execute_workflow_step | |
| from language_core.workflow_migration import migrate_workflow | |
| if graph and isinstance(graph, dict): | |
| graph = migrate_workflow(graph) | |
| return execute_workflow_step( | |
| graph=graph, | |
| target_node_id=target_node_id, | |
| previous_outputs=previous_outputs, | |
| previous_results=previous_results, | |
| timeout_seconds=timeout_seconds, | |
| run_id=run_id, | |
| owner_user_id=owner_user_id, | |
| org_id=org_id, | |
| role=role, | |
| workspace_env=workspace_env, | |
| ) | |
| def cancel_workflow_logic( | |
| run_id: Optional[str] = None, | |
| node_results: Optional[Dict[str, Dict[str, Any]]] = None, | |
| ) -> Dict[str, Any]: | |
| """Pure business logic for authoritative workflow cancellation (Step 3.5).""" | |
| from language_core.workflow_executor import cancel_workflow_run | |
| return cancel_workflow_run(run_id=run_id, node_results=node_results) | |
| def handler(event: Dict[str, Any], context: Any = None) -> Dict[str, Any]: | |
| """Standalone AWS Lambda handler entrypoint for alli-run function.""" | |
| try: | |
| body_raw = event.get("body", "{}") | |
| body = json.loads(body_raw) if isinstance(body_raw, str) else body_raw | |
| timeout = float(body.get("timeout_seconds", 5.0)) | |
| mode = body.get("mode", "local") | |
| if "graph" in body or ("nodes" in body and isinstance(body.get("nodes"), list)): | |
| graph = body.get("graph") or body | |
| graph_mode = body.get("mode") or "in_process" | |
| result = execute_workflow_logic(graph, timeout_seconds=timeout, mode=graph_mode) | |
| else: | |
| code = body.get("code", "") | |
| result = execute_code_logic(code, timeout_seconds=timeout, mode=mode) | |
| return { | |
| "statusCode": 200, | |
| "headers": { | |
| "Content-Type": "application/json", | |
| "Access-Control-Allow-Origin": "*", | |
| "Access-Control-Allow-Methods": "POST, OPTIONS", | |
| }, | |
| "body": json.dumps(result), | |
| } | |
| except Exception as err: | |
| return { | |
| "statusCode": 500, | |
| "headers": {"Content-Type": "application/json", "Access-Control-Allow-Origin": "*"}, | |
| "body": json.dumps({"error": str(err)}), | |
| } | |