Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Log screenshot capture-deadline timeouts at warning, not error (#8553)
Aaron Perez59ea3a27b229
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex 33cadb0a7..77fb74b35 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -5796,9 +5796,14 @@ class ForgeAgent:except Exception:LOG.warning("Failed to get current x, y position of the page", exc_info=True)- screenshot = await browser_state.take_post_action_screenshot(- scrolling_number=scrolling_number,- )+ try:+ screenshot = await browser_state.take_post_action_screenshot(+ scrolling_number=scrolling_number,+ )+ except TimeoutError as e:+ # take_scrolling_screenshot raises the builtin TimeoutError when its capture deadline expires.+ # Converted here so a TimeoutError from the artifact write below still logs at error.+ raise SkyvernPageAnalysisTimeout("Timed out taking the post-action screenshot") from e# scroll back to the original x, y position of the pageif skyvern_frame and x is not None and y is not None:await skyvern_frame.safe_scroll_to_x_y(x, y)@@ -7306,7 +7311,11 @@ class ForgeAgent:browser_state = app.BROWSER_MANAGER.get_for_task(task.task_id)if browser_state is not None and await browser_state.get_working_page() is not None:try:- screenshot = await browser_state.take_fullpage_screenshot()+ try:+ screenshot = await browser_state.take_fullpage_screenshot()+ except TimeoutError as e:+ # Same capture-deadline conversion as record_artifacts_after_action; the write stays at error.+ raise SkyvernPageAnalysisTimeout("Timed out taking the final screenshot") from eawait app.ARTIFACT_MANAGER.create_artifact(step=last_step,artifact_type=ArtifactType.SCREENSHOT_FINAL,@@ -7316,6 +7325,8 @@ class ForgeAgent:LOG.warning("Failed to take screenshot before sending task response, page is closed",)+ except SkyvernPageAnalysisTimeout:+ LOG.warning("Timed out taking the final screenshot before sending task response, skipping it")except Exception:LOG.exception("Failed to take screenshot before sending task response")diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex 77086eae1..4300f4afd 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -9335,7 +9335,8 @@ async def _handle_input_text_action(if phone_bearing:LOG.warning("Phone input browser interaction failed", error_type=type(exc).__name__)return [ActionFailure(PhoneNumberInputBrowserInteractionFailed())]- LOG.exception("Failed to input the value or finish the auto completion")+ # ActionHandler.handle_action logs this re-raised exception at error when it becomes an ActionFailure.+ LOG.warning("Failed to input the value or finish the auto completion", exc_info=True)raisefinally:if tel_outcome is not None and is_tel and tel_outcome.actual_digit_count is None:diff --git a/tests/unit/test_input_text_incremental_timeout_reraise.py b/tests/unit/test_input_text_incremental_timeout_reraise.pyindex b7f22122a..b7e68f559 100644--- a/tests/unit/test_input_text_incremental_timeout_reraise.py+++ b/tests/unit/test_input_text_incremental_timeout_reraise.py@@ -16,10 +16,11 @@ from unittest.mock import AsyncMock, MagicMock, patchimport pytest+import skyvern.webeye.actions.handler as handler_modfrom skyvern.exceptions import SkyvernPageAnalysisTimeoutfrom skyvern.forge.sdk.models import StepStatusfrom skyvern.webeye.actions.actions import InputOrSelectContext, InputTextAction-from skyvern.webeye.actions.handler import handle_input_text_action+from skyvern.webeye.actions.handler import ActionHandler, handle_input_text_actionfrom skyvern.webeye.actions.responses import ActionSuccessfrom skyvern.webeye.browser_engine import BrowserEngineMetadata, BrowserEngineSelectionfrom skyvern.webeye.scraper.scraper import IncrementalScrapePage@@ -75,7 +76,7 @@ async def test_incremental_element_tree_propagates_when_both_attempts_time_out()async def _run_input_with_incremental_error(- error: BaseException, engine_selection: BrowserEngineSelection | None = None+ error: BaseException, engine_selection: BrowserEngineSelection | None = None, via_dispatcher: bool = False) -> list:skyvern_el = make_input_element_mock(element_id="AADC")dom_instance = MagicMock()@@ -110,6 +111,10 @@ async def _run_input_with_incremental_error(return_value=engine_selection,),):+ if via_dispatcher:+ return await ActionHandler._handle_action(+ scraped_page=scraped_page, task=_TASK, step=_STEP, page=MagicMock(), action=action+ )return await handle_input_text_action(action=action, page=MagicMock(), scraped_page=scraped_page, task=_TASK, step=_STEP)@@ -165,3 +170,28 @@ async def test_input_action_reraises_foreign_error_under_selected_engine(messagewith pytest.raises(PlaywrightError):await _run_input_with_incremental_error(PlaywrightError(message), engine_selection=_engine_selection())+++async def test_unexpected_input_failure_logs_one_error_through_the_dispatcher(monkeypatch: pytest.MonkeyPatch) -> None:+ """The input handler re-raises to the dispatcher, which owns the single error-level record of the failure."""+ log = MagicMock()+ monkeypatch.setattr(handler_mod, "LOG", log)+ monkeypatch.setattr(handler_mod.app.AGENT_FUNCTION, "wait_for_challenge_solver", AsyncMock())++ # Importing cloud registers an INPUT_TEXT setup hook on the global registry that would run first on the mock page.+ with (+ patch.dict(ActionHandler._setup_action_types, {}, clear=True),+ patch.dict(ActionHandler._teardown_action_types, {}, clear=True),+ ):+ results = await _run_input_with_incremental_error(+ _EngineError("something genuinely unexpected"), engine_selection=_engine_selection(), via_dispatcher=True+ )++ assert len(results) == 1 and results[0].success is False+ assert results[0].exception_type == "_EngineError"+ assert any(+ call.args[0] == "Failed to input the value or finish the auto completion" for call in log.warning.call_args_list+ )+ assert [call.args[0] for call in log.exception.call_args_list] == ["Unhandled exception in action handler"]+ log.error.assert_not_called()diff --git a/tests/unit/test_screenshot_target_closed_callers.py b/tests/unit/test_screenshot_target_closed_callers.pyindex fb1cb2490..5e5ba51e3 100644--- a/tests/unit/test_screenshot_target_closed_callers.py+++ b/tests/unit/test_screenshot_target_closed_callers.py@@ -134,6 +134,33 @@ class TestRecordArtifactsAfterActionTargetClosed:assert any("Failed to record screenshot after action" in str(call.args[0]) for call in log.error.call_args_list)+ @pytest.mark.asyncio+ async def test_capture_deadline_timeout_warns_without_error(self, monkeypatch: pytest.MonkeyPatch) -> None:+ log, kwargs = _agent_rig(monkeypatch, TimeoutError())++ await ForgeAgent.record_artifacts_after_action(MagicMock(), **kwargs)++ log.error.assert_not_called()+ log.exception.assert_not_called()+ assert any(+ "Timed out taking the post-action screenshot" in str(call.args[0]) for call in log.warning.call_args_list+ )++ @pytest.mark.asyncio+ async def test_artifact_write_timeout_still_logs_error(self, monkeypatch: pytest.MonkeyPatch) -> None:+ log, kwargs = _agent_rig(monkeypatch, TimeoutError())+ kwargs["browser_state"].take_post_action_screenshot = AsyncMock(return_value=b"png")+ agent_mod.SkyvernFrame.create_instance.return_value.safe_scroll_to_x_y = AsyncMock()+ monkeypatch.setattr(+ agent_mod.app.ARTIFACT_MANAGER,+ "accumulate_screenshot_to_step_archive",+ MagicMock(side_effect=TimeoutError()),+ )++ await ForgeAgent.record_artifacts_after_action(MagicMock(), **kwargs)++ assert any("Failed to record screenshot after action" in str(call.args[0]) for call in log.error.call_args_list)+class TestScrapeWebsiteTargetClosed:@pytest.mark.asyncio@@ -438,6 +465,30 @@ class TestCleanUpTaskTargetClosed:for call in log.exception.call_args_list)+ @pytest.mark.asyncio+ async def test_capture_deadline_timeout_warns_without_exception(self, monkeypatch: pytest.MonkeyPatch) -> None:+ log, kwargs = self._rig(monkeypatch, TimeoutError())++ await ForgeAgent().clean_up_task(**kwargs)++ log.exception.assert_not_called()+ assert any("Timed out taking the final screenshot" in str(call.args[0]) for call in log.warning.call_args_list)++ @pytest.mark.asyncio+ async def test_artifact_write_timeout_still_logs_exception(self, monkeypatch: pytest.MonkeyPatch) -> None:+ log, kwargs = self._rig(monkeypatch, TimeoutError())+ agent_mod.app.BROWSER_MANAGER.get_for_task.return_value.take_fullpage_screenshot = AsyncMock(+ return_value=b"png"+ )+ agent_mod.app.ARTIFACT_MANAGER.create_artifact = AsyncMock(side_effect=TimeoutError())++ await ForgeAgent().clean_up_task(**kwargs)++ assert any(+ "Failed to take screenshot before sending task response" in str(call.args[0])+ for call in log.exception.call_args_list+ )+class TestHandleActionTargetClosed:async def _run(self, monkeypatch: pytest.MonkeyPatch, error: Exception) -> tuple[MagicMock, list]: