Merge branch 'dev'
Browse filesFiles changed (2)
139 unmodified lines140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185139 unmodified lines}local_scope: Dict[str, Any] = dict(node_inputs)local_scope["inputs"] = dict(node_inputs)try:compiled = compile(raw_code, f"<custom_script_{node_id}>", "exec")exec(compiled, safe_globals, local_scope)except Exception as exc:raise WorkflowExecutionError(node_id=node_id,node_type="code.python",message=f"[ERR-SCRIPT-RUNTIME] Custom Python script on '{label}' failed at runtime: {exc}",original_exception=exc,)# Check for entrypoint functions: run(...) or main(...)entrypoint_name = params.get("entrypoint")candidate_fns = [entrypoint_name] if entrypoint_name else ["run", "main"]fn = next((local_scope[name] for name in candidate_fns if callable(local_scope.get(name))), None)if fn is not None:try:sig = inspect.signature(fn)matched_kwargs = {k: v for k, v in node_inputs.items() if k in sig.parameters}if not matched_kwargs and len(sig.parameters) == 1:p_name = next(iter(sig.parameters.keys()))first_val = next(iter(node_inputs.values()), None)res = fn(**{p_name: first_val})else:res = fn(**matched_kwargs)except Exception as exc:raise WorkflowExecutionError(node_id=node_id,node_type="code.python",message=f"[ERR-ENTRYPOINT-EXECUTION] Error invoking entrypoint on '{label}': {exc}",original_exception=exc,)elif "result" in local_scope:res = local_scope["result"]elif "out" in local_scope:res = local_scope["out"]else:res = next((v for k, v in local_scope.items() if k not in node_inputs and k != "inputs" and not k.startswith("_")), None)if isinstance(res, dict):out_dict = dict(res)139 unmodified lines140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207139 unmodified lines}local_scope: Dict[str, Any] = dict(node_inputs)local_scope["inputs"] = dict(node_inputs)timeout_sec = float(params.get("timeout_seconds") or 5.0)def _run_in_sandbox() -> Any:try:compiled = compile(raw_code, f"<custom_script_{node_id}>", "exec")exec(compiled, safe_globals, local_scope)except Exception as exc:raise WorkflowExecutionError(node_id=node_id,node_type="code.python",message=f"[ERR-SCRIPT-RUNTIME] Custom Python script on '{label}' failed at runtime: {exc}",original_exception=exc,)entrypoint_name = params.get("entrypoint")candidate_fns = [entrypoint_name] if entrypoint_name else ["run", "main"]fn = next((local_scope[name] for name in candidate_fns if callable(local_scope.get(name))), None)if fn is not None:try:sig = inspect.signature(fn)matched_kwargs = {k: v for k, v in node_inputs.items() if k in sig.parameters}if not matched_kwargs and len(sig.parameters) == 1:p_name = next(iter(sig.parameters.keys()))first_val = next(iter(node_inputs.values()), None)return fn(**{p_name: first_val})return fn(**matched_kwargs)except Exception as exc:raise WorkflowExecutionError(node_id=node_id,node_type="code.python",message=f"[ERR-ENTRYPOINT-EXECUTION] Error invoking entrypoint on '{label}': {exc}",original_exception=exc,)if "result" in local_scope:return local_scope["result"]if "out" in local_scope:return local_scope["out"]return next((v for k, v in local_scope.items() if k not in node_inputs and k != "inputs" and not k.startswith("_")), None)import threadingres_box: Dict[str, Any] = {}err_box: List[Exception] = []def _worker():try:res_box["val"] = _run_in_sandbox()except Exception as exc:err_box.append(exc)t = threading.Thread(target=_worker, daemon=True)t.start()t.join(timeout=timeout_sec)if t.is_alive():raise WorkflowExecutionError(node_id=node_id,node_type="code.python",message=f"[ERR-TIMEOUT] Custom Python script on '{label}' exceeded {timeout_sec}s safety execution quota.",)if err_box:raise err_box[0]res = res_box.get("val")if isinstance(res, dict):out_dict = dict(res)
79 unmodified lines80818283848579 unmodified lines)self.assertIn("[ERR-SYNTAX]", str(ctx.exception))if __name__ == "__main__":unittest.main()79 unmodified lines80818283848586878889909192939479 unmodified lines)self.assertIn("[ERR-SYNTAX]", str(ctx.exception))def test_timeout_enforcement(self):with self.assertRaises(WorkflowExecutionError) as ctx:execute_custom_script_node(node_inputs={},params={"code": "while True: pass", "timeout_seconds": 0.1},node_id="node_timeout",)self.assertIn("[ERR-TIMEOUT]", str(ctx.exception))if __name__ == "__main__":unittest.main()
language_core/executor/custom_script_executor.py+54-32
tests/test_custom_script_executor.py+9