|
| 1 | +"""Tests for classifying eval execution errors as user vs runtime failures.""" |
| 2 | + |
| 3 | +import uuid |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any, AsyncGenerator |
| 6 | + |
| 7 | +from uipath.core.events import EventBus |
| 8 | +from uipath.core.tracing import UiPathTraceManager |
| 9 | +from uipath.eval.helpers import EvalHelpers |
| 10 | +from uipath.eval.runtime import UiPathEvalContext, evaluate |
| 11 | +from uipath.eval.runtime.events import EvalRunUpdatedEvent, EvaluationEvents |
| 12 | +from uipath.eval.runtime.runtime import _is_user_facing_error |
| 13 | +from uipath.runtime import ( |
| 14 | + UiPathExecuteOptions, |
| 15 | + UiPathRuntimeEvent, |
| 16 | + UiPathRuntimeFactorySettings, |
| 17 | + UiPathRuntimeProtocol, |
| 18 | + UiPathRuntimeResult, |
| 19 | + UiPathRuntimeStorageProtocol, |
| 20 | + UiPathStreamOptions, |
| 21 | +) |
| 22 | +from uipath.runtime.errors import ( |
| 23 | + UiPathErrorCategory, |
| 24 | + UiPathErrorCode, |
| 25 | + UiPathRuntimeError, |
| 26 | +) |
| 27 | +from uipath.runtime.schema import UiPathRuntimeSchema |
| 28 | + |
| 29 | + |
| 30 | +def _make_error(category: UiPathErrorCategory) -> UiPathRuntimeError: |
| 31 | + return UiPathRuntimeError( |
| 32 | + UiPathErrorCode.FUNCTION_EXECUTION_ERROR, |
| 33 | + "Some failure", |
| 34 | + "details", |
| 35 | + category, |
| 36 | + include_traceback=False, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def test_user_category_error_is_user_facing(): |
| 41 | + assert _is_user_facing_error(_make_error(UiPathErrorCategory.USER)) is True |
| 42 | + |
| 43 | + |
| 44 | +def test_system_category_error_is_not_user_facing(): |
| 45 | + assert _is_user_facing_error(_make_error(UiPathErrorCategory.SYSTEM)) is False |
| 46 | + |
| 47 | + |
| 48 | +def test_unknown_category_error_is_not_user_facing(): |
| 49 | + assert _is_user_facing_error(_make_error(UiPathErrorCategory.UNKNOWN)) is False |
| 50 | + |
| 51 | + |
| 52 | +def test_plain_exception_is_not_user_facing(): |
| 53 | + assert _is_user_facing_error(ValueError("boom")) is False |
| 54 | + |
| 55 | + |
| 56 | +class _FailingRuntime: |
| 57 | + """Runtime whose execution always raises the configured error.""" |
| 58 | + |
| 59 | + def __init__(self, error: Exception): |
| 60 | + self._error = error |
| 61 | + |
| 62 | + async def execute( |
| 63 | + self, |
| 64 | + input: dict[str, Any] | None = None, |
| 65 | + options: UiPathExecuteOptions | None = None, |
| 66 | + ) -> UiPathRuntimeResult: |
| 67 | + raise self._error |
| 68 | + |
| 69 | + async def stream( |
| 70 | + self, |
| 71 | + input: dict[str, Any] | None = None, |
| 72 | + options: UiPathStreamOptions | None = None, |
| 73 | + ) -> AsyncGenerator[UiPathRuntimeEvent, None]: |
| 74 | + raise self._error |
| 75 | + yield # unreachable; makes this an async generator |
| 76 | + |
| 77 | + async def get_schema(self) -> UiPathRuntimeSchema: |
| 78 | + return UiPathRuntimeSchema( |
| 79 | + filePath="test.py", |
| 80 | + uniqueId="test", |
| 81 | + type="workflow", |
| 82 | + input={"type": "object", "properties": {}}, |
| 83 | + output={"type": "object", "properties": {}}, |
| 84 | + ) |
| 85 | + |
| 86 | + async def dispose(self) -> None: |
| 87 | + pass |
| 88 | + |
| 89 | + |
| 90 | +class _FailingFactory: |
| 91 | + def __init__(self, error: Exception): |
| 92 | + self._error = error |
| 93 | + |
| 94 | + def discover_entrypoints(self) -> list[str]: |
| 95 | + return ["test"] |
| 96 | + |
| 97 | + async def get_storage(self) -> UiPathRuntimeStorageProtocol | None: |
| 98 | + return None |
| 99 | + |
| 100 | + async def get_settings(self) -> UiPathRuntimeFactorySettings | None: |
| 101 | + return None |
| 102 | + |
| 103 | + async def new_runtime( |
| 104 | + self, entrypoint: str, runtime_id: str, **kwargs |
| 105 | + ) -> UiPathRuntimeProtocol: |
| 106 | + return _FailingRuntime(self._error) |
| 107 | + |
| 108 | + async def dispose(self) -> None: |
| 109 | + pass |
| 110 | + |
| 111 | + |
| 112 | +async def _run_failing_eval(error: Exception) -> list[EvalRunUpdatedEvent]: |
| 113 | + """Run an eval whose execution raises, returning captured run-updated events.""" |
| 114 | + event_bus = EventBus() |
| 115 | + trace_manager = UiPathTraceManager() |
| 116 | + captured: list[EvalRunUpdatedEvent] = [] |
| 117 | + |
| 118 | + async def capture(event: EvalRunUpdatedEvent) -> None: |
| 119 | + captured.append(event) |
| 120 | + |
| 121 | + event_bus.subscribe(EvaluationEvents.UPDATE_EVAL_RUN, capture) |
| 122 | + |
| 123 | + factory = _FailingFactory(error) |
| 124 | + eval_set_path = str(Path(__file__).parent / "evals" / "eval-sets" / "default.json") |
| 125 | + evaluation_set, _ = EvalHelpers.load_eval_set(eval_set_path) |
| 126 | + runtime = await factory.new_runtime("test", "test-runtime-id") |
| 127 | + runtime_schema = await runtime.get_schema() |
| 128 | + evaluators = await EvalHelpers.load_evaluators( |
| 129 | + eval_set_path, evaluation_set, agent_model=None |
| 130 | + ) |
| 131 | + |
| 132 | + context = UiPathEvalContext() |
| 133 | + context.execution_id = str(uuid.uuid4()) |
| 134 | + context.evaluation_set = evaluation_set |
| 135 | + context.runtime_schema = runtime_schema |
| 136 | + context.evaluators = evaluators |
| 137 | + |
| 138 | + await evaluate(factory, trace_manager, context, event_bus) |
| 139 | + await event_bus.wait_for_all() |
| 140 | + return captured |
| 141 | + |
| 142 | + |
| 143 | +async def test_user_error_execution_is_not_a_runtime_exception(): |
| 144 | + """A USER-category failure is reported unwrapped with runtime_exception=False.""" |
| 145 | + error = UiPathRuntimeError( |
| 146 | + UiPathErrorCode.INPUT_INVALID_JSON, |
| 147 | + "Invalid input", |
| 148 | + "Input does not match the expected schema", |
| 149 | + UiPathErrorCategory.USER, |
| 150 | + include_traceback=False, |
| 151 | + ) |
| 152 | + events = await _run_failing_eval(error) |
| 153 | + |
| 154 | + failed = [e for e in events if not e.success] |
| 155 | + assert failed |
| 156 | + details = failed[0].exception_details |
| 157 | + assert details is not None |
| 158 | + assert details.exception is error |
| 159 | + assert details.runtime_exception is False |
| 160 | + |
| 161 | + |
| 162 | +async def test_unexpected_error_execution_is_a_runtime_exception(): |
| 163 | + """A non-user failure in the execution path is flagged as a runtime exception.""" |
| 164 | + events = await _run_failing_eval(RuntimeError("infrastructure broke")) |
| 165 | + |
| 166 | + failed = [e for e in events if not e.success] |
| 167 | + assert failed |
| 168 | + details = failed[0].exception_details |
| 169 | + assert details is not None |
| 170 | + assert isinstance(details.exception, RuntimeError) |
| 171 | + assert details.runtime_exception is True |
0 commit comments