Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Pass persisted failure reasons to user-defined error detection (#8526)
LawyZheng1aeb4e1f190d
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex 238c9d796..eebc74857 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -3583,7 +3583,7 @@ class ForgeAgent:failure_category_source="code_level",failure_category_path="exception",)- await self.update_task(+ updated_task = await self.update_task(task,status=TaskStatus.failed,failure_reason=reason,@@ -3604,7 +3604,7 @@ class ForgeAgent:task=task,step=step,browser_state=browser_state,- failure_reason=reason,+ failure_reason=updated_task.failure_reason if reason is not None else None,)# Update task errors if any were detecteddiff --git a/tests/unit/test_fail_task_error_detection.py b/tests/unit/test_fail_task_error_detection.pyindex 34ea8fd17..13b31db48 100644--- a/tests/unit/test_fail_task_error_detection.py+++ b/tests/unit/test_fail_task_error_detection.py@@ -13,9 +13,39 @@ from skyvern.errors.errors import UserDefinedErrorfrom skyvern.forge.agent import ForgeAgentfrom skyvern.forge.sdk.models import StepStatusfrom skyvern.forge.sdk.schemas.tasks import TaskStatus+from skyvern.schemas.steps import AgentStepOutputfrom skyvern.utils.secret_redaction import REDACTED_SECRET_PLACEHOLDER+from skyvern.webeye.actions.actions import Action, ActionType+from skyvern.webeye.actions.responses import ActionSuccessfrom tests.unit.helpers import make_organization, make_step, make_task+_EXPECTED_500 = "An HTTP request made during the download action returned HTTP 500, and no file was received."+++def _download_intent_action() -> Action:+ return Action(action_type=ActionType.CLICK, element_id="download-link", download=True)+++def _failed_download_result(status: int) -> ActionSuccess:+ result = ActionSuccess()+ result.download_triggered = False+ result.download_failure_status = status+ return result+++def _step_output(pairs: list) -> AgentStepOutput:+ return AgentStepOutput(action_results=[], actions_and_results=pairs)+++def _echo_persisted_reason(base_task):+ """Mirror real update_task: return the persisted row so the caller reads back the failure_reason that+ was actually stored (post enrichment/redaction), not the stale local string it passed in."""++ def _update_task(task, status, failure_reason=None, **kwargs):+ return base_task.model_copy(update={"status": status, "failure_reason": failure_reason})++ return _update_task+@pytest.fixturedef agent():@@ -64,7 +94,7 @@ async def test_fail_task_with_error_code_mapping_detects_errors(agent, mock_browwith patch.object(agent, "update_step", new_callable=AsyncMock):with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:- mock_update_task.return_value = task+ mock_update_task.side_effect = _echo_persisted_reason(task)with patch("skyvern.forge.agent.detect_user_defined_errors_for_task",@@ -106,7 +136,7 @@ async def test_fail_task_without_error_code_mapping(agent, mock_browser_state):with patch.object(agent, "update_step", new_callable=AsyncMock):with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:- mock_update_task.return_value = task+ mock_update_task.side_effect = _echo_persisted_reason(task)with patch("skyvern.forge.agent.detect_user_defined_errors_for_task",@@ -142,7 +172,7 @@ async def test_fail_task_without_browser_state(agent):with patch.object(agent, "update_step", new_callable=AsyncMock):with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:- mock_update_task.return_value = task+ mock_update_task.side_effect = _echo_persisted_reason(task)with patch("skyvern.forge.agent.detect_user_defined_errors_for_task",@@ -182,7 +212,7 @@ async def test_fail_task_without_step(agent, mock_browser_state):with patch.object(agent, "update_step", new_callable=AsyncMock) as mock_update_step:with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:- mock_update_task.return_value = task+ mock_update_task.side_effect = _echo_persisted_reason(task)with patch("skyvern.forge.agent.detect_user_defined_errors_for_task",@@ -212,7 +242,7 @@ async def test_fail_task_preserves_completed_step_and_fails_task(agent, mock_browith patch.object(agent, "update_step", new_callable=AsyncMock) as mock_update_step:with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:- mock_update_task.return_value = task+ mock_update_task.side_effect = _echo_persisted_reason(task)result = await agent.fail_task(task, step, "Post-step billing failed", mock_browser_state)@@ -238,7 +268,7 @@ async def test_fail_task_error_detection_fails_gracefully(agent, mock_browser_stwith patch.object(agent, "update_step", new_callable=AsyncMock):with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:- mock_update_task.return_value = task+ mock_update_task.side_effect = _echo_persisted_reason(task)with patch("skyvern.forge.agent.detect_user_defined_errors_for_task",@@ -281,7 +311,7 @@ async def test_fail_task_multiple_errors_detected(agent, mock_browser_state):with patch.object(agent, "update_step", new_callable=AsyncMock):with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:- mock_update_task.return_value = task+ mock_update_task.side_effect = _echo_persisted_reason(task)with patch("skyvern.forge.agent.detect_user_defined_errors_for_task",@@ -319,7 +349,7 @@ async def test_fail_task_no_errors_detected(agent, mock_browser_state):with patch.object(agent, "update_step", new_callable=AsyncMock):with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:- mock_update_task.return_value = task+ mock_update_task.side_effect = _echo_persisted_reason(task)with patch("skyvern.forge.agent.detect_user_defined_errors_for_task",@@ -409,7 +439,7 @@ async def test_fail_task_redacts_a_registered_secret_from_the_failure_reason(agewith patch.object(agent, "update_step", new_callable=AsyncMock):with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:- mock_update_task.return_value = task+ mock_update_task.side_effect = _echo_persisted_reason(task)with patch("skyvern.forge.agent.detect_user_defined_errors_for_task", new_callable=AsyncMock) as mock_detect:@@ -440,7 +470,7 @@ async def test_fail_task_redacts_a_registered_secret_from_detector_written_errorwith patch.object(agent, "update_step", new_callable=AsyncMock):with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:- mock_update_task.return_value = task+ mock_update_task.side_effect = _echo_persisted_reason(task)with patch("skyvern.forge.agent.detect_user_defined_errors_for_task", new_callable=AsyncMock) as mock_detect:@@ -476,7 +506,7 @@ async def test_fail_task_leaves_the_failure_reason_alone_when_no_secrets_are_regwith patch.object(agent, "update_step", new_callable=AsyncMock):with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:- mock_update_task.return_value = task+ mock_update_task.side_effect = _echo_persisted_reason(task)with patch("skyvern.forge.agent.detect_user_defined_errors_for_task", new_callable=AsyncMock) as mock_detect:@@ -490,3 +520,91 @@ async def test_fail_task_leaves_the_failure_reason_alone_when_no_secrets_are_regawait agent.fail_task(task, step, raw, mock_browser_state)assert mock_update_task.await_args.kwargs["failure_reason"] == raw+++async def test_detector_receives_the_enriched_failure_reason_persisted_by_update_task(agent, mock_browser_state):+ """The download 5xx enrichment is stamped inside update_task at the persistence seam, so the string+ fail_task computed locally is stale by the time the row is written. The detector must be handed the+ post-update persisted reason -- the one carrying the HTTP 5xx/no-file sentence -- so its judgment sees+ the same evidence that ships to the customer. Drives real fail_task -> real update_task -> real+ _enrich_failure_reason_with_download_status, mocking only DB/browser/LLM/telemetry boundaries."""+ now = datetime.now()+ organization = make_organization(now)+ task = make_task(+ now,+ organization,+ task_id="task-dl",+ workflow_run_id="wr-dl",+ error_code_mapping={"download_failed": "The document could not be downloaded"},+ )+ step = make_step(+ now,+ task,+ step_id="step-1",+ status=StepStatus.completed,+ order=0,+ output=_step_output([(_download_intent_action(), [_failed_download_result(500)])]),+ )+ reason = "The task failed while attempting to retrieve the document."++ def _claim(task_id, organization_id, **updates):+ persisted = task.model_copy(+ update={"status": updates.get("status"), "failure_reason": updates.get("failure_reason")}+ )+ return persisted, False++ claim = AsyncMock(side_effect=_claim)++ with patch.object(agent, "update_step", new_callable=AsyncMock):+ with patch("skyvern.forge.agent.detect_user_defined_errors_for_task", new_callable=AsyncMock) as mock_detect:+ mock_detect.return_value = []+ with (+ patch("skyvern.forge.agent.app") as mock_app,+ patch("skyvern.forge.agent.save_task_logs", new_callable=AsyncMock),+ ):+ mock_app.WORKFLOW_CONTEXT_MANAGER.artifact_redaction_enabled.return_value = False+ mock_app.WORKFLOW_CONTEXT_MANAGER.runtime_secret_values_for_artifacts.return_value = set()+ mock_app.DATABASE.tasks.get_task = AsyncMock(return_value=task)+ mock_app.DATABASE.tasks.get_task_steps = AsyncMock(return_value=[step])+ mock_app.DATABASE.tasks.update_task_and_claim_finish = claim+ mock_app.DATABASE.tasks.update_task = AsyncMock()+ mock_app.DATABASE.observer.get_workflow_run_block_engine_by_task_id = AsyncMock(return_value=None)+ mock_app.AGENT_FUNCTION.record_run_duration = AsyncMock()+ mock_app.AGENT_FUNCTION.on_task_completed = AsyncMock()++ result = await agent.fail_task(task, step, reason, mock_browser_state)++ assert result is True+ persisted_reason = claim.await_args.kwargs["failure_reason"]+ assert persisted_reason == f"{reason} {_EXPECTED_500}"+ detector_reason = mock_detect.await_args.kwargs["failure_reason"]+ assert _EXPECTED_500 in detector_reason+ assert detector_reason == persisted_reason+++async def test_reason_none_stays_none_even_when_updated_task_carries_a_stale_reason(agent, mock_browser_state):+ """When the caller's reason is None the detector must receive None, even though update_task returns a+ persisted Task whose failure_reason column still holds an older value. fail_task keys the detector+ input off the original reason, not the returned row -- so a None reason never picks up a stale string."""+ now = datetime.now()+ organization = make_organization(now)+ task = make_task(now, organization, error_code_mapping={"download_failed": "The document could not be downloaded"})+ step = make_step(now, task, step_id="step-1", status=StepStatus.running, order=0, output=None)+ stale = make_task(now, organization, failure_reason="a stale failure reason from a previous write")++ with patch.object(agent, "update_step", new_callable=AsyncMock):+ with patch.object(agent, "update_task", new_callable=AsyncMock) as mock_update_task:+ mock_update_task.return_value = stale+ with patch(+ "skyvern.forge.agent.detect_user_defined_errors_for_task", new_callable=AsyncMock+ ) as mock_detect:+ mock_detect.return_value = []+ with patch("skyvern.forge.agent.app") as mock_app:+ mock_app.DATABASE.tasks.update_task = AsyncMock()++ result = await agent.fail_task(task, step, None, mock_browser_state)++ assert result is True+ assert mock_detect.await_args.kwargs["failure_reason"] is None