|
| 1 | +""" |
| 2 | +Tests for `Run.first_failure()` and `ActionDetails.error_message`. |
| 3 | +
|
| 4 | +These are the observation half of an automated repair loop: after a run fails, an agent (or a |
| 5 | +human script) asks which step broke and why, patches the code, and reruns/forks the run. |
| 6 | +""" |
| 7 | + |
| 8 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 9 | + |
| 10 | +from flyteidl2.common import identifier_pb2, phase_pb2 |
| 11 | +from flyteidl2.workflow import run_definition_pb2 |
| 12 | + |
| 13 | +from flyte.remote._action import ActionDetails |
| 14 | +from flyte.remote._run import Run |
| 15 | + |
| 16 | +RUN_NAME = "run-abc" |
| 17 | + |
| 18 | + |
| 19 | +def _action_pb2(name: str, parent: str | None = None, task_name: str | None = None) -> run_definition_pb2.Action: |
| 20 | + action = run_definition_pb2.Action( |
| 21 | + id=identifier_pb2.ActionIdentifier( |
| 22 | + run=identifier_pb2.RunIdentifier(org="o", project="p", domain="d", name=RUN_NAME), |
| 23 | + name=name, |
| 24 | + ), |
| 25 | + status=run_definition_pb2.ActionStatus(phase=phase_pb2.ACTION_PHASE_FAILED), |
| 26 | + ) |
| 27 | + if parent: |
| 28 | + action.metadata.parent = parent |
| 29 | + if task_name: |
| 30 | + action.metadata.task.id.name = task_name |
| 31 | + return action |
| 32 | + |
| 33 | + |
| 34 | +def _details_pb2(name: str, message: str) -> run_definition_pb2.ActionDetails: |
| 35 | + return run_definition_pb2.ActionDetails( |
| 36 | + id=identifier_pb2.ActionIdentifier( |
| 37 | + run=identifier_pb2.RunIdentifier(org="o", project="p", domain="d", name=RUN_NAME), |
| 38 | + name=name, |
| 39 | + ), |
| 40 | + error_info=run_definition_pb2.ErrorInfo(message=message), |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def _run(phase=phase_pb2.ACTION_PHASE_FAILED) -> Run: |
| 45 | + return Run( |
| 46 | + run_definition_pb2.Run( |
| 47 | + action=run_definition_pb2.Action( |
| 48 | + id=identifier_pb2.ActionIdentifier( |
| 49 | + run=identifier_pb2.RunIdentifier(org="o", project="p", domain="d", name=RUN_NAME), |
| 50 | + name="a0", |
| 51 | + ), |
| 52 | + status=run_definition_pb2.ActionStatus(phase=phase), |
| 53 | + ) |
| 54 | + ) |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def _mock_client(failed_actions, details_by_name): |
| 59 | + client = MagicMock() |
| 60 | + |
| 61 | + resp = MagicMock() |
| 62 | + resp.actions = failed_actions |
| 63 | + resp.token = "" |
| 64 | + client.run_service.list_actions = AsyncMock(return_value=resp) |
| 65 | + |
| 66 | + async def get_action_details(request): |
| 67 | + details_resp = MagicMock() |
| 68 | + details_resp.details = details_by_name[request.action_id.name] |
| 69 | + return details_resp |
| 70 | + |
| 71 | + client.run_service.get_action_details = AsyncMock(side_effect=get_action_details) |
| 72 | + return client |
| 73 | + |
| 74 | + |
| 75 | +def _first_failure(run, client): |
| 76 | + cfg = MagicMock() |
| 77 | + cfg.org, cfg.project, cfg.domain = "o", "p", "d" |
| 78 | + with ( |
| 79 | + patch("flyte.remote._action.ensure_client"), |
| 80 | + patch("flyte.remote._action.get_client", return_value=client), |
| 81 | + patch("flyte.remote._action.get_init_config", return_value=cfg), |
| 82 | + ): |
| 83 | + return run.first_failure() |
| 84 | + |
| 85 | + |
| 86 | +class TestRunFirstFailure: |
| 87 | + def test_prefers_failed_sub_action_over_root(self): |
| 88 | + # The root action's error just repeats the step's — the step is the useful answer. |
| 89 | + client = _mock_client( |
| 90 | + failed_actions=[ |
| 91 | + _action_pb2("a0"), |
| 92 | + _action_pb2("clean-1", parent="a0", task_name="clean_records"), |
| 93 | + ], |
| 94 | + details_by_name={ |
| 95 | + "a0": _details_pb2("a0", "child failed"), |
| 96 | + "clean-1": _details_pb2("clean-1", "KeyError: 'price'"), |
| 97 | + }, |
| 98 | + ) |
| 99 | + failure = _first_failure(_run(), client) |
| 100 | + assert failure is not None |
| 101 | + assert failure.error_message == "KeyError: 'price'" |
| 102 | + |
| 103 | + def test_falls_back_to_root_when_only_failure(self): |
| 104 | + client = _mock_client( |
| 105 | + failed_actions=[_action_pb2("a0")], |
| 106 | + details_by_name={"a0": _details_pb2("a0", "OOMKilled")}, |
| 107 | + ) |
| 108 | + failure = _first_failure(_run(), client) |
| 109 | + assert failure is not None |
| 110 | + assert failure.error_message == "OOMKilled" |
| 111 | + |
| 112 | + def test_none_when_no_action_failed(self): |
| 113 | + client = _mock_client(failed_actions=[], details_by_name={}) |
| 114 | + assert _first_failure(_run(phase=phase_pb2.ACTION_PHASE_SUCCEEDED), client) is None |
| 115 | + |
| 116 | + |
| 117 | +class TestActionDetailsErrorMessage: |
| 118 | + def test_message_of_failed_action(self): |
| 119 | + details = ActionDetails(_details_pb2("a0", "boom")) |
| 120 | + assert details.error_message == "boom" |
| 121 | + |
| 122 | + def test_empty_when_no_error_info(self): |
| 123 | + details = ActionDetails( |
| 124 | + run_definition_pb2.ActionDetails( |
| 125 | + id=identifier_pb2.ActionIdentifier( |
| 126 | + run=identifier_pb2.RunIdentifier(org="o", project="p", domain="d", name=RUN_NAME), |
| 127 | + name="a0", |
| 128 | + ), |
| 129 | + ) |
| 130 | + ) |
| 131 | + assert details.error_message == "" |
0 commit comments