Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Register a session browser before its first goto so later blocks see it (SKY-18860) (#9020)
Aaron Perez58021413fc0b
diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex 56889186c..ade78a30d 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -1886,6 +1886,7 @@ class Block(BaseModel, abc.ABC):)return resultexcept Exception as e:+ can_continue_after_failure: bool | None = Noneif isinstance(e, (MissingBrowserState, MissingBrowserStatePage)):LOG.exception("Block execution failed because browser state is unavailable",@@ -1894,6 +1895,11 @@ class Block(BaseModel, abc.ABC):block_type=self.block_type,)failure_reason = get_user_facing_exception_message(e)+ if isinstance(e, MissingBrowserState):+ # Nothing hands a later block a browser once the run has lost its own, so+ # continuing only replays this failure for every remaining block or loop+ # iteration. A missing page stays continuable: a later block can open one.+ can_continue_after_failure = Falseelif isinstance(e, FailedToNavigateToUrl) and not isinstance(e, BlockedNavigationDestination):# The target site did not load. Site-caused, and already surfaced on the run through# failure_reason below. BlockedNavigationDestination is excluded on purpose: it is the@@ -1955,6 +1961,7 @@ class Block(BaseModel, abc.ABC):workflow_run_block_id=workflow_run_block_id,organization_id=organization_id,error_codes=error_codes or None,+ can_continue_after_failure=can_continue_after_failure,)finally:if download_timeout_context is not None:diff --git a/skyvern/webeye/real_browser_manager.py b/skyvern/webeye/real_browser_manager.pyindex 7419854bb..fa0053ac2 100644--- a/skyvern/webeye/real_browser_manager.py+++ b/skyvern/webeye/real_browser_manager.py@@ -1444,6 +1444,11 @@ class RealBrowserManager(BrowserManager):"Browser state not found in persistent sessions manager", browser_session_id=browser_session_id)else:+ # Register before the first goto, as the creation path below does: a navigation+ # failure here raises past the registry write at the end of this method, and every+ # later block of the run would then find an empty registry and report a missing+ # browser instead of the navigation failure.+ self.pages[workflow_run_id] = browser_statepage = await browser_state.get_working_page()if page:if url and navigate:@@ -1470,6 +1475,10 @@ class RealBrowserManager(BrowserManager):organization_id=workflow_run.organization_id,expected=browser_state,)+ # The evicted state's CDP is dead, so it must not stay registered for+ # the run once the reconnect below replaces or fails to replace it.+ if self.pages.get(workflow_run_id) is browser_state:+ self.pages.pop(workflow_run_id, None)browser_state = await app.PERSISTENT_SESSIONS_MANAGER.get_browser_state(browser_session_id,organization_id=workflow_run.organization_id,@@ -1484,6 +1493,7 @@ class RealBrowserManager(BrowserManager):)if browser_state is None:raise+ self.pages[workflow_run_id] = browser_state_retain_run_dialog_answers(browser_state,workflow_run_id,diff --git a/tests/unit/test_real_browser_manager.py b/tests/unit/test_real_browser_manager.pyindex f725f837f..43aec0b05 100644--- a/tests/unit/test_real_browser_manager.py+++ b/tests/unit/test_real_browser_manager.py@@ -18,7 +18,11 @@ from unittest.mock import AsyncMock, MagicMock, call, patchimport pytestfrom structlog.testing import capture_logs-from skyvern.exceptions import BrowserSessionAlreadyOccupiedError, MissingBrowserStateForBrowserSession+from skyvern.exceptions import (+ BrowserSessionAlreadyOccupiedError,+ FailedToNavigateToUrl,+ MissingBrowserStateForBrowserSession,+)from skyvern.forge import app as forge_appfrom skyvern.forge.sdk.artifact.storage.recording_test_helpers import fake_prepared_recordingfrom skyvern.forge.sdk.core import skyvern_context@@ -664,6 +668,48 @@ async def test_pbs_workflow_run_without_lease_still_asserts_run_as_expected_owneassert call_kwargs["expected_runnable_id"] == "wfr_child"[email protected]("browser_survives_the_goto", [True, False])+async def test_pbs_registry_after_a_failed_first_goto_holds_only_a_usable_browser(+ browser_survives_the_goto: bool,+) -> None:+ """SKY-18860: later blocks of the run find their browser through the registry. A goto that fails+ on the site or the proxy leaves the session's browser alive, so it must stay registered —+ otherwise every later block reports a missing browser instead of the navigation failure. A goto+ that fails because the cached CDP dropped leaves nothing usable, so the evicted state must go."""+ manager = RealBrowserManager()+ workflow_run = make_workflow_run("wfr_child")++ error_message = (+ "Page.goto: net::ERR_TUNNEL_CONNECTION_FAILED"+ if browser_survives_the_goto+ else "Connection closed while reading from the driver"+ )+ pbs_state = MagicMock()+ pbs_state.get_working_page = AsyncMock(return_value=MagicMock())+ pbs_state.navigate_to_url = AsyncMock(+ side_effect=FailedToNavigateToUrl(url="https://example.com", error_message=error_message)+ )++ with patch_browser_manager_app() as mock_app:+ configure_browser_context_acquired_hook(mock_app)+ # The reconnect after an evict finds the session gone, so the run keeps no browser at all.+ mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(side_effect=[pbs_state, None])+ mock_app.PERSISTENT_SESSIONS_MANAGER.set_browser_state = AsyncMock()+ mock_app.PERSISTENT_SESSIONS_MANAGER.evict_cached_browser_state = AsyncMock()+ mock_app.PERSISTENT_SESSIONS_MANAGER.supports_evict_and_reconnect = MagicMock(return_value=True)++ with pytest.raises(FailedToNavigateToUrl):+ await manager.get_or_create_for_workflow_run(+ workflow_run=workflow_run,+ url="https://example.com",+ browser_session_id="bs_123",+ )++ expected = pbs_state if browser_survives_the_goto else None+ assert manager.get_for_workflow_run(workflow_run_id="wfr_child") is expected++@pytest.mark.asyncioasync def test_non_pbs_workflow_run_cache_hit_on_second_call() -> None:"""Non-PBS runs must also hit the early cache check on subsequent calls."""diff --git a/tests/unit/workflow/test_execute_single_block_phases.py b/tests/unit/workflow/test_execute_single_block_phases.pyindex faaeed8c5..cb3fbcc74 100644--- a/tests/unit/workflow/test_execute_single_block_phases.py+++ b/tests/unit/workflow/test_execute_single_block_phases.py@@ -19,7 +19,7 @@ from unittest.mock import AsyncMock, MagicMock, patchimport pytestfrom structlog.testing import capture_logs-from skyvern.exceptions import StepTerminationError+from skyvern.exceptions import MissingBrowserState, MissingBrowserStatePage, StepTerminationErrorfrom skyvern.forge import appfrom skyvern.forge.sdk.db.enums import BrowserSeedSourcefrom skyvern.forge.sdk.workflow.context_manager import BlockOutcome, WorkflowRunContext@@ -664,6 +664,37 @@ async def test_ordinary_code_failure_still_honors_continue_on_failure(monkeypatcmark_failed.assert_not_awaited()[email protected]("the_run_lost_its_browser", [True, False])+async def test_a_lost_browser_stops_the_run_despite_continue_on_failure(+ monkeypatch: pytest.MonkeyPatch,+ the_run_lost_its_browser: bool,+) -> None:+ """SKY-18860: nothing hands a later block a browser once the run has lost its own, so every+ remaining block would fail the same way — that failure must not be continuable. A missing page+ is a different story: a later block can open one, so it keeps honoring continue_on_failure."""+ service = WorkflowService()+ block = _navigation_block("nav")+ block.continue_on_failure = True+ error = (+ MissingBrowserState(workflow_run_id="wr_test", failure_reason="browser_state_registry_lookup_miss")+ if the_run_lost_its_browser+ else MissingBrowserStatePage(workflow_run_id="wr_test")+ )+ _wire_real_execute_safe(monkeypatch, [WorkflowRunStatus.running], execute=AsyncMock(side_effect=error))+ failed_run = MagicMock()+ mark_failed = AsyncMock(return_value=failed_run)+ monkeypatch.setattr(WorkflowService, "mark_workflow_run_as_failed_if_not_final", mark_failed)++ workflow_run, _, block_result, should_stop, _ = await _run_single_block(service, block)++ assert block_result is not None+ assert block_result.success is False+ assert block_result.can_continue_after_failure is not the_run_lost_its_browser+ assert should_stop is the_run_lost_its_browser+ assert (workflow_run is failed_run) is the_run_lost_its_browser++@pytest.mark.asyncioasync def test_block_exception_marks_run_failed_with_block_type_reason(monkeypatch: pytest.MonkeyPatch) -> None:service = WorkflowService()