Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat(browser): cloud per-run engine resolver + fail-closed engines (SKY-12007) [5/5] (#7597)
LawyZheng2700c2991359
diff --git a/skyvern/webeye/browser_engine.py b/skyvern/webeye/browser_engine.pyindex 4f300e0d4..83a051f1c 100644--- a/skyvern/webeye/browser_engine.py+++ b/skyvern/webeye/browser_engine.py@@ -255,6 +255,10 @@ class BrowserEngineContext:workflow_permanent_id: str | None = Nonetask_id: str | None = Nonescript_id: str | None = None+ # The canonical key this run's selection is pinned/owned under (set by get_or_resolve_engine_selection).+ # A resolver keys the flag on this so every resource in one run resolves identically even when a caller+ # deliberately leaves workflow_run_id unset here (task-first creation keeps its download-dir scoping).+ run_key: str | None = Nonebrowser_source: str | None = Nonediff --git a/skyvern/webeye/real_browser_manager.py b/skyvern/webeye/real_browser_manager.pyindex 5dd2fd0ea..863a63e06 100644--- a/skyvern/webeye/real_browser_manager.py+++ b/skyvern/webeye/real_browser_manager.py@@ -3,6 +3,7 @@ from __future__ import annotationsimport asyncioimport functoolsimport os+from dataclasses import replaceimport structlog@@ -255,6 +256,11 @@ class RealBrowserManager(BrowserManager):re-resolves cleanly — no orphan owner accumulates on failed run keys."""if run_key is None:return await resolve_browser_engine(context)+ # Resolve the flag under the SAME key the owner is pinned by, so a run selects one engine no+ # matter which resource creates the browser first — a caller may pin under workflow_run_id while+ # deliberately leaving it out of the context (task-first creation keeps its download-dir scoping).+ if context.run_key != run_key:+ context = replace(context, run_key=run_key)while True:owner = self._engine_owners.get(run_key)if owner is None:@@ -337,13 +343,18 @@ class RealBrowserManager(BrowserManager):browser_address: str | None = None,browser_profile_id: str | None = None,engine_run_key: str | None = None,+ engine_workflow_run_id: str | None = None,) -> BrowserState:engine_selection = await self.get_or_resolve_engine_selection(run_key=engine_run_keyor canonical_run_key(workflow_run_id=workflow_run_id, task_id=task_id, script_id=script_id),context=BrowserEngineContext(organization_id=organization_id,- workflow_run_id=workflow_run_id,+ # Engine-flag identity only: a caller that pins under workflow_run_id while keeping it out+ # of browser-context creation (task-first, for download-dir scoping) passes it via+ # engine_workflow_run_id, so the flag's distinct_id AND its workflow_run_id property both+ # match the pinned run. The browser context below still uses the raw workflow_run_id.+ workflow_run_id=engine_workflow_run_id or workflow_run_id,workflow_permanent_id=workflow_permanent_id,task_id=task_id,script_id=script_id,@@ -471,10 +482,11 @@ class RealBrowserManager(BrowserManager):url=task.url,task_id=task.task_id,# Pin the engine under the workflow_run_id for a workflow-owned task so it shares one- # selection owner with the workflow path (not a second task_id owner). Passed only as- # the engine key — workflow_run_id is deliberately kept out of browser-context creation- # here to preserve the task path's existing download-dir / artifact behavior.+ # selection owner (and one flag distinct_id/property) with the workflow path. Both go to+ # engine-flag resolution only — workflow_run_id is still kept out of browser-context+ # creation here to preserve the task path's existing download-dir / artifact behavior.engine_run_key=canonical_run_key(workflow_run_id=task.workflow_run_id, task_id=task.task_id),+ engine_workflow_run_id=task.workflow_run_id,workflow_permanent_id=task.workflow_permanent_id,organization_id=task.organization_id,extra_http_headers=extra_http_headers,diff --git a/tests/unit/test_real_browser_manager.py b/tests/unit/test_real_browser_manager.pyindex 77d5d4c2b..5ee342b11 100644--- a/tests/unit/test_real_browser_manager.py+++ b/tests/unit/test_real_browser_manager.py@@ -33,6 +33,38 @@ def make_workflow_run(return wfr+class _StopBeforeBrowserContext(Exception):+ pass+++async def test_task_first_creation_gives_engine_flag_the_pinned_workflow_id() -> None:+ """A workflow-owned task that creates the browser first pins under workflow_run_id but keeps it+ out of browser-context creation (download-dir scoping). The engine-flag context must still carry+ that workflow_run_id — so both the flag distinct_id and its workflow_run_id property match the+ pinned run — while the browser context keeps the raw (None) workflow_run_id."""+ manager = RealBrowserManager()+ seen: dict[str, object] = {}++ async def capture(*, run_key: str | None, context: object) -> object:+ seen["run_key"] = run_key+ seen["context"] = context+ raise _StopBeforeBrowserContext++ with patch.object(manager, "get_or_resolve_engine_selection", side_effect=capture):+ with pytest.raises(_StopBeforeBrowserContext):+ await manager._create_browser_state(+ task_id="tsk_1",+ workflow_run_id=None, # kept out of browser-context creation (download-dir scoping)+ engine_run_key="wr_1",+ engine_workflow_run_id="wr_1",+ )++ assert seen["run_key"] == "wr_1"+ assert seen["context"].workflow_run_id == "wr_1" # engine flag sees the pinned workflow run+ assert seen["context"].task_id == "tsk_1"++@pytest.mark.asyncioasync def test_pbs_workflow_run_cache_hit_on_second_call() -> None:"""PBS runs must hit the cache on subsequent calls and NOT re-enter the PBS branch."""